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