Make various functions static
[mplayer.git] / libmpcodecs / ad_liba52.c
blob5f3e48ae2c306c7c67c1e79242a77ca52f3eb3e0
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <math.h>
5 #include <assert.h>
7 #include "config.h"
9 #include "mp_msg.h"
10 #include "help_mp.h"
11 #include "mpbswap.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 a52_state_t *a52_state;
23 static uint32_t a52_flags=0;
24 /** Used by a52_resample_float, it defines the mapping between liba52
25 * channels and output channels. The ith nibble from the right in the
26 * hex representation of channel_map is the index of the source
27 * channel corresponding to the ith output channel. Source channels are
28 * indexed 1-6. Silent output channels are marked by 0xf. */
29 static uint32_t channel_map;
31 #define DRC_NO_ACTION 0
32 #define DRC_NO_COMPRESSION 1
33 #define DRC_CALLBACK 2
35 /** The output is multiplied by this var. Used for volume control */
36 static sample_t a52_level = 1;
37 /** The value of the -a52drc switch. */
38 float a52_drc_level = 1.0;
39 static int a52_drc_action = DRC_NO_ACTION;
41 static const ad_info_t info =
43 "AC3 decoding with liba52",
44 "liba52",
45 "Nick Kurshev",
46 "Michel LESPINASSE",
50 LIBAD_EXTERN(liba52)
52 static int a52_fillbuff(sh_audio_t *sh_audio){
53 int length=0;
54 int flags=0;
55 int sample_rate=0;
56 int bit_rate=0;
58 sh_audio->a_in_buffer_len=0;
59 /* sync frame:*/
60 while(1){
61 while(sh_audio->a_in_buffer_len<8){
62 int c=demux_getc(sh_audio->ds);
63 if(c<0) return -1; /* EOF*/
64 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++]=c;
66 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
67 length = a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
68 if(length>=7 && length<=3840) break; /* we're done.*/
69 /* bad file => resync*/
70 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
71 memmove(sh_audio->a_in_buffer,sh_audio->a_in_buffer+1,7);
72 --sh_audio->a_in_buffer_len;
74 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length,flags,sample_rate,bit_rate);
75 sh_audio->samplerate=sample_rate;
76 sh_audio->i_bps=bit_rate/8;
77 sh_audio->samplesize=sh_audio->sample_format==AF_FORMAT_FLOAT_NE ? 4 : 2;
78 demux_read_data(sh_audio->ds,sh_audio->a_in_buffer+8,length-8);
79 if(sh_audio->format!=0x2000)
80 swab(sh_audio->a_in_buffer+8,sh_audio->a_in_buffer+8,length-8);
82 if(crc16_block(sh_audio->a_in_buffer+2,length-2)!=0)
83 mp_msg(MSGT_DECAUDIO,MSGL_STATUS,"a52: CRC check failed! \n");
85 return length;
88 /* returns: number of available channels*/
89 static int a52_printinfo(sh_audio_t *sh_audio){
90 int flags, sample_rate, bit_rate;
91 char* mode="unknown";
92 int channels=0;
93 a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
94 switch(flags&A52_CHANNEL_MASK){
95 case A52_CHANNEL: mode="channel"; channels=2; break;
96 case A52_MONO: mode="mono"; channels=1; break;
97 case A52_STEREO: mode="stereo"; channels=2; break;
98 case A52_3F: mode="3f";channels=3;break;
99 case A52_2F1R: mode="2f+1r";channels=3;break;
100 case A52_3F1R: mode="3f+1r";channels=4;break;
101 case A52_2F2R: mode="2f+2r";channels=4;break;
102 case A52_3F2R: mode="3f+2r";channels=5;break;
103 case A52_CHANNEL1: mode="channel1"; channels=2; break;
104 case A52_CHANNEL2: mode="channel2"; channels=2; break;
105 case A52_DOLBY: mode="dolby"; channels=2; break;
107 mp_msg(MSGT_DECAUDIO,MSGL_V,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
108 channels, (flags&A52_LFE)?1:0,
109 mode, (flags&A52_LFE)?"+lfe":"",
110 sample_rate, bit_rate*0.001f);
111 return (flags&A52_LFE) ? (channels+1) : channels;
114 static sample_t dynrng_call (sample_t c, void *data) {
115 // fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, (double)a52_drc_level, (double)pow((double)c, a52_drc_level));
116 return pow((double)c, a52_drc_level);
120 static int preinit(sh_audio_t *sh)
122 /* Dolby AC3 audio: */
123 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
124 if (sh->samplesize < 2) sh->samplesize = 2;
125 sh->audio_out_minsize=audio_output_channels*sh->samplesize*256*6;
126 sh->audio_in_minsize=3840;
127 a52_level = 1.0;
128 return 1;
132 * \brief Function to convert the "planar" float format used by liba52
133 * into the interleaved float format used by libaf/libao2.
134 * \param in the input buffer containing the planar samples.
135 * \param out the output buffer where the interleaved result is stored.
137 static int a52_resample_float(float *in, int16_t *out)
139 unsigned long i;
140 float *p = (float*) out;
141 for (i = 0; i != 256; i++) {
142 unsigned long map = channel_map;
143 do {
144 unsigned long ch = map & 15;
145 if (ch == 15)
146 *p = 0;
147 else
148 *p = in[i + ((ch-1)<<8)];
149 p++;
150 } while ((map >>= 4));
152 return (int16_t*) p - out;
155 static int init(sh_audio_t *sh_audio)
157 uint32_t a52_accel=0;
158 sample_t level=a52_level, bias=384;
159 int flags=0;
160 /* Dolby AC3 audio:*/
161 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
162 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
163 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
164 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
165 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
166 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
167 a52_state=a52_init (a52_accel);
168 if (a52_state == NULL) {
169 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
170 return 0;
172 if(a52_fillbuff(sh_audio)<0){
173 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
174 return 0;
178 /* Init a52 dynrng */
179 if (a52_drc_level < 0.001) {
180 /* level == 0 --> no compression, init library without callback */
181 a52_drc_action = DRC_NO_COMPRESSION;
182 } else if (a52_drc_level > 0.999) {
183 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
184 a52_drc_action = DRC_NO_ACTION;
185 } else {
186 a52_drc_action = DRC_CALLBACK;
188 /* Library init for dynrng has to be done for each frame, see decode_audio() */
191 /* 'a52 cannot upmix' hotfix:*/
192 a52_printinfo(sh_audio);
193 sh_audio->channels=audio_output_channels;
194 while(sh_audio->channels>0){
195 switch(sh_audio->channels){
196 case 1: a52_flags=A52_MONO; break;
197 /* case 2: a52_flags=A52_STEREO; break;*/
198 case 2: a52_flags=A52_DOLBY; break;
199 /* case 3: a52_flags=A52_3F; break;*/
200 case 3: a52_flags=A52_2F1R; break;
201 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
202 case 5: a52_flags=A52_3F2R; break;
203 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
205 /* test:*/
206 flags=a52_flags|A52_ADJUST_LEVEL;
207 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
208 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
209 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
210 return 0;
212 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
213 /* frame decoded, let's init resampler:*/
214 channel_map = 0;
215 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
216 if (!(flags & A52_LFE)) {
217 switch ((flags<<3) | sh_audio->channels) {
218 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
219 case (A52_CHANNEL << 3) | 2:
220 case (A52_STEREO << 3) | 2:
221 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
222 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
223 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
224 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
225 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
227 } else if (sh_audio->channels == 6) {
228 switch (flags & ~A52_LFE) {
229 case A52_MONO : channel_map = 0x12ffff; break;
230 case A52_CHANNEL:
231 case A52_STEREO :
232 case A52_DOLBY : channel_map = 0x1fff32; break;
233 case A52_3F : channel_map = 0x13ff42; break;
234 case A52_2F1R : channel_map = 0x1f4432; break;
235 case A52_2F2R : channel_map = 0x1f5432; break;
236 case A52_3F2R : channel_map = 0x136542; break;
239 if (channel_map) {
240 a52_resample = a52_resample_float;
241 break;
243 } else
244 if(a52_resample_init(a52_accel,flags,sh_audio->channels)) break;
245 --sh_audio->channels; /* try to decrease no. of channels*/
247 if(sh_audio->channels<=0){
248 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
249 return 0;
251 return 1;
254 static void uninit(sh_audio_t *sh)
256 a52_free(a52_state);
259 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
261 switch(cmd)
263 case ADCTRL_RESYNC_STREAM:
264 case ADCTRL_SKIP_FRAME:
265 a52_fillbuff(sh);
266 return CONTROL_TRUE;
267 case ADCTRL_SET_VOLUME: {
268 float vol = *(float*)arg;
269 if (vol > 60.0) vol = 60.0;
270 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
271 return CONTROL_TRUE;
273 case ADCTRL_QUERY_FORMAT:
274 if (*(int*)arg == AF_FORMAT_S16_NE ||
275 *(int*)arg == AF_FORMAT_FLOAT_NE)
276 return CONTROL_TRUE;
277 return CONTROL_FALSE;
279 return CONTROL_UNKNOWN;
282 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
284 sample_t level=a52_level, bias=384;
285 int flags=a52_flags|A52_ADJUST_LEVEL;
286 int i,len=-1;
287 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
288 bias = 0;
289 if(!sh_audio->a_in_buffer_len)
290 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
291 sh_audio->a_in_buffer_len=0;
292 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
293 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
294 return len;
297 /* handle dynrng */
298 if (a52_drc_action != DRC_NO_ACTION) {
299 if (a52_drc_action == DRC_NO_COMPRESSION)
300 a52_dynrng(a52_state, NULL, NULL);
301 else
302 a52_dynrng(a52_state, dynrng_call, NULL);
305 len=0;
306 for (i = 0; i < 6; i++) {
307 if (a52_block (a52_state)){
308 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
309 break;
311 len+=2*a52_resample(a52_samples(a52_state),(int16_t *)&buf[len]);
313 assert(len <= maxlen);
314 return len;