I'm trying to convert a YCbCr-file from 8 bpp to 10 bpp.
My best approach so far is still order of magnitude slower than the most basic naive C implementation.
Naive approach in C, runs in about 8s. Making the code to work on chunks instead, drops the time to well under 1s.
I'm curious about what kind of performance it's possible to get from standard python dealing with binary files. Example file is in CIF-resolution and is "small" in comparison to content in 1080p. Feel free to add numpy-suggestions as well although I'm mainly interested in standard python.
The test-file can be downloaded from
http://trace.eas.asu.edu/yuv/foreman/foreman_cif.7z
sha1sum
for correct 10-bit output is
c511dabc793383f7fd0ed69b4bb9b9f89ef73b84
python:
#!/usr/bin/env python
import array
f_in = 'foreman_cif.yuv'
f_out = 'py_10bpp.yuv'
def bytesfromfile(f):
while True:
raw = array.array('B')
raw.fromstring(f.read(8192))
if not raw:
break
yield raw
with open(f_in, 'rb') as fd_in, \
open(f_out, 'wb') as fd_out:
for byte in bytesfromfile(fd_in):
data = []
for i in byte:
i <<= 2
data.append(i & 0xff)
data.append((i >> 8) & 0xff)
fd_out.write(array.array('B', data).tostring())
Naive C-dito:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
int c;
int d[2];
FILE* fd_in;
FILE* fd_out;
fd_in = fopen("foreman_cif.yuv", "rb");
fd_out = fopen("c_10bpp.yuv", "wb");
while((c = fgetc(fd_in)) != EOF) {
c <<= 2;
d[0] = c & 0xff;
d[1] = (c >> 8) & 0xff;
fwrite(&d[0], 1, 1, fd_out);
fwrite(&d[1], 1, 1, fd_out);
}
fclose(fd_in);
fclose(fd_out);
return EXIT_SUCCESS;
}
-O2
. First did the measurements without the power-cord to my laptop which caused my cores to run @ 800MHz. "order of magnitude" comes from this. with power-cord connected and cores running @2.2GHz I get the numbers above