h2645_parse: add support for parsing h264
[libav.git] / libavcodec / hevc_parser.c
CommitLineData
064698d3
GM
1/*
2 * HEVC Annex B format parser
3 *
4 * Copyright (C) 2012 - 2013 Guillaume Martres
5 *
6 * This file is part of Libav.
7 *
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.
12 *
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.
17 *
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
21 */
22
23#include "libavutil/common.h"
24
650060df 25#include "golomb.h"
064698d3 26#include "hevc.h"
fa936a30 27#include "h2645_parse.h"
650060df 28#include "parser.h"
064698d3
GM
29
30#define START_CODE 0x000001 ///< start_code_prefix_one_3bytes
31
650060df
AK
32#define IS_IRAP_NAL(nal) (nal->type >= 16 && nal->type <= 23)
33
34typedef struct HEVCParserContext {
35 ParseContext pc;
36
fa936a30 37 H2645Packet pkt;
650060df
AK
38 HEVCParamSets ps;
39
40 int parsed_extradata;
41} HEVCParserContext;
42
fa936a30 43static int hevc_parse_slice_header(AVCodecParserContext *s, H2645NAL *nal,
650060df
AK
44 AVCodecContext *avctx)
45{
46 HEVCParserContext *ctx = s->priv_data;
47 GetBitContext *gb = &nal->gb;
48
49 HEVCPPS *pps;
50 HEVCSPS *sps;
51 unsigned int pps_id;
52
53 get_bits1(gb); // first slice in pic
54 if (IS_IRAP_NAL(nal))
55 get_bits1(gb); // no output of prior pics
56
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;
61 }
62 pps = (HEVCPPS*)ctx->ps.pps_list[pps_id]->data;
63 sps = (HEVCSPS*)ctx->ps.sps_list[pps->sps_id]->data;
64
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;
73
74 /* ignore the rest for now*/
75
76 return 0;
77}
78
79static int parse_nal_units(AVCodecParserContext *s, const uint8_t *buf,
80 int buf_size, AVCodecContext *avctx)
81{
82 HEVCParserContext *ctx = s->priv_data;
83 int ret, i;
84
b667252a
AK
85 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0,
86 AV_CODEC_ID_HEVC);
650060df
AK
87 if (ret < 0)
88 return ret;
89
90 for (i = 0; i < ctx->pkt.nb_nals; i++) {
fa936a30 91 H2645NAL *nal = &ctx->pkt.nals[i];
650060df
AK
92
93 /* ignore everything except parameter sets and VCL NALUs */
94 switch (nal->type) {
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;
98 case NAL_TRAIL_R:
99 case NAL_TRAIL_N:
100 case NAL_TSA_N:
101 case NAL_TSA_R:
102 case NAL_STSA_N:
103 case NAL_STSA_R:
104 case NAL_BLA_W_LP:
105 case NAL_BLA_W_RADL:
106 case NAL_BLA_N_LP:
107 case NAL_IDR_W_RADL:
108 case NAL_IDR_N_LP:
109 case NAL_CRA_NUT:
110 case NAL_RADL_N:
111 case NAL_RADL_R:
112 case NAL_RASL_N:
113 case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
114 }
115 }
116
117 return 0;
118}
119
064698d3
GM
120/**
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
123 */
124static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
125 int buf_size)
126{
650060df
AK
127 HEVCParserContext *ctx = s->priv_data;
128 ParseContext *pc = &ctx->pc;
064698d3 129 int i;
064698d3
GM
130
131 for (i = 0; i < buf_size; i++) {
132 int nut;
133
134 pc->state64 = (pc->state64 << 8) | buf[i];
135
136 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
137 continue;
138
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;
145 return i - 5;
146 }
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;
156 return i - 5;
157 }
158 }
159 }
160 }
161
162 return END_NOT_FOUND;
163}
164
165static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
166 const uint8_t **poutbuf, int *poutbuf_size,
167 const uint8_t *buf, int buf_size)
168{
169 int next;
650060df
AK
170
171 HEVCParserContext *ctx = s->priv_data;
172 ParseContext *pc = &ctx->pc;
173
174 if (avctx->extradata && !ctx->parsed_extradata) {
175 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
176 ctx->parsed_extradata = 1;
177 }
064698d3
GM
178
179 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
180 next = buf_size;
181 } else {
182 next = hevc_find_frame_end(s, buf, buf_size);
183 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
184 *poutbuf = NULL;
185 *poutbuf_size = 0;
186 return buf_size;
187 }
188 }
189
650060df
AK
190 parse_nal_units(s, buf, buf_size, avctx);
191
064698d3
GM
192 *poutbuf = buf;
193 *poutbuf_size = buf_size;
194 return next;
195}
196
197// Split after the parameter sets at the beginning of the stream if they exist.
198static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
199{
200 int i;
201 uint32_t state = -1;
202 int has_ps = 0;
203
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)
209 has_ps = 1;
210 else if (has_ps)
211 return i - 3;
212 else // no parameter set at the beginning of the stream
213 return 0;
214 }
215 }
216 return 0;
217}
218
650060df
AK
219static void hevc_parser_close(AVCodecParserContext *s)
220{
221 HEVCParserContext *ctx = s->priv_data;
222 int i;
223
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]);
230
8229eff4 231 ff_h2645_packet_uninit(&ctx->pkt);
650060df
AK
232
233 av_freep(&ctx->pc.buffer);
234}
235
064698d3
GM
236AVCodecParser ff_hevc_parser = {
237 .codec_ids = { AV_CODEC_ID_HEVC },
650060df 238 .priv_data_size = sizeof(HEVCParserContext),
064698d3 239 .parser_parse = hevc_parse,
650060df 240 .parser_close = hevc_parser_close,
064698d3
GM
241 .split = hevc_split,
242};