Return of the Xvid: check your outputs!

Return of the Xvid: check your outputs!


A long time ago, on a blog post not that far away...

I once wrote an article on Xvid 1.1.3, and the speed boost one could get by enabling assembly-optimized code. Well, this is a case of my being hoisted by my own petard -- however, I must admit that several things were against me.

In short, Murphy's Law struck again. But first, for those of you who don't want to click through old blog posts, a (fair) bit of history.

Back to the Mayo

A few years back, the Motion Picture Expert Group decided to create a new standard for video compression to replace the much abused and too limited ISO/IEC 13818 standard, also known as h.262, and amorously published under the name MPEG-2. In a fit of imagination, they called it h.263 - dubbed MPEG-4 (because, due to some systems' limitations with file suffixes, MPEG-1 layer 3 audio files were now dubbed 'mp3', or MPEG3).

Creating h.263 wasn't instantaneous: several companies started creating their own implementations and adding their grain of salt. Among them, was Microsoft, who published some codecs that became Windows Media Video 7 and 8. These early codecs were hacked and improved by the now famous hacker Gej, and released as DivX 3.11 ;-) (yes, the trailing smiley is part of the name).

But, Gej being a responsible kid, and not liking Microsoft's hounds on his heels, decided to scrap his codecs and, making use of more recent h.263 drafts, started implementing his own codec, with a few DivX 3 ;-) compatible fixes thrown in, as Project Mayo (and yes, Mayo stands for mayonnaise - Gej IS French).

That project succeeded in creating an MPEG-4 codec, but then some developers decided to keep further modifications for themselves (ProjectMayo's code was not released under the GPL, you see) and created the DivX company, seller of the DivX codec, and several tools. They are quite rooted into Free software though, providing some Linux binaries of the codec (but not very often, due to a lack of demand), used to promote Virtualdub as an encoding tool, and released their main tool Doctor DivX under the GPL a while back. Dr. DivX can work with codes OTHER than DivX, amongst which is Xvid.

ProjectMayo pretty much disappeared when DivX 4 came out. Some die-hard developers decided to take the existing code, created a project with a reverse spelling anagram of DivX: Xvid.

Don't think for a second they weren't successful: while initially Xvid didn't enjoy as much development as DivX with its paid staff, the developers were far from being bad, and several of them were actually amongst the most brilliant codec developers available: soon, Xvid reached a quality level that rivalled, and sometimes exceeded, DivX's -- while still remaining very compatible (h.263 being very complex, there were cases of compliant implementations being unable to playback a stream coming from a different encoder), while DivX itself was miles above the rest of the market quality and speed-wise.

A consensus has been reached that, while DivX for Windows (and Mac) is a very powerful yet easy to use set of tools for MPEG-4 encoding, Xvid is the codec of choice for crisp, extreme encoding jobs under the h.263 format.

However, Xvid fell prey to its own success: many of its core developers were hired by companies that produce MPEG-4 encoders, barring them from the project - and Xvid seemingly stalled in mid-2007; after its 1.1.3 revision, which was a bug-fix release, the core code remained basically the same since as it was in 2006, when 1.1.0 came out.

By then, dual core systems had started becoming common, and 64-bit systems were slowly appearing. This gave some developers new opportunities:

  • use multiple parallel threads to encode frames faster,

  • use 64-bit extensions and rely upon a larger set of floating point functions on mainstream processors.

Up till then, most encoders had relied upon the Pentium Pro instruction set, and some had started adding support for MMX and SSE (mainly FPSSE), and were single-threaded; optimizing a codec for 64-bit, SSE2 and SMP actually meant rewriting it almost from scratch. At DivX's, they did that all over the DivX 6 releases (DivX 5 mainly added better motion detection and macroblock compression); at Xvid's, that was a plan for release 1.2... which came out in December of 2008.

Competition, and the power of FOSS

One of the last steady Xvid developers is Koepi, very well known on the Internet for maintaining the Windows binary (Xvid, due to patent concerns, distributed only source code for a long time). Others, like Plugh, Isibaar and skal actually do most of the coding, but are much less public about it. They didn't sit idle for two years: one provided bugfixes (that Koepi included in 'unofficial' tarball and binaries), another made steady improvements towards Xvid 1.2, yet another looked at sharing code between x264 and Xvid... I should mention too that due to its compliance and quality, Xvid has gotten some fame in the industry, and a small company has been built around the project, providing specifications, profiles definition and professional support.

As I wrote earlier, yes, Xvid 1.2 is out (1.2.1 is the latest release); it comes with:

  • SSE2 and SSE3 optimizations: they're here, might as well use them. Support isn't required and fall-backs exist, but Xvid goes faster with such support.

  • 64-bit assembly optimizations: Xvid 1.1.3 could work on 64-bit platforms, but wasn't particularly optimized for it; now it is.

  • partial parallel multiple threading support, with optimized threads detection: in earlier snapshots you had to specify the number of threads you wanted, now Xvid does that for you.

  • revamped motion detection; while it's not yet like x264's, it's better than before, and some fast tests have been made to port x264's into Xvid. Maybe in release 1.3...

MPEG-4 is a specification that divides an image into squares, and analyzes how these squares mutate from one frame to the next (or the previous in some advanced profiles), while at the same time compressing those squares' content: as such, on top of a macroblock compressor, most of the compression comes from how precisely a codec can detect how macroblocks move, rotate and change. Thus, the most expensive part of compression is movement detection, and this is where optimizations come into play:

  • assembly helps save on CPU cycles: motion detection requiring the repeat of the very same procedure over and over, gaining a single cycle in a single operation can result in huge time savings; for example, comparing the C implementation of Xvid with its assembly-optimized version shows a time reduction of 1/3: an hour long compression would be shortened to 20 minutes.

  • images represent a lot of data, and 64-bit operations can help cut on encoding times: where 32-bit processors may require three cycles to accomplish an operation, a 64-bit may do the same in one (in some cases).

  • SSE2 and SSE3 provide extensions that can allow a CPU to do in a few cycles what previously required several hundreds in SSE1 or thousands in the FPU.

Xvid is free software, and as such it can run on many platforms: x86-32, x86-64, Power, ARM... And since assembly is platform-specific, the Xvid developers decided to share as much work as they could across those platforms: so, they used an intermediary platform and language that could be matched to assembly with minimal loss. This platform is NASM. In short, whatever platform NASM supports, Xvid can run faster on. For all the others, there is the plain C reference implementation.

At the time of Xvid 1.1, NASM didn't support SSE2, much less 3, and didn't support x86-64; so, for a while, Xvid on 64-bit AMD and Intel systems made use of YASM, a mostly compatible implementation of NASM that supported 64-bit. Since then though, NASM caught up and Xvid dropped YASM support. Still, NASM's 64-bit support is very young.

Do you see where I'm going?

NASM strikes

When Xvid 1.2.1 came out (yes, I sometimes hit Koepi's website for his bug-fixed Xvid 1.1.3 tarballs), I immediately tried to compile it; during configure time, I glimpsed the 'no assembly' line, so I removed YASM, and installed NASM instead: visibly, YASM support was gone and NASM was back full force. Then I ran configure again, and then make. It chugged along happily, so I got the new library, linked against it and then I started a video compression. On a whim, I encoded only a small part and checked the results.

Big magenta splotches all over the image.

I started disabling encoding options here and there, tested different applications, checked the links with libxvidcore, tried other videos, and even a no-option, plain dumb single thread encoding produced those artefacts. On a hunch, and knowing where most of the rewrites in Xvid were, I recompiled with plain C support.

Gone were the artefacts (and the speed).

I dutifully made a bug report; I got an answer from someone at XvidSolutions, who was also quite alarmed (this is, after all, a critical bug). After many tries, debug runs, and tests, I decided to try something else: I updated NASM to use the latest released version (2.05.1 at the time of writing), recompiled Xvid and eventually determined the source of the problem.

NASM 2.04, as provided with my distribution (Suse provides 2.02, for example), has a regression in ELF64 compliance which couldn't be caught by automated tests; thus, the bug didn't show on 32-bit platforms, didn't show with Xvid 1.1.3, and didn't show on developers' machines.

Next Xvid release's configure script should have a small check added that verifies that NASM 2.04 isn't installed. And yes, I notified my distribution developers; you can thus expect NASM 2.05.1 in Mandriva 2009.0 Real Soon Now (tm).

Conclusion

The new Xvid version encodes faster; much faster. I haven't noticed a real size or quality gain over 1.1.3, but it's certainly faster if you have a dual core. It doesn't scale well though so don't expect 100% CPU use and 4X encoding speed gain over 1.1.3 on your big bad quad core; it's more like a 40% speed gain on a dual core; just be careful how you compile it.

Which, at the end of the day, provides nice time gains. Now, to see if there's interest in h.264 and x264...

Most data was assembled from discussions and blog posts at Doom9's and on Koepi's website.

Category: 
Tagging: 

Comments

caratuva's picture
Submitted by caratuva on

sir,

I have installed the new Xvid 1.2.1 on a Vista Business 32, Intel Core2Duo 4300, 2GB RAM. Also I have installed TMPGEnc XPress 4.6.3.268. When I try to converte a 10MB .mov file to a .avi using the default configuration, the encoding is extremely slow. I managed to discover the problem is with the number of threads. When it is 0 (autodetect) or other number different than 1 the encoding is very slow, taking some minutes. When it is 1 everything goes fine and the encoding takes just some seconds.

I searched in Xvid.org some place to report this but I didn´t find.

Regards,

Marco

Mitch Meyran's picture

First and foremost: I'm no XviD developer. I once wrote to one, because I had found a bug which ended up being in a Xvid dependency, not Xvid itself. I assume you're using Koepi's build of Xvid.

VirtualDub works very well with Xvid 1.2.1, thus the bug could be in TMPGenc. TMPGEnc 4.6 is proprietary. If you have a problem with it, ask its publisher.

If it ends up being a problem with Xvid, the "contact" form in the Xvid Solutions menu on http://www.xvid.org is where you should ask for help.
---
A computer is like air conditioning: it becomes useless when you open windows.

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!