]> Git Repo - linux.git/blob - drivers/media/platform/amphion/vdec.c
net: bgmac: Fix return value check for fixed_phy_register()
[linux.git] / drivers / media / platform / amphion / vdec.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/vmalloc.h>
13 #include <linux/videodev2.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-mem2mem.h>
17 #include <media/v4l2-ioctl.h>
18 #include <media/videobuf2-v4l2.h>
19 #include <media/videobuf2-dma-contig.h>
20 #include <media/videobuf2-vmalloc.h>
21 #include "vpu.h"
22 #include "vpu_defs.h"
23 #include "vpu_core.h"
24 #include "vpu_helpers.h"
25 #include "vpu_v4l2.h"
26 #include "vpu_cmds.h"
27 #include "vpu_rpc.h"
28
29 #define VDEC_MIN_BUFFER_CAP             8
30 #define VDEC_MIN_BUFFER_OUT             8
31
32 struct vdec_fs_info {
33         char name[8];
34         u32 type;
35         u32 max_count;
36         u32 req_count;
37         u32 count;
38         u32 index;
39         u32 size;
40         struct vpu_buffer buffer[32];
41         u32 tag;
42 };
43
44 struct vdec_t {
45         u32 seq_hdr_found;
46         struct vpu_buffer udata;
47         struct vpu_decode_params params;
48         struct vpu_dec_codec_info codec_info;
49         enum vpu_codec_state state;
50
51         struct vpu_vb2_buffer *slots[VB2_MAX_FRAME];
52         u32 req_frame_count;
53         struct vdec_fs_info mbi;
54         struct vdec_fs_info dcp;
55         u32 seq_tag;
56
57         bool reset_codec;
58         bool fixed_fmt;
59         u32 decoded_frame_count;
60         u32 display_frame_count;
61         u32 sequence;
62         u32 eos_received;
63         bool is_source_changed;
64         u32 source_change;
65         u32 drain;
66         bool aborting;
67 };
68
69 static const struct vpu_format vdec_formats[] = {
70         {
71                 .pixfmt = V4L2_PIX_FMT_NV12M_8L128,
72                 .mem_planes = 2,
73                 .comp_planes = 2,
74                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
75                 .sibling = V4L2_PIX_FMT_NV12_8L128,
76         },
77         {
78                 .pixfmt = V4L2_PIX_FMT_NV12_8L128,
79                 .mem_planes = 1,
80                 .comp_planes = 2,
81                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
82                 .sibling = V4L2_PIX_FMT_NV12M_8L128,
83         },
84         {
85                 .pixfmt = V4L2_PIX_FMT_NV12M_10BE_8L128,
86                 .mem_planes = 2,
87                 .comp_planes = 2,
88                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
89                 .sibling = V4L2_PIX_FMT_NV12_10BE_8L128,
90         },
91         {
92                 .pixfmt = V4L2_PIX_FMT_NV12_10BE_8L128,
93                 .mem_planes = 1,
94                 .comp_planes = 2,
95                 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
96                 .sibling = V4L2_PIX_FMT_NV12M_10BE_8L128
97         },
98         {
99                 .pixfmt = V4L2_PIX_FMT_H264,
100                 .mem_planes = 1,
101                 .comp_planes = 1,
102                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
103                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
104         },
105         {
106                 .pixfmt = V4L2_PIX_FMT_H264_MVC,
107                 .mem_planes = 1,
108                 .comp_planes = 1,
109                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
110                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
111         },
112         {
113                 .pixfmt = V4L2_PIX_FMT_HEVC,
114                 .mem_planes = 1,
115                 .comp_planes = 1,
116                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
117                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
118         },
119         {
120                 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_G,
121                 .mem_planes = 1,
122                 .comp_planes = 1,
123                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
124                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
125         },
126         {
127                 .pixfmt = V4L2_PIX_FMT_VC1_ANNEX_L,
128                 .mem_planes = 1,
129                 .comp_planes = 1,
130                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
131                 .flags = V4L2_FMT_FLAG_COMPRESSED
132         },
133         {
134                 .pixfmt = V4L2_PIX_FMT_MPEG2,
135                 .mem_planes = 1,
136                 .comp_planes = 1,
137                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
138                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
139         },
140         {
141                 .pixfmt = V4L2_PIX_FMT_MPEG4,
142                 .mem_planes = 1,
143                 .comp_planes = 1,
144                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
145                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
146         },
147         {
148                 .pixfmt = V4L2_PIX_FMT_XVID,
149                 .mem_planes = 1,
150                 .comp_planes = 1,
151                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
152                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
153         },
154         {
155                 .pixfmt = V4L2_PIX_FMT_VP8,
156                 .mem_planes = 1,
157                 .comp_planes = 1,
158                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
159                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
160         },
161         {
162                 .pixfmt = V4L2_PIX_FMT_H263,
163                 .mem_planes = 1,
164                 .comp_planes = 1,
165                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
166                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
167         },
168         {
169                 .pixfmt = V4L2_PIX_FMT_SPK,
170                 .mem_planes = 1,
171                 .comp_planes = 1,
172                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
173                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
174         },
175         {
176                 .pixfmt = V4L2_PIX_FMT_RV30,
177                 .mem_planes = 1,
178                 .comp_planes = 1,
179                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
180                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
181         },
182         {
183                 .pixfmt = V4L2_PIX_FMT_RV40,
184                 .mem_planes = 1,
185                 .comp_planes = 1,
186                 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
187                 .flags = V4L2_FMT_FLAG_DYN_RESOLUTION | V4L2_FMT_FLAG_COMPRESSED
188         },
189         {0, 0, 0, 0},
190 };
191
192 static int vdec_op_s_ctrl(struct v4l2_ctrl *ctrl)
193 {
194         struct vpu_inst *inst = ctrl_to_inst(ctrl);
195         struct vdec_t *vdec = inst->priv;
196         int ret = 0;
197
198         vpu_inst_lock(inst);
199         switch (ctrl->id) {
200         case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE:
201                 vdec->params.display_delay_enable = ctrl->val;
202                 break;
203         case V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY:
204                 vdec->params.display_delay = ctrl->val;
205                 break;
206         default:
207                 ret = -EINVAL;
208                 break;
209         }
210         vpu_inst_unlock(inst);
211
212         return ret;
213 }
214
215 static const struct v4l2_ctrl_ops vdec_ctrl_ops = {
216         .s_ctrl = vdec_op_s_ctrl,
217         .g_volatile_ctrl = vpu_helper_g_volatile_ctrl,
218 };
219
220 static int vdec_ctrl_init(struct vpu_inst *inst)
221 {
222         struct v4l2_ctrl *ctrl;
223         int ret;
224
225         ret = v4l2_ctrl_handler_init(&inst->ctrl_handler, 20);
226         if (ret)
227                 return ret;
228
229         v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
230                           V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY,
231                           0, 0, 1, 0);
232
233         v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
234                           V4L2_CID_MPEG_VIDEO_DEC_DISPLAY_DELAY_ENABLE,
235                           0, 1, 1, 0);
236
237         ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
238                                  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, 1, 32, 1, 2);
239         if (ctrl)
240                 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
241
242         ctrl = v4l2_ctrl_new_std(&inst->ctrl_handler, &vdec_ctrl_ops,
243                                  V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, 1, 32, 1, 2);
244         if (ctrl)
245                 ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
246
247         if (inst->ctrl_handler.error) {
248                 ret = inst->ctrl_handler.error;
249                 v4l2_ctrl_handler_free(&inst->ctrl_handler);
250                 return ret;
251         }
252
253         ret = v4l2_ctrl_handler_setup(&inst->ctrl_handler);
254         if (ret) {
255                 dev_err(inst->dev, "[%d] setup ctrls fail, ret = %d\n", inst->id, ret);
256                 v4l2_ctrl_handler_free(&inst->ctrl_handler);
257                 return ret;
258         }
259
260         return 0;
261 }
262
263 static void vdec_handle_resolution_change(struct vpu_inst *inst)
264 {
265         struct vdec_t *vdec = inst->priv;
266         struct vb2_queue *q;
267
268         if (!inst->fh.m2m_ctx)
269                 return;
270
271         if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
272                 return;
273         if (!vdec->source_change)
274                 return;
275
276         q = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
277         if (!list_empty(&q->done_list))
278                 return;
279
280         vdec->source_change--;
281         vpu_notify_source_change(inst);
282         vpu_set_last_buffer_dequeued(inst, false);
283 }
284
285 static int vdec_update_state(struct vpu_inst *inst, enum vpu_codec_state state, u32 force)
286 {
287         struct vdec_t *vdec = inst->priv;
288         enum vpu_codec_state pre_state = inst->state;
289
290         if (state == VPU_CODEC_STATE_SEEK) {
291                 if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
292                         vdec->state = inst->state;
293                 else
294                         vdec->state = VPU_CODEC_STATE_ACTIVE;
295         }
296         if (inst->state != VPU_CODEC_STATE_SEEK || force)
297                 inst->state = state;
298         else if (state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
299                 vdec->state = VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE;
300
301         if (inst->state != pre_state)
302                 vpu_trace(inst->dev, "[%d] %d -> %d\n", inst->id, pre_state, inst->state);
303
304         if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
305                 vdec_handle_resolution_change(inst);
306
307         return 0;
308 }
309
310 static void vdec_set_last_buffer_dequeued(struct vpu_inst *inst)
311 {
312         struct vdec_t *vdec = inst->priv;
313
314         if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
315                 return;
316
317         if (vdec->eos_received) {
318                 if (!vpu_set_last_buffer_dequeued(inst, true)) {
319                         vdec->eos_received--;
320                         vdec_update_state(inst, VPU_CODEC_STATE_DRAIN, 0);
321                 }
322         }
323 }
324
325 static int vdec_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
326 {
327         strscpy(cap->driver, "amphion-vpu", sizeof(cap->driver));
328         strscpy(cap->card, "amphion vpu decoder", sizeof(cap->card));
329         strscpy(cap->bus_info, "platform: amphion-vpu", sizeof(cap->bus_info));
330
331         return 0;
332 }
333
334 static int vdec_enum_fmt(struct file *file, void *fh, struct v4l2_fmtdesc *f)
335 {
336         struct vpu_inst *inst = to_inst(file);
337         struct vdec_t *vdec = inst->priv;
338         const struct vpu_format *fmt;
339         int ret = -EINVAL;
340
341         vpu_inst_lock(inst);
342         if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
343                 fmt = vpu_get_format(inst, f->type);
344                 if (f->index == 1)
345                         fmt = vpu_helper_find_sibling(inst, f->type, fmt->pixfmt);
346                 if (f->index > 1)
347                         fmt = NULL;
348         } else {
349                 fmt = vpu_helper_enum_format(inst, f->type, f->index);
350         }
351         if (!fmt)
352                 goto exit;
353
354         memset(f->reserved, 0, sizeof(f->reserved));
355         f->pixelformat = fmt->pixfmt;
356         f->flags = fmt->flags;
357         ret = 0;
358 exit:
359         vpu_inst_unlock(inst);
360         return ret;
361 }
362
363 static int vdec_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
364 {
365         struct vpu_inst *inst = to_inst(file);
366         struct vdec_t *vdec = inst->priv;
367         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
368         struct vpu_format *cur_fmt;
369         int i;
370
371         vpu_inst_lock(inst);
372         cur_fmt = vpu_get_format(inst, f->type);
373
374         pixmp->pixelformat = cur_fmt->pixfmt;
375         pixmp->num_planes = cur_fmt->mem_planes;
376         pixmp->width = cur_fmt->width;
377         pixmp->height = cur_fmt->height;
378         pixmp->field = cur_fmt->field;
379         pixmp->flags = cur_fmt->flags;
380         for (i = 0; i < pixmp->num_planes; i++) {
381                 pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
382                 pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
383         }
384
385         f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
386         f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
387         f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
388         f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
389         vpu_inst_unlock(inst);
390
391         return 0;
392 }
393
394 static int vdec_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
395 {
396         struct vpu_inst *inst = to_inst(file);
397         struct vdec_t *vdec = inst->priv;
398         struct vpu_format fmt;
399
400         vpu_inst_lock(inst);
401         if (V4L2_TYPE_IS_CAPTURE(f->type) && vdec->fixed_fmt) {
402                 struct vpu_format *cap_fmt = vpu_get_format(inst, f->type);
403
404                 if (!vpu_helper_match_format(inst, cap_fmt->type, cap_fmt->pixfmt,
405                                              f->fmt.pix_mp.pixelformat))
406                         f->fmt.pix_mp.pixelformat = cap_fmt->pixfmt;
407         }
408
409         vpu_try_fmt_common(inst, f, &fmt);
410
411         if (vdec->fixed_fmt) {
412                 f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
413                 f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
414                 f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
415                 f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
416         } else {
417                 f->fmt.pix_mp.colorspace = V4L2_COLORSPACE_DEFAULT;
418                 f->fmt.pix_mp.xfer_func = V4L2_XFER_FUNC_DEFAULT;
419                 f->fmt.pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
420                 f->fmt.pix_mp.quantization = V4L2_QUANTIZATION_DEFAULT;
421         }
422         vpu_inst_unlock(inst);
423
424         return 0;
425 }
426
427 static int vdec_s_fmt_common(struct vpu_inst *inst, struct v4l2_format *f)
428 {
429         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
430         struct vpu_format fmt;
431         struct vpu_format *cur_fmt;
432         struct vb2_queue *q;
433         struct vdec_t *vdec = inst->priv;
434         int i;
435
436         if (!inst->fh.m2m_ctx)
437                 return -EINVAL;
438
439         q = v4l2_m2m_get_vq(inst->fh.m2m_ctx, f->type);
440         if (!q)
441                 return -EINVAL;
442         if (vb2_is_busy(q))
443                 return -EBUSY;
444
445         if (vpu_try_fmt_common(inst, f, &fmt))
446                 return -EINVAL;
447
448         cur_fmt = vpu_get_format(inst, f->type);
449         if (V4L2_TYPE_IS_OUTPUT(f->type) && inst->state != VPU_CODEC_STATE_DEINIT) {
450                 if (cur_fmt->pixfmt != fmt.pixfmt) {
451                         vdec->reset_codec = true;
452                         vdec->fixed_fmt = false;
453                 }
454         }
455         if (V4L2_TYPE_IS_OUTPUT(f->type) || !vdec->fixed_fmt) {
456                 memcpy(cur_fmt, &fmt, sizeof(*cur_fmt));
457         } else {
458                 if (vpu_helper_match_format(inst, f->type, cur_fmt->pixfmt, pixmp->pixelformat)) {
459                         cur_fmt->pixfmt = fmt.pixfmt;
460                         cur_fmt->mem_planes = fmt.mem_planes;
461                 }
462                 pixmp->pixelformat = cur_fmt->pixfmt;
463                 pixmp->num_planes = cur_fmt->mem_planes;
464                 pixmp->width = cur_fmt->width;
465                 pixmp->height = cur_fmt->height;
466                 for (i = 0; i < pixmp->num_planes; i++) {
467                         pixmp->plane_fmt[i].bytesperline = cur_fmt->bytesperline[i];
468                         pixmp->plane_fmt[i].sizeimage = vpu_get_fmt_plane_size(cur_fmt, i);
469                 }
470                 pixmp->field = cur_fmt->field;
471         }
472
473         if (!vdec->fixed_fmt) {
474                 if (V4L2_TYPE_IS_OUTPUT(f->type)) {
475                         vdec->params.codec_format = cur_fmt->pixfmt;
476                         vdec->codec_info.color_primaries = f->fmt.pix_mp.colorspace;
477                         vdec->codec_info.transfer_chars = f->fmt.pix_mp.xfer_func;
478                         vdec->codec_info.matrix_coeffs = f->fmt.pix_mp.ycbcr_enc;
479                         vdec->codec_info.full_range = f->fmt.pix_mp.quantization;
480                 } else {
481                         vdec->params.output_format = cur_fmt->pixfmt;
482                         inst->crop.left = 0;
483                         inst->crop.top = 0;
484                         inst->crop.width = cur_fmt->width;
485                         inst->crop.height = cur_fmt->height;
486                 }
487         }
488
489         vpu_trace(inst->dev, "[%d] %c%c%c%c %dx%d\n", inst->id,
490                   f->fmt.pix_mp.pixelformat,
491                   f->fmt.pix_mp.pixelformat >> 8,
492                   f->fmt.pix_mp.pixelformat >> 16,
493                   f->fmt.pix_mp.pixelformat >> 24,
494                   f->fmt.pix_mp.width,
495                   f->fmt.pix_mp.height);
496
497         return 0;
498 }
499
500 static int vdec_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
501 {
502         struct vpu_inst *inst = to_inst(file);
503         struct v4l2_pix_format_mplane *pixmp = &f->fmt.pix_mp;
504         struct vdec_t *vdec = inst->priv;
505         int ret = 0;
506
507         vpu_inst_lock(inst);
508         ret = vdec_s_fmt_common(inst, f);
509         if (ret)
510                 goto exit;
511
512         if (V4L2_TYPE_IS_OUTPUT(f->type) && !vdec->fixed_fmt) {
513                 struct v4l2_format fc;
514
515                 memset(&fc, 0, sizeof(fc));
516                 fc.type = inst->cap_format.type;
517                 fc.fmt.pix_mp.pixelformat = inst->cap_format.pixfmt;
518                 fc.fmt.pix_mp.width = pixmp->width;
519                 fc.fmt.pix_mp.height = pixmp->height;
520                 vdec_s_fmt_common(inst, &fc);
521         }
522
523         f->fmt.pix_mp.colorspace = vdec->codec_info.color_primaries;
524         f->fmt.pix_mp.xfer_func = vdec->codec_info.transfer_chars;
525         f->fmt.pix_mp.ycbcr_enc = vdec->codec_info.matrix_coeffs;
526         f->fmt.pix_mp.quantization = vdec->codec_info.full_range;
527
528 exit:
529         vpu_inst_unlock(inst);
530         return ret;
531 }
532
533 static int vdec_g_selection(struct file *file, void *fh, struct v4l2_selection *s)
534 {
535         struct vpu_inst *inst = to_inst(file);
536
537         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
538                 return -EINVAL;
539
540         switch (s->target) {
541         case V4L2_SEL_TGT_COMPOSE:
542         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
543         case V4L2_SEL_TGT_COMPOSE_PADDED:
544                 s->r = inst->crop;
545                 break;
546         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
547                 s->r.left = 0;
548                 s->r.top = 0;
549                 s->r.width = inst->cap_format.width;
550                 s->r.height = inst->cap_format.height;
551                 break;
552         default:
553                 return -EINVAL;
554         }
555
556         return 0;
557 }
558
559 static int vdec_drain(struct vpu_inst *inst)
560 {
561         struct vdec_t *vdec = inst->priv;
562
563         if (!inst->fh.m2m_ctx)
564                 return 0;
565
566         if (!vdec->drain)
567                 return 0;
568
569         if (!vpu_is_source_empty(inst))
570                 return 0;
571
572         if (!vdec->params.frame_count) {
573                 vpu_set_last_buffer_dequeued(inst, true);
574                 return 0;
575         }
576
577         vpu_iface_add_scode(inst, SCODE_PADDING_EOS);
578         vdec->params.end_flag = 1;
579         vpu_iface_set_decode_params(inst, &vdec->params, 1);
580         vdec->drain = 0;
581         vpu_trace(inst->dev, "[%d] frame_count = %d\n", inst->id, vdec->params.frame_count);
582
583         return 0;
584 }
585
586 static int vdec_cmd_start(struct vpu_inst *inst)
587 {
588         struct vdec_t *vdec = inst->priv;
589
590         switch (inst->state) {
591         case VPU_CODEC_STATE_STARTED:
592         case VPU_CODEC_STATE_DRAIN:
593         case VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE:
594                 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
595                 break;
596         default:
597                 break;
598         }
599         vpu_process_capture_buffer(inst);
600         if (vdec->eos_received)
601                 vdec_set_last_buffer_dequeued(inst);
602         return 0;
603 }
604
605 static int vdec_cmd_stop(struct vpu_inst *inst)
606 {
607         struct vdec_t *vdec = inst->priv;
608
609         vpu_trace(inst->dev, "[%d]\n", inst->id);
610
611         if (inst->state == VPU_CODEC_STATE_DEINIT) {
612                 vpu_set_last_buffer_dequeued(inst, true);
613         } else {
614                 vdec->drain = 1;
615                 vdec_drain(inst);
616         }
617
618         return 0;
619 }
620
621 static int vdec_decoder_cmd(struct file *file, void *fh, struct v4l2_decoder_cmd *cmd)
622 {
623         struct vpu_inst *inst = to_inst(file);
624         int ret;
625
626         ret = v4l2_m2m_ioctl_try_decoder_cmd(file, fh, cmd);
627         if (ret)
628                 return ret;
629
630         vpu_inst_lock(inst);
631         switch (cmd->cmd) {
632         case V4L2_DEC_CMD_START:
633                 vdec_cmd_start(inst);
634                 break;
635         case V4L2_DEC_CMD_STOP:
636                 vdec_cmd_stop(inst);
637                 break;
638         default:
639                 break;
640         }
641         vpu_inst_unlock(inst);
642
643         return 0;
644 }
645
646 static int vdec_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub)
647 {
648         switch (sub->type) {
649         case V4L2_EVENT_EOS:
650                 return v4l2_event_subscribe(fh, sub, 0, NULL);
651         case V4L2_EVENT_SOURCE_CHANGE:
652                 return v4l2_src_change_event_subscribe(fh, sub);
653         case V4L2_EVENT_CTRL:
654                 return v4l2_ctrl_subscribe_event(fh, sub);
655         default:
656                 return -EINVAL;
657         }
658
659         return 0;
660 }
661
662 static const struct v4l2_ioctl_ops vdec_ioctl_ops = {
663         .vidioc_querycap               = vdec_querycap,
664         .vidioc_enum_fmt_vid_cap       = vdec_enum_fmt,
665         .vidioc_enum_fmt_vid_out       = vdec_enum_fmt,
666         .vidioc_g_fmt_vid_cap_mplane   = vdec_g_fmt,
667         .vidioc_g_fmt_vid_out_mplane   = vdec_g_fmt,
668         .vidioc_try_fmt_vid_cap_mplane = vdec_try_fmt,
669         .vidioc_try_fmt_vid_out_mplane = vdec_try_fmt,
670         .vidioc_s_fmt_vid_cap_mplane   = vdec_s_fmt,
671         .vidioc_s_fmt_vid_out_mplane   = vdec_s_fmt,
672         .vidioc_g_selection            = vdec_g_selection,
673         .vidioc_try_decoder_cmd        = v4l2_m2m_ioctl_try_decoder_cmd,
674         .vidioc_decoder_cmd            = vdec_decoder_cmd,
675         .vidioc_subscribe_event        = vdec_subscribe_event,
676         .vidioc_unsubscribe_event      = v4l2_event_unsubscribe,
677         .vidioc_reqbufs                = v4l2_m2m_ioctl_reqbufs,
678         .vidioc_create_bufs            = v4l2_m2m_ioctl_create_bufs,
679         .vidioc_prepare_buf            = v4l2_m2m_ioctl_prepare_buf,
680         .vidioc_querybuf               = v4l2_m2m_ioctl_querybuf,
681         .vidioc_qbuf                   = v4l2_m2m_ioctl_qbuf,
682         .vidioc_expbuf                 = v4l2_m2m_ioctl_expbuf,
683         .vidioc_dqbuf                  = v4l2_m2m_ioctl_dqbuf,
684         .vidioc_streamon               = v4l2_m2m_ioctl_streamon,
685         .vidioc_streamoff              = v4l2_m2m_ioctl_streamoff,
686 };
687
688 static bool vdec_check_ready(struct vpu_inst *inst, unsigned int type)
689 {
690         struct vdec_t *vdec = inst->priv;
691
692         if (V4L2_TYPE_IS_OUTPUT(type))
693                 return true;
694
695         if (vdec->req_frame_count)
696                 return true;
697
698         return false;
699 }
700
701 static struct vb2_v4l2_buffer *vdec_get_src_buffer(struct vpu_inst *inst, u32 count)
702 {
703         if (count > 1)
704                 vpu_skip_frame(inst, count - 1);
705
706         return vpu_next_src_buf(inst);
707 }
708
709 static int vdec_frame_decoded(struct vpu_inst *inst, void *arg)
710 {
711         struct vdec_t *vdec = inst->priv;
712         struct vpu_dec_pic_info *info = arg;
713         struct vpu_vb2_buffer *vpu_buf;
714         struct vb2_v4l2_buffer *vbuf;
715         struct vb2_v4l2_buffer *src_buf;
716         int ret = 0;
717
718         if (!info || info->id >= ARRAY_SIZE(vdec->slots))
719                 return -EINVAL;
720
721         vpu_inst_lock(inst);
722         vpu_buf = vdec->slots[info->id];
723         if (!vpu_buf) {
724                 dev_err(inst->dev, "[%d] decoded invalid frame[%d]\n", inst->id, info->id);
725                 ret = -EINVAL;
726                 goto exit;
727         }
728         vbuf = &vpu_buf->m2m_buf.vb;
729         src_buf = vdec_get_src_buffer(inst, info->consumed_count);
730         if (src_buf) {
731                 v4l2_m2m_buf_copy_metadata(src_buf, vbuf, true);
732                 if (info->consumed_count) {
733                         v4l2_m2m_src_buf_remove(inst->fh.m2m_ctx);
734                         vpu_set_buffer_state(src_buf, VPU_BUF_STATE_IDLE);
735                         v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE);
736                 } else {
737                         vpu_set_buffer_state(src_buf, VPU_BUF_STATE_DECODED);
738                 }
739         }
740         if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED)
741                 dev_info(inst->dev, "[%d] buf[%d] has been decoded\n", inst->id, info->id);
742         vpu_set_buffer_state(vbuf, VPU_BUF_STATE_DECODED);
743         vdec->decoded_frame_count++;
744 exit:
745         vpu_inst_unlock(inst);
746
747         return ret;
748 }
749
750 static struct vpu_vb2_buffer *vdec_find_buffer(struct vpu_inst *inst, u32 luma)
751 {
752         struct vdec_t *vdec = inst->priv;
753         int i;
754
755         for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
756                 if (!vdec->slots[i])
757                         continue;
758                 if (luma == vdec->slots[i]->luma)
759                         return vdec->slots[i];
760         }
761
762         return NULL;
763 }
764
765 static void vdec_buf_done(struct vpu_inst *inst, struct vpu_frame_info *frame)
766 {
767         struct vdec_t *vdec = inst->priv;
768         struct vpu_format *cur_fmt;
769         struct vpu_vb2_buffer *vpu_buf;
770         struct vb2_v4l2_buffer *vbuf;
771         u32 sequence;
772         int i;
773
774         if (!frame)
775                 return;
776
777         vpu_inst_lock(inst);
778         sequence = vdec->sequence++;
779         vpu_buf = vdec_find_buffer(inst, frame->luma);
780         vpu_inst_unlock(inst);
781         if (!vpu_buf) {
782                 dev_err(inst->dev, "[%d] can't find buffer, id = %d, addr = 0x%x\n",
783                         inst->id, frame->id, frame->luma);
784                 return;
785         }
786         if (frame->skipped) {
787                 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
788                 return;
789         }
790
791         cur_fmt = vpu_get_format(inst, inst->cap_format.type);
792         vbuf = &vpu_buf->m2m_buf.vb;
793         if (vbuf->vb2_buf.index != frame->id)
794                 dev_err(inst->dev, "[%d] buffer id(%d, %d) dismatch\n",
795                         inst->id, vbuf->vb2_buf.index, frame->id);
796
797         if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_DECODED)
798                 dev_err(inst->dev, "[%d] buffer(%d) ready without decoded\n", inst->id, frame->id);
799         vpu_set_buffer_state(vbuf, VPU_BUF_STATE_READY);
800         for (i = 0; i < vbuf->vb2_buf.num_planes; i++)
801                 vb2_set_plane_payload(&vbuf->vb2_buf, i, vpu_get_fmt_plane_size(cur_fmt, i));
802         vbuf->field = cur_fmt->field;
803         vbuf->sequence = sequence;
804         dev_dbg(inst->dev, "[%d][OUTPUT TS]%32lld\n", inst->id, vbuf->vb2_buf.timestamp);
805
806         v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_DONE);
807         vpu_inst_lock(inst);
808         vdec->display_frame_count++;
809         vpu_inst_unlock(inst);
810         dev_dbg(inst->dev, "[%d] decoded : %d, display : %d, sequence : %d\n",
811                 inst->id, vdec->decoded_frame_count, vdec->display_frame_count, vdec->sequence);
812 }
813
814 static void vdec_stop_done(struct vpu_inst *inst)
815 {
816         struct vdec_t *vdec = inst->priv;
817
818         vpu_inst_lock(inst);
819         vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 0);
820         vdec->seq_hdr_found = 0;
821         vdec->req_frame_count = 0;
822         vdec->reset_codec = false;
823         vdec->fixed_fmt = false;
824         vdec->params.end_flag = 0;
825         vdec->drain = 0;
826         vdec->params.frame_count = 0;
827         vdec->decoded_frame_count = 0;
828         vdec->display_frame_count = 0;
829         vdec->sequence = 0;
830         vdec->eos_received = 0;
831         vdec->is_source_changed = false;
832         vdec->source_change = 0;
833         inst->total_input_count = 0;
834         vpu_inst_unlock(inst);
835 }
836
837 static bool vdec_check_source_change(struct vpu_inst *inst)
838 {
839         struct vdec_t *vdec = inst->priv;
840         const struct vpu_format *sibling;
841
842         if (!inst->fh.m2m_ctx)
843                 return false;
844
845         if (vdec->reset_codec)
846                 return false;
847
848         sibling = vpu_helper_find_sibling(inst, inst->cap_format.type, inst->cap_format.pixfmt);
849         if (sibling && vdec->codec_info.pixfmt == sibling->pixfmt)
850                 vdec->codec_info.pixfmt = inst->cap_format.pixfmt;
851
852         if (!vb2_is_streaming(v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx)))
853                 return true;
854         if (inst->cap_format.pixfmt != vdec->codec_info.pixfmt)
855                 return true;
856         if (inst->cap_format.width != vdec->codec_info.decoded_width)
857                 return true;
858         if (inst->cap_format.height != vdec->codec_info.decoded_height)
859                 return true;
860         if (vpu_get_num_buffers(inst, inst->cap_format.type) < inst->min_buffer_cap)
861                 return true;
862         if (inst->crop.left != vdec->codec_info.offset_x)
863                 return true;
864         if (inst->crop.top != vdec->codec_info.offset_y)
865                 return true;
866         if (inst->crop.width != vdec->codec_info.width)
867                 return true;
868         if (inst->crop.height != vdec->codec_info.height)
869                 return true;
870
871         return false;
872 }
873
874 static void vdec_init_fmt(struct vpu_inst *inst)
875 {
876         struct vdec_t *vdec = inst->priv;
877         struct v4l2_format f;
878
879         memset(&f, 0, sizeof(f));
880         f.type = inst->cap_format.type;
881         f.fmt.pix_mp.pixelformat = vdec->codec_info.pixfmt;
882         f.fmt.pix_mp.width = vdec->codec_info.decoded_width;
883         f.fmt.pix_mp.height = vdec->codec_info.decoded_height;
884         if (vdec->codec_info.progressive)
885                 f.fmt.pix_mp.field = V4L2_FIELD_NONE;
886         else
887                 f.fmt.pix_mp.field = V4L2_FIELD_SEQ_TB;
888         vpu_try_fmt_common(inst, &f, &inst->cap_format);
889
890         inst->out_format.width = vdec->codec_info.width;
891         inst->out_format.height = vdec->codec_info.height;
892 }
893
894 static void vdec_init_crop(struct vpu_inst *inst)
895 {
896         struct vdec_t *vdec = inst->priv;
897
898         inst->crop.left = vdec->codec_info.offset_x;
899         inst->crop.top = vdec->codec_info.offset_y;
900         inst->crop.width = vdec->codec_info.width;
901         inst->crop.height = vdec->codec_info.height;
902 }
903
904 static void vdec_init_mbi(struct vpu_inst *inst)
905 {
906         struct vdec_t *vdec = inst->priv;
907
908         vdec->mbi.size = vdec->codec_info.mbi_size;
909         vdec->mbi.max_count = ARRAY_SIZE(vdec->mbi.buffer);
910         scnprintf(vdec->mbi.name, sizeof(vdec->mbi.name), "mbi");
911         vdec->mbi.type = MEM_RES_MBI;
912         vdec->mbi.tag = vdec->seq_tag;
913 }
914
915 static void vdec_init_dcp(struct vpu_inst *inst)
916 {
917         struct vdec_t *vdec = inst->priv;
918
919         vdec->dcp.size = vdec->codec_info.dcp_size;
920         vdec->dcp.max_count = ARRAY_SIZE(vdec->dcp.buffer);
921         scnprintf(vdec->dcp.name, sizeof(vdec->dcp.name), "dcp");
922         vdec->dcp.type = MEM_RES_DCP;
923         vdec->dcp.tag = vdec->seq_tag;
924 }
925
926 static void vdec_request_one_fs(struct vdec_fs_info *fs)
927 {
928         fs->req_count++;
929         if (fs->req_count > fs->max_count)
930                 fs->req_count = fs->max_count;
931 }
932
933 static int vdec_alloc_fs_buffer(struct vpu_inst *inst, struct vdec_fs_info *fs)
934 {
935         struct vpu_buffer *buffer;
936
937         if (!fs->size)
938                 return -EINVAL;
939
940         if (fs->count >= fs->req_count)
941                 return -EINVAL;
942
943         buffer = &fs->buffer[fs->count];
944         if (buffer->virt && buffer->length >= fs->size)
945                 return 0;
946
947         vpu_free_dma(buffer);
948         buffer->length = fs->size;
949         return vpu_alloc_dma(inst->core, buffer);
950 }
951
952 static void vdec_alloc_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
953 {
954         int ret;
955
956         while (fs->count < fs->req_count) {
957                 ret = vdec_alloc_fs_buffer(inst, fs);
958                 if (ret)
959                         break;
960                 fs->count++;
961         }
962 }
963
964 static void vdec_clear_fs(struct vdec_fs_info *fs)
965 {
966         u32 i;
967
968         if (!fs)
969                 return;
970
971         for (i = 0; i < ARRAY_SIZE(fs->buffer); i++)
972                 vpu_free_dma(&fs->buffer[i]);
973         memset(fs, 0, sizeof(*fs));
974 }
975
976 static int vdec_response_fs(struct vpu_inst *inst, struct vdec_fs_info *fs)
977 {
978         struct vpu_fs_info info;
979         int ret;
980
981         if (fs->index >= fs->count)
982                 return 0;
983
984         memset(&info, 0, sizeof(info));
985         info.id = fs->index;
986         info.type = fs->type;
987         info.tag = fs->tag;
988         info.luma_addr = fs->buffer[fs->index].phys;
989         info.luma_size = fs->buffer[fs->index].length;
990         ret = vpu_session_alloc_fs(inst, &info);
991         if (ret)
992                 return ret;
993
994         fs->index++;
995         return 0;
996 }
997
998 static int vdec_response_frame_abnormal(struct vpu_inst *inst)
999 {
1000         struct vdec_t *vdec = inst->priv;
1001         struct vpu_fs_info info;
1002
1003         if (!vdec->req_frame_count)
1004                 return 0;
1005
1006         memset(&info, 0, sizeof(info));
1007         info.type = MEM_RES_FRAME;
1008         info.tag = vdec->seq_tag + 0xf0;
1009         vpu_session_alloc_fs(inst, &info);
1010         vdec->req_frame_count--;
1011
1012         return 0;
1013 }
1014
1015 static int vdec_response_frame(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1016 {
1017         struct vdec_t *vdec = inst->priv;
1018         struct vpu_vb2_buffer *vpu_buf;
1019         struct vpu_fs_info info;
1020         int ret;
1021
1022         if (inst->state != VPU_CODEC_STATE_ACTIVE)
1023                 return -EINVAL;
1024
1025         if (vdec->aborting)
1026                 return -EINVAL;
1027
1028         if (!vdec->req_frame_count)
1029                 return -EINVAL;
1030
1031         if (!vbuf)
1032                 return -EINVAL;
1033
1034         if (vdec->slots[vbuf->vb2_buf.index]) {
1035                 dev_err(inst->dev, "[%d] repeat alloc fs %d\n",
1036                         inst->id, vbuf->vb2_buf.index);
1037                 return -EINVAL;
1038         }
1039
1040         dev_dbg(inst->dev, "[%d] state = %d, alloc fs %d, tag = 0x%x\n",
1041                 inst->id, inst->state, vbuf->vb2_buf.index, vdec->seq_tag);
1042         vpu_buf = to_vpu_vb2_buffer(vbuf);
1043
1044         memset(&info, 0, sizeof(info));
1045         info.id = vbuf->vb2_buf.index;
1046         info.type = MEM_RES_FRAME;
1047         info.tag = vdec->seq_tag;
1048         info.luma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 0);
1049         info.luma_size = inst->cap_format.sizeimage[0];
1050         if (vbuf->vb2_buf.num_planes > 1)
1051                 info.chroma_addr = vpu_get_vb_phy_addr(&vbuf->vb2_buf, 1);
1052         else
1053                 info.chroma_addr = info.luma_addr + info.luma_size;
1054         info.chromau_size = inst->cap_format.sizeimage[1];
1055         info.bytesperline = inst->cap_format.bytesperline[0];
1056         ret = vpu_session_alloc_fs(inst, &info);
1057         if (ret)
1058                 return ret;
1059
1060         vpu_buf->tag = info.tag;
1061         vpu_buf->luma = info.luma_addr;
1062         vpu_buf->chroma_u = info.chroma_addr;
1063         vpu_buf->chroma_v = 0;
1064         vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1065         vdec->slots[info.id] = vpu_buf;
1066         vdec->req_frame_count--;
1067
1068         return 0;
1069 }
1070
1071 static void vdec_response_fs_request(struct vpu_inst *inst, bool force)
1072 {
1073         struct vdec_t *vdec = inst->priv;
1074         int i;
1075         int ret;
1076
1077         if (force) {
1078                 for (i = vdec->req_frame_count; i > 0; i--)
1079                         vdec_response_frame_abnormal(inst);
1080                 return;
1081         }
1082
1083         for (i = vdec->req_frame_count; i > 0; i--) {
1084                 ret = vpu_process_capture_buffer(inst);
1085                 if (ret)
1086                         break;
1087                 if (vdec->eos_received)
1088                         break;
1089         }
1090
1091         for (i = vdec->mbi.index; i < vdec->mbi.count; i++) {
1092                 if (vdec_response_fs(inst, &vdec->mbi))
1093                         break;
1094                 if (vdec->eos_received)
1095                         break;
1096         }
1097         for (i = vdec->dcp.index; i < vdec->dcp.count; i++) {
1098                 if (vdec_response_fs(inst, &vdec->dcp))
1099                         break;
1100                 if (vdec->eos_received)
1101                         break;
1102         }
1103 }
1104
1105 static void vdec_response_fs_release(struct vpu_inst *inst, u32 id, u32 tag)
1106 {
1107         struct vpu_fs_info info;
1108
1109         memset(&info, 0, sizeof(info));
1110         info.id = id;
1111         info.tag = tag;
1112         vpu_session_release_fs(inst, &info);
1113 }
1114
1115 static void vdec_recycle_buffer(struct vpu_inst *inst, struct vb2_v4l2_buffer *vbuf)
1116 {
1117         if (!inst->fh.m2m_ctx)
1118                 return;
1119         if (vbuf->vb2_buf.state != VB2_BUF_STATE_ACTIVE)
1120                 return;
1121         if (vpu_find_buf_by_idx(inst, vbuf->vb2_buf.type, vbuf->vb2_buf.index))
1122                 return;
1123         v4l2_m2m_buf_queue(inst->fh.m2m_ctx, vbuf);
1124 }
1125
1126 static void vdec_clear_slots(struct vpu_inst *inst)
1127 {
1128         struct vdec_t *vdec = inst->priv;
1129         struct vpu_vb2_buffer *vpu_buf;
1130         struct vb2_v4l2_buffer *vbuf;
1131         int i;
1132
1133         for (i = 0; i < ARRAY_SIZE(vdec->slots); i++) {
1134                 if (!vdec->slots[i])
1135                         continue;
1136
1137                 vpu_buf = vdec->slots[i];
1138                 vbuf = &vpu_buf->m2m_buf.vb;
1139
1140                 vpu_trace(inst->dev, "clear slot %d\n", i);
1141                 vdec_response_fs_release(inst, i, vpu_buf->tag);
1142                 vdec_recycle_buffer(inst, vbuf);
1143                 vdec->slots[i]->state = VPU_BUF_STATE_IDLE;
1144                 vdec->slots[i] = NULL;
1145         }
1146 }
1147
1148 static void vdec_event_seq_hdr(struct vpu_inst *inst, struct vpu_dec_codec_info *hdr)
1149 {
1150         struct vdec_t *vdec = inst->priv;
1151
1152         vpu_inst_lock(inst);
1153         memcpy(&vdec->codec_info, hdr, sizeof(vdec->codec_info));
1154
1155         vpu_trace(inst->dev, "[%d] %d x %d, crop : (%d, %d) %d x %d, %d, %d\n",
1156                   inst->id,
1157                   vdec->codec_info.decoded_width,
1158                   vdec->codec_info.decoded_height,
1159                   vdec->codec_info.offset_x,
1160                   vdec->codec_info.offset_y,
1161                   vdec->codec_info.width,
1162                   vdec->codec_info.height,
1163                   hdr->num_ref_frms,
1164                   hdr->num_dpb_frms);
1165         inst->min_buffer_cap = hdr->num_ref_frms + hdr->num_dpb_frms;
1166         vdec->is_source_changed = vdec_check_source_change(inst);
1167         vdec_init_fmt(inst);
1168         vdec_init_crop(inst);
1169         vdec_init_mbi(inst);
1170         vdec_init_dcp(inst);
1171         if (!vdec->seq_hdr_found) {
1172                 vdec->seq_tag = vdec->codec_info.tag;
1173                 if (vdec->is_source_changed) {
1174                         vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1175                         vdec->source_change++;
1176                         vdec_handle_resolution_change(inst);
1177                         vdec->is_source_changed = false;
1178                 }
1179         }
1180         if (vdec->seq_tag != vdec->codec_info.tag) {
1181                 vdec_response_fs_request(inst, true);
1182                 vpu_trace(inst->dev, "[%d] seq tag change: %d -> %d\n",
1183                           inst->id, vdec->seq_tag, vdec->codec_info.tag);
1184         }
1185         vdec->seq_hdr_found++;
1186         vdec->fixed_fmt = true;
1187         vpu_inst_unlock(inst);
1188 }
1189
1190 static void vdec_event_resolution_change(struct vpu_inst *inst)
1191 {
1192         struct vdec_t *vdec = inst->priv;
1193
1194         vpu_trace(inst->dev, "[%d]\n", inst->id);
1195         vpu_inst_lock(inst);
1196         vdec->seq_tag = vdec->codec_info.tag;
1197         vdec_clear_fs(&vdec->mbi);
1198         vdec_clear_fs(&vdec->dcp);
1199         vdec_clear_slots(inst);
1200         vdec_init_mbi(inst);
1201         vdec_init_dcp(inst);
1202         if (vdec->is_source_changed) {
1203                 vdec_update_state(inst, VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE, 0);
1204                 vdec->source_change++;
1205                 vdec_handle_resolution_change(inst);
1206                 vdec->is_source_changed = false;
1207         }
1208         vpu_inst_unlock(inst);
1209 }
1210
1211 static void vdec_event_req_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1212 {
1213         struct vdec_t *vdec = inst->priv;
1214
1215         if (!fs)
1216                 return;
1217
1218         vpu_inst_lock(inst);
1219
1220         switch (fs->type) {
1221         case MEM_RES_FRAME:
1222                 vdec->req_frame_count++;
1223                 break;
1224         case MEM_RES_MBI:
1225                 vdec_request_one_fs(&vdec->mbi);
1226                 break;
1227         case MEM_RES_DCP:
1228                 vdec_request_one_fs(&vdec->dcp);
1229                 break;
1230         default:
1231                 break;
1232         }
1233
1234         vdec_alloc_fs(inst, &vdec->mbi);
1235         vdec_alloc_fs(inst, &vdec->dcp);
1236
1237         vdec_response_fs_request(inst, false);
1238
1239         vpu_inst_unlock(inst);
1240 }
1241
1242 static void vdec_evnet_rel_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
1243 {
1244         struct vdec_t *vdec = inst->priv;
1245         struct vpu_vb2_buffer *vpu_buf;
1246         struct vb2_v4l2_buffer *vbuf;
1247
1248         if (!fs || fs->id >= ARRAY_SIZE(vdec->slots))
1249                 return;
1250         if (fs->type != MEM_RES_FRAME)
1251                 return;
1252
1253         if (fs->id >= vpu_get_num_buffers(inst, inst->cap_format.type)) {
1254                 dev_err(inst->dev, "[%d] invalid fs(%d) to release\n", inst->id, fs->id);
1255                 return;
1256         }
1257
1258         vpu_inst_lock(inst);
1259         vpu_buf = vdec->slots[fs->id];
1260         vdec->slots[fs->id] = NULL;
1261
1262         if (!vpu_buf) {
1263                 dev_dbg(inst->dev, "[%d] fs[%d] has bee released\n", inst->id, fs->id);
1264                 goto exit;
1265         }
1266
1267         vbuf = &vpu_buf->m2m_buf.vb;
1268         if (vpu_get_buffer_state(vbuf) == VPU_BUF_STATE_DECODED) {
1269                 dev_dbg(inst->dev, "[%d] frame skip\n", inst->id);
1270                 vdec->sequence++;
1271         }
1272
1273         vdec_response_fs_release(inst, fs->id, vpu_buf->tag);
1274         if (vpu_get_buffer_state(vbuf) != VPU_BUF_STATE_READY)
1275                 vdec_recycle_buffer(inst, vbuf);
1276
1277         vpu_set_buffer_state(vbuf, VPU_BUF_STATE_IDLE);
1278         vpu_process_capture_buffer(inst);
1279
1280 exit:
1281         vpu_inst_unlock(inst);
1282 }
1283
1284 static void vdec_event_eos(struct vpu_inst *inst)
1285 {
1286         struct vdec_t *vdec = inst->priv;
1287
1288         vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1289                   inst->id,
1290                   vdec->params.frame_count,
1291                   vdec->decoded_frame_count,
1292                   vdec->display_frame_count,
1293                   vdec->sequence);
1294         vpu_inst_lock(inst);
1295         vdec->eos_received++;
1296         vdec->fixed_fmt = false;
1297         inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1298         vdec_set_last_buffer_dequeued(inst);
1299         vpu_inst_unlock(inst);
1300 }
1301
1302 static void vdec_event_notify(struct vpu_inst *inst, u32 event, void *data)
1303 {
1304         switch (event) {
1305         case VPU_MSG_ID_SEQ_HDR_FOUND:
1306                 vdec_event_seq_hdr(inst, data);
1307                 break;
1308         case VPU_MSG_ID_RES_CHANGE:
1309                 vdec_event_resolution_change(inst);
1310                 break;
1311         case VPU_MSG_ID_FRAME_REQ:
1312                 vdec_event_req_fs(inst, data);
1313                 break;
1314         case VPU_MSG_ID_FRAME_RELEASE:
1315                 vdec_evnet_rel_fs(inst, data);
1316                 break;
1317         case VPU_MSG_ID_PIC_EOS:
1318                 vdec_event_eos(inst);
1319                 break;
1320         default:
1321                 break;
1322         }
1323 }
1324
1325 static int vdec_process_output(struct vpu_inst *inst, struct vb2_buffer *vb)
1326 {
1327         struct vdec_t *vdec = inst->priv;
1328         struct vb2_v4l2_buffer *vbuf;
1329         struct vpu_rpc_buffer_desc desc;
1330         u32 free_space;
1331         int ret;
1332
1333         vbuf = to_vb2_v4l2_buffer(vb);
1334         dev_dbg(inst->dev, "[%d] dec output [%d] %d : %ld\n",
1335                 inst->id, vbuf->sequence, vb->index, vb2_get_plane_payload(vb, 0));
1336
1337         if (inst->state == VPU_CODEC_STATE_DEINIT)
1338                 return -EINVAL;
1339         if (vdec->reset_codec)
1340                 return -EINVAL;
1341
1342         if (inst->state == VPU_CODEC_STATE_STARTED)
1343                 vdec_update_state(inst, VPU_CODEC_STATE_ACTIVE, 0);
1344
1345         ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1346         if (ret)
1347                 return ret;
1348
1349         free_space = vpu_helper_get_free_space(inst);
1350         if (free_space < vb2_get_plane_payload(vb, 0) + 0x40000)
1351                 return -ENOMEM;
1352
1353         vpu_set_buffer_state(vbuf, VPU_BUF_STATE_INUSE);
1354         ret = vpu_iface_input_frame(inst, vb);
1355         if (ret < 0)
1356                 return -ENOMEM;
1357
1358         dev_dbg(inst->dev, "[%d][INPUT  TS]%32lld\n", inst->id, vb->timestamp);
1359         vdec->params.frame_count++;
1360
1361         if (vdec->drain)
1362                 vdec_drain(inst);
1363
1364         return 0;
1365 }
1366
1367 static int vdec_process_capture(struct vpu_inst *inst, struct vb2_buffer *vb)
1368 {
1369         struct vdec_t *vdec = inst->priv;
1370         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1371         int ret;
1372
1373         if (inst->state == VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE)
1374                 return -EINVAL;
1375         if (vdec->reset_codec)
1376                 return -EINVAL;
1377
1378         ret = vdec_response_frame(inst, vbuf);
1379         if (ret)
1380                 return ret;
1381         v4l2_m2m_dst_buf_remove_by_buf(inst->fh.m2m_ctx, vbuf);
1382         return 0;
1383 }
1384
1385 static void vdec_on_queue_empty(struct vpu_inst *inst, u32 type)
1386 {
1387         struct vdec_t *vdec = inst->priv;
1388
1389         if (V4L2_TYPE_IS_OUTPUT(type))
1390                 return;
1391
1392         vdec_handle_resolution_change(inst);
1393         if (vdec->eos_received)
1394                 vdec_set_last_buffer_dequeued(inst);
1395 }
1396
1397 static void vdec_abort(struct vpu_inst *inst)
1398 {
1399         struct vdec_t *vdec = inst->priv;
1400         struct vpu_rpc_buffer_desc desc;
1401         int ret;
1402
1403         vpu_trace(inst->dev, "[%d] state = %d\n", inst->id, inst->state);
1404
1405         vdec->aborting = true;
1406         vpu_iface_add_scode(inst, SCODE_PADDING_ABORT);
1407         vdec->params.end_flag = 1;
1408         vpu_iface_set_decode_params(inst, &vdec->params, 1);
1409
1410         vpu_session_abort(inst);
1411
1412         ret = vpu_iface_get_stream_buffer_desc(inst, &desc);
1413         if (!ret)
1414                 vpu_iface_update_stream_buffer(inst, desc.rptr, 1);
1415
1416         vpu_session_rst_buf(inst);
1417         vpu_trace(inst->dev, "[%d] input : %d, decoded : %d, display : %d, sequence : %d\n",
1418                   inst->id,
1419                   vdec->params.frame_count,
1420                   vdec->decoded_frame_count,
1421                   vdec->display_frame_count,
1422                   vdec->sequence);
1423         if (!vdec->seq_hdr_found)
1424                 vdec->reset_codec = true;
1425         vdec->params.end_flag = 0;
1426         vdec->drain = 0;
1427         vdec->params.frame_count = 0;
1428         vdec->decoded_frame_count = 0;
1429         vdec->display_frame_count = 0;
1430         vdec->sequence = 0;
1431         vdec->aborting = false;
1432         inst->extra_size = 0;
1433 }
1434
1435 static void vdec_stop(struct vpu_inst *inst, bool free)
1436 {
1437         struct vdec_t *vdec = inst->priv;
1438
1439         vdec_clear_slots(inst);
1440         if (inst->state != VPU_CODEC_STATE_DEINIT)
1441                 vpu_session_stop(inst);
1442         vdec_clear_fs(&vdec->mbi);
1443         vdec_clear_fs(&vdec->dcp);
1444         if (free) {
1445                 vpu_free_dma(&vdec->udata);
1446                 vpu_free_dma(&inst->stream_buffer);
1447         }
1448         vdec_update_state(inst, VPU_CODEC_STATE_DEINIT, 1);
1449         vdec->reset_codec = false;
1450 }
1451
1452 static void vdec_release(struct vpu_inst *inst)
1453 {
1454         if (inst->id != VPU_INST_NULL_ID)
1455                 vpu_trace(inst->dev, "[%d]\n", inst->id);
1456         vpu_inst_lock(inst);
1457         vdec_stop(inst, true);
1458         vpu_inst_unlock(inst);
1459 }
1460
1461 static void vdec_cleanup(struct vpu_inst *inst)
1462 {
1463         struct vdec_t *vdec;
1464
1465         if (!inst)
1466                 return;
1467
1468         vdec = inst->priv;
1469         vfree(vdec);
1470         inst->priv = NULL;
1471         vfree(inst);
1472 }
1473
1474 static void vdec_init_params(struct vdec_t *vdec)
1475 {
1476         vdec->params.frame_count = 0;
1477         vdec->params.end_flag = 0;
1478 }
1479
1480 static int vdec_start(struct vpu_inst *inst)
1481 {
1482         struct vdec_t *vdec = inst->priv;
1483         int stream_buffer_size;
1484         int ret;
1485
1486         if (inst->state != VPU_CODEC_STATE_DEINIT)
1487                 return 0;
1488
1489         vpu_trace(inst->dev, "[%d]\n", inst->id);
1490         if (!vdec->udata.virt) {
1491                 vdec->udata.length = 0x1000;
1492                 ret = vpu_alloc_dma(inst->core, &vdec->udata);
1493                 if (ret) {
1494                         dev_err(inst->dev, "[%d] alloc udata fail\n", inst->id);
1495                         goto error;
1496                 }
1497         }
1498
1499         if (!inst->stream_buffer.virt) {
1500                 stream_buffer_size = vpu_iface_get_stream_buffer_size(inst->core);
1501                 if (stream_buffer_size > 0) {
1502                         inst->stream_buffer.length = stream_buffer_size;
1503                         ret = vpu_alloc_dma(inst->core, &inst->stream_buffer);
1504                         if (ret) {
1505                                 dev_err(inst->dev, "[%d] alloc stream buffer fail\n", inst->id);
1506                                 goto error;
1507                         }
1508                         inst->use_stream_buffer = true;
1509                 }
1510         }
1511
1512         if (inst->use_stream_buffer)
1513                 vpu_iface_config_stream_buffer(inst, &inst->stream_buffer);
1514         vpu_iface_init_instance(inst);
1515         vdec->params.udata.base = vdec->udata.phys;
1516         vdec->params.udata.size = vdec->udata.length;
1517         ret = vpu_iface_set_decode_params(inst, &vdec->params, 0);
1518         if (ret) {
1519                 dev_err(inst->dev, "[%d] set decode params fail\n", inst->id);
1520                 goto error;
1521         }
1522
1523         vdec_init_params(vdec);
1524         ret = vpu_session_start(inst);
1525         if (ret) {
1526                 dev_err(inst->dev, "[%d] start fail\n", inst->id);
1527                 goto error;
1528         }
1529
1530         vdec_update_state(inst, VPU_CODEC_STATE_STARTED, 0);
1531
1532         return 0;
1533 error:
1534         vpu_free_dma(&vdec->udata);
1535         vpu_free_dma(&inst->stream_buffer);
1536         return ret;
1537 }
1538
1539 static int vdec_start_session(struct vpu_inst *inst, u32 type)
1540 {
1541         struct vdec_t *vdec = inst->priv;
1542         int ret = 0;
1543
1544         if (V4L2_TYPE_IS_OUTPUT(type)) {
1545                 if (vdec->reset_codec)
1546                         vdec_stop(inst, false);
1547                 if (inst->state == VPU_CODEC_STATE_DEINIT) {
1548                         ret = vdec_start(inst);
1549                         if (ret)
1550                                 return ret;
1551                 }
1552         }
1553
1554         if (V4L2_TYPE_IS_OUTPUT(type)) {
1555                 vdec_update_state(inst, vdec->state, 1);
1556                 vdec->eos_received = 0;
1557                 vpu_process_output_buffer(inst);
1558         } else {
1559                 vdec_cmd_start(inst);
1560         }
1561         if (inst->state == VPU_CODEC_STATE_ACTIVE)
1562                 vdec_response_fs_request(inst, false);
1563
1564         return ret;
1565 }
1566
1567 static int vdec_stop_session(struct vpu_inst *inst, u32 type)
1568 {
1569         struct vdec_t *vdec = inst->priv;
1570
1571         if (inst->state == VPU_CODEC_STATE_DEINIT)
1572                 return 0;
1573
1574         if (V4L2_TYPE_IS_OUTPUT(type)) {
1575                 vdec_update_state(inst, VPU_CODEC_STATE_SEEK, 0);
1576                 vdec->drain = 0;
1577         } else {
1578                 if (inst->state != VPU_CODEC_STATE_DYAMIC_RESOLUTION_CHANGE) {
1579                         vdec_abort(inst);
1580                         vdec->eos_received = 0;
1581                 }
1582                 vdec_clear_slots(inst);
1583         }
1584
1585         return 0;
1586 }
1587
1588 static int vdec_get_debug_info(struct vpu_inst *inst, char *str, u32 size, u32 i)
1589 {
1590         struct vdec_t *vdec = inst->priv;
1591         int num = -1;
1592
1593         switch (i) {
1594         case 0:
1595                 num = scnprintf(str, size,
1596                                 "req_frame_count = %d\ninterlaced = %d\n",
1597                                 vdec->req_frame_count,
1598                                 vdec->codec_info.progressive ? 0 : 1);
1599                 break;
1600         case 1:
1601                 num = scnprintf(str, size,
1602                                 "mbi: size = 0x%x request = %d, alloc = %d, response = %d\n",
1603                                 vdec->mbi.size,
1604                                 vdec->mbi.req_count,
1605                                 vdec->mbi.count,
1606                                 vdec->mbi.index);
1607                 break;
1608         case 2:
1609                 num = scnprintf(str, size,
1610                                 "dcp: size = 0x%x request = %d, alloc = %d, response = %d\n",
1611                                 vdec->dcp.size,
1612                                 vdec->dcp.req_count,
1613                                 vdec->dcp.count,
1614                                 vdec->dcp.index);
1615                 break;
1616         case 3:
1617                 num = scnprintf(str, size, "input_frame_count = %d\n", vdec->params.frame_count);
1618                 break;
1619         case 4:
1620                 num = scnprintf(str, size, "decoded_frame_count = %d\n", vdec->decoded_frame_count);
1621                 break;
1622         case 5:
1623                 num = scnprintf(str, size, "display_frame_count = %d\n", vdec->display_frame_count);
1624                 break;
1625         case 6:
1626                 num = scnprintf(str, size, "sequence = %d\n", vdec->sequence);
1627                 break;
1628         case 7:
1629                 num = scnprintf(str, size, "drain = %d, eos = %d, source_change = %d\n",
1630                                 vdec->drain, vdec->eos_received, vdec->source_change);
1631                 break;
1632         case 8:
1633                 num = scnprintf(str, size, "fps = %d/%d\n",
1634                                 vdec->codec_info.frame_rate.numerator,
1635                                 vdec->codec_info.frame_rate.denominator);
1636                 break;
1637         case 9:
1638                 num = scnprintf(str, size, "colorspace: %d, %d, %d, %d (%d)\n",
1639                                 vdec->codec_info.color_primaries,
1640                                 vdec->codec_info.transfer_chars,
1641                                 vdec->codec_info.matrix_coeffs,
1642                                 vdec->codec_info.full_range,
1643                                 vdec->codec_info.vui_present);
1644                 break;
1645         default:
1646                 break;
1647         }
1648
1649         return num;
1650 }
1651
1652 static struct vpu_inst_ops vdec_inst_ops = {
1653         .ctrl_init = vdec_ctrl_init,
1654         .check_ready = vdec_check_ready,
1655         .buf_done = vdec_buf_done,
1656         .get_one_frame = vdec_frame_decoded,
1657         .stop_done = vdec_stop_done,
1658         .event_notify = vdec_event_notify,
1659         .release = vdec_release,
1660         .cleanup = vdec_cleanup,
1661         .start = vdec_start_session,
1662         .stop = vdec_stop_session,
1663         .process_output = vdec_process_output,
1664         .process_capture = vdec_process_capture,
1665         .on_queue_empty = vdec_on_queue_empty,
1666         .get_debug_info = vdec_get_debug_info,
1667         .wait_prepare = vpu_inst_unlock,
1668         .wait_finish = vpu_inst_lock,
1669 };
1670
1671 static void vdec_init(struct file *file)
1672 {
1673         struct vpu_inst *inst = to_inst(file);
1674         struct v4l2_format f;
1675
1676         memset(&f, 0, sizeof(f));
1677         f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1678         f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_H264;
1679         f.fmt.pix_mp.width = 1280;
1680         f.fmt.pix_mp.height = 720;
1681         f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1682         vdec_s_fmt(file, &inst->fh, &f);
1683
1684         memset(&f, 0, sizeof(f));
1685         f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1686         f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12M_8L128;
1687         f.fmt.pix_mp.width = 1280;
1688         f.fmt.pix_mp.height = 720;
1689         f.fmt.pix_mp.field = V4L2_FIELD_NONE;
1690         vdec_s_fmt(file, &inst->fh, &f);
1691 }
1692
1693 static int vdec_open(struct file *file)
1694 {
1695         struct vpu_inst *inst;
1696         struct vdec_t *vdec;
1697         int ret;
1698
1699         inst = vzalloc(sizeof(*inst));
1700         if (!inst)
1701                 return -ENOMEM;
1702
1703         vdec = vzalloc(sizeof(*vdec));
1704         if (!vdec) {
1705                 vfree(inst);
1706                 return -ENOMEM;
1707         }
1708
1709         inst->ops = &vdec_inst_ops;
1710         inst->formats = vdec_formats;
1711         inst->type = VPU_CORE_TYPE_DEC;
1712         inst->priv = vdec;
1713
1714         ret = vpu_v4l2_open(file, inst);
1715         if (ret)
1716                 return ret;
1717
1718         vdec->fixed_fmt = false;
1719         vdec->state = VPU_CODEC_STATE_ACTIVE;
1720         inst->min_buffer_cap = VDEC_MIN_BUFFER_CAP;
1721         inst->min_buffer_out = VDEC_MIN_BUFFER_OUT;
1722         vdec_init(file);
1723
1724         return 0;
1725 }
1726
1727 static const struct v4l2_file_operations vdec_fops = {
1728         .owner = THIS_MODULE,
1729         .open = vdec_open,
1730         .release = vpu_v4l2_close,
1731         .unlocked_ioctl = video_ioctl2,
1732         .poll = v4l2_m2m_fop_poll,
1733         .mmap = v4l2_m2m_fop_mmap,
1734 };
1735
1736 const struct v4l2_ioctl_ops *vdec_get_ioctl_ops(void)
1737 {
1738         return &vdec_ioctl_ops;
1739 }
1740
1741 const struct v4l2_file_operations *vdec_get_fops(void)
1742 {
1743         return &vdec_fops;
1744 }
This page took 0.179865 seconds and 4 git commands to generate.