2 * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * H.264 / AVC / MPEG4 part10 sei decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
33 static const uint8_t sei_num_clock_ts_table[9] = {
34 1, 1, 1, 2, 2, 3, 3, 2, 3
37 void ff_h264_reset_sei(H264Context *h)
39 h->sei_recovery_frame_cnt = -1;
40 h->sei_dpb_output_delay = 0;
41 h->sei_cpb_removal_delay = -1;
42 h->sei_buffering_period_present = 0;
43 h->sei_frame_packing_present = 0;
44 h->sei_display_orientation_present = 0;
45 h->sei_reguserdata_afd_present = 0;
47 h->a53_caption_size = 0;
48 av_freep(&h->a53_caption);
51 static int decode_picture_timing(H264Context *h)
56 for (i = 0; i<MAX_SPS_COUNT; i++)
57 if (!sps->log2_max_frame_num && h->sps_buffers[i])
58 sps = h->sps_buffers[i];
60 if (sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag) {
61 h->sei_cpb_removal_delay = get_bits_long(&h->gb,
62 sps->cpb_removal_delay_length);
63 h->sei_dpb_output_delay = get_bits_long(&h->gb,
64 sps->dpb_output_delay_length);
66 if (sps->pic_struct_present_flag) {
67 unsigned int i, num_clock_ts;
69 h->sei_pic_struct = get_bits(&h->gb, 4);
72 if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
73 return AVERROR_INVALIDDATA;
75 num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
77 for (i = 0; i < num_clock_ts; i++) {
78 if (get_bits(&h->gb, 1)) { /* clock_timestamp_flag */
79 unsigned int full_timestamp_flag;
81 h->sei_ct_type |= 1 << get_bits(&h->gb, 2);
82 skip_bits(&h->gb, 1); /* nuit_field_based_flag */
83 skip_bits(&h->gb, 5); /* counting_type */
84 full_timestamp_flag = get_bits(&h->gb, 1);
85 skip_bits(&h->gb, 1); /* discontinuity_flag */
86 skip_bits(&h->gb, 1); /* cnt_dropped_flag */
87 skip_bits(&h->gb, 8); /* n_frames */
88 if (full_timestamp_flag) {
89 skip_bits(&h->gb, 6); /* seconds_value 0..59 */
90 skip_bits(&h->gb, 6); /* minutes_value 0..59 */
91 skip_bits(&h->gb, 5); /* hours_value 0..23 */
93 if (get_bits(&h->gb, 1)) { /* seconds_flag */
94 skip_bits(&h->gb, 6); /* seconds_value range 0..59 */
95 if (get_bits(&h->gb, 1)) { /* minutes_flag */
96 skip_bits(&h->gb, 6); /* minutes_value 0..59 */
97 if (get_bits(&h->gb, 1)) /* hours_flag */
98 skip_bits(&h->gb, 5); /* hours_value 0..23 */
102 if (sps->time_offset_length > 0)
104 sps->time_offset_length); /* time_offset */
108 if (h->avctx->debug & FF_DEBUG_PICT_INFO)
109 av_log(h->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
110 h->sei_ct_type, h->sei_pic_struct);
115 static int decode_registered_user_data_afd(H264Context *h, int size)
120 return AVERROR_INVALIDDATA;
121 skip_bits(&h->gb, 1); // 0
122 flag = get_bits(&h->gb, 1); // active_format_flag
123 skip_bits(&h->gb, 6); // reserved
127 return AVERROR_INVALIDDATA;
128 skip_bits(&h->gb, 4); // reserved
129 h->active_format_description = get_bits(&h->gb, 4);
130 h->sei_reguserdata_afd_present = 1;
132 FF_DISABLE_DEPRECATION_WARNINGS
133 h->avctx->dtg_active_format = h->active_format_description;
134 FF_ENABLE_DEPRECATION_WARNINGS
135 #endif /* FF_API_AFD */
141 static int decode_registered_user_data_closed_caption(H264Context *h, int size)
144 int user_data_type_code;
148 return AVERROR(EINVAL);
150 user_data_type_code = get_bits(&h->gb, 8);
151 if (user_data_type_code == 0x3) {
152 skip_bits(&h->gb, 1); // reserved
154 flag = get_bits(&h->gb, 1); // process_cc_data_flag
156 skip_bits(&h->gb, 1); // zero bit
157 cc_count = get_bits(&h->gb, 5);
158 skip_bits(&h->gb, 8); // reserved
161 if (cc_count && size >= cc_count * 3) {
162 const uint64_t new_size = (h->a53_caption_size + cc_count
166 if (new_size > INT_MAX)
167 return AVERROR(EINVAL);
169 /* Allow merging of the cc data from two fields. */
170 ret = av_reallocp(&h->a53_caption, new_size);
174 for (i = 0; i < cc_count; i++) {
175 h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
176 h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
177 h->a53_caption[h->a53_caption_size++] = get_bits(&h->gb, 8);
180 skip_bits(&h->gb, 8); // marker_bits
185 for (i = 0; i < size - 1; i++)
186 skip_bits(&h->gb, 8);
192 static int decode_registered_user_data(H264Context *h, int size)
194 uint32_t country_code;
195 uint32_t user_identifier;
198 return AVERROR_INVALIDDATA;
201 country_code = get_bits(&h->gb, 8); // itu_t_t35_country_code
202 if (country_code == 0xFF) {
203 skip_bits(&h->gb, 8); // itu_t_t35_country_code_extension_byte
207 /* itu_t_t35_payload_byte follows */
208 skip_bits(&h->gb, 8); // terminal provider code
209 skip_bits(&h->gb, 8); // terminal provider oriented code
210 user_identifier = get_bits_long(&h->gb, 32);
212 switch (user_identifier) {
213 case MKBETAG('D', 'T', 'G', '1'): // afd_data
214 return decode_registered_user_data_afd(h, size);
215 case MKBETAG('G', 'A', '9', '4'): // closed captions
216 return decode_registered_user_data_closed_caption(h, size);
218 skip_bits(&h->gb, size * 8);
225 static int decode_unregistered_user_data(H264Context *h, int size)
227 uint8_t user_data[16 + 256];
231 return AVERROR_INVALIDDATA;
233 for (i = 0; i < sizeof(user_data) - 1 && i < size; i++)
234 user_data[i] = get_bits(&h->gb, 8);
237 e = sscanf(user_data + 16, "x264 - core %d", &build);
238 if (e == 1 && build > 0)
239 h->x264_build = build;
240 if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
243 if (h->avctx->debug & FF_DEBUG_BUGS)
244 av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
246 for (; i < size; i++)
247 skip_bits(&h->gb, 8);
252 static int decode_recovery_point(H264Context *h)
254 h->sei_recovery_frame_cnt = get_ue_golomb(&h->gb);
256 /* 1b exact_match_flag,
257 * 1b broken_link_flag,
258 * 2b changing_slice_group_idc */
259 skip_bits(&h->gb, 4);
261 if (h->avctx->debug & FF_DEBUG_PICT_INFO)
262 av_log(h->avctx, AV_LOG_DEBUG, "sei_recovery_frame_cnt: %d\n", h->sei_recovery_frame_cnt);
264 h->has_recovery_point = 1;
269 static int decode_buffering_period(H264Context *h)
275 sps_id = get_ue_golomb_31(&h->gb);
276 if (sps_id > 31 || !h->sps_buffers[sps_id]) {
277 av_log(h->avctx, AV_LOG_ERROR,
278 "non-existing SPS %d referenced in buffering period\n", sps_id);
279 return AVERROR_INVALIDDATA;
281 sps = h->sps_buffers[sps_id];
283 // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
284 if (sps->nal_hrd_parameters_present_flag) {
285 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
286 h->initial_cpb_removal_delay[sched_sel_idx] =
287 get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
288 // initial_cpb_removal_delay_offset
289 skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
292 if (sps->vcl_hrd_parameters_present_flag) {
293 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
294 h->initial_cpb_removal_delay[sched_sel_idx] =
295 get_bits_long(&h->gb, sps->initial_cpb_removal_delay_length);
296 // initial_cpb_removal_delay_offset
297 skip_bits(&h->gb, sps->initial_cpb_removal_delay_length);
301 h->sei_buffering_period_present = 1;
305 static int decode_frame_packing_arrangement(H264Context *h)
307 h->sei_fpa.frame_packing_arrangement_id = get_ue_golomb(&h->gb);
308 h->sei_fpa.frame_packing_arrangement_cancel_flag = get_bits1(&h->gb);
309 h->sei_frame_packing_present = !h->sei_fpa.frame_packing_arrangement_cancel_flag;
311 if (h->sei_frame_packing_present) {
312 h->sei_fpa.frame_packing_arrangement_type =
313 h->frame_packing_arrangement_type = get_bits(&h->gb, 7);
314 h->sei_fpa.quincunx_sampling_flag =
315 h->quincunx_subsampling = get_bits1(&h->gb);
316 h->sei_fpa.content_interpretation_type =
317 h->content_interpretation_type = get_bits(&h->gb, 6);
319 // the following skips: spatial_flipping_flag, frame0_flipped_flag,
320 // field_views_flag, current_frame_is_frame0_flag,
321 // frame0_self_contained_flag, frame1_self_contained_flag
322 skip_bits(&h->gb, 6);
324 if (!h->quincunx_subsampling && h->frame_packing_arrangement_type != 5)
325 skip_bits(&h->gb, 16); // frame[01]_grid_position_[xy]
326 skip_bits(&h->gb, 8); // frame_packing_arrangement_reserved_byte
327 h->sei_fpa.frame_packing_arrangement_repetition_period = get_ue_golomb(&h->gb) /* frame_packing_arrangement_repetition_period */;
329 skip_bits1(&h->gb); // frame_packing_arrangement_extension_flag
331 if (h->avctx->debug & FF_DEBUG_PICT_INFO)
332 av_log(h->avctx, AV_LOG_DEBUG, "SEI FPA %d %d %d %d %d %d\n",
333 h->sei_fpa.frame_packing_arrangement_id,
334 h->sei_fpa.frame_packing_arrangement_cancel_flag,
335 h->sei_fpa.frame_packing_arrangement_type,
336 h->sei_fpa.quincunx_sampling_flag,
337 h->sei_fpa.content_interpretation_type,
338 h->sei_fpa.frame_packing_arrangement_repetition_period);
343 static int decode_display_orientation(H264Context *h)
345 h->sei_display_orientation_present = !get_bits1(&h->gb);
347 if (h->sei_display_orientation_present) {
348 h->sei_hflip = get_bits1(&h->gb); // hor_flip
349 h->sei_vflip = get_bits1(&h->gb); // ver_flip
351 h->sei_anticlockwise_rotation = get_bits(&h->gb, 16);
352 get_ue_golomb(&h->gb); // display_orientation_repetition_period
353 skip_bits1(&h->gb); // display_orientation_extension_flag
359 static int decode_GreenMetadata(H264Context *h)
361 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
362 av_log(h->avctx, AV_LOG_DEBUG, "Green Metadata Info SEI message\n");
364 h->sei_green_metadata.green_metadata_type=get_bits(&h->gb, 8);
366 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
367 av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_type = %d\n",
368 h->sei_green_metadata.green_metadata_type);
370 if (h->sei_green_metadata.green_metadata_type==0){
371 h->sei_green_metadata.period_type=get_bits(&h->gb, 8);
373 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
374 av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_period_type = %d\n",
375 h->sei_green_metadata.period_type);
377 if (h->sei_green_metadata.green_metadata_type==2){
378 h->sei_green_metadata.num_seconds = get_bits(&h->gb, 16);
379 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
380 av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_num_seconds = %d\n",
381 h->sei_green_metadata.num_seconds);
383 else if (h->sei_green_metadata.period_type==3){
384 h->sei_green_metadata.num_pictures = get_bits(&h->gb, 16);
385 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
386 av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_num_pictures = %d\n",
387 h->sei_green_metadata.num_pictures);
390 h->sei_green_metadata.percent_non_zero_macroblocks=get_bits(&h->gb, 8);
391 h->sei_green_metadata.percent_intra_coded_macroblocks=get_bits(&h->gb, 8);
392 h->sei_green_metadata.percent_six_tap_filtering=get_bits(&h->gb, 8);
393 h->sei_green_metadata.percent_alpha_point_deblocking_instance=get_bits(&h->gb, 8);
395 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
396 av_log(h->avctx, AV_LOG_DEBUG, "SEI GREEN Complexity Metrics = %f %f %f %f\n",
397 (float)h->sei_green_metadata.percent_non_zero_macroblocks/255,
398 (float)h->sei_green_metadata.percent_intra_coded_macroblocks/255,
399 (float)h->sei_green_metadata.percent_six_tap_filtering/255,
400 (float)h->sei_green_metadata.percent_alpha_point_deblocking_instance/255);
402 }else if( h->sei_green_metadata.green_metadata_type==1){
403 h->sei_green_metadata.xsd_metric_type=get_bits(&h->gb, 8);
404 h->sei_green_metadata.xsd_metric_value=get_bits(&h->gb, 16);
406 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
407 av_log(h->avctx, AV_LOG_DEBUG, "xsd_metric_type = %d\n",
408 h->sei_green_metadata.xsd_metric_type);
409 if ( h->sei_green_metadata.xsd_metric_type==0){
410 if (h->avctx->debug & FF_DEBUG_GREEN_MD)
411 av_log(h->avctx, AV_LOG_DEBUG, "xsd_metric_value = %f\n",
412 (float)h->sei_green_metadata.xsd_metric_value/100);
419 int ff_h264_decode_sei(H264Context *h)
421 while (get_bits_left(&h->gb) > 16 && show_bits(&h->gb, 16)) {
428 if (get_bits_left(&h->gb) < 8)
429 return AVERROR_INVALIDDATA;
430 type += show_bits(&h->gb, 8);
431 } while (get_bits(&h->gb, 8) == 255);
434 if (get_bits_left(&h->gb) < 8)
435 return AVERROR_INVALIDDATA;
436 size += show_bits(&h->gb, 8);
437 } while (get_bits(&h->gb, 8) == 255);
439 if (h->avctx->debug&FF_DEBUG_STARTCODE)
440 av_log(h->avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
442 if (size > get_bits_left(&h->gb) / 8) {
443 av_log(h->avctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
444 type, 8*size, get_bits_left(&h->gb));
445 return AVERROR_INVALIDDATA;
447 next = get_bits_count(&h->gb) + 8 * size;
450 case SEI_TYPE_PIC_TIMING: // Picture timing SEI
451 ret = decode_picture_timing(h);
453 case SEI_TYPE_USER_DATA_REGISTERED:
454 ret = decode_registered_user_data(h, size);
456 case SEI_TYPE_USER_DATA_UNREGISTERED:
457 ret = decode_unregistered_user_data(h, size);
459 case SEI_TYPE_RECOVERY_POINT:
460 ret = decode_recovery_point(h);
462 case SEI_TYPE_BUFFERING_PERIOD:
463 ret = decode_buffering_period(h);
465 case SEI_TYPE_FRAME_PACKING:
466 ret = decode_frame_packing_arrangement(h);
468 case SEI_TYPE_DISPLAY_ORIENTATION:
469 ret = decode_display_orientation(h);
471 case SEI_TYPE_GREEN_METADATA:
472 ret = decode_GreenMetadata(h);
475 av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
480 skip_bits_long(&h->gb, next - get_bits_count(&h->gb));
482 // FIXME check bits here
483 align_get_bits(&h->gb);
489 const char* ff_h264_sei_stereo_mode(H264Context *h)
491 if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 0) {
492 switch (h->sei_fpa.frame_packing_arrangement_type) {
493 case SEI_FPA_TYPE_CHECKERBOARD:
494 if (h->sei_fpa.content_interpretation_type == 2)
495 return "checkerboard_rl";
497 return "checkerboard_lr";
498 case SEI_FPA_TYPE_INTERLEAVE_COLUMN:
499 if (h->sei_fpa.content_interpretation_type == 2)
500 return "col_interleaved_rl";
502 return "col_interleaved_lr";
503 case SEI_FPA_TYPE_INTERLEAVE_ROW:
504 if (h->sei_fpa.content_interpretation_type == 2)
505 return "row_interleaved_rl";
507 return "row_interleaved_lr";
508 case SEI_FPA_TYPE_SIDE_BY_SIDE:
509 if (h->sei_fpa.content_interpretation_type == 2)
513 case SEI_FPA_TYPE_TOP_BOTTOM:
514 if (h->sei_fpa.content_interpretation_type == 2)
518 case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
519 if (h->sei_fpa.content_interpretation_type == 2)
523 case SEI_FPA_TYPE_2D:
527 } else if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 1) {