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 "golomb.h"
26 #include "hevc.h"
27 #include "hevc_ps.h"
28 #include "hevc_sei.h"
29 #include "h2645_parse.h"
30 #include "internal.h"
31 #include "parser.h"
32 
33 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
34 
35 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
36 #define IS_IDR_NAL(nal) (nal->type == HEVC_NAL_IDR_W_RADL || nal->type == HEVC_NAL_IDR_N_LP)
37 
38 typedef struct HEVCParserContext {
40 
45 
47 
48  int poc;
49  int pocTid0;
51 
53  AVCodecContext *avctx)
54 {
56  HEVCParamSets *ps = &ctx->ps;
57  HEVCSEIContext *sei = &ctx->sei;
58  SliceHeader *sh = &ctx->sh;
59  GetBitContext *gb = &nal->gb;
60  const HEVCWindow *ow;
61  int i, num = 0, den = 0;
62 
66 
67  if (IS_IRAP_NAL(nal)) {
68  s->key_frame = 1;
70  }
71 
72  sh->pps_id = get_ue_golomb(gb);
73  if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
74  av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
75  return AVERROR_INVALIDDATA;
76  }
77  ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
78 
79  if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
80  av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
81  return AVERROR_INVALIDDATA;
82  }
83  if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
84  ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
85  ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
86  }
87  ow = &ps->sps->output_window;
88 
89  s->coded_width = ps->sps->width;
90  s->coded_height = ps->sps->height;
91  s->width = ps->sps->width - ow->left_offset - ow->right_offset;
92  s->height = ps->sps->height - ow->top_offset - ow->bottom_offset;
93  s->format = ps->sps->pix_fmt;
94  avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
95  avctx->level = ps->sps->ptl.general_ptl.level_idc;
96 
98  num = ps->vps->vps_num_units_in_tick;
99  den = ps->vps->vps_time_scale;
100  } else if (ps->sps->vui.vui_timing_info_present_flag) {
101  num = ps->sps->vui.vui_num_units_in_tick;
102  den = ps->sps->vui.vui_time_scale;
103  }
104 
105  if (num != 0 && den != 0)
106  av_reduce(&avctx->framerate.den, &avctx->framerate.num,
107  num, den, 1 << 30);
108 
109  if (!sh->first_slice_in_pic_flag) {
110  int slice_address_length;
111 
114  else
116 
117  slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
118  ps->sps->ctb_height);
119  sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
120  if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
121  av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
122  sh->slice_segment_addr);
123  return AVERROR_INVALIDDATA;
124  }
125  } else
127 
129  return 0; /* break; */
130 
131  for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
132  skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
133 
134  sh->slice_type = get_ue_golomb(gb);
135  if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
136  sh->slice_type == HEVC_SLICE_B)) {
137  av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
138  sh->slice_type);
139  return AVERROR_INVALIDDATA;
140  }
144 
145  if (ps->pps->output_flag_present_flag)
146  sh->pic_output_flag = get_bits1(gb);
147 
149  sh->colour_plane_id = get_bits(gb, 2);
150 
151  if (!IS_IDR_NAL(nal)) {
154  } else
155  s->output_picture_number = ctx->poc = 0;
156 
157  if (nal->temporal_id == 0 &&
158  nal->type != HEVC_NAL_TRAIL_N &&
159  nal->type != HEVC_NAL_TSA_N &&
160  nal->type != HEVC_NAL_STSA_N &&
161  nal->type != HEVC_NAL_RADL_N &&
162  nal->type != HEVC_NAL_RASL_N &&
163  nal->type != HEVC_NAL_RADL_R &&
164  nal->type != HEVC_NAL_RASL_R)
165  ctx->pocTid0 = ctx->poc;
166 
167  return 1; /* no need to evaluate the rest */
168 }
169 
170 /**
171  * Parse NAL units of found picture and decode some basic information.
172  *
173  * @param s parser context.
174  * @param avctx codec context.
175  * @param buf buffer with field/frame data.
176  * @param buf_size size of the buffer.
177  */
179  int buf_size, AVCodecContext *avctx)
180 {
182  HEVCParamSets *ps = &ctx->ps;
183  HEVCSEIContext *sei = &ctx->sei;
184  int is_global = buf == avctx->extradata;
185  int ret, i;
186 
187  /* set some sane default values */
189  s->key_frame = 0;
191 
192  ff_hevc_reset_sei(sei);
193 
194  ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
195  AV_CODEC_ID_HEVC, 1);
196  if (ret < 0)
197  return ret;
198 
199  for (i = 0; i < ctx->pkt.nb_nals; i++) {
200  H2645NAL *nal = &ctx->pkt.nals[i];
201  GetBitContext *gb = &nal->gb;
202 
203  switch (nal->type) {
204  case HEVC_NAL_VPS:
205  ff_hevc_decode_nal_vps(gb, avctx, ps);
206  break;
207  case HEVC_NAL_SPS:
208  ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
209  break;
210  case HEVC_NAL_PPS:
211  ff_hevc_decode_nal_pps(gb, avctx, ps);
212  break;
213  case HEVC_NAL_SEI_PREFIX:
214  case HEVC_NAL_SEI_SUFFIX:
215  ff_hevc_decode_nal_sei(gb, avctx, sei, ps, nal->type);
216  break;
217  case HEVC_NAL_TRAIL_N:
218  case HEVC_NAL_TRAIL_R:
219  case HEVC_NAL_TSA_N:
220  case HEVC_NAL_TSA_R:
221  case HEVC_NAL_STSA_N:
222  case HEVC_NAL_STSA_R:
223  case HEVC_NAL_BLA_W_LP:
224  case HEVC_NAL_BLA_W_RADL:
225  case HEVC_NAL_BLA_N_LP:
226  case HEVC_NAL_IDR_W_RADL:
227  case HEVC_NAL_IDR_N_LP:
228  case HEVC_NAL_CRA_NUT:
229  case HEVC_NAL_RADL_N:
230  case HEVC_NAL_RADL_R:
231  case HEVC_NAL_RASL_N:
232  case HEVC_NAL_RASL_R:
233 
234  if (is_global) {
235  av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
236  return AVERROR_INVALIDDATA;
237  }
238 
239  ret = hevc_parse_slice_header(s, nal, avctx);
240  if (ret)
241  return ret;
242  break;
243  }
244  }
245  /* didn't find a picture! */
246  if (!is_global)
247  av_log(avctx, AV_LOG_ERROR, "missing picture in access unit\n");
248  return -1;
249 }
250 
251 /**
252  * Find the end of the current frame in the bitstream.
253  * @return the position of the first byte of the next frame, or END_NOT_FOUND
254  */
256  int buf_size)
257 {
259  ParseContext *pc = &ctx->pc;
260  int i;
261 
262  for (i = 0; i < buf_size; i++) {
263  int nut;
264 
265  pc->state64 = (pc->state64 << 8) | buf[i];
266 
267  if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
268  continue;
269 
270  nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
271  // Beginning of access unit
272  if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_EOB_NUT) || nut == HEVC_NAL_SEI_PREFIX ||
273  (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
274  if (pc->frame_start_found) {
275  pc->frame_start_found = 0;
276  return i - 5;
277  }
278  } else if (nut <= HEVC_NAL_RASL_R ||
279  (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
280  int first_slice_segment_in_pic_flag = buf[i] >> 7;
281  if (first_slice_segment_in_pic_flag) {
282  if (!pc->frame_start_found) {
283  pc->frame_start_found = 1;
284  } else { // First slice of next frame found
285  pc->frame_start_found = 0;
286  return i - 5;
287  }
288  }
289  }
290  }
291 
292  return END_NOT_FOUND;
293 }
294 
296  const uint8_t **poutbuf, int *poutbuf_size,
297  const uint8_t *buf, int buf_size)
298 {
299  int next;
301  ParseContext *pc = &ctx->pc;
302 
303  if (avctx->extradata && !ctx->parsed_extradata) {
304  parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
305  ctx->parsed_extradata = 1;
306  }
307 
309  next = buf_size;
310  } else {
311  next = hevc_find_frame_end(s, buf, buf_size);
312  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
313  *poutbuf = NULL;
314  *poutbuf_size = 0;
315  return buf_size;
316  }
317  }
318 
319  parse_nal_units(s, buf, buf_size, avctx);
320 
321  *poutbuf = buf;
322  *poutbuf_size = buf_size;
323  return next;
324 }
325 
326 // Split after the parameter sets at the beginning of the stream if they exist.
327 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
328 {
329  const uint8_t *ptr = buf, *end = buf + buf_size;
330  uint32_t state = -1;
331  int has_vps = 0;
332  int has_sps = 0;
333  int has_pps = 0;
334  int nut;
335 
336  while (ptr < end) {
337  ptr = avpriv_find_start_code(ptr, end, &state);
338  if ((state >> 8) != START_CODE)
339  break;
340  nut = (state >> 1) & 0x3F;
341  if (nut == HEVC_NAL_VPS)
342  has_vps = 1;
343  else if (nut == HEVC_NAL_SPS)
344  has_sps = 1;
345  else if (nut == HEVC_NAL_PPS)
346  has_pps = 1;
347  else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
348  nut != HEVC_NAL_AUD) {
349  if (has_vps && has_sps) {
350  while (ptr - 4 > buf && ptr[-5] == 0)
351  ptr--;
352  return ptr - 4 - buf;
353  }
354  }
355  }
356  return 0;
357 }
358 
360 {
362  int i;
363 
364  for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
365  av_buffer_unref(&ctx->ps.vps_list[i]);
366  for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
367  av_buffer_unref(&ctx->ps.sps_list[i]);
368  for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
369  av_buffer_unref(&ctx->ps.pps_list[i]);
370 
371  ctx->ps.sps = NULL;
372 
374  ff_hevc_reset_sei(&ctx->sei);
375 
376  av_freep(&ctx->pc.buffer);
377 }
378 
381  .priv_data_size = sizeof(HEVCParserContext),
382  .parser_parse = hevc_parse,
383  .parser_close = hevc_parser_close,
384  .split = hevc_split,
385 };
int ff_h2645_packet_split(H2645Packet *pkt, const uint8_t *buf, int length, void *logctx, int is_nalff, int nal_length_size, enum AVCodecID codec_id, int small_padding)
Split an input packet into NAL units.
Definition: h2645_parse.c:250
const HEVCPPS * pps
Definition: hevc_ps.h:402
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:3460
const char * s
Definition: avisynth_c.h:768
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int pic_order_cnt_lsb
Definition: hevc_ps.h:58
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:125
static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal, AVCodecContext *avctx)
Definition: hevc_parser.c:52
HEVCSEIPictureTiming picture_timing
Definition: hevc_sei.h:113
int ctb_height
Definition: hevc_ps.h:298
AVBufferRef * vps_list[HEVC_MAX_VPS_COUNT]
Definition: hevc_ps.h:395
static struct @260 state
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:261
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:327
int width
Dimensions of the decoded video intended for presentation.
Definition: avcodec.h:5314
VUI vui
Definition: hevc_ps.h:250
enum AVFieldOrder field_order
Definition: avcodec.h:5291
int num
Numerator.
Definition: rational.h:59
uint32_t vui_time_scale
Definition: hevc_ps.h:159
int codec_ids[5]
Definition: avcodec.h:5335
int coded_width
Dimensions of the coded video.
Definition: avcodec.h:5320
unsigned int left_offset
Definition: hevc_ps.h:126
H2645Packet pkt
Definition: hevc_parser.c:41
uint8_t dependent_slice_segment_flag
Definition: hevc_ps.h:61
int profile
profile
Definition: avcodec.h:3266
int frame_start_found
Definition: parser.h:34
int width
Definition: hevc_ps.h:295
HEVCWindow output_window
Definition: hevc_ps.h:230
unsigned int slice_segment_addr
address (in raster order) of the first block in the current slice
Definition: hevc_ps.h:52
enum HEVCSliceType slice_type
Definition: hevc_ps.h:56
AVBufferRef * sps_list[HEVC_MAX_SPS_COUNT]
Definition: hevc_ps.h:396
enum AVPictureStructure picture_structure
Indicate whether a picture is coded as a frame, top field or bottom field.
Definition: avcodec.h:5301
uint8_t
uint8_t vps_timing_info_present_flag
Definition: hevc_ps.h:207
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
const HEVCVPS * vps
Definition: hevc_ps.h:400
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1876
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:371
uint8_t first_slice_in_pic_flag
Definition: hevc_ps.h:60
AVBufferRef * pps_list[HEVC_MAX_PPS_COUNT]
Definition: hevc_ps.h:397
uint8_t pic_output_flag
Definition: hevc_ps.h:62
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
#define av_log(a,...)
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
uint8_t no_output_of_prior_pics_flag
Definition: hevc_ps.h:75
HEVCParamSets ps
Definition: hevc_parser.c:42
uint8_t colour_plane_id
RPS coded in the slice header itself is stored here.
Definition: hevc_ps.h:63
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static int get_ue_golomb(GetBitContext *gb)
Read an unsigned Exp-Golomb code in the range 0 to 8190.
Definition: golomb.h:53
unsigned int log2_max_poc_lsb
Definition: hevc_ps.h:239
int ff_hevc_compute_poc(const HEVCSPS *sps, int pocTid0, int poc_lsb, int nal_unit_type)
Compute POC of the current frame and return it.
Definition: hevc_ps.c:1707
HEVCSEIContext sei
Definition: hevc_parser.c:43
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:416
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:251
int vui_timing_info_present_flag
Definition: hevc_ps.h:157
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:255
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, int buf_size, AVCodecContext *avctx)
Parse NAL units of found picture and decode some basic information.
Definition: hevc_parser.c:178
uint32_t vps_num_units_in_tick
Definition: hevc_ps.h:208
const HEVCSPS * sps
Definition: hevc_ps.h:401
uint8_t profile_idc
Definition: hevc_ps.h:178
unsigned int top_offset
Definition: hevc_ps.h:128
AVFormatContext * ctx
Definition: movenc.c:48
int level
level
Definition: avcodec.h:3364
#define IS_IDR_NAL(nal)
Definition: hevc_parser.c:36
int ctb_width
Definition: hevc_ps.h:297
int height
Definition: hevc_ps.h:296
uint8_t output_flag_present_flag
Definition: hevc_ps.h:339
PTLCommon general_ptl
Definition: hevc_ps.h:188
int type
NAL unit type.
Definition: h2645_parse.h:52
#define IS_IRAP_NAL(nal)
Definition: hevc_parser.c:35
unsigned vps_id
Definition: hevc_ps.h:226
#define FF_ARRAY_ELEMS(a)
ParseContext pc
Definition: hevc_parser.c:39
unsigned int pps_id
address (in raster order) of the first block in the current slice segment
Definition: hevc_ps.h:49
uint32_t vps_time_scale
Definition: hevc_ps.h:209
enum AVPixelFormat pix_fmt
Definition: hevc_ps.h:237
uint8_t * buffer
Definition: parser.h:29
PTL ptl
Definition: hevc_ps.h:251
#define START_CODE
start_code_prefix_one_3bytes
Definition: hevc_parser.c:33
unsigned int sps_id
seq_parameter_set_id
Definition: hevc_ps.h:318
main external API structure.
Definition: avcodec.h:1761
int num_extra_slice_header_bits
Definition: hevc_ps.h:364
uint8_t * data
The data buffer.
Definition: buffer.h:89
void * buf
Definition: avisynth_c.h:690
AVCodecParser ff_hevc_parser
Definition: hevc_parser.c:379
uint32_t vui_num_units_in_tick
Definition: hevc_ps.h:158
int extradata_size
Definition: avcodec.h:1877
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
int ff_hevc_decode_nal_sei(GetBitContext *gb, void *logctx, HEVCSEIContext *s, const HEVCParamSets *ps, int type)
Definition: hevc_sei.c:347
SliceHeader sh
Definition: hevc_parser.c:44
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:306
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
#define HEVC_MAX_SPS_COUNT
Definition: hevc.h:82
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1462
#define END_NOT_FOUND
Definition: parser.h:40
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: hevc_ps.c:1218
void ff_hevc_reset_sei(HEVCSEIContext *s)
Reset SEI values that are stored on the Context.
Definition: hevc_sei.c:360
int output_picture_number
Picture number incremented in presentation or output order.
Definition: avcodec.h:5309
uint8_t level_idc
Definition: hevc_ps.h:180
common internal api header.
common internal and external API header
if(ret< 0)
Definition: vf_mcdeint.c:279
Bi-dir predicted.
Definition: avutil.h:276
#define HEVC_MAX_PPS_COUNT
Definition: hevc.h:83
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:5201
int den
Denominator.
Definition: rational.h:60
GetBitContext gb
Definition: h2645_parse.h:47
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
Definition: avcodec.h:5331
H2645NAL * nals
Definition: h2645_parse.h:70
int temporal_id
HEVC only, nuh_temporal_id_plus_1 - 1.
Definition: h2645_parse.h:57
unsigned int right_offset
Definition: hevc_ps.h:127
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:295
#define av_freep(p)
static void hevc_parser_close(AVCodecParserContext *s)
Definition: hevc_parser.c:359
static av_always_inline av_const int av_ceil_log2_c(int x)
Compute ceil(log2(x)).
Definition: common.h:308
exp golomb vlc stuff
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:5216
uint8_t separate_colour_plane_flag
Definition: hevc_ps.h:228
static av_always_inline int get_bitsz(GetBitContext *s, int n)
Read 0-25 bits.
Definition: get_bits.h:276
Predicted.
Definition: avutil.h:275
unsigned int bottom_offset
Definition: hevc_ps.h:129
uint8_t dependent_slice_segments_enabled_flag
Definition: hevc_ps.h:342