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