Multi-pass Video Encoding

Discuss about generic usage of MediaCoder.

Moderator: HuggiL

Locked
meRobs
Expert
Expert
Posts: 3116
Joined: Tue Jun 23, 2009 2:45 pm
Location: Sydney, Australia

Multi-pass Video Encoding

Post by meRobs » Sat Sep 17, 2011 2:39 pm

[Updated 15 May 2012]

MediaCoder offers a number of Rate Modes for the video stream. On the Video Tab, the options are Average Bitrate, Variable Bitrate, Constant Bitrate, 2-Pass and 3-Pass.

Because fast or complex scenes require a higher bit rate than slow/simple scenes for the same video quality, the use of Constant Bitrate or, to a lesser extent Average Bitrate (ABR), is likely to yield a poor result for the fast/complex regions of a clip if the overall bit rate is too low. One option is to choose a high average/nominal rate to begin with, say more than ~2000 kbps. In that case, Average Bitrate would be preferred. The Codec then arranges a reduced bitrate for scenes with less detail and a higher rate for more complex or rapidly changing scenes. A reasonable compromise!

On the other hand, for small bitrates, especially below 1000 kbps, an improvement in video quality can be achieved with multi-pass encoding. It also has the potential for the final bitrate and file size to be closer to the target, at least in principle, but not in practice (see Section 4). On the first pass, the encoder analyses the frames and feeds its findings to a temporary log file and then uses this data to more efficiently share the available file size on the next pass. A third pass, if supported by the Codec, may fine tune the result further.

Making a quick check of the various video/format options in MediaCoder, especially looking at the suggestions in the Encoding Guide (viewtopic.php?f=17&t=9643), the only combinations I could find that supported 2- or 3-pass encoding were:
* Xvid via the Xvidcore encoder (failed to work for the FFmpeg encoder)
* and H.264 via x264.

So, I’ll confine my discussion to these two as encoded in MediaCoder build 5185.
Furthermore, the only advantages claimed for multi-pass encoding are (1) improved video quality and (2) achieving a result closer to the target bitrate or file size. These potential gains come at the expense of longer encoding times. Are they worth it? Let’s see!

Contents
1. Source files
2. Multi-pass in Xvid
3. Multi-pass in H.264
4. Table of Results

1. Source files
In all the following tests, done mostly in build 5185, I had the choice of four source files:
File A: a VOB in NTSC (75 MB of 100 sec = 1.67 min)
File B: a VOB in PAL (247 MB of 537 sec = 8.95 min)
File C: the first VOB of a PAL DVD (1.0 GB of 1541 sec = 25.68 min)
File D: first 6 chapters of a DVD ripped to one VOB (1.92 GB of 2729 sec = 45.48 min).

2. Multi-pass in Xvid
Unless otherwise indicated, the build was 5185 and the settings were those defined in Section 2 of the Guide on Xvid at: viewtopic.php?f=17&t=11023, with Xvidcore as video encoder.

I began with a look at 3-pass encoding. For video bitrates set at 700 or 1000 kbps, 3-pass encoding of File A, above, gave an absurd outcome – in each case, the video bitrate given in the Properties panel of MediaCoder was 5273 kbps for B-Frames set at 0 and 3446 kbps with B-Frames set at 3. Also, the encoding took almost twice as long as a 2-pass. During the first two passes the encoding-frame rate was only half that of the first pass during 2-pass encoding! This indicates that Xvid does not support 3-pass, a finding that confirms a statement by Stanley in Jan 2010: http://www.mediacoderhq.com/forum/viewt ... 39&start=0. Also, there are some helpful comments on 2- and 3-pass with Xvid here: http://www.mediacoderhq.com/forum/viewt ... f=2&t=8377

Moreover, as stated in the Bug Report viewtopic.php?f=28&t=11006 , my attempts to encode with Xvid/AVI (Xvidcore encoder) failed for all source files of more than ~350 MB (~15 min). See also, the more recent Bug: viewtopic.php?f=28&t=11982. So I limited my testing of Xvid to the smaller files, File A and File B, using only single pass (ABR) and 2-pass encoding.

2.1 Video Bitrate
Using the Video Bitrate setting on the Video tab in MediaCoder, various choices were examined for File A and File B, with the results given in Table 1, below. The reason for not trying larger source files is given above.

To begin with, let us consider the results for File A, a short file of only 1.7 min. The single-pass outcome (file header) was higher than the set bitrate by about 2% at 400 kbps and the error became lower at higher bitrates. By 700-1000 kbps the output bitrate was roughly equal to the set value and eventually became low by ~2% at 2000 kbps.

The effect of 2-pass encoding was to make matters worse. At 400 kbps, the output rate was low by 6.5%, even worse at 700 kbps (low by 9.6%) and improved at higher bitrates to become low by only 2% at around 2000 kbps. The results are even worse if you consider the bitrates calculated from the file size.

For the larger file, File B of ~9 min, the situation was much better. The single-pass bitrate in the output (header value) was within 0.2% of the set value for 400 to 1000 kbps and low by only 0.3% at 1500 kbps. The 2-pass results were worse; though much better than for single pass. At worst, the 2-pass result was 2.6% low at 700 kbps, about 4x better. Hence at even larger files, one may expect the bitrates for both the single-pass and the 2-pass to be closer to the set value.

Conclusion: 2-pass encoding offers no improvement over one-pass (ABR) in achieving a desired Video Bitrate! For the above examples, 2-pass encoding took 38% longer than single pass.

2.2 Output Size
I was not able to check the accuracy of Output Size for File B in build 5185. All attempts gave the error: “The encoding thread seems to be blocked … continue current task?”, as described in viewtopic.php?f=28&t=11006. And, there was little point in testing this feature in a file as small as File A.

2.3 Video Quality
The encoded outputs for File A were examined in Gspot. For a set rate of 400 kbps, the change to 2-pass increased the number of B-frames from 56% to 63% and re-arranged them to better suit the scene complexity. Unfortunately, the saving in bits was not made use of in this small file – apart from lowering the average bitrate by 8%! The results for 700 kbps were similar; with the number of B-frames increasing from 59% to 63% to cause a decrease in average bitrate of 10%.

However, the expectation that 2-pass encoding would cause an improvement in video quality is a logical consequence of the second pass being able to give a better distribution of the available bits (bitrate) and to make use of the saving in bits via the use of more B-frames. The latter benefit was not used in the small file, File A. As indicated by the results for File B, 2-pass encoding for larger files, of 20 min or more, would make use of both benefits.

Inspection of the encoded files in a software player seemed to reveal a slight improvement in quality when using 2-pass at 400 or 700 kbps, but was hard to spot. It depends on the source material and is very subjective. So, I will leave such decisions to you!

Conclusion: logically, the use of 2-pass encoding with Xvid should give at least a slight improvement in quality for small files of less than 10 min and the improvement is expected to be more for longer files.

3. Multi-pass in H.264
Unlike the encoding to Xvid, above, 3-pass encoding is possible with H.264. Also, I experienced no problems encoding the video of large source files to H.264. Unless otherwise indicated, the build was 5185 and the settings for the tests on Multi-pass encoding were those defined in Section 2 of the Guide on H.264 at: viewtopic.php?f=17&t=11026.

3.1 Video Bitrate
Using the Video Bitrate setting on the Video tab in MediaCoder various choices were examined for Files A to D. Since the output video bitrate shown in the Properties panel of MediaCoder is the set value, not the actual average value achieved, their values had to be calculated using equation 2 in Section 4 of the above guide on H.264.

For File A, of only 1.7 min, the calculated bitrates for set values of 400 to 2000 kbps applied in single-pass encoding were all low, by 2.5 to 3.8%. For larger files, the single-pass results for 700 kbps were closer to the set value, being low by only ~1%. These, and other results, are shown in Table 2, below.

By the way, the reason I chose 700 kbps as a representative bitrate is that many people seem to compress movies to 700-800 MB (maybe easier to stream on the web?). If for example, we take the capacity of a CD (700 MB) as a starting point, then, from Section 4 of viewtopic.php?f=17&t=8740&start=0, it follows that the sum of the Video and Audio bitrates in kbps must be less than 95,000 divided by the duration of the Movie being burned to the disc. For example, a 2-hour Movie would fit on a CD with video at an average of 700 kbps provided the audio rate is less than 92 kbps, such as 80 kbps (multiple of 8).

Since the frame structure for H.264 is not viewable in Gspot, I can make no comment on what happened in going to 2-pass encoding, other than to note from Table 2 that the resultant bitrate was further from the set value. The extra pass in 3-pass encoding made no improvement on this.

Conclusion: at 700 kbps, at least, 2-pass and 3-pass encoding did not improve on the accuracy of achieving the desired bitrate.

3.2 Output Size
I encoded File C (of ~26 min) to H.264 with Output size set at 145 MB. From equation (1) in the Guide on H.264, the video bitrate for this 1541-sec file with audio at 80 kbps would be (5.316S – 80) kbps, where S is the file size in MB. Thus, we would expect the resultant file to have the set value of 145 MB, with video at 691 kbps. However, the results were not as hoped. Single-pass encoding (ABR) gave a file of 136.10 MB (video calc. at 644 kbps) and both 2-pass and 3-pass gave a file of 135.11 MB (calc. at 638 kbps)!

Conclusion: in this limited test, single-pass encoding to H.264 gave a file size lower than the set value by 6% and an even lower value (7% low) resulted with either 2-pass or 3-pass. This suggests that multi-pass encoding does not improve the accuracy of achieving a desired file size.

3.3 Video Quality
Since the frame structure (B-frames, etc) in H.264 video in MP4 or AVI was not accessible in Gspot I could not check what happened in Multi-pass encoding. Certainly, if the encoder does what it is designed to do, multi-pass should improve quality. However, I must leave this subjective judgement to you.

4. Tables of Results

Table 1: Results for setting Video Bitrate in Xvid/AVI
---------------------------------------------------------------------
Set Bitrate ............ Single pass ...............................2-pass
.............Header bitrate ... Calc. bitrate ... Header bitrate ... Calc. bitrate
Results for File A (only ~1.7 min)
400 ............... 407 ............... 413 ............... 374 ............. 382
500 ............... 506 ............... 510 ............... 456 ............. 462
700 ............... 702 ............... 633 ............... 701 ............. 634
1000 .............. 997 ............... 988 ............... 918 ............. 912
1500 ..............1483 .............. 1462 ..............1423 ............1404
2000 ..............1962 .............. 1928 ............. 1954 ............1920
Now for File B (~9 min)
400 ............... 401 ............... 407 ............... 390 ............. 396
700 ............... 699 ............... 699 ............... 682 ............. 682
1000 .............. 998 ............... 991 ............... 981 ............. 975
1500 ............. 1496 ..............1479 .............. 1486 ............ 1469

Table 2: Results for setting Video Bitrate at 700 kbps with H.264/MP4
------------------------------------------------------------------------------------------
........................ Calculated Bitrate (kbps)
Source File ....... Single pass ..... 2-pass ..... 3-pass
File A (1.7 min) ...... 676 ........... 688 ........ 689
File B (9 min) .........694 ............690 .........689
File C (26 min) ....... 694 ........... 688 ........ 688
File D (45 min) ...... .688 ........... 688 .........688
Have you checked out the Tips & Guides for MediaCoder? Try: http://forum.mediacoderhq.com/viewtopic.php?f=17&t=8061
Also, get older builds at: http://www.videohelp.com/tools/MediaCoder/old-versions#download

meRobs
Expert
Expert
Posts: 3116
Joined: Tue Jun 23, 2009 2:45 pm
Location: Sydney, Australia

Re: Multi-pass Video Encoding

Post by meRobs » Sat Sep 17, 2011 2:41 pm

This Topic has been added to the Tips & Guides setcion.

If you have any usefull commenst or criticisms, please start up a new Topic and refer to this Guide.
Thanks
Have you checked out the Tips & Guides for MediaCoder? Try: http://forum.mediacoderhq.com/viewtopic.php?f=17&t=8061
Also, get older builds at: http://www.videohelp.com/tools/MediaCoder/old-versions#download

Locked