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