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"
27 #include "h2645_parse.h"
30 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
32 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
34 #define ADVANCED_PARSER CONFIG_HEVC_DECODER
36 typedef struct HEVCParserContext {
50 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
51 AVCodecContext *avctx)
53 HEVCParserContext *ctx = s->priv_data;
54 GetBitContext *gb = &nal->gb;
60 get_bits1(gb); // first slice in pic
62 get_bits1(gb); // no output of prior pics
64 pps_id = get_ue_golomb_long(gb);
65 if (pps_id >= MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
66 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
67 return AVERROR_INVALIDDATA;
69 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
70 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
72 /* export the stream parameters */
73 s->coded_width = sps->width;
74 s->coded_height = sps->height;
75 s->width = sps->output_width;
76 s->height = sps->output_height;
77 s->format = sps->pix_fmt;
78 avctx->profile = sps->ptl.general_ptl.profile_idc;
79 avctx->level = sps->ptl.general_ptl.level_idc;
81 /* ignore the rest for now*/
86 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
87 int buf_size, AVCodecContext *avctx)
89 HEVCParserContext *ctx = s->priv_data;
92 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
97 for (i = 0; i < ctx->pkt.nb_nals; i++) {
98 H2645NAL *nal = &ctx->pkt.nals[i];
100 /* ignore everything except parameter sets and VCL NALUs */
102 case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
103 case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
104 case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
121 if (buf == avctx->extradata) {
122 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
123 return AVERROR_INVALIDDATA;
125 hevc_parse_slice_header(s, nal, avctx);
135 * Find the end of the current frame in the bitstream.
136 * @return the position of the first byte of the next frame, or END_NOT_FOUND
138 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
142 ParseContext *pc = s->priv_data;
144 for (i = 0; i < buf_size; i++) {
147 pc->state64 = (pc->state64 << 8) | buf[i];
149 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
152 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
153 // Beginning of access unit
154 if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
155 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
156 if (pc->frame_start_found) {
157 pc->frame_start_found = 0;
160 } else if (nut <= NAL_RASL_R ||
161 (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
162 int first_slice_segment_in_pic_flag = buf[i] >> 7;
163 if (first_slice_segment_in_pic_flag) {
164 if (!pc->frame_start_found) {
165 pc->frame_start_found = 1;
166 } else { // First slice of next frame found
167 pc->frame_start_found = 0;
174 return END_NOT_FOUND;
179 * Parse NAL units of found picture and decode some basic information.
181 * @param s parser context.
182 * @param avctx codec context.
183 * @param buf buffer with field/frame data.
184 * @param buf_size size of the buffer.
186 static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
187 int buf_size, AVCodecContext *avctx)
189 HEVCParserContext *ctx = s->priv_data;
190 HEVCContext *h = &ctx->h;
192 SliceHeader *sh = &h->sh;
193 HEVCParamSets *ps = &h->ps;
194 H2645Packet *pkt = &ctx->pkt;
195 const uint8_t *buf_end = buf + buf_size;
198 int is_global = buf == avctx->extradata;
201 h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
203 return AVERROR(ENOMEM);
207 /* set some sane default values */
208 s->pict_type = AV_PICTURE_TYPE_I;
210 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
214 ff_hevc_reset_sei(h);
219 if (pkt->nals_allocated < 1) {
220 H2645NAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
222 return AVERROR(ENOMEM);
224 memset(pkt->nals, 0, sizeof(*tmp));
225 pkt->nals_allocated = 1;
231 int src_length, consumed;
233 buf = avpriv_find_start_code(buf, buf_end, &state);
234 if (--buf + 2 >= buf_end)
236 src_length = buf_end - buf;
238 h->nal_unit_type = (*buf >> 1) & 0x3f;
239 h->temporal_id = (*(buf + 1) & 0x07) - 1;
240 if (h->nal_unit_type <= NAL_CRA_NUT) {
241 // Do not walk the whole buffer just to decode slice segment header
246 consumed = ff_h2645_extract_rbsp(buf, src_length, nal, 1);
250 ret = init_get_bits8(gb, nal->data + 2, nal->size);
254 switch (h->nal_unit_type) {
256 ff_hevc_decode_nal_vps(gb, avctx, ps);
259 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
262 ff_hevc_decode_nal_pps(gb, avctx, ps);
266 ff_hevc_decode_nal_sei(h);
286 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
287 return AVERROR_INVALIDDATA;
290 sh->first_slice_in_pic_flag = get_bits1(gb);
291 s->picture_structure = h->picture_struct;
292 s->field_order = h->picture_struct;
296 sh->no_output_of_prior_pics_flag = get_bits1(gb);
299 sh->pps_id = get_ue_golomb(gb);
300 if (sh->pps_id >= MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
301 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
302 return AVERROR_INVALIDDATA;
304 ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
306 if (ps->pps->sps_id >= MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
307 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
308 return AVERROR_INVALIDDATA;
310 if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
311 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
312 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
315 s->coded_width = ps->sps->width;
316 s->coded_height = ps->sps->height;
317 s->width = ps->sps->output_width;
318 s->height = ps->sps->output_height;
319 s->format = ps->sps->pix_fmt;
320 avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
321 avctx->level = ps->sps->ptl.general_ptl.level_idc;
323 if (!sh->first_slice_in_pic_flag) {
324 int slice_address_length;
326 if (ps->pps->dependent_slice_segments_enabled_flag)
327 sh->dependent_slice_segment_flag = get_bits1(gb);
329 sh->dependent_slice_segment_flag = 0;
331 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
332 ps->sps->ctb_height);
333 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
334 if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
335 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
336 sh->slice_segment_addr);
337 return AVERROR_INVALIDDATA;
340 sh->dependent_slice_segment_flag = 0;
342 if (sh->dependent_slice_segment_flag)
345 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
346 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
348 sh->slice_type = get_ue_golomb(gb);
349 if (!(sh->slice_type == I_SLICE || sh->slice_type == P_SLICE ||
350 sh->slice_type == B_SLICE)) {
351 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
353 return AVERROR_INVALIDDATA;
355 s->pict_type = sh->slice_type == B_SLICE ? AV_PICTURE_TYPE_B :
356 sh->slice_type == P_SLICE ? AV_PICTURE_TYPE_P :
359 if (ps->pps->output_flag_present_flag)
360 sh->pic_output_flag = get_bits1(gb);
362 if (ps->sps->separate_colour_plane_flag)
363 sh->colour_plane_id = get_bits(gb, 2);
366 sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
367 s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
369 s->output_picture_number = h->poc = 0;
371 if (h->temporal_id == 0 &&
372 h->nal_unit_type != NAL_TRAIL_N &&
373 h->nal_unit_type != NAL_TSA_N &&
374 h->nal_unit_type != NAL_STSA_N &&
375 h->nal_unit_type != NAL_RADL_N &&
376 h->nal_unit_type != NAL_RASL_N &&
377 h->nal_unit_type != NAL_RADL_R &&
378 h->nal_unit_type != NAL_RASL_R)
381 return 0; /* no need to evaluate the rest */
385 /* didn't find a picture! */
387 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
392 static int hevc_parse(AVCodecParserContext *s,
393 AVCodecContext *avctx,
394 const uint8_t **poutbuf, int *poutbuf_size,
395 const uint8_t *buf, int buf_size)
398 HEVCParserContext *ctx = s->priv_data;
399 ParseContext *pc = &ctx->pc;
401 if (avctx->extradata && !ctx->parsed_extradata) {
402 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
403 ctx->parsed_extradata = 1;
406 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
409 next = hevc_find_frame_end(s, buf, buf_size);
410 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
417 parse_nal_units(s, buf, buf_size, avctx);
420 *poutbuf_size = buf_size;
424 // Split after the parameter sets at the beginning of the stream if they exist.
425 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
427 const uint8_t *ptr = buf, *end = buf + buf_size;
435 ptr = avpriv_find_start_code(ptr, end, &state);
436 if ((state >> 8) != START_CODE)
438 nut = (state >> 1) & 0x3F;
441 else if (nut == NAL_SPS)
443 else if (nut == NAL_PPS)
445 else if ((nut != NAL_SEI_PREFIX || has_pps) &&
447 if (has_vps && has_sps) {
448 while (ptr - 4 > buf && ptr[-5] == 0)
450 return ptr - 4 - buf;
457 static void hevc_parser_close(AVCodecParserContext *s)
459 HEVCParserContext *ctx = s->priv_data;
463 HEVCContext *h = &ctx->h;
465 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
466 av_buffer_unref(&h->ps.vps_list[i]);
467 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
468 av_buffer_unref(&h->ps.sps_list[i]);
469 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
470 av_buffer_unref(&h->ps.pps_list[i]);
474 av_freep(&h->HEVClc);
477 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
478 av_buffer_unref(&ctx->ps.vps_list[i]);
479 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
480 av_buffer_unref(&ctx->ps.sps_list[i]);
481 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
482 av_buffer_unref(&ctx->ps.pps_list[i]);
486 ff_h2645_packet_uninit(&ctx->pkt);
488 av_freep(&ctx->pc.buffer);
491 AVCodecParser ff_hevc_parser = {
492 .codec_ids = { AV_CODEC_ID_HEVC },
493 .priv_data_size = sizeof(HEVCParserContext),
494 .parser_parse = hevc_parse,
495 .parser_close = hevc_parser_close,