1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* OS/2 KAI audio output driver
*
* Copyright (c) 2010 by KO Myung-Hun (komh@chollian.net)
*
* This file is part of MPlayer.
*
* MPlayer is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* MPlayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define INCL_DOS
#define INCL_DOSERRORS
#include <os2.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <float.h>
#include <kai.h>
#include "config.h"
#include "libaf/af_format.h"
#include "audio_out.h"
#include "audio_out_internal.h"
#include "mp_msg.h"
#include "libvo/fastmemcpy.h"
#include "subopt-helper.h"
#include "libavutil/fifo.h"
static const ao_info_t info = {
"KAI audio output",
"kai",
"KO Myung-Hun <komh@chollian.net>",
""
};
LIBAO_EXTERN(kai)
#define OUTBURST_SAMPLES 512
#define DEFAULT_SAMPLES (OUTBURST_SAMPLES << 2)
#define CHUNK_SIZE ao_data.outburst
static AVFifoBuffer *m_audioBuf;
static int m_nBufSize = 0;
static volatile int m_fQuit = FALSE;
static KAISPEC m_kaiSpec;
static HKAI m_hkai;
static int write_buffer(unsigned char *data, int len)
{
int nFree = av_fifo_space(m_audioBuf);
len = FFMIN(len, nFree);
return av_fifo_generic_write(m_audioBuf, data, len, NULL);
}
static int read_buffer(unsigned char *data, int len)
{
int nBuffered = av_fifo_size(m_audioBuf);
len = FFMIN(len, nBuffered);
av_fifo_generic_read(m_audioBuf, data, len, NULL);
return len;
}
// end ring buffer stuff
static ULONG APIENTRY kai_audio_callback(PVOID pCBData, PVOID pBuffer,
ULONG ulSize)
{
int nReadLen;
nReadLen = read_buffer(pBuffer, ulSize);
if (nReadLen < ulSize && !m_fQuit) {
memset((uint8_t *)pBuffer + nReadLen, m_kaiSpec.bSilence, ulSize - nReadLen);
nReadLen = ulSize;
}
return nReadLen;
}
// to set/get/query special features/parameters
static int control(int cmd, void *arg)
{
switch (cmd) {
case AOCONTROL_GET_VOLUME:
{
ao_control_vol_t *vol = arg;
vol->left = vol->right = kaiGetVolume(m_hkai, MCI_STATUS_AUDIO_ALL);
return CONTROL_OK;
}
case AOCONTROL_SET_VOLUME:
{
int mid;
ao_control_vol_t *vol = arg;
mid = (vol->left + vol->right) / 2;
kaiSetVolume(m_hkai, MCI_SET_AUDIO_ALL, mid);
return CONTROL_OK;
}
}
return CONTROL_UNKNOWN;
}
static void print_help(void)
{
mp_msg(MSGT_AO, MSGL_FATAL,
"\n-ao kai commandline help:\n"
"Example: mplayer -ao kai:noshare\n"
" open audio in exclusive mode\n"
"\nOptions:\n"
" uniaud\n"
" Use UNIAUD audio driver\n"
" dart\n"
" Use DART audio driver\n"
" (no)share\n"
" Open audio in shareable or exclusive mode\n"
" bufsize=<size>\n"
" Set buffer size to <size> in samples(default: 2048)\n");
}
// open & set up audio device
// return: 1=success 0=fail
static int init(int rate, int channels, int format, int flags)
{
int fUseUniaud = 0;
int fUseDart = 0;
int fShare = 1;
ULONG kaiMode;
KAICAPS kc;
int nSamples = DEFAULT_SAMPLES;
int nBytesPerSample;
KAISPEC ksWanted;
const opt_t subopts[] = {
{"uniaud", OPT_ARG_BOOL, &fUseUniaud, NULL},
{"dart", OPT_ARG_BOOL, &fUseDart, NULL},
{"share", OPT_ARG_BOOL, &fShare, NULL},
{"bufsize", OPT_ARG_INT, &nSamples, int_non_neg},
{NULL}
};
const char *audioDriver[] = {"DART", "UNIAUD",};
if (subopt_parse(ao_subdevice, subopts) != 0) {
print_help();
return 0;
}
if (fUseUniaud && fUseDart)
mp_msg(MSGT_VO, MSGL_WARN,"KAI: Multiple mode specified!!!\n");
if (fUseUniaud)
kaiMode = KAIM_UNIAUD;
else if (fUseDart)
kaiMode = KAIM_DART;
else
kaiMode = KAIM_AUTO;
if (kaiInit(kaiMode)) {
mp_msg(MSGT_VO, MSGL_ERR, "KAI: Init failed!!!\n");
return 0;
}
kaiCaps(&kc);
mp_msg(MSGT_AO, MSGL_V, "KAI: selected audio driver = %s\n",
audioDriver[kc.ulMode - 1]);
mp_msg(MSGT_AO, MSGL_V, "KAI: PDD name = %s, maximum channels = %lu\n",
kc.szPDDName, kc.ulMaxChannels);
if (!nSamples)
nSamples = DEFAULT_SAMPLES;
mp_msg(MSGT_AO, MSGL_V, "KAI: open in %s mode, buffer size = %d sample(s)\n",
fShare ? "shareable" : "exclusive", nSamples);
switch (format) {
case AF_FORMAT_S16_LE:
case AF_FORMAT_S8:
break;
default:
format = AF_FORMAT_S16_LE;
mp_msg(MSGT_AO, MSGL_V, "KAI: format %s not supported defaulting to Signed 16-bit Little-Endian\n",
af_fmt2str_short(format));
break;
}
nBytesPerSample = (af_fmt2bits(format) >> 3) * channels;
ksWanted.usDeviceIndex = 0;
ksWanted.ulType = KAIT_PLAY;
ksWanted.ulBitsPerSample = af_fmt2bits(format);
ksWanted.ulSamplingRate = rate;
ksWanted.ulDataFormat = MCI_WAVE_FORMAT_PCM;
ksWanted.ulChannels = channels;
ksWanted.ulNumBuffers = 2;
ksWanted.ulBufferSize = nBytesPerSample * nSamples;
ksWanted.fShareable = fShare;
ksWanted.pfnCallBack = kai_audio_callback;
ksWanted.pCallBackData = NULL;
if (kaiOpen(&ksWanted, &m_kaiSpec, &m_hkai)) {
mp_msg(MSGT_VO, MSGL_ERR, "KAI: Open failed!!!\n");
return 0;
}
mp_msg(MSGT_AO, MSGL_V, "KAI: obtained buffer count = %lu, size = %lu bytes\n",
m_kaiSpec.ulNumBuffers, m_kaiSpec.ulBufferSize);
m_fQuit = FALSE;
ao_data.channels = channels;
ao_data.samplerate = rate;
ao_data.format = format;
ao_data.bps = nBytesPerSample * rate;
ao_data.outburst = nBytesPerSample * OUTBURST_SAMPLES;
ao_data.buffersize = m_kaiSpec.ulBufferSize;
m_nBufSize = (m_kaiSpec.ulBufferSize * m_kaiSpec.ulNumBuffers) << 2;
// multiple of CHUNK_SIZE
m_nBufSize = (m_nBufSize / CHUNK_SIZE) * CHUNK_SIZE;
// and one more chunk plus round up
m_nBufSize += 2 * CHUNK_SIZE;
mp_msg(MSGT_AO, MSGL_V, "KAI: internal audio buffer size = %d bytes\n",
m_nBufSize);
m_audioBuf = av_fifo_alloc(m_nBufSize);
kaiPlay(m_hkai);
// might cause PM DLLs to be loaded which incorrectly enable SIG_FPE,
// which AAC decoding might trigger.
// so, mask off all floating-point exceptions.
_control87(MCW_EM, MCW_EM);
return 1;
}
// close audio device
static void uninit(int immed)
{
m_fQuit = TRUE;
if (!immed)
while (kaiStatus(m_hkai) & KAIS_PLAYING)
DosSleep(1);
kaiClose(m_hkai);
kaiDone();
av_fifo_free(m_audioBuf);
}
// stop playing and empty buffers (for seeking/pause)
static void reset(void)
{
kaiPause(m_hkai);
// Reset ring-buffer state
av_fifo_reset(m_audioBuf);
kaiResume(m_hkai);
}
// stop playing, keep buffers (for pause)
static void audio_pause(void)
{
kaiPause(m_hkai);
}
// resume playing, after audio_pause()
static void audio_resume(void)
{
kaiResume(m_hkai);
}
// return: how many bytes can be played without blocking
static int get_space(void)
{
return av_fifo_space(m_audioBuf);
}
// plays 'len' bytes of 'data'
// it should round it down to outburst*n
// return: number of bytes played
static int play(void *data, int len, int flags)
{
if (!(flags & AOPLAY_FINAL_CHUNK))
len = (len / ao_data.outburst) * ao_data.outburst;
return write_buffer(data, len);
}
// return: delay in seconds between first and last sample in buffer
static float get_delay(void)
{
int nBuffered = av_fifo_size(m_audioBuf); // could be less
return (float)nBuffered / (float)ao_data.bps;
}
|