2 * This file is part of FFmpeg.
4 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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);
41 } ExtractExtradataContext;
43 static int val_in_array(const int *arr, int len, int val)
46 for (i = 0; i < len; i++)
52 static int extract_extradata_h2645(AVBSFContext *ctx, AVPacket *pkt,
53 uint8_t **data, int *size)
55 static const int extradata_nal_types_hevc[] = {
56 HEVC_NAL_VPS, HEVC_NAL_SPS, HEVC_NAL_PPS,
58 static const int extradata_nal_types_h264[] = {
59 H264_NAL_SPS, H264_NAL_PPS,
62 ExtractExtradataContext *s = ctx->priv_data;
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, has_sps = 0, has_vps = 0, ret = 0;
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);
74 extradata_nal_types = extradata_nal_types_h264;
75 nb_extradata_nal_types = FF_ARRAY_ELEMS(extradata_nal_types_h264);
78 ret = ff_h2645_packet_split(&h2645_pkt, pkt->data, pkt->size,
79 ctx, 0, 0, ctx->par_in->codec_id, 1);
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 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;
91 if (nal->type == H264_NAL_SPS) has_sps = 1;
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))) {
99 AVBufferRef *filtered_buf;
100 uint8_t *extradata, *filtered_data;
103 filtered_buf = av_buffer_alloc(pkt->size + AV_INPUT_BUFFER_PADDING_SIZE);
105 ret = AVERROR(ENOMEM);
108 filtered_data = filtered_buf->data;
111 extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
113 av_buffer_unref(&filtered_buf);
114 ret = AVERROR(ENOMEM);
119 *size = extradata_size;
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,
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;
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;
144 ff_h2645_packet_uninit(&h2645_pkt);
148 static int extract_extradata_vc1(AVBSFContext *ctx, AVPacket *pkt,
149 uint8_t **data, int *size)
151 ExtractExtradataContext *s = ctx->priv_data;
152 const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
153 uint32_t state = UINT32_MAX;
154 int has_extradata = 0, extradata_size = 0;
157 ptr = avpriv_find_start_code(ptr, end, &state);
158 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
160 } else if (has_extradata && IS_MARKER(state)) {
161 extradata_size = ptr - 4 - pkt->data;
166 if (extradata_size) {
167 *data = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
169 return AVERROR(ENOMEM);
171 memcpy(*data, pkt->data, extradata_size);
172 *size = extradata_size;
175 pkt->data += extradata_size;
176 pkt->size -= extradata_size;
183 static int extract_extradata_mpeg12(AVBSFContext *ctx, AVPacket *pkt,
184 uint8_t **data, int *size)
186 ExtractExtradataContext *s = ctx->priv_data;
187 uint32_t state = UINT32_MAX;
190 for (i = 0; i < pkt->size; i++) {
191 state = (state << 8) | pkt->data[i];
194 else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100) {
197 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
199 return AVERROR(ENOMEM);
201 memcpy(*data, pkt->data, *size);
214 static int extract_extradata_mpeg4(AVBSFContext *ctx, AVPacket *pkt,
215 uint8_t **data, int *size)
217 ExtractExtradataContext *s = ctx->priv_data;
218 const uint8_t *ptr = pkt->data, *end = pkt->data + pkt->size;
219 uint32_t state = UINT32_MAX;
222 ptr = avpriv_find_start_code(ptr, end, &state);
223 if (state == 0x1B3 || state == 0x1B6) {
224 if (ptr - pkt->data > 4) {
225 *size = ptr - 4 - pkt->data;
226 *data = av_malloc(*size + AV_INPUT_BUFFER_PADDING_SIZE);
228 return AVERROR(ENOMEM);
230 memcpy(*data, pkt->data, *size);
243 static const struct {
245 int (*extract)(AVBSFContext *ctx, AVPacket *pkt,
246 uint8_t **data, int *size);
248 { AV_CODEC_ID_CAVS, extract_extradata_mpeg4 },
249 { AV_CODEC_ID_H264, extract_extradata_h2645 },
250 { AV_CODEC_ID_HEVC, extract_extradata_h2645 },
251 { AV_CODEC_ID_MPEG1VIDEO, extract_extradata_mpeg12 },
252 { AV_CODEC_ID_MPEG2VIDEO, extract_extradata_mpeg12 },
253 { AV_CODEC_ID_MPEG4, extract_extradata_mpeg4 },
254 { AV_CODEC_ID_VC1, extract_extradata_vc1 },
257 static int extract_extradata_init(AVBSFContext *ctx)
259 ExtractExtradataContext *s = ctx->priv_data;
262 for (i = 0; i < FF_ARRAY_ELEMS(extract_tab); i++) {
263 if (extract_tab[i].id == ctx->par_in->codec_id) {
264 s->extract = extract_tab[i].extract;
274 static int extract_extradata_filter(AVBSFContext *ctx, AVPacket *out)
276 ExtractExtradataContext *s = ctx->priv_data;
278 uint8_t *extradata = NULL;
282 ret = ff_bsf_get_packet(ctx, &in);
286 ret = s->extract(ctx, in, &extradata, &extradata_size);
291 ret = av_packet_add_side_data(in, AV_PKT_DATA_NEW_EXTRADATA,
292 extradata, extradata_size);
294 av_freep(&extradata);
299 av_packet_move_ref(out, in);
306 static const enum AVCodecID codec_ids[] = {
310 AV_CODEC_ID_MPEG1VIDEO,
311 AV_CODEC_ID_MPEG2VIDEO,
317 #define OFFSET(x) offsetof(ExtractExtradataContext, x)
318 static const AVOption options[] = {
319 { "remove", "remove the extradata from the bitstream", OFFSET(remove), AV_OPT_TYPE_INT,
320 { .i64 = 0 }, 0, 1 },
324 static const AVClass extract_extradata_class = {
325 .class_name = "extract_extradata",
326 .item_name = av_default_item_name,
328 .version = LIBAVUTIL_VERSION_INT,
331 const AVBitStreamFilter ff_extract_extradata_bsf = {
332 .name = "extract_extradata",
333 .codec_ids = codec_ids,
334 .priv_data_size = sizeof(ExtractExtradataContext),
335 .priv_class = &extract_extradata_class,
336 .init = extract_extradata_init,
337 .filter = extract_extradata_filter,