extract_extradata: return an error when buffer allocation fails
[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
39 /* AVOptions */
40 int remove;
41} ExtractExtradataContext;
42
43static int val_in_array(const int *arr, int len, int val)
44{
45 int i;
46 for (i = 0; i < len; i++)
47 if (arr[i] == val)
48 return 1;
49 return 0;
50}
51
52static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
53 uint8_t **data, int *size)
54{
55 static const int extradata_nal_types_hevc[] = {
56 HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
57 };
58 static const int extradata_nal_types_h264[] = {
59 H264_NAL_SPS, H264_NAL_PPS,
60 };
61
62 ExtractExtradataContext *s = ctx->priv_data;
63
64 H2645Packet h2645_pkt = { 0 };
65 int extradata_size = 0;
66 const int *extradata_nal_types;
67 int nb_extradata_nal_types;
d7dcd825 68 int i, has_sps = 0, has_vps = 0, ret = 0;
89b35a13
AK
69
70 if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
71 extradata_nal_types = extradata_nal_types_hevc;
72 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_hevc);
73 } else {
74 extradata_nal_types = extradata_nal_types_h264;
75 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
76 }
77
78 ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
79 ctx, 0, 0, ctx->par_in->codec_id);
80 if (ret < 0)
81 return ret;
82
83 for (i = 0; i < h2645_pkt.nb_nals; i++) {
84 H2645NAL *nal = &h2645_pkt.nals[i];
d7dcd825 85 if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
89b35a13 86 extradata_size += nal->raw_size + 3;
d7dcd825
JA
87 if (ctx->par_in->codec_id == AV_CODEC_ID_HEVC) {
88 if (nal->type == HEVC_NAL_SPS) has_sps = 1;
89 if (nal->type == HEVC_NAL_VPS) has_vps = 1;
90 } else {
91 if (nal->type == H264_NAL_SPS) has_sps = 1;
92 }
93 }
89b35a13
AK
94 }
95
d7dcd825
JA
96 if (extradata_size &&
97 ((ctx->par_in->codec_id == AV_CODEC_ID_HEVC && has_sps && has_vps) ||
98 (ctx->par_in->codec_id == AV_CODEC_ID_H264 && has_sps))) {
89b35a13
AK
99 AVBufferRef *filtered_buf;
100 uint8_t *extradata, *filtered_data;
101
102 if (s->remove) {
103 filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
20142310
JA
104 if (!filtered_buf) {
105 ret = AVERROR(ENOMEM);
89b35a13 106 goto fail;
20142310 107 }
89b35a13
AK
108 filtered_data = filtered_buf->data;
109 }
110
111 extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
112 if (!extradata) {
113 av_buffer_unref(&filtered_buf);
20142310 114 ret = AVERROR(ENOMEM);
89b35a13
AK
115 goto fail;
116 }
117
118 *data = extradata;
119 *size = extradata_size;
120
121 for (i = 0; i < h2645_pkt.nb_nals; i++) {
122 H2645NAL *nal = &h2645_pkt.nals[i];
123 if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
124 nal->type)) {
125 AV_WB24(extradata, 1); // startcode
126 memcpy(extradata + 3, nal->raw_data, nal->raw_size);
127 extradata += 3 + nal->raw_size;
128 } else if (s->remove) {
129 AV_WB24(filtered_data, 1); // startcode
130 memcpy(filtered_data + 3, nal->raw_data, nal->raw_size);
131 filtered_data += 3 + nal->raw_size;
132 }
133 }
134
135 if (s->remove) {
136 av_buffer_unref(&pkt->buf);
137 pkt->buf = filtered_buf;
138 pkt->data = filtered_buf->data;
139 pkt->size = filtered_data - filtered_buf->data;
140 }
141 }
142
143fail:
144 ff_h2645_packet_uninit(&h2645_pkt);
145 return ret;
146}
147
148static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
149 uint8_t **data, int *size)
150{
151 ExtractExtradataContext *s = ctx->priv_data;
152 uint32_t state = UINT32_MAX;
153 int has_extradata = 0, extradata_size = 0;
154 int i;
155
156 for (i = 0; i < pkt->size; i++) {
157 state = (state << 8) | pkt->data[i];
158 if (IS_MARKER(state)) {
159 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
160 has_extradata = 1;
161 } else if (has_extradata) {
162 extradata_size = i - 3;
163 break;
164 }
165 }
166 }
167
168 if (extradata_size) {
169 *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
170 if (!*data)
171 return AVERROR(ENOMEM);
172
173 memcpy(*data, pkt->data, extradata_size);
174 *size = extradata_size;
175
176 if (s->remove) {
177 pkt->data += extradata_size;
178 pkt->size -= extradata_size;
179 }
180 }
181
182 return 0;
183}
184
185static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
186 uint8_t **data, int *size)
187{
188 ExtractExtradataContext *s = ctx->priv_data;
189 int is_mpeg12 = ctx->par_in->codec_id == AV_CODEC_ID_MPEG1VIDEO ||
190 ctx->par_in->codec_id == AV_CODEC_ID_MPEG2VIDEO;
191 uint32_t state = UINT32_MAX;
192 int i;
193
194 for (i = 0; i < pkt->size; i++) {
195 state = (state << 8) | pkt->data[i];
196 if ((is_mpeg12 && state != 0x1B3 && state != 0x1B5 && state < 0x200 && state >= 0x100) ||
197 (!is_mpeg12 && (state == 0x1B3 || state == 0x1B6))) {
198 if (i > 3) {
199 *size = i - 3;
200 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
201 if (!*data)
202 return AVERROR(ENOMEM);
203
204 memcpy(*data, pkt->data, *size);
205
206 if (s->remove) {
207 pkt->data += *size;
208 pkt->size -= *size;
209 }
210 }
211 break;
212 }
213 }
214 return 0;
215}
216
217static const struct {
218 enum AVCodecID id;
219 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
220 uint8_t **data, int *size);
221} extract_tab[] = {
222 { AV_CODEC_ID_CAVS, extract_extradata_mpeg124 },
223 { AV_CODEC_ID_H264, extract_extradata_h2645 },
224 { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
225 { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg124 },
226 { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg124 },
227 { AV_CODEC_ID_MPEG4, extract_extradata_mpeg124 },
228 { AV_CODEC_ID_VC1, extract_extradata_vc1 },
229};
230
231static int extract_extradata_init(AVBSFContext *ctx)
232{
233 ExtractExtradataContext *s = ctx->priv_data;
234 int i;
235
236 for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
237 if (extract_tab[i].id == ctx->par_in->codec_id) {
238 s->extract = extract_tab[i].extract;
239 break;
240 }
241 }
242 if (!s->extract)
243 return AVERROR_BUG;
244
245 return 0;
246}
247
248static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
249{
250 ExtractExtradataContext *s = ctx->priv_data;
251 AVPacket *in;
252 uint8_t *extradata = NULL;
253 int extradata_size;
254 int ret = 0;
255
256 ret = ff_bsf_get_packet(ctx, &in);
257 if (ret < 0)
258 return ret;
259
260 ret = s->extract(ctx, in, &extradata, &extradata_size);
261 if (ret < 0)
262 goto fail;
263
264 if (extradata) {
265 ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
266 extradata, extradata_size);
267 if (ret < 0) {
268 av_freep(&extradata);
269 goto fail;
270 }
271 }
272
273 av_packet_move_ref(out, in);
274
275fail:
276 av_packet_free(&in);
277 return ret;
278}
279
280static const enum AVCodecID codec_ids[] = {
281 AV_CODEC_ID_CAVS,
282 AV_CODEC_ID_H264,
283 AV_CODEC_ID_HEVC,
284 AV_CODEC_ID_MPEG1VIDEO,
285 AV_CODEC_ID_MPEG2VIDEO,
286 AV_CODEC_ID_MPEG4,
287 AV_CODEC_ID_VC1,
288 AV_CODEC_ID_NONE,
289};
290
291#define OFFSET(x) offsetof(ExtractExtradataContext, x)
292static const AVOption options[] = {
293 { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
294 { .i64 = 0 }, 0, 1 },
295 { NULL },
296};
297
298static const AVClass extract_extradata_class = {
299 .class_name = "extract_extradata",
300 .item_name = av_default_item_name,
301 .option = options,
302 .version = LIBAVUTIL_VERSION_INT,
303};
304
305const AVBitStreamFilter ff_extract_extradata_bsf = {
306 .name = "extract_extradata",
307 .codec_ids = codec_ids,
308 .priv_data_size = sizeof(ExtractExtradataContext),
309 .priv_class = &extract_extradata_class,
310 .init = extract_extradata_init,
311 .filter = extract_extradata_filter,
312};