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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "SoftwareRenderer"
#include <utils/Log.h>
#include <binder/MemoryHeapBase.h>
#include <media/stagefright/MediaDebug.h>
#include <media/stagefright/SoftwareRenderer.h>
#include <ui/ISurface.h>
namespace android {
#define QCOM_YUV 0
SoftwareRenderer::SoftwareRenderer(
OMX_COLOR_FORMATTYPE colorFormat,
const sp<ISurface> &surface,
size_t displayWidth, size_t displayHeight,
size_t decodedWidth, size_t decodedHeight)
: mColorFormat(colorFormat),
mISurface(surface),
mDisplayWidth(displayWidth),
mDisplayHeight(displayHeight),
mDecodedWidth(decodedWidth),
mDecodedHeight(decodedHeight),
mFrameSize(mDecodedWidth * mDecodedHeight * 2), // RGB565
mMemoryHeap(new MemoryHeapBase(2 * mFrameSize)),
mIndex(0),
mClip(NULL) {
CHECK(mISurface.get() != NULL);
CHECK(mDecodedWidth > 0);
CHECK(mDecodedHeight > 0);
CHECK(mMemoryHeap->heapID() >= 0);
ISurface::BufferHeap bufferHeap(
mDisplayWidth, mDisplayHeight,
mDecodedWidth, mDecodedHeight,
PIXEL_FORMAT_RGB_565,
mMemoryHeap);
status_t err = mISurface->registerBuffers(bufferHeap);
CHECK_EQ(err, OK);
}
SoftwareRenderer::~SoftwareRenderer() {
mISurface->unregisterBuffers();
delete[] mClip;
mClip = NULL;
}
void SoftwareRenderer::render(
const void *data, size_t size, void *platformPrivate) {
static const int OMX_QCOM_COLOR_FormatYVU420SemiPlanar = 0x7FA30C00;
switch (mColorFormat) {
case OMX_COLOR_FormatYUV420Planar:
return renderYUV420Planar(data, size);
case OMX_COLOR_FormatCbYCrY:
return renderCbYCrY(data, size);
case OMX_QCOM_COLOR_FormatYVU420SemiPlanar:
return renderQCOMYUV420SemiPlanar(data, size);
default:
{
LOGW("Cannot render color format %ld", mColorFormat);
break;
}
}
}
void SoftwareRenderer::renderYUV420Planar(
const void *data, size_t size) {
if (size != (mDecodedHeight * mDecodedWidth * 3) / 2) {
LOGE("size is %d, expected %d",
size, (mDecodedHeight * mDecodedWidth * 3) / 2);
}
CHECK(size >= (mDecodedWidth * mDecodedHeight * 3) / 2);
uint8_t *kAdjustedClip = initClip();
size_t offset = mIndex * mFrameSize;
void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
uint32_t *dst_ptr = (uint32_t *)dst;
const uint8_t *src_y = (const uint8_t *)data;
const uint8_t *src_u =
(const uint8_t *)src_y + mDecodedWidth * mDecodedHeight;
#if !QCOM_YUV
const uint8_t *src_v =
(const uint8_t *)src_u + (mDecodedWidth / 2) * (mDecodedHeight / 2);
#endif
for (size_t y = 0; y < mDecodedHeight; ++y) {
for (size_t x = 0; x < mDecodedWidth; x += 2) {
// B = 1.164 * (Y - 16) + 2.018 * (U - 128)
// G = 1.164 * (Y - 16) - 0.813 * (V - 128) - 0.391 * (U - 128)
// R = 1.164 * (Y - 16) + 1.596 * (V - 128)
// B = 298/256 * (Y - 16) + 517/256 * (U - 128)
// G = .................. - 208/256 * (V - 128) - 100/256 * (U - 128)
// R = .................. + 409/256 * (V - 128)
// min_B = (298 * (- 16) + 517 * (- 128)) / 256 = -277
// min_G = (298 * (- 16) - 208 * (255 - 128) - 100 * (255 - 128)) / 256 = -172
// min_R = (298 * (- 16) + 409 * (- 128)) / 256 = -223
// max_B = (298 * (255 - 16) + 517 * (255 - 128)) / 256 = 534
// max_G = (298 * (255 - 16) - 208 * (- 128) - 100 * (- 128)) / 256 = 432
// max_R = (298 * (255 - 16) + 409 * (255 - 128)) / 256 = 481
// clip range -278 .. 535
signed y1 = (signed)src_y[x] - 16;
signed y2 = (signed)src_y[x + 1] - 16;
#if QCOM_YUV
signed u = (signed)src_u[x & ~1] - 128;
signed v = (signed)src_u[(x & ~1) + 1] - 128;
#else
signed u = (signed)src_u[x / 2] - 128;
signed v = (signed)src_v[x / 2] - 128;
#endif
signed u_b = u * 517;
signed u_g = -u * 100;
signed v_g = -v * 208;
signed v_r = v * 409;
signed tmp1 = y1 * 298;
signed b1 = (tmp1 + u_b) / 256;
signed g1 = (tmp1 + v_g + u_g) / 256;
signed r1 = (tmp1 + v_r) / 256;
signed tmp2 = y2 * 298;
signed b2 = (tmp2 + u_b) / 256;
signed g2 = (tmp2 + v_g + u_g) / 256;
signed r2 = (tmp2 + v_r) / 256;
uint32_t rgb1 =
((kAdjustedClip[r1] >> 3) << 11)
| ((kAdjustedClip[g1] >> 2) << 5)
| (kAdjustedClip[b1] >> 3);
uint32_t rgb2 =
((kAdjustedClip[r2] >> 3) << 11)
| ((kAdjustedClip[g2] >> 2) << 5)
| (kAdjustedClip[b2] >> 3);
dst_ptr[x / 2] = (rgb2 << 16) | rgb1;
}
src_y += mDecodedWidth;
if (y & 1) {
#if QCOM_YUV
src_u += mDecodedWidth;
#else
src_u += mDecodedWidth / 2;
src_v += mDecodedWidth / 2;
#endif
}
dst_ptr += mDecodedWidth / 2;
}
mISurface->postBuffer(offset);
mIndex = 1 - mIndex;
}
void SoftwareRenderer::renderCbYCrY(
const void *data, size_t size) {
if (size != (mDecodedHeight * mDecodedWidth * 2)) {
LOGE("size is %d, expected %d",
size, (mDecodedHeight * mDecodedWidth * 2));
}
CHECK(size >= (mDecodedWidth * mDecodedHeight * 2));
uint8_t *kAdjustedClip = initClip();
size_t offset = mIndex * mFrameSize;
void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
uint32_t *dst_ptr = (uint32_t *)dst;
const uint8_t *src = (const uint8_t *)data;
for (size_t y = 0; y < mDecodedHeight; ++y) {
for (size_t x = 0; x < mDecodedWidth; x += 2) {
signed y1 = (signed)src[2 * x + 1] - 16;
signed y2 = (signed)src[2 * x + 3] - 16;
signed u = (signed)src[2 * x] - 128;
signed v = (signed)src[2 * x + 2] - 128;
signed u_b = u * 517;
signed u_g = -u * 100;
signed v_g = -v * 208;
signed v_r = v * 409;
signed tmp1 = y1 * 298;
signed b1 = (tmp1 + u_b) / 256;
signed g1 = (tmp1 + v_g + u_g) / 256;
signed r1 = (tmp1 + v_r) / 256;
signed tmp2 = y2 * 298;
signed b2 = (tmp2 + u_b) / 256;
signed g2 = (tmp2 + v_g + u_g) / 256;
signed r2 = (tmp2 + v_r) / 256;
uint32_t rgb1 =
((kAdjustedClip[r1] >> 3) << 11)
| ((kAdjustedClip[g1] >> 2) << 5)
| (kAdjustedClip[b1] >> 3);
uint32_t rgb2 =
((kAdjustedClip[r2] >> 3) << 11)
| ((kAdjustedClip[g2] >> 2) << 5)
| (kAdjustedClip[b2] >> 3);
dst_ptr[x / 2] = (rgb2 << 16) | rgb1;
}
src += mDecodedWidth * 2;
dst_ptr += mDecodedWidth / 2;
}
mISurface->postBuffer(offset);
mIndex = 1 - mIndex;
}
void SoftwareRenderer::renderQCOMYUV420SemiPlanar(
const void *data, size_t size) {
if (size != (mDecodedHeight * mDecodedWidth * 3) / 2) {
LOGE("size is %d, expected %d",
size, (mDecodedHeight * mDecodedWidth * 3) / 2);
}
CHECK(size >= (mDecodedWidth * mDecodedHeight * 3) / 2);
uint8_t *kAdjustedClip = initClip();
size_t offset = mIndex * mFrameSize;
void *dst = (uint8_t *)mMemoryHeap->getBase() + offset;
uint32_t *dst_ptr = (uint32_t *)dst;
const uint8_t *src_y = (const uint8_t *)data;
const uint8_t *src_u =
(const uint8_t *)src_y + mDecodedWidth * mDecodedHeight;
for (size_t y = 0; y < mDecodedHeight; ++y) {
for (size_t x = 0; x < mDecodedWidth; x += 2) {
signed y1 = (signed)src_y[x] - 16;
signed y2 = (signed)src_y[x + 1] - 16;
signed u = (signed)src_u[x & ~1] - 128;
signed v = (signed)src_u[(x & ~1) + 1] - 128;
signed u_b = u * 517;
signed u_g = -u * 100;
signed v_g = -v * 208;
signed v_r = v * 409;
signed tmp1 = y1 * 298;
signed b1 = (tmp1 + u_b) / 256;
signed g1 = (tmp1 + v_g + u_g) / 256;
signed r1 = (tmp1 + v_r) / 256;
signed tmp2 = y2 * 298;
signed b2 = (tmp2 + u_b) / 256;
signed g2 = (tmp2 + v_g + u_g) / 256;
signed r2 = (tmp2 + v_r) / 256;
uint32_t rgb1 =
((kAdjustedClip[b1] >> 3) << 11)
| ((kAdjustedClip[g1] >> 2) << 5)
| (kAdjustedClip[r1] >> 3);
uint32_t rgb2 =
((kAdjustedClip[b2] >> 3) << 11)
| ((kAdjustedClip[g2] >> 2) << 5)
| (kAdjustedClip[r2] >> 3);
dst_ptr[x / 2] = (rgb2 << 16) | rgb1;
}
src_y += mDecodedWidth;
if (y & 1) {
src_u += mDecodedWidth;
}
dst_ptr += mDecodedWidth / 2;
}
mISurface->postBuffer(offset);
mIndex = 1 - mIndex;
}
uint8_t *SoftwareRenderer::initClip() {
static const signed kClipMin = -278;
static const signed kClipMax = 535;
if (mClip == NULL) {
mClip = new uint8_t[kClipMax - kClipMin + 1];
for (signed i = kClipMin; i <= kClipMax; ++i) {
mClip[i - kClipMin] = (i < 0) ? 0 : (i > 255) ? 255 : (uint8_t)i;
}
}
return &mClip[-kClipMin];
}
} // namespace android
|