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/opt.h"
28 typedef struct FilterUnitsContext {
31 CodedBitstreamContext *cbc;
32 CodedBitstreamFragment fragment;
34 const char *pass_types;
35 const char *remove_types;
42 CodedBitstreamUnitType *type_list;
47 static int filter_units_make_type_list(const char *list_string,
48 CodedBitstreamUnitType **type_list,
51 CodedBitstreamUnitType *list = NULL;
54 for (pass = 1; pass <= 2; pass++) {
55 long value, range_start, range_end;
60 for (str = list_string; *str;) {
61 value = strtol(str, &value_end, 0);
64 str = (const char *)value_end;
68 range_end = strtol(str, &value_end, 0);
72 for (value = range_start; value < range_end; value++) {
86 list = av_malloc_array(count, sizeof(*list));
88 return AVERROR(ENOMEM);
98 return AVERROR(EINVAL);
101 static int filter_units_filter(AVBSFContext *bsf, AVPacket *pkt)
103 FilterUnitsContext *ctx = bsf->priv_data;
104 CodedBitstreamFragment *frag = &ctx->fragment;
107 err = ff_bsf_get_packet_ref(bsf, pkt);
111 if (ctx->mode == NOOP)
114 err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
116 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
120 for (i = frag->nb_units - 1; i >= 0; i--) {
121 for (j = 0; j < ctx->nb_types; j++) {
122 if (frag->units[i].type == ctx->type_list[j])
125 if (ctx->mode == REMOVE ? j < ctx->nb_types
126 : j >= ctx->nb_types)
127 ff_cbs_delete_unit(ctx->cbc, frag, i);
130 if (frag->nb_units == 0) {
131 // Don't return packets with nothing in them.
132 err = AVERROR(EAGAIN);
136 err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
138 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
144 av_packet_unref(pkt);
145 ff_cbs_fragment_reset(ctx->cbc, frag);
150 static int filter_units_init(AVBSFContext *bsf)
152 FilterUnitsContext *ctx = bsf->priv_data;
155 if (ctx->pass_types && ctx->remove_types) {
156 av_log(bsf, AV_LOG_ERROR, "Exactly one of pass_types or "
157 "remove_types is required.\n");
158 return AVERROR(EINVAL);
161 if (ctx->pass_types) {
163 err = filter_units_make_type_list(ctx->pass_types,
164 &ctx->type_list, &ctx->nb_types);
166 av_log(bsf, AV_LOG_ERROR, "Failed to parse pass_types.\n");
169 } else if (ctx->remove_types) {
171 err = filter_units_make_type_list(ctx->remove_types,
172 &ctx->type_list, &ctx->nb_types);
174 av_log(bsf, AV_LOG_ERROR, "Failed to parse remove_types.\n");
181 err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
185 // Don't actually decompose anything, we only want the unit data.
186 ctx->cbc->decompose_unit_types = ctx->type_list;
187 ctx->cbc->nb_decompose_unit_types = 0;
189 if (bsf->par_in->extradata) {
190 CodedBitstreamFragment *frag = &ctx->fragment;
192 err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
194 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
196 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
198 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
201 ff_cbs_fragment_reset(ctx->cbc, frag);
207 static void filter_units_close(AVBSFContext *bsf)
209 FilterUnitsContext *ctx = bsf->priv_data;
211 av_freep(&ctx->type_list);
213 ff_cbs_fragment_free(ctx->cbc, &ctx->fragment);
214 ff_cbs_close(&ctx->cbc);
217 #define OFFSET(x) offsetof(FilterUnitsContext, x)
218 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
219 static const AVOption filter_units_options[] = {
220 { "pass_types", "List of unit types to pass through the filter.",
221 OFFSET(pass_types), AV_OPT_TYPE_STRING,
222 { .str = NULL }, .flags = FLAGS },
223 { "remove_types", "List of unit types to remove in the filter.",
224 OFFSET(remove_types), AV_OPT_TYPE_STRING,
225 { .str = NULL }, .flags = FLAGS },
230 static const AVClass filter_units_class = {
231 .class_name = "filter_units",
232 .item_name = av_default_item_name,
233 .option = filter_units_options,
234 .version = LIBAVUTIL_VERSION_INT,
237 const AVBitStreamFilter ff_filter_units_bsf = {
238 .name = "filter_units",
239 .priv_data_size = sizeof(FilterUnitsContext),
240 .priv_class = &filter_units_class,
241 .init = &filter_units_init,
242 .close = &filter_units_close,
243 .filter = &filter_units_filter,
244 .codec_ids = ff_cbs_all_codec_ids,