2
* FreeRDP: A Remote Desktop Protocol Implementation
3
* RemoteFX Codec Library - Decode
5
* Copyright 2011 Vic Lee
6
* Copyright 2011 Norbert Federa <nfedera@thinstuff.com>
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
12
* http://www.apache.org/licenses/LICENSE-2.0
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.
29
#include <winpr/stream.h>
30
#include <freerdp/primitives.h>
32
#include "rfx_types.h"
34
#include "rfx_differential.h"
35
#include "rfx_quantization.h"
38
#include "rfx_decode.h"
40
/* stride is bytes between rows in the output buffer. */
41
static void rfx_decode_format_rgb(INT16* r_buf, INT16* g_buf, INT16* b_buf,
42
RDP_PIXEL_FORMAT pixel_format, BYTE* dst_buf, int stride)
44
primitives_t *prims = primitives_get();
49
static const prim_size_t roi_64x64 = { 64, 64 };
55
case RDP_PIXEL_FORMAT_B8G8R8A8:
56
pSrc[0] = r; pSrc[1] = g; pSrc[2] = b;
57
prims->RGBToRGB_16s8u_P3AC4R(
58
(const INT16 **) pSrc, 64*sizeof(INT16),
59
dst, stride, &roi_64x64);
61
case RDP_PIXEL_FORMAT_R8G8B8A8:
62
pSrc[0] = b; pSrc[1] = g; pSrc[2] = r;
63
prims->RGBToRGB_16s8u_P3AC4R(
64
(const INT16 **) pSrc, 64*sizeof(INT16),
65
dst, stride, &roi_64x64);
67
case RDP_PIXEL_FORMAT_B8G8R8:
72
*dst++ = (BYTE) (*b++);
73
*dst++ = (BYTE) (*g++);
74
*dst++ = (BYTE) (*r++);
76
dst += stride - (64*3);
79
case RDP_PIXEL_FORMAT_R8G8B8:
84
*dst++ = (BYTE) (*r++);
85
*dst++ = (BYTE) (*g++);
86
*dst++ = (BYTE) (*b++);
88
dst += stride - (64*3);
96
static void rfx_decode_component(RFX_CONTEXT* context, const UINT32* quantization_values,
97
const BYTE* data, int size, INT16* buffer)
101
dwt_buffer = BufferPool_Take(context->priv->BufferPool, -1); /* dwt_buffer */
103
PROFILER_ENTER(context->priv->prof_rfx_decode_component);
105
PROFILER_ENTER(context->priv->prof_rfx_rlgr_decode);
106
context->rlgr_decode(context->mode, data, size, buffer, 4096);
107
PROFILER_EXIT(context->priv->prof_rfx_rlgr_decode);
109
PROFILER_ENTER(context->priv->prof_rfx_differential_decode);
110
rfx_differential_decode(buffer + 4032, 64);
111
PROFILER_EXIT(context->priv->prof_rfx_differential_decode);
113
PROFILER_ENTER(context->priv->prof_rfx_quantization_decode);
114
context->quantization_decode(buffer, quantization_values);
115
PROFILER_EXIT(context->priv->prof_rfx_quantization_decode);
117
PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_decode);
118
context->dwt_2d_decode(buffer, dwt_buffer);
119
PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_decode);
121
PROFILER_EXIT(context->priv->prof_rfx_decode_component);
123
BufferPool_Return(context->priv->BufferPool, dwt_buffer);
126
/* rfx_decode_ycbcr_to_rgb code now resides in the primitives library. */
128
struct _RFX_COMPONENT_WORK_PARAM
133
RFX_CONTEXT* context;
134
const UINT32* quantization_values;
136
typedef struct _RFX_COMPONENT_WORK_PARAM RFX_COMPONENT_WORK_PARAM;
138
void CALLBACK rfx_decode_component_work_callback(PTP_CALLBACK_INSTANCE instance, void* context, PTP_WORK work)
140
RFX_COMPONENT_WORK_PARAM* param = (RFX_COMPONENT_WORK_PARAM*) context;
141
rfx_decode_component(param->context, param->quantization_values, param->data, param->size, param->buffer);
144
/* stride is bytes between rows in the output buffer. */
145
BOOL rfx_decode_rgb(RFX_CONTEXT* context, wStream* data_in,
146
int y_size, const UINT32* y_quants,
147
int cb_size, const UINT32* cb_quants,
148
int cr_size, const UINT32* cr_quants, BYTE* rgb_buffer, int stride)
151
static const prim_size_t roi_64x64 = { 64, 64 };
152
const primitives_t *prims = primitives_get();
154
PROFILER_ENTER(context->priv->prof_rfx_decode_rgb);
156
pSrcDst[0] = (INT16*)((BYTE*)BufferPool_Take(context->priv->BufferPool, -1) + 16); /* y_r_buffer */
157
pSrcDst[1] = (INT16*)((BYTE*)BufferPool_Take(context->priv->BufferPool, -1) + 16); /* cb_g_buffer */
158
pSrcDst[2] = (INT16*)((BYTE*)BufferPool_Take(context->priv->BufferPool, -1) + 16); /* cr_b_buffer */
161
if (context->priv->UseThreads)
163
PTP_WORK work_objects[3];
164
RFX_COMPONENT_WORK_PARAM params[3];
166
params[0].context = context;
167
params[0].quantization_values = y_quants;
168
params[0].buffer = Stream_Pointer(data_in);
169
params[0].capacity = y_size;
170
params[0].buffer = pSrcDst[0];
171
Stream_Seek(data_in, y_size);
173
params[1].context = context;
174
params[1].quantization_values = cb_quants;
175
params[1].buffer = Stream_Pointer(data_in);
176
params[1].capacity = cb_size;
177
params[1].buffer = pSrcDst[1];
178
Stream_Seek(data_in, cb_size);
180
params[2].context = context;
181
params[2].quantization_values = cr_quants;
182
params[2].buffer = Stream_Pointer(data_in);
183
params[2].capacity = cr_size;
184
params[2].buffer = pSrcDst[2];
185
Stream_Seek(data_in, cr_size);
187
work_objects[0] = CreateThreadpoolWork((PTP_WORK_CALLBACK) rfx_decode_component_work_callback,
188
(void*) ¶ms[0], &context->priv->ThreadPoolEnv);
189
work_objects[1] = CreateThreadpoolWork((PTP_WORK_CALLBACK) rfx_decode_component_work_callback,
190
(void*) ¶ms[1], &context->priv->ThreadPoolEnv);
191
work_objects[2] = CreateThreadpoolWork((PTP_WORK_CALLBACK) rfx_decode_component_work_callback,
192
(void*) ¶ms[2], &context->priv->ThreadPoolEnv);
194
SubmitThreadpoolWork(work_objects[0]);
195
SubmitThreadpoolWork(work_objects[1]);
196
SubmitThreadpoolWork(work_objects[2]);
198
WaitForThreadpoolWorkCallbacks(work_objects[0], FALSE);
199
WaitForThreadpoolWorkCallbacks(work_objects[1], FALSE);
200
WaitForThreadpoolWorkCallbacks(work_objects[2], FALSE);
205
if (Stream_GetRemainingLength(data_in) < y_size + cb_size + cr_size)
207
DEBUG_WARN("rfx_decode_rgb: packet too small for y_size+cb_size+cr_size");
211
rfx_decode_component(context, y_quants, Stream_Pointer(data_in), y_size, pSrcDst[0]); /* YData */
212
Stream_Seek(data_in, y_size);
214
rfx_decode_component(context, cb_quants, Stream_Pointer(data_in), cb_size, pSrcDst[1]); /* CbData */
215
Stream_Seek(data_in, cb_size);
217
rfx_decode_component(context, cr_quants, Stream_Pointer(data_in), cr_size, pSrcDst[2]); /* CrData */
218
Stream_Seek(data_in, cr_size);
221
PROFILER_ENTER(context->priv->prof_rfx_ycbcr_to_rgb);
222
prims->yCbCrToRGB_16s16s_P3P3((const INT16**) pSrcDst, 64 * sizeof(INT16),
223
pSrcDst, 64 * sizeof(INT16), &roi_64x64);
224
PROFILER_EXIT(context->priv->prof_rfx_ycbcr_to_rgb);
226
PROFILER_ENTER(context->priv->prof_rfx_decode_format_rgb);
227
rfx_decode_format_rgb(pSrcDst[0], pSrcDst[1], pSrcDst[2],
228
context->pixel_format, rgb_buffer, stride);
229
PROFILER_EXIT(context->priv->prof_rfx_decode_format_rgb);
231
PROFILER_EXIT(context->priv->prof_rfx_decode_rgb);
233
BufferPool_Return(context->priv->BufferPool, (BYTE*)pSrcDst[0] - 16);
234
BufferPool_Return(context->priv->BufferPool, (BYTE*)pSrcDst[1] - 16);
235
BufferPool_Return(context->priv->BufferPool, (BYTE*)pSrcDst[2] - 16);