Encoding to H.264

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

Encoding to H.264

Post by meRobs » Tue Sep 13, 2011 8:35 pm

[Updated 2 Oct 2011] -- added de-blocking

The H.264 Codec is based on the MPEG-4 standard Part 10 and uses the MPEG-4 AVC profile (Advanced Video Coding), which was designed to give a similar quality to earlier standards, such as MPEG2, H263 and Xvid (MPEG4-Part 2), at substantially smaller bitrates, at maybe ~half the bitrates! However, it requires more computing power.

With the use of H.264, bit rate savings of 50% or more have been reported. For example, it is claimed to give the same Digital Satellite TV quality as current MPEG-2 implementations with less than half the bit rate, for example, MPEG-2 at around 3.5 Mbps is equivalent to H.264 at only 1.5 Mbps. Also, the Blu-ray disc format includes the H.264/AVC High Profile as one of 3 mandatory video compression formats.

Contents
1. standard x264 Parameters
2. More Parameters
3. Settings for H.264/x264
4. Tests for H.264/x264

1. Standard x264 Parameters
The parameters for achieving the H.264 format are available in MediaCoder on the x264 tab once x264 is set as the Encoder. The x264 tab is opposite the Video tab. There are 7 options on the tab and there are 3 others that are greyed out (for future extension?) as well as the ‘Advanced’ button. A click on this button offers a few more choices and “More Parameters” that opens Settings > Video Encoders > x264 to reveal 70 settings, some of which reflect the user choices already made on the x264 tab. I suggest you leave these extras alone. The 7 parameters on the x264 tab are enough to play with! They are discussed below.
1. Profiles: MediaCoder offers only 4 of the 17 profiles (sets of capabilities) available for x264. Apart from Auto, there are Baseline, Main, High and High10. Baseline has a minimal implementation for use in low-cost applications, such as video conferencing and mobiles. It supports Progressive, not interlaced, as well as I-frames (Keyframes) and P-frames, but not B-frames. The Main Profile is suitable for Standard Definition TV (720x576 and 720x480), the High is suitable for HD TV and Blu-ray and High10 goes a little further.
2. Levels: a level sets restraints on the maximum resolution, frame rate and bit rate, etc. For example, Level 2 allows up to 2000 kbps for Baseline to Main and up to 30 fps for up to 352x288. Level 3.0 allows up to 10 Mbps for the Baseline and Main profiles and up to 720x576 pixels at 25 fps (PAL) or the equivalent in NTSC (720x480 at 30 fps). Level 4 takes the max bit rate to 20 Mbps and allows 1920x1080 at 30 fps.
3. Presets: MediaCoder offers 9 choices apart from 'Custom'. They vary from 'Ultra-Fast', for max encoding speed at the expense of quality, to 'Placebo' for maximum quality at the expense of encoding time. The default in MediaCoder is 'Medium', which is intermediate. According to http://blog.dest-unreach.be/2009/09/08/x264-presets, quality increases rapidly from Ultra-Fast to Very Fast, then, more gradually through Fast and Medium and offers little improvement from Medium through to Placebo. This test was not definitive, but does suggest that the Fast Preset is possibly the most efficient – a good compromise (see Section 3).
4. Tune: this option allows x264 to optimise things to suit the type of source file. It is ‘Normal’ by default.
5. GOP: the GOP (Group of Pictures) is effectively the number of video frames in the group of frames that begins with each I-frame (Keyframe). Here, the minimum and maximum values are set.
6. B-Frames: B-frames are used more efficiently in H264 than in earlier standards, with up to 16 being allowed consecutively, with rarely a problem if 16 is set, since the best number should be auto chosen. However, note that the number of B-frames actually used by x264 probably cannot exceed the number of Ref frames set (below). By default, MediaCoder sets 3 as the max for B-frames and allows a choice from 1 to 16. Also, even though the 'Baseline' Preset doesn't support B-frames, there is no harm in leaving the B-frame setting at 3, since this merely sets the maximum allowed and the encoder should select the appropriate number (none in this case). I did a quick check by setting B-frames to either 0 or 6 and it made no difference to the file size or encoding time (Baseline profile).
7. Ref. Frames: This is the maximum number of reference frames the encoder can use, presumably, in each GOP group. With a similar function to I-frames, they are used as reference by P- and B-frames. Each B-frame needs two reference frames, one before and one after. Typical content could do with 3 to 5 reference frames. Whereas, the increased number of reference frames tends to increase quality, it does require more encoding and, hence, more time.

2. More Parameters
De-blocking: at low bit-rates, video frames tend to break up into macro blocks that form a grid of squares. This artefact in source files may be minimised by the De-blocking loop filter, which by default, MediaCoder has enabled for x264 (under the ‘Advanced’ button). The filter automatically determines how strongly (alpha parameter) de-blocking should be applied and in doing so it decides how much of the blocky pattern is unwanted and how much is real. The latter sets the threshold, known as the beta parameter. By default, both parameters have the value ‘0’, which should give optimum results. If you must change the value for alpha keep it between + and – 3 (on the x264 tab, under Advanced > More Parameters > “Alpha parameter of de-blocking filter”). It is usually best to leave the beta parameter at 0.
Note: the de-blocking effect applied by x264 during processing does not seem related to the de-blocking available from the Clipping and Effects window (Effects button on Picture tab). The latter seems to be a Post-processing filter and maybe should not be applied when H.264/x264 is used.

3. Settings for H.264/x264
Based on the above comments, I chose the following settings as an optimum combination for H.264 video at resolutions up to 720x576 pixels and AAC audio in MP4. For higher resolutions, increase the x264 Profile, Level and Preset, etc (see above).
* Video tab: Format = H.264, Encoder = x264, Source = MEncoder, Video Bitrate = 1000 kbps and Rate Mode = Average Bitrate (ABR).
* x264 tab: Main Profile, Level 3, Fast Preset, GOP = 25 ~ 250, B-Frames = 3 and Ref Frames= 3, with all else at their defaults
* Audio tab: Format = LC-AAC, Encoder = Nero Encoder, Source = Auto Select and Resample = 24 kHz and (at right) Rate Mode = Target Bitrate set at 80 kbps
* Container: Container = MP4 with Multiplexer = Auto Select,
* Picture tab: Resize = 512x384 pixels and Frame Rate = Original.
These are essentially the same as given in the Encoding Guide (viewtopic.php?f=17&t=9643). The main difference is a change to the x264 tab in the Guide, done for convenience, as explained in Note 10 of that Guide.

4. Tests with H.264/x264
All the following tests were done mostly in the x86 build 5185, and checked in builds 5002 and 5130, on a PC that had a dual-core CPU with 2 GB of RAM and Windows XP SP3.
Hence, any times (speeds) given below are of relative importance only, although, they do serve as a useful guide.

I used the settings given in Section 2, above, except where indicated. I had the choice of four source files:
File A. a 100-sec VOB of 75 MB in NTSC with all 720x480 pixels full (no black bars)
File B. the first VOB of a PAL DVD (1.0 GB – 1454 sec)
File C. the first VOB of a second PAL DVD (1.0 GB – 1541 sec)
File D. first 6 chapters of the DVD in B ripped to one VOB (1.92 GB – 2729 sec).

4.1 Effect of Profile & Preset on Encoding speed
I tested the encoding times for file A, above, using MediaCoder build 5185.
With the Main Profile set, the time to encode the 100-sec file is given in brackets for each Preset: Ultra Fast (took 22 sec), Very Fast (37 sec), Faster (56 sec), Fast (78 sec), Medium (91 sec), Slow (107 sec), Slower (198 sec), very Slow (321 sec) and Placebo (682 sec). Notice than the use of the Placebo Preset requires encoding that takes ~7x longer than the duration of the source file and is ~9x slower than ‘Fast’!

By comparison, with the Baseline Profile and Ref. Frames set at 2, Preset = Ultra Fast (took 21 sec), Very Fast (32 sec), Faster (44 sec), Fast (60 sec), Medium (61 sec), Slow (79 sec), Slower (127 sec), very Slow (230 sec) and Placebo (604 sec).
Thus, encoding with the Baseline Profile is faster, as expected. Also note that the use of Baseline with Very Fast, as suggested in the Encoding Guide (viewtopic.php?f=17&t=9643), is ~2.5 times faster than that recommended here (Main and Fast).

Naturally, if you have plenty of time, or would like the comfort of knowing you have done your best, you could try the slower Presets – slower than ‘Slow’, maybe even ‘Placebo’! I suggest you do a few tests first to see if you can spot any difference?!

4.2 Reference Frames
As a check, with ‘typical’ content, I chose file A (above) and converted it using the settings given in Section 3, but at 700 kbps and Ref-frames set at 0, 2, 4, 10 or 16. Encoding took 61 sec, 67 sec, 70 sec, 94 sec and 115 sec respectively. This suggests that even though a large number of such frames may not be needed, the encoder used them. This conclusion was confirmed by a repeat test with a similar file but containing a scene that moved very, very slowly (slowed down considerably in Adobe Premiere). It gave times that progressed in much the same way, from 46 to 67 sec.

4.3 Effect of Setting Video Bitrate
I converted file A using the settings given in Section 2, but with Video Bitrate set at the values indicated below. I include the encoding times for relative comparison. Also, when encoding using H.264/x264 in MediaCoder, with a set Video Bitrate, the value of video bitrate written to the file header, and hence displayed in the Properties panel (and MediaInfo), is not the actual bitrate in the output. It is the set value. So, I included the bitrates as calculated from equation (2) in the Guide at: viewtopic.php?f=17&t=12438.

When set at 400 kbps it took 57 sec for 5.740 MB, thus calc. bitrate = 390 kbps
When set at 700 kbps it took 69 sec for 9.234 MB, thus calc. bitrate = 676 kbps
When set at 1000 kbps it took 78 sec for 12.721 MB, thus calc. bitrate = 962 kbps
When set at 1500 kbps it took 88 sec for 18.530 MB, thus calc. bitrate = 1438 kbps
When set at 2000 kbps it took 97 sec for 24.358 MB, thus calc. bitrate = 1915 kbps.
Thus, the actual (calculated) bitrate was about 96% of the set value, in each case !!

The four files A to D, varying from 75 MB to 1.92 GB, were then converted with 700 kbps set as the video bitrate.
The output file sizes and calculated video bitrates were as follows.
File A: 9.233 MB with video bitrate of 676 kbps
File B: 136.00 MB with video bitrate of 686 kbps.
File C: 145.51 MB with video bitrate of 693 kbps
File D: 255.82 MB with video bitrate of 688 kbps
In this test, the resultant bitrate was low by 4% for the short duration file and 1-2% for the longer files.
In conclusion, attempts to achieve a desired video bitrate gave values somewhat less than set. The error was about 4% for file of less than 2 min yet only 1-2% for files of 24 to 45 min.

4.4 Effect of Setting Output Size
I examined the ability to achieve a chosen Output Size. For this to happen, you need to check (tick) both Auto Bitrate and Output Size on the Video tab. In this test, the Rate Mode was set at Average Bitrate (ABR).
Using the above results as a guide, I set Output Size = 135 MB for file B, 145 MB for file C and 256 MB for file D, where the source files, file B and file C, were each 1.0 GB and file D was 1.92 GB. The resultant output sizes were 127 MB, 136.1 MB and 236.6 MB, which were all low, at 92-94% of the set value.

4.5 Multi-pass Encoding
Multi-pass encoding (2-pass and 3-pass encoding) is also available. It is as an alternative to Average Bitrate for “Rate Mode” on the Video tab. However, this is the subject of a separate Guide, at viewtopic.php?f=17&t=11033.
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: Encoding to H.264

Post by meRobs » Tue Sep 13, 2011 8:37 pm

The above post has been added to Tips & Guides.

If any users could make any suggestions or add useful comments, please start a new Topic that refers to this one.
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