h2645_parse: add a function for uninitializing the packet
[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
fa936a30 85 ret = ff_h2645_packet_split(&ctx->pkt, buf, buf_size, avctx, 0, 0);
650060df
AK
86 if (ret < 0)
87 return ret;
88
89 for (i = 0; i < ctx->pkt.nb_nals; i++) {
fa936a30 90 H2645NAL *nal = &ctx->pkt.nals[i];
650060df
AK
91
92 /* ignore everything except parameter sets and VCL NALUs */
93 switch (nal->type) {
94 case NAL_VPS: ff_hevc_decode_nal_vps(&nal->gb, avctx, &ctx->ps); break;
95 case NAL_SPS: ff_hevc_decode_nal_sps(&nal->gb, avctx, &ctx->ps, 1); break;
96 case NAL_PPS: ff_hevc_decode_nal_pps(&nal->gb, avctx, &ctx->ps); break;
97 case NAL_TRAIL_R:
98 case NAL_TRAIL_N:
99 case NAL_TSA_N:
100 case NAL_TSA_R:
101 case NAL_STSA_N:
102 case NAL_STSA_R:
103 case NAL_BLA_W_LP:
104 case NAL_BLA_W_RADL:
105 case NAL_BLA_N_LP:
106 case NAL_IDR_W_RADL:
107 case NAL_IDR_N_LP:
108 case NAL_CRA_NUT:
109 case NAL_RADL_N:
110 case NAL_RADL_R:
111 case NAL_RASL_N:
112 case NAL_RASL_R: hevc_parse_slice_header(s, nal, avctx); break;
113 }
114 }
115
116 return 0;
117}
118
064698d3
GM
119/**
120 * Find the end of the current frame in the bitstream.
121 * @return the position of the first byte of the next frame, or END_NOT_FOUND
122 */
123static int hevc_find_frame_end(AVCodecParserContext *s, const uint8_t *buf,
124 int buf_size)
125{
650060df
AK
126 HEVCParserContext *ctx = s->priv_data;
127 ParseContext *pc = &ctx->pc;
064698d3 128 int i;
064698d3
GM
129
130 for (i = 0; i < buf_size; i++) {
131 int nut;
132
133 pc->state64 = (pc->state64 << 8) | buf[i];
134
135 if (((pc->state64 >> 3 * 8) & 0xFFFFFF) != START_CODE)
136 continue;
137
138 nut = (pc->state64 >> 2 * 8 + 1) & 0x3F;
139 // Beginning of access unit
140 if ((nut >= NAL_VPS && nut <= NAL_AUD) || nut == NAL_SEI_PREFIX ||
141 (nut >= 41 && nut <= 44) || (nut >= 48 && nut <= 55)) {
142 if (pc->frame_start_found) {
143 pc->frame_start_found = 0;
144 return i - 5;
145 }
146 } else if (nut <= NAL_RASL_R ||
147 (nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT)) {
148 int first_slice_segment_in_pic_flag = buf[i] >> 7;
149 if (first_slice_segment_in_pic_flag) {
150 if (!pc->frame_start_found) {
151 pc->frame_start_found = 1;
152 s->key_frame = nut >= NAL_BLA_W_LP && nut <= NAL_CRA_NUT;
153 } else { // First slice of next frame found
154 pc->frame_start_found = 0;
155 return i - 5;
156 }
157 }
158 }
159 }
160
161 return END_NOT_FOUND;
162}
163
164static int hevc_parse(AVCodecParserContext *s, AVCodecContext *avctx,
165 const uint8_t **poutbuf, int *poutbuf_size,
166 const uint8_t *buf, int buf_size)
167{
168 int next;
650060df
AK
169
170 HEVCParserContext *ctx = s->priv_data;
171 ParseContext *pc = &ctx->pc;
172
173 if (avctx->extradata && !ctx->parsed_extradata) {
174 parse_nal_units(s, avctx->extradata, avctx->extradata_size, avctx);
175 ctx->parsed_extradata = 1;
176 }
064698d3
GM
177
178 if (s->flags & PARSER_FLAG_COMPLETE_FRAMES) {
179 next = buf_size;
180 } else {
181 next = hevc_find_frame_end(s, buf, buf_size);
182 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
183 *poutbuf = NULL;
184 *poutbuf_size = 0;
185 return buf_size;
186 }
187 }
188
650060df
AK
189 parse_nal_units(s, buf, buf_size, avctx);
190
064698d3
GM
191 *poutbuf = buf;
192 *poutbuf_size = buf_size;
193 return next;
194}
195
196// Split after the parameter sets at the beginning of the stream if they exist.
197static int hevc_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
198{
199 int i;
200 uint32_t state = -1;
201 int has_ps = 0;
202
203 for (i = 0; i < buf_size; i++) {
204 state = (state << 8) | buf[i];
205 if (((state >> 8) & 0xFFFFFF) == START_CODE) {
206 int nut = (state >> 1) & 0x3F;
207 if (nut >= NAL_VPS && nut <= NAL_PPS)
208 has_ps = 1;
209 else if (has_ps)
210 return i - 3;
211 else // no parameter set at the beginning of the stream
212 return 0;
213 }
214 }
215 return 0;
216}
217
650060df
AK
218static void hevc_parser_close(AVCodecParserContext *s)
219{
220 HEVCParserContext *ctx = s->priv_data;
221 int i;
222
223 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.vps_list); i++)
224 av_buffer_unref(&ctx->ps.vps_list[i]);
225 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.sps_list); i++)
226 av_buffer_unref(&ctx->ps.sps_list[i]);
227 for (i = 0; i < FF_ARRAY_ELEMS(ctx->ps.pps_list); i++)
228 av_buffer_unref(&ctx->ps.pps_list[i]);
229
8229eff4 230 ff_h2645_packet_uninit(&ctx->pkt);
650060df
AK
231
232 av_freep(&ctx->pc.buffer);
233}
234
064698d3
GM
235AVCodecParser ff_hevc_parser = {
236 .codec_ids = { AV_CODEC_ID_HEVC },
650060df 237 .priv_data_size = sizeof(HEVCParserContext),
064698d3 238 .parser_parse = hevc_parse,
650060df 239 .parser_close = hevc_parser_close,
064698d3
GM
240 .split = hevc_split,
241};