gstvideobox.c 57.8 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3
 * Copyright (C) <2010> Sebastian Dröge <sebastian.droege@collabora.co.uk>
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 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 43 44 45 46 47 48 49 50 51 52 53
 *
 * 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
 * ]|
54
 */
55

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

#include "gstvideobox.h"

62
#include <math.h>
63
#include <liboil/liboil.h>
64 65
#include <string.h>

66 67
#include <gst/controller/gstcontroller.h>

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

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/* From videotestsrc.c */
static const guint8 yuv_sdtv_colors_Y[VIDEO_BOX_FILL_LAST] = { 16, 145, 41 };
static const guint8 yuv_sdtv_colors_U[VIDEO_BOX_FILL_LAST] = { 128, 54, 240 };
static const guint8 yuv_sdtv_colors_V[VIDEO_BOX_FILL_LAST] = { 128, 34, 110 };

static const guint8 yuv_hdtv_colors_Y[VIDEO_BOX_FILL_LAST] = { 16, 173, 32 };
static const guint8 yuv_hdtv_colors_U[VIDEO_BOX_FILL_LAST] = { 128, 42, 240 };
static const guint8 yuv_hdtv_colors_V[VIDEO_BOX_FILL_LAST] = { 128, 26, 118 };

static const guint8 rgb_colors_R[VIDEO_BOX_FILL_LAST] = { 0, 255, 0 };
static const guint8 rgb_colors_G[VIDEO_BOX_FILL_LAST] = { 0, 0, 0 };
static const guint8 rgb_colors_B[VIDEO_BOX_FILL_LAST] = { 0, 255, 255 };

/* Generated by -bad/ext/cog/generate_tables */
static const int cog_ycbcr_to_rgb_matrix_8bit_hdtv[] = {
  298, 0, 459, -63514,
  298, -55, -136, 19681,
  298, 541, 0, -73988,
};

static const int cog_ycbcr_to_rgb_matrix_8bit_sdtv[] = {
  298, 0, 409, -57068,
  298, -100, -208, 34707,
  298, 516, 0, -70870,
};

static const gint cog_rgb_to_ycbcr_matrix_8bit_hdtv[] = {
  47, 157, 16, 4096,
  -26, -87, 112, 32768,
  112, -102, -10, 32768,
};

static const gint cog_rgb_to_ycbcr_matrix_8bit_sdtv[] = {
  66, 129, 25, 4096,
  -38, -74, 112, 32768,
  112, -94, -18, 32768,
};

static const gint cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit[] = {
  256, -30, -53, 10600,
  0, 261, 29, -4367,
  0, 19, 262, -3289,
};

static const gint cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit[] = {
  256, 25, 49, -9536,
  0, 253, -28, 3958,
  0, -19, 252, 2918,
};

#define APPLY_MATRIX(m,o,v1,v2,v3) ((m[o*4] * v1 + m[o*4+1] * v2 + m[o*4+2] * v3 + m[o*4+3]) >> 8)

static void
124 125
fill_ayuv (GstVideoBoxFill fill_type, guint b_alpha, GstVideoFormat format,
    guint8 * dest, gboolean sdtv, gint width, gint height)
126 127 128 129 130 131 132 133 134 135 136 137
{
  guint32 empty_pixel;

  if (sdtv)
    empty_pixel = GUINT32_FROM_BE ((b_alpha << 24) |
        (yuv_sdtv_colors_Y[fill_type] << 16) |
        (yuv_sdtv_colors_U[fill_type] << 8) | yuv_sdtv_colors_V[fill_type]);
  else
    empty_pixel = GUINT32_FROM_BE ((b_alpha << 24) |
        (yuv_hdtv_colors_Y[fill_type] << 16) |
        (yuv_hdtv_colors_U[fill_type] << 8) | yuv_hdtv_colors_V[fill_type]);

138
  oil_splat_u32_ns ((guint32 *) dest, &empty_pixel, width * height);
139 140 141
}

static void
142 143 144 145 146
copy_ayuv_ayuv (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
{
  gint i, j;
  gint src_stride = 4 * src_width;
  gint dest_stride = 4 * dest_width;

  dest = dest + dest_y * dest_width * 4 + dest_x * 4;
  src = src + src_y * src_width * 4 + src_x * 4;

  w *= 4;

  if (dest_sdtv != src_sdtv) {
    gint matrix[12];
    gint y, u, v;

    memcpy (matrix,
        dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
        cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));

    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        dest[j] = (src[j] * i_alpha) >> 8;
        y = src[j + 1];
        u = src[j + 2];
        v = src[j + 3];
        dest[j + 1] = APPLY_MATRIX (matrix, 0, y, u, v);
        dest[j + 2] = APPLY_MATRIX (matrix, 1, y, u, v);
        dest[j + 3] = APPLY_MATRIX (matrix, 2, y, u, v);
      }
      dest += dest_stride;
      src += src_stride;
    }
  } else {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        dest[j] = (src[j] * i_alpha) >> 8;
        dest[j + 1] = src[j + 1];
        dest[j + 2] = src[j + 2];
        dest[j + 3] = src[j + 3];
      }
      dest += dest_stride;
      src += src_stride;
    }
  }
}

static void
193 194 195 196 197
copy_ayuv_i420 (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
  gint i, j;
  guint8 *destY, *destU, *destV;
  gint dest_strideY, dest_strideUV;
  gint widthY, widthUV;
  gint hY, hUV;

  dest_strideY =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, dest_width);
  dest_strideUV =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, dest_width);

  destY =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 0,
      dest_width, dest_height);
  destU =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 1,
      dest_width, dest_height);
  destV =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 2,
      dest_width, dest_height);

  destY = destY + dest_y * dest_strideY + dest_x;
  destU = destU + (dest_y / 2) * dest_strideUV + dest_x / 2;
  destV = destV + (dest_y / 2) * dest_strideUV + dest_x / 2;

  src = src + src_y * src_width * 4 + src_x * 4;

  widthY = w;
227
  widthUV = w / 2;
228 229

  hY = h;
230
  hUV = h / 2;
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

  if (src_sdtv != dest_sdtv) {
    gint matrix[12];
    gint y1, y2, y3, y4;
    gint u1, u2, u3, u4;
    gint v1, v2, v3, v4;
    guint8 *destY2 = destY + dest_strideY;
    const guint8 *src2 = src + src_width * 4;

    dest_strideY *= 2;

    memcpy (matrix,
        dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
        cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));

    for (i = 0; i < hUV; i++) {
      if (i * 2 == hY) {
        destY2 = destY;
      }

      for (j = 0; j < widthUV; j++) {
        y1 = src[8 * j + 1];
        u1 = src[8 * j + 2];
        v1 = src[8 * j + 3];
        y2 = src[8 * j + 5];
        u2 = src[8 * j + 6];
        v2 = src[8 * j + 7];

        if (j * 2 < widthY) {
          y3 = src2[8 * j + 1];
          u3 = src2[8 * j + 2];
          v3 = src2[8 * j + 3];
          y4 = src2[8 * j + 5];
          u4 = src2[8 * j + 6];
          v4 = src2[8 * j + 7];
        } else {
          y3 = y1;
          u3 = u1;
          v3 = v1;
          y4 = y2;
          u4 = u2;
          v4 = v2;
        }

        y1 = APPLY_MATRIX (matrix, 0, y1, u1, v1);
        u1 = APPLY_MATRIX (matrix, 1, y1, u1, v1);
        v1 = APPLY_MATRIX (matrix, 2, y1, u1, v1);

        y3 = APPLY_MATRIX (matrix, 0, y3, u3, v3);
        u3 = APPLY_MATRIX (matrix, 1, y3, u3, v3);
        v3 = APPLY_MATRIX (matrix, 2, y3, u3, v3);

        if (j * 2 < widthY) {
          y2 = APPLY_MATRIX (matrix, 0, y2, u2, v2);
          u2 = APPLY_MATRIX (matrix, 1, y2, u2, v2);
          v2 = APPLY_MATRIX (matrix, 2, y2, u2, v2);

          y4 = APPLY_MATRIX (matrix, 0, y4, u4, v4);
          u4 = APPLY_MATRIX (matrix, 1, y4, u4, v4);
          v4 = APPLY_MATRIX (matrix, 2, y4, u4, v4);
        } else {
          u2 = u1;
          v2 = v1;
          u4 = u3;
          v4 = v3;
        }

        destY[2 * j] = y1;
        destY2[2 * j] = y3;
        if (j * 2 < widthY) {
          destY[2 * j + 1] = y2;
          destY2[2 * j + 1] = y4;
        }

        destU[j] = (u1 + u2 + u3 + u4) / 4;
        destV[j] = (v1 + v2 + v3 + v4) / 4;
      }
      src += src_width * 8;
      destY += dest_strideY;
      src2 += src_width * 8;
      destY2 += dest_strideY;

      destU += dest_strideUV;
      destV += dest_strideUV;
    }
  } else {
    gint y1, y2, y3, y4;
    gint u1, u2, u3, u4;
    gint v1, v2, v3, v4;
    guint8 *destY2 = destY + dest_strideY;
    const guint8 *src2 = src + src_width * 4;

    dest_strideY *= 2;

    for (i = 0; i < hUV; i++) {
      if (i * 2 == hY) {
        destY2 = destY;
      }

      for (j = 0; j < widthUV; j++) {
        y1 = src[8 * j + 1];
        u1 = src[8 * j + 2];
        v1 = src[8 * j + 3];
        y2 = src[8 * j + 5];
        u2 = src[8 * j + 6];
        v2 = src[8 * j + 7];

        if (j * 2 < widthY) {
          y3 = src2[8 * j + 1];
          u3 = src2[8 * j + 2];
          v3 = src2[8 * j + 3];
          y4 = src2[8 * j + 5];
          u4 = src2[8 * j + 6];
          v4 = src2[8 * j + 7];
        } else {
          y3 = y1;
          u3 = u1;
          v3 = v1;
          y4 = y2;
          u4 = u2;
          v4 = v2;
        }

        destY[2 * j] = y1;
        destY2[2 * j] = y3;
        if (j * 2 < widthY) {
          destY[2 * j + 1] = y2;
          destY2[2 * j + 1] = y4;
        }

        destU[j] = (u1 + u2 + u3 + u4) / 4;
        destV[j] = (v1 + v2 + v3 + v4) / 4;
      }
      src += src_width * 8;
      destY += dest_strideY;
      src2 += src_width * 8;
      destY2 += dest_strideY;

      destU += dest_strideUV;
      destV += dest_strideUV;
    }
  }
}

static void
376 377
fill_i420 (GstVideoBoxFill fill_type, guint b_alpha, GstVideoFormat format,
    guint8 * dest, gboolean sdtv, gint width, gint height)
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 407 408 409 410 411
{
  guint8 empty_pixel[3];
  guint8 *destY, *destU, *destV;
  gint strideY, strideUV;
  gint heightY, heightUV;

  if (sdtv) {
    empty_pixel[0] = yuv_sdtv_colors_Y[fill_type];
    empty_pixel[1] = yuv_sdtv_colors_U[fill_type];
    empty_pixel[2] = yuv_sdtv_colors_V[fill_type];
  } else {
    empty_pixel[0] = yuv_hdtv_colors_Y[fill_type];
    empty_pixel[1] = yuv_hdtv_colors_U[fill_type];
    empty_pixel[2] = yuv_hdtv_colors_V[fill_type];
  }

  strideY = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, width);
  strideUV = gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, width);

  destY =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 0,
      width, height);
  destU =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 1,
      width, height);
  destV =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 2,
      width, height);

  heightY =
      gst_video_format_get_component_height (GST_VIDEO_FORMAT_I420, 0, height);
  heightUV =
      gst_video_format_get_component_height (GST_VIDEO_FORMAT_I420, 1, height);

412 413 414
  oil_splat_u8_ns (destY, &empty_pixel[0], strideY * heightY);
  oil_splat_u8_ns (destU, &empty_pixel[1], strideUV * heightUV);
  oil_splat_u8_ns (destV, &empty_pixel[2], strideUV * heightUV);
415 416 417
}

static void
418 419 420 421 422
copy_i420_i420 (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
{
  gint i;
  guint8 *destY, *destU, *destV;
  const guint8 *srcY, *srcU, *srcV;
  gint dest_strideY, dest_strideUV;
  gint src_strideY, src_strideUV;
  gint widthY, widthUV;
  gint hY, hUV;

  dest_strideY =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, dest_width);
  dest_strideUV =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, dest_width);
  src_strideY =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, src_width);
  src_strideUV =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, src_width);

  destY =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 0,
      dest_width, dest_height);
  destU =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 1,
      dest_width, dest_height);
  destV =
      dest + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 2,
      dest_width, dest_height);

  srcY =
      src + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 0,
      src_width, src_height);
  srcU =
      src + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 1,
      src_width, src_height);
  srcV =
      src + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 2,
      src_width, src_height);


  destY = destY + dest_y * dest_strideY + dest_x;
  destU = destU + (dest_y / 2) * dest_strideUV + dest_x / 2;
  destV = destV + (dest_y / 2) * dest_strideUV + dest_x / 2;

  srcY = srcY + src_y * src_strideY + src_x;
  srcU = srcU + (src_y / 2) * src_strideUV + src_x / 2;
  srcV = srcV + (src_y / 2) * src_strideUV + src_x / 2;

  widthY = w;
  widthUV = (w + 1) / 2;

  hY = h;
  hUV = (h + 1) / 2;

  if (src_sdtv != dest_sdtv) {
    gint matrix[12];
    gint y1, y2, y3, y4;
    gint u1, u2, u3, u4;
    gint v1, v2, v3, v4;
    gint j;
    guint8 *destY2 = destY + dest_strideY;
    const guint8 *srcY2 = srcY + src_strideY;

    dest_strideY *= 2;
    src_strideY *= 2;

    memcpy (matrix,
        dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
        cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));

    for (i = 0; i < hUV; i++) {
      if (i * 2 == hY) {
        destY2 = destY;
        srcY2 = srcY;
      }

      for (j = 0; j < widthUV; j++) {
        y1 = srcY[2 * j];
        y2 = srcY[2 * j + 1];
        y3 = srcY2[2 * j];
        y4 = srcY2[2 * j + 1];

        u1 = u2 = u3 = u4 = srcU[j];
        v1 = v2 = v3 = v4 = srcV[j];

        y1 = APPLY_MATRIX (matrix, 0, y1, u1, v1);
        u1 = APPLY_MATRIX (matrix, 1, y1, u1, v1);
        v1 = APPLY_MATRIX (matrix, 2, y1, u1, v1);

        y2 = APPLY_MATRIX (matrix, 0, y2, u2, v2);
        u2 = APPLY_MATRIX (matrix, 1, y2, u2, v2);
        v2 = APPLY_MATRIX (matrix, 2, y2, u2, v2);

        y3 = APPLY_MATRIX (matrix, 0, y3, u3, v3);
        u3 = APPLY_MATRIX (matrix, 1, y3, u3, v3);
        v3 = APPLY_MATRIX (matrix, 2, y3, u3, v3);

        y4 = APPLY_MATRIX (matrix, 0, y4, u4, v4);
        u4 = APPLY_MATRIX (matrix, 1, y4, u4, v4);
        v4 = APPLY_MATRIX (matrix, 2, y4, u4, v4);

        destY[2 * j] = y1;
        destY[2 * j + 1] = y2;
        destY2[2 * j] = y3;
        destY2[2 * j + 1] = y4;

        destU[j] = (u1 + u2 + u3 + u4) / 4;
        destV[j] = (v1 + v2 + v3 + v4) / 4;
      }
      destY += dest_strideY;
      srcY += src_strideY;
      destY2 += dest_strideY;
      srcY2 += src_strideY;

      destU += dest_strideUV;
      destV += dest_strideUV;
      srcU += src_strideUV;
      srcV += src_strideUV;
    }
  } else {
    for (i = 0; i < hY; i++) {
      oil_copy_u8 (destY, srcY, widthY);
      destY += dest_strideY;
      srcY += src_strideY;
    }

    for (i = 0; i < hUV; i++) {
      oil_copy_u8 (destU, srcU, widthUV);
      oil_copy_u8 (destV, srcV, widthUV);
      destU += dest_strideUV;
      destV += dest_strideUV;
      srcU += src_strideUV;
      srcV += src_strideUV;
    }
  }
}

static void
560 561 562 563 564
copy_i420_ayuv (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 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 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
{
  gint i;
  const guint8 *srcY, *srcU, *srcV;
  gint src_strideY, src_strideUV;
  gint widthY, widthUV;
  gint hY, hUV;

  src_strideY =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 0, src_width);
  src_strideUV =
      gst_video_format_get_row_stride (GST_VIDEO_FORMAT_I420, 1, src_width);

  srcY =
      src + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 0,
      src_width, src_height);
  srcU =
      src + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 1,
      src_width, src_height);
  srcV =
      src + gst_video_format_get_component_offset (GST_VIDEO_FORMAT_I420, 2,
      src_width, src_height);


  dest = dest + dest_y * dest_width * 4 + dest_x * 4;

  srcY = srcY + src_y * src_strideY + src_x;
  srcU = srcU + (src_y / 2) * src_strideUV + src_x / 2;
  srcV = srcV + (src_y / 2) * src_strideUV + src_x / 2;

  widthY = w;
  widthUV = (w + 1) / 2;

  hY = h;
  hUV = (h + 1) / 2;

  if (src_sdtv != dest_sdtv) {
    gint matrix[12];
    gint y1, y2, y3, y4;
    gint u1, u2, u3, u4;
    gint v1, v2, v3, v4;
    gint j;
    guint8 *dest2;
    const guint8 *srcY2 = srcY + src_strideY;

    dest2 = dest + dest_width * 4;

    src_strideY *= 2;

    memcpy (matrix,
        dest_sdtv ? cog_ycbcr_hdtv_to_ycbcr_sdtv_matrix_8bit :
        cog_ycbcr_sdtv_to_ycbcr_hdtv_matrix_8bit, 12 * sizeof (gint));

    for (i = 0; i < hUV; i++) {
      if (i * 2 == hY) {
        srcY2 = srcY;
      }

      for (j = 0; j < widthUV; j++) {
        y1 = srcY[2 * j];
        y3 = srcY2[2 * j];
        y2 = (j * 2 < widthY) ? srcY[2 * j + 1] : y1;
        y4 = (j * 2 < widthY) ? srcY2[2 * j + 1] : y3;

        u1 = u2 = u3 = u4 = srcU[j];
        v1 = v2 = v3 = v4 = srcV[j];

        y1 = APPLY_MATRIX (matrix, 0, y1, u1, v1);
        u1 = APPLY_MATRIX (matrix, 1, y1, u1, v1);
        v1 = APPLY_MATRIX (matrix, 2, y1, u1, v1);

        y3 = APPLY_MATRIX (matrix, 0, y3, u3, v3);
        u3 = APPLY_MATRIX (matrix, 1, y3, u3, v3);
        v3 = APPLY_MATRIX (matrix, 2, y3, u3, v3);

        if (j * 2 < widthY) {
          y2 = APPLY_MATRIX (matrix, 0, y2, u2, v2);
          u2 = APPLY_MATRIX (matrix, 1, y2, u2, v2);
          v2 = APPLY_MATRIX (matrix, 2, y2, u2, v2);

          y4 = APPLY_MATRIX (matrix, 0, y4, u4, v4);
          u4 = APPLY_MATRIX (matrix, 1, y4, u4, v4);
          v4 = APPLY_MATRIX (matrix, 2, y4, u4, v4);
        }

        dest[8 * j] = i_alpha;
        dest[8 * j + 1] = y1;
        dest[8 * j + 2] = u1;
        dest[8 * j + 3] = v1;
        dest2[8 * j] = i_alpha;
        dest2[8 * j + 1] = y3;
        dest2[8 * j + 2] = u3;
        dest2[8 * j + 3] = v3;
        if (j * 2 < widthY) {
          dest[8 * j + 4] = i_alpha;
          dest[8 * j + 5] = y2;
          dest[8 * j + 6] = u2;
          dest[8 * j + 7] = v2;
          dest2[8 * j + 4] = i_alpha;
          dest2[8 * j + 5] = y4;
          dest2[8 * j + 6] = u4;
          dest2[8 * j + 7] = v4;
        }
      }
      dest += dest_width * 8;
      srcY += src_strideY;
      dest2 += dest_width * 8;
      srcY2 += src_strideY;

      srcU += src_strideUV;
      srcV += src_strideUV;
    }
  } else {
    gint y1, y2, y3, y4;
    gint u1, u2, u3, u4;
    gint v1, v2, v3, v4;
    gint j;
    guint8 *dest2;
    const guint8 *srcY2 = srcY + src_strideY;

    dest2 = dest + dest_width * 4;

    src_strideY *= 2;

    for (i = 0; i < hUV; i++) {
      if (i * 2 == hY) {
        srcY2 = srcY;
      }

      for (j = 0; j < widthUV; j++) {
        y1 = srcY[2 * j];
        y3 = srcY2[2 * j];
        y2 = (j * 2 < widthY) ? srcY[2 * j + 1] : y1;
        y4 = (j * 2 < widthY) ? srcY2[2 * j + 1] : y3;

        u1 = u2 = u3 = u4 = srcU[j];
        v1 = v2 = v3 = v4 = srcV[j];

        dest[8 * j] = i_alpha;
        dest[8 * j + 1] = y1;
        dest[8 * j + 2] = u1;
        dest[8 * j + 3] = v1;
        dest2[8 * j] = i_alpha;
        dest2[8 * j + 1] = y3;
        dest2[8 * j + 2] = u3;
        dest2[8 * j + 3] = v3;
        if (j * 2 < widthY) {
          dest[8 * j + 4] = i_alpha;
          dest[8 * j + 5] = y2;
          dest[8 * j + 6] = u2;
          dest[8 * j + 7] = v2;
          dest2[8 * j + 4] = i_alpha;
          dest2[8 * j + 5] = y4;
          dest2[8 * j + 6] = u4;
          dest2[8 * j + 7] = v4;
        }
      }
      dest += dest_width * 8;
      srcY += src_strideY;
      dest2 += dest_width * 8;
      srcY2 += src_strideY;

      srcU += src_strideUV;
      srcV += src_strideUV;
    }
  }
}

732
static void
733
_argb_order (GstVideoFormat format, gint p[4], gboolean * alpha)
734
{
735
  *alpha = FALSE;
736 737
  switch (format) {
    case GST_VIDEO_FORMAT_ARGB:
738
      *alpha = TRUE;
739
    case GST_VIDEO_FORMAT_xRGB:
740 741 742 743
      p[0] = 0;
      p[1] = 1;
      p[2] = 2;
      p[3] = 3;
744 745
      break;
    case GST_VIDEO_FORMAT_ABGR:
746
      *alpha = TRUE;
747
    case GST_VIDEO_FORMAT_xBGR:
748 749 750 751
      p[0] = 0;
      p[1] = 3;
      p[2] = 2;
      p[3] = 1;
752 753
      break;
    case GST_VIDEO_FORMAT_RGBA:
754
      *alpha = TRUE;
755
    case GST_VIDEO_FORMAT_RGBx:
756 757 758 759 760
    case GST_VIDEO_FORMAT_RGB:
      p[0] = 3;
      p[1] = 0;
      p[2] = 1;
      p[3] = 2;
761 762
      break;
    case GST_VIDEO_FORMAT_BGRA:
763
      *alpha = TRUE;
764
    case GST_VIDEO_FORMAT_BGRx:
765 766 767 768 769
    case GST_VIDEO_FORMAT_BGR:
      p[0] = 3;
      p[1] = 2;
      p[2] = 1;
      p[3] = 0;
770 771 772 773
      break;
    default:
      g_assert_not_reached ();
  }
774 775 776 777 778 779 780 781 782
}

static void
fill_rgb32 (GstVideoBoxFill fill_type, guint b_alpha, GstVideoFormat format,
    guint8 * dest, gboolean sdtv, gint width, gint height)
{
  guint32 empty_pixel;
  gint p[4];
  gboolean alpha;
783

784 785 786 787 788 789
  _argb_order (format, p, &alpha);

  empty_pixel = GUINT32_FROM_BE ((b_alpha << (p[0] * 8)) |
      (rgb_colors_R[fill_type] << (p[1] * 8)) |
      (rgb_colors_G[fill_type] << (p[2] * 8)) |
      (rgb_colors_B[fill_type] << (p[3] * 8)));
790 791 792 793

  oil_splat_u32_ns ((guint32 *) dest, &empty_pixel, width * height);
}

794 795 796 797 798 799 800 801 802 803 804 805 806
static void
fill_rgb24 (GstVideoBoxFill fill_type, guint b_alpha, GstVideoFormat format,
    guint8 * dest, gboolean sdtv, gint width, gint height)
{
  gint dest_stride = GST_ROUND_UP_4 (width * 3);
  gint p[4];
  gboolean alpha;
  gint i, j;

  _argb_order (format, p, &alpha);

  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
Sebastian Dröge's avatar
Sebastian Dröge committed
807 808 809
      dest[3 * j + p[1]] = rgb_colors_R[fill_type];
      dest[3 * j + p[2]] = rgb_colors_G[fill_type];
      dest[3 * j + p[3]] = rgb_colors_B[fill_type];
810 811 812 813 814
    }
    dest += dest_stride;
  }
}

815 816 817 818 819 820 821 822
static void
copy_rgb32 (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
{
  gint i, j;
823
  gint src_stride, dest_stride;
824
  gboolean in_alpha, out_alpha;
825
  gint in_bpp, out_bpp;
826 827
  gint p_out[4];
  gint p_in[4];
828 829 830 831
  gboolean packed_out = (dest_format == GST_VIDEO_FORMAT_RGB
      || dest_format == GST_VIDEO_FORMAT_BGR);
  gboolean packed_in = (src_format == GST_VIDEO_FORMAT_RGB
      || src_format == GST_VIDEO_FORMAT_BGR);
832

833 834 835 836
  src_stride = (packed_in) ? GST_ROUND_UP_4 (3 * src_width) : 4 * src_width;
  dest_stride = (packed_out) ? GST_ROUND_UP_4 (3 * dest_width) : 4 * dest_width;
  in_bpp = (packed_in) ? 3 : 4;
  out_bpp = (packed_out) ? 3 : 4;
837

838 839
  _argb_order (dest_format, p_out, &out_alpha);
  _argb_order (src_format, p_in, &in_alpha);
840

841 842
  dest = dest + dest_y * dest_stride + dest_x * out_bpp;
  src = src + src_y * src_stride + src_x * in_bpp;
843 844

  if (in_alpha && out_alpha) {
845
    w *= 4;
846 847 848 849 850 851 852 853 854 855
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        dest[j + p_out[0]] = (src[j + p_in[0]] * i_alpha) >> 8;
        dest[j + p_out[1]] = src[j + p_in[1]];
        dest[j + p_out[2]] = src[j + p_in[2]];
        dest[j + p_out[3]] = src[j + p_in[3]];
      }
      dest += dest_stride;
      src += src_stride;
    }
856 857
  } else if (out_alpha && !packed_in) {
    w *= 4;
858 859 860 861 862 863 864 865 866 867
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        dest[j + p_out[0]] = i_alpha & 0xff;
        dest[j + p_out[1]] = src[j + p_in[1]];
        dest[j + p_out[2]] = src[j + p_in[2]];
        dest[j + p_out[3]] = src[j + p_in[3]];
      }
      dest += dest_stride;
      src += src_stride;
    }
868 869 870 871 872 873 874 875 876 877 878
  } else if (out_alpha && packed_in) {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        dest[4 * j + p_out[0]] = i_alpha & 0xff;
        dest[4 * j + p_out[1]] = src[in_bpp * j + p_in[1]];
        dest[4 * j + p_out[2]] = src[in_bpp * j + p_in[2]];
        dest[4 * j + p_out[3]] = src[in_bpp * j + p_in[3]];
      }
      dest += dest_stride;
      src += src_stride;
    }
879 880
  } else if (!packed_out && !packed_in) {
    w *= 4;
881 882 883 884 885 886 887 888 889
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        dest[j + p_out[1]] = src[j + p_in[1]];
        dest[j + p_out[2]] = src[j + p_in[2]];
        dest[j + p_out[3]] = src[j + p_in[3]];
      }
      dest += dest_stride;
      src += src_stride;
    }
890 891 892 893 894 895 896 897 898 899
  } else {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        dest[out_bpp * j + p_out[1]] = src[in_bpp * j + p_in[1]];
        dest[out_bpp * j + p_out[2]] = src[in_bpp * j + p_in[2]];
        dest[out_bpp * j + p_out[3]] = src[in_bpp * j + p_in[3]];
      }
      dest += dest_stride;
      src += src_stride;
    }
900 901 902
  }
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 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 1018 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 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
static void
copy_rgb32_ayuv (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
{
  gint i, j;
  gint src_stride, dest_stride;
  gboolean in_alpha;
  gint in_bpp;
  gint p_in[4];
  gboolean packed_in = (src_format == GST_VIDEO_FORMAT_RGB
      || src_format == GST_VIDEO_FORMAT_BGR);
  gint matrix[12];
  gint a;
  gint y, u, v;
  gint r, g, b;

  src_stride = (packed_in) ? GST_ROUND_UP_4 (3 * src_width) : 4 * src_width;
  dest_stride = 4 * dest_width;
  in_bpp = (packed_in) ? 3 : 4;

  _argb_order (src_format, p_in, &in_alpha);

  memcpy (matrix,
      (dest_sdtv) ? cog_rgb_to_ycbcr_matrix_8bit_sdtv :
      cog_rgb_to_ycbcr_matrix_8bit_hdtv, 12 * sizeof (gint));

  dest = dest + dest_y * dest_stride + dest_x * 4;
  src = src + src_y * src_stride + src_x * in_bpp;

  if (in_alpha) {
    w *= 4;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        a = (src[j + p_in[0]] * i_alpha) >> 8;
        r = src[j + p_in[1]];
        g = src[j + p_in[2]];
        b = src[j + p_in[3]];

        y = APPLY_MATRIX (matrix, 0, r, g, b);
        u = APPLY_MATRIX (matrix, 1, r, g, b);
        v = APPLY_MATRIX (matrix, 2, r, g, b);

        dest[j + 0] = a;
        dest[j + 1] = CLAMP (y, 0, 255);
        dest[j + 2] = CLAMP (u, 0, 255);
        dest[j + 3] = CLAMP (v, 0, 255);
      }
      dest += dest_stride;
      src += src_stride;
    }
  } else if (!packed_in) {
    w *= 4;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        a = i_alpha & 0xff;
        r = src[j + p_in[1]];
        g = src[j + p_in[2]];
        b = src[j + p_in[3]];

        y = APPLY_MATRIX (matrix, 0, r, g, b);
        u = APPLY_MATRIX (matrix, 1, r, g, b);
        v = APPLY_MATRIX (matrix, 2, r, g, b);

        dest[j + 0] = a;
        dest[j + 1] = CLAMP (y, 0, 255);
        dest[j + 2] = CLAMP (u, 0, 255);
        dest[j + 3] = CLAMP (v, 0, 255);
      }
      dest += dest_stride;
      src += src_stride;
    }
  } else {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        a = i_alpha & 0xff;
        r = src[in_bpp * j + p_in[1]];
        g = src[in_bpp * j + p_in[2]];
        b = src[in_bpp * j + p_in[3]];

        y = APPLY_MATRIX (matrix, 0, r, g, b);
        u = APPLY_MATRIX (matrix, 1, r, g, b);
        v = APPLY_MATRIX (matrix, 2, r, g, b);

        dest[4 * j + 0] = a;
        dest[4 * j + 1] = CLAMP (y, 0, 255);
        dest[4 * j + 2] = CLAMP (u, 0, 255);
        dest[4 * j + 3] = CLAMP (v, 0, 255);
      }
      dest += dest_stride;
      src += src_stride;
    }
  }
}

static void
copy_ayuv_rgb32 (guint i_alpha, GstVideoFormat dest_format, guint8 * dest,
    gboolean dest_sdtv, gint dest_width, gint dest_height, gint dest_x,
    gint dest_y, GstVideoFormat src_format, const guint8 * src,
    gboolean src_sdtv, gint src_width, gint src_height, gint src_x, gint src_y,
    gint w, gint h)
{
  gint i, j;
  gint src_stride, dest_stride;
  gboolean out_alpha;
  gint out_bpp;
  gint p_out[4];
  gboolean packed_out = (dest_format == GST_VIDEO_FORMAT_RGB
      || dest_format == GST_VIDEO_FORMAT_BGR);
  gint matrix[12];
  gint a;
  gint y, u, v;
  gint r, g, b;

  dest_stride = (packed_out) ? GST_ROUND_UP_4 (3 * dest_width) : 4 * dest_width;
  src_stride = 4 * src_width;
  out_bpp = (packed_out) ? 3 : 4;

  _argb_order (dest_format, p_out, &out_alpha);

  memcpy (matrix,
      (src_sdtv) ? cog_ycbcr_to_rgb_matrix_8bit_sdtv :
      cog_ycbcr_to_rgb_matrix_8bit_hdtv, 12 * sizeof (gint));

  dest = dest + dest_y * dest_stride + dest_x * out_bpp;
  src = src + src_y * src_stride + src_x * 4;

  if (out_alpha) {
    w *= 4;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        a = (src[j + 0] * i_alpha) >> 8;
        y = src[j + 1];
        u = src[j + 2];
        v = src[j + 3];

        r = APPLY_MATRIX (matrix, 0, y, u, v);
        g = APPLY_MATRIX (matrix, 1, y, u, v);
        b = APPLY_MATRIX (matrix, 2, y, u, v);

        dest[j + p_out[0]] = a;
        dest[j + p_out[1]] = CLAMP (r, 0, 255);
        dest[j + p_out[2]] = CLAMP (g, 0, 255);
        dest[j + p_out[3]] = CLAMP (b, 0, 255);
      }
      dest += dest_stride;
      src += src_stride;
    }
  } else if (!packed_out) {
    w *= 4;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j += 4) {
        y = src[j + 1];
        u = src[j + 2];
        v = src[j + 3];

        r = APPLY_MATRIX (matrix, 0, y, u, v);
        g = APPLY_MATRIX (matrix, 1, y, u, v);
        b = APPLY_MATRIX (matrix, 2, y, u, v);

        dest[j + p_out[1]] = CLAMP (r, 0, 255);
        dest[j + p_out[2]] = CLAMP (g, 0, 255);
        dest[j + p_out[3]] = CLAMP (b, 0, 255);
      }
      dest += dest_stride;
      src += src_stride;
    }
  } else {
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        y = src[4 * j + 1];
        u = src[4 * j + 2];
        v = src[4 * j + 3];

        r = APPLY_MATRIX (matrix, 0, y, u, v);
        g = APPLY_MATRIX (matrix, 1, y, u, v);
        b = APPLY_MATRIX (matrix, 2, y, u, v);

        dest[out_bpp * j + p_out[1]] = CLAMP (r, 0, 255);
        dest[out_bpp * j + p_out[2]] = CLAMP (g, 0, 255);
        dest[out_bpp * j + p_out[3]] = CLAMP (b, 0, 255);
      }
      dest += dest_stride;
      src += src_stride;
    }
  }
}

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
#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
{
1103 1104 1105 1106 1107 1108 1109
  PROP_0,
  PROP_LEFT,
  PROP_RIGHT,
  PROP_TOP,
  PROP_BOTTOM,
  PROP_FILL_TYPE,
  PROP_ALPHA,
1110 1111
  PROP_BORDER_ALPHA,
  PROP_AUTOCROP
1112
      /* FILL ME */
1113 1114 1115
};

static GstStaticPadTemplate gst_video_box_src_template =
1116
    GST_STATIC_PAD_TEMPLATE ("src",
1117 1118
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
1119
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
1120 1121 1122 1123
        GST_VIDEO_CAPS_YUV ("I420") ";"
        GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_BGRx ";"
        GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_RGBx ";"
        GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_BGRA ";"
1124 1125
        GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA ";"
        GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR)
1126 1127 1128
    );

static GstStaticPadTemplate gst_video_box_sink_template =
1129
    GST_STATIC_PAD_TEMPLATE ("sink",
1130 1131
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
1132
    GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("AYUV") ";"
1133 1134 1135 1136
        GST_VIDEO_CAPS_YUV ("I420") ";"
        GST_VIDEO_CAPS_xRGB ";" GST_VIDEO_CAPS_BGRx ";"
        GST_VIDEO_CAPS_xBGR ";" GST_VIDEO_CAPS_RGBx ";"
        GST_VIDEO_CAPS_ARGB ";" GST_VIDEO_CAPS_BGRA ";"
1137 1138
        GST_VIDEO_CAPS_ABGR ";" GST_VIDEO_CAPS_RGBA ";"
        GST_VIDEO_CAPS_RGB ";" GST_VIDEO_CAPS_BGR)
1139 1140
    );

1141 1142
GST_BOILERPLATE (GstVideoBox, gst_video_box, GstBaseTransform,
    GST_TYPE_BASE_TRANSFORM);
1143 1144 1145 1146 1147 1148

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

1149
static gboolean gst_video_box_recalc_transform (GstVideoBox * video_box);
1150
static GstCaps *gst_video_box_transform_caps (GstBaseTransform * trans,
1151
    GstPadDirection direction, GstCaps * from);
1152 1153
static gboolean gst_video_box_set_caps (GstBaseTransform * trans,
    GstCaps * in, GstCaps * out);
1154 1155
static gboolean gst_video_box_get_unit_size (GstBaseTransform * trans,
    GstCaps * caps, guint * size);
1156
static GstFlowReturn gst_video_box_transform (GstBaseTransform * trans,
Wim Taymans's avatar
Wim Taymans committed
1157
    GstBuffer * in, GstBuffer * out);
1158 1159
static void gst_video_box_fixate_caps (GstBaseTransform * trans,
    GstPadDirection direction, GstCaps * caps, GstCaps * othercaps);
1160 1161 1162 1163 1164 1165

#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;
1166
  static const GEnumValue video_box_fill[] = {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
1167 1168 1169
    {VIDEO_BOX_FILL_BLACK, "Black", "black"},
    {VIDEO_BOX_FILL_GREEN, "Colorkey green", "green"},
    {VIDEO_BOX_FILL_BLUE, "Colorkey blue", "blue"},
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    {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);

Sebastian Dröge's avatar
Sebastian Dröge committed
1186 1187 1188 1189
  gst_element_class_set_details_simple (element_class, "Video box filter",
      "Filter/Effect/Video",
      "Resizes a video by adding borders or cropping",
      "Wim Taymans <wim@fluendo.com>");
1190 1191 1192 1193 1194 1195

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

1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
static void
gst_video_box_finalize (GObject * object)
{
  GstVideoBox *video_box = GST_VIDEO_BOX (object);

  if (video_box->mutex) {
    g_mutex_free (video_box->mutex);
    video_box->mutex = NULL;
  }

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

1210 1211 1212
static void
gst_video_box_class_init (GstVideoBoxClass * klass)
{
Sebastian Dröge's avatar
Sebastian Dröge committed
1213 1214
  GObjectClass *gobject_class = (GObjectClass *) klass;
  GstBaseTransformClass *trans_class = (GstBaseTransformClass *) klass;
1215

Wim Taymans's avatar
Wim Taymans committed
1216 1217
  gobject_class->set_property = gst_video_box_set_property;
  gobject_class->get_property = gst_video_box_get_property;
1218
  gobject_class->finalize = gst_video_box_finalize;
Wim Taymans's avatar
Wim Taymans committed
1219

1220
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FILL_TYPE,
1221 1222
      g_param_spec_enum ("fill", "Fill", "How to fill the borders",
          GST_TYPE_VIDEO_BOX_FILL, DEFAULT_FILL_TYPE,
1223
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1224
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_LEFT,
1225 1226
      g_param_spec_int ("left", "Left",
          "Pixels to box at left (<0  = add a border)", G_MININT, G_MAXINT,
1227 1228
          DEFAULT_LEFT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1229
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RIGHT,
1230 1231
      g_param_spec_int ("right", "Right",
          "Pixels to box at right (<0 = add a border)", G_MININT, G_MAXINT,
1232 1233
          DEFAULT_RIGHT,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1234
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_TOP,
1235 1236
      g_param_spec_int ("top", "Top",
          "Pixels to box at top (<0 = add a border)", G_MININT, G_MAXINT,
1237 1238
          DEFAULT_TOP,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1239
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BOTTOM,
1240 1241
      g_param_spec_int ("bottom", "Bottom",
          "Pixels to box at bottom (<0 = add a border)", G_MININT, G_MAXINT,
1242 1243
          DEFAULT_BOTTOM,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1244
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ALPHA,
1245
      g_param_spec_double ("alpha", "Alpha", "Alpha value picture", 0.0, 1.0,
1246 1247
          DEFAULT_ALPHA,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1248
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BORDER_ALPHA,
Sebastian Dröge's avatar
Sebastian Dröge committed
1249
      g_param_spec_double ("border-alpha", "Border Alpha",
1250
          "Alpha value of the border", 0.0, 1.0, DEFAULT_BORDER_ALPHA,
1251
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
1252 1253 1254 1255 1256 1257 1258 1259
  /**
   * GstVideoBox:autocrop
   *
   * If set to %TRUE videobox will automatically crop/pad the input
   * video to be centered in the output.
   *
   * Since: 0.10.16
   **/
1260 1261
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_AUTOCROP,
      g_param_spec_boolean ("autocrop", "Auto crop",
Sebastian Dröge's avatar
Sebastian Dröge committed
1262
          "Auto crop", FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1263

1264
  trans_class->transform = GST_DEBUG_FUNCPTR (gst_video_box_transform);
1265 1266 1267 1268
  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);
1269
  trans_class->fixate_caps = GST_DEBUG_FUNCPTR (gst_video_box_fixate_caps);
1270 1271 1272
}

static void
1273
gst_video_box_init (GstVideoBox * video_box, GstVideoBoxClass * g_class)
1274 1275 1276 1277 1278
{
  video_box->box_right = DEFAULT_RIGHT;
  video_box->box_left = DEFAULT_LEFT;
  video_box->box_top = DEFAULT_TOP;
  video_box->box_bottom = DEFAULT_BOTTOM;
1279 1280 1281 1282
  video_box->crop_right = 0;
  video_box->crop_left = 0;
  video_box->crop_top = 0;
  video_box->crop_bottom = 0;
1283 1284 1285
  video_box->fill_type = DEFAULT_FILL_TYPE;
  video_box->alpha = DEFAULT_ALPHA;
  video_box->border_alpha = DEFAULT_BORDER_ALPHA;
1286
  video_box->autocrop = FALSE;
1287 1288

  video_box->mutex = g_mutex_new ();
1289 1290 1291 1292 1293 1294
}

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

1297
  g_mutex_lock (video_box->mutex);
1298
  switch (prop_id) {
1299
    case PROP_LEFT:
1300 1301 1302 1303 1304 1305 1306 1307 1308
      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;
1309
    case PROP_RIGHT:
1310 1311 1312 1313 1314 1315 1316 1317 1318
      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;
1319
    case PROP_TOP:
1320 1321 1322 1323 1324 1325 1326 1327 1328
      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;
1329
    case PROP_BOTTOM:
1330 1331 1332 1333 1334 1335 1336 1337 1338
      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;
1339
    case PROP_FILL_TYPE:
1340 1341
      video_box->fill_type = g_value_get_enum (value);
      break;
1342
    case PROP_ALPHA:
1343 1344
      video_box->alpha = g_value_get_double (value);
      break;
1345
    case PROP_BORDER_ALPHA:
1346 1347
      video_box->border_alpha = g_value_get_double (value);
      break;
1348 1349 1350
    case PROP_AUTOCROP:
      video_box->autocrop = g_value_get_boolean (value);
      break;
1351 1352 1353 1354
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
1355
  gst_video_box_recalc_transform (video_box);
1356 1357

  GST_DEBUG_OBJECT (video_box, "Calling reconfigure");
Sebastian Dröge's avatar
Sebastian Dröge committed
1358
  gst_base_transform_reconfigure (GST_BASE_TRANSFORM_CAST (video_box));
1359 1360

  g_mutex_unlock (video_box->mutex);
1361 1362 1363 1364 1365
}

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

1369
  video_box->box_left = crop_w / 2;
1370 1371 1372 1373 1374 1375 1376 1377
  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;
  }

1378 1379 1380 1381 1382 1383 1384
  /* Round down/up for odd width differences */
  if (crop_w < 0)
    crop_w -= 1;
  else
    crop_w += 1;

  video_box->box_right = crop_w / 2;
1385 1386 1387 1388 1389 1390 1391 1392
  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;
  }

1393
  video_box->box_top = crop_h / 2;
1394 1395 1396 1397 1398 1399 1400 1401
  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;
  }

1402 1403 1404 1405 1406 1407 1408
  /* Round down/up for odd height differences */
  if (crop_h < 0)
    crop_h -= 1;
  else
    crop_h += 1;
  video_box->box_bottom = crop_h / 2;

1409 1410 1411 1412 1413 1414 1415
  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;
  }
1416
}
1417

1418 1419 1420 1421
static void
gst_video_box_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
1422
  GstVideoBox *video_box = GST_VIDEO_BOX (object);
1423 1424

  switch (prop_id) {
1425
    case PROP_LEFT:
1426 1427
      g_value_set_int (value, video_box->box_left);
      break;
1428
    case PROP_RIGHT:
1429 1430
      g_value_set_int (value, video_box->box_right);
      break;
1431
    case PROP_TOP:
1432 1433
      g_value_set_int (value, video_box->box_top);
      break;
1434
    case PROP_BOTTOM:
1435 1436
      g_value_set_int (value, video_box->box_bottom);
      break;
1437
    case PROP_FILL_TYPE:
1438 1439
      g_value_set_enum (value, video_box->fill_type);
      break;
1440
    case PROP_ALPHA:
1441 1442
      g_value_set_double (value, video_box->alpha);
      break;
1443
    case PROP_BORDER_ALPHA:
1444 1445
      g_value_set_double (value, video_box->border_alpha);
      break;
1446 1447 1448
    case PROP_AUTOCROP:
      g_value_set_boolean (value, video_box->autocrop);
      break;
1449 1450 1451 1452 1453 1454
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

1455
static GstCaps *
1456 1457
gst_video_box_transform_caps (GstBaseTransform * trans,
    GstPadDirection direction, GstCaps * from)
1458
{
Sebastian Dröge's avatar
Sebastian Dröge committed
1459
  GstVideoBox *video_box = GST_VIDEO_BOX (trans);
1460 1461
  GstCaps *to, *ret;
  const GstCaps *templ;
1462
  const gchar *name;
1463
  GstStructure *structure;
1464 1465
  GstPad *other;
  gint width, height;
1466

1467
  to = gst_caps_copy (from);
1468 1469
  /* Just to be sure... */
  gst_caps_truncate (to);
1470 1471
  structure = gst_caps_get_structure (to, 0);

1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
  /* Transform width/height */
  if (video_box->autocrop) {
    gst_structure_remove_field (structure, "width");
    gst_structure_remove_field (structure, "height");
  } else {
    /* 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);
    }

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

      if (height <= 0)
        height = 1;

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

  /* Supported conversions:
   * I420->AYUV
   * AYUV->I420
   * AYUV->xRGB (24bpp, 32bpp, incl. alpha)
   * xRGB->xRGB (24bpp, 32bpp, from/to all variants, incl. alpha)
   * xRGB->AYUV (24bpp, 32bpp, incl. alpha)
   *
   * Passthrough only for everything else.
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
   */
  name = gst_structure_get_name (structure);
  if (g_str_equal (name, "video/x-raw-yuv")) {
    guint32 fourcc;

    if (gst_structure_get_fourcc (structure, "format", &fourcc) &&
        (fourcc == GST_STR_FOURCC ("AYUV") ||
            fourcc == GST_STR_FOURCC ("I420"))) {
      GValue list = { 0, };
      GValue val = { 0, };
1528
      GstStructure *s2;
1529 1530 1531 1532 1533 1534

      /* get rid of format */
      gst_structure_remove_field (structure, "format");
      gst_structure_remove_field (structure, "color-matrix");
      gst_structure_remove_field (structure, "chroma-site");

1535 1536
      s2 = gst_structure_copy (structure);

1537 1538 1539 1540 1541 1542 1543
      g_value_init (&list, GST_TYPE_LIST);
      g_value_init (&val, GST_TYPE_FOURCC);
      gst_value_set_fourcc (&val, GST_STR_FOURCC ("AYUV"));
      gst_value_list_append_value (&list, &val);
      g_value_reset (&val);
      gst_value_set_fourcc (&val, GST_STR_FOURCC ("I420"));
      gst_value_list_append_value (&list, &val);
1544
      g_value_unset (&val);
1545 1546
      gst_structure_set_value (structure, "format", &list);
      g_value_unset (&list);
1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

      gst_structure_set_name (s2, "video/x-raw-rgb");
      g_value_init (&list, GST_TYPE_LIST);
      g_value_init (&val, G_TYPE_INT);
      g_value_set_int (&val, 32);
      gst_value_list_append_value (&list, &val);
      g_value_reset (&val);
      g_value_set_int (&val, 24);
      gst_value_list_append_value (&list, &val);
      g_value_unset (&val);
      gst_structure_set_value (s2, "depth", &list);
      gst_structure_set_value (s2, "bpp", &list);
      g_value_unset (&list);
      gst_caps_append_structure (to, s2);
1561 1562 1563 1564
    }
  } else if (g_str_equal (name, "video/x-raw-rgb")) {
    gint bpp;

1565 1566
    if (gst_structure_get_int (structure, "bpp", &bpp) &&
        (bpp == 32 || bpp == 24)) {
1567 1568
      GValue list = { 0, };
      GValue val = { 0, };
1569
      GstStructure *s2;
1570 1571 1572

      /* get rid of format */
      gst_structure_remove_field (structure, "depth");
1573
      gst_structure_remove_field (structure, "bpp");
1574 1575 1576 1577 1578
      gst_structure_remove_field (structure, "red_mask");
      gst_structure_remove_field (structure, "green_mask");
      gst_structure_remove_field (structure, "blue_mask");
      gst_structure_remove_field (structure, "alpha_mask");

1579 1580
      s2 = gst_structure_copy (structure);

1581 1582 1583 1584 1585 1586 1587
      g_value_init (&list, GST_TYPE_LIST);
      g_value_init (&val, G_TYPE_INT);
      g_value_set_int (&val, 32);
      gst_value_list_append_value (&list, &val);
      g_value_reset (&val);
      g_value_set_int (&val, 24);
      gst_value_list_append_value (&list, &val);
1588
      g_value_unset (&val);
1589
      gst_structure_set_value (structure, "depth", &list);
1590
      gst_structure_set_value (structure, "bpp", &list);
1591
      g_value_unset (&list);
1592

1593 1594 1595 1596
      gst_structure_set_name (s2, "video/x-raw-yuv");
      gst_structure_set (s2, "format", GST_TYPE_FOURCC, GST_STR_FOURCC ("AYUV"),
          NULL);
      gst_caps_append_structure (to, s2);
1597
    }
1598 1599
  }

1600 1601 1602 1603 1604 1605
  /* 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);
1606

1607
  GST_DEBUG_OBJECT (video_box, "direction %d, transformed %" GST_PTR_FORMAT
1608 1609 1610 1611 1612
      " to %" GST_PTR_FORMAT, direction, from, ret);

  return ret;
}

1613
static gboolean
1614
gst_video_box_recalc_transform (GstVideoBox * video_box)
1615 1616 1617 1618
{
  gboolean res = TRUE;

  /* if we have the same format in and out and we don't need to perform any
Sebastian Dröge's avatar
Sebastian Dröge committed
1619
   * cropping at all, we can just operate in passthrough mode */
1620
  if (video_box->in_format == video_box->out_format &&
1621
      video_box->box_left == 0 && video_box->box_right == 0 &&
1622 1623
      video_box->box_top == 0 && video_box->box_bottom == 0 &&
      video_box->in_sdtv == video_box->out_sdtv) {
1624 1625

    GST_LOG_OBJECT (video_box, "we are using passthrough");
Sebastian Dröge's avatar
Sebastian Dröge committed
1626 1627
    gst_base_transform_set_passthrough (GST_BASE_TRANSFORM_CAST (video_box),
        TRUE);
1628 1629
  } else {
    GST_LOG_OBJECT (video_box, "we are not using passthrough");
Sebastian Dröge's avatar
Sebastian Dröge committed
1630 1631
    gst_base_transform_set_passthrough (GST_BASE_TRANSFORM_CAST (video_box),
        FALSE);
1632 1633 1634 1635
  }
  return res;
}

1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648
static gboolean
gst_video_box_select_processing_functions (GstVideoBox * video_box)
{
  switch (video_box->out_format) {
    case GST_VIDEO_FORMAT_AYUV:
      video_box->fill = fill_ayuv;
      switch (video_box->in_format) {
        case GST_VIDEO_FORMAT_AYUV:
          video_box->copy = copy_ayuv_ayuv;
          break;
        case GST_VIDEO_FORMAT_I420:
          video_box->copy = copy_i420_ayuv;
          break;
1649 1650 1651 1652 1653 1654 1655