gstvideoflip.c 33.8 KB
Newer Older
1 2 3
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 * Copyright (C) <2003> David Schleef <ds@schleef.org>
4
 * Copyright (C) <2010> Sebastian Dröge <sebastian.droege@collabora.co.uk>
5
 * Copyright (C) <2011> Youness Alaoui <youness.alaoui@collabora.co.uk>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23 24
/*
 * This file was (probably) generated from gstvideoflip.c,
25
 * gstvideoflip.c,v 1.7 2003/11/08 02:48:59 dschleef Exp 
26
 */
27 28 29 30
/**
 * SECTION:element-videoflip
 *
 * Flips and rotates video.
31 32
 *
 * <refsect2>
33
 * <title>Example launch line</title>
34
 * |[
35
 * gst-launch videotestsrc ! videoflip method=clockwise ! ffmpegcolorspace ! ximagesink
36
 * ]| This pipeline flips the test image 90 degrees clockwise.
37 38
 * </refsect2>
 *
39
 * Last reviewed on 2010-04-18 (0.10.22)
40 41
 */

42

43 44 45
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
46

47
#include "gstvideoflip.h"
48

49
#include <string.h>
50
#include <gst/gst.h>
Julien Moutte's avatar
Julien Moutte committed
51
#include <gst/video/video.h>
52

53
/* GstVideoFlip properties */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
54 55
enum
{
56 57
  PROP_0,
  PROP_METHOD
58
      /* FILL ME */
59 60
};

61
#define PROP_METHOD_DEFAULT GST_VIDEO_FLIP_METHOD_IDENTITY
62

63
GST_DEBUG_CATEGORY_STATIC (video_flip_debug);
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
64
#define GST_CAT_DEFAULT video_flip_debug
65

Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
66
static GstStaticPadTemplate gst_video_flip_src_template =
Wim Taymans's avatar
Wim Taymans committed
67
GST_STATIC_PAD_TEMPLATE ("src",
Julien Moutte's avatar
Julien Moutte committed
68 69
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
Wim Taymans's avatar
Wim Taymans committed
70 71
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
            "ARGB, BGRA, ABGR, RGBA, Y444, xRGB, RGBx,xBGR, BGRx, "
Wim Taymans's avatar
Wim Taymans committed
72
            "RGB, BGR, I420, YV12, IYUV, YUY2, UYVY, YVYU }"))
Julien Moutte's avatar
Julien Moutte committed
73
    );
74

Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
75
static GstStaticPadTemplate gst_video_flip_sink_template =
Wim Taymans's avatar
Wim Taymans committed
76
GST_STATIC_PAD_TEMPLATE ("sink",
Julien Moutte's avatar
Julien Moutte committed
77 78
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
Wim Taymans's avatar
Wim Taymans committed
79 80
    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("{ AYUV, "
            "ARGB, BGRA, ABGR, RGBA, Y444, xRGB, RGBx,xBGR, BGRx, "
Wim Taymans's avatar
Wim Taymans committed
81
            "RGB, BGR, I420, YV12, IYUV, YUY2, UYVY, YVYU }"))
Julien Moutte's avatar
Julien Moutte committed
82
    );
83

Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
84
#define GST_TYPE_VIDEO_FLIP_METHOD (gst_video_flip_method_get_type())
85

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static const GEnumValue video_flip_methods[] = {
  {GST_VIDEO_FLIP_METHOD_IDENTITY, "Identity (no rotation)", "none"},
  {GST_VIDEO_FLIP_METHOD_90R, "Rotate clockwise 90 degrees", "clockwise"},
  {GST_VIDEO_FLIP_METHOD_180, "Rotate 180 degrees", "rotate-180"},
  {GST_VIDEO_FLIP_METHOD_90L, "Rotate counter-clockwise 90 degrees",
      "counterclockwise"},
  {GST_VIDEO_FLIP_METHOD_HORIZ, "Flip horizontally", "horizontal-flip"},
  {GST_VIDEO_FLIP_METHOD_VERT, "Flip vertically", "vertical-flip"},
  {GST_VIDEO_FLIP_METHOD_TRANS,
      "Flip across upper left/lower right diagonal", "upper-left-diagonal"},
  {GST_VIDEO_FLIP_METHOD_OTHER,
      "Flip across upper right/lower left diagonal", "upper-right-diagonal"},
  {0, NULL, NULL},
};

101
static GType
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
102
gst_video_flip_method_get_type (void)
103
{
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
104
  static GType video_flip_method_type = 0;
105

Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
106 107 108
  if (!video_flip_method_type) {
    video_flip_method_type = g_enum_register_static ("GstVideoFlipMethod",
        video_flip_methods);
109
  }
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
110
  return video_flip_method_type;
111 112
}

Wim Taymans's avatar
Wim Taymans committed
113 114
#define gst_video_flip_parent_class parent_class
G_DEFINE_TYPE (GstVideoFlip, gst_video_flip, GST_TYPE_VIDEO_FILTER);
115

Julien Moutte's avatar
Julien Moutte committed
116
static GstCaps *
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
117
gst_video_flip_transform_caps (GstBaseTransform * trans,
118
    GstPadDirection direction, GstCaps * caps, GstCaps * filter)
Julien Moutte's avatar
Julien Moutte committed
119
{
120
  GstVideoFlip *videoflip = GST_VIDEO_FLIP (trans);
Julien Moutte's avatar
Julien Moutte committed
121 122 123 124 125 126 127
  GstCaps *ret;
  gint width, height, i;

  ret = gst_caps_copy (caps);

  for (i = 0; i < gst_caps_get_size (ret); i++) {
    GstStructure *structure = gst_caps_get_structure (ret, i);
128
    gint par_n, par_d;
Julien Moutte's avatar
Julien Moutte committed
129 130 131 132 133

    if (gst_structure_get_int (structure, "width", &width) &&
        gst_structure_get_int (structure, "height", &height)) {

      switch (videoflip->method) {
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
134 135 136 137
        case GST_VIDEO_FLIP_METHOD_90R:
        case GST_VIDEO_FLIP_METHOD_90L:
        case GST_VIDEO_FLIP_METHOD_TRANS:
        case GST_VIDEO_FLIP_METHOD_OTHER:
Julien Moutte's avatar
Julien Moutte committed
138 139
          gst_structure_set (structure, "width", G_TYPE_INT, height,
              "height", G_TYPE_INT, width, NULL);
140 141 142 143 144 145 146 147 148 149 150
          if (gst_structure_get_fraction (structure, "pixel-aspect-ratio",
                  &par_n, &par_d)) {
            if (par_n != 1 || par_d != 1) {
              GValue val = { 0, };

              g_value_init (&val, GST_TYPE_FRACTION);
              gst_value_set_fraction (&val, par_d, par_n);
              gst_structure_set_value (structure, "pixel-aspect-ratio", &val);
              g_value_unset (&val);
            }
          }
Julien Moutte's avatar
Julien Moutte committed
151
          break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
152 153 154 155
        case GST_VIDEO_FLIP_METHOD_IDENTITY:
        case GST_VIDEO_FLIP_METHOD_180:
        case GST_VIDEO_FLIP_METHOD_HORIZ:
        case GST_VIDEO_FLIP_METHOD_VERT:
Julien Moutte's avatar
Julien Moutte committed
156 157 158 159 160 161 162 163
          gst_structure_set (structure, "width", G_TYPE_INT, width,
              "height", G_TYPE_INT, height, NULL);
          break;
        default:
          g_assert_not_reached ();
          break;
      }
    }
164
  }
Julien Moutte's avatar
Julien Moutte committed
165 166 167 168

  GST_DEBUG_OBJECT (videoflip, "transformed %" GST_PTR_FORMAT " to %"
      GST_PTR_FORMAT, caps, ret);

169 170 171 172 173 174 175 176 177 178 179
  if (filter) {
    GstCaps *intersection;

    GST_DEBUG_OBJECT (videoflip, "Using filter caps %" GST_PTR_FORMAT, filter);
    intersection =
        gst_caps_intersect_full (filter, ret, GST_CAPS_INTERSECT_FIRST);
    gst_caps_unref (ret);
    ret = intersection;
    GST_DEBUG_OBJECT (videoflip, "Intersection %" GST_PTR_FORMAT, ret);
  }

Julien Moutte's avatar
Julien Moutte committed
180
  return ret;
181 182
}

183
static void
Wim Taymans's avatar
Wim Taymans committed
184 185
gst_video_flip_planar_yuv (GstVideoFlip * videoflip, GstVideoFrame * dest,
    const GstVideoFrame * src)
186
{
187 188 189 190 191 192 193 194 195 196
  gint x, y;
  guint8 const *s;
  guint8 *d;
  gint src_y_stride, src_u_stride, src_v_stride;
  gint src_y_height, src_u_height, src_v_height;
  gint src_y_width, src_u_width, src_v_width;
  gint dest_y_stride, dest_u_stride, dest_v_stride;
  gint dest_y_height, dest_u_height, dest_v_height;
  gint dest_y_width, dest_u_width, dest_v_width;

Wim Taymans's avatar
Wim Taymans committed
197 198 199
  src_y_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src, 0);
  src_u_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src, 1);
  src_v_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src, 2);
200

Wim Taymans's avatar
Wim Taymans committed
201 202 203
  dest_y_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest, 0);
  dest_u_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest, 1);
  dest_v_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest, 2);
204

Wim Taymans's avatar
Wim Taymans committed
205 206 207
  src_y_width = GST_VIDEO_FRAME_COMP_WIDTH (src, 0);
  src_u_width = GST_VIDEO_FRAME_COMP_WIDTH (src, 1);
  src_v_width = GST_VIDEO_FRAME_COMP_WIDTH (src, 2);
208

Wim Taymans's avatar
Wim Taymans committed
209 210 211
  dest_y_width = GST_VIDEO_FRAME_COMP_WIDTH (dest, 0);
  dest_u_width = GST_VIDEO_FRAME_COMP_WIDTH (dest, 1);
  dest_v_width = GST_VIDEO_FRAME_COMP_WIDTH (dest, 2);
212

Wim Taymans's avatar
Wim Taymans committed
213 214 215
  src_y_height = GST_VIDEO_FRAME_COMP_HEIGHT (src, 0);
  src_u_height = GST_VIDEO_FRAME_COMP_HEIGHT (src, 1);
  src_v_height = GST_VIDEO_FRAME_COMP_HEIGHT (src, 2);
216

Wim Taymans's avatar
Wim Taymans committed
217 218 219
  dest_y_height = GST_VIDEO_FRAME_COMP_HEIGHT (dest, 0);
  dest_u_height = GST_VIDEO_FRAME_COMP_HEIGHT (dest, 1);
  dest_v_height = GST_VIDEO_FRAME_COMP_HEIGHT (dest, 2);
Julien Moutte's avatar
Julien Moutte committed
220 221

  switch (videoflip->method) {
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
222
    case GST_VIDEO_FLIP_METHOD_90R:
223
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
224 225
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
226 227 228 229
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] =
              s[(src_y_height - 1 - x) * src_y_stride + y];
230 231 232
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
233 234
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
235 236 237 238
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] =
              s[(src_u_height - 1 - x) * src_u_stride + y];
239 240 241
        }
      }
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
242 243
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
244 245 246 247
      for (y = 0; y < dest_v_height; y++) {
        for (x = 0; x < dest_v_width; x++) {
          d[y * dest_v_stride + x] =
              s[(src_v_height - 1 - x) * src_v_stride + y];
Julien Moutte's avatar
Julien Moutte committed
248 249 250
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
251
    case GST_VIDEO_FLIP_METHOD_90L:
252
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
253 254
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
255 256 257 258
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] =
              s[x * src_y_stride + (src_y_width - 1 - y)];
259 260 261
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
262 263
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
264 265 266 267
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] =
              s[x * src_u_stride + (src_u_width - 1 - y)];
268 269 270
        }
      }
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
271 272
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
273 274 275 276
      for (y = 0; y < dest_v_height; y++) {
        for (x = 0; x < dest_v_width; x++) {
          d[y * dest_v_stride + x] =
              s[x * src_v_stride + (src_v_width - 1 - y)];
Julien Moutte's avatar
Julien Moutte committed
277 278 279
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
280
    case GST_VIDEO_FLIP_METHOD_180:
281
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
282 283
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
284 285 286 287
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] =
              s[(src_y_height - 1 - y) * src_y_stride + (src_y_width - 1 - x)];
288 289 290
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
291 292
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
293 294 295 296
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] =
              s[(src_u_height - 1 - y) * src_u_stride + (src_u_width - 1 - x)];
297 298 299
        }
      }
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
300 301
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
302 303 304 305
      for (y = 0; y < dest_v_height; y++) {
        for (x = 0; x < dest_v_width; x++) {
          d[y * dest_v_stride + x] =
              s[(src_v_height - 1 - y) * src_v_stride + (src_v_width - 1 - x)];
Julien Moutte's avatar
Julien Moutte committed
306 307 308
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
309
    case GST_VIDEO_FLIP_METHOD_HORIZ:
310
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
311 312
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
313 314 315 316
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] =
              s[y * src_y_stride + (src_y_width - 1 - x)];
317 318 319
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
320 321
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
322 323 324 325
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] =
              s[y * src_u_stride + (src_u_width - 1 - x)];
326 327 328
        }
      }
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
329 330
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
331 332 333 334
      for (y = 0; y < dest_v_height; y++) {
        for (x = 0; x < dest_v_width; x++) {
          d[y * dest_v_stride + x] =
              s[y * src_v_stride + (src_v_width - 1 - x)];
Julien Moutte's avatar
Julien Moutte committed
335 336 337
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
338
    case GST_VIDEO_FLIP_METHOD_VERT:
339
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
340 341
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
342 343 344 345
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] =
              s[(src_y_height - 1 - y) * src_y_stride + x];
346 347 348
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
349 350
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
351 352 353 354
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] =
              s[(src_u_height - 1 - y) * src_u_stride + x];
355 356 357
        }
      }
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
358 359
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
360 361 362 363
      for (y = 0; y < dest_v_height; y++) {
        for (x = 0; x < dest_v_width; x++) {
          d[y * dest_v_stride + x] =
              s[(src_v_height - 1 - y) * src_v_stride + x];
Julien Moutte's avatar
Julien Moutte committed
364 365 366
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
367
    case GST_VIDEO_FLIP_METHOD_TRANS:
368
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
369 370
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
371 372 373
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] = s[x * src_y_stride + y];
374 375 376
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
377 378
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
379 380 381
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] = s[x * src_u_stride + y];
382 383 384
        }
      }
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
385 386
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
387 388 389
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_v_stride + x] = s[x * src_v_stride + y];
Julien Moutte's avatar
Julien Moutte committed
390 391 392
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
393
    case GST_VIDEO_FLIP_METHOD_OTHER:
394
      /* Flip Y */
Wim Taymans's avatar
Wim Taymans committed
395 396
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);
397 398 399 400
      for (y = 0; y < dest_y_height; y++) {
        for (x = 0; x < dest_y_width; x++) {
          d[y * dest_y_stride + x] =
              s[(src_y_height - 1 - x) * src_y_stride + (src_y_width - 1 - y)];
401 402 403
        }
      }
      /* Flip U */
Wim Taymans's avatar
Wim Taymans committed
404 405
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 1);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 1);
406 407 408 409
      for (y = 0; y < dest_u_height; y++) {
        for (x = 0; x < dest_u_width; x++) {
          d[y * dest_u_stride + x] =
              s[(src_u_height - 1 - x) * src_u_stride + (src_u_width - 1 - y)];
Julien Moutte's avatar
Julien Moutte committed
410 411
        }
      }
412
      /* Flip V */
Wim Taymans's avatar
Wim Taymans committed
413 414
      s = GST_VIDEO_FRAME_PLANE_DATA (src, 2);
      d = GST_VIDEO_FRAME_PLANE_DATA (dest, 2);
415 416 417 418
      for (y = 0; y < dest_v_height; y++) {
        for (x = 0; x < dest_v_width; x++) {
          d[y * dest_v_stride + x] =
              s[(src_v_height - 1 - x) * src_v_stride + (src_v_width - 1 - y)];
419 420 421
        }
      }
      break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
422
    case GST_VIDEO_FLIP_METHOD_IDENTITY:
423
      g_assert_not_reached ();
Julien Moutte's avatar
Julien Moutte committed
424 425
      break;
    default:
426
      g_assert_not_reached ();
Julien Moutte's avatar
Julien Moutte committed
427 428
      break;
  }
429
}
430

431
static void
Wim Taymans's avatar
Wim Taymans committed
432 433
gst_video_flip_packed_simple (GstVideoFlip * videoflip, GstVideoFrame * dest,
    const GstVideoFrame * src)
434 435
{
  gint x, y, z;
Wim Taymans's avatar
Wim Taymans committed
436 437 438 439 440 441
  guint8 const *s;
  guint8 *d;
  gint sw = GST_VIDEO_FRAME_WIDTH (src);
  gint sh = GST_VIDEO_FRAME_HEIGHT (src);
  gint dw = GST_VIDEO_FRAME_WIDTH (dest);
  gint dh = GST_VIDEO_FRAME_HEIGHT (dest);
442 443 444
  gint src_stride, dest_stride;
  gint bpp;

Wim Taymans's avatar
Wim Taymans committed
445 446 447 448 449
  s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
  d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);

  src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src, 0);
  dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest, 0);
450
  /* This is only true for non-subsampled formats! */
Wim Taymans's avatar
Wim Taymans committed
451
  bpp = GST_VIDEO_FRAME_COMP_PSTRIDE (src, 0);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531

  switch (videoflip->method) {
    case GST_VIDEO_FLIP_METHOD_90R:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] =
                s[(sh - 1 - x) * src_stride + y * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_90L:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] =
                s[x * src_stride + (sw - 1 - y) * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_180:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] =
                s[(sh - 1 - y) * src_stride + (sw - 1 - x) * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_HORIZ:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] =
                s[y * src_stride + (sw - 1 - x) * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_VERT:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] =
                s[(sh - 1 - y) * src_stride + x * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_TRANS:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] = s[x * src_stride + y * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_OTHER:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x++) {
          for (z = 0; z < bpp; z++) {
            d[y * dest_stride + x * bpp + z] =
                s[(sh - 1 - x) * src_stride + (sw - 1 - y) * bpp + z];
          }
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_IDENTITY:
      g_assert_not_reached ();
      break;
    default:
      g_assert_not_reached ();
      break;
  }
}

532 533

static void
Wim Taymans's avatar
Wim Taymans committed
534 535
gst_video_flip_y422 (GstVideoFlip * videoflip, GstVideoFrame * dest,
    const GstVideoFrame * src)
536 537
{
  gint x, y;
Wim Taymans's avatar
Wim Taymans committed
538 539 540 541 542 543
  guint8 const *s;
  guint8 *d;
  gint sw = GST_VIDEO_FRAME_WIDTH (src);
  gint sh = GST_VIDEO_FRAME_HEIGHT (src);
  gint dw = GST_VIDEO_FRAME_WIDTH (dest);
  gint dh = GST_VIDEO_FRAME_HEIGHT (dest);
544 545 546 547 548 549 550
  gint src_stride, dest_stride;
  gint bpp;
  gint y_offset;
  gint u_offset;
  gint v_offset;
  gint y_stride;

Wim Taymans's avatar
Wim Taymans committed
551 552 553 554 555
  s = GST_VIDEO_FRAME_PLANE_DATA (src, 0);
  d = GST_VIDEO_FRAME_PLANE_DATA (dest, 0);

  src_stride = GST_VIDEO_FRAME_PLANE_STRIDE (src, 0);
  dest_stride = GST_VIDEO_FRAME_PLANE_STRIDE (dest, 0);
556

Wim Taymans's avatar
Wim Taymans committed
557 558 559 560
  y_offset = GST_VIDEO_FRAME_COMP_OFFSET (src, 0);
  u_offset = GST_VIDEO_FRAME_COMP_OFFSET (src, 1);
  v_offset = GST_VIDEO_FRAME_COMP_OFFSET (src, 2);
  y_stride = GST_VIDEO_FRAME_COMP_PSTRIDE (src, 0);
561 562 563 564 565 566 567 568 569 570 571
  bpp = y_stride;

  switch (videoflip->method) {
    case GST_VIDEO_FLIP_METHOD_90R:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_y = (y & ~1);

572 573 574 575 576 577 578 579 580
          u = s[(sh - 1 - x) * src_stride + even_y * bpp + u_offset];
          if (x + 1 < dw)
            u = (s[(sh - 1 - (x + 1)) * src_stride + even_y * bpp + u_offset]
                + u) >> 1;
          v = s[(sh - 1 - x) * src_stride + even_y * bpp + v_offset];
          if (x + 1 < dw)
            v = (s[(sh - 1 - (x + 1)) * src_stride + even_y * bpp + v_offset]
                + v) >> 1;

581 582 583 584
          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[(sh - 1 - x) * src_stride + y * bpp + y_offset];
585 586 587
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[(sh - 1 - (x + 1)) * src_stride + y * bpp + y_offset];
588 589 590 591 592 593 594 595 596 597 598
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_90L:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_y = ((sw - 1 - y) & ~1);

599 600 601 602 603 604
          u = s[x * src_stride + even_y * bpp + u_offset];
          if (x + 1 < dw)
            u = (s[(x + 1) * src_stride + even_y * bpp + u_offset] + u) >> 1;
          v = s[x * src_stride + even_y * bpp + v_offset];
          if (x + 1 < dw)
            v = (s[(x + 1) * src_stride + even_y * bpp + v_offset] + v) >> 1;
605 606 607 608 609

          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[x * src_stride + (sw - 1 - y) * bpp + y_offset];
610 611 612
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[(x + 1) * src_stride + (sw - 1 - y) * bpp + y_offset];
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_180:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_x = ((sw - 1 - x) & ~1);

          u = (s[(sh - 1 - y) * src_stride + even_x * bpp + u_offset] +
              s[(sh - 1 - y) * src_stride + even_x * bpp + u_offset]) / 2;
          v = (s[(sh - 1 - y) * src_stride + even_x * bpp + v_offset] +
              s[(sh - 1 - y) * src_stride + even_x * bpp + v_offset]) / 2;

          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[(sh - 1 - y) * src_stride + (sw - 1 - x) * bpp + y_offset];
633 634 635 636
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[(sh - 1 - y) * src_stride + (sw - 1 - (x + 1)) * bpp +
                y_offset];
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_HORIZ:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_x = ((sw - 1 - x) & ~1);

          u = (s[y * src_stride + even_x * bpp + u_offset] +
              s[y * src_stride + even_x * bpp + u_offset]) / 2;
          v = (s[y * src_stride + even_x * bpp + v_offset] +
              s[y * src_stride + even_x * bpp + v_offset]) / 2;

          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[y * src_stride + (sw - 1 - x) * bpp + y_offset];
657 658 659
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[y * src_stride + (sw - 1 - (x + 1)) * bpp + y_offset];
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_VERT:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_x = (x & ~1);

          u = (s[(sh - 1 - y) * src_stride + even_x * bpp + u_offset] +
              s[(sh - 1 - y) * src_stride + even_x * bpp + u_offset]) / 2;
          v = (s[(sh - 1 - y) * src_stride + even_x * bpp + v_offset] +
              s[(sh - 1 - y) * src_stride + even_x * bpp + v_offset]) / 2;

          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[(sh - 1 - y) * src_stride + x * bpp + y_offset];
680 681 682
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[(sh - 1 - y) * src_stride + (x + 1) * bpp + y_offset];
683 684 685 686 687 688 689 690 691 692 693
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_TRANS:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_y = (y & ~1);

694 695 696 697 698 699
          u = s[x * src_stride + even_y * bpp + u_offset];
          if (x + 1 < dw)
            u = (s[(x + 1) * src_stride + even_y * bpp + u_offset] + u) >> 1;
          v = s[x * src_stride + even_y * bpp + v_offset];
          if (x + 1 < dw)
            v = (s[(x + 1) * src_stride + even_y * bpp + v_offset] + v) >> 1;
700 701 702 703 704

          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[x * src_stride + y * bpp + y_offset];
705 706 707
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[(x + 1) * src_stride + y * bpp + y_offset];
708 709 710 711 712 713 714 715 716 717 718
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_OTHER:
      for (y = 0; y < dh; y++) {
        for (x = 0; x < dw; x += 2) {
          guint8 u;
          guint8 v;
          /* u/v must be calculated using the offset of the even column */
          gint even_y = ((sw - 1 - y) & ~1);

719 720 721 722 723 724 725 726
          u = s[(sh - 1 - x) * src_stride + even_y * bpp + u_offset];
          if (x + 1 < dw)
            u = (s[(sh - 1 - (x + 1)) * src_stride + even_y * bpp + u_offset]
                + u) >> 1;
          v = s[(sh - 1 - x) * src_stride + even_y * bpp + v_offset];
          if (x + 1 < dw)
            v = (s[(sh - 1 - (x + 1)) * src_stride + even_y * bpp + v_offset]
                + v) >> 1;
727 728 729 730 731

          d[y * dest_stride + x * bpp + u_offset] = u;
          d[y * dest_stride + x * bpp + v_offset] = v;
          d[y * dest_stride + x * bpp + y_offset] =
              s[(sh - 1 - x) * src_stride + (sw - 1 - y) * bpp + y_offset];
732 733 734 735
          if (x + 1 < dw)
            d[y * dest_stride + (x + 1) * bpp + y_offset] =
                s[(sh - 1 - (x + 1)) * src_stride + (sw - 1 - y) * bpp +
                y_offset];
736 737 738 739 740 741 742 743 744 745 746 747 748
        }
      }
      break;
    case GST_VIDEO_FLIP_METHOD_IDENTITY:
      g_assert_not_reached ();
      break;
    default:
      g_assert_not_reached ();
      break;
  }
}


749
static gboolean
Wim Taymans's avatar
Wim Taymans committed
750 751
gst_video_flip_set_info (GstVideoFilter * vfilter, GstCaps * incaps,
    GstVideoInfo * in_info, GstCaps * outcaps, GstVideoInfo * out_info)
752
{
Wim Taymans's avatar
Wim Taymans committed
753
  GstVideoFlip *vf = GST_VIDEO_FLIP (vfilter);
754 755 756 757
  gboolean ret = FALSE;

  vf->process = NULL;

Wim Taymans's avatar
Wim Taymans committed
758
  if (GST_VIDEO_INFO_FORMAT (in_info) != GST_VIDEO_INFO_FORMAT (out_info))
759 760 761 762 763 764 765 766
    goto invalid_caps;

  /* Check that they are correct */
  switch (vf->method) {
    case GST_VIDEO_FLIP_METHOD_90R:
    case GST_VIDEO_FLIP_METHOD_90L:
    case GST_VIDEO_FLIP_METHOD_TRANS:
    case GST_VIDEO_FLIP_METHOD_OTHER:
Wim Taymans's avatar
Wim Taymans committed
767 768
      if ((in_info->width != out_info->height) ||
          (in_info->height != out_info->width)) {
769
        GST_ERROR_OBJECT (vf, "we are inverting width and height but caps "
Wim Taymans's avatar
Wim Taymans committed
770 771
            "are not correct : %dx%d to %dx%d", in_info->width,
            in_info->height, out_info->width, out_info->height);
772 773 774 775 776 777 778 779 780
        goto beach;
      }
      break;
    case GST_VIDEO_FLIP_METHOD_IDENTITY:

      break;
    case GST_VIDEO_FLIP_METHOD_180:
    case GST_VIDEO_FLIP_METHOD_HORIZ:
    case GST_VIDEO_FLIP_METHOD_VERT:
Wim Taymans's avatar
Wim Taymans committed
781 782
      if ((in_info->width != out_info->width) ||
          (in_info->height != out_info->height)) {
783
        GST_ERROR_OBJECT (vf, "we are keeping width and height but caps "
Wim Taymans's avatar
Wim Taymans committed
784 785
            "are not correct : %dx%d to %dx%d", in_info->width,
            in_info->height, out_info->width, out_info->height);
786 787 788 789 790 791 792 793 794 795
        goto beach;
      }
      break;
    default:
      g_assert_not_reached ();
      break;
  }

  ret = TRUE;

Wim Taymans's avatar
Wim Taymans committed
796
  switch (GST_VIDEO_INFO_FORMAT (in_info)) {
797 798
    case GST_VIDEO_FORMAT_I420:
    case GST_VIDEO_FORMAT_YV12:
799
    case GST_VIDEO_FORMAT_Y444:
800 801
      vf->process = gst_video_flip_planar_yuv;
      break;
802 803 804 805 806
    case GST_VIDEO_FORMAT_YUY2:
    case GST_VIDEO_FORMAT_UYVY:
    case GST_VIDEO_FORMAT_YVYU:
      vf->process = gst_video_flip_y422;
      break;
807 808 809 810 811 812 813 814 815 816 817 818 819
    case GST_VIDEO_FORMAT_AYUV:
    case GST_VIDEO_FORMAT_ARGB:
    case GST_VIDEO_FORMAT_ABGR:
    case GST_VIDEO_FORMAT_RGBA:
    case GST_VIDEO_FORMAT_BGRA:
    case GST_VIDEO_FORMAT_xRGB:
    case GST_VIDEO_FORMAT_xBGR:
    case GST_VIDEO_FORMAT_RGBx:
    case GST_VIDEO_FORMAT_BGRx:
    case GST_VIDEO_FORMAT_RGB:
    case GST_VIDEO_FORMAT_BGR:
      vf->process = gst_video_flip_packed_simple;
      break;
820 821 822 823 824 825 826 827 828 829 830
    default:
      break;
  }

beach:
  return ret && (vf->process != NULL);

invalid_caps:
  GST_ERROR_OBJECT (vf, "Invalid caps: %" GST_PTR_FORMAT " -> %" GST_PTR_FORMAT,
      incaps, outcaps);
  return FALSE;
Julien Moutte's avatar
Julien Moutte committed
831
}
832

833 834
static void
gst_video_flip_before_transform (GstBaseTransform * trans, GstBuffer * in)
Julien Moutte's avatar
Julien Moutte committed
835
{
836
  GstVideoFlip *videoflip = GST_VIDEO_FLIP (trans);
837 838
  GstClockTime timestamp, stream_time;

839
  timestamp = GST_BUFFER_TIMESTAMP (in);
840 841 842 843 844 845 846
  stream_time =
      gst_segment_to_stream_time (&trans->segment, GST_FORMAT_TIME, timestamp);

  GST_DEBUG_OBJECT (videoflip, "sync to %" GST_TIME_FORMAT,
      GST_TIME_ARGS (timestamp));

  if (GST_CLOCK_TIME_IS_VALID (stream_time))
847
    gst_object_sync_values (GST_OBJECT (videoflip), stream_time);
848 849 850
}

static GstFlowReturn
Wim Taymans's avatar
Wim Taymans committed
851 852
gst_video_flip_transform_frame (GstVideoFilter * vfilter,
    GstVideoFrame * in_frame, GstVideoFrame * out_frame)
853
{
Wim Taymans's avatar
Wim Taymans committed
854
  GstVideoFlip *videoflip = GST_VIDEO_FLIP (vfilter);
855 856 857

  if (G_UNLIKELY (videoflip->process == NULL))
    goto not_negotiated;
858

Wim Taymans's avatar
Wim Taymans committed
859
  GST_LOG_OBJECT (videoflip, "videoflip: flipping (%s)",
Wim Taymans's avatar
Wim Taymans committed
860
      video_flip_methods[videoflip->method].value_nick);
Julien Moutte's avatar
Julien Moutte committed
861

862
  GST_OBJECT_LOCK (videoflip);
Wim Taymans's avatar
Wim Taymans committed
863
  videoflip->process (videoflip, out_frame, in_frame);
864
  GST_OBJECT_UNLOCK (videoflip);
865

866 867 868
  return GST_FLOW_OK;

not_negotiated:
Wim Taymans's avatar
Wim Taymans committed
869 870 871 872
  {
    GST_ERROR_OBJECT (videoflip, "Not negotiated yet");
    return GST_FLOW_NOT_NEGOTIATED;
  }
873 874
}

Julien Moutte's avatar
Julien Moutte committed
875
static gboolean
876
gst_video_flip_src_event (GstBaseTransform * trans, GstEvent * event)
877
{
878
  GstVideoFlip *vf = GST_VIDEO_FLIP (trans);
879
  gdouble new_x, new_y, x, y;
Julien Moutte's avatar
Julien Moutte committed
880
  GstStructure *structure;
881
  gboolean ret;
Wim Taymans's avatar
Wim Taymans committed
882
  GstVideoInfo *out_info = &GST_VIDEO_FILTER (trans)->out_info;
Julien Moutte's avatar
Julien Moutte committed
883 884 885 886 887 888 889 890 891 892 893

  GST_DEBUG_OBJECT (vf, "handling %s event", GST_EVENT_TYPE_NAME (event));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_NAVIGATION:
      event =
          GST_EVENT (gst_mini_object_make_writable (GST_MINI_OBJECT (event)));

      structure = (GstStructure *) gst_event_get_structure (event);
      if (gst_structure_get_double (structure, "pointer_x", &x) &&
          gst_structure_get_double (structure, "pointer_y", &y)) {
894
        GST_DEBUG_OBJECT (vf, "converting %fx%f", x, y);
Julien Moutte's avatar
Julien Moutte committed
895
        switch (vf->method) {
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
896
          case GST_VIDEO_FLIP_METHOD_90R:
897
            new_x = y;
Wim Taymans's avatar
Wim Taymans committed
898
            new_y = out_info->width - x;
Julien Moutte's avatar
Julien Moutte committed
899
            break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
900
          case GST_VIDEO_FLIP_METHOD_90L:
Wim Taymans's avatar
Wim Taymans committed
901
            new_x = out_info->height - y;
902 903
            new_y = x;
            break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
904
          case GST_VIDEO_FLIP_METHOD_OTHER:
Wim Taymans's avatar
Wim Taymans committed
905 906
            new_x = out_info->height - y;
            new_y = out_info->width - x;
907
            break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
908
          case GST_VIDEO_FLIP_METHOD_TRANS:
909 910
            new_x = y;
            new_y = x;
Julien Moutte's avatar
Julien Moutte committed
911
            break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
912
          case GST_VIDEO_FLIP_METHOD_180:
Wim Taymans's avatar
Wim Taymans committed
913 914
            new_x = out_info->width - x;
            new_y = out_info->height - y;
Julien Moutte's avatar
Julien Moutte committed
915
            break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
916
          case GST_VIDEO_FLIP_METHOD_HORIZ:
Wim Taymans's avatar
Wim Taymans committed
917
            new_x = out_info->width - x;
918
            new_y = y;
Julien Moutte's avatar
Julien Moutte committed
919
            break;
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
920
          case GST_VIDEO_FLIP_METHOD_VERT:
921
            new_x = x;
Wim Taymans's avatar
Wim Taymans committed
922
            new_y = out_info->height - y;
Julien Moutte's avatar
Julien Moutte committed
923 924
            break;
          default:
925 926
            new_x = x;
            new_y = y;
Julien Moutte's avatar
Julien Moutte committed
927 928
            break;
        }
929
        GST_DEBUG_OBJECT (vf, "to %fx%f", new_x, new_y);
930 931
        gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE, new_x,
            "pointer_y", G_TYPE_DOUBLE, new_y, NULL);
Julien Moutte's avatar
Julien Moutte committed
932 933 934 935 936
      }
      break;
    default:
      break;
  }
937

938 939 940
  ret = GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans, event);

  return ret;
941 942 943
}

static void
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
944
gst_video_flip_set_property (GObject * object, guint prop_id,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
945
    const GValue * value, GParamSpec * pspec)
946
{
947
  GstVideoFlip *videoflip = GST_VIDEO_FLIP (object);
948 949

  switch (prop_id) {
950
    case PROP_METHOD:
Julien Moutte's avatar
Julien Moutte committed
951
    {
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
952
      GstVideoFlipMethod method;
Julien Moutte's avatar
Julien Moutte committed
953 954

      method = g_value_get_enum (value);
955
      GST_OBJECT_LOCK (videoflip);
Julien Moutte's avatar
Julien Moutte committed
956 957 958
      if (method != videoflip->method) {
        GstBaseTransform *btrans = GST_BASE_TRANSFORM (videoflip);

959 960 961 962
        GST_DEBUG_OBJECT (videoflip, "Changing method from %s to %s",
            video_flip_methods[videoflip->method].value_nick,
            video_flip_methods[method].value_nick);

Julien Moutte's avatar
Julien Moutte committed
963
        videoflip->method = method;
964 965 966 967
        GST_OBJECT_UNLOCK (videoflip);

        gst_base_transform_set_passthrough (btrans,
            method == GST_VIDEO_FLIP_METHOD_IDENTITY);
Wim Taymans's avatar
Wim Taymans committed
968
        gst_base_transform_reconfigure_src (btrans);
969 970
      } else {
        GST_OBJECT_UNLOCK (videoflip);
971
      }
Julien Moutte's avatar
Julien Moutte committed
972
    }
973 974
      break;
    default:
Julien Moutte's avatar
Julien Moutte committed
975
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
976 977 978 979 980
      break;
  }
}

static void
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
981
gst_video_flip_get_property (GObject * object, guint prop_id, GValue * value,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
982
    GParamSpec * pspec)
983
{
984
  GstVideoFlip *videoflip = GST_VIDEO_FLIP (object);
985 986

  switch (prop_id) {
987
    case PROP_METHOD:
988
      g_value_set_enum (value, videoflip->method);
989 990 991 992 993 994 995
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

996
static void
997
gst_video_flip_class_init (GstVideoFlipClass * klass)
998
{
999
  GObjectClass *gobject_class = (GObjectClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
1000
  GstElementClass *gstelement_class = (GstElementClass *) klass;
1001
  GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
Wim Taymans's avatar
Wim Taymans committed
1002
  GstVideoFilterClass *vfilter_class = (GstVideoFilterClass *) klass;
1003

1004 1005
  GST_DEBUG_CATEGORY_INIT (video_flip_debug, "videoflip", 0, "videoflip");

Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
1006 1007
  gobject_class->set_property = gst_video_flip_set_property;
  gobject_class->get_property = gst_video_flip_get_property;
1008

1009
  g_object_class_install_property (gobject_class, PROP_METHOD,
Julien Moutte's avatar
Julien Moutte committed
1010
      g_param_spec_enum ("method", "method", "method",
1011
          GST_TYPE_VIDEO_FLIP_METHOD, PROP_METHOD_DEFAULT,
1012
          GST_PARAM_CONTROLLABLE | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1013

Wim Taymans's avatar
Wim Taymans committed
1014 1015 1016 1017 1018 1019 1020 1021 1022
  gst_element_class_set_details_simple (gstelement_class, "Video flipper",
      "Filter/Effect/Video",
      "Flips and rotates video", "David Schleef <ds@schleef.org>");

  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_video_flip_sink_template));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&gst_video_flip_src_template));

Julien Moutte's avatar
Julien Moutte committed
1023
  trans_class->transform_caps =
Thomas Vander Stichele's avatar
borgify  
Thomas Vander Stichele committed
1024
      GST_DEBUG_FUNCPTR (gst_video_flip_transform_caps);
1025 1026
  trans_class->before_transform =
      GST_DEBUG_FUNCPTR (gst_video_flip_before_transform);
1027
  trans_class->src_event = GST_DEBUG_FUNCPTR (gst_video_flip_src_event);
Wim Taymans's avatar
Wim Taymans committed
1028 1029 1030 1031

  vfilter_class->set_info = GST_DEBUG_FUNCPTR (gst_video_flip_set_info);
  vfilter_class->transform_frame =
      GST_DEBUG_FUNCPTR (gst_video_flip_transform_frame);
1032 1033
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1034
static void
Wim Taymans's avatar
Wim Taymans committed
1035
gst_video_flip_init (GstVideoFlip * videoflip)
1036
{
1037
  videoflip->method = PROP_METHOD_DEFAULT;
1038
  gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (videoflip), TRUE);
1039
}