static int setup_format(sh_audio_t *sh_audio, const AVCodecContext *lavc_context) { int broken_srate = 0; int samplerate = lavc_context->sample_rate; int sample_format = samplefmt2affmt(lavc_context->sample_fmt); if (!sample_format) sample_format = sh_audio->sample_format; if(sh_audio->wf){ // If the decoder uses the wrong number of channels all is lost anyway. // sh_audio->channels=sh_audio->wf->nChannels; if (lavc_context->codec_id == CODEC_ID_AAC && samplerate == 2*sh_audio->wf->nSamplesPerSec) { broken_srate = 1; } else if (sh_audio->wf->nSamplesPerSec) samplerate=sh_audio->wf->nSamplesPerSec; } if (lavc_context->channels != sh_audio->channels || samplerate != sh_audio->samplerate || sample_format != sh_audio->sample_format) { sh_audio->channels=lavc_context->channels; sh_audio->samplerate=samplerate; sh_audio->sample_format = sample_format; sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8; if (broken_srate) mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Ignoring broken container sample rate for AAC with SBR\n"); return 1; } return 0; }
void ca_fill_asbd(struct ao *ao, AudioStreamBasicDescription *asbd) { asbd->mSampleRate = ao->samplerate; // Set "AC3" for other spdif formats too - unknown if that works. asbd->mFormatID = AF_FORMAT_IS_IEC61937(ao->format) ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; asbd->mChannelsPerFrame = ao->channels.num; asbd->mBitsPerChannel = af_fmt2bits(ao->format); asbd->mFormatFlags = kAudioFormatFlagIsPacked; if ((ao->format & AF_FORMAT_TYPE_MASK) == AF_FORMAT_F) asbd->mFormatFlags |= kAudioFormatFlagIsFloat; if ((ao->format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_SI) asbd->mFormatFlags |= kAudioFormatFlagIsSignedInteger; if (BYTE_ORDER == BIG_ENDIAN) asbd->mFormatFlags |= kAudioFormatFlagIsBigEndian; asbd->mFramesPerPacket = 1; asbd->mBytesPerPacket = asbd->mBytesPerFrame = asbd->mFramesPerPacket * asbd->mChannelsPerFrame * (asbd->mBitsPerChannel / 8); }
static demuxer_t* demux_rawaudio_open(demuxer_t* demuxer) { sh_audio_t* sh_audio; WAVEFORMATEX* w; if ((format & AF_FORMAT_SPECIAL_MASK) != 0) return NULL; sh_audio = new_sh_audio(demuxer,0); sh_audio->gsh->codec = "mp-pcm"; sh_audio->format = format; sh_audio->wf = w = malloc(sizeof(*w)); w->wFormatTag = 0; w->nChannels = sh_audio->channels = channels; w->nSamplesPerSec = sh_audio->samplerate = samplerate; int samplesize = (af_fmt2bits(format) + 7) / 8; w->nAvgBytesPerSec = samplerate * samplesize * channels; w->nBlockAlign = channels * samplesize; w->wBitsPerSample = 8 * samplesize; w->cbSize = 0; demuxer->movi_start = demuxer->stream->start_pos; demuxer->movi_end = demuxer->stream->end_pos; demuxer->audio->id = 0; demuxer->audio->sh = sh_audio; sh_audio->ds = demuxer->audio; sh_audio->needs_parsing = 1; return demuxer; }
int af_fmt_seconds_to_bytes(int format, float seconds, int channels, int samplerate) { assert(!af_fmt_is_planar(format)); int bps = (af_fmt2bits(format) / 8); int framelen = channels * bps; int bytes = seconds * bps * samplerate; if (bytes % framelen) bytes += framelen - (bytes % framelen); return bytes; }
static int init_vqf_audio_codec(sh_audio_t *sh_audio){ WAVEFORMATEX *in_fmt=sh_audio->wf; vqf_priv_t*priv=sh_audio->context; int ver; mp_msg(MSGT_DECAUDIO, MSGL_INFO, "======= Win32 (TWinVQ) AUDIO Codec init =======\n"); sh_audio->channels=in_fmt->nChannels; sh_audio->samplerate=in_fmt->nSamplesPerSec; sh_audio->sample_format=AF_FORMAT_S16_NE; // sh_audio->sample_format=AF_FORMAT_FLOAT_NE; sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/8; priv->o_wf.nChannels=in_fmt->nChannels; priv->o_wf.nSamplesPerSec=in_fmt->nSamplesPerSec; priv->o_wf.nBlockAlign=sh_audio->samplesize*in_fmt->nChannels; priv->o_wf.nAvgBytesPerSec=in_fmt->nBlockAlign*in_fmt->nChannels; priv->o_wf.wFormatTag=0x01; priv->o_wf.wBitsPerSample=in_fmt->wBitsPerSample; priv->o_wf.cbSize=0; if( mp_msg_test(MSGT_DECAUDIO,MSGL_V) ) { mp_msg(MSGT_DECAUDIO, MSGL_V, "Input format:\n"); print_wave_header(in_fmt, MSGL_V); mp_msg(MSGT_DECAUDIO, MSGL_V, "Output fmt:\n"); print_wave_header(&priv->o_wf, MSGL_V); } memcpy(&priv->hi,&in_fmt[1],sizeof(headerInfo)); if((ver=TvqInitialize(&priv->hi,&priv->index,0))){ const char *tvqe[]={ "No errors", "General error", "Wrong version", "Channel setting error", "Wrong coding mode", "Inner parameter setting error", "Wrong number of VQ pre-selection candidates, used only in encoder" }; mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Tvq initialization error: %s\n",ver>=0&&ver<7?tvqe[ver]:"Unknown"); return 0; } ver=TvqCheckVersion(priv->hi.ID); if(ver==TVQ_UNKNOWN_VERSION){ mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Tvq unknown version of stream\n" ); return 0; } TvqGetConfInfo(&priv->cf); TvqGetVectorInfo(priv->bits_0,priv->bits_1); priv->framesize=TvqGetFrameSize(); sh_audio->audio_in_minsize=priv->framesize*in_fmt->nChannels; sh_audio->a_in_buffer_size=4*sh_audio->audio_in_minsize; sh_audio->a_in_buffer=av_malloc(sh_audio->a_in_buffer_size); sh_audio->a_in_buffer_len=0; return 1; }
/* Convert format to str input str is a buffer for the converted string, size is the size of the buffer */ char* af_fmt2str(int format, char* str, int size) { int i=0; if (size < 1) return NULL; size--; // reserve one for terminating 0 // Endianness if(AF_FORMAT_LE == (format & AF_FORMAT_END_MASK)) i+=snprintf(str,size-i,"little-endian "); else i+=snprintf(str,size-i,"big-endian "); if(format & AF_FORMAT_SPECIAL_MASK) { switch(format & AF_FORMAT_SPECIAL_MASK) { case(AF_FORMAT_MU_LAW): i+=snprintf(&str[i],size-i,"mu-law "); break; case(AF_FORMAT_A_LAW): i+=snprintf(&str[i],size-i,"A-law "); break; case(AF_FORMAT_MPEG2): i+=snprintf(&str[i],size-i,"MPEG-2 "); break; case(AF_FORMAT_AC3): i+=snprintf(&str[i],size-i,"AC3 "); break; case(AF_FORMAT_IMA_ADPCM): i+=snprintf(&str[i],size-i,"IMA-ADPCM "); break; default: i+=snprintf(&str[i],size-i,"unknown format "); } } else { // Bits i+=snprintf(&str[i],size-i,"%d-bit ", af_fmt2bits(format)); // Point if(AF_FORMAT_F == (format & AF_FORMAT_POINT_MASK)) { i+=snprintf(&str[i],size-i,"float "); } else { // Sign if(AF_FORMAT_US == (format & AF_FORMAT_SIGN_MASK)) i+=snprintf(&str[i],size-i,"unsigned "); else i+=snprintf(&str[i],size-i,"signed "); i+=snprintf(&str[i],size-i,"int "); } } // remove trailing space if (i > 0 && str[i - 1] == ' ') i--; str[i] = 0; // make sure it is 0 terminated. return str; }
static int af_open(af_instance_t* af){ af_ac3enc_t *s = calloc(1,sizeof(af_ac3enc_t)); af->control=control; af->uninit=uninit; af->play=play; af->mul=1; af->data=calloc(1,sizeof(af_data_t)); af->setup=s; s->lavc_acodec = avcodec_find_encoder_by_name("ac3"); if (!s->lavc_acodec) { mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't find encoder for codec %s.\n", "ac3"); return AF_ERROR; } s->lavc_actx = avcodec_alloc_context3(s->lavc_acodec); if (!s->lavc_actx) { mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, couldn't allocate context!\n"); return AF_ERROR; } const enum AVSampleFormat *fmts = s->lavc_acodec->sample_fmts; for (int i = 0; ; i++) { if (fmts[i] == AV_SAMPLE_FMT_NONE) { mp_msg(MSGT_AFILTER, MSGL_ERR, "Audio LAVC, encoder doesn't " "support expected sample formats!\n"); return AF_ERROR; } else if (fmts[i] == AV_SAMPLE_FMT_S16) { s->in_sampleformat = AF_FORMAT_S16_NE; s->lavc_actx->sample_fmt = fmts[i]; break; } else if (fmts[i] == AV_SAMPLE_FMT_FLT) { s->in_sampleformat = AF_FORMAT_FLOAT_NE; s->lavc_actx->sample_fmt = fmts[i]; break; } } char buf[100]; mp_msg(MSGT_AFILTER, MSGL_V, "[af_lavcac3enc]: in sample format: %s\n", af_fmt2str(s->in_sampleformat, buf, 100)); s->pending_data_size = AF_NCH * AC3_FRAME_SIZE * af_fmt2bits(s->in_sampleformat) / 8; s->pending_data = malloc(s->pending_data_size); return AF_OK; }
/* Prefer playing audio with the samplerate given in container data * if available, but take number the number of channels and sample format * from the codec, since if the codec isn't using the correct values for * those everything breaks anyway. */ static int setup_format(sh_audio_t *sh_audio, const AVCodecContext *lavc_context) { int sample_format = sh_audio->sample_format; switch (lavc_context->sample_fmt) { case AV_SAMPLE_FMT_U8: sample_format = AF_FORMAT_U8; break; case AV_SAMPLE_FMT_S16: sample_format = AF_FORMAT_S16_NE; break; case AV_SAMPLE_FMT_S32: sample_format = AF_FORMAT_S32_NE; break; case AV_SAMPLE_FMT_FLT: sample_format = AF_FORMAT_FLOAT_NE; break; default: mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n"); sample_format = AF_FORMAT_UNKNOWN; } bool broken_srate = false; int samplerate = lavc_context->sample_rate; int container_samplerate = sh_audio->container_out_samplerate; if (!container_samplerate && sh_audio->wf) container_samplerate = sh_audio->wf->nSamplesPerSec; if (lavc_context->codec_id == CODEC_ID_AAC && samplerate == 2 * container_samplerate) broken_srate = true; else if (container_samplerate) samplerate = container_samplerate; if (lavc_context->channels != sh_audio->channels || samplerate != sh_audio->samplerate || sample_format != sh_audio->sample_format) { sh_audio->channels = lavc_context->channels; sh_audio->samplerate = samplerate; sh_audio->sample_format = sample_format; sh_audio->samplesize = af_fmt2bits(sh_audio->sample_format) / 8; if (broken_srate) mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Ignoring broken container sample rate for AAC with SBR\n"); return 1; } return 0; }
static int write_to_ao(struct MPContext *mpctx, void *data, int len, int flags, double pts) { if (mpctx->paused) return 0; struct ao *ao = mpctx->ao; double bps = ao->bps / mpctx->opts->playback_speed; int unitsize = ao->channels.num * af_fmt2bits(ao->format) / 8; ao->pts = pts; int played = ao_play(mpctx->ao, data, len, flags); assert(played <= len); assert(played % unitsize == 0); if (played > 0) { mpctx->shown_aframes += played / unitsize; mpctx->delay += played / bps; // Keep correct pts for remaining data - could be used to flush // remaining buffer when closing ao. ao->pts += played / bps; return played; } return 0; }
// Return pts value corresponding to the end point of audio written to the // ao so far. double written_audio_pts(struct MPContext *mpctx) { sh_audio_t *sh_audio = mpctx->sh_audio; if (!sh_audio) return MP_NOPTS_VALUE; double bps = sh_audio->channels.num * sh_audio->samplerate * (af_fmt2bits(sh_audio->sample_format) / 8); // first calculate the end pts of audio that has been output by decoder double a_pts = sh_audio->pts; if (a_pts == MP_NOPTS_VALUE) return MP_NOPTS_VALUE; // sh_audio->pts is the timestamp of the latest input packet with // known pts that the decoder has decoded. sh_audio->pts_bytes is // the amount of bytes the decoder has written after that timestamp. a_pts += sh_audio->pts_bytes / bps; // Now a_pts hopefully holds the pts for end of audio from decoder. // Subtract data in buffers between decoder and audio out. // Decoded but not filtered a_pts -= sh_audio->a_buffer_len / bps; // Data buffered in audio filters, measured in bytes of "missing" output double buffered_output = af_calc_delay(sh_audio->afilter); // Data that was ready for ao but was buffered because ao didn't fully // accept everything to internal buffers yet buffered_output += mpctx->ao->buffer.len; // Filters divide audio length by playback_speed, so multiply by it // to get the length in original units without speedup or slowdown a_pts -= buffered_output * mpctx->opts->playback_speed / mpctx->ao->bps; return a_pts + mpctx->video_offset; }
/* libmpg123 has a new format ready; query and store, return return value of mpg123_getformat() */ static int set_format(struct dec_audio *da) { struct ad_mpg123_context *con = da->priv; int ret; long rate; int channels; int encoding; ret = mpg123_getformat(con->handle, &rate, &channels, &encoding); if (ret == MPG123_OK) { mp_audio_set_num_channels(&da->decoded, channels); da->decoded.rate = rate; int af = mpg123_format_to_af(encoding); if (!af) { /* This means we got a funny custom build of libmpg123 that only supports an unknown format. */ MP_ERR(da, "Bad encoding from mpg123: %i.\n", encoding); return MPG123_ERR; } mp_audio_set_format(&da->decoded, af); con->sample_size = channels * (af_fmt2bits(af) / 8); con->new_format = 0; } return ret; }
// 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); ac->stream = encode_lavc_alloc_stream(ao->encode_lavc_ctx, AVMEDIA_TYPE_AUDIO); if (!ac->stream) { MP_ERR(ao, "could not get a new audio stream\n"); goto fail; } codec = encode_lavc_get_codec(ao->encode_lavc_ctx, ac->stream); // ac->stream->time_base.num = 1; // ac->stream->time_base.den = ao->samplerate; // doing this breaks mpeg2ts in ffmpeg // which doesn't properly force the time base to be 90000 // furthermore, ffmpeg.c doesn't do this either and works ac->stream->codec->time_base.num = 1; ac->stream->codec->time_base.den = ao->samplerate; ac->stream->codec->sample_rate = ao->samplerate; struct mp_chmap_sel sel = {0}; mp_chmap_sel_add_any(&sel); if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) goto fail; mp_chmap_reorder_to_lavc(&ao->channels); ac->stream->codec->channels = ao->channels.num; ac->stream->codec->channel_layout = mp_chmap_to_lavc(&ao->channels); ac->stream->codec->sample_fmt = AV_SAMPLE_FMT_NONE; select_format(ao, codec); ac->sample_size = af_fmt2bits(ao->format) / 8; ac->stream->codec->sample_fmt = af_to_avformat(ao->format); ac->stream->codec->bits_per_raw_sample = ac->sample_size * 8; if (encode_lavc_open_codec(ao->encode_lavc_ctx, ac->stream) < 0) goto fail; ac->pcmhack = 0; if (ac->stream->codec->frame_size <= 1) ac->pcmhack = av_get_bits_per_sample(ac->stream->codec->codec_id) / 8; if (ac->pcmhack) { ac->aframesize = 16384; // "enough" ac->buffer_size = ac->aframesize * ac->pcmhack * ao->channels.num * 2 + 200; } else { ac->aframesize = ac->stream->codec->frame_size; ac->buffer_size = ac->aframesize * ac->sample_size * ao->channels.num * 2 + 200; } if (ac->buffer_size < FF_MIN_BUFFER_SIZE) ac->buffer_size = FF_MIN_BUFFER_SIZE; ac->buffer = talloc_size(ac, ac->buffer_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 = MP_NOPTS_VALUE; ac->lastpts = MP_NOPTS_VALUE; ao->untimed = true; pthread_mutex_unlock(&ao->encode_lavc_ctx->lock); return 0; fail: pthread_mutex_unlock(&ao->encode_lavc_ctx->lock); return -1; }
/* open & setup audio device return: 1=success 0=fail */ static int init(int rate_hz, int channels, int format, int flags) { unsigned int alsa_buffer_time = 500000; /* 0.5 s */ unsigned int alsa_fragcount = 16; int err; int block; strarg_t device; snd_pcm_uframes_t chunk_size; snd_pcm_uframes_t bufsize; snd_pcm_uframes_t boundary; const opt_t subopts[] = { {"block", OPT_ARG_BOOL, &block, NULL}, {"device", OPT_ARG_STR, &device, str_maxlen}, {NULL} }; char alsa_device[ALSA_DEVICE_SIZE + 1]; // make sure alsa_device is null-terminated even when using strncpy etc. memset(alsa_device, 0, ALSA_DEVICE_SIZE + 1); mp_msg(MSGT_AO,MSGL_V,"alsa-init: requested format: %d Hz, %d channels, %x\n", rate_hz, channels, format); alsa_handler = NULL; #if SND_LIB_VERSION >= 0x010005 mp_msg(MSGT_AO,MSGL_V,"alsa-init: using ALSA %s\n", snd_asoundlib_version()); #else mp_msg(MSGT_AO,MSGL_V,"alsa-init: compiled for ALSA-%s\n", SND_LIB_VERSION_STR); #endif snd_lib_error_set_handler(alsa_error_handler); ao_data.samplerate = rate_hz; ao_data.format = format; ao_data.channels = channels; switch (format) { case AF_FORMAT_S8: alsa_format = SND_PCM_FORMAT_S8; break; case AF_FORMAT_U8: alsa_format = SND_PCM_FORMAT_U8; break; case AF_FORMAT_U16_LE: alsa_format = SND_PCM_FORMAT_U16_LE; break; case AF_FORMAT_U16_BE: alsa_format = SND_PCM_FORMAT_U16_BE; break; case AF_FORMAT_AC3_LE: case AF_FORMAT_S16_LE: case AF_FORMAT_IEC61937_LE: alsa_format = SND_PCM_FORMAT_S16_LE; break; case AF_FORMAT_AC3_BE: case AF_FORMAT_S16_BE: case AF_FORMAT_IEC61937_BE: alsa_format = SND_PCM_FORMAT_S16_BE; break; case AF_FORMAT_U32_LE: alsa_format = SND_PCM_FORMAT_U32_LE; break; case AF_FORMAT_U32_BE: alsa_format = SND_PCM_FORMAT_U32_BE; break; case AF_FORMAT_S32_LE: alsa_format = SND_PCM_FORMAT_S32_LE; break; case AF_FORMAT_S32_BE: alsa_format = SND_PCM_FORMAT_S32_BE; break; case AF_FORMAT_U24_LE: alsa_format = SND_PCM_FORMAT_U24_3LE; break; case AF_FORMAT_U24_BE: alsa_format = SND_PCM_FORMAT_U24_3BE; break; case AF_FORMAT_S24_LE: alsa_format = SND_PCM_FORMAT_S24_3LE; break; case AF_FORMAT_S24_BE: alsa_format = SND_PCM_FORMAT_S24_3BE; break; case AF_FORMAT_FLOAT_LE: alsa_format = SND_PCM_FORMAT_FLOAT_LE; break; case AF_FORMAT_FLOAT_BE: alsa_format = SND_PCM_FORMAT_FLOAT_BE; break; case AF_FORMAT_MU_LAW: alsa_format = SND_PCM_FORMAT_MU_LAW; break; case AF_FORMAT_A_LAW: alsa_format = SND_PCM_FORMAT_A_LAW; break; default: alsa_format = SND_PCM_FORMAT_MPEG; //? default should be -1 break; } //subdevice parsing // set defaults block = 1; /* switch for spdif * sets opening sequence for SPDIF * sets also the playback and other switches 'on the fly' * while opening the abstract alias for the spdif subdevice * 'iec958' */ if (AF_FORMAT_IS_IEC61937(format)) { device.str = "iec958"; mp_msg(MSGT_AO,MSGL_V,"alsa-spdif-init: playing AC3/iec61937/iec958, %i channels\n", channels); } else /* in any case for multichannel playback we should select * appropriate device */ switch (channels) { case 1: case 2: device.str = "default"; mp_msg(MSGT_AO,MSGL_V,"alsa-init: setup for 1/2 channel(s)\n"); break; case 4: if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) // hack - use the converter plugin device.str = "plug:surround40"; else device.str = "surround40"; mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround40\n"); break; case 6: if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) device.str = "plug:surround51"; else device.str = "surround51"; mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround51\n"); break; case 8: if (alsa_format == SND_PCM_FORMAT_FLOAT_LE) device.str = "plug:surround71"; else device.str = "surround71"; mp_msg(MSGT_AO,MSGL_V,"alsa-init: device set to surround71\n"); break; default: device.str = "default"; mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_ChannelsNotSupported,channels); } device.len = strlen(device.str); if (subopt_parse(ao_subdevice, subopts) != 0) { print_help(); return 0; } parse_device(alsa_device, device.str, device.len); mp_msg(MSGT_AO,MSGL_V,"alsa-init: using device %s\n", alsa_device); if (!alsa_handler) { int open_mode = block ? 0 : SND_PCM_NONBLOCK; int isac3 = AF_FORMAT_IS_IEC61937(format); //modes = 0, SND_PCM_NONBLOCK, SND_PCM_ASYNC mp_msg(MSGT_AO,MSGL_V,"alsa-init: opening device in %sblocking mode\n", block ? "" : "non-"); if ((err = try_open_device(alsa_device, open_mode, isac3)) < 0) { if (err != -EBUSY && !block) { mp_msg(MSGT_AO,MSGL_INFO,MSGTR_AO_ALSA_OpenInNonblockModeFailed); if ((err = try_open_device(alsa_device, 0, isac3)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PlaybackOpenError, snd_strerror(err)); return 0; } } else { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_PlaybackOpenError, snd_strerror(err)); return 0; } } if ((err = snd_pcm_nonblock(alsa_handler, 0)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_ErrorSetBlockMode, snd_strerror(err)); } else { mp_msg(MSGT_AO,MSGL_V,"alsa-init: device reopened in blocking mode\n"); } snd_pcm_hw_params_alloca(&alsa_hwparams); snd_pcm_sw_params_alloca(&alsa_swparams); // setting hw-parameters if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetInitialParameters, snd_strerror(err)); return 0; } err = snd_pcm_hw_params_set_access(alsa_handler, alsa_hwparams, SND_PCM_ACCESS_RW_INTERLEAVED); if (err < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetAccessType, snd_strerror(err)); return 0; } /* workaround for nonsupported formats sets default format to S16_LE if the given formats aren't supported */ if ((err = snd_pcm_hw_params_test_format(alsa_handler, alsa_hwparams, alsa_format)) < 0) { mp_msg(MSGT_AO,MSGL_INFO, MSGTR_AO_ALSA_FormatNotSupportedByHardware, af_fmt2str_short(format)); alsa_format = SND_PCM_FORMAT_S16_LE; if (AF_FORMAT_IS_AC3(ao_data.format)) ao_data.format = AF_FORMAT_AC3_LE; else if (AF_FORMAT_IS_IEC61937(ao_data.format)) ao_data.format = AF_FORMAT_IEC61937_LE; else ao_data.format = AF_FORMAT_S16_LE; } if ((err = snd_pcm_hw_params_set_format(alsa_handler, alsa_hwparams, alsa_format)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetFormat, snd_strerror(err)); return 0; } if ((err = snd_pcm_hw_params_set_channels_near(alsa_handler, alsa_hwparams, &ao_data.channels)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetChannels, snd_strerror(err)); return 0; } /* workaround for buggy rate plugin (should be fixed in ALSA 1.0.11) prefer our own resampler, since that allows users to choose the resampler, even per file if desired */ #if SND_LIB_VERSION >= 0x010009 if ((err = snd_pcm_hw_params_set_rate_resample(alsa_handler, alsa_hwparams, 0)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToDisableResampling, snd_strerror(err)); return 0; } #endif if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, &ao_data.samplerate, NULL)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetSamplerate2, snd_strerror(err)); return 0; } bytes_per_sample = af_fmt2bits(ao_data.format) / 8; bytes_per_sample *= ao_data.channels; ao_data.bps = ao_data.samplerate * bytes_per_sample; if ((err = snd_pcm_hw_params_set_buffer_time_near(alsa_handler, alsa_hwparams, &alsa_buffer_time, NULL)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetBufferTimeNear, snd_strerror(err)); return 0; } if ((err = snd_pcm_hw_params_set_periods_near(alsa_handler, alsa_hwparams, &alsa_fragcount, NULL)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetPeriods, snd_strerror(err)); return 0; } /* finally install hardware parameters */ if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetHwParameters, snd_strerror(err)); return 0; } // end setting hw-params // gets buffersize for control if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams, &bufsize)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetBufferSize, snd_strerror(err)); return 0; } else { ao_data.buffersize = bufsize * bytes_per_sample; mp_msg(MSGT_AO,MSGL_V,"alsa-init: got buffersize=%i\n", ao_data.buffersize); } if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, &chunk_size, NULL)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetPeriodSize, snd_strerror(err)); return 0; } else { mp_msg(MSGT_AO,MSGL_V,"alsa-init: got period size %li\n", chunk_size); } ao_data.outburst = chunk_size * bytes_per_sample; /* setting software parameters */ if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetSwParameters, snd_strerror(err)); return 0; } #if SND_LIB_VERSION >= 0x000901 if ((err = snd_pcm_sw_params_get_boundary(alsa_swparams, &boundary)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetBoundary, snd_strerror(err)); return 0; } #else boundary = 0x7fffffff; #endif /* start playing when one period has been written */ if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, chunk_size)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetStartThreshold, snd_strerror(err)); return 0; } /* disable underrun reporting */ if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, boundary)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetStopThreshold, snd_strerror(err)); return 0; } #if SND_LIB_VERSION >= 0x000901 /* play silence when there is an underrun */ if ((err = snd_pcm_sw_params_set_silence_size(alsa_handler, alsa_swparams, boundary)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToSetSilenceSize, snd_strerror(err)); return 0; } #endif if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) { mp_msg(MSGT_AO,MSGL_ERR,MSGTR_AO_ALSA_UnableToGetSwParameters, snd_strerror(err)); return 0; } /* end setting sw-params */ mp_msg(MSGT_AO,MSGL_V,"alsa: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n", ao_data.samplerate, ao_data.channels, (int)bytes_per_sample, ao_data.buffersize, snd_pcm_format_description(alsa_format)); } // end switch alsa_handler (spdif) alsa_can_pause = snd_pcm_hw_params_can_pause(alsa_hwparams); return 1; } // end init
static int init(sh_audio_t *sh_audio) { int tries = 0; int x; AVCodecContext *lavc_context; AVCodec *lavc_codec; mp_msg(MSGT_DECAUDIO,MSGL_V,"FFmpeg's libavcodec audio codec\n"); if(!avcodec_initialized){ avcodec_init(); avcodec_register_all(); avcodec_initialized=1; } lavc_codec = (AVCodec *)avcodec_find_decoder_by_name(sh_audio->codec->dll); if(!lavc_codec){ mp_tmsg(MSGT_DECAUDIO,MSGL_ERR,"Cannot find codec '%s' in libavcodec...\n",sh_audio->codec->dll); return 0; } lavc_context = avcodec_alloc_context(); sh_audio->context=lavc_context; lavc_context->sample_rate = sh_audio->samplerate; lavc_context->bit_rate = sh_audio->i_bps * 8; if(sh_audio->wf){ lavc_context->channels = sh_audio->wf->nChannels; lavc_context->sample_rate = sh_audio->wf->nSamplesPerSec; lavc_context->bit_rate = sh_audio->wf->nAvgBytesPerSec * 8; lavc_context->block_align = sh_audio->wf->nBlockAlign; lavc_context->bits_per_coded_sample = sh_audio->wf->wBitsPerSample; } lavc_context->request_channels = audio_output_channels; lavc_context->codec_tag = sh_audio->format; //FOURCC lavc_context->codec_type = CODEC_TYPE_AUDIO; lavc_context->codec_id = lavc_codec->id; // not sure if required, imho not --A'rpi /* alloc extra data */ if (sh_audio->wf && sh_audio->wf->cbSize > 0) { lavc_context->extradata = av_mallocz(sh_audio->wf->cbSize + FF_INPUT_BUFFER_PADDING_SIZE); lavc_context->extradata_size = sh_audio->wf->cbSize; memcpy(lavc_context->extradata, (char *)sh_audio->wf + sizeof(WAVEFORMATEX), lavc_context->extradata_size); } // for QDM2 if (sh_audio->codecdata_len && sh_audio->codecdata && !lavc_context->extradata) { lavc_context->extradata = av_malloc(sh_audio->codecdata_len); lavc_context->extradata_size = sh_audio->codecdata_len; memcpy(lavc_context->extradata, (char *)sh_audio->codecdata, lavc_context->extradata_size); } /* open it */ if (avcodec_open(lavc_context, lavc_codec) < 0) { mp_tmsg(MSGT_DECAUDIO,MSGL_ERR, "Could not open codec.\n"); return 0; } mp_msg(MSGT_DECAUDIO,MSGL_V,"INFO: libavcodec \"%s\" init OK!\n", lavc_codec->name); // printf("\nFOURCC: 0x%X\n",sh_audio->format); if(sh_audio->format==0x3343414D){ // MACE 3:1 sh_audio->ds->ss_div = 2*3; // 1 samples/packet sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet } else if(sh_audio->format==0x3643414D){ // MACE 6:1 sh_audio->ds->ss_div = 2*6; // 1 samples/packet sh_audio->ds->ss_mul = 2*sh_audio->wf->nChannels; // 1 byte*ch/packet } // Decode at least 1 byte: (to get header filled) do { x=decode_audio(sh_audio,sh_audio->a_buffer,1,sh_audio->a_buffer_size); } while (x <= 0 && tries++ < 5); if(x>0) sh_audio->a_buffer_len=x; sh_audio->channels=lavc_context->channels; sh_audio->samplerate=lavc_context->sample_rate; sh_audio->i_bps=lavc_context->bit_rate/8; switch (lavc_context->sample_fmt) { case SAMPLE_FMT_U8: sh_audio->sample_format = AF_FORMAT_U8; break; case SAMPLE_FMT_S16: sh_audio->sample_format = AF_FORMAT_S16_NE; break; case SAMPLE_FMT_S32: sh_audio->sample_format = AF_FORMAT_S32_NE; break; case SAMPLE_FMT_FLT: sh_audio->sample_format = AF_FORMAT_FLOAT_NE; break; default: mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Unsupported sample format\n"); return 0; } /* If the audio is AAC the container level data may be unreliable * because of SBR handling problems (possibly half real sample rate at * container level). Default AAC decoding with ad_faad has used codec-level * values for a long time without generating complaints so it should be OK. */ if (sh_audio->wf && lavc_context->codec_id != CODEC_ID_AAC) { // If the decoder uses the wrong number of channels all is lost anyway. // sh_audio->channels=sh_audio->wf->nChannels; if (sh_audio->wf->nSamplesPerSec) sh_audio->samplerate=sh_audio->wf->nSamplesPerSec; if (sh_audio->wf->nAvgBytesPerSec) sh_audio->i_bps=sh_audio->wf->nAvgBytesPerSec; } sh_audio->samplesize=af_fmt2bits(sh_audio->sample_format)/ 8; return 1; }
void mp_audio_set_format(struct mp_audio *mpa, int format) { mpa->format = format; mpa->bps = af_fmt2bits(format) / 8; }
int format_2_bps(int format) { return af_fmt2bits(format)/8; }
static int init(struct ao *ao) { struct priv *priv = ao->priv; if (!check_pa_ret(Pa_Initialize())) return -1; pthread_mutex_init(&priv->ring_mutex, NULL); int pa_device = Pa_GetDefaultOutputDevice(); if (priv->cfg_device && priv->cfg_device[0]) pa_device = find_device(priv->cfg_device); if (pa_device == paNoDevice) goto error_exit; // The actual channel order probably depends on the platform. struct mp_chmap_sel sel = {0}; mp_chmap_sel_add_waveext_def(&sel); if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) goto error_exit; PaStreamParameters sp = { .device = pa_device, .channelCount = ao->channels.num, .suggestedLatency = Pa_GetDeviceInfo(pa_device)->defaultHighOutputLatency, }; ao->format = af_fmt_from_planar(ao->format); const struct format_map *fmt = format_maps; while (fmt->pa_format) { if (fmt->mp_format == ao->format) { PaStreamParameters test = sp; test.sampleFormat = fmt->pa_format; if (Pa_IsFormatSupported(NULL, &test, ao->samplerate) == paNoError) break; } fmt++; } if (!fmt->pa_format) { MP_VERBOSE(ao, "Unsupported format, using default.\n"); fmt = format_maps; } ao->format = fmt->mp_format; sp.sampleFormat = fmt->pa_format; priv->framelen = ao->channels.num * (af_fmt2bits(ao->format) / 8); ao->bps = ao->samplerate * priv->framelen; if (!check_pa_ret(Pa_IsFormatSupported(NULL, &sp, ao->samplerate))) goto error_exit; if (!check_pa_ret(Pa_OpenStream(&priv->stream, NULL, &sp, ao->samplerate, paFramesPerBufferUnspecified, paNoFlag, stream_callback, ao))) goto error_exit; priv->ring = mp_ring_new(priv, seconds_to_bytes(ao, 0.5)); return 0; error_exit: uninit(ao, true); return -1; } static int play(struct ao *ao, void **data, int samples, int flags) { struct priv *priv = ao->priv; pthread_mutex_lock(&priv->ring_mutex); int write_len = mp_ring_write(priv->ring, data[0], samples * ao->sstride); if (flags & AOPLAY_FINAL_CHUNK) priv->play_remaining = true; pthread_mutex_unlock(&priv->ring_mutex); if (Pa_IsStreamStopped(priv->stream) == 1) check_pa_ret(Pa_StartStream(priv->stream)); return write_len / ao->sstride; } static int get_space(struct ao *ao) { struct priv *priv = ao->priv; pthread_mutex_lock(&priv->ring_mutex); int free = mp_ring_available(priv->ring); pthread_mutex_unlock(&priv->ring_mutex); return free / ao->sstride; } static float get_delay(struct ao *ao) { struct priv *priv = ao->priv; double stream_time = Pa_GetStreamTime(priv->stream); pthread_mutex_lock(&priv->ring_mutex); float frame_time = priv->play_time ? priv->play_time - stream_time : 0; float buffer_latency = (mp_ring_buffered(priv->ring) + priv->play_silence) / (float)ao->bps; pthread_mutex_unlock(&priv->ring_mutex); return buffer_latency + frame_time; } static void reset(struct ao *ao) { struct priv *priv = ao->priv; if (Pa_IsStreamStopped(priv->stream) != 1) check_pa_ret(Pa_AbortStream(priv->stream)); pthread_mutex_lock(&priv->ring_mutex); mp_ring_reset(priv->ring); priv->play_remaining = false; priv->play_time = 0; priv->play_silence = 0; pthread_mutex_unlock(&priv->ring_mutex); } static void pause(struct ao *ao) { struct priv *priv = ao->priv; check_pa_ret(Pa_AbortStream(priv->stream)); double stream_time = Pa_GetStreamTime(priv->stream); pthread_mutex_lock(&priv->ring_mutex); // When playback resumes, replace the lost audio (due to dropping the // portaudio/driver/hardware internal buffers) with silence. float frame_time = priv->play_time ? priv->play_time - stream_time : 0; priv->play_silence += seconds_to_bytes(ao, FFMAX(frame_time, 0)); priv->play_time = 0; pthread_mutex_unlock(&priv->ring_mutex); } static void resume(struct ao *ao) { struct priv *priv = ao->priv; check_pa_ret(Pa_StartStream(priv->stream)); } #define OPT_BASE_STRUCT struct priv const struct ao_driver audio_out_portaudio = { .description = "PortAudio", .name = "portaudio", .init = init, .uninit = uninit, .reset = reset, .get_space = get_space, .play = play, .get_delay = get_delay, .pause = pause, .resume = resume, .priv_size = sizeof(struct priv), .options = (const struct m_option[]) { OPT_STRING_VALIDATE("device", cfg_device, 0, validate_device_opt), {0} }, };
static int demux_open_tv(demuxer_t *demuxer, enum demux_check check) { tvi_handle_t *tvh; sh_video_t *sh_video; sh_audio_t *sh_audio = NULL; const tvi_functions_t *funcs; if (check > DEMUX_CHECK_REQUEST || demuxer->stream->type != STREAMTYPE_TV) return -1; demuxer->priv=NULL; if(!(tvh=tv_begin(demuxer->stream->priv, demuxer->log))) return -1; if (!tvh->functions->init(tvh->priv)) return -1; tvh->demuxer = demuxer; if (!open_tv(tvh)){ tv_uninit(tvh); return -1; } funcs = tvh->functions; demuxer->priv=tvh; struct sh_stream *sh_v = new_sh_stream(demuxer, STREAM_VIDEO); sh_video = sh_v->video; /* get IMAGE FORMAT */ int fourcc; funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FORMAT, &fourcc); if (fourcc == MP_FOURCC_MJPEG) { sh_v->codec = "mjpeg"; } else { sh_v->codec = "rawvideo"; sh_v->format = fourcc; } /* set FPS and FRAMETIME */ if(!sh_video->fps) { float tmp; if (funcs->control(tvh->priv, TVI_CONTROL_VID_GET_FPS, &tmp) != TVI_CONTROL_TRUE) sh_video->fps = 25.0f; /* on PAL */ else sh_video->fps = tmp; } if (tvh->tv_param->fps != -1.0f) sh_video->fps = tvh->tv_param->fps; /* If playback only mode, go to immediate mode, fail silently */ if(tvh->tv_param->immediate == 1) { funcs->control(tvh->priv, TVI_CONTROL_IMMEDIATE, 0); tvh->tv_param->noaudio = 1; } /* set width */ funcs->control(tvh->priv, TVI_CONTROL_VID_GET_WIDTH, &sh_video->disp_w); /* set height */ funcs->control(tvh->priv, TVI_CONTROL_VID_GET_HEIGHT, &sh_video->disp_h); demuxer->seekable = 0; /* here comes audio init */ if (tvh->tv_param->noaudio == 0 && funcs->control(tvh->priv, TVI_CONTROL_IS_AUDIO, 0) == TVI_CONTROL_TRUE) { int audio_format; /* yeah, audio is present */ funcs->control(tvh->priv, TVI_CONTROL_AUD_SET_SAMPLERATE, &tvh->tv_param->audiorate); if (funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_FORMAT, &audio_format) != TVI_CONTROL_TRUE) goto no_audio; switch(audio_format) { case AF_FORMAT_U8: case AF_FORMAT_S8: case AF_FORMAT_U16_LE: case AF_FORMAT_U16_BE: case AF_FORMAT_S16_LE: case AF_FORMAT_S16_BE: case AF_FORMAT_S32_LE: case AF_FORMAT_S32_BE: break; case AF_FORMAT_MPEG2: default: MP_ERR(tvh, "Audio type '%s' unsupported!\n", af_fmt_to_str(audio_format)); goto no_audio; } struct sh_stream *sh_a = new_sh_stream(demuxer, STREAM_AUDIO); sh_audio = sh_a->audio; funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_SAMPLERATE, &sh_audio->samplerate); int nchannels = sh_audio->channels.num; funcs->control(tvh->priv, TVI_CONTROL_AUD_GET_CHANNELS, &nchannels); mp_chmap_from_channels(&sh_audio->channels, nchannels); sh_a->codec = "mp-pcm"; sh_a->format = audio_format; int samplesize = af_fmt2bits(audio_format) / 8; sh_audio->i_bps = sh_audio->samplerate * samplesize * sh_audio->channels.num; // emulate WF for win32 codecs: sh_audio->wf = talloc_zero(sh_audio, MP_WAVEFORMATEX); sh_audio->wf->wFormatTag = sh_a->format; sh_audio->wf->nChannels = sh_audio->channels.num; sh_audio->wf->wBitsPerSample = samplesize * 8; sh_audio->wf->nSamplesPerSec = sh_audio->samplerate; sh_audio->wf->nBlockAlign = samplesize * sh_audio->channels.num; sh_audio->wf->nAvgBytesPerSec = sh_audio->i_bps; MP_VERBOSE(tvh, " TV audio: %d channels, %d bits, %d Hz\n", sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample, sh_audio->wf->nSamplesPerSec); } no_audio: if(!(funcs->start(tvh->priv))){ // start failed :( tv_uninit(tvh); return -1; } /* set color eq */ tv_set_color_options(tvh, TV_COLOR_BRIGHTNESS, tvh->tv_param->brightness); tv_set_color_options(tvh, TV_COLOR_HUE, tvh->tv_param->hue); tv_set_color_options(tvh, TV_COLOR_SATURATION, tvh->tv_param->saturation); tv_set_color_options(tvh, TV_COLOR_CONTRAST, tvh->tv_param->contrast); if(tvh->tv_param->gain!=-1) if(funcs->control(tvh->priv,TVI_CONTROL_VID_SET_GAIN,&tvh->tv_param->gain)!=TVI_CONTROL_TRUE) MP_WARN(tvh, "Unable to set gain control!\n"); return 0; }
int fill_audio_out_buffers(struct MPContext *mpctx, double endpts) { struct MPOpts *opts = mpctx->opts; struct ao *ao = mpctx->ao; int playsize; int playflags = 0; bool audio_eof = false; bool signal_eof = false; bool partial_fill = false; sh_audio_t * const sh_audio = mpctx->sh_audio; bool modifiable_audio_format = !(ao->format & AF_FORMAT_SPECIAL_MASK); int unitsize = ao->channels.num * af_fmt2bits(ao->format) / 8; if (mpctx->paused) playsize = 1; // just initialize things (audio pts at least) else playsize = ao_get_space(ao); // Coming here with hrseek_active still set means audio-only if (!mpctx->sh_video || !mpctx->sync_audio_to_video) mpctx->syncing_audio = false; if (!opts->initial_audio_sync || !modifiable_audio_format) { mpctx->syncing_audio = false; mpctx->hrseek_active = false; } int res; if (mpctx->syncing_audio || mpctx->hrseek_active) res = audio_start_sync(mpctx, playsize); else res = decode_audio(sh_audio, &ao->buffer, playsize); if (res < 0) { // EOF, error or format change if (res == -2) { /* The format change isn't handled too gracefully. A more precise * implementation would require draining buffered old-format audio * while displaying video, then doing the output format switch. */ if (!mpctx->opts->gapless_audio) uninit_player(mpctx, INITIALIZED_AO); reinit_audio_chain(mpctx); return -1; } else if (res == ASYNC_PLAY_DONE) return 0; else if (demux_stream_eof(mpctx->sh_audio->gsh)) audio_eof = true; } if (endpts != MP_NOPTS_VALUE && modifiable_audio_format) { double bytes = (endpts - written_audio_pts(mpctx) + mpctx->audio_delay) * ao->bps / opts->playback_speed; if (playsize > bytes) { playsize = MPMAX(bytes, 0); audio_eof = true; partial_fill = true; } } assert(ao->buffer.len % unitsize == 0); if (playsize > ao->buffer.len) { partial_fill = true; playsize = ao->buffer.len; } playsize -= playsize % unitsize; if (!playsize) return partial_fill && audio_eof ? -2 : -partial_fill; if (audio_eof && partial_fill) { if (opts->gapless_audio) { // With gapless audio, delay this to ao_uninit. There must be only // 1 final chunk, and that is handled when calling ao_uninit(). signal_eof = true; } else { playflags |= AOPLAY_FINAL_CHUNK; } } assert(ao->buffer_playable_size <= ao->buffer.len); int played = write_to_ao(mpctx, ao->buffer.start, playsize, playflags, written_audio_pts(mpctx)); ao->buffer_playable_size = playsize - played; if (played > 0) { ao->buffer.len -= played; memmove(ao->buffer.start, ao->buffer.start + played, ao->buffer.len); } else if (!mpctx->paused && audio_eof && ao_get_delay(ao) < .04) { // Sanity check to avoid hanging in case current ao doesn't output // partial chunks and doesn't check for AOPLAY_FINAL_CHUNK signal_eof = true; } return signal_eof ? -2 : -partial_fill; }
static int init(int rate,int channels,int format,int flags) { AudioStreamBasicDescription inDesc; ComponentDescription desc; Component comp; AURenderCallbackStruct renderCallback; OSStatus err; UInt32 size, maxFrames, b_alive; char *psz_name; AudioDeviceID devid_def = 0; int device_id, display_help = 0; const opt_t subopts[] = { {"device_id", OPT_ARG_INT, &device_id, NULL}, {"help", OPT_ARG_BOOL, &display_help, NULL}, {NULL} }; // set defaults device_id = 0; if (subopt_parse(ao_subdevice, subopts) != 0 || display_help) { print_help(); if (!display_help) return 0; } ao_msg(MSGT_AO,MSGL_V, "init([%dHz][%dch][%s][%d])\n", rate, channels, af_fmt2str_short(format), flags); ao = calloc(1, sizeof(ao_coreaudio_t)); ao->i_selected_dev = 0; ao->b_supports_digital = 0; ao->b_digital = 0; ao->b_muted = 0; ao->b_stream_format_changed = 0; ao->i_hog_pid = -1; ao->i_stream_id = 0; ao->i_stream_index = -1; ao->b_revert = 0; ao->b_changed_mixing = 0; if (device_id == 0) { /* Find the ID of the default Device. */ err = GetAudioProperty(kAudioObjectSystemObject, kAudioHardwarePropertyDefaultOutputDevice, sizeof(UInt32), &devid_def); if (err != noErr) { ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device: [%4.4s]\n", (char *)&err); goto err_out; } } else { devid_def = device_id; } /* Retrieve the name of the device. */ err = GetAudioPropertyString(devid_def, kAudioObjectPropertyName, &psz_name); if (err != noErr) { ao_msg(MSGT_AO, MSGL_WARN, "could not get default audio device name: [%4.4s]\n", (char *)&err); goto err_out; } ao_msg(MSGT_AO,MSGL_V, "got audio output device ID: %"PRIu32" Name: %s\n", devid_def, psz_name ); /* Probe whether device support S/PDIF stream output if input is AC3 stream. */ if (AF_FORMAT_IS_AC3(format)) { if (AudioDeviceSupportsDigital(devid_def)) { ao->b_supports_digital = 1; } ao_msg(MSGT_AO, MSGL_V, "probe default audio output device about support for digital s/pdif output: %d\n", ao->b_supports_digital ); } free(psz_name); // Save selected device id ao->i_selected_dev = devid_def; // Build Description for the input format inDesc.mSampleRate=rate; inDesc.mFormatID=ao->b_supports_digital ? kAudioFormat60958AC3 : kAudioFormatLinearPCM; inDesc.mChannelsPerFrame=channels; inDesc.mBitsPerChannel=af_fmt2bits(format); if((format&AF_FORMAT_POINT_MASK)==AF_FORMAT_F) { // float inDesc.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked; } else if((format&AF_FORMAT_SIGN_MASK)==AF_FORMAT_SI) { // signed int inDesc.mFormatFlags = kAudioFormatFlagIsSignedInteger|kAudioFormatFlagIsPacked; } else { // unsigned int inDesc.mFormatFlags = kAudioFormatFlagIsPacked; } if ((format & AF_FORMAT_END_MASK) == AF_FORMAT_BE) inDesc.mFormatFlags |= kAudioFormatFlagIsBigEndian; inDesc.mFramesPerPacket = 1; ao->packetSize = inDesc.mBytesPerPacket = inDesc.mBytesPerFrame = inDesc.mFramesPerPacket*channels*(inDesc.mBitsPerChannel/8); print_format(MSGL_V, "source:",&inDesc); if (ao->b_supports_digital) { b_alive = 1; err = GetAudioProperty(ao->i_selected_dev, kAudioDevicePropertyDeviceIsAlive, sizeof(UInt32), &b_alive); if (err != noErr) ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is alive: [%4.4s]\n", (char *)&err); if (!b_alive) ao_msg(MSGT_AO, MSGL_WARN, "device is not alive\n" ); /* S/PDIF output need device in HogMode. */ err = GetAudioProperty(ao->i_selected_dev, kAudioDevicePropertyHogMode, sizeof(pid_t), &ao->i_hog_pid); if (err != noErr) { /* This is not a fatal error. Some drivers simply don't support this property. */ ao_msg(MSGT_AO, MSGL_WARN, "could not check whether device is hogged: [%4.4s]\n", (char *)&err); ao->i_hog_pid = -1; } if (ao->i_hog_pid != -1 && ao->i_hog_pid != getpid()) { ao_msg(MSGT_AO, MSGL_WARN, "Selected audio device is exclusively in use by another program.\n" ); goto err_out; } ao->stream_format = inDesc; return OpenSPDIF(); } /* original analog output code */ desc.componentType = kAudioUnitType_Output; desc.componentSubType = (device_id == 0) ? kAudioUnitSubType_DefaultOutput : kAudioUnitSubType_HALOutput; desc.componentManufacturer = kAudioUnitManufacturer_Apple; desc.componentFlags = 0; desc.componentFlagsMask = 0; comp = FindNextComponent(NULL, &desc); //Finds an component that meets the desc spec's if (comp == NULL) { ao_msg(MSGT_AO, MSGL_WARN, "Unable to find Output Unit component\n"); goto err_out; } err = OpenAComponent(comp, &(ao->theOutputUnit)); //gains access to the services provided by the component if (err) { ao_msg(MSGT_AO, MSGL_WARN, "Unable to open Output Unit component: [%4.4s]\n", (char *)&err); goto err_out; } // Initialize AudioUnit err = AudioUnitInitialize(ao->theOutputUnit); if (err) { ao_msg(MSGT_AO, MSGL_WARN, "Unable to initialize Output Unit component: [%4.4s]\n", (char *)&err); goto err_out1; } size = sizeof(AudioStreamBasicDescription); err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &inDesc, size); if (err) { ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the input format: [%4.4s]\n", (char *)&err); goto err_out2; } size = sizeof(UInt32); err = AudioUnitGetProperty(ao->theOutputUnit, kAudioDevicePropertyBufferSize, kAudioUnitScope_Input, 0, &maxFrames, &size); if (err) { ao_msg(MSGT_AO,MSGL_WARN, "AudioUnitGetProperty returned [%4.4s] when getting kAudioDevicePropertyBufferSize\n", (char *)&err); goto err_out2; } //Set the Current Device to the Default Output Unit. err = AudioUnitSetProperty(ao->theOutputUnit, kAudioOutputUnitProperty_CurrentDevice, kAudioUnitScope_Global, 0, &ao->i_selected_dev, sizeof(ao->i_selected_dev)); ao->chunk_size = maxFrames;//*inDesc.mBytesPerFrame; ao_data.samplerate = inDesc.mSampleRate; ao_data.channels = inDesc.mChannelsPerFrame; ao_data.bps = ao_data.samplerate * inDesc.mBytesPerFrame; ao_data.outburst = ao->chunk_size; ao_data.buffersize = ao_data.bps; ao->num_chunks = (ao_data.bps+ao->chunk_size-1)/ao->chunk_size; ao->buffer_len = ao->num_chunks * ao->chunk_size; ao->buffer = av_fifo_alloc(ao->buffer_len); ao_msg(MSGT_AO,MSGL_V, "using %5d chunks of %d bytes (buffer len %d bytes)\n", (int)ao->num_chunks, (int)ao->chunk_size, (int)ao->buffer_len); renderCallback.inputProc = theRenderProc; renderCallback.inputProcRefCon = 0; err = AudioUnitSetProperty(ao->theOutputUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 0, &renderCallback, sizeof(AURenderCallbackStruct)); if (err) { ao_msg(MSGT_AO, MSGL_WARN, "Unable to set the render callback: [%4.4s]\n", (char *)&err); goto err_out2; } reset(); return CONTROL_OK; err_out2: AudioUnitUninitialize(ao->theOutputUnit); err_out1: CloseComponent(ao->theOutputUnit); err_out: av_fifo_free(ao->buffer); free(ao); ao = NULL; return CONTROL_FALSE; }
static int audio_start_sync(struct MPContext *mpctx, int playsize) { struct ao *ao = mpctx->ao; struct MPOpts *opts = mpctx->opts; sh_audio_t * const sh_audio = mpctx->sh_audio; int res; // Timing info may not be set without res = decode_audio(sh_audio, &ao->buffer, 1); if (res < 0) return res; int bytes; bool did_retry = false; double written_pts; double bps = ao->bps / opts->playback_speed; bool hrseek = mpctx->hrseek_active; // audio only hrseek mpctx->hrseek_active = false; while (1) { written_pts = written_audio_pts(mpctx); double ptsdiff; if (hrseek) ptsdiff = written_pts - mpctx->hrseek_pts; else ptsdiff = written_pts - mpctx->sh_video->pts - mpctx->delay - mpctx->audio_delay; bytes = ptsdiff * bps; bytes -= bytes % (ao->channels.num * af_fmt2bits(ao->format) / 8); // ogg demuxers give packets without timing if (written_pts <= 1 && sh_audio->pts == MP_NOPTS_VALUE) { if (!did_retry) { // Try to read more data to see packets that have pts res = decode_audio(sh_audio, &ao->buffer, ao->bps); if (res < 0) return res; did_retry = true; continue; } bytes = 0; } if (fabs(ptsdiff) > 300 || isnan(ptsdiff)) // pts reset or just broken? bytes = 0; if (bytes > 0) break; mpctx->syncing_audio = false; int a = MPMIN(-bytes, MPMAX(playsize, 20000)); res = decode_audio(sh_audio, &ao->buffer, a); bytes += ao->buffer.len; if (bytes >= 0) { memmove(ao->buffer.start, ao->buffer.start + ao->buffer.len - bytes, bytes); ao->buffer.len = bytes; if (res < 0) return res; return decode_audio(sh_audio, &ao->buffer, playsize); } ao->buffer.len = 0; if (res < 0) return res; } if (hrseek) // Don't add silence in audio-only case even if position is too late return 0; int fillbyte = 0; if ((ao->format & AF_FORMAT_SIGN_MASK) == AF_FORMAT_US) fillbyte = 0x80; if (bytes >= playsize) { /* This case could fall back to the one below with * bytes = playsize, but then silence would keep accumulating * in a_out_buffer if the AO accepts less data than it asks for * in playsize. */ char *p = malloc(playsize); memset(p, fillbyte, playsize); write_to_ao(mpctx, p, playsize, 0, written_pts - bytes / bps); free(p); return ASYNC_PLAY_DONE; } mpctx->syncing_audio = false; decode_audio_prepend_bytes(&ao->buffer, bytes, fillbyte); return decode_audio(sh_audio, &ao->buffer, playsize); }
// Filter data through filter static af_data_t* play(struct af_instance_s* af, af_data_t* data) { af_ac3enc_t *s = af->setup; af_data_t *c = data; // Current working data af_data_t *l; int len, left, outsize = 0, destsize; char *buf, *src, *dest; int max_output_len; int frame_num = (data->len + s->pending_len) / s->expect_len; int samplesize = af_fmt2bits(s->in_sampleformat) / 8; if (s->add_iec61937_header) max_output_len = AC3_FRAME_SIZE * 2 * 2 * frame_num; else max_output_len = AC3_MAX_CODED_FRAME_SIZE * frame_num; if (af->data->len < max_output_len) { mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, " "old len = %i, new len = %i\n", af->info->name, af->data->len, max_output_len); free(af->data->audio); af->data->audio = malloc(max_output_len); if (!af->data->audio) { mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory \n"); return NULL; } af->data->len = max_output_len; } l = af->data; // Local data buf = (char *)l->audio; src = (char *)c->audio; left = c->len; while (left > 0) { if (left + s->pending_len < s->expect_len) { memcpy(s->pending_data + s->pending_len, src, left); src += left; s->pending_len += left; left = 0; break; } dest = s->add_iec61937_header ? buf + 8 : buf; destsize = (char *)l->audio + l->len - buf; if (s->pending_len) { int needs = s->expect_len - s->pending_len; if (needs > 0) { memcpy(s->pending_data + s->pending_len, src, needs); src += needs; left -= needs; } if (c->nch >= 5) reorder_channel_nch(s->pending_data, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, c->nch, s->expect_len / samplesize, samplesize); len = avcodec_encode_audio(s->lavc_actx, dest, destsize, (void *)s->pending_data); s->pending_len = 0; } else { if (c->nch >= 5) reorder_channel_nch(src, AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, c->nch, s->expect_len / samplesize, samplesize); len = avcodec_encode_audio(s->lavc_actx,dest,destsize,(void *)src); src += s->expect_len; left -= s->expect_len; } mp_msg(MSGT_AFILTER, MSGL_DBG2, "avcodec_encode_audio got %d, pending %d.\n", len, s->pending_len); if (s->add_iec61937_header) { int bsmod = dest[5] & 0x7; AV_WB16(buf, 0xF872); // iec 61937 syncword 1 AV_WB16(buf + 2, 0x4E1F); // iec 61937 syncword 2 buf[4] = bsmod; // bsmod buf[5] = 0x01; // data-type ac3 AV_WB16(buf + 6, len << 3); // number of bits in payload memset(buf + 8 + len, 0, AC3_FRAME_SIZE * 2 * 2 - 8 - len); len = AC3_FRAME_SIZE * 2 * 2; } outsize += len; buf += len; } c->audio = l->audio; c->nch = 2; c->bps = 2; c->len = outsize; mp_msg(MSGT_AFILTER, MSGL_DBG2, "play return size %d, pending %d\n", outsize, s->pending_len); return c; }
// Initialization and runtime control static int control(struct af_instance_s* af, int cmd, void* arg) { switch(cmd){ case AF_CONTROL_REINIT:{ char buf1[256]; char buf2[256]; af_data_t *data = arg; // Make sure this filter isn't redundant if(af->data->format == data->format && af->data->bps == data->bps) return AF_DETACH; // Allow trivial AC3-endianness conversion if (!AF_FORMAT_IS_AC3(af->data->format) || !AF_FORMAT_IS_AC3(data->format)) // Check for errors in configuration if((AF_OK != check_bps(data->bps)) || (AF_OK != check_format(data->format)) || (AF_OK != check_bps(af->data->bps)) || (AF_OK != check_format(af->data->format))) return AF_ERROR; mp_msg(MSGT_AFILTER, MSGL_V, "[format] Changing sample format from %s to %s\n", af_fmt2str(data->format,buf1,256), af_fmt2str(af->data->format,buf2,256)); af->data->rate = data->rate; af->data->nch = data->nch; af->mul = (double)af->data->bps / data->bps; af->play = play; // set default // look whether only endianness differences are there if ((af->data->format & ~AF_FORMAT_END_MASK) == (data->format & ~AF_FORMAT_END_MASK)) { mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated endianness conversion only\n"); af->play = play_swapendian; } if ((data->format == AF_FORMAT_FLOAT_NE) && (af->data->format == AF_FORMAT_S16_NE)) { mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", af_fmt2str(data->format,buf1,256), af_fmt2str(af->data->format,buf2,256)); af->play = play_float_s16; } if ((data->format == AF_FORMAT_S16_NE) && (af->data->format == AF_FORMAT_FLOAT_NE)) { mp_msg(MSGT_AFILTER, MSGL_V, "[format] Accelerated %s to %s conversion\n", af_fmt2str(data->format,buf1,256), af_fmt2str(af->data->format,buf2,256)); af->play = play_s16_float; } return AF_OK; } case AF_CONTROL_COMMAND_LINE:{ int format = af_str2fmt_short(bstr0(arg)); if (format == -1) { mp_msg(MSGT_AFILTER, MSGL_ERR, "[format] %s is not a valid format\n", (char *)arg); return AF_ERROR; } if(AF_OK != af->control(af,AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET,&format)) return AF_ERROR; return AF_OK; } case AF_CONTROL_FORMAT_FMT | AF_CONTROL_SET:{ // Check for errors in configuration if(!AF_FORMAT_IS_AC3(*(int*)arg) && AF_OK != check_format(*(int*)arg)) return AF_ERROR; af->data->format = *(int*)arg; af->data->bps = af_fmt2bits(af->data->format)/8; return AF_OK; } } return AF_UNKNOWN; }
// Initialization and runtime control static int control(struct af_instance_s *af, int cmd, void *arg) { af_ac3enc_t *s = (af_ac3enc_t *)af->setup; af_data_t *data = (af_data_t *)arg; int i, bit_rate, test_output_res; static const int default_bit_rate[AC3_MAX_CHANNELS+1] = \ {0, 96000, 192000, 256000, 384000, 448000, 448000}; switch (cmd){ case AF_CONTROL_REINIT: if (AF_FORMAT_IS_AC3(data->format) || data->nch < s->min_channel_num) return AF_DETACH; af->data->format = s->in_sampleformat; af->data->bps = af_fmt2bits(s->in_sampleformat) / 8; if (data->rate == 48000 || data->rate == 44100 || data->rate == 32000) af->data->rate = data->rate; else af->data->rate = 48000; if (data->nch > AC3_MAX_CHANNELS) af->data->nch = AC3_MAX_CHANNELS; else af->data->nch = data->nch; test_output_res = af_test_output(af, data); s->pending_len = 0; s->expect_len = AC3_FRAME_SIZE * data->nch * af->data->bps; assert(s->expect_len <= s->pending_data_size); if (s->add_iec61937_header) af->mul = (double)AC3_FRAME_SIZE * 2 * 2 / s->expect_len; else af->mul = (double)AC3_MAX_CODED_FRAME_SIZE / s->expect_len; mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc reinit: %d, %d, %f, %d.\n", data->nch, data->rate, af->mul, s->expect_len); bit_rate = s->bit_rate ? s->bit_rate : default_bit_rate[af->data->nch]; if (s->lavc_actx->channels != af->data->nch || s->lavc_actx->sample_rate != af->data->rate || s->lavc_actx->bit_rate != bit_rate) { avcodec_close(s->lavc_actx); // Put sample parameters s->lavc_actx->channels = af->data->nch; s->lavc_actx->sample_rate = af->data->rate; s->lavc_actx->bit_rate = bit_rate; if (avcodec_open2(s->lavc_actx, s->lavc_acodec, NULL) < 0) { mp_tmsg(MSGT_AFILTER, MSGL_ERR, "Couldn't open codec %s, br=%d.\n", "ac3", bit_rate); return AF_ERROR; } } if (s->lavc_actx->frame_size != AC3_FRAME_SIZE) { mp_msg(MSGT_AFILTER, MSGL_ERR, "lavcac3enc: unexpected ac3 " "encoder frame size %d\n", s->lavc_actx->frame_size); return AF_ERROR; } af->data->format = AF_FORMAT_AC3_BE; af->data->bps = 2; af->data->nch = 2; return test_output_res; case AF_CONTROL_COMMAND_LINE: mp_msg(MSGT_AFILTER, MSGL_DBG2, "af_lavcac3enc cmdline: %s.\n", (char*)arg); s->bit_rate = 0; s->min_channel_num = 0; s->add_iec61937_header = 0; sscanf((char*)arg,"%d:%d:%d", &s->add_iec61937_header, &s->bit_rate, &s->min_channel_num); if (s->bit_rate < 1000) s->bit_rate *= 1000; if (s->bit_rate) { for (i = 0; i < 19; ++i) if (ac3_bitrate_tab[i] * 1000 == s->bit_rate) break; if (i >= 19) { mp_msg(MSGT_AFILTER, MSGL_WARN, "af_lavcac3enc unable set unsupported " "bitrate %d, use default bitrate (check manpage to see " "supported bitrates).\n", s->bit_rate); s->bit_rate = 0; } } if (s->min_channel_num == 0) s->min_channel_num = 5; mp_msg(MSGT_AFILTER, MSGL_V, "af_lavcac3enc config spdif:%d, bitrate:%d, " "minchnum:%d.\n", s->add_iec61937_header, s->bit_rate, s->min_channel_num); return AF_OK; } return AF_UNKNOWN; }
/** \brief setup sound device \param rate samplerate \param channels number of channels \param format format \param flags unused \return 0=success -1=fail */ static int init(struct ao *ao) { struct priv *p = ao->priv; int res; if (!InitDirectSound(ao)) return -1; ao->no_persistent_volume = true; p->audio_volume = 100; // ok, now create the buffers WAVEFORMATEXTENSIBLE wformat; DSBUFFERDESC dsbpridesc; DSBUFFERDESC dsbdesc; int format = af_fmt_from_planar(ao->format); int rate = ao->samplerate; if (AF_FORMAT_IS_AC3(format)) format = AF_FORMAT_AC3; else { struct mp_chmap_sel sel = {0}; mp_chmap_sel_add_waveext(&sel); if (!ao_chmap_sel_adjust(ao, &sel, &ao->channels)) return -1; } switch (format) { case AF_FORMAT_AC3: case AF_FORMAT_S24_LE: case AF_FORMAT_S16_LE: case AF_FORMAT_U8: break; default: MP_VERBOSE(ao, "format %s not supported defaulting to Signed 16-bit Little-Endian\n", af_fmt_to_str(format)); format = AF_FORMAT_S16_LE; } //set our audio parameters ao->samplerate = rate; ao->format = format; ao->bps = ao->channels.num * rate * (af_fmt2bits(format) >> 3); int buffersize = ao->bps; // space for 1 sec MP_VERBOSE(ao, "Samplerate:%iHz Channels:%i Format:%s\n", rate, ao->channels.num, af_fmt_to_str(format)); MP_VERBOSE(ao, "Buffersize:%d bytes (%d msec)\n", buffersize, buffersize / ao->bps * 1000); //fill waveformatex ZeroMemory(&wformat, sizeof(WAVEFORMATEXTENSIBLE)); wformat.Format.cbSize = (ao->channels.num > 2) ? sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) : 0; wformat.Format.nChannels = ao->channels.num; wformat.Format.nSamplesPerSec = rate; if (AF_FORMAT_IS_AC3(format)) { wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF; wformat.Format.wBitsPerSample = 16; wformat.Format.nBlockAlign = 4; } else { wformat.Format.wFormatTag = (ao->channels.num > 2) ? WAVE_FORMAT_EXTENSIBLE : WAVE_FORMAT_PCM; wformat.Format.wBitsPerSample = af_fmt2bits(format); wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3); } // fill in primary sound buffer descriptor memset(&dsbpridesc, 0, sizeof(DSBUFFERDESC)); dsbpridesc.dwSize = sizeof(DSBUFFERDESC); dsbpridesc.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbpridesc.dwBufferBytes = 0; dsbpridesc.lpwfxFormat = NULL; // fill in the secondary sound buffer (=stream buffer) descriptor memset(&dsbdesc, 0, sizeof(DSBUFFERDESC)); dsbdesc.dwSize = sizeof(DSBUFFERDESC); dsbdesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 /** Better position accuracy */ | DSBCAPS_GLOBALFOCUS /** Allows background playing */ | DSBCAPS_CTRLVOLUME; /** volume control enabled */ if (ao->channels.num > 2) { wformat.dwChannelMask = mp_chmap_to_waveext(&ao->channels); wformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; wformat.Samples.wValidBitsPerSample = wformat.Format.wBitsPerSample; // Needed for 5.1 on emu101k - shit soundblaster dsbdesc.dwFlags |= DSBCAPS_LOCHARDWARE; } wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign; dsbdesc.dwBufferBytes = buffersize; dsbdesc.lpwfxFormat = (WAVEFORMATEX *)&wformat; p->buffer_size = dsbdesc.dwBufferBytes; p->write_offset = 0; p->min_free_space = wformat.Format.nBlockAlign; p->outburst = wformat.Format.nBlockAlign * 512; // create primary buffer and set its format res = IDirectSound_CreateSoundBuffer(p->hds, &dsbpridesc, &p->hdspribuf, NULL); if (res != DS_OK) { UninitDirectSound(ao); MP_ERR(ao, "cannot create primary buffer (%s)\n", dserr2str(res)); return -1; } res = IDirectSoundBuffer_SetFormat(p->hdspribuf, (WAVEFORMATEX *)&wformat); if (res != DS_OK) { MP_WARN(ao, "cannot set primary buffer format (%s), using " "standard setting (bad quality)", dserr2str(res)); } MP_VERBOSE(ao, "primary buffer created\n"); // now create the stream buffer res = IDirectSound_CreateSoundBuffer(p->hds, &dsbdesc, &p->hdsbuf, NULL); if (res != DS_OK) { if (dsbdesc.dwFlags & DSBCAPS_LOCHARDWARE) { // Try without DSBCAPS_LOCHARDWARE dsbdesc.dwFlags &= ~DSBCAPS_LOCHARDWARE; res = IDirectSound_CreateSoundBuffer(p->hds, &dsbdesc, &p->hdsbuf, NULL); } if (res != DS_OK) { UninitDirectSound(ao); MP_ERR(ao, "cannot create secondary (stream)buffer (%s)\n", dserr2str(res)); return -1; } } MP_VERBOSE(ao, "secondary (stream)buffer created\n"); return 0; }
/* Prefer playing audio with the samplerate given in container data * if available, but take number the number of channels and sample format * from the codec, since if the codec isn't using the correct values for * those everything breaks anyway. */ static int setup_format(sh_audio_t *sh_audio) { struct priv *priv = sh_audio->context; AVCodecContext *codec = priv->avctx; int sample_format = sample_fmt_lavc2native(codec->sample_fmt); if (sample_format == AF_FORMAT_UNKNOWN) { #ifndef CONFIG_LIBAVRESAMPLE if (av_sample_fmt_is_planar(codec->sample_fmt)) mp_msg(MSGT_DECAUDIO, MSGL_ERR, "The player has been compiled without libavresample " "support,\nwhich is needed with this libavcodec decoder " "version.\nCompile with libavresample enabled to make " "audio decoding work!\n"); else mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Unsupported sample format\n"); goto error; #else if (priv->avr && (priv->resample_fmt != codec->sample_fmt || priv->resample_channels != codec->channels)) avresample_free(&priv->avr); if (!priv->avr) { int ret; uint8_t error[128]; enum AVSampleFormat out_fmt = av_get_packed_sample_fmt(codec->sample_fmt); uint64_t ch_layout = codec->channel_layout; mp_msg(MSGT_DECAUDIO, MSGL_V, "(Re)initializing libavresample format conversion...\n"); if (!ch_layout) ch_layout = av_get_default_channel_layout(codec->channels); /* if lavc format is planar, try just getting packed equivalent */ sample_format = sample_fmt_lavc2native(out_fmt); if (sample_format == AF_FORMAT_UNKNOWN) { /* fallback to s16 */ out_fmt = AV_SAMPLE_FMT_S16; sample_format = AF_FORMAT_S16_NE; } priv->avr = avresample_alloc_context(); if (!priv->avr) { mp_msg(MSGT_DECAUDIO, MSGL_FATAL, "Out of memory.\n"); abort(); } av_opt_set_int(priv->avr, "in_channel_layout", ch_layout, 0); av_opt_set_int(priv->avr, "out_channel_layout", ch_layout, 0); av_opt_set_int(priv->avr, "in_sample_rate", codec->sample_rate, 0); av_opt_set_int(priv->avr, "out_sample_rate", codec->sample_rate, 0); av_opt_set_int(priv->avr, "in_sample_fmt", codec->sample_fmt, 0); av_opt_set_int(priv->avr, "out_sample_fmt", out_fmt, 0); if ((ret = avresample_open(priv->avr)) < 0) { av_strerror(ret, error, sizeof(error)); mp_msg(MSGT_DECAUDIO, MSGL_ERR, "Error opening libavresample: %s.\n", error); goto error; } priv->resample_fmt = codec->sample_fmt; priv->resample_channels = codec->channels; priv->out_fmt = out_fmt; priv->unitsize = av_get_bytes_per_sample(out_fmt) * codec->channels; } else sample_format = sh_audio->sample_format; } else if (priv->avr) { avresample_free(&priv->avr); #endif } bool broken_srate = false; int samplerate = codec->sample_rate; int container_samplerate = sh_audio->container_out_samplerate; if (!container_samplerate && sh_audio->wf) container_samplerate = sh_audio->wf->nSamplesPerSec; if (codec->codec_id == AV_CODEC_ID_AAC && samplerate == 2 * container_samplerate) broken_srate = true; else if (container_samplerate) samplerate = container_samplerate; if (codec->channels != sh_audio->channels || samplerate != sh_audio->samplerate || sample_format != sh_audio->sample_format) { sh_audio->channels = codec->channels; sh_audio->samplerate = samplerate; sh_audio->sample_format = sample_format; sh_audio->samplesize = af_fmt2bits(sh_audio->sample_format) / 8; if (broken_srate) mp_msg(MSGT_DECAUDIO, MSGL_WARN, "Ignoring broken container sample rate for AAC with SBR\n"); return 1; } return 0; error: #ifdef CONFIG_LIBAVRESAMPLE avresample_free(&priv->avr); #endif return -1; }
// open & setup audio device // return: 1=success 0=fail static int init(int rate,int channels,int format,int flags) { WAVEFORMATEXTENSIBLE wformat; MMRESULT result; unsigned char* buffer; int i; if (AF_FORMAT_IS_AC3(format)) format = AF_FORMAT_AC3_NE; switch(format){ case AF_FORMAT_AC3_NE: case AF_FORMAT_S24_LE: case AF_FORMAT_S16_LE: case AF_FORMAT_U8: break; default: mp_msg(MSGT_AO, MSGL_V,"ao_win32: format %s not supported defaulting to Signed 16-bit Little-Endian\n",af_fmt2str_short(format)); format=AF_FORMAT_S16_LE; } //fill global ao_data ao_data.channels=channels; ao_data.samplerate=rate; ao_data.format=format; ao_data.bps=channels*rate; ao_data.bps*=af_fmt2bits(format)/8; if(ao_data.buffersize==-1) { ao_data.buffersize=af_fmt2bits(format)/8; ao_data.buffersize*= channels; ao_data.buffersize*= SAMPLESIZE; } ao_data.outburst = ao_data.buffersize; mp_msg(MSGT_AO, MSGL_V,"ao_win32: Samplerate:%iHz Channels:%i Format:%s\n",rate, channels, af_fmt2str_short(format)); mp_msg(MSGT_AO, MSGL_V,"ao_win32: Buffersize:%d\n",ao_data.buffersize); //fill waveformatex ZeroMemory( &wformat, sizeof(WAVEFORMATEXTENSIBLE)); wformat.Format.cbSize = (channels>2)?sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX):0; wformat.Format.nChannels = channels; wformat.Format.nSamplesPerSec = rate; wformat.Format.wBitsPerSample = af_fmt2bits(format); if(AF_FORMAT_IS_AC3(format)) { wformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF; wformat.Format.nBlockAlign = 4; } else { wformat.Format.wFormatTag = (channels>2)?WAVE_FORMAT_EXTENSIBLE:WAVE_FORMAT_PCM; wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3); } if(channels>2) { wformat.dwChannelMask = channel_mask[channels-3]; wformat.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; wformat.Samples.wValidBitsPerSample=af_fmt2bits(format); } wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign; //open sound device //WAVE_MAPPER always points to the default wave device on the system result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); if(result == WAVERR_BADFORMAT) { mp_msg(MSGT_AO, MSGL_ERR,"ao_win32: format not supported switching to default\n"); ao_data.channels = wformat.Format.nChannels = 2; ao_data.samplerate = wformat.Format.nSamplesPerSec = 44100; ao_data.format = AF_FORMAT_S16_LE; ao_data.bps=ao_data.channels * ao_data.samplerate*2; wformat.Format.wBitsPerSample=16; wformat.Format.wFormatTag=WAVE_FORMAT_PCM; wformat.Format.nBlockAlign = wformat.Format.nChannels * (wformat.Format.wBitsPerSample >> 3); wformat.Format.nAvgBytesPerSec = wformat.Format.nSamplesPerSec * wformat.Format.nBlockAlign; ao_data.buffersize=(wformat.Format.wBitsPerSample>>3)*wformat.Format.nChannels*SAMPLESIZE; result = waveOutOpen(&hWaveOut,WAVE_MAPPER,(WAVEFORMATEX*)&wformat,(DWORD_PTR)waveOutProc,0,CALLBACK_FUNCTION); }
void af_fix_parameters(af_data_t *data) { data->bps = af_fmt2bits(data->format)/8; }