1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00
FFmpeg/libavfilter/vf_pp.c
Andreas Rheinhardt b4f5201967 avfilter: Replace query_formats callback with union of list and callback
If one looks at the many query_formats callbacks in existence,
one will immediately recognize that there is one type of default
callback for video and a slightly different default callback for
audio: It is "return ff_set_common_formats_from_list(ctx, pix_fmts);"
for video with a filter-specific pix_fmts list. For audio, it is
the same with a filter-specific sample_fmts list together with
ff_set_common_all_samplerates() and ff_set_common_all_channel_counts().

This commit allows to remove the boilerplate query_formats callbacks
by replacing said callback with a union consisting the old callback
and pointers for pixel and sample format arrays. For the not uncommon
case in which these lists only contain a single entry (besides the
sentinel) enum AVPixelFormat and enum AVSampleFormat fields are also
added to the union to store them directly in the AVFilter,
thereby avoiding a relocation.

The state of said union will be contained in a new, dedicated AVFilter
field (the nb_inputs and nb_outputs fields have been shrunk to uint8_t
in order to create a hole for this new field; this is no problem, as
the maximum of all the nb_inputs is four; for nb_outputs it is only
two).

The state's default value coincides with the earlier default of
query_formats being unset, namely that the filter accepts all formats
(and also sample rates and channel counts/layouts for audio)
provided that these properties agree coincide for all inputs and
outputs.

By using different union members for audio and video filters
the type-unsafety of using the same functions for audio and video
lists will furthermore be more confined to formats.c than before.

When the new fields are used, they will also avoid allocations:
Currently something nearly equivalent to ff_default_query_formats()
is called after every successful call to a query_formats callback;
yet in the common case that the newly allocated AVFilterFormats
are not used at all (namely if there are no free links) these newly
allocated AVFilterFormats are freed again without ever being used.
Filters no longer using the callback will not exhibit this any more.

Reviewed-by: Paul B Mahol <onemda@gmail.com>
Reviewed-by: Nicolas George <george@nsup.org>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-10-05 17:48:25 +02:00

202 lines
5.7 KiB
C

/*
* Copyright (c) 2002 A'rpi
* Copyright (C) 2012 Clément Bœsch
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU 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.
*/
/**
* @file
* libpostproc filter, ported from MPlayer.
*/
#include "libavutil/avassert.h"
#include "libavutil/opt.h"
#include "internal.h"
#include "qp_table.h"
#include "libpostproc/postprocess.h"
typedef struct PPFilterContext {
const AVClass *class;
char *subfilters;
int mode_id;
pp_mode *modes[PP_QUALITY_MAX + 1];
void *pp_ctx;
} PPFilterContext;
#define OFFSET(x) offsetof(PPFilterContext, x)
#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
static const AVOption pp_options[] = {
{ "subfilters", "set postprocess subfilters", OFFSET(subfilters), AV_OPT_TYPE_STRING, {.str="de"}, .flags = FLAGS },
{ NULL }
};
AVFILTER_DEFINE_CLASS(pp);
static av_cold int pp_init(AVFilterContext *ctx)
{
int i;
PPFilterContext *pp = ctx->priv;
for (i = 0; i <= PP_QUALITY_MAX; i++) {
pp->modes[i] = pp_get_mode_by_name_and_quality(pp->subfilters, i);
if (!pp->modes[i])
return AVERROR_EXTERNAL;
}
pp->mode_id = PP_QUALITY_MAX;
return 0;
}
static int pp_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
char *res, int res_len, int flags)
{
PPFilterContext *pp = ctx->priv;
if (!strcmp(cmd, "quality")) {
pp->mode_id = av_clip(strtol(args, NULL, 10), 0, PP_QUALITY_MAX);
return 0;
}
return AVERROR(ENOSYS);
}
static int pp_query_formats(AVFilterContext *ctx)
{
static const enum AVPixelFormat pix_fmts[] = {
AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVJ422P,
AV_PIX_FMT_YUV411P,
AV_PIX_FMT_GBRP,
AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ440P,
AV_PIX_FMT_GRAY8,
AV_PIX_FMT_NONE
};
return ff_set_common_formats_from_list(ctx, pix_fmts);
}
static int pp_config_props(AVFilterLink *inlink)
{
int flags = PP_CPU_CAPS_AUTO;
PPFilterContext *pp = inlink->dst->priv;
switch (inlink->format) {
case AV_PIX_FMT_GRAY8:
case AV_PIX_FMT_YUVJ420P:
case AV_PIX_FMT_YUV420P: flags |= PP_FORMAT_420; break;
case AV_PIX_FMT_YUVJ422P:
case AV_PIX_FMT_YUV422P: flags |= PP_FORMAT_422; break;
case AV_PIX_FMT_YUV411P: flags |= PP_FORMAT_411; break;
case AV_PIX_FMT_GBRP:
case AV_PIX_FMT_YUVJ444P:
case AV_PIX_FMT_YUV444P: flags |= PP_FORMAT_444; break;
case AV_PIX_FMT_YUVJ440P:
case AV_PIX_FMT_YUV440P: flags |= PP_FORMAT_440; break;
default: av_assert0(0);
}
pp->pp_ctx = pp_get_context(inlink->w, inlink->h, flags);
if (!pp->pp_ctx)
return AVERROR(ENOMEM);
return 0;
}
static int pp_filter_frame(AVFilterLink *inlink, AVFrame *inbuf)
{
AVFilterContext *ctx = inlink->dst;
PPFilterContext *pp = ctx->priv;
AVFilterLink *outlink = ctx->outputs[0];
const int aligned_w = FFALIGN(outlink->w, 8);
const int aligned_h = FFALIGN(outlink->h, 8);
AVFrame *outbuf;
int qstride = 0;
int8_t *qp_table = NULL;
int ret;
outbuf = ff_get_video_buffer(outlink, aligned_w, aligned_h);
if (!outbuf) {
av_frame_free(&inbuf);
return AVERROR(ENOMEM);
}
av_frame_copy_props(outbuf, inbuf);
outbuf->width = inbuf->width;
outbuf->height = inbuf->height;
ret = ff_qp_table_extract(inbuf, &qp_table, &qstride, NULL, NULL);
if (ret < 0) {
av_frame_free(&inbuf);
av_frame_free(&outbuf);
return ret;
}
pp_postprocess((const uint8_t **)inbuf->data, inbuf->linesize,
outbuf->data, outbuf->linesize,
aligned_w, outlink->h,
qp_table,
qstride,
pp->modes[pp->mode_id],
pp->pp_ctx,
outbuf->pict_type | (qp_table ? PP_PICT_TYPE_QP2 : 0));
av_frame_free(&inbuf);
av_freep(&qp_table);
return ff_filter_frame(outlink, outbuf);
}
static av_cold void pp_uninit(AVFilterContext *ctx)
{
int i;
PPFilterContext *pp = ctx->priv;
for (i = 0; i <= PP_QUALITY_MAX; i++)
pp_free_mode(pp->modes[i]);
if (pp->pp_ctx)
pp_free_context(pp->pp_ctx);
}
static const AVFilterPad pp_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.config_props = pp_config_props,
.filter_frame = pp_filter_frame,
},
};
static const AVFilterPad pp_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
},
};
const AVFilter ff_vf_pp = {
.name = "pp",
.description = NULL_IF_CONFIG_SMALL("Filter video using libpostproc."),
.priv_size = sizeof(PPFilterContext),
.init = pp_init,
.uninit = pp_uninit,
FILTER_INPUTS(pp_inputs),
FILTER_OUTPUTS(pp_outputs),
FILTER_QUERY_FUNC(pp_query_formats),
.process_command = pp_process_command,
.priv_class = &pp_class,
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
};