Skip to content

Instantly share code, notes, and snippets.

@magcius
Created September 26, 2017 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magcius/6c59de7723aa8833b82d8dd556960492 to your computer and use it in GitHub Desktop.
Save magcius/6c59de7723aa8833b82d8dd556960492 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#define w 1920
#define h 1080
static unsigned char yuv[3133440];
static unsigned char rgb[w*h*4];
__attribute__((unused)) static int clamp(int v) {
if (v < 0) return 0;
if (v > 255) return 255;
return v;
}
static uint64_t offs = 0;
static void read8(unsigned char out[8]) {
/* aml is weird -- every 8 bytes is reversed */
int n = 8;
assert(offs < sizeof(yuv));
while (n--) { out[n] = yuv[offs++]; }
}
#define rgbat(ay, ax) ((w*(ay)+(ax))*4)
int main(int argc, char *argv[]) {
FILE *fd;
fd = fopen(argv[1], "r");
fread(yuv, 1, sizeof(yuv), fd);
fclose(fd);
int y, x, dy, dx, si;
/* do y samples first */
int bs = 32;
for (y = 0; y < h; y += bs) {
for (x = 0; x < w; x += bs) {
for (dy = 0; dy < bs; dy++) {
for (dx = 0; dx < bs; dx += 8) {
unsigned char lp[8];
read8(lp);
for (si = 0; si < 8; si++) {
int px = rgbat(y+dy, x+dx+si);
int y = clamp((lp[si]-12) * 1.16);
rgb[px+0] = y;
rgb[px+1] = y;
rgb[px+2] = y;
rgb[px+3] = 255;
}
}
}
}
}
/* now add in uv samples */
for (y = 0; y < h; y += 64) {
for (x = 0; x < w; x += 32) {
int dx, dy, si;
for (dy = 0; dy < 64; dy += 2) {
for (dx = 0; dx < 32; dx += 8) {
unsigned char lp[8];
read8(lp);
for (si = 0; si < 8;) {
int u = ((lp[si++]-128) * 1.1);
int v = ((lp[si++]-128) * 1.1);
int dr = 1.370705f*v;
int dg = -0.698001f*v - 0.337633f*u;
int db = 1.732446f*u;
#define setpx \
rgb[px+0] = clamp(rgb[px+0] + dr); \
rgb[px+1] = clamp(rgb[px+1] + dg); \
rgb[px+2] = clamp(rgb[px+2] + db);
int px;
px = rgbat(y+dy, x+dx+si);
setpx;
px = rgbat(y+dy+1, x+dx+si);
setpx;
px = rgbat(y+dy, x+dx+si+1);
setpx;
px = rgbat(y+dy+1, x+dx+si+1);
setpx;
}
}
}
}
}
FILE *fp = fopen("out.data", "w");
fwrite(rgb, 1, sizeof(rgb), fp);
fclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment