With still no common video format for HTML5 in sight, I decided to implement an MPEG1 decoder in JavaScript. I know there's already an h264 decoder for JavaScript around, but it's huge, compiled with emscripten and quite complicated.
An MPEG1 decoder sounded like a relatively simple and fun weekend project. While the real world use cases for this are of course a bit limited, I still learned a whole lot about video codecs in the process. The size of the source is just around 15kb gzipped and the performance is quite okay-ish - a 320x240 video easily plays with 30fps on the iPhone5.
Mind Blown - 1.8mb MPEG1, 18 seconds, 570 frames. Click to pause.
For a longer demo see the first few minutes of Big Buck Bunny.
Of course this project still has numerous limitations. First and foremost: no streaming. The video file has to be completely loaded before it can be played back. I'm still waiting for chunked, binary XHR support to arrive in browsers to fix this. Till then, jsmpeg only shows a simple loading animation.
The decoder itself also has some bugs. It currently struggles with packetized MPEG files and stumbles over the packet headers that are randomly thrown in. So in order to play correctly, the MPEG file has to be a raw MPEG1 video stream. Also, B-Frames (those that rely on past and future frames for decoding) are not reordered, so video containing B-Frames will look stuttery. However, I found that most encoders tend to not use B-Frames anyway.
What I learned with this all, is that the MPEG1 file format is a truly ugly one. While I may not understand all the limitations present when it was conceived, I still believe that some parts of it are inane at best, or vicious at worst. Nothing in MPEG1 streams besides the "start codes" is byte aligned, so you're dealing with a raw bitstream.
The width and height for instance is encoded in 12 bits each. The framerate is encoded in 4 bits, but not as a number of frames per second or millisecond delay, but as an index into a pre-defined array of possible frame rates. Want to have a 10fps or 48fps video in MPEG1? Not possible. A 2byte field encoding the real frame rate, not an index, would have allowed far more flexibility - and it only has to be transmitted once anyway.
The MPEG1 format is full of such short sighted decisions that make it quite hard to parse. But then again, it was the first truly usable video format available and I'm not sure if anyone working on it anticipated the success it would have.
The source is available at github.com/phoboslab/jsmpeg.
“He must have hit himself trying to get out,” Larry surmised. “I remember that, too,” Larry said. Jeff chuckled rather sourly. “It’s funny,” Jeff remarked. “This-here psychology I’ve read about ain’t so far wrong when it says that folks who gets the wrong slant on a thing comes to believe it so strong that even the truth looks like a fib to them.” She could hear voices confusedly, men hurriedly calling and hallooing as she neared the back of the officers' line and crept into her tent. The door was barely closed when there came a knock, and the voice of the striker asking if she had heard the shot across the river. The blaze of glory had gone suddenly from the clouds, leaving them lifeless gray, when she turned her eyes back to them; and the outlook across the parade ground was very bare. She went and stood by the fire, leaning her arm on the mantel-shelf and setting her determined lips. Just then the whistle blew for a stop. This was enough excuse for Shorty. He fell back until the General was around the corner, out of sight, and then went up to Billings. There was a silence. "W?an't you be never coming here no more?" whispered Bessie in the next pause. His farm itself was at length forfeited, and Holgrave took shelter for the moment at old Hartwell's. The hut his father had reared when he married his mother, was still standing; the roof had fallen in, the ivy had grown over its walls; but even yet it sometimes sheltered the wandering mendicant, and often would the blaze of a large wood fire look cheerily through the shattered casement and the broken door, and shed an air almost of comfort over the bare walls. Holgrave remembered the ruin, as he was considering where he could abide until Margaret, who was far advanced in the family way, should be enabled to travel farther. His resolution was instantly formed; and refusing the assistance offered by Hartwell, and some other neighbours, and as decidedly rejecting the idea they proposed, of striving to regain possession of his house, he requested Lucy Hartwell to look to Margaret for a day or two, while he sought out a place to shelter them; and then, without mentioning his purpose, quitted the house. HoME日屄的能力
ENTER NUMBET 0017 sishe.net.cn www.tinyq.com.cn funa0.com.cn www.vilant.com.cn www.kedu.org.cn www.xl6666.com.cn www.jlfhol.org.cn www.ganbi0.com.cn www.huaqu7.net.cn 9131ych.com.cn