| 1 |
mpeg2enc encodes MJPEG AVIs, Quicktime Files or Edit List Files |
| 2 |
to MPEG-1/2 video streams. |
| 3 |
|
| 4 |
DISCLAIMER: |
| 5 |
|
| 6 |
This software is from the MPEG Software Simulation Group. See notices |
| 7 |
in source code regarding Patents and miscellaneous IP issues. |
| 8 |
|
| 9 |
POST MPEG SSG Development |
| 10 |
|
| 11 |
Rainer Johanni changed the input routine so that "lavtools" Edit |
| 12 |
Lists, AVI's and Quicktime input is accepted, introduced command line |
| 13 |
params instead of a param file and made some simple optimizations. |
| 14 |
Rainer also did the initial mod to accept the new special yuv format. |
| 15 |
|
| 16 |
Andrew Stevens added more intelligent motion compensation and |
| 17 |
optimised some key calculations that run badly on modern highly |
| 18 |
pipelined machines. These were then further refined based on the |
| 19 |
results of some work by Juan Remero Arroyo (who would have guessed 4*4 |
| 20 |
blocks still produce useful results!). |
| 21 |
|
| 22 |
Chris Atenasio's contributed MMX2/SSE code for the motion compensation |
| 23 |
macroblock group difference calculation. |
| 24 |
|
| 25 |
Andrew Stevens added a lot more MMX1 code for motion compensation and |
| 26 |
adapted Chris' code for his intelligent motion compensation mods and |
| 27 |
ported the fdct/idct MMX code to nasm. |
| 28 |
|
| 29 |
Andrew Stevens added some simple pre-processing filters to improve |
| 30 |
output quality when using noisy (e.g. broadcast) source video. |
| 31 |
|
| 32 |
Andrew Stevens modified the bit-allocation routines to use a more |
| 33 |
accurate method of estimating bits needed for good quality results. |
| 34 |
|
| 35 |
Andrew Stevens fixed a number of overflow and calculation errors |
| 36 |
associated with large files. |
| 37 |
|
| 38 |
Andrew Stevens ported in and tidied up a number of MMX routines from |
| 39 |
the bbmpeg project for windows (see www.delphi.com/bbmpeg_ug) to |
| 40 |
accelerate a few "second line" but nonetheless significantly |
| 41 |
time-consuming functions. |
| 42 |
|
| 43 |
Andrew Stevens added a new much faster MMX / 3Dnow / ISSE based |
| 44 |
quantization routines. |
| 45 |
|
| 46 |
Andrew Stevens replaced the use of SAD with variance for selecting |
| 47 |
motion compensations. Slightly more expensive but produces *markedly* |
| 48 |
better results. Similarly added support for checking the |
| 49 |
quality of the chrominance match achieved. |
| 50 |
|
| 51 |
Andrew Stevens added limited SMP support and basic chrominance match |
| 52 |
checking in motion compensation. |
| 53 |
|
| 54 |
Andrew Stevens added support for variable-sized GOP's and sequence |
| 55 |
splitting at file-size boundaries. |
| 56 |
|
| 57 |
James Klikman ported all the SSE/MMX etc stuff to AltiVec |
| 58 |
|
| 59 |
In case you're wondering: the upshot of this disclaimer is that the |
| 60 |
functionality is exactly as per the original but the software now runs |
| 61 |
around 12 times as fast with sensible (search radius 15) motion |
| 62 |
compensation settings, and you can now get half-decent looking results |
| 63 |
when working with typical broadcast quality sources. |
| 64 |
|
| 65 |
|
| 66 |
FAST MOTION COMPENSATION: |
| 67 |
|
| 68 |
For MPEG to achieve good quality results it has to find a good match |
| 69 |
between each 16*16 "macro-block" of pixels in the current frame and |
| 70 |
a 16*16 region in a preceding or following frame. The better the match |
| 71 |
the less information it has to drop when compressing. Obviously, finding |
| 72 |
such a good match in *moving* pictures requires a lot of searching. |
| 73 |
|
| 74 |
To do this (reasonably) quickly we use a trick: we first search |
| 75 |
coarsely comparing sums of 4*4 pixel groups and then 2*2 pixel |
| 76 |
groups rather than individual pixels. Technically: we do an initial |
| 77 |
search using sub-sampled data and use this to choose the places to |
| 78 |
search for detailed single pixel and sub-pixel matches. |
| 79 |
This reduces the amount of search *dramatically*. |
| 80 |
|
| 81 |
Since a good exact matches (down to half-pixels) will also give a good |
| 82 |
2*2 match we know the best match should be in amoungst the best 2*2 |
| 83 |
matches. Similarly for the 2*2 in the 4*4 matches. However, there may |
| 84 |
be some false positives. So we can't just work with the very best 4*4 |
| 85 |
and the best 2*2 near that. Instead we discard only those above some |
| 86 |
threshold. Experiments and a bit of simple statistics revealed that |
| 87 |
it was exceedingly rare to miss the best match doing this, and even |
| 88 |
when we did miss the match found was *almost* as good. You can adjust |
| 89 |
the threshold with the -4 and -2 flags. |
| 90 |
|
| 91 |
Between the settings |
| 92 |
|
| 93 |
-4 1 -2 1 (highest quality) |
| 94 |
|
| 95 |
-4 4 -2 4 (fastest) |
| 96 |
|
| 97 |
Quality is impacted by about 10% quantisation (thats around 0.2 of a |
| 98 |
bit of precision in texture coefficients). Speed is impacted by a |
| 99 |
factor of 2. One older Pentium-II and P-III Katmai machines the speed factor |
| 100 |
is more like 3. For most purposes the default settings will be fine. |
| 101 |
However on P-III Katmai etc -4 2 -2 1 gives a good near-optimum quality |
| 102 |
setting with reasonably speed. |
| 103 |
For MPEG-2 encoding it is better to have the search radius set higher |
| 104 |
than the default of 16. There's very little point going below a radius |
| 105 |
of 16 as the speed gains are pretty limited. |
| 106 |
|
| 107 |
Obviously, your mileage may vary so you may want to fiddle with the |
| 108 |
setting if you're getting rotten results. Contact |
| 109 |
[email protected] |
| 110 |
if you think you have found a case where the fast compensation fails! |
| 111 |
|
| 112 |
In case you're wondering: the performance gain over brute force search |
| 113 |
is something like a factor of 5. |
| 114 |
|
| 115 |
NOISE FILTERING and BIT DROPPING |
| 116 |
|
| 117 |
MPEG compression assumes that the pictures in a video sequence are |
| 118 |
closely correlated (once you've applied motion compensatino to search |
| 119 |
for good matches). Unfortunately, if you have a noisy original video |
| 120 |
source this assumption isn't really true. The noise introduces lots |
| 121 |
of spurious differences betwen actually very similar pictures. The |
| 122 |
end result is ugly "artifacted" results where MPEG has dropped real |
| 123 |
information in favour of spurious noise. The solution is to try to |
| 124 |
filter the noise out of the source. |
| 125 |
|
| 126 |
For mildy noisy stuff mpeg2enc's -N option (which slightly suppresses |
| 127 |
high-frequency information during compression) works well. |
| 128 |
Try this for stuff like captures from a good quality broadcast |
| 129 |
source. However, if that isn't enough lav2yuv has a simple low-pass |
| 130 |
filtering options -n. -n 1 smooths gently and is mostly imperceptible, |
| 131 |
-n 2 is rather stronger and is noticeable if you compare source video. |
| 132 |
However, if you really have noisy sources then -n 1 or -n 2 is |
| 133 |
infinitely preferable to the swimming sea of artefacts MPEG otherwise |
| 134 |
generates. If you need -n 2 and aren't in a hurry then the more |
| 135 |
sophisticated filtering in the yuv2dfilter program does a better job! |
| 136 |
What's really needed is dynamic noise-reduction in the MPEG |
| 137 |
encoder... but that's a job for another release! |
| 138 |
|
| 139 |
SMARTER BIT ALLOCATION (A.Stevens Aug and Nov 2000) |
| 140 |
|
| 141 |
The original source code made heavy use of the variance of |
| 142 |
blocks as a measure of their information content. This is actually |
| 143 |
pretty poor as what really counts is how many bits it will take |
| 144 |
to encoded the quantised DCT of the block without excessive losses. |
| 145 |
The bit allocation strategy is also very naive, essentially the |
| 146 |
encoder (more or less) tries to keep the bits allocated to each |
| 147 |
block the same. This is bad news if you have a picture where |
| 148 |
a lot of the information content comes low down.... |
| 149 |
|
| 150 |
As a first step to better quality encoding the current source now |
| 151 |
uses the sum of absolute block coefficients as a measure of information |
| 152 |
content. Bits are then allocated on the basis of how much information |
| 153 |
is being covered so that bits are held back if high information |
| 154 |
material comes late in the frame. As a second step some attempt is made to |
| 155 |
guess the "right" level of quantisation for a frame in advance so that |
| 156 |
the (inevitably lagging) feedback control of bit-allocation only deals with |
| 157 |
minor fine-tuning. This prevents annoying "oscillation" of bit-allocation". |
| 158 |
|
| 159 |
These modifications appear to give noticeable better results, greatly |
| 160 |
reducing fringes around sharp contrasts (e.g. titles) whilst still |
| 161 |
ensuring no significant bit-rate overshoots. |
| 162 |
|
| 163 |
The encoder now also tries to predict the likely bit-demand for each |
| 164 |
frame so it can set quantisation roughly right so that there isn't a |
| 165 |
"ramping" of quality down a frame with the top (too) good and the |
| 166 |
bottom (excessively) poor. There is also the -Q option for boosting |
| 167 |
the bits allocated to very active blocks (ones where motion |
| 168 |
compensation wasn't possible). The advantage is nice sharp logos and |
| 169 |
suchlike. The drawback is that noisy material can become "swimmy". |
| 170 |
If you're getting swimminess try using -Q 1.0. |
| 171 |
|
| 172 |
|
| 173 |
VARIABLE BIT RATE ENCODING |
| 174 |
|
| 175 |
The encoder now supports a form of variable bit-rate encoding. You |
| 176 |
can specify a quality ceiling (for the technical: a quantisation |
| 177 |
floor). In this case the bitrate is interpeted as a "do not exceed |
| 178 |
ceiling". The encoder bumps quality up as high as it can without |
| 179 |
exceeding this rate and the specified quality ceiling. For this |
| 180 |
option to be usefully use the speed ceiling should be high enough that |
| 181 |
it is rarely if ever reached and a large video buffer size (say 250 or |
| 182 |
400K specified). Sensible numbers for quality ceiling are between 4 |
| 183 |
and 8. Below 4 and quality gains are probably no longer visible. |
| 184 |
Above 8 and artefacting may occur. An interesting effect of setting a |
| 185 |
fixed quality rate is that encoding is actually marginally more |
| 186 |
efficient as the encoder encodes *changes* in quality setting from |
| 187 |
screen block to block.. If they don't change they don't use any bits! |
| 188 |
|
| 189 |
PERFORMANCE: MMX/MMXEXT/SSE/3D-NOW (A.Stevens Jul/Aug 2000) |
| 190 |
|
| 191 |
In addition to improved fast motion compensation the updated code also |
| 192 |
has x86 assembly language routines for key calculations: 4*4. 8*8 and |
| 193 |
16*16 block absolute difference sums, interpolated and simple |
| 194 |
prediction variances, prediction addition and subtraction, foward and |
| 195 |
inverse discrete cosine transforms, and quantisation and inverse |
| 196 |
quantisation. |
| 197 |
|
| 198 |
Unfortunately, the original MMX (MMX1) instruction set on the Pentium |
| 199 |
MMX is somewhat deficient in a couple of key areas (it has obvious |
| 200 |
"gaps"). Subsequent CPU's all started to fill these gaps in. Thus |
| 201 |
there is core MMX plus MMX extensions from one or all of: K6, |
| 202 |
Athlon/Duron, P-III and now even P-IV. Where relevant routines come |
| 203 |
in several versions to make the most of what is available. As an |
| 204 |
extra-special bonus for AMD owners some of the quantisation |
| 205 |
calculations are done (much faster) with the 3D-Now! instructions. |
| 206 |
Similar stuff for P-III/P-IV SSE instructions is being written. |
| 207 |
|
| 208 |
There have also been some gross-me-out hacky optimisations made |
| 209 |
to the non-MMX code to make it run better on modern pipelined CPU's. |
| 210 |
However, if you're trying to compress long MPEG sequences on a pre-MMX |
| 211 |
x86 CPU you *will* need some serious patience ;-). I reckon compressing |
| 212 |
a 1 hour TV show is now tolerable with the new super-duper fast |
| 213 |
mpeg2enc and a 300Mhz+ CPU on the job. Personally, I split the |
| 214 |
work between 4 CPU's: Celeron 366, 2*PIII-450 and a K6-2 500. |
| 215 |
|
| 216 |
Anyway to give you some idea what to expect here are some peformance |
| 217 |
figures. They're for the normal default settings and -4 1 -2 1 which |
| 218 |
tries to squeeze the last few drops of quality out. |
| 219 |
|
| 220 |
Duron-700 : 19fps / 13fps |
| 221 |
PIII-450 (katmai) : 11 / 6fps |
| 222 |
PII-400 : 8fps / 4fps |
| 223 |
Celeron-366 : 7fps / 3fps |
| 224 |
|
| 225 |
It is interesting to note the how much less efficient the Intel CPU's are at |
| 226 |
the motion compensation searches controlled by the -4 and -2 flags. I can |
| 227 |
only assume an Athlon would absolutely *fly* at this workload. |
| 228 |
|
| 229 |
VARIABLE SIZE GOPs |
| 230 |
|
| 231 |
The encoder can be told to dynamically vary the size of groups of |
| 232 |
pictures to try to avoid scene changes in the middle of groups of |
| 233 |
pictures. This can help produce better results in sudden cuts and |
| 234 |
action scenes (especially in conjunction with VBR coding). However, |
| 235 |
the effect on "normal" video is marginal and for MPEG-1 at least many |
| 236 |
hardware decoders react allergically to variable-size GOP's. |
| 237 |
|
| 238 |
STILL MORE PERFORMANCE? |
| 239 |
|
| 240 |
Buy a multi-processor Athlon or P-IV machine! The SMP support now |
| 241 |
gives a speed up of around 1.9 on dual CPU machines. It should give |
| 242 |
useful speed scaling on 3 and 4 CPU machines too. Apart from this it |
| 243 |
is hard to see how big improvements can be made without radical |
| 244 |
rebuilding of the code. The program now spends around 50% of its time |
| 245 |
in routines that are already aggressively implement in MMX (etc). |
| 246 |
|
| 247 |
Note: the Athlon CPU's are a *lot* faster at a couple of key MMX instructions |
| 248 |
than the original P-III Katmai's. |
| 249 |
|
| 250 |
BETTER QUALITY? |
| 251 |
|
| 252 |
- There is still a lot of room for improvements to bit-allocation. |