/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 | | WINPR_ATTR_NODISCARD |
40 | | static BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight); |
41 | | |
42 | | WINPR_ATTR_NODISCARD |
43 | | static BOOL yuv_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height) |
44 | 0 | { |
45 | 0 | BOOL isNull = FALSE; |
46 | 0 | UINT32 pheight = height; |
47 | |
|
48 | 0 | if (!h264) |
49 | 0 | return FALSE; |
50 | | |
51 | 0 | if (stride == 0) |
52 | 0 | { |
53 | 0 | const UINT32 pad = width % 16; |
54 | 0 | stride = width; |
55 | 0 | if (pad > 0) |
56 | 0 | width += 16 - pad; |
57 | 0 | } |
58 | | |
59 | | /* Add padding lines. Allows relaxing bounds checks in decoder functions */ |
60 | 0 | stride += 32 - stride % 16; |
61 | 0 | pheight += 32 - pheight % 16; |
62 | |
|
63 | 0 | const size_t nPlanes = h264->hwAccel ? 2 : 3; |
64 | |
|
65 | 0 | for (size_t x = 0; x < nPlanes; x++) |
66 | 0 | { |
67 | 0 | if (!h264->pYUVData[x] || !h264->pOldYUVData[x]) |
68 | 0 | isNull = TRUE; |
69 | 0 | } |
70 | |
|
71 | 0 | if (pheight == 0) |
72 | 0 | return FALSE; |
73 | 0 | if (stride == 0) |
74 | 0 | return FALSE; |
75 | | |
76 | 0 | if (isNull || (width != h264->width) || (height != h264->height) || |
77 | 0 | (stride != h264->iStride[0])) |
78 | 0 | { |
79 | 0 | UINT32 iStride[3] = WINPR_C_ARRAY_INIT; |
80 | 0 | if (h264->hwAccel) /* NV12 */ |
81 | 0 | { |
82 | 0 | iStride[0] = stride; |
83 | 0 | iStride[1] = stride; |
84 | 0 | iStride[2] = 0; |
85 | 0 | } |
86 | 0 | else /* I420 */ |
87 | 0 | { |
88 | 0 | iStride[0] = stride; |
89 | 0 | iStride[1] = (stride + 1) / 2; |
90 | 0 | iStride[2] = (stride + 1) / 2; |
91 | 0 | } |
92 | |
|
93 | 0 | for (size_t x = 0; x < nPlanes; x++) |
94 | 0 | { |
95 | 0 | BYTE* tmp1 = winpr_aligned_recalloc(h264->pYUVData[x], iStride[x], pheight, 16); |
96 | 0 | BYTE* tmp2 = winpr_aligned_recalloc(h264->pOldYUVData[x], iStride[x], pheight, 16); |
97 | 0 | if (tmp1) |
98 | 0 | h264->pYUVData[x] = tmp1; |
99 | 0 | if (tmp2) |
100 | 0 | h264->pOldYUVData[x] = tmp2; |
101 | 0 | if (!tmp1 || !tmp2) |
102 | 0 | return FALSE; |
103 | 0 | } |
104 | | /* Only update the strides after the buffers were successfully allocated */ |
105 | 0 | memcpy(h264->iStride, iStride, sizeof(h264->iStride)); |
106 | 0 | h264->width = width; |
107 | 0 | h264->height = height; |
108 | 0 | } |
109 | | |
110 | 0 | return TRUE; |
111 | 0 | } |
112 | | |
113 | | BOOL avc420_ensure_buffer(H264_CONTEXT* h264, UINT32 stride, UINT32 width, UINT32 height) |
114 | 0 | { |
115 | 0 | return yuv_ensure_buffer(h264, stride, width, height); |
116 | 0 | } |
117 | | |
118 | | WINPR_ATTR_NODISCARD |
119 | | static BOOL isRectValid(wLog* log, size_t pos, UINT32 width, UINT32 height, |
120 | | const RECTANGLE_16* rect) |
121 | 0 | { |
122 | 0 | WINPR_ASSERT(rect); |
123 | 0 | if ((rect->left > width) || (rect->right > width) || (rect->left >= rect->right) || |
124 | 0 | (rect->top > height) || (rect->bottom > height) || (rect->top >= rect->bottom)) |
125 | 0 | { |
126 | 0 | char buffer[64] = WINPR_C_ARRAY_INIT; |
127 | 0 | WLog_Print(log, WLOG_WARN, |
128 | 0 | "Rectangle %" PRIuz " %s outside of bounding frame %" PRIu32 "x%" PRIu32, pos, |
129 | 0 | rectangle_to_string(rect, buffer, sizeof(buffer)), width, height); |
130 | 0 | return FALSE; |
131 | 0 | } |
132 | | |
133 | 0 | return TRUE; |
134 | 0 | } |
135 | | |
136 | | WINPR_ATTR_NODISCARD |
137 | | static BOOL areRectsValid(wLog* log, UINT32 width, UINT32 height, const RECTANGLE_16* rects, |
138 | | UINT32 count) |
139 | 0 | { |
140 | 0 | WINPR_ASSERT(rects || (count == 0)); |
141 | 0 | for (size_t x = 0; x < count; x++) |
142 | 0 | { |
143 | 0 | const RECTANGLE_16* rect = &rects[x]; |
144 | 0 | if (!isRectValid(log, x, width, height, rect)) |
145 | 0 | return FALSE; |
146 | 0 | } |
147 | 0 | return TRUE; |
148 | 0 | } |
149 | | |
150 | | WINPR_ATTR_NODISCARD |
151 | | static int log_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, |
152 | | const RECTANGLE_16* rects, UINT32 nrRects) |
153 | 0 | { |
154 | 0 | WINPR_ASSERT(h264); |
155 | 0 | WINPR_ASSERT(h264->subsystem); |
156 | 0 | WINPR_ASSERT(h264->subsystem->Decompress); |
157 | |
|
158 | 0 | const int status = h264->subsystem->Decompress(h264, pSrcData, SrcSize); |
159 | 0 | if (status < 0) |
160 | 0 | { |
161 | 0 | WLog_Print(h264->log, WLOG_WARN, "H264 decompress failed with %d", status); |
162 | 0 | return status; |
163 | 0 | } |
164 | | |
165 | | /* no picture decoded, nothing to validate */ |
166 | 0 | if (status == 0) |
167 | 0 | return 0; |
168 | | |
169 | | /* some server implementations (krdc) use H264 frames smaller than the surface sizes, |
170 | | * validate the regions against this size as well */ |
171 | 0 | if (!areRectsValid(h264->log, h264->YUVWidth, h264->YUVHeight, rects, nrRects)) |
172 | 0 | return -1015; |
173 | 0 | if (!areRectsValid(h264->log, h264->width, h264->height, rects, nrRects)) |
174 | 0 | return -1016; |
175 | 0 | return status; |
176 | 0 | } |
177 | | |
178 | | INT32 avc420_decompress(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, BYTE* pDstData, |
179 | | DWORD DstFormat, UINT32 nDstStep, WINPR_ATTR_UNUSED UINT32 nDstWidth, |
180 | | WINPR_ATTR_UNUSED UINT32 nDstHeight, const RECTANGLE_16* regionRects, |
181 | | UINT32 numRegionRects) |
182 | 0 | { |
183 | 0 | int status = 0; |
184 | 0 | const BYTE* pYUVData[3]; |
185 | |
|
186 | 0 | if (!h264 || h264->Compressor) |
187 | 0 | return -1001; |
188 | | |
189 | 0 | if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects)) |
190 | 0 | return -1013; |
191 | | |
192 | 0 | status = log_decompress(h264, pSrcData, SrcSize, regionRects, numRegionRects); |
193 | |
|
194 | 0 | if (status == 0) |
195 | 0 | return 1; |
196 | | |
197 | 0 | if (status < 0) |
198 | 0 | return status; |
199 | | |
200 | 0 | pYUVData[0] = h264->pYUVData[0]; |
201 | 0 | pYUVData[1] = h264->pYUVData[1]; |
202 | 0 | pYUVData[2] = h264->pYUVData[2]; |
203 | 0 | if (!yuv420_context_decode(h264->yuv, pYUVData, h264->iStride, h264->YUVHeight, DstFormat, |
204 | 0 | pDstData, nDstStep, regionRects, numRegionRects)) |
205 | 0 | return -1002; |
206 | | |
207 | 0 | return 1; |
208 | 0 | } |
209 | | |
210 | | WINPR_ATTR_NODISCARD |
211 | | static BOOL allocate_h264_metablock(UINT32 QP, RECTANGLE_16* rectangles, |
212 | | RDPGFX_H264_METABLOCK* meta, size_t count) |
213 | 0 | { |
214 | | /* [MS-RDPEGFX] 2.2.4.4.2 RDPGFX_AVC420_QUANT_QUALITY */ |
215 | 0 | if (!meta || (QP > UINT8_MAX)) |
216 | 0 | { |
217 | 0 | free(rectangles); |
218 | 0 | return FALSE; |
219 | 0 | } |
220 | | |
221 | 0 | meta->regionRects = rectangles; |
222 | 0 | if (count == 0) |
223 | 0 | return TRUE; |
224 | | |
225 | 0 | if (count > UINT32_MAX) |
226 | 0 | return FALSE; |
227 | | |
228 | 0 | meta->quantQualityVals = calloc(count, sizeof(RDPGFX_H264_QUANT_QUALITY)); |
229 | |
|
230 | 0 | if (!meta->quantQualityVals || !meta->regionRects) |
231 | 0 | return FALSE; |
232 | 0 | meta->numRegionRects = (UINT32)count; |
233 | 0 | for (size_t x = 0; x < count; x++) |
234 | 0 | { |
235 | 0 | RDPGFX_H264_QUANT_QUALITY* cur = &meta->quantQualityVals[x]; |
236 | 0 | cur->qp = (UINT8)QP; |
237 | | |
238 | | /* qpVal bit 6 and 7 are flags, so mask them out here. |
239 | | * qualityVal is [0-100] so 100 - qpVal [0-64] is always in range */ |
240 | 0 | cur->qualityVal = 100 - (QP & 0x3F); |
241 | 0 | } |
242 | 0 | return TRUE; |
243 | 0 | } |
244 | | |
245 | | WINPR_ATTR_NODISCARD |
246 | | static inline BOOL diff_tile(const RECTANGLE_16* regionRect, BYTE* pYUVData[3], |
247 | | BYTE* pOldYUVData[3], UINT32 const iStride[3]) |
248 | 0 | { |
249 | 0 | size_t size = 0; |
250 | |
|
251 | 0 | if (!regionRect || !pYUVData || !pOldYUVData || !iStride) |
252 | 0 | return FALSE; |
253 | 0 | size = regionRect->right - regionRect->left; |
254 | 0 | if (regionRect->right > iStride[0]) |
255 | 0 | return FALSE; |
256 | 0 | if (regionRect->right / 2u > iStride[1]) |
257 | 0 | return FALSE; |
258 | 0 | if (regionRect->right / 2u > iStride[2]) |
259 | 0 | return FALSE; |
260 | | |
261 | 0 | for (size_t y = regionRect->top; y < regionRect->bottom; y++) |
262 | 0 | { |
263 | 0 | const BYTE* cur0 = &pYUVData[0][y * iStride[0]]; |
264 | 0 | const BYTE* old0 = &pOldYUVData[0][y * iStride[0]]; |
265 | |
|
266 | 0 | if (memcmp(&cur0[regionRect->left], &old0[regionRect->left], size) != 0) |
267 | 0 | return TRUE; |
268 | 0 | } |
269 | | |
270 | | /* the U and V planes have half the rows: the tile's chroma is on rows top / 2 to |
271 | | * (bottom + 1) / 2. Row y of them is screen row 2 * y, so reading row y here compared |
272 | | * another part of the screen (and past the planes' content below half height). */ |
273 | 0 | const size_t uvLeft = regionRect->left / 2u; |
274 | 0 | const size_t uvSize = (regionRect->right + 1u) / 2u - uvLeft; |
275 | 0 | for (size_t y = regionRect->top / 2u; y < (regionRect->bottom + 1u) / 2u; y++) |
276 | 0 | { |
277 | 0 | const BYTE* cur1 = &pYUVData[1][y * iStride[1]]; |
278 | 0 | const BYTE* cur2 = &pYUVData[2][y * iStride[2]]; |
279 | 0 | const BYTE* old1 = &pOldYUVData[1][y * iStride[1]]; |
280 | 0 | const BYTE* old2 = &pOldYUVData[2][y * iStride[2]]; |
281 | |
|
282 | 0 | if (memcmp(&cur1[uvLeft], &old1[uvLeft], uvSize) != 0) |
283 | 0 | return TRUE; |
284 | 0 | if (memcmp(&cur2[uvLeft], &old2[uvLeft], uvSize) != 0) |
285 | 0 | return TRUE; |
286 | 0 | } |
287 | 0 | return FALSE; |
288 | 0 | } |
289 | | |
290 | | /* The AVC444v2 auxiliary view keeps the chroma detail of screen columns [left, right) in two |
291 | | * halves (MS-RDPEGFX 3.3.8.3.3): in its Y plane at [left / 2, right / 2) and, width / 2 further, |
292 | | * at [width / 2 + left / 2, width / 2 + right / 2) on the same rows; in its U and V planes at a |
293 | | * quarter of those columns on half the rows. Comparing the tile's own columns there, as |
294 | | * diff_tile does, looks at other pixels, so a tile whose chroma changed could go unreported |
295 | | * and the client kept its old colours. */ |
296 | | WINPR_ATTR_NODISCARD |
297 | | static inline BOOL diff_tile_avc444v2_aux(const RECTANGLE_16* tile, UINT32 width, BYTE* pYUVData[3], |
298 | | BYTE* pOldYUVData[3], UINT32 const iStride[3]) |
299 | 0 | { |
300 | 0 | if (!tile || !pYUVData || !pOldYUVData || !iStride || (tile->right <= tile->left)) |
301 | 0 | return FALSE; |
302 | | |
303 | 0 | const size_t l2 = tile->left / 2u; |
304 | 0 | const size_t n2 = (tile->right + 1u) / 2u - l2; |
305 | 0 | const size_t h2 = width / 2u; |
306 | 0 | const size_t l4 = tile->left / 4u; |
307 | 0 | const size_t n4 = (tile->right + 3u) / 4u - l4; |
308 | 0 | const size_t h4 = width / 4u; |
309 | |
|
310 | 0 | if ((h2 + l2 + n2 > iStride[0]) || (h4 + l4 + n4 > iStride[1]) || (h4 + l4 + n4 > iStride[2])) |
311 | 0 | return TRUE; /* can't tell: send it */ |
312 | | |
313 | 0 | for (size_t y = tile->top; y < tile->bottom; y++) |
314 | 0 | { |
315 | 0 | const BYTE* cur = &pYUVData[0][y * iStride[0]]; |
316 | 0 | const BYTE* old = &pOldYUVData[0][y * iStride[0]]; |
317 | 0 | if ((memcmp(&cur[l2], &old[l2], n2) != 0) || |
318 | 0 | (memcmp(&cur[h2 + l2], &old[h2 + l2], n2) != 0)) |
319 | 0 | return TRUE; |
320 | 0 | } |
321 | | |
322 | 0 | for (size_t y = tile->top / 2u; y < (tile->bottom + 1u) / 2u; y++) |
323 | 0 | { |
324 | 0 | for (size_t p = 1; p < 3; p++) |
325 | 0 | { |
326 | 0 | const BYTE* cur = &pYUVData[p][y * iStride[p]]; |
327 | 0 | const BYTE* old = &pOldYUVData[p][y * iStride[p]]; |
328 | 0 | if ((memcmp(&cur[l4], &old[l4], n4) != 0) || |
329 | 0 | (memcmp(&cur[h4 + l4], &old[h4 + l4], n4) != 0)) |
330 | 0 | return TRUE; |
331 | 0 | } |
332 | 0 | } |
333 | 0 | return FALSE; |
334 | 0 | } |
335 | | |
336 | | WINPR_ATTR_NODISCARD |
337 | | static BOOL detect_changes(BOOL firstFrameDone, const UINT32 QP, const RECTANGLE_16* regionRect, |
338 | | BYTE* pYUVData[3], BYTE* pOldYUVData[3], UINT32 const iStride[3], |
339 | | UINT32 v2AuxWidth, RDPGFX_H264_METABLOCK* meta) |
340 | 0 | { |
341 | 0 | size_t count = 0; |
342 | 0 | size_t wc = 0; |
343 | 0 | size_t hc = 0; |
344 | 0 | RECTANGLE_16* rectangles = nullptr; |
345 | |
|
346 | 0 | if (!regionRect || !pYUVData || !pOldYUVData || !iStride || !meta) |
347 | 0 | return FALSE; |
348 | | |
349 | 0 | wc = (regionRect->right - regionRect->left) / 64 + 1; |
350 | 0 | hc = (regionRect->bottom - regionRect->top) / 64 + 1; |
351 | 0 | rectangles = calloc(wc * hc, sizeof(RECTANGLE_16)); |
352 | 0 | if (!rectangles) |
353 | 0 | return FALSE; |
354 | 0 | if (!firstFrameDone) |
355 | 0 | { |
356 | 0 | rectangles[0] = *regionRect; |
357 | 0 | count = 1; |
358 | 0 | } |
359 | 0 | else |
360 | 0 | { |
361 | 0 | for (size_t y = 0; y < regionRect->bottom - regionRect->top; y += 64) |
362 | 0 | { |
363 | 0 | for (size_t x = 0; x < regionRect->right - regionRect->left; x += 64) |
364 | 0 | { |
365 | 0 | RECTANGLE_16 rect; |
366 | 0 | rect.left = (UINT16)MIN(UINT16_MAX, regionRect->left + x); |
367 | 0 | rect.top = (UINT16)MIN(UINT16_MAX, regionRect->top + y); |
368 | 0 | rect.right = |
369 | 0 | (UINT16)MIN(UINT16_MAX, MIN(regionRect->left + x + 64, regionRect->right)); |
370 | 0 | rect.bottom = |
371 | 0 | (UINT16)MIN(UINT16_MAX, MIN(regionRect->top + y + 64, regionRect->bottom)); |
372 | 0 | const BOOL changed = |
373 | 0 | v2AuxWidth |
374 | 0 | ? diff_tile_avc444v2_aux(&rect, v2AuxWidth, pYUVData, pOldYUVData, iStride) |
375 | 0 | : diff_tile(&rect, pYUVData, pOldYUVData, iStride); |
376 | 0 | if (changed) |
377 | 0 | rectangles[count++] = rect; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | } |
381 | 0 | if (!allocate_h264_metablock(QP, rectangles, meta, count)) |
382 | 0 | return FALSE; |
383 | 0 | return TRUE; |
384 | 0 | } |
385 | | |
386 | | INT32 h264_get_yuv_buffer(H264_CONTEXT* h264, UINT32 nSrcStride, UINT32 nSrcWidth, |
387 | | UINT32 nSrcHeight, BYTE* YUVData[3], UINT32 stride[3]) |
388 | 0 | { |
389 | 0 | if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress) |
390 | 0 | return -1; |
391 | | |
392 | 0 | if (!yuv_ensure_buffer(h264, nSrcStride, nSrcWidth, nSrcHeight)) |
393 | 0 | return -1; |
394 | | |
395 | 0 | for (size_t x = 0; x < 3; x++) |
396 | 0 | { |
397 | 0 | YUVData[x] = h264->pYUVData[x]; |
398 | 0 | stride[x] = h264->iStride[x]; |
399 | 0 | } |
400 | |
|
401 | 0 | return 0; |
402 | 0 | } |
403 | | |
404 | | INT32 h264_compress(H264_CONTEXT* h264, BYTE** ppDstData, UINT32* pDstSize) |
405 | 0 | { |
406 | 0 | if (!h264 || !h264->Compressor || !h264->subsystem || !h264->subsystem->Compress) |
407 | 0 | return -1; |
408 | | |
409 | 0 | const BYTE* pcYUVData[3] = { h264->pYUVData[0], h264->pYUVData[1], h264->pYUVData[2] }; |
410 | |
|
411 | 0 | return h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize); |
412 | 0 | } |
413 | | |
414 | | /* The region to encode, rounded out to the 16 pixel macroblock grid inside the frame. |
415 | | * Clients rebuild AVC444 colours relative to each region's corner: the odd chroma columns and |
416 | | * rows, and every fourth sample in AVC444v2 (MS-RDPEGFX 3.3.8.3.2, 3.3.8.3.3), are counted from |
417 | | * left and top. A region starting at an odd or unaligned column or row (the shadow server sends |
418 | | * the bounding box of what changed, at any pixel) put those samples one pixel off. Encoders work |
419 | | * on 16x16 macroblocks anyway, so the wider region costs no bits. */ |
420 | | static RECTANGLE_16 h264_align_region(const RECTANGLE_16* rect, UINT32 width, UINT32 height) |
421 | 0 | { |
422 | 0 | WINPR_ASSERT(rect); |
423 | 0 | const UINT32 right = MIN(width, ((UINT32)rect->right + 15u) & ~15u); |
424 | 0 | const UINT32 bottom = MIN(height, ((UINT32)rect->bottom + 15u) & ~15u); |
425 | 0 | const RECTANGLE_16 aligned = { (UINT16)(rect->left & ~15u), (UINT16)(rect->top & ~15u), |
426 | 0 | (UINT16)MAX(rect->right, right), |
427 | 0 | (UINT16)MAX(rect->bottom, bottom) }; |
428 | 0 | return aligned; |
429 | 0 | } |
430 | | |
431 | | /* The encoder alternates between two sets of planes: the one it converts into this frame held |
432 | | * the frame before last, and only the region is converted. Outside the region the encoder then |
433 | | * saw that older picture, a change every frame where nothing changed, which costs bits (and |
434 | | * takes them from what did change). Start from the last frame, unless the region is all of it. */ |
435 | | static void h264_keep_last_frame(const RECTANGLE_16* region, UINT32 width, UINT32 height, |
436 | | BYTE* dst[3], BYTE* const src[3], const size_t size[3]) |
437 | 0 | { |
438 | 0 | WINPR_ASSERT(region); |
439 | 0 | if ((region->left == 0) && (region->top == 0) && (region->right >= width) && |
440 | 0 | (region->bottom >= height)) |
441 | 0 | return; |
442 | | |
443 | 0 | for (size_t x = 0; x < 3; x++) |
444 | 0 | { |
445 | 0 | if (dst[x] && src[x] && (size[x] > 0)) |
446 | 0 | memcpy(dst[x], src[x], size[x]); |
447 | 0 | } |
448 | 0 | } |
449 | | |
450 | | /* the size of the 4:2:0 planes, as yuv_ensure_buffer allocates them */ |
451 | | static void h264_yuv420_plane_sizes(const H264_CONTEXT* h264, size_t size[3]) |
452 | 0 | { |
453 | 0 | const size_t rows = h264->height + 32u - h264->height % 16u; |
454 | 0 | for (size_t x = 0; x < 3; x++) |
455 | 0 | size[x] = 1ull * h264->iStride[x] * rows; |
456 | 0 | } |
457 | | |
458 | | INT32 avc420_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, |
459 | | UINT32 nSrcWidth, UINT32 nSrcHeight, const RECTANGLE_16* regionRect, |
460 | | BYTE** ppDstData, UINT32* pDstSize, RDPGFX_H264_METABLOCK* meta) |
461 | 0 | { |
462 | 0 | INT32 rc = -1; |
463 | 0 | BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT; |
464 | 0 | const BYTE* pcYUVData[3] = WINPR_C_ARRAY_INIT; |
465 | 0 | BYTE* pOldYUVData[3] = WINPR_C_ARRAY_INIT; |
466 | |
|
467 | 0 | if (!h264 || !regionRect || !meta || !h264->Compressor) |
468 | 0 | return -1; |
469 | | |
470 | 0 | if (!h264->subsystem->Compress) |
471 | 0 | return -1; |
472 | | |
473 | 0 | if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight)) |
474 | 0 | return -1; |
475 | | |
476 | 0 | if (h264->encodingBuffer) |
477 | 0 | { |
478 | 0 | for (size_t x = 0; x < 3; x++) |
479 | 0 | { |
480 | 0 | pYUVData[x] = h264->pYUVData[x]; |
481 | 0 | pOldYUVData[x] = h264->pOldYUVData[x]; |
482 | 0 | } |
483 | 0 | } |
484 | 0 | else |
485 | 0 | { |
486 | 0 | for (size_t x = 0; x < 3; x++) |
487 | 0 | { |
488 | 0 | pYUVData[x] = h264->pOldYUVData[x]; |
489 | 0 | pOldYUVData[x] = h264->pYUVData[x]; |
490 | 0 | } |
491 | 0 | } |
492 | 0 | h264->encodingBuffer = !h264->encodingBuffer; |
493 | 0 | { |
494 | 0 | size_t size420[3] = WINPR_C_ARRAY_INIT; |
495 | 0 | h264_yuv420_plane_sizes(h264, size420); |
496 | 0 | h264_keep_last_frame(regionRect, nSrcWidth, nSrcHeight, pYUVData, pOldYUVData, size420); |
497 | 0 | } |
498 | |
|
499 | 0 | const RECTANGLE_16 aligned = h264_align_region(regionRect, nSrcWidth, nSrcHeight); |
500 | 0 | if (!yuv420_context_encode(h264->yuv, pSrcData, nSrcStep, SrcFormat, h264->iStride, pYUVData, |
501 | 0 | &aligned, 1)) |
502 | 0 | goto fail; |
503 | | |
504 | 0 | if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData, |
505 | 0 | h264->iStride, 0, meta)) |
506 | 0 | goto fail; |
507 | | |
508 | 0 | if (meta->numRegionRects == 0) |
509 | 0 | { |
510 | 0 | rc = 0; |
511 | 0 | goto fail; |
512 | 0 | } |
513 | | |
514 | 0 | for (size_t x = 0; x < 3; x++) |
515 | 0 | pcYUVData[x] = pYUVData[x]; |
516 | |
|
517 | 0 | rc = h264->subsystem->Compress(h264, pcYUVData, h264->iStride, ppDstData, pDstSize); |
518 | 0 | if (rc >= 0) |
519 | 0 | h264->firstLumaFrameDone = TRUE; |
520 | |
|
521 | 0 | fail: |
522 | 0 | if (rc < 0) |
523 | 0 | free_h264_metablock(meta); |
524 | 0 | return rc; |
525 | 0 | } |
526 | | |
527 | | INT32 avc444_compress(H264_CONTEXT* h264, const BYTE* pSrcData, DWORD SrcFormat, UINT32 nSrcStep, |
528 | | UINT32 nSrcWidth, UINT32 nSrcHeight, BYTE version, const RECTANGLE_16* region, |
529 | | BYTE* op, BYTE** ppDstData, UINT32* pDstSize, BYTE** ppAuxDstData, |
530 | | UINT32* pAuxDstSize, RDPGFX_H264_METABLOCK* meta, |
531 | | RDPGFX_H264_METABLOCK* auxMeta) |
532 | 0 | { |
533 | 0 | int rc = -1; |
534 | 0 | BYTE* coded = nullptr; |
535 | 0 | UINT32 codedSize = 0; |
536 | 0 | BYTE** pYUV444Data = nullptr; |
537 | 0 | BYTE** pOldYUV444Data = nullptr; |
538 | 0 | BYTE** pYUVData = nullptr; |
539 | 0 | BYTE** pOldYUVData = nullptr; |
540 | |
|
541 | 0 | if (!h264 || !h264->Compressor) |
542 | 0 | return -1; |
543 | | |
544 | 0 | if (!h264->subsystem->Compress) |
545 | 0 | return -1; |
546 | | |
547 | 0 | if (!avc420_ensure_buffer(h264, nSrcStep, nSrcWidth, nSrcHeight)) |
548 | 0 | return -1; |
549 | | |
550 | 0 | if (!avc444_ensure_buffer(h264, nSrcHeight)) |
551 | 0 | return -1; |
552 | | |
553 | 0 | if (h264->encodingBuffer) |
554 | 0 | { |
555 | 0 | pYUV444Data = h264->pOldYUV444Data; |
556 | 0 | pOldYUV444Data = h264->pYUV444Data; |
557 | 0 | pYUVData = h264->pOldYUVData; |
558 | 0 | pOldYUVData = h264->pYUVData; |
559 | 0 | } |
560 | 0 | else |
561 | 0 | { |
562 | 0 | pYUV444Data = h264->pYUV444Data; |
563 | 0 | pOldYUV444Data = h264->pOldYUV444Data; |
564 | 0 | pYUVData = h264->pYUVData; |
565 | 0 | pOldYUVData = h264->pOldYUVData; |
566 | 0 | } |
567 | 0 | h264->encodingBuffer = !h264->encodingBuffer; |
568 | 0 | { |
569 | 0 | size_t size420[3] = WINPR_C_ARRAY_INIT; |
570 | 0 | size_t size444[3] = WINPR_C_ARRAY_INIT; |
571 | 0 | h264_yuv420_plane_sizes(h264, size420); |
572 | 0 | for (size_t x = 0; x < 3; x++) |
573 | 0 | size444[x] = h264->iYUV444Size[x]; |
574 | 0 | h264_keep_last_frame(region, nSrcWidth, nSrcHeight, pYUVData, pOldYUVData, size420); |
575 | 0 | h264_keep_last_frame(region, nSrcWidth, nSrcHeight, pYUV444Data, pOldYUV444Data, size444); |
576 | 0 | } |
577 | |
|
578 | 0 | const RECTANGLE_16 aligned = h264_align_region(region, nSrcWidth, nSrcHeight); |
579 | 0 | if (!yuv444_context_encode(h264->yuv, version, pSrcData, nSrcStep, SrcFormat, h264->iStride, |
580 | 0 | pYUV444Data, pYUVData, &aligned, 1)) |
581 | 0 | goto fail; |
582 | | |
583 | 0 | if (!detect_changes(h264->firstLumaFrameDone, h264->QP, &aligned, pYUV444Data, pOldYUV444Data, |
584 | 0 | h264->iStride, 0, meta)) |
585 | 0 | goto fail; |
586 | | /* in AVC444v2 the auxiliary view is laid out by the frame width, not by screen position */ |
587 | 0 | if (!detect_changes(h264->firstChromaFrameDone, h264->QP, &aligned, pYUVData, pOldYUVData, |
588 | 0 | h264->iStride, (version == 2) ? h264->width : 0, auxMeta)) |
589 | 0 | goto fail; |
590 | | |
591 | | /* [MS-RDPEGFX] 2.2.4.5 RFX_AVC444_BITMAP_STREAM |
592 | | * LC: |
593 | | * 0 ... Luma & Chroma |
594 | | * 1 ... Luma |
595 | | * 2 ... Chroma |
596 | | */ |
597 | 0 | if ((meta->numRegionRects > 0) && (auxMeta->numRegionRects > 0)) |
598 | 0 | *op = 0; |
599 | 0 | else if (meta->numRegionRects > 0) |
600 | 0 | *op = 1; |
601 | 0 | else if (auxMeta->numRegionRects > 0) |
602 | 0 | *op = 2; |
603 | 0 | else |
604 | 0 | { |
605 | 0 | WLog_Print(h264->log, WLOG_TRACE, "no changes detected for luma or chroma frame"); |
606 | 0 | rc = 0; |
607 | 0 | goto fail; |
608 | 0 | } |
609 | | |
610 | 0 | if ((*op == 0) || (*op == 1)) |
611 | 0 | { |
612 | 0 | const BYTE* pcYUV444Data[3] = { pYUV444Data[0], pYUV444Data[1], pYUV444Data[2] }; |
613 | |
|
614 | 0 | if (h264->subsystem->Compress(h264, pcYUV444Data, h264->iStride, &coded, &codedSize) < 0) |
615 | 0 | goto fail; |
616 | 0 | h264->firstLumaFrameDone = TRUE; |
617 | 0 | memcpy(h264->lumaData, coded, codedSize); |
618 | 0 | *ppDstData = h264->lumaData; |
619 | 0 | *pDstSize = codedSize; |
620 | 0 | } |
621 | | |
622 | 0 | if ((*op == 0) || (*op == 2)) |
623 | 0 | { |
624 | 0 | const BYTE* pcYUVData[3] = { pYUVData[0], pYUVData[1], pYUVData[2] }; |
625 | |
|
626 | 0 | if (h264->subsystem->Compress(h264, pcYUVData, h264->iStride, &coded, &codedSize) < 0) |
627 | 0 | goto fail; |
628 | 0 | h264->firstChromaFrameDone = TRUE; |
629 | 0 | *ppAuxDstData = coded; |
630 | 0 | *pAuxDstSize = codedSize; |
631 | 0 | } |
632 | | |
633 | 0 | rc = 1; |
634 | 0 | fail: |
635 | 0 | if (rc < 0) |
636 | 0 | { |
637 | 0 | free_h264_metablock(meta); |
638 | 0 | free_h264_metablock(auxMeta); |
639 | 0 | } |
640 | 0 | return rc; |
641 | 0 | } |
642 | | |
643 | | BOOL avc444_ensure_buffer(H264_CONTEXT* h264, DWORD nDstHeight) |
644 | 0 | { |
645 | 0 | WINPR_ASSERT(h264); |
646 | |
|
647 | 0 | const UINT32* piMainStride = h264->iStride; |
648 | 0 | UINT32* piDstSize = h264->iYUV444Size; |
649 | 0 | UINT32* piDstStride = h264->iYUV444Stride; |
650 | 0 | BYTE** ppYUVDstData = h264->pYUV444Data; |
651 | 0 | BYTE** ppOldYUVDstData = h264->pOldYUV444Data; |
652 | |
|
653 | 0 | nDstHeight = MAX(h264->height, nDstHeight); |
654 | 0 | const UINT32 pad = nDstHeight % 16; |
655 | 0 | UINT32 padDstHeight = nDstHeight; /* Need alignment to 16x16 blocks */ |
656 | |
|
657 | 0 | if (pad != 0) |
658 | 0 | padDstHeight += 16 - pad; |
659 | | |
660 | | /* Add padding lines. Allows relaxing bounds checks in decoder functions */ |
661 | 0 | padDstHeight += 16; |
662 | |
|
663 | 0 | if ((piMainStride[0] == 0) || (padDstHeight == 0)) |
664 | 0 | return FALSE; |
665 | | |
666 | 0 | const uint64_t dstsize = 1ull * piMainStride[0] * padDstHeight; |
667 | 0 | if (dstsize > UINT32_MAX) |
668 | 0 | return FALSE; |
669 | | |
670 | 0 | if ((piMainStride[0] != piDstStride[0]) || (piDstSize[0] != dstsize)) |
671 | 0 | { |
672 | 0 | const UINT32 size = WINPR_ASSERTING_INT_CAST(UINT32, dstsize); |
673 | |
|
674 | 0 | for (UINT32 x = 0; x < 3; x++) |
675 | 0 | { |
676 | 0 | BYTE* tmp1 = winpr_aligned_recalloc(ppYUVDstData[x], size, 1, 16); |
677 | 0 | if (!tmp1) |
678 | 0 | return FALSE; |
679 | 0 | ppYUVDstData[x] = tmp1; |
680 | 0 | BYTE* tmp2 = winpr_aligned_recalloc(ppOldYUVDstData[x], size, 1, 16); |
681 | 0 | if (!tmp2) |
682 | 0 | return FALSE; |
683 | 0 | ppOldYUVDstData[x] = tmp2; |
684 | 0 | } |
685 | | |
686 | 0 | { |
687 | 0 | BYTE* tmp = winpr_aligned_recalloc(h264->lumaData, size, 4, 16); |
688 | 0 | if (!tmp) |
689 | 0 | goto fail; |
690 | 0 | h264->lumaData = tmp; |
691 | 0 | } |
692 | | |
693 | | /* Only update stride and size after all buffers were successfully allocated, |
694 | | * otherwise the next call would skip the reallocation and use too small planes */ |
695 | 0 | for (UINT32 x = 0; x < 3; x++) |
696 | 0 | { |
697 | 0 | piDstStride[x] = piMainStride[0]; |
698 | 0 | piDstSize[x] = size; |
699 | 0 | } |
700 | 0 | } |
701 | | |
702 | 0 | for (UINT32 x = 0; x < 3; x++) |
703 | 0 | { |
704 | 0 | if (!ppOldYUVDstData[x] || !ppYUVDstData[x] || (piDstSize[x] == 0) || (piDstStride[x] == 0)) |
705 | 0 | { |
706 | 0 | WLog_Print(h264->log, WLOG_ERROR, |
707 | 0 | "YUV buffer not initialized! check your decoder settings"); |
708 | 0 | goto fail; |
709 | 0 | } |
710 | 0 | } |
711 | | |
712 | 0 | if (!h264->lumaData) |
713 | 0 | goto fail; |
714 | | |
715 | 0 | return TRUE; |
716 | 0 | fail: |
717 | 0 | return FALSE; |
718 | 0 | } |
719 | | |
720 | | WINPR_ATTR_NODISCARD |
721 | | static BOOL avc444_process_rects(H264_CONTEXT* h264, const BYTE* pSrcData, UINT32 SrcSize, |
722 | | BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep, |
723 | | WINPR_ATTR_UNUSED UINT32 nDstWidth, UINT32 nDstHeight, |
724 | | const RECTANGLE_16* rects, UINT32 nrRects, avc444_frame_type type) |
725 | 0 | { |
726 | 0 | const BYTE* pYUVData[3] = WINPR_C_ARRAY_INIT; |
727 | 0 | BYTE* pYUVDstData[3] = WINPR_C_ARRAY_INIT; |
728 | 0 | UINT32* piDstStride = h264->iYUV444Stride; |
729 | 0 | BYTE** ppYUVDstData = h264->pYUV444Data; |
730 | 0 | const UINT32* piStride = h264->iStride; |
731 | |
|
732 | 0 | const int status = log_decompress(h264, pSrcData, SrcSize, rects, nrRects); |
733 | 0 | if (status < 0) |
734 | 0 | return FALSE; |
735 | 0 | if (status == 0) |
736 | 0 | return TRUE; |
737 | | |
738 | 0 | pYUVData[0] = h264->pYUVData[0]; |
739 | 0 | pYUVData[1] = h264->pYUVData[1]; |
740 | 0 | pYUVData[2] = h264->pYUVData[2]; |
741 | 0 | if (!avc444_ensure_buffer(h264, nDstHeight)) |
742 | 0 | return FALSE; |
743 | | |
744 | 0 | pYUVDstData[0] = ppYUVDstData[0]; |
745 | 0 | pYUVDstData[1] = ppYUVDstData[1]; |
746 | 0 | pYUVDstData[2] = ppYUVDstData[2]; |
747 | 0 | return (yuv444_context_decode(h264->yuv, (BYTE)type, pYUVData, piStride, h264->YUVHeight, |
748 | 0 | pYUVDstData, piDstStride, DstFormat, pDstData, nDstStep, rects, |
749 | 0 | nrRects)); |
750 | 0 | } |
751 | | |
752 | | #if defined(AVC444_FRAME_STAT) |
753 | | static UINT64 op1 = 0; |
754 | | static double op1sum = 0; |
755 | | static UINT64 op2 = 0; |
756 | | static double op2sum = 0; |
757 | | static UINT64 op3 = 0; |
758 | | static double op3sum = 0; |
759 | | static double avg(UINT64* count, double old, double size) |
760 | | { |
761 | | double tmp = size + *count * old; |
762 | | (*count)++; |
763 | | tmp = tmp / *count; |
764 | | return tmp; |
765 | | } |
766 | | #endif |
767 | | |
768 | | INT32 avc444_decompress(H264_CONTEXT* h264, BYTE op, const RECTANGLE_16* regionRects, |
769 | | UINT32 numRegionRects, const BYTE* pSrcData, UINT32 SrcSize, |
770 | | const RECTANGLE_16* auxRegionRects, UINT32 numAuxRegionRect, |
771 | | const BYTE* pAuxSrcData, UINT32 AuxSrcSize, BYTE* pDstData, DWORD DstFormat, |
772 | | UINT32 nDstStep, UINT32 nDstWidth, UINT32 nDstHeight, UINT32 codecId) |
773 | 0 | { |
774 | 0 | INT32 status = -1; |
775 | 0 | avc444_frame_type chroma = |
776 | 0 | (codecId == RDPGFX_CODECID_AVC444) ? AVC444_CHROMAv1 : AVC444_CHROMAv2; |
777 | |
|
778 | 0 | if (!h264 || !regionRects || !pSrcData || !pDstData || h264->Compressor) |
779 | 0 | return -1001; |
780 | | |
781 | 0 | if (!areRectsValid(h264->log, nDstWidth, nDstHeight, regionRects, numRegionRects)) |
782 | 0 | return -1013; |
783 | 0 | if (!areRectsValid(h264->log, nDstWidth, nDstHeight, auxRegionRects, numAuxRegionRect)) |
784 | 0 | return -1014; |
785 | | |
786 | 0 | switch (op) |
787 | 0 | { |
788 | 0 | case 0: /* YUV420 in stream 1 |
789 | | * Chroma420 in stream 2 */ |
790 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
791 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, |
792 | 0 | AVC444_LUMA)) |
793 | 0 | status = -1; |
794 | 0 | else if (!avc444_process_rects(h264, pAuxSrcData, AuxSrcSize, pDstData, DstFormat, |
795 | 0 | nDstStep, nDstWidth, nDstHeight, auxRegionRects, |
796 | 0 | numAuxRegionRect, chroma)) |
797 | 0 | status = -1; |
798 | 0 | else |
799 | 0 | status = 0; |
800 | |
|
801 | 0 | break; |
802 | | |
803 | 0 | case 2: /* Chroma420 in stream 1 */ |
804 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
805 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, chroma)) |
806 | 0 | status = -1; |
807 | 0 | else |
808 | 0 | status = 0; |
809 | |
|
810 | 0 | break; |
811 | | |
812 | 0 | case 1: /* YUV420 in stream 1 */ |
813 | 0 | if (!avc444_process_rects(h264, pSrcData, SrcSize, pDstData, DstFormat, nDstStep, |
814 | 0 | nDstWidth, nDstHeight, regionRects, numRegionRects, |
815 | 0 | AVC444_LUMA)) |
816 | 0 | status = -1; |
817 | 0 | else |
818 | 0 | status = 0; |
819 | |
|
820 | 0 | break; |
821 | | |
822 | 0 | default: /* WTF? */ |
823 | 0 | break; |
824 | 0 | } |
825 | | |
826 | | #if defined(AVC444_FRAME_STAT) |
827 | | |
828 | | switch (op) |
829 | | { |
830 | | case 0: |
831 | | op1sum = avg(&op1, op1sum, SrcSize + AuxSrcSize); |
832 | | break; |
833 | | |
834 | | case 1: |
835 | | op2sum = avg(&op2, op2sum, SrcSize); |
836 | | break; |
837 | | |
838 | | case 2: |
839 | | op3sum = avg(&op3, op3sum, SrcSize); |
840 | | break; |
841 | | |
842 | | default: |
843 | | break; |
844 | | } |
845 | | |
846 | | WLog_Print(h264->log, WLOG_INFO, |
847 | | "luma=%" PRIu64 " [avg=%lf] chroma=%" PRIu64 " [avg=%lf] combined=%" PRIu64 |
848 | | " [avg=%lf]", |
849 | | op1, op1sum, op2, op2sum, op3, op3sum); |
850 | | #endif |
851 | 0 | return status; |
852 | 0 | } |
853 | | |
854 | 0 | #define MAX_SUBSYSTEMS 10 |
855 | | static INIT_ONCE subsystems_once = INIT_ONCE_STATIC_INIT; |
856 | | static const H264_CONTEXT_SUBSYSTEM* subSystems[MAX_SUBSYSTEMS] = WINPR_C_ARRAY_INIT; |
857 | | |
858 | | WINPR_ATTR_NODISCARD |
859 | | static BOOL CALLBACK h264_register_subsystems(WINPR_ATTR_UNUSED PINIT_ONCE once, |
860 | | WINPR_ATTR_UNUSED PVOID param, |
861 | | WINPR_ATTR_UNUSED PVOID* context) |
862 | 0 | { |
863 | 0 | int i = 0; |
864 | |
|
865 | | #ifdef WITH_MEDIACODEC |
866 | | { |
867 | | subSystems[i] = &g_Subsystem_mediacodec; |
868 | | i++; |
869 | | } |
870 | | #endif |
871 | | #if defined(_WIN32) && defined(WITH_MEDIA_FOUNDATION) |
872 | | { |
873 | | subSystems[i] = &g_Subsystem_MF; |
874 | | i++; |
875 | | } |
876 | | #endif |
877 | | #ifdef WITH_APPLE_VIDEOTOOLBOX |
878 | | { |
879 | | subSystems[i] = &g_Subsystem_VideoToolbox; |
880 | | i++; |
881 | | } |
882 | | #endif |
883 | | #ifdef WITH_OPENH264 |
884 | | { |
885 | | subSystems[i] = &g_Subsystem_OpenH264; |
886 | | i++; |
887 | | } |
888 | | #endif |
889 | | #ifdef WITH_VIDEO_FFMPEG |
890 | | { |
891 | | subSystems[i] = &g_Subsystem_libavcodec; |
892 | | i++; |
893 | | } |
894 | | #endif |
895 | | #ifdef WITH_H264_WEBCODECS |
896 | | { |
897 | | subSystems[i] = &g_Subsystem_WebCodecs; |
898 | | i++; |
899 | | } |
900 | | #endif |
901 | 0 | return i > 0; |
902 | 0 | } |
903 | | |
904 | | WINPR_ATTR_NODISCARD |
905 | | static BOOL h264_context_init(H264_CONTEXT* h264) |
906 | 0 | { |
907 | 0 | if (!h264) |
908 | 0 | return FALSE; |
909 | | |
910 | 0 | h264->subsystem = nullptr; |
911 | 0 | if (!InitOnceExecuteOnce(&subsystems_once, h264_register_subsystems, nullptr, nullptr)) |
912 | 0 | return FALSE; |
913 | | |
914 | 0 | for (size_t i = 0; i < MAX_SUBSYSTEMS; i++) |
915 | 0 | { |
916 | 0 | const H264_CONTEXT_SUBSYSTEM* subsystem = subSystems[i]; |
917 | |
|
918 | 0 | if (!subsystem || !subsystem->Init) |
919 | 0 | break; |
920 | | |
921 | 0 | if (subsystem->Init(h264)) |
922 | 0 | { |
923 | 0 | h264->subsystem = subsystem; |
924 | 0 | return TRUE; |
925 | 0 | } |
926 | 0 | } |
927 | | |
928 | 0 | return FALSE; |
929 | 0 | } |
930 | | |
931 | | static void h264_buffers_free(H264_CONTEXT* h264) |
932 | 0 | { |
933 | 0 | WINPR_ASSERT(h264); |
934 | 0 | for (size_t x = 0; x < 3; x++) |
935 | 0 | { |
936 | 0 | if (h264->Compressor) |
937 | 0 | { |
938 | 0 | winpr_aligned_free(h264->pYUVData[x]); |
939 | 0 | winpr_aligned_free(h264->pOldYUVData[x]); |
940 | 0 | } |
941 | 0 | winpr_aligned_free(h264->pYUV444Data[x]); |
942 | 0 | winpr_aligned_free(h264->pOldYUV444Data[x]); |
943 | |
|
944 | 0 | h264->pYUVData[x] = nullptr; |
945 | 0 | h264->pOldYUVData[x] = nullptr; |
946 | 0 | h264->pYUV444Data[x] = nullptr; |
947 | 0 | h264->pOldYUV444Data[x] = nullptr; |
948 | 0 | } |
949 | 0 | winpr_aligned_free(h264->lumaData); |
950 | 0 | h264->lumaData = nullptr; |
951 | 0 | } |
952 | | |
953 | | BOOL h264_context_reset(H264_CONTEXT* h264, UINT32 width, UINT32 height) |
954 | 0 | { |
955 | 0 | if (!h264) |
956 | 0 | return FALSE; |
957 | | |
958 | 0 | if (width > UINT16_MAX) |
959 | 0 | { |
960 | 0 | WLog_Print(h264->log, WLOG_ERROR, "h264::width %" PRIu32 " > %d, aborting", width, |
961 | 0 | UINT16_MAX); |
962 | 0 | return FALSE; |
963 | 0 | } |
964 | 0 | if (height > UINT16_MAX) |
965 | 0 | { |
966 | 0 | WLog_Print(h264->log, WLOG_ERROR, "h264::height %" PRIu32 " > %d, aborting", height, |
967 | 0 | UINT16_MAX); |
968 | 0 | return FALSE; |
969 | 0 | } |
970 | | |
971 | 0 | if (h264->subsystem && h264->subsystem->Uninit) |
972 | 0 | h264->subsystem->Uninit(h264); |
973 | |
|
974 | 0 | h264_buffers_free(h264); |
975 | 0 | h264->width = width; |
976 | 0 | h264->height = height; |
977 | |
|
978 | 0 | if (!h264_context_init(h264)) |
979 | 0 | return FALSE; |
980 | | |
981 | 0 | return yuv_context_reset(h264->yuv, width, height); |
982 | 0 | } |
983 | | |
984 | | H264_CONTEXT* h264_context_new(BOOL Compressor) |
985 | 0 | { |
986 | 0 | H264_CONTEXT* h264 = (H264_CONTEXT*)calloc(1, sizeof(H264_CONTEXT)); |
987 | 0 | if (!h264) |
988 | 0 | return nullptr; |
989 | | |
990 | 0 | h264->log = WLog_Get(TAG); |
991 | |
|
992 | 0 | if (!h264->log) |
993 | 0 | goto fail; |
994 | | |
995 | 0 | h264->Compressor = Compressor; |
996 | 0 | if (Compressor) |
997 | 0 | { |
998 | | /* Default compressor settings, may be changed by caller */ |
999 | 0 | h264->BitRate = 1000000; |
1000 | 0 | h264->FrameRate = 30; |
1001 | 0 | } |
1002 | |
|
1003 | 0 | if (!h264_context_init(h264)) |
1004 | 0 | goto fail; |
1005 | | |
1006 | 0 | h264->yuv = yuv_context_new(Compressor, 0); |
1007 | 0 | if (!h264->yuv) |
1008 | 0 | goto fail; |
1009 | | |
1010 | 0 | return h264; |
1011 | | |
1012 | 0 | fail: |
1013 | 0 | WINPR_PRAGMA_DIAG_PUSH |
1014 | 0 | WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC |
1015 | 0 | h264_context_free(h264); |
1016 | 0 | WINPR_PRAGMA_DIAG_POP |
1017 | 0 | return nullptr; |
1018 | 0 | } |
1019 | | |
1020 | | void h264_context_free(H264_CONTEXT* h264) |
1021 | 0 | { |
1022 | 0 | if (!h264) |
1023 | 0 | return; |
1024 | | |
1025 | 0 | if (h264->subsystem) |
1026 | 0 | { |
1027 | 0 | WINPR_ASSERT(h264->subsystem->Uninit); |
1028 | 0 | h264->subsystem->Uninit(h264); |
1029 | 0 | } |
1030 | |
|
1031 | 0 | h264_buffers_free(h264); |
1032 | |
|
1033 | 0 | yuv_context_free(h264->yuv); |
1034 | 0 | free(h264); |
1035 | 0 | } |
1036 | | |
1037 | | void free_h264_metablock(RDPGFX_H264_METABLOCK* meta) |
1038 | 0 | { |
1039 | 0 | RDPGFX_H264_METABLOCK m = WINPR_C_ARRAY_INIT; |
1040 | 0 | if (!meta) |
1041 | 0 | return; |
1042 | 0 | free(meta->quantQualityVals); |
1043 | 0 | free(meta->regionRects); |
1044 | 0 | *meta = m; |
1045 | 0 | } |
1046 | | |
1047 | | BOOL h264_context_set_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option, UINT32 value) |
1048 | 0 | { |
1049 | 0 | WINPR_ASSERT(h264); |
1050 | 0 | switch (option) |
1051 | 0 | { |
1052 | 0 | case H264_CONTEXT_OPTION_BITRATE: |
1053 | 0 | h264->BitRate = value; |
1054 | 0 | return TRUE; |
1055 | 0 | case H264_CONTEXT_OPTION_FRAMERATE: |
1056 | 0 | h264->FrameRate = value; |
1057 | 0 | return TRUE; |
1058 | 0 | case H264_CONTEXT_OPTION_RATECONTROL: |
1059 | 0 | { |
1060 | 0 | switch (value) |
1061 | 0 | { |
1062 | 0 | case H264_RATECONTROL_VBR: |
1063 | 0 | h264->RateControlMode = H264_RATECONTROL_VBR; |
1064 | 0 | return TRUE; |
1065 | 0 | case H264_RATECONTROL_CQP: |
1066 | 0 | h264->RateControlMode = H264_RATECONTROL_CQP; |
1067 | 0 | return TRUE; |
1068 | 0 | default: |
1069 | 0 | WLog_Print(h264->log, WLOG_WARN, |
1070 | 0 | "Unknown H264_CONTEXT_OPTION_RATECONTROL value [0x%08" PRIx32 "]", |
1071 | 0 | value); |
1072 | 0 | return FALSE; |
1073 | 0 | } |
1074 | 0 | return TRUE; |
1075 | 0 | } |
1076 | 0 | case H264_CONTEXT_OPTION_QP: |
1077 | 0 | h264->QP = value; |
1078 | 0 | return TRUE; |
1079 | 0 | case H264_CONTEXT_OPTION_USAGETYPE: |
1080 | 0 | h264->UsageType = value; |
1081 | 0 | return TRUE; |
1082 | 0 | case H264_CONTEXT_OPTION_HW_ACCEL: |
1083 | | #ifdef WITH_APPLE_VIDEOTOOLBOX |
1084 | | /* VideoToolbox follows Apple's decoder-selection policy; |
1085 | | * ignore this option without reinitializing the decoder. */ |
1086 | | if (h264->subsystem == &g_Subsystem_VideoToolbox) |
1087 | | return TRUE; |
1088 | | #endif |
1089 | 0 | if (h264->hwAccel == value) |
1090 | 0 | return TRUE; |
1091 | 0 | h264->hwAccel = (value); |
1092 | 0 | IFCALL(h264->subsystem->Uninit, h264); |
1093 | 0 | return IFCALLRESULT(TRUE, h264->subsystem->Init, h264); |
1094 | 0 | default: |
1095 | 0 | WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]", |
1096 | 0 | option); |
1097 | 0 | return FALSE; |
1098 | 0 | } |
1099 | 0 | } |
1100 | | |
1101 | | UINT32 h264_context_get_option(H264_CONTEXT* h264, H264_CONTEXT_OPTION option) |
1102 | 0 | { |
1103 | 0 | WINPR_ASSERT(h264); |
1104 | 0 | switch (option) |
1105 | 0 | { |
1106 | 0 | case H264_CONTEXT_OPTION_BITRATE: |
1107 | 0 | return h264->BitRate; |
1108 | 0 | case H264_CONTEXT_OPTION_FRAMERATE: |
1109 | 0 | return h264->FrameRate; |
1110 | 0 | case H264_CONTEXT_OPTION_RATECONTROL: |
1111 | 0 | return h264->RateControlMode; |
1112 | 0 | case H264_CONTEXT_OPTION_QP: |
1113 | 0 | return h264->QP; |
1114 | 0 | case H264_CONTEXT_OPTION_USAGETYPE: |
1115 | 0 | return h264->UsageType; |
1116 | 0 | case H264_CONTEXT_OPTION_HW_ACCEL: |
1117 | 0 | return h264->hwAccel; |
1118 | 0 | default: |
1119 | 0 | WLog_Print(h264->log, WLOG_WARN, "Unknown H264_CONTEXT_OPTION[0x%08" PRIx32 "]", |
1120 | 0 | option); |
1121 | 0 | return 0; |
1122 | 0 | } |
1123 | 0 | } |