FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
h264_sei.c
Go to the documentation of this file.
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... sei decoding
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * H.264 / AVC / MPEG4 part10 sei decoding.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include "avcodec.h"
29 #include "golomb.h"
30 #include "h264.h"
31 #include "internal.h"
32 
33 static const uint8_t sei_num_clock_ts_table[9] = {
34  1, 1, 1, 2, 2, 3, 3, 2, 3
35 };
36 
38 {
39  h->sei_recovery_frame_cnt = -1;
40  h->sei_dpb_output_delay = 0;
41  h->sei_cpb_removal_delay = -1;
46 
47  h->a53_caption_size = 0;
48  av_freep(&h->a53_caption);
49 }
50 
52 {
53  SPS *sps = &h->sps;
54  int i;
55 
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];
59 
65  }
66  if (sps->pic_struct_present_flag) {
67  unsigned int i, num_clock_ts;
68 
69  h->sei_pic_struct = get_bits(&h->gb, 4);
70  h->sei_ct_type = 0;
71 
73  return AVERROR_INVALIDDATA;
74 
75  num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
76 
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;
80 
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 */
92  } else {
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 */
99  }
100  }
101  }
102  if (sps->time_offset_length > 0)
103  skip_bits(&h->gb,
104  sps->time_offset_length); /* time_offset */
105  }
106  }
107 
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);
111  }
112  return 0;
113 }
114 
116 {
117  int flag;
118 
119  if (size-- < 1)
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
124 
125  if (flag) {
126  if (size-- < 1)
127  return AVERROR_INVALIDDATA;
128  skip_bits(&h->gb, 4); // reserved
131 #if FF_API_AFD
135 #endif /* FF_API_AFD */
136  }
137 
138  return 0;
139 }
140 
142 {
143  int flag;
144  int user_data_type_code;
145  int cc_count;
146 
147  if (size < 3)
148  return AVERROR(EINVAL);
149 
150  user_data_type_code = get_bits(&h->gb, 8);
151  if (user_data_type_code == 0x3) {
152  skip_bits(&h->gb, 1); // reserved
153 
154  flag = get_bits(&h->gb, 1); // process_cc_data_flag
155  if (flag) {
156  skip_bits(&h->gb, 1); // zero bit
157  cc_count = get_bits(&h->gb, 5);
158  skip_bits(&h->gb, 8); // reserved
159  size -= 2;
160 
161  if (cc_count && size >= cc_count * 3) {
162  const uint64_t new_size = (h->a53_caption_size + cc_count
163  * UINT64_C(3));
164  int i, ret;
165 
166  if (new_size > INT_MAX)
167  return AVERROR(EINVAL);
168 
169  /* Allow merging of the cc data from two fields. */
170  ret = av_reallocp(&h->a53_caption, new_size);
171  if (ret < 0)
172  return ret;
173 
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);
178  }
179 
180  skip_bits(&h->gb, 8); // marker_bits
181  }
182  }
183  } else {
184  int i;
185  for (i = 0; i < size - 1; i++)
186  skip_bits(&h->gb, 8);
187  }
188 
189  return 0;
190 }
191 
193 {
194  uint32_t country_code;
195  uint32_t user_identifier;
196 
197  if (size < 7)
198  return AVERROR_INVALIDDATA;
199  size -= 7;
200 
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
204  size--;
205  }
206 
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);
211 
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
217  default:
218  skip_bits(&h->gb, size * 8);
219  break;
220  }
221 
222  return 0;
223 }
224 
226 {
227  uint8_t user_data[16 + 256];
228  int e, build, i;
229 
230  if (size < 16)
231  return AVERROR_INVALIDDATA;
232 
233  for (i = 0; i < sizeof(user_data) - 1 && i < size; i++)
234  user_data[i] = get_bits(&h->gb, 8);
235 
236  user_data[i] = 0;
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))
241  h->x264_build = 67;
242 
243  if (h->avctx->debug & FF_DEBUG_BUGS)
244  av_log(h->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data + 16);
245 
246  for (; i < size; i++)
247  skip_bits(&h->gb, 8);
248 
249  return 0;
250 }
251 
253 {
255 
256  /* 1b exact_match_flag,
257  * 1b broken_link_flag,
258  * 2b changing_slice_group_idc */
259  skip_bits(&h->gb, 4);
260 
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);
263 
264  h->has_recovery_point = 1;
265 
266  return 0;
267 }
268 
270 {
271  unsigned int sps_id;
272  int sched_sel_idx;
273  SPS *sps;
274 
275  sps_id = get_ue_golomb_31(&h->gb);
276  if (sps_id > 31 || !h->sps_buffers[sps_id]) {
278  "non-existing SPS %d referenced in buffering period\n", sps_id);
279  return AVERROR_INVALIDDATA;
280  }
281  sps = h->sps_buffers[sps_id];
282 
283  // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
285  for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
286  h->initial_cpb_removal_delay[sched_sel_idx] =
288  // initial_cpb_removal_delay_offset
290  }
291  }
293  for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
294  h->initial_cpb_removal_delay[sched_sel_idx] =
296  // initial_cpb_removal_delay_offset
298  }
299  }
300 
302  return 0;
303 }
304 
306 {
310 
311  if (h->sei_frame_packing_present) {
318 
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);
323 
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 */;
328  }
329  skip_bits1(&h->gb); // frame_packing_arrangement_extension_flag
330 
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",
339 
340  return 0;
341 }
342 
344 {
346 
348  h->sei_hflip = get_bits1(&h->gb); // hor_flip
349  h->sei_vflip = get_bits1(&h->gb); // ver_flip
350 
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
354  }
355 
356  return 0;
357 }
358 
360 {
361  if (h->avctx->debug & FF_DEBUG_GREEN_MD)
362  av_log(h->avctx, AV_LOG_DEBUG, "Green Metadata Info SEI message\n");
363 
365 
366  if (h->avctx->debug & FF_DEBUG_GREEN_MD)
367  av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_type = %d\n",
369 
372 
373  if (h->avctx->debug & FF_DEBUG_GREEN_MD)
374  av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_period_type = %d\n",
376 
379  if (h->avctx->debug & FF_DEBUG_GREEN_MD)
380  av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_num_seconds = %d\n",
382  }
383  else if (h->sei_green_metadata.period_type==3){
385  if (h->avctx->debug & FF_DEBUG_GREEN_MD)
386  av_log(h->avctx, AV_LOG_DEBUG, "green_metadata_num_pictures = %d\n",
388  }
389 
394 
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",
401 
402  }else if( h->sei_green_metadata.green_metadata_type==1){
405 
406  if (h->avctx->debug & FF_DEBUG_GREEN_MD)
407  av_log(h->avctx, AV_LOG_DEBUG, "xsd_metric_type = %d\n",
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);
413  }
414  }
415 
416  return 0;
417 }
418 
420 {
421  while (get_bits_left(&h->gb) > 16 && show_bits(&h->gb, 16)) {
422  int type = 0;
423  unsigned size = 0;
424  unsigned next;
425  int ret = 0;
426 
427  do {
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);
432 
433  do {
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);
438 
440  av_log(h->avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
441 
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;
446  }
447  next = get_bits_count(&h->gb) + 8 * size;
448 
449  switch (type) {
450  case SEI_TYPE_PIC_TIMING: // Picture timing SEI
451  ret = decode_picture_timing(h);
452  break;
454  ret = decode_registered_user_data(h, size);
455  break;
457  ret = decode_unregistered_user_data(h, size);
458  break;
460  ret = decode_recovery_point(h);
461  break;
463  ret = decode_buffering_period(h);
464  break;
467  break;
470  break;
472  ret = decode_GreenMetadata(h);
473  break;
474  default:
475  av_log(h->avctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
476  }
477  if (ret < 0)
478  return ret;
479 
480  skip_bits_long(&h->gb, next - get_bits_count(&h->gb));
481 
482  // FIXME check bits here
483  align_get_bits(&h->gb);
484  }
485 
486  return 0;
487 }
488 
490 {
495  return "checkerboard_rl";
496  else
497  return "checkerboard_lr";
500  return "col_interleaved_rl";
501  else
502  return "col_interleaved_lr";
505  return "row_interleaved_rl";
506  else
507  return "row_interleaved_lr";
510  return "right_left";
511  else
512  return "left_right";
515  return "bottom_top";
516  else
517  return "top_bottom";
520  return "block_rl";
521  else
522  return "block_lr";
523  case SEI_FPA_TYPE_2D:
524  default:
525  return "mono";
526  }
527  } else if (h->sei_fpa.frame_packing_arrangement_cancel_flag == 1) {
528  return "mono";
529  } else {
530  return NULL;
531  }
532 }
registered user data as specified by Rec. ITU-T T.35
Definition: h264.h:135
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
uint8_t percent_alpha_point_deblocking_instance
Definition: h264.h:282
GetBitContext gb
Definition: h264.h:524
int sei_cpb_removal_delay
cpb_removal_delay in picture timing SEI message, see H.264 C.1.2
Definition: h264.h:767
int quincunx_subsampling
Definition: h264.h:735
int a53_caption_size
Definition: h264.h:749
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
unregistered user data
Definition: h264.h:136
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
Sequence parameter set.
Definition: h264.h:174
int initial_cpb_removal_delay[32]
Initial timestamps for CPBs.
Definition: h264.h:818
#define FF_DEBUG_GREEN_MD
Definition: avcodec.h:2869
int sei_reguserdata_afd_present
User data registered by Rec.
Definition: h264.h:747
H264Context.
Definition: h264.h:517
int frame_packing_arrangement_repetition_period
Definition: h264.h:266
int frame_packing_arrangement_id
Definition: h264.h:263
void ff_h264_reset_sei(H264Context *h)
Reset SEI values at the beginning of the frame.
Definition: h264_sei.c:37
uint8_t percent_six_tap_filtering
Definition: h264.h:281
uint8_t * a53_caption
Definition: h264.h:750
uint8_t
static int decode_picture_timing(H264Context *h)
Definition: h264_sei.c:51
SEI_FpaType frame_packing_arrangement_type
Definition: h264.h:265
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
uint8_t period_type
Definition: h264.h:276
int sei_vflip
Definition: h264.h:742
int flag
Definition: checkasm.c:76
H.264 / AVC / MPEG4 part10 codec.
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:588
static int decode_unregistered_user_data(H264Context *h, int size)
Definition: h264_sei.c:225
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
#define AVERROR(e)
Definition: error.h:43
int time_offset_length
Definition: h264.h:222
AVS_Value void * user_data
Definition: avisynth_c.h:562
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint16_t num_pictures
Definition: h264.h:278
Libavcodec external API header.
GreenMetaData sei_green_metadata
Definition: h264.h:840
int content_interpretation_type
Definition: h264.h:267
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2856
static int decode_registered_user_data_afd(H264Context *h, int size)
Definition: h264_sei.c:115
int sei_anticlockwise_rotation
Definition: h264.h:741
int initial_cpb_removal_delay_length
initial_cpb_removal_delay_length_minus1 + 1
Definition: h264.h:224
FPA sei_fpa
Definition: h264.h:783
int x264_build
Definition: h264.h:616
const char * ff_h264_sei_stereo_mode(H264Context *h)
Get stereo_mode string from the h264 frame_packing_arrangement.
Definition: h264_sei.c:489
int sei_frame_packing_present
frame_packing_arrangment SEI message
Definition: h264.h:732
static int decode_recovery_point(H264Context *h)
Definition: h264_sei.c:252
static int decode_registered_user_data_closed_caption(H264Context *h, int size)
Definition: h264_sei.c:141
SPS sps
current sps
Definition: h264.h:576
uint8_t percent_non_zero_macroblocks
Definition: h264.h:279
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:287
int sei_hflip
Definition: h264.h:742
#define MAX_SPS_COUNT
Definition: h264.h:49
static int decode_display_orientation(H264Context *h)
Definition: h264_sei.c:343
frame packing arrangement
Definition: h264.h:138
uint8_t active_format_description
Definition: h264.h:748
SPS * sps_buffers[MAX_SPS_COUNT]
Definition: h264.h:638
int vcl_hrd_parameters_present_flag
Definition: h264.h:220
AVCodecContext * avctx
Definition: h264.h:519
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:100
int debug
debug
Definition: avcodec.h:2842
int dpb_output_delay_length
dpb_output_delay_length_minus1 + 1
Definition: h264.h:226
uint8_t green_metadata_type
Definition: h264.h:275
int frame_packing_arrangement_type
Definition: h264.h:733
GLint GLenum type
Definition: opengl_enc.c:105
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
SEI_PicStructType sei_pic_struct
pic_struct in picture timing SEI message
Definition: h264.h:719
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:329
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:297
display orientation
Definition: h264.h:139
attribute_deprecated int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
Definition: avcodec.h:1997
int frame_packing_arrangement_cancel_flag
is previous arrangement canceled, -1 if never received
Definition: h264.h:264
uint8_t percent_intra_coded_macroblocks
Definition: h264.h:280
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:337
int sei_buffering_period_present
Buffering period SEI flag.
Definition: h264.h:817
static const uint8_t sei_num_clock_ts_table[9]
Definition: h264_sei.c:33
int quincunx_sampling_flag
Definition: h264.h:268
int pic_struct_present_flag
Definition: h264.h:221
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:2843
picture timing
Definition: h264.h:134
uint16_t xsd_metric_value
Definition: h264.h:284
int has_recovery_point
Definition: h264.h:806
uint16_t num_seconds
Definition: h264.h:277
uint8_t xsd_metric_type
Definition: h264.h:283
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
common internal api header.
int nal_hrd_parameters_present_flag
Definition: h264.h:219
static int decode_buffering_period(H264Context *h)
Definition: h264_sei.c:269
int log2_max_frame_num
log2_max_frame_num_minus4 + 4
Definition: h264.h:180
static int decode_GreenMetadata(H264Context *h)
Definition: h264_sei.c:359
int sei_ct_type
Bit set of clock types for fields/frames in picture timing SEI message.
Definition: h264.h:757
#define MKBETAG(a, b, c, d)
Definition: common.h:331
int ff_h264_decode_sei(H264Context *h)
Decode SEI.
Definition: h264_sei.c:419
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
#define FF_DEBUG_BUGS
Definition: avcodec.h:2862
int sei_display_orientation_present
display orientation SEI message
Definition: h264.h:740
static int decode_registered_user_data(H264Context *h, int size)
Definition: h264_sei.c:192
int content_interpretation_type
Definition: h264.h:734
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:449
buffering period (H.264, D.1.1)
Definition: h264.h:133
int cpb_cnt
See H.264 E.1.2.
Definition: h264.h:223
#define av_freep(p)
int cpb_removal_delay_length
cpb_removal_delay_length_minus1 + 1
Definition: h264.h:225
8: frame tripling
Definition: h264.h:155
exp golomb vlc stuff
int sei_recovery_frame_cnt
recovery_frame_cnt from SEI message
Definition: h264.h:776
int sei_dpb_output_delay
dpb_output_delay in picture timing SEI message, see H.264 C.2.2
Definition: h264.h:762
recovery point (frame # to decoder sync)
Definition: h264.h:137
GreenMPEG information.
Definition: h264.h:140
static int decode_frame_packing_arrangement(H264Context *h)
Definition: h264_sei.c:305