Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ 0ae2a036

History | View | Annotate | Download (10.6 KB)

1
/*
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
 * @file
25
 * 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
#include "libavformat/avformat.h"
33

    
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
        case CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
40
        case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
41
        case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
42
        case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
43
        case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
44
        case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
45
        case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
46
        case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
47
        case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
48
        case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
49
        case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
50
        case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
51
        case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
52
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
53
        case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
54
        case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
55
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
56
        case CODEC_ID_PCM_U8:    return SND_PCM_FORMAT_U8;
57
        case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
58
        case CODEC_ID_PCM_ALAW:  return SND_PCM_FORMAT_A_LAW;
59
        default:                 return SND_PCM_FORMAT_UNKNOWN;
60
    }
61
}
62

    
63
#define REORDER_OUT_50(NAME, TYPE) \
64
static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
65
{ \
66
    const TYPE *in = in_v; \
67
    TYPE * out = out_v; \
68
\
69
    while (n-- > 0) { \
70
        out[0] = in[0]; \
71
        out[1] = in[1]; \
72
        out[2] = in[3]; \
73
        out[3] = in[4]; \
74
        out[4] = in[2]; \
75
        in  += 5; \
76
        out += 5; \
77
    } \
78
}
79

    
80
#define REORDER_OUT_51(NAME, TYPE) \
81
static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
82
{ \
83
    const TYPE *in = in_v; \
84
    TYPE * out = out_v; \
85
\
86
    while (n-- > 0) { \
87
        out[0] = in[0]; \
88
        out[1] = in[1]; \
89
        out[2] = in[4]; \
90
        out[3] = in[5]; \
91
        out[4] = in[2]; \
92
        out[5] = in[3]; \
93
        in  += 6; \
94
        out += 6; \
95
    } \
96
}
97

    
98
#define REORDER_OUT_71(NAME, TYPE) \
99
static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
100
{ \
101
    const TYPE *in = in_v; \
102
    TYPE * out = out_v; \
103
\
104
    while (n-- > 0) { \
105
        out[0] = in[0]; \
106
        out[1] = in[1]; \
107
        out[2] = in[4]; \
108
        out[3] = in[5]; \
109
        out[4] = in[2]; \
110
        out[5] = in[3]; \
111
        out[6] = in[6]; \
112
        out[7] = in[7]; \
113
        in  += 8; \
114
        out += 8; \
115
    } \
116
}
117

    
118
REORDER_OUT_50(s16, int16_t)
119
REORDER_OUT_51(s16, int16_t)
120
REORDER_OUT_71(s16, int16_t)
121
REORDER_OUT_50(s32, int32_t)
122
REORDER_OUT_51(s32, int32_t)
123
REORDER_OUT_71(s32, int32_t)
124

    
125
#define REORDER_DUMMY ((void *)1)
126

    
127
static av_cold ff_reorder_func find_reorder_func(int codec_id,
128
                                                 int64_t layout,
129
                                                 int out)
130
{
131
    return
132
    codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
133
        layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
134
            out ? alsa_reorder_s16_out_50 : NULL :
135
        layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
136
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
137
            out ? alsa_reorder_s16_out_51 : NULL :
138
        layout == AV_CH_LAYOUT_7POINT1 ?
139
            out ? alsa_reorder_s16_out_71 : NULL :
140
            NULL :
141
    codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE ?
142
        layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
143
            out ? alsa_reorder_s32_out_50 : NULL :
144
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
145
            out ? alsa_reorder_s32_out_51 : NULL :
146
        layout == AV_CH_LAYOUT_7POINT1 ?
147
            out ? alsa_reorder_s32_out_71 : NULL :
148
           NULL :
149
        NULL;
150
}
151

    
152
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
153
                         unsigned int *sample_rate,
154
                         int channels, enum CodecID *codec_id)
155
{
156
    AlsaData *s = ctx->priv_data;
157
    const char *audio_device;
158
    int res, flags = 0;
159
    snd_pcm_format_t format;
160
    snd_pcm_t *h;
161
    snd_pcm_hw_params_t *hw_params;
162
    snd_pcm_uframes_t buffer_size, period_size;
163
    int64_t layout = ctx->streams[0]->codec->channel_layout;
164

    
165
    if (ctx->filename[0] == 0) audio_device = "default";
166
    else                       audio_device = ctx->filename;
167

    
168
    if (*codec_id == CODEC_ID_NONE)
169
        *codec_id = DEFAULT_CODEC_ID;
170
    format = codec_id_to_pcm_format(*codec_id);
171
    if (format == SND_PCM_FORMAT_UNKNOWN) {
172
        av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
173
        return AVERROR(ENOSYS);
174
    }
175
    s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
176

    
177
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
178
        flags = SND_PCM_NONBLOCK;
179
    }
180
    res = snd_pcm_open(&h, audio_device, mode, flags);
181
    if (res < 0) {
182
        av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
183
               audio_device, snd_strerror(res));
184
        return AVERROR(EIO);
185
    }
186

    
187
    res = snd_pcm_hw_params_malloc(&hw_params);
188
    if (res < 0) {
189
        av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
190
               snd_strerror(res));
191
        goto fail1;
192
    }
193

    
194
    res = snd_pcm_hw_params_any(h, hw_params);
195
    if (res < 0) {
196
        av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
197
               snd_strerror(res));
198
        goto fail;
199
    }
200

    
201
    res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
202
    if (res < 0) {
203
        av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
204
               snd_strerror(res));
205
        goto fail;
206
    }
207

    
208
    res = snd_pcm_hw_params_set_format(h, hw_params, format);
209
    if (res < 0) {
210
        av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
211
               *codec_id, format, snd_strerror(res));
212
        goto fail;
213
    }
214

    
215
    res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
216
    if (res < 0) {
217
        av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
218
               snd_strerror(res));
219
        goto fail;
220
    }
221

    
222
    res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
223
    if (res < 0) {
224
        av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
225
               channels, snd_strerror(res));
226
        goto fail;
227
    }
228

    
229
    snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
230
    /* TODO: maybe use ctx->max_picture_buffer somehow */
231
    res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
232
    if (res < 0) {
233
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
234
               snd_strerror(res));
235
        goto fail;
236
    }
237

    
238
    snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
239
    res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
240
    if (res < 0) {
241
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
242
               snd_strerror(res));
243
        goto fail;
244
    }
245
    s->period_size = period_size;
246

    
247
    res = snd_pcm_hw_params(h, hw_params);
248
    if (res < 0) {
249
        av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
250
               snd_strerror(res));
251
        goto fail;
252
    }
253

    
254
    snd_pcm_hw_params_free(hw_params);
255

    
256
    if (channels > 2 && layout) {
257
        s->reorder_func = find_reorder_func(*codec_id, layout,
258
                                            mode == SND_PCM_STREAM_PLAYBACK);
259
        if (s->reorder_func == REORDER_DUMMY) {
260
            s->reorder_func = NULL;
261
        } else if (s->reorder_func) {
262
            s->reorder_buf_size = buffer_size;
263
            s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
264
            if (!s->reorder_buf)
265
                goto fail1;
266
        } else {
267
            char name[16];
268
            av_get_channel_layout_string(name, sizeof(name), channels, layout);
269
            av_log(ctx, AV_LOG_WARNING,
270
                   "ALSA channel layout unknown or unimplemented for %s %s.\n",
271
                   name,
272
                   mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
273
        }
274
    }
275

    
276
    s->h = h;
277
    return 0;
278

    
279
fail:
280
    snd_pcm_hw_params_free(hw_params);
281
fail1:
282
    snd_pcm_close(h);
283
    return AVERROR(EIO);
284
}
285

    
286
av_cold int ff_alsa_close(AVFormatContext *s1)
287
{
288
    AlsaData *s = s1->priv_data;
289

    
290
    av_freep(&s->reorder_buf);
291
    snd_pcm_close(s->h);
292
    return 0;
293
}
294

    
295
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
296
{
297
    AlsaData *s = s1->priv_data;
298
    snd_pcm_t *handle = s->h;
299

    
300
    av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
301
    if (err == -EPIPE) {
302
        err = snd_pcm_prepare(handle);
303
        if (err < 0) {
304
            av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
305

    
306
            return AVERROR(EIO);
307
        }
308
    } else if (err == -ESTRPIPE) {
309
        av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
310

    
311
        return -1;
312
    }
313
    return err;
314
}
315

    
316
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
317
{
318
    int size = s->reorder_buf_size;
319
    void *r;
320

    
321
    while (size < min_size)
322
        size *= 2;
323
    r = av_realloc(s->reorder_buf, size * s->frame_size);
324
    if (!r)
325
        return AVERROR(ENOMEM);
326
    s->reorder_buf = r;
327
    s->reorder_buf_size = size;
328
    return 0;
329
}