This is called after a frame of audio has been quantized and coded. It will write the encoded audio to the bitstream. Note that from a layer3 encoder's perspective the bit stream is primarily a series of main_data() blocks, with header and side information inserted at the proper locations to maintai
(final LameGlobalFlags gfp)
| 818 | * to maintain framing. (See Figure A.7 in the IS). |
| 819 | */ |
| 820 | public final int format_bitstream(final LameGlobalFlags gfp) { |
| 821 | final LameInternalFlags gfc = gfp.internal_flags; |
| 822 | IIISideInfo l3_side; |
| 823 | l3_side = gfc.l3_side; |
| 824 | |
| 825 | int bitsPerFrame = getframebits(gfp); |
| 826 | drain_into_ancillary(gfp, l3_side.resvDrain_pre); |
| 827 | |
| 828 | encodeSideInfo2(gfp, bitsPerFrame); |
| 829 | int bits = 8 * gfc.sideinfo_len; |
| 830 | bits += writeMainData(gfp); |
| 831 | drain_into_ancillary(gfp, l3_side.resvDrain_post); |
| 832 | bits += l3_side.resvDrain_post; |
| 833 | |
| 834 | l3_side.main_data_begin += (bitsPerFrame - bits) / 8; |
| 835 | |
| 836 | /* |
| 837 | * compare number of bits needed to clear all buffered mp3 frames with |
| 838 | * what we think the resvsize is: |
| 839 | */ |
| 840 | if (compute_flushbits(gfp, new TotalBytes()) != gfc.ResvSize) { |
| 841 | System.err |
| 842 | .println("Internal buffer inconsistency. flushbits <> ResvSize"); |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | * compare main_data_begin for the next frame with what we think the |
| 847 | * resvsize is: |
| 848 | */ |
| 849 | if ((l3_side.main_data_begin * 8) != gfc.ResvSize) { |
| 850 | System.err.printf("bit reservoir error: \n" |
| 851 | + "l3_side.main_data_begin: %d \n" |
| 852 | + "Resvoir size: %d \n" |
| 853 | + "resv drain (post) %d \n" |
| 854 | + "resv drain (pre) %d \n" |
| 855 | + "header and sideinfo: %d \n" |
| 856 | + "data bits: %d \n" |
| 857 | + "total bits: %d (remainder: %d) \n" |
| 858 | + "bitsperframe: %d \n", |
| 859 | 8 * l3_side.main_data_begin, gfc.ResvSize, |
| 860 | l3_side.resvDrain_post, l3_side.resvDrain_pre, |
| 861 | 8 * gfc.sideinfo_len, bits - l3_side.resvDrain_post - 8 |
| 862 | * gfc.sideinfo_len, bits, bits % 8, bitsPerFrame); |
| 863 | |
| 864 | System.err |
| 865 | .println("This is a fatal error. It has several possible causes:"); |
| 866 | System.err |
| 867 | .println("90%% LAME compiled with buggy version of gcc using advanced optimizations"); |
| 868 | System.err.println(" 9%% Your system is overclocked"); |
| 869 | System.err.println(" 1%% bug in LAME encoding library"); |
| 870 | |
| 871 | gfc.ResvSize = l3_side.main_data_begin * 8; |
| 872 | } |
| 873 | ; |
| 874 | assert (totbit % 8 == 0); |
| 875 | |
| 876 | if (totbit > 1000000000) { |
| 877 | /* |
no test coverage detected