FFmpeg
 All Data Structures 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 {
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  static const enum AVPixelFormat pix_fmts[] = {
71  };
72 
74  return 0;
75 }
76 
77 static int config_props(AVFilterLink *inlink)
78 {
79  FlipContext *flip = inlink->dst->priv;
80  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
81 
82  av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
83  flip->hsub = pix_desc->log2_chroma_w;
84  flip->vsub = pix_desc->log2_chroma_h;
85 
86  return 0;
87 }
88 
90 {
91  AVFilterContext *ctx = inlink->dst;
92  FlipContext *flip = ctx->priv;
93  AVFilterLink *outlink = ctx->outputs[0];
94  AVFilterBufferRef *out;
95  uint8_t *inrow, *outrow;
96  int i, j, plane, step, hsub, vsub;
97 
98  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
99  if (!out) {
101  return AVERROR(ENOMEM);
102  }
104 
105  /* copy palette if required */
107  memcpy(out->data[1], in->data[1], AVPALETTE_SIZE);
108 
109  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
110  step = flip->max_step[plane];
111  hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
112  vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
113 
114  outrow = out->data[plane];
115  inrow = in ->data[plane] + ((inlink->w >> hsub) - 1) * step;
116  for (i = 0; i < in->video->h >> vsub; i++) {
117  switch (step) {
118  case 1:
119  for (j = 0; j < (inlink->w >> hsub); j++)
120  outrow[j] = inrow[-j];
121  break;
122 
123  case 2:
124  {
125  uint16_t *outrow16 = (uint16_t *)outrow;
126  uint16_t * inrow16 = (uint16_t *) inrow;
127  for (j = 0; j < (inlink->w >> hsub); j++)
128  outrow16[j] = inrow16[-j];
129  }
130  break;
131 
132  case 3:
133  {
134  uint8_t *in = inrow;
135  uint8_t *out = outrow;
136  for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
137  int32_t v = AV_RB24(in);
138  AV_WB24(out, v);
139  }
140  }
141  break;
142 
143  case 4:
144  {
145  uint32_t *outrow32 = (uint32_t *)outrow;
146  uint32_t * inrow32 = (uint32_t *) inrow;
147  for (j = 0; j < (inlink->w >> hsub); j++)
148  outrow32[j] = inrow32[-j];
149  }
150  break;
151 
152  default:
153  for (j = 0; j < (inlink->w >> hsub); j++)
154  memcpy(outrow + j*step, inrow - j*step, step);
155  }
156 
157  inrow += in ->linesize[plane];
158  outrow += out->linesize[plane];
159  }
160  }
161 
163  return ff_filter_frame(outlink, out);
164 }
165 
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_VIDEO,
170  .filter_frame = filter_frame,
171  .config_props = config_props,
172  .min_perms = AV_PERM_READ,
173  },
174  { NULL }
175 };
176 
178  {
179  .name = "default",
180  .type = AVMEDIA_TYPE_VIDEO,
181  },
182  { NULL }
183 };
184 
186  .name = "hflip",
187  .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
188  .priv_size = sizeof(FlipContext),
190 
191  .inputs = avfilter_vf_hflip_inputs,
192  .outputs = avfilter_vf_hflip_outputs,
193 };