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