cx2341x-uapi.rst 6.2 KB

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