296
|
1
|
|
2 int init_audio(sh_audio_t *sh_audio){
|
303
|
3
|
|
4 int driver=sh_audio->codec->driver;
|
296
|
5
|
|
6 sh_audio->samplesize=2;
|
303
|
7 sh_audio->samplerate=0;
|
296
|
8 sh_audio->pcm_bswap=0;
|
303
|
9
|
296
|
10 sh_audio->a_buffer_size=16384; // default size, maybe not enough for Win32/ACM
|
303
|
11 sh_audio->a_buffer=NULL;
|
296
|
12
|
303
|
13 if(driver==4){
|
296
|
14 // Win32 ACM audio codec:
|
|
15 if(init_acm_audio_codec(sh_audio)){
|
|
16 sh_audio->channels=sh_audio->o_wf.nChannels;
|
|
17 sh_audio->samplerate=sh_audio->o_wf.nSamplesPerSec;
|
|
18 if(sh_audio->a_buffer_size<sh_audio->audio_out_minsize+OUTBURST)
|
|
19 sh_audio->a_buffer_size=sh_audio->audio_out_minsize+OUTBURST;
|
|
20 } else {
|
|
21 printf("Could not load/initialize Win32/ACM AUDIO codec (missing DLL file?)\n");
|
303
|
22 driver=0;
|
296
|
23 }
|
|
24 }
|
|
25
|
303
|
26 if(driver==7){
|
296
|
27 #ifndef USE_DIRECTSHOW
|
|
28 printf("Compiled without DirectShow support -> force nosound :(\n");
|
303
|
29 driver=0;
|
296
|
30 #else
|
|
31 // Win32 DShow audio codec:
|
340
|
32 // printf("DShow_audio: channs=%d rate=%d\n",sh_audio->channels,sh_audio->samplerate);
|
296
|
33
|
303
|
34 if(DS_AudioDecoder_Open(sh_audio->codec->dll,&sh_audio->codec->guid,&sh_audio->wf)){
|
|
35 printf("ERROR: Could not load/initialize Win32/DirctShow AUDIO codec: %s\n",sh_audio->codec->dll);
|
|
36 driver=0;
|
|
37 } else {
|
|
38 sh_audio->channels=sh_audio->wf.nChannels;
|
|
39 sh_audio->samplerate=sh_audio->wf.nSamplesPerSec;
|
|
40 sh_audio->audio_in_minsize=2*sh_audio->wf.nBlockAlign;
|
296
|
41 if(sh_audio->audio_in_minsize<8192) sh_audio->audio_in_minsize=8192;
|
|
42 sh_audio->a_in_buffer_size=sh_audio->audio_in_minsize;
|
|
43 sh_audio->a_in_buffer=malloc(sh_audio->a_in_buffer_size);
|
|
44 sh_audio->a_in_buffer_len=0;
|
|
45 }
|
|
46 #endif
|
|
47 }
|
|
48
|
303
|
49 if(!driver) return 0;
|
296
|
50
|
|
51 // allocate audio out buffer:
|
|
52 sh_audio->a_buffer=malloc(sh_audio->a_buffer_size);
|
|
53 memset(sh_audio->a_buffer,0,sh_audio->a_buffer_size);
|
|
54 sh_audio->a_buffer_len=0;
|
|
55
|
303
|
56 switch(driver){
|
|
57 case 4: {
|
296
|
58 int ret=acm_decode_audio(sh_audio,sh_audio->a_buffer,sh_audio->a_buffer_size);
|
|
59 if(ret<0){
|
303
|
60 printf("ACM decoding error: %d\n",ret);
|
|
61 driver=0;
|
296
|
62 }
|
303
|
63 break;
|
296
|
64 }
|
303
|
65 case 2: {
|
296
|
66 // if(file_format==DEMUXER_TYPE_AVI){ // FIXME!!!!!!!
|
|
67 // AVI PCM Audio:
|
|
68 WAVEFORMATEX *h=&sh_audio->wf;
|
|
69 sh_audio->channels=h->nChannels;
|
|
70 sh_audio->samplerate=h->nSamplesPerSec;
|
|
71 sh_audio->samplesize=(h->wBitsPerSample+7)/8;
|
|
72 // } else {
|
|
73 // // DVD PCM audio:
|
|
74 // sh_audio->channels=2;
|
|
75 // sh_audio->samplerate=48000;
|
|
76 // sh_audio->pcm_bswap=1;
|
|
77 // }
|
303
|
78 break;
|
|
79 }
|
|
80 case 3: {
|
296
|
81 // Dolby AC3 audio:
|
|
82 ac3_config.fill_buffer_callback = ac3_fill_buffer;
|
|
83 ac3_config.num_output_ch = 2;
|
|
84 ac3_config.flags = 0;
|
|
85 #ifdef HAVE_MMX
|
|
86 ac3_config.flags |= AC3_MMX_ENABLE;
|
|
87 #endif
|
|
88 #ifdef HAVE_3DNOW
|
|
89 ac3_config.flags |= AC3_3DNOW_ENABLE;
|
|
90 #endif
|
|
91 ac3_init();
|
|
92 sh_audio->ac3_frame = ac3_decode_frame();
|
|
93 if(sh_audio->ac3_frame){
|
|
94 sh_audio->samplerate=sh_audio->ac3_frame->sampling_rate;
|
|
95 sh_audio->channels=2;
|
303
|
96 } else {
|
|
97 driver=0; // bad frame -> disable audio
|
|
98 }
|
|
99 break;
|
|
100 }
|
|
101 case 5: {
|
296
|
102 // aLaw audio codec:
|
|
103 Gen_aLaw_2_Signed(); // init table
|
|
104 sh_audio->channels=sh_audio->wf.nChannels;
|
|
105 sh_audio->samplerate=sh_audio->wf.nSamplesPerSec;
|
303
|
106 break;
|
|
107 }
|
|
108 case 6: {
|
296
|
109 // MS-GSM audio codec:
|
|
110 GSM_Init();
|
|
111 sh_audio->channels=sh_audio->wf.nChannels;
|
|
112 sh_audio->samplerate=sh_audio->wf.nSamplesPerSec;
|
303
|
113 break;
|
296
|
114 }
|
303
|
115 case 1: {
|
296
|
116 // MPEG Audio:
|
|
117 MP3_Init();
|
|
118 MP3_samplerate=MP3_channels=0;
|
|
119 // printf("[\n");
|
|
120 sh_audio->a_buffer_len=MP3_DecodeFrame(sh_audio->a_buffer,-1);
|
|
121 // printf("]\n");
|
|
122 sh_audio->channels=2; // hack
|
|
123 sh_audio->samplerate=MP3_samplerate;
|
303
|
124 break;
|
|
125 }
|
296
|
126 }
|
|
127
|
|
128 if(!sh_audio->channels || !sh_audio->samplerate){
|
|
129 printf("Unknown/missing audio format, using nosound\n");
|
303
|
130 driver=0;
|
296
|
131 }
|
|
132
|
303
|
133 if(!driver){
|
|
134 if(sh_audio->a_buffer) free(sh_audio->a_buffer);
|
|
135 sh_audio->o_bps=0;
|
|
136 return 0;
|
|
137 }
|
296
|
138
|
303
|
139 sh_audio->o_bps=sh_audio->channels*sh_audio->samplerate*sh_audio->samplesize;
|
|
140 return driver;
|
296
|
141 }
|
291
|
142
|
|
143 // Audio decoding
|
|
144
|
|
145 int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int maxlen){
|
|
146 int len=-1;
|
303
|
147 switch(sh_audio->codec->driver){
|
291
|
148 case 1: // MPEG layer 2 or 3
|
|
149 len=MP3_DecodeFrame(buf,-1);
|
296
|
150 sh_audio->channels=2; // hack
|
291
|
151 break;
|
|
152 case 2: // PCM
|
|
153 { len=demux_read_data(sh_audio->ds,buf,OUTBURST);
|
|
154 if(sh_audio->pcm_bswap){
|
|
155 int j;
|
|
156 //if(i&1){ printf("Warning! pcm_audio_size&1 !=0 (%d)\n",i);i&=~1; }
|
|
157 for(j=0;j<len;j+=2){
|
|
158 char x=buf[j];
|
|
159 buf[j]=buf[j+1];
|
|
160 buf[j+1]=x;
|
|
161 }
|
|
162 }
|
|
163 break;
|
|
164 }
|
|
165 case 5: // aLaw decoder
|
|
166 { int l=demux_read_data(sh_audio->ds,buf,OUTBURST/2);
|
|
167 unsigned short *d=(unsigned short *) buf;
|
|
168 unsigned char *s=buf;
|
|
169 len=2*l;
|
|
170 while(l>0){
|
|
171 --l;
|
|
172 d[l]=xa_alaw_2_sign[s[l]];
|
|
173 }
|
|
174 break;
|
|
175 }
|
|
176 case 6: // MS-GSM decoder
|
|
177 { unsigned char buf[65]; // 65 bytes / frame
|
|
178 len=0;
|
|
179 while(len<OUTBURST){
|
|
180 if(demux_read_data(d_audio,buf,65)!=65) break; // EOF
|
|
181 XA_MSGSM_Decoder(buf,(unsigned short *) buf); // decodes 65 byte -> 320 short
|
296
|
182 // XA_GSM_Decoder(buf,(unsigned short *) &sh_audio->a_buffer[sh_audio->a_buffer_len]); // decodes 33 byte -> 160 short
|
291
|
183 len+=2*320;
|
|
184 }
|
|
185 break;
|
|
186 }
|
|
187 case 3: // AC3 decoder
|
|
188 //printf("{1:%d}",avi_header.idx_pos);fflush(stdout);
|
|
189 if(!sh_audio->ac3_frame) sh_audio->ac3_frame=ac3_decode_frame();
|
|
190 //printf("{2:%d}",avi_header.idx_pos);fflush(stdout);
|
|
191 if(sh_audio->ac3_frame){
|
296
|
192 len = 256 * 6 *sh_audio->channels*sh_audio->samplesize;
|
291
|
193 memcpy(buf,sh_audio->ac3_frame->audio_data,len);
|
|
194 sh_audio->ac3_frame=NULL;
|
|
195 }
|
|
196 //printf("{3:%d}",avi_header.idx_pos);fflush(stdout);
|
|
197 break;
|
|
198 case 4:
|
|
199 { len=acm_decode_audio(sh_audio,buf,maxlen);
|
|
200 break;
|
|
201 }
|
|
202 #ifdef USE_DIRECTSHOW
|
|
203 case 7: // DirectShow
|
|
204 { int ret;
|
|
205 int size_in=0;
|
|
206 int size_out=0;
|
|
207 int srcsize=DS_AudioDecoder_GetSrcSize(maxlen);
|
|
208 if(verbose>2)printf("DShow says: srcsize=%d (buffsize=%d) out_size=%d\n",srcsize,sh_audio->a_in_buffer_size,maxlen);
|
|
209 if(srcsize>sh_audio->a_in_buffer_size) srcsize=sh_audio->a_in_buffer_size; // !!!!!!
|
|
210 if(sh_audio->a_in_buffer_len<srcsize){
|
|
211 sh_audio->a_in_buffer_len+=
|
|
212 demux_read_data(sh_audio->ds,&sh_audio->a_in_buffer[sh_audio->a_in_buffer_len],
|
|
213 srcsize-sh_audio->a_in_buffer_len);
|
|
214 }
|
|
215 DS_AudioDecoder_Convert(sh_audio->a_in_buffer,sh_audio->a_in_buffer_len,
|
|
216 buf,maxlen, &size_in,&size_out);
|
|
217 if(verbose>2)printf("DShow: audio %d -> %d converted (in_buf_len=%d of %d)\n",size_in,size_out,sh_audio->a_in_buffer_len,sh_audio->a_in_buffer_size);
|
|
218 if(size_in>=sh_audio->a_in_buffer_len){
|
|
219 sh_audio->a_in_buffer_len=0;
|
|
220 } else {
|
|
221 sh_audio->a_in_buffer_len-=size_in;
|
|
222 memcpy(sh_audio->a_in_buffer,&sh_audio->a_in_buffer[size_in],sh_audio->a_in_buffer_len);
|
|
223 }
|
|
224 len=size_out;
|
|
225 break;
|
|
226 }
|
|
227 #endif
|
|
228 }
|
|
229 return len;
|
|
230 }
|
|
231
|
|
232
|