You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1372 lines
45KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
  19. {
  20. int err;
  21. fixed(1, rbsp_stop_one_bit, 1);
  22. while (byte_alignment(rw) != 0)
  23. fixed(1, rbsp_alignment_zero_bit, 0);
  24. return 0;
  25. }
  26. static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
  27. H264RawNALUnitHeader *current,
  28. uint32_t valid_type_mask)
  29. {
  30. int err;
  31. u(1, forbidden_zero_bit, 0, 0);
  32. u(2, nal_ref_idc, 0, 3);
  33. u(5, nal_unit_type, 0, 31);
  34. if (!(1 << current->nal_unit_type & valid_type_mask)) {
  35. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid NAL unit type %d.\n",
  36. current->nal_unit_type);
  37. return AVERROR_INVALIDDATA;
  38. }
  39. if (current->nal_unit_type == 14 ||
  40. current->nal_unit_type == 20 ||
  41. current->nal_unit_type == 21) {
  42. if (current->nal_unit_type != 21)
  43. flag(svc_extension_flag);
  44. else
  45. flag(avc_3d_extension_flag);
  46. if (current->svc_extension_flag) {
  47. av_log(ctx->log_ctx, AV_LOG_ERROR, "SVC not supported.\n");
  48. return AVERROR_PATCHWELCOME;
  49. } else if (current->avc_3d_extension_flag) {
  50. av_log(ctx->log_ctx, AV_LOG_ERROR, "3DAVC not supported.\n");
  51. return AVERROR_PATCHWELCOME;
  52. } else {
  53. av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC not supported.\n");
  54. return AVERROR_PATCHWELCOME;
  55. }
  56. }
  57. return 0;
  58. }
  59. static int FUNC(scaling_list)(CodedBitstreamContext *ctx, RWContext *rw,
  60. H264RawScalingList *current,
  61. int size_of_scaling_list)
  62. {
  63. int err, i, scale;
  64. scale = 8;
  65. for (i = 0; i < size_of_scaling_list; i++) {
  66. ses(delta_scale[i], -128, +127, 1, i);
  67. scale = (scale + current->delta_scale[i] + 256) % 256;
  68. if (scale == 0)
  69. break;
  70. }
  71. return 0;
  72. }
  73. static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
  74. H264RawHRD *current)
  75. {
  76. int err, i;
  77. ue(cpb_cnt_minus1, 0, 31);
  78. u(4, bit_rate_scale, 0, 15);
  79. u(4, cpb_size_scale, 0, 15);
  80. for (i = 0; i <= current->cpb_cnt_minus1; i++) {
  81. ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
  82. ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
  83. flags(cbr_flag[i], 1, i);
  84. }
  85. u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
  86. u(5, cpb_removal_delay_length_minus1, 0, 31);
  87. u(5, dpb_output_delay_length_minus1, 0, 31);
  88. u(5, time_offset_length, 0, 31);
  89. return 0;
  90. }
  91. static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
  92. H264RawVUI *current, H264RawSPS *sps)
  93. {
  94. int err;
  95. flag(aspect_ratio_info_present_flag);
  96. if (current->aspect_ratio_info_present_flag) {
  97. u(8, aspect_ratio_idc, 0, 255);
  98. if (current->aspect_ratio_idc == 255) {
  99. u(16, sar_width, 0, 65535);
  100. u(16, sar_height, 0, 65535);
  101. }
  102. } else {
  103. infer(aspect_ratio_idc, 0);
  104. }
  105. flag(overscan_info_present_flag);
  106. if (current->overscan_info_present_flag)
  107. flag(overscan_appropriate_flag);
  108. flag(video_signal_type_present_flag);
  109. if (current->video_signal_type_present_flag) {
  110. u(3, video_format, 0, 7);
  111. flag(video_full_range_flag);
  112. flag(colour_description_present_flag);
  113. if (current->colour_description_present_flag) {
  114. u(8, colour_primaries, 0, 255);
  115. u(8, transfer_characteristics, 0, 255);
  116. u(8, matrix_coefficients, 0, 255);
  117. }
  118. } else {
  119. infer(video_format, 5);
  120. infer(video_full_range_flag, 0);
  121. infer(colour_primaries, 2);
  122. infer(transfer_characteristics, 2);
  123. infer(matrix_coefficients, 2);
  124. }
  125. flag(chroma_loc_info_present_flag);
  126. if (current->chroma_loc_info_present_flag) {
  127. ue(chroma_sample_loc_type_top_field, 0, 5);
  128. ue(chroma_sample_loc_type_bottom_field, 0, 5);
  129. } else {
  130. infer(chroma_sample_loc_type_top_field, 0);
  131. infer(chroma_sample_loc_type_bottom_field, 0);
  132. }
  133. flag(timing_info_present_flag);
  134. if (current->timing_info_present_flag) {
  135. u(32, num_units_in_tick, 1, UINT32_MAX);
  136. u(32, time_scale, 1, UINT32_MAX);
  137. flag(fixed_frame_rate_flag);
  138. } else {
  139. infer(fixed_frame_rate_flag, 0);
  140. }
  141. flag(nal_hrd_parameters_present_flag);
  142. if (current->nal_hrd_parameters_present_flag)
  143. CHECK(FUNC(hrd_parameters)(ctx, rw, &current->nal_hrd_parameters));
  144. flag(vcl_hrd_parameters_present_flag);
  145. if (current->vcl_hrd_parameters_present_flag)
  146. CHECK(FUNC(hrd_parameters)(ctx, rw, &current->vcl_hrd_parameters));
  147. if (current->nal_hrd_parameters_present_flag ||
  148. current->vcl_hrd_parameters_present_flag)
  149. flag(low_delay_hrd_flag);
  150. else
  151. infer(low_delay_hrd_flag, 1 - current->fixed_frame_rate_flag);
  152. flag(pic_struct_present_flag);
  153. flag(bitstream_restriction_flag);
  154. if (current->bitstream_restriction_flag) {
  155. flag(motion_vectors_over_pic_boundaries_flag);
  156. ue(max_bytes_per_pic_denom, 0, 16);
  157. ue(max_bits_per_mb_denom, 0, 16);
  158. // The current version of the standard constrains this to be in
  159. // [0,15], but older versions allow 16.
  160. ue(log2_max_mv_length_horizontal, 0, 16);
  161. ue(log2_max_mv_length_vertical, 0, 16);
  162. ue(max_num_reorder_frames, 0, H264_MAX_DPB_FRAMES);
  163. ue(max_dec_frame_buffering, 0, H264_MAX_DPB_FRAMES);
  164. } else {
  165. infer(motion_vectors_over_pic_boundaries_flag, 1);
  166. infer(max_bytes_per_pic_denom, 2);
  167. infer(max_bits_per_mb_denom, 1);
  168. infer(log2_max_mv_length_horizontal, 15);
  169. infer(log2_max_mv_length_vertical, 15);
  170. if ((sps->profile_idc == 44 || sps->profile_idc == 86 ||
  171. sps->profile_idc == 100 || sps->profile_idc == 110 ||
  172. sps->profile_idc == 122 || sps->profile_idc == 244) &&
  173. sps->constraint_set3_flag) {
  174. infer(max_num_reorder_frames, 0);
  175. infer(max_dec_frame_buffering, 0);
  176. } else {
  177. infer(max_num_reorder_frames, H264_MAX_DPB_FRAMES);
  178. infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
  179. }
  180. }
  181. return 0;
  182. }
  183. static int FUNC(vui_parameters_default)(CodedBitstreamContext *ctx,
  184. RWContext *rw, H264RawVUI *current,
  185. H264RawSPS *sps)
  186. {
  187. infer(aspect_ratio_idc, 0);
  188. infer(video_format, 5);
  189. infer(video_full_range_flag, 0);
  190. infer(colour_primaries, 2);
  191. infer(transfer_characteristics, 2);
  192. infer(matrix_coefficients, 2);
  193. infer(chroma_sample_loc_type_top_field, 0);
  194. infer(chroma_sample_loc_type_bottom_field, 0);
  195. infer(fixed_frame_rate_flag, 0);
  196. infer(low_delay_hrd_flag, 1);
  197. infer(pic_struct_present_flag, 0);
  198. infer(motion_vectors_over_pic_boundaries_flag, 1);
  199. infer(max_bytes_per_pic_denom, 2);
  200. infer(max_bits_per_mb_denom, 1);
  201. infer(log2_max_mv_length_horizontal, 15);
  202. infer(log2_max_mv_length_vertical, 15);
  203. if ((sps->profile_idc == 44 || sps->profile_idc == 86 ||
  204. sps->profile_idc == 100 || sps->profile_idc == 110 ||
  205. sps->profile_idc == 122 || sps->profile_idc == 244) &&
  206. sps->constraint_set3_flag) {
  207. infer(max_num_reorder_frames, 0);
  208. infer(max_dec_frame_buffering, 0);
  209. } else {
  210. infer(max_num_reorder_frames, H264_MAX_DPB_FRAMES);
  211. infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
  212. }
  213. return 0;
  214. }
  215. static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
  216. H264RawSPS *current)
  217. {
  218. int err, i;
  219. HEADER("Sequence Parameter Set");
  220. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  221. 1 << H264_NAL_SPS));
  222. u(8, profile_idc, 0, 255);
  223. flag(constraint_set0_flag);
  224. flag(constraint_set1_flag);
  225. flag(constraint_set2_flag);
  226. flag(constraint_set3_flag);
  227. flag(constraint_set4_flag);
  228. flag(constraint_set5_flag);
  229. u(2, reserved_zero_2bits, 0, 0);
  230. u(8, level_idc, 0, 255);
  231. ue(seq_parameter_set_id, 0, 31);
  232. if (current->profile_idc == 100 || current->profile_idc == 110 ||
  233. current->profile_idc == 122 || current->profile_idc == 244 ||
  234. current->profile_idc == 44 || current->profile_idc == 83 ||
  235. current->profile_idc == 86 || current->profile_idc == 118 ||
  236. current->profile_idc == 128 || current->profile_idc == 138) {
  237. ue(chroma_format_idc, 0, 3);
  238. if (current->chroma_format_idc == 3)
  239. flag(separate_colour_plane_flag);
  240. else
  241. infer(separate_colour_plane_flag, 0);
  242. ue(bit_depth_luma_minus8, 0, 6);
  243. ue(bit_depth_chroma_minus8, 0, 6);
  244. flag(qpprime_y_zero_transform_bypass_flag);
  245. flag(seq_scaling_matrix_present_flag);
  246. if (current->seq_scaling_matrix_present_flag) {
  247. for (i = 0; i < ((current->chroma_format_idc != 3) ? 8 : 12); i++) {
  248. flags(seq_scaling_list_present_flag[i], 1, i);
  249. if (current->seq_scaling_list_present_flag[i]) {
  250. if (i < 6)
  251. CHECK(FUNC(scaling_list)(ctx, rw,
  252. &current->scaling_list_4x4[i],
  253. 16));
  254. else
  255. CHECK(FUNC(scaling_list)(ctx, rw,
  256. &current->scaling_list_8x8[i - 6],
  257. 64));
  258. }
  259. }
  260. }
  261. } else {
  262. infer(chroma_format_idc, current->profile_idc == 183 ? 0 : 1);
  263. infer(separate_colour_plane_flag, 0);
  264. infer(bit_depth_luma_minus8, 0);
  265. infer(bit_depth_chroma_minus8, 0);
  266. }
  267. ue(log2_max_frame_num_minus4, 0, 12);
  268. ue(pic_order_cnt_type, 0, 2);
  269. if (current->pic_order_cnt_type == 0) {
  270. ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
  271. } else if (current->pic_order_cnt_type == 1) {
  272. flag(delta_pic_order_always_zero_flag);
  273. se(offset_for_non_ref_pic, INT32_MIN + 1, INT32_MAX);
  274. se(offset_for_top_to_bottom_field, INT32_MIN + 1, INT32_MAX);
  275. ue(num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
  276. for (i = 0; i < current->num_ref_frames_in_pic_order_cnt_cycle; i++)
  277. ses(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX, 1, i);
  278. }
  279. ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
  280. flag(gaps_in_frame_num_allowed_flag);
  281. ue(pic_width_in_mbs_minus1, 0, H264_MAX_MB_WIDTH);
  282. ue(pic_height_in_map_units_minus1, 0, H264_MAX_MB_HEIGHT);
  283. flag(frame_mbs_only_flag);
  284. if (!current->frame_mbs_only_flag)
  285. flag(mb_adaptive_frame_field_flag);
  286. flag(direct_8x8_inference_flag);
  287. flag(frame_cropping_flag);
  288. if (current->frame_cropping_flag) {
  289. ue(frame_crop_left_offset, 0, H264_MAX_WIDTH);
  290. ue(frame_crop_right_offset, 0, H264_MAX_WIDTH);
  291. ue(frame_crop_top_offset, 0, H264_MAX_HEIGHT);
  292. ue(frame_crop_bottom_offset, 0, H264_MAX_HEIGHT);
  293. }
  294. flag(vui_parameters_present_flag);
  295. if (current->vui_parameters_present_flag)
  296. CHECK(FUNC(vui_parameters)(ctx, rw, &current->vui, current));
  297. else
  298. CHECK(FUNC(vui_parameters_default)(ctx, rw, &current->vui, current));
  299. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  300. return 0;
  301. }
  302. static int FUNC(sps_extension)(CodedBitstreamContext *ctx, RWContext *rw,
  303. H264RawSPSExtension *current)
  304. {
  305. int err;
  306. HEADER("Sequence Parameter Set Extension");
  307. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  308. 1 << H264_NAL_SPS_EXT));
  309. ue(seq_parameter_set_id, 0, 31);
  310. ue(aux_format_idc, 0, 3);
  311. if (current->aux_format_idc != 0) {
  312. int bits;
  313. ue(bit_depth_aux_minus8, 0, 4);
  314. flag(alpha_incr_flag);
  315. bits = current->bit_depth_aux_minus8 + 9;
  316. u(bits, alpha_opaque_value, 0, MAX_UINT_BITS(bits));
  317. u(bits, alpha_transparent_value, 0, MAX_UINT_BITS(bits));
  318. }
  319. flag(additional_extension_flag);
  320. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  321. return 0;
  322. }
  323. static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
  324. H264RawPPS *current)
  325. {
  326. CodedBitstreamH264Context *h264 = ctx->priv_data;
  327. const H264RawSPS *sps;
  328. int err, i;
  329. HEADER("Picture Parameter Set");
  330. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  331. 1 << H264_NAL_PPS));
  332. ue(pic_parameter_set_id, 0, 255);
  333. ue(seq_parameter_set_id, 0, 31);
  334. sps = h264->sps[current->seq_parameter_set_id];
  335. if (!sps) {
  336. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  337. current->seq_parameter_set_id);
  338. return AVERROR_INVALIDDATA;
  339. }
  340. flag(entropy_coding_mode_flag);
  341. flag(bottom_field_pic_order_in_frame_present_flag);
  342. ue(num_slice_groups_minus1, 0, 7);
  343. if (current->num_slice_groups_minus1 > 0) {
  344. unsigned int pic_size;
  345. int iGroup;
  346. pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
  347. (sps->pic_height_in_map_units_minus1 + 1);
  348. ue(slice_group_map_type, 0, 6);
  349. if (current->slice_group_map_type == 0) {
  350. for (iGroup = 0; iGroup <= current->num_slice_groups_minus1; iGroup++)
  351. ues(run_length_minus1[iGroup], 0, pic_size - 1, 1, iGroup);
  352. } else if (current->slice_group_map_type == 2) {
  353. for (iGroup = 0; iGroup < current->num_slice_groups_minus1; iGroup++) {
  354. ues(top_left[iGroup], 0, pic_size - 1, 1, iGroup);
  355. ues(bottom_right[iGroup],
  356. current->top_left[iGroup], pic_size - 1, 1, iGroup);
  357. }
  358. } else if (current->slice_group_map_type == 3 ||
  359. current->slice_group_map_type == 4 ||
  360. current->slice_group_map_type == 5) {
  361. flag(slice_group_change_direction_flag);
  362. ue(slice_group_change_rate_minus1, 0, pic_size - 1);
  363. } else if (current->slice_group_map_type == 6) {
  364. ue(pic_size_in_map_units_minus1, pic_size - 1, pic_size - 1);
  365. allocate(current->slice_group_id,
  366. current->pic_size_in_map_units_minus1 + 1);
  367. for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
  368. us(av_log2(2 * current->num_slice_groups_minus1 + 1),
  369. slice_group_id[i], 0, current->num_slice_groups_minus1, 1, i);
  370. }
  371. }
  372. ue(num_ref_idx_l0_default_active_minus1, 0, 31);
  373. ue(num_ref_idx_l1_default_active_minus1, 0, 31);
  374. flag(weighted_pred_flag);
  375. u(2, weighted_bipred_idc, 0, 2);
  376. se(pic_init_qp_minus26, -26 - 6 * sps->bit_depth_luma_minus8, +25);
  377. se(pic_init_qs_minus26, -26, +25);
  378. se(chroma_qp_index_offset, -12, +12);
  379. flag(deblocking_filter_control_present_flag);
  380. flag(constrained_intra_pred_flag);
  381. flag(redundant_pic_cnt_present_flag);
  382. if (more_rbsp_data(current->more_rbsp_data))
  383. {
  384. flag(transform_8x8_mode_flag);
  385. flag(pic_scaling_matrix_present_flag);
  386. if (current->pic_scaling_matrix_present_flag) {
  387. for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
  388. current->transform_8x8_mode_flag); i++) {
  389. flags(pic_scaling_list_present_flag[i], 1, i);
  390. if (current->pic_scaling_list_present_flag[i]) {
  391. if (i < 6)
  392. CHECK(FUNC(scaling_list)(ctx, rw,
  393. &current->scaling_list_4x4[i],
  394. 16));
  395. else
  396. CHECK(FUNC(scaling_list)(ctx, rw,
  397. &current->scaling_list_8x8[i - 6],
  398. 64));
  399. }
  400. }
  401. }
  402. se(second_chroma_qp_index_offset, -12, +12);
  403. } else {
  404. infer(transform_8x8_mode_flag, 0);
  405. infer(pic_scaling_matrix_present_flag, 0);
  406. infer(second_chroma_qp_index_offset, current->chroma_qp_index_offset);
  407. }
  408. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  409. return 0;
  410. }
  411. static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
  412. H264RawSEIBufferingPeriod *current)
  413. {
  414. CodedBitstreamH264Context *h264 = ctx->priv_data;
  415. const H264RawSPS *sps;
  416. int err, i, length;
  417. ue(seq_parameter_set_id, 0, 31);
  418. sps = h264->sps[current->seq_parameter_set_id];
  419. if (!sps) {
  420. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  421. current->seq_parameter_set_id);
  422. return AVERROR_INVALIDDATA;
  423. }
  424. h264->active_sps = sps;
  425. if (sps->vui.nal_hrd_parameters_present_flag) {
  426. for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
  427. length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
  428. xu(length, initial_cpb_removal_delay[SchedSelIdx],
  429. current->nal.initial_cpb_removal_delay[i],
  430. 1, MAX_UINT_BITS(length), 1, i);
  431. xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
  432. current->nal.initial_cpb_removal_delay_offset[i],
  433. 0, MAX_UINT_BITS(length), 1, i);
  434. }
  435. }
  436. if (sps->vui.vcl_hrd_parameters_present_flag) {
  437. for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
  438. length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
  439. xu(length, initial_cpb_removal_delay[SchedSelIdx],
  440. current->vcl.initial_cpb_removal_delay[i],
  441. 1, MAX_UINT_BITS(length), 1, i);
  442. xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
  443. current->vcl.initial_cpb_removal_delay_offset[i],
  444. 0, MAX_UINT_BITS(length), 1, i);
  445. }
  446. }
  447. return 0;
  448. }
  449. static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
  450. H264RawSEIPicTimestamp *current)
  451. {
  452. CodedBitstreamH264Context *h264 = ctx->priv_data;
  453. const H264RawSPS *sps;
  454. uint8_t time_offset_length;
  455. int err;
  456. u(2, ct_type, 0, 2);
  457. flag(nuit_field_based_flag);
  458. u(5, counting_type, 0, 6);
  459. flag(full_timestamp_flag);
  460. flag(discontinuity_flag);
  461. flag(cnt_dropped_flag);
  462. u(8, n_frames, 0, 255);
  463. if (current->full_timestamp_flag) {
  464. u(6, seconds_value, 0, 59);
  465. u(6, minutes_value, 0, 59);
  466. u(5, hours_value, 0, 23);
  467. } else {
  468. flag(seconds_flag);
  469. if (current->seconds_flag) {
  470. u(6, seconds_value, 0, 59);
  471. flag(minutes_flag);
  472. if (current->minutes_flag) {
  473. u(6, minutes_value, 0, 59);
  474. flag(hours_flag);
  475. if (current->hours_flag)
  476. u(5, hours_value, 0, 23);
  477. }
  478. }
  479. }
  480. sps = h264->active_sps;
  481. if (sps->vui.nal_hrd_parameters_present_flag)
  482. time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
  483. else if (sps->vui.vcl_hrd_parameters_present_flag)
  484. time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
  485. else
  486. time_offset_length = 24;
  487. if (time_offset_length > 0)
  488. u(time_offset_length, time_offset,
  489. 0, MAX_UINT_BITS(time_offset_length));
  490. else
  491. infer(time_offset, 0);
  492. return 0;
  493. }
  494. static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
  495. H264RawSEIPicTiming *current)
  496. {
  497. CodedBitstreamH264Context *h264 = ctx->priv_data;
  498. const H264RawSPS *sps;
  499. int err;
  500. sps = h264->active_sps;
  501. if (!sps) {
  502. // If there is exactly one possible SPS but it is not yet active
  503. // then just assume that it should be the active one.
  504. int i, k = -1;
  505. for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
  506. if (h264->sps[i]) {
  507. if (k >= 0) {
  508. k = -1;
  509. break;
  510. }
  511. k = i;
  512. }
  513. }
  514. if (k >= 0)
  515. sps = h264->sps[k];
  516. }
  517. if (!sps) {
  518. av_log(ctx->log_ctx, AV_LOG_ERROR,
  519. "No active SPS for pic_timing.\n");
  520. return AVERROR_INVALIDDATA;
  521. }
  522. if (sps->vui.nal_hrd_parameters_present_flag ||
  523. sps->vui.vcl_hrd_parameters_present_flag) {
  524. const H264RawHRD *hrd;
  525. if (sps->vui.nal_hrd_parameters_present_flag)
  526. hrd = &sps->vui.nal_hrd_parameters;
  527. else if (sps->vui.vcl_hrd_parameters_present_flag)
  528. hrd = &sps->vui.vcl_hrd_parameters;
  529. else {
  530. av_log(ctx->log_ctx, AV_LOG_ERROR,
  531. "No HRD parameters for pic_timing.\n");
  532. return AVERROR_INVALIDDATA;
  533. }
  534. u(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay,
  535. 0, MAX_UINT_BITS(hrd->cpb_removal_delay_length_minus1 + 1));
  536. u(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay,
  537. 0, MAX_UINT_BITS(hrd->dpb_output_delay_length_minus1 + 1));
  538. }
  539. if (sps->vui.pic_struct_present_flag) {
  540. static const int num_clock_ts[9] = {
  541. 1, 1, 1, 2, 2, 3, 3, 2, 3
  542. };
  543. int i;
  544. u(4, pic_struct, 0, 8);
  545. if (current->pic_struct > 8)
  546. return AVERROR_INVALIDDATA;
  547. for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
  548. flags(clock_timestamp_flag[i], 1, i);
  549. if (current->clock_timestamp_flag[i])
  550. CHECK(FUNC(sei_pic_timestamp)(ctx, rw, &current->timestamp[i]));
  551. }
  552. }
  553. return 0;
  554. }
  555. static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
  556. H264RawSEIPanScanRect *current)
  557. {
  558. int err, i;
  559. ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
  560. flag(pan_scan_rect_cancel_flag);
  561. if (!current->pan_scan_rect_cancel_flag) {
  562. ue(pan_scan_cnt_minus1, 0, 2);
  563. for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
  564. ses(pan_scan_rect_left_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  565. ses(pan_scan_rect_right_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  566. ses(pan_scan_rect_top_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  567. ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  568. }
  569. ue(pan_scan_rect_repetition_period, 0, 16384);
  570. }
  571. return 0;
  572. }
  573. static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
  574. H264RawSEIUserDataRegistered *current,
  575. uint32_t *payload_size)
  576. {
  577. int err, i, j;
  578. u(8, itu_t_t35_country_code, 0x00, 0xff);
  579. if (current->itu_t_t35_country_code != 0xff)
  580. i = 1;
  581. else {
  582. u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
  583. i = 2;
  584. }
  585. #ifdef READ
  586. if (*payload_size < i) {
  587. av_log(ctx->log_ctx, AV_LOG_ERROR,
  588. "Invalid SEI user data registered payload.\n");
  589. return AVERROR_INVALIDDATA;
  590. }
  591. current->data_length = *payload_size - i;
  592. #else
  593. *payload_size = i + current->data_length;
  594. #endif
  595. allocate(current->data, current->data_length);
  596. for (j = 0; j < current->data_length; j++)
  597. xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
  598. return 0;
  599. }
  600. static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
  601. H264RawSEIUserDataUnregistered *current,
  602. uint32_t *payload_size)
  603. {
  604. int err, i;
  605. #ifdef READ
  606. if (*payload_size < 16) {
  607. av_log(ctx->log_ctx, AV_LOG_ERROR,
  608. "Invalid SEI user data unregistered payload.\n");
  609. return AVERROR_INVALIDDATA;
  610. }
  611. current->data_length = *payload_size - 16;
  612. #else
  613. *payload_size = 16 + current->data_length;
  614. #endif
  615. for (i = 0; i < 16; i++)
  616. us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
  617. allocate(current->data, current->data_length);
  618. for (i = 0; i < current->data_length; i++)
  619. xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
  620. return 0;
  621. }
  622. static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
  623. H264RawSEIRecoveryPoint *current)
  624. {
  625. int err;
  626. ue(recovery_frame_cnt, 0, 65535);
  627. flag(exact_match_flag);
  628. flag(broken_link_flag);
  629. u(2, changing_slice_group_idc, 0, 2);
  630. return 0;
  631. }
  632. static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
  633. H264RawSEIDisplayOrientation *current)
  634. {
  635. int err;
  636. flag(display_orientation_cancel_flag);
  637. if (!current->display_orientation_cancel_flag) {
  638. flag(hor_flip);
  639. flag(ver_flip);
  640. u(16, anticlockwise_rotation, 0, 65535);
  641. ue(display_orientation_repetition_period, 0, 16384);
  642. flag(display_orientation_extension_flag);
  643. }
  644. return 0;
  645. }
  646. static int FUNC(sei_mastering_display_colour_volume)(CodedBitstreamContext *ctx, RWContext *rw,
  647. H264RawSEIMasteringDisplayColourVolume *current)
  648. {
  649. int err, c;
  650. for (c = 0; c < 3; c++) {
  651. us(16, display_primaries_x[c], 0, 50000, 1, c);
  652. us(16, display_primaries_y[c], 0, 50000, 1, c);
  653. }
  654. u(16, white_point_x, 0, 50000);
  655. u(16, white_point_y, 0, 50000);
  656. u(32, max_display_mastering_luminance, 1, MAX_UINT_BITS(32));
  657. u(32, min_display_mastering_luminance, 0, current->max_display_mastering_luminance - 1);
  658. return 0;
  659. }
  660. static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
  661. H264RawSEIPayload *current)
  662. {
  663. int err, i;
  664. int start_position, end_position;
  665. #ifdef READ
  666. start_position = get_bits_count(rw);
  667. #else
  668. start_position = put_bits_count(rw);
  669. #endif
  670. switch (current->payload_type) {
  671. case H264_SEI_TYPE_BUFFERING_PERIOD:
  672. CHECK(FUNC(sei_buffering_period)
  673. (ctx, rw, &current->payload.buffering_period));
  674. break;
  675. case H264_SEI_TYPE_PIC_TIMING:
  676. CHECK(FUNC(sei_pic_timing)
  677. (ctx, rw, &current->payload.pic_timing));
  678. break;
  679. case H264_SEI_TYPE_PAN_SCAN_RECT:
  680. CHECK(FUNC(sei_pan_scan_rect)
  681. (ctx, rw, &current->payload.pan_scan_rect));
  682. break;
  683. case H264_SEI_TYPE_FILLER_PAYLOAD:
  684. {
  685. for (i = 0; i < current->payload_size; i++)
  686. fixed(8, ff_byte, 0xff);
  687. }
  688. break;
  689. case H264_SEI_TYPE_USER_DATA_REGISTERED:
  690. CHECK(FUNC(sei_user_data_registered)
  691. (ctx, rw, &current->payload.user_data_registered, &current->payload_size));
  692. break;
  693. case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
  694. CHECK(FUNC(sei_user_data_unregistered)
  695. (ctx, rw, &current->payload.user_data_unregistered, &current->payload_size));
  696. break;
  697. case H264_SEI_TYPE_RECOVERY_POINT:
  698. CHECK(FUNC(sei_recovery_point)
  699. (ctx, rw, &current->payload.recovery_point));
  700. break;
  701. case H264_SEI_TYPE_DISPLAY_ORIENTATION:
  702. CHECK(FUNC(sei_display_orientation)
  703. (ctx, rw, &current->payload.display_orientation));
  704. break;
  705. case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
  706. CHECK(FUNC(sei_mastering_display_colour_volume)
  707. (ctx, rw, &current->payload.mastering_display_colour_volume));
  708. break;
  709. default:
  710. {
  711. #ifdef READ
  712. current->payload.other.data_length = current->payload_size;
  713. #endif
  714. allocate(current->payload.other.data, current->payload.other.data_length);
  715. for (i = 0; i < current->payload.other.data_length; i++)
  716. xu(8, payload_byte[i], current->payload.other.data[i], 0, 255, 1, i);
  717. }
  718. }
  719. if (byte_alignment(rw)) {
  720. fixed(1, bit_equal_to_one, 1);
  721. while (byte_alignment(rw))
  722. fixed(1, bit_equal_to_zero, 0);
  723. }
  724. #ifdef READ
  725. end_position = get_bits_count(rw);
  726. if (end_position < start_position + 8 * current->payload_size) {
  727. av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
  728. "header %"PRIu32" bits, actually %d bits.\n",
  729. 8 * current->payload_size,
  730. end_position - start_position);
  731. return AVERROR_INVALIDDATA;
  732. }
  733. #else
  734. end_position = put_bits_count(rw);
  735. current->payload_size = (end_position - start_position) / 8;
  736. #endif
  737. return 0;
  738. }
  739. static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
  740. H264RawSEI *current)
  741. {
  742. int err, k;
  743. HEADER("Supplemental Enhancement Information");
  744. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  745. 1 << H264_NAL_SEI));
  746. #ifdef READ
  747. for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
  748. uint32_t payload_type = 0;
  749. uint32_t payload_size = 0;
  750. uint32_t tmp;
  751. while (show_bits(rw, 8) == 0xff) {
  752. fixed(8, ff_byte, 0xff);
  753. payload_type += 255;
  754. }
  755. xu(8, last_payload_type_byte, tmp, 0, 254, 0);
  756. payload_type += tmp;
  757. while (show_bits(rw, 8) == 0xff) {
  758. fixed(8, ff_byte, 0xff);
  759. payload_size += 255;
  760. }
  761. xu(8, last_payload_size_byte, tmp, 0, 254, 0);
  762. payload_size += tmp;
  763. current->payload[k].payload_type = payload_type;
  764. current->payload[k].payload_size = payload_size;
  765. CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
  766. if (!cbs_h2645_read_more_rbsp_data(rw))
  767. break;
  768. }
  769. if (k >= H264_MAX_SEI_PAYLOADS) {
  770. av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
  771. "SEI message: found %d.\n", k);
  772. return AVERROR_INVALIDDATA;
  773. }
  774. current->payload_count = k + 1;
  775. #else
  776. for (k = 0; k < current->payload_count; k++) {
  777. PutBitContext start_state;
  778. uint32_t tmp;
  779. int need_size, i;
  780. // Somewhat clumsy: we write the payload twice when
  781. // we don't know the size in advance. This will mess
  782. // with trace output, but is otherwise harmless.
  783. start_state = *rw;
  784. need_size = !current->payload[k].payload_size;
  785. for (i = 0; i < 1 + need_size; i++) {
  786. *rw = start_state;
  787. tmp = current->payload[k].payload_type;
  788. while (tmp >= 255) {
  789. fixed(8, ff_byte, 0xff);
  790. tmp -= 255;
  791. }
  792. xu(8, last_payload_type_byte, tmp, 0, 254, 0);
  793. tmp = current->payload[k].payload_size;
  794. while (tmp >= 255) {
  795. fixed(8, ff_byte, 0xff);
  796. tmp -= 255;
  797. }
  798. xu(8, last_payload_size_byte, tmp, 0, 254, 0);
  799. CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
  800. }
  801. }
  802. #endif
  803. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  804. return 0;
  805. }
  806. static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
  807. H264RawAUD *current)
  808. {
  809. int err;
  810. HEADER("Access Unit Delimiter");
  811. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  812. 1 << H264_NAL_AUD));
  813. u(3, primary_pic_type, 0, 7);
  814. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  815. return 0;
  816. }
  817. static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
  818. H264RawSliceHeader *current)
  819. {
  820. CodedBitstreamH264Context *h264 = ctx->priv_data;
  821. const H264RawSPS *sps = h264->active_sps;
  822. int err, i, mopn;
  823. if (current->slice_type % 5 != 2 &&
  824. current->slice_type % 5 != 4) {
  825. flag(ref_pic_list_modification_flag_l0);
  826. if (current->ref_pic_list_modification_flag_l0) {
  827. for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
  828. xue(modification_of_pic_nums_idc,
  829. current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
  830. mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
  831. if (mopn == 3)
  832. break;
  833. if (mopn == 0 || mopn == 1)
  834. xue(abs_diff_pic_num_minus1,
  835. current->rplm_l0[i].abs_diff_pic_num_minus1,
  836. 0, (1 + current->field_pic_flag) *
  837. (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
  838. else if (mopn == 2)
  839. xue(long_term_pic_num,
  840. current->rplm_l0[i].long_term_pic_num,
  841. 0, sps->max_num_ref_frames - 1, 0);
  842. }
  843. }
  844. }
  845. if (current->slice_type % 5 == 1) {
  846. flag(ref_pic_list_modification_flag_l1);
  847. if (current->ref_pic_list_modification_flag_l1) {
  848. for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
  849. xue(modification_of_pic_nums_idc,
  850. current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
  851. mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
  852. if (mopn == 3)
  853. break;
  854. if (mopn == 0 || mopn == 1)
  855. xue(abs_diff_pic_num_minus1,
  856. current->rplm_l1[i].abs_diff_pic_num_minus1,
  857. 0, (1 + current->field_pic_flag) *
  858. (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
  859. else if (mopn == 2)
  860. xue(long_term_pic_num,
  861. current->rplm_l1[i].long_term_pic_num,
  862. 0, sps->max_num_ref_frames - 1, 0);
  863. }
  864. }
  865. }
  866. return 0;
  867. }
  868. static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
  869. H264RawSliceHeader *current)
  870. {
  871. CodedBitstreamH264Context *h264 = ctx->priv_data;
  872. const H264RawSPS *sps = h264->active_sps;
  873. int chroma;
  874. int err, i, j;
  875. ue(luma_log2_weight_denom, 0, 7);
  876. chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
  877. if (chroma)
  878. ue(chroma_log2_weight_denom, 0, 7);
  879. for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
  880. flags(luma_weight_l0_flag[i], 1, i);
  881. if (current->luma_weight_l0_flag[i]) {
  882. ses(luma_weight_l0[i], -128, +127, 1, i);
  883. ses(luma_offset_l0[i], -128, +127, 1, i);
  884. }
  885. if (chroma) {
  886. flags(chroma_weight_l0_flag[i], 1, i);
  887. if (current->chroma_weight_l0_flag[i]) {
  888. for (j = 0; j < 2; j++) {
  889. ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
  890. ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
  891. }
  892. }
  893. }
  894. }
  895. if (current->slice_type % 5 == 1) {
  896. for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
  897. flags(luma_weight_l1_flag[i], 1, i);
  898. if (current->luma_weight_l1_flag[i]) {
  899. ses(luma_weight_l1[i], -128, +127, 1, i);
  900. ses(luma_offset_l1[i], -128, +127, 1, i);
  901. }
  902. if (chroma) {
  903. flags(chroma_weight_l1_flag[i], 1, i);
  904. if (current->chroma_weight_l1_flag[i]) {
  905. for (j = 0; j < 2; j++) {
  906. ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
  907. ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
  908. }
  909. }
  910. }
  911. }
  912. }
  913. return 0;
  914. }
  915. static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
  916. H264RawSliceHeader *current, int idr_pic_flag)
  917. {
  918. CodedBitstreamH264Context *h264 = ctx->priv_data;
  919. const H264RawSPS *sps = h264->active_sps;
  920. int err, i;
  921. uint32_t mmco;
  922. if (idr_pic_flag) {
  923. flag(no_output_of_prior_pics_flag);
  924. flag(long_term_reference_flag);
  925. } else {
  926. flag(adaptive_ref_pic_marking_mode_flag);
  927. if (current->adaptive_ref_pic_marking_mode_flag) {
  928. for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
  929. xue(memory_management_control_operation,
  930. current->mmco[i].memory_management_control_operation,
  931. 0, 6, 0);
  932. mmco = current->mmco[i].memory_management_control_operation;
  933. if (mmco == 0)
  934. break;
  935. if (mmco == 1 || mmco == 3)
  936. xue(difference_of_pic_nums_minus1,
  937. current->mmco[i].difference_of_pic_nums_minus1,
  938. 0, INT32_MAX, 0);
  939. if (mmco == 2)
  940. xue(long_term_pic_num,
  941. current->mmco[i].long_term_pic_num,
  942. 0, sps->max_num_ref_frames - 1, 0);
  943. if (mmco == 3 || mmco == 6)
  944. xue(long_term_frame_idx,
  945. current->mmco[i].long_term_frame_idx,
  946. 0, sps->max_num_ref_frames - 1, 0);
  947. if (mmco == 4)
  948. xue(max_long_term_frame_idx_plus1,
  949. current->mmco[i].max_long_term_frame_idx_plus1,
  950. 0, sps->max_num_ref_frames, 0);
  951. }
  952. if (i == H264_MAX_MMCO_COUNT) {
  953. av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
  954. "memory management control operations.\n");
  955. return AVERROR_INVALIDDATA;
  956. }
  957. }
  958. }
  959. return 0;
  960. }
  961. static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
  962. H264RawSliceHeader *current)
  963. {
  964. CodedBitstreamH264Context *h264 = ctx->priv_data;
  965. const H264RawSPS *sps;
  966. const H264RawPPS *pps;
  967. int err;
  968. int idr_pic_flag;
  969. int slice_type_i, slice_type_p, slice_type_b;
  970. int slice_type_si, slice_type_sp;
  971. HEADER("Slice Header");
  972. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  973. 1 << H264_NAL_SLICE |
  974. 1 << H264_NAL_IDR_SLICE |
  975. 1 << H264_NAL_AUXILIARY_SLICE));
  976. if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
  977. if (!h264->last_slice_nal_unit_type) {
  978. av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
  979. "is not decodable without the main picture "
  980. "in the same access unit.\n");
  981. return AVERROR_INVALIDDATA;
  982. }
  983. } else {
  984. h264->last_slice_nal_unit_type =
  985. current->nal_unit_header.nal_unit_type;
  986. }
  987. idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
  988. ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
  989. ue(slice_type, 0, 9);
  990. slice_type_i = current->slice_type % 5 == 2;
  991. slice_type_p = current->slice_type % 5 == 0;
  992. slice_type_b = current->slice_type % 5 == 1;
  993. slice_type_si = current->slice_type % 5 == 4;
  994. slice_type_sp = current->slice_type % 5 == 3;
  995. if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
  996. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
  997. "for IDR picture.\n", current->slice_type);
  998. return AVERROR_INVALIDDATA;
  999. }
  1000. ue(pic_parameter_set_id, 0, 255);
  1001. pps = h264->pps[current->pic_parameter_set_id];
  1002. if (!pps) {
  1003. av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
  1004. current->pic_parameter_set_id);
  1005. return AVERROR_INVALIDDATA;
  1006. }
  1007. h264->active_pps = pps;
  1008. sps = h264->sps[pps->seq_parameter_set_id];
  1009. if (!sps) {
  1010. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  1011. pps->seq_parameter_set_id);
  1012. return AVERROR_INVALIDDATA;
  1013. }
  1014. h264->active_sps = sps;
  1015. if (sps->separate_colour_plane_flag)
  1016. u(2, colour_plane_id, 0, 2);
  1017. u(sps->log2_max_frame_num_minus4 + 4, frame_num,
  1018. 0, MAX_UINT_BITS(sps->log2_max_frame_num_minus4 + 4));
  1019. if (!sps->frame_mbs_only_flag) {
  1020. flag(field_pic_flag);
  1021. if (current->field_pic_flag)
  1022. flag(bottom_field_flag);
  1023. else
  1024. infer(bottom_field_flag, 0);
  1025. } else {
  1026. infer(field_pic_flag, 0);
  1027. infer(bottom_field_flag, 0);
  1028. }
  1029. if (idr_pic_flag)
  1030. ue(idr_pic_id, 0, 65535);
  1031. if (sps->pic_order_cnt_type == 0) {
  1032. u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb,
  1033. 0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
  1034. if (pps->bottom_field_pic_order_in_frame_present_flag &&
  1035. !current->field_pic_flag)
  1036. se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
  1037. } else if (sps->pic_order_cnt_type == 1) {
  1038. if (!sps->delta_pic_order_always_zero_flag) {
  1039. se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
  1040. if (pps->bottom_field_pic_order_in_frame_present_flag &&
  1041. !current->field_pic_flag)
  1042. se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
  1043. else
  1044. infer(delta_pic_order_cnt[1], 0);
  1045. } else {
  1046. infer(delta_pic_order_cnt[0], 0);
  1047. infer(delta_pic_order_cnt[1], 0);
  1048. }
  1049. }
  1050. if (pps->redundant_pic_cnt_present_flag)
  1051. ue(redundant_pic_cnt, 0, 127);
  1052. if (slice_type_b)
  1053. flag(direct_spatial_mv_pred_flag);
  1054. if (slice_type_p || slice_type_sp || slice_type_b) {
  1055. flag(num_ref_idx_active_override_flag);
  1056. if (current->num_ref_idx_active_override_flag) {
  1057. ue(num_ref_idx_l0_active_minus1, 0, 31);
  1058. if (slice_type_b)
  1059. ue(num_ref_idx_l1_active_minus1, 0, 31);
  1060. } else {
  1061. infer(num_ref_idx_l0_active_minus1,
  1062. pps->num_ref_idx_l0_default_active_minus1);
  1063. infer(num_ref_idx_l1_active_minus1,
  1064. pps->num_ref_idx_l1_default_active_minus1);
  1065. }
  1066. }
  1067. if (current->nal_unit_header.nal_unit_type == 20 ||
  1068. current->nal_unit_header.nal_unit_type == 21) {
  1069. av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
  1070. return AVERROR_PATCHWELCOME;
  1071. } else {
  1072. CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
  1073. }
  1074. if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
  1075. (pps->weighted_bipred_idc == 1 && slice_type_b)) {
  1076. CHECK(FUNC(pred_weight_table)(ctx, rw, current));
  1077. }
  1078. if (current->nal_unit_header.nal_ref_idc != 0) {
  1079. CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
  1080. }
  1081. if (pps->entropy_coding_mode_flag &&
  1082. !slice_type_i && !slice_type_si) {
  1083. ue(cabac_init_idc, 0, 2);
  1084. }
  1085. se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
  1086. + 51 + 6 * sps->bit_depth_luma_minus8);
  1087. if (slice_type_sp || slice_type_si) {
  1088. if (slice_type_sp)
  1089. flag(sp_for_switch_flag);
  1090. se(slice_qs_delta, -51, +51);
  1091. }
  1092. if (pps->deblocking_filter_control_present_flag) {
  1093. ue(disable_deblocking_filter_idc, 0, 2);
  1094. if (current->disable_deblocking_filter_idc != 1) {
  1095. se(slice_alpha_c0_offset_div2, -6, +6);
  1096. se(slice_beta_offset_div2, -6, +6);
  1097. } else {
  1098. infer(slice_alpha_c0_offset_div2, 0);
  1099. infer(slice_beta_offset_div2, 0);
  1100. }
  1101. } else {
  1102. infer(disable_deblocking_filter_idc, 0);
  1103. infer(slice_alpha_c0_offset_div2, 0);
  1104. infer(slice_beta_offset_div2, 0);
  1105. }
  1106. if (pps->num_slice_groups_minus1 > 0 &&
  1107. pps->slice_group_map_type >= 3 &&
  1108. pps->slice_group_map_type <= 5) {
  1109. unsigned int pic_size, max, bits;
  1110. pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
  1111. (sps->pic_height_in_map_units_minus1 + 1);
  1112. max = (pic_size + pps->slice_group_change_rate_minus1) /
  1113. (pps->slice_group_change_rate_minus1 + 1);
  1114. bits = av_log2(2 * max - 1);
  1115. u(bits, slice_group_change_cycle, 0, max);
  1116. }
  1117. if (pps->entropy_coding_mode_flag) {
  1118. while (byte_alignment(rw))
  1119. fixed(1, cabac_alignment_one_bit, 1);
  1120. }
  1121. return 0;
  1122. }
  1123. static int FUNC(filler)(CodedBitstreamContext *ctx, RWContext *rw,
  1124. H264RawFiller *current)
  1125. {
  1126. int err;
  1127. HEADER("Filler Data");
  1128. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  1129. 1 << H264_NAL_FILLER_DATA));
  1130. #ifdef READ
  1131. while (show_bits(rw, 8) == 0xff) {
  1132. fixed(8, ff_byte, 0xff);
  1133. ++current->filler_size;
  1134. }
  1135. #else
  1136. {
  1137. uint32_t i;
  1138. for (i = 0; i < current->filler_size; i++)
  1139. fixed(8, ff_byte, 0xff);
  1140. }
  1141. #endif
  1142. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  1143. return 0;
  1144. }