1000l to myself for FFMIN(...,0) instead of ...,6)
[libav.git] / libavformat / audio.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux audio play and grab interface
19720f15 3 * Copyright (c) 2000, 2001 Fabrice Bellard.
de6d9b64 4 *
19720f15
FB
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
de6d9b64 9 *
19720f15 10 * This library is distributed in the hope that it will be useful,
de6d9b64 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
de6d9b64 14 *
19720f15
FB
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 18 */
8be1c656
FB
19#include "avformat.h"
20
de6d9b64
FB
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
b13788c5 24#include <sys/soundcard.h>
de6d9b64
FB
25#include <unistd.h>
26#include <fcntl.h>
27#include <sys/ioctl.h>
28#include <sys/mman.h>
de6d9b64
FB
29#include <sys/time.h>
30
4972b26f
FB
31#define AUDIO_BLOCK_SIZE 4096
32
de6d9b64
FB
33typedef struct {
34 int fd;
4972b26f 35 int sample_rate;
de6d9b64 36 int channels;
4972b26f
FB
37 int frame_size; /* in bytes ! */
38 int codec_id;
1de1cce2 39 int flip_left : 1;
0c1a9eda 40 uint8_t buffer[AUDIO_BLOCK_SIZE];
4972b26f 41 int buffer_ptr;
de6d9b64
FB
42} AudioData;
43
7f172339 44static int audio_open(AudioData *s, int is_output, const char *audio_device)
de6d9b64 45{
4972b26f 46 int audio_fd;
de6d9b64 47 int tmp, err;
1de1cce2 48 char *flip = getenv("AUDIO_FLIP_LEFT");
de6d9b64 49
de6d9b64 50 /* open linux audio device */
7f172339
FB
51 if (!audio_device)
52 audio_device = "/dev/dsp";
53
4972b26f
FB
54 if (is_output)
55 audio_fd = open(audio_device, O_WRONLY);
de6d9b64 56 else
4972b26f 57 audio_fd = open(audio_device, O_RDONLY);
de6d9b64
FB
58 if (audio_fd < 0) {
59 perror(audio_device);
60 return -EIO;
61 }
62
1de1cce2
PG
63 if (flip && *flip == '1') {
64 s->flip_left = 1;
65 }
66
de6d9b64 67 /* non blocking mode */
4364a3e0
FB
68 if (!is_output)
69 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
de6d9b64 70
4972b26f 71 s->frame_size = AUDIO_BLOCK_SIZE;
de6d9b64 72#if 0
4972b26f
FB
73 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
74 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
de6d9b64
FB
75 if (err < 0) {
76 perror("SNDCTL_DSP_SETFRAGMENT");
77 }
78#endif
79
4972b26f
FB
80 /* select format : favour native format */
81 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
82
83#ifdef WORDS_BIGENDIAN
84 if (tmp & AFMT_S16_BE) {
85 tmp = AFMT_S16_BE;
86 } else if (tmp & AFMT_S16_LE) {
87 tmp = AFMT_S16_LE;
88 } else {
89 tmp = 0;
90 }
91#else
92 if (tmp & AFMT_S16_LE) {
93 tmp = AFMT_S16_LE;
94 } else if (tmp & AFMT_S16_BE) {
95 tmp = AFMT_S16_BE;
96 } else {
97 tmp = 0;
98 }
99#endif
100
101 switch(tmp) {
102 case AFMT_S16_LE:
103 s->codec_id = CODEC_ID_PCM_S16LE;
104 break;
105 case AFMT_S16_BE:
106 s->codec_id = CODEC_ID_PCM_S16BE;
107 break;
108 default:
bc874dae 109 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
4972b26f
FB
110 close(audio_fd);
111 return -EIO;
112 }
113 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
de6d9b64
FB
114 if (err < 0) {
115 perror("SNDCTL_DSP_SETFMT");
116 goto fail;
117 }
118
4972b26f
FB
119 tmp = (s->channels == 2);
120 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
de6d9b64
FB
121 if (err < 0) {
122 perror("SNDCTL_DSP_STEREO");
123 goto fail;
124 }
1de1cce2
PG
125 if (tmp)
126 s->channels = 2;
de6d9b64 127
4972b26f
FB
128 tmp = s->sample_rate;
129 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
de6d9b64
FB
130 if (err < 0) {
131 perror("SNDCTL_DSP_SPEED");
132 goto fail;
133 }
4972b26f 134 s->sample_rate = tmp; /* store real sample rate */
de6d9b64
FB
135 s->fd = audio_fd;
136
137 return 0;
138 fail:
139 close(audio_fd);
de6d9b64
FB
140 return -EIO;
141}
142
4972b26f 143static int audio_close(AudioData *s)
de6d9b64 144{
de6d9b64 145 close(s->fd);
4972b26f
FB
146 return 0;
147}
148
149/* sound output support */
150static int audio_write_header(AVFormatContext *s1)
151{
c9a65ca8 152 AudioData *s = s1->priv_data;
4972b26f
FB
153 AVStream *st;
154 int ret;
155
4972b26f
FB
156 st = s1->streams[0];
157 s->sample_rate = st->codec.sample_rate;
158 s->channels = st->codec.channels;
7f172339 159 ret = audio_open(s, 1, NULL);
4972b26f 160 if (ret < 0) {
4972b26f
FB
161 return -EIO;
162 } else {
163 return 0;
164 }
165}
166
e928649b 167static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
4972b26f
FB
168{
169 AudioData *s = s1->priv_data;
170 int len, ret;
e928649b
MN
171 int size= pkt->size;
172 uint8_t *buf= pkt->data;
4972b26f
FB
173
174 while (size > 0) {
175 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
176 if (len > size)
177 len = size;
178 memcpy(s->buffer + s->buffer_ptr, buf, len);
179 s->buffer_ptr += len;
180 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
181 for(;;) {
182 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
4364a3e0 183 if (ret > 0)
4972b26f
FB
184 break;
185 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
186 return -EIO;
187 }
188 s->buffer_ptr = 0;
189 }
190 buf += len;
191 size -= len;
192 }
193 return 0;
194}
195
196static int audio_write_trailer(AVFormatContext *s1)
197{
198 AudioData *s = s1->priv_data;
199
200 audio_close(s);
de6d9b64
FB
201 return 0;
202}
203
4972b26f
FB
204/* grab support */
205
206static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
207{
c9a65ca8 208 AudioData *s = s1->priv_data;
4972b26f
FB
209 AVStream *st;
210 int ret;
211
212 if (!ap || ap->sample_rate <= 0 || ap->channels <= 0)
213 return -1;
214
c9a65ca8 215 st = av_new_stream(s1, 0);
4972b26f 216 if (!st) {
4972b26f
FB
217 return -ENOMEM;
218 }
4972b26f
FB
219 s->sample_rate = ap->sample_rate;
220 s->channels = ap->channels;
221
7f172339 222 ret = audio_open(s, 0, ap->device);
4972b26f 223 if (ret < 0) {
1ea4f593 224 av_free(st);
4972b26f 225 return -EIO;
4972b26f 226 }
45dd5c69
FB
227
228 /* take real parameters */
229 st->codec.codec_type = CODEC_TYPE_AUDIO;
230 st->codec.codec_id = s->codec_id;
231 st->codec.sample_rate = s->sample_rate;
232 st->codec.channels = s->channels;
233
9ee91c2f 234 av_set_pts_info(st, 48, 1, 1000000); /* 48 bits pts in us */
45dd5c69 235 return 0;
4972b26f
FB
236}
237
238static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
239{
240 AudioData *s = s1->priv_data;
45dd5c69
FB
241 int ret, bdelay;
242 int64_t cur_time;
243 struct audio_buf_info abufi;
244
4972b26f
FB
245 if (av_new_packet(pkt, s->frame_size) < 0)
246 return -EIO;
247 for(;;) {
79134973
PG
248 struct timeval tv;
249 fd_set fds;
250
251 tv.tv_sec = 0;
252 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
253
254 FD_ZERO(&fds);
255 FD_SET(s->fd, &fds);
256
257 /* This will block until data is available or we get a timeout */
258 (void) select(s->fd + 1, &fds, 0, 0, &tv);
259
4972b26f
FB
260 ret = read(s->fd, pkt->data, pkt->size);
261 if (ret > 0)
262 break;
4606ac8d
ZK
263 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
264 av_free_packet(pkt);
265 pkt->size = 0;
6e454c38 266 pkt->pts = av_gettime() & ((1LL << 48) - 1);
4606ac8d
ZK
267 return 0;
268 }
4972b26f
FB
269 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
270 av_free_packet(pkt);
271 return -EIO;
272 }
273 }
274 pkt->size = ret;
45dd5c69
FB
275
276 /* compute pts of the start of the packet */
277 cur_time = av_gettime();
278 bdelay = ret;
279 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
280 bdelay += abufi.bytes;
281 }
282 /* substract time represented by the number of bytes in the audio fifo */
283 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
284
285 /* convert to wanted units */
286 pkt->pts = cur_time & ((1LL << 48) - 1);
287
1de1cce2
PG
288 if (s->flip_left && s->channels == 2) {
289 int i;
290 short *p = (short *) pkt->data;
291
292 for (i = 0; i < ret; i += 4) {
293 *p = ~*p;
294 p += 2;
295 }
296 }
4972b26f
FB
297 return 0;
298}
299
300static int audio_read_close(AVFormatContext *s1)
301{
302 AudioData *s = s1->priv_data;
303
304 audio_close(s);
4972b26f
FB
305 return 0;
306}
307
c18a2692 308static AVInputFormat audio_in_format = {
c9a65ca8
FB
309 "audio_device",
310 "audio grab and output",
311 sizeof(AudioData),
312 NULL,
313 audio_read_header,
314 audio_read_packet,
315 audio_read_close,
bb76a117 316 .flags = AVFMT_NOFILE,
c9a65ca8
FB
317};
318
c18a2692 319static AVOutputFormat audio_out_format = {
4972b26f
FB
320 "audio_device",
321 "audio grab and output",
322 "",
323 "",
c9a65ca8 324 sizeof(AudioData),
4972b26f
FB
325 /* XXX: we make the assumption that the soundcard accepts this format */
326 /* XXX: find better solution with "preinit" method, needed also in
327 other formats */
328#ifdef WORDS_BIGENDIAN
329 CODEC_ID_PCM_S16BE,
330#else
331 CODEC_ID_PCM_S16LE,
332#endif
333 CODEC_ID_NONE,
334 audio_write_header,
335 audio_write_packet,
336 audio_write_trailer,
bb76a117 337 .flags = AVFMT_NOFILE,
de6d9b64 338};
c9a65ca8
FB
339
340int audio_init(void)
341{
342 av_register_input_format(&audio_in_format);
343 av_register_output_format(&audio_out_format);
344 return 0;
345}