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 
60  ff_h2645_sei_reset(&h->common);
61 }
62 
64  void *logctx)
65 {
66  GetBitContext gb;
67  av_unused int ret;
68 
69  ret = init_get_bits8(&gb, h->payload, h->payload_size_bytes);
70  av_assert1(ret >= 0);
71 
72  if (sps->nal_hrd_parameters_present_flag ||
73  sps->vcl_hrd_parameters_present_flag) {
74  h->cpb_removal_delay = get_bits_long(&gb, sps->cpb_removal_delay_length);
75  h->dpb_output_delay = get_bits_long(&gb, sps->dpb_output_delay_length);
76  }
77  if (sps->pic_struct_present_flag) {
78  unsigned int i, num_clock_ts;
79 
80  h->pic_struct = get_bits(&gb, 4);
81  h->ct_type = 0;
82 
83  if (h->pic_struct > H264_SEI_PIC_STRUCT_FRAME_TRIPLING)
84  return AVERROR_INVALIDDATA;
85 
86  num_clock_ts = sei_num_clock_ts_table[h->pic_struct];
87  h->timecode_cnt = 0;
88  for (i = 0; i < num_clock_ts; i++) {
89  if (get_bits(&gb, 1)) { /* clock_timestamp_flag */
90  H264SEITimeCode *tc = &h->timecode[h->timecode_cnt++];
91  unsigned int full_timestamp_flag;
92  unsigned int counting_type, cnt_dropped_flag;
93  h->ct_type |= 1 << get_bits(&gb, 2);
94  skip_bits(&gb, 1); /* nuit_field_based_flag */
95  counting_type = get_bits(&gb, 5); /* counting_type */
96  full_timestamp_flag = get_bits(&gb, 1);
97  skip_bits(&gb, 1); /* discontinuity_flag */
98  cnt_dropped_flag = get_bits(&gb, 1); /* cnt_dropped_flag */
99  if (cnt_dropped_flag && counting_type > 1 && counting_type < 7)
100  tc->dropframe = 1;
101  tc->frame = get_bits(&gb, 8); /* n_frames */
102  if (full_timestamp_flag) {
103  tc->full = 1;
104  tc->seconds = get_bits(&gb, 6); /* seconds_value 0..59 */
105  tc->minutes = get_bits(&gb, 6); /* minutes_value 0..59 */
106  tc->hours = get_bits(&gb, 5); /* hours_value 0..23 */
107  } else {
108  tc->seconds = tc->minutes = tc->hours = tc->full = 0;
109  if (get_bits(&gb, 1)) { /* seconds_flag */
110  tc->seconds = get_bits(&gb, 6);
111  if (get_bits(&gb, 1)) { /* minutes_flag */
112  tc->minutes = get_bits(&gb, 6);
113  if (get_bits(&gb, 1)) /* hours_flag */
114  tc->hours = get_bits(&gb, 5);
115  }
116  }
117  }
118 
119  if (sps->time_offset_length > 0)
120  skip_bits(&gb,
121  sps->time_offset_length); /* time_offset */
122  }
123  }
124 
125  av_log(logctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n",
126  h->ct_type, h->pic_struct);
127  }
128 
129  return 0;
130 }
131 
133  void *logctx)
134 {
136 
137  if (size > sizeof(h->payload)) {
138  av_log(logctx, AV_LOG_ERROR, "Picture timing SEI payload too large\n");
139  return AVERROR_INVALIDDATA;
140  }
141  bytestream2_get_bufferu(gb, h->payload, size);
142 
143  h->payload_size_bytes = size;
144 
145  h->present = 1;
146  return 0;
147 }
148 
150 {
151  unsigned recovery_frame_cnt = get_ue_golomb_long(gb);
152 
153  if (recovery_frame_cnt >= (1<<MAX_LOG2_MAX_FRAME_NUM)) {
154  av_log(logctx, AV_LOG_ERROR, "recovery_frame_cnt %u is out of range\n", recovery_frame_cnt);
155  return AVERROR_INVALIDDATA;
156  }
157 
158  h->recovery_frame_cnt = recovery_frame_cnt;
159  /* 1b exact_match_flag,
160  * 1b broken_link_flag,
161  * 2b changing_slice_group_idc */
162  skip_bits(gb, 4);
163 
164  return 0;
165 }
166 
168  const H264ParamSets *ps, void *logctx)
169 {
170  unsigned int sps_id;
171  int sched_sel_idx;
172  const SPS *sps;
173 
174  sps_id = get_ue_golomb_31(gb);
175  if (sps_id > 31 || !ps->sps_list[sps_id]) {
176  av_log(logctx, AV_LOG_ERROR,
177  "non-existing SPS %d referenced in buffering period\n", sps_id);
178  return sps_id > 31 ? AVERROR_INVALIDDATA : AVERROR_PS_NOT_FOUND;
179  }
180  sps = ps->sps_list[sps_id];
181 
182  // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
183  if (sps->nal_hrd_parameters_present_flag) {
184  for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
185  h->initial_cpb_removal_delay[sched_sel_idx] =
186  get_bits_long(gb, sps->initial_cpb_removal_delay_length);
187  // initial_cpb_removal_delay_offset
188  skip_bits(gb, sps->initial_cpb_removal_delay_length);
189  }
190  }
191  if (sps->vcl_hrd_parameters_present_flag) {
192  for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
193  h->initial_cpb_removal_delay[sched_sel_idx] =
194  get_bits_long(gb, sps->initial_cpb_removal_delay_length);
195  // initial_cpb_removal_delay_offset
196  skip_bits(gb, sps->initial_cpb_removal_delay_length);
197  }
198  }
199 
200  h->present = 1;
201  return 0;
202 }
203 
205 {
206  h->green_metadata_type = bytestream2_get_byte(gb);
207 
208  if (h->green_metadata_type == 0) {
209  h->period_type = bytestream2_get_byte(gb);
210 
211  if (h->period_type == 2)
212  h->num_seconds = bytestream2_get_be16(gb);
213  else if (h->period_type == 3)
214  h->num_pictures = bytestream2_get_be16(gb);
215 
216  h->percent_non_zero_macroblocks = bytestream2_get_byte(gb);
217  h->percent_intra_coded_macroblocks = bytestream2_get_byte(gb);
218  h->percent_six_tap_filtering = bytestream2_get_byte(gb);
219  h->percent_alpha_point_deblocking_instance = bytestream2_get_byte(gb);
220 
221  } else if (h->green_metadata_type == 1) {
222  h->xsd_metric_type = bytestream2_get_byte(gb);
223  h->xsd_metric_value = bytestream2_get_be16(gb);
224  }
225 
226  return 0;
227 }
228 
230  const H264ParamSets *ps, void *logctx)
231 {
232  GetByteContext gbyte;
233  int master_ret = 0;
234 
235  av_assert1((get_bits_count(gb) % 8) == 0);
236  bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
237  get_bits_left(gb) / 8);
238 
239  while (bytestream2_get_bytes_left(&gbyte) > 2 && bytestream2_peek_ne16(&gbyte)) {
240  GetByteContext gbyte_payload;
241  GetBitContext gb_payload;
242  int type = 0;
243  unsigned size = 0;
244  int ret = 0;
245 
246  do {
247  if (bytestream2_get_bytes_left(&gbyte) <= 0)
248  return AVERROR_INVALIDDATA;
249  type += bytestream2_peek_byteu(&gbyte);
250  } while (bytestream2_get_byteu(&gbyte) == 255);
251 
252  do {
253  if (bytestream2_get_bytes_left(&gbyte) <= 0)
254  return AVERROR_INVALIDDATA;
255  size += bytestream2_peek_byteu(&gbyte);
256  } while (bytestream2_get_byteu(&gbyte) == 255);
257 
258  if (size > bytestream2_get_bytes_left(&gbyte)) {
259  av_log(logctx, AV_LOG_ERROR, "SEI type %d size %d truncated at %d\n",
261  return AVERROR_INVALIDDATA;
262  }
263 
264  bytestream2_init (&gbyte_payload, gbyte.buffer, size);
265  ret = init_get_bits8(&gb_payload, gbyte.buffer, size);
266  if (ret < 0)
267  return ret;
268 
269  switch (type) {
270  case SEI_TYPE_PIC_TIMING: // Picture timing SEI
271  ret = decode_picture_timing(&h->picture_timing, &gbyte_payload, logctx);
272  break;
274  ret = decode_recovery_point(&h->recovery_point, &gb_payload, logctx);
275  break;
277  ret = decode_buffering_period(&h->buffering_period, &gb_payload, ps, logctx);
278  break;
280  ret = decode_green_metadata(&h->green_metadata, &gbyte_payload);
281  break;
282  default:
284  &gb_payload, &gbyte_payload, logctx);
286  av_log(logctx, AV_LOG_DEBUG, "unknown SEI type %d\n", type);
287  }
288  if (ret < 0 && ret != AVERROR_PS_NOT_FOUND)
289  return ret;
290  if (ret < 0)
291  master_ret = ret;
292 
293  if (get_bits_left(&gb_payload) < 0) {
294  av_log(logctx, AV_LOG_WARNING, "SEI type %d overread by %d bits\n",
295  type, -get_bits_left(&gb_payload));
296  }
297 
298  bytestream2_skipu(&gbyte, size);
299  }
300 
301  return master_ret;
302 }
303 
305 {
306  if (h->arrangement_cancel_flag == 0) {
307  switch (h->arrangement_type) {
309  if (h->content_interpretation_type == 2)
310  return "checkerboard_rl";
311  else
312  return "checkerboard_lr";
314  if (h->content_interpretation_type == 2)
315  return "col_interleaved_rl";
316  else
317  return "col_interleaved_lr";
319  if (h->content_interpretation_type == 2)
320  return "row_interleaved_rl";
321  else
322  return "row_interleaved_lr";
324  if (h->content_interpretation_type == 2)
325  return "right_left";
326  else
327  return "left_right";
329  if (h->content_interpretation_type == 2)
330  return "bottom_top";
331  else
332  return "top_bottom";
334  if (h->content_interpretation_type == 2)
335  return "block_rl";
336  else
337  return "block_lr";
339  default:
340  return "mono";
341  }
342  } else if (h->arrangement_cancel_flag == 1) {
343  return "mono";
344  } else {
345  return NULL;
346  }
347 }
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:216
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:688
H264SEITimeCode::seconds
int seconds
Definition: h264_sei.h:48
SEI_FPA_H264_TYPE_CHECKERBOARD
@ SEI_FPA_H264_TYPE_CHECKERBOARD
Definition: sei.h:148
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition: bytestream.h:158
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:424
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:254
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:204
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:286
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:383
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:337
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:229
GetBitContext
Definition: get_bits.h:109
decode_recovery_point
static int decode_recovery_point(H264SEIRecoveryPoint *h, GetBitContext *gb, void *logctx)
Definition: h264_sei.c:149
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:63
av_unused
#define av_unused
Definition: attributes.h:164
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:544
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:231
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:110
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
error.h
H264SEIPictureTiming
Definition: h264_sei.h:54
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
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:714
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:132
log.h
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:58
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
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
FF_H2645_SEI_MESSAGE_UNHANDLED
@ FF_H2645_SEI_MESSAGE_UNHANDLED
Definition: h2645_sei.h:144
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:304
decode_buffering_period
static int decode_buffering_period(H264SEIBufferingPeriod *h, GetBitContext *gb, const H264ParamSets *ps, void *logctx)
Definition: h264_sei.c:167
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