FFmpeg  2.1.1
hevc_parser.c
Go to the documentation of this file.
1 /*
2  * HEVC Annex B format parser
3  *
4  * Copyright (C) 2012 - 2013 Guillaume Martres
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include "libavutil/common.h"
24 #include "parser.h"
25 #include "hevc.h"
26 #include "golomb.h"
27 
28 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
29 
30 typedef struct HEVCParseContext {
34 
35 /**
36  * Find the end of the current frame in the bitstream.
37  * @return the position of the first byte of the next frame, or END_NOT_FOUND
38  */
39 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int buf_size)
40 {
41  int i;
42  ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
43 
44  for (i = 0; i < buf_size; i++) {
45  int nut;
46 
47  pc->state64 = (pc->state64 << 8) | buf[i];
48 
49  if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
50  continue;
51 
52  nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
53  // Beginning of access unit
54  if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
55  (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
56  if (pc->frame_start_found) {
57  pc->frame_start_found = 0;
58  return i - 5;
59  }
60  } else if (nut <= NAL_RASL_R ||
61  (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
62  int first_slice_segment_in_pic_flag = buf[i] >> 7;
63  if (first_slice_segment_in_pic_flag) {
64  if (!pc->frame_start_found) {
65  pc->frame_start_found = 1;
66  } else { // First slice of next frame found
67  pc->frame_start_found = 0;
68  return i - 5;
69  }
70  }
71  }
72  }
73 
74  return END_NOT_FOUND;
75 }
76 
77 /**
78  * Parse NAL units of found picture and decode some basic information.
79  *
80  * @param s parser context.
81  * @param avctx codec context.
82  * @param buf buffer with field/frame data.
83  * @param buf_size size of the buffer.
84  */
86  AVCodecContext *avctx,
87  const uint8_t *buf, int buf_size)
88 {
89  HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
90  GetBitContext *gb = &h->HEVClc->gb;
91  SliceHeader *sh = &h->sh;
92  const uint8_t *buf_end = buf + buf_size;
93  int state = -1, i;
94  HEVCNAL *nal;
95 
96  /* set some sane default values */
98  s->key_frame = 0;
100 
101  h->avctx = avctx;
102 
103  if (!buf_size)
104  return 0;
105 
106  if (h->nals_allocated < 1) {
107  HEVCNAL *tmp = av_realloc_array(h->nals, 1, sizeof(*tmp));
108  if (!tmp)
109  return AVERROR(ENOMEM);
110  h->nals = tmp;
111  memset(h->nals, 0, sizeof(*tmp));
112  h->nals_allocated = 1;
113  }
114 
115  nal = &h->nals[0];
116 
117  for (;;) {
118  int src_length, consumed;
119  buf = avpriv_find_start_code(buf, buf_end, &state);
120  if (--buf + 2 >= buf_end)
121  break;
122  src_length = buf_end - buf;
123 
124  h->nal_unit_type = (*buf >> 1) & 0x3f;
125  h->temporal_id = (*(buf + 1) & 0x07) - 1;
126  if (h->nal_unit_type <= NAL_CRA_NUT) {
127  // Do not walk the whole buffer just to decode slice segment header
128  if (src_length > 20)
129  src_length = 20;
130  }
131 
132  consumed = ff_hevc_extract_rbsp(h, buf, src_length, nal);
133  if (consumed < 0)
134  return consumed;
135 
136  init_get_bits8(gb, nal->data + 2, nal->size);
137  switch (h->nal_unit_type) {
138  case NAL_VPS:
140  break;
141  case NAL_SPS:
143  break;
144  case NAL_PPS:
146  break;
147  case NAL_SEI_PREFIX:
148  case NAL_SEI_SUFFIX:
150  break;
151  case NAL_TRAIL_N:
152  case NAL_TRAIL_R:
153  case NAL_TSA_N:
154  case NAL_TSA_R:
155  case NAL_STSA_N:
156  case NAL_STSA_R:
157  case NAL_RADL_N:
158  case NAL_RADL_R:
159  case NAL_RASL_N:
160  case NAL_RASL_R:
161  case NAL_BLA_W_LP:
162  case NAL_BLA_W_RADL:
163  case NAL_BLA_N_LP:
164  case NAL_IDR_W_RADL:
165  case NAL_IDR_N_LP:
166  case NAL_CRA_NUT:
168 
169  if (h->nal_unit_type >= 16 && h->nal_unit_type <= 23) {
170  s->key_frame = 1;
172  }
173 
174  sh->pps_id = get_ue_golomb(gb);
175  if (sh->pps_id >= MAX_PPS_COUNT || !h->pps_list[sh->pps_id]) {
176  av_log(h->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
177  return AVERROR_INVALIDDATA;
178  }
179  h->pps = (HEVCPPS*)h->pps_list[sh->pps_id]->data;
180 
181  if (h->pps->sps_id >= MAX_SPS_COUNT || !h->sps_list[h->pps->sps_id]) {
182  av_log(h->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", h->pps->sps_id);
183  return AVERROR_INVALIDDATA;
184  }
185  if (h->sps != (HEVCSPS*)h->sps_list[h->pps->sps_id]->data) {
186  h->sps = (HEVCSPS*)h->sps_list[h->pps->sps_id]->data;
187  h->vps = h->vps_list[h->sps->vps_id];
188  }
189 
190  if (!sh->first_slice_in_pic_flag) {
191  int slice_address_length;
192 
195  else
197 
198  slice_address_length = av_ceil_log2_c(h->sps->ctb_width *
199  h->sps->ctb_height);
200  sh->slice_segment_addr = get_bits(gb, slice_address_length);
201  if (sh->slice_segment_addr >= h->sps->ctb_width * h->sps->ctb_height) {
202  av_log(h->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
203  sh->slice_segment_addr);
204  return AVERROR_INVALIDDATA;
205  }
206  } else
208 
210  break;
211 
212  for (i = 0; i < h->pps->num_extra_slice_header_bits; i++)
213  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
214 
215  sh->slice_type = get_ue_golomb(gb);
216  if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
217  sh->slice_type == B_SLICE)) {
218  av_log(h->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
219  sh->slice_type);
220  return AVERROR_INVALIDDATA;
221  }
225 
227  sh->pic_output_flag = get_bits1(gb);
228 
230  sh->colour_plane_id = get_bits(gb, 2);
231 
232  if (!IS_IDR(h)) {
235  } else
236  s->output_picture_number = h->poc = 0;
237 
238  if (h->temporal_id == 0 &&
239  h->nal_unit_type != NAL_TRAIL_N &&
240  h->nal_unit_type != NAL_TSA_N &&
241  h->nal_unit_type != NAL_STSA_N &&
242  h->nal_unit_type != NAL_RADL_N &&
243  h->nal_unit_type != NAL_RASL_N &&
244  h->nal_unit_type != NAL_RADL_R &&
246  h->pocTid0 = h->poc;
247 
248  return 0; /* no need to evaluate the rest */
249  }
250  buf += consumed;
251  }
252  /* didn't find a picture! */
253  av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
254  return -1;
255 }
256 
258  AVCodecContext *avctx,
259  const uint8_t **poutbuf, int *poutbuf_size,
260  const uint8_t *buf, int buf_size)
261 {
262  int next;
263  ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
264 
266  next = buf_size;
267  } else {
268  next = hevc_find_frame_end(s, buf, buf_size);
269  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
270  *poutbuf = NULL;
271  *poutbuf_size = 0;
272  return buf_size;
273  }
274  }
275 
276  parse_nal_units(s, avctx, buf, buf_size);
277 
278  *poutbuf = buf;
279  *poutbuf_size = buf_size;
280  return next;
281 }
282 
283 // Split after the parameter sets at the beginning of the stream if they exist.
284 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
285 {
286  int i;
287  uint32_t state = -1;
288  int has_ps = 0;
289 
290  for (i = 0; i < buf_size; i++) {
291  state = (state << 8) | buf[i];
292  if (((state >> 8) & 0xFFFFFF) == START_CODE) {
293  int nut = (state >> 1) & 0x3F;
294  if (nut >= NAL_VPS && nut <= NAL_PPS) {
295  has_ps = 1;
296  } else if (has_ps) {
297  return i - 3;
298  } else { // no parameter set at the beginning of the stream
299  return 0;
300  }
301  }
302  }
303  return 0;
304 }
305 
307 {
308  HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
309  h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
310  h->skipped_bytes_pos_size = INT_MAX;
311 
312  return 0;
313 }
314 
316 {
317  int i;
318  HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
319  ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
320 
322  av_freep(&h->HEVClc);
323  av_freep(&pc->buffer);
324 
325  for (i = 0; i < FF_ARRAY_ELEMS(h->vps_list); i++)
326  av_freep(&h->vps_list[i]);
327  for (i = 0; i < FF_ARRAY_ELEMS(h->sps_list); i++)
328  av_buffer_unref(&h->sps_list[i]);
329  for (i = 0; i < FF_ARRAY_ELEMS(h->pps_list); i++)
330  av_buffer_unref(&h->pps_list[i]);
331 
332  for (i = 0; i < h->nals_allocated; i++)
333  av_freep(&h->nals[i].rbsp_buffer);
334  av_freep(&h->nals);
335  h->nals_allocated = 0;
336 }
337 
340  .priv_data_size = sizeof(HEVCParseContext),
341  .parser_init = hevc_init,
342  .parser_parse = hevc_parse,
343  .parser_close = hevc_close,
344  .split = hevc_split,
345 };
int nals_allocated
Definition: hevc.h:859
VPS * vps_list[MAX_VPS_COUNT]
Definition: hevc.h:783
VPS * vps
Definition: hevc.h:780
Definition: h264.h:112
const char * s
Definition: avisynth_c.h:668
int pic_order_cnt_lsb
Definition: hevc.h:534
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:105
Definition: h264.h:113
int ctb_height
Definition: hevc.h:441
int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevc.h:525
Definition: hevc.h:106
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:255
int vps_id
Definition: hevc.h:376
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:284
#define MAX_PPS_COUNT
Definition: h264.h:43
static int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Parse NAL units of found picture and decode some basic information.
Definition: hevc_parser.c:85
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
#define IS_IDR(s)
Definition: hevc.h:82
int * skipped_bytes_pos
Definition: hevc.h:848
Definition: hevc.h:129
uint8_t dependent_slice_segment_flag
Definition: hevc.h:537
int frame_start_found
Definition: parser.h:34
int ff_hevc_decode_nal_sei(HEVCContext *s)
Definition: hevc_sei.c:123
Predicted.
Definition: avcodec.h:2305
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevc.h:528
enum AVPictureStructure picture_structure
Indicate whether a picture is coded as a frame, top field or bottom field.
Definition: avcodec.h:4072
int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length, HEVCNAL *nal)
Definition: hevc.c:2208
uint8_t
const uint8_t * data
Definition: hevc.h:720
static av_always_inline av_const int av_ceil_log2_c(int x)
Compute ceil(log2(x)).
Definition: common.h:256
int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
Compute POC of the current frame and return it.
Definition: hevc_refs.c:442
uint8_t * rbsp_buffer
Definition: hevc.h:716
ParseContext pc
Definition: hevc_parser.c:32
AVCodecContext * avctx
Definition: hevc.h:758
HEVCNAL * nals
Definition: hevc.h:857
uint8_t first_slice_in_pic_flag
Definition: hevc.h:536
uint8_t pic_output_flag
Definition: hevc.h:538
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
int temporal_id
temporal_id_plus1 - 1
Definition: hevc.h:797
uint8_t no_output_of_prior_pics_flag
Definition: hevc.h:548
static int hevc_init(AVCodecParserContext *s)
Definition: hevc_parser.c:306
const HEVCSPS * sps
Definition: hevc.h:781
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevc.h:539
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
static int get_ue_golomb(GetBitContext *gb)
read unsigned exp golomb code.
Definition: golomb.h:53
unsigned int log2_max_poc_lsb
Definition: hevc.h:390
Definition: hevc.h:92
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:216
Definition: h264.h:111
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3961
static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf, int buf_size)
Find the end of the current frame in the bitstream.
Definition: hevc_parser.c:39
static char * split(char *message, char delim)
Definition: af_channelmap.c:82
#define FF_ARRAY_ELEMS(a)
Definition: avcodec.h:929
uint8_t * data
The data buffer.
Definition: buffer.h:89
Definition: hevc.h:127
int size
Definition: hevc.h:719
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: hevc.h:784
#define MAX_SPS_COUNT
Definition: h264.h:42
int ctb_width
Definition: hevc.h:440
HEVCPPS * pps
Definition: hevc.h:782
uint8_t output_flag_present_flag
Definition: hevc.h:478
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:203
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: hevc.h:785
int sps_id
seq_parameter_set_id
Definition: hevc.h:457
Definition: hevc.h:93
Definition: hevc.h:375
HEVCContext h
Definition: hevc_parser.c:31
Definition: hevc.h:456
uint8_t * buffer
Definition: parser.h:29
Definition: hevc.h:715
static void hevc_close(AVCodecParserContext *s)
Definition: hevc_parser.c:315
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:436
#define START_CODE
start_code_prefix_one_3bytes
Definition: hevc_parser.c:28
int codec_ids[5]
Definition: avcodec.h:4084
main external API structure.
Definition: avcodec.h:1146
int num_extra_slice_header_bits
Definition: hevc.h:503
void * buf
Definition: avisynth_c.h:594
AVCodecParser ff_hevc_parser
Definition: hevc_parser.c:338
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:299
Bi-dir predicted.
Definition: avcodec.h:2306
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:292
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
GetBitContext gb
Definition: hevc.h:729
int poc
Definition: hevc.h:800
#define END_NOT_FOUND
Definition: parser.h:40
enum NALUnitType nal_unit_type
Definition: hevc.h:796
int pocTid0
Definition: hevc.h:801
HEVCLocalContext * HEVClc
Definition: hevc.h:763
static uint32_t state
Definition: trasher.c:27
int output_picture_number
Picture number incremented in presentation or output order.
Definition: avcodec.h:4080
int skipped_bytes_pos_size
Definition: hevc.h:849
int ff_hevc_decode_nal_sps(HEVCContext *s)
Definition: hevc_ps.c:596
#define AVERROR_INVALIDDATA
int ff_hevc_decode_nal_pps(HEVCContext *s)
Definition: hevc_ps.c:961
int ff_hevc_decode_nal_vps(HEVCContext *s)
Definition: hevc_ps.c:321
#define AVERROR(e)
static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:257
Definition: hevc.h:128
enum SliceType slice_type
Definition: hevc.h:532
SliceHeader sh
Definition: hevc.h:793
exp golomb vlc stuff
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3976
uint8_t separate_colour_plane_flag
output (i.e. cropped) values
Definition: hevc.h:378
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc.h:481