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