1 | /* $Id: RecordingUtils.cpp 75393 2018-11-12 09:53:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Recording utility code.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2018 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "Recording.h"
|
---|
19 | #include "RecordingUtils.h"
|
---|
20 |
|
---|
21 | #include <iprt/asm.h>
|
---|
22 | #include <iprt/assert.h>
|
---|
23 | #include <iprt/critsect.h>
|
---|
24 | #include <iprt/path.h>
|
---|
25 | #include <iprt/semaphore.h>
|
---|
26 | #include <iprt/thread.h>
|
---|
27 | #include <iprt/time.h>
|
---|
28 |
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Convert an image to YUV420p format.
|
---|
32 | *
|
---|
33 | * @return \c true on success, \c false on failure.
|
---|
34 | * @param aDstBuf The destination image buffer.
|
---|
35 | * @param aDstWidth Width (in pixel) of destination buffer.
|
---|
36 | * @param aDstHeight Height (in pixel) of destination buffer.
|
---|
37 | * @param aSrcBuf The source image buffer.
|
---|
38 | * @param aSrcWidth Width (in pixel) of source buffer.
|
---|
39 | * @param aSrcHeight Height (in pixel) of source buffer.
|
---|
40 | */
|
---|
41 | template <class T>
|
---|
42 | inline bool recordingUtilsColorConvWriteYUV420p(uint8_t *aDstBuf, unsigned aDstWidth, unsigned aDstHeight,
|
---|
43 | uint8_t *aSrcBuf, unsigned aSrcWidth, unsigned aSrcHeight)
|
---|
44 | {
|
---|
45 | RT_NOREF(aDstWidth, aDstHeight);
|
---|
46 |
|
---|
47 | AssertReturn(!(aSrcWidth & 1), false);
|
---|
48 | AssertReturn(!(aSrcHeight & 1), false);
|
---|
49 |
|
---|
50 | bool fRc = true;
|
---|
51 | T iter1(aSrcWidth, aSrcHeight, aSrcBuf);
|
---|
52 | T iter2 = iter1;
|
---|
53 | iter2.skip(aSrcWidth);
|
---|
54 | unsigned cPixels = aSrcWidth * aSrcHeight;
|
---|
55 | unsigned offY = 0;
|
---|
56 | unsigned offU = cPixels;
|
---|
57 | unsigned offV = cPixels + cPixels / 4;
|
---|
58 | unsigned const cyHalf = aSrcHeight / 2;
|
---|
59 | unsigned const cxHalf = aSrcWidth / 2;
|
---|
60 | for (unsigned i = 0; i < cyHalf && fRc; ++i)
|
---|
61 | {
|
---|
62 | for (unsigned j = 0; j < cxHalf; ++j)
|
---|
63 | {
|
---|
64 | unsigned red, green, blue;
|
---|
65 | fRc = iter1.getRGB(&red, &green, &blue);
|
---|
66 | AssertReturn(fRc, false);
|
---|
67 | aDstBuf[offY] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
68 | unsigned u = (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
69 | unsigned v = (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
70 |
|
---|
71 | fRc = iter1.getRGB(&red, &green, &blue);
|
---|
72 | AssertReturn(fRc, false);
|
---|
73 | aDstBuf[offY + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
74 | u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
75 | v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
76 |
|
---|
77 | fRc = iter2.getRGB(&red, &green, &blue);
|
---|
78 | AssertReturn(fRc, false);
|
---|
79 | aDstBuf[offY + aSrcWidth] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
80 | u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
81 | v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
82 |
|
---|
83 | fRc = iter2.getRGB(&red, &green, &blue);
|
---|
84 | AssertReturn(fRc, false);
|
---|
85 | aDstBuf[offY + aSrcWidth + 1] = ((66 * red + 129 * green + 25 * blue + 128) >> 8) + 16;
|
---|
86 | u += (((-38 * red - 74 * green + 112 * blue + 128) >> 8) + 128) / 4;
|
---|
87 | v += (((112 * red - 94 * green - 18 * blue + 128) >> 8) + 128) / 4;
|
---|
88 |
|
---|
89 | aDstBuf[offU] = u;
|
---|
90 | aDstBuf[offV] = v;
|
---|
91 | offY += 2;
|
---|
92 | ++offU;
|
---|
93 | ++offV;
|
---|
94 | }
|
---|
95 |
|
---|
96 | iter1.skip(aSrcWidth);
|
---|
97 | iter2.skip(aSrcWidth);
|
---|
98 | offY += aSrcWidth;
|
---|
99 | }
|
---|
100 |
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Convert an image to RGB24 format.
|
---|
106 | *
|
---|
107 | * @returns true on success, false on failure.
|
---|
108 | * @param aWidth Width of image.
|
---|
109 | * @param aHeight Height of image.
|
---|
110 | * @param aDestBuf An allocated memory buffer large enough to hold the
|
---|
111 | * destination image (i.e. width * height * 12bits).
|
---|
112 | * @param aSrcBuf The source image as an array of bytes.
|
---|
113 | */
|
---|
114 | template <class T>
|
---|
115 | inline bool RecordingUtilsColorConvWriteRGB24(unsigned aWidth, unsigned aHeight,
|
---|
116 | uint8_t *aDestBuf, uint8_t *aSrcBuf)
|
---|
117 | {
|
---|
118 | enum { PIX_SIZE = 3 };
|
---|
119 | bool rc = true;
|
---|
120 | AssertReturn(0 == (aWidth & 1), false);
|
---|
121 | AssertReturn(0 == (aHeight & 1), false);
|
---|
122 | T iter(aWidth, aHeight, aSrcBuf);
|
---|
123 | unsigned cPixels = aWidth * aHeight;
|
---|
124 | for (unsigned i = 0; i < cPixels && rc; ++i)
|
---|
125 | {
|
---|
126 | unsigned red, green, blue;
|
---|
127 | rc = iter.getRGB(&red, &green, &blue);
|
---|
128 | if (rc)
|
---|
129 | {
|
---|
130 | aDestBuf[i * PIX_SIZE ] = red;
|
---|
131 | aDestBuf[i * PIX_SIZE + 1] = green;
|
---|
132 | aDestBuf[i * PIX_SIZE + 2] = blue;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Converts a RGB to YUV buffer.
|
---|
140 | *
|
---|
141 | * @returns IPRT status code.
|
---|
142 | * @param uPixelFormat Pixel format to use for conversion.
|
---|
143 | * @param paDst Pointer to destination buffer.
|
---|
144 | * @param uDstWidth Width (X, in pixels) of destination buffer.
|
---|
145 | * @param uDstHeight Height (Y, in pixels) of destination buffer.
|
---|
146 | * @param paSrc Pointer to source buffer.
|
---|
147 | * @param uSrcWidth Width (X, in pixels) of source buffer.
|
---|
148 | * @param uSrcHeight Height (Y, in pixels) of source buffer.
|
---|
149 | */
|
---|
150 | int RecordingUtilsRGBToYUV(uint32_t uPixelFormat,
|
---|
151 | uint8_t *paDst, uint32_t uDstWidth, uint32_t uDstHeight,
|
---|
152 | uint8_t *paSrc, uint32_t uSrcWidth, uint32_t uSrcHeight)
|
---|
153 | {
|
---|
154 | switch (uPixelFormat)
|
---|
155 | {
|
---|
156 | case RECORDINGPIXELFMT_RGB32:
|
---|
157 | if (!recordingUtilsColorConvWriteYUV420p<ColorConvBGRA32Iter>(paDst, uDstWidth, uDstHeight,
|
---|
158 | paSrc, uSrcWidth, uSrcHeight))
|
---|
159 | return VERR_INVALID_PARAMETER;
|
---|
160 | break;
|
---|
161 | case RECORDINGPIXELFMT_RGB24:
|
---|
162 | if (!recordingUtilsColorConvWriteYUV420p<ColorConvBGR24Iter>(paDst, uDstWidth, uDstHeight,
|
---|
163 | paSrc, uSrcWidth, uSrcHeight))
|
---|
164 | return VERR_INVALID_PARAMETER;
|
---|
165 | break;
|
---|
166 | case RECORDINGPIXELFMT_RGB565:
|
---|
167 | if (!recordingUtilsColorConvWriteYUV420p<ColorConvBGR565Iter>(paDst, uDstWidth, uDstHeight,
|
---|
168 | paSrc, uSrcWidth, uSrcHeight))
|
---|
169 | return VERR_INVALID_PARAMETER;
|
---|
170 | break;
|
---|
171 | default:
|
---|
172 | AssertFailed();
|
---|
173 | return VERR_NOT_SUPPORTED;
|
---|
174 | }
|
---|
175 | return VINF_SUCCESS;
|
---|
176 | }
|
---|
177 |
|
---|