blob: 56ee63731ccdb4d5ac7971ee0d78a00dbe550202
1 | /* |
2 | * Copyright (C) 2017 Amlogic, Inc. All rights reserved. |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
12 | * more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 | * |
18 | * Description: |
19 | */ |
20 | #define DEBUG |
21 | #include <linux/kernel.h> |
22 | #include <linux/types.h> |
23 | #include <linux/errno.h> |
24 | |
25 | #include <linux/amlogic/media/utils/vdec_reg.h> |
26 | #include "../utils/vdec.h" |
27 | #include "../utils/amvdec.h" |
28 | |
29 | #include "h264_dpb.h" |
30 | |
31 | #define FRAME_NUM_MAX_SIZE 0x10000 |
32 | |
33 | #undef pr_info |
34 | #define pr_info printk |
35 | int dpb_print(int index, int debug_flag, const char *fmt, ...) |
36 | { |
37 | if (((h264_debug_flag & debug_flag) && |
38 | ((1 << index) & h264_debug_mask)) |
39 | || (debug_flag == PRINT_FLAG_ERROR)) { |
40 | unsigned char buf[512]; |
41 | int len = 0; |
42 | va_list args; |
43 | |
44 | va_start(args, fmt); |
45 | len = sprintf(buf, "%d: ", index); |
46 | vsnprintf(buf + len, 512-len, fmt, args); |
47 | pr_debug("%s", buf); |
48 | va_end(args); |
49 | } |
50 | return 0; |
51 | } |
52 | |
53 | int dpb_print_cont(int index, int debug_flag, const char *fmt, ...) |
54 | { |
55 | if (((h264_debug_flag & debug_flag) && |
56 | ((1 << index) & h264_debug_mask)) |
57 | || (debug_flag == PRINT_FLAG_ERROR)) { |
58 | unsigned char buf[512]; |
59 | int len = 0; |
60 | va_list args; |
61 | va_start(args, fmt); |
62 | vsnprintf(buf + len, 512-len, fmt, args); |
63 | pr_info("%s", buf); |
64 | va_end(args); |
65 | } |
66 | return 0; |
67 | } |
68 | |
69 | unsigned char dpb_is_debug(int index, int debug_flag) |
70 | { |
71 | if (((h264_debug_flag & debug_flag) && |
72 | ((1 << index) & h264_debug_mask)) |
73 | || (debug_flag == PRINT_FLAG_ERROR)) |
74 | return 1; |
75 | return 0; |
76 | } |
77 | |
78 | #define CHECK_VALID(list_size, mark) {\ |
79 | if (list_size > MAX_LIST_SIZE || list_size < 0) { \ |
80 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_ERROR, \ |
81 | "%s(%d): listXsize[%d] %d is larger than max size\r\n",\ |
82 | __func__, __LINE__, mark, list_size);\ |
83 | list_size = 0; \ |
84 | p_H264_Dpb->dpb_error_flag = __LINE__;\ |
85 | } \ |
86 | } |
87 | |
88 | static struct DecRefPicMarking_s |
89 | dummy_dec_ref_pic_marking_buffer |
90 | [DEC_REF_PIC_MARKING_BUFFER_NUM_MAX]; |
91 | static struct StorablePicture dummy_pic; |
92 | static struct FrameStore dummy_fs; |
93 | static struct StorablePicture *get_new_pic( |
94 | struct h264_dpb_stru *p_H264_Dpb, |
95 | enum PictureStructure structure, unsigned char is_output); |
96 | |
97 | static void update_ref_list(struct DecodedPictureBuffer *p_Dpb); |
98 | |
99 | static void init_dummy_fs(void) |
100 | { |
101 | dummy_fs.frame = &dummy_pic; |
102 | dummy_fs.top_field = &dummy_pic; |
103 | dummy_fs.bottom_field = &dummy_pic; |
104 | |
105 | dummy_pic.top_field = &dummy_pic; |
106 | dummy_pic.bottom_field = &dummy_pic; |
107 | dummy_pic.frame = &dummy_pic; |
108 | |
109 | dummy_pic.dec_ref_pic_marking_buffer = |
110 | &dummy_dec_ref_pic_marking_buffer[0]; |
111 | } |
112 | |
113 | enum { |
114 | LIST_0 = 0, |
115 | LIST_1 = 1, |
116 | BI_PRED = 2, |
117 | BI_PRED_L0 = 3, |
118 | BI_PRED_L1 = 4 |
119 | }; |
120 | |
121 | void ref_pic_list_reordering(struct h264_dpb_stru *p_H264_Dpb, |
122 | struct Slice *currSlice) |
123 | { |
124 | /* struct VideoParameters *p_Vid = currSlice->p_Vid; |
125 | * byte dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER]; |
126 | * DataPartition *partition = &(currSlice->partArr[dP_nr]); |
127 | * Bitstream *currStream = partition->bitstream; |
128 | */ |
129 | int i, j, val; |
130 | unsigned short *reorder_cmd = |
131 | &p_H264_Dpb->dpb_param.mmco.l0_reorder_cmd[0]; |
132 | /* alloc_ref_pic_list_reordering_buffer(currSlice); */ |
133 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
134 | "%s\n", __func__); |
135 | if (currSlice->slice_type != I_SLICE && |
136 | currSlice->slice_type != SI_SLICE) { |
137 | /* val = currSlice->ref_pic_list_reordering_flag[LIST_0] = |
138 | * read_u_1 ("SH: ref_pic_list_reordering_flag_l0", |
139 | * currStream, &p_Dec->UsedBits); |
140 | */ |
141 | if (reorder_cmd[0] != 3) { |
142 | val = currSlice-> |
143 | ref_pic_list_reordering_flag[LIST_0] = 1; |
144 | } else { |
145 | val = currSlice-> |
146 | ref_pic_list_reordering_flag[LIST_0] = 0; |
147 | } |
148 | if (val) { |
149 | dpb_print(p_H264_Dpb->decoder_index, |
150 | PRINT_FLAG_DPB_DETAIL, |
151 | "%s, ref_pic_list_reordering_flag[LIST_0] is 1\n", |
152 | __func__); |
153 | |
154 | j = 0; |
155 | i = 0; |
156 | do { |
157 | val = currSlice-> |
158 | modification_of_pic_nums_idc[LIST_0][i] = |
159 | reorder_cmd[j++]; |
160 | /* read_ue_v( |
161 | * "SH: modification_of_pic_nums_idc_l0", |
162 | * currStream, &p_Dec->UsedBits); |
163 | */ |
164 | dpb_print(p_H264_Dpb->decoder_index, |
165 | PRINT_FLAG_DPB_DETAIL, |
166 | "%d(%d):val %x\n", i, j, val); |
167 | if (j >= 66) { |
168 | currSlice-> |
169 | ref_pic_list_reordering_flag[LIST_0] = |
170 | 0; /* by rain */ |
171 | dpb_print(p_H264_Dpb->decoder_index, |
172 | PRINT_FLAG_ERROR, |
173 | "%s error\n", __func__); |
174 | break; |
175 | } |
176 | if (val == 0 || val == 1) { |
177 | currSlice-> |
178 | abs_diff_pic_num_minus1[LIST_0][i] = |
179 | reorder_cmd[j++]; |
180 | /* read_ue_v("SH: " |
181 | *"abs_diff_pic_num_minus1_l0", |
182 | *currStream, &p_Dec->UsedBits); |
183 | */ |
184 | } else { |
185 | if (val == 2) { |
186 | currSlice-> |
187 | long_term_pic_idx[LIST_0][i] = |
188 | reorder_cmd[j++]; |
189 | /* read_ue_v( |
190 | *"SH: long_term_pic_idx_l0", |
191 | *currStream, |
192 | *&p_Dec->UsedBits); |
193 | */ |
194 | } |
195 | } |
196 | i++; |
197 | /* assert (i>currSlice-> |
198 | * num_ref_idx_active[LIST_0]); |
199 | */ |
200 | if ( |
201 | |
202 | /* |
203 | * i>currSlice->num_ref_idx_active[LIST_0] || |
204 | */ |
205 | i >= REORDERING_COMMAND_MAX_SIZE) { |
206 | dpb_print(p_H264_Dpb->decoder_index, |
207 | PRINT_FLAG_DPB_DETAIL, |
208 | "%s error %d %d\n", |
209 | __func__, i, |
210 | currSlice-> |
211 | num_ref_idx_active[LIST_0]); |
212 | currSlice-> |
213 | ref_pic_list_reordering_flag[LIST_0] = |
214 | 0; /* by rain */ |
215 | break; |
216 | } |
217 | if (j >= 66) { |
218 | dpb_print(p_H264_Dpb->decoder_index, |
219 | PRINT_FLAG_ERROR, "%s error\n", |
220 | __func__); |
221 | currSlice-> |
222 | ref_pic_list_reordering_flag[LIST_0] = |
223 | 0; /* by rain */ |
224 | break; |
225 | } |
226 | |
227 | } while (val != 3); |
228 | } |
229 | } |
230 | |
231 | if (currSlice->slice_type == B_SLICE) { |
232 | reorder_cmd = &p_H264_Dpb->dpb_param.mmco.l1_reorder_cmd[0]; |
233 | /* val = currSlice->ref_pic_list_reordering_flag[LIST_1] |
234 | *= read_u_1 ("SH: ref_pic_list_reordering_flag_l1", |
235 | *currStream, |
236 | *&p_Dec->UsedBits); |
237 | */ |
238 | |
239 | if (reorder_cmd[0] != 3) { |
240 | val = |
241 | currSlice->ref_pic_list_reordering_flag[LIST_1] = 1; |
242 | } else { |
243 | val = |
244 | currSlice->ref_pic_list_reordering_flag[LIST_1] = 0; |
245 | } |
246 | |
247 | if (val) { |
248 | dpb_print(p_H264_Dpb->decoder_index, |
249 | PRINT_FLAG_DPB_DETAIL, |
250 | "%s, ref_pic_list_reordering_flag[LIST_1] is 1\n", |
251 | __func__); |
252 | |
253 | j = 0; |
254 | i = 0; |
255 | do { |
256 | val = currSlice-> |
257 | modification_of_pic_nums_idc[LIST_1][i] = |
258 | reorder_cmd[j++]; |
259 | /* read_ue_v( |
260 | *"SH: modification_of_pic_nums_idc_l1", |
261 | *currStream, |
262 | *&p_Dec->UsedBits); |
263 | */ |
264 | dpb_print(p_H264_Dpb->decoder_index, |
265 | PRINT_FLAG_DPB_DETAIL, |
266 | "%d(%d):val %x\n", |
267 | i, j, val); |
268 | if (j >= 66) { |
269 | dpb_print(p_H264_Dpb->decoder_index, |
270 | PRINT_FLAG_ERROR, "%s error\n", |
271 | __func__); |
272 | currSlice-> |
273 | ref_pic_list_reordering_flag[LIST_1] = |
274 | 0; /* by rain */ |
275 | break; |
276 | } |
277 | if (val == 0 || val == 1) { |
278 | currSlice-> |
279 | abs_diff_pic_num_minus1[LIST_1][i] = |
280 | reorder_cmd[j++]; |
281 | /* read_ue_v( |
282 | *"SH: abs_diff_pic_num_minus1_l1", |
283 | *currStream, &p_Dec->UsedBits); |
284 | */ |
285 | } else { |
286 | if (val == 2) { |
287 | currSlice-> |
288 | long_term_pic_idx[LIST_1][i] = |
289 | reorder_cmd[j++]; |
290 | /* read_ue_v( |
291 | *"SH: long_term_pic_idx_l1", |
292 | *currStream, |
293 | *&p_Dec->UsedBits); |
294 | */ |
295 | } |
296 | } |
297 | i++; |
298 | /* assert(i>currSlice-> |
299 | * num_ref_idx_active[LIST_1]); |
300 | */ |
301 | if ( |
302 | /*i>currSlice->num_ref_idx_active[LIST_1] || */ |
303 | i >= REORDERING_COMMAND_MAX_SIZE) { |
304 | dpb_print(p_H264_Dpb->decoder_index, |
305 | PRINT_FLAG_DPB_DETAIL, |
306 | "%s error %d %d\n", |
307 | __func__, i, |
308 | currSlice-> |
309 | num_ref_idx_active[LIST_0]); |
310 | currSlice-> |
311 | ref_pic_list_reordering_flag[LIST_1] = |
312 | 0; /* by rain */ |
313 | break; |
314 | } |
315 | if (j >= 66) { |
316 | dpb_print(p_H264_Dpb->decoder_index, |
317 | PRINT_FLAG_ERROR, |
318 | "%s error\n", __func__); |
319 | break; |
320 | } |
321 | } while (val != 3); |
322 | } |
323 | } |
324 | |
325 | /* set reference index of redundant slices. */ |
326 | /* |
327 | *if (currSlice->redundant_pic_cnt && |
328 | *(currSlice->slice_type != I_SLICE)) |
329 | *{ |
330 | * currSlice->redundant_slice_ref_idx = |
331 | * currSlice->abs_diff_pic_num_minus1[LIST_0][0] + 1; |
332 | *} |
333 | */ |
334 | } |
335 | |
336 | void slice_prepare(struct h264_dpb_stru *p_H264_Dpb, |
337 | struct DecodedPictureBuffer *p_Dpb, |
338 | struct VideoParameters *p_Vid, |
339 | struct SPSParameters *sps, struct Slice *pSlice) |
340 | { |
341 | int i, j; |
342 | /* p_Vid->active_sps = sps; */ |
343 | unsigned short *mmco_cmd = &p_H264_Dpb->dpb_param.mmco.mmco_cmd[0]; |
344 | /* for decode_poc */ |
345 | sps->pic_order_cnt_type = |
346 | p_H264_Dpb->dpb_param.l.data[PIC_ORDER_CNT_TYPE]; |
347 | sps->log2_max_pic_order_cnt_lsb_minus4 = |
348 | p_H264_Dpb->dpb_param.l.data[LOG2_MAX_PIC_ORDER_CNT_LSB] - 4; |
349 | sps->num_ref_frames_in_pic_order_cnt_cycle = |
350 | p_H264_Dpb-> |
351 | dpb_param.l.data[NUM_REF_FRAMES_IN_PIC_ORDER_CNT_CYCLE]; |
352 | for (i = 0; i < 128; i++) |
353 | sps->offset_for_ref_frame[i] = |
354 | (short) p_H264_Dpb-> |
355 | dpb_param.mmco.offset_for_ref_frame_base[i]; |
356 | sps->offset_for_non_ref_pic = |
357 | (short) p_H264_Dpb->dpb_param.l.data[OFFSET_FOR_NON_REF_PIC]; |
358 | sps->offset_for_top_to_bottom_field = |
359 | (short) p_H264_Dpb->dpb_param.l.data |
360 | [OFFSET_FOR_TOP_TO_BOTTOM_FIELD]; |
361 | |
362 | pSlice->frame_num = p_H264_Dpb->dpb_param.dpb.frame_num; |
363 | pSlice->idr_flag = |
364 | (p_H264_Dpb->dpb_param.dpb.NAL_info_mmco & 0x1f) |
365 | == 5 ? 1 : 0; |
366 | pSlice->nal_reference_idc = |
367 | (p_H264_Dpb->dpb_param.dpb.NAL_info_mmco >> 5) |
368 | & 0x3; |
369 | pSlice->pic_order_cnt_lsb = |
370 | p_H264_Dpb->dpb_param.dpb.pic_order_cnt_lsb; |
371 | pSlice->field_pic_flag = 0; |
372 | pSlice->bottom_field_flag = 0; |
373 | pSlice->delta_pic_order_cnt_bottom = val( |
374 | p_H264_Dpb->dpb_param.dpb.delta_pic_order_cnt_bottom); |
375 | pSlice->delta_pic_order_cnt[0] = val( |
376 | p_H264_Dpb->dpb_param.dpb.delta_pic_order_cnt_0); |
377 | pSlice->delta_pic_order_cnt[1] = val( |
378 | p_H264_Dpb->dpb_param.dpb.delta_pic_order_cnt_1); |
379 | |
380 | p_Vid->last_has_mmco_5 = 0; |
381 | /* last memory_management_control_operation is 5 */ |
382 | p_Vid->last_pic_bottom_field = 0; |
383 | p_Vid->max_frame_num = 1 << |
384 | (p_H264_Dpb->dpb_param.l.data[LOG2_MAX_FRAME_NUM]); |
385 | |
386 | /**/ |
387 | pSlice->structure = (p_H264_Dpb-> |
388 | dpb_param.l.data[NEW_PICTURE_STRUCTURE] == 3) ? |
389 | FRAME : p_H264_Dpb->dpb_param.l.data[NEW_PICTURE_STRUCTURE]; |
390 | if (pSlice->structure == FRAME) { |
391 | pSlice->field_pic_flag = 0; |
392 | pSlice->bottom_field_flag = 0; |
393 | } else { |
394 | pSlice->field_pic_flag = 1; |
395 | if (pSlice->structure == TOP_FIELD) |
396 | pSlice->bottom_field_flag = 0; |
397 | else |
398 | pSlice->bottom_field_flag = 1; |
399 | } |
400 | pSlice->pic_struct = p_H264_Dpb->dpb_param.l.data[PICTURE_STRUCT]; |
401 | |
402 | sps->num_ref_frames = p_H264_Dpb-> |
403 | dpb_param.l.data[MAX_REFERENCE_FRAME_NUM]; |
404 | sps->profile_idc = |
405 | (p_H264_Dpb->dpb_param.l.data[PROFILE_IDC_MMCO] >> 8) & 0xff; |
406 | /*sps->max_dpb_size = p_H264_Dpb->dpb_param.l.data[MAX_DPB_SIZE];*/ |
407 | if (pSlice->idr_flag) { |
408 | pSlice->long_term_reference_flag = mmco_cmd[0] & 1; |
409 | pSlice->no_output_of_prior_pics_flag = (mmco_cmd[0] >> 1) & 1; |
410 | dpb_print(p_H264_Dpb->decoder_index, |
411 | PRINT_FLAG_DPB_DETAIL, |
412 | "IDR: long_term_reference_flag %d no_output_of_prior_pics_flag %d\r\n", |
413 | pSlice->long_term_reference_flag, |
414 | pSlice->no_output_of_prior_pics_flag); |
415 | |
416 | dpb_print(p_H264_Dpb->decoder_index, |
417 | PRINT_FLAG_DPB_DETAIL, |
418 | "idr set pre_frame_num(%d) to frame_num (%d)\n", |
419 | p_Vid->pre_frame_num, pSlice->frame_num); |
420 | |
421 | p_Vid->pre_frame_num = pSlice->frame_num; |
422 | } else if (p_H264_Dpb->mDPB.first_pic_done == 0) { |
423 | /* by rain |
424 | handle the case when first slice is I instead of IDR |
425 | */ |
426 | p_Vid->pre_frame_num = pSlice->frame_num; |
427 | } |
428 | /* pSlice->adaptive_ref_pic_buffering_flag; */ |
429 | sps->log2_max_frame_num_minus4 = |
430 | p_H264_Dpb->dpb_param.l.data[LOG2_MAX_FRAME_NUM] - 4; |
431 | |
432 | p_Vid->non_conforming_stream = |
433 | p_H264_Dpb->dpb_param.l.data[NON_CONFORMING_STREAM]; |
434 | p_Vid->recovery_point = |
435 | p_H264_Dpb->dpb_param.l.data[RECOVERY_POINT]; |
436 | switch (p_H264_Dpb->dpb_param.l.data[SLICE_TYPE]) { |
437 | case I_Slice: |
438 | pSlice->slice_type = I_SLICE; |
439 | break; |
440 | case P_Slice: |
441 | pSlice->slice_type = P_SLICE; |
442 | break; |
443 | case B_Slice: |
444 | pSlice->slice_type = B_SLICE; |
445 | break; |
446 | default: |
447 | pSlice->slice_type = NUM_SLICE_TYPES; |
448 | break; |
449 | } |
450 | |
451 | pSlice->num_ref_idx_active[LIST_0] = |
452 | p_H264_Dpb->dpb_param.dpb.num_ref_idx_l0_active_minus1 + |
453 | 1; |
454 | /* p_H264_Dpb->dpb_param.l.data[PPS_NUM_REF_IDX_L0_ACTIVE_MINUS1]; */ |
455 | pSlice->num_ref_idx_active[LIST_1] = |
456 | p_H264_Dpb->dpb_param.dpb.num_ref_idx_l1_active_minus1 + |
457 | 1; |
458 | /* p_H264_Dpb->dpb_param.l.data[PPS_NUM_REF_IDX_L1_ACTIVE_MINUS1]; */ |
459 | |
460 | pSlice->p_Vid = p_Vid; |
461 | pSlice->p_Dpb = p_Dpb; |
462 | /* |
463 | p_H264_Dpb->colocated_buf_size = |
464 | p_H264_Dpb->dpb_param.l.data[FRAME_SIZE_IN_MB] * 96;*/ |
465 | pSlice->first_mb_in_slice = |
466 | p_H264_Dpb->dpb_param.l.data[FIRST_MB_IN_SLICE]; |
467 | pSlice->mode_8x8_flags = p_H264_Dpb->dpb_param.l.data[MODE_8X8_FLAGS]; |
468 | pSlice->picture_structure_mmco = |
469 | p_H264_Dpb->dpb_param.dpb.picture_structure_mmco; |
470 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
471 | "%s slice_type is %d, num_ref_idx_active[0,1]=%d,%d nal_reference_idc %d pic struct 0x%x(mmco stru 0x%x)\n", |
472 | __func__, pSlice->slice_type, |
473 | pSlice->num_ref_idx_active[LIST_0], |
474 | pSlice->num_ref_idx_active[LIST_1], |
475 | pSlice->nal_reference_idc, |
476 | pSlice->structure, |
477 | pSlice->picture_structure_mmco); |
478 | #ifdef ERROR_CHECK |
479 | if (pSlice->num_ref_idx_active[LIST_0] >= MAX_LIST_SIZE) { |
480 | pSlice->num_ref_idx_active[LIST_0] = MAX_LIST_SIZE - 1; |
481 | p_H264_Dpb->dpb_error_flag = __LINE__; |
482 | } |
483 | if (pSlice->num_ref_idx_active[LIST_1] >= MAX_LIST_SIZE) { |
484 | pSlice->num_ref_idx_active[LIST_1] = MAX_LIST_SIZE - 1; |
485 | p_H264_Dpb->dpb_error_flag = __LINE__; |
486 | } |
487 | #endif |
488 | |
489 | #if 1 |
490 | /* dec_ref_pic_marking_buffer */ |
491 | pSlice->adaptive_ref_pic_buffering_flag = 0; |
492 | if (pSlice->nal_reference_idc) { |
493 | for (i = 0, j = 0; i < 44; j++) { |
494 | unsigned short val; |
495 | struct DecRefPicMarking_s *tmp_drpm = |
496 | &pSlice->dec_ref_pic_marking_buffer[j]; |
497 | memset(tmp_drpm, 0, sizeof(struct DecRefPicMarking_s)); |
498 | val = tmp_drpm-> |
499 | memory_management_control_operation = |
500 | mmco_cmd[i++]; |
501 | tmp_drpm->Next = NULL; |
502 | if (j > 0) { |
503 | pSlice-> |
504 | dec_ref_pic_marking_buffer[j - 1].Next = |
505 | tmp_drpm; |
506 | } |
507 | if (val == 0 || i >= 44) |
508 | break; |
509 | pSlice->adaptive_ref_pic_buffering_flag = 1; |
510 | if ((val == 1) || (val == 3)) { |
511 | tmp_drpm->difference_of_pic_nums_minus1 = |
512 | mmco_cmd[i++]; |
513 | } |
514 | if (val == 2) |
515 | tmp_drpm->long_term_pic_num = mmco_cmd[i++]; |
516 | if (i >= 44) |
517 | break; |
518 | if ((val == 3) || (val == 6)) |
519 | tmp_drpm->long_term_frame_idx = mmco_cmd[i++]; |
520 | if (val == 4) { |
521 | tmp_drpm->max_long_term_frame_idx_plus1 = |
522 | mmco_cmd[i++]; |
523 | } |
524 | dpb_print(p_H264_Dpb->decoder_index, |
525 | PRINT_FLAG_DPB_DETAIL, |
526 | "dec_ref_pic_marking_buffer[%d]:operation %x diff_pic_minus1 %x long_pic_num %x long_frame_idx %x max_long_frame_idx_plus1 %x\n", |
527 | j, |
528 | tmp_drpm->memory_management_control_operation, |
529 | tmp_drpm->difference_of_pic_nums_minus1, |
530 | tmp_drpm->long_term_pic_num, |
531 | tmp_drpm->long_term_frame_idx, |
532 | tmp_drpm->max_long_term_frame_idx_plus1); |
533 | } |
534 | } |
535 | |
536 | ref_pic_list_reordering(p_H264_Dpb, pSlice); |
537 | #endif |
538 | |
539 | /*VUI*/ |
540 | p_H264_Dpb->vui_status = p_H264_Dpb->dpb_param.l.data[VUI_STATUS]; |
541 | p_H264_Dpb->aspect_ratio_idc = |
542 | p_H264_Dpb->dpb_param.l.data[ASPECT_RATIO_IDC]; |
543 | p_H264_Dpb->aspect_ratio_sar_width = |
544 | p_H264_Dpb->dpb_param.l.data[ASPECT_RATIO_SAR_WIDTH]; |
545 | p_H264_Dpb->aspect_ratio_sar_height = |
546 | p_H264_Dpb->dpb_param.l.data[ASPECT_RATIO_SAR_HEIGHT]; |
547 | |
548 | p_H264_Dpb->fixed_frame_rate_flag = p_H264_Dpb->dpb_param.l.data[ |
549 | FIXED_FRAME_RATE_FLAG]; |
550 | p_H264_Dpb->num_units_in_tick = |
551 | p_H264_Dpb->dpb_param.l.data[NUM_UNITS_IN_TICK]; |
552 | p_H264_Dpb->time_scale = p_H264_Dpb->dpb_param.l.data[TIME_SCALE] | |
553 | (p_H264_Dpb->dpb_param.l.data[TIME_SCALE + 1] << 16); |
554 | |
555 | p_H264_Dpb->bitstream_restriction_flag = |
556 | (p_H264_Dpb->dpb_param.l.data[SPS_FLAGS2] >> 3) & 0x1; |
557 | p_H264_Dpb->num_reorder_frames = |
558 | p_H264_Dpb->dpb_param.l.data[NUM_REORDER_FRAMES]; |
559 | p_H264_Dpb->max_dec_frame_buffering = |
560 | p_H264_Dpb->dpb_param.l.data[MAX_BUFFER_FRAME]; |
561 | |
562 | /**/ |
563 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
564 | "%s return\n", __func__); |
565 | } |
566 | |
567 | static void decode_poc(struct VideoParameters *p_Vid, struct Slice *pSlice) |
568 | { |
569 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Vid, |
570 | struct h264_dpb_stru, mVideo); |
571 | struct SPSParameters *active_sps = p_Vid->active_sps; |
572 | int i; |
573 | /* for POC mode 0: */ |
574 | unsigned int MaxPicOrderCntLsb = (1 << |
575 | (active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4)); |
576 | |
577 | dpb_print(p_H264_Dpb->decoder_index, |
578 | PRINT_FLAG_DEBUG_POC, |
579 | "%s:pic_order_cnt_type %d, idr_flag %d last_has_mmco_5 %d last_pic_bottom_field %d pic_order_cnt_lsb %d PrevPicOrderCntLsb %d\r\n", |
580 | __func__, |
581 | active_sps->pic_order_cnt_type, |
582 | pSlice->idr_flag, |
583 | p_Vid->last_has_mmco_5, |
584 | p_Vid->last_pic_bottom_field, |
585 | pSlice->pic_order_cnt_lsb, |
586 | p_Vid->PrevPicOrderCntLsb |
587 | ); |
588 | |
589 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DEBUG_POC, |
590 | "%s:field_pic_flag %d, bottom_field_flag %d frame_num %d PreviousFrameNum %d PreviousFrameNumOffset %d ax_frame_num %d num_ref_frames_in_pic_order_cnt_cycle %d offset_for_non_ref_pic %d\r\n", |
591 | __func__, |
592 | pSlice->field_pic_flag, |
593 | pSlice->bottom_field_flag, |
594 | pSlice->frame_num, |
595 | p_Vid->PreviousFrameNum, |
596 | p_Vid->PreviousFrameNumOffset, |
597 | p_Vid->max_frame_num, |
598 | active_sps->num_ref_frames_in_pic_order_cnt_cycle, |
599 | active_sps->offset_for_non_ref_pic |
600 | ); |
601 | |
602 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DEBUG_POC, |
603 | "%s: delta_pic_order_cnt %d %d nal_reference_idc %d\r\n", |
604 | __func__, |
605 | pSlice->delta_pic_order_cnt[0], pSlice->delta_pic_order_cnt[1], |
606 | pSlice->nal_reference_idc |
607 | ); |
608 | |
609 | |
610 | switch (active_sps->pic_order_cnt_type) { |
611 | case 0: /* POC MODE 0 */ |
612 | /* 1st */ |
613 | if (pSlice->idr_flag) { |
614 | p_Vid->PrevPicOrderCntMsb = 0; |
615 | p_Vid->PrevPicOrderCntLsb = 0; |
616 | } else { |
617 | if (p_Vid->last_has_mmco_5) { |
618 | if (p_Vid->last_pic_bottom_field) { |
619 | p_Vid->PrevPicOrderCntMsb = 0; |
620 | p_Vid->PrevPicOrderCntLsb = 0; |
621 | } else { |
622 | p_Vid->PrevPicOrderCntMsb = 0; |
623 | p_Vid->PrevPicOrderCntLsb = |
624 | pSlice->toppoc; |
625 | } |
626 | } |
627 | } |
628 | /* Calculate the MSBs of current picture */ |
629 | if (pSlice->pic_order_cnt_lsb < p_Vid->PrevPicOrderCntLsb && |
630 | (p_Vid->PrevPicOrderCntLsb - pSlice->pic_order_cnt_lsb) >= |
631 | (MaxPicOrderCntLsb / 2)) |
632 | pSlice->PicOrderCntMsb = p_Vid->PrevPicOrderCntMsb + |
633 | MaxPicOrderCntLsb; |
634 | else if (pSlice->pic_order_cnt_lsb > |
635 | p_Vid->PrevPicOrderCntLsb && |
636 | (pSlice->pic_order_cnt_lsb - |
637 | p_Vid->PrevPicOrderCntLsb) > |
638 | (MaxPicOrderCntLsb / 2)) |
639 | pSlice->PicOrderCntMsb = p_Vid->PrevPicOrderCntMsb - |
640 | MaxPicOrderCntLsb; |
641 | else |
642 | pSlice->PicOrderCntMsb = p_Vid->PrevPicOrderCntMsb; |
643 | |
644 | /* 2nd */ |
645 | if (pSlice->field_pic_flag == 0) { |
646 | /* frame pix */ |
647 | pSlice->toppoc = pSlice->PicOrderCntMsb + |
648 | pSlice->pic_order_cnt_lsb; |
649 | pSlice->bottompoc = pSlice->toppoc + |
650 | pSlice->delta_pic_order_cnt_bottom; |
651 | pSlice->ThisPOC = pSlice->framepoc = |
652 | (pSlice->toppoc < pSlice->bottompoc) ? |
653 | pSlice->toppoc : pSlice->bottompoc; |
654 | /* POC200301 */ |
655 | } else if (pSlice->bottom_field_flag == FALSE) { |
656 | /* top field */ |
657 | pSlice->ThisPOC = pSlice->toppoc = |
658 | pSlice->PicOrderCntMsb + |
659 | pSlice->pic_order_cnt_lsb; |
660 | } else { |
661 | /* bottom field */ |
662 | pSlice->ThisPOC = pSlice->bottompoc = |
663 | pSlice->PicOrderCntMsb + |
664 | pSlice->pic_order_cnt_lsb; |
665 | } |
666 | pSlice->framepoc = pSlice->ThisPOC; |
667 | |
668 | p_Vid->ThisPOC = pSlice->ThisPOC; |
669 | |
670 | /* if ( pSlice->frame_num != p_Vid->PreviousFrameNum) |
671 | * Seems redundant |
672 | */ |
673 | p_Vid->PreviousFrameNum = pSlice->frame_num; |
674 | |
675 | if (pSlice->nal_reference_idc) { |
676 | p_Vid->PrevPicOrderCntLsb = pSlice->pic_order_cnt_lsb; |
677 | p_Vid->PrevPicOrderCntMsb = pSlice->PicOrderCntMsb; |
678 | } |
679 | |
680 | break; |
681 | |
682 | case 1: /* POC MODE 1 */ |
683 | /* 1st */ |
684 | if (pSlice->idr_flag) { |
685 | p_Vid->FrameNumOffset = 0; /* first pix of IDRGOP */ |
686 | if (pSlice->frame_num) |
687 | dpb_print(p_H264_Dpb->decoder_index, |
688 | PRINT_FLAG_DPB_DETAIL, |
689 | "frame_num not equal to zero in IDR picture %d", |
690 | -1020); |
691 | } else { |
692 | if (p_Vid->last_has_mmco_5) { |
693 | p_Vid->PreviousFrameNumOffset = 0; |
694 | p_Vid->PreviousFrameNum = 0; |
695 | } |
696 | if (pSlice->frame_num < p_Vid->PreviousFrameNum) { |
697 | /* not first pix of IDRGOP */ |
698 | p_Vid->FrameNumOffset = |
699 | p_Vid->PreviousFrameNumOffset + |
700 | p_Vid->max_frame_num; |
701 | } else { |
702 | p_Vid->FrameNumOffset = |
703 | p_Vid->PreviousFrameNumOffset; |
704 | } |
705 | } |
706 | |
707 | /* 2nd */ |
708 | if (active_sps->num_ref_frames_in_pic_order_cnt_cycle) |
709 | pSlice->AbsFrameNum = |
710 | p_Vid->FrameNumOffset + pSlice->frame_num; |
711 | else |
712 | pSlice->AbsFrameNum = 0; |
713 | if ((!pSlice->nal_reference_idc) && pSlice->AbsFrameNum > 0) |
714 | pSlice->AbsFrameNum--; |
715 | |
716 | /* 3rd */ |
717 | p_Vid->ExpectedDeltaPerPicOrderCntCycle = 0; |
718 | |
719 | if (active_sps->num_ref_frames_in_pic_order_cnt_cycle) |
720 | for (i = 0; i < (int) active_sps-> |
721 | num_ref_frames_in_pic_order_cnt_cycle; i++) { |
722 | p_Vid->ExpectedDeltaPerPicOrderCntCycle += |
723 | active_sps->offset_for_ref_frame[i]; |
724 | dpb_print(p_H264_Dpb->decoder_index, |
725 | PRINT_FLAG_DEBUG_POC, |
726 | "%s: offset_for_ref_frame %d\r\n", |
727 | __func__, |
728 | active_sps-> |
729 | offset_for_ref_frame[i]); |
730 | } |
731 | |
732 | if (pSlice->AbsFrameNum) { |
733 | p_Vid->PicOrderCntCycleCnt = |
734 | (pSlice->AbsFrameNum - 1) / |
735 | active_sps-> |
736 | num_ref_frames_in_pic_order_cnt_cycle; |
737 | p_Vid->FrameNumInPicOrderCntCycle = |
738 | (pSlice->AbsFrameNum - 1) % |
739 | active_sps-> |
740 | num_ref_frames_in_pic_order_cnt_cycle; |
741 | p_Vid->ExpectedPicOrderCnt = |
742 | p_Vid->PicOrderCntCycleCnt * |
743 | p_Vid->ExpectedDeltaPerPicOrderCntCycle; |
744 | for (i = 0; i <= (int)p_Vid-> |
745 | FrameNumInPicOrderCntCycle; i++) { |
746 | p_Vid->ExpectedPicOrderCnt += |
747 | active_sps->offset_for_ref_frame[i]; |
748 | dpb_print(p_H264_Dpb->decoder_index, |
749 | PRINT_FLAG_DEBUG_POC, |
750 | "%s: offset_for_ref_frame %d\r\n", |
751 | __func__, |
752 | active_sps-> |
753 | offset_for_ref_frame[i]); |
754 | } |
755 | } else |
756 | p_Vid->ExpectedPicOrderCnt = 0; |
757 | |
758 | if (!pSlice->nal_reference_idc) |
759 | p_Vid->ExpectedPicOrderCnt += |
760 | active_sps->offset_for_non_ref_pic; |
761 | |
762 | if (pSlice->field_pic_flag == 0) { |
763 | /* frame pix */ |
764 | pSlice->toppoc = p_Vid->ExpectedPicOrderCnt + |
765 | pSlice->delta_pic_order_cnt[0]; |
766 | pSlice->bottompoc = pSlice->toppoc + |
767 | active_sps->offset_for_top_to_bottom_field + |
768 | pSlice->delta_pic_order_cnt[1]; |
769 | pSlice->ThisPOC = pSlice->framepoc = |
770 | (pSlice->toppoc < pSlice->bottompoc) ? |
771 | pSlice->toppoc : pSlice->bottompoc; |
772 | /* POC200301 */ |
773 | } else if (pSlice->bottom_field_flag == FALSE) { |
774 | /* top field */ |
775 | pSlice->ThisPOC = pSlice->toppoc = |
776 | p_Vid->ExpectedPicOrderCnt + |
777 | pSlice->delta_pic_order_cnt[0]; |
778 | } else { |
779 | /* bottom field */ |
780 | pSlice->ThisPOC = pSlice->bottompoc = |
781 | p_Vid->ExpectedPicOrderCnt + |
782 | active_sps->offset_for_top_to_bottom_field + |
783 | pSlice->delta_pic_order_cnt[0]; |
784 | } |
785 | pSlice->framepoc = pSlice->ThisPOC; |
786 | |
787 | p_Vid->PreviousFrameNum = pSlice->frame_num; |
788 | p_Vid->PreviousFrameNumOffset = p_Vid->FrameNumOffset; |
789 | |
790 | break; |
791 | |
792 | |
793 | case 2: /* POC MODE 2 */ |
794 | if (pSlice->idr_flag) { /* IDR picture */ |
795 | p_Vid->FrameNumOffset = 0; /* first pix of IDRGOP */ |
796 | pSlice->ThisPOC = pSlice->framepoc = pSlice->toppoc = |
797 | pSlice->bottompoc = 0; |
798 | if (pSlice->frame_num) |
799 | dpb_print(p_H264_Dpb->decoder_index, |
800 | PRINT_FLAG_DPB_DETAIL, |
801 | "frame_num not equal to zero in IDR picture %d", |
802 | -1020); |
803 | } else { |
804 | if (p_Vid->last_has_mmco_5) { |
805 | p_Vid->PreviousFrameNum = 0; |
806 | p_Vid->PreviousFrameNumOffset = 0; |
807 | } |
808 | if (pSlice->frame_num < p_Vid->PreviousFrameNum) |
809 | p_Vid->FrameNumOffset = |
810 | p_Vid->PreviousFrameNumOffset + |
811 | p_Vid->max_frame_num; |
812 | else |
813 | p_Vid->FrameNumOffset = |
814 | p_Vid->PreviousFrameNumOffset; |
815 | |
816 | pSlice->AbsFrameNum = p_Vid->FrameNumOffset + |
817 | pSlice->frame_num; |
818 | if (!pSlice->nal_reference_idc) |
819 | pSlice->ThisPOC = |
820 | (2 * pSlice->AbsFrameNum - 1); |
821 | else |
822 | pSlice->ThisPOC = (2 * pSlice->AbsFrameNum); |
823 | |
824 | if (pSlice->field_pic_flag == 0) |
825 | pSlice->toppoc = pSlice->bottompoc = |
826 | pSlice->framepoc = pSlice->ThisPOC; |
827 | else if (pSlice->bottom_field_flag == FALSE) |
828 | pSlice->toppoc = pSlice->framepoc = |
829 | pSlice->ThisPOC; |
830 | else |
831 | pSlice->bottompoc = pSlice->framepoc = |
832 | pSlice->ThisPOC; |
833 | } |
834 | |
835 | p_Vid->PreviousFrameNum = pSlice->frame_num; |
836 | p_Vid->PreviousFrameNumOffset = p_Vid->FrameNumOffset; |
837 | break; |
838 | |
839 | |
840 | default: |
841 | /* error must occurs */ |
842 | /* assert( 1==0 ); */ |
843 | break; |
844 | } |
845 | } |
846 | |
847 | void fill_frame_num_gap(struct VideoParameters *p_Vid, struct Slice *currSlice) |
848 | { |
849 | struct h264_dpb_stru *p_H264_Dpb = |
850 | container_of(p_Vid, struct h264_dpb_stru, mVideo); |
851 | struct SPSParameters *active_sps = p_Vid->active_sps; |
852 | int CurrFrameNum; |
853 | int UnusedShortTermFrameNum; |
854 | struct StorablePicture *picture = NULL; |
855 | int tmp1 = currSlice->delta_pic_order_cnt[0]; |
856 | int tmp2 = currSlice->delta_pic_order_cnt[1]; |
857 | int ret; |
858 | |
859 | currSlice->delta_pic_order_cnt[0] = |
860 | currSlice->delta_pic_order_cnt[1] = 0; |
861 | |
862 | dpb_print(p_H264_Dpb->decoder_index, |
863 | PRINT_FLAG_DPB_DETAIL, |
864 | "A gap in frame number is found, try to fill it.(pre_frame_num %d, max_frame_num %d\n", |
865 | p_Vid->pre_frame_num, p_Vid->max_frame_num |
866 | ); |
867 | |
868 | UnusedShortTermFrameNum = (p_Vid->pre_frame_num + 1) |
869 | % p_Vid->max_frame_num; |
870 | CurrFrameNum = currSlice->frame_num; /*p_Vid->frame_num;*/ |
871 | |
872 | while (CurrFrameNum != UnusedShortTermFrameNum) { |
873 | /*pr_err("CurrFrameNum = %d, UnusedShortTermFrameNum = %d\n", CurrFrameNum, UnusedShortTermFrameNum);*/ |
874 | /*picture = alloc_storable_picture |
875 | *(p_Vid, FRAME, p_Vid->width, |
876 | *p_Vid->height, |
877 | *p_Vid->width_cr, |
878 | *p_Vid->height_cr, 1); |
879 | */ |
880 | picture = get_new_pic(p_H264_Dpb, |
881 | p_H264_Dpb->mSlice.structure, |
882 | /*p_Vid->width, p_Vid->height, |
883 | *p_Vid->width_cr, |
884 | p_Vid->height_cr,*/ 1); |
885 | |
886 | if (picture == NULL) { |
887 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
888 | |
889 | dpb_print(p_H264_Dpb->decoder_index, |
890 | PRINT_FLAG_ERROR, |
891 | "%s Error: get_new_pic return NULL\r\n", |
892 | __func__); |
893 | /*h264_debug_flag |= PRINT_FLAG_DUMP_DPB;*/ |
894 | dump_dpb(p_Dpb, 0); |
895 | return; |
896 | } |
897 | |
898 | picture->colocated_buf_index = -1; |
899 | picture->buf_spec_num = -1; |
900 | picture->buf_spec_is_alloced = 0; |
901 | |
902 | picture->coded_frame = 1; |
903 | picture->pic_num = UnusedShortTermFrameNum; |
904 | picture->frame_num = UnusedShortTermFrameNum; |
905 | picture->non_existing = 1; |
906 | picture->is_output = 1; |
907 | picture->used_for_reference = 1; |
908 | picture->adaptive_ref_pic_buffering_flag = 0; |
909 | #if (MVC_EXTENSION_ENABLE) |
910 | picture->view_id = currSlice->view_id; |
911 | #endif |
912 | |
913 | currSlice->frame_num = UnusedShortTermFrameNum; |
914 | if (active_sps->pic_order_cnt_type != 0) { |
915 | /*decode_poc(p_Vid, p_Vid->ppSliceList[0]);*/ |
916 | decode_poc(&p_H264_Dpb->mVideo, &p_H264_Dpb->mSlice); |
917 | } |
918 | picture->top_poc = currSlice->toppoc; |
919 | picture->bottom_poc = currSlice->bottompoc; |
920 | picture->frame_poc = currSlice->framepoc; |
921 | picture->poc = currSlice->framepoc; |
922 | |
923 | ret = store_picture_in_dpb(p_H264_Dpb, picture, 0); |
924 | if (ret == -1) { |
925 | dpb_print(p_H264_Dpb->decoder_index, |
926 | PRINT_FLAG_ERROR, |
927 | "%s Error: store_picture_in_dpb failed, break\n", |
928 | __func__); |
929 | release_picture(p_H264_Dpb, picture); |
930 | bufmgr_force_recover(p_H264_Dpb); |
931 | return; |
932 | } |
933 | |
934 | picture = NULL; |
935 | p_Vid->pre_frame_num = UnusedShortTermFrameNum; |
936 | UnusedShortTermFrameNum = |
937 | (UnusedShortTermFrameNum + 1) % |
938 | p_Vid->max_frame_num; |
939 | } |
940 | currSlice->delta_pic_order_cnt[0] = tmp1; |
941 | currSlice->delta_pic_order_cnt[1] = tmp2; |
942 | currSlice->frame_num = CurrFrameNum; |
943 | } |
944 | |
945 | void dpb_init_global(struct h264_dpb_stru *p_H264_Dpb, |
946 | int id, int actual_dpb_size, int max_reference_size) |
947 | { |
948 | int i; |
949 | |
950 | init_dummy_fs(); |
951 | |
952 | memset(&p_H264_Dpb->mDPB, 0, sizeof(struct DecodedPictureBuffer)); |
953 | |
954 | memset(&p_H264_Dpb->mSlice, 0, sizeof(struct Slice)); |
955 | memset(&p_H264_Dpb->mVideo, 0, sizeof(struct VideoParameters)); |
956 | memset(&p_H264_Dpb->mSPS, 0, sizeof(struct SPSParameters)); |
957 | |
958 | for (i = 0; i < DPB_SIZE_MAX; i++) { |
959 | memset(&(p_H264_Dpb->mFrameStore[i]), 0, |
960 | sizeof(struct FrameStore)); |
961 | } |
962 | |
963 | for (i = 0; i < MAX_PIC_BUF_NUM; i++) { |
964 | memset(&(p_H264_Dpb->m_PIC[i]), 0, |
965 | sizeof(struct StorablePicture)); |
966 | p_H264_Dpb->m_PIC[i].index = i; |
967 | } |
968 | p_H264_Dpb->decoder_index = id; |
969 | |
970 | /* make sure dpb_init_global |
971 | *can be called during decoding |
972 | *(in DECODE_STATE_IDLE or DECODE_STATE_READY state) |
973 | */ |
974 | p_H264_Dpb->mDPB.size = actual_dpb_size; |
975 | p_H264_Dpb->max_reference_size = max_reference_size; |
976 | p_H264_Dpb->poc_even_odd_flag = 0; |
977 | } |
978 | |
979 | static void init_picture(struct h264_dpb_stru *p_H264_Dpb, |
980 | struct Slice *currSlice, |
981 | struct StorablePicture *dec_picture) |
982 | { |
983 | /* struct VideoParameters *p_Vid = &(p_H264_Dpb->mVideo); */ |
984 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
985 | "%s dec_picture %p\n", __func__, dec_picture); |
986 | dec_picture->top_poc = currSlice->toppoc; |
987 | dec_picture->bottom_poc = currSlice->bottompoc; |
988 | dec_picture->frame_poc = currSlice->framepoc; |
989 | switch (currSlice->structure) { |
990 | case TOP_FIELD: { |
991 | dec_picture->poc = currSlice->toppoc; |
992 | /* p_Vid->number *= 2; */ |
993 | break; |
994 | } |
995 | case BOTTOM_FIELD: { |
996 | dec_picture->poc = currSlice->bottompoc; |
997 | /* p_Vid->number = p_Vid->number * 2 + 1; */ |
998 | break; |
999 | } |
1000 | case FRAME: { |
1001 | dec_picture->poc = currSlice->framepoc; |
1002 | break; |
1003 | } |
1004 | default: |
1005 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1006 | "p_Vid->structure not initialized %d\n", 235); |
1007 | } |
1008 | |
1009 | /* dec_picture->slice_type = p_Vid->type; */ |
1010 | dec_picture->used_for_reference = (currSlice->nal_reference_idc != 0); |
1011 | dec_picture->idr_flag = currSlice->idr_flag; |
1012 | dec_picture->no_output_of_prior_pics_flag = |
1013 | currSlice->no_output_of_prior_pics_flag; |
1014 | dec_picture->long_term_reference_flag = |
1015 | currSlice->long_term_reference_flag; |
1016 | #if 1 |
1017 | dec_picture->adaptive_ref_pic_buffering_flag = |
1018 | currSlice->adaptive_ref_pic_buffering_flag; |
1019 | dec_picture->dec_ref_pic_marking_buffer = |
1020 | &currSlice->dec_ref_pic_marking_buffer[0]; |
1021 | #endif |
1022 | /* currSlice->dec_ref_pic_marking_buffer = NULL; */ |
1023 | |
1024 | /* dec_picture->mb_aff_frame_flag = currSlice->mb_aff_frame_flag; */ |
1025 | /* dec_picture->PicWidthInMbs = p_Vid->PicWidthInMbs; */ |
1026 | |
1027 | /* p_Vid->get_mb_block_pos = |
1028 | * dec_picture->mb_aff_frame_flag ? get_mb_block_pos_mbaff : |
1029 | * get_mb_block_pos_normal; |
1030 | */ |
1031 | /* p_Vid->getNeighbour = |
1032 | * dec_picture->mb_aff_frame_flag ? getAffNeighbour : |
1033 | * getNonAffNeighbour; |
1034 | */ |
1035 | |
1036 | dec_picture->pic_num = currSlice->frame_num; |
1037 | dec_picture->frame_num = currSlice->frame_num; |
1038 | |
1039 | /* dec_picture->recovery_frame = |
1040 | * (unsigned int) ((int) currSlice->frame_num == |
1041 | * p_Vid->recovery_frame_num); |
1042 | */ |
1043 | |
1044 | dec_picture->coded_frame = (currSlice->structure == FRAME); |
1045 | |
1046 | /* dec_picture->chroma_format_idc = active_sps->chroma_format_idc; */ |
1047 | |
1048 | /* dec_picture->frame_mbs_only_flag = |
1049 | * active_sps->frame_mbs_only_flag; |
1050 | */ |
1051 | /* dec_picture->frame_cropping_flag = |
1052 | * active_sps->frame_cropping_flag; |
1053 | */ |
1054 | |
1055 | if ((currSlice->picture_structure_mmco & 0x3) == 3) { |
1056 | dec_picture->mb_aff_frame_flag = 1; |
1057 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1058 | "%s, picture_structure_mmco is %x, set mb_aff_frame_flag to 1\n", |
1059 | __func__, |
1060 | currSlice->picture_structure_mmco); |
1061 | } |
1062 | |
1063 | if (currSlice->pic_struct < PIC_INVALID) { |
1064 | dec_picture->pic_struct = currSlice->pic_struct; |
1065 | } else { |
1066 | dec_picture->pic_struct = PIC_INVALID; |
1067 | } |
1068 | |
1069 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1070 | "%s pic_struct = %d\n", __func__, dec_picture->pic_struct); |
1071 | } |
1072 | |
1073 | void dump_pic(struct h264_dpb_stru *p_H264_Dpb) |
1074 | { |
1075 | int ii; |
1076 | struct StorablePicture *pic; |
1077 | for (ii = 0; ii < MAX_PIC_BUF_NUM; ii++) { |
1078 | pic = &(p_H264_Dpb->m_PIC[ii]); |
1079 | if (pic->is_used) { |
1080 | dpb_print(p_H264_Dpb->decoder_index, 0, |
1081 | "pic(%d,%d) poc %d is_used %d bufspec %d colbuf %d for_ref %d long_term %d pre_out %d output %d nonexist %d data_flag 0x%x\n", |
1082 | ii, pic->index, |
1083 | pic->poc, |
1084 | pic->is_used, |
1085 | pic->buf_spec_num, |
1086 | pic->colocated_buf_index, |
1087 | pic->used_for_reference, |
1088 | pic->is_long_term, |
1089 | pic->pre_output, |
1090 | pic->is_output, |
1091 | pic->non_existing, |
1092 | pic->data_flag); |
1093 | } |
1094 | } |
1095 | } |
1096 | |
1097 | /* |
1098 | static void is_pic_used_by_dpb(struct h264_dpb_stru *p_H264_Dpb, |
1099 | struct StorablePicture *pic) |
1100 | { |
1101 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
1102 | unsigned i; |
1103 | for (i = 0; i < p_Dpb->used_size; i++) { |
1104 | if (p_Dpb->fs[i]->top_field == pic || |
1105 | p_Dpb->fs[i]->bottom_field == pic || |
1106 | p_Dpb->fs[i]->frame == pic |
1107 | ) |
1108 | break; |
1109 | } |
1110 | if (i < p_Dpb->used_size) |
1111 | return 1; |
1112 | return 0; |
1113 | } |
1114 | */ |
1115 | |
1116 | static struct StorablePicture *get_new_pic(struct h264_dpb_stru *p_H264_Dpb, |
1117 | enum PictureStructure structure, unsigned char is_output) |
1118 | { |
1119 | struct StorablePicture *s = NULL; |
1120 | struct StorablePicture *pic; |
1121 | struct VideoParameters *p_Vid = &(p_H264_Dpb->mVideo); |
1122 | /* recycle un-used pic */ |
1123 | int ii = 0; |
1124 | |
1125 | for (ii = 0; ii < MAX_PIC_BUF_NUM; ii++) { |
1126 | pic = &(p_H264_Dpb->m_PIC[ii]); |
1127 | if (pic->is_used == 0) { |
1128 | pic->is_used = 1; |
1129 | s = pic; |
1130 | break; |
1131 | } |
1132 | } |
1133 | |
1134 | if (s) { |
1135 | s->buf_spec_is_alloced = 0; |
1136 | s->pic_num = 0; |
1137 | s->frame_num = 0; |
1138 | s->long_term_frame_idx = 0; |
1139 | s->long_term_pic_num = 0; |
1140 | s->used_for_reference = 0; |
1141 | s->is_long_term = 0; |
1142 | s->non_existing = 0; |
1143 | s->is_output = 0; |
1144 | s->pre_output = 0; |
1145 | s->max_slice_id = 0; |
1146 | s->data_flag &= ~(ERROR_FLAG | NODISP_FLAG); |
1147 | #if (MVC_EXTENSION_ENABLE) |
1148 | s->view_id = -1; |
1149 | #endif |
1150 | |
1151 | s->structure = structure; |
1152 | |
1153 | #if 0 |
1154 | s->size_x = size_x; |
1155 | s->size_y = size_y; |
1156 | s->size_x_cr = size_x_cr; |
1157 | s->size_y_cr = size_y_cr; |
1158 | s->size_x_m1 = size_x - 1; |
1159 | s->size_y_m1 = size_y - 1; |
1160 | s->size_x_cr_m1 = size_x_cr - 1; |
1161 | s->size_y_cr_m1 = size_y_cr - 1; |
1162 | |
1163 | s->top_field = p_Vid->no_reference_picture; |
1164 | s->bottom_field = p_Vid->no_reference_picture; |
1165 | s->frame = p_Vid->no_reference_picture; |
1166 | #endif |
1167 | /* s->dec_ref_pic_marking_buffer = NULL; */ |
1168 | |
1169 | s->coded_frame = 0; |
1170 | s->mb_aff_frame_flag = 0; |
1171 | |
1172 | s->top_poc = s->bottom_poc = s->poc = 0; |
1173 | s->seiHasTone_mapping = 0; |
1174 | s->frame_mbs_only_flag = p_Vid->active_sps->frame_mbs_only_flag; |
1175 | |
1176 | if (!p_Vid->active_sps->frame_mbs_only_flag && |
1177 | structure != FRAME) { |
1178 | int i, j; |
1179 | |
1180 | for (j = 0; j < MAX_NUM_SLICES; j++) { |
1181 | for (i = 0; i < 2; i++) { |
1182 | /* s->listX[j][i] = |
1183 | *calloc(MAX_LIST_SIZE, |
1184 | *sizeof (struct StorablePicture *)); |
1185 | *+1 for reordering ??? |
1186 | |
1187 | *if (NULL == s->listX[j][i]) |
1188 | *no_mem_exit("alloc_storable_picture: |
1189 | *s->listX[i]"); |
1190 | */ |
1191 | } |
1192 | } |
1193 | } |
1194 | } else |
1195 | p_H264_Dpb->buf_alloc_fail = 1; |
1196 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1197 | "%s %p\n", __func__, s); |
1198 | return s; |
1199 | } |
1200 | |
1201 | static void free_picture(struct h264_dpb_stru *p_H264_Dpb, |
1202 | struct StorablePicture *pic) |
1203 | { |
1204 | if (pic == NULL || pic->index < 0 || |
1205 | pic->index >= MAX_PIC_BUF_NUM) |
1206 | return; |
1207 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1208 | "%s %p %d\n", __func__, pic, pic->index); |
1209 | /* assert(pic->index<MAX_PIC_BUF_NUM); */ |
1210 | p_H264_Dpb->m_PIC[pic->index].is_used = 0; |
1211 | } |
1212 | |
1213 | static void gen_field_ref_ids(struct VideoParameters *p_Vid, |
1214 | struct StorablePicture *p) |
1215 | { |
1216 | int i, j; |
1217 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Vid, |
1218 | struct h264_dpb_stru, mVideo); |
1219 | /* ! Generate Frame parameters from field information. */ |
1220 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1221 | "%s\n", __func__); |
1222 | |
1223 | /* copy the list; */ |
1224 | for (j = 0; j < p_Vid->iSliceNumOfCurrPic; j++) { |
1225 | if (p->listX[j][LIST_0]) { |
1226 | p->listXsize[j][LIST_0] = |
1227 | p_Vid->ppSliceList[j]->listXsize[LIST_0]; |
1228 | for (i = 0; i < p->listXsize[j][LIST_0]; i++) |
1229 | p->listX[j][LIST_0][i] = |
1230 | p_Vid->ppSliceList[j]->listX[LIST_0][i]; |
1231 | } |
1232 | if (p->listX[j][LIST_1]) { |
1233 | p->listXsize[j][LIST_1] = |
1234 | p_Vid->ppSliceList[j]->listXsize[LIST_1]; |
1235 | for (i = 0; i < p->listXsize[j][LIST_1]; i++) |
1236 | p->listX[j][LIST_1][i] = |
1237 | p_Vid->ppSliceList[j]->listX[LIST_1][i]; |
1238 | } |
1239 | } |
1240 | } |
1241 | |
1242 | static void init_dpb(struct h264_dpb_stru *p_H264_Dpb, int type) |
1243 | { |
1244 | unsigned int i; |
1245 | struct VideoParameters *p_Vid = &p_H264_Dpb->mVideo; |
1246 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
1247 | struct SPSParameters *active_sps = &p_H264_Dpb->mSPS; |
1248 | |
1249 | p_Vid->active_sps = active_sps; |
1250 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1251 | "%s\n", __func__); |
1252 | |
1253 | p_Dpb->p_Vid = p_Vid; |
1254 | if (p_Dpb->init_done) { |
1255 | /* free_dpb(p_Dpb); */ |
1256 | if (p_Vid->no_reference_picture) { |
1257 | free_picture(p_H264_Dpb, p_Vid->no_reference_picture); |
1258 | p_Vid->no_reference_picture = NULL; |
1259 | } |
1260 | p_Dpb->init_done = 0; |
1261 | } |
1262 | |
1263 | /* p_Dpb->size = 10; //active_sps->max_dpb_size; //16; |
1264 | * getDpbSize(p_Vid, active_sps) + |
1265 | * p_Vid->p_Inp->dpb_plus[type==2? 1: 0]; |
1266 | * p_Dpb->size = active_sps->max_dpb_size; //16; |
1267 | * getDpbSize(p_Vid, active_sps) + |
1268 | * p_Vid->p_Inp->dpb_plus[type==2? 1: 0]; |
1269 | * p_Dpb->size initialzie in vh264.c |
1270 | */ |
1271 | p_Dpb->num_ref_frames = active_sps->num_ref_frames; |
1272 | /* p_Dpb->num_ref_frames initialzie in vh264.c */ |
1273 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1274 | "%s dpb_size is %d num_ref_frames = %d (%d)\n", |
1275 | __func__, p_Dpb->size, |
1276 | p_Dpb->num_ref_frames, |
1277 | active_sps->num_ref_frames); |
1278 | if (active_sps->num_ref_frames == 0xffff) { |
1279 | dpb_print(p_H264_Dpb->decoder_index, 0, |
1280 | "!!!Warning, num_ref_frames = %d is invalid\n", |
1281 | active_sps->num_ref_frames); |
1282 | } |
1283 | |
1284 | #if 0 |
1285 | /* ??? */ |
1286 | #if (MVC_EXTENSION_ENABLE) |
1287 | if ((unsigned int)active_sps->max_dec_frame_buffering < |
1288 | active_sps->num_ref_frames) { |
1289 | #else |
1290 | if (p_Dpb->size < active_sps->num_ref_frames) { |
1291 | #endif |
1292 | error( |
1293 | "DPB size at specified level is smaller than the specified number of reference frames. This is not allowed.\n", |
1294 | 1000); |
1295 | } |
1296 | #endif |
1297 | |
1298 | p_Dpb->used_size = 0; |
1299 | p_Dpb->last_picture = NULL; |
1300 | |
1301 | p_Dpb->ref_frames_in_buffer = 0; |
1302 | p_Dpb->ltref_frames_in_buffer = 0; |
1303 | |
1304 | #if 0 |
1305 | p_Dpb->fs = calloc(p_Dpb->size, sizeof(struct FrameStore *)); |
1306 | if (NULL == p_Dpb->fs) |
1307 | no_mem_exit("init_dpb: p_Dpb->fs"); |
1308 | |
1309 | p_Dpb->fs_ref = calloc(p_Dpb->size, sizeof(struct FrameStore *)); |
1310 | if (NULL == p_Dpb->fs_ref) |
1311 | no_mem_exit("init_dpb: p_Dpb->fs_ref"); |
1312 | |
1313 | p_Dpb->fs_ltref = calloc(p_Dpb->size, sizeof(struct FrameStore *)); |
1314 | if (NULL == p_Dpb->fs_ltref) |
1315 | no_mem_exit("init_dpb: p_Dpb->fs_ltref"); |
1316 | #endif |
1317 | |
1318 | #if (MVC_EXTENSION_ENABLE) |
1319 | p_Dpb->fs_ilref = calloc(1, sizeof(struct FrameStore *)); |
1320 | if (NULL == p_Dpb->fs_ilref) |
1321 | no_mem_exit("init_dpb: p_Dpb->fs_ilref"); |
1322 | #endif |
1323 | |
1324 | for (i = 0; i < p_Dpb->size; i++) { |
1325 | p_Dpb->fs[i] = &(p_H264_Dpb->mFrameStore[i]); |
1326 | /* alloc_frame_store(); */ |
1327 | p_Dpb->fs[i]->index = i; |
1328 | p_Dpb->fs_ref[i] = NULL; |
1329 | p_Dpb->fs_ltref[i] = NULL; |
1330 | p_Dpb->fs[i]->layer_id = 0; /* MVC_INIT_VIEW_ID; */ |
1331 | #if (MVC_EXTENSION_ENABLE) |
1332 | p_Dpb->fs[i]->view_id = MVC_INIT_VIEW_ID; |
1333 | p_Dpb->fs[i]->inter_view_flag[0] = |
1334 | p_Dpb->fs[i]->inter_view_flag[1] = 0; |
1335 | p_Dpb->fs[i]->anchor_pic_flag[0] = |
1336 | p_Dpb->fs[i]->anchor_pic_flag[1] = 0; |
1337 | #endif |
1338 | } |
1339 | #if (MVC_EXTENSION_ENABLE) |
1340 | if (type == 2) { |
1341 | p_Dpb->fs_ilref[0] = alloc_frame_store(); |
1342 | /* These may need some cleanups */ |
1343 | p_Dpb->fs_ilref[0]->view_id = MVC_INIT_VIEW_ID; |
1344 | p_Dpb->fs_ilref[0]->inter_view_flag[0] = |
1345 | p_Dpb->fs_ilref[0]->inter_view_flag[1] = 0; |
1346 | p_Dpb->fs_ilref[0]->anchor_pic_flag[0] = |
1347 | p_Dpb->fs_ilref[0]->anchor_pic_flag[1] = 0; |
1348 | /* given that this is in a different buffer, |
1349 | * do we even need proc_flag anymore? |
1350 | */ |
1351 | } else |
1352 | p_Dpb->fs_ilref[0] = NULL; |
1353 | #endif |
1354 | |
1355 | /* |
1356 | *for (i = 0; i < 6; i++) |
1357 | *{ |
1358 | *currSlice->listX[i] = |
1359 | * calloc(MAX_LIST_SIZE, sizeof (struct StorablePicture *)); |
1360 | * +1 for reordering |
1361 | *if (NULL == currSlice->listX[i]) |
1362 | *no_mem_exit("init_dpb: currSlice->listX[i]"); |
1363 | *} |
1364 | */ |
1365 | /* allocate a dummy storable picture */ |
1366 | if (!p_Vid->no_reference_picture) { |
1367 | p_Vid->no_reference_picture = get_new_pic(p_H264_Dpb, |
1368 | FRAME, |
1369 | /*p_Vid->width, p_Vid->height, |
1370 | *p_Vid->width_cr, p_Vid->height_cr, |
1371 | */ |
1372 | 1); |
1373 | p_Vid->no_reference_picture->top_field = |
1374 | p_Vid->no_reference_picture; |
1375 | p_Vid->no_reference_picture->bottom_field = |
1376 | p_Vid->no_reference_picture; |
1377 | p_Vid->no_reference_picture->frame = |
1378 | p_Vid->no_reference_picture; |
1379 | } |
1380 | p_Dpb->last_output_poc = INT_MIN; |
1381 | |
1382 | #if (MVC_EXTENSION_ENABLE) |
1383 | p_Dpb->last_output_view_id = -1; |
1384 | #endif |
1385 | |
1386 | p_Vid->last_has_mmco_5 = 0; |
1387 | |
1388 | init_colocate_buf(p_H264_Dpb, p_H264_Dpb->max_reference_size); |
1389 | |
1390 | p_Dpb->init_done = 1; |
1391 | |
1392 | #if 0 |
1393 | /* ??? */ |
1394 | /* picture error concealment */ |
1395 | if (p_Vid->conceal_mode != 0 && !p_Vid->last_out_fs) |
1396 | p_Vid->last_out_fs = alloc_frame_store(); |
1397 | #endif |
1398 | } |
1399 | |
1400 | static void dpb_split_field(struct h264_dpb_stru *p_H264_Dpb, |
1401 | struct FrameStore *fs) |
1402 | { |
1403 | struct StorablePicture *fs_top = NULL, *fs_btm = NULL; |
1404 | struct StorablePicture *frame = fs->frame; |
1405 | |
1406 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1407 | "%s %p %p\n", __func__, fs, frame); |
1408 | |
1409 | fs->poc = frame->poc; |
1410 | |
1411 | if (!frame->frame_mbs_only_flag) { |
1412 | fs_top = fs->top_field = get_new_pic(p_H264_Dpb, |
1413 | TOP_FIELD, |
1414 | /* frame->size_x, frame->size_y, |
1415 | *frame->size_x_cr, frame->size_y_cr, |
1416 | */ |
1417 | 1); |
1418 | fs_btm = fs->bottom_field = get_new_pic(p_H264_Dpb, |
1419 | BOTTOM_FIELD, |
1420 | /*frame->size_x, frame->size_y, |
1421 | *frame->size_x_cr, frame->size_y_cr, |
1422 | */ |
1423 | 1); |
1424 | if (fs_top == NULL || fs_btm == NULL) |
1425 | return; |
1426 | #if 1 |
1427 | /* rain */ |
1428 | fs_top->buf_spec_num = frame->buf_spec_num; |
1429 | fs_btm->buf_spec_num = frame->buf_spec_num; |
1430 | |
1431 | fs_top->colocated_buf_index = frame->colocated_buf_index; |
1432 | fs_btm->colocated_buf_index = frame->colocated_buf_index; |
1433 | |
1434 | fs_top->data_flag = frame->data_flag; |
1435 | fs_btm->data_flag = frame->data_flag; |
1436 | #endif |
1437 | fs_top->poc = frame->top_poc; |
1438 | fs_btm->poc = frame->bottom_poc; |
1439 | |
1440 | #if (MVC_EXTENSION_ENABLE) |
1441 | fs_top->view_id = frame->view_id; |
1442 | fs_btm->view_id = frame->view_id; |
1443 | #endif |
1444 | |
1445 | fs_top->frame_poc = frame->frame_poc; |
1446 | |
1447 | fs_top->bottom_poc = fs_btm->bottom_poc = frame->bottom_poc; |
1448 | fs_top->top_poc = fs_btm->top_poc = frame->top_poc; |
1449 | fs_btm->frame_poc = frame->frame_poc; |
1450 | |
1451 | fs_top->used_for_reference = fs_btm->used_for_reference |
1452 | = frame->used_for_reference; |
1453 | fs_top->is_long_term = fs_btm->is_long_term |
1454 | = frame->is_long_term; |
1455 | fs->long_term_frame_idx = fs_top->long_term_frame_idx |
1456 | = fs_btm->long_term_frame_idx |
1457 | = frame->long_term_frame_idx; |
1458 | |
1459 | fs_top->coded_frame = fs_btm->coded_frame = 1; |
1460 | fs_top->mb_aff_frame_flag = fs_btm->mb_aff_frame_flag |
1461 | = frame->mb_aff_frame_flag; |
1462 | |
1463 | frame->top_field = fs_top; |
1464 | frame->bottom_field = fs_btm; |
1465 | frame->frame = frame; |
1466 | fs_top->bottom_field = fs_btm; |
1467 | fs_top->frame = frame; |
1468 | fs_top->top_field = fs_top; |
1469 | fs_btm->top_field = fs_top; |
1470 | fs_btm->frame = frame; |
1471 | fs_btm->bottom_field = fs_btm; |
1472 | |
1473 | #if (MVC_EXTENSION_ENABLE) |
1474 | fs_top->view_id = fs_btm->view_id = fs->view_id; |
1475 | fs_top->inter_view_flag = fs->inter_view_flag[0]; |
1476 | fs_btm->inter_view_flag = fs->inter_view_flag[1]; |
1477 | #endif |
1478 | |
1479 | fs_top->chroma_format_idc = fs_btm->chroma_format_idc = |
1480 | frame->chroma_format_idc; |
1481 | fs_top->iCodingType = fs_btm->iCodingType = frame->iCodingType; |
1482 | } else { |
1483 | fs->top_field = NULL; |
1484 | fs->bottom_field = NULL; |
1485 | frame->top_field = NULL; |
1486 | frame->bottom_field = NULL; |
1487 | frame->frame = frame; |
1488 | } |
1489 | |
1490 | } |
1491 | |
1492 | |
1493 | static void dpb_combine_field(struct h264_dpb_stru *p_H264_Dpb, |
1494 | struct FrameStore *fs) |
1495 | { |
1496 | |
1497 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1498 | "%s\n", __func__); |
1499 | |
1500 | if (!fs->frame) { |
1501 | fs->frame = get_new_pic(p_H264_Dpb, |
1502 | FRAME, |
1503 | /* fs->top_field->size_x, fs->top_field->size_y*2, |
1504 | *fs->top_field->size_x_cr, fs->top_field->size_y_cr*2, |
1505 | */ |
1506 | 1); |
1507 | } |
1508 | if (!fs->frame) |
1509 | return; |
1510 | #if 1 |
1511 | /* rain */ |
1512 | fs->frame->buf_spec_num = fs->top_field->buf_spec_num; |
1513 | fs->frame->colocated_buf_index = fs->top_field->colocated_buf_index; |
1514 | fs->frame->data_flag = fs->top_field->data_flag; |
1515 | fs->frame->slice_type = fs->top_field->slice_type; |
1516 | if (fs->bottom_field) |
1517 | fs->frame->data_flag |= (fs->bottom_field->data_flag & 0xf0); |
1518 | #endif |
1519 | |
1520 | if (fs->bottom_field) { |
1521 | fs->poc = fs->frame->poc = fs->frame->frame_poc = imin( |
1522 | fs->top_field->poc, fs->bottom_field->poc); |
1523 | |
1524 | fs->bottom_field->frame_poc = fs->top_field->frame_poc = fs->frame->poc; |
1525 | |
1526 | fs->bottom_field->top_poc = fs->frame->top_poc = fs->top_field->poc; |
1527 | fs->top_field->bottom_poc = fs->frame->bottom_poc = |
1528 | fs->bottom_field->poc; |
1529 | |
1530 | fs->frame->used_for_reference = (fs->top_field->used_for_reference && |
1531 | fs->bottom_field->used_for_reference); |
1532 | fs->frame->is_long_term = (fs->top_field->is_long_term && |
1533 | fs->bottom_field->is_long_term); |
1534 | } |
1535 | |
1536 | if (fs->frame->is_long_term) |
1537 | fs->frame->long_term_frame_idx = fs->long_term_frame_idx; |
1538 | |
1539 | fs->frame->top_field = fs->top_field; |
1540 | if (fs->bottom_field) |
1541 | fs->frame->bottom_field = fs->bottom_field; |
1542 | fs->frame->frame = fs->frame; |
1543 | |
1544 | fs->frame->coded_frame = 0; |
1545 | |
1546 | fs->frame->chroma_format_idc = fs->top_field->chroma_format_idc; |
1547 | fs->frame->frame_cropping_flag = fs->top_field->frame_cropping_flag; |
1548 | if (fs->frame->frame_cropping_flag) { |
1549 | fs->frame->frame_crop_top_offset = |
1550 | fs->top_field->frame_crop_top_offset; |
1551 | fs->frame->frame_crop_bottom_offset = |
1552 | fs->top_field->frame_crop_bottom_offset; |
1553 | fs->frame->frame_crop_left_offset = |
1554 | fs->top_field->frame_crop_left_offset; |
1555 | fs->frame->frame_crop_right_offset = |
1556 | fs->top_field->frame_crop_right_offset; |
1557 | } |
1558 | if (fs->bottom_field) { |
1559 | fs->top_field->frame = fs->bottom_field->frame = fs->frame; |
1560 | fs->top_field->top_field = fs->top_field; |
1561 | fs->top_field->bottom_field = fs->bottom_field; |
1562 | fs->bottom_field->top_field = fs->top_field; |
1563 | fs->bottom_field->bottom_field = fs->bottom_field; |
1564 | } |
1565 | |
1566 | /**/ |
1567 | #if (MVC_EXTENSION_ENABLE) |
1568 | fs->frame->view_id = fs->view_id; |
1569 | #endif |
1570 | fs->frame->iCodingType = fs->top_field->iCodingType; |
1571 | if (fs->bottom_field && fs->top_field->poc < fs->bottom_field->poc) { |
1572 | fs->pts = fs->top_field->pts; |
1573 | fs->pts64 = fs->top_field->pts64; |
1574 | /*SWPL-7105 fix */ |
1575 | if ((fs->frame->slice_type == B_SLICE) |
1576 | && (!fs->bottom_field->pts) &&(!fs->bottom_field->pts64)) { |
1577 | fs->pts = 0; |
1578 | fs->pts64 = 0; |
1579 | } |
1580 | fs->offset_delimiter = fs->top_field->offset_delimiter; |
1581 | } else if (fs->bottom_field) { |
1582 | fs->pts = fs->bottom_field->pts; |
1583 | fs->pts64 = fs->bottom_field->pts64; |
1584 | fs->offset_delimiter = fs->bottom_field->offset_delimiter; |
1585 | } |
1586 | /* FIELD_CODING ;*/ |
1587 | } |
1588 | |
1589 | static void calculate_frame_no(struct VideoParameters *p_Vid, |
1590 | struct StorablePicture *p) |
1591 | { |
1592 | #if 0 |
1593 | /* ??? */ |
1594 | InputParameters *p_Inp = p_Vid->p_Inp; |
1595 | /* calculate frame number */ |
1596 | int psnrPOC = p_Vid->active_sps->mb_adaptive_frame_field_flag ? |
1597 | p->poc / (p_Inp->poc_scale) : p->poc / (p_Inp->poc_scale); |
1598 | |
1599 | if (psnrPOC == 0) { /* && p_Vid->psnr_number) */ |
1600 | p_Vid->idr_psnr_number = |
1601 | p_Vid->g_nFrame * p_Vid->ref_poc_gap / (p_Inp->poc_scale); |
1602 | } |
1603 | p_Vid->psnr_number = imax(p_Vid->psnr_number, |
1604 | p_Vid->idr_psnr_number + psnrPOC); |
1605 | |
1606 | p_Vid->frame_no = p_Vid->idr_psnr_number + psnrPOC; |
1607 | #endif |
1608 | } |
1609 | |
1610 | static void insert_picture_in_dpb(struct h264_dpb_stru *p_H264_Dpb, |
1611 | struct FrameStore *fs, |
1612 | struct StorablePicture *p, |
1613 | unsigned char data_flag) |
1614 | { |
1615 | struct vdec_frames_s *mvfrm = p_H264_Dpb->vdec->mvfrm; |
1616 | struct VideoParameters *p_Vid = &p_H264_Dpb->mVideo; |
1617 | /* InputParameters *p_Inp = p_Vid->p_Inp; |
1618 | * dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1619 | * "insert (%s) pic with frame_num #%d, poc %d\n", |
1620 | * (p->structure == FRAME)?"FRAME": |
1621 | * (p->structure == TOP_FIELD)?"TOP_FIELD": |
1622 | * "BOTTOM_FIELD", p->pic_num, p->poc); |
1623 | * assert (p!=NULL); |
1624 | * assert (fs!=NULL); |
1625 | */ |
1626 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1627 | "%s %p %p\n", __func__, fs, p); |
1628 | p_H264_Dpb->dpb_frame_count++; |
1629 | fs->dpb_frame_count = p_H264_Dpb->dpb_frame_count; |
1630 | #if 1 |
1631 | /* rain */ |
1632 | /* p->buf_spec_num = fs->index; */ |
1633 | p->data_flag = data_flag; |
1634 | fs->data_flag |= data_flag; |
1635 | fs->buf_spec_num = p->buf_spec_num; |
1636 | fs->colocated_buf_index = p->colocated_buf_index; |
1637 | #endif |
1638 | p->slice_type = p_H264_Dpb->mSlice.slice_type; |
1639 | switch (p->structure) { |
1640 | case FRAME: |
1641 | fs->frame = p; |
1642 | fs->is_used = 3; |
1643 | fs->slice_type = p->slice_type; |
1644 | fs->frame_size = p->frame_size; |
1645 | fs->offset_delimiter = p->offset_delimiter; |
1646 | if (p->used_for_reference) { |
1647 | fs->is_reference = 3; |
1648 | fs->is_orig_reference = 3; |
1649 | if (p->is_long_term) { |
1650 | fs->is_long_term = 3; |
1651 | fs->long_term_frame_idx = |
1652 | p->long_term_frame_idx; |
1653 | } |
1654 | } |
1655 | fs->pts = p->pts; |
1656 | fs->pts64 = p->pts64; |
1657 | fs->layer_id = p->layer_id; |
1658 | #if (MVC_EXTENSION_ENABLE) |
1659 | fs->view_id = p->view_id; |
1660 | fs->inter_view_flag[0] = fs->inter_view_flag[1] = |
1661 | p->inter_view_flag; |
1662 | fs->anchor_pic_flag[0] = fs->anchor_pic_flag[1] = |
1663 | p->anchor_pic_flag; |
1664 | #endif |
1665 | /* generate field views */ |
1666 | /* return; */ |
1667 | dpb_split_field(p_H264_Dpb, fs); |
1668 | /* return; */ |
1669 | break; |
1670 | case TOP_FIELD: |
1671 | fs->top_field = p; |
1672 | fs->is_used |= 1; |
1673 | fs->layer_id = p->layer_id; |
1674 | if (fs->frame_size == 0) { |
1675 | fs->slice_type = p->slice_type; |
1676 | // fs->pts = p->pts; |
1677 | // fs->pts64 = p->pts64; |
1678 | } |
1679 | fs->frame_size += p->frame_size; |
1680 | #if (MVC_EXTENSION_ENABLE) |
1681 | fs->view_id = p->view_id; |
1682 | fs->inter_view_flag[0] = p->inter_view_flag; |
1683 | fs->anchor_pic_flag[0] = p->anchor_pic_flag; |
1684 | #endif |
1685 | if (p->used_for_reference) { |
1686 | fs->is_reference |= 1; |
1687 | fs->is_orig_reference |= 1; |
1688 | if (p->is_long_term) { |
1689 | fs->is_long_term |= 1; |
1690 | fs->long_term_frame_idx = |
1691 | p->long_term_frame_idx; |
1692 | } |
1693 | } |
1694 | if (fs->is_used == 3) { |
1695 | /* generate frame view */ |
1696 | dpb_combine_field(p_H264_Dpb, fs); |
1697 | } else { |
1698 | fs->poc = p->poc; |
1699 | } |
1700 | gen_field_ref_ids(p_Vid, p); |
1701 | break; |
1702 | case BOTTOM_FIELD: |
1703 | fs->bottom_field = p; |
1704 | fs->is_used |= 2; |
1705 | fs->layer_id = p->layer_id; |
1706 | if (fs->frame_size == 0) { |
1707 | fs->slice_type = p->slice_type; |
1708 | // fs->pts = p->pts; |
1709 | // fs->pts64 = p->pts64; |
1710 | } |
1711 | fs->frame_size += p->frame_size; |
1712 | #if (MVC_EXTENSION_ENABLE) |
1713 | fs->view_id = p->view_id; |
1714 | fs->inter_view_flag[1] = p->inter_view_flag; |
1715 | fs->anchor_pic_flag[1] = p->anchor_pic_flag; |
1716 | #endif |
1717 | if (p->used_for_reference) { |
1718 | fs->is_reference |= 2; |
1719 | fs->is_orig_reference |= 2; |
1720 | if (p->is_long_term) { |
1721 | fs->is_long_term |= 2; |
1722 | fs->long_term_frame_idx = |
1723 | p->long_term_frame_idx; |
1724 | } |
1725 | } |
1726 | if (fs->is_used == 3) { |
1727 | /* generate frame view */ |
1728 | dpb_combine_field(p_H264_Dpb, fs); |
1729 | } else { |
1730 | fs->poc = p->poc; |
1731 | } |
1732 | gen_field_ref_ids(p_Vid, p); |
1733 | break; |
1734 | } |
1735 | fs->frame_num = p->pic_num; |
1736 | fs->recovery_frame = p->recovery_frame; |
1737 | |
1738 | fs->is_output = p->is_output; |
1739 | fs->pre_output = p->pre_output; |
1740 | |
1741 | /* picture qos infomation*/ |
1742 | fs->max_mv = p->max_mv; |
1743 | fs->avg_mv = p->avg_mv; |
1744 | fs->min_mv = p->min_mv; |
1745 | |
1746 | fs->max_qp = p->max_qp; |
1747 | fs->avg_qp = p->avg_qp; |
1748 | fs->min_qp = p->min_qp; |
1749 | |
1750 | fs->max_skip = p->max_skip; |
1751 | fs->avg_skip = p->avg_skip; |
1752 | fs->min_skip = p->min_skip; |
1753 | |
1754 | if (fs->is_used == 3) { |
1755 | calculate_frame_no(p_Vid, p); |
1756 | #if 0 |
1757 | /* ??? */ |
1758 | if (-1 != p_Vid->p_ref && !p_Inp->silent) |
1759 | find_snr(p_Vid, fs->frame, &p_Vid->p_ref); |
1760 | #endif |
1761 | //fs->pts = p->pts; |
1762 | //fs->pts64 = p->pts64; |
1763 | } |
1764 | fs->timestamp = p->timestamp; |
1765 | if (mvfrm) { |
1766 | fs->frame_size2 = mvfrm->frame_size; |
1767 | fs->hw_decode_time = mvfrm->hw_decode_time; |
1768 | } |
1769 | } |
1770 | |
1771 | void reset_frame_store(struct h264_dpb_stru *p_H264_Dpb, |
1772 | struct FrameStore *f) |
1773 | { |
1774 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1775 | "%s\n", __func__); |
1776 | |
1777 | if (f) { |
1778 | if (f->frame) { |
1779 | free_picture(p_H264_Dpb, f->frame); |
1780 | f->frame = NULL; |
1781 | } |
1782 | if (f->top_field) { |
1783 | free_picture(p_H264_Dpb, f->top_field); |
1784 | f->top_field = NULL; |
1785 | } |
1786 | if (f->bottom_field) { |
1787 | free_picture(p_H264_Dpb, f->bottom_field); |
1788 | f->bottom_field = NULL; |
1789 | } |
1790 | |
1791 | /**/ |
1792 | f->is_used = 0; |
1793 | f->is_reference = 0; |
1794 | f->is_long_term = 0; |
1795 | f->is_orig_reference = 0; |
1796 | |
1797 | f->is_output = 0; |
1798 | f->pre_output = 0; |
1799 | |
1800 | f->frame = NULL; |
1801 | f->top_field = NULL; |
1802 | f->bottom_field = NULL; |
1803 | |
1804 | /* free(f); */ |
1805 | } |
1806 | } |
1807 | |
1808 | void unmark_for_reference(struct DecodedPictureBuffer *p_Dpb, |
1809 | struct FrameStore *fs) |
1810 | { |
1811 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
1812 | struct h264_dpb_stru, mDPB); |
1813 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
1814 | "%s %p %p %p %p\n", __func__, |
1815 | fs, fs->frame, fs->top_field, fs->bottom_field); |
1816 | /* return; */ |
1817 | if (fs->is_used & 1) { |
1818 | if (fs->top_field) |
1819 | fs->top_field->used_for_reference = 0; |
1820 | } |
1821 | if (fs->is_used & 2) { |
1822 | if (fs->bottom_field) |
1823 | fs->bottom_field->used_for_reference = 0; |
1824 | } |
1825 | if (fs->is_used == 3) { |
1826 | if (fs->top_field && fs->bottom_field) { |
1827 | fs->top_field->used_for_reference = 0; |
1828 | fs->bottom_field->used_for_reference = 0; |
1829 | } |
1830 | fs->frame->used_for_reference = 0; |
1831 | } |
1832 | |
1833 | fs->is_reference = 0; |
1834 | |
1835 | } |
1836 | |
1837 | int get_long_term_flag_by_buf_spec_num(struct h264_dpb_stru *p_H264_Dpb, |
1838 | int buf_spec_num) |
1839 | { |
1840 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
1841 | unsigned int i; |
1842 | |
1843 | for (i = 0; i < p_Dpb->used_size; i++) { |
1844 | if (p_Dpb->fs[i]->buf_spec_num == buf_spec_num) |
1845 | return p_Dpb->fs[i]->is_long_term; |
1846 | } |
1847 | return -1; |
1848 | } |
1849 | |
1850 | static void update_pic_num(struct h264_dpb_stru *p_H264_Dpb) |
1851 | { |
1852 | unsigned int i; |
1853 | struct Slice *currSlice = &p_H264_Dpb->mSlice; |
1854 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
1855 | struct DecodedPictureBuffer *p_Dpb = currSlice->p_Dpb; |
1856 | struct SPSParameters *active_sps = p_Vid->active_sps; |
1857 | int add_top = 0, add_bottom = 0; |
1858 | int max_frame_num = 1 << (active_sps->log2_max_frame_num_minus4 + 4); |
1859 | |
1860 | if (currSlice->structure == FRAME) { |
1861 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
1862 | #ifdef ERROR_CHECK |
1863 | if (p_Dpb->fs_ref[i] == NULL || |
1864 | p_Dpb->fs_ref[i]->frame == NULL) { |
1865 | p_H264_Dpb->dpb_error_flag = __LINE__; |
1866 | continue; |
1867 | } |
1868 | #endif |
1869 | if (p_Dpb->fs_ref[i]->is_used == 3) { |
1870 | if ((p_Dpb->fs_ref[i]->frame-> |
1871 | used_for_reference) && |
1872 | (!p_Dpb->fs_ref[i]->frame-> |
1873 | is_long_term)) { |
1874 | if (p_Dpb->fs_ref[i]->frame_num > |
1875 | currSlice->frame_num) { |
1876 | p_Dpb->fs_ref[i]-> |
1877 | frame_num_wrap = |
1878 | p_Dpb->fs_ref[i]->frame_num |
1879 | - max_frame_num; |
1880 | } else { |
1881 | p_Dpb->fs_ref[i]-> |
1882 | frame_num_wrap = |
1883 | p_Dpb->fs_ref[i]->frame_num; |
1884 | } |
1885 | p_Dpb->fs_ref[i]->frame->pic_num = |
1886 | p_Dpb->fs_ref[i]->frame_num_wrap; |
1887 | } |
1888 | } |
1889 | } |
1890 | /* update long_term_pic_num */ |
1891 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
1892 | #ifdef ERROR_CHECK |
1893 | if (p_Dpb->fs_ltref[i] == NULL || |
1894 | p_Dpb->fs_ltref[i]->frame == NULL) { |
1895 | p_H264_Dpb->dpb_error_flag = __LINE__; |
1896 | continue; |
1897 | } |
1898 | #endif |
1899 | if (p_Dpb->fs_ltref[i]->is_used == 3) { |
1900 | if (p_Dpb->fs_ltref[i]->frame->is_long_term) { |
1901 | p_Dpb->fs_ltref[i]->frame-> |
1902 | long_term_pic_num = |
1903 | p_Dpb->fs_ltref[i]->frame-> |
1904 | long_term_frame_idx; |
1905 | } |
1906 | } |
1907 | } |
1908 | } else { |
1909 | if (currSlice->structure == TOP_FIELD) { |
1910 | add_top = 1; |
1911 | add_bottom = 0; |
1912 | } else { |
1913 | add_top = 0; |
1914 | add_bottom = 1; |
1915 | } |
1916 | |
1917 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
1918 | #ifdef ERROR_CHECK |
1919 | if (p_Dpb->fs_ref[i] == NULL) { |
1920 | p_H264_Dpb->dpb_error_flag = __LINE__; |
1921 | continue; |
1922 | } |
1923 | #endif |
1924 | if (p_Dpb->fs_ref[i]->is_reference) { |
1925 | if (p_Dpb->fs_ref[i]->frame_num > currSlice-> |
1926 | frame_num) { |
1927 | p_Dpb->fs_ref[i]->frame_num_wrap = |
1928 | p_Dpb->fs_ref[i]->frame_num - |
1929 | max_frame_num; |
1930 | } else { |
1931 | p_Dpb->fs_ref[i]->frame_num_wrap = |
1932 | p_Dpb->fs_ref[i]->frame_num; |
1933 | } |
1934 | if (p_Dpb->fs_ref[i]->is_reference & 1) { |
1935 | #ifdef ERROR_CHECK |
1936 | if (p_Dpb->fs_ref[i]->top_field |
1937 | == NULL) { |
1938 | p_H264_Dpb->dpb_error_flag = |
1939 | __LINE__; |
1940 | continue; |
1941 | } |
1942 | #endif |
1943 | p_Dpb->fs_ref[i]->top_field-> |
1944 | pic_num = (2 * p_Dpb->fs_ref[i]-> |
1945 | frame_num_wrap) + add_top; |
1946 | } |
1947 | if (p_Dpb->fs_ref[i]->is_reference & 2) { |
1948 | #ifdef ERROR_CHECK |
1949 | if (p_Dpb->fs_ref[i]->bottom_field |
1950 | == NULL) { |
1951 | p_H264_Dpb->dpb_error_flag = |
1952 | __LINE__; |
1953 | continue; |
1954 | } |
1955 | #endif |
1956 | p_Dpb->fs_ref[i]->bottom_field-> |
1957 | pic_num = (2 * p_Dpb->fs_ref[i]-> |
1958 | frame_num_wrap) + add_bottom; |
1959 | } |
1960 | } |
1961 | } |
1962 | /* update long_term_pic_num */ |
1963 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
1964 | #ifdef ERROR_CHECK |
1965 | if (p_Dpb->fs_ltref[i] == NULL) { |
1966 | p_H264_Dpb->dpb_error_flag = __LINE__; |
1967 | continue; |
1968 | } |
1969 | #endif |
1970 | if (p_Dpb->fs_ltref[i]->is_long_term & 1) { |
1971 | #ifdef ERROR_CHECK |
1972 | if (p_Dpb->fs_ltref[i]->top_field == NULL) { |
1973 | p_H264_Dpb->dpb_error_flag = __LINE__; |
1974 | continue; |
1975 | } |
1976 | #endif |
1977 | p_Dpb->fs_ltref[i]->top_field-> |
1978 | long_term_pic_num = 2 * |
1979 | p_Dpb->fs_ltref[i]->top_field-> |
1980 | long_term_frame_idx + add_top; |
1981 | } |
1982 | if (p_Dpb->fs_ltref[i]->is_long_term & 2) { |
1983 | #ifdef ERROR_CHECK |
1984 | if (p_Dpb->fs_ltref[i]->bottom_field == NULL) { |
1985 | p_H264_Dpb->dpb_error_flag = __LINE__; |
1986 | continue; |
1987 | } |
1988 | #endif |
1989 | p_Dpb->fs_ltref[i]->bottom_field-> |
1990 | long_term_pic_num = 2 * |
1991 | p_Dpb->fs_ltref[i]->bottom_field-> |
1992 | long_term_frame_idx + add_bottom; |
1993 | } |
1994 | } |
1995 | } |
1996 | } |
1997 | |
1998 | static void remove_frame_from_dpb(struct h264_dpb_stru *p_H264_Dpb, int pos) |
1999 | { |
2000 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2001 | struct FrameStore *fs = p_Dpb->fs[pos]; |
2002 | struct FrameStore *tmp; |
2003 | unsigned int i; |
2004 | |
2005 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2006 | "%s pos %d %p\n", __func__, pos, fs); |
2007 | |
2008 | /* dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2009 | * "remove frame with frame_num #%d\n", fs->frame_num); |
2010 | */ |
2011 | switch (fs->is_used) { |
2012 | case 3: |
2013 | free_picture(p_H264_Dpb, fs->frame); |
2014 | free_picture(p_H264_Dpb, fs->top_field); |
2015 | free_picture(p_H264_Dpb, fs->bottom_field); |
2016 | fs->frame = NULL; |
2017 | fs->top_field = NULL; |
2018 | fs->bottom_field = NULL; |
2019 | break; |
2020 | case 2: |
2021 | free_picture(p_H264_Dpb, fs->bottom_field); |
2022 | fs->bottom_field = NULL; |
2023 | break; |
2024 | case 1: |
2025 | free_picture(p_H264_Dpb, fs->top_field); |
2026 | fs->top_field = NULL; |
2027 | break; |
2028 | case 0: |
2029 | break; |
2030 | default: |
2031 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2032 | "invalid frame store type %x", 500); |
2033 | } |
2034 | fs->data_flag = 0; |
2035 | fs->is_used = 0; |
2036 | fs->is_long_term = 0; |
2037 | fs->is_reference = 0; |
2038 | fs->is_orig_reference = 0; |
2039 | fs->frame_size = 0; |
2040 | /* move empty framestore to end of buffer */ |
2041 | tmp = p_Dpb->fs[pos]; |
2042 | |
2043 | for (i = pos; i < p_Dpb->used_size - 1; i++) |
2044 | p_Dpb->fs[i] = p_Dpb->fs[i + 1]; |
2045 | p_Dpb->fs[p_Dpb->used_size - 1] = tmp; |
2046 | |
2047 | if (p_Dpb->used_size) |
2048 | p_Dpb->used_size--; |
2049 | } |
2050 | |
2051 | static int is_used_for_reference(struct FrameStore *fs) |
2052 | { |
2053 | if (fs->is_reference) |
2054 | return 1; |
2055 | |
2056 | if (fs->is_used == 3) { /* frame */ |
2057 | if (fs->frame->used_for_reference) |
2058 | return 1; |
2059 | } |
2060 | |
2061 | if (fs->is_used & 1) { /* top field */ |
2062 | if (fs->top_field) { |
2063 | if (fs->top_field->used_for_reference) |
2064 | return 1; |
2065 | } |
2066 | } |
2067 | |
2068 | if (fs->is_used & 2) { /* bottom field */ |
2069 | if (fs->bottom_field) { |
2070 | if (fs->bottom_field->used_for_reference) |
2071 | return 1; |
2072 | } |
2073 | } |
2074 | return 0; |
2075 | } |
2076 | |
2077 | static int remove_unused_frame_from_dpb(struct h264_dpb_stru *p_H264_Dpb) |
2078 | { |
2079 | unsigned int i; |
2080 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2081 | /* check for frames that were already output and no longer |
2082 | * used for reference |
2083 | */ |
2084 | for (i = 0; i < p_Dpb->used_size; i++) { |
2085 | if ((!is_used_for_reference(p_Dpb->fs[i])) && |
2086 | (p_Dpb->fs[i]->colocated_buf_index >= 0)) { |
2087 | dpb_print(p_H264_Dpb->decoder_index, |
2088 | PRINT_FLAG_DPB_DETAIL, |
2089 | "release_colocate_buf[%d] for fs[%d]\n", |
2090 | p_Dpb->fs[i]->colocated_buf_index, i); |
2091 | |
2092 | release_colocate_buf(p_H264_Dpb, |
2093 | p_Dpb->fs[i]->colocated_buf_index); /* rain */ |
2094 | p_Dpb->fs[i]->colocated_buf_index = -1; |
2095 | } |
2096 | } |
2097 | |
2098 | for (i = 0; i < p_Dpb->used_size; i++) { |
2099 | if (p_Dpb->fs[i]->is_output && |
2100 | (!is_used_for_reference(p_Dpb->fs[i]))) { |
2101 | release_buf_spec_num(p_H264_Dpb->vdec, |
2102 | p_Dpb->fs[i]->buf_spec_num); |
2103 | p_Dpb->fs[i]->buf_spec_num = -1; |
2104 | remove_frame_from_dpb(p_H264_Dpb, i); |
2105 | |
2106 | dpb_print(p_H264_Dpb->decoder_index, |
2107 | PRINT_FLAG_DPB_DETAIL, "%s[%d]\n", |
2108 | __func__, i); |
2109 | |
2110 | return 1; |
2111 | } |
2112 | } |
2113 | return 0; |
2114 | } |
2115 | |
2116 | static int unmark_one_error_out_frame(struct h264_dpb_stru *p_H264_Dpb) |
2117 | { |
2118 | int ret = 0; |
2119 | unsigned i; |
2120 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2121 | |
2122 | for (i = 0; i < p_Dpb->used_size; i++) { |
2123 | if (p_Dpb->fs[i]->is_output && |
2124 | ((p_Dpb->fs[i]->data_flag & ERROR_FLAG) || |
2125 | (p_Dpb->fs[i]->data_flag & NULL_FLAG)) |
2126 | ) { |
2127 | unmark_for_reference(p_Dpb, p_Dpb->fs[i]); |
2128 | |
2129 | ret = 1; |
2130 | break; |
2131 | } |
2132 | } |
2133 | return ret; |
2134 | } |
2135 | |
2136 | static int unmark_one_out_frame(struct h264_dpb_stru *p_H264_Dpb) |
2137 | { |
2138 | int ret = 0; |
2139 | unsigned i; |
2140 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2141 | |
2142 | for (i = 0; i < p_Dpb->used_size; i++) { |
2143 | if (p_Dpb->fs[i]->is_output) { |
2144 | unmark_for_reference(p_Dpb, p_Dpb->fs[i]); |
2145 | |
2146 | ret = 1; |
2147 | } |
2148 | } |
2149 | return ret; |
2150 | } |
2151 | /* |
2152 | force_flag, |
2153 | 1, remove one error buf (is_out is 1) if there is no un-used buf |
2154 | 2, remove one buf (is_out is 1) if there is no un-used buf |
2155 | */ |
2156 | void bufmgr_h264_remove_unused_frame(struct h264_dpb_stru *p_H264_Dpb, |
2157 | u8 force_flag) |
2158 | { |
2159 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2160 | int ret = 0; |
2161 | unsigned char removed_flag = 0; |
2162 | do { |
2163 | ret = remove_unused_frame_from_dpb(p_H264_Dpb); |
2164 | if (ret != 0) |
2165 | removed_flag = 1; |
2166 | } while (ret != 0); |
2167 | if (removed_flag) { |
2168 | dpb_print(p_H264_Dpb->decoder_index, |
2169 | PRINT_FLAG_DPB_DETAIL, "%s\r\n", __func__); |
2170 | dump_dpb(p_Dpb, 0); |
2171 | } else if (force_flag == 2) { |
2172 | if (unmark_one_out_frame(p_H264_Dpb)) { |
2173 | dpb_print(p_H264_Dpb->decoder_index, |
2174 | 0, "%s, Warnning, force unmark one frame\r\n", |
2175 | __func__); |
2176 | update_ref_list(p_Dpb); |
2177 | remove_unused_frame_from_dpb(p_H264_Dpb); |
2178 | dump_dpb(p_Dpb, 0); |
2179 | } |
2180 | } else if (force_flag == 1) { |
2181 | if (unmark_one_error_out_frame(p_H264_Dpb)) { |
2182 | dpb_print(p_H264_Dpb->decoder_index, |
2183 | 0, "%s, unmark error frame\r\n", |
2184 | __func__); |
2185 | update_ref_list(p_Dpb); |
2186 | remove_unused_frame_from_dpb(p_H264_Dpb); |
2187 | dump_dpb(p_Dpb, 0); |
2188 | } |
2189 | } |
2190 | } |
2191 | |
2192 | #ifdef OUTPUT_BUFFER_IN_C |
2193 | int is_there_unused_frame_from_dpb(struct DecodedPictureBuffer *p_Dpb) |
2194 | { |
2195 | unsigned int i; |
2196 | |
2197 | /* check for frames that were already output and no longer |
2198 | * used for reference |
2199 | */ |
2200 | for (i = 0; i < p_Dpb->used_size; i++) { |
2201 | if (p_Dpb->fs[i]->is_output && |
2202 | (!is_used_for_reference(p_Dpb->fs[i]))) { |
2203 | return 1; |
2204 | } |
2205 | } |
2206 | return 0; |
2207 | } |
2208 | #endif |
2209 | |
2210 | static void get_smallest_poc(struct DecodedPictureBuffer *p_Dpb, int *poc, |
2211 | int *pos) |
2212 | { |
2213 | unsigned int i; |
2214 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
2215 | struct h264_dpb_stru, mDPB); |
2216 | dpb_print(p_H264_Dpb->decoder_index, |
2217 | PRINT_FLAG_DPB_DETAIL, "%s\n", __func__); |
2218 | if (p_Dpb->used_size < 1) { |
2219 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2220 | "Cannot determine smallest POC, DPB empty. %d\n", |
2221 | 150); |
2222 | } |
2223 | |
2224 | *pos = -1; |
2225 | *poc = INT_MAX; |
2226 | for (i = 0; i < p_Dpb->used_size; i++) { |
2227 | #ifdef OUTPUT_BUFFER_IN_C |
2228 | /* rain */ |
2229 | if ((*poc > p_Dpb->fs[i]->poc) && |
2230 | (!p_Dpb->fs[i]->is_output) && |
2231 | (!p_Dpb->fs[i]->pre_output)) { |
2232 | #else |
2233 | if ((*poc > p_Dpb->fs[i]->poc) && (!p_Dpb->fs[i]->is_output)) { |
2234 | #endif |
2235 | *poc = p_Dpb->fs[i]->poc; |
2236 | *pos = i; |
2237 | } |
2238 | } |
2239 | } |
2240 | |
2241 | int output_frames(struct h264_dpb_stru *p_H264_Dpb, unsigned char flush_flag) |
2242 | { |
2243 | int poc, pos; |
2244 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2245 | int i; |
2246 | int none_displayed_num = 0; |
2247 | unsigned char fast_output_flag = 0; |
2248 | if (!flush_flag) { |
2249 | for (i = 0; i < p_Dpb->used_size; i++) { |
2250 | if ((!p_Dpb->fs[i]->is_output) && |
2251 | (!p_Dpb->fs[i]->pre_output) &&((p_Dpb->fs[i]->is_used == 3 |
2252 | ||p_Dpb->fs[i]->data_flag & ERROR_FLAG ))) { |
2253 | none_displayed_num++; |
2254 | if ((p_H264_Dpb->first_insert_frame == FirstInsertFrm_IDLE || |
2255 | p_H264_Dpb->first_insert_frame == FirstInsertFrm_RESET) |
2256 | && (p_Dpb->fs[i]->is_used == 3) |
2257 | && (p_Dpb->last_output_poc == INT_MIN)) { |
2258 | if (p_H264_Dpb->first_insert_frame == FirstInsertFrm_IDLE) |
2259 | fast_output_flag = 1; |
2260 | p_H264_Dpb->first_insert_frame = FirstInsertFrm_OUT; |
2261 | p_H264_Dpb->first_output_poc = p_Dpb->fs[i]->poc; |
2262 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2263 | "%s first insert frame i %d poc %d frame_num %x\n", |
2264 | __func__, i, p_Dpb->fs[i]->poc, p_Dpb->fs[i]->frame_num); |
2265 | } |
2266 | |
2267 | /*check poc even/odd*/ |
2268 | if (p_H264_Dpb->poc_even_odd_flag == 0 && |
2269 | p_H264_Dpb->decode_pic_count >= 3) |
2270 | p_H264_Dpb->poc_even_odd_flag = 2; |
2271 | if (p_Dpb->fs[i]->poc & 0x1) |
2272 | p_H264_Dpb->poc_even_odd_flag = 1; |
2273 | /**/ |
2274 | |
2275 | if ((p_H264_Dpb->fast_output_enable & 0x1) && |
2276 | (p_Dpb->fs[i]->data_flag & IDR_FLAG)) |
2277 | fast_output_flag = 1; |
2278 | if ((p_H264_Dpb->fast_output_enable & 0x2) && |
2279 | ((p_Dpb->fs[i]->poc - |
2280 | p_Dpb->last_output_poc) |
2281 | == 1)) |
2282 | fast_output_flag = 1; |
2283 | if ((p_H264_Dpb->fast_output_enable & 0x4) && |
2284 | (p_H264_Dpb->poc_even_odd_flag == 2) && |
2285 | (p_Dpb->fs[i]->is_used == 3) && |
2286 | ((p_Dpb->fs[i]->poc - |
2287 | p_Dpb->last_output_poc) |
2288 | == 2)) |
2289 | fast_output_flag = 1; |
2290 | } |
2291 | } |
2292 | if (fast_output_flag) |
2293 | ; |
2294 | else if (none_displayed_num < |
2295 | p_H264_Dpb->origin_max_reference) |
2296 | return 0; |
2297 | } |
2298 | |
2299 | get_smallest_poc(p_Dpb, &poc, &pos); |
2300 | |
2301 | if (pos == -1) |
2302 | return 0; |
2303 | #if 0 |
2304 | if (is_used_for_reference(p_Dpb->fs[pos])) |
2305 | return 0; |
2306 | #endif |
2307 | if (p_H264_Dpb->first_insert_frame == FirstInsertFrm_OUT) { |
2308 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2309 | "%s pos %d pos->poc %d first_output_poc %d \n", |
2310 | __func__, pos, p_Dpb->fs[pos]->poc, p_H264_Dpb->first_output_poc); |
2311 | |
2312 | if (p_Dpb->fs[pos]->poc < p_H264_Dpb->first_output_poc) |
2313 | p_Dpb->fs[pos]->data_flag |= NODISP_FLAG; |
2314 | else if (p_Dpb->last_output_poc != INT_MIN) |
2315 | p_H264_Dpb->first_insert_frame = FirstInsertFrm_SKIPDONE; |
2316 | |
2317 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2318 | "%s first_insert_frame %d \n", __func__, p_H264_Dpb->first_insert_frame); |
2319 | } |
2320 | if (prepare_display_buf(p_H264_Dpb->vdec, p_Dpb->fs[pos]) >= 0) |
2321 | p_Dpb->fs[pos]->pre_output = 1; |
2322 | else { |
2323 | if (h264_debug_flag & PRINT_FLAG_DPB_DETAIL) { |
2324 | dpb_print(p_H264_Dpb->decoder_index, 0, |
2325 | "%s[%d] poc:%d last_output_poc:%d poc_even_odd_flag:%d\n", |
2326 | __func__, pos, poc, |
2327 | p_Dpb->last_output_poc, |
2328 | p_H264_Dpb->poc_even_odd_flag); |
2329 | dump_dpb(p_Dpb, 1); |
2330 | } |
2331 | return 0; |
2332 | } |
2333 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2334 | "%s[%d] poc %d last_output_poc %d poc_even_odd_flag %d\n", |
2335 | __func__, pos, poc, |
2336 | p_Dpb->last_output_poc, |
2337 | p_H264_Dpb->poc_even_odd_flag); |
2338 | |
2339 | p_Dpb->last_output_poc = poc; |
2340 | return 1; |
2341 | |
2342 | } |
2343 | |
2344 | |
2345 | void flush_dpb(struct h264_dpb_stru *p_H264_Dpb) |
2346 | { |
2347 | /* struct VideoParameters *p_Vid = p_Dpb->p_Vid; */ |
2348 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2349 | unsigned int i; |
2350 | |
2351 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2352 | "%s\n", __func__); |
2353 | |
2354 | /* diagnostics */ |
2355 | /* dpb_print(p_H264_Dpb->decoder_index, |
2356 | *PRINT_FLAG_DPB_DETAIL, |
2357 | *"Flush remaining frames from the dpb." |
2358 | *"p_Dpb->size = %d, p_Dpb->used_size = %d\n", |
2359 | *p_Dpb->size, p_Dpb->used_size); |
2360 | */ |
2361 | |
2362 | if (!p_Dpb->init_done) |
2363 | return; |
2364 | /* if(p_Vid->conceal_mode == 0) */ |
2365 | #if 0 |
2366 | /* ??? */ |
2367 | if (p_Vid->conceal_mode != 0) |
2368 | conceal_non_ref_pics(p_Dpb, 0); |
2369 | #endif |
2370 | /* mark all frames unused */ |
2371 | for (i = 0; i < p_Dpb->used_size; i++) { |
2372 | #if MVC_EXTENSION_ENABLE |
2373 | assert(p_Dpb->fs[i]->view_id == p_Dpb->layer_id); |
2374 | #endif |
2375 | unmark_for_reference(p_Dpb, p_Dpb->fs[i]); |
2376 | |
2377 | } |
2378 | |
2379 | while (remove_unused_frame_from_dpb(p_H264_Dpb)) |
2380 | ; |
2381 | |
2382 | /* output frames in POC order */ |
2383 | while (output_frames(p_H264_Dpb, 1)) |
2384 | ; |
2385 | |
2386 | |
2387 | p_Dpb->last_output_poc = INT_MIN; |
2388 | } |
2389 | |
2390 | static int is_short_term_reference(struct DecodedPictureBuffer *p_Dpb, |
2391 | struct FrameStore *fs) |
2392 | { |
2393 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
2394 | struct h264_dpb_stru, mDPB); |
2395 | if (fs->is_used == 3) { /* frame */ |
2396 | if ((fs->frame->used_for_reference) && |
2397 | (!fs->frame->is_long_term)) { |
2398 | dpb_print(p_H264_Dpb->decoder_index, |
2399 | PRINT_FLAG_DPB_DETAIL, "[[%s 1]]", |
2400 | __func__); |
2401 | return 1; |
2402 | } |
2403 | } |
2404 | |
2405 | if (fs->is_used & 1) { /* top field */ |
2406 | if (fs->top_field) { |
2407 | if ((fs->top_field->used_for_reference) && |
2408 | (!fs->top_field->is_long_term)) { |
2409 | dpb_print(p_H264_Dpb->decoder_index, |
2410 | PRINT_FLAG_DPB_DETAIL, "[[%s 2]]", |
2411 | __func__); |
2412 | return 1; |
2413 | } |
2414 | } |
2415 | } |
2416 | |
2417 | if (fs->is_used & 2) { /* bottom field */ |
2418 | if (fs->bottom_field) { |
2419 | if ((fs->bottom_field->used_for_reference) && |
2420 | (!fs->bottom_field->is_long_term)) { |
2421 | dpb_print(p_H264_Dpb->decoder_index, |
2422 | PRINT_FLAG_DPB_DETAIL, "[[%s 3]]", |
2423 | __func__); |
2424 | return 1; |
2425 | } |
2426 | } |
2427 | } |
2428 | return 0; |
2429 | } |
2430 | |
2431 | static int is_long_term_reference(struct FrameStore *fs) |
2432 | { |
2433 | |
2434 | if (fs->is_used == 3) { /* frame */ |
2435 | if ((fs->frame->used_for_reference) && |
2436 | (fs->frame->is_long_term)) { |
2437 | return 1; |
2438 | } |
2439 | } |
2440 | |
2441 | if (fs->is_used & 1) { /* top field */ |
2442 | if (fs->top_field) { |
2443 | if ((fs->top_field->used_for_reference) && |
2444 | (fs->top_field->is_long_term)) { |
2445 | return 1; |
2446 | } |
2447 | } |
2448 | } |
2449 | |
2450 | if (fs->is_used & 2) { /* bottom field */ |
2451 | if (fs->bottom_field) { |
2452 | if ((fs->bottom_field->used_for_reference) && |
2453 | (fs->bottom_field->is_long_term)) { |
2454 | return 1; |
2455 | } |
2456 | } |
2457 | } |
2458 | return 0; |
2459 | } |
2460 | |
2461 | static void update_ref_list(struct DecodedPictureBuffer *p_Dpb) |
2462 | { |
2463 | unsigned int i, j; |
2464 | |
2465 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
2466 | struct h264_dpb_stru, mDPB); |
2467 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2468 | "%s (%d, %d)\n", __func__, p_Dpb->size, p_Dpb->used_size); |
2469 | for (i = 0, j = 0; i < p_Dpb->used_size; i++) { |
2470 | #if 1 |
2471 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2472 | "fs[%d]: fs %p frame %p is_reference %d %d %d\n", |
2473 | i, p_Dpb->fs[i], p_Dpb->fs[i]->frame, |
2474 | p_Dpb->fs[i]->frame != NULL ? |
2475 | p_Dpb->fs[i]->frame->used_for_reference : 0, |
2476 | p_Dpb->fs[i]->top_field != NULL ? |
2477 | p_Dpb->fs[i]->top_field->used_for_reference : |
2478 | 0, |
2479 | p_Dpb->fs[i]->bottom_field != NULL ? |
2480 | p_Dpb->fs[i]->bottom_field->used_for_reference : 0); |
2481 | #endif |
2482 | if (is_short_term_reference(p_Dpb, p_Dpb->fs[i])) { |
2483 | dpb_print(p_H264_Dpb->decoder_index, |
2484 | PRINT_FLAG_DPB_DETAIL, |
2485 | "fs_ref[%d]=fs[%d]: fs %p\n", j, i, p_Dpb->fs[i]); |
2486 | p_Dpb->fs_ref[j++] = p_Dpb->fs[i]; |
2487 | } |
2488 | } |
2489 | |
2490 | p_Dpb->ref_frames_in_buffer = j; |
2491 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2492 | "%s dpb size is %d, %d\n", __func__, p_Dpb->size, j); |
2493 | while (j < p_Dpb->size) { |
2494 | /* dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2495 | *"fs_ref[%d]=null\n", j); |
2496 | */ |
2497 | p_Dpb->fs_ref[j++] = NULL; |
2498 | } |
2499 | #ifdef ERROR_CHECK |
2500 | for (i = 0; i < DPB_SIZE_MAX; i++) { |
2501 | if (p_Dpb->fs_ref[i] == NULL) |
2502 | p_Dpb->fs_ref[i] = &dummy_fs; |
2503 | } |
2504 | #endif |
2505 | } |
2506 | |
2507 | static void update_ltref_list(struct DecodedPictureBuffer *p_Dpb) |
2508 | { |
2509 | unsigned int i, j; |
2510 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
2511 | struct h264_dpb_stru, mDPB); |
2512 | |
2513 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2514 | "%s\n", __func__); |
2515 | for (i = 0, j = 0; i < p_Dpb->used_size; i++) { |
2516 | if (is_long_term_reference(p_Dpb->fs[i])) |
2517 | p_Dpb->fs_ltref[j++] = p_Dpb->fs[i]; |
2518 | } |
2519 | |
2520 | p_Dpb->ltref_frames_in_buffer = j; |
2521 | |
2522 | while (j < p_Dpb->size) |
2523 | p_Dpb->fs_ltref[j++] = NULL; |
2524 | #ifdef ERROR_CHECK |
2525 | for (i = 0; i < DPB_SIZE_MAX; i++) { |
2526 | if (p_Dpb->fs_ltref[i] == NULL) |
2527 | p_Dpb->fs_ltref[i] = &dummy_fs; |
2528 | } |
2529 | #endif |
2530 | } |
2531 | |
2532 | static void idr_memory_management(struct h264_dpb_stru *p_H264_Dpb, |
2533 | struct StorablePicture *p) |
2534 | { |
2535 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
2536 | |
2537 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2538 | "%s ref_frames_in_buffer %d ltref_frames_in_buffer %d\n", |
2539 | __func__, p_Dpb->ref_frames_in_buffer, |
2540 | p_Dpb->ltref_frames_in_buffer); |
2541 | |
2542 | |
2543 | if (p->no_output_of_prior_pics_flag) { |
2544 | #if 0 |
2545 | /*???*/ |
2546 | /* free all stored pictures */ |
2547 | int i; |
2548 | |
2549 | for (i = 0; i < p_Dpb->used_size; i++) { |
2550 | /* reset all reference settings |
2551 | * free_frame_store(p_Dpb->fs[i]); |
2552 | * p_Dpb->fs[i] = alloc_frame_store(); |
2553 | */ |
2554 | reset_frame_store(p_H264_Dpb, p_Dpb->fs[i]); /* ??? */ |
2555 | } |
2556 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) |
2557 | p_Dpb->fs_ref[i] = NULL; |
2558 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) |
2559 | p_Dpb->fs_ltref[i] = NULL; |
2560 | p_Dpb->used_size = 0; |
2561 | #endif |
2562 | } else { |
2563 | flush_dpb(p_H264_Dpb); |
2564 | } |
2565 | p_Dpb->last_picture = NULL; |
2566 | |
2567 | update_ref_list(p_Dpb); |
2568 | update_ltref_list(p_Dpb); |
2569 | p_Dpb->last_output_poc = INT_MIN; |
2570 | |
2571 | if (p->long_term_reference_flag) { |
2572 | p_Dpb->max_long_term_pic_idx = 0; |
2573 | p->is_long_term = 1; |
2574 | p->long_term_frame_idx = 0; |
2575 | } else { |
2576 | p_Dpb->max_long_term_pic_idx = -1; |
2577 | p->is_long_term = 0; |
2578 | } |
2579 | |
2580 | #if (MVC_EXTENSION_ENABLE) |
2581 | p_Dpb->last_output_view_id = -1; |
2582 | #endif |
2583 | |
2584 | } |
2585 | |
2586 | static void sliding_window_memory_management( |
2587 | struct DecodedPictureBuffer *p_Dpb, |
2588 | struct StorablePicture *p) |
2589 | { |
2590 | unsigned int i; |
2591 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
2592 | struct h264_dpb_stru, mDPB); |
2593 | unsigned char slide_flag = 0; |
2594 | unsigned int sliding_margin = imax( |
2595 | 1, p_Dpb->num_ref_frames) - p_Dpb->ltref_frames_in_buffer; |
2596 | /* assert (!p->idr_flag); */ |
2597 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2598 | "%s ref_frames_in_buffer %d ltref_frames_in_buffer %d\n", |
2599 | __func__, p_Dpb->ref_frames_in_buffer, |
2600 | p_Dpb->ltref_frames_in_buffer); |
2601 | /* if this is a reference pic with sliding window, |
2602 | unmark first ref frame */ |
2603 | if (p_Dpb->ref_frames_in_buffer == sliding_margin) |
2604 | slide_flag = 1; |
2605 | /*else if ((h264_error_proc_policy & 0x8) && |
2606 | (p_Dpb->ref_frames_in_buffer > sliding_margin)) |
2607 | slide_flag = 1;*/ |
2608 | |
2609 | if (slide_flag) { |
2610 | for (i = 0; i < p_Dpb->used_size; i++) { |
2611 | if (p_Dpb->fs[i]->is_reference && |
2612 | (!(p_Dpb->fs[i]->is_long_term))) { |
2613 | dpb_print(p_H264_Dpb->decoder_index, |
2614 | PRINT_FLAG_DPB_DETAIL, "unmark %d\n", i); |
2615 | unmark_for_reference(p_Dpb, p_Dpb->fs[i]); |
2616 | update_ref_list(p_Dpb); |
2617 | break; |
2618 | } |
2619 | } |
2620 | } |
2621 | |
2622 | p->is_long_term = 0; |
2623 | } |
2624 | |
2625 | static void check_num_ref(struct DecodedPictureBuffer *p_Dpb) |
2626 | { |
2627 | if ((int)(p_Dpb->ltref_frames_in_buffer + |
2628 | p_Dpb->ref_frames_in_buffer) > |
2629 | imax(1, p_Dpb->num_ref_frames)) { |
2630 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
2631 | struct h264_dpb_stru, mDPB); |
2632 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
2633 | "Max. number of reference frames exceeded. Invalid stream. lt %d ref %d mum_ref %d\n", |
2634 | p_Dpb->ltref_frames_in_buffer, |
2635 | p_Dpb->ref_frames_in_buffer, |
2636 | p_Dpb->num_ref_frames); |
2637 | } |
2638 | } |
2639 | |
2640 | void dump_dpb(struct DecodedPictureBuffer *p_Dpb, u8 force) |
2641 | { |
2642 | unsigned i; |
2643 | struct h264_dpb_stru *p_H264_Dpb = |
2644 | container_of(p_Dpb, struct h264_dpb_stru, mDPB); |
2645 | if ((h264_debug_flag & PRINT_FLAG_DUMP_DPB) == 0 && |
2646 | force == 0) |
2647 | return; |
2648 | for (i = 0; i < p_Dpb->used_size; i++) { |
2649 | dpb_print(p_H264_Dpb->decoder_index, |
2650 | 0, |
2651 | "("); |
2652 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2653 | 0, |
2654 | "fn=%d is_used %d ", |
2655 | p_Dpb->fs[i]->frame_num, |
2656 | p_Dpb->fs[i]->is_used); |
2657 | if (p_Dpb->fs[i]->is_used & 1) { |
2658 | if (p_Dpb->fs[i]->top_field) |
2659 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2660 | 0, |
2661 | "T: poc=%d pic_num=%d ", |
2662 | p_Dpb->fs[i]->top_field->poc, |
2663 | p_Dpb->fs[i]->top_field->pic_num); |
2664 | else |
2665 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2666 | 0, |
2667 | "T: poc=%d ", |
2668 | p_Dpb->fs[i]->frame->top_poc); |
2669 | } |
2670 | if (p_Dpb->fs[i]->is_used & 2) { |
2671 | if (p_Dpb->fs[i]->bottom_field) |
2672 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2673 | 0, |
2674 | "B: poc=%d pic_num=%d ", |
2675 | p_Dpb->fs[i]->bottom_field->poc, |
2676 | p_Dpb->fs[i]->bottom_field->pic_num); |
2677 | else |
2678 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2679 | 0, |
2680 | "B: poc=%d ", |
2681 | p_Dpb->fs[i]->frame->bottom_poc); |
2682 | } |
2683 | if (p_Dpb->fs[i]->is_used == 3) |
2684 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2685 | 0, |
2686 | "F: poc=%d pic_num=%d ", |
2687 | p_Dpb->fs[i]->frame->poc, |
2688 | p_Dpb->fs[i]->frame->pic_num); |
2689 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2690 | 0, |
2691 | "G: poc=%d) ", p_Dpb->fs[i]->poc); |
2692 | if (p_Dpb->fs[i]->is_reference) |
2693 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2694 | 0, |
2695 | "ref (%d) ", p_Dpb->fs[i]->is_reference); |
2696 | if (p_Dpb->fs[i]->is_long_term) |
2697 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2698 | 0, |
2699 | "lt_ref (%d) ", p_Dpb->fs[i]->is_reference); |
2700 | if (p_Dpb->fs[i]->is_output) |
2701 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2702 | 0, |
2703 | "out(displayed) "); |
2704 | if (p_Dpb->fs[i]->pre_output) |
2705 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2706 | 0, |
2707 | "pre_output(in dispq or displaying) "); |
2708 | if (p_Dpb->fs[i]->is_used == 3) { |
2709 | if (p_Dpb->fs[i]->frame->non_existing) |
2710 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2711 | 0, |
2712 | "non_existing "); |
2713 | } |
2714 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2715 | 0, |
2716 | "dpb_frame_count %d ", |
2717 | p_Dpb->fs[i]->dpb_frame_count); |
2718 | |
2719 | #if (MVC_EXTENSION_ENABLE) |
2720 | if (p_Dpb->fs[i]->is_reference) |
2721 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2722 | 0, |
2723 | "view_id (%d) ", p_Dpb->fs[i]->view_id); |
2724 | #endif |
2725 | if (p_Dpb->fs[i]->data_flag) { |
2726 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2727 | 0, |
2728 | "data_flag(0x%x)", |
2729 | p_Dpb->fs[i]->data_flag); |
2730 | } |
2731 | dpb_print_cont(p_H264_Dpb->decoder_index, |
2732 | 0, |
2733 | " bufspec %d\n", |
2734 | p_Dpb->fs[i]->buf_spec_num); |
2735 | } |
2736 | } |
2737 | |
2738 | /*! |
2739 | ************************************************************************ |
2740 | * \brief |
2741 | * adaptive memory management |
2742 | * |
2743 | ************************************************************************ |
2744 | */ |
2745 | |
2746 | static int get_pic_num_x(struct StorablePicture *p, |
2747 | int difference_of_pic_nums_minus1) |
2748 | { |
2749 | int currPicNum; |
2750 | |
2751 | if (p->structure == FRAME) |
2752 | currPicNum = p->frame_num; |
2753 | else |
2754 | currPicNum = 2 * p->frame_num + 1; |
2755 | |
2756 | return currPicNum - (difference_of_pic_nums_minus1 + 1); |
2757 | } |
2758 | |
2759 | /*! |
2760 | ************************************************************************ |
2761 | * \brief |
2762 | * Adaptive Memory Management: Mark short term picture unused |
2763 | ************************************************************************ |
2764 | */ |
2765 | static void mm_unmark_short_term_for_reference(struct DecodedPictureBuffer |
2766 | *p_Dpb, struct StorablePicture *p, |
2767 | int difference_of_pic_nums_minus1) |
2768 | { |
2769 | struct h264_dpb_stru *p_H264_Dpb = |
2770 | container_of(p_Dpb, struct h264_dpb_stru, mDPB); |
2771 | int picNumX; |
2772 | |
2773 | unsigned int i; |
2774 | |
2775 | picNumX = get_pic_num_x(p, difference_of_pic_nums_minus1); |
2776 | |
2777 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
2778 | #ifdef ERROR_CHECK |
2779 | if (p_Dpb->fs_ref[i] == NULL) { |
2780 | p_H264_Dpb->dpb_error_flag = __LINE__; |
2781 | continue; |
2782 | } |
2783 | #endif |
2784 | if (p->structure == FRAME) { |
2785 | if ((p_Dpb->fs_ref[i]->is_reference == 3) && |
2786 | (p_Dpb->fs_ref[i]->is_long_term == 0)) { |
2787 | #ifdef ERROR_CHECK |
2788 | if (p_Dpb->fs_ref[i]->frame == NULL) { |
2789 | p_H264_Dpb->dpb_error_flag = __LINE__; |
2790 | continue; |
2791 | } |
2792 | #endif |
2793 | if (p_Dpb->fs_ref[i]->frame->pic_num == |
2794 | picNumX) { |
2795 | unmark_for_reference(p_Dpb, |
2796 | p_Dpb->fs_ref[i]); |
2797 | return; |
2798 | } |
2799 | } |
2800 | } else { |
2801 | if ((p_Dpb->fs_ref[i]->is_reference & 1) && |
2802 | (!(p_Dpb->fs_ref[i]->is_long_term & 1))) { |
2803 | #ifdef ERROR_CHECK |
2804 | if (p_Dpb->fs_ref[i]->top_field == NULL) { |
2805 | p_H264_Dpb->dpb_error_flag = __LINE__; |
2806 | continue; |
2807 | } |
2808 | #endif |
2809 | if (p_Dpb->fs_ref[i]->top_field->pic_num == |
2810 | picNumX) { |
2811 | p_Dpb->fs_ref[i]-> |
2812 | top_field->used_for_reference = 0; |
2813 | p_Dpb->fs_ref[i]->is_reference &= 2; |
2814 | if ((p_Dpb->fs_ref[i]->is_used == 3) |
2815 | #ifdef ERROR_CHECK |
2816 | && p_Dpb->fs_ref[i]->frame |
2817 | #endif |
2818 | ) { |
2819 | p_Dpb->fs_ref[i]->frame-> |
2820 | used_for_reference = 0; |
2821 | } |
2822 | return; |
2823 | } |
2824 | } |
2825 | if ((p_Dpb->fs_ref[i]->is_reference & 2) && |
2826 | (!(p_Dpb->fs_ref[i]->is_long_term & 2))) { |
2827 | #ifdef ERROR_CHECK |
2828 | if (p_Dpb->fs_ref[i]->bottom_field == NULL) { |
2829 | p_H264_Dpb->dpb_error_flag = __LINE__; |
2830 | continue; |
2831 | } |
2832 | #endif |
2833 | if (p_Dpb->fs_ref[i]->bottom_field->pic_num == |
2834 | picNumX) { |
2835 | p_Dpb->fs_ref[i]->bottom_field-> |
2836 | used_for_reference = 0; |
2837 | p_Dpb->fs_ref[i]->is_reference &= 1; |
2838 | if ((p_Dpb->fs_ref[i]->is_used == 3) |
2839 | #ifdef ERROR_CHECK |
2840 | && p_Dpb->fs_ref[i]->frame |
2841 | #endif |
2842 | ) { |
2843 | p_Dpb->fs_ref[i]->frame-> |
2844 | used_for_reference = 0; |
2845 | } |
2846 | return; |
2847 | } |
2848 | } |
2849 | } |
2850 | } |
2851 | } |
2852 | |
2853 | static void unmark_for_long_term_reference(struct FrameStore *fs) |
2854 | { |
2855 | if (fs->is_used & 1) { |
2856 | if (fs->top_field) { |
2857 | fs->top_field->used_for_reference = 0; |
2858 | fs->top_field->is_long_term = 0; |
2859 | } |
2860 | } |
2861 | if (fs->is_used & 2) { |
2862 | if (fs->bottom_field) { |
2863 | fs->bottom_field->used_for_reference = 0; |
2864 | fs->bottom_field->is_long_term = 0; |
2865 | } |
2866 | } |
2867 | if (fs->is_used == 3) { |
2868 | if (fs->top_field && fs->bottom_field) { |
2869 | fs->top_field->used_for_reference = 0; |
2870 | fs->top_field->is_long_term = 0; |
2871 | fs->bottom_field->used_for_reference = 0; |
2872 | fs->bottom_field->is_long_term = 0; |
2873 | } |
2874 | fs->frame->used_for_reference = 0; |
2875 | fs->frame->is_long_term = 0; |
2876 | } |
2877 | |
2878 | fs->is_reference = 0; |
2879 | fs->is_long_term = 0; |
2880 | } |
2881 | |
2882 | /*! |
2883 | ************************************************************************ |
2884 | * \brief |
2885 | * Adaptive Memory Management: Mark long term picture unused |
2886 | ************************************************************************ |
2887 | */ |
2888 | static void mm_unmark_long_term_for_reference(struct DecodedPictureBuffer |
2889 | *p_Dpb, struct StorablePicture *p, int long_term_pic_num) |
2890 | { |
2891 | unsigned int i; |
2892 | |
2893 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
2894 | if (p->structure == FRAME) { |
2895 | if ((p_Dpb->fs_ltref[i]->is_reference == 3) && |
2896 | (p_Dpb->fs_ltref[i]->is_long_term == 3)) { |
2897 | if (p_Dpb->fs_ltref[i]->frame-> |
2898 | long_term_pic_num == |
2899 | long_term_pic_num) { |
2900 | unmark_for_long_term_reference( |
2901 | p_Dpb->fs_ltref[i]); |
2902 | } |
2903 | } |
2904 | } else { |
2905 | if ((p_Dpb->fs_ltref[i]->is_reference & 1) && |
2906 | ((p_Dpb->fs_ltref[i]->is_long_term & 1))) { |
2907 | if (p_Dpb->fs_ltref[i]->top_field-> |
2908 | long_term_pic_num == |
2909 | long_term_pic_num) { |
2910 | p_Dpb->fs_ltref[i]->top_field-> |
2911 | used_for_reference = 0; |
2912 | p_Dpb->fs_ltref[i]->top_field-> |
2913 | is_long_term = 0; |
2914 | p_Dpb->fs_ltref[i]->is_reference &= 2; |
2915 | p_Dpb->fs_ltref[i]->is_long_term &= 2; |
2916 | if (p_Dpb->fs_ltref[i]->is_used == 3) { |
2917 | p_Dpb->fs_ltref[i]->frame-> |
2918 | used_for_reference = 0; |
2919 | p_Dpb->fs_ltref[i]->frame-> |
2920 | is_long_term = 0; |
2921 | } |
2922 | return; |
2923 | } |
2924 | } |
2925 | if ((p_Dpb->fs_ltref[i]->is_reference & 2) && |
2926 | ((p_Dpb->fs_ltref[i]->is_long_term & 2))) { |
2927 | if (p_Dpb->fs_ltref[i]->bottom_field-> |
2928 | long_term_pic_num == |
2929 | long_term_pic_num) { |
2930 | p_Dpb->fs_ltref[i]->bottom_field-> |
2931 | used_for_reference = 0; |
2932 | p_Dpb->fs_ltref[i]->bottom_field-> |
2933 | is_long_term = 0; |
2934 | p_Dpb->fs_ltref[i]->is_reference &= 1; |
2935 | p_Dpb->fs_ltref[i]->is_long_term &= 1; |
2936 | if (p_Dpb->fs_ltref[i]->is_used == 3) { |
2937 | p_Dpb->fs_ltref[i]->frame-> |
2938 | used_for_reference = 0; |
2939 | p_Dpb->fs_ltref[i]->frame-> |
2940 | is_long_term = 0; |
2941 | } |
2942 | return; |
2943 | } |
2944 | } |
2945 | } |
2946 | } |
2947 | } |
2948 | |
2949 | |
2950 | /*! |
2951 | ************************************************************************ |
2952 | * \brief |
2953 | * Mark a long-term reference frame or complementary |
2954 | * field pair unused for referemce |
2955 | ************************************************************************ |
2956 | */ |
2957 | static void unmark_long_term_frame_for_reference_by_frame_idx( |
2958 | struct DecodedPictureBuffer *p_Dpb, int long_term_frame_idx) |
2959 | { |
2960 | unsigned int i; |
2961 | |
2962 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
2963 | if (p_Dpb->fs_ltref[i]->long_term_frame_idx == |
2964 | long_term_frame_idx) |
2965 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
2966 | } |
2967 | } |
2968 | |
2969 | |
2970 | static void unmark1(struct DecodedPictureBuffer *p_Dpb, |
2971 | unsigned int curr_frame_num, int i) |
2972 | { |
2973 | if (p_Dpb->last_picture) { |
2974 | /*if ((p_Dpb->last_picture != p_Dpb->fs_ltref[i]) || |
2975 | p_Dpb->last_picture->frame_num != curr_frame_num) {*/ |
2976 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
2977 | /*} else { |
2978 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
2979 | }*/ |
2980 | } |
2981 | } |
2982 | |
2983 | static void unmark2(struct DecodedPictureBuffer *p_Dpb, |
2984 | int curr_pic_num, int i) |
2985 | { |
2986 | if ((p_Dpb->fs_ltref[i]->frame_num) != |
2987 | (unsigned int)(curr_pic_num >> 1)) |
2988 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
2989 | } |
2990 | |
2991 | static void unmark3_top(struct DecodedPictureBuffer *p_Dpb, |
2992 | unsigned int curr_frame_num, int curr_pic_num, int mark_current, int i) |
2993 | { |
2994 | if (p_Dpb->fs_ltref[i]->is_long_term == 3) { |
2995 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
2996 | } else { |
2997 | if (p_Dpb->fs_ltref[i]->is_long_term == 1) { |
2998 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
2999 | } else { |
3000 | if (mark_current) |
3001 | unmark1(p_Dpb, curr_frame_num, i); |
3002 | else |
3003 | unmark2(p_Dpb, curr_pic_num, i); |
3004 | } |
3005 | } |
3006 | } |
3007 | |
3008 | static void unmark3_bottom(struct DecodedPictureBuffer *p_Dpb, |
3009 | unsigned int curr_frame_num, int curr_pic_num, int mark_current, int i) |
3010 | { |
3011 | if (p_Dpb->fs_ltref[i]->is_long_term == 2) { |
3012 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
3013 | } else { |
3014 | if (mark_current) |
3015 | unmark1(p_Dpb, curr_frame_num, i); |
3016 | else |
3017 | unmark2(p_Dpb, curr_pic_num, i); |
3018 | } |
3019 | } |
3020 | |
3021 | static void unmark_long_term_field_for_reference_by_frame_idx( |
3022 | struct DecodedPictureBuffer *p_Dpb, enum PictureStructure structure, |
3023 | int long_term_frame_idx, int mark_current, unsigned int curr_frame_num, |
3024 | int curr_pic_num) |
3025 | { |
3026 | struct VideoParameters *p_Vid = p_Dpb->p_Vid; |
3027 | unsigned int i; |
3028 | |
3029 | /* assert(structure!=FRAME); */ |
3030 | if (curr_pic_num < 0) |
3031 | curr_pic_num += (2 * p_Vid->max_frame_num); |
3032 | |
3033 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
3034 | if (p_Dpb->fs_ltref[i]->long_term_frame_idx == |
3035 | long_term_frame_idx) { |
3036 | if (structure == TOP_FIELD) |
3037 | unmark3_top(p_Dpb, curr_frame_num, |
3038 | curr_pic_num, mark_current, i); |
3039 | |
3040 | if (structure == BOTTOM_FIELD) |
3041 | unmark3_bottom(p_Dpb, curr_frame_num, |
3042 | curr_pic_num, mark_current, i); |
3043 | } |
3044 | } |
3045 | } |
3046 | |
3047 | /*! |
3048 | ************************************************************************ |
3049 | * \brief |
3050 | * mark a picture as long-term reference |
3051 | ************************************************************************ |
3052 | */ |
3053 | static void mark_pic_long_term(struct DecodedPictureBuffer *p_Dpb, |
3054 | struct StorablePicture *p, |
3055 | int long_term_frame_idx, int picNumX) |
3056 | { |
3057 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
3058 | struct h264_dpb_stru, mDPB); |
3059 | unsigned int i; |
3060 | int add_top, add_bottom; |
3061 | |
3062 | if (p->structure == FRAME) { |
3063 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
3064 | #ifdef ERROR_CHECK |
3065 | if (p_Dpb->fs_ref[i] == NULL) { |
3066 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3067 | continue; |
3068 | } |
3069 | #endif |
3070 | if (p_Dpb->fs_ref[i]->is_reference == 3) { |
3071 | #ifdef ERROR_CHECK |
3072 | if (p_Dpb->fs_ref[i]->frame == NULL) { |
3073 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3074 | continue; |
3075 | } |
3076 | #endif |
3077 | if ((!p_Dpb->fs_ref[i]->frame-> |
3078 | is_long_term) && |
3079 | (p_Dpb->fs_ref[i]->frame->pic_num == |
3080 | picNumX)) { |
3081 | p_Dpb->fs_ref[i]-> |
3082 | long_term_frame_idx = |
3083 | p_Dpb->fs_ref[i]->frame-> |
3084 | long_term_frame_idx = |
3085 | long_term_frame_idx; |
3086 | p_Dpb->fs_ref[i]->frame-> |
3087 | long_term_pic_num = |
3088 | long_term_frame_idx; |
3089 | p_Dpb->fs_ref[i]->frame-> |
3090 | is_long_term = 1; |
3091 | |
3092 | if (p_Dpb->fs_ref[i]->top_field && |
3093 | p_Dpb->fs_ref[i]->bottom_field) { |
3094 | p_Dpb->fs_ref[i]->top_field-> |
3095 | long_term_frame_idx = |
3096 | p_Dpb->fs_ref[i]-> |
3097 | bottom_field-> |
3098 | long_term_frame_idx = |
3099 | long_term_frame_idx; |
3100 | p_Dpb->fs_ref[i]->top_field-> |
3101 | long_term_pic_num = |
3102 | long_term_frame_idx; |
3103 | p_Dpb->fs_ref[i]-> |
3104 | bottom_field-> |
3105 | long_term_pic_num = |
3106 | long_term_frame_idx; |
3107 | |
3108 | p_Dpb->fs_ref[i]->top_field-> |
3109 | is_long_term = |
3110 | p_Dpb->fs_ref[i]-> |
3111 | bottom_field-> |
3112 | is_long_term |
3113 | = 1; |
3114 | |
3115 | } |
3116 | p_Dpb->fs_ref[i]->is_long_term = 3; |
3117 | return; |
3118 | } |
3119 | } |
3120 | } |
3121 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
3122 | "Warning: reference frame for long term marking not found\n"); |
3123 | } else { |
3124 | if (p->structure == TOP_FIELD) { |
3125 | add_top = 1; |
3126 | add_bottom = 0; |
3127 | } else { |
3128 | add_top = 0; |
3129 | add_bottom = 1; |
3130 | } |
3131 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
3132 | #ifdef ERROR_CHECK |
3133 | if (p_Dpb->fs_ref[i] == NULL) { |
3134 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3135 | continue; |
3136 | } |
3137 | #endif |
3138 | if (p_Dpb->fs_ref[i]->is_reference & 1) { |
3139 | #ifdef ERROR_CHECK |
3140 | if (p_Dpb->fs_ref[i]->top_field == NULL) { |
3141 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3142 | continue; |
3143 | } |
3144 | #endif |
3145 | if ((!p_Dpb->fs_ref[i]->top_field-> |
3146 | is_long_term) && |
3147 | (p_Dpb->fs_ref[i]->top_field->pic_num == |
3148 | picNumX)) { |
3149 | if ((p_Dpb->fs_ref[i]-> |
3150 | is_long_term) && |
3151 | (p_Dpb->fs_ref[i]-> |
3152 | long_term_frame_idx != |
3153 | long_term_frame_idx)) { |
3154 | dpb_print(p_H264_Dpb-> |
3155 | decoder_index, |
3156 | PRINT_FLAG_DPB_DETAIL, |
3157 | "Warning: assigning long_term_frame_idx different from other field\n"); |
3158 | } |
3159 | |
3160 | p_Dpb->fs_ref[i]-> |
3161 | long_term_frame_idx = |
3162 | p_Dpb->fs_ref[i]->top_field-> |
3163 | long_term_frame_idx |
3164 | = long_term_frame_idx; |
3165 | p_Dpb->fs_ref[i]->top_field-> |
3166 | long_term_pic_num = |
3167 | 2 * long_term_frame_idx + |
3168 | add_top; |
3169 | p_Dpb->fs_ref[i]->top_field-> |
3170 | is_long_term = 1; |
3171 | p_Dpb->fs_ref[i]->is_long_term |= 1; |
3172 | if ((p_Dpb->fs_ref[i]->is_long_term |
3173 | == 3) |
3174 | #ifdef ERROR_CHECK |
3175 | && p_Dpb->fs_ref[i]->frame |
3176 | #endif |
3177 | ) { |
3178 | p_Dpb->fs_ref[i]->frame-> |
3179 | is_long_term = 1; |
3180 | p_Dpb->fs_ref[i]->frame-> |
3181 | long_term_frame_idx = |
3182 | p_Dpb->fs_ref[i]-> |
3183 | frame-> |
3184 | long_term_pic_num = |
3185 | long_term_frame_idx; |
3186 | } |
3187 | return; |
3188 | } |
3189 | } |
3190 | if (p_Dpb->fs_ref[i]->is_reference & 2) { |
3191 | #ifdef ERROR_CHECK |
3192 | if (p_Dpb->fs_ref[i]->bottom_field == NULL) { |
3193 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3194 | continue; |
3195 | } |
3196 | #endif |
3197 | if ((!p_Dpb->fs_ref[i]->bottom_field-> |
3198 | is_long_term) && |
3199 | (p_Dpb->fs_ref[i]->bottom_field->pic_num |
3200 | == picNumX)) { |
3201 | if ((p_Dpb->fs_ref[i]-> |
3202 | is_long_term) && |
3203 | (p_Dpb->fs_ref[i]-> |
3204 | long_term_frame_idx != |
3205 | long_term_frame_idx)) { |
3206 | dpb_print(p_H264_Dpb-> |
3207 | decoder_index, |
3208 | PRINT_FLAG_DPB_DETAIL, |
3209 | "Warning: assigning long_term_frame_idx different from other field\n"); |
3210 | } |
3211 | |
3212 | p_Dpb->fs_ref[i]-> |
3213 | long_term_frame_idx = |
3214 | p_Dpb->fs_ref[i]->bottom_field |
3215 | ->long_term_frame_idx |
3216 | = long_term_frame_idx; |
3217 | p_Dpb->fs_ref[i]->bottom_field-> |
3218 | long_term_pic_num = 2 * |
3219 | long_term_frame_idx + |
3220 | add_bottom; |
3221 | p_Dpb->fs_ref[i]->bottom_field-> |
3222 | is_long_term = 1; |
3223 | p_Dpb->fs_ref[i]->is_long_term |= 2; |
3224 | if ((p_Dpb->fs_ref[i]-> |
3225 | is_long_term == 3) |
3226 | #ifdef ERROR_CHECK |
3227 | && p_Dpb->fs_ref[i]->frame |
3228 | #endif |
3229 | ) { |
3230 | p_Dpb->fs_ref[i]->frame-> |
3231 | is_long_term = 1; |
3232 | p_Dpb->fs_ref[i]->frame-> |
3233 | long_term_frame_idx = |
3234 | p_Dpb->fs_ref[i]-> |
3235 | frame-> |
3236 | long_term_pic_num = |
3237 | long_term_frame_idx; |
3238 | } |
3239 | return; |
3240 | } |
3241 | } |
3242 | } |
3243 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
3244 | "Warning: reference field for long term marking not found\n"); |
3245 | } |
3246 | } |
3247 | |
3248 | |
3249 | /*! |
3250 | ************************************************************************ |
3251 | * \brief |
3252 | * Assign a long term frame index to a short term picture |
3253 | ************************************************************************ |
3254 | */ |
3255 | static void mm_assign_long_term_frame_idx(struct DecodedPictureBuffer *p_Dpb, |
3256 | struct StorablePicture *p, int difference_of_pic_nums_minus1, |
3257 | int long_term_frame_idx) |
3258 | { |
3259 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
3260 | struct h264_dpb_stru, mDPB); |
3261 | int picNumX = get_pic_num_x(p, difference_of_pic_nums_minus1); |
3262 | |
3263 | /* remove frames/fields with same long_term_frame_idx */ |
3264 | if (p->structure == FRAME) { |
3265 | unmark_long_term_frame_for_reference_by_frame_idx(p_Dpb, |
3266 | long_term_frame_idx); |
3267 | } else { |
3268 | unsigned int i; |
3269 | enum PictureStructure structure = FRAME; |
3270 | |
3271 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
3272 | #ifdef ERROR_CHECK |
3273 | if (p_Dpb->fs_ref[i] == NULL) { |
3274 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3275 | continue; |
3276 | } |
3277 | #endif |
3278 | if (p_Dpb->fs_ref[i]->is_reference & 1) { |
3279 | #ifdef ERROR_CHECK |
3280 | if (p_Dpb->fs_ref[i]->top_field == NULL) { |
3281 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3282 | continue; |
3283 | } |
3284 | #endif |
3285 | if (p_Dpb->fs_ref[i]->top_field-> |
3286 | pic_num == picNumX) { |
3287 | structure = TOP_FIELD; |
3288 | break; |
3289 | } |
3290 | } |
3291 | if (p_Dpb->fs_ref[i]->is_reference & 2) { |
3292 | #ifdef ERROR_CHECK |
3293 | if (p_Dpb->fs_ref[i]->bottom_field == NULL) { |
3294 | p_H264_Dpb->dpb_error_flag = __LINE__; |
3295 | continue; |
3296 | } |
3297 | #endif |
3298 | if (p_Dpb->fs_ref[i]->bottom_field-> |
3299 | pic_num == picNumX) { |
3300 | structure = BOTTOM_FIELD; |
3301 | break; |
3302 | } |
3303 | } |
3304 | } |
3305 | if (structure == FRAME) { |
3306 | dpb_print(p_H264_Dpb->decoder_index, |
3307 | PRINT_FLAG_DPB_DETAIL, |
3308 | "field for long term marking not found %d", |
3309 | 200); |
3310 | } |
3311 | |
3312 | unmark_long_term_field_for_reference_by_frame_idx(p_Dpb, |
3313 | structure, |
3314 | long_term_frame_idx, 0, 0, picNumX); |
3315 | } |
3316 | |
3317 | mark_pic_long_term(p_Dpb, p, long_term_frame_idx, picNumX); |
3318 | } |
3319 | |
3320 | /*! |
3321 | ************************************************************************ |
3322 | * \brief |
3323 | * Set new max long_term_frame_idx |
3324 | ************************************************************************ |
3325 | */ |
3326 | static void mm_update_max_long_term_frame_idx(struct DecodedPictureBuffer |
3327 | *p_Dpb, int max_long_term_frame_idx_plus1) |
3328 | { |
3329 | unsigned int i; |
3330 | |
3331 | p_Dpb->max_long_term_pic_idx = max_long_term_frame_idx_plus1 - 1; |
3332 | |
3333 | /* check for invalid frames */ |
3334 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
3335 | if (p_Dpb->fs_ltref[i]->long_term_frame_idx > |
3336 | p_Dpb->max_long_term_pic_idx) { |
3337 | unmark_for_long_term_reference(p_Dpb->fs_ltref[i]); |
3338 | } |
3339 | } |
3340 | } |
3341 | |
3342 | |
3343 | /*! |
3344 | ************************************************************************ |
3345 | * \brief |
3346 | * Mark all long term reference pictures unused for reference |
3347 | ************************************************************************ |
3348 | */ |
3349 | static void mm_unmark_all_long_term_for_reference(struct DecodedPictureBuffer |
3350 | *p_Dpb) |
3351 | { |
3352 | mm_update_max_long_term_frame_idx(p_Dpb, 0); |
3353 | } |
3354 | |
3355 | /*! |
3356 | ************************************************************************ |
3357 | * \brief |
3358 | * Mark all short term reference pictures unused for reference |
3359 | ************************************************************************ |
3360 | */ |
3361 | static void mm_unmark_all_short_term_for_reference(struct DecodedPictureBuffer |
3362 | *p_Dpb) |
3363 | { |
3364 | unsigned int i; |
3365 | |
3366 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) |
3367 | unmark_for_reference(p_Dpb, p_Dpb->fs_ref[i]); |
3368 | update_ref_list(p_Dpb); |
3369 | } |
3370 | |
3371 | |
3372 | /*! |
3373 | ************************************************************************ |
3374 | * \brief |
3375 | * Mark the current picture used for long term reference |
3376 | ************************************************************************ |
3377 | */ |
3378 | static void mm_mark_current_picture_long_term(struct DecodedPictureBuffer |
3379 | *p_Dpb, struct StorablePicture *p, int long_term_frame_idx) |
3380 | { |
3381 | /* remove long term pictures with same long_term_frame_idx */ |
3382 | if (p->structure == FRAME) { |
3383 | unmark_long_term_frame_for_reference_by_frame_idx(p_Dpb, |
3384 | long_term_frame_idx); |
3385 | } else { |
3386 | unmark_long_term_field_for_reference_by_frame_idx(p_Dpb, |
3387 | p->structure, long_term_frame_idx, |
3388 | 1, p->pic_num, 0); |
3389 | } |
3390 | |
3391 | p->is_long_term = 1; |
3392 | p->long_term_frame_idx = long_term_frame_idx; |
3393 | } |
3394 | |
3395 | static void adaptive_memory_management(struct h264_dpb_stru *p_H264_Dpb, |
3396 | struct StorablePicture *p) |
3397 | { |
3398 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
3399 | struct DecRefPicMarking_s *tmp_drpm; |
3400 | struct VideoParameters *p_Vid = p_Dpb->p_Vid; |
3401 | |
3402 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
3403 | "%s\n", __func__); |
3404 | p_Vid->last_has_mmco_5 = 0; |
3405 | |
3406 | /* assert (!p->idr_flag); */ |
3407 | /* assert (p->adaptive_ref_pic_buffering_flag); */ |
3408 | |
3409 | while (p->dec_ref_pic_marking_buffer) { |
3410 | tmp_drpm = p->dec_ref_pic_marking_buffer; |
3411 | switch (tmp_drpm->memory_management_control_operation) { |
3412 | case 0: |
3413 | if (tmp_drpm->Next != NULL) |
3414 | dpb_print(p_H264_Dpb->decoder_index, |
3415 | PRINT_FLAG_ERROR, |
3416 | "error, memory_management_control_operation = 0 not last operation in buffer\n"); |
3417 | break; |
3418 | case 1: |
3419 | mm_unmark_short_term_for_reference(p_Dpb, p, |
3420 | tmp_drpm->difference_of_pic_nums_minus1); |
3421 | update_ref_list(p_Dpb); |
3422 | break; |
3423 | case 2: |
3424 | mm_unmark_long_term_for_reference(p_Dpb, p, |
3425 | tmp_drpm->long_term_pic_num); |
3426 | update_ltref_list(p_Dpb); |
3427 | break; |
3428 | case 3: |
3429 | mm_assign_long_term_frame_idx(p_Dpb, p, |
3430 | tmp_drpm->difference_of_pic_nums_minus1, |
3431 | tmp_drpm->long_term_frame_idx); |
3432 | update_ref_list(p_Dpb); |
3433 | update_ltref_list(p_Dpb); |
3434 | break; |
3435 | case 4: |
3436 | mm_update_max_long_term_frame_idx(p_Dpb, |
3437 | tmp_drpm->max_long_term_frame_idx_plus1); |
3438 | update_ltref_list(p_Dpb); |
3439 | break; |
3440 | case 5: |
3441 | mm_unmark_all_short_term_for_reference(p_Dpb); |
3442 | mm_unmark_all_long_term_for_reference(p_Dpb); |
3443 | p_Vid->last_has_mmco_5 = 1; |
3444 | break; |
3445 | case 6: |
3446 | mm_mark_current_picture_long_term(p_Dpb, p, |
3447 | tmp_drpm->long_term_frame_idx); |
3448 | check_num_ref(p_Dpb); |
3449 | break; |
3450 | default: |
3451 | dpb_print(p_H264_Dpb->decoder_index, |
3452 | PRINT_FLAG_ERROR, |
3453 | "error, invalid memory_management_control_operation in buffer\n"); |
3454 | } |
3455 | p->dec_ref_pic_marking_buffer = tmp_drpm->Next; |
3456 | /* free (tmp_drpm); */ |
3457 | } |
3458 | if (p_Vid->last_has_mmco_5) { |
3459 | p->pic_num = p->frame_num = 0; |
3460 | |
3461 | switch (p->structure) { |
3462 | case TOP_FIELD: { |
3463 | /* p->poc = p->top_poc = p_Vid->toppoc =0; */ |
3464 | p->poc = p->top_poc = 0; |
3465 | break; |
3466 | } |
3467 | case BOTTOM_FIELD: { |
3468 | /* p->poc = p->bottom_poc = p_Vid->bottompoc = 0; */ |
3469 | p->poc = p->bottom_poc = 0; |
3470 | break; |
3471 | } |
3472 | case FRAME: { |
3473 | p->top_poc -= p->poc; |
3474 | p->bottom_poc -= p->poc; |
3475 | |
3476 | /* p_Vid->toppoc = p->top_poc; */ |
3477 | /* p_Vid->bottompoc = p->bottom_poc; */ |
3478 | |
3479 | p->poc = imin(p->top_poc, p->bottom_poc); |
3480 | /* p_Vid->framepoc = p->poc; */ |
3481 | break; |
3482 | } |
3483 | } |
3484 | /* currSlice->ThisPOC = p->poc; */ |
3485 | #if (MVC_EXTENSION_ENABLE) |
3486 | if (p->view_id == 0) { |
3487 | flush_dpb(p_Vid->p_Dpb_layer[0]); |
3488 | flush_dpb(p_Vid->p_Dpb_layer[1]); |
3489 | } else { |
3490 | flush_dpb(p_Dpb); |
3491 | } |
3492 | #else |
3493 | flush_dpb(p_H264_Dpb); |
3494 | #endif |
3495 | } |
3496 | } |
3497 | |
3498 | |
3499 | int store_picture_in_dpb(struct h264_dpb_stru *p_H264_Dpb, |
3500 | struct StorablePicture *p, |
3501 | unsigned char data_flag) |
3502 | { |
3503 | /* struct VideoParameters *p_Vid = p_Dpb->p_Vid; */ |
3504 | struct VideoParameters *p_Vid = &p_H264_Dpb->mVideo; |
3505 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
3506 | unsigned int i; |
3507 | #if 0 |
3508 | int poc, pos; |
3509 | #endif |
3510 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
3511 | "%s p_Vid %p\n", __func__, p_Vid); |
3512 | |
3513 | /* picture error concealment */ |
3514 | |
3515 | /* diagnostics */ |
3516 | /* dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
3517 | * "Storing (%s) non-ref pic with frame_num #%d\n", |
3518 | * (p->type == FRAME)?"FRAME":(p->type == TOP_FIELD)? |
3519 | * "TOP_FIELD":"BOTTOM_FIELD", p->pic_num); |
3520 | */ |
3521 | /* if frame, check for new store, */ |
3522 | /* assert (p!=NULL); */ |
3523 | |
3524 | p_Vid->last_has_mmco_5 = 0; |
3525 | p_Vid->last_pic_bottom_field = (p->structure == BOTTOM_FIELD); |
3526 | if (p->idr_flag) { |
3527 | idr_memory_management(p_H264_Dpb, p); |
3528 | if (p_H264_Dpb->first_insert_frame == FirstInsertFrm_OUT) |
3529 | p_H264_Dpb->first_insert_frame = FirstInsertFrm_SKIPDONE; |
3530 | #if 0 |
3531 | /* ??? */ |
3532 | /* picture error concealment */ |
3533 | memset(p_Vid->pocs_in_dpb, 0, sizeof(int) * 100); |
3534 | #endif |
3535 | } else { |
3536 | #if 1 |
3537 | /* ??? */ |
3538 | /* adaptive memory management */ |
3539 | if (p->used_for_reference && |
3540 | (p->adaptive_ref_pic_buffering_flag)) |
3541 | adaptive_memory_management(p_H264_Dpb, p); |
3542 | #endif |
3543 | } |
3544 | |
3545 | if ((p->structure == TOP_FIELD) || (p->structure == BOTTOM_FIELD)) { |
3546 | /* check for frame store with same pic_number */ |
3547 | if (p_Dpb->last_picture) { |
3548 | if ((int)p_Dpb->last_picture->frame_num == |
3549 | p->pic_num) { |
3550 | if (((p->structure == TOP_FIELD) && |
3551 | (p_Dpb->last_picture->is_used == 2)) || |
3552 | ((p->structure == BOTTOM_FIELD) && |
3553 | (p_Dpb->last_picture->is_used == 1))) { |
3554 | if ((p->used_for_reference && |
3555 | (p_Dpb->last_picture-> |
3556 | is_orig_reference != 0)) || |
3557 | (!p->used_for_reference && |
3558 | (p_Dpb->last_picture-> |
3559 | is_orig_reference == 0))) { |
3560 | insert_picture_in_dpb( |
3561 | p_H264_Dpb, |
3562 | p_Dpb->last_picture, |
3563 | p, data_flag); |
3564 | update_ref_list(p_Dpb); |
3565 | update_ltref_list(p_Dpb); |
3566 | dump_dpb(p_Dpb, 0); |
3567 | p_Dpb->last_picture = NULL; |
3568 | return 0; |
3569 | } |
3570 | } |
3571 | } |
3572 | } |
3573 | } |
3574 | /* this is a frame or a field which has no stored |
3575 | * complementary field |
3576 | */ |
3577 | |
3578 | /* sliding window, if necessary */ |
3579 | if ((!p->idr_flag) && (p->used_for_reference && |
3580 | (!p->adaptive_ref_pic_buffering_flag))) { |
3581 | sliding_window_memory_management(p_Dpb, p); |
3582 | } |
3583 | |
3584 | /* picture error concealment */ |
3585 | if (p_Vid->conceal_mode != 0) { |
3586 | for (i = 0; i < p_Dpb->size; i++) |
3587 | if (p_Dpb->fs[i]->is_reference) |
3588 | p_Dpb->fs[i]->concealment_reference = 1; |
3589 | } |
3590 | |
3591 | while (remove_unused_frame_from_dpb(p_H264_Dpb)) |
3592 | ; |
3593 | |
3594 | while (output_frames(p_H264_Dpb, 0)) |
3595 | ; |
3596 | |
3597 | /* check for duplicate frame number in short term reference buffer */ |
3598 | if ((p->used_for_reference) && (!p->is_long_term)) { |
3599 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
3600 | #ifdef ERROR_CHECK |
3601 | if (p_Dpb->fs_ref[i] == NULL) |
3602 | continue; |
3603 | #endif |
3604 | if (p_Dpb->fs_ref[i]->frame_num == p->frame_num) { |
3605 | dpb_print(p_H264_Dpb->decoder_index, |
3606 | PRINT_FLAG_DPB_DETAIL, |
3607 | "duplicate frame_num in short-term reference picture buffer %d\n", |
3608 | 500); |
3609 | } |
3610 | } |
3611 | } |
3612 | /* store at end of buffer */ |
3613 | |
3614 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
3615 | "%s p_Dpb->used_size %d\n", __func__, p_Dpb->used_size); |
3616 | if (p_Dpb->used_size >= p_Dpb->size) { |
3617 | dpb_print(p_H264_Dpb->decoder_index, |
3618 | PRINT_FLAG_ERROR, |
3619 | "%s Error: used_sizd %d is large than dpb size\r\n", |
3620 | __func__, p_Dpb->used_size); |
3621 | /*h264_debug_flag |= PRINT_FLAG_DUMP_DPB;*/ |
3622 | dump_dpb(p_Dpb, 0); |
3623 | return -1; |
3624 | } |
3625 | |
3626 | insert_picture_in_dpb(p_H264_Dpb, p_Dpb->fs[p_Dpb->used_size], |
3627 | p, data_flag); |
3628 | |
3629 | /* picture error concealment */ |
3630 | if (p->idr_flag) |
3631 | p_Vid->earlier_missing_poc = 0; |
3632 | |
3633 | if (p->structure != FRAME) |
3634 | p_Dpb->last_picture = p_Dpb->fs[p_Dpb->used_size]; |
3635 | else |
3636 | p_Dpb->last_picture = NULL; |
3637 | |
3638 | p_Dpb->used_size++; |
3639 | #if 0 |
3640 | /* ??? */ |
3641 | if (p_Vid->conceal_mode != 0) |
3642 | p_Vid->pocs_in_dpb[p_Dpb->used_size - 1] = p->poc; |
3643 | #endif |
3644 | update_ref_list(p_Dpb); |
3645 | update_ltref_list(p_Dpb); |
3646 | |
3647 | check_num_ref(p_Dpb); |
3648 | if (p_H264_Dpb->fast_output_enable == H264_OUTPUT_MODE_FAST) |
3649 | i = 1; |
3650 | else |
3651 | i = 0; |
3652 | if (i || (p_H264_Dpb->first_insert_frame < FirstInsertFrm_SKIPDONE)) { |
3653 | while (output_frames(p_H264_Dpb, i)) |
3654 | ; |
3655 | } |
3656 | |
3657 | dump_dpb(p_Dpb, 0); |
3658 | p_Dpb->first_pic_done = 1; /*by rain*/ |
3659 | |
3660 | return 0; |
3661 | } |
3662 | |
3663 | void bufmgr_post(struct h264_dpb_stru *p_H264_Dpb) |
3664 | { |
3665 | /*VideoParameters *p_Vid = p_Dpb->p_Vid;*/ |
3666 | struct VideoParameters *p_Vid = &p_H264_Dpb->mVideo; |
3667 | |
3668 | if (p_Vid->last_has_mmco_5) |
3669 | p_Vid->pre_frame_num = 0; |
3670 | } |
3671 | /********************************** |
3672 | * |
3673 | * Initialize reference lists |
3674 | ********************************** |
3675 | */ |
3676 | #define __COMPARE(context, p1, p2) comp(p1, p2) |
3677 | #define __SHORTSORT(lo, hi, width, comp, context) \ |
3678 | shortsort(lo, hi, width, comp) |
3679 | #define CUTOFF 8 /* testing shows that this is good value */ |
3680 | #define STKSIZ (8*sizeof(void *) - 2) |
3681 | |
3682 | #undef swap |
3683 | static void swap( |
3684 | char *a, |
3685 | char *b, |
3686 | size_t width |
3687 | ) |
3688 | { |
3689 | char tmp; |
3690 | |
3691 | if (a != b) |
3692 | /* Do the swap one character at a time to avoid potential |
3693 | * alignment problems. |
3694 | */ |
3695 | while (width--) { |
3696 | tmp = *a; |
3697 | *a++ = *b; |
3698 | *b++ = tmp; |
3699 | } |
3700 | } |
3701 | |
3702 | static void shortsort( |
3703 | char *lo, |
3704 | char *hi, |
3705 | size_t width, |
3706 | int (*comp)(const void *, const void *) |
3707 | ) |
3708 | { |
3709 | char *p, *max; |
3710 | |
3711 | /* Note: in assertions below, i and j are alway inside original |
3712 | * bound of array to sort. |
3713 | */ |
3714 | |
3715 | while (hi > lo) { |
3716 | /* A[i] <= A[j] for i <= j, j > hi */ |
3717 | max = lo; |
3718 | for (p = lo + width; p <= hi; p += width) { |
3719 | /* A[i] <= A[max] for lo <= i < p */ |
3720 | if (__COMPARE(context, p, max) > 0) |
3721 | max = p; |
3722 | /* A[i] <= A[max] for lo <= i <= p */ |
3723 | } |
3724 | |
3725 | /* A[i] <= A[max] for lo <= i <= hi */ |
3726 | |
3727 | swap(max, hi, width); |
3728 | |
3729 | /* A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, |
3730 | * j >= hi |
3731 | */ |
3732 | |
3733 | hi -= width; |
3734 | |
3735 | /* A[i] <= A[j] for i <= j, j > hi, loop top condition |
3736 | * established |
3737 | */ |
3738 | } |
3739 | /* A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] |
3740 | * for i < j, so array is sorted |
3741 | */ |
3742 | } |
3743 | |
3744 | static void qsort( |
3745 | void *base, |
3746 | size_t num, |
3747 | size_t width, |
3748 | int (*comp)(const void *, const void *) |
3749 | ) |
3750 | { |
3751 | char *lo, *hi; /* ends of sub-array currently sorting */ |
3752 | char *mid; /* points to middle of subarray */ |
3753 | char *loguy, *higuy; /* traveling pointers for partition step */ |
3754 | size_t size; /* size of the sub-array */ |
3755 | char *lostk[STKSIZ], *histk[STKSIZ]; |
3756 | int stkptr; |
3757 | |
3758 | /* stack for saving sub-array to be |
3759 | * processed |
3760 | */ |
3761 | #if 0 |
3762 | /* validation section */ |
3763 | _VALIDATE_RETURN_VOID(base != NULL || num == 0, EINVAL); |
3764 | _VALIDATE_RETURN_VOID(width > 0, EINVAL); |
3765 | _VALIDATE_RETURN_VOID(comp != NULL, EINVAL); |
3766 | #endif |
3767 | if (num < 2) |
3768 | return; /* nothing to do */ |
3769 | |
3770 | stkptr = 0; /* initialize stack */ |
3771 | |
3772 | lo = (char *)base; |
3773 | hi = (char *)base + width * (num - 1); /* initialize limits */ |
3774 | |
3775 | /* this entry point is for pseudo-recursion calling: setting |
3776 | * lo and hi and jumping to here is like recursion, but stkptr is |
3777 | * preserved, locals aren't, so we preserve stuff on the stack |
3778 | */ |
3779 | recurse: |
3780 | |
3781 | size = (hi - lo) / width + 1; /* number of el's to sort */ |
3782 | |
3783 | /* below a certain size, it is faster to use a O(n^2) sorting method */ |
3784 | if (size <= CUTOFF) { |
3785 | __SHORTSORT(lo, hi, width, comp, context); |
3786 | } else { |
3787 | /* First we pick a partitioning element. The efficiency of |
3788 | * the algorithm demands that we find one that is approximately |
3789 | * the median of the values, but also that we select one fast. |
3790 | * We choose the median of the first, middle, and last |
3791 | * elements, to avoid bad performance in the face of already |
3792 | * sorted data, or data that is made up of multiple sorted |
3793 | * runs appended together. Testing shows that a |
3794 | * median-of-three algorithm provides better performance than |
3795 | * simply picking the middle element for the latter case. |
3796 | */ |
3797 | |
3798 | mid = lo + (size / 2) * width; /* find middle element */ |
3799 | |
3800 | /* Sort the first, middle, last elements into order */ |
3801 | if (__COMPARE(context, lo, mid) > 0) |
3802 | swap(lo, mid, width); |
3803 | if (__COMPARE(context, lo, hi) > 0) |
3804 | swap(lo, hi, width); |
3805 | if (__COMPARE(context, mid, hi) > 0) |
3806 | swap(mid, hi, width); |
3807 | |
3808 | /* We now wish to partition the array into three pieces, one |
3809 | * consisting of elements <= partition element, one of elements |
3810 | * equal to the partition element, and one of elements > than |
3811 | * it. This is done below; comments indicate conditions |
3812 | * established at every step. |
3813 | */ |
3814 | |
3815 | loguy = lo; |
3816 | higuy = hi; |
3817 | |
3818 | /* Note that higuy decreases and loguy increases on every |
3819 | * iteration, so loop must terminate. |
3820 | */ |
3821 | for (;;) { |
3822 | /* lo <= loguy < hi, lo < higuy <= hi, |
3823 | * A[i] <= A[mid] for lo <= i <= loguy, |
3824 | * A[i] > A[mid] for higuy <= i < hi, |
3825 | * A[hi] >= A[mid] |
3826 | */ |
3827 | |
3828 | /* The doubled loop is to avoid calling comp(mid,mid), |
3829 | * since some existing comparison funcs don't work |
3830 | * when passed the same value for both pointers. |
3831 | */ |
3832 | |
3833 | if (mid > loguy) { |
3834 | do { |
3835 | loguy += width; |
3836 | } while (loguy < mid && |
3837 | __COMPARE(context, loguy, mid) <= 0); |
3838 | } |
3839 | if (mid <= loguy) { |
3840 | do { |
3841 | loguy += width; |
3842 | } while (loguy <= hi && |
3843 | __COMPARE(context, loguy, mid) <= 0); |
3844 | } |
3845 | |
3846 | /* lo < loguy <= hi+1, A[i] <= A[mid] for |
3847 | * lo <= i < loguy, |
3848 | * either loguy > hi or A[loguy] > A[mid] |
3849 | */ |
3850 | |
3851 | do { |
3852 | higuy -= width; |
3853 | } while (higuy > mid && |
3854 | __COMPARE(context, higuy, mid) > 0); |
3855 | |
3856 | /* lo <= higuy < hi, A[i] > A[mid] for higuy < i < hi, |
3857 | * either higuy == lo or A[higuy] <= A[mid] |
3858 | */ |
3859 | |
3860 | if (higuy < loguy) |
3861 | break; |
3862 | |
3863 | /* if loguy > hi or higuy == lo, then we would have |
3864 | * exited, so A[loguy] > A[mid], A[higuy] <= A[mid], |
3865 | * loguy <= hi, higuy > lo |
3866 | */ |
3867 | |
3868 | swap(loguy, higuy, width); |
3869 | |
3870 | /* If the partition element was moved, follow it. |
3871 | * Only need to check for mid == higuy, since before |
3872 | * the swap, A[loguy] > A[mid] implies loguy != mid. |
3873 | */ |
3874 | |
3875 | if (mid == higuy) |
3876 | mid = loguy; |
3877 | |
3878 | /* A[loguy] <= A[mid], A[higuy] > A[mid]; so condition |
3879 | * at top of loop is re-established |
3880 | */ |
3881 | } |
3882 | |
3883 | /* A[i] <= A[mid] for lo <= i < loguy, |
3884 | * A[i] > A[mid] for higuy < i < hi, |
3885 | * A[hi] >= A[mid] |
3886 | * higuy < loguy |
3887 | * implying: |
3888 | * higuy == loguy-1 |
3889 | * or higuy == hi - 1, loguy == hi + 1, A[hi] == A[mid] |
3890 | */ |
3891 | |
3892 | /* Find adjacent elements equal to the partition element. The |
3893 | * doubled loop is to avoid calling comp(mid,mid), since some |
3894 | * existing comparison funcs don't work when passed the same |
3895 | * value for both pointers. |
3896 | */ |
3897 | |
3898 | higuy += width; |
3899 | if (mid < higuy) { |
3900 | do { |
3901 | higuy -= width; |
3902 | } while (higuy > mid && |
3903 | __COMPARE(context, higuy, mid) == 0); |
3904 | } |
3905 | if (mid >= higuy) { |
3906 | do { |
3907 | higuy -= width; |
3908 | } while (higuy > lo && |
3909 | __COMPARE(context, higuy, mid) == 0); |
3910 | } |
3911 | |
3912 | /* OK, now we have the following: |
3913 | * higuy < loguy |
3914 | * lo <= higuy <= hi |
3915 | * A[i] <= A[mid] for lo <= i <= higuy |
3916 | * A[i] == A[mid] for higuy < i < loguy |
3917 | * A[i] > A[mid] for loguy <= i < hi |
3918 | * A[hi] >= A[mid] |
3919 | */ |
3920 | |
3921 | /* We've finished the partition, now we want to sort the |
3922 | * subarrays [lo, higuy] and [loguy, hi]. |
3923 | * We do the smaller one first to minimize stack usage. |
3924 | * We only sort arrays of length 2 or more. |
3925 | */ |
3926 | |
3927 | if (higuy - lo >= hi - loguy) { |
3928 | if (lo < higuy) { |
3929 | lostk[stkptr] = lo; |
3930 | histk[stkptr] = higuy; |
3931 | ++stkptr; |
3932 | } /* save big recursion for later */ |
3933 | |
3934 | if (loguy < hi) { |
3935 | lo = loguy; |
3936 | goto recurse; /* do small recursion */ |
3937 | } |
3938 | } else { |
3939 | if (loguy < hi) { |
3940 | lostk[stkptr] = loguy; |
3941 | histk[stkptr] = hi; |
3942 | ++stkptr; /* save big recursion for later */ |
3943 | } |
3944 | |
3945 | if (lo < higuy) { |
3946 | hi = higuy; |
3947 | goto recurse; /* do small recursion */ |
3948 | } |
3949 | } |
3950 | } |
3951 | |
3952 | /* We have sorted the array, except for any pending sorts on the stack. |
3953 | * Check if there are any, and do them. |
3954 | */ |
3955 | |
3956 | --stkptr; |
3957 | if (stkptr >= 0) { |
3958 | lo = lostk[stkptr]; |
3959 | hi = histk[stkptr]; |
3960 | goto recurse; /* pop subarray from stack */ |
3961 | } else |
3962 | return; /* all subarrays done */ |
3963 | } |
3964 | |
3965 | /*! |
3966 | ************************************************************************ |
3967 | * \brief |
3968 | * compares two stored pictures by picture number for qsort in |
3969 | * descending order |
3970 | * |
3971 | ************************************************************************ |
3972 | */ |
3973 | static inline int compare_pic_by_pic_num_desc(const void *arg1, |
3974 | const void *arg2) |
3975 | { |
3976 | int pic_num1 = (*(struct StorablePicture **)arg1)->pic_num; |
3977 | int pic_num2 = (*(struct StorablePicture **)arg2)->pic_num; |
3978 | |
3979 | if (pic_num1 < pic_num2) |
3980 | return 1; |
3981 | if (pic_num1 > pic_num2) |
3982 | return -1; |
3983 | else |
3984 | return 0; |
3985 | } |
3986 | |
3987 | /*! |
3988 | ************************************************************************ |
3989 | * \brief |
3990 | * compares two stored pictures by picture number for qsort in |
3991 | * descending order |
3992 | * |
3993 | ************************************************************************ |
3994 | */ |
3995 | static inline int compare_pic_by_lt_pic_num_asc(const void *arg1, |
3996 | const void *arg2) |
3997 | { |
3998 | int long_term_pic_num1 = |
3999 | (*(struct StorablePicture **)arg1)->long_term_pic_num; |
4000 | int long_term_pic_num2 = |
4001 | (*(struct StorablePicture **)arg2)->long_term_pic_num; |
4002 | |
4003 | if (long_term_pic_num1 < long_term_pic_num2) |
4004 | return -1; |
4005 | if (long_term_pic_num1 > long_term_pic_num2) |
4006 | return 1; |
4007 | else |
4008 | return 0; |
4009 | } |
4010 | |
4011 | /*! |
4012 | ************************************************************************ |
4013 | * \brief |
4014 | * compares two frame stores by pic_num for qsort in descending order |
4015 | * |
4016 | ************************************************************************ |
4017 | */ |
4018 | static inline int compare_fs_by_frame_num_desc(const void *arg1, |
4019 | const void *arg2) |
4020 | { |
4021 | int frame_num_wrap1 = (*(struct FrameStore **)arg1)->frame_num_wrap; |
4022 | int frame_num_wrap2 = (*(struct FrameStore **)arg2)->frame_num_wrap; |
4023 | |
4024 | if (frame_num_wrap1 < frame_num_wrap2) |
4025 | return 1; |
4026 | if (frame_num_wrap1 > frame_num_wrap2) |
4027 | return -1; |
4028 | else |
4029 | return 0; |
4030 | } |
4031 | |
4032 | |
4033 | /*! |
4034 | ************************************************************************ |
4035 | * \brief |
4036 | * compares two frame stores by lt_pic_num for qsort in descending order |
4037 | * |
4038 | ************************************************************************ |
4039 | */ |
4040 | static inline int compare_fs_by_lt_pic_idx_asc(const void *arg1, |
4041 | const void *arg2) |
4042 | { |
4043 | int long_term_frame_idx1 = |
4044 | (*(struct FrameStore **)arg1)->long_term_frame_idx; |
4045 | int long_term_frame_idx2 = |
4046 | (*(struct FrameStore **)arg2)->long_term_frame_idx; |
4047 | |
4048 | if (long_term_frame_idx1 < long_term_frame_idx2) |
4049 | return -1; |
4050 | else if (long_term_frame_idx1 > long_term_frame_idx2) |
4051 | return 1; |
4052 | else |
4053 | return 0; |
4054 | } |
4055 | |
4056 | |
4057 | /*! |
4058 | ************************************************************************ |
4059 | * \brief |
4060 | * compares two stored pictures by poc for qsort in ascending order |
4061 | * |
4062 | ************************************************************************ |
4063 | */ |
4064 | static inline int compare_pic_by_poc_asc(const void *arg1, const void *arg2) |
4065 | { |
4066 | int poc1 = (*(struct StorablePicture **)arg1)->poc; |
4067 | int poc2 = (*(struct StorablePicture **)arg2)->poc; |
4068 | |
4069 | if (poc1 < poc2) |
4070 | return -1; |
4071 | else if (poc1 > poc2) |
4072 | return 1; |
4073 | else |
4074 | return 0; |
4075 | } |
4076 | |
4077 | |
4078 | /*! |
4079 | ************************************************************************ |
4080 | * \brief |
4081 | * compares two stored pictures by poc for qsort in descending order |
4082 | * |
4083 | ************************************************************************ |
4084 | */ |
4085 | static inline int compare_pic_by_poc_desc(const void *arg1, const void *arg2) |
4086 | { |
4087 | int poc1 = (*(struct StorablePicture **)arg1)->poc; |
4088 | int poc2 = (*(struct StorablePicture **)arg2)->poc; |
4089 | |
4090 | if (poc1 < poc2) |
4091 | return 1; |
4092 | else if (poc1 > poc2) |
4093 | return -1; |
4094 | else |
4095 | return 0; |
4096 | } |
4097 | |
4098 | |
4099 | /*! |
4100 | ************************************************************************ |
4101 | * \brief |
4102 | * compares two frame stores by poc for qsort in ascending order |
4103 | * |
4104 | ************************************************************************ |
4105 | */ |
4106 | static inline int compare_fs_by_poc_asc(const void *arg1, const void *arg2) |
4107 | { |
4108 | int poc1 = (*(struct FrameStore **)arg1)->poc; |
4109 | int poc2 = (*(struct FrameStore **)arg2)->poc; |
4110 | |
4111 | if (poc1 < poc2) |
4112 | return -1; |
4113 | else if (poc1 > poc2) |
4114 | return 1; |
4115 | else |
4116 | return 0; |
4117 | } |
4118 | |
4119 | |
4120 | /*! |
4121 | ************************************************************************ |
4122 | * \brief |
4123 | * compares two frame stores by poc for qsort in descending order |
4124 | * |
4125 | ************************************************************************ |
4126 | */ |
4127 | static inline int compare_fs_by_poc_desc(const void *arg1, const void *arg2) |
4128 | { |
4129 | int poc1 = (*(struct FrameStore **)arg1)->poc; |
4130 | int poc2 = (*(struct FrameStore **)arg2)->poc; |
4131 | |
4132 | if (poc1 < poc2) |
4133 | return 1; |
4134 | else if (poc1 > poc2) |
4135 | return -1; |
4136 | else |
4137 | return 0; |
4138 | } |
4139 | |
4140 | /*! |
4141 | ************************************************************************ |
4142 | * \brief |
4143 | * returns true, if picture is short term reference picture |
4144 | * |
4145 | ************************************************************************ |
4146 | */ |
4147 | static inline int is_short_ref(struct StorablePicture *s) |
4148 | { |
4149 | #ifdef ERROR_CHECK |
4150 | return (s && |
4151 | (s->used_for_reference) && (!(s->is_long_term))); |
4152 | #else |
4153 | return (s->used_for_reference) && (!(s->is_long_term)); |
4154 | #endif |
4155 | } |
4156 | |
4157 | |
4158 | /*! |
4159 | ************************************************************************ |
4160 | * \brief |
4161 | * returns true, if picture is long term reference picture |
4162 | * |
4163 | ************************************************************************ |
4164 | */ |
4165 | static inline int is_long_ref(struct StorablePicture *s) |
4166 | { |
4167 | #ifdef ERROR_CHECK |
4168 | return (s && |
4169 | s->used_for_reference) && (s->is_long_term); |
4170 | #else |
4171 | return (s->used_for_reference) && (s->is_long_term); |
4172 | #endif |
4173 | } |
4174 | |
4175 | /*! |
4176 | ************************************************************************ |
4177 | * \brief |
4178 | * Initialize reference lists for a P Slice |
4179 | * |
4180 | ************************************************************************ |
4181 | */ |
4182 | /*! |
4183 | ************************************************************************ |
4184 | * \brief |
4185 | * Generates a alternating field list from a given FrameStore list |
4186 | * |
4187 | ************************************************************************ |
4188 | */ |
4189 | static void gen_pic_list_from_frame_list(enum PictureStructure currStructure, |
4190 | struct FrameStore **fs_list, int list_idx, |
4191 | struct StorablePicture **list, |
4192 | char *list_size, int long_term) |
4193 | { |
4194 | int top_idx = 0; |
4195 | int bot_idx = 0; |
4196 | |
4197 | int (*is_ref)(struct StorablePicture *s) = (long_term) ? is_long_ref : |
4198 | is_short_ref; |
4199 | |
4200 | |
4201 | if (currStructure == TOP_FIELD) { |
4202 | while ((top_idx < list_idx) || (bot_idx < list_idx)) { |
4203 | for (; top_idx < list_idx; top_idx++) { |
4204 | if (fs_list[top_idx]->is_used & 1) { |
4205 | if (is_ref(fs_list[top_idx]-> |
4206 | top_field)) { |
4207 | /* short term ref pic */ |
4208 | list[(short) *list_size] = |
4209 | fs_list[top_idx]->top_field; |
4210 | (*list_size)++; |
4211 | top_idx++; |
4212 | break; |
4213 | } |
4214 | } |
4215 | } |
4216 | for (; bot_idx < list_idx; bot_idx++) { |
4217 | if (fs_list[bot_idx]->is_used & 2) { |
4218 | if (is_ref(fs_list[bot_idx]-> |
4219 | bottom_field)) { |
4220 | /* short term ref pic */ |
4221 | list[(short) *list_size] = |
4222 | fs_list[bot_idx]->bottom_field; |
4223 | (*list_size)++; |
4224 | bot_idx++; |
4225 | break; |
4226 | } |
4227 | } |
4228 | } |
4229 | } |
4230 | } |
4231 | if (currStructure == BOTTOM_FIELD) { |
4232 | while ((top_idx < list_idx) || (bot_idx < list_idx)) { |
4233 | for (; bot_idx < list_idx; bot_idx++) { |
4234 | if (fs_list[bot_idx]->is_used & 2) { |
4235 | if (is_ref(fs_list[bot_idx]-> |
4236 | bottom_field)) { |
4237 | /* short term ref pic */ |
4238 | list[(short) *list_size] = |
4239 | fs_list[bot_idx]->bottom_field; |
4240 | (*list_size)++; |
4241 | bot_idx++; |
4242 | break; |
4243 | } |
4244 | } |
4245 | } |
4246 | for (; top_idx < list_idx; top_idx++) { |
4247 | if (fs_list[top_idx]->is_used & 1) { |
4248 | if (is_ref(fs_list[top_idx]-> |
4249 | top_field)) { |
4250 | /* short term ref pic */ |
4251 | list[(short) *list_size] = |
4252 | fs_list[top_idx]->top_field; |
4253 | (*list_size)++; |
4254 | top_idx++; |
4255 | break; |
4256 | } |
4257 | } |
4258 | } |
4259 | } |
4260 | } |
4261 | } |
4262 | |
4263 | static void init_lists_p_slice(struct Slice *currSlice) |
4264 | { |
4265 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
4266 | struct DecodedPictureBuffer *p_Dpb = currSlice->p_Dpb; |
4267 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
4268 | struct h264_dpb_stru, mDPB); |
4269 | |
4270 | unsigned int i; |
4271 | |
4272 | int list0idx = 0; |
4273 | int listltidx = 0; |
4274 | |
4275 | struct FrameStore **fs_list0; |
4276 | struct FrameStore **fs_listlt; |
4277 | |
4278 | #if (MVC_EXTENSION_ENABLE) |
4279 | currSlice->listinterviewidx0 = 0; |
4280 | currSlice->listinterviewidx1 = 0; |
4281 | #endif |
4282 | |
4283 | if (currSlice->structure == FRAME) { |
4284 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4285 | #ifdef ERROR_CHECK |
4286 | if (p_Dpb->fs_ref[i] == NULL || |
4287 | p_Dpb->fs_ref[i]->frame == NULL) { |
4288 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4289 | continue; |
4290 | } |
4291 | #endif |
4292 | if (p_Dpb->fs_ref[i]->is_used == 3) { |
4293 | if ((p_Dpb->fs_ref[i]->frame-> |
4294 | used_for_reference) && |
4295 | (!p_Dpb->fs_ref[i]->frame-> |
4296 | is_long_term)) { |
4297 | currSlice->listX[0][list0idx++] = |
4298 | p_Dpb->fs_ref[i]->frame; |
4299 | } |
4300 | } |
4301 | } |
4302 | /* order list 0 by PicNum */ |
4303 | qsort((void *)currSlice->listX[0], list0idx, |
4304 | sizeof(struct StorablePicture *), |
4305 | compare_pic_by_pic_num_desc); |
4306 | currSlice->listXsize[0] = (char) list0idx; |
4307 | CHECK_VALID(currSlice->listXsize[0], 0); |
4308 | if (h264_debug_flag & PRINT_FLAG_DPB_DETAIL) { |
4309 | dpb_print(p_H264_Dpb->decoder_index, |
4310 | PRINT_FLAG_DPB_DETAIL, |
4311 | "listX[0] (PicNum): "); |
4312 | for (i = 0; i < list0idx; i++) { |
4313 | dpb_print(p_H264_Dpb->decoder_index, |
4314 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4315 | currSlice->listX[0][i]->pic_num); |
4316 | } |
4317 | dpb_print(p_H264_Dpb->decoder_index, |
4318 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4319 | } |
4320 | /* long term handling */ |
4321 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
4322 | if (p_Dpb->fs_ltref[i]->is_used == 3) { |
4323 | if (p_Dpb->fs_ltref[i]->frame->is_long_term) { |
4324 | currSlice->listX[0][list0idx++] = |
4325 | p_Dpb->fs_ltref[i]->frame; |
4326 | } |
4327 | } |
4328 | } |
4329 | qsort((void *)&currSlice->listX[0][ |
4330 | (short) currSlice->listXsize[0]], |
4331 | list0idx - currSlice->listXsize[0], |
4332 | sizeof(struct StorablePicture *), |
4333 | compare_pic_by_lt_pic_num_asc); |
4334 | currSlice->listXsize[0] = (char) list0idx; |
4335 | CHECK_VALID(currSlice->listXsize[0], 0); |
4336 | } else { |
4337 | #if 0 |
4338 | fs_list0 = calloc(p_Dpb->size, sizeof(struct FrameStore *)); |
4339 | if (fs_list0 == NULL) |
4340 | no_mem_exit("init_lists: fs_list0"); |
4341 | fs_listlt = calloc(p_Dpb->size, sizeof(struct FrameStore *)); |
4342 | if (fs_listlt == NULL) |
4343 | no_mem_exit("init_lists: fs_listlt"); |
4344 | #else |
4345 | fs_list0 = &(p_Dpb->fs_list0[0]); |
4346 | fs_listlt = &(p_Dpb->fs_listlt[0]); |
4347 | #endif |
4348 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4349 | #ifdef ERROR_CHECK |
4350 | if (p_Dpb->fs_ref[i] == NULL) { |
4351 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4352 | continue; |
4353 | } |
4354 | #endif |
4355 | if (p_Dpb->fs_ref[i]->is_reference) |
4356 | fs_list0[list0idx++] = p_Dpb->fs_ref[i]; |
4357 | } |
4358 | |
4359 | qsort((void *)fs_list0, list0idx, sizeof(struct FrameStore *), |
4360 | compare_fs_by_frame_num_desc); |
4361 | |
4362 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
4363 | "fs_list0 (FrameNum): "); |
4364 | for (i = 0; i < list0idx; i++) { |
4365 | dpb_print(p_H264_Dpb->decoder_index, |
4366 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4367 | fs_list0[i]->frame_num_wrap); |
4368 | } |
4369 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
4370 | "\n"); |
4371 | |
4372 | currSlice->listXsize[0] = 0; |
4373 | gen_pic_list_from_frame_list(currSlice->structure, fs_list0, |
4374 | list0idx, currSlice->listX[0], |
4375 | &currSlice->listXsize[0], 0); |
4376 | |
4377 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
4378 | "listX[0] (PicNum): "); |
4379 | for (i = 0; i < currSlice->listXsize[0]; i++) { |
4380 | dpb_print(p_H264_Dpb->decoder_index, |
4381 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4382 | currSlice->listX[0][i]->pic_num); |
4383 | } |
4384 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
4385 | "\n"); |
4386 | |
4387 | /* long term handling */ |
4388 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) |
4389 | fs_listlt[listltidx++] = p_Dpb->fs_ltref[i]; |
4390 | |
4391 | qsort((void *)fs_listlt, listltidx, sizeof(struct FrameStore *), |
4392 | compare_fs_by_lt_pic_idx_asc); |
4393 | |
4394 | gen_pic_list_from_frame_list(currSlice->structure, fs_listlt, |
4395 | listltidx, currSlice->listX[0], |
4396 | &currSlice->listXsize[0], 1); |
4397 | |
4398 | /* free(fs_list0); */ |
4399 | /* free(fs_listlt); */ |
4400 | } |
4401 | currSlice->listXsize[1] = 0; |
4402 | |
4403 | |
4404 | /* set max size */ |
4405 | currSlice->listXsize[0] = (char) imin(currSlice->listXsize[0], |
4406 | currSlice->num_ref_idx_active[LIST_0]); |
4407 | currSlice->listXsize[1] = (char) imin(currSlice->listXsize[1], |
4408 | currSlice->num_ref_idx_active[LIST_1]); |
4409 | CHECK_VALID(currSlice->listXsize[0], 0); |
4410 | CHECK_VALID(currSlice->listXsize[1], 1); |
4411 | |
4412 | /* set the unused list entries to NULL */ |
4413 | for (i = currSlice->listXsize[0]; i < (MAX_LIST_SIZE); i++) |
4414 | currSlice->listX[0][i] = p_Vid->no_reference_picture; |
4415 | for (i = currSlice->listXsize[1]; i < (MAX_LIST_SIZE); i++) |
4416 | currSlice->listX[1][i] = p_Vid->no_reference_picture; |
4417 | |
4418 | #if PRINTREFLIST |
4419 | #if (MVC_EXTENSION_ENABLE) |
4420 | /* print out for h264_debug_flag purpose */ |
4421 | if ((p_Vid->profile_idc == MVC_HIGH || |
4422 | p_Vid->profile_idc == STEREO_HIGH) && |
4423 | currSlice->current_slice_nr == 0) { |
4424 | if (currSlice->listXsize[0] > 0) { |
4425 | dpb_print(p_H264_Dpb->decoder_index, |
4426 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4427 | dpb_print(p_H264_Dpb->decoder_index, |
4428 | PRINT_FLAG_DPB_DETAIL, |
4429 | " ** (CurViewID:%d %d) %s Ref Pic List 0 ****\n", |
4430 | currSlice->view_id, |
4431 | currSlice->ThisPOC, |
4432 | currSlice->structure == FRAME ? "FRM" : |
4433 | (currSlice->structure == TOP_FIELD ? |
4434 | "TOP" : "BOT")); |
4435 | for (i = 0; i < (unsigned int)(currSlice-> |
4436 | listXsize[0]); i++) { /* ref list 0 */ |
4437 | dpb_print(p_H264_Dpb->decoder_index, |
4438 | PRINT_FLAG_DPB_DETAIL, |
4439 | " %2d -> POC: %4d PicNum: %4d ViewID: %d\n", |
4440 | i, |
4441 | currSlice->listX[0][i]->poc, |
4442 | currSlice->listX[0][i]->pic_num, |
4443 | currSlice->listX[0][i]->view_id); |
4444 | } |
4445 | } |
4446 | } |
4447 | #endif |
4448 | #endif |
4449 | } |
4450 | |
4451 | |
4452 | /*! |
4453 | ************************************************************************ |
4454 | * \brief |
4455 | * Initialize reference lists |
4456 | * |
4457 | ************************************************************************ |
4458 | */ |
4459 | static void init_mbaff_lists(struct h264_dpb_stru *p_H264_Dpb, |
4460 | struct Slice *currSlice) |
4461 | { |
4462 | unsigned int j; |
4463 | int i; |
4464 | struct VideoParameters *p_Vid = &p_H264_Dpb->mVideo; |
4465 | for (i = 2; i < 6; i++) { |
4466 | for (j = 0; j < MAX_LIST_SIZE; j++) |
4467 | currSlice->listX[i][j] = p_Vid->no_reference_picture; |
4468 | currSlice->listXsize[i] = 0; |
4469 | } |
4470 | |
4471 | for (i = 0; i < currSlice->listXsize[0]; i++) { |
4472 | #ifdef ERROR_CHECK |
4473 | if (currSlice->listX[0][i] == NULL) { |
4474 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4475 | pr_info( |
4476 | "error currSlice->listX[0][%d] is NULL\r\n", i); |
4477 | break; |
4478 | } |
4479 | #endif |
4480 | currSlice->listX[2][2 * i] = |
4481 | currSlice->listX[0][i]->top_field; |
4482 | currSlice->listX[2][2 * i + 1] = |
4483 | currSlice->listX[0][i]->bottom_field; |
4484 | currSlice->listX[4][2 * i] = |
4485 | currSlice->listX[0][i]->bottom_field; |
4486 | currSlice->listX[4][2 * i + 1] = |
4487 | currSlice->listX[0][i]->top_field; |
4488 | } |
4489 | currSlice->listXsize[2] = currSlice->listXsize[4] = |
4490 | currSlice->listXsize[0] * 2; |
4491 | |
4492 | for (i = 0; i < currSlice->listXsize[1]; i++) { |
4493 | #ifdef ERROR_CHECK |
4494 | if (currSlice->listX[1][i] == NULL) { |
4495 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4496 | pr_info( |
4497 | "error currSlice->listX[1][%d] is NULL\r\n", i); |
4498 | break; |
4499 | } |
4500 | #endif |
4501 | currSlice->listX[3][2 * i] = |
4502 | currSlice->listX[1][i]->top_field; |
4503 | currSlice->listX[3][2 * i + 1] = |
4504 | currSlice->listX[1][i]->bottom_field; |
4505 | currSlice->listX[5][2 * i] = |
4506 | currSlice->listX[1][i]->bottom_field; |
4507 | currSlice->listX[5][2 * i + 1] = |
4508 | currSlice->listX[1][i]->top_field; |
4509 | } |
4510 | currSlice->listXsize[3] = currSlice->listXsize[5] = |
4511 | currSlice->listXsize[1] * 2; |
4512 | } |
4513 | |
4514 | |
4515 | |
4516 | static void init_lists_i_slice(struct Slice *currSlice) |
4517 | { |
4518 | |
4519 | #if (MVC_EXTENSION_ENABLE) |
4520 | currSlice->listinterviewidx0 = 0; |
4521 | currSlice->listinterviewidx1 = 0; |
4522 | #endif |
4523 | |
4524 | currSlice->listXsize[0] = 0; |
4525 | currSlice->listXsize[1] = 0; |
4526 | } |
4527 | |
4528 | static void init_lists_b_slice(struct Slice *currSlice) |
4529 | { |
4530 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
4531 | struct DecodedPictureBuffer *p_Dpb = currSlice->p_Dpb; |
4532 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
4533 | struct h264_dpb_stru, mDPB); |
4534 | |
4535 | unsigned int i; |
4536 | int j; |
4537 | |
4538 | int list0idx = 0; |
4539 | int list0idx_1 = 0; |
4540 | int listltidx = 0; |
4541 | |
4542 | struct FrameStore **fs_list0; |
4543 | struct FrameStore **fs_list1; |
4544 | struct FrameStore **fs_listlt; |
4545 | |
4546 | #if (MVC_EXTENSION_ENABLE) |
4547 | currSlice->listinterviewidx0 = 0; |
4548 | currSlice->listinterviewidx1 = 0; |
4549 | #endif |
4550 | |
4551 | { |
4552 | /* B-Slice */ |
4553 | if (currSlice->structure == FRAME) { |
4554 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4555 | #ifdef ERROR_CHECK |
4556 | if (p_Dpb->fs_ref[i] == NULL || |
4557 | p_Dpb->fs_ref[i]->frame == NULL) { |
4558 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4559 | continue; |
4560 | } |
4561 | #endif |
4562 | if ((p_Dpb->fs_ref[i]->is_used == 3) && |
4563 | ((p_Dpb->fs_ref[i]->frame-> |
4564 | used_for_reference) && |
4565 | (!p_Dpb->fs_ref[i]->frame-> |
4566 | is_long_term)) && |
4567 | (currSlice->framepoc >= |
4568 | p_Dpb->fs_ref[i]->frame->poc)) { |
4569 | /* !KS use >= for error |
4570 | * concealment |
4571 | */ |
4572 | currSlice->listX[0][list0idx++] = |
4573 | p_Dpb->fs_ref[i]->frame; |
4574 | } |
4575 | } |
4576 | qsort((void *)currSlice->listX[0], list0idx, |
4577 | sizeof(struct StorablePicture *), |
4578 | compare_pic_by_poc_desc); |
4579 | |
4580 | /* get the backward reference picture |
4581 | * (POC>current POC) in list0; |
4582 | */ |
4583 | list0idx_1 = list0idx; |
4584 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4585 | #ifdef ERROR_CHECK |
4586 | if (p_Dpb->fs_ref[i] == NULL || |
4587 | p_Dpb->fs_ref[i]->frame == NULL) { |
4588 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4589 | continue; |
4590 | } |
4591 | #endif |
4592 | if ((p_Dpb->fs_ref[i]->is_used == 3) && |
4593 | ((p_Dpb->fs_ref[i]->frame-> |
4594 | used_for_reference) && |
4595 | (!p_Dpb->fs_ref[i]->frame-> |
4596 | is_long_term)) && |
4597 | (currSlice->framepoc < |
4598 | p_Dpb->fs_ref[i]->frame->poc)) { |
4599 | currSlice-> |
4600 | listX[0][list0idx++] = |
4601 | p_Dpb->fs_ref[i]->frame; |
4602 | } |
4603 | } |
4604 | qsort((void *)&currSlice->listX[0][list0idx_1], |
4605 | list0idx - list0idx_1, |
4606 | sizeof(struct StorablePicture *), |
4607 | compare_pic_by_poc_asc); |
4608 | |
4609 | for (j = 0; j < list0idx_1; j++) { |
4610 | currSlice-> |
4611 | listX[1][list0idx - list0idx_1 + j] = |
4612 | currSlice->listX[0][j]; |
4613 | } |
4614 | for (j = list0idx_1; j < list0idx; j++) { |
4615 | currSlice->listX[1][j - list0idx_1] = |
4616 | currSlice->listX[0][j]; |
4617 | } |
4618 | |
4619 | currSlice->listXsize[0] = currSlice->listXsize[1] = |
4620 | (char) list0idx; |
4621 | CHECK_VALID(currSlice->listXsize[0], 0); |
4622 | CHECK_VALID(currSlice->listXsize[1], 1); |
4623 | |
4624 | dpb_print(p_H264_Dpb->decoder_index, |
4625 | PRINT_FLAG_DPB_DETAIL, |
4626 | "listX[0] (PicNum): "); |
4627 | for (i = 0; i < currSlice->listXsize[0]; i++) { |
4628 | dpb_print(p_H264_Dpb->decoder_index, |
4629 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4630 | currSlice->listX[0][i]->pic_num); |
4631 | } |
4632 | dpb_print(p_H264_Dpb->decoder_index, |
4633 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4634 | dpb_print(p_H264_Dpb->decoder_index, |
4635 | PRINT_FLAG_DPB_DETAIL, |
4636 | "listX[1] (PicNum): "); |
4637 | for (i = 0; i < currSlice->listXsize[1]; i++) { |
4638 | dpb_print(p_H264_Dpb->decoder_index, |
4639 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4640 | currSlice->listX[1][i]->pic_num); |
4641 | } |
4642 | dpb_print(p_H264_Dpb->decoder_index, |
4643 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4644 | /* dpb_print(p_H264_Dpb->decoder_index, |
4645 | * PRINT_FLAG_DPB_DETAIL, |
4646 | * "currSlice->listX[0] currPoc=%d (Poc): ", |
4647 | * p_Vid->framepoc); |
4648 | * for (i=0; i<currSlice->listXsize[0]; i++) { |
4649 | * dpb_print(p_H264_Dpb->decoder_index, |
4650 | * PRINT_FLAG_DPB_DETAIL, |
4651 | * "%d ", currSlice->listX[0][i]->poc); |
4652 | * } |
4653 | * dpb_print(p_H264_Dpb->decoder_index, |
4654 | * PRINT_FLAG_DPB_DETAIL, "\n"); |
4655 | * dpb_print(p_H264_Dpb->decoder_index, |
4656 | * PRINT_FLAG_DPB_DETAIL, |
4657 | * "currSlice->listX[1] currPoc=%d (Poc): ", |
4658 | * p_Vid->framepoc); |
4659 | * for (i=0; i<currSlice->listXsize[1]; i++) { |
4660 | * dpb_print(p_H264_Dpb->decoder_index, |
4661 | * PRINT_FLAG_DPB_DETAIL, |
4662 | * "%d ", |
4663 | * currSlice->listX[1][i]->poc); |
4664 | * } |
4665 | * dpb_print(p_H264_Dpb->decoder_index, |
4666 | * PRINT_FLAG_DPB_DETAIL, "\n"); |
4667 | */ |
4668 | |
4669 | /* long term handling */ |
4670 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
4671 | if (p_Dpb->fs_ltref[i]->is_used == 3) { |
4672 | if (p_Dpb->fs_ltref[i]->frame-> |
4673 | is_long_term) { |
4674 | currSlice-> |
4675 | listX[0][list0idx] = |
4676 | p_Dpb->fs_ltref[i]->frame; |
4677 | currSlice-> |
4678 | listX[1][list0idx++] = |
4679 | p_Dpb->fs_ltref[i]->frame; |
4680 | } |
4681 | } |
4682 | } |
4683 | qsort((void *)&currSlice-> |
4684 | listX[0][(short) currSlice->listXsize[0]], |
4685 | list0idx - currSlice->listXsize[0], |
4686 | sizeof(struct StorablePicture *), |
4687 | compare_pic_by_lt_pic_num_asc); |
4688 | qsort((void *)&currSlice-> |
4689 | listX[1][(short) currSlice->listXsize[0]], |
4690 | list0idx - currSlice->listXsize[0], |
4691 | sizeof(struct StorablePicture *), |
4692 | compare_pic_by_lt_pic_num_asc); |
4693 | currSlice->listXsize[0] = currSlice->listXsize[1] = |
4694 | (char) list0idx; |
4695 | CHECK_VALID(currSlice->listXsize[0], 0); |
4696 | CHECK_VALID(currSlice->listXsize[1], 1); |
4697 | } else { |
4698 | #if 0 |
4699 | fs_list0 = calloc(p_Dpb->size, |
4700 | sizeof(struct FrameStore *)); |
4701 | if (fs_list0 == NULL) |
4702 | no_mem_exit("init_lists: fs_list0"); |
4703 | fs_list1 = calloc(p_Dpb->size, |
4704 | sizeof(struct FrameStore *)); |
4705 | if (fs_list1 == NULL) |
4706 | no_mem_exit("init_lists: fs_list1"); |
4707 | fs_listlt = calloc(p_Dpb->size, |
4708 | sizeof(struct FrameStore *)); |
4709 | if (fs_listlt == NULL) |
4710 | no_mem_exit("init_lists: fs_listlt"); |
4711 | #else |
4712 | fs_list0 = &(p_Dpb->fs_list0[0]); |
4713 | fs_list1 = &(p_Dpb->fs_list1[0]); |
4714 | fs_listlt = &(p_Dpb->fs_listlt[0]); |
4715 | |
4716 | #endif |
4717 | currSlice->listXsize[0] = 0; |
4718 | currSlice->listXsize[1] = 1; |
4719 | |
4720 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4721 | #ifdef ERROR_CHECK |
4722 | if (p_Dpb->fs_ref[i] == NULL) { |
4723 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4724 | continue; |
4725 | } |
4726 | #endif |
4727 | if (p_Dpb->fs_ref[i]->is_used) { |
4728 | if (currSlice->ThisPOC >= |
4729 | p_Dpb->fs_ref[i]->poc) { |
4730 | fs_list0[list0idx++] = |
4731 | p_Dpb->fs_ref[i]; |
4732 | } |
4733 | } |
4734 | } |
4735 | qsort((void *)fs_list0, list0idx, |
4736 | sizeof(struct FrameStore *), |
4737 | compare_fs_by_poc_desc); |
4738 | list0idx_1 = list0idx; |
4739 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4740 | #ifdef ERROR_CHECK |
4741 | if (p_Dpb->fs_ref[i] == NULL) { |
4742 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4743 | continue; |
4744 | } |
4745 | #endif |
4746 | if (p_Dpb->fs_ref[i]->is_used) { |
4747 | if (currSlice->ThisPOC < |
4748 | p_Dpb->fs_ref[i]->poc) { |
4749 | fs_list0[list0idx++] = |
4750 | p_Dpb->fs_ref[i]; |
4751 | } |
4752 | } |
4753 | } |
4754 | qsort((void *)&fs_list0[list0idx_1], |
4755 | list0idx - list0idx_1, |
4756 | sizeof(struct FrameStore *), |
4757 | compare_fs_by_poc_asc); |
4758 | |
4759 | for (j = 0; j < list0idx_1; j++) { |
4760 | fs_list1[list0idx - list0idx_1 + j] = |
4761 | fs_list0[j]; |
4762 | } |
4763 | for (j = list0idx_1; j < list0idx; j++) |
4764 | fs_list1[j - list0idx_1] = fs_list0[j]; |
4765 | |
4766 | dpb_print(p_H264_Dpb->decoder_index, |
4767 | PRINT_FLAG_DPB_DETAIL, |
4768 | "fs_list0 currPoc=%d (Poc): ", |
4769 | currSlice->ThisPOC); |
4770 | for (i = 0; i < list0idx; i++) { |
4771 | dpb_print(p_H264_Dpb->decoder_index, |
4772 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4773 | fs_list0[i]->poc); |
4774 | } |
4775 | dpb_print(p_H264_Dpb->decoder_index, |
4776 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4777 | dpb_print(p_H264_Dpb->decoder_index, |
4778 | PRINT_FLAG_DPB_DETAIL, |
4779 | "fs_list1 currPoc=%d (Poc): ", |
4780 | currSlice->ThisPOC); |
4781 | for (i = 0; i < list0idx; i++) { |
4782 | dpb_print(p_H264_Dpb->decoder_index, |
4783 | PRINT_FLAG_DPB_DETAIL, "%d ", |
4784 | fs_list1[i]->poc); |
4785 | } |
4786 | dpb_print(p_H264_Dpb->decoder_index, |
4787 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4788 | |
4789 | currSlice->listXsize[0] = 0; |
4790 | currSlice->listXsize[1] = 0; |
4791 | gen_pic_list_from_frame_list(currSlice->structure, |
4792 | fs_list0, list0idx, |
4793 | currSlice->listX[0], |
4794 | &currSlice->listXsize[0], 0); |
4795 | gen_pic_list_from_frame_list(currSlice->structure, |
4796 | fs_list1, list0idx, |
4797 | currSlice->listX[1], |
4798 | &currSlice->listXsize[1], 0); |
4799 | |
4800 | /* dpb_print(p_H264_Dpb->decoder_index, |
4801 | * PRINT_FLAG_DPB_DETAIL, |
4802 | * "currSlice->listX[0] currPoc=%d (Poc): ", |
4803 | * p_Vid->framepoc); |
4804 | * for (i=0; i<currSlice->listXsize[0]; i++) { |
4805 | * dpb_print(p_H264_Dpb->decoder_index, |
4806 | * PRINT_FLAG_DPB_DETAIL, "%d ", |
4807 | * currSlice->listX[0][i]->poc); |
4808 | * } |
4809 | * dpb_print(p_H264_Dpb->decoder_index, |
4810 | * PRINT_FLAG_DPB_DETAIL, "\n"); |
4811 | */ |
4812 | /* dpb_print(p_H264_Dpb->decoder_index, |
4813 | * PRINT_FLAG_DPB_DETAIL, |
4814 | * "currSlice->listX[1] currPoc=%d (Poc): ", |
4815 | * p_Vid->framepoc); |
4816 | * for (i=0; i<currSlice->listXsize[1]; i++) { |
4817 | * dpb_print(p_H264_Dpb->decoder_index, |
4818 | * PRINT_FLAG_DPB_DETAIL, "%d ", |
4819 | * currSlice->listX[1][i]->poc); |
4820 | * } |
4821 | * dpb_print(p_H264_Dpb->decoder_index, |
4822 | * PRINT_FLAG_DPB_DETAIL, |
4823 | * "\n"); |
4824 | */ |
4825 | |
4826 | /* long term handling */ |
4827 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) |
4828 | fs_listlt[listltidx++] = p_Dpb->fs_ltref[i]; |
4829 | |
4830 | qsort((void *)fs_listlt, listltidx, |
4831 | sizeof(struct FrameStore *), |
4832 | compare_fs_by_lt_pic_idx_asc); |
4833 | |
4834 | gen_pic_list_from_frame_list(currSlice->structure, |
4835 | fs_listlt, listltidx, |
4836 | currSlice->listX[0], |
4837 | &currSlice->listXsize[0], 1); |
4838 | gen_pic_list_from_frame_list(currSlice->structure, |
4839 | fs_listlt, listltidx, |
4840 | currSlice->listX[1], |
4841 | &currSlice->listXsize[1], 1); |
4842 | |
4843 | /* free(fs_list0); */ |
4844 | /* free(fs_list1); */ |
4845 | /* free(fs_listlt); */ |
4846 | } |
4847 | } |
4848 | |
4849 | if ((currSlice->listXsize[0] == currSlice->listXsize[1]) && |
4850 | (currSlice->listXsize[0] > 1)) { |
4851 | /* check if lists are identical, |
4852 | *if yes swap first two elements of currSlice->listX[1] |
4853 | */ |
4854 | int diff = 0; |
4855 | |
4856 | for (j = 0; j < currSlice->listXsize[0]; j++) { |
4857 | if (currSlice->listX[0][j] != |
4858 | currSlice->listX[1][j]) { |
4859 | diff = 1; |
4860 | break; |
4861 | } |
4862 | } |
4863 | if (!diff) { |
4864 | struct StorablePicture *tmp_s = |
4865 | currSlice->listX[1][0]; |
4866 | currSlice->listX[1][0] = currSlice->listX[1][1]; |
4867 | currSlice->listX[1][1] = tmp_s; |
4868 | } |
4869 | } |
4870 | |
4871 | /* set max size */ |
4872 | currSlice->listXsize[0] = (char) imin(currSlice->listXsize[0], |
4873 | currSlice->num_ref_idx_active[LIST_0]); |
4874 | currSlice->listXsize[1] = (char) imin(currSlice->listXsize[1], |
4875 | currSlice->num_ref_idx_active[LIST_1]); |
4876 | CHECK_VALID(currSlice->listXsize[0], 0); |
4877 | CHECK_VALID(currSlice->listXsize[1], 1); |
4878 | |
4879 | /* set the unused list entries to NULL */ |
4880 | for (i = currSlice->listXsize[0]; i < (MAX_LIST_SIZE); i++) |
4881 | currSlice->listX[0][i] = p_Vid->no_reference_picture; |
4882 | for (i = currSlice->listXsize[1]; i < (MAX_LIST_SIZE); i++) |
4883 | currSlice->listX[1][i] = p_Vid->no_reference_picture; |
4884 | |
4885 | #if PRINTREFLIST |
4886 | #if (MVC_EXTENSION_ENABLE) |
4887 | /* print out for h264_debug_flag purpose */ |
4888 | if ((p_Vid->profile_idc == MVC_HIGH || |
4889 | p_Vid->profile_idc == STEREO_HIGH) && |
4890 | currSlice->current_slice_nr == 0) { |
4891 | if ((currSlice->listXsize[0] > 0) || |
4892 | (currSlice->listXsize[1] > 0)) |
4893 | dpb_print(p_H264_Dpb->decoder_index, |
4894 | PRINT_FLAG_DPB_DETAIL, "\n"); |
4895 | if (currSlice->listXsize[0] > 0) { |
4896 | dpb_print(p_H264_Dpb->decoder_index, |
4897 | PRINT_FLAG_DPB_DETAIL, |
4898 | " ** (CurViewID:%d %d) %s Ref Pic List 0 ****\n", |
4899 | currSlice->view_id, |
4900 | currSlice->ThisPOC, |
4901 | currSlice->structure == FRAME ? "FRM" : |
4902 | (currSlice->structure == TOP_FIELD ? |
4903 | "TOP" : "BOT")); |
4904 | for (i = 0; i < (unsigned int)(currSlice-> |
4905 | listXsize[0]); i++) { /* ref list 0 */ |
4906 | dpb_print(p_H264_Dpb->decoder_index, |
4907 | PRINT_FLAG_DPB_DETAIL, |
4908 | " %2d -> POC: %4d PicNum: %4d ViewID: %d\n", |
4909 | i, |
4910 | currSlice->listX[0][i]->poc, |
4911 | currSlice->listX[0][i]->pic_num, |
4912 | currSlice->listX[0][i]->view_id); |
4913 | } |
4914 | } |
4915 | if (currSlice->listXsize[1] > 0) { |
4916 | dpb_print(p_H264_Dpb->decoder_index, |
4917 | PRINT_FLAG_DPB_DETAIL, |
4918 | " ** (CurViewID:%d %d) %s Ref Pic List 1 ****\n", |
4919 | currSlice->view_id, |
4920 | currSlice->ThisPOC, |
4921 | currSlice->structure == FRAME ? "FRM" : |
4922 | (currSlice->structure == TOP_FIELD ? "TOP" : |
4923 | "BOT")); |
4924 | for (i = 0; i < (unsigned int)(currSlice-> |
4925 | listXsize[1]); i++) { /* ref list 1 */ |
4926 | dpb_print(p_H264_Dpb->decoder_index, |
4927 | PRINT_FLAG_DPB_DETAIL, |
4928 | " %2d -> POC: %4d PicNum: %4d ViewID: %d\n", |
4929 | i, |
4930 | currSlice->listX[1][i]->poc, |
4931 | currSlice->listX[1][i]->pic_num, |
4932 | currSlice->listX[1][i]->view_id); |
4933 | } |
4934 | } |
4935 | } |
4936 | #endif |
4937 | #endif |
4938 | } |
4939 | |
4940 | static struct StorablePicture *get_short_term_pic(struct Slice *currSlice, |
4941 | struct DecodedPictureBuffer *p_Dpb, int picNum) |
4942 | { |
4943 | unsigned int i; |
4944 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Dpb, |
4945 | struct h264_dpb_stru, mDPB); |
4946 | for (i = 0; i < p_Dpb->ref_frames_in_buffer; i++) { |
4947 | if (currSlice->structure == FRAME) { |
4948 | #ifdef ERROR_CHECK |
4949 | if (p_Dpb->fs_ref[i] == NULL) { |
4950 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4951 | continue; |
4952 | } |
4953 | #endif |
4954 | if (p_Dpb->fs_ref[i]->is_reference == 3) { |
4955 | #ifdef ERROR_CHECK |
4956 | if (p_Dpb->fs_ref[i]->frame == NULL) { |
4957 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4958 | continue; |
4959 | } |
4960 | #endif |
4961 | if ((!p_Dpb->fs_ref[i]->frame-> |
4962 | is_long_term) && |
4963 | (p_Dpb->fs_ref[i]->frame-> |
4964 | pic_num == picNum)) |
4965 | return p_Dpb->fs_ref[i]->frame; |
4966 | } |
4967 | } else { |
4968 | #ifdef ERROR_CHECK |
4969 | if (p_Dpb->fs_ref[i] == NULL) { |
4970 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4971 | continue; |
4972 | } |
4973 | #endif |
4974 | if (p_Dpb->fs_ref[i]->is_reference & 1) { |
4975 | #ifdef ERROR_CHECK |
4976 | if (p_Dpb->fs_ref[i]->top_field == NULL) { |
4977 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4978 | continue; |
4979 | } |
4980 | #endif |
4981 | if ((!p_Dpb->fs_ref[i]->top_field-> |
4982 | is_long_term) && |
4983 | (p_Dpb->fs_ref[i]->top_field-> |
4984 | pic_num == picNum)) |
4985 | return p_Dpb->fs_ref[i]->top_field; |
4986 | } |
4987 | if (p_Dpb->fs_ref[i]->is_reference & 2) { |
4988 | #ifdef ERROR_CHECK |
4989 | if (p_Dpb->fs_ref[i]->bottom_field == NULL) { |
4990 | p_H264_Dpb->dpb_error_flag = __LINE__; |
4991 | continue; |
4992 | } |
4993 | #endif |
4994 | if ((!p_Dpb->fs_ref[i]->bottom_field-> |
4995 | is_long_term) && |
4996 | (p_Dpb->fs_ref[i]->bottom_field-> |
4997 | pic_num == picNum)) |
4998 | return p_Dpb->fs_ref[i]->bottom_field; |
4999 | } |
5000 | } |
5001 | } |
5002 | |
5003 | return currSlice->p_Vid->no_reference_picture; |
5004 | } |
5005 | |
5006 | |
5007 | static void reorder_short_term(struct Slice *currSlice, int cur_list, |
5008 | int num_ref_idx_lX_active_minus1, |
5009 | int picNumLX, int *refIdxLX) |
5010 | { |
5011 | struct h264_dpb_stru *p_H264_Dpb = container_of(currSlice->p_Vid, |
5012 | struct h264_dpb_stru, mVideo); |
5013 | |
5014 | struct StorablePicture **RefPicListX = currSlice->listX[cur_list]; |
5015 | int cIdx, nIdx; |
5016 | |
5017 | struct StorablePicture *picLX; |
5018 | |
5019 | picLX = get_short_term_pic(currSlice, currSlice->p_Dpb, picNumLX); |
5020 | |
5021 | for (cIdx = num_ref_idx_lX_active_minus1 + 1; cIdx > *refIdxLX; |
5022 | cIdx--) { |
5023 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
5024 | "%s: RefPicListX[ %d ] = RefPicListX[ %d ]\n", |
5025 | __func__, cIdx, cIdx - 1); |
5026 | RefPicListX[cIdx] = RefPicListX[cIdx - 1]; |
5027 | } |
5028 | |
5029 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
5030 | "%s: RefPicListX[ %d ] = pic %x (%d)\n", __func__, |
5031 | *refIdxLX, picLX, picNumLX); |
5032 | |
5033 | RefPicListX[(*refIdxLX)++] = picLX; |
5034 | |
5035 | nIdx = *refIdxLX; |
5036 | |
5037 | for (cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1 + 1; |
5038 | cIdx++) { |
5039 | if (RefPicListX[cIdx]) |
5040 | if ((RefPicListX[cIdx]->is_long_term) || |
5041 | (RefPicListX[cIdx]->pic_num != picNumLX)) { |
5042 | dpb_print(p_H264_Dpb->decoder_index, |
5043 | PRINT_FLAG_DPB_DETAIL, |
5044 | "%s: RefPicListX[ %d ] = RefPicListX[ %d ]\n", |
5045 | __func__, nIdx, cIdx); |
5046 | RefPicListX[nIdx++] = RefPicListX[cIdx]; |
5047 | } |
5048 | } |
5049 | } |
5050 | |
5051 | |
5052 | static struct StorablePicture *get_long_term_pic(struct Slice *currSlice, |
5053 | struct DecodedPictureBuffer *p_Dpb, int LongtermPicNum) |
5054 | { |
5055 | unsigned int i; |
5056 | |
5057 | for (i = 0; i < p_Dpb->ltref_frames_in_buffer; i++) { |
5058 | if (currSlice->structure == FRAME) { |
5059 | if (p_Dpb->fs_ltref[i]->is_reference == 3) |
5060 | if ((p_Dpb->fs_ltref[i]->frame) && |
5061 | (p_Dpb->fs_ltref[i]->frame-> |
5062 | is_long_term) && |
5063 | (p_Dpb->fs_ltref[i]->frame-> |
5064 | long_term_pic_num == |
5065 | LongtermPicNum)) |
5066 | return p_Dpb->fs_ltref[i]->frame; |
5067 | } else { |
5068 | if (p_Dpb->fs_ltref[i]->is_reference & 1) |
5069 | if ((p_Dpb->fs_ltref[i]->top_field) && |
5070 | (p_Dpb->fs_ltref[i]->top_field-> |
5071 | is_long_term) && |
5072 | (p_Dpb->fs_ltref[i]->top_field-> |
5073 | long_term_pic_num == LongtermPicNum)) |
5074 | return p_Dpb->fs_ltref[i]->top_field; |
5075 | |
5076 | if (p_Dpb->fs_ltref[i]->is_reference & 2) |
5077 | if ((p_Dpb->fs_ltref[i]->bottom_field) && |
5078 | (p_Dpb->fs_ltref[i]->bottom_field-> |
5079 | is_long_term) && |
5080 | (p_Dpb->fs_ltref[i]->bottom_field-> |
5081 | long_term_pic_num == |
5082 | LongtermPicNum)) |
5083 | return p_Dpb->fs_ltref[i]-> |
5084 | bottom_field; |
5085 | } |
5086 | } |
5087 | return NULL; |
5088 | } |
5089 | |
5090 | /*! |
5091 | ************************************************************************ |
5092 | * \brief |
5093 | * Reordering process for long-term reference pictures |
5094 | * |
5095 | ************************************************************************ |
5096 | */ |
5097 | static void reorder_long_term(struct Slice *currSlice, |
5098 | struct StorablePicture **RefPicListX, |
5099 | int num_ref_idx_lX_active_minus1, |
5100 | int LongTermPicNum, int *refIdxLX) |
5101 | { |
5102 | int cIdx, nIdx; |
5103 | |
5104 | struct StorablePicture *picLX; |
5105 | |
5106 | picLX = get_long_term_pic(currSlice, currSlice->p_Dpb, LongTermPicNum); |
5107 | |
5108 | for (cIdx = num_ref_idx_lX_active_minus1 + 1; cIdx > *refIdxLX; cIdx--) |
5109 | RefPicListX[cIdx] = RefPicListX[cIdx - 1]; |
5110 | |
5111 | RefPicListX[(*refIdxLX)++] = picLX; |
5112 | |
5113 | nIdx = *refIdxLX; |
5114 | |
5115 | for (cIdx = *refIdxLX; cIdx <= num_ref_idx_lX_active_minus1 + 1; |
5116 | cIdx++) { |
5117 | if (RefPicListX[cIdx]) { |
5118 | if ((!RefPicListX[cIdx]->is_long_term) || |
5119 | (RefPicListX[cIdx]->long_term_pic_num != |
5120 | LongTermPicNum)) |
5121 | RefPicListX[nIdx++] = RefPicListX[cIdx]; |
5122 | } |
5123 | } |
5124 | } |
5125 | |
5126 | static void reorder_ref_pic_list(struct Slice *currSlice, int cur_list) |
5127 | { |
5128 | int *modification_of_pic_nums_idc = |
5129 | currSlice->modification_of_pic_nums_idc[cur_list]; |
5130 | int *abs_diff_pic_num_minus1 = |
5131 | currSlice->abs_diff_pic_num_minus1[cur_list]; |
5132 | int *long_term_pic_idx = currSlice->long_term_pic_idx[cur_list]; |
5133 | int num_ref_idx_lX_active_minus1 = |
5134 | currSlice->num_ref_idx_active[cur_list] - 1; |
5135 | |
5136 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
5137 | int i; |
5138 | |
5139 | int maxPicNum, currPicNum, picNumLXNoWrap, picNumLXPred, picNumLX; |
5140 | int refIdxLX = 0; |
5141 | |
5142 | if (currSlice->structure == FRAME) { |
5143 | maxPicNum = p_Vid->max_frame_num; |
5144 | currPicNum = currSlice->frame_num; |
5145 | } else { |
5146 | maxPicNum = 2 * p_Vid->max_frame_num; |
5147 | currPicNum = 2 * currSlice->frame_num + 1; |
5148 | } |
5149 | |
5150 | picNumLXPred = currPicNum; |
5151 | |
5152 | for (i = 0; i < REORDERING_COMMAND_MAX_SIZE && |
5153 | modification_of_pic_nums_idc[i] != 3; i++) { |
5154 | if (modification_of_pic_nums_idc[i] > 3) { |
5155 | struct h264_dpb_stru *p_H264_Dpb = |
5156 | container_of(p_Vid, struct h264_dpb_stru, mVideo); |
5157 | dpb_print(p_H264_Dpb->decoder_index, |
5158 | PRINT_FLAG_ERROR, |
5159 | "error, Invalid modification_of_pic_nums_idc command\n"); |
5160 | /*h264_debug_flag = 0x1f;*/ |
5161 | break; |
5162 | } |
5163 | if (modification_of_pic_nums_idc[i] < 2) { |
5164 | if (modification_of_pic_nums_idc[i] == 0) { |
5165 | if (picNumLXPred - (abs_diff_pic_num_minus1[i] |
5166 | + 1) < 0) |
5167 | picNumLXNoWrap = picNumLXPred - |
5168 | (abs_diff_pic_num_minus1[i] + 1) + |
5169 | maxPicNum; |
5170 | else |
5171 | picNumLXNoWrap = picNumLXPred - |
5172 | (abs_diff_pic_num_minus1[i] + 1); |
5173 | } else { /* (modification_of_pic_nums_idc[i] == 1) */ |
5174 | if (picNumLXPred + (abs_diff_pic_num_minus1[i] |
5175 | + 1) >= maxPicNum) |
5176 | picNumLXNoWrap = picNumLXPred + |
5177 | (abs_diff_pic_num_minus1[i] + 1) - |
5178 | maxPicNum; |
5179 | else |
5180 | picNumLXNoWrap = picNumLXPred + |
5181 | (abs_diff_pic_num_minus1[i] + 1); |
5182 | } |
5183 | picNumLXPred = picNumLXNoWrap; |
5184 | |
5185 | if (picNumLXNoWrap > currPicNum) |
5186 | picNumLX = picNumLXNoWrap - maxPicNum; |
5187 | else |
5188 | picNumLX = picNumLXNoWrap; |
5189 | |
5190 | #if (MVC_EXTENSION_ENABLE) |
5191 | reorder_short_term(currSlice, cur_list, |
5192 | num_ref_idx_lX_active_minus1, picNumLX, |
5193 | &refIdxLX, -1); |
5194 | #else |
5195 | reorder_short_term(currSlice, cur_list, |
5196 | num_ref_idx_lX_active_minus1, picNumLX, |
5197 | &refIdxLX); |
5198 | #endif |
5199 | } else { /* (modification_of_pic_nums_idc[i] == 2) */ |
5200 | #if (MVC_EXTENSION_ENABLE) |
5201 | reorder_long_term(currSlice, currSlice->listX[cur_list], |
5202 | num_ref_idx_lX_active_minus1, |
5203 | long_term_pic_idx[i], &refIdxLX, -1); |
5204 | #else |
5205 | reorder_long_term(currSlice, currSlice->listX[cur_list], |
5206 | num_ref_idx_lX_active_minus1, |
5207 | long_term_pic_idx[i], &refIdxLX); |
5208 | #endif |
5209 | } |
5210 | |
5211 | } |
5212 | /* that's a definition */ |
5213 | currSlice->listXsize[cur_list] = |
5214 | (char)(num_ref_idx_lX_active_minus1 + 1); |
5215 | } |
5216 | |
5217 | static void reorder_lists(struct Slice *currSlice) |
5218 | { |
5219 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
5220 | struct h264_dpb_stru *p_H264_Dpb = container_of(p_Vid, |
5221 | struct h264_dpb_stru, mVideo); |
5222 | int i; |
5223 | |
5224 | if ((currSlice->slice_type != I_SLICE) && |
5225 | (currSlice->slice_type != SI_SLICE)) { |
5226 | if (currSlice->ref_pic_list_reordering_flag[LIST_0]) |
5227 | reorder_ref_pic_list(currSlice, LIST_0); |
5228 | if (p_Vid->no_reference_picture == |
5229 | currSlice-> |
5230 | listX[0][currSlice->num_ref_idx_active[LIST_0] - 1]) { |
5231 | if (p_Vid->non_conforming_stream) |
5232 | dpb_print(p_H264_Dpb->decoder_index, |
5233 | PRINT_FLAG_DPB_DETAIL, |
5234 | "RefPicList0[ %d ] is equal to 'no reference picture'\n", |
5235 | currSlice-> |
5236 | num_ref_idx_active[LIST_0] - 1); |
5237 | else |
5238 | dpb_print(p_H264_Dpb->decoder_index, |
5239 | PRINT_FLAG_DPB_DETAIL, |
5240 | "RefPicList0 [ num_ref_idx_l0_active_minus1 ] is equal to 'no reference picture', invalid bitstream %d\n", |
5241 | 500); |
5242 | } |
5243 | /* that's a definition */ |
5244 | currSlice->listXsize[0] = |
5245 | (char)currSlice->num_ref_idx_active[LIST_0]; |
5246 | CHECK_VALID(currSlice->listXsize[0], 0); |
5247 | if (h264_debug_flag & PRINT_FLAG_DPB_DETAIL) { |
5248 | dpb_print(p_H264_Dpb->decoder_index, |
5249 | PRINT_FLAG_DPB_DETAIL, |
5250 | "listX[0] reorder (PicNum): "); |
5251 | for (i = 0; i < currSlice->listXsize[0]; i++) { |
5252 | dpb_print_cont(p_H264_Dpb->decoder_index, |
5253 | PRINT_FLAG_DPB_DETAIL, "%d ", |
5254 | currSlice->listX[0][i]->pic_num); |
5255 | } |
5256 | dpb_print_cont(p_H264_Dpb->decoder_index, |
5257 | PRINT_FLAG_DPB_DETAIL, "\n"); |
5258 | } |
5259 | } |
5260 | |
5261 | if (currSlice->slice_type == B_SLICE) { |
5262 | if (currSlice->ref_pic_list_reordering_flag[LIST_1]) |
5263 | reorder_ref_pic_list(currSlice, LIST_1); |
5264 | if (p_Vid->no_reference_picture == |
5265 | currSlice->listX[1][currSlice-> |
5266 | num_ref_idx_active[LIST_1] - 1]) { |
5267 | if (p_Vid->non_conforming_stream) |
5268 | dpb_print(p_H264_Dpb->decoder_index, |
5269 | PRINT_FLAG_DPB_DETAIL, |
5270 | "RefPicList1[ %d ] is equal to 'no reference picture'\n", |
5271 | currSlice-> |
5272 | num_ref_idx_active[LIST_1] - 1); |
5273 | else |
5274 | dpb_print(p_H264_Dpb->decoder_index, |
5275 | PRINT_FLAG_DPB_DETAIL, |
5276 | "RefPicList1 [ num_ref_idx_l1_active_minus1 ] is equal to 'no reference picture', invalid bitstream %d\n", |
5277 | 500); |
5278 | } |
5279 | /* that's a definition */ |
5280 | currSlice->listXsize[1] = |
5281 | (char)currSlice->num_ref_idx_active[LIST_1]; |
5282 | if (h264_debug_flag & PRINT_FLAG_DPB_DETAIL) { |
5283 | dpb_print(p_H264_Dpb->decoder_index, |
5284 | PRINT_FLAG_DPB_DETAIL, |
5285 | "listX[1] reorder (PicNum): "); |
5286 | for (i = 0; i < currSlice->listXsize[1]; i++) { |
5287 | dpb_print_cont(p_H264_Dpb->decoder_index, |
5288 | PRINT_FLAG_DPB_DETAIL, "%d ", |
5289 | currSlice->listX[1][i]->pic_num); |
5290 | } |
5291 | dpb_print_cont(p_H264_Dpb->decoder_index, |
5292 | PRINT_FLAG_DPB_DETAIL, "\n"); |
5293 | } |
5294 | } |
5295 | |
5296 | /* free_ref_pic_list_reordering_buffer(currSlice); */ |
5297 | |
5298 | if (currSlice->slice_type == P_SLICE) { |
5299 | #if PRINTREFLIST |
5300 | unsigned int i; |
5301 | #if (MVC_EXTENSION_ENABLE) |
5302 | /* print out for h264_debug_flag purpose */ |
5303 | if ((p_Vid->profile_idc == MVC_HIGH || |
5304 | p_Vid->profile_idc == STEREO_HIGH) && |
5305 | currSlice->current_slice_nr == 0) { |
5306 | if (currSlice->listXsize[0] > 0 |
5307 | && (h264_debug_flag & PRINT_FLAG_DPB_DETAIL)) { |
5308 | dpb_print(p_H264_Dpb->decoder_index, |
5309 | PRINT_FLAG_DPB_DETAIL, "\n"); |
5310 | dpb_print(p_H264_Dpb->decoder_index, |
5311 | PRINT_FLAG_DPB_DETAIL, |
5312 | " ** (FinalViewID:%d) %s Ref Pic List 0 ****\n", |
5313 | currSlice->view_id, |
5314 | currSlice->structure == FRAME ? |
5315 | "FRM" : |
5316 | (currSlice->structure == TOP_FIELD ? |
5317 | "TOP" : "BOT")); |
5318 | for (i = 0; i < (unsigned int)(currSlice-> |
5319 | listXsize[0]); i++) { /* ref list 0 */ |
5320 | dpb_print(p_H264_Dpb->decoder_index, |
5321 | PRINT_FLAG_DPB_DETAIL, |
5322 | " %2d -> POC: %4d PicNum: %4d ViewID: %d\n", |
5323 | i, |
5324 | currSlice->listX[0][i]->poc, |
5325 | currSlice->listX[0][i]-> |
5326 | pic_num, |
5327 | currSlice->listX[0][i]-> |
5328 | view_id); |
5329 | } |
5330 | } |
5331 | } |
5332 | #endif |
5333 | #endif |
5334 | } else if (currSlice->slice_type == B_SLICE) { |
5335 | #if PRINTREFLIST |
5336 | unsigned int i; |
5337 | #if (MVC_EXTENSION_ENABLE) |
5338 | /* print out for h264_debug_flag purpose */ |
5339 | if ((p_Vid->profile_idc == MVC_HIGH || |
5340 | p_Vid->profile_idc == STEREO_HIGH) && |
5341 | currSlice->current_slice_nr == 0) { |
5342 | if ((currSlice->listXsize[0] > 0) || |
5343 | (currSlice->listXsize[1] > 0)) |
5344 | dpb_print(p_H264_Dpb->decoder_index, |
5345 | PRINT_FLAG_DPB_DETAIL, "\n"); |
5346 | if (currSlice->listXsize[0] > 0 |
5347 | && (h264_debug_flag & PRINT_FLAG_DPB_DETAIL)) { |
5348 | dpb_print(p_H264_Dpb->decoder_index, |
5349 | PRINT_FLAG_DPB_DETAIL, |
5350 | " ** (FinalViewID:%d) %s Ref Pic List 0 ****\n", |
5351 | currSlice->view_id, |
5352 | currSlice->structure == FRAME ? |
5353 | "FRM" : |
5354 | (currSlice->structure == TOP_FIELD ? |
5355 | "TOP" : "BOT")); |
5356 | for (i = 0; i < (unsigned int)(currSlice-> |
5357 | listXsize[0]); i++) { /* ref list 0 */ |
5358 | dpb_print(p_H264_Dpb->decoder_index, |
5359 | PRINT_FLAG_DPB_DETAIL, |
5360 | " %2d -> POC: %4d PicNum: %4d ViewID: %d\n", |
5361 | i, |
5362 | currSlice->listX[0][i]->poc, |
5363 | currSlice->listX[0][i]-> |
5364 | pic_num, |
5365 | currSlice->listX[0][i]-> |
5366 | view_id); |
5367 | } |
5368 | } |
5369 | if (currSlice->listXsize[1] > 0 |
5370 | && (h264_debug_flag & PRINT_FLAG_DPB_DETAIL)) { |
5371 | dpb_print(p_H264_Dpb->decoder_index, |
5372 | PRINT_FLAG_DPB_DETAIL, |
5373 | " ** (FinalViewID:%d) %s Ref Pic List 1 ****\n", |
5374 | currSlice->view_id, |
5375 | currSlice->structure == FRAME ? |
5376 | "FRM" : |
5377 | (currSlice->structure == TOP_FIELD ? |
5378 | "TOP" : "BOT")); |
5379 | for (i = 0; i < (unsigned int)(currSlice-> |
5380 | listXsize[1]); i++) { /* ref list 1 */ |
5381 | dpb_print(p_H264_Dpb->decoder_index, |
5382 | PRINT_FLAG_DPB_DETAIL, |
5383 | " %2d -> POC: %4d PicNum: %4d ViewID: %d\n", |
5384 | i, |
5385 | currSlice->listX[1][i]->poc, |
5386 | currSlice->listX[1][i]-> |
5387 | pic_num, |
5388 | currSlice->listX[1][i]-> |
5389 | view_id); |
5390 | } |
5391 | } |
5392 | } |
5393 | #endif |
5394 | |
5395 | #endif |
5396 | } |
5397 | } |
5398 | |
5399 | void init_colocate_buf(struct h264_dpb_stru *p_H264_Dpb, int count) |
5400 | { |
5401 | p_H264_Dpb->colocated_buf_map = 0; |
5402 | p_H264_Dpb->colocated_buf_count = count; |
5403 | } |
5404 | |
5405 | int allocate_colocate_buf(struct h264_dpb_stru *p_H264_Dpb) |
5406 | { |
5407 | int i; |
5408 | |
5409 | for (i = 0; i < p_H264_Dpb->colocated_buf_count; i++) { |
5410 | if (((p_H264_Dpb->colocated_buf_map >> i) & 0x1) == 0) { |
5411 | p_H264_Dpb->colocated_buf_map |= (1 << i); |
5412 | break; |
5413 | } |
5414 | } |
5415 | if (i == p_H264_Dpb->colocated_buf_count) { |
5416 | i = -1; |
5417 | p_H264_Dpb->buf_alloc_fail = 1; |
5418 | } |
5419 | return i; |
5420 | } |
5421 | |
5422 | int release_colocate_buf(struct h264_dpb_stru *p_H264_Dpb, int index) |
5423 | { |
5424 | if (index >= 0) { |
5425 | if (index >= p_H264_Dpb->colocated_buf_count) { |
5426 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_ERROR, |
5427 | "%s error, index %d is bigger than buf count %d\n", |
5428 | __func__, index, |
5429 | p_H264_Dpb->colocated_buf_count); |
5430 | } else { |
5431 | if (((p_H264_Dpb->colocated_buf_map >> |
5432 | index) & 0x1) == 0x1) { |
5433 | p_H264_Dpb->colocated_buf_map &= |
5434 | (~(1 << index)); |
5435 | } else { |
5436 | dpb_print(p_H264_Dpb->decoder_index, |
5437 | PRINT_FLAG_ERROR, |
5438 | "%s error, index %d is not allocated\n", |
5439 | __func__, index); |
5440 | } |
5441 | } |
5442 | } |
5443 | return 0; |
5444 | } |
5445 | |
5446 | void set_frame_output_flag(struct h264_dpb_stru *p_H264_Dpb, int index) |
5447 | { |
5448 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
5449 | |
5450 | p_H264_Dpb->mFrameStore[index].is_output = 1; |
5451 | p_H264_Dpb->mFrameStore[index].pre_output = 0; |
5452 | dump_dpb(p_Dpb, 0); |
5453 | } |
5454 | |
5455 | #if 0 |
5456 | void init_old_slice(OldSliceParams *p_old_slice) |
5457 | { |
5458 | p_old_slice->field_pic_flag = 0; |
5459 | p_old_slice->pps_id = INT_MAX; |
5460 | p_old_slice->frame_num = INT_MAX; |
5461 | p_old_slice->nal_ref_idc = INT_MAX; |
5462 | p_old_slice->idr_flag = FALSE; |
5463 | |
5464 | p_old_slice->pic_oder_cnt_lsb = UINT_MAX; |
5465 | p_old_slice->delta_pic_oder_cnt_bottom = INT_MAX; |
5466 | |
5467 | p_old_slice->delta_pic_order_cnt[0] = INT_MAX; |
5468 | p_old_slice->delta_pic_order_cnt[1] = INT_MAX; |
5469 | } |
5470 | |
5471 | |
5472 | void copy_slice_info(struct Slice *currSlice, OldSliceParams *p_old_slice) |
5473 | { |
5474 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
5475 | |
5476 | p_old_slice->pps_id = currSlice->pic_parameter_set_id; |
5477 | p_old_slice->frame_num = currSlice->frame_num; |
5478 | /* p_Vid->frame_num; */ |
5479 | p_old_slice->field_pic_flag = |
5480 | currSlice->field_pic_flag; |
5481 | /* p_Vid->field_pic_flag; */ |
5482 | |
5483 | if (currSlice->field_pic_flag) |
5484 | p_old_slice->bottom_field_flag = currSlice->bottom_field_flag; |
5485 | |
5486 | p_old_slice->nal_ref_idc = currSlice->nal_reference_idc; |
5487 | p_old_slice->idr_flag = (byte) currSlice->idr_flag; |
5488 | |
5489 | if (currSlice->idr_flag) |
5490 | p_old_slice->idr_pic_id = currSlice->idr_pic_id; |
5491 | |
5492 | if (p_Vid->active_sps->pic_order_cnt_type == 0) { |
5493 | p_old_slice->pic_oder_cnt_lsb = |
5494 | currSlice->pic_order_cnt_lsb; |
5495 | p_old_slice->delta_pic_oder_cnt_bottom = |
5496 | currSlice->delta_pic_order_cnt_bottom; |
5497 | } |
5498 | |
5499 | if (p_Vid->active_sps->pic_order_cnt_type == 1) { |
5500 | p_old_slice->delta_pic_order_cnt[0] = |
5501 | currSlice->delta_pic_order_cnt[0]; |
5502 | p_old_slice->delta_pic_order_cnt[1] = |
5503 | currSlice->delta_pic_order_cnt[1]; |
5504 | } |
5505 | #if (MVC_EXTENSION_ENABLE) |
5506 | p_old_slice->view_id = currSlice->view_id; |
5507 | p_old_slice->inter_view_flag = currSlice->inter_view_flag; |
5508 | p_old_slice->anchor_pic_flag = currSlice->anchor_pic_flag; |
5509 | #endif |
5510 | p_old_slice->layer_id = currSlice->layer_id; |
5511 | } |
5512 | |
5513 | int is_new_picture(StorablePicture *dec_picture, struct Slice *currSlice, |
5514 | OldSliceParams *p_old_slice) |
5515 | { |
5516 | struct VideoParameters *p_Vid = currSlice->p_Vid; |
5517 | |
5518 | int result = 0; |
5519 | |
5520 | result |= (dec_picture == NULL); |
5521 | |
5522 | result |= (p_old_slice->pps_id != currSlice->pic_parameter_set_id); |
5523 | |
5524 | result |= (p_old_slice->frame_num != currSlice->frame_num); |
5525 | |
5526 | result |= (p_old_slice->field_pic_flag != currSlice->field_pic_flag); |
5527 | |
5528 | if (currSlice->field_pic_flag && p_old_slice->field_pic_flag) { |
5529 | result |= (p_old_slice->bottom_field_flag != |
5530 | currSlice->bottom_field_flag); |
5531 | } |
5532 | |
5533 | result |= (p_old_slice->nal_ref_idc != |
5534 | currSlice->nal_reference_idc) && |
5535 | ((p_old_slice->nal_ref_idc == 0) || |
5536 | (currSlice->nal_reference_idc == 0)); |
5537 | result |= (p_old_slice->idr_flag != currSlice->idr_flag); |
5538 | |
5539 | if (currSlice->idr_flag && p_old_slice->idr_flag) |
5540 | result |= (p_old_slice->idr_pic_id != currSlice->idr_pic_id); |
5541 | |
5542 | if (p_Vid->active_sps->pic_order_cnt_type == 0) { |
5543 | result |= (p_old_slice->pic_oder_cnt_lsb != |
5544 | currSlice->pic_order_cnt_lsb); |
5545 | if (p_Vid->active_pps-> |
5546 | bottom_field_pic_order_in_frame_present_flag == 1 && |
5547 | !currSlice->field_pic_flag) { |
5548 | result |= (p_old_slice->delta_pic_oder_cnt_bottom != |
5549 | currSlice->delta_pic_order_cnt_bottom); |
5550 | } |
5551 | } |
5552 | |
5553 | if (p_Vid->active_sps->pic_order_cnt_type == 1) { |
5554 | if (!p_Vid->active_sps->delta_pic_order_always_zero_flag) { |
5555 | result |= (p_old_slice->delta_pic_order_cnt[0] != |
5556 | currSlice->delta_pic_order_cnt[0]); |
5557 | if (p_Vid->active_pps-> |
5558 | bottom_field_pic_order_in_frame_present_flag == 1 && |
5559 | !currSlice->field_pic_flag) { |
5560 | result |= (p_old_slice-> |
5561 | delta_pic_order_cnt[1] != |
5562 | currSlice->delta_pic_order_cnt[1]); |
5563 | } |
5564 | } |
5565 | } |
5566 | |
5567 | #if (MVC_EXTENSION_ENABLE) |
5568 | result |= (currSlice->view_id != p_old_slice->view_id); |
5569 | result |= (currSlice->inter_view_flag != p_old_slice->inter_view_flag); |
5570 | result |= (currSlice->anchor_pic_flag != p_old_slice->anchor_pic_flag); |
5571 | #endif |
5572 | result |= (currSlice->layer_id != p_old_slice->layer_id); |
5573 | return result; |
5574 | } |
5575 | #else |
5576 | int is_new_picture(struct StorablePicture *dec_picture, |
5577 | struct h264_dpb_stru *p_H264_Dpb, |
5578 | struct OldSliceParams *p_old_slice) |
5579 | { |
5580 | int ret = 0; |
5581 | |
5582 | if (p_H264_Dpb->dpb_param.l.data[FIRST_MB_IN_SLICE] == 0) |
5583 | ret = 1; |
5584 | return ret; |
5585 | } |
5586 | |
5587 | #endif |
5588 | |
5589 | /* |
5590 | * release bufspec and pic for picture not in dpb buf |
5591 | */ |
5592 | int release_picture(struct h264_dpb_stru *p_H264_Dpb, |
5593 | struct StorablePicture *pic) |
5594 | { |
5595 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
5596 | |
5597 | if (p_Dpb->last_picture == NULL) { |
5598 | if (pic->colocated_buf_index >= 0) { |
5599 | release_colocate_buf(p_H264_Dpb, |
5600 | pic->colocated_buf_index); |
5601 | pic->colocated_buf_index = -1; |
5602 | } |
5603 | release_buf_spec_num(p_H264_Dpb->vdec, pic->buf_spec_num); |
5604 | } else { |
5605 | if (pic->buf_spec_is_alloced == 1) |
5606 | release_buf_spec_num(p_H264_Dpb->vdec, |
5607 | pic->buf_spec_num); |
5608 | } |
5609 | |
5610 | free_picture(p_H264_Dpb, pic); |
5611 | return 0; |
5612 | } |
5613 | |
5614 | #ifdef ERROR_HANDLE_TEST |
5615 | /* |
5616 | * remove all pictures in dpb and release bufspec/pic of them |
5617 | */ |
5618 | void remove_dpb_pictures(struct h264_dpb_stru *p_H264_Dpb) |
5619 | { |
5620 | /* struct VideoParameters *p_Vid = p_Dpb->p_Vid; */ |
5621 | struct DecodedPictureBuffer *p_Dpb = &p_H264_Dpb->mDPB; |
5622 | struct Slice *currSlice = &p_H264_Dpb->mSlice; |
5623 | unsigned i, j; |
5624 | |
5625 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
5626 | "%s\n", __func__); |
5627 | |
5628 | if (!p_Dpb->init_done) |
5629 | return; |
5630 | |
5631 | for (i = 0; i < p_Dpb->used_size; i++) { |
5632 | if (p_Dpb->fs[i]->colocated_buf_index >= 0) { |
5633 | dpb_print(p_H264_Dpb->decoder_index, |
5634 | PRINT_FLAG_DPB_DETAIL, |
5635 | "release_colocate_buf[%d] for fs[%d]\n", |
5636 | p_Dpb->fs[i]->colocated_buf_index, i); |
5637 | |
5638 | release_colocate_buf(p_H264_Dpb, |
5639 | p_Dpb->fs[i]->colocated_buf_index); /* rain */ |
5640 | p_Dpb->fs[i]->colocated_buf_index = -1; |
5641 | } |
5642 | if (!p_Dpb->fs[i]->pre_output) { |
5643 | release_buf_spec_num(p_H264_Dpb->vdec, |
5644 | p_Dpb->fs[i]->buf_spec_num); |
5645 | p_Dpb->fs[i]->buf_spec_num = -1; |
5646 | } |
5647 | remove_frame_from_dpb(p_H264_Dpb, i); |
5648 | } |
5649 | |
5650 | for (i = 0; i < p_Dpb->used_size; i++) { |
5651 | p_Dpb->fs_ref[i] = NULL; |
5652 | p_Dpb->fs_ltref[i] = NULL; |
5653 | p_Dpb->fs_list0[i] = NULL; |
5654 | p_Dpb->fs_list1[i] = NULL; |
5655 | p_Dpb->fs_listlt[i] = NULL; |
5656 | } |
5657 | for (i = 0; i < 2; i++) { |
5658 | currSlice->listXsize[i] = 0; |
5659 | for (j = 0; j < (MAX_LIST_SIZE * 2); j++) |
5660 | currSlice->listX[i][j] = NULL; |
5661 | } |
5662 | p_Dpb->ref_frames_in_buffer = 0; |
5663 | p_Dpb->ltref_frames_in_buffer = 0; |
5664 | p_Dpb->last_output_poc = INT_MIN; |
5665 | } |
5666 | #endif |
5667 | |
5668 | static void check_frame_store_same_pic_num(struct DecodedPictureBuffer *p_Dpb, |
5669 | struct StorablePicture *p, struct Slice *currSlice) |
5670 | { |
5671 | if (p_Dpb->last_picture) { |
5672 | if ((int)p_Dpb->last_picture->frame_num == p->pic_num) { |
5673 | if (((p->structure == TOP_FIELD) && |
5674 | (p_Dpb->last_picture->is_used == 2)) || |
5675 | ((p->structure == BOTTOM_FIELD) && |
5676 | (p_Dpb->last_picture->is_used == 1))) { |
5677 | if ((p->used_for_reference && |
5678 | (p_Dpb->last_picture-> |
5679 | is_orig_reference != 0)) || |
5680 | (!p->used_for_reference && |
5681 | (p_Dpb->last_picture-> |
5682 | is_orig_reference == 0))) { |
5683 | p->buf_spec_num = |
5684 | p_Dpb->last_picture-> |
5685 | buf_spec_num; |
5686 | p->buf_spec_is_alloced = 0; |
5687 | p->colocated_buf_index = p_Dpb-> |
5688 | last_picture-> |
5689 | colocated_buf_index; |
5690 | if (currSlice->structure == |
5691 | TOP_FIELD) { |
5692 | p->bottom_poc = |
5693 | p_Dpb->last_picture-> |
5694 | bottom_field->poc; |
5695 | } else { |
5696 | p->top_poc = |
5697 | p_Dpb->last_picture-> |
5698 | top_field->poc; |
5699 | } |
5700 | p->frame_poc = imin(p->bottom_poc, |
5701 | p->top_poc); |
5702 | } |
5703 | } |
5704 | } |
5705 | } |
5706 | } |
5707 | |
5708 | int h264_slice_header_process(struct h264_dpb_stru *p_H264_Dpb, int *frame_num_gap) |
5709 | { |
5710 | |
5711 | int new_pic_flag = 0; |
5712 | struct Slice *currSlice = &p_H264_Dpb->mSlice; |
5713 | struct VideoParameters *p_Vid = &p_H264_Dpb->mVideo; |
5714 | struct DecodedPictureBuffer *p_Dpb = |
5715 | &p_H264_Dpb->mDPB; |
5716 | #if 0 |
5717 | new_pic_flag = is_new_picture(p_H264_Dpb->mVideo.dec_picture, |
5718 | p_H264_Dpb, |
5719 | &p_H264_Dpb->mVideo.old_slice); |
5720 | |
5721 | if (new_pic_flag) { /* new picture */ |
5722 | if (p_H264_Dpb->mVideo.dec_picture) { |
5723 | store_picture_in_dpb(p_H264_Dpb, |
5724 | p_H264_Dpb->mVideo.dec_picture); |
5725 | /* dump_dpb(&p_H264_Dpb->mDPB); */ |
5726 | } |
5727 | } |
5728 | #else |
5729 | new_pic_flag = (p_H264_Dpb->mVideo.dec_picture == NULL); |
5730 | #endif |
5731 | p_H264_Dpb->buf_alloc_fail = 0; |
5732 | p_H264_Dpb->dpb_error_flag = 0; |
5733 | slice_prepare(p_H264_Dpb, &p_H264_Dpb->mDPB, &p_H264_Dpb->mVideo, |
5734 | &p_H264_Dpb->mSPS, &p_H264_Dpb->mSlice); |
5735 | |
5736 | if (p_Dpb->num_ref_frames != p_H264_Dpb->mSPS.num_ref_frames) { |
5737 | dpb_print(p_H264_Dpb->decoder_index, 0, |
5738 | "num_ref_frames change from %d to %d\r\n", |
5739 | p_Dpb->num_ref_frames, p_H264_Dpb->mSPS.num_ref_frames); |
5740 | p_Dpb->num_ref_frames = p_H264_Dpb->mSPS.num_ref_frames; |
5741 | } |
5742 | /* if (p_Vid->active_sps != sps) { */ |
5743 | if (p_H264_Dpb->mDPB.init_done == 0) { |
5744 | /*init_global_buffers(p_Vid, 0); |
5745 | * ** * *if (!p_Vid->no_output_of_prior_pics_flag) |
5746 | ** * *{ |
5747 | ** * * flush_dpb(p_Vid->p_Dpb_layer[0]); |
5748 | ** * *} |
5749 | ** * *init_dpb(p_Vid, p_Vid->p_Dpb_layer[0], 0); |
5750 | */ |
5751 | init_dpb(p_H264_Dpb, 0); |
5752 | } |
5753 | |
5754 | |
5755 | if (new_pic_flag) { /* new picture */ |
5756 | dpb_print(p_H264_Dpb->decoder_index, PRINT_FLAG_DPB_DETAIL, |
5757 | "check frame_num gap: cur frame_num %d pre_frame_num %d max_frmae_num %d\r\n", |
5758 | currSlice->frame_num, |
5759 | p_Vid->pre_frame_num, |
5760 | p_Vid->max_frame_num); |
5761 | if (p_Vid->recovery_point == 0 && |
5762 | p_Vid->max_frame_num <= FRAME_NUM_MAX_SIZE && |
5763 | currSlice->frame_num != p_Vid->pre_frame_num && |
5764 | currSlice->frame_num != |
5765 | (p_Vid->pre_frame_num + 1) % p_Vid->max_frame_num) { |
5766 | /*if (active_sps-> |
5767 | *gaps_in_frame_num_value_allowed_flag |
5768 | *== 0) { |
5769 | * error("An unintentional |
5770 | * loss of pictures occurs! Exit\n", |
5771 | * 100); |
5772 | *} |
5773 | *if (p_Vid->conceal_mode == 0) |
5774 | */ |
5775 | fill_frame_num_gap(p_Vid, currSlice); |
5776 | *frame_num_gap = 1; |
5777 | } |
5778 | |
5779 | if (currSlice->nal_reference_idc) { |
5780 | dpb_print(p_H264_Dpb->decoder_index, |
5781 | PRINT_FLAG_DPB_DETAIL, |
5782 | "nal_reference_idc not 0, set pre_frame_num(%d) to frame_num (%d)\n", |
5783 | p_Vid->pre_frame_num, currSlice->frame_num); |
5784 | p_Vid->pre_frame_num = currSlice->frame_num; |
5785 | } |
5786 | |
5787 | decode_poc(&p_H264_Dpb->mVideo, &p_H264_Dpb->mSlice); |
5788 | p_H264_Dpb->mVideo.dec_picture = get_new_pic(p_H264_Dpb, |
5789 | p_H264_Dpb->mSlice.structure, |
5790 | /*p_Vid->width, p_Vid->height, |
5791 | * p_Vid->width_cr, |
5792 | * p_Vid->height_cr, |
5793 | */ |
5794 | 1); |
5795 | if (p_H264_Dpb->mVideo.dec_picture) { |
5796 | u32 offset_lo, offset_hi; |
5797 | struct DecodedPictureBuffer *p_Dpb = |
5798 | &p_H264_Dpb->mDPB; |
5799 | struct StorablePicture *p = |
5800 | p_H264_Dpb->mVideo.dec_picture; |
5801 | init_picture(p_H264_Dpb, &p_H264_Dpb->mSlice, |
5802 | p_H264_Dpb->mVideo.dec_picture); |
5803 | #if 1 |
5804 | /* rain */ |
5805 | offset_lo = |
5806 | p_H264_Dpb->dpb_param.l.data[OFFSET_DELIMITER_LO]; |
5807 | offset_hi = |
5808 | p_H264_Dpb->dpb_param.l.data[OFFSET_DELIMITER_HI]; |
5809 | p_H264_Dpb->mVideo.dec_picture->offset_delimiter = |
5810 | (offset_lo | offset_hi << 16); |
5811 | p_H264_Dpb->mVideo.dec_picture->buf_spec_num = -1; |
5812 | p_H264_Dpb->mVideo.dec_picture-> |
5813 | colocated_buf_index = -1; |
5814 | update_pic_num(p_H264_Dpb); |
5815 | |
5816 | if ((currSlice->structure == TOP_FIELD) || |
5817 | (currSlice->structure == BOTTOM_FIELD)) { |
5818 | /* check for frame store with same |
5819 | * pic_number |
5820 | */ |
5821 | check_frame_store_same_pic_num(p_Dpb, p, |
5822 | currSlice); |
5823 | } |
5824 | |
5825 | if (p_H264_Dpb->mVideo.dec_picture->buf_spec_num == |
5826 | -1) { |
5827 | p_H264_Dpb->mVideo.dec_picture->buf_spec_num = |
5828 | get_free_buf_idx(p_H264_Dpb->vdec); |
5829 | if (p_H264_Dpb->mVideo.dec_picture->buf_spec_num |
5830 | < 0) { |
5831 | p_H264_Dpb->buf_alloc_fail = 1; |
5832 | p_H264_Dpb->mVideo.dec_picture-> |
5833 | buf_spec_is_alloced = 0; |
5834 | } else |
5835 | p_H264_Dpb->mVideo.dec_picture-> |
5836 | buf_spec_is_alloced = 1; |
5837 | |
5838 | if (p_H264_Dpb->mVideo.dec_picture-> |
5839 | used_for_reference) { |
5840 | p_H264_Dpb->mVideo.dec_picture-> |
5841 | colocated_buf_index = |
5842 | allocate_colocate_buf( |
5843 | p_H264_Dpb); |
5844 | } |
5845 | } |
5846 | #endif |
5847 | } |
5848 | } |
5849 | |
5850 | if (p_H264_Dpb->mSlice.slice_type == P_SLICE) |
5851 | init_lists_p_slice(&p_H264_Dpb->mSlice); |
5852 | else if (p_H264_Dpb->mSlice.slice_type == B_SLICE) |
5853 | init_lists_b_slice(&p_H264_Dpb->mSlice); |
5854 | else |
5855 | init_lists_i_slice(&p_H264_Dpb->mSlice); |
5856 | |
5857 | reorder_lists(&p_H264_Dpb->mSlice); |
5858 | |
5859 | if (p_H264_Dpb->mSlice.structure == FRAME) |
5860 | init_mbaff_lists(p_H264_Dpb, &p_H264_Dpb->mSlice); |
5861 | |
5862 | if (new_pic_flag) |
5863 | return 1; |
5864 | |
5865 | return 0; |
5866 | } |
5867 | |
5868 | enum PictureStructure get_cur_slice_picture_struct( |
5869 | struct h264_dpb_stru *p_H264_Dpb) |
5870 | { |
5871 | struct Slice *currSlice = &p_H264_Dpb->mSlice; |
5872 | return currSlice->structure; |
5873 | } |
5874 | |
5875 | static unsigned char is_pic_in_dpb(struct h264_dpb_stru *p_H264_Dpb, |
5876 | struct StorablePicture *pic) |
5877 | { |
5878 | unsigned char ret = 0; |
5879 | int i; |
5880 | struct DecodedPictureBuffer *p_Dpb = |
5881 | &p_H264_Dpb->mDPB; |
5882 | for (i = 0; i < p_Dpb->used_size; i++) { |
5883 | if (p_Dpb->fs[i]->top_field == pic || |
5884 | p_Dpb->fs[i]->bottom_field == pic || |
5885 | p_Dpb->fs[i]->frame == pic) { |
5886 | ret = 1; |
5887 | break; |
5888 | } |
5889 | } |
5890 | return ret; |
5891 | } |
5892 | |
5893 | int dpb_check_ref_list_error( |
5894 | struct h264_dpb_stru *p_H264_Dpb) |
5895 | { |
5896 | int i; |
5897 | /*int j;*/ |
5898 | struct Slice *currSlice = &p_H264_Dpb->mSlice; |
5899 | /* in first output, ignore ref check */ |
5900 | if (p_H264_Dpb->first_insert_frame < FirstInsertFrm_SKIPDONE) |
5901 | return 0; |
5902 | if ((currSlice->slice_type != I_SLICE) && |
5903 | (currSlice->slice_type != SI_SLICE)) { |
5904 | for (i = 0; i < currSlice->listXsize[0]; i++) { |
5905 | /*for (j = i + 1; j < currSlice->listXsize[0]; j++) { |
5906 | if(currSlice->listX[0][i]->pic_num == |
5907 | currSlice->listX[0][j]->pic_num) |
5908 | return 1; |
5909 | }*/ |
5910 | if (currSlice->listX[0][i] == NULL) |
5911 | return 5; |
5912 | if (!is_pic_in_dpb(p_H264_Dpb, |
5913 | currSlice->listX[0][i])) |
5914 | return 1; |
5915 | if (currSlice->listX[0][i]->frame && |
5916 | currSlice->listX[0][i]->frame->non_existing) |
5917 | return 3; |
5918 | } |
5919 | } |
5920 | |
5921 | if (currSlice->slice_type == B_SLICE) { |
5922 | for (i = 0; i < currSlice->listXsize[1]; i++) { |
5923 | /*for (j = i + 1; j < currSlice->listXsize[1]; j++) { |
5924 | if(currSlice->listX[1][i]->pic_num == |
5925 | currSlice->listX[1][j]->pic_num) |
5926 | return 2; |
5927 | } |
5928 | for (j = 0; j < currSlice->listXsize[0]; j++) { |
5929 | if(currSlice->listX[1][i]->pic_num == |
5930 | currSlice->listX[0][j]->pic_num) |
5931 | return 3; |
5932 | }*/ |
5933 | if (currSlice->listX[1][i] == NULL) |
5934 | return 6; |
5935 | if (!is_pic_in_dpb(p_H264_Dpb, |
5936 | currSlice->listX[1][i])) |
5937 | return 2; |
5938 | if (currSlice->listX[1][i]->frame && |
5939 | currSlice->listX[1][i]->frame->non_existing) |
5940 | return 4; |
5941 | #if 0 |
5942 | if (currSlice->listXsize[0] == 1 && |
5943 | currSlice->listXsize[1] == 1 && |
5944 | currSlice->listX[1][0] == |
5945 | currSlice->listX[0][0]) |
5946 | return 3; |
5947 | #endif |
5948 | } |
5949 | } |
5950 | return 0; |
5951 | } |
5952 | |
5953 |