summaryrefslogtreecommitdiffstats
path: root/gfx/ycbcr/ycbcr_to_rgb565.cpp
blob: fe28245a9c0aae058842094ec256f1dd04075424 (plain)
1
2
3
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <stdlib.h>
#include <limits.h>
#include "nsDebug.h"
#include "ycbcr_to_rgb565.h"
#include "nsAlgorithm.h"



#ifdef HAVE_YCBCR_TO_RGB565

namespace mozilla {

namespace gfx {

/*This contains all of the parameters that are needed to convert a row.
  Passing them in a struct instead of as individual parameters saves the need
   to continually push onto the stack the ones that are fixed for every row.*/
struct yuv2rgb565_row_scale_bilinear_ctx{
  uint16_t *rgb_row;
  const uint8_t *y_row;
  const uint8_t *u_row;
  const uint8_t *v_row;
  int y_yweight;
  int y_pitch;
  int width;
  int source_x0_q16;
  int source_dx_q16;
  /*Not used for 4:4:4, except with chroma-nearest.*/
  int source_uv_xoffs_q16;
  /*Not used for 4:4:4 or chroma-nearest.*/
  int uv_pitch;
  /*Not used for 4:2:2, 4:4:4, or chroma-nearest.*/
  int uv_yweight;
};



/*This contains all of the parameters that are needed to convert a row.
  Passing them in a struct instead of as individual parameters saves the need
   to continually push onto the stack the ones that are fixed for every row.*/
struct yuv2rgb565_row_scale_nearest_ctx{
  uint16_t *rgb_row;
  const uint8_t *y_row;
  const uint8_t *u_row;
  const uint8_t *v_row;
  int width;
  int source_x0_q16;
  int source_dx_q16;
  /*Not used for 4:4:4.*/
  int source_uv_xoffs_q16;
};



typedef void (*yuv2rgb565_row_scale_bilinear_func)(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither);

typedef void (*yuv2rgb565_row_scale_nearest_func)(
 const yuv2rgb565_row_scale_nearest_ctx *ctx, int dither);



//TODO: fix NEON asm for iOS
# if defined(MOZILLA_MAY_SUPPORT_NEON) && !defined(__APPLE__)

extern "C" void ScaleYCbCr42xToRGB565_BilinearY_Row_NEON(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither);

void __attribute((noinline)) yuv42x_to_rgb565_row_neon(uint16_t *dst,
                                                       const uint8_t *y,
                                                       const uint8_t *u,
                                                       const uint8_t *v,
                                                       int n,
                                                       int oddflag);

#endif



/*Bilinear interpolation of a single value.
  This uses the exact same formulas as the asm, even though it adds some extra
   shifts that do nothing but reduce accuracy.*/
static int bislerp(const uint8_t *row,
                   int pitch,
                   int source_x,
                   int xweight,
                   int yweight) {
  int a;
  int b;
  int c;
  int d;
  a = row[source_x];
  b = row[source_x+1];
  c = row[source_x+pitch];
  d = row[source_x+pitch+1];
  a = ((a<<8)+(c-a)*yweight+128)>>8;
  b = ((b<<8)+(d-b)*yweight+128)>>8;
  return ((a<<8)+(b-a)*xweight+128)>>8;
}

/*Convert a single pixel from Y'CbCr to RGB565.
  This uses the exact same formulas as the asm, even though we could make the
   constants a lot more accurate with 32-bit wide registers.*/
static uint16_t yu2rgb565(int y, int u, int v, int dither) {
  /*This combines the constant offset that needs to be added during the Y'CbCr
     conversion with a rounding offset that depends on the dither parameter.*/
  static const int DITHER_BIAS[4][3]={
    {-14240,    8704,    -17696},
    {-14240+128,8704+64, -17696+128},
    {-14240+256,8704+128,-17696+256},
    {-14240+384,8704+192,-17696+384}
  };
  int r;
  int g;
  int b;
  r = clamped((74*y+102*v+DITHER_BIAS[dither][0])>>9, 0, 31);
  g = clamped((74*y-25*u-52*v+DITHER_BIAS[dither][1])>>8, 0, 63);
  b = clamped((74*y+129*u+DITHER_BIAS[dither][2])>>9, 0, 31);
  return (uint16_t)(r<<11 | g<<5 | b);
}

static void ScaleYCbCr420ToRGB565_Bilinear_Row_C(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither){
  int x;
  int source_x_q16;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    int source_x;
    int xweight;
    int y;
    int u;
    int v;
    xweight = ((source_x_q16&0xFFFF)+128)>>8;
    source_x = source_x_q16>>16;
    y = bislerp(ctx->y_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    xweight = (((source_x_q16+ctx->source_uv_xoffs_q16)&0x1FFFF)+256)>>9;
    source_x = (source_x_q16+ctx->source_uv_xoffs_q16)>>17;
    source_x_q16 += ctx->source_dx_q16;
    u = bislerp(ctx->u_row, ctx->uv_pitch, source_x, xweight, ctx->uv_yweight);
    v = bislerp(ctx->v_row, ctx->uv_pitch, source_x, xweight, ctx->uv_yweight);
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

static void ScaleYCbCr422ToRGB565_Bilinear_Row_C(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither){
  int x;
  int source_x_q16;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    int source_x;
    int xweight;
    int y;
    int u;
    int v;
    xweight = ((source_x_q16&0xFFFF)+128)>>8;
    source_x = source_x_q16>>16;
    y = bislerp(ctx->y_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    xweight = (((source_x_q16+ctx->source_uv_xoffs_q16)&0x1FFFF)+256)>>9;
    source_x = (source_x_q16+ctx->source_uv_xoffs_q16)>>17;
    source_x_q16 += ctx->source_dx_q16;
    u = bislerp(ctx->u_row, ctx->uv_pitch, source_x, xweight, ctx->y_yweight);
    v = bislerp(ctx->v_row, ctx->uv_pitch, source_x, xweight, ctx->y_yweight);
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

static void ScaleYCbCr444ToRGB565_Bilinear_Row_C(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither){
  int x;
  int source_x_q16;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    int source_x;
    int xweight;
    int y;
    int u;
    int v;
    xweight = ((source_x_q16&0xFFFF)+128)>>8;
    source_x = source_x_q16>>16;
    source_x_q16 += ctx->source_dx_q16;
    y = bislerp(ctx->y_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    u = bislerp(ctx->u_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    v = bislerp(ctx->v_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

static void ScaleYCbCr42xToRGB565_BilinearY_Row_C(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither){
  int x;
  int source_x_q16;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    int source_x;
    int xweight;
    int y;
    int u;
    int v;
    xweight = ((source_x_q16&0xFFFF)+128)>>8;
    source_x = source_x_q16>>16;
    y = bislerp(ctx->y_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    source_x = (source_x_q16+ctx->source_uv_xoffs_q16)>>17;
    source_x_q16 += ctx->source_dx_q16;
    u = ctx->u_row[source_x];
    v = ctx->v_row[source_x];
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

static void ScaleYCbCr444ToRGB565_BilinearY_Row_C(
 const yuv2rgb565_row_scale_bilinear_ctx *ctx, int dither){
  int x;
  int source_x_q16;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    int source_x;
    int xweight;
    int y;
    int u;
    int v;
    xweight = ((source_x_q16&0xFFFF)+128)>>8;
    source_x = source_x_q16>>16;
    y = bislerp(ctx->y_row, ctx->y_pitch, source_x, xweight, ctx->y_yweight);
    source_x = (source_x_q16+ctx->source_uv_xoffs_q16)>>16;
    source_x_q16 += ctx->source_dx_q16;
    u = ctx->u_row[source_x];
    v = ctx->v_row[source_x];
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

static void ScaleYCbCr42xToRGB565_Nearest_Row_C(
 const yuv2rgb565_row_scale_nearest_ctx *ctx, int dither){
  int y;
  int u;
  int v;
  int x;
  int source_x_q16;
  int source_x;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    source_x = source_x_q16>>16;
    y = ctx->y_row[source_x];
    source_x = (source_x_q16+ctx->source_uv_xoffs_q16)>>17;
    source_x_q16 += ctx->source_dx_q16;
    u = ctx->u_row[source_x];
    v = ctx->v_row[source_x];
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

static void ScaleYCbCr444ToRGB565_Nearest_Row_C(
 const yuv2rgb565_row_scale_nearest_ctx *ctx, int dither){
  int y;
  int u;
  int v;
  int x;
  int source_x_q16;
  int source_x;
  source_x_q16 = ctx->source_x0_q16;
  for (x = 0; x < ctx->width; x++) {
    source_x = source_x_q16>>16;
    source_x_q16 += ctx->source_dx_q16;
    y = ctx->y_row[source_x];
    u = ctx->u_row[source_x];
    v = ctx->v_row[source_x];
    ctx->rgb_row[x] = yu2rgb565(y, u, v, dither);
    dither ^= 3;
  }
}

void ScaleYCbCrToRGB565(const uint8_t *y_buf,
                                 const uint8_t *u_buf,
                                 const uint8_t *v_buf,
                                 uint8_t *rgb_buf,
                                 int source_x0,
                                 int source_y0,
                                 int source_width,
                                 int source_height,
                                 int width,
                                 int height,
                                 int y_pitch,
                                 int uv_pitch,
                                 int rgb_pitch,
                                 YUVType yuv_type,
                                 ScaleFilter filter) {
  int source_x0_q16;
  int source_y0_q16;
  int source_dx_q16;
  int source_dy_q16;
  int source_uv_xoffs_q16;
  int source_uv_yoffs_q16;
  int x_shift;
  int y_shift;
  int ymin;
  int ymax;
  int uvmin;
  int uvmax;
  int dither;
  /*We don't support negative destination rectangles (just flip the source
     instead), and for empty ones there's nothing to do.*/
  if (width <= 0 || height <= 0)
    return;
  /*These bounds are required to avoid 16.16 fixed-point overflow.*/
  NS_ASSERTION(source_x0 > (INT_MIN>>16) && source_x0 < (INT_MAX>>16),
    "ScaleYCbCrToRGB565 source X offset out of bounds.");
  NS_ASSERTION(source_x0+source_width > (INT_MIN>>16)
            && source_x0+source_width < (INT_MAX>>16),
    "ScaleYCbCrToRGB565 source width out of bounds.");
  NS_ASSERTION(source_y0 > (INT_MIN>>16) && source_y0 < (INT_MAX>>16),
    "ScaleYCbCrToRGB565 source Y offset out of bounds.");
  NS_ASSERTION(source_y0+source_height > (INT_MIN>>16)
            && source_y0+source_height < (INT_MAX>>16),
    "ScaleYCbCrToRGB565 source height out of bounds.");
  /*We require the same stride for Y' and Cb and Cr for 4:4:4 content.*/
  NS_ASSERTION(yuv_type != YV24 || y_pitch == uv_pitch,
    "ScaleYCbCrToRGB565 luma stride differs from chroma for 4:4:4 content.");
  /*We assume we can read outside the bounds of the input, because it makes
     the code much simpler (and in practice is true: both Theora and VP8 return
     padded reference frames).
    In practice, we do not even _have_ the actual bounds of the source, as
     we are passed a crop rectangle from it, and not the dimensions of the full
     image.
    This assertion will not guarantee our out-of-bounds reads are safe, but it
     should at least catch the simple case of passing in an unpadded buffer.*/
  NS_ASSERTION(abs(y_pitch) >= abs(source_width)+16,
    "ScaleYCbCrToRGB565 source image unpadded?");
  /*The NEON code requires the pointers to be aligned to a 16-byte boundary at
     the start of each row.
    This should be true for all of our sources.
    We could try to fix this up if it's not true by adjusting source_x0, but
     that would require the mis-alignment to be the same for the U and V
     planes.*/
  NS_ASSERTION((y_pitch&15) == 0 && (uv_pitch&15) == 0 &&
   ((y_buf-(uint8_t *)nullptr)&15) == 0 &&
   ((u_buf-(uint8_t *)nullptr)&15) == 0 &&
   ((v_buf-(uint8_t *)nullptr)&15) == 0,
   "ScaleYCbCrToRGB565 source image unaligned");
  /*We take an area-based approach to pixel coverage to avoid shifting by small
     amounts (or not so small, when up-scaling or down-scaling by a large
     factor).

    An illustrative example: scaling 4:2:0 up by 2, using JPEG chroma cositing^.

    + = RGB destination locations
    * = Y' source locations
    - = Cb, Cr source locations

    +   +   +   +  +   +   +   +
      *       *      *       *
    +   +   +   +  +   +   +   +
          -              -
    +   +   +   +  +   +   +   +
      *       *      *       *
    +   +   +   +  +   +   +   +

    +   +   +   +  +   +   +   +
      *       *      *       *
    +   +   +   +  +   +   +   +
          -              -
    +   +   +   +  +   +   +   +
      *       *      *       *
    +   +   +   +  +   +   +   +

    So, the coordinates of the upper-left + (first destination site) should
     be (-0.25,-0.25) in the source Y' coordinate system.
    Similarly, the coordinates should be (-0.375,-0.375) in the source Cb, Cr
     coordinate system.
    Note that the origin and scale of these two coordinate systems is not the
     same!

    ^JPEG cositing is required for Theora; VP8 doesn't specify cositing rules,
     but nearly all software converters in existence (at least those that are
     open source, and many that are not) use JPEG cositing instead of MPEG.*/
  source_dx_q16 = (source_width<<16) / width;
  source_x0_q16 = (source_x0<<16)+(source_dx_q16>>1)-0x8000;
  source_dy_q16 = (source_height<<16) / height;
  source_y0_q16 = (source_y0<<16)+(source_dy_q16>>1)-0x8000;
  x_shift = (yuv_type != YV24);
  y_shift = (yuv_type == YV12);
  /*These two variables hold the difference between the origins of the Y' and
     the Cb, Cr coordinate systems, using the scale of the Y' coordinate
     system.*/
  source_uv_xoffs_q16 = -(x_shift<<15);
  source_uv_yoffs_q16 = -(y_shift<<15);
  /*Compute the range of source rows we'll actually use.
    This doesn't guarantee we won't read outside this range.*/
  ymin = source_height >= 0 ? source_y0 : source_y0+source_height-1;
  ymax = source_height >= 0 ? source_y0+source_height-1 : source_y0;
  uvmin = ymin>>y_shift;
  uvmax = ((ymax+1+y_shift)>>y_shift)-1;
  /*Pick a dithering pattern.
    The "&3" at the end is just in case RAND_MAX is lying.*/
  dither = (rand()/(RAND_MAX>>2))&3;
  /*Nearest-neighbor scaling.*/
  if (filter == FILTER_NONE) {
    yuv2rgb565_row_scale_nearest_ctx ctx;
    yuv2rgb565_row_scale_nearest_func scale_row;
    int y;
    /*Add rounding offsets once, in advance.*/
    source_x0_q16 += 0x8000;
    source_y0_q16 += 0x8000;
    source_uv_xoffs_q16 += (x_shift<<15);
    source_uv_yoffs_q16 += (y_shift<<15);
    if (yuv_type == YV12)
      scale_row = ScaleYCbCr42xToRGB565_Nearest_Row_C;
    else
      scale_row = ScaleYCbCr444ToRGB565_Nearest_Row_C;
    ctx.width = width;
    ctx.source_x0_q16 = source_x0_q16;
    ctx.source_dx_q16 = source_dx_q16;
    ctx.source_uv_xoffs_q16 = source_uv_xoffs_q16;
    for (y=0; y<height; y++) {
      int source_y;
      ctx.rgb_row = (uint16_t *)(rgb_buf + y*rgb_pitch);
      source_y = source_y0_q16>>16;
      source_y = clamped(source_y, ymin, ymax);
      ctx.y_row = y_buf + source_y*y_pitch;
      source_y = (source_y0_q16+source_uv_yoffs_q16)>>(16+y_shift);
      source_y = clamped(source_y, uvmin, uvmax);
      source_y0_q16 += source_dy_q16;
      ctx.u_row = u_buf + source_y*uv_pitch;
      ctx.v_row = v_buf + source_y*uv_pitch;
      (*scale_row)(&ctx, dither);
      dither ^= 2;
    }
  }
  /*Bilinear scaling.*/
  else {
    yuv2rgb565_row_scale_bilinear_ctx ctx;
    yuv2rgb565_row_scale_bilinear_func scale_row;
    int uvxscale_min;
    int uvxscale_max;
    int uvyscale_min;
    int uvyscale_max;
    int y;
    /*Check how close the chroma scaling is to unity.
      If it's close enough, we can get away with nearest-neighbor chroma
       sub-sampling, and only doing bilinear on luma.
      If a given axis is subsampled, we use bounds on the luma step of
       [0.67...2], which is equivalent to scaling chroma by [1...3].
      If it's not subsampled, we use bounds of [0.5...1.33], which is
       equivalent to scaling chroma by [0.75...2].
      The lower bound is chosen as a trade-off between speed and how terrible
       nearest neighbor looks when upscaling.*/
# define CHROMA_NEAREST_SUBSAMP_STEP_MIN  0xAAAA
# define CHROMA_NEAREST_NORMAL_STEP_MIN   0x8000
# define CHROMA_NEAREST_SUBSAMP_STEP_MAX 0x20000
# define CHROMA_NEAREST_NORMAL_STEP_MAX  0x15555
    uvxscale_min = yuv_type != YV24 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MIN : CHROMA_NEAREST_NORMAL_STEP_MIN;
    uvxscale_max = yuv_type != YV24 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MAX : CHROMA_NEAREST_NORMAL_STEP_MAX;
    uvyscale_min = yuv_type == YV12 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MIN : CHROMA_NEAREST_NORMAL_STEP_MIN;
    uvyscale_max = yuv_type == YV12 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MAX : CHROMA_NEAREST_NORMAL_STEP_MAX;
    if (uvxscale_min <= abs(source_dx_q16)
     && abs(source_dx_q16) <= uvxscale_max
     && uvyscale_min <= abs(source_dy_q16)
     && abs(source_dy_q16) <= uvyscale_max) {
      /*Add the rounding offsets now.*/
      source_uv_xoffs_q16 += 1<<(15+x_shift);
      source_uv_yoffs_q16 += 1<<(15+y_shift);
      if (yuv_type != YV24) {
        scale_row =
//TODO: fix NEON asm for iOS
#  if defined(MOZILLA_MAY_SUPPORT_NEON) && !defined(__APPLE__)
         supports_neon() ? ScaleYCbCr42xToRGB565_BilinearY_Row_NEON :
#  endif
         ScaleYCbCr42xToRGB565_BilinearY_Row_C;
      }
      else
        scale_row = ScaleYCbCr444ToRGB565_BilinearY_Row_C;
    }
    else {
      if (yuv_type == YV12)
        scale_row = ScaleYCbCr420ToRGB565_Bilinear_Row_C;
      else if (yuv_type == YV16)
        scale_row = ScaleYCbCr422ToRGB565_Bilinear_Row_C;
      else
        scale_row = ScaleYCbCr444ToRGB565_Bilinear_Row_C;
    }
    ctx.width = width;
    ctx.y_pitch = y_pitch;
    ctx.source_x0_q16 = source_x0_q16;
    ctx.source_dx_q16 = source_dx_q16;
    ctx.source_uv_xoffs_q16 = source_uv_xoffs_q16;
    ctx.uv_pitch = uv_pitch;
    for (y=0; y<height; y++) {
      int source_y;
      int yweight;
      int uvweight;
      ctx.rgb_row = (uint16_t *)(rgb_buf + y*rgb_pitch);
      source_y = (source_y0_q16+128)>>16;
      yweight = ((source_y0_q16+128)>>8)&0xFF;
      if (source_y < ymin) {
        source_y = ymin;
        yweight = 0;
      }
      if (source_y > ymax) {
        source_y = ymax;
        yweight = 0;
      }
      ctx.y_row = y_buf + source_y*y_pitch;
      source_y = source_y0_q16+source_uv_yoffs_q16+(128<<y_shift);
      source_y0_q16 += source_dy_q16;
      uvweight = source_y>>(8+y_shift)&0xFF;
      source_y >>= 16+y_shift;
      if (source_y < uvmin) {
        source_y = uvmin;
        uvweight = 0;
      }
      if (source_y > uvmax) {
        source_y = uvmax;
        uvweight = 0;
      }
      ctx.u_row = u_buf + source_y*uv_pitch;
      ctx.v_row = v_buf + source_y*uv_pitch;
      ctx.y_yweight = yweight;
      ctx.uv_yweight = uvweight;
      (*scale_row)(&ctx, dither);
      dither ^= 2;
    }
  }
}

bool IsScaleYCbCrToRGB565Fast(int source_x0,
                                       int source_y0,
                                       int source_width,
                                       int source_height,
                                       int width,
                                       int height,
                                       YUVType yuv_type,
                                       ScaleFilter filter)
{
  // Very fast.
  if (width <= 0 || height <= 0)
    return true;
#  if defined(MOZILLA_MAY_SUPPORT_NEON)
  if (filter != FILTER_NONE) {
    int source_dx_q16;
    int source_dy_q16;
    int uvxscale_min;
    int uvxscale_max;
    int uvyscale_min;
    int uvyscale_max;
    source_dx_q16 = (source_width<<16) / width;
    source_dy_q16 = (source_height<<16) / height;
    uvxscale_min = yuv_type != YV24 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MIN : CHROMA_NEAREST_NORMAL_STEP_MIN;
    uvxscale_max = yuv_type != YV24 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MAX : CHROMA_NEAREST_NORMAL_STEP_MAX;
    uvyscale_min = yuv_type == YV12 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MIN : CHROMA_NEAREST_NORMAL_STEP_MIN;
    uvyscale_max = yuv_type == YV12 ?
     CHROMA_NEAREST_SUBSAMP_STEP_MAX : CHROMA_NEAREST_NORMAL_STEP_MAX;
    if (uvxscale_min <= abs(source_dx_q16)
     && abs(source_dx_q16) <= uvxscale_max
     && uvyscale_min <= abs(source_dy_q16)
     && abs(source_dy_q16) <= uvyscale_max) {
      if (yuv_type != YV24)
        return supports_neon();
    }
  }
#  endif
  return false;
}



void yuv_to_rgb565_row_c(uint16_t *dst,
                         const uint8_t *y,
                         const uint8_t *u,
                         const uint8_t *v,
                         int x_shift,
                         int pic_x,
                         int pic_width)
{
  int x;
  for (x = 0; x < pic_width; x++)
  {
    dst[x] = yu2rgb565(y[pic_x+x],
                       u[(pic_x+x)>>x_shift],
                       v[(pic_x+x)>>x_shift],
                       2); // Disable dithering for now.
  }
}

void ConvertYCbCrToRGB565(const uint8_t* y_buf,
                                   const uint8_t* u_buf,
                                   const uint8_t* v_buf,
                                   uint8_t* rgb_buf,
                                   int pic_x,
                                   int pic_y,
                                   int pic_width,
                                   int pic_height,
                                   int y_pitch,
                                   int uv_pitch,
                                   int rgb_pitch,
                                   YUVType yuv_type)
{
  int x_shift;
  int y_shift;
  x_shift = yuv_type != YV24;
  y_shift = yuv_type == YV12;
//TODO: fix NEON asm for iOS
#  if defined(MOZILLA_MAY_SUPPORT_NEON) && !defined(__APPLE__)
  if (yuv_type != YV24 && supports_neon())
  {
    for (int i = 0; i < pic_height; i++) {
      int yoffs;
      int uvoffs;
      yoffs = y_pitch * (pic_y+i) + pic_x;
      uvoffs = uv_pitch * ((pic_y+i)>>y_shift) + (pic_x>>x_shift);
      yuv42x_to_rgb565_row_neon((uint16_t*)(rgb_buf + rgb_pitch * i),
                                y_buf + yoffs,
                                u_buf + uvoffs,
                                v_buf + uvoffs,
                                pic_width,
                                pic_x&x_shift);
    }
  }
  else
#  endif
  {
    for (int i = 0; i < pic_height; i++) {
      int yoffs;
      int uvoffs;
      yoffs = y_pitch * (pic_y+i);
      uvoffs = uv_pitch * ((pic_y+i)>>y_shift);
      yuv_to_rgb565_row_c((uint16_t*)(rgb_buf + rgb_pitch * i),
                          y_buf + yoffs,
                          u_buf + uvoffs,
                          v_buf + uvoffs,
                          x_shift,
                          pic_x,
                          pic_width);
    }
  }
}

bool IsConvertYCbCrToRGB565Fast(int pic_x,
                                         int pic_y,
                                         int pic_width,
                                         int pic_height,
                                         YUVType yuv_type)
{
#  if defined(MOZILLA_MAY_SUPPORT_NEON)
  return (yuv_type != YV24 && supports_neon());
#  else
  return false;
#  endif
}

} // namespace gfx

} // namespace mozilla

#endif // HAVE_YCBCR_TO_RGB565