gstvideobox.c 38.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
 *
 * 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.
 */
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * SECTION:element-videobox
 * @see_also: #GstVideoCrop
 *
 * This plugin crops or enlarges the image. It takes 4 values as input, a
 * top, bottom, left and right offset. Positive values will crop that much
 * pixels from the respective border of the image, negative values will add
 * that much pixels. When pixels are added, you can specify their color. 
 * Some predefined colors are usable with an enum property.
 * 
 * The plugin is alpha channel aware and will try to negotiate with a format
 * that supports alpha channels first. When alpha channel is active two
 * other properties, alpha and border_alpha can be used to set the alpha
 * values of the inner picture and the border respectively. an alpha value of
 * 0.0 means total transparency, 1.0 is opaque.
 * 
 * The videobox plugin has many uses such as doing a mosaic of pictures, 
 * letterboxing video, cutting out pieces of video, picture in picture, etc..
 */
38

39 40 41 42
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
43
#include <gst/base/gstbasetransform.h>
44
#include <gst/video/video.h>
45
#include <math.h>
46
#include <liboil/liboil.h>
47 48
#include <string.h>

49
GST_DEBUG_CATEGORY_STATIC (videobox_debug);
50 51
#define GST_CAT_DEFAULT videobox_debug

52 53 54 55 56 57 58 59
#define GST_TYPE_VIDEO_BOX \
  (gst_video_box_get_type())
#define GST_VIDEO_BOX(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_VIDEO_BOX,GstVideoBox))
#define GST_VIDEO_BOX_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_VIDEO_BOX,GstVideoBoxClass))
#define GST_IS_VIDEO_BOX(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_VIDEO_BOX))
Stefan Kost's avatar
Stefan Kost committed
60
#define GST_IS_VIDEO_BOX_CLASS(klass) \
61 62 63 64 65 66 67 68 69 70
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_VIDEO_BOX))

typedef struct _GstVideoBox GstVideoBox;
typedef struct _GstVideoBoxClass GstVideoBoxClass;

typedef enum
{
  VIDEO_BOX_FILL_BLACK,
  VIDEO_BOX_FILL_GREEN,
  VIDEO_BOX_FILL_BLUE,
71
  VIDEO_BOX_FILL_LAST
72 73 74 75 76
}
GstVideoBoxFill;

struct _GstVideoBox
{
77
  GstBaseTransform element;
78 79

  /* caps */
80
  guint32 in_fourcc;
81
  gint in_width, in_height;
82
  guint32 out_fourcc;
83 84 85 86 87 88 89 90 91 92 93
  gint out_width, out_height;

  gint box_left, box_right, box_top, box_bottom;

  gint border_left, border_right, border_top, border_bottom;
  gint crop_left, crop_right, crop_top, crop_bottom;

  gdouble alpha;
  gdouble border_alpha;

  GstVideoBoxFill fill_type;
94 95

  gboolean autocrop;
96 97 98 99
};

struct _GstVideoBoxClass
{
100
  GstBaseTransformClass parent_class;
101 102 103
};

/* elementfactory information */
104
static const GstElementDetails gst_video_box_details =
Wim Taymans's avatar
Wim Taymans committed
105
GST_ELEMENT_DETAILS ("Video box filter",
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    "Filter/Effect/Video",
    "Resizes a video by adding borders or cropping",
    "Wim Taymans <wim@fluendo.com>");


#define DEFAULT_LEFT      0
#define DEFAULT_RIGHT     0
#define DEFAULT_TOP       0
#define DEFAULT_BOTTOM    0
#define DEFAULT_FILL_TYPE VIDEO_BOX_FILL_BLACK
#define DEFAULT_ALPHA     1.0
#define DEFAULT_BORDER_ALPHA 1.0

enum
{
121 122 123 124 125 126 127
  PROP_0,
  PROP_LEFT,
  PROP_RIGHT,
  PROP_TOP,
  PROP_BOTTOM,
  PROP_FILL_TYPE,
  PROP_ALPHA,
128 129
  PROP_BORDER_ALPHA,
  PROP_AUTOCROP
130
      /* FILL ME */
131 132
};

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/*

Setting autocrop to true changes the behavior of the plugin so that
caps determine crop properties rather than the other way around: given
input and output dimensions, the crop values are selected so that the
smaller frame is effectively centered in the larger frame.  This
involves either cropping or padding.

If you use autocrop there is little point in setting the other
properties manually because they will be overriden if the caps change,
but nothing stops you from doing so.

Sample pipeline:
gst-launch videotestsrc ! videobox autocrop=true ! \
"video/x-raw-yuv, width=600, height=400" ! ffmpegcolorspace ! ximagesink

*/


152
static GstStaticPadTemplate gst_video_box_src_template =
153
    GST_STATIC_PAD_TEMPLATE ("src",
154 155
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
156 157
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
        GST_VIDEO_CAPS_YUV ("I420"))
158 159 160
    );

static GstStaticPadTemplate gst_video_box_sink_template =
161
    GST_STATIC_PAD_TEMPLATE ("sink",
162 163
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
164 165
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
        GST_VIDEO_CAPS_YUV ("I420"))
166 167 168
    );


169 170
GST_BOILERPLATE (GstVideoBox, gst_video_box, GstBaseTransform,
    GST_TYPE_BASE_TRANSFORM);
171 172 173 174 175 176

static void gst_video_box_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_video_box_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

177
static gboolean video_box_recalc_transform (GstVideoBox * video_box);
178
static GstCaps *gst_video_box_transform_caps (GstBaseTransform * trans,
179
    GstPadDirection direction, GstCaps * from);
180 181
static gboolean gst_video_box_set_caps (GstBaseTransform * trans,
    GstCaps * in, GstCaps * out);
182 183
static gboolean gst_video_box_get_unit_size (GstBaseTransform * trans,
    GstCaps * caps, guint * size);
184
static GstFlowReturn gst_video_box_transform (GstBaseTransform * trans,
Wim Taymans's avatar
Wim Taymans committed
185
    GstBuffer * in, GstBuffer * out);
186 187 188 189 190 191 192


#define GST_TYPE_VIDEO_BOX_FILL (gst_video_box_fill_get_type())
static GType
gst_video_box_fill_get_type (void)
{
  static GType video_box_fill_type = 0;
193
  static const GEnumValue video_box_fill[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
194 195 196
    {VIDEO_BOX_FILL_BLACK, "Black", "black"},
    {VIDEO_BOX_FILL_GREEN, "Colorkey green", "green"},
    {VIDEO_BOX_FILL_BLUE, "Colorkey blue", "blue"},
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    {0, NULL, NULL},
  };

  if (!video_box_fill_type) {
    video_box_fill_type =
        g_enum_register_static ("GstVideoBoxFill", video_box_fill);
  }
  return video_box_fill_type;
}


static void
gst_video_box_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_set_details (element_class, &gst_video_box_details);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_video_box_sink_template));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_video_box_src_template));
}
220

221 222 223 224
static void
gst_video_box_class_init (GstVideoBoxClass * klass)
{
  GObjectClass *gobject_class;
225
  GstBaseTransformClass *trans_class;
226 227

  gobject_class = (GObjectClass *) klass;
228
  trans_class = (GstBaseTransformClass *) klass;
229

Wim Taymans's avatar
Wim Taymans committed
230 231 232
  gobject_class->set_property = gst_video_box_set_property;
  gobject_class->get_property = gst_video_box_get_property;

233
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILL_TYPE,
234 235 236
      g_param_spec_enum ("fill", "Fill", "How to fill the borders",
          GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
          (GParamFlags) G_PARAM_READWRITE));
237
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEFT,
238 239 240
      g_param_spec_int ("left", "Left",
          "Pixels to box at left (<0  = add a border)", G_MININT, G_MAXINT,
          DEFAULT_LEFT, G_PARAM_READWRITE));
241
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RIGHT,
242 243 244
      g_param_spec_int ("right", "Right",
          "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
          DEFAULT_RIGHT, G_PARAM_READWRITE));
245
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOP,
246 247 248
      g_param_spec_int ("top", "Top",
          "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
          DEFAULT_TOP, G_PARAM_READWRITE));
249
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BOTTOM,
250 251 252
      g_param_spec_int ("bottom", "Bottom",
          "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
          DEFAULT_BOTTOM, G_PARAM_READWRITE));
253
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALPHA,
254 255
      g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
          DEFAULT_ALPHA, G_PARAM_READWRITE));
256
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER_ALPHA,
257 258 259
      g_param_spec_double ("border_alpha", "Border Alpha",
          "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
          G_PARAM_READWRITE));
260 261 262
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_AUTOCROP,
      g_param_spec_boolean ("autocrop", "Auto crop",
          "Auto crop", FALSE, G_PARAM_READWRITE));
263

264
  trans_class->transform = GST_DEBUG_FUNCPTR (gst_video_box_transform);
265 266 267 268
  trans_class->transform_caps =
      GST_DEBUG_FUNCPTR (gst_video_box_transform_caps);
  trans_class->set_caps = GST_DEBUG_FUNCPTR (gst_video_box_set_caps);
  trans_class->get_unit_size = GST_DEBUG_FUNCPTR (gst_video_box_get_unit_size);
269 270 271

  GST_DEBUG_CATEGORY_INIT (videobox_debug, "videobox", 0,
      "Resizes a video by adding borders or cropping");
272 273 274
}

static void
275
gst_video_box_init (GstVideoBox * video_box, GstVideoBoxClass * g_class)
276 277 278 279 280
{
  video_box->box_right = DEFAULT_RIGHT;
  video_box->box_left = DEFAULT_LEFT;
  video_box->box_top = DEFAULT_TOP;
  video_box->box_bottom = DEFAULT_BOTTOM;
281 282 283 284
  video_box->crop_right = 0;
  video_box->crop_left = 0;
  video_box->crop_top = 0;
  video_box->crop_bottom = 0;
285 286 287
  video_box->fill_type = DEFAULT_FILL_TYPE;
  video_box->alpha = DEFAULT_ALPHA;
  video_box->border_alpha = DEFAULT_BORDER_ALPHA;
288
  video_box->autocrop = FALSE;
289 290 291 292 293 294
}

static void
gst_video_box_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
295
  GstVideoBox *video_box = GST_VIDEO_BOX (object);
296

297
  GST_BASE_TRANSFORM_LOCK (GST_BASE_TRANSFORM_CAST (video_box));
298
  switch (prop_id) {
299
    case PROP_LEFT:
300 301 302 303 304 305 306 307 308
      video_box->box_left = g_value_get_int (value);
      if (video_box->box_left < 0) {
        video_box->border_left = -video_box->box_left;
        video_box->crop_left = 0;
      } else {
        video_box->border_left = 0;
        video_box->crop_left = video_box->box_left;
      }
      break;
309
    case PROP_RIGHT:
310 311 312 313 314 315 316 317 318
      video_box->box_right = g_value_get_int (value);
      if (video_box->box_right < 0) {
        video_box->border_right = -video_box->box_right;
        video_box->crop_right = 0;
      } else {
        video_box->border_right = 0;
        video_box->crop_right = video_box->box_right;
      }
      break;
319
    case PROP_TOP:
320 321 322 323 324 325 326 327 328
      video_box->box_top = g_value_get_int (value);
      if (video_box->box_top < 0) {
        video_box->border_top = -video_box->box_top;
        video_box->crop_top = 0;
      } else {
        video_box->border_top = 0;
        video_box->crop_top = video_box->box_top;
      }
      break;
329
    case PROP_BOTTOM:
330 331 332 333 334 335 336 337 338
      video_box->box_bottom = g_value_get_int (value);
      if (video_box->box_bottom < 0) {
        video_box->border_bottom = -video_box->box_bottom;
        video_box->crop_bottom = 0;
      } else {
        video_box->border_bottom = 0;
        video_box->crop_bottom = video_box->box_bottom;
      }
      break;
339
    case PROP_FILL_TYPE:
340 341
      video_box->fill_type = g_value_get_enum (value);
      break;
342
    case PROP_ALPHA:
343 344
      video_box->alpha = g_value_get_double (value);
      break;
345
    case PROP_BORDER_ALPHA:
346 347
      video_box->border_alpha = g_value_get_double (value);
      break;
348 349 350
    case PROP_AUTOCROP:
      video_box->autocrop = g_value_get_boolean (value);
      break;
351 352 353 354
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
  video_box_recalc_transform (video_box);
  /*
     GST_DEBUG_OBJECT (video_box, "Calling reconfigure");
     gst_base_transform_reconfigure (GST_BASE_TRANSFORM (video_box));
   */
  GST_BASE_TRANSFORM_UNLOCK (GST_BASE_TRANSFORM_CAST (video_box));
}

static void
gst_video_box_autocrop (GstVideoBox * video_box)
{
  gint crop_w = (video_box->in_width - video_box->out_width) / 2;
  gint crop_h = (video_box->in_height - video_box->out_height) / 2;

  GST_BASE_TRANSFORM_LOCK (GST_BASE_TRANSFORM_CAST (video_box));

  video_box->box_left = crop_w;
  if (video_box->box_left < 0) {
    video_box->border_left = -video_box->box_left;
    video_box->crop_left = 0;
  } else {
    video_box->border_left = 0;
    video_box->crop_left = video_box->box_left;
  }

  video_box->box_right = crop_w;
  if (video_box->box_right < 0) {
    video_box->border_right = -video_box->box_right;
    video_box->crop_right = 0;
  } else {
    video_box->border_right = 0;
    video_box->crop_right = video_box->box_right;
  }

  video_box->box_top = crop_h;
  if (video_box->box_top < 0) {
    video_box->border_top = -video_box->box_top;
    video_box->crop_top = 0;
  } else {
    video_box->border_top = 0;
    video_box->crop_top = video_box->box_top;
  }

  video_box->box_bottom = crop_h;
  if (video_box->box_bottom < 0) {
    video_box->border_bottom = -video_box->box_bottom;
    video_box->crop_bottom = 0;
  } else {
    video_box->border_bottom = 0;
    video_box->crop_bottom = video_box->box_bottom;
  }

407
  GST_BASE_TRANSFORM_UNLOCK (GST_BASE_TRANSFORM_CAST (video_box));
408
}
409

410 411 412 413
static void
gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
414
  GstVideoBox *video_box = GST_VIDEO_BOX (object);
415 416

  switch (prop_id) {
417
    case PROP_LEFT:
418 419
      g_value_set_int (value, video_box->box_left);
      break;
420
    case PROP_RIGHT:
421 422
      g_value_set_int (value, video_box->box_right);
      break;
423
    case PROP_TOP:
424 425
      g_value_set_int (value, video_box->box_top);
      break;
426
    case PROP_BOTTOM:
427 428
      g_value_set_int (value, video_box->box_bottom);
      break;
429
    case PROP_FILL_TYPE:
430 431
      g_value_set_enum (value, video_box->fill_type);
      break;
432
    case PROP_ALPHA:
433 434
      g_value_set_double (value, video_box->alpha);
      break;
435
    case PROP_BORDER_ALPHA:
436 437
      g_value_set_double (value, video_box->border_alpha);
      break;
438 439 440
    case PROP_AUTOCROP:
      g_value_set_boolean (value, video_box->autocrop);
      break;
441 442 443 444 445 446
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

447
static GstCaps *
448 449
gst_video_box_transform_caps (GstBaseTransform * trans,
    GstPadDirection direction, GstCaps * from)
450 451
{
  GstVideoBox *video_box;
452 453
  GstCaps *to, *ret;
  const GstCaps *templ;
454
  GstStructure *structure;
455 456
  GstPad *other;
  gint width, height;
457 458

  video_box = GST_VIDEO_BOX (trans);
459

460
  to = gst_caps_copy (from);
461 462 463 464 465
  structure = gst_caps_get_structure (to, 0);

  /* get rid of format */
  gst_structure_remove_field (structure, "format");

466 467 468 469
  /* otherwise caps nego will fail: */
  if (video_box->autocrop) {
    gst_structure_remove_field (structure, "width");
    gst_structure_remove_field (structure, "height");
470 471
  }

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
  if (!video_box->autocrop) {

    /* calculate width and height */
    if (gst_structure_get_int (structure, "width", &width)) {
      if (direction == GST_PAD_SINK) {
        width -= video_box->box_left;
        width -= video_box->box_right;
      } else {
        width += video_box->box_left;
        width += video_box->box_right;
      }
      if (width <= 0)
        width = 1;

      GST_DEBUG_OBJECT (trans, "New caps width: %d", width);
      gst_structure_set (structure, "width", G_TYPE_INT, width, NULL);
488
    }
489

490 491 492 493 494 495 496 497
    if (gst_structure_get_int (structure, "height", &height)) {
      if (direction == GST_PAD_SINK) {
        height -= video_box->box_top;
        height -= video_box->box_bottom;
      } else {
        height += video_box->box_top;
        height += video_box->box_bottom;
      }
498

499 500 501 502 503 504
      if (height <= 0)
        height = 1;

      GST_DEBUG_OBJECT (trans, "New caps height: %d", height);
      gst_structure_set (structure, "height", G_TYPE_INT, height, NULL);
    }
505 506
  }

507 508 509 510 511 512
  /* filter against set allowed caps on the pad */
  other = (direction == GST_PAD_SINK) ? trans->srcpad : trans->sinkpad;

  templ = gst_pad_get_pad_template_caps (other);
  ret = gst_caps_intersect (to, templ);
  gst_caps_unref (to);
513

514
  GST_DEBUG_OBJECT (video_box, "direction %d, transformed %" GST_PTR_FORMAT
515 516 517 518 519
      " to %" GST_PTR_FORMAT, direction, from, ret);

  return ret;
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
static gboolean
video_box_recalc_transform (GstVideoBox * video_box)
{
  gboolean res = TRUE;

  /* if we have the same format in and out and we don't need to perform any
   * cropping at all, we can just operate in passthorugh mode */
  if (video_box->in_fourcc == video_box->out_fourcc &&
      video_box->box_left == 0 && video_box->box_right == 0 &&
      video_box->box_top == 0 && video_box->box_bottom == 0) {

    GST_LOG_OBJECT (video_box, "we are using passthrough");
    gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (video_box), TRUE);
  } else {
    GST_LOG_OBJECT (video_box, "we are not using passthrough");
    gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (video_box), FALSE);
  }
  return res;
}

Wim Taymans's avatar
Wim Taymans committed
540
static gboolean
541
gst_video_box_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
542 543 544 545 546
{
  GstVideoBox *video_box;
  GstStructure *structure;
  gboolean ret;

547
  video_box = GST_VIDEO_BOX (trans);
548

549
  structure = gst_caps_get_structure (in, 0);
550 551
  ret = gst_structure_get_int (structure, "width", &video_box->in_width);
  ret &= gst_structure_get_int (structure, "height", &video_box->in_height);
552
  ret &= gst_structure_get_fourcc (structure, "format", &video_box->in_fourcc);
553

554 555 556
  structure = gst_caps_get_structure (out, 0);
  ret &= gst_structure_get_int (structure, "width", &video_box->out_width);
  ret &= gst_structure_get_int (structure, "height", &video_box->out_height);
557
  ret &= gst_structure_get_fourcc (structure, "format", &video_box->out_fourcc);
558

559 560 561 562
  /* something wrong getting the caps */
  if (!ret)
    goto no_caps;

563 564 565 566 567
  GST_DEBUG_OBJECT (trans, "Input w: %d h: %d", video_box->in_width,
      video_box->in_height);
  GST_DEBUG_OBJECT (trans, "Output w: %d h: %d", video_box->out_width,
      video_box->out_height);

568 569
  if (video_box->autocrop)
    gst_video_box_autocrop (video_box);
570

571 572
  /* recalc the transformation strategy */
  ret = video_box_recalc_transform (video_box);
573

Wim Taymans's avatar
Wim Taymans committed
574
  return ret;
575 576 577 578 579 580 581

  /* ERRORS */
no_caps:
  {
    GST_DEBUG_OBJECT (video_box, "Could not get all caps fields");
    return FALSE;
  }
582 583
}

584
/* see gst-plugins/gst/games/gstvideoimage.c, paint_setup_I420() */
585 586 587
#define GST_VIDEO_I420_Y_ROWSTRIDE(width) (GST_ROUND_UP_4(width))
#define GST_VIDEO_I420_U_ROWSTRIDE(width) (GST_ROUND_UP_8(width)/2)
#define GST_VIDEO_I420_V_ROWSTRIDE(width) ((GST_ROUND_UP_8(GST_VIDEO_I420_Y_ROWSTRIDE(width)))/2)
588

589
#define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
590 591
#define GST_VIDEO_I420_U_OFFSET(w,h) (GST_VIDEO_I420_Y_OFFSET(w,h)+(GST_VIDEO_I420_Y_ROWSTRIDE(w)*GST_ROUND_UP_2(h)))
#define GST_VIDEO_I420_V_OFFSET(w,h) (GST_VIDEO_I420_U_OFFSET(w,h)+(GST_VIDEO_I420_U_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
592

593
#define GST_VIDEO_I420_SIZE(w,h)     (GST_VIDEO_I420_V_OFFSET(w,h)+(GST_VIDEO_I420_V_ROWSTRIDE(w)*GST_ROUND_UP_2(h)/2))
594

595 596 597
static gboolean
gst_video_box_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
    guint * size)
Wim Taymans's avatar
Wim Taymans committed
598
{
599

Wim Taymans's avatar
Wim Taymans committed
600
  GstVideoBox *video_box;
601 602 603
  GstStructure *structure = NULL;
  guint32 fourcc;
  gint width, height;
Wim Taymans's avatar
Wim Taymans committed
604

Stefan Kost's avatar
Stefan Kost committed
605 606
  g_assert (size);

Wim Taymans's avatar
Wim Taymans committed
607 608
  video_box = GST_VIDEO_BOX (trans);

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  structure = gst_caps_get_structure (caps, 0);
  gst_structure_get_fourcc (structure, "format", &fourcc);
  gst_structure_get_int (structure, "width", &width);
  gst_structure_get_int (structure, "height", &height);

  switch (fourcc) {
    case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
      *size = width * height * 4;
      break;
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
      *size = GST_VIDEO_I420_SIZE (width, height);
      break;
    default:
      return FALSE;
      break;
Wim Taymans's avatar
Wim Taymans committed
624
  }
625

626 627
  GST_LOG_OBJECT (video_box, "Returning from _unit_size %d", *size);

628
  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
629 630
}

631 632 633
static const guint8 yuv_colors_Y[VIDEO_BOX_FILL_LAST] = { 16, 150, 29 };
static const guint8 yuv_colors_U[VIDEO_BOX_FILL_LAST] = { 128, 46, 255 };
static const guint8 yuv_colors_V[VIDEO_BOX_FILL_LAST] = { 128, 21, 107 };
634

635 636 637 638 639 640 641 642 643 644
static void
gst_video_box_copy_plane_i420 (GstVideoBox * video_box, guint8 * src,
    guint8 * dest, gint br, gint bl, gint bt, gint bb, gint src_crop_width,
    gint src_crop_height, gint src_stride, gint dest_width, gint dest_stride,
    guint8 fill_color)
{
  gint j;

  /* top border */
  for (j = 0; j < bt; j++) {
645
    oil_splat_u8_ns (dest, &fill_color, dest_width);
646 647 648 649 650
    dest += dest_stride;
  }

  /* copy and add left and right border */
  for (j = 0; j < src_crop_height; j++) {
651 652 653
    oil_splat_u8_ns (dest, &fill_color, bl);
    oil_memcpy (dest + bl, src, src_crop_width);
    oil_splat_u8_ns (dest + bl + src_crop_width, &fill_color, br);
654 655 656 657 658 659
    dest += dest_stride;
    src += src_stride;
  }

  /* bottom border */
  for (j = 0; j < bb; j++) {
660
    oil_splat_u8_ns (dest, &fill_color, dest_width);
661 662 663 664
    dest += dest_stride;
  }
}

665
static void
666
gst_video_box_apply_alpha (guint8 * dest, guint8 alpha)
667
{
668 669 670
  if (dest[0] != 0)
    dest[0] = alpha;
}
671

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
static void
gst_video_box_ayuv_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
{
  gint dblen = video_box->out_height * video_box->out_width;
  guint32 *destb = (guint32 *) dest;
  guint32 *srcb = (guint32 *) src;
  guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
  guint8 i_alpha = (guint8) (video_box->alpha * 255);
  gint br, bl, bt, bb, crop_w, crop_h;
  gint i;
  guint32 *loc = destb;
  guint32 empty_pixel;

  GST_LOG ("Processing AYUV -> AYUV data");

  crop_h = 0;
  crop_w = 0;
  empty_pixel = GUINT32_FROM_BE ((b_alpha << 24) |
      (yuv_colors_Y[video_box->fill_type] << 16) |
      (yuv_colors_U[video_box->fill_type] << 8) |
      yuv_colors_V[video_box->fill_type]);

  br = video_box->box_right;
  bl = video_box->box_left;
  bt = video_box->box_top;
  bb = video_box->box_bottom;

  if (br >= 0 && bl >= 0) {
    crop_w = video_box->in_width - (br + bl);
  } else if (br >= 0 && bl < 0) {
    crop_w = video_box->in_width - (br);
  } else if (br < 0 && bl >= 0) {
    crop_w = video_box->in_width - (bl);
  } else if (br < 0 && bl < 0) {
    crop_w = video_box->in_width;
  }
708

709 710 711 712 713 714 715 716 717
  if (bb >= 0 && bt >= 0) {
    crop_h = video_box->in_height - (bb + bt);
  } else if (bb >= 0 && bt < 0) {
    crop_h = video_box->in_height - (bb);
  } else if (bb < 0 && bt >= 0) {
    crop_h = video_box->in_height - (bt);
  } else if (bb < 0 && bt < 0) {
    crop_h = video_box->in_height;
  }
718

719 720
  GST_DEBUG ("Borders are: L:%d, R:%d, T:%d, B:%d", bl, br, bt, bb);
  GST_DEBUG ("Alpha value is: %d", i_alpha);
721

722
  if (crop_h <= 0 || crop_w <= 0) {
723

724
    oil_splat_u32_ns (destb, &empty_pixel, dblen);
725

726
  } else {
727

728
    guint32 *src_loc = srcb;
729

730 731 732 733 734 735 736
    /* Top border */
    if (bt < 0) {
      oil_splat_u32_ns (loc, &empty_pixel, (-bt) * video_box->out_width);
      loc = loc + ((-bt) * video_box->out_width);
    } else {
      src_loc = src_loc + (bt * video_box->in_width);
    }
737

738 739
    if (bl >= 0)
      src_loc += bl;
740

741 742
    for (i = 0; i < crop_h; i++) {
      gint j;
743

744 745 746 747 748
      /* Left border */
      if (bl < 0) {
        oil_splat_u32_ns (loc, &empty_pixel, -bl);
        loc += (-bl);
      }
749

750 751
      /* Cropped area */
      oil_copy_u8 ((guint8 *) loc, (guint8 *) src_loc, crop_w * 4);
752

753 754
      for (j = 0; j < crop_w; j++)
        gst_video_box_apply_alpha ((guint8 *) & loc[j], i_alpha);
755

756 757
      src_loc += video_box->in_width;
      loc += crop_w;
758

759 760 761 762 763 764
      /* Right border */
      if (br < 0) {
        oil_splat_u32_ns (loc, &empty_pixel, -br);
        loc += (-br);
      }
    }
765

766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    /* Bottom border */
    if (bb < 0) {
      oil_splat_u32_ns (loc, &empty_pixel, (-bb) * video_box->out_width);
    }
  }

  GST_LOG ("image created");

}

static gpointer
gst_video_box_clear (gpointer dest, gint size)
{
  guint8 nil = 255;

  oil_splat_u8_ns (dest, &nil, size);

  return dest;
}

static gint
UVfloor (gint j)
{
  return floor (((float) j) / 2);
}

static gint
UVceil (gint j)
{
  return ceil (((float) j) / 2);
}

static void
gst_video_box_ayuv_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
{
  gint br, bl, bt, bb, crop_w, crop_h, rest;
  gint Ysize, Usize, Vsize;
  guint8 *Ydest, *Udest, *Vdest;
  guint8 *Utemp, *Vtemp;
  guint32 empty_px_values[3];
  gint i, j;
  guint Ywidth, Uwidth, Vwidth;

809
  GST_LOG ("AYUV to I420 conversion");
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862

  crop_h = 0;
  crop_w = 0;
  rest = 0;

  empty_px_values[0] = yuv_colors_Y[video_box->fill_type];
  empty_px_values[1] = yuv_colors_U[video_box->fill_type];
  empty_px_values[2] = yuv_colors_V[video_box->fill_type];

  Ywidth = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->out_width);
  Uwidth = GST_VIDEO_I420_U_ROWSTRIDE (video_box->out_width);
  Vwidth = GST_VIDEO_I420_V_ROWSTRIDE (video_box->out_width);

  Ydest = dest + GST_VIDEO_I420_Y_OFFSET (video_box->out_width,
      video_box->out_height);
  Udest = Ydest + GST_VIDEO_I420_U_OFFSET (video_box->out_width,
      video_box->out_height);
  Vdest = Ydest + GST_VIDEO_I420_V_OFFSET (video_box->out_width,
      video_box->out_height);

  Ysize = Ywidth * video_box->out_height;
  Usize = Uwidth * UVceil (video_box->out_height);
  Vsize = Vwidth * UVceil (video_box->out_height);

  br = video_box->box_right;
  bl = video_box->box_left;
  bt = video_box->box_top;
  bb = video_box->box_bottom;

  if (br >= 0 && bl >= 0) {
    rest = Ywidth - video_box->out_width;
    crop_w = video_box->in_width - (bl + br);
  } else if (br >= 0 && bl < 0) {
    rest = Ywidth - video_box->out_width;
    crop_w = video_box->in_width - (br);
  } else if (br < 0 && bl >= 0) {
    rest = Ywidth - video_box->out_width;
    crop_w = video_box->in_width - (bl);
  } else if (br < 0 && bl < 0) {
    rest = Ywidth - video_box->out_width;
    crop_w = video_box->in_width;
  }

  if (bb >= 0 && bt >= 0) {
    crop_h = video_box->in_height - (bb + bt);
  } else if (bb >= 0 && bt < 0) {
    crop_h = video_box->in_height - (bb);
  } else if (bb < 0 && bt >= 0) {
    crop_h = video_box->in_height - (bt);
  } else if (bb < 0 && bt < 0) {
    crop_h = video_box->in_height;
  }

863 864
  Utemp = g_malloc0 (Uwidth);
  Vtemp = g_malloc0 (Vwidth);
865

866
  GST_LOG ("Borders are: L:%d, R:%d, T:%d, B:%d", bl, br, bt, bb);
867

868
  GST_LOG ("Starting conversion");
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908

  if (crop_h <= 0 || crop_w <= 0) {

    oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], Ysize);
    oil_splat_u8_ns (Udest, (guint8 *) & empty_px_values[1], Usize);
    oil_splat_u8_ns (Vdest, (guint8 *) & empty_px_values[2], Vsize);

  } else {

    gboolean sumbuff = FALSE;
    guint32 *src_loc1;
    gint a = 0;

    src_loc1 = (guint32 *) src;

    if (bt < 0) {
      oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], (-bt) * Ywidth);

      oil_splat_u8_ns (Udest, (guint8 *) & empty_px_values[1],
          (UVfloor (-bt) * Uwidth) + 7);
      oil_splat_u8_ns (Vdest, (guint8 *) & empty_px_values[2],
          UVfloor (-bt) * Vwidth);

      if ((-bt) % 2 > 0) {
        oil_splat_u8_ns (Utemp, (guint8 *) & empty_px_values[1], Uwidth);
        oil_splat_u8_ns (Vtemp, (guint8 *) & empty_px_values[2], Vwidth);
        sumbuff = TRUE;
      }

      Ydest += ((-bt) * Ywidth);
      Udest += (UVfloor (-bt) * Uwidth);
      Vdest += (UVfloor (-bt) * Vwidth);

    } else {
      src_loc1 = src_loc1 + (bt * video_box->in_width);
    }

    if (bl >= 0)
      src_loc1 += bl;

909 910 911 912 913
    GST_LOG ("Cropped area");
    GST_LOG ("Ydest value: %p Ywidth: %u", Ydest, Ywidth);
    GST_LOG ("Udest value: %p Uwidth: %u", Udest, Uwidth);
    GST_LOG ("Vdest value: %p Vwidth: %u", Vdest, Vwidth);
    GST_LOG ("Rest: %d", rest);
914 915 916 917
    for (i = 0; i < crop_h; i++) {

      a = 0;
      if (sumbuff) {
918
        /* left border */
919 920 921 922 923 924 925 926 927 928 929 930
        if (bl < 0) {
          oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], -bl);

          for (j = 0; j < -bl; j++) {
            Utemp[UVfloor (j)] = (Utemp[UVfloor (j)] + empty_px_values[1]) / 2;
            Vtemp[UVfloor (j)] = (Vtemp[UVfloor (j)] + empty_px_values[2]) / 2;
          }
          Ydest += -bl;
          a = -bl;
        }

        for (j = 0; j < crop_w; j++) {
931
          /* check ARCH */
932 933 934 935 936 937 938 939
          Ydest[j] = ((guint8 *) & src_loc1[j])[1];
          Utemp[UVfloor (a + j)] =
              (Utemp[UVfloor (a + j)] + ((guint8 *) & src_loc1[j])[2]) / 2;
          Vtemp[UVfloor (a + j)] =
              (Vtemp[UVfloor (a + j)] + ((guint8 *) & src_loc1[j])[3]) / 2;
        }
        Ydest += crop_w;

940
        /* right border */
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
        if (br < 0) {
          oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], -br);
          for (j = 0; j < -br; j++) {
            Utemp[UVfloor (a + crop_w + j)] =
                (Utemp[UVfloor (a + crop_w + j)] + empty_px_values[1]) / 2;
            Vtemp[UVfloor (a + crop_w + j)] =
                (Vtemp[UVfloor (a + crop_w + j)] + empty_px_values[2]) / 2;
          }
          Ydest += -br;
        }
        oil_copy_u8 (Udest, Utemp, Uwidth);
        oil_copy_u8 (Vdest, Vtemp, Vwidth);
        Udest += Uwidth;
        Vdest += Vwidth;
        Ydest += rest;
        gst_video_box_clear (Utemp, Uwidth);
        gst_video_box_clear (Vtemp, Vwidth);
        src_loc1 += video_box->in_width;
        sumbuff = FALSE;

      } else {

963
        /* left border */
964 965 966 967 968 969 970 971 972 973 974 975 976
        a = 0;
        if (bl < 0) {
          oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], -bl);
          oil_splat_u8_ns (Vtemp, (guint8 *) & empty_px_values[1],
              UVceil (-bl));
          oil_splat_u8_ns (Utemp, (guint8 *) & empty_px_values[2],
              UVceil (-bl));
          Ydest += -bl;
          a = -bl;
        }

        for (j = 0; j < crop_w; j++) {

977
          /* check ARCH */
978 979 980 981 982 983 984 985 986 987 988 989 990 991
          Ydest[j] = ((guint8 *) & src_loc1[j])[1];

          if ((a + j) % 2 > 0) {
            Utemp[UVfloor (a + j)] =
                (Utemp[UVfloor (a + j)] + ((guint8 *) & src_loc1[j])[2]) / 2;
            Vtemp[UVfloor (a + j)] =
                (Vtemp[UVfloor (a + j)] + ((guint8 *) & src_loc1[j])[3]) / 2;
          } else {
            Utemp[UVfloor (a + j)] = ((guint8 *) & src_loc1[j])[2] / 2;
            Vtemp[UVfloor (a + j)] = ((guint8 *) & src_loc1[j])[3] / 2;
          }
        }
        Ydest += crop_w;

992
        /* right border */
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
        if (br < 0) {
          j = 0;
          if ((a + crop_w) % 2 > 0) {
            Utemp[UVfloor (a + crop_w)] =
                (Utemp[UVfloor (a + crop_w)] + empty_px_values[1]) / 2;
            Vtemp[UVfloor (a + crop_w)] =
                (Vtemp[UVfloor (a + crop_w)] + empty_px_values[2]) / 2;
            a++;
            j = -1;
          }

          oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], -br);
          oil_splat_u8_ns (&Utemp[UVfloor (a + crop_w)],
              (guint8 *) & empty_px_values[1], UVceil ((-br) + j));
          oil_splat_u8_ns (&Vtemp[UVfloor (a + crop_w)],
              (guint8 *) & empty_px_values[2], UVceil ((-br) + j));
          Ydest += -br;
        }
        Ydest += rest;
        src_loc1 += video_box->in_width;
        sumbuff = TRUE;

      }
    }

1018
    /* bottom border */
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    if (bb < 0) {
      oil_splat_u8_ns (Ydest, (guint8 *) & empty_px_values[0], (-bb) * Ywidth);
      if (sumbuff) {
        for (i = 0; i < Uwidth; i++) {
          Utemp[i] = (Utemp[i] + empty_px_values[1]) / 2;
        }
        for (i = 0; i < Vwidth; i++) {
          Vtemp[i] = (Vtemp[i] + empty_px_values[2]) / 2;
        }

        oil_copy_u8 (Udest, Utemp, Uwidth);
        oil_copy_u8 (Vdest, Vtemp, Vwidth);
        Udest += Uwidth;
        Vdest += Vwidth;
        sumbuff = FALSE;
      }
      oil_splat_u8_ns (Udest, (guint8 *) & empty_px_values[1],
          (UVfloor ((-bb))) * Uwidth);
      oil_splat_u8_ns (Vdest, (guint8 *) & empty_px_values[2],
          (UVfloor ((-bb))) * Vwidth);
    }
    if (sumbuff) {
      oil_copy_u8 (Udest, Utemp, Uwidth);
      oil_copy_u8 (Vdest, Vtemp, Vwidth);
    }
  }

  GST_LOG ("image created");
  g_free (Utemp);
  g_free (Vtemp);
1049 1050 1051
}

static void
1052
gst_video_box_i420_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
1053 1054 1055
{
  guint8 *srcY, *srcU, *srcV;
  gint crop_width, crop_width2, crop_height;
1056
  gint out_width, out_height;
1057
  gint src_stridey, src_strideu, src_stridev;
1058 1059 1060 1061 1062
  gint br, bl, bt, bb;
  gint colorY, colorU, colorV;
  gint i, j;
  guint8 b_alpha = (guint8) (video_box->border_alpha * 255);
  guint8 i_alpha = (guint8) (video_box->alpha * 255);
1063 1064
  guint32 *destp;
  guint32 *destb = (guint32 *) dest;
1065 1066 1067 1068 1069 1070 1071 1072
  guint32 ayuv;

  br = video_box->border_right;
  bl = video_box->border_left;
  bt = video_box->border_top;
  bb = video_box->border_bottom;

  out_width = video_box->out_width;
1073
  out_height = video_box->out_height;
1074

1075 1076 1077
  src_stridey = GST_VIDEO_I420_Y_ROWSTRIDE (video_box->in_width);
  src_strideu = GST_VIDEO_I420_U_ROWSTRIDE (video_box->in_width);
  src_stridev = GST_VIDEO_I420_V_ROWSTRIDE (video_box->in_width);
1078

1079 1080
  crop_width = video_box->in_width;
  crop_width -= (video_box->crop_left + video_box->crop_right);
1081
  crop_width2 = crop_width / 2;
1082 1083
  crop_height = video_box->in_height;
  crop_height -= (video_box->crop_top + video_box->crop_bottom);
1084 1085 1086

  srcY =
      src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
1087
  srcY += src_stridey * video_box->crop_top + video_box->crop_left;
1088 1089
  srcU =
      src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
1090
  srcU += src_strideu * (video_box->crop_top / 2) + (video_box->crop_left / 2);
1091 1092
  srcV =
      src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
1093
  srcV += src_stridev * (video_box->crop_top / 2) + (video_box->crop_left / 2);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103

  colorY = yuv_colors_Y[video_box->fill_type];
  colorU = yuv_colors_U[video_box->fill_type];
  colorV = yuv_colors_V[video_box->fill_type];

  ayuv =
      GUINT32_FROM_BE ((b_alpha << 24) | (colorY << 16) | (colorU << 8) |
      colorV);

  /* top border */
1104 1105 1106
  if (bt) {
    size_t nb_pixels = bt * out_width;

1107 1108
    oil_splat_u32_ns (destb, &ayuv, nb_pixels);
    destb += nb_pixels;
1109 1110
  }
  for (i = 0; i < crop_height; i++) {
1111
    destp = destb;
1112
    /* left border */
1113 1114 1115
    if (bl) {
      oil_splat_u32_ns (destp, &ayuv, bl);
      destp += bl;
1116 1117 1118
    }
    dest = (guint8 *) destp;
    /* center */
1119 1120
    /* We can splat the alpha channel for the whole line */
    oil_splat_u8 (dest, 4, &i_alpha, crop_width);
1121
    for (j = 0; j < crop_width2; j++) {
1122
      dest++;
1123 1124 1125
      *dest++ = *srcY++;
      *dest++ = *srcU;
      *dest++ = *srcV;
1126
      dest++;
1127 1128 1129 1130 1131 1132 1133 1134
      *dest++ = *srcY++;
      *dest++ = *srcU++;
      *dest++ = *srcV++;
    }
    if (i % 2 == 0) {
      srcU -= crop_width2;
      srcV -= crop_width2;
    } else {
1135 1136
      srcU += src_strideu - crop_width2;
      srcV += src_stridev - crop_width2;
1137
    }
1138
    srcY += src_stridey - (crop_width2 * 2);
1139 1140 1141

    destp = (guint32 *) dest;
    /* right border */
1142 1143
    if (br) {
      oil_splat_u32_ns (destp, &ayuv, br);
1144
    }
1145
    destb += out_width;
1146 1147
  }
  /* bottom border */
1148 1149 1150
  if (bb) {
    size_t nb_pixels = bb * out_width;

1151
    oil_splat_u32_ns (destb, &ayuv, nb_pixels);
1152 1153 1154
  }
}

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

static void
gst_video_box_i420_i420 (GstVideoBox * video_box, guint8 * src, guint8 * dest)
{
  guint8 *srcY, *srcU, *srcV;
  guint8 *destY, *destU, *destV;
  gint crop_width, crop_height;
  gint out_width, out_height;
  gint src_width, src_height;
  gint src_stride, dest_stride;
  gint br, bl, bt, bb;

  br = video_box->border_right;
  bl = video_box->border_left;
  bt = video_box->border_top;
  bb = video_box->border_bottom;

  out_width = video_box->out_width;
  out_height = video_box->out_height;

  src_width = video_box->in_width;
  src_height = video_box->in_height;

  crop_width = src_width - (video_box->crop_left + video_box->crop_right);
  crop_height = src_height - (video_box->crop_top + video_box->crop_bottom);

  /* Y plane */
  src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (src_width);
  dest_stride = GST_VIDEO_I420_Y_ROWSTRIDE (out_width);

  destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);

  srcY = src + GST_VIDEO_I420_Y_OFFSET (src_width, src_height);
  srcY += src_stride * video_box->crop_top + video_box->crop_left;

  gst_video_box_copy_plane_i420 (video_box, srcY, destY, br, bl, bt, bb,
      crop_width, crop_height, src_stride, out_width, dest_stride,
      yuv_colors_Y[video_box->fill_type]);

1194 1195 1196 1197 1198 1199 1200 1201 1202
  br /= 2;
  bb /= 2;
  bl /= 2;
  bt /= 2;

  /* we need to round up to make sure we draw all the U and V lines */
  crop_width = (crop_width + 1) / 2;
  crop_height = (crop_height + 1) / 2;

1203 1204 1205 1206 1207 1208 1209 1210 1211
  /* U plane */
  src_stride = GST_VIDEO_I420_U_ROWSTRIDE (src_width);
  dest_stride = GST_VIDEO_I420_U_ROWSTRIDE (out_width);

  destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);

  srcU = src + GST_VIDEO_I420_U_OFFSET (src_width, src_height);
  srcU += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);

1212 1213 1214
  gst_video_box_copy_plane_i420 (video_box, srcU, destU, br, bl, bt, bb,
      crop_width, crop_height, src_stride, out_width / 2, dest_stride,
      yuv_colors_U[video_box->fill_type]);
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224

  /* V plane */
  src_stride = GST_VIDEO_I420_V_ROWSTRIDE (src_width);
  dest_stride = GST_VIDEO_I420_V_ROWSTRIDE (out_width);

  destV = dest + GST_VIDEO_I420_V_OFFSET (out_width, out_height);

  srcV = src + GST_VIDEO_I420_V_OFFSET (src_width, src_height);
  srcV += src_stride * (video_box->crop_top / 2) + (video_box->crop_left / 2);

1225 1226 1227
  gst_video_box_copy_plane_i420 (video_box, srcV, destV, br, bl, bt, bb,
      crop_width, crop_height, src_stride, out_width / 2, dest_stride,
      yuv_colors_V[video_box->fill_type]);
1228 1229
}

Wim Taymans's avatar
Wim Taymans committed
1230
static GstFlowReturn
1231
gst_video_box_transform (GstBaseTransform * trans, GstBuffer * in,
Wim Taymans's avatar
Wim Taymans committed
1232
    GstBuffer * out)
1233 1234
{
  GstVideoBox *video_box;
1235
  guint8 *indata, *outdata;
1236

1237
  video_box = GST_VIDEO_BOX (trans);
1238

1239 1240
  indata = GST_BUFFER_DATA (in);
  outdata = GST_BUFFER_DATA (out);
1241

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
  switch (video_box->in_fourcc) {
    case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
      switch (video_box->out_fourcc) {
        case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
          gst_video_box_ayuv_ayuv (video_box, indata, outdata);
          break;
        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
          gst_video_box_ayuv_i420 (video_box, indata, outdata);
          break;
        default:
          goto invalid_format;
      }
      break;
    case GST_MAKE_FOURCC ('I', '4', '2', '0'):
      switch (video_box->out_fourcc) {
        case GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'):
          gst_video_box_i420_ayuv (video_box, indata, outdata);
          break;
        case GST_MAKE_FOURCC ('I', '4', '2', '0'):
          gst_video_box_i420_i420 (video_box, indata, outdata);
          break;
        default:
          goto invalid_format;
      }
      break;
    default:
      goto invalid_format;
  }
Wim Taymans's avatar
Wim Taymans committed
1270
  return GST_FLOW_OK;
1271 1272 1273 1274 1275 1276

  /* ERRORS */
invalid_format:
  {
    return GST_FLOW_ERROR;
  }
1277 1278
}

1279
/* FIXME: 0.11 merge with videocrop plugin */
1280 1281 1282
static gboolean
plugin_init (GstPlugin * plugin)
{
1283 1284
  oil_init ();

1285 1286 1287 1288 1289 1290 1291 1292
  return gst_element_register (plugin, "videobox", GST_RANK_NONE,
      GST_TYPE_VIDEO_BOX);
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "videobox",
    "resizes a video by adding borders or cropping",
1293
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)