| 1 |
<html> |
| 2 |
<head> |
| 3 |
<title>tooLAME: MPEG Audio Layer II VBR</title> |
| 4 |
<style> |
| 5 |
<!-- BODY { BACKGROUND: #FFFFFF; COLOR: #000000; FONT-SIZE: 10pt; FONT-FAMILY: verdana, sans-serif } |
| 6 |
A { COLOR: #111177; TEXT-DECORATION: none } |
| 7 |
TD { font-size: medium; font-weight:normal } |
| 8 |
--!> |
| 9 |
</STYLE> |
| 10 |
</head> |
| 11 |
<body> |
| 12 |
|
| 13 |
<table border = 0 width="75%" align="center"><tr><td> |
| 14 |
<h1> tooLAME: MPEG Audio Layer II VBR </h1> |
| 15 |
|
| 16 |
<h2>Contents</h2> |
| 17 |
<Ul> |
| 18 |
<LI>Introduction |
| 19 |
<LI>Usage |
| 20 |
<LI>Bitrate Ranges for various Sampling frequencies |
| 21 |
<LI>Why can't the bitrate vary from 32kbps to 384kbps for every file? |
| 22 |
<UL> |
| 23 |
<LI>Short Answer |
| 24 |
<LI>Long Answer |
| 25 |
</UL> |
| 26 |
<LI> Tech Stuff |
| 27 |
</UL> |
| 28 |
|
| 29 |
<h2>Introduction</h2> |
| 30 |
VBR mode works by selecting a different bitrate for each frame. Frames |
| 31 |
which are harder to encode will be allocated more bits i.e. a higher bitrate.</p> |
| 32 |
|
| 33 |
LayerII VBR is a complete hack - the ISO standard actually says that decoders are not |
| 34 |
required to support it. As a hack, its implementation is a pain to try and understand. |
| 35 |
If you're mega-keen to get full range VBR working, either (a) send me money (b) grab the |
| 36 |
ISO standard and a C compiler and email me.</p> |
| 37 |
|
| 38 |
<h2>Usage</h2> |
| 39 |
<pre> |
| 40 |
toolame -v [level] inputfile outputfile. |
| 41 |
</pre> |
| 42 |
A level of 5 works very well for me.</p> |
| 43 |
|
| 44 |
The level value can is a measurement of quality - the higher |
| 45 |
the level the higher the average bitrate of the resultant file. |
| 46 |
[See TECH STUFF for a better explanation of what the value does]</p> |
| 47 |
|
| 48 |
The confusing part of my implementation of LayerII VBR is that it's different from MP3 VBR. |
| 49 |
<UL> |
| 50 |
<LI>The range of bitrates used is controlled by the input sampling frequency. (See below "Bitrate ranges") |
| 51 |
<LI>The tendency to use higher bitrates is governed by the <level>. |
| 52 |
</ul> |
| 53 |
|
| 54 |
E.g. Say you have a 44.1kHz Stereo file. In VBR mode, the bitrate can range from 192 to 384 kbps. |
| 55 |
Using "-v -5" will force the encoder to favour the lower bitrate. |
| 56 |
Using "-v 5" will force the encoder to favour the upper bitrate. |
| 57 |
The value can actually be *any* int. -27, 233, 47. The larger the number, the greater |
| 58 |
the bitrate bias.</p> |
| 59 |
|
| 60 |
<h2>Bitrate Ranges</h2> |
| 61 |
|
| 62 |
When making a VBR stream, the bitrate is only allowed to vary within |
| 63 |
set limits</p> |
| 64 |
|
| 65 |
<pre> |
| 66 |
48kHz |
| 67 |
Stereo: 112-384kbps Mono: 56-192kbps |
| 68 |
|
| 69 |
44.1kHz & 32kHz |
| 70 |
Stereo: 192-384kbps Mono: 96-192kbps |
| 71 |
|
| 72 |
24kHz, 22.05kHz & 16kHz |
| 73 |
Stereo/Mono: 8-160kbps |
| 74 |
</pre> |
| 75 |
|
| 76 |
<h2>Why doesn't the VBR mode work the same as MP3VBR? The Short Answer</h2> |
| 77 |
<b>Why can't the bitrate vary from 32kbps to 384kbps for every file?</b></p> |
| 78 |
According to the standard (ISO/IEC 11172-3:1993) Section 2.4.2.3 |
| 79 |
<pre> |
| 80 |
"In order to provide the smallest possible delay and complexity, the |
| 81 |
decoder is not required to support a continuously variable bitrate when |
| 82 |
in layer I or II. Layer III supports variable bitrate by switching the |
| 83 |
bitrate index." |
| 84 |
|
| 85 |
and |
| 86 |
|
| 87 |
"For Layer II, not all combinations of total bitrate and mode are allowed." |
| 88 |
</pre> |
| 89 |
|
| 90 |
Hence, most LayerII coders would not have been written with VBR in mind, and |
| 91 |
LayerII VBR is a hack. It works for limited cases. Getting it to work to |
| 92 |
the same extent as MP3-style VBR will be a major hack.</p> |
| 93 |
|
| 94 |
(If you *really* want better bitrate ranges, read "The Long Answer" and submit your mega-patch.)</p> |
| 95 |
|
| 96 |
<h2>Why doesn't the VBR mode work the same as MP3VBR? The Long Answer</h2> |
| 97 |
<b>Why can't the bitrate vary from 32kbps to 384kbps for every file?</b> |
| 98 |
|
| 99 |
<h3>Reason 1: The standard limits the range</h3> |
| 100 |
|
| 101 |
As quoted above from the standard for 48/44.1/32kHz: |
| 102 |
<pre> |
| 103 |
"For Layer II, not all combinations of total bitrate and mode are allowed. See |
| 104 |
the following table." |
| 105 |
|
| 106 |
Bitrate Allowed Modes |
| 107 |
(kbps) |
| 108 |
32 mono only |
| 109 |
48 mono only |
| 110 |
56 mono only |
| 111 |
64 all modes |
| 112 |
80 mono only |
| 113 |
96 all modes |
| 114 |
112 all modes |
| 115 |
128 all modes |
| 116 |
160 all modes |
| 117 |
192 all modes |
| 118 |
224 stereo only |
| 119 |
256 stereo only |
| 120 |
320 stereo only |
| 121 |
384 stereo only |
| 122 |
</pre> |
| 123 |
|
| 124 |
So based upon this table alone, you *could* have VBR stereo encoding which varies |
| 125 |
smoothly from 96 to 384kbps. Or you could have have VBR mono encoding which varies from |
| 126 |
32 to 192kbps. But since the top and bottom bitrates don't apply to all modes, it would |
| 127 |
be impossible to have a stereo file encoded from 32 to 384 kbps.</p> |
| 128 |
|
| 129 |
But this isn't what is really limiting the allowable bitrate range - the bit allocation |
| 130 |
tables are the major hurdle.</p> |
| 131 |
|
| 132 |
<h3>Reason 2: The bit allocation tables don't allow it</h3> |
| 133 |
|
| 134 |
From the standard, Section 2.4.3.3.1 "Bit allocation decoding"</p> |
| 135 |
<pre> |
| 136 |
"For different combinations of bitrate and sampling frequency, different bit |
| 137 |
allocation tables exist. |
| 138 |
</pre> |
| 139 |
These bit allocation tables are pre-determined tables (in Annex B of the standard) which |
| 140 |
indicate |
| 141 |
<UL> |
| 142 |
<LI>how many bits to read for the initial data (2,3 or 4) |
| 143 |
<LI>these bits are then used as an index back into the table to |
| 144 |
find the number of quantize levels for the samples in this subband |
| 145 |
</ul> |
| 146 |
But the table used (and hence the number of bits and the calculated index) are different |
| 147 |
for different combinations of bitrate and sampling frequency.</p> |
| 148 |
|
| 149 |
I will use TableB.2a as an example.</p> |
| 150 |
|
| 151 |
Table B.2a Applies for the following combinations. |
| 152 |
<pre> |
| 153 |
Sampling Freq Bitrates in (kbps/channel) [emphasis: this is a PER CHANNEL bitrate] |
| 154 |
48 56, 64, 80, 96, 112, 128, 160, 192 |
| 155 |
44.1 56, 64, 80 |
| 156 |
32 56, 64, 80 |
| 157 |
</pre> |
| 158 |
If we have a STEREO 48kHz input file, and we use this table, then the bitrates |
| 159 |
we could calculate from this would be 112, 128, 160, 192, 224, 256, 320 and 384 kbps.</p> |
| 160 |
|
| 161 |
This table contains no information on how to encode stuff at bitrates less than 112kbps |
| 162 |
(for a stereo file). You would have to load allocation table B.2c to encode stereo at |
| 163 |
64kbps and 128kbps.</p> |
| 164 |
|
| 165 |
Since it would be a MAJOR piece of hacking to get the different tables shifted in and out |
| 166 |
during the encoding process, once an allocation table is loaded *IT IS NOT CHANGED*.</p> |
| 167 |
|
| 168 |
Hence, the best table is picked at the start of the encoding process, and the encoder |
| 169 |
is stuck with it for the rest of the encode. </p> |
| 170 |
|
| 171 |
For toolame-02j, I have picked the table it loads for different |
| 172 |
sampling frequencies in order to optimize the range of bitrates possible. |
| 173 |
<pre> |
| 174 |
48 kHz - Table B.2a |
| 175 |
Stereo Bitrate Range: 112 - 384 |
| 176 |
Mono Bitrate Range : 56 - 192 |
| 177 |
|
| 178 |
44.1/32 kHz - Table B.2b |
| 179 |
Stereo Bitrate Range: 192 - 384 |
| 180 |
Mono Bitrate Range: 96 - 192 |
| 181 |
|
| 182 |
24/22.05/16 kHz - LSF Table (Standard ISO/IEC 13818.3:1995 Annex B, Table B.1) |
| 183 |
There is only 1 table for the Lower Sampling Frequencies |
| 184 |
All modes (mono and stereo) are allowable at all bitrates |
| 185 |
So at the Lower Sampling Frequencies you *can* have a completely variable |
| 186 |
bitrate over the entire range. |
| 187 |
</pre> |
| 188 |
<h2>Tech Stuff</h2> |
| 189 |
|
| 190 |
The VBR mode is mainly centered around the main_bit_allocation() and |
| 191 |
a_bit_allocation() routines in encode.c.</p> |
| 192 |
|
| 193 |
The limited range of VBR is due to my particular implementation which restricts |
| 194 |
ranges to within one alloc table (see tables B.2a, B.2b, B.2c and B.2d in ISO 11172). |
| 195 |
The VBR range for 32/44.1khz lies within B.2b, and the 48khz VBR lies within table B.2a.</p> |
| 196 |
|
| 197 |
I'm not sure whether it is worth extending these ranges down to lower bitrates. |
| 198 |
The work required to switch alloc tables *during* the encoding is major.</p> |
| 199 |
|
| 200 |
In the case of silence, it might be worth doing a quick check for very low signals |
| 201 |
and writing a pre-calculated *blank* 32kpbs frame. [probably also a lot of work].</p> |
| 202 |
|
| 203 |
<h3>How CBR works</h3> |
| 204 |
<UL> |
| 205 |
<LI> Use the psycho model to determine the MNRs for each subband |
| 206 |
[MNR = the ratio of "masking" to "noise"] |
| 207 |
(From an encoding perspective, a bigger MNR in a subband means that |
| 208 |
it sounds better since the noise is more masked)) |
| 209 |
<LI> calculate the available data bits (adb) for this bitrate. |
| 210 |
<LI> Based upon the MNR (Masking:Noise Ratio) values, allocate bits to each |
| 211 |
subband |
| 212 |
<LI> Keep increasing the bits to whichever subband currently has the min MNR |
| 213 |
value until we have no bits left. |
| 214 |
<LI> This mode does not guarentee that all the subbands are without noise |
| 215 |
ie there may still be subbands with MNR less than 0.0 (noisy!) |
| 216 |
</ul> |
| 217 |
|
| 218 |
<h3>How VBR works</h3> |
| 219 |
<UL> |
| 220 |
<LI> pretend we have lots of bits to spare, and work out the bits which would |
| 221 |
raise the MNR in each subband to the level given by the argument on the |
| 222 |
command line "-v [int]" |
| 223 |
<LI> Pick the bitrate which has more bits than the required_bits we just calculated |
| 224 |
<LI> calculate a_bit_allocation() |
| 225 |
<LI> VBR "guarantees" that all subbands have MNR > VBRLEVEL or that we have |
| 226 |
reached the maximum bitrate. |
| 227 |
</ul> |
| 228 |
|
| 229 |
<h2>FUTURE</h2> |
| 230 |
<UL> |
| 231 |
<LI> with this VBR mode, we know the bits aren't going to run out, so we can |
| 232 |
just assign them "greedily". |
| 233 |
<LI> VBR_a_bit_allocation() is yet to be written :) |
| 234 |
</ul> |
| 235 |
|
| 236 |
|
| 237 |
</tr></td> |
| 238 |
</body> |
| 239 |
</html> |