Coverage Report

Created: 2026-11-30 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/audio/out/ao.c
Line
Count
Source
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <math.h>
22
#include <assert.h>
23
24
#include "mpv_talloc.h"
25
26
#include "config.h"
27
#include "ao.h"
28
#include "internal.h"
29
#include "audio/format.h"
30
31
#include "options/options.h"
32
#include "options/m_config_frontend.h"
33
#include "osdep/endian.h"
34
#include "common/msg.h"
35
#include "common/common.h"
36
#include "common/global.h"
37
38
extern const struct ao_driver audio_out_oss;
39
extern const struct ao_driver audio_out_audiotrack;
40
extern const struct ao_driver audio_out_audiounit;
41
extern const struct ao_driver audio_out_coreaudio;
42
extern const struct ao_driver audio_out_coreaudio_exclusive;
43
extern const struct ao_driver audio_out_avfoundation;
44
extern const struct ao_driver audio_out_rsound;
45
extern const struct ao_driver audio_out_pipewire;
46
extern const struct ao_driver audio_out_sndio;
47
extern const struct ao_driver audio_out_pulse;
48
extern const struct ao_driver audio_out_jack;
49
extern const struct ao_driver audio_out_openal;
50
extern const struct ao_driver audio_out_opensles;
51
extern const struct ao_driver audio_out_null;
52
extern const struct ao_driver audio_out_alsa;
53
extern const struct ao_driver audio_out_wasapi;
54
extern const struct ao_driver audio_out_pcm;
55
extern const struct ao_driver audio_out_lavc;
56
extern const struct ao_driver audio_out_sdl;
57
extern const struct ao_driver audio_out_aaudio;
58
59
static const struct ao_driver * const audio_out_drivers[] = {
60
// native:
61
#if HAVE_AUDIOTRACK
62
    &audio_out_audiotrack,
63
#endif
64
#if HAVE_AAUDIO
65
    &audio_out_aaudio,
66
#endif
67
#if HAVE_AUDIOUNIT
68
    &audio_out_audiounit,
69
#endif
70
#if HAVE_COREAUDIO
71
    &audio_out_coreaudio,
72
#endif
73
#if HAVE_AVFOUNDATION
74
    &audio_out_avfoundation,
75
#endif
76
#if HAVE_PIPEWIRE
77
    &audio_out_pipewire,
78
#endif
79
#if HAVE_PULSE
80
    &audio_out_pulse,
81
#endif
82
#if HAVE_ALSA
83
    &audio_out_alsa,
84
#endif
85
#if HAVE_WASAPI
86
    &audio_out_wasapi,
87
#endif
88
#if HAVE_OSS_AUDIO
89
    &audio_out_oss,
90
#endif
91
    // wrappers:
92
#if HAVE_JACK
93
    &audio_out_jack,
94
#endif
95
#if HAVE_OPENAL
96
    &audio_out_openal,
97
#endif
98
#if HAVE_OPENSLES
99
    &audio_out_opensles,
100
#endif
101
#if HAVE_SDL2_AUDIO
102
    &audio_out_sdl,
103
#endif
104
#if HAVE_SNDIO
105
    &audio_out_sndio,
106
#endif
107
    &audio_out_null,
108
#if HAVE_COREAUDIO
109
    &audio_out_coreaudio_exclusive,
110
#endif
111
    &audio_out_pcm,
112
    &audio_out_lavc,
113
};
114
115
static bool get_desc(struct m_obj_desc *dst, int index)
116
2.75M
{
117
2.75M
    if (index >= MP_ARRAY_SIZE(audio_out_drivers))
118
355k
        return false;
119
2.39M
    const struct ao_driver *ao = audio_out_drivers[index];
120
2.39M
    *dst = (struct m_obj_desc) {
121
2.39M
        .name = ao->name,
122
2.39M
        .description = ao->description,
123
2.39M
        .priv_size = ao->priv_size,
124
2.39M
        .priv_defaults = ao->priv_defaults,
125
2.39M
        .options = ao->options,
126
2.39M
        .options_prefix = ao->options_prefix,
127
2.39M
        .global_opts = ao->global_opts,
128
2.39M
        .hidden = ao->encode,
129
2.39M
        .p = ao,
130
2.39M
    };
131
2.39M
    return true;
132
2.75M
}
133
134
// For the ao option
135
static const struct m_obj_list ao_obj_list = {
136
    .get_desc = get_desc,
137
    .description = "audio outputs",
138
    .allow_trailer = true,
139
    .disallow_positional_parameters = true,
140
    .use_global_options = true,
141
};
142
143
#define OPT_BASE_STRUCT struct ao_opts
144
const struct m_sub_options ao_conf = {
145
    .opts = (const struct m_option[]) {
146
        {"ao", OPT_SETTINGSLIST(audio_driver_list, &ao_obj_list),
147
            .flags = UPDATE_AUDIO},
148
        {"audio-device", OPT_STRING(audio_device), .flags = UPDATE_AUDIO},
149
        {"audio-client-name", OPT_STRING(audio_client_name), .flags = UPDATE_AUDIO},
150
        {"audio-buffer", OPT_DOUBLE(audio_buffer),
151
            .flags = UPDATE_AUDIO, M_RANGE(0, 10)},
152
        {"audio-set-media-role", OPT_BOOL(audio_set_media_role),
153
            .flags = UPDATE_AUDIO},
154
        {0}
155
    },
156
    .size = sizeof(OPT_BASE_STRUCT),
157
    .defaults = &(const OPT_BASE_STRUCT){
158
        .audio_buffer = 0.2,
159
        .audio_device = "auto",
160
        .audio_client_name = "mpv",
161
    },
162
};
163
164
static struct ao *ao_alloc(bool probing, struct mpv_global *global,
165
                           void (*wakeup_cb)(void *ctx), void *wakeup_ctx,
166
                           char *name)
167
82.3k
{
168
82.3k
    mp_assert(wakeup_cb);
169
170
82.3k
    struct mp_log *log = mp_log_new(NULL, global->log, "ao");
171
82.3k
    struct m_obj_desc desc;
172
82.3k
    if (!m_obj_list_find(&desc, &ao_obj_list, bstr0(name))) {
173
164
        mp_msg(log, MSGL_ERR, "Audio output %s not found!\n", name);
174
164
        talloc_free(log);
175
164
        return NULL;
176
82.2k
    };
177
82.2k
    struct ao_opts *opts = mp_get_config_group(NULL, global, &ao_conf);
178
82.2k
    struct ao *ao = talloc_ptrtype(NULL, ao);
179
82.2k
    talloc_steal(ao, log);
180
82.2k
    *ao = (struct ao) {
181
82.2k
        .driver = desc.p,
182
82.2k
        .probing = probing,
183
82.2k
        .global = global,
184
82.2k
        .wakeup_cb = wakeup_cb,
185
82.2k
        .wakeup_ctx = wakeup_ctx,
186
82.2k
        .log = mp_log_new(ao, log, name),
187
82.2k
        .def_buffer = opts->audio_buffer,
188
82.2k
        .client_name = talloc_strdup(ao, opts->audio_client_name),
189
82.2k
        .set_media_role = opts->audio_set_media_role
190
82.2k
    };
191
82.2k
    talloc_free(opts);
192
82.2k
    ao->priv = m_config_group_from_desc(ao, ao->log, global, &desc, name);
193
82.2k
    if (!ao->priv)
194
0
        goto error;
195
82.2k
    ao_set_gain(ao, 1.0f);
196
82.2k
    return ao;
197
0
error:
198
0
    talloc_free(ao);
199
0
    return NULL;
200
82.2k
}
201
202
static struct ao *ao_init(bool probing, struct mpv_global *global,
203
                          void (*wakeup_cb)(void *ctx), void *wakeup_ctx,
204
                          struct encode_lavc_context *encode_lavc_ctx, int flags,
205
                          int samplerate, int format, struct mp_chmap channels,
206
                          char *dev, char *name)
207
82.3k
{
208
82.3k
    struct ao *ao = ao_alloc(probing, global, wakeup_cb, wakeup_ctx, name);
209
82.3k
    if (!ao)
210
164
        return NULL;
211
82.2k
    ao->samplerate = samplerate;
212
82.2k
    ao->channels = channels;
213
82.2k
    ao->format = format;
214
82.2k
    ao->encode_lavc_ctx = encode_lavc_ctx;
215
82.2k
    ao->init_flags = flags;
216
82.2k
    if (ao->driver->encode != !!ao->encode_lavc_ctx)
217
11
        goto fail;
218
219
82.2k
    MP_VERBOSE(ao, "requested format: %d Hz, %s channels, %s\n",
220
82.2k
               ao->samplerate, mp_chmap_to_str(&ao->channels),
221
82.2k
               af_fmt_to_str(ao->format));
222
223
82.2k
    ao->device = talloc_strdup(ao, dev);
224
82.2k
    ao->stream_silence = flags & AO_INIT_STREAM_SILENCE;
225
226
82.2k
    init_buffer_pre(ao);
227
228
82.2k
    int r = ao->driver->init(ao);
229
82.2k
    if (r < 0) {
230
        // Silly exception for coreaudio spdif redirection
231
0
        if (ao->redirect) {
232
0
            char redirect[80], rdevice[80];
233
0
            snprintf(redirect, sizeof(redirect), "%s", ao->redirect);
234
0
            snprintf(rdevice, sizeof(rdevice), "%s", ao->device ? ao->device : "");
235
0
            ao_uninit(ao);
236
0
            return ao_init(probing, global, wakeup_cb, wakeup_ctx,
237
0
                           encode_lavc_ctx, flags, samplerate, format, channels,
238
0
                           rdevice, redirect);
239
0
        }
240
0
        goto fail;
241
0
    }
242
82.2k
    ao->driver_initialized = true;
243
244
82.2k
    ao->sstride = af_fmt_to_bytes(ao->format);
245
82.2k
    ao->num_planes = 1;
246
82.2k
    if (af_fmt_is_planar(ao->format)) {
247
44.6k
        ao->num_planes = ao->channels.num;
248
44.6k
    } else {
249
37.5k
        ao->sstride *= ao->channels.num;
250
37.5k
    }
251
82.2k
    ao->bps = (int64_t)ao->samplerate * ao->sstride;
252
253
82.2k
    if (ao->device_buffer <= 0 && ao->driver->write) {
254
547
        MP_ERR(ao, "Device buffer size not set.\n");
255
547
        goto fail;
256
547
    }
257
81.6k
    if (ao->device_buffer)
258
81.6k
        MP_VERBOSE(ao, "device buffer: %d samples.\n", ao->device_buffer);
259
81.6k
    ao->buffer = MPMAX(ao->device_buffer, ao->def_buffer * ao->samplerate);
260
81.6k
    ao->buffer = MPMAX(ao->buffer, 1);
261
262
81.6k
    int align = af_format_sample_alignment(ao->format);
263
81.6k
    ao->buffer = (ao->buffer + align - 1) / align * align;
264
81.6k
    MP_VERBOSE(ao, "using soft-buffer of %d samples.\n", ao->buffer);
265
266
81.6k
    if (!init_buffer_post(ao))
267
0
        goto fail;
268
81.6k
    return ao;
269
270
558
fail:
271
558
    ao_uninit(ao);
272
558
    return NULL;
273
81.6k
}
274
275
static void split_ao_device(void *tmp, char *opt, char **out_ao, char **out_dev)
276
82.2k
{
277
82.2k
    *out_ao = NULL;
278
82.2k
    *out_dev = NULL;
279
82.2k
    if (!opt)
280
0
        return;
281
82.2k
    if (!opt[0] || strcmp(opt, "auto") == 0)
282
82.2k
        return;
283
    // Split on "/". If "/" is the final character, or absent, out_dev is NULL.
284
14
    bstr b_dev, b_ao;
285
14
    bstr_split_tok(bstr0(opt), "/", &b_ao, &b_dev);
286
14
    if (b_dev.len > 0)
287
5
        *out_dev = bstrto0(tmp, b_dev);
288
14
    *out_ao = bstrto0(tmp, b_ao);
289
14
}
290
291
struct ao *ao_init_best(struct mpv_global *global,
292
                        int init_flags,
293
                        void (*wakeup_cb)(void *ctx), void *wakeup_ctx,
294
                        struct encode_lavc_context *encode_lavc_ctx,
295
                        int samplerate, int format, struct mp_chmap channels)
296
82.2k
{
297
82.2k
    void *tmp = talloc_new(NULL);
298
82.2k
    struct ao_opts *opts = mp_get_config_group(tmp, global, &ao_conf);
299
82.2k
    struct mp_log *log = mp_log_new(tmp, global->log, "ao");
300
82.2k
    struct ao *ao = NULL;
301
82.2k
    struct m_obj_settings *ao_list = NULL;
302
82.2k
    int ao_num = 0;
303
304
164k
    for (int n = 0; opts->audio_driver_list && opts->audio_driver_list[n].name; n++)
305
82.3k
        MP_TARRAY_APPEND(tmp, ao_list, ao_num, opts->audio_driver_list[n]);
306
307
82.2k
    bool forced_dev = false;
308
82.2k
    char *pref_ao, *pref_dev;
309
82.2k
    split_ao_device(tmp, opts->audio_device, &pref_ao, &pref_dev);
310
82.2k
    if (!ao_num && pref_ao) {
311
        // Reuse the autoselection code
312
0
        MP_TARRAY_APPEND(tmp, ao_list, ao_num,
313
0
            (struct m_obj_settings){.name = pref_ao});
314
0
        forced_dev = true;
315
0
    }
316
317
82.2k
    bool autoprobe = ao_num == 0;
318
319
    // Something like "--ao=a,b," means do autoprobing after a and b fail.
320
82.2k
    if (ao_num && strlen(ao_list[ao_num - 1].name) == 0) {
321
6
        ao_num -= 1;
322
6
        autoprobe = true;
323
6
    }
324
325
82.2k
    if (autoprobe) {
326
6
        for (int n = 0; n < MP_ARRAY_SIZE(audio_out_drivers); n++) {
327
6
            const struct ao_driver *driver = audio_out_drivers[n];
328
6
            if (driver == &audio_out_null)
329
6
                break;
330
0
            MP_TARRAY_APPEND(tmp, ao_list, ao_num,
331
0
                (struct m_obj_settings){.name = (char *)driver->name});
332
0
        }
333
6
    }
334
335
82.2k
    if (init_flags & AO_INIT_NULL_FALLBACK) {
336
1
        MP_TARRAY_APPEND(tmp, ao_list, ao_num,
337
1
            (struct m_obj_settings){.name = "null"});
338
1
    }
339
340
82.9k
    for (int n = 0; n < ao_num; n++) {
341
82.3k
        struct m_obj_settings *entry = &ao_list[n];
342
82.3k
        bool probing = n + 1 != ao_num;
343
82.3k
        mp_verbose(log, "Trying audio driver '%s'\n", entry->name);
344
82.3k
        char *dev = NULL;
345
82.3k
        if (pref_ao && pref_dev && strcmp(entry->name, pref_ao) == 0) {
346
0
            dev = pref_dev;
347
0
            mp_verbose(log, "Using preferred device '%s'\n", dev);
348
0
        }
349
82.3k
        ao = ao_init(probing, global, wakeup_cb, wakeup_ctx, encode_lavc_ctx,
350
82.3k
                     init_flags, samplerate, format, channels, dev, entry->name);
351
82.3k
        if (ao)
352
81.6k
            break;
353
722
        if (!probing)
354
586
            mp_err(log, "Failed to initialize audio driver '%s'\n", entry->name);
355
722
        if (dev && forced_dev) {
356
0
            mp_err(log, "This audio driver/device was forced with the "
357
0
                        "--audio-device option.\nTry unsetting it.\n");
358
0
        }
359
722
    }
360
361
82.2k
    talloc_free(tmp);
362
82.2k
    return ao;
363
82.2k
}
364
365
// Query the AO_EVENT_*s as requested by the events parameter, and return them.
366
int ao_query_and_reset_events(struct ao *ao, int events)
367
5.27M
{
368
5.27M
    return atomic_fetch_and(&ao->events_, ~(unsigned)events) & events;
369
5.27M
}
370
371
// Returns events that were set by this calls.
372
static int ao_add_events(struct ao *ao, int events)
373
0
{
374
0
    unsigned prev_events = atomic_fetch_or(&ao->events_, events);
375
0
    unsigned new = events & ~prev_events;
376
0
    if (new)
377
0
        ao->wakeup_cb(ao->wakeup_ctx);
378
0
    return new;
379
0
}
380
381
// Request that the player core destroys and recreates the AO. Fully thread-safe.
382
void ao_request_reload(struct ao *ao)
383
0
{
384
0
    ao_add_events(ao, AO_EVENT_RELOAD);
385
0
}
386
387
// Notify the player that the device list changed. Fully thread-safe.
388
void ao_hotplug_event(struct ao *ao)
389
0
{
390
0
    ao_add_events(ao, AO_EVENT_HOTPLUG);
391
0
}
392
393
bool ao_chmap_sel_adjust(struct ao *ao, const struct mp_chmap_sel *s,
394
                         struct mp_chmap *map)
395
82.2k
{
396
82.2k
    MP_VERBOSE(ao, "Channel layouts:\n");
397
82.2k
    mp_chmal_sel_log(s, ao->log, MSGL_V);
398
82.2k
    bool r = mp_chmap_sel_adjust(s, map);
399
82.2k
    if (r)
400
82.2k
        MP_VERBOSE(ao, "result: %s\n", mp_chmap_to_str(map));
401
82.2k
    return r;
402
82.2k
}
403
404
// safe_multichannel=true behaves like ao_chmap_sel_adjust.
405
// safe_multichannel=false is a helper for callers which do not support safe
406
// handling of arbitrary channel layouts. If the multichannel layouts are not
407
// considered "always safe" (e.g. HDMI), then allow only stereo or mono, if
408
// they are part of the list in *s.
409
bool ao_chmap_sel_adjust2(struct ao *ao, const struct mp_chmap_sel *s,
410
                          struct mp_chmap *map, bool safe_multichannel)
411
0
{
412
0
    if (!safe_multichannel && (ao->init_flags & AO_INIT_SAFE_MULTICHANNEL_ONLY)) {
413
0
        struct mp_chmap res = *map;
414
0
        if (mp_chmap_sel_adjust(s, &res)) {
415
0
            if (!mp_chmap_equals(&res, &(struct mp_chmap)MP_CHMAP_INIT_MONO) &&
416
0
                !mp_chmap_equals(&res, &(struct mp_chmap)MP_CHMAP_INIT_STEREO))
417
0
            {
418
0
                MP_VERBOSE(ao, "Disabling multichannel output.\n");
419
0
                *map = (struct mp_chmap)MP_CHMAP_INIT_STEREO;
420
0
            }
421
0
        }
422
0
    }
423
424
0
    return ao_chmap_sel_adjust(ao, s, map);
425
0
}
426
427
bool ao_chmap_sel_get_def(struct ao *ao, const struct mp_chmap_sel *s,
428
                          struct mp_chmap *map, int num)
429
0
{
430
0
    return mp_chmap_sel_get_def(s, map, num);
431
0
}
432
433
// --- The following functions just return immutable information.
434
435
void ao_get_format(struct ao *ao,
436
                   int *samplerate, int *format, struct mp_chmap *channels)
437
163k
{
438
163k
    *samplerate = ao->samplerate;
439
163k
    *format = ao->format;
440
163k
    *channels = ao->channels;
441
163k
}
442
443
const char *ao_get_name(struct ao *ao)
444
81.7k
{
445
81.7k
    return ao->driver->name;
446
81.7k
}
447
448
const char *ao_get_description(struct ao *ao)
449
81.6k
{
450
81.6k
    return ao->driver->description;
451
81.6k
}
452
453
bool ao_untimed(struct ao *ao)
454
177k
{
455
177k
    return ao->untimed;
456
177k
}
457
458
// ---
459
460
struct ao_hotplug {
461
    struct mpv_global *global;
462
    void (*wakeup_cb)(void *ctx);
463
    void *wakeup_ctx;
464
    // A single AO instance is used to listen to hotplug events. It wouldn't
465
    // make much sense to allow multiple AO drivers; all sane platforms have
466
    // a single audio API providing all events.
467
    // This is _not_ necessarily the same AO instance as used for playing
468
    // audio.
469
    struct ao *ao;
470
    // cached
471
    struct ao_device_list *list;
472
    bool needs_update;
473
};
474
475
struct ao_hotplug *ao_hotplug_create(struct mpv_global *global,
476
                                     void (*wakeup_cb)(void *ctx),
477
                                     void *wakeup_ctx)
478
129
{
479
129
    struct ao_hotplug *hp = talloc_ptrtype(NULL, hp);
480
129
    *hp = (struct ao_hotplug){
481
129
        .global = global,
482
129
        .wakeup_cb = wakeup_cb,
483
129
        .wakeup_ctx = wakeup_ctx,
484
129
        .needs_update = true,
485
129
    };
486
129
    return hp;
487
129
}
488
489
static void get_devices(struct ao *ao, struct ao_device_list *list)
490
0
{
491
0
    if (ao->driver->list_devs) {
492
0
        ao->driver->list_devs(ao, list);
493
0
    } else {
494
0
        ao_device_list_add(list, ao, &(struct ao_device_desc){"", ""});
495
0
    }
496
0
}
497
498
bool ao_hotplug_check_update(struct ao_hotplug *hp)
499
760
{
500
760
    if (hp->ao && ao_query_and_reset_events(hp->ao, AO_EVENT_HOTPLUG)) {
501
0
        hp->needs_update = true;
502
0
        return true;
503
0
    }
504
760
    return false;
505
760
}
506
507
// The return value is valid until the next call to this API.
508
struct ao_device_list *ao_hotplug_get_device_list(struct ao_hotplug *hp,
509
                                                  struct ao *playback_ao)
510
710
{
511
710
    if (hp->list && !hp->needs_update)
512
591
        return hp->list;
513
514
119
    talloc_free(hp->list);
515
119
    struct ao_device_list *list = talloc_zero(hp, struct ao_device_list);
516
119
    hp->list = list;
517
518
119
    MP_TARRAY_APPEND(list, list->devices, list->num_devices,
519
119
        (struct ao_device_desc){"auto", "Autoselect device"});
520
521
    // Try to use the same AO for hotplug handling as for playback.
522
    // Different AOs may not agree and the playback one is the only one the
523
    // user knows about and may even have configured explicitly.
524
119
    if (!hp->ao && playback_ao && playback_ao->driver->hotplug_init) {
525
0
        struct ao *ao = ao_alloc(true, hp->global, hp->wakeup_cb, hp->wakeup_ctx,
526
0
                                 (char *)playback_ao->driver->name);
527
0
        if (playback_ao->driver->hotplug_init(ao) >= 0) {
528
0
            hp->ao = ao;
529
0
        } else {
530
0
            talloc_free(ao);
531
0
        }
532
0
    }
533
534
119
    for (int n = 0; n < MP_ARRAY_SIZE(audio_out_drivers); n++) {
535
119
        const struct ao_driver *d = audio_out_drivers[n];
536
119
        if (d == &audio_out_null)
537
119
            break; // don't add unsafe/special entries
538
539
0
        struct ao *ao = ao_alloc(true, hp->global, hp->wakeup_cb, hp->wakeup_ctx,
540
0
                                 (char *)d->name);
541
0
        if (!ao)
542
0
            continue;
543
544
0
        if (ao->driver->hotplug_init) {
545
0
            if (ao->driver->hotplug_init(ao) >= 0) {
546
0
                get_devices(ao, list);
547
0
                if (hp->ao)
548
0
                    ao->driver->hotplug_uninit(ao);
549
0
                else
550
0
                    hp->ao = ao; // keep this one
551
0
            }
552
0
        } else {
553
0
            get_devices(ao, list);
554
0
        }
555
0
        if (ao != hp->ao)
556
0
            talloc_free(ao);
557
0
    }
558
119
    hp->needs_update = false;
559
119
    return list;
560
710
}
561
562
void ao_device_list_add(struct ao_device_list *list, struct ao *ao,
563
                        struct ao_device_desc *e)
564
0
{
565
0
    struct ao_device_desc c = *e;
566
0
    const char *dname = ao->driver->name;
567
0
    char buf[80];
568
0
    if (!c.desc || !c.desc[0]) {
569
0
        if (c.name && c.name[0]) {
570
0
            c.desc = c.name;
571
0
        } else if (list->num_devices) {
572
            // Assume this is the default device.
573
0
            snprintf(buf, sizeof(buf), "Default (%s)", dname);
574
0
            c.desc = buf;
575
0
        } else {
576
            // First default device (and maybe the only one).
577
0
            c.desc = "Default";
578
0
        }
579
0
    }
580
0
    c.name = (c.name && c.name[0]) ? talloc_asprintf(list, "%s/%s", dname, c.name)
581
0
                                   : talloc_strdup(list, dname);
582
0
    c.desc = talloc_strdup(list, c.desc);
583
0
    MP_TARRAY_APPEND(list, list->devices, list->num_devices, c);
584
0
}
585
586
void ao_hotplug_destroy(struct ao_hotplug *hp)
587
317k
{
588
317k
    if (!hp)
589
317k
        return;
590
129
    if (hp->ao && hp->ao->driver->hotplug_uninit)
591
0
        hp->ao->driver->hotplug_uninit(hp->ao);
592
129
    talloc_free(hp->ao);
593
129
    talloc_free(hp);
594
129
}
595
596
static void dummy_wakeup(void *ctx)
597
0
{
598
0
}
599
600
void ao_print_devices(struct mpv_global *global, struct mp_log *log,
601
                      struct ao *playback_ao)
602
0
{
603
0
    struct ao_hotplug *hp = ao_hotplug_create(global, dummy_wakeup, NULL);
604
0
    struct ao_device_list *list = ao_hotplug_get_device_list(hp, playback_ao);
605
0
    mp_info(log, "List of detected audio devices:\n");
606
0
    for (int n = 0; n < list->num_devices; n++) {
607
0
        struct ao_device_desc *desc = &list->devices[n];
608
0
        mp_info(log, "  '%s' (%s)\n", desc->name, desc->desc);
609
0
    }
610
0
    ao_hotplug_destroy(hp);
611
0
}
612
613
void ao_set_gain(struct ao *ao, float gain)
614
251k
{
615
251k
    atomic_store(&ao->gain, gain);
616
251k
}
617
618
#define MUL_GAIN_i(d, num_samples, gain, low, center, high)                     \
619
422k
    for (int n = 0; n < (num_samples); n++)                                     \
620
422k
        (d)[n] = MPCLAMP(                                                       \
621
505
            ((((int64_t)((d)[n]) - (center)) * (gain) + 128) >> 8) + (center),  \
622
505
            (low), (high))
623
624
#define MUL_GAIN_f(d, num_samples, gain)                                        \
625
1.01M
    for (int n = 0; n < (num_samples); n++)                                     \
626
1.00M
        (d)[n] = (d)[n] * (gain)
627
628
static void process_plane(struct ao *ao, void *data, int num_samples)
629
13.0M
{
630
13.0M
    float gain = atomic_load_explicit(&ao->gain, memory_order_relaxed);
631
13.0M
    int gi = lrint(256.0 * gain);
632
13.0M
    if (gi == 256)
633
13.0M
        return;
634
9.42k
    switch (af_fmt_from_planar(ao->format)) {
635
0
    case AF_FORMAT_U8:
636
0
        MUL_GAIN_i((uint8_t *)data, num_samples, gi, 0, 128, 255);
637
0
        break;
638
505
    case AF_FORMAT_S16:
639
505
        MUL_GAIN_i((int16_t *)data, num_samples, gi, INT16_MIN, 0, INT16_MAX);
640
505
        break;
641
0
    case AF_FORMAT_S32:
642
0
        MUL_GAIN_i((int32_t *)data, num_samples, gi, INT32_MIN, 0, INT32_MAX);
643
0
        break;
644
8.92k
    case AF_FORMAT_FLOAT:
645
8.92k
        MUL_GAIN_f((float *)data, num_samples, gain);
646
8.92k
        break;
647
0
    case AF_FORMAT_DOUBLE:
648
0
        MUL_GAIN_f((double *)data, num_samples, gain);
649
0
        break;
650
0
    default:;
651
        // all other sample formats are simply not supported
652
9.42k
    }
653
9.42k
}
654
655
void ao_post_process_data(struct ao *ao, void **data, int num_samples)
656
11.5M
{
657
11.5M
    bool planar = af_fmt_is_planar(ao->format);
658
11.5M
    int planes = planar ? ao->channels.num : 1;
659
11.5M
    int plane_samples = num_samples * (planar ? 1: ao->channels.num);
660
24.5M
    for (int n = 0; n < planes; n++)
661
13.0M
        process_plane(ao, data[n], plane_samples);
662
11.5M
}
663
664
static int get_conv_type(struct ao_convert_fmt *fmt)
665
0
{
666
0
    if (af_fmt_to_bytes(fmt->src_fmt) * 8 == fmt->dst_bits && !fmt->pad_msb)
667
0
        return 0; // passthrough
668
0
    if (fmt->src_fmt == AF_FORMAT_S32 && fmt->dst_bits == 24 && !fmt->pad_msb)
669
0
        return 1; // simple 32->24 bit conversion
670
0
    if (fmt->src_fmt == AF_FORMAT_S32 && fmt->dst_bits == 32 && fmt->pad_msb == 8)
671
0
        return 2; // simple 32->24 bit conversion, with MSB padding
672
0
    return -1; // unsupported
673
0
}
674
675
// Check whether ao_convert_inplace() can be called. As an exception, the
676
// planar-ness of the sample format and the number of channels is ignored.
677
// All other parameters must be as passed to ao_convert_inplace().
678
bool ao_can_convert_inplace(struct ao_convert_fmt *fmt)
679
0
{
680
0
    return get_conv_type(fmt) >= 0;
681
0
}
682
683
bool ao_need_conversion(struct ao_convert_fmt *fmt)
684
0
{
685
0
    return get_conv_type(fmt) != 0;
686
0
}
687
688
// The LSB is always ignored.
689
#if BYTE_ORDER == BIG_ENDIAN
690
#define SHIFT24(x) ((3-(x))*8)
691
#else
692
0
#define SHIFT24(x) (((x)+1)*8)
693
#endif
694
695
static void convert_plane(int type, void *data, int num_samples)
696
0
{
697
0
    switch (type) {
698
0
    case 0:
699
0
        break;
700
0
    case 1: /* fall through */
701
0
    case 2: {
702
0
        int bytes = type == 1 ? 3 : 4;
703
0
        for (int s = 0; s < num_samples; s++) {
704
0
            uint32_t val = *((uint32_t *)data + s);
705
0
            uint8_t *ptr = (uint8_t *)data + s * bytes;
706
0
            ptr[0] = val >> SHIFT24(0);
707
0
            ptr[1] = val >> SHIFT24(1);
708
0
            ptr[2] = val >> SHIFT24(2);
709
0
            if (type == 2)
710
0
                ptr[3] = 0;
711
0
        }
712
0
        break;
713
0
    }
714
0
    default:
715
0
        MP_ASSERT_UNREACHABLE();
716
0
    }
717
0
}
718
719
// data[n] contains the pointer to the first sample of the n-th plane, in the
720
// format implied by fmt->src_fmt. src_fmt also controls whether the data is
721
// all in one plane, or if there is a plane per channel.
722
void ao_convert_inplace(struct ao_convert_fmt *fmt, void **data, int num_samples)
723
0
{
724
0
    int type = get_conv_type(fmt);
725
0
    bool planar = af_fmt_is_planar(fmt->src_fmt);
726
0
    int planes = planar ? fmt->channels : 1;
727
0
    int plane_samples = num_samples * (planar ? 1: fmt->channels);
728
0
    for (int n = 0; n < planes; n++)
729
0
        convert_plane(type, data[n], plane_samples);
730
0
}