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