]> Git Repo - J-linux.git/blob - drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec.c
sysctl: delete unused define SYSCTL_PERM_EMPTY_DIR
[J-linux.git] / drivers / media / platform / mediatek / vcodec / decoder / mtk_vcodec_dec.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  * Author: PC Chen <[email protected]>
5  *         Tiffany Lin <[email protected]>
6  */
7
8 #include <media/v4l2-event.h>
9 #include <media/v4l2-mem2mem.h>
10 #include <media/videobuf2-dma-contig.h>
11
12 #include "mtk_vcodec_dec_drv.h"
13 #include "mtk_vcodec_dec.h"
14 #include "vdec_drv_if.h"
15 #include "mtk_vcodec_dec_pm.h"
16
17 #define DFT_CFG_WIDTH   MTK_VDEC_MIN_W
18 #define DFT_CFG_HEIGHT  MTK_VDEC_MIN_H
19
20 static const struct mtk_video_fmt *
21 mtk_vdec_find_format(struct v4l2_format *f,
22                      const struct mtk_vcodec_dec_pdata *dec_pdata)
23 {
24         const struct mtk_video_fmt *fmt;
25         unsigned int k;
26
27         for (k = 0; k < *dec_pdata->num_formats; k++) {
28                 fmt = &dec_pdata->vdec_formats[k];
29                 if (fmt->fourcc == f->fmt.pix_mp.pixelformat)
30                         return fmt;
31         }
32
33         return NULL;
34 }
35
36 static bool mtk_vdec_get_cap_fmt(struct mtk_vcodec_dec_ctx *ctx, int format_index)
37 {
38         const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
39         const struct mtk_video_fmt *fmt;
40         struct mtk_q_data *q_data;
41         int num_frame_count = 0, i;
42         bool ret = false;
43
44         fmt = &dec_pdata->vdec_formats[format_index];
45         for (i = 0; i < *dec_pdata->num_formats; i++) {
46                 if (dec_pdata->vdec_formats[i].type != MTK_FMT_FRAME)
47                         continue;
48
49                 num_frame_count++;
50         }
51
52         if (num_frame_count == 1 || (!ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MM21))
53                 return true;
54
55         q_data = &ctx->q_data[MTK_Q_DATA_SRC];
56         switch (q_data->fmt->fourcc) {
57         case V4L2_PIX_FMT_H264_SLICE:
58                 if (ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MT2110R)
59                         ret = true;
60                 break;
61         case V4L2_PIX_FMT_VP9_FRAME:
62         case V4L2_PIX_FMT_AV1_FRAME:
63         case V4L2_PIX_FMT_HEVC_SLICE:
64                 if (ctx->is_10bit_bitstream && fmt->fourcc == V4L2_PIX_FMT_MT2110T)
65                         ret = true;
66                 break;
67         default:
68                 break;
69         }
70
71         return ret;
72 }
73
74 static struct mtk_q_data *mtk_vdec_get_q_data(struct mtk_vcodec_dec_ctx *ctx,
75                                               enum v4l2_buf_type type)
76 {
77         if (V4L2_TYPE_IS_OUTPUT(type))
78                 return &ctx->q_data[MTK_Q_DATA_SRC];
79
80         return &ctx->q_data[MTK_Q_DATA_DST];
81 }
82
83 static int vidioc_try_decoder_cmd(struct file *file, void *priv,
84                                 struct v4l2_decoder_cmd *cmd)
85 {
86         return v4l2_m2m_ioctl_try_decoder_cmd(file, priv, cmd);
87 }
88
89
90 static int vidioc_decoder_cmd(struct file *file, void *priv,
91                                 struct v4l2_decoder_cmd *cmd)
92 {
93         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
94         struct vb2_queue *src_vq, *dst_vq;
95         int ret;
96
97         ret = vidioc_try_decoder_cmd(file, priv, cmd);
98         if (ret)
99                 return ret;
100
101         mtk_v4l2_vdec_dbg(1, ctx, "decoder cmd=%u", cmd->cmd);
102         dst_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
103                                 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
104         switch (cmd->cmd) {
105         case V4L2_DEC_CMD_STOP:
106                 src_vq = v4l2_m2m_get_vq(ctx->m2m_ctx,
107                                 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
108                 if (!vb2_is_streaming(src_vq)) {
109                         mtk_v4l2_vdec_dbg(1, ctx, "Output stream is off. No need to flush.");
110                         return 0;
111                 }
112                 if (!vb2_is_streaming(dst_vq)) {
113                         mtk_v4l2_vdec_dbg(1, ctx, "Capture stream is off. No need to flush.");
114                         return 0;
115                 }
116                 v4l2_m2m_buf_queue(ctx->m2m_ctx, &ctx->empty_flush_buf.vb);
117                 v4l2_m2m_try_schedule(ctx->m2m_ctx);
118                 break;
119
120         case V4L2_DEC_CMD_START:
121                 vb2_clear_last_buffer_dequeued(dst_vq);
122                 break;
123
124         default:
125                 return -EINVAL;
126         }
127
128         return 0;
129 }
130
131 void mtk_vdec_unlock(struct mtk_vcodec_dec_ctx *ctx)
132 {
133         mutex_unlock(&ctx->dev->dec_mutex[ctx->hw_id]);
134 }
135
136 void mtk_vdec_lock(struct mtk_vcodec_dec_ctx *ctx)
137 {
138         mutex_lock(&ctx->dev->dec_mutex[ctx->hw_id]);
139 }
140
141 void mtk_vcodec_dec_release(struct mtk_vcodec_dec_ctx *ctx)
142 {
143         vdec_if_deinit(ctx);
144         ctx->state = MTK_STATE_FREE;
145 }
146
147 void mtk_vcodec_dec_set_default_params(struct mtk_vcodec_dec_ctx *ctx)
148 {
149         struct mtk_q_data *q_data;
150
151         ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
152         ctx->fh.m2m_ctx = ctx->m2m_ctx;
153         ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
154         INIT_WORK(&ctx->decode_work, ctx->dev->vdec_pdata->worker);
155         ctx->colorspace = V4L2_COLORSPACE_REC709;
156         ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
157         ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
158         ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
159
160         q_data = &ctx->q_data[MTK_Q_DATA_SRC];
161         memset(q_data, 0, sizeof(struct mtk_q_data));
162         q_data->visible_width = DFT_CFG_WIDTH;
163         q_data->visible_height = DFT_CFG_HEIGHT;
164         q_data->fmt = ctx->dev->vdec_pdata->default_out_fmt;
165         q_data->field = V4L2_FIELD_NONE;
166
167         q_data->sizeimage[0] = DFT_CFG_WIDTH * DFT_CFG_HEIGHT;
168         q_data->bytesperline[0] = 0;
169
170         q_data = &ctx->q_data[MTK_Q_DATA_DST];
171         memset(q_data, 0, sizeof(struct mtk_q_data));
172         q_data->visible_width = DFT_CFG_WIDTH;
173         q_data->visible_height = DFT_CFG_HEIGHT;
174         q_data->coded_width = DFT_CFG_WIDTH;
175         q_data->coded_height = DFT_CFG_HEIGHT;
176         q_data->fmt = ctx->dev->vdec_pdata->default_cap_fmt;
177         q_data->field = V4L2_FIELD_NONE;
178
179         q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
180         q_data->bytesperline[0] = q_data->coded_width;
181         q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
182         q_data->bytesperline[1] = q_data->coded_width;
183 }
184
185 static int vidioc_vdec_qbuf(struct file *file, void *priv,
186                             struct v4l2_buffer *buf)
187 {
188         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
189
190         if (ctx->state == MTK_STATE_ABORT) {
191                 mtk_v4l2_vdec_err(ctx, "[%d] Call on QBUF after unrecoverable error", ctx->id);
192                 return -EIO;
193         }
194
195         return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
196 }
197
198 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
199                              struct v4l2_buffer *buf)
200 {
201         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
202
203         if (ctx->state == MTK_STATE_ABORT) {
204                 mtk_v4l2_vdec_err(ctx, "[%d] Call on DQBUF after unrecoverable error", ctx->id);
205                 return -EIO;
206         }
207
208         return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
209 }
210
211 static int mtk_vcodec_dec_get_chip_name(void *priv)
212 {
213         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
214         struct device *dev = &ctx->dev->plat_dev->dev;
215
216         if (of_device_is_compatible(dev->of_node, "mediatek,mt8173-vcodec-dec"))
217                 return 8173;
218         else if (of_device_is_compatible(dev->of_node, "mediatek,mt8183-vcodec-dec"))
219                 return 8183;
220         else if (of_device_is_compatible(dev->of_node, "mediatek,mt8192-vcodec-dec"))
221                 return 8192;
222         else if (of_device_is_compatible(dev->of_node, "mediatek,mt8195-vcodec-dec"))
223                 return 8195;
224         else if (of_device_is_compatible(dev->of_node, "mediatek,mt8186-vcodec-dec"))
225                 return 8186;
226         else if (of_device_is_compatible(dev->of_node, "mediatek,mt8188-vcodec-dec"))
227                 return 8188;
228         else
229                 return 8173;
230 }
231
232 static int vidioc_vdec_querycap(struct file *file, void *priv,
233                                 struct v4l2_capability *cap)
234 {
235         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
236         struct device *dev = &ctx->dev->plat_dev->dev;
237         int platform_name = mtk_vcodec_dec_get_chip_name(priv);
238
239         strscpy(cap->driver, dev->driver->name, sizeof(cap->driver));
240         snprintf(cap->card, sizeof(cap->card), "MT%d video decoder", platform_name);
241
242         return 0;
243 }
244
245 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
246                                      const struct v4l2_event_subscription *sub)
247 {
248         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(fh);
249
250         if (ctx->dev->vdec_pdata->uses_stateless_api)
251                 return v4l2_ctrl_subscribe_event(fh, sub);
252
253         switch (sub->type) {
254         case V4L2_EVENT_EOS:
255                 return v4l2_event_subscribe(fh, sub, 2, NULL);
256         case V4L2_EVENT_SOURCE_CHANGE:
257                 return v4l2_src_change_event_subscribe(fh, sub);
258         default:
259                 return v4l2_ctrl_subscribe_event(fh, sub);
260         }
261 }
262
263 static int vidioc_try_fmt(struct mtk_vcodec_dec_ctx *ctx, struct v4l2_format *f,
264                           const struct mtk_video_fmt *fmt)
265 {
266         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
267         const struct v4l2_frmsize_stepwise *frmsize;
268
269         pix_fmt_mp->field = V4L2_FIELD_NONE;
270
271         /* Always apply frame size constraints from the coded side */
272         if (V4L2_TYPE_IS_OUTPUT(f->type))
273                 frmsize = &fmt->frmsize;
274         else
275                 frmsize = &ctx->q_data[MTK_Q_DATA_SRC].fmt->frmsize;
276
277         pix_fmt_mp->width = clamp(pix_fmt_mp->width, MTK_VDEC_MIN_W, frmsize->max_width);
278         pix_fmt_mp->height = clamp(pix_fmt_mp->height, MTK_VDEC_MIN_H, frmsize->max_height);
279
280         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
281                 pix_fmt_mp->num_planes = 1;
282                 pix_fmt_mp->plane_fmt[0].bytesperline = 0;
283         } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
284                 int tmp_w, tmp_h;
285
286                 /*
287                  * Find next closer width align 64, heign align 64, size align
288                  * 64 rectangle
289                  * Note: This only get default value, the real HW needed value
290                  *       only available when ctx in MTK_STATE_HEADER state
291                  */
292                 tmp_w = pix_fmt_mp->width;
293                 tmp_h = pix_fmt_mp->height;
294                 v4l_bound_align_image(&pix_fmt_mp->width, MTK_VDEC_MIN_W, frmsize->max_width, 6,
295                                       &pix_fmt_mp->height, MTK_VDEC_MIN_H, frmsize->max_height, 6,
296                                       9);
297
298                 if (pix_fmt_mp->width < tmp_w &&
299                     (pix_fmt_mp->width + 64) <= frmsize->max_width)
300                         pix_fmt_mp->width += 64;
301                 if (pix_fmt_mp->height < tmp_h &&
302                     (pix_fmt_mp->height + 64) <= frmsize->max_height)
303                         pix_fmt_mp->height += 64;
304
305                 mtk_v4l2_vdec_dbg(0, ctx,
306                                   "before resize wxh=%dx%d, after resize wxh=%dx%d, sizeimage=%d",
307                                   tmp_w, tmp_h, pix_fmt_mp->width, pix_fmt_mp->height,
308                                   pix_fmt_mp->width * pix_fmt_mp->height);
309
310                 pix_fmt_mp->num_planes = fmt->num_planes;
311                 pix_fmt_mp->plane_fmt[0].sizeimage =
312                                 pix_fmt_mp->width * pix_fmt_mp->height;
313                 pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
314
315                 if (pix_fmt_mp->num_planes == 2) {
316                         pix_fmt_mp->plane_fmt[1].sizeimage =
317                                 (pix_fmt_mp->width * pix_fmt_mp->height) / 2;
318                         pix_fmt_mp->plane_fmt[1].bytesperline =
319                                 pix_fmt_mp->width;
320                 }
321         }
322
323         pix_fmt_mp->flags = 0;
324         return 0;
325 }
326
327 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
328                                 struct v4l2_format *f)
329 {
330         const struct mtk_video_fmt *fmt;
331         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
332         const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
333
334         fmt = mtk_vdec_find_format(f, dec_pdata);
335         if (!fmt) {
336                 f->fmt.pix.pixelformat =
337                         ctx->q_data[MTK_Q_DATA_DST].fmt->fourcc;
338                 fmt = mtk_vdec_find_format(f, dec_pdata);
339         }
340
341         return vidioc_try_fmt(ctx, f, fmt);
342 }
343
344 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
345                                 struct v4l2_format *f)
346 {
347         struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
348         const struct mtk_video_fmt *fmt;
349         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
350         const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
351
352         fmt = mtk_vdec_find_format(f, dec_pdata);
353         if (!fmt) {
354                 f->fmt.pix.pixelformat =
355                         ctx->q_data[MTK_Q_DATA_SRC].fmt->fourcc;
356                 fmt = mtk_vdec_find_format(f, dec_pdata);
357         }
358
359         if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
360                 mtk_v4l2_vdec_err(ctx, "sizeimage of output format must be given");
361                 return -EINVAL;
362         }
363
364         return vidioc_try_fmt(ctx, f, fmt);
365 }
366
367 static int vidioc_vdec_g_selection(struct file *file, void *priv,
368                         struct v4l2_selection *s)
369 {
370         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
371         struct mtk_q_data *q_data;
372
373         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
374                 return -EINVAL;
375
376         q_data = &ctx->q_data[MTK_Q_DATA_DST];
377
378         switch (s->target) {
379         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
380                 s->r.left = 0;
381                 s->r.top = 0;
382                 s->r.width = ctx->picinfo.pic_w;
383                 s->r.height = ctx->picinfo.pic_h;
384                 break;
385         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
386                 s->r.left = 0;
387                 s->r.top = 0;
388                 s->r.width = ctx->picinfo.buf_w;
389                 s->r.height = ctx->picinfo.buf_h;
390                 break;
391         case V4L2_SEL_TGT_COMPOSE:
392                 if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
393                         /* set to default value if header info not ready yet*/
394                         s->r.left = 0;
395                         s->r.top = 0;
396                         s->r.width = q_data->visible_width;
397                         s->r.height = q_data->visible_height;
398                 }
399                 break;
400         default:
401                 return -EINVAL;
402         }
403
404         if (ctx->state < MTK_STATE_HEADER) {
405                 /* set to default value if header info not ready yet*/
406                 s->r.left = 0;
407                 s->r.top = 0;
408                 s->r.width = q_data->visible_width;
409                 s->r.height = q_data->visible_height;
410                 return 0;
411         }
412
413         return 0;
414 }
415
416 static int vidioc_vdec_s_selection(struct file *file, void *priv,
417                                 struct v4l2_selection *s)
418 {
419         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
420
421         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
422                 return -EINVAL;
423
424         switch (s->target) {
425         case V4L2_SEL_TGT_COMPOSE:
426                 s->r.left = 0;
427                 s->r.top = 0;
428                 s->r.width = ctx->picinfo.pic_w;
429                 s->r.height = ctx->picinfo.pic_h;
430                 break;
431         default:
432                 return -EINVAL;
433         }
434
435         return 0;
436 }
437
438 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
439                              struct v4l2_format *f)
440 {
441         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
442         struct v4l2_pix_format_mplane *pix_mp;
443         struct mtk_q_data *q_data;
444         int ret = 0;
445         const struct mtk_video_fmt *fmt;
446         const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
447
448         mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id);
449
450         q_data = mtk_vdec_get_q_data(ctx, f->type);
451         if (!q_data)
452                 return -EINVAL;
453
454         pix_mp = &f->fmt.pix_mp;
455         /*
456          * Setting OUTPUT format after OUTPUT buffers are allocated is invalid
457          * if using the stateful API.
458          */
459         if (!dec_pdata->uses_stateless_api &&
460             f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
461             vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
462                 mtk_v4l2_vdec_err(ctx, "out_q_ctx buffers already requested");
463                 ret = -EBUSY;
464         }
465
466         /*
467          * Setting CAPTURE format after CAPTURE buffers are allocated is
468          * invalid.
469          */
470         if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
471             vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
472                 mtk_v4l2_vdec_err(ctx, "cap_q_ctx buffers already requested");
473                 ret = -EBUSY;
474         }
475
476         fmt = mtk_vdec_find_format(f, dec_pdata);
477         if (fmt == NULL) {
478                 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
479                         f->fmt.pix.pixelformat =
480                                 dec_pdata->default_out_fmt->fourcc;
481                         fmt = mtk_vdec_find_format(f, dec_pdata);
482                 } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
483                         f->fmt.pix.pixelformat =
484                                 dec_pdata->default_cap_fmt->fourcc;
485                         fmt = mtk_vdec_find_format(f, dec_pdata);
486                 }
487         }
488         if (fmt == NULL)
489                 return -EINVAL;
490
491         q_data->fmt = fmt;
492         vidioc_try_fmt(ctx, f, q_data->fmt);
493         if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
494                 q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
495                 q_data->coded_width = pix_mp->width;
496                 q_data->coded_height = pix_mp->height;
497
498                 ctx->colorspace = pix_mp->colorspace;
499                 ctx->ycbcr_enc = pix_mp->ycbcr_enc;
500                 ctx->quantization = pix_mp->quantization;
501                 ctx->xfer_func = pix_mp->xfer_func;
502
503                 ctx->current_codec = fmt->fourcc;
504                 if (ctx->state == MTK_STATE_FREE) {
505                         ret = vdec_if_init(ctx, q_data->fmt->fourcc);
506                         if (ret) {
507                                 mtk_v4l2_vdec_err(ctx, "[%d]: vdec_if_init() fail ret=%d",
508                                                   ctx->id, ret);
509                                 return -EINVAL;
510                         }
511                         ctx->state = MTK_STATE_INIT;
512                 }
513         } else {
514                 ctx->capture_fourcc = fmt->fourcc;
515         }
516
517         /*
518          * If using the stateless API, S_FMT should have the effect of setting
519          * the CAPTURE queue resolution no matter which queue it was called on.
520          */
521         if (dec_pdata->uses_stateless_api) {
522                 ctx->picinfo.pic_w = pix_mp->width;
523                 ctx->picinfo.pic_h = pix_mp->height;
524
525                 /*
526                  * If get pic info fail, need to use the default pic info params, or
527                  * v4l2-compliance will fail
528                  */
529                 ret = vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo);
530                 if (ret) {
531                         mtk_v4l2_vdec_err(ctx, "[%d]Error!! Get GET_PARAM_PICTURE_INFO Fail",
532                                           ctx->id);
533                 }
534
535                 ctx->last_decoded_picinfo = ctx->picinfo;
536
537                 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 1) {
538                         ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
539                                 ctx->picinfo.fb_sz[0] +
540                                 ctx->picinfo.fb_sz[1];
541                         ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
542                                 ctx->picinfo.buf_w;
543                 } else {
544                         ctx->q_data[MTK_Q_DATA_DST].sizeimage[0] =
545                                 ctx->picinfo.fb_sz[0];
546                         ctx->q_data[MTK_Q_DATA_DST].bytesperline[0] =
547                                 ctx->picinfo.buf_w;
548                         ctx->q_data[MTK_Q_DATA_DST].sizeimage[1] =
549                                 ctx->picinfo.fb_sz[1];
550                         ctx->q_data[MTK_Q_DATA_DST].bytesperline[1] =
551                                 ctx->picinfo.buf_w;
552                 }
553
554                 ctx->q_data[MTK_Q_DATA_DST].coded_width = ctx->picinfo.buf_w;
555                 ctx->q_data[MTK_Q_DATA_DST].coded_height = ctx->picinfo.buf_h;
556                 mtk_v4l2_vdec_dbg(2, ctx,
557                                   "[%d] init() plane:%d wxh=%dx%d pic wxh=%dx%d sz=0x%x_0x%x",
558                                   ctx->id, pix_mp->num_planes,
559                                   ctx->picinfo.buf_w, ctx->picinfo.buf_h,
560                                   ctx->picinfo.pic_w, ctx->picinfo.pic_h,
561                                   ctx->q_data[MTK_Q_DATA_DST].sizeimage[0],
562                                   ctx->q_data[MTK_Q_DATA_DST].sizeimage[1]);
563         }
564         return 0;
565 }
566
567 static int vidioc_enum_framesizes(struct file *file, void *priv,
568                                 struct v4l2_frmsizeenum *fsize)
569 {
570         int i = 0;
571         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
572         const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
573
574         if (fsize->index != 0)
575                 return -EINVAL;
576
577         for (i = 0; i < *dec_pdata->num_formats; i++) {
578                 if (fsize->pixel_format != dec_pdata->vdec_formats[i].fourcc)
579                         continue;
580
581                 /* Only coded formats have frame sizes set */
582                 if (!dec_pdata->vdec_formats[i].frmsize.max_width)
583                         return -ENOTTY;
584
585                 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
586                 fsize->stepwise = dec_pdata->vdec_formats[i].frmsize;
587
588                 mtk_v4l2_vdec_dbg(1, ctx, "%x, %d %d %d %d %d %d",
589                                   ctx->dev->dec_capability, fsize->stepwise.min_width,
590                                   fsize->stepwise.max_width, fsize->stepwise.step_width,
591                                   fsize->stepwise.min_height, fsize->stepwise.max_height,
592                                   fsize->stepwise.step_height);
593
594                 return 0;
595         }
596
597         return -EINVAL;
598 }
599
600 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, void *priv,
601                            bool output_queue)
602 {
603         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
604         const struct mtk_vcodec_dec_pdata *dec_pdata = ctx->dev->vdec_pdata;
605         const struct mtk_video_fmt *fmt;
606         int i, j = 0;
607
608         for (i = 0; i < *dec_pdata->num_formats; i++) {
609                 if (output_queue &&
610                     dec_pdata->vdec_formats[i].type != MTK_FMT_DEC)
611                         continue;
612                 if (!output_queue &&
613                     dec_pdata->vdec_formats[i].type != MTK_FMT_FRAME)
614                         continue;
615
616                 if (!output_queue && !mtk_vdec_get_cap_fmt(ctx, i))
617                         continue;
618
619                 if (j == f->index)
620                         break;
621                 ++j;
622         }
623
624         if (i == *dec_pdata->num_formats)
625                 return -EINVAL;
626
627         fmt = &dec_pdata->vdec_formats[i];
628         f->pixelformat = fmt->fourcc;
629         f->flags = fmt->flags;
630
631         return 0;
632 }
633
634 static int vidioc_vdec_enum_fmt_vid_cap(struct file *file, void *priv,
635                                         struct v4l2_fmtdesc *f)
636 {
637         return vidioc_enum_fmt(f, priv, false);
638 }
639
640 static int vidioc_vdec_enum_fmt_vid_out(struct file *file, void *priv,
641                                         struct v4l2_fmtdesc *f)
642 {
643         return vidioc_enum_fmt(f, priv, true);
644 }
645
646 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
647                              struct v4l2_format *f)
648 {
649         struct mtk_vcodec_dec_ctx *ctx = fh_to_dec_ctx(priv);
650         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
651         struct vb2_queue *vq;
652         struct mtk_q_data *q_data;
653
654         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
655         if (!vq) {
656                 mtk_v4l2_vdec_err(ctx, "no vb2 queue for type=%d", f->type);
657                 return -EINVAL;
658         }
659
660         q_data = mtk_vdec_get_q_data(ctx, f->type);
661
662         pix_mp->field = V4L2_FIELD_NONE;
663         pix_mp->colorspace = ctx->colorspace;
664         pix_mp->ycbcr_enc = ctx->ycbcr_enc;
665         pix_mp->quantization = ctx->quantization;
666         pix_mp->xfer_func = ctx->xfer_func;
667
668         if ((f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) &&
669             (ctx->state >= MTK_STATE_HEADER)) {
670                 /* Until STREAMOFF is called on the CAPTURE queue
671                  * (acknowledging the event), the driver operates as if
672                  * the resolution hasn't changed yet.
673                  * So we just return picinfo yet, and update picinfo in
674                  * stop_streaming hook function
675                  */
676                 q_data->sizeimage[0] = ctx->picinfo.fb_sz[0];
677                 q_data->sizeimage[1] = ctx->picinfo.fb_sz[1];
678                 q_data->bytesperline[0] = ctx->last_decoded_picinfo.buf_w;
679                 q_data->bytesperline[1] = ctx->last_decoded_picinfo.buf_w;
680                 q_data->coded_width = ctx->picinfo.buf_w;
681                 q_data->coded_height = ctx->picinfo.buf_h;
682                 ctx->last_decoded_picinfo.cap_fourcc = q_data->fmt->fourcc;
683
684                 /*
685                  * Width and height are set to the dimensions
686                  * of the movie, the buffer is bigger and
687                  * further processing stages should crop to this
688                  * rectangle.
689                  */
690                 pix_mp->width = q_data->coded_width;
691                 pix_mp->height = q_data->coded_height;
692
693                 /*
694                  * Set pixelformat to the format in which mt vcodec
695                  * outputs the decoded frame
696                  */
697                 pix_mp->num_planes = q_data->fmt->num_planes;
698                 pix_mp->pixelformat = q_data->fmt->fourcc;
699                 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
700                 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
701                 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
702                 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
703
704         } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
705                 /*
706                  * This is run on OUTPUT
707                  * The buffer contains compressed image
708                  * so width and height have no meaning.
709                  * Assign value here to pass v4l2-compliance test
710                  */
711                 pix_mp->width = q_data->visible_width;
712                 pix_mp->height = q_data->visible_height;
713                 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
714                 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
715                 pix_mp->pixelformat = q_data->fmt->fourcc;
716                 pix_mp->num_planes = q_data->fmt->num_planes;
717         } else {
718                 pix_mp->width = q_data->coded_width;
719                 pix_mp->height = q_data->coded_height;
720                 pix_mp->num_planes = q_data->fmt->num_planes;
721                 pix_mp->pixelformat = q_data->fmt->fourcc;
722                 pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
723                 pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
724                 pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
725                 pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
726
727                 mtk_v4l2_vdec_dbg(1, ctx, "[%d] type=%d state=%d Format information not ready!",
728                                   ctx->id, f->type, ctx->state);
729         }
730
731         return 0;
732 }
733
734 int vb2ops_vdec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
735                             unsigned int *nplanes, unsigned int sizes[],
736                             struct device *alloc_devs[])
737 {
738         struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vq);
739         struct mtk_q_data *q_data;
740         unsigned int i;
741
742         q_data = mtk_vdec_get_q_data(ctx, vq->type);
743
744         if (q_data == NULL) {
745                 mtk_v4l2_vdec_err(ctx, "vq->type=%d err\n", vq->type);
746                 return -EINVAL;
747         }
748
749         if (*nplanes) {
750                 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
751                         if (*nplanes != q_data->fmt->num_planes)
752                                 return -EINVAL;
753                 } else {
754                         if (*nplanes != 1)
755                                 return -EINVAL;
756                 }
757                 for (i = 0; i < *nplanes; i++) {
758                         if (sizes[i] < q_data->sizeimage[i])
759                                 return -EINVAL;
760                 }
761         } else {
762                 if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
763                         *nplanes = q_data->fmt->num_planes;
764                 else
765                         *nplanes = 1;
766
767                 for (i = 0; i < *nplanes; i++)
768                         sizes[i] = q_data->sizeimage[i];
769         }
770
771         mtk_v4l2_vdec_dbg(1, ctx,
772                           "[%d]\t type = %d, get %d plane(s), %d buffer(s) of size 0x%x 0x%x ",
773                           ctx->id, vq->type, *nplanes, *nbuffers, sizes[0], sizes[1]);
774
775         return 0;
776 }
777
778 int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
779 {
780         struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
781         struct mtk_q_data *q_data;
782         int i;
783
784         mtk_v4l2_vdec_dbg(3, ctx, "[%d] (%d) id=%d",
785                           ctx->id, vb->vb2_queue->type, vb->index);
786
787         q_data = mtk_vdec_get_q_data(ctx, vb->vb2_queue->type);
788
789         for (i = 0; i < q_data->fmt->num_planes; i++) {
790                 if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
791                         mtk_v4l2_vdec_err(ctx, "data will not fit into plane %d (%lu < %d)",
792                                           i, vb2_plane_size(vb, i), q_data->sizeimage[i]);
793                         return -EINVAL;
794                 }
795                 if (!V4L2_TYPE_IS_OUTPUT(vb->type))
796                         vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
797         }
798
799         return 0;
800 }
801
802 void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
803 {
804         struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
805         struct vb2_v4l2_buffer *vb2_v4l2;
806         struct mtk_video_dec_buf *buf;
807         bool buf_error;
808
809         vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
810         buf = container_of(vb2_v4l2, struct mtk_video_dec_buf, m2m_buf.vb);
811         mutex_lock(&ctx->lock);
812         if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
813                 buf->queued_in_v4l2 = false;
814                 buf->queued_in_vb2 = false;
815         }
816         buf_error = buf->error;
817         mutex_unlock(&ctx->lock);
818
819         if (buf_error) {
820                 mtk_v4l2_vdec_err(ctx, "Unrecoverable error on buffer.");
821                 ctx->state = MTK_STATE_ABORT;
822         }
823 }
824
825 int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
826 {
827         struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
828                                         struct vb2_v4l2_buffer, vb2_buf);
829         struct mtk_video_dec_buf *buf = container_of(vb2_v4l2,
830                                         struct mtk_video_dec_buf, m2m_buf.vb);
831
832         if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
833                 buf->used = false;
834                 buf->queued_in_v4l2 = false;
835         }
836
837         return 0;
838 }
839
840 int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
841 {
842         struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(q);
843
844         if (ctx->state == MTK_STATE_FLUSH)
845                 ctx->state = MTK_STATE_HEADER;
846
847         return 0;
848 }
849
850 void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
851 {
852         struct vb2_v4l2_buffer *src_buf = NULL, *dst_buf = NULL;
853         struct mtk_vcodec_dec_ctx *ctx = vb2_get_drv_priv(q);
854         int ret;
855
856         mtk_v4l2_vdec_dbg(3, ctx, "[%d] (%d) state=(%x) ctx->decoded_frame_cnt=%d",
857                           ctx->id, q->type, ctx->state, ctx->decoded_frame_cnt);
858
859         if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
860                 while ((src_buf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx))) {
861                         if (src_buf != &ctx->empty_flush_buf.vb) {
862                                 struct media_request *req =
863                                         src_buf->vb2_buf.req_obj.req;
864                                 v4l2_m2m_buf_done(src_buf,
865                                                 VB2_BUF_STATE_ERROR);
866                                 if (req)
867                                         v4l2_ctrl_request_complete(req, &ctx->ctrl_hdl);
868                         }
869                 }
870                 return;
871         }
872
873         if (ctx->state >= MTK_STATE_HEADER) {
874
875                 /* Until STREAMOFF is called on the CAPTURE queue
876                  * (acknowledging the event), the driver operates
877                  * as if the resolution hasn't changed yet, i.e.
878                  * VIDIOC_G_FMT< etc. return previous resolution.
879                  * So we update picinfo here
880                  */
881                 ctx->picinfo = ctx->last_decoded_picinfo;
882
883                 mtk_v4l2_vdec_dbg(2, ctx,
884                                   "[%d]-> new(%d,%d), old(%d,%d), real(%d,%d)",
885                                   ctx->id, ctx->last_decoded_picinfo.pic_w,
886                                   ctx->last_decoded_picinfo.pic_h,
887                                   ctx->picinfo.pic_w, ctx->picinfo.pic_h,
888                                   ctx->last_decoded_picinfo.buf_w,
889                                   ctx->last_decoded_picinfo.buf_h);
890
891                 ret = ctx->dev->vdec_pdata->flush_decoder(ctx);
892                 if (ret)
893                         mtk_v4l2_vdec_err(ctx, "DecodeFinal failed, ret=%d", ret);
894         }
895         ctx->state = MTK_STATE_FLUSH;
896
897         while ((dst_buf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx))) {
898                 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
899                 if (ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes == 2)
900                         vb2_set_plane_payload(&dst_buf->vb2_buf, 1, 0);
901                 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR);
902         }
903
904 }
905
906 static void m2mops_vdec_device_run(void *priv)
907 {
908         struct mtk_vcodec_dec_ctx *ctx = priv;
909         struct mtk_vcodec_dec_dev *dev = ctx->dev;
910
911         queue_work(dev->decode_workqueue, &ctx->decode_work);
912 }
913
914 static int m2mops_vdec_job_ready(void *m2m_priv)
915 {
916         struct mtk_vcodec_dec_ctx *ctx = m2m_priv;
917
918         mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id);
919
920         if (ctx->state == MTK_STATE_ABORT)
921                 return 0;
922
923         if ((ctx->last_decoded_picinfo.pic_w != ctx->picinfo.pic_w) ||
924             (ctx->last_decoded_picinfo.pic_h != ctx->picinfo.pic_h))
925                 return 0;
926
927         if (ctx->state != MTK_STATE_HEADER)
928                 return 0;
929
930         return 1;
931 }
932
933 static void m2mops_vdec_job_abort(void *priv)
934 {
935         struct mtk_vcodec_dec_ctx *ctx = priv;
936
937         ctx->state = MTK_STATE_ABORT;
938 }
939
940 const struct v4l2_m2m_ops mtk_vdec_m2m_ops = {
941         .device_run     = m2mops_vdec_device_run,
942         .job_ready      = m2mops_vdec_job_ready,
943         .job_abort      = m2mops_vdec_job_abort,
944 };
945
946 const struct v4l2_ioctl_ops mtk_vdec_ioctl_ops = {
947         .vidioc_streamon        = v4l2_m2m_ioctl_streamon,
948         .vidioc_streamoff       = v4l2_m2m_ioctl_streamoff,
949         .vidioc_reqbufs         = v4l2_m2m_ioctl_reqbufs,
950         .vidioc_querybuf        = v4l2_m2m_ioctl_querybuf,
951         .vidioc_expbuf          = v4l2_m2m_ioctl_expbuf,
952
953         .vidioc_qbuf            = vidioc_vdec_qbuf,
954         .vidioc_dqbuf           = vidioc_vdec_dqbuf,
955
956         .vidioc_try_fmt_vid_cap_mplane  = vidioc_try_fmt_vid_cap_mplane,
957         .vidioc_try_fmt_vid_out_mplane  = vidioc_try_fmt_vid_out_mplane,
958
959         .vidioc_s_fmt_vid_cap_mplane    = vidioc_vdec_s_fmt,
960         .vidioc_s_fmt_vid_out_mplane    = vidioc_vdec_s_fmt,
961         .vidioc_g_fmt_vid_cap_mplane    = vidioc_vdec_g_fmt,
962         .vidioc_g_fmt_vid_out_mplane    = vidioc_vdec_g_fmt,
963
964         .vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
965
966         .vidioc_enum_fmt_vid_cap        = vidioc_vdec_enum_fmt_vid_cap,
967         .vidioc_enum_fmt_vid_out        = vidioc_vdec_enum_fmt_vid_out,
968         .vidioc_enum_framesizes = vidioc_enum_framesizes,
969
970         .vidioc_querycap                = vidioc_vdec_querycap,
971         .vidioc_subscribe_event         = vidioc_vdec_subscribe_evt,
972         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
973         .vidioc_g_selection             = vidioc_vdec_g_selection,
974         .vidioc_s_selection             = vidioc_vdec_s_selection,
975
976         .vidioc_decoder_cmd = vidioc_decoder_cmd,
977         .vidioc_try_decoder_cmd = vidioc_try_decoder_cmd,
978 };
979
980 int mtk_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
981                            struct vb2_queue *dst_vq)
982 {
983         struct mtk_vcodec_dec_ctx *ctx = priv;
984         int ret = 0;
985
986         mtk_v4l2_vdec_dbg(3, ctx, "[%d]", ctx->id);
987
988         src_vq->type            = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
989         src_vq->io_modes        = VB2_DMABUF | VB2_MMAP;
990         src_vq->drv_priv        = ctx;
991         src_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
992         src_vq->ops             = ctx->dev->vdec_pdata->vdec_vb2_ops;
993         src_vq->mem_ops         = &vb2_dma_contig_memops;
994         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
995         src_vq->lock            = &ctx->dev->dev_mutex;
996         src_vq->dev             = &ctx->dev->plat_dev->dev;
997         src_vq->allow_cache_hints = 1;
998
999         ret = vb2_queue_init(src_vq);
1000         if (ret) {
1001                 mtk_v4l2_vdec_err(ctx, "Failed to initialize videobuf2 queue(output)");
1002                 return ret;
1003         }
1004         dst_vq->type            = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1005         dst_vq->io_modes        = VB2_DMABUF | VB2_MMAP;
1006         dst_vq->drv_priv        = ctx;
1007         dst_vq->buf_struct_size = sizeof(struct mtk_video_dec_buf);
1008         dst_vq->ops             = ctx->dev->vdec_pdata->vdec_vb2_ops;
1009         dst_vq->mem_ops         = &vb2_dma_contig_memops;
1010         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1011         dst_vq->lock            = &ctx->dev->dev_mutex;
1012         dst_vq->dev             = &ctx->dev->plat_dev->dev;
1013         dst_vq->allow_cache_hints = 1;
1014
1015         ret = vb2_queue_init(dst_vq);
1016         if (ret)
1017                 mtk_v4l2_vdec_err(ctx, "Failed to initialize videobuf2 queue(capture)");
1018
1019         return ret;
1020 }
This page took 0.091253 seconds and 4 git commands to generate.