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