colorspace.c 69.7 KB
Newer Older
1 2
/* GStreamer
 * Copyright (C) 2010 David Schleef <ds@schleef.org>
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 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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 "colorspace.h"
#include <glib.h>
#include <string.h>
#include "gstcolorspaceorc.h"


static void colorspace_convert_generic (ColorspaceConvert * convert,
32
    guint8 * dest, const guint8 * src);
33 34 35 36 37
static void colorspace_convert_lookup_fastpath (ColorspaceConvert * convert);
static void colorspace_convert_lookup_getput (ColorspaceConvert * convert);


ColorspaceConvert *
38 39 40
colorspace_convert_new (GstVideoFormat to_format, ColorSpaceColorSpec to_spec,
    GstVideoFormat from_format, ColorSpaceColorSpec from_spec,
    int width, int height)
41 42 43 44
{
  ColorspaceConvert *convert;
  int i;

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  g_return_val_if_fail (!gst_video_format_is_rgb (to_format)
      || to_spec == COLOR_SPEC_RGB, NULL);
  g_return_val_if_fail (!gst_video_format_is_yuv (to_format)
      || to_spec == COLOR_SPEC_YUV_BT709
      || to_spec == COLOR_SPEC_YUV_BT470_6, NULL);
  g_return_val_if_fail (gst_video_format_is_rgb (to_format)
      || gst_video_format_is_yuv (to_format)
      || (gst_video_format_is_gray (to_format) &&
          to_spec == COLOR_SPEC_GRAY), NULL);

  g_return_val_if_fail (!gst_video_format_is_rgb (from_format)
      || from_spec == COLOR_SPEC_RGB, NULL);
  g_return_val_if_fail (!gst_video_format_is_yuv (from_format)
      || from_spec == COLOR_SPEC_YUV_BT709
      || from_spec == COLOR_SPEC_YUV_BT470_6, NULL);
  g_return_val_if_fail (gst_video_format_is_rgb (from_format)
      || gst_video_format_is_yuv (from_format)
      || (gst_video_format_is_gray (from_format) &&
          from_spec == COLOR_SPEC_GRAY), NULL);

65 66 67 68
  convert = g_malloc (sizeof (ColorspaceConvert));
  memset (convert, 0, sizeof (ColorspaceConvert));

  convert->to_format = to_format;
69
  convert->to_spec = to_spec;
70
  convert->from_format = from_format;
71
  convert->from_spec = from_spec;
72 73 74 75
  convert->height = height;
  convert->width = width;
  convert->convert = colorspace_convert_generic;

76 77 78 79 80 81 82
  if (gst_video_format_get_component_depth (to_format, 0) > 8 ||
      gst_video_format_get_component_depth (from_format, 0) > 8) {
    convert->use_16bit = TRUE;
  } else {
    convert->use_16bit = FALSE;
  }

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  for (i = 0; i < 4; i++) {
    convert->dest_stride[i] = gst_video_format_get_row_stride (to_format, i,
        width);
    convert->dest_offset[i] = gst_video_format_get_component_offset (to_format,
        i, width, height);
    if (i == 0)
      convert->dest_offset[i] = 0;

    convert->src_stride[i] = gst_video_format_get_row_stride (from_format, i,
        width);
    convert->src_offset[i] = gst_video_format_get_component_offset (from_format,
        i, width, height);
    if (i == 0)
      convert->src_offset[i] = 0;

    GST_DEBUG ("%d: dest %d %d src %d %d", i,
        convert->dest_stride[i], convert->dest_offset[i],
        convert->src_stride[i], convert->src_offset[i]);
  }

  colorspace_convert_lookup_fastpath (convert);
  colorspace_convert_lookup_getput (convert);

106 107
  convert->tmpline = g_malloc (sizeof (guint8) * (width + 8) * 4);
  convert->tmpline16 = g_malloc (sizeof (guint16) * (width + 8) * 4);
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  if (to_format == GST_VIDEO_FORMAT_RGB8_PALETTED) {
    /* build poor man's palette, taken from ffmpegcolorspace */
    static const guint8 pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
    guint32 *palette;
    gint r, g, b;

    convert->palette = palette = g_new (guint32, 256);
    i = 0;
    for (r = 0; r < 6; r++) {
      for (g = 0; g < 6; g++) {
        for (b = 0; b < 6; b++) {
          palette[i++] =
              (0xffU << 24) | (pal_value[r] << 16) | (pal_value[g] << 8) |
              pal_value[b];
        }
      }
    }
    palette[i++] = 0;           /* 100% transparent, i == 6*6*6 */
    while (i < 256)
      palette[i++] = 0xff000000;
  }

131 132 133 134 135 136
  return convert;
}

void
colorspace_convert_free (ColorspaceConvert * convert)
{
137 138 139
  g_free (convert->palette);
  g_free (convert->tmpline);
  g_free (convert->tmpline16);
140 141 142 143 144 145 146 147 148 149 150 151

  g_free (convert);
}

void
colorspace_convert_set_interlaced (ColorspaceConvert * convert,
    gboolean interlaced)
{
  convert->interlaced = interlaced;
}

void
152 153
colorspace_convert_set_palette (ColorspaceConvert * convert,
    const guint32 * palette)
154 155 156 157 158 159 160
{
  if (convert->palette == NULL) {
    convert->palette = g_malloc (sizeof (guint32) * 256);
  }
  memcpy (convert->palette, palette, sizeof (guint32) * 256);
}

161 162 163 164 165 166
const guint32 *
colorspace_convert_get_palette (ColorspaceConvert * convert)
{
  return convert->palette;
}

167 168
void
colorspace_convert_convert (ColorspaceConvert * convert,
169
    guint8 * dest, const guint8 * src)
170 171 172 173 174 175 176 177 178 179
{
  convert->convert (convert, dest, src);
}

/* Line conversion to AYUV */

#define FRAME_GET_LINE(dir, comp, line) \
  ((dir) + convert-> dir ## _offset[(comp)] + convert-> dir ## _stride[(comp)] * (line))

static void
180 181
getline_I420 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
182 183 184 185 186 187 188 189
{
  cogorc_getline_I420 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j >> 1),
      FRAME_GET_LINE (src, 2, j >> 1), convert->width);
}

static void
190 191
putline_I420 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
192 193 194 195 196 197 198
{
  cogorc_putline_I420 (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 1, j >> 1),
      FRAME_GET_LINE (dest, 2, j >> 1), src, convert->width / 2);
}

static void
199 200
getline_YV12 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
201 202 203 204 205 206 207 208
{
  cogorc_getline_I420 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j >> 1),
      FRAME_GET_LINE (src, 2, j >> 1), convert->width);
}

static void
209 210
putline_YV12 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
211 212 213 214 215 216 217
{
  cogorc_putline_I420 (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 1, j >> 1),
      FRAME_GET_LINE (dest, 2, j >> 1), src, convert->width / 2);
}

static void
218 219
getline_YUY2 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
220 221 222 223 224
{
  cogorc_getline_YUY2 (dest, FRAME_GET_LINE (src, 0, j), convert->width / 2);
}

static void
225 226
putline_YUY2 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
227 228 229 230 231
{
  cogorc_putline_YUY2 (FRAME_GET_LINE (dest, 0, j), src, convert->width / 2);
}

static void
232 233
getline_UYVY (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
234 235 236 237 238
{
  cogorc_getline_UYVY (dest, FRAME_GET_LINE (src, 0, j), convert->width / 2);
}

static void
239 240
putline_UYVY (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
241 242 243 244 245
{
  cogorc_putline_UYVY (FRAME_GET_LINE (dest, 0, j), src, convert->width / 2);
}

static void
246 247
getline_YVYU (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
248 249 250 251 252
{
  cogorc_getline_YVYU (dest, FRAME_GET_LINE (src, 0, j), convert->width / 2);
}

static void
253 254
putline_YVYU (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
255 256 257 258 259
{
  cogorc_putline_YVYU (FRAME_GET_LINE (dest, 0, j), src, convert->width / 2);
}

static void
260 261
getline_v308 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
262 263
{
  int i;
264
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
265 266 267 268 269 270 271 272 273
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = srcline[i * 3 + 0];
    dest[i * 4 + 2] = srcline[i * 3 + 1];
    dest[i * 4 + 3] = srcline[i * 3 + 2];
  }
}

static void
274 275
putline_v308 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
276 277 278 279 280 281 282 283 284 285 286
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i * 3 + 0] = src[i * 4 + 1];
    destline[i * 3 + 1] = src[i * 4 + 2];
    destline[i * 3 + 2] = src[i * 4 + 3];
  }
}

static void
287 288
getline_AYUV (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
289 290 291 292 293
{
  memcpy (dest, FRAME_GET_LINE (src, 0, j), convert->width * 4);
}

static void
294 295
putline_AYUV (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
296 297 298 299 300 301
{
  memcpy (FRAME_GET_LINE (dest, 0, j), src, convert->width * 4);
}

#if 0
static void
302 303
getline_v410 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
304 305
{
  int i;
306
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
307 308 309 310 311 312 313 314 315 316
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = GST_READ_UINT16_LE (srcline + i * 8 + 0);
    dest[i * 4 + 1] = GST_READ_UINT16_LE (srcline + i * 8 + 2);
    dest[i * 4 + 2] = GST_READ_UINT16_LE (srcline + i * 8 + 4);
    dest[i * 4 + 3] = GST_READ_UINT16_LE (srcline + i * 8 + 6);
  }
}
#endif

static void
317 318
getline_v210 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
319 320
{
  int i;
321
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
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 376 377 378 379 380 381 382 383

  for (i = 0; i < convert->width; i += 6) {
    guint32 a0, a1, a2, a3;
    guint16 y0, y1, y2, y3, y4, y5;
    guint16 u0, u2, u4;
    guint16 v0, v2, v4;

    a0 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 0);
    a1 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 4);
    a2 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 8);
    a3 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 12);

    u0 = ((a0 >> 0) & 0x3ff) >> 2;
    y0 = ((a0 >> 10) & 0x3ff) >> 2;
    v0 = ((a0 >> 20) & 0x3ff) >> 2;
    y1 = ((a1 >> 0) & 0x3ff) >> 2;

    u2 = ((a1 >> 10) & 0x3ff) >> 2;
    y2 = ((a1 >> 20) & 0x3ff) >> 2;
    v2 = ((a2 >> 0) & 0x3ff) >> 2;
    y3 = ((a2 >> 10) & 0x3ff) >> 2;

    u4 = ((a2 >> 20) & 0x3ff) >> 2;
    y4 = ((a3 >> 0) & 0x3ff) >> 2;
    v4 = ((a3 >> 10) & 0x3ff) >> 2;
    y5 = ((a3 >> 20) & 0x3ff) >> 2;

    dest[4 * (i + 0) + 0] = 0xff;
    dest[4 * (i + 0) + 1] = y0;
    dest[4 * (i + 0) + 2] = u0;
    dest[4 * (i + 0) + 3] = v0;

    dest[4 * (i + 1) + 0] = 0xff;
    dest[4 * (i + 1) + 1] = y1;
    dest[4 * (i + 1) + 2] = u0;
    dest[4 * (i + 1) + 3] = v0;

    dest[4 * (i + 2) + 0] = 0xff;
    dest[4 * (i + 2) + 1] = y2;
    dest[4 * (i + 2) + 2] = u2;
    dest[4 * (i + 2) + 3] = v2;

    dest[4 * (i + 3) + 0] = 0xff;
    dest[4 * (i + 3) + 1] = y3;
    dest[4 * (i + 3) + 2] = u2;
    dest[4 * (i + 3) + 3] = v2;

    dest[4 * (i + 4) + 0] = 0xff;
    dest[4 * (i + 4) + 1] = y4;
    dest[4 * (i + 4) + 2] = u4;
    dest[4 * (i + 4) + 3] = v4;

    dest[4 * (i + 5) + 0] = 0xff;
    dest[4 * (i + 5) + 1] = y5;
    dest[4 * (i + 5) + 2] = u4;
    dest[4 * (i + 5) + 3] = v4;

  }

}

static void
384 385
putline_v210 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
386 387 388 389 390 391 392 393 394 395
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);

  for (i = 0; i < convert->width + 5; i += 6) {
    guint32 a0, a1, a2, a3;
    guint16 y0, y1, y2, y3, y4, y5;
    guint16 u0, u1, u2;
    guint16 v0, v1, v2;

396 397 398 399 400 401
    y0 = src[4 * (i + 0) + 1] << 2;
    y1 = src[4 * (i + 1) + 1] << 2;
    y2 = src[4 * (i + 2) + 1] << 2;
    y3 = src[4 * (i + 3) + 1] << 2;
    y4 = src[4 * (i + 4) + 1] << 2;
    y5 = src[4 * (i + 5) + 1] << 2;
402

403 404 405
    u0 = (src[4 * (i + 0) + 2] + src[4 * (i + 1) + 2]) << 1;
    u1 = (src[4 * (i + 2) + 2] + src[4 * (i + 3) + 2]) << 1;
    u2 = (src[4 * (i + 4) + 2] + src[4 * (i + 5) + 2]) << 1;
406

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 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
    v0 = (src[4 * (i + 0) + 3] + src[4 * (i + 1) + 3]) << 1;
    v1 = (src[4 * (i + 2) + 3] + src[4 * (i + 3) + 3]) << 1;
    v2 = (src[4 * (i + 4) + 3] + src[4 * (i + 5) + 3]) << 1;

    a0 = u0 | (y0 << 10) | (v0 << 20);
    a1 = y1 | (u1 << 10) | (y2 << 20);
    a2 = v1 | (y3 << 10) | (u2 << 20);
    a3 = y4 | (v2 << 10) | (y5 << 20);

    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 0, a0);
    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 4, a1);
    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 8, a2);
    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 12, a3);
  }
}

static void
getline16_v210 (ColorspaceConvert * convert, guint16 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);

  for (i = 0; i < convert->width; i += 6) {
    guint32 a0, a1, a2, a3;
    guint16 y0, y1, y2, y3, y4, y5;
    guint16 u0, u2, u4;
    guint16 v0, v2, v4;

    a0 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 0);
    a1 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 4);
    a2 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 8);
    a3 = GST_READ_UINT32_LE (srcline + (i / 6) * 16 + 12);

    u0 = ((a0 >> 0) & 0x3ff) << 6;
    y0 = ((a0 >> 10) & 0x3ff) << 6;
    v0 = ((a0 >> 20) & 0x3ff) << 6;
    y1 = ((a1 >> 0) & 0x3ff) << 6;

    u2 = ((a1 >> 10) & 0x3ff) << 6;
    y2 = ((a1 >> 20) & 0x3ff) << 6;
    v2 = ((a2 >> 0) & 0x3ff) << 6;
    y3 = ((a2 >> 10) & 0x3ff) << 6;

    u4 = ((a2 >> 20) & 0x3ff) << 6;
    y4 = ((a3 >> 0) & 0x3ff) << 6;
    v4 = ((a3 >> 10) & 0x3ff) << 6;
    y5 = ((a3 >> 20) & 0x3ff) << 6;

    dest[4 * (i + 0) + 0] = 0xffff;
    dest[4 * (i + 0) + 1] = y0;
    dest[4 * (i + 0) + 2] = u0;
    dest[4 * (i + 0) + 3] = v0;

    dest[4 * (i + 1) + 0] = 0xffff;
    dest[4 * (i + 1) + 1] = y1;
    dest[4 * (i + 1) + 2] = u0;
    dest[4 * (i + 1) + 3] = v0;

    dest[4 * (i + 2) + 0] = 0xffff;
    dest[4 * (i + 2) + 1] = y2;
    dest[4 * (i + 2) + 2] = u2;
    dest[4 * (i + 2) + 3] = v2;

    dest[4 * (i + 3) + 0] = 0xffff;
    dest[4 * (i + 3) + 1] = y3;
    dest[4 * (i + 3) + 2] = u2;
    dest[4 * (i + 3) + 3] = v2;

    dest[4 * (i + 4) + 0] = 0xffff;
    dest[4 * (i + 4) + 1] = y4;
    dest[4 * (i + 4) + 2] = u4;
    dest[4 * (i + 4) + 3] = v4;

    dest[4 * (i + 5) + 0] = 0xffff;
    dest[4 * (i + 5) + 1] = y5;
    dest[4 * (i + 5) + 2] = u4;
    dest[4 * (i + 5) + 3] = v4;

  }
}

static void
putline16_v210 (ColorspaceConvert * convert, guint8 * dest, const guint16 * src,
    int j)
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);

  for (i = 0; i < convert->width + 5; i += 6) {
    guint32 a0, a1, a2, a3;
    guint16 y0, y1, y2, y3, y4, y5;
    guint16 u0, u1, u2;
    guint16 v0, v1, v2;

    y0 = src[4 * (i + 0) + 1] >> 6;
    y1 = src[4 * (i + 1) + 1] >> 6;
    y2 = src[4 * (i + 2) + 1] >> 6;
    y3 = src[4 * (i + 3) + 1] >> 6;
    y4 = src[4 * (i + 4) + 1] >> 6;
    y5 = src[4 * (i + 5) + 1] >> 6;

    u0 = (src[4 * (i + 0) + 2] + src[4 * (i + 1) + 2] + 1) >> 7;
    u1 = (src[4 * (i + 2) + 2] + src[4 * (i + 3) + 2] + 1) >> 7;
    u2 = (src[4 * (i + 4) + 2] + src[4 * (i + 5) + 2] + 1) >> 7;

    v0 = (src[4 * (i + 0) + 3] + src[4 * (i + 1) + 3] + 1) >> 7;
    v1 = (src[4 * (i + 2) + 3] + src[4 * (i + 3) + 3] + 1) >> 7;
    v2 = (src[4 * (i + 4) + 3] + src[4 * (i + 5) + 3] + 1) >> 7;
516

517 518 519 520
    a0 = u0 | (y0 << 10) | (v0 << 20);
    a1 = y1 | (u1 << 10) | (y2 << 20);
    a2 = v1 | (y3 << 10) | (u2 << 20);
    a3 = y4 | (v2 << 10) | (y5 << 20);
521 522 523 524 525 526 527 528 529

    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 0, a0);
    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 4, a1);
    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 8, a2);
    GST_WRITE_UINT32_LE (destline + (i / 6) * 16 + 12, a3);
  }
}

static void
530 531
getline_v216 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
532 533
{
  int i;
534
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
535 536
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
537 538 539
    dest[i * 4 + 1] = GST_READ_UINT16_LE (srcline + i * 4 + 2) >> 8;
    dest[i * 4 + 2] = GST_READ_UINT16_LE (srcline + (i >> 1) * 8 + 0) >> 8;
    dest[i * 4 + 3] = GST_READ_UINT16_LE (srcline + (i >> 1) * 8 + 4) >> 8;
540 541 542 543
  }
}

static void
544 545
putline_v216 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
546 547 548 549 550 551 552 553 554 555 556
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width / 2; i++) {
    GST_WRITE_UINT16_LE (destline + i * 8 + 0, src[(i * 2 + 0) * 4 + 2] << 8);
    GST_WRITE_UINT16_LE (destline + i * 8 + 2, src[(i * 2 + 0) * 4 + 1] << 8);
    GST_WRITE_UINT16_LE (destline + i * 8 + 4, src[(i * 2 + 1) * 4 + 3] << 8);
    GST_WRITE_UINT16_LE (destline + i * 8 + 8, src[(i * 2 + 0) * 4 + 1] << 8);
  }
}

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static void
getline16_v216 (ColorspaceConvert * convert, guint16 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xffff;
    dest[i * 4 + 1] = GST_READ_UINT16_LE (srcline + i * 4 + 2);
    dest[i * 4 + 2] = GST_READ_UINT16_LE (srcline + (i >> 1) * 8 + 0);
    dest[i * 4 + 3] = GST_READ_UINT16_LE (srcline + (i >> 1) * 8 + 4);
  }
}

static void
putline16_v216 (ColorspaceConvert * convert, guint8 * dest, const guint16 * src,
    int j)
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width / 2; i++) {
    GST_WRITE_UINT16_LE (destline + i * 8 + 0, src[(i * 2 + 0) * 4 + 2]);
    GST_WRITE_UINT16_LE (destline + i * 8 + 2, src[(i * 2 + 0) * 4 + 1]);
    GST_WRITE_UINT16_LE (destline + i * 8 + 4, src[(i * 2 + 1) * 4 + 3]);
    GST_WRITE_UINT16_LE (destline + i * 8 + 8, src[(i * 2 + 0) * 4 + 1]);
  }
}

585 586 587 588
static void
getline_Y41B (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
589 590
  cogorc_getline_YUV9 (dest,
      FRAME_GET_LINE (src, 0, j),
591 592
      FRAME_GET_LINE (src, 1, j), FRAME_GET_LINE (src, 2, j),
      convert->width / 2);
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
}

static void
putline_Y41B (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint8 *destlineY = FRAME_GET_LINE (dest, 0, j);
  guint8 *destlineU = FRAME_GET_LINE (dest, 1, j);
  guint8 *destlineV = FRAME_GET_LINE (dest, 2, j);

  for (i = 0; i < convert->width - 3; i += 4) {
    destlineY[i] = src[i * 4 + 1];
    destlineY[i + 1] = src[i * 4 + 5];
    destlineY[i + 2] = src[i * 4 + 9];
    destlineY[i + 3] = src[i * 4 + 13];

    destlineU[i >> 2] =
        (src[i * 4 + 2] + src[i * 4 + 6] + src[i * 4 + 10] + src[i * 4 + 14] +
        2) >> 2;
    destlineV[i >> 2] =
        (src[i * 4 + 3] + src[i * 4 + 7] + src[i * 4 + 11] + src[i * 4 + 15] +
        2) >> 2;
  }

  if (i == convert->width - 3) {
    destlineY[i] = src[i * 4 + 1];
    destlineY[i + 1] = src[i * 4 + 5];
    destlineY[i + 2] = src[i * 4 + 9];

    destlineU[i >> 2] =
        (src[i * 4 + 2] + src[i * 4 + 6] + src[i * 4 + 10] + 1) / 3;
    destlineV[i >> 2] =
        (src[i * 4 + 3] + src[i * 4 + 7] + src[i * 4 + 11] + 1) / 3;
  } else if (i == convert->width - 2) {
    destlineY[i] = src[i * 4 + 1];
    destlineY[i + 1] = src[i * 4 + 5];

    destlineU[i >> 2] = (src[i * 4 + 2] + src[i * 4 + 6] + 1) >> 1;
    destlineV[i >> 2] = (src[i * 4 + 3] + src[i * 4 + 7] + 1) >> 1;
  } else if (i == convert->width - 1) {
    destlineY[i + 1] = src[i * 4 + 5];

    destlineU[i >> 2] = src[i * 4 + 2];
    destlineV[i >> 2] = src[i * 4 + 3];
  }
}

641
static void
642 643
getline_Y42B (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
644 645 646 647 648 649 650 651
{
  cogorc_getline_Y42B (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j),
      FRAME_GET_LINE (src, 2, j), convert->width / 2);
}

static void
652 653
putline_Y42B (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
654 655 656 657 658 659 660
{
  cogorc_putline_Y42B (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 1, j),
      FRAME_GET_LINE (dest, 2, j), src, convert->width / 2);
}

static void
661 662
getline_Y444 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
663 664 665 666 667 668 669
{
  cogorc_getline_Y444 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j), FRAME_GET_LINE (src, 2, j), convert->width);
}

static void
670 671
putline_Y444 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
672 673 674 675 676 677 678
{
  cogorc_putline_Y444 (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 1, j),
      FRAME_GET_LINE (dest, 2, j), src, convert->width);
}

static void
679 680
getline_Y800 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
681 682 683 684 685
{
  cogorc_getline_Y800 (dest, FRAME_GET_LINE (src, 0, j), convert->width);
}

static void
686 687
putline_Y800 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
688 689 690 691
{
  cogorc_putline_Y800 (FRAME_GET_LINE (dest, 0, j), src, convert->width);
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705
static void
getline_Y16 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  cogorc_getline_Y16 (dest, FRAME_GET_LINE (src, 0, j), convert->width);
}

static void
putline_Y16 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  cogorc_putline_Y16 (FRAME_GET_LINE (dest, 0, j), src, convert->width);
}

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 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
static void
getline_RGB16 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint16 *srcline = (const guint16 *) FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = ((srcline[i] >> 11) & 0x1f) << 3;
    dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x3f) << 2;
    dest[i * 4 + 3] = ((srcline[i]) & 0x1f) << 3;
  }
}

static void
putline_RGB16 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint16 *destline = (guint16 *) FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i] =
        ((src[i * 4 + 1] >> 3) << 11) | ((src[i * 4 +
                2] >> 2) << 5) | (src[i * 4 + 3] >> 3);
  }
}

static void
getline_BGR16 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint16 *srcline = (const guint16 *) FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 3] = ((srcline[i] >> 11) & 0x1f) << 3;
    dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x3f) << 2;
    dest[i * 4 + 1] = ((srcline[i]) & 0x1f) << 3;
  }
}

static void
putline_BGR16 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint16 *destline = (guint16 *) FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i] =
        ((src[i * 4 + 3] >> 3) << 11) | ((src[i * 4 +
                2] >> 2) << 5) | (src[i * 4 + 1] >> 3);
  }
}

760 761 762 763 764 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 809 810 811 812 813
static void
getline_RGB15 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint16 *srcline = (const guint16 *) FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = ((srcline[i] >> 10) & 0x1f) << 3;
    dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x1f) << 3;
    dest[i * 4 + 3] = ((srcline[i]) & 0x1f) << 3;
  }
}

static void
putline_RGB15 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint16 *destline = (guint16 *) FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i] =
        ((src[i * 4 + 1] >> 3) << 10) | ((src[i * 4 +
                2] >> 3) << 5) | (src[i * 4 + 3] >> 3);
  }
}

static void
getline_BGR15 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint16 *srcline = (const guint16 *) FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 3] = ((srcline[i] >> 10) & 0x1f) << 3;
    dest[i * 4 + 2] = ((srcline[i] >> 5) & 0x1f) << 3;
    dest[i * 4 + 1] = ((srcline[i]) & 0x1f) << 3;
  }
}

static void
putline_BGR15 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint16 *destline = (guint16 *) FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i] =
        ((src[i * 4 + 3] >> 3) << 10) | ((src[i * 4 +
                2] >> 3) << 5) | (src[i * 4 + 1] >> 3);
  }
}

814
static void
815 816
getline_BGRA (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
817 818 819 820 821
{
  cogorc_getline_BGRA (dest, FRAME_GET_LINE (src, 0, j), convert->width);
}

static void
822 823
putline_BGRA (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
824 825 826 827 828
{
  cogorc_putline_BGRA (FRAME_GET_LINE (dest, 0, j), src, convert->width);
}

static void
829 830
getline_ABGR (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
831 832 833 834 835
{
  cogorc_getline_ABGR (dest, FRAME_GET_LINE (src, 0, j), convert->width);
}

static void
836 837
putline_ABGR (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
838 839 840 841 842
{
  cogorc_putline_ABGR (FRAME_GET_LINE (dest, 0, j), src, convert->width);
}

static void
843 844
getline_RGBA (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
845 846 847 848 849
{
  cogorc_getline_RGBA (dest, FRAME_GET_LINE (src, 0, j), convert->width);
}

static void
850 851
putline_RGBA (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
852 853 854 855 856
{
  cogorc_putline_RGBA (FRAME_GET_LINE (dest, 0, j), src, convert->width);
}

static void
857 858
getline_RGB (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
859 860
{
  int i;
861
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
862 863 864 865 866 867 868 869 870
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = srcline[i * 3 + 0];
    dest[i * 4 + 2] = srcline[i * 3 + 1];
    dest[i * 4 + 3] = srcline[i * 3 + 2];
  }
}

static void
871 872
putline_RGB (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
873 874 875 876 877 878 879 880 881 882 883
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i * 3 + 0] = src[i * 4 + 1];
    destline[i * 3 + 1] = src[i * 4 + 2];
    destline[i * 3 + 2] = src[i * 4 + 3];
  }
}

static void
884 885
getline_BGR (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
886 887
{
  int i;
888
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
889 890 891 892 893 894 895 896 897
  for (i = 0; i < convert->width; i++) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = srcline[i * 3 + 2];
    dest[i * 4 + 2] = srcline[i * 3 + 1];
    dest[i * 4 + 3] = srcline[i * 3 + 0];
  }
}

static void
898 899
putline_BGR (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
900 901 902 903 904 905 906 907 908 909 910
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width; i++) {
    destline[i * 3 + 0] = src[i * 4 + 3];
    destline[i * 3 + 1] = src[i * 4 + 2];
    destline[i * 3 + 2] = src[i * 4 + 1];
  }
}

static void
911 912
getline_NV12 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
913 914 915 916 917 918 919
{
  cogorc_getline_NV12 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j >> 1), convert->width / 2);
}

static void
920 921
putline_NV12 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
922 923 924 925 926 927
{
  cogorc_putline_NV12 (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 1, j >> 1), src, convert->width / 2);
}

static void
928 929
getline_NV21 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
930 931 932 933 934 935 936
{
  cogorc_getline_NV21 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 2, j >> 1), convert->width / 2);
}

static void
937 938
putline_NV21 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
939 940 941 942 943 944
{
  cogorc_putline_NV21 (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 2, j >> 1), src, convert->width / 2);
}

static void
945 946
getline_UYVP (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
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
{
  int i;

  for (i = 0; i < convert->width; i += 2) {
    guint16 y0, y1;
    guint16 u0;
    guint16 v0;

    u0 = (src[(i / 2) * 5 + 0] << 2) | (src[(i / 2) * 5 + 1] >> 6);
    y0 = ((src[(i / 2) * 5 + 1] & 0x3f) << 4) | (src[(i / 2) * 5 + 2] >> 4);
    v0 = ((src[(i / 2) * 5 + 2] & 0x0f) << 6) | (src[(i / 2) * 5 + 3] >> 2);
    y1 = ((src[(i / 2) * 5 + 3] & 0x03) << 8) | src[(i / 2) * 5 + 4];

    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = y0 >> 2;
    dest[i * 4 + 2] = u0 >> 2;
    dest[i * 4 + 3] = v0 >> 2;
    dest[i * 4 + 4] = 0xff;
    dest[i * 4 + 5] = y1 >> 2;
    dest[i * 4 + 6] = u0 >> 2;
    dest[i * 4 + 7] = v0 >> 2;
  }
}

static void
972 973
putline_UYVP (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
{
  int i;

  for (i = 0; i < convert->width; i += 2) {
    guint16 y0, y1;
    guint16 u0;
    guint16 v0;

    y0 = src[4 * (i + 0) + 1];
    y1 = src[4 * (i + 1) + 1];
    u0 = (src[4 * (i + 0) + 2] + src[4 * (i + 1) + 2] + 1) >> 1;
    v0 = (src[4 * (i + 0) + 3] + src[4 * (i + 1) + 3] + 1) >> 1;

    dest[(i / 2) * 5 + 0] = u0;
    dest[(i / 2) * 5 + 1] = y0 >> 2;
    dest[(i / 2) * 5 + 2] = (y0 << 6) | (v0 >> 4);
    dest[(i / 2) * 5 + 3] = (v0 << 4) | (y1 >> 2);
    dest[(i / 2) * 5 + 4] = (y1 << 2);
  }
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
static void
getline_A420 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  cogorc_getline_A420 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j >> 1),
      FRAME_GET_LINE (src, 2, j >> 1),
      FRAME_GET_LINE (src, 3, j), convert->width);
}

static void
putline_A420 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  cogorc_putline_A420 (FRAME_GET_LINE (dest, 0, j),
      FRAME_GET_LINE (dest, 1, j >> 1),
      FRAME_GET_LINE (dest, 2, j >> 1),
      FRAME_GET_LINE (dest, 3, j), src, convert->width / 2);
}

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
static void
getline_RGB8P (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width; i++) {
    guint32 v = convert->palette[srcline[i]];
    dest[i * 4 + 0] = (v >> 24) & 0xff;
    dest[i * 4 + 1] = (v >> 16) & 0xff;
    dest[i * 4 + 2] = (v >> 8) & 0xff;
    dest[i * 4 + 3] = (v) & 0xff;
  }
}

static void
putline_RGB8P (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);
  /* Use our poor man's palette, taken from ffmpegcolorspace too */
  for (i = 0; i < convert->width; i++) {
    /* crude approximation for alpha ! */
    if (src[i * 4 + 0] < 0x80)
      destline[i] = 6 * 6 * 6;
    else
      destline[i] =
          ((((src[i * 4 + 1]) / 47) % 6) * 6 * 6 + (((src[i * 4 +
                          2]) / 47) % 6) * 6 + (((src[i * 4 + 3]) / 47) % 6));
  }
}
1048 1049 1050 1051 1052 1053 1054 1055 1056


static void
getline_YUV9 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  cogorc_getline_YUV9 (dest,
      FRAME_GET_LINE (src, 0, j),
      FRAME_GET_LINE (src, 1, j >> 2),
1057
      FRAME_GET_LINE (src, 2, j >> 2), convert->width / 2);
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 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
}

static void
putline_YUV9 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint8 *destY = FRAME_GET_LINE (dest, 0, j);
  guint8 *destU = FRAME_GET_LINE (dest, 1, j >> 2);
  guint8 *destV = FRAME_GET_LINE (dest, 2, j >> 2);

  for (i = 0; i < convert->width - 3; i += 4) {
    destY[i] = src[i * 4 + 1];
    destY[i + 1] = src[i * 4 + 5];
    destY[i + 2] = src[i * 4 + 9];
    destY[i + 3] = src[i * 4 + 13];
    if (j % 4 == 0) {
      destU[i >> 2] =
          (src[i * 4 + 2] + src[i * 4 + 6] + src[i * 4 + 10] + src[i * 4 +
              14]) >> 2;
      destV[i >> 2] =
          (src[i * 4 + 3] + src[i * 4 + 7] + src[i * 4 + 11] + src[i * 4 +
              15]) >> 2;
    }
  }

  if (i == convert->width - 3) {
    destY[i] = src[i * 4 + 1];
    destY[i + 1] = src[i * 4 + 5];
    destY[i + 2] = src[i * 4 + 9];
    if (j % 4 == 0) {
      destU[i >> 2] = (src[i * 4 + 2] + src[i * 4 + 6] + src[i * 4 + 10]) / 3;
      destV[i >> 2] = (src[i * 4 + 3] + src[i * 4 + 7] + src[i * 4 + 11]) / 3;
    }
  } else if (i == convert->width - 2) {
    destY[i] = src[i * 4 + 1];
    destY[i + 1] = src[i * 4 + 5];
    if (j % 4 == 0) {
      destU[i >> 2] = (src[i * 4 + 2] + src[i * 4 + 6]) >> 1;
      destV[i >> 2] = (src[i * 4 + 3] + src[i * 4 + 7]) >> 1;
    }
  } else if (i == convert->width - 1) {
    destY[i] = src[i * 4 + 1];
    destU[i >> 2] = src[i * 4 + 2];
    destV[i >> 2] = src[i * 4 + 3];
  }
}
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 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

static void
getline_IYU1 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint8 *srcline = FRAME_GET_LINE (src, 0, j);

  for (i = 0; i < convert->width - 3; i += 4) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 4] = 0xff;
    dest[i * 4 + 8] = 0xff;
    dest[i * 4 + 12] = 0xff;
    dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
    dest[i * 4 + 5] = srcline[(i >> 2) * 6 + 2];
    dest[i * 4 + 9] = srcline[(i >> 2) * 6 + 4];
    dest[i * 4 + 13] = srcline[(i >> 2) * 6 + 5];
    dest[i * 4 + 2] = dest[i * 4 + 6] = dest[i * 4 + 10] = dest[i * 4 + 14] =
        srcline[(i >> 2) * 6 + 0];
    dest[i * 4 + 3] = dest[i * 4 + 7] = dest[i * 4 + 11] = dest[i * 4 + 15] =
        srcline[(i >> 2) * 6 + 3];
  }

  if (i == convert->width - 3) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 4] = 0xff;
    dest[i * 4 + 8] = 0xff;
    dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
    dest[i * 4 + 5] = srcline[(i >> 2) * 6 + 2];
    dest[i * 4 + 9] = srcline[(i >> 2) * 6 + 4];
    dest[i * 4 + 2] = dest[i * 4 + 6] = dest[i * 4 + 10] =
        srcline[(i >> 2) * 6 + 0];
    dest[i * 4 + 3] = dest[i * 4 + 7] = dest[i * 4 + 11] =
        srcline[(i >> 2) * 6 + 3];
  } else if (i == convert->width - 2) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 4] = 0xff;
    dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
    dest[i * 4 + 5] = srcline[(i >> 2) * 6 + 2];
    dest[i * 4 + 2] = dest[i * 4 + 6] = srcline[(i >> 2) * 6 + 0];
    dest[i * 4 + 3] = dest[i * 4 + 7] = srcline[(i >> 2) * 6 + 3];
  } else if (i == convert->width - 1) {
    dest[i * 4 + 0] = 0xff;
    dest[i * 4 + 1] = srcline[(i >> 2) * 6 + 1];
    dest[i * 4 + 2] = srcline[(i >> 2) * 6 + 0];
    dest[i * 4 + 3] = srcline[(i >> 2) * 6 + 3];
  }
}

static void
putline_IYU1 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint8 *destline = FRAME_GET_LINE (dest, 0, j);

  for (i = 0; i < convert->width - 3; i += 4) {
    destline[(i >> 2) * 6 + 1] = src[i * 4 + 1];
    destline[(i >> 2) * 6 + 2] = src[i * 4 + 5];
    destline[(i >> 2) * 6 + 4] = src[i * 4 + 9];
    destline[(i >> 2) * 6 + 5] = src[i * 4 + 13];
    destline[(i >> 2) * 6 + 0] =
        (src[i * 4 + 2] + src[i * 4 + 6] + src[i * 4 + 10] + src[i * 4 +
            14]) >> 2;
    destline[(i >> 2) * 6 + 3] =
        (src[i * 4 + 3] + src[i * 4 + 7] + src[i * 4 + 11] + src[i * 4 +
            15]) >> 2;
  }

  if (i == convert->width - 3) {
    destline[(i >> 2) * 6 + 1] = src[i * 4 + 1];
    destline[(i >> 2) * 6 + 2] = src[i * 4 + 5];
    destline[(i >> 2) * 6 + 4] = src[i * 4 + 9];
    destline[(i >> 2) * 6 + 0] =
        (src[i * 4 + 2] + src[i * 4 + 6] + src[i * 4 + 10]) / 3;
    destline[(i >> 2) * 6 + 3] =
        (src[i * 4 + 3] + src[i * 4 + 7] + src[i * 4 + 11]) / 3;
  } else if (i == convert->width - 2) {
    destline[(i >> 2) * 6 + 1] = src[i * 4 + 1];
    destline[(i >> 2) * 6 + 2] = src[i * 4 + 5];
    destline[(i >> 2) * 6 + 0] = (src[i * 4 + 2] + src[i * 4 + 6]) >> 1;
    destline[(i >> 2) * 6 + 3] = (src[i * 4 + 3] + src[i * 4 + 7]) >> 1;
  } else if (i == convert->width - 1) {
    destline[(i >> 2) * 6 + 1] = src[i * 4 + 1];
    destline[(i >> 2) * 6 + 0] = src[i * 4 + 2];
    destline[(i >> 2) * 6 + 3] = src[i * 4 + 3];
  }
}
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250

static void
getline_AY64 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  const guint16 *srcline = (const guint16 *) FRAME_GET_LINE (src, 0, j);
  for (i = 0; i < convert->width * 4; i++) {
    dest[i] = srcline[i] >> 8;
  }
}

static void
putline_AY64 (ColorspaceConvert * convert, guint8 * dest, const guint8 * src,
    int j)
{
  int i;
  guint16 *destline = (guint16 *) FRAME_GET_LINE (dest, 0, j);
  for (i = 0; i < convert->width * 4; i++) {
    destline[i] = src[i] << 8;
  }
}

static void
getline16_AY64 (ColorspaceConvert * convert, guint16 * dest, const guint8 * src,
    int j)
{
  memcpy (dest, FRAME_GET_LINE (src, 0, j), convert->width * 8);
}

static void
putline16_AY64 (ColorspaceConvert * convert, guint8 * dest, const guint16 * src,
    int j)
{
  memcpy (FRAME_GET_LINE (dest, 0, j), src, convert->width * 8);
}

static void
getline16_convert (ColorspaceConvert * convert, guint16 * dest,
    const guint8 * src, int j)
{
  int i;
  convert->getline (convert, convert->tmpline, src, j);
  for (i = 0; i < convert->width * 4; i++) {
    dest[i] = convert->tmpline[i] << 8;
  }
}

static void
putline16_convert (ColorspaceConvert * convert, guint8 * dest,
    const guint16 * src, int j)
{
  int i;
  for (i = 0; i < convert->width * 4; i++) {
    convert->tmpline[i] = src[i] >> 8;
  }
  convert->putline (convert, dest, convert->tmpline, j);
}
1251

1252 1253 1254
typedef struct
{
  GstVideoFormat format;
1255 1256 1257 1258
  void (*getline) (ColorspaceConvert * convert, guint8 * dest,
      const guint8 * src, int j);
  void (*putline) (ColorspaceConvert * convert, guint8 * dest,
      const guint8 * src, int j);
1259 1260 1261 1262
  void (*getline16) (ColorspaceConvert * convert, guint16 * dest,
      const guint8 * src, int j);
  void (*putline16) (ColorspaceConvert * convert, guint8 * dest,
      const guint16 * src, int j);
1263
} ColorspaceLine;
1264
static const ColorspaceLine lines[] = {
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
  {GST_VIDEO_FORMAT_I420, getline_I420, putline_I420},
  {GST_VIDEO_FORMAT_YV12, getline_YV12, putline_YV12},
  {GST_VIDEO_FORMAT_YUY2, getline_YUY2, putline_YUY2},
  {GST_VIDEO_FORMAT_UYVY, getline_UYVY, putline_UYVY},
  {GST_VIDEO_FORMAT_AYUV, getline_AYUV, putline_AYUV},
  {GST_VIDEO_FORMAT_RGBx, getline_RGBA, putline_RGBA},
  {GST_VIDEO_FORMAT_BGRx, getline_BGRA, putline_BGRA},
  {GST_VIDEO_FORMAT_xRGB, getline_AYUV, putline_AYUV},
  {GST_VIDEO_FORMAT_xBGR, getline_ABGR, putline_ABGR},
  {GST_VIDEO_FORMAT_RGBA, getline_RGBA, putline_RGBA},
  {GST_VIDEO_FORMAT_BGRA, getline_BGRA, putline_BGRA},
  {GST_VIDEO_FORMAT_ARGB, getline_AYUV, putline_AYUV},
  {GST_VIDEO_FORMAT_ABGR, getline_ABGR, putline_ABGR},
  {GST_VIDEO_FORMAT_RGB, getline_RGB, putline_RGB},
  {GST_VIDEO_FORMAT_BGR, getline_BGR, putline_BGR},
1280
  {GST_VIDEO_FORMAT_Y41B, getline_Y41B, putline_Y41B},
1281 1282 1283
  {GST_VIDEO_FORMAT_Y42B, getline_Y42B, putline_Y42B},
  {GST_VIDEO_FORMAT_YVYU, getline_YVYU, putline_YVYU},
  {GST_VIDEO_FORMAT_Y444, getline_Y444, putline_Y444},
1284 1285 1286 1287
  {GST_VIDEO_FORMAT_v210, getline_v210, putline_v210,
      getline16_v210, putline16_v210},
  {GST_VIDEO_FORMAT_v216, getline_v216, putline_v216,
      getline16_v216, putline16_v216},
1288 1289 1290 1291 1292 1293 1294
  {GST_VIDEO_FORMAT_NV12, getline_NV12, putline_NV12},
  {GST_VIDEO_FORMAT_NV21, getline_NV21, putline_NV21},
  //{GST_VIDEO_FORMAT_GRAY8, getline_GRAY8, putline_GRAY8},
  //{GST_VIDEO_FORMAT_GRAY16_BE, getline_GRAY16_BE, putline_GRAY16_BE},
  //{GST_VIDEO_FORMAT_GRAY16_LE, getline_GRAY16_LE, putline_GRAY16_LE},
  {GST_VIDEO_FORMAT_v308, getline_v308, putline_v308},
  {GST_VIDEO_FORMAT_Y800, getline_Y800, putline_Y800},
1295
  {GST_VIDEO_FORMAT_Y16, getline_Y16, putline_Y16},
1296 1297
  {GST_VIDEO_FORMAT_RGB16, getline_RGB16, putline_RGB16},
  {GST_VIDEO_FORMAT_BGR16, getline_BGR16, putline_BGR16},
1298 1299
  {GST_VIDEO_FORMAT_RGB15, getline_RGB15, putline_RGB15},
  {GST_VIDEO_FORMAT_BGR15, getline_BGR15, putline_BGR15},
1300 1301
  {GST_VIDEO_FORMAT_UYVP, getline_UYVP, putline_UYVP},
  {GST_VIDEO_FORMAT_A420, getline_A420, putline_A420}
1302 1303 1304
  , {GST_VIDEO_FORMAT_RGB8_PALETTED, getline_RGB8P, putline_RGB8P},
  {GST_VIDEO_FORMAT_YUV9, getline_YUV9, putline_YUV9},
  {GST_VIDEO_FORMAT_YVU9, getline_YUV9, putline_YUV9},  /* alias */
1305 1306 1307 1308 1309
  {GST_VIDEO_FORMAT_IYU1, getline_IYU1, putline_IYU1},
  {GST_VIDEO_FORMAT_ARGB64, getline_AY64, putline_AY64, getline16_AY64,
      putline16_AY64},
  {GST_VIDEO_FORMAT_AYUV64, getline_AY64, putline_AY64, getline16_AY64,
      putline16_AY64}
1310 1311 1312
};

static void
1313
matrix_rgb_to_yuv_bt470_6 (ColorspaceConvert * convert)
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
{
  int i;
  int r, g, b;
  int y, u, v;
  guint8 *tmpline = convert->tmpline;

  for (i = 0; i < convert->width; i++) {
    r = tmpline[i * 4 + 1];
    g = tmpline[i * 4 + 2];
    b = tmpline[i * 4 + 3];

    y = (66 * r + 129 * g + 25 * b + 4096) >> 8;
    u = (-38 * r - 74 * g + 112 * b + 32768) >> 8;
    v = (112 * r - 94 * g - 18 * b + 32768) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
  }
}

static void
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
matrix_rgb_to_yuv_bt709 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint8 *tmpline = convert->tmpline;

  for (i = 0; i < convert->width; i++) {
    r = tmpline[i * 4 + 1];
    g = tmpline[i * 4 + 2];
    b = tmpline[i * 4 + 3];

    y = (47 * r + 157 * g + 16 * b + 4096) >> 8;
    u = (-26 * r - 87 * g + 112 * b + 32768) >> 8;
    v = (112 * r - 102 * g - 10 * b + 32768) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 255);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 255);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 255);
  }
}

static void
matrix_yuv_bt470_6_to_rgb (ColorspaceConvert * convert)
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
{
  int i;
  int r, g, b;
  int y, u, v;
  guint8 *tmpline = convert->tmpline;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (298 * y + 409 * v - 57068) >> 8;
    g = (298 * y - 100 * u - 208 * v + 34707) >> 8;
    b = (298 * y + 516 * u - 70870) >> 8;

    tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
    tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
    tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
  }
}

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
static void
matrix_yuv_bt709_to_rgb (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint8 *tmpline = convert->tmpline;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (298 * y + 459 * v - 63514) >> 8;
    g = (298 * y - 55 * u - 136 * v + 19681) >> 8;
    b = (298 * y + 541 * u - 73988) >> 8;

    tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
    tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
    tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
  }
}

static void
matrix_yuv_bt709_to_yuv_bt470_6 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint8 *tmpline = convert->tmpline;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (256 * y + 25 * u + 49 * v - 9536) >> 8;
    g = (253 * u - 28 * v + 3958) >> 8;
    b = (-19 * u + 252 * v + 2918) >> 8;

    tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
    tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
    tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
  }
}

static void
matrix_yuv_bt470_6_to_yuv_bt709 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint8 *tmpline = convert->tmpline;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (256 * y - 30 * u - 53 * v + 10600) >> 8;
    g = (261 * u + 29 * v - 4367) >> 8;
    b = (19 * u + 262 * v - 3289) >> 8;

    tmpline[i * 4 + 1] = CLAMP (r, 0, 255);
    tmpline[i * 4 + 2] = CLAMP (g, 0, 255);
    tmpline[i * 4 + 3] = CLAMP (b, 0, 255);
  }
}

1450 1451 1452 1453 1454 1455
static void
matrix_identity (ColorspaceConvert * convert)
{
  /* do nothing */
}

1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 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 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600
static void
matrix16_rgb_to_yuv_bt470_6 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *tmpline = convert->tmpline16;

  for (i = 0; i < convert->width; i++) {
    r = tmpline[i * 4 + 1];
    g = tmpline[i * 4 + 2];
    b = tmpline[i * 4 + 3];

    y = (66 * r + 129 * g + 25 * b + 4096 * 256) >> 8;
    u = (-38 * r - 74 * g + 112 * b + 32768 * 256) >> 8;
    v = (112 * r - 94 * g - 18 * b + 32768 * 256) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
  }
}

static void
matrix16_rgb_to_yuv_bt709 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *tmpline = convert->tmpline16;

  for (i = 0; i < convert->width; i++) {
    r = tmpline[i * 4 + 1];
    g = tmpline[i * 4 + 2];
    b = tmpline[i * 4 + 3];

    y = (47 * r + 157 * g + 16 * b + 4096 * 256) >> 8;
    u = (-26 * r - 87 * g + 112 * b + 32768 * 256) >> 8;
    v = (112 * r - 102 * g - 10 * b + 32768 * 256) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
  }
}

static void
matrix16_yuv_bt470_6_to_rgb (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *tmpline = convert->tmpline16;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (298 * y + 409 * v - 57068 * 256) >> 8;
    g = (298 * y - 100 * u - 208 * v + 34707 * 256) >> 8;
    b = (298 * y + 516 * u - 70870 * 256) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
  }
}

static void
matrix16_yuv_bt709_to_rgb (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *tmpline = convert->tmpline16;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (298 * y + 459 * v - 63514 * 256) >> 8;
    g = (298 * y - 55 * u - 136 * v + 19681 * 256) >> 8;
    b = (298 * y + 541 * u - 73988 * 256) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
  }
}

static void
matrix16_yuv_bt709_to_yuv_bt470_6 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *tmpline = convert->tmpline16;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (256 * y + 25 * u + 49 * v - 9536 * 256) >> 8;
    g = (253 * u - 28 * v + 3958 * 256) >> 8;
    b = (-19 * u + 252 * v + 2918 * 256) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
  }
}

static void
matrix16_yuv_bt470_6_to_yuv_bt709 (ColorspaceConvert * convert)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *tmpline = convert->tmpline16;

  for (i = 0; i < convert->width; i++) {
    y = tmpline[i * 4 + 1];
    u = tmpline[i * 4 + 2];
    v = tmpline[i * 4 + 3];

    r = (256 * y - 30 * u - 53 * v + 10600 * 256) >> 8;
    g = (261 * u + 29 * v - 4367 * 256) >> 8;
    b = (19 * u + 262 * v - 3289 * 256) >> 8;

    tmpline[i * 4 + 1] = CLAMP (y, 0, 65535);
    tmpline[i * 4 + 2] = CLAMP (u, 0, 65535);
    tmpline[i * 4 + 3] = CLAMP (v, 0, 65535);
  }
}

static void
matrix16_identity (ColorspaceConvert * convert)
{
  /* do nothing */
}


1601 1602 1603 1604 1605 1606 1607

static void
colorspace_convert_lookup_getput (ColorspaceConvert * convert)
{
  int i;

  convert->getline = NULL;
1608
  convert->getline16 = NULL;
1609 1610 1611
  for (i = 0; i < sizeof (lines) / sizeof (lines[0]); i++) {
    if (lines[i].format == convert->from_format) {
      convert->getline = lines[i].getline;
1612
      convert->getline16 = lines[i].getline16;
1613 1614 1615 1616
      break;
    }
  }
  convert->putline = NULL;
1617
  convert->putline16 = NULL;
1618 1619 1620
  for (i = 0; i < sizeof (lines) / sizeof (lines[0]); i++) {
    if (lines[i].format == convert->to_format) {
      convert->putline = lines[i].putline;
1621
      convert->putline16 = lines[i].putline16;
1622 1623 1624
      break;
    }
  }
1625
  GST_DEBUG ("get %p put %p", convert->getline, convert->putline);
1626

1627 1628 1629 1630 1631 1632 1633 1634
  if (convert->getline16 == NULL) {
    convert->getline16 = getline16_convert;
  }
  if (convert->putline16 == NULL) {
    convert->putline16 = putline16_convert;
  }

  if (convert->from_spec == convert->to_spec) {
1635
    convert->matrix = matrix_identity;
1636 1637 1638
    convert->matrix16 = matrix16_identity;
  } else if (convert->from_spec == COLOR_SPEC_RGB
      && convert->to_spec == COLOR_SPEC_YUV_BT470_6) {
1639
    convert->matrix = matrix_rgb_to_yuv_bt470_6;
1640 1641 1642
    convert->matrix16 = matrix16_rgb_to_yuv_bt470_6;
  } else if (convert->from_spec == COLOR_SPEC_RGB
      && convert->to_spec == COLOR_SPEC_YUV_BT709) {
1643
    convert->matrix = matrix_rgb_to_yuv_bt709;
1644 1645 1646
    convert->matrix16 = matrix16_rgb_to_yuv_bt709;
  } else if (convert->from_spec == COLOR_SPEC_YUV_BT470_6
      && convert->to_spec == COLOR_SPEC_RGB) {
1647
    convert->matrix = matrix_yuv_bt470_6_to_rgb;
1648 1649 1650
    convert->matrix16 = matrix16_yuv_bt470_6_to_rgb;
  } else if (convert->from_spec == COLOR_SPEC_YUV_BT709
      && convert->to_spec == COLOR_SPEC_RGB) {
1651
    convert->matrix = matrix_yuv_bt709_to_rgb;
1652 1653 1654
    convert->matrix16 = matrix16_yuv_bt709_to_rgb;
  } else if (convert->from_spec == COLOR_SPEC_YUV_BT709
      && convert->to_spec == COLOR_SPEC_YUV_BT470_6) {
1655
    convert->matrix = matrix_yuv_bt709_to_yuv_bt470_6;
1656 1657 1658
    convert->matrix16 = matrix16_yuv_bt709_to_yuv_bt470_6;
  } else if (convert->from_spec == COLOR_SPEC_YUV_BT470_6
      && convert->to_spec == COLOR_SPEC_YUV_BT709) {
1659
    convert->matrix = matrix_yuv_bt470_6_to_yuv_bt709;
1660 1661
    convert->matrix16 = matrix16_yuv_bt470_6_to_yuv_bt709;
  }
1662 1663 1664 1665
}

static void
colorspace_convert_generic (ColorspaceConvert * convert, guint8 * dest,
1666
    const guint8 * src)
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
{
  int j;

  if (convert->getline == NULL) {
    GST_ERROR ("no getline");
    return;
  }

  if (convert->putline == NULL) {
    GST_ERROR ("no putline");
    return;
  }

1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
  if (convert->use_16bit) {
    for (j = 0; j < convert->height; j++) {
      convert->getline16 (convert, convert->tmpline16, src, j);
      convert->matrix16 (convert);
      convert->putline16 (convert, dest, convert->tmpline16, j);
    }
  } else {
    for (j = 0; j < convert->height; j++) {
      convert->getline (convert, convert->tmpline, src, j);
      convert->matrix (convert);
      convert->putline (convert, dest, convert->tmpline, j);
    }
1692 1693 1694 1695 1696 1697 1698
  }
}


/* Fast paths */

static void
1699 1700
convert_I420_YUY2 (ColorspaceConvert * convert, guint8 * dest,
    const guint8 * src)