Raise LIBASS_VERSION, forgotten in r31293.
[mplayer/glamo.git] / libmpcodecs / ad_liba52.c
blob9b749a70ece2031df6016bcf66b09d2d0a2fe075
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"
28 #include "mp_msg.h"
29 #include "help_mp.h"
30 #include "mpbswap.h"
32 #include "ad_internal.h"
34 #include "cpudetect.h"
36 #include "libaf/af_format.h"
38 #include <a52dec/a52.h>
39 #include <a52dec/mm_accel.h>
40 int (* a52_resample) (float * _f, int16_t * s16);
42 static a52_state_t *a52_state;
43 static uint32_t a52_flags=0;
44 /** Used by a52_resample_float, it defines the mapping between liba52
45 * channels and output channels. The ith nibble from the right in the
46 * hex representation of channel_map is the index of the source
47 * channel corresponding to the ith output channel. Source channels are
48 * indexed 1-6. Silent output channels are marked by 0xf. */
49 static uint32_t channel_map;
51 #define DRC_NO_ACTION 0
52 #define DRC_NO_COMPRESSION 1
53 #define DRC_CALLBACK 2
55 /** The output is multiplied by this var. Used for volume control */
56 static sample_t a52_level = 1;
57 static int a52_drc_action = DRC_NO_ACTION;
59 static const ad_info_t info =
61 "AC3 decoding with liba52",
62 "liba52",
63 "Nick Kurshev",
64 "Michel LESPINASSE",
68 LIBAD_EXTERN(liba52)
70 static int a52_fillbuff(sh_audio_t *sh_audio)
72 int length=0;
73 int flags=0;
74 int sample_rate=0;
75 int bit_rate=0;
77 sh_audio->a_in_buffer_len=0;
78 /* sync frame:*/
79 while(1){
80 while(sh_audio->a_in_buffer_len<8){
81 int c=demux_getc(sh_audio->ds);
82 if(c<0) return -1; /* EOF*/
83 sh_audio->a_in_buffer[sh_audio->a_in_buffer_len++]=c;
85 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
86 length = a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
87 if(length>=7 && length<=3840) break; /* we're done.*/
88 /* bad file => resync*/
89 if(sh_audio->format!=0x2000) swab(sh_audio->a_in_buffer,sh_audio->a_in_buffer,8);
90 memmove(sh_audio->a_in_buffer,sh_audio->a_in_buffer+1,7);
91 --sh_audio->a_in_buffer_len;
93 mp_msg(MSGT_DECAUDIO,MSGL_DBG2,"a52: len=%d flags=0x%X %d Hz %d bit/s\n",length,flags,sample_rate,bit_rate);
94 sh_audio->samplerate=sample_rate;
95 sh_audio->i_bps=bit_rate/8;
96 sh_audio->samplesize=sh_audio->sample_format==AF_FORMAT_FLOAT_NE ? 4 : 2;
97 demux_read_data(sh_audio->ds,sh_audio->a_in_buffer+8,length-8);
98 if(sh_audio->format!=0x2000)
99 swab(sh_audio->a_in_buffer+8,sh_audio->a_in_buffer+8,length-8);
101 #ifdef CONFIG_LIBA52_INTERNAL
102 if(crc16_block(sh_audio->a_in_buffer+2,length-2)!=0)
103 mp_msg(MSGT_DECAUDIO,MSGL_STATUS,"a52: CRC check failed! \n");
104 #endif
106 return length;
109 /* returns: number of available channels*/
110 static int a52_printinfo(sh_audio_t *sh_audio){
111 int flags, sample_rate, bit_rate;
112 char* mode="unknown";
113 int channels=0;
114 a52_syncinfo (sh_audio->a_in_buffer, &flags, &sample_rate, &bit_rate);
115 switch(flags&A52_CHANNEL_MASK){
116 case A52_CHANNEL: mode="channel"; channels=2; break;
117 case A52_MONO: mode="mono"; channels=1; break;
118 case A52_STEREO: mode="stereo"; channels=2; break;
119 case A52_3F: mode="3f";channels=3;break;
120 case A52_2F1R: mode="2f+1r";channels=3;break;
121 case A52_3F1R: mode="3f+1r";channels=4;break;
122 case A52_2F2R: mode="2f+2r";channels=4;break;
123 case A52_3F2R: mode="3f+2r";channels=5;break;
124 case A52_CHANNEL1: mode="channel1"; channels=2; break;
125 case A52_CHANNEL2: mode="channel2"; channels=2; break;
126 case A52_DOLBY: mode="dolby"; channels=2; break;
128 mp_msg(MSGT_DECAUDIO,MSGL_V,"AC3: %d.%d (%s%s) %d Hz %3.1f kbit/s\n",
129 channels, (flags&A52_LFE)?1:0,
130 mode, (flags&A52_LFE)?"+lfe":"",
131 sample_rate, bit_rate*0.001f);
132 return (flags&A52_LFE) ? (channels+1) : channels;
135 static sample_t dynrng_call (sample_t c, void *data)
137 // fprintf(stderr, "(%lf, %lf): %lf\n", (double)c, (double)drc_level, (double)pow((double)c, drc_level));
138 return pow((double)c, drc_level);
142 static int preinit(sh_audio_t *sh)
144 /* Dolby AC3 audio: */
145 /* however many channels, 2 bytes in a word, 256 samples in a block, 6 blocks in a frame */
146 if (sh->samplesize < 4) sh->samplesize = 4;
147 sh->audio_out_minsize=audio_output_channels*sh->samplesize*256*6;
148 sh->audio_in_minsize=3840;
149 a52_level = 1.0;
150 return 1;
154 * \brief Function to convert the "planar" float format used by liba52
155 * into the interleaved float format used by libaf/libao2.
156 * \param in the input buffer containing the planar samples.
157 * \param out the output buffer where the interleaved result is stored.
159 static int a52_resample_float(float *in, int16_t *out)
161 unsigned long i;
162 float *p = (float*) out;
163 for (i = 0; i != 256; i++) {
164 unsigned long map = channel_map;
165 do {
166 unsigned long ch = map & 15;
167 if (ch == 15)
168 *p = 0;
169 else
170 *p = in[i + ((ch-1)<<8)];
171 p++;
172 } while ((map >>= 4));
174 return (int16_t*) p - out;
177 static int init(sh_audio_t *sh_audio)
179 uint32_t a52_accel=0;
180 sample_t level=a52_level, bias=384;
181 int flags=0;
182 /* Dolby AC3 audio:*/
183 #ifdef MM_ACCEL_X86_SSE
184 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
185 #endif
186 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
187 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
188 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
189 #ifdef MM_ACCEL_X86_3DNOWEXT
190 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
191 #endif
192 #ifdef MM_ACCEL_PPC_ALTIVEC
193 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
194 #endif
195 a52_state=a52_init (a52_accel);
196 if (a52_state == NULL) {
197 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
198 return 0;
200 sh_audio->sample_format = AF_FORMAT_FLOAT_NE;
201 if(a52_fillbuff(sh_audio)<0){
202 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
203 return 0;
207 /* Init a52 dynrng */
208 if (drc_level < 0.001) {
209 /* level == 0 --> no compression, init library without callback */
210 a52_drc_action = DRC_NO_COMPRESSION;
211 } else if (drc_level > 0.999) {
212 /* level == 1 --> full compression, do nothing at all (library default = full compression) */
213 a52_drc_action = DRC_NO_ACTION;
214 } else {
215 a52_drc_action = DRC_CALLBACK;
217 /* Library init for dynrng has to be done for each frame, see decode_audio() */
220 /* 'a52 cannot upmix' hotfix:*/
221 a52_printinfo(sh_audio);
222 sh_audio->channels=audio_output_channels;
223 while(sh_audio->channels>0){
224 switch(sh_audio->channels){
225 case 1: a52_flags=A52_MONO; break;
226 /* case 2: a52_flags=A52_STEREO; break;*/
227 case 2: a52_flags=A52_DOLBY; break;
228 /* case 3: a52_flags=A52_3F; break;*/
229 case 3: a52_flags=A52_2F1R; break;
230 case 4: a52_flags=A52_2F2R; break; /* 2+2*/
231 case 5: a52_flags=A52_3F2R; break;
232 case 6: a52_flags=A52_3F2R|A52_LFE; break; /* 5.1*/
234 /* test:*/
235 flags=a52_flags|A52_ADJUST_LEVEL;
236 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags before a52_frame: 0x%X\n",flags);
237 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
238 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: error decoding frame -> nosound\n");
239 return 0;
241 mp_msg(MSGT_DECAUDIO,MSGL_V,"A52 flags after a52_frame: 0x%X\n",flags);
242 /* frame decoded, let's init resampler:*/
243 channel_map = 0;
244 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE) {
245 if (!(flags & A52_LFE)) {
246 switch ((flags<<3) | sh_audio->channels) {
247 case (A52_MONO << 3) | 1: channel_map = 0x1; break;
248 case (A52_CHANNEL << 3) | 2:
249 case (A52_STEREO << 3) | 2:
250 case (A52_DOLBY << 3) | 2: channel_map = 0x21; break;
251 case (A52_2F1R << 3) | 3: channel_map = 0x321; break;
252 case (A52_2F2R << 3) | 4: channel_map = 0x4321; break;
253 case (A52_3F << 3) | 5: channel_map = 0x2ff31; break;
254 case (A52_3F2R << 3) | 5: channel_map = 0x25431; break;
256 } else if (sh_audio->channels == 6) {
257 switch (flags & ~A52_LFE) {
258 case A52_MONO : channel_map = 0x12ffff; break;
259 case A52_CHANNEL:
260 case A52_STEREO :
261 case A52_DOLBY : channel_map = 0x1fff32; break;
262 case A52_3F : channel_map = 0x13ff42; break;
263 case A52_2F1R : channel_map = 0x1f4432; break;
264 case A52_2F2R : channel_map = 0x1f5432; break;
265 case A52_3F2R : channel_map = 0x136542; break;
268 if (channel_map) {
269 a52_resample = a52_resample_float;
270 break;
272 } else
273 break;
275 if(sh_audio->channels<=0){
276 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"a52: no resampler. try different channel setup!\n");
277 return 0;
279 return 1;
282 static void uninit(sh_audio_t *sh)
284 a52_free(a52_state);
287 static int control(sh_audio_t *sh,int cmd,void* arg, ...)
289 switch(cmd)
291 case ADCTRL_RESYNC_STREAM:
292 case ADCTRL_SKIP_FRAME:
293 a52_fillbuff(sh);
294 return CONTROL_TRUE;
295 case ADCTRL_SET_VOLUME: {
296 float vol = *(float*)arg;
297 if (vol > 60.0) vol = 60.0;
298 a52_level = vol <= -200.0 ? 0 : pow(10.0,vol/20.0);
299 return CONTROL_TRUE;
301 case ADCTRL_QUERY_FORMAT:
302 if (*(int*)arg == AF_FORMAT_S16_NE ||
303 *(int*)arg == AF_FORMAT_FLOAT_NE)
304 return CONTROL_TRUE;
305 return CONTROL_FALSE;
307 return CONTROL_UNKNOWN;
310 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
312 sample_t level=a52_level, bias=384;
313 int flags=a52_flags|A52_ADJUST_LEVEL;
314 int i,len=-1;
315 if (sh_audio->sample_format == AF_FORMAT_FLOAT_NE)
316 bias = 0;
317 if(!sh_audio->a_in_buffer_len)
318 if(a52_fillbuff(sh_audio)<0) return len; /* EOF */
319 sh_audio->a_in_buffer_len=0;
320 if (a52_frame (a52_state, sh_audio->a_in_buffer, &flags, &level, bias)){
321 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error decoding frame\n");
322 return len;
325 /* handle dynrng */
326 if (a52_drc_action != DRC_NO_ACTION) {
327 if (a52_drc_action == DRC_NO_COMPRESSION)
328 a52_dynrng(a52_state, NULL, NULL);
329 else
330 a52_dynrng(a52_state, dynrng_call, NULL);
333 len=0;
334 for (i = 0; i < 6; i++) {
335 if (a52_block (a52_state)){
336 mp_msg(MSGT_DECAUDIO,MSGL_WARN,"a52: error at resampling\n");
337 break;
339 len+=2*a52_resample(a52_samples(a52_state),(int16_t *)&buf[len]);
341 assert(len <= maxlen);
342 return len;