gstvideocrop.c 23.2 KB
Newer Older
1 2
/* GStreamer video frame cropping
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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.
 */

20 21
/**
 * SECTION:element-videocrop
22
 * @see_also: #GstVideoBox
23 24 25 26 27
 *
 * This element crops video frames, meaning it can remove parts of the
 * picture on the left, right, top or bottom of the picture and output
 * a smaller picture than the input picture, with the unwanted parts at the
 * border removed.
28
 *
29 30 31 32
 * The videocrop element is similar to the videobox element, but its main
 * goal is to support a multitude of formats as efficiently as possible.
 * Unlike videbox, it cannot add borders to the picture and unlike videbox
 * it will always output images in exactly the same format as the input image.
33
 *
34
 * If there is nothing to crop, the element will operate in pass-through mode.
35
 *
36 37
 * Note that no special efforts are made to handle chroma-subsampled formats
 * in the case of odd-valued cropping and compensate for sub-unit chroma plane
38 39 40 41 42
 * shifts for such formats in the case where the #GstVideoCrop:left or
 * #GstVideoCrop:top property is set to an odd number. This doesn't matter for 
 * most use cases, but it might matter for yours.
 *
 * <refsect2>
43
 * <title>Example launch line</title>
44
 * |[
45
 * gst-launch -v videotestsrc ! videocrop top=42 left=1 right=4 bottom=0 ! ximagesink
46
 * ]|
47 48 49 50 51 52 53 54 55
 * </refsect2>
 */

/* TODO:
 *  - for packed formats, we could avoid memcpy() in case crop_left
 *    and crop_right are 0 and just create a sub-buffer of the input
 *    buffer
 */

56 57 58
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
59

60
#include <gst/gst.h>
61
#include <gst/video/video.h>
62 63

#include "gstvideocrop.h"
64
#include "gstaspectratiocrop.h"
65

66 67
#include <string.h>

68 69 70
GST_DEBUG_CATEGORY_STATIC (videocrop_debug);
#define GST_CAT_DEFAULT videocrop_debug

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
71 72
enum
{
73
  ARG_0,
74 75 76
  ARG_LEFT,
  ARG_RIGHT,
  ARG_TOP,
77
  ARG_BOTTOM
78 79
};

Thiago Santos's avatar
Thiago Santos committed
80 81 82 83
#define VIDEO_CROP_CAPS                                \
  GST_VIDEO_CAPS_MAKE ("{ RGBx, xRGB, BGRx, xBGR, "    \
      "RGBA, ARGB, BGRA, ABGR, RGB, BGR, AYUV, YUY2, " \
      "YVYU, UYVY, Y800, I420, RGB16, RGB15, GRAY8 }")
84 85

static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
86 87
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
88
    GST_STATIC_CAPS (VIDEO_CROP_CAPS)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89
    );
David Schleef's avatar
David Schleef committed
90

91
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
92 93
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
94
    GST_STATIC_CAPS (VIDEO_CROP_CAPS)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
95
    );
96

Thiago Santos's avatar
Thiago Santos committed
97 98
#define gst_video_crop_parent_class parent_class
G_DEFINE_TYPE (GstVideoCrop, gst_video_crop, GST_TYPE_BASE_TRANSFORM);
99

100 101
static void gst_video_crop_finalize (GObject * object);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
102 103 104 105
static void gst_video_crop_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_video_crop_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
106

107
static GstCaps *gst_video_crop_transform_caps (GstBaseTransform * trans,
Thiago Santos's avatar
Thiago Santos committed
108
    GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps);
109 110 111
static GstFlowReturn gst_video_crop_transform (GstBaseTransform * trans,
    GstBuffer * inbuf, GstBuffer * outbuf);
static gboolean gst_video_crop_get_unit_size (GstBaseTransform * trans,
Wim Taymans's avatar
Wim Taymans committed
112
    GstCaps * caps, gsize * size);
113 114
static gboolean gst_video_crop_set_caps (GstBaseTransform * trans,
    GstCaps * in_caps, GstCaps * outcaps);
115 116
static gboolean gst_video_crop_src_event (GstBaseTransform * trans,
    GstEvent * event);
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
static gboolean
gst_video_crop_src_event (GstBaseTransform * trans, GstEvent * event)
{
  GstEvent *new_event;
  GstStructure *new_structure;
  const GstStructure *structure;
  const gchar *event_name;
  double pointer_x;
  double pointer_y;

  GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
  new_event = NULL;

  GST_OBJECT_LOCK (vcrop);
  if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION &&
      (vcrop->crop_left != 0 || vcrop->crop_top != 0)) {
    structure = gst_event_get_structure (event);
    event_name = gst_structure_get_string (structure, "event");

    if (event_name &&
138 139 140
        (strcmp (event_name, "mouse-move") == 0 ||
            strcmp (event_name, "mouse-button-press") == 0 ||
            strcmp (event_name, "mouse-button-release") == 0)) {
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

      if (gst_structure_get_double (structure, "pointer_x", &pointer_x) &&
          gst_structure_get_double (structure, "pointer_y", &pointer_y)) {

        new_structure = gst_structure_copy (structure);
        gst_structure_set (new_structure,
            "pointer_x", G_TYPE_DOUBLE, (double) (pointer_x + vcrop->crop_left),
            "pointer_y", G_TYPE_DOUBLE, (double) (pointer_y + vcrop->crop_top),
            NULL);

        new_event = gst_event_new_navigation (new_structure);
        gst_event_unref (event);
      } else {
        GST_WARNING_OBJECT (vcrop, "Failed to read navigation event");
      }
    }
  }

  GST_OBJECT_UNLOCK (vcrop);
160

161 162 163 164
  return GST_BASE_TRANSFORM_CLASS (parent_class)->src_event (trans,
      (new_event ? new_event : event));
}

165
static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
166
gst_video_crop_class_init (GstVideoCropClass * klass)
167 168
{
  GObjectClass *gobject_class;
Thiago Santos's avatar
Thiago Santos committed
169
  GstElementClass *element_class;
170
  GstBaseTransformClass *basetransform_class;
171

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
172
  gobject_class = (GObjectClass *) klass;
Thiago Santos's avatar
Thiago Santos committed
173
  element_class = (GstElementClass *) klass;
174
  basetransform_class = (GstBaseTransformClass *) klass;
175

176
  gobject_class->finalize = gst_video_crop_finalize;
177 178
  gobject_class->set_property = gst_video_crop_set_property;
  gobject_class->get_property = gst_video_crop_get_property;
179

180
  g_object_class_install_property (gobject_class, ARG_LEFT,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
181
      g_param_spec_int ("left", "Left", "Pixels to crop at left",
182
          0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
  g_object_class_install_property (gobject_class, ARG_RIGHT,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
184
      g_param_spec_int ("right", "Right", "Pixels to crop at right",
185
          0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186
  g_object_class_install_property (gobject_class, ARG_TOP,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
187
      g_param_spec_int ("top", "Top", "Pixels to crop at top",
188
          0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189
  g_object_class_install_property (gobject_class, ARG_BOTTOM,
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
190
      g_param_spec_int ("bottom", "Bottom", "Pixels to crop at bottom",
191
          0, G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192

193 194 195 196 197 198 199 200 201
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_template));
  gst_element_class_set_details_simple (element_class, "Crop",
      "Filter/Effect/Video",
      "Crops video into a user-defined region",
      "Tim-Philipp Müller <tim centricular net>");

202 203 204 205 206 207 208
  basetransform_class->transform = GST_DEBUG_FUNCPTR (gst_video_crop_transform);
  basetransform_class->transform_caps =
      GST_DEBUG_FUNCPTR (gst_video_crop_transform_caps);
  basetransform_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_crop_set_caps);
  basetransform_class->get_unit_size =
      GST_DEBUG_FUNCPTR (gst_video_crop_get_unit_size);

209
  basetransform_class->passthrough_on_same_caps = FALSE;
210
  basetransform_class->src_event = GST_DEBUG_FUNCPTR (gst_video_crop_src_event);
211 212 213
}

static void
Thiago Santos's avatar
Thiago Santos committed
214
gst_video_crop_init (GstVideoCrop * vcrop)
215
{
216 217 218 219
  vcrop->crop_right = 0;
  vcrop->crop_left = 0;
  vcrop->crop_top = 0;
  vcrop->crop_bottom = 0;
220 221 222 223 224 225 226 227 228 229 230 231 232 233

  g_mutex_init (&vcrop->lock);
}

static void
gst_video_crop_finalize (GObject * object)
{
  GstVideoCrop *vcrop;

  vcrop = GST_VIDEO_CROP (object);

  g_mutex_clear (&vcrop->lock);

  G_OBJECT_CLASS (parent_class)->finalize (object);
234 235
}

236 237 238
static gboolean
gst_video_crop_get_image_details_from_caps (GstVideoCrop * vcrop,
    GstVideoCropImageDetails * details, GstCaps * caps)
239
{
Thiago Santos's avatar
Thiago Santos committed
240 241
  gst_video_info_init (&details->info);
  if (!gst_video_info_from_caps (&details->info, caps)) {
242 243
    goto incomplete_format;
  }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
244

Thiago Santos's avatar
Thiago Santos committed
245 246 247
  if (details->info.width == 0 && details->info.height == 0) {
    goto incomplete_format;
  }
248

Thiago Santos's avatar
Thiago Santos committed
249 250
  if (GST_VIDEO_INFO_IS_RGB (&details->info)
      || GST_VIDEO_INFO_IS_GRAY (&details->info)) {
251
    details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
Thiago Santos's avatar
Thiago Santos committed
252 253 254
  } else {
    switch (GST_VIDEO_INFO_FORMAT (&details->info)) {
      case GST_VIDEO_FORMAT_AYUV:
255
        details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
256
        break;
Thiago Santos's avatar
Thiago Santos committed
257 258 259
      case GST_VIDEO_FORMAT_YVYU:
      case GST_VIDEO_FORMAT_YUY2:
      case GST_VIDEO_FORMAT_UYVY:
260
        details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX;
Thiago Santos's avatar
Thiago Santos committed
261
        if (GST_VIDEO_INFO_FORMAT (&details->info) == GST_VIDEO_FORMAT_UYVY) {
262
          /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5] */
263 264
          details->macro_y_off = 1;
        } else {
265
          /* YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
266 267
          details->macro_y_off = 0;
        }
268
        break;
Thiago Santos's avatar
Thiago Santos committed
269
      case GST_VIDEO_FORMAT_Y800:
270
        details->packing = VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE;
271
        break;
Thiago Santos's avatar
Thiago Santos committed
272 273
      case GST_VIDEO_FORMAT_I420:
      case GST_VIDEO_FORMAT_YV12:
274
        details->packing = VIDEO_CROP_PIXEL_FORMAT_PLANAR;
275 276 277 278 279
        break;
      default:
        goto unknown_format;
    }
  }
280

281 282 283 284 285 286 287 288
  return TRUE;

  /* ERRORS */
unknown_format:
  {
    GST_ELEMENT_ERROR (vcrop, STREAM, NOT_IMPLEMENTED, (NULL),
        ("Unsupported format"));
    return FALSE;
289
  }
290 291 292 293 294 295 296 297 298 299 300

incomplete_format:
  {
    GST_ELEMENT_ERROR (vcrop, CORE, NEGOTIATION, (NULL),
        ("Incomplete caps, some required field is missing"));
    return FALSE;
  }
}

static gboolean
gst_video_crop_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
Wim Taymans's avatar
Wim Taymans committed
301
    gsize * size)
302 303 304 305 306 307 308
{
  GstVideoCropImageDetails img_details = { 0, };
  GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);

  if (!gst_video_crop_get_image_details_from_caps (vcrop, &img_details, caps))
    return FALSE;

Thiago Santos's avatar
Thiago Santos committed
309
  *size = GST_VIDEO_INFO_SIZE (&img_details.info);
310
  return TRUE;
311
}
312

313 314
#define ROUND_DOWN_2(n)  ((n)&(~1))

315
static void
316 317 318
gst_video_crop_transform_packed_complex (GstVideoCrop * vcrop,
    GstBuffer * inbuf, GstBuffer * outbuf)
{
Wim Taymans's avatar
Wim Taymans committed
319
  GstMapInfo in_map, out_map;
320 321
  guint8 *in_data, *out_data;
  guint i, dx;
Thiago Santos's avatar
Thiago Santos committed
322 323
  gint in_stride;
  gint out_stride;
324

Wim Taymans's avatar
Wim Taymans committed
325 326
  gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
  gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
327

Wim Taymans's avatar
Wim Taymans committed
328 329
  in_data = in_map.data;
  out_data = out_map.data;
Thiago Santos's avatar
Thiago Santos committed
330 331 332 333 334

  in_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
  out_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);

  in_data += vcrop->crop_top * in_stride;
335 336 337

  /* rounding down here so we end up at the start of a macro-pixel and not
   * in the middle of one */
Thiago Santos's avatar
Thiago Santos committed
338 339 340
  in_data +=
      ROUND_DOWN_2 (vcrop->crop_left) *
      GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->in.info, 0);
341

Thiago Santos's avatar
Thiago Santos committed
342 343
  dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) *
      GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->out.info, 0);
344

345 346
  /* UYVY = 4:2:2 - [U0 Y0 V0 Y1] [U2 Y2 V2 Y3] [U4 Y4 V4 Y5]
   * YUYV = 4:2:2 - [Y0 U0 Y1 V0] [Y2 U2 Y3 V2] [Y4 U4 Y5 V4] = YUY2 */
347
  if ((vcrop->crop_left % 2) != 0) {
Thiago Santos's avatar
Thiago Santos committed
348
    for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
349 350 351 352
      gint j;

      memcpy (out_data, in_data, dx);

353 354
      /* move just the Y samples one pixel to the left, don't worry about
       * chroma shift */
Thiago Santos's avatar
Thiago Santos committed
355
      for (j = vcrop->in.macro_y_off; j < out_stride - 2; j += 2)
356
        out_data[j] = in_data[j + 2];
357

Thiago Santos's avatar
Thiago Santos committed
358 359
      in_data += in_stride;
      out_data += out_stride;
360 361
    }
  } else {
Thiago Santos's avatar
Thiago Santos committed
362
    for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
363
      memcpy (out_data, in_data, dx);
Thiago Santos's avatar
Thiago Santos committed
364 365
      in_data += in_stride;
      out_data += out_stride;
366 367
    }
  }
Wim Taymans's avatar
Wim Taymans committed
368 369
  gst_buffer_unmap (inbuf, &in_map);
  gst_buffer_unmap (outbuf, &out_map);
370 371 372 373 374
}

static void
gst_video_crop_transform_packed_simple (GstVideoCrop * vcrop,
    GstBuffer * inbuf, GstBuffer * outbuf)
375
{
Wim Taymans's avatar
Wim Taymans committed
376
  GstMapInfo in_map, out_map;
377 378
  guint8 *in_data, *out_data;
  guint i, dx;
Thiago Santos's avatar
Thiago Santos committed
379 380
  gint in_stride, out_stride;

Wim Taymans's avatar
Wim Taymans committed
381 382
  gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
  gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
383

Wim Taymans's avatar
Wim Taymans committed
384 385
  in_data = in_map.data;
  out_data = out_map.data;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
386

Thiago Santos's avatar
Thiago Santos committed
387 388
  in_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
  out_stride = GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
389

Thiago Santos's avatar
Thiago Santos committed
390 391 392
  in_data += vcrop->crop_top * in_stride;
  in_data +=
      vcrop->crop_left * GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->in.info, 0);
393

Thiago Santos's avatar
Thiago Santos committed
394 395 396 397
  dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) *
      GST_VIDEO_INFO_COMP_PSTRIDE (&vcrop->out.info, 0);

  for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
398
    memcpy (out_data, in_data, dx);
Thiago Santos's avatar
Thiago Santos committed
399 400
    in_data += in_stride;
    out_data += out_stride;
401
  }
Wim Taymans's avatar
Wim Taymans committed
402 403
  gst_buffer_unmap (inbuf, &in_map);
  gst_buffer_unmap (outbuf, &out_map);
404 405 406
}

static void
407 408
gst_video_crop_transform_planar (GstVideoCrop * vcrop, GstBuffer * inbuf,
    GstBuffer * outbuf)
409
{
Wim Taymans's avatar
Wim Taymans committed
410
  GstMapInfo in_map, out_map;
411 412 413
  guint8 *y_out, *u_out, *v_out;
  guint8 *y_in, *u_in, *v_in;
  guint i, dx;
Thiago Santos's avatar
Thiago Santos committed
414

Wim Taymans's avatar
Wim Taymans committed
415 416
  gst_buffer_map (inbuf, &in_map, GST_MAP_READ);
  gst_buffer_map (outbuf, &out_map, GST_MAP_WRITE);
417

418
  /* Y plane */
Wim Taymans's avatar
Wim Taymans committed
419 420
  y_in = in_map.data;
  y_out = out_map.data;
421

Thiago Santos's avatar
Thiago Santos committed
422 423 424 425
  y_in +=
      (vcrop->crop_top * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info,
          0)) + vcrop->crop_left;
  dx = GST_VIDEO_INFO_WIDTH (&vcrop->out.info) * 1;
426

Thiago Santos's avatar
Thiago Santos committed
427
  for (i = 0; i < GST_VIDEO_INFO_HEIGHT (&vcrop->out.info); ++i) {
428
    memcpy (y_out, y_in, dx);
Thiago Santos's avatar
Thiago Santos committed
429 430
    y_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 0);
    y_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 0);
431 432
  }

433
  /* U + V planes */
Thiago Santos's avatar
Thiago Santos committed
434
  u_in =
Wim Taymans's avatar
Wim Taymans committed
435
      (guint8 *) in_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->in.info, 1);
Thiago Santos's avatar
Thiago Santos committed
436
  u_out =
Wim Taymans's avatar
Wim Taymans committed
437
      (guint8 *) out_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->out.info,
Thiago Santos's avatar
Thiago Santos committed
438 439 440 441
      1);

  u_in +=
      (vcrop->crop_top / 2) * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 1);
442
  u_in += vcrop->crop_left / 2;
443

Thiago Santos's avatar
Thiago Santos committed
444
  v_in =
Wim Taymans's avatar
Wim Taymans committed
445
      (guint8 *) in_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->in.info, 2);
Thiago Santos's avatar
Thiago Santos committed
446
  v_out =
Wim Taymans's avatar
Wim Taymans committed
447
      (guint8 *) out_map.data + GST_VIDEO_INFO_PLANE_OFFSET (&vcrop->out.info,
Thiago Santos's avatar
Thiago Santos committed
448
      2);
449

Thiago Santos's avatar
Thiago Santos committed
450 451
  v_in +=
      (vcrop->crop_top / 2) * GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 2);
452
  v_in += vcrop->crop_left / 2;
453

Thiago Santos's avatar
Thiago Santos committed
454
  dx = GST_ROUND_UP_2 (GST_VIDEO_INFO_WIDTH (&vcrop->out.info)) / 2;
455

Thiago Santos's avatar
Thiago Santos committed
456 457
  for (i = 0; i < GST_ROUND_UP_2 (GST_VIDEO_INFO_HEIGHT (&vcrop->out.info)) / 2;
      ++i) {
458 459
    memcpy (u_out, u_in, dx);
    memcpy (v_out, v_in, dx);
Thiago Santos's avatar
Thiago Santos committed
460 461 462 463
    u_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 1);
    u_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 1);
    v_in += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->in.info, 2);
    v_out += GST_VIDEO_INFO_PLANE_STRIDE (&vcrop->out.info, 2);
464
  }
Thiago Santos's avatar
Thiago Santos committed
465

Wim Taymans's avatar
Wim Taymans committed
466 467
  gst_buffer_unmap (inbuf, &in_map);
  gst_buffer_unmap (outbuf, &out_map);
468 469
}

470 471 472
static GstFlowReturn
gst_video_crop_transform (GstBaseTransform * trans, GstBuffer * inbuf,
    GstBuffer * outbuf)
473
{
474
  GstVideoCrop *vcrop = GST_VIDEO_CROP (trans);
475

476
  g_mutex_lock (&vcrop->lock);
477 478 479 480 481 482 483 484 485 486 487 488
  switch (vcrop->in.packing) {
    case VIDEO_CROP_PIXEL_FORMAT_PACKED_SIMPLE:
      gst_video_crop_transform_packed_simple (vcrop, inbuf, outbuf);
      break;
    case VIDEO_CROP_PIXEL_FORMAT_PACKED_COMPLEX:
      gst_video_crop_transform_packed_complex (vcrop, inbuf, outbuf);
      break;
    case VIDEO_CROP_PIXEL_FORMAT_PLANAR:
      gst_video_crop_transform_planar (vcrop, inbuf, outbuf);
      break;
    default:
      g_assert_not_reached ();
489
  }
490
  g_mutex_unlock (&vcrop->lock);
491

492
  return GST_FLOW_OK;
493 494
}

495 496
static gint
gst_video_crop_transform_dimension (gint val, gint delta)
497
{
498
  gint64 new_val = (gint64) val + (gint64) delta;
499

500
  new_val = CLAMP (new_val, 1, G_MAXINT);
501

502 503
  return (gint) new_val;
}
504

505 506 507 508 509
static gboolean
gst_video_crop_transform_dimension_value (const GValue * src_val,
    gint delta, GValue * dest_val)
{
  gboolean ret = TRUE;
510

511
  g_value_init (dest_val, G_VALUE_TYPE (src_val));
512

513 514
  if (G_VALUE_HOLDS_INT (src_val)) {
    gint ival = g_value_get_int (src_val);
515

516 517 518 519 520
    ival = gst_video_crop_transform_dimension (ival, delta);
    g_value_set_int (dest_val, ival);
  } else if (GST_VALUE_HOLDS_INT_RANGE (src_val)) {
    gint min = gst_value_get_int_range_min (src_val);
    gint max = gst_value_get_int_range_max (src_val);
521

522 523 524 525 526
    min = gst_video_crop_transform_dimension (min, delta);
    max = gst_video_crop_transform_dimension (max, delta);
    gst_value_set_int_range (dest_val, min, max);
  } else if (GST_VALUE_HOLDS_LIST (src_val)) {
    gint i;
527

528 529 530
    for (i = 0; i < gst_value_list_get_size (src_val); ++i) {
      const GValue *list_val;
      GValue newval = { 0, };
531

532 533 534 535 536
      list_val = gst_value_list_get_value (src_val, i);
      if (gst_video_crop_transform_dimension_value (list_val, delta, &newval))
        gst_value_list_append_value (dest_val, &newval);
      g_value_unset (&newval);
    }
537

538 539 540 541 542 543 544
    if (gst_value_list_get_size (dest_val) == 0) {
      g_value_unset (dest_val);
      ret = FALSE;
    }
  } else {
    g_value_unset (dest_val);
    ret = FALSE;
545 546
  }

547
  return ret;
548 549
}

Thiago Santos's avatar
Thiago Santos committed
550
/* TODO use filter_caps */
551 552
static GstCaps *
gst_video_crop_transform_caps (GstBaseTransform * trans,
Thiago Santos's avatar
Thiago Santos committed
553
    GstPadDirection direction, GstCaps * caps, GstCaps * filter_caps)
554
{
555 556 557
  GstVideoCrop *vcrop;
  GstCaps *other_caps;
  gint dy, dx, i;
558

559
  vcrop = GST_VIDEO_CROP (trans);
560

561 562
  GST_OBJECT_LOCK (vcrop);

563 564
  GST_LOG_OBJECT (vcrop, "l=%d,r=%d,b=%d,t=%d",
      vcrop->crop_left, vcrop->crop_right, vcrop->crop_bottom, vcrop->crop_top);
565 566 567 568 569 570 571 572 573

  if (direction == GST_PAD_SRC) {
    dx = vcrop->crop_left + vcrop->crop_right;
    dy = vcrop->crop_top + vcrop->crop_bottom;
  } else {
    dx = 0 - (vcrop->crop_left + vcrop->crop_right);
    dy = 0 - (vcrop->crop_top + vcrop->crop_bottom);
  }
  GST_OBJECT_UNLOCK (vcrop);
574

575
  GST_LOG_OBJECT (vcrop, "transforming caps %" GST_PTR_FORMAT, caps);
576

577
  other_caps = gst_caps_new_empty ();
578

579 580 581 582 583
  for (i = 0; i < gst_caps_get_size (caps); ++i) {
    const GValue *v;
    GstStructure *structure, *new_structure;
    GValue w_val = { 0, }, h_val = {
    0,};
584

585 586 587 588 589 590 591
    structure = gst_caps_get_structure (caps, i);

    v = gst_structure_get_value (structure, "width");
    if (!gst_video_crop_transform_dimension_value (v, dx, &w_val)) {
      GST_WARNING_OBJECT (vcrop, "could not tranform width value with dx=%d"
          ", caps structure=%" GST_PTR_FORMAT, dx, structure);
      continue;
592
    }
593

594 595 596 597 598 599 600
    v = gst_structure_get_value (structure, "height");
    if (!gst_video_crop_transform_dimension_value (v, dy, &h_val)) {
      g_value_unset (&w_val);
      GST_WARNING_OBJECT (vcrop, "could not tranform height value with dy=%d"
          ", caps structure=%" GST_PTR_FORMAT, dy, structure);
      continue;
    }
601

602 603 604 605 606 607 608 609
    new_structure = gst_structure_copy (structure);
    gst_structure_set_value (new_structure, "width", &w_val);
    gst_structure_set_value (new_structure, "height", &h_val);
    g_value_unset (&w_val);
    g_value_unset (&h_val);
    GST_LOG_OBJECT (vcrop, "transformed structure %2d: %" GST_PTR_FORMAT
        " => %" GST_PTR_FORMAT, i, structure, new_structure);
    gst_caps_append_structure (other_caps, new_structure);
610 611
  }

612 613 614
  if (gst_caps_is_empty (other_caps)) {
    gst_caps_unref (other_caps);
    other_caps = NULL;
615 616
  }

Thiago Santos's avatar
Thiago Santos committed
617 618 619 620 621 622 623
  if (other_caps && filter_caps) {
    GstCaps *tmp = gst_caps_intersect_full (filter_caps, other_caps,
        GST_CAPS_INTERSECT_FIRST);
    gst_caps_replace (&other_caps, tmp);
    gst_caps_unref (tmp);
  }

624 625
  return other_caps;
}
626

627 628 629 630 631
static gboolean
gst_video_crop_set_caps (GstBaseTransform * trans, GstCaps * incaps,
    GstCaps * outcaps)
{
  GstVideoCrop *crop = GST_VIDEO_CROP (trans);
632

633 634 635 636 637 638
  if (!gst_video_crop_get_image_details_from_caps (crop, &crop->in, incaps))
    goto wrong_input;

  if (!gst_video_crop_get_image_details_from_caps (crop, &crop->out, outcaps))
    goto wrong_output;

Thiago Santos's avatar
Thiago Santos committed
639 640 641 642
  if (G_UNLIKELY ((crop->crop_left + crop->crop_right) >=
          GST_VIDEO_INFO_WIDTH (&crop->in.info)
          || (crop->crop_top + crop->crop_bottom) >=
          GST_VIDEO_INFO_HEIGHT (&crop->in.info)))
643 644 645 646 647
    goto cropping_too_much;

  GST_LOG_OBJECT (crop, "incaps = %" GST_PTR_FORMAT ", outcaps = %"
      GST_PTR_FORMAT, incaps, outcaps);

648 649
  if ((crop->crop_left | crop->crop_right | crop->
          crop_top | crop->crop_bottom) == 0) {
650 651 652 653 654 655 656 657 658 659 660 661
    GST_LOG_OBJECT (crop, "we are using passthrough");
    gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), TRUE);
  } else {
    GST_LOG_OBJECT (crop, "we are not using passthrough");
    gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (crop), FALSE);
  }

  return TRUE;

  /* ERROR */
wrong_input:
  {
662 663 664 665
    GST_DEBUG_OBJECT (crop, "failed to parse input caps %" GST_PTR_FORMAT,
        incaps);
    return FALSE;
  }
666 667
wrong_output:
  {
668 669 670 671
    GST_DEBUG_OBJECT (crop, "failed to parse output caps %" GST_PTR_FORMAT,
        outcaps);
    return FALSE;
  }
672 673 674 675 676
cropping_too_much:
  {
    GST_DEBUG_OBJECT (crop, "we are cropping too much");
    return FALSE;
  }
677 678
}

679 680 681
static void
gst_video_crop_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
682 683 684
{
  GstVideoCrop *video_crop;

685
  video_crop = GST_VIDEO_CROP (object);
686

687
  /* don't modify while we are transforming */
688
  g_mutex_lock (&video_crop->lock);
689 690

  /* protect with the object lock so that we can read them */
691 692 693 694
  GST_OBJECT_LOCK (video_crop);
  switch (prop_id) {
    case ARG_LEFT:
      video_crop->crop_left = g_value_get_int (value);
695
      break;
696 697
    case ARG_RIGHT:
      video_crop->crop_right = g_value_get_int (value);
698
      break;
699 700
    case ARG_TOP:
      video_crop->crop_top = g_value_get_int (value);
701
      break;
702 703
    case ARG_BOTTOM:
      video_crop->crop_bottom = g_value_get_int (value);
704
      break;
705 706
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
707 708
      break;
  }
709
  GST_LOG_OBJECT (video_crop, "l=%d,r=%d,b=%d,t=%d",
710
      video_crop->crop_left, video_crop->crop_right, video_crop->crop_bottom,
711
      video_crop->crop_top);
712
  GST_OBJECT_UNLOCK (video_crop);
713

Wim Taymans's avatar
Wim Taymans committed
714
  gst_base_transform_reconfigure_src (GST_BASE_TRANSFORM (video_crop));
715
  g_mutex_unlock (&video_crop->lock);
716 717 718 719 720 721 722 723 724
}

static void
gst_video_crop_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstVideoCrop *video_crop;

  video_crop = GST_VIDEO_CROP (object);
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
  GST_OBJECT_LOCK (video_crop);
  switch (prop_id) {
    case ARG_LEFT:
      g_value_set_int (value, video_crop->crop_left);
      break;
    case ARG_RIGHT:
      g_value_set_int (value, video_crop->crop_right);
      break;
    case ARG_TOP:
      g_value_set_int (value, video_crop->crop_top);
      break;
    case ARG_BOTTOM:
      g_value_set_int (value, video_crop->crop_bottom);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
  GST_OBJECT_UNLOCK (video_crop);
745 746 747
}

static gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
748
plugin_init (GstPlugin * plugin)
749
{
750 751
  GST_DEBUG_CATEGORY_INIT (videocrop_debug, "videocrop", 0, "videocrop");

752 753 754 755 756 757 758
  if (gst_element_register (plugin, "videocrop", GST_RANK_NONE,
          GST_TYPE_VIDEO_CROP)
      && gst_element_register (plugin, "aspectratiocrop", GST_RANK_NONE,
          GST_TYPE_ASPECT_RATIO_CROP))
    return TRUE;

  return FALSE;
759 760
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
761 762 763
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "videocrop",
764
    "Crops video into a user-defined region",
765
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)