Thoughts of a multimedia madman

Friday, July 21, 2006

Why Theora and Dirac matter

If you’re a small multimedia developer who uses video in your product then you’ve probably been through the dilemma of trying to pick a suitable video codec for your project. For those who haven’t if you think this is an easy process and that some flavour of MPEG4 such as DivX, xvid, etc is the ideal choice then you’d be very much mistaken. Video codecs are a patent and licensing nightmare which makes choosing one both a legal minefield and very costly. Consider MPEG4 to license the codec at raw patent licensing costs is $0.25 per decoder and there are fees for the encoded footage as well, MPEG2 is oddly far more expensive at $2.50. So if you thought the margins were slim on those £20 DVD players consider that about 7% of the cost goes just on licensing! As I said those figures I quoted are the raw licensing costs, unless you plan on writing your own decoder or can find something you can use for free you will also have to license a 3rd party decoder to distribute with your software. All these costs add up and unless you make a large margin on each sale of your product they it may be uneconomic to license a video codec.

So what are the free options? One of the most widely used codecs is MPEG1 as Windows has been supplied with an MPEG1 decoder since the Windows 95 days. Unfortunately MPEG1 is being to show its age and for high action scenes the video can become very blocky also the colours tend to be washed out. The other free option is Windows Media (WMV) but the quality of the video isn’t very high and the decoding complexity can push your application’s processor requirements up quite significantly. On2 released a free version of their now quite old VP3 codec but again like WMV the decoding complexity is high. Now even if you are in a position to license MPEG4 there are now two different patent pools neither of which contain each others patents in full and in addition there may be companies who have chosen not to put their patents into either pool and may demand you license separately. You could license one pool or even both and still be at risk from potential legal challenges from submarine patents.

So what choices does a small developer have? Are there any other free alternatives that can be used? There are two alternative choices but neither are ready for commercial deployment yet. Theora and Dirac are the two main options and come from different perspectives. Theora is based on VP3 which On2 donated to the Open Source community with an irrevocable free license for use. Dirac on the other hand is an experimental Open Source codec being developed by the BBC to be powerful, royalty free and has been developed to avoid existing patents which aren’t owned by the BBC. So lets take a look at these in detail.

Currently Theora is in the alpha stage, as of 20th June alpha7 was released with the bitstream being frozen since alpha3 so all videos encoded since then should still be playable on the current releases. Theora while based on VP3 aims to replace some of the fixed internal parameters with a more flexible encoding model so that the codec could be more easily tweaked for better video performance. Development of Theora is complemented by a series of related projects under the same banner, there are some audio codecs most noticeably Vorbis as well as the Ogg container format which allows for a completely free and open video system to be used. There is also a project for implementing the various components as a series of DirectShow filters thus making implementing Theora relatively straightforward. The big issues with Theora are that the development has been painfully slow, On2 made the original sources to the codec available back in September 2001 and yet nearly 5 years later we still haven’t even reached beta yet. My personal evaluation of the codec and DirectShow filters threw up a few problems that meant I couldn’t use the codec in a commercial product.

Dirac was made public back in March 2004 and has since progressed to the point where a reference implementation of the codec is almost complete however the reference implementation is very CPU intensive to encode and decode with decoding speeds that are less than real-time on the fastest machines I have access to. There were also visual issues with some video footage such as quite severe blocking artefacts. Until the reference codec is complete and some heavy optimisation is applied Dirac is nowhere near ready for commercial deployment. It also suffers from the fact it has very few tools to help encode the video which makes the process of generating video non-trivial. My personal opinion is it will probably be at least at year before this codec could potentially be consider for commercial use.

Overall I believe Theora and Dirac are definitely a step in the right direction of software producers who need a royalty free video codec however there could be a long wait before anyone could use either of these codecs in real products. The process could be speeded up with some assistance and I would encourage you to at least try and use these codecs and report any bugs or issues you find to the developers. The more people who test the code the and report the problems the quicker it can reach a stable 1.0 release, after all a bit of QA is the least you can give in return for a free video codec.

Tags:


Wednesday, July 12, 2006

Open source code quality

As part of my job I quite often work with OpenSource components in Delphi as they are invaluable time savers for getting my work done within a reasonable timeframe. There are many great projects such as the JEDI JVCL and JCL, Graphics32, DSpack and PicShow. These components work great most of the time however as with any code from time to time a problem will be discovered and some maintenance required. The problem comes whenever you dive into the code to take a look at what you can do to fix the problems you are almost always faced with poorly written, badly formatted and generally uncommented code. I appreciate that I didn’t have to write the components in the first place but at the same time so many of these components aren’t great examples of good coding practice yet hundreds of developers will rely on them. I take particularly issue with OpenSource projects because I’ve seen so few if any that I would say had a good coding style.

As a programmer I find the near universal lack of commenting very worrying as I personally think that commenting code is almost as important as writing it, for me comments represent writing what I think the code is or should be doing at the time I wrote it versus what I might later find out it isn’t doing. It helps to align my thinking with my execution. You learn about the importance of commenting every time you revisit a non-trivial piece of code after it’s faded from active memory.

The coding style is quite important for readability, take a look at this snippet of some code I’m looking at right now…


if assigned(FFilters) then
if FFilters.Count > 0 then
for i := 0 to FFilters.count - 1 do
FilterList.Remove(IFilter(FFilters.Items[i]).GetFilter);

The above is perfectly acceptable compiling code but by leaving out the begins and the ends (Delphi’s curly brackets for all you C coders) in the nested code isn’t recommended for readability nor for the risk of introducing errors. Also it could be argued that the 2nd line in unnecessary as the 3rd line would prevent the 4th line from executing under that condition anyway. There are some other things I could pick at but you get the idea. I saw another example in the same file where the programmer had reused a TList (linked list class) variable and in some cases this ended up being created twice but freed only once leading to a memory leak, in fact the whole component was leaking a significant amount of resources which is why I ended up looking at the code in the first place. This may sound picky but when you start leaking interfaces and thread lists on 9x machines with low resources you at serious risk of taking a one way ticket to Crash City or BSODville.

I don’t want to berate the quality of OpenSource code nor the coding ability of the authors of the code, nor do I want to tar everyone with the same brush as I’m sure there are plenty of extremely well written projects out there. What I do want say is just because something works for you 99% of the time you should not expect that the code your interfacing with to be written to the same standards you work to or to be fault free more often than not it’s down to code never having operating outside the expected conditions. It’s also advisable that if you do write code that you release to the public you should uphold professional standards of coding practice because you never know when a prospective employer may want to use that code to evaluate your coding ability.