1
/* Copyright (C) 2005 Ole Andr� Vadla Ravn�s <oleavr@gmail.com>
3
* This library is free software; you can redistribute it and/or
4
* modify it under the terms of the GNU Lesser General Public
5
* License as published by the Free Software Foundation; either
6
* version 2.1 of the License, or (at your option) any later version.
8
* This library is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public
14
* License along with this library; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#include "mimic-private.h"
21
static gboolean decode(MimCtx *ctx, gboolean is_pframe);
24
* Decode a MIMIC-encoded frame into RGB data.
26
* @param ctx the mimic context
27
* @param input_buffer buffer containing the MIMIC-encoded frame to decode
28
* @param output_buffer buffer that will receive the decoded frame in RGB 24-bpp packed pixel top-down format
29
* (use #mimic_get_property to determine the required buffer size, as well as frame width and height)
30
* @returns #TRUE on success
32
gboolean mimic_decode_frame(MimCtx *ctx,
33
const guchar *input_buffer,
34
guchar *output_buffer)
36
gboolean result, is_pframe;
37
guchar *input_y, *input_cr, *input_cb;
43
if (ctx == NULL || input_buffer == NULL || output_buffer == NULL)
48
if (!ctx->decoder_initialized)
52
* Get frame dimensions.
54
width = GUINT16_FROM_LE(*((guint16 *) (input_buffer + 4)));
55
height = GUINT16_FROM_LE(*((guint16 *) (input_buffer + 6)));
58
* Resolution changing is not supported.
60
if (width != ctx->frame_width ||
61
height != ctx->frame_height)
67
* Increment frame counter.
74
ctx->quality = GUINT16_FROM_LE(*((guint16 *) (input_buffer + 2)));
75
is_pframe = GUINT32_FROM_LE(*((guint32 *) (input_buffer + 12)));
76
ctx->num_coeffs = input_buffer[16];
78
ctx->data_buffer = (gchar *) (input_buffer + 20);
80
ctx->cur_chunk_len = 16;
81
ctx->read_odd = FALSE;
86
if (!(is_pframe && ctx->prev_frame_buf == NULL))
87
result = decode(ctx, is_pframe);
92
* Perform YUV 420 to RGB conversion.
94
input_y = ctx->cur_frame_buf;
95
input_cr = ctx->cur_frame_buf + ctx->y_size;
96
input_cb = ctx->cur_frame_buf + ctx->y_size + ctx->crcb_size;
111
* Main decoding loop.
113
static gboolean decode(MimCtx *ctx, gboolean is_pframe)
115
gint y, x, i, j, chrom_ch, *bptr, base_offset, offset;
117
guchar *src, *dst, *p;
121
* Clear Cr and Cb planes.
123
p = ctx->cur_frame_buf + ctx->y_size;
124
memset(p, 128, 2 * ctx->crcb_size);
129
for (y = 0; y < ctx->num_vblocks_y; y++) {
131
base_offset = ctx->y_stride * 8 * y;
133
src = ctx->prev_frame_buf + base_offset;
134
dst = ctx->cur_frame_buf + base_offset;
136
for (x = 0; x < ctx->num_hblocks_y; x++) {
138
/* Check for a change condition in the current block. */
141
bit = _read_bits(ctx, 1);
147
/* Yes: Is the new content the same as it was in one of
148
* the 15 last frames preceding the previous? */
151
bit = _read_bits(ctx, 1);
157
if (_vlc_decode_block(ctx, dct_block, ctx->num_coeffs) == FALSE) {
159
/* Corruped frame, return. */
163
_idct_dequant_block(ctx, dct_block, 0);
166
for (i = 0; i < 8; i++) {
167
offset = ctx->y_stride * i;
169
for (j = 0; j < 8; j++) {
173
v = (bptr[j] >= 0) ? bptr[j] : 0;
177
*(dst + offset + j) = v;
185
/* Yes: read the backreference (4 bits) and copy. */
187
backref = _read_bits(ctx, 4);
189
p = ctx->buf_ptrs[(ctx->ptr_index + backref) % 16];
190
p += base_offset + (x * 8);
192
for (i = 0; i < 8; i++) {
193
offset = ctx->y_stride * i;
195
memcpy(dst + offset, p + offset, 8);
200
/* No change no worries: just copy from the previous frame. */
202
for (i = 0; i < 8; i++) {
203
offset = ctx->y_stride * i;
205
memcpy(dst + offset, src + offset, 8);
215
* Decode Cr and Cb planes.
217
for (chrom_ch = 0; chrom_ch < 2; chrom_ch++) {
219
base_offset = ctx->y_size + (ctx->crcb_size * chrom_ch);
221
for (y = 0; y < ctx->num_vblocks_cbcr; y++) {
224
/* The last row of blocks in chrominance for 160x120 resolution
225
* is half the normal height and must be accounted for. */
226
if (y + 1 == ctx->num_vblocks_cbcr && ctx->frame_height % 16 != 0)
229
offset = base_offset + (ctx->crcb_stride * 8 * y);
231
src = ctx->prev_frame_buf + offset;
232
dst = ctx->cur_frame_buf + offset;
234
for (x = 0; x < ctx->num_hblocks_cbcr; x++) {
236
/* Check for a change condition in the current block. */
239
bit = _read_bits(ctx, 1);
245
/* Yes: decode it. */
247
if (_vlc_decode_block(ctx, dct_block, ctx->num_coeffs) == FALSE) {
249
/* Corrupted frame: clear Cr and Cb planes and return. */
250
p = ctx->cur_frame_buf + ctx->y_size;
251
memset(p, 128, ctx->crcb_size * 2);
256
_idct_dequant_block(ctx, dct_block, 1);
258
for (i = 0; i < num_rows; i++) {
259
p = dst + (ctx->crcb_stride * i);
261
for (j = 0; j < 8; j++)
262
p[j] = dct_block[(i * 8) + j];
267
/* No change no worries: just copy from the previous frame. */
269
for (i = 0; i < num_rows; i++) {
270
offset = ctx->crcb_stride * i;
272
memcpy(dst + offset, src + offset, 8);
283
* Make a copy of the current frame and store in
284
* the circular pointer list of 16 entries.
286
ctx->prev_frame_buf = ctx->buf_ptrs[ctx->ptr_index];
287
memcpy(ctx->prev_frame_buf, ctx->cur_frame_buf,
288
ctx->y_size + (ctx->crcb_size * 2));
290
if (--ctx->ptr_index < 0)
294
* Perform deblocking on all planes.
296
_deblock(ctx->cur_frame_buf,
297
ctx->y_stride, ctx->y_row_count);
299
_deblock(ctx->cur_frame_buf + ctx->y_size,
300
ctx->crcb_stride, ctx->crcb_row_count);
302
_deblock(ctx->cur_frame_buf + ctx->y_size + ctx->crcb_size,
303
ctx->crcb_stride, ctx->crcb_row_count);