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 *out)
103 FilterUnitsContext *ctx = bsf->priv_data;
104 CodedBitstreamFragment *frag = &ctx->fragment;
109 err = ff_bsf_get_packet(bsf, &in);
113 if (ctx->mode == NOOP) {
114 av_packet_move_ref(out, in);
119 err = ff_cbs_read_packet(ctx->cbc, frag, in);
121 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
125 for (i = 0; i < frag->nb_units; i++) {
126 for (j = 0; j < ctx->nb_types; j++) {
127 if (frag->units[i].type == ctx->type_list[j])
130 if (ctx->mode == REMOVE ? j < ctx->nb_types
131 : j >= ctx->nb_types) {
132 ff_cbs_delete_unit(ctx->cbc, frag, i);
137 if (frag->nb_units > 0)
140 // Don't return packets with nothing in them.
142 ff_cbs_fragment_reset(ctx->cbc, frag);
145 err = ff_cbs_write_packet(ctx->cbc, out, frag);
147 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
151 err = av_packet_copy_props(out, in);
156 ff_cbs_fragment_reset(ctx->cbc, frag);
162 static int filter_units_init(AVBSFContext *bsf)
164 FilterUnitsContext *ctx = bsf->priv_data;
167 if (ctx->pass_types && ctx->remove_types) {
168 av_log(bsf, AV_LOG_ERROR, "Exactly one of pass_types or "
169 "remove_types is required.\n");
170 return AVERROR(EINVAL);
173 if (ctx->pass_types) {
175 err = filter_units_make_type_list(ctx->pass_types,
176 &ctx->type_list, &ctx->nb_types);
178 av_log(bsf, AV_LOG_ERROR, "Failed to parse pass_types.\n");
181 } else if (ctx->remove_types) {
183 err = filter_units_make_type_list(ctx->remove_types,
184 &ctx->type_list, &ctx->nb_types);
186 av_log(bsf, AV_LOG_ERROR, "Failed to parse remove_types.\n");
193 err = ff_cbs_init(&ctx->cbc, bsf->par_in->codec_id, bsf);
197 // Don't actually decompose anything, we only want the unit data.
198 ctx->cbc->decompose_unit_types = ctx->type_list;
199 ctx->cbc->nb_decompose_unit_types = 0;
201 if (bsf->par_in->extradata) {
202 CodedBitstreamFragment *frag = &ctx->fragment;
204 err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
206 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
208 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
210 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
213 ff_cbs_fragment_reset(ctx->cbc, frag);
219 static void filter_units_close(AVBSFContext *bsf)
221 FilterUnitsContext *ctx = bsf->priv_data;
223 av_freep(&ctx->type_list);
225 ff_cbs_fragment_free(ctx->cbc, &ctx->fragment);
226 ff_cbs_close(&ctx->cbc);
229 #define OFFSET(x) offsetof(FilterUnitsContext, x)
230 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
231 static const AVOption filter_units_options[] = {
232 { "pass_types", "List of unit types to pass through the filter.",
233 OFFSET(pass_types), AV_OPT_TYPE_STRING,
234 { .str = NULL }, .flags = FLAGS },
235 { "remove_types", "List of unit types to remove in the filter.",
236 OFFSET(remove_types), AV_OPT_TYPE_STRING,
237 { .str = NULL }, .flags = FLAGS },
242 static const AVClass filter_units_class = {
243 .class_name = "filter_units",
244 .item_name = av_default_item_name,
245 .option = filter_units_options,
246 .version = LIBAVUTIL_VERSION_INT,
249 const AVBitStreamFilter ff_filter_units_bsf = {
250 .name = "filter_units",
251 .priv_data_size = sizeof(FilterUnitsContext),
252 .priv_class = &filter_units_class,
253 .init = &filter_units_init,
254 .close = &filter_units_close,
255 .filter = &filter_units_filter,
256 .codec_ids = ff_cbs_all_codec_ids,