readme
来自「Motion JPEG编解码器源代码」· 代码 · 共 272 行
TXT
272 行
mpeg2enc encodes MJPEG AVIs, Quicktime Files or Edit List Filesto MPEG-1/2 video streams.DISCLAIMER:This software is from the MPEG Software Simulation Group. See noticesin source code regarding Patents and miscellaneous IP issues.POST MPEG SSG DevelopmentRainer Johanni changed the input routine so that "lavtools" EditLists, AVI's and Quicktime input is accepted, introduced command lineparams instead of a param file and made some simple optimizations.Rainer also did the initial mod to accept the new special yuv format.Andrew Stevens added more intelligent motion compensation andoptimised some key calculations that run badly on modern highlypipelined machines. These were then further refined based on theresults of some work by Juan Remero Arroyo (who would have guessed 4*4blocks still produce useful results!). Chris Atenasio's contributed MMX2/SSE code for the motion compensationmacroblock group difference calculation.Andrew Stevens added a lot more MMX1 code for motion compensation andadapted Chris' code for his intelligent motion compensation mods andported the fdct/idct MMX code to nasm.Andrew Stevens added some simple pre-processing filters to improveoutput quality when using noisy (e.g. broadcast) source video.Andrew Stevens modified the bit-allocation routines to use a moreaccurate method of estimating bits needed for good quality results.Andrew Stevens fixed a number of overflow and calculation errorsassociated with large files.Andrew Stevens ported in and tidied up a number of MMX routines fromthe bbmpeg project for windows (see www.delphi.com/bbmpeg_ug) toaccelerate a few "second line" but nonetheless significantlytime-consuming functions.Andrew Stevens added a new much faster MMX / 3Dnow / ISSE basedquantization routines.Andrew Stevens replaced the use of SAD with variance for selecting motion compensations. Slightly more expensive but produces *markedly* better results. Similarly added support for checking the quality of the chrominance match achieved.Andrew Stevens added limited SMP support and basic chrominance matchchecking in motion compensation.Andrew Stevens added support for variable-sized GOP's and sequencesplitting at file-size boundaries.James Klikman ported all the SSE/MMX etc stuff to AltiVecIn case you're wondering: the upshot of this disclaimer is that thefunctionality is exactly as per the original but the software now runsaround 12 times as fast with sensible (search radius 15) motioncompensation settings, and you can now get half-decent looking resultswhen working with typical broadcast quality sources.FAST MOTION COMPENSATION:For MPEG to achieve good quality results it has to find a good matchbetween each 16*16 "macro-block" of pixels in the current frame anda 16*16 region in a preceding or following frame. The better the matchthe less information it has to drop when compressing. Obviously, findingsuch a good match in *moving* pictures requires a lot of searching.To do this (reasonably) quickly we use a trick: we first searchcoarsely comparing sums of 4*4 pixel groups and then 2*2 pixelgroups rather than individual pixels. Technically: we do an initialsearch using sub-sampled data and use this to choose the places tosearch for detailed single pixel and sub-pixel matches.This reduces the amount of search *dramatically*. Since a good exact matches (down to half-pixels) will also give a good2*2 match we know the best match should be in amoungst the best 2*2matches. Similarly for the 2*2 in the 4*4 matches. However, there maybe some false positives. So we can't just work with the very best 4*4and the best 2*2 near that. Instead we discard only those above somethreshold. Experiments and a bit of simple statistics revealed thatit was exceedingly rare to miss the best match doing this, and evenwhen we did miss the match found was *almost* as good. You can adjustthe threshold with the -4 and -2 flags.Between the settings-4 1 -2 1 (highest quality)-4 4 -2 4 (fastest)Quality is impacted by about 10% quantisation (thats around 0.2 of abit of precision in texture coefficients). Speed is impacted by afactor of 2. One older Pentium-II and P-III Katmai machines the speed factoris more like 3. For most purposes the default settings will be fine.However on P-III Katmai etc -4 2 -2 1 gives a good near-optimum qualitysetting with reasonably speed.For MPEG-2 encoding it is better to have the search radius set higherthan the default of 16. There's very little point going below a radiusof 16 as the speed gains are pretty limited.Obviously, your mileage may vary so you may want to fiddle with thesetting if you're getting rotten results. Contactandrew.stevens@planet-interkom.deif you think you have found a case where the fast compensation fails!In case you're wondering: the performance gain over brute force searchis something like a factor of 5.NOISE FILTERING and BIT DROPPINGMPEG compression assumes that the pictures in a video sequence areclosely correlated (once you've applied motion compensatino to searchfor good matches). Unfortunately, if you have a noisy original videosource this assumption isn't really true. The noise introduces lotsof spurious differences betwen actually very similar pictures. Theend result is ugly "artifacted" results where MPEG has dropped realinformation in favour of spurious noise. The solution is to try tofilter the noise out of the source. For mildy noisy stuff mpeg2enc's -N option (which slightly suppresseshigh-frequency information during compression) works well.Try this for stuff like captures from a good quality broadcastsource. However, if that isn't enough lav2yuv has a simple low-passfiltering options -n. -n 1 smooths gently and is mostly imperceptible,-n 2 is rather stronger and is noticeable if you compare source video.However, if you really have noisy sources then -n 1 or -n 2 isinfinitely preferable to the swimming sea of artefacts MPEG otherwisegenerates. If you need -n 2 and aren't in a hurry then the moresophisticated filtering in the yuv2dfilter program does a better job!What's really needed is dynamic noise-reduction in the MPEGencoder... but that's a job for another release!SMARTER BIT ALLOCATION (A.Stevens Aug and Nov 2000)The original source code made heavy use of the variance ofblocks as a measure of their information content. This is actuallypretty poor as what really counts is how many bits it will taketo encoded the quantised DCT of the block without excessive losses.The bit allocation strategy is also very naive, essentially theencoder (more or less) tries to keep the bits allocated to eachblock the same. This is bad news if you have a picture wherea lot of the information content comes low down....As a first step to better quality encoding the current source nowuses the sum of absolute block coefficients as a measure of informationcontent. Bits are then allocated on the basis of how much informationis being covered so that bits are held back if high informationmaterial comes late in the frame. As a second step some attempt is made toguess the "right" level of quantisation for a frame in advance so thatthe (inevitably lagging) feedback control of bit-allocation only deals withminor fine-tuning. This prevents annoying "oscillation" of bit-allocation".These modifications appear to give noticeable better results, greatlyreducing fringes around sharp contrasts (e.g. titles) whilst stillensuring no significant bit-rate overshoots.The encoder now also tries to predict the likely bit-demand for eachframe so it can set quantisation roughly right so that there isn't a"ramping" of quality down a frame with the top (too) good and thebottom (excessively) poor. There is also the -Q option for boostingthe bits allocated to very active blocks (ones where motioncompensation wasn't possible). The advantage is nice sharp logos andsuchlike. The drawback is that noisy material can become "swimmy".If you're getting swimminess try using -Q 1.0.VARIABLE BIT RATE ENCODINGThe encoder now supports a form of variable bit-rate encoding. Youcan specify a quality ceiling (for the technical: a quantisationfloor). In this case the bitrate is interpeted as a "do not exceedceiling". The encoder bumps quality up as high as it can withoutexceeding this rate and the specified quality ceiling. For thisoption to be usefully use the speed ceiling should be high enough thatit is rarely if ever reached and a large video buffer size (say 250 or400K specified). Sensible numbers for quality ceiling are between 4and 8. Below 4 and quality gains are probably no longer visible.Above 8 and artefacting may occur. An interesting effect of setting afixed quality rate is that encoding is actually marginally moreefficient as the encoder encodes *changes* in quality setting fromscreen block to block.. If they don't change they don't use any bits!PERFORMANCE: MMX/MMXEXT/SSE/3D-NOW (A.Stevens Jul/Aug 2000)In addition to improved fast motion compensation the updated code alsohas x86 assembly language routines for key calculations: 4*4. 8*8 and16*16 block absolute difference sums, interpolated and simpleprediction variances, prediction addition and subtraction, foward andinverse discrete cosine transforms, and quantisation and inversequantisation.Unfortunately, the original MMX (MMX1) instruction set on the PentiumMMX is somewhat deficient in a couple of key areas (it has obvious"gaps"). Subsequent CPU's all started to fill these gaps in. Thusthere is core MMX plus MMX extensions from one or all of: K6,Athlon/Duron, P-III and now even P-IV. Where relevant routines comein several versions to make the most of what is available. As anextra-special bonus for AMD owners some of the quantisationcalculations are done (much faster) with the 3D-Now! instructions.Similar stuff for P-III/P-IV SSE instructions is being written.There have also been some gross-me-out hacky optimisations madeto the non-MMX code to make it run better on modern pipelined CPU's.However, if you're trying to compress long MPEG sequences on a pre-MMXx86 CPU you *will* need some serious patience ;-). I reckon compressinga 1 hour TV show is now tolerable with the new super-duper fastmpeg2enc and a 300Mhz+ CPU on the job. Personally, I split thework between 4 CPU's: Celeron 366, 2*PIII-450 and a K6-2 500.Anyway to give you some idea what to expect here are some peformancefigures. They're for the normal default settings and -4 1 -2 1 which tries to squeeze the last few drops of quality out.Duron-700 : 19fps / 13fps PIII-450 (katmai) : 11 / 6fpsPII-400 : 8fps / 4fpsCeleron-366 : 7fps / 3fpsIt is interesting to note the how much less efficient the Intel CPU's are at the motion compensation searches controlled by the -4 and -2 flags. I can only assume an Athlon would absolutely *fly* at this workload.VARIABLE SIZE GOOP'sThe encoder can be told to dynamically vary the size of groups ofpictures to try to avoid scene changes in the middle of groups ofpictures. This can help produce better results in sudden cuts andaction scenes (especially in conjunction with VBR coding). However,the effect on "normal" video is marginal and for MPEG-1 at least manyhardware decoders react allergically to variable-size GOP's.STILL MORE PERFORMANCE?Buy a multi-processor Athlon or P-IV machine! The SMP support nowgives a speed up of around 1.9 on dual CPU machines. It should giveuseful speed scaling on 3 and 4 CPU machines too. Apart from this itis hard to see how big improvements can be made without radicalrebuilding of the code. The program now spends around 50% of its timein routines that are already aggressively implement in MMX (etc).Note: the Athlon CPU's are a *lot* faster at a couple of key MMX instructionsthan the original P-III Katmai's. BETTER QUALITY?- There is still a lot of room for improvements to bit-allocation.- It should also be possible to intelligently use switching ofquantisation matrices to suppress mosaicing in very active scenes. I.e.to "blur" rather than "mosaic".- Some minor gains could be made by intelligently selecting f-code's- There are easy gains to be made (for people intending to usesoftware decoders or suitable hardware) by gettting veriable-bit-rateencoding working once again. This is one of the next things on thelist and should be pretty easy.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?