Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 The cx2341x driver
0004 ==================
0005 
0006 Non-compressed file format
0007 --------------------------
0008 
0009 The cx23416 can produce (and the cx23415 can also read) raw YUV output. The
0010 format of a YUV frame is 16x16 linear tiled NV12 (V4L2_PIX_FMT_NV12_16L16).
0011 
0012 The format is YUV 4:2:0 which uses 1 Y byte per pixel and 1 U and V byte per
0013 four pixels.
0014 
0015 The data is encoded as two macroblock planes, the first containing the Y
0016 values, the second containing UV macroblocks.
0017 
0018 The Y plane is divided into blocks of 16x16 pixels from left to right
0019 and from top to bottom. Each block is transmitted in turn, line-by-line.
0020 
0021 So the first 16 bytes are the first line of the top-left block, the
0022 second 16 bytes are the second line of the top-left block, etc. After
0023 transmitting this block the first line of the block on the right to the
0024 first block is transmitted, etc.
0025 
0026 The UV plane is divided into blocks of 16x8 UV values going from left
0027 to right, top to bottom. Each block is transmitted in turn, line-by-line.
0028 
0029 So the first 16 bytes are the first line of the top-left block and
0030 contain 8 UV value pairs (16 bytes in total). The second 16 bytes are the
0031 second line of 8 UV pairs of the top-left block, etc. After transmitting
0032 this block the first line of the block on the right to the first block is
0033 transmitted, etc.
0034 
0035 The code below is given as an example on how to convert V4L2_PIX_FMT_NV12_16L16
0036 to separate Y, U and V planes. This code assumes frames of 720x576 (PAL) pixels.
0037 
0038 The width of a frame is always 720 pixels, regardless of the actual specified
0039 width.
0040 
0041 If the height is not a multiple of 32 lines, then the captured video is
0042 missing macroblocks at the end and is unusable. So the height must be a
0043 multiple of 32.
0044 
0045 Raw format c example
0046 ~~~~~~~~~~~~~~~~~~~~
0047 
0048 .. code-block:: c
0049 
0050         #include <stdio.h>
0051         #include <stdlib.h>
0052         #include <string.h>
0053 
0054         static unsigned char frame[576*720*3/2];
0055         static unsigned char framey[576*720];
0056         static unsigned char frameu[576*720 / 4];
0057         static unsigned char framev[576*720 / 4];
0058 
0059         static void de_macro_y(unsigned char* dst, unsigned char *src, int dstride, int w, int h)
0060         {
0061         unsigned int y, x, i;
0062 
0063         // descramble Y plane
0064         // dstride = 720 = w
0065         // The Y plane is divided into blocks of 16x16 pixels
0066         // Each block in transmitted in turn, line-by-line.
0067         for (y = 0; y < h; y += 16) {
0068                 for (x = 0; x < w; x += 16) {
0069                 for (i = 0; i < 16; i++) {
0070                         memcpy(dst + x + (y + i) * dstride, src, 16);
0071                         src += 16;
0072                 }
0073                 }
0074         }
0075         }
0076 
0077         static void de_macro_uv(unsigned char *dstu, unsigned char *dstv, unsigned char *src, int dstride, int w, int h)
0078         {
0079         unsigned int y, x, i;
0080 
0081         // descramble U/V plane
0082         // dstride = 720 / 2 = w
0083         // The U/V values are interlaced (UVUV...).
0084         // Again, the UV plane is divided into blocks of 16x16 UV values.
0085         // Each block in transmitted in turn, line-by-line.
0086         for (y = 0; y < h; y += 16) {
0087                 for (x = 0; x < w; x += 8) {
0088                 for (i = 0; i < 16; i++) {
0089                         int idx = x + (y + i) * dstride;
0090 
0091                         dstu[idx+0] = src[0];  dstv[idx+0] = src[1];
0092                         dstu[idx+1] = src[2];  dstv[idx+1] = src[3];
0093                         dstu[idx+2] = src[4];  dstv[idx+2] = src[5];
0094                         dstu[idx+3] = src[6];  dstv[idx+3] = src[7];
0095                         dstu[idx+4] = src[8];  dstv[idx+4] = src[9];
0096                         dstu[idx+5] = src[10]; dstv[idx+5] = src[11];
0097                         dstu[idx+6] = src[12]; dstv[idx+6] = src[13];
0098                         dstu[idx+7] = src[14]; dstv[idx+7] = src[15];
0099                         src += 16;
0100                 }
0101                 }
0102         }
0103         }
0104 
0105         /*************************************************************************/
0106         int main(int argc, char **argv)
0107         {
0108         FILE *fin;
0109         int i;
0110 
0111         if (argc == 1) fin = stdin;
0112         else fin = fopen(argv[1], "r");
0113 
0114         if (fin == NULL) {
0115                 fprintf(stderr, "cannot open input\n");
0116                 exit(-1);
0117         }
0118         while (fread(frame, sizeof(frame), 1, fin) == 1) {
0119                 de_macro_y(framey, frame, 720, 720, 576);
0120                 de_macro_uv(frameu, framev, frame + 720 * 576, 720 / 2, 720 / 2, 576 / 2);
0121                 fwrite(framey, sizeof(framey), 1, stdout);
0122                 fwrite(framev, sizeof(framev), 1, stdout);
0123                 fwrite(frameu, sizeof(frameu), 1, stdout);
0124         }
0125         fclose(fin);
0126         return 0;
0127         }
0128 
0129 
0130 Format of embedded V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI data
0131 ---------------------------------------------------------
0132 
0133 Author: Hans Verkuil <hverkuil@xs4all.nl>
0134 
0135 
0136 This section describes the V4L2_MPEG_STREAM_VBI_FMT_IVTV format of the VBI data
0137 embedded in an MPEG-2 program stream. This format is in part dictated by some
0138 hardware limitations of the ivtv driver (the driver for the Conexant cx23415/6
0139 chips), in particular a maximum size for the VBI data. Anything longer is cut
0140 off when the MPEG stream is played back through the cx23415.
0141 
0142 The advantage of this format is it is very compact and that all VBI data for
0143 all lines can be stored while still fitting within the maximum allowed size.
0144 
0145 The stream ID of the VBI data is 0xBD. The maximum size of the embedded data is
0146 4 + 43 * 36, which is 4 bytes for a header and 2 * 18 VBI lines with a 1 byte
0147 header and a 42 bytes payload each. Anything beyond this limit is cut off by
0148 the cx23415/6 firmware. Besides the data for the VBI lines we also need 36 bits
0149 for a bitmask determining which lines are captured and 4 bytes for a magic cookie,
0150 signifying that this data package contains V4L2_MPEG_STREAM_VBI_FMT_IVTV VBI data.
0151 If all lines are used, then there is no longer room for the bitmask. To solve this
0152 two different magic numbers were introduced:
0153 
0154 'itv0': After this magic number two unsigned longs follow. Bits 0-17 of the first
0155 unsigned long denote which lines of the first field are captured. Bits 18-31 of
0156 the first unsigned long and bits 0-3 of the second unsigned long are used for the
0157 second field.
0158 
0159 'ITV0': This magic number assumes all VBI lines are captured, i.e. it implicitly
0160 implies that the bitmasks are 0xffffffff and 0xf.
0161 
0162 After these magic cookies (and the 8 byte bitmask in case of cookie 'itv0') the
0163 captured VBI lines start:
0164 
0165 For each line the least significant 4 bits of the first byte contain the data type.
0166 Possible values are shown in the table below. The payload is in the following 42
0167 bytes.
0168 
0169 Here is the list of possible data types:
0170 
0171 .. code-block:: c
0172 
0173         #define IVTV_SLICED_TYPE_TELETEXT       0x1     // Teletext (uses lines 6-22 for PAL)
0174         #define IVTV_SLICED_TYPE_CC             0x4     // Closed Captions (line 21 NTSC)
0175         #define IVTV_SLICED_TYPE_WSS            0x5     // Wide Screen Signal (line 23 PAL)
0176         #define IVTV_SLICED_TYPE_VPS            0x7     // Video Programming System (PAL) (line 16)
0177