459e74a46737e84e490cbbfc62596f33c35e85e0
[platform/kernel/linux-rpi.git] / drivers / media / platform / mediatek / vcodec / mtk_vcodec_dec_stateful.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include <media/v4l2-event.h>
4 #include <media/v4l2-mem2mem.h>
5 #include <media/videobuf2-dma-contig.h>
6
7 #include "mtk_vcodec_dec.h"
8 #include "mtk_vcodec_intr.h"
9 #include "mtk_vcodec_util.h"
10 #include "mtk_vcodec_dec_pm.h"
11 #include "vdec_drv_if.h"
12
13 static struct mtk_video_fmt mtk_video_formats[] = {
14         {
15                 .fourcc = V4L2_PIX_FMT_H264,
16                 .type = MTK_FMT_DEC,
17                 .num_planes = 1,
18                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
19                 .frmsize = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
20                              MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
21         },
22         {
23                 .fourcc = V4L2_PIX_FMT_VP8,
24                 .type = MTK_FMT_DEC,
25                 .num_planes = 1,
26                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
27                 .frmsize = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
28                              MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
29         },
30         {
31                 .fourcc = V4L2_PIX_FMT_VP9,
32                 .type = MTK_FMT_DEC,
33                 .num_planes = 1,
34                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION,
35                 .frmsize = { MTK_VDEC_MIN_W, MTK_VDEC_MAX_W, 16,
36                              MTK_VDEC_MIN_H, MTK_VDEC_MAX_H, 16 },
37         },
38         {
39                 .fourcc = V4L2_PIX_FMT_MT21C,
40                 .type = MTK_FMT_FRAME,
41                 .num_planes = 2,
42         },
43 };
44
45 static const unsigned int num_supported_formats =
46         ARRAY_SIZE(mtk_video_formats);
47
48 #define DEFAULT_OUT_FMT_IDX 0
49 #define DEFAULT_CAP_FMT_IDX 3
50
51 /*
52  * This function tries to clean all display buffers, the buffers will return
53  * in display order.
54  * Note the buffers returned from codec driver may still be in driver's
55  * reference list.
56  */
57 static struct vb2_buffer *get_display_buffer(struct mtk_vcodec_dec_ctx *ctx)
58 {
59         struct vdec_fb *disp_frame_buffer = NULL;
60         struct mtk_video_dec_buf *dstbuf;
61         struct vb2_v4l2_buffer *vb;
62
63         mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id);
64         if (vdec_if_get_param(ctx, GET_PARAM_DISP_FRAME_BUFFER,
65                               &disp_frame_buffer)) {
66                 mtk_v4l2_vdec_err(ctx, "[%d]Cannot get param : GET_PARAM_DISP_FRAME_BUFFER",
67                                   ctx->id);
68                 return NULL;
69         }
70
71         if (!disp_frame_buffer) {
72                 mtk_v4l2_vdec_dbg(3, ctx, "No display frame buffer");
73                 return NULL;
74         }
75
76         dstbuf = container_of(disp_frame_buffer, struct mtk_video_dec_buf,
77                               frame_buffer);
78         vb = &dstbuf->m2m_buf.vb;
79         mutex_lock(&ctx->lock);
80         if (dstbuf->used) {
81                 mtk_v4l2_vdec_dbg(2, ctx, "[%d]status=%x queue id=%d to done_list %d",
82                                   ctx->id, disp_frame_buffer->status,
83                                   vb->vb2_buf.index, dstbuf->queued_in_vb2);
84
85                 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_DONE);
86                 ctx->decoded_frame_cnt++;
87         }
88         mutex_unlock(&ctx->lock);
89         return &vb->vb2_buf;
90 }
91
92 /*
93  * This function tries to clean all capture buffers that are not used as
94  * reference buffers by codec driver any more
95  * In this case, we need re-queue buffer to vb2 buffer if user space
96  * already returns this buffer to v4l2 or this buffer is just the output of
97  * previous sps/pps/resolution change decode, or do nothing if user
98  * space still owns this buffer
99  */
100 static struct vb2_buffer *get_free_buffer(struct mtk_vcodec_dec_ctx *ctx)
101 {
102         struct mtk_video_dec_buf *dstbuf;
103         struct vdec_fb *free_frame_buffer = NULL;
104         struct vb2_v4l2_buffer *vb;
105
106         if (vdec_if_get_param(ctx, GET_PARAM_FREE_FRAME_BUFFER,
107                               &free_frame_buffer)) {
108                 mtk_v4l2_vdec_err(ctx, "[%d] Error!! Cannot get param", ctx->id);
109                 return NULL;
110         }
111         if (!free_frame_buffer) {
112                 mtk_v4l2_vdec_dbg(3, ctx, " No free frame buffer");
113                 return NULL;
114         }
115
116         mtk_v4l2_vdec_dbg(3, ctx, "[%d] tmp_frame_addr = 0x%p", ctx->id,
117                           free_frame_buffer);
118
119         dstbuf = container_of(free_frame_buffer, struct mtk_video_dec_buf,
120                               frame_buffer);
121         vb = &dstbuf->m2m_buf.vb;
122
123         mutex_lock(&ctx->lock);
124         if (dstbuf->used) {
125                 if (dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2 &&
126                     free_frame_buffer->status == FB_ST_FREE) {
127                         /*
128                          * After decode sps/pps or non-display buffer, we don't
129                          * need to return capture buffer to user space, but
130                          * just re-queue this capture buffer to vb2 queue.
131                          * This reduce overheads that dq/q unused capture
132                          * buffer. In this case, queued_in_vb2 = true.
133                          */
134                         mtk_v4l2_vdec_dbg(2, ctx, "[%d]status=%x queue id=%d to rdy_queue %d",
135                                           ctx->id, free_frame_buffer->status,
136                                           vb->vb2_buf.index, dstbuf->queued_in_vb2);
137                         v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
138                 } else if (!dstbuf->queued_in_vb2 && dstbuf->queued_in_v4l2) {
139                         /*
140                          * If buffer in v4l2 driver but not in vb2 queue yet,
141                          * and we get this buffer from free_list, it means
142                          * that codec driver do not use this buffer as
143                          * reference buffer anymore. We should q buffer to vb2
144                          * queue, so later work thread could get this buffer
145                          * for decode. In this case, queued_in_vb2 = false
146                          * means this buffer is not from previous decode
147                          * output.
148                          */
149                         mtk_v4l2_vdec_dbg(2, ctx,
150                                           "[%d]status=%x queue id=%d to rdy_queue",
151                                           ctx->id, free_frame_buffer->status,
152                                           vb->vb2_buf.index);
153                         v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
154                         dstbuf->queued_in_vb2 = true;
155                 } else {
156                         /*
157                          * Codec driver do not need to reference this capture
158                          * buffer and this buffer is not in v4l2 driver.
159                          * Then we don't need to do any thing, just add log when
160                          * we need to debug buffer flow.
161                          * When this buffer q from user space, it could
162                          * directly q to vb2 buffer
163                          */
164                         mtk_v4l2_vdec_dbg(3, ctx, "[%d]status=%x err queue id=%d %d %d",
165                                           ctx->id, free_frame_buffer->status,
166                                           vb->vb2_buf.index, dstbuf->queued_in_vb2,
167                                           dstbuf->queued_in_v4l2);
168                 }
169                 dstbuf->used = false;
170         }
171         mutex_unlock(&ctx->lock);
172         return &vb->vb2_buf;
173 }
174
175 static void clean_display_buffer(struct mtk_vcodec_dec_ctx *ctx)
176 {
177         while (get_display_buffer(ctx))
178                 ;
179 }
180
181 static void clean_free_buffer(struct mtk_vcodec_dec_ctx *ctx)
182 {
183         while (get_free_buffer(ctx))
184                 ;
185 }
186
187 static void mtk_vdec_queue_res_chg_event(struct mtk_vcodec_dec_ctx *ctx)
188 {
189         static const struct v4l2_event ev_src_ch = {
190                 .type = V4L2_EVENT_SOURCE_CHANGE,
191                 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
192         };
193
194         mtk_v4l2_vdec_dbg(1, ctx, "[%d]", ctx->id);
195         v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
196 }
197
198 static int mtk_vdec_flush_decoder(struct mtk_vcodec_dec_ctx *ctx)
199 {
200         bool res_chg;
201         int ret;
202
203         ret = vdec_if_decode(ctx, NULL, NULL, &res_chg);
204         if (ret)
205                 mtk_v4l2_vdec_err(ctx, "DecodeFinal failed, ret=%d", ret);
206
207         clean_display_buffer(ctx);
208         clean_free_buffer(ctx);
209
210         return 0;
211 }
212
213 static void mtk_vdec_update_fmt(struct mtk_vcodec_dec_ctx *ctx,
214                                 unsigned int pixelformat)
215 {
216         const struct mtk_video_fmt *fmt;
217         struct mtk_q_data *dst_q_data;
218         unsigned int k;
219
220         dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
221         for (k = 0; k < num_supported_formats; k++) {
222                 fmt = &mtk_video_formats[k];
223                 if (fmt->fourcc == pixelformat) {
224                         mtk_v4l2_vdec_dbg(1, ctx, "Update cap fourcc(%d -> %d)",
225                                           dst_q_data->fmt->fourcc, pixelformat);
226                         dst_q_data->fmt = fmt;
227                         return;
228                 }
229         }
230
231         mtk_v4l2_vdec_err(ctx, "Cannot get fourcc(%d), using init value", pixelformat);
232 }
233
234 static int mtk_vdec_pic_info_update(struct mtk_vcodec_dec_ctx *ctx)
235 {
236         unsigned int dpbsize = 0;
237         int ret;
238
239         if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO,
240                               &ctx->last_decoded_picinfo)) {
241                 mtk_v4l2_vdec_err(ctx, "[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
242                                   ctx->id);
243                 return -EINVAL;
244         }
245
246         if (ctx->last_decoded_picinfo.pic_w == 0 ||
247             ctx->last_decoded_picinfo.pic_h == 0 ||
248             ctx->last_decoded_picinfo.buf_w == 0 ||
249             ctx->last_decoded_picinfo.buf_h == 0) {
250                 mtk_v4l2_vdec_err(ctx, "Cannot get correct pic info");
251                 return -EINVAL;
252         }
253
254         if (ctx->last_decoded_picinfo.cap_fourcc != ctx->picinfo.cap_fourcc &&
255             ctx->picinfo.cap_fourcc != 0)
256                 mtk_vdec_update_fmt(ctx, ctx->picinfo.cap_fourcc);
257
258         if (ctx->last_decoded_picinfo.pic_w == ctx->picinfo.pic_w ||
259             ctx->last_decoded_picinfo.pic_h == ctx->picinfo.pic_h)
260                 return 0;
261
262         mtk_v4l2_vdec_dbg(1, ctx, "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)", ctx->id,
263                           ctx->last_decoded_picinfo.pic_w,
264                           ctx->last_decoded_picinfo.pic_h, ctx->picinfo.pic_w,
265                           ctx->picinfo.pic_h, ctx->last_decoded_picinfo.buf_w,
266                           ctx->last_decoded_picinfo.buf_h);
267
268         ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
269         if (dpbsize == 0)
270                 mtk_v4l2_vdec_err(ctx, "Incorrect dpb size, ret=%d", ret);
271
272         ctx->dpb_size = dpbsize;
273
274         return ret;
275 }
276
277 static void mtk_vdec_worker(struct work_struct *work)
278 {
279         struct mtk_vcodec_dec_ctx *ctx =
280                 container_of(work, struct mtk_vcodec_dec_ctx, decode_work);
281         struct mtk_vcodec_dec_dev *dev = ctx->dev;
282         struct vb2_v4l2_buffer *src_buf, *dst_buf;
283         struct mtk_vcodec_mem buf;
284         struct vdec_fb *pfb;
285         bool res_chg = false;
286         int ret;
287         struct mtk_video_dec_buf *dst_buf_info, *src_buf_info;
288
289         src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
290         if (!src_buf) {
291                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
292                 mtk_v4l2_vdec_dbg(1, ctx, "[%d] src_buf empty!!", ctx->id);
293                 return;
294         }
295
296         dst_buf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
297         if (!dst_buf) {
298                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
299                 mtk_v4l2_vdec_dbg(1, ctx, "[%d] dst_buf empty!!", ctx->id);
300                 return;
301         }
302
303         dst_buf_info =
304                 container_of(dst_buf, struct mtk_video_dec_buf, m2m_buf.vb);
305
306         pfb = &dst_buf_info->frame_buffer;
307         pfb->base_y.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 0);
308         pfb->base_y.dma_addr =
309                 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
310         pfb->base_y.size = ctx->picinfo.fb_sz[0];
311
312         pfb->base_c.va = vb2_plane_vaddr(&dst_buf->vb2_buf, 1);
313         pfb->base_c.dma_addr =
314                 vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 1);
315         pfb->base_c.size = ctx->picinfo.fb_sz[1];
316         pfb->status = 0;
317         mtk_v4l2_vdec_dbg(3, ctx, "===>[%d] vdec_if_decode() ===>", ctx->id);
318
319         mtk_v4l2_vdec_dbg(3, ctx,
320                           "id=%d Framebuf  pfb=%p VA=%p Y_DMA=%pad C_DMA=%pad Size=%zx",
321                           dst_buf->vb2_buf.index, pfb, pfb->base_y.va,
322                           &pfb->base_y.dma_addr, &pfb->base_c.dma_addr, pfb->base_y.size);
323
324         if (src_buf == &ctx->empty_flush_buf.vb) {
325                 mtk_v4l2_vdec_dbg(1, ctx, "Got empty flush input buffer.");
326                 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
327
328                 /* update dst buf status */
329                 dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
330                 mutex_lock(&ctx->lock);
331                 dst_buf_info->used = false;
332                 mutex_unlock(&ctx->lock);
333
334                 vdec_if_decode(ctx, NULL, NULL, &res_chg);
335                 clean_display_buffer(ctx);
336                 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
337                 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
338                         vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
339                 dst_buf->flags |= V4L2_BUF_FLAG_LAST;
340                 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
341                 clean_free_buffer(ctx);
342                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
343                 return;
344         }
345
346         src_buf_info =
347                 container_of(src_buf, struct mtk_video_dec_buf, m2m_buf.vb);
348
349         buf.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
350         buf.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
351         buf.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
352         if (!buf.va) {
353                 v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
354                 mtk_v4l2_vdec_err(ctx, "[%d] id=%d src_addr is NULL!!", ctx->id,
355                                   src_buf->vb2_buf.index);
356                 return;
357         }
358         mtk_v4l2_vdec_dbg(3, ctx, "[%d] Bitstream VA=%p DMA=%pad Size=%zx vb=%p",
359                           ctx->id, buf.va, &buf.dma_addr, buf.size, src_buf);
360         dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp;
361         dst_buf->timecode = src_buf->timecode;
362         mutex_lock(&ctx->lock);
363         dst_buf_info->used = true;
364         mutex_unlock(&ctx->lock);
365         src_buf_info->used = true;
366
367         ret = vdec_if_decode(ctx, &buf, pfb, &res_chg);
368
369         if (ret) {
370                 mtk_v4l2_vdec_err(ctx,
371                                   "[%d] decode src[%d] sz=0x%zx pts=%llu dst[%d] ret=%d res_chg=%d",
372                                   ctx->id, src_buf->vb2_buf.index, buf.size,
373                                   src_buf->vb2_buf.timestamp, dst_buf->vb2_buf.index, ret, res_chg);
374                 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
375                 if (ret == -EIO) {
376                         mutex_lock(&ctx->lock);
377                         src_buf_info->error = true;
378                         mutex_unlock(&ctx->lock);
379                 }
380                 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
381         } else if (!res_chg) {
382                 /*
383                  * we only return src buffer with VB2_BUF_STATE_DONE
384                  * when decode success without resolution change
385                  */
386                 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
387                 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
388         }
389
390         dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
391         clean_display_buffer(ctx);
392         clean_free_buffer(ctx);
393
394         if (!ret && res_chg) {
395                 mtk_vdec_pic_info_update(ctx);
396                 /*
397                  * On encountering a resolution change in the stream.
398                  * The driver must first process and decode all
399                  * remaining buffers from before the resolution change
400                  * point, so call flush decode here
401                  */
402                 mtk_vdec_flush_decoder(ctx);
403                 /*
404                  * After all buffers containing decoded frames from
405                  * before the resolution change point ready to be
406                  * dequeued on the CAPTURE queue, the driver sends a
407                  * V4L2_EVENT_SOURCE_CHANGE event for source change
408                  * type V4L2_EVENT_SRC_CH_RESOLUTION
409                  */
410                 mtk_vdec_queue_res_chg_event(ctx);
411         }
412         v4l2_m2m_job_finish(dev->m2m_dev_dec, ctx->m2m_ctx);
413 }
414
415 static void vb2ops_vdec_stateful_buf_queue(struct vb2_buffer *vb)
416 {
417         struct vb2_v4l2_buffer *src_buf;
418         struct mtk_vcodec_mem src_mem;
419         bool res_chg = false;
420         int ret;
421         unsigned int dpbsize = 1, i;
422         struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
423         struct vb2_v4l2_buffer *vb2_v4l2;
424         struct mtk_q_data *dst_q_data;
425
426         mtk_v4l2_vdec_dbg(3, ctx, "[%d] (%d) id=%d, vb=%p", ctx->id,
427                           vb->vb2_queue->type, vb->index, vb);
428         /*
429          * check if this buffer is ready to be used after decode
430          */
431         if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
432                 struct mtk_video_dec_buf *buf;
433
434                 vb2_v4l2 = to_vb2_v4l2_buffer(vb);
435                 buf = container_of(vb2_v4l2, struct mtk_video_dec_buf,
436                                    m2m_buf.vb);
437                 mutex_lock(&ctx->lock);
438                 if (!buf->used) {
439                         v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
440                         buf->queued_in_vb2 = true;
441                         buf->queued_in_v4l2 = true;
442                 } else {
443                         buf->queued_in_vb2 = false;
444                         buf->queued_in_v4l2 = true;
445                 }
446                 mutex_unlock(&ctx->lock);
447                 return;
448         }
449
450         v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
451
452         if (ctx->state != MTK_STATE_INIT) {
453                 mtk_v4l2_vdec_dbg(3, ctx, "[%d] already init driver %d", ctx->id, ctx->state);
454                 return;
455         }
456
457         src_buf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
458         if (!src_buf) {
459                 mtk_v4l2_vdec_err(ctx, "No src buffer");
460                 return;
461         }
462
463         if (src_buf == &ctx->empty_flush_buf.vb) {
464                 /* This shouldn't happen. Just in case. */
465                 mtk_v4l2_vdec_err(ctx, "Invalid flush buffer.");
466                 v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
467                 return;
468         }
469
470         src_mem.va = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
471         src_mem.dma_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
472         src_mem.size = (size_t)src_buf->vb2_buf.planes[0].bytesused;
473         mtk_v4l2_vdec_dbg(2, ctx, "[%d] buf id=%d va=%p dma=%pad size=%zx", ctx->id,
474                           src_buf->vb2_buf.index, src_mem.va, &src_mem.dma_addr, src_mem.size);
475
476         ret = vdec_if_decode(ctx, &src_mem, NULL, &res_chg);
477         if (ret || !res_chg) {
478                 /*
479                  * fb == NULL means to parse SPS/PPS header or
480                  * resolution info in src_mem. Decode can fail
481                  * if there is no SPS header or picture info
482                  * in bs
483                  */
484
485                 src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
486                 if (ret == -EIO) {
487                         mtk_v4l2_vdec_err(ctx, "[%d] Unrecoverable error in vdec_if_decode.",
488                                           ctx->id);
489                         ctx->state = MTK_STATE_ABORT;
490                         v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR);
491                 } else {
492                         v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
493                 }
494                 mtk_v4l2_vdec_dbg(ret ? 0 : 1, ctx,
495                                   "[%d] decode() src_buf=%d, size=%zu, fail=%d, res_chg=%d",
496                                   ctx->id, src_buf->vb2_buf.index, src_mem.size, ret, res_chg);
497                 return;
498         }
499
500         if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
501                 mtk_v4l2_vdec_err(ctx, "[%d]Error!! Cannot get param : GET_PARAM_PICTURE_INFO ERR",
502                                   ctx->id);
503                 return;
504         }
505
506         ctx->last_decoded_picinfo = ctx->picinfo;
507         dst_q_data = &ctx->q_data[MTK_Q_DATA_DST];
508         for (i = 0; i < dst_q_data->fmt->num_planes; i++) {
509                 dst_q_data->sizeimage[i] = ctx->picinfo.fb_sz[i];
510                 dst_q_data->bytesperline[i] = ctx->picinfo.buf_w;
511         }
512
513         mtk_v4l2_vdec_dbg(2, ctx, "[%d] init OK wxh=%dx%d pic wxh=%dx%d sz[0]=0x%x sz[1]=0x%x",
514                           ctx->id, ctx->picinfo.buf_w, ctx->picinfo.buf_h, ctx->picinfo.pic_w,
515                           ctx->picinfo.pic_h, dst_q_data->sizeimage[0], dst_q_data->sizeimage[1]);
516
517         ret = vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpbsize);
518         if (dpbsize == 0)
519                 mtk_v4l2_vdec_err(ctx, "[%d] GET_PARAM_DPB_SIZE fail=%d", ctx->id, ret);
520
521         ctx->dpb_size = dpbsize;
522         ctx->state = MTK_STATE_HEADER;
523         mtk_v4l2_vdec_dbg(1, ctx, "[%d] dpbsize=%d", ctx->id, ctx->dpb_size);
524
525         mtk_vdec_queue_res_chg_event(ctx);
526 }
527
528 static int mtk_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
529 {
530         struct mtk_vcodec_dec_ctx *ctx = ctrl_to_dec_ctx(ctrl);
531         int ret = 0;
532
533         switch (ctrl->id) {
534         case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
535                 if (ctx->state >= MTK_STATE_HEADER) {
536                         ctrl->val = ctx->dpb_size;
537                 } else {
538                         mtk_v4l2_vdec_dbg(0, ctx, "Seqinfo not ready");
539                         ctrl->val = 0;
540                 }
541                 break;
542         default:
543                 ret = -EINVAL;
544         }
545         return ret;
546 }
547
548 static const struct v4l2_ctrl_ops mtk_vcodec_dec_ctrl_ops = {
549         .g_volatile_ctrl = mtk_vdec_g_v_ctrl,
550 };
551
552 static int mtk_vcodec_dec_ctrls_setup(struct mtk_vcodec_dec_ctx *ctx)
553 {
554         struct v4l2_ctrl *ctrl;
555
556         v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 1);
557
558         ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
559                                  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 0, 32, 1, 1);
560         ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
561         v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
562                                V4L2_CID_MPEG_VIDEO_VP9_PROFILE,
563                                V4L2_MPEG_VIDEO_VP9_PROFILE_0, 0,
564                                V4L2_MPEG_VIDEO_VP9_PROFILE_0);
565         /*
566          * H264. Baseline / Extended decoding is not supported.
567          */
568         v4l2_ctrl_new_std_menu(&ctx->ctrl_hdl, &mtk_vcodec_dec_ctrl_ops,
569                                V4L2_CID_MPEG_VIDEO_H264_PROFILE, V4L2_MPEG_VIDEO_H264_PROFILE_HIGH,
570                                BIT(V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) |
571                                BIT(V4L2_MPEG_VIDEO_H264_PROFILE_EXTENDED),
572                                V4L2_MPEG_VIDEO_H264_PROFILE_MAIN);
573
574         if (ctx->ctrl_hdl.error) {
575                 mtk_v4l2_vdec_err(ctx, "Adding control failed %d", ctx->ctrl_hdl.error);
576                 return ctx->ctrl_hdl.error;
577         }
578
579         v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
580         return 0;
581 }
582
583 static void mtk_init_vdec_params(struct mtk_vcodec_dec_ctx *ctx)
584 {
585         unsigned int i;
586
587         if (!(ctx->dev->dec_capability & VCODEC_CAPABILITY_4K_DISABLED)) {
588                 for (i = 0; i < num_supported_formats; i++) {
589                         if (mtk_video_formats[i].type != MTK_FMT_DEC)
590                                 continue;
591
592                         mtk_video_formats[i].frmsize.max_width =
593                                 VCODEC_DEC_4K_CODED_WIDTH;
594                         mtk_video_formats[i].frmsize.max_height =
595                                 VCODEC_DEC_4K_CODED_HEIGHT;
596                 }
597         }
598 }
599
600 static struct vb2_ops mtk_vdec_frame_vb2_ops = {
601         .queue_setup = vb2ops_vdec_queue_setup,
602         .buf_prepare = vb2ops_vdec_buf_prepare,
603         .wait_prepare = vb2_ops_wait_prepare,
604         .wait_finish = vb2_ops_wait_finish,
605         .start_streaming = vb2ops_vdec_start_streaming,
606
607         .buf_queue = vb2ops_vdec_stateful_buf_queue,
608         .buf_init = vb2ops_vdec_buf_init,
609         .buf_finish = vb2ops_vdec_buf_finish,
610         .stop_streaming = vb2ops_vdec_stop_streaming,
611 };
612
613 const struct mtk_vcodec_dec_pdata mtk_vdec_8173_pdata = {
614         .init_vdec_params = mtk_init_vdec_params,
615         .ctrls_setup = mtk_vcodec_dec_ctrls_setup,
616         .vdec_vb2_ops = &mtk_vdec_frame_vb2_ops,
617         .vdec_formats = mtk_video_formats,
618         .num_formats = &num_supported_formats,
619         .default_out_fmt = &mtk_video_formats[DEFAULT_OUT_FMT_IDX],
620         .default_cap_fmt = &mtk_video_formats[DEFAULT_CAP_FMT_IDX],
621         .worker = mtk_vdec_worker,
622         .flush_decoder = mtk_vdec_flush_decoder,
623         .is_subdev_supported = false,
624         .hw_arch = MTK_VDEC_PURE_SINGLE_CORE,
625 };