FFmpeg
vf_vflip.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Bobby Bingham
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * video vertical flip filter
24  */
25 
26 #include "libavutil/internal.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "filters.h"
30 #include "video.h"
31 
32 typedef struct FlipContext {
33  int vsub; ///< vertical chroma subsampling
34  int bayer;
35 } FlipContext;
36 
38 {
39  FlipContext *flip = link->dst->priv;
41 
42  flip->vsub = desc->log2_chroma_h;
43  flip->bayer = !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
44 
45  return 0;
46 }
47 
49 {
50  FlipContext *flip = link->dst->priv;
51  AVFrame *frame;
52  int i;
53 
54  frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
55  if (!frame)
56  return NULL;
57 
58  for (i = 0; i < 4; i ++) {
59  int vsub = i == 1 || i == 2 ? flip->vsub : 0;
60  int height = AV_CEIL_RSHIFT(h, vsub);
61 
62  if (frame->data[i]) {
63  frame->data[i] += (height - 1) * frame->linesize[i];
64  frame->linesize[i] = -frame->linesize[i];
65  }
66  }
67 
68  return frame;
69 }
70 
72 {
73  AVFilterContext *ctx = link->dst;
74  AVFilterLink *outlink = ctx->outputs[0];
75  AVFrame *out;
76  uint8_t *inrow = in->data[0], *outrow;
77  int i, width = outlink->w << (av_pix_fmt_desc_get(link->format)->comp[0].step > 1);
78  if (outlink->h & 1) {
79  av_log(ctx, AV_LOG_ERROR, "Bayer vertical flip needs even height\n");
80  return AVERROR_INVALIDDATA;
81  }
82 
83  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
84  if (!out) {
85  av_frame_free(&in);
86  return AVERROR(ENOMEM);
87  }
89  outrow = out->data[0] + out->linesize[0] * (outlink->h - 2);
90  for (i = 0; i < outlink->h >> 1; i++) {
91  memcpy(outrow, inrow, width);
92  memcpy(outrow + out->linesize[0], inrow + in->linesize[0], width);
93  inrow += 2 * in->linesize[0];
94  outrow -= 2 * out->linesize[0];
95  }
96  av_frame_free(&in);
97  return ff_filter_frame(outlink, out);
98 }
99 
101 {
102  FlipContext *flip = link->dst->priv;
103  int i;
104 
105  if (flip->bayer)
106  return flip_bayer(link, frame);
107 
108  for (i = 0; i < 4; i ++) {
109  int vsub = i == 1 || i == 2 ? flip->vsub : 0;
110  int height = AV_CEIL_RSHIFT(link->h, vsub);
111 
112  if (frame->data[i]) {
113  frame->data[i] += (height - 1) * frame->linesize[i];
114  frame->linesize[i] = -frame->linesize[i];
115  }
116  }
117 
118  return ff_filter_frame(link->dst->outputs[0], frame);
119 }
121  {
122  .name = "default",
123  .type = AVMEDIA_TYPE_VIDEO,
124  .get_buffer.video = get_video_buffer,
125  .filter_frame = filter_frame,
126  .config_props = config_input,
127  },
128 };
129 
131  .name = "vflip",
132  .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
133  .priv_size = sizeof(FlipContext),
137 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
avfilter_vf_vflip_inputs
static const AVFilterPad avfilter_vf_vflip_inputs[]
Definition: vf_vflip.c:120
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out
FILE * out
Definition: movenc.c:55
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1061
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
w
uint8_t w
Definition: llviddspenc.c:38
AVComponentDescriptor::step
int step
Number of elements between 2 horizontally consecutive pixels.
Definition: pixdesc.h:40
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
video.h
config_input
static int config_input(AVFilterLink *link)
Definition: vf_vflip.c:37
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
filter_frame
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_vflip.c:100
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
ff_vf_vflip
const AVFilter ff_vf_vflip
Definition: vf_vflip.c:130
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FlipContext::vsub
int vsub
vertical chroma subsampling
Definition: vf_vflip.c:33
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:725
get_video_buffer
static AVFrame * get_video_buffer(AVFilterLink *link, int w, int h)
Definition: vf_vflip.c:48
flip
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:131
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
height
#define height
Definition: dsp.h:85
AVFrame::format
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:476
AV_PIX_FMT_FLAG_BAYER
#define AV_PIX_FMT_FLAG_BAYER
The pixel format is following a Bayer pattern.
Definition: pixdesc.h:152
AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:182
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
internal.h
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AVFilter
Filter definition.
Definition: avfilter.h:201
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FlipContext::bayer
int bayer
Definition: vf_vflip.c:34
FlipContext
Definition: hflip.h:27
avfilter.h
AVPixFmtDescriptor::comp
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:105
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070
width
#define width
Definition: dsp.h:85
flip_bayer
static int flip_bayer(AVFilterLink *link, AVFrame *in)
Definition: vf_vflip.c:71