Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ 5778466e

History | View | Annotate | Download (9.24 KB)

1 35fd8122 Nicolas George
/*
2
 * ALSA input and output
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 FFmpeg.
7
 *
8
 * FFmpeg 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
 * FFmpeg 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 FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24 ba87f080 Diego Biurrun
 * @file
25 35fd8122 Nicolas George
 * ALSA input and output: common code
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 419bddd3 Matthieu Castet
#include "libavformat/avformat.h"
33 35fd8122 Nicolas George
34
#include "alsa-audio.h"
35
36
static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
37
{
38
    switch(codec_id) {
39 2ed05f65 Carl Eugen Hoyos
        case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
40
        case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
41 4d1d6f7a Carl Eugen Hoyos
        case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
42
        case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
43 5778466e Carl Eugen Hoyos
        case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
44
        case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
45 35fd8122 Nicolas George
        case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
46
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
47
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
48
        default:                 return SND_PCM_FORMAT_UNKNOWN;
49
    }
50
}
51
52 efec2d7b Carl Eugen Hoyos
#define REORDER_OUT_51(NAME, TYPE) \
53
static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
54
{ \
55
    const TYPE *in = in_v; \
56
    TYPE * out = out_v; \
57
\
58
    while (n-- > 0) { \
59
        out[0] = in[0]; \
60
        out[1] = in[1]; \
61
        out[2] = in[4]; \
62
        out[3] = in[5]; \
63
        out[4] = in[2]; \
64
        out[5] = in[3]; \
65
        in  += 6; \
66
        out += 6; \
67
    } \
68 813dbb44 Nicolas George
}
69
70 efec2d7b Carl Eugen Hoyos
#define REORDER_OUT_71(NAME, TYPE) \
71
static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
72
{ \
73
    const TYPE *in = in_v; \
74
    TYPE * out = out_v; \
75
\
76
    while (n-- > 0) { \
77
        out[0] = in[0]; \
78
        out[1] = in[1]; \
79
        out[2] = in[4]; \
80
        out[3] = in[5]; \
81
        out[4] = in[2]; \
82
        out[5] = in[3]; \
83
        out[6] = in[6]; \
84
        out[7] = in[7]; \
85
        in  += 8; \
86
        out += 8; \
87
    } \
88 813dbb44 Nicolas George
}
89
90 efec2d7b Carl Eugen Hoyos
REORDER_OUT_51(s16, int16_t)
91
REORDER_OUT_71(s16, int16_t)
92 4d1d6f7a Carl Eugen Hoyos
REORDER_OUT_51(s32, int32_t)
93
REORDER_OUT_71(s32, int32_t)
94 efec2d7b Carl Eugen Hoyos
95 813dbb44 Nicolas George
#define REORDER_DUMMY ((void *)1)
96
97
static av_cold ff_reorder_func find_reorder_func(int codec_id,
98
                                                 int64_t layout,
99
                                                 int out)
100
{
101
    return
102
    codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
103
        layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
104 3efaa24f Carl Eugen Hoyos
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
105 813dbb44 Nicolas George
            out ? alsa_reorder_s16_out_51 : NULL :
106
        layout == AV_CH_LAYOUT_7POINT1 ?
107
            out ? alsa_reorder_s16_out_71 : NULL :
108
            NULL :
109 4d1d6f7a Carl Eugen Hoyos
    codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE ?
110
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
111
            out ? alsa_reorder_s32_out_51 : NULL :
112
        layout == AV_CH_LAYOUT_7POINT1 ?
113
            out ? alsa_reorder_s32_out_71 : NULL :
114
           NULL :
115 813dbb44 Nicolas George
        NULL;
116
}
117
118 899f38df Carl Eugen Hoyos
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
119 35fd8122 Nicolas George
                         unsigned int *sample_rate,
120 899f38df Carl Eugen Hoyos
                         int channels, enum CodecID *codec_id)
121 35fd8122 Nicolas George
{
122
    AlsaData *s = ctx->priv_data;
123
    const char *audio_device;
124
    int res, flags = 0;
125
    snd_pcm_format_t format;
126
    snd_pcm_t *h;
127
    snd_pcm_hw_params_t *hw_params;
128
    snd_pcm_uframes_t buffer_size, period_size;
129 813dbb44 Nicolas George
    int64_t layout = ctx->streams[0]->codec->channel_layout;
130 35fd8122 Nicolas George
131
    if (ctx->filename[0] == 0) audio_device = "default";
132
    else                       audio_device = ctx->filename;
133
134
    if (*codec_id == CODEC_ID_NONE)
135
        *codec_id = DEFAULT_CODEC_ID;
136
    format = codec_id_to_pcm_format(*codec_id);
137
    if (format == SND_PCM_FORMAT_UNKNOWN) {
138
        av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
139
        return AVERROR(ENOSYS);
140
    }
141
    s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
142
143
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
144 3cac2f13 Luca Abeni
        flags = SND_PCM_NONBLOCK;
145 35fd8122 Nicolas George
    }
146
    res = snd_pcm_open(&h, audio_device, mode, flags);
147
    if (res < 0) {
148
        av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
149
               audio_device, snd_strerror(res));
150 5ae092ee Stefano Sabatini
        return AVERROR(EIO);
151 35fd8122 Nicolas George
    }
152
153
    res = snd_pcm_hw_params_malloc(&hw_params);
154
    if (res < 0) {
155
        av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
156
               snd_strerror(res));
157
        goto fail1;
158
    }
159
160
    res = snd_pcm_hw_params_any(h, hw_params);
161
    if (res < 0) {
162
        av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
163
               snd_strerror(res));
164
        goto fail;
165
    }
166
167
    res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
168
    if (res < 0) {
169
        av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
170
               snd_strerror(res));
171
        goto fail;
172
    }
173
174
    res = snd_pcm_hw_params_set_format(h, hw_params, format);
175
    if (res < 0) {
176
        av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
177
               *codec_id, format, snd_strerror(res));
178
        goto fail;
179
    }
180
181
    res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
182
    if (res < 0) {
183
        av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
184
               snd_strerror(res));
185
        goto fail;
186
    }
187
188
    res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
189
    if (res < 0) {
190
        av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
191
               channels, snd_strerror(res));
192
        goto fail;
193
    }
194
195
    snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
196
    /* TODO: maybe use ctx->max_picture_buffer somehow */
197
    res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
198
    if (res < 0) {
199
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
200
               snd_strerror(res));
201
        goto fail;
202
    }
203
204
    snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
205
    res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
206
    if (res < 0) {
207
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
208
               snd_strerror(res));
209
        goto fail;
210
    }
211
    s->period_size = period_size;
212
213
    res = snd_pcm_hw_params(h, hw_params);
214
    if (res < 0) {
215
        av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
216
               snd_strerror(res));
217
        goto fail;
218
    }
219
220
    snd_pcm_hw_params_free(hw_params);
221
222 813dbb44 Nicolas George
    if (channels > 2 && layout) {
223
        s->reorder_func = find_reorder_func(*codec_id, layout,
224
                                            mode == SND_PCM_STREAM_PLAYBACK);
225
        if (s->reorder_func == REORDER_DUMMY) {
226
            s->reorder_func = NULL;
227
        } else if (s->reorder_func) {
228
            s->reorder_buf_size = buffer_size;
229
            s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
230
            if (!s->reorder_buf)
231
                goto fail1;
232
        } else {
233
            char name[16];
234
            av_get_channel_layout_string(name, sizeof(name), channels, layout);
235
            av_log(ctx, AV_LOG_WARNING,
236
                   "ALSA channel layout unknown or unimplemented for %s %s.\n",
237
                   name,
238
                   mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
239
        }
240
    }
241
242 35fd8122 Nicolas George
    s->h = h;
243
    return 0;
244
245
fail:
246
    snd_pcm_hw_params_free(hw_params);
247
fail1:
248
    snd_pcm_close(h);
249 5ae092ee Stefano Sabatini
    return AVERROR(EIO);
250 35fd8122 Nicolas George
}
251
252
av_cold int ff_alsa_close(AVFormatContext *s1)
253
{
254
    AlsaData *s = s1->priv_data;
255
256 1e96d4c7 Nicolas George
    av_freep(&s->reorder_buf);
257 35fd8122 Nicolas George
    snd_pcm_close(s->h);
258
    return 0;
259
}
260
261
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
262
{
263
    AlsaData *s = s1->priv_data;
264
    snd_pcm_t *handle = s->h;
265
266
    av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
267
    if (err == -EPIPE) {
268
        err = snd_pcm_prepare(handle);
269
        if (err < 0) {
270
            av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
271
272 5ae092ee Stefano Sabatini
            return AVERROR(EIO);
273 35fd8122 Nicolas George
        }
274
    } else if (err == -ESTRPIPE) {
275
        av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
276
277
        return -1;
278
    }
279
    return err;
280
}
281 813dbb44 Nicolas George
282
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
283
{
284
    int size = s->reorder_buf_size;
285
    void *r;
286
287
    while (size < min_size)
288
        size *= 2;
289
    r = av_realloc(s->reorder_buf, size * s->frame_size);
290
    if (!r)
291
        return AVERROR(ENOMEM);
292
    s->reorder_buf = r;
293
    s->reorder_buf_size = size;
294
    return 0;
295
}