xref: /linux/Documentation/userspace-api/media/drivers/cx2341x-uapi.rst (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
1b3634386SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
2b3634386SMauro Carvalho Chehab
3b3634386SMauro Carvalho ChehabThe cx2341x driver
4b3634386SMauro Carvalho Chehab==================
5b3634386SMauro Carvalho Chehab
6b3634386SMauro Carvalho ChehabNon-compressed file format
7b3634386SMauro Carvalho Chehab--------------------------
8b3634386SMauro Carvalho Chehab
9b3634386SMauro Carvalho ChehabThe cx23416 can produce (and the cx23415 can also read) raw YUV output. The
10*78eee7b5SEzequiel Garciaformat of a YUV frame is 16x16 linear tiled NV12 (V4L2_PIX_FMT_NV12_16L16).
11b3634386SMauro Carvalho Chehab
12b3634386SMauro Carvalho ChehabThe format is YUV 4:2:0 which uses 1 Y byte per pixel and 1 U and V byte per
13b3634386SMauro Carvalho Chehabfour pixels.
14b3634386SMauro Carvalho Chehab
15b3634386SMauro Carvalho ChehabThe data is encoded as two macroblock planes, the first containing the Y
16b3634386SMauro Carvalho Chehabvalues, the second containing UV macroblocks.
17b3634386SMauro Carvalho Chehab
18b3634386SMauro Carvalho ChehabThe Y plane is divided into blocks of 16x16 pixels from left to right
19b3634386SMauro Carvalho Chehaband from top to bottom. Each block is transmitted in turn, line-by-line.
20b3634386SMauro Carvalho Chehab
21b3634386SMauro Carvalho ChehabSo the first 16 bytes are the first line of the top-left block, the
22b3634386SMauro Carvalho Chehabsecond 16 bytes are the second line of the top-left block, etc. After
23b3634386SMauro Carvalho Chehabtransmitting this block the first line of the block on the right to the
24b3634386SMauro Carvalho Chehabfirst block is transmitted, etc.
25b3634386SMauro Carvalho Chehab
26b3634386SMauro Carvalho ChehabThe UV plane is divided into blocks of 16x8 UV values going from left
27b3634386SMauro Carvalho Chehabto right, top to bottom. Each block is transmitted in turn, line-by-line.
28b3634386SMauro Carvalho Chehab
29b3634386SMauro Carvalho ChehabSo the first 16 bytes are the first line of the top-left block and
30b3634386SMauro Carvalho Chehabcontain 8 UV value pairs (16 bytes in total). The second 16 bytes are the
31b3634386SMauro Carvalho Chehabsecond line of 8 UV pairs of the top-left block, etc. After transmitting
32b3634386SMauro Carvalho Chehabthis block the first line of the block on the right to the first block is
33b3634386SMauro Carvalho Chehabtransmitted, etc.
34b3634386SMauro Carvalho Chehab
35*78eee7b5SEzequiel GarciaThe code below is given as an example on how to convert V4L2_PIX_FMT_NV12_16L16
36*78eee7b5SEzequiel Garciato separate Y, U and V planes. This code assumes frames of 720x576 (PAL) pixels.
37b3634386SMauro Carvalho Chehab
38b3634386SMauro Carvalho ChehabThe width of a frame is always 720 pixels, regardless of the actual specified
39b3634386SMauro Carvalho Chehabwidth.
40b3634386SMauro Carvalho Chehab
41b3634386SMauro Carvalho ChehabIf the height is not a multiple of 32 lines, then the captured video is
42b3634386SMauro Carvalho Chehabmissing macroblocks at the end and is unusable. So the height must be a
43b3634386SMauro Carvalho Chehabmultiple of 32.
44b3634386SMauro Carvalho Chehab
45b3634386SMauro Carvalho ChehabRaw format c example
46b3634386SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~
47b3634386SMauro Carvalho Chehab
48b3634386SMauro Carvalho Chehab.. code-block:: c
49b3634386SMauro Carvalho Chehab
50b3634386SMauro Carvalho Chehab	#include <stdio.h>
51b3634386SMauro Carvalho Chehab	#include <stdlib.h>
52b3634386SMauro Carvalho Chehab	#include <string.h>
53b3634386SMauro Carvalho Chehab
54b3634386SMauro Carvalho Chehab	static unsigned char frame[576*720*3/2];
55b3634386SMauro Carvalho Chehab	static unsigned char framey[576*720];
56b3634386SMauro Carvalho Chehab	static unsigned char frameu[576*720 / 4];
57b3634386SMauro Carvalho Chehab	static unsigned char framev[576*720 / 4];
58b3634386SMauro Carvalho Chehab
59b3634386SMauro Carvalho Chehab	static void de_macro_y(unsigned char* dst, unsigned char *src, int dstride, int w, int h)
60b3634386SMauro Carvalho Chehab	{
61b3634386SMauro Carvalho Chehab	unsigned int y, x, i;
62b3634386SMauro Carvalho Chehab
63b3634386SMauro Carvalho Chehab	// descramble Y plane
64b3634386SMauro Carvalho Chehab	// dstride = 720 = w
65b3634386SMauro Carvalho Chehab	// The Y plane is divided into blocks of 16x16 pixels
66b3634386SMauro Carvalho Chehab	// Each block in transmitted in turn, line-by-line.
67b3634386SMauro Carvalho Chehab	for (y = 0; y < h; y += 16) {
68b3634386SMauro Carvalho Chehab		for (x = 0; x < w; x += 16) {
69b3634386SMauro Carvalho Chehab		for (i = 0; i < 16; i++) {
70b3634386SMauro Carvalho Chehab			memcpy(dst + x + (y + i) * dstride, src, 16);
71b3634386SMauro Carvalho Chehab			src += 16;
72b3634386SMauro Carvalho Chehab		}
73b3634386SMauro Carvalho Chehab		}
74b3634386SMauro Carvalho Chehab	}
75b3634386SMauro Carvalho Chehab	}
76b3634386SMauro Carvalho Chehab
77b3634386SMauro Carvalho Chehab	static void de_macro_uv(unsigned char *dstu, unsigned char *dstv, unsigned char *src, int dstride, int w, int h)
78b3634386SMauro Carvalho Chehab	{
79b3634386SMauro Carvalho Chehab	unsigned int y, x, i;
80b3634386SMauro Carvalho Chehab
81b3634386SMauro Carvalho Chehab	// descramble U/V plane
82b3634386SMauro Carvalho Chehab	// dstride = 720 / 2 = w
83b3634386SMauro Carvalho Chehab	// The U/V values are interlaced (UVUV...).
84b3634386SMauro Carvalho Chehab	// Again, the UV plane is divided into blocks of 16x16 UV values.
85b3634386SMauro Carvalho Chehab	// Each block in transmitted in turn, line-by-line.
86b3634386SMauro Carvalho Chehab	for (y = 0; y < h; y += 16) {
87b3634386SMauro Carvalho Chehab		for (x = 0; x < w; x += 8) {
88b3634386SMauro Carvalho Chehab		for (i = 0; i < 16; i++) {
89b3634386SMauro Carvalho Chehab			int idx = x + (y + i) * dstride;
90b3634386SMauro Carvalho Chehab
91b3634386SMauro Carvalho Chehab			dstu[idx+0] = src[0];  dstv[idx+0] = src[1];
92b3634386SMauro Carvalho Chehab			dstu[idx+1] = src[2];  dstv[idx+1] = src[3];
93b3634386SMauro Carvalho Chehab			dstu[idx+2] = src[4];  dstv[idx+2] = src[5];
94b3634386SMauro Carvalho Chehab			dstu[idx+3] = src[6];  dstv[idx+3] = src[7];
95b3634386SMauro Carvalho Chehab			dstu[idx+4] = src[8];  dstv[idx+4] = src[9];
96b3634386SMauro Carvalho Chehab			dstu[idx+5] = src[10]; dstv[idx+5] = src[11];
97b3634386SMauro Carvalho Chehab			dstu[idx+6] = src[12]; dstv[idx+6] = src[13];
98b3634386SMauro Carvalho Chehab			dstu[idx+7] = src[14]; dstv[idx+7] = src[15];
99b3634386SMauro Carvalho Chehab			src += 16;
100b3634386SMauro Carvalho Chehab		}
101b3634386SMauro Carvalho Chehab		}
102b3634386SMauro Carvalho Chehab	}
103b3634386SMauro Carvalho Chehab	}
104b3634386SMauro Carvalho Chehab
105b3634386SMauro Carvalho Chehab	/*************************************************************************/
106b3634386SMauro Carvalho Chehab	int main(int argc, char **argv)
107b3634386SMauro Carvalho Chehab	{
108b3634386SMauro Carvalho Chehab	FILE *fin;
109b3634386SMauro Carvalho Chehab	int i;
110b3634386SMauro Carvalho Chehab
111b3634386SMauro Carvalho Chehab	if (argc == 1) fin = stdin;
112b3634386SMauro Carvalho Chehab	else fin = fopen(argv[1], "r");
113b3634386SMauro Carvalho Chehab
114b3634386SMauro Carvalho Chehab	if (fin == NULL) {
115b3634386SMauro Carvalho Chehab		fprintf(stderr, "cannot open input\n");
116b3634386SMauro Carvalho Chehab		exit(-1);
117b3634386SMauro Carvalho Chehab	}
118b3634386SMauro Carvalho Chehab	while (fread(frame, sizeof(frame), 1, fin) == 1) {
119b3634386SMauro Carvalho Chehab		de_macro_y(framey, frame, 720, 720, 576);
120b3634386SMauro Carvalho Chehab		de_macro_uv(frameu, framev, frame + 720 * 576, 720 / 2, 720 / 2, 576 / 2);
121b3634386SMauro Carvalho Chehab		fwrite(framey, sizeof(framey), 1, stdout);
122b3634386SMauro Carvalho Chehab		fwrite(framev, sizeof(framev), 1, stdout);
123b3634386SMauro Carvalho Chehab		fwrite(frameu, sizeof(frameu), 1, stdout);
124b3634386SMauro Carvalho Chehab	}
125b3634386SMauro Carvalho Chehab	fclose(fin);
126b3634386SMauro Carvalho Chehab	return 0;
127b3634386SMauro Carvalho Chehab	}
128b3634386SMauro Carvalho Chehab
129b3634386SMauro Carvalho Chehab
130b3634386SMauro Carvalho ChehabFormat of embedded V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI data
131b3634386SMauro Carvalho Chehab---------------------------------------------------------
132b3634386SMauro Carvalho Chehab
133b3634386SMauro Carvalho ChehabAuthor: Hans Verkuil <hverkuil@xs4all.nl>
134b3634386SMauro Carvalho Chehab
135b3634386SMauro Carvalho Chehab
136b3634386SMauro Carvalho ChehabThis section describes the V4L2_MPEG_STREAM_VBI_FMT_IVTV format of the VBI data
137b3634386SMauro Carvalho Chehabembedded in an MPEG-2 program stream. This format is in part dictated by some
138b3634386SMauro Carvalho Chehabhardware limitations of the ivtv driver (the driver for the Conexant cx23415/6
139b3634386SMauro Carvalho Chehabchips), in particular a maximum size for the VBI data. Anything longer is cut
140b3634386SMauro Carvalho Chehaboff when the MPEG stream is played back through the cx23415.
141b3634386SMauro Carvalho Chehab
142b3634386SMauro Carvalho ChehabThe advantage of this format is it is very compact and that all VBI data for
143b3634386SMauro Carvalho Chehaball lines can be stored while still fitting within the maximum allowed size.
144b3634386SMauro Carvalho Chehab
145b3634386SMauro Carvalho ChehabThe stream ID of the VBI data is 0xBD. The maximum size of the embedded data is
146b3634386SMauro Carvalho Chehab4 + 43 * 36, which is 4 bytes for a header and 2 * 18 VBI lines with a 1 byte
147b3634386SMauro Carvalho Chehabheader and a 42 bytes payload each. Anything beyond this limit is cut off by
148b3634386SMauro Carvalho Chehabthe cx23415/6 firmware. Besides the data for the VBI lines we also need 36 bits
149b3634386SMauro Carvalho Chehabfor a bitmask determining which lines are captured and 4 bytes for a magic cookie,
150b3634386SMauro Carvalho Chehabsignifying that this data package contains V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI data.
151b3634386SMauro Carvalho ChehabIf all lines are used, then there is no longer room for the bitmask. To solve this
152b3634386SMauro Carvalho Chehabtwo different magic numbers were introduced:
153b3634386SMauro Carvalho Chehab
154b3634386SMauro Carvalho Chehab'itv0': After this magic number two unsigned longs follow. Bits 0-17 of the first
155b3634386SMauro Carvalho Chehabunsigned long denote which lines of the first field are captured. Bits 18-31 of
156b3634386SMauro Carvalho Chehabthe first unsigned long and bits 0-3 of the second unsigned long are used for the
157b3634386SMauro Carvalho Chehabsecond field.
158b3634386SMauro Carvalho Chehab
159b3634386SMauro Carvalho Chehab'ITV0': This magic number assumes all VBI lines are captured, i.e. it implicitly
160b3634386SMauro Carvalho Chehabimplies that the bitmasks are 0xffffffff and 0xf.
161b3634386SMauro Carvalho Chehab
162b3634386SMauro Carvalho ChehabAfter these magic cookies (and the 8 byte bitmask in case of cookie 'itv0') the
163b3634386SMauro Carvalho Chehabcaptured VBI lines start:
164b3634386SMauro Carvalho Chehab
165b3634386SMauro Carvalho ChehabFor each line the least significant 4 bits of the first byte contain the data type.
166b3634386SMauro Carvalho ChehabPossible values are shown in the table below. The payload is in the following 42
167b3634386SMauro Carvalho Chehabbytes.
168b3634386SMauro Carvalho Chehab
169b3634386SMauro Carvalho ChehabHere is the list of possible data types:
170b3634386SMauro Carvalho Chehab
171b3634386SMauro Carvalho Chehab.. code-block:: c
172b3634386SMauro Carvalho Chehab
173b3634386SMauro Carvalho Chehab	#define IVTV_SLICED_TYPE_TELETEXT       0x1     // Teletext (uses lines 6-22 for PAL)
174b3634386SMauro Carvalho Chehab	#define IVTV_SLICED_TYPE_CC             0x4     // Closed Captions (line 21 NTSC)
175b3634386SMauro Carvalho Chehab	#define IVTV_SLICED_TYPE_WSS            0x5     // Wide Screen Signal (line 23 PAL)
176b3634386SMauro Carvalho Chehab	#define IVTV_SLICED_TYPE_VPS            0x7     // Video Programming System (PAL) (line 16)
177b3634386SMauro Carvalho Chehab
178