blob: 570203990c2f756db505e5196bfc5ff0dcd17dbe
1 | /* |
2 | * Copyright (C) 2017 Amlogic, Inc. All rights reserved. |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
12 | * more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 | * |
18 | * Description: |
19 | */ |
20 | #include <media/v4l2-event.h> |
21 | #include <media/v4l2-mem2mem.h> |
22 | #include <media/videobuf2-dma-contig.h> |
23 | |
24 | #include "aml_vcodec_drv.h" |
25 | #include "aml_vcodec_dec.h" |
26 | //#include "aml_vcodec_intr.h" |
27 | #include "aml_vcodec_util.h" |
28 | #include "vdec_drv_if.h" |
29 | #include "aml_vcodec_dec_pm.h" |
30 | #include <linux/delay.h> |
31 | #include <linux/atomic.h> |
32 | #include <linux/crc32.h> |
33 | #include "aml_vcodec_adapt.h" |
34 | #include <linux/spinlock.h> |
35 | |
36 | #include "aml_vcodec_vfm.h" |
37 | #include "../frame_provider/decoder/utils/decoder_bmmu_box.h" |
38 | #include "../frame_provider/decoder/utils/decoder_mmu_box.h" |
39 | |
40 | #define OUT_FMT_IDX 0 //default h264 |
41 | #define CAP_FMT_IDX 8 //capture nv21 |
42 | |
43 | #define AML_VDEC_MIN_W 64U |
44 | #define AML_VDEC_MIN_H 64U |
45 | #define DFT_CFG_WIDTH AML_VDEC_MIN_W |
46 | #define DFT_CFG_HEIGHT AML_VDEC_MIN_H |
47 | |
48 | #define V4L2_CID_USER_AMLOGIC_BASE (V4L2_CID_USER_BASE + 0x1100) |
49 | #define AML_V4L2_SET_DECMODE (V4L2_CID_USER_AMLOGIC_BASE + 0) |
50 | |
51 | #define WORK_ITEMS_MAX (32) |
52 | |
53 | //#define USEC_PER_SEC 1000000 |
54 | |
55 | #define call_void_memop(vb, op, args...) \ |
56 | do { \ |
57 | if ((vb)->vb2_queue->mem_ops->op) \ |
58 | (vb)->vb2_queue->mem_ops->op(args); \ |
59 | } while (0) |
60 | |
61 | static struct aml_video_fmt aml_video_formats[] = { |
62 | { |
63 | .fourcc = V4L2_PIX_FMT_H264, |
64 | .type = AML_FMT_DEC, |
65 | .num_planes = 1, |
66 | }, |
67 | { |
68 | .fourcc = V4L2_PIX_FMT_HEVC, |
69 | .type = AML_FMT_DEC, |
70 | .num_planes = 1, |
71 | }, |
72 | { |
73 | .fourcc = V4L2_PIX_FMT_VP9, |
74 | .type = AML_FMT_DEC, |
75 | .num_planes = 1, |
76 | }, |
77 | { |
78 | .fourcc = V4L2_PIX_FMT_MPEG1, |
79 | .type = AML_FMT_DEC, |
80 | .num_planes = 1, |
81 | }, |
82 | { |
83 | .fourcc = V4L2_PIX_FMT_MPEG2, |
84 | .type = AML_FMT_DEC, |
85 | .num_planes = 1, |
86 | }, |
87 | { |
88 | .fourcc = V4L2_PIX_FMT_MPEG4, |
89 | .type = AML_FMT_DEC, |
90 | .num_planes = 1, |
91 | }, |
92 | { |
93 | .fourcc = V4L2_PIX_FMT_MJPEG, |
94 | .type = AML_FMT_DEC, |
95 | .num_planes = 1, |
96 | }, |
97 | { |
98 | .fourcc = V4L2_PIX_FMT_NV21, |
99 | .type = AML_FMT_FRAME, |
100 | .num_planes = 1, |
101 | }, |
102 | { |
103 | .fourcc = V4L2_PIX_FMT_NV21M, |
104 | .type = AML_FMT_FRAME, |
105 | .num_planes = 2, |
106 | }, |
107 | { |
108 | .fourcc = V4L2_PIX_FMT_NV12, |
109 | .type = AML_FMT_FRAME, |
110 | .num_planes = 1, |
111 | }, |
112 | { |
113 | .fourcc = V4L2_PIX_FMT_NV12M, |
114 | .type = AML_FMT_FRAME, |
115 | .num_planes = 2, |
116 | }, |
117 | }; |
118 | |
119 | static const struct aml_codec_framesizes aml_vdec_framesizes[] = { |
120 | { |
121 | .fourcc = V4L2_PIX_FMT_H264, |
122 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
123 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
124 | }, |
125 | { |
126 | .fourcc = V4L2_PIX_FMT_HEVC, |
127 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
128 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
129 | }, |
130 | { |
131 | .fourcc = V4L2_PIX_FMT_VP9, |
132 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
133 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
134 | }, |
135 | { |
136 | .fourcc = V4L2_PIX_FMT_MPEG1, |
137 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
138 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
139 | }, |
140 | { |
141 | .fourcc = V4L2_PIX_FMT_MPEG2, |
142 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
143 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
144 | }, |
145 | { |
146 | .fourcc = V4L2_PIX_FMT_MPEG4, |
147 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
148 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
149 | }, |
150 | { |
151 | .fourcc = V4L2_PIX_FMT_MJPEG, |
152 | .stepwise = { AML_VDEC_MIN_W, AML_VDEC_MAX_W, 8, |
153 | AML_VDEC_MIN_H, AML_VDEC_MAX_H, 8 }, |
154 | }, |
155 | }; |
156 | |
157 | #define NUM_SUPPORTED_FRAMESIZE ARRAY_SIZE(aml_vdec_framesizes) |
158 | #define NUM_FORMATS ARRAY_SIZE(aml_video_formats) |
159 | |
160 | extern bool multiplanar; |
161 | |
162 | extern int dmabuf_fd_install_data(int fd, void* data, u32 size); |
163 | extern bool is_v4l2_buf_file(struct file *file); |
164 | |
165 | static ulong aml_vcodec_ctx_lock(struct aml_vcodec_ctx *ctx) |
166 | { |
167 | ulong flags; |
168 | |
169 | spin_lock_irqsave(&ctx->slock, flags); |
170 | |
171 | return flags; |
172 | } |
173 | |
174 | static void aml_vcodec_ctx_unlock(struct aml_vcodec_ctx *ctx, ulong flags) |
175 | { |
176 | spin_unlock_irqrestore(&ctx->slock, flags); |
177 | } |
178 | |
179 | static struct aml_video_fmt *aml_vdec_find_format(struct v4l2_format *f) |
180 | { |
181 | struct aml_video_fmt *fmt; |
182 | unsigned int k; |
183 | |
184 | aml_v4l2_debug(4, "%s, type: %u, planes: %u, fmt: %u\n", |
185 | __func__, f->type, f->fmt.pix_mp.num_planes, |
186 | f->fmt.pix_mp.pixelformat); |
187 | |
188 | for (k = 0; k < NUM_FORMATS; k++) { |
189 | fmt = &aml_video_formats[k]; |
190 | if (fmt->fourcc == f->fmt.pix_mp.pixelformat) |
191 | return fmt; |
192 | } |
193 | |
194 | return NULL; |
195 | } |
196 | |
197 | static struct aml_q_data *aml_vdec_get_q_data(struct aml_vcodec_ctx *ctx, |
198 | enum v4l2_buf_type type) |
199 | { |
200 | if (V4L2_TYPE_IS_OUTPUT(type)) |
201 | return &ctx->q_data[AML_Q_DATA_SRC]; |
202 | |
203 | return &ctx->q_data[AML_Q_DATA_DST]; |
204 | } |
205 | |
206 | void aml_vdec_dispatch_event(struct aml_vcodec_ctx *ctx, u32 changes) |
207 | { |
208 | struct v4l2_event event = {0}; |
209 | |
210 | if (ctx->receive_cmd_stop) { |
211 | ctx->state = AML_STATE_ABORT; |
212 | changes = V4L2_EVENT_REQUEST_EXIT; |
213 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_ABORT)", |
214 | ctx->id, __func__); |
215 | } |
216 | |
217 | switch (changes) { |
218 | case V4L2_EVENT_SRC_CH_RESOLUTION: |
219 | case V4L2_EVENT_SRC_CH_HDRINFO: |
220 | case V4L2_EVENT_REQUEST_RESET: |
221 | case V4L2_EVENT_REQUEST_EXIT: |
222 | event.type = V4L2_EVENT_SOURCE_CHANGE; |
223 | event.u.src_change.changes = changes; |
224 | break; |
225 | default: |
226 | pr_err("unsupport dispatch event %x\n", changes); |
227 | return; |
228 | } |
229 | |
230 | v4l2_event_queue_fh(&ctx->fh, &event); |
231 | aml_v4l2_debug(3, "[%d] %s() changes: %x", |
232 | ctx->id, __func__, changes); |
233 | } |
234 | |
235 | static void aml_vdec_flush_decoder(struct aml_vcodec_ctx *ctx) |
236 | { |
237 | aml_v4l2_debug(3, "[%d] %s() [%d]", ctx->id, __func__, __LINE__); |
238 | |
239 | aml_decoder_flush(ctx->ada_ctx); |
240 | } |
241 | |
242 | static void aml_vdec_pic_info_update(struct aml_vcodec_ctx *ctx) |
243 | { |
244 | unsigned int dpbsize = 0; |
245 | int ret; |
246 | |
247 | if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->last_decoded_picinfo)) { |
248 | aml_v4l2_err("[%d] Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR", ctx->id); |
249 | return; |
250 | } |
251 | |
252 | if (ctx->last_decoded_picinfo.visible_width == 0 || |
253 | ctx->last_decoded_picinfo.visible_height == 0 || |
254 | ctx->last_decoded_picinfo.coded_width == 0 || |
255 | ctx->last_decoded_picinfo.coded_height == 0) { |
256 | aml_v4l2_err("Cannot get correct pic info"); |
257 | return; |
258 | } |
259 | |
260 | /*if ((ctx->last_decoded_picinfo.visible_width == ctx->picinfo.visible_width) || |
261 | (ctx->last_decoded_picinfo.visible_height == ctx->picinfo.visible_height)) |
262 | return;*/ |
263 | |
264 | aml_v4l2_debug(4, "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", |
265 | ctx->id, ctx->last_decoded_picinfo.visible_width, |
266 | ctx->last_decoded_picinfo.visible_height, |
267 | ctx->picinfo.visible_width, ctx->picinfo.visible_height, |
268 | ctx->last_decoded_picinfo.coded_width, |
269 | ctx->last_decoded_picinfo.coded_width); |
270 | |
271 | ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize); |
272 | if (dpbsize == 0) |
273 | aml_v4l2_err("[%d] Incorrect dpb size, ret=%d", ctx->id, ret); |
274 | |
275 | /* update picture information */ |
276 | ctx->dpb_size = dpbsize; |
277 | ctx->picinfo = ctx->last_decoded_picinfo; |
278 | } |
279 | |
280 | void vdec_frame_buffer_release(void *data) |
281 | { |
282 | struct file_privdata *priv_data = |
283 | (struct file_privdata *) data; |
284 | struct vframe_s *vf = &priv_data->vf; |
285 | |
286 | if (decoder_bmmu_box_valide_check(vf->mm_box.bmmu_box)) { |
287 | decoder_bmmu_box_free_idx(vf->mm_box.bmmu_box, |
288 | vf->mm_box.bmmu_idx); |
289 | decoder_bmmu_try_to_release_box(vf->mm_box.bmmu_box); |
290 | } |
291 | |
292 | if (decoder_mmu_box_valide_check(vf->mm_box.mmu_box)) { |
293 | decoder_mmu_box_free_idx(vf->mm_box.mmu_box, |
294 | vf->mm_box.mmu_idx); |
295 | decoder_mmu_try_to_release_box(vf->mm_box.mmu_box); |
296 | } |
297 | |
298 | aml_v4l2_debug(2, "%s vf idx: %d, bmmu idx: %d, bmmu_box: %p", |
299 | __func__, vf->index, vf->mm_box.bmmu_idx, vf->mm_box.bmmu_box); |
300 | aml_v4l2_debug(2, "%s vf idx: %d, mmu_idx: %d, mmu_box: %p", |
301 | __func__, vf->index, vf->mm_box.mmu_idx, vf->mm_box.mmu_box); |
302 | |
303 | memset(data, 0, sizeof(struct file_privdata)); |
304 | kfree(data); |
305 | } |
306 | |
307 | int get_fb_from_queue(struct aml_vcodec_ctx *ctx, struct vdec_v4l2_buffer **out_fb) |
308 | { |
309 | ulong flags; |
310 | struct vb2_buffer *dst_buf = NULL; |
311 | struct vdec_v4l2_buffer *pfb; |
312 | struct aml_video_dec_buf *dst_buf_info, *info; |
313 | struct vb2_v4l2_buffer *dst_vb2_v4l2; |
314 | |
315 | flags = aml_vcodec_ctx_lock(ctx); |
316 | |
317 | if (ctx->state == AML_STATE_ABORT) { |
318 | aml_vcodec_ctx_unlock(ctx, flags); |
319 | return -1; |
320 | } |
321 | |
322 | dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); |
323 | if (!dst_buf) { |
324 | aml_vcodec_ctx_unlock(ctx, flags); |
325 | return -1; |
326 | } |
327 | |
328 | aml_v4l2_debug(2, "[%d] %s() vbuf idx: %d, state: %d, ready: %d", |
329 | ctx->id, __func__, dst_buf->index, dst_buf->state, |
330 | v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx)); |
331 | |
332 | dst_vb2_v4l2 = container_of(dst_buf, struct vb2_v4l2_buffer, vb2_buf); |
333 | dst_buf_info = container_of(dst_vb2_v4l2, struct aml_video_dec_buf, vb); |
334 | |
335 | if (ctx->scatter_mem_enable) { |
336 | pfb = &dst_buf_info->frame_buffer; |
337 | pfb->mem_type = VDEC_SCATTER_MEMORY_TYPE; |
338 | pfb->status = FB_ST_NORMAL; |
339 | } else if (dst_buf->num_planes == 1) { |
340 | pfb = &dst_buf_info->frame_buffer; |
341 | pfb->m.mem[0].dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
342 | pfb->m.mem[0].addr = dma_to_phys(v4l_get_dev_from_codec_mm(), pfb->m.mem[0].dma_addr); |
343 | pfb->m.mem[0].size = ctx->picinfo.y_len_sz + ctx->picinfo.c_len_sz; |
344 | pfb->m.mem[0].offset = ctx->picinfo.y_len_sz; |
345 | pfb->num_planes = dst_buf->num_planes; |
346 | pfb->status = FB_ST_NORMAL; |
347 | |
348 | aml_v4l2_debug(4, "[%d] idx: %u, 1 plane, y:(0x%lx, %d)", |
349 | ctx->id, dst_buf->index, |
350 | pfb->m.mem[0].addr, pfb->m.mem[0].size); |
351 | } else if (dst_buf->num_planes == 2) { |
352 | pfb = &dst_buf_info->frame_buffer; |
353 | pfb->m.mem[0].dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
354 | pfb->m.mem[0].addr = dma_to_phys(v4l_get_dev_from_codec_mm(), pfb->m.mem[0].dma_addr); |
355 | pfb->m.mem[0].size = ctx->picinfo.y_len_sz; |
356 | pfb->m.mem[0].offset = 0; |
357 | |
358 | pfb->m.mem[1].dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1); |
359 | pfb->m.mem[1].addr = dma_to_phys(v4l_get_dev_from_codec_mm(), pfb->m.mem[1].dma_addr); |
360 | pfb->m.mem[1].size = ctx->picinfo.c_len_sz; |
361 | pfb->m.mem[1].offset = ctx->picinfo.c_len_sz >> 1; |
362 | pfb->num_planes = dst_buf->num_planes; |
363 | pfb->status = FB_ST_NORMAL; |
364 | |
365 | aml_v4l2_debug(4, "[%d] idx: %u, 2 planes, y:(0x%lx, %d), c:(0x%lx, %d)", |
366 | ctx->id, dst_buf->index, |
367 | pfb->m.mem[0].addr, pfb->m.mem[0].size, |
368 | pfb->m.mem[1].addr, pfb->m.mem[1].size); |
369 | } else { |
370 | pfb = &dst_buf_info->frame_buffer; |
371 | pfb->m.mem[0].dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 0); |
372 | pfb->m.mem[0].addr = dma_to_phys(v4l_get_dev_from_codec_mm(), pfb->m.mem[0].dma_addr); |
373 | pfb->m.mem[0].size = ctx->picinfo.y_len_sz; |
374 | pfb->m.mem[0].offset = 0; |
375 | |
376 | pfb->m.mem[1].dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 1); |
377 | pfb->m.mem[1].addr = dma_to_phys(v4l_get_dev_from_codec_mm(), pfb->m.mem[2].dma_addr); |
378 | pfb->m.mem[1].size = ctx->picinfo.c_len_sz >> 1; |
379 | pfb->m.mem[1].offset = 0; |
380 | |
381 | pfb->m.mem[2].dma_addr = vb2_dma_contig_plane_dma_addr(dst_buf, 2); |
382 | pfb->m.mem[2].addr = dma_to_phys(v4l_get_dev_from_codec_mm(), pfb->m.mem[3].dma_addr); |
383 | pfb->m.mem[2].size = ctx->picinfo.c_len_sz >> 1; |
384 | pfb->m.mem[2].offset = 0; |
385 | pfb->num_planes = dst_buf->num_planes; |
386 | pfb->status = FB_ST_NORMAL; |
387 | |
388 | aml_v4l2_debug(4, "[%d] idx: %u, 3 planes, y:(0x%lx, %d), u:(0x%lx, %d), v:(0x%lx, %d)", |
389 | ctx->id, dst_buf->index, |
390 | pfb->m.mem[0].addr, pfb->m.mem[0].size, |
391 | pfb->m.mem[1].addr, pfb->m.mem[1].size, |
392 | pfb->m.mem[2].addr, pfb->m.mem[2].size); |
393 | } |
394 | |
395 | dst_buf_info->used = true; |
396 | ctx->buf_used_count++; |
397 | |
398 | *out_fb = pfb; |
399 | |
400 | info = container_of(pfb, struct aml_video_dec_buf, frame_buffer); |
401 | |
402 | v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); |
403 | |
404 | aml_vcodec_ctx_unlock(ctx, flags); |
405 | |
406 | return 0; |
407 | } |
408 | EXPORT_SYMBOL(get_fb_from_queue); |
409 | |
410 | int put_fb_to_queue(struct aml_vcodec_ctx *ctx, struct vdec_v4l2_buffer *in_fb) |
411 | { |
412 | struct aml_video_dec_buf *dstbuf; |
413 | |
414 | pr_info("[%d] %s() [%d]\n", ctx->id, __func__, __LINE__); |
415 | |
416 | if (in_fb == NULL) { |
417 | aml_v4l2_debug(4, "[%d] No free frame buffer", ctx->id); |
418 | return -1; |
419 | } |
420 | |
421 | aml_v4l2_debug(4, "[%d] tmp_frame_addr = 0x%p", ctx->id, in_fb); |
422 | |
423 | dstbuf = container_of(in_fb, struct aml_video_dec_buf, frame_buffer); |
424 | |
425 | mutex_lock(&ctx->lock); |
426 | |
427 | if (!dstbuf->used) |
428 | goto out; |
429 | |
430 | aml_v4l2_debug(4, |
431 | "[%d] status=%x queue id=%d to rdy_queue", |
432 | ctx->id, in_fb->status, |
433 | dstbuf->vb.vb2_buf.index); |
434 | |
435 | v4l2_m2m_buf_queue(ctx->m2m_ctx, &dstbuf->vb); |
436 | |
437 | dstbuf->used = false; |
438 | out: |
439 | mutex_unlock(&ctx->lock); |
440 | |
441 | return 0; |
442 | |
443 | } |
444 | EXPORT_SYMBOL(put_fb_to_queue); |
445 | |
446 | void trans_vframe_to_user(struct aml_vcodec_ctx *ctx, struct vdec_v4l2_buffer *fb) |
447 | { |
448 | struct aml_video_dec_buf *dstbuf = NULL; |
449 | struct vframe_s *vf = (struct vframe_s *)fb->vf_handle; |
450 | |
451 | aml_v4l2_debug(3, "[%d] FROM (%s %s) vf: %p, ts: %llx, idx: %d", |
452 | ctx->id, vf_get_provider(ctx->ada_ctx->recv_name)->name, |
453 | ctx->ada_ctx->vfm_path != FRAME_BASE_PATH_V4L_VIDEO ? "OSD" : "VIDEO", |
454 | vf, vf->timestamp, vf->index); |
455 | |
456 | aml_v4l2_debug(4, "[%d] FROM Y:(%lx, %u) C/U:(%lx, %u) V:(%lx, %u)", |
457 | ctx->id, fb->m.mem[0].addr, fb->m.mem[0].size, |
458 | fb->m.mem[1].addr, fb->m.mem[1].size, |
459 | fb->m.mem[2].addr, fb->m.mem[2].size); |
460 | |
461 | dstbuf = container_of(fb, struct aml_video_dec_buf, frame_buffer); |
462 | if (dstbuf->frame_buffer.num_planes == 1) { |
463 | vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0, fb->m.mem[0].bytes_used); |
464 | } else if (dstbuf->frame_buffer.num_planes == 2) { |
465 | vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0, fb->m.mem[0].bytes_used); |
466 | vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1, fb->m.mem[1].bytes_used); |
467 | } |
468 | dstbuf->vb.vb2_buf.timestamp = vf->timestamp; |
469 | dstbuf->ready_to_display = true; |
470 | |
471 | if (vf->flag & VFRAME_FLAG_EMPTY_FRAME_V4L) { |
472 | dstbuf->lastframe = true; |
473 | dstbuf->vb.flags = V4L2_BUF_FLAG_LAST; |
474 | if (dstbuf->frame_buffer.num_planes == 1) { |
475 | vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0, 0); |
476 | } else if (dstbuf->frame_buffer.num_planes == 2) { |
477 | vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 0, 0); |
478 | vb2_set_plane_payload(&dstbuf->vb.vb2_buf, 1, 0); |
479 | } |
480 | ctx->has_receive_eos = true; |
481 | pr_info("[%d] recevie a empty frame. idx: %d, state: %d\n", |
482 | ctx->id, dstbuf->vb.vb2_buf.index, |
483 | dstbuf->vb.vb2_buf.state); |
484 | } |
485 | |
486 | aml_v4l2_debug(4, "[%d] receive vbuf idx: %d, state: %d", |
487 | ctx->id, dstbuf->vb.vb2_buf.index, |
488 | dstbuf->vb.vb2_buf.state); |
489 | |
490 | if (dstbuf->vb.vb2_buf.state == VB2_BUF_STATE_ACTIVE) { |
491 | /* binding vframe handle. */ |
492 | vf->flag |= VFRAME_FLAG_VIDEO_LINEAR; |
493 | dstbuf->privdata.vf = *vf; |
494 | |
495 | v4l2_m2m_buf_done(&dstbuf->vb, VB2_BUF_STATE_DONE); |
496 | } |
497 | |
498 | mutex_lock(&ctx->state_lock); |
499 | if (ctx->state == AML_STATE_FLUSHING && |
500 | ctx->has_receive_eos) { |
501 | ctx->state = AML_STATE_FLUSHED; |
502 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_FLUSHED)", |
503 | ctx->id, __func__); |
504 | } |
505 | mutex_unlock(&ctx->state_lock); |
506 | |
507 | if (dstbuf->lastframe && |
508 | ctx->q_data[AML_Q_DATA_SRC].resolution_changed) { |
509 | |
510 | /* make the run to stanby until new buffs to enque. */ |
511 | ctx->v4l_codec_dpb_ready = false; |
512 | |
513 | /* |
514 | * After all buffers containing decoded frames from |
515 | * before the resolution change point ready to be |
516 | * dequeued on the CAPTURE queue, the driver sends a |
517 | * V4L2_EVENT_SOURCE_CHANGE event for source change |
518 | * type V4L2_EVENT_SRC_CH_RESOLUTION, also the upper |
519 | * layer will get new information from cts->picinfo. |
520 | */ |
521 | aml_vdec_dispatch_event(ctx, V4L2_EVENT_SRC_CH_RESOLUTION); |
522 | } |
523 | |
524 | ctx->decoded_frame_cnt++; |
525 | } |
526 | |
527 | static int get_display_buffer(struct aml_vcodec_ctx *ctx, struct vdec_v4l2_buffer **out) |
528 | { |
529 | int ret = -1; |
530 | |
531 | aml_v4l2_debug(4, "[%d] %s()", ctx->id, __func__); |
532 | |
533 | ret = vdec_if_get_param(ctx, GET_PARAM_DISP_FRAME_BUFFER, out); |
534 | if (ret) { |
535 | aml_v4l2_err("[%d] Cannot get param : GET_PARAM_DISP_FRAME_BUFFER", ctx->id); |
536 | return -1; |
537 | } |
538 | |
539 | if (!*out) { |
540 | aml_v4l2_debug(4, "[%d] No display frame buffer", ctx->id); |
541 | return -1; |
542 | } |
543 | |
544 | return ret; |
545 | } |
546 | |
547 | static void aml_check_dpb_ready(struct aml_vcodec_ctx *ctx) |
548 | { |
549 | if (!ctx->v4l_codec_dpb_ready) { |
550 | int buf_ready_num; |
551 | |
552 | /* is there enough dst bufs for decoding? */ |
553 | buf_ready_num = v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx); |
554 | if ((ctx->dpb_size) && |
555 | ((buf_ready_num + ctx->buf_used_count) >= ctx->dpb_size)) |
556 | ctx->v4l_codec_dpb_ready = true; |
557 | aml_v4l2_debug(2, "[%d] %s() dpb: %d, ready: %d, used: %d, dpb is ready: %s", |
558 | ctx->id, __func__, ctx->dpb_size, |
559 | buf_ready_num, ctx->buf_used_count, |
560 | ctx->v4l_codec_dpb_ready ? "yes" : "no"); |
561 | } |
562 | } |
563 | |
564 | static int is_vdec_ready(struct aml_vcodec_ctx *ctx) |
565 | { |
566 | struct aml_vcodec_dev *dev = ctx->dev; |
567 | |
568 | if (!is_input_ready(ctx->ada_ctx)) { |
569 | pr_err("[%d] %s() the decoder intput has not ready.\n", |
570 | ctx->id, __func__); |
571 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
572 | return 0; |
573 | } |
574 | |
575 | if (ctx->state == AML_STATE_PROBE) { |
576 | mutex_lock(&ctx->state_lock); |
577 | if (ctx->state == AML_STATE_PROBE) { |
578 | ctx->state = AML_STATE_READY; |
579 | ctx->v4l_codec_ready = true; |
580 | wake_up_interruptible(&ctx->wq); |
581 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_READY)", |
582 | ctx->id, __func__); |
583 | } |
584 | mutex_unlock(&ctx->state_lock); |
585 | } |
586 | |
587 | mutex_lock(&ctx->state_lock); |
588 | if (ctx->state == AML_STATE_READY) { |
589 | if (ctx->m2m_ctx->out_q_ctx.q.streaming && |
590 | ctx->m2m_ctx->cap_q_ctx.q.streaming) { |
591 | ctx->state = AML_STATE_ACTIVE; |
592 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_ACTIVE)", |
593 | ctx->id, __func__); |
594 | } |
595 | } |
596 | mutex_unlock(&ctx->state_lock); |
597 | |
598 | /* check dpb ready */ |
599 | //aml_check_dpb_ready(ctx); |
600 | |
601 | return 1; |
602 | } |
603 | |
604 | static bool is_enough_work_items(struct aml_vcodec_ctx *ctx) |
605 | { |
606 | struct aml_vcodec_dev *dev = ctx->dev; |
607 | |
608 | if (vdec_frame_number(ctx->ada_ctx) >= WORK_ITEMS_MAX) { |
609 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
610 | return false; |
611 | } |
612 | |
613 | return true; |
614 | } |
615 | |
616 | static void aml_wait_dpb_ready(struct aml_vcodec_ctx *ctx) |
617 | { |
618 | ulong expires; |
619 | |
620 | expires = jiffies + msecs_to_jiffies(1000); |
621 | while (!ctx->v4l_codec_dpb_ready) { |
622 | u32 ready_num = 0; |
623 | |
624 | if (time_after(jiffies, expires)) { |
625 | pr_err("the DPB state has not ready.\n"); |
626 | break; |
627 | } |
628 | |
629 | ready_num = v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx); |
630 | if ((ready_num + ctx->buf_used_count) >= ctx->dpb_size) |
631 | ctx->v4l_codec_dpb_ready = true; |
632 | } |
633 | } |
634 | |
635 | static void aml_vdec_worker(struct work_struct *work) |
636 | { |
637 | struct aml_vcodec_ctx *ctx = |
638 | container_of(work, struct aml_vcodec_ctx, decode_work); |
639 | struct aml_vcodec_dev *dev = ctx->dev; |
640 | struct vb2_buffer *src_buf; |
641 | struct aml_vcodec_mem buf; |
642 | bool res_chg = false; |
643 | int ret; |
644 | struct aml_video_dec_buf *src_buf_info; |
645 | struct vb2_v4l2_buffer *src_vb2_v4l2; |
646 | |
647 | aml_v4l2_debug(4, "[%d] entry [%d] [%s]", ctx->id, __LINE__, __func__); |
648 | |
649 | if (ctx->state < AML_STATE_INIT || |
650 | ctx->state > AML_STATE_FLUSHED) { |
651 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
652 | goto out; |
653 | } |
654 | |
655 | if (!is_vdec_ready(ctx)) { |
656 | pr_err("[%d] %s() the decoder has not ready.\n", |
657 | ctx->id, __func__); |
658 | goto out; |
659 | } |
660 | |
661 | src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx); |
662 | if (src_buf == NULL) { |
663 | pr_err("[%d] src_buf empty!\n", ctx->id); |
664 | goto out; |
665 | } |
666 | |
667 | /*this case for google, but some frames are droped on ffmpeg, so disabled temp.*/ |
668 | if (0 && !is_enough_work_items(ctx)) |
669 | goto out; |
670 | |
671 | src_vb2_v4l2 = container_of(src_buf, struct vb2_v4l2_buffer, vb2_buf); |
672 | src_buf_info = container_of(src_vb2_v4l2, struct aml_video_dec_buf, vb); |
673 | |
674 | if (src_buf_info->lastframe) { |
675 | /*the empty data use to flushed the decoder.*/ |
676 | aml_v4l2_debug(2, "[%d] Got empty flush input buffer.", ctx->id); |
677 | |
678 | /* |
679 | * when inputs a small amount of src buff, then soon to |
680 | * switch state FLUSHING, must to wait the DBP to be ready. |
681 | */ |
682 | if (!ctx->v4l_codec_dpb_ready) { |
683 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
684 | goto out; |
685 | } |
686 | |
687 | mutex_lock(&ctx->state_lock); |
688 | if (ctx->state == AML_STATE_ACTIVE) { |
689 | ctx->state = AML_STATE_FLUSHING;// prepare flushing |
690 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_FLUSHING-LASTFRM)", |
691 | ctx->id, __func__); |
692 | } |
693 | mutex_unlock(&ctx->state_lock); |
694 | |
695 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
696 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
697 | |
698 | /* sets eos data for vdec input. */ |
699 | aml_vdec_flush_decoder(ctx); |
700 | |
701 | goto out; |
702 | } |
703 | |
704 | buf.vaddr = vb2_plane_vaddr(src_buf, 0); |
705 | buf.dma_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0); |
706 | |
707 | buf.size = src_buf->planes[0].bytesused; |
708 | if (!buf.vaddr) { |
709 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
710 | aml_v4l2_err("[%d] id=%d src_addr is NULL!!", |
711 | ctx->id, src_buf->index); |
712 | goto out; |
713 | } |
714 | |
715 | src_buf_info->used = true; |
716 | |
717 | /* pr_err("%s() [%d], size: 0x%zx, crc: 0x%x\n", |
718 | __func__, __LINE__, buf.size, crc32(0, buf.va, buf.size));*/ |
719 | |
720 | /* pts = (time / 10e6) * (90k / fps) */ |
721 | aml_v4l2_debug(4, "[%d] %s() timestamp: 0x%llx", ctx->id , __func__, |
722 | src_buf->timestamp); |
723 | |
724 | ret = vdec_if_decode(ctx, &buf, src_buf->timestamp, &res_chg); |
725 | if (ret > 0) { |
726 | /* |
727 | * we only return src buffer with VB2_BUF_STATE_DONE |
728 | * when decode success without resolution change. |
729 | */ |
730 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
731 | v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_DONE); |
732 | } else if (ret && ret != -EAGAIN) { |
733 | src_buf_info->error = (ret == -EIO ? true : false); |
734 | src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
735 | v4l2_m2m_buf_done(&src_buf_info->vb, VB2_BUF_STATE_ERROR); |
736 | aml_v4l2_err("[%d] %s() error processing src data. %d.", |
737 | ctx->id, __func__, ret); |
738 | } else if (res_chg) { |
739 | /* wait the DPB state to be ready. */ |
740 | aml_wait_dpb_ready(ctx); |
741 | |
742 | src_buf_info->used = false; |
743 | aml_vdec_pic_info_update(ctx); |
744 | /* |
745 | * On encountering a resolution change in the stream. |
746 | * The driver must first process and decode all |
747 | * remaining buffers from before the resolution change |
748 | * point, so call flush decode here |
749 | */ |
750 | mutex_lock(&ctx->state_lock); |
751 | if (ctx->state == AML_STATE_ACTIVE) { |
752 | ctx->state = AML_STATE_FLUSHING;// prepare flushing |
753 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_FLUSHING-RESCHG)", |
754 | ctx->id, __func__); |
755 | } |
756 | mutex_unlock(&ctx->state_lock); |
757 | |
758 | ctx->q_data[AML_Q_DATA_SRC].resolution_changed = true; |
759 | v4l2_m2m_job_pause(dev->m2m_dev_dec, ctx->m2m_ctx); |
760 | |
761 | aml_vdec_flush_decoder(ctx); |
762 | |
763 | goto out; |
764 | } |
765 | |
766 | v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx); |
767 | out: |
768 | return; |
769 | } |
770 | |
771 | static void aml_vdec_reset(struct aml_vcodec_ctx *ctx) |
772 | { |
773 | if (ctx->state == AML_STATE_ABORT) { |
774 | pr_err("[%d] %s() the decoder will be exited.\n", |
775 | ctx->id, __func__); |
776 | goto out; |
777 | } |
778 | |
779 | if (aml_codec_reset(ctx->ada_ctx, &ctx->reset_flag)) { |
780 | ctx->state = AML_STATE_ABORT; |
781 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_ABORT)", |
782 | ctx->id, __func__); |
783 | goto out; |
784 | } |
785 | |
786 | if (ctx->state == AML_STATE_RESET) { |
787 | ctx->state = AML_STATE_PROBE; |
788 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_PROBE)", |
789 | ctx->id, __func__); |
790 | |
791 | aml_v4l2_debug(0, "[%d] %s() dpb: %d, ready: %d, used: %d", |
792 | ctx->id, __func__, ctx->dpb_size, |
793 | v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx), ctx->buf_used_count); |
794 | |
795 | /* vdec has ready to decode subsequence data of new resolution. */ |
796 | ctx->q_data[AML_Q_DATA_SRC].resolution_changed = false; |
797 | v4l2_m2m_job_resume(ctx->dev->m2m_dev_dec, ctx->m2m_ctx); |
798 | } |
799 | |
800 | out: |
801 | complete(&ctx->comp); |
802 | return; |
803 | } |
804 | |
805 | void wait_vcodec_ending(struct aml_vcodec_ctx *ctx) |
806 | { |
807 | struct aml_vcodec_dev *dev = ctx->dev; |
808 | |
809 | /* pause inject output data to vdec. */ |
810 | v4l2_m2m_job_pause(dev->m2m_dev_dec, ctx->m2m_ctx); |
811 | |
812 | /* flush worker. */ |
813 | flush_workqueue(dev->decode_workqueue); |
814 | |
815 | ctx->v4l_codec_ready = false; |
816 | ctx->v4l_codec_dpb_ready = false; |
817 | |
818 | /* stop decoder. */ |
819 | if (ctx->state > AML_STATE_INIT) |
820 | aml_vdec_reset(ctx); |
821 | } |
822 | |
823 | void try_to_capture(struct aml_vcodec_ctx *ctx) |
824 | { |
825 | int ret = 0; |
826 | struct vdec_v4l2_buffer *fb = NULL; |
827 | |
828 | ret = get_display_buffer(ctx, &fb); |
829 | if (ret) { |
830 | aml_v4l2_debug(4, "[%d] %s() [%d], the que have no disp buf,ret: %d", |
831 | ctx->id, __func__, __LINE__, ret); |
832 | return; |
833 | } |
834 | |
835 | trans_vframe_to_user(ctx, fb); |
836 | } |
837 | EXPORT_SYMBOL_GPL(try_to_capture); |
838 | |
839 | static int vdec_thread(void *data) |
840 | { |
841 | struct sched_param param = |
842 | {.sched_priority = MAX_RT_PRIO / 2}; |
843 | struct aml_vdec_thread *thread = |
844 | (struct aml_vdec_thread *) data; |
845 | struct aml_vcodec_ctx *ctx = |
846 | (struct aml_vcodec_ctx *) thread->priv; |
847 | |
848 | sched_setscheduler(current, SCHED_FIFO, ¶m); |
849 | |
850 | for (;;) { |
851 | aml_v4l2_debug(3, "[%d] %s() state: %d", ctx->id, |
852 | __func__, ctx->state); |
853 | |
854 | if (down_interruptible(&thread->sem)) |
855 | break; |
856 | |
857 | if (thread->stop) |
858 | break; |
859 | |
860 | /* handle event. */ |
861 | thread->func(ctx); |
862 | } |
863 | |
864 | while (!kthread_should_stop()) { |
865 | set_current_state(TASK_INTERRUPTIBLE); |
866 | schedule(); |
867 | } |
868 | |
869 | return 0; |
870 | } |
871 | |
872 | void aml_thread_notify(struct aml_vcodec_ctx *ctx, |
873 | enum aml_thread_type type) |
874 | { |
875 | struct aml_vdec_thread *thread = NULL; |
876 | |
877 | list_for_each_entry(thread, &ctx->vdec_thread_list, node) { |
878 | if (thread->task == NULL) |
879 | continue; |
880 | |
881 | if (thread->type == type) |
882 | up(&thread->sem); |
883 | } |
884 | } |
885 | EXPORT_SYMBOL_GPL(aml_thread_notify); |
886 | |
887 | int aml_thread_start(struct aml_vcodec_ctx *ctx, aml_thread_func func, |
888 | enum aml_thread_type type, const char *thread_name) |
889 | { |
890 | struct aml_vdec_thread *thread; |
891 | int ret = 0; |
892 | |
893 | thread = kzalloc(sizeof(*thread), GFP_KERNEL); |
894 | if (thread == NULL) |
895 | return -ENOMEM; |
896 | |
897 | thread->type = type; |
898 | thread->func = func; |
899 | thread->priv = ctx; |
900 | sema_init(&thread->sem, 0); |
901 | |
902 | thread->task = kthread_run(vdec_thread, thread, "aml-%s", thread_name); |
903 | if (IS_ERR(thread->task)) { |
904 | ret = PTR_ERR(thread->task); |
905 | thread->task = NULL; |
906 | goto err; |
907 | } |
908 | |
909 | list_add(&thread->node, &ctx->vdec_thread_list); |
910 | |
911 | return 0; |
912 | |
913 | err: |
914 | kfree(thread); |
915 | |
916 | return ret; |
917 | } |
918 | EXPORT_SYMBOL_GPL(aml_thread_start); |
919 | |
920 | void aml_thread_stop(struct aml_vcodec_ctx *ctx) |
921 | { |
922 | struct aml_vdec_thread *thread = NULL; |
923 | |
924 | while (!list_empty(&ctx->vdec_thread_list)) { |
925 | thread = list_entry(ctx->vdec_thread_list.next, |
926 | struct aml_vdec_thread, node); |
927 | list_del(&thread->node); |
928 | thread->stop = true; |
929 | up(&thread->sem); |
930 | kthread_stop(thread->task); |
931 | thread->task = NULL; |
932 | kfree(thread); |
933 | } |
934 | } |
935 | EXPORT_SYMBOL_GPL(aml_thread_stop); |
936 | |
937 | static int vidioc_try_decoder_cmd(struct file *file, void *priv, |
938 | struct v4l2_decoder_cmd *cmd) |
939 | { |
940 | switch (cmd->cmd) { |
941 | case V4L2_DEC_CMD_STOP: |
942 | case V4L2_DEC_CMD_START: |
943 | if (cmd->flags != 0) { |
944 | aml_v4l2_err("cmd->flags=%u", cmd->flags); |
945 | return -EINVAL; |
946 | } |
947 | break; |
948 | default: |
949 | return -EINVAL; |
950 | } |
951 | |
952 | return 0; |
953 | } |
954 | |
955 | static int vidioc_decoder_cmd(struct file *file, void *priv, |
956 | struct v4l2_decoder_cmd *cmd) |
957 | { |
958 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
959 | struct vb2_queue *src_vq, *dst_vq; |
960 | int ret; |
961 | |
962 | ret = vidioc_try_decoder_cmd(file, priv, cmd); |
963 | if (ret) |
964 | return ret; |
965 | |
966 | aml_v4l2_debug(2, "[%d] %s() [%d], cmd: %u", |
967 | ctx->id, __func__, __LINE__, cmd->cmd); |
968 | dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, |
969 | multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : |
970 | V4L2_BUF_TYPE_VIDEO_CAPTURE); |
971 | switch (cmd->cmd) { |
972 | case V4L2_DEC_CMD_STOP: |
973 | if (ctx->state != AML_STATE_ACTIVE) { |
974 | if (ctx->state >= AML_STATE_IDLE && |
975 | ctx->state <= AML_STATE_PROBE) { |
976 | ctx->state = AML_STATE_ABORT; |
977 | aml_vdec_dispatch_event(ctx, V4L2_EVENT_REQUEST_EXIT); |
978 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_ABORT)", |
979 | ctx->id, __func__); |
980 | return 0; |
981 | } |
982 | } |
983 | |
984 | src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx, |
985 | V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); |
986 | if (!vb2_is_streaming(src_vq)) { |
987 | pr_err("[%d] Output stream is off. No need to flush.\n", ctx->id); |
988 | return 0; |
989 | } |
990 | |
991 | if (!vb2_is_streaming(dst_vq)) { |
992 | pr_err("[%d] Capture stream is off. No need to flush.\n", ctx->id); |
993 | return 0; |
994 | } |
995 | |
996 | /* flush src */ |
997 | v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf->vb); |
998 | v4l2_m2m_try_schedule(ctx->m2m_ctx);//pay attention |
999 | ctx->receive_cmd_stop = true; |
1000 | break; |
1001 | |
1002 | case V4L2_DEC_CMD_START: |
1003 | aml_v4l2_debug(4, "[%d] CMD V4L2_DEC_CMD_START ", ctx->id); |
1004 | vb2_clear_last_buffer_dequeued(dst_vq);//pay attention |
1005 | break; |
1006 | |
1007 | default: |
1008 | return -EINVAL; |
1009 | } |
1010 | |
1011 | return 0; |
1012 | } |
1013 | |
1014 | static int vidioc_decoder_streamon(struct file *file, void *priv, |
1015 | enum v4l2_buf_type i) |
1016 | { |
1017 | struct v4l2_fh *fh = file->private_data; |
1018 | struct aml_vcodec_ctx *ctx = fh_to_ctx(fh); |
1019 | struct vb2_queue *q; |
1020 | |
1021 | q = v4l2_m2m_get_vq(fh->m2m_ctx, i); |
1022 | if (!V4L2_TYPE_IS_OUTPUT(q->type)) { |
1023 | if (ctx->is_stream_off) { |
1024 | mutex_lock(&ctx->state_lock); |
1025 | if ((ctx->state == AML_STATE_ACTIVE || |
1026 | ctx->state == AML_STATE_FLUSHING || |
1027 | ctx->state == AML_STATE_FLUSHED) || |
1028 | (ctx->reset_flag == 2)) { |
1029 | ctx->state = AML_STATE_RESET; |
1030 | ctx->v4l_codec_ready = false; |
1031 | ctx->v4l_codec_dpb_ready = false; |
1032 | |
1033 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_RESET)", |
1034 | ctx->id, __func__); |
1035 | aml_vdec_reset(ctx); |
1036 | } |
1037 | mutex_unlock(&ctx->state_lock); |
1038 | |
1039 | ctx->is_stream_off = false; |
1040 | } |
1041 | } |
1042 | return v4l2_m2m_ioctl_streamon(file, priv, i); |
1043 | } |
1044 | |
1045 | static int vidioc_decoder_streamoff(struct file *file, void *priv, |
1046 | enum v4l2_buf_type i) |
1047 | { |
1048 | struct v4l2_fh *fh = file->private_data; |
1049 | struct aml_vcodec_ctx *ctx = fh_to_ctx(fh); |
1050 | struct vb2_queue *q; |
1051 | |
1052 | q = v4l2_m2m_get_vq(fh->m2m_ctx, i); |
1053 | if (!V4L2_TYPE_IS_OUTPUT(q->type)) { |
1054 | ctx->is_stream_off = true; |
1055 | } |
1056 | |
1057 | return v4l2_m2m_ioctl_streamoff(file, priv, i); |
1058 | } |
1059 | |
1060 | static int vidioc_decoder_reqbufs(struct file *file, void *priv, |
1061 | struct v4l2_requestbuffers *rb) |
1062 | { |
1063 | struct v4l2_fh *fh = file->private_data; |
1064 | struct vb2_queue *q; |
1065 | |
1066 | q = v4l2_m2m_get_vq(fh->m2m_ctx, rb->type); |
1067 | |
1068 | if (!rb->count) |
1069 | vb2_queue_release(q); |
1070 | |
1071 | return v4l2_m2m_ioctl_reqbufs(file, priv, rb); |
1072 | } |
1073 | |
1074 | void aml_vcodec_dec_release(struct aml_vcodec_ctx *ctx) |
1075 | { |
1076 | ulong flags; |
1077 | |
1078 | flags = aml_vcodec_ctx_lock(ctx); |
1079 | ctx->state = AML_STATE_ABORT; |
1080 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_ABORT)", |
1081 | ctx->id, __func__); |
1082 | aml_vcodec_ctx_unlock(ctx, flags); |
1083 | |
1084 | vdec_if_deinit(ctx); |
1085 | } |
1086 | |
1087 | void aml_vcodec_dec_set_default_params(struct aml_vcodec_ctx *ctx) |
1088 | { |
1089 | struct aml_q_data *q_data; |
1090 | |
1091 | ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex; |
1092 | ctx->fh.m2m_ctx = ctx->m2m_ctx; |
1093 | ctx->fh.ctrl_handler = &ctx->ctrl_hdl; |
1094 | INIT_WORK(&ctx->decode_work, aml_vdec_worker); |
1095 | ctx->colorspace = V4L2_COLORSPACE_REC709; |
1096 | ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; |
1097 | ctx->quantization = V4L2_QUANTIZATION_DEFAULT; |
1098 | ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; |
1099 | ctx->dev->dec_capability = 0;//VCODEC_CAPABILITY_4K_DISABLED;//disable 4k |
1100 | |
1101 | q_data = &ctx->q_data[AML_Q_DATA_SRC]; |
1102 | memset(q_data, 0, sizeof(struct aml_q_data)); |
1103 | q_data->visible_width = DFT_CFG_WIDTH; |
1104 | q_data->visible_height = DFT_CFG_HEIGHT; |
1105 | q_data->fmt = &aml_video_formats[OUT_FMT_IDX]; |
1106 | q_data->field = V4L2_FIELD_NONE; |
1107 | |
1108 | q_data->sizeimage[0] = (1024 * 1024);//DFT_CFG_WIDTH * DFT_CFG_HEIGHT; //1m |
1109 | q_data->bytesperline[0] = 0; |
1110 | |
1111 | q_data = &ctx->q_data[AML_Q_DATA_DST]; |
1112 | memset(q_data, 0, sizeof(struct aml_q_data)); |
1113 | q_data->visible_width = DFT_CFG_WIDTH; |
1114 | q_data->visible_height = DFT_CFG_HEIGHT; |
1115 | q_data->coded_width = DFT_CFG_WIDTH; |
1116 | q_data->coded_height = DFT_CFG_HEIGHT; |
1117 | q_data->fmt = &aml_video_formats[CAP_FMT_IDX]; |
1118 | q_data->field = V4L2_FIELD_NONE; |
1119 | |
1120 | v4l_bound_align_image(&q_data->coded_width, |
1121 | AML_VDEC_MIN_W, |
1122 | AML_VDEC_MAX_W, 4, |
1123 | &q_data->coded_height, |
1124 | AML_VDEC_MIN_H, |
1125 | AML_VDEC_MAX_H, 5, 6); |
1126 | |
1127 | q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height; |
1128 | q_data->bytesperline[0] = q_data->coded_width; |
1129 | q_data->sizeimage[1] = q_data->sizeimage[0] / 2; |
1130 | q_data->bytesperline[1] = q_data->coded_width; |
1131 | |
1132 | ctx->state = AML_STATE_IDLE; |
1133 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_IDLE)", |
1134 | ctx->id, __func__); |
1135 | } |
1136 | |
1137 | static int vidioc_vdec_qbuf(struct file *file, void *priv, |
1138 | struct v4l2_buffer *buf) |
1139 | { |
1140 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1141 | |
1142 | if (ctx->state == AML_STATE_ABORT) { |
1143 | aml_v4l2_err("[%d] Call on QBUF after unrecoverable error, type = %s", |
1144 | ctx->id, V4L2_TYPE_IS_OUTPUT(buf->type) ? |
1145 | "OUT" : "IN"); |
1146 | return -EIO; |
1147 | } |
1148 | |
1149 | return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); |
1150 | } |
1151 | |
1152 | static int vidioc_vdec_dqbuf(struct file *file, void *priv, |
1153 | struct v4l2_buffer *buf) |
1154 | { |
1155 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1156 | int ret; |
1157 | |
1158 | if (ctx->state == AML_STATE_ABORT) { |
1159 | aml_v4l2_err("[%d] Call on DQBUF after unrecoverable error, type = %s", |
1160 | ctx->id, V4L2_TYPE_IS_OUTPUT(buf->type) ? |
1161 | "OUT" : "IN"); |
1162 | if (!V4L2_TYPE_IS_OUTPUT(buf->type)) |
1163 | return -EIO; |
1164 | } |
1165 | |
1166 | ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); |
1167 | |
1168 | if (!ret && !V4L2_TYPE_IS_OUTPUT(buf->type)) { |
1169 | struct vb2_queue *vq; |
1170 | struct vb2_v4l2_buffer *vb2_v4l2 = NULL; |
1171 | struct aml_video_dec_buf *aml_buf = NULL; |
1172 | struct file *file = NULL; |
1173 | |
1174 | mutex_lock(&ctx->lock); |
1175 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type); |
1176 | vb2_v4l2 = to_vb2_v4l2_buffer(vq->bufs[buf->index]); |
1177 | aml_buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb); |
1178 | |
1179 | file = fget(vb2_v4l2->private); |
1180 | if (is_v4l2_buf_file(file) && |
1181 | !aml_buf->privdata.is_install) { |
1182 | dmabuf_fd_install_data(vb2_v4l2->private, |
1183 | (void*)&aml_buf->privdata, |
1184 | sizeof(struct file_privdata)); |
1185 | aml_buf->privdata.is_install = true; |
1186 | } |
1187 | fput(file); |
1188 | mutex_unlock(&ctx->lock); |
1189 | } |
1190 | |
1191 | return ret; |
1192 | } |
1193 | |
1194 | static int vidioc_vdec_querycap(struct file *file, void *priv, |
1195 | struct v4l2_capability *cap) |
1196 | { |
1197 | strlcpy(cap->driver, AML_VCODEC_DEC_NAME, sizeof(cap->driver)); |
1198 | strlcpy(cap->bus_info, AML_PLATFORM_STR, sizeof(cap->bus_info)); |
1199 | strlcpy(cap->card, AML_PLATFORM_STR, sizeof(cap->card)); |
1200 | |
1201 | return 0; |
1202 | } |
1203 | |
1204 | static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh, |
1205 | const struct v4l2_event_subscription *sub) |
1206 | { |
1207 | switch (sub->type) { |
1208 | case V4L2_EVENT_EOS: |
1209 | return v4l2_event_subscribe(fh, sub, 2, NULL); |
1210 | case V4L2_EVENT_SOURCE_CHANGE: |
1211 | return v4l2_src_change_event_subscribe(fh, sub); |
1212 | default: |
1213 | return v4l2_ctrl_subscribe_event(fh, sub); |
1214 | } |
1215 | } |
1216 | |
1217 | static int vidioc_try_fmt(struct v4l2_format *f, struct aml_video_fmt *fmt) |
1218 | { |
1219 | struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; |
1220 | int i; |
1221 | |
1222 | pix_fmt_mp->field = V4L2_FIELD_NONE; |
1223 | |
1224 | if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { |
1225 | pix_fmt_mp->num_planes = 1; |
1226 | pix_fmt_mp->plane_fmt[0].bytesperline = 0; |
1227 | } else if (!V4L2_TYPE_IS_OUTPUT(f->type)) { |
1228 | int tmp_w, tmp_h; |
1229 | |
1230 | pix_fmt_mp->height = clamp(pix_fmt_mp->height, |
1231 | AML_VDEC_MIN_H, |
1232 | AML_VDEC_MAX_H); |
1233 | pix_fmt_mp->width = clamp(pix_fmt_mp->width, |
1234 | AML_VDEC_MIN_W, |
1235 | AML_VDEC_MAX_W); |
1236 | |
1237 | /* |
1238 | * Find next closer width align 64, heign align 64, size align |
1239 | * 64 rectangle |
1240 | * Note: This only get default value, the real HW needed value |
1241 | * only available when ctx in AML_STATE_PROBE state |
1242 | */ |
1243 | tmp_w = pix_fmt_mp->width; |
1244 | tmp_h = pix_fmt_mp->height; |
1245 | v4l_bound_align_image(&pix_fmt_mp->width, |
1246 | AML_VDEC_MIN_W, |
1247 | AML_VDEC_MAX_W, 6, |
1248 | &pix_fmt_mp->height, |
1249 | AML_VDEC_MIN_H, |
1250 | AML_VDEC_MAX_H, 6, 9); |
1251 | |
1252 | if (pix_fmt_mp->width < tmp_w && |
1253 | (pix_fmt_mp->width + 64) <= AML_VDEC_MAX_W) |
1254 | pix_fmt_mp->width += 64; |
1255 | if (pix_fmt_mp->height < tmp_h && |
1256 | (pix_fmt_mp->height + 64) <= AML_VDEC_MAX_H) |
1257 | pix_fmt_mp->height += 64; |
1258 | |
1259 | aml_v4l2_debug(4, |
1260 | "before resize width=%d, height=%d, after resize width=%d, height=%d, sizeimage=%d", |
1261 | tmp_w, tmp_h, pix_fmt_mp->width, |
1262 | pix_fmt_mp->height, |
1263 | pix_fmt_mp->width * pix_fmt_mp->height); |
1264 | |
1265 | pix_fmt_mp->num_planes = fmt->num_planes; |
1266 | pix_fmt_mp->plane_fmt[0].sizeimage = |
1267 | pix_fmt_mp->width * pix_fmt_mp->height; |
1268 | pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width; |
1269 | |
1270 | if (pix_fmt_mp->num_planes == 2) { |
1271 | pix_fmt_mp->plane_fmt[1].sizeimage = |
1272 | (pix_fmt_mp->width * pix_fmt_mp->height) / 2; |
1273 | pix_fmt_mp->plane_fmt[1].bytesperline = |
1274 | pix_fmt_mp->width; |
1275 | } |
1276 | } |
1277 | |
1278 | for (i = 0; i < pix_fmt_mp->num_planes; i++) |
1279 | memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0, |
1280 | sizeof(pix_fmt_mp->plane_fmt[0].reserved)); |
1281 | |
1282 | pix_fmt_mp->flags = 0; |
1283 | memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved)); |
1284 | return 0; |
1285 | } |
1286 | |
1287 | static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv, |
1288 | struct v4l2_format *f) |
1289 | { |
1290 | struct aml_video_fmt *fmt = NULL; |
1291 | |
1292 | fmt = aml_vdec_find_format(f); |
1293 | if (!fmt) |
1294 | return -EINVAL; |
1295 | |
1296 | return vidioc_try_fmt(f, fmt); |
1297 | } |
1298 | |
1299 | static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv, |
1300 | struct v4l2_format *f) |
1301 | { |
1302 | struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp; |
1303 | struct aml_video_fmt *fmt = NULL; |
1304 | |
1305 | fmt = aml_vdec_find_format(f); |
1306 | if (!fmt) |
1307 | return -EINVAL; |
1308 | |
1309 | if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) { |
1310 | aml_v4l2_err("sizeimage of output format must be given"); |
1311 | return -EINVAL; |
1312 | } |
1313 | |
1314 | return vidioc_try_fmt(f, fmt); |
1315 | } |
1316 | |
1317 | static int vidioc_vdec_g_selection(struct file *file, void *priv, |
1318 | struct v4l2_selection *s) |
1319 | { |
1320 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1321 | struct aml_q_data *q_data; |
1322 | |
1323 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
1324 | return -EINVAL; |
1325 | |
1326 | q_data = &ctx->q_data[AML_Q_DATA_DST]; |
1327 | |
1328 | switch (s->target) { |
1329 | case V4L2_SEL_TGT_COMPOSE_DEFAULT: |
1330 | s->r.left = 0; |
1331 | s->r.top = 0; |
1332 | s->r.width = ctx->picinfo.visible_width; |
1333 | s->r.height = ctx->picinfo.visible_height; |
1334 | break; |
1335 | case V4L2_SEL_TGT_COMPOSE_BOUNDS: |
1336 | s->r.left = 0; |
1337 | s->r.top = 0; |
1338 | s->r.width = ctx->picinfo.coded_width; |
1339 | s->r.height = ctx->picinfo.coded_height; |
1340 | break; |
1341 | case V4L2_SEL_TGT_COMPOSE: |
1342 | if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) { |
1343 | /* set to default value if header info not ready yet*/ |
1344 | s->r.left = 0; |
1345 | s->r.top = 0; |
1346 | s->r.width = q_data->visible_width; |
1347 | s->r.height = q_data->visible_height; |
1348 | } |
1349 | break; |
1350 | default: |
1351 | return -EINVAL; |
1352 | } |
1353 | |
1354 | if (ctx->state < AML_STATE_PROBE) { |
1355 | /* set to default value if header info not ready yet*/ |
1356 | s->r.left = 0; |
1357 | s->r.top = 0; |
1358 | s->r.width = q_data->visible_width; |
1359 | s->r.height = q_data->visible_height; |
1360 | return 0; |
1361 | } |
1362 | |
1363 | return 0; |
1364 | } |
1365 | |
1366 | static int vidioc_vdec_s_selection(struct file *file, void *priv, |
1367 | struct v4l2_selection *s) |
1368 | { |
1369 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1370 | |
1371 | if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
1372 | return -EINVAL; |
1373 | |
1374 | switch (s->target) { |
1375 | case V4L2_SEL_TGT_COMPOSE: |
1376 | s->r.left = 0; |
1377 | s->r.top = 0; |
1378 | s->r.width = ctx->picinfo.visible_width; |
1379 | s->r.height = ctx->picinfo.visible_height; |
1380 | break; |
1381 | default: |
1382 | return -EINVAL; |
1383 | } |
1384 | |
1385 | return 0; |
1386 | } |
1387 | |
1388 | static int vidioc_vdec_s_fmt(struct file *file, void *priv, |
1389 | struct v4l2_format *f) |
1390 | { |
1391 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1392 | struct v4l2_pix_format_mplane *pix_mp; |
1393 | struct aml_q_data *q_data; |
1394 | int ret = 0; |
1395 | struct aml_video_fmt *fmt; |
1396 | |
1397 | aml_v4l2_debug(4, "[%d] %s()", ctx->id, __func__); |
1398 | |
1399 | q_data = aml_vdec_get_q_data(ctx, f->type); |
1400 | if (!q_data) |
1401 | return -EINVAL; |
1402 | |
1403 | pix_mp = &f->fmt.pix_mp; |
1404 | if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) && |
1405 | vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) { |
1406 | aml_v4l2_err("[%d] out_q_ctx buffers already requested", ctx->id); |
1407 | } |
1408 | |
1409 | if ((!V4L2_TYPE_IS_OUTPUT(f->type)) && |
1410 | vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) { |
1411 | aml_v4l2_err("[%d] cap_q_ctx buffers already requested", ctx->id); |
1412 | } |
1413 | |
1414 | fmt = aml_vdec_find_format(f); |
1415 | if (fmt == NULL) { |
1416 | if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { |
1417 | f->fmt.pix.pixelformat = |
1418 | aml_video_formats[OUT_FMT_IDX].fourcc; |
1419 | fmt = aml_vdec_find_format(f); |
1420 | } else if (!V4L2_TYPE_IS_OUTPUT(f->type)) { |
1421 | f->fmt.pix.pixelformat = |
1422 | aml_video_formats[CAP_FMT_IDX].fourcc; |
1423 | fmt = aml_vdec_find_format(f); |
1424 | } |
1425 | } |
1426 | |
1427 | q_data->fmt = fmt; |
1428 | vidioc_try_fmt(f, q_data->fmt); |
1429 | if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { |
1430 | q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage; |
1431 | q_data->coded_width = pix_mp->width; |
1432 | q_data->coded_height = pix_mp->height; |
1433 | |
1434 | aml_v4l2_debug(4, "[%d] %s() [%d], w: %d, h: %d, size: %d", |
1435 | ctx->id, __func__, __LINE__, pix_mp->width, pix_mp->height, |
1436 | pix_mp->plane_fmt[0].sizeimage); |
1437 | |
1438 | ctx->colorspace = f->fmt.pix_mp.colorspace; |
1439 | ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc; |
1440 | ctx->quantization = f->fmt.pix_mp.quantization; |
1441 | ctx->xfer_func = f->fmt.pix_mp.xfer_func; |
1442 | |
1443 | mutex_lock(&ctx->state_lock); |
1444 | if (ctx->state == AML_STATE_IDLE) { |
1445 | ret = vdec_if_init(ctx, q_data->fmt->fourcc); |
1446 | if (ret) { |
1447 | aml_v4l2_err("[%d]: vdec_if_init() fail ret=%d", |
1448 | ctx->id, ret); |
1449 | mutex_unlock(&ctx->state_lock); |
1450 | return -EINVAL; |
1451 | } |
1452 | ctx->state = AML_STATE_INIT; |
1453 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_INIT)", |
1454 | ctx->id, __func__); |
1455 | } |
1456 | mutex_unlock(&ctx->state_lock); |
1457 | } |
1458 | |
1459 | return 0; |
1460 | } |
1461 | |
1462 | static int vidioc_enum_framesizes(struct file *file, void *priv, |
1463 | struct v4l2_frmsizeenum *fsize) |
1464 | { |
1465 | int i = 0; |
1466 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1467 | |
1468 | if (fsize->index != 0) |
1469 | return -EINVAL; |
1470 | |
1471 | for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) { |
1472 | if (fsize->pixel_format != aml_vdec_framesizes[i].fourcc) |
1473 | continue; |
1474 | |
1475 | fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; |
1476 | fsize->stepwise = aml_vdec_framesizes[i].stepwise; |
1477 | if (!(ctx->dev->dec_capability & |
1478 | VCODEC_CAPABILITY_4K_DISABLED)) { |
1479 | aml_v4l2_debug(4, "[%d] 4K is enabled", ctx->id); |
1480 | fsize->stepwise.max_width = |
1481 | VCODEC_DEC_4K_CODED_WIDTH; |
1482 | fsize->stepwise.max_height = |
1483 | VCODEC_DEC_4K_CODED_HEIGHT; |
1484 | } |
1485 | aml_v4l2_debug(4, "[%d] %x, %d %d %d %d %d %d", |
1486 | ctx->id, ctx->dev->dec_capability, |
1487 | fsize->stepwise.min_width, |
1488 | fsize->stepwise.max_width, |
1489 | fsize->stepwise.step_width, |
1490 | fsize->stepwise.min_height, |
1491 | fsize->stepwise.max_height, |
1492 | fsize->stepwise.step_height); |
1493 | return 0; |
1494 | } |
1495 | |
1496 | return -EINVAL; |
1497 | } |
1498 | |
1499 | static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue) |
1500 | { |
1501 | struct aml_video_fmt *fmt; |
1502 | int i, j = 0; |
1503 | |
1504 | for (i = 0; i < NUM_FORMATS; i++) { |
1505 | if (output_queue && (aml_video_formats[i].type != AML_FMT_DEC)) |
1506 | continue; |
1507 | if (!output_queue && (aml_video_formats[i].type != AML_FMT_FRAME)) |
1508 | continue; |
1509 | |
1510 | if (j == f->index) { |
1511 | fmt = &aml_video_formats[i]; |
1512 | f->pixelformat = fmt->fourcc; |
1513 | return 0; |
1514 | } |
1515 | ++j; |
1516 | } |
1517 | |
1518 | return -EINVAL; |
1519 | } |
1520 | |
1521 | static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file, void *pirv, |
1522 | struct v4l2_fmtdesc *f) |
1523 | { |
1524 | return vidioc_enum_fmt(f, false); |
1525 | } |
1526 | |
1527 | static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file, void *priv, |
1528 | struct v4l2_fmtdesc *f) |
1529 | { |
1530 | return vidioc_enum_fmt(f, true); |
1531 | } |
1532 | |
1533 | static int vidioc_vdec_g_fmt(struct file *file, void *priv, |
1534 | struct v4l2_format *f) |
1535 | { |
1536 | struct aml_vcodec_ctx *ctx = fh_to_ctx(priv); |
1537 | struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; |
1538 | struct vb2_queue *vq; |
1539 | struct aml_q_data *q_data; |
1540 | |
1541 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); |
1542 | if (!vq) { |
1543 | aml_v4l2_err("[%d] no vb2 queue for type=%d", ctx->id, f->type); |
1544 | return -EINVAL; |
1545 | } |
1546 | |
1547 | q_data = aml_vdec_get_q_data(ctx, f->type); |
1548 | |
1549 | pix_mp->field = V4L2_FIELD_NONE; |
1550 | pix_mp->colorspace = ctx->colorspace; |
1551 | pix_mp->ycbcr_enc = ctx->ycbcr_enc; |
1552 | pix_mp->quantization = ctx->quantization; |
1553 | pix_mp->xfer_func = ctx->xfer_func; |
1554 | |
1555 | if ((!V4L2_TYPE_IS_OUTPUT(f->type)) && |
1556 | (ctx->state >= AML_STATE_PROBE)) { |
1557 | /* Until STREAMOFF is called on the CAPTURE queue |
1558 | * (acknowledging the event), the driver operates as if |
1559 | * the resolution hasn't changed yet. |
1560 | * So we just return picinfo yet, and update picinfo in |
1561 | * stop_streaming hook function |
1562 | */ |
1563 | /* it is used for alloc the decode buffer size. */ |
1564 | q_data->sizeimage[0] = ctx->picinfo.y_len_sz; |
1565 | q_data->sizeimage[1] = ctx->picinfo.c_len_sz; |
1566 | |
1567 | /* it is used for alloc the EGL image buffer size. */ |
1568 | q_data->coded_width = ctx->picinfo.coded_width; |
1569 | q_data->coded_height = ctx->picinfo.coded_height; |
1570 | |
1571 | q_data->bytesperline[0] = ctx->picinfo.coded_width; |
1572 | q_data->bytesperline[1] = ctx->picinfo.coded_width; |
1573 | |
1574 | /* |
1575 | * Width and height are set to the dimensions |
1576 | * of the movie, the buffer is bigger and |
1577 | * further processing stages should crop to this |
1578 | * rectangle. |
1579 | */ |
1580 | pix_mp->width = q_data->coded_width; |
1581 | pix_mp->height = q_data->coded_height; |
1582 | |
1583 | /* |
1584 | * Set pixelformat to the format in which mt vcodec |
1585 | * outputs the decoded frame |
1586 | */ |
1587 | pix_mp->num_planes = q_data->fmt->num_planes; |
1588 | pix_mp->pixelformat = q_data->fmt->fourcc; |
1589 | pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; |
1590 | pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; |
1591 | pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1]; |
1592 | pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1]; |
1593 | } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) { |
1594 | /* |
1595 | * This is run on OUTPUT |
1596 | * The buffer contains compressed image |
1597 | * so width and height have no meaning. |
1598 | * Assign value here to pass v4l2-compliance test |
1599 | */ |
1600 | pix_mp->width = q_data->visible_width; |
1601 | pix_mp->height = q_data->visible_height; |
1602 | pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; |
1603 | pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; |
1604 | pix_mp->pixelformat = q_data->fmt->fourcc; |
1605 | pix_mp->num_planes = q_data->fmt->num_planes; |
1606 | } else { |
1607 | pix_mp->width = q_data->coded_width; |
1608 | pix_mp->height = q_data->coded_height; |
1609 | pix_mp->num_planes = q_data->fmt->num_planes; |
1610 | pix_mp->pixelformat = q_data->fmt->fourcc; |
1611 | pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0]; |
1612 | pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0]; |
1613 | pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1]; |
1614 | pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1]; |
1615 | |
1616 | aml_v4l2_debug(4, "[%d] type=%d state=%d Format information could not be read, not ready yet!", |
1617 | ctx->id, f->type, ctx->state); |
1618 | return -EINVAL; |
1619 | } |
1620 | |
1621 | return 0; |
1622 | } |
1623 | |
1624 | /*int vidioc_vdec_g_ctrl(struct file *file, void *fh, |
1625 | struct v4l2_control *a) |
1626 | { |
1627 | struct aml_vcodec_ctx *ctx = fh_to_ctx(fh); |
1628 | |
1629 | if (a->id == V4L2_CID_MIN_BUFFERS_FOR_CAPTURE) |
1630 | a->value = 20; |
1631 | |
1632 | return 0; |
1633 | }*/ |
1634 | |
1635 | static int vb2ops_vdec_queue_setup(struct vb2_queue *vq, |
1636 | unsigned int *nbuffers, |
1637 | unsigned int *nplanes, |
1638 | unsigned int sizes[], struct device *alloc_devs[]) |
1639 | { |
1640 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vq); |
1641 | struct aml_q_data *q_data; |
1642 | unsigned int i; |
1643 | |
1644 | q_data = aml_vdec_get_q_data(ctx, vq->type); |
1645 | |
1646 | if (q_data == NULL) { |
1647 | aml_v4l2_err("[%d] vq->type=%d err", ctx->id, vq->type); |
1648 | return -EINVAL; |
1649 | } |
1650 | |
1651 | if (*nplanes) { |
1652 | for (i = 0; i < *nplanes; i++) { |
1653 | if (sizes[i] < q_data->sizeimage[i]) |
1654 | return -EINVAL; |
1655 | //alloc_devs[i] = &ctx->dev->plat_dev->dev; |
1656 | alloc_devs[i] = v4l_get_dev_from_codec_mm();//alloc mm from the codec mm |
1657 | } |
1658 | } else { |
1659 | if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) |
1660 | *nplanes = 2; |
1661 | else |
1662 | *nplanes = 1; |
1663 | |
1664 | for (i = 0; i < *nplanes; i++) { |
1665 | sizes[i] = q_data->sizeimage[i]; |
1666 | //alloc_devs[i] = &ctx->dev->plat_dev->dev; |
1667 | alloc_devs[i] = v4l_get_dev_from_codec_mm();//alloc mm from the codec mm |
1668 | } |
1669 | } |
1670 | |
1671 | pr_info("[%d] type: %d, plane: %d, buf cnt: %d, size: [Y: %u, C: %u]\n", |
1672 | ctx->id, vq->type, *nplanes, *nbuffers, sizes[0], sizes[1]); |
1673 | |
1674 | return 0; |
1675 | } |
1676 | |
1677 | static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb) |
1678 | { |
1679 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
1680 | struct aml_q_data *q_data; |
1681 | int i; |
1682 | |
1683 | aml_v4l2_debug(4, "[%d] (%d) id=%d", |
1684 | ctx->id, vb->vb2_queue->type, vb->index); |
1685 | |
1686 | q_data = aml_vdec_get_q_data(ctx, vb->vb2_queue->type); |
1687 | |
1688 | for (i = 0; i < q_data->fmt->num_planes; i++) { |
1689 | if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) { |
1690 | aml_v4l2_err("[%d] data will not fit into plane %d (%lu < %d)", |
1691 | ctx->id, i, vb2_plane_size(vb, i), |
1692 | q_data->sizeimage[i]); |
1693 | } |
1694 | } |
1695 | |
1696 | return 0; |
1697 | } |
1698 | |
1699 | static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb) |
1700 | { |
1701 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
1702 | struct vb2_v4l2_buffer *vb2_v4l2 = NULL; |
1703 | struct aml_video_dec_buf *buf = NULL; |
1704 | struct aml_vcodec_mem src_mem; |
1705 | unsigned int dpb = 0; |
1706 | |
1707 | vb2_v4l2 = to_vb2_v4l2_buffer(vb); |
1708 | buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb); |
1709 | |
1710 | aml_v4l2_debug(3, "[%d] %s(), vb: %p, type: %d, idx: %d, state: %d, used: %d", |
1711 | ctx->id, __func__, vb, vb->vb2_queue->type, |
1712 | vb->index, vb->state, buf->used); |
1713 | /* |
1714 | * check if this buffer is ready to be used after decode |
1715 | */ |
1716 | if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { |
1717 | aml_v4l2_debug(3, "[%d] %s() [%d], y_addr: %lx, vf_h: %lx, state: %d", ctx->id, |
1718 | __func__, __LINE__, buf->frame_buffer.m.mem[0].addr, |
1719 | buf->frame_buffer.vf_handle, buf->frame_buffer.status); |
1720 | |
1721 | if (!buf->que_in_m2m) { |
1722 | aml_v4l2_debug(2, "[%d] enque capture buf idx %d, %p", |
1723 | ctx->id, vb->index, vb); |
1724 | v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2); |
1725 | buf->que_in_m2m = true; |
1726 | buf->queued_in_vb2 = true; |
1727 | buf->queued_in_v4l2 = true; |
1728 | buf->ready_to_display = false; |
1729 | |
1730 | /* check dpb ready */ |
1731 | aml_check_dpb_ready(ctx); |
1732 | } else if (buf->frame_buffer.status == FB_ST_DISPLAY) { |
1733 | buf->queued_in_vb2 = false; |
1734 | buf->queued_in_v4l2 = true; |
1735 | buf->ready_to_display = false; |
1736 | |
1737 | /* recycle vf */ |
1738 | video_vf_put(ctx->ada_ctx->recv_name, |
1739 | &buf->frame_buffer, ctx->id); |
1740 | } |
1741 | return; |
1742 | } |
1743 | |
1744 | v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb)); |
1745 | |
1746 | if (ctx->state != AML_STATE_INIT) { |
1747 | aml_v4l2_debug(4, "[%d] already init driver %d", |
1748 | ctx->id, ctx->state); |
1749 | return; |
1750 | } |
1751 | |
1752 | vb2_v4l2 = to_vb2_v4l2_buffer(vb); |
1753 | buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb); |
1754 | if (buf->lastframe) { |
1755 | /* This shouldn't happen. Just in case. */ |
1756 | aml_v4l2_err("[%d] Invalid flush buffer.", ctx->id); |
1757 | v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
1758 | return; |
1759 | } |
1760 | |
1761 | src_mem.vaddr = vb2_plane_vaddr(vb, 0); |
1762 | src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0); |
1763 | src_mem.size = vb->planes[0].bytesused; |
1764 | if (vdec_if_probe(ctx, &src_mem, NULL)) { |
1765 | v4l2_m2m_src_buf_remove(ctx->m2m_ctx); |
1766 | v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_DONE); |
1767 | return; |
1768 | } |
1769 | |
1770 | if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) { |
1771 | pr_err("[%d] GET_PARAM_PICTURE_INFO err\n", ctx->id); |
1772 | return; |
1773 | } |
1774 | |
1775 | if (vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpb)) { |
1776 | pr_err("[%d] GET_PARAM_DPB_SIZE err\n", ctx->id); |
1777 | return; |
1778 | } |
1779 | |
1780 | if (!dpb) |
1781 | return; |
1782 | |
1783 | ctx->dpb_size = dpb; |
1784 | ctx->last_decoded_picinfo = ctx->picinfo; |
1785 | aml_vdec_dispatch_event(ctx, V4L2_EVENT_SRC_CH_RESOLUTION); |
1786 | |
1787 | mutex_lock(&ctx->state_lock); |
1788 | if (ctx->state == AML_STATE_INIT) { |
1789 | ctx->state = AML_STATE_PROBE; |
1790 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_PROBE)", |
1791 | ctx->id, __func__); |
1792 | } |
1793 | mutex_unlock(&ctx->state_lock); |
1794 | } |
1795 | |
1796 | static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb) |
1797 | { |
1798 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
1799 | struct vb2_v4l2_buffer *vb2_v4l2 = NULL; |
1800 | struct aml_video_dec_buf *buf = NULL; |
1801 | bool buf_error; |
1802 | |
1803 | vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf); |
1804 | buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb); |
1805 | |
1806 | if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { |
1807 | buf->queued_in_v4l2 = false; |
1808 | buf->queued_in_vb2 = false; |
1809 | } |
1810 | buf_error = buf->error; |
1811 | |
1812 | if (buf_error) { |
1813 | aml_v4l2_err("[%d] Unrecoverable error on buffer.", ctx->id); |
1814 | ctx->state = AML_STATE_ABORT; |
1815 | aml_v4l2_debug(1, "[%d] %s() vcodec state (AML_STATE_ABORT)", |
1816 | ctx->id, __func__); |
1817 | } |
1818 | } |
1819 | |
1820 | static int vb2ops_vdec_buf_init(struct vb2_buffer *vb) |
1821 | { |
1822 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
1823 | struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb, |
1824 | struct vb2_v4l2_buffer, vb2_buf); |
1825 | struct aml_video_dec_buf *buf = container_of(vb2_v4l2, |
1826 | struct aml_video_dec_buf, vb); |
1827 | unsigned int size, phy_addr = 0; |
1828 | char *owner = __getname(); |
1829 | |
1830 | aml_v4l2_debug(4, "[%d] (%d) id=%d", |
1831 | ctx->id, vb->vb2_queue->type, vb->index); |
1832 | |
1833 | if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) { |
1834 | buf->used = false; |
1835 | buf->ready_to_display = false; |
1836 | buf->queued_in_v4l2 = false; |
1837 | buf->frame_buffer.status = FB_ST_NORMAL; |
1838 | } else { |
1839 | buf->lastframe = false; |
1840 | } |
1841 | |
1842 | /* codec_mm buffers count */ |
1843 | if (V4L2_TYPE_IS_OUTPUT(vb->type)) { |
1844 | size = vb->planes[0].length; |
1845 | phy_addr = vb2_dma_contig_plane_dma_addr(vb, 0); |
1846 | snprintf(owner, PATH_MAX, "%s-%d", "v4l-input", ctx->id); |
1847 | strncpy(buf->mem_onwer, owner, sizeof(buf->mem_onwer)); |
1848 | buf->mem_onwer[sizeof(buf->mem_onwer) - 1] = '\0'; |
1849 | |
1850 | buf->mem[0] = v4l_reqbufs_from_codec_mm(buf->mem_onwer, |
1851 | phy_addr, size, vb->index); |
1852 | aml_v4l2_debug(3, "[%d] IN alloc, addr: %x, size: %u, idx: %u", |
1853 | ctx->id, phy_addr, size, vb->index); |
1854 | } else { |
1855 | snprintf(owner, PATH_MAX, "%s-%d", "v4l-output", ctx->id); |
1856 | strncpy(buf->mem_onwer, owner, sizeof(buf->mem_onwer)); |
1857 | buf->mem_onwer[sizeof(buf->mem_onwer) - 1] = '\0'; |
1858 | |
1859 | if ((vb->memory == VB2_MEMORY_MMAP) && (vb->num_planes == 1)) { |
1860 | size = vb->planes[0].length; |
1861 | phy_addr = vb2_dma_contig_plane_dma_addr(vb, 0); |
1862 | buf->mem[0] = v4l_reqbufs_from_codec_mm(buf->mem_onwer, |
1863 | phy_addr, size, vb->index); |
1864 | aml_v4l2_debug(3, "[%d] OUT Y alloc, addr: %x, size: %u, idx: %u", |
1865 | ctx->id, phy_addr, size, vb->index); |
1866 | } else if ((vb->memory == VB2_MEMORY_MMAP) && (vb->num_planes == 2)) { |
1867 | size = vb->planes[0].length; |
1868 | phy_addr = vb2_dma_contig_plane_dma_addr(vb, 0); |
1869 | buf->mem[0] = v4l_reqbufs_from_codec_mm(buf->mem_onwer, |
1870 | phy_addr, size, vb->index); |
1871 | aml_v4l2_debug(3, "[%d] OUT Y alloc, addr: %x, size: %u, idx: %u", |
1872 | ctx->id, phy_addr, size, vb->index); |
1873 | |
1874 | size = vb->planes[1].length; |
1875 | phy_addr = vb2_dma_contig_plane_dma_addr(vb, 1); |
1876 | buf->mem[1] = v4l_reqbufs_from_codec_mm(buf->mem_onwer, |
1877 | phy_addr, size, vb->index); |
1878 | aml_v4l2_debug(3, "[%d] OUT C alloc, addr: %x, size: %u, idx: %u", |
1879 | ctx->id, phy_addr, size, vb->index); |
1880 | } |
1881 | } |
1882 | |
1883 | __putname(owner); |
1884 | |
1885 | return 0; |
1886 | } |
1887 | |
1888 | static void codec_mm_bufs_cnt_clean(struct vb2_queue *q) |
1889 | { |
1890 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(q); |
1891 | struct vb2_v4l2_buffer *vb2_v4l2 = NULL; |
1892 | struct aml_video_dec_buf *buf = NULL; |
1893 | int i; |
1894 | |
1895 | for (i = 0; i < q->num_buffers; ++i) { |
1896 | vb2_v4l2 = to_vb2_v4l2_buffer(q->bufs[i]); |
1897 | buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb); |
1898 | if (IS_ERR_OR_NULL(buf->mem[0])) |
1899 | return; |
1900 | |
1901 | if (V4L2_TYPE_IS_OUTPUT(q->bufs[i]->type)) { |
1902 | v4l_freebufs_back_to_codec_mm(buf->mem_onwer, buf->mem[0]); |
1903 | |
1904 | aml_v4l2_debug(3, "[%d] IN clean, addr: %lx, size: %u, idx: %u", |
1905 | ctx->id, buf->mem[0]->phy_addr, buf->mem[0]->buffer_size, i); |
1906 | buf->mem[0] = NULL; |
1907 | continue; |
1908 | } |
1909 | |
1910 | if (q->memory == VB2_MEMORY_MMAP) { |
1911 | v4l_freebufs_back_to_codec_mm(buf->mem_onwer, buf->mem[0]); |
1912 | v4l_freebufs_back_to_codec_mm(buf->mem_onwer, buf->mem[1]); |
1913 | |
1914 | aml_v4l2_debug(3, "[%d] OUT Y clean, addr: %lx, size: %u, idx: %u", |
1915 | ctx->id, buf->mem[0]->phy_addr, buf->mem[0]->buffer_size, i); |
1916 | aml_v4l2_debug(3, "[%d] OUT C clean, addr: %lx, size: %u, idx: %u", |
1917 | ctx->id, buf->mem[1]->phy_addr, buf->mem[1]->buffer_size, i); |
1918 | buf->mem[0] = NULL; |
1919 | buf->mem[1] = NULL; |
1920 | } |
1921 | } |
1922 | } |
1923 | |
1924 | static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count) |
1925 | { |
1926 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(q); |
1927 | |
1928 | ctx->has_receive_eos = false; |
1929 | v4l2_m2m_set_dst_buffered(ctx->fh.m2m_ctx, true); |
1930 | |
1931 | return 0; |
1932 | } |
1933 | |
1934 | static void vb2ops_vdec_stop_streaming(struct vb2_queue *q) |
1935 | { |
1936 | struct aml_video_dec_buf *buf = NULL; |
1937 | struct vb2_v4l2_buffer *vb2_v4l2 = NULL; |
1938 | struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(q); |
1939 | int i; |
1940 | |
1941 | aml_v4l2_debug(3, "[%d] (%d) state=(%x) frame_cnt=%d", |
1942 | ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt); |
1943 | |
1944 | codec_mm_bufs_cnt_clean(q); |
1945 | |
1946 | if (V4L2_TYPE_IS_OUTPUT(q->type)) { |
1947 | while ((vb2_v4l2 = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) |
1948 | v4l2_m2m_buf_done(vb2_v4l2, VB2_BUF_STATE_ERROR); |
1949 | } else { |
1950 | for (i = 0; i < q->num_buffers; ++i) { |
1951 | vb2_v4l2 = to_vb2_v4l2_buffer(q->bufs[i]); |
1952 | buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb); |
1953 | buf->frame_buffer.status = FB_ST_NORMAL; |
1954 | buf->que_in_m2m = false; |
1955 | buf->privdata.is_install = false; |
1956 | |
1957 | if (vb2_v4l2->vb2_buf.state == VB2_BUF_STATE_ACTIVE) |
1958 | v4l2_m2m_buf_done(vb2_v4l2, VB2_BUF_STATE_ERROR); |
1959 | |
1960 | /*pr_info("idx: %d, state: %d\n", |
1961 | q->bufs[i]->index, q->bufs[i]->state);*/ |
1962 | } |
1963 | } |
1964 | ctx->buf_used_count = 0; |
1965 | } |
1966 | |
1967 | static void m2mops_vdec_device_run(void *priv) |
1968 | { |
1969 | struct aml_vcodec_ctx *ctx = priv; |
1970 | struct aml_vcodec_dev *dev = ctx->dev; |
1971 | |
1972 | aml_v4l2_debug(4, "[%d] %s() [%d]", ctx->id, __func__, __LINE__); |
1973 | |
1974 | queue_work(dev->decode_workqueue, &ctx->decode_work); |
1975 | } |
1976 | |
1977 | void vdec_device_vf_run(struct aml_vcodec_ctx *ctx) |
1978 | { |
1979 | aml_v4l2_debug(3, "[%d] %s() [%d]", ctx->id, __func__, __LINE__); |
1980 | |
1981 | if (ctx->state < AML_STATE_INIT || |
1982 | ctx->state > AML_STATE_FLUSHED) |
1983 | return; |
1984 | |
1985 | aml_thread_notify(ctx, AML_THREAD_CAPTURE); |
1986 | } |
1987 | |
1988 | static int m2mops_vdec_job_ready(void *m2m_priv) |
1989 | { |
1990 | struct aml_vcodec_ctx *ctx = m2m_priv; |
1991 | |
1992 | aml_v4l2_debug(4, "[%d] %s(), state: %d", ctx->id, |
1993 | __func__, ctx->state); |
1994 | |
1995 | if (ctx->state < AML_STATE_PROBE || |
1996 | ctx->state > AML_STATE_FLUSHED) |
1997 | return 0; |
1998 | |
1999 | return 1; |
2000 | } |
2001 | |
2002 | static void m2mops_vdec_job_abort(void *priv) |
2003 | { |
2004 | struct aml_vcodec_ctx *ctx = priv; |
2005 | |
2006 | aml_v4l2_debug(3, "[%d] %s() [%d]", ctx->id, __func__, __LINE__); |
2007 | } |
2008 | |
2009 | static int aml_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl) |
2010 | { |
2011 | struct aml_vcodec_ctx *ctx = ctrl_to_ctx(ctrl); |
2012 | int ret = 0; |
2013 | |
2014 | aml_v4l2_debug(4, "%s() [%d]", __func__, __LINE__); |
2015 | |
2016 | switch (ctrl->id) { |
2017 | case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE: |
2018 | if (ctx->state >= AML_STATE_PROBE) { |
2019 | ctrl->val = ctx->dpb_size; |
2020 | } else { |
2021 | pr_err("Seqinfo not ready.\n"); |
2022 | ctrl->val = 0; |
2023 | ret = -EINVAL; |
2024 | } |
2025 | break; |
2026 | default: |
2027 | ret = -EINVAL; |
2028 | } |
2029 | return ret; |
2030 | } |
2031 | |
2032 | static int aml_vdec_try_s_v_ctrl(struct v4l2_ctrl *ctrl) |
2033 | { |
2034 | struct aml_vcodec_ctx *ctx = ctrl_to_ctx(ctrl); |
2035 | |
2036 | aml_v4l2_debug(4, "%s() [%d]", __func__, __LINE__); |
2037 | |
2038 | if (ctrl->id == AML_V4L2_SET_DECMODE) { |
2039 | ctx->is_drm_mode = ctrl->val; |
2040 | pr_info("set stream mode: %x\n", ctrl->val); |
2041 | } |
2042 | |
2043 | return 0; |
2044 | } |
2045 | |
2046 | static const struct v4l2_ctrl_ops aml_vcodec_dec_ctrl_ops = { |
2047 | .g_volatile_ctrl = aml_vdec_g_v_ctrl, |
2048 | .try_ctrl = aml_vdec_try_s_v_ctrl, |
2049 | }; |
2050 | |
2051 | static const struct v4l2_ctrl_config ctrl_st_mode = { |
2052 | .name = "stream mode", |
2053 | .id = AML_V4L2_SET_DECMODE, |
2054 | .ops = &aml_vcodec_dec_ctrl_ops, |
2055 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
2056 | .flags = V4L2_CTRL_FLAG_WRITE_ONLY, |
2057 | .min = 0, |
2058 | .max = 1, |
2059 | .step = 1, |
2060 | .def = 0, |
2061 | }; |
2062 | |
2063 | int aml_vcodec_dec_ctrls_setup(struct aml_vcodec_ctx *ctx) |
2064 | { |
2065 | int ret; |
2066 | struct v4l2_ctrl *ctrl; |
2067 | |
2068 | v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1); |
2069 | ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, |
2070 | &aml_vcodec_dec_ctrl_ops, |
2071 | V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, |
2072 | 0, 32, 1, 1); |
2073 | ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE; |
2074 | if (ctx->ctrl_hdl.error) { |
2075 | ret = ctx->ctrl_hdl.error; |
2076 | goto err; |
2077 | } |
2078 | |
2079 | ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &ctrl_st_mode, NULL); |
2080 | if (ctx->ctrl_hdl.error) { |
2081 | ret = ctx->ctrl_hdl.error; |
2082 | goto err; |
2083 | } |
2084 | |
2085 | v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); |
2086 | |
2087 | return 0; |
2088 | err: |
2089 | aml_v4l2_err("[%d] Adding control failed %d", |
2090 | ctx->id, ctx->ctrl_hdl.error); |
2091 | v4l2_ctrl_handler_free(&ctx->ctrl_hdl); |
2092 | return ret; |
2093 | } |
2094 | |
2095 | static int vidioc_vdec_g_parm(struct file *file, void *fh, |
2096 | struct v4l2_streamparm *a) |
2097 | { |
2098 | struct aml_vcodec_ctx *ctx = fh_to_ctx(fh); |
2099 | |
2100 | if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
2101 | if (vdec_if_get_param(ctx, GET_PARAM_CONFIG_INFO, |
2102 | &ctx->config.parm.dec)) { |
2103 | pr_err("[%d] GET_PARAM_CONFIG_INFO err\n", ctx->id); |
2104 | return -1; |
2105 | } |
2106 | memcpy(a->parm.raw_data, ctx->config.parm.data, |
2107 | sizeof(a->parm.raw_data)); |
2108 | } |
2109 | |
2110 | return 0; |
2111 | } |
2112 | |
2113 | static int vidioc_vdec_s_parm(struct file *file, void *fh, |
2114 | struct v4l2_streamparm *a) |
2115 | { |
2116 | struct aml_vcodec_ctx *ctx = fh_to_ctx(fh); |
2117 | |
2118 | if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { |
2119 | struct aml_dec_params *in = |
2120 | (struct aml_dec_params *) a->parm.raw_data; |
2121 | struct aml_dec_params *dec = &ctx->config.parm.dec; |
2122 | |
2123 | ctx->config.type = V4L2_CONFIG_PARM_DECODE; |
2124 | |
2125 | if (in->parms_status & V4L2_CONFIG_PARM_DECODE_CFGINFO) |
2126 | dec->cfg = in->cfg; |
2127 | if (in->parms_status & V4L2_CONFIG_PARM_DECODE_PSINFO) |
2128 | dec->ps = in->ps; |
2129 | if (in->parms_status & V4L2_CONFIG_PARM_DECODE_HDRINFO) |
2130 | dec->hdr = in->hdr; |
2131 | if (in->parms_status & V4L2_CONFIG_PARM_DECODE_CNTINFO) |
2132 | dec->cnt = in->cnt; |
2133 | |
2134 | dec->parms_status |= in->parms_status; |
2135 | } |
2136 | |
2137 | return 0; |
2138 | } |
2139 | |
2140 | static void m2mops_vdec_lock(void *m2m_priv) |
2141 | { |
2142 | struct aml_vcodec_ctx *ctx = m2m_priv; |
2143 | |
2144 | aml_v4l2_debug(4, "[%d] %s()", ctx->id, __func__); |
2145 | mutex_lock(&ctx->dev->dev_mutex); |
2146 | } |
2147 | |
2148 | static void m2mops_vdec_unlock(void *m2m_priv) |
2149 | { |
2150 | struct aml_vcodec_ctx *ctx = m2m_priv; |
2151 | |
2152 | aml_v4l2_debug(4, "[%d] %s()", ctx->id, __func__); |
2153 | mutex_unlock(&ctx->dev->dev_mutex); |
2154 | } |
2155 | |
2156 | const struct v4l2_m2m_ops aml_vdec_m2m_ops = { |
2157 | .device_run = m2mops_vdec_device_run, |
2158 | .job_ready = m2mops_vdec_job_ready, |
2159 | .job_abort = m2mops_vdec_job_abort, |
2160 | .lock = m2mops_vdec_lock, |
2161 | .unlock = m2mops_vdec_unlock, |
2162 | }; |
2163 | |
2164 | static const struct vb2_ops aml_vdec_vb2_ops = { |
2165 | .queue_setup = vb2ops_vdec_queue_setup, |
2166 | .buf_prepare = vb2ops_vdec_buf_prepare, |
2167 | .buf_queue = vb2ops_vdec_buf_queue, |
2168 | .wait_prepare = vb2_ops_wait_prepare, |
2169 | .wait_finish = vb2_ops_wait_finish, |
2170 | .buf_init = vb2ops_vdec_buf_init, |
2171 | .buf_finish = vb2ops_vdec_buf_finish, |
2172 | .start_streaming = vb2ops_vdec_start_streaming, |
2173 | .stop_streaming = vb2ops_vdec_stop_streaming, |
2174 | }; |
2175 | |
2176 | const struct v4l2_ioctl_ops aml_vdec_ioctl_ops = { |
2177 | .vidioc_streamon = vidioc_decoder_streamon, |
2178 | .vidioc_streamoff = vidioc_decoder_streamoff, |
2179 | .vidioc_reqbufs = vidioc_decoder_reqbufs, |
2180 | .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, |
2181 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,//?? |
2182 | //.vidioc_g_ctrl = vidioc_vdec_g_ctrl, |
2183 | |
2184 | .vidioc_qbuf = vidioc_vdec_qbuf, |
2185 | .vidioc_dqbuf = vidioc_vdec_dqbuf, |
2186 | |
2187 | .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane, |
2188 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap_mplane, |
2189 | .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane, |
2190 | .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out_mplane, |
2191 | |
2192 | .vidioc_s_fmt_vid_cap_mplane = vidioc_vdec_s_fmt, |
2193 | .vidioc_s_fmt_vid_cap = vidioc_vdec_s_fmt, |
2194 | .vidioc_s_fmt_vid_out_mplane = vidioc_vdec_s_fmt, |
2195 | .vidioc_s_fmt_vid_out = vidioc_vdec_s_fmt, |
2196 | .vidioc_g_fmt_vid_cap_mplane = vidioc_vdec_g_fmt, |
2197 | .vidioc_g_fmt_vid_cap = vidioc_vdec_g_fmt, |
2198 | .vidioc_g_fmt_vid_out_mplane = vidioc_vdec_g_fmt, |
2199 | .vidioc_g_fmt_vid_out = vidioc_vdec_g_fmt, |
2200 | |
2201 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
2202 | |
2203 | .vidioc_enum_fmt_vid_cap_mplane = vidioc_vdec_enum_fmt_vid_cap_mplane, |
2204 | .vidioc_enum_fmt_vid_cap = vidioc_vdec_enum_fmt_vid_cap_mplane, |
2205 | .vidioc_enum_fmt_vid_out_mplane = vidioc_vdec_enum_fmt_vid_out_mplane, |
2206 | .vidioc_enum_fmt_vid_out = vidioc_vdec_enum_fmt_vid_out_mplane, |
2207 | .vidioc_enum_framesizes = vidioc_enum_framesizes, |
2208 | |
2209 | .vidioc_querycap = vidioc_vdec_querycap, |
2210 | .vidioc_subscribe_event = vidioc_vdec_subscribe_evt, |
2211 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
2212 | .vidioc_g_selection = vidioc_vdec_g_selection, |
2213 | .vidioc_s_selection = vidioc_vdec_s_selection, |
2214 | |
2215 | .vidioc_decoder_cmd = vidioc_decoder_cmd, |
2216 | .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd, |
2217 | |
2218 | .vidioc_g_parm = vidioc_vdec_g_parm, |
2219 | .vidioc_s_parm = vidioc_vdec_s_parm, |
2220 | }; |
2221 | |
2222 | int aml_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq, |
2223 | struct vb2_queue *dst_vq) |
2224 | { |
2225 | struct aml_vcodec_ctx *ctx = priv; |
2226 | int ret = 0; |
2227 | |
2228 | aml_v4l2_debug(4, "[%d] %s()", ctx->id, __func__); |
2229 | |
2230 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
2231 | src_vq->io_modes = VB2_DMABUF | VB2_MMAP; |
2232 | src_vq->drv_priv = ctx; |
2233 | src_vq->buf_struct_size = sizeof(struct aml_video_dec_buf); |
2234 | src_vq->ops = &aml_vdec_vb2_ops; |
2235 | src_vq->mem_ops = &vb2_dma_contig_memops; |
2236 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
2237 | src_vq->lock = &ctx->dev->dev_mutex; |
2238 | ret = vb2_queue_init(src_vq); |
2239 | if (ret) { |
2240 | aml_v4l2_err("[%d] Failed to initialize videobuf2 queue(output)", ctx->id); |
2241 | return ret; |
2242 | } |
2243 | |
2244 | dst_vq->type = multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : |
2245 | V4L2_BUF_TYPE_VIDEO_CAPTURE; |
2246 | dst_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; |
2247 | dst_vq->drv_priv = ctx; |
2248 | dst_vq->buf_struct_size = sizeof(struct aml_video_dec_buf); |
2249 | dst_vq->ops = &aml_vdec_vb2_ops; |
2250 | dst_vq->mem_ops = &vb2_dma_contig_memops; |
2251 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
2252 | dst_vq->lock = &ctx->dev->dev_mutex; |
2253 | ret = vb2_queue_init(dst_vq); |
2254 | if (ret) { |
2255 | vb2_queue_release(src_vq); |
2256 | aml_v4l2_err("[%d] Failed to initialize videobuf2 queue(capture)", ctx->id); |
2257 | } |
2258 | |
2259 | return ret; |
2260 | } |
2261 | |
2262 |