FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "avfilter.h"
30 #include "formats.h"
31 #include "internal.h"
32 #include "video.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/internal.h"
35 #include "libavutil/intreadwrite.h"
36 #include "libavutil/imgutils.h"
37 
38 typedef struct FlipContext {
39  int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
40  int planewidth[4]; ///< width of each plane
41  int planeheight[4]; ///< height of each plane
42 } FlipContext;
43 
45 {
47  int fmt, ret;
48 
49  for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
50  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
51  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
53  (desc->log2_chroma_w != desc->log2_chroma_h &&
54  desc->comp[0].plane == desc->comp[1].plane)) &&
55  (ret = ff_add_format(&pix_fmts, fmt)) < 0)
56  return ret;
57  }
58 
59  return ff_set_common_formats(ctx, pix_fmts);
60 }
61 
62 static int config_props(AVFilterLink *inlink)
63 {
64  FlipContext *s = inlink->dst->priv;
65  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
66  const int hsub = pix_desc->log2_chroma_w;
67  const int vsub = pix_desc->log2_chroma_h;
68 
70  s->planewidth[0] = s->planewidth[3] = inlink->w;
71  s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, hsub);
72  s->planeheight[0] = s->planeheight[3] = inlink->h;
73  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, vsub);
74 
75  return 0;
76 }
77 
78 typedef struct ThreadData {
79  AVFrame *in, *out;
80 } ThreadData;
81 
82 static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
83 {
84  FlipContext *s = ctx->priv;
85  ThreadData *td = arg;
86  AVFrame *in = td->in;
87  AVFrame *out = td->out;
88  uint8_t *inrow, *outrow;
89  int i, j, plane, step;
90 
91  for (plane = 0; plane < 4 && in->data[plane] && in->linesize[plane]; plane++) {
92  const int width = s->planewidth[plane];
93  const int height = s->planeheight[plane];
94  const int start = (height * job ) / nb_jobs;
95  const int end = (height * (job+1)) / nb_jobs;
96 
97  step = s->max_step[plane];
98 
99  outrow = out->data[plane] + start * out->linesize[plane];
100  inrow = in ->data[plane] + start * in->linesize[plane] + (width - 1) * step;
101  for (i = start; i < end; i++) {
102  switch (step) {
103  case 1:
104  for (j = 0; j < width; j++)
105  outrow[j] = inrow[-j];
106  break;
107 
108  case 2:
109  {
110  uint16_t *outrow16 = (uint16_t *)outrow;
111  uint16_t * inrow16 = (uint16_t *) inrow;
112  for (j = 0; j < width; j++)
113  outrow16[j] = inrow16[-j];
114  }
115  break;
116 
117  case 3:
118  {
119  uint8_t *in = inrow;
120  uint8_t *out = outrow;
121  for (j = 0; j < width; j++, out += 3, in -= 3) {
122  int32_t v = AV_RB24(in);
123  AV_WB24(out, v);
124  }
125  }
126  break;
127 
128  case 4:
129  {
130  uint32_t *outrow32 = (uint32_t *)outrow;
131  uint32_t * inrow32 = (uint32_t *) inrow;
132  for (j = 0; j < width; j++)
133  outrow32[j] = inrow32[-j];
134  }
135  break;
136 
137  default:
138  for (j = 0; j < width; j++)
139  memcpy(outrow + j*step, inrow - j*step, step);
140  }
141 
142  inrow += in ->linesize[plane];
143  outrow += out->linesize[plane];
144  }
145  }
146 
147  return 0;
148 }
149 
150 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
151 {
152  AVFilterContext *ctx = inlink->dst;
153  AVFilterLink *outlink = ctx->outputs[0];
154  ThreadData td;
155  AVFrame *out;
156 
157  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
158  if (!out) {
159  av_frame_free(&in);
160  return AVERROR(ENOMEM);
161  }
162  av_frame_copy_props(out, in);
163 
164  /* copy palette if required */
166  memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
167 
168  td.in = in, td.out = out;
169  ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(outlink->h, ctx->graph->nb_threads));
170 
171  av_frame_free(&in);
172  return ff_filter_frame(outlink, out);
173 }
174 
176  {
177  .name = "default",
178  .type = AVMEDIA_TYPE_VIDEO,
179  .filter_frame = filter_frame,
180  .config_props = config_props,
181  },
182  { NULL }
183 };
184 
186  {
187  .name = "default",
188  .type = AVMEDIA_TYPE_VIDEO,
189  },
190  { NULL }
191 };
192 
194  .name = "hflip",
195  .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
196  .priv_size = sizeof(FlipContext),
198  .inputs = avfilter_vf_hflip_inputs,
199  .outputs = avfilter_vf_hflip_outputs,
201 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
int plane
Definition: avisynth_c.h:291
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_hflip.c:150
int plane
Which of the 4 planes contains the component.
Definition: pixdesc.h:35
#define NULL
Definition: coverity.c:32
int planeheight[4]
height of each plane
Definition: vf_hflip.c:41
const char * s
Definition: avisynth_c.h:631
AVFrame * out
Definition: af_sofalizer.c:499
AVFilter ff_vf_hflip
Definition: vf_hflip.c:193
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2157
This structure describes decoded (raw) audio or video data.
Definition: frame.h:181
int max_step[4]
max pixel step for each plane, expressed as a number of bytes
Definition: vf_hflip.c:39
AVFormatContext * ctx
Definition: movenc-test.c:48
const char * fmt
Definition: avisynth_c.h:632
misc image utilities
Main libavfilter public API header.
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:76
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:321
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:34
const char * name
Pad name.
Definition: internal.h:59
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1163
AVFrame * in
Definition: af_sofalizer.c:499
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
uint8_t
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
static const AVFilterPad avfilter_vf_hflip_outputs[]
Definition: vf_hflip.c:185
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:788
static int config_props(AVFilterLink *inlink)
Definition: vf_hflip.c:62
A filter pad used for either input or output.
Definition: internal.h:53
int planewidth[4]
width of each plane
Definition: vf_hflip.c:40
static int query_formats(AVFilterContext *ctx)
Definition: vf_hflip.c:44
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
#define td
Definition: regdef.h:70
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:154
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
void * priv
private data for use by the filter
Definition: avfilter.h:319
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:113
const char * arg
Definition: jacosubdec.c:66
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:337
common internal API header
uint64_t flags
Combination of AV_PIX_FMT_FLAG_...
Definition: pixdesc.h:106
#define FFMIN(a, b)
Definition: common.h:96
static const AVFilterPad avfilter_vf_hflip_inputs[]
Definition: vf_hflip.c:175
int32_t
#define AV_WB24(p, d)
Definition: intreadwrite.h:450
static const AVFilterPad outputs[]
Definition: af_afftfilt.c:385
FILE * out
Definition: movenc-test.c:54
static const AVFilterPad inputs[]
Definition: af_afftfilt.c:375
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:87
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:209
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
Filter definition.
Definition: avfilter.h:141
const char * name
Filter name.
Definition: avfilter.h:145
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:316
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
Definition: avfilter.h:344
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:192
avfilter_execute_func * execute
Definition: internal.h:153
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:304
void INT64 start
Definition: avisynth_c.h:553
static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
Definition: vf_hflip.c:82
internal API functions
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:565
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
static int width