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