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