Coverage Report

Created: 2025-07-01 06:46

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