cosmetics: make some arguments const, "unsigned char"->uint8_t
[mplayer/glamo.git] / libmpcodecs / ad_liba52.c
blob45126a0786b2d6d9bad69cdc9f8f8fdaea94be19
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #define _XOPEN_SOURCE 600
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <math.h>
24 #include <assert.h>
26 #include "config.h"
27 #include "options.h"
28 #include "mp_msg.h"
29 #include "mpbswap.h"
31 #include "ad_internal.h"
33 #include "cpudetect.h"
35 #include "libaf/af_format.h"
37 #include <a52dec/a52.h>
38 #include <a52dec/mm_accel.h>
39 int (* a52_resample) (float * _f, int16_t * s16);
41 static a52_state_t *a52_state;
42 static uint32_t a52_flags=0;
43 /** Used by a52_resample_float, it defines the mapping between liba52
44 * channels and output channels. The ith nibble from the right in the
45 * hex representation of channel_map is the index of the source
46 * channel corresponding to the ith output channel. Source channels are
47 * indexed 1-6. Silent output channels are marked by 0xf. */
48 static uint32_t channel_map;
50 #define DRC_NO_ACTION 0
51 #define DRC_NO_COMPRESSION 1
52 #define DRC_CALLBACK 2
54 /** The output is multiplied by this var. Used for volume control */
55 static sample_t a52_level = 1;
56 static int a52_drc_action = DRC_NO_ACTION;
58 static const ad_info_t info =
60 "AC3 decoding with liba52",
61 "liba52",
62 "Nick Kurshev",
63 "Michel LESPINASSE",
67 LIBAD_EXTERN(liba52)
69 static int a52_fillbuff(sh_audio_t *sh_audio)
71 int length=0;
72 int flags=0;
73 int sample_rate=0;
74 int bit_rate=0;
76 sh_audio->a_in_buffer_len=0;
77 /* sync frame:*/
78 while(1){
79 while(sh_audio->a_in_buffer_len<8){
80 int c=demux_getc(sh_audio->ds);
81 if(c<0) return -1; /* EOF*/
82 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++]=c;
84 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
85 length = a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
86 if(length>=7 && length<=3840) break; /* we're done.*/
87 /* bad file => resync*/
88 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
89 memmove(sh_audio->a_in_buffer,sh_audio->a_in_buffer+1,7);
90 --sh_audio->a_in_buffer_len;
92 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length,flags,sample_rate,bit_rate);
93 sh_audio->samplerate=sample_rate;
94 sh_audio->i_bps=bit_rate/8;
95 sh_audio->samplesize=sh_audio->sample_format==AF_FORMAT_FLOAT_NE ? 4 : 2;
96 demux_read_data(sh_audio->ds,sh_audio->a_in_buffer+8,length-8);
97 if(sh_audio->format!=0x2000)
98 swab(sh_audio->a_in_buffer+8,sh_audio->a_in_buffer+8,length-8);
100 #ifdef CONFIG_LIBA52_INTERNAL
101 if(crc16_block(sh_audio->a_in_buffer+2,length-2)!=0)
102 mp_msg(MSGT_DECAUDIO,MSGL_STATUS,"a52: CRC check failed! \n");
103 #endif
105 return length;
108 /* returns: number of available channels*/
109 static int a52_printinfo(sh_audio_t *sh_audio){
110 int flags, sample_rate, bit_rate;
111 char* mode="unknown";
112 int channels=0;
113 a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
114 switch(flags&A52_CHANNEL_MASK){
115 case A52_CHANNEL: mode="channel"; channels=2; break;
116 case A52_MONO: mode="mono"; channels=1; break;
117 case A52_STEREO: mode="stereo"; channels=2; break;
118 case A52_3F: mode="3f";channels=3;break;
119 case A52_2F1R: mode="2f+1r";channels=3;break;
120 case A52_3F1R: mode="3f+1r";channels=4;break;
121 case A52_2F2R: mode="2f+2r";channels=4;break;
122 case A52_3F2R: mode="3f+2r";channels=5;break;
123 case A52_CHANNEL1: mode="channel1"; channels=2; break;
124 case A52_CHANNEL2: mode="channel2"; channels=2; break;
125 case A52_DOLBY: mode="dolby"; channels=2; break;
127 mp_msg(MSGT_DECAUDIO,MSGL_V,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
128 channels, (flags&A52_LFE)?1:0,
129 mode, (flags&A52_LFE)?"+lfe":"",
130 sample_rate, bit_rate*0.001f);
131 return (flags&A52_LFE) ? (channels+1) : channels;
134 static sample_t dynrng_call (sample_t c, void *data)
136 struct MPOpts *opts = data;
137 //fprintf(stderr, "(%f, %f): %f\n", (double)c, (double)drc_level, (double)pow((double)c, drc_level));
138 //fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, opts->drc_level, pow(c, opts->drc_level));
139 return pow(c, opts->drc_level);
143 static int preinit(sh_audio_t *sh)
145 struct MPOpts *opts = sh->opts;
146 /* Dolby AC3 audio: */
147 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
148 if (sh->samplesize < 4) sh->samplesize = 4;
149 sh->audio_out_minsize=opts->audio_output_channels*sh->samplesize*256*6;
150 sh->audio_in_minsize=3840;
151 a52_level = 1.0;
152 return 1;
156 * \brief Function to convert the "planar" float format used by liba52
157 * into the interleaved float format used by libaf/libao2.
158 * \param in the input buffer containing the planar samples.
159 * \param out the output buffer where the interleaved result is stored.
161 static int a52_resample_float(float *in, int16_t *out)
163 unsigned long i;
164 float *p = (float*) out;
165 for (i = 0; i != 256; i++) {
166 unsigned long map = channel_map;
167 do {
168 unsigned long ch = map & 15;
169 if (ch == 15)
170 *p = 0;
171 else
172 *p = in[i + ((ch-1)<<8)];
173 p++;
174 } while ((map >>= 4));
176 return (int16_t*) p - out;
179 static int init(sh_audio_t *sh_audio)
181 struct MPOpts *opts = sh_audio->opts;
182 uint32_t a52_accel=0;
183 sample_t level=a52_level, bias=384;
184 int flags=0;
185 /* Dolby AC3 audio:*/
186 #ifdef MM_ACCEL_X86_SSE
187 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
188 #endif
189 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
190 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
191 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
192 #ifdef MM_ACCEL_X86_3DNOWEXT
193 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
194 #endif
195 #ifdef MM_ACCEL_PPC_ALTIVEC
196 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
197 #endif
198 a52_state=a52_init (a52_accel);
199 if (a52_state == NULL) {
200 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
201 return 0;
203 sh_audio->sample_format = AF_FORMAT_FLOAT_NE;
204 if(a52_fillbuff(sh_audio)<0){
205 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
206 return 0;
209 /* Init a52 dynrng */
210 if (opts->drc_level < 0.001) {
211 /* level == 0 --> no compression, init library without callback */
212 a52_drc_action = DRC_NO_COMPRESSION;
213 } else if (opts->drc_level > 0.999 && opts->drc_level < 1.001) {
214 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
215 a52_drc_action = DRC_NO_ACTION;
216 } else {
217 a52_drc_action = DRC_CALLBACK;
219 /* Library init for dynrng has to be done for each frame, see decode_audio() */
222 /* 'a52 cannot upmix' hotfix:*/
223 a52_printinfo(sh_audio);
224 sh_audio->channels=opts->audio_output_channels;
225 while(sh_audio->channels>0){
226 switch(sh_audio->channels){
227 case 1: a52_flags=A52_MONO; break;
228 /* case 2: a52_flags=A52_STEREO; break;*/
229 case 2: a52_flags=A52_DOLBY; break;
230 /* case 3: a52_flags=A52_3F; break;*/
231 case 3: a52_flags=A52_2F1R; break;
232 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
233 case 5: a52_flags=A52_3F2R; break;
234 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
236 /* test:*/
237 flags=a52_flags|A52_ADJUST_LEVEL;
238 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
239 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
240 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
241 return 0;
243 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
244 /* frame decoded, let's init resampler:*/
245 channel_map = 0;
246 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
247 if (!(flags & A52_LFE)) {
248 switch ((flags<<3) | sh_audio->channels) {
249 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
250 case (A52_CHANNEL << 3) | 2:
251 case (A52_STEREO << 3) | 2:
252 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
253 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
254 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
255 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
256 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
258 } else if (sh_audio->channels == 6) {
259 switch (flags & ~A52_LFE) {
260 case A52_MONO : channel_map = 0x12ffff; break;
261 case A52_CHANNEL:
262 case A52_STEREO :
263 case A52_DOLBY : channel_map = 0x1fff32; break;
264 case A52_3F : channel_map = 0x13ff42; break;
265 case A52_2F1R : channel_map = 0x1f4432; break;
266 case A52_2F2R : channel_map = 0x1f5432; break;
267 case A52_3F2R : channel_map = 0x136542; break;
270 if (channel_map) {
271 a52_resample = a52_resample_float;
272 break;
274 } else
275 break;
277 if(sh_audio->channels<=0){
278 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
279 return 0;
281 return 1;
284 static void uninit(sh_audio_t *sh)
286 a52_free(a52_state);
289 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
291 switch(cmd)
293 case ADCTRL_RESYNC_STREAM:
294 case ADCTRL_SKIP_FRAME:
295 a52_fillbuff(sh);
296 return CONTROL_TRUE;
297 case ADCTRL_SET_VOLUME: {
298 float vol = *(float*)arg;
299 if (vol > 60.0) vol = 60.0;
300 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
301 return CONTROL_TRUE;
303 case ADCTRL_QUERY_FORMAT:
304 if (*(int*)arg == AF_FORMAT_S16_NE ||
305 *(int*)arg == AF_FORMAT_FLOAT_NE)
306 return CONTROL_TRUE;
307 return CONTROL_FALSE;
309 return CONTROL_UNKNOWN;
312 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
314 sample_t level=a52_level, bias=384;
315 int flags=a52_flags|A52_ADJUST_LEVEL;
316 int i,len=-1;
317 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
318 bias = 0;
319 if(!sh_audio->a_in_buffer_len)
320 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
321 sh_audio->a_in_buffer_len=0;
322 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
323 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
324 return len;
327 /* handle dynrng */
328 if (a52_drc_action != DRC_NO_ACTION) {
329 if (a52_drc_action == DRC_NO_COMPRESSION)
330 a52_dynrng(a52_state, NULL, NULL);
331 else
332 a52_dynrng(a52_state, dynrng_call, sh_audio->opts);
335 len=0;
336 for (i = 0; i < 6; i++) {
337 if (a52_block (a52_state)){
338 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
339 break;
341 len+=2*a52_resample(a52_samples(a52_state),(int16_t *)&buf[len]);
343 assert(len <= maxlen);
344 return len;