2 * This file is part of FFmpeg.
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.
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.
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
19 static int FUNC(rbsp_trailing_bits)(CodedBitstreamContext *ctx, RWContext *rw)
23 fixed(1, rbsp_stop_one_bit, 1);
24 while (byte_alignment(rw) != 0)
25 fixed(1, rbsp_alignment_zero_bit, 0);
30 static int FUNC(nal_unit_header)(CodedBitstreamContext *ctx, RWContext *rw,
31 H264RawNALUnitHeader *current,
32 uint32_t valid_type_mask)
36 u(1, forbidden_zero_bit, 0, 0);
37 u(2, nal_ref_idc, 0, 3);
38 u(5, nal_unit_type, 0, 31);
40 if (!(1 << current->nal_unit_type & valid_type_mask)) {
41 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid NAL unit type %d.\n",
42 current->nal_unit_type);
43 return AVERROR_INVALIDDATA;
46 if (current->nal_unit_type == 14 ||
47 current->nal_unit_type == 20 ||
48 current->nal_unit_type == 21) {
49 if (current->nal_unit_type != 21)
50 flag(svc_extension_flag);
52 flag(avc_3d_extension_flag);
54 if (current->svc_extension_flag) {
55 av_log(ctx->log_ctx, AV_LOG_ERROR, "SVC not supported.\n");
56 return AVERROR_PATCHWELCOME;
58 } else if (current->avc_3d_extension_flag) {
59 av_log(ctx->log_ctx, AV_LOG_ERROR, "3DAVC not supported.\n");
60 return AVERROR_PATCHWELCOME;
63 av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC not supported.\n");
64 return AVERROR_PATCHWELCOME;
71 static int FUNC(scaling_list)(CodedBitstreamContext *ctx, RWContext *rw,
72 H264RawScalingList *current,
73 int size_of_scaling_list)
78 for (i = 0; i < size_of_scaling_list; i++) {
79 ses(delta_scale[i], -128, +127, 1, i);
80 scale = (scale + current->delta_scale[i] + 256) % 256;
88 static int FUNC(hrd_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
93 ue(cpb_cnt_minus1, 0, 31);
94 u(4, bit_rate_scale, 0, 15);
95 u(4, cpb_size_scale, 0, 15);
97 for (i = 0; i <= current->cpb_cnt_minus1; i++) {
98 ues(bit_rate_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
99 ues(cpb_size_value_minus1[i], 0, UINT32_MAX - 1, 1, i);
100 flags(cbr_flag[i], 1, i);
103 u(5, initial_cpb_removal_delay_length_minus1, 0, 31);
104 u(5, cpb_removal_delay_length_minus1, 0, 31);
105 u(5, dpb_output_delay_length_minus1, 0, 31);
106 u(5, time_offset_length, 0, 31);
111 static int FUNC(vui_parameters)(CodedBitstreamContext *ctx, RWContext *rw,
112 H264RawVUI *current, H264RawSPS *sps)
116 flag(aspect_ratio_info_present_flag);
117 if (current->aspect_ratio_info_present_flag) {
118 u(8, aspect_ratio_idc, 0, 255);
119 if (current->aspect_ratio_idc == 255) {
120 u(16, sar_width, 0, 65535);
121 u(16, sar_height, 0, 65535);
124 infer(aspect_ratio_idc, 0);
127 flag(overscan_info_present_flag);
128 if (current->overscan_info_present_flag)
129 flag(overscan_appropriate_flag);
131 flag(video_signal_type_present_flag);
132 if (current->video_signal_type_present_flag) {
133 u(3, video_format, 0, 7);
134 flag(video_full_range_flag);
135 flag(colour_description_present_flag);
136 if (current->colour_description_present_flag) {
137 u(8, colour_primaries, 0, 255);
138 u(8, transfer_characteristics, 0, 255);
139 u(8, matrix_coefficients, 0, 255);
142 infer(video_format, 5);
143 infer(video_full_range_flag, 0);
144 infer(colour_primaries, 2);
145 infer(transfer_characteristics, 2);
146 infer(matrix_coefficients, 2);
149 flag(chroma_loc_info_present_flag);
150 if (current->chroma_loc_info_present_flag) {
151 ue(chroma_sample_loc_type_top_field, 0, 5);
152 ue(chroma_sample_loc_type_bottom_field, 0, 5);
154 infer(chroma_sample_loc_type_top_field, 0);
155 infer(chroma_sample_loc_type_bottom_field, 0);
158 flag(timing_info_present_flag);
159 if (current->timing_info_present_flag) {
160 u(32, num_units_in_tick, 1, UINT32_MAX);
161 u(32, time_scale, 1, UINT32_MAX);
162 flag(fixed_frame_rate_flag);
164 infer(fixed_frame_rate_flag, 0);
167 flag(nal_hrd_parameters_present_flag);
168 if (current->nal_hrd_parameters_present_flag)
169 CHECK(FUNC(hrd_parameters)(ctx, rw, ¤t->nal_hrd_parameters));
171 flag(vcl_hrd_parameters_present_flag);
172 if (current->vcl_hrd_parameters_present_flag)
173 CHECK(FUNC(hrd_parameters)(ctx, rw, ¤t->vcl_hrd_parameters));
175 if (current->nal_hrd_parameters_present_flag ||
176 current->vcl_hrd_parameters_present_flag)
177 flag(low_delay_hrd_flag);
179 infer(low_delay_hrd_flag, 1 - current->fixed_frame_rate_flag);
181 flag(pic_struct_present_flag);
183 flag(bitstream_restriction_flag);
184 if (current->bitstream_restriction_flag) {
185 flag(motion_vectors_over_pic_boundaries_flag);
186 ue(max_bytes_per_pic_denom, 0, 16);
187 ue(max_bits_per_mb_denom, 0, 16);
188 // The current version of the standard constrains this to be in
189 // [0,15], but older versions allow 16.
190 ue(log2_max_mv_length_horizontal, 0, 16);
191 ue(log2_max_mv_length_vertical, 0, 16);
192 ue(max_num_reorder_frames, 0, H264_MAX_DPB_FRAMES);
193 ue(max_dec_frame_buffering, 0, H264_MAX_DPB_FRAMES);
195 infer(motion_vectors_over_pic_boundaries_flag, 1);
196 infer(max_bytes_per_pic_denom, 2);
197 infer(max_bits_per_mb_denom, 1);
198 infer(log2_max_mv_length_horizontal, 15);
199 infer(log2_max_mv_length_vertical, 15);
201 if ((sps->profile_idc == 44 || sps->profile_idc == 86 ||
202 sps->profile_idc == 100 || sps->profile_idc == 110 ||
203 sps->profile_idc == 122 || sps->profile_idc == 244) &&
204 sps->constraint_set3_flag) {
205 infer(max_num_reorder_frames, 0);
206 infer(max_dec_frame_buffering, 0);
208 infer(max_num_reorder_frames, H264_MAX_DPB_FRAMES);
209 infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
216 static int FUNC(vui_parameters_default)(CodedBitstreamContext *ctx,
217 RWContext *rw, H264RawVUI *current,
220 infer(aspect_ratio_idc, 0);
222 infer(video_format, 5);
223 infer(video_full_range_flag, 0);
224 infer(colour_primaries, 2);
225 infer(transfer_characteristics, 2);
226 infer(matrix_coefficients, 2);
228 infer(chroma_sample_loc_type_top_field, 0);
229 infer(chroma_sample_loc_type_bottom_field, 0);
231 infer(fixed_frame_rate_flag, 0);
232 infer(low_delay_hrd_flag, 1);
234 infer(pic_struct_present_flag, 0);
236 infer(motion_vectors_over_pic_boundaries_flag, 1);
237 infer(max_bytes_per_pic_denom, 2);
238 infer(max_bits_per_mb_denom, 1);
239 infer(log2_max_mv_length_horizontal, 15);
240 infer(log2_max_mv_length_vertical, 15);
242 if ((sps->profile_idc == 44 || sps->profile_idc == 86 ||
243 sps->profile_idc == 100 || sps->profile_idc == 110 ||
244 sps->profile_idc == 122 || sps->profile_idc == 244) &&
245 sps->constraint_set3_flag) {
246 infer(max_num_reorder_frames, 0);
247 infer(max_dec_frame_buffering, 0);
249 infer(max_num_reorder_frames, H264_MAX_DPB_FRAMES);
250 infer(max_dec_frame_buffering, H264_MAX_DPB_FRAMES);
256 static int FUNC(sps)(CodedBitstreamContext *ctx, RWContext *rw,
261 HEADER("Sequence Parameter Set");
263 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
266 u(8, profile_idc, 0, 255);
268 flag(constraint_set0_flag);
269 flag(constraint_set1_flag);
270 flag(constraint_set2_flag);
271 flag(constraint_set3_flag);
272 flag(constraint_set4_flag);
273 flag(constraint_set5_flag);
275 u(2, reserved_zero_2bits, 0, 0);
277 u(8, level_idc, 0, 255);
279 ue(seq_parameter_set_id, 0, 31);
281 if (current->profile_idc == 100 || current->profile_idc == 110 ||
282 current->profile_idc == 122 || current->profile_idc == 244 ||
283 current->profile_idc == 44 || current->profile_idc == 83 ||
284 current->profile_idc == 86 || current->profile_idc == 118 ||
285 current->profile_idc == 128 || current->profile_idc == 138) {
286 ue(chroma_format_idc, 0, 3);
288 if (current->chroma_format_idc == 3)
289 flag(separate_colour_plane_flag);
291 infer(separate_colour_plane_flag, 0);
293 ue(bit_depth_luma_minus8, 0, 6);
294 ue(bit_depth_chroma_minus8, 0, 6);
296 flag(qpprime_y_zero_transform_bypass_flag);
298 flag(seq_scaling_matrix_present_flag);
299 if (current->seq_scaling_matrix_present_flag) {
300 for (i = 0; i < ((current->chroma_format_idc != 3) ? 8 : 12); i++) {
301 flags(seq_scaling_list_present_flag[i], 1, i);
302 if (current->seq_scaling_list_present_flag[i]) {
304 CHECK(FUNC(scaling_list)(ctx, rw,
305 ¤t->scaling_list_4x4[i],
308 CHECK(FUNC(scaling_list)(ctx, rw,
309 ¤t->scaling_list_8x8[i - 6],
315 infer(chroma_format_idc, current->profile_idc == 183 ? 0 : 1);
317 infer(separate_colour_plane_flag, 0);
318 infer(bit_depth_luma_minus8, 0);
319 infer(bit_depth_chroma_minus8, 0);
322 ue(log2_max_frame_num_minus4, 0, 12);
323 ue(pic_order_cnt_type, 0, 2);
325 if (current->pic_order_cnt_type == 0) {
326 ue(log2_max_pic_order_cnt_lsb_minus4, 0, 12);
327 } else if (current->pic_order_cnt_type == 1) {
328 flag(delta_pic_order_always_zero_flag);
329 se(offset_for_non_ref_pic, INT32_MIN + 1, INT32_MAX);
330 se(offset_for_top_to_bottom_field, INT32_MIN + 1, INT32_MAX);
331 ue(num_ref_frames_in_pic_order_cnt_cycle, 0, 255);
333 for (i = 0; i < current->num_ref_frames_in_pic_order_cnt_cycle; i++)
334 ses(offset_for_ref_frame[i], INT32_MIN + 1, INT32_MAX, 1, i);
337 ue(max_num_ref_frames, 0, H264_MAX_DPB_FRAMES);
338 flag(gaps_in_frame_num_allowed_flag);
340 ue(pic_width_in_mbs_minus1, 0, H264_MAX_MB_WIDTH);
341 ue(pic_height_in_map_units_minus1, 0, H264_MAX_MB_HEIGHT);
343 flag(frame_mbs_only_flag);
344 if (!current->frame_mbs_only_flag)
345 flag(mb_adaptive_frame_field_flag);
347 flag(direct_8x8_inference_flag);
349 flag(frame_cropping_flag);
350 if (current->frame_cropping_flag) {
351 ue(frame_crop_left_offset, 0, H264_MAX_WIDTH);
352 ue(frame_crop_right_offset, 0, H264_MAX_WIDTH);
353 ue(frame_crop_top_offset, 0, H264_MAX_HEIGHT);
354 ue(frame_crop_bottom_offset, 0, H264_MAX_HEIGHT);
357 flag(vui_parameters_present_flag);
358 if (current->vui_parameters_present_flag)
359 CHECK(FUNC(vui_parameters)(ctx, rw, ¤t->vui, current));
361 CHECK(FUNC(vui_parameters_default)(ctx, rw, ¤t->vui, current));
363 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
368 static int FUNC(sps_extension)(CodedBitstreamContext *ctx, RWContext *rw,
369 H264RawSPSExtension *current)
373 HEADER("Sequence Parameter Set Extension");
375 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
376 1 << H264_NAL_SPS_EXT));
378 ue(seq_parameter_set_id, 0, 31);
380 ue(aux_format_idc, 0, 3);
382 if (current->aux_format_idc != 0) {
385 ue(bit_depth_aux_minus8, 0, 4);
386 flag(alpha_incr_flag);
388 bits = current->bit_depth_aux_minus8 + 9;
389 u(bits, alpha_opaque_value, 0, MAX_UINT_BITS(bits));
390 u(bits, alpha_transparent_value, 0, MAX_UINT_BITS(bits));
393 flag(additional_extension_flag);
395 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
400 static int FUNC(pps)(CodedBitstreamContext *ctx, RWContext *rw,
403 CodedBitstreamH264Context *h264 = ctx->priv_data;
404 const H264RawSPS *sps;
407 HEADER("Picture Parameter Set");
409 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
412 ue(pic_parameter_set_id, 0, 255);
413 ue(seq_parameter_set_id, 0, 31);
415 sps = h264->sps[current->seq_parameter_set_id];
417 av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
418 current->seq_parameter_set_id);
419 return AVERROR_INVALIDDATA;
422 flag(entropy_coding_mode_flag);
423 flag(bottom_field_pic_order_in_frame_present_flag);
425 ue(num_slice_groups_minus1, 0, 7);
426 if (current->num_slice_groups_minus1 > 0) {
427 unsigned int pic_size;
430 pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
431 (sps->pic_height_in_map_units_minus1 + 1);
433 ue(slice_group_map_type, 0, 6);
435 if (current->slice_group_map_type == 0) {
436 for (iGroup = 0; iGroup <= current->num_slice_groups_minus1; iGroup++)
437 ues(run_length_minus1[iGroup], 0, pic_size - 1, 1, iGroup);
439 } else if (current->slice_group_map_type == 2) {
440 for (iGroup = 0; iGroup < current->num_slice_groups_minus1; iGroup++) {
441 ues(top_left[iGroup], 0, pic_size - 1, 1, iGroup);
442 ues(bottom_right[iGroup],
443 current->top_left[iGroup], pic_size - 1, 1, iGroup);
445 } else if (current->slice_group_map_type == 3 ||
446 current->slice_group_map_type == 4 ||
447 current->slice_group_map_type == 5) {
448 flag(slice_group_change_direction_flag);
449 ue(slice_group_change_rate_minus1, 0, pic_size - 1);
450 } else if (current->slice_group_map_type == 6) {
451 ue(pic_size_in_map_units_minus1, pic_size - 1, pic_size - 1);
453 allocate(current->slice_group_id,
454 current->pic_size_in_map_units_minus1 + 1);
455 for (i = 0; i <= current->pic_size_in_map_units_minus1; i++)
456 us(av_log2(2 * current->num_slice_groups_minus1 + 1),
457 slice_group_id[i], 0, current->num_slice_groups_minus1, 1, i);
461 ue(num_ref_idx_l0_default_active_minus1, 0, 31);
462 ue(num_ref_idx_l1_default_active_minus1, 0, 31);
464 flag(weighted_pred_flag);
465 u(2, weighted_bipred_idc, 0, 2);
467 se(pic_init_qp_minus26, -26 - 6 * sps->bit_depth_luma_minus8, +25);
468 se(pic_init_qs_minus26, -26, +25);
469 se(chroma_qp_index_offset, -12, +12);
471 flag(deblocking_filter_control_present_flag);
472 flag(constrained_intra_pred_flag);
473 flag(redundant_pic_cnt_present_flag);
475 if (more_rbsp_data(current->more_rbsp_data))
477 flag(transform_8x8_mode_flag);
479 flag(pic_scaling_matrix_present_flag);
480 if (current->pic_scaling_matrix_present_flag) {
481 for (i = 0; i < 6 + (((sps->chroma_format_idc != 3) ? 2 : 6) *
482 current->transform_8x8_mode_flag); i++) {
483 flags(pic_scaling_list_present_flag[i], 1, i);
484 if (current->pic_scaling_list_present_flag[i]) {
486 CHECK(FUNC(scaling_list)(ctx, rw,
487 ¤t->scaling_list_4x4[i],
490 CHECK(FUNC(scaling_list)(ctx, rw,
491 ¤t->scaling_list_8x8[i - 6],
497 se(second_chroma_qp_index_offset, -12, +12);
499 infer(transform_8x8_mode_flag, 0);
500 infer(pic_scaling_matrix_present_flag, 0);
501 infer(second_chroma_qp_index_offset, current->chroma_qp_index_offset);
504 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
509 static int FUNC(sei_buffering_period)(CodedBitstreamContext *ctx, RWContext *rw,
510 H264RawSEIBufferingPeriod *current)
512 CodedBitstreamH264Context *h264 = ctx->priv_data;
513 const H264RawSPS *sps;
516 ue(seq_parameter_set_id, 0, 31);
518 sps = h264->sps[current->seq_parameter_set_id];
520 av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
521 current->seq_parameter_set_id);
522 return AVERROR_INVALIDDATA;
524 h264->active_sps = sps;
526 if (sps->vui.nal_hrd_parameters_present_flag) {
527 for (i = 0; i <= sps->vui.nal_hrd_parameters.cpb_cnt_minus1; i++) {
528 length = sps->vui.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
529 xu(length, initial_cpb_removal_delay[SchedSelIdx],
530 current->nal.initial_cpb_removal_delay[i],
531 1, MAX_UINT_BITS(length), 1, i);
532 xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
533 current->nal.initial_cpb_removal_delay_offset[i],
534 0, MAX_UINT_BITS(length), 1, i);
538 if (sps->vui.vcl_hrd_parameters_present_flag) {
539 for (i = 0; i <= sps->vui.vcl_hrd_parameters.cpb_cnt_minus1; i++) {
540 length = sps->vui.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1 + 1;
541 xu(length, initial_cpb_removal_delay[SchedSelIdx],
542 current->vcl.initial_cpb_removal_delay[i],
543 1, MAX_UINT_BITS(length), 1, i);
544 xu(length, initial_cpb_removal_delay_offset[SchedSelIdx],
545 current->vcl.initial_cpb_removal_delay_offset[i],
546 0, MAX_UINT_BITS(length), 1, i);
553 static int FUNC(sei_pic_timestamp)(CodedBitstreamContext *ctx, RWContext *rw,
554 H264RawSEIPicTimestamp *current)
556 CodedBitstreamH264Context *h264 = ctx->priv_data;
557 const H264RawSPS *sps;
558 uint8_t time_offset_length;
562 flag(nuit_field_based_flag);
563 u(5, counting_type, 0, 6);
564 flag(full_timestamp_flag);
565 flag(discontinuity_flag);
566 flag(cnt_dropped_flag);
567 u(8, n_frames, 0, 255);
568 if (current->full_timestamp_flag) {
569 u(6, seconds_value, 0, 59);
570 u(6, minutes_value, 0, 59);
571 u(5, hours_value, 0, 23);
574 if (current->seconds_flag) {
575 u(6, seconds_value, 0, 59);
577 if (current->minutes_flag) {
578 u(6, minutes_value, 0, 59);
580 if (current->hours_flag)
581 u(5, hours_value, 0, 23);
586 sps = h264->active_sps;
587 if (sps->vui.nal_hrd_parameters_present_flag)
588 time_offset_length = sps->vui.nal_hrd_parameters.time_offset_length;
589 else if (sps->vui.vcl_hrd_parameters_present_flag)
590 time_offset_length = sps->vui.vcl_hrd_parameters.time_offset_length;
592 time_offset_length = 24;
594 if (time_offset_length > 0)
595 u(time_offset_length, time_offset,
596 0, MAX_UINT_BITS(time_offset_length));
598 infer(time_offset, 0);
603 static int FUNC(sei_pic_timing)(CodedBitstreamContext *ctx, RWContext *rw,
604 H264RawSEIPicTiming *current)
606 CodedBitstreamH264Context *h264 = ctx->priv_data;
607 const H264RawSPS *sps;
610 sps = h264->active_sps;
612 // If there is exactly one possible SPS but it is not yet active
613 // then just assume that it should be the active one.
615 for (i = 0; i < H264_MAX_SPS_COUNT; i++) {
628 av_log(ctx->log_ctx, AV_LOG_ERROR,
629 "No active SPS for pic_timing.\n");
630 return AVERROR_INVALIDDATA;
633 if (sps->vui.nal_hrd_parameters_present_flag ||
634 sps->vui.vcl_hrd_parameters_present_flag) {
635 const H264RawHRD *hrd;
637 if (sps->vui.nal_hrd_parameters_present_flag)
638 hrd = &sps->vui.nal_hrd_parameters;
639 else if (sps->vui.vcl_hrd_parameters_present_flag)
640 hrd = &sps->vui.vcl_hrd_parameters;
642 av_log(ctx->log_ctx, AV_LOG_ERROR,
643 "No HRD parameters for pic_timing.\n");
644 return AVERROR_INVALIDDATA;
647 u(hrd->cpb_removal_delay_length_minus1 + 1, cpb_removal_delay,
648 0, MAX_UINT_BITS(hrd->cpb_removal_delay_length_minus1 + 1));
649 u(hrd->dpb_output_delay_length_minus1 + 1, dpb_output_delay,
650 0, MAX_UINT_BITS(hrd->dpb_output_delay_length_minus1 + 1));
653 if (sps->vui.pic_struct_present_flag) {
654 static const int num_clock_ts[9] = {
655 1, 1, 1, 2, 2, 3, 3, 2, 3
659 u(4, pic_struct, 0, 8);
660 if (current->pic_struct > 8)
661 return AVERROR_INVALIDDATA;
663 for (i = 0; i < num_clock_ts[current->pic_struct]; i++) {
664 flags(clock_timestamp_flag[i], 1, i);
665 if (current->clock_timestamp_flag[i])
666 CHECK(FUNC(sei_pic_timestamp)(ctx, rw, ¤t->timestamp[i]));
673 static int FUNC(sei_pan_scan_rect)(CodedBitstreamContext *ctx, RWContext *rw,
674 H264RawSEIPanScanRect *current)
678 ue(pan_scan_rect_id, 0, UINT32_MAX - 1);
679 flag(pan_scan_rect_cancel_flag);
681 if (!current->pan_scan_rect_cancel_flag) {
682 ue(pan_scan_cnt_minus1, 0, 2);
684 for (i = 0; i <= current->pan_scan_cnt_minus1; i++) {
685 ses(pan_scan_rect_left_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
686 ses(pan_scan_rect_right_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
687 ses(pan_scan_rect_top_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
688 ses(pan_scan_rect_bottom_offset[i], INT32_MIN + 1, INT32_MAX, 1, i);
691 ue(pan_scan_rect_repetition_period, 0, 16384);
697 static int FUNC(sei_user_data_registered)(CodedBitstreamContext *ctx, RWContext *rw,
698 H264RawSEIUserDataRegistered *current,
699 uint32_t *payload_size)
703 u(8, itu_t_t35_country_code, 0x00, 0xff);
704 if (current->itu_t_t35_country_code != 0xff)
707 u(8, itu_t_t35_country_code_extension_byte, 0x00, 0xff);
712 if (*payload_size < i) {
713 av_log(ctx->log_ctx, AV_LOG_ERROR,
714 "Invalid SEI user data registered payload.\n");
715 return AVERROR_INVALIDDATA;
717 current->data_length = *payload_size - i;
719 *payload_size = i + current->data_length;
722 allocate(current->data, current->data_length);
723 for (j = 0; j < current->data_length; j++)
724 xu(8, itu_t_t35_payload_byte[i], current->data[j], 0x00, 0xff, 1, i + j);
729 static int FUNC(sei_user_data_unregistered)(CodedBitstreamContext *ctx, RWContext *rw,
730 H264RawSEIUserDataUnregistered *current,
731 uint32_t *payload_size)
736 if (*payload_size < 16) {
737 av_log(ctx->log_ctx, AV_LOG_ERROR,
738 "Invalid SEI user data unregistered payload.\n");
739 return AVERROR_INVALIDDATA;
741 current->data_length = *payload_size - 16;
743 *payload_size = 16 + current->data_length;
746 for (i = 0; i < 16; i++)
747 us(8, uuid_iso_iec_11578[i], 0x00, 0xff, 1, i);
749 allocate(current->data, current->data_length);
751 for (i = 0; i < current->data_length; i++)
752 xu(8, user_data_payload_byte[i], current->data[i], 0x00, 0xff, 1, i);
757 static int FUNC(sei_recovery_point)(CodedBitstreamContext *ctx, RWContext *rw,
758 H264RawSEIRecoveryPoint *current)
762 ue(recovery_frame_cnt, 0, 65535);
763 flag(exact_match_flag);
764 flag(broken_link_flag);
765 u(2, changing_slice_group_idc, 0, 2);
770 static int FUNC(sei_display_orientation)(CodedBitstreamContext *ctx, RWContext *rw,
771 H264RawSEIDisplayOrientation *current)
775 flag(display_orientation_cancel_flag);
776 if (!current->display_orientation_cancel_flag) {
779 u(16, anticlockwise_rotation, 0, 65535);
780 ue(display_orientation_repetition_period, 0, 16384);
781 flag(display_orientation_extension_flag);
787 static int FUNC(sei_mastering_display_colour_volume)(CodedBitstreamContext *ctx, RWContext *rw,
788 H264RawSEIMasteringDisplayColourVolume *current)
792 for (c = 0; c < 3; c++) {
793 us(16, display_primaries_x[c], 0, 50000, 1, c);
794 us(16, display_primaries_y[c], 0, 50000, 1, c);
797 u(16, white_point_x, 0, 50000);
798 u(16, white_point_y, 0, 50000);
800 u(32, max_display_mastering_luminance, 1, MAX_UINT_BITS(32));
801 u(32, min_display_mastering_luminance, 0, current->max_display_mastering_luminance - 1);
806 static int FUNC(sei_payload)(CodedBitstreamContext *ctx, RWContext *rw,
807 H264RawSEIPayload *current)
810 int start_position, end_position;
813 start_position = get_bits_count(rw);
815 start_position = put_bits_count(rw);
818 switch (current->payload_type) {
819 case H264_SEI_TYPE_BUFFERING_PERIOD:
820 CHECK(FUNC(sei_buffering_period)
821 (ctx, rw, ¤t->payload.buffering_period));
823 case H264_SEI_TYPE_PIC_TIMING:
824 CHECK(FUNC(sei_pic_timing)
825 (ctx, rw, ¤t->payload.pic_timing));
827 case H264_SEI_TYPE_PAN_SCAN_RECT:
828 CHECK(FUNC(sei_pan_scan_rect)
829 (ctx, rw, ¤t->payload.pan_scan_rect));
831 case H264_SEI_TYPE_FILLER_PAYLOAD:
833 for (i = 0; i < current->payload_size; i++)
834 fixed(8, ff_byte, 0xff);
837 case H264_SEI_TYPE_USER_DATA_REGISTERED:
838 CHECK(FUNC(sei_user_data_registered)
839 (ctx, rw, ¤t->payload.user_data_registered, ¤t->payload_size));
841 case H264_SEI_TYPE_USER_DATA_UNREGISTERED:
842 CHECK(FUNC(sei_user_data_unregistered)
843 (ctx, rw, ¤t->payload.user_data_unregistered, ¤t->payload_size));
845 case H264_SEI_TYPE_RECOVERY_POINT:
846 CHECK(FUNC(sei_recovery_point)
847 (ctx, rw, ¤t->payload.recovery_point));
849 case H264_SEI_TYPE_DISPLAY_ORIENTATION:
850 CHECK(FUNC(sei_display_orientation)
851 (ctx, rw, ¤t->payload.display_orientation));
853 case H264_SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
854 CHECK(FUNC(sei_mastering_display_colour_volume)
855 (ctx, rw, ¤t->payload.mastering_display_colour_volume));
860 current->payload.other.data_length = current->payload_size;
862 allocate(current->payload.other.data, current->payload.other.data_length);
863 for (i = 0; i < current->payload.other.data_length; i++)
864 xu(8, payload_byte[i], current->payload.other.data[i], 0, 255, 1, i);
868 if (byte_alignment(rw)) {
869 fixed(1, bit_equal_to_one, 1);
870 while (byte_alignment(rw))
871 fixed(1, bit_equal_to_zero, 0);
875 end_position = get_bits_count(rw);
876 if (end_position < start_position + 8 * current->payload_size) {
877 av_log(ctx->log_ctx, AV_LOG_ERROR, "Incorrect SEI payload length: "
878 "header %"PRIu32" bits, actually %d bits.\n",
879 8 * current->payload_size,
880 end_position - start_position);
881 return AVERROR_INVALIDDATA;
884 end_position = put_bits_count(rw);
885 current->payload_size = (end_position - start_position) / 8;
891 static int FUNC(sei)(CodedBitstreamContext *ctx, RWContext *rw,
896 HEADER("Supplemental Enhancement Information");
898 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
902 for (k = 0; k < H264_MAX_SEI_PAYLOADS; k++) {
903 uint32_t payload_type = 0;
904 uint32_t payload_size = 0;
907 while (show_bits(rw, 8) == 0xff) {
908 fixed(8, ff_byte, 0xff);
911 xu(8, last_payload_type_byte, tmp, 0, 254, 0);
914 while (show_bits(rw, 8) == 0xff) {
915 fixed(8, ff_byte, 0xff);
918 xu(8, last_payload_size_byte, tmp, 0, 254, 0);
921 current->payload[k].payload_type = payload_type;
922 current->payload[k].payload_size = payload_size;
924 CHECK(FUNC(sei_payload)(ctx, rw, ¤t->payload[k]));
926 if (!cbs_h2645_read_more_rbsp_data(rw))
929 if (k >= H264_MAX_SEI_PAYLOADS) {
930 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many payloads in "
931 "SEI message: found %d.\n", k);
932 return AVERROR_INVALIDDATA;
934 current->payload_count = k + 1;
936 for (k = 0; k < current->payload_count; k++) {
937 PutBitContext start_state;
941 // Somewhat clumsy: we write the payload twice when
942 // we don't know the size in advance. This will mess
943 // with trace output, but is otherwise harmless.
945 need_size = !current->payload[k].payload_size;
946 for (i = 0; i < 1 + need_size; i++) {
949 tmp = current->payload[k].payload_type;
951 fixed(8, ff_byte, 0xff);
954 xu(8, last_payload_type_byte, tmp, 0, 254, 0);
956 tmp = current->payload[k].payload_size;
958 fixed(8, ff_byte, 0xff);
961 xu(8, last_payload_size_byte, tmp, 0, 254, 0);
963 CHECK(FUNC(sei_payload)(ctx, rw, ¤t->payload[k]));
968 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
973 static int FUNC(aud)(CodedBitstreamContext *ctx, RWContext *rw,
978 HEADER("Access Unit Delimiter");
980 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
983 u(3, primary_pic_type, 0, 7);
985 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));
990 static int FUNC(ref_pic_list_modification)(CodedBitstreamContext *ctx, RWContext *rw,
991 H264RawSliceHeader *current)
993 CodedBitstreamH264Context *h264 = ctx->priv_data;
994 const H264RawSPS *sps = h264->active_sps;
997 if (current->slice_type % 5 != 2 &&
998 current->slice_type % 5 != 4) {
999 flag(ref_pic_list_modification_flag_l0);
1000 if (current->ref_pic_list_modification_flag_l0) {
1001 for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
1002 xue(modification_of_pic_nums_idc,
1003 current->rplm_l0[i].modification_of_pic_nums_idc, 0, 3, 0);
1005 mopn = current->rplm_l0[i].modification_of_pic_nums_idc;
1009 if (mopn == 0 || mopn == 1)
1010 xue(abs_diff_pic_num_minus1,
1011 current->rplm_l0[i].abs_diff_pic_num_minus1,
1012 0, (1 + current->field_pic_flag) *
1013 (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
1015 xue(long_term_pic_num,
1016 current->rplm_l0[i].long_term_pic_num,
1017 0, sps->max_num_ref_frames - 1, 0);
1022 if (current->slice_type % 5 == 1) {
1023 flag(ref_pic_list_modification_flag_l1);
1024 if (current->ref_pic_list_modification_flag_l1) {
1025 for (i = 0; i < H264_MAX_RPLM_COUNT; i++) {
1026 xue(modification_of_pic_nums_idc,
1027 current->rplm_l1[i].modification_of_pic_nums_idc, 0, 3, 0);
1029 mopn = current->rplm_l1[i].modification_of_pic_nums_idc;
1033 if (mopn == 0 || mopn == 1)
1034 xue(abs_diff_pic_num_minus1,
1035 current->rplm_l1[i].abs_diff_pic_num_minus1,
1036 0, (1 + current->field_pic_flag) *
1037 (1 << (sps->log2_max_frame_num_minus4 + 4)), 0);
1039 xue(long_term_pic_num,
1040 current->rplm_l1[i].long_term_pic_num,
1041 0, sps->max_num_ref_frames - 1, 0);
1049 static int FUNC(pred_weight_table)(CodedBitstreamContext *ctx, RWContext *rw,
1050 H264RawSliceHeader *current)
1052 CodedBitstreamH264Context *h264 = ctx->priv_data;
1053 const H264RawSPS *sps = h264->active_sps;
1057 ue(luma_log2_weight_denom, 0, 7);
1059 chroma = !sps->separate_colour_plane_flag && sps->chroma_format_idc != 0;
1061 ue(chroma_log2_weight_denom, 0, 7);
1063 for (i = 0; i <= current->num_ref_idx_l0_active_minus1; i++) {
1064 flags(luma_weight_l0_flag[i], 1, i);
1065 if (current->luma_weight_l0_flag[i]) {
1066 ses(luma_weight_l0[i], -128, +127, 1, i);
1067 ses(luma_offset_l0[i], -128, +127, 1, i);
1070 flags(chroma_weight_l0_flag[i], 1, i);
1071 if (current->chroma_weight_l0_flag[i]) {
1072 for (j = 0; j < 2; j++) {
1073 ses(chroma_weight_l0[i][j], -128, +127, 2, i, j);
1074 ses(chroma_offset_l0[i][j], -128, +127, 2, i, j);
1080 if (current->slice_type % 5 == 1) {
1081 for (i = 0; i <= current->num_ref_idx_l1_active_minus1; i++) {
1082 flags(luma_weight_l1_flag[i], 1, i);
1083 if (current->luma_weight_l1_flag[i]) {
1084 ses(luma_weight_l1[i], -128, +127, 1, i);
1085 ses(luma_offset_l1[i], -128, +127, 1, i);
1088 flags(chroma_weight_l1_flag[i], 1, i);
1089 if (current->chroma_weight_l1_flag[i]) {
1090 for (j = 0; j < 2; j++) {
1091 ses(chroma_weight_l1[i][j], -128, +127, 2, i, j);
1092 ses(chroma_offset_l1[i][j], -128, +127, 2, i, j);
1102 static int FUNC(dec_ref_pic_marking)(CodedBitstreamContext *ctx, RWContext *rw,
1103 H264RawSliceHeader *current, int idr_pic_flag)
1105 CodedBitstreamH264Context *h264 = ctx->priv_data;
1106 const H264RawSPS *sps = h264->active_sps;
1111 flag(no_output_of_prior_pics_flag);
1112 flag(long_term_reference_flag);
1114 flag(adaptive_ref_pic_marking_mode_flag);
1115 if (current->adaptive_ref_pic_marking_mode_flag) {
1116 for (i = 0; i < H264_MAX_MMCO_COUNT; i++) {
1117 xue(memory_management_control_operation,
1118 current->mmco[i].memory_management_control_operation,
1121 mmco = current->mmco[i].memory_management_control_operation;
1125 if (mmco == 1 || mmco == 3)
1126 xue(difference_of_pic_nums_minus1,
1127 current->mmco[i].difference_of_pic_nums_minus1,
1130 xue(long_term_pic_num,
1131 current->mmco[i].long_term_pic_num,
1132 0, sps->max_num_ref_frames - 1, 0);
1133 if (mmco == 3 || mmco == 6)
1134 xue(long_term_frame_idx,
1135 current->mmco[i].long_term_frame_idx,
1136 0, sps->max_num_ref_frames - 1, 0);
1138 xue(max_long_term_frame_idx_plus1,
1139 current->mmco[i].max_long_term_frame_idx_plus1,
1140 0, sps->max_num_ref_frames, 0);
1142 if (i == H264_MAX_MMCO_COUNT) {
1143 av_log(ctx->log_ctx, AV_LOG_ERROR, "Too many "
1144 "memory management control operations.\n");
1145 return AVERROR_INVALIDDATA;
1153 static int FUNC(slice_header)(CodedBitstreamContext *ctx, RWContext *rw,
1154 H264RawSliceHeader *current)
1156 CodedBitstreamH264Context *h264 = ctx->priv_data;
1157 const H264RawSPS *sps;
1158 const H264RawPPS *pps;
1161 int slice_type_i, slice_type_p, slice_type_b;
1162 int slice_type_si, slice_type_sp;
1164 HEADER("Slice Header");
1166 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
1167 1 << H264_NAL_SLICE |
1168 1 << H264_NAL_IDR_SLICE |
1169 1 << H264_NAL_AUXILIARY_SLICE));
1171 if (current->nal_unit_header.nal_unit_type == H264_NAL_AUXILIARY_SLICE) {
1172 if (!h264->last_slice_nal_unit_type) {
1173 av_log(ctx->log_ctx, AV_LOG_ERROR, "Auxiliary slice "
1174 "is not decodable without the main picture "
1175 "in the same access unit.\n");
1176 return AVERROR_INVALIDDATA;
1179 h264->last_slice_nal_unit_type =
1180 current->nal_unit_header.nal_unit_type;
1182 idr_pic_flag = h264->last_slice_nal_unit_type == H264_NAL_IDR_SLICE;
1184 ue(first_mb_in_slice, 0, H264_MAX_MB_PIC_SIZE - 1);
1185 ue(slice_type, 0, 9);
1187 slice_type_i = current->slice_type % 5 == 2;
1188 slice_type_p = current->slice_type % 5 == 0;
1189 slice_type_b = current->slice_type % 5 == 1;
1190 slice_type_si = current->slice_type % 5 == 4;
1191 slice_type_sp = current->slice_type % 5 == 3;
1193 if (idr_pic_flag && !(slice_type_i || slice_type_si)) {
1194 av_log(ctx->log_ctx, AV_LOG_ERROR, "Invalid slice type %d "
1195 "for IDR picture.\n", current->slice_type);
1196 return AVERROR_INVALIDDATA;
1199 ue(pic_parameter_set_id, 0, 255);
1201 pps = h264->pps[current->pic_parameter_set_id];
1203 av_log(ctx->log_ctx, AV_LOG_ERROR, "PPS id %d not available.\n",
1204 current->pic_parameter_set_id);
1205 return AVERROR_INVALIDDATA;
1207 h264->active_pps = pps;
1209 sps = h264->sps[pps->seq_parameter_set_id];
1211 av_log(ctx->log_ctx, AV_LOG_ERROR, "SPS id %d not available.\n",
1212 pps->seq_parameter_set_id);
1213 return AVERROR_INVALIDDATA;
1215 h264->active_sps = sps;
1217 if (sps->separate_colour_plane_flag)
1218 u(2, colour_plane_id, 0, 2);
1220 u(sps->log2_max_frame_num_minus4 + 4, frame_num,
1221 0, MAX_UINT_BITS(sps->log2_max_frame_num_minus4 + 4));
1223 if (!sps->frame_mbs_only_flag) {
1224 flag(field_pic_flag);
1225 if (current->field_pic_flag)
1226 flag(bottom_field_flag);
1228 infer(bottom_field_flag, 0);
1230 infer(field_pic_flag, 0);
1231 infer(bottom_field_flag, 0);
1235 ue(idr_pic_id, 0, 65535);
1237 if (sps->pic_order_cnt_type == 0) {
1238 u(sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_order_cnt_lsb,
1239 0, MAX_UINT_BITS(sps->log2_max_pic_order_cnt_lsb_minus4 + 4));
1240 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1241 !current->field_pic_flag)
1242 se(delta_pic_order_cnt_bottom, INT32_MIN + 1, INT32_MAX);
1244 } else if (sps->pic_order_cnt_type == 1) {
1245 if (!sps->delta_pic_order_always_zero_flag) {
1246 se(delta_pic_order_cnt[0], INT32_MIN + 1, INT32_MAX);
1247 if (pps->bottom_field_pic_order_in_frame_present_flag &&
1248 !current->field_pic_flag)
1249 se(delta_pic_order_cnt[1], INT32_MIN + 1, INT32_MAX);
1251 infer(delta_pic_order_cnt[1], 0);
1253 infer(delta_pic_order_cnt[0], 0);
1254 infer(delta_pic_order_cnt[1], 0);
1258 if (pps->redundant_pic_cnt_present_flag)
1259 ue(redundant_pic_cnt, 0, 127);
1262 flag(direct_spatial_mv_pred_flag);
1264 if (slice_type_p || slice_type_sp || slice_type_b) {
1265 flag(num_ref_idx_active_override_flag);
1266 if (current->num_ref_idx_active_override_flag) {
1267 ue(num_ref_idx_l0_active_minus1, 0, 31);
1269 ue(num_ref_idx_l1_active_minus1, 0, 31);
1271 infer(num_ref_idx_l0_active_minus1,
1272 pps->num_ref_idx_l0_default_active_minus1);
1273 infer(num_ref_idx_l1_active_minus1,
1274 pps->num_ref_idx_l1_default_active_minus1);
1278 if (current->nal_unit_header.nal_unit_type == 20 ||
1279 current->nal_unit_header.nal_unit_type == 21) {
1280 av_log(ctx->log_ctx, AV_LOG_ERROR, "MVC / 3DAVC not supported.\n");
1281 return AVERROR_PATCHWELCOME;
1283 CHECK(FUNC(ref_pic_list_modification)(ctx, rw, current));
1286 if ((pps->weighted_pred_flag && (slice_type_p || slice_type_sp)) ||
1287 (pps->weighted_bipred_idc == 1 && slice_type_b)) {
1288 CHECK(FUNC(pred_weight_table)(ctx, rw, current));
1291 if (current->nal_unit_header.nal_ref_idc != 0) {
1292 CHECK(FUNC(dec_ref_pic_marking)(ctx, rw, current, idr_pic_flag));
1295 if (pps->entropy_coding_mode_flag &&
1296 !slice_type_i && !slice_type_si) {
1297 ue(cabac_init_idc, 0, 2);
1300 se(slice_qp_delta, - 51 - 6 * sps->bit_depth_luma_minus8,
1301 + 51 + 6 * sps->bit_depth_luma_minus8);
1302 if (slice_type_sp || slice_type_si) {
1304 flag(sp_for_switch_flag);
1305 se(slice_qs_delta, -51, +51);
1308 if (pps->deblocking_filter_control_present_flag) {
1309 ue(disable_deblocking_filter_idc, 0, 2);
1310 if (current->disable_deblocking_filter_idc != 1) {
1311 se(slice_alpha_c0_offset_div2, -6, +6);
1312 se(slice_beta_offset_div2, -6, +6);
1314 infer(slice_alpha_c0_offset_div2, 0);
1315 infer(slice_beta_offset_div2, 0);
1318 infer(disable_deblocking_filter_idc, 0);
1319 infer(slice_alpha_c0_offset_div2, 0);
1320 infer(slice_beta_offset_div2, 0);
1323 if (pps->num_slice_groups_minus1 > 0 &&
1324 pps->slice_group_map_type >= 3 &&
1325 pps->slice_group_map_type <= 5) {
1326 unsigned int pic_size, max, bits;
1328 pic_size = (sps->pic_width_in_mbs_minus1 + 1) *
1329 (sps->pic_height_in_map_units_minus1 + 1);
1330 max = (pic_size + pps->slice_group_change_rate_minus1) /
1331 (pps->slice_group_change_rate_minus1 + 1);
1332 bits = av_log2(2 * max - 1);
1334 u(bits, slice_group_change_cycle, 0, max);
1337 if (pps->entropy_coding_mode_flag) {
1338 while (byte_alignment(rw))
1339 fixed(1, cabac_alignment_one_bit, 1);
1345 static int FUNC(filler)(CodedBitstreamContext *ctx, RWContext *rw,
1346 H264RawFiller *current)
1350 HEADER("Filler Data");
1352 CHECK(FUNC(nal_unit_header)(ctx, rw, ¤t->nal_unit_header,
1353 1 << H264_NAL_FILLER_DATA));
1356 while (show_bits(rw, 8) == 0xff) {
1357 fixed(8, ff_byte, 0xff);
1358 ++current->filler_size;
1363 for (i = 0; i < current->filler_size; i++)
1364 fixed(8, ff_byte, 0xff);
1368 CHECK(FUNC(rbsp_trailing_bits)(ctx, rw));