File: /usr/src/linux/drivers/usb/pwc-uncompress.c

1     /* Linux driver for Philips webcam 
2        Decompression frontend.
3        (C) 1999-2001 Nemosoft Unv. (webcam@smcc.demon.nl)
4     
5        This program is free software; you can redistribute it and/or modify
6        it under the terms of the GNU General Public License as published by
7        the Free Software Foundation; either version 2 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 General Public License for more details.
14     
15        You should have received a copy of the GNU 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     /*
20        This is where the decompression routines register and unregister 
21        themselves. It also has a decompressor wrapper function.
22     */
23     
24     #include <asm/types.h>
25     
26     #include "pwc.h"
27     #include "pwc-uncompress.h"
28     
29     
30     /* This contains a list of all registered decompressors */
31     static LIST_HEAD(pwc_decompressor_list);
32     
33     /* Should the pwc_decompress structure ever change, we increase the 
34        version number so that we don't get nasty surprises, or can 
35        dynamicly adjust our structure.
36      */
37     const int pwc_decompressor_version = PWC_MAJOR;
38     
39     /* Add decompressor to list, ignoring duplicates */
40     void pwc_register_decompressor(struct pwc_decompressor *pwcd)
41     {
42     	if (pwc_find_decompressor(pwcd->type) == NULL) {
43     		Trace(TRACE_PWCX, "Adding decompressor for model %d.\n", pwcd->type);
44     		list_add_tail(&pwcd->pwcd_list, &pwc_decompressor_list);
45     	}
46     }
47     
48     /* Remove decompressor from list */
49     void pwc_unregister_decompressor(int type)
50     {
51     	struct pwc_decompressor *find;
52     	
53     	find = pwc_find_decompressor(type);
54     	if (find != NULL) {
55     		Trace(TRACE_PWCX, "Removing decompressor for model %d.\n", type);
56     		list_del(&find->pwcd_list);
57     	}
58     }
59     
60     /* Find decompressor in list */
61     struct pwc_decompressor *pwc_find_decompressor(int type)
62     {
63     	struct list_head *tmp;
64     	struct pwc_decompressor *pwcd;
65     
66     	list_for_each(tmp, &pwc_decompressor_list) {
67     		pwcd  = list_entry(tmp, struct pwc_decompressor, pwcd_list);
68     		if (pwcd->type == type)
69     			return pwcd;
70     	}
71     	return NULL;
72     }
73     
74     
75     
76     int pwc_decompress(struct pwc_device *pdev)
77     {
78     	struct pwc_frame_buf *fbuf;
79     	int n, line, col, stride;
80     	void *yuv, *image, *dst;
81     	u16 *src;
82     	u16 *dsty, *dstu, *dstv;
83     
84     	
85     	if (pdev == NULL)
86     		return -EFAULT;
87     #if defined(__KERNEL__) && defined(PWC_MAGIC)
88     	if (pdev->magic != PWC_MAGIC) {
89     		Err("pwc_decompress(): magic failed.\n");
90     		return -EFAULT;
91     	}
92     #endif
93     
94     	fbuf = pdev->read_frame;
95     	if (fbuf == NULL)
96     		return -EFAULT;
97     	image = pdev->image_ptr[pdev->fill_image];
98     	if (!image)
99     		return -EFAULT;
100     	
101     #if PWC_DEBUG
102     	/* This is a quickie */
103     	if (pdev->vpalette == VIDEO_PALETTE_RAW) {
104     		memcpy(image, fbuf->data, pdev->frame_size);
105     		return 0;
106     	}
107     #endif
108     
109     	yuv = fbuf->data + pdev->frame_header_size;  /* Skip header */
110     	if (pdev->vbandlength == 0) { 
111     		/* Uncompressed mode. We copy the data into the output buffer,
112     		   using the viewport size (which may be larger than the image
113     		   size). Unfortunately we have to do a bit of byte stuffing
114     		   to get the desired output format/size.
115     		 */
116     		switch (pdev->vpalette) {
117     		case VIDEO_PALETTE_YUV420:
118     			/* Calculate byte offsets per line in image & view */
119     			n   = (pdev->image.x * 3) / 2;
120     			col = (pdev->view.x  * 3) / 2;
121     			/* Offset into image */
122     			dst = image + (pdev->view.x * pdev->offset.y + pdev->offset.x) * 3 / 2;
123     			for (line = 0; line < pdev->image.y; line++) {
124     				memcpy(dst, yuv, n);
125     				yuv += n;
126     				dst += col;
127     			}
128     			break;
129     
130     		case VIDEO_PALETTE_YUV420P:
131     			/* 
132     			 * We do some byte shuffling here to go from the 
133     			 * native format to YUV420P.
134     			 */
135     			src = (u16 *)yuv;
136     			n = pdev->view.x * pdev->view.y;
137     
138     			/* offset in Y plane */
139     			stride = pdev->view.x * pdev->offset.y + pdev->offset.x;
140     			dsty = (u16 *)(image + stride);
141     
142     			/* offsets in U/V planes */
143     			stride = pdev->view.x * pdev->offset.y / 4 + pdev->offset.x / 2;
144     			dstu = (u16 *)(image + n +         stride);
145     			dstv = (u16 *)(image + n + n / 4 + stride);
146     
147     			/* increment after each line */
148     			stride = (pdev->view.x - pdev->image.x) / 2; /* u16 is 2 bytes */
149     
150     			for (line = 0; line < pdev->image.y; line++) {
151     				for (col = 0; col < pdev->image.x; col += 4) {
152     					*dsty++ = *src++;
153     					*dsty++ = *src++;
154     					if (line & 1)
155     						*dstv++ = *src++;
156     					else
157     						*dstu++ = *src++;
158     				}
159     				dsty += stride;
160     				if (line & 1)
161     					dstv += (stride >> 1);
162     				else
163     					dstu += (stride >> 1);
164     			}
165     			break;
166     		}
167     	}
168     	else { 
169     		/* Compressed; the decompressor routines will write the data 
170     		   in interlaced or planar format immediately.
171     		 */
172     		if (pdev->decompressor)
173     			pdev->decompressor->decompress(
174     				&pdev->image, &pdev->view, &pdev->offset,
175     				yuv, image, 
176     				pdev->vpalette == VIDEO_PALETTE_YUV420P ? 1 : 0,
177     				pdev->decompress_data, pdev->vbandlength);
178     		else
179     			return -ENXIO; /* No such device or address: missing decompressor */
180     	}
181     	return 0;
182     }
183     
184     /* Make sure these functions are available for the decompressor plugin
185        both when this code is compiled into the kernel or as as module.
186      */
187     
188     EXPORT_SYMBOL_NOVERS(pwc_decompressor_version);
189     EXPORT_SYMBOL(pwc_register_decompressor);
190     EXPORT_SYMBOL(pwc_unregister_decompressor);
191