Encoding Guide for DVD to H264
From NMTWiki
SD Encoding Guide (from DVD sources) - Credit to jase99
Use this guide to encode DVDs to H.264 for playback on your PCH.
Contents
* 1 What You Need * 2 Step 1: Source Selection * 3 Step 2: Extract Movie/Episode * 4 Step 3: Index Video & Demux Audio * 5 Step 4: Generate Encoding Script * 6 Step 5: Encode * 7 Step 6: Mux * 8 Q&A
What You Need
Tools and minimum versions:
AviSynth 2.57 DGMPGDec 1.5.0 DVDShrink 3.2.0.15 MeGUI 0.2.6.1046 x264 build 883 mkvmerge 2.2 PCH Profiles
Follow installation guides accompanying the software.
Step 1: Source Selection
For the majority of DVDs, select all VOB files apart from file 0 belonging to the largest titleset and drag them into DGIndex.
Files can reside on harddisk if the DVD has already been ripped or if it was downloaded, or files can be located on a mounted physical DVD as long as AnyDVD is running.
For example, in the image below, titleset 2 (files beginning with VTS_02) is the largest:
Using the slider at the bottom of DGIndex's video window check at the beginning and at the end that there are no copyright warnings or trailers. If encoding an episode of a series check there is only one episode. Skip to step 3 if this is true.
Step 2: Extract Movie/Episode
If there are trailers or warnings present, or the title set contains more than one episode of a TV series then additional processing is required using DVDShrink. Either click 'Open Files' and select the folder containing the downloaded DVD files or click 'Open Disc' if you're working from a disc.
(a) Click 'Re-author'.
(b) Drag the appropriate feature you want to encode (usually to be found in the Main Movie section) into the compilation window on the left.
(c) Click 'Compression Settings'
(d) Select 'No Compression'
(e) Click 'Backup!'
If a 'Target Exceeded' dialog is displayed, click Yes to continue.
The Backup DVD dialog is displayed. Select 'Hard Disk Folder':
Click OK to extract the feature.
Go to step 1 using the newly created set of VOBs as the source.
Step 3: Index Video & Demux Audio
The video we want to encode is now loaded into DGIndex. From the Video, Audio and Options menu check your settings match those shown below:
From the File menu select 'Save Project'.
Type in a name or accept the default and click Save.
After a few minutes the process will be complete.
A file with extension .d2v will have been created and one or more audio tracks will have been demuxed.
An information window will be displayed. Make note of the 'Frame Size' and 'Aspect Ratio' values, we will need them later. The information will also be written into the log file when DGIndex is closed. Some examples are shown:
Step 4: Generate Encoding Script
Create a base AviSynth script (use your favourite editor) called encode.avs as follows:
MPEG2Source("VTS_01_1.d2v",cpu=0) # d2v file created from Step 3
Start MeGUI.
Do NOT update the core and data of meGUI when asked.
Select 'AviSynth Script Creator' from the 'Tools' menu:
Select the 'Video Input' dialog:
Pick 'All files' from the 'Files of type' drop down menu:
Select encode.avs and ok the dialog. A preview window will appear.
Do not attempt to resize the picture. If the picture looks squashed, the DVD is anamorphic. The picture is displaying correctly and will be adjusted during playback.
Deinterlace / IVTC
Use MeGUI's preview window to check for interlacing together with DGIndex's 'Video Type' value from the information window (or log file) from Step 3.
MeGUI has an 'Analyze' function in the 'Filters' tab of its 'AviSynth Script Creator' dialog that may get the de-interlacing method correct.
Below are some common de-interlacing filters with examples of when they might be used:
TDeint() # De-interlace - Pure PAL / NTSC video: Sports, some tv shows/documentaries, PAL movies TFM().TDecimate() # Inverse Telecine - Film: NTSC movies Yadif() # Hybrid - Hard or soft telecined Film with overlayed interlaced video graphics AnimeIVTC() # Specialist - Anime, cartoons Vinverse() # Specialist - Deals with residual combing (invented for Family Guy) Yatta # Superb de-interlacer for masochists
For those of you who want the best de-interlacing possible for pure interlaced content you'll need fast multi-core boxes to work with these:
mcbob()+nnedi() # [doom9]
yadmifmod()+nnedi() # [tritical's filters]
There are many online resources that discuss interlacing, here are a few to be familiar with:
DGIndex Field Operation
Force Film, IVTC, and De-interlacing
Wiki articles Interlace, Telecine
Add appropriate de-interlacing filters to encode.avs and reload it into MeGUI.
Use the slider to scan through the preview window, check that all interlace artifacts are removed.
Post in the Encoding forum for help with specific sources.
Crop
Uncheck 'Resize'. Check 'Crop'. Click 'Auto Crop':
Ensure the cropped width and height are mod 4, divisible by 4 with no remainder. Over crop as necessary to achieve mod 4. Use the slider to scan through the video, review cropping in the preview window. If further cropping is required, crop to the next mod 4 resolution.
In this example the:
Width has been auto cropped by 10+2=12 pixels making the width 720-12=708 pixels
Height has been auto cropped by 6+4=10 pixels making the height 576-10=566 pixels
To achieve mod 4 the:
Width needs no further cropping, 708/4=177 with no remainder
Height needs to be cropped by a further 2 pixels to make 564 pixels
Since cropping can only be performed on a multiple of 2 pixels in the colour space we're working with, the extra 2 pixels can be cropped from either the top or the bottom, we'll crop them from the bottom.
Final crop settings are:
Left = 10 Top = 6 Right = 2 Bottom = 6
If cropping was required then add a crop filter to encode.avs:
MPEG2Source("VTS_01_1.d2v",cpu=0) # d2v file created from Step 3
TDeint()
Crop(10,6,-2,-6) # crop(left,top,-right,-bottom)
Important Note: If the number of pixels cropped from the top of the frame is not divisible by 4 then ensure de-interlacing filters appear before crop filters.
Step 5: Encode
MeGUI or command line based x264 can be used to perform encodes.
MeGUI Encoding
1. Load encode.avs
2. Select 'Tools->Bitrate Calculator', add the appropriate audio tracks and set the required file size
3. Select 'MKV' from the 'File format' drop down menu
4. Select the appropriate PCH profile from the 'Video profile' drop down menu (see Source Aspect Ratio below)
5. Click 'Enqueue'
6. Select the 'Queue' tab
7. Click Start
PCH encoding profiles can be downloaded here.
Extract profiles.rar file into directory: C:\Program Files\megui\profiles\Video
Additional MeGUI resources:
Wiki
General Questions and Troubleshooting
Development
Source Aspect Ratio
Refer to the 'Frame Size' and 'Aspect Ratio' in DGIndex's information window (or log file) from Step 3 to determine the correct PCH Profile for MeGUI or SAR for x264 command line encoding:
Frame Size Aspect Ratio SAR PCH Profile 720x576 4:3 12:11 PCH PAL 720x576 16:9 16:11 PCH ANAMORPHIC PAL 720x480 4:3 10:11 PCH NTSC 720x480 16:9 40:33 PCH ANAMORPHIC NTSC
Level 3.0 compliance
Level 3.0 is designed for encoding standard definition.
If you want to encode to level 3.0 specs then first calculate the number of reference frames as follows:
Round up width and height to the nearest mod 16 value Reference frames = 2073600 / ( mod16_width * mod16_height ) Round down the result
In our cropped example above the resolution is 708x564, rounding width and height up to mod 16 gives us 720x576.
The number of reference frames is therefore 2073600/(720*576) = 5.
--vbv-maxrate and --vbv-bufsize must be set to 10000 for level 3.0 compliance.
Command Line
Here's an example of x264 command line parameters to use for a level 3.0 compliant crf encode of our cropped PAL example:
x264 --threads auto --thread-input --level 3.0 --ref 5 --vbv-maxrate 10000 --vbv-bufsize 10000 --fps 25.000 --sar 12:11 --crf 20 --direct auto --deblock -1:-1 --subme 7 --me esa --merange 24 --partitions all --8x8dct --bframes 3 --b-pyramid --mixed-refs --rdcmp psy --b-rdo --bime --weightb --trellis 2 --no-fast-pskip --progress --output encode.h264 encode.avs
Step 6: Mux
Use mkvmerge, dsmux or their respective GUI's to merge the video produced by x264 and the audio extracted in Step 3 to create the final media file. The delay automatically placed into the filename of the demuxed audio track by DGIndex can be used as a starting point when syncing audio and video, it's not correct 100% of the time.
Example:
mkvmerge -o "The Matrix Revisited PAL DVD.mkv" --track-name "0:The Matrix Revisited" --default-duration 0:25fps -d 0 -A -S "encode.h264" --language 0:eng --sync 0:0 --track-name "0:AC3 384 kbps" -a 0 -D -S "VTS_01_1 T80 3_2ch 384Kbps DELAY 0ms.ac3" --track-order 0:0,1:0
Q&A
Q: Does this guide have to be followed exactly?
A: No, it is a guideline. General rules to be followed are:
* Width and height must be mod 4
* Must use correct SAR value
* Must not use any resize filters
Q: Should I use ColorMatrix() ?
A: No. DVD sources do not require this filter.
Q: Should I use bitrate based 2 pass or 1 pass crf?
A: Either is fine. It is generally acknowledged that bitrate based 2 pass produces a slightly higher quality encode than crf.
Q: Is it ok to use DGDecode_MPEG2Source(index.d2v,cpu=0) instead of MPEG2Source(index.d2v,cpu=0). What are the differences?
A: If another dll was to expose a function called MPEG2Source then a call to that function would be ambiguous; using DGDecode_MPEG2Source ensures the function MPEG2Source() function from the dgdecode.dll is used. Feel free to use what you prefer. cpu=0 is recommended, it is the default value and therefore doesn't need to be specified.
Q: I get an error when trying to load my avs script saying there is no function named MPEG2Source or DGDecode_MPEG2Source, what is the cause?
A: Avisynth is unable to locate the MPEG2Source function, which is part of the DGIndex application. To fix either copy the file DGDecode.dll from C:\Program Files\DGMPGDec to C:\Program Files\AviSynth 2.5\plugins or alternatively add the line LoadPlugin("C:\Program Files\DGMPGDec\DGDecode.dll") to the start of your avs script.
Q: What should one do if various sections of the DVD need different crop values? This is most common on Documentary films, which are often made up of archive material.
A: Find a scene with the widest possible picture and crop the side at those values. Find a scene with the maximum possible height and crop the top and bottom at those values.
Q: What should one do if various sections of the DVD need de-interlacing and others don't ?
A: Use a motion-adaptive de-interlacer such as: mcbob or yadif.
Q: MeGUI shows a dialog saying the clip does not have mod16 dimensions, what should I do?
A: As long as the clip has mod4 dimensions the message can be safely ignored.
Q: I am seeing "x264 [warning]: width or height not divisible by 16 (XXxYY), compression will suffer", what should I do?
A: As long as the clip has mod4 dimensions the message can be safely ignored.
Q: I'm using a non-stable version of MeGUI and can't get the PCH profiles to load because the XML format is different. How can I get this working?
A: Use final stable version 0.2.6.1046 only.
Q: My encode has a different width to the source, why?
A: If no cropping and no resizing is performed then the source and encode are precisely the same width. The source and encode do not have the same width when played using a software player because the software player does not stretch (or shrink) the width correctly when playing DVD discs, the image is displayed with a small aspect ratio error. In contrast, the image is displayed correctly when playing an encode. Therefore, the width of an incorrectly sized screen from a software player playing a DVD disc will be different to the width of a correctly sized screen from a software player playing an encode. See this doom9 article for more info, in particular read the penultimate paragraph of the first post. By ensuring the SARs from the guide are used we guarantee the image from the encode displayed on the screen using a software player has the smallest aspect ratio error possible when compared to the DVD source disc played on a standalone, which is how it should look
Q: Should I specify SAR with a slash or colon, e.g., 12/11 or 12:11?
A: Either. Both formats work.
Q: Where did the SAR values come from?
A: H.264 specification Annex E. They are recommended and closest to the true SAR values (listed here in section 3). In all cases the difference between the H.264 compliant SAR and the true SAR introduces a small 0.3% aspect ratio error that is not noticeable.
Q: Are the SAR values correct?
A: Yes. They are correctly derived from the visible dimensions of PAL and NTSC at 702x576 and 702x480 respectively. Without cropping, they have a display aspect ratio of 4:3 or 16:9. Example: Full frame PAL displays at 702*12/11x576 = 765x576 = 4:3. Note: 12/11 differs from the true SAR of 128/117 by a tiny amount (see previous Q&A), the true SAR produces 702*128/117x576 = 768x576.
Q: Are the SAR values correct for incorrectly mastered DVDs?
A: No. On rare occasion DVDs are mastered incorrectly as wanting the full overscaned area (720x576 or 720x480) displayed at 4:3 or 16:9. These display with a slightly squished or stretched image when played in a standalone. DVDs incorrectly mastered this way require a different SAR value to correct the error. For example full frame overscan PAL 720x576 displaying at 768x576 requires a SAR value of 16:15. Since the aspect ratio error is small it can be difficult to notice.
Q: Should I make encodes level 3.0 compliant?
A: It's up to you, your choice. It is included in the guide because most H.264 streams produced by x264 from DVD sources will be level 3.0 compliant anyway. The player won't know that if there's no level specified in the H.264 stream header. Might be useful for possible future portable SD H.264 players, mobile phones, etc.
Q: Do the number of reference frames for level compliance depend on --b-pyramid and --bframes?
A: No. Use a build of x264 greater than 721.
Q: Where did the number 2073600 come from in the number of reference frames calculation for level 3.0 compliance? A: Annex A of the H.264 specification shows the MaxDPB for level 3.0 is 3037.5. That's in KB so multiply by 1024 to get bytes then divide by 1.5 to convert from bytes to pixels (1.5 bytes per pixel in YV12 format). 3037.5 * 1024 / 1.5 = 2073600.
