comparison DOCS/xml/en/mencoder.xml @ 15010:aec21cf429cb

Improved encoding guide: - explains how to do a smart resize - calculate a bitrate to target a certain size - rip audio and transcode in Ogg/Vorbis - a lot of libavcodec's options to come: muxing in matroska files...
author gpoirier
date Fri, 25 Mar 2005 21:17:21 +0000
parents 85546d215f6e
children b0dd473ed38a
comparison
equal deleted inserted replaced
15009:ae563b0f4d46 15010:aec21cf429cb
519 519
520 <sect1 id="menc-feat-dvd-mpeg4"> 520 <sect1 id="menc-feat-dvd-mpeg4">
521 <title>Making a high quality MPEG-4 (&quot;DivX&quot;) rip of a DVD movie</title> 521 <title>Making a high quality MPEG-4 (&quot;DivX&quot;) rip of a DVD movie</title>
522 522
523 <para> 523 <para>
524 One frequently asked question is "How do I make the highest quality DVD 524 One frequently asked question is "How do I make highest quality rip for
525 rip possible? I don't care about file size, I just want the best 525 a given size?". Another question is "How do I make the highest quality DVD
526 quality." 526 rip possible? I don't care about file size, I just want the best quality."
527 </para> 527 </para>
528 528
529 <para> 529 <para>
530 This question is perhaps at least somewhat wrongly posed. After all, if 530 The latter question is perhaps at least somewhat wrongly posed. After all, if
531 you don't care about file size, why not simply copy the MPEG-2 video 531 you don't care about file size, why not simply copy the entire MPEG-2 video
532 stream from the DVD whole? Sure, your AVI will end up being 5GB, give 532 stream from the the DVD? Sure, your AVI will end up being 5GB, give
533 or take, but if you want the best quality and don't care about size, 533 or take, but if you want the best quality and don't care about size,
534 this is certainly your best option. 534 this is certainly your best option.
535 </para> 535 </para>
536 536
537 <para> 537 <para>
549 <systemitem class="library">libavcodec</systemitem> to encode the video, 549 <systemitem class="library">libavcodec</systemitem> to encode the video,
550 although the theory applies to other codecs as well. 550 although the theory applies to other codecs as well.
551 </para> 551 </para>
552 552
553 <para> 553 <para>
554 The main assumption of this guide is that you have no specific file size 554 If this seems to be too much for you, you should probably use one of the
555 constraints and have no problem giving up bits in exchange for quality. 555 many fine front-ends that are listed on our
556 While much of the information presented here is useful in any case, some 556 <ulink url="http://mplayerhq.hu/homepage/design7/projects.html">related projects page</ulink>.
557 of it may work against you if you have a file size goal, such as fitting a 557 That way, you should be able to achieve high quality rips without too much
558 video on a CD. 558 thinking, because most of those tools are designed to take clever decisions
559 for you.
559 </para> 560 </para>
560 561
561 <sect2 id="menc-feat-dvd-mpeg4-2pass"> 562 <sect2 id="menc-feat-dvd-mpeg4-2pass">
562 <title>Constant Quantizer vs. two pass</title> 563 <title>Constant Quantizer vs. two pass</title>
563 564
588 <systemitem class="library">libavcodec</systemitem> will use the lowest 589 <systemitem class="library">libavcodec</systemitem> will use the lowest
589 possible quantizer for each macroblock (as specified by 590 possible quantizer for each macroblock (as specified by
590 <option>vqmin</option>, which is 2 by default). As soon as you specify a 591 <option>vqmin</option>, which is 2 by default). As soon as you specify a
591 low enough bitrate that <systemitem class="library">libavcodec</systemitem> 592 low enough bitrate that <systemitem class="library">libavcodec</systemitem>
592 is forced to use a higher quantizer, then you're almost certainly ruining 593 is forced to use a higher quantizer, then you're almost certainly ruining
593 the quality of your video. In general, you should avoid CBR altogether if 594 the quality of your video.
594 you care about quality. 595 In order to avoid that, you should probably down-scale your video, according
596 to the method which will be exposed later on that guide.
597 In general, you should avoid CBR altogether if you care about quality.
595 </para> 598 </para>
596 599
597 <para> 600 <para>
598 With constant quantizer, <systemitem 601 With constant quantizer, <systemitem
599 class="library">libavcodec</systemitem> uses the same quantizer, as 602 class="library">libavcodec</systemitem> uses the same quantizer, as
640 best balance between quality and file size. This is going to vary 643 best balance between quality and file size. This is going to vary
641 depending on the source video. 644 depending on the source video.
642 </para> 645 </para>
643 646
644 <para> 647 <para>
645 A good starting point for a very high quality rip is about 2000Kbit plus 648 If size doesn't matter, a good starting point for a very high quality
646 or minus 200Kbit. For fast action or high detail source video, or if 649 rip is about 2000Kbit plus or minus 200Kbit.
647 you just have a very critical eye, you might decide on 2400 or 2600. 650 For fast action or high detail source video, or if you just have a very
651 critical eye, you might decide on 2400 or 2600.
648 For some DVDs, you might not notice a difference at 1400Kbit. It's a 652 For some DVDs, you might not notice a difference at 1400Kbit. It's a
649 good idea to experiment with scenes at different bitrates to get a feel. 653 good idea to experiment with scenes at different bitrates to get a feel.
654 </para>
655
656 <para>
657 If you aim at a certain size, you will have to somehow calculate the bitrate.
658 But before that, you need to know how much space you should reserve for the
659 audio track(s), so you should <link linkend="menc-feat-dvd-mpeg4-audio">rip
660 those</link> first.
661 You can compute the bitrate with the following equation:
662 bitrate = (target_size_in_Mbytes - sound_size_in_Mbytes) * 1024 * 1024 / length_in_secs * 8 / 1000
663 For instance, to squeeze a two-hour movie into a 702Mbytes CD, with 60Mbytes
664 of audio track, the video bitrate will have to be
665 (702 - 60) * 1024 * 1024 / (120*60) * 8 / 1000 = 740kbps.
650 </para> 666 </para>
651 667
652 </sect2> 668 </sect2>
653 669
654 <sect2 id="menc-feat-dvd-mpeg4-crop"> 670 <sect2 id="menc-feat-dvd-mpeg4-crop">
675 video is sampled, make sure the offset is an even number. (In fact, as a 691 video is sampled, make sure the offset is an even number. (In fact, as a
676 rule, never use odd values for any parameter when you're cropping and 692 rule, never use odd values for any parameter when you're cropping and
677 scaling video.) If you're not comfortable throwing a few extra pixels 693 scaling video.) If you're not comfortable throwing a few extra pixels
678 away, you might prefer instead to scale the video instead. We'll look 694 away, you might prefer instead to scale the video instead. We'll look
679 at this in our example below. 695 at this in our example below.
696 You can actually let the <option>cropdetect</option> filter do all of the
697 above for you, as it has an optional <option>round</option> parameter that
698 is equal to 16 by default.
680 </para> 699 </para>
681 700
682 <para> 701 <para>
683 Also, be careful about "half black" pixels at the edges. Make sure you 702 Also, be careful about "half black" pixels at the edges. Make sure you
684 crop these out too, or else you'll be wasting bits there that 703 crop these out too, or else you'll be wasting bits there that
689 After all is said and done, you'll probably end up with video whose pixels 708 After all is said and done, you'll probably end up with video whose pixels
690 aren't quite 1.85:1 or 2.35:1, but rather something close to that. You 709 aren't quite 1.85:1 or 2.35:1, but rather something close to that. You
691 could calculate the new aspect ratio manually, but 710 could calculate the new aspect ratio manually, but
692 <application>MEncoder</application> offers an option for <systemitem 711 <application>MEncoder</application> offers an option for <systemitem
693 class="library">libavcodec</systemitem> called <option>autoaspect</option> 712 class="library">libavcodec</systemitem> called <option>autoaspect</option>
694 that will do this for you. Absolutely do not scale this video in order to 713 that will do this for you. Absolutely do not scale this video up in order to
695 square the pixels unless you like to waste your hard disk space. Scaling 714 square the pixels unless you like to waste your hard disk space. Scaling
696 should be done on playback, and the player will use the aspect stored in 715 should be done on playback, and the player will use the aspect stored in
697 the AVI to determine the correct resolution. 716 the AVI to determine the correct resolution.
717 Unfortunately, not all players enforce this auto-scaling information,
718 therefore you may still want to rescale.
719 </para>
720
721 <para>
722 First, you should compute the encoded aspect ratio:
723 ARc = (Wc x (ARa / PRdvd )) / Hc
724 <itemizedlist>
725 <title>where:</title>
726 <listitem><para>
727 Wc and Hc are the width and height of the cropped video,
728 </para></listitem>
729 <listitem><para>
730 PRdvd is the pixel ratio of the DVD wich is equal to 1.25=(720/576) for PAL,
731 </para></listitem>
732 <listitem><para>
733 DVDs and 1.5=(720/480) for NTSC DVDs,
734 </para></listitem>
735 </itemizedlist>
736 </para>
737
738 <para>
739 Then, you can compute the X and Y resolution, according to a certain
740 Compression Quality (CQ) factor:
741 ResY = INT( SQRT(1000 * Bitrate / 25 / ARc / CQ) / 16 ) * 16
742 ResX = INT( ResY * ARc / 16) * 16
743 </para>
744
745 <para>
746 Okay, but what is the CQ?
747 The CQ represents the number of bits per pixel and per frame of the encode.
748 Roughly speaking, the greater the CQ, the less the likelihood to see
749 encoding artifacts.
750 However, if you have a target size for your movie (1 or 2 CDs for instance),
751 there's a limited total number of bits that you can spend; therefore it's
752 necessary to find a good tradeoff between compressibility and quality.
753 The CQ depends both on the bitrate and the movie resolution.
754 In order to raise the CQ, typically you'd downscale the movie given that the
755 bitrate is computed in function of the target size and the length of the
756 movie, which are constant.
757 A CQ below 0.18 usually ends up in a very blocky picture, because there
758 aren't enough bits to code the information of each macroblock (MPEG4, like
759 many other codecs, groups pixels by blocks of several pixels to compress the
760 image; if there aren't enough bits, the edge of those blocks are
761 visible).
762 It's therefore wise to take a CQ ranging from 0.20 to 0.22 for a 1 CD rip,
763 and 0.26-0.28 for 2 CDs.
764 Please thake note that the CQ is just an indicative figure, as depending on
765 the encoded content, a CQ of 0.18 may look just fine for a Bergman, contrary
766 to a movie such as The Matrix, which contains many high-motion scenes.
767 On the other hand, it's worthless to raise CQ as higher than 0.30 as you'd
768 be wasting bits without any noticeable quality gain.
698 </para> 769 </para>
699 770
700 </sect2> 771 </sect2>
701 772
702 <sect2 id="menc-feat-dvd-mpeg4-audio"> 773 <sect2 id="menc-feat-dvd-mpeg4-audio">
703 <title>Audio</title> 774 <title>Audio</title>
704 775
705 <para> 776 <para>
706 Audio is a much simpler problem to solve: just leave it as is. Even AC3 777 Audio is a much simpler problem to solve: if you care about quality, just
707 5.1 streams are at most 448Kbit/s, and they're worth every bit. You 778 leave it as is.
708 might be tempted to transcode the audio to high quality Ogg Vorbis, but 779 Even AC3 5.1 streams are at most 448Kbit/s, and they're worth every bit.
780 You might be tempted to transcode the audio to high quality Ogg Vorbis, but
709 just because you don't have an A/V receiver for AC3 pass-through today 781 just because you don't have an A/V receiver for AC3 pass-through today
710 doesn't mean you won't have one tomorrow. Future-proof your DVD rips by 782 doesn't mean you won't have one tomorrow. Future-proof your DVD rips by
711 preserving the AC3 stream. 783 preserving the AC3 stream.
712 </para> 784 You can keep the AC3 stream either by copying it directly into the video
713 785 stream <link linkend="menc-feat-mpeg4">during the encoding</link>.
786 You can also extract the AC3 stream in order to mux it into containers such
787 as NUT, Matroska or OGM.
788 <screen>mplayer source_file.vob -aid 129 -dumpaudio -dumpfile sound.ac3</screen>
789 will dump into the file sound.ac3 the audio track number 129 from the file
790 source_file.vob (NB: DVD VOB files usually use a different audio numbering,
791 which means that the VOB audio track 128 is the 2nd audio track of the file).
792 </para>
793
794 <para>
795 But sometimes you truly have no choice but to further compress the
796 sound so that more bits can be spent on the video.
797 Most people choose to compress audio with either MP3 or Ogg Vorbis audio
798 codecs.
799 While the latter is a very space-efficient codec, MP3 is better supported
800 by hardware players, although this trend is changing.
801 </para>
802
803 <para>
804 First of all, you will have to convert the DVD sound into a WAV file that the
805 audio codec can use as input.
806 For example:
807 <screen>mplayer source_file.vob -ao pcm:file=destination_sound.wav -vc dummy -aid 1 -vo null</screen>
808 will dump the second audio track from the file source_file.vob into the file
809 destination_sound.wav.
810 You may want to normalize the sound before encoding, as DVD audio tracks
811 are commonly recorded at low volumes.
812 You can use the tool <application>normalize</application> for instance,
813 which is available in most distribution.
814 You will compress in either Ogg Vorbis or MP3.
815 For example:
816 <screen>oggenc -q1 destination_sound.wav</screen>
817 will encode destination_sound.wav with the encoding quality 1, which is
818 roughly equivalent to 80Kb/s, and is the minimum quality at which you
819 should encode if you care about quality.
820 Please note that MEncoder currently can't currently mux Ogg Vorbis files
821 into a video stream because it can only create AVI and MPEG files.
822 Don't worry, this document will show you how you can do that with third
823 party programs.
824 </para>
714 825
715 </sect2> 826 </sect2>
716 827
717 <sect2 id="menc-feat-dvd-mpeg4-interlacing"> 828 <sect2 id="menc-feat-dvd-mpeg4-interlacing">
718 <title>Interlacing and Telecine</title> 829 <title>Interlacing and Telecine</title>
799 experiment with higher values and observe the results for yourself. 910 experiment with higher values and observe the results for yourself.
800 </para> 911 </para>
801 912
802 </sect2> 913 </sect2>
803 914
915 <sect2 id="menc-feat-dvd-mpeg4-encoding-options">
916 <title>Encoding options</title>
917
918 <para>
919 Ideally, you'd probably want to be able to just tell the encoder to switch
920 into "high quality" mode and move on.
921 That would probably be nice, but unfortunately hard to implement as different
922 encoding options yield different qualities depending on the source material.
923 Anime and live action are for example two very different materials that
924 require different care.
925 The good news is that some options should never be left out, like
926 <option>mbd=2</option>, <option>trell</option>, and <option>v4mv</option>.
927 See below a detailed description of common encoding options.
928 </para>
929
930
931 <itemizedlist>
932 <title>Options to adjust:</title>
933 <listitem><para>
934 <emphasis role="bold">vmax_b_frames</emphasis>: 1 or 2 is good, depending on
935 the movie.
936 Note that libavcodec does not yet support closed GOP (the option
937 <option>cgop</option> doesn't currently work), so DivX5 won't be able to
938 decode anything encoded with B-frames.
939 </para></listitem>
940
941 <listitem><para>
942 <emphasis role="bold">vb_strategy=1</emphasis>: helps in high-motion scenes.
943 Requires vmax_b_frames >= 2.
944 On some videos, vmax_b_frames may hurt quality, but vmax_b_frames=2 along
945 with vb_strategy=1 helps.
946 </para></listitem>
947
948 <listitem><para>
949 <emphasis role="bold">dia</emphasis>: motion search range. Bigger is better
950 and slower.
951 Negative values are a completely different scale.
952 Good values are -1 for a fast encode, or 2-4 for slower.
953 </para></listitem>
954
955 <listitem><para>
956 <emphasis role="bold">predia</emphasis>: motion search pre-pass.
957 Not as important as dia. Good values are 1 (default) to 4. Requires preme=2
958 to really be useful.
959 </para></listitem>
960
961 <listitem><para>
962 <emphasis role="bold">cmp, subcmp, precmp</emphasis>: Comparison function for
963 motion estimation.
964 Experiment with values of 0 (default), 2 (hadamard), 3 (dct), and 6 (rate
965 distortion).
966 0 is fastest, and sufficient for precmp.
967 For cmp and subcmp, 2 is good for anime, and 3 is good for live action.
968 6 may or may not be slightly better, but is slow.
969 </para></listitem>
970
971 <listitem><para>
972 <emphasis role="bold">last_pred</emphasis>: Number of motion predictors to
973 take from the previous frame.
974 1-3 or so help at little speed cost.
975 Higher values are slow for no extra gain.
976 </para></listitem>
977
978 <listitem><para>
979 <emphasis role="bold">cbp, mv0</emphasis>: Controls the selection of macroblocks.
980 Small speed cost for small quality gain.
981 </para></listitem>
982
983 <listitem><para>
984 <emphasis role="bold">qprd</emphasis>: adaptive quantization based on the
985 macroblock's complexity.
986 May help or hurt depending on the video and other options.
987 This can cause artifacts unless you set vqmax to some reasonably small value
988 (6 is good, maybe as low as 4); vqmin=1 should also help.
989 </para></listitem>
990
991 <listitem><para>
992 <emphasis role="bold">qns</emphasis>: very slow, especially when combined
993 with qprd.
994 This option will make the encoder reduce as much as possible noise due to
995 compression artifacts instead making the encoded video strictly match the
996 source.
997 Don't use this unless you've already tweaked everything else as far as it
998 will go and the results still aren't good enough.
999 </para></listitem>
1000
1001 <listitem><para>
1002 <emphasis role="bold">vqcomp</emphasis>: Tweak ratecontrol.
1003 What values are good depends on the movie.
1004 You can safely leave this alone if you want.
1005 Reducing vqcomp puts more bits on low-complexity scenes, increasing it puts
1006 them on high-complexity scenes (default: 0.5, range: 0-1. recommended range:
1007 0.5-0.7).
1008 </para></listitem>
1009
1010 <listitem><para>
1011 <emphasis role="bold">vlelim, vcelim</emphasis>: Sets the single coefficient
1012 elimination threshold for luminance and chroma planes.
1013 These are encoded separately in all MPEG-like algorithms.
1014 The idea behind these options is to use some good heuristics to determine
1015 when the change in a block is less than the threshold you specify, and in
1016 such a case, to just encode the block as "no change".
1017 This saves bits and perhaps speeds up encoding. vlelim=-4 and vcelim=9
1018 seem to be good for live movies, but seem not to help in with anime;
1019 when encoding animation, you should probably leave them unchanged.
1020 </para></listitem>
1021
1022 <listitem><para>
1023 <emphasis role="bold">qpel</emphasis>: Quarter pixel motion estimation.
1024 MPEG-4 uses a half pixel precision for its motion search by default,
1025 therefore this option comes with an overhead as more information will be
1026 stored in the encoded file.
1027 The compression gain/loss depends on the movie, but it's usually not very
1028 effective on anime.
1029 qpel always incurs a significant cost in CPU time needed to decode (+20% in
1030 practice).
1031 </para></listitem>
1032
1033 <listitem><para>
1034 <emphasis role="bold">psnr</emphasis>: doesn't affect the actual encoding,
1035 but writes a log file giving the type/size/quality of each frame, and
1036 prints a sumary of PSNR (Peak Signal to Noise Ratio) at the end.
1037 </para></listitem>
1038
1039 </itemizedlist>
1040
1041 <itemizedlist>
1042 <title>Options not recommended to play with:</title>
1043 <listitem><para>
1044 <emphasis role="bold">vme</emphasis>: The default is best.
1045 </para></listitem>
1046
1047 <listitem><para>
1048 <emphasis role="bold">lumi_mask, dark_mask</emphasis>: Psychovisual adaptive
1049 quantization.
1050 You don't want to play with those options if you care about quality.
1051 Reasonable values may be effective in your case, but be warned this is a very
1052 subjective.
1053 </para></listitem>
1054
1055 <listitem><para>
1056 <emphasis role="bold">scplx_mask</emphasis>: Tries to prevent blocky
1057 artifacts, but postprocessing is better.
1058 </para></listitem>
1059 </itemizedlist>
1060
1061 </sect2>
804 1062
805 <sect2 id="menc-feat-dvd-mpeg4-example"> 1063 <sect2 id="menc-feat-dvd-mpeg4-example">
806 <title>Example</title> 1064 <title>Example</title>
807 1065
808 <para> 1066 <para>
1370 D Richard Felker III has to say: 1628 D Richard Felker III has to say:
1371 1629
1372 <blockquote><para>It's OK, but IMO it tries to deinterlace rather 1630 <blockquote><para>It's OK, but IMO it tries to deinterlace rather
1373 than doing inverse telecine too often (much like settop DVD 1631 than doing inverse telecine too often (much like settop DVD
1374 players &amp; progressive TVs) which gives ugly flickering and 1632 players &amp; progressive TVs) which gives ugly flickering and
1375 other artefacts. If you're going to use it, you at least need to 1633 other artifacts. If you're going to use it, you at least need to
1376 spend some time tuning the options and watching the output first 1634 spend some time tuning the options and watching the output first
1377 to make sure it's not messing up.</para></blockquote> 1635 to make sure it's not messing up.</para></blockquote>
1378 </para></listitem> 1636 </para></listitem>
1379 </itemizedlist> 1637 </itemizedlist>
1380 </sect3> 1638 </sect3>