Here’s a little project of absolutely no use to anyone (a specialty of mine, as if you didn’t know): Pure Python classes for writing and reading bitstreams. This was just one of those things where I was sitting around wondering what it would take to accomplish, and a cursory Google search didn’t reveal anything useful (though it’s probably out there, in all likelihood), so I sat down and pounded out the code.
To what end? Oh, I don’t know– reimplement FFmpeg in Python; go crazy. Behold brute force bit banging in Python:
Then there is the small matter of testing this slop. At first, I was creating some simple cases, easily verifiable via visual inspection. Then I remembered that I’m a fan of making computers do heavy and thorough testing on my behalf. With that, here’s the testing code:
Writing and reading 100,000 random bit combinations seems to turn out okay, even though it feels like an eternity. I didn’t say I was aiming for the fastest, just any Python solution.
Probably copying the FFmpeg bitstream stuff more closely (in particular reading/writing 4 bytes at once) probably would make the speed more acceptable. Especially the bitstream writer looks like it might involve one realloc per byte written :-)
Off the top of my head, Hachoir is a Python app for dissecting binary data down to bit level. Might give some inspiration for own designs.