@DATABASE mp3enc.guide

@WIDTH 80

@AUTHOR{Michael Cheng}

@NODE MAIN "mp3enc.guide"

mp3enc v0.1

Dodgy docs written by @{" Michael Cheng " link "Author"}

@{" Introduction " link "Introduction"}
@{" Requirements " link "Requirements"}
@{" Usage " link "Usage"}
@{" Speed " link "Speed"}
@{" Problems " link "Problems"}
@{" Source Code " link "Source"}
@{" Future " link "Future"}
@{" Acknowledgements " link "Acknowledgements"}
@{" Author " link "Author"}
@{" Appendix " link "Appendix"}

@ENDNODE


@NODE "Introduction" "Introduction"

This is the first amiga release version of ISO MPEG's public C source
code for mpeg audio layer 3 encoding.

The source if freely available from ISO MPEG, but they do not make any
testimony as to its usefulness, completeness or quickness.

This amiga release is very alpha-ish. Make sure you read the @{"Problems" link "Problems"}
section for info.

@ENDNODE

@NODE "Requirements" "Requirements"

REQUIREMENTS

    - An Amiga with an FPU.
	Encoding without an FPU is worse than your worst nightmare.

    - The fastest CPU you can find :)

@ENDNODE

@NODE "Usage" "Usage"

USAGE

The 'encode' program may either be run interactively at the command line,
or may be given exact command line arguments to run it without
interaction.

Interactive mode is quite self explanatory.

The following command line arguments are taken from the source code. Use
    encode -h
		to view the command options.

 -l  is followed by the layer number  (1,2,3)
 -m  is followed by the mode ([s]tereo [j]ointstereo [d]ualchannel [m]ono)
 -p  is followed by the psychoacoustic model number (1,2)
 -s  is followed by the sampling rate
 -b  is followed by the total bitrate, irrespective of the mode
 -d  is followed by the emphasis flag ([n]one [5]0/15microseconds [c]citt)
 -c  is followed by the copyright/no_copyright flag
 -o  is followed by the original/not_original flag
 -e  is followed by the error_protection on/off flag

If the input file is in AIFF format, the sampling frequency is read
from the AIFF header.  (NB: AIFF reading is untested)


LIMITATIONS

    - Only psychoacoustic model 2 is available for mp3 encoding.
    - Joint Stereo encoding is not available for mp3 encoding.


EXAMPLE

    encode -l 3 -p 2 -s 44.1 -b 128 -m m spot1.raw

    The source file is "spot1.raw" which is a 44khz 16bit mono pcm file.
    It is being encoded to layer 3 (-l 3)
    Using psychoacoustic model 2 (-p 2)
    Source frequency is 44.1khz (-s 44.1)
    Total encoded bitrate is 128 (-b 128)
    mp3 mode is mono (-m m)
    The output file is spot1.raw.mpg (by default)



@ENDNODE


@NODE "Problems" "Problems"

PROBLEMS

    - Layer I and Layer II audio encoding work fine, but you'd be much
	better off getting musicin.lha which is nicely optimized (but
	doesn't yet do layer III)

    - There are weird memory problems with encoding stereo samples to mp3.
	This problem doesn't arise when encoding mono samples to mp3.
	The problem can cause invalidated partitions and a spectacularly
	big crash of your amiga. So until you're confident, work on a
	scratch partition, or encode to ram:

    - Stephane Tavenard (mpega author) told me that there's a bug in part
	of the encoder (the bit allocation?)

    - Doesn't yet encode in the common "joint stereo" mp3 format.

    - Program seems to only like 44.1khz 16bit PCM samples. Let me
	know if you have success with other frequencies.

@ENDNODE

@NODE "Speed" "Speed"

Encoding Layer 3 mpeg audio files is very CPU intensive.  Just to give you
an idea on what sort of times you can expect I've included a "standard"
sound file which I have been using to time the encoding. [spot1.raw is 1
second of 44.1khz 16bit mono pcm sound]

My 030/881/25Mhz takes 190 seconds to encode this to 128bit/s mp3

An 060/50 takes 20 seconds.

Since this was just a mono sound file, approximately double the time for
encoding stereo sound.

So encoding a 44.1khz stereo song of 3 minutes will take about 2 hours at
least on an 060.  This is just a guess mind you, as I haven't met anyone
masochistic enough to do full scale mp3 encoding on an amiga.

@ENDNODE

@NODE "Source" "Source"

The complete mpeg audio demonstration C source is available from

ftp://ftp.tnt.uni-hannover.de/pub/MPEG/audio/mpeg2/software/technical_report/dist10.tar.gz

Any changes I have made to this default source are included in the /src
directory of this archive. You can simply copy my files over the default
distribution.


@ENDNODE

@NODE "Future" "Future"

FUTURE

    - Figure out that @{"memory corruption bug" link "Problems"}

    - @{"Optimize" link "Optimize"} code

    - Add in joint stereo encoding.

    - Keep up to date with the @{"net effort" link "Net"}

@ENDNODE

@NODE "Net" "Net"

The mailing list for amp has had some discussion recently on improving the
ISO MPEG source-code. These guys are pretty good programmers and have
lots of ideas for optimization and speedier code.

As improvements and optimizations become known to me, I'll release new
versions.

@ENDNODE

@NODE "Optimize" "Optimize"


The code is written in ANSI C and is meant to be demonstrative C source
into how to implement the mpeg audio standard in software.  It was not
meant to be fast, tight or optimized for any particular processor.

Easy C optimizations don't seem that apparent.

Significant speedups will probably come from ASM replacements for the CPU
intensive routines.

Included below is a profile of the encode program.  Lots of time is spent
in 3 or 4 main routines.  So if you feel like having a go at optimizing in
C or ASM, concentrate on those functions ie fft, filter_subband, quantize.

NB: I'm not at all experienced in putting ASM code in C code using the gcc
compiler. Any pointers would be appreciated.


Flat profile:

Each sample counts as 0.02 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ns/call  ns/call  name    
 24.83     51.58    51.58                             fft
 18.82     90.68    39.10                             filter_subband
 14.45    120.70    30.02                             quantize
  8.97    139.34    18.64                             mdct
  7.17    154.23    14.89                             mcount
  5.17    164.97    10.74                             L3psycho_anal
  4.62    174.57     9.60                             HuffmanCode
  3.97    182.81     8.24                             nint
  3.71    190.51     7.70                             window_subband
  1.02    192.63     2.12                             calc_noise
  0.82    194.33     1.70                             count_bit
  0.75    195.89     1.56                             quantanf_init
  0.69    197.33     1.44                             mdct_sub
  0.64    198.65     1.32   956280  1380.35  1380.35  mcount
  0.39    199.47     0.82                             calc_scfsi
  0.32    200.13     0.66                             xr_max
  0.31    200.78     0.65                             putbits
  0.26    201.32     0.54                             BF_PartLength
  0.25    201.84     0.52                             count_bits
  0.22    202.30     0.46                             BF_addEntry
  0.19    202.70     0.40                             BF_addElement
  0.19    203.10     0.40                             get_audio
  0.19    203.49     0.39                             _mcleanup
  0.18    203.87     0.38                             iteration_loop
  0.17    204.23     0.36                             L3para_read
  0.17    204.59     0.36                             calc_xmin
  0.16    204.93     0.34                             new_choose_table
  0.13    205.19     0.26                             II_bits_for_nonoise
  0.11    205.41     0.22                             I_bits_for_nonoise
  0.10    205.61     0.20                             BF_resizePartHolder
  0.09    205.79     0.18                             III_FlushBitstream
  0.09    205.97     0.18                             huffman_decoder
  0.08    206.13     0.16                             III_format_bitstream
  0.08    206.29     0.16                             count1_bitcount
  0.07    206.43     0.14                             create_ana_filter
  0.07    206.57     0.14                             ix_max
  0.07    206.71     0.14                             putMyBits
  0.06    206.83     0.12                             BF_NewHolderFromBitstreamPart
  0.06    206.95     0.12                             read_decoder_table
  0.06    207.07     0.12                             read_huffcodetab
  0.05    207.17     0.10                             abs_and_sign
  0.04    207.25     0.08                             bigv_bitcount
  0.04    207.33     0.08                             huffman_coder
  0.03    207.39     0.06                             BF_newPartHolder
  0.03    207.45     0.06                             ConvertToIeeeSingle
  0.02    207.49     0.04                             I_scale_factor_calc
  0.02    207.53     0.04                             rewindNbytes
  0.01    207.55     0.02                             BF_LoadHolderFromBitstreamPart
  0.01    207.57     0.02                             BF_freePartHolder
  0.01    207.59     0.02                             BitrateIndex
  0.01    207.61     0.02                             ConvertFromIeeeSingle
  0.01    207.63     0.02                             aint
  0.01    207.65     0.02                             calc_runlen
  0.01    207.67     0.02                             free
  0.01    207.69     0.02                             fwrite
  0.01    207.71     0.02                             read_ana_window
  0.01    207.73     0.02                             sscanf
  0.01    207.75     0.02                             subdivide
  0.00    207.76     0.01                             getbits

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
	   listing.

calls      the number of times this function was invoked, if
	   this function is profiled, else blank.
 
 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
	   else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this 
	   function is profiled, else blank.

name       the name of the function.  This is the minor sort
	   for this listing. The index shows the location of
	   the function in the gprof listing. If the index is
	   in parenthesis it shows where it would appear in
	   the gprof listing if it were to be printed.

		     Call graph (explanation follows)


granularity: each sample hit covers 4 byte(s) for 0.01% of 191.16 seconds

index % time    self  children    called     name
						 <spontaneous>
[1]     27.0   51.58    0.00                 fft [1]
		0.00    0.00       2/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[2]     20.5   39.10    0.00                 filter_subband [2]
		0.00    0.00       2/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[3]     15.7   30.02    0.00                 quantize [3]
		0.00    0.00  432576/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[4]      9.8   18.64    0.00                 mdct [4]
-----------------------------------------------
						 <spontaneous>
[5]      5.6   10.74    0.00                 L3psycho_anal [5]
		0.00    0.00    2354/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[6]      5.0    9.60    0.00                 HuffmanCode [6]
-----------------------------------------------
						 <spontaneous>
[7]      4.3    8.24    0.00                 nint [7]
-----------------------------------------------
						 <spontaneous>
[8]      4.0    7.70    0.00                 window_subband [8]
		0.00    0.00       3/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[9]      1.1    2.12    0.00                 calc_noise [9]
-----------------------------------------------
						 <spontaneous>
[10]     0.9    1.70    0.00                 count_bit [10]
		0.00    0.00  296344/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[11]     0.8    1.56    0.00                 quantanf_init [11]
		0.00    0.00      75/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[12]     0.8    1.44    0.00                 mdct_sub [12]
		0.00    0.00    2496/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[13]     0.4    0.82    0.00                 calc_scfsi [13]
		0.00    0.00      78/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[14]     0.3    0.66    0.00                 xr_max [14]
-----------------------------------------------
						 <spontaneous>
[15]     0.3    0.65    0.00                 putbits [15]
		0.00    0.00   12279/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[16]     0.3    0.54    0.00                 BF_PartLength [16]
		0.00    0.00   22670/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[17]     0.3    0.52    0.00                 count_bits [17]
	     181170887527902135823437523267474075217788929994682683463763719356416.00    0.00    2532/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[18]     0.2    0.46    0.00                 BF_addEntry [18]
	     605818841847439428249236568578918254354336256457716771825440177277036634835629933150429943074526587700645054172271344758075074219307229338132676649943040.00 185012683372606779184939189948247748191489885389347812729919754993664.00   47264/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[19]     0.2    0.40    0.00                 BF_addElement [19]
		0.00    0.00      12/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[20]     0.2    0.40    0.00                 get_audio [20]
	     4047476907590138774493894118156762147196630027686710400253952.00 186528604498704993296828330197696015318492646116357259408375526981632.00      43/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[21]     0.2    0.38    0.00                 iteration_loop [21]
		0.00    0.00     777/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[22]     0.2    0.36    0.00                 L3para_read [22]
		0.00    0.00    1516/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[23]     0.2    0.36    0.00                 calc_xmin [23]
-----------------------------------------------
						 <spontaneous>
[24]     0.2    0.34    0.00                 new_choose_table [24]
		0.00    0.00    3542/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[25]     0.1    0.26    0.00                 II_bits_for_nonoise [25]
-----------------------------------------------
						 <spontaneous>
[26]     0.1    0.22    0.00                 I_bits_for_nonoise [26]
-----------------------------------------------
						 <spontaneous>
[27]     0.1    0.20    0.00                 BF_resizePartHolder [27]
-----------------------------------------------
						 <spontaneous>
[28]     0.1    0.18    0.00                 III_FlushBitstream [28]
		0.00    0.00   45225/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[29]     0.1    0.18    0.00                 huffman_decoder [29]
-----------------------------------------------
						 <spontaneous>
[30]     0.1    0.16    0.00                 III_format_bitstream [30]
		0.00    0.00     179/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[31]     0.1    0.16    0.00                 count1_bitcount [31]
		0.00    0.00   29640/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[32]     0.1    0.14    0.00                 create_ana_filter [32]
-----------------------------------------------
						 <spontaneous>
[33]     0.1    0.14    0.00                 ix_max [33]
-----------------------------------------------
						 <spontaneous>
[34]     0.1    0.14    0.00                 putMyBits [34]
		0.00    0.00   23668/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[35]     0.1    0.12    0.00                 BF_NewHolderFromBitstreamPart [35]
-----------------------------------------------
						 <spontaneous>
[36]     0.1    0.12    0.00                 read_decoder_table [36]
-----------------------------------------------
						 <spontaneous>
[37]     0.1    0.12    0.00                 read_huffcodetab [37]
	     72036449917790047203119181568699121193716387992961993939503963630018535499313810933233172723059338501907572632602295074500600661725914190418899158432327893052179950275114164516833180574811284745742205321216.00    0.00    3162/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[38]     0.1    0.10    0.00                 abs_and_sign [38]
-----------------------------------------------
						 <spontaneous>
[39]     0.0    0.08    0.00                 bigv_bitcount [39]
		0.00    0.00    9494/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[40]     0.0    0.08    0.00                 huffman_coder [40]
-----------------------------------------------
						 <spontaneous>
[41]     0.0    0.06    0.00                 BF_newPartHolder [41]
		0.00    0.00      93/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[42]     0.0    0.06    0.00                 ConvertToIeeeSingle [42]
-----------------------------------------------
						 <spontaneous>
[43]     0.0    0.04    0.00                 I_scale_factor_calc [43]
-----------------------------------------------
						 <spontaneous>
[44]     0.0    0.04    0.00                 rewindNbytes [44]
-----------------------------------------------
						 <spontaneous>
[45]     0.0    0.02    0.00                 BF_LoadHolderFromBitstreamPart [45]
		0.00 63831252999598997616031938012764816835702467370965125406544302975248479091727950234001828619861383636281895469195849632052327734654757268413895341528040058105560535902771711594484156597258142353929044623657979067897665039957491712.00    1765/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[46]     0.0    0.02    0.00                 BF_freePartHolder [46]
		0.00    0.00      39/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[47]     0.0    0.02    0.00                 BitrateIndex [47]
-----------------------------------------------
						 <spontaneous>
[48]     0.0    0.02    0.00                 ConvertFromIeeeSingle [48]
-----------------------------------------------
						 <spontaneous>
[49]     0.0    0.02    0.00                 aint [49]
-----------------------------------------------
						 <spontaneous>
[50]     0.0    0.02    0.00                 calc_runlen [50]
-----------------------------------------------
						 <spontaneous>
[51]     0.0    0.02    0.00                 free [51]
-----------------------------------------------
						 <spontaneous>
[52]     0.0    0.02    0.00                 fwrite [52]
-----------------------------------------------
						 <spontaneous>
[53]     0.0    0.02    0.00                 read_ana_window [53]
		0.00    0.00     386/956280      mcount (57)
-----------------------------------------------
						 <spontaneous>
[54]     0.0    0.02    0.00                 sscanf [54]
-----------------------------------------------
						 <spontaneous>
[55]     0.0    0.02    0.00                 subdivide [55]
-----------------------------------------------
						 <spontaneous>
[56]     0.0    0.01    0.00                 getbits [56]
-----------------------------------------------

 This table describes the call tree of the program, and was sorted by
 the total amount of time spent in each function and its children.

 Each entry in this table consists of several lines.  The line with the
 index number at the left hand margin lists the current function.
 The lines above it list the functions that called this function,
 and the lines below it list the functions this one called.
 This line lists:
     index      A unique number given to each element of the table.
		Index numbers are sorted numerically.
		The index number is printed next to every function name so
		it is easier to look up where the function in the table.

     % time     This is the percentage of the `total' time that was spent
		in this function and its children.  Note that due to
		different viewpoints, functions excluded by options, etc,
		these numbers will NOT add up to 100%.

     self       This is the total amount of time spent in this function.

     children   This is the total amount of time propagated into this
		function by its children.

     called     This is the number of times the function was called.
		If the function called itself recursively, the number
		only includes non-recursive calls, and is followed by
		a `+' and the number of recursive calls.

     name       The name of the current function.  The index number is
		printed after it.  If the function is a member of a
		cycle, the cycle number is printed between the
		function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self       This is the amount of time that was propagated directly
		from the function into this parent.

     children   This is the amount of time that was propagated from
		the function's children into this parent.

     called     This is the number of times this parent called the
		function `/' the total number of times the function
		was called.  Recursive calls to the function are not
		included in the number after the `/'.

     name       This is the name of the parent.  The parent's index
		number is printed after it.  If the parent is a
		member of a cycle, the cycle number is printed between
		the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self       This is the amount of time that was propagated directly
		from the child into the function.

     children   This is the amount of time that was propagated from the
		child's children to the function.

     called     This is the number of times the function called
		this child `/' the total number of times the child
		was called.  Recursive calls by the child are not
		listed in the number after the `/'.

     name       This is the name of the child.  The child's index
		number is printed after it.  If the child is a
		member of a cycle, the cycle number is printed
		between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 entry for the cycle-as-a-whole.  This entry shows who called the
 cycle (as parents) and the members of the cycle (as children.)
 The `+' recursive calls entry shows the number of function calls that
 were internal to the cycle, and the calls entry for each member shows,
 for that member, how many times it was called from other members of
 the cycle.


Index by function name

  [45] BF_LoadHolderFromBitstreamPart [38] abs_and_sign   [33] ix_max
  [35] BF_NewHolderFromBitstreamPart [49] aint           (217) mcount
  [16] BF_PartLength          [39] bigv_bitcount           [4] mdct
  [19] BF_addElement           [9] calc_noise             [12] mdct_sub
  [18] BF_addEntry            [50] calc_runlen            [24] new_choose_table
  [46] BF_freePartHolder      [13] calc_scfsi              [7] nint
  [41] BF_newPartHolder       [23] calc_xmin              [34] putMyBits
  [27] BF_resizePartHolder    [31] count1_bitcount        [15] putbits
  [47] BitrateIndex           [10] count_bit              [11] quantanf_init
  [48] ConvertFromIeeeSingle  [17] count_bits              [3] quantize
  [42] ConvertToIeeeSingle    [32] create_ana_filter      [53] read_ana_window
   [6] HuffmanCode             [1] fft                    [36] read_decoder_table
  [28] III_FlushBitstream      [2] filter_subband         [37] read_huffcodetab
  [30] III_format_bitstream   [51] free                   [44] rewindNbytes
  [25] II_bits_for_nonoise    [52] fwrite                 [54] sscanf
  [26] I_bits_for_nonoise     [20] get_audio              [55] subdivide
  [43] I_scale_factor_calc    [56] getbits                 [8] window_subband
  [22] L3para_read            [40] huffman_coder          [14] xr_max
   [5] L3psycho_anal          [29] huffman_decoder        (57) mcount
 (154) _mcleanup              [21] iteration_loop


@ENDNODE

@NODE "Acknowledgements" "Acknowledgements"

THANKS

    - Stéphane TAVENARD (tavenard@xiii.univ-angers.fr)for mpega, the best
	amiga mpeg audio player.
 
    - Troels and SimD and Lazaruz and others i've forgotten on #amiga
	for helping me test.

    - ISO MPEG Audio Subgroup Software Simulation Group for making the
	source available.


@ENDNODE

@NODE "Author" "Author"

mike cheng
Cstar on #amiga
mikecheng@cryogen.com
http://www.netforward.com/cryogen/?mikecheng
http://mikecheng.dyn.ml.org/

@ENDNODE

@NODE "Appendix" "Appendix"


@ENDNODE
