2009-10-23 01:00:33 +03:00
|
|
|
/*
|
2010-11-28 12:22:58 +02:00
|
|
|
* Copyright (c) 2007 Bobby Bingham
|
2009-10-23 01:00:33 +03: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 17:45:34 +03:00
|
|
|
* @file
|
2009-10-23 01:00:33 +03:00
|
|
|
* video vertical flip filter
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/internal.h"
|
2016-08-28 11:00:06 +02:00
|
|
|
#include "libavutil/opt.h"
|
2010-01-31 18:33:29 +02:00
|
|
|
#include "libavutil/pixdesc.h"
|
2009-10-23 01:00:33 +03:00
|
|
|
#include "avfilter.h"
|
2012-06-12 21:12:42 +03:00
|
|
|
#include "internal.h"
|
2012-05-19 11:37:56 +03:00
|
|
|
#include "video.h"
|
2009-10-23 01:00:33 +03:00
|
|
|
|
2014-04-11 12:54:15 +03:00
|
|
|
typedef struct FlipContext {
|
2016-08-28 11:00:06 +02:00
|
|
|
const AVClass *class;
|
2009-10-23 01:00:33 +03:00
|
|
|
int vsub; ///< vertical chroma subsampling
|
2020-09-05 21:14:01 +02:00
|
|
|
int bayer;
|
2009-10-23 01:00:33 +03:00
|
|
|
} FlipContext;
|
|
|
|
|
2016-08-28 11:00:06 +02:00
|
|
|
static const AVOption vflip_options[] = {
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
AVFILTER_DEFINE_CLASS(vflip);
|
|
|
|
|
2009-10-23 01:00:33 +03:00
|
|
|
static int config_input(AVFilterLink *link)
|
|
|
|
{
|
|
|
|
FlipContext *flip = link->dst->priv;
|
2012-10-06 14:29:37 +03:00
|
|
|
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
|
2009-10-23 01:00:33 +03:00
|
|
|
|
2012-10-06 14:29:37 +03:00
|
|
|
flip->vsub = desc->log2_chroma_h;
|
2020-09-05 21:14:01 +02:00
|
|
|
flip->bayer = !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
|
2009-10-23 01:00:33 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static AVFrame *get_video_buffer(AVFilterLink *link, int w, int h)
|
2009-10-23 01:00:33 +03:00
|
|
|
{
|
|
|
|
FlipContext *flip = link->dst->priv;
|
2012-11-28 10:41:07 +03:00
|
|
|
AVFrame *frame;
|
2009-10-23 01:00:33 +03:00
|
|
|
int i;
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
|
|
|
|
if (!frame)
|
2012-07-15 12:16:53 +03:00
|
|
|
return NULL;
|
|
|
|
|
2009-12-08 19:08:49 +02:00
|
|
|
for (i = 0; i < 4; i ++) {
|
|
|
|
int vsub = i == 1 || i == 2 ? flip->vsub : 0;
|
2016-01-27 17:19:38 +02:00
|
|
|
int height = AV_CEIL_RSHIFT(h, vsub);
|
2009-12-08 19:08:49 +02:00
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
if (frame->data[i]) {
|
2013-05-11 02:06:53 +03:00
|
|
|
frame->data[i] += (height - 1) * frame->linesize[i];
|
2012-11-28 10:41:07 +03:00
|
|
|
frame->linesize[i] = -frame->linesize[i];
|
2009-10-23 01:00:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
return frame;
|
2009-10-23 01:00:33 +03:00
|
|
|
}
|
|
|
|
|
2020-09-05 21:14:01 +02:00
|
|
|
static int flip_bayer(AVFilterLink *link, AVFrame *in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = link->dst;
|
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
|
|
|
AVFrame *out;
|
|
|
|
uint8_t *inrow = in->data[0], *outrow;
|
|
|
|
int i, width = outlink->w << (av_pix_fmt_desc_get(link->format)->comp[0].step > 1);
|
|
|
|
if (outlink->h & 1) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Bayer vertical flip needs even height\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
|
|
|
if (!out) {
|
|
|
|
av_frame_free(&in);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
av_frame_copy_props(out, in);
|
|
|
|
outrow = out->data[0] + out->linesize[0] * (outlink->h - 2);
|
|
|
|
for (i = 0; i < outlink->h >> 1; i++) {
|
|
|
|
memcpy(outrow, inrow, width);
|
|
|
|
memcpy(outrow + out->linesize[0], inrow + in->linesize[0], width);
|
|
|
|
inrow += 2 * in->linesize[0];
|
|
|
|
outrow -= 2 * out->linesize[0];
|
|
|
|
}
|
|
|
|
av_frame_free(&in);
|
|
|
|
return ff_filter_frame(outlink, out);
|
|
|
|
}
|
|
|
|
|
2012-11-28 10:41:07 +03:00
|
|
|
static int filter_frame(AVFilterLink *link, AVFrame *frame)
|
2009-10-23 01:00:33 +03:00
|
|
|
{
|
|
|
|
FlipContext *flip = link->dst->priv;
|
|
|
|
int i;
|
|
|
|
|
2020-09-05 21:14:01 +02:00
|
|
|
if (flip->bayer)
|
|
|
|
return flip_bayer(link, frame);
|
|
|
|
|
2009-12-08 19:08:49 +02:00
|
|
|
for (i = 0; i < 4; i ++) {
|
|
|
|
int vsub = i == 1 || i == 2 ? flip->vsub : 0;
|
2016-01-27 17:19:38 +02:00
|
|
|
int height = AV_CEIL_RSHIFT(link->h, vsub);
|
2009-12-08 19:08:49 +02:00
|
|
|
|
2012-11-29 02:09:31 +03:00
|
|
|
if (frame->data[i]) {
|
2013-05-11 02:06:53 +03:00
|
|
|
frame->data[i] += (height - 1) * frame->linesize[i];
|
2012-11-29 02:09:31 +03:00
|
|
|
frame->linesize[i] = -frame->linesize[i];
|
2009-10-23 01:00:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-29 02:09:31 +03:00
|
|
|
return ff_filter_frame(link->dst->outputs[0], frame);
|
2009-10-23 01:00:33 +03:00
|
|
|
}
|
2012-07-24 16:14:01 +03:00
|
|
|
static const AVFilterPad avfilter_vf_vflip_inputs[] = {
|
|
|
|
{
|
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
2021-08-17 05:48:27 +02:00
|
|
|
.get_buffer.video = get_video_buffer,
|
2012-11-29 02:09:31 +03:00
|
|
|
.filter_frame = filter_frame,
|
2012-07-24 16:14:01 +03:00
|
|
|
.config_props = config_input,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_vf_vflip = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "vflip",
|
2009-10-27 02:43:45 +02:00
|
|
|
.description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
|
2013-09-07 15:13:50 +03:00
|
|
|
.priv_size = sizeof(FlipContext),
|
2016-08-28 11:00:06 +02:00
|
|
|
.priv_class = &vflip_class,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(avfilter_vf_vflip_inputs),
|
2023-08-03 14:37:51 +02:00
|
|
|
FILTER_OUTPUTS(ff_video_default_filterpad),
|
2016-08-28 11:00:06 +02:00
|
|
|
.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
|
2009-10-23 01:00:33 +03:00
|
|
|
};
|