void reinit_audio_chain(struct MPContext *mpctx) { struct MPOpts *opts = mpctx->opts; struct track *track = mpctx->current_track[0][STREAM_AUDIO]; struct sh_stream *sh = track ? track->stream : NULL; if (!sh) { uninit_player(mpctx, INITIALIZED_AO); goto no_audio; } mp_notify(mpctx, MPV_EVENT_AUDIO_RECONFIG, NULL); if (!(mpctx->initialized_flags & INITIALIZED_ACODEC)) { mpctx->initialized_flags |= INITIALIZED_ACODEC; assert(!mpctx->d_audio); mpctx->d_audio = talloc_zero(NULL, struct dec_audio); mpctx->d_audio->log = mp_log_new(mpctx->d_audio, mpctx->log, "!ad"); mpctx->d_audio->global = mpctx->global; mpctx->d_audio->opts = opts; mpctx->d_audio->header = sh; mpctx->d_audio->afilter = af_new(mpctx->global); mpctx->d_audio->afilter->replaygain_data = sh->audio->replaygain_data; mpctx->ao_buffer = mp_audio_buffer_create(NULL); if (!audio_init_best_codec(mpctx->d_audio, opts->audio_decoders)) goto init_error; reset_audio_state(mpctx); if (mpctx->ao) { struct mp_audio fmt; ao_get_format(mpctx->ao, &fmt); mp_audio_buffer_reinit(mpctx->ao_buffer, &fmt); } }
// Drop audio buffer and reinit it (after format change) // Returns whether the format was valid at all. static bool reinit_audio_buffer(struct dec_audio *da) { if (!mp_audio_config_valid(&da->decoded)) { MP_ERR(da, "Audio decoder did not specify audio " "format, or requested an unsupported configuration!\n"); return false; } mp_audio_buffer_reinit(da->decode_buffer, &da->decoded); return true; }
// Drop audio buffer and reinit it (after format change) // Returns whether the format was valid at all. static bool reinit_audio_buffer(struct dec_audio *da) { if (!mp_audio_config_valid(&da->decoded)) { mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Audio decoder did not specify audio " "format, or requested an unsupported configuration!\n"); return false; } mp_audio_buffer_reinit(da->decode_buffer, &da->decoded); mp_audio_buffer_preallocate_min(da->decode_buffer, DECODE_BUFFER_SAMPLES); return true; }
// Initialization and runtime control static int control(struct af_instance *af, int cmd, void *arg) { af_ac3enc_t *s = af->priv; static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \ {0, 96000, 192000, 256000, 384000, 448000, 448000}; switch (cmd){ case AF_CONTROL_REINIT: { struct mp_audio *in = arg; struct mp_audio orig_in = *in; if (AF_FORMAT_IS_SPECIAL(in->format) || in->nch < s->cfg_min_channel_num) return AF_DETACH; mp_audio_set_format(in, s->in_sampleformat); if (in->rate != 48000 && in->rate != 44100 && in->rate != 32000) in->rate = 48000; af->data->rate = in->rate; mp_chmap_reorder_to_lavc(&in->channels); if (in->nch > AC3_MAX_CHANNELS) mp_audio_set_num_channels(in, AC3_MAX_CHANNELS); mp_audio_set_format(af->data, AF_FORMAT_AC3_BE); mp_audio_set_num_channels(af->data, 2); if (!mp_audio_config_equals(in, &orig_in)) return AF_FALSE; s->in_samples = AC3_FRAME_SIZE; if (s->cfg_add_iec61937_header) { s->out_samples = AC3_FRAME_SIZE; } else { s->out_samples = AC3_MAX_CODED_FRAME_SIZE / af->data->sstride; } af->mul = s->out_samples / (double)s->in_samples; mp_audio_buffer_reinit(s->pending, in); MP_DBG(af, "af_lavcac3enc reinit: %d, %d, %f, %d.\n", in->nch, in->rate, af->mul, s->in_samples); int bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[in->nch]; if (s->lavc_actx->channels != in->nch || s->lavc_actx->sample_rate != in->rate || s->lavc_actx->bit_rate != bit_rate) { avcodec_close(s->lavc_actx); // Put sample parameters s->lavc_actx->channels = in->nch; s->lavc_actx->channel_layout = mp_chmap_to_lavc(&in->channels); s->lavc_actx->sample_rate = in->rate; s->lavc_actx->bit_rate = bit_rate; if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) { MP_ERR(af, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate); return AF_ERROR; } } if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) { MP_ERR(af, "lavcac3enc: unexpected ac3 " "encoder frame size %d\n", s->lavc_actx->frame_size); return AF_ERROR; } return AF_OK; } } return AF_UNKNOWN; }
// Drop audio buffer and reinit it (after format change) static void reinit_audio_buffer(struct dec_audio *da) { mp_audio_buffer_reinit(da->decode_buffer, &da->decoded); mp_audio_buffer_preallocate_min(da->decode_buffer, DECODE_BUFFER_SAMPLES); }