YCbCrUtils.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "gfx2DGlue.h"
  6. #include "YCbCrUtils.h"
  7. #include "yuv_convert.h"
  8. #include "ycbcr_to_rgb565.h"
  9. namespace mozilla {
  10. namespace gfx {
  11. void
  12. GetYCbCrToRGBDestFormatAndSize(const layers::PlanarYCbCrData& aData,
  13. SurfaceFormat& aSuggestedFormat,
  14. IntSize& aSuggestedSize)
  15. {
  16. YUVType yuvtype =
  17. TypeFromSize(aData.mYSize.width,
  18. aData.mYSize.height,
  19. aData.mCbCrSize.width,
  20. aData.mCbCrSize.height);
  21. // 'prescale' is true if the scaling is to be done as part of the
  22. // YCbCr to RGB conversion rather than on the RGB data when rendered.
  23. bool prescale = aSuggestedSize.width > 0 && aSuggestedSize.height > 0 &&
  24. aSuggestedSize != aData.mPicSize;
  25. if (aSuggestedFormat == SurfaceFormat::R5G6B5_UINT16) {
  26. #if defined(HAVE_YCBCR_TO_RGB565)
  27. if (prescale &&
  28. !IsScaleYCbCrToRGB565Fast(aData.mPicX,
  29. aData.mPicY,
  30. aData.mPicSize.width,
  31. aData.mPicSize.height,
  32. aSuggestedSize.width,
  33. aSuggestedSize.height,
  34. yuvtype,
  35. FILTER_BILINEAR) &&
  36. IsConvertYCbCrToRGB565Fast(aData.mPicX,
  37. aData.mPicY,
  38. aData.mPicSize.width,
  39. aData.mPicSize.height,
  40. yuvtype)) {
  41. prescale = false;
  42. }
  43. #else
  44. // yuv2rgb16 function not available
  45. aSuggestedFormat = SurfaceFormat::B8G8R8X8;
  46. #endif
  47. }
  48. else if (aSuggestedFormat != SurfaceFormat::B8G8R8X8) {
  49. // No other formats are currently supported.
  50. aSuggestedFormat = SurfaceFormat::B8G8R8X8;
  51. }
  52. if (aSuggestedFormat == SurfaceFormat::B8G8R8X8) {
  53. /* ScaleYCbCrToRGB32 does not support a picture offset, nor 4:4:4 data.
  54. See bugs 639415 and 640073. */
  55. if (aData.mPicX != 0 || aData.mPicY != 0 || yuvtype == YV24)
  56. prescale = false;
  57. }
  58. if (!prescale) {
  59. aSuggestedSize = aData.mPicSize;
  60. }
  61. }
  62. void
  63. ConvertYCbCrToRGB(const layers::PlanarYCbCrData& aData,
  64. const SurfaceFormat& aDestFormat,
  65. const IntSize& aDestSize,
  66. unsigned char* aDestBuffer,
  67. int32_t aStride)
  68. {
  69. // ConvertYCbCrToRGB et al. assume the chroma planes are rounded up if the
  70. // luma plane is odd sized.
  71. MOZ_ASSERT((aData.mCbCrSize.width == aData.mYSize.width ||
  72. aData.mCbCrSize.width == (aData.mYSize.width + 1) >> 1) &&
  73. (aData.mCbCrSize.height == aData.mYSize.height ||
  74. aData.mCbCrSize.height == (aData.mYSize.height + 1) >> 1));
  75. YUVType yuvtype =
  76. TypeFromSize(aData.mYSize.width,
  77. aData.mYSize.height,
  78. aData.mCbCrSize.width,
  79. aData.mCbCrSize.height);
  80. // Convert from YCbCr to RGB now, scaling the image if needed.
  81. if (aDestSize != aData.mPicSize) {
  82. #if defined(HAVE_YCBCR_TO_RGB565)
  83. if (aDestFormat == SurfaceFormat::R5G6B5_UINT16) {
  84. ScaleYCbCrToRGB565(aData.mYChannel,
  85. aData.mCbChannel,
  86. aData.mCrChannel,
  87. aDestBuffer,
  88. aData.mPicX,
  89. aData.mPicY,
  90. aData.mPicSize.width,
  91. aData.mPicSize.height,
  92. aDestSize.width,
  93. aDestSize.height,
  94. aData.mYStride,
  95. aData.mCbCrStride,
  96. aStride,
  97. yuvtype,
  98. FILTER_BILINEAR);
  99. } else
  100. #endif
  101. ScaleYCbCrToRGB32(aData.mYChannel, //
  102. aData.mCbChannel,
  103. aData.mCrChannel,
  104. aDestBuffer,
  105. aData.mPicSize.width,
  106. aData.mPicSize.height,
  107. aDestSize.width,
  108. aDestSize.height,
  109. aData.mYStride,
  110. aData.mCbCrStride,
  111. aStride,
  112. yuvtype,
  113. aData.mYUVColorSpace,
  114. FILTER_BILINEAR);
  115. } else { // no prescale
  116. #if defined(HAVE_YCBCR_TO_RGB565)
  117. if (aDestFormat == SurfaceFormat::R5G6B5_UINT16) {
  118. ConvertYCbCrToRGB565(aData.mYChannel,
  119. aData.mCbChannel,
  120. aData.mCrChannel,
  121. aDestBuffer,
  122. aData.mPicX,
  123. aData.mPicY,
  124. aData.mPicSize.width,
  125. aData.mPicSize.height,
  126. aData.mYStride,
  127. aData.mCbCrStride,
  128. aStride,
  129. yuvtype);
  130. } else // aDestFormat != SurfaceFormat::R5G6B5_UINT16
  131. #endif
  132. ConvertYCbCrToRGB32(aData.mYChannel, //
  133. aData.mCbChannel,
  134. aData.mCrChannel,
  135. aDestBuffer,
  136. aData.mPicX,
  137. aData.mPicY,
  138. aData.mPicSize.width,
  139. aData.mPicSize.height,
  140. aData.mYStride,
  141. aData.mCbCrStride,
  142. aStride,
  143. yuvtype,
  144. aData.mYUVColorSpace);
  145. }
  146. }
  147. } // namespace gfx
  148. } // namespace mozilla