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.

1423 lines
46KB

  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. }
  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. ub(8, profile_idc);
  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. ub(8, level_idc);
  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. ub(bits, alpha_opaque_value);
  317. ub(bits, alpha_transparent_value);
  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. HEADER("Buffering Period");
  418. ue(seq_parameter_set_id, 0, 31);
  419. sps = h264->sps[current->seq_parameter_set_id];
  420. if (!sps) {
  421. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  422. current->seq_parameter_set_id);
  423. return AVERROR_INVALIDDATA;
  424. }
  425. h264->active_sps = sps;
  426. if (sps->vui.nal_hrd_parameters_present_flag) {
  427. for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
  428. length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
  429. xu(length, initial_cpb_removal_delay[SchedSelIdx],
  430. current->nal.initial_cpb_removal_delay[i],
  431. 1, MAX_UINT_BITS(length), 1, i);
  432. xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
  433. current->nal.initial_cpb_removal_delay_offset[i],
  434. 0, MAX_UINT_BITS(length), 1, i);
  435. }
  436. }
  437. if (sps->vui.vcl_hrd_parameters_present_flag) {
  438. for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
  439. length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
  440. xu(length, initial_cpb_removal_delay[SchedSelIdx],
  441. current->vcl.initial_cpb_removal_delay[i],
  442. 1, MAX_UINT_BITS(length), 1, i);
  443. xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
  444. current->vcl.initial_cpb_removal_delay_offset[i],
  445. 0, MAX_UINT_BITS(length), 1, i);
  446. }
  447. }
  448. return 0;
  449. }
  450. static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
  451. H264RawSEIPicTimestamp *current,
  452. const H264RawSPS *sps)
  453. {
  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. ub(8, n_frames);
  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. if (sps->vui.nal_hrd_parameters_present_flag)
  481. time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
  482. else if (sps->vui.vcl_hrd_parameters_present_flag)
  483. time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
  484. else
  485. time_offset_length = 24;
  486. if (time_offset_length > 0)
  487. ib(time_offset_length, time_offset);
  488. else
  489. infer(time_offset, 0);
  490. return 0;
  491. }
  492. static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
  493. H264RawSEIPicTiming *current)
  494. {
  495. CodedBitstreamH264Context *h264 = ctx->priv_data;
  496. const H264RawSPS *sps;
  497. int err;
  498. HEADER("Picture Timing");
  499. sps = h264->active_sps;
  500. if (!sps) {
  501. // If there is exactly one possible SPS but it is not yet active
  502. // then just assume that it should be the active one.
  503. int i, k = -1;
  504. for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
  505. if (h264->sps[i]) {
  506. if (k >= 0) {
  507. k = -1;
  508. break;
  509. }
  510. k = i;
  511. }
  512. }
  513. if (k >= 0)
  514. sps = h264->sps[k];
  515. }
  516. if (!sps) {
  517. av_log(ctx->log_ctx, AV_LOG_ERROR,
  518. "No active SPS for pic_timing.\n");
  519. return AVERROR_INVALIDDATA;
  520. }
  521. if (sps->vui.nal_hrd_parameters_present_flag ||
  522. sps->vui.vcl_hrd_parameters_present_flag) {
  523. const H264RawHRD *hrd;
  524. if (sps->vui.nal_hrd_parameters_present_flag)
  525. hrd = &sps->vui.nal_hrd_parameters;
  526. else if (sps->vui.vcl_hrd_parameters_present_flag)
  527. hrd = &sps->vui.vcl_hrd_parameters;
  528. else {
  529. av_log(ctx->log_ctx, AV_LOG_ERROR,
  530. "No HRD parameters for pic_timing.\n");
  531. return AVERROR_INVALIDDATA;
  532. }
  533. ub(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay);
  534. ub(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay);
  535. }
  536. if (sps->vui.pic_struct_present_flag) {
  537. static const uint8_t num_clock_ts[9] = {
  538. 1, 1, 1, 2, 2, 3, 3, 2, 3
  539. };
  540. int i;
  541. u(4, pic_struct, 0, 8);
  542. if (current->pic_struct > 8)
  543. return AVERROR_INVALIDDATA;
  544. for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
  545. flags(clock_timestamp_flag[i], 1, i);
  546. if (current->clock_timestamp_flag[i])
  547. CHECK(FUNC(sei_pic_timestamp)(ctx, rw,
  548. &current->timestamp[i], sps));
  549. }
  550. }
  551. return 0;
  552. }
  553. static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
  554. H264RawSEIPanScanRect *current)
  555. {
  556. int err, i;
  557. HEADER("Pan-Scan Rectangle");
  558. ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
  559. flag(pan_scan_rect_cancel_flag);
  560. if (!current->pan_scan_rect_cancel_flag) {
  561. ue(pan_scan_cnt_minus1, 0, 2);
  562. for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
  563. ses(pan_scan_rect_left_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  564. ses(pan_scan_rect_right_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  565. ses(pan_scan_rect_top_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  566. ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
  567. }
  568. ue(pan_scan_rect_repetition_period, 0, 16384);
  569. }
  570. return 0;
  571. }
  572. static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
  573. H264RawSEIUserDataRegistered *current,
  574. uint32_t *payload_size)
  575. {
  576. int err, i, j;
  577. HEADER("User Data Registered ITU-T T.35");
  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. HEADER("User Data Unregistered");
  606. #ifdef READ
  607. if (*payload_size < 16) {
  608. av_log(ctx->log_ctx, AV_LOG_ERROR,
  609. "Invalid SEI user data unregistered payload.\n");
  610. return AVERROR_INVALIDDATA;
  611. }
  612. current->data_length = *payload_size - 16;
  613. #else
  614. *payload_size = 16 + current->data_length;
  615. #endif
  616. for (i = 0; i < 16; i++)
  617. us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
  618. allocate(current->data, current->data_length);
  619. for (i = 0; i < current->data_length; i++)
  620. xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
  621. return 0;
  622. }
  623. static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
  624. H264RawSEIRecoveryPoint *current)
  625. {
  626. int err;
  627. HEADER("Recovery Point");
  628. ue(recovery_frame_cnt, 0, 65535);
  629. flag(exact_match_flag);
  630. flag(broken_link_flag);
  631. u(2, changing_slice_group_idc, 0, 2);
  632. return 0;
  633. }
  634. static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
  635. H264RawSEIDisplayOrientation *current)
  636. {
  637. int err;
  638. HEADER("Display Orientation");
  639. flag(display_orientation_cancel_flag);
  640. if (!current->display_orientation_cancel_flag) {
  641. flag(hor_flip);
  642. flag(ver_flip);
  643. ub(16, anticlockwise_rotation);
  644. ue(display_orientation_repetition_period, 0, 16384);
  645. flag(display_orientation_extension_flag);
  646. }
  647. return 0;
  648. }
  649. static int FUNC(sei_mastering_display_colour_volume)(CodedBitstreamContext *ctx, RWContext *rw,
  650. H264RawSEIMasteringDisplayColourVolume *current)
  651. {
  652. int err, c;
  653. HEADER("Mastering Display Colour Volume");
  654. for (c = 0; c < 3; c++) {
  655. us(16, display_primaries_x[c], 0, 50000, 1, c);
  656. us(16, display_primaries_y[c], 0, 50000, 1, c);
  657. }
  658. u(16, white_point_x, 0, 50000);
  659. u(16, white_point_y, 0, 50000);
  660. u(32, max_display_mastering_luminance, 1, MAX_UINT_BITS(32));
  661. u(32, min_display_mastering_luminance, 0, current->max_display_mastering_luminance - 1);
  662. return 0;
  663. }
  664. static int FUNC(sei_alternative_transfer_characteristics)(CodedBitstreamContext *ctx,
  665. RWContext *rw,
  666. H264RawSEIAlternativeTransferCharacteristics *current)
  667. {
  668. int err;
  669. HEADER("Alternative Transfer Characteristics");
  670. ub(8, preferred_transfer_characteristics);
  671. return 0;
  672. }
  673. static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
  674. H264RawSEIPayload *current)
  675. {
  676. int err, i;
  677. int start_position, end_position;
  678. #ifdef READ
  679. start_position = get_bits_count(rw);
  680. #else
  681. start_position = put_bits_count(rw);
  682. #endif
  683. switch (current->payload_type) {
  684. case H264_SEI_TYPE_BUFFERING_PERIOD:
  685. CHECK(FUNC(sei_buffering_period)
  686. (ctx, rw, &current->payload.buffering_period));
  687. break;
  688. case H264_SEI_TYPE_PIC_TIMING:
  689. CHECK(FUNC(sei_pic_timing)
  690. (ctx, rw, &current->payload.pic_timing));
  691. break;
  692. case H264_SEI_TYPE_PAN_SCAN_RECT:
  693. CHECK(FUNC(sei_pan_scan_rect)
  694. (ctx, rw, &current->payload.pan_scan_rect));
  695. break;
  696. case H264_SEI_TYPE_FILLER_PAYLOAD:
  697. {
  698. for (i = 0; i < current->payload_size; i++)
  699. fixed(8, ff_byte, 0xff);
  700. }
  701. break;
  702. case H264_SEI_TYPE_USER_DATA_REGISTERED:
  703. CHECK(FUNC(sei_user_data_registered)
  704. (ctx, rw, &current->payload.user_data_registered, &current->payload_size));
  705. break;
  706. case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
  707. CHECK(FUNC(sei_user_data_unregistered)
  708. (ctx, rw, &current->payload.user_data_unregistered, &current->payload_size));
  709. break;
  710. case H264_SEI_TYPE_RECOVERY_POINT:
  711. CHECK(FUNC(sei_recovery_point)
  712. (ctx, rw, &current->payload.recovery_point));
  713. break;
  714. case H264_SEI_TYPE_DISPLAY_ORIENTATION:
  715. CHECK(FUNC(sei_display_orientation)
  716. (ctx, rw, &current->payload.display_orientation));
  717. break;
  718. case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
  719. CHECK(FUNC(sei_mastering_display_colour_volume)
  720. (ctx, rw, &current->payload.mastering_display_colour_volume));
  721. break;
  722. case H264_SEI_TYPE_ALTERNATIVE_TRANSFER:
  723. CHECK(FUNC(sei_alternative_transfer_characteristics)
  724. (ctx, rw, &current->payload.alternative_transfer_characteristics));
  725. break;
  726. default:
  727. {
  728. #ifdef READ
  729. current->payload.other.data_length = current->payload_size;
  730. #endif
  731. allocate(current->payload.other.data, current->payload.other.data_length);
  732. for (i = 0; i < current->payload.other.data_length; i++)
  733. xu(8, payload_byte[i], current->payload.other.data[i], 0, 255, 1, i);
  734. }
  735. }
  736. if (byte_alignment(rw)) {
  737. fixed(1, bit_equal_to_one, 1);
  738. while (byte_alignment(rw))
  739. fixed(1, bit_equal_to_zero, 0);
  740. }
  741. #ifdef READ
  742. end_position = get_bits_count(rw);
  743. if (end_position < start_position + 8 * current->payload_size) {
  744. av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
  745. "header %"PRIu32" bits, actually %d bits.\n",
  746. 8 * current->payload_size,
  747. end_position - start_position);
  748. return AVERROR_INVALIDDATA;
  749. }
  750. #else
  751. end_position = put_bits_count(rw);
  752. current->payload_size = (end_position - start_position) / 8;
  753. #endif
  754. return 0;
  755. }
  756. static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
  757. H264RawSEI *current)
  758. {
  759. int err, k;
  760. HEADER("Supplemental Enhancement Information");
  761. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  762. 1 << H264_NAL_SEI));
  763. #ifdef READ
  764. for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
  765. uint32_t payload_type = 0;
  766. uint32_t payload_size = 0;
  767. uint32_t tmp;
  768. while (show_bits(rw, 8) == 0xff) {
  769. fixed(8, ff_byte, 0xff);
  770. payload_type += 255;
  771. }
  772. xu(8, last_payload_type_byte, tmp, 0, 254, 0);
  773. payload_type += tmp;
  774. while (show_bits(rw, 8) == 0xff) {
  775. fixed(8, ff_byte, 0xff);
  776. payload_size += 255;
  777. }
  778. xu(8, last_payload_size_byte, tmp, 0, 254, 0);
  779. payload_size += tmp;
  780. current->payload[k].payload_type = payload_type;
  781. current->payload[k].payload_size = payload_size;
  782. CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
  783. if (!cbs_h2645_read_more_rbsp_data(rw))
  784. break;
  785. }
  786. if (k >= H264_MAX_SEI_PAYLOADS) {
  787. av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
  788. "SEI message: found %d.\n", k);
  789. return AVERROR_INVALIDDATA;
  790. }
  791. current->payload_count = k + 1;
  792. #else
  793. for (k = 0; k < current->payload_count; k++) {
  794. PutBitContext start_state;
  795. uint32_t tmp;
  796. int need_size, i;
  797. // Somewhat clumsy: we write the payload twice when
  798. // we don't know the size in advance. This will mess
  799. // with trace output, but is otherwise harmless.
  800. start_state = *rw;
  801. need_size = !current->payload[k].payload_size;
  802. for (i = 0; i < 1 + need_size; i++) {
  803. *rw = start_state;
  804. tmp = current->payload[k].payload_type;
  805. while (tmp >= 255) {
  806. fixed(8, ff_byte, 0xff);
  807. tmp -= 255;
  808. }
  809. xu(8, last_payload_type_byte, tmp, 0, 254, 0);
  810. tmp = current->payload[k].payload_size;
  811. while (tmp >= 255) {
  812. fixed(8, ff_byte, 0xff);
  813. tmp -= 255;
  814. }
  815. xu(8, last_payload_size_byte, tmp, 0, 254, 0);
  816. CHECK(FUNC(sei_payload)(ctx, rw, &current->payload[k]));
  817. }
  818. }
  819. #endif
  820. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  821. return 0;
  822. }
  823. static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
  824. H264RawAUD *current)
  825. {
  826. int err;
  827. HEADER("Access Unit Delimiter");
  828. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  829. 1 << H264_NAL_AUD));
  830. ub(3, primary_pic_type);
  831. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  832. return 0;
  833. }
  834. static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
  835. H264RawSliceHeader *current)
  836. {
  837. CodedBitstreamH264Context *h264 = ctx->priv_data;
  838. const H264RawSPS *sps = h264->active_sps;
  839. int err, i, mopn;
  840. if (current->slice_type % 5 != 2 &&
  841. current->slice_type % 5 != 4) {
  842. flag(ref_pic_list_modification_flag_l0);
  843. if (current->ref_pic_list_modification_flag_l0) {
  844. for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
  845. xue(modification_of_pic_nums_idc,
  846. current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
  847. mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
  848. if (mopn == 3)
  849. break;
  850. if (mopn == 0 || mopn == 1)
  851. xue(abs_diff_pic_num_minus1,
  852. current->rplm_l0[i].abs_diff_pic_num_minus1,
  853. 0, (1 + current->field_pic_flag) *
  854. (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
  855. else if (mopn == 2)
  856. xue(long_term_pic_num,
  857. current->rplm_l0[i].long_term_pic_num,
  858. 0, sps->max_num_ref_frames - 1, 0);
  859. }
  860. }
  861. }
  862. if (current->slice_type % 5 == 1) {
  863. flag(ref_pic_list_modification_flag_l1);
  864. if (current->ref_pic_list_modification_flag_l1) {
  865. for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
  866. xue(modification_of_pic_nums_idc,
  867. current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
  868. mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
  869. if (mopn == 3)
  870. break;
  871. if (mopn == 0 || mopn == 1)
  872. xue(abs_diff_pic_num_minus1,
  873. current->rplm_l1[i].abs_diff_pic_num_minus1,
  874. 0, (1 + current->field_pic_flag) *
  875. (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
  876. else if (mopn == 2)
  877. xue(long_term_pic_num,
  878. current->rplm_l1[i].long_term_pic_num,
  879. 0, sps->max_num_ref_frames - 1, 0);
  880. }
  881. }
  882. }
  883. return 0;
  884. }
  885. static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
  886. H264RawSliceHeader *current)
  887. {
  888. CodedBitstreamH264Context *h264 = ctx->priv_data;
  889. const H264RawSPS *sps = h264->active_sps;
  890. int chroma;
  891. int err, i, j;
  892. ue(luma_log2_weight_denom, 0, 7);
  893. chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
  894. if (chroma)
  895. ue(chroma_log2_weight_denom, 0, 7);
  896. for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
  897. flags(luma_weight_l0_flag[i], 1, i);
  898. if (current->luma_weight_l0_flag[i]) {
  899. ses(luma_weight_l0[i], -128, +127, 1, i);
  900. ses(luma_offset_l0[i], -128, +127, 1, i);
  901. }
  902. if (chroma) {
  903. flags(chroma_weight_l0_flag[i], 1, i);
  904. if (current->chroma_weight_l0_flag[i]) {
  905. for (j = 0; j < 2; j++) {
  906. ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
  907. ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
  908. }
  909. }
  910. }
  911. }
  912. if (current->slice_type % 5 == 1) {
  913. for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
  914. flags(luma_weight_l1_flag[i], 1, i);
  915. if (current->luma_weight_l1_flag[i]) {
  916. ses(luma_weight_l1[i], -128, +127, 1, i);
  917. ses(luma_offset_l1[i], -128, +127, 1, i);
  918. }
  919. if (chroma) {
  920. flags(chroma_weight_l1_flag[i], 1, i);
  921. if (current->chroma_weight_l1_flag[i]) {
  922. for (j = 0; j < 2; j++) {
  923. ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
  924. ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
  925. }
  926. }
  927. }
  928. }
  929. }
  930. return 0;
  931. }
  932. static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
  933. H264RawSliceHeader *current, int idr_pic_flag)
  934. {
  935. CodedBitstreamH264Context *h264 = ctx->priv_data;
  936. const H264RawSPS *sps = h264->active_sps;
  937. int err, i;
  938. uint32_t mmco;
  939. if (idr_pic_flag) {
  940. flag(no_output_of_prior_pics_flag);
  941. flag(long_term_reference_flag);
  942. } else {
  943. flag(adaptive_ref_pic_marking_mode_flag);
  944. if (current->adaptive_ref_pic_marking_mode_flag) {
  945. for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
  946. xue(memory_management_control_operation,
  947. current->mmco[i].memory_management_control_operation,
  948. 0, 6, 0);
  949. mmco = current->mmco[i].memory_management_control_operation;
  950. if (mmco == 0)
  951. break;
  952. if (mmco == 1 || mmco == 3)
  953. xue(difference_of_pic_nums_minus1,
  954. current->mmco[i].difference_of_pic_nums_minus1,
  955. 0, INT32_MAX, 0);
  956. if (mmco == 2)
  957. xue(long_term_pic_num,
  958. current->mmco[i].long_term_pic_num,
  959. 0, sps->max_num_ref_frames - 1, 0);
  960. if (mmco == 3 || mmco == 6)
  961. xue(long_term_frame_idx,
  962. current->mmco[i].long_term_frame_idx,
  963. 0, sps->max_num_ref_frames - 1, 0);
  964. if (mmco == 4)
  965. xue(max_long_term_frame_idx_plus1,
  966. current->mmco[i].max_long_term_frame_idx_plus1,
  967. 0, sps->max_num_ref_frames, 0);
  968. }
  969. if (i == H264_MAX_MMCO_COUNT) {
  970. av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
  971. "memory management control operations.\n");
  972. return AVERROR_INVALIDDATA;
  973. }
  974. }
  975. }
  976. return 0;
  977. }
  978. static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
  979. H264RawSliceHeader *current)
  980. {
  981. CodedBitstreamH264Context *h264 = ctx->priv_data;
  982. const H264RawSPS *sps;
  983. const H264RawPPS *pps;
  984. int err;
  985. int idr_pic_flag;
  986. int slice_type_i, slice_type_p, slice_type_b;
  987. int slice_type_si, slice_type_sp;
  988. HEADER("Slice Header");
  989. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  990. 1 << H264_NAL_SLICE |
  991. 1 << H264_NAL_IDR_SLICE |
  992. 1 << H264_NAL_AUXILIARY_SLICE));
  993. if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
  994. if (!h264->last_slice_nal_unit_type) {
  995. av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
  996. "is not decodable without the main picture "
  997. "in the same access unit.\n");
  998. return AVERROR_INVALIDDATA;
  999. }
  1000. idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
  1001. } else {
  1002. idr_pic_flag = current->nal_unit_header.nal_unit_type == H264_NAL_IDR_SLICE;
  1003. }
  1004. ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
  1005. ue(slice_type, 0, 9);
  1006. slice_type_i = current->slice_type % 5 == 2;
  1007. slice_type_p = current->slice_type % 5 == 0;
  1008. slice_type_b = current->slice_type % 5 == 1;
  1009. slice_type_si = current->slice_type % 5 == 4;
  1010. slice_type_sp = current->slice_type % 5 == 3;
  1011. if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
  1012. av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
  1013. "for IDR picture.\n", current->slice_type);
  1014. return AVERROR_INVALIDDATA;
  1015. }
  1016. ue(pic_parameter_set_id, 0, 255);
  1017. pps = h264->pps[current->pic_parameter_set_id];
  1018. if (!pps) {
  1019. av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
  1020. current->pic_parameter_set_id);
  1021. return AVERROR_INVALIDDATA;
  1022. }
  1023. h264->active_pps = pps;
  1024. sps = h264->sps[pps->seq_parameter_set_id];
  1025. if (!sps) {
  1026. av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
  1027. pps->seq_parameter_set_id);
  1028. return AVERROR_INVALIDDATA;
  1029. }
  1030. h264->active_sps = sps;
  1031. if (sps->separate_colour_plane_flag)
  1032. u(2, colour_plane_id, 0, 2);
  1033. ub(sps->log2_max_frame_num_minus4 + 4, frame_num);
  1034. if (!sps->frame_mbs_only_flag) {
  1035. flag(field_pic_flag);
  1036. if (current->field_pic_flag)
  1037. flag(bottom_field_flag);
  1038. else
  1039. infer(bottom_field_flag, 0);
  1040. } else {
  1041. infer(field_pic_flag, 0);
  1042. infer(bottom_field_flag, 0);
  1043. }
  1044. if (idr_pic_flag)
  1045. ue(idr_pic_id, 0, 65535);
  1046. if (sps->pic_order_cnt_type == 0) {
  1047. ub(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb);
  1048. if (pps->bottom_field_pic_order_in_frame_present_flag &&
  1049. !current->field_pic_flag)
  1050. se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
  1051. } else if (sps->pic_order_cnt_type == 1) {
  1052. if (!sps->delta_pic_order_always_zero_flag) {
  1053. se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
  1054. if (pps->bottom_field_pic_order_in_frame_present_flag &&
  1055. !current->field_pic_flag)
  1056. se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
  1057. else
  1058. infer(delta_pic_order_cnt[1], 0);
  1059. } else {
  1060. infer(delta_pic_order_cnt[0], 0);
  1061. infer(delta_pic_order_cnt[1], 0);
  1062. }
  1063. }
  1064. if (pps->redundant_pic_cnt_present_flag)
  1065. ue(redundant_pic_cnt, 0, 127);
  1066. else
  1067. infer(redundant_pic_cnt, 0);
  1068. if (current->nal_unit_header.nal_unit_type != H264_NAL_AUXILIARY_SLICE
  1069. && !current->redundant_pic_cnt)
  1070. h264->last_slice_nal_unit_type =
  1071. current->nal_unit_header.nal_unit_type;
  1072. if (slice_type_b)
  1073. flag(direct_spatial_mv_pred_flag);
  1074. if (slice_type_p || slice_type_sp || slice_type_b) {
  1075. flag(num_ref_idx_active_override_flag);
  1076. if (current->num_ref_idx_active_override_flag) {
  1077. ue(num_ref_idx_l0_active_minus1, 0, 31);
  1078. if (slice_type_b)
  1079. ue(num_ref_idx_l1_active_minus1, 0, 31);
  1080. } else {
  1081. infer(num_ref_idx_l0_active_minus1,
  1082. pps->num_ref_idx_l0_default_active_minus1);
  1083. infer(num_ref_idx_l1_active_minus1,
  1084. pps->num_ref_idx_l1_default_active_minus1);
  1085. }
  1086. }
  1087. if (current->nal_unit_header.nal_unit_type == 20 ||
  1088. current->nal_unit_header.nal_unit_type == 21) {
  1089. av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
  1090. return AVERROR_PATCHWELCOME;
  1091. } else {
  1092. CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
  1093. }
  1094. if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
  1095. (pps->weighted_bipred_idc == 1 && slice_type_b)) {
  1096. CHECK(FUNC(pred_weight_table)(ctx, rw, current));
  1097. }
  1098. if (current->nal_unit_header.nal_ref_idc != 0) {
  1099. CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
  1100. }
  1101. if (pps->entropy_coding_mode_flag &&
  1102. !slice_type_i && !slice_type_si) {
  1103. ue(cabac_init_idc, 0, 2);
  1104. }
  1105. se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
  1106. + 51 + 6 * sps->bit_depth_luma_minus8);
  1107. if (slice_type_sp || slice_type_si) {
  1108. if (slice_type_sp)
  1109. flag(sp_for_switch_flag);
  1110. se(slice_qs_delta, -51, +51);
  1111. }
  1112. if (pps->deblocking_filter_control_present_flag) {
  1113. ue(disable_deblocking_filter_idc, 0, 2);
  1114. if (current->disable_deblocking_filter_idc != 1) {
  1115. se(slice_alpha_c0_offset_div2, -6, +6);
  1116. se(slice_beta_offset_div2, -6, +6);
  1117. } else {
  1118. infer(slice_alpha_c0_offset_div2, 0);
  1119. infer(slice_beta_offset_div2, 0);
  1120. }
  1121. } else {
  1122. infer(disable_deblocking_filter_idc, 0);
  1123. infer(slice_alpha_c0_offset_div2, 0);
  1124. infer(slice_beta_offset_div2, 0);
  1125. }
  1126. if (pps->num_slice_groups_minus1 > 0 &&
  1127. pps->slice_group_map_type >= 3 &&
  1128. pps->slice_group_map_type <= 5) {
  1129. unsigned int pic_size, max, bits;
  1130. pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
  1131. (sps->pic_height_in_map_units_minus1 + 1);
  1132. max = (pic_size + pps->slice_group_change_rate_minus1) /
  1133. (pps->slice_group_change_rate_minus1 + 1);
  1134. bits = av_log2(2 * max - 1);
  1135. u(bits, slice_group_change_cycle, 0, max);
  1136. }
  1137. if (pps->entropy_coding_mode_flag) {
  1138. while (byte_alignment(rw))
  1139. fixed(1, cabac_alignment_one_bit, 1);
  1140. }
  1141. return 0;
  1142. }
  1143. static int FUNC(filler)(CodedBitstreamContext *ctx, RWContext *rw,
  1144. H264RawFiller *current)
  1145. {
  1146. int err;
  1147. HEADER("Filler Data");
  1148. CHECK(FUNC(nal_unit_header)(ctx, rw, &current->nal_unit_header,
  1149. 1 << H264_NAL_FILLER_DATA));
  1150. #ifdef READ
  1151. while (show_bits(rw, 8) == 0xff) {
  1152. fixed(8, ff_byte, 0xff);
  1153. ++current->filler_size;
  1154. }
  1155. #else
  1156. {
  1157. uint32_t i;
  1158. for (i = 0; i < current->filler_size; i++)
  1159. fixed(8, ff_byte, 0xff);
  1160. }
  1161. #endif
  1162. CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
  1163. return 0;
  1164. }
  1165. static int FUNC(end_of_sequence)(CodedBitstreamContext *ctx, RWContext *rw,
  1166. H264RawNALUnitHeader *current)
  1167. {
  1168. HEADER("End of Sequence");
  1169. return FUNC(nal_unit_header)(ctx, rw, current,
  1170. 1 << H264_NAL_END_SEQUENCE);
  1171. }
  1172. static int FUNC(end_of_stream)(CodedBitstreamContext *ctx, RWContext *rw,
  1173. H264RawNALUnitHeader *current)
  1174. {
  1175. HEADER("End of Stream");
  1176. return FUNC(nal_unit_header)(ctx, rw, current,
  1177. 1 << H264_NAL_END_STREAM);
  1178. }