video-converter.c 212 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
 *
 * 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
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19 20 21 22 23 24
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

25 26 27 28 29 30 31
#if 0
#ifdef HAVE_PTHREAD
#define _GNU_SOURCE
#include <pthread.h>
#endif
#endif

Wim Taymans's avatar
Wim Taymans committed
32
#include "video-converter.h"
33

34 35
#include <glib.h>
#include <string.h>
36 37
#include <math.h>

38
#include "video-orc.h"
39

40
/**
Wim Taymans's avatar
Wim Taymans committed
41
 * SECTION:videoconverter
42
 * @title: GstVideoConverter
43 44 45 46
 * @short_description: Generic video conversion
 *
 * This object is used to convert video frames from one format to another.
 * The object can perform conversion of:
47 48 49 50 51 52
 *
 *  * video format
 *  * video colorspace
 *  * chroma-siting
 *  * video size
 *
53 54
 */

Wim Taymans's avatar
Wim Taymans committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/*
 * (a)  unpack
 * (b)  chroma upsample
 * (c)  (convert Y'CbCr to R'G'B')
 * (d)  gamma decode
 * (e)  downscale
 * (f)  colorspace convert through XYZ
 * (g)  upscale
 * (h)  gamma encode
 * (i)  (convert R'G'B' to Y'CbCr)
 * (j)  chroma downsample
 * (k)  pack
 *
 * quality options
 *
 *  (a) range truncate, range expand
 *  (b) full upsample, 1-1 non-cosited upsample, no upsample
 *  (c) 8 bits, 16 bits
 *  (d)
 *  (e) 8 bits, 16 bits
 *  (f) 8 bits, 16 bits
 *  (g) 8 bits, 16 bits
 *  (h)
 *  (i) 8 bits, 16 bits
 *  (j) 1-1 cosited downsample, no downsample
 *  (k)
 *
 *
 *         1 : a ->   ->   ->   -> e  -> f  -> g  ->   ->   ->   -> k
 *         2 : a ->   ->   ->   -> e  -> f* -> g  ->   ->   ->   -> k
 *         3 : a ->   ->   ->   -> e* -> f* -> g* ->   ->   ->   -> k
 *         4 : a -> b ->   ->   -> e  -> f  -> g  ->   ->   -> j -> k
 *         5 : a -> b ->   ->   -> e* -> f* -> g* ->   ->   -> j -> k
 *         6 : a -> b -> c -> d -> e  -> f  -> g  -> h -> i -> j -> k
 *         7 : a -> b -> c -> d -> e* -> f* -> g* -> h -> i -> j -> k
 *
 *         8 : a -> b -> c -> d -> e* -> f* -> g* -> h -> i -> j -> k
 *         9 : a -> b -> c -> d -> e* -> f* -> g* -> h -> i -> j -> k
 *        10 : a -> b -> c -> d -> e* -> f* -> g* -> h -> i -> j -> k
 */
Wim Taymans's avatar
Wim Taymans committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

#ifndef GST_DISABLE_GST_DEBUG
#define GST_CAT_DEFAULT ensure_debug_category()
static GstDebugCategory *
ensure_debug_category (void)
{
  static gsize cat_gonce = 0;

  if (g_once_init_enter (&cat_gonce)) {
    gsize cat_done;

    cat_done = (gsize) _gst_debug_category_new ("video-converter", 0,
        "video-converter object");

    g_once_init_leave (&cat_gonce, cat_done);
  }

  return (GstDebugCategory *) cat_gonce;
}
#else
#define ensure_debug_category() /* NOOP */
#endif /* GST_DISABLE_GST_DEBUG */

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 193 194 195 196 197 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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
typedef void (*GstParallelizedTaskFunc) (gpointer user_data);

typedef struct _GstParallelizedTaskRunner GstParallelizedTaskRunner;
typedef struct _GstParallelizedTaskThread GstParallelizedTaskThread;

struct _GstParallelizedTaskThread
{
  GstParallelizedTaskRunner *runner;
  guint idx;
  GThread *thread;
};

struct _GstParallelizedTaskRunner
{
  guint n_threads;

  GstParallelizedTaskThread *threads;

  GstParallelizedTaskFunc func;
  gpointer *task_data;

  GMutex lock;
  GCond cond_todo, cond_done;
  gint n_todo, n_done;
  gboolean quit;
};

static gpointer
gst_parallelized_task_thread_func (gpointer data)
{
  GstParallelizedTaskThread *self = data;

#if 0
#ifdef HAVE_PTHREAD
  {
    pthread_t thread = pthread_self ();
    cpu_set_t cpuset;
    int r;

    CPU_ZERO (&cpuset);
    CPU_SET (self->idx, &cpuset);
    if ((r = pthread_setaffinity_np (thread, sizeof (cpuset), &cpuset)) != 0)
      GST_ERROR ("Failed to set thread affinity for thread %d: %s", self->idx,
          g_strerror (r));
  }
#endif
#endif

  g_mutex_lock (&self->runner->lock);
  self->runner->n_done++;
  if (self->runner->n_done == self->runner->n_threads - 1)
    g_cond_signal (&self->runner->cond_done);

  do {
    gint idx;

    while (self->runner->n_todo == -1 && !self->runner->quit)
      g_cond_wait (&self->runner->cond_todo, &self->runner->lock);

    if (self->runner->quit)
      break;

    idx = self->runner->n_todo--;
    g_assert (self->runner->n_todo >= -1);
    g_mutex_unlock (&self->runner->lock);

    g_assert (self->runner->func != NULL);

    self->runner->func (self->runner->task_data[idx]);

    g_mutex_lock (&self->runner->lock);
    self->runner->n_done++;
    if (self->runner->n_done == self->runner->n_threads - 1)
      g_cond_signal (&self->runner->cond_done);
  } while (TRUE);

  g_mutex_unlock (&self->runner->lock);

  return NULL;
}

static void
gst_parallelized_task_runner_free (GstParallelizedTaskRunner * self)
{
  guint i;

  g_mutex_lock (&self->lock);
  self->quit = TRUE;
  g_cond_broadcast (&self->cond_todo);
  g_mutex_unlock (&self->lock);

  for (i = 1; i < self->n_threads; i++) {
    if (!self->threads[i].thread)
      continue;

    g_thread_join (self->threads[i].thread);
  }

  g_mutex_clear (&self->lock);
  g_cond_clear (&self->cond_todo);
  g_cond_clear (&self->cond_done);
  g_free (self->threads);
  g_free (self);
}

static GstParallelizedTaskRunner *
gst_parallelized_task_runner_new (guint n_threads)
{
  GstParallelizedTaskRunner *self;
  guint i;
  GError *err = NULL;

  if (n_threads == 0)
    n_threads = g_get_num_processors ();

  self = g_new0 (GstParallelizedTaskRunner, 1);
  self->n_threads = n_threads;
  self->threads = g_new0 (GstParallelizedTaskThread, n_threads);

  self->quit = FALSE;
  self->n_todo = -1;
  self->n_done = 0;
  g_mutex_init (&self->lock);
  g_cond_init (&self->cond_todo);
  g_cond_init (&self->cond_done);

  /* Set when scheduling a job */
  self->func = NULL;
  self->task_data = NULL;

  for (i = 0; i < n_threads; i++) {
    self->threads[i].runner = self;
    self->threads[i].idx = i;

    /* First thread is the one calling run() */
    if (i > 0) {
      self->threads[i].thread =
255
          g_thread_try_new ("videoconvert", gst_parallelized_task_thread_func,
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
          &self->threads[i], &err);
      if (!self->threads[i].thread)
        goto error;
    }
  }

  g_mutex_lock (&self->lock);
  while (self->n_done < self->n_threads - 1)
    g_cond_wait (&self->cond_done, &self->lock);
  self->n_done = 0;
  g_mutex_unlock (&self->lock);

  return self;

error:
  {
    GST_ERROR ("Failed to start thread %u: %s", i, err->message);
    g_clear_error (&err);

    gst_parallelized_task_runner_free (self);
    return NULL;
  }
}

static void
gst_parallelized_task_runner_run (GstParallelizedTaskRunner * self,
    GstParallelizedTaskFunc func, gpointer * task_data)
{
284 285
  guint n_threads = self->n_threads;

286 287 288
  self->func = func;
  self->task_data = task_data;

289 290 291 292 293 294 295
  if (n_threads > 1) {
    g_mutex_lock (&self->lock);
    self->n_todo = self->n_threads - 2;
    self->n_done = 0;
    g_cond_broadcast (&self->cond_todo);
    g_mutex_unlock (&self->lock);
  }
296 297 298

  self->func (self->task_data[self->n_threads - 1]);

299 300 301 302 303 304 305
  if (n_threads > 1) {
    g_mutex_lock (&self->lock);
    while (self->n_done < self->n_threads - 1)
      g_cond_wait (&self->cond_done, &self->lock);
    self->n_done = 0;
    g_mutex_unlock (&self->lock);
  }
306 307 308 309 310

  self->func = NULL;
  self->task_data = NULL;
}

311 312
typedef struct _GstLineCache GstLineCache;

313 314 315
#define SCALE    (8)
#define SCALE_F  ((float) (1 << SCALE))

316 317 318
typedef struct _MatrixData MatrixData;

struct _MatrixData
319
{
320 321 322 323 324 325
  gdouble dm[4][4];
  gint im[4][4];
  gint width;
  guint64 orc_p1;
  guint64 orc_p2;
  guint64 orc_p3;
326
  guint64 orc_p4;
327 328 329 330
  gint64 *t_r;
  gint64 *t_g;
  gint64 *t_b;
  gint64 t_c;
331 332 333 334 335 336 337 338 339 340 341
  void (*matrix_func) (MatrixData * data, gpointer pixels);
};

typedef struct _GammaData GammaData;

struct _GammaData
{
  gpointer gamma_table;
  gint width;
  void (*gamma_func) (GammaData * data, gpointer dest, gpointer src);
};
342

343 344 345 346 347 348 349 350
typedef enum
{
  ALPHA_MODE_NONE = 0,
  ALPHA_MODE_COPY = (1 << 0),
  ALPHA_MODE_SET = (1 << 1),
  ALPHA_MODE_MULT = (1 << 2)
} AlphaMode;

351 352 353 354 355 356 357 358 359 360
typedef struct
{
  guint8 *data;
  guint stride;
  guint n_lines;
  guint idx;
  gpointer user_data;
  GDestroyNotify notify;
} ConverterAlloc;

361 362 363
typedef void (*FastConvertFunc) (GstVideoConverter * convert,
    const GstVideoFrame * src, GstVideoFrame * dest, gint plane);

Wim Taymans's avatar
Wim Taymans committed
364
struct _GstVideoConverter
365
{
366 367
  gint flags;

368 369 370
  GstVideoInfo in_info;
  GstVideoInfo out_info;

371 372
  gint in_x;
  gint in_y;
373 374
  gint in_width;
  gint in_height;
375 376 377 378
  gint in_maxwidth;
  gint in_maxheight;
  gint out_x;
  gint out_y;
379 380
  gint out_width;
  gint out_height;
381 382
  gint out_maxwidth;
  gint out_maxheight;
383 384 385

  gint current_pstride;
  gint current_width;
386
  gint current_height;
387
  GstVideoFormat current_format;
388
  gint current_bits;
389 390 391

  GstStructure *config;

392 393 394
  GstParallelizedTaskRunner *conversion_runner;

  guint16 **tmpline;
395

396
  gboolean fill_border;
397
  gpointer borderline;
398
  guint64 borders[4];
399
  guint32 border_argb;
400 401
  guint32 alpha_value;
  AlphaMode alpha_mode;
402

Wim Taymans's avatar
Wim Taymans committed
403 404
  void (*convert) (GstVideoConverter * convert, const GstVideoFrame * src,
      GstVideoFrame * dest);
405

406
  /* data for unpack */
407
  GstLineCache **unpack_lines;
408
  GstVideoFormat unpack_format;
409 410 411 412 413 414
  guint unpack_bits;
  gboolean unpack_rgb;
  gboolean identity_unpack;
  gint unpack_pstride;

  /* chroma upsample */
415 416 417 418
  GstLineCache **upsample_lines;
  GstVideoChromaResample **upsample;
  GstVideoChromaResample **upsample_p;
  GstVideoChromaResample **upsample_i;
419 420
  guint up_n_lines;
  gint up_offset;
421

422
  /* to R'G'B */
423
  GstLineCache **to_RGB_lines;
424
  MatrixData to_RGB_matrix;
425
  /* gamma decode */
426
  GammaData gamma_dec;
427 428

  /* scaling */
429 430
  GstLineCache **hscale_lines;
  GstVideoScaler **h_scaler;
431
  gint h_scale_format;
432 433 434 435
  GstLineCache **vscale_lines;
  GstVideoScaler **v_scaler;
  GstVideoScaler **v_scaler_p;
  GstVideoScaler **v_scaler_i;
436 437
  gint v_scale_width;
  gint v_scale_format;
438 439

  /* color space conversion */
440
  GstLineCache **convert_lines;
441 442 443
  MatrixData convert_matrix;
  gint in_bits;
  gint out_bits;
444

445
  /* alpha correction */
446
  GstLineCache **alpha_lines;
447 448
  void (*alpha_func) (GstVideoConverter * convert, gpointer pixels, gint width);

449
  /* gamma encode */
450
  GammaData gamma_enc;
451
  /* to Y'CbCr */
452
  GstLineCache **to_YUV_lines;
453
  MatrixData to_YUV_matrix;
454 455

  /* chroma downsample */
456 457 458 459
  GstLineCache **downsample_lines;
  GstVideoChromaResample **downsample;
  GstVideoChromaResample **downsample_p;
  GstVideoChromaResample **downsample_i;
460 461 462
  guint down_n_lines;
  gint down_offset;

Wim Taymans's avatar
Wim Taymans committed
463
  /* dither */
464 465
  GstLineCache **dither_lines;
  GstVideoDither **dither;
Wim Taymans's avatar
Wim Taymans committed
466

467
  /* pack */
468
  GstLineCache **pack_lines;
469
  guint pack_nlines;
470
  GstVideoFormat pack_format;
471 472 473
  guint pack_bits;
  gboolean pack_rgb;
  gboolean identity_pack;
474
  gint pack_pstride;
475 476
  gconstpointer pack_pal;
  gsize pack_palsize;
477

478 479
  const GstVideoFrame *src;
  GstVideoFrame *dest;
480 481

  /* fastpath */
482 483 484 485 486 487 488 489
  GstVideoFormat fformat[4];
  gint fin_x[4];
  gint fin_y[4];
  gint fout_x[4];
  gint fout_y[4];
  gint fout_width[4];
  gint fout_height[4];
  gint fsplane[4];
490
  gint ffill[4];
491 492 493 494 495 496 497 498 499

  struct
  {
    GstVideoScaler **scaler;
  } fh_scaler[4];
  struct
  {
    GstVideoScaler **scaler;
  } fv_scaler[4];
500
  FastConvertFunc fconvert[4];
501 502
};

503
typedef gpointer (*GstLineCacheAllocLineFunc) (GstLineCache * cache, gint idx,
504
    gpointer user_data);
505
typedef gboolean (*GstLineCacheNeedLineFunc) (GstLineCache * cache, gint idx,
506
    gint out_line, gint in_line, gpointer user_data);
507 508 509 510

struct _GstLineCache
{
  gint first;
511
  gint backlog;
512 513
  GPtrArray *lines;

514 515 516 517
  GstLineCache *prev;
  gboolean write_input;
  gboolean pass_alloc;
  gboolean alloc_writable;
518

519
  GstLineCacheNeedLineFunc need_line;
520
  gint need_line_idx;
521 522
  gpointer need_line_data;
  GDestroyNotify need_line_notify;
523

524
  guint n_lines;
525
  guint stride;
526 527 528
  GstLineCacheAllocLineFunc alloc_line;
  gpointer alloc_line_data;
  GDestroyNotify alloc_line_notify;
529 530
};

531
static GstLineCache *
532
gst_line_cache_new (GstLineCache * prev)
533 534 535 536 537
{
  GstLineCache *result;

  result = g_slice_new0 (GstLineCache);
  result->lines = g_ptr_array_new ();
538
  result->prev = prev;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554

  return result;
}

static void
gst_line_cache_clear (GstLineCache * cache)
{
  g_return_if_fail (cache != NULL);

  g_ptr_array_set_size (cache->lines, 0);
  cache->first = 0;
}

static void
gst_line_cache_free (GstLineCache * cache)
{
555 556 557 558
  if (cache->need_line_notify)
    cache->need_line_notify (cache->need_line_data);
  if (cache->alloc_line_notify)
    cache->alloc_line_notify (cache->alloc_line_data);
559 560 561 562 563 564 565
  gst_line_cache_clear (cache);
  g_ptr_array_unref (cache->lines);
  g_slice_free (GstLineCache, cache);
}

static void
gst_line_cache_set_need_line_func (GstLineCache * cache,
566
    GstLineCacheNeedLineFunc need_line, gint idx, gpointer user_data,
567 568 569
    GDestroyNotify notify)
{
  cache->need_line = need_line;
570
  cache->need_line_idx = idx;
571 572 573 574 575
  cache->need_line_data = user_data;
  cache->need_line_notify = notify;
}

static void
576 577
gst_line_cache_set_alloc_line_func (GstLineCache * cache,
    GstLineCacheAllocLineFunc alloc_line, gpointer user_data,
578 579
    GDestroyNotify notify)
{
580 581 582
  cache->alloc_line = alloc_line;
  cache->alloc_line_data = user_data;
  cache->alloc_line_notify = notify;
583 584
}

585
/* keep this much backlog for interlaced video */
586 587
#define BACKLOG 2

588
static gpointer *
589 590
gst_line_cache_get_lines (GstLineCache * cache, gint idx, gint out_line,
    gint in_line, gint n_lines)
591
{
592
  if (cache->first + cache->backlog < in_line) {
593
    gint to_remove =
594
        MIN (in_line - (cache->first + cache->backlog), cache->lines->len);
595
    if (to_remove > 0) {
596
      g_ptr_array_remove_range (cache->lines, 0, to_remove);
597
    }
598
    cache->first += to_remove;
599
  } else if (in_line < cache->first) {
600
    gst_line_cache_clear (cache);
601
    cache->first = in_line;
602 603 604
  }

  while (TRUE) {
605 606
    gint oline;

607 608 609
    if (cache->first <= in_line
        && in_line + n_lines <= cache->first + (gint) cache->lines->len) {
      return cache->lines->pdata + (in_line - cache->first);
610 611 612 613 614
    }

    if (cache->need_line == NULL)
      break;

615 616
    oline = out_line + cache->first + cache->lines->len - in_line;

617
    if (!cache->need_line (cache, idx, oline, cache->first + cache->lines->len,
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
            cache->need_line_data))
      break;
  }
  GST_DEBUG ("no lines");
  return NULL;
}

static void
gst_line_cache_add_line (GstLineCache * cache, gint idx, gpointer line)
{
  if (cache->first + cache->lines->len != idx) {
    gst_line_cache_clear (cache);
    cache->first = idx;
  }
  g_ptr_array_add (cache->lines, line);
}
634

635 636 637 638 639 640 641 642 643 644 645 646 647
static gpointer
gst_line_cache_alloc_line (GstLineCache * cache, gint idx)
{
  gpointer res;

  if (cache->alloc_line)
    res = cache->alloc_line (cache, idx, cache->alloc_line_data);
  else
    res = NULL;

  return res;
}

Wim Taymans's avatar
Wim Taymans committed
648
static void video_converter_generic (GstVideoConverter * convert,
Wim Taymans's avatar
Wim Taymans committed
649
    const GstVideoFrame * src, GstVideoFrame * dest);
Wim Taymans's avatar
Wim Taymans committed
650
static gboolean video_converter_lookup_fastpath (GstVideoConverter * convert);
651
static void video_converter_compute_matrix (GstVideoConverter * convert);
652 653
static void video_converter_compute_resample (GstVideoConverter * convert,
    gint idx);
654

655 656
static gpointer get_dest_line (GstLineCache * cache, gint idx,
    gpointer user_data);
657

658
static gboolean do_unpack_lines (GstLineCache * cache, gint idx, gint out_line,
659
    gint in_line, gpointer user_data);
660 661 662 663 664
static gboolean do_downsample_lines (GstLineCache * cache, gint idx,
    gint out_line, gint in_line, gpointer user_data);
static gboolean do_convert_to_RGB_lines (GstLineCache * cache, gint idx,
    gint out_line, gint in_line, gpointer user_data);
static gboolean do_convert_lines (GstLineCache * cache, gint idx, gint out_line,
665
    gint in_line, gpointer user_data);
666
static gboolean do_alpha_lines (GstLineCache * cache, gint idx, gint out_line,
667
    gint in_line, gpointer user_data);
668 669 670 671 672
static gboolean do_convert_to_YUV_lines (GstLineCache * cache, gint idx,
    gint out_line, gint in_line, gpointer user_data);
static gboolean do_upsample_lines (GstLineCache * cache, gint idx,
    gint out_line, gint in_line, gpointer user_data);
static gboolean do_vscale_lines (GstLineCache * cache, gint idx, gint out_line,
673
    gint in_line, gpointer user_data);
674
static gboolean do_hscale_lines (GstLineCache * cache, gint idx, gint out_line,
675
    gint in_line, gpointer user_data);
676
static gboolean do_dither_lines (GstLineCache * cache, gint idx, gint out_line,
Wim Taymans's avatar
Wim Taymans committed
677
    gint in_line, gpointer user_data);
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
static ConverterAlloc *
converter_alloc_new (guint stride, guint n_lines, gpointer user_data,
    GDestroyNotify notify)
{
  ConverterAlloc *alloc;

  GST_DEBUG ("stride %d, n_lines %d", stride, n_lines);
  alloc = g_slice_new0 (ConverterAlloc);
  alloc->data = g_malloc (stride * n_lines);
  alloc->stride = stride;
  alloc->n_lines = n_lines;
  alloc->idx = 0;
  alloc->user_data = user_data;
  alloc->notify = notify;

  return alloc;
}

static void
converter_alloc_free (ConverterAlloc * alloc)
{
  if (alloc->notify)
    alloc->notify (alloc->user_data);
  g_free (alloc->data);
  g_slice_free (ConverterAlloc, alloc);
}

706
static void
707
setup_border_alloc (GstVideoConverter * convert, ConverterAlloc * alloc)
708 709 710
{
  gint i;

711 712 713 714
  if (convert->borderline) {
    for (i = 0; i < alloc->n_lines; i++)
      memcpy (&alloc->data[i * alloc->stride], convert->borderline,
          alloc->stride);
715
  }
716 717 718
}

static gpointer
719
get_temp_line (GstLineCache * cache, gint idx, gpointer user_data)
720
{
721
  ConverterAlloc *alloc = user_data;
722 723
  gpointer tmpline;

724 725 726
  GST_DEBUG ("get temp line %d (%p %d)", idx, alloc, alloc->idx);
  tmpline = &alloc->data[alloc->stride * alloc->idx];
  alloc->idx = (alloc->idx + 1) % alloc->n_lines;
727 728 729 730

  return tmpline;
}

731 732 733
static gpointer
get_border_temp_line (GstLineCache * cache, gint idx, gpointer user_data)
{
734 735
  ConverterAlloc *alloc = user_data;
  GstVideoConverter *convert = alloc->user_data;
736 737
  gpointer tmpline;

738 739
  GST_DEBUG ("get temp line %d (%p %d)", idx, alloc, alloc->idx);
  tmpline = &alloc->data[alloc->stride * alloc->idx] +
740
      (convert->out_x * convert->pack_pstride);
741
  alloc->idx = (alloc->idx + 1) % alloc->n_lines;
742 743 744 745

  return tmpline;
}

746 747
static gint
get_opt_int (GstVideoConverter * convert, const gchar * opt, gint def)
748
{
749 750 751 752 753
  gint res;
  if (!gst_structure_get_int (convert->config, opt, &res))
    res = def;
  return res;
}
754

755 756 757 758 759 760 761 762
static guint
get_opt_uint (GstVideoConverter * convert, const gchar * opt, guint def)
{
  guint res;
  if (!gst_structure_get_uint (convert->config, opt, &res))
    res = def;
  return res;
}
763

764 765 766 767 768 769 770 771 772
static gdouble
get_opt_double (GstVideoConverter * convert, const gchar * opt, gdouble def)
{
  gdouble res;
  if (!gst_structure_get_double (convert->config, opt, &res))
    res = def;
  return res;
}

773 774 775 776 777 778 779
static gboolean
get_opt_bool (GstVideoConverter * convert, const gchar * opt, gboolean def)
{
  gboolean res;
  if (!gst_structure_get_boolean (convert->config, opt, &res))
    res = def;
  return res;
780 781
}

782 783 784 785 786 787 788 789 790
static gint
get_opt_enum (GstVideoConverter * convert, const gchar * opt, GType type,
    gint def)
{
  gint res;
  if (!gst_structure_get_enum (convert->config, opt, type, &res))
    res = def;
  return res;
}
791

792
#define DEFAULT_OPT_FILL_BORDER TRUE
793
#define DEFAULT_OPT_ALPHA_VALUE 1.0
794
/* options copy, set, mult */
795
#define DEFAULT_OPT_ALPHA_MODE GST_VIDEO_ALPHA_MODE_COPY
796
#define DEFAULT_OPT_BORDER_ARGB 0xff000000
797
/* options full, input-only, output-only, none */
798
#define DEFAULT_OPT_MATRIX_MODE GST_VIDEO_MATRIX_MODE_FULL
799
/* none, remap */
800
#define DEFAULT_OPT_GAMMA_MODE GST_VIDEO_GAMMA_MODE_NONE
801
/* none, merge-only, fast */
802
#define DEFAULT_OPT_PRIMARIES_MODE GST_VIDEO_PRIMARIES_MODE_NONE
803
/* options full, upsample-only, downsample-only, none */
804
#define DEFAULT_OPT_CHROMA_MODE GST_VIDEO_CHROMA_MODE_FULL
805
#define DEFAULT_OPT_RESAMPLER_METHOD GST_VIDEO_RESAMPLER_METHOD_CUBIC
806
#define DEFAULT_OPT_CHROMA_RESAMPLER_METHOD GST_VIDEO_RESAMPLER_METHOD_LINEAR
807
#define DEFAULT_OPT_RESAMPLER_TAPS 0
Wim Taymans's avatar
Wim Taymans committed
808 809
#define DEFAULT_OPT_DITHER_METHOD GST_VIDEO_DITHER_BAYER
#define DEFAULT_OPT_DITHER_QUANTIZATION 1
810 811 812

#define GET_OPT_FILL_BORDER(c) get_opt_bool(c, \
    GST_VIDEO_CONVERTER_OPT_FILL_BORDER, DEFAULT_OPT_FILL_BORDER)
813 814
#define GET_OPT_ALPHA_VALUE(c) get_opt_double(c, \
    GST_VIDEO_CONVERTER_OPT_ALPHA_VALUE, DEFAULT_OPT_ALPHA_VALUE)
815 816
#define GET_OPT_ALPHA_MODE(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_ALPHA_MODE, GST_TYPE_VIDEO_ALPHA_MODE, DEFAULT_OPT_ALPHA_MODE)
817 818
#define GET_OPT_BORDER_ARGB(c) get_opt_uint(c, \
    GST_VIDEO_CONVERTER_OPT_BORDER_ARGB, DEFAULT_OPT_BORDER_ARGB)
819 820 821 822 823 824 825 826
#define GET_OPT_MATRIX_MODE(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_MATRIX_MODE, GST_TYPE_VIDEO_MATRIX_MODE, DEFAULT_OPT_MATRIX_MODE)
#define GET_OPT_GAMMA_MODE(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_GAMMA_MODE, GST_TYPE_VIDEO_GAMMA_MODE, DEFAULT_OPT_GAMMA_MODE)
#define GET_OPT_PRIMARIES_MODE(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_PRIMARIES_MODE, GST_TYPE_VIDEO_PRIMARIES_MODE, DEFAULT_OPT_PRIMARIES_MODE)
#define GET_OPT_CHROMA_MODE(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_CHROMA_MODE, GST_TYPE_VIDEO_CHROMA_MODE, DEFAULT_OPT_CHROMA_MODE)
827 828 829
#define GET_OPT_RESAMPLER_METHOD(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_RESAMPLER_METHOD, GST_TYPE_VIDEO_RESAMPLER_METHOD, \
    DEFAULT_OPT_RESAMPLER_METHOD)
830 831 832
#define GET_OPT_CHROMA_RESAMPLER_METHOD(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_CHROMA_RESAMPLER_METHOD, GST_TYPE_VIDEO_RESAMPLER_METHOD, \
    DEFAULT_OPT_CHROMA_RESAMPLER_METHOD)
833 834
#define GET_OPT_RESAMPLER_TAPS(c) get_opt_uint(c, \
    GST_VIDEO_CONVERTER_OPT_RESAMPLER_TAPS, DEFAULT_OPT_RESAMPLER_TAPS)
Wim Taymans's avatar
Wim Taymans committed
835 836 837 838 839
#define GET_OPT_DITHER_METHOD(c) get_opt_enum(c, \
    GST_VIDEO_CONVERTER_OPT_DITHER_METHOD, GST_TYPE_VIDEO_DITHER_METHOD, \
    DEFAULT_OPT_DITHER_METHOD)
#define GET_OPT_DITHER_QUANTIZATION(c) get_opt_uint(c, \
    GST_VIDEO_CONVERTER_OPT_DITHER_QUANTIZATION, DEFAULT_OPT_DITHER_QUANTIZATION)
840

841 842 843
#define CHECK_ALPHA_COPY(c) (GET_OPT_ALPHA_MODE(c) == GST_VIDEO_ALPHA_MODE_COPY)
#define CHECK_ALPHA_SET(c) (GET_OPT_ALPHA_MODE(c) == GST_VIDEO_ALPHA_MODE_SET)
#define CHECK_ALPHA_MULT(c) (GET_OPT_ALPHA_MODE(c) == GST_VIDEO_ALPHA_MODE_MULT)
844

845 846 847 848
#define CHECK_MATRIX_FULL(c) (GET_OPT_MATRIX_MODE(c) == GST_VIDEO_MATRIX_MODE_FULL)
#define CHECK_MATRIX_INPUT(c) (GET_OPT_MATRIX_MODE(c) == GST_VIDEO_MATRIX_MODE_INPUT_ONLY)
#define CHECK_MATRIX_OUTPUT(c) (GET_OPT_MATRIX_MODE(c) == GST_VIDEO_MATRIX_MODE_OUTPUT_ONLY)
#define CHECK_MATRIX_NONE(c) (GET_OPT_MATRIX_MODE(c) == GST_VIDEO_MATRIX_MODE_NONE)
849

850 851
#define CHECK_GAMMA_NONE(c) (GET_OPT_GAMMA_MODE(c) == GST_VIDEO_GAMMA_MODE_NONE)
#define CHECK_GAMMA_REMAP(c) (GET_OPT_GAMMA_MODE(c) == GST_VIDEO_GAMMA_MODE_REMAP)
852

853 854 855
#define CHECK_PRIMARIES_NONE(c) (GET_OPT_PRIMARIES_MODE(c) == GST_VIDEO_PRIMARIES_MODE_NONE)
#define CHECK_PRIMARIES_MERGE(c) (GET_OPT_PRIMARIES_MODE(c) == GST_VIDEO_PRIMARIES_MODE_MERGE_ONLY)
#define CHECK_PRIMARIES_FAST(c) (GET_OPT_PRIMARIES_MODE(c) == GST_VIDEO_PRIMARIES_MODE_FAST)
856

857 858 859 860
#define CHECK_CHROMA_FULL(c) (GET_OPT_CHROMA_MODE(c) == GST_VIDEO_CHROMA_MODE_FULL)
#define CHECK_CHROMA_UPSAMPLE(c) (GET_OPT_CHROMA_MODE(c) == GST_VIDEO_CHROMA_MODE_UPSAMPLE_ONLY)
#define CHECK_CHROMA_DOWNSAMPLE(c) (GET_OPT_CHROMA_MODE(c) == GST_VIDEO_CHROMA_MODE_DOWNSAMPLE_ONLY)
#define CHECK_CHROMA_NONE(c) (GET_OPT_CHROMA_MODE(c) == GST_VIDEO_CHROMA_MODE_NONE)
861

862
static GstLineCache *
863
chain_unpack_line (GstVideoConverter * convert, gint idx)
864
{
865
  GstLineCache *prev;
866
  GstVideoInfo *info;
867

868 869 870 871 872
  info = &convert->in_info;

  convert->current_format = convert->unpack_format;
  convert->current_bits = convert->unpack_bits;
  convert->current_pstride = convert->current_bits >> 1;
873

874 875
  convert->unpack_pstride = convert->current_pstride;
  convert->identity_unpack = (convert->current_format == info->finfo->format);
876

Wim Taymans's avatar
Wim Taymans committed
877 878 879 880
  GST_DEBUG ("chain unpack line format %s, pstride %d, identity_unpack %d",
      gst_video_format_to_string (convert->current_format),
      convert->current_pstride, convert->identity_unpack);

881
  prev = convert->unpack_lines[idx] = gst_line_cache_new (NULL);
882 883
  prev->write_input = FALSE;
  prev->pass_alloc = FALSE;
884 885
  prev->n_lines = 1;
  prev->stride = convert->current_pstride * convert->current_width;
886
  gst_line_cache_set_need_line_func (prev, do_unpack_lines, idx, convert, NULL);
887

888
  return prev;
889 890
}

891
static GstLineCache *
892
chain_upsample (GstVideoConverter * convert, GstLineCache * prev, gint idx)
893
{
894
  video_converter_compute_resample (convert, idx);
895

896
  if (convert->upsample_p[idx] || convert->upsample_i[idx]) {
Wim Taymans's avatar
Wim Taymans committed
897
    GST_DEBUG ("chain upsample");
898
    prev = convert->upsample_lines[idx] = gst_line_cache_new (prev);
899 900
    prev->write_input = TRUE;
    prev->pass_alloc = TRUE;
901 902
    prev->n_lines = 4;
    prev->stride = convert->current_pstride * convert->current_width;
903 904
    gst_line_cache_set_need_line_func (prev,
        do_upsample_lines, idx, convert, NULL);
905
  }
906
  return prev;
907 908
}

909
static void
910
color_matrix_set_identity (MatrixData * m)
911
{
912 913 914 915
  int i, j;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
916
      m->dm[i][j] = (i == j);
917
    }
918
  }
919 920
}

921 922 923 924 925 926 927 928 929 930
static void
color_matrix_copy (MatrixData * d, const MatrixData * s)
{
  gint i, j;

  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
      d->dm[i][j] = s->dm[i][j];
}

931 932 933 934 935
/* Perform 4x4 matrix multiplication:
 *  - @dst@ = @a@ * @b@
 *  - @dst@ may be a pointer to @a@ andor @b@
 */
static void
936
color_matrix_multiply (MatrixData * dst, MatrixData * a, MatrixData * b)
937
{
938
  MatrixData tmp;
939 940 941 942 943 944
  int i, j, k;

  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      double x = 0;
      for (k = 0; k < 4; k++) {
945
        x += a->dm[i][k] * b->dm[k][j];
946
      }
947
      tmp.dm[i][j] = x;
948 949
    }
  }
950 951
  color_matrix_copy (dst, &tmp);
}
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
static void
color_matrix_invert (MatrixData * d, MatrixData * s)
{
  MatrixData tmp;
  int i, j;
  double det;

  color_matrix_set_identity (&tmp);
  for (j = 0; j < 3; j++) {
    for (i = 0; i < 3; i++) {
      tmp.dm[j][i] =
          s->dm[(i + 1) % 3][(j + 1) % 3] * s->dm[(i + 2) % 3][(j + 2) % 3] -
          s->dm[(i + 1) % 3][(j + 2) % 3] * s->dm[(i + 2) % 3][(j + 1) % 3];
    }
  }
  det =
      tmp.dm[0][0] * s->dm[0][0] + tmp.dm[0][1] * s->dm[1][0] +
      tmp.dm[0][2] * s->dm[2][0];
  for (j = 0; j < 3; j++) {
    for (i = 0; i < 3; i++) {
      tmp.dm[i][j] /= det;
    }
  }
  color_matrix_copy (d, &tmp);
977 978 979
}

static void
980
color_matrix_offset_components (MatrixData * m, double a1, double a2, double a3)
981
{
982
  MatrixData a;
983 984

  color_matrix_set_identity (&a);
985 986 987
  a.dm[0][3] = a1;
  a.dm[1][3] = a2;
  a.dm[2][3] = a3;
988 989 990 991
  color_matrix_multiply (m, &a, m);
}

static void
992
color_matrix_scale_components (MatrixData * m, double a1, double a2, double a3)
993
{
994
  MatrixData a;
995 996

  color_matrix_set_identity (&a);
997 998 999
  a.dm[0][0] = a1;
  a.dm[1][1] = a2;
  a.dm[2][2] = a3;
1000 1001 1002 1003
  color_matrix_multiply (m, &a, m);
}

static void
1004
color_matrix_debug (const MatrixData * s)
1005
{
1006 1007 1008 1009 1010 1011 1012 1013
  GST_DEBUG ("[%f %f %f %f]", s->dm[0][0], s->dm[0][1], s->dm[0][2],
      s->dm[0][3]);
  GST_DEBUG ("[%f %f %f %f]", s->dm[1][0], s->dm[1][1], s->dm[1][2],
      s->dm[1][3]);
  GST_DEBUG ("[%f %f %f %f]", s->dm[2][0], s->dm[2][1], s->dm[2][2],
      s->dm[2][3]);
  GST_DEBUG ("[%f %f %f %f]", s->dm[3][0], s->dm[3][1], s->dm[3][2],
      s->dm[3][3]);
1014 1015 1016
}

static void
1017
color_matrix_convert (MatrixData * s)
1018 1019 1020 1021 1022
{
  gint i, j;

  for (i = 0; i < 4; i++)
    for (j = 0; j < 4; j++)
1023
      s->im[i][j] = rint (s->dm[i][j]);
1024

1025 1026 1027 1028 1029 1030 1031 1032
  GST_DEBUG ("[%6d %6d %6d %6d]", s->im[0][0], s->im[0][1], s->im[0][2],
      s->im[0][3]);
  GST_DEBUG ("[%6d %6d %6d %6d]", s->im[1][0], s->im[1][1], s->im[1][2],
      s->im[1][3]);
  GST_DEBUG ("[%6d %6d %6d %6d]", s->im[2][0], s->im[2][1], s->im[2][2],
      s->im[2][3]);
  GST_DEBUG ("[%6d %6d %6d %6d]", s->im[3][0], s->im[3][1], s->im[3][2],
      s->im[3][3]);
1033 1034 1035
}

static void
1036
color_matrix_YCbCr_to_RGB (MatrixData * m, double Kr, double Kb)
1037 1038
{
  double Kg = 1.0 - Kr - Kb;
1039
  MatrixData k = {
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    {
          {1., 0., 2 * (1 - Kr), 0.},
          {1., -2 * Kb * (1 - Kb) / Kg, -2 * Kr * (1 - Kr) / Kg, 0.},
          {1., 2 * (1 - Kb), 0., 0.},
          {0., 0., 0., 1.},
        }
  };

  color_matrix_multiply (m, &k, m);
}

static void
1052
color_matrix_RGB_to_YCbCr (MatrixData * m, double Kr, double Kb)
1053 1054
{
  double Kg = 1.0 - Kr - Kb;
1055
  MatrixData k;
1056 1057
  double x;

1058 1059 1060 1061
  k.dm[0][0] = Kr;
  k.dm[0][1] = Kg;
  k.dm[0][2] = Kb;
  k.dm[0][3] = 0;
1062 1063

  x = 1 / (2 * (1 - Kb));
1064 1065 1066 1067
  k.dm[1][0] = -x * Kr;
  k.dm[1][1] = -x * Kg;
  k.dm[1][2] = x * (1 - Kb);
  k.dm[1][3] = 0;
1068 1069

  x = 1 / (2 * (1 - Kr));
1070 1071 1072 1073
  k.dm[2][0] = x * (1 - Kr);
  k.dm[2][1] = -x * Kg;
  k.dm[2][2] = -x * Kb;
  k.dm[2][3] = 0;
1074

1075 1076 1077 1078
  k.dm[3][0] = 0;
  k.dm[3][1] = 0;
  k.dm[3][2] = 0;
  k.dm[3][3] = 1;
1079 1080 1081 1082 1083

  color_matrix_multiply (m, &k, m);
}

static void
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
color_matrix_RGB_to_XYZ (MatrixData * dst, double Rx, double Ry, double Gx,
    double Gy, double Bx, double By, double Wx, double Wy)
{
  MatrixData m, im;
  double sx, sy, sz;
  double wx, wy, wz;

  color_matrix_set_identity (&m);

  m.dm[0][0] = Rx;
  m.dm[1][0] = Ry;
  m.dm[2][0] = (1.0 - Rx - Ry);
  m.dm[0][1] = Gx;
  m.dm[1][1] = Gy;
  m.dm[2][1] = (1.0 - Gx - Gy);
  m.dm[0][2] = Bx;
  m.dm[1][2] = By;
  m.dm[2][2] = (1.0 - Bx - By);

  color_matrix_invert (&im, &m);

  wx = Wx / Wy;
  wy = 1.0;
  wz = (1.0 - Wx - Wy) / Wy;

  sx = im.dm[0][0] * wx + im.dm[0][1] * wy + im.dm[0][2] * wz;
  sy = im.dm[1][0] * wx + im.dm[1][1] * wy + im.dm[1][2] * wz;
  sz = im.dm[2][0] * wx + im.dm[2][1] * wy + im.dm[2][2] * wz;

  m.dm[0][0] *= sx;
  m.dm[1][0] *= sx;
  m.dm[2][0] *= sx;
  m.dm[0][1] *= sy;
  m.dm[1][1] *= sy;
  m.dm[2][1] *= sy;
  m.dm[0][2] *= sz;
  m.dm[1][2] *= sz;
  m.dm[2][2] *= sz;

  color_matrix_copy (dst, &m);
}

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
static void
videoconvert_convert_init_tables (MatrixData * data)
{
  gint i, j;

  data->t_r = g_new (gint64, 256);
  data->t_g = g_new (gint64, 256);
  data->t_b = g_new (gint64, 256);

  for (i = 0; i < 256; i++) {
    gint64 r = 0, g = 0, b = 0;

    for (j = 0; j < 3; j++) {
      r = (r << 16) + data->im[j][0] * i;
      g = (g << 16) + data->im[j][1] * i;
      b = (b << 16) + data->im[j][2] * i;
    }
    data->t_r[i] = r;
    data->t_g[i] = g;
    data->t_b[i] = b;
  }
  data->t_c = ((gint64) data->im[0][3] << 32)
      + ((gint64) data->im[1][3] << 16)
      + ((gint64) data->im[2][3] << 0);
}

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
void
_custom_video_orc_matrix8 (guint8 * ORC_RESTRICT d1,
    const guint8 * ORC_RESTRICT s1, orc_int64 p1, orc_int64 p2, orc_int64 p3,
    orc_int64 p4, int n)
{
  gint i;
  gint r, g, b;
  gint y, u, v;
  gint a00, a01, a02, a03;
  gint a10, a11, a12, a13;
  gint a20, a21, a22, a23;

  a00 = (gint16) (p1 >> 16);
  a01 = (gint16) (p2 >> 16);
  a02 = (gint16) (p3 >> 16);
  a03 = (gint16) (p4 >> 16);
  a10 = (gint16) (p1 >> 32);
  a11 = (gint16) (p2 >> 32);
  a12 = (gint16) (p3 >> 32);
  a13 = (gint16) (p4 >> 32);
  a20 = (gint16) (p1 >> 48);
  a21 = (gint16) (p2 >> 48);
  a22 = (gint16) (p3 >> 48);
  a23 = (gint16) (p4 >> 48);

  for (i = 0; i < n; i++) {
    r = s1[i * 4 + 1];
    g = s1[i * 4 + 2];
    b = s1[i * 4 + 3];

    y = ((a00 * r + a01 * g + a02 * b) >> SCALE) + a03;
    u = ((a10 * r + a11 * g + a12 * b) >> SCALE) + a13;
    v = ((a20 * r + a21 * g + a22 * b) >> SCALE) + a23;

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

1192 1193 1194 1195
static void
video_converter_matrix8 (MatrixData * data, gpointer pixels)
{
  video_orc_matrix8 (pixels, pixels, data->orc_p1, data->orc_p2,
1196
      data->orc_p3, data->orc_p4, data->width);
1197 1198
}

1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
static void
video_converter_matrix8_table (MatrixData * data, gpointer pixels)
{
  gint i, width = data->width * 4;
  guint8 r, g, b;
  gint64 c = data->t_c;
  guint8 *p = pixels;
  gint64 x;

  for (i = 0; i < width; i += 4) {
    r = p[i + 1];
    g = p[i + 2];
    b = p[i + 3];

    x = data->t_r[r] + data->t_g[g] + data->t_b[b] + c;

    p[i + 1] = x >> (32 + SCALE);
    p[i + 2] = x >> (16 + SCALE);
    p[i + 3] = x >> (0 + SCALE);
  }
}

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
static void
video_converter_matrix8_AYUV_ARGB (MatrixData * data, gpointer pixels)
{
  video_orc_convert_AYUV_ARGB (pixels, 0, pixels, 0,
      data->im[0][0], data->im[0][2],
      data->im[2][1], data->im[1][1], data->im[1][2], data->width, 1);
}

static gboolean
is_ayuv_to_rgb_matrix (MatrixData * data)
{
  if (data->im[0][0] != data->im[1][0] || data->im[1][0] != data->im[2][0])
    return FALSE;

  if (data->im[0][1] != 0 || data->im[2][2] != 0)
    return FALSE;

  return TRUE;
}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
static gboolean
is_identity_matrix (MatrixData * data)
{
  gint i, j;
  gint c = data->im[0][0];

  /* not really checking identity because of rounding errors but given
   * the conversions we do we just check for anything that looks like:
   *
   *  c 0 0 0
   *  0 c 0 0
   *  0 0 c 0
   *  0 0 0 1
   */
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      if (i == j) {
        if (i == 3 && data->im[i][j] != 1)
          return FALSE;
        else if (data->im[i][j] != c)
          return FALSE;
      } else if (data->im[i][j] != 0)
        return FALSE;
    }
  }
  return TRUE;
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
static gboolean
is_no_clip_matrix (MatrixData * data)
{
  gint i;
  static const guint8 test[8][3] = {
    {0, 0, 0},
    {0, 0, 255},
    {0, 255, 0},
    {0, 255, 255},
    {255, 0, 0},
    {255, 0, 255},
    {255, 255, 0},
    {255, 255, 255}
  };

  for (i = 0; i < 8; i++) {
    gint r, g, b;
    gint y, u, v;

    r = test[i][0];
    g = test[i][1];
    b = test[i][2];

    y = (data->im[0][0] * r + data->im[0][1] * g +
        data->im[0][2] * b + data->im[0][3]) >> SCALE;
    u = (data->im[1][0] * r + data->im[1][1] * g +
        data->im[1][2] * b + data->im[1][3]) >> SCALE;
    v = (data->im[2][0] * r + data->im[2][1] * g +
        data->im[2][2] * b + data->im[2][3]) >> SCALE;

    if (y != CLAMP (y, 0, 255) || u != CLAMP (u, 0, 255)
        || v != CLAMP (v, 0, 255))
      return FALSE;
  }
  return TRUE;
}

1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
static void
video_converter_matrix16 (MatrixData * data, gpointer pixels)
{
  int i;
  int r, g, b;
  int y, u, v;
  guint16 *p = pixels;
  gint width = data->width;

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

    y = (data->im[0][0] * r + data->im[0][1] * g +
        data->im[0][2] * b + data->im[0][3]) >> SCALE;
    u = (data->im[1][0] * r + data->im[1][1] * g +
        data->im[1][2] * b + data->im[1][3]) >> SCALE;
    v = (data->im[2][0] * r + data->im[2][1] * g +
        data->im[2][2] * b + data->im[2][3]) >> SCALE;

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


static void
prepare_matrix (GstVideoConverter * convert, MatrixData * data)
{
1337 1338 1339
  if (is_identity_matrix (data))
    return;

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
  color_matrix_scale_components (data, SCALE_F, SCALE_F, SCALE_F);
  color_matrix_convert (data);

  data->width = convert->current_width;

  if (convert->current_bits == 8) {
    if (!convert->unpack_rgb && convert->pack_rgb
        && is_ayuv_to_rgb_matrix (data)) {
      GST_DEBUG ("use fast AYUV -> RGB matrix");
      data->matrix_func = video_converter_matrix8_AYUV_ARGB;
1350 1351 1352 1353
    } else if (is_no_clip_matrix (data)) {
      GST_DEBUG ("use 8bit table");
      data->matrix_func = video_converter_matrix8_table;
      videoconvert_convert_init_tables (data);
1354
    } else {
1355 1356
      gint a03, a13, a23;

1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
      GST_DEBUG ("use 8bit matrix");
      data->matrix_func = video_converter_matrix8;

      data->orc_p1 = (((guint64) (guint16) data->im[2][0]) << 48) |
          (((guint64) (guint16) data->im[1][0]) << 32) |
          (((guint64) (guint16) data->im[0][0]) << 16);
      data->orc_p2 = (((guint64) (guint16) data->im[2][1]) << 48) |
          (((guint64) (guint16) data->im[1][1]) << 32) |
          (((guint64) (guint16) data->im[0][1]) << 16);
      data->orc_p3 = (((guint64) (guint16) data->