PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/Show/avc/header.cpp

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