Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This webpage shows layout of YV12 and other YUV formats
      6 // http://www.fourcc.org/yuv.php
      7 // The actual conversion is best described here
      8 // http://en.wikipedia.org/wiki/YUV
      9 // An article on optimizing YUV conversion using tables instead of multiplies
     10 // http://lestourtereaux.free.fr/papers/data/yuvrgb.pdf
     11 //
     12 // YV12 is a full plane of Y and a half height, half width chroma planes
     13 // YV16 is a full plane of Y and a full height, half width chroma planes
     14 //
     15 // ARGB pixel format is output, which on little endian is stored as BGRA.
     16 // The alpha is set to 255, allowing the application to use RGBA or RGB32.
     17 
     18 #include "media/base/yuv_convert.h"
     19 
     20 #include "base/cpu.h"
     21 #include "base/logging.h"
     22 #include "base/memory/scoped_ptr.h"
     23 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
     24 #include "build/build_config.h"
     25 #include "media/base/simd/convert_rgb_to_yuv.h"
     26 #include "media/base/simd/convert_yuv_to_rgb.h"
     27 #include "media/base/simd/filter_yuv.h"
     28 #include "media/base/simd/yuv_to_rgb_table.h"
     29 
     30 #if defined(ARCH_CPU_X86_FAMILY)
     31 #if defined(COMPILER_MSVC)
     32 #include <intrin.h>
     33 #else
     34 #include <mmintrin.h>
     35 #endif
     36 #endif
     37 
     38 // Assembly functions are declared without namespace.
     39 extern "C" { void EmptyRegisterState_MMX(); }  // extern "C"
     40 
     41 namespace media {
     42 
     43 typedef void (*FilterYUVRowsProc)(uint8*, const uint8*, const uint8*, int, int);
     44 
     45 typedef void (*ConvertRGBToYUVProc)(const uint8*,
     46                                     uint8*,
     47                                     uint8*,
     48                                     uint8*,
     49                                     int,
     50                                     int,
     51                                     int,
     52                                     int,
     53                                     int);
     54 
     55 typedef void (*ConvertYUVToRGB32Proc)(const uint8*,
     56                                       const uint8*,
     57                                       const uint8*,
     58                                       uint8*,
     59                                       int,
     60                                       int,
     61                                       int,
     62                                       int,
     63                                       int,
     64                                       YUVType);
     65 
     66 typedef void (*ConvertYUVAToARGBProc)(const uint8*,
     67                                       const uint8*,
     68                                       const uint8*,
     69                                       const uint8*,
     70                                       uint8*,
     71                                       int,
     72                                       int,
     73                                       int,
     74                                       int,
     75                                       int,
     76                                       int,
     77                                       YUVType);
     78 
     79 typedef void (*ConvertYUVToRGB32RowProc)(const uint8*,
     80                                          const uint8*,
     81                                          const uint8*,
     82                                          uint8*,
     83                                          ptrdiff_t,
     84                                          const int16[1024][4]);
     85 
     86 typedef void (*ConvertYUVAToARGBRowProc)(const uint8*,
     87                                          const uint8*,
     88                                          const uint8*,
     89                                          const uint8*,
     90                                          uint8*,
     91                                          ptrdiff_t,
     92                                          const int16[1024][4]);
     93 
     94 typedef void (*ScaleYUVToRGB32RowProc)(const uint8*,
     95                                        const uint8*,
     96                                        const uint8*,
     97                                        uint8*,
     98                                        ptrdiff_t,
     99                                        ptrdiff_t,
    100                                        const int16[1024][4]);
    101 
    102 static FilterYUVRowsProc g_filter_yuv_rows_proc_ = NULL;
    103 static ConvertYUVToRGB32RowProc g_convert_yuv_to_rgb32_row_proc_ = NULL;
    104 static ScaleYUVToRGB32RowProc g_scale_yuv_to_rgb32_row_proc_ = NULL;
    105 static ScaleYUVToRGB32RowProc g_linear_scale_yuv_to_rgb32_row_proc_ = NULL;
    106 static ConvertRGBToYUVProc g_convert_rgb32_to_yuv_proc_ = NULL;
    107 static ConvertRGBToYUVProc g_convert_rgb24_to_yuv_proc_ = NULL;
    108 static ConvertYUVToRGB32Proc g_convert_yuv_to_rgb32_proc_ = NULL;
    109 static ConvertYUVAToARGBProc g_convert_yuva_to_argb_proc_ = NULL;
    110 
    111 // Empty SIMD registers state after using them.
    112 void EmptyRegisterStateStub() {}
    113 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
    114 void EmptyRegisterStateIntrinsic() { _mm_empty(); }
    115 #endif
    116 typedef void (*EmptyRegisterStateProc)();
    117 static EmptyRegisterStateProc g_empty_register_state_proc_ = NULL;
    118 
    119 // Get the appropriate value to bitshift by for vertical indices.
    120 int GetVerticalShift(YUVType type) {
    121   switch (type) {
    122     case YV16:
    123       return 0;
    124     case YV12:
    125     case YV12J:
    126       return 1;
    127   }
    128   NOTREACHED();
    129   return 0;
    130 }
    131 
    132 const int16 (&GetLookupTable(YUVType type))[1024][4] {
    133   switch (type) {
    134     case YV12:
    135     case YV16:
    136       return kCoefficientsRgbY;
    137     case YV12J:
    138       return kCoefficientsRgbY_JPEG;
    139   }
    140   NOTREACHED();
    141   return kCoefficientsRgbY;
    142 }
    143 
    144 void InitializeCPUSpecificYUVConversions() {
    145   CHECK(!g_filter_yuv_rows_proc_);
    146   CHECK(!g_convert_yuv_to_rgb32_row_proc_);
    147   CHECK(!g_scale_yuv_to_rgb32_row_proc_);
    148   CHECK(!g_linear_scale_yuv_to_rgb32_row_proc_);
    149   CHECK(!g_convert_rgb32_to_yuv_proc_);
    150   CHECK(!g_convert_rgb24_to_yuv_proc_);
    151   CHECK(!g_convert_yuv_to_rgb32_proc_);
    152   CHECK(!g_convert_yuva_to_argb_proc_);
    153   CHECK(!g_empty_register_state_proc_);
    154 
    155   g_filter_yuv_rows_proc_ = FilterYUVRows_C;
    156   g_convert_yuv_to_rgb32_row_proc_ = ConvertYUVToRGB32Row_C;
    157   g_scale_yuv_to_rgb32_row_proc_ = ScaleYUVToRGB32Row_C;
    158   g_linear_scale_yuv_to_rgb32_row_proc_ = LinearScaleYUVToRGB32Row_C;
    159   g_convert_rgb32_to_yuv_proc_ = ConvertRGB32ToYUV_C;
    160   g_convert_rgb24_to_yuv_proc_ = ConvertRGB24ToYUV_C;
    161   g_convert_yuv_to_rgb32_proc_ = ConvertYUVToRGB32_C;
    162   g_convert_yuva_to_argb_proc_ = ConvertYUVAToARGB_C;
    163   g_empty_register_state_proc_ = EmptyRegisterStateStub;
    164 
    165   // Assembly code confuses MemorySanitizer.
    166 #if defined(ARCH_CPU_X86_FAMILY) && !defined(MEMORY_SANITIZER)
    167   g_convert_yuva_to_argb_proc_ = ConvertYUVAToARGB_MMX;
    168 
    169 #if defined(MEDIA_MMX_INTRINSICS_AVAILABLE)
    170   g_empty_register_state_proc_ = EmptyRegisterStateIntrinsic;
    171 #else
    172   g_empty_register_state_proc_ = EmptyRegisterState_MMX;
    173 #endif
    174 
    175   g_convert_yuv_to_rgb32_row_proc_ = ConvertYUVToRGB32Row_SSE;
    176   g_convert_yuv_to_rgb32_proc_ = ConvertYUVToRGB32_SSE;
    177 
    178   g_filter_yuv_rows_proc_ = FilterYUVRows_SSE2;
    179   g_convert_rgb32_to_yuv_proc_ = ConvertRGB32ToYUV_SSE2;
    180 
    181 #if defined(ARCH_CPU_X86_64)
    182   g_scale_yuv_to_rgb32_row_proc_ = ScaleYUVToRGB32Row_SSE2_X64;
    183 
    184   // Technically this should be in the MMX section, but MSVC will optimize out
    185   // the export of LinearScaleYUVToRGB32Row_MMX, which is required by the unit
    186   // tests, if that decision can be made at compile time.  Since all X64 CPUs
    187   // have SSE2, we can hack around this by making the selection here.
    188   g_linear_scale_yuv_to_rgb32_row_proc_ = LinearScaleYUVToRGB32Row_MMX_X64;
    189 #else
    190   g_scale_yuv_to_rgb32_row_proc_ = ScaleYUVToRGB32Row_SSE;
    191   g_linear_scale_yuv_to_rgb32_row_proc_ = LinearScaleYUVToRGB32Row_SSE;
    192 #endif
    193 
    194   base::CPU cpu;
    195   if (cpu.has_ssse3()) {
    196     g_convert_rgb24_to_yuv_proc_ = &ConvertRGB24ToYUV_SSSE3;
    197 
    198     // TODO(hclam): Add ConvertRGB32ToYUV_SSSE3 when the cyan problem is solved.
    199     // See: crbug.com/100462
    200   }
    201 #endif
    202 }
    203 
    204 // Empty SIMD registers state after using them.
    205 void EmptyRegisterState() { g_empty_register_state_proc_(); }
    206 
    207 // 16.16 fixed point arithmetic
    208 const int kFractionBits = 16;
    209 const int kFractionMax = 1 << kFractionBits;
    210 const int kFractionMask = ((1 << kFractionBits) - 1);
    211 
    212 // Scale a frame of YUV to 32 bit ARGB.
    213 void ScaleYUVToRGB32(const uint8* y_buf,
    214                      const uint8* u_buf,
    215                      const uint8* v_buf,
    216                      uint8* rgb_buf,
    217                      int source_width,
    218                      int source_height,
    219                      int width,
    220                      int height,
    221                      int y_pitch,
    222                      int uv_pitch,
    223                      int rgb_pitch,
    224                      YUVType yuv_type,
    225                      Rotate view_rotate,
    226                      ScaleFilter filter) {
    227   // Handle zero sized sources and destinations.
    228   if ((yuv_type == YV12 && (source_width < 2 || source_height < 2)) ||
    229       (yuv_type == YV16 && (source_width < 2 || source_height < 1)) ||
    230       width == 0 || height == 0)
    231     return;
    232 
    233   // 4096 allows 3 buffers to fit in 12k.
    234   // Helps performance on CPU with 16K L1 cache.
    235   // Large enough for 3830x2160 and 30" displays which are 2560x1600.
    236   const int kFilterBufferSize = 4096;
    237   // Disable filtering if the screen is too big (to avoid buffer overflows).
    238   // This should never happen to regular users: they don't have monitors
    239   // wider than 4096 pixels.
    240   // TODO(fbarchard): Allow rotated videos to filter.
    241   if (source_width > kFilterBufferSize || view_rotate)
    242     filter = FILTER_NONE;
    243 
    244   unsigned int y_shift = GetVerticalShift(yuv_type);
    245   // Diagram showing origin and direction of source sampling.
    246   // ->0   4<-
    247   // 7       3
    248   //
    249   // 6       5
    250   // ->1   2<-
    251   // Rotations that start at right side of image.
    252   if ((view_rotate == ROTATE_180) || (view_rotate == ROTATE_270) ||
    253       (view_rotate == MIRROR_ROTATE_0) || (view_rotate == MIRROR_ROTATE_90)) {
    254     y_buf += source_width - 1;
    255     u_buf += source_width / 2 - 1;
    256     v_buf += source_width / 2 - 1;
    257     source_width = -source_width;
    258   }
    259   // Rotations that start at bottom of image.
    260   if ((view_rotate == ROTATE_90) || (view_rotate == ROTATE_180) ||
    261       (view_rotate == MIRROR_ROTATE_90) || (view_rotate == MIRROR_ROTATE_180)) {
    262     y_buf += (source_height - 1) * y_pitch;
    263     u_buf += ((source_height >> y_shift) - 1) * uv_pitch;
    264     v_buf += ((source_height >> y_shift) - 1) * uv_pitch;
    265     source_height = -source_height;
    266   }
    267 
    268   int source_dx = source_width * kFractionMax / width;
    269 
    270   if ((view_rotate == ROTATE_90) || (view_rotate == ROTATE_270)) {
    271     int tmp = height;
    272     height = width;
    273     width = tmp;
    274     tmp = source_height;
    275     source_height = source_width;
    276     source_width = tmp;
    277     int source_dy = source_height * kFractionMax / height;
    278     source_dx = ((source_dy >> kFractionBits) * y_pitch) << kFractionBits;
    279     if (view_rotate == ROTATE_90) {
    280       y_pitch = -1;
    281       uv_pitch = -1;
    282       source_height = -source_height;
    283     } else {
    284       y_pitch = 1;
    285       uv_pitch = 1;
    286     }
    287   }
    288 
    289   // Need padding because FilterRows() will write 1 to 16 extra pixels
    290   // after the end for SSE2 version.
    291   uint8 yuvbuf[16 + kFilterBufferSize * 3 + 16];
    292   uint8* ybuf =
    293       reinterpret_cast<uint8*>(reinterpret_cast<uintptr_t>(yuvbuf + 15) & ~15);
    294   uint8* ubuf = ybuf + kFilterBufferSize;
    295   uint8* vbuf = ubuf + kFilterBufferSize;
    296 
    297   // TODO(fbarchard): Fixed point math is off by 1 on negatives.
    298 
    299   // We take a y-coordinate in [0,1] space in the source image space, and
    300   // transform to a y-coordinate in [0,1] space in the destination image space.
    301   // Note that the coordinate endpoints lie on pixel boundaries, not on pixel
    302   // centers: e.g. a two-pixel-high image will have pixel centers at 0.25 and
    303   // 0.75.  The formula is as follows (in fixed-point arithmetic):
    304   //   y_dst = dst_height * ((y_src + 0.5) / src_height)
    305   //   dst_pixel = clamp([0, dst_height - 1], floor(y_dst - 0.5))
    306   // Implement this here as an accumulator + delta, to avoid expensive math
    307   // in the loop.
    308   int source_y_subpixel_accum =
    309       ((kFractionMax / 2) * source_height) / height - (kFractionMax / 2);
    310   int source_y_subpixel_delta = ((1 << kFractionBits) * source_height) / height;
    311 
    312   // TODO(fbarchard): Split this into separate function for better efficiency.
    313   for (int y = 0; y < height; ++y) {
    314     uint8* dest_pixel = rgb_buf + y * rgb_pitch;
    315     int source_y_subpixel = source_y_subpixel_accum;
    316     source_y_subpixel_accum += source_y_subpixel_delta;
    317     if (source_y_subpixel < 0)
    318       source_y_subpixel = 0;
    319     else if (source_y_subpixel > ((source_height - 1) << kFractionBits))
    320       source_y_subpixel = (source_height - 1) << kFractionBits;
    321 
    322     const uint8* y_ptr = NULL;
    323     const uint8* u_ptr = NULL;
    324     const uint8* v_ptr = NULL;
    325     // Apply vertical filtering if necessary.
    326     // TODO(fbarchard): Remove memcpy when not necessary.
    327     if (filter & media::FILTER_BILINEAR_V) {
    328       int source_y = source_y_subpixel >> kFractionBits;
    329       y_ptr = y_buf + source_y * y_pitch;
    330       u_ptr = u_buf + (source_y >> y_shift) * uv_pitch;
    331       v_ptr = v_buf + (source_y >> y_shift) * uv_pitch;
    332 
    333       // Vertical scaler uses 16.8 fixed point.
    334       int source_y_fraction = (source_y_subpixel & kFractionMask) >> 8;
    335       if (source_y_fraction != 0) {
    336         g_filter_yuv_rows_proc_(
    337             ybuf, y_ptr, y_ptr + y_pitch, source_width, source_y_fraction);
    338       } else {
    339         memcpy(ybuf, y_ptr, source_width);
    340       }
    341       y_ptr = ybuf;
    342       ybuf[source_width] = ybuf[source_width - 1];
    343 
    344       int uv_source_width = (source_width + 1) / 2;
    345       int source_uv_fraction;
    346 
    347       // For formats with half-height UV planes, each even-numbered pixel row
    348       // should not interpolate, since the next row to interpolate from should
    349       // be a duplicate of the current row.
    350       if (y_shift && (source_y & 0x1) == 0)
    351         source_uv_fraction = 0;
    352       else
    353         source_uv_fraction = source_y_fraction;
    354 
    355       if (source_uv_fraction != 0) {
    356         g_filter_yuv_rows_proc_(
    357             ubuf, u_ptr, u_ptr + uv_pitch, uv_source_width, source_uv_fraction);
    358         g_filter_yuv_rows_proc_(
    359             vbuf, v_ptr, v_ptr + uv_pitch, uv_source_width, source_uv_fraction);
    360       } else {
    361         memcpy(ubuf, u_ptr, uv_source_width);
    362         memcpy(vbuf, v_ptr, uv_source_width);
    363       }
    364       u_ptr = ubuf;
    365       v_ptr = vbuf;
    366       ubuf[uv_source_width] = ubuf[uv_source_width - 1];
    367       vbuf[uv_source_width] = vbuf[uv_source_width - 1];
    368     } else {
    369       // Offset by 1/2 pixel for center sampling.
    370       int source_y = (source_y_subpixel + (kFractionMax / 2)) >> kFractionBits;
    371       y_ptr = y_buf + source_y * y_pitch;
    372       u_ptr = u_buf + (source_y >> y_shift) * uv_pitch;
    373       v_ptr = v_buf + (source_y >> y_shift) * uv_pitch;
    374     }
    375     if (source_dx == kFractionMax) {  // Not scaled
    376       g_convert_yuv_to_rgb32_row_proc_(
    377           y_ptr, u_ptr, v_ptr, dest_pixel, width, kCoefficientsRgbY);
    378     } else {
    379       if (filter & FILTER_BILINEAR_H) {
    380         g_linear_scale_yuv_to_rgb32_row_proc_(y_ptr,
    381                                               u_ptr,
    382                                               v_ptr,
    383                                               dest_pixel,
    384                                               width,
    385                                               source_dx,
    386                                               kCoefficientsRgbY);
    387       } else {
    388         g_scale_yuv_to_rgb32_row_proc_(y_ptr,
    389                                        u_ptr,
    390                                        v_ptr,
    391                                        dest_pixel,
    392                                        width,
    393                                        source_dx,
    394                                        kCoefficientsRgbY);
    395       }
    396     }
    397   }
    398 
    399   g_empty_register_state_proc_();
    400 }
    401 
    402 // Scale a frame of YV12 to 32 bit ARGB for a specific rectangle.
    403 void ScaleYUVToRGB32WithRect(const uint8* y_buf,
    404                              const uint8* u_buf,
    405                              const uint8* v_buf,
    406                              uint8* rgb_buf,
    407                              int source_width,
    408                              int source_height,
    409                              int dest_width,
    410                              int dest_height,
    411                              int dest_rect_left,
    412                              int dest_rect_top,
    413                              int dest_rect_right,
    414                              int dest_rect_bottom,
    415                              int y_pitch,
    416                              int uv_pitch,
    417                              int rgb_pitch) {
    418   // This routine doesn't currently support up-scaling.
    419   CHECK_LE(dest_width, source_width);
    420   CHECK_LE(dest_height, source_height);
    421 
    422   // Sanity-check the destination rectangle.
    423   DCHECK(dest_rect_left >= 0 && dest_rect_right <= dest_width);
    424   DCHECK(dest_rect_top >= 0 && dest_rect_bottom <= dest_height);
    425   DCHECK(dest_rect_right > dest_rect_left);
    426   DCHECK(dest_rect_bottom > dest_rect_top);
    427 
    428   // Fixed-point value of vertical and horizontal scale down factor.
    429   // Values are in the format 16.16.
    430   int y_step = kFractionMax * source_height / dest_height;
    431   int x_step = kFractionMax * source_width / dest_width;
    432 
    433   // Determine the coordinates of the rectangle in 16.16 coords.
    434   // NB: Our origin is the *center* of the top/left pixel, NOT its top/left.
    435   // If we're down-scaling by more than a factor of two, we start with a 50%
    436   // fraction to avoid degenerating to point-sampling - we should really just
    437   // fix the fraction at 50% for all pixels in that case.
    438   int source_left = dest_rect_left * x_step;
    439   int source_right = (dest_rect_right - 1) * x_step;
    440   if (x_step < kFractionMax * 2) {
    441     source_left += ((x_step - kFractionMax) / 2);
    442     source_right += ((x_step - kFractionMax) / 2);
    443   } else {
    444     source_left += kFractionMax / 2;
    445     source_right += kFractionMax / 2;
    446   }
    447   int source_top = dest_rect_top * y_step;
    448   if (y_step < kFractionMax * 2) {
    449     source_top += ((y_step - kFractionMax) / 2);
    450   } else {
    451     source_top += kFractionMax / 2;
    452   }
    453 
    454   // Determine the parts of the Y, U and V buffers to interpolate.
    455   int source_y_left = source_left >> kFractionBits;
    456   int source_y_right =
    457       std::min((source_right >> kFractionBits) + 2, source_width + 1);
    458 
    459   int source_uv_left = source_y_left / 2;
    460   int source_uv_right = std::min((source_right >> (kFractionBits + 1)) + 2,
    461                                  (source_width + 1) / 2);
    462 
    463   int source_y_width = source_y_right - source_y_left;
    464   int source_uv_width = source_uv_right - source_uv_left;
    465 
    466   // Determine number of pixels in each output row.
    467   int dest_rect_width = dest_rect_right - dest_rect_left;
    468 
    469   // Intermediate buffer for vertical interpolation.
    470   // 4096 bytes allows 3 buffers to fit in 12k, which fits in a 16K L1 cache,
    471   // and is bigger than most users will generally need.
    472   // The buffer is 16-byte aligned and padded with 16 extra bytes; some of the
    473   // FilterYUVRowProcs have alignment requirements, and the SSE version can
    474   // write up to 16 bytes past the end of the buffer.
    475   const int kFilterBufferSize = 4096;
    476   const bool kAvoidUsingOptimizedFilter = source_width > kFilterBufferSize;
    477   uint8 yuv_temp[16 + kFilterBufferSize * 3 + 16];
    478   // memset() yuv_temp to 0 to avoid bogus warnings when running on Valgrind.
    479   if (RunningOnValgrind())
    480     memset(yuv_temp, 0, sizeof(yuv_temp));
    481   uint8* y_temp = reinterpret_cast<uint8*>(
    482       reinterpret_cast<uintptr_t>(yuv_temp + 15) & ~15);
    483   uint8* u_temp = y_temp + kFilterBufferSize;
    484   uint8* v_temp = u_temp + kFilterBufferSize;
    485 
    486   // Move to the top-left pixel of output.
    487   rgb_buf += dest_rect_top * rgb_pitch;
    488   rgb_buf += dest_rect_left * 4;
    489 
    490   // For each destination row perform interpolation and color space
    491   // conversion to produce the output.
    492   for (int row = dest_rect_top; row < dest_rect_bottom; ++row) {
    493     // Round the fixed-point y position to get the current row.
    494     int source_row = source_top >> kFractionBits;
    495     int source_uv_row = source_row / 2;
    496     DCHECK(source_row < source_height);
    497 
    498     // Locate the first row for each plane for interpolation.
    499     const uint8* y0_ptr = y_buf + y_pitch * source_row + source_y_left;
    500     const uint8* u0_ptr = u_buf + uv_pitch * source_uv_row + source_uv_left;
    501     const uint8* v0_ptr = v_buf + uv_pitch * source_uv_row + source_uv_left;
    502     const uint8* y1_ptr = NULL;
    503     const uint8* u1_ptr = NULL;
    504     const uint8* v1_ptr = NULL;
    505 
    506     // Locate the second row for interpolation, being careful not to overrun.
    507     if (source_row + 1 >= source_height) {
    508       y1_ptr = y0_ptr;
    509     } else {
    510       y1_ptr = y0_ptr + y_pitch;
    511     }
    512     if (source_uv_row + 1 >= (source_height + 1) / 2) {
    513       u1_ptr = u0_ptr;
    514       v1_ptr = v0_ptr;
    515     } else {
    516       u1_ptr = u0_ptr + uv_pitch;
    517       v1_ptr = v0_ptr + uv_pitch;
    518     }
    519 
    520     if (!kAvoidUsingOptimizedFilter) {
    521       // Vertical scaler uses 16.8 fixed point.
    522       int fraction = (source_top & kFractionMask) >> 8;
    523       g_filter_yuv_rows_proc_(
    524           y_temp + source_y_left, y0_ptr, y1_ptr, source_y_width, fraction);
    525       g_filter_yuv_rows_proc_(
    526           u_temp + source_uv_left, u0_ptr, u1_ptr, source_uv_width, fraction);
    527       g_filter_yuv_rows_proc_(
    528           v_temp + source_uv_left, v0_ptr, v1_ptr, source_uv_width, fraction);
    529 
    530       // Perform horizontal interpolation and color space conversion.
    531       // TODO(hclam): Use the MMX version after more testing.
    532       LinearScaleYUVToRGB32RowWithRange_C(y_temp,
    533                                           u_temp,
    534                                           v_temp,
    535                                           rgb_buf,
    536                                           dest_rect_width,
    537                                           source_left,
    538                                           x_step,
    539                                           kCoefficientsRgbY);
    540     } else {
    541       // If the frame is too large then we linear scale a single row.
    542       LinearScaleYUVToRGB32RowWithRange_C(y0_ptr,
    543                                           u0_ptr,
    544                                           v0_ptr,
    545                                           rgb_buf,
    546                                           dest_rect_width,
    547                                           source_left,
    548                                           x_step,
    549                                           kCoefficientsRgbY);
    550     }
    551 
    552     // Advance vertically in the source and destination image.
    553     source_top += y_step;
    554     rgb_buf += rgb_pitch;
    555   }
    556 
    557   g_empty_register_state_proc_();
    558 }
    559 
    560 void ConvertRGB32ToYUV(const uint8* rgbframe,
    561                        uint8* yplane,
    562                        uint8* uplane,
    563                        uint8* vplane,
    564                        int width,
    565                        int height,
    566                        int rgbstride,
    567                        int ystride,
    568                        int uvstride) {
    569   g_convert_rgb32_to_yuv_proc_(rgbframe,
    570                                yplane,
    571                                uplane,
    572                                vplane,
    573                                width,
    574                                height,
    575                                rgbstride,
    576                                ystride,
    577                                uvstride);
    578 }
    579 
    580 void ConvertRGB24ToYUV(const uint8* rgbframe,
    581                        uint8* yplane,
    582                        uint8* uplane,
    583                        uint8* vplane,
    584                        int width,
    585                        int height,
    586                        int rgbstride,
    587                        int ystride,
    588                        int uvstride) {
    589   g_convert_rgb24_to_yuv_proc_(rgbframe,
    590                                yplane,
    591                                uplane,
    592                                vplane,
    593                                width,
    594                                height,
    595                                rgbstride,
    596                                ystride,
    597                                uvstride);
    598 }
    599 
    600 void ConvertYUY2ToYUV(const uint8* src,
    601                       uint8* yplane,
    602                       uint8* uplane,
    603                       uint8* vplane,
    604                       int width,
    605                       int height) {
    606   for (int i = 0; i < height / 2; ++i) {
    607     for (int j = 0; j < (width / 2); ++j) {
    608       yplane[0] = src[0];
    609       *uplane = src[1];
    610       yplane[1] = src[2];
    611       *vplane = src[3];
    612       src += 4;
    613       yplane += 2;
    614       uplane++;
    615       vplane++;
    616     }
    617     for (int j = 0; j < (width / 2); ++j) {
    618       yplane[0] = src[0];
    619       yplane[1] = src[2];
    620       src += 4;
    621       yplane += 2;
    622     }
    623   }
    624 }
    625 
    626 void ConvertNV21ToYUV(const uint8* src,
    627                       uint8* yplane,
    628                       uint8* uplane,
    629                       uint8* vplane,
    630                       int width,
    631                       int height) {
    632   int y_plane_size = width * height;
    633   memcpy(yplane, src, y_plane_size);
    634 
    635   src += y_plane_size;
    636   int u_plane_size = y_plane_size >> 2;
    637   for (int i = 0; i < u_plane_size; ++i) {
    638     *vplane++ = *src++;
    639     *uplane++ = *src++;
    640   }
    641 }
    642 
    643 void ConvertYUVToRGB32(const uint8* yplane,
    644                        const uint8* uplane,
    645                        const uint8* vplane,
    646                        uint8* rgbframe,
    647                        int width,
    648                        int height,
    649                        int ystride,
    650                        int uvstride,
    651                        int rgbstride,
    652                        YUVType yuv_type) {
    653   g_convert_yuv_to_rgb32_proc_(yplane,
    654                                uplane,
    655                                vplane,
    656                                rgbframe,
    657                                width,
    658                                height,
    659                                ystride,
    660                                uvstride,
    661                                rgbstride,
    662                                yuv_type);
    663 }
    664 
    665 void ConvertYUVAToARGB(const uint8* yplane,
    666                        const uint8* uplane,
    667                        const uint8* vplane,
    668                        const uint8* aplane,
    669                        uint8* rgbframe,
    670                        int width,
    671                        int height,
    672                        int ystride,
    673                        int uvstride,
    674                        int astride,
    675                        int rgbstride,
    676                        YUVType yuv_type) {
    677   g_convert_yuva_to_argb_proc_(yplane,
    678                                uplane,
    679                                vplane,
    680                                aplane,
    681                                rgbframe,
    682                                width,
    683                                height,
    684                                ystride,
    685                                uvstride,
    686                                astride,
    687                                rgbstride,
    688                                yuv_type);
    689 }
    690 
    691 }  // namespace media
    692