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