FFmpeg
vf_hflip.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 Benoit Fouet
3  * Copyright (c) 2010 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file
24  * horizontal flip filter
25  */
26 
27 #include <string.h>
28 
29 #include "libavutil/opt.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "hflip.h"
33 #include "internal.h"
34 #include "video.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/internal.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/imgutils.h"
39 
40 static const AVOption hflip_options[] = {
41  { NULL }
42 };
43 
45 
47 {
49  int fmt, ret;
50 
51  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
53  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
55  (desc->log2_chroma_w != desc->log2_chroma_h &&
56  desc->comp[0].plane == desc->comp[1].plane)) &&
57  (ret = ff_add_format(&pix_fmts, fmt)) < 0)
58  return ret;
59  }
60 
62 }
63 
64 static void hflip_byte_c(const uint8_t *src, uint8_t *dst, int w)
65 {
66  int j;
67 
68  for (j = 0; j < w; j++)
69  dst[j] = src[-j];
70 }
71 
72 static void hflip_short_c(const uint8_t *ssrc, uint8_t *ddst, int w)
73 {
74  const uint16_t *src = (const uint16_t *)ssrc;
75  uint16_t *dst = (uint16_t *)ddst;
76  int j;
77 
78  for (j = 0; j < w; j++)
79  dst[j] = src[-j];
80 }
81 
82 static void hflip_dword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
83 {
84  const uint32_t *src = (const uint32_t *)ssrc;
85  uint32_t *dst = (uint32_t *)ddst;
86  int j;
87 
88  for (j = 0; j < w; j++)
89  dst[j] = src[-j];
90 }
91 
92 static void hflip_b24_c(const uint8_t *src, uint8_t *dst, int w)
93 {
94  const uint8_t *in = src;
95  uint8_t *out = dst;
96  int j;
97 
98  for (j = 0; j < w; j++, out += 3, in -= 3) {
99  int32_t v = AV_RB24(in);
100 
101  AV_WB24(out, v);
102  }
103 }
104 
105 static void hflip_b48_c(const uint8_t *src, uint8_t *dst, int w)
106 {
107  const uint8_t *in = src;
108  uint8_t *out = dst;
109  int j;
110 
111  for (j = 0; j < w; j++, out += 6, in -= 6) {
112  int64_t v = AV_RB48(in);
113 
114  AV_WB48(out, v);
115  }
116 }
117 
118 static void hflip_qword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
119 {
120  const uint64_t *src = (const uint64_t *)ssrc;
121  uint64_t *dst = (uint64_t *)ddst;
122  int j;
123 
124  for (j = 0; j < w; j++)
125  dst[j] = src[-j];
126 }
127 
129 {
130  FlipContext *s = inlink->dst->priv;
131  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
132  const int hsub = pix_desc->log2_chroma_w;
133  const int vsub = pix_desc->log2_chroma_h;
134  int nb_planes;
135 
136  av_image_fill_max_pixsteps(s->max_step, NULL, pix_desc);
137  s->planewidth[0] = s->planewidth[3] = inlink->w;
138  s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
139  s->planeheight[0] = s->planeheight[3] = inlink->h;
140  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
141 
142  nb_planes = av_pix_fmt_count_planes(inlink->format);
143 
144  return ff_hflip_init(s, s->max_step, nb_planes);
145 }
146 
147 int ff_hflip_init(FlipContext *s, int step[4], int nb_planes)
148 {
149  int i;
150 
151  for (i = 0; i < nb_planes; i++) {
152  switch (step[i]) {
153  case 1: s->flip_line[i] = hflip_byte_c; break;
154  case 2: s->flip_line[i] = hflip_short_c; break;
155  case 3: s->flip_line[i] = hflip_b24_c; break;
156  case 4: s->flip_line[i] = hflip_dword_c; break;
157  case 6: s->flip_line[i] = hflip_b48_c; break;
158  case 8: s->flip_line[i] = hflip_qword_c; break;
159  default:
160  return AVERROR_BUG;
161  }
162  }
163  if (ARCH_X86)
164  ff_hflip_init_x86(s, step, nb_planes);
165 
166  return 0;
167 }
168 
169 typedef struct ThreadData {
170  AVFrame *in, *out;
171 } ThreadData;
172 
173 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
174 {
175  FlipContext *s = ctx->priv;
176  ThreadData *td = arg;
177  AVFrame *in = td->in;
178  AVFrame *out = td->out;
179  uint8_t *inrow, *outrow;
180  int i, plane, step;
181 
182  for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
183  const int width = s->planewidth[plane];
184  const int height = s->planeheight[plane];
185  const int start = (height * job ) / nb_jobs;
186  const int end = (height * (job+1)) / nb_jobs;
187 
188  step = s->max_step[plane];
189 
190  outrow = out->data[plane] + start * out->linesize[plane];
191  inrow = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
192  for (i = start; i < end; i++) {
193  s->flip_line[plane](inrow, outrow, width);
194 
195  inrow += in ->linesize[plane];
196  outrow += out->linesize[plane];
197  }
198  }
199 
200  return 0;
201 }
202 
204 {
205  AVFilterContext *ctx = inlink->dst;
206  AVFilterLink *outlink = ctx->outputs[0];
207  ThreadData td;
208  AVFrame *out;
209 
210  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
211  if (!out) {
212  av_frame_free(&in);
213  return AVERROR(ENOMEM);
214  }
216 
217  /* copy palette if required */
219  memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
220 
221  td.in = in, td.out = out;
222  ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ff_filter_get_nb_threads(ctx)));
223 
224  av_frame_free(&in);
225  return ff_filter_frame(outlink, out);
226 }
227 
229  {
230  .name = "default",
231  .type = AVMEDIA_TYPE_VIDEO,
232  .filter_frame = filter_frame,
233  .config_props = config_props,
234  },
235  { NULL }
236 };
237 
239  {
240  .name = "default",
241  .type = AVMEDIA_TYPE_VIDEO,
242  },
243  { NULL }
244 };
245 
247  .name = "hflip",
248  .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
249  .priv_size = sizeof(FlipContext),
250  .priv_class = &hflip_class,
255 };
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:99
td
#define td
Definition: regdef.h:70
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
opt.h
AV_RB48
#define AV_RB48(x)
Definition: intreadwrite.h:472
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2522
hflip_options
static const AVOption hflip_options[]
Definition: vf_hflip.c:40
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
end
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixdesc.h
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
w
uint8_t w
Definition: llviddspenc.c:38
AVOption
AVOption.
Definition: opt.h:246
hflip_qword_c
static void hflip_qword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
Definition: vf_hflip.c:118
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:488
video.h
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1795
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:64
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2562
fmt
const char * fmt
Definition: avisynth_c.h:861
start
void INT64 start
Definition: avisynth_c.h:767
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
plane
int plane
Definition: avisynth_c.h:384
src
#define src
Definition: vp8dsp.c:254
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:568
ff_hflip_init_x86
void ff_hflip_init_x86(FlipContext *s, int step[4], int nb_planes)
Definition: vf_hflip_init.c:31
width
#define width
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:257
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_hflip.c:203
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AVPixFmtDescriptor::log2_chroma_w
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
int32_t
int32_t
Definition: audio_convert.c:194
arg
const char * arg
Definition: jacosubdec.c:66
ff_vf_hflip
AVFilter ff_vf_hflip
Definition: vf_hflip.c:246
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:654
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
ff_add_format
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(hflip)
inputs
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
Definition: filter_design.txt:243
AVPixFmtDescriptor::flags
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
desc
const char * desc
Definition: nvenc.c:68
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:188
hflip_dword_c
static void hflip_dword_c(const uint8_t *ssrc, uint8_t *ddst, int w)
Definition: vf_hflip.c:82
AV_PIX_FMT_FLAG_BITSTREAM
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
AV_WB24
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
hflip.h
height
#define height
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
ff_hflip_init
int ff_hflip_init(FlipContext *s, int step[4], int nb_planes)
Definition: vf_hflip.c:147
internal.h
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:125
hflip_b24_c
static void hflip_b24_c(const uint8_t *src, uint8_t *dst, int w)
Definition: vf_hflip.c:92
in
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
Definition: audio_convert.c:326
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
internal.h
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:802
ThreadData
Used for passing data between threads.
Definition: af_adeclick.c:487
uint8_t
uint8_t
Definition: audio_convert.c:194
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AV_WB48
#define AV_WB48(p, darg)
Definition: intreadwrite.h:481
avfilter_vf_hflip_inputs
static const AVFilterPad avfilter_vf_hflip_inputs[]
Definition: vf_hflip.c:228
AVFilter
Filter definition.
Definition: avfilter.h:144
config_props
static int config_props(AVFilterLink *inlink)
Definition: vf_hflip.c:128
ret
ret
Definition: filter_design.txt:187
FlipContext
Definition: hflip.h:27
avfilter.h
av_image_fill_max_pixsteps
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:35
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:116
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ThreadData::in
AVFrame * in
Definition: af_afftdn.c:1082
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
hflip_byte_c
static void hflip_byte_c(const uint8_t *src, uint8_t *dst, int w)
Definition: vf_hflip.c:64
hflip_b48_c
static void hflip_b48_c(const uint8_t *src, uint8_t *dst, int w)
Definition: vf_hflip.c:105
avfilter_vf_hflip_outputs
static const AVFilterPad avfilter_vf_hflip_outputs[]
Definition: vf_hflip.c:238
filter_slice
static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
Definition: vf_hflip.c:173
imgutils.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
AV_RB24
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
Definition: bytestream.h:93
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
AVPixFmtDescriptor::log2_chroma_h
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_hflip.c:46
hflip_short_c
static void hflip_short_c(const uint8_t *ssrc, uint8_t *ddst, int w)
Definition: vf_hflip.c:72