/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 | 1.17k | { |
41 | 1.17k | RECTANGLE_16* regionRect = nullptr; |
42 | 1.17k | RDPGFX_H264_QUANT_QUALITY* quantQualityVal = nullptr; |
43 | 1.17k | UINT error = ERROR_INVALID_DATA; |
44 | 1.17k | meta->regionRects = nullptr; |
45 | 1.17k | meta->quantQualityVals = nullptr; |
46 | | |
47 | 1.17k | if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 4)) |
48 | 4 | goto error_out; |
49 | | |
50 | 1.17k | Stream_Read_UINT32(s, meta->numRegionRects); /* numRegionRects (4 bytes) */ |
51 | | |
52 | 1.17k | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, meta->numRegionRects, 8ull)) |
53 | 19 | goto error_out; |
54 | | |
55 | 1.15k | meta->regionRects = (RECTANGLE_16*)calloc(meta->numRegionRects, sizeof(RECTANGLE_16)); |
56 | | |
57 | 1.15k | 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 | 1.15k | meta->quantQualityVals = |
65 | 1.15k | (RDPGFX_H264_QUANT_QUALITY*)calloc(meta->numRegionRects, sizeof(RDPGFX_H264_QUANT_QUALITY)); |
66 | | |
67 | 1.15k | 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 | 1.15k | WLog_Print(gfx->base.log, WLOG_TRACE, "H264_METABLOCK: numRegionRects: %" PRIu32 "", |
75 | 1.15k | meta->numRegionRects); |
76 | | |
77 | 1.22k | for (UINT32 index = 0; index < meta->numRegionRects; index++) |
78 | 89 | { |
79 | 89 | regionRect = &(meta->regionRects[index]); |
80 | | |
81 | 89 | if ((error = rdpgfx_read_rect16(gfx->base.log, s, regionRect))) |
82 | 13 | { |
83 | 13 | WLog_Print(gfx->base.log, WLOG_ERROR, |
84 | 13 | "rdpgfx_read_rect16 failed with error %" PRIu32 "!", error); |
85 | 13 | goto error_out; |
86 | 13 | } |
87 | | |
88 | 76 | WLog_Print(gfx->base.log, WLOG_TRACE, |
89 | 76 | "regionRects[%" PRIu32 "]: left: %" PRIu16 " top: %" PRIu16 " right: %" PRIu16 |
90 | 76 | " bottom: %" PRIu16 "", |
91 | 76 | index, regionRect->left, regionRect->top, regionRect->right, regionRect->bottom); |
92 | 76 | } |
93 | | |
94 | 1.13k | if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(gfx->base.log, s, meta->numRegionRects, 2ull)) |
95 | 3 | { |
96 | 3 | error = ERROR_INVALID_DATA; |
97 | 3 | goto error_out; |
98 | 3 | } |
99 | | |
100 | 1.18k | for (UINT32 index = 0; index < meta->numRegionRects; index++) |
101 | 51 | { |
102 | 51 | quantQualityVal = &(meta->quantQualityVals[index]); |
103 | 51 | Stream_Read_UINT8(s, quantQualityVal->qpVal); /* qpVal (1 byte) */ |
104 | 51 | Stream_Read_UINT8(s, quantQualityVal->qualityVal); /* qualityVal (1 byte) */ |
105 | 51 | quantQualityVal->qp = quantQualityVal->qpVal & 0x3F; |
106 | 51 | quantQualityVal->r = (quantQualityVal->qpVal >> 6) & 1; |
107 | 51 | quantQualityVal->p = (quantQualityVal->qpVal >> 7) & 1; |
108 | 51 | WLog_Print(gfx->base.log, WLOG_TRACE, |
109 | 51 | "quantQualityVals[%" PRIu32 "]: qp: %" PRIu8 " r: %" PRIu8 " p: %" PRIu8 |
110 | 51 | " qualityVal: %" PRIu8 "", |
111 | 51 | index, quantQualityVal->qp, quantQualityVal->r, quantQualityVal->p, |
112 | 51 | quantQualityVal->qualityVal); |
113 | 51 | } |
114 | | |
115 | 1.13k | return CHANNEL_RC_OK; |
116 | 39 | error_out: |
117 | 39 | free_h264_metablock(meta); |
118 | 39 | return error; |
119 | 1.13k | } |
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 | 10 | { |
128 | 10 | RDPGFX_AVC420_BITMAP_STREAM h264 = WINPR_C_ARRAY_INIT; |
129 | 10 | wStream* s = Stream_New(cmd->data, cmd->length); |
130 | | |
131 | 10 | 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 | 10 | UINT error = rdpgfx_read_h264_metablock(gfx, s, &(h264.meta)); |
138 | 10 | if (error != CHANNEL_RC_OK) |
139 | 5 | { |
140 | 5 | Stream_Free(s, FALSE); |
141 | 5 | WLog_Print(gfx->base.log, WLOG_ERROR, |
142 | 5 | "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error); |
143 | 5 | return error; |
144 | 5 | } |
145 | | |
146 | 5 | h264.data = Stream_Pointer(s); |
147 | 5 | h264.length = (UINT32)Stream_GetRemainingLength(s); |
148 | 5 | Stream_Free(s, FALSE); |
149 | 5 | cmd->extra = (void*)&h264; |
150 | | |
151 | 5 | error = logSurfaceCommand(gfx, cmd); |
152 | | |
153 | 5 | free_h264_metablock(&h264.meta); |
154 | 5 | cmd->extra = nullptr; |
155 | 5 | return error; |
156 | 10 | } |
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 | 1.11k | { |
165 | 1.11k | UINT error = CHANNEL_RC_OK; |
166 | 1.11k | UINT32 tmp = 0; |
167 | 1.11k | size_t pos1 = 0; |
168 | 1.11k | size_t pos2 = 0; |
169 | | |
170 | 1.11k | RDPGFX_AVC444_BITMAP_STREAM h264 = WINPR_C_ARRAY_INIT; |
171 | 1.11k | wStream* s = Stream_New(cmd->data, cmd->length); |
172 | | |
173 | 1.11k | 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 | 1.11k | if (!Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, 4)) |
180 | 3 | { |
181 | 3 | error = ERROR_INVALID_DATA; |
182 | 3 | goto fail; |
183 | 3 | } |
184 | | |
185 | 1.11k | Stream_Read_UINT32(s, tmp); |
186 | 1.11k | h264.cbAvc420EncodedBitstream1 = tmp & 0x3FFFFFFFUL; |
187 | 1.11k | h264.LC = (tmp >> 30UL) & 0x03UL; |
188 | | |
189 | 1.11k | if (h264.LC == 0x03) |
190 | 6 | { |
191 | 6 | error = ERROR_INVALID_DATA; |
192 | 6 | goto fail; |
193 | 6 | } |
194 | | |
195 | 1.10k | pos1 = Stream_GetPosition(s); |
196 | | |
197 | 1.10k | if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.bitstream[0].meta)))) |
198 | 20 | { |
199 | 20 | WLog_Print(gfx->base.log, WLOG_ERROR, |
200 | 20 | "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error); |
201 | 20 | goto fail; |
202 | 20 | } |
203 | | |
204 | 1.08k | pos2 = Stream_GetPosition(s); |
205 | 1.08k | h264.bitstream[0].data = Stream_Pointer(s); |
206 | | |
207 | 1.08k | if (h264.LC == 0) |
208 | 98 | { |
209 | 98 | const size_t bitstreamLen = 1ULL * h264.cbAvc420EncodedBitstream1 - pos2 + pos1; |
210 | | |
211 | 98 | if ((bitstreamLen > UINT32_MAX) || (h264.cbAvc420EncodedBitstream1 < pos2 - pos1) || |
212 | 91 | !Stream_CheckAndLogRequiredLengthWLog(gfx->base.log, s, bitstreamLen)) |
213 | 39 | { |
214 | 39 | error = ERROR_INVALID_DATA; |
215 | 39 | goto fail; |
216 | 39 | } |
217 | | |
218 | 59 | h264.bitstream[0].length = (UINT32)bitstreamLen; |
219 | 59 | Stream_Seek(s, bitstreamLen); |
220 | | |
221 | 59 | if ((error = rdpgfx_read_h264_metablock(gfx, s, &(h264.bitstream[1].meta)))) |
222 | 14 | { |
223 | 14 | WLog_Print(gfx->base.log, WLOG_ERROR, |
224 | 14 | "rdpgfx_read_h264_metablock failed with error %" PRIu32 "!", error); |
225 | 14 | goto fail; |
226 | 14 | } |
227 | | |
228 | 45 | h264.bitstream[1].data = Stream_Pointer(s); |
229 | | |
230 | 45 | const size_t len = Stream_GetRemainingLength(s); |
231 | 45 | if (len > UINT32_MAX) |
232 | 0 | goto fail; |
233 | 45 | h264.bitstream[1].length = (UINT32)len; |
234 | 45 | } |
235 | 987 | else |
236 | 987 | { |
237 | 987 | const size_t len = Stream_GetRemainingLength(s); |
238 | 987 | if (len > UINT32_MAX) |
239 | 0 | goto fail; |
240 | 987 | h264.bitstream[0].length = (UINT32)len; |
241 | 987 | } |
242 | | |
243 | 1.03k | cmd->extra = (void*)&h264; |
244 | | |
245 | 1.03k | error = logSurfaceCommand(gfx, cmd); |
246 | | |
247 | 1.11k | fail: |
248 | 1.11k | Stream_Free(s, FALSE); |
249 | 1.11k | free_h264_metablock(&h264.bitstream[0].meta); |
250 | 1.11k | free_h264_metablock(&h264.bitstream[1].meta); |
251 | 1.11k | cmd->extra = nullptr; |
252 | 1.11k | return error; |
253 | 1.03k | } |
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 | 1.33k | { |
262 | 1.33k | UINT error = CHANNEL_RC_OK; |
263 | 1.33k | WINPR_ASSERT(gfx); |
264 | 1.33k | WINPR_ASSERT(cmd); |
265 | | |
266 | 1.33k | PROFILER_ENTER(gfx->context->SurfaceProfiler) |
267 | | |
268 | 1.33k | switch (cmd->codecId) |
269 | 1.33k | { |
270 | | #if defined(WITH_GFX_AV1) |
271 | | case RDPGFX_CODECID_AV1: |
272 | | #endif |
273 | 10 | case RDPGFX_CODECID_AVC420: |
274 | 10 | if ((error = rdpgfx_decode_AVC420(gfx, cmd))) |
275 | 5 | WLog_Print(gfx->base.log, WLOG_ERROR, |
276 | 10 | "rdpgfx_decode_AVC420 failed with error %" PRIu32 "", error); |
277 | | |
278 | 10 | break; |
279 | | |
280 | 733 | case RDPGFX_CODECID_AVC444: |
281 | 1.11k | case RDPGFX_CODECID_AVC444v2: |
282 | 1.11k | if ((error = rdpgfx_decode_AVC444(gfx, cmd))) |
283 | 82 | WLog_Print(gfx->base.log, WLOG_ERROR, |
284 | 1.11k | "rdpgfx_decode_AVC444 failed with error %" PRIu32 "", error); |
285 | | |
286 | 1.11k | break; |
287 | | |
288 | 211 | default: |
289 | 211 | error = logSurfaceCommand(gfx, cmd); |
290 | 211 | break; |
291 | 1.33k | } |
292 | | |
293 | 1.33k | PROFILER_EXIT(gfx->context->SurfaceProfiler) |
294 | 1.33k | return error; |
295 | 1.33k | } |