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