summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/YUVBufferGenerator.cpp
blob: 60c8c6adcb26bda1cddae8de8ccdb3ca1b75a8af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "YUVBufferGenerator.h"

#include "VideoUtils.h"

using namespace mozilla::layers;
using namespace mozilla;

void YUVBufferGenerator::Init(const mozilla::gfx::IntSize& aSize) {
  mImageSize = aSize;

  int yPlaneLen = aSize.width * aSize.height;
  int cbcrPlaneLen = (yPlaneLen + 1) / 2;
  int frameLen = yPlaneLen + cbcrPlaneLen;

  // Generate source buffer.
  mSourceBuffer.SetLength(frameLen);

  // Fill Y plane.
  memset(mSourceBuffer.Elements(), 0x10, yPlaneLen);

  // Fill Cb/Cr planes.
  memset(mSourceBuffer.Elements() + yPlaneLen, 0x80, cbcrPlaneLen);
}

mozilla::gfx::IntSize YUVBufferGenerator::GetSize() const { return mImageSize; }

already_AddRefed<Image> YUVBufferGenerator::GenerateI420Image() {
  return do_AddRef(CreateI420Image());
}

already_AddRefed<Image> YUVBufferGenerator::GenerateNV12Image() {
  return do_AddRef(CreateNV12Image());
}

already_AddRefed<Image> YUVBufferGenerator::GenerateNV21Image() {
  return do_AddRef(CreateNV21Image());
}

Image* YUVBufferGenerator::CreateI420Image() {
  PlanarYCbCrImage* image =
      new RecyclingPlanarYCbCrImage(new BufferRecycleBin());
  PlanarYCbCrData data;
  data.mPictureRect = gfx::IntRect(0, 0, mImageSize.width, mImageSize.height);

  const uint32_t yPlaneSize = mImageSize.width * mImageSize.height;
  const uint32_t halfWidth = (mImageSize.width + 1) / 2;
  const uint32_t halfHeight = (mImageSize.height + 1) / 2;
  const uint32_t uvPlaneSize = halfWidth * halfHeight;

  // Y plane.
  uint8_t* y = mSourceBuffer.Elements();
  data.mYChannel = y;
  data.mYStride = mImageSize.width;
  data.mYSkip = 0;

  // Cr plane (aka V).
  uint8_t* cr = y + yPlaneSize + uvPlaneSize;
  data.mCrChannel = cr;
  data.mCrSkip = 0;

  // Cb plane (aka U).
  uint8_t* cb = y + yPlaneSize;
  data.mCbChannel = cb;
  data.mCbSkip = 0;

  // CrCb plane vectors.
  data.mCbCrStride = halfWidth;
  data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT;

  data.mYUVColorSpace = DefaultColorSpace(mImageSize);

  image->CopyData(data);
  return image;
}

Image* YUVBufferGenerator::CreateNV12Image() {
  NVImage* image = new NVImage();
  PlanarYCbCrData data;
  data.mPictureRect = gfx::IntRect(0, 0, mImageSize.width, mImageSize.height);

  const uint32_t yPlaneSize = mImageSize.width * mImageSize.height;

  // Y plane.
  uint8_t* y = mSourceBuffer.Elements();
  data.mYChannel = y;
  data.mYStride = mImageSize.width;
  data.mYSkip = 0;

  // Cb plane (aka U).
  uint8_t* cb = y + yPlaneSize;
  data.mCbChannel = cb;
  data.mCbSkip = 1;

  // Cr plane (aka V).
  uint8_t* cr = y + yPlaneSize + 1;
  data.mCrChannel = cr;
  data.mCrSkip = 1;

  // 4:2:0.
  data.mCbCrStride = mImageSize.width;
  data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT;

  image->SetData(data);
  return image;
}

Image* YUVBufferGenerator::CreateNV21Image() {
  NVImage* image = new NVImage();
  PlanarYCbCrData data;
  data.mPictureRect = gfx::IntRect(0, 0, mImageSize.width, mImageSize.height);

  const uint32_t yPlaneSize = mImageSize.width * mImageSize.height;

  // Y plane.
  uint8_t* y = mSourceBuffer.Elements();
  data.mYChannel = y;
  data.mYStride = mImageSize.width;
  data.mYSkip = 0;

  // Cb plane (aka U).
  uint8_t* cb = y + yPlaneSize + 1;
  data.mCbChannel = cb;
  data.mCbSkip = 1;

  // Cr plane (aka V).
  uint8_t* cr = y + yPlaneSize;
  data.mCrChannel = cr;
  data.mCrSkip = 1;

  // 4:2:0.
  data.mCbCrStride = mImageSize.width;
  data.mChromaSubsampling = gfx::ChromaSubsampling::HALF_WIDTH_AND_HEIGHT;

  data.mYUVColorSpace = DefaultColorSpace(mImageSize);

  image->SetData(data);
  return image;
}