blob: 30e40724eada5782c2dee59ee99b08ae14dcb73e
1 | /* |
2 | * drivers/amlogic/amports/vmpeg4.c |
3 | * |
4 | * Copyright (C) 2015 Amlogic, Inc. All rights reserved. |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, but WITHOUT |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
14 | * more details. |
15 | * |
16 | */ |
17 | |
18 | #include <linux/kernel.h> |
19 | #include <linux/module.h> |
20 | #include <linux/types.h> |
21 | #include <linux/errno.h> |
22 | #include <linux/interrupt.h> |
23 | #include <linux/timer.h> |
24 | #include <linux/kfifo.h> |
25 | #include <linux/platform_device.h> |
26 | #include <linux/dma-mapping.h> |
27 | #include <linux/amlogic/media/utils/amstream.h> |
28 | #include <linux/amlogic/media/frame_sync/ptsserv.h> |
29 | |
30 | #include <linux/amlogic/media/vfm/vframe.h> |
31 | #include <linux/amlogic/media/vfm/vframe_provider.h> |
32 | #include <linux/amlogic/media/vfm/vframe_receiver.h> |
33 | #include <linux/amlogic/media/canvas/canvas.h> |
34 | #include <linux/amlogic/media/codec_mm/codec_mm.h> |
35 | #include <linux/amlogic/tee.h> |
36 | #include <linux/amlogic/media/utils/vdec_reg.h> |
37 | #include <linux/amlogic/media/registers/register.h> |
38 | #include "../../../stream_input/amports/amports_priv.h" |
39 | |
40 | #include "../utils/amvdec.h" |
41 | #include "../utils/vdec_input.h" |
42 | #include "../utils/vdec.h" |
43 | #include "../utils/firmware.h" |
44 | #include "../utils/decoder_mmu_box.h" |
45 | #include "../utils/decoder_bmmu_box.h" |
46 | #include <linux/amlogic/media/codec_mm/configs.h> |
47 | #include "../utils/firmware.h" |
48 | #include "../utils/vdec_v4l2_buffer_ops.h" |
49 | |
50 | #define DRIVER_NAME "ammvdec_mpeg4" |
51 | #define MODULE_NAME "ammvdec_mpeg4" |
52 | |
53 | #define MEM_NAME "codec_mmpeg4" |
54 | |
55 | #define DEBUG_PTS |
56 | |
57 | #define NV21 |
58 | #define I_PICTURE 0 |
59 | #define P_PICTURE 1 |
60 | #define B_PICTURE 2 |
61 | #define GET_PIC_TYPE(type) ("IPB####"[type&0x3]) |
62 | |
63 | #define ORI_BUFFER_START_ADDR 0x01000000 |
64 | #define DEFAULT_MEM_SIZE (32*SZ_1M) |
65 | |
66 | #define INTERLACE_FLAG 0x80 |
67 | #define TOP_FIELD_FIRST_FLAG 0x40 |
68 | |
69 | /* protocol registers */ |
70 | #define MREG_REF0 AV_SCRATCH_1 |
71 | #define MREG_REF1 AV_SCRATCH_2 |
72 | #define MP4_PIC_RATIO AV_SCRATCH_5 |
73 | #define MP4_RATE AV_SCRATCH_3 |
74 | #define MP4_ERR_COUNT AV_SCRATCH_6 |
75 | #define MP4_PIC_WH AV_SCRATCH_7 |
76 | #define MREG_INPUT AV_SCRATCH_8 |
77 | #define MREG_BUFFEROUT AV_SCRATCH_9 |
78 | #define MP4_NOT_CODED_CNT AV_SCRATCH_A |
79 | #define MP4_VOP_TIME_INC AV_SCRATCH_B |
80 | #define MP4_OFFSET_REG AV_SCRATCH_C |
81 | #define MP4_SYS_RATE AV_SCRATCH_E |
82 | #define MEM_OFFSET_REG AV_SCRATCH_F |
83 | |
84 | #define PARC_FORBIDDEN 0 |
85 | #define PARC_SQUARE 1 |
86 | #define PARC_CIF 2 |
87 | #define PARC_10_11 3 |
88 | #define PARC_16_11 4 |
89 | #define PARC_40_33 5 |
90 | #define PARC_RESERVED 6 |
91 | /* values between 6 and 14 are reserved */ |
92 | #define PARC_EXTENDED 15 |
93 | |
94 | #define VF_POOL_SIZE 16 |
95 | #define DECODE_BUFFER_NUM_MAX 8 |
96 | #define PUT_INTERVAL (HZ/100) |
97 | #define MAX_BMMU_BUFFER_NUM (DECODE_BUFFER_NUM_MAX + 1) |
98 | #define WORKSPACE_SIZE (12*SZ_64K) |
99 | static u32 buf_size = 32 * 1024 * 1024; |
100 | |
101 | #define CTX_LMEM_SWAP_OFFSET 0 |
102 | #define CTX_QUANT_MATRIX_OFFSET 0x800 |
103 | /* dcac buffer must align at 4k boundary */ |
104 | #define CTX_DCAC_BUF_OFFSET 0x1000 |
105 | #define CTX_DECBUF_OFFSET (0x0c0000 + 0x1000) |
106 | |
107 | #define RATE_DETECT_COUNT 5 |
108 | #define DURATION_UNIT 96000 |
109 | #define PTS_UNIT 90000 |
110 | #define CHECK_INTERVAL (HZ/100) |
111 | |
112 | #define DUR2PTS(x) ((x) - ((x) >> 4)) |
113 | |
114 | /* 96000/(60fps* 2field) = 800, 96000/10fps = 9600 */ |
115 | #define MPEG4_VALID_DUR(x) ((x < 9600) && (x > 799)) |
116 | |
117 | #define MAX_MPEG4_SUPPORT_SIZE (1920*1088) |
118 | |
119 | #define DEC_RESULT_NONE 0 |
120 | #define DEC_RESULT_DONE 1 |
121 | #define DEC_RESULT_AGAIN 2 |
122 | #define DEC_RESULT_ERROR 3 |
123 | #define DEC_RESULT_FORCE_EXIT 4 |
124 | #define DEC_RESULT_EOS 5 |
125 | #define DEC_RESULT_UNFINISH 6 |
126 | |
127 | #define DEC_DECODE_TIMEOUT 0x21 |
128 | #define DECODE_ID(hw) (hw_to_vdec(hw)->id) |
129 | #define DECODE_STOP_POS AV_SCRATCH_K |
130 | static u32 udebug_flag; |
131 | |
132 | static struct vframe_s *vmpeg_vf_peek(void *); |
133 | static struct vframe_s *vmpeg_vf_get(void *); |
134 | static void vmpeg_vf_put(struct vframe_s *, void *); |
135 | static int vmpeg_vf_states(struct vframe_states *states, void *); |
136 | static int vmpeg_event_cb(int type, void *data, void *private_data); |
137 | static int pre_decode_buf_level = 0x800; |
138 | static int debug_enable; |
139 | static unsigned int radr; |
140 | static unsigned int rval; |
141 | /* 0x40bit = 8byte */ |
142 | static unsigned int frmbase_cont_bitlevel = 0x40; |
143 | |
144 | #define VMPEG4_DEV_NUM 9 |
145 | static unsigned int max_decode_instance_num = VMPEG4_DEV_NUM; |
146 | static unsigned int max_process_time[VMPEG4_DEV_NUM]; |
147 | static unsigned int decode_timeout_val = 100; |
148 | |
149 | static u32 without_display_mode; |
150 | |
151 | #undef pr_info |
152 | #define pr_info printk |
153 | unsigned int mpeg4_debug_mask = 0xff; |
154 | |
155 | #define PRINT_FLAG_ERROR 0x0 |
156 | #define PRINT_FLAG_RUN_FLOW 0X0001 |
157 | #define PRINT_FLAG_TIMEINFO 0x0002 |
158 | #define PRINT_FLAG_UCODE_DETAIL 0x0004 |
159 | #define PRINT_FLAG_VLD_DETAIL 0x0008 |
160 | #define PRINT_FLAG_DEC_DETAIL 0x0010 |
161 | #define PRINT_FLAG_BUFFER_DETAIL 0x0020 |
162 | #define PRINT_FLAG_RESTORE 0x0040 |
163 | #define PRINT_FRAME_NUM 0x0080 |
164 | #define PRINT_FLAG_FORCE_DONE 0x0100 |
165 | #define PRINT_FLAG_COUNTER 0X0200 |
166 | #define PRINT_FRAMEBASE_DATA 0x0400 |
167 | #define PRINT_FLAG_VDEC_STATUS 0x0800 |
168 | #define PRINT_FLAG_TIMEOUT_STATUS 0x1000 |
169 | #define PRINT_FLAG_V4L_DETAIL 0x8000 |
170 | |
171 | int mmpeg4_debug_print(int index, int debug_flag, const char *fmt, ...) |
172 | { |
173 | if (((debug_enable & debug_flag) && |
174 | ((1 << index) & mpeg4_debug_mask)) |
175 | || (debug_flag == PRINT_FLAG_ERROR)) { |
176 | unsigned char buf[512]; |
177 | int len = 0; |
178 | va_list args; |
179 | va_start(args, fmt); |
180 | len = sprintf(buf, "%d: ", index); |
181 | vsnprintf(buf + len, 512-len, fmt, args); |
182 | pr_info("%s", buf); |
183 | va_end(args); |
184 | } |
185 | return 0; |
186 | } |
187 | |
188 | struct pic_info_t { |
189 | int index; |
190 | u32 pic_type; |
191 | u32 pic_info; |
192 | u32 pts; |
193 | u64 pts64; |
194 | bool pts_valid; |
195 | u32 duration; |
196 | u32 repeat_cnt; |
197 | ulong v4l_ref_buf_addr; |
198 | }; |
199 | |
200 | struct vdec_mpeg4_hw_s { |
201 | spinlock_t lock; |
202 | struct platform_device *platform_dev; |
203 | /* struct device *cma_dev; */ |
204 | |
205 | DECLARE_KFIFO(newframe_q, struct vframe_s *, VF_POOL_SIZE); |
206 | DECLARE_KFIFO(display_q, struct vframe_s *, VF_POOL_SIZE); |
207 | struct vframe_s vfpool[VF_POOL_SIZE]; |
208 | |
209 | s32 vfbuf_use[DECODE_BUFFER_NUM_MAX]; |
210 | u32 frame_width; |
211 | u32 frame_height; |
212 | u32 frame_dur; |
213 | u32 frame_prog; |
214 | |
215 | u32 ctx_valid; |
216 | u32 reg_vcop_ctrl_reg; |
217 | u32 reg_pic_head_info; |
218 | u32 reg_mpeg1_2_reg; |
219 | u32 reg_slice_qp; |
220 | u32 reg_mp4_pic_wh; |
221 | u32 reg_mp4_rate; |
222 | u32 reg_mb_info; |
223 | u32 reg_dc_ac_ctrl; |
224 | u32 reg_iqidct_control; |
225 | u32 reg_resync_marker_length; |
226 | u32 reg_rv_ai_mb_count; |
227 | struct timer_list check_timer; |
228 | u32 decode_timeout_count; |
229 | u32 timeout_cnt; |
230 | unsigned long int start_process_time; |
231 | |
232 | u32 last_vld_level; |
233 | u8 init_flag; |
234 | u32 eos; |
235 | void *mm_blk_handle; |
236 | |
237 | struct vframe_chunk_s *chunk; |
238 | u32 chunk_offset; |
239 | u32 chunk_size; |
240 | u32 chunk_frame_count; |
241 | u32 stat; |
242 | unsigned long buf_start; |
243 | u32 buf_size; |
244 | /* |
245 | unsigned long cma_alloc_addr; |
246 | int cma_alloc_count; |
247 | */ |
248 | u32 vmpeg4_ratio; |
249 | u64 vmpeg4_ratio64; |
250 | u32 rate_detect; |
251 | u32 vmpeg4_rotation; |
252 | u32 total_frame; |
253 | u32 last_vop_time_inc; |
254 | u32 last_duration; |
255 | u32 last_anch_pts; |
256 | u32 vop_time_inc_since_last_anch; |
257 | u32 frame_num_since_last_anch; |
258 | u64 last_anch_pts_us64; |
259 | |
260 | u32 last_pts; |
261 | u64 last_pts64; |
262 | u32 pts_hit; |
263 | u32 pts_missed; |
264 | u32 pts_i_hit; |
265 | u32 pts_i_missed; |
266 | struct pic_info_t pic[DECODE_BUFFER_NUM_MAX]; |
267 | u32 canvas_spec[DECODE_BUFFER_NUM_MAX]; |
268 | #ifdef NV21 |
269 | struct canvas_config_s canvas_config[DECODE_BUFFER_NUM_MAX][2]; |
270 | #else |
271 | struct canvas_config_s canvas_config[DECODE_BUFFER_NUM_MAX][3]; |
272 | #endif |
273 | struct dec_sysinfo vmpeg4_amstream_dec_info; |
274 | |
275 | s32 refs[2]; |
276 | int dec_result; |
277 | struct work_struct work; |
278 | |
279 | void (*vdec_cb)(struct vdec_s *, void *); |
280 | void *vdec_cb_arg; |
281 | u32 frame_num; |
282 | u32 put_num; |
283 | u32 sys_mp4_rate; |
284 | u32 run_count; |
285 | u32 not_run_ready; |
286 | u32 buffer_not_ready; |
287 | u32 input_empty; |
288 | u32 peek_num; |
289 | u32 get_num; |
290 | u32 first_i_frame_ready; |
291 | u32 drop_frame_count; |
292 | u32 unstable_pts; |
293 | u32 last_dec_pts; |
294 | |
295 | struct firmware_s *fw; |
296 | u32 blkmode; |
297 | wait_queue_head_t wait_q; |
298 | bool is_used_v4l; |
299 | void *v4l2_ctx; |
300 | bool v4l_params_parsed; |
301 | }; |
302 | static void vmpeg4_local_init(struct vdec_mpeg4_hw_s *hw); |
303 | static int vmpeg4_hw_ctx_restore(struct vdec_mpeg4_hw_s *hw); |
304 | static unsigned char |
305 | get_data_check_sum(struct vdec_mpeg4_hw_s *hw, int size); |
306 | |
307 | #define PROVIDER_NAME "vdec.mpeg4" |
308 | |
309 | /* |
310 | *int query_video_status(int type, int *value); |
311 | */ |
312 | static const struct vframe_operations_s vf_provider_ops = { |
313 | .peek = vmpeg_vf_peek, |
314 | .get = vmpeg_vf_get, |
315 | .put = vmpeg_vf_put, |
316 | .event_cb = vmpeg_event_cb, |
317 | .vf_states = vmpeg_vf_states, |
318 | }; |
319 | |
320 | static unsigned char aspect_ratio_table[16] = { |
321 | PARC_FORBIDDEN, |
322 | PARC_SQUARE, |
323 | PARC_CIF, |
324 | PARC_10_11, |
325 | PARC_16_11, |
326 | PARC_40_33, |
327 | PARC_RESERVED, PARC_RESERVED, PARC_RESERVED, PARC_RESERVED, |
328 | PARC_RESERVED, PARC_RESERVED, PARC_RESERVED, PARC_RESERVED, |
329 | PARC_RESERVED, PARC_EXTENDED |
330 | }; |
331 | |
332 | static void reset_process_time(struct vdec_mpeg4_hw_s *hw); |
333 | |
334 | static int vmpeg4_v4l_alloc_buff_config_canvas(struct vdec_mpeg4_hw_s *hw, int i) |
335 | { |
336 | int ret; |
337 | u32 canvas; |
338 | ulong decbuf_start = 0; |
339 | int decbuf_y_size = 0; |
340 | u32 canvas_width = 0, canvas_height = 0; |
341 | struct vdec_s *vdec = hw_to_vdec(hw); |
342 | struct vdec_v4l2_buffer *fb = NULL; |
343 | |
344 | if (hw->pic[i].v4l_ref_buf_addr) |
345 | return 0; |
346 | |
347 | ret = vdec_v4l_get_buffer(hw->v4l2_ctx, &fb); |
348 | if (ret) { |
349 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
350 | "[%d] get fb fail.\n", |
351 | ((struct aml_vcodec_ctx *) |
352 | (hw->v4l2_ctx))->id); |
353 | return ret; |
354 | } |
355 | |
356 | hw->pic[i].v4l_ref_buf_addr = (ulong)fb; |
357 | if (fb->num_planes == 1) { |
358 | decbuf_start = fb->m.mem[0].addr; |
359 | decbuf_y_size = fb->m.mem[0].offset; |
360 | canvas_width = ALIGN(hw->frame_width, 64); |
361 | canvas_height = ALIGN(hw->frame_height, 32); |
362 | fb->m.mem[0].bytes_used = fb->m.mem[0].size; |
363 | } else if (fb->num_planes == 2) { |
364 | decbuf_start = fb->m.mem[0].addr; |
365 | decbuf_y_size = fb->m.mem[0].size; |
366 | canvas_width = ALIGN(hw->frame_width, 64); |
367 | canvas_height = ALIGN(hw->frame_height, 32); |
368 | fb->m.mem[0].bytes_used = decbuf_y_size; |
369 | fb->m.mem[1].bytes_used = decbuf_y_size << 1; |
370 | } |
371 | |
372 | mmpeg4_debug_print(DECODE_ID(hw), 0, "[%d] %s(), v4l ref buf addr: 0x%x\n", |
373 | ((struct aml_vcodec_ctx *)(hw->v4l2_ctx))->id, __func__, fb); |
374 | |
375 | if (vdec->parallel_dec == 1) { |
376 | u32 tmp; |
377 | if (canvas_y(hw->canvas_spec[i]) == 0xff) { |
378 | tmp = vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id); |
379 | hw->canvas_spec[i] &= ~0xff; |
380 | hw->canvas_spec[i] |= tmp; |
381 | } |
382 | if (canvas_u(hw->canvas_spec[i]) == 0xff) { |
383 | tmp = vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id); |
384 | hw->canvas_spec[i] &= ~(0xffff << 8); |
385 | hw->canvas_spec[i] |= tmp << 8; |
386 | hw->canvas_spec[i] |= tmp << 16; |
387 | } |
388 | canvas = hw->canvas_spec[i]; |
389 | } else { |
390 | canvas = vdec->get_canvas(i, 2); |
391 | hw->canvas_spec[i] = canvas; |
392 | } |
393 | |
394 | hw->canvas_config[i][0].phy_addr = decbuf_start; |
395 | hw->canvas_config[i][0].width = canvas_width; |
396 | hw->canvas_config[i][0].height = canvas_height; |
397 | hw->canvas_config[i][0].block_mode = hw->blkmode; |
398 | if (hw->blkmode == CANVAS_BLKMODE_LINEAR) |
399 | hw->canvas_config[i][0].endian = 7; |
400 | else |
401 | hw->canvas_config[i][0].endian = 0; |
402 | canvas_config_config(canvas_y(canvas), |
403 | &hw->canvas_config[i][0]); |
404 | |
405 | hw->canvas_config[i][1].phy_addr = |
406 | decbuf_start + decbuf_y_size; |
407 | hw->canvas_config[i][1].width = canvas_width; |
408 | hw->canvas_config[i][1].height = (canvas_height >> 1); |
409 | hw->canvas_config[i][1].block_mode = hw->blkmode; |
410 | if (hw->blkmode == CANVAS_BLKMODE_LINEAR) |
411 | hw->canvas_config[i][1].endian = 7; |
412 | else |
413 | hw->canvas_config[i][1].endian = 0; |
414 | canvas_config_config(canvas_u(canvas), |
415 | &hw->canvas_config[i][1]); |
416 | |
417 | return 0; |
418 | } |
419 | |
420 | static bool is_enough_free_buffer(struct vdec_mpeg4_hw_s *hw) |
421 | { |
422 | int i; |
423 | |
424 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) { |
425 | if (hw->vfbuf_use[i] == 0) |
426 | break; |
427 | } |
428 | |
429 | return i == DECODE_BUFFER_NUM_MAX ? false : true; |
430 | } |
431 | |
432 | static int find_free_buffer(struct vdec_mpeg4_hw_s *hw) |
433 | { |
434 | int i; |
435 | |
436 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) { |
437 | if (hw->vfbuf_use[i] == 0) |
438 | break; |
439 | } |
440 | |
441 | if (i == DECODE_BUFFER_NUM_MAX) |
442 | return -1; |
443 | |
444 | if (hw->is_used_v4l) |
445 | if (vmpeg4_v4l_alloc_buff_config_canvas(hw, i)) |
446 | return -1; |
447 | |
448 | return i; |
449 | } |
450 | |
451 | static int spec_to_index(struct vdec_mpeg4_hw_s *hw, u32 spec) |
452 | { |
453 | int i; |
454 | |
455 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) { |
456 | if (hw->canvas_spec[i] == spec) |
457 | return i; |
458 | } |
459 | |
460 | return -1; |
461 | } |
462 | |
463 | static void set_frame_info(struct vdec_mpeg4_hw_s *hw, struct vframe_s *vf, |
464 | int buffer_index) |
465 | { |
466 | int ar = 0; |
467 | unsigned int num = 0; |
468 | unsigned int den = 0; |
469 | unsigned int pixel_ratio = READ_VREG(MP4_PIC_RATIO); |
470 | |
471 | if (hw->vmpeg4_ratio64 != 0) { |
472 | num = hw->vmpeg4_ratio64>>32; |
473 | den = hw->vmpeg4_ratio64 & 0xffffffff; |
474 | } else { |
475 | num = hw->vmpeg4_ratio>>16; |
476 | den = hw->vmpeg4_ratio & 0xffff; |
477 | |
478 | } |
479 | if ((num == 0) || (den == 0)) { |
480 | num = 1; |
481 | den = 1; |
482 | } |
483 | |
484 | if (hw->vmpeg4_ratio == 0) { |
485 | vf->ratio_control |= (0x90 << DISP_RATIO_ASPECT_RATIO_BIT); |
486 | /* always stretch to 16:9 */ |
487 | } else if (pixel_ratio > 0x0f) { |
488 | num = (pixel_ratio >> 8) * |
489 | hw->frame_width * num; |
490 | ar = div_u64((pixel_ratio & 0xff) * |
491 | hw->frame_height * den * 0x100ULL + |
492 | (num >> 1), num); |
493 | } else { |
494 | switch (aspect_ratio_table[pixel_ratio]) { |
495 | case 0: |
496 | num = hw->frame_width * num; |
497 | ar = (hw->frame_height * den * |
498 | 0x100 + (num >> 1)) / num; |
499 | break; |
500 | case 1: |
501 | num = vf->width * num; |
502 | ar = (vf->height * den * 0x100 + (num >> 1)) / num; |
503 | break; |
504 | case 2: |
505 | num = (vf->width * 12) * num; |
506 | ar = (vf->height * den * 0x100 * 11 + |
507 | ((num) >> 1)) / num; |
508 | break; |
509 | case 3: |
510 | num = (vf->width * 10) * num; |
511 | ar = (vf->height * den * 0x100 * 11 + (num >> 1)) / |
512 | num; |
513 | break; |
514 | case 4: |
515 | num = (vf->width * 16) * num; |
516 | ar = (vf->height * den * 0x100 * 11 + (num >> 1)) / |
517 | num; |
518 | break; |
519 | case 5: |
520 | num = (vf->width * 40) * num; |
521 | ar = (vf->height * den * 0x100 * 33 + (num >> 1)) / |
522 | num; |
523 | break; |
524 | default: |
525 | num = vf->width * num; |
526 | ar = (vf->height * den * 0x100 + (num >> 1)) / num; |
527 | break; |
528 | } |
529 | } |
530 | |
531 | ar = min(ar, DISP_RATIO_ASPECT_RATIO_MAX); |
532 | |
533 | vf->signal_type = 0; |
534 | vf->type_original = vf->type; |
535 | vf->ratio_control = (ar << DISP_RATIO_ASPECT_RATIO_BIT); |
536 | vf->canvas0Addr = vf->canvas1Addr = -1; |
537 | #ifdef NV21 |
538 | vf->plane_num = 2; |
539 | #else |
540 | vf->plane_num = 3; |
541 | #endif |
542 | vf->canvas0_config[0] = hw->canvas_config[buffer_index][0]; |
543 | vf->canvas0_config[1] = hw->canvas_config[buffer_index][1]; |
544 | #ifndef NV21 |
545 | vf->canvas0_config[2] = hw->canvas_config[buffer_index][2]; |
546 | #endif |
547 | vf->canvas1_config[0] = hw->canvas_config[buffer_index][0]; |
548 | vf->canvas1_config[1] = hw->canvas_config[buffer_index][1]; |
549 | #ifndef NV21 |
550 | vf->canvas1_config[2] = hw->canvas_config[buffer_index][2]; |
551 | #endif |
552 | } |
553 | |
554 | static inline void vmpeg4_save_hw_context(struct vdec_mpeg4_hw_s *hw) |
555 | { |
556 | hw->reg_mpeg1_2_reg = READ_VREG(MPEG1_2_REG); |
557 | hw->reg_vcop_ctrl_reg = READ_VREG(VCOP_CTRL_REG); |
558 | hw->reg_pic_head_info = READ_VREG(PIC_HEAD_INFO); |
559 | hw->reg_slice_qp = READ_VREG(SLICE_QP); |
560 | hw->reg_mp4_pic_wh = READ_VREG(MP4_PIC_WH); |
561 | hw->reg_mp4_rate = READ_VREG(MP4_RATE); |
562 | hw->reg_mb_info = READ_VREG(MB_INFO); |
563 | hw->reg_dc_ac_ctrl = READ_VREG(DC_AC_CTRL); |
564 | hw->reg_iqidct_control = READ_VREG(IQIDCT_CONTROL); |
565 | hw->reg_resync_marker_length = READ_VREG(RESYNC_MARKER_LENGTH); |
566 | hw->reg_rv_ai_mb_count = READ_VREG(RV_AI_MB_COUNT); |
567 | } |
568 | |
569 | static int update_ref(struct vdec_mpeg4_hw_s *hw, int index) |
570 | { |
571 | hw->vfbuf_use[index]++; |
572 | |
573 | if (hw->refs[1] == -1) { |
574 | hw->refs[1] = index; |
575 | index = -1; |
576 | } else if (hw->refs[0] == -1) { |
577 | hw->refs[0] = hw->refs[1]; |
578 | hw->refs[1] = index; |
579 | index = hw->refs[0]; |
580 | } else { |
581 | hw->vfbuf_use[hw->refs[0]]--; |
582 | hw->refs[0] = hw->refs[1]; |
583 | hw->refs[1] = index; |
584 | index = hw->refs[0]; |
585 | } |
586 | |
587 | return index; |
588 | } |
589 | |
590 | static int prepare_display_buf(struct vdec_mpeg4_hw_s * hw, |
591 | struct pic_info_t *pic) |
592 | { |
593 | struct vframe_s *vf = NULL; |
594 | struct vdec_s *vdec = hw_to_vdec(hw); |
595 | int index = pic->index; |
596 | |
597 | if (pic->pic_info & INTERLACE_FLAG) { |
598 | if (kfifo_get(&hw->newframe_q, &vf) == 0) { |
599 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
600 | "fatal error, no available buffer slot."); |
601 | return -1; |
602 | } |
603 | |
604 | if (hw->is_used_v4l) { |
605 | vf->v4l_mem_handle |
606 | = hw->pic[index].v4l_ref_buf_addr; |
607 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_V4L_DETAIL, |
608 | "[%d] %s(), v4l mem handle: 0x%lx\n", |
609 | ((struct aml_vcodec_ctx *)(hw->v4l2_ctx))->id, |
610 | __func__, vf->v4l_mem_handle); |
611 | } |
612 | |
613 | vf->index = pic->index; |
614 | vf->width = hw->frame_width; |
615 | vf->height = hw->frame_height; |
616 | vf->bufWidth = 1920; |
617 | vf->flag = 0; |
618 | vf->orientation = hw->vmpeg4_rotation; |
619 | vf->pts = pic->pts; |
620 | vf->pts_us64 = pic->pts64; |
621 | vf->duration = pic->duration >> 1; |
622 | vf->duration_pulldown = 0; |
623 | vf->type = (pic->pic_info & TOP_FIELD_FIRST_FLAG) ? |
624 | VIDTYPE_INTERLACE_TOP : VIDTYPE_INTERLACE_BOTTOM; |
625 | #ifdef NV21 |
626 | vf->type |= VIDTYPE_VIU_NV21; |
627 | #endif |
628 | set_frame_info(hw, vf, pic->index); |
629 | |
630 | hw->vfbuf_use[pic->index]++; |
631 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO, |
632 | "field0: pts %d, pts64 %lld, w %d, h %d, dur %d\n", |
633 | vf->pts, vf->pts_us64, vf->width, vf->height, vf->duration); |
634 | |
635 | if ((hw->first_i_frame_ready == 0) |
636 | && (pic->pic_type != I_PICTURE)) { |
637 | hw->drop_frame_count++; |
638 | hw->vfbuf_use[index]--; |
639 | kfifo_put(&hw->newframe_q, |
640 | (const struct vframe_s *)vf); |
641 | return 0; |
642 | } else { |
643 | vf->mem_handle = |
644 | decoder_bmmu_box_get_mem_handle( |
645 | hw->mm_blk_handle, index); |
646 | kfifo_put(&hw->display_q, |
647 | (const struct vframe_s *)vf); |
648 | ATRACE_COUNTER(MODULE_NAME, vf->pts); |
649 | vdec->vdec_fps_detec(vdec->id); |
650 | hw->frame_num++; |
651 | if (without_display_mode == 0) { |
652 | vf_notify_receiver(vdec->vf_provider_name, |
653 | VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL); |
654 | } else |
655 | vmpeg_vf_put(vmpeg_vf_get(vdec), vdec); |
656 | } |
657 | |
658 | if (kfifo_get(&hw->newframe_q, &vf) == 0) { |
659 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
660 | "error, no available buf.\n"); |
661 | hw->dec_result = DEC_RESULT_ERROR; |
662 | return -1; |
663 | } |
664 | |
665 | vf->index = pic->index; |
666 | vf->width = hw->frame_width; |
667 | vf->height = hw->frame_height; |
668 | vf->bufWidth = 1920; |
669 | vf->flag = 0; |
670 | vf->orientation = hw->vmpeg4_rotation; |
671 | vf->pts = 0; |
672 | vf->pts_us64 = 0; |
673 | vf->duration = pic->duration >> 1; |
674 | vf->duration_pulldown = 0; |
675 | vf->type = (pic->pic_info & TOP_FIELD_FIRST_FLAG) ? |
676 | VIDTYPE_INTERLACE_BOTTOM : VIDTYPE_INTERLACE_TOP; |
677 | #ifdef NV21 |
678 | vf->type |= VIDTYPE_VIU_NV21; |
679 | #endif |
680 | set_frame_info(hw, vf, pic->index); |
681 | |
682 | hw->vfbuf_use[pic->index]++; |
683 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO, |
684 | "filed1: pts %d, pts64 %lld, w %d, h %d, dur: %d\n", |
685 | vf->pts, vf->pts_us64, vf->width, vf->height, vf->duration); |
686 | if ((hw->first_i_frame_ready == 0) && |
687 | (pic->pic_type != I_PICTURE)) { |
688 | hw->drop_frame_count++; |
689 | hw->vfbuf_use[index]--; |
690 | kfifo_put(&hw->newframe_q, |
691 | (const struct vframe_s *)vf); |
692 | } else { |
693 | vf->mem_handle = |
694 | decoder_bmmu_box_get_mem_handle( |
695 | hw->mm_blk_handle, index); |
696 | kfifo_put(&hw->display_q, |
697 | (const struct vframe_s *)vf); |
698 | ATRACE_COUNTER(MODULE_NAME, vf->pts); |
699 | vdec->vdec_fps_detec(vdec->id); |
700 | decoder_do_frame_check(vdec, vf); |
701 | hw->frame_num++; |
702 | if (without_display_mode == 0) { |
703 | vf_notify_receiver(vdec->vf_provider_name, |
704 | VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL); |
705 | } else |
706 | vmpeg_vf_put(vmpeg_vf_get(vdec), vdec); |
707 | } |
708 | } else { |
709 | /* progressive */ |
710 | if (kfifo_get(&hw->newframe_q, &vf) == 0) { |
711 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
712 | "error, no available buf\n"); |
713 | hw->dec_result = DEC_RESULT_ERROR; |
714 | return -1; |
715 | } |
716 | |
717 | if (hw->is_used_v4l) { |
718 | vf->v4l_mem_handle |
719 | = hw->pic[index].v4l_ref_buf_addr; |
720 | if (vdec_v4l_binding_fd_and_vf(vf->v4l_mem_handle, vf) < 0) { |
721 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_V4L_DETAIL, |
722 | "v4l: binding vf fail.\n"); |
723 | return -1; |
724 | } |
725 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_V4L_DETAIL, |
726 | "[%d] %s(), v4l mem handle: 0x%lx\n", |
727 | ((struct aml_vcodec_ctx *)(hw->v4l2_ctx))->id, |
728 | __func__, vf->v4l_mem_handle); |
729 | } |
730 | |
731 | vf->index = index; |
732 | vf->width = hw->frame_width; |
733 | vf->height = hw->frame_height; |
734 | vf->bufWidth = 1920; |
735 | vf->flag = 0; |
736 | vf->orientation = hw->vmpeg4_rotation; |
737 | vf->pts = pic->pts; |
738 | vf->pts_us64 = pic->pts64; |
739 | vf->duration = pic->duration; |
740 | vf->duration_pulldown = pic->repeat_cnt * |
741 | pic->duration; |
742 | #ifdef NV21 |
743 | vf->type = VIDTYPE_PROGRESSIVE | |
744 | VIDTYPE_VIU_FIELD | VIDTYPE_VIU_NV21; |
745 | #else |
746 | vf->type = VIDTYPE_PROGRESSIVE | |
747 | VIDTYPE_VIU_FIELD; |
748 | #endif |
749 | set_frame_info(hw, vf, index); |
750 | |
751 | hw->vfbuf_use[index]++; |
752 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO, |
753 | "prog: pts %d, pts64 %lld, w %d, h %d, dur %d\n", |
754 | vf->pts, vf->pts_us64, vf->width, vf->height, vf->duration); |
755 | |
756 | if ((hw->first_i_frame_ready == 0) && |
757 | (pic->pic_type != I_PICTURE)) { |
758 | hw->drop_frame_count++; |
759 | hw->vfbuf_use[index]--; |
760 | kfifo_put(&hw->newframe_q, |
761 | (const struct vframe_s *)vf); |
762 | } else { |
763 | vf->mem_handle = |
764 | decoder_bmmu_box_get_mem_handle( |
765 | hw->mm_blk_handle, index); |
766 | kfifo_put(&hw->display_q, |
767 | (const struct vframe_s *)vf); |
768 | ATRACE_COUNTER(MODULE_NAME, vf->pts); |
769 | vdec->vdec_fps_detec(vdec->id); |
770 | decoder_do_frame_check(vdec, vf); |
771 | hw->frame_num++; |
772 | |
773 | if (without_display_mode == 0) { |
774 | vf_notify_receiver(vdec->vf_provider_name, |
775 | VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL); |
776 | } else |
777 | vmpeg_vf_put(vmpeg_vf_get(vdec), vdec); |
778 | } |
779 | |
780 | } |
781 | return 0; |
782 | } |
783 | |
784 | static void vmpeg4_prepare_input(struct vdec_mpeg4_hw_s *hw) |
785 | { |
786 | struct vdec_s *vdec = hw_to_vdec(hw); |
787 | struct vdec_input_s *input = &vdec->input; |
788 | struct vframe_block_list_s *block = NULL; |
789 | struct vframe_chunk_s *chunk = hw->chunk; |
790 | int dummy; |
791 | |
792 | if (chunk == NULL) |
793 | return; |
794 | |
795 | /* full reset to HW input */ |
796 | WRITE_VREG(VLD_MEM_VIFIFO_CONTROL, 0); |
797 | |
798 | /* reset VLD fifo for all vdec */ |
799 | WRITE_VREG(DOS_SW_RESET0, (1<<5) | (1<<4) | (1<<3)); |
800 | WRITE_VREG(DOS_SW_RESET0, 0); |
801 | |
802 | dummy = READ_RESET_REG(RESET0_REGISTER); |
803 | WRITE_VREG(POWER_CTL_VLD, 1 << 4); |
804 | |
805 | /* |
806 | *setup HW decoder input buffer (VLD context) |
807 | * based on input->type and input->target |
808 | */ |
809 | if (input_frame_based(input)) { |
810 | block = chunk->block; |
811 | |
812 | WRITE_VREG(VLD_MEM_VIFIFO_START_PTR, block->start); |
813 | WRITE_VREG(VLD_MEM_VIFIFO_END_PTR, block->start + |
814 | block->size - 8); |
815 | WRITE_VREG(VLD_MEM_VIFIFO_CURR_PTR, |
816 | round_down(block->start + hw->chunk_offset, |
817 | VDEC_FIFO_ALIGN)); |
818 | |
819 | WRITE_VREG(VLD_MEM_VIFIFO_CONTROL, 1); |
820 | WRITE_VREG(VLD_MEM_VIFIFO_CONTROL, 0); |
821 | |
822 | /* set to manual mode */ |
823 | WRITE_VREG(VLD_MEM_VIFIFO_BUF_CNTL, 2); |
824 | WRITE_VREG(VLD_MEM_VIFIFO_RP, |
825 | round_down(block->start + hw->chunk_offset, |
826 | VDEC_FIFO_ALIGN)); |
827 | dummy = hw->chunk_offset + hw->chunk_size + |
828 | VLD_PADDING_SIZE; |
829 | if (dummy >= block->size) |
830 | dummy -= block->size; |
831 | WRITE_VREG(VLD_MEM_VIFIFO_WP, |
832 | round_down(block->start + dummy, |
833 | VDEC_FIFO_ALIGN)); |
834 | |
835 | WRITE_VREG(VLD_MEM_VIFIFO_BUF_CNTL, 3); |
836 | WRITE_VREG(VLD_MEM_VIFIFO_BUF_CNTL, 2); |
837 | |
838 | WRITE_VREG(VLD_MEM_VIFIFO_CONTROL, |
839 | (0x11 << 16) | (1<<10) | (7<<3)); |
840 | |
841 | } |
842 | } |
843 | |
844 | static irqreturn_t vmpeg4_isr_thread_fn(struct vdec_s *vdec, int irq) |
845 | { |
846 | u32 reg; |
847 | u32 picture_type; |
848 | int index; |
849 | u32 pts, offset = 0; |
850 | u64 pts_us64 = 0; |
851 | u32 frame_size, dec_w, dec_h; |
852 | u32 time_increment_resolution, fixed_vop_rate, vop_time_inc; |
853 | u32 repeat_cnt, duration = 3200; |
854 | struct pic_info_t *dec_pic, *disp_pic; |
855 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)(vdec->private); |
856 | |
857 | if (hw->eos) |
858 | return IRQ_HANDLED; |
859 | |
860 | WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1); |
861 | if (READ_VREG(AV_SCRATCH_M) != 0 && |
862 | (debug_enable & PRINT_FLAG_UCODE_DETAIL)) { |
863 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_UCODE_DETAIL, |
864 | "dbg %x: %x, level %x, wp %x, rp %x, cnt %x\n", |
865 | READ_VREG(AV_SCRATCH_M), READ_VREG(AV_SCRATCH_N), |
866 | READ_VREG(VLD_MEM_VIFIFO_LEVEL), |
867 | READ_VREG(VLD_MEM_VIFIFO_WP), |
868 | READ_VREG(VLD_MEM_VIFIFO_RP), |
869 | READ_VREG(VIFF_BIT_CNT)); |
870 | WRITE_VREG(AV_SCRATCH_M, 0); |
871 | return IRQ_HANDLED; |
872 | } |
873 | reg = READ_VREG(MREG_BUFFEROUT); |
874 | |
875 | time_increment_resolution = READ_VREG(MP4_RATE); |
876 | fixed_vop_rate = time_increment_resolution >> 16; |
877 | time_increment_resolution &= 0xffff; |
878 | if (time_increment_resolution > 0 && |
879 | fixed_vop_rate == 0) |
880 | hw->sys_mp4_rate = time_increment_resolution; |
881 | |
882 | if (hw->vmpeg4_amstream_dec_info.rate == 0) { |
883 | if ((fixed_vop_rate != 0) && |
884 | (time_increment_resolution != 0)) { |
885 | hw->vmpeg4_amstream_dec_info.rate = fixed_vop_rate * |
886 | DURATION_UNIT / time_increment_resolution; |
887 | } else if (time_increment_resolution == 0 |
888 | && hw->sys_mp4_rate > 0) |
889 | time_increment_resolution = hw->sys_mp4_rate; |
890 | } |
891 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_DEC_DETAIL, |
892 | "time_inc_res = %d, fixed_vop_rate = %d, rate = %d\n", |
893 | time_increment_resolution, fixed_vop_rate, |
894 | hw->vmpeg4_amstream_dec_info.rate); |
895 | |
896 | if (reg == 2) { |
897 | /* timeout when decoding next frame */ |
898 | |
899 | /* for frame based case, insufficient result may happen |
900 | * at the beginning when only VOL head is available save |
901 | * HW context also, such as for the QTable from VCOP register |
902 | */ |
903 | mmpeg4_debug_print(DECODE_ID(hw), |
904 | PRINT_FLAG_VLD_DETAIL, |
905 | "%s, level = %x, vfifo_ctrl = %x, bitcnt = %d\n", |
906 | __func__, |
907 | READ_VREG(VLD_MEM_VIFIFO_LEVEL), |
908 | READ_VREG(VLD_MEM_VIFIFO_CONTROL), |
909 | READ_VREG(VIFF_BIT_CNT)); |
910 | |
911 | if (vdec_frame_based(vdec)) { |
912 | //vmpeg4_save_hw_context(hw); |
913 | hw->dec_result = DEC_RESULT_DONE; |
914 | vdec_schedule_work(&hw->work); |
915 | } else { |
916 | reset_process_time(hw); |
917 | hw->dec_result = DEC_RESULT_AGAIN; |
918 | vdec_schedule_work(&hw->work); |
919 | } |
920 | return IRQ_HANDLED; |
921 | } else { |
922 | reset_process_time(hw); |
923 | picture_type = (reg >> 3) & 7; |
924 | repeat_cnt = READ_VREG(MP4_NOT_CODED_CNT); |
925 | vop_time_inc = READ_VREG(MP4_VOP_TIME_INC); |
926 | |
927 | index = spec_to_index(hw, READ_VREG(REC_CANVAS_ADDR)); |
928 | if (index < 0) { |
929 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
930 | "invalid buffer index %d. rec = %x\n", |
931 | index, READ_VREG(REC_CANVAS_ADDR)); |
932 | hw->dec_result = DEC_RESULT_ERROR; |
933 | vdec_schedule_work(&hw->work); |
934 | return IRQ_HANDLED; |
935 | } |
936 | hw->dec_result = DEC_RESULT_DONE; |
937 | dec_pic = &hw->pic[index]; |
938 | dec_pic->pts_valid = false; |
939 | dec_pic->pts = 0; |
940 | dec_pic->pts64 = 0; |
941 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_BUFFER_DETAIL, |
942 | "new pic: index=%d, used=%d, repeat=%d, time_inc=%d\n", |
943 | index, hw->vfbuf_use[index], repeat_cnt, vop_time_inc); |
944 | |
945 | dec_w = READ_VREG(MP4_PIC_WH)>> 16; |
946 | dec_h = READ_VREG(MP4_PIC_WH) & 0xffff; |
947 | if (dec_w != 0) |
948 | hw->frame_width = dec_w; |
949 | if (dec_h != 0) |
950 | hw->frame_height = dec_h; |
951 | |
952 | if (hw->is_used_v4l) { |
953 | struct aml_vcodec_ctx *ctx = |
954 | (struct aml_vcodec_ctx *)(hw->v4l2_ctx); |
955 | |
956 | if (ctx->param_sets_from_ucode && !hw->v4l_params_parsed) { |
957 | struct aml_vdec_pic_infos info; |
958 | |
959 | info.visible_width = hw->frame_width; |
960 | info.visible_height = hw->frame_height; |
961 | info.coded_width = ALIGN(hw->frame_width, 64); |
962 | info.coded_height = ALIGN(hw->frame_height, 64); |
963 | info.dpb_size = MAX_BMMU_BUFFER_NUM - 1; |
964 | hw->v4l_params_parsed = true; |
965 | vdec_v4l_set_pic_infos(ctx, &info); |
966 | } |
967 | |
968 | if (!ctx->v4l_codec_ready) |
969 | return IRQ_HANDLED; |
970 | } |
971 | |
972 | if (hw->vmpeg4_amstream_dec_info.rate == 0) { |
973 | if (vop_time_inc < hw->last_vop_time_inc) { |
974 | duration = vop_time_inc + |
975 | time_increment_resolution - |
976 | hw->last_vop_time_inc; |
977 | } else { |
978 | duration = vop_time_inc - |
979 | hw->last_vop_time_inc; |
980 | } |
981 | |
982 | if (duration == hw->last_duration) { |
983 | hw->rate_detect++; |
984 | if ((hw->rate_detect >= RATE_DETECT_COUNT) && |
985 | (time_increment_resolution != 0)) { |
986 | hw->vmpeg4_amstream_dec_info.rate = |
987 | duration * DURATION_UNIT / |
988 | time_increment_resolution; |
989 | duration = |
990 | hw->vmpeg4_amstream_dec_info.rate; |
991 | } |
992 | } else { |
993 | hw->rate_detect = 0; |
994 | hw->last_duration = duration; |
995 | } |
996 | if (MPEG4_VALID_DUR(duration)) { |
997 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO, |
998 | "warn: duration %x, set 0\n", duration); |
999 | duration = 0; |
1000 | } |
1001 | } else { |
1002 | duration = hw->vmpeg4_amstream_dec_info.rate; |
1003 | #if 0 |
1004 | pr_info("info rate = %d, ucode rate = 0x%x:0x%x\n", |
1005 | hw->vmpeg4_amstream_dec_info.rate, |
1006 | READ_VREG(MP4_RATE), vop_time_inc); |
1007 | #endif |
1008 | } |
1009 | |
1010 | /* frame mode with unstable pts */ |
1011 | if (hw->unstable_pts && hw->chunk) { |
1012 | dec_pic->pts_valid = hw->chunk->pts_valid; |
1013 | dec_pic->pts = hw->chunk->pts; |
1014 | dec_pic->pts64 = hw->chunk->pts64; |
1015 | if ((B_PICTURE == picture_type) || |
1016 | (hw->last_dec_pts == dec_pic->pts)) |
1017 | dec_pic->pts_valid = 0; |
1018 | |
1019 | hw->last_dec_pts = dec_pic->pts; |
1020 | } else if ((I_PICTURE == picture_type) || |
1021 | (P_PICTURE == picture_type)) { |
1022 | offset = READ_VREG(MP4_OFFSET_REG); |
1023 | if (hw->chunk) { |
1024 | dec_pic->pts_valid = hw->chunk->pts_valid; |
1025 | dec_pic->pts = hw->chunk->pts; |
1026 | dec_pic->pts64 = hw->chunk->pts64; |
1027 | } else { |
1028 | if (pts_lookup_offset_us64(PTS_TYPE_VIDEO, offset, |
1029 | &pts, &frame_size, 3000, &pts_us64) == 0) { |
1030 | dec_pic->pts_valid = true; |
1031 | dec_pic->pts = pts; |
1032 | dec_pic->pts64 = pts_us64; |
1033 | hw->pts_hit++; |
1034 | } else { |
1035 | dec_pic->pts_valid = false; |
1036 | hw->pts_missed++; |
1037 | } |
1038 | } |
1039 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO, |
1040 | "%c, offset=0x%x, pts=0x%x(%d), index=%d, used=%d\n", |
1041 | GET_PIC_TYPE(picture_type), offset, dec_pic->pts, |
1042 | dec_pic->pts_valid, index, hw->vfbuf_use[index]); |
1043 | } |
1044 | |
1045 | dec_pic->index = index; |
1046 | dec_pic->pic_info = reg; |
1047 | dec_pic->pic_type = picture_type; |
1048 | dec_pic->duration = duration; |
1049 | hw->vfbuf_use[index] = 0; |
1050 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW, |
1051 | "mmpeg4: pic_num: %d, index %d, type %c, pts %x\n", |
1052 | hw->frame_num, index, |
1053 | GET_PIC_TYPE(picture_type), |
1054 | dec_pic->pts); |
1055 | |
1056 | /* buffer management */ |
1057 | if ((picture_type == I_PICTURE) || |
1058 | (picture_type == P_PICTURE)) { |
1059 | index = update_ref(hw, index); |
1060 | } else { |
1061 | /* drop B frame or disp immediately. |
1062 | * depend on if there are two ref frames |
1063 | */ |
1064 | if (hw->refs[1] == -1) |
1065 | index = -1; |
1066 | } |
1067 | vmpeg4_save_hw_context(hw); |
1068 | if (index < 0) { |
1069 | vdec_schedule_work(&hw->work); |
1070 | return IRQ_HANDLED; |
1071 | } |
1072 | disp_pic = &hw->pic[index]; |
1073 | if ((hw->first_i_frame_ready == 0) && |
1074 | (I_PICTURE == disp_pic->pic_type)) |
1075 | hw->first_i_frame_ready = 1; |
1076 | |
1077 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW, |
1078 | "disp: index=%d, pts=%x(%d), used=%d, picout=%c(dec=%c)\n", |
1079 | index, disp_pic->pts, disp_pic->pts_valid, |
1080 | hw->vfbuf_use[index], |
1081 | GET_PIC_TYPE(disp_pic->pic_type), |
1082 | GET_PIC_TYPE(picture_type)); |
1083 | |
1084 | if (disp_pic->pts_valid) { |
1085 | hw->last_anch_pts = disp_pic->pts; |
1086 | hw->last_anch_pts_us64 = disp_pic->pts64; |
1087 | hw->frame_num_since_last_anch = 0; |
1088 | hw->vop_time_inc_since_last_anch = 0; |
1089 | } else if (vdec_stream_based(vdec)) { |
1090 | disp_pic->pts = hw->last_anch_pts; |
1091 | disp_pic->pts64 = hw->last_anch_pts_us64; |
1092 | |
1093 | if ((time_increment_resolution != 0) && |
1094 | (fixed_vop_rate == 0) && |
1095 | (hw->vmpeg4_amstream_dec_info.rate == 0)) { |
1096 | /* variable PTS rate */ |
1097 | /*bug on variable pts calc, |
1098 | *do as dixed vop first if we |
1099 | *have rate setting before. |
1100 | */ |
1101 | if (vop_time_inc > hw->last_vop_time_inc) { |
1102 | duration = vop_time_inc - |
1103 | hw->last_vop_time_inc; |
1104 | } else { |
1105 | duration = vop_time_inc + |
1106 | time_increment_resolution - |
1107 | hw->last_vop_time_inc; |
1108 | } |
1109 | |
1110 | hw->vop_time_inc_since_last_anch += duration; |
1111 | |
1112 | disp_pic->pts += hw->vop_time_inc_since_last_anch * |
1113 | PTS_UNIT / time_increment_resolution; |
1114 | disp_pic->pts64 += (hw->vop_time_inc_since_last_anch * |
1115 | PTS_UNIT / time_increment_resolution) * |
1116 | 100 / 9; |
1117 | |
1118 | if (hw->vop_time_inc_since_last_anch > |
1119 | (1 << 14)) { |
1120 | /* avoid overflow */ |
1121 | hw->last_anch_pts = disp_pic->pts; |
1122 | hw->last_anch_pts_us64 = disp_pic->pts64; |
1123 | hw->vop_time_inc_since_last_anch = 0; |
1124 | } |
1125 | } else { |
1126 | /* fixed VOP rate */ |
1127 | hw->frame_num_since_last_anch++; |
1128 | disp_pic->pts += DUR2PTS(hw->frame_num_since_last_anch * |
1129 | hw->vmpeg4_amstream_dec_info.rate); |
1130 | disp_pic->pts64 += DUR2PTS( |
1131 | hw->frame_num_since_last_anch * |
1132 | hw->vmpeg4_amstream_dec_info.rate) * 100 / 9; |
1133 | |
1134 | if (hw->frame_num_since_last_anch > (1 << 15)) { |
1135 | /* avoid overflow */ |
1136 | hw->last_anch_pts = disp_pic->pts; |
1137 | hw->last_anch_pts_us64 = disp_pic->pts64; |
1138 | hw->frame_num_since_last_anch = 0; |
1139 | } |
1140 | } |
1141 | } else if (hw->unstable_pts && hw->chunk && |
1142 | MPEG4_VALID_DUR(duration)) { |
1143 | /* invalid pts calc */ |
1144 | hw->frame_num_since_last_anch = hw->chunk_frame_count; |
1145 | disp_pic->pts = hw->last_anch_pts + |
1146 | DUR2PTS(hw->frame_num_since_last_anch * |
1147 | duration); |
1148 | disp_pic->pts64 = hw->last_anch_pts_us64 + |
1149 | DUR2PTS(hw->frame_num_since_last_anch * |
1150 | duration) * 100 / 9; |
1151 | |
1152 | if (hw->frame_num_since_last_anch > (1 << 15)) { |
1153 | /* avoid overflow */ |
1154 | hw->last_anch_pts = disp_pic->pts; |
1155 | hw->last_anch_pts_us64 = disp_pic->pts64; |
1156 | hw->frame_num_since_last_anch = 0; |
1157 | } else |
1158 | disp_pic->pts_valid = 1; |
1159 | } |
1160 | |
1161 | if (vdec_frame_based(vdec) && |
1162 | (hw->unstable_pts) && |
1163 | MPEG4_VALID_DUR(duration)) { |
1164 | |
1165 | u32 threshold = DUR2PTS(duration) >> 3; |
1166 | |
1167 | if (disp_pic->pts <= (hw->last_pts + threshold)) { |
1168 | disp_pic->pts = hw->last_pts + DUR2PTS(duration); |
1169 | disp_pic->pts64 = hw->last_pts64 + |
1170 | (DUR2PTS(duration)*100/9); |
1171 | } |
1172 | if (!disp_pic->pts_valid) { |
1173 | disp_pic->pts = 0; |
1174 | disp_pic->pts64 = 0; |
1175 | } |
1176 | } |
1177 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_TIMEINFO, |
1178 | "disp: pic_type %c, pts %d(%lld), diff %d, cnt %d\n", |
1179 | GET_PIC_TYPE(disp_pic->pic_type), |
1180 | disp_pic->pts, |
1181 | disp_pic->pts64, |
1182 | disp_pic->pts - hw->last_pts, |
1183 | hw->chunk_frame_count); |
1184 | hw->last_pts = disp_pic->pts; |
1185 | hw->last_pts64 = disp_pic->pts64; |
1186 | hw->frame_dur = duration; |
1187 | disp_pic->duration = duration; |
1188 | disp_pic->repeat_cnt = repeat_cnt; |
1189 | |
1190 | prepare_display_buf(hw, disp_pic); |
1191 | |
1192 | hw->total_frame += repeat_cnt + 1; |
1193 | hw->last_vop_time_inc = vop_time_inc; |
1194 | |
1195 | if (vdec_frame_based(vdec) && |
1196 | (frmbase_cont_bitlevel != 0) && |
1197 | (hw->first_i_frame_ready)) { |
1198 | u32 consume_byte, res_byte, bitcnt; |
1199 | |
1200 | bitcnt = READ_VREG(VIFF_BIT_CNT); |
1201 | res_byte = bitcnt >> 3; |
1202 | |
1203 | if (hw->chunk_size > res_byte) { |
1204 | if (bitcnt > frmbase_cont_bitlevel) { |
1205 | consume_byte = hw->chunk_size - res_byte; |
1206 | |
1207 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW, |
1208 | "%s, size %d, consume %d, res %d\n", __func__, |
1209 | hw->chunk_size, consume_byte, res_byte); |
1210 | |
1211 | if (consume_byte > VDEC_FIFO_ALIGN) { |
1212 | consume_byte -= VDEC_FIFO_ALIGN; |
1213 | res_byte += VDEC_FIFO_ALIGN; |
1214 | } |
1215 | hw->chunk_offset += consume_byte; |
1216 | hw->chunk_size = res_byte; |
1217 | hw->dec_result = DEC_RESULT_UNFINISH; |
1218 | hw->chunk_frame_count++; |
1219 | hw->unstable_pts = 1; |
1220 | } else { |
1221 | hw->chunk_size = 0; |
1222 | hw->chunk_offset = 0; |
1223 | } |
1224 | } else { |
1225 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR, |
1226 | "error: bitbyte %d hw->chunk_size %d\n", res_byte, hw->chunk_size); |
1227 | hw->chunk_size = 0; |
1228 | hw->chunk_offset = 0; |
1229 | } |
1230 | } |
1231 | vdec_schedule_work(&hw->work); |
1232 | } |
1233 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FRAME_NUM, |
1234 | "%s: frame num:%d\n", __func__, hw->frame_num); |
1235 | |
1236 | return IRQ_HANDLED; |
1237 | } |
1238 | |
1239 | static irqreturn_t vmpeg4_isr(struct vdec_s *vdec, int irq) |
1240 | { |
1241 | struct vdec_mpeg4_hw_s *hw = |
1242 | (struct vdec_mpeg4_hw_s *)(vdec->private); |
1243 | |
1244 | if (hw->eos) |
1245 | return IRQ_HANDLED; |
1246 | |
1247 | return IRQ_WAKE_THREAD; |
1248 | } |
1249 | |
1250 | static void flush_output(struct vdec_mpeg4_hw_s * hw) |
1251 | { |
1252 | struct pic_info_t *pic; |
1253 | |
1254 | if (hw->vfbuf_use[hw->refs[1]] > 0) { |
1255 | pic = &hw->pic[hw->refs[1]]; |
1256 | prepare_display_buf(hw, pic); |
1257 | } |
1258 | } |
1259 | |
1260 | static int notify_v4l_eos(struct vdec_s *vdec) |
1261 | { |
1262 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
1263 | struct aml_vcodec_ctx *ctx = (struct aml_vcodec_ctx *)(hw->v4l2_ctx); |
1264 | struct vframe_s *vf = NULL; |
1265 | struct vdec_v4l2_buffer *fb = NULL; |
1266 | |
1267 | if (hw->is_used_v4l && hw->eos) { |
1268 | if (kfifo_get(&hw->newframe_q, &vf) == 0 || vf == NULL) { |
1269 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR, |
1270 | "%s fatal error, no available buffer slot.\n", |
1271 | __func__); |
1272 | return -1; |
1273 | } |
1274 | |
1275 | if (vdec_v4l_get_buffer(hw->v4l2_ctx, &fb)) { |
1276 | pr_err("[%d] get fb fail.\n", ctx->id); |
1277 | return -1; |
1278 | } |
1279 | |
1280 | vf->timestamp = ULONG_MAX; |
1281 | vf->v4l_mem_handle = (unsigned long)fb; |
1282 | vf->flag = VFRAME_FLAG_EMPTY_FRAME_V4L; |
1283 | |
1284 | kfifo_put(&hw->display_q, (const struct vframe_s *)vf); |
1285 | vf_notify_receiver(vdec->vf_provider_name, |
1286 | VFRAME_EVENT_PROVIDER_VFRAME_READY, NULL); |
1287 | |
1288 | pr_info("[%d] mpeg4 EOS notify.\n", ctx->id); |
1289 | } |
1290 | |
1291 | return 0; |
1292 | } |
1293 | |
1294 | static void vmpeg4_work(struct work_struct *work) |
1295 | { |
1296 | struct vdec_mpeg4_hw_s *hw = |
1297 | container_of(work, struct vdec_mpeg4_hw_s, work); |
1298 | struct vdec_s *vdec = hw_to_vdec(hw); |
1299 | |
1300 | /* finished decoding one frame or error, |
1301 | * notify vdec core to switch context |
1302 | */ |
1303 | if (hw->dec_result != DEC_RESULT_DONE) |
1304 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW, |
1305 | "vmpeg4_work: result=%d,status=%d\n", |
1306 | hw->dec_result, hw_to_vdec(hw)->next_status); |
1307 | |
1308 | if (hw->dec_result == DEC_RESULT_UNFINISH) { |
1309 | if (!hw->ctx_valid) |
1310 | hw->ctx_valid = 1; |
1311 | |
1312 | } else if ((hw->dec_result == DEC_RESULT_DONE) || |
1313 | ((input_frame_based(&vdec->input)) && hw->chunk)) { |
1314 | if (!hw->ctx_valid) |
1315 | hw->ctx_valid = 1; |
1316 | |
1317 | vdec_vframe_dirty(vdec, hw->chunk); |
1318 | hw->chunk = NULL; |
1319 | } else if (hw->dec_result == DEC_RESULT_AGAIN |
1320 | && (vdec->next_status != VDEC_STATUS_DISCONNECTED)) { |
1321 | /* |
1322 | stream base: stream buf empty or timeout |
1323 | frame base: vdec_prepare_input fail |
1324 | */ |
1325 | if (!vdec_has_more_input(vdec)) { |
1326 | hw->dec_result = DEC_RESULT_EOS; |
1327 | vdec_schedule_work(&hw->work); |
1328 | return; |
1329 | } |
1330 | } else if (hw->dec_result == DEC_RESULT_FORCE_EXIT) { |
1331 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR, |
1332 | "%s: force exit\n", __func__); |
1333 | if (hw->stat & STAT_ISR_REG) { |
1334 | amvdec_stop(); |
1335 | vdec_free_irq(VDEC_IRQ_1, (void *)hw); |
1336 | hw->stat &= ~STAT_ISR_REG; |
1337 | } |
1338 | } else if (hw->dec_result == DEC_RESULT_EOS) { |
1339 | hw->eos = 1; |
1340 | if (hw->stat & STAT_VDEC_RUN) { |
1341 | amvdec_stop(); |
1342 | hw->stat &= ~STAT_VDEC_RUN; |
1343 | } |
1344 | vdec_vframe_dirty(vdec, hw->chunk); |
1345 | hw->chunk = NULL; |
1346 | vdec_clean_input(vdec); |
1347 | flush_output(hw); |
1348 | |
1349 | if (hw->is_used_v4l) |
1350 | notify_v4l_eos(vdec); |
1351 | |
1352 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1353 | "%s: eos flushed, frame_num %d\n", |
1354 | __func__, hw->frame_num); |
1355 | } |
1356 | |
1357 | if (hw->stat & STAT_VDEC_RUN) { |
1358 | amvdec_stop(); |
1359 | hw->stat &= ~STAT_VDEC_RUN; |
1360 | } |
1361 | /*disable mbox interrupt */ |
1362 | WRITE_VREG(ASSIST_MBOX1_MASK, 0); |
1363 | del_timer_sync(&hw->check_timer); |
1364 | hw->stat &= ~STAT_TIMER_ARM; |
1365 | |
1366 | /* mark itself has all HW resource released and input released */ |
1367 | if (vdec->parallel_dec == 1) |
1368 | vdec_core_finish_run(vdec, CORE_MASK_VDEC_1); |
1369 | else |
1370 | vdec_core_finish_run(vdec, CORE_MASK_VDEC_1 | CORE_MASK_HEVC); |
1371 | |
1372 | wake_up_interruptible(&hw->wait_q); |
1373 | if (hw->vdec_cb) |
1374 | hw->vdec_cb(vdec, hw->vdec_cb_arg); |
1375 | } |
1376 | |
1377 | static struct vframe_s *vmpeg_vf_peek(void *op_arg) |
1378 | { |
1379 | struct vframe_s *vf; |
1380 | struct vdec_s *vdec = op_arg; |
1381 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
1382 | |
1383 | if (!hw) |
1384 | return NULL; |
1385 | hw->peek_num++; |
1386 | if (kfifo_peek(&hw->display_q, &vf)) |
1387 | return vf; |
1388 | |
1389 | return NULL; |
1390 | } |
1391 | |
1392 | static struct vframe_s *vmpeg_vf_get(void *op_arg) |
1393 | { |
1394 | struct vframe_s *vf; |
1395 | struct vdec_s *vdec = op_arg; |
1396 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
1397 | hw->get_num++; |
1398 | if (kfifo_get(&hw->display_q, &vf)) |
1399 | return vf; |
1400 | |
1401 | return NULL; |
1402 | } |
1403 | |
1404 | static void vmpeg_vf_put(struct vframe_s *vf, void *op_arg) |
1405 | { |
1406 | struct vdec_s *vdec = op_arg; |
1407 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
1408 | |
1409 | hw->vfbuf_use[vf->index]--; |
1410 | hw->put_num++; |
1411 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FRAME_NUM, |
1412 | "%s: put num:%d\n",__func__, hw->put_num); |
1413 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_BUFFER_DETAIL, |
1414 | "index=%d, used=%d\n", vf->index, hw->vfbuf_use[vf->index]); |
1415 | kfifo_put(&hw->newframe_q, (const struct vframe_s *)vf); |
1416 | } |
1417 | |
1418 | static int vmpeg_event_cb(int type, void *data, void *private_data) |
1419 | { |
1420 | return 0; |
1421 | } |
1422 | |
1423 | static int vmpeg_vf_states(struct vframe_states *states, void *op_arg) |
1424 | { |
1425 | unsigned long flags; |
1426 | struct vdec_s *vdec = op_arg; |
1427 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
1428 | |
1429 | spin_lock_irqsave(&hw->lock, flags); |
1430 | |
1431 | states->vf_pool_size = VF_POOL_SIZE; |
1432 | states->buf_free_num = kfifo_len(&hw->newframe_q); |
1433 | states->buf_avail_num = kfifo_len(&hw->display_q); |
1434 | states->buf_recycle_num = 0; |
1435 | |
1436 | spin_unlock_irqrestore(&hw->lock, flags); |
1437 | |
1438 | return 0; |
1439 | } |
1440 | |
1441 | |
1442 | static int dec_status(struct vdec_s *vdec, struct vdec_info *vstatus) |
1443 | { |
1444 | struct vdec_mpeg4_hw_s *hw = |
1445 | (struct vdec_mpeg4_hw_s *)vdec->private; |
1446 | |
1447 | if (!hw) |
1448 | return -1; |
1449 | |
1450 | vstatus->frame_width = hw->frame_width; |
1451 | vstatus->frame_height = hw->frame_height; |
1452 | if (0 != hw->vmpeg4_amstream_dec_info.rate) |
1453 | vstatus->frame_rate = DURATION_UNIT / |
1454 | hw->vmpeg4_amstream_dec_info.rate; |
1455 | else |
1456 | vstatus->frame_rate = DURATION_UNIT; |
1457 | vstatus->error_count = READ_VREG(MP4_ERR_COUNT); |
1458 | vstatus->status = hw->stat; |
1459 | vstatus->frame_dur = hw->frame_dur; |
1460 | snprintf(vstatus->vdec_name, sizeof(vstatus->vdec_name), |
1461 | "%s", DRIVER_NAME); |
1462 | |
1463 | return 0; |
1464 | } |
1465 | |
1466 | /****************************************/ |
1467 | static int vmpeg4_canvas_init(struct vdec_mpeg4_hw_s *hw) |
1468 | { |
1469 | int i, ret; |
1470 | u32 canvas_width, canvas_height; |
1471 | u32 decbuf_size, decbuf_y_size; |
1472 | struct vdec_s *vdec = hw_to_vdec(hw); |
1473 | unsigned long decbuf_start; |
1474 | |
1475 | if (buf_size <= 0x00400000) { |
1476 | /* SD only */ |
1477 | canvas_width = 768; |
1478 | canvas_height = 576; |
1479 | decbuf_y_size = 0x80000; |
1480 | decbuf_size = 0x100000; |
1481 | } else { |
1482 | int w = 1920; |
1483 | int h = 1088; |
1484 | int align_w, align_h; |
1485 | int max, min; |
1486 | |
1487 | align_w = ALIGN(w, 64); |
1488 | align_h = ALIGN(h, 64); |
1489 | if (align_w > align_h) { |
1490 | max = align_w; |
1491 | min = align_h; |
1492 | } else { |
1493 | max = align_h; |
1494 | min = align_w; |
1495 | } |
1496 | /* HD & SD */ |
1497 | if ((max > 1920 || min > 1088) && |
1498 | ALIGN(align_w * align_h * 3/2, SZ_64K) * 9 <= |
1499 | buf_size) { |
1500 | canvas_width = align_w; |
1501 | canvas_height = align_h; |
1502 | decbuf_y_size = |
1503 | ALIGN(align_w * align_h, SZ_64K); |
1504 | decbuf_size = |
1505 | ALIGN(align_w * align_h * 3/2, SZ_64K); |
1506 | } else { /*1080p*/ |
1507 | if (h > w) { |
1508 | canvas_width = 1088; |
1509 | canvas_height = 1920; |
1510 | } else { |
1511 | canvas_width = 1920; |
1512 | canvas_height = 1088; |
1513 | } |
1514 | decbuf_y_size = 0x200000; |
1515 | decbuf_size = 0x300000; |
1516 | } |
1517 | } |
1518 | |
1519 | for (i = 0; i < MAX_BMMU_BUFFER_NUM; i++) { |
1520 | |
1521 | unsigned canvas; |
1522 | |
1523 | if (i == (MAX_BMMU_BUFFER_NUM - 1)) |
1524 | decbuf_size = WORKSPACE_SIZE; |
1525 | |
1526 | if (hw->is_used_v4l && !(i == (MAX_BMMU_BUFFER_NUM - 1))) { |
1527 | continue; |
1528 | } else { |
1529 | ret = decoder_bmmu_box_alloc_buf_phy(hw->mm_blk_handle, i, |
1530 | decbuf_size, DRIVER_NAME, &decbuf_start); |
1531 | if (ret < 0) { |
1532 | pr_err("mmu alloc failed! size %d idx %d\n", |
1533 | decbuf_size, i); |
1534 | return ret; |
1535 | } |
1536 | } |
1537 | |
1538 | if (i == (MAX_BMMU_BUFFER_NUM - 1)) { |
1539 | hw->buf_start = decbuf_start; |
1540 | } else { |
1541 | if (vdec->parallel_dec == 1) { |
1542 | unsigned tmp; |
1543 | if (canvas_y(hw->canvas_spec[i]) == 0xff) { |
1544 | tmp = |
1545 | vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id); |
1546 | hw->canvas_spec[i] &= ~0xff; |
1547 | hw->canvas_spec[i] |= tmp; |
1548 | } |
1549 | if (canvas_u(hw->canvas_spec[i]) == 0xff) { |
1550 | tmp = |
1551 | vdec->get_canvas_ex(CORE_MASK_VDEC_1, vdec->id); |
1552 | hw->canvas_spec[i] &= ~(0xffff << 8); |
1553 | hw->canvas_spec[i] |= tmp << 8; |
1554 | hw->canvas_spec[i] |= tmp << 16; |
1555 | } |
1556 | canvas = hw->canvas_spec[i]; |
1557 | } else { |
1558 | canvas = vdec->get_canvas(i, 2); |
1559 | hw->canvas_spec[i] = canvas; |
1560 | } |
1561 | |
1562 | hw->canvas_config[i][0].phy_addr = decbuf_start; |
1563 | hw->canvas_config[i][0].width = canvas_width; |
1564 | hw->canvas_config[i][0].height = canvas_height; |
1565 | hw->canvas_config[i][0].block_mode = hw->blkmode; |
1566 | if (hw->blkmode == CANVAS_BLKMODE_LINEAR) |
1567 | hw->canvas_config[i][0].endian = 7; |
1568 | else |
1569 | hw->canvas_config[i][0].endian = 0; |
1570 | canvas_config_config(canvas_y(canvas), |
1571 | &hw->canvas_config[i][0]); |
1572 | |
1573 | hw->canvas_config[i][1].phy_addr = |
1574 | decbuf_start + decbuf_y_size; |
1575 | hw->canvas_config[i][1].width = canvas_width; |
1576 | hw->canvas_config[i][1].height = (canvas_height >> 1); |
1577 | hw->canvas_config[i][1].block_mode = hw->blkmode; |
1578 | if (hw->blkmode == CANVAS_BLKMODE_LINEAR) |
1579 | hw->canvas_config[i][1].endian = 7; |
1580 | else |
1581 | hw->canvas_config[i][1].endian = 0; |
1582 | canvas_config_config(canvas_u(canvas), |
1583 | &hw->canvas_config[i][1]); |
1584 | } |
1585 | } |
1586 | |
1587 | return 0; |
1588 | } |
1589 | |
1590 | static void vmpeg4_dump_state(struct vdec_s *vdec) |
1591 | { |
1592 | struct vdec_mpeg4_hw_s *hw = |
1593 | (struct vdec_mpeg4_hw_s *)(vdec->private); |
1594 | u32 i; |
1595 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1596 | "====== %s\n", __func__); |
1597 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1598 | "width/height (%d/%d), i_fram:%d, buffer_not_ready %d\n", |
1599 | hw->frame_width, |
1600 | hw->frame_height, |
1601 | hw->first_i_frame_ready, |
1602 | hw->buffer_not_ready |
1603 | ); |
1604 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) { |
1605 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1606 | "index %d, used %d\n", i, hw->vfbuf_use[i]); |
1607 | } |
1608 | |
1609 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1610 | "is_framebase(%d), eos %d, state 0x%x, dec_result 0x%x dec_frm %d\n", |
1611 | vdec_frame_based(vdec), |
1612 | hw->eos, |
1613 | hw->stat, |
1614 | hw->dec_result, |
1615 | hw->frame_num |
1616 | ); |
1617 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1618 | "is_framebase(%d), put_frm %d run %d not_run_ready %d input_empty %d,drop %d\n", |
1619 | vdec_frame_based(vdec), |
1620 | hw->put_num, |
1621 | hw->run_count, |
1622 | hw->not_run_ready, |
1623 | hw->input_empty, |
1624 | hw->drop_frame_count |
1625 | ); |
1626 | |
1627 | if (vf_get_receiver(vdec->vf_provider_name)) { |
1628 | enum receviver_start_e state = |
1629 | vf_notify_receiver(vdec->vf_provider_name, |
1630 | VFRAME_EVENT_PROVIDER_QUREY_STATE, |
1631 | NULL); |
1632 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1633 | "\nreceiver(%s) state %d\n", |
1634 | vdec->vf_provider_name, |
1635 | state); |
1636 | } |
1637 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1638 | "%s, newq(%d/%d), dispq(%d/%d) vf peek/get/put (%d/%d/%d)\n", |
1639 | __func__, |
1640 | kfifo_len(&hw->newframe_q), VF_POOL_SIZE, |
1641 | kfifo_len(&hw->display_q), VF_POOL_SIZE, |
1642 | hw->peek_num, hw->get_num, hw->put_num |
1643 | ); |
1644 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1645 | "VIFF_BIT_CNT=0x%x\n", |
1646 | READ_VREG(VIFF_BIT_CNT)); |
1647 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1648 | "VLD_MEM_VIFIFO_LEVEL=0x%x\n", |
1649 | READ_VREG(VLD_MEM_VIFIFO_LEVEL)); |
1650 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1651 | "VLD_MEM_VIFIFO_WP=0x%x\n", |
1652 | READ_VREG(VLD_MEM_VIFIFO_WP)); |
1653 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1654 | "VLD_MEM_VIFIFO_RP=0x%x\n", |
1655 | READ_VREG(VLD_MEM_VIFIFO_RP)); |
1656 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1657 | "PARSER_VIDEO_RP=0x%x\n", |
1658 | READ_PARSER_REG(PARSER_VIDEO_RP)); |
1659 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1660 | "PARSER_VIDEO_WP=0x%x\n", |
1661 | READ_PARSER_REG(PARSER_VIDEO_WP)); |
1662 | if (vdec_frame_based(vdec) && |
1663 | debug_enable & PRINT_FRAMEBASE_DATA) { |
1664 | int jj; |
1665 | if (hw->chunk && hw->chunk->block && |
1666 | hw->chunk->size > 0) { |
1667 | u8 *data = NULL; |
1668 | |
1669 | if (!hw->chunk->block->is_mapped) |
1670 | data = codec_mm_vmap(hw->chunk->block->start + |
1671 | hw->chunk->offset, hw->chunk->size); |
1672 | else |
1673 | data = ((u8 *)hw->chunk->block->start_virt) + |
1674 | hw->chunk->offset; |
1675 | |
1676 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1677 | "frame data size 0x%x\n", |
1678 | hw->chunk->size); |
1679 | for (jj = 0; jj < hw->chunk->size; jj++) { |
1680 | if ((jj & 0xf) == 0) |
1681 | mmpeg4_debug_print(DECODE_ID(hw), |
1682 | PRINT_FRAMEBASE_DATA, |
1683 | "%06x:", jj); |
1684 | mmpeg4_debug_print(DECODE_ID(hw), |
1685 | PRINT_FRAMEBASE_DATA, |
1686 | "%02x ", data[jj]); |
1687 | if (((jj + 1) & 0xf) == 0) |
1688 | mmpeg4_debug_print(DECODE_ID(hw), |
1689 | PRINT_FRAMEBASE_DATA, |
1690 | "\n"); |
1691 | } |
1692 | |
1693 | if (!hw->chunk->block->is_mapped) |
1694 | codec_mm_unmap_phyaddr(data); |
1695 | } |
1696 | } |
1697 | } |
1698 | |
1699 | static void reset_process_time(struct vdec_mpeg4_hw_s *hw) |
1700 | { |
1701 | if (hw->start_process_time) { |
1702 | unsigned process_time = |
1703 | 1000 * (jiffies - hw->start_process_time) / HZ; |
1704 | hw->start_process_time = 0; |
1705 | if (process_time > max_process_time[DECODE_ID(hw)]) |
1706 | max_process_time[DECODE_ID(hw)] = process_time; |
1707 | } |
1708 | } |
1709 | static void start_process_time(struct vdec_mpeg4_hw_s *hw) |
1710 | { |
1711 | hw->decode_timeout_count = 2; |
1712 | hw->start_process_time = jiffies; |
1713 | } |
1714 | |
1715 | static void timeout_process(struct vdec_mpeg4_hw_s *hw) |
1716 | { |
1717 | if (hw->stat & STAT_VDEC_RUN) { |
1718 | amvdec_stop(); |
1719 | hw->stat &= ~STAT_VDEC_RUN; |
1720 | } |
1721 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1722 | "%s decoder timeout %d\n", __func__, hw->timeout_cnt); |
1723 | if (vdec_frame_based((hw_to_vdec(hw)))) { |
1724 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1725 | "%s frame_num %d, chunk size 0x%x, chksum 0x%x\n", |
1726 | __func__, |
1727 | hw->frame_num, |
1728 | hw->chunk->size, |
1729 | get_data_check_sum(hw, hw->chunk->size)); |
1730 | } |
1731 | hw->timeout_cnt++; |
1732 | /* timeout: data droped, frame_num inaccurate*/ |
1733 | hw->frame_num++; |
1734 | reset_process_time(hw); |
1735 | hw->first_i_frame_ready = 0; |
1736 | hw->dec_result = DEC_RESULT_DONE; |
1737 | vdec_schedule_work(&hw->work); |
1738 | } |
1739 | |
1740 | |
1741 | static void check_timer_func(unsigned long arg) |
1742 | { |
1743 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)arg; |
1744 | struct vdec_s *vdec = hw_to_vdec(hw); |
1745 | unsigned int timeout_val = decode_timeout_val; |
1746 | |
1747 | if (radr != 0) { |
1748 | if (rval != 0) { |
1749 | WRITE_VREG(radr, rval); |
1750 | pr_info("WRITE_VREG(%x,%x)\n", radr, rval); |
1751 | } else |
1752 | pr_info("READ_VREG(%x)=%x\n", radr, READ_VREG(radr)); |
1753 | rval = 0; |
1754 | radr = 0; |
1755 | } |
1756 | |
1757 | if (((debug_enable & PRINT_FLAG_TIMEOUT_STATUS) == 0) && |
1758 | (vdec_frame_based(vdec) || |
1759 | ((u32)READ_VREG(VLD_MEM_VIFIFO_LEVEL) > 0x100)) && |
1760 | (timeout_val > 0) && |
1761 | (hw->start_process_time > 0) && |
1762 | ((1000 * (jiffies - hw->start_process_time) / HZ) |
1763 | > timeout_val)) { |
1764 | if (hw->last_vld_level == READ_VREG(VLD_MEM_VIFIFO_LEVEL)) { |
1765 | if (hw->decode_timeout_count > 0) |
1766 | hw->decode_timeout_count--; |
1767 | if (hw->decode_timeout_count == 0) |
1768 | timeout_process(hw); |
1769 | } |
1770 | hw->last_vld_level = READ_VREG(VLD_MEM_VIFIFO_LEVEL); |
1771 | } |
1772 | |
1773 | if (vdec->next_status == VDEC_STATUS_DISCONNECTED) { |
1774 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_ERROR, |
1775 | "vdec requested to be disconnected\n"); |
1776 | hw->dec_result = DEC_RESULT_FORCE_EXIT; |
1777 | vdec_schedule_work(&hw->work); |
1778 | return; |
1779 | } |
1780 | |
1781 | mod_timer(&hw->check_timer, jiffies + CHECK_INTERVAL); |
1782 | } |
1783 | |
1784 | static int vmpeg4_hw_ctx_restore(struct vdec_mpeg4_hw_s *hw) |
1785 | { |
1786 | int index, i; |
1787 | void *workspace_buf = NULL; |
1788 | |
1789 | index = find_free_buffer(hw); |
1790 | if (index < 0) |
1791 | return -1; |
1792 | |
1793 | if (!hw->init_flag) { |
1794 | if (vmpeg4_canvas_init(hw) < 0) |
1795 | return -1; |
1796 | } else { |
1797 | if (!hw->is_used_v4l) { |
1798 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) { |
1799 | canvas_config_config(canvas_y(hw->canvas_spec[i]), |
1800 | &hw->canvas_config[i][0]); |
1801 | canvas_config_config(canvas_u(hw->canvas_spec[i]), |
1802 | &hw->canvas_config[i][1]); |
1803 | } |
1804 | } |
1805 | } |
1806 | /* prepare REF0 & REF1 |
1807 | * points to the past two IP buffers |
1808 | * prepare REC_CANVAS_ADDR and ANC2_CANVAS_ADDR |
1809 | * points to the output buffer |
1810 | */ |
1811 | if (hw->refs[0] == -1) { |
1812 | WRITE_VREG(MREG_REF0, (hw->refs[1] == -1) ? 0xffffffff : |
1813 | hw->canvas_spec[hw->refs[1]]); |
1814 | } else { |
1815 | WRITE_VREG(MREG_REF0, (hw->refs[0] == -1) ? 0xffffffff : |
1816 | hw->canvas_spec[hw->refs[0]]); |
1817 | } |
1818 | WRITE_VREG(MREG_REF1, (hw->refs[1] == -1) ? 0xffffffff : |
1819 | hw->canvas_spec[hw->refs[1]]); |
1820 | |
1821 | WRITE_VREG(REC_CANVAS_ADDR, hw->canvas_spec[index]); |
1822 | WRITE_VREG(ANC2_CANVAS_ADDR, hw->canvas_spec[index]); |
1823 | |
1824 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_RESTORE, |
1825 | "restore ref0=0x%x, ref1=0x%x, rec=0x%x, ctx_valid=%d,index=%d\n", |
1826 | READ_VREG(MREG_REF0), |
1827 | READ_VREG(MREG_REF1), |
1828 | READ_VREG(REC_CANVAS_ADDR), |
1829 | hw->ctx_valid, index); |
1830 | |
1831 | /* notify ucode the buffer start address */ |
1832 | workspace_buf = codec_mm_vmap(hw->buf_start, WORKSPACE_SIZE); |
1833 | if (workspace_buf) { |
1834 | /* clear to fix decoder timeout at first time */ |
1835 | if (!hw->init_flag) |
1836 | memset(workspace_buf, 0, WORKSPACE_SIZE); |
1837 | codec_mm_dma_flush(workspace_buf, |
1838 | WORKSPACE_SIZE, DMA_TO_DEVICE); |
1839 | codec_mm_unmap_phyaddr(workspace_buf); |
1840 | } |
1841 | WRITE_VREG(MEM_OFFSET_REG, hw->buf_start); |
1842 | |
1843 | /* disable PSCALE for hardware sharing */ |
1844 | WRITE_VREG(PSCALE_CTRL, 0); |
1845 | |
1846 | WRITE_VREG(MREG_BUFFEROUT, 0); |
1847 | |
1848 | /* clear mailbox interrupt */ |
1849 | WRITE_VREG(ASSIST_MBOX1_CLR_REG, 1); |
1850 | |
1851 | /* enable mailbox interrupt */ |
1852 | WRITE_VREG(ASSIST_MBOX1_MASK, 1); |
1853 | |
1854 | /* clear repeat count */ |
1855 | WRITE_VREG(MP4_NOT_CODED_CNT, 0); |
1856 | |
1857 | #ifdef NV21 |
1858 | SET_VREG_MASK(MDEC_PIC_DC_CTRL, 1 << 17); |
1859 | #endif |
1860 | |
1861 | #if 1/* /MESON_CPU_TYPE >= MESON_CPU_TYPE_MESON8 */ |
1862 | WRITE_VREG(MDEC_PIC_DC_THRESH, 0x404038aa); |
1863 | #endif |
1864 | |
1865 | WRITE_VREG(MP4_PIC_WH, (hw->ctx_valid) ? |
1866 | hw->reg_mp4_pic_wh : |
1867 | ((hw->frame_width << 16) | hw->frame_height)); |
1868 | WRITE_VREG(MP4_SYS_RATE, hw->vmpeg4_amstream_dec_info.rate); |
1869 | |
1870 | if (hw->ctx_valid) { |
1871 | WRITE_VREG(DC_AC_CTRL, hw->reg_dc_ac_ctrl); |
1872 | WRITE_VREG(IQIDCT_CONTROL, hw->reg_iqidct_control); |
1873 | WRITE_VREG(RESYNC_MARKER_LENGTH, hw->reg_resync_marker_length); |
1874 | WRITE_VREG(RV_AI_MB_COUNT, hw->reg_rv_ai_mb_count); |
1875 | } |
1876 | WRITE_VREG(MPEG1_2_REG, (hw->ctx_valid) ? hw->reg_mpeg1_2_reg : 1); |
1877 | WRITE_VREG(VCOP_CTRL_REG, hw->reg_vcop_ctrl_reg); |
1878 | WRITE_VREG(PIC_HEAD_INFO, hw->reg_pic_head_info); |
1879 | WRITE_VREG(SLICE_QP, hw->reg_slice_qp); |
1880 | WRITE_VREG(MB_INFO, hw->reg_mb_info); |
1881 | |
1882 | if (vdec_frame_based(hw_to_vdec(hw)) && hw->chunk) { |
1883 | /* frame based input */ |
1884 | WRITE_VREG(MREG_INPUT, (hw->chunk->offset & 7) | (1<<7) | |
1885 | (hw->ctx_valid<<6)); |
1886 | } else { |
1887 | /* stream based input */ |
1888 | WRITE_VREG(MREG_INPUT, (hw->ctx_valid<<6)); |
1889 | } |
1890 | |
1891 | return 0; |
1892 | } |
1893 | |
1894 | static void vmpeg4_local_init(struct vdec_mpeg4_hw_s *hw) |
1895 | { |
1896 | int i; |
1897 | |
1898 | hw->vmpeg4_ratio = hw->vmpeg4_amstream_dec_info.ratio; |
1899 | |
1900 | hw->vmpeg4_ratio64 = hw->vmpeg4_amstream_dec_info.ratio64; |
1901 | |
1902 | hw->vmpeg4_rotation = |
1903 | (((unsigned long)hw->vmpeg4_amstream_dec_info.param) >> 16) & 0xffff; |
1904 | hw->sys_mp4_rate = hw->vmpeg4_amstream_dec_info.rate; |
1905 | hw->frame_width = hw->vmpeg4_amstream_dec_info.width; |
1906 | hw->frame_height = hw->vmpeg4_amstream_dec_info.height; |
1907 | hw->frame_dur = 0; |
1908 | hw->frame_prog = 0; |
1909 | hw->unstable_pts = |
1910 | (((unsigned long) hw->vmpeg4_amstream_dec_info.param & 0x40) >> 6); |
1911 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
1912 | "param = 0x%x unstable_pts = %d\n", |
1913 | hw->vmpeg4_amstream_dec_info.param, |
1914 | hw->unstable_pts); |
1915 | hw->last_dec_pts = -1; |
1916 | |
1917 | hw->total_frame = 0; |
1918 | |
1919 | hw->last_anch_pts = 0; |
1920 | |
1921 | hw->last_anch_pts_us64 = 0; |
1922 | |
1923 | hw->last_vop_time_inc = hw->last_duration = 0; |
1924 | |
1925 | hw->vop_time_inc_since_last_anch = 0; |
1926 | hw->last_pts = 0; |
1927 | hw->last_pts64 = 0; |
1928 | hw->frame_num_since_last_anch = 0; |
1929 | hw->frame_num = 0; |
1930 | hw->put_num = 0; |
1931 | hw->run_count = 0; |
1932 | hw->not_run_ready = 0; |
1933 | hw->input_empty = 0; |
1934 | hw->peek_num = 0; |
1935 | hw->get_num = 0; |
1936 | |
1937 | hw->pts_hit = hw->pts_missed = hw->pts_i_hit = hw->pts_i_missed = 0; |
1938 | hw->refs[0] = -1; |
1939 | hw->refs[1] = -1; |
1940 | hw->first_i_frame_ready = 0; |
1941 | hw->drop_frame_count = 0; |
1942 | hw->buffer_not_ready = 0; |
1943 | hw->init_flag = 0; |
1944 | hw->dec_result = DEC_RESULT_NONE; |
1945 | hw->timeout_cnt = 0; |
1946 | |
1947 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) |
1948 | hw->vfbuf_use[i] = 0; |
1949 | |
1950 | INIT_KFIFO(hw->display_q); |
1951 | INIT_KFIFO(hw->newframe_q); |
1952 | |
1953 | for (i = 0; i < VF_POOL_SIZE; i++) { |
1954 | const struct vframe_s *vf = &hw->vfpool[i]; |
1955 | |
1956 | hw->vfpool[i].index = DECODE_BUFFER_NUM_MAX; |
1957 | kfifo_put(&hw->newframe_q, (const struct vframe_s *)vf); |
1958 | } |
1959 | if (hw->mm_blk_handle) { |
1960 | decoder_bmmu_box_free(hw->mm_blk_handle); |
1961 | hw->mm_blk_handle = NULL; |
1962 | } |
1963 | hw->mm_blk_handle = decoder_bmmu_box_alloc_box( |
1964 | DRIVER_NAME, |
1965 | 0, |
1966 | MAX_BMMU_BUFFER_NUM, |
1967 | 4 + PAGE_SHIFT, |
1968 | CODEC_MM_FLAGS_CMA_CLEAR | |
1969 | CODEC_MM_FLAGS_FOR_VDECODER); |
1970 | INIT_WORK(&hw->work, vmpeg4_work); |
1971 | |
1972 | init_waitqueue_head(&hw->wait_q); |
1973 | } |
1974 | |
1975 | static s32 vmmpeg4_init(struct vdec_mpeg4_hw_s *hw) |
1976 | { |
1977 | int trickmode_fffb = 0; |
1978 | int size = -1, fw_size = 0x1000 * 16; |
1979 | struct firmware_s *fw = NULL; |
1980 | |
1981 | fw = vmalloc(sizeof(struct firmware_s) + fw_size); |
1982 | if (IS_ERR_OR_NULL(fw)) |
1983 | return -ENOMEM; |
1984 | |
1985 | if (hw->vmpeg4_amstream_dec_info.format == |
1986 | VIDEO_DEC_FORMAT_MPEG4_5) { |
1987 | size = get_firmware_data(VIDEO_DEC_MPEG4_5_MULTI, fw->data); |
1988 | strncpy(fw->name, "mmpeg4_mc_5", sizeof(fw->name)); |
1989 | } else if (hw->vmpeg4_amstream_dec_info.format == |
1990 | VIDEO_DEC_FORMAT_H263) { |
1991 | size = get_firmware_data(VIDEO_DEC_H263_MULTI, fw->data); |
1992 | strncpy(fw->name, "mh263_mc", sizeof(fw->name)); |
1993 | } else |
1994 | pr_err("unsupport mpeg4 sub format %d\n", |
1995 | hw->vmpeg4_amstream_dec_info.format); |
1996 | pr_info("mmpeg4 get fw %s, size %x\n", fw->name, size); |
1997 | if (size < 0) { |
1998 | pr_err("get firmware failed."); |
1999 | vfree(fw); |
2000 | return -1; |
2001 | } |
2002 | |
2003 | fw->len = size; |
2004 | hw->fw = fw; |
2005 | |
2006 | query_video_status(0, &trickmode_fffb); |
2007 | |
2008 | pr_info("%s\n", __func__); |
2009 | |
2010 | amvdec_enable(); |
2011 | |
2012 | init_timer(&hw->check_timer); |
2013 | hw->check_timer.data = (unsigned long)hw; |
2014 | hw->check_timer.function = check_timer_func; |
2015 | hw->check_timer.expires = jiffies + CHECK_INTERVAL; |
2016 | hw->stat |= STAT_TIMER_ARM; |
2017 | hw->eos = 0; |
2018 | WRITE_VREG(DECODE_STOP_POS, udebug_flag); |
2019 | |
2020 | vmpeg4_local_init(hw); |
2021 | wmb(); |
2022 | |
2023 | return 0; |
2024 | } |
2025 | |
2026 | static unsigned long run_ready(struct vdec_s *vdec, unsigned long mask) |
2027 | { |
2028 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
2029 | |
2030 | if (hw->eos) |
2031 | return 0; |
2032 | if (vdec_stream_based(vdec) && (hw->init_flag == 0) |
2033 | && pre_decode_buf_level != 0) { |
2034 | u32 rp, wp, level; |
2035 | |
2036 | rp = READ_PARSER_REG(PARSER_VIDEO_RP); |
2037 | wp = READ_PARSER_REG(PARSER_VIDEO_WP); |
2038 | if (wp < rp) |
2039 | level = vdec->input.size + wp - rp; |
2040 | else |
2041 | level = wp - rp; |
2042 | if (level < pre_decode_buf_level) { |
2043 | hw->not_run_ready++; |
2044 | return 0; |
2045 | } |
2046 | } |
2047 | |
2048 | if (!is_enough_free_buffer(hw)) { |
2049 | hw->buffer_not_ready++; |
2050 | return 0; |
2051 | } |
2052 | |
2053 | hw->not_run_ready = 0; |
2054 | hw->buffer_not_ready = 0; |
2055 | if (vdec->parallel_dec == 1) |
2056 | return (unsigned long)(CORE_MASK_VDEC_1); |
2057 | else |
2058 | return (unsigned long)(CORE_MASK_VDEC_1 | CORE_MASK_HEVC); |
2059 | } |
2060 | |
2061 | static unsigned char get_data_check_sum |
2062 | (struct vdec_mpeg4_hw_s *hw, int size) |
2063 | { |
2064 | int jj; |
2065 | int sum = 0; |
2066 | u8 *data = NULL; |
2067 | |
2068 | if (!hw->chunk->block->is_mapped) |
2069 | data = codec_mm_vmap(hw->chunk->block->start + |
2070 | hw->chunk->offset, size); |
2071 | else |
2072 | data = ((u8 *)hw->chunk->block->start_virt) + |
2073 | hw->chunk->offset; |
2074 | |
2075 | for (jj = 0; jj < size; jj++) |
2076 | sum += data[jj]; |
2077 | |
2078 | if (!hw->chunk->block->is_mapped) |
2079 | codec_mm_unmap_phyaddr(data); |
2080 | return sum; |
2081 | } |
2082 | |
2083 | static void run(struct vdec_s *vdec, unsigned long mask, |
2084 | void (*callback)(struct vdec_s *, void *), void *arg) |
2085 | { |
2086 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
2087 | int size = 0, ret = 0; |
2088 | |
2089 | hw->run_count++; |
2090 | hw->vdec_cb_arg = arg; |
2091 | hw->vdec_cb = callback; |
2092 | vdec_reset_core(vdec); |
2093 | |
2094 | if ((vdec_frame_based(vdec)) && |
2095 | (hw->dec_result == DEC_RESULT_UNFINISH)) { |
2096 | vmpeg4_prepare_input(hw); |
2097 | size = hw->chunk_size; |
2098 | } else { |
2099 | size = vdec_prepare_input(vdec, &hw->chunk); |
2100 | if (size < 4) { /*less than start code size 00 00 01 xx*/ |
2101 | hw->input_empty++; |
2102 | hw->dec_result = DEC_RESULT_AGAIN; |
2103 | vdec_schedule_work(&hw->work); |
2104 | return; |
2105 | } |
2106 | if ((vdec_frame_based(vdec)) && |
2107 | (hw->chunk != NULL)) { |
2108 | hw->chunk_offset = hw->chunk->offset; |
2109 | hw->chunk_size = hw->chunk->size; |
2110 | hw->chunk_frame_count = 0; |
2111 | } |
2112 | } |
2113 | if (vdec_frame_based(vdec) && |
2114 | (debug_enable & 0xc00)) { |
2115 | u8 *data = NULL; |
2116 | |
2117 | if (!hw->chunk->block->is_mapped) |
2118 | data = codec_mm_vmap(hw->chunk->block->start + |
2119 | hw->chunk->offset, size); |
2120 | else |
2121 | data = ((u8 *)hw->chunk->block->start_virt) + |
2122 | hw->chunk->offset; |
2123 | |
2124 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_VDEC_STATUS, |
2125 | "%s: size 0x%x sum 0x%x %02x %02x %02x %02x %02x %02x .. %02x %02x %02x %02x\n", |
2126 | __func__, size, get_data_check_sum(hw, size), |
2127 | data[0], data[1], data[2], data[3], |
2128 | data[4], data[5], data[size - 4], |
2129 | data[size - 3], data[size - 2], |
2130 | data[size - 1]); |
2131 | |
2132 | if (debug_enable & PRINT_FRAMEBASE_DATA) { |
2133 | int jj; |
2134 | |
2135 | for (jj = 0; jj < size; jj++) { |
2136 | if ((jj & 0xf) == 0) |
2137 | mmpeg4_debug_print(DECODE_ID(hw), |
2138 | PRINT_FRAMEBASE_DATA, |
2139 | "%06x:", jj); |
2140 | mmpeg4_debug_print(DECODE_ID(hw), |
2141 | PRINT_FRAMEBASE_DATA, |
2142 | "%02x ", data[jj]); |
2143 | if (((jj + 1) & 0xf) == 0) |
2144 | mmpeg4_debug_print(DECODE_ID(hw), |
2145 | PRINT_FRAMEBASE_DATA, |
2146 | "\n"); |
2147 | } |
2148 | } |
2149 | |
2150 | if (!hw->chunk->block->is_mapped) |
2151 | codec_mm_unmap_phyaddr(data); |
2152 | } |
2153 | |
2154 | mmpeg4_debug_print(DECODE_ID(hw), PRINT_FLAG_RUN_FLOW, |
2155 | "%s, size=%d, %x %x %x %x %x\n", |
2156 | __func__, size, |
2157 | READ_VREG(VLD_MEM_VIFIFO_LEVEL), |
2158 | READ_VREG(VLD_MEM_VIFIFO_WP), |
2159 | READ_VREG(VLD_MEM_VIFIFO_RP), |
2160 | READ_PARSER_REG(PARSER_VIDEO_RP), |
2161 | READ_PARSER_REG(PARSER_VIDEO_WP)); |
2162 | |
2163 | hw->dec_result = DEC_RESULT_NONE; |
2164 | if (vdec->mc_loaded) { |
2165 | /*firmware have load before, |
2166 | and not changes to another. |
2167 | ignore reload. |
2168 | */ |
2169 | } else { |
2170 | ret = amvdec_vdec_loadmc_buf_ex(VFORMAT_MPEG4,hw->fw->name, vdec, |
2171 | hw->fw->data, hw->fw->len); |
2172 | if (ret < 0) { |
2173 | pr_err("[%d] %s: the %s fw loading failed, err: %x\n", vdec->id, |
2174 | hw->fw->name, tee_enabled() ? "TEE" : "local", ret); |
2175 | hw->dec_result = DEC_RESULT_FORCE_EXIT; |
2176 | vdec_schedule_work(&hw->work); |
2177 | return; |
2178 | } |
2179 | vdec->mc_loaded = 1; |
2180 | vdec->mc_type = VFORMAT_MPEG4; |
2181 | } |
2182 | if (vmpeg4_hw_ctx_restore(hw) < 0) { |
2183 | hw->dec_result = DEC_RESULT_ERROR; |
2184 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
2185 | "amvdec_mpeg4: error HW context restore\n"); |
2186 | vdec_schedule_work(&hw->work); |
2187 | return; |
2188 | } |
2189 | if (vdec_frame_based(vdec)) { |
2190 | size = hw->chunk_size + |
2191 | (hw->chunk_offset & (VDEC_FIFO_ALIGN - 1)); |
2192 | WRITE_VREG(VIFF_BIT_CNT, size * 8); |
2193 | } |
2194 | hw->input_empty = 0; |
2195 | hw->last_vld_level = 0; |
2196 | start_process_time(hw); |
2197 | vdec_enable_input(vdec); |
2198 | /* wmb before ISR is handled */ |
2199 | wmb(); |
2200 | |
2201 | amvdec_start(); |
2202 | hw->stat |= STAT_VDEC_RUN; |
2203 | hw->init_flag = 1; |
2204 | mod_timer(&hw->check_timer, jiffies + CHECK_INTERVAL); |
2205 | } |
2206 | |
2207 | static int vmpeg4_stop(struct vdec_mpeg4_hw_s *hw) |
2208 | { |
2209 | cancel_work_sync(&hw->work); |
2210 | |
2211 | if (hw->mm_blk_handle) { |
2212 | decoder_bmmu_box_free(hw->mm_blk_handle); |
2213 | hw->mm_blk_handle = NULL; |
2214 | } |
2215 | |
2216 | if (hw->stat & STAT_TIMER_ARM) { |
2217 | del_timer_sync(&hw->check_timer); |
2218 | hw->stat &= ~STAT_TIMER_ARM; |
2219 | } |
2220 | |
2221 | if (hw->fw) { |
2222 | vfree(hw->fw); |
2223 | hw->fw = NULL; |
2224 | } |
2225 | return 0; |
2226 | } |
2227 | static void reset(struct vdec_s *vdec) |
2228 | { |
2229 | struct vdec_mpeg4_hw_s *hw = (struct vdec_mpeg4_hw_s *)vdec->private; |
2230 | |
2231 | pr_info("amvdec_mmpeg4: reset.\n"); |
2232 | |
2233 | vmpeg4_local_init(hw); |
2234 | |
2235 | hw->ctx_valid = 0; |
2236 | } |
2237 | |
2238 | static int ammvdec_mpeg4_probe(struct platform_device *pdev) |
2239 | { |
2240 | struct vdec_s *pdata = *(struct vdec_s **)pdev->dev.platform_data; |
2241 | struct vdec_mpeg4_hw_s *hw = NULL; |
2242 | |
2243 | if (pdata == NULL) { |
2244 | pr_err("%s memory resource undefined.\n", __func__); |
2245 | return -EFAULT; |
2246 | } |
2247 | |
2248 | hw = vmalloc(sizeof(struct vdec_mpeg4_hw_s)); |
2249 | if (hw == NULL) { |
2250 | pr_err("\namvdec_mpeg4 decoder driver alloc failed\n"); |
2251 | return -ENOMEM; |
2252 | } |
2253 | memset(hw, 0, sizeof(struct vdec_mpeg4_hw_s)); |
2254 | |
2255 | /* the ctx from v4l2 driver. */ |
2256 | hw->v4l2_ctx = pdata->private; |
2257 | |
2258 | pdata->private = hw; |
2259 | pdata->dec_status = dec_status; |
2260 | /* pdata->set_trickmode = set_trickmode; */ |
2261 | pdata->run_ready = run_ready; |
2262 | pdata->run = run; |
2263 | pdata->reset = reset; |
2264 | pdata->irq_handler = vmpeg4_isr; |
2265 | pdata->threaded_irq_handler = vmpeg4_isr_thread_fn; |
2266 | pdata->dump_state = vmpeg4_dump_state; |
2267 | |
2268 | if (pdata->use_vfm_path) |
2269 | snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE, |
2270 | VFM_DEC_PROVIDER_NAME); |
2271 | else |
2272 | snprintf(pdata->vf_provider_name, VDEC_PROVIDER_NAME_SIZE, |
2273 | PROVIDER_NAME ".%02x", pdev->id & 0xff); |
2274 | |
2275 | if (pdata->parallel_dec == 1) { |
2276 | int i; |
2277 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) |
2278 | hw->canvas_spec[i] = 0xffffff; |
2279 | } |
2280 | |
2281 | vf_provider_init(&pdata->vframe_provider, |
2282 | pdata->vf_provider_name, &vf_provider_ops, pdata); |
2283 | |
2284 | platform_set_drvdata(pdev, pdata); |
2285 | hw->platform_dev = pdev; |
2286 | hw->blkmode = pdata->canvas_mode; |
2287 | |
2288 | if (pdata->sys_info) { |
2289 | hw->vmpeg4_amstream_dec_info = *pdata->sys_info; |
2290 | if ((hw->vmpeg4_amstream_dec_info.height != 0) && |
2291 | (hw->vmpeg4_amstream_dec_info.width > |
2292 | (MAX_MPEG4_SUPPORT_SIZE/hw->vmpeg4_amstream_dec_info.height))) { |
2293 | pr_info("ammvdec_mpeg4: oversize, unsupport: %d*%d\n", |
2294 | hw->vmpeg4_amstream_dec_info.width, |
2295 | hw->vmpeg4_amstream_dec_info.height); |
2296 | pdata->dec_status = NULL; |
2297 | vfree((void *)hw); |
2298 | hw = NULL; |
2299 | return -EFAULT; |
2300 | } |
2301 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
2302 | "sysinfo: %d x %d, rate: %d\n", |
2303 | hw->vmpeg4_amstream_dec_info.width, |
2304 | hw->vmpeg4_amstream_dec_info.height, |
2305 | hw->vmpeg4_amstream_dec_info.rate); |
2306 | hw->is_used_v4l = (((unsigned long) |
2307 | hw->vmpeg4_amstream_dec_info.param & 0x80) >> 7); |
2308 | } |
2309 | |
2310 | if (vmmpeg4_init(hw) < 0) { |
2311 | pr_err("%s init failed.\n", __func__); |
2312 | |
2313 | if (hw) { |
2314 | vfree((void *)hw); |
2315 | hw = NULL; |
2316 | } |
2317 | pdata->dec_status = NULL; |
2318 | return -ENODEV; |
2319 | } |
2320 | |
2321 | if (pdata->parallel_dec == 1) |
2322 | vdec_core_request(pdata, CORE_MASK_VDEC_1); |
2323 | else { |
2324 | vdec_core_request(pdata, CORE_MASK_VDEC_1 | CORE_MASK_HEVC |
2325 | | CORE_MASK_COMBINE); |
2326 | } |
2327 | |
2328 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
2329 | "%s end.\n", __func__); |
2330 | return 0; |
2331 | } |
2332 | |
2333 | static int ammvdec_mpeg4_remove(struct platform_device *pdev) |
2334 | { |
2335 | struct vdec_mpeg4_hw_s *hw = |
2336 | (struct vdec_mpeg4_hw_s *) |
2337 | (((struct vdec_s *)(platform_get_drvdata(pdev)))->private); |
2338 | struct vdec_s *vdec = hw_to_vdec(hw); |
2339 | int i; |
2340 | |
2341 | if (vdec->next_status == VDEC_STATUS_DISCONNECTED |
2342 | && (vdec->status == VDEC_STATUS_ACTIVE)) { |
2343 | mmpeg4_debug_print(DECODE_ID(hw), 0, |
2344 | "%s force exit %d\n", __func__, __LINE__); |
2345 | hw->dec_result = DEC_RESULT_FORCE_EXIT; |
2346 | vdec_schedule_work(&hw->work); |
2347 | wait_event_interruptible_timeout(hw->wait_q, |
2348 | (vdec->status == VDEC_STATUS_CONNECTED), |
2349 | msecs_to_jiffies(1000)); /* wait for work done */ |
2350 | } |
2351 | |
2352 | vmpeg4_stop(hw); |
2353 | |
2354 | if (vdec->parallel_dec == 1) |
2355 | vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1); |
2356 | else |
2357 | vdec_core_release(hw_to_vdec(hw), CORE_MASK_VDEC_1 | CORE_MASK_HEVC); |
2358 | vdec_set_status(hw_to_vdec(hw), VDEC_STATUS_DISCONNECTED); |
2359 | |
2360 | if (vdec->parallel_dec == 1) { |
2361 | for (i = 0; i < DECODE_BUFFER_NUM_MAX; i++) { |
2362 | vdec->free_canvas_ex(canvas_y(hw->canvas_spec[i]), vdec->id); |
2363 | vdec->free_canvas_ex(canvas_u(hw->canvas_spec[i]), vdec->id); |
2364 | } |
2365 | } |
2366 | |
2367 | mmpeg4_debug_print(DECODE_ID(hw), 0, "%s\n", __func__); |
2368 | vfree((void *)hw); |
2369 | hw = NULL; |
2370 | |
2371 | return 0; |
2372 | } |
2373 | |
2374 | /****************************************/ |
2375 | #ifdef CONFIG_PM |
2376 | static int mmpeg4_suspend(struct device *dev) |
2377 | { |
2378 | amvdec_suspend(to_platform_device(dev), dev->power.power_state); |
2379 | return 0; |
2380 | } |
2381 | |
2382 | static int mmpeg4_resume(struct device *dev) |
2383 | { |
2384 | amvdec_resume(to_platform_device(dev)); |
2385 | return 0; |
2386 | } |
2387 | |
2388 | static const struct dev_pm_ops mmpeg4_pm_ops = { |
2389 | SET_SYSTEM_SLEEP_PM_OPS(mmpeg4_suspend, mmpeg4_resume) |
2390 | }; |
2391 | #endif |
2392 | |
2393 | static struct platform_driver ammvdec_mpeg4_driver = { |
2394 | .probe = ammvdec_mpeg4_probe, |
2395 | .remove = ammvdec_mpeg4_remove, |
2396 | .driver = { |
2397 | .name = DRIVER_NAME, |
2398 | #ifdef CONFIG_PM |
2399 | .pm = &mmpeg4_pm_ops, |
2400 | #endif |
2401 | } |
2402 | }; |
2403 | |
2404 | static struct codec_profile_t amvdec_mpeg4_profile = { |
2405 | .name = "mmpeg4", |
2406 | .profile = "" |
2407 | }; |
2408 | |
2409 | static int __init ammvdec_mpeg4_driver_init_module(void) |
2410 | { |
2411 | pr_info("%s \n", __func__); |
2412 | |
2413 | if (platform_driver_register(&ammvdec_mpeg4_driver)) { |
2414 | pr_err("failed to register ammvdec_mpeg4 driver\n"); |
2415 | return -ENODEV; |
2416 | } |
2417 | vcodec_profile_register(&amvdec_mpeg4_profile); |
2418 | return 0; |
2419 | } |
2420 | |
2421 | static void __exit ammvdec_mpeg4_driver_remove_module(void) |
2422 | { |
2423 | pr_info("ammvdec_mpeg4 module remove.\n"); |
2424 | |
2425 | platform_driver_unregister(&ammvdec_mpeg4_driver); |
2426 | } |
2427 | |
2428 | /****************************************/ |
2429 | module_param(debug_enable, uint, 0664); |
2430 | MODULE_PARM_DESC(debug_enable, |
2431 | "\n ammvdec_mpeg4 debug enable\n"); |
2432 | |
2433 | module_param(frmbase_cont_bitlevel, uint, 0664); |
2434 | MODULE_PARM_DESC(frmbase_cont_bitlevel, "\nfrmbase_cont_bitlevel\n"); |
2435 | |
2436 | module_param(radr, uint, 0664); |
2437 | MODULE_PARM_DESC(radr, "\nradr\n"); |
2438 | |
2439 | module_param(rval, uint, 0664); |
2440 | MODULE_PARM_DESC(rval, "\nrval\n"); |
2441 | |
2442 | module_param(decode_timeout_val, uint, 0664); |
2443 | MODULE_PARM_DESC(decode_timeout_val, "\n ammvdec_mpeg4 decode_timeout_val\n"); |
2444 | |
2445 | module_param_array(max_process_time, uint, &max_decode_instance_num, 0664); |
2446 | |
2447 | module_param(pre_decode_buf_level, int, 0664); |
2448 | MODULE_PARM_DESC(pre_decode_buf_level, |
2449 | "\n ammvdec_ mpeg4 pre_decode_buf_level\n"); |
2450 | |
2451 | module_param(udebug_flag, uint, 0664); |
2452 | MODULE_PARM_DESC(udebug_flag, "\n ammvdec_mpeg4 udebug_flag\n"); |
2453 | |
2454 | module_param(without_display_mode, uint, 0664); |
2455 | MODULE_PARM_DESC(without_display_mode, "\n ammvdec_mpeg4 without_display_mode\n"); |
2456 | |
2457 | module_init(ammvdec_mpeg4_driver_init_module); |
2458 | module_exit(ammvdec_mpeg4_driver_remove_module); |
2459 | |
2460 | MODULE_DESCRIPTION("AMLOGIC MPEG4 Video Decoder Driver"); |
2461 | MODULE_LICENSE("GPL"); |
2462 | MODULE_AUTHOR("Tim Yao <timyao@amlogic.com>"); |
2463 | |
2464 |