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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
* Copyright (C) 2000, 2001 the xine project
*
* This file is part of xine, a unix video player.
*
* xine 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.
*
* xine 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 self program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: audio_out.c,v 1.11 2001/09/06 13:33:20 jkeil Exp $
*
* 22-8-2001 James imported some useful AC3 sections from the previous alsa driver.
* (c) 2001 Andy Lo A Foe <andy@alsaplayer.org>
* 20-8-2001 First implementation of Audio sync and Audio driver separation.
* (c) 2001 James Courtier-Dutton James@superbug.demon.co.uk
*
* General Programming Guidelines: -
* New concept of an "audio_frame".
* An audio_frame consists of all the samples required to fill every audio channel to a full amount of bits.
* So, it does not mater how many bits per sample, or how many audio channels are being used, the number of audio_frames is the same.
* E.g. 16 bit stereo is 4 bytes, but one frame.
* 16 bit 5.1 surround is 12 bytes, but one frame.
* The purpose of this is to make the audio_sync code a lot more readable, rather than having to multiply by the amount of channels all the time
* when dealing with audio_bytes instead of audio_frames.
*
* The number of samples passed to/from the audio driver is also sent in units of audio_frames.
*
* Currently, James has tested with OSS: Standard stereo out, SPDIF PCM, SPDIF AC3
* ALSA: Standard stereo out
* No testing has been done of ALSA SPDIF AC3 or any 4,5,5.1 channel output.
* Currently, I don't think resampling functions, as I cannot test it.
*/
/* required for swab() */
#define _XOPEN_SOURCE 500
/* required for FNDELAY decl */
#define _BSD_SOURCE 1
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include <unistd.h>
#include <inttypes.h>
#include "xine_internal.h"
#include "monitor.h"
#include "audio_out.h"
#include "resample.h"
#include "metronom.h"
#include "utils.h"
#define ZERO_BUF_SIZE 5000
#define MAX_GAP 90000
struct frmsize_s
{
uint16_t bit_rate;
uint16_t frm_size[3];
};
static const struct frmsize_s frmsizecod_tbl[64] =
{
{ 32 ,{64 ,69 ,96 } },
{ 32 ,{64 ,70 ,96 } },
{ 40 ,{80 ,87 ,120 } },
{ 40 ,{80 ,88 ,120 } },
{ 48 ,{96 ,104 ,144 } },
{ 48 ,{96 ,105 ,144 } },
{ 56 ,{112 ,121 ,168 } },
{ 56 ,{112 ,122 ,168 } },
{ 64 ,{128 ,139 ,192 } },
{ 64 ,{128 ,140 ,192 } },
{ 80 ,{160 ,174 ,240 } },
{ 80 ,{160 ,175 ,240 } },
{ 96 ,{192 ,208 ,288 } },
{ 96 ,{192 ,209 ,288 } },
{ 112 ,{224 ,243 ,336 } },
{ 112 ,{224 ,244 ,336 } },
{ 128 ,{256 ,278 ,384 } },
{ 128 ,{256 ,279 ,384 } },
{ 160 ,{320 ,348 ,480 } },
{ 160 ,{320 ,349 ,480 } },
{ 192 ,{384 ,417 ,576 } },
{ 192 ,{384 ,418 ,576 } },
{ 224 ,{448 ,487 ,672 } },
{ 224 ,{448 ,488 ,672 } },
{ 256 ,{512 ,557 ,768 } },
{ 256 ,{512 ,558 ,768 } },
{ 320 ,{640 ,696 ,960 } },
{ 320 ,{640 ,697 ,960 } },
{ 384 ,{768 ,835 ,1152 } },
{ 384 ,{768 ,836 ,1152 } },
{ 448 ,{896 ,975 ,1344 } },
{ 448 ,{896 ,976 ,1344 } },
{ 512 ,{1024 ,1114 ,1536 } },
{ 512 ,{1024 ,1115 ,1536 } },
{ 576 ,{1152 ,1253 ,1728 } },
{ 576 ,{1152 ,1254 ,1728 } },
{ 640 ,{1280 ,1393 ,1920 } },
{ 640 ,{1280 ,1394 ,1920 } }
};
/*
* open the audio device for writing to
*/
static int ao_open(ao_instance_t *this,
uint32_t bits, uint32_t rate, int mode)
{
int output_sample_rate;
if ((output_sample_rate=this->driver->open(this->driver,bits,rate,mode)) == 0) {
printf("audio_out: open failed!\n");
return 0;
};
printf("audio_out: output sample rate %d\n", output_sample_rate);
this->mode = mode;
this->input_frame_rate = rate;
this->audio_started = 0;
this->last_audio_vpts = 0;
this->output_frame_rate = output_sample_rate;
switch (this->resample_conf) {
case 1: /* force off */
this->do_resample = 0;
break;
case 2: /* force on */
this->do_resample = 1;
break;
default: /* AUTO */
this->do_resample = this->output_frame_rate != this->input_frame_rate;
}
if (this->do_resample)
printf("audio_out: will resample audio from %d to %d\n",
this->input_frame_rate, this->output_frame_rate);
this->num_channels = this->driver->num_channels(this->driver);
this->frame_rate_factor = (double) this->output_frame_rate / (double) this->input_frame_rate;
this->audio_step = (uint32_t) 90000 * (uint32_t) 32768 / this->input_frame_rate;
this->frames_per_kpts = this->output_frame_rate * 1024 / 90000;
xprintf (VERBOSE|AUDIO, "audio_out : audio_step %d pts per 32768 frames\n", this->audio_step);
this->metronom->set_audio_rate(this->metronom, this->audio_step);
return 1;
}
static void ao_fill_gap (ao_instance_t *this, uint32_t pts_len) {
int num_frames ;
xprintf (VERBOSE|AUDIO, "audio_out : fill_gap\n");
if (pts_len > MAX_GAP)
pts_len = MAX_GAP;
num_frames = pts_len * this->frames_per_kpts / 1024;
if (this->mode == AO_CAP_MODE_A52) return; /* FIXME */
printf ("audio_out: inserting %d 0-frames to fill a gap of %d pts\n",num_frames, pts_len);
while (num_frames > 0) {
if (num_frames > ZERO_BUF_SIZE) {
this->driver->write(this->driver, this->zero_space, ZERO_BUF_SIZE);
num_frames -= ZERO_BUF_SIZE;
} else {
this->driver->write(this->driver, this->zero_space, num_frames);
num_frames = 0;
}
}
}
/*
* This routine is currently not used, but I do not want to loose it.
* I think "(c) 2001 Andy Lo A Foe <andy@alsaplayer.org>" originally added it
* to ./xine-lib/src/audio_out/audio_alsa_out.c before the architecture changes
* So it has moved to here.
*/
void write_pause_burst(ao_instance_t *this, int error)
{
unsigned char buf[8192];
unsigned short *sbuf = (unsigned short *)&buf[0];
sbuf[0] = 0xf872;
sbuf[1] = 0x4e1f;
if (error == 0)
/* Audio ES Channel empty, wait for DD Decoder or pause */
sbuf[2] = 0x0003;
else
/* user stop, skip or error */
sbuf[2] = 0x0103;
sbuf[3] = 0x0020;
sbuf[4] = 0x0000;
sbuf[5] = 0x0000;
memset(&sbuf[6], 0, 6144 - 96);
this->driver->write(this->driver, sbuf, 6144 / 4);
}
static int ao_write(ao_instance_t *this,
int16_t* output_frames, uint32_t num_frames,
uint32_t pts)
{
uint32_t vpts, buffer_vpts;
int32_t gap;
int bDropPackage;
int delay;
int frame_size;
int fscod;
int frmsizecod;
uint8_t *data;
if (this->driver<0)
return 1;
vpts = this->metronom->got_audio_samples (this->metronom, pts, num_frames);
xprintf (VERBOSE|AUDIO, "audio_out: got %d frames, vpts=%d pts=%d\n",
num_frames, vpts, pts);
if (vpts<this->last_audio_vpts) {
/* reject this */
xprintf (VERBOSE|AUDIO, "audio_out: rejected frame vpts=%d, last_audio_vpts=%d\n", vpts,this->last_audio_vpts)
return 1;
}
this->last_audio_vpts = vpts;
bDropPackage = 0;
/*
* where, in the timeline is the "end" of the audio buffer at the moment?
*/
buffer_vpts = this->metronom->get_current_time (this->metronom);
if (this->audio_started)
delay = this->driver->delay(this->driver);
else
delay = 0;
/* External A52 decoder delay correction */
if (this->mode==AO_CAP_MODE_A52)
delay+=10;
buffer_vpts += delay * 1024 / this->frames_per_kpts;
/*
* calculate gap:
*/
gap = vpts - buffer_vpts;
/*
printf ("vpts : %d buffer_vpts : %d gap %d\n",
vpts, buffer_vpts, gap);
*/
if (gap>this->gap_tolerance) {
ao_fill_gap (this, gap);
/* keep xine responsive */
if (gap>MAX_GAP)
return 0;
} else if (gap < (-1 * this->gap_tolerance)) {
bDropPackage = 1;
xprintf (VERBOSE|AUDIO, "audio_out: audio package (vpts = %d %d)"
"dropped\n", vpts, gap);
}
/*
* resample and output frames
*/
if (this->mode == AO_CAP_MODE_A52)
bDropPackage=0;
if (!bDropPackage) {
int num_output_frames = (double) num_frames * this->frame_rate_factor;
if ((!this->do_resample) && (this->mode != AO_CAP_MODE_A52)) {
xprintf (VERBOSE|AUDIO, "audio_out: writing without resampling\n");
this->driver->write (this->driver, output_frames,
num_output_frames );
} else switch (this->mode) {
case AO_CAP_MODE_MONO:
audio_out_resample_mono (output_frames, num_frames,
this->frame_buffer, num_output_frames);
this->driver->write(this->driver, this->frame_buffer, num_output_frames);
break;
case AO_CAP_MODE_STEREO:
audio_out_resample_stereo (output_frames, num_frames,
this->frame_buffer, num_output_frames);
this->driver->write(this->driver, this->frame_buffer, num_output_frames);
break;
case AO_CAP_MODE_4CHANNEL:
audio_out_resample_4channel (output_frames, num_frames,
this->frame_buffer, num_output_frames);
this->driver->write(this->driver, this->frame_buffer, num_output_frames);
break;
case AO_CAP_MODE_5CHANNEL:
audio_out_resample_5channel (output_frames, num_frames,
this->frame_buffer, num_output_frames);
this->driver->write(this->driver, this->frame_buffer, num_output_frames);
break;
case AO_CAP_MODE_5_1CHANNEL:
audio_out_resample_6channel (output_frames, num_frames,
this->frame_buffer, num_output_frames);
this->driver->write(this->driver, this->frame_buffer, num_output_frames);
break;
case AO_CAP_MODE_A52:
this->frame_buffer[0] = 0xf872; /* spdif syncword */
this->frame_buffer[1] = 0x4e1f; /* ............. */
this->frame_buffer[2] = 0x0001; /* AC3 data */
data = (uint8_t *)&output_frames[1]; /* skip AC3 sync */
fscod = (data[2] >> 6) & 0x3;
frmsizecod = data[2] & 0x3f;
frame_size = frmsizecod_tbl[frmsizecod].frm_size[fscod] << 4;
this->frame_buffer[3] = frame_size;
/* ac3 seems to be swabbed data */
swab(output_frames,this->frame_buffer+4, num_frames );
this->driver->write(this->driver, this->frame_buffer, 1536);
break;
}
xprintf (AUDIO|VERBOSE, "audio_out :audio package written\n");
/*
* step values
*/
this->audio_started = 1;
}
return 1;
}
static void ao_close(ao_instance_t *this)
{
this->driver->close(this->driver);
}
static uint32_t ao_get_capabilities (ao_instance_t *this) {
uint32_t result;
result=this->driver->get_capabilities(this->driver);
return result;
}
ao_instance_t *ao_new_instance (ao_driver_t *driver, metronom_t *metronom,
config_values_t *config) {
ao_instance_t *this;
this = xmalloc (sizeof (ao_instance_t)) ;
this->driver = driver;
this->metronom = metronom;
this->open = ao_open;
this->write = ao_write;
this->close = ao_close;
this->get_capabilities = ao_get_capabilities;
this->audio_loop_running = 0;
this->frame_buffer = xmalloc (40000);
this->zero_space = xmalloc (ZERO_BUF_SIZE * 2 * 6);
this->gap_tolerance = driver->get_gap_tolerance (this->driver);
this->resample_conf = config->lookup_int (config, "audio_resample_mode", 0);
return this;
}
|