options: move audio_output_channels, audio_output_format to struct
[mplayer/glamo.git] / libmpcodecs / ad_liba52.c
blobabd6a7ac5474aa65b77de4f0e4093411000368cc
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, "(%lf, %lf): %lf\n", (double)c, opts->drc_level, pow(c, opts->drc_level));
138 return pow(c, opts->drc_level);
142 static int preinit(sh_audio_t *sh)
144 struct MPOpts *opts = sh->opts;
145 /* Dolby AC3 audio: */
146 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
147 if (sh->samplesize < 4) sh->samplesize = 4;
148 sh->audio_out_minsize=opts->audio_output_channels*sh->samplesize*256*6;
149 sh->audio_in_minsize=3840;
150 a52_level = 1.0;
151 return 1;
155 * \brief Function to convert the "planar" float format used by liba52
156 * into the interleaved float format used by libaf/libao2.
157 * \param in the input buffer containing the planar samples.
158 * \param out the output buffer where the interleaved result is stored.
160 static int a52_resample_float(float *in, int16_t *out)
162 unsigned long i;
163 float *p = (float*) out;
164 for (i = 0; i != 256; i++) {
165 unsigned long map = channel_map;
166 do {
167 unsigned long ch = map & 15;
168 if (ch == 15)
169 *p = 0;
170 else
171 *p = in[i + ((ch-1)<<8)];
172 p++;
173 } while ((map >>= 4));
175 return (int16_t*) p - out;
178 static int init(sh_audio_t *sh_audio)
180 struct MPOpts *opts = sh_audio->opts;
181 uint32_t a52_accel=0;
182 sample_t level=a52_level, bias=384;
183 int flags=0;
184 /* Dolby AC3 audio:*/
185 #ifdef MM_ACCEL_X86_SSE
186 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
187 #endif
188 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
189 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
190 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
191 #ifdef MM_ACCEL_X86_3DNOWEXT
192 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
193 #endif
194 #ifdef MM_ACCEL_PPC_ALTIVEC
195 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
196 #endif
197 a52_state=a52_init (a52_accel);
198 if (a52_state == NULL) {
199 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
200 return 0;
202 sh_audio->sample_format = AF_FORMAT_FLOAT_NE;
203 if(a52_fillbuff(sh_audio)<0){
204 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
205 return 0;
208 /* Init a52 dynrng */
209 if (opts->drc_level < 0.001) {
210 /* level == 0 --> no compression, init library without callback */
211 a52_drc_action = DRC_NO_COMPRESSION;
212 } else if (opts->drc_level > 0.999 || opts->drc_level < 1.001) {
213 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
214 a52_drc_action = DRC_NO_ACTION;
215 } else {
216 a52_drc_action = DRC_CALLBACK;
218 /* Library init for dynrng has to be done for each frame, see decode_audio() */
221 /* 'a52 cannot upmix' hotfix:*/
222 a52_printinfo(sh_audio);
223 sh_audio->channels=opts->audio_output_channels;
224 while(sh_audio->channels>0){
225 switch(sh_audio->channels){
226 case 1: a52_flags=A52_MONO; break;
227 /* case 2: a52_flags=A52_STEREO; break;*/
228 case 2: a52_flags=A52_DOLBY; break;
229 /* case 3: a52_flags=A52_3F; break;*/
230 case 3: a52_flags=A52_2F1R; break;
231 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
232 case 5: a52_flags=A52_3F2R; break;
233 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
235 /* test:*/
236 flags=a52_flags|A52_ADJUST_LEVEL;
237 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
238 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
239 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
240 return 0;
242 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
243 /* frame decoded, let's init resampler:*/
244 channel_map = 0;
245 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
246 if (!(flags & A52_LFE)) {
247 switch ((flags<<3) | sh_audio->channels) {
248 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
249 case (A52_CHANNEL << 3) | 2:
250 case (A52_STEREO << 3) | 2:
251 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
252 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
253 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
254 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
255 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
257 } else if (sh_audio->channels == 6) {
258 switch (flags & ~A52_LFE) {
259 case A52_MONO : channel_map = 0x12ffff; break;
260 case A52_CHANNEL:
261 case A52_STEREO :
262 case A52_DOLBY : channel_map = 0x1fff32; break;
263 case A52_3F : channel_map = 0x13ff42; break;
264 case A52_2F1R : channel_map = 0x1f4432; break;
265 case A52_2F2R : channel_map = 0x1f5432; break;
266 case A52_3F2R : channel_map = 0x136542; break;
269 if (channel_map) {
270 a52_resample = a52_resample_float;
271 break;
273 } else
274 break;
276 if(sh_audio->channels<=0){
277 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
278 return 0;
280 return 1;
283 static void uninit(sh_audio_t *sh)
285 a52_free(a52_state);
288 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
290 switch(cmd)
292 case ADCTRL_RESYNC_STREAM:
293 case ADCTRL_SKIP_FRAME:
294 a52_fillbuff(sh);
295 return CONTROL_TRUE;
296 case ADCTRL_SET_VOLUME: {
297 float vol = *(float*)arg;
298 if (vol > 60.0) vol = 60.0;
299 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
300 return CONTROL_TRUE;
302 case ADCTRL_QUERY_FORMAT:
303 if (*(int*)arg == AF_FORMAT_S16_NE ||
304 *(int*)arg == AF_FORMAT_FLOAT_NE)
305 return CONTROL_TRUE;
306 return CONTROL_FALSE;
308 return CONTROL_UNKNOWN;
311 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
313 sample_t level=a52_level, bias=384;
314 int flags=a52_flags|A52_ADJUST_LEVEL;
315 int i,len=-1;
316 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
317 bias = 0;
318 if(!sh_audio->a_in_buffer_len)
319 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
320 sh_audio->a_in_buffer_len=0;
321 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
322 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
323 return len;
326 /* handle dynrng */
327 if (a52_drc_action != DRC_NO_ACTION) {
328 if (a52_drc_action == DRC_NO_COMPRESSION)
329 a52_dynrng(a52_state, NULL, NULL);
330 else
331 a52_dynrng(a52_state, dynrng_call, sh_audio->opts);
334 len=0;
335 for (i = 0; i < 6; i++) {
336 if (a52_block (a52_state)){
337 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
338 break;
340 len+=2*a52_resample(a52_samples(a52_state),(int16_t *)&buf[len]);
342 assert(len <= maxlen);
343 return len;