FFmpeg
alsa.c
Go to the documentation of this file.
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"
35 
36 #include "alsa.h"
37 
38 static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
39 {
40  switch(codec_id) {
41  case AV_CODEC_ID_PCM_F64LE: return SND_PCM_FORMAT_FLOAT64_LE;
42  case AV_CODEC_ID_PCM_F64BE: return SND_PCM_FORMAT_FLOAT64_BE;
43  case AV_CODEC_ID_PCM_F32LE: return SND_PCM_FORMAT_FLOAT_LE;
44  case AV_CODEC_ID_PCM_F32BE: return SND_PCM_FORMAT_FLOAT_BE;
45  case AV_CODEC_ID_PCM_S32LE: return SND_PCM_FORMAT_S32_LE;
46  case AV_CODEC_ID_PCM_S32BE: return SND_PCM_FORMAT_S32_BE;
47  case AV_CODEC_ID_PCM_U32LE: return SND_PCM_FORMAT_U32_LE;
48  case AV_CODEC_ID_PCM_U32BE: return SND_PCM_FORMAT_U32_BE;
49  case AV_CODEC_ID_PCM_S24LE: return SND_PCM_FORMAT_S24_3LE;
50  case AV_CODEC_ID_PCM_S24BE: return SND_PCM_FORMAT_S24_3BE;
51  case AV_CODEC_ID_PCM_U24LE: return SND_PCM_FORMAT_U24_3LE;
52  case AV_CODEC_ID_PCM_U24BE: return SND_PCM_FORMAT_U24_3BE;
53  case AV_CODEC_ID_PCM_S16LE: return SND_PCM_FORMAT_S16_LE;
54  case AV_CODEC_ID_PCM_S16BE: return SND_PCM_FORMAT_S16_BE;
55  case AV_CODEC_ID_PCM_U16LE: return SND_PCM_FORMAT_U16_LE;
56  case AV_CODEC_ID_PCM_U16BE: return SND_PCM_FORMAT_U16_BE;
57  case AV_CODEC_ID_PCM_S8: return SND_PCM_FORMAT_S8;
58  case AV_CODEC_ID_PCM_U8: return SND_PCM_FORMAT_U8;
59  case AV_CODEC_ID_PCM_MULAW: return SND_PCM_FORMAT_MU_LAW;
60  case AV_CODEC_ID_PCM_ALAW: return SND_PCM_FORMAT_A_LAW;
61  default: return SND_PCM_FORMAT_UNKNOWN;
62  }
63 }
64 
65 #define MAKE_REORDER_FUNC(NAME, TYPE, CHANNELS, LAYOUT, MAP) \
66 static void alsa_reorder_ ## NAME ## _ ## LAYOUT(const void *in_v, \
67  void *out_v, \
68  int n) \
69 { \
70  const TYPE *in = in_v; \
71  TYPE *out = out_v; \
72  \
73  while (n-- > 0) { \
74  MAP \
75  in += CHANNELS; \
76  out += CHANNELS; \
77  } \
78 }
79 
80 #define MAKE_REORDER_FUNCS(CHANNELS, LAYOUT, MAP) \
81  MAKE_REORDER_FUNC(int8, int8_t, CHANNELS, LAYOUT, MAP) \
82  MAKE_REORDER_FUNC(int16, int16_t, CHANNELS, LAYOUT, MAP) \
83  MAKE_REORDER_FUNC(int32, int32_t, CHANNELS, LAYOUT, MAP) \
84  MAKE_REORDER_FUNC(f32, float, CHANNELS, LAYOUT, MAP)
85 
86 MAKE_REORDER_FUNCS(5, out_50, \
87  out[0] = in[0]; \
88  out[1] = in[1]; \
89  out[2] = in[3]; \
90  out[3] = in[4]; \
91  out[4] = in[2]; \
92  )
93 
94 MAKE_REORDER_FUNCS(6, out_51, \
95  out[0] = in[0]; \
96  out[1] = in[1]; \
97  out[2] = in[4]; \
98  out[3] = in[5]; \
99  out[4] = in[2]; \
100  out[5] = in[3]; \
101  )
102 
103 MAKE_REORDER_FUNCS(8, out_71, \
104  out[0] = in[0]; \
105  out[1] = in[1]; \
106  out[2] = in[4]; \
107  out[3] = in[5]; \
108  out[4] = in[2]; \
109  out[5] = in[3]; \
110  out[6] = in[6]; \
111  out[7] = in[7]; \
112  )
113 
114 #define FORMAT_I8 0
115 #define FORMAT_I16 1
116 #define FORMAT_I32 2
117 #define FORMAT_F32 3
118 
119 #define PICK_REORDER(layout)\
120 switch(format) {\
121  case FORMAT_I8: s->reorder_func = alsa_reorder_int8_out_ ##layout; break;\
122  case FORMAT_I16: s->reorder_func = alsa_reorder_int16_out_ ##layout; break;\
123  case FORMAT_I32: s->reorder_func = alsa_reorder_int32_out_ ##layout; break;\
124  case FORMAT_F32: s->reorder_func = alsa_reorder_f32_out_ ##layout; break;\
125 }
126 
127 static av_cold int find_reorder_func(AlsaData *s, int codec_id, uint64_t layout, int out)
128 {
129  int format;
130 
131  /* reordering input is not currently supported */
132  if (!out)
133  return AVERROR(ENOSYS);
134 
135  /* reordering is not needed for QUAD or 2_2 layout */
136  if (layout == AV_CH_LAYOUT_QUAD || layout == AV_CH_LAYOUT_2_2)
137  return 0;
138 
139  switch (codec_id) {
140  case AV_CODEC_ID_PCM_S8:
141  case AV_CODEC_ID_PCM_U8:
143  case AV_CODEC_ID_PCM_MULAW: format = FORMAT_I8; break;
147  case AV_CODEC_ID_PCM_U16BE: format = FORMAT_I16; break;
151  case AV_CODEC_ID_PCM_U32BE: format = FORMAT_I32; break;
153  case AV_CODEC_ID_PCM_F32BE: format = FORMAT_F32; break;
154  default: return AVERROR(ENOSYS);
155  }
156 
157  if (layout == AV_CH_LAYOUT_5POINT0_BACK || layout == AV_CH_LAYOUT_5POINT0)
158  PICK_REORDER(50)
159  else if (layout == AV_CH_LAYOUT_5POINT1_BACK || layout == AV_CH_LAYOUT_5POINT1)
160  PICK_REORDER(51)
161  else if (layout == AV_CH_LAYOUT_7POINT1)
162  PICK_REORDER(71)
163 
164  return s->reorder_func ? 0 : AVERROR(ENOSYS);
165 }
166 
167 av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode,
168  unsigned int *sample_rate,
169  int channels, enum AVCodecID *codec_id)
170 {
171  AlsaData *s = ctx->priv_data;
172  const char *audio_device;
173  int res, flags = 0;
174  snd_pcm_format_t format;
175  snd_pcm_t *h;
176  snd_pcm_hw_params_t *hw_params;
177  snd_pcm_uframes_t buffer_size, period_size;
178  uint64_t layout = ctx->streams[0]->codecpar->channel_layout;
179 
180  if (ctx->url[0] == 0) audio_device = "default";
181  else audio_device = ctx->url;
182 
183  if (*codec_id == AV_CODEC_ID_NONE)
184  *codec_id = DEFAULT_CODEC_ID;
185  format = codec_id_to_pcm_format(*codec_id);
186  if (format == SND_PCM_FORMAT_UNKNOWN) {
187  av_log(ctx, AV_LOG_ERROR, "sample format 0x%04x is not supported\n", *codec_id);
188  return AVERROR(ENOSYS);
189  }
190  s->frame_size = av_get_bits_per_sample(*codec_id) / 8 * channels;
191 
192  if (ctx->flags & AVFMT_FLAG_NONBLOCK) {
193  flags = SND_PCM_NONBLOCK;
194  }
195  res = snd_pcm_open(&h, audio_device, mode, flags);
196  if (res < 0) {
197  av_log(ctx, AV_LOG_ERROR, "cannot open audio device %s (%s)\n",
198  audio_device, snd_strerror(res));
199  return AVERROR(EIO);
200  }
201 
202  res = snd_pcm_hw_params_malloc(&hw_params);
203  if (res < 0) {
204  av_log(ctx, AV_LOG_ERROR, "cannot allocate hardware parameter structure (%s)\n",
205  snd_strerror(res));
206  goto fail1;
207  }
208 
209  res = snd_pcm_hw_params_any(h, hw_params);
210  if (res < 0) {
211  av_log(ctx, AV_LOG_ERROR, "cannot initialize hardware parameter structure (%s)\n",
212  snd_strerror(res));
213  goto fail;
214  }
215 
216  res = snd_pcm_hw_params_set_access(h, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
217  if (res < 0) {
218  av_log(ctx, AV_LOG_ERROR, "cannot set access type (%s)\n",
219  snd_strerror(res));
220  goto fail;
221  }
222 
223  res = snd_pcm_hw_params_set_format(h, hw_params, format);
224  if (res < 0) {
225  av_log(ctx, AV_LOG_ERROR, "cannot set sample format 0x%04x %d (%s)\n",
226  *codec_id, format, snd_strerror(res));
227  goto fail;
228  }
229 
230  res = snd_pcm_hw_params_set_rate_near(h, hw_params, sample_rate, 0);
231  if (res < 0) {
232  av_log(ctx, AV_LOG_ERROR, "cannot set sample rate (%s)\n",
233  snd_strerror(res));
234  goto fail;
235  }
236 
237  res = snd_pcm_hw_params_set_channels(h, hw_params, channels);
238  if (res < 0) {
239  av_log(ctx, AV_LOG_ERROR, "cannot set channel count to %d (%s)\n",
240  channels, snd_strerror(res));
241  goto fail;
242  }
243 
244  snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size);
245  buffer_size = FFMIN(buffer_size, ALSA_BUFFER_SIZE_MAX);
246  /* TODO: maybe use ctx->max_picture_buffer somehow */
247  res = snd_pcm_hw_params_set_buffer_size_near(h, hw_params, &buffer_size);
248  if (res < 0) {
249  av_log(ctx, AV_LOG_ERROR, "cannot set ALSA buffer size (%s)\n",
250  snd_strerror(res));
251  goto fail;
252  }
253 
254  snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, NULL);
255  if (!period_size)
256  period_size = buffer_size / 4;
257  res = snd_pcm_hw_params_set_period_size_near(h, hw_params, &period_size, NULL);
258  if (res < 0) {
259  av_log(ctx, AV_LOG_ERROR, "cannot set ALSA period size (%s)\n",
260  snd_strerror(res));
261  goto fail;
262  }
263  s->period_size = period_size;
264 
265  res = snd_pcm_hw_params(h, hw_params);
266  if (res < 0) {
267  av_log(ctx, AV_LOG_ERROR, "cannot set parameters (%s)\n",
268  snd_strerror(res));
269  goto fail;
270  }
271 
272  snd_pcm_hw_params_free(hw_params);
273 
274  if (channels > 2 && layout) {
275  if (find_reorder_func(s, *codec_id, layout, mode == SND_PCM_STREAM_PLAYBACK) < 0) {
276  char name[128];
277  av_get_channel_layout_string(name, sizeof(name), channels, layout);
278  av_log(ctx, AV_LOG_WARNING, "ALSA channel layout unknown or unimplemented for %s %s.\n",
279  name, mode == SND_PCM_STREAM_PLAYBACK ? "playback" : "capture");
280  }
281  if (s->reorder_func) {
282  s->reorder_buf_size = buffer_size;
284  if (!s->reorder_buf)
285  goto fail1;
286  }
287  }
288 
289  s->h = h;
290  return 0;
291 
292 fail:
293  snd_pcm_hw_params_free(hw_params);
294 fail1:
295  snd_pcm_close(h);
296  return AVERROR(EIO);
297 }
298 
300 {
301  AlsaData *s = s1->priv_data;
302 
303  if (snd_pcm_stream(s->h) == SND_PCM_STREAM_PLAYBACK) {
304  snd_pcm_nonblock(s->h, 0);
305  snd_pcm_drain(s->h);
306  }
307  av_freep(&s->reorder_buf);
308  if (CONFIG_ALSA_INDEV)
310  snd_pcm_close(s->h);
311  return 0;
312 }
313 
315 {
316  AlsaData *s = s1->priv_data;
317  snd_pcm_t *handle = s->h;
318 
319  av_log(s1, AV_LOG_WARNING, "ALSA buffer xrun.\n");
320  if (err == -EPIPE) {
321  err = snd_pcm_prepare(handle);
322  if (err < 0) {
323  av_log(s1, AV_LOG_ERROR, "cannot recover from underrun (snd_pcm_prepare failed: %s)\n", snd_strerror(err));
324 
325  return AVERROR(EIO);
326  }
327  } else if (err == -ESTRPIPE) {
328  av_log(s1, AV_LOG_ERROR, "-ESTRPIPE... Unsupported!\n");
329 
330  return -1;
331  }
332  return err;
333 }
334 
336 {
337  int size = s->reorder_buf_size;
338  void *r;
339 
340  av_assert0(size != 0);
341  while (size < min_size)
342  size *= 2;
343  r = av_realloc_array(s->reorder_buf, size, s->frame_size);
344  if (!r)
345  return AVERROR(ENOMEM);
346  s->reorder_buf = r;
347  s->reorder_buf_size = size;
348  return 0;
349 }
350 
351 /* ported from alsa-utils/aplay.c */
352 int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type)
353 {
354  int ret = 0;
355  void **hints, **n;
356  char *name = NULL, *descr = NULL, *io = NULL, *tmp;
357  AVDeviceInfo *new_device = NULL;
358  const char *filter = stream_type == SND_PCM_STREAM_PLAYBACK ? "Output" : "Input";
359 
360  if (snd_device_name_hint(-1, "pcm", &hints) < 0)
361  return AVERROR_EXTERNAL;
362  n = hints;
363  while (*n && !ret) {
364  name = snd_device_name_get_hint(*n, "NAME");
365  descr = snd_device_name_get_hint(*n, "DESC");
366  io = snd_device_name_get_hint(*n, "IOID");
367  if (!io || !strcmp(io, filter)) {
368  new_device = av_mallocz(sizeof(AVDeviceInfo));
369  if (!new_device) {
370  ret = AVERROR(ENOMEM);
371  goto fail;
372  }
373  new_device->device_name = av_strdup(name);
374  if ((tmp = strrchr(descr, '\n')) && tmp[1])
375  new_device->device_description = av_strdup(&tmp[1]);
376  else
377  new_device->device_description = av_strdup(descr);
378  if (!new_device->device_description || !new_device->device_name) {
379  ret = AVERROR(ENOMEM);
380  goto fail;
381  }
382  if ((ret = av_dynarray_add_nofree(&device_list->devices,
383  &device_list->nb_devices, new_device)) < 0) {
384  goto fail;
385  }
386  if (!strcmp(new_device->device_name, "default"))
387  device_list->default_device = device_list->nb_devices - 1;
388  new_device = NULL;
389  }
390  fail:
391  free(io);
392  free(name);
393  free(descr);
394  n++;
395  }
396  if (new_device) {
397  av_free(new_device->device_description);
398  av_free(new_device->device_name);
399  av_free(new_device);
400  }
401  snd_device_name_free_hint(hints);
402  return ret;
403 }
#define NULL
Definition: coverity.c:32
Structure describes basic parameters of the device.
Definition: avdevice.h:452
#define AV_CH_LAYOUT_7POINT1
void ff_timefilter_destroy(TimeFilter *self)
Free all resources associated with the filter.
Definition: timefilter.c:62
char * device_description
human friendly name
Definition: avdevice.h:454
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
channels
Definition: aptx.c:30
enum AVCodecID codec_id
Definition: qsv.c:77
char * device_name
device name, format depends on device
Definition: avdevice.h:453
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
#define AV_CH_LAYOUT_5POINT0
ALSA input and output: definitions and structures.
Format I/O context.
Definition: avformat.h:1358
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define AVFMT_FLAG_NONBLOCK
Do not block when reading packets from input.
Definition: avformat.h:1492
#define av_cold
Definition: attributes.h:82
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1426
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1489
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:294
ptrdiff_t size
Definition: opengl_enc.c:100
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:198
int ff_alsa_extend_reorder_buf(AlsaData *s, int min_size)
Definition: alsa.c:335
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4062
#define av_log(a,...)
#define AV_CH_LAYOUT_5POINT1
Main libavdevice API header.
#define FORMAT_F32
static av_cold snd_pcm_format_t codec_id_to_pcm_format(int codec_id)
Definition: alsa.c:38
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1517
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce then the filter should push the output frames on the output link immediately As an exception to the previous rule if the input frame is enough to produce several output frames then the filter needs output only at least one per link The additional frames can be left buffered in the filter
#define DEFAULT_CODEC_ID
Definition: alsa.h:42
Definition: alsa.h:48
char * url
input or output URL.
Definition: avformat.h:1454
const char * r
Definition: vf_curves.c:114
simple assert() macros that are a bit more flexible than ISO C assert().
#define AV_CH_LAYOUT_QUAD
AVDeviceInfo ** devices
list of autodetected devices
Definition: avdevice.h:461
#define FORMAT_I16
#define MAKE_REORDER_FUNCS(CHANNELS, LAYOUT, MAP)
Definition: alsa.c:80
#define ALSA_BUFFER_SIZE_MAX
Definition: alsa.h:46
#define fail()
Definition: checkasm.h:121
#define AV_CH_LAYOUT_2_2
void(* reorder_func)(const void *, void *, int)
Definition: alsa.h:57
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:96
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:760
#define AV_CH_LAYOUT_5POINT1_BACK
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
sample_rate
void * reorder_buf
Definition: alsa.h:58
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:251
int ff_alsa_xrun_recover(AVFormatContext *s1, int err)
Try to recover from ALSA buffer underrun.
Definition: alsa.c:314
av_cold int ff_alsa_close(AVFormatContext *s1)
Close the ALSA PCM.
Definition: alsa.c:299
TimeFilter * timefilter
Definition: alsa.h:56
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
int ff_alsa_get_device_list(AVDeviceInfoList *device_list, snd_pcm_stream_t stream_type)
Definition: alsa.c:352
#define AV_CH_LAYOUT_5POINT0_BACK
#define s1
Definition: regdef.h:38
int default_device
index of default device or -1 if no default
Definition: avdevice.h:463
List of devices.
Definition: avdevice.h:460
#define PICK_REORDER(layout)
#define flags(name, subs,...)
Definition: cbs_av1.c:561
int period_size
preferred size for reads and writes, in frames
Definition: alsa.h:52
#define FORMAT_I32
#define FORMAT_I8
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1386
snd_pcm_t * h
Definition: alsa.h:50
FILE * out
Definition: movenc.c:54
#define av_freep(p)
int frame_size
bytes per sample * channels
Definition: alsa.h:51
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1028
#define av_malloc_array(a, b)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
int nb_devices
number of autodetected devices
Definition: avdevice.h:462
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
int reorder_buf_size
in frames
Definition: alsa.h:59
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel layout
const char * name
Definition: opengl_enc.c:102
av_cold int ff_alsa_open(AVFormatContext *ctx, snd_pcm_stream_t mode, unsigned int *sample_rate, int channels, enum AVCodecID *codec_id)
Open an ALSA PCM.
Definition: alsa.c:167
static uint8_t tmp[11]
Definition: aes_ctr.c:26