~ubuntu-branches/ubuntu/precise/v4l-utils/precise

« back to all changes in this revision

Viewing changes to lib/libv4lconvert/sn9c10x.c

  • Committer: Bazaar Package Importer
  • Author(s): Gregor Jasny
  • Date: 2010-02-28 19:44:15 UTC
  • Revision ID: james.westby@ubuntu.com-20100228194415-067hdj8rvawj91zw
Tags: upstream-0.7.90
ImportĀ upstreamĀ versionĀ 0.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
# sonix decoder
 
3
#               Bertrik.Sikken. (C) 2005
 
4
 
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU Lesser General Public License as published by
 
7
# the Free Software Foundation; either version 2.1 of the License, or
 
8
# (at your option) any later version.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU Lesser General Public License for more details.
 
14
#
 
15
# You should have received a copy of the GNU Lesser General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 
 
19
# Note this code was originally licensed under the GNU GPL instead of the
 
20
# GNU LGPL, its license has been changed with permission, see the permission
 
21
# mail at the end of this file.
 
22
*/
 
23
 
 
24
#include "libv4lconvert-priv.h"
 
25
 
 
26
#define CLAMP(x)        ((x)<0?0:((x)>255)?255:(x))
 
27
 
 
28
typedef struct {
 
29
        int is_abs;
 
30
        int len;
 
31
        int val;
 
32
        int unk;
 
33
} code_table_t;
 
34
 
 
35
 
 
36
/* local storage */
 
37
/* FIXME not thread safe !! */
 
38
static code_table_t table[256];
 
39
static int init_done = 0;
 
40
 
 
41
/*
 
42
        sonix_decompress_init
 
43
        =====================
 
44
                pre-calculates a locally stored table for efficient huffman-decoding.
 
45
 
 
46
        Each entry at index x in the table represents the codeword
 
47
        present at the MSB of byte x.
 
48
 
 
49
*/
 
50
static void sonix_decompress_init(void)
 
51
{
 
52
        int i;
 
53
        int is_abs, val, len, unk;
 
54
 
 
55
        for (i = 0; i < 256; i++) {
 
56
                is_abs = 0;
 
57
                val = 0;
 
58
                len = 0;
 
59
                unk = 0;
 
60
                if ((i & 0x80) == 0) {
 
61
                        /* code 0 */
 
62
                        val = 0;
 
63
                        len = 1;
 
64
                }
 
65
                else if ((i & 0xE0) == 0x80) {
 
66
                        /* code 100 */
 
67
                        val = +4;
 
68
                        len = 3;
 
69
                }
 
70
                else if ((i & 0xE0) == 0xA0) {
 
71
                        /* code 101 */
 
72
                        val = -4;
 
73
                        len = 3;
 
74
                }
 
75
                else if ((i & 0xF0) == 0xD0) {
 
76
                        /* code 1101 */
 
77
                        val = +11;
 
78
                        len = 4;
 
79
                }
 
80
                else if ((i & 0xF0) == 0xF0) {
 
81
                        /* code 1111 */
 
82
                        val = -11;
 
83
                        len = 4;
 
84
                }
 
85
                else if ((i & 0xF8) == 0xC8) {
 
86
                        /* code 11001 */
 
87
                        val = +20;
 
88
                        len = 5;
 
89
                }
 
90
                else if ((i & 0xFC) == 0xC0) {
 
91
                        /* code 110000 */
 
92
                        val = -20;
 
93
                        len = 6;
 
94
                }
 
95
                else if ((i & 0xFC) == 0xC4) {
 
96
                        /* code 110001xx: unknown */
 
97
                        val = 0;
 
98
                        len = 8;
 
99
                        unk = 1;
 
100
                }
 
101
                else if ((i & 0xF0) == 0xE0) {
 
102
                        /* code 1110xxxx */
 
103
                        is_abs = 1;
 
104
                        val = (i & 0x0F) << 4;
 
105
                        len = 8;
 
106
                }
 
107
                table[i].is_abs = is_abs;
 
108
                table[i].val = val;
 
109
                table[i].len = len;
 
110
                table[i].unk = unk;
 
111
        }
 
112
 
 
113
        init_done = 1;
 
114
}
 
115
 
 
116
 
 
117
/*
 
118
        sonix_decompress
 
119
        ================
 
120
                decompresses an image encoded by a SN9C101 camera controller chip.
 
121
 
 
122
        IN      width
 
123
                height
 
124
                inp             pointer to compressed frame (with header already stripped)
 
125
        OUT     outp    pointer to decompressed frame
 
126
 
 
127
        Returns 0 if the operation was successful.
 
128
        Returns <0 if operation failed.
 
129
 
 
130
*/
 
131
void v4lconvert_decode_sn9c10x(const unsigned char *inp, unsigned char *outp,
 
132
        int width, int height)
 
133
{
 
134
        int row, col;
 
135
        int val;
 
136
        int bitpos;
 
137
        unsigned char code;
 
138
        const unsigned char *addr;
 
139
 
 
140
        if (!init_done)
 
141
                sonix_decompress_init();
 
142
 
 
143
        bitpos = 0;
 
144
        for (row = 0; row < height; row++) {
 
145
 
 
146
                col = 0;
 
147
 
 
148
                /* first two pixels in first two rows are stored as raw 8-bit */
 
149
                if (row < 2) {
 
150
                        addr = inp + (bitpos >> 3);
 
151
                        code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
 
152
                        bitpos += 8;
 
153
                        *outp++ = code;
 
154
 
 
155
                        addr = inp + (bitpos >> 3);
 
156
                        code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
 
157
                        bitpos += 8;
 
158
                        *outp++ = code;
 
159
 
 
160
                        col += 2;
 
161
                }
 
162
 
 
163
                while (col < width) {
 
164
                        /* get bitcode from bitstream */
 
165
                        addr = inp + (bitpos >> 3);
 
166
                        code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
 
167
 
 
168
                        /* update bit position */
 
169
                        bitpos += table[code].len;
 
170
 
 
171
                        /* Skip unknown codes (most likely they indicate
 
172
                           a change of the delta's the various codes encode) */
 
173
                        if (table[code].unk)
 
174
                                continue;
 
175
 
 
176
                        /* calculate pixel value */
 
177
                        val = table[code].val;
 
178
                        if (!table[code].is_abs) {
 
179
                                /* value is relative to top and left pixel */
 
180
                                if (col < 2) {
 
181
                                        /* left column: relative to top pixel */
 
182
                                        val += outp[-2*width];
 
183
                                }
 
184
                                else if (row < 2) {
 
185
                                        /* top row: relative to left pixel */
 
186
                                        val += outp[-2];
 
187
                                }
 
188
                                else {
 
189
                                        /* main area: average of left pixel and top pixel */
 
190
                                        val += (outp[-2] + outp[-2*width]) / 2;
 
191
                                }
 
192
                        }
 
193
 
 
194
                        /* store pixel */
 
195
                        *outp++ = CLAMP(val);
 
196
                        col++;
 
197
                }
 
198
        }
 
199
}
 
200
 
 
201
/*
 
202
Return-Path: <bertrik@sikken.nl>
 
203
Received: from koko.hhs.nl ([145.52.2.16] verified)
 
204
  by hhs.nl (CommuniGate Pro SMTP 4.3.6)
 
205
  with ESMTP id 89132066 for j.w.r.degoede@hhs.nl; Thu, 03 Jul 2008 15:19:55 +0200
 
206
Received: from exim (helo=koko)
 
207
        by koko.hhs.nl with local-smtp (Exim 4.62)
 
208
        (envelope-from <bertrik@sikken.nl>)
 
209
        id 1KEOj5-0000nq-KR
 
210
        for j.w.r.degoede@hhs.nl; Thu, 03 Jul 2008 15:19:55 +0200
 
211
Received: from [192.87.102.69] (port=33783 helo=filter1-ams.mf.surf.net)
 
212
        by koko.hhs.nl with esmtp (Exim 4.62)
 
213
        (envelope-from <bertrik@sikken.nl>)
 
214
        id 1KEOj5-0000nj-7r
 
215
        for j.w.r.degoede@hhs.nl; Thu, 03 Jul 2008 15:19:55 +0200
 
216
Received: from cardassian.kabelfoon.nl (cardassian3.kabelfoon.nl [62.45.45.105])
 
217
        by filter1-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m63DJsKW032598
 
218
        for <j.w.r.degoede@hhs.nl>; Thu, 3 Jul 2008 15:19:54 +0200
 
219
Received: from [192.168.1.1] (044-013-045-062.dynamic.caiway.nl [62.45.13.44])
 
220
        by cardassian.kabelfoon.nl (Postfix) with ESMTP id 77761341D9A
 
221
        for <j.w.r.degoede@hhs.nl>; Thu,  3 Jul 2008 15:19:54 +0200 (CEST)
 
222
Message-ID: <486CD1F9.8000307@sikken.nl>
 
223
Date: Thu, 03 Jul 2008 15:19:53 +0200
 
224
From: Bertrik Sikken <bertrik@sikken.nl>
 
225
User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
 
226
MIME-Version: 1.0
 
227
To: Hans de Goede <j.w.r.degoede@hhs.nl>
 
228
Subject: Re: pac207 bayer decompression algorithm license question
 
229
References: <48633F02.3040108@hhs.nl> <4863F611.80104@sikken.nl> <486CC6AF.7050509@hhs.nl>
 
230
In-Reply-To: <486CC6AF.7050509@hhs.nl>
 
231
X-Enigmail-Version: 0.95.6
 
232
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
 
233
Content-Transfer-Encoding: 7bit
 
234
X-Canit-CHI2: 0.00
 
235
X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN)
 
236
X-Spam-Score: 0.00 () [Tag at 8.00]
 
237
X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default)
 
238
X-Canit-Stats-ID: 90943081 - 6a9ff19e8165
 
239
X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.69
 
240
X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 03072008 #811719, status: clean
 
241
 
 
242
-----BEGIN PGP SIGNED MESSAGE-----
 
243
Hash: SHA1
 
244
 
 
245
Hans de Goede wrote:
 
246
| Bertrik Sikken wrote:
 
247
|> Hallo Hans,
 
248
|>
 
249
|> Hans de Goede wrote:
 
250
|>> I would like to also add support for decompressing the pac207's
 
251
|>> compressed
 
252
|>> bayer to this lib (and remove it from the kernel driver) and I've
 
253
|>> heard from Thomas Kaiser that you are a co-author of the
 
254
|>> decompression code. In order to add support for decompressing pac207
 
255
|>> compressed bayer to libv4l I need
 
256
|>> permission to relicense the decompression code under the LGPL
 
257
|>> (version 2 or later).
 
258
|>>
 
259
|>> Can you give me permission for this?
 
260
|>
 
261
|> Ja, vind ik goed.
 
262
|>
 
263
|
 
264
| Thanks!
 
265
|
 
266
| I'm currently working on adding support for the sn9c10x bayer
 
267
| compression to libv4l too, and I noticed this was written by you too.
 
268
|
 
269
| May I have your permission to relicense the sn9c10x bayer decompression
 
270
| code under the LGPL (version 2 or later)?
 
271
 
 
272
I hereby grant you permission to relicense the sn9c10x bayer
 
273
decompression code under the LGPL (version 2 or later).
 
274
 
 
275
Kind regards,
 
276
Bertrik
 
277
-----BEGIN PGP SIGNATURE-----
 
278
Version: GnuPG v1.4.7 (MingW32)
 
279
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
 
280
 
 
281
iD8DBQFIbNH5ETD6mlrWxPURAipvAJ9sv1ZpHyb81NMFejr6x0wqHX3i7QCfRDoB
 
282
jZi2e5lUjEh5KvS0dqXbi9I=
 
283
=KQfR
 
284
-----END PGP SIGNATURE-----
 
285
*/