With all the hoopla surrounding which web browsers will support HTML5 video (and which codecs they will support), a buddy of mine mused about decoding video in JavaScript. I mean with a canvas tag, people have been able to produce some amazing things. And these browser JavaScript engines just keep getting faster and faster. So isn't it remotely possible that some level of video decoding (of certain video formats) in JavaScript would be usable? And it's pretty cool when one begins to think of the different possibilities. I admit that most of the possibilities I'm thinking of might be used for some sort of pseudo-DRM. But I could also imagine some pretty nifty uses for it too. There are, if I remember correctly, some sites out there that allow HTTP download of torrents. What if there was a way to preview a video before they it was completely downloaded? What if the first X pieces of a file were downloaded and available for preview before the torrent was complete? That way, a user would be able to determine if they were retrieving what they really wanted.
I could even imagine subscription videos. For instance, using a JavaScript-based decoder in conjunction with chunk/byte-range XHR requests, a server/service could limit you to the amount video you're allowed to watch. For instance, you would pay from X GB or MB of video time and the server just rings that up to your account every time you watch a chunk. So you only pay for what you actually watch -- not the whole video. And service could also implement some sort of pseudo-DRM. With enough talent, one could create one's own encoding scheme and use rotating time keys or something for "DRM". Of course, you hand over your decoding code when the user hits your site - but minification of JavaScript has to be the most (un)intentionally effective obfuscation I've ever seen.
But anyways, I'm not saying these things don't exist or are not already possible -- just that a completely ubiquitous approach to it is becoming more and more feasible. The only big piece missing from the pie, so to speak, is a way to write to an audio buffer. And the developers of FireFox are working on that. Hopefully, their API becomes standard much the way Apple's canvas has become a standard (except for IE). As a proof of concept, I've implemented a pseudo-H261 decoder in JavaScript. It's not entirely...uhmmm...accurate...but it works for what I wanted to demonstrate. It can decode an intra frame. No motion compensation routines etc. were implemented for inter frames/macroblocks -- it just decodes one picture. When I figure out (from the spec) how motion compensation should work -- I'll have it decode multiple frames. On my laptop it can decode a CIF intra frame in about 25 ms and render it in under 100ms -- in Chrome. FireFox is a little slower -- around 68ms and 120ms respectively. The times are a little slow for full motion video (I think), but this doesn't account for multiple opportunities for optimization. There are many inefficiencies in my code that could drop the decoding and (especially) render time -- probably by a significant margin.
(note: I've only tested in Chrome and FireFox. IE will not work. Safari is an unknown but it should work.)
You can check out the decoder here. You can download the files here.