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