typo fixes
[mplayer/greg.git] / libmpcodecs / ad_liba52.c
blobdbdb89a6197ab486f36ec2bbe50fab8a36fe68e3
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <math.h>
5 #include <assert.h>
7 #include "config.h"
8 #ifdef USE_LIBA52
10 #include "mp_msg.h"
11 #include "help_mp.h"
13 #include "ad_internal.h"
15 #include "cpudetect.h"
17 #include "libaf/af_format.h"
19 #include "liba52/a52.h"
20 #include "liba52/mm_accel.h"
22 static sample_t * a52_samples;
23 static a52_state_t a52_state;
24 static uint32_t a52_flags=0;
25 /** Used by a52_resample_float, it defines the mapping between liba52
26 * channels and output channels. The ith nibble from the right in the
27 * hex representation of channel_map is the index of the source
28 * channel corresponding to the ith output channel. Source channels are
29 * indexed 1-6. Silent output channels are marked by 0xf. */
30 static uint32_t channel_map;
32 #define DRC_NO_ACTION 0
33 #define DRC_NO_COMPRESSION 1
34 #define DRC_CALLBACK 2
36 /** The output is multiplied by this var. Used for volume control */
37 static sample_t a52_level = 1;
38 /** The value of the -a52drc switch. */
39 float a52_drc_level = 1.0;
40 static int a52_drc_action = DRC_NO_ACTION;
42 #include "bswap.h"
44 static ad_info_t info =
46 "AC3 decoding with liba52",
47 "liba52",
48 "Nick Kurshev",
49 "Michel LESPINASSE",
53 LIBAD_EXTERN(liba52)
55 extern int audio_output_channels;
57 int a52_fillbuff(sh_audio_t *sh_audio){
58 int length=0;
59 int flags=0;
60 int sample_rate=0;
61 int bit_rate=0;
63 sh_audio->a_in_buffer_len=0;
64 /* sync frame:*/
65 while(1){
66 while(sh_audio->a_in_buffer_len<8){
67 int c=demux_getc(sh_audio->ds);
68 if(c<0) return -1; /* EOF*/
69 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++]=c;
71 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
72 length = a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
73 if(length>=7 && length<=3840) break; /* we're done.*/
74 /* bad file => resync*/
75 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
76 memmove(sh_audio->a_in_buffer,sh_audio->a_in_buffer+1,7);
77 --sh_audio->a_in_buffer_len;
79 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length,flags,sample_rate,bit_rate);
80 sh_audio->samplerate=sample_rate;
81 sh_audio->i_bps=bit_rate/8;
82 sh_audio->samplesize=sh_audio->sample_format==AF_FORMAT_FLOAT_NE ? 4 : 2;
83 demux_read_data(sh_audio->ds,sh_audio->a_in_buffer+8,length-8);
84 if(sh_audio->format!=0x2000)
85 swab(sh_audio->a_in_buffer+8,sh_audio->a_in_buffer+8,length-8);
87 if(crc16_block(sh_audio->a_in_buffer+2,length-2)!=0)
88 mp_msg(MSGT_DECAUDIO,MSGL_STATUS,"a52: CRC check failed! \n");
90 return length;
93 /* returns: number of available channels*/
94 static int a52_printinfo(sh_audio_t *sh_audio){
95 int flags, sample_rate, bit_rate;
96 char* mode="unknown";
97 int channels=0;
98 a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
99 switch(flags&A52_CHANNEL_MASK){
100 case A52_CHANNEL: mode="channel"; channels=2; break;
101 case A52_MONO: mode="mono"; channels=1; break;
102 case A52_STEREO: mode="stereo"; channels=2; break;
103 case A52_3F: mode="3f";channels=3;break;
104 case A52_2F1R: mode="2f+1r";channels=3;break;
105 case A52_3F1R: mode="3f+1r";channels=4;break;
106 case A52_2F2R: mode="2f+2r";channels=4;break;
107 case A52_3F2R: mode="3f+2r";channels=5;break;
108 case A52_CHANNEL1: mode="channel1"; channels=2; break;
109 case A52_CHANNEL2: mode="channel2"; channels=2; break;
110 case A52_DOLBY: mode="dolby"; channels=2; break;
112 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
113 channels, (flags&A52_LFE)?1:0,
114 mode, (flags&A52_LFE)?"+lfe":"",
115 sample_rate, bit_rate*0.001f);
116 return (flags&A52_LFE) ? (channels+1) : channels;
119 sample_t dynrng_call (sample_t c, void *data) {
120 // fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, (double)a52_drc_level, (double)pow((double)c, a52_drc_level));
121 return pow((double)c, a52_drc_level);
125 static int preinit(sh_audio_t *sh)
127 /* Dolby AC3 audio: */
128 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
129 if (sh->samplesize < 2) sh->samplesize = 2;
130 sh->audio_out_minsize=audio_output_channels*sh->samplesize*256*6;
131 sh->audio_in_minsize=3840;
132 a52_level = 1.0;
133 return 1;
137 * \brief Function to convert the "planar" float format used by liba52
138 * into the interleaved float format used by libaf/libao2.
139 * \param in the input buffer containing the planar samples.
140 * \param out the output buffer where the interleaved result is stored.
142 static int a52_resample_float(float *in, int16_t *out)
144 unsigned long i;
145 float *p = (float*) out;
146 for (i = 0; i != 256; i++) {
147 unsigned long map = channel_map;
148 do {
149 unsigned long ch = map & 15;
150 if (ch == 15)
151 *p = 0;
152 else
153 *p = in[i + ((ch-1)<<8)];
154 p++;
155 } while ((map >>= 4));
157 return (int16_t*) p - out;
160 static int init(sh_audio_t *sh_audio)
162 uint32_t a52_accel=0;
163 sample_t level=a52_level, bias=384;
164 int flags=0;
165 /* Dolby AC3 audio:*/
166 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
167 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
168 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
169 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
170 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
171 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
172 a52_samples=a52_init (a52_accel);
173 if (a52_samples == NULL) {
174 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
175 return 0;
177 if(a52_fillbuff(sh_audio)<0){
178 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
179 return 0;
183 /* Init a52 dynrng */
184 if (a52_drc_level < 0.001) {
185 /* level == 0 --> no compression, init library without callback */
186 a52_drc_action = DRC_NO_COMPRESSION;
187 } else if (a52_drc_level > 0.999) {
188 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
189 a52_drc_action = DRC_NO_ACTION;
190 } else {
191 a52_drc_action = DRC_CALLBACK;
193 /* Library init for dynrng has to be done for each frame, see decode_audio() */
196 /* 'a52 cannot upmix' hotfix:*/
197 a52_printinfo(sh_audio);
198 sh_audio->channels=audio_output_channels;
199 while(sh_audio->channels>0){
200 switch(sh_audio->channels){
201 case 1: a52_flags=A52_MONO; break;
202 /* case 2: a52_flags=A52_STEREO; break;*/
203 case 2: a52_flags=A52_DOLBY; break;
204 /* case 3: a52_flags=A52_3F; break;*/
205 case 3: a52_flags=A52_2F1R; break;
206 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
207 case 5: a52_flags=A52_3F2R; break;
208 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
210 /* test:*/
211 flags=a52_flags|A52_ADJUST_LEVEL;
212 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
213 if (a52_frame (&a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
214 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
215 return 0;
217 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
218 /* frame decoded, let's init resampler:*/
219 channel_map = 0;
220 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
221 if (!(flags & A52_LFE)) {
222 switch ((flags<<3) | sh_audio->channels) {
223 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
224 case (A52_CHANNEL << 3) | 2:
225 case (A52_STEREO << 3) | 2:
226 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
227 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
228 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
229 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
230 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
232 } else if (sh_audio->channels == 6) {
233 switch (flags & ~A52_LFE) {
234 case A52_MONO : channel_map = 0x12ffff; break;
235 case A52_CHANNEL:
236 case A52_STEREO :
237 case A52_DOLBY : channel_map = 0x1fff32; break;
238 case A52_3F : channel_map = 0x13ff42; break;
239 case A52_2F1R : channel_map = 0x1f4432; break;
240 case A52_2F2R : channel_map = 0x1f5432; break;
241 case A52_3F2R : channel_map = 0x136542; break;
244 if (channel_map) {
245 a52_resample = a52_resample_float;
246 break;
248 } else
249 if(a52_resample_init(a52_accel,flags,sh_audio->channels)) break;
250 --sh_audio->channels; /* try to decrease no. of channels*/
252 if(sh_audio->channels<=0){
253 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
254 return 0;
256 return 1;
259 static void uninit(sh_audio_t *sh)
263 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
265 switch(cmd)
267 case ADCTRL_RESYNC_STREAM:
268 case ADCTRL_SKIP_FRAME:
269 a52_fillbuff(sh);
270 return CONTROL_TRUE;
271 case ADCTRL_SET_VOLUME: {
272 float vol = *(float*)arg;
273 if (vol > 60.0) vol = 60.0;
274 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
275 return CONTROL_TRUE;
277 case ADCTRL_QUERY_FORMAT:
278 if (*(int*)arg == AF_FORMAT_S16_NE ||
279 *(int*)arg == AF_FORMAT_FLOAT_NE)
280 return CONTROL_TRUE;
281 return CONTROL_FALSE;
283 return CONTROL_UNKNOWN;
286 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
288 sample_t level=a52_level, bias=384;
289 int flags=a52_flags|A52_ADJUST_LEVEL;
290 int i,len=-1;
291 if (maxlen / sh_audio->samplesize / 256 / sh_audio->channels < 6) {
292 mp_msg(MSGT_DECAUDIO, MSGL_V, "maxlen too small in decode_audio\n");
293 return len;
295 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
296 bias = 0;
297 if(!sh_audio->a_in_buffer_len)
298 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
299 sh_audio->a_in_buffer_len=0;
300 if (a52_frame (&a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
301 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
302 return len;
305 /* handle dynrng */
306 if (a52_drc_action != DRC_NO_ACTION) {
307 if (a52_drc_action == DRC_NO_COMPRESSION)
308 a52_dynrng(&a52_state, NULL, NULL);
309 else
310 a52_dynrng(&a52_state, dynrng_call, NULL);
313 len=0;
314 for (i = 0; i < 6; i++) {
315 if (a52_block (&a52_state, a52_samples)){
316 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
317 break;
319 len+=2*a52_resample(a52_samples,(int16_t *)&buf[len]);
321 assert(len <= maxlen);
322 return len;
324 #endif