178 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3The cx2341x driver4==================5 6Non-compressed file format7--------------------------8 9The cx23416 can produce (and the cx23415 can also read) raw YUV output. The10format of a YUV frame is 16x16 linear tiled NV12 (V4L2_PIX_FMT_NV12_16L16).11 12The format is YUV 4:2:0 which uses 1 Y byte per pixel and 1 U and V byte per13four pixels.14 15The data is encoded as two macroblock planes, the first containing the Y16values, the second containing UV macroblocks.17 18The Y plane is divided into blocks of 16x16 pixels from left to right19and from top to bottom. Each block is transmitted in turn, line-by-line.20 21So the first 16 bytes are the first line of the top-left block, the22second 16 bytes are the second line of the top-left block, etc. After23transmitting this block the first line of the block on the right to the24first block is transmitted, etc.25 26The UV plane is divided into blocks of 16x8 UV values going from left27to right, top to bottom. Each block is transmitted in turn, line-by-line.28 29So the first 16 bytes are the first line of the top-left block and30contain 8 UV value pairs (16 bytes in total). The second 16 bytes are the31second line of 8 UV pairs of the top-left block, etc. After transmitting32this block the first line of the block on the right to the first block is33transmitted, etc.34 35The code below is given as an example on how to convert V4L2_PIX_FMT_NV12_16L1636to separate Y, U and V planes. This code assumes frames of 720x576 (PAL) pixels.37 38The width of a frame is always 720 pixels, regardless of the actual specified39width.40 41If the height is not a multiple of 32 lines, then the captured video is42missing macroblocks at the end and is unusable. So the height must be a43multiple of 32.44 45Raw format c example46~~~~~~~~~~~~~~~~~~~~47 48.. code-block:: c49 50 #include <stdio.h>51 #include <stdlib.h>52 #include <string.h>53 54 static unsigned char frame[576*720*3/2];55 static unsigned char framey[576*720];56 static unsigned char frameu[576*720 / 4];57 static unsigned char framev[576*720 / 4];58 59 static void de_macro_y(unsigned char* dst, unsigned char *src, int dstride, int w, int h)60 {61 unsigned int y, x, i;62 63 // descramble Y plane64 // dstride = 720 = w65 // The Y plane is divided into blocks of 16x16 pixels66 // Each block in transmitted in turn, line-by-line.67 for (y = 0; y < h; y += 16) {68 for (x = 0; x < w; x += 16) {69 for (i = 0; i < 16; i++) {70 memcpy(dst + x + (y + i) * dstride, src, 16);71 src += 16;72 }73 }74 }75 }76 77 static void de_macro_uv(unsigned char *dstu, unsigned char *dstv, unsigned char *src, int dstride, int w, int h)78 {79 unsigned int y, x, i;80 81 // descramble U/V plane82 // dstride = 720 / 2 = w83 // The U/V values are interlaced (UVUV...).84 // Again, the UV plane is divided into blocks of 16x16 UV values.85 // Each block in transmitted in turn, line-by-line.86 for (y = 0; y < h; y += 16) {87 for (x = 0; x < w; x += 8) {88 for (i = 0; i < 16; i++) {89 int idx = x + (y + i) * dstride;90 91 dstu[idx+0] = src[0]; dstv[idx+0] = src[1];92 dstu[idx+1] = src[2]; dstv[idx+1] = src[3];93 dstu[idx+2] = src[4]; dstv[idx+2] = src[5];94 dstu[idx+3] = src[6]; dstv[idx+3] = src[7];95 dstu[idx+4] = src[8]; dstv[idx+4] = src[9];96 dstu[idx+5] = src[10]; dstv[idx+5] = src[11];97 dstu[idx+6] = src[12]; dstv[idx+6] = src[13];98 dstu[idx+7] = src[14]; dstv[idx+7] = src[15];99 src += 16;100 }101 }102 }103 }104 105 /*************************************************************************/106 int main(int argc, char **argv)107 {108 FILE *fin;109 int i;110 111 if (argc == 1) fin = stdin;112 else fin = fopen(argv[1], "r");113 114 if (fin == NULL) {115 fprintf(stderr, "cannot open input\n");116 exit(-1);117 }118 while (fread(frame, sizeof(frame), 1, fin) == 1) {119 de_macro_y(framey, frame, 720, 720, 576);120 de_macro_uv(frameu, framev, frame + 720 * 576, 720 / 2, 720 / 2, 576 / 2);121 fwrite(framey, sizeof(framey), 1, stdout);122 fwrite(framev, sizeof(framev), 1, stdout);123 fwrite(frameu, sizeof(frameu), 1, stdout);124 }125 fclose(fin);126 return 0;127 }128 129 130Format of embedded V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI data131---------------------------------------------------------132 133Author: Hans Verkuil <hverkuil@xs4all.nl>134 135 136This section describes the V4L2_MPEG_STREAM_VBI_FMT_IVTV format of the VBI data137embedded in an MPEG-2 program stream. This format is in part dictated by some138hardware limitations of the ivtv driver (the driver for the Conexant cx23415/6139chips), in particular a maximum size for the VBI data. Anything longer is cut140off when the MPEG stream is played back through the cx23415.141 142The advantage of this format is it is very compact and that all VBI data for143all lines can be stored while still fitting within the maximum allowed size.144 145The stream ID of the VBI data is 0xBD. The maximum size of the embedded data is1464 + 43 * 36, which is 4 bytes for a header and 2 * 18 VBI lines with a 1 byte147header and a 42 bytes payload each. Anything beyond this limit is cut off by148the cx23415/6 firmware. Besides the data for the VBI lines we also need 36 bits149for a bitmask determining which lines are captured and 4 bytes for a magic cookie,150signifying that this data package contains V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI data.151If all lines are used, then there is no longer room for the bitmask. To solve this152two different magic numbers were introduced:153 154'itv0': After this magic number two unsigned longs follow. Bits 0-17 of the first155unsigned long denote which lines of the first field are captured. Bits 18-31 of156the first unsigned long and bits 0-3 of the second unsigned long are used for the157second field.158 159'ITV0': This magic number assumes all VBI lines are captured, i.e. it implicitly160implies that the bitmasks are 0xffffffff and 0xf.161 162After these magic cookies (and the 8 byte bitmask in case of cookie 'itv0') the163captured VBI lines start:164 165For each line the least significant 4 bits of the first byte contain the data type.166Possible values are shown in the table below. The payload is in the following 42167bytes.168 169Here is the list of possible data types:170 171.. code-block:: c172 173 #define IVTV_SLICED_TYPE_TELETEXT 0x1 // Teletext (uses lines 6-22 for PAL)174 #define IVTV_SLICED_TYPE_CC 0x4 // Closed Captions (line 21 NTSC)175 #define IVTV_SLICED_TYPE_WSS 0x5 // Wide Screen Signal (line 23 PAL)176 #define IVTV_SLICED_TYPE_VPS 0x7 // Video Programming System (PAL) (line 16)177 178