tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

yuv_convert.cpp (22346B)


      1 // Copyright (c) 2010 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 // YV24 is a full plane of Y and a full height, full width chroma planes
     15 // Y8   is a full plane of Y and no chroma planes (i.e., monochrome)
     16 //
     17 // ARGB pixel format is output, which on little endian is stored as BGRA.
     18 // The alpha is set to 255, allowing the application to use RGBA or RGB32.
     19 
     20 #include "yuv_convert.h"
     21 
     22 #include "libyuv.h"
     23 #include "mozilla/IntegerRange.h"
     24 #include "mozilla/SSE.h"
     25 #include "mozilla/StaticPrefs_gfx.h"
     26 #include "scale_yuv_argb.h"
     27 // Header for low level row functions.
     28 #include "yuv_row.h"
     29 
     30 namespace mozilla {
     31 
     32 namespace gfx {
     33 
     34 // 16.16 fixed point arithmetic
     35 const int kFractionBits = 16;
     36 const int kFractionMax = 1 << kFractionBits;
     37 const int kFractionMask = ((1 << kFractionBits) - 1);
     38 
     39 // clang-format off
     40 
     41 nsresult ToNSResult(int aLibyuvResult) {
     42  // Docs for libyuv::ConvertToI420 say:
     43  // Returns 0 for successful; -1 for invalid parameter. Non-zero for failure.
     44  switch (aLibyuvResult) {
     45    case 0:
     46      return NS_OK;
     47    case -1:
     48      return NS_ERROR_INVALID_ARG;
     49    default:
     50      return NS_ERROR_FAILURE;
     51  }
     52 }
     53 
     54 libyuv::FourCC FourCCFromYUVType(YUVType aYUVType) {
     55  switch (aYUVType) {
     56    case YV24: return libyuv::FOURCC_I444;
     57    case YV16: return libyuv::FOURCC_I422;
     58    case YV12: return libyuv::FOURCC_I420;
     59    case   Y8: return libyuv::FOURCC_I400;
     60    default:   return libyuv::FOURCC_ANY;
     61  }
     62 }
     63 
     64 int GBRPlanarToARGB(const uint8_t* src_y, int y_pitch,
     65                     const uint8_t* src_u, int u_pitch,
     66                     const uint8_t* src_v, int v_pitch,
     67                     uint8_t* rgb_buf, int rgb_pitch,
     68                     int pic_width, int pic_height) {
     69  // libyuv has no native conversion function for this
     70  // fixme: replace with something less awful
     71  for (const auto row : IntegerRange(pic_height)) {
     72    for (const auto col : IntegerRange(pic_width)) {
     73      rgb_buf[rgb_pitch * row + col * 4 + 0] = src_u[u_pitch * row + col];
     74      rgb_buf[rgb_pitch * row + col * 4 + 1] = src_y[y_pitch * row + col];
     75      rgb_buf[rgb_pitch * row + col * 4 + 2] = src_v[v_pitch * row + col];
     76      rgb_buf[rgb_pitch * row + col * 4 + 3] = 255;
     77    }
     78  }
     79  return 0;
     80 }
     81 
     82 // Convert a frame of YUV to 32 bit ARGB or ABGR.
     83 nsresult
     84 ConvertYCbCrToRGB32(const uint8_t* y_buf,
     85                    const uint8_t* u_buf,
     86                    const uint8_t* v_buf,
     87                    uint8_t* rgb_buf,
     88                    int pic_x,
     89                    int pic_y,
     90                    int pic_width,
     91                    int pic_height,
     92                    int y_pitch,
     93                    int uv_pitch,
     94                    int rgb_pitch,
     95                    YUVType yuv_type,
     96                    YUVColorSpace yuv_color_space,
     97                    ColorRange color_range,
     98                    RGB32Type rgb32_type) {
     99  // Deprecated function's conversion is accurate.
    100  // libyuv converion is a bit inaccurate to get performance. It dynamically
    101  // calculates RGB from YUV to use simd. In it, signed byte is used for
    102  // conversion's coefficient, but it requests 129. libyuv cut 129 to 127. And
    103  // only 6 bits are used for a decimal part during the dynamic calculation.
    104  //
    105  // The function is still fast on some old intel chips.
    106  // See Bug 1256475.
    107  bool use_deprecated = StaticPrefs::gfx_ycbcr_accurate_conversion() ||
    108                        (supports_mmx() && supports_sse() && !supports_sse3() &&
    109                         yuv_color_space == YUVColorSpace::BT601 &&
    110                         color_range == ColorRange::LIMITED);
    111  // The deprecated function only support BT601.
    112  // See Bug 1210357.
    113  if (yuv_color_space != YUVColorSpace::BT601) {
    114    use_deprecated = false;
    115  }
    116  if (use_deprecated) {
    117    return ConvertYCbCrToRGB32_deprecated(
    118        y_buf, u_buf, v_buf, rgb_buf, pic_x, pic_y, pic_width, pic_height,
    119        y_pitch, uv_pitch, rgb_pitch, yuv_type, rgb32_type);
    120  }
    121 
    122  decltype(libyuv::I420ToARGBMatrix)* fConvertYUVToARGB = nullptr;
    123  const uint8_t* src_y = nullptr;
    124  const uint8_t* src_u = nullptr;
    125  const uint8_t* src_v = nullptr;
    126  const libyuv::YuvConstants* yuv_constant = nullptr;
    127  bool swap_uv = rgb32_type == RGB32Type::ABGR;
    128 
    129  switch (yuv_color_space) {
    130    case YUVColorSpace::BT2020:
    131      yuv_constant = color_range == ColorRange::LIMITED
    132        ? swap_uv? &libyuv::kYvu2020Constants : &libyuv::kYuv2020Constants
    133        : swap_uv? &libyuv::kYvuV2020Constants : &libyuv::kYuvV2020Constants;
    134      break;
    135    case YUVColorSpace::BT709:
    136      yuv_constant = color_range == ColorRange::LIMITED
    137        ? swap_uv? &libyuv::kYvuH709Constants : &libyuv::kYuvH709Constants
    138        : swap_uv? &libyuv::kYvuF709Constants : &libyuv::kYuvF709Constants;
    139      break;
    140    case YUVColorSpace::Identity:
    141      if (yuv_type == YV24) {
    142        break;
    143      }
    144      NS_WARNING("Identity (aka RGB) with chroma subsampling is unsupported");
    145      return NS_ERROR_NOT_IMPLEMENTED;
    146      // TODO: Consider using BT601 for unsupported input?
    147    default:
    148      MOZ_FALLTHROUGH_ASSERT("Unsupported YUVColorSpace");
    149    case YUVColorSpace::BT601:
    150      yuv_constant = color_range == ColorRange::LIMITED
    151        ? swap_uv? &libyuv::kYvuI601Constants : &libyuv::kYuvI601Constants
    152        : swap_uv? &libyuv::kYvuJPEGConstants : &libyuv::kYuvJPEGConstants;
    153      break;
    154  }
    155 
    156  switch (yuv_type) {
    157    case YV24: {
    158      src_y = y_buf + y_pitch * pic_y + pic_x;
    159      src_u = u_buf + uv_pitch * pic_y + pic_x;
    160      src_v = v_buf + uv_pitch * pic_y + pic_x;
    161 
    162      if (yuv_color_space == YUVColorSpace::Identity) {
    163        const uint8_t* u_channel = swap_uv? src_v : src_u;
    164        const uint8_t* v_channel = swap_uv? src_u : src_v;
    165        // Special case for RGB image
    166        return ToNSResult(GBRPlanarToARGB(src_y, y_pitch, u_channel, uv_pitch, v_channel,
    167                          uv_pitch, rgb_buf, rgb_pitch, pic_width, pic_height));
    168      }
    169 
    170      fConvertYUVToARGB = libyuv::I444ToARGBMatrix;
    171      break;
    172    }
    173    case YV16: {
    174      src_y = y_buf + y_pitch * pic_y + pic_x;
    175      src_u = u_buf + uv_pitch * pic_y + pic_x / 2;
    176      src_v = v_buf + uv_pitch * pic_y + pic_x / 2;
    177 
    178      fConvertYUVToARGB = libyuv::I422ToARGBMatrix;
    179      break;
    180    }
    181    case YV12: {
    182      src_y = y_buf + y_pitch * pic_y + pic_x;
    183      src_u = u_buf + (uv_pitch * pic_y + pic_x) / 2;
    184      src_v = v_buf + (uv_pitch * pic_y + pic_x) / 2;
    185 
    186      fConvertYUVToARGB = libyuv::I420ToARGBMatrix;
    187      break;
    188    }
    189    case Y8: {
    190      src_y = y_buf + y_pitch * pic_y + pic_x;
    191      MOZ_ASSERT(u_buf == nullptr);
    192      MOZ_ASSERT(v_buf == nullptr);
    193 
    194      if (color_range == ColorRange::LIMITED) {
    195        return ToNSResult(libyuv::I400ToARGB(src_y, y_pitch, rgb_buf, rgb_pitch,
    196                                             pic_width, pic_height));
    197      }
    198      return ToNSResult(libyuv::J400ToARGB(src_y, y_pitch, rgb_buf, rgb_pitch,
    199                                           pic_width, pic_height));
    200    }
    201    default:
    202      MOZ_ASSERT_UNREACHABLE("Unsupported YUV type");
    203      return NS_ERROR_NOT_IMPLEMENTED;
    204  }
    205 
    206  const uint8_t* u_channel = swap_uv? src_v : src_u;
    207  const uint8_t* v_channel = swap_uv? src_u : src_v;
    208  return ToNSResult(fConvertYUVToARGB(src_y, y_pitch, u_channel, uv_pitch,
    209                                      v_channel, uv_pitch, rgb_buf, rgb_pitch,
    210                                      yuv_constant, pic_width, pic_height));
    211 }
    212 
    213 // Convert a frame of YUV to 32 bit ARGB or ABGR.
    214 nsresult
    215 ConvertYCbCrToRGB32_deprecated(const uint8_t* y_buf,
    216                               const uint8_t* u_buf,
    217                               const uint8_t* v_buf,
    218                               uint8_t* rgb_buf,
    219                               int pic_x,
    220                               int pic_y,
    221                               int pic_width,
    222                               int pic_height,
    223                               int y_pitch,
    224                               int uv_pitch,
    225                               int rgb_pitch,
    226                               YUVType yuv_type,
    227                               RGB32Type rgb32_type) {
    228  unsigned int y_shift = yuv_type == YV12 ? 1 : 0;
    229  unsigned int x_shift = yuv_type == YV24 ? 0 : 1;
    230  // Test for SSE because the optimized code uses movntq, which is not part of MMX.
    231  bool has_sse = supports_mmx() && supports_sse();
    232  // There is no optimized YV24 SSE routine so we check for this and
    233  // fall back to the C code.
    234  has_sse &= yuv_type != YV24;
    235  bool odd_pic_x = yuv_type != YV24 && pic_x % 2 != 0;
    236  int x_width = odd_pic_x ? pic_width - 1 : pic_width;
    237  bool swap_uv = rgb32_type == RGB32Type::ABGR;
    238  const uint8_t* u_channel = swap_uv? v_buf : u_buf;
    239  const uint8_t* v_channel = swap_uv? u_buf : v_buf;
    240 
    241  for (int y = pic_y; y < pic_height + pic_y; ++y) {
    242    uint8_t* rgb_row = rgb_buf + (y - pic_y) * rgb_pitch;
    243    const uint8_t* y_ptr = y_buf + y * y_pitch + pic_x;
    244    const uint8_t* u_ptr = u_channel + (y >> y_shift) * uv_pitch + (pic_x >> x_shift);
    245    const uint8_t* v_ptr = v_channel + (y >> y_shift) * uv_pitch + (pic_x >> x_shift);
    246 
    247    if (odd_pic_x) {
    248      // Handle the single odd pixel manually and use the
    249      // fast routines for the remaining.
    250      FastConvertYUVToRGB32Row_C(y_ptr++,
    251                                 u_ptr++,
    252                                 v_ptr++,
    253                                 rgb_row,
    254                                 1,
    255                                 x_shift);
    256      rgb_row += 4;
    257    }
    258 
    259    if (has_sse) {
    260      FastConvertYUVToRGB32Row(y_ptr,
    261                               u_ptr,
    262                               v_ptr,
    263                               rgb_row,
    264                               x_width);
    265    }
    266    else {
    267      FastConvertYUVToRGB32Row_C(y_ptr,
    268                                 u_ptr,
    269                                 v_ptr,
    270                                 rgb_row,
    271                                 x_width,
    272                                 x_shift);
    273    }
    274  }
    275 
    276  // MMX used for FastConvertYUVToRGB32Row requires emms instruction.
    277  if (has_sse)
    278    EMMS();
    279 
    280  return NS_OK;
    281 }
    282 
    283 // C version does 8 at a time to mimic MMX code
    284 static void FilterRows_C(uint8_t* ybuf, const uint8_t* y0_ptr, const uint8_t* y1_ptr,
    285                         int source_width, int source_y_fraction) {
    286  int y1_fraction = source_y_fraction;
    287  int y0_fraction = 256 - y1_fraction;
    288  uint8_t* end = ybuf + source_width;
    289  do {
    290    ybuf[0] = (y0_ptr[0] * y0_fraction + y1_ptr[0] * y1_fraction) >> 8;
    291    ybuf[1] = (y0_ptr[1] * y0_fraction + y1_ptr[1] * y1_fraction) >> 8;
    292    ybuf[2] = (y0_ptr[2] * y0_fraction + y1_ptr[2] * y1_fraction) >> 8;
    293    ybuf[3] = (y0_ptr[3] * y0_fraction + y1_ptr[3] * y1_fraction) >> 8;
    294    ybuf[4] = (y0_ptr[4] * y0_fraction + y1_ptr[4] * y1_fraction) >> 8;
    295    ybuf[5] = (y0_ptr[5] * y0_fraction + y1_ptr[5] * y1_fraction) >> 8;
    296    ybuf[6] = (y0_ptr[6] * y0_fraction + y1_ptr[6] * y1_fraction) >> 8;
    297    ybuf[7] = (y0_ptr[7] * y0_fraction + y1_ptr[7] * y1_fraction) >> 8;
    298    y0_ptr += 8;
    299    y1_ptr += 8;
    300    ybuf += 8;
    301  } while (ybuf < end);
    302 }
    303 
    304 #ifdef MOZILLA_MAY_SUPPORT_MMX
    305 void FilterRows_MMX(uint8_t* ybuf, const uint8_t* y0_ptr, const uint8_t* y1_ptr,
    306                    int source_width, int source_y_fraction);
    307 #endif
    308 
    309 #ifdef MOZILLA_MAY_SUPPORT_SSE2
    310 void FilterRows_SSE2(uint8_t* ybuf, const uint8_t* y0_ptr, const uint8_t* y1_ptr,
    311                     int source_width, int source_y_fraction);
    312 #endif
    313 
    314 static inline void FilterRows(uint8_t* ybuf, const uint8_t* y0_ptr,
    315                              const uint8_t* y1_ptr, int source_width,
    316                              int source_y_fraction) {
    317 #ifdef MOZILLA_MAY_SUPPORT_SSE2
    318  if (mozilla::supports_sse2()) {
    319    FilterRows_SSE2(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
    320    return;
    321  }
    322 #endif
    323 
    324 #ifdef MOZILLA_MAY_SUPPORT_MMX
    325  if (mozilla::supports_mmx()) {
    326    FilterRows_MMX(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
    327    return;
    328  }
    329 #endif
    330 
    331  FilterRows_C(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
    332 }
    333 
    334 
    335 // Scale a frame of YUV to 32 bit ARGB.
    336 nsresult
    337 ScaleYCbCrToRGB32(const uint8_t* y_buf,
    338                  const uint8_t* u_buf,
    339                  const uint8_t* v_buf,
    340                  uint8_t* rgb_buf,
    341                  int source_width,
    342                  int source_height,
    343                  int width,
    344                  int height,
    345                  int y_pitch,
    346                  int uv_pitch,
    347                  int rgb_pitch,
    348                  YUVType yuv_type,
    349                  YUVColorSpace yuv_color_space,
    350                  ScaleFilter filter) {
    351  bool use_deprecated =
    352      StaticPrefs::gfx_ycbcr_accurate_conversion() ||
    353 #if defined(XP_WIN) && defined(_M_X64)
    354      // libyuv does not support SIMD scaling on win 64bit. See Bug 1295927.
    355      supports_sse3() ||
    356 #endif
    357      (supports_mmx() && supports_sse() && !supports_sse3());
    358  // The deprecated function only support BT601.
    359  // See Bug 1210357.
    360  if (yuv_color_space != YUVColorSpace::BT601) {
    361    use_deprecated = false;
    362  }
    363  if (use_deprecated) {
    364    return ScaleYCbCrToRGB32_deprecated(
    365        y_buf, u_buf, v_buf, rgb_buf, source_width, source_height, width,
    366        height, y_pitch, uv_pitch, rgb_pitch, yuv_type, ROTATE_0, filter);
    367  }
    368 
    369  return ToNSResult(libyuv::YUVToARGBScale(
    370      y_buf, y_pitch, u_buf, uv_pitch, v_buf, uv_pitch,
    371      FourCCFromYUVType(yuv_type), yuv_color_space, source_width, source_height,
    372      rgb_buf, rgb_pitch, width, height, libyuv::kFilterBilinear));
    373 }
    374 
    375 // Scale a frame of YUV to 32 bit ARGB.
    376 nsresult
    377 ScaleYCbCrToRGB32_deprecated(const uint8_t* y_buf,
    378                             const uint8_t* u_buf,
    379                             const uint8_t* v_buf,
    380                             uint8_t* rgb_buf,
    381                             int source_width,
    382                             int source_height,
    383                             int width,
    384                             int height,
    385                             int y_pitch,
    386                             int uv_pitch,
    387                             int rgb_pitch,
    388                             YUVType yuv_type,
    389                             Rotate view_rotate,
    390                             ScaleFilter filter) {
    391  bool has_mmx = supports_mmx();
    392 
    393  // 4096 allows 3 buffers to fit in 12k.
    394  // Helps performance on CPU with 16K L1 cache.
    395  // Large enough for 3830x2160 and 30" displays which are 2560x1600.
    396  const int kFilterBufferSize = 4096;
    397  // Disable filtering if the screen is too big (to avoid buffer overflows).
    398  // This should never happen to regular users: they don't have monitors
    399  // wider than 4096 pixels.
    400  // TODO(fbarchard): Allow rotated videos to filter.
    401  if (source_width > kFilterBufferSize || view_rotate)
    402    filter = FILTER_NONE;
    403 
    404  unsigned int y_shift = yuv_type == YV12 ? 1 : 0;
    405  // Diagram showing origin and direction of source sampling.
    406  // ->0   4<-
    407  // 7       3
    408  //
    409  // 6       5
    410  // ->1   2<-
    411  // Rotations that start at right side of image.
    412  if ((view_rotate == ROTATE_180) ||
    413      (view_rotate == ROTATE_270) ||
    414      (view_rotate == MIRROR_ROTATE_0) ||
    415      (view_rotate == MIRROR_ROTATE_90)) {
    416    y_buf += source_width - 1;
    417    u_buf += source_width / 2 - 1;
    418    v_buf += source_width / 2 - 1;
    419    source_width = -source_width;
    420  }
    421  // Rotations that start at bottom of image.
    422  if ((view_rotate == ROTATE_90) ||
    423      (view_rotate == ROTATE_180) ||
    424      (view_rotate == MIRROR_ROTATE_90) ||
    425      (view_rotate == MIRROR_ROTATE_180)) {
    426    y_buf += (source_height - 1) * y_pitch;
    427    u_buf += ((source_height >> y_shift) - 1) * uv_pitch;
    428    v_buf += ((source_height >> y_shift) - 1) * uv_pitch;
    429    source_height = -source_height;
    430  }
    431 
    432  // Handle zero sized destination.
    433  if (width == 0 || height == 0)
    434    return NS_ERROR_INVALID_ARG;
    435  int source_dx = source_width * kFractionMax / width;
    436  int source_dy = source_height * kFractionMax / height;
    437  int source_dx_uv = source_dx;
    438 
    439  if ((view_rotate == ROTATE_90) ||
    440      (view_rotate == ROTATE_270)) {
    441    int tmp = height;
    442    height = width;
    443    width = tmp;
    444    tmp = source_height;
    445    source_height = source_width;
    446    source_width = tmp;
    447    int original_dx = source_dx;
    448    int original_dy = source_dy;
    449    source_dx = ((original_dy >> kFractionBits) * y_pitch) << kFractionBits;
    450    source_dx_uv = ((original_dy >> kFractionBits) * uv_pitch) << kFractionBits;
    451    source_dy = original_dx;
    452    if (view_rotate == ROTATE_90) {
    453      y_pitch = -1;
    454      uv_pitch = -1;
    455      source_height = -source_height;
    456    } else {
    457      y_pitch = 1;
    458      uv_pitch = 1;
    459    }
    460  }
    461 
    462  // Need padding because FilterRows() will write 1 to 16 extra pixels
    463  // after the end for SSE2 version.
    464  uint8_t yuvbuf[16 + kFilterBufferSize * 3 + 16];
    465  uint8_t* ybuf =
    466      reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(yuvbuf + 15) & ~15);
    467  uint8_t* ubuf = ybuf + kFilterBufferSize;
    468  uint8_t* vbuf = ubuf + kFilterBufferSize;
    469  // TODO(fbarchard): Fixed point math is off by 1 on negatives.
    470  int yscale_fixed = (source_height << kFractionBits) / height;
    471 
    472  // TODO(fbarchard): Split this into separate function for better efficiency.
    473  for (int y = 0; y < height; ++y) {
    474    uint8_t* dest_pixel = rgb_buf + y * rgb_pitch;
    475    int source_y_subpixel = (y * yscale_fixed);
    476    if (yscale_fixed >= (kFractionMax * 2)) {
    477      source_y_subpixel += kFractionMax / 2;  // For 1/2 or less, center filter.
    478    }
    479    int source_y = source_y_subpixel >> kFractionBits;
    480 
    481    const uint8_t* y0_ptr = y_buf + source_y * y_pitch;
    482    const uint8_t* y1_ptr = y0_ptr + y_pitch;
    483 
    484    const uint8_t* u0_ptr = u_buf + (source_y >> y_shift) * uv_pitch;
    485    const uint8_t* u1_ptr = u0_ptr + uv_pitch;
    486    const uint8_t* v0_ptr = v_buf + (source_y >> y_shift) * uv_pitch;
    487    const uint8_t* v1_ptr = v0_ptr + uv_pitch;
    488 
    489    // vertical scaler uses 16.8 fixed point
    490    int source_y_fraction = (source_y_subpixel & kFractionMask) >> 8;
    491    int source_uv_fraction =
    492        ((source_y_subpixel >> y_shift) & kFractionMask) >> 8;
    493 
    494    const uint8_t* y_ptr = y0_ptr;
    495    const uint8_t* u_ptr = u0_ptr;
    496    const uint8_t* v_ptr = v0_ptr;
    497    // Apply vertical filtering if necessary.
    498    // TODO(fbarchard): Remove memcpy when not necessary.
    499    if (filter & mozilla::gfx::FILTER_BILINEAR_V) {
    500      if (yscale_fixed != kFractionMax &&
    501          source_y_fraction && ((source_y + 1) < source_height)) {
    502        FilterRows(ybuf, y0_ptr, y1_ptr, source_width, source_y_fraction);
    503      } else {
    504        memcpy(ybuf, y0_ptr, source_width);
    505      }
    506      y_ptr = ybuf;
    507      ybuf[source_width] = ybuf[source_width-1];
    508      int uv_source_width = (source_width + 1) / 2;
    509      if (yscale_fixed != kFractionMax &&
    510          source_uv_fraction &&
    511          (((source_y >> y_shift) + 1) < (source_height >> y_shift))) {
    512        FilterRows(ubuf, u0_ptr, u1_ptr, uv_source_width, source_uv_fraction);
    513        FilterRows(vbuf, v0_ptr, v1_ptr, uv_source_width, source_uv_fraction);
    514      } else {
    515        memcpy(ubuf, u0_ptr, uv_source_width);
    516        memcpy(vbuf, v0_ptr, uv_source_width);
    517      }
    518      u_ptr = ubuf;
    519      v_ptr = vbuf;
    520      ubuf[uv_source_width] = ubuf[uv_source_width - 1];
    521      vbuf[uv_source_width] = vbuf[uv_source_width - 1];
    522    }
    523    if (source_dx == kFractionMax) {  // Not scaled
    524      FastConvertYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
    525                               dest_pixel, width);
    526    } else if (filter & FILTER_BILINEAR_H) {
    527        LinearScaleYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
    528                                 dest_pixel, width, source_dx);
    529    } else {
    530 // Specialized scalers and rotation.
    531 #if defined(MOZILLA_MAY_SUPPORT_SSE) && defined(_MSC_VER) && defined(_M_IX86) && !defined(__clang__)
    532      if(mozilla::supports_sse()) {
    533        if (width == (source_width * 2)) {
    534          DoubleYUVToRGB32Row_SSE(y_ptr, u_ptr, v_ptr,
    535                                  dest_pixel, width);
    536        } else if ((source_dx & kFractionMask) == 0) {
    537          // Scaling by integer scale factor. ie half.
    538          ConvertYUVToRGB32Row_SSE(y_ptr, u_ptr, v_ptr,
    539                                   dest_pixel, width,
    540                                   source_dx >> kFractionBits);
    541        } else if (source_dx_uv == source_dx) {  // Not rotated.
    542          ScaleYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
    543                             dest_pixel, width, source_dx);
    544        } else {
    545          RotateConvertYUVToRGB32Row_SSE(y_ptr, u_ptr, v_ptr,
    546                                         dest_pixel, width,
    547                                         source_dx >> kFractionBits,
    548                                         source_dx_uv >> kFractionBits);
    549        }
    550      }
    551      else {
    552        ScaleYUVToRGB32Row_C(y_ptr, u_ptr, v_ptr,
    553                             dest_pixel, width, source_dx);
    554      }
    555 #else
    556      (void)source_dx_uv;
    557      ScaleYUVToRGB32Row(y_ptr, u_ptr, v_ptr,
    558                         dest_pixel, width, source_dx);
    559 #endif
    560    }
    561  }
    562  // MMX used for FastConvertYUVToRGB32Row and FilterRows requires emms.
    563  if (has_mmx)
    564    EMMS();
    565 
    566  return NS_OK;
    567 }
    568 
    569 nsresult
    570 ConvertI420AlphaToARGB32(const uint8_t* y_buf,
    571                         const uint8_t* u_buf,
    572                         const uint8_t* v_buf,
    573                         const uint8_t* a_buf,
    574                         uint8_t* argb_buf,
    575                         int pic_width,
    576                         int pic_height,
    577                         int ya_pitch,
    578                         int uv_pitch,
    579                         int argb_pitch) {
    580 
    581  // The downstream graphics stack expects an attenuated input, hence why the
    582  // attenuation parameter is set.
    583  return ToNSResult(libyuv::I420AlphaToARGB(
    584      y_buf, ya_pitch, u_buf, uv_pitch, v_buf, uv_pitch, a_buf, ya_pitch,
    585      argb_buf, argb_pitch, pic_width, pic_height, 1));
    586 }
    587 
    588 } // namespace gfx
    589 } // namespace mozilla