2009-10-27 22:40:43 +01:00
|
|
|
/*
|
2010-11-28 11:22:58 +01:00
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
2009-10-27 22:40:43 +01:00
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 16:45:34 +02:00
|
|
|
* @file
|
2009-11-03 22:59:41 +01:00
|
|
|
* format and noformat video filters
|
2009-10-27 22:40:43 +01:00
|
|
|
*/
|
|
|
|
|
2022-02-23 13:56:49 +01:00
|
|
|
#include "config_components.h"
|
|
|
|
|
2012-08-06 15:49:32 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libavutil/internal.h"
|
|
|
|
#include "libavutil/mem.h"
|
2010-01-30 20:10:26 +01:00
|
|
|
#include "libavutil/pixdesc.h"
|
2013-02-25 21:21:29 +01:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
2009-10-27 22:40:43 +01:00
|
|
|
#include "avfilter.h"
|
2012-05-30 10:12:55 +02:00
|
|
|
#include "formats.h"
|
2012-06-12 20:12:42 +02:00
|
|
|
#include "internal.h"
|
2012-05-19 10:37:56 +02:00
|
|
|
#include "video.h"
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2014-04-11 11:54:15 +02:00
|
|
|
typedef struct FormatContext {
|
2013-02-25 21:21:29 +01:00
|
|
|
const AVClass *class;
|
|
|
|
char *pix_fmts;
|
2023-10-25 13:42:24 +02:00
|
|
|
char *csps;
|
|
|
|
char *ranges;
|
2014-05-24 11:15:15 +02:00
|
|
|
|
2023-10-21 16:51:51 +02:00
|
|
|
AVFilterFormats *formats; ///< parsed from `pix_fmts`
|
2023-10-25 13:42:24 +02:00
|
|
|
AVFilterFormats *color_spaces; ///< parsed from `csps`
|
|
|
|
AVFilterFormats *color_ranges; ///< parsed from `ranges`
|
2009-10-27 22:40:43 +01:00
|
|
|
} FormatContext;
|
|
|
|
|
2014-05-24 11:15:15 +02:00
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
FormatContext *s = ctx->priv;
|
2023-10-21 16:51:51 +02:00
|
|
|
ff_formats_unref(&s->formats);
|
2023-10-25 13:42:24 +02:00
|
|
|
ff_formats_unref(&s->color_spaces);
|
|
|
|
ff_formats_unref(&s->color_ranges);
|
2023-10-21 16:51:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int invert_formats(AVFilterFormats **fmts,
|
|
|
|
AVFilterFormats *allfmts)
|
|
|
|
{
|
|
|
|
if (!allfmts)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
if (!*fmts) {
|
|
|
|
/* empty fmt list means no restriction, regardless of filter type */
|
|
|
|
ff_formats_unref(&allfmts);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < allfmts->nb_formats; i++) {
|
|
|
|
for (int j = 0; j < (*fmts)->nb_formats; j++) {
|
|
|
|
if (allfmts->formats[i] == (*fmts)->formats[j]) {
|
|
|
|
/* format is forbidden, remove it from allfmts list */
|
|
|
|
memmove(&allfmts->formats[i], &allfmts->formats[i+1],
|
|
|
|
(allfmts->nb_formats - (i+1)) * sizeof(*allfmts->formats));
|
|
|
|
allfmts->nb_formats--;
|
|
|
|
i--; /* repeat loop with same idx */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_formats_unref(fmts);
|
|
|
|
*fmts = allfmts;
|
|
|
|
return 0;
|
2014-05-24 11:15:15 +02:00
|
|
|
}
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2013-03-13 08:26:39 +01:00
|
|
|
static av_cold int init(AVFilterContext *ctx)
|
2009-10-27 22:40:43 +01:00
|
|
|
{
|
2013-03-18 20:44:36 +01:00
|
|
|
FormatContext *s = ctx->priv;
|
2023-10-21 16:51:51 +02:00
|
|
|
enum AVPixelFormat pix_fmt;
|
2014-05-27 02:46:56 +02:00
|
|
|
int ret;
|
2014-05-24 11:15:15 +02:00
|
|
|
|
2023-10-21 16:51:51 +02:00
|
|
|
for (char *sep, *cur = s->pix_fmts; cur; cur = sep) {
|
2014-05-24 11:15:15 +02:00
|
|
|
sep = strchr(cur, '|');
|
2023-10-21 16:51:51 +02:00
|
|
|
if (sep && *sep)
|
2014-05-24 11:15:15 +02:00
|
|
|
*sep++ = 0;
|
2023-10-21 16:51:51 +02:00
|
|
|
if ((ret = ff_parse_pixel_format(&pix_fmt, cur, ctx)) < 0 ||
|
|
|
|
(ret = ff_add_format(&s->formats, pix_fmt)) < 0)
|
2011-08-22 14:56:23 +02:00
|
|
|
return ret;
|
2014-05-24 11:15:15 +02:00
|
|
|
}
|
|
|
|
|
2023-10-25 13:42:24 +02:00
|
|
|
for (char *sep, *cur = s->csps; cur; cur = sep) {
|
|
|
|
sep = strchr(cur, '|');
|
|
|
|
if (sep && *sep)
|
|
|
|
*sep++ = 0;
|
|
|
|
if ((ret = av_color_space_from_name(cur)) < 0 ||
|
|
|
|
(ret = ff_add_format(&s->color_spaces, ret)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (char *sep, *cur = s->ranges; cur; cur = sep) {
|
|
|
|
sep = strchr(cur, '|');
|
|
|
|
if (sep && *sep)
|
|
|
|
*sep++ = 0;
|
|
|
|
if ((ret = av_color_range_from_name(cur)) < 0 ||
|
|
|
|
(ret = ff_add_format(&s->color_ranges, ret)) < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-05-24 11:15:15 +02:00
|
|
|
if (!strcmp(ctx->filter->name, "noformat")) {
|
2023-10-25 13:42:24 +02:00
|
|
|
if ((ret = invert_formats(&s->formats, ff_all_formats(AVMEDIA_TYPE_VIDEO))) < 0 ||
|
|
|
|
(ret = invert_formats(&s->color_spaces, ff_all_color_spaces())) < 0 ||
|
|
|
|
(ret = invert_formats(&s->color_ranges, ff_all_color_ranges())) < 0)
|
2023-10-21 16:51:51 +02:00
|
|
|
return ret;
|
2009-10-27 22:40:43 +01:00
|
|
|
}
|
|
|
|
|
2023-10-21 16:51:51 +02:00
|
|
|
/* hold on to a ref for the lifetime of the filter */
|
2023-10-25 13:43:28 +02:00
|
|
|
if (s->formats && (ret = ff_formats_ref(s->formats, &s->formats)) < 0 ||
|
2023-10-25 13:42:24 +02:00
|
|
|
s->color_spaces && (ret = ff_formats_ref(s->color_spaces, &s->color_spaces)) < 0 ||
|
|
|
|
s->color_ranges && (ret = ff_formats_ref(s->color_ranges, &s->color_ranges)) < 0)
|
2023-10-21 16:51:51 +02:00
|
|
|
return ret;
|
|
|
|
|
2009-10-27 22:40:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-24 11:15:15 +02:00
|
|
|
static int query_formats(AVFilterContext *ctx)
|
2009-10-27 22:40:43 +01:00
|
|
|
{
|
2014-05-24 11:15:15 +02:00
|
|
|
FormatContext *s = ctx->priv;
|
2023-10-25 13:42:24 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-10-25 13:43:28 +02:00
|
|
|
if (s->formats && (ret = ff_set_common_formats(ctx, s->formats)) < 0 ||
|
2023-10-25 13:42:24 +02:00
|
|
|
s->color_spaces && (ret = ff_set_common_color_spaces(ctx, s->color_spaces)) < 0 ||
|
|
|
|
s->color_ranges && (ret = ff_set_common_color_ranges(ctx, s->color_ranges)) < 0)
|
|
|
|
return ret;
|
2014-05-24 11:15:15 +02:00
|
|
|
|
2023-10-25 13:42:24 +02:00
|
|
|
return 0;
|
2009-10-27 22:40:43 +01:00
|
|
|
}
|
|
|
|
|
2014-05-24 11:15:15 +02:00
|
|
|
|
2013-02-25 21:21:29 +01:00
|
|
|
#define OFFSET(x) offsetof(FormatContext, x)
|
|
|
|
static const AVOption options[] = {
|
2016-04-21 16:49:00 +02:00
|
|
|
{ "pix_fmts", "A '|'-separated list of pixel formats", OFFSET(pix_fmts), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
|
2023-10-25 13:42:24 +02:00
|
|
|
{ "color_spaces", "A '|'-separated list of color spaces", OFFSET(csps), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
|
|
|
|
{ "color_ranges", "A '|'-separated list of color ranges", OFFSET(ranges), AV_OPT_TYPE_STRING, .flags = AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM },
|
2013-09-07 14:13:50 +02:00
|
|
|
{ NULL }
|
2013-02-25 21:21:29 +01:00
|
|
|
};
|
|
|
|
|
2021-09-10 22:40:20 +02:00
|
|
|
AVFILTER_DEFINE_CLASS_EXT(format, "(no)format", options);
|
2010-03-17 05:37:36 +01:00
|
|
|
|
2023-11-01 09:10:09 +01:00
|
|
|
static const AVFilterPad inputs[] = {
|
2012-07-24 15:14:01 +02:00
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2021-08-17 05:48:27 +02:00
|
|
|
.get_buffer.video = ff_null_get_video_buffer,
|
2012-07-24 15:14:01 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-11-01 09:10:09 +01:00
|
|
|
#if CONFIG_FORMAT_FILTER
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_format = {
|
2013-09-07 14:13:50 +02:00
|
|
|
.name = "format",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Convert the input video to one of the specified pixel formats."),
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2013-09-07 14:13:50 +02:00
|
|
|
.init = init,
|
2014-05-27 02:46:56 +02:00
|
|
|
.uninit = uninit,
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2013-09-07 14:13:50 +02:00
|
|
|
.priv_size = sizeof(FormatContext),
|
|
|
|
.priv_class = &format_class,
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2021-11-22 14:39:11 +01:00
|
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY,
|
|
|
|
|
2023-11-01 09:10:09 +01:00
|
|
|
FILTER_INPUTS(inputs),
|
2023-08-03 14:37:51 +02:00
|
|
|
FILTER_OUTPUTS(ff_video_default_filterpad),
|
2021-09-27 12:07:35 +02:00
|
|
|
|
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2009-10-27 22:40:43 +01:00
|
|
|
};
|
2010-03-17 05:37:40 +01:00
|
|
|
#endif /* CONFIG_FORMAT_FILTER */
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2010-03-17 05:37:40 +01:00
|
|
|
#if CONFIG_NOFORMAT_FILTER
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_noformat = {
|
2013-09-07 14:13:50 +02:00
|
|
|
.name = "noformat",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Force libavfilter not to use any of the specified pixel formats for the input to the next filter."),
|
2021-09-10 22:40:20 +02:00
|
|
|
.priv_class = &format_class,
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2013-09-07 14:13:50 +02:00
|
|
|
.init = init,
|
2014-05-27 02:46:56 +02:00
|
|
|
.uninit = uninit,
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2013-09-07 14:13:50 +02:00
|
|
|
.priv_size = sizeof(FormatContext),
|
2009-10-27 22:40:43 +01:00
|
|
|
|
2021-11-22 14:39:11 +01:00
|
|
|
.flags = AVFILTER_FLAG_METADATA_ONLY,
|
|
|
|
|
2023-11-01 09:10:09 +01:00
|
|
|
FILTER_INPUTS(inputs),
|
2023-08-03 14:37:51 +02:00
|
|
|
FILTER_OUTPUTS(ff_video_default_filterpad),
|
2021-09-27 12:07:35 +02:00
|
|
|
|
|
|
|
FILTER_QUERY_FUNC(query_formats),
|
2009-10-27 22:40:43 +01:00
|
|
|
};
|
2010-03-17 05:37:40 +01:00
|
|
|
#endif /* CONFIG_NOFORMAT_FILTER */
|