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 #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 };