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 {
39  int max_step[4]; ///< max pixel step for each plane, expressed as a number of bytes
40  int hsub, vsub; ///< chroma subsampling
41 } FlipContext;
42 
44 {
45  AVFilterFormats *pix_fmts = NULL;
46  int fmt;
47 
48  for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
49  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
50  if (!(desc->flags & PIX_FMT_HWACCEL ||
51  desc->flags & PIX_FMT_BITSTREAM ||
52  (desc->log2_chroma_w != desc->log2_chroma_h &&
53  desc->comp[0].plane == desc->comp[1].plane)))
54  ff_add_format(&pix_fmts, fmt);
55  }
56 
57  ff_set_common_formats(ctx, pix_fmts);
58  return 0;
59 }
60 
61 static int config_props(AVFilterLink *inlink)
62 {
63  FlipContext *flip = inlink->dst->priv;
64  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
65 
66  av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
67  flip->hsub = pix_desc->log2_chroma_w;
68  flip->vsub = pix_desc->log2_chroma_h;
69 
70  return 0;
71 }
72 
73 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
74 {
75  AVFilterContext *ctx = inlink->dst;
76  FlipContext *flip = ctx->priv;
77  AVFilterLink *outlink = ctx->outputs[0];
78  AVFrame *out;
79  uint8_t *inrow, *outrow;
80  int i, j, plane, step, hsub, vsub;
81 
82  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
83  if (!out) {
84  av_frame_free(&in);
85  return AVERROR(ENOMEM);
86  }
87  av_frame_copy_props(out, in);
88 
89  /* copy palette if required */
91  memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
92 
93  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
94  step = flip->max_step[plane];
95  hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
96  vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
97 
98  outrow = out->data[plane];
99  inrow = in ->data[plane] + ((inlink->w >> hsub) - 1) * step;
100  for (i = 0; i < in->height >> vsub; i++) {
101  switch (step) {
102  case 1:
103  for (j = 0; j < (inlink->w >> hsub); j++)
104  outrow[j] = inrow[-j];
105  break;
106 
107  case 2:
108  {
109  uint16_t *outrow16 = (uint16_t *)outrow;
110  uint16_t * inrow16 = (uint16_t *) inrow;
111  for (j = 0; j < (inlink->w >> hsub); j++)
112  outrow16[j] = inrow16[-j];
113  }
114  break;
115 
116  case 3:
117  {
118  uint8_t *in = inrow;
119  uint8_t *out = outrow;
120  for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
121  int32_t v = AV_RB24(in);
122  AV_WB24(out, v);
123  }
124  }
125  break;
126 
127  case 4:
128  {
129  uint32_t *outrow32 = (uint32_t *)outrow;
130  uint32_t * inrow32 = (uint32_t *) inrow;
131  for (j = 0; j < (inlink->w >> hsub); j++)
132  outrow32[j] = inrow32[-j];
133  }
134  break;
135 
136  default:
137  for (j = 0; j < (inlink->w >> hsub); j++)
138  memcpy(outrow + j*step, inrow - j*step, step);
139  }
140 
141  inrow += in ->linesize[plane];
142  outrow += out->linesize[plane];
143  }
144  }
145 
146  av_frame_free(&in);
147  return ff_filter_frame(outlink, out);
148 }
149 
151  {
152  .name = "default",
153  .type = AVMEDIA_TYPE_VIDEO,
154  .filter_frame = filter_frame,
155  .config_props = config_props,
156  },
157  { NULL }
158 };
159 
161  {
162  .name = "default",
163  .type = AVMEDIA_TYPE_VIDEO,
164  },
165  { NULL }
166 };
167 
169  .name = "hflip",
170  .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
171  .priv_size = sizeof(FlipContext),
173 
174  .inputs = avfilter_vf_hflip_inputs,
175  .outputs = avfilter_vf_hflip_outputs,
176 };
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_hflip.c:73
float v
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
int max_step[4]
max pixel step for each plane, expressed as a number of bytes
Definition: vf_hflip.c:39
const char * fmt
Definition: avisynth_c.h:669
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
#define AV_RB24
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
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:32
const char * name
Pad name.
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:86
uint8_t
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
static const AVFilterPad avfilter_vf_hflip_outputs[]
Definition: vf_hflip.c:160
static int config_props(AVFilterLink *inlink)
Definition: vf_hflip.c:61
void 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:545
A filter pad used for either input or output.
static int query_formats(AVFilterContext *ctx)
Definition: vf_hflip.c:43
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
#define PIX_FMT_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:90
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:344
common internal API header
static const AVFilterPad avfilter_vf_hflip_inputs[]
Definition: vf_hflip.c:150
int32_t
#define AV_WB24(p, d)
Definition: intreadwrite.h:442
#define PIX_FMT_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:91
AVFilter avfilter_vf_hflip
Definition: vf_hflip.c:168
#define PIX_FMT_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:92
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
Filter definition.
Definition: avfilter.h:436
synthesis window for stochastic i
int hsub
Definition: vf_hflip.c:40
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
uint16_t plane
which of the 4 planes contains the component
Definition: pixdesc.h:29
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
static void flip(AVCodecContext *avctx, AVPicture *picture)
A list of supported formats for one end of a filter link.
Definition: formats.h:64
An instance of a filter.
Definition: avfilter.h:524
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:237
int height
Definition: frame.h:122
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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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_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_dlog(ac->avr,"%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-> out
int vsub
chroma subsampling
Definition: vf_hflip.c:40
internal API functions
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
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