2 * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/log.h"
22 #include "libavutil/opt.h"
24 #include "av1_parse.h"
26 #include "bsf_internal.h"
30 #include "vc1_common.h"
35 REMOVE_FREQ_NONKEYFRAME,
38 #define START_CODE 0x000001
40 typedef struct RemoveExtradataContext {
43 } RemoveExtradataContext;
45 static int av1_split(const uint8_t *buf, int buf_size, void *logctx)
48 const uint8_t *ptr = buf, *end = buf + buf_size;
51 int len = ff_av1_extract_obu(&obu, ptr, buf_size, logctx);
55 if (obu.type == AV1_OBU_FRAME_HEADER ||
56 obu.type == AV1_OBU_FRAME) {
66 static int h264_split(const uint8_t *buf, int buf_size)
68 const uint8_t *ptr = buf, *end = buf + buf_size;
75 ptr = avpriv_find_start_code(ptr, end, &state);
76 if ((state & 0xFFFFFF00) != 0x100)
78 nalu_type = state & 0x1F;
79 if (nalu_type == H264_NAL_SPS) {
81 } else if (nalu_type == H264_NAL_PPS)
83 /* else if (nalu_type == 0x01 ||
84 * nalu_type == 0x02 ||
85 * nalu_type == 0x05) {
88 else if ((nalu_type != H264_NAL_SEI || has_pps) &&
89 nalu_type != H264_NAL_AUD && nalu_type != H264_NAL_SPS_EXT &&
92 while (ptr - 4 > buf && ptr[-5] == 0)
102 // Split after the parameter sets at the beginning of the stream if they exist.
103 static int hevc_split(const uint8_t *buf, int buf_size)
105 const uint8_t *ptr = buf, *end = buf + buf_size;
113 ptr = avpriv_find_start_code(ptr, end, &state);
114 if ((state >> 8) != START_CODE)
116 nut = (state >> 1) & 0x3F;
117 if (nut == HEVC_NAL_VPS)
119 else if (nut == HEVC_NAL_SPS)
121 else if (nut == HEVC_NAL_PPS)
123 else if ((nut != HEVC_NAL_SEI_PREFIX || has_pps) &&
124 nut != HEVC_NAL_AUD) {
125 if (has_vps && has_sps) {
126 while (ptr - 4 > buf && ptr[-5] == 0)
128 return ptr - 4 - buf;
135 static int mpegvideo_split(const uint8_t *buf, int buf_size)
140 for (int i = 0; i < buf_size; i++) {
141 state = (state << 8) | buf[i];
142 if (state == 0x1B3) {
144 } else if (found && state != 0x1B5 && state < 0x200 && state >= 0x100)
150 static int mpeg4video_split(const uint8_t *buf, int buf_size)
152 const uint8_t *ptr = buf, *end = buf + buf_size;
156 ptr = avpriv_find_start_code(ptr, end, &state);
157 if (state == 0x1B3 || state == 0x1B6)
158 return ptr - 4 - buf;
164 static int vc1_split(const uint8_t *buf, int buf_size)
166 const uint8_t *ptr = buf, *end = buf + buf_size;
171 ptr = avpriv_find_start_code(ptr, end, &state);
172 if (state == VC1_CODE_SEQHDR || state == VC1_CODE_ENTRYPOINT) {
174 } else if (charged && IS_MARKER(state))
175 return ptr - 4 - buf;
181 static int remove_extradata(AVBSFContext *ctx, AVPacket *pkt)
183 RemoveExtradataContext *s = ctx->priv_data;
187 ret = ff_bsf_get_packet_ref(ctx, pkt);
191 if (s->freq == REMOVE_FREQ_ALL ||
192 (s->freq == REMOVE_FREQ_NONKEYFRAME && !(pkt->flags & AV_PKT_FLAG_KEY)) ||
193 (s->freq == REMOVE_FREQ_KEYFRAME && pkt->flags & AV_PKT_FLAG_KEY)) {
196 switch (ctx->par_in->codec_id) {
197 case AV_CODEC_ID_AV1:
198 i = av1_split(pkt->data, pkt->size, ctx);
200 case AV_CODEC_ID_AVS2:
201 case AV_CODEC_ID_AVS3:
202 case AV_CODEC_ID_CAVS:
203 case AV_CODEC_ID_MPEG4:
204 i = mpeg4video_split(pkt->data, pkt->size);
206 case AV_CODEC_ID_H264:
207 i = h264_split(pkt->data, pkt->size);
209 case AV_CODEC_ID_HEVC:
210 i = hevc_split(pkt->data, pkt->size);
212 case AV_CODEC_ID_MPEG1VIDEO:
213 case AV_CODEC_ID_MPEG2VIDEO:
214 i = mpegvideo_split(pkt->data, pkt->size);
216 case AV_CODEC_ID_VC1:
217 i = vc1_split(pkt->data, pkt->size);
230 #define OFFSET(x) offsetof(RemoveExtradataContext, x)
231 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
232 static const AVOption options[] = {
233 { "freq", NULL, OFFSET(freq), AV_OPT_TYPE_INT, { .i64 = REMOVE_FREQ_KEYFRAME }, REMOVE_FREQ_KEYFRAME, REMOVE_FREQ_NONKEYFRAME, FLAGS, "freq" },
234 { "k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_NONKEYFRAME }, .flags = FLAGS, .unit = "freq" },
235 { "keyframe", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_KEYFRAME }, .flags = FLAGS, .unit = "freq" },
236 { "e", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .flags = FLAGS, .unit = "freq" },
237 { "all", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = REMOVE_FREQ_ALL }, .flags = FLAGS, .unit = "freq" },
241 static const AVClass remove_extradata_class = {
242 .class_name = "remove_extradata",
243 .item_name = av_default_item_name,
245 .version = LIBAVUTIL_VERSION_INT,
248 const AVBitStreamFilter ff_remove_extradata_bsf = {
249 .name = "remove_extra",
250 .priv_data_size = sizeof(RemoveExtradataContext),
251 .priv_class = &remove_extradata_class,
252 .filter = remove_extradata,