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