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/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avfilter.h"
30 #include "internal.h"
31 #include "video.h"
32 
33 typedef struct FlipContext {
34  const AVClass *class;
35  int vsub; ///< vertical chroma subsampling
36  int bayer;
37 } FlipContext;
38 
39 static const AVOption vflip_options[] = {
40  { NULL }
41 };
42 
44 
46 {
47  FlipContext *flip = link->dst->priv;
49 
50  flip->vsub = desc->log2_chroma_h;
51  flip->bayer = !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
52 
53  return 0;
54 }
55 
57 {
58  FlipContext *flip = link->dst->priv;
59  AVFrame *frame;
60  int i;
61 
62  frame = ff_get_video_buffer(link->dst->outputs[0], w, h);
63  if (!frame)
64  return NULL;
65 
66  for (i = 0; i < 4; i ++) {
67  int vsub = i == 1 || i == 2 ? flip->vsub : 0;
68  int height = AV_CEIL_RSHIFT(h, vsub);
69 
70  if (frame->data[i]) {
71  frame->data[i] += (height - 1) * frame->linesize[i];
72  frame->linesize[i] = -frame->linesize[i];
73  }
74  }
75 
76  return frame;
77 }
78 
80 {
81  AVFilterContext *ctx = link->dst;
82  AVFilterLink *outlink = ctx->outputs[0];
83  AVFrame *out;
84  uint8_t *inrow = in->data[0], *outrow;
85  int i, width = outlink->w << (av_pix_fmt_desc_get(link->format)->comp[0].step > 1);
86  if (outlink->h & 1) {
87  av_log(ctx, AV_LOG_ERROR, "Bayer vertical flip needs even height\n");
88  return AVERROR_INVALIDDATA;
89  }
90 
91  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
92  if (!out) {
93  av_frame_free(&in);
94  return AVERROR(ENOMEM);
95  }
96  av_frame_copy_props(out, in);
97  outrow = out->data[0] + out->linesize[0] * (outlink->h - 2);
98  for (i = 0; i < outlink->h >> 1; i++) {
99  memcpy(outrow, inrow, width);
100  memcpy(outrow + out->linesize[0], inrow + in->linesize[0], width);
101  inrow += 2 * in->linesize[0];
102  outrow -= 2 * out->linesize[0];
103  }
104  av_frame_free(&in);
105  return ff_filter_frame(outlink, out);
106 }
107 
109 {
110  FlipContext *flip = link->dst->priv;
111  int i;
112 
113  if (flip->bayer)
114  return flip_bayer(link, frame);
115 
116  for (i = 0; i < 4; i ++) {
117  int vsub = i == 1 || i == 2 ? flip->vsub : 0;
118  int height = AV_CEIL_RSHIFT(link->h, vsub);
119 
120  if (frame->data[i]) {
121  frame->data[i] += (height - 1) * frame->linesize[i];
122  frame->linesize[i] = -frame->linesize[i];
123  }
124  }
125 
126  return ff_filter_frame(link->dst->outputs[0], frame);
127 }
129  {
130  .name = "default",
131  .type = AVMEDIA_TYPE_VIDEO,
132  .get_video_buffer = get_video_buffer,
133  .filter_frame = filter_frame,
134  .config_props = config_input,
135  },
136  { NULL }
137 };
138 
140  {
141  .name = "default",
142  .type = AVMEDIA_TYPE_VIDEO,
143  },
144  { NULL }
145 };
146 
148  .name = "vflip",
149  .description = NULL_IF_CONFIG_SMALL("Flip the input video vertically."),
150  .priv_size = sizeof(FlipContext),
151  .priv_class = &vflip_class,
152  .inputs = avfilter_vf_vflip_inputs,
153  .outputs = avfilter_vf_vflip_outputs,
155 };
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
AVOption.
Definition: opt.h:248
const char * desc
Definition: libsvtav1.c:79
Main libavfilter public API header.
static int flip_bayer(AVFilterLink *link, AVFrame *in)
Definition: vf_vflip.c:79
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:99
#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:126
const char * name
Pad name.
Definition: internal.h:60
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1094
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
AVOptions.
static const AVFilterPad avfilter_vf_vflip_inputs[]
Definition: vf_vflip.c:128
static int filter_frame(AVFilterLink *link, AVFrame *frame)
Definition: vf_vflip.c:108
int bayer
Definition: vf_vflip.c:36
#define height
#define av_log(a,...)
static const AVOption vflip_options[]
Definition: vf_vflip.c:39
A filter pad used for either input or output.
Definition: internal.h:54
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
static void flip(AVCodecContext *avctx, AVFrame *frame)
Definition: rawdec.c:137
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
void * priv
private data for use by the filter
Definition: avfilter.h:356
AVFILTER_DEFINE_CLASS(vflip)
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
#define width
uint8_t w
Definition: llviddspenc.c:39
AVFormatContext * ctx
Definition: movenc.c:48
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
#define AV_PIX_FMT_FLAG_BAYER
The pixel format is following a Bayer pattern.
Definition: pixdesc.h:184
static int config_input(AVFilterLink *link)
Definition: vf_vflip.c:45
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:345
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
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 inputs
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
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
AVFilter ff_vf_vflip
Definition: vf_vflip.c:147
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
#define flags(name, subs,...)
Definition: cbs_av1.c:561
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:328
static const AVFilterPad avfilter_vf_vflip_outputs[]
Definition: vf_vflip.c:139
An instance of a filter.
Definition: avfilter.h:341
static AVFrame * get_video_buffer(AVFilterLink *link, int w, int h)
Definition: vf_vflip.c:56
FILE * out
Definition: movenc.c:54
int vsub
vertical chroma subsampling
Definition: vf_vflip.c:35
internal API functions
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
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:658
int i
Definition: input.c:407
int step
Number of elements between 2 horizontally consecutive pixels.
Definition: pixdesc.h:41
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58