gstvideobox.c 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* 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.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gst/gst.h>
24
#include <gst/base/gstbasetransform.h>
25 26
#include <gst/video/video.h>

27
#include <liboil/liboil.h>
28 29
#include <string.h>

30
GST_DEBUG_CATEGORY_STATIC (videobox_debug);
31 32
#define GST_CAT_DEFAULT videobox_debug

33 34 35 36 37 38 39 40
#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
41
#define GST_IS_VIDEO_BOX_CLASS(klass) \
42 43 44 45 46 47 48 49 50 51
  (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,
52
  VIDEO_BOX_FILL_LAST
53 54 55 56 57
}
GstVideoBoxFill;

struct _GstVideoBox
{
58
  GstBaseTransform element;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

  /* caps */
  gint in_width, in_height;
  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;

  gboolean use_alpha;
  gdouble alpha;
  gdouble border_alpha;

  GstVideoBoxFill fill_type;
};

struct _GstVideoBoxClass
{
78
  GstBaseTransformClass parent_class;
79 80 81
};

/* elementfactory information */
82
static const GstElementDetails gst_video_box_details =
Wim Taymans's avatar
Wim Taymans committed
83
GST_ELEMENT_DETAILS ("Video box filter",
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    "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
{
99 100 101 102 103 104 105
  PROP_0,
  PROP_LEFT,
  PROP_RIGHT,
  PROP_TOP,
  PROP_BOTTOM,
  PROP_FILL_TYPE,
  PROP_ALPHA,
106 107
  PROP_BORDER_ALPHA
      /* FILL ME */
108 109 110 111 112 113
};

static GstStaticPadTemplate gst_video_box_src_template =
GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
114
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ AYUV, I420 }"))
115 116 117 118 119 120 121 122 123 124
    );

static GstStaticPadTemplate gst_video_box_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("I420"))
    );


125 126
GST_BOILERPLATE (GstVideoBox, gst_video_box, GstBaseTransform,
    GST_TYPE_BASE_TRANSFORM);
127 128 129 130 131 132

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);

133
static GstCaps *gst_video_box_transform_caps (GstBaseTransform * trans,
134
    GstPadDirection direction, GstCaps * from);
135 136
static gboolean gst_video_box_set_caps (GstBaseTransform * trans,
    GstCaps * in, GstCaps * out);
137 138
static gboolean gst_video_box_get_unit_size (GstBaseTransform * trans,
    GstCaps * caps, guint * size);
139
static GstFlowReturn gst_video_box_transform (GstBaseTransform * trans,
Wim Taymans's avatar
Wim Taymans committed
140
    GstBuffer * in, GstBuffer * out);
141 142 143 144 145 146 147


#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;
148
  static const GEnumValue video_box_fill[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
149 150 151
    {VIDEO_BOX_FILL_BLACK, "Black", "black"},
    {VIDEO_BOX_FILL_GREEN, "Colorkey green", "green"},
    {VIDEO_BOX_FILL_BLUE, "Colorkey blue", "blue"},
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    {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));
}
175

176 177 178 179
static void
gst_video_box_class_init (GstVideoBoxClass * klass)
{
  GObjectClass *gobject_class;
180
  GstBaseTransformClass *trans_class;
181 182

  gobject_class = (GObjectClass *) klass;
183
  trans_class = (GstBaseTransformClass *) klass;
184

Wim Taymans's avatar
Wim Taymans committed
185 186 187
  gobject_class->set_property = gst_video_box_set_property;
  gobject_class->get_property = gst_video_box_get_property;

188
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILL_TYPE,
189 190 191
      g_param_spec_enum ("fill", "Fill", "How to fill the borders",
          GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
          (GParamFlags) G_PARAM_READWRITE));
192
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEFT,
193 194 195
      g_param_spec_int ("left", "Left",
          "Pixels to box at left (<0  = add a border)", G_MININT, G_MAXINT,
          DEFAULT_LEFT, G_PARAM_READWRITE));
196
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RIGHT,
197 198 199
      g_param_spec_int ("right", "Right",
          "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
          DEFAULT_RIGHT, G_PARAM_READWRITE));
200
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOP,
201 202 203
      g_param_spec_int ("top", "Top",
          "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
          DEFAULT_TOP, G_PARAM_READWRITE));
204
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BOTTOM,
205 206 207
      g_param_spec_int ("bottom", "Bottom",
          "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
          DEFAULT_BOTTOM, G_PARAM_READWRITE));
208
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALPHA,
209 210
      g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
          DEFAULT_ALPHA, G_PARAM_READWRITE));
211
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER_ALPHA,
212 213 214 215
      g_param_spec_double ("border_alpha", "Border Alpha",
          "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
          G_PARAM_READWRITE));

216 217 218 219 220
  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);
  trans_class->transform = GST_DEBUG_FUNCPTR (gst_video_box_transform);
221 222 223

  GST_DEBUG_CATEGORY_INIT (videobox_debug, "videobox", 0,
      "Resizes a video by adding borders or cropping");
224 225

  oil_init ();
226 227 228
}

static void
229
gst_video_box_init (GstVideoBox * video_box, GstVideoBoxClass * g_class)
230 231 232 233 234
{
  video_box->box_right = DEFAULT_RIGHT;
  video_box->box_left = DEFAULT_LEFT;
  video_box->box_top = DEFAULT_TOP;
  video_box->box_bottom = DEFAULT_BOTTOM;
235 236 237 238
  video_box->crop_right = 0;
  video_box->crop_left = 0;
  video_box->crop_top = 0;
  video_box->crop_bottom = 0;
239 240 241 242 243 244 245 246 247
  video_box->fill_type = DEFAULT_FILL_TYPE;
  video_box->alpha = DEFAULT_ALPHA;
  video_box->border_alpha = DEFAULT_BORDER_ALPHA;
}

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

  switch (prop_id) {
251
    case PROP_LEFT:
252 253 254 255 256 257 258 259 260
      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;
261
    case PROP_RIGHT:
262 263 264 265 266 267 268 269 270
      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;
271
    case PROP_TOP:
272 273 274 275 276 277 278 279 280
      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;
281
    case PROP_BOTTOM:
282 283 284 285 286 287 288 289 290
      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;
291
    case PROP_FILL_TYPE:
292 293
      video_box->fill_type = g_value_get_enum (value);
      break;
294
    case PROP_ALPHA:
295 296
      video_box->alpha = g_value_get_double (value);
      break;
297
    case PROP_BORDER_ALPHA:
298 299 300 301 302 303 304
      video_box->border_alpha = g_value_get_double (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}
305

306 307 308 309
static void
gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
310
  GstVideoBox *video_box = GST_VIDEO_BOX (object);
311 312

  switch (prop_id) {
313
    case PROP_LEFT:
314 315
      g_value_set_int (value, video_box->box_left);
      break;
316
    case PROP_RIGHT:
317 318
      g_value_set_int (value, video_box->box_right);
      break;
319
    case PROP_TOP:
320 321
      g_value_set_int (value, video_box->box_top);
      break;
322
    case PROP_BOTTOM:
323 324
      g_value_set_int (value, video_box->box_bottom);
      break;
325
    case PROP_FILL_TYPE:
326 327
      g_value_set_enum (value, video_box->fill_type);
      break;
328
    case PROP_ALPHA:
329 330
      g_value_set_double (value, video_box->alpha);
      break;
331
    case PROP_BORDER_ALPHA:
332 333 334 335 336 337 338 339
      g_value_set_double (value, video_box->border_alpha);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

340
static GstCaps *
341 342
gst_video_box_transform_caps (GstBaseTransform * trans,
    GstPadDirection direction, GstCaps * from)
343 344 345
{
  GstVideoBox *video_box;
  GstCaps *to;
346 347 348 349
  GstStructure *structure;
  GValue list_value = { 0 }, value = {
  0};
  gint dir, i, tmp;
350 351

  video_box = GST_VIDEO_BOX (trans);
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  g_value_init (&list_value, GST_TYPE_LIST);
  g_value_init (&value, GST_TYPE_FOURCC);
  gst_value_set_fourcc (&value, GST_MAKE_FOURCC ('I', '4', '2', '0'));
  gst_value_list_append_value (&list_value, &value);
  g_value_unset (&value);

  to = gst_caps_copy (from);
  dir = (direction == GST_PAD_SINK) ? -1 : 1;

  for (i = 0; i < gst_caps_get_size (to); i++) {
    structure = gst_caps_get_structure (to, i);
    if (direction == GST_PAD_SINK) {    /* I420 to { I420, AYUV } */
      g_value_init (&value, GST_TYPE_FOURCC);
      gst_value_set_fourcc (&value, GST_MAKE_FOURCC ('A', 'Y', 'U', 'V'));
      gst_value_list_append_value (&list_value, &value);
      g_value_unset (&value);
      gst_structure_set_value (structure, "format", &list_value);
    } else if (direction == GST_PAD_SRC) {
      gst_structure_set_value (structure, "format", &list_value);
372
    }
373 374
    if (gst_structure_get_int (structure, "width", &tmp))
      gst_structure_set (structure, "width", G_TYPE_INT,
375
          tmp + dir * (video_box->box_left + video_box->box_right), NULL);
376 377
    if (gst_structure_get_int (structure, "height", &tmp))
      gst_structure_set (structure, "height", G_TYPE_INT,
378
          tmp + dir * (video_box->box_top + video_box->box_bottom), NULL);
379 380
  }

381 382
  g_value_unset (&list_value);

383 384
  GST_DEBUG_OBJECT (video_box, "direction %d, transformed %" GST_PTR_FORMAT
      " to %" GST_PTR_FORMAT, direction, from, to);
385

386 387 388
  return to;
}

Wim Taymans's avatar
Wim Taymans committed
389
static gboolean
390
gst_video_box_set_caps (GstBaseTransform * trans, GstCaps * in, GstCaps * out)
391 392 393 394
{
  GstVideoBox *video_box;
  GstStructure *structure;
  gboolean ret;
395
  guint32 fourcc = 0;
396

397
  video_box = GST_VIDEO_BOX (trans);
398

399
  structure = gst_caps_get_structure (in, 0);
400 401 402
  ret = gst_structure_get_int (structure, "width", &video_box->in_width);
  ret &= gst_structure_get_int (structure, "height", &video_box->in_height);

403 404 405
  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);
406 407
  ret &= gst_structure_get_fourcc (structure, "format", &fourcc);

408 409 410 411 412 413 414 415 416 417 418 419 420
  if (fourcc == GST_STR_FOURCC ("AYUV")) {
    video_box->use_alpha = TRUE;
  } else {
    if (video_box->box_left == 0 && video_box->box_right == 0 &&
        video_box->box_top == 0 && video_box->box_bottom == 0) {
      gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (video_box), TRUE);
      GST_LOG ("we are using passthrough");
    } else {
      gst_base_transform_set_passthrough (GST_BASE_TRANSFORM (video_box),
          FALSE);
      GST_LOG ("we are not using passthrough");
    }
  }
421

Wim Taymans's avatar
Wim Taymans committed
422
  return ret;
423 424
}

425
/* see gst-plugins/gst/games/gstvideoimage.c, paint_setup_I420() */
426 427 428
#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)
429

430
#define GST_VIDEO_I420_Y_OFFSET(w,h) (0)
431 432
#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))
433

434
#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))
435

436 437 438
static gboolean
gst_video_box_get_unit_size (GstBaseTransform * trans, GstCaps * caps,
    guint * size)
Wim Taymans's avatar
Wim Taymans committed
439 440
{
  GstVideoBox *video_box;
441 442 443
  GstStructure *structure = NULL;
  guint32 fourcc;
  gint width, height;
Wim Taymans's avatar
Wim Taymans committed
444

Stefan Kost's avatar
Stefan Kost committed
445 446
  g_assert (size);

Wim Taymans's avatar
Wim Taymans committed
447 448
  video_box = GST_VIDEO_BOX (trans);

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  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
464
  }
465

466
  return TRUE;
Wim Taymans's avatar
Wim Taymans committed
467 468
}

469 470 471
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 };
472

473 474 475 476 477 478 479 480 481 482
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++) {
483
    oil_splat_u8_ns (dest, &fill_color, dest_width);
484 485 486 487 488
    dest += dest_stride;
  }

  /* copy and add left and right border */
  for (j = 0; j < src_crop_height; j++) {
489 490 491
    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);
492 493 494 495 496 497
    dest += dest_stride;
    src += src_stride;
  }

  /* bottom border */
  for (j = 0; j < bb; j++) {
498
    oil_splat_u8_ns (dest, &fill_color, dest_width);
499 500 501 502
    dest += dest_stride;
  }
}

503 504 505 506 507 508 509
static void
gst_video_box_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;
510 511
  gint src_width, src_height;
  gint src_stride, dest_stride;
512 513 514 515 516 517 518 519 520 521
  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;

522 523
  src_width = video_box->in_width;
  src_height = video_box->in_height;
524

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

528 529 530
  /* Y plane */
  src_stride = GST_VIDEO_I420_Y_ROWSTRIDE (src_width);
  dest_stride = GST_VIDEO_I420_Y_ROWSTRIDE (out_width);
531

532
  destY = dest + GST_VIDEO_I420_Y_OFFSET (out_width, out_height);
533

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

537 538 539
  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]);
540

541 542 543
  /* U plane */
  src_stride = GST_VIDEO_I420_U_ROWSTRIDE (src_width);
  dest_stride = GST_VIDEO_I420_U_ROWSTRIDE (out_width);
544 545 546

  destU = dest + GST_VIDEO_I420_U_OFFSET (out_width, out_height);

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

550 551 552
  gst_video_box_copy_plane_i420 (video_box, srcU, destU, br / 2, bl / 2, bt / 2,
      bb / 2, crop_width / 2, crop_height / 2, src_stride, out_width / 2,
      dest_stride, yuv_colors_U[video_box->fill_type]);
553

554 555 556 557 558 559 560 561 562 563 564 565
  /* 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);

  gst_video_box_copy_plane_i420 (video_box, srcV, destV, br / 2, bl / 2, bt / 2,
      bb / 2, crop_width / 2, crop_height / 2, src_stride, out_width / 2,
      dest_stride, yuv_colors_V[video_box->fill_type]);
566 567
}

568 569
/* Note the source image is always I420, we
 * are converting to AYUV on the fly here */
570 571 572 573 574 575
static void
gst_video_box_ayuv (GstVideoBox * video_box, guint8 * src, guint8 * dest)
{
  guint8 *srcY, *srcU, *srcV;
  gint crop_width, crop_width2, crop_height;
  gint out_width, out_height;
576
  gint src_stridey, src_strideu, src_stridev;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
  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);
  guint32 *destp = (guint32 *) dest;
  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;
  out_height = video_box->out_height;

593 594 595
  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);
596 597 598 599 600 601 602 603 604

  crop_width =
      video_box->in_width - (video_box->crop_left + video_box->crop_right);
  crop_width2 = crop_width / 2;
  crop_height =
      video_box->in_height - (video_box->crop_top + video_box->crop_bottom);

  srcY =
      src + GST_VIDEO_I420_Y_OFFSET (video_box->in_width, video_box->in_height);
605
  srcY += src_stridey * video_box->crop_top + video_box->crop_left;
606 607
  srcU =
      src + GST_VIDEO_I420_U_OFFSET (video_box->in_width, video_box->in_height);
608
  srcU += src_strideu * (video_box->crop_top / 2) + (video_box->crop_left / 2);
609 610
  srcV =
      src + GST_VIDEO_I420_V_OFFSET (video_box->in_width, video_box->in_height);
611
  srcV += src_stridev * (video_box->crop_top / 2) + (video_box->crop_left / 2);
612 613 614 615 616 617 618 619 620 621

  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 */
622 623 624 625 626
  if (bt) {
    size_t nb_pixels = bt * out_width;

    oil_splat_u32_ns (destp, &ayuv, nb_pixels);
    destp += nb_pixels;
627 628 629
  }
  for (i = 0; i < crop_height; i++) {
    /* left border */
630 631 632
    if (bl) {
      oil_splat_u32_ns (destp, &ayuv, bl);
      destp += bl;
633 634 635
    }
    dest = (guint8 *) destp;
    /* center */
636 637
    /* We can splat the alpha channel for the whole line */
    oil_splat_u8 (dest, 4, &i_alpha, crop_width);
638
    for (j = 0; j < crop_width2; j++) {
639
      dest++;
640 641 642
      *dest++ = *srcY++;
      *dest++ = *srcU;
      *dest++ = *srcV;
643
      dest++;
644 645 646 647 648 649 650 651
      *dest++ = *srcY++;
      *dest++ = *srcU++;
      *dest++ = *srcV++;
    }
    if (i % 2 == 0) {
      srcU -= crop_width2;
      srcV -= crop_width2;
    } else {
652 653
      srcU += src_strideu - crop_width2;
      srcV += src_stridev - crop_width2;
654
    }
655
    srcY += src_stridey - crop_width;
656 657 658

    destp = (guint32 *) dest;
    /* right border */
659 660 661
    if (br) {
      oil_splat_u32_ns (destp, &ayuv, br);
      destp += br;
662 663 664
    }
  }
  /* bottom border */
665 666 667 668 669
  if (bb) {
    size_t nb_pixels = bb * out_width;

    oil_splat_u32_ns (destp, &ayuv, nb_pixels);
    destp += nb_pixels;
670 671 672
  }
}

Wim Taymans's avatar
Wim Taymans committed
673
static GstFlowReturn
674
gst_video_box_transform (GstBaseTransform * trans, GstBuffer * in,
Wim Taymans's avatar
Wim Taymans committed
675
    GstBuffer * out)
676 677 678
{
  GstVideoBox *video_box;

679
  video_box = GST_VIDEO_BOX (trans);
680 681

  if (video_box->use_alpha) {
Wim Taymans's avatar
Wim Taymans committed
682
    gst_video_box_ayuv (video_box, GST_BUFFER_DATA (in), GST_BUFFER_DATA (out));
683
  } else {
Wim Taymans's avatar
Wim Taymans committed
684
    gst_video_box_i420 (video_box, GST_BUFFER_DATA (in), GST_BUFFER_DATA (out));
685 686
  }

Wim Taymans's avatar
Wim Taymans committed
687
  return GST_FLOW_OK;
688 689 690 691 692
}

static gboolean
plugin_init (GstPlugin * plugin)
{
693

694 695 696 697 698 699 700 701
  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",
702
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)