| 1 |
<?xml version="1.0" encoding="UTF-8"?> |
| 2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" |
| 3 |
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> |
| 4 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> |
| 5 |
<head> |
| 6 |
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" /> |
| 7 |
<meta name="generator" content="AsciiDoc 8.6.10" /> |
| 8 |
<title>TwoLAME: MPEG Audio Layer II VBR</title> |
| 9 |
<link rel="stylesheet" href="./asciidoc.css" type="text/css" /> |
| 10 |
|
| 11 |
|
| 12 |
<link rel="stylesheet" href="./twolame.css" type="text/css" /> |
| 13 |
<script type="text/javascript" src="./asciidoc.js"></script> |
| 14 |
<script type="text/javascript"> |
| 15 |
/*<![CDATA[*/ |
| 16 |
asciidoc.install(); |
| 17 |
/*]]>*/ |
| 18 |
</script> |
| 19 |
</head> |
| 20 |
<body class="article"> |
| 21 |
<div id="header"> |
| 22 |
<h1>TwoLAME: MPEG Audio Layer II VBR</h1> |
| 23 |
<span id="revnumber">version 0.4.0</span> |
| 24 |
</div> |
| 25 |
<div id="content"> |
| 26 |
<div class="sect1"> |
| 27 |
<h2 id="_contents">Contents</h2> |
| 28 |
<div class="sectionbody"> |
| 29 |
<div class="ulist"><ul> |
| 30 |
<li> |
| 31 |
<p> |
| 32 |
Introduction |
| 33 |
</p> |
| 34 |
</li> |
| 35 |
<li> |
| 36 |
<p> |
| 37 |
Usage |
| 38 |
</p> |
| 39 |
</li> |
| 40 |
<li> |
| 41 |
<p> |
| 42 |
Bitrate Ranges for various Sampling frequencies |
| 43 |
</p> |
| 44 |
</li> |
| 45 |
<li> |
| 46 |
<p> |
| 47 |
Why can’t the bitrate vary from 32kbps to 384kbps for every file? |
| 48 |
</p> |
| 49 |
</li> |
| 50 |
<li> |
| 51 |
<p> |
| 52 |
Short Answer |
| 53 |
</p> |
| 54 |
</li> |
| 55 |
<li> |
| 56 |
<p> |
| 57 |
Long Answer |
| 58 |
</p> |
| 59 |
</li> |
| 60 |
<li> |
| 61 |
<p> |
| 62 |
Tech Stuff |
| 63 |
</p> |
| 64 |
</li> |
| 65 |
</ul></div> |
| 66 |
</div> |
| 67 |
</div> |
| 68 |
<div class="sect1"> |
| 69 |
<h2 id="_introduction">Introduction</h2> |
| 70 |
<div class="sectionbody"> |
| 71 |
<div class="paragraph"><p>VBR mode works by selecting a different bitrate for each frame. Frames |
| 72 |
which are harder to encode will be allocated more bits i.e. a higher bitrate.</p></div> |
| 73 |
<div class="paragraph"><p>LayerII VBR is a complete hack - the ISO standard actually says that decoders are not |
| 74 |
required to support it. As a hack, its implementation is a pain to try and understand. |
| 75 |
If you’re mega-keen to get full range VBR working, either (a) send me money (b) grab the |
| 76 |
ISO standard and a C compiler and email me.</p></div> |
| 77 |
</div> |
| 78 |
</div> |
| 79 |
<div class="sect1"> |
| 80 |
<h2 id="_usage">Usage</h2> |
| 81 |
<div class="sectionbody"> |
| 82 |
<div class="literalblock"> |
| 83 |
<div class="content"> |
| 84 |
<pre><code>twolame -v [level] inputfile outputfile.</code></pre> |
| 85 |
</div></div> |
| 86 |
<div class="paragraph"><p>A level of 5 works very well for me.</p></div> |
| 87 |
<div class="paragraph"><p>The level value can is a measurement of quality - the higher |
| 88 |
the level the higher the average bitrate of the resultant file. |
| 89 |
See TECH STUFF for a better explanation of what the value does.</p></div> |
| 90 |
<div class="paragraph"><p>The confusing part of my implementation of LayerII VBR is that it’s different from MP3 VBR.</p></div> |
| 91 |
<div class="ulist"><ul> |
| 92 |
<li> |
| 93 |
<p> |
| 94 |
The range of bitrates used is controlled by the input sampling frequency. (See below "Bitrate ranges") |
| 95 |
</p> |
| 96 |
</li> |
| 97 |
<li> |
| 98 |
<p> |
| 99 |
The tendency to use higher bitrates is governed by the <level>. |
| 100 |
</p> |
| 101 |
</li> |
| 102 |
</ul></div> |
| 103 |
<div class="paragraph"><p>E.g. Say you have a 44.1kHz Stereo file. In VBR mode, the bitrate can range from 192 to 384 kbps.</p></div> |
| 104 |
<div class="paragraph"><p>Using "-v -5" will force the encoder to favour the lower bitrate.</p></div> |
| 105 |
<div class="paragraph"><p>Using "-v 5" will force the encoder to favour the upper bitrate.</p></div> |
| 106 |
<div class="paragraph"><p>The value can actually be <strong>any</strong> int. -27, 233, 47. The larger the number, the greater |
| 107 |
the bitrate bias.</p></div> |
| 108 |
</div> |
| 109 |
</div> |
| 110 |
<div class="sect1"> |
| 111 |
<h2 id="_bitrate_ranges">Bitrate Ranges</h2> |
| 112 |
<div class="sectionbody"> |
| 113 |
<div class="paragraph"><p>When making a VBR stream, the bitrate is only allowed to vary within |
| 114 |
set limits</p></div> |
| 115 |
<div class="literalblock"> |
| 116 |
<div class="content"> |
| 117 |
<pre><code>48kHz |
| 118 |
Stereo: 112-384kbps Mono: 56-192kbps</code></pre> |
| 119 |
</div></div> |
| 120 |
<div class="literalblock"> |
| 121 |
<div class="content"> |
| 122 |
<pre><code>44.1kHz & 32kHz |
| 123 |
Stereo: 192-384kbps Mono: 96-192kbps</code></pre> |
| 124 |
</div></div> |
| 125 |
<div class="literalblock"> |
| 126 |
<div class="content"> |
| 127 |
<pre><code>24kHz, 22.05kHz & 16kHz |
| 128 |
Stereo/Mono: 8-160kbps</code></pre> |
| 129 |
</div></div> |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
<div class="sect1"> |
| 133 |
<h2 id="_why_doesn_8217_t_the_vbr_mode_work_the_same_as_mp3vbr_the_short_answer">Why doesn’t the VBR mode work the same as MP3VBR? The Short Answer</h2> |
| 134 |
<div class="sectionbody"> |
| 135 |
<div class="paragraph"><p><strong>Why can’t the bitrate vary from 32kbps to 384kbps for every file?</strong></p></div> |
| 136 |
<div class="paragraph"><p>According to the standard (ISO/IEC 11172-3:1993) Section 2.4.2.3</p></div> |
| 137 |
<div class="literalblock"> |
| 138 |
<div class="content"> |
| 139 |
<pre><code>"In order to provide the smallest possible delay and complexity, the |
| 140 |
decoder is not required to support a continuously variable bitrate when |
| 141 |
in layer I or II. Layer III supports variable bitrate by switching the |
| 142 |
bitrate index."</code></pre> |
| 143 |
</div></div> |
| 144 |
<div class="literalblock"> |
| 145 |
<div class="content"> |
| 146 |
<pre><code>and</code></pre> |
| 147 |
</div></div> |
| 148 |
<div class="literalblock"> |
| 149 |
<div class="content"> |
| 150 |
<pre><code>"For Layer II, not all combinations of total bitrate and mode are allowed."</code></pre> |
| 151 |
</div></div> |
| 152 |
<div class="paragraph"><p>Hence, most LayerII coders would not have been written with VBR in mind, and |
| 153 |
LayerII VBR is a hack. It works for limited cases. Getting it to work to |
| 154 |
the same extent as MP3-style VBR will be a major hack.</p></div> |
| 155 |
<div class="paragraph"><p>(If you <strong>really</strong> want better bitrate ranges, read "The Long Answer" and submit your mega-patch.)</p></div> |
| 156 |
</div> |
| 157 |
</div> |
| 158 |
<div class="sect1"> |
| 159 |
<h2 id="_why_doesn_8217_t_the_vbr_mode_work_the_same_as_mp3vbr_the_long_answer">Why doesn’t the VBR mode work the same as MP3VBR? The Long Answer</h2> |
| 160 |
<div class="sectionbody"> |
| 161 |
<div class="paragraph"><p><strong>Why can’t the bitrate vary from 32kbps to 384kbps for every file?</strong></p></div> |
| 162 |
<div class="sect2"> |
| 163 |
<h3 id="_reason_1_the_standard_limits_the_range">Reason 1: The standard limits the range</h3> |
| 164 |
<div class="paragraph"><p>As quoted above from the standard for 48/44.1/32kHz:</p></div> |
| 165 |
<div class="literalblock"> |
| 166 |
<div class="content"> |
| 167 |
<pre><code>"For Layer II, not all combinations of total bitrate and mode are allowed. See |
| 168 |
the following table."</code></pre> |
| 169 |
</div></div> |
| 170 |
<div class="literalblock"> |
| 171 |
<div class="content"> |
| 172 |
<pre><code>Bitrate Allowed Modes |
| 173 |
(kbps) |
| 174 |
32 mono only |
| 175 |
48 mono only |
| 176 |
56 mono only |
| 177 |
64 all modes |
| 178 |
80 mono only |
| 179 |
96 all modes |
| 180 |
112 all modes |
| 181 |
128 all modes |
| 182 |
160 all modes |
| 183 |
192 all modes |
| 184 |
224 stereo only |
| 185 |
256 stereo only |
| 186 |
320 stereo only |
| 187 |
384 stereo only</code></pre> |
| 188 |
</div></div> |
| 189 |
<div class="paragraph"><p>So based upon this table alone, you <strong>could</strong> have VBR stereo encoding which varies |
| 190 |
smoothly from 96 to 384kbps. Or you could have have VBR mono encoding which varies from |
| 191 |
32 to 192kbps. But since the top and bottom bitrates don’t apply to all modes, it would |
| 192 |
be impossible to have a stereo file encoded from 32 to 384 kbps.</p></div> |
| 193 |
<div class="paragraph"><p>But this isn’t what is really limiting the allowable bitrate range - the bit allocation |
| 194 |
tables are the major hurdle.</p></div> |
| 195 |
</div> |
| 196 |
<div class="sect2"> |
| 197 |
<h3 id="_reason_2_the_bit_allocation_tables_don_8217_t_allow_it">Reason 2: The bit allocation tables don’t allow it</h3> |
| 198 |
<div class="paragraph"><p>From the standard, Section 2.4.3.3.1 "Bit allocation decoding"</p></div> |
| 199 |
<div class="literalblock"> |
| 200 |
<div class="content"> |
| 201 |
<pre><code>"For different combinations of bitrate and sampling frequency, different bit |
| 202 |
allocation tables exist.</code></pre> |
| 203 |
</div></div> |
| 204 |
<div class="paragraph"><p>These bit allocation tables are pre-determined tables (in Annex B of the standard) which |
| 205 |
indicate</p></div> |
| 206 |
<div class="ulist"><ul> |
| 207 |
<li> |
| 208 |
<p> |
| 209 |
how many bits to read for the initial data (2,3 or 4) |
| 210 |
</p> |
| 211 |
</li> |
| 212 |
<li> |
| 213 |
<p> |
| 214 |
these bits are then used as an index back into the table to |
| 215 |
find the number of quantize levels for the samples in this subband |
| 216 |
</p> |
| 217 |
</li> |
| 218 |
</ul></div> |
| 219 |
<div class="paragraph"><p>But the table used (and hence the number of bits and the calculated index) are different |
| 220 |
for different combinations of bitrate and sampling frequency.</p></div> |
| 221 |
<div class="paragraph"><p>I will use TableB.2a as an example.</p></div> |
| 222 |
<div class="paragraph"><p>Table B.2a Applies for the following combinations.</p></div> |
| 223 |
<div class="literalblock"> |
| 224 |
<div class="content"> |
| 225 |
<pre><code>Sampling Freq Bitrates in (kbps/channel) [emphasis: this is a PER CHANNEL bitrate] |
| 226 |
48 56, 64, 80, 96, 112, 128, 160, 192 |
| 227 |
44.1 56, 64, 80 |
| 228 |
32 56, 64, 80</code></pre> |
| 229 |
</div></div> |
| 230 |
<div class="paragraph"><p>If we have a STEREO 48kHz input file, and we use this table, then the bitrates |
| 231 |
we could calculate from this would be 112, 128, 160, 192, 224, 256, 320 and 384 kbps.</p></div> |
| 232 |
<div class="paragraph"><p>This table contains no information on how to encode stuff at bitrates less than 112kbps |
| 233 |
(for a stereo file). You would have to load allocation table B.2c to encode stereo at |
| 234 |
64kbps and 128kbps.</p></div> |
| 235 |
<div class="paragraph"><p>Since it would be a MAJOR piece of hacking to get the different tables shifted in and out |
| 236 |
during the encoding process, once an allocation table is loaded <strong>IT IS NOT CHANGED</strong>.</p></div> |
| 237 |
<div class="paragraph"><p>Hence, the best table is picked at the start of the encoding process, and the encoder |
| 238 |
is stuck with it for the rest of the encode.</p></div> |
| 239 |
<div class="paragraph"><p>For twolame-02j, I have picked the table it loads for different |
| 240 |
sampling frequencies in order to optimize the range of bitrates possible.</p></div> |
| 241 |
<div class="literalblock"> |
| 242 |
<div class="content"> |
| 243 |
<pre><code>48 kHz - Table B.2a |
| 244 |
Stereo Bitrate Range: 112 - 384 |
| 245 |
Mono Bitrate Range : 56 - 192</code></pre> |
| 246 |
</div></div> |
| 247 |
<div class="literalblock"> |
| 248 |
<div class="content"> |
| 249 |
<pre><code>44.1/32 kHz - Table B.2b |
| 250 |
Stereo Bitrate Range: 192 - 384 |
| 251 |
Mono Bitrate Range: 96 - 192</code></pre> |
| 252 |
</div></div> |
| 253 |
<div class="literalblock"> |
| 254 |
<div class="content"> |
| 255 |
<pre><code>24/22.05/16 kHz - LSF Table (Standard ISO/IEC 13818.3:1995 Annex B, Table B.1) |
| 256 |
There is only 1 table for the Lower Sampling Frequencies |
| 257 |
All modes (mono and stereo) are allowable at all bitrates |
| 258 |
So at the Lower Sampling Frequencies you *can* have a completely variable |
| 259 |
bitrate over the entire range.</code></pre> |
| 260 |
</div></div> |
| 261 |
</div> |
| 262 |
</div> |
| 263 |
</div> |
| 264 |
<div class="sect1"> |
| 265 |
<h2 id="_tech_stuff">Tech Stuff</h2> |
| 266 |
<div class="sectionbody"> |
| 267 |
<div class="paragraph"><p>The VBR mode is mainly centered around the main_bit_allocation() and |
| 268 |
a_bit_allocation() routines in encode.c.</p></div> |
| 269 |
<div class="paragraph"><p>The limited range of VBR is due to my particular implementation which restricts |
| 270 |
ranges to within one alloc table (see tables B.2a, B.2b, B.2c and B.2d in ISO 11172). |
| 271 |
The VBR range for 32/44.1khz lies within B.2b, and the 48khz VBR lies within table B.2a.</p></div> |
| 272 |
<div class="paragraph"><p>I’m not sure whether it is worth extending these ranges down to lower bitrates. |
| 273 |
The work required to switch alloc tables <strong>during</strong> the encoding is major.</p></div> |
| 274 |
<div class="paragraph"><p>In the case of silence, it might be worth doing a quick check for very low signals |
| 275 |
and writing a pre-calculated <strong>blank</strong> 32kpbs frame. [probably also a lot of work].</p></div> |
| 276 |
</div> |
| 277 |
</div> |
| 278 |
<div class="sect1"> |
| 279 |
<h2 id="_how_cbr_works">How CBR works</h2> |
| 280 |
<div class="sectionbody"> |
| 281 |
<div class="ulist"><ul> |
| 282 |
<li> |
| 283 |
<p> |
| 284 |
Use the psycho model to determine the MNRs for each subband |
| 285 |
[MNR = the ratio of "masking" to "noise"] |
| 286 |
(From an encoding perspective, a bigger MNR in a subband means that |
| 287 |
it sounds better since the noise is more masked)) |
| 288 |
</p> |
| 289 |
</li> |
| 290 |
<li> |
| 291 |
<p> |
| 292 |
calculate the available data bits (adb) for this bitrate. |
| 293 |
</p> |
| 294 |
</li> |
| 295 |
<li> |
| 296 |
<p> |
| 297 |
Based upon the MNR (Masking:Noise Ratio) values, allocate bits to each |
| 298 |
subband |
| 299 |
</p> |
| 300 |
</li> |
| 301 |
<li> |
| 302 |
<p> |
| 303 |
Keep increasing the bits to whichever subband currently has the min MNR |
| 304 |
value until we have no bits left. |
| 305 |
</p> |
| 306 |
</li> |
| 307 |
<li> |
| 308 |
<p> |
| 309 |
This mode does not guarentee that all the subbands are without noise |
| 310 |
ie there may still be subbands with MNR less than 0.0 (noisy!) |
| 311 |
</p> |
| 312 |
</li> |
| 313 |
</ul></div> |
| 314 |
</div> |
| 315 |
</div> |
| 316 |
<div class="sect1"> |
| 317 |
<h2 id="_how_vbr_works">How VBR works</h2> |
| 318 |
<div class="sectionbody"> |
| 319 |
<div class="ulist"><ul> |
| 320 |
<li> |
| 321 |
<p> |
| 322 |
pretend we have lots of bits to spare, and work out the bits which would |
| 323 |
raise the MNR in each subband to the level given by the argument on the |
| 324 |
command line "-v [int]" |
| 325 |
</p> |
| 326 |
</li> |
| 327 |
<li> |
| 328 |
<p> |
| 329 |
Pick the bitrate which has more bits than the required_bits we just calculated |
| 330 |
</p> |
| 331 |
</li> |
| 332 |
<li> |
| 333 |
<p> |
| 334 |
calculate a_bit_allocation() |
| 335 |
</p> |
| 336 |
</li> |
| 337 |
<li> |
| 338 |
<p> |
| 339 |
VBR "guarantees" that all subbands have MNR > VBRLEVEL or that we have |
| 340 |
reached the maximum bitrate. |
| 341 |
</p> |
| 342 |
</li> |
| 343 |
</ul></div> |
| 344 |
</div> |
| 345 |
</div> |
| 346 |
<div class="sect1"> |
| 347 |
<h2 id="_future">FUTURE</h2> |
| 348 |
<div class="sectionbody"> |
| 349 |
<div class="ulist"><ul> |
| 350 |
<li> |
| 351 |
<p> |
| 352 |
with this VBR mode, we know the bits aren’t going to run out, so we can |
| 353 |
just assign them "greedily". |
| 354 |
</p> |
| 355 |
</li> |
| 356 |
<li> |
| 357 |
<p> |
| 358 |
VBR_a_bit_allocation() is yet to be written :) |
| 359 |
</p> |
| 360 |
</li> |
| 361 |
</ul></div> |
| 362 |
</div> |
| 363 |
</div> |
| 364 |
</div> |
| 365 |
<div id="footnotes"><hr /></div> |
| 366 |
<div id="footer"> |
| 367 |
<div id="footer-text"> |
| 368 |
Version 0.4.0<br /> |
| 369 |
Last updated |
| 370 |
2019-10-11 17:26:25 BST |
| 371 |
</div> |
| 372 |
</div> |
| 373 |
</body> |
| 374 |
</html> |