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 <stddef.h>
19 #include <stdbool.h>
20 #include <inttypes.h>
21 #include <limits.h>
22 #include <math.h>
23 #include <assert.h>
24 
25 #include "config.h"
26 #include "mpv_talloc.h"
27 
28 #include "common/msg.h"
29 #include "common/encode.h"
30 #include "options/options.h"
31 #include "common/common.h"
32 #include "osdep/timer.h"
33 
34 #include "audio/format.h"
35 #include "audio/out/ao.h"
36 #include "demux/demux.h"
37 #include "filters/f_async_queue.h"
38 #include "filters/f_decoder_wrapper.h"
39 #include "filters/filter_internal.h"
40 
41 #include "core.h"
42 #include "command.h"
43 
44 enum {
45     AD_OK = 0,
46     AD_EOF = -2,
47     AD_WAIT = -4,
48 };
49 
50 static void ao_process(struct mp_filter *f);
51 
update_speed_filters(struct MPContext * mpctx)52 static void update_speed_filters(struct MPContext *mpctx)
53 {
54     struct ao_chain *ao_c = mpctx->ao_chain;
55     if (!ao_c)
56         return;
57 
58     double speed = mpctx->opts->playback_speed;
59     double resample = mpctx->speed_factor_a;
60     double drop = 1.0;
61 
62     if (!mpctx->opts->pitch_correction) {
63         resample *= speed;
64         speed = 1.0;
65     }
66 
67     if (mpctx->display_sync_active && mpctx->opts->video_sync == VS_DISP_ADROP) {
68         drop *= speed * resample;
69         resample = speed = 1.0;
70     }
71 
72     mp_output_chain_set_audio_speed(ao_c->filter, speed, resample, drop);
73 }
74 
recreate_audio_filters(struct MPContext * mpctx)75 static int recreate_audio_filters(struct MPContext *mpctx)
76 {
77     struct ao_chain *ao_c = mpctx->ao_chain;
78     assert(ao_c);
79 
80     if (!mp_output_chain_update_filters(ao_c->filter, mpctx->opts->af_settings))
81         goto fail;
82 
83     update_speed_filters(mpctx);
84 
85     mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
86 
87     return 0;
88 
89 fail:
90     MP_ERR(mpctx, "Audio filter initialized failed!\n");
91     return -1;
92 }
93 
reinit_audio_filters(struct MPContext * mpctx)94 int reinit_audio_filters(struct MPContext *mpctx)
95 {
96     struct ao_chain *ao_c = mpctx->ao_chain;
97     if (!ao_c)
98         return 0;
99 
100     double delay = mp_output_get_measured_total_delay(ao_c->filter);
101 
102     if (recreate_audio_filters(mpctx) < 0)
103         return -1;
104 
105     double ndelay = mp_output_get_measured_total_delay(ao_c->filter);
106 
107     // Only force refresh if the amount of dropped buffered data is going to
108     // cause "issues" for the A/V sync logic.
109     if (mpctx->audio_status == STATUS_PLAYING && delay - ndelay >= 0.2)
110         issue_refresh_seek(mpctx, MPSEEK_EXACT);
111     return 1;
112 }
113 
db_gain(double db)114 static double db_gain(double db)
115 {
116     return pow(10.0, db/20.0);
117 }
118 
compute_replaygain(struct MPContext * mpctx)119 static float compute_replaygain(struct MPContext *mpctx)
120 {
121     struct MPOpts *opts = mpctx->opts;
122 
123     float rgain = 1.0;
124 
125     struct replaygain_data *rg = NULL;
126     struct track *track = mpctx->current_track[0][STREAM_AUDIO];
127     if (track)
128         rg = track->stream->codec->replaygain_data;
129     if (opts->rgain_mode && rg) {
130         MP_VERBOSE(mpctx, "Replaygain: Track=%f/%f Album=%f/%f\n",
131                    rg->track_gain, rg->track_peak,
132                    rg->album_gain, rg->album_peak);
133 
134         float gain, peak;
135         if (opts->rgain_mode == 1) {
136             gain = rg->track_gain;
137             peak = rg->track_peak;
138         } else {
139             gain = rg->album_gain;
140             peak = rg->album_peak;
141         }
142 
143         gain += opts->rgain_preamp;
144         rgain = db_gain(gain);
145 
146         MP_VERBOSE(mpctx, "Applying replay-gain: %f\n", rgain);
147 
148         if (!opts->rgain_clip) { // clipping prevention
149             rgain = MPMIN(rgain, 1.0 / peak);
150             MP_VERBOSE(mpctx, "...with clipping prevention: %f\n", rgain);
151         }
152     } else if (opts->rgain_fallback) {
153         rgain = db_gain(opts->rgain_fallback);
154         MP_VERBOSE(mpctx, "Applying fallback gain: %f\n", rgain);
155     }
156 
157     return rgain;
158 }
159 
160 // Called when opts->softvol_volume or opts->softvol_mute were changed.
audio_update_volume(struct MPContext * mpctx)161 void audio_update_volume(struct MPContext *mpctx)
162 {
163     struct MPOpts *opts = mpctx->opts;
164     struct ao_chain *ao_c = mpctx->ao_chain;
165     if (!ao_c || !ao_c->ao)
166         return;
167 
168     float gain = MPMAX(opts->softvol_volume / 100.0, 0);
169     gain = pow(gain, 3);
170     gain *= compute_replaygain(mpctx);
171     if (opts->softvol_mute == 1)
172         gain = 0.0;
173 
174     ao_set_gain(ao_c->ao, gain);
175 }
176 
177 // Call this if opts->playback_speed or mpctx->speed_factor_* change.
update_playback_speed(struct MPContext * mpctx)178 void update_playback_speed(struct MPContext *mpctx)
179 {
180     mpctx->audio_speed = mpctx->opts->playback_speed * mpctx->speed_factor_a;
181     mpctx->video_speed = mpctx->opts->playback_speed * mpctx->speed_factor_v;
182 
183     update_speed_filters(mpctx);
184 }
185 
ao_chain_reset_state(struct ao_chain * ao_c)186 static void ao_chain_reset_state(struct ao_chain *ao_c)
187 {
188     ao_c->last_out_pts = MP_NOPTS_VALUE;
189     ao_c->out_eof = false;
190     ao_c->underrun = false;
191     ao_c->start_pts_known = false;
192     ao_c->start_pts = MP_NOPTS_VALUE;
193     ao_c->untimed_throttle = false;
194     ao_c->underrun = false;
195 }
196 
reset_audio_state(struct MPContext * mpctx)197 void reset_audio_state(struct MPContext *mpctx)
198 {
199     if (mpctx->ao_chain) {
200         ao_chain_reset_state(mpctx->ao_chain);
201         struct track *t = mpctx->ao_chain->track;
202         if (t && t->dec)
203             mp_decoder_wrapper_set_play_dir(t->dec, mpctx->play_dir);
204     }
205     mpctx->audio_status = mpctx->ao_chain ? STATUS_SYNCING : STATUS_EOF;
206     mpctx->delay = 0;
207     mpctx->logged_async_diff = -1;
208 }
209 
uninit_audio_out(struct MPContext * mpctx)210 void uninit_audio_out(struct MPContext *mpctx)
211 {
212     struct ao_chain *ao_c = mpctx->ao_chain;
213     if (ao_c) {
214         ao_c->ao_queue = NULL;
215         TA_FREEP(&ao_c->queue_filter);
216         ao_c->ao = NULL;
217     }
218     if (mpctx->ao) {
219         // Note: with gapless_audio, stop_play is not correctly set
220         if ((mpctx->opts->gapless_audio || mpctx->stop_play == AT_END_OF_FILE) &&
221             ao_is_playing(mpctx->ao) && !get_internal_paused(mpctx))
222         {
223             MP_VERBOSE(mpctx, "draining left over audio\n");
224             ao_drain(mpctx->ao);
225         }
226         ao_uninit(mpctx->ao);
227 
228         mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
229     }
230     mpctx->ao = NULL;
231     TA_FREEP(&mpctx->ao_filter_fmt);
232 }
233 
ao_chain_uninit(struct ao_chain * ao_c)234 static void ao_chain_uninit(struct ao_chain *ao_c)
235 {
236     struct track *track = ao_c->track;
237     if (track) {
238         assert(track->ao_c == ao_c);
239         track->ao_c = NULL;
240         if (ao_c->dec_src)
241             assert(track->dec->f->pins[0] == ao_c->dec_src);
242         talloc_free(track->dec->f);
243         track->dec = NULL;
244     }
245 
246     if (ao_c->filter_src)
247         mp_pin_disconnect(ao_c->filter_src);
248 
249     talloc_free(ao_c->filter->f);
250     talloc_free(ao_c->ao_filter);
251     talloc_free(ao_c);
252 }
253 
uninit_audio_chain(struct MPContext * mpctx)254 void uninit_audio_chain(struct MPContext *mpctx)
255 {
256     if (mpctx->ao_chain) {
257         ao_chain_uninit(mpctx->ao_chain);
258         mpctx->ao_chain = NULL;
259 
260         mpctx->audio_status = STATUS_EOF;
261 
262         mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
263     }
264 }
265 
audio_config_to_str_buf(char * buf,size_t buf_sz,int rate,int format,struct mp_chmap channels)266 static char *audio_config_to_str_buf(char *buf, size_t buf_sz, int rate,
267                                      int format, struct mp_chmap channels)
268 {
269     char ch[128];
270     mp_chmap_to_str_buf(ch, sizeof(ch), &channels);
271     char *hr_ch = mp_chmap_to_str_hr(&channels);
272     if (strcmp(hr_ch, ch) != 0)
273         mp_snprintf_cat(ch, sizeof(ch), " (%s)", hr_ch);
274     snprintf(buf, buf_sz, "%dHz %s %dch %s", rate,
275              ch, channels.num, af_fmt_to_str(format));
276     return buf;
277 }
278 
279 // Decide whether on a format change, we should reinit the AO.
keep_weak_gapless_format(struct mp_aframe * old,struct mp_aframe * new)280 static bool keep_weak_gapless_format(struct mp_aframe *old, struct mp_aframe* new)
281 {
282     bool res = false;
283     struct mp_aframe *new_mod = mp_aframe_new_ref(new);
284     if (!new_mod)
285         abort();
286 
287     // If the sample formats are compatible (== libswresample generally can
288     // convert them), keep the AO. On other changes, recreate it.
289 
290     int old_fmt = mp_aframe_get_format(old);
291     int new_fmt = mp_aframe_get_format(new);
292 
293     if (af_format_conversion_score(old_fmt, new_fmt) == INT_MIN)
294         goto done; // completely incompatible formats
295 
296     if (!mp_aframe_set_format(new_mod, old_fmt))
297         goto done;
298 
299     res = mp_aframe_config_equals(old, new_mod);
300 
301 done:
302     talloc_free(new_mod);
303     return res;
304 }
305 
ao_chain_set_ao(struct ao_chain * ao_c,struct ao * ao)306 static void ao_chain_set_ao(struct ao_chain *ao_c, struct ao *ao)
307 {
308     if (ao_c->ao != ao) {
309         assert(!ao_c->ao);
310         ao_c->ao = ao;
311         ao_c->ao_queue = ao_get_queue(ao_c->ao);
312         ao_c->queue_filter = mp_async_queue_create_filter(ao_c->ao_filter,
313                                                           MP_PIN_IN, ao_c->ao_queue);
314         mp_async_queue_set_notifier(ao_c->queue_filter, ao_c->ao_filter);
315         // Make sure filtering never stops with frames stuck in access filter.
316         mp_filter_set_high_priority(ao_c->queue_filter, true);
317         audio_update_volume(ao_c->mpctx);
318     }
319 
320     if (ao_c->filter->ao_needs_update)
321         mp_output_chain_set_ao(ao_c->filter, ao_c->ao);
322 
323     mp_filter_wakeup(ao_c->ao_filter);
324 }
325 
reinit_audio_filters_and_output(struct MPContext * mpctx)326 static int reinit_audio_filters_and_output(struct MPContext *mpctx)
327 {
328     struct MPOpts *opts = mpctx->opts;
329     struct ao_chain *ao_c = mpctx->ao_chain;
330     assert(ao_c);
331     struct track *track = ao_c->track;
332 
333     assert(ao_c->filter->ao_needs_update);
334 
335     // The "ideal" filter output format
336     struct mp_aframe *out_fmt = mp_aframe_new_ref(ao_c->filter->output_aformat);
337     if (!out_fmt)
338         abort();
339 
340     if (!mp_aframe_config_is_valid(out_fmt)) {
341         talloc_free(out_fmt);
342         goto init_error;
343     }
344 
345     if (af_fmt_is_pcm(mp_aframe_get_format(out_fmt))) {
346         if (opts->force_srate)
347             mp_aframe_set_rate(out_fmt, opts->force_srate);
348         if (opts->audio_output_format)
349             mp_aframe_set_format(out_fmt, opts->audio_output_format);
350         if (opts->audio_output_channels.num_chmaps == 1)
351             mp_aframe_set_chmap(out_fmt, &opts->audio_output_channels.chmaps[0]);
352     }
353 
354     // Weak gapless audio: if the filter output format is the same as the
355     // previous one, keep the AO and don't reinit anything.
356     // Strong gapless: always keep the AO
357     if ((mpctx->ao_filter_fmt && mpctx->ao && opts->gapless_audio < 0 &&
358          keep_weak_gapless_format(mpctx->ao_filter_fmt, out_fmt)) ||
359         (mpctx->ao && opts->gapless_audio > 0))
360     {
361         ao_chain_set_ao(ao_c, mpctx->ao);
362         talloc_free(out_fmt);
363         return 0;
364     }
365 
366     // Wait until all played.
367     if (mpctx->ao && ao_is_playing(mpctx->ao)) {
368         talloc_free(out_fmt);
369         return 0;
370     }
371     // Format change during syncing. Force playback start early, then wait.
372     if (ao_c->ao_queue && mp_async_queue_get_frames(ao_c->ao_queue) &&
373         mpctx->audio_status == STATUS_SYNCING)
374     {
375         mpctx->audio_status = STATUS_READY;
376         mp_wakeup_core(mpctx);
377         talloc_free(out_fmt);
378         return 0;
379     }
380     if (mpctx->audio_status == STATUS_READY) {
381         talloc_free(out_fmt);
382         return 0;
383     }
384 
385     uninit_audio_out(mpctx);
386 
387     int out_rate = mp_aframe_get_rate(out_fmt);
388     int out_format = mp_aframe_get_format(out_fmt);
389     struct mp_chmap out_channels = {0};
390     mp_aframe_get_chmap(out_fmt, &out_channels);
391 
392     int ao_flags = 0;
393     bool spdif_fallback = af_fmt_is_spdif(out_format) &&
394                           ao_c->spdif_passthrough;
395 
396     if (opts->ao_null_fallback && !spdif_fallback)
397         ao_flags |= AO_INIT_NULL_FALLBACK;
398 
399     if (opts->audio_stream_silence)
400         ao_flags |= AO_INIT_STREAM_SILENCE;
401 
402     if (opts->audio_exclusive)
403         ao_flags |= AO_INIT_EXCLUSIVE;
404 
405     if (af_fmt_is_pcm(out_format)) {
406         if (!opts->audio_output_channels.set ||
407             opts->audio_output_channels.auto_safe)
408             ao_flags |= AO_INIT_SAFE_MULTICHANNEL_ONLY;
409 
410         mp_chmap_sel_list(&out_channels,
411                           opts->audio_output_channels.chmaps,
412                           opts->audio_output_channels.num_chmaps);
413     }
414 
415     mpctx->ao_filter_fmt = out_fmt;
416 
417     mpctx->ao = ao_init_best(mpctx->global, ao_flags, mp_wakeup_core_cb,
418                              mpctx, mpctx->encode_lavc_ctx, out_rate,
419                              out_format, out_channels);
420 
421     int ao_rate = 0;
422     int ao_format = 0;
423     struct mp_chmap ao_channels = {0};
424     if (mpctx->ao)
425         ao_get_format(mpctx->ao, &ao_rate, &ao_format, &ao_channels);
426 
427     // Verify passthrough format was not changed.
428     if (mpctx->ao && af_fmt_is_spdif(out_format)) {
429         if (out_rate != ao_rate || out_format != ao_format ||
430             !mp_chmap_equals(&out_channels, &ao_channels))
431         {
432             MP_ERR(mpctx, "Passthrough format unsupported.\n");
433             ao_uninit(mpctx->ao);
434             mpctx->ao = NULL;
435         }
436     }
437 
438     if (!mpctx->ao) {
439         // If spdif was used, try to fallback to PCM.
440         if (spdif_fallback && ao_c->track && ao_c->track->dec) {
441             MP_VERBOSE(mpctx, "Falling back to PCM output.\n");
442             ao_c->spdif_passthrough = false;
443             ao_c->spdif_failed = true;
444             mp_decoder_wrapper_set_spdif_flag(ao_c->track->dec, false);
445             if (!mp_decoder_wrapper_reinit(ao_c->track->dec))
446                 goto init_error;
447             reset_audio_state(mpctx);
448             mp_output_chain_reset_harder(ao_c->filter);
449             mp_wakeup_core(mpctx); // reinit with new format next time
450             return 0;
451         }
452 
453         MP_ERR(mpctx, "Could not open/initialize audio device -> no sound.\n");
454         mpctx->error_playing = MPV_ERROR_AO_INIT_FAILED;
455         goto init_error;
456     }
457 
458     char tmp[192];
459     MP_INFO(mpctx, "AO: [%s] %s\n", ao_get_name(mpctx->ao),
460             audio_config_to_str_buf(tmp, sizeof(tmp), ao_rate, ao_format,
461                                     ao_channels));
462     MP_VERBOSE(mpctx, "AO: Description: %s\n", ao_get_description(mpctx->ao));
463     update_window_title(mpctx, true);
464 
465     ao_c->ao_resume_time =
466         opts->audio_wait_open > 0 ? mp_time_sec() + opts->audio_wait_open : 0;
467 
468     ao_set_paused(mpctx->ao, get_internal_paused(mpctx));
469 
470     ao_chain_set_ao(ao_c, mpctx->ao);
471 
472     audio_update_volume(mpctx);
473 
474     // Almost nonsensical hack to deal with certain format change scenarios.
475     if (mpctx->audio_status == STATUS_PLAYING)
476         ao_start(mpctx->ao);
477 
478     mp_wakeup_core(mpctx);
479     mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
480 
481     return 0;
482 
483 init_error:
484     uninit_audio_chain(mpctx);
485     uninit_audio_out(mpctx);
486     error_on_track(mpctx, track);
487     return -1;
488 }
489 
init_audio_decoder(struct MPContext * mpctx,struct track * track)490 int init_audio_decoder(struct MPContext *mpctx, struct track *track)
491 {
492     assert(!track->dec);
493     if (!track->stream)
494         goto init_error;
495 
496     track->dec = mp_decoder_wrapper_create(mpctx->filter_root, track->stream);
497     if (!track->dec)
498         goto init_error;
499 
500     if (track->ao_c)
501         mp_decoder_wrapper_set_spdif_flag(track->dec, true);
502 
503     if (!mp_decoder_wrapper_reinit(track->dec))
504         goto init_error;
505 
506     return 1;
507 
508 init_error:
509     if (track->sink)
510         mp_pin_disconnect(track->sink);
511     track->sink = NULL;
512     error_on_track(mpctx, track);
513     return 0;
514 }
515 
reinit_audio_chain(struct MPContext * mpctx)516 void reinit_audio_chain(struct MPContext *mpctx)
517 {
518     struct track *track = NULL;
519     track = mpctx->current_track[0][STREAM_AUDIO];
520     if (!track || !track->stream) {
521         if (!mpctx->encode_lavc_ctx)
522             uninit_audio_out(mpctx);
523         error_on_track(mpctx, track);
524         return;
525     }
526     reinit_audio_chain_src(mpctx, track);
527 }
528 
529 static const struct mp_filter_info ao_filter = {
530     .name = "ao",
531     .process = ao_process,
532 };
533 
534 // (track=NULL creates a blank chain, used for lavfi-complex)
reinit_audio_chain_src(struct MPContext * mpctx,struct track * track)535 void reinit_audio_chain_src(struct MPContext *mpctx, struct track *track)
536 {
537     assert(!mpctx->ao_chain);
538 
539     mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL);
540 
541     struct ao_chain *ao_c = talloc_zero(NULL, struct ao_chain);
542     mpctx->ao_chain = ao_c;
543     ao_c->mpctx = mpctx;
544     ao_c->log = mpctx->log;
545     ao_c->filter =
546         mp_output_chain_create(mpctx->filter_root, MP_OUTPUT_CHAIN_AUDIO);
547     ao_c->spdif_passthrough = true;
548     ao_c->last_out_pts = MP_NOPTS_VALUE;
549     ao_c->delay = mpctx->opts->audio_delay;
550 
551     ao_c->ao_filter = mp_filter_create(mpctx->filter_root, &ao_filter);
552     if (!ao_c->filter || !ao_c->ao_filter)
553         goto init_error;
554     ao_c->ao_filter->priv = ao_c;
555 
556     mp_filter_add_pin(ao_c->ao_filter, MP_PIN_IN, "in");
557     mp_pin_connect(ao_c->ao_filter->pins[0], ao_c->filter->f->pins[1]);
558 
559     if (track) {
560         ao_c->track = track;
561         track->ao_c = ao_c;
562         if (!init_audio_decoder(mpctx, track))
563             goto init_error;
564         ao_c->dec_src = track->dec->f->pins[0];
565         mp_pin_connect(ao_c->filter->f->pins[0], ao_c->dec_src);
566     }
567 
568     reset_audio_state(mpctx);
569 
570     if (recreate_audio_filters(mpctx) < 0)
571         goto init_error;
572 
573     if (mpctx->ao)
574         audio_update_volume(mpctx);
575 
576     mp_wakeup_core(mpctx);
577     return;
578 
579 init_error:
580     uninit_audio_chain(mpctx);
581     uninit_audio_out(mpctx);
582     error_on_track(mpctx, track);
583 }
584 
585 // Return pts value corresponding to the start point of audio written to the
586 // ao queue so far.
written_audio_pts(struct MPContext * mpctx)587 double written_audio_pts(struct MPContext *mpctx)
588 {
589     return mpctx->ao_chain ? mpctx->ao_chain->last_out_pts : MP_NOPTS_VALUE;
590 }
591 
592 // Return pts value corresponding to currently playing audio.
playing_audio_pts(struct MPContext * mpctx)593 double playing_audio_pts(struct MPContext *mpctx)
594 {
595     double pts = written_audio_pts(mpctx);
596     if (pts == MP_NOPTS_VALUE || !mpctx->ao)
597         return pts;
598     return pts - mpctx->audio_speed * ao_get_delay(mpctx->ao);
599 }
600 
601 // This garbage is needed for untimed AOs. These consume audio infinitely fast,
602 // so try keeping approximate A/V sync by blocking audio transfer as needed.
update_throttle(struct MPContext * mpctx)603 static void update_throttle(struct MPContext *mpctx)
604 {
605     struct ao_chain *ao_c = mpctx->ao_chain;
606     bool new_throttle = mpctx->audio_status == STATUS_PLAYING &&
607                         mpctx->delay > 0 && ao_c && ao_c->ao &&
608                         ao_untimed(ao_c->ao) &&
609                         mpctx->video_status != STATUS_EOF;
610     if (ao_c && new_throttle != ao_c->untimed_throttle) {
611         ao_c->untimed_throttle = new_throttle;
612         mp_wakeup_core(mpctx);
613         mp_filter_wakeup(ao_c->ao_filter);
614     }
615 }
616 
ao_process(struct mp_filter * f)617 static void ao_process(struct mp_filter *f)
618 {
619     struct ao_chain *ao_c = f->priv;
620     struct MPContext *mpctx = ao_c->mpctx;
621 
622     if (!ao_c->queue_filter) {
623         // This will eventually lead to the creation of the AO + queue, due
624         // to how f_output_chain and AO management works.
625         mp_pin_out_request_data(f->ppins[0]);
626         // Check for EOF with no data case, which is a mess because everything
627         // hates us.
628         struct mp_frame frame = mp_pin_out_read(f->ppins[0]);
629         if (frame.type == MP_FRAME_EOF) {
630             MP_VERBOSE(mpctx, "got EOF with no data before it\n");
631             ao_c->out_eof = true;
632             mpctx->audio_status = STATUS_DRAINING;
633             mp_wakeup_core(mpctx);
634         } else if (frame.type) {
635             mp_pin_out_unread(f->ppins[0], frame);
636         }
637         return;
638     }
639 
640     // Due to mp_async_queue_set_notifier() thhis function is called when the
641     // queue becomes full. This affects state changes in the normal playloop,
642     // so wake it up. But avoid redundant wakeups during normal playback.
643     if (mpctx->audio_status != STATUS_PLAYING &&
644         mp_async_queue_is_full(ao_c->ao_queue))
645         mp_wakeup_core(mpctx);
646 
647     if (mpctx->audio_status == STATUS_SYNCING && !ao_c->start_pts_known)
648         return;
649 
650     if (ao_c->untimed_throttle)
651         return;
652 
653     if (!mp_pin_can_transfer_data(ao_c->queue_filter->pins[0], f->ppins[0]))
654         return;
655 
656     struct mp_frame frame = mp_pin_out_read(f->ppins[0]);
657     if (frame.type == MP_FRAME_AUDIO) {
658         struct mp_aframe *af = frame.data;
659 
660         double endpts = get_play_end_pts(mpctx);
661         if (endpts != MP_NOPTS_VALUE) {
662             endpts *= mpctx->play_dir;
663             // Avoid decoding and discarding the entire rest of the file.
664             if (mp_aframe_get_pts(af) >= endpts) {
665                 mp_pin_out_unread(f->ppins[0], frame);
666                 if (!ao_c->out_eof) {
667                     ao_c->out_eof = true;
668                     mp_pin_in_write(ao_c->queue_filter->pins[0], MP_EOF_FRAME);
669                 }
670                 return;
671             }
672         }
673         double startpts = mpctx->audio_status == STATUS_SYNCING ?
674                                             ao_c->start_pts : MP_NOPTS_VALUE;
675         mp_aframe_clip_timestamps(af, startpts, endpts);
676 
677         int samples = mp_aframe_get_size(af);
678         if (!samples) {
679             mp_filter_internal_mark_progress(f);
680             mp_frame_unref(&frame);
681             return;
682         }
683 
684         ao_c->out_eof = false;
685 
686         if (mpctx->audio_status == STATUS_DRAINING ||
687             mpctx->audio_status == STATUS_EOF)
688         {
689             // If a new frame comes decoder/filter EOF, we should preferably
690             // call get_sync_pts() again, which (at least in obscure situations)
691             // may require us to wait a while until the sync PTS is known. Our
692             // code sucks and can't deal with that, so jump through a hoop to
693             // get things done in the correct order.
694             mp_pin_out_unread(f->ppins[0], frame);
695             ao_c->start_pts_known = false;
696             mpctx->audio_status = STATUS_SYNCING;
697             mp_wakeup_core(mpctx);
698             MP_VERBOSE(mpctx, "new audio frame after EOF\n");
699             return;
700         }
701 
702         mpctx->shown_aframes += samples;
703         double real_samplerate = mp_aframe_get_rate(af) / mpctx->audio_speed;
704         mpctx->delay += samples / real_samplerate;
705         ao_c->last_out_pts = mp_aframe_end_pts(af);
706         update_throttle(mpctx);
707 
708         // Gapless case: the AO is still playing from previous file. It makes
709         // no sense to wait, and in fact the "full queue" event we're waiting
710         // for may never happen, so start immediately.
711         // If the new audio starts "later" (big video sync offset), transfer
712         // of data is stopped somewhere else.
713         if (mpctx->audio_status == STATUS_SYNCING && ao_is_playing(ao_c->ao)) {
714             mpctx->audio_status = STATUS_READY;
715             mp_wakeup_core(mpctx);
716             MP_VERBOSE(mpctx, "previous audio still playing; continuing\n");
717         }
718 
719         mp_pin_in_write(ao_c->queue_filter->pins[0], frame);
720     } else if (frame.type == MP_FRAME_EOF) {
721         MP_VERBOSE(mpctx, "audio filter EOF\n");
722 
723         ao_c->out_eof = true;
724         mp_wakeup_core(mpctx);
725 
726         mp_pin_in_write(ao_c->queue_filter->pins[0], frame);
727         mp_filter_internal_mark_progress(f);
728     } else {
729         mp_frame_unref(&frame);
730     }
731 }
732 
reload_audio_output(struct MPContext * mpctx)733 void reload_audio_output(struct MPContext *mpctx)
734 {
735     if (!mpctx->ao)
736         return;
737 
738     ao_reset(mpctx->ao);
739     uninit_audio_out(mpctx);
740     reinit_audio_filters(mpctx); // mostly to issue refresh seek
741 
742     struct ao_chain *ao_c = mpctx->ao_chain;
743 
744     if (ao_c) {
745         reset_audio_state(mpctx);
746         mp_output_chain_reset_harder(ao_c->filter);
747     }
748 
749     // Whether we can use spdif might have changed. If we failed to use spdif
750     // in the previous initialization, try it with spdif again (we'll fallback
751     // to PCM again if necessary).
752     if (ao_c && ao_c->track) {
753         struct mp_decoder_wrapper *dec = ao_c->track->dec;
754         if (dec && ao_c->spdif_failed) {
755             ao_c->spdif_passthrough = true;
756             ao_c->spdif_failed = false;
757             mp_decoder_wrapper_set_spdif_flag(ao_c->track->dec, true);
758             if (!mp_decoder_wrapper_reinit(dec)) {
759                 MP_ERR(mpctx, "Error reinitializing audio.\n");
760                 error_on_track(mpctx, ao_c->track);
761             }
762         }
763     }
764 
765     mp_wakeup_core(mpctx);
766 }
767 
768 // Returns audio start pts for seeking or video sync.
769 // Returns false if PTS is not known yet.
get_sync_pts(struct MPContext * mpctx,double * pts)770 static bool get_sync_pts(struct MPContext *mpctx, double *pts)
771 {
772     struct MPOpts *opts = mpctx->opts;
773 
774     *pts = MP_NOPTS_VALUE;
775 
776     if (!opts->initial_audio_sync)
777         return true;
778 
779     bool sync_to_video = mpctx->vo_chain && mpctx->video_status != STATUS_EOF &&
780                          !mpctx->vo_chain->is_sparse;
781 
782     if (sync_to_video) {
783         if (mpctx->video_status < STATUS_READY)
784             return false; // wait until we know a video PTS
785         if (mpctx->video_pts != MP_NOPTS_VALUE)
786             *pts = mpctx->video_pts - opts->audio_delay;
787     } else if (mpctx->hrseek_active) {
788         *pts = mpctx->hrseek_pts;
789     } else {
790         // If audio-only is enabled mid-stream during playback, sync accordingly.
791         *pts = mpctx->playback_pts;
792     }
793 
794     return true;
795 }
796 
797 // Look whether audio can be started yet - if audio has to start some time
798 // after video.
799 // Caller needs to ensure mpctx->restart_complete is OK
audio_start_ao(struct MPContext * mpctx)800 void audio_start_ao(struct MPContext *mpctx)
801 {
802     struct ao_chain *ao_c = mpctx->ao_chain;
803     if (!ao_c || !ao_c->ao || mpctx->audio_status != STATUS_READY)
804         return;
805     double pts = MP_NOPTS_VALUE;
806     if (!get_sync_pts(mpctx, &pts))
807         return;
808     double apts = playing_audio_pts(mpctx); // (basically including mpctx->delay)
809     if (pts != MP_NOPTS_VALUE && apts != MP_NOPTS_VALUE && pts < apts &&
810         mpctx->video_status != STATUS_EOF)
811     {
812         double diff = (apts - pts) / mpctx->opts->playback_speed;
813         if (!get_internal_paused(mpctx))
814             mp_set_timeout(mpctx, diff);
815         if (mpctx->logged_async_diff != diff) {
816             MP_VERBOSE(mpctx, "delaying audio start %f vs. %f, diff=%f\n",
817                        apts, pts, diff);
818             mpctx->logged_async_diff = diff;
819         }
820         return;
821     }
822 
823     MP_VERBOSE(mpctx, "starting audio playback\n");
824     ao_start(ao_c->ao);
825     mpctx->audio_status = STATUS_PLAYING;
826     if (ao_c->out_eof) {
827         mpctx->audio_status = STATUS_DRAINING;
828         MP_VERBOSE(mpctx, "audio draining\n");
829     }
830     ao_c->underrun = false;
831     mpctx->logged_async_diff = -1;
832     mp_wakeup_core(mpctx);
833 }
834 
fill_audio_out_buffers(struct MPContext * mpctx)835 void fill_audio_out_buffers(struct MPContext *mpctx)
836 {
837     struct MPOpts *opts = mpctx->opts;
838 
839     if (mpctx->ao && ao_query_and_reset_events(mpctx->ao, AO_EVENT_RELOAD))
840         reload_audio_output(mpctx);
841 
842     if (mpctx->ao && ao_query_and_reset_events(mpctx->ao,
843                                                AO_EVENT_INITIAL_UNBLOCK))
844         ao_unblock(mpctx->ao);
845 
846     update_throttle(mpctx);
847 
848     struct ao_chain *ao_c = mpctx->ao_chain;
849     if (!ao_c)
850         return;
851 
852     if (ao_c->filter->failed_output_conversion) {
853         error_on_track(mpctx, ao_c->track);
854         return;
855     }
856 
857     if (ao_c->filter->ao_needs_update) {
858         if (reinit_audio_filters_and_output(mpctx) < 0)
859             return;
860     }
861 
862     if (mpctx->vo_chain && ao_c->track && ao_c->track->dec &&
863         mp_decoder_wrapper_get_pts_reset(ao_c->track->dec))
864     {
865         MP_WARN(mpctx, "Reset playback due to audio timestamp reset.\n");
866         reset_playback_state(mpctx);
867         mp_wakeup_core(mpctx);
868     }
869 
870     if (mpctx->audio_status == STATUS_SYNCING) {
871         double pts;
872         bool ok = get_sync_pts(mpctx, &pts);
873 
874         // If the AO is still playing from the previous file (due to gapless),
875         // but if video is active, this may not work if audio starts later than
876         // video, and gapless has no advantages anyway. So block doing anything
877         // until the old audio is fully played.
878         // (Buggy if AO underruns.)
879         if (mpctx->ao && ao_is_playing(mpctx->ao) &&
880             mpctx->video_status != STATUS_EOF) {
881             MP_VERBOSE(mpctx, "blocked, waiting for old audio to play\n");
882             ok = false;
883         }
884 
885         if (ao_c->start_pts_known != ok || ao_c->start_pts != pts) {
886             ao_c->start_pts_known = ok;
887             ao_c->start_pts = pts;
888             mp_filter_wakeup(ao_c->ao_filter);
889         }
890 
891         if (ao_c->ao && mp_async_queue_is_full(ao_c->ao_queue)) {
892             mpctx->audio_status = STATUS_READY;
893             mp_wakeup_core(mpctx);
894             MP_VERBOSE(mpctx, "audio ready\n");
895         } else if (ao_c->out_eof) {
896             // Force playback start early.
897             mpctx->audio_status = STATUS_READY;
898             mp_wakeup_core(mpctx);
899             MP_VERBOSE(mpctx, "audio ready (and EOF)\n");
900         }
901     }
902 
903     if (ao_c->ao && !ao_is_playing(ao_c->ao) && !ao_c->underrun &&
904         (mpctx->audio_status == STATUS_PLAYING ||
905          mpctx->audio_status == STATUS_DRAINING))
906     {
907         // Should be playing, but somehow isn't.
908 
909         if (ao_c->out_eof && !mp_async_queue_get_frames(ao_c->ao_queue)) {
910             MP_VERBOSE(mpctx, "AO signaled EOF (while in state %s)\n",
911                        mp_status_str(mpctx->audio_status));
912             mpctx->audio_status = STATUS_EOF;
913             mp_wakeup_core(mpctx);
914             // stops untimed AOs, stops pull AOs from streaming silence
915             ao_reset(ao_c->ao);
916         } else {
917             if (!ao_c->ao_underrun) {
918                 MP_WARN(mpctx, "Audio device underrun detected.\n");
919                 ao_c->ao_underrun = true;
920                 mp_wakeup_core(mpctx);
921                 ao_c->underrun = true;
922             }
923 
924             // Wait until buffers are filled before recovering underrun.
925             if (ao_c->out_eof || mp_async_queue_is_full(ao_c->ao_queue)) {
926                 MP_VERBOSE(mpctx, "restarting audio after underrun\n");
927                 ao_start(mpctx->ao_chain->ao);
928                 ao_c->ao_underrun = false;
929                 ao_c->underrun = false;
930                 mp_wakeup_core(mpctx);
931             }
932         }
933     }
934 
935     if (mpctx->audio_status == STATUS_PLAYING && ao_c->out_eof) {
936         mpctx->audio_status = STATUS_DRAINING;
937         MP_VERBOSE(mpctx, "audio draining\n");
938         mp_wakeup_core(mpctx);
939     }
940 
941     if (mpctx->audio_status == STATUS_DRAINING) {
942         // Wait until the AO has played all queued data. In the gapless case,
943         // we trigger EOF immediately, and let it play asynchronously.
944         if (!ao_c->ao || (!ao_is_playing(ao_c->ao) ||
945                           (opts->gapless_audio && !ao_untimed(ao_c->ao))))
946         {
947             MP_VERBOSE(mpctx, "audio EOF reached\n");
948             mpctx->audio_status = STATUS_EOF;
949             mp_wakeup_core(mpctx);
950         }
951     }
952 
953     if (mpctx->restart_complete)
954         audio_start_ao(mpctx); // in case it got delayed
955 }
956 
957 // Drop data queued for output, or which the AO is currently outputting.
clear_audio_output_buffers(struct MPContext * mpctx)958 void clear_audio_output_buffers(struct MPContext *mpctx)
959 {
960     if (mpctx->ao)
961         ao_reset(mpctx->ao);
962 }
963