xref: /OK3568_Linux_fs/external/mpp/mpp/codec/dec/h264/h264d_init.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2 *
3 * Copyright 2015 Rockchip Electronics Co. LTD
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #define MODULE_TAG "h264d_init"
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "mpp_mem.h"
25 #include "mpp_compat_impl.h"
26 #include "mpp_frame_impl.h"
27 
28 #include "h264d_global.h"
29 #include "h264d_init.h"
30 #include "h264d_dpb.h"
31 #include "h264d_scalist.h"
32 #include "h264d_fill.h"
33 #include "h264d_slice.h"
34 
decode_poc(H264dVideoCtx_t * p_Vid,H264_SLICE_t * pSlice)35 static MPP_RET decode_poc(H264dVideoCtx_t *p_Vid, H264_SLICE_t *pSlice)
36 {
37     RK_S32 i = 0;
38     RK_U32 MaxPicOrderCntLsb = 0;
39     MPP_RET ret = MPP_ERR_UNKNOW;
40     H264_SPS_t *active_sps = p_Vid->active_sps;
41     // for POC mode 0:
42     MaxPicOrderCntLsb = (1 << (active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
43 
44     switch (active_sps->pic_order_cnt_type) {
45     case 0: // POC MODE 0
46         // 1st
47         if (pSlice->idr_flag) {
48             p_Vid->PrevPicOrderCntMsb = 0;
49             p_Vid->PrevPicOrderCntLsb = 0;
50         } else {
51             if (p_Vid->last_has_mmco_5) {
52                 if (p_Vid->last_pic_bottom_field) {
53                     p_Vid->PrevPicOrderCntMsb = 0;
54                     p_Vid->PrevPicOrderCntLsb = 0;
55                 } else {
56                     p_Vid->PrevPicOrderCntMsb = 0;
57                     p_Vid->PrevPicOrderCntLsb = pSlice->toppoc;
58                 }
59             }
60         }
61         // Calculate the MSBs of current picture
62         if (pSlice->pic_order_cnt_lsb  <  p_Vid->PrevPicOrderCntLsb &&
63             (p_Vid->PrevPicOrderCntLsb - pSlice->pic_order_cnt_lsb) >= (RK_S32)(MaxPicOrderCntLsb / 2)) {
64             pSlice->PicOrderCntMsb = p_Vid->PrevPicOrderCntMsb + MaxPicOrderCntLsb;
65         } else if (pSlice->pic_order_cnt_lsb  >  p_Vid->PrevPicOrderCntLsb &&
66                    (pSlice->pic_order_cnt_lsb - p_Vid->PrevPicOrderCntLsb) > (RK_S32)(MaxPicOrderCntLsb / 2)) {
67             pSlice->PicOrderCntMsb = p_Vid->PrevPicOrderCntMsb - MaxPicOrderCntLsb;
68         } else {
69             pSlice->PicOrderCntMsb = p_Vid->PrevPicOrderCntMsb;
70         }
71         // 2nd
72         if (pSlice->field_pic_flag == 0) {
73             //frame pix
74             pSlice->toppoc = pSlice->PicOrderCntMsb + pSlice->pic_order_cnt_lsb;
75             pSlice->bottompoc = pSlice->toppoc + pSlice->delta_pic_order_cnt_bottom;
76             pSlice->ThisPOC = pSlice->framepoc = (pSlice->toppoc < pSlice->bottompoc) ? pSlice->toppoc : pSlice->bottompoc; // POC200301
77         } else if (pSlice->bottom_field_flag == 0) {
78             //top field
79             pSlice->ThisPOC = pSlice->toppoc = pSlice->PicOrderCntMsb + pSlice->pic_order_cnt_lsb;
80         } else {
81             //bottom field
82             pSlice->ThisPOC = pSlice->bottompoc = pSlice->PicOrderCntMsb + pSlice->pic_order_cnt_lsb;
83         }
84         pSlice->framepoc = pSlice->ThisPOC;
85         p_Vid->ThisPOC = pSlice->ThisPOC;
86         //if ( pSlice->frame_num != p_Vid->PreviousFrameNum) //Seems redundant
87         p_Vid->PreviousFrameNum = pSlice->frame_num;
88         if (pSlice->nal_reference_idc) {
89             p_Vid->PrevPicOrderCntLsb = pSlice->pic_order_cnt_lsb;
90             p_Vid->PrevPicOrderCntMsb = pSlice->PicOrderCntMsb;
91         }
92         break;
93 
94     case 1: // POC MODE 1
95         // 1st
96         if (pSlice->idr_flag) {
97             p_Vid->FrameNumOffset = 0;     //  first pix of IDRGOP,
98             VAL_CHECK(ret, 0 == pSlice->frame_num);
99         } else {
100             if (p_Vid->last_has_mmco_5) {
101                 p_Vid->PreviousFrameNumOffset = 0;
102                 p_Vid->PreviousFrameNum = 0;
103             }
104             if (pSlice->frame_num < (RK_S32)p_Vid->PreviousFrameNum) {
105                 //not first pix of IDRGOP
106                 p_Vid->FrameNumOffset = p_Vid->PreviousFrameNumOffset + p_Vid->max_frame_num;
107             } else {
108                 p_Vid->FrameNumOffset = p_Vid->PreviousFrameNumOffset;
109             }
110         }
111         // 2nd
112         if (active_sps->num_ref_frames_in_pic_order_cnt_cycle) {
113             pSlice->AbsFrameNum = p_Vid->FrameNumOffset + pSlice->frame_num;
114         } else {
115             pSlice->AbsFrameNum = 0;
116         }
117         if ((!pSlice->nal_reference_idc) && pSlice->AbsFrameNum > 0) {
118             pSlice->AbsFrameNum--;
119         }
120         // 3rd
121         p_Vid->ExpectedDeltaPerPicOrderCntCycle = 0;
122         if (active_sps->num_ref_frames_in_pic_order_cnt_cycle) {
123             for (i = 0; i < (RK_S32)active_sps->num_ref_frames_in_pic_order_cnt_cycle; i++) {
124                 p_Vid->ExpectedDeltaPerPicOrderCntCycle += active_sps->offset_for_ref_frame[i];
125             }
126         }
127         if (pSlice->AbsFrameNum) {
128             p_Vid->PicOrderCntCycleCnt = (pSlice->AbsFrameNum - 1) / active_sps->num_ref_frames_in_pic_order_cnt_cycle;
129             p_Vid->FrameNumInPicOrderCntCycle = (pSlice->AbsFrameNum - 1) % active_sps->num_ref_frames_in_pic_order_cnt_cycle;
130             p_Vid->ExpectedPicOrderCnt = p_Vid->PicOrderCntCycleCnt * p_Vid->ExpectedDeltaPerPicOrderCntCycle;
131             for (i = 0; i <= (RK_S32)p_Vid->FrameNumInPicOrderCntCycle; i++)
132                 p_Vid->ExpectedPicOrderCnt += active_sps->offset_for_ref_frame[i];
133         } else {
134             p_Vid->ExpectedPicOrderCnt = 0;
135         }
136         if (!pSlice->nal_reference_idc) {
137             p_Vid->ExpectedPicOrderCnt += active_sps->offset_for_non_ref_pic;
138         }
139         if (pSlice->field_pic_flag == 0) {
140             //frame pix
141             pSlice->toppoc = p_Vid->ExpectedPicOrderCnt + pSlice->delta_pic_order_cnt[0];
142             pSlice->bottompoc = pSlice->toppoc + active_sps->offset_for_top_to_bottom_field + pSlice->delta_pic_order_cnt[1];
143             pSlice->ThisPOC = pSlice->framepoc = (pSlice->toppoc < pSlice->bottompoc) ? pSlice->toppoc : pSlice->bottompoc; // POC200301
144         } else if (pSlice->bottom_field_flag == 0) {
145             //top field
146             pSlice->ThisPOC = pSlice->toppoc = p_Vid->ExpectedPicOrderCnt + pSlice->delta_pic_order_cnt[0];
147         } else {
148             //bottom field
149             pSlice->ThisPOC = pSlice->bottompoc = p_Vid->ExpectedPicOrderCnt + active_sps->offset_for_top_to_bottom_field + pSlice->delta_pic_order_cnt[0];
150         }
151         pSlice->framepoc = pSlice->ThisPOC;
152         p_Vid->PreviousFrameNum = pSlice->frame_num;
153         p_Vid->PreviousFrameNumOffset = p_Vid->FrameNumOffset;
154         break;
155 
156 
157     case 2: // POC MODE 2
158         if (pSlice->idr_flag) { // IDR picture
159             p_Vid->FrameNumOffset = 0;     //  first pix of IDRGOP,
160             pSlice->ThisPOC = pSlice->framepoc = pSlice->toppoc = pSlice->bottompoc = 0;
161             VAL_CHECK(ret, 0 == pSlice->frame_num);
162         } else {
163             if (p_Vid->last_has_mmco_5) {
164                 p_Vid->PreviousFrameNum = 0;
165                 p_Vid->PreviousFrameNumOffset = 0;
166             }
167             if (pSlice->frame_num < (RK_S32)p_Vid->PreviousFrameNum) {
168                 p_Vid->FrameNumOffset = p_Vid->PreviousFrameNumOffset + p_Vid->max_frame_num;
169             } else {
170                 p_Vid->FrameNumOffset = p_Vid->PreviousFrameNumOffset;
171             }
172             pSlice->AbsFrameNum = p_Vid->FrameNumOffset + pSlice->frame_num;
173             if (!pSlice->nal_reference_idc) {
174                 pSlice->ThisPOC = (2 * pSlice->AbsFrameNum - 1);
175             } else {
176                 pSlice->ThisPOC = (2 * pSlice->AbsFrameNum);
177             }
178             if (pSlice->field_pic_flag == 0) {
179                 pSlice->toppoc = pSlice->bottompoc = pSlice->framepoc = pSlice->ThisPOC;
180             } else if (pSlice->bottom_field_flag == 0) {
181                 pSlice->toppoc = pSlice->framepoc = pSlice->ThisPOC;
182             } else {
183                 pSlice->bottompoc = pSlice->framepoc = pSlice->ThisPOC;
184             }
185         }
186         p_Vid->PreviousFrameNum = pSlice->frame_num;
187         p_Vid->PreviousFrameNumOffset = p_Vid->FrameNumOffset;
188         break;
189     default:
190         ret = MPP_NOK;
191         goto __FAILED;
192     }
193     return ret = MPP_OK;
194 
195 __FAILED:
196     return ret;
197 }
198 
store_proc_picture_in_dpb(H264_DpbBuf_t * p_Dpb,H264_StorePic_t * p)199 static MPP_RET store_proc_picture_in_dpb(H264_DpbBuf_t *p_Dpb, H264_StorePic_t *p)
200 {
201     MPP_RET ret = MPP_ERR_UNKNOW;
202     H264dVideoCtx_t *p_Vid = p_Dpb->p_Vid;
203     H264_FrameStore_t *fs = p_Dpb->fs_ilref[0];
204     H264_DecCtx_t *p_Dec = p_Dpb->p_Vid->p_Dec;
205 
206     VAL_CHECK(ret, NULL != p);
207     if (p_Dpb->used_size_il > 0) {
208         if (fs->frame) {
209             free_storable_picture(p_Dec, fs->frame);
210             fs->frame = NULL;
211         }
212         if (fs->top_field) {
213             free_storable_picture(p_Dec, fs->top_field);
214             fs->top_field = NULL;
215         }
216         if (fs->bottom_field) {
217             free_storable_picture(p_Dec, fs->bottom_field);
218             fs->bottom_field = NULL;
219         }
220         fs->is_used = 0;
221         fs->is_reference = 0;
222         p_Dpb->used_size_il--;
223     }
224     if (fs->is_used > 0) { //checking;
225         if (p->structure == FRAME) {
226             VAL_CHECK(ret, fs->frame == NULL);
227         } else if (p->structure == TOP_FIELD) {
228             VAL_CHECK(ret, fs->top_field == NULL);
229         } else if (p->structure == BOTTOM_FIELD) {
230             VAL_CHECK(ret, fs->bottom_field == NULL);
231         }
232     }
233     FUN_CHECK(ret = insert_picture_in_dpb(p_Vid, fs, p, 0));
234     if ((p->structure == FRAME && fs->is_used == 3)
235         || (p->structure != FRAME && fs->is_used && fs->is_used < 3)) {
236         p_Dpb->used_size_il++;
237     }
238 
239     return ret = MPP_OK;
240 __FAILED:
241     return ret;
242 }
243 
dpb_mark_add_used(H264_DpbMark_t * p_mark,RK_S32 structure)244 static void dpb_mark_add_used(H264_DpbMark_t *p_mark, RK_S32 structure)
245 {
246     //!<---- index add ----
247     if (structure == FRAME || structure == TOP_FIELD) {
248         p_mark->top_used += 1;
249     }
250     if (structure == FRAME || structure == BOTTOM_FIELD) {
251         p_mark->bot_used += 1;
252     }
253 }
254 
clone_storable_picture(H264dVideoCtx_t * p_Vid,H264_StorePic_t * p_pic)255 static H264_StorePic_t* clone_storable_picture(H264dVideoCtx_t *p_Vid, H264_StorePic_t *p_pic)
256 {
257     MPP_RET ret = MPP_ERR_UNKNOW;
258     H264_StorePic_t *p_stored_pic = alloc_storable_picture(p_Vid, p_Vid->structure);
259 
260     MEM_CHECK(ret, p_stored_pic);
261     p_stored_pic->mem_malloc_type = Mem_Clone;
262     p_stored_pic->mem_mark = p_pic->mem_mark;
263     dpb_mark_add_used(p_stored_pic->mem_mark, p_stored_pic->structure);
264     p_stored_pic->colmv_no_used_flag = 1;  // clone, colmv is not be used
265 
266     p_stored_pic->pic_num = p_pic->pic_num;
267     p_stored_pic->frame_num = p_pic->frame_num;
268     p_stored_pic->long_term_frame_idx = p_pic->long_term_frame_idx;
269     p_stored_pic->long_term_pic_num = p_pic->long_term_pic_num;
270     p_stored_pic->is_long_term = 0;
271     p_stored_pic->non_existing = p_pic->non_existing;
272     p_stored_pic->max_slice_id = p_pic->max_slice_id;
273     p_stored_pic->structure = p_pic->structure;
274 
275     p_stored_pic->mb_aff_frame_flag = p_pic->mb_aff_frame_flag;
276     p_stored_pic->poc = p_pic->poc;
277     p_stored_pic->top_poc = p_pic->top_poc;
278     p_stored_pic->bottom_poc = p_pic->bottom_poc;
279     p_stored_pic->frame_poc = p_pic->frame_poc;
280     p_stored_pic->is_mmco_5 = p_pic->is_mmco_5;
281     p_stored_pic->poc_mmco5 = p_pic->poc_mmco5;
282     p_stored_pic->top_poc_mmco5 = p_pic->top_poc_mmco5;
283     p_stored_pic->bot_poc_mmco5 = p_pic->bot_poc_mmco5;
284     p_stored_pic->pic_num = p_pic->pic_num;
285     p_stored_pic->frame_num = p_pic->frame_num;
286     p_stored_pic->slice_type = p_pic->slice_type;
287     p_stored_pic->idr_flag = p_pic->idr_flag;
288     p_stored_pic->no_output_of_prior_pics_flag = p_pic->no_output_of_prior_pics_flag;
289     p_stored_pic->long_term_reference_flag = 0;
290     p_stored_pic->adaptive_ref_pic_buffering_flag = 0;
291     p_stored_pic->dec_ref_pic_marking_buffer = NULL;
292     p_stored_pic->PicWidthInMbs = p_pic->PicWidthInMbs;
293 
294     p_stored_pic->chroma_format_idc = p_pic->chroma_format_idc;
295     p_stored_pic->frame_mbs_only_flag = p_pic->frame_mbs_only_flag;
296     p_stored_pic->frame_cropping_flag = p_pic->frame_cropping_flag;
297     if (p_stored_pic->frame_cropping_flag) {
298         p_stored_pic->frame_crop_left_offset = p_pic->frame_crop_left_offset;
299         p_stored_pic->frame_crop_right_offset = p_pic->frame_crop_right_offset;
300         p_stored_pic->frame_crop_top_offset = p_pic->frame_crop_top_offset;
301         p_stored_pic->frame_crop_bottom_offset = p_pic->frame_crop_bottom_offset;
302     }
303     // MVC-related parameters
304     p_stored_pic->inter_view_flag = p_pic->inter_view_flag;
305     p_stored_pic->anchor_pic_flag = 0;
306     p_stored_pic->view_id = p_pic->view_id;
307     p_stored_pic->layer_id = p_pic->layer_id;
308     p_stored_pic->proc_flag = 1;
309     p_stored_pic->is_output = 1;
310     p_stored_pic->used_for_reference = 1;
311 
312     return p_stored_pic;
313 __FAILED:
314     (void)ret;
315     return NULL;
316 }
317 
init_mvc_picture(H264_SLICE_t * currSlice)318 static MPP_RET init_mvc_picture(H264_SLICE_t *currSlice)
319 {
320     RK_U32 i = 0;
321     RK_S32 poc = 0;
322     MPP_RET ret = MPP_ERR_UNKNOW;
323     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
324     H264_DpbBuf_t *p_Dpb = p_Vid->p_Dpb_layer[0];
325     H264_StorePic_t *p_pic = NULL;
326     H264_FrameStore_t *fs = NULL;
327     H264_StorePic_t *p_clone = NULL;
328 
329     // find BL reconstructed picture
330     if (currSlice->structure == FRAME) {
331         for (i = 0; i < p_Dpb->used_size; i++) {
332             fs = p_Dpb->fs[i];
333             if (fs->frame) {
334                 poc = fs->frame->is_mmco_5 ? fs->frame->poc_mmco5 : fs->frame->poc;
335             }
336             if (fs->frame && (fs->frame->layer_id == 0) && (poc == currSlice->framepoc)) {
337                 p_pic = fs->frame;
338                 if (!fs->frame->is_mmco_5) {
339                     break;
340                 }
341             }
342         }
343     } else if (currSlice->structure == TOP_FIELD) {
344         for (i = 0; i < p_Dpb->used_size; i++) {
345             fs = p_Dpb->fs[i];
346             if (fs->top_field) {
347                 poc = fs->top_field->is_mmco_5 ? fs->top_field->top_poc_mmco5 : fs->top_field->top_poc;
348             }
349             if (fs->top_field && (fs->top_field->layer_id == 0) && (poc == currSlice->toppoc)) {
350                 p_pic = fs->top_field;
351                 if (!fs->top_field->is_mmco_5) {
352                     break;
353                 }
354             }
355         }
356     } else {
357         for (i = 0; i < p_Dpb->used_size; i++) {
358             fs = p_Dpb->fs[i];
359             if (fs->bottom_field) {
360                 poc = fs->bottom_field->is_mmco_5 ? fs->bottom_field->bot_poc_mmco5 : fs->bottom_field->bottom_poc;
361             }
362             if (fs->bottom_field && (fs->bottom_field->layer_id == 0) && (poc == currSlice->bottompoc)) {
363                 p_pic = fs->bottom_field;
364                 if (!fs->bottom_field->is_mmco_5) {
365                     break;
366                 }
367             }
368         }
369     }
370     if (p_pic) {
371         p_clone = clone_storable_picture(p_Vid, p_pic);
372         MEM_CHECK(ret, p_clone);
373         FUN_CHECK(ret = store_proc_picture_in_dpb(currSlice->p_Dpb, p_clone));
374     }
375 
376     return ret = MPP_OK;
377 __FAILED:
378     return ret;
379 }
380 
rkv_len_align_422(RK_U32 val)381 static inline RK_U32 rkv_len_align_422(RK_U32 val)
382 {
383     return ((5 * MPP_ALIGN(val, 16)) / 2);
384 }
385 
hor_align_64(RK_U32 val)386 static RK_U32 hor_align_64(RK_U32 val)
387 {
388     return MPP_ALIGN(val, 64);
389 }
390 
dpb_mark_malloc(H264dVideoCtx_t * p_Vid,H264_StorePic_t * dec_pic)391 static MPP_RET dpb_mark_malloc(H264dVideoCtx_t *p_Vid, H264_StorePic_t *dec_pic)
392 {
393     MPP_RET ret = MPP_ERR_UNKNOW;
394     H264_DpbMark_t *cur_mark = NULL;
395     H264_DecCtx_t *p_Dec = p_Vid->p_Dec;
396     H264_DpbMark_t *p_mark = p_Vid->p_Dec->dpb_mark;
397     RK_S32 structure = dec_pic->structure;
398     RK_S32 layer_id = dec_pic->layer_id;
399 
400     if (!dec_pic->combine_flag) {
401         RK_U8 idx = 0;
402         while (p_mark[idx].out_flag || p_mark[idx].top_used
403                || p_mark[idx].bot_used) {
404             idx++;
405             ASSERT(MAX_MARK_SIZE > idx);
406         }
407 
408         mpp_buf_slot_get_unused(p_Vid->p_Dec->frame_slots, &p_mark[idx].slot_idx);
409         if (p_mark[idx].slot_idx < 0) {
410             H264D_WARNNING("[dpb_mark_malloc] error, buf_slot has not get.");
411             ret = MPP_NOK;
412             goto __FAILED;
413         }
414         cur_mark = &p_mark[idx];
415 
416         cur_mark->out_flag = 1;
417         {
418             RK_U32 hor_stride, ver_stride;
419             MppFrameFormat fmt = MPP_FMT_YUV_BUTT;
420             MppFrameFormat out_fmt = p_Dec->cfg->base.out_fmt;
421             MppFrameImpl *impl = (MppFrameImpl *)p_Dec->curframe;
422 
423             if ((H264_CHROMA_400 == p_Vid->yuv_format) && (8 == p_Vid->bit_depth_luma)) {
424                 fmt = MPP_FMT_YUV400;
425             } else if ((H264_CHROMA_420 == p_Vid->yuv_format) && (8 == p_Vid->bit_depth_luma)) {
426                 fmt = MPP_FMT_YUV420SP;
427             } else if ((H264_CHROMA_420 == p_Vid->yuv_format) && (10 == p_Vid->bit_depth_luma)) {
428                 fmt = MPP_FMT_YUV420SP_10BIT;
429             } else if ((H264_CHROMA_422 == p_Vid->yuv_format) && (8 == p_Vid->bit_depth_luma)) {
430                 fmt = MPP_FMT_YUV422SP;
431                 mpp_slots_set_prop(p_Dec->frame_slots, SLOTS_LEN_ALIGN, rkv_len_align_422);
432             } else if ((H264_CHROMA_422 == p_Vid->yuv_format) && (10 == p_Vid->bit_depth_luma)) {
433                 fmt = MPP_FMT_YUV422SP_10BIT;
434                 mpp_slots_set_prop(p_Dec->frame_slots, SLOTS_LEN_ALIGN, rkv_len_align_422);
435             }
436 
437             if (MPP_FRAME_FMT_IS_FBC(out_fmt)) {
438                 do {
439                     /*
440                      * field mode can not use FBC, but VOP only support fbc fmt for 10bit source.
441                      * Generally, there is no 10bit field source.
442                      */
443                     if (!p_Vid->frame_mbs_only_flag && p_Vid->bit_depth_luma < 10)
444                         break;
445 
446                     /*
447                      * When iep working on detection mode disable fbc output mode
448                      */
449                     if ((p_Dec->cfg->base.enable_vproc & MPP_VPROC_MODE_DETECTION) &&
450                         p_Vid->width <= 1920 && p_Vid->height <= 1088 && p_Vid->bit_depth_luma == 8)
451                         break;
452 
453                     mpp_slots_set_prop(p_Dec->frame_slots, SLOTS_HOR_ALIGN, hor_align_64);
454                     fmt |= (out_fmt & MPP_FRAME_FBC_MASK);
455                 } while (0);
456 
457                 p_Dec->cfg->base.out_fmt = fmt;
458                 out_fmt = fmt;
459             }
460             impl->fmt = fmt;
461 
462             hor_stride = MPP_ALIGN(p_Vid->width * p_Vid->bit_depth_luma, 8) / 8;
463             ver_stride = p_Vid->height;
464             /* Before cropping */
465             impl->hor_stride = hor_stride;
466             impl->ver_stride = ver_stride;
467 
468             /* After cropped */
469             impl->width = p_Vid->width_after_crop;
470             impl->height = p_Vid->height_after_crop;
471             impl->pts = p_Vid->p_Cur->last_pts;
472             impl->dts = p_Vid->p_Cur->last_dts;
473 
474             if (MPP_FRAME_FMT_IS_FBC(out_fmt)) {
475                 impl->offset_x = 0;
476                 impl->offset_y = 4;
477 
478                 if (*compat_ext_fbc_buf_size)
479                     impl->ver_stride += 16;
480 
481                 impl->fbc_hdr_stride =  MPP_ALIGN(impl->width, 64);
482                 if (*compat_ext_fbc_hdr_256_odd)
483                     impl->fbc_hdr_stride =  MPP_ALIGN(impl->width, 256) | 256;
484             }
485 
486             /* Setting the interlace mode for the picture */
487             switch (structure) {
488             case FRAME:
489                 impl->mode = MPP_FRAME_FLAG_FRAME;
490                 /* When vproc detection is enabled set frame to field mode */
491                 if ((p_Dec->cfg->base.enable_vproc & MPP_VPROC_MODE_DETECTION) &&
492                     p_Vid->width <= 1920 && p_Vid->height <= 1088 && p_Vid->bit_depth_luma == 8)
493                     impl->mode = MPP_FRAME_FLAG_DEINTERLACED;
494                 break;
495             case TOP_FIELD:
496                 impl->mode = MPP_FRAME_FLAG_PAIRED_FIELD | MPP_FRAME_FLAG_TOP_FIRST;
497                 break;
498             case BOTTOM_FIELD:
499                 impl->mode = MPP_FRAME_FLAG_PAIRED_FIELD | MPP_FRAME_FLAG_BOT_FIRST;
500                 break;
501             default:
502                 H264D_DBG(H264D_DBG_FIELD_PAIRED, "Unknown interlace mode");
503                 mpp_assert(0);
504             }
505 
506             if (p_Dec->svc_valid) {
507                 struct h264_slice_t *slice = &p_Dec->p_Cur->slice;
508                 struct h264_nalu_svc_ext_t *svcExt = &slice->svcExt;
509 
510                 if (svcExt->valid && impl->meta)
511                     mpp_meta_set_s32(impl->meta, KEY_TEMPORAL_ID, svcExt->temporal_id);
512             }
513 
514             if (p_Vid->p_Cur->sei &&
515                 ((p_Vid->active_sps->vui_parameters_present_flag &&
516                   p_Vid->active_sps->vui_seq_parameters.pic_struct_present_flag &&
517                   p_Vid->p_Cur->sei->type == H264_SEI_PIC_TIMING) ||
518                  p_Vid->p_Cur->sei->pic_timing.pic_struct != 0)) {
519                 if (p_Vid->p_Cur->sei->pic_timing.pic_struct == 3 ||
520                     p_Vid->p_Cur->sei->pic_timing.pic_struct == 5)
521                     impl->mode = MPP_FRAME_FLAG_PAIRED_FIELD | MPP_FRAME_FLAG_TOP_FIRST;
522                 if (p_Vid->p_Cur->sei->pic_timing.pic_struct == 4 ||
523                     p_Vid->p_Cur->sei->pic_timing.pic_struct == 6)
524                     impl->mode = MPP_FRAME_FLAG_PAIRED_FIELD | MPP_FRAME_FLAG_BOT_FIRST;
525             }
526 
527             if (p_Dec->cfg->base.enable_thumbnail && p_Dec->hw_info->cap_down_scale)
528                 mpp_frame_set_thumbnail_en(p_Dec->curframe, 1);
529             else
530                 mpp_frame_set_thumbnail_en(p_Dec->curframe, 0);
531 
532             //!< set display parameter
533             if (p_Vid->active_sps->vui_parameters_present_flag) {
534                 H264_VUI_t *p = &p_Vid->active_sps->vui_seq_parameters;
535 
536                 if (p->video_signal_type_present_flag && p->video_full_range_flag)
537                     impl->color_range = MPP_FRAME_RANGE_JPEG;
538                 else
539                     impl->color_range = MPP_FRAME_RANGE_MPEG;
540 
541                 if (p->colour_description_present_flag) {
542                     impl->color_primaries = p->colour_primaries;
543                     impl->color_trc = p->transfer_characteristics;
544                     impl->colorspace = p->matrix_coefficients;
545                 } else {
546                     impl->color_primaries = MPP_FRAME_PRI_UNSPECIFIED;
547                     impl->color_trc = MPP_FRAME_TRC_UNSPECIFIED;
548                     impl->colorspace = MPP_FRAME_SPC_UNSPECIFIED;
549                 }
550             }
551 
552             impl->poc = dec_pic->poc;
553             impl->viewid = dec_pic->layer_id;
554             impl->status.is_intra = dec_pic->slice_type == H264_I_SLICE;
555             impl->status.is_idr = dec_pic->idr_flag;
556             impl->status.is_non_ref = !dec_pic->used_for_reference;
557             impl->status.is_lt_ref = dec_pic->long_term_reference_flag;
558             impl->status.is_b_frame = ((dec_pic->slice_type % 5) == H264_B_SLICE);
559 
560             mpp_buf_slot_set_prop(p_Dec->frame_slots, cur_mark->slot_idx, SLOT_FRAME, p_Dec->curframe);
561             mpp_buf_slot_get_prop(p_Dec->frame_slots, cur_mark->slot_idx, SLOT_FRAME_PTR, &cur_mark->mframe);
562         }
563 
564         p_Vid->active_dpb_mark[layer_id] = cur_mark;
565     }
566     cur_mark = p_Vid->active_dpb_mark[layer_id];
567     if (cur_mark->slot_idx < 0) {
568         H264D_WARNNING("[dpb_mark_malloc] error, current mark slot idx is nagative.");
569         ret = MPP_NOK;
570         goto __FAILED;
571     }
572     if (structure == FRAME || structure == TOP_FIELD) {
573         cur_mark->top_used += 1;
574     }
575     if (structure == FRAME || structure == BOTTOM_FIELD) {
576         cur_mark->bot_used += 1;
577     }
578     H264D_DBG(H264D_DBG_DPB_MALLIC,
579               "[DPB_malloc] g_framecnt=%d, com_flag=%d, mark_idx=%d, slot_idx=%d, slice_type=%d, struct=%d, lay_id=%d\n",
580               p_Vid->g_framecnt, dec_pic->combine_flag, cur_mark->mark_idx,
581               cur_mark->slot_idx, dec_pic->slice_type, dec_pic->structure,
582               layer_id);
583 
584     p_Vid->p_Dec->in_task->output = cur_mark->slot_idx;
585     mpp_buf_slot_set_flag(p_Dec->frame_slots, cur_mark->slot_idx, SLOT_HAL_OUTPUT);
586     p_Dec->last_frame_slot_idx = cur_mark->slot_idx;
587     dec_pic->mem_mark = p_Vid->active_dpb_mark[layer_id];
588     dec_pic->mem_mark->pic = dec_pic;
589 
590     return ret = MPP_OK;
591 __FAILED:
592     dec_pic->mem_mark = NULL;
593     return ret;
594 }
595 
check_dpb_discontinuous(H264_StorePic_t * p_last,H264_StorePic_t * dec_pic,H264_SLICE_t * currSlice)596 static MPP_RET check_dpb_discontinuous(H264_StorePic_t *p_last, H264_StorePic_t *dec_pic, H264_SLICE_t *currSlice)
597 {
598     MPP_RET ret = MPP_ERR_UNKNOW;
599 
600     if (p_last && dec_pic && (dec_pic->slice_type != H264_I_SLICE)
601         && (currSlice->p_Cur->sps.gaps_in_frame_num_value_allowed_flag == 0)) {
602         RK_U32 error_flag = 0;
603 
604         if (dec_pic->combine_flag) {
605             if (dec_pic->frame_num != p_last->frame_num)
606                 error_flag = 1;
607         } else {
608             RK_U32 frame_num = currSlice->p_Vid->last_ref_frame_num;
609 
610             if (dec_pic->frame_num != frame_num &&
611                 dec_pic->frame_num != ((frame_num + 1) % currSlice->p_Vid->max_frame_num))
612                 error_flag = 1;
613         }
614 
615         currSlice->p_Dec->errctx.cur_err_flag |= error_flag ? 1 : 0;
616         currSlice->p_Dec->errctx.dpb_err_flag |= error_flag ? 1 : 0;
617 
618         H264D_DBG(H264D_DBG_DISCONTINUOUS, "[discontinuous] last_slice=%d, cur_slice=%d, last_fnum=%d, cur_fnum=%d, last_poc=%d, cur_poc=%d",
619                   p_last->slice_type, dec_pic->slice_type, p_last->frame_num, dec_pic->frame_num, p_last->poc, dec_pic->poc);
620     }
621 
622     if (dec_pic->idr_flag || dec_pic->used_for_reference)
623         currSlice->p_Vid->last_ref_frame_num = dec_pic->frame_num;
624     return ret = MPP_OK;
625 }
626 
alloc_decpic(H264_SLICE_t * currSlice)627 static MPP_RET alloc_decpic(H264_SLICE_t *currSlice)
628 {
629     MPP_RET ret = MPP_ERR_UNKNOW;
630     H264_StorePic_t *dec_pic = NULL;
631 
632     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
633     H264_SPS_t *active_sps = p_Vid->active_sps;
634     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
635 
636     dec_pic = alloc_storable_picture(p_Vid, currSlice->structure);
637     MEM_CHECK(ret, dec_pic);
638     currSlice->toppoc    = p_Vid->last_toppoc[currSlice->layer_id];
639     currSlice->bottompoc = p_Vid->last_bottompoc[currSlice->layer_id];
640     currSlice->framepoc  = p_Vid->last_framepoc[currSlice->layer_id];
641     currSlice->ThisPOC   = p_Vid->last_thispoc[currSlice->layer_id];
642     FUN_CHECK(ret = decode_poc(p_Vid, currSlice));  //!< calculate POC
643 
644     dec_pic->top_poc    = currSlice->toppoc;
645     dec_pic->bottom_poc = currSlice->bottompoc;
646     dec_pic->frame_poc  = currSlice->framepoc;
647     dec_pic->ThisPOC    = currSlice->ThisPOC;
648 
649     p_Vid->last_toppoc[currSlice->layer_id]    = currSlice->toppoc;
650     p_Vid->last_bottompoc[currSlice->layer_id] = currSlice->bottompoc;
651     p_Vid->last_framepoc[currSlice->layer_id]  = currSlice->framepoc;
652     p_Vid->last_thispoc[currSlice->layer_id]   = currSlice->ThisPOC;
653 
654     if (currSlice->structure == FRAME) {
655         if (currSlice->mb_aff_frame_flag) {
656             dec_pic->iCodingType = FRAME_MB_PAIR_CODING;
657         } else {
658             dec_pic->iCodingType = FRAME_CODING;
659         }
660     } else {
661         dec_pic->iCodingType = FIELD_CODING;
662     }
663     dec_pic->layer_id = currSlice->layer_id;
664     dec_pic->view_id  = currSlice->view_id;
665     dec_pic->inter_view_flag = currSlice->inter_view_flag;
666     dec_pic->anchor_pic_flag = currSlice->anchor_pic_flag;
667     if (dec_pic->layer_id == 1) {
668         if ((p_Vid->profile_idc == H264_PROFILE_MVC_HIGH) || (p_Vid->profile_idc == H264_PROFILE_STEREO_HIGH)) {
669             FUN_CHECK(ret = init_mvc_picture(currSlice));
670         }
671     }
672     if (currSlice->structure == TOP_FIELD) {
673         dec_pic->poc = currSlice->toppoc;
674     } else if (currSlice->structure == BOTTOM_FIELD) {
675         dec_pic->poc = currSlice->bottompoc;
676     } else if (currSlice->structure == FRAME) {
677         dec_pic->poc = currSlice->framepoc;
678     } else {
679         ret = MPP_NOK;
680         goto __FAILED;
681     }
682     dec_pic->slice_type = p_Vid->type;
683     dec_pic->used_for_reference = (currSlice->nal_reference_idc != 0);
684     dec_pic->idr_flag = currSlice->idr_flag;
685     dec_pic->no_output_of_prior_pics_flag = currSlice->no_output_of_prior_pics_flag;
686     dec_pic->long_term_reference_flag = currSlice->long_term_reference_flag;
687     dec_pic->adaptive_ref_pic_buffering_flag = currSlice->adaptive_ref_pic_buffering_flag;
688     dec_pic->dec_ref_pic_marking_buffer = currSlice->dec_ref_pic_marking_buffer;
689 
690     currSlice->dec_ref_pic_marking_buffer = NULL;
691     dec_pic->mb_aff_frame_flag = currSlice->mb_aff_frame_flag;
692     dec_pic->PicWidthInMbs = p_Vid->PicWidthInMbs;
693     dec_pic->pic_num = currSlice->frame_num;
694     dec_pic->frame_num = currSlice->frame_num;
695     dec_pic->chroma_format_idc = active_sps->chroma_format_idc;
696 
697     dec_pic->frame_mbs_only_flag = active_sps->frame_mbs_only_flag;
698     dec_pic->frame_cropping_flag = active_sps->frame_cropping_flag;
699     if (dec_pic->frame_cropping_flag) {
700         dec_pic->frame_crop_left_offset = active_sps->frame_crop_left_offset;
701         dec_pic->frame_crop_right_offset = active_sps->frame_crop_right_offset;
702         dec_pic->frame_crop_top_offset = active_sps->frame_crop_top_offset;
703         dec_pic->frame_crop_bottom_offset = active_sps->frame_crop_bottom_offset;
704     } else {
705         dec_pic->frame_crop_left_offset = 0;
706         dec_pic->frame_crop_right_offset = 0;
707         dec_pic->frame_crop_top_offset = 0;
708         dec_pic->frame_crop_bottom_offset = 0;
709     }
710     dec_pic->width = p_Vid->width;
711     dec_pic->height = p_Vid->height;
712     dec_pic->width_after_crop = p_Vid->width_after_crop;
713     dec_pic->height_after_crop = p_Vid->height_after_crop;
714     dec_pic->combine_flag = get_field_dpb_combine_flag(p_Dpb->last_picture, dec_pic);
715     /* malloc dpb_memory */
716     FUN_CHECK(ret = dpb_mark_malloc(p_Vid, dec_pic));
717     FUN_CHECK(ret = check_dpb_discontinuous(p_Vid->last_pic, dec_pic, currSlice));
718     dec_pic->mem_malloc_type = Mem_Malloc;
719     dec_pic->colmv_no_used_flag = 0;
720     p_Vid->dec_pic = dec_pic;
721 
722     return ret = MPP_OK;
723 __FAILED:
724     mpp_mem_pool_put(p_Vid->pic_st, dec_pic);
725     p_Vid->dec_pic = NULL;
726 
727     return ret;
728 }
729 
update_pic_num(H264_SLICE_t * currSlice)730 static void update_pic_num(H264_SLICE_t *currSlice)
731 {
732     RK_U32 i = 0;
733     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
734     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
735     H264_SPS_t *active_sps = p_Vid->active_sps;
736 
737     RK_S32 add_top = 0, add_bottom = 0;
738     RK_S32 max_frame_num = 1 << (active_sps->log2_max_frame_num_minus4 + 4);
739 
740     if (currSlice->idr_flag) {
741         return;
742     }
743     if (currSlice->structure == FRAME) {
744         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
745             if (p_Dpb->fs_ref[i]->is_used == 3) {
746                 if ((p_Dpb->fs_ref[i]->frame->used_for_reference) && (!p_Dpb->fs_ref[i]->frame->is_long_term)) {
747                     if ((RK_S32)p_Dpb->fs_ref[i]->frame_num > currSlice->frame_num) {
748                         p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num - max_frame_num;
749                     } else {
750                         p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num;
751                     }
752                     p_Dpb->fs_ref[i]->frame->pic_num = p_Dpb->fs_ref[i]->frame_num_wrap;
753                 }
754             }
755         }
756         //!< update long_term_pic_num
757         for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
758             if (p_Dpb->fs_ltref[i]->is_used == 3) {
759                 if (p_Dpb->fs_ltref[i]->frame->is_long_term) {
760                     p_Dpb->fs_ltref[i]->frame->long_term_pic_num = p_Dpb->fs_ltref[i]->frame->long_term_frame_idx;
761                 }
762             }
763         }
764     } else {
765         if (currSlice->structure == TOP_FIELD) {
766             add_top = 1;
767             add_bottom = 0;
768         } else {
769             add_top = 0;
770             add_bottom = 1;
771         }
772 
773         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
774             if (p_Dpb->fs_ref[i]->is_reference) {
775                 if ((RK_S32)p_Dpb->fs_ref[i]->frame_num > currSlice->frame_num) {
776                     p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num - max_frame_num;
777                 } else {
778                     p_Dpb->fs_ref[i]->frame_num_wrap = p_Dpb->fs_ref[i]->frame_num;
779                 }
780                 if (p_Dpb->fs_ref[i]->is_reference & 1) {
781                     p_Dpb->fs_ref[i]->top_field->pic_num = (2 * p_Dpb->fs_ref[i]->frame_num_wrap) + add_top;
782                 }
783                 if (p_Dpb->fs_ref[i]->is_reference & 2) {
784                     p_Dpb->fs_ref[i]->bottom_field->pic_num = (2 * p_Dpb->fs_ref[i]->frame_num_wrap) + add_bottom;
785                 }
786             }
787         }
788         //!< update long_term_pic_num
789         for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
790             if (p_Dpb->fs_ltref[i]->is_long_term & 1) {
791                 p_Dpb->fs_ltref[i]->top_field->long_term_pic_num = 2 * p_Dpb->fs_ltref[i]->top_field->long_term_frame_idx + add_top;
792             }
793             if (p_Dpb->fs_ltref[i]->is_long_term & 2) {
794                 p_Dpb->fs_ltref[i]->bottom_field->long_term_pic_num = 2 * p_Dpb->fs_ltref[i]->bottom_field->long_term_frame_idx + add_bottom;
795             }
796         }
797     }
798 }
799 
800 
801 
802 
compare_pic_by_pic_num_desc(const void * arg1,const void * arg2)803 static RK_S32 compare_pic_by_pic_num_desc(const void *arg1, const void *arg2)
804 {
805     RK_S32 pic_num1 = (*(H264_StorePic_t**)arg1)->pic_num;
806     RK_S32 pic_num2 = (*(H264_StorePic_t**)arg2)->pic_num;
807 
808     if (pic_num1 < pic_num2)
809         return 1;
810     if (pic_num1 > pic_num2)
811         return -1;
812     else
813         return 0;
814 }
815 
compare_pic_by_lt_pic_num_asc(const void * arg1,const void * arg2)816 static RK_S32 compare_pic_by_lt_pic_num_asc(const void *arg1, const void *arg2)
817 {
818     RK_S32 long_term_pic_num1 = (*(H264_StorePic_t**)arg1)->long_term_pic_num;
819     RK_S32 long_term_pic_num2 = (*(H264_StorePic_t**)arg2)->long_term_pic_num;
820 
821     if (long_term_pic_num1 < long_term_pic_num2)
822         return -1;
823     if (long_term_pic_num1 > long_term_pic_num2)
824         return 1;
825     else
826         return 0;
827 }
828 
compare_fs_by_frame_num_desc(const void * arg1,const void * arg2)829 static RK_S32 compare_fs_by_frame_num_desc(const void *arg1, const void *arg2)
830 {
831     RK_S32 frame_num_wrap1 = (*(H264_FrameStore_t**)arg1)->frame_num_wrap;
832     RK_S32 frame_num_wrap2 = (*(H264_FrameStore_t**)arg2)->frame_num_wrap;
833     if (frame_num_wrap1 < frame_num_wrap2)
834         return 1;
835     if (frame_num_wrap1 > frame_num_wrap2)
836         return -1;
837     else
838         return 0;
839 }
840 
compare_fs_by_lt_pic_idx_asc(const void * arg1,const void * arg2)841 static RK_S32 compare_fs_by_lt_pic_idx_asc(const void *arg1, const void *arg2)
842 {
843     RK_S32 long_term_frame_idx1 = (*(H264_FrameStore_t**)arg1)->long_term_frame_idx;
844     RK_S32 long_term_frame_idx2 = (*(H264_FrameStore_t**)arg2)->long_term_frame_idx;
845 
846     if (long_term_frame_idx1 < long_term_frame_idx2)
847         return -1;
848     else if (long_term_frame_idx1 > long_term_frame_idx2)
849         return 1;
850     else
851         return 0;
852 }
853 
is_long_ref(H264_StorePic_t * s)854 static RK_U32 is_long_ref(H264_StorePic_t *s)
855 {
856     return ((s->used_for_reference) && (s->is_long_term));
857 }
858 
is_short_ref(H264_StorePic_t * s)859 static RK_U32 is_short_ref(H264_StorePic_t *s)
860 {
861     return ((s->used_for_reference) && (!(s->is_long_term)));
862 }
863 
gen_pic_list_from_frame_list(RK_S32 currStructure,H264_FrameStore_t ** fs_list,RK_S32 list_idx,H264_StorePic_t ** list,RK_U8 * list_size,RK_U32 long_term)864 static void gen_pic_list_from_frame_list(RK_S32 currStructure, H264_FrameStore_t **fs_list,
865                                          RK_S32 list_idx, H264_StorePic_t **list, RK_U8 *list_size, RK_U32 long_term)
866 {
867     RK_S32 top_idx = 0;
868     RK_S32 bot_idx = 0;
869 
870     RK_U32(*is_ref)(H264_StorePic_t * s) = (long_term) ? is_long_ref : is_short_ref;
871 
872     if (currStructure == TOP_FIELD) {
873         while ((top_idx < list_idx) || (bot_idx < list_idx)) {
874             for (; top_idx < list_idx; top_idx++) {
875                 if (fs_list[top_idx]->is_used & 1) {
876                     if (is_ref(fs_list[top_idx]->top_field)) {
877                         //<! short term ref pic
878                         list[(RK_S16)*list_size] = fs_list[top_idx]->top_field;
879                         (*list_size)++;
880                         top_idx++;
881                         break;
882                     }
883                 }
884             }
885             for (; bot_idx < list_idx; bot_idx++) {
886                 if (fs_list[bot_idx]->is_used & 2) {
887                     if (is_ref(fs_list[bot_idx]->bottom_field)) {
888                         //<! short term ref pic
889                         list[(RK_S16)*list_size] = fs_list[bot_idx]->bottom_field;
890                         (*list_size)++;
891                         bot_idx++;
892                         break;
893                     }
894                 }
895             }
896         }
897     }
898     if (currStructure == BOTTOM_FIELD) {
899         while ((top_idx < list_idx) || (bot_idx < list_idx)) {
900             for (; bot_idx < list_idx; bot_idx++) {
901                 if (fs_list[bot_idx]->is_used & 2) {
902                     if (is_ref(fs_list[bot_idx]->bottom_field)) {
903                         // short term ref pic
904                         list[(RK_S16)*list_size] = fs_list[bot_idx]->bottom_field;
905                         (*list_size)++;
906                         bot_idx++;
907                         break;
908                     }
909                 }
910             }
911             for (; top_idx < list_idx; top_idx++) {
912                 if (fs_list[top_idx]->is_used & 1) {
913                     if (is_ref(fs_list[top_idx]->top_field)) {
914                         //!< short term ref pic
915                         list[(RK_S16)*list_size] = fs_list[top_idx]->top_field;
916                         (*list_size)++;
917                         top_idx++;
918                         break;
919                     }
920                 }
921             }
922         }
923     }
924 }
925 
is_view_id_in_ref_view_list(RK_S32 view_id,RK_S32 * ref_view_id,RK_S32 num_ref_views)926 static RK_U32 is_view_id_in_ref_view_list(RK_S32 view_id, RK_S32 *ref_view_id, RK_S32 num_ref_views)
927 {
928     RK_S32 i;
929     for (i = 0; i < num_ref_views; i++) {
930         if (view_id == ref_view_id[i])
931             break;
932     }
933 
934     return (num_ref_views && (i < num_ref_views));
935 }
936 
append_interview_list(H264_DpbBuf_t * p_Dpb,RK_S32 currPicStructure,RK_S32 list_idx,H264_FrameStore_t ** list,RK_S32 * listXsize,RK_S32 currPOC,RK_S32 curr_layer_id,RK_S32 anchor_pic_flag)937 static MPP_RET append_interview_list(H264_DpbBuf_t *p_Dpb,
938                                      RK_S32 currPicStructure, RK_S32 list_idx, H264_FrameStore_t **list,
939                                      RK_S32 *listXsize, RK_S32 currPOC, RK_S32 curr_layer_id, RK_S32 anchor_pic_flag)
940 {
941     RK_S32 poc = 0;
942     RK_S32 fld_idx = 0;
943     RK_U32 pic_avail = 0;
944     RK_S32 num_ref_views = 0;
945     RK_S32 *ref_view_id = NULL;
946     MPP_RET ret = MPP_ERR_UNKNOW;
947     RK_S32 iVOIdx = curr_layer_id;
948     H264_FrameStore_t *fs = p_Dpb->fs_ilref[0];
949     H264dVideoCtx_t *p_Vid = p_Dpb->p_Vid;
950 
951     VAL_CHECK(ret, iVOIdx >= 0); //!< Error: iVOIdx: %d is not less than 0
952     if (anchor_pic_flag) {
953         num_ref_views = list_idx ? p_Vid->active_subsps->num_anchor_refs_l1[iVOIdx] : p_Vid->active_subsps->num_anchor_refs_l0[iVOIdx];
954         ref_view_id = list_idx ? p_Vid->active_subsps->anchor_ref_l1[iVOIdx] : p_Vid->active_subsps->anchor_ref_l0[iVOIdx];
955     } else {
956         num_ref_views = list_idx ? p_Vid->active_subsps->num_non_anchor_refs_l1[iVOIdx] : p_Vid->active_subsps->num_non_anchor_refs_l0[iVOIdx];
957         ref_view_id = list_idx ? p_Vid->active_subsps->non_anchor_ref_l1[iVOIdx] : p_Vid->active_subsps->non_anchor_ref_l0[iVOIdx];
958     }
959 
960     if (currPicStructure == BOTTOM_FIELD)
961         fld_idx = 1;
962     else
963         fld_idx = 0;
964 
965     if (currPicStructure == FRAME) {
966         pic_avail = (fs->is_used == 3);
967         if (pic_avail) {
968             poc = fs->frame->is_mmco_5 ? fs->frame->poc_mmco5 : fs->frame->poc;
969         }
970     } else if (currPicStructure == TOP_FIELD) {
971         pic_avail = fs->is_used & 1;
972         if (pic_avail) {
973             poc = fs->top_field->is_mmco_5 ? fs->top_field->top_poc_mmco5 : fs->top_field->poc;
974         }
975     } else if (currPicStructure == BOTTOM_FIELD) {
976         pic_avail = fs->is_used & 2;
977         if (pic_avail) {
978             poc = fs->bottom_field->is_mmco_5 ? fs->bottom_field->bot_poc_mmco5 : fs->bottom_field->poc;
979         }
980     } else {
981         pic_avail = 0;
982     }
983 
984     if (pic_avail && fs->inter_view_flag[fld_idx]) {
985         if (poc == currPOC) {
986             if (is_view_id_in_ref_view_list(fs->view_id, ref_view_id, num_ref_views)) {
987                 //!< add one inter-view reference;
988                 list[*listXsize] = fs;
989                 //!< next;
990                 (*listXsize)++;
991             }
992         }
993     }
994     return ret = MPP_OK;
995 __FAILED:
996     return ret;
997 }
998 
gen_pic_list_from_frame_interview_list(RK_S32 currStructure,H264_FrameStore_t ** fs_list,RK_S32 list_idx,H264_StorePic_t ** list,RK_U8 * list_size)999 static void gen_pic_list_from_frame_interview_list(RK_S32 currStructure,
1000                                                    H264_FrameStore_t **fs_list, RK_S32 list_idx, H264_StorePic_t **list, RK_U8 *list_size)
1001 {
1002     RK_S32 i;
1003 
1004     if (currStructure == TOP_FIELD) {
1005         for (i = 0; i < list_idx; i++) {
1006             list[(RK_S32)(*list_size)] = fs_list[i]->top_field;
1007             (*list_size)++;
1008         }
1009     }
1010     if (currStructure == BOTTOM_FIELD) {
1011         for (i = 0; i < list_idx; i++) {
1012             list[(RK_S32)(*list_size)] = fs_list[i]->bottom_field;
1013             (*list_size)++;
1014         }
1015     }
1016 }
1017 
compare_pic_by_poc_desc(const void * arg1,const void * arg2)1018 static RK_S32 compare_pic_by_poc_desc(const void *arg1, const void *arg2)
1019 {
1020     RK_S32 poc1 = (*(H264_StorePic_t**)arg1)->poc;
1021     RK_S32 poc2 = (*(H264_StorePic_t**)arg2)->poc;
1022 
1023     if (poc1 < poc2)
1024         return 1;
1025     if (poc1 > poc2)
1026         return -1;
1027     else
1028         return 0;
1029 }
1030 
compare_pic_by_poc_asc(const void * arg1,const void * arg2)1031 static RK_S32 compare_pic_by_poc_asc(const void *arg1, const void *arg2)
1032 {
1033     RK_S32 poc1 = (*(H264_StorePic_t**)arg1)->poc;
1034     RK_S32 poc2 = (*(H264_StorePic_t**)arg2)->poc;
1035 
1036     if (poc1 < poc2)
1037         return -1;
1038     if (poc1 > poc2)
1039         return 1;
1040     else
1041         return 0;
1042 }
1043 
compare_fs_by_poc_desc(const void * arg1,const void * arg2)1044 static RK_S32 compare_fs_by_poc_desc(const void *arg1, const void *arg2)
1045 {
1046     RK_S32 poc1 = (*(H264_FrameStore_t**)arg1)->poc;
1047     RK_S32 poc2 = (*(H264_FrameStore_t**)arg2)->poc;
1048 
1049     if (poc1 < poc2)
1050         return 1;
1051     else if (poc1 > poc2)
1052         return -1;
1053     else
1054         return 0;
1055 }
1056 
compare_fs_by_poc_asc(const void * arg1,const void * arg2)1057 static RK_S32 compare_fs_by_poc_asc(const void *arg1, const void *arg2)
1058 {
1059     RK_S32 poc1 = (*(H264_FrameStore_t**)arg1)->poc;
1060     RK_S32 poc2 = (*(H264_FrameStore_t**)arg2)->poc;
1061 
1062     if (poc1 < poc2)
1063         return -1;
1064     else if (poc1 > poc2)
1065         return 1;
1066     else
1067         return 0;
1068 }
1069 
init_lists_p_slice_mvc(H264_SLICE_t * currSlice)1070 static MPP_RET init_lists_p_slice_mvc(H264_SLICE_t *currSlice)
1071 {
1072     RK_U32 i = 0;
1073     RK_S32 list0idx = 0;
1074     RK_S32 listltidx = 0;
1075     H264_FrameStore_t **fs_list0 = 0;
1076     H264_FrameStore_t **fs_listlt = 0;
1077     MPP_RET ret = MPP_ERR_UNKNOW;
1078     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
1079     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
1080     RK_S32 currPOC = currSlice->ThisPOC;
1081     RK_S32 anchor_pic_flag = currSlice->anchor_pic_flag;
1082 
1083     currSlice->listXsizeP[0] = 0;
1084     currSlice->listXsizeP[1] = 0;
1085     currSlice->listinterviewidx0 = 0;
1086     currSlice->listinterviewidx1 = 0;
1087 
1088     if (currSlice->structure == FRAME) {
1089         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
1090             if (p_Dpb->fs_ref[i]->is_used == 3) {
1091                 if ((p_Dpb->fs_ref[i]->frame->used_for_reference) && (!p_Dpb->fs_ref[i]->frame->is_long_term)) {
1092                     currSlice->listP[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
1093                 }
1094             }
1095         }
1096         // order list 0 by PicNum
1097         qsort((void *)currSlice->listP[0], list0idx, sizeof(H264_StorePic_t*), compare_pic_by_pic_num_desc);
1098         currSlice->listXsizeP[0] = (RK_U8)list0idx;
1099         // long term handling
1100         for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
1101             if (p_Dpb->fs_ltref[i]->is_used == 3) {
1102                 if (p_Dpb->fs_ltref[i]->frame->is_long_term) {
1103                     currSlice->listP[0][list0idx++] = p_Dpb->fs_ltref[i]->frame;
1104                 }
1105             }
1106         }
1107         qsort((void *)&currSlice->listP[0][(RK_S16)currSlice->listXsizeP[0]],
1108               list0idx - currSlice->listXsizeP[0], sizeof(H264_StorePic_t*), compare_pic_by_lt_pic_num_asc);
1109         currSlice->listXsizeP[0] = (RK_U8)list0idx;
1110     } else {
1111         fs_list0  = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1112         fs_listlt = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1113         MEM_CHECK(ret, fs_list0 && fs_listlt);
1114         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
1115             if (p_Dpb->fs_ref[i]->is_reference) {
1116                 fs_list0[list0idx++] = p_Dpb->fs_ref[i];
1117             }
1118         }
1119         qsort((void *)fs_list0, list0idx, sizeof(H264_FrameStore_t*), compare_fs_by_frame_num_desc);
1120         currSlice->listXsizeP[0] = 0;
1121         gen_pic_list_from_frame_list(currSlice->structure, fs_list0, list0idx, currSlice->listP[0], &currSlice->listXsizeP[0], 0);
1122         // long term handling
1123         for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
1124             fs_listlt[listltidx++] = p_Dpb->fs_ltref[i];
1125         }
1126         qsort((void *)fs_listlt, listltidx, sizeof(H264_FrameStore_t*), compare_fs_by_lt_pic_idx_asc);
1127         gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, currSlice->listP[0], &currSlice->listXsizeP[0], 1);
1128         MPP_FREE(fs_list0);
1129         MPP_FREE(fs_listlt);
1130     }
1131 
1132     currSlice->listXsizeP[1] = 0;
1133     if (currSlice->mvcExt.valid && currSlice->svc_extension_flag == 0) {
1134         RK_S32 curr_layer_id = currSlice->layer_id;
1135         currSlice->fs_listinterview0 = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1136         MEM_CHECK(ret, currSlice->fs_listinterview0);
1137         list0idx = currSlice->listXsizeP[0];
1138         if (currSlice->structure == FRAME) {
1139             FUN_CHECK(ret = append_interview_list(p_Vid->p_Dpb_layer[1], currSlice->structure, 0,
1140                                                   currSlice->fs_listinterview0, &currSlice->listinterviewidx0, currPOC, curr_layer_id, anchor_pic_flag));
1141             for (i = 0; i < (RK_U32)currSlice->listinterviewidx0; i++) {
1142                 currSlice->listP[0][list0idx++] = currSlice->fs_listinterview0[i]->frame;
1143             }
1144             currSlice->listXsizeP[0] = (RK_U8)list0idx;
1145         } else {
1146             FUN_CHECK(ret = append_interview_list(p_Vid->p_Dpb_layer[1], currSlice->structure, 0,
1147                                                   currSlice->fs_listinterview0, &currSlice->listinterviewidx0, currPOC, curr_layer_id, anchor_pic_flag));
1148             gen_pic_list_from_frame_interview_list(currSlice->structure, currSlice->fs_listinterview0,
1149                                                    currSlice->listinterviewidx0, currSlice->listP[0], &currSlice->listXsizeP[0]);
1150         }
1151     }
1152     // set the unused list entries to NULL
1153     for (i = currSlice->listXsizeP[0]; i < (MAX_LIST_SIZE); i++) {
1154         currSlice->listP[0][i] = p_Vid->no_ref_pic;
1155     }
1156     for (i = currSlice->listXsizeP[1]; i < (MAX_LIST_SIZE); i++) {
1157         currSlice->listP[1][i] = p_Vid->no_ref_pic;
1158     }
1159     MPP_FREE(currSlice->fs_listinterview0);
1160 
1161     return ret = MPP_OK;
1162 __FAILED:
1163     MPP_FREE(fs_list0);
1164     MPP_FREE(fs_listlt);
1165     MPP_FREE(currSlice->fs_listinterview0);
1166 
1167     return ret;
1168 }
1169 
init_lists_b_slice_mvc(H264_SLICE_t * currSlice)1170 static MPP_RET init_lists_b_slice_mvc(H264_SLICE_t *currSlice)
1171 {
1172     RK_U32 i = 0;
1173     RK_S32 j = 0;
1174     RK_S32 list0idx = 0;
1175     RK_S32 list0idx_1 = 0;
1176     RK_S32 listltidx = 0;
1177     H264_FrameStore_t **fs_list0 = NULL;
1178     H264_FrameStore_t **fs_list1 = NULL;
1179     H264_FrameStore_t **fs_listlt = NULL;
1180     MPP_RET ret = MPP_ERR_UNKNOW;
1181 
1182     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
1183     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
1184     RK_S32 currPOC = currSlice->ThisPOC;
1185     RK_S32 anchor_pic_flag = currSlice->anchor_pic_flag;
1186 
1187     currSlice->listXsizeB[0] = 0;
1188     currSlice->listXsizeB[1] = 0;
1189     currSlice->listinterviewidx0 = 0;
1190     currSlice->listinterviewidx1 = 0;
1191     // B-Slice
1192     if (currSlice->structure == FRAME) {
1193         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
1194             if (p_Dpb->fs_ref[i]->is_used == 3) {
1195                 if ((p_Dpb->fs_ref[i]->frame->used_for_reference) && (!p_Dpb->fs_ref[i]->frame->is_long_term)) {
1196                     if (currSlice->framepoc >= p_Dpb->fs_ref[i]->frame->poc) {
1197                         currSlice->listB[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
1198                     }
1199                 }
1200             }
1201         }
1202         qsort((void *)currSlice->listB[0], list0idx, sizeof(H264_StorePic_t*), compare_pic_by_poc_desc);
1203         list0idx_1 = list0idx;
1204         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
1205             if (p_Dpb->fs_ref[i]->is_used == 3) {
1206                 if ((p_Dpb->fs_ref[i]->frame->used_for_reference) && (!p_Dpb->fs_ref[i]->frame->is_long_term)) {
1207                     if (currSlice->framepoc < p_Dpb->fs_ref[i]->frame->poc) {
1208                         currSlice->listB[0][list0idx++] = p_Dpb->fs_ref[i]->frame;
1209                     }
1210                 }
1211             }
1212         }
1213         qsort((void *)&currSlice->listB[0][list0idx_1], list0idx - list0idx_1, sizeof(H264_StorePic_t*), compare_pic_by_poc_asc);
1214 
1215         for (j = 0; j < list0idx_1; j++) {
1216             currSlice->listB[1][list0idx - list0idx_1 + j] = currSlice->listB[0][j];
1217         }
1218         for (j = list0idx_1; j < list0idx; j++) {
1219             currSlice->listB[1][j - list0idx_1] = currSlice->listB[0][j];
1220         }
1221         currSlice->listXsizeB[0] = currSlice->listXsizeB[1] = (RK_U8)list0idx;
1222         // long term handling
1223         for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
1224             if (p_Dpb->fs_ltref[i]->is_used == 3) {
1225                 if (p_Dpb->fs_ltref[i]->frame->is_long_term) {
1226                     currSlice->listB[0][list0idx] = p_Dpb->fs_ltref[i]->frame;
1227                     currSlice->listB[1][list0idx++] = p_Dpb->fs_ltref[i]->frame;
1228                 }
1229             }
1230         }
1231         qsort((void *)&currSlice->listB[0][(RK_S16)currSlice->listXsizeB[0]],
1232               list0idx - currSlice->listXsizeB[0], sizeof(H264_StorePic_t*), compare_pic_by_lt_pic_num_asc);
1233         qsort((void *)&currSlice->listB[1][(RK_S16)currSlice->listXsizeB[0]],
1234               list0idx - currSlice->listXsizeB[0], sizeof(H264_StorePic_t*), compare_pic_by_lt_pic_num_asc);
1235         currSlice->listXsizeB[0] = currSlice->listXsizeB[1] = (RK_U8)list0idx;
1236     } else {
1237         fs_list0  = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1238         fs_list1  = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1239         fs_listlt = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1240         MEM_CHECK(ret, fs_list0 && fs_list1 && fs_listlt);
1241         currSlice->listXsizeB[0] = 0;
1242         currSlice->listXsizeB[1] = 1;
1243         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
1244             if (p_Dpb->fs_ref[i]->is_used) {
1245                 if (currSlice->ThisPOC >= p_Dpb->fs_ref[i]->poc) {
1246                     fs_list0[list0idx++] = p_Dpb->fs_ref[i];
1247                 }
1248             }
1249         }
1250         qsort((void *)fs_list0, list0idx, sizeof(H264_FrameStore_t*), compare_fs_by_poc_desc);
1251         list0idx_1 = list0idx;
1252         for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) {
1253             if (p_Dpb->fs_ref[i]->is_used) {
1254                 if (currSlice->ThisPOC < p_Dpb->fs_ref[i]->poc) {
1255                     fs_list0[list0idx++] = p_Dpb->fs_ref[i];
1256                 }
1257             }
1258         }
1259         qsort((void *)&fs_list0[list0idx_1], list0idx - list0idx_1, sizeof(H264_FrameStore_t*), compare_fs_by_poc_asc);
1260 
1261         for (j = 0; j < list0idx_1; j++) {
1262             fs_list1[list0idx - list0idx_1 + j] = fs_list0[j];
1263         }
1264         for (j = list0idx_1; j < list0idx; j++) {
1265             fs_list1[j - list0idx_1] = fs_list0[j];
1266         }
1267         currSlice->listXsizeB[0] = 0;
1268         currSlice->listXsizeB[1] = 0;
1269         gen_pic_list_from_frame_list(currSlice->structure, fs_list0, list0idx, currSlice->listB[0], &currSlice->listXsizeB[0], 0);
1270         gen_pic_list_from_frame_list(currSlice->structure, fs_list1, list0idx, currSlice->listB[1], &currSlice->listXsizeB[1], 0);
1271 
1272         // long term handling
1273         for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
1274             fs_listlt[listltidx++] = p_Dpb->fs_ltref[i];
1275         }
1276         qsort((void *)fs_listlt, listltidx, sizeof(H264_FrameStore_t*), compare_fs_by_lt_pic_idx_asc);
1277 
1278         gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, currSlice->listB[0], &currSlice->listXsizeB[0], 1);
1279         gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, listltidx, currSlice->listB[1], &currSlice->listXsizeB[1], 1);
1280 
1281         MPP_FREE(fs_list0);
1282         MPP_FREE(fs_list1);
1283         MPP_FREE(fs_listlt);
1284     }
1285     if ((currSlice->listXsizeB[0] == currSlice->listXsizeB[1]) && (currSlice->listXsizeB[0] > 1)) {
1286         // check if lists are identical, if yes swap first two elements of currSlice->listX[1]
1287         RK_S32 diff = 0;
1288         for (j = 0; j < currSlice->listXsizeB[0]; j++) {
1289             if (currSlice->listB[0][j] != currSlice->listB[1][j]) {
1290                 diff = 1;
1291                 break;
1292             }
1293         }
1294         if (!diff) {
1295             H264_StorePic_t *tmp_s = currSlice->listB[1][0];
1296             currSlice->listB[1][0] = currSlice->listB[1][1];
1297             currSlice->listB[1][1] = tmp_s;
1298         }
1299     }
1300     if (currSlice->mvcExt.valid && currSlice->svc_extension_flag == 0) {
1301         RK_S32 curr_layer_id = currSlice->layer_id;
1302         // B-Slice
1303         currSlice->fs_listinterview0 = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1304         currSlice->fs_listinterview1 = mpp_calloc(H264_FrameStore_t*, p_Dpb->size);
1305         MEM_CHECK(ret, currSlice->fs_listinterview0 && currSlice->fs_listinterview1);
1306         list0idx = currSlice->listXsizeB[0];
1307         if (currSlice->structure == FRAME) {
1308             FUN_CHECK(ret = append_interview_list(p_Vid->p_Dpb_layer[1], currSlice->structure, 0,
1309                                                   currSlice->fs_listinterview0, &currSlice->listinterviewidx0, currPOC, curr_layer_id, anchor_pic_flag));
1310             FUN_CHECK(ret = append_interview_list(p_Vid->p_Dpb_layer[1], currSlice->structure, 1,
1311                                                   currSlice->fs_listinterview1, &currSlice->listinterviewidx1, currPOC, curr_layer_id, anchor_pic_flag));
1312 
1313             for (i = 0; i < (RK_U32)currSlice->listinterviewidx0; i++) {
1314                 currSlice->listB[0][list0idx++] = currSlice->fs_listinterview0[i]->frame;
1315             }
1316             currSlice->listXsizeB[0] = (RK_U8)list0idx;
1317             list0idx = currSlice->listXsizeB[1];
1318             for (i = 0; i < (RK_U32)currSlice->listinterviewidx1; i++) {
1319                 currSlice->listB[1][list0idx++] = currSlice->fs_listinterview1[i]->frame;
1320             }
1321             currSlice->listXsizeB[1] = (RK_U8)list0idx;
1322         } else {
1323             FUN_CHECK(ret = append_interview_list(p_Vid->p_Dpb_layer[1], currSlice->structure, 0,
1324                                                   currSlice->fs_listinterview0, &currSlice->listinterviewidx0, currPOC, curr_layer_id, anchor_pic_flag));
1325             gen_pic_list_from_frame_interview_list(currSlice->structure, currSlice->fs_listinterview0,
1326                                                    currSlice->listinterviewidx0, currSlice->listB[0], &currSlice->listXsizeB[0]);
1327             FUN_CHECK(ret = append_interview_list(p_Vid->p_Dpb_layer[1], currSlice->structure, 1,
1328                                                   currSlice->fs_listinterview1, &currSlice->listinterviewidx1, currPOC, curr_layer_id, anchor_pic_flag));
1329             gen_pic_list_from_frame_interview_list(currSlice->structure, currSlice->fs_listinterview1,
1330                                                    currSlice->listinterviewidx1, currSlice->listB[1], &currSlice->listXsizeB[1]);
1331         }
1332     }
1333     // set the unused list entries to NULL
1334     for (i = currSlice->listXsizeB[0]; i < (MAX_LIST_SIZE); i++) {
1335         currSlice->listB[0][i] = p_Vid->no_ref_pic;
1336     }
1337     for (i = currSlice->listXsizeB[1]; i < (MAX_LIST_SIZE); i++) {
1338         currSlice->listB[1][i] = p_Vid->no_ref_pic;
1339     }
1340     MPP_FREE(currSlice->fs_listinterview0);
1341     MPP_FREE(currSlice->fs_listinterview1);
1342 
1343     return ret = MPP_OK;
1344 __FAILED:
1345     MPP_FREE(fs_list0);
1346     MPP_FREE(fs_list1);
1347     MPP_FREE(fs_listlt);
1348     MPP_FREE(currSlice->fs_listinterview0);
1349     MPP_FREE(currSlice->fs_listinterview1);
1350 
1351     return ret;
1352 }
1353 
get_short_term_pic(H264_SLICE_t * currSlice,RK_S32 picNum,H264_StorePic_t ** find_pic)1354 static RK_U32 get_short_term_pic(H264_SLICE_t *currSlice, RK_S32 picNum, H264_StorePic_t **find_pic)
1355 {
1356     RK_S32 i = 0;
1357     H264_StorePic_t *ret_pic = NULL;
1358     H264_StorePic_t *near_pic = NULL;
1359     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
1360 
1361     for (i = p_Dpb->ref_frames_in_buffer - 1; i >= 0; i--) {
1362         if (currSlice->structure == FRAME) {
1363             if ((p_Dpb->fs_ref[i]->is_reference == 3)
1364                 && (!p_Dpb->fs_ref[i]->frame->is_long_term)) {
1365                 if (p_Dpb->fs_ref[i]->frame->pic_num == picNum) {
1366                     ret_pic = p_Dpb->fs_ref[i]->frame;
1367                     break;
1368                 } else {
1369                     near_pic = p_Dpb->fs_ref[i]->frame;
1370                 }
1371             }
1372         } else {
1373             if ((p_Dpb->fs_ref[i]->is_reference & 1)
1374                 && (!p_Dpb->fs_ref[i]->top_field->is_long_term)) {
1375                 if (p_Dpb->fs_ref[i]->top_field->pic_num == picNum) {
1376                     ret_pic = p_Dpb->fs_ref[i]->top_field;
1377                     break;
1378                 } else {
1379                     near_pic = p_Dpb->fs_ref[i]->top_field;
1380                 }
1381             }
1382             if ((p_Dpb->fs_ref[i]->is_reference & 2)
1383                 && (!p_Dpb->fs_ref[i]->bottom_field->is_long_term)) {
1384                 if (p_Dpb->fs_ref[i]->bottom_field->pic_num == picNum) {
1385                     ret_pic = p_Dpb->fs_ref[i]->bottom_field;
1386                     break;
1387                 } else {
1388                     near_pic = p_Dpb->fs_ref[i]->bottom_field;
1389                 }
1390             }
1391         }
1392     }
1393     *find_pic = ret_pic ? ret_pic : near_pic;
1394     return (ret_pic ? 1 : 0);
1395 }
1396 
1397 
get_long_term_pic(H264_SLICE_t * currSlice,RK_S32 LongtermPicNum)1398 static H264_StorePic_t *get_long_term_pic(H264_SLICE_t *currSlice, RK_S32 LongtermPicNum)
1399 {
1400     RK_U32 i = 0;
1401     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
1402 
1403     for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) {
1404         if (currSlice->structure == FRAME) {
1405             if (p_Dpb->fs_ltref[i]->is_reference == 3)
1406                 if ((p_Dpb->fs_ltref[i]->frame->is_long_term)
1407                     && (p_Dpb->fs_ltref[i]->frame->long_term_pic_num == LongtermPicNum))
1408                     return p_Dpb->fs_ltref[i]->frame;
1409         } else {
1410             if (p_Dpb->fs_ltref[i]->is_reference & 1)
1411                 if ((p_Dpb->fs_ltref[i]->top_field->is_long_term)
1412                     && (p_Dpb->fs_ltref[i]->top_field->long_term_pic_num == LongtermPicNum))
1413                     return p_Dpb->fs_ltref[i]->top_field;
1414             if (p_Dpb->fs_ltref[i]->is_reference & 2)
1415                 if ((p_Dpb->fs_ltref[i]->bottom_field->is_long_term)
1416                     && (p_Dpb->fs_ltref[i]->bottom_field->long_term_pic_num == LongtermPicNum))
1417                     return p_Dpb->fs_ltref[i]->bottom_field;
1418         }
1419     }
1420     return NULL;
1421 }
check_ref_pic_list(H264_SLICE_t * currSlice,RK_S32 cur_list)1422 static RK_U32 check_ref_pic_list(H264_SLICE_t *currSlice, RK_S32 cur_list)
1423 {
1424     RK_S32 i = 0;
1425     RK_U32 dpb_error_flag = 0;
1426     RK_S32 maxPicNum = 0, currPicNum = 0;
1427     RK_S32 picNumLXNoWrap = 0, picNumLXPred = 0, picNumLX = 0;
1428 
1429     RK_S32 *modification_of_pic_nums_idc = currSlice->modification_of_pic_nums_idc[cur_list];
1430     RK_S32 *abs_diff_pic_num_minus1 = currSlice->abs_diff_pic_num_minus1[cur_list];
1431     RK_S32 *long_term_pic_idx = currSlice->long_term_pic_idx[cur_list];
1432     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
1433 
1434     if (currSlice->structure == FRAME) {
1435         maxPicNum  = p_Vid->max_frame_num;
1436         currPicNum = currSlice->frame_num;
1437     } else {
1438         maxPicNum  = 2 * p_Vid->max_frame_num;
1439         currPicNum = 2 * currSlice->frame_num + 1;
1440     }
1441     picNumLXPred = currPicNum;
1442     for (i = 0; modification_of_pic_nums_idc[i] != 3 && i < MAX_REORDER_TIMES; i++) {
1443         H264_StorePic_t *tmp = NULL;
1444         RK_U32 error_flag = 0;
1445         if (modification_of_pic_nums_idc[i] > 3)
1446             continue;
1447         if (modification_of_pic_nums_idc[i] < 2) {
1448             if (modification_of_pic_nums_idc[i] == 0) {
1449                 if ( (picNumLXPred - (abs_diff_pic_num_minus1[i] + 1)) < 0)
1450                     picNumLXNoWrap = picNumLXPred - (abs_diff_pic_num_minus1[i] + 1) + maxPicNum;
1451                 else
1452                     picNumLXNoWrap = picNumLXPred - (abs_diff_pic_num_minus1[i] + 1);
1453             } else { //!< (modification_of_pic_nums_idc[i] == 1)
1454                 if (picNumLXPred + (abs_diff_pic_num_minus1[i] + 1) >= maxPicNum)
1455                     picNumLXNoWrap = picNumLXPred + (abs_diff_pic_num_minus1[i] + 1) - maxPicNum;
1456                 else
1457                     picNumLXNoWrap = picNumLXPred + (abs_diff_pic_num_minus1[i] + 1);
1458             }
1459             picNumLXPred = picNumLXNoWrap;
1460             picNumLX = (picNumLXNoWrap > currPicNum) ? (picNumLXNoWrap - maxPicNum) : picNumLXNoWrap;
1461             error_flag = 1;
1462             if (get_short_term_pic(currSlice, picNumLX, &tmp)) { //!< find short reference
1463                 MppFrame mframe = NULL;
1464                 H264D_DBG(H264D_DBG_DPB_REF_ERR, "find short reference, slot_idx=%d.\n", tmp->mem_mark->slot_idx);
1465                 if (tmp && tmp->mem_mark) {
1466                     mpp_buf_slot_get_prop(p_Vid->p_Dec->frame_slots, tmp->mem_mark->slot_idx, SLOT_FRAME_PTR, &mframe);
1467                     if (mframe && !mpp_frame_get_errinfo(mframe)) {
1468                         error_flag = 0;
1469                     }
1470                 }
1471             }
1472 
1473         } else { //!< (modification_of_pic_nums_idc[i] == 2)
1474             tmp = get_long_term_pic(currSlice, long_term_pic_idx[i]);
1475         }
1476         dpb_error_flag |= error_flag;
1477     }
1478 
1479     return dpb_error_flag;
1480 }
1481 
check_ref_dbp_err(H264_DecCtx_t * p_Dec,H264_RefPicInfo_t * pref,RK_U32 active_refs)1482 static RK_U32 check_ref_dbp_err(H264_DecCtx_t *p_Dec, H264_RefPicInfo_t *pref, RK_U32 active_refs)
1483 {
1484     RK_U32 i = 0;
1485     RK_U32 dpb_error_flag = 0;
1486 
1487     for (i = 0; i < MAX_REF_SIZE; i++) {
1488         if (pref[i].valid) {
1489             MppFrame mframe = NULL;
1490             RK_S32 slot_idx = p_Dec->dpb_info[pref[i].dpb_idx].slot_index;
1491             if (slot_idx < 0) {
1492                 dpb_error_flag |= 1;
1493                 break;
1494             }
1495             mpp_buf_slot_get_prop(p_Dec->frame_slots, slot_idx, SLOT_FRAME_PTR, &mframe);
1496             if (mframe) {
1497                 if (i < active_refs) {
1498                     dpb_error_flag |= mpp_frame_get_errinfo(mframe);
1499                 }
1500                 H264D_DBG(H264D_DBG_DPB_REF_ERR, "[DPB_REF_ERR] slot_idx=%d, dpb_err[%d]=%d", slot_idx, i, mpp_frame_get_errinfo(mframe));
1501             }
1502         }
1503     }
1504     return dpb_error_flag;
1505 }
1506 
check_refer_picture_lists(H264_SLICE_t * currSlice)1507 static void check_refer_picture_lists(H264_SLICE_t *currSlice)
1508 {
1509     H264_DecCtx_t *p_Dec = currSlice->p_Dec;
1510     H264dErrCtx_t *p_err = &p_Dec->errctx;
1511 
1512     if (H264_I_SLICE == currSlice->slice_type) {
1513         p_err->dpb_err_flag = 0;
1514         return;
1515     }
1516 #if 1
1517 
1518     if ((currSlice->slice_type % 5) != H264_I_SLICE
1519         && (currSlice->slice_type % 5) != H264_SI_SLICE) {
1520         if (currSlice->ref_pic_list_reordering_flag[LIST_0]) {
1521             p_err->cur_err_flag |= check_ref_pic_list(currSlice, 0) ? 1 : 0;
1522         } else {
1523             RK_S32 pps_refs = currSlice->active_pps->num_ref_idx_l0_default_active_minus1 + 1;
1524             RK_S32 over_flag = currSlice->num_ref_idx_override_flag;
1525             RK_S32 active_l0 = over_flag ? currSlice->num_ref_idx_active[LIST_0] : pps_refs;
1526             if (currSlice->slice_type % 5 == H264_B_SLICE)
1527                 p_err->cur_err_flag |= check_ref_dbp_err(p_Dec, p_Dec->refpic_info_b[0], active_l0) ? 1 : 0;
1528             else
1529                 p_err->cur_err_flag |= check_ref_dbp_err(p_Dec, p_Dec->refpic_info_p, active_l0) ? 1 : 0;
1530             H264D_DBG(H264D_DBG_DPB_REF_ERR, "list0 dpb: cur_err_flag=%d, pps_refs=%d, over_flag=%d, num_ref_l0=%d\n",
1531                       p_err->cur_err_flag, pps_refs, over_flag, active_l0);
1532         }
1533     }
1534     if (currSlice->slice_type % 5 == H264_B_SLICE) {
1535         if (currSlice->ref_pic_list_reordering_flag[LIST_1]) {
1536             p_err->cur_err_flag |= check_ref_pic_list(currSlice, 1) ? 1 : 0;
1537         } else {
1538             RK_S32 pps_refs = currSlice->active_pps->num_ref_idx_l1_default_active_minus1 + 1;
1539             RK_S32 over_flag = currSlice->num_ref_idx_override_flag;
1540             RK_S32 active_l1 = over_flag ? currSlice->num_ref_idx_active[LIST_1] : pps_refs;
1541             p_err->cur_err_flag |= check_ref_dbp_err(p_Dec, p_Dec->refpic_info_b[1], active_l1) ? 1 : 0;
1542             H264D_DBG(H264D_DBG_DPB_REF_ERR, "list1 dpb: cur_err_flag=%d, pps_refs=%d, over_flag=%d, num_ref_l1=%d\n",
1543                       p_err->cur_err_flag, pps_refs, over_flag, active_l1);
1544         }
1545         //!< B_SLICE only has one refer
1546         if ((currSlice->active_sps->vui_seq_parameters.num_reorder_frames > 1)
1547             && (currSlice->p_Dpb->ref_frames_in_buffer < 2)) {
1548             p_err->cur_err_flag |= 1;
1549             H264D_DBG(H264D_DBG_DPB_REF_ERR, "[DPB_REF_ERR] error, B frame only has one refer");
1550         }
1551     }
1552 
1553 #endif
1554 
1555 }
1556 
reset_dpb_info(H264_DpbInfo_t * p)1557 static void reset_dpb_info(H264_DpbInfo_t *p)
1558 {
1559     p->refpic = NULL;
1560     p->TOP_POC = 0;
1561     p->BOT_POC = 0;
1562     p->field_flag = 0;
1563     p->slot_index = -1;
1564     p->colmv_is_used = 0;
1565     p->frame_num = 0;
1566     p->is_long_term = 0;
1567     p->long_term_pic_num = 0;
1568     p->voidx = 0;
1569     p->view_id = 0;
1570     p->is_used = 0;
1571 }
1572 
prepare_init_dpb_info(H264_SLICE_t * currSlice)1573 static MPP_RET prepare_init_dpb_info(H264_SLICE_t *currSlice)
1574 {
1575     RK_U32 i = 0, j = 0;
1576     H264_DpbBuf_t *p_Dpb = currSlice->p_Dpb;
1577     H264_DecCtx_t *p_Dec = currSlice->p_Dec;
1578 
1579     //!< reset parameters
1580     for (i = 0; i < MAX_DPB_SIZE; i++) {
1581         reset_dpb_info(&p_Dec->dpb_info[i]);
1582     }
1583     //!< reference
1584 #if 1
1585     for (i = 0, j = 0; j < p_Dpb->ref_frames_in_buffer; i++, j++) {
1586         if (p_Dpb->fs_ref[j]->is_used == 3) {
1587             p_Dec->dpb_info[i].refpic = p_Dpb->fs_ref[j]->frame;
1588             if (p_Dpb->fs_ref[j]->frame->iCodingType == FIELD_CODING) {
1589                 p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ref[j]->top_field->poc;
1590                 p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ref[j]->bottom_field->poc;
1591             } else {
1592                 if (p_Dpb->fs_ref[j]->frame->frame_poc != p_Dpb->fs_ref[j]->frame->poc) {
1593                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ref[j]->frame->top_poc - p_Dpb->fs_ref[j]->frame->frame_poc;
1594                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ref[j]->frame->bottom_poc - p_Dpb->fs_ref[j]->frame->frame_poc;
1595                 } else {
1596                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ref[j]->frame->top_poc;
1597                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ref[j]->frame->bottom_poc;
1598                 }
1599             }
1600             p_Dec->dpb_info[i].field_flag = p_Dpb->fs_ref[j]->frame->iCodingType == FIELD_CODING;
1601             p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ref[j]->frame->mem_mark->slot_idx;
1602             p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ref[j]->frame->colmv_no_used_flag ? 0 : 1);
1603         } else if (p_Dpb->fs_ref[j]->is_used) {
1604             if (p_Dpb->fs_ref[j]->is_used & 0x1) { // top
1605                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ref[j]->top_field;
1606 
1607                 p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ref[j]->top_field->poc;
1608                 p_Dec->dpb_info[i].BOT_POC = 0;
1609                 p_Dec->dpb_info[i].field_flag = 1;
1610                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ref[j]->top_field->mem_mark->slot_idx;
1611                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ref[j]->top_field->colmv_no_used_flag ? 0 : 1);
1612             } else { // if(p_Dpb->fs_ref[j]->is_used & 0x2) // bottom
1613                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ref[j]->bottom_field;
1614                 p_Dec->dpb_info[i].TOP_POC = 0;
1615                 p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ref[j]->bottom_field->poc;
1616                 p_Dec->dpb_info[i].field_flag = 1;
1617                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ref[j]->bottom_field->mem_mark->slot_idx;
1618                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ref[j]->bottom_field->colmv_no_used_flag ? 0 : 1);
1619             }
1620         }
1621         p_Dec->dpb_info[i].frame_num = p_Dpb->fs_ref[j]->frame_num;
1622         p_Dec->dpb_info[i].is_long_term = 0;
1623         p_Dec->dpb_info[i].long_term_pic_num = 0;
1624         p_Dec->dpb_info[i].long_term_frame_idx = 0;
1625         p_Dec->dpb_info[i].voidx = p_Dpb->fs_ref[j]->layer_id;
1626         p_Dec->dpb_info[i].view_id = p_Dpb->fs_ref[j]->view_id;
1627         p_Dec->dpb_info[i].is_used = p_Dpb->fs_ref[j]->is_used;
1628     }
1629 #endif
1630     //!<---- long term reference
1631 #if 1
1632     for (j = 0; j < p_Dpb->ltref_frames_in_buffer; i++, j++) {
1633         if (p_Dpb->fs_ltref[j]->is_used == 3) {
1634             p_Dec->dpb_info[i].refpic = p_Dpb->fs_ltref[j]->frame;
1635 
1636             if (p_Dpb->fs_ltref[j]->frame->iCodingType == FIELD_CODING) {
1637                 p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ltref[j]->top_field->poc;
1638                 p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ltref[j]->bottom_field->poc;
1639             } else {
1640                 if (p_Dpb->fs_ltref[j]->frame->frame_poc != p_Dpb->fs_ltref[j]->frame->poc) {
1641                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ltref[j]->frame->top_poc - p_Dpb->fs_ltref[j]->frame->frame_poc;
1642                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ltref[j]->frame->bottom_poc - p_Dpb->fs_ltref[j]->frame->frame_poc;
1643                 } else {
1644                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ltref[j]->frame->top_poc;
1645                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ltref[j]->frame->bottom_poc;
1646                 }
1647 
1648             }
1649             p_Dec->dpb_info[i].field_flag = p_Dpb->fs_ltref[j]->frame->iCodingType == FIELD_CODING;
1650             p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ltref[j]->frame->mem_mark->slot_idx;
1651             p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ltref[j]->frame->colmv_no_used_flag ? 0 : 1);
1652             p_Dec->dpb_info[i].long_term_pic_num = p_Dpb->fs_ltref[j]->frame->long_term_pic_num;
1653         } else if (p_Dpb->fs_ltref[j]->is_used) {
1654             if (p_Dpb->fs_ltref[j]->is_used & 0x1) {
1655                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ltref[j]->top_field;
1656                 p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ltref[j]->top_field->poc;
1657                 p_Dec->dpb_info[i].BOT_POC = 0;
1658                 p_Dec->dpb_info[i].field_flag = 1;
1659                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ltref[j]->top_field->mem_mark->slot_idx;
1660                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ltref[j]->top_field->colmv_no_used_flag ? 0 : 1);
1661                 p_Dec->dpb_info[i].long_term_pic_num = p_Dpb->fs_ltref[j]->top_field->long_term_pic_num;
1662             } else { // if(p_Dpb->fs_ref[j]->is_used & 0x2)
1663                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ltref[j]->bottom_field;
1664                 p_Dec->dpb_info[i].TOP_POC = 0;
1665                 p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ltref[j]->bottom_field->poc;
1666                 p_Dec->dpb_info[i].field_flag = 1;
1667                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ltref[j]->bottom_field->mem_mark->slot_idx;
1668                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ltref[j]->bottom_field->colmv_no_used_flag ? 0 : 1);
1669                 p_Dec->dpb_info[i].long_term_pic_num = p_Dpb->fs_ltref[j]->bottom_field->long_term_pic_num;
1670             }
1671         }
1672         p_Dec->dpb_info[i].frame_num = p_Dpb->fs_ltref[j]->long_term_frame_idx; //long term use long_term_frame_idx
1673         p_Dec->dpb_info[i].is_long_term = 1;
1674         p_Dec->dpb_info[i].long_term_frame_idx = p_Dpb->fs_ltref[j]->long_term_frame_idx;
1675         p_Dec->dpb_info[i].voidx = p_Dpb->fs_ltref[j]->layer_id;
1676         p_Dec->dpb_info[i].view_id = p_Dpb->fs_ltref[j]->view_id;
1677         p_Dec->dpb_info[i].is_used = p_Dpb->fs_ltref[j]->is_used;
1678     }
1679 #endif
1680     //!< inter-layer reference (for multi-layered codecs)
1681 #if 1
1682     for (j = 0; j < p_Dpb->used_size_il; i++, j++) {
1683         if (currSlice->structure == FRAME && p_Dpb->fs_ilref[j]->is_used == 3) {
1684             if (p_Dpb->fs_ilref[j]->inter_view_flag[0] == 0 && p_Dpb->fs_ilref[j]->inter_view_flag[1] == 0)
1685                 break;
1686             p_Dec->dpb_info[i].refpic = p_Dpb->fs_ilref[j]->frame;
1687 
1688             if (p_Dpb->fs_ilref[j]->frame->is_mmco_5) {
1689                 p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ilref[j]->frame->top_poc_mmco5;
1690                 p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ilref[j]->frame->bot_poc_mmco5;
1691             } else {
1692                 p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ilref[j]->frame->top_poc;
1693                 p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ilref[j]->frame->bottom_poc;
1694             }
1695             p_Dec->dpb_info[i].field_flag = p_Dpb->fs_ilref[j]->frame->iCodingType == FIELD_CODING;
1696             p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ilref[j]->frame->mem_mark->slot_idx;
1697             p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ilref[j]->frame->colmv_no_used_flag ? 0 : 1);
1698         } else if (currSlice->structure != FRAME && p_Dpb->fs_ilref[j]->is_used) {
1699             if (p_Dpb->fs_ilref[j]->is_used == 0x3) {
1700                 if (p_Dpb->fs_ilref[j]->inter_view_flag[currSlice->structure == BOTTOM_FIELD] == 0)
1701                     break;
1702                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ilref[j]->top_field;
1703 
1704                 if (p_Dpb->fs_ilref[j]->top_field->is_mmco_5) {
1705                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ilref[j]->top_field->top_poc_mmco5;
1706                 } else {
1707                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ilref[j]->top_field->top_poc;
1708                 }
1709                 if (p_Dpb->fs_ilref[j]->bottom_field->is_mmco_5) {
1710                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ilref[j]->bottom_field->bot_poc_mmco5;
1711                 } else {
1712                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ilref[j]->bottom_field->bottom_poc;
1713                 }
1714                 p_Dec->dpb_info[i].field_flag = p_Dpb->fs_ilref[j]->frame->iCodingType == FIELD_CODING;
1715                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ilref[j]->frame->mem_mark->slot_idx;
1716                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ilref[j]->frame->colmv_no_used_flag ? 0 : 1);
1717             }
1718             if (p_Dpb->fs_ilref[j]->is_used & 0x1) {
1719                 if (p_Dpb->fs_ilref[j]->inter_view_flag[0] == 0)
1720                     break;
1721                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ilref[j]->top_field;
1722 
1723                 if (p_Dpb->fs_ilref[j]->top_field->is_mmco_5) {
1724                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ilref[j]->top_field->top_poc_mmco5;
1725                 } else {
1726                     p_Dec->dpb_info[i].TOP_POC = p_Dpb->fs_ilref[j]->top_field->top_poc;
1727                 }
1728                 p_Dec->dpb_info[i].BOT_POC = 0;
1729                 p_Dec->dpb_info[i].field_flag = 1;
1730                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ilref[j]->top_field->mem_mark->slot_idx;
1731                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ilref[j]->top_field->colmv_no_used_flag ? 0 : 1);
1732             } else { // if(p_Dpb->fs_ref[j]->is_used & 0x2)
1733                 if (p_Dpb->fs_ilref[j]->inter_view_flag[1] == 0)
1734                     break;
1735                 p_Dec->dpb_info[i].refpic = p_Dpb->fs_ilref[j]->bottom_field;
1736 
1737                 p_Dec->dpb_info[i].TOP_POC = 0;
1738                 if (p_Dpb->fs_ilref[j]->bottom_field->is_mmco_5) {
1739                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ilref[j]->bottom_field->bot_poc_mmco5;
1740                 } else {
1741                     p_Dec->dpb_info[i].BOT_POC = p_Dpb->fs_ilref[j]->bottom_field->bottom_poc;
1742                 }
1743                 p_Dec->dpb_info[i].field_flag = 1;
1744                 p_Dec->dpb_info[i].slot_index = p_Dpb->fs_ilref[j]->bottom_field->mem_mark->slot_idx;
1745                 p_Dec->dpb_info[i].colmv_is_used = (p_Dpb->fs_ilref[j]->bottom_field->colmv_no_used_flag ? 0 : 1);
1746             }
1747         }
1748         p_Dec->dpb_info[i].frame_num = p_Dpb->fs_ilref[j]->frame_num;
1749         p_Dec->dpb_info[i].is_long_term = 0;//p_Dpb->fs_ilref[j]->is_long_term;
1750         p_Dec->dpb_info[i].is_ilt_flag = 1;
1751         p_Dec->dpb_info[i].long_term_pic_num = 0;
1752         p_Dec->dpb_info[i].long_term_frame_idx = 0;
1753         p_Dec->dpb_info[i].voidx = p_Dpb->fs_ilref[j]->layer_id;
1754         p_Dec->dpb_info[i].view_id = p_Dpb->fs_ilref[j]->view_id;
1755         p_Dec->dpb_info[i].is_used = p_Dpb->fs_ilref[j]->is_used;
1756     }
1757 #endif
1758 
1759     return MPP_OK;
1760 }
1761 
prepare_init_ref_info(H264_SLICE_t * currSlice)1762 static MPP_RET prepare_init_ref_info(H264_SLICE_t *currSlice)
1763 {
1764     void *refpic = NULL;
1765     RK_U32 i = 0, j = 0, k = 0;
1766     RK_S32 poc = 0, TOP_POC = 0, BOT_POC = 0;
1767     RK_S32 min_poc = 0, max_poc = 0;
1768     RK_S32 layer_id = 0, voidx = 0, is_used = 0, near_dpb_idx = 0;
1769     RK_U32 mmco5_flag = 0;
1770     H264_DecCtx_t *p_Dec = currSlice->p_Dec;
1771 
1772     memset(p_Dec->refpic_info_p,    0, MAX_REF_SIZE * sizeof(H264_RefPicInfo_t));
1773     memset(p_Dec->refpic_info_b[0], 0, MAX_REF_SIZE * sizeof(H264_RefPicInfo_t));
1774     memset(p_Dec->refpic_info_b[1], 0, MAX_REF_SIZE * sizeof(H264_RefPicInfo_t));
1775 
1776     if (currSlice->idr_flag && (currSlice->layer_id == 0)) { // idr_flag==1 && layer_id==0
1777         goto __RETURN;
1778     }
1779     //!<------ set listP -------
1780     near_dpb_idx = 0;
1781     for (j = 0; j < 32; j++) {
1782         if (j >= currSlice->listXsizeP[0])
1783             break;
1784         poc = 0;
1785         layer_id = currSlice->listP[0][j]->layer_id;
1786         mmco5_flag = (currSlice->listP[0][j]->is_mmco_5 && currSlice->layer_id && !currSlice->listP[0][j]->layer_id) ? 1 : 0;
1787         if (currSlice->listP[0][j]->structure == FRAME) {
1788             poc = mmco5_flag ? currSlice->listP[0][j]->poc_mmco5 : currSlice->listP[0][j]->poc;
1789         } else if (currSlice->listP[0][j]->structure == TOP_FIELD) {
1790             poc = mmco5_flag ? currSlice->listP[0][j]->top_poc_mmco5 : currSlice->listP[0][j]->top_poc;
1791         } else {
1792             poc = mmco5_flag ? currSlice->listP[0][j]->bot_poc_mmco5 : currSlice->listP[0][j]->bottom_poc;
1793         }
1794         for (i = 0; i < 16; i++) {
1795             refpic = p_Dec->dpb_info[i].refpic;
1796             TOP_POC = p_Dec->dpb_info[i].TOP_POC;
1797             BOT_POC = p_Dec->dpb_info[i].BOT_POC;
1798             voidx = p_Dec->dpb_info[i].voidx;
1799             is_used = p_Dec->dpb_info[i].is_used;
1800             if (currSlice->structure == FRAME && refpic) {
1801                 if (poc == MPP_MIN(TOP_POC, BOT_POC) && (layer_id == voidx))
1802                     break;
1803             } else {
1804                 if (is_used == 3) {
1805                     if ((poc == BOT_POC || poc == TOP_POC) && layer_id == voidx)
1806                         break;
1807                 } else if (is_used & 1) {
1808                     if (poc == TOP_POC && layer_id == voidx)
1809                         break;
1810                 } else if (is_used & 2) {
1811                     if (poc == BOT_POC && layer_id == voidx)
1812                         break;
1813                 }
1814             }
1815         }
1816         if (i < 16) {
1817             near_dpb_idx = i;
1818             p_Dec->refpic_info_p[j].dpb_idx = i;
1819         } else {
1820             ASSERT(near_dpb_idx >= 0);
1821             p_Dec->refpic_info_p[j].dpb_idx = near_dpb_idx;
1822             p_Dec->errctx.cur_err_flag |= 1;
1823         }
1824         if (currSlice->listP[0][j]->structure == BOTTOM_FIELD) {
1825             p_Dec->refpic_info_p[j].bottom_flag = 1;
1826         } else {
1827             p_Dec->refpic_info_p[j].bottom_flag = 0;
1828         }
1829         p_Dec->refpic_info_p[j].valid = 1;
1830     }
1831     //!<------  set listB -------
1832     for (k = 0; k < 2; k++) {
1833         RK_U32 tmp[16] = {0};
1834 
1835         min_poc =  0xFFFF;
1836         max_poc = -0xFFFF;
1837         near_dpb_idx = 0;
1838         for (j = 0; j < 32; j++) {
1839             if (j >= currSlice->listXsizeB[k])
1840                 break;
1841 
1842             poc = 0;
1843             layer_id = currSlice->listB[k][j]->layer_id;
1844             mmco5_flag = (currSlice->listB[k][j]->is_mmco_5 && currSlice->layer_id && !currSlice->listB[k][j]->layer_id) ? 1 : 0;
1845             if (currSlice->listB[k][j]->structure == FRAME) {
1846                 poc = mmco5_flag ? currSlice->listB[k][j]->poc_mmco5 : currSlice->listB[k][j]->poc;
1847             } else if (currSlice->listB[k][j]->structure == TOP_FIELD) {
1848                 poc = mmco5_flag ? currSlice->listB[k][j]->top_poc_mmco5 : currSlice->listB[k][j]->top_poc;
1849             } else {
1850                 poc = mmco5_flag ? currSlice->listB[k][j]->bot_poc_mmco5 : currSlice->listB[k][j]->bottom_poc;
1851             }
1852 
1853             min_poc = MPP_MIN(min_poc, poc);
1854             max_poc = MPP_MAX(max_poc, poc);
1855             for (i = 0; i < 16; i++) {
1856                 refpic = p_Dec->dpb_info[i].refpic;
1857                 TOP_POC = p_Dec->dpb_info[i].TOP_POC;
1858                 BOT_POC = p_Dec->dpb_info[i].BOT_POC;
1859                 voidx = p_Dec->dpb_info[i].voidx;
1860                 is_used = p_Dec->dpb_info[i].is_used;
1861                 if (currSlice->structure == FRAME && refpic) {
1862                     if (poc == MPP_MIN(TOP_POC, BOT_POC) && (layer_id == voidx) && !tmp[i]) {
1863                         tmp[i] = 1;
1864                         break;
1865                     }
1866                 } else {
1867                     if (is_used == 3) {
1868                         if ((poc == BOT_POC || poc == TOP_POC) && layer_id == voidx)
1869                             break;
1870                     } else if (is_used & 1) {
1871                         if (poc == TOP_POC && (layer_id == voidx || (layer_id != voidx && !currSlice->bottom_field_flag)))
1872                             break;
1873                     } else if (is_used & 2) {
1874                         if (poc == BOT_POC && (layer_id == voidx || (layer_id != voidx && currSlice->bottom_field_flag)))
1875                             break;
1876                     }
1877                 }
1878             }
1879             if (i < 16) {
1880                 near_dpb_idx = i;
1881                 p_Dec->refpic_info_b[k][j].dpb_idx = i;
1882             } else {
1883                 ASSERT(near_dpb_idx >= 0);
1884                 p_Dec->refpic_info_b[k][j].dpb_idx = near_dpb_idx;
1885                 p_Dec->errctx.cur_err_flag |= 1;
1886             }
1887             if (currSlice->listB[k][j]->structure == BOTTOM_FIELD) {
1888                 p_Dec->refpic_info_b[k][j].bottom_flag = 1;
1889             } else {
1890                 p_Dec->refpic_info_b[k][j].bottom_flag = 0;
1891             }
1892             p_Dec->refpic_info_b[k][j].valid = 1;
1893         }
1894 
1895     }
1896     //!< check dpb list poc
1897 #if 0
1898     {
1899         RK_S32 cur_poc = p_Dec->p_Vid->dec_pic->poc;
1900         if ((currSlice->slice_type % 5) != H264_I_SLICE
1901             && (currSlice->slice_type % 5) != H264_SI_SLICE) {
1902             if (cur_poc < min_poc) {
1903                 p_Dec->errctx.cur_err_flag |= 1;
1904                 H264D_DBG(H264D_DBG_DPB_REF_ERR, "[DPB_REF_ERR] min_poc=%d, dec_poc=%d", min_poc, cur_poc);
1905             }
1906         }
1907         if (currSlice->slice_type % 5 == H264_B_SLICE) {
1908             if (cur_poc > max_poc) {
1909                 p_Dec->errctx.cur_err_flag |= 1;
1910                 H264D_DBG(H264D_DBG_DPB_REF_ERR, "[DPB_REF_ERR] max_poc=%d, dec_poc=%d", max_poc, cur_poc);
1911             }
1912         }
1913     }
1914 #endif
1915 __RETURN:
1916     return MPP_OK;
1917 }
1918 
check_refer_dpb_buf_slots(H264_SLICE_t * currSlice)1919 static MPP_RET check_refer_dpb_buf_slots(H264_SLICE_t *currSlice)
1920 {
1921     RK_U32 i = 0;
1922     RK_S32 slot_idx = 0;
1923     RK_U32 dpb_used = 0;
1924     H264_DecCtx_t *p_Dec = NULL;
1925     H264_DpbMark_t *p_mark = NULL;
1926 
1927     p_Dec = currSlice->p_Dec;
1928     //!< set buf slot flag
1929     for (i = 0; i < MAX_DPB_SIZE; i++) {
1930         if ((NULL != p_Dec->dpb_info[i].refpic) && (p_Dec->dpb_info[i].slot_index >= 0)) {
1931             p_Dec->in_task->refer[i] = p_Dec->dpb_info[i].slot_index;
1932             mpp_buf_slot_set_flag(p_Dec->frame_slots, p_Dec->dpb_info[i].slot_index, SLOT_HAL_INPUT);
1933             mpp_buf_slot_set_flag(p_Dec->frame_slots, p_Dec->dpb_info[i].slot_index, SLOT_CODEC_USE);
1934         } else {
1935             p_Dec->in_task->refer[i] = -1;
1936         }
1937     }
1938     //!< dpb info
1939     if (h264d_debug & H264D_DBG_DPB_INFO) {
1940         H264D_DBG(H264D_DBG_DPB_INFO, "[DPB_INFO] cur_slot_idx=%d", p_Dec->in_task->output);
1941         for (i = 0; i < MAX_DPB_SIZE; i++) {
1942             slot_idx = p_Dec->in_task->refer[i];
1943             if (slot_idx >= 0) {
1944                 H264D_DBG(H264D_DBG_DPB_INFO, "[DPB_INFO] ref_slot_idx[%d]=%d", i, slot_idx);
1945             }
1946         }
1947     }
1948     //!< mark info
1949     for (i = 0; i < MAX_MARK_SIZE; i++) {
1950         p_mark = &p_Dec->dpb_mark[i];
1951         if (p_mark->out_flag && (p_mark->slot_idx >= 0)) {
1952             dpb_used++;
1953             if (h264d_debug & H264D_DBG_DPB_INFO) {
1954                 RK_S32 fd = 0;
1955                 MppFrame mframe = NULL;
1956                 MppBuffer mbuffer = NULL;
1957                 mpp_buf_slot_get_prop(p_Dec->frame_slots, p_mark->slot_idx, SLOT_FRAME_PTR, &mframe);
1958                 mbuffer = mframe ? mpp_frame_get_buffer(mframe) : NULL;
1959                 fd = mbuffer ? mpp_buffer_get_fd(mbuffer) : 0xFF;
1960                 H264D_DBG(H264D_DBG_DPB_INFO, "[DPB_MARK_INFO] slot_idx=%d, top_used=%d, bot_used=%d, out_flag=%d, fd=0x%02x, poc=%d, view_id=%d",
1961                           p_mark->slot_idx, p_mark->top_used, p_mark->bot_used,
1962                           p_mark->out_flag, fd, p_mark->pic->poc, p_mark->pic->layer_id);
1963             }
1964         }
1965     }
1966     H264D_DBG(H264D_DBG_DPB_INFO, "[DPB_MARK_INFO] ---------- cur_slot=%d --------------------", p_Dec->in_task->output);
1967 
1968     if (dpb_used > MPP_MIN(p_Dec->p_Vid->dpb_size[0] + p_Dec->p_Vid->dpb_size[1], 16) + 2)  {
1969         H264D_ERR("[h264d_reset_error]");
1970         h264d_reset((void *)p_Dec);
1971         return MPP_NOK;
1972     }
1973 
1974     return MPP_OK;
1975 }
1976 
1977 
1978 /*!
1979 ***********************************************************************
1980 * \brief
1981 *    flush dpb buffer slot
1982 ***********************************************************************
1983 */
1984 //extern "C"
flush_dpb_buf_slot(H264_DecCtx_t * p_Dec)1985 void flush_dpb_buf_slot(H264_DecCtx_t *p_Dec)
1986 {
1987     RK_U32 i = 0;
1988     H264_DpbMark_t *p_mark = NULL;
1989 
1990     for (i = 0; i < MAX_MARK_SIZE; i++) {
1991         p_mark = &p_Dec->dpb_mark[i];
1992         if (p_mark && p_mark->out_flag && (p_mark->slot_idx >= 0)) {
1993             MppFrame mframe = NULL;
1994             mpp_buf_slot_get_prop(p_Dec->frame_slots, p_mark->slot_idx, SLOT_FRAME_PTR, &mframe);
1995             if (mframe) {
1996                 H264D_DBG(H264D_DBG_SLOT_FLUSH, "[DPB_BUF_FLUSH] slot_idx=%d, top_used=%d, bot_used=%d",
1997                           p_mark->slot_idx, p_mark->top_used, p_mark->bot_used);
1998                 mpp_frame_set_discard(mframe, 1);
1999                 mpp_buf_slot_set_flag(p_Dec->frame_slots, p_mark->slot_idx, SLOT_QUEUE_USE);
2000                 mpp_buf_slot_enqueue(p_Dec->frame_slots, p_mark->slot_idx, QUEUE_DISPLAY);
2001                 mpp_buf_slot_clr_flag(p_Dec->frame_slots, p_mark->slot_idx, SLOT_CODEC_USE);
2002                 p_Dec->last_frame_slot_idx = p_mark->slot_idx;
2003             }
2004         }
2005         reset_dpb_mark(p_mark);
2006     }
2007 }
2008 
2009 /*!
2010 ***********************************************************************
2011 * \brief
2012 *    reset dpb mark
2013 ***********************************************************************
2014 */
2015 //extern "C"
reset_dpb_mark(H264_DpbMark_t * p_mark)2016 MPP_RET reset_dpb_mark(H264_DpbMark_t *p_mark)
2017 {
2018     if (p_mark) {
2019         p_mark->top_used = 0;
2020         p_mark->bot_used = 0;
2021         p_mark->out_flag = 0;
2022         p_mark->slot_idx = -1;
2023         p_mark->pic = NULL;
2024         p_mark->mframe = NULL;
2025     }
2026 
2027     return MPP_OK;
2028 }
2029 /*!
2030 ***********************************************************************
2031 * \brief
2032 *    init picture
2033 ***********************************************************************
2034 */
2035 //extern "C"
init_picture(H264_SLICE_t * currSlice)2036 MPP_RET init_picture(H264_SLICE_t *currSlice)
2037 {
2038     MPP_RET ret = MPP_ERR_UNKNOW;
2039     H264_DecCtx_t *p_Dec   = currSlice->p_Vid->p_Dec;
2040     H264dVideoCtx_t *p_Vid = currSlice->p_Vid;
2041     H264dErrCtx_t *p_err   = &p_Dec->errctx;
2042 
2043     //!< discard stream before I_SLICE
2044     p_err->i_slice_no += ((!currSlice->layer_id) && (H264_I_SLICE == currSlice->slice_type ||
2045                                                      (p_Vid->recovery.valid_flag && p_Vid->recovery.first_frm_valid &&
2046                                                       p_Vid->recovery.first_frm_id == currSlice->frame_num))) ? 1 : 0;
2047 
2048     if (!p_err->i_slice_no) {
2049         H264D_WARNNING("[Discard] Discard slice before I Slice. \n");
2050         ret = MPP_NOK;
2051         goto __FAILED;
2052     }
2053     FUN_CHECK(ret = alloc_decpic(currSlice));
2054     if (((p_err->i_slice_no < 2) && (!currSlice->layer_id) && (H264_I_SLICE == currSlice->slice_type)) ||
2055         currSlice->idr_flag) {
2056         p_err->first_iframe_poc = p_Vid->dec_pic->poc; //!< recoder first i frame poc
2057         p_err->first_iframe_is_output = 0;
2058     }
2059     //!< idr_memory_management MVC_layer, idr_flag==1
2060     if (currSlice->layer_id && !currSlice->svc_extension_flag && !currSlice->mvcExt.non_idr_flag) {
2061         ASSERT(currSlice->layer_id == 1);
2062         FUN_CHECK(ret = idr_memory_management(p_Vid->p_Dpb_layer[currSlice->layer_id], p_Vid->dec_pic));
2063     }
2064 
2065     // if cur pic i frame poc & frame_num <= last pic, flush dpb.
2066     if (p_Vid->last_pic != NULL && p_Vid->dec_pic->poc != 0) {
2067         if (p_Vid->last_pic->frame_num >= p_Vid->dec_pic->frame_num
2068             && p_Vid->last_pic->poc >= p_Vid->dec_pic->poc
2069             && p_Vid->dec_pic->slice_type == H264_I_SLICE
2070             && p_Vid->dec_pic->structure == 3) {
2071             if (currSlice->layer_id == 0) {
2072                 FUN_CHECK(ret = flush_dpb(p_Vid->p_Dpb_layer[0], 1));
2073             } else {
2074                 FUN_CHECK(ret = flush_dpb(p_Vid->p_Dpb_layer[1], 2));
2075             }
2076         }
2077     }
2078     update_ref_list(p_Vid->p_Dpb_layer[currSlice->layer_id]);
2079     update_ltref_list(p_Vid->p_Dpb_layer[currSlice->layer_id]);
2080     update_pic_num(currSlice);
2081     //!< reorder
2082     if (!currSlice->idr_flag || currSlice->layer_id) {
2083         FUN_CHECK(ret = init_lists_p_slice_mvc(currSlice));
2084         FUN_CHECK(ret = init_lists_b_slice_mvc(currSlice));
2085     }
2086     prepare_init_dpb_info(currSlice);
2087     prepare_init_ref_info(currSlice);
2088 
2089     FUN_CHECK(ret = check_refer_dpb_buf_slots(currSlice));
2090     check_refer_picture_lists(currSlice);
2091     {
2092         H264dDxvaCtx_t *dxva_ctx = p_Dec->dxva_ctx;
2093 
2094         fill_picparams(currSlice->p_Vid, &dxva_ctx->pp);
2095         if (dxva_ctx->pp.scaleing_list_enable_flag) {
2096             prepare_init_scanlist(currSlice);
2097             fill_scanlist(currSlice->p_Vid, &dxva_ctx->qm);
2098         }
2099     }
2100 
2101     return ret = MPP_OK;
2102 __FAILED:
2103     return ret;
2104 }
2105 
2106 /*!
2107 ***********************************************************************
2108 * \brief
2109 *    update dpb
2110 ***********************************************************************
2111 */
2112 //extern "C"
update_dpb(H264_DecCtx_t * p_Dec)2113 MPP_RET update_dpb(H264_DecCtx_t *p_Dec)
2114 {
2115     MPP_RET ret = MPP_ERR_UNKNOW;
2116 
2117     p_Dec->p_Vid->exit_picture_flag = 1;
2118     p_Dec->p_Vid->have_outpicture_flag = 1;
2119     ret = exit_picture(p_Dec->p_Vid, &p_Dec->p_Vid->dec_pic);
2120 
2121     p_Dec->p_Vid->iNumOfSlicesDecoded = 0;
2122     p_Dec->p_Vid->exit_picture_flag = 0;
2123 
2124     return ret;
2125 }
2126 
2127