vo_gl2: Use identical code to -vo gl for -wid mode
[mplayer/glamo.git] / libmpcodecs / ad_liba52.c
blob0cc282776dc13c73cb2a03507f2ea2ce7256e98a
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 /* 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 struct MPOpts *opts = sh_audio->opts;
180 uint32_t a52_accel=0;
181 sample_t level=a52_level, bias=384;
182 int flags=0;
183 /* Dolby AC3 audio:*/
184 #ifdef MM_ACCEL_X86_SSE
185 if(gCpuCaps.hasSSE) a52_accel|=MM_ACCEL_X86_SSE;
186 #endif
187 if(gCpuCaps.hasMMX) a52_accel|=MM_ACCEL_X86_MMX;
188 if(gCpuCaps.hasMMX2) a52_accel|=MM_ACCEL_X86_MMXEXT;
189 if(gCpuCaps.has3DNow) a52_accel|=MM_ACCEL_X86_3DNOW;
190 #ifdef MM_ACCEL_X86_3DNOWEXT
191 if(gCpuCaps.has3DNowExt) a52_accel|=MM_ACCEL_X86_3DNOWEXT;
192 #endif
193 #ifdef MM_ACCEL_PPC_ALTIVEC
194 if(gCpuCaps.hasAltiVec) a52_accel|=MM_ACCEL_PPC_ALTIVEC;
195 #endif
196 a52_state=a52_init (a52_accel);
197 if (a52_state == NULL) {
198 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 init failed\n");
199 return 0;
201 sh_audio->sample_format = AF_FORMAT_FLOAT_NE;
202 if(a52_fillbuff(sh_audio)<0){
203 mp_msg(MSGT_DECAUDIO,MSGL_ERR,"A52 sync failed\n");
204 return 0;
207 /* Init a52 dynrng */
208 if (opts->drc_level < 0.001) {
209 /* level == 0 --> no compression, init library without callback */
210 a52_drc_action = DRC_NO_COMPRESSION;
211 } else if (opts->drc_level > 0.999 || opts->drc_level < 1.001) {
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, sh_audio->opts);
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;