-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorConverter.cpp
398 lines (331 loc) · 10.3 KB
/
ColorConverter.cpp
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "ColorConverter.h"
#include <d3d9.h>
#include <mferror.h>
#include "BufferLock.h"
const ConversionFunction CColorConverter::s_FormatConversions[] =
{
{ MFVideoFormat_RGB32, CColorConverter::TransformImage_RGB32 },
{ MFVideoFormat_RGB24, CColorConverter::TransformImage_RGB24 },
{ MFVideoFormat_YUY2, CColorConverter::TransformImage_YUY2 },
{ MFVideoFormat_NV12, CColorConverter::TransformImage_NV12 }
};
const int CColorConverter::s_NumConversionFuncs = ARRAYSIZE(s_FormatConversions);
CColorConverter::CColorConverter()
{
m_convertFn = NULL;
}
CColorConverter::~CColorConverter()
{
}
//-------------------------------------------------------------------
//
// Conversion functions
//
//-------------------------------------------------------------------
__forceinline BYTE Clip(int clr)
{
return (BYTE)(clr < 0 ? 0 : (clr > 255 ? 255 : clr));
}
__forceinline RGBQUAD ConvertYCrCbToRGB(
int y,
int cr,
int cb
)
{
RGBQUAD rgbq;
int c = y - 16;
int d = cb - 128;
int e = cr - 128;
rgbq.rgbRed = Clip((298 * c + 409 * e + 128) >> 8);
rgbq.rgbGreen = Clip((298 * c - 100 * d - 208 * e + 128) >> 8);
rgbq.rgbBlue = Clip((298 * c + 516 * d + 128) >> 8);
return rgbq;
}
//-------------------------------------------------------------------
// TransformImage_RGB24
//
// RGB-24 to RGB-32
//-------------------------------------------------------------------
void CColorConverter::TransformImage_RGB24(
BYTE* pDest,
LONG lDestStride,
const BYTE* pSrc,
LONG lSrcStride,
DWORD dwWidthInPixels,
DWORD dwHeightInPixels
)
{
for(DWORD y = 0; y < dwHeightInPixels; y++)
{
RGBTRIPLE *pSrcPel = (RGBTRIPLE*)pSrc;
DWORD *pDestPel = (DWORD*)pDest;
for(DWORD x = 0; x < dwWidthInPixels; x++)
{
pDestPel[x] = D3DCOLOR_XRGB(
pSrcPel[x].rgbtRed,
pSrcPel[x].rgbtGreen,
pSrcPel[x].rgbtBlue
);
}
pSrc += lSrcStride;
pDest += lDestStride;
}
}
//-------------------------------------------------------------------
// TransformImage_RGB32
//
// RGB-32 to RGB-32
//
// Note: This function is needed to copy the image from system
// memory to the Direct3D surface.
//-------------------------------------------------------------------
void CColorConverter::TransformImage_RGB32(
BYTE* pDest,
LONG lDestStride,
const BYTE* pSrc,
LONG lSrcStride,
DWORD dwWidthInPixels,
DWORD dwHeightInPixels
)
{
MFCopyImage(pDest, lDestStride, pSrc, lSrcStride, dwWidthInPixels * 4, dwHeightInPixels);
}
//-------------------------------------------------------------------
// TransformImage_YUY2
//
// YUY2 to RGB-32
//-------------------------------------------------------------------
void CColorConverter::TransformImage_YUY2(
BYTE* pDest,
LONG lDestStride,
const BYTE* pSrc,
LONG lSrcStride,
DWORD dwWidthInPixels,
DWORD dwHeightInPixels
)
{
for(DWORD y = 0; y < dwHeightInPixels; y++)
{
RGBQUAD *pDestPel = (RGBQUAD*)pDest;
WORD *pSrcPel = (WORD*)pSrc;
for(DWORD x = 0; x < dwWidthInPixels; x += 2)
{
// Byte order is U0 Y0 V0 Y1
int y0 = (int)LOBYTE(pSrcPel[x]);
int u0 = (int)HIBYTE(pSrcPel[x]);
int y1 = (int)LOBYTE(pSrcPel[x + 1]);
int v0 = (int)HIBYTE(pSrcPel[x + 1]);
pDestPel[x] = ConvertYCrCbToRGB(y0, v0, u0);
pDestPel[x + 1] = ConvertYCrCbToRGB(y1, v0, u0);
}
pSrc += lSrcStride;
pDest += lDestStride;
}
}
//-------------------------------------------------------------------
// TransformImage_NV12
//
// NV12 to RGB-32
//-------------------------------------------------------------------
void CColorConverter::TransformImage_NV12(
BYTE* pDst,
LONG dstStride,
const BYTE* pSrc,
LONG srcStride,
DWORD dwWidthInPixels,
DWORD dwHeightInPixels
)
{
const BYTE* lpBitsY = pSrc;
const BYTE* lpBitsCb = lpBitsY + (dwHeightInPixels * srcStride);;
const BYTE* lpBitsCr = lpBitsCb + 1;
for(UINT y = 0; y < dwHeightInPixels; y += 2)
{
const BYTE* lpLineY1 = lpBitsY;
const BYTE* lpLineY2 = lpBitsY + srcStride;
const BYTE* lpLineCr = lpBitsCr;
const BYTE* lpLineCb = lpBitsCb;
LPBYTE lpDibLine1 = pDst;
LPBYTE lpDibLine2 = pDst + dstStride;
for(UINT x = 0; x < dwWidthInPixels; x += 2)
{
int y0 = (int)lpLineY1[0];
int y1 = (int)lpLineY1[1];
int y2 = (int)lpLineY2[0];
int y3 = (int)lpLineY2[1];
int cb = (int)lpLineCb[0];
int cr = (int)lpLineCr[0];
RGBQUAD r = ConvertYCrCbToRGB(y0, cr, cb);
lpDibLine1[0] = r.rgbBlue;
lpDibLine1[1] = r.rgbGreen;
lpDibLine1[2] = r.rgbRed;
lpDibLine1[3] = 0; // Alpha
r = ConvertYCrCbToRGB(y1, cr, cb);
lpDibLine1[4] = r.rgbBlue;
lpDibLine1[5] = r.rgbGreen;
lpDibLine1[6] = r.rgbRed;
lpDibLine1[7] = 0; // Alpha
r = ConvertYCrCbToRGB(y2, cr, cb);
lpDibLine2[0] = r.rgbBlue;
lpDibLine2[1] = r.rgbGreen;
lpDibLine2[2] = r.rgbRed;
lpDibLine2[3] = 0; // Alpha
r = ConvertYCrCbToRGB(y3, cr, cb);
lpDibLine2[4] = r.rgbBlue;
lpDibLine2[5] = r.rgbGreen;
lpDibLine2[6] = r.rgbRed;
lpDibLine2[7] = 0; // Alpha
lpLineY1 += 2;
lpLineY2 += 2;
lpLineCr += 2;
lpLineCb += 2;
lpDibLine1 += 8;
lpDibLine2 += 8;
}
pDst += (2 * dstStride);
lpBitsY += (2 * srcStride);
lpBitsCr += srcStride;
lpBitsCb += srcStride;
}
}
HRESULT CColorConverter::SetConversionFunction(REFGUID subtype)
{
m_convertFn = NULL;
for(DWORD i = 0; i < s_NumConversionFuncs; i++)
{
if(s_FormatConversions[i].subtype == subtype)
{
m_convertFn = s_FormatConversions[i].xform;
return S_OK;
}
}
return MF_E_INVALIDMEDIATYPE;
}
void CColorConverter::ConvertImageToRGB32(BYTE* pDest, LONG destStride, IMFMediaBuffer *buf)
{
BYTE *pbScanline0 = NULL;
LONG lStride = 0;
VideoBufferLock buffer(buf);
HRESULT hr = buffer.LockBuffer(m_lDefaultStride, m_height, &pbScanline0, &lStride);
if(SUCCEEDED(hr))
{
if(m_convertFn)
m_convertFn(pDest, destStride, pbScanline0, lStride, m_width, m_height);
}
}
bool CColorConverter::IsFormatSupported(REFGUID subtype) const
{
for(DWORD i = 0; i < s_NumConversionFuncs; i++)
{
if(subtype == s_FormatConversions[i].subtype)
{
return TRUE;
}
}
return FALSE;
}
HRESULT CColorConverter::GetFormat(DWORD index, GUID *pSubtype) const
{
if(index < s_NumConversionFuncs)
{
*pSubtype = s_FormatConversions[index].subtype;
return S_OK;
}
return MF_E_NO_MORE_TYPES;
}
//-------------------------------------------------------------------
// SetVideoType
//
// Set the video format.
//-------------------------------------------------------------------
HRESULT CColorConverter::SetVideoType(IMFMediaType *pType)
{
HRESULT hr = S_OK;
GUID subtype = { 0 };
MFRatio PAR = { 0 };
// Find the video subtype.
hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
if(SUCCEEDED(hr))
{
// Choose a conversion function.
// (This also validates the format type.)
hr = SetConversionFunction(subtype);
if(SUCCEEDED(hr))
{
//
// Get some video attributes.
//
// Get the frame size.
hr = MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &m_width, &m_height);
if(SUCCEEDED(hr))
{
// Get the interlace mode. Default: assume progressive.
m_interlace = (MFVideoInterlaceMode)MFGetAttributeUINT32(
pType,
MF_MT_INTERLACE_MODE,
MFVideoInterlace_Progressive
);
// Get the image stride.
hr = GetDefaultStride(pType, &m_lDefaultStride);
if(SUCCEEDED(hr))
{
// Get the pixel aspect ratio. Default: Assume square pixels (1:1)
hr = MFGetAttributeRatio(
pType,
MF_MT_PIXEL_ASPECT_RATIO,
(UINT32*)&PAR.Numerator,
(UINT32*)&PAR.Denominator
);
if(SUCCEEDED(hr))
{
m_PixelAR = PAR;
}
else
{
m_PixelAR.Numerator = m_PixelAR.Denominator = 1;
}
}
}
}
}
return hr;
}
//-----------------------------------------------------------------------------
// GetDefaultStride
//
// Gets the default stride for a video frame, assuming no extra padding bytes.
//
//-----------------------------------------------------------------------------
HRESULT CColorConverter::GetDefaultStride(IMFMediaType *pType, LONG *plStride)
{
LONG lStride = 0;
// Try to get the default stride from the media type.
HRESULT hr = pType->GetUINT32(MF_MT_DEFAULT_STRIDE, (UINT32*)&lStride);
if(FAILED(hr))
{
// Attribute not set. Try to calculate the default stride.
GUID subtype = GUID_NULL;
UINT32 width = 0;
UINT32 height = 0;
// Get the subtype and the image size.
hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
if(SUCCEEDED(hr))
{
hr = MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
}
if(SUCCEEDED(hr))
{
hr = MFGetStrideForBitmapInfoHeader(subtype.Data1, width, &lStride);
}
// Set the attribute for later reference.
if(SUCCEEDED(hr))
{
(void)pType->SetUINT32(MF_MT_DEFAULT_STRIDE, UINT32(lStride));
}
}
if(SUCCEEDED(hr))
{
*plStride = lStride;
}
return hr;
}