2 * This file is part of Libav.
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.
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.
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
21 #include "libavutil/common.h"
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/log.h"
24 #include "libavutil/opt.h"
28 #include "h2645_parse.h"
31 #include "vc1_common.h"
33 typedef struct ExtractExtradataContext {
36 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
37 uint8_t **data, int *size);
39 /* H264/HEVC specifc fields */
40 H2645Packet h2645_pkt;
44 } ExtractExtradataContext;
46 static int val_in_array(const int *arr, int len, int val)
49 for (i = 0; i < len; i++)
55 static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
56 uint8_t **data, int *size)
58 static const int extradata_nal_types_hevc[] = {
59 HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
61 static const int extradata_nal_types_h264[] = {
62 H264_NAL_SPS, H264_NAL_PPS,
65 ExtractExtradataContext *s = ctx->priv_data;
67 int extradata_size = 0, filtered_size = 0;
68 const int *extradata_nal_types;
69 int nb_extradata_nal_types;
70 int i, has_sps = 0, has_vps = 0, ret = 0;
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);
76 extradata_nal_types = extradata_nal_types_h264;
77 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
80 ret = ff_h2645_packet_split(&s->h2645_pkt, pkt->data, pkt->size,
81 ctx, 0, 0, ctx->par_in->codec_id);
85 for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
86 H2645NAL *nal = &s->h2645_pkt.nals[i];
87 if (val_in_array(extradata_nal_types, nb_extradata_nal_types, nal->type)) {
88 extradata_size += nal->raw_size + 3;
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;
93 if (nal->type == H264_NAL_SPS) has_sps = 1;
95 } else if (s->remove) {
96 filtered_size += nal->raw_size + 3;
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))) {
103 AVBufferRef *filtered_buf;
104 uint8_t *extradata, *filtered_data;
107 filtered_buf = av_buffer_alloc(filtered_size + AV_INPUT_BUFFER_PADDING_SIZE);
109 return AVERROR(ENOMEM);
111 memset(filtered_buf->data + filtered_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
113 filtered_data = filtered_buf->data;
116 extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
118 av_buffer_unref(&filtered_buf);
119 return AVERROR(ENOMEM);
121 memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
124 *size = extradata_size;
126 for (i = 0; i < s->h2645_pkt.nb_nals; i++) {
127 H2645NAL *nal = &s->h2645_pkt.nals[i];
128 if (val_in_array(extradata_nal_types, nb_extradata_nal_types,
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;
141 av_buffer_unref(&pkt->buf);
142 pkt->buf = filtered_buf;
143 pkt->data = filtered_buf->data;
144 pkt->size = filtered_size;
151 static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
152 uint8_t **data, int *size)
154 ExtractExtradataContext *s = ctx->priv_data;
155 uint32_t state = UINT32_MAX;
156 int has_extradata = 0, extradata_size = 0;
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) {
164 } else if (has_extradata) {
165 extradata_size = i - 3;
171 if (extradata_size) {
172 *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
174 return AVERROR(ENOMEM);
176 memcpy(*data, pkt->data, extradata_size);
177 memset(*data + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
178 *size = extradata_size;
181 pkt->data += extradata_size;
182 pkt->size -= extradata_size;
189 static int extract_extradata_mpeg124(AVBSFContext *ctx, AVPacket *pkt,
190 uint8_t **data, int *size)
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;
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))) {
204 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
206 return AVERROR(ENOMEM);
208 memcpy(*data, pkt->data, *size);
209 memset(*data + *size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
222 static const struct {
224 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
225 uint8_t **data, int *size);
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 },
236 static int extract_extradata_init(AVBSFContext *ctx)
238 ExtractExtradataContext *s = ctx->priv_data;
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;
253 static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *pkt)
255 ExtractExtradataContext *s = ctx->priv_data;
256 uint8_t *extradata = NULL;
260 ret = ff_bsf_get_packet_ref(ctx, pkt);
264 ret = s->extract(ctx, pkt, &extradata, &extradata_size);
269 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
270 extradata, extradata_size);
272 av_freep(&extradata);
280 av_packet_unref(pkt);
284 static void extract_extradata_close(AVBSFContext *ctx)
286 ExtractExtradataContext *s = ctx->priv_data;
287 ff_h2645_packet_uninit(&s->h2645_pkt);
290 static const enum AVCodecID codec_ids[] = {
294 AV_CODEC_ID_MPEG1VIDEO,
295 AV_CODEC_ID_MPEG2VIDEO,
301 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
302 static const AVOption options[] = {
303 { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
304 { .i64 = 0 }, 0, 1 },
308 static const AVClass extract_extradata_class = {
309 .class_name = "extract_extradata",
310 .item_name = av_default_item_name,
312 .version = LIBAVUTIL_VERSION_INT,
315 const 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,
322 .close = extract_extradata_close,