2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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
)
22 av_unused
int one
= 1, zero
= 0;
23 xu(1, rbsp_stop_one_bit
, one
, 1, 1);
24 while (byte_alignment(rw
) != 0)
25 xu(1, rbsp_alignment_zero_bit
, zero
, 0, 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 xse(delta_scale
, current
->delta_scale
[i
], -128, +127);
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 ue(bit_rate_value_minus1
[i
], 0, UINT32_MAX
- 1);
99 ue(cpb_size_value_minus1
[i
], 0, UINT32_MAX
- 1);
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 ue(log2_max_mv_length_horizontal
, 0, 16);
189 ue(log2_max_mv_length_vertical
, 0, 16);
190 ue(max_num_reorder_frames
, 0, H264_MAX_DPB_FRAMES
);
191 ue(max_dec_frame_buffering
, 0, H264_MAX_DPB_FRAMES
);
193 infer(motion_vectors_over_pic_boundaries_flag
, 1);
194 infer(max_bytes_per_pic_denom
, 2);
195 infer(max_bits_per_mb_denom
, 1);
196 infer(log2_max_mv_length_horizontal
, 16);
197 infer(log2_max_mv_length_vertical
, 16);
199 if ((sps
->profile_idc
== 44 || sps
->profile_idc
== 86 ||
200 sps
->profile_idc
== 110 || sps
->profile_idc
== 110 ||
201 sps
->profile_idc
== 122 || sps
->profile_idc
== 244) &&
202 sps
->constraint_set3_flag
) {
203 infer(max_num_reorder_frames
, 0);
204 infer(max_dec_frame_buffering
, 0);
206 infer(max_num_reorder_frames
, H264_MAX_DPB_FRAMES
);
207 infer(max_dec_frame_buffering
, H264_MAX_DPB_FRAMES
);
214 static int FUNC(sps
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
219 HEADER("Sequence Parameter Set");
221 CHECK(FUNC(nal_unit_header
)(ctx
, rw
, ¤t
->nal_unit_header
,
224 u(8, profile_idc
, 0, 255);
226 flag(constraint_set0_flag
);
227 flag(constraint_set1_flag
);
228 flag(constraint_set2_flag
);
229 flag(constraint_set3_flag
);
230 flag(constraint_set4_flag
);
231 flag(constraint_set5_flag
);
233 u(2, reserved_zero_2bits
, 0, 0);
235 u(8, level_idc
, 0, 255);
237 ue(seq_parameter_set_id
, 0, 31);
239 if (current
->profile_idc
== 100 || current
->profile_idc
== 110 ||
240 current
->profile_idc
== 122 || current
->profile_idc
== 244 ||
241 current
->profile_idc
== 44 || current
->profile_idc
== 83 ||
242 current
->profile_idc
== 86 || current
->profile_idc
== 118 ||
243 current
->profile_idc
== 128 || current
->profile_idc
== 138) {
244 ue(chroma_format_idc
, 0, 3);
246 if (current
->chroma_format_idc
== 3)
247 flag(separate_colour_plane_flag
);
249 infer(separate_colour_plane_flag
, 0);
251 ue(bit_depth_luma_minus8
, 0, 6);
252 ue(bit_depth_chroma_minus8
, 0, 6);
254 flag(qpprime_y_zero_transform_bypass_flag
);
256 flag(seq_scaling_matrix_present_flag
);
257 if (current
->seq_scaling_matrix_present_flag
) {
258 for (i
= 0; i
< ((current
->chroma_format_idc
!= 3) ?
8 : 12); i
++) {
259 flag(seq_scaling_list_present_flag
[i
]);
260 if (current
->seq_scaling_list_present_flag
[i
]) {
262 CHECK(FUNC(scaling_list
)(ctx
, rw
,
263 ¤t
->scaling_list_4x4
[i
],
266 CHECK(FUNC(scaling_list
)(ctx
, rw
,
267 ¤t
->scaling_list_8x8
[i
- 6],
273 infer(chroma_format_idc
, current
->profile_idc
== 183 ?
0 : 1);
275 infer(separate_colour_plane_flag
, 0);
276 infer(bit_depth_luma_minus8
, 0);
277 infer(bit_depth_chroma_minus8
, 0);
280 ue(log2_max_frame_num_minus4
, 0, 12);
281 ue(pic_order_cnt_type
, 0, 2);
283 if (current
->pic_order_cnt_type
== 0) {
284 ue(log2_max_pic_order_cnt_lsb_minus4
, 0, 12);
285 } else if (current
->pic_order_cnt_type
== 1) {
286 flag(delta_pic_order_always_zero_flag
);
287 se(offset_for_non_ref_pic
, INT32_MIN
+ 1, INT32_MAX
);
288 se(offset_for_top_to_bottom_field
, INT32_MIN
+ 1, INT32_MAX
);
289 ue(num_ref_frames_in_pic_order_cnt_cycle
, 0, 255);
291 for (i
= 0; i
< current
->num_ref_frames_in_pic_order_cnt_cycle
; i
++)
292 se(offset_for_ref_frame
[i
], INT32_MIN
+ 1, INT32_MAX
);
295 ue(max_num_ref_frames
, 0, H264_MAX_DPB_FRAMES
);
296 flag(gaps_in_frame_num_allowed_flag
);
298 ue(pic_width_in_mbs_minus1
, 0, H264_MAX_MB_WIDTH
);
299 ue(pic_height_in_map_units_minus1
, 0, H264_MAX_MB_HEIGHT
);
301 flag(frame_mbs_only_flag
);
302 if (!current
->frame_mbs_only_flag
)
303 flag(mb_adaptive_frame_field_flag
);
305 flag(direct_8x8_inference_flag
);
307 flag(frame_cropping_flag
);
308 if (current
->frame_cropping_flag
) {
309 ue(frame_crop_left_offset
, 0, H264_MAX_WIDTH
);
310 ue(frame_crop_right_offset
, 0, H264_MAX_WIDTH
);
311 ue(frame_crop_top_offset
, 0, H264_MAX_HEIGHT
);
312 ue(frame_crop_bottom_offset
, 0, H264_MAX_HEIGHT
);
315 flag(vui_parameters_present_flag
);
316 if (current
->vui_parameters_present_flag
)
317 CHECK(FUNC(vui_parameters
)(ctx
, rw
, ¤t
->vui
, current
));
319 CHECK(FUNC(rbsp_trailing_bits
)(ctx
, rw
));
324 static int FUNC(sps_extension
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
325 H264RawSPSExtension
*current
)
329 HEADER("Sequence Parameter Set Extension");
331 CHECK(FUNC(nal_unit_header
)(ctx
, rw
, ¤t
->nal_unit_header
,
332 1 << H264_NAL_SPS_EXT
));
334 ue(seq_parameter_set_id
, 0, 31);
336 ue(aux_format_idc
, 0, 3);
338 if (current
->aux_format_idc
!= 0) {
341 ue(bit_depth_aux_minus8
, 0, 4);
342 flag(alpha_incr_flag
);
344 bits
= current
->bit_depth_aux_minus8
+ 9;
345 u(bits
, alpha_opaque_value
, 0, (1 << bits
) - 1);
346 u(bits
, alpha_transparent_value
, 0, (1 << bits
) - 1);
349 flag(additional_extension_flag
);
351 CHECK(FUNC(rbsp_trailing_bits
)(ctx
, rw
));
356 static int FUNC(pps
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
359 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
360 const H264RawSPS
*sps
;
363 HEADER("Picture Parameter Set");
365 CHECK(FUNC(nal_unit_header
)(ctx
, rw
, ¤t
->nal_unit_header
,
368 ue(pic_parameter_set_id
, 0, 255);
369 ue(seq_parameter_set_id
, 0, 31);
371 sps
= h264
->sps
[current
->seq_parameter_set_id
];
373 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "SPS id %d not available.\n",
374 current
->seq_parameter_set_id
);
375 return AVERROR_INVALIDDATA
;
378 flag(entropy_coding_mode_flag
);
379 flag(bottom_field_pic_order_in_frame_present_flag
);
381 ue(num_slice_groups_minus1
, 0, 7);
382 if (current
->num_slice_groups_minus1
> 0) {
383 unsigned int pic_size
;
386 pic_size
= (sps
->pic_width_in_mbs_minus1
+ 1) *
387 (sps
->pic_height_in_map_units_minus1
+ 1);
389 ue(slice_group_map_type
, 0, 6);
391 if (current
->slice_group_map_type
== 0) {
392 for (iGroup
= 0; iGroup
<= current
->num_slice_groups_minus1
; iGroup
++)
393 ue(run_length_minus1
[iGroup
], 0, pic_size
- 1);
395 } else if (current
->slice_group_map_type
== 2) {
396 for (iGroup
= 0; iGroup
< current
->num_slice_groups_minus1
; iGroup
++) {
397 ue(top_left
[iGroup
], 0, pic_size
- 1);
398 ue(bottom_right
[iGroup
], current
->top_left
[iGroup
], pic_size
- 1);
400 } else if (current
->slice_group_map_type
== 3 ||
401 current
->slice_group_map_type
== 4 ||
402 current
->slice_group_map_type
== 5) {
403 flag(slice_group_change_direction_flag
);
404 ue(slice_group_change_rate_minus1
, 0, pic_size
- 1);
405 } else if (current
->slice_group_map_type
== 6) {
406 ue(pic_size_in_map_units_minus1
, pic_size
- 1, pic_size
- 1);
407 for (i
= 0; i
<= current
->pic_size_in_map_units_minus1
; i
++)
408 u(av_log2(2 * current
->num_slice_groups_minus1
+ 1),
409 slice_group_id
[i
], 0, current
->num_slice_groups_minus1
);
413 ue(num_ref_idx_l0_default_active_minus1
, 0, 31);
414 ue(num_ref_idx_l1_default_active_minus1
, 0, 31);
416 flag(weighted_pred_flag
);
417 u(2, weighted_bipred_idc
, 0, 2);
419 se(pic_init_qp_minus26
, -26 - 6 * sps
->bit_depth_luma_minus8
, +25);
420 se(pic_init_qs_minus26
, -26, +25);
421 se(chroma_qp_index_offset
, -12, +12);
423 flag(deblocking_filter_control_present_flag
);
424 flag(constrained_intra_pred_flag
);
425 flag(redundant_pic_cnt_present_flag
);
427 if (more_rbsp_data(current
->more_rbsp_data
))
429 flag(transform_8x8_mode_flag
);
431 flag(pic_scaling_matrix_present_flag
);
432 if (current
->pic_scaling_matrix_present_flag
) {
433 for (i
= 0; i
< 6 + (((sps
->chroma_format_idc
!= 3) ?
2 : 6) *
434 current
->transform_8x8_mode_flag
); i
++) {
435 flag(pic_scaling_list_present_flag
[i
]);
436 if (current
->pic_scaling_list_present_flag
[i
]) {
438 CHECK(FUNC(scaling_list
)(ctx
, rw
,
439 ¤t
->scaling_list_4x4
[i
],
442 CHECK(FUNC(scaling_list
)(ctx
, rw
,
443 ¤t
->scaling_list_8x8
[i
- 6],
449 se(second_chroma_qp_index_offset
, -12, +12);
451 infer(transform_8x8_mode_flag
, 0);
452 infer(pic_scaling_matrix_present_flag
, 0);
453 infer(second_chroma_qp_index_offset
, current
->chroma_qp_index_offset
);
456 CHECK(FUNC(rbsp_trailing_bits
)(ctx
, rw
));
461 static int FUNC(sei_buffering_period
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
462 H264RawSEIBufferingPeriod
*current
)
464 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
465 const H264RawSPS
*sps
;
468 ue(seq_parameter_set_id
, 0, 31);
470 sps
= h264
->sps
[current
->seq_parameter_set_id
];
472 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "SPS id %d not available.\n",
473 current
->seq_parameter_set_id
);
474 return AVERROR_INVALIDDATA
;
476 h264
->active_sps
= sps
;
478 if (sps
->vui
.nal_hrd_parameters_present_flag
) {
479 for (i
= 0; i
<= sps
->vui
.nal_hrd_parameters
.cpb_cnt_minus1
; i
++) {
480 length
= sps
->vui
.nal_hrd_parameters
.initial_cpb_removal_delay_length_minus1
+ 1;
481 xu(length
, initial_cpb_removal_delay
[SchedSelIdx
],
482 current
->nal
.initial_cpb_removal_delay
[i
],
483 0, (1 << (uint64_t)length
) - 1);
484 xu(length
, initial_cpb_removal_delay_offset
[SchedSelIdx
],
485 current
->nal
.initial_cpb_removal_delay_offset
[i
],
486 0, (1 << (uint64_t)length
) - 1);
490 if (sps
->vui
.vcl_hrd_parameters_present_flag
) {
491 for (i
= 0; i
<= sps
->vui
.vcl_hrd_parameters
.cpb_cnt_minus1
; i
++) {
492 length
= sps
->vui
.vcl_hrd_parameters
.initial_cpb_removal_delay_length_minus1
+ 1;
493 xu(length
, initial_cpb_removal_delay
[SchedSelIdx
],
494 current
->vcl
.initial_cpb_removal_delay
[i
],
495 0, (1 << (uint64_t)length
) - 1);
496 xu(length
, initial_cpb_removal_delay_offset
[SchedSelIdx
],
497 current
->vcl
.initial_cpb_removal_delay_offset
[i
],
498 0, (1 << (uint64_t)length
) - 1);
505 static int FUNC(sei_pic_timestamp
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
506 H264RawSEIPicTimestamp
*current
)
508 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
509 const H264RawSPS
*sps
;
510 uint8_t time_offset_length
;
514 flag(nuit_field_based_flag
);
515 u(5, counting_type
, 0, 6);
516 flag(full_timestamp_flag
);
517 flag(discontinuity_flag
);
518 flag(cnt_dropped_flag
);
519 u(8, n_frames
, 0, 255);
520 if (current
->full_timestamp_flag
) {
521 u(6, seconds_value
, 0, 59);
522 u(6, minutes_value
, 0, 59);
523 u(5, hours_value
, 0, 23);
526 if (current
->seconds_flag
) {
527 u(6, seconds_value
, 0, 59);
529 if (current
->minutes_flag
) {
530 u(6, minutes_value
, 0, 59);
532 if (current
->hours_flag
)
533 u(5, hours_value
, 0, 23);
538 sps
= h264
->active_sps
;
539 if (sps
->vui
.nal_hrd_parameters_present_flag
)
540 time_offset_length
= sps
->vui
.nal_hrd_parameters
.time_offset_length
;
541 else if (sps
->vui
.vcl_hrd_parameters_present_flag
)
542 time_offset_length
= sps
->vui
.vcl_hrd_parameters
.time_offset_length
;
544 time_offset_length
= 24;
546 if (time_offset_length
> 0)
547 u(time_offset_length
, time_offset
,
548 0, (1 << (uint64_t)time_offset_length
) - 1);
550 infer(time_offset
, 0);
555 static int FUNC(sei_pic_timing
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
556 H264RawSEIPicTiming
*current
)
558 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
559 const H264RawSPS
*sps
;
562 sps
= h264
->active_sps
;
564 av_log(ctx
->log_ctx
, AV_LOG_ERROR
,
565 "No active SPS for pic_timing.\n");
566 return AVERROR_INVALIDDATA
;
569 if (sps
->vui
.nal_hrd_parameters_present_flag
||
570 sps
->vui
.vcl_hrd_parameters_present_flag
) {
571 const H264RawHRD
*hrd
;
573 if (sps
->vui
.nal_hrd_parameters_present_flag
)
574 hrd
= &sps
->vui
.nal_hrd_parameters
;
575 else if (sps
->vui
.vcl_hrd_parameters_present_flag
)
576 hrd
= &sps
->vui
.vcl_hrd_parameters
;
578 av_log(ctx
->log_ctx
, AV_LOG_ERROR
,
579 "No HRD parameters for pic_timing.\n");
580 return AVERROR_INVALIDDATA
;
583 u(hrd
->cpb_removal_delay_length_minus1
+ 1, cpb_removal_delay
,
584 0, (1 << (uint64_t)hrd
->cpb_removal_delay_length_minus1
) + 1);
585 u(hrd
->dpb_output_delay_length_minus1
+ 1, dpb_output_delay
,
586 0, (1 << (uint64_t)hrd
->dpb_output_delay_length_minus1
) + 1);
589 if (sps
->vui
.pic_struct_present_flag
) {
590 static const int num_clock_ts
[9] = {
591 1, 1, 1, 2, 2, 3, 3, 2, 3
595 u(4, pic_struct
, 0, 8);
596 if (current
->pic_struct
> 8)
597 return AVERROR_INVALIDDATA
;
599 for (i
= 0; i
< num_clock_ts
[current
->pic_struct
]; i
++) {
600 flag(clock_timestamp_flag
[i
]);
601 if (current
->clock_timestamp_flag
[i
])
602 CHECK(FUNC(sei_pic_timestamp
)(ctx
, rw
, ¤t
->timestamp
[i
]));
609 static int FUNC(sei_user_data_registered
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
610 H264RawSEIUserDataRegistered
*current
,
611 uint32_t *payload_size
)
615 u(8, itu_t_t35_country_code
, 0x00, 0xff);
616 if (current
->itu_t_t35_country_code
!= 0xff)
619 u(8, itu_t_t35_country_code_extension_byte
, 0x00, 0xff);
624 if (*payload_size
< i
) {
625 av_log(ctx
->log_ctx
, AV_LOG_ERROR
,
626 "Invalid SEI user data registered payload.\n");
627 return AVERROR_INVALIDDATA
;
629 current
->data_length
= *payload_size
- i
;
631 *payload_size
= i
+ current
->data_length
;
634 allocate(current
->data
, current
->data_length
);
635 for (j
= 0; j
< current
->data_length
; j
++)
636 xu(8, itu_t_t35_payload_byte
, current
->data
[j
], 0x00, 0xff);
641 static int FUNC(sei_user_data_unregistered
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
642 H264RawSEIUserDataUnregistered
*current
,
643 uint32_t *payload_size
)
648 if (*payload_size
< 16) {
649 av_log(ctx
->log_ctx
, AV_LOG_ERROR
,
650 "Invalid SEI user data unregistered payload.\n");
651 return AVERROR_INVALIDDATA
;
653 current
->data_length
= *payload_size
- 16;
655 *payload_size
= 16 + current
->data_length
;
658 for (i
= 0; i
< 16; i
++) {
659 xu(8, uuid_iso_iec_11578
,
660 current
->uuid_iso_iec_11578
[i
], 0x00, 0xff);
663 allocate(current
->data
, current
->data_length
);
665 for (i
= 0; i
< current
->data_length
; i
++)
666 xu(8, user_data_payload_byte
, current
->data
[i
], 0x00, 0xff);
671 static int FUNC(sei_recovery_point
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
672 H264RawSEIRecoveryPoint
*current
)
676 ue(recovery_frame_cnt
, 0, 65535);
677 flag(exact_match_flag
);
678 flag(broken_link_flag
);
679 u(2, changing_slice_group_idc
, 0, 2);
684 static int FUNC(sei_display_orientation
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
685 H264RawSEIDisplayOrientation
*current
)
689 flag(display_orientation_cancel_flag
);
690 if (!current
->display_orientation_cancel_flag
) {
693 u(16, anticlockwise_rotation
, 0, 65535);
694 ue(display_orientation_repetition_period
, 0, 16384);
695 flag(display_orientation_extension_flag
);
701 static int FUNC(sei_payload
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
702 H264RawSEIPayload
*current
)
705 int start_position
, end_position
;
708 start_position
= bitstream_tell(rw
);
710 start_position
= put_bits_count(rw
);
713 switch (current
->payload_type
) {
714 case H264_SEI_TYPE_BUFFERING_PERIOD
:
715 CHECK(FUNC(sei_buffering_period
)
716 (ctx
, rw
, ¤t
->payload
.buffering_period
));
718 case H264_SEI_TYPE_PIC_TIMING
:
719 CHECK(FUNC(sei_pic_timing
)
720 (ctx
, rw
, ¤t
->payload
.pic_timing
));
722 case H264_SEI_TYPE_FILLER_PAYLOAD
:
724 av_unused
int ff_byte
= 0xff;
725 for (i
= 0; i
< current
->payload_size
; i
++)
726 xu(8, ff_byte
, ff_byte
, 0xff, 0xff);
729 case H264_SEI_TYPE_USER_DATA_REGISTERED
:
730 CHECK(FUNC(sei_user_data_registered
)
731 (ctx
, rw
, ¤t
->payload
.user_data_registered
, ¤t
->payload_size
));
733 case H264_SEI_TYPE_USER_DATA_UNREGISTERED
:
734 CHECK(FUNC(sei_user_data_unregistered
)
735 (ctx
, rw
, ¤t
->payload
.user_data_unregistered
, ¤t
->payload_size
));
737 case H264_SEI_TYPE_RECOVERY_POINT
:
738 CHECK(FUNC(sei_recovery_point
)
739 (ctx
, rw
, ¤t
->payload
.recovery_point
));
741 case H264_SEI_TYPE_DISPLAY_ORIENTATION
:
742 CHECK(FUNC(sei_display_orientation
)
743 (ctx
, rw
, ¤t
->payload
.display_orientation
));
747 allocate(current
->payload
.other
.data
, current
->payload_size
);
748 for (i
= 0; i
< current
->payload_size
; i
++)
749 xu(8, payload_byte
, current
->payload
.other
.data
[i
], 0, 255);
753 if (byte_alignment(rw
)) {
754 av_unused
int one
= 1, zero
= 0;
755 xu(1, bit_equal_to_one
, one
, 1, 1);
756 while (byte_alignment(rw
))
757 xu(1, bit_equal_to_zero
, zero
, 0, 0);
761 end_position
= bitstream_tell(rw
);
762 if (end_position
< start_position
+ 8 * current
->payload_size
) {
763 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Incorrect SEI payload length: "
764 "header %d bits, actually %d bits.\n",
765 8 * current
->payload_size
,
766 end_position
- start_position
);
767 return AVERROR_INVALIDDATA
;
770 end_position
= put_bits_count(rw
);
771 current
->payload_size
= (end_position
- start_position
) / 8;
777 static int FUNC(sei
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
782 HEADER("Supplemental Enhancement Information");
784 CHECK(FUNC(nal_unit_header
)(ctx
, rw
, ¤t
->nal_unit_header
,
788 for (k
= 0; k
< H264_MAX_SEI_PAYLOADS
; k
++) {
789 uint32_t payload_type
= 0;
790 uint32_t payload_size
= 0;
793 while (bitstream_peek(rw
, 8) == 0xff) {
794 xu(8, ff_byte
, tmp
, 0xff, 0xff);
797 xu(8, last_payload_type_byte
, tmp
, 0, 254);
800 while (bitstream_peek(rw
, 8) == 0xff) {
801 xu(8, ff_byte
, tmp
, 0xff, 0xff);
804 xu(8, last_payload_size_byte
, tmp
, 0, 254);
807 current
->payload
[k
].payload_type
= payload_type
;
808 current
->payload
[k
].payload_size
= payload_size
;
810 CHECK(FUNC(sei_payload
)(ctx
, rw
, ¤t
->payload
[k
]));
812 if (!cbs_h2645_read_more_rbsp_data(rw
))
815 if (k
>= H264_MAX_SEI_PAYLOADS
) {
816 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Too many payloads in "
817 "SEI message: found %d.\n", k
);
818 return AVERROR_INVALIDDATA
;
820 current
->payload_count
= k
+ 1;
822 for (k
= 0; k
< current
->payload_count
; k
++) {
823 PutBitContext start_state
;
827 // Somewhat clumsy: we write the payload twice when
828 // we don't know the size in advance. This will mess
829 // with trace output, but is otherwise harmless.
831 need_size
= !current
->payload
[k
].payload_size
;
832 for (i
= 0; i
< 1 + need_size
; i
++) {
835 tmp
= current
->payload
[k
].payload_type
;
837 xu(8, ff_byte
, 0xff, 0xff, 0xff);
840 xu(8, last_payload_type_byte
, tmp
, 0, 254);
842 tmp
= current
->payload
[k
].payload_size
;
844 xu(8, ff_byte
, 0xff, 0xff, 0xff);
847 xu(8, last_payload_size_byte
, tmp
, 0, 254);
849 CHECK(FUNC(sei_payload
)(ctx
, rw
, ¤t
->payload
[k
]));
854 CHECK(FUNC(rbsp_trailing_bits
)(ctx
, rw
));
859 static int FUNC(aud
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
864 HEADER("Access Unit Delimiter");
866 CHECK(FUNC(nal_unit_header
)(ctx
, rw
, ¤t
->nal_unit_header
,
869 u(3, primary_pic_type
, 0, 7);
871 CHECK(FUNC(rbsp_trailing_bits
)(ctx
, rw
));
876 static int FUNC(ref_pic_list_modification
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
877 H264RawSliceHeader
*current
)
879 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
880 const H264RawSPS
*sps
= h264
->active_sps
;
883 if (current
->slice_type
% 5 != 2 &&
884 current
->slice_type
% 5 != 4) {
885 flag(ref_pic_list_modification_flag_l0
);
886 if (current
->ref_pic_list_modification_flag_l0
) {
887 for (i
= 0; i
< H264_MAX_RPLM_COUNT
; i
++) {
888 xue(modification_of_pic_nums_idc
,
889 current
->rplm_l0
[i
].modification_of_pic_nums_idc
, 0, 3);
891 mopn
= current
->rplm_l0
[i
].modification_of_pic_nums_idc
;
895 if (mopn
== 0 || mopn
== 1)
896 xue(abs_diff_pic_num_minus1
,
897 current
->rplm_l0
[i
].abs_diff_pic_num_minus1
,
898 0, (1 + current
->field_pic_flag
) *
899 (1 << (sps
->log2_max_frame_num_minus4
+ 4)));
901 xue(long_term_pic_num
,
902 current
->rplm_l0
[i
].long_term_pic_num
,
903 0, sps
->max_num_ref_frames
- 1);
908 if (current
->slice_type
% 5 == 1) {
909 flag(ref_pic_list_modification_flag_l1
);
910 if (current
->ref_pic_list_modification_flag_l1
) {
911 for (i
= 0; i
< H264_MAX_RPLM_COUNT
; i
++) {
912 xue(modification_of_pic_nums_idc
,
913 current
->rplm_l1
[i
].modification_of_pic_nums_idc
, 0, 3);
915 mopn
= current
->rplm_l1
[i
].modification_of_pic_nums_idc
;
919 if (mopn
== 0 || mopn
== 1)
920 xue(abs_diff_pic_num_minus1
,
921 current
->rplm_l1
[i
].abs_diff_pic_num_minus1
,
922 0, (1 + current
->field_pic_flag
) *
923 (1 << (sps
->log2_max_frame_num_minus4
+ 4)));
925 xue(long_term_pic_num
,
926 current
->rplm_l1
[i
].long_term_pic_num
,
927 0, sps
->max_num_ref_frames
- 1);
935 static int FUNC(pred_weight_table
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
936 H264RawSliceHeader
*current
)
938 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
939 const H264RawSPS
*sps
= h264
->active_sps
;
943 ue(luma_log2_weight_denom
, 0, 7);
945 chroma
= !sps
->separate_colour_plane_flag
&& sps
->chroma_format_idc
!= 0;
947 ue(chroma_log2_weight_denom
, 0, 7);
949 for (i
= 0; i
<= current
->num_ref_idx_l0_active_minus1
; i
++) {
950 flag(luma_weight_l0_flag
[i
]);
951 if (current
->luma_weight_l0_flag
[i
]) {
952 se(luma_weight_l0
[i
], -128, +127);
953 se(luma_offset_l0
[i
], -128, +127);
956 flag(chroma_weight_l0_flag
[i
]);
957 if (current
->chroma_weight_l0_flag
[i
]) {
958 for (j
= 0; j
< 2; j
++) {
959 se(chroma_weight_l0
[i
][j
], -128, +127);
960 se(chroma_offset_l0
[i
][j
], -128, +127);
966 if (current
->slice_type
% 5 == 1) {
967 for (i
= 0; i
<= current
->num_ref_idx_l1_active_minus1
; i
++) {
968 flag(luma_weight_l1_flag
[i
]);
969 if (current
->luma_weight_l1_flag
[i
]) {
970 se(luma_weight_l1
[i
], -128, +127);
971 se(luma_offset_l1
[i
], -128, +127);
974 flag(chroma_weight_l1_flag
[i
]);
975 if (current
->chroma_weight_l1_flag
[i
]) {
976 for (j
= 0; j
< 2; j
++) {
977 se(chroma_weight_l1
[i
][j
], -128, +127);
978 se(chroma_offset_l1
[i
][j
], -128, +127);
988 static int FUNC(dec_ref_pic_marking
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
989 H264RawSliceHeader
*current
, int idr_pic_flag
)
991 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
992 const H264RawSPS
*sps
= h264
->active_sps
;
997 flag(no_output_of_prior_pics_flag
);
998 flag(long_term_reference_flag
);
1000 flag(adaptive_ref_pic_marking_mode_flag
);
1001 if (current
->adaptive_ref_pic_marking_mode_flag
) {
1002 for (i
= 0; i
< H264_MAX_MMCO_COUNT
; i
++) {
1003 xue(memory_management_control_operation
,
1004 current
->mmco
[i
].memory_management_control_operation
,
1007 mmco
= current
->mmco
[i
].memory_management_control_operation
;
1011 if (mmco
== 1 || mmco
== 3)
1012 xue(difference_of_pic_nums_minus1
,
1013 current
->mmco
[i
].difference_of_pic_nums_minus1
,
1016 xue(long_term_pic_num
,
1017 current
->mmco
[i
].long_term_pic_num
,
1018 0, sps
->max_num_ref_frames
- 1);
1019 if (mmco
== 3 || mmco
== 6)
1020 xue(long_term_frame_idx
,
1021 current
->mmco
[i
].long_term_frame_idx
,
1022 0, sps
->max_num_ref_frames
- 1);
1024 xue(max_long_term_frame_idx_plus1
,
1025 current
->mmco
[i
].max_long_term_frame_idx_plus1
,
1026 0, sps
->max_num_ref_frames
);
1028 if (i
== H264_MAX_MMCO_COUNT
) {
1029 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Too many "
1030 "memory management control operations.\n");
1031 return AVERROR_INVALIDDATA
;
1039 static int FUNC(slice_header
)(CodedBitstreamContext
*ctx
, RWContext
*rw
,
1040 H264RawSliceHeader
*current
)
1042 CodedBitstreamH264Context
*h264
= ctx
->priv_data
;
1043 const H264RawSPS
*sps
;
1044 const H264RawPPS
*pps
;
1047 int slice_type_i
, slice_type_p
, slice_type_b
;
1048 int slice_type_si
, slice_type_sp
;
1050 HEADER("Slice Header");
1052 CHECK(FUNC(nal_unit_header
)(ctx
, rw
, ¤t
->nal_unit_header
,
1053 1 << H264_NAL_SLICE
|
1054 1 << H264_NAL_IDR_SLICE
|
1055 1 << H264_NAL_AUXILIARY_SLICE
));
1057 if (current
->nal_unit_header
.nal_unit_type
== H264_NAL_AUXILIARY_SLICE
) {
1058 if (!h264
->last_slice_nal_unit_type
) {
1059 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Auxiliary slice "
1060 "is not decodable without the main picture "
1061 "in the same access unit.\n");
1062 return AVERROR_INVALIDDATA
;
1065 h264
->last_slice_nal_unit_type
=
1066 current
->nal_unit_header
.nal_unit_type
;
1068 idr_pic_flag
= h264
->last_slice_nal_unit_type
== H264_NAL_IDR_SLICE
;
1070 ue(first_mb_in_slice
, 0, H264_MAX_MB_PIC_SIZE
- 1);
1071 ue(slice_type
, 0, 9);
1073 slice_type_i
= current
->slice_type
% 5 == 2;
1074 slice_type_p
= current
->slice_type
% 5 == 0;
1075 slice_type_b
= current
->slice_type
% 5 == 1;
1076 slice_type_si
= current
->slice_type
% 5 == 4;
1077 slice_type_sp
= current
->slice_type
% 5 == 3;
1079 if (idr_pic_flag
&& !(slice_type_i
|| slice_type_si
)) {
1080 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "Invalid slice type %d "
1081 "for IDR picture.\n", current
->slice_type
);
1082 return AVERROR_INVALIDDATA
;
1085 ue(pic_parameter_set_id
, 0, 255);
1087 pps
= h264
->pps
[current
->pic_parameter_set_id
];
1089 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "PPS id %d not available.\n",
1090 current
->pic_parameter_set_id
);
1091 return AVERROR_INVALIDDATA
;
1093 h264
->active_pps
= pps
;
1095 sps
= h264
->sps
[pps
->seq_parameter_set_id
];
1097 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "SPS id %d not available.\n",
1098 pps
->seq_parameter_set_id
);
1099 return AVERROR_INVALIDDATA
;
1101 h264
->active_sps
= sps
;
1103 if (sps
->separate_colour_plane_flag
)
1104 u(2, colour_plane_id
, 0, 2);
1106 u(sps
->log2_max_frame_num_minus4
+ 4, frame_num
,
1107 0, (1 << (sps
->log2_max_frame_num_minus4
+ 4)) - 1);
1109 if (!sps
->frame_mbs_only_flag
) {
1110 flag(field_pic_flag
);
1111 if (current
->field_pic_flag
)
1112 flag(bottom_field_flag
);
1114 infer(bottom_field_flag
, 0);
1116 infer(field_pic_flag
, 0);
1117 infer(bottom_field_flag
, 0);
1121 ue(idr_pic_id
, 0, 65535);
1123 if (sps
->pic_order_cnt_type
== 0) {
1124 u(sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4, pic_order_cnt_lsb
,
1125 0, (1 << (sps
->log2_max_pic_order_cnt_lsb_minus4
+ 4)) - 1);
1126 if (pps
->bottom_field_pic_order_in_frame_present_flag
&&
1127 !current
->field_pic_flag
)
1128 se(delta_pic_order_cnt_bottom
, INT32_MIN
+ 1, INT32_MAX
);
1130 } else if (sps
->pic_order_cnt_type
== 1) {
1131 if (!sps
->delta_pic_order_always_zero_flag
) {
1132 se(delta_pic_order_cnt
[0], INT32_MIN
+ 1, INT32_MAX
);
1133 if (pps
->bottom_field_pic_order_in_frame_present_flag
&&
1134 !current
->field_pic_flag
)
1135 se(delta_pic_order_cnt
[1], INT32_MIN
+ 1, INT32_MAX
);
1137 infer(delta_pic_order_cnt
[1], 0);
1139 infer(delta_pic_order_cnt
[0], 0);
1140 infer(delta_pic_order_cnt
[1], 0);
1144 if (pps
->redundant_pic_cnt_present_flag
)
1145 ue(redundant_pic_cnt
, 0, 127);
1148 flag(direct_spatial_mv_pred_flag
);
1150 if (slice_type_p
|| slice_type_sp
|| slice_type_b
) {
1151 flag(num_ref_idx_active_override_flag
);
1152 if (current
->num_ref_idx_active_override_flag
) {
1153 ue(num_ref_idx_l0_active_minus1
, 0, 31);
1155 ue(num_ref_idx_l1_active_minus1
, 0, 31);
1157 infer(num_ref_idx_l0_active_minus1
,
1158 pps
->num_ref_idx_l0_default_active_minus1
);
1159 infer(num_ref_idx_l1_active_minus1
,
1160 pps
->num_ref_idx_l1_default_active_minus1
);
1164 if (current
->nal_unit_header
.nal_unit_type
== 20 ||
1165 current
->nal_unit_header
.nal_unit_type
== 21) {
1166 av_log(ctx
->log_ctx
, AV_LOG_ERROR
, "MVC / 3DAVC not supported.\n");
1167 return AVERROR_PATCHWELCOME
;
1169 CHECK(FUNC(ref_pic_list_modification
)(ctx
, rw
, current
));
1172 if ((pps
->weighted_pred_flag
&& (slice_type_p
|| slice_type_sp
)) ||
1173 (pps
->weighted_bipred_idc
== 1 && slice_type_b
)) {
1174 CHECK(FUNC(pred_weight_table
)(ctx
, rw
, current
));
1177 if (current
->nal_unit_header
.nal_ref_idc
!= 0) {
1178 CHECK(FUNC(dec_ref_pic_marking
)(ctx
, rw
, current
, idr_pic_flag
));
1181 if (pps
->entropy_coding_mode_flag
&&
1182 !slice_type_i
&& !slice_type_si
) {
1183 ue(cabac_init_idc
, 0, 2);
1186 se(slice_qp_delta
, - 51 - 6 * sps
->bit_depth_luma_minus8
,
1187 + 51 + 6 * sps
->bit_depth_luma_minus8
);
1188 if (slice_type_sp
|| slice_type_si
) {
1190 flag(sp_for_switch_flag
);
1191 se(slice_qs_delta
, -51, +51);
1194 if (pps
->deblocking_filter_control_present_flag
) {
1195 ue(disable_deblocking_filter_idc
, 0, 2);
1196 if (current
->disable_deblocking_filter_idc
!= 1) {
1197 se(slice_alpha_c0_offset_div2
, -6, +6);
1198 se(slice_beta_offset_div2
, -6, +6);
1200 infer(slice_alpha_c0_offset_div2
, 0);
1201 infer(slice_beta_offset_div2
, 0);
1204 infer(disable_deblocking_filter_idc
, 0);
1205 infer(slice_alpha_c0_offset_div2
, 0);
1206 infer(slice_beta_offset_div2
, 0);
1209 if (pps
->num_slice_groups_minus1
> 0 &&
1210 pps
->slice_group_map_type
>= 3 &&
1211 pps
->slice_group_map_type
<= 5) {
1212 unsigned int pic_size
, max
, bits
;
1214 pic_size
= (sps
->pic_width_in_mbs_minus1
+ 1) *
1215 (sps
->pic_height_in_map_units_minus1
+ 1);
1216 max
= (pic_size
+ pps
->slice_group_change_rate_minus1
) /
1217 (pps
->slice_group_change_rate_minus1
+ 1);
1218 bits
= av_log2(2 * max
- 1);
1220 u(bits
, slice_group_change_cycle
, 0, max
);
1223 if (pps
->entropy_coding_mode_flag
) {
1224 av_unused
int one
= 1;
1225 while (byte_alignment(rw
))
1226 xu(1, cabac_alignment_one_bit
, one
, 1, 1);