blob: d110e38c4de0bbbfdbe74b26166ea3f581e2d9c6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Linux driver for Philips webcam
2 Decompression frontend.
3 (C) 1999-2003 Nemosoft Unv.
Luc Saillard2b455db2006-04-24 10:29:46 -03004 (C) 2004-2006 Luc Saillard (luc@saillard.org)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
7 driver and thus may have bugs that are not present in the original version.
8 Please send bug reports and support requests to <luc@saillard.org>.
9 The decompression routines have been implemented by reverse-engineering the
10 Nemosoft binary pwcx module. Caveat emptor.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Luc Saillard2b455db2006-04-24 10:29:46 -030025
26 vim: set ts=8:
Linus Torvalds1da177e2005-04-16 15:20:36 -070027*/
28
29#include <asm/current.h>
30#include <asm/types.h>
31
32#include "pwc.h"
33#include "pwc-uncompress.h"
Luc Saillard2b455db2006-04-24 10:29:46 -030034#include "pwc-dec1.h"
35#include "pwc-dec23.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Hans de Goede885fe182011-06-06 15:33:44 -030037int pwc_decompress(struct pwc_device *pdev, struct pwc_frame_buf *fbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int n, line, col, stride;
40 void *yuv, *image;
41 u16 *src;
42 u16 *dsty, *dstu, *dstv;
43
Hans de Goede885fe182011-06-06 15:33:44 -030044 image = vb2_plane_vaddr(&fbuf->vb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 yuv = fbuf->data + pdev->frame_header_size; /* Skip header */
47
48 /* Raw format; that's easy... */
Hans Verkuil479567c2010-09-12 17:05:11 -030049 if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 {
Luc Saillard2b455db2006-04-24 10:29:46 -030051 struct pwc_raw_frame *raw_frame = image;
52 raw_frame->type = cpu_to_le16(pdev->type);
53 raw_frame->vbandlength = cpu_to_le16(pdev->vbandlength);
54 /* cmd_buf is always 4 bytes, but sometimes, only the
55 * first 3 bytes is filled (Nala case). We can
56 * determine this using the type of the webcam */
57 memcpy(raw_frame->cmd, pdev->cmd_buf, 4);
58 memcpy(raw_frame+1, yuv, pdev->frame_size);
Hans de Goede885fe182011-06-06 15:33:44 -030059 vb2_set_plane_payload(&fbuf->vb, 0,
60 pdev->frame_size + sizeof(struct pwc_raw_frame));
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return 0;
62 }
63
Hans de Goede885fe182011-06-06 15:33:44 -030064 vb2_set_plane_payload(&fbuf->vb, 0, pdev->view.size);
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (pdev->vbandlength == 0) {
Luc Saillard2b455db2006-04-24 10:29:46 -030067 /* Uncompressed mode.
68 * We copy the data into the output buffer, using the viewport
69 * size (which may be larger than the image size).
70 * Unfortunately we have to do a bit of byte stuffing to get
71 * the desired output format/size.
72 *
73 * We do some byte shuffling here to go from the
74 * native format to YUV420P.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Luc Saillard2b455db2006-04-24 10:29:46 -030076 src = (u16 *)yuv;
77 n = pdev->view.x * pdev->view.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Luc Saillard2b455db2006-04-24 10:29:46 -030079 /* offset in Y plane */
80 stride = pdev->view.x * pdev->offset.y + pdev->offset.x;
81 dsty = (u16 *)(image + stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Luc Saillard2b455db2006-04-24 10:29:46 -030083 /* offsets in U/V planes */
84 stride = pdev->view.x * pdev->offset.y / 4 + pdev->offset.x / 2;
85 dstu = (u16 *)(image + n + stride);
86 dstv = (u16 *)(image + n + n / 4 + stride);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Luc Saillard2b455db2006-04-24 10:29:46 -030088 /* increment after each line */
89 stride = (pdev->view.x - pdev->image.x) / 2; /* u16 is 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Luc Saillard2b455db2006-04-24 10:29:46 -030091 for (line = 0; line < pdev->image.y; line++) {
92 for (col = 0; col < pdev->image.x; col += 4) {
93 *dsty++ = *src++;
94 *dsty++ = *src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (line & 1)
Luc Saillard2b455db2006-04-24 10:29:46 -030096 *dstv++ = *src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 else
Luc Saillard2b455db2006-04-24 10:29:46 -030098 *dstu++ = *src++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Luc Saillard2b455db2006-04-24 10:29:46 -0300100 dsty += stride;
101 if (line & 1)
102 dstv += (stride >> 1);
103 else
104 dstu += (stride >> 1);
105 }
106
107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300109
Luc Saillard2b455db2006-04-24 10:29:46 -0300110 /*
111 * Compressed;
112 * the decompressor routines will write the data in planar format
113 * immediately.
114 */
115 if (pdev->vsize == PSZ_VGA && pdev->vframes == 5 && pdev->vsnapshot) {
116 PWC_ERROR("Mode Bayer is not supported for now\n");
117 /* flags |= PWCX_FLAG_BAYER; */
118 return -ENXIO; /* No such device or address: missing decompressor */
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Luc Saillard2b455db2006-04-24 10:29:46 -0300121 if (DEVICE_USE_CODEC1(pdev->type)) {
122
123 /* TODO & FIXME */
124 PWC_ERROR("This chipset is not supported for now\n");
125 return -ENXIO; /* No such device or address: missing decompressor */
126
127 } else {
128 pwc_dec23_decompress(pdev, yuv, image, PWCX_FLAG_PLANAR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130 return 0;
131}
132
133
Luc Saillard2b455db2006-04-24 10:29:46 -0300134/* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */