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 "hevc.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  }
170  return 1;
171 }
172 
174 {
175  int flag;
176  int user_data_type_code;
177  int cc_count;
178 
179  GetBitContext *gb = &s->HEVClc->gb;
180 
181  if (size < 3)
182  return AVERROR(EINVAL);
183 
184  user_data_type_code = get_bits(gb, 8);
185  if (user_data_type_code == 0x3) {
186  skip_bits(gb, 1); // reserved
187 
188  flag = get_bits(gb, 1); // process_cc_data_flag
189  if (flag) {
190  skip_bits(gb, 1);
191  cc_count = get_bits(gb, 5);
192  skip_bits(gb, 8); // reserved
193  size -= 2;
194 
195  if (cc_count && size >= cc_count * 3) {
196  const uint64_t new_size = (s->a53_caption_size + cc_count
197  * UINT64_C(3));
198  int i, ret;
199 
200  if (new_size > INT_MAX)
201  return AVERROR(EINVAL);
202 
203  /* Allow merging of the cc data from two fields. */
204  ret = av_reallocp(&s->a53_caption, new_size);
205  if (ret < 0)
206  return ret;
207 
208  for (i = 0; i < cc_count; i++) {
209  s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
210  s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
211  s->a53_caption[s->a53_caption_size++] = get_bits(gb, 8);
212  }
213  skip_bits(gb, 8); // marker_bits
214  }
215  }
216  } else {
217  int i;
218  for (i = 0; i < size - 1; i++)
219  skip_bits(gb, 8);
220  }
221 
222  return 0;
223 }
224 
226 {
227  uint32_t country_code;
228  uint32_t user_identifier;
229 
230  GetBitContext *gb = &s->HEVClc->gb;
231 
232  if (size < 7)
233  return AVERROR(EINVAL);
234  size -= 7;
235 
236  country_code = get_bits(gb, 8);
237  if (country_code == 0xFF) {
238  skip_bits(gb, 8);
239  size--;
240  }
241 
242  skip_bits(gb, 8);
243  skip_bits(gb, 8);
244 
245  user_identifier = get_bits_long(gb, 32);
246 
247  switch (user_identifier) {
248  case MKBETAG('G', 'A', '9', '4'):
250  default:
251  skip_bits_long(gb, size * 8);
252  break;
253  }
254  return 0;
255 }
256 
258 {
259  GetBitContext *gb = &s->HEVClc->gb;
260  int num_sps_ids_minus1;
261  int i;
262  unsigned active_seq_parameter_set_id;
263 
264  get_bits(gb, 4); // active_video_parameter_set_id
265  get_bits(gb, 1); // self_contained_cvs_flag
266  get_bits(gb, 1); // num_sps_ids_minus1
267  num_sps_ids_minus1 = get_ue_golomb_long(gb); // num_sps_ids_minus1
268 
269  if (num_sps_ids_minus1 < 0 || num_sps_ids_minus1 > 15) {
270  av_log(s->avctx, AV_LOG_ERROR, "num_sps_ids_minus1 %d invalid\n", num_sps_ids_minus1);
271  return AVERROR_INVALIDDATA;
272  }
273 
274  active_seq_parameter_set_id = get_ue_golomb_long(gb);
275  if (active_seq_parameter_set_id >= MAX_SPS_COUNT) {
276  av_log(s->avctx, AV_LOG_ERROR, "active_parameter_set_id %d invalid\n", active_seq_parameter_set_id);
277  return AVERROR_INVALIDDATA;
278  }
279  s->active_seq_parameter_set_id = active_seq_parameter_set_id;
280 
281  for (i = 1; i <= num_sps_ids_minus1; i++)
282  get_ue_golomb_long(gb); // active_seq_parameter_set_id[i]
283 
284  return 0;
285 }
286 
288 {
289  GetBitContext *gb = &s->HEVClc->gb;
290 
291  switch (type) {
292  case 256: // Mismatched value from HM 8.1
299  {
300  int ret = decode_pic_timing(s);
301  av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
302  skip_bits(gb, 8 * size);
303  return ret;
304  }
309  av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
310  return 0;
313  default:
314  av_log(s->avctx, AV_LOG_DEBUG, "Skipped PREFIX SEI %d\n", type);
315  skip_bits_long(gb, 8 * size);
316  return 0;
317  }
318 }
319 
321 {
322  GetBitContext *gb = &s->HEVClc->gb;
323 
324  switch (type) {
327  default:
328  av_log(s->avctx, AV_LOG_DEBUG, "Skipped SUFFIX SEI %d\n", type);
329  skip_bits_long(gb, 8 * size);
330  return 0;
331  }
332 }
333 
335 {
336  GetBitContext *gb = &s->HEVClc->gb;
337 
338  int payload_type = 0;
339  int payload_size = 0;
340  int byte = 0xFF;
341  av_log(s->avctx, AV_LOG_DEBUG, "Decoding SEI\n");
342 
343  while (byte == 0xFF) {
344  byte = get_bits(gb, 8);
345  payload_type += byte;
346  }
347  byte = 0xFF;
348  while (byte == 0xFF) {
349  byte = get_bits(gb, 8);
350  payload_size += byte;
351  }
352  if (s->nal_unit_type == NAL_SEI_PREFIX) {
353  return decode_nal_sei_prefix(s, payload_type, payload_size);
354  } else { /* nal_unit_type == NAL_SEI_SUFFIX */
355  return decode_nal_sei_suffix(s, payload_type, payload_size);
356  }
357  return 1;
358 }
359 
361 {
362  return get_bits_left(gb) > 0 && show_bits(gb, 8) != 0x80;
363 }
364 
366 {
367  int ret;
368 
369  do {
370  ret = decode_nal_sei_message(s);
371  if (ret < 0)
372  return(AVERROR(ENOMEM));
373  } while (more_rbsp_data(&s->HEVClc->gb));
374  return 1;
375 }
376 
378 {
379  s->a53_caption_size = 0;
380  av_freep(&s->a53_caption);
381 }
int frame_packing_arrangement_type
Definition: hevc.h:904
int sei_frame_packing_present
frame packing arrangement variables
Definition: hevc.h:903
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int quincunx_subsampling
Definition: hevc.h:906
int active_seq_parameter_set_id
Definition: hevc.h:897
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:247
int content_interpretation_type
Definition: hevc.h:905
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:204
int sei_hflip
Definition: hevc.h:911
VUI vui
Definition: hevc.h:424
int flag
Definition: cpu.c:34
HEVCParamSets ps
Definition: hevc.h:818
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:360
uint32_t min_mastering_luminance
Definition: hevc.h:923
uint8_t
void ff_hevc_reset_sei(HEVCContext *s)
Reset SEI values that are stored on the Context.
Definition: hevc_sei.c:377
AVCodecContext * avctx
Definition: hevc.h:795
int frame_field_info_present_flag
Definition: hevc.h:329
int sei_anticlockwise_rotation
Definition: hevc.h:910
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:568
#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: hevc.h:920
int picture_struct
Definition: hevc.h:913
static int decode_registered_user_data_closed_caption(HEVCContext *s, int size)
Definition: hevc_sei.c:173
static int decode_nal_sei_prefix(HEVCContext *s, int type, int size)
Definition: hevc_sei.c:287
static int decode_nal_sei_user_data_registered_itu_t_t35(HEVCContext *s, int size)
Definition: hevc_sei.c:225
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:282
int sei_mastering_display_info_present
mastering display
Definition: hevc.h:919
#define MAX_SPS_COUNT
Definition: h264.h:53
static int decode_nal_sei_suffix(HEVCContext *s, int type, int size)
Definition: hevc_sei.c:320
uint32_t max_mastering_luminance
Definition: hevc.h:922
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
Definition: hevc.h:399
int sei_display_orientation_present
display orientation
Definition: hevc.h:909
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:365
uint8_t md5[3][16]
Definition: hevc.h:889
uint8_t is_md5
Definition: hevc.h:890
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:299
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:324
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
GetBitContext gb
Definition: hevc.h:757
enum NALUnitType nal_unit_type
Definition: hevc.h:829
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:332
HEVCLocalContext * HEVClc
Definition: hevc.h:800
static int decode_nal_sei_frame_packing_arrangement(HEVCContext *s)
Definition: hevc_sei.c:105
int sei_vflip
Definition: hevc.h:911
if(ret< 0)
Definition: vf_mcdeint.c:282
uint8_t * a53_caption
Definition: hevc.h:915
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:187
static int active_parameter_sets(HEVCContext *s)
Definition: hevc_sei.c:257
#define MKBETAG(a, b, c, d)
Definition: common.h:343
uint16_t white_point[2]
Definition: hevc.h:921
static int decode_nal_sei_message(HEVCContext *s)
Definition: hevc_sei.c:334
static int decode_pic_timing(HEVCContext *s)
Definition: hevc_sei.c:148
#define av_freep(p)
exp golomb vlc stuff
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: hevc.h:564
int a53_caption_size
Definition: hevc.h:916