summaryrefslogtreecommitdiff
path: root/drivers/amvdec_ports/decoder/vdec_mpeg12_if.c (plain)
blob: b5b128bc1191ca023fac5c6337aade8b210e1807
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#include "../vdec_drv_if.h"
27#include "../aml_vcodec_util.h"
28#include "../aml_vcodec_dec.h"
29#include "../aml_vcodec_adapt.h"
30#include "../vdec_drv_base.h"
31#include "../aml_vcodec_vfm.h"
32#include "aml_mpeg12_parser.h"
33
34#define NAL_TYPE(value) ((value) & 0x1F)
35#define HEADER_BUFFER_SIZE (32 * 1024)
36
37/**
38 * struct mpeg12_fb - mpeg12 decode frame buffer information
39 * @vdec_fb_va : virtual address of struct vdec_fb
40 * @y_fb_dma : dma address of Y frame buffer (luma)
41 * @c_fb_dma : dma address of C frame buffer (chroma)
42 * @poc : picture order count of frame buffer
43 * @reserved : for 8 bytes alignment
44 */
45struct mpeg12_fb {
46 uint64_t vdec_fb_va;
47 uint64_t y_fb_dma;
48 uint64_t c_fb_dma;
49 int32_t poc;
50 uint32_t reserved;
51};
52
53/**
54 * struct vdec_mpeg12_dec_info - decode information
55 * @dpb_sz : decoding picture buffer size
56 * @resolution_changed : resoltion change happen
57 * @reserved : for 8 bytes alignment
58 * @bs_dma : Input bit-stream buffer dma address
59 * @y_fb_dma : Y frame buffer dma address
60 * @c_fb_dma : C frame buffer dma address
61 * @vdec_fb_va : VDEC frame buffer struct virtual address
62 */
63struct vdec_mpeg12_dec_info {
64 uint32_t dpb_sz;
65 uint32_t resolution_changed;
66 uint32_t reserved;
67 uint64_t bs_dma;
68 uint64_t y_fb_dma;
69 uint64_t c_fb_dma;
70 uint64_t vdec_fb_va;
71};
72
73/**
74 * struct vdec_mpeg12_vsi - shared memory for decode information exchange
75 * between VPU and Host.
76 * The memory is allocated by VPU then mapping to Host
77 * in vpu_dec_init() and freed in vpu_dec_deinit()
78 * by VPU.
79 * AP-W/R : AP is writer/reader on this item
80 * VPU-W/R: VPU is write/reader on this item
81 * @hdr_buf : Header parsing buffer (AP-W, VPU-R)
82 * @list_free : free frame buffer ring list (AP-W/R, VPU-W)
83 * @list_disp : display frame buffer ring list (AP-R, VPU-W)
84 * @dec : decode information (AP-R, VPU-W)
85 * @pic : picture information (AP-R, VPU-W)
86 * @crop : crop information (AP-R, VPU-W)
87 */
88struct vdec_mpeg12_vsi {
89 char *header_buf;
90 int sps_size;
91 int pps_size;
92 int sei_size;
93 int head_offset;
94 struct vdec_mpeg12_dec_info dec;
95 struct vdec_pic_info pic;
96 struct vdec_pic_info cur_pic;
97 struct v4l2_rect crop;
98 bool is_combine;
99 int nalu_pos;
100 //struct mpeg12_param_sets ps;
101};
102
103/**
104 * struct vdec_mpeg12_inst - mpeg12 decoder instance
105 * @num_nalu : how many nalus be decoded
106 * @ctx : point to aml_vcodec_ctx
107 * @vsi : VPU shared information
108 */
109struct vdec_mpeg12_inst {
110 unsigned int num_nalu;
111 struct aml_vcodec_ctx *ctx;
112 struct aml_vdec_adapt vdec;
113 struct vdec_mpeg12_vsi *vsi;
114 struct vcodec_vfm_s vfm;
115 struct aml_dec_params parms;
116 struct completion comp;
117};
118
119static void get_pic_info(struct vdec_mpeg12_inst *inst,
120 struct vdec_pic_info *pic)
121{
122 *pic = inst->vsi->pic;
123
124 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_EXINFO,
125 "pic(%d, %d), buf(%d, %d)\n",
126 pic->visible_width, pic->visible_height,
127 pic->coded_width, pic->coded_height);
128 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_EXINFO,
129 "Y(%d, %d), C(%d, %d)\n",
130 pic->y_bs_sz, pic->y_len_sz,
131 pic->c_bs_sz, pic->c_len_sz);
132}
133
134static void get_crop_info(struct vdec_mpeg12_inst *inst, struct v4l2_rect *cr)
135{
136 cr->left = inst->vsi->crop.left;
137 cr->top = inst->vsi->crop.top;
138 cr->width = inst->vsi->crop.width;
139 cr->height = inst->vsi->crop.height;
140
141 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_EXINFO,
142 "l=%d, t=%d, w=%d, h=%d\n",
143 cr->left, cr->top, cr->width, cr->height);
144}
145
146static void get_dpb_size(struct vdec_mpeg12_inst *inst, unsigned int *dpb_sz)
147{
148 *dpb_sz = inst->vsi->dec.dpb_sz;
149 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_EXINFO, "sz=%d\n", *dpb_sz);
150}
151
152static u32 vdec_config_default_parms(u8 *parm)
153{
154 u8 *pbuf = parm;
155
156 pbuf += sprintf(pbuf, "parm_v4l_codec_enable:1;");
157 pbuf += sprintf(pbuf, "parm_v4l_canvas_mem_mode:0;");
158 pbuf += sprintf(pbuf, "parm_v4l_buffer_margin:0;");
159
160 return pbuf - parm;
161}
162
163static void vdec_parser_parms(struct vdec_mpeg12_inst *inst)
164{
165 struct aml_vcodec_ctx *ctx = inst->ctx;
166
167 if (ctx->config.parm.dec.parms_status &
168 V4L2_CONFIG_PARM_DECODE_CFGINFO) {
169 u8 *pbuf = ctx->config.buf;
170
171 pbuf += sprintf(pbuf, "parm_v4l_codec_enable:1;");
172 pbuf += sprintf(pbuf, "parm_v4l_canvas_mem_mode:%d;",
173 ctx->config.parm.dec.cfg.canvas_mem_mode);
174 pbuf += sprintf(pbuf, "parm_v4l_buffer_margin:%d;",
175 ctx->config.parm.dec.cfg.ref_buf_margin);
176 ctx->config.length = pbuf - ctx->config.buf;
177 } else {
178 ctx->config.length = vdec_config_default_parms(ctx->config.buf);
179 }
180
181 inst->vdec.config = ctx->config;
182 inst->parms.cfg = ctx->config.parm.dec.cfg;
183 inst->parms.parms_status |= V4L2_CONFIG_PARM_DECODE_CFGINFO;
184}
185
186static int vdec_mpeg12_init(struct aml_vcodec_ctx *ctx, unsigned long *h_vdec)
187{
188 struct vdec_mpeg12_inst *inst = NULL;
189 int ret = -1;
190
191 inst = kzalloc(sizeof(*inst), GFP_KERNEL);
192 if (!inst)
193 return -ENOMEM;
194
195 inst->vdec.video_type = VFORMAT_MPEG12;
196 inst->vdec.dev = ctx->dev->vpu_plat_dev;
197 inst->vdec.filp = ctx->dev->filp;
198 inst->vdec.config = ctx->config;
199 inst->vdec.ctx = ctx;
200 inst->ctx = ctx;
201
202 vdec_parser_parms(inst);
203
204 /* set play mode.*/
205 if (ctx->is_drm_mode)
206 inst->vdec.port.flag |= PORT_FLAG_DRM;
207
208 /* to eable mpeg12 hw.*/
209 inst->vdec.port.type = PORT_TYPE_VIDEO;
210
211 /* init vfm */
212 inst->vfm.ctx = ctx;
213 inst->vfm.ada_ctx = &inst->vdec;
214 ret = vcodec_vfm_init(&inst->vfm);
215 if (ret) {
216 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
217 "init vfm failed.\n");
218 goto err;
219 }
220
221 ret = video_decoder_init(&inst->vdec);
222 if (ret) {
223 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
224 "vdec_mpeg12 init err=%d\n", ret);
225 goto err;
226 }
227
228 /* probe info from the stream */
229 inst->vsi = kzalloc(sizeof(struct vdec_mpeg12_vsi), GFP_KERNEL);
230 if (!inst->vsi) {
231 ret = -ENOMEM;
232 goto err;
233 }
234
235 /* alloc the header buffer to be used cache sps or spp etc.*/
236 inst->vsi->header_buf = kzalloc(HEADER_BUFFER_SIZE, GFP_KERNEL);
237 if (!inst->vsi->header_buf) {
238 ret = -ENOMEM;
239 goto err;
240 }
241
242 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_PRINFO,
243 "mpeg12 Instance >> %lx\n", (ulong) inst);
244 init_completion(&inst->comp);
245 ctx->ada_ctx = &inst->vdec;
246 *h_vdec = (unsigned long)inst;
247
248 //dump_init();
249
250 return 0;
251
252err:
253 if (inst)
254 vcodec_vfm_release(&inst->vfm);
255 if (inst && inst->vsi && inst->vsi->header_buf)
256 kfree(inst->vsi->header_buf);
257 if (inst && inst->vsi)
258 kfree(inst->vsi);
259 if (inst)
260 kfree(inst);
261 *h_vdec = 0;
262
263 return ret;
264}
265
266static void fill_vdec_params(struct vdec_mpeg12_inst *inst,
267 struct MpvParseContext *dec_ps)
268{
269 struct vdec_pic_info *pic = &inst->vsi->pic;
270 struct vdec_mpeg12_dec_info *dec = &inst->vsi->dec;
271 struct v4l2_rect *rect = &inst->vsi->crop;
272
273 /* fill visible area size that be used for EGL. */
274 pic->visible_width = dec_ps->width;
275 pic->visible_height = dec_ps->height;
276
277 /* calc visible ares. */
278 rect->left = 0;
279 rect->top = 0;
280 rect->width = pic->visible_width;
281 rect->height = pic->visible_height;
282
283 /* config canvas size that be used for decoder. */
284 pic->coded_width = ALIGN(dec_ps->coded_width, 64);
285 pic->coded_height = ALIGN(dec_ps->coded_height, 32);
286
287 pic->y_len_sz = pic->coded_width * pic->coded_height;
288 pic->c_len_sz = pic->y_len_sz >> 1;
289
290 /*7(parm_v4l_buffer_margin) + 8(DECODE_BUFFER_NUM_DEF)*/
291 dec->dpb_sz = 15;
292
293 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_BUFMGR,
294 "The stream infos, coded:(%d x %d), visible:(%d x %d), DPB: %d\n",
295 pic->coded_width, pic->coded_height,
296 pic->visible_width, pic->visible_height, dec->dpb_sz);
297}
298
299static int parse_stream_ucode(struct vdec_mpeg12_inst *inst, u8 *buf, u32 size)
300{
301 int ret = 0;
302 struct aml_vdec_adapt *vdec = &inst->vdec;
303
304 ret = vdec_vframe_write(vdec, buf, size, 0);
305 if (ret < 0) {
306 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
307 "write frame data failed. err: %d\n", ret);
308 return ret;
309 }
310
311 /* wait ucode parse ending. */
312 wait_for_completion_timeout(&inst->comp,
313 msecs_to_jiffies(1000));
314
315 return inst->vsi->dec.dpb_sz ? 0 : -1;
316}
317
318static int parse_stream_ucode_dma(struct vdec_mpeg12_inst *inst,
319 ulong buf, u32 size, u32 handle)
320{
321 int ret = 0;
322 struct aml_vdec_adapt *vdec = &inst->vdec;
323
324 ret = vdec_vframe_write_with_dma(vdec, buf, size, 0, handle);
325 if (ret < 0) {
326 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
327 "write frame data failed. err: %d\n", ret);
328 return ret;
329 }
330
331 /* wait ucode parse ending. */
332 wait_for_completion_timeout(&inst->comp,
333 msecs_to_jiffies(1000));
334
335 return inst->vsi->dec.dpb_sz ? 0 : -1;
336}
337
338static int parse_stream_cpu(struct vdec_mpeg12_inst *inst, u8 *buf, u32 size)
339{
340 int ret = 0;
341 struct mpeg12_param_sets *ps = NULL;
342
343 ps = kzalloc(sizeof(struct mpeg12_param_sets), GFP_KERNEL);
344 if (ps == NULL)
345 return -ENOMEM;
346
347 ret = mpeg12_decode_extradata_ps(buf, size, ps);
348 if (ret) {
349 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
350 "parse extra data failed. err: %d\n", ret);
351 goto out;
352 }
353
354 if (ps->head_parsed)
355 fill_vdec_params(inst, &ps->dec_ps);
356
357 ret = ps->head_parsed ? 0 : -1;
358out:
359 kfree(ps);
360
361 return ret;
362}
363
364static int vdec_mpeg12_probe(unsigned long h_vdec,
365 struct aml_vcodec_mem *bs, void *out)
366{
367 struct vdec_mpeg12_inst *inst =
368 (struct vdec_mpeg12_inst *)h_vdec;
369 u8 *buf = (u8 *)bs->vaddr;
370 u32 size = bs->size;
371 int ret = 0;
372
373 if (inst->ctx->is_drm_mode) {
374 if (bs->model == VB2_MEMORY_MMAP) {
375 struct aml_video_stream *s =
376 (struct aml_video_stream *) buf;
377
378 if ((s->magic != AML_VIDEO_MAGIC) &&
379 (s->type != V4L_STREAM_TYPE_MATEDATA))
380 return -1;
381
382 if (inst->ctx->param_sets_from_ucode) {
383 ret = parse_stream_ucode(inst, s->data, s->len);
384 } else {
385 ret = parse_stream_cpu(inst, s->data, s->len);
386 }
387 } else if (bs->model == VB2_MEMORY_DMABUF ||
388 bs->model == VB2_MEMORY_USERPTR) {
389 ret = parse_stream_ucode_dma(inst, bs->addr, size,
390 BUFF_IDX(bs, bs->index));
391 }
392 } else {
393 if (inst->ctx->param_sets_from_ucode) {
394 ret = parse_stream_ucode(inst, buf, size);
395 } else {
396 ret = parse_stream_cpu(inst, buf, size);
397 }
398 }
399
400 inst->vsi->cur_pic = inst->vsi->pic;
401
402 return ret;
403}
404
405static void vdec_mpeg12_deinit(unsigned long h_vdec)
406{
407 struct vdec_mpeg12_inst *inst = (struct vdec_mpeg12_inst *)h_vdec;
408
409 if (!inst)
410 return;
411
412 video_decoder_release(&inst->vdec);
413
414 vcodec_vfm_release(&inst->vfm);
415
416 //dump_deinit();
417
418 if (inst->vsi && inst->vsi->header_buf)
419 kfree(inst->vsi->header_buf);
420
421 if (inst->vsi)
422 kfree(inst->vsi);
423
424 kfree(inst);
425}
426
427static int vdec_mpeg12_get_fb(struct vdec_mpeg12_inst *inst, struct vdec_v4l2_buffer **out)
428{
429 return get_fb_from_queue(inst->ctx, out);
430}
431
432static void vdec_mpeg12_get_vf(struct vdec_mpeg12_inst *inst, struct vdec_v4l2_buffer **out)
433{
434 struct vframe_s *vf = NULL;
435 struct vdec_v4l2_buffer *fb = NULL;
436
437 vf = peek_video_frame(&inst->vfm);
438 if (!vf) {
439 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
440 "there is no vframe.\n");
441 *out = NULL;
442 return;
443 }
444
445 vf = get_video_frame(&inst->vfm);
446 if (!vf) {
447 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
448 "the vframe is avalid.\n");
449 *out = NULL;
450 return;
451 }
452
453 atomic_set(&vf->use_cnt, 1);
454
455 fb = (struct vdec_v4l2_buffer *)vf->v4l_mem_handle;
456 fb->vf_handle = (unsigned long)vf;
457 fb->status = FB_ST_DISPLAY;
458
459 *out = fb;
460
461 //pr_info("%s, %d\n", __func__, fb->base_y.bytes_used);
462 //dump_write(fb->base_y.va, fb->base_y.bytes_used);
463 //dump_write(fb->base_c.va, fb->base_c.bytes_used);
464
465 /* convert yuv format. */
466 //swap_uv(fb->base_c.va, fb->base_c.size);
467}
468
469static int vdec_write_nalu(struct vdec_mpeg12_inst *inst,
470 u8 *buf, u32 size, u64 ts)
471{
472 int ret = 0;
473 struct aml_vdec_adapt *vdec = &inst->vdec;
474
475 ret = vdec_vframe_write(vdec, buf, size, ts);
476
477 return ret;
478}
479
480static int vdec_mpeg12_decode(unsigned long h_vdec, struct aml_vcodec_mem *bs,
481 u64 timestamp, bool *res_chg)
482{
483 struct vdec_mpeg12_inst *inst = (struct vdec_mpeg12_inst *)h_vdec;
484 struct aml_vdec_adapt *vdec = &inst->vdec;
485 u8 *buf = (u8 *) bs->vaddr;
486 u32 size = bs->size;
487 int ret = -1;
488
489 if (vdec_input_full(vdec))
490 return -EAGAIN;
491
492 if (inst->ctx->is_drm_mode) {
493 if (bs->model == VB2_MEMORY_MMAP) {
494 struct aml_video_stream *s =
495 (struct aml_video_stream *) buf;
496
497 if (s->magic != AML_VIDEO_MAGIC)
498 return -1;
499
500 ret = vdec_vframe_write(vdec,
501 s->data,
502 s->len,
503 timestamp);
504 } else if (bs->model == VB2_MEMORY_DMABUF ||
505 bs->model == VB2_MEMORY_USERPTR) {
506 ret = vdec_vframe_write_with_dma(vdec,
507 bs->addr, size, timestamp,
508 BUFF_IDX(bs, bs->index));
509 }
510 } else {
511 ret = vdec_write_nalu(inst, buf, size, timestamp);
512 }
513
514 return ret;
515}
516
517static int vdec_mpeg12_get_param(unsigned long h_vdec,
518 enum vdec_get_param_type type, void *out)
519{
520 int ret = 0;
521 struct vdec_mpeg12_inst *inst = (struct vdec_mpeg12_inst *)h_vdec;
522
523 if (!inst) {
524 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
525 "the mpeg12 inst of dec is invalid.\n");
526 return -1;
527 }
528
529 switch (type) {
530 case GET_PARAM_DISP_FRAME_BUFFER:
531 vdec_mpeg12_get_vf(inst, out);
532 break;
533
534 case GET_PARAM_FREE_FRAME_BUFFER:
535 ret = vdec_mpeg12_get_fb(inst, out);
536 break;
537
538 case GET_PARAM_PIC_INFO:
539 get_pic_info(inst, out);
540 break;
541
542 case GET_PARAM_DPB_SIZE:
543 get_dpb_size(inst, out);
544 break;
545
546 case GET_PARAM_CROP_INFO:
547 get_crop_info(inst, out);
548 break;
549
550 default:
551 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
552 "invalid get parameter type=%d\n", type);
553 ret = -EINVAL;
554 }
555
556 return ret;
557}
558
559static void set_param_write_sync(struct vdec_mpeg12_inst *inst)
560{
561 complete(&inst->comp);
562}
563
564static void set_param_ps_info(struct vdec_mpeg12_inst *inst,
565 struct aml_vdec_ps_infos *ps)
566{
567 struct vdec_pic_info *pic = &inst->vsi->pic;
568 struct vdec_mpeg12_dec_info *dec = &inst->vsi->dec;
569 struct v4l2_rect *rect = &inst->vsi->crop;
570
571 /* fill visible area size that be used for EGL. */
572 pic->visible_width = ps->visible_width;
573 pic->visible_height = ps->visible_height;
574
575 /* calc visible ares. */
576 rect->left = 0;
577 rect->top = 0;
578 rect->width = pic->visible_width;
579 rect->height = pic->visible_height;
580
581 /* config canvas size that be used for decoder. */
582 pic->coded_width = ps->coded_width;
583 pic->coded_height = ps->coded_height;
584 pic->y_len_sz = pic->coded_width * pic->coded_height;
585 pic->c_len_sz = pic->y_len_sz >> 1;
586
587 dec->dpb_sz = ps->dpb_size;
588
589 inst->parms.ps = *ps;
590 inst->parms.parms_status |=
591 V4L2_CONFIG_PARM_DECODE_PSINFO;
592
593 /*wake up*/
594 complete(&inst->comp);
595
596 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_PRINFO,
597 "Parse from ucode, crop(%d x %d), coded(%d x %d) dpb: %d\n",
598 ps->visible_width, ps->visible_height,
599 ps->coded_width, ps->coded_height,
600 dec->dpb_sz);
601}
602
603static int vdec_mpeg12_set_param(unsigned long h_vdec,
604 enum vdec_set_param_type type, void *in)
605{
606 int ret = 0;
607 struct vdec_mpeg12_inst *inst = (struct vdec_mpeg12_inst *)h_vdec;
608
609 if (!inst) {
610 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
611 "the mpeg12 inst of dec is invalid.\n");
612 return -1;
613 }
614
615 switch (type) {
616 case SET_PARAM_WRITE_FRAME_SYNC:
617 set_param_write_sync(inst);
618 break;
619 case SET_PARAM_PS_INFO:
620 set_param_ps_info(inst, in);
621 break;
622
623 default:
624 v4l_dbg(inst->ctx, V4L_DEBUG_CODEC_ERROR,
625 "invalid set parameter type=%d\n", type);
626 ret = -EINVAL;
627 }
628
629 return ret;
630}
631
632static struct vdec_common_if vdec_mpeg12_if = {
633 .init = vdec_mpeg12_init,
634 .probe = vdec_mpeg12_probe,
635 .decode = vdec_mpeg12_decode,
636 .get_param = vdec_mpeg12_get_param,
637 .set_param = vdec_mpeg12_set_param,
638 .deinit = vdec_mpeg12_deinit,
639};
640
641struct vdec_common_if *get_mpeg12_dec_comm_if(void);
642
643struct vdec_common_if *get_mpeg12_dec_comm_if(void)
644{
645 return &vdec_mpeg12_if;
646}
647