~ubuntu-branches/ubuntu/gutsy/amsn/gutsy

« back to all changes in this revision

Viewing changes to utils/webcamsn/src/vlc_decode.c

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Karkoulis
  • Date: 2006-01-04 15:26:02 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060104152602-ipe1yg00rl3nlklv
Tags: 0.95-1
New Upstream Release (closes: #345052, #278575).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2005  Ole Andr� Vadla Ravn�s <oleavr@gmail.com>
 
2
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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
 
16
 */
 
17
 
 
18
#include <string.h>
 
19
#include "mimic-private.h"
 
20
 
 
21
extern guchar _col_zag[64];
 
22
 
 
23
/*
 
24
 * _vlc_decode_block
 
25
 *
 
26
 * De-serialize (reconstruct) a variable length coded 8x8 block.
 
27
 */
 
28
gboolean _vlc_decode_block(MimCtx *ctx, gint *block, gint num_coeffs)
 
29
{
 
30
    guint pos;
 
31
 
 
32
    memset(block, 0, 64 * sizeof(gint));
 
33
 
 
34
    /* The DC-value is read in as is. */
 
35
    block[0] = _read_bits(ctx, 8);
 
36
 
 
37
    for (pos = 1; pos < num_coeffs; pos++) {
 
38
        
 
39
        guint prev_data_index, prev_cur_chunk_len, prev_chunk;
 
40
        guint value, num_bits;
 
41
        gboolean prev_read_odd, found_magic;
 
42
        
 
43
        /* Save context. */
 
44
        prev_data_index = ctx->data_index;
 
45
        prev_cur_chunk_len = ctx->cur_chunk_len;
 
46
        prev_chunk = ctx->cur_chunk;
 
47
        prev_read_odd = ctx->read_odd;
 
48
 
 
49
        /* Grab 16 bits. */
 
50
        value = _read_bits(ctx, 16) << 16;
 
51
 
 
52
        /* Restore context. */
 
53
        ctx->data_index = prev_data_index;
 
54
        ctx->cur_chunk_len = prev_cur_chunk_len;
 
55
        ctx->cur_chunk = prev_chunk;
 
56
        ctx->read_odd = prev_read_odd;
 
57
 
 
58
        /* Analyze and determine number of bits to read initially. */
 
59
        num_bits = 3;
 
60
        if ((value >> 30) == 0 || (value >> 30) == 1) {
 
61
            num_bits = 2;
 
62
        } else if ((value & 0xE0000000) != 0x80000000) {
 
63
            guint nibble = value >> 28;
 
64
 
 
65
            if (nibble == 11 || nibble == 12) {
 
66
                num_bits = 4;
 
67
            } else if (nibble == 10) {
 
68
                _read_bits(ctx, 4);
 
69
 
 
70
                return TRUE;
 
71
            } else {
 
72
                if (((value << 2) & 0x8000000) == 0)
 
73
                    num_bits = 2;
 
74
 
 
75
                num_bits += 2;
 
76
            }
 
77
        }
 
78
 
 
79
        /* Read that number of bits. */
 
80
        value = _read_bits(ctx, num_bits);
 
81
 
 
82
        /*
 
83
         * Look up the current value against the magic ones,
 
84
         * and continue extending it bit by bit from the input
 
85
         * stream until the magic value is found or we have
 
86
         * read 32 bits (in which case we give up).
 
87
         */
 
88
        found_magic = FALSE;
 
89
        while (!found_magic) {
 
90
            VlcMagic *magic;
 
91
 
 
92
            if (num_bits > 32)
 
93
                return FALSE;
 
94
 
 
95
            magic = _find_magic(value);
 
96
 
 
97
            if (magic != NULL) {
 
98
                pos += magic->pos_add;
 
99
                num_bits = magic->num_bits;
 
100
 
 
101
                found_magic = TRUE;
 
102
            } else {
 
103
                value <<= 1;
 
104
                value |= _read_bits(ctx, 1);
 
105
 
 
106
                num_bits++;
 
107
            }
 
108
        }
 
109
 
 
110
        /* Read the number of bits given by magic value entry. */
 
111
        value = _read_bits(ctx, num_bits);
 
112
        
 
113
        /* Gotcha! :-) */
 
114
        block[_col_zag[pos]] = ctx->vlcdec_lookup[(num_bits * 255) + value];
 
115
    }
 
116
 
 
117
    return TRUE;
 
118
}
 
119