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 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
33 #define ADVANCED_PARSER CONFIG_HEVC_DECODER
35 typedef struct HEVCParserContext {
49 static int hevc_parse_slice_header(AVCodecParserContext *s, HEVCNAL *nal,
50 AVCodecContext *avctx)
52 HEVCParserContext *ctx = s->priv_data;
53 GetBitContext *gb = &nal->gb;
59 get_bits1(gb); // first slice in pic
61 get_bits1(gb); // no output of prior pics
63 pps_id = get_ue_golomb_long(gb);
64 if (pps_id >= MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
65 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
66 return AVERROR_INVALIDDATA;
68 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
69 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
71 /* export the stream parameters */
72 s->coded_width = sps->width;
73 s->coded_height = sps->height;
74 s->width = sps->output_width;
75 s->height = sps->output_height;
76 s->format = sps->pix_fmt;
77 avctx->profile = sps->ptl.general_ptl.profile_idc;
78 avctx->level = sps->ptl.general_ptl.level_idc;
80 /* ignore the rest for now*/
85 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
86 int buf_size, AVCodecContext *avctx)
88 HEVCParserContext *ctx = s->priv_data;
91 ret = ff_hevc_split_packet(NULL, &ctx->pkt, buf, buf_size, avctx, 0, 0);
95 for (i = 0; i < ctx->pkt.nb_nals; i++) {
96 HEVCNAL *nal = &ctx->pkt.nals[i];
98 /* ignore everything except parameter sets and VCL NALUs */
100 case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
101 case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
102 case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
119 if (buf == avctx->extradata) {
120 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
121 return AVERROR_INVALIDDATA;
123 hevc_parse_slice_header(s, nal, avctx);
133 * Find the end of the current frame in the bitstream.
134 * @return the position of the first byte of the next frame, or END_NOT_FOUND
136 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
140 ParseContext *pc = s->priv_data;
142 for (i = 0; i < buf_size; i++) {
145 pc->state64 = (pc->state64 << 8) | buf[i];
147 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
150 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
151 // Beginning of access unit
152 if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
153 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
154 if (pc->frame_start_found) {
155 pc->frame_start_found = 0;
158 } else if (nut <= NAL_RASL_R ||
159 (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
160 int first_slice_segment_in_pic_flag = buf[i] >> 7;
161 if (first_slice_segment_in_pic_flag) {
162 if (!pc->frame_start_found) {
163 pc->frame_start_found = 1;
164 } else { // First slice of next frame found
165 pc->frame_start_found = 0;
172 return END_NOT_FOUND;
177 * Parse NAL units of found picture and decode some basic information.
179 * @param s parser context.
180 * @param avctx codec context.
181 * @param buf buffer with field/frame data.
182 * @param buf_size size of the buffer.
184 static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
185 int buf_size, AVCodecContext *avctx)
187 HEVCParserContext *ctx = s->priv_data;
188 HEVCContext *h = &ctx->h;
190 SliceHeader *sh = &h->sh;
191 HEVCParamSets *ps = &h->ps;
192 HEVCPacket *pkt = &ctx->pkt;
193 const uint8_t *buf_end = buf + buf_size;
196 int is_global = buf == avctx->extradata;
199 h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
201 return AVERROR(ENOMEM);
205 /* set some sane default values */
206 s->pict_type = AV_PICTURE_TYPE_I;
208 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
215 if (pkt->nals_allocated < 1) {
216 HEVCNAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
218 return AVERROR(ENOMEM);
220 memset(pkt->nals, 0, sizeof(*tmp));
221 pkt->nals_allocated = 1;
227 int src_length, consumed;
229 buf = avpriv_find_start_code(buf, buf_end, &state);
230 if (--buf + 2 >= buf_end)
232 src_length = buf_end - buf;
234 h->nal_unit_type = (*buf >> 1) & 0x3f;
235 h->temporal_id = (*(buf + 1) & 0x07) - 1;
236 if (h->nal_unit_type <= NAL_CRA_NUT) {
237 // Do not walk the whole buffer just to decode slice segment header
242 consumed = ff_hevc_extract_rbsp(NULL, buf, src_length, nal);
246 ret = init_get_bits8(gb, nal->data + 2, nal->size);
250 switch (h->nal_unit_type) {
252 ff_hevc_decode_nal_vps(gb, avctx, ps);
255 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
258 ff_hevc_decode_nal_pps(gb, avctx, ps);
262 ff_hevc_decode_nal_sei(h);
282 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
283 return AVERROR_INVALIDDATA;
286 sh->first_slice_in_pic_flag = get_bits1(gb);
287 s->picture_structure = h->picture_struct;
288 s->field_order = h->picture_struct;
292 sh->no_output_of_prior_pics_flag = get_bits1(gb);
295 sh->pps_id = get_ue_golomb(gb);
296 if (sh->pps_id >= MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
297 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
298 return AVERROR_INVALIDDATA;
300 ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
302 if (ps->pps->sps_id >= MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
303 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
304 return AVERROR_INVALIDDATA;
306 if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
307 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
308 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
311 if (!sh->first_slice_in_pic_flag) {
312 int slice_address_length;
314 if (ps->pps->dependent_slice_segments_enabled_flag)
315 sh->dependent_slice_segment_flag = get_bits1(gb);
317 sh->dependent_slice_segment_flag = 0;
319 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
320 ps->sps->ctb_height);
321 sh->slice_segment_addr = slice_address_length ? get_bits(gb, slice_address_length) : 0;
322 if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
323 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
324 sh->slice_segment_addr);
325 return AVERROR_INVALIDDATA;
328 sh->dependent_slice_segment_flag = 0;
330 if (sh->dependent_slice_segment_flag)
333 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
334 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
336 sh->slice_type = get_ue_golomb(gb);
337 if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
338 sh->slice_type == B_SLICE)) {
339 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
341 return AVERROR_INVALIDDATA;
343 s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
344 sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
347 if (ps->pps->output_flag_present_flag)
348 sh->pic_output_flag = get_bits1(gb);
350 if (ps->sps->separate_colour_plane_flag)
351 sh->colour_plane_id = get_bits(gb, 2);
354 sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
355 s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
357 s->output_picture_number = h->poc = 0;
359 if (h->temporal_id == 0 &&
360 h->nal_unit_type != NAL_TRAIL_N &&
361 h->nal_unit_type != NAL_TSA_N &&
362 h->nal_unit_type != NAL_STSA_N &&
363 h->nal_unit_type != NAL_RADL_N &&
364 h->nal_unit_type != NAL_RASL_N &&
365 h->nal_unit_type != NAL_RADL_R &&
366 h->nal_unit_type != NAL_RASL_R)
369 return 0; /* no need to evaluate the rest */
373 /* didn't find a picture! */
375 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
380 static int hevc_parse(AVCodecParserContext *s,
381 AVCodecContext *avctx,
382 const uint8_t **poutbuf, int *poutbuf_size,
383 const uint8_t *buf, int buf_size)
386 HEVCParserContext *ctx = s->priv_data;
387 ParseContext *pc = &ctx->pc;
389 if (avctx->extradata && !ctx->parsed_extradata) {
390 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
391 ctx->parsed_extradata = 1;
394 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
397 next = hevc_find_frame_end(s, buf, buf_size);
398 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
405 parse_nal_units(s, buf, buf_size, avctx);
408 *poutbuf_size = buf_size;
412 // Split after the parameter sets at the beginning of the stream if they exist.
413 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
415 const uint8_t *ptr = buf, *end = buf + buf_size;
423 ptr = avpriv_find_start_code(ptr, end, &state);
424 if ((state >> 8) != START_CODE)
426 nut = (state >> 1) & 0x3F;
429 else if (nut == NAL_SPS)
431 else if (nut == NAL_PPS)
433 else if ((nut != NAL_SEI_PREFIX || has_pps) &&
435 if (has_vps && has_sps) {
436 while (ptr - 4 > buf && ptr[-5] == 0)
438 return ptr - 4 - buf;
445 static void hevc_parser_close(AVCodecParserContext *s)
447 HEVCParserContext *ctx = s->priv_data;
451 HEVCContext *h = &ctx->h;
453 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
454 av_buffer_unref(&h->ps.vps_list[i]);
455 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
456 av_buffer_unref(&h->ps.sps_list[i]);
457 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
458 av_buffer_unref(&h->ps.pps_list[i]);
462 av_freep(&h->HEVClc);
465 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
466 av_buffer_unref(&ctx->ps.vps_list[i]);
467 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
468 av_buffer_unref(&ctx->ps.sps_list[i]);
469 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
470 av_buffer_unref(&ctx->ps.pps_list[i]);
474 for (i = 0; i < ctx->pkt.nals_allocated; i++) {
475 av_freep(&ctx->pkt.nals[i].rbsp_buffer);
476 av_freep(&ctx->pkt.nals[i].skipped_bytes_pos);
478 av_freep(&ctx->pkt.nals);
479 ctx->pkt.nals_allocated = 0;
481 av_freep(&ctx->pc.buffer);
484 AVCodecParser ff_hevc_parser = {
485 .codec_ids = { AV_CODEC_ID_HEVC },
486 .priv_data_size = sizeof(HEVCParserContext),
487 .parser_parse = hevc_parse,
488 .parser_close = hevc_parser_close,