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 "avfilter.h"
28 #include "libavutil/pixdesc.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/imgutils.h"
31 
32 typedef struct {
33  int max_step[4];
34  int hsub, vsub;
35 } FlipContext;
36 
38 {
39  static const enum PixelFormat pix_fmts[] = {
63  };
64 
66  return 0;
67 }
68 
69 static int config_props(AVFilterLink *inlink)
70 {
71  FlipContext *flip = inlink->dst->priv;
72  const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
73 
74  av_image_fill_max_pixsteps(flip->max_step, NULL, pix_desc);
77 
78  return 0;
79 }
80 
81 static void draw_slice(AVFilterLink *inlink, int y, int h, int slice_dir)
82 {
83  FlipContext *flip = inlink->dst->priv;
84  AVFilterBufferRef *inpic = inlink->cur_buf;
85  AVFilterBufferRef *outpic = inlink->dst->outputs[0]->out_buf;
86  uint8_t *inrow, *outrow;
87  int i, j, plane, step, hsub, vsub;
88 
89  for (plane = 0; plane < 4 && inpic->data[plane]; plane++) {
90  step = flip->max_step[plane];
91  hsub = (plane == 1 || plane == 2) ? flip->hsub : 0;
92  vsub = (plane == 1 || plane == 2) ? flip->vsub : 0;
93 
94  outrow = outpic->data[plane] + (y>>vsub) * outpic->linesize[plane];
95  inrow = inpic ->data[plane] + (y>>vsub) * inpic ->linesize[plane] + ((inlink->w >> hsub) - 1) * step;
96  for (i = 0; i < h>>vsub; i++) {
97  switch (step) {
98  case 1:
99  {
100  for (j = 0; j < (inlink->w >> hsub); j++)
101  outrow[j] = inrow[-j];
102  }
103  break;
104 
105  case 2:
106  {
107  uint16_t *outrow16 = (uint16_t *)outrow;
108  uint16_t * inrow16 = (uint16_t *) inrow;
109  for (j = 0; j < (inlink->w >> hsub); j++)
110  outrow16[j] = inrow16[-j];
111  }
112  break;
113 
114  case 3:
115  {
116  uint8_t *in = inrow;
117  uint8_t *out = outrow;
118  for (j = 0; j < (inlink->w >> hsub); j++, out += 3, in -= 3) {
119  int32_t v = AV_RB24(in);
120  AV_WB24(out, v);
121  }
122  }
123  break;
124 
125  case 4:
126  {
127  uint32_t *outrow32 = (uint32_t *)outrow;
128  uint32_t * inrow32 = (uint32_t *) inrow;
129  for (j = 0; j < (inlink->w >> hsub); j++)
130  outrow32[j] = inrow32[-j];
131  }
132  break;
133 
134  default:
135  for (j = 0; j < (inlink->w >> hsub); j++)
136  memcpy(outrow + j*step, inrow - j*step, step);
137  }
138 
139  inrow += inpic ->linesize[plane];
140  outrow += outpic->linesize[plane];
141  }
142  }
143 
144  avfilter_draw_slice(inlink->dst->outputs[0], y, h, slice_dir);
145 }
146 
148  .name = "hflip",
149  .description = NULL_IF_CONFIG_SMALL("Horizontally flip the input video."),
150  .priv_size = sizeof(FlipContext),
152 
153  .inputs = (AVFilterPad[]) {{ .name = "default",
154  .type = AVMEDIA_TYPE_VIDEO,
155  .draw_slice = draw_slice,
156  .config_props = config_props,
157  .min_perms = AV_PERM_READ, },
158  { .name = NULL}},
159  .outputs = (AVFilterPad[]) {{ .name = "default",
160  .type = AVMEDIA_TYPE_VIDEO, },
161  { .name = NULL}},
162 };