OSDN Git Service

Merge remote-tracking branch 'qatar/master'
[coroid/ffmpeg_saccubus.git] / libavdevice / alsa-audio-common.c
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 "avdevice.h"
33 #include "libavutil/avassert.h"
34
35 #include "alsa-audio.h"
36
37 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
38 {
39     switch(codec_id) {
40         case CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
41         case CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
42         case CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
43         case CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
44         case CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
45         case CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
46         case CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
47         case CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
48         case CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
49         case CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
50         case CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
51         case CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
52         case CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
53         case CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
54         case CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
55         case CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
56         case CODEC_ID_PCM_S8:    return SND_PCM_FORMAT_S8;
57         case CODEC_ID_PCM_U8:    return SND_PCM_FORMAT_U8;
58         case CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
59         case CODEC_ID_PCM_ALAW:  return SND_PCM_FORMAT_A_LAW;
60         default:                 return SND_PCM_FORMAT_UNKNOWN;
61     }
62 }
63
64 #define REORDER_OUT_50(NAME, TYPE) \
65 static void alsa_reorder_ ## NAME ## _out_50(const void *in_v, void *out_v, int n) \
66 { \
67     const TYPE *in = in_v; \
68     TYPE      *out = out_v; \
69 \
70     while (n-- > 0) { \
71         out[0] = in[0]; \
72         out[1] = in[1]; \
73         out[2] = in[3]; \
74         out[3] = in[4]; \
75         out[4] = in[2]; \
76         in  += 5; \
77         out += 5; \
78     } \
79 }
80
81 #define REORDER_OUT_51(NAME, TYPE) \
82 static void alsa_reorder_ ## NAME ## _out_51(const void *in_v, void *out_v, int n) \
83 { \
84     const TYPE *in = in_v; \
85     TYPE      *out = out_v; \
86 \
87     while (n-- > 0) { \
88         out[0] = in[0]; \
89         out[1] = in[1]; \
90         out[2] = in[4]; \
91         out[3] = in[5]; \
92         out[4] = in[2]; \
93         out[5] = in[3]; \
94         in  += 6; \
95         out += 6; \
96     } \
97 }
98
99 #define REORDER_OUT_71(NAME, TYPE) \
100 static void alsa_reorder_ ## NAME ## _out_71(const void *in_v, void *out_v, int n) \
101 { \
102     const TYPE *in = in_v; \
103     TYPE      *out = out_v; \
104 \
105     while (n-- > 0) { \
106         out[0] = in[0]; \
107         out[1] = in[1]; \
108         out[2] = in[4]; \
109         out[3] = in[5]; \
110         out[4] = in[2]; \
111         out[5] = in[3]; \
112         out[6] = in[6]; \
113         out[7] = in[7]; \
114         in  += 8; \
115         out += 8; \
116     } \
117 }
118
119 REORDER_OUT_50(int8, int8_t)
120 REORDER_OUT_51(int8, int8_t)
121 REORDER_OUT_71(int8, int8_t)
122 REORDER_OUT_50(int16, int16_t)
123 REORDER_OUT_51(int16, int16_t)
124 REORDER_OUT_71(int16, int16_t)
125 REORDER_OUT_50(int32, int32_t)
126 REORDER_OUT_51(int32, int32_t)
127 REORDER_OUT_71(int32, int32_t)
128 REORDER_OUT_50(f32, float)
129 REORDER_OUT_51(f32, float)
130 REORDER_OUT_71(f32, float)
131
132 #define FORMAT_I8  0
133 #define FORMAT_I16 1
134 #define FORMAT_I32 2
135 #define FORMAT_F32 3
136
137 #define PICK_REORDER(layout)\
138 switch(format) {\
139     case FORMAT_I8:  s->reorder_func = alsa_reorder_int8_out_ ##layout;  break;\
140     case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
141     case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
142     case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout;   break;\
143 }
144
145 static av_cold int find_reorder_func(AlsaData *s, int codec_id, int64_t layout, int out)
146 {
147     int format;
148
149     /* reordering input is not currently supported */
150     if (!out)
151         return AVERROR(ENOSYS);
152
153     /* reordering is not needed for QUAD or 2_2 layout */
154     if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
155         return 0;
156
157     switch (codec_id) {
158     case CODEC_ID_PCM_S8:
159     case CODEC_ID_PCM_U8:
160     case CODEC_ID_PCM_ALAW:
161     case CODEC_ID_PCM_MULAW: format = FORMAT_I8;  break;
162     case CODEC_ID_PCM_S16LE:
163     case CODEC_ID_PCM_S16BE:
164     case CODEC_ID_PCM_U16LE:
165     case CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
166     case CODEC_ID_PCM_S32LE:
167     case CODEC_ID_PCM_S32BE:
168     case CODEC_ID_PCM_U32LE:
169     case CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
170     case CODEC_ID_PCM_F32LE:
171     case CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
172     default:                 return AVERROR(ENOSYS);
173     }
174
175     if      (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
176         PICK_REORDER(50)
177     else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
178         PICK_REORDER(51)
179     else if (layout == AV_CH_LAYOUT_7POINT1)
180         PICK_REORDER(71)
181
182     return s->reorder_func ? 0 : AVERROR(ENOSYS);
183 }
184
185 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
186                          unsigned int *sample_rate,
187                          int channels, enum CodecID *codec_id)
188 {
189     AlsaData *s = ctx->priv_data;
190     const char *audio_device;
191     int res, flags = 0;
192     snd_pcm_format_t format;
193     snd_pcm_t *h;
194     snd_pcm_hw_params_t *hw_params;
195     snd_pcm_uframes_t buffer_size, period_size;
196     int64_t layout = ctx->streams[0]->codec->channel_layout;
197
198     if (ctx->filename[0] == 0) audio_device = "default";
199     else                       audio_device = ctx->filename;
200
201     if (*codec_id == CODEC_ID_NONE)
202         *codec_id = DEFAULT_CODEC_ID;
203     format = codec_id_to_pcm_format(*codec_id);
204     if (format == SND_PCM_FORMAT_UNKNOWN) {
205         av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
206         return AVERROR(ENOSYS);
207     }
208     s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
209
210     if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
211         flags = SND_PCM_NONBLOCK;
212     }
213     res = snd_pcm_open(&h, audio_device, mode, flags);
214     if (res < 0) {
215         av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
216                audio_device, snd_strerror(res));
217         return AVERROR(EIO);
218     }
219
220     res = snd_pcm_hw_params_malloc(&hw_params);
221     if (res < 0) {
222         av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
223                snd_strerror(res));
224         goto fail1;
225     }
226
227     res = snd_pcm_hw_params_any(h, hw_params);
228     if (res < 0) {
229         av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
230                snd_strerror(res));
231         goto fail;
232     }
233
234     res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
235     if (res < 0) {
236         av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
237                snd_strerror(res));
238         goto fail;
239     }
240
241     res = snd_pcm_hw_params_set_format(h, hw_params, format);
242     if (res < 0) {
243         av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
244                *codec_id, format, snd_strerror(res));
245         goto fail;
246     }
247
248     res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
249     if (res < 0) {
250         av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
251                snd_strerror(res));
252         goto fail;
253     }
254
255     res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
256     if (res < 0) {
257         av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
258                channels, snd_strerror(res));
259         goto fail;
260     }
261
262     snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
263     buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
264     /* TODO: maybe use ctx->max_picture_buffer somehow */
265     res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
266     if (res < 0) {
267         av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
268                snd_strerror(res));
269         goto fail;
270     }
271
272     snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
273     if (!period_size)
274         period_size = buffer_size / 4;
275     res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
276     if (res < 0) {
277         av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
278                snd_strerror(res));
279         goto fail;
280     }
281     s->period_size = period_size;
282
283     res = snd_pcm_hw_params(h, hw_params);
284     if (res < 0) {
285         av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
286                snd_strerror(res));
287         goto fail;
288     }
289
290     snd_pcm_hw_params_free(hw_params);
291
292     if (channels > 2 && layout) {
293         if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
294             char name[128];
295             av_get_channel_layout_string(name, sizeof(name), channels, layout);
296             av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
297                    name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
298         }
299         if (s->reorder_func) {
300             s->reorder_buf_size = buffer_size;
301             s->reorder_buf = av_malloc(s->reorder_buf_size * s->frame_size);
302             if (!s->reorder_buf)
303                 goto fail1;
304         }
305     }
306
307     s->h = h;
308     return 0;
309
310 fail:
311     snd_pcm_hw_params_free(hw_params);
312 fail1:
313     snd_pcm_close(h);
314     return AVERROR(EIO);
315 }
316
317 av_cold int ff_alsa_close(AVFormatContext *s1)
318 {
319     AlsaData *s = s1->priv_data;
320
321     av_freep(&s->reorder_buf);
322     ff_timefilter_destroy(s->timefilter);
323     snd_pcm_close(s->h);
324     return 0;
325 }
326
327 int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
328 {
329     AlsaData *s = s1->priv_data;
330     snd_pcm_t *handle = s->h;
331
332     av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
333     if (err == -EPIPE) {
334         err = snd_pcm_prepare(handle);
335         if (err < 0) {
336             av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
337
338             return AVERROR(EIO);
339         }
340     } else if (err == -ESTRPIPE) {
341         av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
342
343         return -1;
344     }
345     return err;
346 }
347
348 int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
349 {
350     int size = s->reorder_buf_size;
351     void *r;
352
353     av_assert0(size != 0);
354     while (size < min_size)
355         size *= 2;
356     r = av_realloc(s->reorder_buf, size * s->frame_size);
357     if (!r)
358         return AVERROR(ENOMEM);
359     s->reorder_buf = r;
360     s->reorder_buf_size = size;
361     return 0;
362 }