Coverage Report

Created: 2026-06-15 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/rdpgfx/client/rdpgfx_codec.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Graphics Pipeline Extension
4
 *
5
 * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <winpr/crt.h>
25
#include <winpr/stream.h>
26
#include <freerdp/log.h>
27
#include <freerdp/utils/profiler.h>
28
29
#include "rdpgfx_common.h"
30
31
#include "rdpgfx_codec.h"
32
33
/**
34
 * Function description
35
 *
36
 * @return 0 on success, otherwise a Win32 error code
37
 */
38
static UINT rdpgfx_read_h264_metablock(WINPR_ATTR_UNUSED RDPGFX_PLUGIN* gfx, wStream* s,
39
                                       RDPGFX_H264_METABLOCK* meta)
40
0
{
41
0
  RECTANGLE_16* regionRect = nullptr;
42
0
  RDPGFX_H264_QUANT_QUALITY* quantQualityVal = nullptr;
43
0
  UINT error = ERROR_INVALID_DATA;
44
0
  meta->regionRects = nullptr;
45
0
  meta->quantQualityVals = nullptr;
46
47
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 4))
48
0
    goto error_out;
49
50
0
  Stream_Read_UINT32(s, meta->numRegionRects); /* numRegionRects (4 bytes) */
51
52
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, meta->numRegionRects, 8ull))
53
0
    goto error_out;
54
55
0
  meta->regionRects = (RECTANGLE_16*)calloc(meta->numRegionRects, sizeof(RECTANGLE_16));
56
57
0
  if (!meta->regionRects)
58
0
  {
59
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "malloc failed!");
60
0
    error = CHANNEL_RC_NO_MEMORY;
61
0
    goto error_out;
62
0
  }
63
64
0
  meta->quantQualityVals =
65
0
      (RDPGFX_H264_QUANT_QUALITY*)calloc(meta->numRegionRects, sizeof(RDPGFX_H264_QUANT_QUALITY));
66
67
0
  if (!meta->quantQualityVals)
68
0
  {
69
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "malloc failed!");
70
0
    error = CHANNEL_RC_NO_MEMORY;
71
0
    goto error_out;
72
0
  }
73
74
0
  WLog_Print(gfx->base.log, WLOG_TRACE, "H264_METABLOCK: numRegionRects: %" PRIu32 "",
75
0
             meta->numRegionRects);
76
77
0
  for (UINT32 index = 0; index < meta->numRegionRects; index++)
78
0
  {
79
0
    regionRect = &(meta->regionRects[index]);
80
81
0
    if ((error = rdpgfx_read_rect16(gfx->base.log, s, regionRect)))
82
0
    {
83
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
84
0
                 "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error);
85
0
      goto error_out;
86
0
    }
87
88
0
    WLog_Print(gfx->base.log, WLOG_TRACE,
89
0
               "regionRects[%" PRIu32 "]: left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16
90
0
               " bottom: %" PRIu16 "",
91
0
               index, regionRect->left, regionRect->top, regionRect->right, regionRect->bottom);
92
0
  }
93
94
0
  if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, meta->numRegionRects, 2ull))
95
0
  {
96
0
    error = ERROR_INVALID_DATA;
97
0
    goto error_out;
98
0
  }
99
100
0
  for (UINT32 index = 0; index < meta->numRegionRects; index++)
101
0
  {
102
0
    quantQualityVal = &(meta->quantQualityVals[index]);
103
0
    Stream_Read_UINT8(s, quantQualityVal->qpVal);      /* qpVal (1 byte) */
104
0
    Stream_Read_UINT8(s, quantQualityVal->qualityVal); /* qualityVal (1 byte) */
105
0
    quantQualityVal->qp = quantQualityVal->qpVal & 0x3F;
106
0
    quantQualityVal->r = (quantQualityVal->qpVal >> 6) & 1;
107
0
    quantQualityVal->p = (quantQualityVal->qpVal >> 7) & 1;
108
0
    WLog_Print(gfx->base.log, WLOG_TRACE,
109
0
               "quantQualityVals[%" PRIu32 "]: qp: %" PRIu8 " r: %" PRIu8 " p: %" PRIu8
110
0
               " qualityVal: %" PRIu8 "",
111
0
               index, quantQualityVal->qp, quantQualityVal->r, quantQualityVal->p,
112
0
               quantQualityVal->qualityVal);
113
0
  }
114
115
0
  return CHANNEL_RC_OK;
116
0
error_out:
117
0
  free_h264_metablock(meta);
118
0
  return error;
119
0
}
120
121
/**
122
 * Function description
123
 *
124
 * @return 0 on success, otherwise a Win32 error code
125
 */
126
static UINT rdpgfx_decode_AVC420(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
127
0
{
128
0
  RDPGFX_AVC420_BITMAP_STREAM h264 = WINPR_C_ARRAY_INIT;
129
0
  wStream* s = Stream_New(cmd->data, cmd->length);
130
131
0
  if (!s)
132
0
  {
133
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
134
0
    return CHANNEL_RC_NO_MEMORY;
135
0
  }
136
137
0
  UINT error = rdpgfx_read_h264_metablock(gfx, s, &(h264.meta));
138
0
  if (error != CHANNEL_RC_OK)
139
0
  {
140
0
    Stream_Free(s, FALSE);
141
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
142
0
               "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error);
143
0
    return error;
144
0
  }
145
146
0
  h264.data = Stream_Pointer(s);
147
0
  h264.length = (UINT32)Stream_GetRemainingLength(s);
148
0
  Stream_Free(s, FALSE);
149
0
  cmd->extra = (void*)&h264;
150
151
0
  error = logSurfaceCommand(gfx, cmd);
152
153
0
  free_h264_metablock(&h264.meta);
154
0
  cmd->extra = nullptr;
155
0
  return error;
156
0
}
157
158
/**
159
 * Function description
160
 *
161
 * @return 0 on success, otherwise a Win32 error code
162
 */
163
static UINT rdpgfx_decode_AVC444(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
164
0
{
165
0
  UINT error = CHANNEL_RC_OK;
166
0
  UINT32 tmp = 0;
167
0
  size_t pos1 = 0;
168
0
  size_t pos2 = 0;
169
170
0
  RDPGFX_AVC444_BITMAP_STREAM h264 = WINPR_C_ARRAY_INIT;
171
0
  wStream* s = Stream_New(cmd->data, cmd->length);
172
173
0
  if (!s)
174
0
  {
175
0
    WLog_Print(gfx->base.log, WLOG_ERROR, "Stream_New failed!");
176
0
    return CHANNEL_RC_NO_MEMORY;
177
0
  }
178
179
0
  if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 4))
180
0
  {
181
0
    error = ERROR_INVALID_DATA;
182
0
    goto fail;
183
0
  }
184
185
0
  Stream_Read_UINT32(s, tmp);
186
0
  h264.cbAvc420EncodedBitstream1 = tmp & 0x3FFFFFFFUL;
187
0
  h264.LC = (tmp >> 30UL) & 0x03UL;
188
189
0
  if (h264.LC == 0x03)
190
0
  {
191
0
    error = ERROR_INVALID_DATA;
192
0
    goto fail;
193
0
  }
194
195
0
  pos1 = Stream_GetPosition(s);
196
197
0
  if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.bitstream[0].meta))))
198
0
  {
199
0
    WLog_Print(gfx->base.log, WLOG_ERROR,
200
0
               "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error);
201
0
    goto fail;
202
0
  }
203
204
0
  pos2 = Stream_GetPosition(s);
205
0
  h264.bitstream[0].data = Stream_Pointer(s);
206
207
0
  if (h264.LC == 0)
208
0
  {
209
0
    const size_t bitstreamLen = 1ULL * h264.cbAvc420EncodedBitstream1 - pos2 + pos1;
210
211
0
    if ((bitstreamLen > UINT32_MAX) || (h264.cbAvc420EncodedBitstream1 < pos2 - pos1) ||
212
0
        !Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, bitstreamLen))
213
0
    {
214
0
      error = ERROR_INVALID_DATA;
215
0
      goto fail;
216
0
    }
217
218
0
    h264.bitstream[0].length = (UINT32)bitstreamLen;
219
0
    Stream_Seek(s, bitstreamLen);
220
221
0
    if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.bitstream[1].meta))))
222
0
    {
223
0
      WLog_Print(gfx->base.log, WLOG_ERROR,
224
0
                 "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error);
225
0
      goto fail;
226
0
    }
227
228
0
    h264.bitstream[1].data = Stream_Pointer(s);
229
230
0
    const size_t len = Stream_GetRemainingLength(s);
231
0
    if (len > UINT32_MAX)
232
0
      goto fail;
233
0
    h264.bitstream[1].length = (UINT32)len;
234
0
  }
235
0
  else
236
0
  {
237
0
    const size_t len = Stream_GetRemainingLength(s);
238
0
    if (len > UINT32_MAX)
239
0
      goto fail;
240
0
    h264.bitstream[0].length = (UINT32)len;
241
0
  }
242
243
0
  cmd->extra = (void*)&h264;
244
245
0
  error = logSurfaceCommand(gfx, cmd);
246
247
0
fail:
248
0
  Stream_Free(s, FALSE);
249
0
  free_h264_metablock(&h264.bitstream[0].meta);
250
0
  free_h264_metablock(&h264.bitstream[1].meta);
251
0
  cmd->extra = nullptr;
252
0
  return error;
253
0
}
254
255
/**
256
 * Function description
257
 *
258
 * @return 0 on success, otherwise a Win32 error code
259
 */
260
UINT rdpgfx_decode(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
261
0
{
262
0
  UINT error = CHANNEL_RC_OK;
263
0
  PROFILER_ENTER(context->SurfaceProfiler)
264
265
0
  switch (cmd->codecId)
266
0
  {
267
#if defined(WITH_GFX_AV1)
268
    case RDPGFX_CODECID_AV1:
269
#endif
270
0
    case RDPGFX_CODECID_AVC420:
271
0
      if ((error = rdpgfx_decode_AVC420(gfx, cmd)))
272
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
273
0
                   "rdpgfx_decode_AVC420 failed with error %" PRIu32 "", error);
274
275
0
      break;
276
277
0
    case RDPGFX_CODECID_AVC444:
278
0
    case RDPGFX_CODECID_AVC444v2:
279
0
      if ((error = rdpgfx_decode_AVC444(gfx, cmd)))
280
0
        WLog_Print(gfx->base.log, WLOG_ERROR,
281
0
                   "rdpgfx_decode_AVC444 failed with error %" PRIu32 "", error);
282
283
0
      break;
284
285
0
    default:
286
0
      error = logSurfaceCommand(gfx, cmd);
287
0
      break;
288
0
  }
289
290
0
  PROFILER_EXIT(context->SurfaceProfiler)
291
0
  return error;
292
0
}