FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "golomb.h"
26 #include "hevcdec.h"
27 
54 };
55 
57 {
58  int cIdx, i;
59  uint8_t hash_type;
60  //uint16_t picture_crc;
61  //uint32_t picture_checksum;
62  GetBitContext *gb = &s->HEVClc->gb;
63  hash_type = get_bits(gb, 8);
64 
65  for (cIdx = 0; cIdx < 3/*((s->sps->chroma_format_idc == 0) ? 1 : 3)*/; cIdx++) {
66  if (hash_type == 0) {
67  s->is_md5 = 1;
68  for (i = 0; i < 16; i++)
69  s->md5[cIdx][i] = get_bits(gb, 8);
70  } else if (hash_type == 1) {
71  // picture_crc = get_bits(gb, 16);
72  skip_bits(gb, 16);
73  } else if (hash_type == 2) {
74  // picture_checksum = get_bits_long(gb, 32);
75  skip_bits(gb, 32);
76  }
77  }
78  return 0;
79 }
80 
82 {
83  GetBitContext *gb = &s->HEVClc->gb;
84  int i;
85  // Mastering primaries
86  for (i = 0; i < 3; i++) {
87  s->display_primaries[i][0] = get_bits(gb, 16);
88  s->display_primaries[i][1] = get_bits(gb, 16);
89  }
90  // White point (x, y)
91  s->white_point[0] = get_bits(gb, 16);
92  s->white_point[1] = get_bits(gb, 16);
93 
94  // Max and min luminance of mastering display
97 
98  // As this SEI message comes before the first frame that references it,
99  // initialize the flag to 2 and decrement on IRAP access unit so it
100  // persists for the coded video sequence (e.g., between two IRAPs)
102  return 0;
103 }
104 
106 {
107  GetBitContext *gb = &s->HEVClc->gb;
108 
109  get_ue_golomb_long(gb); // frame_packing_arrangement_id
111 
112  if (s->sei_frame_packing_present) {
116 
117  // the following skips spatial_flipping_flag frame0_flipped_flag
118  // field_views_flag current_frame_is_frame0_flag
119  // frame0_self_contained_flag frame1_self_contained_flag
120  skip_bits(gb, 6);
121 
123  skip_bits(gb, 16); // frame[01]_grid_position_[xy]
124  skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
125  skip_bits1(gb); // frame_packing_arrangement_persistence_flag
126  }
127  skip_bits1(gb); // upsampled_aspect_ratio_flag
128  return 0;
129 }
130 
132 {
133  GetBitContext *gb = &s->HEVClc->gb;
134 
136 
138  s->sei_hflip = get_bits1(gb); // hor_flip
139  s->sei_vflip = get_bits1(gb); // ver_flip
140 
142  skip_bits1(gb); // display_orientation_persistence_flag
143  }
144 
145  return 0;
146 }
147 
149 {
150  GetBitContext *gb = &s->HEVClc->gb;
151  HEVCSPS *sps;
152 
154  return(AVERROR(ENOMEM));
156 
158  int pic_struct = get_bits(gb, 4);
160  if (pic_struct == 2) {
161  av_log(s->avctx, AV_LOG_DEBUG, "BOTTOM Field\n");
163  } else if (pic_struct == 1) {
164  av_log(s->avctx, AV_LOG_DEBUG, "TOP Field\n");
166  }
167  get_bits(gb, 2); // source_scan_type
168  get_bits(gb, 1); // duplicate_flag
169  skip_bits1(gb);
170  size--;
171  }
172  skip_bits_long(gb, 8 * size);
173 
174  return 0;
175 }
176 
178 {
179  int flag;
180  int user_data_type_code;
181  int cc_count;
182 
183  GetBitContext *gb = &s->HEVClc->gb;
184 
185  if (size < 3)
186  return AVERROR(EINVAL);
187 
188  user_data_type_code = get_bits(gb, 8);
189  if (user_data_type_code == 0x3) {
190  skip_bits(gb, 1); // reserved
191 
192  flag = get_bits(gb, 1); // process_cc_data_flag
193  if (flag) {
194  skip_bits(gb, 1);
195  cc_count = get_bits(gb, 5);
196  skip_bits(gb, 8); // reserved
197  size -= 2;
198 
199  if (cc_count && size >= cc_count * 3) {
200  const uint64_t new_size = (s->a53_caption_size + cc_count
201  * UINT64_C(3));
202  int i, ret;
203 
204  if (new_size > INT_MAX)
205  return AVERROR(EINVAL);
206 
207  /* Allow merging of the cc data from two fields. */
208  ret = av_reallocp(&s->a53_caption, new_size);
209  if (ret < 0)
210  return ret;
211 
212  for (i = 0; i < cc_count; i++) {
213  s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
214  s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
215  s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
216  }
217  skip_bits(gb, 8); // marker_bits
218  }
219  }
220  } else {
221  int i;
222  for (i = 0; i < size - 1; i++)
223  skip_bits(gb, 8);
224  }
225 
226  return 0;
227 }
228 
230 {
231  uint32_t country_code;
232  uint32_t user_identifier;
233 
234  GetBitContext *gb = &s->HEVClc->gb;
235 
236  if (size < 7)
237  return AVERROR(EINVAL);
238  size -= 7;
239 
240  country_code = get_bits(gb, 8);
241  if (country_code == 0xFF) {
242  skip_bits(gb, 8);
243  size--;
244  }
245 
246  skip_bits(gb, 8);
247  skip_bits(gb, 8);
248 
249  user_identifier = get_bits_long(gb, 32);
250 
251  switch (user_identifier) {
252  case MKBETAG('G', 'A', '9', '4'):
254  default:
255  skip_bits_long(gb, size * 8);
256  break;
257  }
258  return 0;
259 }
260 
262 {
263  GetBitContext *gb = &s->HEVClc->gb;
264  int num_sps_ids_minus1;
265  int i;
266  unsigned active_seq_parameter_set_id;
267 
268  get_bits(gb, 4); // active_video_parameter_set_id
269  get_bits(gb, 1); // self_contained_cvs_flag
270  get_bits(gb, 1); // num_sps_ids_minus1
271  num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
272 
273  if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
274  av_log(s->avctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
275  return AVERROR_INVALIDDATA;
276  }
277 
278  active_seq_parameter_set_id = get_ue_golomb_long(gb);
279  if (active_seq_parameter_set_id >= HEVC_MAX_SPS_COUNT) {
280  av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
281  return AVERROR_INVALIDDATA;
282  }
283  s->active_seq_parameter_set_id = active_seq_parameter_set_id;
284 
285  for (i = 1; i <= num_sps_ids_minus1; i++)
286  get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
287 
288  return 0;
289 }
290 
292 {
293  GetBitContext *gb = &s->HEVClc->gb;
294 
295  switch (type) {
296  case 256: // Mismatched value from HM 8.1
303  {
304  int ret = decode_pic_timing(s, size);
305  av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
306  return ret;
307  }
312  av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
313  return 0;
316  default:
317  av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
318  skip_bits_long(gb, 8 * size);
319  return 0;
320  }
321 }
322 
324 {
325  GetBitContext *gb = &s->HEVClc->gb;
326 
327  switch (type) {
330  default:
331  av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
332  skip_bits_long(gb, 8 * size);
333  return 0;
334  }
335 }
336 
338 {
339  GetBitContext *gb = &s->HEVClc->gb;
340 
341  int payload_type = 0;
342  int payload_size = 0;
343  int byte = 0xFF;
344  av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
345 
346  while (byte == 0xFF) {
347  if (get_bits_left(gb) < 16 || payload_type > INT_MAX - 255)
348  return AVERROR_INVALIDDATA;
349  byte = get_bits(gb, 8);
350  payload_type += byte;
351  }
352  byte = 0xFF;
353  while (byte == 0xFF) {
354  if (get_bits_left(gb) < 8 + 8LL*payload_size)
355  return AVERROR_INVALIDDATA;
356  byte = get_bits(gb, 8);
357  payload_size += byte;
358  }
360  return decode_nal_sei_prefix(s, payload_type, payload_size);
361  } else { /* nal_unit_type == NAL_SEI_SUFFIX */
362  return decode_nal_sei_suffix(s, payload_type, payload_size);
363  }
364 }
365 
367 {
368  return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
369 }
370 
372 {
373  int ret;
374 
375  do {
376  ret = decode_nal_sei_message(s);
377  if (ret < 0)
378  return(AVERROR(ENOMEM));
379  } while (more_rbsp_data(&s->HEVClc->gb));
380  return 1;
381 }
382 
384 {
385  s->a53_caption_size = 0;
386  av_freep(&s->a53_caption);
387 }
int frame_packing_arrangement_type
Definition: hevcdec.h:578
int sei_frame_packing_present
frame packing arrangement variables
Definition: hevcdec.h:577
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int quincunx_subsampling
Definition: hevcdec.h:580
int active_seq_parameter_set_id
Definition: hevcdec.h:571
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
int content_interpretation_type
Definition: hevcdec.h:579
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
int sei_hflip
Definition: hevcdec.h:585
VUI vui
Definition: hevc_ps.h:167
int flag
Definition: cpu.c:34
HEVCParamSets ps
Definition: hevcdec.h:492
static int decode_nal_sei_mastering_display_info(HEVCContext *s)
Definition: hevc_sei.c:81
HEVC_SEI_TYPE
Definition: hevc_sei.c:28
static int more_rbsp_data(GetBitContext *gb)
Definition: hevc_sei.c:366
uint32_t min_mastering_luminance
Definition: hevcdec.h:597
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
Definition: hevc_ps.h:313
uint8_t
enum HEVCNALUnitType nal_unit_type
Definition: hevcdec.h:503
void ff_hevc_reset_sei(HEVCContext *s)
Reset SEI values that are stored on the Context.
Definition: hevc_sei.c:383
AVCodecContext * avctx
Definition: hevcdec.h:469
int frame_field_info_present_flag
Definition: hevc_ps.h:69
int sei_anticlockwise_rotation
Definition: hevcdec.h:584
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:587
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int decode_nal_sei_decoded_picture_hash(HEVCContext *s)
Definition: hevc_sei.c:56
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
uint16_t display_primaries[3][2]
Definition: hevcdec.h:594
int picture_struct
Definition: hevcdec.h:587
static int decode_registered_user_data_closed_caption(HEVCContext *s, int size)
Definition: hevc_sei.c:177
static int decode_pic_timing(HEVCContext *s, int size)
Definition: hevc_sei.c:148
static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
Definition: hevc_sei.c:291
static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCContext *s, int size)
Definition: hevc_sei.c:229
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:296
int sei_mastering_display_info_present
mastering display
Definition: hevcdec.h:593
static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
Definition: hevc_sei.c:323
uint32_t max_mastering_luminance
Definition: hevcdec.h:596
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:85
int sei_display_orientation_present
display orientation
Definition: hevcdec.h:583
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition: mem.c:163
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:87
int ff_hevc_decode_nal_sei(HEVCContext *s)
Definition: hevc_sei.c:371
uint8_t md5[3][16]
Definition: hevcdec.h:563
uint8_t is_md5
Definition: hevcdec.h:564
uint8_t * data
The data buffer.
Definition: buffer.h:89
static int decode_nal_sei_display_orientation(HEVCContext *s)
Definition: hevc_sei.c:131
GLint GLenum type
Definition: opengl_enc.c:105
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:338
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
#define HEVC_MAX_SPS_COUNT
Definition: hevc.h:66
GetBitContext gb
Definition: hevcdec.h:431
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:346
HEVCLocalContext * HEVClc
Definition: hevcdec.h:474
static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
Definition: hevc_sei.c:105
int sei_vflip
Definition: hevcdec.h:585
if(ret< 0)
Definition: vf_mcdeint.c:282
uint8_t * a53_caption
Definition: hevcdec.h:589
static int active_parameter_sets(HEVCContext *s)
Definition: hevc_sei.c:261
#define MKBETAG(a, b, c, d)
Definition: common.h:343
uint16_t white_point[2]
Definition: hevcdec.h:595
static int decode_nal_sei_message(HEVCContext *s)
Definition: hevc_sei.c:337
#define av_freep(p)
exp golomb vlc stuff
int a53_caption_size
Definition: hevcdec.h:590