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