h264_metadata: Add option to delete filler data
[libav.git] / libavdevice / oss.c
1 /*
2 * Linux audio play and grab interface
3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23
24 #include <string.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/ioctl.h>
28 #include <sys/soundcard.h>
29
30 #include "libavutil/log.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/time.h"
33
34 #include "libavcodec/avcodec.h"
35
36 #include "libavformat/avformat.h"
37 #include "libavformat/internal.h"
38
39 #define OSS_AUDIO_BLOCK_SIZE 4096
40
41 typedef 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
53 static int oss_audio_open(AVFormatContext *s1, int is_output,
54 const char *audio_device)
55 {
56 OSSAudioData *s = s1->priv_data;
57 int audio_fd;
58 int tmp, err;
59 char *flip = getenv("AUDIO_FLIP_LEFT");
60 char errbuff[128];
61
62 if (is_output)
63 audio_fd = avpriv_open(audio_device, O_WRONLY);
64 else
65 audio_fd = avpriv_open(audio_device, O_RDONLY);
66 if (audio_fd < 0) {
67 av_log(s1, AV_LOG_ERROR, "%s: %s\n", audio_device, strerror(errno));
68 return AVERROR(EIO);
69 }
70
71 if (flip && *flip == '1') {
72 s->flip_left = 1;
73 }
74
75 /* non blocking mode */
76 if (!is_output)
77 fcntl(audio_fd, F_SETFL, O_NONBLOCK);
78
79 s->frame_size = OSS_AUDIO_BLOCK_SIZE;
80
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);
93
94 #if HAVE_BIGENDIAN
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:
114 s->codec_id = AV_CODEC_ID_PCM_S16LE;
115 break;
116 case AFMT_S16_BE:
117 s->codec_id = AV_CODEC_ID_PCM_S16BE;
118 break;
119 default:
120 av_log(s1, AV_LOG_ERROR, "Soundcard does not support 16 bit sample format\n");
121 close(audio_fd);
122 return AVERROR(EIO);
123 }
124 err=ioctl(audio_fd, SNDCTL_DSP_SETFMT, &tmp);
125 CHECK_IOCTL_ERROR(SNDCTL_DSP_SETFMTS)
126
127 tmp = (s->channels == 2);
128 err = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
129 CHECK_IOCTL_ERROR(SNDCTL_DSP_STEREO)
130
131 tmp = s->sample_rate;
132 err = ioctl(audio_fd, SNDCTL_DSP_SPEED, &tmp);
133 CHECK_IOCTL_ERROR(SNDCTL_DSP_SPEED)
134 s->sample_rate = tmp; /* store real sample rate */
135 s->fd = audio_fd;
136
137 return 0;
138 fail:
139 close(audio_fd);
140 return AVERROR(EIO);
141 #undef CHECK_IOCTL_ERROR
142 }
143
144 static int audio_read_header(AVFormatContext *s1)
145 {
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
170 static 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
213 static int audio_read_close(AVFormatContext *s1)
214 {
215 OSSAudioData *s = s1->priv_data;
216
217 close(s->fd);
218 return 0;
219 }
220
221 static 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
227 static 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
234 AVInputFormat 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 };