h264_metadata: Add option to delete filler data
[libav.git] / libavdevice / sndio.c
CommitLineData
15d59d2c 1/*
8d3db95f 2 * sndio grab interface
15d59d2c
B
3 * Copyright (c) 2010 Jacob Meuser
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 <stdint.h>
23#include <sndio.h>
24
8d3db95f
DB
25#include "libavutil/internal.h"
26#include "libavutil/opt.h"
27#include "libavutil/time.h"
28
15d59d2c 29#include "libavformat/avformat.h"
8d3db95f 30#include "libavformat/internal.h"
15d59d2c 31
8d3db95f
DB
32typedef struct SndioData {
33 AVClass *class;
34 struct sio_hdl *hdl;
35 enum AVCodecID codec_id;
36 int64_t hwpos;
37 int64_t softpos;
38 uint8_t *buffer;
39 int bps;
40 int buffer_size;
41 int buffer_offset;
42 int channels;
43 int sample_rate;
44} SndioData;
15d59d2c
B
45
46static inline void movecb(void *addr, int delta)
47{
48 SndioData *s = addr;
49
50 s->hwpos += delta * s->channels * s->bps;
51}
52
8d3db95f
DB
53static av_cold int sndio_open(AVFormatContext *s1, int is_output,
54 const char *audio_device)
15d59d2c
B
55{
56 SndioData *s = s1->priv_data;
57 struct sio_hdl *hdl;
58 struct sio_par par;
59
60 hdl = sio_open(audio_device, is_output ? SIO_PLAY : SIO_REC, 0);
61 if (!hdl) {
62 av_log(s1, AV_LOG_ERROR, "Could not open sndio device\n");
63 return AVERROR(EIO);
64 }
65
66 sio_initpar(&par);
67
68 par.bits = 16;
69 par.sig = 1;
70 par.le = SIO_LE_NATIVE;
71
72 if (is_output)
73 par.pchan = s->channels;
74 else
75 par.rchan = s->channels;
76 par.rate = s->sample_rate;
77
78 if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
79 av_log(s1, AV_LOG_ERROR, "Impossible to set sndio parameters, "
80 "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
81 goto fail;
82 }
83
2eddfb2b 84 if (par.bits != 16 || par.sig != 1 ||
15d59d2c
B
85 (is_output && (par.pchan != s->channels)) ||
86 (!is_output && (par.rchan != s->channels)) ||
87 (par.rate != s->sample_rate)) {
88 av_log(s1, AV_LOG_ERROR, "Could not set appropriate sndio parameters, "
89 "channels: %d sample rate: %d\n", s->channels, s->sample_rate);
90 goto fail;
91 }
92
93 s->buffer_size = par.round * par.bps *
94 (is_output ? par.pchan : par.rchan);
95
96 if (is_output) {
97 s->buffer = av_malloc(s->buffer_size);
98 if (!s->buffer) {
99 av_log(s1, AV_LOG_ERROR, "Could not allocate buffer\n");
100 goto fail;
101 }
102 }
103
36ef5369 104 s->codec_id = par.le ? AV_CODEC_ID_PCM_S16LE : AV_CODEC_ID_PCM_S16BE;
15d59d2c
B
105 s->channels = is_output ? par.pchan : par.rchan;
106 s->sample_rate = par.rate;
107 s->bps = par.bps;
108
109 sio_onmove(hdl, movecb, s);
110
111 if (!sio_start(hdl)) {
112 av_log(s1, AV_LOG_ERROR, "Could not start sndio\n");
113 goto fail;
114 }
115
116 s->hdl = hdl;
117
118 return 0;
119
120fail:
121 av_freep(&s->buffer);
122
123 if (hdl)
124 sio_close(hdl);
125
126 return AVERROR(EIO);
127}
128
8d3db95f
DB
129static av_cold int audio_read_header(AVFormatContext *s1)
130{
131 SndioData *s = s1->priv_data;
132 AVStream *st;
133 int ret;
134
135 st = avformat_new_stream(s1, NULL);
136 if (!st)
137 return AVERROR(ENOMEM);
138
139 ret = sndio_open(s1, 0, s1->filename);
140 if (ret < 0)
141 return ret;
142
143 /* take real parameters */
144 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
145 st->codecpar->codec_id = s->codec_id;
146 st->codecpar->sample_rate = s->sample_rate;
147 st->codecpar->channels = s->channels;
148
149 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
150
151 return 0;
152}
153
154static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
155{
156 SndioData *s = s1->priv_data;
157 int64_t bdelay, cur_time;
158 int ret;
159
160 if ((ret = av_new_packet(pkt, s->buffer_size)) < 0)
161 return ret;
162
163 ret = sio_read(s->hdl, pkt->data, pkt->size);
164 if (ret == 0 || sio_eof(s->hdl)) {
165 av_packet_unref(pkt);
166 return AVERROR_EOF;
167 }
168
169 pkt->size = ret;
170 s->softpos += ret;
171
172 /* compute pts of the start of the packet */
173 cur_time = av_gettime();
174
175 bdelay = ret + s->hwpos - s->softpos;
176
177 /* convert to pts */
178 pkt->pts = cur_time - ((bdelay * 1000000) /
179 (s->bps * s->channels * s->sample_rate));
180
181 return 0;
182}
183
184static av_cold int audio_read_close(AVFormatContext *s1)
15d59d2c 185{
8d3db95f
DB
186 SndioData *s = s1->priv_data;
187
15d59d2c
B
188 av_freep(&s->buffer);
189
190 if (s->hdl)
191 sio_close(s->hdl);
192
193 return 0;
194}
8d3db95f
DB
195
196static const AVOption options[] = {
197 { "sample_rate", "", offsetof(SndioData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
198 { "channels", "", offsetof(SndioData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
199 { NULL },
200};
201
202static const AVClass sndio_demuxer_class = {
203 .class_name = "sndio indev",
204 .item_name = av_default_item_name,
205 .option = options,
206 .version = LIBAVUTIL_VERSION_INT,
207};
208
209AVInputFormat ff_sndio_demuxer = {
210 .name = "sndio",
211 .long_name = NULL_IF_CONFIG_SMALL("sndio audio capture"),
212 .priv_data_size = sizeof(SndioData),
213 .read_header = audio_read_header,
214 .read_packet = audio_read_packet,
215 .read_close = audio_read_close,
216 .flags = AVFMT_NOFILE,
217 .priv_class = &sndio_demuxer_class,
218};