summaryrefslogtreecommitdiff
path: root/drivers/frame_provider/decoder/avs2/avs2_bufmgr.c (plain)
blob: 3da87e41b815a603b523680b1d37dac166c1a33d
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/kernel.h>
21#include <linux/module.h>
22#include <linux/types.h>
23#include <linux/errno.h>
24#include <linux/interrupt.h>
25#include <linux/semaphore.h>
26#include <linux/delay.h>
27#include <linux/timer.h>
28#include <linux/kfifo.h>
29#include <linux/kthread.h>
30#include <linux/spinlock.h>
31#include <linux/platform_device.h>
32#include <linux/amlogic/media/vfm/vframe.h>
33#include <linux/amlogic/media/utils/amstream.h>
34#include <linux/amlogic/media/utils/vformat.h>
35#include <linux/amlogic/media/frame_sync/ptsserv.h>
36#include <linux/amlogic/media/canvas/canvas.h>
37#include <linux/amlogic/media/vfm/vframe_provider.h>
38#include <linux/amlogic/media/vfm/vframe_receiver.h>
39#include <linux/dma-mapping.h>
40#include <linux/dma-contiguous.h>
41#include <linux/slab.h>
42#include <linux/amlogic/tee.h>
43#include "../../../stream_input/amports/amports_priv.h"
44#include <linux/amlogic/media/codec_mm/codec_mm.h>
45#include "../utils/decoder_mmu_box.h"
46#include "../utils/decoder_bmmu_box.h"
47#include "avs2_global.h"
48
49#include <linux/amlogic/media/utils/vdec_reg.h>
50#include "../utils/vdec.h"
51#include "../utils/amvdec.h"
52
53#undef pr_info
54#define pr_info printk
55
56#define assert(chk_cond) {\
57 if (!(chk_cond))\
58 pr_info("error line %d\n", __LINE__);\
59 while (!(chk_cond))\
60 ;\
61}
62
63int16_t get_param(uint16_t value, int8_t *print_info)
64{
65 if (is_avs2_print_param())
66 pr_info("%s = %x\n", print_info, value);
67 return (int16_t)value;
68}
69
70void readAlfCoeff(struct avs2_decoder *avs2_dec, struct ALFParam_s *Alfp)
71{
72 int32_t pos;
73 union param_u *rpm_param = &avs2_dec->param;
74
75 int32_t f = 0, symbol, pre_symbole;
76 const int32_t numCoeff = (int32_t)ALF_MAX_NUM_COEF;
77
78 switch (Alfp->componentID) {
79 case ALF_Cb:
80 case ALF_Cr: {
81 for (pos = 0; pos < numCoeff; pos++) {
82 if (Alfp->componentID == ALF_Cb)
83 Alfp->coeffmulti[0][pos] =
84 get_param(
85 rpm_param->alf.alf_cb_coeffmulti[pos],
86 "Chroma ALF coefficients");
87 else
88 Alfp->coeffmulti[0][pos] =
89 get_param(
90 rpm_param->alf.alf_cr_coeffmulti[pos],
91 "Chroma ALF coefficients");
92#if Check_Bitstream
93 if (pos <= 7)
94 assert(Alfp->coeffmulti[0][pos] >= -64
95 && Alfp->coeffmulti[0][pos] <= 63);
96 if (pos == 8)
97 assert(Alfp->coeffmulti[0][pos] >= -1088
98 && Alfp->coeffmulti[0][pos] <= 1071);
99#endif
100 }
101 }
102 break;
103 case ALF_Y: {
104 int32_t region_distance_idx = 0;
105 Alfp->filters_per_group =
106 get_param(rpm_param->alf.alf_filters_num_m_1,
107 "ALF_filter_number_minus_1");
108#if Check_Bitstream
109 assert(Alfp->filters_per_group >= 0
110 && Alfp->filters_per_group <= 15);
111#endif
112 Alfp->filters_per_group = Alfp->filters_per_group + 1;
113
114 memset(Alfp->filterPattern, 0, NO_VAR_BINS * sizeof(int32_t));
115 pre_symbole = 0;
116 symbol = 0;
117 for (f = 0; f < Alfp->filters_per_group; f++) {
118 if (f > 0) {
119 if (Alfp->filters_per_group != 16) {
120 symbol =
121 get_param(rpm_param->alf.region_distance
122 [region_distance_idx++],
123 "Region distance");
124 } else {
125 symbol = 1;
126 }
127 Alfp->filterPattern[symbol + pre_symbole] = 1;
128 pre_symbole = symbol + pre_symbole;
129 }
130
131 for (pos = 0; pos < numCoeff; pos++) {
132 Alfp->coeffmulti[f][pos] =
133 get_param(
134 rpm_param->alf.alf_y_coeffmulti[f][pos],
135 "Luma ALF coefficients");
136#if Check_Bitstream
137 if (pos <= 7)
138 assert(
139 Alfp->coeffmulti[f][pos]
140 >= -64 &&
141 Alfp->coeffmulti[f][pos]
142 <= 63);
143 if (pos == 8)
144 assert(
145 Alfp->coeffmulti[f][pos]
146 >= -1088 &&
147 Alfp->coeffmulti[f][pos]
148 <= 1071);
149#endif
150
151 }
152 }
153
154#if Check_Bitstream
155 assert(pre_symbole >= 0 && pre_symbole <= 15);
156
157#endif
158 }
159 break;
160 default: {
161 pr_info("Not a legal component ID\n");
162 assert(0);
163 return; /* exit(-1);*/
164 }
165 }
166}
167
168void Read_ALF_param(struct avs2_decoder *avs2_dec)
169{
170 struct inp_par *input = &avs2_dec->input;
171 struct ImageParameters_s *img = &avs2_dec->img;
172 union param_u *rpm_param = &avs2_dec->param;
173 int32_t compIdx;
174 if (input->alf_enable) {
175 img->pic_alf_on[0] =
176 get_param(
177 rpm_param->alf.picture_alf_enable_Y,
178 "alf_pic_flag_Y");
179 img->pic_alf_on[1] =
180 get_param(
181 rpm_param->alf.picture_alf_enable_Cb,
182 "alf_pic_flag_Cb");
183 img->pic_alf_on[2] =
184 get_param(
185 rpm_param->alf.picture_alf_enable_Cr,
186 "alf_pic_flag_Cr");
187
188 avs2_dec->m_alfPictureParam[ALF_Y].alf_flag
189 = img->pic_alf_on[ALF_Y];
190 avs2_dec->m_alfPictureParam[ALF_Cb].alf_flag
191 = img->pic_alf_on[ALF_Cb];
192 avs2_dec->m_alfPictureParam[ALF_Cr].alf_flag
193 = img->pic_alf_on[ALF_Cr];
194 if (img->pic_alf_on[0]
195 || img->pic_alf_on[1]
196 || img->pic_alf_on[2]) {
197 for (compIdx = 0;
198 compIdx < NUM_ALF_COMPONENT;
199 compIdx++) {
200 if (img->pic_alf_on[compIdx]) {
201 readAlfCoeff(
202 avs2_dec,
203 &avs2_dec->m_alfPictureParam[compIdx]);
204 }
205 }
206 }
207 }
208
209}
210
211void Get_SequenceHeader(struct avs2_decoder *avs2_dec)
212{
213 struct inp_par *input = &avs2_dec->input;
214 struct ImageParameters_s *img = &avs2_dec->img;
215 struct Video_Com_data_s *hc = &avs2_dec->hc;
216 struct Video_Dec_data_s *hd = &avs2_dec->hd;
217 union param_u *rpm_param = &avs2_dec->param;
218 /*int32_t i, j;*/
219
220 /*fpr_info(stdout, "Sequence Header\n");*/
221 /*memcpy(currStream->streamBuffer, buf, length);*/
222 /*currStream->code_len = currStream->bitstream_length = length;*/
223 /*currStream->read_len = currStream->frame_bitoffset = (startcodepos +
224 1) * 8;*/
225
226 input->profile_id =
227 get_param(rpm_param->p.profile_id, "profile_id");
228 input->level_id =
229 get_param(rpm_param->p.level_id, "level_id");
230 hd->progressive_sequence =
231 get_param(
232 rpm_param->p.progressive_sequence,
233 "progressive_sequence");
234#if INTERLACE_CODING
235 hd->is_field_sequence =
236 get_param(
237 rpm_param->p.is_field_sequence,
238 "field_coded_sequence");
239#endif
240#if HALF_PIXEL_COMPENSATION || HALF_PIXEL_CHROMA
241 img->is_field_sequence = hd->is_field_sequence;
242#endif
243 hd->horizontal_size =
244 get_param(rpm_param->p.horizontal_size, "horizontal_size");
245 hd->vertical_size =
246 get_param(rpm_param->p.vertical_size, "vertical_size");
247 input->chroma_format =
248 get_param(rpm_param->p.chroma_format, "chroma_format");
249 input->output_bit_depth = 8;
250 input->sample_bit_depth = 8;
251 hd->sample_precision = 1;
252 if (input->profile_id == BASELINE10_PROFILE) { /* 10bit profile (0x52)*/
253 input->output_bit_depth =
254 get_param(rpm_param->p.sample_precision,
255 "sample_precision");
256 input->output_bit_depth =
257 6 + (input->output_bit_depth) * 2;
258 input->sample_bit_depth =
259 get_param(rpm_param->p.encoding_precision,
260 "encoding_precision");
261 input->sample_bit_depth =
262 6 + (input->sample_bit_depth) * 2;
263 } else { /* other profile*/
264 hd->sample_precision =
265 get_param(rpm_param->p.sample_precision,
266 "sample_precision");
267 }
268 hd->aspect_ratio_information =
269 get_param(rpm_param->p.aspect_ratio_information,
270 "aspect_ratio_information");
271 hd->frame_rate_code =
272 get_param(rpm_param->p.frame_rate_code, "frame_rate_code");
273
274 hd->bit_rate_lower =
275 get_param(rpm_param->p.bit_rate_lower, "bit_rate_lower");
276 /*hd->marker_bit = get_param(rpm_param->p.marker_bit,
277 * "marker bit");*/
278 /*CHECKMARKERBIT*/
279 hd->bit_rate_upper =
280 get_param(rpm_param->p.bit_rate_upper, "bit_rate_upper");
281 hd->low_delay =
282 get_param(rpm_param->p.low_delay, "low_delay");
283 /*hd->marker_bit =
284 get_param(rpm_param->p.marker_bit2,
285 "marker bit");*/
286 /*CHECKMARKERBIT*/
287#if M3480_TEMPORAL_SCALABLE
288 hd->temporal_id_exist_flag =
289 get_param(rpm_param->p.temporal_id_exist_flag,
290 "temporal_id exist flag"); /*get
291 Extention Flag*/
292#endif
293 /*u_v(18, "bbv buffer size");*/
294 input->g_uiMaxSizeInBit =
295 get_param(rpm_param->p.g_uiMaxSizeInBit,
296 "Largest Coding Block Size");
297
298
299 /*hd->background_picture_enable = 0x01 ^
300 (get_param(rpm_param->p.avs2_seq_flags,
301 "background_picture_disable")
302 >> BACKGROUND_PICTURE_DISABLE_BIT) & 0x1;*/
303 /*rain???*/
304 hd->background_picture_enable = 0x01 ^
305 ((get_param(rpm_param->p.avs2_seq_flags,
306 "background_picture_disable")
307 >> BACKGROUND_PICTURE_DISABLE_BIT) & 0x1);
308
309
310 hd->b_dmh_enabled = 1;
311
312 hd->b_mhpskip_enabled =
313 get_param(rpm_param->p.avs2_seq_flags >> B_MHPSKIP_ENABLED_BIT,
314 "mhpskip enabled") & 0x1;
315 hd->dhp_enabled =
316 get_param(rpm_param->p.avs2_seq_flags >> DHP_ENABLED_BIT,
317 "dhp enabled") & 0x1;
318 hd->wsm_enabled =
319 get_param(rpm_param->p.avs2_seq_flags >> WSM_ENABLED_BIT,
320 "wsm enabled") & 0x1;
321
322 img->inter_amp_enable =
323 get_param(rpm_param->p.avs2_seq_flags >> INTER_AMP_ENABLE_BIT,
324 "Asymmetric Motion Partitions") & 0x1;
325 input->useNSQT =
326 get_param(rpm_param->p.avs2_seq_flags >> USENSQT_BIT,
327 "useNSQT") & 0x1;
328 input->useSDIP =
329 get_param(rpm_param->p.avs2_seq_flags >> USESDIP_BIT,
330 "useNSIP") & 0x1;
331
332 hd->b_secT_enabled =
333 get_param(rpm_param->p.avs2_seq_flags >> B_SECT_ENABLED_BIT,
334 "secT enabled") & 0x1;
335
336 input->sao_enable =
337 get_param(rpm_param->p.avs2_seq_flags >> SAO_ENABLE_BIT,
338 "SAO Enable Flag") & 0x1;
339 input->alf_enable =
340 get_param(rpm_param->p.avs2_seq_flags >> ALF_ENABLE_BIT,
341 "ALF Enable Flag") & 0x1;
342 hd->b_pmvr_enabled =
343 get_param(rpm_param->p.avs2_seq_flags >> B_PMVR_ENABLED_BIT,
344 "pmvr enabled") & 0x1;
345
346
347 hd->gop_size = get_param(rpm_param->p.num_of_RPS,
348 "num_of_RPS");
349#if Check_Bitstream
350 /*assert(hd->gop_size<=32);*/
351#endif
352
353 if (hd->low_delay == 0) {
354 hd->picture_reorder_delay =
355 get_param(rpm_param->p.picture_reorder_delay,
356 "picture_reorder_delay");
357 }
358
359 input->crossSliceLoopFilter =
360 get_param(rpm_param->p.avs2_seq_flags
361 >> CROSSSLICELOOPFILTER_BIT,
362 "Cross Loop Filter Flag") & 0x1;
363
364#if BCBR
365 if ((input->profile_id == SCENE_PROFILE ||
366 input->profile_id == SCENE10_PROFILE) &&
367 hd->background_picture_enable) {
368 hd->bcbr_enable = u_v(1,
369 "block_composed_background_picture_enable");
370 u_v(1, "reserved bits");
371 } else {
372 hd->bcbr_enable = 0;
373 u_v(2, "reserved bits");
374 }
375#else
376 /*u_v(2, "reserved bits");*/
377#endif
378
379 img->width = hd->horizontal_size;
380 img->height = hd->vertical_size;
381 img->width_cr = (img->width >> 1);
382
383 if (input->chroma_format == 1) {
384 img->height_cr
385 = (img->height >> 1);
386 }
387
388 img->PicWidthInMbs = img->width / MIN_CU_SIZE;
389 img->PicHeightInMbs = img->height / MIN_CU_SIZE;
390 img->PicSizeInMbs = img->PicWidthInMbs * img->PicHeightInMbs;
391 img->buf_cycle = input->buf_cycle + 1;
392 img->max_mb_nr = (img->width * img->height)
393 / (MIN_CU_SIZE * MIN_CU_SIZE);
394
395#ifdef AML
396avs2_dec->lcu_size =
397 get_param(rpm_param->p.lcu_size, "lcu_size");
398avs2_dec->lcu_size = 1<<(avs2_dec->lcu_size);
399#endif
400hc->seq_header++;
401}
402
403
404void Get_I_Picture_Header(struct avs2_decoder *avs2_dec)
405{
406 struct ImageParameters_s *img = &avs2_dec->img;
407 struct Video_Dec_data_s *hd = &avs2_dec->hd;
408 union param_u *rpm_param = &avs2_dec->param;
409
410#if RD1501_FIX_BG /*//Longfei.Wang@mediatek.com*/
411 hd->background_picture_flag = 0;
412 hd->background_picture_output_flag = 0;
413 img->typeb = 0;
414#endif
415
416 hd->time_code_flag =
417 get_param(rpm_param->p.time_code_flag,
418 "time_code_flag");
419
420 if (hd->time_code_flag) {
421 hd->time_code =
422 get_param(rpm_param->p.time_code,
423 "time_code");
424 }
425 if (hd->background_picture_enable) {
426 hd->background_picture_flag =
427 get_param(rpm_param->p.background_picture_flag,
428 "background_picture_flag");
429
430 if (hd->background_picture_flag) {
431 img->typeb =
432 BACKGROUND_IMG;
433 } else {
434 img->typeb = 0;
435 }
436
437 if (img->typeb == BACKGROUND_IMG) {
438 hd->background_picture_output_flag =
439 get_param(
440 rpm_param->p.background_picture_output_flag,
441 "background_picture_output_flag");
442 }
443 }
444
445
446 {
447 img->coding_order =
448 get_param(rpm_param->p.coding_order,
449 "coding_order");
450
451
452
453#if M3480_TEMPORAL_SCALABLE
454 if (hd->temporal_id_exist_flag == 1) {
455 hd->cur_layer =
456 get_param(rpm_param->p.cur_layer,
457 "temporal_id");
458 }
459#endif
460#if RD1501_FIX_BG /*Longfei.Wang@mediatek.com*/
461 if (hd->low_delay == 0
462 && !(hd->background_picture_flag &&
463 !hd->background_picture_output_flag)) { /*cdp*/
464#else
465 if (hd->low_delay == 0 &&
466 !(hd->background_picture_enable &&
467 !hd->background_picture_output_flag)) { /*cdp*/
468#endif
469 hd->displaydelay =
470 get_param(rpm_param->p.displaydelay,
471 "picture_output_delay");
472 }
473
474 }
475 {
476 int32_t RPS_idx;/* = (img->coding_order-1) % gop_size;*/
477 int32_t predict;
478 int32_t j;
479 predict =
480 get_param(rpm_param->p.predict,
481 "use RCS in SPS");
482 /*if (predict) {*/
483 RPS_idx =
484 get_param(rpm_param->p.RPS_idx,
485 "predict for RCS");
486 /* hd->curr_RPS = hd->decod_RPS[RPS_idx];*/
487 /*} else {*/
488 /*gop size16*/
489 hd->curr_RPS.referd_by_others =
490 get_param(rpm_param->p.referd_by_others_cur,
491 "refered by others");
492 hd->curr_RPS.num_of_ref =
493 get_param(rpm_param->p.num_of_ref_cur,
494 "num of reference picture");
495 for (j = 0; j < hd->curr_RPS.num_of_ref; j++) {
496 hd->curr_RPS.ref_pic[j] =
497 get_param(rpm_param->p.ref_pic_cur[j],
498 "delta COI of ref pic");
499 }
500 hd->curr_RPS.num_to_remove =
501 get_param(rpm_param->p.num_to_remove_cur,
502 "num of removed picture");
503#ifdef SANITY_CHECK
504 if (hd->curr_RPS.num_to_remove > MAXREF) {
505 hd->curr_RPS.num_to_remove = MAXREF;
506 pr_info("Warning, %s: num_to_remove %d beyond range, force to MAXREF\n",
507 __func__, hd->curr_RPS.num_to_remove);
508 }
509#endif
510
511 for (j = 0; j < hd->curr_RPS.num_to_remove; j++) {
512 hd->curr_RPS.remove_pic[j] =
513 get_param(
514 rpm_param->p.remove_pic_cur[j],
515 "delta COI of removed pic");
516 }
517 /*u_v(1, "marker bit");*/
518
519 /*}*/
520 }
521 /*xyji 12.23*/
522 if (hd->low_delay) {
523 /*ue_v(
524 "bbv check times");*/
525 }
526
527 hd->progressive_frame =
528 get_param(rpm_param->p.progressive_frame,
529 "progressive_frame");
530
531 if (!hd->progressive_frame) {
532 img->picture_structure =
533 get_param(rpm_param->p.picture_structure,
534 "picture_structure");
535 } else {
536 img->picture_structure
537 = 1;
538 }
539
540 hd->top_field_first =
541 get_param(rpm_param->p.top_field_first,
542 "top_field_first");
543 hd->repeat_first_field =
544 get_param(rpm_param->p.repeat_first_field,
545 "repeat_first_field");
546#if INTERLACE_CODING
547 if (hd->is_field_sequence) {
548 hd->is_top_field =
549 get_param(rpm_param->p.is_top_field,
550 "is_top_field");
551#if HALF_PIXEL_COMPENSATION || HALF_PIXEL_CHROMA
552 img->is_top_field = hd->is_top_field;
553#endif
554 }
555#endif
556
557
558 img->qp = hd->picture_qp;
559
560 img->type = I_IMG;
561
562}
563
564/*
565 * Function:pb picture header
566 * Input:
567 * Output:
568 * Return:
569 * Attention:
570 */
571
572void Get_PB_Picture_Header(struct avs2_decoder *avs2_dec)
573{
574 struct ImageParameters_s *img = &avs2_dec->img;
575 struct Video_Dec_data_s *hd = &avs2_dec->hd;
576 union param_u *rpm_param = &avs2_dec->param;
577
578
579 /*u_v(32, "bbv delay");*/
580
581 hd->picture_coding_type =
582 get_param(rpm_param->p.picture_coding_type,
583 "picture_coding_type");
584
585 if (hd->background_picture_enable &&
586 (hd->picture_coding_type == 1 ||
587 hd->picture_coding_type == 3)) {
588 if (hd->picture_coding_type == 1) {
589 hd->background_pred_flag =
590 get_param(
591 rpm_param->p.background_pred_flag,
592 "background_pred_flag");
593 } else {
594 hd->background_pred_flag = 0;
595 }
596 if (hd->background_pred_flag == 0) {
597
598 hd->background_reference_enable =
599 get_param(
600 rpm_param->
601 p.background_reference_enable,
602 "background_reference_enable");
603
604 } else {
605#if RD170_FIX_BG
606 hd->background_reference_enable = 1;
607#else
608 hd->background_reference_enable = 0;
609#endif
610 }
611
612 } else {
613 hd->background_pred_flag = 0;
614 hd->background_reference_enable = 0;
615 }
616
617
618
619 if (hd->picture_coding_type == 1) {
620 img->type =
621 P_IMG;
622 } else if (hd->picture_coding_type == 3) {
623 img->type =
624 F_IMG;
625 } else {
626 img->type =
627 B_IMG;
628 }
629
630
631 if (hd->picture_coding_type == 1 &&
632 hd->background_pred_flag) {
633 img->typeb = BP_IMG;
634 } else {
635 img->typeb = 0;
636 }
637
638
639 {
640 img->coding_order =
641 get_param(
642 rpm_param->p.coding_order,
643 "coding_order");
644
645
646#if M3480_TEMPORAL_SCALABLE
647 if (hd->temporal_id_exist_flag == 1) {
648 hd->cur_layer =
649 get_param(rpm_param->p.cur_layer,
650 "temporal_id");
651 }
652#endif
653
654 if (hd->low_delay == 0) {
655 hd->displaydelay =
656 get_param(rpm_param->p.displaydelay,
657 "displaydelay");
658 }
659 }
660 {
661 int32_t RPS_idx;/* = (img->coding_order-1) % gop_size;*/
662 int32_t predict;
663 predict =
664 get_param(rpm_param->p.predict,
665 "use RPS in SPS");
666 if (predict) {
667 RPS_idx =
668 get_param(rpm_param->p.RPS_idx,
669 "predict for RPS");
670 hd->curr_RPS = hd->decod_RPS[RPS_idx];
671 } /*else*/
672 {
673 /*gop size16*/
674 int32_t j;
675 hd->curr_RPS.referd_by_others =
676 get_param(
677 rpm_param->p.referd_by_others_cur,
678 "refered by others");
679 hd->curr_RPS.num_of_ref =
680 get_param(
681 rpm_param->p.num_of_ref_cur,
682 "num of reference picture");
683 for (j = 0; j < hd->curr_RPS.num_of_ref; j++) {
684 hd->curr_RPS.ref_pic[j] =
685 get_param(
686 rpm_param->p.ref_pic_cur[j],
687 "delta COI of ref pic");
688 }
689 hd->curr_RPS.num_to_remove =
690 get_param(
691 rpm_param->p.num_to_remove_cur,
692 "num of removed picture");
693#ifdef SANITY_CHECK
694 if (hd->curr_RPS.num_to_remove > MAXREF) {
695 hd->curr_RPS.num_to_remove = MAXREF;
696 pr_info("Warning, %s: num_to_remove %d beyond range, force to MAXREF\n",
697 __func__, hd->curr_RPS.num_to_remove);
698 }
699#endif
700 for (j = 0;
701 j < hd->curr_RPS.num_to_remove; j++) {
702 hd->curr_RPS.remove_pic[j] =
703 get_param(
704 rpm_param->p.remove_pic_cur[j],
705 "delta COI of removed pic");
706 }
707 /*u_v(1, "marker bit");*/
708
709 }
710 }
711 /*xyji 12.23*/
712 if (hd->low_delay) {
713 /*ue_v(
714 "bbv check times");*/
715 }
716
717 hd->progressive_frame =
718 get_param(rpm_param->p.progressive_frame,
719 "progressive_frame");
720
721 if (!hd->progressive_frame) {
722 img->picture_structure =
723 get_param(rpm_param->p.picture_structure,
724 "picture_structure");
725 } else {
726 img->picture_structure = 1;
727 }
728
729 hd->top_field_first =
730 get_param(rpm_param->p.top_field_first,
731 "top_field_first");
732 hd->repeat_first_field =
733 get_param(rpm_param->p.repeat_first_field,
734 "repeat_first_field");
735#if INTERLACE_CODING
736 if (hd->is_field_sequence) {
737 hd->is_top_field =
738 get_param(rpm_param->p.is_top_field,
739 "is_top_field");
740#if HALF_PIXEL_COMPENSATION || HALF_PIXEL_CHROMA
741 img->is_top_field = hd->is_top_field;
742#endif
743 /*u_v(1, "reserved bit for interlace coding");*/
744 }
745#endif
746
747#if Check_Bitstream
748 /*assert(hd->picture_qp>=0&&hd->picture_qp<=(63 + 8 *
749 (input->sample_bit_depth - 8)));*/
750#endif
751
752 img->random_access_decodable_flag =
753 get_param(rpm_param->p.random_access_decodable_flag,
754 "random_access_decodable_flag");
755
756 img->qp = hd->picture_qp;
757}
758
759
760
761
762void calc_picture_distance(struct avs2_decoder *avs2_dec)
763{
764 struct ImageParameters_s *img = &avs2_dec->img;
765 struct Video_Com_data_s *hc = &avs2_dec->hc;
766 struct Video_Dec_data_s *hd = &avs2_dec->hd;
767 /*
768 union param_u *rpm_param = &avs2_dec->param;
769
770 for POC mode 0:
771 uint32_t MaxPicDistanceLsb = (1 << 8);
772 */
773 if (img->coding_order < img->PrevPicDistanceLsb)
774
775 {
776 int32_t i, j;
777
778 hc->total_frames++;
779 for (i = 0; i < avs2_dec->ref_maxbuffer; i++) {
780 if (
781 avs2_dec->fref[i]->imgtr_fwRefDistance
782 >= 0) {
783 avs2_dec->fref[i]->
784 imgtr_fwRefDistance -= 256;
785 avs2_dec->fref[i]->
786 imgcoi_ref -= 256;
787 }
788#if RD170_FIX_BG
789 for (j = 0; j < MAXREF; j++) {
790#else
791 for (j = 0; j < 4; j++) {
792#endif
793 avs2_dec->fref[i]->ref_poc[j] -= 256;
794 }
795 }
796 for (i = 0; i < avs2_dec->outprint.buffer_num; i++) {
797 avs2_dec->outprint.stdoutdata[i].framenum -= 256;
798 avs2_dec->outprint.stdoutdata[i].tr -= 256;
799 }
800
801 hd->last_output -= 256;
802 hd->curr_IDRtr -= 256;
803 hd->curr_IDRcoi -= 256;
804 hd->next_IDRtr -= 256;
805 hd->next_IDRcoi -= 256;
806 }
807 if (hd->low_delay == 0) {
808 img->tr = img->coding_order +
809 hd->displaydelay - hd->picture_reorder_delay;
810 } else {
811 img->tr =
812 img->coding_order;
813 }
814
815#if REMOVE_UNUSED
816 img->pic_distance = img->tr;
817#else
818 img->pic_distance = img->tr % 256;
819#endif
820 hc->picture_distance = img->pic_distance;
821
822}
823
824int32_t avs2_init_global_buffers(struct avs2_decoder *avs2_dec)
825{
826 struct inp_par *input = &avs2_dec->input;
827 struct ImageParameters_s *img = &avs2_dec->img;
828 struct Video_Com_data_s *hc = &avs2_dec->hc;
829
830 int32_t refnum;
831
832 int32_t memory_size = 0;
833 /*
834int32_t img_height = (hd->vertical_size + img->auto_crop_bottom);
835 */
836 img->buf_cycle = input->buf_cycle + 1;
837
838 img->buf_cycle *= 2;
839
840 hc->background_ref = hc->backgroundReferenceFrame;
841
842 for (refnum = 0; refnum < REF_MAXBUFFER; refnum++) {
843 avs2_dec->fref[refnum] = &avs2_dec->frm_pool[refnum];
844
845 /*//avs2_dec->fref[i] memory allocation*/
846 if (is_avs2_print_bufmgr_detail())
847 pr_info("[t] avs2_dec->fref[%d]@0x%p\n",
848 refnum, avs2_dec->fref[refnum]);
849 avs2_dec->fref[refnum]->imgcoi_ref = -257;
850 avs2_dec->fref[refnum]->is_output = -1;
851 avs2_dec->fref[refnum]->refered_by_others = -1;
852 avs2_dec->fref[refnum]->
853 imgtr_fwRefDistance = -256;
854 init_frame_t(avs2_dec->fref[refnum]);
855#ifdef AML
856 avs2_dec->fref[refnum]->index = refnum;
857#endif
858 }
859#ifdef AML
860 avs2_dec->f_bg = NULL;
861
862 avs2_dec->m_bg = &avs2_dec->frm_pool[REF_MAXBUFFER];
863 /*///avs2_dec->fref[i] memory allocation*/
864 if (is_avs2_print_bufmgr_detail())
865 pr_info("[t] avs2_dec->m_bg@0x%p\n",
866 avs2_dec->m_bg);
867 avs2_dec->m_bg->imgcoi_ref = -257;
868 avs2_dec->m_bg->is_output = -1;
869 avs2_dec->m_bg->refered_by_others = -1;
870 avs2_dec->m_bg->imgtr_fwRefDistance = -256;
871 init_frame_t(avs2_dec->m_bg);
872 avs2_dec->m_bg->index = refnum;
873#endif
874
875#if BCBR
876 /*init BCBR related*/
877 img->iNumCUsInFrame =
878 ((img->width + MAX_CU_SIZE - 1) / MAX_CU_SIZE)
879 * ((img->height + MAX_CU_SIZE - 1)
880 / MAX_CU_SIZE);
881 /*img->BLCUidx = (int32_t*) calloc(
882 img->iNumCUsInFrame, sizeof(int32_t));*/
883 /*memset( img->BLCUidx, 0, img->iNumCUsInFrame);*/
884#endif
885 return memory_size;
886}
887
888#ifdef AML
889static void free_unused_buffers(struct avs2_decoder *avs2_dec)
890{
891 struct inp_par *input = &avs2_dec->input;
892 struct ImageParameters_s *img = &avs2_dec->img;
893 struct Video_Com_data_s *hc = &avs2_dec->hc;
894
895 int32_t refnum;
896
897 img->buf_cycle = input->buf_cycle + 1;
898
899 img->buf_cycle *= 2;
900
901 hc->background_ref = hc->backgroundReferenceFrame;
902
903 for (refnum = 0; refnum < REF_MAXBUFFER; refnum++) {
904#ifndef NO_DISPLAY
905 if (avs2_dec->fref[refnum]->vf_ref > 0 ||
906 avs2_dec->fref[refnum]->to_prepare_disp)
907 continue;
908#endif
909 if (is_avs2_print_bufmgr_detail())
910 pr_info("%s[t] avs2_dec->fref[%d]@0x%p\n",
911 __func__, refnum, avs2_dec->fref[refnum]);
912 avs2_dec->fref[refnum]->imgcoi_ref = -257;
913 avs2_dec->fref[refnum]->is_output = -1;
914 avs2_dec->fref[refnum]->refered_by_others = -1;
915 avs2_dec->fref[refnum]->
916 imgtr_fwRefDistance = -256;
917 memset(avs2_dec->fref[refnum]->ref_poc, 0,
918 sizeof(avs2_dec->fref[refnum]->ref_poc));
919 }
920 avs2_dec->f_bg = NULL;
921
922 if (is_avs2_print_bufmgr_detail())
923 pr_info("%s[t] avs2_dec->m_bg@0x%p\n",
924 __func__, avs2_dec->m_bg);
925 avs2_dec->m_bg->imgcoi_ref = -257;
926 avs2_dec->m_bg->is_output = -1;
927 avs2_dec->m_bg->refered_by_others = -1;
928 avs2_dec->m_bg->imgtr_fwRefDistance = -256;
929 memset(avs2_dec->m_bg->ref_poc, 0,
930 sizeof(avs2_dec->m_bg->ref_poc));
931
932#if BCBR
933 /*init BCBR related*/
934 img->iNumCUsInFrame =
935 ((img->width + MAX_CU_SIZE - 1) / MAX_CU_SIZE)
936 * ((img->height + MAX_CU_SIZE - 1)
937 / MAX_CU_SIZE);
938 /*img->BLCUidx = (int32_t*) calloc(
939 img->iNumCUsInFrame, sizeof(int32_t));*/
940 /*memset( img->BLCUidx, 0, img->iNumCUsInFrame);*/
941#endif
942}
943#endif
944
945void init_frame_t(struct avs2_frame_s *currfref)
946{
947 memset(currfref, 0, sizeof(struct avs2_frame_s));
948 currfref->imgcoi_ref = -257;
949 currfref->is_output = -1;
950 currfref->refered_by_others = -1;
951 currfref->imgtr_fwRefDistance = -256;
952 memset(currfref->ref_poc, 0, sizeof(currfref->ref_poc));
953}
954
955void get_reference_list_info(struct avs2_decoder *avs2_dec, int8_t *str)
956{
957 struct ImageParameters_s *img = &avs2_dec->img;
958 struct Video_Com_data_s *hc = &avs2_dec->hc;
959
960 int8_t str_tmp[16];
961 int32_t i;
962 /* int32_t poc = hc->f_rec->imgtr_fwRefDistance;
963 fred.chiu@mediatek.com*/
964
965 if (img->num_of_references > 0) {
966 strcpy(str, "[");
967 for (i = 0; i < img->num_of_references; i++) {
968#if RD1510_FIX_BG
969 if (img->type == B_IMG) {
970 sprintf(str_tmp, "%4d ",
971 hc->f_rec->
972 ref_poc[
973 img->num_of_references - 1 - i]);
974 } else {
975 sprintf(str_tmp, "%4d ",
976 hc->f_rec->ref_poc[i]);
977 }
978#else
979 sprintf(str_tmp, "%4d ",
980 avs2_dec->fref[i]->imgtr_fwRefDistance);
981#endif
982
983 str_tmp[5] = '\0';
984 strcat(str, str_tmp);
985 }
986 strcat(str, "]");
987 } else {
988 str[0] = '\0';
989 }
990}
991
992void prepare_RefInfo(struct avs2_decoder *avs2_dec)
993{
994 struct ImageParameters_s *img = &avs2_dec->img;
995 struct Video_Com_data_s *hc = &avs2_dec->hc;
996 struct Video_Dec_data_s *hd = &avs2_dec->hd;
997
998 int32_t i, j;
999 int32_t ii;
1000 struct avs2_frame_s *tmp_fref;
1001
1002 /*update IDR frame*/
1003 if (img->tr > hd->next_IDRtr && hd->curr_IDRtr != hd->next_IDRtr) {
1004 hd->curr_IDRtr = hd->next_IDRtr;
1005 hd->curr_IDRcoi = hd->next_IDRcoi;
1006 }
1007 /* re-order the ref buffer according to RPS*/
1008 img->num_of_references = hd->curr_RPS.num_of_ref;
1009
1010#if 1
1011 /*rain*/
1012 if (is_avs2_print_bufmgr_detail()) {
1013 pr_info("%s: coding_order is %d, curr_IDRcoi is %d\n",
1014 __func__, img->coding_order, hd->curr_IDRcoi);
1015 for (ii = 0; ii < MAXREF; ii++) {
1016 pr_info("ref_pic(%d)=%d\n",
1017 ii, hd->curr_RPS.ref_pic[ii]);
1018 }
1019 for (ii = 0; ii < avs2_dec->ref_maxbuffer; ii++) {
1020 pr_info(
1021 "fref[%d]: index %d imgcoi_ref %d imgtr_fwRefDistance %d\n",
1022 ii, avs2_dec->fref[ii]->index,
1023 avs2_dec->fref[ii]->imgcoi_ref,
1024 avs2_dec->fref[ii]->imgtr_fwRefDistance);
1025 }
1026 }
1027#endif
1028
1029 for (i = 0; i < hd->curr_RPS.num_of_ref; i++) {
1030 /*int32_t accumulate = 0;*/
1031 /* copy tmp_fref from avs2_dec->fref[i] */
1032 tmp_fref = avs2_dec->fref[i];
1033
1034#if REMOVE_UNUSED
1035 for (j = i; j < avs2_dec->ref_maxbuffer; j++) {
1036 /*/////////////to be modified IDR*/
1037 if (avs2_dec->fref[j]->imgcoi_ref ==
1038 img->coding_order -
1039 hd->curr_RPS.ref_pic[i]) {
1040 break;
1041 }
1042 }
1043#else
1044
1045 for (j = i; j < avs2_dec->ref_maxbuffer; j++) {
1046 /*/////////////to be modified IDR*/
1047 int32_t k , tmp_tr;
1048 for (k = 0; k < avs2_dec->ref_maxbuffer; k++) {
1049 if (((int32_t)img->coding_order -
1050 (int32_t)hd->curr_RPS.ref_pic[i]) ==
1051 avs2_dec->fref[k]->imgcoi_ref &&
1052 avs2_dec->fref[k]->imgcoi_ref >= -256) {
1053 break;
1054 }
1055 }
1056 if (k == avs2_dec->ref_maxbuffer) {
1057 tmp_tr =
1058 -1-1;
1059 } else {
1060 tmp_tr =
1061 avs2_dec->fref[k]->imgtr_fwRefDistance;
1062 }
1063 if (tmp_tr < hd->curr_IDRtr) {
1064 hd->curr_RPS.ref_pic[i] =
1065 img->coding_order - hd->curr_IDRcoi;
1066
1067 for (k = 0; k < i; k++) {
1068 if (hd->curr_RPS.ref_pic[k] ==
1069 hd->curr_RPS.ref_pic[i]) {
1070 accumulate++;
1071 break;
1072 }
1073 }
1074 }
1075 if (avs2_dec->fref[j]->imgcoi_ref ==
1076 img->coding_order - hd->curr_RPS.ref_pic[i]) {
1077 break;
1078 }
1079 }
1080 if (j == avs2_dec->ref_maxbuffer || accumulate)
1081 img->num_of_references--;
1082#endif
1083 if (j != avs2_dec->ref_maxbuffer) {
1084 /* copy avs2_dec->fref[i] from avs2_dec->fref[j] */
1085 avs2_dec->fref[i] = avs2_dec->fref[j];
1086 /* copy avs2_dec->fref[j] from ferf[tmp] */
1087 avs2_dec->fref[j] = tmp_fref;
1088 if (is_avs2_print_bufmgr_detail()) {
1089 pr_info("%s, switch %d %d: ", __func__, i, j);
1090 for (ii = 0; ii < hd->curr_RPS.num_of_ref
1091 || ii <= j; ii++)
1092 pr_info("%d ",
1093 avs2_dec->fref[ii]->index);
1094 pr_info("\n");
1095 }
1096 }
1097 }
1098 if (img->type == B_IMG &&
1099 (avs2_dec->fref[0]->imgtr_fwRefDistance <= img->tr
1100 || avs2_dec->fref[1]->imgtr_fwRefDistance >= img->tr)) {
1101
1102 pr_info("wrong reference configuration for B frame\n");
1103 pr_info(
1104 "fref0 imgtr_fwRefDistance %d, fref1 imgtr_fwRefDistance %d, img->tr %d\n",
1105 avs2_dec->fref[0]->imgtr_fwRefDistance,
1106 avs2_dec->fref[1]->imgtr_fwRefDistance,
1107 img->tr);
1108 hc->f_rec->error_mark = 1;
1109 avs2_dec->bufmgr_error_flag = 1;
1110 return; /* exit(-1);*/
1111 /*******************************************/
1112 }
1113
1114#if !FIX_PROFILE_LEVEL_DPB_RPS_1
1115 /* delete the frame that will never be used*/
1116 for (i = 0; i < hd->curr_RPS.num_to_remove; i++) {
1117 for (j = 0; j < avs2_dec->ref_maxbuffer; j++) {
1118 if (avs2_dec->fref[j]->imgcoi_ref >= -256
1119 && avs2_dec->fref[j]->imgcoi_ref
1120 == img->coding_order -
1121 hd->curr_RPS.remove_pic[i]) {
1122 break;
1123 }
1124 }
1125 if (j < avs2_dec->ref_maxbuffer &&
1126 j >= img->num_of_references) {
1127 avs2_dec->fref[j]->imgcoi_ref = -257;
1128#if M3480_TEMPORAL_SCALABLE
1129 avs2_dec->fref[j]->temporal_id = -1;
1130#endif
1131 if (avs2_dec->fref[j]->is_output == -1) {
1132 avs2_dec->fref[j]->
1133 imgtr_fwRefDistance = -256;
1134 }
1135 }
1136 }
1137#endif
1138
1139 /* add inter-view reference picture*/
1140
1141 /* add current frame to ref buffer*/
1142 for (i = 0; i < avs2_dec->ref_maxbuffer; i++) {
1143 if ((avs2_dec->fref[i]->imgcoi_ref < -256
1144 || abs(avs2_dec->fref[i]->
1145 imgtr_fwRefDistance - img->tr) >= 128)
1146 && avs2_dec->fref[i]->is_output == -1
1147 && avs2_dec->fref[i]->bg_flag == 0
1148#ifndef NO_DISPLAY
1149 && avs2_dec->fref[i]->vf_ref == 0
1150 && avs2_dec->fref[i]->to_prepare_disp == 0
1151#endif
1152 ) {
1153 break;
1154 }
1155 }
1156 if (i == avs2_dec->ref_maxbuffer) {
1157 pr_info(
1158 "%s, warning, no enough buf\n",
1159 __func__);
1160 i--;
1161 }
1162
1163 hc->f_rec = avs2_dec->fref[i];
1164 hc->currentFrame = hc->f_rec->ref;
1165 hc->f_rec->imgtr_fwRefDistance = img->tr;
1166 hc->f_rec->imgcoi_ref = img->coding_order;
1167#if M3480_TEMPORAL_SCALABLE
1168 hc->f_rec->temporal_id = hd->cur_layer;
1169#endif
1170 hc->f_rec->is_output = 1;
1171#ifdef AML
1172 hc->f_rec->error_mark = 0;
1173 hc->f_rec->decoded_lcu = 0;
1174 hc->f_rec->slice_type = img->type;
1175#endif
1176 hc->f_rec->refered_by_others = hd->curr_RPS.referd_by_others;
1177 if (is_avs2_print_bufmgr_detail())
1178 pr_info(
1179 "%s, set f_rec (cur_pic) <= fref[%d] img->tr %d coding_order %d img_type %d\n",
1180 __func__, i, img->tr, img->coding_order,
1181 img->type);
1182
1183 if (img->type != B_IMG) {
1184 for (j = 0;
1185 j < img->num_of_references; j++) {
1186 hc->f_rec->ref_poc[j] =
1187 avs2_dec->fref[j]->imgtr_fwRefDistance;
1188 }
1189 } else {
1190 hc->f_rec->ref_poc[0] =
1191 avs2_dec->fref[1]->imgtr_fwRefDistance;
1192 hc->f_rec->ref_poc[1] =
1193 avs2_dec->fref[0]->imgtr_fwRefDistance;
1194 }
1195
1196#if M3480_TEMPORAL_SCALABLE
1197
1198 for (j = img->num_of_references;
1199 j < 4; j++) {
1200 /**/
1201 hc->f_rec->ref_poc[j] = 0;
1202 }
1203
1204 if (img->type == INTRA_IMG) {
1205 int32_t l;
1206 for (l = 0; l < 4; l++) {
1207 hc->f_rec->ref_poc[l]
1208 = img->tr;
1209 }
1210 }
1211
1212#endif
1213
1214/*////////////////////////////////////////////////////////////////////////*/
1215 /* updata ref pointer*/
1216
1217 if (img->type != I_IMG) {
1218
1219 img->imgtr_next_P = img->type == B_IMG ?
1220 avs2_dec->fref[0]->imgtr_fwRefDistance : img->tr;
1221 if (img->type == B_IMG) {
1222 hd->trtmp = avs2_dec->fref[0]->imgtr_fwRefDistance;
1223 avs2_dec->fref[0]->imgtr_fwRefDistance =
1224 avs2_dec->fref[1]->imgtr_fwRefDistance;
1225 }
1226 }
1227#if 1
1228 /*rain*/
1229 if (is_avs2_print_bufmgr_detail()) {
1230 for (ii = 0; ii < avs2_dec->ref_maxbuffer; ii++) {
1231 pr_info(
1232 "fref[%d]: index %d imgcoi_ref %d imgtr_fwRefDistance %d refered %d, is_out %d, bg %d, vf_ref %d ref_pos(%d,%d,%d,%d,%d,%d,%d)\n",
1233 ii, avs2_dec->fref[ii]->index,
1234 avs2_dec->fref[ii]->imgcoi_ref,
1235 avs2_dec->fref[ii]->imgtr_fwRefDistance,
1236 avs2_dec->fref[ii]->refered_by_others,
1237 avs2_dec->fref[ii]->is_output,
1238 avs2_dec->fref[ii]->bg_flag,
1239 avs2_dec->fref[ii]->vf_ref,
1240 avs2_dec->fref[ii]->ref_poc[0],
1241 avs2_dec->fref[ii]->ref_poc[1],
1242 avs2_dec->fref[ii]->ref_poc[2],
1243 avs2_dec->fref[ii]->ref_poc[3],
1244 avs2_dec->fref[ii]->ref_poc[4],
1245 avs2_dec->fref[ii]->ref_poc[5],
1246 avs2_dec->fref[ii]->ref_poc[6]
1247 );
1248 }
1249 }
1250#endif
1251}
1252
1253int32_t init_frame(struct avs2_decoder *avs2_dec)
1254{
1255 struct ImageParameters_s *img = &avs2_dec->img;
1256 struct Video_Com_data_s *hc = &avs2_dec->hc;
1257 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1258
1259
1260#if RD1510_FIX_BG
1261 if (img->type == I_IMG &&
1262 img->typeb == BACKGROUND_IMG) { /*G/GB frame*/
1263 img->num_of_references = 0;
1264 } else if (img->type == P_IMG && img->typeb == BP_IMG) {
1265 /* only one reference frame(G\GB) for S frame*/
1266 img->num_of_references = 1;
1267 }
1268#endif
1269
1270 if (img->typeb == BACKGROUND_IMG &&
1271 hd->background_picture_output_flag == 0) {
1272 hc->currentFrame = hc->background_ref;
1273#ifdef AML
1274 hc->cur_pic = avs2_dec->m_bg;
1275#endif
1276 } else {
1277 prepare_RefInfo(avs2_dec);
1278#ifdef AML
1279 hc->cur_pic = hc->f_rec;
1280#endif
1281 }
1282
1283
1284#ifdef FIX_CHROMA_FIELD_MV_BK_DIST
1285 if (img->typeb == BACKGROUND_IMG
1286 && img->is_field_sequence) {
1287 avs2_dec->bk_img_is_top_field
1288 = img->is_top_field;
1289 }
1290#endif
1291 return 0;
1292}
1293
1294void delete_trbuffer(struct outdata_s *data, int32_t pos)
1295{
1296 int32_t i;
1297 for (i = pos;
1298 i < data->buffer_num - 1; i++) {
1299 data->stdoutdata[i] =
1300 data->stdoutdata[i + 1];
1301 }
1302 data->buffer_num--;
1303}
1304
1305#if RD170_FIX_BG
1306void flushDPB(struct avs2_decoder *avs2_dec)
1307{
1308 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1309 int j, tmp_min, i, pos = -1;
1310 int search_times = avs2_dec->outprint.buffer_num;
1311
1312 tmp_min = 1 << 20;
1313 i = 0, j = 0;
1314 pos = -1;
1315
1316 for (j = 0; j < search_times; j++) {
1317 pos = -1;
1318 tmp_min = (1 << 20);
1319 /*search for min poi picture to display*/
1320 for (i = 0; i < avs2_dec->outprint.buffer_num; i++) {
1321 if (avs2_dec->outprint.stdoutdata[i].tr < tmp_min) {
1322 pos = i;
1323 tmp_min = avs2_dec->outprint.stdoutdata[i].tr;
1324 }
1325 }
1326
1327 if (pos != -1) {
1328 hd->last_output = avs2_dec->outprint.stdoutdata[pos].tr;
1329 report_frame(avs2_dec, &avs2_dec->outprint, pos);
1330 if (avs2_dec->outprint.stdoutdata[pos].typeb
1331 == BACKGROUND_IMG &&
1332 avs2_dec->outprint.stdoutdata[pos].
1333 background_picture_output_flag
1334 == 0) {
1335 /*write_GB_frame(hd->p_out_background);*/
1336 } else {
1337 write_frame(avs2_dec,
1338 avs2_dec->outprint.stdoutdata[pos].tr);
1339 }
1340
1341 delete_trbuffer(&avs2_dec->outprint, pos);
1342 }
1343 }
1344
1345 /*clear dpb info*/
1346 for (j = 0; j < REF_MAXBUFFER; j++) {
1347 avs2_dec->fref[j]->imgtr_fwRefDistance = -256;
1348 avs2_dec->fref[j]->imgcoi_ref = -257;
1349 avs2_dec->fref[j]->temporal_id = -1;
1350 avs2_dec->fref[j]->refered_by_others = 0;
1351 }
1352}
1353#endif
1354
1355
1356
1357#if M3480_TEMPORAL_SCALABLE
1358void cleanRefMVBufRef(int pos)
1359{
1360#if 0
1361 int k, x, y;
1362 /*re-init mvbuf*/
1363 for (k = 0; k < 2; k++) {
1364 for (y = 0; y < img->height / MIN_BLOCK_SIZE; y++) {
1365 for (x = 0; x < img->width / MIN_BLOCK_SIZE; x++)
1366 fref[pos]->mvbuf[y][x][k] = 0;
1367
1368 }
1369 }
1370 /*re-init refbuf*/
1371 for (y = 0; y < img->height / MIN_BLOCK_SIZE; y++) {
1372 for (x = 0; x < img->width / MIN_BLOCK_SIZE ; x++)
1373 fref[pos]->refbuf[y][x] = -1;
1374
1375 }
1376#endif
1377}
1378#endif
1379
1380static int frame_postprocessing(struct avs2_decoder *avs2_dec)
1381{
1382 struct ImageParameters_s *img = &avs2_dec->img;
1383 struct Video_Com_data_s *hc = &avs2_dec->hc;
1384 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1385
1386 int32_t pointer_tmp = avs2_dec->outprint.buffer_num;
1387 int32_t i;
1388 struct STDOUT_DATA_s *p_outdata;
1389#if RD160_FIX_BG
1390 int32_t j, tmp_min, output_cur_dec_pic, pos = -1;
1391 int32_t search_times = avs2_dec->outprint.buffer_num;
1392#endif
1393 /*pic dist by Grandview Semi. @ [06-07-20 15:25]*/
1394 img->PrevPicDistanceLsb = (img->coding_order % 256);
1395
1396 pointer_tmp = avs2_dec->outprint.buffer_num;
1397 p_outdata = &avs2_dec->outprint.stdoutdata[pointer_tmp];
1398
1399 p_outdata->type = img->type;
1400 p_outdata->typeb = img->typeb;
1401 p_outdata->framenum = img->tr;
1402 p_outdata->tr = img->tr;
1403#if 0 /*def ORI*/
1404 p_outdata->qp = img->qp;
1405#else
1406 p_outdata->qp = 0;
1407#endif
1408 /*p_outdata->snr_y = snr->snr_y;*/
1409 /*p_outdata->snr_u = snr->snr_u;*/
1410 /*p_outdata->snr_v = snr->snr_v;*/
1411 p_outdata->tmp_time = hd->tmp_time;
1412 p_outdata->picture_structure = img->picture_structure;
1413 /*p_outdata->curr_frame_bits =
1414 StatBitsPtr->curr_frame_bits;*/
1415 /*p_outdata->emulate_bits = StatBitsPtr->emulate_bits;*/
1416#if RD1501_FIX_BG
1417 p_outdata->background_picture_output_flag
1418 = hd->background_picture_output_flag;
1419 /*Longfei.Wang@mediatek.com*/
1420#endif
1421
1422#if RD160_FIX_BG
1423 p_outdata->picture_reorder_delay = hd->picture_reorder_delay;
1424#endif
1425 avs2_dec->outprint.buffer_num++;
1426
1427#if RD170_FIX_BG
1428 search_times = avs2_dec->outprint.buffer_num;
1429#endif
1430 /* record the reference list*/
1431 strcpy(p_outdata->str_reference_list, hc->str_list_reference);
1432
1433#if !REF_OUTPUT
1434 #error "!!!REF_OUTPUT should be 1"
1435 for (i = 0; i < avs2_dec->outprint.buffer_num; i++) {
1436 min_tr(avs2_dec->outprint, &pos);
1437 if (avs2_dec->outprint.stdoutdata[pos].tr < img->tr
1438 || avs2_dec->outprint.stdoutdata[pos].tr
1439 == (hd->last_output + 1)) {
1440 hd->last_output = avs2_dec->outprint.stdoutdata[pos].tr;
1441 report_frame(avs2_dec, &avs2_dec->outprint, pos);
1442#if 0 /*def ORI*/
1443 write_frame(hd->p_out,
1444 avs2_dec->outprint.stdoutdata[pos].tr);
1445#endif
1446 delete_trbuffer(&avs2_dec->outprint, pos);
1447 i--;
1448 } else {
1449 break;
1450 }
1451 }
1452#else
1453#if RD160_FIX_BG /*Longfei.Wang@mediatek.com*/
1454 tmp_min = 1 << 20;
1455 i = 0, j = 0;
1456 output_cur_dec_pic = 0;
1457 pos = -1;
1458 for (j = 0; j < search_times; j++) {
1459 pos = -1;
1460 tmp_min = (1 << 20);
1461 /*search for min poi picture to display*/
1462 for (i = 0; i < avs2_dec->outprint.buffer_num; i++) {
1463 if ((avs2_dec->outprint.stdoutdata[i].tr < tmp_min) &&
1464 ((avs2_dec->outprint.stdoutdata[i].tr
1465 + avs2_dec->outprint.stdoutdata[i].
1466 picture_reorder_delay)
1467 <= (int32_t)img->coding_order)) {
1468 pos = i;
1469 tmp_min = avs2_dec->outprint.stdoutdata[i].tr;
1470 }
1471 }
1472
1473 if ((0 == hd->displaydelay) && (0 == output_cur_dec_pic)) {
1474 if (img->tr <= tmp_min) {/*fred.chiu@mediatek.com*/
1475 /*output current decode picture
1476 right now*/
1477 pos = avs2_dec->outprint.buffer_num - 1;
1478 output_cur_dec_pic = 1;
1479 }
1480 }
1481 if (pos != -1) {
1482 hd->last_output = avs2_dec->outprint.stdoutdata[pos].tr;
1483 report_frame(avs2_dec, &avs2_dec->outprint, pos);
1484#if 1 /*def ORI*/
1485 if (avs2_dec->outprint.stdoutdata[pos].typeb
1486 == BACKGROUND_IMG &&
1487 avs2_dec->outprint.stdoutdata[pos].
1488 background_picture_output_flag == 0) {
1489 /**/
1490 /**/
1491 } else {
1492 write_frame(avs2_dec,
1493 avs2_dec->outprint.stdoutdata[pos].tr);
1494 }
1495#endif
1496 delete_trbuffer(&avs2_dec->outprint, pos);
1497 }
1498
1499 }
1500
1501#else
1502 #error "!!!RD160_FIX_BG should be defined"
1503 if (img->coding_order +
1504 (uint32_t)hc->total_frames * 256 >=
1505 (uint32_t)hd->picture_reorder_delay) {
1506 int32_t tmp_min, pos = -1;
1507 tmp_min = 1 << 20;
1508
1509 for (i = 0; i <
1510 avs2_dec->outprint.buffer_num; i++) {
1511 if (avs2_dec->outprint.stdoutdata[i].tr
1512 < tmp_min &&
1513 avs2_dec->outprint.stdoutdata[i].tr
1514 >= hd->last_output) {
1515 /*GB has the same "tr" with "last_output"*/
1516 pos = i;
1517 tmp_min =
1518 avs2_dec->outprint.stdoutdata[i].tr;
1519 }
1520 }
1521
1522 if (pos != -1) {
1523 hd->last_output = avs2_dec->outprint.stdoutdata[pos].tr;
1524 report_frame(avs2_dec, &avs2_dec->outprint, pos);
1525#if RD1501_FIX_BG
1526 if (avs2_dec->outprint.stdoutdata[pos].typeb
1527 == BACKGROUND_IMG && avs2_dec->
1528 outprint.stdoutdata[pos].
1529 background_picture_output_flag == 0) {
1530#else
1531 if (avs2_dec->outprint.stdoutdata[pos].typeb
1532 == BACKGROUND_IMG &&
1533 hd->background_picture_output_flag
1534 == 0) {
1535#endif
1536 write_GB_frame(
1537 hd->p_out_background);
1538 } else {
1539 write_frame(avs2_dec,
1540 avs2_dec->outprint.stdoutdata[pos].tr);
1541 }
1542 delete_trbuffer(&avs2_dec->outprint, pos);
1543
1544 }
1545
1546 }
1547#endif
1548#endif
1549 return pos;
1550
1551 }
1552
1553void write_frame(struct avs2_decoder *avs2_dec, int32_t pos)
1554{
1555 int32_t j;
1556
1557 if (is_avs2_print_bufmgr_detail())
1558 pr_info("%s(pos = %d)\n", __func__, pos);
1559
1560 for (j = 0; j < avs2_dec->ref_maxbuffer; j++) {
1561 if (avs2_dec->fref[j]->imgtr_fwRefDistance == pos) {
1562 avs2_dec->fref[j]->imgtr_fwRefDistance_bak = pos;
1563 avs2_dec->fref[j]->is_output = -1;
1564 avs2_dec->fref[j]->to_prepare_disp =
1565 avs2_dec->to_prepare_disp_count++;
1566 if (avs2_dec->fref[j]->refered_by_others == 0
1567 || avs2_dec->fref[j]->imgcoi_ref
1568 == -257) {
1569 avs2_dec->fref[j]->imgtr_fwRefDistance
1570 = -256;
1571 avs2_dec->fref[j]->imgcoi_ref = -257;
1572#if M3480_TEMPORAL_SCALABLE
1573 avs2_dec->fref[j]->temporal_id = -1;
1574#endif
1575 if (is_avs2_print_bufmgr_detail())
1576 pr_info("%s, fref index %d\n",
1577 __func__, j);
1578 }
1579 break;
1580 }
1581 }
1582}
1583
1584/*rain???, outdata *data*/
1585void report_frame(struct avs2_decoder *avs2_dec,
1586 struct outdata_s *data, int32_t pos)
1587{
1588 struct ImageParameters_s *img = &avs2_dec->img;
1589 struct Video_Com_data_s *hc = &avs2_dec->hc;
1590 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1591
1592 int8_t *Frmfld;
1593 int8_t Frm[] = "FRM";
1594 int8_t Fld[] = "FLD";
1595 struct STDOUT_DATA_s *p_stdoutdata
1596 = &data->stdoutdata[pos];
1597 const int8_t *typ;
1598
1599#if 0
1600 if (input->MD5Enable & 0x02) {
1601 sprintf(MD5str, "%08X%08X%08X%08X\0",
1602 p_stdoutdata->DecMD5Value[0],
1603 p_stdoutdata->DecMD5Value[1],
1604 p_stdoutdata->DecMD5Value[2],
1605 p_stdoutdata->DecMD5Value[3]);
1606 } else {
1607 memset(MD5val, 0, 16);
1608 memset(MD5str, 0, 33);
1609 }
1610#endif
1611
1612 if (p_stdoutdata->
1613 picture_structure) {
1614 Frmfld = Frm;
1615 } else {
1616 Frmfld = Fld;
1617 }
1618#if INTERLACE_CODING
1619 if (img->is_field_sequence) { /*rcs??*/
1620 Frmfld = Fld;
1621 }
1622#endif
1623 if ((p_stdoutdata->tr + hc->total_frames * 256)
1624 == hd->end_SeqTr) { /* I picture*/
1625 /*if ( img->new_sequence_flag == 1 )*/
1626 {
1627 img->sequence_end_flag = 0;
1628 /*fprintf(stdout, "Sequence
1629 End\n\n");*/
1630 }
1631 }
1632 if ((p_stdoutdata->tr + hc->total_frames * 256)
1633 == hd->next_IDRtr) {
1634#if !RD170_FIX_BG
1635 if (hd->vec_flag) /**/
1636#endif
1637 {
1638 hd->vec_flag = 0;
1639 /*fprintf(stdout, "Video Edit
1640 Code\n");*/
1641 }
1642 }
1643
1644 if (p_stdoutdata->typeb == BACKGROUND_IMG) {
1645 typ = (hd->background_picture_output_flag != 0) ? "G" : "GB";
1646 } else {
1647#if REMOVE_UNUSED
1648 typ = (p_stdoutdata->type == INTRA_IMG)
1649 ? "I" : (p_stdoutdata->type == INTER_IMG) ?
1650 ((p_stdoutdata->typeb == BP_IMG) ? "S" : "P")
1651 : (p_stdoutdata->type == F_IMG ? "F" : "B");
1652#else
1653 typ = (p_stdoutdata->type == INTRA_IMG) ? "I" :
1654 (p_stdoutdata->type == INTER_IMG) ?
1655 ((p_stdoutdata->type == BP_IMG) ? "S" : "P")
1656 : (p_stdoutdata->type == F_IMG ? "F" : "B");
1657#endif
1658 }
1659
1660#if 0
1661 /*rain???*/
1662 pr_info("%3d(%s) %3d %5d %7.4f %7.4f %7.4f %5d\t\t%s %8d %6d\t%s",
1663 p_stdoutdata->framenum + hc->total_frames * 256,
1664 typ, p_stdoutdata->tr + hc->total_frames * 256,
1665 p_stdoutdata->qp, p_stdoutdata->snr_y,
1666 p_stdoutdata->snr_u, p_stdoutdata->snr_v,
1667 p_stdoutdata->tmp_time, Frmfld,
1668 p_stdoutdata->curr_frame_bits,
1669 p_stdoutdata->emulate_bits,
1670 "");
1671#endif
1672 if (is_avs2_print_bufmgr_detail())
1673 pr_info(" %s\n", p_stdoutdata->str_reference_list);
1674
1675 /*fflush(stdout);*/
1676 hd->FrameNum++;
1677}
1678
1679void avs2_prepare_header(struct avs2_decoder *avs2_dec, int32_t start_code)
1680{
1681 struct ImageParameters_s *img = &avs2_dec->img;
1682 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1683
1684 switch (start_code) {
1685 case SEQUENCE_HEADER_CODE:
1686 img->new_sequence_flag = 1;
1687 if (is_avs2_print_bufmgr_detail())
1688 pr_info("SEQUENCE\n");
1689#ifdef TO_CHECK
1690#if SEQ_CHANGE_CHECKER
1691 if (seq_checker_buf == NULL) {
1692 seq_checker_buf = malloc(length);
1693 seq_checker_length = length;
1694 memcpy(seq_checker_buf, Buf, length);
1695 } else {
1696 if ((seq_checker_length != length) ||
1697 (memcmp(seq_checker_buf, Buf, length) != 0)) {
1698 free(seq_checker_buf);
1699 /*fprintf(stdout,
1700 "Non-conformance
1701 stream: sequence
1702 header cannot change
1703 !!\n");*/
1704#if RD170_FIX_BG
1705 seq_checker_buf = NULL;
1706 seq_checker_length = 0;
1707 seq_checker_buf = malloc(length);
1708 seq_checker_length = length;
1709 memcpy(seq_checker_buf, Buf, length);
1710#endif
1711 }
1712
1713
1714 }
1715#endif
1716#if RD170_FIX_BG
1717 if (input->alf_enable
1718 && alfParAllcoated == 1) {
1719 ReleaseAlfGlobalBuffer();
1720 alfParAllcoated = 0;
1721 }
1722#endif
1723/*TO_CHECK*/
1724#endif
1725#if FIX_FLUSH_DPB_BY_LF
1726 if (hd->vec_flag) {
1727 int32_t k;
1728 if (is_avs2_print_bufmgr_detail())
1729 pr_info("vec_flag is 1, flushDPB and reinit bugmgr\n");
1730
1731 flushDPB(avs2_dec);
1732 for (k = 0; k < avs2_dec->ref_maxbuffer; k++)
1733 cleanRefMVBufRef(k);
1734
1735 hd->vec_flag = 0;
1736#ifdef AML
1737 free_unused_buffers(avs2_dec);
1738#else
1739 free_global_buffers(avs2_dec);
1740#endif
1741 img->number = 0;
1742 img->PrevPicDistanceLsb = 0;
1743 avs2_dec->init_hw_flag = 0;
1744 }
1745#endif
1746
1747#if FIX_SEQ_END_FLUSH_DPB_BY_LF
1748 if (img->new_sequence_flag
1749 && img->sequence_end_flag) {
1750 int32_t k;
1751 if (is_avs2_print_bufmgr_detail())
1752 pr_info(
1753 "new_sequence_flag after sequence_end_flag, flushDPB and reinit bugmgr\n");
1754 flushDPB(avs2_dec);
1755 for (k = 0; k < avs2_dec->ref_maxbuffer; k++)
1756 cleanRefMVBufRef(k);
1757
1758#ifdef AML
1759 free_unused_buffers(avs2_dec);
1760#else
1761 free_global_buffers(avs2_dec);
1762#endif
1763 img->number = 0;
1764 img->PrevPicDistanceLsb = 0;
1765 avs2_dec->init_hw_flag = 0;
1766 }
1767#endif
1768 img->seq_header_indicate = 1;
1769 break;
1770 case I_PICTURE_START_CODE:
1771 if (is_avs2_print_bufmgr_detail())
1772 pr_info("PIC-I\n");
1773 Get_SequenceHeader(avs2_dec);
1774 Get_I_Picture_Header(avs2_dec);
1775 calc_picture_distance(avs2_dec);
1776 Read_ALF_param(avs2_dec);
1777 if (!img->seq_header_indicate) {
1778 img->B_discard_flag = 1;
1779 /*fprintf(stdout, " I
1780 %3d\t\tDIDSCARD!!\n",
1781 img->tr);*/
1782 break;
1783 }
1784 break;
1785 case PB_PICTURE_START_CODE:
1786 if (is_avs2_print_bufmgr_detail())
1787 pr_info("PIC-PB\n");
1788 Get_SequenceHeader(avs2_dec);
1789 Get_PB_Picture_Header(avs2_dec);
1790 calc_picture_distance(avs2_dec);
1791 Read_ALF_param(avs2_dec);
1792 /* xiaozhen zheng, 20071009*/
1793 if (!img->seq_header_indicate) {
1794 img->B_discard_flag = 1;
1795
1796 if (img->type == P_IMG) {
1797 /*fprintf(stdout, " P
1798 %3d\t\tDIDSCARD!!\n",
1799 img->tr);*/
1800 }
1801 if (img->type == F_IMG) {
1802 /*fprintf(stdout, " F
1803 %3d\t\tDIDSCARD!!\n",
1804 img->tr);*/
1805 } else {
1806 /*fprintf(stdout, " B
1807 %3d\t\tDIDSCARD!!\n",
1808 img->tr);*/
1809 }
1810
1811 break;
1812 }
1813
1814 if (img->seq_header_indicate == 1
1815 && img->type != B_IMG) {
1816 img->B_discard_flag = 0;
1817 }
1818 if (img->type == B_IMG && img->B_discard_flag == 1
1819 && !img->random_access_decodable_flag) {
1820 /*fprintf(stdout, " B
1821 %3d\t\tDIDSCARD!!\n",
1822 img->tr);*/
1823 break;
1824 }
1825
1826 break;
1827 case SEQUENCE_END_CODE:
1828 if (is_avs2_print_bufmgr_detail())
1829 pr_info("SEQUENCE_END_CODE\n");
1830#ifdef TO_CHECK
1831#if SEQ_CHANGE_CHECKER
1832 if (seq_checker_buf != NULL) {
1833 free(seq_checker_buf);
1834 seq_checker_buf = NULL;
1835 seq_checker_length = 0;
1836 }
1837#endif
1838#endif
1839img->new_sequence_flag = 1;
1840img->sequence_end_flag = 1;
1841break;
1842 case VIDEO_EDIT_CODE:
1843 if (is_avs2_print_bufmgr_detail())
1844 pr_info("VIDEO_EDIT_CODE\n");
1845 /*video_edit_code_data(Buf, startcodepos, length);*/
1846 hd->vec_flag = 1;
1847#ifdef TO_CHECK
1848#if SEQ_CHANGE_CHECKER
1849 if (seq_checker_buf != NULL) {
1850 free(seq_checker_buf);
1851 seq_checker_buf = NULL;
1852 seq_checker_length = 0;
1853 }
1854#endif
1855#endif
1856
1857break;
1858 }
1859}
1860
1861#ifdef AML
1862static uint32_t log2i(uint32_t val)
1863{
1864 uint32_t ret = -1;
1865 while (val != 0) {
1866 val >>= 1;
1867 ret++;
1868 }
1869 return ret;
1870}
1871#endif
1872
1873int32_t avs2_process_header(struct avs2_decoder *avs2_dec)
1874{
1875 struct inp_par *input = &avs2_dec->input;
1876 struct ImageParameters_s *img = &avs2_dec->img;
1877 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1878 int32_t lcu_x_num_div;
1879 int32_t lcu_y_num_div;
1880
1881 int32_t N8_SizeScale;
1882 /*pr_info("%s\n", __func__);*/
1883 {
1884 N8_SizeScale = 1;
1885
1886 if (hd->horizontal_size %
1887 (MIN_CU_SIZE * N8_SizeScale) != 0) {
1888 img->auto_crop_right =
1889 (MIN_CU_SIZE * N8_SizeScale) -
1890 (hd->horizontal_size %
1891 (MIN_CU_SIZE * N8_SizeScale));
1892 } else
1893 img->auto_crop_right = 0;
1894
1895#if !INTERLACE_CODING
1896 if (hd->progressive_sequence) /**/
1897#endif
1898 {
1899 if (hd->vertical_size %
1900 (MIN_CU_SIZE * N8_SizeScale) != 0) {
1901 img->auto_crop_bottom =
1902 (MIN_CU_SIZE * N8_SizeScale) -
1903 (hd->vertical_size %
1904 (MIN_CU_SIZE * N8_SizeScale));
1905 } else
1906 img->auto_crop_bottom = 0;
1907 }
1908
1909 /* Reinit parameters (NOTE: need to do
1910 before init_frame //*/
1911 img->width =
1912 (hd->horizontal_size + img->auto_crop_right);
1913 img->height =
1914 (hd->vertical_size + img->auto_crop_bottom);
1915 img->width_cr = (img->width >> 1);
1916
1917 if (input->chroma_format == 1)
1918 img->height_cr = (img->height >> 1);
1919
1920 img->PicWidthInMbs = img->width / MIN_CU_SIZE;
1921 img->PicHeightInMbs = img->height / MIN_CU_SIZE;
1922 img->PicSizeInMbs = img->PicWidthInMbs * img->PicHeightInMbs;
1923 img->max_mb_nr = (img->width * img->height) /
1924 (MIN_CU_SIZE * MIN_CU_SIZE);
1925 }
1926
1927 if (img->new_sequence_flag && img->sequence_end_flag) {
1928#if 0/*RD170_FIX_BG //*/
1929 int32_t k;
1930 flushDPB();
1931 for (k = 0; k < avs2_dec->ref_maxbuffer; k++)
1932 cleanRefMVBufRef(k);
1933
1934 free_global_buffers();
1935 img->number = 0;
1936#endif
1937 hd->end_SeqTr = img->tr;
1938 img->sequence_end_flag = 0;
1939 }
1940 if (img->new_sequence_flag) {
1941 hd->next_IDRtr = img->tr;
1942 hd->next_IDRcoi = img->coding_order;
1943 img->new_sequence_flag = 0;
1944 }
1945#if 0/*RD170_FIX_BG*/
1946 if (hd->vec_flag) {
1947 int32_t k;
1948 flushDPB();
1949 for (k = 0; k < avs2_dec->ref_maxbuffer; k++)
1950 cleanRefMVBufRef(k);
1951
1952 hd->vec_flag = 0;
1953 free_global_buffers();
1954 img->number = 0;
1955 }
1956#endif
1957/* allocate memory for frame buffers*/
1958#if 0
1959/* called in vavs2.c*/
1960 if (img->number == 0)
1961 avs2_init_global_buffers(avs2_dec);
1962#endif
1963 img->current_mb_nr = 0;
1964
1965 init_frame(avs2_dec);
1966
1967 img->types = img->type; /* jlzheng 7.15*/
1968
1969 if (img->type != B_IMG) {
1970 hd->pre_img_type = img->type;
1971 hd->pre_img_types = img->types;
1972 }
1973
1974#ifdef AML
1975 avs2_dec->lcu_size_log2 = log2i(avs2_dec->lcu_size);
1976 lcu_x_num_div = (img->width/avs2_dec->lcu_size);
1977 lcu_y_num_div = (img->height/avs2_dec->lcu_size);
1978 avs2_dec->lcu_x_num = ((img->width % avs2_dec->lcu_size) == 0) ?
1979 lcu_x_num_div : lcu_x_num_div+1;
1980 avs2_dec->lcu_y_num = ((img->height % avs2_dec->lcu_size) == 0) ?
1981 lcu_y_num_div : lcu_y_num_div+1;
1982 avs2_dec->lcu_total = avs2_dec->lcu_x_num*avs2_dec->lcu_y_num;
1983#endif
1984 return SOP;
1985}
1986
1987int avs2_post_process(struct avs2_decoder *avs2_dec)
1988{
1989 struct ImageParameters_s *img = &avs2_dec->img;
1990 struct Video_Com_data_s *hc = &avs2_dec->hc;
1991 struct Video_Dec_data_s *hd = &avs2_dec->hd;
1992 int32_t i;
1993 int ret;
1994 if (img->typeb == BACKGROUND_IMG && hd->background_picture_enable) {
1995#ifdef AML
1996 for (i = 0; i < avs2_dec->ref_maxbuffer; i++) {
1997 if (avs2_dec->fref[i]->bg_flag != 0) {
1998 avs2_dec->fref[i]->bg_flag = 0;
1999 if (is_avs2_print_bufmgr_detail())
2000 pr_info(
2001 "clear old BACKGROUND_IMG for index %d\r\n",
2002 avs2_dec->fref[i]->index);
2003 }
2004 }
2005 if (is_avs2_print_bufmgr_detail())
2006 pr_info(
2007 "post_process: set BACKGROUND_IMG flag for %d\r\n",
2008 hc->cur_pic->index);
2009 avs2_dec->f_bg = hc->cur_pic;
2010 hc->cur_pic->bg_flag = 1;
2011#endif
2012 }
2013
2014#if BCBR
2015 if (hd->background_picture_enable
2016 && hd->bcbr_enable && img->number > 0)
2017 updateBgReference();
2018#endif
2019
2020 if (img->typeb == BACKGROUND_IMG &&
2021 hd->background_picture_output_flag == 0)
2022 hd->background_number++;
2023
2024 if (img->type == B_IMG) {
2025 avs2_dec->fref[0]->imgtr_fwRefDistance
2026 = hd->trtmp;
2027 }
2028
2029 /* record the reference list information*/
2030 get_reference_list_info(avs2_dec, avs2_dec->hc.str_list_reference);
2031
2032 /*pr_info("%s\n", __func__);*/
2033 ret = frame_postprocessing(avs2_dec);
2034
2035#if FIX_PROFILE_LEVEL_DPB_RPS_1
2036 /* delete the frame that will never be used*/
2037 {
2038 int32_t i, j;
2039 if (is_avs2_print_bufmgr_detail()) {
2040 pr_info(
2041 "%s, coding_order %d to remove %d buf: ",
2042 __func__,
2043 img->coding_order,
2044 hd->curr_RPS.num_to_remove);
2045 for (i = 0; i < hd->curr_RPS.num_to_remove; i++)
2046 pr_info("%d ", hd->curr_RPS.remove_pic[i]);
2047 pr_info("\n");
2048 }
2049 for (i = 0; i < hd->curr_RPS.num_to_remove; i++) {
2050 for (j = 0; j < avs2_dec->ref_maxbuffer; j++) {
2051
2052 if (avs2_dec->fref[j]->imgcoi_ref >= -256
2053 && avs2_dec->fref[j]->imgcoi_ref ==
2054 img->coding_order -
2055 hd->curr_RPS.remove_pic[i])
2056 break;
2057 }
2058 if (j < avs2_dec->ref_maxbuffer) { /**/
2059#if FIX_RPS_PICTURE_REMOVE
2060/* Label new frames as "un-referenced" */
2061 avs2_dec->fref[j]->refered_by_others = 0;
2062
2063 /* remove frames which have been outputted */
2064 if (avs2_dec->fref[j]->is_output == -1) {
2065 avs2_dec->fref[j]->
2066 imgtr_fwRefDistance = -256;
2067 avs2_dec->fref[j]->imgcoi_ref = -257;
2068 avs2_dec->fref[j]->temporal_id = -1;
2069
2070 }
2071#else
2072 avs2_dec->fref[j]->imgcoi_ref = -257;
2073#if M3480_TEMPORAL_SCALABLE
2074 avs2_dec->fref[j]->temporal_id = -1;
2075#endif
2076 if (avs2_dec->fref[j]->is_output == -1) {
2077 avs2_dec->fref[j]->imgtr_fwRefDistance
2078 = -256;
2079 }
2080#endif
2081 }
2082 }
2083 }
2084#endif
2085
2086
2087 /*! TO 19.11.2001 Known Problem: for init_frame
2088 * we have to know the picture type of the
2089 * actual frame*/
2090 /*! in case the first slice of the P-Frame
2091 * following the I-Frame was lost we decode this
2092 * P-Frame but! do not write it because it
2093 * was
2094 * assumed to be an I-Frame in init_frame.So we
2095 * force the decoder to*/
2096 /*! guess the right picture type. This is a hack
2097 * a should be removed by the time there is a
2098 * clean*/
2099 /*! solution where we do not have to know the
2100 * picture type for the function init_frame.*/
2101 /*! End TO 19.11.2001//Lou*/
2102
2103 {
2104 if (img->type == I_IMG ||
2105 img->type == P_IMG ||
2106 img->type == F_IMG)
2107 img->number++;
2108 else {
2109 hc->Bframe_ctr++; /* B
2110 pictures*/
2111 }
2112 }
2113 return ret;
2114}
2115
2116void init_avs2_decoder(struct avs2_decoder *avs2_dec)
2117{
2118 int32_t i, j, k;
2119
2120 struct inp_par *input = &avs2_dec->input;
2121 struct ImageParameters_s *img = &avs2_dec->img;
2122 struct Video_Com_data_s *hc = &avs2_dec->hc;
2123 struct Video_Dec_data_s *hd = &avs2_dec->hd;
2124 if (is_avs2_print_bufmgr_detail())
2125 pr_info("[t] struct avs2_dec @0x%p\n", avs2_dec);
2126 memset(avs2_dec, 0, sizeof(struct avs2_decoder));
2127#ifdef AML
2128 avs2_dec->to_prepare_disp_count = 1;
2129#endif
2130 /*
2131 * ALFParam init
2132 */
2133 for (i = 0; i < 3; i++) {
2134 avs2_dec->m_alfPictureParam[i].alf_flag = 0; /*1*/
2135 avs2_dec->m_alfPictureParam[i].num_coeff = 9; /*1*/
2136 avs2_dec->m_alfPictureParam[i].filters_per_group = 3; /*1*/
2137 avs2_dec->m_alfPictureParam[i].componentID = i; /*1*/
2138 for (j = 0; j < 16; j++) {
2139 avs2_dec->m_alfPictureParam[i].filterPattern[j] = 0;
2140 /*16*/
2141 }
2142 for (j = 0; j < 16; j++) {
2143 for (k = 0; k < 9; k++) {
2144 avs2_dec->
2145 m_alfPictureParam[i].coeffmulti[j][k] = 0;
2146 /*16*9*/
2147 }
2148 }
2149 }
2150
2151 img->seq_header_indicate = 0;
2152 img->B_discard_flag = 0;
2153
2154 hd->eos = 0;
2155
2156 if (input->ref_pic_order) { /*ref order*/
2157 hd->dec_ref_num = 0;
2158 }
2159
2160 /*
2161 memset(g_log2size, -1, MAX_CU_SIZE + 1);
2162 c = 2;
2163 for (k = 4; k <= MAX_CU_SIZE; k *= 2) {
2164 g_log2size[k] = c;
2165 c++;
2166 }
2167 */
2168
2169 avs2_dec->outprint.buffer_num = 0;
2170
2171 hd->last_output = -1;
2172 hd->end_SeqTr = -1;
2173 hd->curr_IDRtr = 0;
2174 hd->curr_IDRcoi = 0;
2175 hd->next_IDRtr = 0;
2176 hd->next_IDRcoi = 0;
2177 /* Allocate Slice data struct*/
2178 img->number = 0;
2179 img->type = I_IMG;
2180
2181 img->imgtr_next_P = 0;
2182
2183 img->imgcoi_next_ref = 0;
2184
2185
2186 img->num_of_references = 0;
2187 hc->seq_header = 0;
2188
2189 img->new_sequence_flag = 1;
2190
2191 hd->vec_flag = 0;
2192
2193 hd->FrameNum = 0;
2194
2195 /* B pictures*/
2196 hc->Bframe_ctr = 0;
2197 hc->total_frames = 0;
2198
2199 /* time for total decoding session*/
2200 hc->tot_time = 0;
2201
2202}
2203
2204