static void set_waveformat(WAVEFORMATEXTENSIBLE *wformat, int format, WORD valid_bits, DWORD samplerate, struct mp_chmap *channels) { wformat->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; wformat->Format.nChannels = channels->num; wformat->Format.nSamplesPerSec = samplerate; wformat->Format.wBitsPerSample = af_fmt_to_bytes(format) * 8; wformat->Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX); wformat->SubFormat = *format_to_subtype(format); wformat->Samples.wValidBitsPerSample = valid_bits ? valid_bits : wformat->Format.wBitsPerSample; wformat->dwChannelMask = mp_chmap_to_waveext(channels); update_waveformat_datarate(wformat); }
static int init(struct ao *ao) { float position[3] = {0, 0, 0}; float direction[6] = {0, 0, -1, 0, 1, 0}; ALCdevice *dev = NULL; ALCcontext *ctx = NULL; ALCint freq = 0; ALCint attribs[] = {ALC_FREQUENCY, ao->samplerate, 0, 0}; int i; struct priv *p = ao->priv; if (ao_data) { MP_FATAL(ao, "Not reentrant!\n"); return -1; } ao_data = ao; struct mp_chmap_sel sel = {0}; for (i = 0; speaker_pos[i].id != -1; i++) mp_chmap_sel_add_speaker(&sel, speaker_pos[i].id); if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) goto err_out; struct speaker speakers[MAX_CHANS]; for (i = 0; i < ao->channels.num; i++) { speakers[i].id = -1; for (int n = 0; speaker_pos[n].id >= 0; n++) { if (speaker_pos[n].id == ao->channels.speaker[i]) speakers[i] = speaker_pos[n]; } if (speakers[i].id < 0) { MP_FATAL(ao, "Unknown channel layout\n"); goto err_out; } } char *dev_name = ao->device; dev = alcOpenDevice(dev_name && dev_name[0] ? dev_name : NULL); if (!dev) { MP_FATAL(ao, "could not open device\n"); goto err_out; } ctx = alcCreateContext(dev, attribs); alcMakeContextCurrent(ctx); alListenerfv(AL_POSITION, position); alListenerfv(AL_ORIENTATION, direction); alGenSources(ao->channels.num, sources); for (i = 0; i < ao->channels.num; i++) { cur_buf[i] = 0; unqueue_buf[i] = 0; alGenBuffers(NUM_BUF, buffers[i]); alSourcefv(sources[i], AL_POSITION, speakers[i].pos); alSource3f(sources[i], AL_VELOCITY, 0, 0, 0); } alcGetIntegerv(dev, ALC_FREQUENCY, 1, &freq); if (alcGetError(dev) == ALC_NO_ERROR && freq) ao->samplerate = freq; p->al_format = AL_FALSE; int try_formats[AF_FORMAT_COUNT]; af_get_best_sample_formats(ao->format, try_formats); for (int n = 0; try_formats[n]; n++) { p->al_format = get_al_format(try_formats[n]); if (p->al_format != AL_FALSE) { ao->format = try_formats[n]; break; } } if (p->al_format == AL_FALSE) { MP_FATAL(ao, "Can't find appropriate sample format.\n"); uninit(ao); goto err_out; } p->chunk_size = CHUNK_SAMPLES * af_fmt_to_bytes(ao->format); return 0; err_out: ao_data = NULL; return -1; }
// open & setup audio device static int init(struct ao *ao) { struct priv *ac = talloc_zero(ao, struct priv); AVCodec *codec; ao->priv = ac; if (!encode_lavc_available(ao->encode_lavc_ctx)) { MP_ERR(ao, "the option --o (output file) must be specified\n"); return -1; } pthread_mutex_lock(&ao->encode_lavc_ctx->lock); if (encode_lavc_alloc_stream(ao->encode_lavc_ctx, AVMEDIA_TYPE_AUDIO, &ac->stream, &ac->codec) < 0) { MP_ERR(ao, "could not get a new audio stream\n"); goto fail; } codec = ao->encode_lavc_ctx->ac; int samplerate = af_select_best_samplerate(ao->samplerate, codec->supported_samplerates); if (samplerate > 0) ao->samplerate = samplerate; // TODO: Remove this redundancy with encode_lavc_alloc_stream also // setting the time base. // Using codec->time_bvase is deprecated, but needed for older lavf. ac->stream->time_base.num = 1; ac->stream->time_base.den = ao->samplerate; ac->codec->time_base.num = 1; ac->codec->time_base.den = ao->samplerate; ac->codec->sample_rate = ao->samplerate; struct mp_chmap_sel sel = {0}; mp_chmap_sel_add_any(&sel); if (!ao_chmap_sel_adjust2(ao, &sel, &ao->channels, false)) goto fail; mp_chmap_reorder_to_lavc(&ao->channels); ac->codec->channels = ao->channels.num; ac->codec->channel_layout = mp_chmap_to_lavc(&ao->channels); ac->codec->sample_fmt = AV_SAMPLE_FMT_NONE; select_format(ao, codec); ac->sample_size = af_fmt_to_bytes(ao->format); ac->codec->sample_fmt = af_to_avformat(ao->format); ac->codec->bits_per_raw_sample = ac->sample_size * 8; if (encode_lavc_open_codec(ao->encode_lavc_ctx, ac->codec) < 0) goto fail; ac->pcmhack = 0; if (ac->codec->frame_size <= 1) ac->pcmhack = av_get_bits_per_sample(ac->codec->codec_id) / 8; if (ac->pcmhack) ac->aframesize = 16384; // "enough" else ac->aframesize = ac->codec->frame_size; // enough frames for at least 0.25 seconds ac->framecount = ceil(ao->samplerate * 0.25 / ac->aframesize); // but at least one! ac->framecount = FFMAX(ac->framecount, 1); ac->savepts = AV_NOPTS_VALUE; ac->lastpts = AV_NOPTS_VALUE; ao->untimed = true; if (ao->channels.num > AV_NUM_DATA_POINTERS) goto fail; pthread_mutex_unlock(&ao->encode_lavc_ctx->lock); return 0; fail: pthread_mutex_unlock(&ao->encode_lavc_ctx->lock); ac->shutdown = true; return -1; }