FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
25 #include "parser.h"
26 #include "hevc.h"
27 #include "golomb.h"
28 
29 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
30 
31 typedef struct HEVCParseContext {
35 
36 /**
37  * Find the end of the current frame in the bitstream.
38  * @return the position of the first byte of the next frame, or END_NOT_FOUND
39  */
41  int buf_size)
42 {
43  int i;
44  ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
45 
46  for (i = 0; i < buf_size; i++) {
47  int nut;
48 
49  pc->state64 = (pc->state64 << 8) | buf[i];
50 
51  if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
52  continue;
53 
54  nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
55  // Beginning of access unit
56  if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
57  (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
58  if (pc->frame_start_found) {
59  pc->frame_start_found = 0;
60  return i - 5;
61  }
62  } else if (nut <= NAL_RASL_R ||
63  (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
64  int first_slice_segment_in_pic_flag = buf[i] >> 7;
65  if (first_slice_segment_in_pic_flag) {
66  if (!pc->frame_start_found) {
67  pc->frame_start_found = 1;
68  } else { // First slice of next frame found
69  pc->frame_start_found = 0;
70  return i - 5;
71  }
72  }
73  }
74  }
75 
76  return END_NOT_FOUND;
77 }
78 
79 /**
80  * Parse NAL units of found picture and decode some basic information.
81  *
82  * @param s parser context.
83  * @param avctx codec context.
84  * @param buf buffer with field/frame data.
85  * @param buf_size size of the buffer.
86  */
88  const uint8_t *buf, int buf_size)
89 {
90  HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
91  GetBitContext *gb = &h->HEVClc->gb;
92  SliceHeader *sh = &h->sh;
93  const uint8_t *buf_end = buf + buf_size;
94  int state = -1, i;
95  HEVCNAL *nal;
96 
97  /* set some sane default values */
99  s->key_frame = 0;
101 
102  h->avctx = avctx;
103 
104  if (!buf_size)
105  return 0;
106 
107  if (h->nals_allocated < 1) {
108  HEVCNAL *tmp = av_realloc_array(h->nals, 1, sizeof(*tmp));
109  if (!tmp)
110  return AVERROR(ENOMEM);
111  h->nals = tmp;
112  memset(h->nals, 0, sizeof(*tmp));
113  h->nals_allocated = 1;
114  }
115 
116  nal = &h->nals[0];
117 
118  for (;;) {
119  int src_length, consumed;
120  buf = avpriv_find_start_code(buf, buf_end, &state);
121  if (--buf + 2 >= buf_end)
122  break;
123  src_length = buf_end - buf;
124 
125  h->nal_unit_type = (*buf >> 1) & 0x3f;
126  h->temporal_id = (*(buf + 1) & 0x07) - 1;
127  if (h->nal_unit_type <= NAL_CRA_NUT) {
128  // Do not walk the whole buffer just to decode slice segment header
129  if (src_length > 20)
130  src_length = 20;
131  }
132 
133  consumed = ff_hevc_extract_rbsp(h, buf, src_length, nal);
134  if (consumed < 0)
135  return consumed;
136 
137  init_get_bits8(gb, nal->data + 2, nal->size);
138  switch (h->nal_unit_type) {
139  case NAL_VPS:
141  break;
142  case NAL_SPS:
144  break;
145  case NAL_PPS:
147  break;
148  case NAL_SEI_PREFIX:
149  case NAL_SEI_SUFFIX:
151  break;
152  case NAL_TRAIL_N:
153  case NAL_TRAIL_R:
154  case NAL_TSA_N:
155  case NAL_TSA_R:
156  case NAL_STSA_N:
157  case NAL_STSA_R:
158  case NAL_RADL_N:
159  case NAL_RADL_R:
160  case NAL_RASL_N:
161  case NAL_RASL_R:
162  case NAL_BLA_W_LP:
163  case NAL_BLA_W_RADL:
164  case NAL_BLA_N_LP:
165  case NAL_IDR_W_RADL:
166  case NAL_IDR_N_LP:
167  case NAL_CRA_NUT:
170  s->field_order = h->picture_struct;
171 
172  if (IS_IRAP(h)) {
173  s->key_frame = 1;
175  }
176 
177  sh->pps_id = get_ue_golomb(gb);
178  if (sh->pps_id >= MAX_PPS_COUNT || !h->pps_list[sh->pps_id]) {
179  av_log(h->avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
180  return AVERROR_INVALIDDATA;
181  }
182  h->pps = (HEVCPPS*)h->pps_list[sh->pps_id]->data;
183 
184  if (h->pps->sps_id >= MAX_SPS_COUNT || !h->sps_list[h->pps->sps_id]) {
185  av_log(h->avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", h->pps->sps_id);
186  return AVERROR_INVALIDDATA;
187  }
188  if (h->sps != (HEVCSPS*)h->sps_list[h->pps->sps_id]->data) {
189  h->sps = (HEVCSPS*)h->sps_list[h->pps->sps_id]->data;
190  h->vps = (HEVCVPS*)h->vps_list[h->sps->vps_id]->data;
191  }
192 
193  if (!sh->first_slice_in_pic_flag) {
194  int slice_address_length;
195 
198  else
200 
201  slice_address_length = av_ceil_log2_c(h->sps->ctb_width *
202  h->sps->ctb_height);
203  sh->slice_segment_addr = get_bits(gb, slice_address_length);
204  if (sh->slice_segment_addr >= h->sps->ctb_width * h->sps->ctb_height) {
205  av_log(h->avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
206  sh->slice_segment_addr);
207  return AVERROR_INVALIDDATA;
208  }
209  } else
211 
213  break;
214 
215  for (i = 0; i < h->pps->num_extra_slice_header_bits; i++)
216  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
217 
218  sh->slice_type = get_ue_golomb(gb);
219  if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
220  sh->slice_type == B_SLICE)) {
221  av_log(h->avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
222  sh->slice_type);
223  return AVERROR_INVALIDDATA;
224  }
228 
230  sh->pic_output_flag = get_bits1(gb);
231 
233  sh->colour_plane_id = get_bits(gb, 2);
234 
235  if (!IS_IDR(h)) {
238  } else
239  s->output_picture_number = h->poc = 0;
240 
241  if (h->temporal_id == 0 &&
242  h->nal_unit_type != NAL_TRAIL_N &&
243  h->nal_unit_type != NAL_TSA_N &&
244  h->nal_unit_type != NAL_STSA_N &&
245  h->nal_unit_type != NAL_RADL_N &&
246  h->nal_unit_type != NAL_RASL_N &&
247  h->nal_unit_type != NAL_RADL_R &&
249  h->pocTid0 = h->poc;
250 
251  return 0; /* no need to evaluate the rest */
252  }
253  buf += consumed;
254  }
255  /* didn't find a picture! */
256  av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
257  return -1;
258 }
259 
261  AVCodecContext *avctx,
262  const uint8_t **poutbuf, int *poutbuf_size,
263  const uint8_t *buf, int buf_size)
264 {
265  int next;
266  ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
267 
269  next = buf_size;
270  } else {
271  next = hevc_find_frame_end(s, buf, buf_size);
272  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
273  *poutbuf = NULL;
274  *poutbuf_size = 0;
275  return buf_size;
276  }
277  }
278 
279  parse_nal_units(s, avctx, buf, buf_size);
280 
281  *poutbuf = buf;
282  *poutbuf_size = buf_size;
283  return next;
284 }
285 
286 // Split after the parameter sets at the beginning of the stream if they exist.
287 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
288 {
289  const uint8_t *ptr = buf, *end = buf + buf_size;
290  uint32_t state = -1;
291  int has_ps = 0, nut;
292 
293  while (ptr < end) {
294  ptr = avpriv_find_start_code(ptr, end, &state);
295  if ((state >> 8) != START_CODE)
296  break;
297  nut = (state >> 1) & 0x3F;
298  if (nut >= NAL_VPS && nut <= NAL_PPS)
299  has_ps = 1;
300  else if (has_ps)
301  return ptr - 4 - buf;
302  else // no parameter set at the beginning of the stream
303  return 0;
304  }
305  return 0;
306 }
307 
309 {
310  HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
311  h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
312  if (!h->HEVClc)
313  return AVERROR(ENOMEM);
314  h->skipped_bytes_pos_size = INT_MAX;
315 
316  return 0;
317 }
318 
320 {
321  int i;
322  HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
323  ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
324 
326  av_freep(&h->HEVClc);
327  av_freep(&pc->buffer);
328 
329  for (i = 0; i < FF_ARRAY_ELEMS(h->vps_list); i++)
330  av_buffer_unref(&h->vps_list[i]);
331  for (i = 0; i < FF_ARRAY_ELEMS(h->sps_list); i++)
332  av_buffer_unref(&h->sps_list[i]);
333  for (i = 0; i < FF_ARRAY_ELEMS(h->pps_list); i++)
334  av_buffer_unref(&h->pps_list[i]);
335 
336  h->sps = NULL;
337 
338  for (i = 0; i < h->nals_allocated; i++)
339  av_freep(&h->nals[i].rbsp_buffer);
340  av_freep(&h->nals);
341  h->nals_allocated = 0;
342 }
343 
346  .priv_data_size = sizeof(HEVCParseContext),
347  .parser_init = hevc_init,
348  .parser_parse = hevc_parse,
349  .parser_close = hevc_close,
350  .split = hevc_split,
351 };
int nals_allocated
Definition: hevc.h:893
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int pic_order_cnt_lsb
Definition: hevc.h:570
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:124
int ctb_height
Definition: hevc.h:470
Definition: hevc.h:96
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
int ff_hevc_decode_nal_sps(HEVCContext *s)
Definition: hevc_ps.c:752
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:287
#define MAX_PPS_COUNT
Definition: h264.h:50
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:87
enum AVFieldOrder field_order
Definition: avcodec.h:4382
int codec_ids[5]
Definition: avcodec.h:4426
#define FF_ARRAY_ELEMS(a)
int * skipped_bytes_pos
Definition: hevc.h:882
uint8_t dependent_slice_segment_flag
Definition: hevc.h:573
int frame_start_found
Definition: parser.h:34
Definition: h264.h:118
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevc.h:564
Definition: h264.h:119
enum AVPictureStructure picture_structure
Indicate whether a picture is coded as a frame, top field or bottom field.
Definition: avcodec.h:4392
if()
Definition: avfilter.c:975
uint8_t
const uint8_t * data
Definition: hevc.h:741
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
uint8_t * rbsp_buffer
Definition: hevc.h:737
ParseContext pc
Definition: hevc_parser.c:33
AVCodecContext * avctx
Definition: hevc.h:792
HEVCNAL * nals
Definition: hevc.h:891
uint8_t first_slice_in_pic_flag
Definition: hevc.h:572
int ff_hevc_decode_nal_sei(HEVCContext *s)
Definition: hevc_sei.c:200
uint8_t pic_output_flag
Definition: hevc.h:574
#define av_log(a,...)
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:832
uint8_t no_output_of_prior_pics_flag
Definition: hevc.h:586
static int hevc_init(AVCodecParserContext *s)
Definition: hevc_parser.c:308
const HEVCSPS * sps
Definition: hevc.h:816
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevc.h:575
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:412
AVBufferRef * vps_list[MAX_VPS_COUNT]
Definition: hevc.h:818
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:231
#define AVERROR(e)
Definition: error.h:43
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4281
int ff_hevc_compute_poc(HEVCContext *s, int poc_lsb)
Compute POC of the current frame and return it.
Definition: hevc_refs.c:518
int picture_struct
Definition: hevc.h:923
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:40
static char * split(char *message, char delim)
Definition: af_channelmap.c:82
int ff_hevc_decode_nal_vps(HEVCContext *s)
Definition: hevc_ps.c:399
const HEVCPPS * pps
Definition: hevc.h:817
#define IS_IDR(s)
Definition: hevc.h:85
int size
Definition: hevc.h:740
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: hevc.h:819
#define MAX_SPS_COUNT
Definition: h264.h:49
Definition: hevc.h:133
int ctb_width
Definition: hevc.h:469
uint8_t output_flag_present_flag
Definition: hevc.h:508
unsigned vps_id
Definition: hevc.h:398
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: hevc.h:820
unsigned int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevc.h:561
Definition: hevc.h:132
Definition: hevc.h:397
HEVCContext h
Definition: hevc_parser.c:32
Definition: hevc.h:370
Definition: hevc.h:486
uint8_t * buffer
Definition: parser.h:29
#define IS_IRAP(s)
Definition: hevc.h:88
Definition: hevc.h:736
static void hevc_close(AVCodecParserContext *s)
Definition: hevc_parser.c:319
int ff_hevc_decode_nal_pps(HEVCContext *s)
Definition: hevc_ps.c:1250
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:441
#define START_CODE
start_code_prefix_one_3bytes
Definition: hevc_parser.c:29
unsigned int sps_id
seq_parameter_set_id
Definition: hevc.h:487
main external API structure.
Definition: avcodec.h:1241
int num_extra_slice_header_bits
Definition: hevc.h:533
uint8_t * data
The data buffer.
Definition: buffer.h:89
Definition: hevc.h:110
const HEVCVPS * vps
Definition: hevc.h:815
void * buf
Definition: avisynth_c.h:553
AVCodecParser ff_hevc_parser
Definition: hevc_parser.c:344
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:297
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
Definition: h264.h:120
GetBitContext gb
Definition: hevc.h:754
int poc
Definition: hevc.h:835
#define END_NOT_FOUND
Definition: parser.h:40
enum NALUnitType nal_unit_type
Definition: hevc.h:831
int pocTid0
Definition: hevc.h:836
HEVCLocalContext * HEVClc
Definition: hevc.h:797
static uint32_t state
Definition: trasher.c:27
int output_picture_number
Picture number incremented in presentation or output order.
Definition: avcodec.h:4400
int ff_hevc_extract_rbsp(HEVCContext *s, const uint8_t *src, int length, HEVCNAL *nal)
Definition: hevc.c:2786
int skipped_bytes_pos_size
Definition: hevc.h:883
common internal and external API header
Definition: hevc.h:97
Bi-dir predicted.
Definition: avutil.h:269
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:206
Definition: hevc.h:131
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:260
#define av_freep(p)
enum SliceType slice_type
Definition: hevc.h:568
static av_always_inline av_const int av_ceil_log2_c(int x)
Compute ceil(log2(x)).
Definition: common.h:286
SliceHeader sh
Definition: hevc.h:828
exp golomb vlc stuff
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:4296
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
uint8_t separate_colour_plane_flag
output (i.e. cropped) values
Definition: hevc.h:400
Predicted.
Definition: avutil.h:268
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc.h:511