Statistics
| Branch: | Revision:

ffmpeg / libavdevice / alsa-audio-common.c @ 075a5560

History | View | Annotate | Download (10.2 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
        case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
51
        case CODEC_ID_PCM_ALAW:  return SND_PCM_FORMAT_A_LAW;
52
        default:                 return SND_PCM_FORMAT_UNKNOWN;
53
    }
54
}
55

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

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

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

    
111
REORDER_OUT_50(s16, int16_t)
112
REORDER_OUT_51(s16, int16_t)
113
REORDER_OUT_71(s16, int16_t)
114
REORDER_OUT_50(s32, int32_t)
115
REORDER_OUT_51(s32, int32_t)
116
REORDER_OUT_71(s32, int32_t)
117

    
118
#define REORDER_DUMMY ((void *)1)
119

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
247
    snd_pcm_hw_params_free(hw_params);
248

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

    
269
    s->h = h;
270
    return 0;
271

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

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

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

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

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

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

    
304
        return -1;
305
    }
306
    return err;
307
}
308

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

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