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