• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/amlogic/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 
63 static 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 
121 static 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 
182 extern bool multiplanar;
183 extern bool dump_capture_frame;
184 
185 extern int dmabuf_fd_install_data(int fd, void* data, u32 size);
186 extern bool is_v4l2_buf_file(struct file *file);
187 
aml_vcodec_ctx_lock(struct aml_vcodec_ctx * ctx)188 static 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 
aml_vcodec_ctx_unlock(struct aml_vcodec_ctx * ctx,ulong flags)197 static void aml_vcodec_ctx_unlock(struct aml_vcodec_ctx *ctx, ulong flags)
198 {
199 	spin_unlock_irqrestore(&ctx->slock, flags);
200 }
201 
aml_vdec_find_format(struct v4l2_format * f)202 static 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 
aml_vdec_get_q_data(struct aml_vcodec_ctx * ctx,enum v4l2_buf_type type)216 static 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 
aml_vdec_dispatch_event(struct aml_vcodec_ctx * ctx,u32 changes)225 void 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 
aml_vdec_flush_decoder(struct aml_vcodec_ctx * ctx)260 static 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 
aml_vdec_pic_info_update(struct aml_vcodec_ctx * ctx)267 static 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 
aml_check_inst_quit(struct aml_vcodec_dev * dev,struct aml_vcodec_ctx * inst,u32 id)309 static 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 	}
333 out:
334 	mutex_unlock(&dev->dev_mutex);
335 
336 	return ret;
337 }
338 
vdec_frame_buffer_release(void * data)339 void 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 
get_fb_from_queue(struct aml_vcodec_ctx * ctx,struct vdec_v4l2_buffer ** out_fb)378 int 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 }
479 EXPORT_SYMBOL(get_fb_from_queue);
480 
put_fb_to_queue(struct aml_vcodec_ctx * ctx,struct vdec_v4l2_buffer * in_fb)481 int 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;
506 out:
507 	mutex_unlock(&ctx->lock);
508 
509 	return 0;
510 
511 }
512 EXPORT_SYMBOL(put_fb_to_queue);
513 
trans_vframe_to_user(struct aml_vcodec_ctx * ctx,struct vdec_v4l2_buffer * fb)514 void 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 
get_display_buffer(struct aml_vcodec_ctx * ctx,struct vdec_v4l2_buffer ** out)617 static 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 
aml_check_dpb_ready(struct aml_vcodec_ctx * ctx)639 static 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 
is_vdec_ready(struct aml_vcodec_ctx * ctx)656 static 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 
is_enough_work_items(struct aml_vcodec_ctx * ctx)696 static 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 
aml_wait_dpb_ready(struct aml_vcodec_ctx * ctx)708 static 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 
aml_recycle_dma_buffers(struct aml_vcodec_ctx * ctx)728 static 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 
aml_vdec_worker(struct work_struct * work)752 static 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);
900 out:
901 	return;
902 }
903 
aml_vdec_reset(struct aml_vcodec_ctx * ctx)904 static 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 
936 out:
937 	complete(&ctx->comp);
938 	return;
939 }
940 
wait_vcodec_ending(struct aml_vcodec_ctx * ctx)941 void 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 
try_to_capture(struct aml_vcodec_ctx * ctx)963 void 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 }
977 EXPORT_SYMBOL_GPL(try_to_capture);
978 
vdec_thread(void * data)979 static 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 
aml_thread_notify(struct aml_vcodec_ctx * ctx,enum aml_thread_type type)1012 void 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 }
1027 EXPORT_SYMBOL_GPL(aml_thread_notify);
1028 
aml_thread_start(struct aml_vcodec_ctx * ctx,aml_thread_func func,enum aml_thread_type type,const char * thread_name)1029 int 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 
1057 err:
1058 	kfree(thread);
1059 
1060 	return ret;
1061 }
1062 EXPORT_SYMBOL_GPL(aml_thread_start);
1063 
aml_thread_stop(struct aml_vcodec_ctx * ctx)1064 void 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 }
1082 EXPORT_SYMBOL_GPL(aml_thread_stop);
1083 
vidioc_try_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)1084 static 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 
vidioc_decoder_cmd(struct file * file,void * priv,struct v4l2_decoder_cmd * cmd)1108 static 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 
vidioc_decoder_streamon(struct file * file,void * priv,enum v4l2_buf_type i)1172 static 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 
vidioc_decoder_streamoff(struct file * file,void * priv,enum v4l2_buf_type i)1207 static 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 
vidioc_decoder_reqbufs(struct file * file,void * priv,struct v4l2_requestbuffers * rb)1225 static 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 	} else {
1250 		if (rb->memory == VB2_MEMORY_DMABUF) {
1251 			v4l_dbg(ctx, V4L_DEBUG_CODEC_INPUT,
1252 					"%s, output_dma_mode set", __func__);
1253 			ctx->output_dma_mode = true;
1254 		}
1255 	}
1256 
1257 	return v4l2_m2m_ioctl_reqbufs(file, priv, rb);
1258 }
1259 
vidioc_vdec_querybuf(struct file * file,void * priv,struct v4l2_buffer * buf)1260 static int vidioc_vdec_querybuf(struct file *file, void *priv,
1261 	struct v4l2_buffer *buf)
1262 {
1263 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1264 
1265 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1266 		"%s, type: %d\n", __func__, buf->type);
1267 
1268 	return v4l2_m2m_ioctl_querybuf(file, priv, buf);
1269 }
1270 
vidioc_vdec_expbuf(struct file * file,void * priv,struct v4l2_exportbuffer * eb)1271 static int vidioc_vdec_expbuf(struct file *file, void *priv,
1272 	struct v4l2_exportbuffer *eb)
1273 {
1274 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1275 
1276 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1277 		"%s, type: %d\n", __func__, eb->type);
1278 
1279 	return v4l2_m2m_ioctl_expbuf(file, priv, eb);
1280 }
1281 
aml_vcodec_dec_release(struct aml_vcodec_ctx * ctx)1282 void aml_vcodec_dec_release(struct aml_vcodec_ctx *ctx)
1283 {
1284 	ulong flags;
1285 
1286 	flags = aml_vcodec_ctx_lock(ctx);
1287 	ctx->state = AML_STATE_ABORT;
1288 	ATRACE_COUNTER("v4l2_state", ctx->state);
1289 	v4l_dbg(ctx, V4L_DEBUG_CODEC_STATE,
1290 		"vcodec state (AML_STATE_ABORT)\n");
1291 	aml_vcodec_ctx_unlock(ctx, flags);
1292 
1293 	vdec_if_deinit(ctx);
1294 }
1295 
aml_vcodec_dec_set_default_params(struct aml_vcodec_ctx * ctx)1296 void aml_vcodec_dec_set_default_params(struct aml_vcodec_ctx *ctx)
1297 {
1298 	struct aml_q_data *q_data;
1299 
1300 	ctx->m2m_ctx->q_lock = &ctx->dev->dev_mutex;
1301 	ctx->fh.m2m_ctx = ctx->m2m_ctx;
1302 	ctx->fh.ctrl_handler = &ctx->ctrl_hdl;
1303 	INIT_WORK(&ctx->decode_work, aml_vdec_worker);
1304 	ctx->colorspace = V4L2_COLORSPACE_REC709;
1305 	ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1306 	ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
1307 	ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
1308 	ctx->dev->dec_capability = 0;//VCODEC_CAPABILITY_4K_DISABLED;//disable 4k
1309 
1310 	q_data = &ctx->q_data[AML_Q_DATA_SRC];
1311 	memset(q_data, 0, sizeof(struct aml_q_data));
1312 	q_data->visible_width = DFT_CFG_WIDTH;
1313 	q_data->visible_height = DFT_CFG_HEIGHT;
1314 	q_data->fmt = &aml_video_formats[OUT_FMT_IDX];
1315 	q_data->field = V4L2_FIELD_NONE;
1316 
1317 	q_data->sizeimage[0] = (1024 * 1024);//DFT_CFG_WIDTH * DFT_CFG_HEIGHT; //1m
1318 	q_data->bytesperline[0] = 0;
1319 
1320 	q_data = &ctx->q_data[AML_Q_DATA_DST];
1321 	memset(q_data, 0, sizeof(struct aml_q_data));
1322 	q_data->visible_width = DFT_CFG_WIDTH;
1323 	q_data->visible_height = DFT_CFG_HEIGHT;
1324 	q_data->coded_width = DFT_CFG_WIDTH;
1325 	q_data->coded_height = DFT_CFG_HEIGHT;
1326 	q_data->fmt = &aml_video_formats[CAP_FMT_IDX];
1327 	q_data->field = V4L2_FIELD_NONE;
1328 
1329 	v4l_bound_align_image(&q_data->coded_width,
1330 				AML_VDEC_MIN_W,
1331 				AML_VDEC_MAX_W, 4,
1332 				&q_data->coded_height,
1333 				AML_VDEC_MIN_H,
1334 				AML_VDEC_MAX_H, 5, 6);
1335 
1336 	q_data->sizeimage[0] = q_data->coded_width * q_data->coded_height;
1337 	q_data->bytesperline[0] = q_data->coded_width;
1338 	q_data->sizeimage[1] = q_data->sizeimage[0] / 2;
1339 	q_data->bytesperline[1] = q_data->coded_width;
1340 	ctx->reset_flag = V4L_RESET_MODE_NORMAL;
1341 
1342 	ctx->state = AML_STATE_IDLE;
1343 	ATRACE_COUNTER("v4l2_state", ctx->state);
1344 	v4l_dbg(ctx, V4L_DEBUG_CODEC_STATE,
1345 		"vcodec state (AML_STATE_IDLE)\n");
1346 }
1347 
vidioc_vdec_qbuf(struct file * file,void * priv,struct v4l2_buffer * buf)1348 static int vidioc_vdec_qbuf(struct file *file, void *priv,
1349 	struct v4l2_buffer *buf)
1350 {
1351 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1352 	int ret;
1353 
1354 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1355 		"%s, type: %d\n", __func__, buf->type);
1356 
1357 	if (ctx->state == AML_STATE_ABORT) {
1358 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1359 			"Call on QBUF after unrecoverable error, type = %s\n",
1360 			V4L2_TYPE_IS_OUTPUT(buf->type) ? "OUT" : "IN");
1361 		return -EIO;
1362 	}
1363 
1364 	ret = v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
1365 
1366 	if (V4L2_TYPE_IS_OUTPUT(buf->type)) {
1367 		if (ret == -EAGAIN)
1368 			ATRACE_COUNTER("v4l2_qbuf_eagain", 0);
1369 		else
1370 			ATRACE_COUNTER("v4l2_qbuf_ok", 0);
1371 	}
1372 	return ret;
1373 }
1374 
vidioc_vdec_dqbuf(struct file * file,void * priv,struct v4l2_buffer * buf)1375 static int vidioc_vdec_dqbuf(struct file *file, void *priv,
1376 	struct v4l2_buffer *buf)
1377 {
1378 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1379 	int ret;
1380 
1381 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1382 		"%s, type: %d\n", __func__, buf->type);
1383 
1384 	if (ctx->state == AML_STATE_ABORT) {
1385 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1386 			"Call on DQBUF after unrecoverable error, type = %s\n",
1387 			V4L2_TYPE_IS_OUTPUT(buf->type) ? "OUT" : "IN");
1388 		if (!V4L2_TYPE_IS_OUTPUT(buf->type))
1389 			return -EIO;
1390 	}
1391 
1392 	ret = v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
1393 	if (V4L2_TYPE_IS_OUTPUT(buf->type)) {
1394 		if (ret == -EAGAIN)
1395 			ATRACE_COUNTER("v4l2_dqin_eagain", 0);
1396 		else
1397 			ATRACE_COUNTER("v4l2_dqin_ok", 0);
1398 	} else {
1399 		if (ret == -EAGAIN)
1400 			ATRACE_COUNTER("v4l2_dqout_eagain", 0);
1401 	}
1402 
1403 	if (!ret && !V4L2_TYPE_IS_OUTPUT(buf->type)) {
1404 		struct vb2_queue *vq;
1405 		struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
1406 		struct aml_video_dec_buf *aml_buf = NULL;
1407 		struct file *file = NULL;
1408 
1409 		if (ctx->is_drm_mode && ctx->output_dma_mode)
1410 			aml_recycle_dma_buffers(ctx);
1411 
1412 		vq = v4l2_m2m_get_vq(ctx->m2m_ctx, buf->type);
1413 		vb2_v4l2 = to_vb2_v4l2_buffer(vq->bufs[buf->index]);
1414 		aml_buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb);
1415 		aml_buf->privdata.v4l_dev_handle	= (ulong) ctx->dev;
1416 		aml_buf->privdata.v4l_inst_handle	= (ulong) ctx;
1417 		aml_buf->privdata.v4l_inst_id		= ctx->id;
1418 
1419 		file = fget(vb2_v4l2->private);
1420 		if (is_v4l2_buf_file(file)) {
1421 			dmabuf_fd_install_data(vb2_v4l2->private,
1422 				(void*)&aml_buf->privdata,
1423 				sizeof(struct file_private_data));
1424 			ATRACE_COUNTER("v4l2_dqout_ok", aml_buf->privdata.vf.index_disp);
1425 			v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO, "disp: %d, vf: %lx\n",
1426 				aml_buf->privdata.vf.index_disp,
1427 				(ulong) v4l_get_vf_handle(vb2_v4l2->private));
1428 		}
1429 		fput(file);
1430 	}
1431 
1432 	return ret;
1433 }
1434 
vidioc_vdec_querycap(struct file * file,void * priv,struct v4l2_capability * cap)1435 static int vidioc_vdec_querycap(struct file *file, void *priv,
1436 	struct v4l2_capability *cap)
1437 {
1438 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1439 
1440 	strlcpy(cap->driver, AML_VCODEC_DEC_NAME, sizeof(cap->driver));
1441 	strlcpy(cap->bus_info, AML_PLATFORM_STR, sizeof(cap->bus_info));
1442 	strlcpy(cap->card, AML_PLATFORM_STR, sizeof(cap->card));
1443 
1444 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, %s\n", __func__, cap->card);
1445 
1446 	return 0;
1447 }
1448 
vidioc_vdec_subscribe_evt(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)1449 static int vidioc_vdec_subscribe_evt(struct v4l2_fh *fh,
1450 	const struct v4l2_event_subscription *sub)
1451 {
1452 	struct aml_vcodec_ctx *ctx = fh_to_ctx(fh);
1453 
1454 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1455 		"%s, type: %d\n", __func__, sub->type);
1456 
1457 	switch (sub->type) {
1458 	case V4L2_EVENT_EOS:
1459 		return v4l2_event_subscribe(fh, sub, 2, NULL);
1460 	case V4L2_EVENT_SOURCE_CHANGE:
1461 		return v4l2_src_change_event_subscribe(fh, sub);
1462 	default:
1463 		return v4l2_ctrl_subscribe_event(fh, sub);
1464 	}
1465 }
1466 
vidioc_vdec_event_unsubscribe(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)1467 static int vidioc_vdec_event_unsubscribe(struct v4l2_fh *fh,
1468 	const struct v4l2_event_subscription *sub)
1469 {
1470 	struct aml_vcodec_ctx *ctx = fh_to_ctx(fh);
1471 
1472 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, type: %d\n",
1473 		__func__, sub->type);
1474 
1475 	return v4l2_event_unsubscribe(fh, sub);
1476 }
1477 
vidioc_try_fmt(struct v4l2_format * f,struct aml_video_fmt * fmt)1478 static int vidioc_try_fmt(struct v4l2_format *f, struct aml_video_fmt *fmt)
1479 {
1480 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1481 	int i;
1482 
1483 	pix_fmt_mp->field = V4L2_FIELD_NONE;
1484 
1485 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1486 		pix_fmt_mp->num_planes = 1;
1487 		pix_fmt_mp->plane_fmt[0].bytesperline = 0;
1488 	} else if (!V4L2_TYPE_IS_OUTPUT(f->type)) {
1489 		int tmp_w, tmp_h;
1490 
1491 		pix_fmt_mp->height = clamp(pix_fmt_mp->height,
1492 					AML_VDEC_MIN_H,
1493 					AML_VDEC_MAX_H);
1494 		pix_fmt_mp->width = clamp(pix_fmt_mp->width,
1495 					AML_VDEC_MIN_W,
1496 					AML_VDEC_MAX_W);
1497 
1498 		/*
1499 		 * Find next closer width align 64, heign align 64, size align
1500 		 * 64 rectangle
1501 		 * Note: This only get default value, the real HW needed value
1502 		 *       only available when ctx in AML_STATE_PROBE state
1503 		 */
1504 		tmp_w = pix_fmt_mp->width;
1505 		tmp_h = pix_fmt_mp->height;
1506 		v4l_bound_align_image(&pix_fmt_mp->width,
1507 					AML_VDEC_MIN_W,
1508 					AML_VDEC_MAX_W, 6,
1509 					&pix_fmt_mp->height,
1510 					AML_VDEC_MIN_H,
1511 					AML_VDEC_MAX_H, 6, 9);
1512 
1513 		if (pix_fmt_mp->width < tmp_w &&
1514 			(pix_fmt_mp->width + 64) <= AML_VDEC_MAX_W)
1515 			pix_fmt_mp->width += 64;
1516 		if (pix_fmt_mp->height < tmp_h &&
1517 			(pix_fmt_mp->height + 64) <= AML_VDEC_MAX_H)
1518 			pix_fmt_mp->height += 64;
1519 
1520 		pix_fmt_mp->num_planes = fmt->num_planes;
1521 		pix_fmt_mp->plane_fmt[0].sizeimage =
1522 				pix_fmt_mp->width * pix_fmt_mp->height;
1523 		pix_fmt_mp->plane_fmt[0].bytesperline = pix_fmt_mp->width;
1524 
1525 		if (pix_fmt_mp->num_planes == 2) {
1526 			pix_fmt_mp->plane_fmt[1].sizeimage =
1527 				(pix_fmt_mp->width * pix_fmt_mp->height) / 2;
1528 			pix_fmt_mp->plane_fmt[1].bytesperline =
1529 				pix_fmt_mp->width;
1530 		}
1531 	}
1532 
1533 	for (i = 0; i < pix_fmt_mp->num_planes; i++)
1534 		memset(&(pix_fmt_mp->plane_fmt[i].reserved[0]), 0x0,
1535 			   sizeof(pix_fmt_mp->plane_fmt[0].reserved));
1536 
1537 	pix_fmt_mp->flags = 0;
1538 	memset(&pix_fmt_mp->reserved, 0x0, sizeof(pix_fmt_mp->reserved));
1539 	return 0;
1540 }
1541 
vidioc_try_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_format * f)1542 static int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
1543 				struct v4l2_format *f)
1544 {
1545 	struct aml_video_fmt *fmt = NULL;
1546 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1547 
1548 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1549 		"%s, type: %u, planes: %u, fmt: %u\n",
1550 		__func__, f->type, f->fmt.pix_mp.num_planes,
1551 		f->fmt.pix_mp.pixelformat);
1552 
1553 	fmt = aml_vdec_find_format(f);
1554 	if (!fmt)
1555 		return -EINVAL;
1556 
1557 	return vidioc_try_fmt(f, fmt);
1558 }
1559 
vidioc_try_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_format * f)1560 static int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
1561 				struct v4l2_format *f)
1562 {
1563 	struct v4l2_pix_format_mplane *pix_fmt_mp = &f->fmt.pix_mp;
1564 	struct aml_video_fmt *fmt = NULL;
1565 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1566 
1567 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1568 		"%s, type: %u, planes: %u, fmt: %u\n",
1569 		__func__, f->type, f->fmt.pix_mp.num_planes,
1570 		f->fmt.pix_mp.pixelformat);
1571 
1572 	fmt = aml_vdec_find_format(f);
1573 	if (!fmt)
1574 		return -EINVAL;
1575 
1576 	if (pix_fmt_mp->plane_fmt[0].sizeimage == 0) {
1577 		v4l_dbg(0, V4L_DEBUG_CODEC_ERROR,
1578 			"sizeimage of output format must be given\n");
1579 		return -EINVAL;
1580 	}
1581 
1582 	return vidioc_try_fmt(f, fmt);
1583 }
1584 
vidioc_vdec_g_selection(struct file * file,void * priv,struct v4l2_selection * s)1585 static int vidioc_vdec_g_selection(struct file *file, void *priv,
1586 	struct v4l2_selection *s)
1587 {
1588 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1589 	struct aml_q_data *q_data;
1590 
1591 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1592 		return -EINVAL;
1593 
1594 	q_data = &ctx->q_data[AML_Q_DATA_DST];
1595 
1596 	switch (s->target) {
1597 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1598 		s->r.left = 0;
1599 		s->r.top = 0;
1600 		s->r.width = ctx->picinfo.visible_width;
1601 		s->r.height = ctx->picinfo.visible_height;
1602 		break;
1603 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1604 		s->r.left = 0;
1605 		s->r.top = 0;
1606 		s->r.width = ctx->picinfo.coded_width;
1607 		s->r.height = ctx->picinfo.coded_height;
1608 		break;
1609 	case V4L2_SEL_TGT_COMPOSE:
1610 		if (vdec_if_get_param(ctx, GET_PARAM_CROP_INFO, &(s->r))) {
1611 			/* set to default value if header info not ready yet*/
1612 			s->r.left = 0;
1613 			s->r.top = 0;
1614 			s->r.width = q_data->visible_width;
1615 			s->r.height = q_data->visible_height;
1616 		}
1617 		break;
1618 	default:
1619 		return -EINVAL;
1620 	}
1621 
1622 	if (ctx->state < AML_STATE_PROBE) {
1623 		/* set to default value if header info not ready yet*/
1624 		s->r.left = 0;
1625 		s->r.top = 0;
1626 		s->r.width = q_data->visible_width;
1627 		s->r.height = q_data->visible_height;
1628 	}
1629 
1630 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, type: %d\n",
1631 		__func__, s->type);
1632 
1633 	return 0;
1634 }
1635 
vidioc_vdec_s_selection(struct file * file,void * priv,struct v4l2_selection * s)1636 static int vidioc_vdec_s_selection(struct file *file, void *priv,
1637 	struct v4l2_selection *s)
1638 {
1639 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1640 
1641 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, type: %d\n",
1642 		__func__, s->type);
1643 
1644 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1645 		return -EINVAL;
1646 
1647 	switch (s->target) {
1648 	case V4L2_SEL_TGT_COMPOSE:
1649 		s->r.left = 0;
1650 		s->r.top = 0;
1651 		s->r.width = ctx->picinfo.visible_width;
1652 		s->r.height = ctx->picinfo.visible_height;
1653 		break;
1654 	default:
1655 		return -EINVAL;
1656 	}
1657 
1658 	return 0;
1659 }
1660 
copy_v4l2_format_dimention(struct v4l2_pix_format_mplane * pix_mp,struct aml_q_data * q_data,u32 type)1661 static void copy_v4l2_format_dimention(struct v4l2_pix_format_mplane *pix_mp,
1662 		struct aml_q_data *q_data, u32 type)
1663 {
1664 	if (!pix_mp || !q_data)
1665 		return;
1666 
1667 	if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1668 		pix_mp->width = q_data->visible_width;
1669 		pix_mp->height = q_data->visible_height;
1670 	} else {
1671 		/*
1672 		 * Width and height are set to the dimensions
1673 		 * of the movie, the buffer is bigger and
1674 		 * further processing stages should crop to this
1675 		 * rectangle.
1676 		 */
1677 		pix_mp->width = q_data->coded_width;
1678 		pix_mp->height = q_data->coded_height;
1679 	}
1680 
1681 	/*
1682 	 * Set pixelformat to the format in which mt vcodec
1683 	 * outputs the decoded frame
1684 	 */
1685 	pix_mp->num_planes = q_data->fmt->num_planes;
1686 	pix_mp->pixelformat = q_data->fmt->fourcc;
1687 	pix_mp->plane_fmt[0].bytesperline = q_data->bytesperline[0];
1688 	pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage[0];
1689 	if (type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1690 		pix_mp->plane_fmt[1].bytesperline = q_data->bytesperline[1];
1691 		pix_mp->plane_fmt[1].sizeimage = q_data->sizeimage[1];
1692 	}
1693 }
1694 
vidioc_vdec_s_fmt(struct file * file,void * priv,struct v4l2_format * f)1695 static int vidioc_vdec_s_fmt(struct file *file, void *priv,
1696 	struct v4l2_format *f)
1697 {
1698 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1699 	struct v4l2_pix_format_mplane *pix_mp;
1700 	struct aml_q_data *q_data;
1701 	int ret = 0;
1702 	struct aml_video_fmt *fmt;
1703 
1704 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1705 		"%s, type: %u, planes: %u, fmt: %u\n",
1706 		__func__, f->type, f->fmt.pix_mp.num_planes,
1707 		f->fmt.pix_mp.pixelformat);
1708 
1709 	q_data = aml_vdec_get_q_data(ctx, f->type);
1710 	if (!q_data)
1711 		return -EINVAL;
1712 
1713 	pix_mp = &f->fmt.pix_mp;
1714 	if ((f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) &&
1715 	    vb2_is_busy(&ctx->m2m_ctx->out_q_ctx.q)) {
1716 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1717 			"out_q_ctx buffers already requested\n");
1718 	}
1719 
1720 	if ((!V4L2_TYPE_IS_OUTPUT(f->type)) &&
1721 	    vb2_is_busy(&ctx->m2m_ctx->cap_q_ctx.q)) {
1722 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1723 			"cap_q_ctx buffers already requested\n");
1724 	}
1725 
1726 	fmt = aml_vdec_find_format(f);
1727 	if (fmt == NULL) {
1728 		if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1729 			f->fmt.pix.pixelformat =
1730 				aml_video_formats[OUT_FMT_IDX].fourcc;
1731 			fmt = aml_vdec_find_format(f);
1732 		} else if (!V4L2_TYPE_IS_OUTPUT(f->type)) {
1733 			f->fmt.pix.pixelformat =
1734 				aml_video_formats[CAP_FMT_IDX].fourcc;
1735 			fmt = aml_vdec_find_format(f);
1736 		}
1737 	}
1738 
1739 	q_data->fmt = fmt;
1740 	vidioc_try_fmt(f, q_data->fmt);
1741 	if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1742 		if (ctx->is_drm_mode)
1743 			pix_mp->plane_fmt[0].sizeimage = 1;
1744 		q_data->sizeimage[0] = pix_mp->plane_fmt[0].sizeimage;
1745 		q_data->coded_width = pix_mp->width;
1746 		q_data->coded_height = pix_mp->height;
1747 
1748 		v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO,
1749 			"w: %d, h: %d, size: %d\n",
1750 			pix_mp->width, pix_mp->height,
1751 			pix_mp->plane_fmt[0].sizeimage);
1752 
1753 		ctx->colorspace = f->fmt.pix_mp.colorspace;
1754 		ctx->ycbcr_enc = f->fmt.pix_mp.ycbcr_enc;
1755 		ctx->quantization = f->fmt.pix_mp.quantization;
1756 		ctx->xfer_func = f->fmt.pix_mp.xfer_func;
1757 
1758 		mutex_lock(&ctx->state_lock);
1759 		if (ctx->state == AML_STATE_IDLE) {
1760 			ret = vdec_if_init(ctx, q_data->fmt->fourcc);
1761 			if (ret) {
1762 				v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1763 					"vdec_if_init() fail ret=%d\n", ret);
1764 				mutex_unlock(&ctx->state_lock);
1765 				return -EINVAL;
1766 			}
1767 			ctx->state = AML_STATE_INIT;
1768 			ATRACE_COUNTER("v4l2_state", ctx->state);
1769 			v4l_dbg(ctx, V4L_DEBUG_CODEC_STATE,
1770 				"vcodec state (AML_STATE_INIT)\n");
1771 		}
1772 		mutex_unlock(&ctx->state_lock);
1773 	}
1774 
1775 	if (!V4L2_TYPE_IS_OUTPUT(f->type)) {
1776 		ctx->cap_pix_fmt = pix_mp->pixelformat;
1777 		if (ctx->state >= AML_STATE_PROBE)
1778 			copy_v4l2_format_dimention(pix_mp, q_data, f->type);
1779 	}
1780 
1781 	return 0;
1782 }
1783 
vidioc_enum_framesizes(struct file * file,void * priv,struct v4l2_frmsizeenum * fsize)1784 static int vidioc_enum_framesizes(struct file *file, void *priv,
1785 				struct v4l2_frmsizeenum *fsize)
1786 {
1787 	int i = 0;
1788 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1789 
1790 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, idx: %d, pix fmt: %x\n",
1791 		__func__, fsize->index, fsize->pixel_format);
1792 
1793 	if (fsize->index != 0)
1794 		return -EINVAL;
1795 
1796 	for (i = 0; i < NUM_SUPPORTED_FRAMESIZE; ++i) {
1797 		if (fsize->pixel_format != aml_vdec_framesizes[i].fourcc)
1798 			continue;
1799 
1800 		fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1801 		fsize->stepwise = aml_vdec_framesizes[i].stepwise;
1802 		if (!(ctx->dev->dec_capability &
1803 				VCODEC_CAPABILITY_4K_DISABLED)) {
1804 			v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO, "4K is enabled\n");
1805 			fsize->stepwise.max_width =
1806 					VCODEC_DEC_4K_CODED_WIDTH;
1807 			fsize->stepwise.max_height =
1808 					VCODEC_DEC_4K_CODED_HEIGHT;
1809 		}
1810 		v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO,
1811 			"%x, %d %d %d %d %d %d\n",
1812 			ctx->dev->dec_capability,
1813 			fsize->stepwise.min_width,
1814 			fsize->stepwise.max_width,
1815 			fsize->stepwise.step_width,
1816 			fsize->stepwise.min_height,
1817 			fsize->stepwise.max_height,
1818 			fsize->stepwise.step_height);
1819 		return 0;
1820 	}
1821 
1822 	return -EINVAL;
1823 }
1824 
vidioc_enum_fmt(struct v4l2_fmtdesc * f,bool output_queue)1825 static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool output_queue)
1826 {
1827 	struct aml_video_fmt *fmt;
1828 	int i, j = 0;
1829 
1830 	for (i = 0; i < NUM_FORMATS; i++) {
1831 		if (output_queue && (aml_video_formats[i].type != AML_FMT_DEC))
1832 			continue;
1833 		if (!output_queue && (aml_video_formats[i].type != AML_FMT_FRAME))
1834 			continue;
1835 
1836 		if (j == f->index) {
1837 			fmt = &aml_video_formats[i];
1838 			f->pixelformat = fmt->fourcc;
1839 			return 0;
1840 		}
1841 		++j;
1842 	}
1843 
1844 	return -EINVAL;
1845 }
1846 
vidioc_vdec_enum_fmt_vid_cap_mplane(struct file * file,void * priv,struct v4l2_fmtdesc * f)1847 static int vidioc_vdec_enum_fmt_vid_cap_mplane(struct file *file,
1848 	void *priv, struct v4l2_fmtdesc *f)
1849 {
1850 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1851 
1852 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s\n", __func__);
1853 
1854 	return vidioc_enum_fmt(f, false);
1855 }
1856 
vidioc_vdec_enum_fmt_vid_out_mplane(struct file * file,void * priv,struct v4l2_fmtdesc * f)1857 static int vidioc_vdec_enum_fmt_vid_out_mplane(struct file *file,
1858 	void *priv, struct v4l2_fmtdesc *f)
1859 {
1860 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1861 
1862 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s\n", __func__);
1863 
1864 	return vidioc_enum_fmt(f, true);
1865 }
1866 
vidioc_vdec_g_fmt(struct file * file,void * priv,struct v4l2_format * f)1867 static int vidioc_vdec_g_fmt(struct file *file, void *priv,
1868 	struct v4l2_format *f)
1869 {
1870 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1871 	struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
1872 	struct vb2_queue *vq;
1873 	struct aml_q_data *q_data;
1874 
1875 	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
1876 	if (!vq) {
1877 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1878 			"no vb2 queue for type=%d\n", f->type);
1879 		return -EINVAL;
1880 	}
1881 
1882 	q_data = aml_vdec_get_q_data(ctx, f->type);
1883 
1884 	pix_mp->field = V4L2_FIELD_NONE;
1885 	pix_mp->colorspace = ctx->colorspace;
1886 	pix_mp->ycbcr_enc = ctx->ycbcr_enc;
1887 	pix_mp->quantization = ctx->quantization;
1888 	pix_mp->xfer_func = ctx->xfer_func;
1889 
1890 	if ((!V4L2_TYPE_IS_OUTPUT(f->type)) &&
1891 	    (ctx->state >= AML_STATE_PROBE)) {
1892 		/* Until STREAMOFF is called on the CAPTURE queue
1893 		 * (acknowledging the event), the driver operates as if
1894 		 * the resolution hasn't changed yet.
1895 		 * So we just return picinfo yet, and update picinfo in
1896 		 * stop_streaming hook function
1897 		 */
1898 		/* it is used for alloc the decode buffer size. */
1899 		q_data->sizeimage[0] = ctx->picinfo.y_len_sz;
1900 		q_data->sizeimage[1] = ctx->picinfo.c_len_sz;
1901 
1902 		/* it is used for alloc the EGL image buffer size. */
1903 		q_data->coded_width = ctx->picinfo.coded_width;
1904 		q_data->coded_height = ctx->picinfo.coded_height;
1905 
1906 		q_data->bytesperline[0] = ctx->picinfo.coded_width;
1907 		q_data->bytesperline[1] = ctx->picinfo.coded_width;
1908 
1909 		copy_v4l2_format_dimention(pix_mp, q_data, f->type);
1910 	} else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1911 		/*
1912 		 * This is run on OUTPUT
1913 		 * The buffer contains compressed image
1914 		 * so width and height have no meaning.
1915 		 * Assign value here to pass v4l2-compliance test
1916 		 */
1917 		copy_v4l2_format_dimention(pix_mp, q_data, f->type);
1918 	} else {
1919 		copy_v4l2_format_dimention(pix_mp, q_data, f->type);
1920 
1921 		v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO,
1922 			"type=%d state=%d Format information could not be read, not ready yet!\n",
1923 			f->type, ctx->state);
1924 		return -EINVAL;
1925 	}
1926 
1927 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1928 		"%s, type: %u, planes: %u, fmt: %u\n",
1929 		__func__, f->type, f->fmt.pix_mp.num_planes,
1930 		f->fmt.pix_mp.pixelformat);
1931 
1932 	return 0;
1933 }
1934 
vidioc_vdec_create_bufs(struct file * file,void * priv,struct v4l2_create_buffers * create)1935 static int vidioc_vdec_create_bufs(struct file *file, void *priv,
1936 	struct v4l2_create_buffers *create)
1937 {
1938 	struct aml_vcodec_ctx *ctx = fh_to_ctx(priv);
1939 
1940 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1941 		"%s, type: %u, count: %u\n",
1942 		__func__, create->format.type, create->count);
1943 
1944 	return v4l2_m2m_ioctl_create_bufs(file, priv, create);
1945 }
1946 
1947 /*int vidioc_vdec_g_ctrl(struct file *file, void *fh,
1948 	struct v4l2_control *a)
1949 {
1950 	struct aml_vcodec_ctx *ctx = fh_to_ctx(fh);
1951 
1952 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
1953 		"%s, id: %d\n", __func__, a->id);
1954 
1955 	if (a->id == V4L2_CID_MIN_BUFFERS_FOR_CAPTURE)
1956 		a->value = 4;
1957 	else if (a->id == V4L2_CID_MIN_BUFFERS_FOR_OUTPUT)
1958 		a->value = 8;
1959 
1960 	return 0;
1961 }*/
1962 
vb2ops_vdec_queue_setup(struct vb2_queue * vq,unsigned int * nbuffers,unsigned int * nplanes,unsigned int sizes[],struct device * alloc_devs[])1963 static int vb2ops_vdec_queue_setup(struct vb2_queue *vq,
1964 				unsigned int *nbuffers,
1965 				unsigned int *nplanes,
1966 				unsigned int sizes[], struct device *alloc_devs[])
1967 {
1968 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vq);
1969 	struct aml_q_data *q_data;
1970 	unsigned int i;
1971 
1972 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, type: %d\n",
1973 		__func__, vq->type);
1974 
1975 	q_data = aml_vdec_get_q_data(ctx, vq->type);
1976 	if (q_data == NULL) {
1977 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
1978 			"vq->type=%d err\n", vq->type);
1979 		return -EINVAL;
1980 	}
1981 
1982 	if (*nplanes) {
1983 		for (i = 0; i < *nplanes; i++) {
1984 			if (sizes[i] < q_data->sizeimage[i])
1985 				return -EINVAL;
1986 			//alloc_devs[i] = &ctx->dev->plat_dev->dev;
1987 			alloc_devs[i] = v4l_get_dev_from_codec_mm();//alloc mm from the codec mm
1988 		}
1989 	} else {
1990 		if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
1991 			*nplanes = 2;
1992 		else
1993 			*nplanes = 1;
1994 
1995 		for (i = 0; i < *nplanes; i++) {
1996 			sizes[i] = q_data->sizeimage[i];
1997 			if (V4L2_TYPE_IS_OUTPUT(vq->type) && ctx->output_dma_mode)
1998 				sizes[i] = 0;
1999 			//alloc_devs[i] = &ctx->dev->plat_dev->dev;
2000 			alloc_devs[i] = v4l_get_dev_from_codec_mm();//alloc mm from the codec mm
2001 		}
2002 	}
2003 
2004 	v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2005 		"type: %d, plane: %d, buf cnt: %d, size: [Y: %u, C: %u]\n",
2006 		vq->type, *nplanes, *nbuffers, sizes[0], sizes[1]);
2007 
2008 	return 0;
2009 }
2010 
vb2ops_vdec_buf_prepare(struct vb2_buffer * vb)2011 static int vb2ops_vdec_buf_prepare(struct vb2_buffer *vb)
2012 {
2013 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2014 	struct aml_q_data *q_data;
2015 	int i;
2016 
2017 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
2018 		"%s, type: %d, idx: %d\n",
2019 		__func__, vb->vb2_queue->type, vb->index);
2020 
2021 	if (vb->memory == VB2_MEMORY_DMABUF
2022 		&& V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type))
2023 		return 0;
2024 
2025 	q_data = aml_vdec_get_q_data(ctx, vb->vb2_queue->type);
2026 
2027 	for (i = 0; i < q_data->fmt->num_planes; i++) {
2028 		if (vb2_plane_size(vb, i) < q_data->sizeimage[i]) {
2029 			v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2030 				"data will not fit into plane %d (%lu < %d)\n",
2031 				i, vb2_plane_size(vb, i),
2032 				q_data->sizeimage[i]);
2033 		}
2034 	}
2035 
2036 	return 0;
2037 }
2038 
vb2ops_vdec_buf_queue(struct vb2_buffer * vb)2039 static void vb2ops_vdec_buf_queue(struct vb2_buffer *vb)
2040 {
2041 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2042 	struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
2043 	struct aml_video_dec_buf *buf = NULL;
2044 	struct aml_vcodec_mem src_mem;
2045 	unsigned int dpb = 0;
2046 
2047 	vb2_v4l2 = to_vb2_v4l2_buffer(vb);
2048 	buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb);
2049 
2050 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
2051 		"%s, vb: %lx, type: %d, idx: %d, state: %d, used: %d\n",
2052 		__func__, (ulong) vb, vb->vb2_queue->type,
2053 		vb->index, vb->state, buf->used);
2054 	/*
2055 	 * check if this buffer is ready to be used after decode
2056 	 */
2057 	if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2058 		if (vb->index >= ctx->dpb_size) {
2059 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2060 				"enque capture buf idx %d/%d is invalid.\n",
2061 				vb->index, ctx->dpb_size);
2062 			return;
2063 		}
2064 
2065 		v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO,
2066 			"y_addr: %lx, vf_h: %lx, state: %d",
2067 			buf->frame_buffer.m.mem[0].addr,
2068 			buf->frame_buffer.vf_handle,
2069 			buf->frame_buffer.status);
2070 
2071 		if (!buf->que_in_m2m) {
2072 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2073 				"enque capture buf idx %d, vf: %lx\n",
2074 				vb->index, (ulong) v4l_get_vf_handle(vb2_v4l2->private));
2075 
2076 			v4l2_m2m_buf_queue(ctx->m2m_ctx, vb2_v4l2);
2077 			buf->que_in_m2m = true;
2078 			buf->queued_in_vb2 = true;
2079 			buf->queued_in_v4l2 = true;
2080 			buf->ready_to_display = false;
2081 			ctx->cap_pool.seq[ctx->cap_pool.in++] =
2082 				(V4L_CAP_BUFF_IN_M2M << 16 | vb->index);
2083 
2084 			/* check dpb ready */
2085 			aml_check_dpb_ready(ctx);
2086 		} else if (buf->frame_buffer.status == FB_ST_DISPLAY) {
2087 			buf->queued_in_vb2 = false;
2088 			buf->queued_in_v4l2 = true;
2089 			buf->ready_to_display = false;
2090 
2091 			/* recycle vf */
2092 			video_vf_put(ctx->ada_ctx->recv_name,
2093 				&buf->frame_buffer, ctx->id);
2094 		}
2095 		return;
2096 	}
2097 
2098 	v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
2099 
2100 	if (ctx->state != AML_STATE_INIT) {
2101 		return;
2102 	}
2103 
2104 	vb2_v4l2 = to_vb2_v4l2_buffer(vb);
2105 	buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb);
2106 	if (buf->lastframe) {
2107 		/* This shouldn't happen. Just in case. */
2108 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2109 			"Invalid flush buffer.\n");
2110 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2111 		return;
2112 	}
2113 
2114 	src_mem.index	= vb->index;
2115 	src_mem.vaddr	= vb2_plane_vaddr(vb, 0);
2116 	src_mem.addr	= vb2_dma_contig_plane_dma_addr(vb, 0);
2117 	src_mem.size	= vb->planes[0].bytesused;
2118 	src_mem.model	= vb->memory;
2119 
2120 	if (vdec_if_probe(ctx, &src_mem, NULL)) {
2121 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2122 
2123 		if (ctx->is_drm_mode && src_mem.model == VB2_MEMORY_DMABUF)
2124 			aml_recycle_dma_buffers(ctx);
2125 		else
2126 			v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_DONE);
2127 		return;
2128 	}
2129 
2130 	/*
2131 	 * If on model dmabuf must remove the buffer
2132 	 * because this data has been consumed by hw.
2133 	 */
2134 	if (ctx->is_drm_mode && src_mem.model == VB2_MEMORY_DMABUF) {
2135 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2136 		aml_recycle_dma_buffers(ctx);
2137 	} else if (ctx->param_sets_from_ucode) {
2138 		v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
2139 		v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb),
2140 			VB2_BUF_STATE_DONE);
2141 	}
2142 
2143 	if (vdec_if_get_param(ctx, GET_PARAM_PIC_INFO, &ctx->picinfo)) {
2144 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2145 			"GET_PARAM_PICTURE_INFO err\n");
2146 		return;
2147 	}
2148 
2149 	if (vdec_if_get_param(ctx, GET_PARAM_DPB_SIZE, &dpb)) {
2150 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2151 			"GET_PARAM_DPB_SIZE err\n");
2152 		return;
2153 	}
2154 
2155 	if (!dpb)
2156 		return;
2157 
2158 	ctx->dpb_size = dpb;
2159 	ctx->last_decoded_picinfo = ctx->picinfo;
2160 	aml_vdec_dispatch_event(ctx, V4L2_EVENT_SRC_CH_RESOLUTION);
2161 
2162 	mutex_lock(&ctx->state_lock);
2163 	if (ctx->state == AML_STATE_INIT) {
2164 		ctx->state = AML_STATE_PROBE;
2165 		ATRACE_COUNTER("v4l2_state", ctx->state);
2166 		v4l_dbg(ctx, V4L_DEBUG_CODEC_STATE,
2167 			"vcodec state (AML_STATE_PROBE)\n");
2168 	}
2169 	mutex_unlock(&ctx->state_lock);
2170 }
2171 
vb2ops_vdec_buf_finish(struct vb2_buffer * vb)2172 static void vb2ops_vdec_buf_finish(struct vb2_buffer *vb)
2173 {
2174 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2175 	struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
2176 	struct aml_video_dec_buf *buf = NULL;
2177 	bool buf_error;
2178 
2179 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
2180 		"%s, type: %d, idx: %d\n",
2181 		__func__, vb->vb2_queue->type, vb->index);
2182 
2183 	vb2_v4l2 = container_of(vb, struct vb2_v4l2_buffer, vb2_buf);
2184 	buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb);
2185 
2186 	if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2187 		buf->queued_in_v4l2 = false;
2188 		buf->queued_in_vb2 = false;
2189 	}
2190 	buf_error = buf->error;
2191 
2192 	if (buf_error) {
2193 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2194 			"Unrecoverable error on buffer.\n");
2195 		ctx->state = AML_STATE_ABORT;
2196 		ATRACE_COUNTER("v4l2_state", ctx->state);
2197 		v4l_dbg(ctx, V4L_DEBUG_CODEC_STATE,
2198 			"vcodec state (AML_STATE_ABORT)\n");
2199 	}
2200 }
2201 
vb2ops_vdec_buf_init(struct vb2_buffer * vb)2202 static int vb2ops_vdec_buf_init(struct vb2_buffer *vb)
2203 {
2204 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2205 	struct vb2_v4l2_buffer *vb2_v4l2 = container_of(vb,
2206 					struct vb2_v4l2_buffer, vb2_buf);
2207 	struct aml_video_dec_buf *buf = container_of(vb2_v4l2,
2208 					struct aml_video_dec_buf, vb);
2209 	unsigned int size, phy_addr = 0;
2210 	char *owner = __getname();
2211 
2212 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s, type: %d, idx: %d\n",
2213 		__func__, vb->vb2_queue->type, vb->index);
2214 
2215 	if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
2216 		buf->used = false;
2217 		buf->ready_to_display = false;
2218 		buf->queued_in_v4l2 = false;
2219 		buf->frame_buffer.status = FB_ST_NORMAL;
2220 	} else {
2221 		buf->lastframe = false;
2222 	}
2223 
2224 	/* codec_mm buffers count */
2225 	if (V4L2_TYPE_IS_OUTPUT(vb->type)) {
2226 		if (vb->memory == VB2_MEMORY_MMAP) {
2227 			size = vb->planes[0].length;
2228 			phy_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
2229 			snprintf(owner, PATH_MAX, "%s-%d", "v4l-input", ctx->id);
2230 			strncpy(buf->mem_onwer, owner, sizeof(buf->mem_onwer));
2231 			buf->mem_onwer[sizeof(buf->mem_onwer) - 1] = '\0';
2232 
2233 			buf->mem[0] = v4l_reqbufs_from_codec_mm(buf->mem_onwer,
2234 					phy_addr, size, vb->index);
2235 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2236 				"IN alloc, addr: %x, size: %u, idx: %u\n",
2237 				phy_addr, size, vb->index);
2238 		}
2239 	} else {
2240 		snprintf(owner, PATH_MAX, "%s-%d", "v4l-output", ctx->id);
2241 		strncpy(buf->mem_onwer, owner, sizeof(buf->mem_onwer));
2242 		buf->mem_onwer[sizeof(buf->mem_onwer) - 1] = '\0';
2243 
2244 		if ((vb->memory == VB2_MEMORY_MMAP) && (vb->num_planes == 1)) {
2245 			size = vb->planes[0].length;
2246 			phy_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
2247 			buf->mem[0] = v4l_reqbufs_from_codec_mm(buf->mem_onwer,
2248 				phy_addr, size, vb->index);
2249 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2250 				"OUT Y alloc, addr: %x, size: %u, idx: %u\n",
2251 				phy_addr, size, vb->index);
2252 		} else if ((vb->memory == VB2_MEMORY_MMAP) && (vb->num_planes == 2)) {
2253 			size = vb->planes[0].length;
2254 			phy_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
2255 			buf->mem[0] = v4l_reqbufs_from_codec_mm(buf->mem_onwer,
2256 				phy_addr, size, vb->index);
2257 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2258 				"OUT Y alloc, addr: %x, size: %u, idx: %u\n",
2259 				phy_addr, size, vb->index);
2260 
2261 			size = vb->planes[1].length;
2262 			phy_addr = vb2_dma_contig_plane_dma_addr(vb, 1);
2263 			buf->mem[1] = v4l_reqbufs_from_codec_mm(buf->mem_onwer,
2264 					phy_addr, size, vb->index);
2265 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2266 				"OUT C alloc, addr: %x, size: %u, idx: %u\n",
2267 				phy_addr, size, vb->index);
2268 		}
2269 	}
2270 
2271 	__putname(owner);
2272 
2273 	return 0;
2274 }
2275 
codec_mm_bufs_cnt_clean(struct vb2_queue * q)2276 static void codec_mm_bufs_cnt_clean(struct vb2_queue *q)
2277 {
2278 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(q);
2279 	struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
2280 	struct aml_video_dec_buf *buf = NULL;
2281 	int i;
2282 
2283 	for (i = 0; i < q->num_buffers; ++i) {
2284 		vb2_v4l2 = to_vb2_v4l2_buffer(q->bufs[i]);
2285 		buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb);
2286 		if (IS_ERR_OR_NULL(buf->mem[0]))
2287 			return;
2288 
2289 		if (V4L2_TYPE_IS_OUTPUT(q->bufs[i]->type)) {
2290 			v4l_freebufs_back_to_codec_mm(buf->mem_onwer, buf->mem[0]);
2291 
2292 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2293 				"IN clean, addr: %lx, size: %u, idx: %u\n",
2294 				buf->mem[0]->phy_addr, buf->mem[0]->buffer_size, i);
2295 			buf->mem[0] = NULL;
2296 			continue;
2297 		}
2298 
2299 		if (q->memory == VB2_MEMORY_MMAP) {
2300 			v4l_freebufs_back_to_codec_mm(buf->mem_onwer, buf->mem[0]);
2301 			v4l_freebufs_back_to_codec_mm(buf->mem_onwer, buf->mem[1]);
2302 
2303 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2304 				"OUT Y clean, addr: %lx, size: %u, idx: %u\n",
2305 				buf->mem[0]->phy_addr, buf->mem[0]->buffer_size, i);
2306 			v4l_dbg(ctx, V4L_DEBUG_CODEC_BUFMGR,
2307 				"OUT C clean, addr: %lx, size: %u, idx: %u\n",
2308 				buf->mem[1]->phy_addr, buf->mem[1]->buffer_size, i);
2309 			buf->mem[0] = NULL;
2310 			buf->mem[1] = NULL;
2311 		}
2312 	}
2313 }
2314 
vb2ops_vdec_start_streaming(struct vb2_queue * q,unsigned int count)2315 static int vb2ops_vdec_start_streaming(struct vb2_queue *q, unsigned int count)
2316 {
2317 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(q);
2318 
2319 	ctx->has_receive_eos = false;
2320 	v4l2_m2m_set_dst_buffered(ctx->fh.m2m_ctx, true);
2321 
2322 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
2323 		"%s, type: %d\n", __func__, q->type);
2324 
2325 	return 0;
2326 }
2327 
vb2ops_vdec_stop_streaming(struct vb2_queue * q)2328 static void vb2ops_vdec_stop_streaming(struct vb2_queue *q)
2329 {
2330 	struct aml_video_dec_buf *buf = NULL;
2331 	struct vb2_v4l2_buffer *vb2_v4l2 = NULL;
2332 	struct aml_vcodec_ctx *ctx = vb2_get_drv_priv(q);
2333 	int i;
2334 
2335 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
2336 		"%s, type: %d, state: %x, frame_cnt: %d\n",
2337 		__func__, q->type, ctx->state, ctx->decoded_frame_cnt);
2338 
2339 	codec_mm_bufs_cnt_clean(q);
2340 
2341 	if (V4L2_TYPE_IS_OUTPUT(q->type)) {
2342 		if (ctx->is_drm_mode && q->memory == VB2_MEMORY_DMABUF)
2343 			aml_recycle_dma_buffers(ctx);
2344 
2345 		while ((vb2_v4l2 = v4l2_m2m_src_buf_remove(ctx->m2m_ctx)))
2346 			v4l2_m2m_buf_done(vb2_v4l2, VB2_BUF_STATE_ERROR);
2347 
2348 		for (i = 0; i < q->num_buffers; ++i) {
2349 			vb2_v4l2 = to_vb2_v4l2_buffer(q->bufs[i]);
2350 			if (vb2_v4l2->vb2_buf.state == VB2_BUF_STATE_ACTIVE)
2351 				v4l2_m2m_buf_done(vb2_v4l2, VB2_BUF_STATE_ERROR);
2352 		}
2353 	} else {
2354 		/* clean output cache and decoder status . */
2355 		if (ctx->state > AML_STATE_INIT)
2356 			aml_vdec_reset(ctx);
2357 
2358 		while ((vb2_v4l2 = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx)))
2359 			v4l2_m2m_buf_done(vb2_v4l2, VB2_BUF_STATE_ERROR);
2360 
2361 		for (i = 0; i < q->num_buffers; ++i) {
2362 			vb2_v4l2 = to_vb2_v4l2_buffer(q->bufs[i]);
2363 			buf = container_of(vb2_v4l2, struct aml_video_dec_buf, vb);
2364 			buf->frame_buffer.status = FB_ST_NORMAL;
2365 			buf->que_in_m2m = false;
2366 			ctx->cap_pool.seq[i] = 0;
2367 
2368 			if (vb2_v4l2->vb2_buf.state == VB2_BUF_STATE_ACTIVE)
2369 				v4l2_m2m_buf_done(vb2_v4l2, VB2_BUF_STATE_ERROR);
2370 
2371 			/*v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO, "idx: %d, state: %d\n",
2372 				q->bufs[i]->index, q->bufs[i]->state);*/
2373 		}
2374 	}
2375 
2376 	ctx->buf_used_count = 0;
2377 	ctx->cap_pool.in = 0;
2378 	ctx->cap_pool.out = 0;
2379 }
2380 
m2mops_vdec_device_run(void * priv)2381 static void m2mops_vdec_device_run(void *priv)
2382 {
2383 	struct aml_vcodec_ctx *ctx = priv;
2384 	struct aml_vcodec_dev *dev = ctx->dev;
2385 
2386 	if (ctx->output_thread_ready)
2387 		queue_work(dev->decode_workqueue, &ctx->decode_work);
2388 }
2389 
vdec_device_vf_run(struct aml_vcodec_ctx * ctx)2390 void vdec_device_vf_run(struct aml_vcodec_ctx *ctx)
2391 {
2392 	if (ctx->state < AML_STATE_INIT ||
2393 		ctx->state > AML_STATE_FLUSHED)
2394 		return;
2395 
2396 	aml_thread_notify(ctx, AML_THREAD_CAPTURE);
2397 }
2398 
m2mops_vdec_job_ready(void * m2m_priv)2399 static int m2mops_vdec_job_ready(void *m2m_priv)
2400 {
2401 	struct aml_vcodec_ctx *ctx = m2m_priv;
2402 
2403 	if (ctx->state < AML_STATE_PROBE ||
2404 		ctx->state > AML_STATE_FLUSHED)
2405 		return 0;
2406 
2407 	return 1;
2408 }
2409 
m2mops_vdec_job_abort(void * priv)2410 static void m2mops_vdec_job_abort(void *priv)
2411 {
2412 	struct aml_vcodec_ctx *ctx = priv;
2413 
2414 	v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO, "%s\n", __func__);
2415 }
2416 
aml_vdec_g_v_ctrl(struct v4l2_ctrl * ctrl)2417 static int aml_vdec_g_v_ctrl(struct v4l2_ctrl *ctrl)
2418 {
2419 	struct aml_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
2420 	int ret = 0;
2421 
2422 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT,
2423 		"%s, id: %d\n", __func__, ctrl->id);
2424 
2425 	switch (ctrl->id) {
2426 	case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
2427 		if (ctx->state >= AML_STATE_PROBE) {
2428 			ctrl->val = ctx->dpb_size;
2429 		} else {
2430 			v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2431 				"Seqinfo not ready.\n");
2432 			ctrl->val = 0;
2433 			ret = -EINVAL;
2434 		}
2435 		break;
2436 	case V4L2_CID_MIN_BUFFERS_FOR_OUTPUT:
2437 		ctrl->val = 4;
2438 		break;
2439 	default:
2440 		ret = -EINVAL;
2441 	}
2442 	return ret;
2443 }
2444 
aml_vdec_try_s_v_ctrl(struct v4l2_ctrl * ctrl)2445 static int aml_vdec_try_s_v_ctrl(struct v4l2_ctrl *ctrl)
2446 {
2447 	struct aml_vcodec_ctx *ctx = ctrl_to_ctx(ctrl);
2448 
2449 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s\n", __func__);
2450 
2451 	if (ctrl->id == AML_V4L2_SET_DRMMODE) {
2452 		ctx->is_drm_mode = ctrl->val;
2453 		ctx->param_sets_from_ucode = true;
2454 		v4l_dbg(ctx, V4L_DEBUG_CODEC_PRINFO,
2455 			"set stream mode: %x\n", ctrl->val);
2456 	}
2457 
2458 	return 0;
2459 }
2460 
2461 static const struct v4l2_ctrl_ops aml_vcodec_dec_ctrl_ops = {
2462 	.g_volatile_ctrl = aml_vdec_g_v_ctrl,
2463 	.try_ctrl = aml_vdec_try_s_v_ctrl,
2464 };
2465 
2466 static const struct v4l2_ctrl_config ctrl_st_mode = {
2467 	.name	= "drm mode",
2468 	.id	= AML_V4L2_SET_DRMMODE,
2469 	.ops	= &aml_vcodec_dec_ctrl_ops,
2470 	.type	= V4L2_CTRL_TYPE_BOOLEAN,
2471 	.flags	= V4L2_CTRL_FLAG_WRITE_ONLY,
2472 	.min	= 0,
2473 	.max	= 1,
2474 	.step	= 1,
2475 	.def	= 0,
2476 };
2477 
aml_vcodec_dec_ctrls_setup(struct aml_vcodec_ctx * ctx)2478 int aml_vcodec_dec_ctrls_setup(struct aml_vcodec_ctx *ctx)
2479 {
2480 	int ret;
2481 	struct v4l2_ctrl *ctrl;
2482 
2483 	v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 3);
2484 	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
2485 				&aml_vcodec_dec_ctrl_ops,
2486 				V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
2487 				0, 32, 1, 2);
2488 	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2489 	if (ctx->ctrl_hdl.error) {
2490 		ret = ctx->ctrl_hdl.error;
2491 		goto err;
2492 	}
2493 
2494 	ctrl = v4l2_ctrl_new_std(&ctx->ctrl_hdl,
2495 				&aml_vcodec_dec_ctrl_ops,
2496 				V4L2_CID_MIN_BUFFERS_FOR_OUTPUT,
2497 				0, 32, 1, 8);
2498 	ctrl->flags |= V4L2_CTRL_FLAG_VOLATILE;
2499 	if (ctx->ctrl_hdl.error) {
2500 		ret = ctx->ctrl_hdl.error;
2501 		goto err;
2502 	}
2503 
2504 	ctrl = v4l2_ctrl_new_custom(&ctx->ctrl_hdl, &ctrl_st_mode, NULL);
2505 	if (ctx->ctrl_hdl.error) {
2506 		ret = ctx->ctrl_hdl.error;
2507 		goto err;
2508 	}
2509 
2510 	v4l2_ctrl_handler_setup(&ctx->ctrl_hdl);
2511 
2512 	return 0;
2513 err:
2514 	v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2515 		"Adding control failed %d\n",
2516 		ctx->ctrl_hdl.error);
2517 	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
2518 	return ret;
2519 }
2520 
vidioc_vdec_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)2521 static int vidioc_vdec_g_parm(struct file *file, void *fh,
2522 	struct v4l2_streamparm *a)
2523 {
2524 	struct aml_vcodec_ctx *ctx = fh_to_ctx(fh);
2525 
2526 	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2527 		if (vdec_if_get_param(ctx, GET_PARAM_CONFIG_INFO,
2528 			&ctx->config.parm.dec)) {
2529 			v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2530 				"GET_PARAM_CONFIG_INFO err\n");
2531 			return -1;
2532 		}
2533 		memcpy(a->parm.raw_data, ctx->config.parm.data,
2534 			sizeof(a->parm.raw_data));
2535 	}
2536 
2537 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s\n", __func__);
2538 
2539 	return 0;
2540 }
2541 
vidioc_vdec_s_parm(struct file * file,void * fh,struct v4l2_streamparm * a)2542 static int vidioc_vdec_s_parm(struct file *file, void *fh,
2543 	struct v4l2_streamparm *a)
2544 {
2545 	struct aml_vcodec_ctx *ctx = fh_to_ctx(fh);
2546 
2547 	v4l_dbg(ctx, V4L_DEBUG_CODEC_PROT, "%s\n", __func__);
2548 
2549 	if (a->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
2550 		struct aml_dec_params *in =
2551 			(struct aml_dec_params *) a->parm.raw_data;
2552 		struct aml_dec_params *dec = &ctx->config.parm.dec;
2553 
2554 		ctx->config.type = V4L2_CONFIG_PARM_DECODE;
2555 
2556 		if (in->parms_status & V4L2_CONFIG_PARM_DECODE_CFGINFO)
2557 			dec->cfg = in->cfg;
2558 		if (in->parms_status & V4L2_CONFIG_PARM_DECODE_PSINFO)
2559 			dec->ps = in->ps;
2560 		if (in->parms_status & V4L2_CONFIG_PARM_DECODE_HDRINFO)
2561 			dec->hdr = in->hdr;
2562 		if (in->parms_status & V4L2_CONFIG_PARM_DECODE_CNTINFO)
2563 			dec->cnt = in->cnt;
2564 
2565 		dec->parms_status |= in->parms_status;
2566 	}
2567 
2568 	return 0;
2569 }
2570 
m2mops_vdec_lock(void * m2m_priv)2571 static void m2mops_vdec_lock(void *m2m_priv)
2572 {
2573 	struct aml_vcodec_ctx *ctx = m2m_priv;
2574 
2575 	mutex_lock(&ctx->dev->dev_mutex);
2576 }
2577 
m2mops_vdec_unlock(void * m2m_priv)2578 static void m2mops_vdec_unlock(void *m2m_priv)
2579 {
2580 	struct aml_vcodec_ctx *ctx = m2m_priv;
2581 
2582 	mutex_unlock(&ctx->dev->dev_mutex);
2583 }
2584 
2585 const struct v4l2_m2m_ops aml_vdec_m2m_ops = {
2586 	.device_run	= m2mops_vdec_device_run,
2587 	.job_ready	= m2mops_vdec_job_ready,
2588 	.job_abort	= m2mops_vdec_job_abort,
2589 	.lock		= m2mops_vdec_lock,
2590 	.unlock		= m2mops_vdec_unlock,
2591 };
2592 
2593 static const struct vb2_ops aml_vdec_vb2_ops = {
2594 	.queue_setup	= vb2ops_vdec_queue_setup,
2595 	.buf_prepare	= vb2ops_vdec_buf_prepare,
2596 	.buf_queue	= vb2ops_vdec_buf_queue,
2597 	.wait_prepare	= vb2_ops_wait_prepare,
2598 	.wait_finish	= vb2_ops_wait_finish,
2599 	.buf_init	= vb2ops_vdec_buf_init,
2600 	.buf_finish	= vb2ops_vdec_buf_finish,
2601 	.start_streaming = vb2ops_vdec_start_streaming,
2602 	.stop_streaming	= vb2ops_vdec_stop_streaming,
2603 };
2604 
2605 const struct v4l2_ioctl_ops aml_vdec_ioctl_ops = {
2606 	.vidioc_streamon		= vidioc_decoder_streamon,
2607 	.vidioc_streamoff		= vidioc_decoder_streamoff,
2608 	.vidioc_reqbufs			= vidioc_decoder_reqbufs,
2609 	.vidioc_querybuf		= vidioc_vdec_querybuf,
2610 	.vidioc_expbuf			= vidioc_vdec_expbuf,
2611 	//.vidioc_g_ctrl		= vidioc_vdec_g_ctrl,
2612 
2613 	.vidioc_qbuf			= vidioc_vdec_qbuf,
2614 	.vidioc_dqbuf			= vidioc_vdec_dqbuf,
2615 
2616 	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
2617 	.vidioc_try_fmt_vid_cap		= vidioc_try_fmt_vid_cap_mplane,
2618 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
2619 	.vidioc_try_fmt_vid_out		= vidioc_try_fmt_vid_out_mplane,
2620 
2621 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_vdec_s_fmt,
2622 	.vidioc_s_fmt_vid_cap		= vidioc_vdec_s_fmt,
2623 	.vidioc_s_fmt_vid_out_mplane	= vidioc_vdec_s_fmt,
2624 	.vidioc_s_fmt_vid_out		= vidioc_vdec_s_fmt,
2625 	.vidioc_g_fmt_vid_cap_mplane	= vidioc_vdec_g_fmt,
2626 	.vidioc_g_fmt_vid_cap		= vidioc_vdec_g_fmt,
2627 	.vidioc_g_fmt_vid_out_mplane	= vidioc_vdec_g_fmt,
2628 	.vidioc_g_fmt_vid_out		= vidioc_vdec_g_fmt,
2629 
2630 	.vidioc_create_bufs		= vidioc_vdec_create_bufs,
2631 
2632 	.vidioc_enum_fmt_vid_cap_mplane	= vidioc_vdec_enum_fmt_vid_cap_mplane,
2633 	.vidioc_enum_fmt_vid_cap	= vidioc_vdec_enum_fmt_vid_cap_mplane,
2634 	.vidioc_enum_fmt_vid_out_mplane	= vidioc_vdec_enum_fmt_vid_out_mplane,
2635 	.vidioc_enum_fmt_vid_out	= vidioc_vdec_enum_fmt_vid_out_mplane,
2636 	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
2637 
2638 	.vidioc_querycap		= vidioc_vdec_querycap,
2639 	.vidioc_subscribe_event		= vidioc_vdec_subscribe_evt,
2640 	.vidioc_unsubscribe_event	= vidioc_vdec_event_unsubscribe,
2641 	.vidioc_g_selection             = vidioc_vdec_g_selection,
2642 	.vidioc_s_selection             = vidioc_vdec_s_selection,
2643 
2644 	.vidioc_decoder_cmd		= vidioc_decoder_cmd,
2645 	.vidioc_try_decoder_cmd		= vidioc_try_decoder_cmd,
2646 
2647 	.vidioc_g_parm			= vidioc_vdec_g_parm,
2648 	.vidioc_s_parm			= vidioc_vdec_s_parm,
2649 };
2650 
aml_vcodec_dec_queue_init(void * priv,struct vb2_queue * src_vq,struct vb2_queue * dst_vq)2651 int aml_vcodec_dec_queue_init(void *priv, struct vb2_queue *src_vq,
2652 			   struct vb2_queue *dst_vq)
2653 {
2654 	struct aml_vcodec_ctx *ctx = priv;
2655 	int ret = 0;
2656 
2657 	v4l_dbg(ctx, V4L_DEBUG_CODEC_EXINFO, "%s\n", __func__);
2658 
2659 	src_vq->type		= V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
2660 	src_vq->io_modes	= VB2_DMABUF | VB2_MMAP;
2661 	src_vq->drv_priv	= ctx;
2662 	src_vq->buf_struct_size = sizeof(struct aml_video_dec_buf);
2663 	src_vq->ops		= &aml_vdec_vb2_ops;
2664 	src_vq->mem_ops		= &vb2_dma_contig_memops;
2665 	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2666 	src_vq->lock		= &ctx->dev->dev_mutex;
2667 	ret = vb2_queue_init(src_vq);
2668 	if (ret) {
2669 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2670 			"Failed to initialize videobuf2 queue(output)\n");
2671 		return ret;
2672 	}
2673 
2674 	dst_vq->type		= multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
2675 					V4L2_BUF_TYPE_VIDEO_CAPTURE;
2676 	dst_vq->io_modes	= VB2_DMABUF | VB2_MMAP | VB2_USERPTR;
2677 	dst_vq->drv_priv	= ctx;
2678 	dst_vq->buf_struct_size = sizeof(struct aml_video_dec_buf);
2679 	dst_vq->ops		= &aml_vdec_vb2_ops;
2680 	dst_vq->mem_ops		= &vb2_dma_contig_memops;
2681 	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
2682 	dst_vq->lock		= &ctx->dev->dev_mutex;
2683 	ret = vb2_queue_init(dst_vq);
2684 	if (ret) {
2685 		vb2_queue_release(src_vq);
2686 		v4l_dbg(ctx, V4L_DEBUG_CODEC_ERROR,
2687 			"Failed to initialize videobuf2 queue(capture)\n");
2688 	}
2689 
2690 	return ret;
2691 }
2692 
2693