Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ ff2614ee

History | View | Annotate | Download (10.1 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_S24LE: return SND_PCM_FORMAT_S24_3LE;
46
        case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
47
        case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
48
        case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
49
        case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
50
        default:                 return SND_PCM_FORMAT_UNKNOWN;
51
    }
52
}
53

    
54
#define REORDER_OUT_50(NAME, TYPE) \
55
static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
56
{ \
57
    const TYPE *in = in_v; \
58
    TYPE * out = out_v; \
59
\
60
    while (n-- > 0) { \
61
        out[0] = in[0]; \
62
        out[1] = in[1]; \
63
        out[2] = in[3]; \
64
        out[3] = in[4]; \
65
        out[4] = in[2]; \
66
        in  += 5; \
67
        out += 5; \
68
    } \
69
}
70

    
71
#define REORDER_OUT_51(NAME, TYPE) \
72
static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
73
{ \
74
    const TYPE *in = in_v; \
75
    TYPE * out = out_v; \
76
\
77
    while (n-- > 0) { \
78
        out[0] = in[0]; \
79
        out[1] = in[1]; \
80
        out[2] = in[4]; \
81
        out[3] = in[5]; \
82
        out[4] = in[2]; \
83
        out[5] = in[3]; \
84
        in  += 6; \
85
        out += 6; \
86
    } \
87
}
88

    
89
#define REORDER_OUT_71(NAME, TYPE) \
90
static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
91
{ \
92
    const TYPE *in = in_v; \
93
    TYPE * out = out_v; \
94
\
95
    while (n-- > 0) { \
96
        out[0] = in[0]; \
97
        out[1] = in[1]; \
98
        out[2] = in[4]; \
99
        out[3] = in[5]; \
100
        out[4] = in[2]; \
101
        out[5] = in[3]; \
102
        out[6] = in[6]; \
103
        out[7] = in[7]; \
104
        in  += 8; \
105
        out += 8; \
106
    } \
107
}
108

    
109
REORDER_OUT_50(s16, int16_t)
110
REORDER_OUT_51(s16, int16_t)
111
REORDER_OUT_71(s16, int16_t)
112
REORDER_OUT_50(s32, int32_t)
113
REORDER_OUT_51(s32, int32_t)
114
REORDER_OUT_71(s32, int32_t)
115

    
116
#define REORDER_DUMMY ((void *)1)
117

    
118
static av_cold ff_reorder_func find_reorder_func(int codec_id,
119
                                                 int64_t layout,
120
                                                 int out)
121
{
122
    return
123
    codec_id == CODEC_ID_PCM_S16LE || codec_id == CODEC_ID_PCM_S16BE ?
124
        layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
125
            out ? alsa_reorder_s16_out_50 : NULL :
126
        layout == AV_CH_LAYOUT_QUAD ? REORDER_DUMMY :
127
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
128
            out ? alsa_reorder_s16_out_51 : NULL :
129
        layout == AV_CH_LAYOUT_7POINT1 ?
130
            out ? alsa_reorder_s16_out_71 : NULL :
131
            NULL :
132
    codec_id == CODEC_ID_PCM_S32LE || codec_id == CODEC_ID_PCM_S32BE ?
133
        layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0 ?
134
            out ? alsa_reorder_s32_out_50 : NULL :
135
        layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1 ?
136
            out ? alsa_reorder_s32_out_51 : NULL :
137
        layout == AV_CH_LAYOUT_7POINT1 ?
138
            out ? alsa_reorder_s32_out_71 : NULL :
139
           NULL :
140
        NULL;
141
}
142

    
143
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
144
                         unsigned int *sample_rate,
145
                         int channels, enum CodecID *codec_id)
146
{
147
    AlsaData *s = ctx->priv_data;
148
    const char *audio_device;
149
    int res, flags = 0;
150
    snd_pcm_format_t format;
151
    snd_pcm_t *h;
152
    snd_pcm_hw_params_t *hw_params;
153
    snd_pcm_uframes_t buffer_size, period_size;
154
    int64_t layout = ctx->streams[0]->codec->channel_layout;
155

    
156
    if (ctx->filename[0] == 0) audio_device = "default";
157
    else                       audio_device = ctx->filename;
158

    
159
    if (*codec_id == CODEC_ID_NONE)
160
        *codec_id = DEFAULT_CODEC_ID;
161
    format = codec_id_to_pcm_format(*codec_id);
162
    if (format == SND_PCM_FORMAT_UNKNOWN) {
163
        av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
164
        return AVERROR(ENOSYS);
165
    }
166
    s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
167

    
168
    if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
169
        flags = SND_PCM_NONBLOCK;
170
    }
171
    res = snd_pcm_open(&h, audio_device, mode, flags);
172
    if (res < 0) {
173
        av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
174
               audio_device, snd_strerror(res));
175
        return AVERROR(EIO);
176
    }
177

    
178
    res = snd_pcm_hw_params_malloc(&hw_params);
179
    if (res < 0) {
180
        av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
181
               snd_strerror(res));
182
        goto fail1;
183
    }
184

    
185
    res = snd_pcm_hw_params_any(h, hw_params);
186
    if (res < 0) {
187
        av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
188
               snd_strerror(res));
189
        goto fail;
190
    }
191

    
192
    res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
193
    if (res < 0) {
194
        av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
195
               snd_strerror(res));
196
        goto fail;
197
    }
198

    
199
    res = snd_pcm_hw_params_set_format(h, hw_params, format);
200
    if (res < 0) {
201
        av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
202
               *codec_id, format, snd_strerror(res));
203
        goto fail;
204
    }
205

    
206
    res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
207
    if (res < 0) {
208
        av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
209
               snd_strerror(res));
210
        goto fail;
211
    }
212

    
213
    res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
214
    if (res < 0) {
215
        av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
216
               channels, snd_strerror(res));
217
        goto fail;
218
    }
219

    
220
    snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
221
    /* TODO: maybe use ctx->max_picture_buffer somehow */
222
    res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
223
    if (res < 0) {
224
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
225
               snd_strerror(res));
226
        goto fail;
227
    }
228

    
229
    snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
230
    res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
231
    if (res < 0) {
232
        av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
233
               snd_strerror(res));
234
        goto fail;
235
    }
236
    s->period_size = period_size;
237

    
238
    res = snd_pcm_hw_params(h, hw_params);
239
    if (res < 0) {
240
        av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
241
               snd_strerror(res));
242
        goto fail;
243
    }
244

    
245
    snd_pcm_hw_params_free(hw_params);
246

    
247
    if (channels > 2 && layout) {
248
        s->reorder_func = find_reorder_func(*codec_id, layout,
249
                                            mode == SND_PCM_STREAM_PLAYBACK);
250
        if (s->reorder_func == REORDER_DUMMY) {
251
            s->reorder_func = NULL;
252
        } else if (s->reorder_func) {
253
            s->reorder_buf_size = buffer_size;
254
            s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
255
            if (!s->reorder_buf)
256
                goto fail1;
257
        } else {
258
            char name[16];
259
            av_get_channel_layout_string(name, sizeof(name), channels, layout);
260
            av_log(ctx, AV_LOG_WARNING,
261
                   "ALSA channel layout unknown or unimplemented for %s %s.\n",
262
                   name,
263
                   mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
264
        }
265
    }
266

    
267
    s->h = h;
268
    return 0;
269

    
270
fail:
271
    snd_pcm_hw_params_free(hw_params);
272
fail1:
273
    snd_pcm_close(h);
274
    return AVERROR(EIO);
275
}
276

    
277
av_cold int ff_alsa_close(AVFormatContext *s1)
278
{
279
    AlsaData *s = s1->priv_data;
280

    
281
    av_freep(&s->reorder_buf);
282
    snd_pcm_close(s->h);
283
    return 0;
284
}
285

    
286
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
287
{
288
    AlsaData *s = s1->priv_data;
289
    snd_pcm_t *handle = s->h;
290

    
291
    av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
292
    if (err == -EPIPE) {
293
        err = snd_pcm_prepare(handle);
294
        if (err < 0) {
295
            av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
296

    
297
            return AVERROR(EIO);
298
        }
299
    } else if (err == -ESTRPIPE) {
300
        av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
301

    
302
        return -1;
303
    }
304
    return err;
305
}
306

    
307
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
308
{
309
    int size = s->reorder_buf_size;
310
    void *r;
311

    
312
    while (size < min_size)
313
        size *= 2;
314
    r = av_realloc(s->reorder_buf, size * s->frame_size);
315
    if (!r)
316
        return AVERROR(ENOMEM);
317
    s->reorder_buf = r;
318
    s->reorder_buf_size = size;
319
    return 0;
320
}