Coverage Report

Created: 2026-09-14 06:32

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