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