1 |
14 |
jamey.hick |
|
2 |
|
|
/*!
|
3 |
|
|
*************************************************************************************
|
4 |
|
|
* \file header.c
|
5 |
|
|
*
|
6 |
|
|
* \brief
|
7 |
|
|
* H.264 Slice headers
|
8 |
|
|
*
|
9 |
|
|
*************************************************************************************
|
10 |
|
|
*/
|
11 |
|
|
|
12 |
|
|
#include <stdlib.h>
|
13 |
|
|
#include <assert.h>
|
14 |
|
|
#include <string.h>
|
15 |
|
|
#include <stdio.h>
|
16 |
|
|
|
17 |
|
|
#include "global.h"
|
18 |
|
|
#include "elements.h"
|
19 |
|
|
#include "defines.h"
|
20 |
|
|
#include "fmo.h"
|
21 |
|
|
#include "vlc.h"
|
22 |
|
|
#include "mbuffer.h"
|
23 |
|
|
#include "header.h"
|
24 |
|
|
|
25 |
|
|
#include "ctx_tables.h"
|
26 |
|
|
|
27 |
|
|
extern StorablePicture *dec_picture;
|
28 |
|
|
|
29 |
|
|
#if TRACE
|
30 |
|
|
#define SYMTRACESTRING(s) strncpy(sym.tracestring,s,TRACESTRING_SIZE)
|
31 |
|
|
#else
|
32 |
|
|
#define SYMTRACESTRING(s) // to nothing
|
33 |
|
|
#endif
|
34 |
|
|
|
35 |
|
|
extern int UsedBits;
|
36 |
|
|
|
37 |
|
|
static void ref_pic_list_reordering();
|
38 |
|
|
static void pred_weight_table();
|
39 |
|
|
|
40 |
|
|
|
41 |
|
|
/*!
|
42 |
|
|
************************************************************************
|
43 |
|
|
* \brief
|
44 |
|
|
* calculate Ceil(Log2(uiVal))
|
45 |
|
|
************************************************************************
|
46 |
|
|
*/
|
47 |
|
|
unsigned CeilLog2( unsigned uiVal)
|
48 |
|
|
{
|
49 |
|
|
unsigned uiTmp = uiVal-1;
|
50 |
|
|
unsigned uiRet = 0;
|
51 |
|
|
|
52 |
|
|
while( uiTmp != 0 )
|
53 |
|
|
{
|
54 |
|
|
uiTmp >>= 1;
|
55 |
|
|
uiRet++;
|
56 |
|
|
}
|
57 |
|
|
return uiRet;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
/*!
|
62 |
|
|
************************************************************************
|
63 |
|
|
* \brief
|
64 |
|
|
* read the first part of the header (only the pic_parameter_set_id)
|
65 |
|
|
* \return
|
66 |
|
|
* Length of the first part of the slice header (in bits)
|
67 |
|
|
************************************************************************
|
68 |
|
|
*/
|
69 |
|
|
int FirstPartOfSliceHeader()
|
70 |
|
|
{
|
71 |
|
|
Slice *currSlice = img->currentSlice;
|
72 |
|
|
int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
|
73 |
|
|
DataPartition *partition = &(currSlice->partArr[dP_nr]);
|
74 |
|
|
Bitstream *currStream = partition->bitstream;
|
75 |
|
|
int tmp;
|
76 |
|
|
|
77 |
|
|
UsedBits= partition->bitstream->frame_bitoffset; // was hardcoded to 31 for previous start-code. This is better.
|
78 |
|
|
|
79 |
|
|
// Get first_mb_in_slice
|
80 |
|
|
currSlice->start_mb_nr = ue_v ("SH: first_mb_in_slice", currStream);
|
81 |
|
|
|
82 |
|
|
tmp = ue_v ("SH: slice_type", currStream);
|
83 |
|
|
|
84 |
|
|
if (tmp>4) tmp -=5;
|
85 |
|
|
|
86 |
|
|
img->type = currSlice->picture_type = (SliceType) tmp;
|
87 |
|
|
|
88 |
|
|
currSlice->pic_parameter_set_id = ue_v ("SH: pic_parameter_set_id", currStream);
|
89 |
|
|
|
90 |
|
|
return UsedBits;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/*!
|
94 |
|
|
************************************************************************
|
95 |
|
|
* \brief
|
96 |
|
|
* read the scond part of the header (without the pic_parameter_set_id
|
97 |
|
|
* \return
|
98 |
|
|
* Length of the second part of the Slice header in bits
|
99 |
|
|
************************************************************************
|
100 |
|
|
*/
|
101 |
|
|
int RestOfSliceHeader()
|
102 |
|
|
{
|
103 |
|
|
Slice *currSlice = img->currentSlice;
|
104 |
|
|
int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
|
105 |
|
|
DataPartition *partition = &(currSlice->partArr[dP_nr]);
|
106 |
|
|
Bitstream *currStream = partition->bitstream;
|
107 |
|
|
|
108 |
|
|
int val, len;
|
109 |
|
|
|
110 |
|
|
img->frame_num = u_v (active_sps->log2_max_frame_num_minus4 + 4, "SH: frame_num", currStream);
|
111 |
|
|
|
112 |
|
|
/* Tian Dong: frame_num gap processing, if found */
|
113 |
|
|
if (img->idr_flag)
|
114 |
|
|
{
|
115 |
|
|
img->pre_frame_num = img->frame_num;
|
116 |
|
|
// picture error concealment
|
117 |
|
|
img->last_ref_pic_poc = 0;
|
118 |
|
|
assert(img->frame_num == 0);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
if (active_sps->frame_mbs_only_flag)
|
122 |
|
|
{
|
123 |
|
|
img->structure = FRAME;
|
124 |
|
|
img->field_pic_flag=0;
|
125 |
|
|
}
|
126 |
|
|
else
|
127 |
|
|
{
|
128 |
|
|
// field_pic_flag u(1)
|
129 |
|
|
img->field_pic_flag = u_1("SH: field_pic_flag", currStream);
|
130 |
|
|
if (img->field_pic_flag)
|
131 |
|
|
{
|
132 |
|
|
// bottom_field_flag u(1)
|
133 |
|
|
img->bottom_field_flag = u_1("SH: bottom_field_flag", currStream);
|
134 |
|
|
|
135 |
|
|
img->structure = img->bottom_field_flag ? BOTTOM_FIELD : TOP_FIELD;
|
136 |
|
|
}
|
137 |
|
|
else
|
138 |
|
|
{
|
139 |
|
|
img->structure = FRAME;
|
140 |
|
|
img->bottom_field_flag=0;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
currSlice->structure = (PictureStructure) img->structure;
|
145 |
|
|
|
146 |
|
|
img->MbaffFrameFlag=(active_sps->mb_adaptive_frame_field_flag && (img->field_pic_flag==0));
|
147 |
|
|
|
148 |
|
|
if (img->structure == FRAME ) assert (img->field_pic_flag == 0);
|
149 |
|
|
if (img->structure == TOP_FIELD ) assert (img->field_pic_flag == 1 && img->bottom_field_flag == 0);
|
150 |
|
|
if (img->structure == BOTTOM_FIELD) assert (img->field_pic_flag == 1 && img->bottom_field_flag == 1);
|
151 |
|
|
|
152 |
|
|
if (img->idr_flag)
|
153 |
|
|
{
|
154 |
|
|
img->idr_pic_id = ue_v("SH: idr_pic_id", currStream);
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
if (active_sps->pic_order_cnt_type == 0)
|
158 |
|
|
{
|
159 |
|
|
img->pic_order_cnt_lsb = u_v(active_sps->log2_max_pic_order_cnt_lsb_minus4 + 4, "SH: pic_order_cnt_lsb", currStream);
|
160 |
|
|
if( active_pps->pic_order_present_flag == 1 && !img->field_pic_flag )
|
161 |
|
|
img->delta_pic_order_cnt_bottom = se_v("SH: delta_pic_order_cnt_bottom", currStream);
|
162 |
|
|
else
|
163 |
|
|
img->delta_pic_order_cnt_bottom = 0;
|
164 |
|
|
}
|
165 |
|
|
if( active_sps->pic_order_cnt_type == 1 && !active_sps->delta_pic_order_always_zero_flag )
|
166 |
|
|
{
|
167 |
|
|
img->delta_pic_order_cnt[ 0 ] = se_v("SH: delta_pic_order_cnt[0]", currStream);
|
168 |
|
|
if( active_pps->pic_order_present_flag == 1 && !img->field_pic_flag )
|
169 |
|
|
img->delta_pic_order_cnt[ 1 ] = se_v("SH: delta_pic_order_cnt[1]", currStream);
|
170 |
|
|
}else
|
171 |
|
|
{
|
172 |
|
|
if (active_sps->pic_order_cnt_type == 1)
|
173 |
|
|
{
|
174 |
|
|
img->delta_pic_order_cnt[ 0 ] = 0;
|
175 |
|
|
img->delta_pic_order_cnt[ 1 ] = 0;
|
176 |
|
|
}
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
//! redundant_pic_cnt is missing here
|
180 |
|
|
if (active_pps->redundant_pic_cnt_present_flag)
|
181 |
|
|
{
|
182 |
|
|
img->redundant_pic_cnt = ue_v ("SH: redundant_pic_cnt", currStream);
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
if(img->type==B_SLICE)
|
186 |
|
|
{
|
187 |
|
|
img->direct_spatial_mv_pred_flag = u_1 ("SH: direct_spatial_mv_pred_flag", currStream);
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
img->num_ref_idx_l0_active = active_pps->num_ref_idx_l0_active_minus1 + 1;
|
191 |
|
|
img->num_ref_idx_l1_active = active_pps->num_ref_idx_l1_active_minus1 + 1;
|
192 |
|
|
|
193 |
|
|
if(img->type==P_SLICE || img->type == SP_SLICE || img->type==B_SLICE)
|
194 |
|
|
{
|
195 |
|
|
val = u_1 ("SH: num_ref_idx_override_flag", currStream);
|
196 |
|
|
if (val)
|
197 |
|
|
{
|
198 |
|
|
img->num_ref_idx_l0_active = 1 + ue_v ("SH: num_ref_idx_l0_active_minus1", currStream);
|
199 |
|
|
|
200 |
|
|
if(img->type==B_SLICE)
|
201 |
|
|
{
|
202 |
|
|
img->num_ref_idx_l1_active = 1 + ue_v ("SH: num_ref_idx_l1_active_minus1", currStream);
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
if (img->type!=B_SLICE)
|
207 |
|
|
{
|
208 |
|
|
img->num_ref_idx_l1_active = 0;
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
ref_pic_list_reordering();
|
212 |
|
|
|
213 |
|
|
img->apply_weights = ((active_pps->weighted_pred_flag && (currSlice->picture_type == P_SLICE || currSlice->picture_type == SP_SLICE) )
|
214 |
|
|
|| ((active_pps->weighted_bipred_idc > 0 ) && (currSlice->picture_type == B_SLICE)));
|
215 |
|
|
|
216 |
|
|
if ((active_pps->weighted_pred_flag&&(img->type==P_SLICE|| img->type == SP_SLICE))||
|
217 |
|
|
(active_pps->weighted_bipred_idc==1 && (img->type==B_SLICE)))
|
218 |
|
|
{
|
219 |
|
|
pred_weight_table();
|
220 |
|
|
}
|
221 |
|
|
|
222 |
|
|
if (img->nal_reference_idc)
|
223 |
|
|
dec_ref_pic_marking(currStream);
|
224 |
|
|
|
225 |
|
|
if (active_pps->entropy_coding_mode_flag && img->type!=I_SLICE && img->type!=SI_SLICE)
|
226 |
|
|
{
|
227 |
|
|
img->model_number = ue_v("SH: cabac_init_idc", currStream);
|
228 |
|
|
}
|
229 |
|
|
else
|
230 |
|
|
{
|
231 |
|
|
img->model_number = 0;
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
val = se_v("SH: slice_qp_delta", currStream);
|
235 |
|
|
currSlice->qp = img->qp = 26 + active_pps->pic_init_qp_minus26 + val;
|
236 |
|
|
if ((img->qp < -img->bitdepth_luma_qp_scale) || (img->qp > 51))
|
237 |
|
|
error ("slice_qp_delta makes slice_qp_y out of range", 500);
|
238 |
|
|
|
239 |
|
|
|
240 |
|
|
currSlice->slice_qp_delta = val;
|
241 |
|
|
|
242 |
|
|
if(img->type==SP_SLICE || img->type == SI_SLICE)
|
243 |
|
|
{
|
244 |
|
|
if(img->type==SP_SLICE)
|
245 |
|
|
{
|
246 |
|
|
img->sp_switch = u_1 ("SH: sp_for_switch_flag", currStream);
|
247 |
|
|
}
|
248 |
|
|
val = se_v("SH: slice_qs_delta", currStream);
|
249 |
|
|
img->qpsp = 26 + active_pps->pic_init_qs_minus26 + val;
|
250 |
|
|
if ((img->qpsp < 0) || (img->qpsp > 51))
|
251 |
|
|
error ("slice_qs_delta makes slice_qs_y out of range", 500);
|
252 |
|
|
}
|
253 |
|
|
|
254 |
|
|
if (active_pps->deblocking_filter_control_present_flag)
|
255 |
|
|
{
|
256 |
|
|
currSlice->LFDisableIdc = ue_v ("SH: disable_deblocking_filter_idc", currStream);
|
257 |
|
|
|
258 |
|
|
if (currSlice->LFDisableIdc!=1)
|
259 |
|
|
{
|
260 |
|
|
currSlice->LFAlphaC0Offset = 2 * se_v("SH: slice_alpha_c0_offset_div2", currStream);
|
261 |
|
|
currSlice->LFBetaOffset = 2 * se_v("SH: slice_beta_offset_div2", currStream);
|
262 |
|
|
}
|
263 |
|
|
else
|
264 |
|
|
{
|
265 |
|
|
currSlice->LFAlphaC0Offset = currSlice->LFBetaOffset = 0;
|
266 |
|
|
}
|
267 |
|
|
}
|
268 |
|
|
else
|
269 |
|
|
{
|
270 |
|
|
currSlice->LFDisableIdc = currSlice->LFAlphaC0Offset = currSlice->LFBetaOffset = 0;
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
if (active_pps->num_slice_groups_minus1>0 && active_pps->slice_group_map_type>=3 &&
|
274 |
|
|
active_pps->slice_group_map_type<=5)
|
275 |
|
|
{
|
276 |
|
|
len = (active_sps->pic_height_in_map_units_minus1+1)*(active_sps->pic_width_in_mbs_minus1+1)/
|
277 |
|
|
(active_pps->slice_group_change_rate_minus1+1);
|
278 |
|
|
if (((active_sps->pic_height_in_map_units_minus1+1)*(active_sps->pic_width_in_mbs_minus1+1))%
|
279 |
|
|
(active_pps->slice_group_change_rate_minus1+1))
|
280 |
|
|
len +=1;
|
281 |
|
|
|
282 |
|
|
len = CeilLog2(len+1);
|
283 |
|
|
|
284 |
|
|
img->slice_group_change_cycle = u_v (len, "SH: slice_group_change_cycle", currStream);
|
285 |
|
|
}
|
286 |
|
|
img->PicHeightInMbs = img->FrameHeightInMbs / ( 1 + img->field_pic_flag );
|
287 |
|
|
img->PicSizeInMbs = img->PicWidthInMbs * img->PicHeightInMbs;
|
288 |
|
|
img->FrameSizeInMbs = img->PicWidthInMbs * img->FrameHeightInMbs;
|
289 |
|
|
|
290 |
|
|
return UsedBits;
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
/*!
|
295 |
|
|
************************************************************************
|
296 |
|
|
* \brief
|
297 |
|
|
* read the reference picture reordering information
|
298 |
|
|
************************************************************************
|
299 |
|
|
*/
|
300 |
|
|
static void ref_pic_list_reordering()
|
301 |
|
|
{
|
302 |
|
|
Slice *currSlice = img->currentSlice;
|
303 |
|
|
int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
|
304 |
|
|
DataPartition *partition = &(currSlice->partArr[dP_nr]);
|
305 |
|
|
Bitstream *currStream = partition->bitstream;
|
306 |
|
|
int i, val;
|
307 |
|
|
|
308 |
|
|
alloc_ref_pic_list_reordering_buffer(currSlice);
|
309 |
|
|
|
310 |
|
|
if (img->type!=I_SLICE && img->type!=SI_SLICE)
|
311 |
|
|
{
|
312 |
|
|
val = currSlice->ref_pic_list_reordering_flag_l0 = u_1 ("SH: ref_pic_list_reordering_flag_l0", currStream);
|
313 |
|
|
|
314 |
|
|
if (val)
|
315 |
|
|
{
|
316 |
|
|
i=0;
|
317 |
|
|
do
|
318 |
|
|
{
|
319 |
|
|
val = currSlice->reordering_of_pic_nums_idc_l0[i] = ue_v("SH: reordering_of_pic_nums_idc_l0", currStream);
|
320 |
|
|
if (val==0 || val==1)
|
321 |
|
|
{
|
322 |
|
|
currSlice->abs_diff_pic_num_minus1_l0[i] = ue_v("SH: abs_diff_pic_num_minus1_l0", currStream);
|
323 |
|
|
}
|
324 |
|
|
else
|
325 |
|
|
{
|
326 |
|
|
if (val==2)
|
327 |
|
|
{
|
328 |
|
|
currSlice->long_term_pic_idx_l0[i] = ue_v("SH: long_term_pic_idx_l0", currStream);
|
329 |
|
|
}
|
330 |
|
|
}
|
331 |
|
|
i++;
|
332 |
|
|
// assert (i>img->num_ref_idx_l0_active);
|
333 |
|
|
} while (val != 3);
|
334 |
|
|
}
|
335 |
|
|
}
|
336 |
|
|
|
337 |
|
|
if (img->type==B_SLICE)
|
338 |
|
|
{
|
339 |
|
|
val = currSlice->ref_pic_list_reordering_flag_l1 = u_1 ("SH: ref_pic_list_reordering_flag_l1", currStream);
|
340 |
|
|
|
341 |
|
|
if (val)
|
342 |
|
|
{
|
343 |
|
|
i=0;
|
344 |
|
|
do
|
345 |
|
|
{
|
346 |
|
|
val = currSlice->reordering_of_pic_nums_idc_l1[i] = ue_v("SH: reordering_of_pic_nums_idc_l1", currStream);
|
347 |
|
|
if (val==0 || val==1)
|
348 |
|
|
{
|
349 |
|
|
currSlice->abs_diff_pic_num_minus1_l1[i] = ue_v("SH: abs_diff_pic_num_minus1_l1", currStream);
|
350 |
|
|
}
|
351 |
|
|
else
|
352 |
|
|
{
|
353 |
|
|
if (val==2)
|
354 |
|
|
{
|
355 |
|
|
currSlice->long_term_pic_idx_l1[i] = ue_v("SH: long_term_pic_idx_l1", currStream);
|
356 |
|
|
}
|
357 |
|
|
}
|
358 |
|
|
i++;
|
359 |
|
|
// assert (i>img->num_ref_idx_l1_active);
|
360 |
|
|
} while (val != 3);
|
361 |
|
|
}
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
// set reference index of redundant slices.
|
365 |
|
|
if(img->redundant_pic_cnt)
|
366 |
|
|
{
|
367 |
|
|
redundant_slice_ref_idx = currSlice->abs_diff_pic_num_minus1_l0[0] + 1;
|
368 |
|
|
}
|
369 |
|
|
}
|
370 |
|
|
|
371 |
|
|
/*!
|
372 |
|
|
************************************************************************
|
373 |
|
|
* \brief
|
374 |
|
|
* read the weighted prediction tables
|
375 |
|
|
************************************************************************
|
376 |
|
|
*/
|
377 |
|
|
static void pred_weight_table()
|
378 |
|
|
{
|
379 |
|
|
Slice *currSlice = img->currentSlice;
|
380 |
|
|
int dP_nr = assignSE2partition[currSlice->dp_mode][SE_HEADER];
|
381 |
|
|
DataPartition *partition = &(currSlice->partArr[dP_nr]);
|
382 |
|
|
Bitstream *currStream = partition->bitstream;
|
383 |
|
|
int luma_weight_flag_l0, luma_weight_flag_l1, chroma_weight_flag_l0, chroma_weight_flag_l1;
|
384 |
|
|
int i,j;
|
385 |
|
|
|
386 |
|
|
img->luma_log2_weight_denom = ue_v ("SH: luma_log2_weight_denom", currStream);
|
387 |
|
|
img->wp_round_luma = img->luma_log2_weight_denom ? 1<<(img->luma_log2_weight_denom - 1): 0;
|
388 |
|
|
|
389 |
|
|
if ( 0 != active_sps->chroma_format_idc)
|
390 |
|
|
{
|
391 |
|
|
img->chroma_log2_weight_denom = ue_v ("SH: chroma_log2_weight_denom", currStream);
|
392 |
|
|
img->wp_round_chroma = img->chroma_log2_weight_denom ? 1<<(img->chroma_log2_weight_denom - 1): 0;
|
393 |
|
|
}
|
394 |
|
|
|
395 |
|
|
reset_wp_params(img);
|
396 |
|
|
|
397 |
|
|
for (i=0; i<img->num_ref_idx_l0_active; i++)
|
398 |
|
|
{
|
399 |
|
|
luma_weight_flag_l0 = u_1("SH: luma_weight_flag_l0", currStream);
|
400 |
|
|
|
401 |
|
|
if (luma_weight_flag_l0)
|
402 |
|
|
{
|
403 |
|
|
img->wp_weight[0][i][0] = se_v ("SH: luma_weight_l0", currStream);
|
404 |
|
|
img->wp_offset[0][i][0] = se_v ("SH: luma_offset_l0", currStream);
|
405 |
|
|
}
|
406 |
|
|
else
|
407 |
|
|
{
|
408 |
|
|
img->wp_weight[0][i][0] = 1<<img->luma_log2_weight_denom;
|
409 |
|
|
img->wp_offset[0][i][0] = 0;
|
410 |
|
|
}
|
411 |
|
|
|
412 |
|
|
if (active_sps->chroma_format_idc != 0)
|
413 |
|
|
{
|
414 |
|
|
chroma_weight_flag_l0 = u_1 ("SH: chroma_weight_flag_l0", currStream);
|
415 |
|
|
|
416 |
|
|
for (j=1; j<3; j++)
|
417 |
|
|
{
|
418 |
|
|
if (chroma_weight_flag_l0)
|
419 |
|
|
{
|
420 |
|
|
img->wp_weight[0][i][j] = se_v("SH: chroma_weight_l0", currStream);
|
421 |
|
|
img->wp_offset[0][i][j] = se_v("SH: chroma_offset_l0", currStream);
|
422 |
|
|
}
|
423 |
|
|
else
|
424 |
|
|
{
|
425 |
|
|
img->wp_weight[0][i][j] = 1<<img->chroma_log2_weight_denom;
|
426 |
|
|
img->wp_offset[0][i][j] = 0;
|
427 |
|
|
}
|
428 |
|
|
}
|
429 |
|
|
}
|
430 |
|
|
}
|
431 |
|
|
if ((img->type == B_SLICE) && active_pps->weighted_bipred_idc == 1)
|
432 |
|
|
{
|
433 |
|
|
for (i=0; i<img->num_ref_idx_l1_active; i++)
|
434 |
|
|
{
|
435 |
|
|
luma_weight_flag_l1 = u_1("SH: luma_weight_flag_l1", currStream);
|
436 |
|
|
|
437 |
|
|
if (luma_weight_flag_l1)
|
438 |
|
|
{
|
439 |
|
|
img->wp_weight[1][i][0] = se_v ("SH: luma_weight_l1", currStream);
|
440 |
|
|
img->wp_offset[1][i][0] = se_v ("SH: luma_offset_l1", currStream);
|
441 |
|
|
}
|
442 |
|
|
else
|
443 |
|
|
{
|
444 |
|
|
img->wp_weight[1][i][0] = 1<<img->luma_log2_weight_denom;
|
445 |
|
|
img->wp_offset[1][i][0] = 0;
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
if (active_sps->chroma_format_idc != 0)
|
449 |
|
|
{
|
450 |
|
|
chroma_weight_flag_l1 = u_1 ("SH: chroma_weight_flag_l1", currStream);
|
451 |
|
|
|
452 |
|
|
for (j=1; j<3; j++)
|
453 |
|
|
{
|
454 |
|
|
if (chroma_weight_flag_l1)
|
455 |
|
|
{
|
456 |
|
|
img->wp_weight[1][i][j] = se_v("SH: chroma_weight_l1", currStream);
|
457 |
|
|
img->wp_offset[1][i][j] = se_v("SH: chroma_offset_l1", currStream);
|
458 |
|
|
}
|
459 |
|
|
else
|
460 |
|
|
{
|
461 |
|
|
img->wp_weight[1][i][j] = 1<<img->chroma_log2_weight_denom;
|
462 |
|
|
img->wp_offset[1][i][j] = 0;
|
463 |
|
|
}
|
464 |
|
|
}
|
465 |
|
|
}
|
466 |
|
|
}
|
467 |
|
|
}
|
468 |
|
|
}
|
469 |
|
|
|
470 |
|
|
|
471 |
|
|
/*!
|
472 |
|
|
************************************************************************
|
473 |
|
|
* \brief
|
474 |
|
|
* read the memory control operations
|
475 |
|
|
************************************************************************
|
476 |
|
|
*/
|
477 |
|
|
void dec_ref_pic_marking(Bitstream *currStream)
|
478 |
|
|
{
|
479 |
|
|
int val;
|
480 |
|
|
|
481 |
|
|
DecRefPicMarking_t *tmp_drpm,*tmp_drpm2;
|
482 |
|
|
|
483 |
|
|
// free old buffer content
|
484 |
|
|
while (img->dec_ref_pic_marking_buffer)
|
485 |
|
|
{
|
486 |
|
|
tmp_drpm=img->dec_ref_pic_marking_buffer;
|
487 |
|
|
|
488 |
|
|
img->dec_ref_pic_marking_buffer=tmp_drpm->Next;
|
489 |
|
|
free (tmp_drpm);
|
490 |
|
|
}
|
491 |
|
|
|
492 |
|
|
if (img->idr_flag)
|
493 |
|
|
{
|
494 |
|
|
img->no_output_of_prior_pics_flag = u_1("SH: no_output_of_prior_pics_flag", currStream);
|
495 |
|
|
img->long_term_reference_flag = u_1("SH: long_term_reference_flag", currStream);
|
496 |
|
|
}
|
497 |
|
|
else
|
498 |
|
|
{
|
499 |
|
|
img->adaptive_ref_pic_buffering_flag = u_1("SH: adaptive_ref_pic_buffering_flag", currStream);
|
500 |
|
|
if (img->adaptive_ref_pic_buffering_flag)
|
501 |
|
|
{
|
502 |
|
|
// read Memory Management Control Operation
|
503 |
|
|
do
|
504 |
|
|
{
|
505 |
|
|
tmp_drpm=(DecRefPicMarking_t*)calloc (1,sizeof (DecRefPicMarking_t));
|
506 |
|
|
tmp_drpm->Next=NULL;
|
507 |
|
|
|
508 |
|
|
val = tmp_drpm->memory_management_control_operation = ue_v("SH: memory_management_control_operation", currStream);
|
509 |
|
|
|
510 |
|
|
if ((val==1)||(val==3))
|
511 |
|
|
{
|
512 |
|
|
tmp_drpm->difference_of_pic_nums_minus1 = ue_v("SH: difference_of_pic_nums_minus1", currStream);
|
513 |
|
|
}
|
514 |
|
|
if (val==2)
|
515 |
|
|
{
|
516 |
|
|
tmp_drpm->long_term_pic_num = ue_v("SH: long_term_pic_num", currStream);
|
517 |
|
|
}
|
518 |
|
|
|
519 |
|
|
if ((val==3)||(val==6))
|
520 |
|
|
{
|
521 |
|
|
tmp_drpm->long_term_frame_idx = ue_v("SH: long_term_frame_idx", currStream);
|
522 |
|
|
}
|
523 |
|
|
if (val==4)
|
524 |
|
|
{
|
525 |
|
|
tmp_drpm->max_long_term_frame_idx_plus1 = ue_v("SH: max_long_term_pic_idx_plus1", currStream);
|
526 |
|
|
}
|
527 |
|
|
|
528 |
|
|
// add command
|
529 |
|
|
if (img->dec_ref_pic_marking_buffer==NULL)
|
530 |
|
|
{
|
531 |
|
|
img->dec_ref_pic_marking_buffer=tmp_drpm;
|
532 |
|
|
}
|
533 |
|
|
else
|
534 |
|
|
{
|
535 |
|
|
tmp_drpm2=img->dec_ref_pic_marking_buffer;
|
536 |
|
|
while (tmp_drpm2->Next!=NULL) tmp_drpm2=tmp_drpm2->Next;
|
537 |
|
|
tmp_drpm2->Next=tmp_drpm;
|
538 |
|
|
}
|
539 |
|
|
|
540 |
|
|
}while (val != 0);
|
541 |
|
|
|
542 |
|
|
}
|
543 |
|
|
}
|
544 |
|
|
}
|
545 |
|
|
|
546 |
|
|
/*!
|
547 |
|
|
************************************************************************
|
548 |
|
|
* \brief
|
549 |
|
|
* To calculate the poc values
|
550 |
|
|
* based upon JVT-F100d2
|
551 |
|
|
* POC200301: Until Jan 2003, this function will calculate the correct POC
|
552 |
|
|
* values, but the management of POCs in buffered pictures may need more work.
|
553 |
|
|
* \return
|
554 |
|
|
* none
|
555 |
|
|
************************************************************************
|
556 |
|
|
*/
|
557 |
|
|
void decode_poc(struct img_par *img)
|
558 |
|
|
{
|
559 |
|
|
int i;
|
560 |
|
|
// for POC mode 0:
|
561 |
|
|
unsigned int MaxPicOrderCntLsb = (1<<(active_sps->log2_max_pic_order_cnt_lsb_minus4+4));
|
562 |
|
|
|
563 |
|
|
switch ( active_sps->pic_order_cnt_type )
|
564 |
|
|
{
|
565 |
|
|
case 0: // POC MODE 0
|
566 |
|
|
// 1st
|
567 |
|
|
if(img->idr_flag)
|
568 |
|
|
{
|
569 |
|
|
img->PrevPicOrderCntMsb = 0;
|
570 |
|
|
img->PrevPicOrderCntLsb = 0;
|
571 |
|
|
}
|
572 |
|
|
else
|
573 |
|
|
{
|
574 |
|
|
if (img->last_has_mmco_5)
|
575 |
|
|
{
|
576 |
|
|
if (img->last_pic_bottom_field)
|
577 |
|
|
{
|
578 |
|
|
img->PrevPicOrderCntMsb = 0;
|
579 |
|
|
img->PrevPicOrderCntLsb = 0;
|
580 |
|
|
}
|
581 |
|
|
else
|
582 |
|
|
{
|
583 |
|
|
img->PrevPicOrderCntMsb = 0;
|
584 |
|
|
img->PrevPicOrderCntLsb = img->toppoc;
|
585 |
|
|
}
|
586 |
|
|
}
|
587 |
|
|
}
|
588 |
|
|
// Calculate the MSBs of current picture
|
589 |
|
|
if( img->pic_order_cnt_lsb < img->PrevPicOrderCntLsb &&
|
590 |
|
|
( img->PrevPicOrderCntLsb - img->pic_order_cnt_lsb ) >= ( MaxPicOrderCntLsb / 2 ) )
|
591 |
|
|
img->PicOrderCntMsb = img->PrevPicOrderCntMsb + MaxPicOrderCntLsb;
|
592 |
|
|
else if ( img->pic_order_cnt_lsb > img->PrevPicOrderCntLsb &&
|
593 |
|
|
( img->pic_order_cnt_lsb - img->PrevPicOrderCntLsb ) > ( MaxPicOrderCntLsb / 2 ) )
|
594 |
|
|
img->PicOrderCntMsb = img->PrevPicOrderCntMsb - MaxPicOrderCntLsb;
|
595 |
|
|
else
|
596 |
|
|
img->PicOrderCntMsb = img->PrevPicOrderCntMsb;
|
597 |
|
|
|
598 |
|
|
// 2nd
|
599 |
|
|
|
600 |
|
|
if(img->field_pic_flag==0)
|
601 |
|
|
{ //frame pix
|
602 |
|
|
img->toppoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
|
603 |
|
|
img->bottompoc = img->toppoc + img->delta_pic_order_cnt_bottom;
|
604 |
|
|
img->ThisPOC = img->framepoc = (img->toppoc < img->bottompoc)? img->toppoc : img->bottompoc; // POC200301
|
605 |
|
|
}
|
606 |
|
|
else if (img->bottom_field_flag==0)
|
607 |
|
|
{ //top field
|
608 |
|
|
img->ThisPOC= img->toppoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
|
609 |
|
|
}
|
610 |
|
|
else
|
611 |
|
|
{ //bottom field
|
612 |
|
|
img->ThisPOC= img->bottompoc = img->PicOrderCntMsb + img->pic_order_cnt_lsb;
|
613 |
|
|
}
|
614 |
|
|
img->framepoc=img->ThisPOC;
|
615 |
|
|
|
616 |
|
|
if ( img->frame_num!=img->PreviousFrameNum)
|
617 |
|
|
img->PreviousFrameNum=img->frame_num;
|
618 |
|
|
|
619 |
|
|
if(img->nal_reference_idc)
|
620 |
|
|
{
|
621 |
|
|
img->PrevPicOrderCntLsb = img->pic_order_cnt_lsb;
|
622 |
|
|
img->PrevPicOrderCntMsb = img->PicOrderCntMsb;
|
623 |
|
|
}
|
624 |
|
|
|
625 |
|
|
break;
|
626 |
|
|
|
627 |
|
|
case 1: // POC MODE 1
|
628 |
|
|
// 1st
|
629 |
|
|
if(img->idr_flag)
|
630 |
|
|
{
|
631 |
|
|
img->FrameNumOffset=0; // first pix of IDRGOP,
|
632 |
|
|
img->delta_pic_order_cnt[0]=0; //ignore first delta
|
633 |
|
|
if(img->frame_num)
|
634 |
|
|
error("frame_num not equal to zero in IDR picture", -1020);
|
635 |
|
|
}
|
636 |
|
|
else
|
637 |
|
|
{
|
638 |
|
|
if (img->last_has_mmco_5)
|
639 |
|
|
{
|
640 |
|
|
img->PreviousFrameNumOffset = 0;
|
641 |
|
|
img->PreviousFrameNum = 0;
|
642 |
|
|
}
|
643 |
|
|
if (img->frame_num<img->PreviousFrameNum)
|
644 |
|
|
{ //not first pix of IDRGOP
|
645 |
|
|
img->FrameNumOffset = img->PreviousFrameNumOffset + img->MaxFrameNum;
|
646 |
|
|
}
|
647 |
|
|
else
|
648 |
|
|
{
|
649 |
|
|
img->FrameNumOffset = img->PreviousFrameNumOffset;
|
650 |
|
|
}
|
651 |
|
|
}
|
652 |
|
|
|
653 |
|
|
// 2nd
|
654 |
|
|
if(active_sps->num_ref_frames_in_pic_order_cnt_cycle)
|
655 |
|
|
img->AbsFrameNum = img->FrameNumOffset+img->frame_num;
|
656 |
|
|
else
|
657 |
|
|
img->AbsFrameNum=0;
|
658 |
|
|
if( (!img->nal_reference_idc) && img->AbsFrameNum>0)
|
659 |
|
|
img->AbsFrameNum--;
|
660 |
|
|
|
661 |
|
|
// 3rd
|
662 |
|
|
img->ExpectedDeltaPerPicOrderCntCycle=0;
|
663 |
|
|
|
664 |
|
|
if(active_sps->num_ref_frames_in_pic_order_cnt_cycle)
|
665 |
|
|
for(i=0;i<(int) active_sps->num_ref_frames_in_pic_order_cnt_cycle;i++)
|
666 |
|
|
img->ExpectedDeltaPerPicOrderCntCycle += active_sps->offset_for_ref_frame[i];
|
667 |
|
|
|
668 |
|
|
if(img->AbsFrameNum)
|
669 |
|
|
{
|
670 |
|
|
img->PicOrderCntCycleCnt = (img->AbsFrameNum-1)/active_sps->num_ref_frames_in_pic_order_cnt_cycle;
|
671 |
|
|
img->FrameNumInPicOrderCntCycle = (img->AbsFrameNum-1)%active_sps->num_ref_frames_in_pic_order_cnt_cycle;
|
672 |
|
|
img->ExpectedPicOrderCnt = img->PicOrderCntCycleCnt*img->ExpectedDeltaPerPicOrderCntCycle;
|
673 |
|
|
for(i=0;i<=(int)img->FrameNumInPicOrderCntCycle;i++)
|
674 |
|
|
img->ExpectedPicOrderCnt += active_sps->offset_for_ref_frame[i];
|
675 |
|
|
}
|
676 |
|
|
else
|
677 |
|
|
img->ExpectedPicOrderCnt=0;
|
678 |
|
|
|
679 |
|
|
if(!img->nal_reference_idc)
|
680 |
|
|
img->ExpectedPicOrderCnt += active_sps->offset_for_non_ref_pic;
|
681 |
|
|
|
682 |
|
|
if(img->field_pic_flag==0)
|
683 |
|
|
{ //frame pix
|
684 |
|
|
img->toppoc = img->ExpectedPicOrderCnt + img->delta_pic_order_cnt[0];
|
685 |
|
|
img->bottompoc = img->toppoc + active_sps->offset_for_top_to_bottom_field + img->delta_pic_order_cnt[1];
|
686 |
|
|
img->ThisPOC = img->framepoc = (img->toppoc < img->bottompoc)? img->toppoc : img->bottompoc; // POC200301
|
687 |
|
|
}
|
688 |
|
|
else if (img->bottom_field_flag==0)
|
689 |
|
|
{ //top field
|
690 |
|
|
img->ThisPOC = img->toppoc = img->ExpectedPicOrderCnt + img->delta_pic_order_cnt[0];
|
691 |
|
|
}
|
692 |
|
|
else
|
693 |
|
|
{ //bottom field
|
694 |
|
|
img->ThisPOC = img->bottompoc = img->ExpectedPicOrderCnt + active_sps->offset_for_top_to_bottom_field + img->delta_pic_order_cnt[0];
|
695 |
|
|
}
|
696 |
|
|
img->framepoc=img->ThisPOC;
|
697 |
|
|
|
698 |
|
|
img->PreviousFrameNum=img->frame_num;
|
699 |
|
|
img->PreviousFrameNumOffset=img->FrameNumOffset;
|
700 |
|
|
|
701 |
|
|
break;
|
702 |
|
|
|
703 |
|
|
|
704 |
|
|
case 2: // POC MODE 2
|
705 |
|
|
if(img->idr_flag) // IDR picture
|
706 |
|
|
{
|
707 |
|
|
img->FrameNumOffset=0; // first pix of IDRGOP,
|
708 |
|
|
img->ThisPOC = img->framepoc = img->toppoc = img->bottompoc = 0;
|
709 |
|
|
if(img->frame_num)
|
710 |
|
|
error("frame_num not equal to zero in IDR picture", -1020);
|
711 |
|
|
}
|
712 |
|
|
else
|
713 |
|
|
{
|
714 |
|
|
if (img->last_has_mmco_5)
|
715 |
|
|
{
|
716 |
|
|
img->PreviousFrameNum = 0;
|
717 |
|
|
img->PreviousFrameNumOffset = 0;
|
718 |
|
|
}
|
719 |
|
|
if (img->frame_num<img->PreviousFrameNum)
|
720 |
|
|
img->FrameNumOffset = img->PreviousFrameNumOffset + img->MaxFrameNum;
|
721 |
|
|
else
|
722 |
|
|
img->FrameNumOffset = img->PreviousFrameNumOffset;
|
723 |
|
|
|
724 |
|
|
|
725 |
|
|
img->AbsFrameNum = img->FrameNumOffset+img->frame_num;
|
726 |
|
|
if(!img->nal_reference_idc)
|
727 |
|
|
img->ThisPOC = (2*img->AbsFrameNum - 1);
|
728 |
|
|
else
|
729 |
|
|
img->ThisPOC = (2*img->AbsFrameNum);
|
730 |
|
|
|
731 |
|
|
if (img->field_pic_flag==0)
|
732 |
|
|
img->toppoc = img->bottompoc = img->framepoc = img->ThisPOC;
|
733 |
|
|
else if (img->bottom_field_flag==0)
|
734 |
|
|
img->toppoc = img->framepoc = img->ThisPOC;
|
735 |
|
|
else img->bottompoc = img->framepoc = img->ThisPOC;
|
736 |
|
|
}
|
737 |
|
|
|
738 |
|
|
img->PreviousFrameNum=img->frame_num;
|
739 |
|
|
img->PreviousFrameNumOffset=img->FrameNumOffset;
|
740 |
|
|
break;
|
741 |
|
|
|
742 |
|
|
|
743 |
|
|
default:
|
744 |
|
|
//error must occurs
|
745 |
|
|
assert( 1==0 );
|
746 |
|
|
break;
|
747 |
|
|
}
|
748 |
|
|
}
|
749 |
|
|
|
750 |
|
|
/*!
|
751 |
|
|
************************************************************************
|
752 |
|
|
* \brief
|
753 |
|
|
* A little helper for the debugging of POC code
|
754 |
|
|
* \return
|
755 |
|
|
* none
|
756 |
|
|
************************************************************************
|
757 |
|
|
*/
|
758 |
|
|
int dumppoc(struct img_par *img) {
|
759 |
|
|
printf ("\nPOC locals...\n");
|
760 |
|
|
printf ("toppoc %d\n", img->toppoc);
|
761 |
|
|
printf ("bottompoc %d\n", img->bottompoc);
|
762 |
|
|
printf ("frame_num %d\n", img->frame_num);
|
763 |
|
|
printf ("field_pic_flag %d\n", img->field_pic_flag);
|
764 |
|
|
printf ("bottom_field_flag %d\n", img->bottom_field_flag);
|
765 |
|
|
printf ("POC SPS\n");
|
766 |
|
|
printf ("log2_max_frame_num_minus4 %d\n", active_sps->log2_max_frame_num_minus4); // POC200301
|
767 |
|
|
printf ("log2_max_pic_order_cnt_lsb_minus4 %d\n", active_sps->log2_max_pic_order_cnt_lsb_minus4);
|
768 |
|
|
printf ("pic_order_cnt_type %d\n", active_sps->pic_order_cnt_type);
|
769 |
|
|
printf ("num_ref_frames_in_pic_order_cnt_cycle %d\n", active_sps->num_ref_frames_in_pic_order_cnt_cycle);
|
770 |
|
|
printf ("delta_pic_order_always_zero_flag %d\n", active_sps->delta_pic_order_always_zero_flag);
|
771 |
|
|
printf ("offset_for_non_ref_pic %d\n", active_sps->offset_for_non_ref_pic);
|
772 |
|
|
printf ("offset_for_top_to_bottom_field %d\n", active_sps->offset_for_top_to_bottom_field);
|
773 |
|
|
printf ("offset_for_ref_frame[0] %d\n", active_sps->offset_for_ref_frame[0]);
|
774 |
|
|
printf ("offset_for_ref_frame[1] %d\n", active_sps->offset_for_ref_frame[1]);
|
775 |
|
|
printf ("POC in SLice Header\n");
|
776 |
|
|
printf ("pic_order_present_flag %d\n", active_pps->pic_order_present_flag);
|
777 |
|
|
printf ("delta_pic_order_cnt[0] %d\n", img->delta_pic_order_cnt[0]);
|
778 |
|
|
printf ("delta_pic_order_cnt[1] %d\n", img->delta_pic_order_cnt[1]);
|
779 |
|
|
printf ("delta_pic_order_cnt[2] %d\n", img->delta_pic_order_cnt[2]);
|
780 |
|
|
printf ("idr_flag %d\n", img->idr_flag);
|
781 |
|
|
printf ("MaxFrameNum %d\n", img->MaxFrameNum);
|
782 |
|
|
|
783 |
|
|
return 0;
|
784 |
|
|
}
|
785 |
|
|
|
786 |
|
|
/*!
|
787 |
|
|
************************************************************************
|
788 |
|
|
* \brief
|
789 |
|
|
* return the poc of img as per (8-1) JVT-F100d2
|
790 |
|
|
* POC200301
|
791 |
|
|
************************************************************************
|
792 |
|
|
*/
|
793 |
|
|
int picture_order(struct img_par *img)
|
794 |
|
|
{
|
795 |
|
|
if (img->field_pic_flag==0) // is a frame
|
796 |
|
|
return img->framepoc;
|
797 |
|
|
else if (img->bottom_field_flag==0) // top field
|
798 |
|
|
return img->toppoc;
|
799 |
|
|
else // bottom field
|
800 |
|
|
return img->bottompoc;
|
801 |
|
|
}
|
802 |
|
|
|