Debian Codegraph @

lib/libv4lconvert/sn9c10x.c

raw
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., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 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 struct code_table {
29 int is_abs;
30 int len;
31 int val;
32 int unk;
33 };
34
35
36 /* local storage */
37 /* FIXME not thread safe !! */
38 static struct code_table table[256];
39 static int init_done;
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 } else if ((i & 0xE0) == 0x80) {
65 /* code 100 */
66 val = 4;
67 len = 3;
68 } else if ((i & 0xE0) == 0xA0) {
69 /* code 101 */
70 val = -4;
71 len = 3;
72 } else if ((i & 0xF0) == 0xD0) {
73 /* code 1101 */
74 val = 11;
75 len = 4;
76 } else if ((i & 0xF0) == 0xF0) {
77 /* code 1111 */
78 val = -11;
79 len = 4;
80 } else if ((i & 0xF8) == 0xC8) {
81 /* code 11001 */
82 val = 20;
83 len = 5;
84 } else if ((i & 0xFC) == 0xC0) {
85 /* code 110000 */
86 val = -20;
87 len = 6;
88 } else if ((i & 0xFC) == 0xC4) {
89 /* code 110001xx: unknown */
90 val = 0;
91 len = 8;
92 unk = 1;
93 } else if ((i & 0xF0) == 0xE0) {
94 /* code 1110xxxx */
95 is_abs = 1;
96 val = (i & 0x0F) << 4;
97 len = 8;
98 }
99 table[i].is_abs = is_abs;
100 table[i].val = val;
101 table[i].len = len;
102 table[i].unk = unk;
103 }
104
105 init_done = 1;
106 }
107
108
109 /*
110 sonix_decompress
111 ================
112 decompresses an image encoded by a SN9C101 camera controller chip.
113
114 IN width
115 height
116 inp pointer to compressed frame (with header already stripped)
117 OUT outp pointer to decompressed frame
118
119 Returns 0 if the operation was successful.
120 Returns <0 if operation failed.
121
122 */
123 void v4lconvert_decode_sn9c10x(const unsigned char *inp, unsigned char *outp,
124 int width, int height)
125 {
126 int row, col;
127 int val;
128 int bitpos;
129 unsigned char code;
130 const unsigned char *addr;
131
132 if (!init_done)
133 sonix_decompress_init();
134
135 bitpos = 0;
136 for (row = 0; row < height; row++) {
137 col = 0;
138
139 /* first two pixels in first two rows are stored as raw 8-bit */
140 if (row < 2) {
141 addr = inp + (bitpos >> 3);
142 code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
143 bitpos += 8;
144 *outp++ = code;
145
146 addr = inp + (bitpos >> 3);
147 code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
148 bitpos += 8;
149 *outp++ = code;
150
151 col += 2;
152 }
153
154 while (col < width) {
155 /* get bitcode from bitstream */
156 addr = inp + (bitpos >> 3);
157 code = (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
158
159 /* update bit position */
160 bitpos += table[code].len;
161
162 /* Skip unknown codes (most likely they indicate
163 a change of the delta's the various codes encode) */
164 if (table[code].unk)
165 continue;
166
167 /* calculate pixel value */
168 val = table[code].val;
169 if (!table[code].is_abs) {
170 /* value is relative to top and left pixel */
171 if (col < 2) {
172 /* left column: relative to top pixel */
173 val += outp[-2 * width];
174 } else if (row < 2) {
175 /* top row: relative to left pixel */
176 val += outp[-2];
177 } else {
178 /* main area: average of left pixel and top pixel */
179 val += (outp[-2] + outp[-2 * width]) / 2;
180 }
181 }
182
183 /* store pixel */
184 *outp++ = CLAMP(val);
185 col++;
186 }
187 }
188 }
189
190 /*
191 Return-Path: <[email protected]>
192 Received: from koko.hhs.nl ([145.52.2.16] verified)
193 by hhs.nl (CommuniGate Pro SMTP 4.3.6)
194 with ESMTP id 89132066 for [email protected]; Thu, 03 Jul 2008 15:19:55 +0200
195 Received: from exim (helo=koko)
196 by koko.hhs.nl with local-smtp (Exim 4.62)
197 (envelope-from <[email protected]>)
198 id 1KEOj5-0000nq-KR
199 for [email protected]; Thu, 03 Jul 2008 15:19:55 +0200
200 Received: from [192.87.102.69] (port=33783 helo=filter1-ams.mf.surf.net)
201 by koko.hhs.nl with esmtp (Exim 4.62)
202 (envelope-from <[email protected]>)
203 id 1KEOj5-0000nj-7r
204 for [email protected]; Thu, 03 Jul 2008 15:19:55 +0200
205 Received: from cardassian.kabelfoon.nl (cardassian3.kabelfoon.nl [62.45.45.105])
206 by filter1-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m63DJsKW032598
207 for <[email protected]>; Thu, 3 Jul 2008 15:19:54 +0200
208 Received: from [192.168.1.1] (044-013-045-062.dynamic.caiway.nl [62.45.13.44])
209 by cardassian.kabelfoon.nl (Postfix) with ESMTP id 77761341D9A
210 for <[email protected]>; Thu, 3 Jul 2008 15:19:54 +0200 (CEST)
211 Message-ID: <[email protected]>
212 Date: Thu, 03 Jul 2008 15:19:53 +0200
213 From: Bertrik Sikken <[email protected]>
214 User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
215 MIME-Version: 1.0
216 To: Hans de Goede <[email protected]>
217 Subject: Re: pac207 bayer decompression algorithm license question
218 References: <[email protected]> <[email protected]> <[email protected]>
219 In-Reply-To: <[email protected]>
220 X-Enigmail-Version: 0.95.6
221 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
222 Content-Transfer-Encoding: 7bit
223 X-Canit-CHI2: 0.00
224 X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN)
225 X-Spam-Score: 0.00 () [Tag at 8.00]
226 X-CanItPRO-Stream: hhs:[email protected] (inherits from hhs:default,base:default)
227 X-Canit-Stats-ID: 90943081 - 6a9ff19e8165
228 X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.69
229 X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 03072008 #811719, status: clean
230
231 -----BEGIN PGP SIGNED MESSAGE-----
232 Hash: SHA1
233
234 Hans de Goede wrote:
235 | Bertrik Sikken wrote:
236 |> Hallo Hans,
237 |>
238 |> Hans de Goede wrote:
239 |>> I would like to also add support for decompressing the pac207's
240 |>> compressed
241 |>> bayer to this lib (and remove it from the kernel driver) and I've
242 |>> heard from Thomas Kaiser that you are a co-author of the
243 |>> decompression code. In order to add support for decompressing pac207
244 |>> compressed bayer to libv4l I need
245 |>> permission to relicense the decompression code under the LGPL
246 |>> (version 2 or later).
247 |>>
248 |>> Can you give me permission for this?
249 |>
250 |> Ja, vind ik goed.
251 |>
252 |
253 | Thanks!
254 |
255 | I'm currently working on adding support for the sn9c10x bayer
256 | compression to libv4l too, and I noticed this was written by you too.
257 |
258 | May I have your permission to relicense the sn9c10x bayer decompression
259 | code under the LGPL (version 2 or later)?
260
261 I hereby grant you permission to relicense the sn9c10x bayer
262 decompression code under the LGPL (version 2 or later).
263
264 Kind regards,
265 Bertrik
266 -----BEGIN PGP SIGNATURE-----
267 Version: GnuPG v1.4.7 (MingW32)
268 Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
269
270 iD8DBQFIbNH5ETD6mlrWxPURAipvAJ9sv1ZpHyb81NMFejr6x0wqHX3i7QCfRDoB
271 jZi2e5lUjEh5KvS0dqXbi9I=
272 =KQfR
273 -----END PGP SIGNATURE-----
274 */