Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "h2645_parse.h"
28 #include "parser.h"
29 
30 #define START_CODE 0x000001
31 
32 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
33 
34 typedef struct HEVCParserContext {
36 
39 
42 
44  AVCodecContext *avctx)
45 {
47  GetBitContext *gb = &nal->gb;
48 
49  HEVCPPS *pps;
50  HEVCSPS *sps;
51  unsigned int pps_id;
52 
53  get_bits1(gb); // first slice in pic
54  if (IS_IRAP_NAL(nal))
55  get_bits1(gb); // no output of prior pics
56 
57  pps_id = get_ue_golomb_long(gb);
58  if (pps_id >= MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
59  av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
60  return AVERROR_INVALIDDATA;
61  }
62  pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
63  sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
64 
65  /* export the stream parameters */
66  s->coded_width = sps->width;
67  s->coded_height = sps->height;
68  s->width = sps->output_width;
69  s->height = sps->output_height;
70  s->format = sps->pix_fmt;
71  avctx->profile = sps->ptl.general_ptl.profile_idc;
72  avctx->level = sps->ptl.general_ptl.level_idc;
73 
74  /* ignore the rest for now*/
75 
76  return 0;
77 }
78 
79 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
80  int buf_size, AVCodecContext *avctx)
81 {
83  int ret, i;
84 
85  ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
87  if (ret < 0)
88  return ret;
89 
90  for (i = 0; i < ctx->pkt.nb_nals; i++) {
91  H2645NAL *nal = &ctx->pkt.nals[i];
92 
93  /* ignore everything except parameter sets and VCL NALUs */
94  switch (nal->type) {
95  case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
96  case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
97  case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
98  case NAL_TRAIL_R:
99  case NAL_TRAIL_N:
100  case NAL_TSA_N:
101  case NAL_TSA_R:
102  case NAL_STSA_N:
103  case NAL_STSA_R:
104  case NAL_BLA_W_LP:
105  case NAL_BLA_W_RADL:
106  case NAL_BLA_N_LP:
107  case NAL_IDR_W_RADL:
108  case NAL_IDR_N_LP:
109  case NAL_CRA_NUT:
110  case NAL_RADL_N:
111  case NAL_RADL_R:
112  case NAL_RASL_N:
113  case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
114  }
115  }
116 
117  return 0;
118 }
119 
125  int buf_size)
126 {
128  ParseContext *pc = &ctx->pc;
129  int i;
130 
131  for (i = 0; i < buf_size; i++) {
132  int nut;
133 
134  pc->state64 = (pc->state64 << 8) | buf[i];
135 
136  if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
137  continue;
138 
139  nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
140  // Beginning of access unit
141  if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
142  (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
143  if (pc->frame_start_found) {
144  pc->frame_start_found = 0;
145  return i - 5;
146  }
147  } else if (nut <= NAL_RASL_R ||
148  (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
149  int first_slice_segment_in_pic_flag = buf[i] >> 7;
150  if (first_slice_segment_in_pic_flag) {
151  if (!pc->frame_start_found) {
152  pc->frame_start_found = 1;
153  s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
154  } else { // First slice of next frame found
155  pc->frame_start_found = 0;
156  return i - 5;
157  }
158  }
159  }
160  }
161 
162  return END_NOT_FOUND;
163 }
164 
166  const uint8_t **poutbuf, int *poutbuf_size,
167  const uint8_t *buf, int buf_size)
168 {
169  int next;
170 
172  ParseContext *pc = &ctx->pc;
173 
174  if (avctx->extradata && !ctx->parsed_extradata) {
175  parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
176  ctx->parsed_extradata = 1;
177  }
178 
180  next = buf_size;
181  } else {
182  next = hevc_find_frame_end(s, buf, buf_size);
183  if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
184  *poutbuf = NULL;
185  *poutbuf_size = 0;
186  return buf_size;
187  }
188  }
189 
190  parse_nal_units(s, buf, buf_size, avctx);
191 
192  *poutbuf = buf;
193  *poutbuf_size = buf_size;
194  return next;
195 }
196 
197 // Split after the parameter sets at the beginning of the stream if they exist.
198 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
199 {
200  int i;
201  uint32_t state = -1;
202  int has_ps = 0;
203 
204  for (i = 0; i < buf_size; i++) {
205  state = (state << 8) | buf[i];
206  if (((state >> 8) & 0xFFFFFF) == START_CODE) {
207  int nut = (state >> 1) & 0x3F;
208  if (nut >= NAL_VPS && nut <= NAL_PPS)
209  has_ps = 1;
210  else if (has_ps)
211  return i - 3;
212  else // no parameter set at the beginning of the stream
213  return 0;
214  }
215  }
216  return 0;
217 }
218 
220 {
222  int i;
223 
224  for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
225  av_buffer_unref(&ctx->ps.vps_list[i]);
226  for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
227  av_buffer_unref(&ctx->ps.sps_list[i]);
228  for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
229  av_buffer_unref(&ctx->ps.pps_list[i]);
230 
232 
233  av_freep(&ctx->pc.buffer);
234 }
235 
238  .priv_data_size = sizeof(HEVCParserContext),
239  .parser_parse = hevc_parse,
240  .parser_close = hevc_parser_close,
241  .split = hevc_split,
242 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
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:106
static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal, AVCodecContext *avctx)
Definition: hevc_parser.c:43
Definition: hevc.h:97
static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: hevc_parser.c:198
int width
Dimensions of the decoded video intended for presentation.
Definition: avcodec.h:4508
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
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)
Split an input packet into NAL units.
Definition: h2645_parse.c:214
int codec_ids[5]
Definition: avcodec.h:4529
int coded_width
Dimensions of the coded video.
Definition: avcodec.h:4514
H2645Packet pkt
Definition: hevc_parser.c:37
#define FF_ARRAY_ELEMS(a)
int ff_hevc_decode_nal_sps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps, int apply_defdispwin)
Definition: hevc_ps.c:984
int profile
profile
Definition: avcodec.h:2880
AVBufferRef * vps_list[MAX_VPS_COUNT]
Definition: hevc.h:531
int frame_start_found
Definition: parser.h:34
int width
Definition: hevc.h:448
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
int output_width
Definition: hevc.h:391
uint8_t
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
void ff_h2645_packet_uninit(H2645Packet *pkt)
Free all the allocated memory in the packet.
Definition: h2645_parse.c:323
#define MAX_PPS_COUNT
Definition: h264_ps.h:38
Definition: hevc.h:113
HEVCParamSets ps
Definition: hevc_parser.c:38
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int ff_hevc_decode_nal_pps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:1175
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:227
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:124
static char * split(char *message, char delim)
Definition: af_channelmap.c:85
static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf, int buf_size, AVCodecContext *avctx)
Definition: hevc_parser.c:79
uint8_t profile_idc
Definition: hevc.h:341
int output_height
Definition: hevc.h:391
AVBufferRef * pps_list[MAX_PPS_COUNT]
Definition: hevc.h:533
AVFormatContext * ctx
Definition: movenc.c:48
int level
level
Definition: avcodec.h:2970
int height
Definition: hevc.h:449
PTLCommon general_ptl
Definition: hevc.h:351
int type
NAL unit type.
Definition: h2645_parse.h:50
#define IS_IRAP_NAL(nal)
Definition: hevc_parser.c:32
Definition: hevc.h:112
ParseContext pc
Definition: hevc_parser.c:35
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:81
Definition: hevc.h:385
enum AVPixelFormat pix_fmt
Definition: hevc.h:398
NULL
Definition: eval.c:55
Definition: hevc.h:466
uint8_t * buffer
Definition: parser.h:29
PTL ptl
Definition: hevc.h:411
#define START_CODE
start_code_prefix_one_3bytes
Definition: hevc_parser.c:30
unsigned int sps_id
seq_parameter_set_id
Definition: hevc.h:467
main external API structure.
Definition: avcodec.h:1409
uint8_t * data
The data buffer.
Definition: buffer.h:89
Definition: hevc.h:111
AVCodecParser ff_hevc_parser
Definition: hevc_parser.c:236
int extradata_size
Definition: avcodec.h:1524
Definition: hevc.h:114
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
#define END_NOT_FOUND
Definition: parser.h:40
int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx, HEVCParamSets *ps)
Definition: hevc_ps.c:358
uint8_t level_idc
Definition: hevc.h:343
static struct @174 state
common internal and external API header
Definition: hevc.h:98
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4396
GetBitContext gb
Definition: h2645_parse.h:45
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
Definition: avcodec.h:4525
H2645NAL * nals
Definition: h2645_parse.h:65
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:165
static void hevc_parser_close(AVCodecParserContext *s)
Definition: hevc_parser.c:219
exp golomb vlc stuff
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:4410
AVBufferRef * sps_list[MAX_SPS_COUNT]
Definition: hevc.h:532