summaryrefslogtreecommitdiff
path: root/drivers/amvdec_ports/decoder/vdec_h264_if.c (plain)
blob: ea1d0f8b367ffa0cfbccbc3f2d0afc4833f50e01
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 */
67struct 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 */
82struct 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 */
100struct 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 */
122struct 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 */
146struct 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"
160static struct file *filp;
161static loff_t file_pos;
162
163void 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
179void 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
188void dump_deinit(void)
189{
190 if (filp) {
191 filp_close(filp, current->files);
192 filp = NULL;
193 file_pos = 0;
194 }
195}
196
197void 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
209static 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
221static 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
232static 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
238static 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
249static 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
260static void vdec_parser_parms(struct vdec_h264_inst *inst)
261{
262 struct aml_vcodec_ctx *ctx = inst->ctx;
263
264 if (ctx->config.parm.dec.parms_status &
265 V4L2_CONFIG_PARM_DECODE_CFGINFO) {
266 u8 *pbuf = ctx->config.buf;
267
268 pbuf += sprintf(pbuf, "parm_v4l_codec_enable:1;");
269 pbuf += sprintf(pbuf, "mh264_double_write_mode:%d;",
270 ctx->config.parm.dec.cfg.double_write_mode);
271 pbuf += sprintf(pbuf, "parm_v4l_buffer_margin:%d;",
272 ctx->config.parm.dec.cfg.ref_buf_margin);
273 ctx->config.length = pbuf - ctx->config.buf;
274 } else {
275 ctx->config.parm.dec.cfg.double_write_mode = 16;
276 ctx->config.length = vdec_config_default_parms(ctx->config.buf);
277 }
278
279 inst->vdec.config = ctx->config;
280 inst->parms.cfg = ctx->config.parm.dec.cfg;
281 inst->parms.parms_status |= V4L2_CONFIG_PARM_DECODE_CFGINFO;
282}
283
284static int vdec_h264_init(struct aml_vcodec_ctx *ctx, unsigned long *h_vdec)
285{
286 struct vdec_h264_inst *inst = NULL;
287 int ret = -1;
288
289 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
290 if (!inst)
291 return -ENOMEM;
292
293 inst->vdec.video_type = VFORMAT_H264;
294 inst->vdec.dev = ctx->dev->vpu_plat_dev;
295 inst->vdec.filp = ctx->dev->filp;
296 inst->vdec.ctx = ctx;
297 inst->ctx = ctx;
298
299 vdec_parser_parms(inst);
300
301 /* set play mode.*/
302 if (ctx->is_drm_mode)
303 inst->vdec.port.flag |= PORT_FLAG_DRM;
304
305 /* init vfm */
306 inst->vfm.ctx = ctx;
307 inst->vfm.ada_ctx = &inst->vdec;
308 ret = vcodec_vfm_init(&inst->vfm);
309 if (ret) {
310 pr_err("%s, init vfm failed.\n", __func__);
311 goto err;
312 }
313
314 ret = video_decoder_init(&inst->vdec);
315 if (ret) {
316 aml_vcodec_err(inst, "vdec_h264 init err=%d", ret);
317 goto err;
318 }
319
320 /* probe info from the stream */
321 inst->vsi = kzalloc(sizeof(struct vdec_h264_vsi), GFP_KERNEL);
322 if (!inst->vsi) {
323 ret = -ENOMEM;
324 goto err;
325 }
326
327 /* alloc the header buffer to be used cache sps or spp etc.*/
328 inst->vsi->header_buf = kzalloc(HEADER_BUFFER_SIZE, GFP_KERNEL);
329 if (!inst->vsi) {
330 ret = -ENOMEM;
331 goto err;
332 }
333
334 init_completion(&inst->comp);
335
336 aml_vcodec_debug(inst, "H264 Instance >> %p", inst);
337
338 ctx->ada_ctx = &inst->vdec;
339 *h_vdec = (unsigned long)inst;
340
341 //dump_init();
342
343 return 0;
344err:
345 if (inst)
346 vcodec_vfm_release(&inst->vfm);
347 if (inst && inst->vsi && inst->vsi->header_buf)
348 kfree(inst->vsi->header_buf);
349 if (inst && inst->vsi)
350 kfree(inst->vsi);
351 if (inst)
352 kfree(inst);
353 *h_vdec = 0;
354
355 return ret;
356}
357
358#if 0
359static int refer_buffer_num(int level_idc, int max_poc_cnt,
360 int mb_width, int mb_height)
361{
362 int size;
363 int pic_size = mb_width * mb_height * 384;
364
365 switch (level_idc) {
366 case 9:
367 size = 152064;
368 break;
369 case 10:
370 size = 152064;
371 break;
372 case 11:
373 size = 345600;
374 break;
375 case 12:
376 size = 912384;
377 break;
378 case 13:
379 size = 912384;
380 break;
381 case 20:
382 size = 912384;
383 break;
384 case 21:
385 size = 1824768;
386 break;
387 case 22:
388 size = 3110400;
389 break;
390 case 30:
391 size = 3110400;
392 break;
393 case 31:
394 size = 6912000;
395 break;
396 case 32:
397 size = 7864320;
398 break;
399 case 40:
400 size = 12582912;
401 break;
402 case 41:
403 size = 12582912;
404 break;
405 case 42:
406 size = 13369344;
407 break;
408 case 50:
409 size = 42393600;
410 break;
411 case 51:
412 case 52:
413 default:
414 size = 70778880;
415 break;
416 }
417
418 size /= pic_size;
419 size = size + 1; /* need more buffers */
420
421 if (size > max_poc_cnt)
422 size = max_poc_cnt;
423
424 return size;
425}
426#endif
427
428static void vdec_config_dw_mode(struct vdec_pic_info *pic, int dw_mode)
429{
430 switch (dw_mode) {
431 case 0x1: /* (w x h) + (w/2 x h) */
432 pic->coded_width += pic->coded_width >> 1;
433 pic->y_len_sz = pic->coded_width * pic->coded_height;
434 pic->c_len_sz = pic->y_len_sz >> 1;
435 break;
436 case 0x2: /* (w x h) + (w/2 x h/2) */
437 pic->coded_width += pic->coded_width >> 1;
438 pic->coded_height += pic->coded_height >> 1;
439 pic->y_len_sz = pic->coded_width * pic->coded_height;
440 pic->c_len_sz = pic->y_len_sz >> 1;
441 break;
442 default: /* nothing to do */
443 break;
444 }
445}
446
447static void fill_vdec_params(struct vdec_h264_inst *inst, struct h264_SPS_t *sps)
448{
449 struct vdec_pic_info *pic = &inst->vsi->pic;
450 struct vdec_h264_dec_info *dec = &inst->vsi->dec;
451 struct v4l2_rect *rect = &inst->vsi->crop;
452 int dw = inst->parms.cfg.double_write_mode;
453 int margin = inst->parms.cfg.ref_buf_margin;
454 u32 mb_w, mb_h, width, height;
455
456 mb_w = sps->mb_width;
457 mb_h = sps->mb_height;
458
459 width = mb_w << 4;
460 height = mb_h << 4;
461
462 width -= (sps->crop_left + sps->crop_right);
463 height -= (sps->crop_top + sps->crop_bottom);
464
465 /* fill visible area size that be used for EGL. */
466 pic->visible_width = width;
467 pic->visible_height = height;
468
469 /* calc visible ares. */
470 rect->left = 0;
471 rect->top = 0;
472 rect->width = pic->visible_width;
473 rect->height = pic->visible_height;
474
475 /* config canvas size that be used for decoder. */
476 pic->coded_width = ALIGN(mb_w, 4) << 4;
477 pic->coded_height = ALIGN(mb_h, 4) << 4;
478 pic->y_len_sz = pic->coded_width * pic->coded_height;
479 pic->c_len_sz = pic->y_len_sz >> 1;
480 pic->profile_idc = sps->profile_idc;
481
482 /* calc DPB size */
483 dec->dpb_sz = sps->num_reorder_frames + margin;
484
485 inst->parms.ps.visible_width = pic->visible_width;
486 inst->parms.ps.visible_height = pic->visible_height;
487 inst->parms.ps.coded_width = pic->coded_width;
488 inst->parms.ps.coded_height = pic->coded_height;
489 inst->parms.ps.profile = sps->profile_idc;
490 inst->parms.ps.mb_width = sps->mb_width;
491 inst->parms.ps.mb_height = sps->mb_height;
492 inst->parms.ps.ref_frames = sps->ref_frame_count;
493 inst->parms.ps.reorder_frames = sps->num_reorder_frames;
494 inst->parms.ps.dpb_size = dec->dpb_sz;
495 inst->parms.parms_status |= V4L2_CONFIG_PARM_DECODE_PSINFO;
496
497 vdec_config_dw_mode(pic, dw);
498
499 aml_vcodec_debug(inst, "[%d] The stream infos, dw: %d, coded:(%d x %d), visible:(%d x %d), DPB: %d, margin: %d\n",
500 inst->ctx->id, dw, pic->coded_width, pic->coded_height,
501 pic->visible_width, pic->visible_height,
502 dec->dpb_sz - margin, margin);
503}
504
505static bool check_frame_combine(u8 *buf, u32 size, int *pos)
506{
507 bool combine = false;
508 int i = 0, j = 0, cnt = 0;
509 u8 *p = buf;
510
511 for (i = 4; i < size; i++) {
512 j = find_start_code(p, 7);
513 if (j > 0) {
514 if (++cnt > 1) {
515 combine = true;
516 break;
517 }
518
519 *pos = p - buf + j;
520 p += j;
521 i += j;
522 }
523 p++;
524 }
525
526 //pr_info("nal pos: %d, is_combine: %d\n",*pos, *is_combine);
527 return combine;
528}
529
530static int vdec_search_startcode(u8 *buf, u32 range)
531{
532 int pos = -1;
533 int i = 0, j = 0;
534 u8 *p = buf;
535
536 for (i = 4; i < range; i++) {
537 j = find_start_code(p, 7);
538 if (j > 0) {
539 pos = p - buf + j;
540 break;
541 }
542 p++;
543 }
544
545 return pos;
546}
547
548static int stream_parse_by_ucode(struct vdec_h264_inst *inst, u8 *buf, u32 size)
549{
550 int ret = 0;
551 struct aml_vdec_adapt *vdec = &inst->vdec;
552
553 ret = vdec_vframe_write(vdec, buf, size, 0);
554 if (ret < 0) {
555 pr_err("write frame data failed. err: %d\n", ret);
556 return ret;
557 }
558
559 /* wait ucode parse ending. */
560 wait_for_completion_timeout(&inst->comp,
561 msecs_to_jiffies(1000));
562
563 return inst->vsi->dec.dpb_sz ? 0 : -1;
564}
565
566static int stream_parse(struct vdec_h264_inst *inst, u8 *buf, u32 size)
567{
568 int ret = 0;
569 struct h264_param_sets *ps;
570 int nal_idx = 0;
571 bool is_combine = false;
572
573 is_combine = check_frame_combine(buf, size, &nal_idx);
574 if (nal_idx < 0)
575 return -1;
576
577 /* if the st compose from csd + slice that is the combine data. */
578 inst->vsi->is_combine = is_combine;
579 inst->vsi->nalu_pos = nal_idx;
580
581 ps = kzalloc(sizeof(struct h264_param_sets), GFP_KERNEL);
582 if (ps == NULL)
583 return -ENOMEM;
584
585 ret = h264_decode_extradata_ps(buf, size, ps);
586 if (ret) {
587 pr_err("parse extra data failed. err: %d\n", ret);
588 goto out;
589 }
590
591 if (ps->sps_parsed)
592 fill_vdec_params(inst, &ps->sps);
593
594 ret = ps->sps_parsed ? 0 : -1;
595out:
596 kfree(ps);
597
598 return ret;
599}
600
601static int vdec_h264_probe(unsigned long h_vdec,
602 struct aml_vcodec_mem *bs, void *out)
603{
604 struct vdec_h264_inst *inst =
605 (struct vdec_h264_inst *)h_vdec;
606 struct stream_info *st;
607 u8 *buf = (u8 *)bs->vaddr;
608 u32 size = bs->size;
609 int ret = 0;
610
611 st = (struct stream_info *)buf;
612 if (inst->ctx->is_drm_mode && (st->magic == DRMe || st->magic == DRMn))
613 return 0;
614
615 if (st->magic == NORe || st->magic == NORn) {
616 buf = st->data;
617 size = st->length;
618 }
619
620 skip_aud_data(&buf, &size);
621
622 if (inst->ctx->param_sets_from_ucode)
623 ret = stream_parse_by_ucode(inst, buf, size);
624 else
625 ret = stream_parse(inst, buf, size);
626
627 inst->vsi->cur_pic = inst->vsi->pic;
628
629 return ret;
630}
631
632static void vdec_h264_deinit(unsigned long h_vdec)
633{
634 ulong flags;
635 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
636 struct aml_vcodec_ctx *ctx = inst->ctx;
637
638 aml_vcodec_debug_enter(inst);
639
640 video_decoder_release(&inst->vdec);
641
642 vcodec_vfm_release(&inst->vfm);
643
644 //dump_deinit();
645
646 spin_lock_irqsave(&ctx->slock, flags);
647 if (inst->vsi && inst->vsi->header_buf)
648 kfree(inst->vsi->header_buf);
649
650 if (inst->vsi)
651 kfree(inst->vsi);
652
653 kfree(inst);
654
655 ctx->drv_handle = 0;
656 spin_unlock_irqrestore(&ctx->slock, flags);
657}
658
659static int vdec_h264_get_fb(struct vdec_h264_inst *inst, struct vdec_v4l2_buffer **out)
660{
661 return get_fb_from_queue(inst->ctx, out);
662}
663
664static void vdec_h264_get_vf(struct vdec_h264_inst *inst, struct vdec_v4l2_buffer **out)
665{
666 struct vframe_s *vf = NULL;
667 struct vdec_v4l2_buffer *fb = NULL;
668
669 vf = peek_video_frame(&inst->vfm);
670 if (!vf) {
671 aml_vcodec_debug(inst, "there is no vframe.");
672 *out = NULL;
673 return;
674 }
675
676 vf = get_video_frame(&inst->vfm);
677 if (!vf) {
678 aml_vcodec_debug(inst, "the vframe is avalid.");
679 *out = NULL;
680 return;
681 }
682
683 atomic_set(&vf->use_cnt, 1);
684
685 fb = (struct vdec_v4l2_buffer *)vf->v4l_mem_handle;
686 fb->vf_handle = (unsigned long)vf;
687 fb->status = FB_ST_DISPLAY;
688
689 *out = fb;
690
691 //pr_info("%s, %d\n", __func__, fb->base_y.bytes_used);
692 //dump_write(fb->base_y.vaddr, fb->base_y.bytes_used);
693 //dump_write(fb->base_c.vaddr, fb->base_c.bytes_used);
694
695 /* convert yuv format. */
696 //swap_uv(fb->base_c.vaddr, fb->base_c.size);
697}
698
699static int vdec_write_nalu(struct vdec_h264_inst *inst,
700 u8 *buf, u32 size, u64 ts)
701{
702 int ret = -1;
703 struct aml_vdec_adapt *vdec = &inst->vdec;
704 bool is_combine = inst->vsi->is_combine;
705 int nalu_pos;
706 u32 nal_type;
707
708 /*print_hex_debug(buf, size, 32);*/
709
710 nalu_pos = vdec_search_startcode(buf, 16);
711 if (nalu_pos < 0)
712 goto err;
713
714 nal_type = AVC_NAL_TYPE(buf[nalu_pos]);
715 //aml_vcodec_debug(inst, "NALU type: %d, size: %u", nal_type, size);
716
717 if (nal_type == NAL_H264_SPS && !is_combine) {
718 if (inst->vsi->head_offset + size > HEADER_BUFFER_SIZE) {
719 ret = -EILSEQ;
720 goto err;
721 }
722 inst->vsi->sps_size = size;
723 memcpy(inst->vsi->header_buf + inst->vsi->head_offset, buf, size);
724 inst->vsi->head_offset += inst->vsi->sps_size;
725 ret = size;
726 } else if (nal_type == NAL_H264_PPS && !is_combine) {
727 //buf_sz -= nal_start_idx;
728 if (inst->vsi->head_offset + size > HEADER_BUFFER_SIZE) {
729 ret = -EILSEQ;
730 goto err;
731 }
732 inst->vsi->pps_size = size;
733 memcpy(inst->vsi->header_buf + inst->vsi->head_offset, buf, size);
734 inst->vsi->head_offset += inst->vsi->pps_size;
735 ret = size;
736 } else if (nal_type == NAL_H264_SEI && !is_combine) {
737 if (inst->vsi->head_offset + size > HEADER_BUFFER_SIZE) {
738 ret = -EILSEQ;
739 goto err;
740 }
741 inst->vsi->sei_size = size;
742 memcpy(inst->vsi->header_buf + inst->vsi->head_offset, buf, size);
743 inst->vsi->head_offset += inst->vsi->sei_size;
744 ret = size;
745 } else if (inst->vsi->head_offset == 0) {
746 ret = vdec_vframe_write(vdec, buf, size, ts);
747 } else {
748 char *write_buf = vmalloc(inst->vsi->head_offset + size);
749 if (!write_buf) {
750 ret = -ENOMEM;
751 goto err;
752 }
753
754 memcpy(write_buf, inst->vsi->header_buf, inst->vsi->head_offset);
755 memcpy(write_buf + inst->vsi->head_offset, buf, size);
756
757 ret = vdec_vframe_write(vdec, write_buf,
758 inst->vsi->head_offset + size, ts);
759
760 memset(inst->vsi->header_buf, 0, HEADER_BUFFER_SIZE);
761 inst->vsi->head_offset = 0;
762 inst->vsi->sps_size = 0;
763 inst->vsi->pps_size = 0;
764 inst->vsi->sei_size = 0;
765
766 vfree(write_buf);
767 }
768
769 return ret;
770err:
771 aml_vcodec_err(inst, "%s err(%d)", __func__, ret);
772 return ret;
773}
774
775static bool monitor_res_change(struct vdec_h264_inst *inst, u8 *buf, u32 size)
776{
777 int ret = 0, i = 0, j = 0;
778 u8 *p = buf;
779 int len = size;
780 u32 type;
781
782 for (i = 4; i < size; i++) {
783 j = find_start_code(p, len);
784 if (j > 0) {
785 len = size - (p - buf);
786 type = AVC_NAL_TYPE(p[j]);
787 if (type != NAL_H264_AUD &&
788 (type > NAL_H264_PPS || type < NAL_H264_SEI))
789 break;
790
791 if (type == NAL_H264_SPS) {
792 ret = stream_parse(inst, p, len);
793 if (!ret && ((inst->vsi->cur_pic.coded_width !=
794 inst->vsi->pic.coded_width ||
795 inst->vsi->cur_pic.coded_height !=
796 inst->vsi->pic.coded_height) ||
797 (inst->vsi->pic.profile_idc !=
798 inst->vsi->cur_pic.profile_idc))) {
799 pr_info("res change\n");
800 inst->vsi->cur_pic = inst->vsi->pic;
801 return true;
802 }
803 }
804 p += j;
805 }
806 p++;
807 }
808
809 return false;
810}
811
812static int vdec_h264_decode(unsigned long h_vdec, struct aml_vcodec_mem *bs,
813 u64 timestamp, bool *res_chg)
814{
815 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
816 struct aml_vdec_adapt *vdec = &inst->vdec;
817 struct stream_info *st;
818 u8 *buf;
819 u32 size;
820 int ret = -1;
821
822 /* bs NULL means flush decoder */
823 if (bs == NULL)
824 return -1;
825
826 buf = (u8 *)bs->vaddr;
827 size = bs->size;
828 st = (struct stream_info *)buf;
829
830 if (inst->ctx->is_drm_mode && (st->magic == DRMe || st->magic == DRMn))
831 ret = vdec_vbuf_write(vdec, st->m.buf, sizeof(st->m.drm));
832 else if (st->magic == NORe)
833 ret = vdec_vbuf_write(vdec, st->data, st->length);
834 else if (st->magic == NORn)
835 ret = vdec_write_nalu(inst, st->data, st->length, timestamp);
836 else if (inst->ctx->is_stream_mode)
837 ret = vdec_vbuf_write(vdec, buf, size);
838 else {
839 /*checked whether the resolution changes.*/
840 if ((*res_chg = monitor_res_change(inst, buf, size)))
841 return 0;
842
843 ret = vdec_write_nalu(inst, buf, size, timestamp);
844 }
845
846 return ret;
847}
848
849static void get_param_config_info(struct vdec_h264_inst *inst,
850 struct aml_dec_params *parms)
851{
852 if (inst->parms.parms_status & V4L2_CONFIG_PARM_DECODE_CFGINFO)
853 parms->cfg = inst->parms.cfg;
854 if (inst->parms.parms_status & V4L2_CONFIG_PARM_DECODE_PSINFO)
855 parms->ps = inst->parms.ps;
856 if (inst->parms.parms_status & V4L2_CONFIG_PARM_DECODE_HDRINFO)
857 parms->hdr = inst->parms.hdr;
858 if (inst->parms.parms_status & V4L2_CONFIG_PARM_DECODE_CNTINFO)
859 parms->cnt = inst->parms.cnt;
860
861 parms->parms_status |= inst->parms.parms_status;
862
863 aml_vcodec_debug(inst, "parms status: %u", parms->parms_status);
864}
865
866static int vdec_h264_get_param(unsigned long h_vdec,
867 enum vdec_get_param_type type, void *out)
868{
869 int ret = 0;
870 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
871
872 if (!inst) {
873 pr_err("the h264 inst of dec is invalid.\n");
874 return -1;
875 }
876
877 switch (type) {
878 case GET_PARAM_DISP_FRAME_BUFFER:
879 vdec_h264_get_vf(inst, out);
880 break;
881
882 case GET_PARAM_FREE_FRAME_BUFFER:
883 ret = vdec_h264_get_fb(inst, out);
884 break;
885
886 case GET_PARAM_PIC_INFO:
887 get_pic_info(inst, out);
888 break;
889
890 case GET_PARAM_DPB_SIZE:
891 get_dpb_size(inst, out);
892 break;
893
894 case GET_PARAM_CROP_INFO:
895 get_crop_info(inst, out);
896 break;
897
898 case GET_PARAM_CONFIG_INFO:
899 get_param_config_info(inst, out);
900 break;
901 default:
902 aml_vcodec_err(inst, "invalid get parameter type=%d", type);
903 ret = -EINVAL;
904 }
905
906 return ret;
907}
908
909static void set_param_write_sync(struct vdec_h264_inst *inst)
910{
911 complete(&inst->comp);
912}
913
914static void set_param_ps_info(struct vdec_h264_inst *inst,
915 struct aml_vdec_ps_infos *ps)
916{
917 struct vdec_pic_info *pic = &inst->vsi->pic;
918 struct vdec_h264_dec_info *dec = &inst->vsi->dec;
919 struct v4l2_rect *rect = &inst->vsi->crop;
920
921 /* fill visible area size that be used for EGL. */
922 pic->visible_width = ps->visible_width;
923 pic->visible_height = ps->visible_height;
924
925 /* calc visible ares. */
926 rect->left = 0;
927 rect->top = 0;
928 rect->width = pic->visible_width;
929 rect->height = pic->visible_height;
930
931 /* config canvas size that be used for decoder. */
932 pic->coded_width = ps->coded_width;
933 pic->coded_height = ps->coded_height;
934 pic->y_len_sz = pic->coded_width * pic->coded_height;
935 pic->c_len_sz = pic->y_len_sz >> 1;
936
937 dec->dpb_sz = ps->dpb_size;
938
939 inst->parms.ps = *ps;
940 inst->parms.parms_status |=
941 V4L2_CONFIG_PARM_DECODE_PSINFO;
942
943 /*wake up*/
944 complete(&inst->comp);
945
946 pr_info("Parse from ucode, crop(%d x %d), coded(%d x %d) dpb: %d\n",
947 ps->visible_width, ps->visible_height,
948 ps->coded_width, ps->coded_height,
949 dec->dpb_sz);
950}
951
952static void set_param_hdr_info(struct vdec_h264_inst *inst,
953 struct aml_vdec_hdr_infos *hdr)
954{
955 inst->parms.hdr = *hdr;
956 if (!(inst->parms.parms_status &
957 V4L2_CONFIG_PARM_DECODE_HDRINFO)) {
958 inst->parms.hdr = *hdr;
959 inst->parms.parms_status |=
960 V4L2_CONFIG_PARM_DECODE_HDRINFO;
961 aml_vdec_dispatch_event(inst->ctx,
962 V4L2_EVENT_SRC_CH_HDRINFO);
963 pr_info("H264 set HDR infos\n");
964 }
965}
966
967static void set_param_post_event(struct vdec_h264_inst *inst, u32 *event)
968{
969 aml_vdec_dispatch_event(inst->ctx, *event);
970 pr_info("H264 post event: %d\n", *event);
971}
972
973static int vdec_h264_set_param(unsigned long h_vdec,
974 enum vdec_set_param_type type, void *in)
975{
976 int ret = 0;
977 struct vdec_h264_inst *inst = (struct vdec_h264_inst *)h_vdec;
978
979 if (!inst) {
980 pr_err("the h264 inst of dec is invalid.\n");
981 return -1;
982 }
983
984 switch (type) {
985 case SET_PARAM_WRITE_FRAME_SYNC:
986 set_param_write_sync(inst);
987 break;
988
989 case SET_PARAM_PS_INFO:
990 set_param_ps_info(inst, in);
991 break;
992
993 case SET_PARAM_HDR_INFO:
994 set_param_hdr_info(inst, in);
995 break;
996
997 case SET_PARAM_POST_EVENT:
998 set_param_post_event(inst, in);
999 break;
1000 default:
1001 aml_vcodec_err(inst, "invalid set parameter type=%d", type);
1002 ret = -EINVAL;
1003 }
1004
1005 return ret;
1006}
1007
1008static struct vdec_common_if vdec_h264_if = {
1009 .init = vdec_h264_init,
1010 .probe = vdec_h264_probe,
1011 .decode = vdec_h264_decode,
1012 .get_param = vdec_h264_get_param,
1013 .set_param = vdec_h264_set_param,
1014 .deinit = vdec_h264_deinit,
1015};
1016
1017struct vdec_common_if *get_h264_dec_comm_if(void);
1018
1019struct vdec_common_if *get_h264_dec_comm_if(void)
1020{
1021 return &vdec_h264_if;
1022}
1023