/src/FreeRDP/libfreerdp/codec/h264.c
Line | Count | Source |
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 | | #include <freerdp/codec/region.h> |
34 | | |
35 | | #include "h264.h" |
36 | | |
37 | 0 | #define TAG FREERDP_TAG("codec") |
38 | | |
39 | | static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight); |
40 | | |
41 | | static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height) |
42 | 0 | { |
43 | 0 | BOOL isNull = FALSE; |
44 | 0 | UINT32 pheight = height; |
45 | |
|
46 | 0 | if (!h264) |
47 | 0 | return FALSE; |
48 | | |
49 | 0 | if (stride == 0) |
50 | 0 | stride = width; |
51 | |
|
52 | 0 | stride += 16 - stride % 16; |
53 | 0 | pheight += 16 - pheight % 16; |
54 | |
|
55 | 0 | const size_t nPlanes = h264->hwAccel ? 2 : 3; |
56 | |
|
57 | 0 | for (size_t x = 0; x < nPlanes; 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 | if (h264->hwAccel) /* NV12 */ |
72 | 0 | { |
73 | 0 | h264->iStride[0] = stride; |
74 | 0 | h264->iStride[1] = stride; |
75 | 0 | h264->iStride[2] = 0; |
76 | 0 | } |
77 | 0 | else /* I420 */ |
78 | 0 | { |
79 | 0 | h264->iStride[0] = stride; |
80 | 0 | h264->iStride[1] = (stride + 1) / 2; |
81 | 0 | h264->iStride[2] = (stride + 1) / 2; |
82 | 0 | } |
83 | |
|
84 | 0 | for (size_t x = 0; x < nPlanes; x++) |
85 | 0 | { |
86 | 0 | BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], h264->iStride[x], pheight, 16); |
87 | 0 | BYTE* tmp2 = |
88 | 0 | winpr_aligned_recalloc(h264->pOldYUVData[x], h264->iStride[x], pheight, 16); |
89 | 0 | if (tmp1) |
90 | 0 | h264->pYUVData[x] = tmp1; |
91 | 0 | if (tmp2) |
92 | 0 | h264->pOldYUVData[x] = tmp2; |
93 | 0 | if (!tmp1 || !tmp2) |
94 | 0 | return FALSE; |
95 | 0 | } |
96 | 0 | h264->width = width; |
97 | 0 | h264->height = height; |
98 | 0 | } |
99 | | |
100 | 0 | return TRUE; |
101 | 0 | } |
102 | | |
103 | | BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height) |
104 | 0 | { |
105 | 0 | return yuv_ensure_buffer(h264, stride, width, height); |
106 | 0 | } |
107 | | |
108 | | static BOOL isRectValid(UINT32 width, UINT32 height, const RECTANGLE_16* rect) |
109 | 0 | { |
110 | 0 | WINPR_ASSERT(rect); |
111 | 0 | if (rect->left > width) |
112 | 0 | return FALSE; |
113 | 0 | if (rect->right > width) |
114 | 0 | return FALSE; |
115 | 0 | if (rect->left >= rect->right) |
116 | 0 | return FALSE; |
117 | 0 | if (rect->top > height) |
118 | 0 | return FALSE; |
119 | 0 | if (rect->bottom > height) |
120 | 0 | return FALSE; |
121 | 0 | if (rect->top >= rect->bottom) |
122 | 0 | return FALSE; |
123 | 0 | return TRUE; |
124 | 0 | } |
125 | | |
126 | | static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height, const RECTANGLE_16* rects, |
127 | | UINT32 count) |
128 | 0 | { |
129 | 0 | WINPR_ASSERT(rects || (count == 0)); |
130 | 0 | for (size_t x = 0; x < count; x++) |
131 | 0 | { |
132 | 0 | const RECTANGLE_16* rect = &rects[x]; |
133 | 0 | if (!isRectValid(width, height, rect)) |
134 | 0 | { |
135 | 0 | char buffer[64] = WINPR_C_ARRAY_INIT; |
136 | 0 | WLog_Print(log, WLOG_WARN, |
137 | 0 | "Rectangle %" PRIuz " %s outside of bounding frame %" PRIu32 "x%" PRIu32, x, |
138 | 0 | rectangle_to_string(rect, buffer, sizeof(buffer)), width, height); |
139 | 0 | return FALSE; |
140 | 0 | } |
141 | 0 | } |
142 | 0 | return TRUE; |
143 | 0 | } |
144 | | |
145 | | static int log_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, |
146 | | const RECTANGLE_16* rects, UINT32 nrRects) |
147 | 0 | { |
148 | 0 | WINPR_ASSERT(h264); |
149 | 0 | WINPR_ASSERT(h264->subsystem); |
150 | 0 | WINPR_ASSERT(h264->subsystem->Decompress); |
151 | |
|
152 | 0 | const int status = h264->subsystem->Decompress(h264, pSrcData, SrcSize); |
153 | 0 | if (status < 0) |
154 | 0 | { |
155 | 0 | WLog_Print(h264->log, WLOG_WARN, "H264 decompress failed with %d", status); |
156 | 0 | return status; |
157 | 0 | } |
158 | | |
159 | | /* some server implementations (krdc) use H264 frames smaller than the surface sizes, |
160 | | * validate the regions against this size as well */ |
161 | 0 | if (!areRectsValid(h264->log, h264->YUVWidth, h264->YUVHeight, rects, nrRects)) |
162 | 0 | return -1015; |
163 | 0 | if (!areRectsValid(h264->log, h264->width, h264->height, rects, nrRects)) |
164 | 0 | return -1016; |
165 | 0 | return status; |
166 | 0 | } |
167 | | |
168 | | INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData, |
169 | | DWORD DstFormat, UINT32 nDstStep, WINPR_ATTR_UNUSED UINT32 nDstWidth, |
170 | | WINPR_ATTR_UNUSED UINT32 nDstHeight, const RECTANGLE_16* regionRects, |
171 | | UINT32 numRegionRects) |
172 | 0 | { |
173 | 0 | int status = 0; |
174 | 0 | const BYTE* pYUVData[3]; |
175 | |
|
176 | 0 | if (!h264 || h264->Compressor) |
177 | 0 | return -1001; |
178 | | |
179 | 0 | if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects)) |
180 | 0 | return -1013; |
181 | | |
182 | 0 | status = log_decompress(h264, pSrcData, SrcSize, regionRects, numRegionRects); |
183 | |
|
184 | 0 | if (status == 0) |
185 | 0 | return 1; |
186 | | |
187 | 0 | if (status < 0) |
188 | 0 | return status; |
189 | | |
190 | 0 | pYUVData[0] = h264->pYUVData[0]; |
191 | 0 | pYUVData[1] = h264->pYUVData[1]; |
192 | 0 | pYUVData[2] = h264->pYUVData[2]; |
193 | 0 | if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->YUVHeight, DstFormat, |
194 | 0 | pDstData, nDstStep, regionRects, numRegionRects)) |
195 | 0 | return -1002; |
196 | | |
197 | 0 | return 1; |
198 | 0 | } |
199 | | |
200 | | static BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles, |
201 | | RDPGFX_H264_METABLOCK* meta, size_t count) |
202 | 0 | { |
203 | | /* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */ |
204 | 0 | if (!meta || (QP > UINT8_MAX)) |
205 | 0 | { |
206 | 0 | free(rectangles); |
207 | 0 | return FALSE; |
208 | 0 | } |
209 | | |
210 | 0 | meta->regionRects = rectangles; |
211 | 0 | if (count == 0) |
212 | 0 | return TRUE; |
213 | | |
214 | 0 | if (count > UINT32_MAX) |
215 | 0 | return FALSE; |
216 | | |
217 | 0 | meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY)); |
218 | |
|
219 | 0 | if (!meta->quantQualityVals || !meta->regionRects) |
220 | 0 | return FALSE; |
221 | 0 | meta->numRegionRects = (UINT32)count; |
222 | 0 | for (size_t x = 0; x < count; x++) |
223 | 0 | { |
224 | 0 | RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x]; |
225 | 0 | cur->qp = (UINT8)QP; |
226 | | |
227 | | /* qpVal bit 6 and 7 are flags, so mask them out here. |
228 | | * qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */ |
229 | 0 | cur->qualityVal = 100 - (QP & 0x3F); |
230 | 0 | } |
231 | 0 | return TRUE; |
232 | 0 | } |
233 | | |
234 | | static inline BOOL diff_tile(const RECTANGLE_16* regionRect, BYTE* pYUVData[3], |
235 | | BYTE* pOldYUVData[3], UINT32 const iStride[3]) |
236 | 0 | { |
237 | 0 | size_t size = 0; |
238 | |
|
239 | 0 | if (!regionRect || !pYUVData || !pOldYUVData || !iStride) |
240 | 0 | return FALSE; |
241 | 0 | size = regionRect->right - regionRect->left; |
242 | 0 | if (regionRect->right > iStride[0]) |
243 | 0 | return FALSE; |
244 | 0 | if (regionRect->right / 2u > iStride[1]) |
245 | 0 | return FALSE; |
246 | 0 | if (regionRect->right / 2u > iStride[2]) |
247 | 0 | return FALSE; |
248 | | |
249 | 0 | for (size_t y = regionRect->top; y < regionRect->bottom; y++) |
250 | 0 | { |
251 | 0 | const BYTE* cur0 = &pYUVData[0][y * iStride[0]]; |
252 | 0 | const BYTE* cur1 = &pYUVData[1][y * iStride[1]]; |
253 | 0 | const BYTE* cur2 = &pYUVData[2][y * iStride[2]]; |
254 | 0 | const BYTE* old0 = &pOldYUVData[0][y * iStride[0]]; |
255 | 0 | const BYTE* old1 = &pOldYUVData[1][y * iStride[1]]; |
256 | 0 | const BYTE* old2 = &pOldYUVData[2][y * iStride[2]]; |
257 | |
|
258 | 0 | if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0) |
259 | 0 | return TRUE; |
260 | 0 | if (memcmp(&cur1[regionRect->left / 2], &old1[regionRect->left / 2], size / 2) != 0) |
261 | 0 | return TRUE; |
262 | 0 | if (memcmp(&cur2[regionRect->left / 2], &old2[regionRect->left / 2], size / 2) != 0) |
263 | 0 | return TRUE; |
264 | 0 | } |
265 | 0 | return FALSE; |
266 | 0 | } |
267 | | |
268 | | static BOOL detect_changes(BOOL firstFrameDone, const UINT32 QP, const RECTANGLE_16* regionRect, |
269 | | BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32 const iStride[3], |
270 | | RDPGFX_H264_METABLOCK* meta) |
271 | 0 | { |
272 | 0 | size_t count = 0; |
273 | 0 | size_t wc = 0; |
274 | 0 | size_t hc = 0; |
275 | 0 | RECTANGLE_16* rectangles = nullptr; |
276 | |
|
277 | 0 | if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta) |
278 | 0 | return FALSE; |
279 | | |
280 | 0 | wc = (regionRect->right - regionRect->left) / 64 + 1; |
281 | 0 | hc = (regionRect->bottom - regionRect->top) / 64 + 1; |
282 | 0 | rectangles = calloc(wc * hc, sizeof(RECTANGLE_16)); |
283 | 0 | if (!rectangles) |
284 | 0 | return FALSE; |
285 | 0 | if (!firstFrameDone) |
286 | 0 | { |
287 | 0 | rectangles[0] = *regionRect; |
288 | 0 | count = 1; |
289 | 0 | } |
290 | 0 | else |
291 | 0 | { |
292 | 0 | for (size_t y = 0; y < regionRect->bottom - regionRect->top; y += 64) |
293 | 0 | { |
294 | 0 | for (size_t x = 0; x < regionRect->right - regionRect->left; x += 64) |
295 | 0 | { |
296 | 0 | RECTANGLE_16 rect; |
297 | 0 | rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x); |
298 | 0 | rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y); |
299 | 0 | rect.right = |
300 | 0 | (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right)); |
301 | 0 | rect.bottom = |
302 | 0 | (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom)); |
303 | 0 | if (diff_tile(&rect, pYUVData, pOldYUVData, iStride)) |
304 | 0 | rectangles[count++] = rect; |
305 | 0 | } |
306 | 0 | } |
307 | 0 | } |
308 | 0 | if (!allocate_h264_metablock(QP, rectangles, meta, count)) |
309 | 0 | return FALSE; |
310 | 0 | return TRUE; |
311 | 0 | } |
312 | | |
313 | | INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth, |
314 | | UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3]) |
315 | 0 | { |
316 | 0 | if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress) |
317 | 0 | return -1; |
318 | | |
319 | 0 | if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight)) |
320 | 0 | return -1; |
321 | | |
322 | 0 | for (size_t x = 0; x < 3; x++) |
323 | 0 | { |
324 | 0 | YUVData[x] = h264->pYUVData[x]; |
325 | 0 | stride[x] = h264->iStride[x]; |
326 | 0 | } |
327 | |
|
328 | 0 | return 0; |
329 | 0 | } |
330 | | |
331 | | INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize) |
332 | 0 | { |
333 | 0 | if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress) |
334 | 0 | return -1; |
335 | | |
336 | 0 | const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] }; |
337 | |
|
338 | 0 | return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize); |
339 | 0 | } |
340 | | |
341 | | INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, |
342 | | UINT32 nSrcWidth, UINT32 nSrcHeight, const RECTANGLE_16* regionRect, |
343 | | BYTE** ppDstData, UINT32* pDstSize, RDPGFX_H264_METABLOCK* meta) |
344 | 0 | { |
345 | 0 | INT32 rc = -1; |
346 | 0 | BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT; |
347 | 0 | const BYTE* pcYUVData[3] = WINPR_C_ARRAY_INIT; |
348 | 0 | BYTE* pOldYUVData[3] = WINPR_C_ARRAY_INIT; |
349 | |
|
350 | 0 | if (!h264 || !regionRect || !meta || !h264->Compressor) |
351 | 0 | return -1; |
352 | | |
353 | 0 | if (!h264->subsystem->Compress) |
354 | 0 | return -1; |
355 | | |
356 | 0 | if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight)) |
357 | 0 | return -1; |
358 | | |
359 | 0 | if (h264->encodingBuffer) |
360 | 0 | { |
361 | 0 | for (size_t x = 0; x < 3; x++) |
362 | 0 | { |
363 | 0 | pYUVData[x] = h264->pYUVData[x]; |
364 | 0 | pOldYUVData[x] = h264->pOldYUVData[x]; |
365 | 0 | } |
366 | 0 | } |
367 | 0 | else |
368 | 0 | { |
369 | 0 | for (size_t x = 0; x < 3; x++) |
370 | 0 | { |
371 | 0 | pYUVData[x] = h264->pOldYUVData[x]; |
372 | 0 | pOldYUVData[x] = h264->pYUVData[x]; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | h264->encodingBuffer = !h264->encodingBuffer; |
376 | |
|
377 | 0 | if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData, |
378 | 0 | regionRect, 1)) |
379 | 0 | goto fail; |
380 | | |
381 | 0 | if (!detect_changes(h264->firstLumaFrameDone, h264->QP, regionRect, pYUVData, pOldYUVData, |
382 | 0 | h264->iStride, meta)) |
383 | 0 | goto fail; |
384 | | |
385 | 0 | if (meta->numRegionRects == 0) |
386 | 0 | { |
387 | 0 | rc = 0; |
388 | 0 | goto fail; |
389 | 0 | } |
390 | | |
391 | 0 | for (size_t x = 0; x < 3; x++) |
392 | 0 | pcYUVData[x] = pYUVData[x]; |
393 | |
|
394 | 0 | rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize); |
395 | 0 | if (rc >= 0) |
396 | 0 | h264->firstLumaFrameDone = TRUE; |
397 | |
|
398 | 0 | fail: |
399 | 0 | if (rc < 0) |
400 | 0 | free_h264_metablock(meta); |
401 | 0 | return rc; |
402 | 0 | } |
403 | | |
404 | | INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, |
405 | | UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version, const RECTANGLE_16* region, |
406 | | BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData, |
407 | | UINT32* pAuxDstSize, RDPGFX_H264_METABLOCK* meta, |
408 | | RDPGFX_H264_METABLOCK* auxMeta) |
409 | 0 | { |
410 | 0 | int rc = -1; |
411 | 0 | BYTE* coded = nullptr; |
412 | 0 | UINT32 codedSize = 0; |
413 | 0 | BYTE** pYUV444Data = nullptr; |
414 | 0 | BYTE** pOldYUV444Data = nullptr; |
415 | 0 | BYTE** pYUVData = nullptr; |
416 | 0 | BYTE** pOldYUVData = nullptr; |
417 | |
|
418 | 0 | if (!h264 || !h264->Compressor) |
419 | 0 | return -1; |
420 | | |
421 | 0 | if (!h264->subsystem->Compress) |
422 | 0 | return -1; |
423 | | |
424 | 0 | if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight)) |
425 | 0 | return -1; |
426 | | |
427 | 0 | if (!avc444_ensure_buffer(h264, nSrcHeight)) |
428 | 0 | return -1; |
429 | | |
430 | 0 | if (h264->encodingBuffer) |
431 | 0 | { |
432 | 0 | pYUV444Data = h264->pOldYUV444Data; |
433 | 0 | pOldYUV444Data = h264->pYUV444Data; |
434 | 0 | pYUVData = h264->pOldYUVData; |
435 | 0 | pOldYUVData = h264->pYUVData; |
436 | 0 | } |
437 | 0 | else |
438 | 0 | { |
439 | 0 | pYUV444Data = h264->pYUV444Data; |
440 | 0 | pOldYUV444Data = h264->pOldYUV444Data; |
441 | 0 | pYUVData = h264->pYUVData; |
442 | 0 | pOldYUVData = h264->pOldYUVData; |
443 | 0 | } |
444 | 0 | h264->encodingBuffer = !h264->encodingBuffer; |
445 | |
|
446 | 0 | if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride, |
447 | 0 | pYUV444Data, pYUVData, region, 1)) |
448 | 0 | goto fail; |
449 | | |
450 | 0 | if (!detect_changes(h264->firstLumaFrameDone, h264->QP, region, pYUV444Data, pOldYUV444Data, |
451 | 0 | h264->iStride, meta)) |
452 | 0 | goto fail; |
453 | 0 | if (!detect_changes(h264->firstChromaFrameDone, h264->QP, region, pYUVData, pOldYUVData, |
454 | 0 | h264->iStride, auxMeta)) |
455 | 0 | goto fail; |
456 | | |
457 | | /* [MS-RDPEGFX] 2.2.4.5 RFX_AVC444_BITMAP_STREAM |
458 | | * LC: |
459 | | * 0 ... Luma & Chroma |
460 | | * 1 ... Luma |
461 | | * 2 ... Chroma |
462 | | */ |
463 | 0 | if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0)) |
464 | 0 | *op = 0; |
465 | 0 | else if (meta->numRegionRects > 0) |
466 | 0 | *op = 1; |
467 | 0 | else if (auxMeta->numRegionRects > 0) |
468 | 0 | *op = 2; |
469 | 0 | else |
470 | 0 | { |
471 | 0 | WLog_Print(h264->log, WLOG_TRACE, "no changes detected for luma or chroma frame"); |
472 | 0 | rc = 0; |
473 | 0 | goto fail; |
474 | 0 | } |
475 | | |
476 | 0 | if ((*op == 0) || (*op == 1)) |
477 | 0 | { |
478 | 0 | const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] }; |
479 | |
|
480 | 0 | if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0) |
481 | 0 | goto fail; |
482 | 0 | h264->firstLumaFrameDone = TRUE; |
483 | 0 | memcpy(h264->lumaData, coded, codedSize); |
484 | 0 | *ppDstData = h264->lumaData; |
485 | 0 | *pDstSize = codedSize; |
486 | 0 | } |
487 | | |
488 | 0 | if ((*op == 0) || (*op == 2)) |
489 | 0 | { |
490 | 0 | const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] }; |
491 | |
|
492 | 0 | if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0) |
493 | 0 | goto fail; |
494 | 0 | h264->firstChromaFrameDone = TRUE; |
495 | 0 | *ppAuxDstData = coded; |
496 | 0 | *pAuxDstSize = codedSize; |
497 | 0 | } |
498 | | |
499 | 0 | rc = 1; |
500 | 0 | fail: |
501 | 0 | if (rc < 0) |
502 | 0 | { |
503 | 0 | free_h264_metablock(meta); |
504 | 0 | free_h264_metablock(auxMeta); |
505 | 0 | } |
506 | 0 | return rc; |
507 | 0 | } |
508 | | |
509 | | static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight) |
510 | 0 | { |
511 | 0 | WINPR_ASSERT(h264); |
512 | |
|
513 | 0 | const UINT32* piMainStride = h264->iStride; |
514 | 0 | UINT32* piDstSize = h264->iYUV444Size; |
515 | 0 | UINT32* piDstStride = h264->iYUV444Stride; |
516 | 0 | BYTE** ppYUVDstData = h264->pYUV444Data; |
517 | 0 | BYTE** ppOldYUVDstData = h264->pOldYUV444Data; |
518 | |
|
519 | 0 | nDstHeight = MAX(h264->height, nDstHeight); |
520 | 0 | const UINT32 pad = nDstHeight % 16; |
521 | 0 | UINT32 padDstHeight = nDstHeight; /* Need alignment to 16x16 blocks */ |
522 | |
|
523 | 0 | if (pad != 0) |
524 | 0 | padDstHeight += 16 - pad; |
525 | |
|
526 | 0 | if ((piMainStride[0] == 0) || (padDstHeight == 0)) |
527 | 0 | return FALSE; |
528 | | |
529 | 0 | const uint64_t dstsize = 1ull * piMainStride[0] * padDstHeight; |
530 | 0 | if (dstsize > UINT32_MAX) |
531 | 0 | return FALSE; |
532 | | |
533 | 0 | if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != dstsize)) |
534 | 0 | { |
535 | 0 | for (UINT32 x = 0; x < 3; x++) |
536 | 0 | { |
537 | 0 | piDstStride[x] = piMainStride[0]; |
538 | |
|
539 | 0 | const uint64_t dstride = 1ull * piDstStride[x] * padDstHeight; |
540 | 0 | if (dstride > UINT32_MAX) |
541 | 0 | return FALSE; |
542 | | |
543 | 0 | piDstSize[x] = WINPR_ASSERTING_INT_CAST(UINT32, dstride); |
544 | 0 | if (piDstSize[x] == 0) |
545 | 0 | return FALSE; |
546 | | |
547 | 0 | BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], piDstSize[x], 1, 16); |
548 | 0 | if (!tmp1) |
549 | 0 | return FALSE; |
550 | 0 | ppYUVDstData[x] = tmp1; |
551 | 0 | BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], piDstSize[x], 1, 16); |
552 | 0 | if (!tmp2) |
553 | 0 | return FALSE; |
554 | 0 | ppOldYUVDstData[x] = tmp2; |
555 | 0 | } |
556 | | |
557 | 0 | { |
558 | 0 | BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, piDstSize[0], 4, 16); |
559 | 0 | if (!tmp) |
560 | 0 | goto fail; |
561 | 0 | h264->lumaData = tmp; |
562 | 0 | } |
563 | 0 | } |
564 | | |
565 | 0 | for (UINT32 x = 0; x < 3; x++) |
566 | 0 | { |
567 | 0 | if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0)) |
568 | 0 | { |
569 | 0 | WLog_Print(h264->log, WLOG_ERROR, |
570 | 0 | "YUV buffer not initialized! check your decoder settings"); |
571 | 0 | goto fail; |
572 | 0 | } |
573 | 0 | } |
574 | | |
575 | 0 | if (!h264->lumaData) |
576 | 0 | goto fail; |
577 | | |
578 | 0 | return TRUE; |
579 | 0 | fail: |
580 | 0 | return FALSE; |
581 | 0 | } |
582 | | |
583 | | static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, |
584 | | BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep, |
585 | | WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight, |
586 | | const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type) |
587 | 0 | { |
588 | 0 | const BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT; |
589 | 0 | BYTE* pYUVDstData[3] = WINPR_C_ARRAY_INIT; |
590 | 0 | UINT32* piDstStride = h264->iYUV444Stride; |
591 | 0 | BYTE** ppYUVDstData = h264->pYUV444Data; |
592 | 0 | const UINT32* piStride = h264->iStride; |
593 | |
|
594 | 0 | if (log_decompress(h264, pSrcData, SrcSize, rects, nrRects) < 0) |
595 | 0 | return FALSE; |
596 | | |
597 | 0 | pYUVData[0] = h264->pYUVData[0]; |
598 | 0 | pYUVData[1] = h264->pYUVData[1]; |
599 | 0 | pYUVData[2] = h264->pYUVData[2]; |
600 | 0 | if (!avc444_ensure_buffer(h264, nDstHeight)) |
601 | 0 | return FALSE; |
602 | | |
603 | 0 | pYUVDstData[0] = ppYUVDstData[0]; |
604 | 0 | pYUVDstData[1] = ppYUVDstData[1]; |
605 | 0 | pYUVDstData[2] = ppYUVDstData[2]; |
606 | 0 | return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->YUVHeight, |
607 | 0 | pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects, |
608 | 0 | nrRects)); |
609 | 0 | } |
610 | | |
611 | | #if defined(AVC444_FRAME_STAT) |
612 | | static UINT64 op1 = 0; |
613 | | static double op1sum = 0; |
614 | | static UINT64 op2 = 0; |
615 | | static double op2sum = 0; |
616 | | static UINT64 op3 = 0; |
617 | | static double op3sum = 0; |
618 | | static double avg(UINT64* count, double old, double size) |
619 | | { |
620 | | double tmp = size + *count * old; |
621 | | (*count)++; |
622 | | tmp = tmp / *count; |
623 | | return tmp; |
624 | | } |
625 | | #endif |
626 | | |
627 | | INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op, const RECTANGLE_16* regionRects, |
628 | | UINT32 numRegionRects, const BYTE* pSrcData, UINT32 SrcSize, |
629 | | const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect, |
630 | | const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat, |
631 | | UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId) |
632 | 0 | { |
633 | 0 | INT32 status = -1; |
634 | 0 | avc444_frame_type chroma = |
635 | 0 | (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2; |
636 | |
|
637 | 0 | if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor) |
638 | 0 | return -1001; |
639 | | |
640 | 0 | if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects)) |
641 | 0 | return -1013; |
642 | 0 | if (!areRectsValid(h264->log, nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect)) |
643 | 0 | return -1014; |
644 | | |
645 | 0 | switch (op) |
646 | 0 | { |
647 | 0 | case 0: /* YUV420 in stream 1 |
648 | | * Chroma420 in stream 2 */ |
649 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
650 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, |
651 | 0 | AVC444_LUMA)) |
652 | 0 | status = -1; |
653 | 0 | else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat, |
654 | 0 | nDstStep, nDstWidth, nDstHeight, auxRegionRects, |
655 | 0 | numAuxRegionRect, chroma)) |
656 | 0 | status = -1; |
657 | 0 | else |
658 | 0 | status = 0; |
659 | |
|
660 | 0 | break; |
661 | | |
662 | 0 | case 2: /* Chroma420 in stream 1 */ |
663 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
664 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, chroma)) |
665 | 0 | status = -1; |
666 | 0 | else |
667 | 0 | status = 0; |
668 | |
|
669 | 0 | break; |
670 | | |
671 | 0 | case 1: /* YUV420 in stream 1 */ |
672 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
673 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, |
674 | 0 | AVC444_LUMA)) |
675 | 0 | status = -1; |
676 | 0 | else |
677 | 0 | status = 0; |
678 | |
|
679 | 0 | break; |
680 | | |
681 | 0 | default: /* WTF? */ |
682 | 0 | break; |
683 | 0 | } |
684 | | |
685 | | #if defined(AVC444_FRAME_STAT) |
686 | | |
687 | | switch (op) |
688 | | { |
689 | | case 0: |
690 | | op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize); |
691 | | break; |
692 | | |
693 | | case 1: |
694 | | op2sum = avg(&op2, op2sum, SrcSize); |
695 | | break; |
696 | | |
697 | | case 2: |
698 | | op3sum = avg(&op3, op3sum, SrcSize); |
699 | | break; |
700 | | |
701 | | default: |
702 | | break; |
703 | | } |
704 | | |
705 | | WLog_Print(h264->log, WLOG_INFO, |
706 | | "luma=%" PRIu64 " [avg=%lf] chroma=%" PRIu64 " [avg=%lf] combined=%" PRIu64 |
707 | | " [avg=%lf]", |
708 | | op1, op1sum, op2, op2sum, op3, op3sum); |
709 | | #endif |
710 | 0 | return status; |
711 | 0 | } |
712 | | |
713 | 0 | #define MAX_SUBSYSTEMS 10 |
714 | | static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT; |
715 | | static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT; |
716 | | |
717 | | static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED PINIT_ONCE once, |
718 | | WINPR_ATTR_UNUSED PVOID param, |
719 | | WINPR_ATTR_UNUSED PVOID* context) |
720 | 0 | { |
721 | 0 | int i = 0; |
722 | |
|
723 | | #ifdef WITH_MEDIACODEC |
724 | | { |
725 | | subSystems[i] = &g_Subsystem_mediacodec; |
726 | | i++; |
727 | | } |
728 | | #endif |
729 | | #if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION) |
730 | | { |
731 | | subSystems[i] = &g_Subsystem_MF; |
732 | | i++; |
733 | | } |
734 | | #endif |
735 | | #ifdef WITH_OPENH264 |
736 | | { |
737 | | subSystems[i] = &g_Subsystem_OpenH264; |
738 | | i++; |
739 | | } |
740 | | #endif |
741 | | #ifdef WITH_VIDEO_FFMPEG |
742 | | { |
743 | | subSystems[i] = &g_Subsystem_libavcodec; |
744 | | i++; |
745 | | } |
746 | | #endif |
747 | 0 | return i > 0; |
748 | 0 | } |
749 | | |
750 | | static BOOL h264_context_init(H264_CONTEXT* h264) |
751 | 0 | { |
752 | 0 | if (!h264) |
753 | 0 | return FALSE; |
754 | | |
755 | 0 | h264->subsystem = nullptr; |
756 | 0 | if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems, nullptr, nullptr)) |
757 | 0 | return FALSE; |
758 | | |
759 | 0 | for (size_t i = 0; i < MAX_SUBSYSTEMS; i++) |
760 | 0 | { |
761 | 0 | const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i]; |
762 | |
|
763 | 0 | if (!subsystem || !subsystem->Init) |
764 | 0 | break; |
765 | | |
766 | 0 | if (subsystem->Init(h264)) |
767 | 0 | { |
768 | 0 | h264->subsystem = subsystem; |
769 | 0 | return TRUE; |
770 | 0 | } |
771 | 0 | } |
772 | | |
773 | 0 | return FALSE; |
774 | 0 | } |
775 | | |
776 | | BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height) |
777 | 0 | { |
778 | 0 | if (!h264) |
779 | 0 | return FALSE; |
780 | | |
781 | 0 | h264->width = width; |
782 | 0 | h264->height = height; |
783 | |
|
784 | 0 | if (h264->subsystem && h264->subsystem->Uninit) |
785 | 0 | h264->subsystem->Uninit(h264); |
786 | 0 | if (!h264_context_init(h264)) |
787 | 0 | return FALSE; |
788 | | |
789 | 0 | return yuv_context_reset(h264->yuv, width, height); |
790 | 0 | } |
791 | | |
792 | | H264_CONTEXT* h264_context_new(BOOL Compressor) |
793 | 0 | { |
794 | 0 | H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1, sizeof(H264_CONTEXT)); |
795 | 0 | if (!h264) |
796 | 0 | return nullptr; |
797 | | |
798 | 0 | h264->log = WLog_Get(TAG); |
799 | |
|
800 | 0 | if (!h264->log) |
801 | 0 | goto fail; |
802 | | |
803 | 0 | h264->Compressor = Compressor; |
804 | 0 | if (Compressor) |
805 | 0 | { |
806 | | /* Default compressor settings, may be changed by caller */ |
807 | 0 | h264->BitRate = 1000000; |
808 | 0 | h264->FrameRate = 30; |
809 | 0 | } |
810 | |
|
811 | 0 | if (!h264_context_init(h264)) |
812 | 0 | goto fail; |
813 | | |
814 | 0 | h264->yuv = yuv_context_new(Compressor, 0); |
815 | 0 | if (!h264->yuv) |
816 | 0 | goto fail; |
817 | | |
818 | 0 | return h264; |
819 | | |
820 | 0 | fail: |
821 | 0 | WINPR_PRAGMA_DIAG_PUSH |
822 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
823 | 0 | h264_context_free(h264); |
824 | 0 | WINPR_PRAGMA_DIAG_POP |
825 | 0 | return nullptr; |
826 | 0 | } |
827 | | |
828 | | void h264_context_free(H264_CONTEXT* h264) |
829 | 0 | { |
830 | 0 | if (h264) |
831 | 0 | { |
832 | 0 | if (h264->subsystem) |
833 | 0 | { |
834 | 0 | WINPR_ASSERT(h264->subsystem->Uninit); |
835 | 0 | h264->subsystem->Uninit(h264); |
836 | 0 | } |
837 | |
|
838 | 0 | for (size_t x = 0; x < 3; x++) |
839 | 0 | { |
840 | 0 | if (h264->Compressor) |
841 | 0 | { |
842 | 0 | winpr_aligned_free(h264->pYUVData[x]); |
843 | 0 | winpr_aligned_free(h264->pOldYUVData[x]); |
844 | 0 | } |
845 | 0 | winpr_aligned_free(h264->pYUV444Data[x]); |
846 | 0 | winpr_aligned_free(h264->pOldYUV444Data[x]); |
847 | 0 | } |
848 | 0 | winpr_aligned_free(h264->lumaData); |
849 | |
|
850 | 0 | yuv_context_free(h264->yuv); |
851 | 0 | free(h264); |
852 | 0 | } |
853 | 0 | } |
854 | | |
855 | | void free_h264_metablock(RDPGFX_H264_METABLOCK* meta) |
856 | 0 | { |
857 | 0 | RDPGFX_H264_METABLOCK m = WINPR_C_ARRAY_INIT; |
858 | 0 | if (!meta) |
859 | 0 | return; |
860 | 0 | free(meta->quantQualityVals); |
861 | 0 | free(meta->regionRects); |
862 | 0 | *meta = m; |
863 | 0 | } |
864 | | |
865 | | BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value) |
866 | 0 | { |
867 | 0 | WINPR_ASSERT(h264); |
868 | 0 | switch (option) |
869 | 0 | { |
870 | 0 | case H264_CONTEXT_OPTION_BITRATE: |
871 | 0 | h264->BitRate = value; |
872 | 0 | return TRUE; |
873 | 0 | case H264_CONTEXT_OPTION_FRAMERATE: |
874 | 0 | h264->FrameRate = value; |
875 | 0 | return TRUE; |
876 | 0 | case H264_CONTEXT_OPTION_RATECONTROL: |
877 | 0 | { |
878 | 0 | switch (value) |
879 | 0 | { |
880 | 0 | case H264_RATECONTROL_VBR: |
881 | 0 | h264->RateControlMode = H264_RATECONTROL_VBR; |
882 | 0 | return TRUE; |
883 | 0 | case H264_RATECONTROL_CQP: |
884 | 0 | h264->RateControlMode = H264_RATECONTROL_CQP; |
885 | 0 | return TRUE; |
886 | 0 | default: |
887 | 0 | WLog_Print(h264->log, WLOG_WARN, |
888 | 0 | "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32 "]", |
889 | 0 | value); |
890 | 0 | return FALSE; |
891 | 0 | } |
892 | 0 | return TRUE; |
893 | 0 | } |
894 | 0 | case H264_CONTEXT_OPTION_QP: |
895 | 0 | h264->QP = value; |
896 | 0 | return TRUE; |
897 | 0 | case H264_CONTEXT_OPTION_USAGETYPE: |
898 | 0 | h264->UsageType = value; |
899 | 0 | return TRUE; |
900 | 0 | case H264_CONTEXT_OPTION_HW_ACCEL: |
901 | 0 | h264->hwAccel = (value); |
902 | 0 | return TRUE; |
903 | 0 | default: |
904 | 0 | WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]", |
905 | 0 | option); |
906 | 0 | return FALSE; |
907 | 0 | } |
908 | 0 | } |
909 | | |
910 | | UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option) |
911 | 0 | { |
912 | 0 | WINPR_ASSERT(h264); |
913 | 0 | switch (option) |
914 | 0 | { |
915 | 0 | case H264_CONTEXT_OPTION_BITRATE: |
916 | 0 | return h264->BitRate; |
917 | 0 | case H264_CONTEXT_OPTION_FRAMERATE: |
918 | 0 | return h264->FrameRate; |
919 | 0 | case H264_CONTEXT_OPTION_RATECONTROL: |
920 | 0 | return h264->RateControlMode; |
921 | 0 | case H264_CONTEXT_OPTION_QP: |
922 | 0 | return h264->QP; |
923 | 0 | case H264_CONTEXT_OPTION_USAGETYPE: |
924 | 0 | return h264->UsageType; |
925 | 0 | case H264_CONTEXT_OPTION_HW_ACCEL: |
926 | 0 | return h264->hwAccel; |
927 | 0 | default: |
928 | 0 | WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]", |
929 | 0 | option); |
930 | 0 | return 0; |
931 | 0 | } |
932 | 0 | } |