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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
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];
40  int hsub, vsub;
41 } FlipContext;
42 
44 {
45  static const enum AVPixelFormat pix_fmts[] = {
69  };
70 
72  return 0;
73 }
74 
75 static int config_props(AVFilterLink *inlink)
76 {
77  FlipContext *flip = inlink->dst->priv;
78  const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(inlink->format);
79 
80  av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
81  flip->hsub = pix_desc->log2_chroma_w;
82  flip->vsub = pix_desc->log2_chroma_h;
83 
84  return 0;
85 }
86 
88 {
89  AVFilterContext *ctx = inlink->dst;
90  FlipContext *flip = ctx->priv;
91  AVFilterLink *outlink = ctx->outputs[0];
92  AVFilterBufferRef *out;
93  uint8_t *inrow, *outrow;
94  int i, j, plane, step, hsub, vsub;
95 
96  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
97  if (!out) {
99  return AVERROR(ENOMEM);
100  }
102 
103  for (plane = 0; plane < 4 && in->data[plane]; plane++) {
104  step = flip->max_step[plane];
105  hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
106  vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
107 
108  outrow = out->data[plane];
109  inrow = in ->data[plane] + ((inlink->w >> hsub) - 1) * step;
110  for (i = 0; i < in->video->h >> vsub; i++) {
111  switch (step) {
112  case 1:
113  for (j = 0; j < (inlink->w >> hsub); j++)
114  outrow[j] = inrow[-j];
115  break;
116 
117  case 2:
118  {
119  uint16_t *outrow16 = (uint16_t *)outrow;
120  uint16_t * inrow16 = (uint16_t *) inrow;
121  for (j = 0; j < (inlink->w >> hsub); j++)
122  outrow16[j] = inrow16[-j];
123  }
124  break;
125 
126  case 3:
127  {
128  uint8_t *in = inrow;
129  uint8_t *out = outrow;
130  for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
131  int32_t v = AV_RB24(in);
132  AV_WB24(out, v);
133  }
134  }
135  break;
136 
137  case 4:
138  {
139  uint32_t *outrow32 = (uint32_t *)outrow;
140  uint32_t * inrow32 = (uint32_t *) inrow;
141  for (j = 0; j < (inlink->w >> hsub); j++)
142  outrow32[j] = inrow32[-j];
143  }
144  break;
145 
146  default:
147  for (j = 0; j < (inlink->w >> hsub); j++)
148  memcpy(outrow + j*step, inrow - j*step, step);
149  }
150 
151  inrow += in ->linesize[plane];
152  outrow += out->linesize[plane];
153  }
154  }
155 
157  return ff_filter_frame(outlink, out);
158 }
159 
161  {
162  .name = "default",
163  .type = AVMEDIA_TYPE_VIDEO,
164  .filter_frame = filter_frame,
165  .config_props = config_props,
166  .min_perms = AV_PERM_READ,
167  },
168  { NULL }
169 };
170 
172  {
173  .name = "default",
174  .type = AVMEDIA_TYPE_VIDEO,
175  },
176  { NULL }
177 };
178 
180  .name = "hflip",
181  .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
182  .priv_size = sizeof(FlipContext),
184 
185  .inputs = avfilter_vf_hflip_inputs,
186  .outputs = avfilter_vf_hflip_outputs,
187 };