2 * HEVC Annex B format parser
4 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * This file is part of Libav.
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.
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.
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
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 typedef struct HEVCParserContext {
43 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
44 AVCodecContext *avctx)
46 HEVCParserContext *ctx = s->priv_data;
47 GetBitContext *gb = &nal->gb;
53 get_bits1(gb); // first slice in pic
55 get_bits1(gb); // no output of prior pics
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;
62 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
63 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
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;
74 /* ignore the rest for now*/
79 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
80 int buf_size, AVCodecContext *avctx)
82 HEVCParserContext *ctx = s->priv_data;
85 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
90 for (i = 0; i < ctx->pkt.nb_nals; i++) {
91 H2645NAL *nal = &ctx->pkt.nals[i];
93 /* ignore everything except parameter sets and VCL NALUs */
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;
113 case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
121 * Find the end of the current frame in the bitstream.
122 * @return the position of the first byte of the next frame, or END_NOT_FOUND
124 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
127 HEVCParserContext *ctx = s->priv_data;
128 ParseContext *pc = &ctx->pc;
131 for (i = 0; i < buf_size; i++) {
134 pc->state64 = (pc->state64 << 8) | buf[i];
136 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
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;
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;
162 return END_NOT_FOUND;
165 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
166 const uint8_t **poutbuf, int *poutbuf_size,
167 const uint8_t *buf, int buf_size)
171 HEVCParserContext *ctx = s->priv_data;
172 ParseContext *pc = &ctx->pc;
174 if (avctx->extradata && !ctx->parsed_extradata) {
175 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
176 ctx->parsed_extradata = 1;
179 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
182 next = hevc_find_frame_end(s, buf, buf_size);
183 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
190 parse_nal_units(s, buf, buf_size, avctx);
193 *poutbuf_size = buf_size;
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)
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)
212 else // no parameter set at the beginning of the stream
219 static void hevc_parser_close(AVCodecParserContext *s)
221 HEVCParserContext *ctx = s->priv_data;
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]);
231 ff_h2645_packet_uninit(&ctx->pkt);
233 av_freep(&ctx->pc.buffer);
236 AVCodecParser ff_hevc_parser = {
237 .codec_ids = { AV_CODEC_ID_HEVC },
238 .priv_data_size = sizeof(HEVCParserContext),
239 .parser_parse = hevc_parse,
240 .parser_close = hevc_parser_close,