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"
25 #include "golomb_legacy.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 typedef struct HEVCParserContext {
44 static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
45 AVCodecContext *avctx)
47 HEVCParserContext *ctx = s->priv_data;
48 GetBitContext *gb = &nal->gb;
55 get_bits1(gb); // first slice in pic
57 get_bits1(gb); // no output of prior pics
59 pps_id = get_ue_golomb_long(gb);
60 if (pps_id >= HEVC_MAX_PPS_COUNT || !ctx->ps.pps_list[pps_id]) {
61 av_log(avctx, AV_LOG_ERROR, "PPS id out of range: %d\n", pps_id);
62 return AVERROR_INVALIDDATA;
64 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
65 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
66 ow = &sps->output_window;
68 /* export the stream parameters */
69 s->coded_width = sps->width;
70 s->coded_height = sps->height;
71 s->width = sps->width - ow->left_offset - ow->right_offset;
72 s->height = sps->height - ow->top_offset - ow->bottom_offset;
73 s->format = sps->pix_fmt;
74 avctx->profile = sps->ptl.general_ptl.profile_idc;
75 avctx->level = sps->ptl.general_ptl.level_idc;
77 /* ignore the rest for now*/
82 static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
83 int buf_size, AVCodecContext *avctx)
85 HEVCParserContext *ctx = s->priv_data;
88 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
93 for (i = 0; i < ctx->pkt.nb_nals; i++) {
94 H2645NAL *nal = &ctx->pkt.nals[i];
96 /* ignore everything except parameter sets and VCL NALUs */
98 case HEVC_NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
99 case HEVC_NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
100 case HEVC_NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
101 case HEVC_NAL_TRAIL_R:
102 case HEVC_NAL_TRAIL_N:
105 case HEVC_NAL_STSA_N:
106 case HEVC_NAL_STSA_R:
107 case HEVC_NAL_BLA_W_LP:
108 case HEVC_NAL_BLA_W_RADL:
109 case HEVC_NAL_BLA_N_LP:
110 case HEVC_NAL_IDR_W_RADL:
111 case HEVC_NAL_IDR_N_LP:
112 case HEVC_NAL_CRA_NUT:
113 case HEVC_NAL_RADL_N:
114 case HEVC_NAL_RADL_R:
115 case HEVC_NAL_RASL_N:
116 case HEVC_NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
124 * Find the end of the current frame in the bitstream.
125 * @return the position of the first byte of the next frame, or END_NOT_FOUND
127 static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
130 HEVCParserContext *ctx = s->priv_data;
131 ParseContext *pc = &ctx->pc;
134 for (i = 0; i < buf_size; i++) {
137 pc->state64 = (pc->state64 << 8) | buf[i];
139 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
142 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
143 // Beginning of access unit
144 if ((nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_AUD) || nut == HEVC_NAL_SEI_PREFIX ||
145 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
146 if (pc->frame_start_found) {
147 pc->frame_start_found = 0;
150 } else if (nut <= HEVC_NAL_RASL_R ||
151 (nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT)) {
152 int first_slice_segment_in_pic_flag = buf[i] >> 7;
153 if (first_slice_segment_in_pic_flag) {
154 if (!pc->frame_start_found) {
155 pc->frame_start_found = 1;
156 s->key_frame = nut >= HEVC_NAL_BLA_W_LP && nut <= HEVC_NAL_CRA_NUT;
157 } else { // First slice of next frame found
158 pc->frame_start_found = 0;
165 return END_NOT_FOUND;
168 static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
169 const uint8_t **poutbuf, int *poutbuf_size,
170 const uint8_t *buf, int buf_size)
174 HEVCParserContext *ctx = s->priv_data;
175 ParseContext *pc = &ctx->pc;
177 if (avctx->extradata && !ctx->parsed_extradata) {
178 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
179 ctx->parsed_extradata = 1;
182 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
185 next = hevc_find_frame_end(s, buf, buf_size);
186 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
193 parse_nal_units(s, buf, buf_size, avctx);
196 *poutbuf_size = buf_size;
200 // Split after the parameter sets at the beginning of the stream if they exist.
201 static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
207 for (i = 0; i < buf_size; i++) {
208 state = (state << 8) | buf[i];
209 if (((state >> 8) & 0xFFFFFF) == START_CODE) {
210 int nut = (state >> 1) & 0x3F;
211 if (nut >= HEVC_NAL_VPS && nut <= HEVC_NAL_PPS)
215 else // no parameter set at the beginning of the stream
222 static void hevc_parser_close(AVCodecParserContext *s)
224 HEVCParserContext *ctx = s->priv_data;
227 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
228 av_buffer_unref(&ctx->ps.vps_list[i]);
229 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
230 av_buffer_unref(&ctx->ps.sps_list[i]);
231 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
232 av_buffer_unref(&ctx->ps.pps_list[i]);
234 ff_h2645_packet_uninit(&ctx->pkt);
236 av_freep(&ctx->pc.buffer);
239 AVCodecParser ff_hevc_parser = {
240 .codec_ids = { AV_CODEC_ID_HEVC },
241 .priv_data_size = sizeof(HEVCParserContext),
242 .parser_parse = hevc_parse,
243 .parser_close = hevc_parser_close,