Tale of a codec optimisation: doing things the GNU/Linux way

Tale of a codec optimisation: doing things the GNU/Linux way


Encoding is a CPU-intensive operation. Whilst encoding, using optimised code is crucial. In this short article I will explain how I gained a 300% speed boost when encoding DVDs and will show how having the program's sources and being able to talk to the maintainers sometimes really, really helps. Welcome to doing things "the GNU/Linux way".

What's in a codec

Ever since Gej cracked Microsoft's almost-MPEG4 compliant codecs and named them DivX, people have tried their damnedest best to package video in the most efficient way: retaining quality and reducing size. This led to a rewrite of the DivX codec under the name ProjectMayo. It generated two forks: one called DivX, proprietary and closed, edited by DivX Networks, with Windows, MacOS and (obsolete) GNU/Linux versions available in binary form. The other, keeping up the work from ProjectMayo, called itself Xvid and was aimed at creating a GPL-licensed MPEG-7-compliant encoder, distributed in source form "for educational purposes". (It is forbidden to distribute the binary in those countries where MPEG-4 is patented, but patents don't apply to source files, as they describe mathematical formulas, and in most countries those aren't patentable.)

Under Windows, installing Xvid requires you to either compile it yourself (a rather daunting task if you're not a developer) or go the "gray market way" and develop a pre-built binary. Under GNU/Linux, if you install GCC, you can easily download the latest official tarball and build it -and get libxvidcore4.so.1 on your system, ready for use, in five minutes. Or so you'd think.

How do you get your software?

Grab it!

Under Windows, you get whatever software you can get, and run it; if it doesn't fit, you either make do or look for something else. Under GNU/Linux you can do that too but there are often less radical alternatives, or you just forget one thing.

The piece of software can have several packagers--that is, people creating a binary version of it.

Those of you familiar with Gentoo probably know what I am talking about; others, who use Fedora, or Ubuntu, or Mandriva, or /insert-name-of-generic-distro-here/, are now staring at this post a bit dumbly, or thinking I'm crazy. Not quite.

The matter of compile-time optimizations

Currently, most distributions compile their packages with the Intel i586 (original Pentium) instruction set; however the kernel is often compiled with Intel i686 (Pentium Pro) instructions, usually rendering a 10% performance improvement (a legacy kernel is available for users of older processors, like AMD K6 or Pentium MMX). Other packages don't load the processor hard enough to make further optimization really worthwhile. It remains perfectly possible to compile all packages with the i386 instruction set, but this would incur a 20% to 40% performance penalty and that would be unsustainable.

Still, if you use an application that loads your processor hard, for a long time, and which is unable to decide at run-time which optimizations it can use, considering a recompilation is plausible; it may instead mean an extra 20-40% performance boost (don't worry about mplayer: it is already able to load special MMX, SSE or 3DNow! code paths if it detects those processor extensions).

However, one reason I switched to 64-bit was that at least i686 instructions, SSE1 and MMX support would be built into all packages: indeed, the performance boost I got, compared with 32-bit packages, was in most cases self evident.

However, this was not the case with Xvid encoding, which remained just as slow as the Windows 32-bit version--while the use of Linux, 64-bit and full MMX and SSE support built into the binary should have provided me with a significant boost!

The jaw-dropping experience

One day, after a long encoding failed due to an external event (power outage), I decided I'd look into the matter, and build the latest Xvid tarball.

Xvid hasn't been updated in a long while. I decided to get the latest official source package which was the same one my distribution had used some time ago. I did the usual ./configure && make && make install and waited.

The resulting file was a very marginally faster than the previous one, but nothing really ground breaking: using a newer version of GCC could have explained the "boost". So, I looked at ./configure's output - and noticed the message "yasm not detected - using C implementation" (or something along those lines).

Xvid is an educational project. It runs on any platform. So it uses C for its reference implementation, which is supplemented with assembly code for those platforms that interest its programmers enough. To prevent code duplication, the Xvid authors used nasm's syntax to write assembly optimizations for x86, PowerPC, ARM... whatever nasm supported.

nasm added support for x86-64 very recently. Up to then, only the beta version of yasm supported it -and my distribution's package was old. I looked at the package's source RPM, and lo and behold, it required nasm 0.98, which doesn't work in 64-bit -not yasm.

My distribution's package had been compiled using the slow reference C code. So, I installed yasm (I actually compiled it from the very latest released version: 0.7.1, as my distribution was still at 0.6.8): it took five minutes to compile and install. I then recompiled Xvid--and replaced the current libxvidcore4.so.1 file with the result.

I took a few five-second video samples in 640x480 resolution, with some high quality MPEG-4 ASP settings thrown in (GMC, 4 vectors, quarter pixels, Trellis, B-frames...), and timed their compression speed with the original codec and then with my replacement one. Only one core showed constant activity, so I wasn't accidentally using the SMP experimental code path.

It was three times faster. I am not kidding: I had a 300% speed gain. From 7 fps to 21 fps on an Athlon64 X2 3800+ at its factory speed of 2 GHz, with a reasonable DDR400 in dual channel. For a typical DVD backup (a 120 minute movie), this means five hours saved in terms of CPU time.

Conclusion

I reported my results to my distribution, and to the packager. In the next 24 hours, a new package was issued, containing my proposed correction and a few other small changes.

If you also load up your processor with various applications, take a long hard look at its compilation options. I'm no fan of Gentoo, where everything is compiled from scratch, but I can certainly relate to its users and advocates in this instance.

Category: 
Tagging: 

Author information

Mitch Meyran's picture

Biography

Have you ever fixed a computer with a hammer, glue and a soldering iron? Why not? It's fun!

Most forwarded

Interview with Dave Mohyla, of DTIDATA

Dave Mohyla is the president and founder of dtidata.com, a hard drive recovery facility based in Tampa, Florida.

TM: Where are you based? What does your company do?
DTI Data recovery is based in South Pasadena, Florida which is a suburb of Tampa. We have been here for over 10 years. We operate a bio-metrically secured class 100 clean room where we perform hard drive recovery on all types of hard disks, from laptop hard drives to multi drive RAID systems.

Anybody up to writing good directory software?

Since the very beginning, directories (of any kind) have had a very central role in the internet. (I have recently grown fond of Free Web Directory. Even Slashdot can be considered a directory: a collection of great news and invaluable user-generated comments. As far as software is concerned, doing a quick search on Google about software directories will return the free (as in freedom) software directories like Savannah, SourceForge, Freshmeat and so on, followed by shareware and freeware sites such as FileBuzz, PCWin Download Center and All Freeware (great if you're looking for shareware and freeware, but definitely less comprehensive than their free-as-in-freedom counterparts).

Interview with Mark Shuttleworth

Mark Shuttleworth is the founder of Thawte, the first Certification Authority to sell public SSL certificates. After selling Thawte to Verisign, Mark moved on to training as an astronaut in Russia and visiting space. Once he got back he founded Ubuntu, the leading GNU/Linux distribution. He agreed on releasing a quick interview to Free Software Magazine.

Is better education the key to finding better software?

I read David Jonathon's article Anybody Up To Writing Good Directory Software? the other day, which got me thinking about software directories in general. As David mentioned, many of the software directories one finds when doing a quick google search are free as in beer, not as in freedom. But what interests me is the software directories that already exist, providing a combination of both free as in beer software, and open source software. Sites such as Freeware Downloads and Shareware Download don't advertise themselves as providing free as in liberty software, but each of them have a good selection of open source software available... if you know where to look.

Most emailed

Free Open Document label templates

If you’ve ever spent hours at work doing mailings, cursed your printer for printing outside the lines on your labels, or moaned “There has got to be a better way to do this,” here’s the solution you’ve been looking for. Working smarter, not harder! Worldlabel.com, a manufacture of labels offers Open Office / Libre Office labels templates for downloading in ODF format which will save you time, effort, and (if you want) make really cool-looking labels

Creating a user-centric site in Drupal

A little while ago, while talking in the #drupal mailing list, I showed my latest creation to one of the core developers there. His reaction was "Wow, I am always surprised what people use Drupal for". His surprise is somehow justified: I did create a site for a bunch of entertainers in Perth, a company set to use Drupal to take over the world with Entertainers.Biz.

Update: since writing this article, I have updated the system so that the whole booking process happens online. I will update the article accordingly!

So, why, why do people and companies develop free software?

More and more people are discovering free software. Many people only do so after weeks, or even months, of using it. I wonder, for example, how many Firefox users actually know how free Firefox really is—many of them realise that you can get it for free, but find it hard to believe that anybody can modify it and even redistribute it legally.

When the discovery is made, the first instinct is to ask: why do they do it? Programming is hard work. Even though most (if not all) programmers are driven by their higher-than-normal IQs and their amazing passion for solving problems, it’s still hard to understand why so many of them would donate so much of their time to creating something that they can’t really show off to anybody but their colleagues or geek friends.

Sure, anybody can buy laptops, and just program. No need to get a full-on lab or spend thousands of dollars in equipment. But... is that the full story?

Fun articles

Santa Claus - the most successful open source project

It dawned on me the other day, as I was shopping for the dozens of gifts it seems I have to buy every December, that Santa Claus is the most successful open source project in history. (Bridget @ Illiterarty would agree with that). Santa Claus is essentially a marketing development that is embodied by everyone who stuffs a sock, gives a gift, hosts a dinner or wishes Merry Christmas over the holiday season.

Most emailed

Editorial

When I first started thinking about Free Software Magazine, I was feeling enthusiastic about the dream. I had Dave, Gianluca, and Alan willing to help me, I had established members of the free software community willing to help me out, I had writers volunteering their time and energy for free, and I had a generous offer from OpenHosting for servers, all before I'd proved myself. There was a sense of excitement in the air, and I thought maybe, just maybe, I could make this work.

Free Software Magazine uses Apollo project management software and CRM for its everyday activities!