/src/FreeRDP/libfreerdp/codec/h264.c
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * H.264 Bitmap Compression |
4 | | * |
5 | | * Copyright 2014 Mike McDonald <Mike.McDonald@software.dell.com> |
6 | | * Copyright 2017 David Fort <contact@hardening-consulting.com> |
7 | | * |
8 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
9 | | * you may not use this file except in compliance with the License. |
10 | | * You may obtain a copy of the License at |
11 | | * |
12 | | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | | * |
14 | | * Unless required by applicable law or agreed to in writing, software |
15 | | * distributed under the License is distributed on an "AS IS" BASIS, |
16 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17 | | * See the License for the specific language governing permissions and |
18 | | * limitations under the License. |
19 | | */ |
20 | | |
21 | | #include <freerdp/config.h> |
22 | | |
23 | | #include <winpr/crt.h> |
24 | | #include <winpr/print.h> |
25 | | #include <winpr/library.h> |
26 | | #include <winpr/bitstream.h> |
27 | | #include <winpr/synch.h> |
28 | | |
29 | | #include <freerdp/primitives.h> |
30 | | #include <freerdp/codec/h264.h> |
31 | | #include <freerdp/codec/yuv.h> |
32 | | #include <freerdp/log.h> |
33 | | |
34 | | #include "h264.h" |
35 | | |
36 | 0 | #define TAG FREERDP_TAG("codec") |
37 | | |
38 | | static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight); |
39 | | |
40 | | BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height) |
41 | 0 | { |
42 | 0 | BOOL isNull = FALSE; |
43 | 0 | UINT32 pheight = height; |
44 | |
|
45 | 0 | if (!h264) |
46 | 0 | return FALSE; |
47 | | |
48 | 0 | if (stride == 0) |
49 | 0 | stride = width; |
50 | |
|
51 | 0 | if (stride % 16 != 0) |
52 | 0 | stride += 16 - stride % 16; |
53 | |
|
54 | 0 | if (pheight % 16 != 0) |
55 | 0 | pheight += 16 - pheight % 16; |
56 | |
|
57 | 0 | for (size_t x = 0; x < 3; x++) |
58 | 0 | { |
59 | 0 | if (!h264->pYUVData[x] || !h264->pOldYUVData[x]) |
60 | 0 | isNull = TRUE; |
61 | 0 | } |
62 | |
|
63 | 0 | if (pheight == 0) |
64 | 0 | return FALSE; |
65 | 0 | if (stride == 0) |
66 | 0 | return FALSE; |
67 | | |
68 | 0 | if (isNull || (width != h264->width) || (height != h264->height) || |
69 | 0 | (stride != h264->iStride[0])) |
70 | 0 | { |
71 | 0 | h264->iStride[0] = stride; |
72 | 0 | h264->iStride[1] = (stride + 1) / 2; |
73 | 0 | h264->iStride[2] = (stride + 1) / 2; |
74 | 0 | h264->width = width; |
75 | 0 | h264->height = height; |
76 | |
|
77 | 0 | for (size_t x = 0; x < 3; x++) |
78 | 0 | { |
79 | 0 | BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], h264->iStride[x], pheight, 16); |
80 | 0 | BYTE* tmp2 = |
81 | 0 | winpr_aligned_recalloc(h264->pOldYUVData[x], h264->iStride[x], pheight, 16); |
82 | 0 | if (tmp1) |
83 | 0 | h264->pYUVData[x] = tmp1; |
84 | 0 | if (tmp2) |
85 | 0 | h264->pOldYUVData[x] = tmp2; |
86 | 0 | if (!tmp1 || !tmp2) |
87 | 0 | return FALSE; |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | 0 | return TRUE; |
92 | 0 | } |
93 | | |
94 | | INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData, |
95 | | DWORD DstFormat, UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, |
96 | | const RECTANGLE_16* regionRects, UINT32 numRegionRects) |
97 | 0 | { |
98 | 0 | int status = 0; |
99 | 0 | const BYTE* pYUVData[3]; |
100 | |
|
101 | 0 | if (!h264 || h264->Compressor) |
102 | 0 | return -1001; |
103 | | |
104 | 0 | status = h264->subsystem->Decompress(h264, pSrcData, SrcSize); |
105 | |
|
106 | 0 | if (status == 0) |
107 | 0 | return 1; |
108 | | |
109 | 0 | if (status < 0) |
110 | 0 | return status; |
111 | | |
112 | 0 | pYUVData[0] = h264->pYUVData[0]; |
113 | 0 | pYUVData[1] = h264->pYUVData[1]; |
114 | 0 | pYUVData[2] = h264->pYUVData[2]; |
115 | 0 | if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->height, DstFormat, |
116 | 0 | pDstData, nDstStep, regionRects, numRegionRects)) |
117 | 0 | return -1002; |
118 | | |
119 | 0 | return 1; |
120 | 0 | } |
121 | | |
122 | | static BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles, |
123 | | RDPGFX_H264_METABLOCK* meta, size_t count) |
124 | 0 | { |
125 | | /* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */ |
126 | 0 | if (!meta || (QP > UINT8_MAX)) |
127 | 0 | { |
128 | 0 | free(rectangles); |
129 | 0 | return FALSE; |
130 | 0 | } |
131 | | |
132 | 0 | meta->regionRects = rectangles; |
133 | 0 | if (count == 0) |
134 | 0 | return TRUE; |
135 | | |
136 | 0 | if (count > UINT32_MAX) |
137 | 0 | return FALSE; |
138 | | |
139 | 0 | meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY)); |
140 | |
|
141 | 0 | if (!meta->quantQualityVals || !meta->regionRects) |
142 | 0 | return FALSE; |
143 | 0 | meta->numRegionRects = (UINT32)count; |
144 | 0 | for (size_t x = 0; x < count; x++) |
145 | 0 | { |
146 | 0 | RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x]; |
147 | 0 | cur->qp = (UINT8)QP; |
148 | | |
149 | | /* qpVal bit 6 and 7 are flags, so mask them out here. |
150 | | * qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */ |
151 | 0 | cur->qualityVal = 100 - (QP & 0x3F); |
152 | 0 | } |
153 | 0 | return TRUE; |
154 | 0 | } |
155 | | |
156 | | static INLINE BOOL diff_tile(const RECTANGLE_16* regionRect, BYTE* pYUVData[3], |
157 | | BYTE* pOldYUVData[3], UINT32 const iStride[3]) |
158 | 0 | { |
159 | 0 | size_t size = 0; |
160 | |
|
161 | 0 | if (!regionRect || !pYUVData || !pOldYUVData || !iStride) |
162 | 0 | return FALSE; |
163 | 0 | size = regionRect->right - regionRect->left; |
164 | 0 | if (regionRect->right > iStride[0]) |
165 | 0 | return FALSE; |
166 | 0 | if (regionRect->right / 2u > iStride[1]) |
167 | 0 | return FALSE; |
168 | 0 | if (regionRect->right / 2u > iStride[2]) |
169 | 0 | return FALSE; |
170 | | |
171 | 0 | for (UINT16 y = regionRect->top; y < regionRect->bottom; y++) |
172 | 0 | { |
173 | 0 | const BYTE* cur0 = &pYUVData[0][y * iStride[0]]; |
174 | 0 | const BYTE* cur1 = &pYUVData[1][y * iStride[1]]; |
175 | 0 | const BYTE* cur2 = &pYUVData[2][y * iStride[2]]; |
176 | 0 | const BYTE* old0 = &pOldYUVData[0][y * iStride[0]]; |
177 | 0 | const BYTE* old1 = &pOldYUVData[1][y * iStride[1]]; |
178 | 0 | const BYTE* old2 = &pOldYUVData[2][y * iStride[2]]; |
179 | |
|
180 | 0 | if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0) |
181 | 0 | return TRUE; |
182 | 0 | if (memcmp(&cur1[regionRect->left / 2], &old1[regionRect->left / 2], size / 2) != 0) |
183 | 0 | return TRUE; |
184 | 0 | if (memcmp(&cur2[regionRect->left / 2], &old2[regionRect->left / 2], size / 2) != 0) |
185 | 0 | return TRUE; |
186 | 0 | } |
187 | 0 | return FALSE; |
188 | 0 | } |
189 | | |
190 | | static BOOL detect_changes(BOOL firstFrameDone, const UINT32 QP, const RECTANGLE_16* regionRect, |
191 | | BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32 const iStride[3], |
192 | | RDPGFX_H264_METABLOCK* meta) |
193 | 0 | { |
194 | 0 | size_t count = 0; |
195 | 0 | size_t wc = 0; |
196 | 0 | size_t hc = 0; |
197 | 0 | RECTANGLE_16* rectangles = NULL; |
198 | |
|
199 | 0 | if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta) |
200 | 0 | return FALSE; |
201 | | |
202 | 0 | wc = (regionRect->right - regionRect->left) / 64 + 1; |
203 | 0 | hc = (regionRect->bottom - regionRect->top) / 64 + 1; |
204 | 0 | rectangles = calloc(wc * hc, sizeof(RECTANGLE_16)); |
205 | 0 | if (!rectangles) |
206 | 0 | return FALSE; |
207 | 0 | if (!firstFrameDone) |
208 | 0 | { |
209 | 0 | rectangles[0] = *regionRect; |
210 | 0 | count = 1; |
211 | 0 | } |
212 | 0 | else |
213 | 0 | { |
214 | 0 | for (size_t y = regionRect->top; y < regionRect->bottom; y += 64) |
215 | 0 | { |
216 | 0 | for (size_t x = regionRect->left; x < regionRect->right; x += 64) |
217 | 0 | { |
218 | 0 | RECTANGLE_16 rect; |
219 | 0 | rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x); |
220 | 0 | rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y); |
221 | 0 | rect.right = |
222 | 0 | (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right)); |
223 | 0 | rect.bottom = |
224 | 0 | (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom)); |
225 | 0 | if (diff_tile(&rect, pYUVData, pOldYUVData, iStride)) |
226 | 0 | rectangles[count++] = rect; |
227 | 0 | } |
228 | 0 | } |
229 | 0 | } |
230 | 0 | if (!allocate_h264_metablock(QP, rectangles, meta, count)) |
231 | 0 | return FALSE; |
232 | 0 | return TRUE; |
233 | 0 | } |
234 | | |
235 | | INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, |
236 | | UINT32 nSrcWidth, UINT32 nSrcHeight, const RECTANGLE_16* regionRect, |
237 | | BYTE** ppDstData, UINT32* pDstSize, RDPGFX_H264_METABLOCK* meta) |
238 | 0 | { |
239 | 0 | INT32 rc = -1; |
240 | 0 | BYTE* pYUVData[3] = { 0 }; |
241 | 0 | const BYTE* pcYUVData[3] = { 0 }; |
242 | 0 | BYTE* pOldYUVData[3] = { 0 }; |
243 | |
|
244 | 0 | if (!h264 || !regionRect || !meta || !h264->Compressor) |
245 | 0 | return -1; |
246 | | |
247 | 0 | if (!h264->subsystem->Compress) |
248 | 0 | return -1; |
249 | | |
250 | 0 | if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight)) |
251 | 0 | return -1; |
252 | | |
253 | 0 | if (h264->encodingBuffer) |
254 | 0 | { |
255 | 0 | for (size_t x = 0; x < 3; x++) |
256 | 0 | { |
257 | 0 | pYUVData[x] = h264->pYUVData[x]; |
258 | 0 | pOldYUVData[x] = h264->pOldYUVData[x]; |
259 | 0 | } |
260 | 0 | } |
261 | 0 | else |
262 | 0 | { |
263 | 0 | for (size_t x = 0; x < 3; x++) |
264 | 0 | { |
265 | 0 | pYUVData[x] = h264->pOldYUVData[x]; |
266 | 0 | pOldYUVData[x] = h264->pYUVData[x]; |
267 | 0 | } |
268 | 0 | } |
269 | 0 | h264->encodingBuffer = !h264->encodingBuffer; |
270 | |
|
271 | 0 | if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData, |
272 | 0 | regionRect, 1)) |
273 | 0 | goto fail; |
274 | | |
275 | 0 | if (!detect_changes(h264->firstLumaFrameDone, h264->QP, regionRect, pYUVData, pOldYUVData, |
276 | 0 | h264->iStride, meta)) |
277 | 0 | goto fail; |
278 | | |
279 | 0 | if (meta->numRegionRects == 0) |
280 | 0 | { |
281 | 0 | rc = 0; |
282 | 0 | goto fail; |
283 | 0 | } |
284 | | |
285 | 0 | for (size_t x = 0; x < 3; x++) |
286 | 0 | pcYUVData[x] = pYUVData[x]; |
287 | |
|
288 | 0 | rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize); |
289 | 0 | if (rc >= 0) |
290 | 0 | h264->firstLumaFrameDone = TRUE; |
291 | |
|
292 | 0 | fail: |
293 | 0 | if (rc < 0) |
294 | 0 | free_h264_metablock(meta); |
295 | 0 | return rc; |
296 | 0 | } |
297 | | |
298 | | INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, |
299 | | UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version, const RECTANGLE_16* region, |
300 | | BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData, |
301 | | UINT32* pAuxDstSize, RDPGFX_H264_METABLOCK* meta, |
302 | | RDPGFX_H264_METABLOCK* auxMeta) |
303 | 0 | { |
304 | 0 | int rc = -1; |
305 | 0 | BYTE* coded = NULL; |
306 | 0 | UINT32 codedSize = 0; |
307 | 0 | BYTE** pYUV444Data = NULL; |
308 | 0 | BYTE** pOldYUV444Data = NULL; |
309 | 0 | BYTE** pYUVData = NULL; |
310 | 0 | BYTE** pOldYUVData = NULL; |
311 | |
|
312 | 0 | if (!h264 || !h264->Compressor) |
313 | 0 | return -1; |
314 | | |
315 | 0 | if (!h264->subsystem->Compress) |
316 | 0 | return -1; |
317 | | |
318 | 0 | if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight)) |
319 | 0 | return -1; |
320 | | |
321 | 0 | if (!avc444_ensure_buffer(h264, nSrcHeight)) |
322 | 0 | return -1; |
323 | | |
324 | 0 | if (h264->encodingBuffer) |
325 | 0 | { |
326 | 0 | pYUV444Data = h264->pOldYUV444Data; |
327 | 0 | pOldYUV444Data = h264->pYUV444Data; |
328 | 0 | pYUVData = h264->pOldYUVData; |
329 | 0 | pOldYUVData = h264->pYUVData; |
330 | 0 | } |
331 | 0 | else |
332 | 0 | { |
333 | 0 | pYUV444Data = h264->pYUV444Data; |
334 | 0 | pOldYUV444Data = h264->pOldYUV444Data; |
335 | 0 | pYUVData = h264->pYUVData; |
336 | 0 | pOldYUVData = h264->pOldYUVData; |
337 | 0 | } |
338 | 0 | h264->encodingBuffer = !h264->encodingBuffer; |
339 | |
|
340 | 0 | if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride, |
341 | 0 | pYUV444Data, pYUVData, region, 1)) |
342 | 0 | goto fail; |
343 | | |
344 | 0 | if (!detect_changes(h264->firstLumaFrameDone, h264->QP, region, pYUV444Data, pOldYUV444Data, |
345 | 0 | h264->iStride, meta)) |
346 | 0 | goto fail; |
347 | 0 | if (!detect_changes(h264->firstChromaFrameDone, h264->QP, region, pYUVData, pOldYUVData, |
348 | 0 | h264->iStride, auxMeta)) |
349 | 0 | goto fail; |
350 | | |
351 | | /* [MS-RDPEGFX] 2.2.4.5 RFX_AVC444_BITMAP_STREAM |
352 | | * LC: |
353 | | * 0 ... Luma & Chroma |
354 | | * 1 ... Luma |
355 | | * 2 ... Chroma |
356 | | */ |
357 | 0 | if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0)) |
358 | 0 | *op = 0; |
359 | 0 | else if (meta->numRegionRects > 0) |
360 | 0 | *op = 1; |
361 | 0 | else if (auxMeta->numRegionRects > 0) |
362 | 0 | *op = 2; |
363 | 0 | else |
364 | 0 | { |
365 | 0 | WLog_INFO(TAG, "no changes detected for luma or chroma frame"); |
366 | 0 | rc = 0; |
367 | 0 | goto fail; |
368 | 0 | } |
369 | | |
370 | 0 | if ((*op == 0) || (*op == 1)) |
371 | 0 | { |
372 | 0 | const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] }; |
373 | |
|
374 | 0 | if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0) |
375 | 0 | goto fail; |
376 | 0 | h264->firstLumaFrameDone = TRUE; |
377 | 0 | memcpy(h264->lumaData, coded, codedSize); |
378 | 0 | *ppDstData = h264->lumaData; |
379 | 0 | *pDstSize = codedSize; |
380 | 0 | } |
381 | | |
382 | 0 | if ((*op == 0) || (*op == 2)) |
383 | 0 | { |
384 | 0 | const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] }; |
385 | |
|
386 | 0 | if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0) |
387 | 0 | goto fail; |
388 | 0 | h264->firstChromaFrameDone = TRUE; |
389 | 0 | *ppAuxDstData = coded; |
390 | 0 | *pAuxDstSize = codedSize; |
391 | 0 | } |
392 | | |
393 | 0 | rc = 1; |
394 | 0 | fail: |
395 | 0 | if (rc < 0) |
396 | 0 | { |
397 | 0 | free_h264_metablock(meta); |
398 | 0 | free_h264_metablock(auxMeta); |
399 | 0 | } |
400 | 0 | return rc; |
401 | 0 | } |
402 | | |
403 | | static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight) |
404 | 0 | { |
405 | 0 | WINPR_ASSERT(h264); |
406 | | |
407 | 0 | const UINT32* piMainStride = h264->iStride; |
408 | 0 | UINT32* piDstSize = h264->iYUV444Size; |
409 | 0 | UINT32* piDstStride = h264->iYUV444Stride; |
410 | 0 | BYTE** ppYUVDstData = h264->pYUV444Data; |
411 | 0 | BYTE** ppOldYUVDstData = h264->pOldYUV444Data; |
412 | |
|
413 | 0 | nDstHeight = MAX(h264->height, nDstHeight); |
414 | 0 | const UINT32 pad = nDstHeight % 16; |
415 | 0 | UINT32 padDstHeight = nDstHeight; /* Need alignment to 16x16 blocks */ |
416 | |
|
417 | 0 | if (pad != 0) |
418 | 0 | padDstHeight += 16 - pad; |
419 | |
|
420 | 0 | if ((piMainStride[0] != piDstStride[0]) || |
421 | 0 | (piDstSize[0] != 1ull * piMainStride[0] * padDstHeight)) |
422 | 0 | { |
423 | 0 | for (UINT32 x = 0; x < 3; x++) |
424 | 0 | { |
425 | 0 | piDstStride[x] = piMainStride[0]; |
426 | 0 | piDstSize[x] = piDstStride[x] * padDstHeight; |
427 | |
|
428 | 0 | if (piDstSize[x] == 0) |
429 | 0 | return FALSE; |
430 | | |
431 | 0 | BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16); |
432 | 0 | if (!tmp1) |
433 | 0 | return FALSE; |
434 | 0 | ppYUVDstData[x] = tmp1; |
435 | 0 | BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16); |
436 | 0 | if (!tmp2) |
437 | 0 | return FALSE; |
438 | 0 | ppOldYUVDstData[x] = tmp2; |
439 | 0 | } |
440 | | |
441 | 0 | { |
442 | 0 | BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16); |
443 | 0 | if (!tmp) |
444 | 0 | goto fail; |
445 | 0 | h264->lumaData = tmp; |
446 | 0 | } |
447 | 0 | } |
448 | | |
449 | 0 | for (UINT32 x = 0; x < 3; x++) |
450 | 0 | { |
451 | 0 | if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0)) |
452 | 0 | { |
453 | 0 | WLog_Print(h264->log, WLOG_ERROR, |
454 | 0 | "YUV buffer not initialized! check your decoder settings"); |
455 | 0 | goto fail; |
456 | 0 | } |
457 | 0 | } |
458 | | |
459 | 0 | if (!h264->lumaData) |
460 | 0 | goto fail; |
461 | | |
462 | 0 | return TRUE; |
463 | 0 | fail: |
464 | 0 | return FALSE; |
465 | 0 | } |
466 | | |
467 | | static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, |
468 | | BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep, |
469 | | UINT32 nDstWidth, UINT32 nDstHeight, const RECTANGLE_16* rects, |
470 | | UINT32 nrRects, avc444_frame_type type) |
471 | 0 | { |
472 | 0 | const BYTE* pYUVData[3]; |
473 | 0 | BYTE* pYUVDstData[3]; |
474 | 0 | UINT32* piDstStride = h264->iYUV444Stride; |
475 | 0 | BYTE** ppYUVDstData = h264->pYUV444Data; |
476 | 0 | const UINT32* piStride = h264->iStride; |
477 | |
|
478 | 0 | if (h264->subsystem->Decompress(h264, pSrcData, SrcSize) < 0) |
479 | 0 | return FALSE; |
480 | | |
481 | 0 | pYUVData[0] = h264->pYUVData[0]; |
482 | 0 | pYUVData[1] = h264->pYUVData[1]; |
483 | 0 | pYUVData[2] = h264->pYUVData[2]; |
484 | 0 | if (!avc444_ensure_buffer(h264, nDstHeight)) |
485 | 0 | return FALSE; |
486 | | |
487 | 0 | pYUVDstData[0] = ppYUVDstData[0]; |
488 | 0 | pYUVDstData[1] = ppYUVDstData[1]; |
489 | 0 | pYUVDstData[2] = ppYUVDstData[2]; |
490 | 0 | if (!yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->height, pYUVDstData, |
491 | 0 | piDstStride, DstFormat, pDstData, nDstStep, rects, nrRects)) |
492 | 0 | return FALSE; |
493 | | |
494 | 0 | return TRUE; |
495 | 0 | } |
496 | | |
497 | | #if defined(AVC444_FRAME_STAT) |
498 | | static UINT64 op1 = 0; |
499 | | static double op1sum = 0; |
500 | | static UINT64 op2 = 0; |
501 | | static double op2sum = 0; |
502 | | static UINT64 op3 = 0; |
503 | | static double op3sum = 0; |
504 | | static double avg(UINT64* count, double old, double size) |
505 | | { |
506 | | double tmp = size + *count * old; |
507 | | (*count)++; |
508 | | tmp = tmp / *count; |
509 | | return tmp; |
510 | | } |
511 | | #endif |
512 | | |
513 | | INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op, const RECTANGLE_16* regionRects, |
514 | | UINT32 numRegionRects, const BYTE* pSrcData, UINT32 SrcSize, |
515 | | const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect, |
516 | | const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat, |
517 | | UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId) |
518 | 0 | { |
519 | 0 | INT32 status = -1; |
520 | 0 | avc444_frame_type chroma = |
521 | 0 | (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2; |
522 | |
|
523 | 0 | if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor) |
524 | 0 | return -1001; |
525 | | |
526 | 0 | switch (op) |
527 | 0 | { |
528 | 0 | case 0: /* YUV420 in stream 1 |
529 | | * Chroma420 in stream 2 */ |
530 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
531 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, |
532 | 0 | AVC444_LUMA)) |
533 | 0 | status = -1; |
534 | 0 | else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat, |
535 | 0 | nDstStep, nDstWidth, nDstHeight, auxRegionRects, |
536 | 0 | numAuxRegionRect, chroma)) |
537 | 0 | status = -1; |
538 | 0 | else |
539 | 0 | status = 0; |
540 | |
|
541 | 0 | break; |
542 | | |
543 | 0 | case 2: /* Chroma420 in stream 1 */ |
544 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
545 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, chroma)) |
546 | 0 | status = -1; |
547 | 0 | else |
548 | 0 | status = 0; |
549 | |
|
550 | 0 | break; |
551 | | |
552 | 0 | case 1: /* YUV420 in stream 1 */ |
553 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
554 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, |
555 | 0 | AVC444_LUMA)) |
556 | 0 | status = -1; |
557 | 0 | else |
558 | 0 | status = 0; |
559 | |
|
560 | 0 | break; |
561 | | |
562 | 0 | default: /* WTF? */ |
563 | 0 | break; |
564 | 0 | } |
565 | | |
566 | | #if defined(AVC444_FRAME_STAT) |
567 | | |
568 | | switch (op) |
569 | | { |
570 | | case 0: |
571 | | op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize); |
572 | | break; |
573 | | |
574 | | case 1: |
575 | | op2sum = avg(&op2, op2sum, SrcSize); |
576 | | break; |
577 | | |
578 | | case 2: |
579 | | op3sum = avg(&op3, op3sum, SrcSize); |
580 | | break; |
581 | | |
582 | | default: |
583 | | break; |
584 | | } |
585 | | |
586 | | WLog_Print(h264->log, WLOG_INFO, |
587 | | "luma=%" PRIu64 " [avg=%lf] chroma=%" PRIu64 " [avg=%lf] combined=%" PRIu64 |
588 | | " [avg=%lf]", |
589 | | op1, op1sum, op2, op2sum, op3, op3sum); |
590 | | #endif |
591 | 0 | return status; |
592 | 0 | } |
593 | | |
594 | 0 | #define MAX_SUBSYSTEMS 10 |
595 | | static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT; |
596 | | static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = { 0 }; |
597 | | |
598 | | static BOOL CALLBACK h264_register_subsystems(PINIT_ONCE once, PVOID param, PVOID* context) |
599 | 0 | { |
600 | 0 | int i = 0; |
601 | |
|
602 | | #ifdef WITH_MEDIACODEC |
603 | | { |
604 | | subSystems[i] = &g_Subsystem_mediacodec; |
605 | | i++; |
606 | | } |
607 | | #endif |
608 | | #if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION) |
609 | | { |
610 | | subSystems[i] = &g_Subsystem_MF; |
611 | | i++; |
612 | | } |
613 | | #endif |
614 | | #ifdef WITH_OPENH264 |
615 | | { |
616 | | subSystems[i] = &g_Subsystem_OpenH264; |
617 | | i++; |
618 | | } |
619 | | #endif |
620 | | #ifdef WITH_VIDEO_FFMPEG |
621 | | { |
622 | | subSystems[i] = &g_Subsystem_libavcodec; |
623 | | i++; |
624 | | } |
625 | | #endif |
626 | 0 | return i > 0; |
627 | 0 | } |
628 | | |
629 | | static BOOL h264_context_init(H264_CONTEXT* h264) |
630 | 0 | { |
631 | 0 | if (!h264) |
632 | 0 | return FALSE; |
633 | | |
634 | 0 | h264->log = WLog_Get(TAG); |
635 | |
|
636 | 0 | if (!h264->log) |
637 | 0 | return FALSE; |
638 | | |
639 | 0 | h264->subsystem = NULL; |
640 | 0 | InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems, NULL, NULL); |
641 | |
|
642 | 0 | for (size_t i = 0; i < MAX_SUBSYSTEMS; i++) |
643 | 0 | { |
644 | 0 | const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i]; |
645 | |
|
646 | 0 | if (!subsystem || !subsystem->Init) |
647 | 0 | break; |
648 | | |
649 | 0 | if (subsystem->Init(h264)) |
650 | 0 | { |
651 | 0 | h264->subsystem = subsystem; |
652 | 0 | return TRUE; |
653 | 0 | } |
654 | 0 | } |
655 | | |
656 | 0 | return FALSE; |
657 | 0 | } |
658 | | |
659 | | BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height) |
660 | 0 | { |
661 | 0 | if (!h264) |
662 | 0 | return FALSE; |
663 | | |
664 | 0 | h264->width = width; |
665 | 0 | h264->height = height; |
666 | 0 | return yuv_context_reset(h264->yuv, width, height); |
667 | 0 | } |
668 | | |
669 | | H264_CONTEXT* h264_context_new(BOOL Compressor) |
670 | 0 | { |
671 | 0 | H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1, sizeof(H264_CONTEXT)); |
672 | 0 | if (!h264) |
673 | 0 | return NULL; |
674 | | |
675 | 0 | h264->Compressor = Compressor; |
676 | 0 | if (Compressor) |
677 | | |
678 | 0 | { |
679 | | /* Default compressor settings, may be changed by caller */ |
680 | 0 | h264->BitRate = 1000000; |
681 | 0 | h264->FrameRate = 30; |
682 | 0 | } |
683 | |
|
684 | 0 | if (!h264_context_init(h264)) |
685 | 0 | goto fail; |
686 | | |
687 | 0 | h264->yuv = yuv_context_new(Compressor, 0); |
688 | 0 | if (!h264->yuv) |
689 | 0 | goto fail; |
690 | | |
691 | 0 | return h264; |
692 | | |
693 | 0 | fail: |
694 | 0 | WINPR_PRAGMA_DIAG_PUSH |
695 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
696 | 0 | h264_context_free(h264); |
697 | 0 | WINPR_PRAGMA_DIAG_POP |
698 | 0 | return NULL; |
699 | 0 | } |
700 | | |
701 | | void h264_context_free(H264_CONTEXT* h264) |
702 | 0 | { |
703 | 0 | if (h264) |
704 | 0 | { |
705 | 0 | if (h264->subsystem) |
706 | 0 | h264->subsystem->Uninit(h264); |
707 | |
|
708 | 0 | for (size_t x = 0; x < 3; x++) |
709 | 0 | { |
710 | 0 | if (h264->Compressor) |
711 | 0 | { |
712 | 0 | winpr_aligned_free(h264->pYUVData[x]); |
713 | 0 | winpr_aligned_free(h264->pOldYUVData[x]); |
714 | 0 | } |
715 | 0 | winpr_aligned_free(h264->pYUV444Data[x]); |
716 | 0 | winpr_aligned_free(h264->pOldYUV444Data[x]); |
717 | 0 | } |
718 | 0 | winpr_aligned_free(h264->lumaData); |
719 | |
|
720 | 0 | yuv_context_free(h264->yuv); |
721 | 0 | free(h264); |
722 | 0 | } |
723 | 0 | } |
724 | | |
725 | | void free_h264_metablock(RDPGFX_H264_METABLOCK* meta) |
726 | 0 | { |
727 | 0 | RDPGFX_H264_METABLOCK m = { 0 }; |
728 | 0 | if (!meta) |
729 | 0 | return; |
730 | 0 | free(meta->quantQualityVals); |
731 | 0 | free(meta->regionRects); |
732 | 0 | *meta = m; |
733 | 0 | } |
734 | | |
735 | | BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value) |
736 | 0 | { |
737 | 0 | WINPR_ASSERT(h264); |
738 | 0 | switch (option) |
739 | 0 | { |
740 | 0 | case H264_CONTEXT_OPTION_BITRATE: |
741 | 0 | h264->BitRate = value; |
742 | 0 | return TRUE; |
743 | 0 | case H264_CONTEXT_OPTION_FRAMERATE: |
744 | 0 | h264->FrameRate = value; |
745 | 0 | return TRUE; |
746 | 0 | case H264_CONTEXT_OPTION_RATECONTROL: |
747 | 0 | h264->RateControlMode = value; |
748 | 0 | return TRUE; |
749 | 0 | case H264_CONTEXT_OPTION_QP: |
750 | 0 | h264->QP = value; |
751 | 0 | return TRUE; |
752 | 0 | default: |
753 | 0 | WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]", |
754 | 0 | option); |
755 | 0 | return FALSE; |
756 | 0 | } |
757 | 0 | } |
758 | | |
759 | | UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option) |
760 | 0 | { |
761 | 0 | WINPR_ASSERT(h264); |
762 | 0 | switch (option) |
763 | 0 | { |
764 | 0 | case H264_CONTEXT_OPTION_BITRATE: |
765 | 0 | return h264->BitRate; |
766 | 0 | case H264_CONTEXT_OPTION_FRAMERATE: |
767 | 0 | return h264->FrameRate; |
768 | 0 | case H264_CONTEXT_OPTION_RATECONTROL: |
769 | 0 | return h264->RateControlMode; |
770 | 0 | case H264_CONTEXT_OPTION_QP: |
771 | 0 | return h264->QP; |
772 | 0 | default: |
773 | 0 | WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]", |
774 | 0 | option); |
775 | 0 | return 0; |
776 | 0 | } |
777 | 0 | } |