| 1 |
Using the libdca API |
| 2 |
-------------------- |
| 3 |
|
| 4 |
libdca provides a low-level interface to decoding audio frames encoded |
| 5 |
using DTS Coherent Acoustics. libdca provides downmixing and |
| 6 |
dynamic range compression for the following output configurations: |
| 7 |
|
| 8 |
DCA_CHANNEL : Dual mono. Two independant mono channels. |
| 9 |
DCA_CHANNEL1 : First of the two mono channels above. |
| 10 |
DCA_CHANNEL2 : Second of the two mono channels above. |
| 11 |
DCA_MONO : Mono. |
| 12 |
DCA_STEREO : Stereo. |
| 13 |
DCA_DOLBY : Dolby surround compatible stereo. |
| 14 |
DCA_3F : 3 front channels (left, center, right) |
| 15 |
DCA_2F1R : 2 front, 1 rear surround channel (L, R, S) |
| 16 |
DCA_3F1R : 3 front, 1 rear surround channel (L, C, R, S) |
| 17 |
DCA_2F2R : 2 front, 2 rear surround channels (L, R, LS, RS) |
| 18 |
DCA_3F2R : 3 front, 2 rear surround channels (L, C, R, LS, RS) |
| 19 |
|
| 20 |
DCA_LFE : Low frequency effects channel. Normally used to connect a |
| 21 |
subwoofer. Can be combined with any of the above channels. |
| 22 |
For example: DCA_3F2R | DCA_LFE -> 3 front, 2 rear, 1 LFE (5.1) |
| 23 |
|
| 24 |
|
| 25 |
Initialization |
| 26 |
-------------- |
| 27 |
|
| 28 |
dca_state_t * dca_init (uint32_t mm_accel); |
| 29 |
|
| 30 |
Initializes the DTS library. Takes as a parameter the acceptable |
| 31 |
optimizations which may be used, such as MMX. These are found in the |
| 32 |
included header file 'mm_accel', along with an autodetection function |
| 33 |
(mm_accel()). Currently, there is no accelleration implemented. |
| 34 |
|
| 35 |
The return value is a pointer to a dca state object. |
| 36 |
|
| 37 |
|
| 38 |
Probing the bitstream |
| 39 |
--------------------- |
| 40 |
|
| 41 |
int dca_syncinfo (uint8_t * buf, int * flags, |
| 42 |
int * sample_rate, int * bit_rate, int * frame_length); |
| 43 |
|
| 44 |
The DTS bitstream is composed of several DTS frames concatenated one |
| 45 |
after each other. A DTS frame is the smallest independantly decodable |
| 46 |
unit in the stream. |
| 47 |
|
| 48 |
buf must contain at least 14 bytes from the input stream. If these look |
| 49 |
like the start of a valid DTS frame, dca_syncinfo() returns the size |
| 50 |
of the coded frame in bytes, and fills flags, sample_rate, bit_rate and |
| 51 |
frame_length with the information encoded in the stream. The returned size |
| 52 |
is guaranteed to be an even number between 96 and 16384 for the 16 bits |
| 53 |
version of the bitstream and 109 and 18726 for the 14 bits version. |
| 54 |
sample_rate will be the sampling frequency in Hz, bit_rate is for the |
| 55 |
compressed stream and is in bits per second, and flags is a description of |
| 56 |
the coded channels: the DCA_LFE bit is set if there is an LFE channel coded |
| 57 |
in this stream, and by masking flags with DCA_CHANNEL_MASK you will get a |
| 58 |
value that describes the full-bandwidth channels, as one of the |
| 59 |
DCA_CHANNEL...DCA_3F2R flags. |
| 60 |
|
| 61 |
If this can not possibly be a valid frame, then the function returns |
| 62 |
0. You should then try to re-synchronize with the DTS stream - one way |
| 63 |
to try this would be to advance buf by one byte until its contents |
| 64 |
looks like a valid frame, but there might be better |
| 65 |
application-specific ways to synchronize. |
| 66 |
|
| 67 |
You need to call this function for each frame, for several |
| 68 |
reasons: this function detects errors that the other functions will |
| 69 |
not double-check, consecutive frames might have different lengths, and |
| 70 |
it helps you re-sync with the stream if you get de-synchronized. It will as |
| 71 |
well detect the kind of bitstream it is dealing with (big/little endian, |
| 72 |
16/14 bits mode) |
| 73 |
|
| 74 |
|
| 75 |
Starting to decode a frame |
| 76 |
-------------------------- |
| 77 |
|
| 78 |
int dca_frame (dca_state_t * state, uint8_t * buf, int * flags, |
| 79 |
sample_t * level, sample_t bias); |
| 80 |
|
| 81 |
This starts the work of decoding the DTS frame (to be completed using |
| 82 |
dca_block()). buf should point to the beginning of the complete frame |
| 83 |
of the full size returned by dca_syncinfo(). |
| 84 |
|
| 85 |
You should pass in the flags the speaker configuration that you |
| 86 |
support, and libdca will return the speaker configuration it will use |
| 87 |
for its output, based on what is coded in the stream and what you |
| 88 |
asked for. For example, if the stream contains 2+2 channels |
| 89 |
(dca_syncinfo() returned DCA_2F2R in the flags), and you have 3+1 |
| 90 |
speakers (you passed DCA_3F1R), then libdca will choose do downmix to |
| 91 |
2+1 speakers, since there is no center channel to send to your center |
| 92 |
speaker. So in that case the left and right channels will be |
| 93 |
essentially unmodified by the downmix, and the two surround channels |
| 94 |
will be added together and sent to your surround speaker. libdca will |
| 95 |
return DCA_2F1R to indicate this. |
| 96 |
|
| 97 |
The good news is that when you downmix to stereo you dont have to |
| 98 |
worry about this, you will ALWAYS get a stereo output no matter what |
| 99 |
was coded in the stream. For more complex output configurations you |
| 100 |
will have to handle the case where libdca couldnt give you what you |
| 101 |
wanted because some of the channels were not encoded in the stream |
| 102 |
though. |
| 103 |
|
| 104 |
Level, bias, and DCA_ADJUST_LEVEL: |
| 105 |
|
| 106 |
Before downmixing, samples are floating point values with a range of |
| 107 |
[-1,1]. Most types of downmixing will combine channels together, which |
| 108 |
will potentially result in a larger range for the output |
| 109 |
samples. libdca provides two methods of controlling the range of the |
| 110 |
output, either before or after the downmix stage. |
| 111 |
|
| 112 |
If you do not set DCA_ADJUST_LEVEL, libdca will multiply the samples |
| 113 |
by your level value, so that they fit in the [-level,level] |
| 114 |
range. Then it will apply the standardized downmix equations, |
| 115 |
potentially making the samples go out of that interval again. The |
| 116 |
level parameter is not modified. |
| 117 |
|
| 118 |
Setting the DCA_ADJUST_LEVEL flag will instruct libdca to treat your |
| 119 |
level value as the intended range interval after downmixing. It will |
| 120 |
then figure out what level to use before the downmix (what you should |
| 121 |
have passed if you hadnt used the DCA_ADJUST_LEVEL flag), and |
| 122 |
overwrite the level value you gave it with that new level value. |
| 123 |
|
| 124 |
The bias represents a value which should be added to the result |
| 125 |
regardless: |
| 126 |
|
| 127 |
output_sample = (input_sample * level) + bias; |
| 128 |
|
| 129 |
For example, a bias of 384 and a level of 1 tells liba52 you want |
| 130 |
samples between 383 and 385 instead of -1 and 1. This is what the |
| 131 |
sample program dcadec does, as it makes it faster to convert the |
| 132 |
samples to integer format, using a trick based on the IEEE |
| 133 |
floating-point format. |
| 134 |
|
| 135 |
This function also initialises the state for that frame, which will be |
| 136 |
reused next when decoding blocks. |
| 137 |
|
| 138 |
|
| 139 |
Dynamic range compression |
| 140 |
------------------------- |
| 141 |
|
| 142 |
void dca_dynrng (dca_state_t * state, |
| 143 |
sample_t (* call) (sample_t, void *), void * data); |
| 144 |
|
| 145 |
This function is purely optional. If you dont call it, libdca will |
| 146 |
provide the default behaviour, which is to apply the full dynamic |
| 147 |
range compression as specified in the DTS stream. This basically |
| 148 |
makes the loud sounds softer, and the soft sounds louder, so you can |
| 149 |
more easily listen to the stream in a noisy environment without |
| 150 |
disturbing anyone. |
| 151 |
|
| 152 |
If you do call this function and set a NULL callback, this will |
| 153 |
totally disable the dynamic range compression and provide a playback |
| 154 |
more adapted to a movie theater or a listening room. |
| 155 |
|
| 156 |
If you call this function and specify a callback function, this |
| 157 |
callback might be called up to once for each block, with two |
| 158 |
arguments: the compression factor 'c' recommended by the bitstream, |
| 159 |
and the private data pointer you specified in dca_dynrng(). The |
| 160 |
callback will then return the amount of compression to actually use - |
| 161 |
typically pow(c,x) where x is somewhere between 0 and 1. More |
| 162 |
elaborate compression functions might want to use a different value |
| 163 |
for 'x' depending wether c>1 or c<1 - or even something more complex |
| 164 |
if this is what you want. |
| 165 |
|
| 166 |
|
| 167 |
Finding the number of blocks |
| 168 |
---------------------------- |
| 169 |
|
| 170 |
int dca_blocks_num (dca_state_t * state); |
| 171 |
|
| 172 |
Every DTS frame is composed of a variable number of blocks. Calling |
| 173 |
dca_blocks_num() after dca_frame() will give you the number of blocks in the |
| 174 |
current frame. |
| 175 |
|
| 176 |
Decoding blocks |
| 177 |
--------------- |
| 178 |
|
| 179 |
int dca_block (dca_state_t * state); |
| 180 |
|
| 181 |
Every DTS frame is composed of a variable number of blocks, each with an |
| 182 |
output of 256 samples for each channel. The dca_block() function decodes |
| 183 |
the next block in the frame, and should be called dca_blocks_num() times to |
| 184 |
decode all of the audio in the frame. |
| 185 |
|
| 186 |
Getting the decoded audio samples |
| 187 |
--------------------------------- |
| 188 |
|
| 189 |
sample_t * dca_samples (dca_state_t * state); |
| 190 |
|
| 191 |
After each call to dca_block(), you should extract the audio data from the |
| 192 |
internal samples buffer. |
| 193 |
|
| 194 |
This function returns a pointer to an internal buffer which will contain 256 |
| 195 |
samples for the first channel, followed by 256 samples for the second |
| 196 |
channel, etc... the channel order is center, left, right, left |
| 197 |
surround, right surround, LFE. If one of the channels is not present in the |
| 198 |
libdca output, as indicated by the flags returned by dca_frame(), then |
| 199 |
this channel is skipped and the following channels are shifted so |
| 200 |
libdca does not leave an empty space between channels. |
| 201 |
|
| 202 |
|
| 203 |
Pseudocode example |
| 204 |
------------------ |
| 205 |
|
| 206 |
dca_state_t * state = dca_init (mm_accel()); |
| 207 |
|
| 208 |
loop on input bytes: |
| 209 |
if at least 14 bytes in the buffer: |
| 210 |
|
| 211 |
bytes_to_get = dca_syncinfo (...) |
| 212 |
|
| 213 |
if bytes_to_get == 0: |
| 214 |
goto loop to keep looking for sync point |
| 215 |
else |
| 216 |
get rest of bytes |
| 217 |
|
| 218 |
dca_frame (state, buf, ...) |
| 219 |
[dca_dynrng (state, ...); this is only optional] |
| 220 |
for i = 1 ... dca_blocks_num(): |
| 221 |
dca_block (state) |
| 222 |
dca_samples (state) |
| 223 |
convert samples to integer and queue to soundcard |