demux_lavf: for avi, use packet .dts instead of .pts
[mplayer/glamo.git] / libmpcodecs / ad_acm.c
blob1b1184a06adf11c2f323d692e0c0a11836ab83c3
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 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
23 #include "config.h"
24 #include "mp_msg.h"
25 #include "libmpdemux/aviprint.h"
26 #include "loader/wineacm.h"
28 #include "ad_internal.h"
29 #include "osdep/timer.h"
31 static const ad_info_t info =
33 "Win32/ACM decoders",
34 "acm",
35 "A'rpi",
36 "A'rpi & Alex",
40 LIBAD_EXTERN(acm)
42 typedef struct {
43 WAVEFORMATEX *o_wf;
44 HACMSTREAM handle;
45 } acm_context_t;
47 static int init(sh_audio_t *sh_audio)
49 int ret=decode_audio(sh_audio,sh_audio->a_buffer,4096,sh_audio->a_buffer_size);
50 if(ret<0){
51 mp_msg(MSGT_DECAUDIO,MSGL_INFO,"ACM decoding error: %d\n",ret);
52 return 0;
54 sh_audio->a_buffer_len=ret;
55 return 1;
58 static int preinit(sh_audio_t *sh_audio)
60 HRESULT ret;
61 WAVEFORMATEX *in_fmt = sh_audio->wf;
62 DWORD srcsize = 0;
63 acm_context_t *priv;
65 priv = malloc(sizeof(acm_context_t));
66 if (!priv)
67 return 0;
68 sh_audio->context = priv;
70 mp_msg(MSGT_WIN32, MSGL_V, "======= Win32 (ACM) AUDIO Codec init =======\n");
72 // priv->handle = NULL;
74 priv->o_wf = malloc(sizeof(*priv->o_wf));
75 if (!priv->o_wf)
77 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Could not load/initialize Win32/ACM audio codec (missing DLL file?).\n");
78 return 0;
81 priv->o_wf->nChannels = in_fmt->nChannels;
82 priv->o_wf->nSamplesPerSec = in_fmt->nSamplesPerSec;
83 priv->o_wf->nAvgBytesPerSec = 2*in_fmt->nSamplesPerSec*in_fmt->nChannels;
84 priv->o_wf->wFormatTag = WAVE_FORMAT_PCM;
85 priv->o_wf->nBlockAlign = 2*in_fmt->nChannels;
86 priv->o_wf->wBitsPerSample = 16;
87 // priv->o_wf->wBitsPerSample = inf_fmt->wBitsPerSample;
88 priv->o_wf->cbSize = 0;
90 if ( mp_msg_test(MSGT_DECAUDIO,MSGL_V) )
92 mp_msg(MSGT_DECAUDIO, MSGL_V, "Input format:\n");
93 print_wave_header(in_fmt, MSGL_V);
94 mp_msg(MSGT_DECAUDIO, MSGL_V, "Output format:\n");
95 print_wave_header(priv->o_wf, MSGL_V);
98 MSACM_RegisterDriver((const char *)sh_audio->codec->dll, in_fmt->wFormatTag, 0);
99 ret = acmStreamOpen(&priv->handle, (HACMDRIVER)NULL, in_fmt,
100 priv->o_wf, NULL, 0, 0, 0);
101 if (ret)
103 if (ret == ACMERR_NOTPOSSIBLE)
104 mp_msg(MSGT_WIN32, MSGL_ERR, "ACM_Decoder: Unappropriate audio format\n");
105 else
106 mp_msg(MSGT_WIN32, MSGL_ERR, "ACM_Decoder: acmStreamOpen error: %d\n",
107 (int)ret);
108 mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Could not load/initialize Win32/ACM audio codec (missing DLL file?).\n");
109 return 0;
111 mp_msg(MSGT_WIN32, MSGL_V, "Audio codec opened OK! ;-)\n");
113 acmStreamSize(priv->handle, in_fmt->nBlockAlign, &srcsize, ACM_STREAMSIZEF_SOURCE);
114 //if ( mp_msg_test(MSGT_DECAUDIO,MSGL_V) ) printf("Audio ACM output buffer min. size: %ld (reported by codec)\n", srcsize);
115 srcsize *= 2;
116 //if (srcsize < MAX_OUTBURST) srcsize = MAX_OUTBURST;
117 if (!srcsize)
119 mp_msg(MSGT_WIN32, MSGL_WARN, "Warning! ACM codec reports srcsize=0\n");
120 srcsize = 16384;
122 // limit srcsize to 4-16kb
123 //while(srcsize && srcsize<4096) srcsize*=2;
124 //while(srcsize>16384) srcsize/=2;
125 sh_audio->audio_out_minsize=srcsize; // audio output min. size
126 mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM output buffer min. size: %ld\n",srcsize);
128 acmStreamSize(priv->handle, srcsize, &srcsize, ACM_STREAMSIZEF_DESTINATION);
129 // if(srcsize<in_fmt->nBlockAlign) srcsize=in_fmt->nBlockAlign;
131 if (!srcsize)
133 mp_msg(MSGT_WIN32, MSGL_WARN, "Warning! ACM codec reports srcsize=0\n");
134 srcsize = 2*in_fmt->nBlockAlign;
137 mp_msg(MSGT_WIN32,MSGL_V,"Audio ACM input buffer min. size: %ld\n",srcsize);
139 sh_audio->audio_in_minsize=2*srcsize; // audio input min. size
141 sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec;
142 sh_audio->channels=priv->o_wf->nChannels;
143 sh_audio->samplerate=priv->o_wf->nSamplesPerSec;
144 sh_audio->samplesize=2;
146 mp_msg(MSGT_DECVIDEO,MSGL_V,"INFO: Win32/ACM audio codec init OK!\n");
147 return 1;
150 static void uninit(sh_audio_t *sh)
152 HRESULT ret;
153 acm_context_t *priv = sh->context;
155 retry:
156 ret = acmStreamClose(priv->handle, 0);
158 if (ret)
159 switch(ret)
161 case ACMERR_BUSY:
162 case ACMERR_CANCELED:
163 mp_msg(MSGT_WIN32, MSGL_DBG2, "ACM_Decoder: stream busy, waiting..\n");
164 usec_sleep(100000000);
165 goto retry;
166 case ACMERR_UNPREPARED:
167 case ACMERR_NOTPOSSIBLE:
168 return;
169 default:
170 mp_msg(MSGT_WIN32, MSGL_WARN, "ACM_Decoder: unknown error occurred: %ld\n", ret);
171 return;
174 MSACM_UnregisterAllDrivers();
176 free(priv->o_wf);
177 free(priv);
180 static int control(sh_audio_t *sh_audio,int cmd,void* arg, ...)
182 int skip;
183 switch(cmd)
185 case ADCTRL_SKIP_FRAME:
186 skip=sh_audio->wf->nBlockAlign;
187 if(skip<16){
188 skip=(sh_audio->wf->nAvgBytesPerSec/16)&(~7);
189 if(skip<16) skip=16;
191 demux_read_data(sh_audio->ds,NULL,skip);
192 return CONTROL_TRUE;
194 return CONTROL_UNKNOWN;
197 static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen)
199 ACMSTREAMHEADER ash;
200 HRESULT hr;
201 DWORD srcsize=0;
202 DWORD len=minlen;
203 acm_context_t *priv = sh_audio->context;
205 acmStreamSize(priv->handle, len, &srcsize, ACM_STREAMSIZEF_DESTINATION);
206 mp_msg(MSGT_WIN32,MSGL_DBG3,"acm says: srcsize=%ld (buffsize=%d) out_size=%ld\n",srcsize,sh_audio->a_in_buffer_size,len);
208 if(srcsize<sh_audio->wf->nBlockAlign){
209 srcsize=sh_audio->wf->nBlockAlign;
210 acmStreamSize(priv->handle, srcsize, &len, ACM_STREAMSIZEF_SOURCE);
211 if(len>maxlen) len=maxlen;
214 // if(srcsize==0) srcsize=((WAVEFORMATEX *)&sh_audio->o_wf_ext)->nBlockAlign;
215 if(srcsize>sh_audio->a_in_buffer_size) srcsize=sh_audio->a_in_buffer_size; // !!!!!!
216 if(sh_audio->a_in_buffer_len<srcsize){
217 sh_audio->a_in_buffer_len+=
218 demux_read_data(sh_audio->ds,&sh_audio->a_in_buffer[sh_audio->a_in_buffer_len],
219 srcsize-sh_audio->a_in_buffer_len);
221 mp_msg(MSGT_WIN32,MSGL_DBG3,"acm convert %d -> %ld bytes\n",sh_audio->a_in_buffer_len,len);
222 memset(&ash, 0, sizeof(ash));
223 ash.cbStruct=sizeof(ash);
224 ash.fdwStatus=0;
225 ash.dwUser=0;
226 ash.pbSrc=sh_audio->a_in_buffer;
227 ash.cbSrcLength=sh_audio->a_in_buffer_len;
228 ash.pbDst=buf;
229 ash.cbDstLength=len;
230 hr=acmStreamPrepareHeader(priv->handle,&ash,0);
231 if(hr){
232 mp_msg(MSGT_WIN32,MSGL_V,"ACM_Decoder: acmStreamPrepareHeader error %d\n",(int)hr);
233 return -1;
235 hr=acmStreamConvert(priv->handle,&ash,0);
236 if(hr){
237 mp_msg(MSGT_WIN32,MSGL_DBG2,"ACM_Decoder: acmStreamConvert error %d\n",(int)hr);
238 switch(hr)
240 case ACMERR_NOTPOSSIBLE:
241 case ACMERR_UNPREPARED:
242 mp_msg(MSGT_WIN32, MSGL_DBG2, "ACM_Decoder: acmStreamConvert error: probarly not initialized!\n");
244 // return -1;
246 mp_msg(MSGT_WIN32,MSGL_DBG2,"acm converted %ld -> %ld\n",ash.cbSrcLengthUsed,ash.cbDstLengthUsed);
247 if(ash.cbSrcLengthUsed>=sh_audio->a_in_buffer_len){
248 sh_audio->a_in_buffer_len=0;
249 } else {
250 sh_audio->a_in_buffer_len-=ash.cbSrcLengthUsed;
251 memcpy(sh_audio->a_in_buffer,&sh_audio->a_in_buffer[ash.cbSrcLengthUsed],sh_audio->a_in_buffer_len);
253 len=ash.cbDstLengthUsed;
254 hr=acmStreamUnprepareHeader(priv->handle,&ash,0);
255 if(hr){
256 mp_msg(MSGT_WIN32,MSGL_V,"ACM_Decoder: acmStreamUnprepareHeader error %d\n",(int)hr);
258 return len;