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  const HEVCParamSets *ps, void *logctx)
54 {
55  HEVCSEIPictureTiming *h = &s->picture_timing;
56  HEVCSPS *sps;
57 
58  if (!ps->sps_list[s->active_seq_parameter_set_id])
59  return(AVERROR(ENOMEM));
60  sps = (HEVCSPS*)ps->sps_list[s->active_seq_parameter_set_id]->data;
61 
62  if (sps->vui.frame_field_info_present_flag) {
63  int pic_struct = get_bits(gb, 4);
64  h->picture_struct = AV_PICTURE_STRUCTURE_UNKNOWN;
65  if (pic_struct == 2 || pic_struct == 10 || pic_struct == 12) {
66  av_log(logctx, AV_LOG_DEBUG, "BOTTOM Field\n");
67  h->picture_struct = AV_PICTURE_STRUCTURE_BOTTOM_FIELD;
68  } else if (pic_struct == 1 || pic_struct == 9 || pic_struct == 11) {
69  av_log(logctx, AV_LOG_DEBUG, "TOP Field\n");
70  h->picture_struct = AV_PICTURE_STRUCTURE_TOP_FIELD;
71  } else if (pic_struct == 7) {
72  av_log(logctx, AV_LOG_DEBUG, "Frame/Field Doubling\n");
73  h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_DOUBLING;
74  } else if (pic_struct == 8) {
75  av_log(logctx, AV_LOG_DEBUG, "Frame/Field Tripling\n");
76  h->picture_struct = HEVC_SEI_PIC_STRUCT_FRAME_TRIPLING;
77  }
78  }
79 
80  return 0;
81 }
82 
84 {
85  int num_sps_ids_minus1;
86  unsigned active_seq_parameter_set_id;
87 
88  get_bits(gb, 4); // active_video_parameter_set_id
89  get_bits(gb, 1); // self_contained_cvs_flag
90  get_bits(gb, 1); // num_sps_ids_minus1
91  num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
92 
93  if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
94  av_log(logctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
95  return AVERROR_INVALIDDATA;
96  }
97 
98  active_seq_parameter_set_id = get_ue_golomb_long(gb);
99  if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
100  av_log(logctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
101  return AVERROR_INVALIDDATA;
102  }
103  s->active_seq_parameter_set_id = active_seq_parameter_set_id;
104 
105  return 0;
106 }
107 
109 {
110  s->num_clock_ts = get_bits(gb, 2);
111 
112  for (int i = 0; i < s->num_clock_ts; i++) {
113  s->clock_timestamp_flag[i] = get_bits(gb, 1);
114 
115  if (s->clock_timestamp_flag[i]) {
116  s->units_field_based_flag[i] = get_bits(gb, 1);
117  s->counting_type[i] = get_bits(gb, 5);
118  s->full_timestamp_flag[i] = get_bits(gb, 1);
119  s->discontinuity_flag[i] = get_bits(gb, 1);
120  s->cnt_dropped_flag[i] = get_bits(gb, 1);
121 
122  s->n_frames[i] = get_bits(gb, 9);
123 
124  if (s->full_timestamp_flag[i]) {
125  s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
126  s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
127  s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
128  } else {
129  s->seconds_flag[i] = get_bits(gb, 1);
130  if (s->seconds_flag[i]) {
131  s->seconds_value[i] = av_clip(get_bits(gb, 6), 0, 59);
132  s->minutes_flag[i] = get_bits(gb, 1);
133  if (s->minutes_flag[i]) {
134  s->minutes_value[i] = av_clip(get_bits(gb, 6), 0, 59);
135  s->hours_flag[i] = get_bits(gb, 1);
136  if (s->hours_flag[i]) {
137  s->hours_value[i] = av_clip(get_bits(gb, 5), 0, 23);
138  }
139  }
140  }
141  }
142 
143  s->time_offset_length[i] = get_bits(gb, 5);
144  if (s->time_offset_length[i] > 0) {
145  s->time_offset_value[i] = get_bits_long(gb, s->time_offset_length[i]);
146  }
147  }
148  }
149 
150  s->present = 1;
151  return 0;
152 }
153 
155  void *logctx, HEVCSEI *s,
156  const HEVCParamSets *ps, int type)
157 {
158  switch (type) {
159  case 256: // Mismatched value from HM 8.1
160  return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
161  case SEI_TYPE_PIC_TIMING:
162  return decode_nal_sei_pic_timing(s, gb, ps, logctx);
164  return decode_nal_sei_active_parameter_sets(s, gb, logctx);
165  case SEI_TYPE_TIME_CODE:
166  return decode_nal_sei_timecode(&s->timecode, gb);
167  default: {
169  gb, gbyte, logctx);
171  av_log(logctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
172  return ret;
173  }
174  }
175 }
176 
178  void *logctx, HEVCSEI *s, int type)
179 {
180  switch (type) {
182  return decode_nal_sei_decoded_picture_hash(&s->picture_hash, gbyte);
183  default:
184  av_log(logctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
185  return 0;
186  }
187 }
188 
189 static int decode_nal_sei_message(GetByteContext *gb, void *logctx, HEVCSEI *s,
190  const HEVCParamSets *ps, int nal_unit_type)
191 {
192  GetByteContext message_gbyte;
193  GetBitContext message_gb;
194  int payload_type = 0;
195  int payload_size = 0;
196  int byte = 0xFF;
197  av_unused int ret;
198  av_log(logctx, AV_LOG_DEBUG, "Decoding SEI\n");
199 
200  while (byte == 0xFF) {
201  if (bytestream2_get_bytes_left(gb) < 2 || payload_type > INT_MAX - 255)
202  return AVERROR_INVALIDDATA;
203  byte = bytestream2_get_byteu(gb);
204  payload_type += byte;
205  }
206  byte = 0xFF;
207  while (byte == 0xFF) {
208  if (bytestream2_get_bytes_left(gb) < 1 + payload_size)
209  return AVERROR_INVALIDDATA;
210  byte = bytestream2_get_byteu(gb);
211  payload_size += byte;
212  }
213  if (bytestream2_get_bytes_left(gb) < payload_size)
214  return AVERROR_INVALIDDATA;
215  bytestream2_init(&message_gbyte, gb->buffer, payload_size);
216  ret = init_get_bits8(&message_gb, gb->buffer, payload_size);
217  av_assert1(ret >= 0);
218  bytestream2_skipu(gb, payload_size);
219  if (nal_unit_type == HEVC_NAL_SEI_PREFIX) {
220  return decode_nal_sei_prefix(&message_gb, &message_gbyte,
221  logctx, s, ps, payload_type);
222  } else { /* nal_unit_type == NAL_SEI_SUFFIX */
223  return decode_nal_sei_suffix(&message_gb, &message_gbyte,
224  logctx, s, payload_type);
225  }
226 }
227 
229  const HEVCParamSets *ps, enum HEVCNALUnitType type)
230 {
231  GetByteContext gbyte;
232  int ret;
233 
234  av_assert1((get_bits_count(gb) % 8) == 0);
235  bytestream2_init(&gbyte, gb->buffer + get_bits_count(gb) / 8,
236  get_bits_left(gb) / 8);
237 
238  do {
239  ret = decode_nal_sei_message(&gbyte, logctx, s, ps, type);
240  if (ret < 0)
241  return ret;
242  } while (bytestream2_get_bytes_left(&gbyte) > 0);
243  return 1;
244 }
FF_H2645_SEI_MESSAGE_UNHANDLED
@ FF_H2645_SEI_MESSAGE_UNHANDLED
Definition: h2645_sei.h:139
av_clip
#define av_clip
Definition: common.h:96
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
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:2762
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
HEVC_MAX_SPS_COUNT
@ HEVC_MAX_SPS_COUNT
Definition: hevc.h:112
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
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:442
HEVCSEITimeCode
Definition: hevc_sei.h:61
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
GetBitContext
Definition: get_bits.h:108
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:154
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:83
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:228
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:545
s
#define s(width, name)
Definition: cbs_vp9.c:198
GetByteContext::buffer
const uint8_t * buffer
Definition: bytestream.h:34
HEVCSEI
Definition: hevc_sei.h:81
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:52
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:2764
GetBitContext::buffer
const uint8_t * buffer
Definition: get_bits.h:109
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:2763
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:189
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:108
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
hevc_ps.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:244
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
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
HEVCParamSets::sps_list
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
Definition: hevc_ps.h:441
decode_nal_sei_suffix
static int decode_nal_sei_suffix(GetBitContext *gb, GetByteContext *gbyte, void *logctx, HEVCSEI *s, int type)
Definition: hevc_sei.c:177
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:187
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: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:439