h264_metadata: Add option to delete filler data
[libav.git] / libavdevice / alsa.c
1 /*
2 * ALSA input
3 * Copyright (c) 2007 Luca Abeni ( lucabe72 email it )
4 * Copyright (c) 2007 Benoit Fouet ( benoit fouet free fr )
5 *
6 * This file is part of Libav.
7 *
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * ALSA input
26 * @author Luca Abeni ( lucabe72 email it )
27 * @author Benoit Fouet ( benoit fouet free fr )
28 * @author Nicolas George ( nicolas george normalesup org )
29 */
30
31 #include <alsa/asoundlib.h>
32
33 #include "libavutil/avassert.h"
34 #include "libavutil/channel_layout.h"
35 #include "libavutil/opt.h"
36
37 #include "libavformat/avformat.h"
38 #include "libavformat/internal.h"
39
40 /* XXX: we make the assumption that the soundcard accepts this format */
41 /* XXX: find better solution with "preinit" method, needed also in
42 other formats */
43 #define DEFAULT_CODEC_ID AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE)
44
45 #define ALSA_BUFFER_SIZE_MAX 32768
46
47 typedef struct AlsaData {
48 AVClass *class;
49 snd_pcm_t *h;
50 int frame_size; ///< preferred size for reads and writes
51 int period_size; ///< bytes per sample * channels
52 int sample_rate; ///< sample rate set by user
53 int channels; ///< number of channels set by user
54 void (*reorder_func)(const void *, void *, int);
55 void *reorder_buf;
56 int reorder_buf_size; ///< in frames
57 } AlsaData;
58
59 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
60 {
61 switch(codec_id) {
62 case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
63 case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
64 case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
65 case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
66 case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
67 case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
68 case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
69 case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
70 case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
71 case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
72 case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
73 case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
74 case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
75 case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
76 case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
77 case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
78 case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
79 case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
80 case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
81 case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
82 default: return SND_PCM_FORMAT_UNKNOWN;
83 }
84 }
85
86 #define REORDER_OUT_50(NAME, TYPE) \
87 static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
88 { \
89 const TYPE *in = in_v; \
90 TYPE *out = out_v; \
91 \
92 while (n-- > 0) { \
93 out[0] = in[0]; \
94 out[1] = in[1]; \
95 out[2] = in[3]; \
96 out[3] = in[4]; \
97 out[4] = in[2]; \
98 in += 5; \
99 out += 5; \
100 } \
101 }
102
103 #define REORDER_OUT_51(NAME, TYPE) \
104 static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
105 { \
106 const TYPE *in = in_v; \
107 TYPE *out = out_v; \
108 \
109 while (n-- > 0) { \
110 out[0] = in[0]; \
111 out[1] = in[1]; \
112 out[2] = in[4]; \
113 out[3] = in[5]; \
114 out[4] = in[2]; \
115 out[5] = in[3]; \
116 in += 6; \
117 out += 6; \
118 } \
119 }
120
121 #define REORDER_OUT_71(NAME, TYPE) \
122 static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
123 { \
124 const TYPE *in = in_v; \
125 TYPE *out = out_v; \
126 \
127 while (n-- > 0) { \
128 out[0] = in[0]; \
129 out[1] = in[1]; \
130 out[2] = in[4]; \
131 out[3] = in[5]; \
132 out[4] = in[2]; \
133 out[5] = in[3]; \
134 out[6] = in[6]; \
135 out[7] = in[7]; \
136 in += 8; \
137 out += 8; \
138 } \
139 }
140
141 REORDER_OUT_50(int8, int8_t)
142 REORDER_OUT_51(int8, int8_t)
143 REORDER_OUT_71(int8, int8_t)
144 REORDER_OUT_50(int16, int16_t)
145 REORDER_OUT_51(int16, int16_t)
146 REORDER_OUT_71(int16, int16_t)
147 REORDER_OUT_50(int32, int32_t)
148 REORDER_OUT_51(int32, int32_t)
149 REORDER_OUT_71(int32, int32_t)
150 REORDER_OUT_50(f32, float)
151 REORDER_OUT_51(f32, float)
152 REORDER_OUT_71(f32, float)
153
154 #define FORMAT_I8 0
155 #define FORMAT_I16 1
156 #define FORMAT_I32 2
157 #define FORMAT_F32 3
158
159 #define PICK_REORDER(layout)\
160 switch(format) {\
161 case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
162 case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
163 case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
164 case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
165 }
166
167 static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
168 {
169 int format;
170
171 /* reordering input is not currently supported */
172 if (!out)
173 return AVERROR(ENOSYS);
174
175 /* reordering is not needed for QUAD or 2_2 layout */
176 if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
177 return 0;
178
179 switch (codec_id) {
180 case AV_CODEC_ID_PCM_S8:
181 case AV_CODEC_ID_PCM_U8:
182 case AV_CODEC_ID_PCM_ALAW:
183 case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
184 case AV_CODEC_ID_PCM_S16LE:
185 case AV_CODEC_ID_PCM_S16BE:
186 case AV_CODEC_ID_PCM_U16LE:
187 case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
188 case AV_CODEC_ID_PCM_S32LE:
189 case AV_CODEC_ID_PCM_S32BE:
190 case AV_CODEC_ID_PCM_U32LE:
191 case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
192 case AV_CODEC_ID_PCM_F32LE:
193 case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
194 default: return AVERROR(ENOSYS);
195 }
196
197 if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
198 PICK_REORDER(50)
199 else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
200 PICK_REORDER(51)
201 else if (layout == AV_CH_LAYOUT_7POINT1)
202 PICK_REORDER(71)
203
204 return s->reorder_func ? 0 : AVERROR(ENOSYS);
205 }
206
207 /**
208 * Open an ALSA PCM.
209 *
210 * @param s media file handle
211 * @param mode either SND_PCM_STREAM_CAPTURE or SND_PCM_STREAM_PLAYBACK
212 * @param sample_rate in: requested sample rate;
213 * out: actually selected sample rate
214 * @param channels number of channels
215 * @param codec_id in: requested AVCodecID or AV_CODEC_ID_NONE;
216 * out: actually selected AVCodecID, changed only if
217 * AV_CODEC_ID_NONE was requested
218 *
219 * @return 0 if OK, AVERROR_xxx on error
220 */
221 static av_cold int alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
222 unsigned int *sample_rate,
223 int channels, enum AVCodecID *codec_id)
224 {
225 AlsaData *s = ctx->priv_data;
226 const char *audio_device;
227 int res, flags = 0;
228 snd_pcm_format_t format;
229 snd_pcm_t *h;
230 snd_pcm_hw_params_t *hw_params;
231 snd_pcm_uframes_t buffer_size, period_size;
232 uint64_t layout = ctx->streams[0]->codecpar->channel_layout;
233
234 if (ctx->filename[0] == 0) audio_device = "default";
235 else audio_device = ctx->filename;
236
237 if (*codec_id == AV_CODEC_ID_NONE)
238 *codec_id = DEFAULT_CODEC_ID;
239 format = codec_id_to_pcm_format(*codec_id);
240 if (format == SND_PCM_FORMAT_UNKNOWN) {
241 av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
242 return AVERROR(ENOSYS);
243 }
244 s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
245
246 if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
247 flags = SND_PCM_NONBLOCK;
248 }
249 res = snd_pcm_open(&h, audio_device, mode, flags);
250 if (res < 0) {
251 av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
252 audio_device, snd_strerror(res));
253 return AVERROR(EIO);
254 }
255
256 res = snd_pcm_hw_params_malloc(&hw_params);
257 if (res < 0) {
258 av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
259 snd_strerror(res));
260 goto fail1;
261 }
262
263 res = snd_pcm_hw_params_any(h, hw_params);
264 if (res < 0) {
265 av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
266 snd_strerror(res));
267 goto fail;
268 }
269
270 res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
271 if (res < 0) {
272 av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
273 snd_strerror(res));
274 goto fail;
275 }
276
277 res = snd_pcm_hw_params_set_format(h, hw_params, format);
278 if (res < 0) {
279 av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
280 *codec_id, format, snd_strerror(res));
281 goto fail;
282 }
283
284 res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
285 if (res < 0) {
286 av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
287 snd_strerror(res));
288 goto fail;
289 }
290
291 res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
292 if (res < 0) {
293 av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
294 channels, snd_strerror(res));
295 goto fail;
296 }
297
298 snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
299 buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
300 /* TODO: maybe use ctx->max_picture_buffer somehow */
301 res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
302 if (res < 0) {
303 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
304 snd_strerror(res));
305 goto fail;
306 }
307
308 snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
309 if (!period_size)
310 period_size = buffer_size / 4;
311 res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
312 if (res < 0) {
313 av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
314 snd_strerror(res));
315 goto fail;
316 }
317 s->period_size = period_size;
318
319 res = snd_pcm_hw_params(h, hw_params);
320 if (res < 0) {
321 av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
322 snd_strerror(res));
323 goto fail;
324 }
325
326 snd_pcm_hw_params_free(hw_params);
327
328 if (channels > 2 && layout) {
329 if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
330 char name[128];
331 av_get_channel_layout_string(name, sizeof(name), channels, layout);
332 av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
333 name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
334 }
335 if (s->reorder_func) {
336 s->reorder_buf_size = buffer_size;
337 s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
338 if (!s->reorder_buf)
339 goto fail1;
340 }
341 }
342
343 s->h = h;
344 return 0;
345
346 fail:
347 snd_pcm_hw_params_free(hw_params);
348 fail1:
349 snd_pcm_close(h);
350 return AVERROR(EIO);
351 }
352
353 /**
354 * Close the ALSA PCM.
355 *
356 * @param s1 media file handle
357 *
358 * @return 0
359 */
360 static av_cold int alsa_close(AVFormatContext *s1)
361 {
362 AlsaData *s = s1->priv_data;
363
364 av_freep(&s->reorder_buf);
365 snd_pcm_close(s->h);
366 return 0;
367 }
368
369 /**
370 * Try to recover from ALSA buffer underrun.
371 *
372 * @param s1 media file handle
373 * @param err error code reported by the previous ALSA call
374 *
375 * @return 0 if OK, AVERROR_xxx on error
376 */
377 static int alsa_xrun_recover(AVFormatContext *s1, int err)
378 {
379 AlsaData *s = s1->priv_data;
380 snd_pcm_t *handle = s->h;
381
382 av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
383 if (err == -EPIPE) {
384 err = snd_pcm_prepare(handle);
385 if (err < 0) {
386 av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
387
388 return AVERROR(EIO);
389 }
390 } else if (err == -ESTRPIPE) {
391 av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
392
393 return -1;
394 }
395 return err;
396 }
397
398 static av_cold int audio_read_header(AVFormatContext *s1)
399 {
400 AlsaData *s = s1->priv_data;
401 AVStream *st;
402 int ret;
403 enum AVCodecID codec_id;
404 snd_pcm_sw_params_t *sw_params;
405
406 st = avformat_new_stream(s1, NULL);
407 if (!st) {
408 av_log(s1, AV_LOG_ERROR, "Cannot add stream\n");
409
410 return AVERROR(ENOMEM);
411 }
412 codec_id = s1->audio_codec_id;
413
414 ret = alsa_open(s1, SND_PCM_STREAM_CAPTURE, &s->sample_rate, s->channels,
415 &codec_id);
416 if (ret < 0) {
417 return AVERROR(EIO);
418 }
419
420 if (snd_pcm_type(s->h) != SND_PCM_TYPE_HW)
421 av_log(s1, AV_LOG_WARNING,
422 "capture with some ALSA plugins, especially dsnoop, "
423 "may hang.\n");
424
425 ret = snd_pcm_sw_params_malloc(&sw_params);
426 if (ret < 0) {
427 av_log(s1, AV_LOG_ERROR, "cannot allocate software parameters structure (%s)\n",
428 snd_strerror(ret));
429 goto fail;
430 }
431
432 snd_pcm_sw_params_current(s->h, sw_params);
433 snd_pcm_sw_params_set_tstamp_mode(s->h, sw_params, SND_PCM_TSTAMP_ENABLE);
434
435 ret = snd_pcm_sw_params(s->h, sw_params);
436 snd_pcm_sw_params_free(sw_params);
437 if (ret < 0) {
438 av_log(s1, AV_LOG_ERROR, "cannot install ALSA software parameters (%s)\n",
439 snd_strerror(ret));
440 goto fail;
441 }
442
443 /* take real parameters */
444 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
445 st->codecpar->codec_id = codec_id;
446 st->codecpar->sample_rate = s->sample_rate;
447 st->codecpar->channels = s->channels;
448 avpriv_set_pts_info(st, 64, 1, 1000000); /* 64 bits pts in us */
449
450 return 0;
451
452 fail:
453 snd_pcm_close(s->h);
454 return AVERROR(EIO);
455 }
456
457 static int audio_read_packet(AVFormatContext *s1, AVPacket *pkt)
458 {
459 AlsaData *s = s1->priv_data;
460 AVStream *st = s1->streams[0];
461 int res;
462 snd_htimestamp_t timestamp;
463 snd_pcm_uframes_t ts_delay;
464
465 if (av_new_packet(pkt, s->period_size) < 0) {
466 return AVERROR(EIO);
467 }
468
469 while ((res = snd_pcm_readi(s->h, pkt->data, pkt->size / s->frame_size)) < 0) {
470 if (res == -EAGAIN) {
471 av_packet_unref(pkt);
472
473 return AVERROR(EAGAIN);
474 }
475 if (alsa_xrun_recover(s1, res) < 0) {
476 av_log(s1, AV_LOG_ERROR, "ALSA read error: %s\n",
477 snd_strerror(res));
478 av_packet_unref(pkt);
479
480 return AVERROR(EIO);
481 }
482 }
483
484 snd_pcm_htimestamp(s->h, &ts_delay, &timestamp);
485 ts_delay += res;
486 pkt->pts = timestamp.tv_sec * 1000000LL
487 + (timestamp.tv_nsec * st->codecpar->sample_rate
488 - (int64_t)ts_delay * 1000000000LL + st->codecpar->sample_rate * 500LL)
489 / (st->codecpar->sample_rate * 1000LL);
490
491 pkt->size = res * s->frame_size;
492
493 return 0;
494 }
495
496 static const AVOption options[] = {
497 { "sample_rate", "", offsetof(AlsaData, sample_rate), AV_OPT_TYPE_INT, {.i64 = 48000}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
498 { "channels", "", offsetof(AlsaData, channels), AV_OPT_TYPE_INT, {.i64 = 2}, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
499 { NULL },
500 };
501
502 static const AVClass alsa_demuxer_class = {
503 .class_name = "ALSA demuxer",
504 .item_name = av_default_item_name,
505 .option = options,
506 .version = LIBAVUTIL_VERSION_INT,
507 };
508
509 AVInputFormat ff_alsa_demuxer = {
510 .name = "alsa",
511 .long_name = NULL_IF_CONFIG_SMALL("ALSA audio input"),
512 .priv_data_size = sizeof(AlsaData),
513 .read_header = audio_read_header,
514 .read_packet = audio_read_packet,
515 .read_close = alsa_close,
516 .flags = AVFMT_NOFILE,
517 .priv_class = &alsa_demuxer_class,
518 };