| 1 |
SOME NOTES IN AND AROUND MPEG ENCODING |
| 2 |
|
| 3 |
(1) The importance of precision... |
| 4 |
|
| 5 |
Its not just quantizing "about right" that matters. |
| 6 |
|
| 7 |
However, mp1e has a very very good idea: why not precompile a |
| 8 |
combined quantisation / fdct matrix with the multipliers set up |
| 9 |
"just right" to include the necessary frig factors. If nothing |
| 10 |
else it saves accumulating numeric errors in the intermediate |
| 11 |
results of the coefficients! |
| 12 |
|
| 13 |
That mp1e guy was *smart*... |
| 14 |
|
| 15 |
(2) |
| 16 |
|
| 17 |
If you want to accurately compute an integer division using |
| 18 |
multiplication... |
| 19 |
|
| 20 |
|
| 21 |
Notation: [x](k) means x floor-ed to nearest k. [x] is [x] to the nearest |
| 22 |
unit. |
| 23 |
|
| 24 |
|
| 25 |
[x / d](1) = [Kx/d](K) = [ [K/d]x +(K%d)x/d ](K) / K |
| 26 |
|
| 27 |
Observe that for large K >> d [K/d]x >> (K%d)x/d |
| 28 |
so that the (K%d)x/d is in effect a rounding correction |
| 29 |
and hence need not itself be desperately accurate. Thus we |
| 30 |
expand another step to obtain... |
| 31 |
|
| 32 |
= [[K/d]x + [(K%d)[K/d]x + d](K)/K ](K) /K |
| 33 |
|
| 34 |
Where d is a very small correction factor. We now observe that since |
| 35 |
we're flooring to K on the outside we're really only interested in |
| 36 |
whether the rounding factor generates a "carry" so it doesn't need to |
| 37 |
be 100% accurate. |
| 38 |
|
| 39 |
= [[K/d]x ](K)/K + [[K/d]x]%K + (K%d)[K/d]x + d](K)/K |
| 40 |
|
| 41 |
For K twice word size the first term is simply the high word of the |
| 42 |
product [K/d]x |
| 43 |
|
| 44 |
The second term is simply the low word of the product [K/d]x plus the |
| 45 |
term (K%d)[K/d]x + d which for large K and small d will fit into a |
| 46 |
single word. Thus the carry can at most be a single bit. |
| 47 |
|
| 48 |
We can compute it "exactly" adding a small fixed round up d (32 |
| 49 |
appears to work nicely for K=2^16) and d < 256). However, for MPEG |
| 50 |
applications we have fairly limited range of d and x. For these we |
| 51 |
can get "almost" right results with a (coarse approximation). We know |
| 52 |
0 <= K%d < d. We also know the factor gets very small for larger d. |
| 53 |
Thus we really only need a correction that "works" for small d. |
| 54 |
Taking a wild guess to try x ~= (K%d)[K/d]x for smallish d. |
| 55 |
|
| 56 |
|
| 57 |
Miracle of miracles it works just great! No too-lows, and only very |
| 58 |
rare too highs for large x. |
| 59 |
|
| 60 |
Thus [x/d] ~= hiword([K/d]x) + (loword([K/d]x)+x) >= K ) |
| 61 |
|
| 62 |
Clever eh. The actual computation we quantize in MPEG is |
| 63 |
|
| 64 |
[coefficient[i]*32 + quantisation/2) / quantisation*2*coeffquant[i] ] |
| 65 |
|
| 66 |
So, combining with Mr mp1e's sneakiness what we need is a table of |
| 67 |
[K/quantisation*2*coeffquant[i]] for each quantisation. Ooooerrr... |
| 68 |
|
| 69 |
If we want to get *really* clever we combine the quantisation with the |
| 70 |
DCT coefficients to go yet faster.... |
| 71 |
|
| 72 |
|
| 73 |
(3) Predicting motion vectors.... |
| 74 |
|
| 75 |
|
| 76 |
You'd think you could use the motion vectors from the P frame as an |
| 77 |
upper bound for the those needed in the following (in encoding order) |
| 78 |
B-frames. Wrrrrrooooong. Doesn't work. Well, actually it works, but: |
| 79 |
|
| 80 |
(a) The gains ain't impressive (10% maybe). |
| 81 |
(b) The is some quality loss. |
| 82 |
(c) It is well night impossible to get *any* gains in the MPEG-2 |
| 83 |
interlaced (field MC case) which is where you'd really want the speed gains. |
| 84 |
|
| 85 |
(4) The wonderful DCT quantisation matrix |
| 86 |
|
| 87 |
Experiments have revealed that if you're compressing on the margins of |
| 88 |
acceptable quality you do *far* better if you squeeze the higher |
| 89 |
frequency DCT coefficients using the quantisation matrix rather than |
| 90 |
simply allow the encoder to jack up the global quantisation factor. |
| 91 |
|
| 92 |
Why? The latter starts run an unacceptable risk of producing |
| 93 |
noticeable mosaic-ing due to big DC offssets. The latter just |
| 94 |
starts blurring the results by killing off h.f. components. |
| 95 |
|
| 96 |
|
| 97 |
(5) Spares and Sub-sampled matching. |
| 98 |
|
| 99 |
You get *good* results searching for sub-sampled matches at |
| 100 |
sub-sampling resolution. |
| 101 |
|
| 102 |
However, if you go below the sampling resolution you *will* miss |
| 103 |
the odd good match. |
| 104 |
|
| 105 |
Furthermore, the sad of sub-sampled match leading (eventually) to |
| 106 |
an optimal 1-pel match can be surprisingly much higher than you'd |
| 107 |
expect. 6*sad scaled for sampling density can occasionally be |
| 108 |
observed. I was amazed... |
| 109 |
|
| 110 |
Andrew |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|