Coverage Report

Created: 2024-05-20 06:11

/src/FreeRDP/channels/rdpgfx/client/rdpgfx_codec.c
Line
Count
Source (jump to first uncovered line)
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
#define TAG CHANNELS_TAG("rdpgfx.client")
34
35
/**
36
 * Function description
37
 *
38
 * @return 0 on success, otherwise a Win32 error code
39
 */
40
static UINT rdpgfx_read_h264_metablock(RDPGFX_PLUGIN* gfx, wStream* s, RDPGFX_H264_METABLOCK* meta)
41
0
{
42
0
  RECTANGLE_16* regionRect = NULL;
43
0
  RDPGFX_H264_QUANT_QUALITY* quantQualityVal = NULL;
44
0
  UINT error = ERROR_INVALID_DATA;
45
0
  meta->regionRects = NULL;
46
0
  meta->quantQualityVals = NULL;
47
48
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
49
0
    goto error_out;
50
51
0
  Stream_Read_UINT32(s, meta->numRegionRects); /* numRegionRects (4 bytes) */
52
53
0
  if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, meta->numRegionRects, 8ull))
54
0
    goto error_out;
55
56
0
  meta->regionRects = (RECTANGLE_16*)calloc(meta->numRegionRects, sizeof(RECTANGLE_16));
57
58
0
  if (!meta->regionRects)
59
0
  {
60
0
    WLog_ERR(TAG, "malloc failed!");
61
0
    error = CHANNEL_RC_NO_MEMORY;
62
0
    goto error_out;
63
0
  }
64
65
0
  meta->quantQualityVals =
66
0
      (RDPGFX_H264_QUANT_QUALITY*)calloc(meta->numRegionRects, sizeof(RDPGFX_H264_QUANT_QUALITY));
67
68
0
  if (!meta->quantQualityVals)
69
0
  {
70
0
    WLog_ERR(TAG, "malloc failed!");
71
0
    error = CHANNEL_RC_NO_MEMORY;
72
0
    goto error_out;
73
0
  }
74
75
0
  WLog_DBG(TAG, "H264_METABLOCK: numRegionRects: %" PRIu32 "", 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(s, regionRect)))
82
0
    {
83
0
      WLog_ERR(TAG, "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error);
84
0
      goto error_out;
85
0
    }
86
87
0
    WLog_DBG(TAG,
88
0
             "regionRects[%" PRIu32 "]: left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16
89
0
             " bottom: %" PRIu16 "",
90
0
             index, regionRect->left, regionRect->top, regionRect->right, regionRect->bottom);
91
0
  }
92
93
0
  if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, meta->numRegionRects, 2ull))
94
0
  {
95
0
    error = ERROR_INVALID_DATA;
96
0
    goto error_out;
97
0
  }
98
99
0
  for (UINT32 index = 0; index < meta->numRegionRects; index++)
100
0
  {
101
0
    quantQualityVal = &(meta->quantQualityVals[index]);
102
0
    Stream_Read_UINT8(s, quantQualityVal->qpVal);      /* qpVal (1 byte) */
103
0
    Stream_Read_UINT8(s, quantQualityVal->qualityVal); /* qualityVal (1 byte) */
104
0
    quantQualityVal->qp = quantQualityVal->qpVal & 0x3F;
105
0
    quantQualityVal->r = (quantQualityVal->qpVal >> 6) & 1;
106
0
    quantQualityVal->p = (quantQualityVal->qpVal >> 7) & 1;
107
0
    WLog_DBG(TAG,
108
0
             "quantQualityVals[%" PRIu32 "]: qp: %" PRIu8 " r: %" PRIu8 " p: %" PRIu8
109
0
             " qualityVal: %" PRIu8 "",
110
0
             index, quantQualityVal->qp, quantQualityVal->r, quantQualityVal->p,
111
0
             quantQualityVal->qualityVal);
112
0
  }
113
114
0
  return CHANNEL_RC_OK;
115
0
error_out:
116
0
  free_h264_metablock(meta);
117
0
  return error;
118
0
}
119
120
/**
121
 * Function description
122
 *
123
 * @return 0 on success, otherwise a Win32 error code
124
 */
125
static UINT rdpgfx_decode_AVC420(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
126
0
{
127
0
  UINT error = 0;
128
0
  wStream* s = NULL;
129
0
  RDPGFX_AVC420_BITMAP_STREAM h264;
130
0
  RdpgfxClientContext* context = gfx->context;
131
0
  s = Stream_New(cmd->data, cmd->length);
132
133
0
  if (!s)
134
0
  {
135
0
    WLog_ERR(TAG, "Stream_New failed!");
136
0
    return CHANNEL_RC_NO_MEMORY;
137
0
  }
138
139
0
  if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.meta))))
140
0
  {
141
0
    Stream_Free(s, FALSE);
142
0
    WLog_ERR(TAG, "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
  if (context)
152
0
  {
153
0
    IFCALLRET(context->SurfaceCommand, error, context, cmd);
154
155
0
    if (error)
156
0
      WLog_ERR(TAG, "context->SurfaceCommand failed with error %" PRIu32 "", error);
157
0
  }
158
159
0
  free_h264_metablock(&h264.meta);
160
0
  return error;
161
0
}
162
163
/**
164
 * Function description
165
 *
166
 * @return 0 on success, otherwise a Win32 error code
167
 */
168
static UINT rdpgfx_decode_AVC444(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
169
0
{
170
0
  UINT error = 0;
171
0
  UINT32 tmp = 0;
172
0
  size_t pos1 = 0;
173
0
  size_t pos2 = 0;
174
175
0
  RDPGFX_AVC444_BITMAP_STREAM h264 = { 0 };
176
0
  RdpgfxClientContext* context = gfx->context;
177
0
  wStream* s = Stream_New(cmd->data, cmd->length);
178
179
0
  if (!s)
180
0
  {
181
0
    WLog_ERR(TAG, "Stream_New failed!");
182
0
    return CHANNEL_RC_NO_MEMORY;
183
0
  }
184
185
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
186
0
  {
187
0
    error = ERROR_INVALID_DATA;
188
0
    goto fail;
189
0
  }
190
191
0
  Stream_Read_UINT32(s, tmp);
192
0
  h264.cbAvc420EncodedBitstream1 = tmp & 0x3FFFFFFFUL;
193
0
  h264.LC = (tmp >> 30UL) & 0x03UL;
194
195
0
  if (h264.LC == 0x03)
196
0
  {
197
0
    error = ERROR_INVALID_DATA;
198
0
    goto fail;
199
0
  }
200
201
0
  pos1 = Stream_GetPosition(s);
202
203
0
  if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.bitstream[0].meta))))
204
0
  {
205
0
    WLog_ERR(TAG, "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error);
206
0
    goto fail;
207
0
  }
208
209
0
  pos2 = Stream_GetPosition(s);
210
0
  h264.bitstream[0].data = Stream_Pointer(s);
211
212
0
  if (h264.LC == 0)
213
0
  {
214
0
    tmp = h264.cbAvc420EncodedBitstream1 - pos2 + pos1;
215
216
0
    if (!Stream_CheckAndLogRequiredLength(TAG, s, tmp))
217
0
    {
218
0
      error = ERROR_INVALID_DATA;
219
0
      goto fail;
220
0
    }
221
222
0
    h264.bitstream[0].length = tmp;
223
0
    Stream_Seek(s, tmp);
224
225
0
    if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.bitstream[1].meta))))
226
0
    {
227
0
      WLog_ERR(TAG, "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error);
228
0
      goto fail;
229
0
    }
230
231
0
    h264.bitstream[1].data = Stream_Pointer(s);
232
0
    h264.bitstream[1].length = Stream_GetRemainingLength(s);
233
0
  }
234
0
  else
235
0
    h264.bitstream[0].length = Stream_GetRemainingLength(s);
236
237
0
  cmd->extra = (void*)&h264;
238
239
0
  if (context)
240
0
  {
241
0
    IFCALLRET(context->SurfaceCommand, error, context, cmd);
242
243
0
    if (error)
244
0
      WLog_ERR(TAG, "context->SurfaceCommand failed with error %" PRIu32 "", error);
245
0
  }
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
  return error;
252
0
}
253
254
/**
255
 * Function description
256
 *
257
 * @return 0 on success, otherwise a Win32 error code
258
 */
259
UINT rdpgfx_decode(RDPGFX_PLUGIN* gfx, RDPGFX_SURFACE_COMMAND* cmd)
260
0
{
261
0
  UINT error = CHANNEL_RC_OK;
262
0
  RdpgfxClientContext* context = gfx->context;
263
0
  PROFILER_ENTER(context->SurfaceProfiler)
264
265
0
  switch (cmd->codecId)
266
0
  {
267
0
    case RDPGFX_CODECID_AVC420:
268
0
      if ((error = rdpgfx_decode_AVC420(gfx, cmd)))
269
0
        WLog_ERR(TAG, "rdpgfx_decode_AVC420 failed with error %" PRIu32 "", error);
270
271
0
      break;
272
273
0
    case RDPGFX_CODECID_AVC444:
274
0
    case RDPGFX_CODECID_AVC444v2:
275
0
      if ((error = rdpgfx_decode_AVC444(gfx, cmd)))
276
0
        WLog_ERR(TAG, "rdpgfx_decode_AVC444 failed with error %" PRIu32 "", error);
277
278
0
      break;
279
280
0
    default:
281
0
      if (context)
282
0
      {
283
0
        IFCALLRET(context->SurfaceCommand, error, context, cmd);
284
285
0
        if (error)
286
0
          WLog_ERR(TAG, "context->SurfaceCommand failed with error %" PRIu32 "", error);
287
0
      }
288
289
0
      break;
290
0
  }
291
292
0
  PROFILER_EXIT(context->SurfaceProfiler)
293
0
  return error;
294
0
}