2 * HEVC Annex B format parser
4 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * This file is part of FFmpeg.
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.
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.
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
23 #include "libavutil/common.h"
29 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
31 typedef struct HEVCParseContext {
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
40 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
44 ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
46 for (i = 0; i < buf_size; i++) {
49 pc->state64 = (pc->state64 << 8) | buf[i];
51 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
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;
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;
80 * Parse NAL units of found picture and decode some basic information.
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.
87 static inline int parse_nal_units(AVCodecParserContext *s, AVCodecContext *avctx,
88 const uint8_t *buf, int buf_size)
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;
97 /* set some sane default values */
98 s->pict_type = AV_PICTURE_TYPE_I;
100 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
107 if (h->nals_allocated < 1) {
108 HEVCNAL *tmp = av_realloc_array(h->nals, 1, sizeof(*tmp));
110 return AVERROR(ENOMEM);
112 memset(h->nals, 0, sizeof(*tmp));
113 h->nals_allocated = 1;
119 int src_length, consumed;
120 buf = avpriv_find_start_code(buf, buf_end, &state);
121 if (--buf + 2 >= buf_end)
123 src_length = buf_end - buf;
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
133 consumed = ff_hevc_extract_rbsp(h, buf, src_length, nal);
137 init_get_bits8(gb, nal->data + 2, nal->size);
138 switch (h->nal_unit_type) {
140 ff_hevc_decode_nal_vps(h);
143 ff_hevc_decode_nal_sps(h);
146 ff_hevc_decode_nal_pps(h);
150 ff_hevc_decode_nal_sei(h);
168 sh->first_slice_in_pic_flag = get_bits1(gb);
169 s->picture_structure = h->picture_struct;
170 s->field_order = h->picture_struct;
172 if (h->nal_unit_type >= 16 && h->nal_unit_type <= 23) {
174 sh->no_output_of_prior_pics_flag = get_bits1(gb);
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;
182 h->pps = (HEVCPPS*)h->pps_list[sh->pps_id]->data;
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;
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;
193 if (!sh->first_slice_in_pic_flag) {
194 int slice_address_length;
196 if (h->pps->dependent_slice_segments_enabled_flag)
197 sh->dependent_slice_segment_flag = get_bits1(gb);
199 sh->dependent_slice_segment_flag = 0;
201 slice_address_length = av_ceil_log2_c(h->sps->ctb_width *
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;
210 sh->dependent_slice_segment_flag = 0;
212 if (sh->dependent_slice_segment_flag)
215 for (i = 0; i < h->pps->num_extra_slice_header_bits; i++)
216 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
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",
223 return AVERROR_INVALIDDATA;
225 s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
226 sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
229 if (h->pps->output_flag_present_flag)
230 sh->pic_output_flag = get_bits1(gb);
232 if (h->sps->separate_colour_plane_flag)
233 sh->colour_plane_id = get_bits(gb, 2);
236 sh->pic_order_cnt_lsb = get_bits(gb, h->sps->log2_max_poc_lsb);
237 s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
239 s->output_picture_number = h->poc = 0;
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 &&
248 h->nal_unit_type != NAL_RASL_R)
251 return 0; /* no need to evaluate the rest */
255 /* didn't find a picture! */
256 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
260 static int hevc_parse(AVCodecParserContext *s,
261 AVCodecContext *avctx,
262 const uint8_t **poutbuf, int *poutbuf_size,
263 const uint8_t *buf, int buf_size)
266 ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
268 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
271 next = hevc_find_frame_end(s, buf, buf_size);
272 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
279 parse_nal_units(s, avctx, buf, buf_size);
282 *poutbuf_size = buf_size;
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)
293 for (i = 0; i < buf_size; i++) {
294 state = (state << 8) | buf[i];
295 if (((state >> 8) & 0xFFFFFF) == START_CODE) {
296 int nut = (state >> 1) & 0x3F;
297 if (nut >= NAL_VPS && nut <= NAL_PPS)
301 else // no parameter set at the beginning of the stream
308 static int hevc_init(AVCodecParserContext *s)
310 HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
311 h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
312 h->skipped_bytes_pos_size = INT_MAX;
317 static void hevc_close(AVCodecParserContext *s)
320 HEVCContext *h = &((HEVCParseContext *)s->priv_data)->h;
321 ParseContext *pc = &((HEVCParseContext *)s->priv_data)->pc;
323 av_freep(&h->skipped_bytes_pos);
324 av_freep(&h->HEVClc);
325 av_freep(&pc->buffer);
327 for (i = 0; i < FF_ARRAY_ELEMS(h->vps_list); i++)
328 av_buffer_unref(&h->vps_list[i]);
329 for (i = 0; i < FF_ARRAY_ELEMS(h->sps_list); i++)
330 av_buffer_unref(&h->sps_list[i]);
331 for (i = 0; i < FF_ARRAY_ELEMS(h->pps_list); i++)
332 av_buffer_unref(&h->pps_list[i]);
334 for (i = 0; i < h->nals_allocated; i++)
335 av_freep(&h->nals[i].rbsp_buffer);
337 h->nals_allocated = 0;
340 AVCodecParser ff_hevc_parser = {
341 .codec_ids = { AV_CODEC_ID_HEVC },
342 .priv_data_size = sizeof(HEVCParseContext),
343 .parser_init = hevc_init,
344 .parser_parse = hevc_parse,
345 .parser_close = hevc_close,