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