1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Linux driver for Philips webcam
3    Decompression frontend.
4    (C) 1999-2003 Nemosoft Unv.
5    (C) 2004-2006 Luc Saillard (luc@saillard.org)
6 
7    NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
8    driver and thus may have bugs that are not present in the original version.
9    Please send bug reports and support requests to <luc@saillard.org>.
10    The decompression routines have been implemented by reverse-engineering the
11    Nemosoft binary pwcx module. Caveat emptor.
12 
13 
14    vim: set ts=8:
15 */
16 
17 #include <asm/current.h>
18 #include <asm/types.h>
19 
20 #include "pwc.h"
21 #include "pwc-dec1.h"
22 #include "pwc-dec23.h"
23 
24 int pwc_decompress(struct pwc_device *pdev, struct pwc_frame_buf *fbuf)
25 {
26 	int n, line, col;
27 	void *yuv, *image;
28 	u16 *src;
29 	u16 *dsty, *dstu, *dstv;
30 
31 	image = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
32 
33 	yuv = fbuf->data + pdev->frame_header_size;  /* Skip header */
34 
35 	/* Raw format; that's easy... */
36 	if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
37 	{
38 		struct pwc_raw_frame *raw_frame = image;
39 		raw_frame->type = cpu_to_le16(pdev->type);
40 		raw_frame->vbandlength = cpu_to_le16(pdev->vbandlength);
41 			/* cmd_buf is always 4 bytes, but sometimes, only the
42 			 * first 3 bytes is filled (Nala case). We can
43 			 * determine this using the type of the webcam */
44 		memcpy(raw_frame->cmd, pdev->cmd_buf, 4);
45 		memcpy(raw_frame+1, yuv, pdev->frame_size);
46 		vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
47 			pdev->frame_size + sizeof(struct pwc_raw_frame));
48 		return 0;
49 	}
50 
51 	vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
52 			      pdev->width * pdev->height * 3 / 2);
53 
54 	if (pdev->vbandlength == 0) {
55 		/* Uncompressed mode.
56 		 *
57 		 * We do some byte shuffling here to go from the
58 		 * native format to YUV420P.
59 		 */
60 		src = (u16 *)yuv;
61 		n = pdev->width * pdev->height;
62 		dsty = (u16 *)(image);
63 		dstu = (u16 *)(image + n);
64 		dstv = (u16 *)(image + n + n / 4);
65 
66 		for (line = 0; line < pdev->height; line++) {
67 			for (col = 0; col < pdev->width; col += 4) {
68 				*dsty++ = *src++;
69 				*dsty++ = *src++;
70 				if (line & 1)
71 					*dstv++ = *src++;
72 				else
73 					*dstu++ = *src++;
74 			}
75 		}
76 
77 		return 0;
78 	}
79 
80 	/*
81 	 * Compressed;
82 	 * the decompressor routines will write the data in planar format
83 	 * immediately.
84 	 */
85 	if (DEVICE_USE_CODEC1(pdev->type)) {
86 
87 		/* TODO & FIXME */
88 		PWC_ERROR("This chipset is not supported for now\n");
89 		return -ENXIO; /* No such device or address: missing decompressor */
90 
91 	} else {
92 		pwc_dec23_decompress(pdev, yuv, image);
93 	}
94 	return 0;
95 }
96