FFmpeg
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 / MPEG-4 part10 SEI decoding.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27 
28 #include <limits.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include "libavutil/error.h"
32 #include "libavutil/log.h"
33 #include "libavutil/macros.h"
34 #include "libavutil/mem.h"
35 #include "bytestream.h"
36 #include "get_bits.h"
37 #include "golomb.h"
38 #include "h264_ps.h"
39 #include "h264_sei.h"
40 #include "sei.h"
41 
42 #define AVERROR_PS_NOT_FOUND FFERRTAG(0xF8,'?','P','S')
43 
44 static const uint8_t sei_num_clock_ts_table[9] = {
45  1, 1, 1, 2, 2, 3, 3, 2, 3
46 };
47 
49 {
50  h->recovery_point.recovery_frame_cnt = -1;
51 
52  h->picture_timing.dpb_output_delay = 0;
53  h->picture_timing.cpb_removal_delay = -1;
54 
55  h->picture_timing.present = 0;
56  h->buffering_period.present = 0;
57  h->common.frame_packing.present = 0;
58  h->common.display_orientation.present = 0;
59  h->common.afd.present = 0;
60 
61  ff_h2645_sei_reset(&h->common);
62 }
63 
65  void *logctx)
66 {
67  GetBitContext gb;
68  av_unused int ret;
69 
70  ret = init_get_bits8(&gb, h->payload, h->payload_size_bytes);
71  av_assert1(ret >= 0);
72 
73  if (sps->nal_hrd_parameters_present_flag ||
74  sps->vcl_hrd_parameters_present_flag) {
75  h->cpb_removal_delay = get_bits_long(&gb, sps->cpb_removal_delay_length);
76  h->dpb_output_delay = get_bits_long(&gb, sps->dpb_output_delay_length);
77  }
78  if (sps->pic_struct_present_flag) {
79  unsigned int i, num_clock_ts;
80 
81  h->pic_struct = get_bits(&gb, 4);
82  h->ct_type = 0;
83 
84  if (h->pic_struct > H264_SEI_PIC_STRUCT_FRAME_TRIPLING)
85  return AVERROR_INVALIDDATA;
86 
87  num_clock_ts = sei_num_clock_ts_table[h->pic_struct];
88  h->timecode_cnt = 0;
89  for (i = 0; i < num_clock_ts; i++) {
90  if (get_bits(&gb, 1)) { /* clock_timestamp_flag */
91  H264SEITimeCode *tc = &h->timecode[h->timecode_cnt++];
92  unsigned int full_timestamp_flag;
93  unsigned int counting_type, cnt_dropped_flag;
94  h->ct_type |= 1 << get_bits(&gb, 2);
95  skip_bits(&gb, 1); /* nuit_field_based_flag */
96  counting_type = get_bits(&gb, 5); /* counting_type */
97  full_timestamp_flag = get_bits(&gb, 1);
98  skip_bits(&gb, 1); /* discontinuity_flag */
99  cnt_dropped_flag = get_bits(&gb, 1); /* cnt_dropped_flag */
100  if (cnt_dropped_flag && counting_type > 1 && counting_type < 7)
101  tc->dropframe = 1;
102  tc->frame = get_bits(&gb, 8); /* n_frames */
103  if (full_timestamp_flag) {
104  tc->full = 1;
105  tc->seconds = get_bits(&gb, 6); /* seconds_value 0..59 */
106  tc->minutes = get_bits(&gb, 6); /* minutes_value 0..59 */
107  tc->hours = get_bits(&gb, 5); /* hours_value 0..23 */
108  } else {
109  tc->seconds = tc->minutes = tc->hours = tc->full = 0;
110  if (get_bits(&gb, 1)) { /* seconds_flag */
111  tc->seconds = get_bits(&gb, 6);
112  if (get_bits(&gb, 1)) { /* minutes_flag */
113  tc->minutes = get_bits(&gb, 6);
114  if (get_bits(&gb, 1)) /* hours_flag */
115  tc->hours = get_bits(&gb, 5);
116  }
117  }
118  }
119 
120  if (sps->time_offset_length > 0)
121  skip_bits(&gb,
122  sps->time_offset_length); /* time_offset */
123  }
124  }
125 
126  av_log(logctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
127  h->ct_type, h->pic_struct);
128  }
129 
130  return 0;
131 }
132 
134  void *logctx)
135 {
137 
138  if (size > sizeof(h->payload)) {
139  av_log(logctx, AV_LOG_ERROR, "Picture timing SEI payload too large\n");
140  return AVERROR_INVALIDDATA;
141  }
142  bytestream2_get_bufferu(gb, h->payload, size);
143 
144  h->payload_size_bytes = size;
145 
146  h->present = 1;
147  return 0;
148 }
149 
151 {
152  unsigned recovery_frame_cnt = get_ue_golomb_long(gb);
153 
154  if (recovery_frame_cnt >= (1<<MAX_LOG2_MAX_FRAME_NUM)) {
155  av_log(logctx, AV_LOG_ERROR, "recovery_frame_cnt %u is out of range\n", recovery_frame_cnt);
156  return AVERROR_INVALIDDATA;
157  }
158 
159  h->recovery_frame_cnt = recovery_frame_cnt;
160  /* 1b exact_match_flag,
161  * 1b broken_link_flag,
162  * 2b changing_slice_group_idc */
163  skip_bits(gb, 4);
164 
165  return 0;
166 }
167 
169  const H264ParamSets *ps, void *logctx)
170 {
171  unsigned int sps_id;
172  int sched_sel_idx;
173  const SPS *sps;
174 
175  sps_id = get_ue_golomb_31(gb);
176  if (sps_id > 31 || !ps->sps_list[sps_id]) {
177  av_log(logctx, AV_LOG_ERROR,
178  "non-existing SPS %d referenced in buffering period\n", sps_id);
179  return sps_id > 31 ? AVERROR_INVALIDDATA : AVERROR_PS_NOT_FOUND;
180  }
181  sps = ps->sps_list[sps_id];
182 
183  // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
184  if (sps->nal_hrd_parameters_present_flag) {
185  for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
186  h->initial_cpb_removal_delay[sched_sel_idx] =
187  get_bits_long(gb, sps->initial_cpb_removal_delay_length);
188  // initial_cpb_removal_delay_offset
189  skip_bits(gb, sps->initial_cpb_removal_delay_length);
190  }
191  }
192  if (sps->vcl_hrd_parameters_present_flag) {
193  for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
194  h->initial_cpb_removal_delay[sched_sel_idx] =
195  get_bits_long(gb, sps->initial_cpb_removal_delay_length);
196  // initial_cpb_removal_delay_offset
197  skip_bits(gb, sps->initial_cpb_removal_delay_length);
198  }
199  }
200 
201  h->present = 1;
202  return 0;
203 }
204 
206 {
207  h->green_metadata_type = bytestream2_get_byte(gb);
208 
209  if (h->green_metadata_type == 0) {
210  h->period_type = bytestream2_get_byte(gb);
211 
212  if (h->period_type == 2)
213  h->num_seconds = bytestream2_get_be16(gb);
214  else if (h->period_type == 3)
215  h->num_pictures = bytestream2_get_be16(gb);
216 
217  h->percent_non_zero_macroblocks = bytestream2_get_byte(gb);
218  h->percent_intra_coded_macroblocks = bytestream2_get_byte(gb);
219  h->percent_six_tap_filtering = bytestream2_get_byte(gb);
220  h->percent_alpha_point_deblocking_instance = bytestream2_get_byte(gb);
221 
222  } else if (h->green_metadata_type == 1) {
223  h->xsd_metric_type = bytestream2_get_byte(gb);
224  h->xsd_metric_value = bytestream2_get_be16(gb);
225  }
226 
227  return 0;
228 }
229 
231  const H264ParamSets *ps, void *logctx)
232 {
233  GetByteContext gbyte;
234  int master_ret = 0;
235 
236  av_assert1((get_bits_count(gb) % 8) == 0);
237  bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
238  get_bits_left(gb) / 8);
239 
240  while (bytestream2_get_bytes_left(&gbyte) > 2 && bytestream2_peek_ne16(&gbyte)) {
241  GetByteContext gbyte_payload;
242  GetBitContext gb_payload;
243  int type = 0;
244  unsigned size = 0;
245  int ret = 0;
246 
247  do {
248  if (bytestream2_get_bytes_left(&gbyte) <= 0)
249  return AVERROR_INVALIDDATA;
250  type += bytestream2_peek_byteu(&gbyte);
251  } while (bytestream2_get_byteu(&gbyte) == 255);
252 
253  do {
254  if (bytestream2_get_bytes_left(&gbyte) <= 0)
255  return AVERROR_INVALIDDATA;
256  size += bytestream2_peek_byteu(&gbyte);
257  } while (bytestream2_get_byteu(&gbyte) == 255);
258 
259  if (size > bytestream2_get_bytes_left(&gbyte)) {
260  av_log(logctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
262  return AVERROR_INVALIDDATA;
263  }
264 
265  bytestream2_init (&gbyte_payload, gbyte.buffer, size);
266  ret = init_get_bits8(&gb_payload, gbyte.buffer, size);
267  if (ret < 0)
268  return ret;
269 
270  switch (type) {
271  case SEI_TYPE_PIC_TIMING: // Picture timing SEI
272  ret = decode_picture_timing(&h->picture_timing, &gbyte_payload, logctx);
273  break;
275  ret = decode_recovery_point(&h->recovery_point, &gb_payload, logctx);
276  break;
278  ret = decode_buffering_period(&h->buffering_period, &gb_payload, ps, logctx);
279  break;
281  ret = decode_green_metadata(&h->green_metadata, &gbyte_payload);
282  break;
283  default:
285  &gb_payload, &gbyte_payload, logctx);
287  av_log(logctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
288  }
289  if (ret < 0 && ret != AVERROR_PS_NOT_FOUND)
290  return ret;
291  if (ret < 0)
292  master_ret = ret;
293 
294  if (get_bits_left(&gb_payload) < 0) {
295  av_log(logctx, AV_LOG_WARNING, "SEI type %d overread by %d bits\n",
296  type, -get_bits_left(&gb_payload));
297  }
298 
299  bytestream2_skipu(&gbyte, size);
300  }
301 
302  return master_ret;
303 }
304 
306 {
307  if (h->arrangement_cancel_flag == 0) {
308  switch (h->arrangement_type) {
310  if (h->content_interpretation_type == 2)
311  return "checkerboard_rl";
312  else
313  return "checkerboard_lr";
315  if (h->content_interpretation_type == 2)
316  return "col_interleaved_rl";
317  else
318  return "col_interleaved_lr";
320  if (h->content_interpretation_type == 2)
321  return "row_interleaved_rl";
322  else
323  return "row_interleaved_lr";
325  if (h->content_interpretation_type == 2)
326  return "right_left";
327  else
328  return "left_right";
330  if (h->content_interpretation_type == 2)
331  return "bottom_top";
332  else
333  return "top_bottom";
335  if (h->content_interpretation_type == 2)
336  return "block_rl";
337  else
338  return "block_lr";
340  default:
341  return "mono";
342  }
343  } else if (h->arrangement_cancel_flag == 1) {
344  return "mono";
345  } else {
346  return NULL;
347  }
348 }
H264ParamSets::sps_list
const SPS * sps_list[MAX_SPS_COUNT]
RefStruct references.
Definition: h264_ps.h:145
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVERROR_PS_NOT_FOUND
#define AVERROR_PS_NOT_FOUND
Definition: h264_sei.c:42
ff_h264_sei_uninit
void ff_h264_sei_uninit(H264SEIContext *h)
Reset SEI values at the beginning of the frame.
Definition: h264_sei.c:48
bytestream2_peek_ne16
#define bytestream2_peek_ne16
Definition: bytestream.h:131
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:695
H264SEITimeCode::seconds
int seconds
Definition: h264_sei.h:48
SEI_FPA_H264_TYPE_CHECKERBOARD
@ SEI_FPA_H264_TYPE_CHECKERBOARD
Definition: sei.h:148
SEI_FPA_TYPE_INTERLEAVE_TEMPORAL
@ SEI_FPA_TYPE_INTERLEAVE_TEMPORAL
Definition: sei.h:153
H2645SEIFramePacking
Definition: h2645_sei.h:63
GetByteContext
Definition: bytestream.h:33
H264SEITimeCode::full
int full
Definition: h264_sei.h:46
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
bytestream2_skipu
static av_always_inline void bytestream2_skipu(GetByteContext *g, unsigned int size)
Definition: bytestream.h:174
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
av_unused
#define av_unused
Definition: attributes.h:131
SEI_FPA_TYPE_TOP_BOTTOM
@ SEI_FPA_TYPE_TOP_BOTTOM
Definition: sei.h:152
H264SEITimeCode::minutes
int minutes
Definition: h264_sei.h:49
decode_green_metadata
static int decode_green_metadata(H264SEIGreenMetaData *h, GetByteContext *gb)
Definition: h264_sei.c:205
ff_h2645_sei_message_decode
int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type, enum AVCodecID codec_id, GetBitContext *gb, GetByteContext *gbyte, void *logctx)
Decode a single SEI message.
Definition: h2645_sei.c:487
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
golomb.h
exp golomb vlc stuff
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
sei_num_clock_ts_table
static const uint8_t sei_num_clock_ts_table[9]
Definition: h264_sei.c:44
SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN
@ SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN
Definition: sei.h:149
macros.h
ff_h264_sei_decode
int ff_h264_sei_decode(H264SEIContext *h, GetBitContext *gb, const H264ParamSets *ps, void *logctx)
Definition: h264_sei.c:230
GetBitContext
Definition: get_bits.h:108
decode_recovery_point
static int decode_recovery_point(H264SEIRecoveryPoint *h, GetBitContext *gb, void *logctx)
Definition: h264_sei.c:150
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
ff_h264_sei_process_picture_timing
int ff_h264_sei_process_picture_timing(H264SEIPictureTiming *h, const SPS *sps, void *logctx)
Parse the contents of a picture timing message given an active SPS.
Definition: h264_sei.c:64
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
SEI_FPA_H264_TYPE_INTERLEAVE_ROW
@ SEI_FPA_H264_TYPE_INTERLEAVE_ROW
Definition: sei.h:150
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
get_bits.h
limits.h
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:109
NULL
#define NULL
Definition: coverity.c:32
H264SEIContext
Definition: h264_sei.h:119
SPS
Sequence parameter set.
Definition: h264_ps.h:44
sei.h
H264SEIGreenMetaData
Definition: h264_sei.h:106
SEI_TYPE_GREEN_METADATA
@ SEI_TYPE_GREEN_METADATA
Definition: sei.h:85
H264SEIRecoveryPoint
Definition: h264_sei.h:90
h264_ps.h
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
error.h
H264SEIPictureTiming
Definition: h264_sei.h:54
size
int size
Definition: twinvq_data.h:10344
H264SEITimeCode::frame
int frame
Definition: h264_sei.h:47
MAX_LOG2_MAX_FRAME_NUM
#define MAX_LOG2_MAX_FRAME_NUM
Definition: h264_ps.h:39
SEI_FPA_TYPE_SIDE_BY_SIDE
@ SEI_FPA_TYPE_SIDE_BY_SIDE
Definition: sei.h:151
ff_h2645_sei_reset
void ff_h2645_sei_reset(H2645SEI *s)
Definition: h2645_sei.c:921
H264_SEI_PIC_STRUCT_FRAME_TRIPLING
@ H264_SEI_PIC_STRUCT_FRAME_TRIPLING
8: frame tripling
Definition: h264_sei.h:40
h264_sei.h
SEI_FPA_H264_TYPE_2D
@ SEI_FPA_H264_TYPE_2D
Definition: sei.h:154
decode_picture_timing
static int decode_picture_timing(H264SEIPictureTiming *h, GetByteContext *gb, void *logctx)
Definition: h264_sei.c:133
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
H264SEITimeCode
Definition: h264_sei.h:43
SEI_TYPE_PIC_TIMING
@ SEI_TYPE_PIC_TIMING
Definition: sei.h:31
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
H264SEIBufferingPeriod
Definition: h264_sei.h:101
ret
ret
Definition: filter_design.txt:187
sps
static int FUNC() sps(CodedBitstreamContext *ctx, RWContext *rw, H264RawSPS *current)
Definition: cbs_h264_syntax_template.c:260
get_ue_golomb_31
static int get_ue_golomb_31(GetBitContext *gb)
read unsigned exp golomb code, constraint to a max of 31.
Definition: golomb.h:120
SEI_TYPE_BUFFERING_PERIOD
@ SEI_TYPE_BUFFERING_PERIOD
Definition: sei.h:30
H264SEITimeCode::hours
int hours
Definition: h264_sei.h:50
H264ParamSets
Definition: h264_ps.h:144
FF_H2645_SEI_MESSAGE_UNHANDLED
@ FF_H2645_SEI_MESSAGE_UNHANDLED
Definition: h2645_sei.h:149
SEI_TYPE_RECOVERY_POINT
@ SEI_TYPE_RECOVERY_POINT
Definition: sei.h:36
mem.h
bytestream2_get_bufferu
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
get_ue_golomb_long
static unsigned get_ue_golomb_long(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to UINT32_MAX-1.
Definition: golomb.h:104
ff_h264_sei_stereo_mode
const char * ff_h264_sei_stereo_mode(const H2645SEIFramePacking *h)
Get stereo_mode string from the h264 frame_packing_arrangement.
Definition: h264_sei.c:305
decode_buffering_period
static int decode_buffering_period(H264SEIBufferingPeriod *h, GetBitContext *gb, const H264ParamSets *ps, void *logctx)
Definition: h264_sei.c:168
H264SEITimeCode::dropframe
int dropframe
Definition: h264_sei.h:51
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070