typo fixes
[mplayer/greg.git] / libmpcodecs / ad_acm.c
blob73c5c11ca88f5d899ff6e798e4ceec9e638cc90b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
5 #include "config.h"
6 #include "mp_msg.h"
7 #include "help_mp.h"
9 #ifdef USE_WIN32DLL
11 #include "wineacm.h"
13 #include "ad_internal.h"
14 #include "osdep/timer.h"
16 static ad_info_t info =
18 "Win32/ACM decoders",
19 "acm",
20 "A'rpi",
21 "A'rpi & Alex",
25 LIBAD_EXTERN(acm)
27 typedef struct {
28 WAVEFORMATEX *o_wf;
29 HACMSTREAM handle;
30 } acm_context_t;
32 static int init(sh_audio_t *sh_audio)
34 int ret=decode_audio(sh_audio,sh_audio->a_buffer,4096,sh_audio->a_buffer_size);
35 if(ret<0){
36 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"ACM decoding error: %d\n",ret);
37 return 0;
39 sh_audio->a_buffer_len=ret;
40 return 1;
43 extern void print_wave_header(WAVEFORMATEX *h, int verbose_level);
45 static int preinit(sh_audio_t *sh_audio)
47 HRESULT ret;
48 WAVEFORMATEX *in_fmt = sh_audio->wf;
49 DWORD srcsize = 0;
50 acm_context_t *priv;
52 priv = malloc(sizeof(acm_context_t));
53 if (!priv)
54 return 0;
55 sh_audio->context = priv;
57 mp_msg(MSGT_WIN32, MSGL_V, "======= Win32 (ACM) AUDIO Codec init =======\n");
59 // priv->handle = NULL;
61 priv->o_wf = malloc(sizeof(WAVEFORMATEX));
62 if (!priv->o_wf)
64 mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_ACMiniterror);
65 return 0;
68 priv->o_wf->nChannels = in_fmt->nChannels;
69 priv->o_wf->nSamplesPerSec = in_fmt->nSamplesPerSec;
70 priv->o_wf->nAvgBytesPerSec = 2*in_fmt->nSamplesPerSec*in_fmt->nChannels;
71 priv->o_wf->wFormatTag = WAVE_FORMAT_PCM;
72 priv->o_wf->nBlockAlign = 2*in_fmt->nChannels;
73 priv->o_wf->wBitsPerSample = 16;
74 // priv->o_wf->wBitsPerSample = inf_fmt->wBitsPerSample;
75 priv->o_wf->cbSize = 0;
77 if ( mp_msg_test(MSGT_DECAUDIO,MSGL_V) )
79 mp_msg(MSGT_DECAUDIO, MSGL_V, "Input format:\n");
80 print_wave_header(in_fmt, MSGL_V);
81 mp_msg(MSGT_DECAUDIO, MSGL_V, "Output format:\n");
82 print_wave_header(priv->o_wf, MSGL_V);
85 MSACM_RegisterDriver((const char *)sh_audio->codec->dll, in_fmt->wFormatTag, 0);
86 ret = acmStreamOpen(&priv->handle, (HACMDRIVER)NULL, in_fmt,
87 priv->o_wf, NULL, 0, 0, 0);
88 if (ret)
90 if (ret == ACMERR_NOTPOSSIBLE)
91 mp_msg(MSGT_WIN32, MSGL_ERR, "ACM_Decoder: Unappropriate audio format\n");
92 else
93 mp_msg(MSGT_WIN32, MSGL_ERR, "ACM_Decoder: acmStreamOpen error: %d\n",
94 (int)ret);
95 mp_msg(MSGT_DECAUDIO,MSGL_ERR,MSGTR_ACMiniterror);
96 return 0;
98 mp_msg(MSGT_WIN32, MSGL_V, "Audio codec opened OK! ;-)\n");
100 acmStreamSize(priv->handle, in_fmt->nBlockAlign, &srcsize, ACM_STREAMSIZEF_SOURCE);
101 //if ( mp_msg_test(MSGT_DECAUDIO,MSGL_V) ) printf("Audio ACM output buffer min. size: %ld (reported by codec)\n", srcsize);
102 srcsize *= 2;
103 //if (srcsize < MAX_OUTBURST) srcsize = MAX_OUTBURST;
104 if (!srcsize)
106 mp_msg(MSGT_WIN32, MSGL_WARN, "Warning! ACM codec reports srcsize=0\n");
107 srcsize = 16384;
109 // limit srcsize to 4-16kb
110 //while(srcsize && srcsize<4096) srcsize*=2;
111 //while(srcsize>16384) srcsize/=2;
112 sh_audio->audio_out_minsize=srcsize; // audio output min. size
113 mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM output buffer min. size: %ld\n",srcsize);
115 acmStreamSize(priv->handle, srcsize, &srcsize, ACM_STREAMSIZEF_DESTINATION);
116 // if(srcsize<in_fmt->nBlockAlign) srcsize=in_fmt->nBlockAlign;
118 if (!srcsize)
120 mp_msg(MSGT_WIN32, MSGL_WARN, "Warning! ACM codec reports srcsize=0\n");
121 srcsize = 2*in_fmt->nBlockAlign;
124 mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM input buffer min. size: %ld\n",srcsize);
126 sh_audio->audio_in_minsize=2*srcsize; // audio input min. size
128 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
129 sh_audio->channels=priv->o_wf->nChannels;
130 sh_audio->samplerate=priv->o_wf->nSamplesPerSec;
131 sh_audio->samplesize=2;
133 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Win32/ACM audio codec init OK!\n");
134 return 1;
137 static void uninit(sh_audio_t *sh)
139 HRESULT ret;
140 acm_context_t *priv = sh->context;
142 ret = acmStreamClose(priv->handle, 0);
144 if (ret)
145 switch(ret)
147 case ACMERR_BUSY:
148 case ACMERR_CANCELED:
149 mp_msg(MSGT_WIN32, MSGL_DBG2, "ACM_Decoder: stream busy, waiting..\n");
150 usec_sleep(100000000);
151 return(uninit(sh));
152 case ACMERR_UNPREPARED:
153 case ACMERR_NOTPOSSIBLE:
154 return;
155 default:
156 mp_msg(MSGT_WIN32, MSGL_WARN, "ACM_Decoder: unknown error occurred: %ld\n", ret);
157 return;
160 MSACM_UnregisterAllDrivers();
162 free(priv->o_wf);
163 free(priv);
166 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
168 int skip;
169 switch(cmd)
171 case ADCTRL_SKIP_FRAME:
172 skip=sh_audio->wf->nBlockAlign;
173 if(skip<16){
174 skip=(sh_audio->wf->nAvgBytesPerSec/16)&(~7);
175 if(skip<16) skip=16;
177 demux_read_data(sh_audio->ds,NULL,skip);
178 return CONTROL_TRUE;
180 return CONTROL_UNKNOWN;
183 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
185 ACMSTREAMHEADER ash;
186 HRESULT hr;
187 DWORD srcsize=0;
188 DWORD len=minlen;
189 acm_context_t *priv = sh_audio->context;
191 acmStreamSize(priv->handle, len, &srcsize, ACM_STREAMSIZEF_DESTINATION);
192 mp_msg(MSGT_WIN32,MSGL_DBG3,"acm says: srcsize=%ld (buffsize=%d) out_size=%ld\n",srcsize,sh_audio->a_in_buffer_size,len);
194 if(srcsize<sh_audio->wf->nBlockAlign){
195 srcsize=sh_audio->wf->nBlockAlign;
196 acmStreamSize(priv->handle, srcsize, &len, ACM_STREAMSIZEF_SOURCE);
197 if(len>maxlen) len=maxlen;
200 // if(srcsize==0) srcsize=((WAVEFORMATEX *)&sh_audio->o_wf_ext)->nBlockAlign;
201 if(srcsize>sh_audio->a_in_buffer_size) srcsize=sh_audio->a_in_buffer_size; // !!!!!!
202 if(sh_audio->a_in_buffer_len<srcsize){
203 sh_audio->a_in_buffer_len+=
204 demux_read_data(sh_audio->ds,&sh_audio->a_in_buffer[sh_audio->a_in_buffer_len],
205 srcsize-sh_audio->a_in_buffer_len);
207 mp_msg(MSGT_WIN32,MSGL_DBG3,"acm convert %d -> %ld bytes\n",sh_audio->a_in_buffer_len,len);
208 memset(&ash, 0, sizeof(ash));
209 ash.cbStruct=sizeof(ash);
210 ash.fdwStatus=0;
211 ash.dwUser=0;
212 ash.pbSrc=sh_audio->a_in_buffer;
213 ash.cbSrcLength=sh_audio->a_in_buffer_len;
214 ash.pbDst=buf;
215 ash.cbDstLength=len;
216 hr=acmStreamPrepareHeader(priv->handle,&ash,0);
217 if(hr){
218 mp_msg(MSGT_WIN32,MSGL_V,"ACM_Decoder: acmStreamPrepareHeader error %d\n",(int)hr);
219 return -1;
221 hr=acmStreamConvert(priv->handle,&ash,0);
222 if(hr){
223 mp_msg(MSGT_WIN32,MSGL_DBG2,"ACM_Decoder: acmStreamConvert error %d\n",(int)hr);
224 switch(hr)
226 case ACMERR_NOTPOSSIBLE:
227 case ACMERR_UNPREPARED:
228 mp_msg(MSGT_WIN32, MSGL_DBG2, "ACM_Decoder: acmStreamConvert error: probarly not initialized!\n");
230 // return -1;
232 mp_msg(MSGT_WIN32,MSGL_DBG2,"acm converted %ld -> %ld\n",ash.cbSrcLengthUsed,ash.cbDstLengthUsed);
233 if(ash.cbSrcLengthUsed>=sh_audio->a_in_buffer_len){
234 sh_audio->a_in_buffer_len=0;
235 } else {
236 sh_audio->a_in_buffer_len-=ash.cbSrcLengthUsed;
237 memcpy(sh_audio->a_in_buffer,&sh_audio->a_in_buffer[ash.cbSrcLengthUsed],sh_audio->a_in_buffer_len);
239 len=ash.cbDstLengthUsed;
240 hr=acmStreamUnprepareHeader(priv->handle,&ash,0);
241 if(hr){
242 mp_msg(MSGT_WIN32,MSGL_V,"ACM_Decoder: acmStreamUnprepareHeader error %d\n",(int)hr);
244 return len;
246 #endif