FFmpeg
hevc_sei.c
Go to the documentation of this file.
1 /*
2  * HEVC Supplementary Enhancement Information messages
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  * Copyright (C) 2012 - 2013 Gildas Cocherel
6  * Copyright (C) 2013 Vittorio Giovara
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "bytestream.h"
26 #include "golomb.h"
27 #include "hevc_ps.h"
28 #include "hevc_sei.h"
29 
31  GetByteContext *gb)
32 {
33  int cIdx;
34  uint8_t hash_type;
35  //uint16_t picture_crc;
36  //uint32_t picture_checksum;
37  hash_type = bytestream2_get_byte(gb);
38 
39  for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
40  if (hash_type == 0) {
41  s->is_md5 = 1;
42  bytestream2_get_buffer(gb, s->md5[cIdx], sizeof(s->md5[cIdx]));
43  } else if (hash_type == 1) {
44  // picture_crc = get_bits(gb, 16);
45  } else if (hash_type == 2) {
46  // picture_checksum = get_bits_long(gb, 32);
47  }
48  }
49  return 0;
50 }
51 
53  GetByteContext *gb)
54 {
55  int i;
56 
57  if (bytestream2_get_bytes_left(gb) < 24)
58  return AVERROR_INVALIDDATA;
59 
60  // Mastering primaries
61  for (i = 0; i < 3; i++) {
62  s->display_primaries[i][0] = bytestream2_get_be16u(gb);
63  s->display_primaries[i][1] = bytestream2_get_be16u(gb);
64  }
65  // White point (x, y)
66  s->white_point[0] = bytestream2_get_be16u(gb);
67  s->white_point[1] = bytestream2_get_be16u(gb);
68 
69  // Max and min luminance of mastering display
70  s->max_luminance = bytestream2_get_be32u(gb);
71  s->min_luminance = bytestream2_get_be32u(gb);
72 
73  // As this SEI message comes before the first frame that references it,
74  // initialize the flag to 2 and decrement on IRAP access unit so it
75  // persists for the coded video sequence (e.g., between two IRAPs)
76  s->present = 2;
77 
78  return 0;
79 }
80 
82  GetByteContext *gb)
83 {
84  if (bytestream2_get_bytes_left(gb) < 4)
85  return AVERROR_INVALIDDATA;
86 
87  // Max and average light levels
88  s->max_content_light_level = bytestream2_get_be16u(gb);
89  s->max_pic_average_light_level = bytestream2_get_be16u(gb);
90  // As this SEI message comes before the first frame that references it,
91  // initialize the flag to 2 and decrement on IRAP access unit so it
92  // persists for the coded video sequence (e.g., between two IRAPs)
93  s->present = 2;
94 
95  return 0;
96 }
97 
99  const HEVCParamSets *ps, void *logctx)
100 {
101  HEVCSEIPictureTiming *h = &s->picture_timing;
102  HEVCSPS *sps;
103 
104  if (!ps->sps_list[s->active_seq_parameter_set_id])
105  return(AVERROR(ENOMEM));
106  sps = (HEVCSPS*)ps->sps_list[s->active_seq_parameter_set_id]->data;
107 
108  if (sps->vui.frame_field_info_present_flag) {
109  int pic_struct = get_bits(gb, 4);
110  h->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
111  if (pic_struct == 2 || pic_struct == 10 || pic_struct == 12) {
112  av_log(logctx, AV_LOG_DEBUG, "BOTTOM Field\n");
113  h->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
114  } else if (pic_struct == 1 || pic_struct == 9 || pic_struct == 11) {
115  av_log(logctx, AV_LOG_DEBUG, "TOP Field\n");
116  h->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
117  } else if (pic_struct == 7) {
118  av_log(logctx, AV_LOG_DEBUG, "Frame/Field Doubling\n");
119  h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING;
120  } else if (pic_struct == 8) {
121  av_log(logctx, AV_LOG_DEBUG, "Frame/Field Tripling\n");
122  h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING;
123  }
124  }
125 
126  return 0;
127 }
128 
130 {
131  int num_sps_ids_minus1;
132  unsigned active_seq_parameter_set_id;
133 
134  get_bits(gb, 4); // active_video_parameter_set_id
135  get_bits(gb, 1); // self_contained_cvs_flag
136  get_bits(gb, 1); // num_sps_ids_minus1
137  num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
138 
139  if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
140  av_log(logctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
141  return AVERROR_INVALIDDATA;
142  }
143 
144  active_seq_parameter_set_id = get_ue_golomb_long(gb);
145  if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
146  av_log(logctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
147  return AVERROR_INVALIDDATA;
148  }
149  s->active_seq_parameter_set_id = active_seq_parameter_set_id;
150 
151  return 0;
152 }
153 
155 {
156  s->num_clock_ts = get_bits(gb, 2);
157 
158  for (int i = 0; i < s->num_clock_ts; i++) {
159  s->clock_timestamp_flag[i] = get_bits(gb, 1);
160 
161  if (s->clock_timestamp_flag[i]) {
162  s->units_field_based_flag[i] = get_bits(gb, 1);
163  s->counting_type[i] = get_bits(gb, 5);
164  s->full_timestamp_flag[i] = get_bits(gb, 1);
165  s->discontinuity_flag[i] = get_bits(gb, 1);
166  s->cnt_dropped_flag[i] = get_bits(gb, 1);
167 
168  s->n_frames[i] = get_bits(gb, 9);
169 
170  if (s->full_timestamp_flag[i]) {
171  s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
172  s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
173  s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
174  } else {
175  s->seconds_flag[i] = get_bits(gb, 1);
176  if (s->seconds_flag[i]) {
177  s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
178  s->minutes_flag[i] = get_bits(gb, 1);
179  if (s->minutes_flag[i]) {
180  s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
181  s->hours_flag[i] = get_bits(gb, 1);
182  if (s->hours_flag[i]) {
183  s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
184  }
185  }
186  }
187  }
188 
189  s->time_offset_length[i] = get_bits(gb, 5);
190  if (s->time_offset_length[i] > 0) {
191  s->time_offset_value[i] = get_bits_long(gb, s->time_offset_length[i]);
192  }
193  }
194  }
195 
196  s->present = 1;
197  return 0;
198 }
199 
201  void *logctx, HEVCSEI *s,
202  const HEVCParamSets *ps, int type)
203 {
204  switch (type) {
205  case 256: // Mismatched value from HM 8.1
206  return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
207  case SEI_TYPE_PIC_TIMING:
208  return decode_nal_sei_pic_timing(s, gb, ps, logctx);
210  return decode_nal_sei_mastering_display_info(&s->mastering_display, gbyte);
212  return decode_nal_sei_content_light_info(&s->content_light, gbyte);
214  return decode_nal_sei_active_parameter_sets(s, gb, logctx);
215  case SEI_TYPE_TIME_CODE:
216  return decode_nal_sei_timecode(&s->timecode, gb);
217  default: {
219  gb, gbyte, logctx);
221  av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
222  return ret;
223  }
224  }
225 }
226 
228  void *logctx, HEVCSEI *s, int type)
229 {
230  switch (type) {
232  return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
233  default:
234  av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
235  return 0;
236  }
237 }
238 
239 static int decode_nal_sei_message(GetByteContext *gb, void *logctx, HEVCSEI *s,
240  const HEVCParamSets *ps, int nal_unit_type)
241 {
242  GetByteContext message_gbyte;
243  GetBitContext message_gb;
244  int payload_type = 0;
245  int payload_size = 0;
246  int byte = 0xFF;
247  av_unused int ret;
248  av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n");
249 
250  while (byte == 0xFF) {
251  if (bytestream2_get_bytes_left(gb) < 2 || payload_type > INT_MAX - 255)
252  return AVERROR_INVALIDDATA;
253  byte = bytestream2_get_byteu(gb);
254  payload_type += byte;
255  }
256  byte = 0xFF;
257  while (byte == 0xFF) {
258  if (bytestream2_get_bytes_left(gb) < 1 + payload_size)
259  return AVERROR_INVALIDDATA;
260  byte = bytestream2_get_byteu(gb);
261  payload_size += byte;
262  }
263  if (bytestream2_get_bytes_left(gb) < payload_size)
264  return AVERROR_INVALIDDATA;
265  bytestream2_init(&message_gbyte, gb->buffer, payload_size);
266  ret = init_get_bits8(&message_gb, gb->buffer, payload_size);
267  av_assert1(ret >= 0);
268  bytestream2_skipu(gb, payload_size);
269  if (nal_unit_type == HEVC_NAL_SEI_PREFIX) {
270  return decode_nal_sei_prefix(&message_gb, &message_gbyte,
271  logctx, s, ps, payload_type);
272  } else { /* nal_unit_type == NAL_SEI_SUFFIX */
273  return decode_nal_sei_suffix(&message_gb, &message_gbyte,
274  logctx, s, payload_type);
275  }
276 }
277 
279  const HEVCParamSets *ps, enum HEVCNALUnitType type)
280 {
281  GetByteContext gbyte;
282  int ret;
283 
284  av_assert1((get_bits_count(gb) % 8) == 0);
285  bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
286  get_bits_left(gb) / 8);
287 
288  do {
289  ret = decode_nal_sei_message(&gbyte, logctx, s, ps, type);
290  if (ret < 0)
291  return ret;
292  } while (bytestream2_get_bytes_left(&gbyte) > 0);
293  return 1;
294 }
av_clip
#define av_clip
Definition: common.h:95
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:664
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
GetByteContext
Definition: bytestream.h:33
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
AV_PICTURE_STRUCTURE_UNKNOWN
@ AV_PICTURE_STRUCTURE_UNKNOWN
unknown
Definition: avcodec.h:2806
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:411
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:256
av_unused
#define av_unused
Definition: attributes.h:131
HEVC_MAX_SPS_COUNT
@ HEVC_MAX_SPS_COUNT
Definition: hevc.h:112
decode_nal_sei_decoded_picture_hash
static int decode_nal_sei_decoded_picture_hash(HEVCSEIPictureHash *s, GetByteContext *gb)
Definition: hevc_sei.c:30
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:395
HEVCSEITimeCode
Definition: hevc_sei.h:75
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:325
GetBitContext
Definition: get_bits.h:107
decode_nal_sei_prefix
static int decode_nal_sei_prefix(GetBitContext *gb, GetByteContext *gbyte, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, int type)
Definition: hevc_sei.c:200
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
decode_nal_sei_active_parameter_sets
static int decode_nal_sei_active_parameter_sets(HEVCSEI *s, GetBitContext *gb, void *logctx)
Definition: hevc_sei.c:129
ff_hevc_decode_nal_sei
int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, enum HEVCNALUnitType type)
Definition: hevc_sei.c:278
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:524
s
#define s(width, name)
Definition: cbs_vp9.c:256
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
HEVCSEI
Definition: hevc_sei.h:95
HEVCSEIMasteringDisplay
Definition: hevc_sei.h:56
decode_nal_sei_pic_timing
static int decode_nal_sei_pic_timing(HEVCSEI *s, GetBitContext *gb, const HEVCParamSets *ps, void *logctx)
Definition: hevc_sei.c:98
HEVCSEIPictureHash
Definition: hevc_sei.h:39
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING
@ HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING
Definition: hevc_sei.h:36
if
if(ret)
Definition: filter_design.txt:179
AV_PICTURE_STRUCTURE_BOTTOM_FIELD
@ AV_PICTURE_STRUCTURE_BOTTOM_FIELD
coded as bottom field
Definition: avcodec.h:2808
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:108
SEI_TYPE_TIME_CODE
@ SEI_TYPE_TIME_CODE
Definition: sei.h:95
AV_PICTURE_STRUCTURE_TOP_FIELD
@ AV_PICTURE_STRUCTURE_TOP_FIELD
coded as top field
Definition: avcodec.h:2807
FF_H2645_SEI_MESSAGE_UNHANDLED
@ FF_H2645_SEI_MESSAGE_UNHANDLED
Definition: h2645_sei.h:123
decode_nal_sei_content_light_info
static int decode_nal_sei_content_light_info(HEVCSEIContentLight *s, GetByteContext *gb)
Definition: hevc_sei.c:81
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
decode_nal_sei_message
static int decode_nal_sei_message(GetByteContext *gb, void *logctx, HEVCSEI *s, const HEVCParamSets *ps, int nal_unit_type)
Definition: hevc_sei.c:239
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
decode_nal_sei_timecode
static int decode_nal_sei_timecode(HEVCSEITimeCode *s, GetBitContext *gb)
Definition: hevc_sei.c:154
byte
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
Definition: bytestream.h:99
HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING
@ HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING
Definition: hevc_sei.h:35
SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME
@ SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME
Definition: sei.h:96
hevc_ps.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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:53
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
HEVCNALUnitType
HEVCNALUnitType
Table 7-1 – NAL unit type codes and NAL unit type classes in T-REC-H.265-201802.
Definition: hevc.h:28
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
decode_nal_sei_mastering_display_info
static int decode_nal_sei_mastering_display_info(HEVCSEIMasteringDisplay *s, GetByteContext *gb)
Definition: hevc_sei.c:52
HEVCParamSets::sps_list
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
Definition: hevc_ps.h:316
decode_nal_sei_suffix
static int decode_nal_sei_suffix(GetBitContext *gb, GetByteContext *gbyte, void *logctx, HEVCSEI *s, int type)
Definition: hevc_sei.c:227
SEI_TYPE_DECODED_PICTURE_HASH
@ SEI_TYPE_DECODED_PICTURE_HASH
Definition: sei.h:91
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
HEVCSPS
Definition: hevc_ps.h:140
HEVCSEIContentLight
Definition: hevc_sei.h:64
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
SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
@ SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO
Definition: sei.h:103
h
h
Definition: vp9dsp_template.c:2038
HEVCSEIPictureTiming
Definition: hevc_sei.h:52
HEVC_NAL_SEI_PREFIX
@ HEVC_NAL_SEI_PREFIX
Definition: hevc.h:68
SEI_TYPE_ACTIVE_PARAMETER_SETS
@ SEI_TYPE_ACTIVE_PARAMETER_SETS
Definition: sei.h:87
hevc_sei.h
HEVCParamSets
Definition: hevc_ps.h:314