summaryrefslogtreecommitdiff
path: root/Documentation/video4linux/cx2341x/README.hm12
blob: b36148ea07501bdac37ae74b31cc258150f75a81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
The cx23416 can produce (and the cx23415 can also read) raw YUV output. The
format of a YUV frame is specific to this chip and is called HM12. 'HM' stands
for 'Hauppauge Macroblock', which is a misnomer as 'Conexant Macroblock' would
be more accurate.

The format is YUV 4:2:0 which uses 1 Y byte per pixel and 1 U and V byte per
four pixels.

The data is encoded as two macroblock planes, the first containing the Y
values, the second containing UV macroblocks.

The Y plane is divided into blocks of 16x16 pixels from left to right
and from top to bottom. Each block is transmitted in turn, line-by-line.

So the first 16 bytes are the first line of the top-left block, the
second 16 bytes are the second line of the top-left block, etc. After
transmitting this block the first line of the block on the right to the
first block is transmitted, etc.

The UV plane is divided into blocks of 16x8 UV values going from left
to right, top to bottom. Each block is transmitted in turn, line-by-line.

So the first 16 bytes are the first line of the top-left block and
contain 8 UV value pairs (16 bytes in total). The second 16 bytes are the
second line of 8 UV pairs of the top-left block, etc. After transmitting
this block the first line of the block on the right to the first block is
transmitted, etc.

The code below is given as an example on how to convert HM12 to separate
Y, U and V planes. This code assumes frames of 720x576 (PAL) pixels.

The width of a frame is always 720 pixels, regardless of the actual specified
width.

If the height is not a multiple of 32 lines, then the captured video is
missing macroblocks at the end and is unusable. So the height must be a
multiple of 32.

--------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static unsigned char frame[576*720*3/2];
static unsigned char framey[576*720];
static unsigned char frameu[576*720 / 4];
static unsigned char framev[576*720 / 4];

static void de_macro_y(unsigned char* dst, unsigned char *src, int dstride, int w, int h)
{
    unsigned int y, x, i;

    // descramble Y plane
    // dstride = 720 = w
    // The Y plane is divided into blocks of 16x16 pixels
    // Each block in transmitted in turn, line-by-line.
    for (y = 0; y < h; y += 16) {
	for (x = 0; x < w; x += 16) {
	    for (i = 0; i < 16; i++) {
		memcpy(dst + x + (y + i) * dstride, src, 16);
		src += 16;
	    }
	}
    }
}

static void de_macro_uv(unsigned char *dstu, unsigned char *dstv, unsigned char *src, int dstride, int w, int h)
{
    unsigned int y, x, i;

    // descramble U/V plane
    // dstride = 720 / 2 = w
    // The U/V values are interlaced (UVUV...).
    // Again, the UV plane is divided into blocks of 16x16 UV values.
    // Each block in transmitted in turn, line-by-line.
    for (y = 0; y < h; y += 16) {
	for (x = 0; x < w; x += 8) {
	    for (i = 0; i < 16; i++) {
		int idx = x + (y + i) * dstride;

		dstu[idx+0] = src[0];  dstv[idx+0] = src[1];
		dstu[idx+1] = src[2];  dstv[idx+1] = src[3];
		dstu[idx+2] = src[4];  dstv[idx+2] = src[5];
		dstu[idx+3] = src[6];  dstv[idx+3] = src[7];
		dstu[idx+4] = src[8];  dstv[idx+4] = src[9];
		dstu[idx+5] = src[10]; dstv[idx+5] = src[11];
		dstu[idx+6] = src[12]; dstv[idx+6] = src[13];
		dstu[idx+7] = src[14]; dstv[idx+7] = src[15];
		src += 16;
	    }
	}
    }
}

/*************************************************************************/
int main(int argc, char **argv)
{
    FILE *fin;
    int i;

    if (argc == 1) fin = stdin;
    else fin = fopen(argv[1], "r");

    if (fin == NULL) {
	fprintf(stderr, "cannot open input\n");
	exit(-1);
    }
    while (fread(frame, sizeof(frame), 1, fin) == 1) {
	de_macro_y(framey, frame, 720, 720, 576);
	de_macro_uv(frameu, framev, frame + 720 * 576, 720 / 2, 720 / 2, 576 / 2);
	fwrite(framey, sizeof(framey), 1, stdout);
	fwrite(framev, sizeof(framev), 1, stdout);
	fwrite(frameu, sizeof(frameu), 1, stdout);
    }
    fclose(fin);
    return 0;
}

--------------------------------------------------------------------------