Coverage Report

Created: 2026-03-04 06:17

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