Coverage Report

Created: 2026-07-16 07:14

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