• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 #include "avcdec_lib.h"
19 #include "avcdec_bitstream.h"
20 #include "oscl_mem.h"
21 #include "avcdec_api.h"
22 
23 /** see subclause 7.4.2.1 */
DecodeSPS(AVCDecObject * decvid,AVCDecBitstream * stream)24 AVCDec_Status DecodeSPS(AVCDecObject *decvid, AVCDecBitstream *stream)
25 {
26     AVCDec_Status status = AVCDEC_SUCCESS;
27     AVCSeqParamSet *seqParam;
28     uint temp;
29     int i;
30     uint profile_idc, constrained_set0_flag, constrained_set1_flag, constrained_set2_flag;
31     uint level_idc, seq_parameter_set_id;
32     void *userData = decvid->avcHandle->userData;
33     AVCHandle *avcHandle = decvid->avcHandle;
34 
35     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "DecodeSPS", -1, -1);
36 
37     BitstreamReadBits(stream, 8, &profile_idc);
38     BitstreamRead1Bit(stream, &constrained_set0_flag);
39 //  if (profile_idc != 66 && constrained_set0_flag != 1)
40 //  {
41 //      return AVCDEC_FAIL;
42 //  }
43     BitstreamRead1Bit(stream, &constrained_set1_flag);
44     BitstreamRead1Bit(stream, &constrained_set2_flag);
45     BitstreamReadBits(stream, 5, &temp);
46     BitstreamReadBits(stream, 8, &level_idc);
47     if (level_idc > 51)
48     {
49         return AVCDEC_FAIL;
50     }
51     if (mapLev2Idx[level_idc] == 255)
52     {
53         return AVCDEC_FAIL;
54     }
55     ue_v(stream, &seq_parameter_set_id);
56 
57     if (seq_parameter_set_id > 31)
58     {
59         return AVCDEC_FAIL;
60     }
61 
62     /* Allocate sequence param set for seqParams[seq_parameter_set_id]. */
63     if (decvid->seqParams[seq_parameter_set_id] == NULL)  /* allocate seqParams[id] */
64     {
65         decvid->seqParams[seq_parameter_set_id] =
66             (AVCSeqParamSet*) avcHandle->CBAVC_Malloc(userData, sizeof(AVCSeqParamSet), DEFAULT_ATTR);
67 
68         if (decvid->seqParams[seq_parameter_set_id] == NULL)
69         {
70             return AVCDEC_MEMORY_FAIL;
71         }
72     }
73 
74     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "done alloc seqParams", -1, -1);
75 
76     seqParam = decvid->seqParams[seq_parameter_set_id];
77 
78     seqParam->profile_idc = profile_idc;
79     seqParam->constrained_set0_flag = constrained_set0_flag;
80     seqParam->constrained_set1_flag = constrained_set1_flag;
81     seqParam->constrained_set2_flag = constrained_set2_flag;
82     seqParam->level_idc = level_idc;
83     seqParam->seq_parameter_set_id = seq_parameter_set_id;
84 
85     /* continue decoding SPS */
86     ue_v(stream, &(seqParam->log2_max_frame_num_minus4));
87 
88     if (seqParam->log2_max_frame_num_minus4 > 12)
89     {
90         return AVCDEC_FAIL;
91     }
92 
93     ue_v(stream, &(seqParam->pic_order_cnt_type));
94 
95     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 1", seqParam->log2_max_frame_num_minus4, seqParam->pic_order_cnt_type);
96 
97     if (seqParam->pic_order_cnt_type == 0)
98     {
99         ue_v(stream, &(seqParam->log2_max_pic_order_cnt_lsb_minus4));
100     }
101     else if (seqParam->pic_order_cnt_type == 1)
102     {               // MC_CHECK
103         BitstreamRead1Bit(stream, (uint*)&(seqParam->delta_pic_order_always_zero_flag));
104         se_v32bit(stream, &(seqParam->offset_for_non_ref_pic));
105         se_v32bit(stream, &(seqParam->offset_for_top_to_bottom_field));
106         ue_v(stream, &(seqParam->num_ref_frames_in_pic_order_cnt_cycle));
107 
108         for (i = 0; i < (int)(seqParam->num_ref_frames_in_pic_order_cnt_cycle); i++)
109         {
110             se_v32bit(stream, &(seqParam->offset_for_ref_frame[i]));
111         }
112     }
113 
114     ue_v(stream, &(seqParam->num_ref_frames));
115 
116     if (seqParam->num_ref_frames > 16)
117     {
118         return AVCDEC_FAIL;
119     }
120 
121     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 2", seqParam->num_ref_frames, -1);
122 
123     BitstreamRead1Bit(stream, (uint*)&(seqParam->gaps_in_frame_num_value_allowed_flag));
124     ue_v(stream, &(seqParam->pic_width_in_mbs_minus1));
125 
126     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "picwidth", seqParam->pic_width_in_mbs_minus1, -1);
127 
128     ue_v(stream, &(seqParam->pic_height_in_map_units_minus1));
129 
130     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "picwidth", seqParam->pic_height_in_map_units_minus1, -1);
131 
132     BitstreamRead1Bit(stream, (uint*)&(seqParam->frame_mbs_only_flag));
133 
134     seqParam->mb_adaptive_frame_field_flag = 0; /* default value */
135     if (!seqParam->frame_mbs_only_flag)
136     {
137         BitstreamRead1Bit(stream, (uint*)&(seqParam->mb_adaptive_frame_field_flag));
138     }
139 
140     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 3", seqParam->frame_mbs_only_flag, -1);
141 
142     BitstreamRead1Bit(stream, (uint*)&(seqParam->direct_8x8_inference_flag));
143 
144     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 4", seqParam->direct_8x8_inference_flag, -1);
145 
146     BitstreamRead1Bit(stream, (uint*)&(seqParam->frame_cropping_flag));
147     seqParam->frame_crop_left_offset = 0;  /* default value */
148     seqParam->frame_crop_right_offset = 0;/* default value */
149     seqParam->frame_crop_top_offset = 0;/* default value */
150     seqParam->frame_crop_bottom_offset = 0;/* default value */
151     if (seqParam->frame_cropping_flag)
152     {
153         ue_v(stream, &(seqParam->frame_crop_left_offset));
154         ue_v(stream, &(seqParam->frame_crop_right_offset));
155         ue_v(stream, &(seqParam->frame_crop_top_offset));
156         ue_v(stream, &(seqParam->frame_crop_bottom_offset));
157     }
158 
159     DEBUG_LOG(userData, AVC_LOGTYPE_INFO, "check point 5", seqParam->frame_cropping_flag, -1);
160 
161     BitstreamRead1Bit(stream, (uint*)&(seqParam->vui_parameters_present_flag));
162     if (seqParam->vui_parameters_present_flag)
163     {
164         status = vui_parameters(decvid, stream, seqParam);
165         if (status != AVCDEC_SUCCESS)
166         {
167             return AVCDEC_FAIL;
168         }
169     }
170 
171     return status;
172 }
173 
174 
vui_parameters(AVCDecObject * decvid,AVCDecBitstream * stream,AVCSeqParamSet * currSPS)175 AVCDec_Status vui_parameters(AVCDecObject *decvid, AVCDecBitstream *stream, AVCSeqParamSet *currSPS)
176 {
177     uint temp;
178     uint temp32;
179     uint aspect_ratio_idc, overscan_appopriate_flag, video_format, video_full_range_flag;
180     /* aspect_ratio_info_present_flag */
181     BitstreamRead1Bit(stream, &temp);
182     if (temp)
183     {
184         BitstreamReadBits(stream, 8, &aspect_ratio_idc);
185         if (aspect_ratio_idc == 255)
186         {
187             /* sar_width */
188             BitstreamReadBits(stream, 16, &temp);
189             /* sar_height */
190             BitstreamReadBits(stream, 16, &temp);
191         }
192     }
193     /* overscan_info_present */
194     BitstreamRead1Bit(stream, &temp);
195     if (temp)
196     {
197         BitstreamRead1Bit(stream, &overscan_appopriate_flag);
198     }
199     /* video_signal_type_present_flag */
200     BitstreamRead1Bit(stream, &temp);
201     if (temp)
202     {
203         BitstreamReadBits(stream, 3, &video_format);
204         BitstreamRead1Bit(stream, &video_full_range_flag);
205         /* colour_description_present_flag */
206         BitstreamRead1Bit(stream, &temp);
207         if (temp)
208         {
209             /* colour_primaries */
210             BitstreamReadBits(stream, 8, &temp);
211             /* transfer_characteristics */
212             BitstreamReadBits(stream, 8, &temp);
213             /* matrix coefficients */
214             BitstreamReadBits(stream, 8, &temp);
215         }
216     }
217     /*  chroma_loc_info_present_flag */
218     BitstreamRead1Bit(stream, &temp);
219     if (temp)
220     {
221         /*  chroma_sample_loc_type_top_field */
222         ue_v(stream, &temp);
223         /*  chroma_sample_loc_type_bottom_field */
224         ue_v(stream, &temp);
225     }
226 
227     /*  timing_info_present_flag*/
228     BitstreamRead1Bit(stream, &temp);
229     if (temp)
230     {
231         /*  num_unit_in_tick*/
232         BitstreamReadBits(stream, 32, &temp32);
233         /*  time_scale */
234         BitstreamReadBits(stream, 32, &temp32);
235         /*  fixed_frame_rate_flag */
236         BitstreamRead1Bit(stream, &temp);
237     }
238 
239     /*  nal_hrd_parameters_present_flag */
240     BitstreamRead1Bit(stream, &temp);
241     currSPS->vui_parameters.nal_hrd_parameters_present_flag = temp;
242     if (temp)
243     {
244         hrd_parameters(decvid, stream, &(currSPS->vui_parameters.nal_hrd_parameters));
245     }
246     /*  vcl_hrd_parameters_present_flag*/
247     BitstreamRead1Bit(stream, &temp);
248     currSPS->vui_parameters.vcl_hrd_parameters_present_flag = temp;
249     if (temp)
250     {
251         hrd_parameters(decvid, stream, &(currSPS->vui_parameters.vcl_hrd_parameters));
252     }
253     if (currSPS->vui_parameters.nal_hrd_parameters_present_flag || currSPS->vui_parameters.vcl_hrd_parameters_present_flag)
254     {
255         /*  low_delay_hrd_flag */
256         BitstreamRead1Bit(stream, &temp);
257     }
258     /*  pic_struct_present_flag */
259     BitstreamRead1Bit(stream, &temp);
260     currSPS->vui_parameters.pic_struct_present_flag = temp;
261     /*  bitstream_restriction_flag */
262     BitstreamRead1Bit(stream, &temp);
263     if (temp)
264     {
265         /*  motion_vectors_over_pic_boundaries_flag */
266         BitstreamRead1Bit(stream, &temp);
267         /*  max_bytes_per_pic_denom */
268         ue_v(stream, &temp);
269         /*  max_bits_per_mb_denom */
270         ue_v(stream, &temp);
271         /*  log2_max_mv_length_horizontal */
272         ue_v(stream, &temp);
273         /*  log2_max_mv_length_vertical */
274         ue_v(stream, &temp);
275         /*  num_reorder_frames */
276         ue_v(stream, &temp);
277         /*  max_dec_frame_buffering */
278         ue_v(stream, &temp);
279     }
280     return AVCDEC_SUCCESS;
281 }
hrd_parameters(AVCDecObject * decvid,AVCDecBitstream * stream,AVCHRDParams * HRDParam)282 AVCDec_Status hrd_parameters(AVCDecObject *decvid, AVCDecBitstream *stream, AVCHRDParams *HRDParam)
283 {
284     OSCL_UNUSED_ARG(decvid);
285     uint temp;
286     uint cpb_cnt_minus1;
287     uint i;
288     ue_v(stream, &cpb_cnt_minus1);
289     HRDParam->cpb_cnt_minus1 = cpb_cnt_minus1;
290     /*  bit_rate_scale */
291     BitstreamReadBits(stream, 4, &temp);
292     /*  cpb_size_scale */
293     BitstreamReadBits(stream, 4, &temp);
294     for (i = 0; i <= cpb_cnt_minus1; i++)
295     {
296         /*  bit_rate_value_minus1[i] */
297         ue_v(stream, &temp);
298         /*  cpb_size_value_minus1[i] */
299         ue_v(stream, &temp);
300         /*  cbr_flag[i] */
301         ue_v(stream, &temp);
302     }
303     /*  initial_cpb_removal_delay_length_minus1 */
304     BitstreamReadBits(stream, 5, &temp);
305     /*  cpb_removal_delay_length_minus1 */
306     BitstreamReadBits(stream, 5, &temp);
307     HRDParam->cpb_removal_delay_length_minus1 = temp;
308     /*  dpb_output_delay_length_minus1 */
309     BitstreamReadBits(stream, 5, &temp);
310     HRDParam->dpb_output_delay_length_minus1 = temp;
311     /*  time_offset_length  */
312     BitstreamReadBits(stream, 5, &temp);
313     HRDParam->time_offset_length = temp;
314     return AVCDEC_SUCCESS;
315 }
316 
317 
318 /** see subclause 7.4.2.2 */
DecodePPS(AVCDecObject * decvid,AVCCommonObj * video,AVCDecBitstream * stream)319 AVCDec_Status DecodePPS(AVCDecObject *decvid, AVCCommonObj *video, AVCDecBitstream *stream)
320 {
321     AVCPicParamSet *picParam;
322     AVCDec_Status status;
323     int i, iGroup, numBits;
324     int PicWidthInMbs, PicHeightInMapUnits, PicSizeInMapUnits;
325     uint pic_parameter_set_id, seq_parameter_set_id;
326     void *userData = decvid->avcHandle->userData;
327     AVCHandle *avcHandle = decvid->avcHandle;
328 
329     ue_v(stream, &pic_parameter_set_id);
330     if (pic_parameter_set_id > 255)
331     {
332         return AVCDEC_FAIL;
333     }
334 
335     ue_v(stream, &seq_parameter_set_id);
336 
337     if (seq_parameter_set_id > 31)
338     {
339         return AVCDEC_FAIL;
340     }
341 
342     /* 2.1 if picParams[pic_param_set_id] is NULL, allocate it. */
343     if (decvid->picParams[pic_parameter_set_id] == NULL)
344     {
345         decvid->picParams[pic_parameter_set_id] =
346             (AVCPicParamSet*)avcHandle->CBAVC_Malloc(userData, sizeof(AVCPicParamSet), DEFAULT_ATTR);
347         if (decvid->picParams[pic_parameter_set_id] == NULL)
348         {
349             return AVCDEC_MEMORY_FAIL;
350         }
351 
352         decvid->picParams[pic_parameter_set_id]->slice_group_id = NULL;
353     }
354 
355     video->currPicParams = picParam = decvid->picParams[pic_parameter_set_id];
356     picParam->seq_parameter_set_id = seq_parameter_set_id;
357     picParam->pic_parameter_set_id = pic_parameter_set_id;
358 
359     BitstreamRead1Bit(stream, (uint*)&(picParam->entropy_coding_mode_flag));
360     if (picParam->entropy_coding_mode_flag)
361     {
362         status = AVCDEC_FAIL;
363         goto clean_up;
364     }
365     BitstreamRead1Bit(stream, (uint*)&(picParam->pic_order_present_flag));
366     ue_v(stream, &(picParam->num_slice_groups_minus1));
367 
368     if (picParam->num_slice_groups_minus1 > MAX_NUM_SLICE_GROUP - 1)
369     {
370         status = AVCDEC_FAIL;
371         goto clean_up;
372     }
373 
374     picParam->slice_group_change_rate_minus1 = 0; /* default value */
375     if (picParam->num_slice_groups_minus1 > 0)
376     {
377         ue_v(stream, &(picParam->slice_group_map_type));
378         if (picParam->slice_group_map_type == 0)
379         {
380             for (iGroup = 0; iGroup <= (int)picParam->num_slice_groups_minus1; iGroup++)
381             {
382                 ue_v(stream, &(picParam->run_length_minus1[iGroup]));
383             }
384         }
385         else if (picParam->slice_group_map_type == 2)
386         {   // MC_CHECK  <= or <
387             for (iGroup = 0; iGroup < (int)picParam->num_slice_groups_minus1; iGroup++)
388             {
389                 ue_v(stream, &(picParam->top_left[iGroup]));
390                 ue_v(stream, &(picParam->bottom_right[iGroup]));
391             }
392         }
393         else if (picParam->slice_group_map_type == 3 ||
394                  picParam->slice_group_map_type == 4 ||
395                  picParam->slice_group_map_type == 5)
396         {
397             BitstreamRead1Bit(stream, (uint*)&(picParam->slice_group_change_direction_flag));
398             ue_v(stream, &(picParam->slice_group_change_rate_minus1));
399         }
400         else if (picParam->slice_group_map_type == 6)
401         {
402             ue_v(stream, &(picParam->pic_size_in_map_units_minus1));
403 
404             numBits = 0;/* ceil(log2(num_slice_groups_minus1+1)) bits */
405             i = picParam->num_slice_groups_minus1;
406             while (i > 0)
407             {
408                 numBits++;
409                 i >>= 1;
410             }
411 
412             i = picParam->seq_parameter_set_id;
413             if (decvid->seqParams[i] == NULL)
414             {
415                 status = AVCDEC_FAIL;
416                 goto clean_up;
417             }
418 
419 
420             PicWidthInMbs = decvid->seqParams[i]->pic_width_in_mbs_minus1 + 1;
421             PicHeightInMapUnits = decvid->seqParams[i]->pic_height_in_map_units_minus1 + 1 ;
422             PicSizeInMapUnits = PicWidthInMbs * PicHeightInMapUnits ;
423 
424             /* information has to be consistent with the seq_param */
425             if ((int)picParam->pic_size_in_map_units_minus1 != PicSizeInMapUnits - 1)
426             {
427                 status = AVCDEC_FAIL;
428                 goto clean_up;
429             }
430 
431             if (picParam->slice_group_id)
432             {
433                 avcHandle->CBAVC_Free(userData, (int)picParam->slice_group_id);
434             }
435             picParam->slice_group_id = (uint*)avcHandle->CBAVC_Malloc(userData, sizeof(uint) * PicSizeInMapUnits, DEFAULT_ATTR);
436             if (picParam->slice_group_id == NULL)
437             {
438                 status =  AVCDEC_MEMORY_FAIL;
439                 goto clean_up;
440             }
441 
442             for (i = 0; i < PicSizeInMapUnits; i++)
443             {
444                 BitstreamReadBits(stream, numBits, &(picParam->slice_group_id[i]));
445             }
446         }
447 
448     }
449 
450     ue_v(stream, &(picParam->num_ref_idx_l0_active_minus1));
451     if (picParam->num_ref_idx_l0_active_minus1 > 31)
452     {
453         status = AVCDEC_FAIL; /* out of range */
454         goto clean_up;
455     }
456 
457     ue_v(stream, &(picParam->num_ref_idx_l1_active_minus1));
458     if (picParam->num_ref_idx_l1_active_minus1 > 31)
459     {
460         status = AVCDEC_FAIL; /* out of range */
461         goto clean_up;
462     }
463 
464     BitstreamRead1Bit(stream, (uint*)&(picParam->weighted_pred_flag));
465     BitstreamReadBits(stream, 2, &(picParam->weighted_bipred_idc));
466     if (picParam->weighted_bipred_idc > 2)
467     {
468         status = AVCDEC_FAIL; /* out of range */
469         goto clean_up;
470     }
471 
472     se_v(stream, &(picParam->pic_init_qp_minus26));
473     if (picParam->pic_init_qp_minus26 < -26 || picParam->pic_init_qp_minus26 > 25)
474     {
475         status = AVCDEC_FAIL; /* out of range */
476         goto clean_up;
477     }
478 
479     se_v(stream, &(picParam->pic_init_qs_minus26));
480     if (picParam->pic_init_qs_minus26 < -26 || picParam->pic_init_qs_minus26 > 25)
481     {
482         status = AVCDEC_FAIL; /* out of range */
483         goto clean_up;
484     }
485 
486     se_v(stream, &(picParam->chroma_qp_index_offset));
487     if (picParam->chroma_qp_index_offset < -12 || picParam->chroma_qp_index_offset > 12)
488     {
489         status = AVCDEC_FAIL; /* out of range */
490         status = AVCDEC_FAIL; /* out of range */
491         goto clean_up;
492     }
493 
494     BitstreamReadBits(stream, 3, &pic_parameter_set_id);
495     picParam->deblocking_filter_control_present_flag = pic_parameter_set_id >> 2;
496     picParam->constrained_intra_pred_flag = (pic_parameter_set_id >> 1) & 1;
497     picParam->redundant_pic_cnt_present_flag = pic_parameter_set_id & 1;
498 
499     return AVCDEC_SUCCESS;
500 clean_up:
501     if (decvid->picParams[pic_parameter_set_id])
502     {
503         if (picParam->slice_group_id)
504         {
505             avcHandle->CBAVC_Free(userData, (int)picParam->slice_group_id);
506         }
507         decvid->picParams[pic_parameter_set_id]->slice_group_id = NULL;
508         avcHandle->CBAVC_Free(userData, (int)decvid->picParams[pic_parameter_set_id]);
509         decvid->picParams[pic_parameter_set_id] = NULL;
510         return status;
511     }
512     return AVCDEC_SUCCESS;
513 }
514 
515 
516 /* FirstPartOfSliceHeader();
517     RestOfSliceHeader() */
518 /** see subclause 7.4.3 */
DecodeSliceHeader(AVCDecObject * decvid,AVCCommonObj * video,AVCDecBitstream * stream)519 AVCDec_Status DecodeSliceHeader(AVCDecObject *decvid, AVCCommonObj *video, AVCDecBitstream *stream)
520 {
521     AVCSliceHeader *sliceHdr = video->sliceHdr;
522     AVCPicParamSet *currPPS;
523     AVCSeqParamSet *currSPS;
524     AVCDec_Status status;
525     uint idr_pic_id;
526     int slice_type, temp, i;
527 
528     ue_v(stream, &(sliceHdr->first_mb_in_slice));
529     ue_v(stream, (uint*)&slice_type);
530 
531     if (sliceHdr->first_mb_in_slice != 0)
532     {
533         if ((int)sliceHdr->slice_type >= 5 && slice_type != (int)sliceHdr->slice_type - 5)
534         {
535             return AVCDEC_FAIL; /* slice type doesn't follow the first slice in the picture */
536         }
537     }
538     sliceHdr->slice_type = (AVCSliceType) slice_type;
539     if (slice_type > 4)
540     {
541         slice_type -= 5;
542     }
543 
544     if (slice_type == 1 || slice_type > 2)
545     {
546         return AVCDEC_FAIL;
547     }
548 
549     video->slice_type = (AVCSliceType) slice_type;
550 
551     ue_v(stream, &(sliceHdr->pic_parameter_set_id));
552     /* end FirstPartSliceHeader() */
553     /* begin RestOfSliceHeader() */
554     /* after getting pic_parameter_set_id, we have to load corresponding SPS and PPS */
555     if (sliceHdr->pic_parameter_set_id > 255)
556     {
557         return AVCDEC_FAIL;
558     }
559 
560     if (decvid->picParams[sliceHdr->pic_parameter_set_id] == NULL)
561         return AVCDEC_FAIL; /* PPS doesn't exist */
562 
563     currPPS = video->currPicParams = decvid->picParams[sliceHdr->pic_parameter_set_id];
564 
565     if (decvid->seqParams[currPPS->seq_parameter_set_id] == NULL)
566         return AVCDEC_FAIL; /* SPS doesn't exist */
567 
568     currSPS = video->currSeqParams = decvid->seqParams[currPPS->seq_parameter_set_id];
569 
570     if (currPPS->seq_parameter_set_id != video->seq_parameter_set_id)
571     {
572         video->seq_parameter_set_id = currPPS->seq_parameter_set_id;
573         status = (AVCDec_Status)AVCConfigureSequence(decvid->avcHandle, video, false);
574         if (status != AVCDEC_SUCCESS)
575             return status;
576         video->level_idc = currSPS->level_idc;
577     }
578 
579     /* derived variables from SPS */
580     video->MaxFrameNum = 1 << (currSPS->log2_max_frame_num_minus4 + 4);
581     // MC_OPTIMIZE
582     video->PicWidthInMbs = currSPS->pic_width_in_mbs_minus1 + 1;
583     video->PicWidthInSamplesL = video->PicWidthInMbs * 16 ;
584     video->PicWidthInSamplesC = video->PicWidthInMbs * 8 ;
585     video->PicHeightInMapUnits = currSPS->pic_height_in_map_units_minus1 + 1 ;
586     video->PicSizeInMapUnits = video->PicWidthInMbs * video->PicHeightInMapUnits ;
587     video->FrameHeightInMbs = (2 - currSPS->frame_mbs_only_flag) * video->PicHeightInMapUnits ;
588 
589     /* derived from PPS */
590     video->SliceGroupChangeRate = currPPS->slice_group_change_rate_minus1 + 1;
591 
592     /* then we can continue decoding slice header */
593 
594     BitstreamReadBits(stream, currSPS->log2_max_frame_num_minus4 + 4, &(sliceHdr->frame_num));
595 
596     if (video->currFS == NULL && sliceHdr->frame_num != 0)
597     {
598         video->prevFrameNum = video->PrevRefFrameNum = sliceHdr->frame_num - 1;
599     }
600 
601     if (!currSPS->frame_mbs_only_flag)
602     {
603         BitstreamRead1Bit(stream, &(sliceHdr->field_pic_flag));
604         if (sliceHdr->field_pic_flag)
605         {
606             return AVCDEC_FAIL;
607         }
608     }
609 
610     /* derived variables from slice header*/
611     video->PicHeightInMbs = video->FrameHeightInMbs;
612     video->PicHeightInSamplesL = video->PicHeightInMbs * 16;
613     video->PicHeightInSamplesC = video->PicHeightInMbs * 8;
614     video->PicSizeInMbs = video->PicWidthInMbs * video->PicHeightInMbs;
615 
616     if (sliceHdr->first_mb_in_slice >= video->PicSizeInMbs)
617     {
618         return AVCDEC_FAIL;
619     }
620     video->MaxPicNum = video->MaxFrameNum;
621     video->CurrPicNum = sliceHdr->frame_num;
622 
623 
624     if (video->nal_unit_type == AVC_NALTYPE_IDR)
625     {
626         if (sliceHdr->frame_num != 0)
627         {
628             return AVCDEC_FAIL;
629         }
630         ue_v(stream, &idr_pic_id);
631     }
632 
633     sliceHdr->delta_pic_order_cnt_bottom = 0; /* default value */
634     sliceHdr->delta_pic_order_cnt[0] = 0; /* default value */
635     sliceHdr->delta_pic_order_cnt[1] = 0; /* default value */
636     if (currSPS->pic_order_cnt_type == 0)
637     {
638         BitstreamReadBits(stream, currSPS->log2_max_pic_order_cnt_lsb_minus4 + 4,
639                           &(sliceHdr->pic_order_cnt_lsb));
640         video->MaxPicOrderCntLsb =  1 << (currSPS->log2_max_pic_order_cnt_lsb_minus4 + 4);
641         if (sliceHdr->pic_order_cnt_lsb > video->MaxPicOrderCntLsb - 1)
642             return AVCDEC_FAIL; /* out of range */
643 
644         if (currPPS->pic_order_present_flag)
645         {
646             se_v32bit(stream, &(sliceHdr->delta_pic_order_cnt_bottom));
647         }
648     }
649     if (currSPS->pic_order_cnt_type == 1 && !currSPS->delta_pic_order_always_zero_flag)
650     {
651         se_v32bit(stream, &(sliceHdr->delta_pic_order_cnt[0]));
652         if (currPPS->pic_order_present_flag)
653         {
654             se_v32bit(stream, &(sliceHdr->delta_pic_order_cnt[1]));
655         }
656     }
657 
658     sliceHdr->redundant_pic_cnt = 0; /* default value */
659     if (currPPS->redundant_pic_cnt_present_flag)
660     {
661         // MC_CHECK
662         ue_v(stream, &(sliceHdr->redundant_pic_cnt));
663         if (sliceHdr->redundant_pic_cnt > 127) /* out of range */
664             return AVCDEC_FAIL;
665 
666         if (sliceHdr->redundant_pic_cnt > 0) /* redundant picture */
667             return AVCDEC_FAIL; /* not supported */
668     }
669     sliceHdr->num_ref_idx_l0_active_minus1 = currPPS->num_ref_idx_l0_active_minus1;
670     sliceHdr->num_ref_idx_l1_active_minus1 = currPPS->num_ref_idx_l1_active_minus1;
671 
672     if (slice_type == AVC_P_SLICE)
673     {
674         BitstreamRead1Bit(stream, &(sliceHdr->num_ref_idx_active_override_flag));
675         if (sliceHdr->num_ref_idx_active_override_flag)
676         {
677             ue_v(stream, &(sliceHdr->num_ref_idx_l0_active_minus1));
678         }
679         else  /* the following condition is not allowed if the flag is zero */
680         {
681             if ((slice_type == AVC_P_SLICE) && currPPS->num_ref_idx_l0_active_minus1 > 15)
682             {
683                 return AVCDEC_FAIL; /* not allowed */
684             }
685         }
686     }
687 
688 
689     if (sliceHdr->num_ref_idx_l0_active_minus1 > 15 ||
690             sliceHdr->num_ref_idx_l1_active_minus1 > 15)
691     {
692         return AVCDEC_FAIL; /* not allowed */
693     }
694     /* if MbaffFrameFlag =1,
695     max value of index is num_ref_idx_l0_active_minus1 for frame MBs and
696     2*sliceHdr->num_ref_idx_l0_active_minus1 + 1 for field MBs */
697 
698     /* ref_pic_list_reordering() */
699     status = ref_pic_list_reordering(video, stream, sliceHdr, slice_type);
700     if (status != AVCDEC_SUCCESS)
701     {
702         return status;
703     }
704 
705 
706     if (video->nal_ref_idc != 0)
707     {
708         dec_ref_pic_marking(video, stream, sliceHdr);
709     }
710     se_v(stream, &(sliceHdr->slice_qp_delta));
711 
712     video->QPy = 26 + currPPS->pic_init_qp_minus26 + sliceHdr->slice_qp_delta;
713     if (video->QPy > 51 || video->QPy < 0)
714     {
715         video->QPy = AVC_CLIP3(0, 51, video->QPy);
716 //                  return AVCDEC_FAIL;
717     }
718     video->QPc = mapQPi2QPc[AVC_CLIP3(0, 51, video->QPy + video->currPicParams->chroma_qp_index_offset)];
719 
720     video->QPy_div_6 = (video->QPy * 43) >> 8;
721     video->QPy_mod_6 = video->QPy - 6 * video->QPy_div_6;
722 
723     video->QPc_div_6 = (video->QPc * 43) >> 8;
724     video->QPc_mod_6 = video->QPc - 6 * video->QPc_div_6;
725 
726     sliceHdr->slice_alpha_c0_offset_div2 = 0;
727     sliceHdr->slice_beta_offset_div_2 = 0;
728     sliceHdr->disable_deblocking_filter_idc = 0;
729     video->FilterOffsetA = video->FilterOffsetB = 0;
730 
731     if (currPPS->deblocking_filter_control_present_flag)
732     {
733         ue_v(stream, &(sliceHdr->disable_deblocking_filter_idc));
734         if (sliceHdr->disable_deblocking_filter_idc > 2)
735         {
736             return AVCDEC_FAIL; /* out of range */
737         }
738         if (sliceHdr->disable_deblocking_filter_idc != 1)
739         {
740             se_v(stream, &(sliceHdr->slice_alpha_c0_offset_div2));
741             if (sliceHdr->slice_alpha_c0_offset_div2 < -6 ||
742                     sliceHdr->slice_alpha_c0_offset_div2 > 6)
743             {
744                 return AVCDEC_FAIL;
745             }
746             video->FilterOffsetA = sliceHdr->slice_alpha_c0_offset_div2 << 1;
747 
748             se_v(stream, &(sliceHdr->slice_beta_offset_div_2));
749             if (sliceHdr->slice_beta_offset_div_2 < -6 ||
750                     sliceHdr->slice_beta_offset_div_2 > 6)
751             {
752                 return AVCDEC_FAIL;
753             }
754             video->FilterOffsetB = sliceHdr->slice_beta_offset_div_2 << 1;
755         }
756     }
757 
758     if (currPPS->num_slice_groups_minus1 > 0 && currPPS->slice_group_map_type >= 3
759             && currPPS->slice_group_map_type <= 5)
760     {
761         /* Ceil(Log2(PicSizeInMapUnits/(float)SliceGroupChangeRate + 1)) */
762         temp = video->PicSizeInMapUnits / video->SliceGroupChangeRate;
763         if (video->PicSizeInMapUnits % video->SliceGroupChangeRate)
764         {
765             temp++;
766         }
767         i = 0;
768         temp++;
769         while (temp)
770         {
771             temp >>= 1;
772             i++;
773         }
774 
775         BitstreamReadBits(stream, i, &(sliceHdr->slice_group_change_cycle));
776         video->MapUnitsInSliceGroup0 =
777             AVC_MIN(sliceHdr->slice_group_change_cycle * video->SliceGroupChangeRate, video->PicSizeInMapUnits);
778     }
779 
780     return AVCDEC_SUCCESS;
781 }
782 
783 
fill_frame_num_gap(AVCHandle * avcHandle,AVCCommonObj * video)784 AVCDec_Status fill_frame_num_gap(AVCHandle *avcHandle, AVCCommonObj *video)
785 {
786     AVCDec_Status status;
787     int CurrFrameNum;
788     int UnusedShortTermFrameNum;
789     int tmp1 = video->sliceHdr->delta_pic_order_cnt[0];
790     int tmp2 = video->sliceHdr->delta_pic_order_cnt[1];
791     int tmp3 = video->CurrPicNum;
792     int tmp4 = video->sliceHdr->adaptive_ref_pic_marking_mode_flag;
793     UnusedShortTermFrameNum = (video->prevFrameNum + 1) % video->MaxFrameNum;
794     CurrFrameNum = video->sliceHdr->frame_num;
795 
796     video->sliceHdr->delta_pic_order_cnt[0] = 0;
797     video->sliceHdr->delta_pic_order_cnt[1] = 0;
798     while (CurrFrameNum != UnusedShortTermFrameNum)
799     {
800         video->CurrPicNum = UnusedShortTermFrameNum;
801         video->sliceHdr->frame_num = UnusedShortTermFrameNum;
802 
803         status = (AVCDec_Status)DPBInitBuffer(avcHandle, video);
804         if (status != AVCDEC_SUCCESS)  /* no buffer available */
805         {
806             return status;
807         }
808         DecodePOC(video);
809         DPBInitPic(video, UnusedShortTermFrameNum);
810 
811 
812         video->currFS->PicOrderCnt = video->PicOrderCnt;
813         video->currFS->FrameNum = video->sliceHdr->frame_num;
814 
815         /* initialize everything to zero */
816         video->currFS->IsOutputted = 0x01;
817         video->currFS->IsReference = 3;
818         video->currFS->IsLongTerm = 0;
819         video->currFS->frame.isReference = TRUE;
820         video->currFS->frame.isLongTerm = FALSE;
821 
822         video->sliceHdr->adaptive_ref_pic_marking_mode_flag = 0;
823 
824         status = (AVCDec_Status)StorePictureInDPB(avcHandle, video);  // MC_CHECK check the return status
825         if (status != AVCDEC_SUCCESS)
826         {
827             return AVCDEC_FAIL;
828         }
829         video->prevFrameNum = UnusedShortTermFrameNum;
830         UnusedShortTermFrameNum = (UnusedShortTermFrameNum + 1) % video->MaxFrameNum;
831     }
832     video->sliceHdr->frame_num = CurrFrameNum;
833     video->CurrPicNum = tmp3;
834     video->sliceHdr->delta_pic_order_cnt[0] = tmp1;
835     video->sliceHdr->delta_pic_order_cnt[1] = tmp2;
836     video->sliceHdr->adaptive_ref_pic_marking_mode_flag = tmp4;
837     return AVCDEC_SUCCESS;
838 }
839 
840 /** see subclause 7.4.3.1 */
ref_pic_list_reordering(AVCCommonObj * video,AVCDecBitstream * stream,AVCSliceHeader * sliceHdr,int slice_type)841 AVCDec_Status ref_pic_list_reordering(AVCCommonObj *video, AVCDecBitstream *stream, AVCSliceHeader *sliceHdr, int slice_type)
842 {
843     int i;
844 
845     if (slice_type != AVC_I_SLICE)
846     {
847         BitstreamRead1Bit(stream, &(sliceHdr->ref_pic_list_reordering_flag_l0));
848         if (sliceHdr->ref_pic_list_reordering_flag_l0)
849         {
850             i = 0;
851             do
852             {
853                 ue_v(stream, &(sliceHdr->reordering_of_pic_nums_idc_l0[i]));
854                 if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 0 ||
855                         sliceHdr->reordering_of_pic_nums_idc_l0[i] == 1)
856                 {
857                     ue_v(stream, &(sliceHdr->abs_diff_pic_num_minus1_l0[i]));
858                     if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 0 &&
859                             sliceHdr->abs_diff_pic_num_minus1_l0[i] > video->MaxPicNum / 2 - 1)
860                     {
861                         return AVCDEC_FAIL; /* out of range */
862                     }
863                     if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 1 &&
864                             sliceHdr->abs_diff_pic_num_minus1_l0[i] > video->MaxPicNum / 2 - 2)
865                     {
866                         return AVCDEC_FAIL; /* out of range */
867                     }
868                 }
869                 else if (sliceHdr->reordering_of_pic_nums_idc_l0[i] == 2)
870                 {
871                     ue_v(stream, &(sliceHdr->long_term_pic_num_l0[i]));
872                 }
873                 i++;
874             }
875             while (sliceHdr->reordering_of_pic_nums_idc_l0[i-1] != 3
876                     && i <= (int)sliceHdr->num_ref_idx_l0_active_minus1 + 1) ;
877         }
878     }
879     return AVCDEC_SUCCESS;
880 }
881 
882 /** see subclause 7.4.3.3 */
dec_ref_pic_marking(AVCCommonObj * video,AVCDecBitstream * stream,AVCSliceHeader * sliceHdr)883 AVCDec_Status dec_ref_pic_marking(AVCCommonObj *video, AVCDecBitstream *stream, AVCSliceHeader *sliceHdr)
884 {
885     int i;
886     if (video->nal_unit_type == AVC_NALTYPE_IDR)
887     {
888         BitstreamRead1Bit(stream, &(sliceHdr->no_output_of_prior_pics_flag));
889         BitstreamRead1Bit(stream, &(sliceHdr->long_term_reference_flag));
890         if (sliceHdr->long_term_reference_flag == 0) /* used for short-term */
891         {
892             video->MaxLongTermFrameIdx = -1; /* no long-term frame indx */
893         }
894         else /* used for long-term */
895         {
896             video->MaxLongTermFrameIdx = 0;
897             video->LongTermFrameIdx = 0;
898         }
899     }
900     else
901     {
902         BitstreamRead1Bit(stream, &(sliceHdr->adaptive_ref_pic_marking_mode_flag));
903         if (sliceHdr->adaptive_ref_pic_marking_mode_flag)
904         {
905             i = 0;
906             do
907             {
908                 ue_v(stream, &(sliceHdr->memory_management_control_operation[i]));
909                 if (sliceHdr->memory_management_control_operation[i] == 1 ||
910                         sliceHdr->memory_management_control_operation[i] == 3)
911                 {
912                     ue_v(stream, &(sliceHdr->difference_of_pic_nums_minus1[i]));
913                 }
914                 if (sliceHdr->memory_management_control_operation[i] == 2)
915                 {
916                     ue_v(stream, &(sliceHdr->long_term_pic_num[i]));
917                 }
918                 if (sliceHdr->memory_management_control_operation[i] == 3 ||
919                         sliceHdr->memory_management_control_operation[i] == 6)
920                 {
921                     ue_v(stream, &(sliceHdr->long_term_frame_idx[i]));
922                 }
923                 if (sliceHdr->memory_management_control_operation[i] == 4)
924                 {
925                     ue_v(stream, &(sliceHdr->max_long_term_frame_idx_plus1[i]));
926                 }
927                 i++;
928             }
929             while (sliceHdr->memory_management_control_operation[i-1] != 0 && i < MAX_DEC_REF_PIC_MARKING);
930             if (i >= MAX_DEC_REF_PIC_MARKING)
931             {
932                 return AVCDEC_FAIL; /* we're screwed!!, not enough memory */
933             }
934         }
935     }
936 
937     return AVCDEC_SUCCESS;
938 }
939 
940 /* see subclause 8.2.1 Decoding process for picture order count. */
DecodePOC(AVCCommonObj * video)941 AVCDec_Status DecodePOC(AVCCommonObj *video)
942 {
943     AVCSeqParamSet *currSPS = video->currSeqParams;
944     AVCSliceHeader *sliceHdr = video->sliceHdr;
945     int i;
946 
947     switch (currSPS->pic_order_cnt_type)
948     {
949         case 0: /* POC MODE 0 , subclause 8.2.1.1 */
950             if (video->nal_unit_type == AVC_NALTYPE_IDR)
951             {
952                 video->prevPicOrderCntMsb = 0;
953                 video->prevPicOrderCntLsb = 0;
954             }
955 
956             /* Calculate the MSBs of current picture */
957             if (sliceHdr->pic_order_cnt_lsb  <  video->prevPicOrderCntLsb  &&
958                     (video->prevPicOrderCntLsb - sliceHdr->pic_order_cnt_lsb)  >= (video->MaxPicOrderCntLsb / 2))
959                 video->PicOrderCntMsb = video->prevPicOrderCntMsb + video->MaxPicOrderCntLsb;
960             else if (sliceHdr->pic_order_cnt_lsb  >  video->prevPicOrderCntLsb  &&
961                      (sliceHdr->pic_order_cnt_lsb - video->prevPicOrderCntLsb)  > (video->MaxPicOrderCntLsb / 2))
962                 video->PicOrderCntMsb = video->prevPicOrderCntMsb - video->MaxPicOrderCntLsb;
963             else
964                 video->PicOrderCntMsb = video->prevPicOrderCntMsb;
965 
966             /* JVT-I010 page 81 is different from JM7.3 */
967 
968 
969             video->PicOrderCnt = video->TopFieldOrderCnt = video->PicOrderCntMsb + sliceHdr->pic_order_cnt_lsb;
970             video->BottomFieldOrderCnt = video->TopFieldOrderCnt + sliceHdr->delta_pic_order_cnt_bottom;
971 
972             break;
973 
974 
975         case 1: /* POC MODE 1, subclause 8.2.1.2 */
976             /* calculate FrameNumOffset */
977             if (video->nal_unit_type == AVC_NALTYPE_IDR)
978             {
979                 video->prevFrameNumOffset = 0;
980                 video->FrameNumOffset = 0;
981             }
982             else if (video->prevFrameNum > sliceHdr->frame_num)
983             {
984                 video->FrameNumOffset = video->prevFrameNumOffset + video->MaxFrameNum;
985             }
986             else
987             {
988                 video->FrameNumOffset = video->prevFrameNumOffset;
989             }
990             /* calculate absFrameNum */
991             if (currSPS->num_ref_frames_in_pic_order_cnt_cycle)
992             {
993                 video->absFrameNum = video->FrameNumOffset + sliceHdr->frame_num;
994             }
995             else
996             {
997                 video->absFrameNum = 0;
998             }
999 
1000             if (video->absFrameNum > 0 && video->nal_ref_idc == 0)
1001             {
1002                 video->absFrameNum--;
1003             }
1004 
1005             /* derive picOrderCntCycleCnt and frameNumInPicOrderCntCycle */
1006             if (video->absFrameNum > 0)
1007             {
1008                 video->picOrderCntCycleCnt = (video->absFrameNum - 1) / currSPS->num_ref_frames_in_pic_order_cnt_cycle;
1009                 video->frameNumInPicOrderCntCycle = (video->absFrameNum - 1) % currSPS->num_ref_frames_in_pic_order_cnt_cycle;
1010             }
1011             /* derive expectedDeltaPerPicOrderCntCycle */
1012             video->expectedDeltaPerPicOrderCntCycle = 0;
1013             for (i = 0; i < (int)currSPS->num_ref_frames_in_pic_order_cnt_cycle; i++)
1014             {
1015                 video->expectedDeltaPerPicOrderCntCycle += currSPS->offset_for_ref_frame[i];
1016             }
1017             /* derive expectedPicOrderCnt */
1018             if (video->absFrameNum)
1019             {
1020                 video->expectedPicOrderCnt = video->picOrderCntCycleCnt * video->expectedDeltaPerPicOrderCntCycle;
1021                 for (i = 0; i <= video->frameNumInPicOrderCntCycle; i++)
1022                 {
1023                     video->expectedPicOrderCnt += currSPS->offset_for_ref_frame[i];
1024                 }
1025             }
1026             else
1027             {
1028                 video->expectedPicOrderCnt = 0;
1029             }
1030 
1031             if (video->nal_ref_idc == 0)
1032             {
1033                 video->expectedPicOrderCnt += currSPS->offset_for_non_ref_pic;
1034             }
1035             /* derive TopFieldOrderCnt and BottomFieldOrderCnt */
1036 
1037             video->TopFieldOrderCnt = video->expectedPicOrderCnt + sliceHdr->delta_pic_order_cnt[0];
1038             video->BottomFieldOrderCnt = video->TopFieldOrderCnt + currSPS->offset_for_top_to_bottom_field + sliceHdr->delta_pic_order_cnt[1];
1039 
1040             video->PicOrderCnt = AVC_MIN(video->TopFieldOrderCnt, video->BottomFieldOrderCnt);
1041 
1042 
1043             break;
1044 
1045 
1046         case 2: /* POC MODE 2, subclause 8.2.1.3 */
1047             if (video->nal_unit_type == AVC_NALTYPE_IDR)
1048             {
1049                 video->FrameNumOffset = 0;
1050             }
1051             else if (video->prevFrameNum > sliceHdr->frame_num)
1052             {
1053                 video->FrameNumOffset = video->prevFrameNumOffset + video->MaxFrameNum;
1054             }
1055             else
1056             {
1057                 video->FrameNumOffset = video->prevFrameNumOffset;
1058             }
1059             /* derive tempPicOrderCnt, we just use PicOrderCnt */
1060             if (video->nal_unit_type == AVC_NALTYPE_IDR)
1061             {
1062                 video->PicOrderCnt = 0;
1063             }
1064             else if (video->nal_ref_idc == 0)
1065             {
1066                 video->PicOrderCnt = 2 * (video->FrameNumOffset + sliceHdr->frame_num) - 1;
1067             }
1068             else
1069             {
1070                 video->PicOrderCnt = 2 * (video->FrameNumOffset + sliceHdr->frame_num);
1071             }
1072             video->TopFieldOrderCnt = video->BottomFieldOrderCnt = video->PicOrderCnt;
1073             break;
1074         default:
1075             return AVCDEC_FAIL;
1076     }
1077 
1078     return AVCDEC_SUCCESS;
1079 }
1080 
1081 
DecodeSEI(AVCDecObject * decvid,AVCDecBitstream * stream)1082 AVCDec_Status DecodeSEI(AVCDecObject *decvid, AVCDecBitstream *stream)
1083 {
1084     OSCL_UNUSED_ARG(decvid);
1085     OSCL_UNUSED_ARG(stream);
1086     return AVCDEC_SUCCESS;
1087 }
1088 
sei_payload(AVCDecObject * decvid,AVCDecBitstream * stream,uint payloadType,uint payloadSize)1089 AVCDec_Status sei_payload(AVCDecObject *decvid, AVCDecBitstream *stream, uint payloadType, uint payloadSize)
1090 {
1091     AVCDec_Status status = AVCDEC_SUCCESS;
1092     uint i;
1093     switch (payloadType)
1094     {
1095         case 0:
1096             /*  buffering period SEI */
1097             status = buffering_period(decvid, stream);
1098             break;
1099         case 1:
1100             /*  picture timing SEI */
1101             status = pic_timing(decvid, stream);
1102             break;
1103         case 2:
1104 
1105         case 3:
1106 
1107         case 4:
1108 
1109         case 5:
1110 
1111         case 8:
1112 
1113         case 9:
1114 
1115         case 10:
1116 
1117         case 11:
1118 
1119         case 12:
1120 
1121         case 13:
1122 
1123         case 14:
1124 
1125         case 15:
1126 
1127         case 16:
1128 
1129         case 17:
1130             for (i = 0; i < payloadSize; i++)
1131             {
1132                 BitstreamFlushBits(stream, 8);
1133             }
1134             break;
1135         case 6:
1136             /*      recovery point SEI              */
1137             status = recovery_point(decvid, stream);
1138             break;
1139         case 7:
1140             /*      decoded reference picture marking repetition SEI */
1141             status = dec_ref_pic_marking_repetition(decvid, stream);
1142             break;
1143 
1144         case 18:
1145             /*      motion-constrained slice group set SEI */
1146             status = motion_constrained_slice_group_set(decvid, stream);
1147             break;
1148         default:
1149             /*          reserved_sei_message */
1150             for (i = 0; i < payloadSize; i++)
1151             {
1152                 BitstreamFlushBits(stream, 8);
1153             }
1154             break;
1155     }
1156     BitstreamByteAlign(stream);
1157     return status;
1158 }
1159 
buffering_period(AVCDecObject * decvid,AVCDecBitstream * stream)1160 AVCDec_Status buffering_period(AVCDecObject *decvid, AVCDecBitstream *stream)
1161 {
1162     AVCSeqParamSet *currSPS;
1163     uint seq_parameter_set_id;
1164     uint temp;
1165     uint i;
1166     ue_v(stream, &seq_parameter_set_id);
1167     if (seq_parameter_set_id > 31)
1168     {
1169         return AVCDEC_FAIL;
1170     }
1171 
1172 //  decvid->common->seq_parameter_set_id = seq_parameter_set_id;
1173 
1174     currSPS = decvid->seqParams[seq_parameter_set_id];
1175     if (currSPS->vui_parameters.nal_hrd_parameters_present_flag)
1176     {
1177         for (i = 0; i <= currSPS->vui_parameters.nal_hrd_parameters.cpb_cnt_minus1; i++)
1178         {
1179             /* initial_cpb_removal_delay[i] */
1180             BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1181             /*initial _cpb_removal_delay_offset[i] */
1182             BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1183         }
1184     }
1185 
1186     if (currSPS->vui_parameters.vcl_hrd_parameters_present_flag)
1187     {
1188         for (i = 0; i <= currSPS->vui_parameters.vcl_hrd_parameters.cpb_cnt_minus1; i++)
1189         {
1190             /* initial_cpb_removal_delay[i] */
1191             BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1192             /*initial _cpb_removal_delay_offset[i] */
1193             BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1194         }
1195     }
1196 
1197     return AVCDEC_SUCCESS;
1198 }
pic_timing(AVCDecObject * decvid,AVCDecBitstream * stream)1199 AVCDec_Status pic_timing(AVCDecObject *decvid, AVCDecBitstream *stream)
1200 {
1201     AVCSeqParamSet *currSPS;
1202     uint temp, NumClockTs = 0, time_offset_length = 24, full_timestamp_flag;
1203     uint i;
1204 
1205     currSPS = decvid->seqParams[decvid->common->seq_parameter_set_id];
1206 
1207     if (currSPS->vui_parameters.nal_hrd_parameters_present_flag)
1208     {
1209         BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1210         BitstreamReadBits(stream, currSPS->vui_parameters.nal_hrd_parameters.dpb_output_delay_length_minus1 + 1, &temp);
1211         time_offset_length = currSPS->vui_parameters.nal_hrd_parameters.time_offset_length;
1212     }
1213     else if (currSPS->vui_parameters.vcl_hrd_parameters_present_flag)
1214     {
1215         BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1, &temp);
1216         BitstreamReadBits(stream, currSPS->vui_parameters.vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1, &temp);
1217         time_offset_length = currSPS->vui_parameters.vcl_hrd_parameters.time_offset_length;
1218     }
1219 
1220     if (currSPS->vui_parameters.pic_struct_present_flag)
1221     {
1222         /* pic_struct */
1223         BitstreamReadBits(stream, 4, &temp);
1224 
1225         switch (temp)
1226         {
1227             case 0:
1228             case 1:
1229             case 2:
1230                 NumClockTs = 1;
1231                 break;
1232             case 3:
1233             case 4:
1234             case 7:
1235                 NumClockTs = 2;
1236                 break;
1237             case 5:
1238             case 6:
1239             case 8:
1240                 NumClockTs = 3;
1241                 break;
1242             default:
1243                 NumClockTs = 0;
1244                 break;
1245         }
1246 
1247         for (i = 0; i < NumClockTs; i++)
1248         {
1249             /* clock_timestamp_flag[i] */
1250             BitstreamRead1Bit(stream, &temp);
1251             if (temp)
1252             {
1253                 /* ct_type */
1254                 BitstreamReadBits(stream, 2, &temp);
1255                 /* nuit_field_based_flag */
1256                 BitstreamRead1Bit(stream, &temp);
1257                 /* counting_type        */
1258                 BitstreamReadBits(stream, 5, &temp);
1259                 /* full_timestamp_flag */
1260                 BitstreamRead1Bit(stream, &temp);
1261                 full_timestamp_flag = temp;
1262                 /* discontinuity_flag */
1263                 BitstreamRead1Bit(stream, &temp);
1264                 /* cnt_dropped_flag */
1265                 BitstreamRead1Bit(stream, &temp);
1266                 /* n_frames           */
1267                 BitstreamReadBits(stream, 8, &temp);
1268 
1269 
1270                 if (full_timestamp_flag)
1271                 {
1272                     /* seconds_value */
1273                     BitstreamReadBits(stream, 6, &temp);
1274                     /* minutes_value */
1275                     BitstreamReadBits(stream, 6, &temp);
1276                     /* hours_value */
1277                     BitstreamReadBits(stream, 5, &temp);
1278                 }
1279                 else
1280                 {
1281                     /* seconds_flag  */
1282                     BitstreamRead1Bit(stream, &temp);
1283                     if (temp)
1284                     {
1285                         /* seconds_value */
1286                         BitstreamReadBits(stream, 6, &temp);
1287                         /* minutes_flag  */
1288                         BitstreamRead1Bit(stream, &temp);
1289                         if (temp)
1290                         {
1291                             /* minutes_value */
1292                             BitstreamReadBits(stream, 6, &temp);
1293 
1294                             /* hourss_flag  */
1295                             BitstreamRead1Bit(stream, &temp);
1296 
1297                             if (temp)
1298                             {
1299                                 /* hours_value */
1300                                 BitstreamReadBits(stream, 5, &temp);
1301                             }
1302 
1303                         }
1304                     }
1305                 }
1306 
1307                 if (time_offset_length)
1308                 {
1309                     /* time_offset */
1310                     BitstreamReadBits(stream, time_offset_length, &temp);
1311                 }
1312                 else
1313                 {
1314                     /* time_offset */
1315                     temp = 0;
1316                 }
1317             }
1318         }
1319     }
1320     return AVCDEC_SUCCESS;
1321 }
recovery_point(AVCDecObject * decvid,AVCDecBitstream * stream)1322 AVCDec_Status recovery_point(AVCDecObject *decvid, AVCDecBitstream *stream)
1323 {
1324     OSCL_UNUSED_ARG(decvid);
1325     uint temp;
1326     /* recover_frame_cnt */
1327     ue_v(stream, &temp);
1328     /* exact_match_flag */
1329     BitstreamRead1Bit(stream, &temp);
1330     /* broken_link_flag */
1331     BitstreamRead1Bit(stream, &temp);
1332     /* changing slic_group_idc */
1333     BitstreamReadBits(stream, 2, &temp);
1334     return AVCDEC_SUCCESS;
1335 }
dec_ref_pic_marking_repetition(AVCDecObject * decvid,AVCDecBitstream * stream)1336 AVCDec_Status dec_ref_pic_marking_repetition(AVCDecObject *decvid, AVCDecBitstream *stream)
1337 {
1338     AVCSeqParamSet *currSPS;
1339     uint temp;
1340     currSPS = decvid->seqParams[decvid->common->seq_parameter_set_id];
1341     /* original_idr_flag */
1342     BitstreamRead1Bit(stream, &temp);
1343     /* original_frame_num */
1344     ue_v(stream, &temp);
1345     if (currSPS->frame_mbs_only_flag == 0)
1346     {
1347         /* original_field_pic_flag */
1348         BitstreamRead1Bit(stream, &temp);
1349         if (temp)
1350         {
1351             /* original_bottom_field_flag */
1352             BitstreamRead1Bit(stream, &temp);
1353         }
1354     }
1355 
1356     /*  dec_ref_pic_marking(video,stream,sliceHdr); */
1357 
1358 
1359     return AVCDEC_SUCCESS;
1360 }
motion_constrained_slice_group_set(AVCDecObject * decvid,AVCDecBitstream * stream)1361 AVCDec_Status motion_constrained_slice_group_set(AVCDecObject *decvid, AVCDecBitstream *stream)
1362 {
1363     OSCL_UNUSED_ARG(decvid);
1364     uint temp, i, numBits;
1365     /* num_slice_groups_in_set_minus1 */
1366     ue_v(stream, &temp);
1367 
1368     numBits = 0;/* ceil(log2(num_slice_groups_minus1+1)) bits */
1369     i = temp;
1370     while (i > 0)
1371     {
1372         numBits++;
1373         i >>= 1;
1374     }
1375     for (i = 0; i <= temp; i++)
1376     {
1377         /* slice_group_id */
1378         BitstreamReadBits(stream, numBits, &temp);
1379     }
1380     /* exact_sample_value_match_flag */
1381     BitstreamRead1Bit(stream, &temp);
1382     /* pan_scan_rect_flag */
1383     BitstreamRead1Bit(stream, &temp);
1384     if (temp)
1385     {
1386         /* pan_scan_rect_id */
1387         ue_v(stream, &temp);
1388     }
1389 
1390     return AVCDEC_SUCCESS;
1391 }
1392 
1393