1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/* Copyright (C) 2005 Ole André Vadla Ravnås <oleavr@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "mimic-private.h"
static gboolean decode(MimCtx *ctx, gboolean is_pframe);
/**
* Decode a MIMIC-encoded frame into RGB data.
*
* @param ctx the mimic context
* @param input_buffer buffer containing the MIMIC-encoded frame to decode
* @param output_buffer buffer that will receive the decoded frame in RGB 24-bpp packed pixel top-down format
* (use #mimic_get_property to determine the required buffer size, as well as frame width and height)
* @returns #TRUE on success
*/
gboolean mimic_decode_frame(MimCtx *ctx,
const guchar *input_buffer,
guchar *output_buffer)
{
gboolean result, is_pframe;
guchar *input_y, *input_cr, *input_cb;
gint width, height;
/*
* Some sanity checks.
*/
if (ctx == NULL || input_buffer == NULL || output_buffer == NULL)
{
return FALSE;
}
if (!ctx->decoder_initialized)
{
return FALSE;
}
/*
* Get frame dimensions.
*/
width = GUINT16_FROM_LE(*((guint16 *) (input_buffer + 4)));
height = GUINT16_FROM_LE(*((guint16 *) (input_buffer + 6)));
/*
* Resolution changing is not supported.
*/
if (width != ctx->frame_width ||
height != ctx->frame_height)
{
return FALSE;
}
/*
* Increment frame counter.
*/
ctx->frame_num++;
/*
* Initialize state.
*/
ctx->quality = GUINT16_FROM_LE(*((guint16 *) (input_buffer + 2)));
is_pframe = GUINT32_FROM_LE(*((guint32 *) (input_buffer + 12)));
ctx->num_coeffs = input_buffer[16];
ctx->data_buffer = (gchar *) (input_buffer + 20);
ctx->data_index = 0;
ctx->cur_chunk_len = 16;
ctx->read_odd = FALSE;
/*
* Decode frame.
*/
if (!(is_pframe && ctx->prev_frame_buf == NULL))
result = decode(ctx, is_pframe);
else
{
result = FALSE;
}
/*
* Perform YUV 420 to RGB conversion.
*/
input_y = ctx->cur_frame_buf;
input_cr = ctx->cur_frame_buf + ctx->y_size;
input_cb = ctx->cur_frame_buf + ctx->y_size + ctx->crcb_size;
_yuv_to_rgb(input_y,
input_cb,
input_cr,
output_buffer,
ctx->frame_width,
ctx->frame_height);
return result;
}
/*
* decode_main
*
* Main decoding loop.
*/
static gboolean decode(MimCtx *ctx, gboolean is_pframe)
{
gint y, x, i, j, chrom_ch, *bptr, base_offset, offset;
gint dct_block[64];
guchar *src, *dst, *p;
guint32 bit;
/*
* Clear Cr and Cb planes.
*/
p = ctx->cur_frame_buf + ctx->y_size;
memset(p, 128, 2 * ctx->crcb_size);
/*
* Decode Y plane.
*/
for (y = 0; y < ctx->num_vblocks_y; y++) {
base_offset = ctx->y_stride * 8 * y;
src = ctx->prev_frame_buf + base_offset;
dst = ctx->cur_frame_buf + base_offset;
for (x = 0; x < ctx->num_hblocks_y; x++) {
/* Check for a change condition in the current block. */
if (is_pframe)
bit = _read_bits(ctx, 1);
else
bit = 0;
if (bit == 0) {
/* Yes: Is the new content the same as it was in one of
* the 15 last frames preceding the previous? */
if (is_pframe)
bit = _read_bits(ctx, 1);
if (bit == 0) {
/* No: decode it. */
if (_vlc_decode_block(ctx, dct_block, ctx->num_coeffs) == FALSE) {
return FALSE;
}
_idct_dequant_block(ctx, dct_block, 0);
bptr = dct_block;
for (i = 0; i < 8; i++) {
offset = ctx->y_stride * i;
for (j = 0; j < 8; j++) {
guint v;
if (bptr[j] <= 255)
v = (bptr[j] >= 0) ? bptr[j] : 0;
else
v = 255;
*(dst + offset + j) = v;
}
bptr += 8;
}
} else {
guint32 backref;
/* Yes: read the backreference (4 bits) and copy. */
backref = _read_bits(ctx, 4);
p = ctx->buf_ptrs[(ctx->ptr_index + backref) % 16];
p += base_offset + (x * 8);
for (i = 0; i < 8; i++) {
offset = ctx->y_stride * i;
memcpy(dst + offset, p + offset, 8);
}
}
} else {
/* No change no worries: just copy from the previous frame. */
for (i = 0; i < 8; i++) {
offset = ctx->y_stride * i;
memcpy(dst + offset, src + offset, 8);
}
}
src += 8;
dst += 8;
}
}
/*
* Decode Cr and Cb planes.
*/
for (chrom_ch = 0; chrom_ch < 2; chrom_ch++) {
base_offset = ctx->y_size + (ctx->crcb_size * chrom_ch);
for (y = 0; y < ctx->num_vblocks_cbcr; y++) {
guint num_rows = 8;
/* The last row of blocks in chrominance for 160x120 resolution
* is half the normal height and must be accounted for. */
if (y + 1 == ctx->num_vblocks_cbcr && ctx->frame_height % 16 != 0)
num_rows = 4;
offset = base_offset + (ctx->crcb_stride * 8 * y);
src = ctx->prev_frame_buf + offset;
dst = ctx->cur_frame_buf + offset;
for (x = 0; x < ctx->num_hblocks_cbcr; x++) {
/* Check for a change condition in the current block. */
if (is_pframe)
bit = _read_bits(ctx, 1);
else
bit = 1;
if (bit == 1) {
/* Yes: decode it. */
if (_vlc_decode_block(ctx, dct_block, ctx->num_coeffs) == FALSE) {
/* Corrupted frame: clear Cr and Cb planes and return. */
p = ctx->cur_frame_buf + ctx->y_size;
memset(p, 128, ctx->crcb_size * 2);
return FALSE;
}
_idct_dequant_block(ctx, dct_block, 1);
for (i = 0; i < num_rows; i++) {
p = dst + (ctx->crcb_stride * i);
for (j = 0; j < 8; j++)
p[j] = dct_block[(i * 8) + j];
}
} else {
/* No change no worries: just copy from the previous frame. */
for (i = 0; i < num_rows; i++) {
offset = ctx->crcb_stride * i;
memcpy(dst + offset, src + offset, 8);
}
}
src += 8;
dst += 8;
}
}
}
/*
* Make a copy of the current frame and store in
* the circular pointer list of 16 entries.
*/
ctx->prev_frame_buf = ctx->buf_ptrs[ctx->ptr_index];
memcpy(ctx->prev_frame_buf, ctx->cur_frame_buf,
ctx->y_size + (ctx->crcb_size * 2));
if (--ctx->ptr_index < 0)
ctx->ptr_index = 15;
/*
* Perform deblocking on all planes.
*/
_deblock(ctx->cur_frame_buf,
ctx->y_stride, ctx->y_row_count);
_deblock(ctx->cur_frame_buf + ctx->y_size,
ctx->crcb_stride, ctx->crcb_row_count);
_deblock(ctx->cur_frame_buf + ctx->y_size + ctx->crcb_size,
ctx->crcb_stride, ctx->crcb_row_count);
return TRUE;
}
|