2 * FreeRDP: A Remote Desktop Protocol client.
3 * RemoteFX Codec Library - Encode
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.
24 #include "rfx_types.h"
26 #include "rfx_differential.h"
27 #include "rfx_quantization.h"
30 #include "rfx_encode.h"
32 #define MINMAX(_v,_l,_h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))
34 static void rfx_encode_format_rgb(const uint8* rgb_data, int width, int height, int rowstride,
35 RFX_PIXEL_FORMAT pixel_format, const uint8* palette, sint16* r_buf, sint16* g_buf, sint16* b_buf)
42 sint16 *r_last, *g_last, *b_last;
44 x_exceed = 64 - width;
45 y_exceed = 64 - height;
47 for (y = 0; y < height; y++)
49 src = rgb_data + y * rowstride;
53 case RFX_PIXEL_FORMAT_BGRA:
54 for (x = 0; x < width; x++)
56 *b_buf++ = (sint16) (*src++);
57 *g_buf++ = (sint16) (*src++);
58 *r_buf++ = (sint16) (*src++);
62 case RFX_PIXEL_FORMAT_RGBA:
63 for (x = 0; x < width; x++)
65 *r_buf++ = (sint16) (*src++);
66 *g_buf++ = (sint16) (*src++);
67 *b_buf++ = (sint16) (*src++);
71 case RFX_PIXEL_FORMAT_BGR:
72 for (x = 0; x < width; x++)
74 *b_buf++ = (sint16) (*src++);
75 *g_buf++ = (sint16) (*src++);
76 *r_buf++ = (sint16) (*src++);
79 case RFX_PIXEL_FORMAT_RGB:
80 for (x = 0; x < width; x++)
82 *r_buf++ = (sint16) (*src++);
83 *g_buf++ = (sint16) (*src++);
84 *b_buf++ = (sint16) (*src++);
87 case RFX_PIXEL_FORMAT_BGR565_LE:
88 for (x = 0; x < width; x++)
90 *b_buf++ = (sint16) (((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
91 *g_buf++ = (sint16) ((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
92 *r_buf++ = (sint16) ((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
96 case RFX_PIXEL_FORMAT_RGB565_LE:
97 for (x = 0; x < width; x++)
99 *r_buf++ = (sint16) (((*(src + 1)) & 0xF8) | ((*(src + 1)) >> 5));
100 *g_buf++ = (sint16) ((((*(src + 1)) & 0x07) << 5) | (((*src) & 0xE0) >> 3));
101 *b_buf++ = (sint16) ((((*src) & 0x1F) << 3) | (((*src) >> 2) & 0x07));
105 case RFX_PIXEL_FORMAT_PALETTE4_PLANER:
108 for (x = 0; x < width; x++)
113 shift = (7 - (x % 8));
114 idx = ((*src) >> shift) & 1;
115 idx |= (((*(src + 1)) >> shift) & 1) << 1;
116 idx |= (((*(src + 2)) >> shift) & 1) << 2;
117 idx |= (((*(src + 3)) >> shift) & 1) << 3;
119 *r_buf++ = (sint16) palette[idx];
120 *g_buf++ = (sint16) palette[idx + 1];
121 *b_buf++ = (sint16) palette[idx + 2];
126 case RFX_PIXEL_FORMAT_PALETTE8:
129 for (x = 0; x < width; x++)
131 int idx = (*src) * 3;
133 *r_buf++ = (sint16) palette[idx];
134 *g_buf++ = (sint16) palette[idx + 1];
135 *b_buf++ = (sint16) palette[idx + 2];
142 /* Fill the horizontal region outside of 64x64 tile size with the right-most pixel for best quality */
149 for (x = 0; x < x_exceed; x++)
158 /* Fill the vertical region outside of 64x64 tile size with the last line. */
167 memcpy(r_buf, r_last, 64 * sizeof(sint16));
168 memcpy(g_buf, g_last, 64 * sizeof(sint16));
169 memcpy(b_buf, b_last, 64 * sizeof(sint16));
178 void rfx_encode_rgb_to_ycbcr(sint16* y_r_buf, sint16* cb_g_buf, sint16* cr_b_buf)
180 /* sint32 is used intentionally because we calculate with shifted factors! */
186 * The encoded YCbCr coefficients are represented as 11.5 fixed-point numbers:
188 * 1 sign bit + 10 integer bits + 5 fractional bits
190 * However only 7 integer bits will be actually used since the value range is [-128.0, 127.0].
191 * In other words, the encoded coefficients is scaled by << 5 when interpreted as sint16.
192 * It will be scaled down to original during the quantization phase.
194 for (i = 0; i < 4096; i++)
201 * We scale the factors by << 15 into 32-bit integers in order to avoid slower
202 * floating point multiplications. Since the terms need to be scaled by << 5 we
203 * simply scale the final sum by >> 10
205 * Y: 0.299000 << 15 = 9798, 0.587000 << 15 = 19235, 0.114000 << 15 = 3735
206 * Cb: 0.168935 << 15 = 5535, 0.331665 << 15 = 10868, 0.500590 << 15 = 16403
207 * Cr: 0.499813 << 15 = 16377, 0.418531 << 15 = 13714, 0.081282 << 15 = 2663
210 y = (r * 9798 + g * 19235 + b * 3735) >> 10;
211 cb = (r * -5535 + g * -10868 + b * 16403) >> 10;
212 cr = (r * 16377 + g * -13714 + b * -2663) >> 10;
214 y_r_buf[i] = MINMAX(y - 4096, -4096, 4095);
215 cb_g_buf[i] = MINMAX(cb, -4096, 4095);
216 cr_b_buf[i] = MINMAX(cr, -4096, 4095);
220 static void rfx_encode_component(RFX_CONTEXT* context, const uint32* quantization_values,
221 sint16* data, uint8* buffer, int buffer_size, int* size)
223 PROFILER_ENTER(context->priv->prof_rfx_encode_component);
225 PROFILER_ENTER(context->priv->prof_rfx_dwt_2d_encode);
226 context->dwt_2d_encode(data, context->priv->dwt_buffer);
227 PROFILER_EXIT(context->priv->prof_rfx_dwt_2d_encode);
229 PROFILER_ENTER(context->priv->prof_rfx_quantization_encode);
230 context->quantization_encode(data, quantization_values);
231 PROFILER_EXIT(context->priv->prof_rfx_quantization_encode);
233 PROFILER_ENTER(context->priv->prof_rfx_differential_encode);
234 rfx_differential_encode(data + 4032, 64);
235 PROFILER_EXIT(context->priv->prof_rfx_differential_encode);
237 PROFILER_ENTER(context->priv->prof_rfx_rlgr_encode);
238 *size = rfx_rlgr_encode(context->mode, data, 4096, buffer, buffer_size);
239 PROFILER_EXIT(context->priv->prof_rfx_rlgr_encode);
241 PROFILER_EXIT(context->priv->prof_rfx_encode_component);
244 void rfx_encode_rgb(RFX_CONTEXT* context, const uint8* rgb_data, int width, int height, int rowstride,
245 const uint32* y_quants, const uint32* cb_quants, const uint32* cr_quants,
246 STREAM* data_out, int* y_size, int* cb_size, int* cr_size)
248 sint16* y_r_buffer = context->priv->y_r_buffer;
249 sint16* cb_g_buffer = context->priv->cb_g_buffer;
250 sint16* cr_b_buffer = context->priv->cr_b_buffer;
252 PROFILER_ENTER(context->priv->prof_rfx_encode_rgb);
254 PROFILER_ENTER(context->priv->prof_rfx_encode_format_rgb);
255 rfx_encode_format_rgb(rgb_data, width, height, rowstride,
256 context->pixel_format, context->palette, y_r_buffer, cb_g_buffer, cr_b_buffer);
257 PROFILER_EXIT(context->priv->prof_rfx_encode_format_rgb);
259 PROFILER_ENTER(context->priv->prof_rfx_encode_rgb_to_ycbcr);
260 context->encode_rgb_to_ycbcr(context->priv->y_r_buffer, context->priv->cb_g_buffer, context->priv->cr_b_buffer);
261 PROFILER_EXIT(context->priv->prof_rfx_encode_rgb_to_ycbcr);
263 /* Ensure the buffer is reasonably large enough */
264 stream_check_size(data_out, 4096);
265 rfx_encode_component(context, y_quants, context->priv->y_r_buffer,
266 stream_get_tail(data_out), stream_get_left(data_out), y_size);
267 stream_seek(data_out, *y_size);
269 stream_check_size(data_out, 4096);
270 rfx_encode_component(context, cb_quants, context->priv->cb_g_buffer,
271 stream_get_tail(data_out), stream_get_left(data_out), cb_size);
272 stream_seek(data_out, *cb_size);
274 stream_check_size(data_out, 4096);
275 rfx_encode_component(context, cr_quants, context->priv->cr_b_buffer,
276 stream_get_tail(data_out), stream_get_left(data_out), cr_size);
277 stream_seek(data_out, *cr_size);
279 PROFILER_EXIT(context->priv->prof_rfx_encode_rgb);