Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-codec / rfx_encode.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol client.
3  * RemoteFX Codec Library - Encode
4  *
5  * Copyright 2011 Vic Lee
6  * Copyright 2011 Norbert Federa <nfedera@thinstuff.com>
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "rfx_types.h"
25 #include "rfx_rlgr.h"
26 #include "rfx_differential.h"
27 #include "rfx_quantization.h"
28 #include "rfx_dwt.h"
29
30 #include "rfx_encode.h"
31
32 #define MINMAX(_v,_l,_h) ((_v) < (_l) ? (_l) : ((_v) > (_h) ? (_h) : (_v)))
33
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)
36 {
37         int x, y;
38         int x_exceed;
39         int y_exceed;
40         const uint8* src;
41         sint16 r, g, b;
42         sint16 *r_last, *g_last, *b_last;
43
44         x_exceed = 64 - width;
45         y_exceed = 64 - height;
46
47         for (y = 0; y < height; y++)
48         {
49                 src = rgb_data + y * rowstride;
50
51                 switch (pixel_format)
52                 {
53                         case RFX_PIXEL_FORMAT_BGRA:
54                                 for (x = 0; x < width; x++)
55                                 {
56                                         *b_buf++ = (sint16) (*src++);
57                                         *g_buf++ = (sint16) (*src++);
58                                         *r_buf++ = (sint16) (*src++);
59                                         src++;
60                                 }
61                                 break;
62                         case RFX_PIXEL_FORMAT_RGBA:
63                                 for (x = 0; x < width; x++)
64                                 {
65                                         *r_buf++ = (sint16) (*src++);
66                                         *g_buf++ = (sint16) (*src++);
67                                         *b_buf++ = (sint16) (*src++);
68                                         src++;
69                                 }
70                                 break;
71                         case RFX_PIXEL_FORMAT_BGR:
72                                 for (x = 0; x < width; x++)
73                                 {
74                                         *b_buf++ = (sint16) (*src++);
75                                         *g_buf++ = (sint16) (*src++);
76                                         *r_buf++ = (sint16) (*src++);
77                                 }
78                                 break;
79                         case RFX_PIXEL_FORMAT_RGB:
80                                 for (x = 0; x < width; x++)
81                                 {
82                                         *r_buf++ = (sint16) (*src++);
83                                         *g_buf++ = (sint16) (*src++);
84                                         *b_buf++ = (sint16) (*src++);
85                                 }
86                                 break;
87                         case RFX_PIXEL_FORMAT_BGR565_LE:
88                                 for (x = 0; x < width; x++)
89                                 {
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));
93                                         src += 2;
94                                 }
95                                 break;
96                         case RFX_PIXEL_FORMAT_RGB565_LE:
97                                 for (x = 0; x < width; x++)
98                                 {
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));
102                                         src += 2;
103                                 }
104                                 break;
105                         case RFX_PIXEL_FORMAT_PALETTE4_PLANER:
106                                 if (!palette)
107                                         break;
108                                 for (x = 0; x < width; x++)
109                                 {
110                                         int shift;
111                                         uint8 idx;
112
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;
118                                         idx *= 3;
119                                         *r_buf++ = (sint16) palette[idx];
120                                         *g_buf++ = (sint16) palette[idx + 1];
121                                         *b_buf++ = (sint16) palette[idx + 2];
122                                         if (shift == 0)
123                                                 src += 4;
124                                 }
125                                 break;
126                         case RFX_PIXEL_FORMAT_PALETTE8:
127                                 if (!palette)
128                                         break;
129                                 for (x = 0; x < width; x++)
130                                 {
131                                         int idx = (*src) * 3;
132
133                                         *r_buf++ = (sint16) palette[idx];
134                                         *g_buf++ = (sint16) palette[idx + 1];
135                                         *b_buf++ = (sint16) palette[idx + 2];
136                                         src++;
137                                 }
138                                 break;
139                         default:
140                                 break;
141                 }
142                 /* Fill the horizontal region outside of 64x64 tile size with the right-most pixel for best quality */
143                 if (x_exceed > 0)
144                 {
145                         r = *(r_buf - 1);
146                         g = *(g_buf - 1);
147                         b = *(b_buf - 1);
148
149                         for (x = 0; x < x_exceed; x++)
150                         {
151                                 *r_buf++ = r;
152                                 *g_buf++ = g;
153                                 *b_buf++ = b;
154                         }
155                 }
156         }
157
158         /* Fill the vertical region outside of 64x64 tile size with the last line. */
159         if (y_exceed > 0)
160         {
161                 r_last = r_buf - 64;
162                 g_last = g_buf - 64;
163                 b_last = b_buf - 64;
164
165                 while (y_exceed > 0)
166                 {
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));
170                         r_buf += 64;
171                         g_buf += 64;
172                         b_buf += 64;
173                         y_exceed--;
174                 }
175         }
176 }
177
178 void rfx_encode_rgb_to_ycbcr(sint16* y_r_buf, sint16* cb_g_buf, sint16* cr_b_buf)
179 {
180         /* sint32 is used intentionally because we calculate with shifted factors! */
181         int i;
182         sint32 r, g, b;
183         sint32 y, cb, cr;
184
185         /**
186          * The encoded YCbCr coefficients are represented as 11.5 fixed-point numbers:
187          *
188          * 1 sign bit + 10 integer bits + 5 fractional bits
189          *
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.
193          */
194         for (i = 0; i < 4096; i++)
195         {
196                 r = y_r_buf[i];
197                 g = cb_g_buf[i];
198                 b = cr_b_buf[i];
199
200                 /*
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
204                  *
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
208                  */
209
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;
213
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);
217         }
218 }
219
220 static void rfx_encode_component(RFX_CONTEXT* context, const uint32* quantization_values,
221         sint16* data, uint8* buffer, int buffer_size, int* size)
222 {
223         PROFILER_ENTER(context->priv->prof_rfx_encode_component);
224
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);
228
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);
232
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);
236
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);
240
241         PROFILER_EXIT(context->priv->prof_rfx_encode_component);
242 }
243
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)
247 {
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;
251
252         PROFILER_ENTER(context->priv->prof_rfx_encode_rgb);
253
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);
258
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);
262
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);
268
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);
273
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);
278
279         PROFILER_EXIT(context->priv->prof_rfx_encode_rgb);
280 }