Blame doc/liba52.txt

Packit 64f477
Using the liba52 API
Packit 64f477
--------------------
Packit 64f477
Packit 64f477
liba52 provides a low-level interface to decoding audio frames encoded
Packit 64f477
using ATSC standard A/52 aka AC-3. liba52 provides downmixing and
Packit 64f477
dynamic range compression for the following output configurations:
Packit 64f477
Packit 64f477
A52_CHANNEL  : Dual mono. Two independant mono channels.
Packit 64f477
A52_CHANNEL1 : First of the two mono channels above.
Packit 64f477
A52_CHANNEL2 : Second of the two mono channels above.
Packit 64f477
A52_MONO     : Mono.
Packit 64f477
A52_STEREO   : Stereo.
Packit 64f477
A52_DOLBY    : Dolby surround compatible stereo.
Packit 64f477
A52_3F       : 3 front channels (left, center, right)
Packit 64f477
A52_2F1R     : 2 front, 1 rear surround channel (L, R, S)
Packit 64f477
A52_3F1R     : 3 front, 1 rear surround channel (L, C, R, S)
Packit 64f477
A52_2F2R     : 2 front, 2 rear surround channels (L, R, LS, RS)
Packit 64f477
A52_3F2R     : 3 front, 2 rear surround channels (L, C, R, LS, RS)
Packit 64f477
Packit 64f477
A52_LFE      : Low frequency effects channel. Normally used to connect a
Packit 64f477
               subwoofer. Can be combined with any of the above channels.
Packit 64f477
               For example: A52_3F2R | A52_LFE -> 3 front, 2 rear, 1 LFE (5.1)
Packit 64f477
Packit 64f477
Packit 64f477
Initialization
Packit 64f477
--------------
Packit 64f477
Packit 64f477
sample_t * a52_init (uint32_t mm_accel);
Packit 64f477
Packit 64f477
Initializes the A/52 library. Takes as a parameter the acceptable
Packit 64f477
optimizations which may be used, such as MMX. These are found in the
Packit 64f477
included header file 'mm_accel', along with an autodetection function
Packit 64f477
(mm_accel()). Currently, the only accelleration implemented is
Packit 64f477
MM_ACCEL_MLIB, which uses the 'mlib' library if installed. mlib is
Packit 64f477
only available on some Sun Microsystems platforms.
Packit 64f477
Packit 64f477
The return value is a pointer to a properly-aligned sample buffer used
Packit 64f477
for output samples.
Packit 64f477
Packit 64f477
Packit 64f477
Probing the bitstream
Packit 64f477
---------------------
Packit 64f477
Packit 64f477
int a52_syncinfo (uint8_t * buf, int * flags,
Packit 64f477
                  int * sample_rate, int * bit_rate);
Packit 64f477
Packit 64f477
The A/52 bitstream is composed of several a52 frames concatenated one
Packit 64f477
after each other. An a52 frame is the smallest independantly decodable
Packit 64f477
unit in the stream.
Packit 64f477
Packit 64f477
buf must contain at least 7 bytes from the input stream. If these look
Packit 64f477
like the start of a valid a52 frame, a52_syncinfo() returns the size
Packit 64f477
of the coded frame in bytes, and fills flags, sample_rate and bit_rate
Packit 64f477
with the information encoded in the stream. The returned size is
Packit 64f477
guaranteed to be an even number between 128 and 3840. sample_rate will
Packit 64f477
be the sampling frequency in Hz, bit_rate is for the compressed stream
Packit 64f477
and is in bits per second, and flags is a description of the coded
Packit 64f477
channels: the A52_LFE bit is set if there is an LFE channel coded in
Packit 64f477
this stream, and by masking flags with A52_CHANNEL_MASK you will get a
Packit 64f477
value that describes the full-bandwidth channels, as one of the
Packit 64f477
A52_CHANNEL...A52_3F2R flags.
Packit 64f477
Packit 64f477
If this can not possibly be a valid frame, then the function returns
Packit 64f477
0. You should then try to re-synchronize with the a52 stream - one way
Packit 64f477
to try this would be to advance buf by one byte until its contents
Packit 64f477
looks like a valid frame, but there might be better
Packit 64f477
application-specific ways to synchronize.
Packit 64f477
Packit 64f477
It is recommended to call this function for each frame, for several
Packit 64f477
reasons: this function detects errors that the other functions will
Packit 64f477
not double-check, consecutive frames might have different lengths, and
Packit 64f477
it helps you re-sync with the stream if you get de-synchronized.
Packit 64f477
Packit 64f477
Packit 64f477
Starting to decode a frame
Packit 64f477
--------------------------
Packit 64f477
Packit 64f477
int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
Packit 64f477
	       sample_t * level, sample_t bias);
Packit 64f477
Packit 64f477
This starts the work of decoding the A/52 frame (to be completed using
Packit 64f477
a52_block()). buf should point to the beginning of the complete frame
Packit 64f477
of the full size returned by a52_syncinfo().
Packit 64f477
Packit 64f477
You should pass in the flags the speaker configuration that you
Packit 64f477
support, and liba52 will return the speaker configuration it will use
Packit 64f477
for its output, based on what is coded in the stream and what you
Packit 64f477
asked for. For example, if the stream contains 2+2 channels
Packit 64f477
(a52_syncinfo() returned A52_2F2R in the flags), and you have 3+1
Packit 64f477
speakers (you passed A52_3F1R), then liba52 will choose do downmix to
Packit 64f477
2+1 speakers, since there is no center channel to send to your center
Packit 64f477
speaker. So in that case the left and right channels will be
Packit 64f477
essentially unmodified by the downmix, and the two surround channels
Packit 64f477
will be added together and sent to your surround speaker. liba52 will
Packit 64f477
return A52_2F1R to indicate this.
Packit 64f477
Packit 64f477
The good news is that when you downmix to stereo you dont have to
Packit 64f477
worry about this, you will ALWAYS get a stereo output no matter what
Packit 64f477
was coded in the stream. For more complex output configurations you
Packit 64f477
will have to handle the case where liba52 couldnt give you what you
Packit 64f477
wanted because some of the channels were not encoded in the stream
Packit 64f477
though.
Packit 64f477
Packit 64f477
Level, bias, and A52_ADJUST_LEVEL:
Packit 64f477
Packit 64f477
Before downmixing, samples are floating point values with a range of
Packit 64f477
[-1,1]. Most types of downmixing will combine channels together, which
Packit 64f477
will potentially result in a larger range for the output
Packit 64f477
samples. liba52 provides two methods of controlling the range of the
Packit 64f477
output, either before or after the downmix stage.
Packit 64f477
Packit 64f477
If you do not set A52_ADJUST_LEVEL, liba52 will multiply the samples
Packit 64f477
by your level value, so that they fit in the [-level,level]
Packit 64f477
range. Then it will apply the standardized downmix equations,
Packit 64f477
potentially making the samples go out of that interval again. The
Packit 64f477
level parameter is not modified.
Packit 64f477
Packit 64f477
Setting the A52_ADJUST_LEVEL flag will instruct liba52 to treat your
Packit 64f477
level value as the intended range interval after downmixing. It will
Packit 64f477
then figure out what level to use before the downmix (what you should
Packit 64f477
have passed if you hadnt used the A52_ADJUST_LEVEL flag), and
Packit 64f477
overwrite the level value you gave it with that new level value.
Packit 64f477
Packit 64f477
The bias represents a value which should be added to the result
Packit 64f477
regardless:
Packit 64f477
Packit 64f477
output_sample = (input_sample * level) + bias;
Packit 64f477
Packit 64f477
For example, a bias of 384 and a level of 1 tells liba52 you want
Packit 64f477
samples between 383 and 385 instead of -1 and 1. This is what the
Packit 64f477
sample program a52dec does, as it makes it faster to convert the
Packit 64f477
samples to integer format, using a trick based on the IEEE
Packit 64f477
floating-point format.
Packit 64f477
Packit 64f477
This function also initialises the state for that frame, which will be
Packit 64f477
reused next when decoding blocks.
Packit 64f477
Packit 64f477
Packit 64f477
Dynamic range compression
Packit 64f477
-------------------------
Packit 64f477
Packit 64f477
void a52_dynrng (a52_state_t * state,
Packit 64f477
                 sample_t (* call) (sample_t, void *), void * data);
Packit 64f477
Packit 64f477
This function is purely optional. If you dont call it, liba52 will
Packit 64f477
provide the default behaviour, which is to apply the full dynamic
Packit 64f477
range compression as specified in the A/52 stream. This basically
Packit 64f477
makes the loud sounds softer, and the soft sounds louder, so you can
Packit 64f477
more easily listen to the stream in a noisy environment without
Packit 64f477
disturbing anyone.
Packit 64f477
Packit 64f477
If you do call this function and set a NULL callback, this will
Packit 64f477
totally disable the dynamic range compression and provide a playback
Packit 64f477
more adapted to a movie theater or a listening room.
Packit 64f477
Packit 64f477
If you call this function and specify a callback function, this
Packit 64f477
callback might be called up to once for each block, with two
Packit 64f477
arguments: the compression factor 'c' recommended by the bitstream,
Packit 64f477
and the private data pointer you specified in a52_dynrng(). The
Packit 64f477
callback will then return the amount of compression to actually use -
Packit 64f477
typically pow(c,x) where x is somewhere between 0 and 1. More
Packit 64f477
elaborate compression functions might want to use a different value
Packit 64f477
for 'x' depending wether c>1 or c<1 - or even something more complex
Packit 64f477
if this is what you want.
Packit 64f477
Packit 64f477
Packit 64f477
Decoding blocks
Packit 64f477
---------------
Packit 64f477
Packit 64f477
int a52_block (a52_state_t * state, sample_t * samples);
Packit 64f477
Packit 64f477
Every A/52 frame is composed of 6 blocks, each with an output of 256
Packit 64f477
samples for each channel. The a52_block() function decodes the next
Packit 64f477
block in the frame, and should be called 6 times to decode all of the
Packit 64f477
audio in the frame. After each call, you should extract the audio data
Packit 64f477
from the sample buffer.
Packit 64f477
Packit 64f477
The sample pointer given should be the one a52_init() returned.
Packit 64f477
Packit 64f477
After this function returns, the samples buuffer will contain 256
Packit 64f477
samples for the first channel, followed by 256 samples for the second
Packit 64f477
channel, etc... the channel order is LFE, left, center, right, left
Packit 64f477
surround, right surround. If one of the channels is not present in the
Packit 64f477
liba52 output, as indicated by the flags returned by a52_frame(), then
Packit 64f477
this channel is skipped and the following channels are shifted so
Packit 64f477
liba52 does not leave an empty space between channels.
Packit 64f477
Packit 64f477
Packit 64f477
Pseudocode example
Packit 64f477
------------------
Packit 64f477
Packit 64f477
sample_t * samples = a52_init (mm_accel());
Packit 64f477
Packit 64f477
loop on input bytes:
Packit 64f477
  if at least 7 bytes in the buffer:
Packit 64f477
Packit 64f477
    bytes_to_get = a52_syncinfo (...)
Packit 64f477
Packit 64f477
    if bytes_to_get == 0:
Packit 64f477
      goto loop to keep looking for sync point
Packit 64f477
    else
Packit 64f477
      get rest of bytes
Packit 64f477
Packit 64f477
      a52_frame (state, buf, ...)
Packit 64f477
      [a52_dynrng (state, ...); this is only optional]
Packit 64f477
      for i = 1 ... 6:
Packit 64f477
        a52_block (state, samples)
Packit 64f477
        convert samples to integer and queue to soundcard