## ~ft/hj264

hj264/yuv.c -rw-r--r-- 806 bytes
70b1cf8aSigrid Solveig Haflínudóttir give up on weird shit 3 months ago
`                                                                                `
```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```
```#include <u.h>
#include <libc.h>
#include "yuv.h"

#define Y ((( 77*r + 150*g +  29*b + 128) >> 8) +   0)
#define U (((-43*r -  84*g + 127*b + 128) >> 8) + 128)
#define V (((127*r - 106*g -  21*b + 128) >> 8) + 128)

void
xrgb2yuv420(u8int *bgrx, int w, int h, YUV *yuv)
{
u8int *py, *pu, *pv;
int x, y, r, g, b;

py = yuv->y;
pu = yuv->u;
pv = yuv->v;

for(y = 0; y < h;){
for(x = 0; x < w;){
b = bgrx[0];
g = bgrx[1];
r = bgrx[2];
bgrx += 4;

py[x] = Y;
pu[x/2] = U;
pv[x/2] = V;
x++;

b = bgrx[0];
g = bgrx[1];
r = bgrx[2];
bgrx += 4;
py[x] = Y;
x++;
}
py += yuv->ys;
y++;

for(x = 0; x < w;){
b = bgrx[0];
g = bgrx[1];
r = bgrx[2];
bgrx += 4;
py[x] = Y;
x++;
}
py += yuv->ys;
pu += yuv->us;
pv += yuv->vs;
y++;
}
}
```