OpenCores
URL https://opencores.org/ocsvn/bluespec-h264/bluespec-h264/trunk

Subversion Repositories bluespec-h264

[/] [bluespec-h264/] [trunk/] [test/] [decoder/] [ldecod/] [src/] [parset.c] - Blame information for rev 100

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jamey.hick
 
2
/*!
3
 ************************************************************************
4
 *  \file
5
 *     parset.c
6
 *  \brief
7
 *     Parameter Sets
8
 *  \author
9
 *     Main contributors (see contributors.h for copyright, address and affiliation details)
10
 *     - Stephan Wenger          <stewe@cs.tu-berlin.de>
11
 *
12
 ***********************************************************************
13
 */
14
 
15
#include <stdlib.h>
16
#include <assert.h>
17
#include <string.h>
18
 
19
#include "global.h"
20
#include "parsetcommon.h"
21
#include "parset.h"
22
#include "nalu.h"
23
#include "memalloc.h"
24
#include "fmo.h"
25
#include "cabac.h"
26
#include "vlc.h"
27
#include "mbuffer.h"
28
#include "erc_api.h"
29
 
30
#if TRACE
31
#define SYMTRACESTRING(s) strncpy(sym->tracestring,s,TRACESTRING_SIZE)
32
#else
33
#define SYMTRACESTRING(s) // do nothing
34
#endif
35
 
36
const byte ZZ_SCAN[16]  =
37
{  0,  1,  4,  8,  5,  2,  3,  6,  9, 12, 13, 10,  7, 11, 14, 15
38
};
39
 
40
const byte ZZ_SCAN8[64] =
41
{  0,  1,  8, 16,  9,  2,  3, 10, 17, 24, 32, 25, 18, 11,  4,  5,
42
   12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13,  6,  7, 14, 21, 28,
43
   35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
44
   58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63
45
};
46
 
47
extern int UsedBits;      // for internal statistics, is adjusted by se_v, ue_v, u_1
48
extern ColocatedParams *Co_located;
49
 
50
extern int quant_intra_default[16];
51
extern int quant_inter_default[16];
52
extern int quant8_intra_default[64];
53
extern int quant8_inter_default[64];
54
 
55
seq_parameter_set_rbsp_t SeqParSet[MAXSPS];
56
pic_parameter_set_rbsp_t PicParSet[MAXPPS];
57
 
58
extern StorablePicture* dec_picture;
59
 
60
extern void init_frext(struct img_par *img);
61
 
62
// syntax for scaling list matrix values
63
void Scaling_List(int *scalingList, int sizeOfScalingList, Boolean *UseDefaultScalingMatrix, Bitstream *s)
64
{
65
  int j, scanj;
66
  int delta_scale, lastScale, nextScale;
67
 
68
  lastScale      = 8;
69
  nextScale      = 8;
70
 
71
  for(j=0; j<sizeOfScalingList; j++)
72
  {
73
    scanj = (sizeOfScalingList==16) ? ZZ_SCAN[j]:ZZ_SCAN8[j];
74
 
75
    if(nextScale!=0)
76
    {
77
      delta_scale = se_v (   "   : delta_sl   "                           , s);
78
      nextScale = (lastScale + delta_scale + 256) % 256;
79
      *UseDefaultScalingMatrix = (Boolean) (scanj==0 && nextScale==0);
80
    }
81
 
82
    scalingList[scanj] = (nextScale==0) ? lastScale:nextScale;
83
    lastScale = scalingList[scanj];
84
  }
85
}
86
// fill sps with content of p
87
 
88
int InterpretSPS (DataPartition *p, seq_parameter_set_rbsp_t *sps)
89
{
90
  unsigned i;
91
  int reserved_zero;
92
  Bitstream *s = p->bitstream;
93
 
94
  assert (p != NULL);
95
  assert (p->bitstream != NULL);
96
  assert (p->bitstream->streamBuffer != 0);
97
  assert (sps != NULL);
98
 
99
  UsedBits = 0;
100
 
101
  sps->profile_idc                            = u_v  (8, "SPS: profile_idc"                           , s);
102
 
103
  if ((sps->profile_idc!=66 ) &&
104
      (sps->profile_idc!=77 ) &&
105
      (sps->profile_idc!=88 ) &&
106
      (sps->profile_idc!=100 ) &&
107
      (sps->profile_idc!=110 ) &&
108
      (sps->profile_idc!=122 ) &&
109
      (sps->profile_idc!=144 ))
110
  {
111
    return UsedBits;
112
  }
113
 
114
  sps->constrained_set0_flag                  = u_1  (   "SPS: constrained_set0_flag"                 , s);
115
  sps->constrained_set1_flag                  = u_1  (   "SPS: constrained_set1_flag"                 , s);
116
  sps->constrained_set2_flag                  = u_1  (   "SPS: constrained_set2_flag"                 , s);
117
  sps->constrained_set3_flag                  = u_1  (   "SPS: constrained_set3_flag"                 , s);
118
  reserved_zero                               = u_v  (4, "SPS: reserved_zero_4bits"                   , s);
119
  assert (reserved_zero==0);
120
 
121
  sps->level_idc                              = u_v  (8, "SPS: level_idc"                             , s);
122
 
123
  sps->seq_parameter_set_id                   = ue_v ("SPS: seq_parameter_set_id"                     , s);
124
 
125
  // Fidelity Range Extensions stuff
126
  sps->chroma_format_idc = 1;
127
  sps->bit_depth_luma_minus8   = 0;
128
  sps->bit_depth_chroma_minus8 = 0;
129
  img->lossless_qpprime_flag   = 0;
130
 
131
  if((sps->profile_idc==FREXT_HP   ) ||
132
     (sps->profile_idc==FREXT_Hi10P) ||
133
     (sps->profile_idc==FREXT_Hi422) ||
134
     (sps->profile_idc==FREXT_Hi444))
135
  {
136
    sps->chroma_format_idc                      = ue_v ("SPS: chroma_format_idc"                       , s);
137
 
138
    // Residue Color Transform
139
    if(sps->chroma_format_idc == 3)
140
    {
141
      i                                         = u_1  ("SPS: residue_transform_flag"                  , s);
142
      if (i==1)
143
      {
144
        error ("[Deprecated High444 Profile] residue_transform_flag = 1 is no longer supported", 1000);
145
      }
146
    }
147
 
148
    sps->bit_depth_luma_minus8                  = ue_v ("SPS: bit_depth_luma_minus8"                   , s);
149
    sps->bit_depth_chroma_minus8                = ue_v ("SPS: bit_depth_chroma_minus8"                 , s);
150
    img->lossless_qpprime_flag                  = u_1  ("SPS: lossless_qpprime_y_zero_flag"            , s);
151
 
152
    sps->seq_scaling_matrix_present_flag        = u_1  (   "SPS: seq_scaling_matrix_present_flag"       , s);
153
 
154
    if(sps->seq_scaling_matrix_present_flag)
155
    {
156
      for(i=0; i<8; i++)
157
      {
158
        sps->seq_scaling_list_present_flag[i]   = u_1  (   "SPS: seq_scaling_list_present_flag"         , s);
159
        if(sps->seq_scaling_list_present_flag[i])
160
        {
161
          if(i<6)
162
            Scaling_List(sps->ScalingList4x4[i], 16, &sps->UseDefaultScalingMatrix4x4Flag[i], s);
163
          else
164
            Scaling_List(sps->ScalingList8x8[i-6], 64, &sps->UseDefaultScalingMatrix8x8Flag[i-6], s);
165
        }
166
      }
167
    }
168
  }
169
 
170
  sps->log2_max_frame_num_minus4              = ue_v ("SPS: log2_max_frame_num_minus4"                , s);
171
  sps->pic_order_cnt_type                     = ue_v ("SPS: pic_order_cnt_type"                       , s);
172
 
173
  if (sps->pic_order_cnt_type == 0)
174
    sps->log2_max_pic_order_cnt_lsb_minus4 = ue_v ("SPS: log2_max_pic_order_cnt_lsb_minus4"           , s);
175
  else if (sps->pic_order_cnt_type == 1)
176
  {
177
    sps->delta_pic_order_always_zero_flag      = u_1  ("SPS: delta_pic_order_always_zero_flag"       , s);
178
    sps->offset_for_non_ref_pic                = se_v ("SPS: offset_for_non_ref_pic"                 , s);
179
    sps->offset_for_top_to_bottom_field        = se_v ("SPS: offset_for_top_to_bottom_field"         , s);
180
    sps->num_ref_frames_in_pic_order_cnt_cycle = ue_v ("SPS: num_ref_frames_in_pic_order_cnt_cycle"  , s);
181
    for(i=0; i<sps->num_ref_frames_in_pic_order_cnt_cycle; i++)
182
      sps->offset_for_ref_frame[i]               = se_v ("SPS: offset_for_ref_frame[i]"              , s);
183
  }
184
  sps->num_ref_frames                        = ue_v ("SPS: num_ref_frames"                         , s);
185
  sps->gaps_in_frame_num_value_allowed_flag  = u_1  ("SPS: gaps_in_frame_num_value_allowed_flag"   , s);
186
  sps->pic_width_in_mbs_minus1               = ue_v ("SPS: pic_width_in_mbs_minus1"                , s);
187
  sps->pic_height_in_map_units_minus1        = ue_v ("SPS: pic_height_in_map_units_minus1"         , s);
188
  sps->frame_mbs_only_flag                   = u_1  ("SPS: frame_mbs_only_flag"                    , s);
189
  if (!sps->frame_mbs_only_flag)
190
  {
191
    sps->mb_adaptive_frame_field_flag        = u_1  ("SPS: mb_adaptive_frame_field_flag"           , s);
192
  }
193
  sps->direct_8x8_inference_flag             = u_1  ("SPS: direct_8x8_inference_flag"              , s);
194
  sps->frame_cropping_flag                   = u_1  ("SPS: frame_cropping_flag"                , s);
195
 
196
  if (sps->frame_cropping_flag)
197
  {
198
    sps->frame_cropping_rect_left_offset      = ue_v ("SPS: frame_cropping_rect_left_offset"           , s);
199
    sps->frame_cropping_rect_right_offset     = ue_v ("SPS: frame_cropping_rect_right_offset"          , s);
200
    sps->frame_cropping_rect_top_offset       = ue_v ("SPS: frame_cropping_rect_top_offset"            , s);
201
    sps->frame_cropping_rect_bottom_offset    = ue_v ("SPS: frame_cropping_rect_bottom_offset"         , s);
202
  }
203
  sps->vui_parameters_present_flag           = (Boolean) u_1  ("SPS: vui_parameters_present_flag"            , s);
204
 
205
  InitVUI(sps);
206
  ReadVUI(p, sps);
207
 
208
  sps->Valid = TRUE;
209
 
210
  return UsedBits;
211
}
212
 
213
 
214
void InitVUI(seq_parameter_set_rbsp_t *sps)
215
{
216
  sps->vui_seq_parameters.matrix_coefficients = 2;
217
}
218
 
219
 
220
int ReadVUI(DataPartition *p, seq_parameter_set_rbsp_t *sps)
221
{
222
  Bitstream *s = p->bitstream;
223
  if (sps->vui_parameters_present_flag)
224
  {
225
    sps->vui_seq_parameters.aspect_ratio_info_present_flag = u_1  ("VUI: aspect_ratio_info_present_flag"   , s);
226
    if (sps->vui_seq_parameters.aspect_ratio_info_present_flag)
227
    {
228
      sps->vui_seq_parameters.aspect_ratio_idc             = u_v  ( 8, "VUI: aspect_ratio_idc"              , s);
229
      if (255==sps->vui_seq_parameters.aspect_ratio_idc)
230
      {
231
        sps->vui_seq_parameters.sar_width                  = u_v  (16, "VUI: sar_width"                     , s);
232
        sps->vui_seq_parameters.sar_height                 = u_v  (16, "VUI: sar_height"                    , s);
233
      }
234
  }
235
 
236
    sps->vui_seq_parameters.overscan_info_present_flag     = u_1  ("VUI: overscan_info_present_flag"        , s);
237
    if (sps->vui_seq_parameters.overscan_info_present_flag)
238
    {
239
      sps->vui_seq_parameters.overscan_appropriate_flag    = u_1  ("VUI: overscan_appropriate_flag"         , s);
240
    }
241
 
242
    sps->vui_seq_parameters.video_signal_type_present_flag = u_1  ("VUI: video_signal_type_present_flag"    , s);
243
    if (sps->vui_seq_parameters.video_signal_type_present_flag)
244
    {
245
      sps->vui_seq_parameters.video_format                    = u_v  ( 3,"VUI: video_format"                      , s);
246
      sps->vui_seq_parameters.video_full_range_flag           = u_1  (   "VUI: video_full_range_flag"             , s);
247
      sps->vui_seq_parameters.colour_description_present_flag = u_1  (   "VUI: color_description_present_flag"    , s);
248
      if(sps->vui_seq_parameters.colour_description_present_flag)
249
      {
250
        sps->vui_seq_parameters.colour_primaries              = u_v  ( 8,"VUI: colour_primaries"                  , s);
251
        sps->vui_seq_parameters.transfer_characteristics      = u_v  ( 8,"VUI: transfer_characteristics"          , s);
252
        sps->vui_seq_parameters.matrix_coefficients           = u_v  ( 8,"VUI: matrix_coefficients"               , s);
253
      }
254
    }
255
    sps->vui_seq_parameters.chroma_location_info_present_flag = u_1  (   "VUI: chroma_loc_info_present_flag"      , s);
256
    if(sps->vui_seq_parameters.chroma_location_info_present_flag)
257
    {
258
      sps->vui_seq_parameters.chroma_sample_loc_type_top_field     = ue_v  ( "VUI: chroma_sample_loc_type_top_field"    , s);
259
      sps->vui_seq_parameters.chroma_sample_loc_type_bottom_field  = ue_v  ( "VUI: chroma_sample_loc_type_bottom_field" , s);
260
    }
261
    sps->vui_seq_parameters.timing_info_present_flag          = u_1  ("VUI: timing_info_present_flag"           , s);
262
    if (sps->vui_seq_parameters.timing_info_present_flag)
263
    {
264
      sps->vui_seq_parameters.num_units_in_tick               = u_v  (32,"VUI: num_units_in_tick"               , s);
265
      sps->vui_seq_parameters.time_scale                      = u_v  (32,"VUI: time_scale"                      , s);
266
      sps->vui_seq_parameters.fixed_frame_rate_flag           = u_1  (   "VUI: fixed_frame_rate_flag"           , s);
267
    }
268
    sps->vui_seq_parameters.nal_hrd_parameters_present_flag   = u_1  ("VUI: nal_hrd_parameters_present_flag"    , s);
269
    if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
270
    {
271
      ReadHRDParameters(p, &(sps->vui_seq_parameters.nal_hrd_parameters));
272
    }
273
    sps->vui_seq_parameters.vcl_hrd_parameters_present_flag   = u_1  ("VUI: vcl_hrd_parameters_present_flag"    , s);
274
    if (sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
275
    {
276
      ReadHRDParameters(p, &(sps->vui_seq_parameters.vcl_hrd_parameters));
277
    }
278
    if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag || sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
279
    {
280
      sps->vui_seq_parameters.low_delay_hrd_flag             =  u_1  ("VUI: low_delay_hrd_flag"                 , s);
281
    }
282
    sps->vui_seq_parameters.pic_struct_present_flag          =  u_1  ("VUI: pic_struct_present_flag   "         , s);
283
    sps->vui_seq_parameters.bitstream_restriction_flag       =  u_1  ("VUI: bitstream_restriction_flag"         , s);
284
    if (sps->vui_seq_parameters.bitstream_restriction_flag)
285
    {
286
      sps->vui_seq_parameters.motion_vectors_over_pic_boundaries_flag =  u_1  ("VUI: motion_vectors_over_pic_boundaries_flag", s);
287
      sps->vui_seq_parameters.max_bytes_per_pic_denom                 =  ue_v ("VUI: max_bytes_per_pic_denom"                , s);
288
      sps->vui_seq_parameters.max_bits_per_mb_denom                   =  ue_v ("VUI: max_bits_per_mb_denom"                  , s);
289
      sps->vui_seq_parameters.log2_max_mv_length_horizontal           =  ue_v ("VUI: log2_max_mv_length_horizontal"          , s);
290
      sps->vui_seq_parameters.log2_max_mv_length_vertical             =  ue_v ("VUI: log2_max_mv_length_vertical"            , s);
291
      sps->vui_seq_parameters.num_reorder_frames                      =  ue_v ("VUI: num_reorder_frames"                     , s);
292
      sps->vui_seq_parameters.max_dec_frame_buffering                 =  ue_v ("VUI: max_dec_frame_buffering"                , s);
293
    }
294
  }
295
 
296
  return 0;
297
}
298
 
299
 
300
int ReadHRDParameters(DataPartition *p, hrd_parameters_t *hrd)
301
{
302
  Bitstream *s = p->bitstream;
303
  unsigned int SchedSelIdx;
304
 
305
  hrd->cpb_cnt_minus1                                      = ue_v (   "VUI: cpb_cnt_minus1"                       , s);
306
  hrd->bit_rate_scale                                      = u_v  ( 4,"VUI: bit_rate_scale"                       , s);
307
  hrd->cpb_size_scale                                      = u_v  ( 4,"VUI: cpb_size_scale"                       , s);
308
 
309
  for( SchedSelIdx = 0; SchedSelIdx <= hrd->cpb_cnt_minus1; SchedSelIdx++ )
310
  {
311
    hrd->bit_rate_value_minus1[ SchedSelIdx ]             = ue_v  ( "VUI: bit_rate_value_minus1"                  , s);
312
    hrd->cpb_size_value_minus1[ SchedSelIdx ]             = ue_v  ( "VUI: cpb_size_value_minus1"                  , s);
313
    hrd->cbr_flag[ SchedSelIdx ]                          = u_1   ( "VUI: cbr_flag"                               , s);
314
  }
315
 
316
  hrd->initial_cpb_removal_delay_length_minus1            = u_v  ( 5,"VUI: initial_cpb_removal_delay_length_minus1" , s);
317
  hrd->cpb_removal_delay_length_minus1                    = u_v  ( 5,"VUI: cpb_removal_delay_length_minus1"         , s);
318
  hrd->dpb_output_delay_length_minus1                     = u_v  ( 5,"VUI: dpb_output_delay_length_minus1"          , s);
319
  hrd->time_offset_length                                 = u_v  ( 5,"VUI: time_offset_length"          , s);
320
 
321
  return 0;
322
}
323
 
324
 
325
int InterpretPPS (DataPartition *p, pic_parameter_set_rbsp_t *pps)
326
{
327
  unsigned i;
328
  int NumberBitsPerSliceGroupId;
329
  Bitstream *s = p->bitstream;
330
 
331
  assert (p != NULL);
332
  assert (p->bitstream != NULL);
333
  assert (p->bitstream->streamBuffer != 0);
334
  assert (pps != NULL);
335
 
336
  UsedBits = 0;
337
 
338
  pps->pic_parameter_set_id                  = ue_v ("PPS: pic_parameter_set_id"                   , s);
339
  pps->seq_parameter_set_id                  = ue_v ("PPS: seq_parameter_set_id"                   , s);
340
  pps->entropy_coding_mode_flag              = u_1  ("PPS: entropy_coding_mode_flag"               , s);
341
 
342
  //! Note: as per JVT-F078 the following bit is unconditional.  If F078 is not accepted, then
343
  //! one has to fetch the correct SPS to check whether the bit is present (hopefully there is
344
  //! no consistency problem :-(
345
  //! The current encoder code handles this in the same way.  When you change this, don't forget
346
  //! the encoder!  StW, 12/8/02
347
  pps->pic_order_present_flag                = u_1  ("PPS: pic_order_present_flag"                 , s);
348
 
349
  pps->num_slice_groups_minus1               = ue_v ("PPS: num_slice_groups_minus1"                , s);
350
 
351
  // FMO stuff begins here
352
  if (pps->num_slice_groups_minus1 > 0)
353
  {
354
    pps->slice_group_map_type               = ue_v ("PPS: slice_group_map_type"                , s);
355
    if (pps->slice_group_map_type == 0)
356
    {
357
      for (i=0; i<=pps->num_slice_groups_minus1; i++)
358
        pps->run_length_minus1 [i]                  = ue_v ("PPS: run_length_minus1 [i]"              , s);
359
    }
360
    else if (pps->slice_group_map_type == 2)
361
    {
362
      for (i=0; i<pps->num_slice_groups_minus1; i++)
363
      {
364
        //! JVT-F078: avoid reference of SPS by using ue(v) instead of u(v)
365
        pps->top_left [i]                          = ue_v ("PPS: top_left [i]"                        , s);
366
        pps->bottom_right [i]                      = ue_v ("PPS: bottom_right [i]"                    , s);
367
      }
368
    }
369
    else if (pps->slice_group_map_type == 3 ||
370
             pps->slice_group_map_type == 4 ||
371
             pps->slice_group_map_type == 5)
372
    {
373
      pps->slice_group_change_direction_flag     = u_1  ("PPS: slice_group_change_direction_flag"      , s);
374
      pps->slice_group_change_rate_minus1        = ue_v ("PPS: slice_group_change_rate_minus1"         , s);
375
    }
376
    else if (pps->slice_group_map_type == 6)
377
    {
378
      if (pps->num_slice_groups_minus1+1 >4)
379
        NumberBitsPerSliceGroupId = 3;
380
      else if (pps->num_slice_groups_minus1+1 > 2)
381
        NumberBitsPerSliceGroupId = 2;
382
      else
383
        NumberBitsPerSliceGroupId = 1;
384
      //! JVT-F078, exlicitly signal number of MBs in the map
385
      pps->num_slice_group_map_units_minus1      = ue_v ("PPS: num_slice_group_map_units_minus1"               , s);
386
      for (i=0; i<=pps->num_slice_group_map_units_minus1; i++)
387
        pps->slice_group_id[i] = u_v (NumberBitsPerSliceGroupId, "slice_group_id[i]", s);
388
    }
389
  }
390
 
391
  // End of FMO stuff
392
 
393
  pps->num_ref_idx_l0_active_minus1          = ue_v ("PPS: num_ref_idx_l0_active_minus1"           , s);
394
  pps->num_ref_idx_l1_active_minus1          = ue_v ("PPS: num_ref_idx_l1_active_minus1"           , s);
395
  pps->weighted_pred_flag                    = u_1  ("PPS: weighted_pred_flag"                     , s);
396
  pps->weighted_bipred_idc                   = u_v  ( 2, "PPS: weighted_bipred_idc"                , s);
397
  pps->pic_init_qp_minus26                   = se_v ("PPS: pic_init_qp_minus26"                    , s);
398
  pps->pic_init_qs_minus26                   = se_v ("PPS: pic_init_qs_minus26"                    , s);
399
 
400
  pps->chroma_qp_index_offset                = se_v ("PPS: chroma_qp_index_offset"                 , s);
401
 
402
  pps->deblocking_filter_control_present_flag = u_1 ("PPS: deblocking_filter_control_present_flag" , s);
403
  pps->constrained_intra_pred_flag           = u_1  ("PPS: constrained_intra_pred_flag"            , s);
404
  pps->redundant_pic_cnt_present_flag        = u_1  ("PPS: redundant_pic_cnt_present_flag"         , s);
405
 
406
  if(more_rbsp_data(s->streamBuffer, s->frame_bitoffset,s->bitstream_length)) // more_data_in_rbsp()
407
  {
408
    //Fidelity Range Extensions Stuff
409
    pps->transform_8x8_mode_flag           = u_1  ("PPS: transform_8x8_mode_flag"                , s);
410
    pps->pic_scaling_matrix_present_flag   =  u_1  ("PPS: pic_scaling_matrix_present_flag"        , s);
411
 
412
    if(pps->pic_scaling_matrix_present_flag)
413
    {
414
      for(i=0; i<(6+((unsigned)pps->transform_8x8_mode_flag<<1)); i++)
415
      {
416
        pps->pic_scaling_list_present_flag[i]= u_1  ("PPS: pic_scaling_list_present_flag"          , s);
417
 
418
        if(pps->pic_scaling_list_present_flag[i])
419
        {
420
          if(i<6)
421
            Scaling_List(pps->ScalingList4x4[i], 16, &pps->UseDefaultScalingMatrix4x4Flag[i], s);
422
          else
423
            Scaling_List(pps->ScalingList8x8[i-6], 64, &pps->UseDefaultScalingMatrix8x8Flag[i-6], s);
424
        }
425
      }
426
    }
427
    pps->second_chroma_qp_index_offset      = se_v ("PPS: second_chroma_qp_index_offset"          , s);
428
  }
429
  else
430
  {
431
    pps->second_chroma_qp_index_offset      = pps->chroma_qp_index_offset;
432
  }
433
 
434
  pps->Valid = TRUE;
435
  return UsedBits;
436
}
437
 
438
 
439
void PPSConsistencyCheck (pic_parameter_set_rbsp_t *pps)
440
{
441
  printf ("Consistency checking a picture parset, to be implemented\n");
442
//  if (pps->seq_parameter_set_id invalid then do something)
443
}
444
 
445
void SPSConsistencyCheck (seq_parameter_set_rbsp_t *sps)
446
{
447
  printf ("Consistency checking a sequence parset, to be implemented\n");
448
}
449
 
450
void MakePPSavailable (int id, pic_parameter_set_rbsp_t *pps)
451
{
452
  assert (pps->Valid == TRUE);
453
 
454
  if (PicParSet[id].Valid == TRUE && PicParSet[id].slice_group_id != NULL)
455
    free (PicParSet[id].slice_group_id);
456
 
457
  memcpy (&PicParSet[id], pps, sizeof (pic_parameter_set_rbsp_t));
458
 
459
  // we can simply use the memory provided with the pps. the PPS is destroyed after this function
460
  // call and will not try to free if pps->slice_group_id == NULL
461
  PicParSet[id].slice_group_id = pps->slice_group_id;
462
  pps->slice_group_id          = NULL;
463
}
464
 
465
void CleanUpPPS()
466
{
467
  int i;
468
 
469
  for (i=0; i<MAXPPS; i++)
470
  {
471
    if (PicParSet[i].Valid == TRUE && PicParSet[i].slice_group_id != NULL)
472
      free (PicParSet[i].slice_group_id);
473
 
474
    PicParSet[i].Valid = FALSE;
475
  }
476
}
477
 
478
 
479
void MakeSPSavailable (int id, seq_parameter_set_rbsp_t *sps)
480
{
481
  assert (sps->Valid == TRUE);
482
  memcpy (&SeqParSet[id], sps, sizeof (seq_parameter_set_rbsp_t));
483
}
484
 
485
 
486
void ProcessSPS (NALU_t *nalu)
487
{
488
  DataPartition *dp = AllocPartition(1);
489
  seq_parameter_set_rbsp_t *sps = AllocSPS();
490
  int dummy;
491
 
492
  memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
493
  dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
494
  dp->bitstream->ei_flag = 0;
495
  dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
496
  dummy = InterpretSPS (dp, sps);
497
 
498
  if (sps->Valid)
499
  {
500
    if (active_sps)
501
    {
502
      if (sps->seq_parameter_set_id == active_sps->seq_parameter_set_id)
503
      {
504
        if (!sps_is_equal(sps, active_sps))
505
        {
506
          if (dec_picture)
507
          {
508
            // this may only happen on slice loss
509
            exit_picture();
510
          }
511
          active_sps=NULL;
512
        }
513
      }
514
    }
515
    // SPSConsistencyCheck (pps);
516
    MakeSPSavailable (sps->seq_parameter_set_id, sps);
517
    img->profile_idc = sps->profile_idc; //ADD-VG
518
  }
519
 
520
  FreePartition (dp, 1);
521
  FreeSPS (sps);
522
}
523
 
524
 
525
void ProcessPPS (NALU_t *nalu)
526
{
527
  DataPartition *dp;
528
  pic_parameter_set_rbsp_t *pps;
529
  int dummy;
530
 
531
  dp = AllocPartition(1);
532
  pps = AllocPPS();
533
  memcpy (dp->bitstream->streamBuffer, &nalu->buf[1], nalu->len-1);
534
  dp->bitstream->code_len = dp->bitstream->bitstream_length = RBSPtoSODB (dp->bitstream->streamBuffer, nalu->len-1);
535
  dp->bitstream->ei_flag = 0;
536
  dp->bitstream->read_len = dp->bitstream->frame_bitoffset = 0;
537
  dummy = InterpretPPS (dp, pps);
538
  // PPSConsistencyCheck (pps);
539
  if (active_pps)
540
  {
541
    if (pps->pic_parameter_set_id == active_pps->pic_parameter_set_id)
542
    {
543
      if (!pps_is_equal(pps, active_pps))
544
      {
545
        if (dec_picture)
546
        {
547
          // this may only happen on slice loss
548
          exit_picture();
549
        }
550
        active_pps = NULL;
551
      }
552
    }
553
  }
554
  MakePPSavailable (pps->pic_parameter_set_id, pps);
555
  FreePartition (dp, 1);
556
  FreePPS (pps);
557
}
558
 
559
void activate_sps (seq_parameter_set_rbsp_t *sps)
560
{
561
  if (active_sps != sps)
562
  {
563
    if (dec_picture)
564
    {
565
      // this may only happen on slice loss
566
      exit_picture();
567
    }
568
    active_sps = sps;
569
 
570
    img->bitdepth_chroma = 0;
571
    img->width_cr        = 0;
572
    img->height_cr       = 0;
573
 
574
    // Fidelity Range Extensions stuff (part 1)
575
    img->bitdepth_luma   = sps->bit_depth_luma_minus8 + 8;
576
    if (sps->chroma_format_idc != YUV400)
577
      img->bitdepth_chroma = sps->bit_depth_chroma_minus8 + 8;
578
 
579
    img->MaxFrameNum = 1<<(sps->log2_max_frame_num_minus4+4);
580
    img->PicWidthInMbs = (sps->pic_width_in_mbs_minus1 +1);
581
    img->PicHeightInMapUnits = (sps->pic_height_in_map_units_minus1 +1);
582
    img->FrameHeightInMbs = ( 2 - sps->frame_mbs_only_flag ) * img->PicHeightInMapUnits;
583
    img->FrameSizeInMbs = img->PicWidthInMbs * img->FrameHeightInMbs;
584
 
585
    img->yuv_format=sps->chroma_format_idc;
586
 
587
    img->width = img->PicWidthInMbs * MB_BLOCK_SIZE;
588
    img->height = img->FrameHeightInMbs * MB_BLOCK_SIZE;
589
 
590
    if (sps->chroma_format_idc == YUV420)
591
    {
592
      img->width_cr = img->width >>1;
593
      img->height_cr = img->height >>1;
594
    }
595
    else if (sps->chroma_format_idc == YUV422)
596
    {
597
      img->width_cr = img->width >>1;
598
      img->height_cr = img->height;
599
    }
600
    else if (sps->chroma_format_idc == YUV444)
601
    {
602
      //YUV444
603
      img->width_cr = img->width;
604
      img->height_cr = img->height;
605
    }
606
 
607
    img->width_cr_m1 = img->width_cr - 1;
608
    init_frext(img);
609
    init_global_buffers();
610
    if (!img->no_output_of_prior_pics_flag)
611
    {
612
      flush_dpb();
613
    }
614
    init_dpb();
615
 
616
    if (NULL!=Co_located)
617
    {
618
      free_colocated(Co_located);
619
    }
620
    Co_located = alloc_colocated (img->width, img->height,sps->mb_adaptive_frame_field_flag);
621
    ercInit(img->width, img->height, 1);
622
  }
623
}
624
 
625
void activate_pps(pic_parameter_set_rbsp_t *pps)
626
{
627
  if (active_pps != pps)
628
  {
629
    if (dec_picture)
630
    {
631
      // this may only happen on slice loss
632
      exit_picture();
633
    }
634
 
635
    active_pps = pps;
636
 
637
    // Fidelity Range Extensions stuff (part 2)
638
    img->Transform8x8Mode = pps->transform_8x8_mode_flag;
639
 
640
  }
641
}
642
 
643
void UseParameterSet (int PicParsetId)
644
{
645
  seq_parameter_set_rbsp_t *sps = &SeqParSet[PicParSet[PicParsetId].seq_parameter_set_id];
646
  pic_parameter_set_rbsp_t *pps = &PicParSet[PicParsetId];
647
  int i;
648
 
649
 
650
  if (PicParSet[PicParsetId].Valid != TRUE)
651
    printf ("Trying to use an invalid (uninitialized) Picture Parameter Set with ID %d, expect the unexpected...\n", PicParsetId);
652
  if (SeqParSet[PicParSet[PicParsetId].seq_parameter_set_id].Valid != TRUE)
653
    printf ("PicParset %d references an invalid (uninitialized) Sequence Parameter Set with ID %d, expect the unexpected...\n", PicParsetId, PicParSet[PicParsetId].seq_parameter_set_id);
654
 
655
  sps =  &SeqParSet[PicParSet[PicParsetId].seq_parameter_set_id];
656
 
657
 
658
  // In theory, and with a well-designed software, the lines above
659
  // are everything necessary.  In practice, we need to patch many values
660
  // in img-> (but no more in inp-> -- these have been taken care of)
661
 
662
  // Sequence Parameter Set Stuff first
663
 
664
//  printf ("Using Picture Parameter set %d and associated Sequence Parameter Set %d\n", PicParsetId, PicParSet[PicParsetId].seq_parameter_set_id);
665
 
666
  if ((int) sps->pic_order_cnt_type < 0 || sps->pic_order_cnt_type > 2)  // != 1
667
  {
668
    printf ("invalid sps->pic_order_cnt_type = %d\n", sps->pic_order_cnt_type);
669
    error ("pic_order_cnt_type != 1", -1000);
670
  }
671
 
672
  if (sps->pic_order_cnt_type == 1)
673
  {
674
    if(sps->num_ref_frames_in_pic_order_cnt_cycle >= MAXnum_ref_frames_in_pic_order_cnt_cycle)
675
    {
676
      error("num_ref_frames_in_pic_order_cnt_cycle too large",-1011);
677
    }
678
  }
679
 
680
  activate_sps(sps);
681
  activate_pps(pps);
682
 
683
 
684
  // currSlice->dp_mode is set by read_new_slice (NALU first byte available there)
685
  if (pps->entropy_coding_mode_flag == UVLC)
686
  {
687
    nal_startcode_follows = uvlc_startcode_follows;
688
    for (i=0; i<3; i++)
689
    {
690
      img->currentSlice->partArr[i].readSyntaxElement = readSyntaxElement_UVLC;
691
    }
692
  }
693
  else
694
  {
695
    nal_startcode_follows = cabac_startcode_follows;
696
    for (i=0; i<3; i++)
697
    {
698
      img->currentSlice->partArr[i].readSyntaxElement = readSyntaxElement_CABAC;
699
    }
700
  }
701
}
702
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.