cleanup
[FFMpeg-mirror/ordered_chapters.git] / libavformat / audio.c
blobad0ad7ce45e4312061d37bd201f686991304bb80
1 /*
2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "avformat.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef __OpenBSD__
25 #include <soundcard.h>
26 #else
27 #include <sys/soundcard.h>
28 #endif
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <sys/mman.h>
33 #include <sys/time.h>
35 #define AUDIO_BLOCK_SIZE 4096
37 typedef struct {
38 int fd;
39 int sample_rate;
40 int channels;
41 int frame_size; /* in bytes ! */
42 int codec_id;
43 int flip_left : 1;
44 uint8_t buffer[AUDIO_BLOCK_SIZE];
45 int buffer_ptr;
46 } AudioData;
48 static int audio_open(AudioData *s, int is_output, const char *audio_device)
50 int audio_fd;
51 int tmp, err;
52 char *flip = getenv("AUDIO_FLIP_LEFT");
54 /* open linux audio device */
55 if (!audio_device)
56 #ifdef __OpenBSD__
57 audio_device = "/dev/sound";
58 #else
59 audio_device = "/dev/dsp";
60 #endif
62 if (is_output)
63 audio_fd = open(audio_device, O_WRONLY);
64 else
65 audio_fd = open(audio_device, O_RDONLY);
66 if (audio_fd < 0) {
67 perror(audio_device);
68 return AVERROR_IO;
71 if (flip && *flip == '1') {
72 s->flip_left = 1;
75 /* non blocking mode */
76 if (!is_output)
77 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
79 s->frame_size = AUDIO_BLOCK_SIZE;
80 #if 0
81 tmp = (NB_FRAGMENTS << 16) | FRAGMENT_BITS;
82 err = ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &tmp);
83 if (err < 0) {
84 perror("SNDCTL_DSP_SETFRAGMENT");
86 #endif
88 /* select format : favour native format */
89 err = ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
91 #ifdef WORDS_BIGENDIAN
92 if (tmp & AFMT_S16_BE) {
93 tmp = AFMT_S16_BE;
94 } else if (tmp & AFMT_S16_LE) {
95 tmp = AFMT_S16_LE;
96 } else {
97 tmp = 0;
99 #else
100 if (tmp & AFMT_S16_LE) {
101 tmp = AFMT_S16_LE;
102 } else if (tmp & AFMT_S16_BE) {
103 tmp = AFMT_S16_BE;
104 } else {
105 tmp = 0;
107 #endif
109 switch(tmp) {
110 case AFMT_S16_LE:
111 s->codec_id = CODEC_ID_PCM_S16LE;
112 break;
113 case AFMT_S16_BE:
114 s->codec_id = CODEC_ID_PCM_S16BE;
115 break;
116 default:
117 av_log(NULL, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
118 close(audio_fd);
119 return AVERROR_IO;
121 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
122 if (err < 0) {
123 perror("SNDCTL_DSP_SETFMT");
124 goto fail;
127 tmp = (s->channels == 2);
128 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
129 if (err < 0) {
130 perror("SNDCTL_DSP_STEREO");
131 goto fail;
133 if (tmp)
134 s->channels = 2;
136 tmp = s->sample_rate;
137 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
138 if (err < 0) {
139 perror("SNDCTL_DSP_SPEED");
140 goto fail;
142 s->sample_rate = tmp; /* store real sample rate */
143 s->fd = audio_fd;
145 return 0;
146 fail:
147 close(audio_fd);
148 return AVERROR_IO;
151 static int audio_close(AudioData *s)
153 close(s->fd);
154 return 0;
157 /* sound output support */
158 static int audio_write_header(AVFormatContext *s1)
160 AudioData *s = s1->priv_data;
161 AVStream *st;
162 int ret;
164 st = s1->streams[0];
165 s->sample_rate = st->codec->sample_rate;
166 s->channels = st->codec->channels;
167 ret = audio_open(s, 1, NULL);
168 if (ret < 0) {
169 return AVERROR_IO;
170 } else {
171 return 0;
175 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
177 AudioData *s = s1->priv_data;
178 int len, ret;
179 int size= pkt->size;
180 uint8_t *buf= pkt->data;
182 while (size > 0) {
183 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
184 if (len > size)
185 len = size;
186 memcpy(s->buffer + s->buffer_ptr, buf, len);
187 s->buffer_ptr += len;
188 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
189 for(;;) {
190 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
191 if (ret > 0)
192 break;
193 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
194 return AVERROR_IO;
196 s->buffer_ptr = 0;
198 buf += len;
199 size -= len;
201 return 0;
204 static int audio_write_trailer(AVFormatContext *s1)
206 AudioData *s = s1->priv_data;
208 audio_close(s);
209 return 0;
212 /* grab support */
214 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
216 AudioData *s = s1->priv_data;
217 AVStream *st;
218 int ret;
220 if (ap->sample_rate <= 0 || ap->channels <= 0)
221 return -1;
223 st = av_new_stream(s1, 0);
224 if (!st) {
225 return -ENOMEM;
227 s->sample_rate = ap->sample_rate;
228 s->channels = ap->channels;
230 ret = audio_open(s, 0, ap->device);
231 if (ret < 0) {
232 av_free(st);
233 return AVERROR_IO;
236 /* take real parameters */
237 st->codec->codec_type = CODEC_TYPE_AUDIO;
238 st->codec->codec_id = s->codec_id;
239 st->codec->sample_rate = s->sample_rate;
240 st->codec->channels = s->channels;
242 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
243 return 0;
246 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
248 AudioData *s = s1->priv_data;
249 int ret, bdelay;
250 int64_t cur_time;
251 struct audio_buf_info abufi;
253 if (av_new_packet(pkt, s->frame_size) < 0)
254 return AVERROR_IO;
255 for(;;) {
256 struct timeval tv;
257 fd_set fds;
259 tv.tv_sec = 0;
260 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
262 FD_ZERO(&fds);
263 FD_SET(s->fd, &fds);
265 /* This will block until data is available or we get a timeout */
266 (void) select(s->fd + 1, &fds, 0, 0, &tv);
268 ret = read(s->fd, pkt->data, pkt->size);
269 if (ret > 0)
270 break;
271 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
272 av_free_packet(pkt);
273 pkt->size = 0;
274 pkt->pts = av_gettime();
275 return 0;
277 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
278 av_free_packet(pkt);
279 return AVERROR_IO;
282 pkt->size = ret;
284 /* compute pts of the start of the packet */
285 cur_time = av_gettime();
286 bdelay = ret;
287 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
288 bdelay += abufi.bytes;
290 /* substract time represented by the number of bytes in the audio fifo */
291 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
293 /* convert to wanted units */
294 pkt->pts = cur_time;
296 if (s->flip_left && s->channels == 2) {
297 int i;
298 short *p = (short *) pkt->data;
300 for (i = 0; i < ret; i += 4) {
301 *p = ~*p;
302 p += 2;
305 return 0;
308 static int audio_read_close(AVFormatContext *s1)
310 AudioData *s = s1->priv_data;
312 audio_close(s);
313 return 0;
316 #ifdef CONFIG_AUDIO_DEMUXER
317 AVInputFormat audio_demuxer = {
318 "audio_device",
319 "audio grab and output",
320 sizeof(AudioData),
321 NULL,
322 audio_read_header,
323 audio_read_packet,
324 audio_read_close,
325 .flags = AVFMT_NOFILE,
327 #endif
329 #ifdef CONFIG_AUDIO_MUXER
330 AVOutputFormat audio_muxer = {
331 "audio_device",
332 "audio grab and output",
335 sizeof(AudioData),
336 /* XXX: we make the assumption that the soundcard accepts this format */
337 /* XXX: find better solution with "preinit" method, needed also in
338 other formats */
339 #ifdef WORDS_BIGENDIAN
340 CODEC_ID_PCM_S16BE,
341 #else
342 CODEC_ID_PCM_S16LE,
343 #endif
344 CODEC_ID_NONE,
345 audio_write_header,
346 audio_write_packet,
347 audio_write_trailer,
348 .flags = AVFMT_NOFILE,
350 #endif