blob: 7a40a4ac6a2f42c65147e2226e940aa81d5ecb7b
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_mpeg4_parser.h" |
33 | |
34 | #define NAL_TYPE(value) ((value) & 0x1F) |
35 | #define HEADER_BUFFER_SIZE (32 * 1024) |
36 | |
37 | /** |
38 | * struct mpeg4_fb - mpeg4 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 | */ |
45 | struct mpeg4_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_mpeg4_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 | */ |
63 | struct vdec_mpeg4_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_mpeg4_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 | */ |
88 | struct vdec_mpeg4_vsi { |
89 | char *header_buf; |
90 | int sps_size; |
91 | int pps_size; |
92 | int sei_size; |
93 | int head_offset; |
94 | struct vdec_mpeg4_dec_info dec; |
95 | struct vdec_pic_info pic; |
96 | struct v4l2_rect crop; |
97 | bool is_combine; |
98 | int nalu_pos; |
99 | //struct mpeg4ParamSets ps; |
100 | }; |
101 | |
102 | /** |
103 | * struct vdec_mpeg4_inst - mpeg4 decoder instance |
104 | * @num_nalu : how many nalus be decoded |
105 | * @ctx : point to aml_vcodec_ctx |
106 | * @vsi : VPU shared information |
107 | */ |
108 | struct vdec_mpeg4_inst { |
109 | unsigned int num_nalu; |
110 | struct aml_vcodec_ctx *ctx; |
111 | struct aml_vdec_adapt vdec; |
112 | struct vdec_mpeg4_vsi *vsi; |
113 | struct vcodec_vfm_s vfm; |
114 | }; |
115 | |
116 | static void get_pic_info(struct vdec_mpeg4_inst *inst, |
117 | struct vdec_pic_info *pic) |
118 | { |
119 | *pic = inst->vsi->pic; |
120 | |
121 | aml_vcodec_debug(inst, "pic(%d, %d), buf(%d, %d)", |
122 | pic->visible_width, pic->visible_height, |
123 | pic->coded_width, pic->coded_height); |
124 | aml_vcodec_debug(inst, "Y(%d, %d), C(%d, %d)", pic->y_bs_sz, |
125 | pic->y_len_sz, pic->c_bs_sz, pic->c_len_sz); |
126 | } |
127 | |
128 | static void get_crop_info(struct vdec_mpeg4_inst *inst, struct v4l2_rect *cr) |
129 | { |
130 | cr->left = inst->vsi->crop.left; |
131 | cr->top = inst->vsi->crop.top; |
132 | cr->width = inst->vsi->crop.width; |
133 | cr->height = inst->vsi->crop.height; |
134 | |
135 | aml_vcodec_debug(inst, "l=%d, t=%d, w=%d, h=%d", |
136 | cr->left, cr->top, cr->width, cr->height); |
137 | } |
138 | |
139 | static void get_dpb_size(struct vdec_mpeg4_inst *inst, unsigned int *dpb_sz) |
140 | { |
141 | *dpb_sz = 9;//inst->vsi->dec.dpb_sz; |
142 | aml_vcodec_debug(inst, "sz=%d", *dpb_sz); |
143 | } |
144 | |
145 | static int vdec_mpeg4_init(struct aml_vcodec_ctx *ctx, unsigned long *h_vdec) |
146 | { |
147 | struct vdec_mpeg4_inst *inst = NULL; |
148 | int ret = -1; |
149 | |
150 | inst = kzalloc(sizeof(*inst), GFP_KERNEL); |
151 | if (!inst) |
152 | return -ENOMEM; |
153 | |
154 | inst->ctx = ctx; |
155 | |
156 | inst->vdec.video_type = VFORMAT_MPEG4; |
157 | inst->vdec.format = VIDEO_DEC_FORMAT_MPEG4_5; |
158 | inst->vdec.dev = ctx->dev->vpu_plat_dev; |
159 | inst->vdec.filp = ctx->dev->filp; |
160 | inst->vdec.ctx = ctx; |
161 | |
162 | /* set play mode.*/ |
163 | if (ctx->is_drm_mode) |
164 | inst->vdec.port.flag |= PORT_FLAG_DRM; |
165 | |
166 | /* to eable mpeg4 hw.*/ |
167 | inst->vdec.port.type = PORT_TYPE_VIDEO; |
168 | |
169 | /* init vfm */ |
170 | inst->vfm.ctx = ctx; |
171 | inst->vfm.ada_ctx = &inst->vdec; |
172 | vcodec_vfm_init(&inst->vfm); |
173 | |
174 | ret = video_decoder_init(&inst->vdec); |
175 | if (ret) { |
176 | aml_vcodec_err(inst, "vdec_mpeg4 init err=%d", ret); |
177 | goto error_free_inst; |
178 | } |
179 | |
180 | /* probe info from the stream */ |
181 | inst->vsi = kzalloc(sizeof(struct vdec_mpeg4_vsi), GFP_KERNEL); |
182 | if (!inst->vsi) { |
183 | ret = -ENOMEM; |
184 | goto error_free_inst; |
185 | } |
186 | |
187 | /* alloc the header buffer to be used cache sps or spp etc.*/ |
188 | inst->vsi->header_buf = kzalloc(HEADER_BUFFER_SIZE, GFP_KERNEL); |
189 | if (!inst->vsi) { |
190 | ret = -ENOMEM; |
191 | goto error_free_vsi; |
192 | } |
193 | |
194 | inst->vsi->pic.visible_width = 1920; |
195 | inst->vsi->pic.visible_height = 1080; |
196 | inst->vsi->pic.coded_width = 1920; |
197 | inst->vsi->pic.coded_height = 1088; |
198 | inst->vsi->pic.y_bs_sz = 0; |
199 | inst->vsi->pic.y_len_sz = (1920 * 1088); |
200 | inst->vsi->pic.c_bs_sz = 0; |
201 | inst->vsi->pic.c_len_sz = (1920 * 1088 / 2); |
202 | |
203 | aml_vcodec_debug(inst, "mpeg4 Instance >> %p", inst); |
204 | |
205 | ctx->ada_ctx = &inst->vdec; |
206 | *h_vdec = (unsigned long)inst; |
207 | |
208 | //dump_init(); |
209 | |
210 | return 0; |
211 | |
212 | error_free_vsi: |
213 | kfree(inst->vsi); |
214 | error_free_inst: |
215 | kfree(inst); |
216 | *h_vdec = 0; |
217 | |
218 | return ret; |
219 | } |
220 | |
221 | #if 0 |
222 | static int refer_buffer_num(int level_idc, int poc_cnt, |
223 | int mb_width, int mb_height) |
224 | { |
225 | return 20; |
226 | } |
227 | #endif |
228 | |
229 | static void fill_vdec_params(struct vdec_mpeg4_inst *inst, |
230 | struct mpeg4_dec_param *dec_ps) |
231 | { |
232 | struct vdec_pic_info *pic = &inst->vsi->pic; |
233 | struct vdec_mpeg4_dec_info *dec = &inst->vsi->dec; |
234 | struct v4l2_rect *rect = &inst->vsi->crop; |
235 | |
236 | /* fill visible area size that be used for EGL. */ |
237 | pic->visible_width = dec_ps->m.width; |
238 | pic->visible_height = dec_ps->m.height; |
239 | |
240 | /* calc visible ares. */ |
241 | rect->left = 0; |
242 | rect->top = 0; |
243 | rect->width = pic->visible_width; |
244 | rect->height = pic->visible_height; |
245 | |
246 | /* config canvas size that be used for decoder. */ |
247 | pic->coded_width = ALIGN(dec_ps->m.width, 64); |
248 | pic->coded_height = ALIGN(dec_ps->m.height, 64); |
249 | |
250 | pic->y_len_sz = pic->coded_width * pic->coded_height; |
251 | pic->c_len_sz = pic->y_len_sz >> 1; |
252 | |
253 | /* calc DPB size */ |
254 | dec->dpb_sz = 9;//refer_buffer_num(sps->level_idc, poc_cnt, mb_w, mb_h); |
255 | |
256 | pr_info("[%d] The stream infos, coded:(%d x %d), visible:(%d x %d), DPB: %d\n", |
257 | inst->ctx->id, pic->coded_width, pic->coded_height, |
258 | pic->visible_width, pic->visible_height, dec->dpb_sz); |
259 | } |
260 | |
261 | static int stream_parse(struct vdec_mpeg4_inst *inst, u8 *buf, u32 size) |
262 | { |
263 | int ret = 0; |
264 | struct mpeg4_param_sets *ps = NULL; |
265 | |
266 | ps = kzalloc(sizeof(struct mpeg4_param_sets), GFP_KERNEL); |
267 | if (ps == NULL) |
268 | return -ENOMEM; |
269 | |
270 | ret = mpeg4_decode_extradata_ps(buf, size, ps); |
271 | if (ret) { |
272 | pr_err("parse extra data failed. err: %d\n", ret); |
273 | goto out; |
274 | } |
275 | |
276 | if (ps->head_parsed) |
277 | fill_vdec_params(inst, &ps->dec_ps); |
278 | |
279 | ret = ps->head_parsed ? 0 : -1; |
280 | out: |
281 | kfree(ps); |
282 | |
283 | return ret; |
284 | } |
285 | |
286 | static int vdec_mpeg4_probe(unsigned long h_vdec, |
287 | struct aml_vcodec_mem *bs, void *out) |
288 | { |
289 | struct vdec_mpeg4_inst *inst = |
290 | (struct vdec_mpeg4_inst *)h_vdec; |
291 | struct stream_info *st; |
292 | u8 *buf = (u8 *)bs->vaddr; |
293 | u32 size = bs->size; |
294 | int ret = 0; |
295 | |
296 | st = (struct stream_info *)buf; |
297 | if (inst->ctx->is_drm_mode && (st->magic == DRMe || st->magic == DRMn)) |
298 | return 0; |
299 | |
300 | if (st->magic == NORe || st->magic == NORn) |
301 | ret = stream_parse(inst, st->data, st->length); |
302 | else |
303 | ret = stream_parse(inst, buf, size); |
304 | |
305 | return ret; |
306 | } |
307 | |
308 | static void vdec_mpeg4_deinit(unsigned long h_vdec) |
309 | { |
310 | struct vdec_mpeg4_inst *inst = (struct vdec_mpeg4_inst *)h_vdec; |
311 | |
312 | if (!inst) |
313 | return; |
314 | |
315 | aml_vcodec_debug_enter(inst); |
316 | |
317 | video_decoder_release(&inst->vdec); |
318 | |
319 | vcodec_vfm_release(&inst->vfm); |
320 | |
321 | //dump_deinit(); |
322 | |
323 | if (inst->vsi && inst->vsi->header_buf) |
324 | kfree(inst->vsi->header_buf); |
325 | |
326 | if (inst->vsi) |
327 | kfree(inst->vsi); |
328 | |
329 | kfree(inst); |
330 | } |
331 | |
332 | static int vdec_mpeg4_get_fb(struct vdec_mpeg4_inst *inst, struct vdec_v4l2_buffer **out) |
333 | { |
334 | return get_fb_from_queue(inst->ctx, out); |
335 | } |
336 | |
337 | static void vdec_mpeg4_get_vf(struct vdec_mpeg4_inst *inst, struct vdec_v4l2_buffer **out) |
338 | { |
339 | struct vframe_s *vf = NULL; |
340 | struct vdec_v4l2_buffer *fb = NULL; |
341 | |
342 | vf = peek_video_frame(&inst->vfm); |
343 | if (!vf) { |
344 | aml_vcodec_debug(inst, "there is no vframe."); |
345 | *out = NULL; |
346 | return; |
347 | } |
348 | |
349 | vf = get_video_frame(&inst->vfm); |
350 | if (!vf) { |
351 | aml_vcodec_debug(inst, "the vframe is avalid."); |
352 | *out = NULL; |
353 | return; |
354 | } |
355 | |
356 | atomic_set(&vf->use_cnt, 1); |
357 | |
358 | fb = (struct vdec_v4l2_buffer *)vf->v4l_mem_handle; |
359 | fb->vf_handle = (unsigned long)vf; |
360 | fb->status = FB_ST_DISPLAY; |
361 | |
362 | *out = fb; |
363 | |
364 | //pr_info("%s, %d\n", __func__, fb->base_y.bytes_used); |
365 | //dump_write(fb->base_y.va, fb->base_y.bytes_used); |
366 | //dump_write(fb->base_c.va, fb->base_c.bytes_used); |
367 | |
368 | /* convert yuv format. */ |
369 | //swap_uv(fb->base_c.va, fb->base_c.size); |
370 | } |
371 | |
372 | static int vdec_write_nalu(struct vdec_mpeg4_inst *inst, |
373 | u8 *buf, u32 size, u64 ts) |
374 | { |
375 | int ret = 0; |
376 | struct aml_vdec_adapt *vdec = &inst->vdec; |
377 | |
378 | ret = vdec_vframe_write(vdec, buf, size, ts); |
379 | |
380 | return ret; |
381 | } |
382 | |
383 | static int vdec_mpeg4_decode(unsigned long h_vdec, struct aml_vcodec_mem *bs, |
384 | u64 timestamp, bool *res_chg) |
385 | { |
386 | struct vdec_mpeg4_inst *inst = (struct vdec_mpeg4_inst *)h_vdec; |
387 | struct aml_vdec_adapt *vdec = &inst->vdec; |
388 | struct stream_info *st; |
389 | u8 *buf; |
390 | u32 size; |
391 | int ret = 0; |
392 | |
393 | /* bs NULL means flush decoder */ |
394 | if (bs == NULL) |
395 | return 0; |
396 | |
397 | buf = (u8 *)bs->vaddr; |
398 | size = bs->size; |
399 | st = (struct stream_info *)buf; |
400 | |
401 | if (inst->ctx->is_drm_mode && (st->magic == DRMe || st->magic == DRMn)) |
402 | ret = vdec_vbuf_write(vdec, st->m.buf, sizeof(st->m.drm)); |
403 | else if (st->magic == NORe) |
404 | ret = vdec_vbuf_write(vdec, st->data, st->length); |
405 | else if (st->magic == NORn) |
406 | ret = vdec_write_nalu(inst, st->data, st->length, timestamp); |
407 | else if (inst->ctx->is_stream_mode) |
408 | ret = vdec_vbuf_write(vdec, buf, size); |
409 | else |
410 | ret = vdec_write_nalu(inst, buf, size, timestamp); |
411 | |
412 | return ret; |
413 | } |
414 | |
415 | static int vdec_mpeg4_get_param(unsigned long h_vdec, |
416 | enum vdec_get_param_type type, void *out) |
417 | { |
418 | int ret = 0; |
419 | struct vdec_mpeg4_inst *inst = (struct vdec_mpeg4_inst *)h_vdec; |
420 | |
421 | if (!inst) { |
422 | pr_err("the mpeg4 inst of dec is invalid.\n"); |
423 | return -1; |
424 | } |
425 | |
426 | switch (type) { |
427 | case GET_PARAM_DISP_FRAME_BUFFER: |
428 | vdec_mpeg4_get_vf(inst, out); |
429 | break; |
430 | |
431 | case GET_PARAM_FREE_FRAME_BUFFER: |
432 | ret = vdec_mpeg4_get_fb(inst, out); |
433 | break; |
434 | |
435 | case GET_PARAM_PIC_INFO: |
436 | get_pic_info(inst, out); |
437 | break; |
438 | |
439 | case GET_PARAM_DPB_SIZE: |
440 | get_dpb_size(inst, out); |
441 | break; |
442 | |
443 | case GET_PARAM_CROP_INFO: |
444 | get_crop_info(inst, out); |
445 | break; |
446 | |
447 | default: |
448 | aml_vcodec_err(inst, "invalid get parameter type=%d", type); |
449 | ret = -EINVAL; |
450 | } |
451 | |
452 | return ret; |
453 | } |
454 | |
455 | static void set_param_pic_info(struct vdec_mpeg4_inst *inst, |
456 | struct aml_vdec_pic_infos *info) |
457 | { |
458 | pr_info("---%s, %d\n", __func__, __LINE__); |
459 | } |
460 | |
461 | static int vdec_mpeg4_set_param(unsigned long h_vdec, |
462 | enum vdec_set_param_type type, void *in) |
463 | { |
464 | int ret = 0; |
465 | struct vdec_mpeg4_inst *inst = (struct vdec_mpeg4_inst *)h_vdec; |
466 | |
467 | if (!inst) { |
468 | pr_err("the mpeg4 inst of dec is invalid.\n"); |
469 | return -1; |
470 | } |
471 | |
472 | switch (type) { |
473 | case SET_PARAM_PIC_INFO: |
474 | set_param_pic_info(inst, in); |
475 | break; |
476 | |
477 | default: |
478 | aml_vcodec_err(inst, "invalid set parameter type=%d", type); |
479 | ret = -EINVAL; |
480 | } |
481 | |
482 | return ret; |
483 | } |
484 | |
485 | static struct vdec_common_if vdec_mpeg4_if = { |
486 | .init = vdec_mpeg4_init, |
487 | .probe = vdec_mpeg4_probe, |
488 | .decode = vdec_mpeg4_decode, |
489 | .get_param = vdec_mpeg4_get_param, |
490 | .set_param = vdec_mpeg4_set_param, |
491 | .deinit = vdec_mpeg4_deinit, |
492 | }; |
493 | |
494 | struct vdec_common_if *get_mpeg4_dec_comm_if(void); |
495 | |
496 | struct vdec_common_if *get_mpeg4_dec_comm_if(void) |
497 | { |
498 | return &vdec_mpeg4_if; |
499 | } |
500 |