Strong filtering function for future RV40 loop filter
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavdevice / audio.c
blobd056711d03d69ae7c517dd3f5fe3600c35494c04
1 /*
2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <errno.h>
28 #ifdef HAVE_SOUNDCARD_H
29 #include <soundcard.h>
30 #else
31 #include <sys/soundcard.h>
32 #endif
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <sys/ioctl.h>
36 #include <sys/time.h>
37 #include <sys/select.h>
39 #include "libavutil/log.h"
40 #include "libavcodec/avcodec.h"
41 #include "libavformat/avformat.h"
43 #define AUDIO_BLOCK_SIZE 4096
45 typedef struct {
46 int fd;
47 int sample_rate;
48 int channels;
49 int frame_size; /* in bytes ! */
50 enum CodecID codec_id;
51 unsigned int flip_left : 1;
52 uint8_t buffer[AUDIO_BLOCK_SIZE];
53 int buffer_ptr;
54 } AudioData;
56 static int audio_open(AudioData *s, int is_output, const char *audio_device)
58 int audio_fd;
59 int tmp, err;
60 char *flip = getenv("AUDIO_FLIP_LEFT");
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 av_log(NULL, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
68 return AVERROR(EIO);
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(EIO);
121 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
122 if (err < 0) {
123 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SETFMT: %s\n", strerror(errno));
124 goto fail;
127 tmp = (s->channels == 2);
128 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
129 if (err < 0) {
130 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_STEREO: %s\n", strerror(errno));
131 goto fail;
134 tmp = s->sample_rate;
135 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
136 if (err < 0) {
137 av_log(NULL, AV_LOG_ERROR, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
138 goto fail;
140 s->sample_rate = tmp; /* store real sample rate */
141 s->fd = audio_fd;
143 return 0;
144 fail:
145 close(audio_fd);
146 return AVERROR(EIO);
149 static int audio_close(AudioData *s)
151 close(s->fd);
152 return 0;
155 /* sound output support */
156 static int audio_write_header(AVFormatContext *s1)
158 AudioData *s = s1->priv_data;
159 AVStream *st;
160 int ret;
162 st = s1->streams[0];
163 s->sample_rate = st->codec->sample_rate;
164 s->channels = st->codec->channels;
165 ret = audio_open(s, 1, s1->filename);
166 if (ret < 0) {
167 return AVERROR(EIO);
168 } else {
169 return 0;
173 static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
175 AudioData *s = s1->priv_data;
176 int len, ret;
177 int size= pkt->size;
178 uint8_t *buf= pkt->data;
180 while (size > 0) {
181 len = AUDIO_BLOCK_SIZE - s->buffer_ptr;
182 if (len > size)
183 len = size;
184 memcpy(s->buffer + s->buffer_ptr, buf, len);
185 s->buffer_ptr += len;
186 if (s->buffer_ptr >= AUDIO_BLOCK_SIZE) {
187 for(;;) {
188 ret = write(s->fd, s->buffer, AUDIO_BLOCK_SIZE);
189 if (ret > 0)
190 break;
191 if (ret < 0 && (errno != EAGAIN && errno != EINTR))
192 return AVERROR(EIO);
194 s->buffer_ptr = 0;
196 buf += len;
197 size -= len;
199 return 0;
202 static int audio_write_trailer(AVFormatContext *s1)
204 AudioData *s = s1->priv_data;
206 audio_close(s);
207 return 0;
210 /* grab support */
212 static int audio_read_header(AVFormatContext *s1, AVFormatParameters *ap)
214 AudioData *s = s1->priv_data;
215 AVStream *st;
216 int ret;
218 if (ap->sample_rate <= 0 || ap->channels <= 0)
219 return -1;
221 st = av_new_stream(s1, 0);
222 if (!st) {
223 return AVERROR(ENOMEM);
225 s->sample_rate = ap->sample_rate;
226 s->channels = ap->channels;
228 ret = audio_open(s, 0, s1->filename);
229 if (ret < 0) {
230 av_free(st);
231 return AVERROR(EIO);
234 /* take real parameters */
235 st->codec->codec_type = CODEC_TYPE_AUDIO;
236 st->codec->codec_id = s->codec_id;
237 st->codec->sample_rate = s->sample_rate;
238 st->codec->channels = s->channels;
240 av_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
241 return 0;
244 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
246 AudioData *s = s1->priv_data;
247 int ret, bdelay;
248 int64_t cur_time;
249 struct audio_buf_info abufi;
251 if (av_new_packet(pkt, s->frame_size) < 0)
252 return AVERROR(EIO);
253 for(;;) {
254 struct timeval tv;
255 fd_set fds;
257 tv.tv_sec = 0;
258 tv.tv_usec = 30 * 1000; /* 30 msecs -- a bit shorter than 1 frame at 30fps */
260 FD_ZERO(&fds);
261 FD_SET(s->fd, &fds);
263 /* This will block until data is available or we get a timeout */
264 (void) select(s->fd + 1, &fds, 0, 0, &tv);
266 ret = read(s->fd, pkt->data, pkt->size);
267 if (ret > 0)
268 break;
269 if (ret == -1 && (errno == EAGAIN || errno == EINTR)) {
270 av_free_packet(pkt);
271 pkt->size = 0;
272 pkt->pts = av_gettime();
273 return 0;
275 if (!(ret == 0 || (ret == -1 && (errno == EAGAIN || errno == EINTR)))) {
276 av_free_packet(pkt);
277 return AVERROR(EIO);
280 pkt->size = ret;
282 /* compute pts of the start of the packet */
283 cur_time = av_gettime();
284 bdelay = ret;
285 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
286 bdelay += abufi.bytes;
288 /* subtract time represented by the number of bytes in the audio fifo */
289 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
291 /* convert to wanted units */
292 pkt->pts = cur_time;
294 if (s->flip_left && s->channels == 2) {
295 int i;
296 short *p = (short *) pkt->data;
298 for (i = 0; i < ret; i += 4) {
299 *p = ~*p;
300 p += 2;
303 return 0;
306 static int audio_read_close(AVFormatContext *s1)
308 AudioData *s = s1->priv_data;
310 audio_close(s);
311 return 0;
314 #ifdef CONFIG_OSS_DEMUXER
315 AVInputFormat oss_demuxer = {
316 "oss",
317 NULL_IF_CONFIG_SMALL("Open Sound System capture"),
318 sizeof(AudioData),
319 NULL,
320 audio_read_header,
321 audio_read_packet,
322 audio_read_close,
323 .flags = AVFMT_NOFILE,
325 #endif
327 #ifdef CONFIG_OSS_MUXER
328 AVOutputFormat oss_muxer = {
329 "oss",
330 NULL_IF_CONFIG_SMALL("Open Sound System playback"),
333 sizeof(AudioData),
334 /* XXX: we make the assumption that the soundcard accepts this format */
335 /* XXX: find better solution with "preinit" method, needed also in
336 other formats */
337 #ifdef WORDS_BIGENDIAN
338 CODEC_ID_PCM_S16BE,
339 #else
340 CODEC_ID_PCM_S16LE,
341 #endif
342 CODEC_ID_NONE,
343 audio_write_header,
344 audio_write_packet,
345 audio_write_trailer,
346 .flags = AVFMT_NOFILE,
348 #endif