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"
28 #include "h2645_parse.h"
31 #define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
33 #define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
35 #define ADVANCED_PARSER CONFIG_HEVC_DECODER
37 typedef struct HEVCParserContext {
51 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
52 AVCodecContext *avctx)
54 HEVCParserContext *ctx = s->priv_data;
55 GetBitContext *gb = &nal->gb;
61 get_bits1(gb); // first slice in pic
63 get_bits1(gb); // no output of prior pics
65 pps_id = get_ue_golomb_long(gb);
66 if (pps_id >= HEVC_MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
67 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
68 return AVERROR_INVALIDDATA;
70 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
71 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
73 /* export the stream parameters */
74 s->coded_width = sps->width;
75 s->coded_height = sps->height;
76 s->width = sps->output_width;
77 s->height = sps->output_height;
78 s->format = sps->pix_fmt;
79 avctx->profile = sps->ptl.general_ptl.profile_idc;
80 avctx->level = sps->ptl.general_ptl.level_idc;
82 /* ignore the rest for now*/
87 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
88 int buf_size, AVCodecContext *avctx)
90 HEVCParserContext *ctx = s->priv_data;
93 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
98 for (i = 0; i < ctx->pkt.nb_nals; i++) {
99 H2645NAL *nal = &ctx->pkt.nals[i];
101 /* ignore everything except parameter sets and VCL NALUs */
103 case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
104 case HEVC_NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
105 case HEVC_NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
106 case HEVC_NAL_TRAIL_R:
107 case HEVC_NAL_TRAIL_N:
110 case HEVC_NAL_STSA_N:
111 case HEVC_NAL_STSA_R:
112 case HEVC_NAL_BLA_W_LP:
113 case HEVC_NAL_BLA_W_RADL:
114 case HEVC_NAL_BLA_N_LP:
115 case HEVC_NAL_IDR_W_RADL:
116 case HEVC_NAL_IDR_N_LP:
117 case HEVC_NAL_CRA_NUT:
118 case HEVC_NAL_RADL_N:
119 case HEVC_NAL_RADL_R:
120 case HEVC_NAL_RASL_N:
121 case HEVC_NAL_RASL_R:
122 if (buf == avctx->extradata) {
123 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", nal->type);
124 return AVERROR_INVALIDDATA;
126 hevc_parse_slice_header(s, nal, avctx);
136 * Find the end of the current frame in the bitstream.
137 * @return the position of the first byte of the next frame, or END_NOT_FOUND
139 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
143 ParseContext *pc = s->priv_data;
145 for (i = 0; i < buf_size; i++) {
148 pc->state64 = (pc->state64 << 8) | buf[i];
150 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
153 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
154 // Beginning of access unit
155 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_AUD) || nut == HEVC_NAL_SEI_PREFIX ||
156 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
157 if (pc->frame_start_found) {
158 pc->frame_start_found = 0;
161 } else if (nut <= HEVC_NAL_RASL_R ||
162 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
163 int first_slice_segment_in_pic_flag = buf[i] >> 7;
164 if (first_slice_segment_in_pic_flag) {
165 if (!pc->frame_start_found) {
166 pc->frame_start_found = 1;
167 } else { // First slice of next frame found
168 pc->frame_start_found = 0;
175 return END_NOT_FOUND;
180 * Parse NAL units of found picture and decode some basic information.
182 * @param s parser context.
183 * @param avctx codec context.
184 * @param buf buffer with field/frame data.
185 * @param buf_size size of the buffer.
187 static inline int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
188 int buf_size, AVCodecContext *avctx)
190 HEVCParserContext *ctx = s->priv_data;
191 HEVCContext *h = &ctx->h;
193 SliceHeader *sh = &h->sh;
194 HEVCParamSets *ps = &h->ps;
195 HEVCSEIContext *sei = &h->sei;
196 H2645Packet *pkt = &ctx->pkt;
197 const uint8_t *buf_end = buf + buf_size;
200 int is_global = buf == avctx->extradata;
203 h->HEVClc = av_mallocz(sizeof(HEVCLocalContext));
205 return AVERROR(ENOMEM);
209 /* set some sane default values */
210 s->pict_type = AV_PICTURE_TYPE_I;
212 s->picture_structure = AV_PICTURE_STRUCTURE_UNKNOWN;
216 ff_hevc_reset_sei(sei);
221 if (pkt->nals_allocated < 1) {
222 H2645NAL *tmp = av_realloc_array(pkt->nals, 1, sizeof(*tmp));
224 return AVERROR(ENOMEM);
226 memset(pkt->nals, 0, sizeof(*tmp));
227 pkt->nals_allocated = 1;
233 int src_length, consumed;
235 int num = 0, den = 0;
236 buf = avpriv_find_start_code(buf, buf_end, &state);
237 if (--buf + 2 >= buf_end)
239 src_length = buf_end - buf;
241 h->nal_unit_type = (*buf >> 1) & 0x3f;
242 h->temporal_id = (*(buf + 1) & 0x07) - 1;
243 if (h->nal_unit_type <= HEVC_NAL_CRA_NUT) {
244 // Do not walk the whole buffer just to decode slice segment header
249 consumed = ff_h2645_extract_rbsp(buf, src_length, nal, 1);
253 ret = init_get_bits8(gb, nal->data + 2, nal->size);
257 switch (h->nal_unit_type) {
259 ff_hevc_decode_nal_vps(gb, avctx, ps);
262 ff_hevc_decode_nal_sps(gb, avctx, ps, 1);
265 ff_hevc_decode_nal_pps(gb, avctx, ps);
267 case HEVC_NAL_SEI_PREFIX:
268 case HEVC_NAL_SEI_SUFFIX:
269 ff_hevc_decode_nal_sei(gb, avctx, sei, ps, h->nal_unit_type);
271 case HEVC_NAL_TRAIL_N:
272 case HEVC_NAL_TRAIL_R:
275 case HEVC_NAL_STSA_N:
276 case HEVC_NAL_STSA_R:
277 case HEVC_NAL_RADL_N:
278 case HEVC_NAL_RADL_R:
279 case HEVC_NAL_RASL_N:
280 case HEVC_NAL_RASL_R:
281 case HEVC_NAL_BLA_W_LP:
282 case HEVC_NAL_BLA_W_RADL:
283 case HEVC_NAL_BLA_N_LP:
284 case HEVC_NAL_IDR_W_RADL:
285 case HEVC_NAL_IDR_N_LP:
286 case HEVC_NAL_CRA_NUT:
289 av_log(avctx, AV_LOG_ERROR, "Invalid NAL unit: %d\n", h->nal_unit_type);
290 return AVERROR_INVALIDDATA;
293 sh->first_slice_in_pic_flag = get_bits1(gb);
294 s->picture_structure = h->sei.picture_timing.picture_struct;
295 s->field_order = h->sei.picture_timing.picture_struct;
299 sh->no_output_of_prior_pics_flag = get_bits1(gb);
302 sh->pps_id = get_ue_golomb(gb);
303 if (sh->pps_id >= HEVC_MAX_PPS_COUNT || !ps->pps_list[sh->pps_id]) {
304 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", sh->pps_id);
305 return AVERROR_INVALIDDATA;
307 ps->pps = (HEVCPPS*)ps->pps_list[sh->pps_id]->data;
309 if (ps->pps->sps_id >= HEVC_MAX_SPS_COUNT || !ps->sps_list[ps->pps->sps_id]) {
310 av_log(avctx, AV_LOG_ERROR, "SPS id out of range: %d\n", ps->pps->sps_id);
311 return AVERROR_INVALIDDATA;
313 if (ps->sps != (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data) {
314 ps->sps = (HEVCSPS*)ps->sps_list[ps->pps->sps_id]->data;
315 ps->vps = (HEVCVPS*)ps->vps_list[ps->sps->vps_id]->data;
318 s->coded_width = ps->sps->width;
319 s->coded_height = ps->sps->height;
320 s->width = ps->sps->output_width;
321 s->height = ps->sps->output_height;
322 s->format = ps->sps->pix_fmt;
323 avctx->profile = ps->sps->ptl.general_ptl.profile_idc;
324 avctx->level = ps->sps->ptl.general_ptl.level_idc;
326 if (ps->vps->vps_timing_info_present_flag) {
327 num = ps->vps->vps_num_units_in_tick;
328 den = ps->vps->vps_time_scale;
329 } else if (ps->sps->vui.vui_timing_info_present_flag) {
330 num = ps->sps->vui.vui_num_units_in_tick;
331 den = ps->sps->vui.vui_time_scale;
334 if (num != 0 && den != 0)
335 av_reduce(&avctx->framerate.den, &avctx->framerate.num,
338 if (!sh->first_slice_in_pic_flag) {
339 int slice_address_length;
341 if (ps->pps->dependent_slice_segments_enabled_flag)
342 sh->dependent_slice_segment_flag = get_bits1(gb);
344 sh->dependent_slice_segment_flag = 0;
346 slice_address_length = av_ceil_log2_c(ps->sps->ctb_width *
347 ps->sps->ctb_height);
348 sh->slice_segment_addr = get_bitsz(gb, slice_address_length);
349 if (sh->slice_segment_addr >= ps->sps->ctb_width * ps->sps->ctb_height) {
350 av_log(avctx, AV_LOG_ERROR, "Invalid slice segment address: %u.\n",
351 sh->slice_segment_addr);
352 return AVERROR_INVALIDDATA;
355 sh->dependent_slice_segment_flag = 0;
357 if (sh->dependent_slice_segment_flag)
360 for (i = 0; i < ps->pps->num_extra_slice_header_bits; i++)
361 skip_bits(gb, 1); // slice_reserved_undetermined_flag[]
363 sh->slice_type = get_ue_golomb(gb);
364 if (!(sh->slice_type == HEVC_SLICE_I || sh->slice_type == HEVC_SLICE_P ||
365 sh->slice_type == HEVC_SLICE_B)) {
366 av_log(avctx, AV_LOG_ERROR, "Unknown slice type: %d.\n",
368 return AVERROR_INVALIDDATA;
370 s->pict_type = sh->slice_type == HEVC_SLICE_B ? AV_PICTURE_TYPE_B :
371 sh->slice_type == HEVC_SLICE_P ? AV_PICTURE_TYPE_P :
374 if (ps->pps->output_flag_present_flag)
375 sh->pic_output_flag = get_bits1(gb);
377 if (ps->sps->separate_colour_plane_flag)
378 sh->colour_plane_id = get_bits(gb, 2);
381 sh->pic_order_cnt_lsb = get_bits(gb, ps->sps->log2_max_poc_lsb);
382 s->output_picture_number = h->poc = ff_hevc_compute_poc(h, sh->pic_order_cnt_lsb);
384 s->output_picture_number = h->poc = 0;
386 if (h->temporal_id == 0 &&
387 h->nal_unit_type != HEVC_NAL_TRAIL_N &&
388 h->nal_unit_type != HEVC_NAL_TSA_N &&
389 h->nal_unit_type != HEVC_NAL_STSA_N &&
390 h->nal_unit_type != HEVC_NAL_RADL_N &&
391 h->nal_unit_type != HEVC_NAL_RASL_N &&
392 h->nal_unit_type != HEVC_NAL_RADL_R &&
393 h->nal_unit_type != HEVC_NAL_RASL_R)
396 return 0; /* no need to evaluate the rest */
400 /* didn't find a picture! */
402 av_log(h->avctx, AV_LOG_ERROR, "missing picture in access unit\n");
407 static int hevc_parse(AVCodecParserContext *s,
408 AVCodecContext *avctx,
409 const uint8_t **poutbuf, int *poutbuf_size,
410 const uint8_t *buf, int buf_size)
413 HEVCParserContext *ctx = s->priv_data;
414 ParseContext *pc = &ctx->pc;
416 if (avctx->extradata && !ctx->parsed_extradata) {
417 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
418 ctx->parsed_extradata = 1;
421 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
424 next = hevc_find_frame_end(s, buf, buf_size);
425 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
432 parse_nal_units(s, buf, buf_size, avctx);
435 *poutbuf_size = buf_size;
439 // Split after the parameter sets at the beginning of the stream if they exist.
440 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
442 const uint8_t *ptr = buf, *end = buf + buf_size;
450 ptr = avpriv_find_start_code(ptr, end, &state);
451 if ((state >> 8) != START_CODE)
453 nut = (state >> 1) & 0x3F;
454 if (nut == HEVC_NAL_VPS)
456 else if (nut == HEVC_NAL_SPS)
458 else if (nut == HEVC_NAL_PPS)
460 else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
461 nut != HEVC_NAL_AUD) {
462 if (has_vps && has_sps) {
463 while (ptr - 4 > buf && ptr[-5] == 0)
465 return ptr - 4 - buf;
472 static void hevc_parser_close(AVCodecParserContext *s)
474 HEVCParserContext *ctx = s->priv_data;
478 HEVCContext *h = &ctx->h;
480 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.vps_list); i++)
481 av_buffer_unref(&h->ps.vps_list[i]);
482 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.sps_list); i++)
483 av_buffer_unref(&h->ps.sps_list[i]);
484 for (i = 0; i < FF_ARRAY_ELEMS(h->ps.pps_list); i++)
485 av_buffer_unref(&h->ps.pps_list[i]);
489 av_freep(&h->HEVClc);
492 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
493 av_buffer_unref(&ctx->ps.vps_list[i]);
494 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
495 av_buffer_unref(&ctx->ps.sps_list[i]);
496 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
497 av_buffer_unref(&ctx->ps.pps_list[i]);
501 ff_h2645_packet_uninit(&ctx->pkt);
503 av_freep(&ctx->pc.buffer);
506 AVCodecParser ff_hevc_parser = {
507 .codec_ids = { AV_CODEC_ID_HEVC },
508 .priv_data_size = sizeof(HEVCParserContext),
509 .parser_parse = hevc_parse,
510 .parser_close = hevc_parser_close,