1 /* Linux driver for Philips webcam
2    Decompression frontend.
3    (C) 1999-2003 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    dynamically adjust our structure.
36  */
37 const int pwc_decompressor_version = PWC_MAJOR;
38 
39 /* Add decompressor to list, ignoring duplicates */
pwc_register_decompressor(struct pwc_decompressor * pwcd)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 */
pwc_unregister_decompressor(int type)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 */
pwc_find_decompressor(int type)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 
pwc_decompress(struct pwc_device * pdev)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;
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 	yuv = fbuf->data + pdev->frame_header_size;  /* Skip header */
102 	if (pdev->vbandlength == 0) {
103 		/* Uncompressed mode. We copy the data into the output buffer,
104 		   using the viewport size (which may be larger than the image
105 		   size). Unfortunately we have to do a bit of byte stuffing
106 		   to get the desired output format/size.
107 		 */
108 			/*
109 			 * We do some byte shuffling here to go from the
110 			 * native format to YUV420P.
111 			 */
112 			src = (u16 *)yuv;
113 			n = pdev->view.x * pdev->view.y;
114 
115 			/* offset in Y plane */
116 			stride = pdev->view.x * pdev->offset.y + pdev->offset.x;
117 			dsty = (u16 *)(image + stride);
118 
119 			/* offsets in U/V planes */
120 			stride = pdev->view.x * pdev->offset.y / 4 + pdev->offset.x / 2;
121 			dstu = (u16 *)(image + n +         stride);
122 			dstv = (u16 *)(image + n + n / 4 + stride);
123 
124 			/* increment after each line */
125 			stride = (pdev->view.x - pdev->image.x) / 2; /* u16 is 2 bytes */
126 
127 			for (line = 0; line < pdev->image.y; line++) {
128 				for (col = 0; col < pdev->image.x; col += 4) {
129 					*dsty++ = *src++;
130 					*dsty++ = *src++;
131 					if (line & 1)
132 						*dstv++ = *src++;
133 					else
134 						*dstu++ = *src++;
135 				}
136 				dsty += stride;
137 				if (line & 1)
138 					dstv += (stride >> 1);
139 				else
140 					dstu += (stride >> 1);
141 			}
142 	}
143 	else {
144 		/* Compressed; the decompressor routines will write the data
145 		   in planar format immediately.
146 		 */
147 		if (pdev->decompressor)
148 			pdev->decompressor->decompress(
149 				&pdev->image, &pdev->view, &pdev->offset,
150 				yuv, image,
151 				1,
152 				pdev->decompress_data, pdev->vbandlength);
153 		else
154 			return -ENXIO; /* No such device or address: missing decompressor */
155 	}
156 	return 0;
157 }
158 
159 /* Make sure these functions are available for the decompressor plugin
160    both when this code is compiled into the kernel or as as module.
161  */
162 
163 EXPORT_SYMBOL_NOVERS(pwc_decompressor_version);
164 EXPORT_SYMBOL(pwc_register_decompressor);
165 EXPORT_SYMBOL(pwc_unregister_decompressor);
166