1 /*
2
3 # PAC207 decoder
4 # Bertrik.Sikken. Thomas Kaiser (C) 2005
5 # Copyright (C) 2003 2004 2005 Michel Xhaard
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
20
21 # Note this code was originally licensed under the GNU GPL instead of the
22 # GNU LGPL, its license has been changed with permission, see the permission
23 # mails at the end of this file.
24
25 */
26
27 #include <string.h>
28 #include "libv4lconvert-priv.h"
29
30 #define CLIP(color) (unsigned char)(((color) > 0xFF) ? 0xff : (((color) < 0) ? 0 : (color)))
31
32 /* FIXME not threadsafe */
33 static int decoder_initialized;
34
35 static struct {
36 unsigned char is_abs;
37 unsigned char len;
38 signed char val;
39 } table[256];
40
init_pixart_decoder(void)41 static void init_pixart_decoder(void)
42 {
43 int i;
44 int is_abs, val, len;
45
46 for (i = 0; i < 256; i++) {
47 is_abs = 0;
48 val = 0;
49 len = 0;
50 if ((i & 0xC0) == 0) {
51 /* code 00 */
52 val = 0;
53 len = 2;
54 } else if ((i & 0xC0) == 0x40) {
55 /* code 01 */
56 val = -1;
57 len = 2;
58 } else if ((i & 0xC0) == 0x80) {
59 /* code 10 */
60 val = 1;
61 len = 2;
62 } else if ((i & 0xF0) == 0xC0) {
63 /* code 1100 */
64 val = -2;
65 len = 4;
66 } else if ((i & 0xF0) == 0xD0) {
67 /* code 1101 */
68 val = 2;
69 len = 4;
70 } else if ((i & 0xF8) == 0xE0) {
71 /* code 11100 */
72 val = -3;
73 len = 5;
74 } else if ((i & 0xF8) == 0xE8) {
75 /* code 11101 */
76 val = 3;
77 len = 5;
78 } else if ((i & 0xFC) == 0xF0) {
79 /* code 111100 */
80 val = -4;
81 len = 6;
82 } else if ((i & 0xFC) == 0xF4) {
83 /* code 111101 */
84 val = 4;
85 len = 6;
86 } else if ((i & 0xF8) == 0xF8) {
87 /* code 11111xxxxxx */
88 is_abs = 1;
89 val = 0;
90 len = 5;
91 }
92 table[i].is_abs = is_abs;
93 table[i].val = val;
94 table[i].len = len;
95 }
96 decoder_initialized = 1;
97 }
98
getByte(const unsigned char * inp,unsigned int bitpos)99 static inline unsigned char getByte(const unsigned char *inp,
100 unsigned int bitpos)
101 {
102 const unsigned char *addr;
103
104 addr = inp + (bitpos >> 3);
105 return (addr[0] << (bitpos & 7)) | (addr[1] >> (8 - (bitpos & 7)));
106 }
107
getShort(const unsigned char * pt)108 static inline unsigned short getShort(const unsigned char *pt)
109 {
110 return ((pt[0] << 8) | pt[1]);
111 }
112
113 static int
pac_decompress_row(const unsigned char * inp,unsigned char * outp,int width,int step_size,int abs_bits)114 pac_decompress_row(const unsigned char *inp, unsigned char *outp, int width,
115 int step_size, int abs_bits)
116 {
117 int col;
118 int val;
119 int bitpos;
120 unsigned char code;
121
122 if (!decoder_initialized)
123 init_pixart_decoder();
124
125 /* first two pixels are stored as raw 8-bit */
126 *outp++ = inp[2];
127 *outp++ = inp[3];
128 bitpos = 32;
129
130 /* main decoding loop */
131 for (col = 2; col < width; col++) {
132 /* get bitcode */
133
134 code = getByte(inp, bitpos);
135 bitpos += table[code].len;
136
137 /* calculate pixel value */
138 if (table[code].is_abs) {
139 /* absolute value: get 6 more bits */
140 code = getByte(inp, bitpos);
141 bitpos += abs_bits;
142 *outp++ = code & ~(0xff >> abs_bits);
143 } else {
144 /* relative to left pixel */
145 val = outp[-2] + table[code].val * step_size;
146 *outp++ = CLIP(val);
147 }
148 }
149
150 /* return line length, rounded up to next 16-bit word */
151 return 2 * ((bitpos + 15) / 16);
152 }
153
v4lconvert_decode_pac207(struct v4lconvert_data * data,const unsigned char * inp,int src_size,unsigned char * outp,int width,int height)154 int v4lconvert_decode_pac207(struct v4lconvert_data *data,
155 const unsigned char *inp, int src_size, unsigned char *outp,
156 int width, int height)
157 {
158 /* we should received a whole frame with header and EOL marker
159 in myframe->data and return a GBRG pattern in frame->tmpbuffer
160 remove the header then copy line by line EOL is set with 0x0f 0xf0 marker
161 or 0x1e 0xe1 for compressed line*/
162 const unsigned char *end = inp + src_size;
163 unsigned short word;
164 int row;
165
166 /* iterate over all rows */
167 for (row = 0; row < height; row++) {
168 if ((inp + 2) > end) {
169 V4LCONVERT_ERR("incomplete pac207 frame\n");
170 return -1;
171 }
172 word = getShort(inp);
173 switch (word) {
174 case 0x0FF0:
175 memcpy(outp, inp + 2, width);
176 inp += (2 + width);
177 break;
178 case 0x1EE1:
179 inp += pac_decompress_row(inp, outp, width, 5, 6);
180 break;
181
182 case 0x2DD2:
183 inp += pac_decompress_row(inp, outp, width, 9, 5);
184 break;
185
186 case 0x3CC3:
187 inp += pac_decompress_row(inp, outp, width, 17, 4);
188 break;
189
190 case 0x4BB4:
191 /* skip or copy line? */
192 memcpy(outp, outp - 2 * width, width);
193 inp += 2;
194 break;
195
196 default: /* corrupt frame */
197 V4LCONVERT_ERR("unknown pac207 row header: 0x%04x\n", (int)word);
198 return -1;
199 }
200 outp += width;
201 }
202
203 return 0;
204 }
205
206
207
208
209 /*
210 Return-Path: <thomas@kaiser-linux.li>
211 Received: from koko.hhs.nl ([145.52.2.16] verified)
212 by hhs.nl (CommuniGate Pro SMTP 4.3.6)
213 with ESMTP id 88906346 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 01:17:00 +0200
214 Received: from exim (helo=koko)
215 by koko.hhs.nl with local-smtp (Exim 4.62)
216 (envelope-from <thomas@kaiser-linux.li>)
217 id 1KBeEW-0001qu-H6
218 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 01:17:00 +0200
219 Received: from [192.87.102.74] (port=41049 helo=filter6-ams.mf.surf.net)
220 by koko.hhs.nl with esmtp (Exim 4.62)
221 (envelope-from <thomas@kaiser-linux.li>)
222 id 1KBeEV-0001qn-2T
223 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 01:17:00 +0200
224 Received: from smtp0.lie-comtel.li (smtp0.lie-comtel.li [217.173.238.80])
225 by filter6-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5PNGwSF007539
226 for <j.w.r.degoede@hhs.nl>; Thu, 26 Jun 2008 01:16:58 +0200
227 Received: from localhost (localhost.lie-comtel.li [127.0.0.1])
228 by smtp0.lie-comtel.li (Postfix) with ESMTP id DDB609FEC1D;
229 Thu, 26 Jun 2008 00:16:56 +0100 (GMT-1)
230 X-Virus-Scanned: Virus scanned by amavis at smtp.lie-comtel.li
231 Received: from [192.168.0.16] (217-173-228-198.cmts.powersurf.li [217.173.228.198])
232 by smtp0.lie-comtel.li (Postfix) with ESMTP id 80B589FEC19;
233 Thu, 26 Jun 2008 00:16:56 +0100 (GMT-1)
234 Message-ID: <4862D211.3000802@kaiser-linux.li>
235 Date: Thu, 26 Jun 2008 01:17:37 +0200
236 From: Thomas Kaiser <thomas@kaiser-linux.li>
237 User-Agent: Thunderbird 2.0.0.14 (X11/20080505)
238 MIME-Version: 1.0
239 To: Hans de Goede <j.w.r.degoede@hhs.nl>
240 CC: Thomas Kaiser <spca5xx@kaiser-linux.li>, bertrik@zonnet.nl,
241 mxhaard@magic.fr
242 Subject: Re: pac207 bayer decompression algorithm license question
243 References: <4862C0A4.3060003@hhs.nl>
244 In-Reply-To: <4862C0A4.3060003@hhs.nl>
245 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
246 Content-Transfer-Encoding: 7bit
247 X-Canit-CHI2: 0.00
248 X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN)
249 X-Spam-Score: 0.00 () [Tag at 8.00]
250 X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default)
251 X-Canit-Stats-ID: 88604132 - 38b3b44cd798
252 X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.74
253 X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 25062008 #787666, status: clean
254
255 Hello Hans
256
257 Hans de Goede wrote:
258 > Hi,
259 >
260 > As you may have seen on the mailinglist, I've created a userspace
261 > library to handle cam specific format handling in userspace where it
262 > belongs, see:
263 > http://hansdegoede.livejournal.com/
264 Yes, I saw it on the mail list and I think it is a good idea :-)
265 >
266 > I would like to also add support for decompressing the pac207's
267 > compressed bayer to this lib (and remove it from the kernel driver)
268 > for this I need permission to relicense the decompress code under the
269 > LGPL (version 2 or later).
270 Actually, this was done by Bertrik Sikken (bertrik@zonnet.nl), Michel
271 Xhaard (mxhaard@magic.fr) and me. But Bertrik was the one who found out
272 how to decode the lines :-)
273 >
274 > Can you give me permission for this, or if the code is not yours put
275 > me in contact with someone who can?
276 For me it's no problem to release it with LGPL. Maybe you have to ask
277 the other one's also.
278 >
279 > Thanks & Regards,
280 >
281 > Hans
282
283 Rgeards, Thomas
284 */
285
286 /*
287 Return-Path: <mxhaard@magic.fr>
288 Received: from koko.hhs.nl ([145.52.2.16] verified)
289 by hhs.nl (CommuniGate Pro SMTP 4.3.6)
290 with ESMTP id 88910192 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 09:15:37 +0200
291 Received: from exim (helo=koko)
292 by koko.hhs.nl with local-smtp (Exim 4.62)
293 (envelope-from <mxhaard@magic.fr>)
294 id 1KBlhh-0006Fi-Oe
295 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 09:15:37 +0200
296 Received: from [194.171.167.220] (port=54180 helo=filter4-til.mf.surf.net)
297 by koko.hhs.nl with esmtp (Exim 4.62)
298 (envelope-from <mxhaard@magic.fr>)
299 id 1KBlhh-0006Fd-FY
300 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 09:15:37 +0200
301 Received: from smtp4-g19.free.fr (smtp4-g19.free.fr [212.27.42.30])
302 by filter4-til.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5Q7FY1I006360
303 for <j.w.r.degoede@hhs.nl>; Thu, 26 Jun 2008 09:15:34 +0200
304 Received: from smtp4-g19.free.fr (localhost.localdomain [127.0.0.1])
305 by smtp4-g19.free.fr (Postfix) with ESMTP id 51C683EA0E7;
306 Thu, 26 Jun 2008 09:15:34 +0200 (CEST)
307 Received: from [192.168.1.11] (lns-bzn-54-82-251-105-53.adsl.proxad.net [82.251.105.53])
308 by smtp4-g19.free.fr (Postfix) with ESMTP id 1149E3EA0C7;
309 Thu, 26 Jun 2008 09:15:34 +0200 (CEST)
310 From: Michel Xhaard <mxhaard@magic.fr>
311 To: Hans de Goede <j.w.r.degoede@hhs.nl>
312 Subject: Re: pac207 bayer decompression algorithm license question
313 Date: Thu, 26 Jun 2008 11:15:32 +0200
314 User-Agent: KMail/1.9.5
315 Cc: bertrik@zonnet.nl, spca5xx@kaiser-linux.li,
316 "Jean-Francois Moine" <moinejf@free.fr>
317 References: <48633F02.3040108@hhs.nl>
318 In-Reply-To: <48633F02.3040108@hhs.nl>
319 MIME-Version: 1.0
320 Content-Type: text/plain;
321 charset="iso-8859-1"
322 Content-Transfer-Encoding: quoted-printable
323 Content-Disposition: inline
324 Message-Id: <200806261115.32909.mxhaard@magic.fr>
325 X-Canit-CHI2: 0.00
326 X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN)
327 X-Spam-Score: 0.00 () [Tag at 8.00]
328 X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default)
329 X-Canit-Stats-ID: 88656338 - 0dde233cb8b5
330 X-Scanned-By: CanIt (www . roaringpenguin . com) on 194.171.167.220
331 X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 26062008 #787720, status: clean
332
333 Le jeudi 26 juin 2008 09:02, Hans de Goede a =E9crit=A0:
334 > Hi,
335 >
336 > As you may have seen on the mailinglist, I've created a userspace library
337 > to handle cam specific format handling in userspace, see:
338 > http://hansdegoede.livejournal.com/
339 >
340 > I would like to also add support for decompressing the pac207's compressed
341 > bayer to this lib (and remove it from the kernel driver) and I've heard
342 > from Thomas Kaiser that you are a co-author of the decompression code. In
343 > order to add support for decompressing pac207 compressed bayer to libv4l I
344 > need permission to relicense the decompression code under the LGPL (versi=
345 on
346 > 2 or later).
347 >
348 > Can you give me permission for this?
349 >
350 > Thanks & Regards,
351 >
352 > Hans
353 >
354 >
355 >
356 > p.s.
357 >
358 > Thomas has already given permission.
359
360 =46or me it is ok and a good idea for all free world familly ;-).
361 Bests regards
362 =2D-=20
363 Michel Xhaard
364 http://mxhaard.free.fr
365 */
366
367 /*
368 Return-Path: <bertrik@sikken.nl>
369 Received: from koko.hhs.nl ([145.52.2.16] verified)
370 by hhs.nl (CommuniGate Pro SMTP 4.3.6)
371 with ESMTP id 88940205 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 22:03:30 +0200
372 Received: from exim (helo=koko)
373 by koko.hhs.nl with local-smtp (Exim 4.62)
374 (envelope-from <bertrik@sikken.nl>)
375 id 1KBxgo-0003Dj-ET
376 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 22:03:30 +0200
377 Received: from [192.87.102.69] (port=51992 helo=filter1-ams.mf.surf.net)
378 by koko.hhs.nl with esmtp (Exim 4.62)
379 (envelope-from <bertrik@sikken.nl>)
380 id 1KBxgo-0003Dd-5i
381 for j.w.r.degoede@hhs.nl; Thu, 26 Jun 2008 22:03:30 +0200
382 Received: from pelian.kabelfoon.nl (pelian3.kabelfoon.nl [62.45.45.106])
383 by filter1-ams.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id m5QK3ThE007720
384 for <j.w.r.degoede@hhs.nl>; Thu, 26 Jun 2008 22:03:29 +0200
385 Received: from [192.168.1.1] (062-015-045-062.dynamic.caiway.nl [62.45.15.62])
386 by pelian.kabelfoon.nl (Postfix) with ESMTP id 9239B428100
387 for <j.w.r.degoede@hhs.nl>; Thu, 26 Jun 2008 22:03:29 +0200 (CEST)
388 Message-ID: <4863F611.80104@sikken.nl>
389 Date: Thu, 26 Jun 2008 22:03:29 +0200
390 From: Bertrik Sikken <bertrik@sikken.nl>
391 User-Agent: Thunderbird 2.0.0.14 (Windows/20080421)
392 MIME-Version: 1.0
393 To: Hans de Goede <j.w.r.degoede@hhs.nl>
394 Subject: Re: pac207 bayer decompression algorithm license question
395 References: <48633F02.3040108@hhs.nl>
396 In-Reply-To: <48633F02.3040108@hhs.nl>
397 X-Enigmail-Version: 0.95.6
398 Content-Type: text/plain; charset=ISO-8859-1; format=flowed
399 Content-Transfer-Encoding: 7bit
400 X-Canit-CHI2: 0.00
401 X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN)
402 X-Spam-Score: 0.00 () [Tag at 8.00]
403 X-CanItPRO-Stream: hhs:j.w.r.degoede@hhs.nl (inherits from hhs:default,base:default)
404 X-Canit-Stats-ID: 88938005 - ef1f0836ffc7
405 X-Scanned-By: CanIt (www . roaringpenguin . com) on 192.87.102.69
406 X-Anti-Virus: Kaspersky Anti-Virus for MailServers 5.5.2/RELEASE, bases: 26062008 #787877, status: clean
407
408 Hallo Hans,
409
410 Hans de Goede wrote:
411 > Hi,
412 >
413 > As you may have seen on the mailinglist, I've created a userspace
414 > library to
415 > handle cam specific format handling in userspace, see:
416 > http://hansdegoede.livejournal.com/
417
418 O leuk, zoiets is naar mijn idee precies wat er nodig is voor webcam
419 support onder linux. Ik ben een jaar of 3 geleden heel actief geweest
420 met een aantal webcams, maar doe er tegenwoordig helemaal niets meer
421 aan.
422
423 > I would like to also add support for decompressing the pac207's compressed
424 > bayer to this lib (and remove it from the kernel driver) and I've heard
425 > from Thomas Kaiser that you are a co-author of the decompression code.
426 > In order to add support for decompressing pac207 compressed bayer to
427 > libv4l I need
428 > permission to relicense the decompression code under the LGPL (version 2
429 > or later).
430 >
431 > Can you give me permission for this?
432
433 Ja, vind ik goed.
434
435 Vriendelijke groet,
436 Bertrik
437 */
438