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 #include <asm/current.h>
15 #include <asm/types.h>
16 
17 #include "pwc.h"
18 #include "pwc-dec1.h"
19 #include "pwc-dec23.h"
20 
21 int pwc_decompress(struct pwc_device *pdev, struct pwc_frame_buf *fbuf)
22 {
23 	int n, line, col;
24 	void *yuv, *image;
25 	u16 *src;
26 	u16 *dsty, *dstu, *dstv;
27 
28 	image = vb2_plane_vaddr(&fbuf->vb.vb2_buf, 0);
29 
30 	yuv = fbuf->data + pdev->frame_header_size;  /* Skip header */
31 
32 	/* Raw format; that's easy... */
33 	if (pdev->pixfmt != V4L2_PIX_FMT_YUV420)
34 	{
35 		struct pwc_raw_frame *raw_frame = image;
36 		raw_frame->type = cpu_to_le16(pdev->type);
37 		raw_frame->vbandlength = cpu_to_le16(pdev->vbandlength);
38 			/* cmd_buf is always 4 bytes, but sometimes, only the
39 			 * first 3 bytes is filled (Nala case). We can
40 			 * determine this using the type of the webcam */
41 		memcpy(raw_frame->cmd, pdev->cmd_buf, 4);
42 		memcpy(raw_frame+1, yuv, pdev->frame_size);
43 		vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
44 			struct_size(raw_frame, rawframe, pdev->frame_size));
45 		return 0;
46 	}
47 
48 	vb2_set_plane_payload(&fbuf->vb.vb2_buf, 0,
49 			      pdev->width * pdev->height * 3 / 2);
50 
51 	if (pdev->vbandlength == 0) {
52 		/* Uncompressed mode.
53 		 *
54 		 * We do some byte shuffling here to go from the
55 		 * native format to YUV420P.
56 		 */
57 		src = (u16 *)yuv;
58 		n = pdev->width * pdev->height;
59 		dsty = (u16 *)(image);
60 		dstu = (u16 *)(image + n);
61 		dstv = (u16 *)(image + n + n / 4);
62 
63 		for (line = 0; line < pdev->height; line++) {
64 			for (col = 0; col < pdev->width; col += 4) {
65 				*dsty++ = *src++;
66 				*dsty++ = *src++;
67 				if (line & 1)
68 					*dstv++ = *src++;
69 				else
70 					*dstu++ = *src++;
71 			}
72 		}
73 
74 		return 0;
75 	}
76 
77 	/*
78 	 * Compressed;
79 	 * the decompressor routines will write the data in planar format
80 	 * immediately.
81 	 */
82 	if (DEVICE_USE_CODEC1(pdev->type)) {
83 
84 		/* TODO & FIXME */
85 		PWC_ERROR("This chipset is not supported for now\n");
86 		return -ENXIO; /* No such device or address: missing decompressor */
87 
88 	} else {
89 		pwc_dec23_decompress(pdev, yuv, image);
90 	}
91 	return 0;
92 }
93