YUVBufferGenerator.cpp (3939B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "YUVBufferGenerator.h" 8 9 #include "VideoUtils.h" 10 11 using namespace mozilla::layers; 12 using namespace mozilla; 13 14 void YUVBufferGenerator::Init(const mozilla::gfx::IntSize& aSize) { 15 mImageSize = aSize; 16 17 int yPlaneLen = aSize.width * aSize.height; 18 int cbcrPlaneLen = (yPlaneLen + 1) / 2; 19 int frameLen = yPlaneLen + cbcrPlaneLen; 20 21 // Generate source buffer. 22 mSourceBuffer.SetLength(frameLen); 23 24 // Fill Y plane. 25 memset(mSourceBuffer.Elements(), 0x10, yPlaneLen); 26 27 // Fill Cb/Cr planes. 28 memset(mSourceBuffer.Elements() + yPlaneLen, 0x80, cbcrPlaneLen); 29 } 30 31 mozilla::gfx::IntSize YUVBufferGenerator::GetSize() const { return mImageSize; } 32 33 already_AddRefed<Image> YUVBufferGenerator::GenerateI420Image() { 34 return do_AddRef(CreateI420Image()); 35 } 36 37 already_AddRefed<Image> YUVBufferGenerator::GenerateNV12Image() { 38 return do_AddRef(CreateNV12Image()); 39 } 40 41 already_AddRefed<Image> YUVBufferGenerator::GenerateNV21Image() { 42 return do_AddRef(CreateNV21Image()); 43 } 44 45 Image* YUVBufferGenerator::CreateI420Image() { 46 PlanarYCbCrImage* image = 47 new RecyclingPlanarYCbCrImage(new BufferRecycleBin()); 48 PlanarYCbCrData data; 49 data.mPictureRect = gfx::IntRect(0, 0, mImageSize.width, mImageSize.height); 50 51 const uint32_t yPlaneSize = mImageSize.width * mImageSize.height; 52 const uint32_t halfWidth = (mImageSize.width + 1) / 2; 53 const uint32_t halfHeight = (mImageSize.height + 1) / 2; 54 const uint32_t uvPlaneSize = halfWidth * halfHeight; 55 56 // Y plane. 57 uint8_t* y = mSourceBuffer.Elements(); 58 data.mYChannel = y; 59 data.mYStride = mImageSize.width; 60 data.mYSkip = 0; 61 62 // Cr plane (aka V). 63 uint8_t* cr = y + yPlaneSize + uvPlaneSize; 64 data.mCrChannel = cr; 65 data.mCrSkip = 0; 66 67 // Cb plane (aka U). 68 uint8_t* cb = y + yPlaneSize; 69 data.mCbChannel = cb; 70 data.mCbSkip = 0; 71 72 // CrCb plane vectors. 73 data.mCbCrStride = halfWidth; 74 data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT; 75 76 data.mYUVColorSpace = DefaultColorSpace(mImageSize); 77 78 image->CopyData(data); 79 return image; 80 } 81 82 Image* YUVBufferGenerator::CreateNV12Image() { 83 NVImage* image = new NVImage(); 84 PlanarYCbCrData data; 85 data.mPictureRect = gfx::IntRect(0, 0, mImageSize.width, mImageSize.height); 86 87 const uint32_t yPlaneSize = mImageSize.width * mImageSize.height; 88 89 // Y plane. 90 uint8_t* y = mSourceBuffer.Elements(); 91 data.mYChannel = y; 92 data.mYStride = mImageSize.width; 93 data.mYSkip = 0; 94 95 // Cb plane (aka U). 96 uint8_t* cb = y + yPlaneSize; 97 data.mCbChannel = cb; 98 data.mCbSkip = 1; 99 100 // Cr plane (aka V). 101 uint8_t* cr = y + yPlaneSize + 1; 102 data.mCrChannel = cr; 103 data.mCrSkip = 1; 104 105 // 4:2:0. 106 data.mCbCrStride = mImageSize.width; 107 data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT; 108 109 image->SetData(data); 110 return image; 111 } 112 113 Image* YUVBufferGenerator::CreateNV21Image() { 114 NVImage* image = new NVImage(); 115 PlanarYCbCrData data; 116 data.mPictureRect = gfx::IntRect(0, 0, mImageSize.width, mImageSize.height); 117 118 const uint32_t yPlaneSize = mImageSize.width * mImageSize.height; 119 120 // Y plane. 121 uint8_t* y = mSourceBuffer.Elements(); 122 data.mYChannel = y; 123 data.mYStride = mImageSize.width; 124 data.mYSkip = 0; 125 126 // Cb plane (aka U). 127 uint8_t* cb = y + yPlaneSize + 1; 128 data.mCbChannel = cb; 129 data.mCbSkip = 1; 130 131 // Cr plane (aka V). 132 uint8_t* cr = y + yPlaneSize; 133 data.mCrChannel = cr; 134 data.mCrSkip = 1; 135 136 // 4:2:0. 137 data.mCbCrStride = mImageSize.width; 138 data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT; 139 140 data.mYUVColorSpace = DefaultColorSpace(mImageSize); 141 142 image->SetData(data); 143 return image; 144 }