h264_metadata: Add option to delete filler data
[libav.git] / libavdevice / oss.c
CommitLineData
de6d9b64
FB
1/*
2 * Linux audio play and grab interface
406792e7 3 * Copyright (c) 2000, 2001 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; 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"
d6e1d371 23
de6d9b64 24#include <string.h>
de6d9b64
FB
25#include <unistd.h>
26#include <fcntl.h>
27#include <sys/ioctl.h>
b586903a 28#include <sys/soundcard.h>
de6d9b64 29
245976da 30#include "libavutil/log.h"
6ce13070
DB
31#include "libavutil/opt.h"
32#include "libavutil/time.h"
d6e1d371 33
245976da 34#include "libavcodec/avcodec.h"
4f786086 35
d6e1d371 36#include "libavformat/avformat.h"
6ce13070
DB
37#include "libavformat/internal.h"
38
39#define OSS_AUDIO_BLOCK_SIZE 4096
40
41typedef struct OSSAudioData {
42 AVClass *class;
43 int fd;
44 int sample_rate;
45 int channels;
46 int frame_size; /* in bytes ! */
47 enum AVCodecID codec_id;
48 unsigned int flip_left : 1;
49 uint8_t buffer[OSS_AUDIO_BLOCK_SIZE];
50 int buffer_ptr;
51} OSSAudioData;
52
53static int oss_audio_open(AVFormatContext *s1, int is_output,
54 const char *audio_device)
de6d9b64 55{
d6e1d371 56 OSSAudioData *s = s1->priv_data;
4972b26f 57 int audio_fd;
de6d9b64 58 int tmp, err;
1de1cce2 59 char *flip = getenv("AUDIO_FLIP_LEFT");
b7c77912 60 char errbuff[128];
de6d9b64 61
4972b26f 62 if (is_output)
71bf6b41 63 audio_fd = avpriv_open(audio_device, O_WRONLY);
de6d9b64 64 else
71bf6b41 65 audio_fd = avpriv_open(audio_device, O_RDONLY);
de6d9b64 66 if (audio_fd < 0) {
0c26e964 67 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
6f3e0b21 68 return AVERROR(EIO);
de6d9b64
FB
69 }
70
1de1cce2
PG
71 if (flip && *flip == '1') {
72 s->flip_left = 1;
73 }
74
de6d9b64 75 /* non blocking mode */
4364a3e0
FB
76 if (!is_output)
77 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
de6d9b64 78
d6e1d371 79 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
de6d9b64 80
b7c77912
TG
81#define CHECK_IOCTL_ERROR(event) \
82 if (err < 0) { \
83 av_strerror(AVERROR(errno), errbuff, sizeof(errbuff)); \
84 av_log(s1, AV_LOG_ERROR, #event ": %s\n", errbuff); \
85 goto fail; \
86 }
87
88 /* select format : favour native format
89 * We don't CHECK_IOCTL_ERROR here because even if failed OSS still may be
90 * usable. If OSS is not usable the SNDCTL_DSP_SETFMTS later is going to
91 * fail anyway. */
92 (void) ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &tmp);
115329f1 93
63613fe6 94#if HAVE_BIGENDIAN
4972b26f
FB
95 if (tmp & AFMT_S16_BE) {
96 tmp = AFMT_S16_BE;
97 } else if (tmp & AFMT_S16_LE) {
98 tmp = AFMT_S16_LE;
99 } else {
100 tmp = 0;
101 }
102#else
103 if (tmp & AFMT_S16_LE) {
104 tmp = AFMT_S16_LE;
105 } else if (tmp & AFMT_S16_BE) {
106 tmp = AFMT_S16_BE;
107 } else {
108 tmp = 0;
109 }
110#endif
111
112 switch(tmp) {
113 case AFMT_S16_LE:
36ef5369 114 s->codec_id = AV_CODEC_ID_PCM_S16LE;
4972b26f
FB
115 break;
116 case AFMT_S16_BE:
36ef5369 117 s->codec_id = AV_CODEC_ID_PCM_S16BE;
4972b26f
FB
118 break;
119 default:
0c26e964 120 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
4972b26f 121 close(audio_fd);
6f3e0b21 122 return AVERROR(EIO);
4972b26f
FB
123 }
124 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
b7c77912 125 CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
115329f1 126
4972b26f
FB
127 tmp = (s->channels == 2);
128 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
b7c77912 129 CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
115329f1 130
4972b26f
FB
131 tmp = s->sample_rate;
132 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
b7c77912 133 CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
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);
6f3e0b21 140 return AVERROR(EIO);
b7c77912 141#undef CHECK_IOCTL_ERROR
de6d9b64
FB
142}
143
6ce13070 144static int audio_read_header(AVFormatContext *s1)
de6d9b64 145{
6ce13070
DB
146 OSSAudioData *s = s1->priv_data;
147 AVStream *st;
148 int ret;
149
150 st = avformat_new_stream(s1, NULL);
151 if (!st) {
152 return AVERROR(ENOMEM);
153 }
154
155 ret = oss_audio_open(s1, 0, s1->filename);
156 if (ret < 0) {
157 return AVERROR(EIO);
158 }
159
160 /* take real parameters */
161 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
162 st->codecpar->codec_id = s->codec_id;
163 st->codecpar->sample_rate = s->sample_rate;
164 st->codecpar->channels = s->channels;
165
166 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
167 return 0;
168}
169
170static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
171{
172 OSSAudioData *s = s1->priv_data;
173 int ret, bdelay;
174 int64_t cur_time;
175 struct audio_buf_info abufi;
176
177 if ((ret=av_new_packet(pkt, s->frame_size)) < 0)
178 return ret;
179
180 ret = read(s->fd, pkt->data, pkt->size);
181 if (ret <= 0){
182 av_packet_unref(pkt);
183 pkt->size = 0;
184 if (ret<0) return AVERROR(errno);
185 else return AVERROR_EOF;
186 }
187 pkt->size = ret;
188
189 /* compute pts of the start of the packet */
190 cur_time = av_gettime();
191 bdelay = ret;
192 if (ioctl(s->fd, SNDCTL_DSP_GETISPACE, &abufi) == 0) {
193 bdelay += abufi.bytes;
194 }
195 /* subtract time represented by the number of bytes in the audio fifo */
196 cur_time -= (bdelay * 1000000LL) / (s->sample_rate * s->channels);
197
198 /* convert to wanted units */
199 pkt->pts = cur_time;
200
201 if (s->flip_left && s->channels == 2) {
202 int i;
203 short *p = (short *) pkt->data;
204
205 for (i = 0; i < ret; i += 4) {
206 *p = ~*p;
207 p += 2;
208 }
209 }
210 return 0;
211}
212
213static int audio_read_close(AVFormatContext *s1)
214{
215 OSSAudioData *s = s1->priv_data;
216
de6d9b64 217 close(s->fd);
4972b26f
FB
218 return 0;
219}
6ce13070
DB
220
221static const AVOption options[] = {
222 { "sample_rate", "", offsetof(OSSAudioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
223 { "channels", "", offsetof(OSSAudioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
224 { NULL },
225};
226
227static const AVClass oss_demuxer_class = {
228 .class_name = "OSS demuxer",
229 .item_name = av_default_item_name,
230 .option = options,
231 .version = LIBAVUTIL_VERSION_INT,
232};
233
234AVInputFormat ff_oss_demuxer = {
235 .name = "oss",
236 .long_name = NULL_IF_CONFIG_SMALL("OSS (Open Sound System) capture"),
237 .priv_data_size = sizeof(OSSAudioData),
238 .read_header = audio_read_header,
239 .read_packet = audio_read_packet,
240 .read_close = audio_read_close,
241 .flags = AVFMT_NOFILE,
242 .priv_class = &oss_demuxer_class,
243};