avcodec/extract_extradata: don't uninitialize the H2645Packet on every processed...
[libav.git] / libavcodec / extract_extradata_bsf.c
CommitLineData
89b35a13
AK
1/*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdint.h>
20
21#include "libavutil/common.h"
22#include "libavutil/intreadwrite.h"
23#include "libavutil/log.h"
24#include "libavutil/opt.h"
25
26#include "avcodec.h"
27#include "bsf.h"
28#include "h2645_parse.h"
29#include "h264.h"
30#include "hevc.h"
31#include "vc1_common.h"
32
33typedef struct ExtractExtradataContext {
34 const AVClass *class;
35
36 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
37 uint8_t **data, int *size);
38
3365e91b
JA
39 /* H264/HEVC specifc fields */
40 H2645Packet h2645_pkt;
41
89b35a13
AK
42 /* AVOptions */
43 int remove;
44} ExtractExtradataContext;
45
46static int val_in_array(const int *arr, int len, int val)
47{
48 int i;
49 for (i = 0; i < len; i++)
50 if (arr[i] == val)
51 return 1;
52 return 0;
53}
54
55static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
56 uint8_t **data, int *size)
57{
58 static const int extradata_nal_types_hevc[] = {
59 HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
60 };
61 static const int extradata_nal_types_h264[] = {
62 H264_NAL_SPS, H264_NAL_PPS,
63 };
64
65 ExtractExtradataContext *s = ctx->priv_data;
66
70f11ee9 67 int extradata_size = 0, filtered_size = 0;
89b35a13
AK
68 const int *extradata_nal_types;
69 int nb_extradata_nal_types;
d7dcd825 70 int i, has_sps = 0, has_vps = 0, ret = 0;
89b35a13
AK
71
72 if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
73 extradata_nal_types = extradata_nal_types_hevc;
74 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
75 } else {
76 extradata_nal_types = extradata_nal_types_h264;
77 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
78 }
79
3365e91b 80 ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
89b35a13
AK
81 ctx, 0, 0, ctx->par_in->codec_id);
82 if (ret < 0)
3365e91b 83 return ret;
89b35a13 84
3365e91b
JA
85 for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
86 H2645NAL *nal = &s->h2645_pkt.nals[i];
d7dcd825 87 if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
89b35a13 88 extradata_size += nal->raw_size + 3;
d7dcd825
JA
89 if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
90 if (nal->type == HEVC_NAL_SPS) has_sps = 1;
91 if (nal->type == HEVC_NAL_VPS) has_vps = 1;
92 } else {
93 if (nal->type == H264_NAL_SPS) has_sps = 1;
94 }
70f11ee9
JA
95 } else if (s->remove) {
96 filtered_size += nal->raw_size + 3;
d7dcd825 97 }
89b35a13
AK
98 }
99
d7dcd825
JA
100 if (extradata_size &&
101 ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
102 (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
89b35a13
AK
103 AVBufferRef *filtered_buf;
104 uint8_t *extradata, *filtered_data;
105
106 if (s->remove) {
70f11ee9 107 filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
20142310 108 if (!filtered_buf) {
3365e91b 109 return AVERROR(ENOMEM);
20142310 110 }
70f11ee9
JA
111 memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
112
89b35a13
AK
113 filtered_data = filtered_buf->data;
114 }
115
116 extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
117 if (!extradata) {
118 av_buffer_unref(&filtered_buf);
3365e91b 119 return AVERROR(ENOMEM);
89b35a13 120 }
be65995d 121 memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
89b35a13
AK
122
123 *data = extradata;
124 *size = extradata_size;
125
3365e91b
JA
126 for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
127 H2645NAL *nal = &s->h2645_pkt.nals[i];
89b35a13
AK
128 if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
129 nal->type)) {
130 AV_WB24(extradata, 1); // startcode
131 memcpy(extradata + 3, nal->raw_data, nal->raw_size);
132 extradata += 3 + nal->raw_size;
133 } else if (s->remove) {
134 AV_WB24(filtered_data, 1); // startcode
135 memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
136 filtered_data += 3 + nal->raw_size;
137 }
138 }
139
140 if (s->remove) {
141 av_buffer_unref(&pkt->buf);
142 pkt->buf = filtered_buf;
143 pkt->data = filtered_buf->data;
70f11ee9 144 pkt->size = filtered_size;
89b35a13
AK
145 }
146 }
147
3365e91b 148 return 0;
89b35a13
AK
149}
150
151static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
152 uint8_t **data, int *size)
153{
154 ExtractExtradataContext *s = ctx->priv_data;
155 uint32_t state = UINT32_MAX;
156 int has_extradata = 0, extradata_size = 0;
157 int i;
158
159 for (i = 0; i < pkt->size; i++) {
160 state = (state << 8) | pkt->data[i];
161 if (IS_MARKER(state)) {
162 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
163 has_extradata = 1;
164 } else if (has_extradata) {
165 extradata_size = i - 3;
166 break;
167 }
168 }
169 }
170
171 if (extradata_size) {
172 *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
173 if (!*data)
174 return AVERROR(ENOMEM);
175
176 memcpy(*data, pkt->data, extradata_size);
be65995d 177 memset(*data + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
89b35a13
AK
178 *size = extradata_size;
179
180 if (s->remove) {
181 pkt->data += extradata_size;
182 pkt->size -= extradata_size;
183 }
184 }
185
186 return 0;
187}
188
189static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
190 uint8_t **data, int *size)
191{
192 ExtractExtradataContext *s = ctx->priv_data;
193 int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
194 ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO;
195 uint32_t state = UINT32_MAX;
196 int i;
197
198 for (i = 0; i < pkt->size; i++) {
199 state = (state << 8) | pkt->data[i];
200 if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) ||
201 (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) {
202 if (i > 3) {
203 *size = i - 3;
204 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
205 if (!*data)
206 return AVERROR(ENOMEM);
207
208 memcpy(*data, pkt->data, *size);
be65995d 209 memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
89b35a13
AK
210
211 if (s->remove) {
212 pkt->data += *size;
213 pkt->size -= *size;
214 }
215 }
216 break;
217 }
218 }
219 return 0;
220}
221
222static const struct {
223 enum AVCodecID id;
224 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
225 uint8_t **data, int *size);
226} extract_tab[] = {
227 { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 },
228 { AV_CODEC_ID_H264, extract_extradata_h2645 },
229 { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
230 { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 },
231 { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 },
232 { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 },
233 { AV_CODEC_ID_VC1, extract_extradata_vc1 },
234};
235
236static int extract_extradata_init(AVBSFContext *ctx)
237{
238 ExtractExtradataContext *s = ctx->priv_data;
239 int i;
240
241 for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
242 if (extract_tab[i].id == ctx->par_in->codec_id) {
243 s->extract = extract_tab[i].extract;
244 break;
245 }
246 }
247 if (!s->extract)
248 return AVERROR_BUG;
249
250 return 0;
251}
252
6442d8ba 253static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
89b35a13
AK
254{
255 ExtractExtradataContext *s = ctx->priv_data;
89b35a13
AK
256 uint8_t *extradata = NULL;
257 int extradata_size;
258 int ret = 0;
259
6442d8ba 260 ret = ff_bsf_get_packet_ref(ctx, pkt);
89b35a13
AK
261 if (ret < 0)
262 return ret;
263
6442d8ba 264 ret = s->extract(ctx, pkt, &extradata, &extradata_size);
89b35a13
AK
265 if (ret < 0)
266 goto fail;
267
268 if (extradata) {
6442d8ba 269 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
89b35a13
AK
270 extradata, extradata_size);
271 if (ret < 0) {
272 av_freep(&extradata);
273 goto fail;
274 }
275 }
276
6442d8ba 277 return 0;
89b35a13
AK
278
279fail:
6442d8ba 280 av_packet_unref(pkt);
89b35a13
AK
281 return ret;
282}
283
3365e91b
JA
284static void extract_extradata_close(AVBSFContext *ctx)
285{
286 ExtractExtradataContext *s = ctx->priv_data;
287 ff_h2645_packet_uninit(&s->h2645_pkt);
288}
289
89b35a13
AK
290static const enum AVCodecID codec_ids[] = {
291 AV_CODEC_ID_CAVS,
292 AV_CODEC_ID_H264,
293 AV_CODEC_ID_HEVC,
294 AV_CODEC_ID_MPEG1VIDEO,
295 AV_CODEC_ID_MPEG2VIDEO,
296 AV_CODEC_ID_MPEG4,
297 AV_CODEC_ID_VC1,
298 AV_CODEC_ID_NONE,
299};
300
301#define OFFSET(x) offsetof(ExtractExtradataContext, x)
302static const AVOption options[] = {
303 { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
304 { .i64 = 0 }, 0, 1 },
305 { NULL },
306};
307
308static const AVClass extract_extradata_class = {
309 .class_name = "extract_extradata",
310 .item_name = av_default_item_name,
311 .option = options,
312 .version = LIBAVUTIL_VERSION_INT,
313};
314
315const AVBitStreamFilter ff_extract_extradata_bsf = {
316 .name = "extract_extradata",
317 .codec_ids = codec_ids,
318 .priv_data_size = sizeof(ExtractExtradataContext),
319 .priv_class = &extract_extradata_class,
320 .init = extract_extradata_init,
321 .filter = extract_extradata_filter,
3365e91b 322 .close = extract_extradata_close,
89b35a13 323};