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