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.

1427 lines
47KB

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