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.

1330 lines
44KB

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