summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/msn/webcam/libmimic/mimic.c
blob: 955647555d0e0eaa30c5d39217e95fc1dd36c38e (plain)
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <string.h>
#include "mimic-private.h"

/**
 * Creates a new instance and returns a pointer to the new context
 * that can be used for either encoding or decoding by calling
 * #mimic_encoder_init or #mimic_decoder_init.
 *
 * #mimic_close is called to free any resources associated with
 * the context once done.
 *
 * @returns a new mimic context
 */
MimCtx *mimic_open()
{
    MimCtx *ctx;
    
    ctx = g_new0(MimCtx, 1);
    
    ctx->encoder_initialized = FALSE;
    ctx->decoder_initialized = FALSE;
    
    return ctx;
}

/**
 * Frees any resources associated with the given context.
 *
 * @param ctx the mimic context to free
 */
void mimic_close(MimCtx *ctx)
{
    if (ctx->encoder_initialized || ctx->decoder_initialized) {
        gint i;
        
        g_free(ctx->cur_frame_buf);
        
        for (i = 0; i < 16; i++)
            g_free(ctx->buf_ptrs[i]);
    }
    
    g_free(ctx);
}

/*
 * mimic_init
 *
 * Internal helper-function used to initialize
 * a given context.
 */
static void mimic_init(MimCtx *ctx, gint width, gint height)
{
    gint bufsize, i;
    
    /*
     * Dimensions-related.
     */
    ctx->frame_width = width;
    ctx->frame_height = height;

    ctx->y_stride    = ctx->frame_width;
    ctx->y_row_count = ctx->frame_height;
    ctx->y_size      = ctx->y_stride * ctx->y_row_count;
    
    ctx->crcb_stride    = ctx->y_stride    / 2;
    ctx->crcb_row_count = ctx->y_row_count / 2;
    ctx->crcb_size      = ctx->crcb_stride * ctx->crcb_row_count;
    
    ctx->num_vblocks_y = ctx->frame_height / 8;
    ctx->num_hblocks_y = ctx->frame_width  / 8;

    ctx->num_vblocks_cbcr = ctx->frame_height / 16;
    ctx->num_hblocks_cbcr = ctx->frame_width  / 16;

    if (ctx->frame_height % 16 != 0)
        ctx->num_vblocks_cbcr++;

    /*
     * Initialize state.
     */
    ctx->frame_num = 0;
    ctx->ptr_index = 15;
    ctx->num_coeffs = 28;

    /*
     * Allocate memory for buffers.
     */
    ctx->cur_frame_buf = g_new(guchar, (320 * 240 * 3) / 2);

    bufsize = ctx->y_size + (ctx->crcb_size * 2);
    for (i = 0; i < 16; i++)
        ctx->buf_ptrs[i] = g_new(guchar, bufsize);

    /*
     * Initialize vlc lookup used by decoder.
     */
    _initialize_vlcdec_lookup(ctx->vlcdec_lookup);
}

/**
 * Initialize the mimic encoder and prepare for encoding by
 * initializing internal state and allocating resources as
 * needed.
 * 
 * After initializing use #mimic_get_property to determine
 * the size of the output buffer needed for calls to
 * #mimic_encode_frame. Use #mimic_set_property to set
 * encoding quality.
 *
 * Note that once a given context has been initialized
 * for either encoding or decoding it is not possible
 * to initialize it again.
 *
 * @param ctx the mimic context to initialize
 * @param resolution a #MimicResEnum used to specify the resolution
 * @returns #TRUE on success
 */
gboolean mimic_encoder_init(MimCtx *ctx, const MimicResEnum resolution)
{
    gint width, height;
    
    /* Check if we've been initialized before. */
    if (ctx->encoder_initialized || ctx->decoder_initialized)
        return FALSE;

    /* Check resolution. */
    if (resolution == MIMIC_RES_LOW) {
        width = 160;
        height = 120;
    } else if (resolution == MIMIC_RES_HIGH) {
        width = 320;
        height = 240;
    } else {
        return FALSE;
    }

    /* Initialize! */
    mimic_init(ctx, width, height);

    /* Set a default quality setting. */
    ctx->quality = ENCODER_QUALITY_DEFAULT;

    ctx->encoder_initialized = TRUE;
    
    return TRUE;
}

/**
 * Initialize the mimic decoder. The frame passed in frame_buffer
 * is used to determine the resolution so that the internal state
 * can be prepared and resources allocated accordingly. Note that
 * the frame passed has to be a keyframe.
 *
 * After initializing use #mimic_get_property to determine required
 * buffer-size, resolution, quality, etc.
 *
 * Note that once a given context has been initialized
 * for either encoding or decoding it is not possible
 * to initialize it again.
 *
 * @param ctx the mimic context to initialize
 * @param frame_buffer buffer containing the first frame to decode
 * @returns #TRUE on success
 */
gboolean mimic_decoder_init(MimCtx *ctx, const guchar *frame_buffer)
{
    gint width, height;
    gboolean is_keyframe;
    
    /* Check if we've been initialized before and that
     * frame_buffer is not NULL. */
    if (ctx->encoder_initialized || ctx->decoder_initialized ||
        frame_buffer == NULL)
    {
        return FALSE;
    }
    
    /* Check resolution. */
    width  = GUINT16_FROM_LE(*((guint16 *) (frame_buffer + 4)));
    height = GUINT16_FROM_LE(*((guint16 *) (frame_buffer + 6)));
    
    if (!(width == 160 && height == 120) && !(width == 320 && height == 240))
        return FALSE;

    /* Check that we're initialized with a keyframe. */
    is_keyframe = (GUINT32_FROM_LE(*((guint32 *) (frame_buffer + 12))) == 0);
    
    if (!is_keyframe)
        return FALSE;

    /* Get quality setting (in case we get queried for it before decoding). */
    ctx->quality = GUINT16_FROM_LE(*((guint16 *) (frame_buffer + 2)));
 
    /* Initialize! */
    mimic_init(ctx, width, height);

    ctx->decoder_initialized = TRUE;

    return TRUE;
}

/**
 * Get a property from a given mimic context. The context
 * has to be initialized.
 *
 * Currently the following properties are defined:
 *   - "buffer_size"
 *       - Required output buffer size
 *   - "width"
 *       - Frame width
 *   - "height"
 *       - Frame height
 *   - "quality"
 *       - Encoder: Encoding quality used
 *       - Decoder: Decoding quality of the last known frame
 *
 * @param ctx the mimic context to retrieve the property from
 * @param name of the property to retrieve the current value of
 * @param data pointer to the data that will receive the retrieved value
 * @returns #TRUE on success
 */
gboolean mimic_get_property(MimCtx *ctx, const gchar *name, gpointer data)
{
    /* Either the encoder or the decoder has to be initialized. */
    if (!ctx->encoder_initialized && !ctx->decoder_initialized)
        return FALSE;

    if (ctx->encoder_initialized) {

        if (strcmp(name, "buffer_size") == 0) {
            *((gint *) data) = ENCODER_BUFFER_SIZE;

            return TRUE;
        }
        
    } else { /* decoder_initialized */

        if (strcmp(name, "buffer_size") == 0) {
            *((gint *) data) = ctx->frame_width * ctx->frame_height * 3;

            return TRUE;
        }
    }

    if (strcmp(name, "width") == 0) {
        *((gint *) data) = ctx->frame_width;

        return TRUE;
    } else if (strcmp(name, "height") == 0) {
        *((gint *) data) = ctx->frame_height;
        
        return TRUE;
    } else if (strcmp(name, "quality") == 0) {
        *((gint *) data) = ctx->quality;
        
        return TRUE;
    }

    return FALSE;
}

/**
 * Set a property in a given mimic context. The context
 * has to be initialized.
 *
 * Currently the following properties are defined:
 *   - "quality"
 *       - Encoding quality used by encoder.
 *
 * @param ctx the mimic context to set a property in
 * @param name of the property to set to a new value
 * @param data pointer to the data that contains the new value
 * @returns #TRUE on success
 */
gboolean mimic_set_property(MimCtx *ctx, const gchar *name, gpointer data)
{
    /* Either the encoder or the decoder has to be initialized. */
    if (!ctx->encoder_initialized && !ctx->decoder_initialized)
        return FALSE;

    if (ctx->encoder_initialized) {

        if (strcmp(name, "quality") == 0) {
            gint new_quality = *((gint *) data);

            if (new_quality < ENCODER_QUALITY_MIN ||
                new_quality > ENCODER_QUALITY_MAX)
            {
                return FALSE;
            }

            ctx->quality = new_quality;

            return TRUE;
        }
        
    } else { /* decoder_initialized */ }

    return FALSE;
}

/*
 * _clamp_value
 *
 * Internal helper-function used to clamp a given
 * value to the range [ 0, 255 ].
 */
guchar _clamp_value(gint value)
{
    if (value < 0)
        return 0;
    else if (value > 255)
        return 255;
    else
        return value;
}