PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Avc/header.cpp

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