blob: 7bf4d5e8dec272eeb2efda6609baa815722f9c58
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 <linux/module.h> |
21 | #include <linux/slab.h> |
22 | #include <linux/timer.h> |
23 | #include <linux/delay.h> |
24 | #include <linux/kernel.h> |
25 | #include <uapi/linux/swab.h> |
26 | |
27 | #include "../vdec_drv_if.h" |
28 | #include "../aml_vcodec_util.h" |
29 | #include "../aml_vcodec_dec.h" |
30 | #include "../aml_vcodec_drv.h" |
31 | #include "../aml_vcodec_adapt.h" |
32 | #include "../vdec_drv_base.h" |
33 | #include "../aml_vcodec_vfm.h" |
34 | #include "aml_h264_parser.h" |
35 | #include "../utils/common.h" |
36 | |
37 | /* h264 NALU type */ |
38 | #define NAL_NON_IDR_SLICE 0x01 |
39 | #define NAL_IDR_SLICE 0x05 |
40 | #define NAL_H264_SEI 0x06 |
41 | #define NAL_H264_SPS 0x07 |
42 | #define NAL_H264_PPS 0x08 |
43 | #define NAL_H264_AUD 0x09 |
44 | |
45 | #define AVC_NAL_TYPE(value) ((value) & 0x1F) |
46 | |
47 | #define BUF_PREDICTION_SZ (64 * 1024)//(32 * 1024) |
48 | |
49 | #define MB_UNIT_LEN 16 |
50 | |
51 | /* motion vector size (bytes) for every macro block */ |
52 | #define HW_MB_STORE_SZ 64 |
53 | |
54 | #define H264_MAX_FB_NUM 17 |
55 | #define HDR_PARSING_BUF_SZ 1024 |
56 | |
57 | #define HEADER_BUFFER_SIZE (128 * 1024) |
58 | |
59 | /** |
60 | * struct h264_fb - h264 decode frame buffer information |
61 | * @vdec_fb_va : virtual address of struct vdec_fb |
62 | * @y_fb_dma : dma address of Y frame buffer (luma) |
63 | * @c_fb_dma : dma address of C frame buffer (chroma) |
64 | * @poc : picture order count of frame buffer |
65 | * @reserved : for 8 bytes alignment |
66 | */ |
67 | struct h264_fb { |
68 | uint64_t vdec_fb_va; |
69 | uint64_t y_fb_dma; |
70 | uint64_t c_fb_dma; |
71 | int32_t poc; |
72 | uint32_t reserved; |
73 | }; |
74 | |
75 | /** |
76 | * struct h264_ring_fb_list - ring frame buffer list |
77 | * @fb_list : frame buffer arrary |
78 | * @read_idx : read index |
79 | * @write_idx : write index |
80 | * @count : buffer count in list |
81 | */ |
82 | struct h264_ring_fb_list { |
83 | struct h264_fb fb_list[H264_MAX_FB_NUM]; |
84 | unsigned int read_idx; |
85 | unsigned int write_idx; |
86 | unsigned int count; |
87 | unsigned int reserved; |
88 | }; |
89 | |
90 | /** |
91 | * struct vdec_h264_dec_info - decode information |
92 | * @dpb_sz : decoding picture buffer size |
93 | * @realloc_mv_buf : flag to notify driver to re-allocate mv buffer |
94 | * @reserved : for 8 bytes alignment |
95 | * @bs_dma : Input bit-stream buffer dma address |
96 | * @y_fb_dma : Y frame buffer dma address |
97 | * @c_fb_dma : C frame buffer dma address |
98 | * @vdec_fb_va : VDEC frame buffer struct virtual address |
99 | */ |
100 | struct vdec_h264_dec_info { |
101 | uint32_t dpb_sz; |
102 | uint32_t realloc_mv_buf; |
103 | uint32_t reserved; |
104 | uint64_t bs_dma; |
105 | uint64_t y_fb_dma; |
106 | uint64_t c_fb_dma; |
107 | uint64_t vdec_fb_va; |
108 | }; |
109 | |
110 | /** |
111 | * struct vdec_h264_vsi - shared memory for decode information exchange |
112 | * between VPU and Host. |
113 | * The memory is allocated by VPU then mapping to Host |
114 | * in vpu_dec_init() and freed in vpu_dec_deinit() |
115 | * by VPU. |
116 | * AP-W/R : AP is writer/reader on this item |
117 | * VPU-W/R: VPU is write/reader on this item |
118 | * @dec : decode information (AP-R, VPU-W) |
119 | * @pic : picture information (AP-R, VPU-W) |
120 | * @crop : crop information (AP-R, VPU-W) |
121 | */ |
122 | struct vdec_h264_vsi { |
123 | unsigned char hdr_buf[HDR_PARSING_BUF_SZ]; |
124 | char *header_buf; |
125 | int sps_size; |
126 | int pps_size; |
127 | int sei_size; |
128 | int head_offset; |
129 | struct vdec_h264_dec_info dec; |
130 | struct vdec_pic_info pic; |
131 | struct vdec_pic_info cur_pic; |
132 | struct v4l2_rect crop; |
133 | bool is_combine; |
134 | int nalu_pos; |
135 | }; |
136 | |
137 | /** |
138 | * struct vdec_h264_inst - h264 decoder instance |
139 | * @num_nalu : how many nalus be decoded |
140 | * @ctx : point to aml_vcodec_ctx |
141 | * @pred_buf : HW working predication buffer |
142 | * @mv_buf : HW working motion vector buffer |
143 | * @vpu : VPU instance |
144 | * @vsi : VPU shared information |
145 | */ |
146 | struct vdec_h264_inst { |
147 | unsigned int num_nalu; |
148 | struct aml_vcodec_ctx *ctx; |
149 | struct aml_vcodec_mem pred_buf; |
150 | struct aml_vcodec_mem mv_buf[H264_MAX_FB_NUM]; |
151 | struct aml_vdec_adapt vdec; |
152 | struct vdec_h264_vsi *vsi; |
153 | struct vcodec_vfm_s vfm; |
154 | struct aml_dec_params parms; |
155 | struct completion comp; |
156 | }; |
157 | |
158 | #if 0 |
159 | #define DUMP_FILE_NAME "/data/dump/dump.tmp" |
160 | static struct file *filp; |
161 | static loff_t file_pos; |
162 | |
163 | void dump_write(const char __user *buf, size_t count) |
164 | { |
165 | mm_segment_t old_fs; |
166 | |
167 | if (!filp) |
168 | return; |
169 | |
170 | old_fs = get_fs(); |
171 | set_fs(KERNEL_DS); |
172 | |
173 | if (count != vfs_write(filp, buf, count, &file_pos)) |
174 | pr_err("Failed to write file\n"); |
175 | |
176 | set_fs(old_fs); |
177 | } |
178 | |
179 | void dump_init(void) |
180 | { |
181 | filp = filp_open(DUMP_FILE_NAME, O_CREAT | O_RDWR, 0644); |
182 | if (IS_ERR(filp)) { |
183 | pr_err("open dump file failed\n"); |
184 | filp = NULL; |
185 | } |
186 | } |
187 | |
188 | void dump_deinit(void) |
189 | { |
190 | if (filp) { |
191 | filp_close(filp, current->files); |
192 | filp = NULL; |
193 | file_pos = 0; |
194 | } |
195 | } |
196 | |
197 | void swap_uv(void *uv, int size) |
198 | { |
199 | int i; |
200 | __u16 *p = uv; |
201 | |
202 | size /= 2; |
203 | |
204 | for (i = 0; i < size; i++, p++) |
205 | *p = __swab16(*p); |
206 | } |
207 | #endif |
208 | |
209 | static void get_pic_info(struct vdec_h264_inst *inst, |
210 | struct vdec_pic_info *pic) |
211 | { |
212 | *pic = inst->vsi->pic; |
213 | |
214 | aml_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)", |
215 | pic->visible_width, pic->visible_height, |
216 | pic->coded_width, pic->coded_height); |
217 | aml_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz, |
218 | pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz); |
219 | } |
220 | |
221 | static void get_crop_info(struct vdec_h264_inst *inst, struct v4l2_rect *cr) |
222 | { |
223 | cr->left = inst->vsi->crop.left; |
224 | cr->top = inst->vsi->crop.top; |
225 | cr->width = inst->vsi->crop.width; |
226 | cr->height = inst->vsi->crop.height; |
227 | |
228 | aml_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d", |
229 | cr->left, cr->top, cr->width, cr->height); |
230 | } |
231 | |
232 | static void get_dpb_size(struct vdec_h264_inst *inst, unsigned int *dpb_sz) |
233 | { |
234 | *dpb_sz = inst->vsi->dec.dpb_sz; |
235 | aml_vcodec_debug(inst, "sz=%d", *dpb_sz); |
236 | } |
237 | |
238 | static void skip_aud_data(u8 **data, u32 *size) |
239 | { |
240 | int i; |
241 | |
242 | i = find_start_code(*data, *size); |
243 | if (i > 0 && (*data)[i++] == 0x9 && (*data)[i++] == 0xf0) { |
244 | *size -= i; |
245 | *data += i; |
246 | } |
247 | } |
248 | |
249 | static u32 vdec_config_default_parms(u8 *parm) |
250 | { |
251 | u8 *pbuf = parm; |
252 | |
253 | pbuf += sprintf(pbuf, "parm_v4l_codec_enable:1;"); |
254 | pbuf += sprintf(pbuf, "mh264_double_write_mode:16;"); |
255 | pbuf += sprintf(pbuf, "parm_v4l_buffer_margin:7;"); |
256 | |
257 | return parm - pbuf; |
258 | } |
259 | |
260 | static void vdec_parser_parms(struct vdec_h264_inst *inst) |
261 | { |
262 | struct aml_vcodec_ctx *ctx = inst->ctx; |
263 | |
264 | if (!ctx->config.length) { |
265 | ctx->config.type = V4L2_CONFIG_PARM_DECODE; |
266 | ctx->config.parm.dec.double_write_mode = 16; |
267 | inst->parms = ctx->config.parm.dec; |
268 | |
269 | ctx->config.length = |
270 | vdec_config_default_parms(ctx->config.buf); |
271 | } else { |
272 | u8 *pbuf = ctx->config.buf; |
273 | |
274 | inst->parms = ctx->config.parm.dec; |
275 | pbuf += sprintf(pbuf, "parm_v4l_codec_enable:1;"); |
276 | pbuf += sprintf(pbuf, "mh264_double_write_mode:%d;", |
277 | inst->parms.double_write_mode); |
278 | pbuf += sprintf(pbuf, "parm_v4l_buffer_margin:%d;", |
279 | inst->parms.buffer_margin); |
280 | ctx->config.length = pbuf - ctx->config.buf; |
281 | } |
282 | |
283 | inst->vdec.config = ctx->config; |
284 | |
285 | inst->parms.dec_parms_status |= |
286 | V4L2_CONFIG_PARM_DECODE_COMMON; |
287 | } |
288 | |
289 | static int vdec_h264_init(struct aml_vcodec_ctx *ctx, unsigned long *h_vdec) |
290 | { |
291 | struct vdec_h264_inst *inst = NULL; |
292 | int ret = -1; |
293 | |
294 | inst = kzalloc(sizeof(*inst), GFP_KERNEL); |
295 | if (!inst) |
296 | return -ENOMEM; |
297 | |
298 | inst->vdec.video_type = VFORMAT_H264; |
299 | inst->vdec.dev = ctx->dev->vpu_plat_dev; |
300 | inst->vdec.filp = ctx->dev->filp; |
301 | inst->vdec.ctx = ctx; |
302 | inst->ctx = ctx; |
303 | |
304 | vdec_parser_parms(inst); |
305 | |
306 | /* set play mode.*/ |
307 | if (ctx->is_drm_mode) |
308 | inst->vdec.port.flag |= PORT_FLAG_DRM; |
309 | |
310 | /* init vfm */ |
311 | inst->vfm.ctx = ctx; |
312 | inst->vfm.ada_ctx = &inst->vdec; |
313 | vcodec_vfm_init(&inst->vfm); |
314 | |
315 | ret = video_decoder_init(&inst->vdec); |
316 | if (ret) { |
317 | aml_vcodec_err(inst, "vdec_h264 init err=%d", ret); |
318 | goto error_free_inst; |
319 | } |
320 | |
321 | /* probe info from the stream */ |
322 | inst->vsi = kzalloc(sizeof(struct vdec_h264_vsi), GFP_KERNEL); |
323 | if (!inst->vsi) { |
324 | ret = -ENOMEM; |
325 | goto error_free_inst; |
326 | } |
327 | |
328 | /* alloc the header buffer to be used cache sps or spp etc.*/ |
329 | inst->vsi->header_buf = kzalloc(HEADER_BUFFER_SIZE, GFP_KERNEL); |
330 | if (!inst->vsi) { |
331 | ret = -ENOMEM; |
332 | goto error_free_vsi; |
333 | } |
334 | |
335 | init_completion(&inst->comp); |
336 | |
337 | aml_vcodec_debug(inst, "H264 Instance >> %p", inst); |
338 | |
339 | ctx->ada_ctx = &inst->vdec; |
340 | *h_vdec = (unsigned long)inst; |
341 | |
342 | //dump_init(); |
343 | |
344 | return 0; |
345 | |
346 | error_free_vsi: |
347 | kfree(inst->vsi); |
348 | error_free_inst: |
349 | kfree(inst); |
350 | *h_vdec = 0; |
351 | |
352 | return ret; |
353 | } |
354 | |
355 | #if 0 |
356 | static int refer_buffer_num(int level_idc, int max_poc_cnt, |
357 | int mb_width, int mb_height) |
358 | { |
359 | int size; |
360 | int pic_size = mb_width * mb_height * 384; |
361 | |
362 | switch (level_idc) { |
363 | case 9: |
364 | size = 152064; |
365 | break; |
366 | case 10: |
367 | size = 152064; |
368 | break; |
369 | case 11: |
370 | size = 345600; |
371 | break; |
372 | case 12: |
373 | size = 912384; |
374 | break; |
375 | case 13: |
376 | size = 912384; |
377 | break; |
378 | case 20: |
379 | size = 912384; |
380 | break; |
381 | case 21: |
382 | size = 1824768; |
383 | break; |
384 | case 22: |
385 | size = 3110400; |
386 | break; |
387 | case 30: |
388 | size = 3110400; |
389 | break; |
390 | case 31: |
391 | size = 6912000; |
392 | break; |
393 | case 32: |
394 | size = 7864320; |
395 | break; |
396 | case 40: |
397 | size = 12582912; |
398 | break; |
399 | case 41: |
400 | size = 12582912; |
401 | break; |
402 | case 42: |
403 | size = 13369344; |
404 | break; |
405 | case 50: |
406 | size = 42393600; |
407 | break; |
408 | case 51: |
409 | case 52: |
410 | default: |
411 | size = 70778880; |
412 | break; |
413 | } |
414 | |
415 | size /= pic_size; |
416 | size = size + 1; /* need more buffers */ |
417 | |
418 | if (size > max_poc_cnt) |
419 | size = max_poc_cnt; |
420 | |
421 | return size; |
422 | } |
423 | #endif |
424 | |
425 | static void vdec_config_dw_mode(struct vdec_pic_info *pic, int dw_mode) |
426 | { |
427 | switch (dw_mode) { |
428 | case 0x1: /* (w x h) + (w/2 x h) */ |
429 | pic->coded_width += pic->coded_width >> 1; |
430 | pic->y_len_sz = pic->coded_width * pic->coded_height; |
431 | pic->c_len_sz = pic->y_len_sz >> 1; |
432 | break; |
433 | case 0x2: /* (w x h) + (w/2 x h/2) */ |
434 | pic->coded_width += pic->coded_width >> 1; |
435 | pic->coded_height += pic->coded_height >> 1; |
436 | pic->y_len_sz = pic->coded_width * pic->coded_height; |
437 | pic->c_len_sz = pic->y_len_sz >> 1; |
438 | break; |
439 | default: /* nothing to do */ |
440 | break; |
441 | } |
442 | } |
443 | |
444 | static void fill_vdec_params(struct vdec_h264_inst *inst, struct h264_SPS_t *sps) |
445 | { |
446 | struct vdec_pic_info *pic = &inst->vsi->pic; |
447 | struct vdec_h264_dec_info *dec = &inst->vsi->dec; |
448 | struct v4l2_rect *rect = &inst->vsi->crop; |
449 | int dw = inst->parms.double_write_mode; |
450 | int margin = inst->parms.buffer_margin; |
451 | u32 mb_w, mb_h, width, height; |
452 | |
453 | mb_w = sps->mb_width; |
454 | mb_h = sps->mb_height; |
455 | |
456 | width = mb_w << 4; |
457 | height = mb_h << 4; |
458 | |
459 | width -= (sps->crop_left + sps->crop_right); |
460 | height -= (sps->crop_top + sps->crop_bottom); |
461 | |
462 | /* fill visible area size that be used for EGL. */ |
463 | pic->visible_width = width; |
464 | pic->visible_height = height; |
465 | |
466 | /* calc visible ares. */ |
467 | rect->left = 0; |
468 | rect->top = 0; |
469 | rect->width = pic->visible_width; |
470 | rect->height = pic->visible_height; |
471 | |
472 | /* config canvas size that be used for decoder. */ |
473 | pic->coded_width = ALIGN(mb_w, 4) << 4; |
474 | pic->coded_height = ALIGN(mb_h, 4) << 4; |
475 | pic->y_len_sz = pic->coded_width * pic->coded_height; |
476 | pic->c_len_sz = pic->y_len_sz >> 1; |
477 | |
478 | /* calc DPB size */ |
479 | dec->dpb_sz = sps->num_reorder_frames + 1 + margin; |
480 | |
481 | vdec_config_dw_mode(pic, dw); |
482 | |
483 | inst->parms.dec_parms_status |= |
484 | V4L2_CONFIG_PARM_DECODE_PICINFO; |
485 | |
486 | aml_vcodec_debug(inst, "[%d] The stream infos, dw: %d, coded:(%d x %d), visible:(%d x %d), DPB: %d, margin: %d\n", |
487 | inst->ctx->id,inst->parms.double_write_mode, |
488 | pic->coded_width, pic->coded_height, |
489 | pic->visible_width, pic->visible_height, |
490 | dec->dpb_sz - margin + 1, margin); |
491 | } |
492 | |
493 | static bool check_frame_combine(u8 *buf, u32 size, int *pos) |
494 | { |
495 | bool combine = false; |
496 | int i = 0, j = 0, cnt = 0; |
497 | u8 *p = buf; |
498 | |
499 | for (i = 4; i < size; i++) { |
500 | j = find_start_code(p, 7); |
501 | if (j > 0) { |
502 | if (++cnt > 1) { |
503 | combine = true; |
504 | break; |
505 | } |
506 | |
507 | *pos = p - buf + j; |
508 | p += j; |
509 | i += j; |
510 | } |
511 | p++; |
512 | } |
513 | |
514 | //pr_info("nal pos: %d, is_combine: %d\n",*pos, *is_combine); |
515 | return combine; |
516 | } |
517 | |
518 | static int vdec_search_startcode(u8 *buf, u32 range) |
519 | { |
520 | int pos = -1; |
521 | int i = 0, j = 0; |
522 | u8 *p = buf; |
523 | |
524 | for (i = 4; i < range; i++) { |
525 | j = find_start_code(p, 7); |
526 | if (j > 0) { |
527 | pos = p - buf + j; |
528 | break; |
529 | } |
530 | p++; |
531 | } |
532 | |
533 | return pos; |
534 | } |
535 | |
536 | static int stream_parse_by_ucode(struct vdec_h264_inst *inst, u8 *buf, u32 size) |
537 | { |
538 | int ret = 0; |
539 | struct aml_vdec_adapt *vdec = &inst->vdec; |
540 | |
541 | ret = vdec_vframe_write(vdec, buf, size, 0); |
542 | if (ret < 0) { |
543 | pr_err("write frame data failed. err: %d\n", ret); |
544 | return ret; |
545 | } |
546 | |
547 | /* wait ucode parse ending. */ |
548 | wait_for_completion_timeout(&inst->comp, |
549 | msecs_to_jiffies(1000)); |
550 | |
551 | return inst->vsi->dec.dpb_sz ? 0 : -1; |
552 | } |
553 | |
554 | static int stream_parse(struct vdec_h264_inst *inst, u8 *buf, u32 size) |
555 | { |
556 | int ret = 0; |
557 | struct h264_param_sets *ps; |
558 | int nal_idx = 0; |
559 | bool is_combine = false; |
560 | |
561 | is_combine = check_frame_combine(buf, size, &nal_idx); |
562 | if (nal_idx < 0) |
563 | return -1; |
564 | |
565 | /* if the st compose from csd + slice that is the combine data. */ |
566 | inst->vsi->is_combine = is_combine; |
567 | inst->vsi->nalu_pos = nal_idx; |
568 | |
569 | ps = kzalloc(sizeof(struct h264_param_sets), GFP_KERNEL); |
570 | if (ps == NULL) |
571 | return -ENOMEM; |
572 | |
573 | ret = h264_decode_extradata_ps(buf, size, ps); |
574 | if (ret) { |
575 | pr_err("parse extra data failed. err: %d\n", ret); |
576 | goto out; |
577 | } |
578 | |
579 | if (ps->sps_parsed) |
580 | fill_vdec_params(inst, &ps->sps); |
581 | |
582 | ret = ps->sps_parsed ? 0 : -1; |
583 | out: |
584 | kfree(ps); |
585 | |
586 | return ret; |
587 | } |
588 | |
589 | static int vdec_h264_probe(unsigned long h_vdec, |
590 | struct aml_vcodec_mem *bs, void *out) |
591 | { |
592 | struct vdec_h264_inst *inst = |
593 | (struct vdec_h264_inst *)h_vdec; |
594 | struct stream_info *st; |
595 | u8 *buf = (u8 *)bs->vaddr; |
596 | u32 size = bs->size; |
597 | int ret = 0; |
598 | |
599 | st = (struct stream_info *)buf; |
600 | if (inst->ctx->is_drm_mode && (st->magic == DRMe || st->magic == DRMn)) |
601 | return 0; |
602 | |
603 | if (st->magic == NORe || st->magic == NORn) { |
604 | buf = st->data; |
605 | size = st->length; |
606 | } |
607 | |
608 | skip_aud_data(&buf, &size); |
609 | |
610 | if (inst->ctx->param_sets_from_ucode) |
611 | ret = stream_parse_by_ucode(inst, buf, size); |
612 | else |
613 | ret = stream_parse(inst, buf, size); |
614 | |
615 | inst->vsi->cur_pic = inst->vsi->pic; |
616 | |
617 | return ret; |
618 | } |
619 | |
620 | static void vdec_h264_deinit(unsigned long h_vdec) |
621 | { |
622 | ulong flags; |
623 | struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec; |
624 | struct aml_vcodec_ctx *ctx = inst->ctx; |
625 | |
626 | aml_vcodec_debug_enter(inst); |
627 | |
628 | video_decoder_release(&inst->vdec); |
629 | |
630 | vcodec_vfm_release(&inst->vfm); |
631 | |
632 | //dump_deinit(); |
633 | |
634 | spin_lock_irqsave(&ctx->slock, flags); |
635 | if (inst->vsi && inst->vsi->header_buf) |
636 | kfree(inst->vsi->header_buf); |
637 | |
638 | if (inst->vsi) |
639 | kfree(inst->vsi); |
640 | |
641 | kfree(inst); |
642 | |
643 | ctx->drv_handle = 0; |
644 | spin_unlock_irqrestore(&ctx->slock, flags); |
645 | } |
646 | |
647 | static int vdec_h264_get_fb(struct vdec_h264_inst *inst, struct vdec_v4l2_buffer **out) |
648 | { |
649 | return get_fb_from_queue(inst->ctx, out); |
650 | } |
651 | |
652 | static void vdec_h264_get_vf(struct vdec_h264_inst *inst, struct vdec_v4l2_buffer **out) |
653 | { |
654 | struct vframe_s *vf = NULL; |
655 | struct vdec_v4l2_buffer *fb = NULL; |
656 | |
657 | vf = peek_video_frame(&inst->vfm); |
658 | if (!vf) { |
659 | aml_vcodec_debug(inst, "there is no vframe."); |
660 | *out = NULL; |
661 | return; |
662 | } |
663 | |
664 | vf = get_video_frame(&inst->vfm); |
665 | if (!vf) { |
666 | aml_vcodec_debug(inst, "the vframe is avalid."); |
667 | *out = NULL; |
668 | return; |
669 | } |
670 | |
671 | atomic_set(&vf->use_cnt, 1); |
672 | |
673 | fb = (struct vdec_v4l2_buffer *)vf->v4l_mem_handle; |
674 | fb->vf_handle = (unsigned long)vf; |
675 | fb->status = FB_ST_DISPLAY; |
676 | |
677 | *out = fb; |
678 | |
679 | //pr_info("%s, %d\n", __func__, fb->base_y.bytes_used); |
680 | //dump_write(fb->base_y.vaddr, fb->base_y.bytes_used); |
681 | //dump_write(fb->base_c.vaddr, fb->base_c.bytes_used); |
682 | |
683 | /* convert yuv format. */ |
684 | //swap_uv(fb->base_c.vaddr, fb->base_c.size); |
685 | } |
686 | |
687 | static int vdec_write_nalu(struct vdec_h264_inst *inst, |
688 | u8 *buf, u32 size, u64 ts) |
689 | { |
690 | int ret = -1; |
691 | struct aml_vdec_adapt *vdec = &inst->vdec; |
692 | bool is_combine = inst->vsi->is_combine; |
693 | int nalu_pos; |
694 | u32 nal_type; |
695 | |
696 | /*print_hex_debug(buf, size, 32);*/ |
697 | |
698 | nalu_pos = vdec_search_startcode(buf, 16); |
699 | if (nalu_pos < 0) |
700 | goto err; |
701 | |
702 | nal_type = AVC_NAL_TYPE(buf[nalu_pos]); |
703 | //aml_vcodec_debug(inst, "NALU type: %d, size: %u", nal_type, size); |
704 | |
705 | if (nal_type == NAL_H264_SPS && !is_combine) { |
706 | if (inst->vsi->head_offset + size > HEADER_BUFFER_SIZE) { |
707 | ret = -EILSEQ; |
708 | goto err; |
709 | } |
710 | inst->vsi->sps_size = size; |
711 | memcpy(inst->vsi->header_buf + inst->vsi->head_offset, buf, size); |
712 | inst->vsi->head_offset += inst->vsi->sps_size; |
713 | ret = size; |
714 | } else if (nal_type == NAL_H264_PPS && !is_combine) { |
715 | //buf_sz -= nal_start_idx; |
716 | if (inst->vsi->head_offset + size > HEADER_BUFFER_SIZE) { |
717 | ret = -EILSEQ; |
718 | goto err; |
719 | } |
720 | inst->vsi->pps_size = size; |
721 | memcpy(inst->vsi->header_buf + inst->vsi->head_offset, buf, size); |
722 | inst->vsi->head_offset += inst->vsi->pps_size; |
723 | ret = size; |
724 | } else if (nal_type == NAL_H264_SEI && !is_combine) { |
725 | if (inst->vsi->head_offset + size > HEADER_BUFFER_SIZE) { |
726 | ret = -EILSEQ; |
727 | goto err; |
728 | } |
729 | inst->vsi->sei_size = size; |
730 | memcpy(inst->vsi->header_buf + inst->vsi->head_offset, buf, size); |
731 | inst->vsi->head_offset += inst->vsi->sei_size; |
732 | ret = size; |
733 | } else if (inst->vsi->head_offset == 0) { |
734 | ret = vdec_vframe_write(vdec, buf, size, ts); |
735 | } else { |
736 | char *write_buf = vmalloc(inst->vsi->head_offset + size); |
737 | if (!write_buf) { |
738 | ret = -ENOMEM; |
739 | goto err; |
740 | } |
741 | |
742 | memcpy(write_buf, inst->vsi->header_buf, inst->vsi->head_offset); |
743 | memcpy(write_buf + inst->vsi->head_offset, buf, size); |
744 | |
745 | ret = vdec_vframe_write(vdec, write_buf, |
746 | inst->vsi->head_offset + size, ts); |
747 | |
748 | memset(inst->vsi->header_buf, 0, HEADER_BUFFER_SIZE); |
749 | inst->vsi->head_offset = 0; |
750 | inst->vsi->sps_size = 0; |
751 | inst->vsi->pps_size = 0; |
752 | inst->vsi->sei_size = 0; |
753 | |
754 | vfree(write_buf); |
755 | } |
756 | |
757 | return ret; |
758 | err: |
759 | aml_vcodec_err(inst, "%s err(%d)", __func__, ret); |
760 | return ret; |
761 | } |
762 | |
763 | static bool monitor_res_change(struct vdec_h264_inst *inst, u8 *buf, u32 size) |
764 | { |
765 | int ret = 0, i = 0, j = 0; |
766 | u8 *p = buf; |
767 | int len = size; |
768 | u32 type; |
769 | |
770 | for (i = 4; i < size; i++) { |
771 | j = find_start_code(p, len); |
772 | if (j > 0) { |
773 | len = size - (p - buf); |
774 | type = AVC_NAL_TYPE(p[j]); |
775 | if (type != NAL_H264_AUD && |
776 | (type > NAL_H264_PPS || type < NAL_H264_SEI)) |
777 | break; |
778 | |
779 | if (type == NAL_H264_SPS) { |
780 | ret = stream_parse(inst, p, len); |
781 | if (!ret && (inst->vsi->cur_pic.coded_width != |
782 | inst->vsi->pic.coded_width || |
783 | inst->vsi->cur_pic.coded_height != |
784 | inst->vsi->pic.coded_height)) { |
785 | inst->vsi->cur_pic = inst->vsi->pic; |
786 | return true; |
787 | } |
788 | } |
789 | p += j; |
790 | } |
791 | p++; |
792 | } |
793 | |
794 | return false; |
795 | } |
796 | |
797 | static int vdec_h264_decode(unsigned long h_vdec, struct aml_vcodec_mem *bs, |
798 | u64 timestamp, bool *res_chg) |
799 | { |
800 | struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec; |
801 | struct aml_vdec_adapt *vdec = &inst->vdec; |
802 | struct stream_info *st; |
803 | u8 *buf; |
804 | u32 size; |
805 | int ret = -1; |
806 | |
807 | /* bs NULL means flush decoder */ |
808 | if (bs == NULL) |
809 | return -1; |
810 | |
811 | buf = (u8 *)bs->vaddr; |
812 | size = bs->size; |
813 | st = (struct stream_info *)buf; |
814 | |
815 | if (inst->ctx->is_drm_mode && (st->magic == DRMe || st->magic == DRMn)) |
816 | ret = vdec_vbuf_write(vdec, st->m.buf, sizeof(st->m.drm)); |
817 | else if (st->magic == NORe) |
818 | ret = vdec_vbuf_write(vdec, st->data, st->length); |
819 | else if (st->magic == NORn) |
820 | ret = vdec_write_nalu(inst, st->data, st->length, timestamp); |
821 | else if (inst->ctx->is_stream_mode) |
822 | ret = vdec_vbuf_write(vdec, buf, size); |
823 | else { |
824 | /*checked whether the resolution changes.*/ |
825 | if ((*res_chg = monitor_res_change(inst, buf, size))) |
826 | return 0; |
827 | |
828 | ret = vdec_write_nalu(inst, buf, size, timestamp); |
829 | } |
830 | |
831 | return ret; |
832 | } |
833 | |
834 | static void get_param_config_info(struct vdec_h264_inst *inst, |
835 | struct aml_dec_params *parms) |
836 | { |
837 | *parms = inst->parms; |
838 | |
839 | aml_vcodec_debug(inst, "parms status: %u", parms->dec_parms_status); |
840 | } |
841 | |
842 | static int vdec_h264_get_param(unsigned long h_vdec, |
843 | enum vdec_get_param_type type, void *out) |
844 | { |
845 | int ret = 0; |
846 | struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec; |
847 | |
848 | if (!inst) { |
849 | pr_err("the h264 inst of dec is invalid.\n"); |
850 | return -1; |
851 | } |
852 | |
853 | switch (type) { |
854 | case GET_PARAM_DISP_FRAME_BUFFER: |
855 | vdec_h264_get_vf(inst, out); |
856 | break; |
857 | |
858 | case GET_PARAM_FREE_FRAME_BUFFER: |
859 | ret = vdec_h264_get_fb(inst, out); |
860 | break; |
861 | |
862 | case GET_PARAM_PIC_INFO: |
863 | get_pic_info(inst, out); |
864 | break; |
865 | |
866 | case GET_PARAM_DPB_SIZE: |
867 | get_dpb_size(inst, out); |
868 | break; |
869 | |
870 | case GET_PARAM_CROP_INFO: |
871 | get_crop_info(inst, out); |
872 | break; |
873 | |
874 | case GET_PARAM_CONFIG_INFO: |
875 | get_param_config_info(inst, out); |
876 | break; |
877 | default: |
878 | aml_vcodec_err(inst, "invalid get parameter type=%d", type); |
879 | ret = -EINVAL; |
880 | } |
881 | |
882 | return ret; |
883 | } |
884 | |
885 | static void set_param_write_sync(struct vdec_h264_inst *inst) |
886 | { |
887 | complete(&inst->comp); |
888 | } |
889 | |
890 | static void set_param_pic_info(struct vdec_h264_inst *inst, |
891 | struct aml_vdec_pic_infos *info) |
892 | { |
893 | struct vdec_pic_info *pic = &inst->vsi->pic; |
894 | struct vdec_h264_dec_info *dec = &inst->vsi->dec; |
895 | struct v4l2_rect *rect = &inst->vsi->crop; |
896 | |
897 | /* fill visible area size that be used for EGL. */ |
898 | pic->visible_width = info->visible_width; |
899 | pic->visible_height = info->visible_height; |
900 | |
901 | /* calc visible ares. */ |
902 | rect->left = 0; |
903 | rect->top = 0; |
904 | rect->width = pic->visible_width; |
905 | rect->height = pic->visible_height; |
906 | |
907 | /* config canvas size that be used for decoder. */ |
908 | pic->coded_width = info->coded_width; |
909 | pic->coded_height = info->coded_height; |
910 | pic->y_len_sz = pic->coded_width * pic->coded_height; |
911 | pic->c_len_sz = pic->y_len_sz >> 1; |
912 | |
913 | dec->dpb_sz = info->dpb_size; |
914 | |
915 | inst->parms.dec_parms_status |= |
916 | V4L2_CONFIG_PARM_DECODE_PICINFO; |
917 | |
918 | /*wake up*/ |
919 | complete(&inst->comp); |
920 | |
921 | pr_info("Parse from ucode, crop(%d x %d), coded(%d x %d) dpb: %d\n", |
922 | info->visible_width, info->visible_height, |
923 | info->coded_width, info->coded_height, |
924 | info->dpb_size); |
925 | } |
926 | |
927 | static void set_param_hdr_info(struct vdec_h264_inst *inst, |
928 | struct aml_vdec_hdr_infos *hdr) |
929 | { |
930 | inst->parms.hdr = *hdr; |
931 | inst->parms.dec_parms_status |= |
932 | V4L2_CONFIG_PARM_DECODE_HDRINFO; |
933 | |
934 | //pr_info("H264 set HDR infos\n"); |
935 | } |
936 | |
937 | static int vdec_h264_set_param(unsigned long h_vdec, |
938 | enum vdec_set_param_type type, void *in) |
939 | { |
940 | int ret = 0; |
941 | struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec; |
942 | |
943 | if (!inst) { |
944 | pr_err("the h264 inst of dec is invalid.\n"); |
945 | return -1; |
946 | } |
947 | |
948 | switch (type) { |
949 | case SET_PARAM_WRITE_FRAME_SYNC: |
950 | set_param_write_sync(inst); |
951 | break; |
952 | |
953 | case SET_PARAM_PIC_INFO: |
954 | set_param_pic_info(inst, in); |
955 | break; |
956 | |
957 | case SET_PARAM_HDR_INFO: |
958 | set_param_hdr_info(inst, in); |
959 | break; |
960 | default: |
961 | aml_vcodec_err(inst, "invalid set parameter type=%d", type); |
962 | ret = -EINVAL; |
963 | } |
964 | |
965 | return ret; |
966 | } |
967 | |
968 | static struct vdec_common_if vdec_h264_if = { |
969 | .init = vdec_h264_init, |
970 | .probe = vdec_h264_probe, |
971 | .decode = vdec_h264_decode, |
972 | .get_param = vdec_h264_get_param, |
973 | .set_param = vdec_h264_set_param, |
974 | .deinit = vdec_h264_deinit, |
975 | }; |
976 | |
977 | struct vdec_common_if *get_h264_dec_comm_if(void); |
978 | |
979 | struct vdec_common_if *get_h264_dec_comm_if(void) |
980 | { |
981 | return &vdec_h264_if; |
982 | } |
983 |