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