diff options
author | wm4 <wm4@nowhere> | 2012-11-05 17:02:04 +0100 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2012-11-12 20:06:14 +0100 |
commit | d4bdd0473d6f43132257c9fb3848d829755167a3 (patch) | |
tree | 8021c2f7da1841393c8c832105e20cd527826d6c /audio/decode | |
parent | bd48deba77bd5582c5829d6fe73a7d2571088aba (diff) |
Rename directories, move files (step 1 of 2) (does not compile)
Tis drops the silly lib prefixes, and attempts to organize the tree in
a more logical way. Make the top-level directory less cluttered as
well.
Renames the following directories:
libaf -> audio/filter
libao2 -> audio/out
libvo -> video/out
libmpdemux -> demux
Split libmpcodecs:
vf* -> video/filter
vd*, dec_video.* -> video/decode
mp_image*, img_format*, ... -> video/
ad*, dec_audio.* -> audio/decode
libaf/format.* is moved to audio/ - this is similar to how mp_image.*
is located in video/.
Move most top-level .c/.h files to core. (talloc.c/.h is left on top-
level, because it's external.) Park some of the more annoying files
in compat/. Some of these are relicts from the time mplayer used
ffmpeg internals.
sub/ is not split, because it's too much of a mess (subtitle code is
mixed with OSD display and rendering).
Maybe the organization of core is not ideal: it mixes playback core
(like mplayer.c) and utility helpers (like bstr.c/h). Should the need
arise, the playback core will be moved somewhere else, while core
contains all helper and common code.
Diffstat (limited to 'audio/decode')
-rw-r--r-- | audio/decode/ad.c | 50 | ||||
-rw-r--r-- | audio/decode/ad.h | 54 | ||||
-rw-r--r-- | audio/decode/ad_dvdpcm.c | 162 | ||||
-rw-r--r-- | audio/decode/ad_internal.h | 46 | ||||
-rw-r--r-- | audio/decode/ad_lavc.c | 413 | ||||
-rw-r--r-- | audio/decode/ad_mpg123.c | 489 | ||||
-rw-r--r-- | audio/decode/ad_pcm.c | 220 | ||||
-rw-r--r-- | audio/decode/ad_spdif.c | 310 | ||||
-rw-r--r-- | audio/decode/dec_audio.c | 462 | ||||
-rw-r--r-- | audio/decode/dec_audio.h | 38 |
10 files changed, 2244 insertions, 0 deletions
diff --git a/audio/decode/ad.c b/audio/decode/ad.c new file mode 100644 index 0000000000..93cebed86d --- /dev/null +++ b/audio/decode/ad.c @@ -0,0 +1,50 @@ +/* + * audio decoder interface + * + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "config.h" + +#include "stream/stream.h" +#include "libmpdemux/demuxer.h" +#include "libmpdemux/stheader.h" +#include "ad.h" + +/* Missed vorbis, mad, dshow */ + +extern const ad_functions_t mpcodecs_ad_mpg123; +extern const ad_functions_t mpcodecs_ad_ffmpeg; +extern const ad_functions_t mpcodecs_ad_pcm; +extern const ad_functions_t mpcodecs_ad_dvdpcm; +extern const ad_functions_t mpcodecs_ad_spdif; + +const ad_functions_t * const mpcodecs_ad_drivers[] = +{ +#ifdef CONFIG_MPG123 + &mpcodecs_ad_mpg123, +#endif + &mpcodecs_ad_ffmpeg, + &mpcodecs_ad_pcm, + &mpcodecs_ad_dvdpcm, + &mpcodecs_ad_spdif, + NULL +}; diff --git a/audio/decode/ad.h b/audio/decode/ad.h new file mode 100644 index 0000000000..5396085d04 --- /dev/null +++ b/audio/decode/ad.h @@ -0,0 +1,54 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_AD_H +#define MPLAYER_AD_H + +#include "mpc_info.h" +#include "libmpdemux/stheader.h" + +typedef struct mp_codec_info ad_info_t; + +/* interface of video decoder drivers */ +typedef struct ad_functions +{ + const ad_info_t *info; + int (*preinit)(sh_audio_t *sh); + int (*init)(sh_audio_t *sh); + void (*uninit)(sh_audio_t *sh); + int (*control)(sh_audio_t *sh,int cmd,void* arg, ...); + int (*decode_audio)(sh_audio_t *sh, unsigned char *buffer, int minlen, + int maxlen); +} ad_functions_t; + +// NULL terminated array of all drivers +extern const ad_functions_t * const mpcodecs_ad_drivers[]; + +// fallback if ADCTRL_RESYNC not implemented: sh_audio->a_in_buffer_len=0; +#define ADCTRL_RESYNC_STREAM 1 // resync, called after seeking + +// fallback if ADCTRL_SKIP not implemented: ds_fill_buffer(sh_audio->ds); +#define ADCTRL_SKIP_FRAME 2 // skip block/frame, called while seeking + +// fallback if ADCTRL_QUERY_FORMAT not implemented: sh_audio->sample_format +#define ADCTRL_QUERY_FORMAT 3 // test for availabilty of a format + +// fallback: use hw mixer in libao +#define ADCTRL_SET_VOLUME 4 // not used at the moment + +#endif /* MPLAYER_AD_H */ diff --git a/audio/decode/ad_dvdpcm.c b/audio/decode/ad_dvdpcm.c new file mode 100644 index 0000000000..41f6a1426d --- /dev/null +++ b/audio/decode/ad_dvdpcm.c @@ -0,0 +1,162 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "config.h" +#include "mp_msg.h" +#include "ad_internal.h" + +static const ad_info_t info = +{ + "Uncompressed DVD/VOB LPCM audio decoder", + "dvdpcm", + "Nick Kurshev", + "A'rpi", + "" +}; + +LIBAD_EXTERN(dvdpcm) + +static int init(sh_audio_t *sh) +{ +/* DVD PCM Audio:*/ + sh->i_bps = 0; + if(sh->codecdata_len==3){ + // we have LPCM header: + unsigned char h=sh->codecdata[1]; + sh->channels=1+(h&7); + switch((h>>4)&3){ + case 0: sh->samplerate=48000;break; + case 1: sh->samplerate=96000;break; + case 2: sh->samplerate=44100;break; + case 3: sh->samplerate=32000;break; + } + switch ((h >> 6) & 3) { + case 0: + sh->sample_format = AF_FORMAT_S16_BE; + sh->samplesize = 2; + break; + case 1: + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Samples of this format are needed to improve support. Please contact the developers.\n"); + sh->i_bps = sh->channels * sh->samplerate * 5 / 2; + case 2: + sh->sample_format = AF_FORMAT_S24_BE; + sh->samplesize = 3; + break; + default: + sh->sample_format = AF_FORMAT_S16_BE; + sh->samplesize = 2; + } + } else { + // use defaults: + sh->channels=2; + sh->samplerate=48000; + sh->sample_format = AF_FORMAT_S16_BE; + sh->samplesize = 2; + } + if (!sh->i_bps) + sh->i_bps = sh->samplesize * sh->channels * sh->samplerate; + return 1; +} + +static int preinit(sh_audio_t *sh) +{ + sh->audio_out_minsize=2048; + return 1; +} + +static void uninit(sh_audio_t *sh) +{ +} + +static int control(sh_audio_t *sh,int cmd,void* arg, ...) +{ + int skip; + switch(cmd) + { + case ADCTRL_SKIP_FRAME: + skip=sh->i_bps/16; + skip=skip&(~3); + demux_read_data(sh->ds,NULL,skip); + return CONTROL_TRUE; + } + return CONTROL_UNKNOWN; +} + +static int decode_audio(sh_audio_t *sh_audio,unsigned char *buf,int minlen,int maxlen) +{ + int j,len; + if (sh_audio->samplesize == 3) { + if (((sh_audio->codecdata[1] >> 6) & 3) == 1) { + // 20 bit + // not sure if the "& 0xf0" and "<< 4" are the right way around + // can somebody clarify? + for (j = 0; j < minlen; j += 12) { + char tmp[10]; + len = demux_read_data(sh_audio->ds, tmp, 10); + if (len < 10) break; + // first sample + buf[j + 0] = tmp[0]; + buf[j + 1] = tmp[1]; + buf[j + 2] = tmp[8] & 0xf0; + // second sample + buf[j + 3] = tmp[2]; + buf[j + 4] = tmp[3]; + buf[j + 5] = tmp[8] << 4; + // third sample + buf[j + 6] = tmp[4]; + buf[j + 7] = tmp[5]; + buf[j + 8] = tmp[9] & 0xf0; + // fourth sample + buf[j + 9] = tmp[6]; + buf[j + 10] = tmp[7]; + buf[j + 11] = tmp[9] << 4; + } + len = j; + } else { + // 24 bit + for (j = 0; j < minlen; j += 12) { + char tmp[12]; + len = demux_read_data(sh_audio->ds, tmp, 12); + if (len < 12) break; + // first sample + buf[j + 0] = tmp[0]; + buf[j + 1] = tmp[1]; + buf[j + 2] = tmp[8]; + // second sample + buf[j + 3] = tmp[2]; + buf[j + 4] = tmp[3]; + buf[j + 5] = tmp[9]; + // third sample + buf[j + 6] = tmp[4]; + buf[j + 7] = tmp[5]; + buf[j + 8] = tmp[10]; + // fourth sample + buf[j + 9] = tmp[6]; + buf[j + 10] = tmp[7]; + buf[j + 11] = tmp[11]; + } + len = j; + } + } else + len=demux_read_data(sh_audio->ds,buf,(minlen+3)&(~3)); + return len; +} diff --git a/audio/decode/ad_internal.h b/audio/decode/ad_internal.h new file mode 100644 index 0000000000..4cffc95126 --- /dev/null +++ b/audio/decode/ad_internal.h @@ -0,0 +1,46 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_AD_INTERNAL_H +#define MPLAYER_AD_INTERNAL_H + +#include "codec-cfg.h" +#include "libaf/format.h" + +#include "stream/stream.h" +#include "libmpdemux/demuxer.h" +#include "libmpdemux/stheader.h" + +#include "ad.h" + +static int init(sh_audio_t *sh); +static int preinit(sh_audio_t *sh); +static void uninit(sh_audio_t *sh); +static int control(sh_audio_t *sh,int cmd,void* arg, ...); +static int decode_audio(sh_audio_t *sh,unsigned char *buffer,int minlen,int maxlen); + +#define LIBAD_EXTERN(x) const ad_functions_t mpcodecs_ad_##x = {\ + &info,\ + preinit,\ + init,\ + uninit,\ + control,\ + decode_audio\ +}; + +#endif /* MPLAYER_AD_INTERNAL_H */ diff --git a/audio/decode/ad_lavc.c b/audio/decode/ad_lavc.c new file mode 100644 index 0000000000..2eacfadb8f --- /dev/null +++ b/audio/decode/ad_lavc.c @@ -0,0 +1,413 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdbool.h> +#include <assert.h> + +#include <libavcodec/avcodec.h> +#include <libavutil/opt.h> + +#include "talloc.h" + +#include "config.h" +#include "mp_msg.h" +#include "options.h" + +#include "ad_internal.h" +#include "libaf/reorder_ch.h" + +#include "mpbswap.h" + +static const ad_info_t info = +{ + "libavcodec audio decoders", + "ffmpeg", + "", + "", + "", + .print_name = "libavcodec", +}; + +LIBAD_EXTERN(ffmpeg) + +struct priv { + AVCodecContext *avctx; + AVFrame *avframe; + char *output; + char *output_packed; // used by deplanarize to store packed audio samples + int output_left; + int unitsize; + int previous_data_left; // input demuxer packet data +}; + +static int preinit(sh_audio_t *sh) +{ + return 1; +} + +/* 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 (av_get_packed_sample_fmt(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 init(sh_audio_t *sh_audio) +{ + struct MPOpts *opts = sh_audio->opts; + AVCodecContext *lavc_context; + AVCodec *lavc_codec; + + if (sh_audio->codec->dll) { + lavc_codec = 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; + } + } else if (!sh_audio->libav_codec_id) { + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "No Libav codec ID known. " + "Generic lavc decoder is not applicable.\n"); + return 0; + } else { + lavc_codec = avcodec_find_decoder(sh_audio->libav_codec_id); + if (!lavc_codec) { + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Libavcodec has no decoder " + "for this codec\n"); + return 0; + } + } + + sh_audio->codecname = lavc_codec->long_name; + if (!sh_audio->codecname) + sh_audio->codecname = lavc_codec->name; + + struct priv *ctx = talloc_zero(NULL, struct priv); + sh_audio->context = ctx; + lavc_context = avcodec_alloc_context3(lavc_codec); + ctx->avctx = lavc_context; + ctx->avframe = avcodec_alloc_frame(); + + // Always try to set - option only exists for AC3 at the moment + av_opt_set_double(lavc_context, "drc_scale", opts->drc_level, + AV_OPT_SEARCH_CHILDREN); + 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 = opts->audio_output_channels; + lavc_context->codec_tag = sh_audio->format; //FOURCC + if (sh_audio->gsh->lavf_codec_tag) + lavc_context->codec_tag = sh_audio->gsh->lavf_codec_tag; + lavc_context->codec_type = AVMEDIA_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, sh_audio->wf + 1, + 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 + + FF_INPUT_BUFFER_PADDING_SIZE); + lavc_context->extradata_size = sh_audio->codecdata_len; + memcpy(lavc_context->extradata, (char *)sh_audio->codecdata, + lavc_context->extradata_size); + } + + /* open it */ + if (avcodec_open2(lavc_context, lavc_codec, NULL) < 0) { + mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Could not open codec.\n"); + uninit(sh_audio); + return 0; + } + mp_msg(MSGT_DECAUDIO, MSGL_V, "INFO: libavcodec \"%s\" init OK!\n", + lavc_codec->name); + + 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) + for (int tries = 0;;) { + int x = decode_audio(sh_audio, sh_audio->a_buffer, 1, + sh_audio->a_buffer_size); + if (x > 0) { + sh_audio->a_buffer_len = x; + break; + } + if (++tries >= 5) { + mp_msg(MSGT_DECAUDIO, MSGL_ERR, + "ad_ffmpeg: initial decode failed\n"); + uninit(sh_audio); + return 0; + } + } + + sh_audio->i_bps = lavc_context->bit_rate / 8; + if (sh_audio->wf && sh_audio->wf->nAvgBytesPerSec) + sh_audio->i_bps = sh_audio->wf->nAvgBytesPerSec; + + switch (av_get_packed_sample_fmt(lavc_context->sample_fmt)) { + case AV_SAMPLE_FMT_U8: + case AV_SAMPLE_FMT_S16: + case AV_SAMPLE_FMT_S32: + case AV_SAMPLE_FMT_FLT: + break; + default: + uninit(sh_audio); + return 0; + } + return 1; +} + +static void uninit(sh_audio_t *sh) +{ + sh->codecname = NULL; + struct priv *ctx = sh->context; + if (!ctx) + return; + AVCodecContext *lavc_context = ctx->avctx; + + if (lavc_context) { + if (avcodec_close(lavc_context) < 0) + mp_tmsg(MSGT_DECVIDEO, MSGL_ERR, "Could not close codec.\n"); + av_freep(&lavc_context->extradata); + av_freep(&lavc_context); + } + avcodec_free_frame(&ctx->avframe); + talloc_free(ctx); + sh->context = NULL; +} + +static int control(sh_audio_t *sh, int cmd, void *arg, ...) +{ + struct priv *ctx = sh->context; + switch (cmd) { + case ADCTRL_RESYNC_STREAM: + avcodec_flush_buffers(ctx->avctx); + ds_clear_parser(sh->ds); + ctx->previous_data_left = 0; + ctx->output_left = 0; + return CONTROL_TRUE; + } + return CONTROL_UNKNOWN; +} + +static av_always_inline void deplanarize(struct sh_audio *sh) +{ + struct priv *priv = sh->context; + + size_t bps = av_get_bytes_per_sample(priv->avctx->sample_fmt); + size_t nb_samples = priv->avframe->nb_samples; + size_t channels = priv->avctx->channels; + size_t size = bps * nb_samples * channels; + + if (talloc_get_size(priv->output_packed) != size) + priv->output_packed = + talloc_realloc_size(priv, priv->output_packed, size); + + size_t offset = 0; + unsigned char *output_ptr = priv->output_packed; + unsigned char **src = priv->avframe->data; + + for (size_t s = 0; s < nb_samples; s++) { + for (size_t c = 0; c < channels; c++) { + memcpy(output_ptr, src[c] + offset, bps); + output_ptr += bps; + } + offset += bps; + } + + priv->output = priv->output_packed; +} + +static int decode_new_packet(struct sh_audio *sh) +{ + struct priv *priv = sh->context; + AVCodecContext *avctx = priv->avctx; + double pts = MP_NOPTS_VALUE; + int insize; + bool packet_already_used = priv->previous_data_left; + struct demux_packet *mpkt = ds_get_packet2(sh->ds, + priv->previous_data_left); + unsigned char *start; + if (!mpkt) { + assert(!priv->previous_data_left); + start = NULL; + insize = 0; + ds_parse(sh->ds, &start, &insize, pts, 0); + if (insize <= 0) + return -1; // error or EOF + } else { + assert(mpkt->len >= priv->previous_data_left); + if (!priv->previous_data_left) { + priv->previous_data_left = mpkt->len; + pts = mpkt->pts; + } + insize = priv->previous_data_left; + start = mpkt->buffer + mpkt->len - priv->previous_data_left; + int consumed = ds_parse(sh->ds, &start, &insize, pts, 0); + priv->previous_data_left -= consumed; + priv->previous_data_left = FFMAX(priv->previous_data_left, 0); + } + + AVPacket pkt; + av_init_packet(&pkt); + pkt.data = start; + pkt.size = insize; + if (mpkt && mpkt->avpacket) { + pkt.side_data = mpkt->avpacket->side_data; + pkt.side_data_elems = mpkt->avpacket->side_data_elems; + } + if (pts != MP_NOPTS_VALUE && !packet_already_used) { + sh->pts = pts; + sh->pts_bytes = 0; + } + int got_frame = 0; + int ret = avcodec_decode_audio4(avctx, priv->avframe, &got_frame, &pkt); + // LATM may need many packets to find mux info + if (ret == AVERROR(EAGAIN)) + return 0; + if (ret < 0) { + mp_msg(MSGT_DECAUDIO, MSGL_V, "lavc_audio: error\n"); + return -1; + } + // The "insize >= ret" test is sanity check against decoder overreads + if (!sh->parser && insize >= ret) + priv->previous_data_left = insize - ret; + if (!got_frame) + return 0; + uint64_t unitsize = (uint64_t)av_get_bytes_per_sample(avctx->sample_fmt) * + avctx->channels; + if (unitsize > 100000) + abort(); + priv->unitsize = unitsize; + uint64_t output_left = unitsize * priv->avframe->nb_samples; + if (output_left > 500000000) + abort(); + priv->output_left = output_left; + if (av_sample_fmt_is_planar(avctx->sample_fmt) && avctx->channels > 1) { + deplanarize(sh); + } else { + priv->output = priv->avframe->data[0]; + } + mp_dbg(MSGT_DECAUDIO, MSGL_DBG2, "Decoded %d -> %d \n", insize, + priv->output_left); + return 0; +} + + +static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen, + int maxlen) +{ + struct priv *priv = sh_audio->context; + AVCodecContext *avctx = priv->avctx; + + int len = -1; + while (len < minlen) { + if (!priv->output_left) { + if (decode_new_packet(sh_audio) < 0) + break; + continue; + } + if (setup_format(sh_audio, avctx)) + return len; + int size = (minlen - len + priv->unitsize - 1); + size -= size % priv->unitsize; + size = FFMIN(size, priv->output_left); + if (size > maxlen) + abort(); + memcpy(buf, priv->output, size); + priv->output += size; + priv->output_left -= size; + if (avctx->channels >= 5) { + int samplesize = av_get_bytes_per_sample(avctx->sample_fmt); + reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_LAVC_DEFAULT, + AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, + avctx->channels, + size / samplesize, samplesize); + } + if (len < 0) + len = size; + else + len += size; + buf += size; + maxlen -= size; + sh_audio->pts_bytes += size; + } + return len; +} diff --git a/audio/decode/ad_mpg123.c b/audio/decode/ad_mpg123.c new file mode 100644 index 0000000000..a3ce2cdcf6 --- /dev/null +++ b/audio/decode/ad_mpg123.c @@ -0,0 +1,489 @@ +/* + * MPEG 1.0/2.0/2.5 audio layer I, II, III decoding with libmpg123 + * + * Copyright (C) 2010-2012 Thomas Orgis <thomas@orgis.org> + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +#include "config.h" + +#include "ad_internal.h" + +static const ad_info_t info = { + "MPEG 1.0/2.0/2.5 layers I, II, III", + "mpg123", + "Thomas Orgis", + "mpg123.org", + "High-performance decoder using libmpg123." +}; + +LIBAD_EXTERN(mpg123) + +/* Reducing the ifdeffery to two main variants: + * 1. most compatible to any libmpg123 version + * 2. fastest variant with recent libmpg123 (>=1.14) + * Running variant 2 on older libmpg123 versions may work in + * principle, but is not supported. + * So, please leave the check for MPG123_API_VERSION there, m-kay? + */ +#include <mpg123.h> + +/* Enable faster mode of operation with newer libmpg123, avoiding + * unnecessary memcpy() calls. */ +#if (defined MPG123_API_VERSION) && (MPG123_API_VERSION >= 33) +#define AD_MPG123_FRAMEWISE +#endif + +/* Switch for updating bitrate info of VBR files. Not essential. */ +#define AD_MPG123_MEAN_BITRATE + +/* Funny thing, that. I assume I shall use it for selecting mpg123 channels. + * Please correct me if I guessed wrong. */ +extern int fakemono; + +struct ad_mpg123_context { + mpg123_handle *handle; +#ifdef AD_MPG123_MEAN_BITRATE + /* Running mean for bit rate, stream length estimation. */ + float mean_rate; + unsigned int mean_count; + /* Time delay for updates. */ + short delay; +#endif + /* If the stream is actually VBR. */ + char vbr; +}; + +/* This initializes libmpg123 and prepares the handle, including funky + * parameters. */ +static int preinit(sh_audio_t *sh) +{ + int err, flag; + struct ad_mpg123_context *con; + /* Assumption: You always call preinit + init + uninit, on every file. + * But you stop at preinit in case it fails. + * If that is not true, one must ensure not to call mpg123_init / exit + * twice in a row. */ + if (mpg123_init() != MPG123_OK) + return 0; + + sh->context = malloc(sizeof(struct ad_mpg123_context)); + con = sh->context; + /* Auto-choice of optimized decoder (first argument NULL). */ + con->handle = mpg123_new(NULL, &err); + if (!con->handle) + goto bad_end; + + /* Guessing here: Default value triggers forced upmix of mono to stereo. */ + flag = fakemono == 0 ? MPG123_FORCE_STEREO : + fakemono == 1 ? MPG123_MONO_LEFT : + fakemono == 2 ? MPG123_MONO_RIGHT : 0; + if (mpg123_param(con->handle, MPG123_ADD_FLAGS, flag, 0.0) != MPG123_OK) + goto bad_end; + + /* Basic settings. + * Don't spill messages, enable better resync with non-seekable streams. + * Give both flags individually without error checking to keep going with + * old libmpg123. Generally, it is not fatal if the flags are not + * honored */ + mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_QUIET, 0.0); + /* Do not bail out on malformed streams at all. + * MPlayer does not handle a decoder throwing the towel on crappy input. */ + mpg123_param(con->handle, MPG123_RESYNC_LIMIT, -1, 0.0); + + /* Open decisions: Configure libmpg123 to force encoding (or stay open about + * library builds that support only float or int32 output), (de)configure + * gapless decoding (won't work with seeking in MPlayer, though). + * Don't forget to eventually enable ReplayGain/RVA support, too. + * Let's try to run with the default for now. */ + + /* That would produce floating point output. + * You can get 32 and 24 bit ints, even 8 bit via format matrix. */ + /* mpg123_param(con->handle, MPG123_ADD_FLAGS, MPG123_FORCE_FLOAT, 0.); */ + + /* Example for RVA choice (available since libmpg123 1.0.0): + mpg123_param(con->handle, MPG123_RVA, MPG123_RVA_MIX, 0.0) */ + +#ifdef AD_MPG123_FRAMEWISE + /* Prevent funky automatic resampling. + * This way, we can be sure that one frame will never produce + * more than 1152 stereo samples. */ + mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_AUTO_RESAMPLE, 0.); +#else + /* Older mpg123 is vulnerable to concatenated streams when gapless cutting + * is enabled (will only play the jingle of a badly constructed radio + * stream). The versions using framewise decoding are fine with that. */ + mpg123_param(con->handle, MPG123_REMOVE_FLAGS, MPG123_GAPLESS, 0.); +#endif + + return 1; + + bad_end: + if (!con->handle) + mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", + mpg123_plain_strerror(err)); + else + mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 preinit error: %s\n", + mpg123_strerror(con->handle)); + + if (con->handle) + mpg123_delete(con->handle); + mpg123_exit(); + free(sh->context); + sh->context = NULL; + return 0; +} + +/* Compute bitrate from frame size. */ +static int compute_bitrate(struct mpg123_frameinfo *i) +{ + static const int samples_per_frame[4][4] = { + {-1, 384, 1152, 1152}, /* MPEG 1 */ + {-1, 384, 1152, 576}, /* MPEG 2 */ + {-1, 384, 1152, 576}, /* MPEG 2.5 */ + {-1, -1, -1, -1}, /* Unknown */ + }; + return (int) ((i->framesize + 4) * 8 * i->rate * 0.001 / + samples_per_frame[i->version][i->layer] + 0.5); +} + +/* Opted against the header printout from old mp3lib, too much + * irrelevant info. This is modelled after the mpg123 app's + * standard output line. + * If more verbosity is demanded, one can add more detail and + * also throw in ID3v2 info which libmpg123 collects anyway. */ +static void print_header_compact(struct mpg123_frameinfo *i) +{ + static const char *smodes[5] = { + "stereo", "joint-stereo", "dual-channel", "mono", "invalid" + }; + static const char *layers[4] = { + "Unknown", "I", "II", "III" + }; + static const char *versions[4] = { + "1.0", "2.0", "2.5", "x.x" + }; + + mp_msg(MSGT_DECAUDIO, MSGL_V, "MPEG %s layer %s, ", + versions[i->version], layers[i->layer]); + switch (i->vbr) { + case MPG123_CBR: + if (i->bitrate) + mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s", i->bitrate); + else + mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s (free format)", + compute_bitrate(i)); + break; + case MPG123_VBR: + mp_msg(MSGT_DECAUDIO, MSGL_V, "VBR"); + break; + case MPG123_ABR: + mp_msg(MSGT_DECAUDIO, MSGL_V, "%d kbit/s ABR", i->abr_rate); + break; + default: + mp_msg(MSGT_DECAUDIO, MSGL_V, "???"); + } + mp_msg(MSGT_DECAUDIO, MSGL_V, ", %ld Hz %s\n", i->rate, + smodes[i->mode]); +} + +/* This tries to extract a requested amount of decoded data. + * Even when you request 0 bytes, it will feed enough input so that + * the decoder _could_ have delivered something. + * Returns byte count >= 0, -1 on error. + * + * Thoughts on exact pts keeping: + * We have to assume that MPEG frames are cut in pieces by packet boundaries. + * Also, it might be possible that the first packet does not contain enough + * data to ensure initial stream sync... or re-sync on erroneous streams. + * So we need something robust to relate the decoded byte count to the correct + * time stamp. This is tricky, though. From the outside, you cannot tell if, + * after having fed two packets until the first output arrives, one should + * start counting from the first packet's pts or the second packet's. + * So, let's just count from the last fed package's pts. If the packets are + * exactly cut to MPEG frames, this will cause one frame mismatch in the + * beginning (when mpg123 peeks ahead for the following header), but will + * be corrected with the third frame already. One might add special code to + * not increment the base pts past the first packet's after a resync before + * the first decoded bytes arrived. */ +static int decode_a_bit(sh_audio_t *sh, unsigned char *buf, int count) +{ + int ret = MPG123_OK; + int got = 0; + struct ad_mpg123_context *con = sh->context; + + /* There will be one MPG123_NEW_FORMAT message on first open. + * This will be handled in init(). */ + do { + size_t got_now = 0; + + /* Feed the decoder. This will only fire from the second round on. */ + if (ret == MPG123_NEED_MORE) { + int incount; + double pts; + unsigned char *inbuf; + /* Feed more input data. */ + incount = ds_get_packet_pts(sh->ds, &inbuf, &pts); + if (incount <= 0) + break; /* Apparently that's it. EOF. */ + + /* Next bytes from that presentation time. */ + if (pts != MP_NOPTS_VALUE) { + sh->pts = pts; + sh->pts_bytes = 0; + } + +#ifdef AD_MPG123_FRAMEWISE + /* Have to use mpg123_feed() to avoid decoding here. */ + ret = mpg123_feed(con->handle, inbuf, incount); +#else + /* Do not use mpg123_feed(), added in later libmpg123 versions. */ + ret = mpg123_decode(con->handle, inbuf, incount, NULL, 0, NULL); +#endif + if (ret == MPG123_ERR) + break; + } + /* Theoretically, mpg123 could return MPG123_DONE, so be prepared. + * Should not happen in our usage, but it is a valid return code. */ + else if (ret == MPG123_ERR || ret == MPG123_DONE) + break; + + /* Try to decode a bit. This is the return value that counts + * for the loop condition. */ +#ifdef AD_MPG123_FRAMEWISE + if (!buf) { /* fake call just for feeding to get format */ + ret = mpg123_getformat(con->handle, NULL, NULL, NULL); + } else { /* This is the decoding. One frame at a time. */ + ret = mpg123_replace_buffer(con->handle, buf, count); + if (ret == MPG123_OK) + ret = mpg123_decode_frame(con->handle, NULL, NULL, &got_now); + } +#else + ret = mpg123_decode(con->handle, NULL, 0, buf + got, count - got, + &got_now); +#endif + + got += got_now; + sh->pts_bytes += got_now; + +#ifdef AD_MPG123_FRAMEWISE + } while (ret == MPG123_NEED_MORE || (got == 0 && count != 0)); +#else + } while (ret == MPG123_NEED_MORE || got < count); +#endif + + if (ret == MPG123_ERR) { + mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 decoding failed: %s\n", + mpg123_strerror(con->handle)); + mpg123_close(con->handle); + return -1; + } + + return got; +} + +/* Close, reopen stream. Feed data until we know the format of the stream. + * 1 on success, 0 on error */ +static int reopen_stream(sh_audio_t *sh) +{ + struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context; + + mpg123_close(con->handle); + /* No resetting of the context: + * We do not want to loose the mean bitrate data. */ + + /* Open and make sure we have fed enough data to get stream properties. */ + if (MPG123_OK == mpg123_open_feed(con->handle) && + /* Feed data until mpg123 is ready (has found stream beginning). */ + !decode_a_bit(sh, NULL, 0)) { + return 1; + } else { + mp_msg(MSGT_DECAUDIO, MSGL_ERR, + "mpg123 failed to reopen stream: %s\n", + mpg123_strerror(con->handle)); + mpg123_close(con->handle); + return 0; + } +} + +/* Now we really start accessing some data and determining file format. + * Paranoia note: The mpg123_close() on errors is not really necessary, + * But it ensures that we don't accidentally continue decoding with a + * bad state (possibly interpreting the format badly or whatnot). */ +static int init(sh_audio_t *sh) +{ + long rate = 0; + int channels = 0; + int encoding = 0; + mpg123_id3v2 *v2; + struct mpg123_frameinfo finfo; + struct ad_mpg123_context *con = sh->context; + + /* We're open about any output format that libmpg123 will suggest. + * Note that a standard build will always default to 16 bit signed and + * the native sample rate of the file. */ + if (MPG123_OK == mpg123_format_all(con->handle) && + reopen_stream(sh) && + MPG123_OK == mpg123_getformat(con->handle, &rate, &channels, &encoding) && + /* Forbid the format to change later on. */ + MPG123_OK == mpg123_format_none(con->handle) && + MPG123_OK == mpg123_format(con->handle, rate, channels, encoding) && + /* Get MPEG header info. */ + MPG123_OK == mpg123_info(con->handle, &finfo) && + /* Since we queried format, mpg123 should have read past ID3v2 tags. + * We need to decide if printing of UTF-8 encoded text info is wanted. */ + MPG123_OK == mpg123_id3(con->handle, NULL, &v2)) { + /* If we are here, we passed all hurdles. Yay! Extract the info. */ + print_header_compact(&finfo); + /* Do we want to print out the UTF-8 Id3v2 info? + if (v2) + print_id3v2(v2); */ + + /* Have kb/s, want B/s + * For VBR, the first frame will be a bad estimate. */ + sh->i_bps = (finfo.bitrate ? finfo.bitrate : compute_bitrate(&finfo)) + * 1000 / 8; +#ifdef AD_MPG123_MEAN_BITRATE + con->delay = 1; + con->mean_rate = 0.; + con->mean_count = 0; +#endif + con->vbr = (finfo.vbr != MPG123_CBR); + sh->channels = channels; + sh->samplerate = rate; + /* Without external force, mpg123 will always choose signed encoding, + * and non-16-bit only on builds that don't support it. + * Be reminded that it doesn't matter to the MPEG file what encoding + * is produced from it. */ + switch (encoding) { + case MPG123_ENC_SIGNED_8: + sh->sample_format = AF_FORMAT_S8; + sh->samplesize = 1; + break; + case MPG123_ENC_SIGNED_16: + sh->sample_format = AF_FORMAT_S16_NE; + sh->samplesize = 2; + break; + /* To stay compatible with the oldest libmpg123 headers, do not rely + * on float and 32 bit encoding symbols being defined. + * Those formats came later */ + case 0x1180: /* MPG123_ENC_SIGNED_32 */ + sh->sample_format = AF_FORMAT_S32_NE; + sh->samplesize = 4; + break; + case 0x200: /* MPG123_ENC_FLOAT_32 */ + sh->sample_format = AF_FORMAT_FLOAT_NE; + sh->samplesize = 4; + break; + default: + mp_msg(MSGT_DECAUDIO, MSGL_ERR, + "Bad encoding from mpg123: %i.\n", encoding); + mpg123_close(con->handle); + return 0; + } +#ifdef AD_MPG123_FRAMEWISE + /* Going to decode directly to MPlayer's memory. It is important + * to have MPG123_AUTO_RESAMPLE disabled for the buffer size + * being an all-time limit. */ + sh->audio_out_minsize = 1152 * 2 * sh->samplesize; +#endif + + return 1; + } else { + mp_msg(MSGT_DECAUDIO, MSGL_ERR, "mpg123 init error: %s\n", + mpg123_strerror(con->handle)); + mpg123_close(con->handle); + return 0; + } +} + +static void uninit(sh_audio_t *sh) +{ + struct ad_mpg123_context *con = (struct ad_mpg123_context*) sh->context; + + mpg123_close(con->handle); + mpg123_delete(con->handle); + free(sh->context); + sh->context = NULL; + mpg123_exit(); +} + +#ifdef AD_MPG123_MEAN_BITRATE +/* Update mean bitrate. This could be dropped if accurate time display + * on audio file playback is not desired. */ +static void update_info(sh_audio_t *sh) +{ + struct ad_mpg123_context *con = sh->context; + if (con->vbr && --con->delay < 1) { + struct mpg123_frameinfo finfo; + if (MPG123_OK == mpg123_info(con->handle, &finfo)) { + if (++con->mean_count > ((unsigned int) -1) / 2) + con->mean_count = ((unsigned int) -1) / 4; + + /* Might not be numerically optimal, but works fine enough. */ + con->mean_rate = ((con->mean_count - 1) * con->mean_rate + + finfo.bitrate) / con->mean_count; + sh->i_bps = (int) (con->mean_rate * 1000 / 8); + + con->delay = 10; + } + } +} +#endif + +static int decode_audio(sh_audio_t *sh, unsigned char *buf, int minlen, + int maxlen) +{ + int bytes; + + bytes = decode_a_bit(sh, buf, maxlen); + if (bytes == 0) + return -1; /* EOF */ + +#ifdef AD_MPG123_MEAN_BITRATE + update_info(sh); +#endif + return bytes; +} + +static int control(sh_audio_t *sh, int cmd, void *arg, ...) +{ + switch (cmd) { + case ADCTRL_RESYNC_STREAM: + /* Close/reopen the stream for mpg123 to make sure it doesn't + * think that it still knows the exact stream position. + * Otherwise, we would have funny effects from the gapless code. + * Oh, and it helps to minimize artifacts from jumping in the stream. */ + if (reopen_stream(sh)) { +#ifdef AD_MPG123_MEAN_BITRATE + update_info(sh); +#endif + return CONTROL_TRUE; + } else { + /* MPlayer ignores this case! It just keeps on decoding. + * So we have to make sure resync never fails ... */ + mp_msg(MSGT_DECAUDIO, MSGL_ERR, + "mpg123 cannot reopen stream for resync.\n"); + return CONTROL_FALSE; + } + break; + } + return CONTROL_UNKNOWN; +} diff --git a/audio/decode/ad_pcm.c b/audio/decode/ad_pcm.c new file mode 100644 index 0000000000..c265dfcd56 --- /dev/null +++ b/audio/decode/ad_pcm.c @@ -0,0 +1,220 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdbool.h> + +#include <libavutil/common.h> + +#include "talloc.h" +#include "config.h" +#include "ad_internal.h" +#include "libaf/format.h" +#include "libaf/reorder_ch.h" + +static const ad_info_t info = { + "Uncompressed PCM audio decoder", + "pcm", + "Nick Kurshev", + "A'rpi", + "" +}; + +struct ad_pcm_context { + unsigned char *buffer; + int buffer_pos; + int buffer_len; + int buffer_size; +}; + +LIBAD_EXTERN(pcm) + +static int init(sh_audio_t * sh_audio) +{ + WAVEFORMATEX *h = sh_audio->wf; + if (!h) + return 0; + sh_audio->i_bps = h->nAvgBytesPerSec; + sh_audio->channels = h->nChannels; + sh_audio->samplerate = h->nSamplesPerSec; + sh_audio->samplesize = (h->wBitsPerSample + 7) / 8; + sh_audio->sample_format = AF_FORMAT_S16_LE; // default + switch (sh_audio->format) { /* hardware formats: */ + case 0x0: + case 0x1: // Microsoft PCM + case 0xfffe: // Extended + switch (sh_audio->samplesize) { + case 1: sh_audio->sample_format = AF_FORMAT_U8; break; + case 2: sh_audio->sample_format = AF_FORMAT_S16_LE; break; + case 3: sh_audio->sample_format = AF_FORMAT_S24_LE; break; + case 4: sh_audio->sample_format = AF_FORMAT_S32_LE; break; + } + break; + case 0x3: // IEEE float + sh_audio->sample_format = AF_FORMAT_FLOAT_LE; + break; + case 0x6: sh_audio->sample_format = AF_FORMAT_A_LAW; break; + case 0x7: sh_audio->sample_format = AF_FORMAT_MU_LAW; break; + case 0x11: sh_audio->sample_format = AF_FORMAT_IMA_ADPCM; break; + case 0x50: sh_audio->sample_format = AF_FORMAT_MPEG2; break; +/* case 0x2000: sh_audio->sample_format=AFMT_AC3; */ + case 0x20776172: // 'raw ' + sh_audio->sample_format = AF_FORMAT_S16_BE; + if (sh_audio->samplesize == 1) + sh_audio->sample_format = AF_FORMAT_U8; + break; + case 0x736F7774: // 'twos' + sh_audio->sample_format = AF_FORMAT_S16_BE; + // intended fall-through + case 0x74776F73: // 'sowt' + if (sh_audio->samplesize == 1) + sh_audio->sample_format = AF_FORMAT_S8; + break; + case 0x32336c66: // 'fl32', bigendian float32 + case 0x32334C46: // 'FL32', bigendian float32 in aiff + sh_audio->sample_format = AF_FORMAT_FLOAT_BE; + sh_audio->samplesize = 4; + break; + case 0x666c3332: // '23lf', little endian float32, MPlayer internal fourCC + case 0x6D63706C: // 'lpcm' + sh_audio->sample_format = AF_FORMAT_FLOAT_LE; + sh_audio->samplesize = 4; + break; +/* case 0x34366c66: // 'fl64', bigendian float64 + sh_audio->sample_format=AF_FORMAT_FLOAT_BE; + sh_audio->samplesize=8; + break; + case 0x666c3634: // '46lf', little endian float64, MPlayer internal fourCC + sh_audio->sample_format=AF_FORMAT_FLOAT_LE; + sh_audio->samplesize=8; + break;*/ + case 0x34326e69: // 'in24', bigendian int24 + sh_audio->sample_format = AF_FORMAT_S24_BE; + sh_audio->samplesize = 3; + break; + case 0x696e3234: // '42ni', little endian int24, MPlayer internal fourCC + sh_audio->sample_format = AF_FORMAT_S24_LE; + sh_audio->samplesize = 3; + break; + case 0x32336e69: // 'in32', bigendian int32 + sh_audio->sample_format = AF_FORMAT_S32_BE; + sh_audio->samplesize = 4; + break; + case 0x696e3332: // '23ni', little endian int32, MPlayer internal fourCC + sh_audio->sample_format = AF_FORMAT_S32_LE; + sh_audio->samplesize = 4; + break; + case MKTAG('M', 'P', 'a', 'f'): + sh_audio->sample_format = h->wFormatTag; + sh_audio->samplesize = (af_fmt2bits(sh_audio->sample_format) + 7) / 8; + break; + default: + if (sh_audio->samplesize != 2) + sh_audio->sample_format = AF_FORMAT_U8; + } + if (!sh_audio->samplesize) // this would cause MPlayer to hang later + sh_audio->samplesize = 2; + sh_audio->context = talloc_zero(NULL, struct ad_pcm_context); + return 1; +} + +static int preinit(sh_audio_t *sh) +{ + sh->audio_out_minsize = 2048; + return 1; +} + +static void uninit(sh_audio_t *sh) +{ + talloc_free(sh->context); +} + +static int control(sh_audio_t *sh, int cmd, void *arg, ...) +{ + struct ad_pcm_context *ctx = sh->context; + int skip; + switch (cmd) { + case ADCTRL_RESYNC_STREAM: + ctx->buffer_len = 0; + return true; + case ADCTRL_SKIP_FRAME: + skip = sh->i_bps / 16; + skip = skip & (~3); + demux_read_data(sh->ds, NULL, skip); + return CONTROL_TRUE; + } + return CONTROL_UNKNOWN; +} + +static int decode_audio(sh_audio_t *sh_audio, unsigned char *buf, int minlen, + int maxlen) +{ + int unitsize = sh_audio->channels * sh_audio->samplesize; + minlen = (minlen + unitsize - 1) / unitsize * unitsize; + if (minlen > maxlen) + // if someone needs hundreds of channels adjust audio_out_minsize + // based on channels in preinit() + return -1; + + int len = 0; + struct ad_pcm_context *ctx = sh_audio->context; + while (len < minlen) { + if (ctx->buffer_len - ctx->buffer_pos <= 0) { + double pts; + unsigned char *ptr; + int plen = ds_get_packet_pts(sh_audio->ds, &ptr, &pts); + if (plen < 0) + break; + if (ctx->buffer_size < plen) { + talloc_free(ctx->buffer); + ctx->buffer = talloc_size(ctx, plen); + ctx->buffer_size = plen; + } + memcpy(ctx->buffer, ptr, plen); + ctx->buffer_len = plen; + ctx->buffer_pos = 0; + if (pts != MP_NOPTS_VALUE) { + sh_audio->pts = pts; + sh_audio->pts_bytes = 0; + } + } + int from_stored = ctx->buffer_len - ctx->buffer_pos; + if (from_stored > minlen - len) + from_stored = minlen - len; + memcpy(buf + len, ctx->buffer + ctx->buffer_pos, from_stored); + ctx->buffer_pos += from_stored; + sh_audio->pts_bytes += from_stored; + len += from_stored; + } + if (len % unitsize) { + mp_msg(MSGT_DECAUDIO, MSGL_WARN, "[ad_pcm] discarding partial sample " + "at end\n"); + len -= len % unitsize; + } + if (len == 0) + len = -1; // The loop above only exits at error/EOF + if (len > 0 && sh_audio->channels >= 5) { + reorder_channel_nch(buf, AF_CHANNEL_LAYOUT_WAVEEX_DEFAULT, + AF_CHANNEL_LAYOUT_MPLAYER_DEFAULT, + sh_audio->channels, len / sh_audio->samplesize, + sh_audio->samplesize); + } + return len; +} diff --git a/audio/decode/ad_spdif.c b/audio/decode/ad_spdif.c new file mode 100644 index 0000000000..877bc99317 --- /dev/null +++ b/audio/decode/ad_spdif.c @@ -0,0 +1,310 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <string.h> + +#include <libavformat/avformat.h> +#include <libavcodec/avcodec.h> +#include <libavutil/opt.h> + +#include "config.h" +#include "mp_msg.h" +#include "ad_internal.h" + +static const ad_info_t info = { + "libavformat/spdifenc audio pass-through decoder.", + "spdif", + "Naoya OYAMA", + "Naoya OYAMA", + "For ALL hardware decoders" +}; + +LIBAD_EXTERN(spdif) + +#define FILENAME_SPDIFENC "spdif" +#define OUTBUF_SIZE 65536 +struct spdifContext { + AVFormatContext *lavf_ctx; + int iec61937_packet_size; + int out_buffer_len; + int out_buffer_size; + uint8_t *out_buffer; + uint8_t pb_buffer[OUTBUF_SIZE]; +}; + +static int read_packet(void *p, uint8_t *buf, int buf_size) +{ + // spdifenc does not use read callback. + return 0; +} + +static int write_packet(void *p, uint8_t *buf, int buf_size) +{ + int len; + struct spdifContext *ctx = p; + + len = FFMIN(buf_size, ctx->out_buffer_size -ctx->out_buffer_len); + memcpy(&ctx->out_buffer[ctx->out_buffer_len], buf, len); + ctx->out_buffer_len += len; + return len; +} + +static int64_t seek(void *p, int64_t offset, int whence) +{ + // spdifenc does not use seek callback. + return 0; +} + +static int preinit(sh_audio_t *sh) +{ + sh->samplesize = 2; + return 1; +} + +static int init(sh_audio_t *sh) +{ + int i, x, in_size, srate, bps, *dtshd_rate; + unsigned char *start; + double pts; + static const struct { + const char *name; enum CodecID id; + } fmt_id_type[] = { + { "aac" , CODEC_ID_AAC }, + { "ac3" , CODEC_ID_AC3 }, + { "dca" , CODEC_ID_DTS }, + { "eac3", CODEC_ID_EAC3 }, + { "mpa" , CODEC_ID_MP3 }, + { "thd" , CODEC_ID_TRUEHD }, + { NULL , 0 } + }; + AVFormatContext *lavf_ctx = NULL; + AVStream *stream = NULL; + const AVOption *opt = NULL; + struct spdifContext *spdif_ctx = NULL; + + spdif_ctx = av_mallocz(sizeof(*spdif_ctx)); + if (!spdif_ctx) + goto fail; + spdif_ctx->lavf_ctx = avformat_alloc_context(); + if (!spdif_ctx->lavf_ctx) + goto fail; + + sh->context = spdif_ctx; + lavf_ctx = spdif_ctx->lavf_ctx; + + lavf_ctx->oformat = av_guess_format(FILENAME_SPDIFENC, NULL, NULL); + if (!lavf_ctx->oformat) + goto fail; + lavf_ctx->priv_data = av_mallocz(lavf_ctx->oformat->priv_data_size); + if (!lavf_ctx->priv_data) + goto fail; + lavf_ctx->pb = avio_alloc_context(spdif_ctx->pb_buffer, OUTBUF_SIZE, 1, spdif_ctx, + read_packet, write_packet, seek); + if (!lavf_ctx->pb) + goto fail; + stream = avformat_new_stream(lavf_ctx, 0); + if (!stream) + goto fail; + lavf_ctx->duration = AV_NOPTS_VALUE; + lavf_ctx->start_time = AV_NOPTS_VALUE; + for (i = 0; fmt_id_type[i].name; i++) { + if (!strcmp(sh->codec->dll, fmt_id_type[i].name)) { + lavf_ctx->streams[0]->codec->codec_id = fmt_id_type[i].id; + break; + } + } + lavf_ctx->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE; + if (AVERROR_PATCHWELCOME == lavf_ctx->oformat->write_header(lavf_ctx)) { + mp_msg(MSGT_DECAUDIO,MSGL_INFO, + "This codec is not supported by spdifenc.\n"); + goto fail; + } + + // get sample_rate & bitrate from parser + bps = srate = 0; + x = ds_get_packet_pts(sh->ds, &start, &pts); + in_size = x; + if (x <= 0) { + pts = MP_NOPTS_VALUE; + x = 0; + } + ds_parse(sh->ds, &start, &x, pts, 0); + if (x == 0) { // not enough buffer + srate = 48000; //fake value + bps = 768000/8; //fake value + } else if (sh->avctx) { + if (sh->avctx->sample_rate < 44100) { + mp_msg(MSGT_DECAUDIO,MSGL_INFO, + "This stream sample_rate[%d Hz] may be broken. " + "Force reset 48000Hz.\n", + sh->avctx->sample_rate); + srate = 48000; //fake value + } else + srate = sh->avctx->sample_rate; + bps = sh->avctx->bit_rate/8; + } + sh->ds->buffer_pos -= in_size; + + switch (lavf_ctx->streams[0]->codec->codec_id) { + case CODEC_ID_AAC: + spdif_ctx->iec61937_packet_size = 16384; + sh->sample_format = AF_FORMAT_IEC61937_LE; + sh->samplerate = srate; + sh->channels = 2; + sh->i_bps = bps; + break; + case CODEC_ID_AC3: + spdif_ctx->iec61937_packet_size = 6144; + sh->sample_format = AF_FORMAT_IEC61937_LE; + sh->samplerate = srate; + sh->channels = 2; + sh->i_bps = bps; + break; + case CODEC_ID_DTS: // FORCE USE DTS-HD + opt = av_opt_find(&lavf_ctx->oformat->priv_class, + "dtshd_rate", NULL, 0, 0); + if (!opt) + goto fail; + dtshd_rate = (int*)(((uint8_t*)lavf_ctx->priv_data) + + opt->offset); + *dtshd_rate = 192000*4; + spdif_ctx->iec61937_packet_size = 32768; + sh->sample_format = AF_FORMAT_IEC61937_LE; + sh->samplerate = 192000; // DTS core require 48000 + sh->channels = 2*4; + sh->i_bps = bps; + break; + case CODEC_ID_EAC3: + spdif_ctx->iec61937_packet_size = 24576; + sh->sample_format = AF_FORMAT_IEC61937_LE; + sh->samplerate = 192000; + sh->channels = 2; + sh->i_bps = bps; + break; + case CODEC_ID_MP3: + spdif_ctx->iec61937_packet_size = 4608; + sh->sample_format = AF_FORMAT_MPEG2; + sh->samplerate = srate; + sh->channels = 2; + sh->i_bps = bps; + break; + case CODEC_ID_TRUEHD: + spdif_ctx->iec61937_packet_size = 61440; + sh->sample_format = AF_FORMAT_IEC61937_LE; + sh->samplerate = 192000; + sh->channels = 8; + sh->i_bps = bps; + break; + default: + break; + } + + return 1; + +fail: + uninit(sh); + return 0; +} + +static int decode_audio(sh_audio_t *sh, unsigned char *buf, + int minlen, int maxlen) +{ + struct spdifContext *spdif_ctx = sh->context; + AVFormatContext *lavf_ctx = spdif_ctx->lavf_ctx; + AVPacket pkt; + double pts; + int ret, in_size, consumed, x; + unsigned char *start = NULL; + + consumed = spdif_ctx->out_buffer_len = 0; + spdif_ctx->out_buffer_size = maxlen; + spdif_ctx->out_buffer = buf; + while (spdif_ctx->out_buffer_len + spdif_ctx->iec61937_packet_size < maxlen + && spdif_ctx->out_buffer_len < minlen) { + if (sh->ds->eof) + break; + x = ds_get_packet_pts(sh->ds, &start, &pts); + if (x <= 0) { + x = 0; + ds_parse(sh->ds, &start, &x, MP_NOPTS_VALUE, 0); + if (x == 0) + continue; // END_NOT_FOUND + in_size = x; + } else { + in_size = x; + consumed = ds_parse(sh->ds, &start, &x, pts, 0); + if (x == 0) { + mp_msg(MSGT_DECAUDIO,MSGL_V, + "start[%p] in_size[%d] consumed[%d] x[%d].\n", + start, in_size, consumed, x); + continue; // END_NOT_FOUND + } + sh->ds->buffer_pos -= in_size - consumed; + } + av_init_packet(&pkt); + pkt.data = start; + pkt.size = x; + mp_msg(MSGT_DECAUDIO,MSGL_V, + "start[%p] pkt.size[%d] in_size[%d] consumed[%d] x[%d].\n", + start, pkt.size, in_size, consumed, x); + if (pts != MP_NOPTS_VALUE) { + sh->pts = pts; + sh->pts_bytes = 0; + } + ret = lavf_ctx->oformat->write_packet(lavf_ctx, &pkt); + if (ret < 0) + break; + } + sh->pts_bytes += spdif_ctx->out_buffer_len; + return spdif_ctx->out_buffer_len; +} + +static int control(sh_audio_t *sh, int cmd, void* arg, ...) +{ + unsigned char *start; + double pts; + + switch (cmd) { + case ADCTRL_RESYNC_STREAM: + case ADCTRL_SKIP_FRAME: + ds_get_packet_pts(sh->ds, &start, &pts); + return CONTROL_TRUE; + } + return CONTROL_UNKNOWN; +} + +static void uninit(sh_audio_t *sh) +{ + struct spdifContext *spdif_ctx = sh->context; + AVFormatContext *lavf_ctx = spdif_ctx->lavf_ctx; + + if (lavf_ctx) { + if (lavf_ctx->oformat) + lavf_ctx->oformat->write_trailer(lavf_ctx); + av_freep(&lavf_ctx->pb); + if (lavf_ctx->streams) { + av_freep(&lavf_ctx->streams[0]->codec); + av_freep(&lavf_ctx->streams[0]->info); + av_freep(&lavf_ctx->streams[0]); + } + av_freep(&lavf_ctx->streams); + av_freep(&lavf_ctx->priv_data); + } + av_freep(&lavf_ctx); + av_freep(&spdif_ctx); +} diff --git a/audio/decode/dec_audio.c b/audio/decode/dec_audio.c new file mode 100644 index 0000000000..2602352e52 --- /dev/null +++ b/audio/decode/dec_audio.c @@ -0,0 +1,462 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <assert.h> + +#include "config.h" +#include "mp_msg.h" +#include "bstr.h" + +#include "stream/stream.h" +#include "libmpdemux/demuxer.h" + +#include "codec-cfg.h" +#include "libmpdemux/stheader.h" + +#include "dec_audio.h" +#include "ad.h" +#include "libaf/format.h" + +#include "libaf/af.h" + +int fakemono = 0; + +struct af_cfg af_cfg = { 1, NULL }; // Configuration for audio filters + +void afm_help(void) +{ + int i; + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Available (compiled-in) audio codec families/drivers:\n"); + mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_AUDIO_DRIVERS\n"); + mp_msg(MSGT_DECAUDIO, MSGL_INFO, " afm: info: (comment)\n"); + for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++) + if (mpcodecs_ad_drivers[i]->info->comment + && mpcodecs_ad_drivers[i]->info->comment[0]) + mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s (%s)\n", + mpcodecs_ad_drivers[i]->info->short_name, + mpcodecs_ad_drivers[i]->info->name, + mpcodecs_ad_drivers[i]->info->comment); + else + mp_msg(MSGT_DECAUDIO, MSGL_INFO, "%9s %s\n", + mpcodecs_ad_drivers[i]->info->short_name, + mpcodecs_ad_drivers[i]->info->name); +} + +static int init_audio_codec(sh_audio_t *sh_audio) +{ + assert(!sh_audio->initialized); + resync_audio_stream(sh_audio); + if ((af_cfg.force & AF_INIT_FORMAT_MASK) == AF_INIT_FLOAT) { + int fmt = AF_FORMAT_FLOAT_NE; + if (sh_audio->ad_driver->control(sh_audio, ADCTRL_QUERY_FORMAT, + &fmt) == CONTROL_TRUE) { + sh_audio->sample_format = fmt; + sh_audio->samplesize = 4; + } + } + sh_audio->audio_out_minsize = 8192; // default, preinit() may change it + if (!sh_audio->ad_driver->preinit(sh_audio)) { + mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "ADecoder preinit failed :(\n"); + return 0; + } + + /* allocate audio in buffer: */ + if (sh_audio->audio_in_minsize > 0) { + sh_audio->a_in_buffer_size = sh_audio->audio_in_minsize; + mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d bytes for input buffer.\n", + sh_audio->a_in_buffer_size); + sh_audio->a_in_buffer = av_mallocz(sh_audio->a_in_buffer_size); + } + + const int base_size = 65536; + // At least 64 KiB plus rounding up to next decodable unit size + sh_audio->a_buffer_size = base_size + sh_audio->audio_out_minsize; + + mp_tmsg(MSGT_DECAUDIO, MSGL_V, "dec_audio: Allocating %d + %d = %d bytes for output buffer.\n", + sh_audio->audio_out_minsize, base_size, sh_audio->a_buffer_size); + + sh_audio->a_buffer = av_mallocz(sh_audio->a_buffer_size); + if (!sh_audio->a_buffer) + abort(); + sh_audio->a_buffer_len = 0; + + if (!sh_audio->ad_driver->init(sh_audio)) { + mp_tmsg(MSGT_DECAUDIO, MSGL_V, "ADecoder init failed :(\n"); + uninit_audio(sh_audio); // free buffers + return 0; + } + + sh_audio->initialized = 1; + + if (!sh_audio->channels || !sh_audio->samplerate) { + mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Audio decoder did not specify " + "audio format!\n"); + uninit_audio(sh_audio); // free buffers + return 0; + } + + if (!sh_audio->o_bps) + sh_audio->o_bps = sh_audio->channels * sh_audio->samplerate + * sh_audio->samplesize; + return 1; +} + +static int init_audio(sh_audio_t *sh_audio, char *codecname, char *afm, + int status, stringset_t *selected) +{ + int force = 0; + if (codecname && codecname[0] == '+') { + codecname = &codecname[1]; + force = 1; + } + sh_audio->codec = NULL; + while (1) { + const ad_functions_t *mpadec; + sh_audio->ad_driver = 0; + if (!(sh_audio->codec = find_audio_codec(sh_audio->format, + NULL, + sh_audio->codec, force))) + break; + // ok we found one codec + if (stringset_test(selected, sh_audio->codec->name)) + continue; // already tried & failed + if (codecname && strcmp(sh_audio->codec->name, codecname)) + continue; // -ac + if (afm && strcmp(sh_audio->codec->drv, afm)) + continue; // afm doesn't match + if (!force && sh_audio->codec->status < status) + continue; // too unstable + stringset_add(selected, sh_audio->codec->name); // tagging it + // ok, it matches all rules, let's find the driver! + int i; + for (i = 0; mpcodecs_ad_drivers[i] != NULL; i++) + if (!strcmp(mpcodecs_ad_drivers[i]->info->short_name, + sh_audio->codec->drv)) + break; + mpadec = mpcodecs_ad_drivers[i]; + if (!mpadec) { // driver not available (==compiled in) + mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, + "Requested audio codec family [%s] (afm=%s) not available.\nEnable it at compilation.\n", + sh_audio->codec->name, sh_audio->codec->drv); + continue; + } + // it's available, let's try to init! + // init() + mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Opening audio decoder: [%s] %s\n", + mpadec->info->short_name, mpadec->info->name); + sh_audio->ad_driver = mpadec; + if (!init_audio_codec(sh_audio)) { + mp_tmsg(MSGT_DECAUDIO, MSGL_WARN, "Audio decoder init failed for " + "codecs.conf entry \"%s\".\n", sh_audio->codec->name); + continue; // try next... + } + // Yeah! We got it! + return 1; + } + return 0; +} + +int init_best_audio_codec(sh_audio_t *sh_audio, char **audio_codec_list, + char **audio_fm_list) +{ + stringset_t selected; + char *ac_l_default[2] = { "", (char *) NULL }; + // hack: + if (!audio_codec_list) + audio_codec_list = ac_l_default; + // Go through the codec.conf and find the best codec... + sh_audio->initialized = 0; + stringset_init(&selected); + while (!sh_audio->initialized && *audio_codec_list) { + char *audio_codec = *(audio_codec_list++); + if (audio_codec[0]) { + if (audio_codec[0] == '-') { + // disable this codec: + stringset_add(&selected, audio_codec + 1); + } else { + // forced codec by name: + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Forced audio codec: %s\n", + audio_codec); + init_audio(sh_audio, audio_codec, NULL, -1, &selected); + } + } else { + int status; + // try in stability order: UNTESTED, WORKING, BUGGY. + // never try CRASHING. + if (audio_fm_list) { + char **fmlist = audio_fm_list; + // try first the preferred codec families: + while (!sh_audio->initialized && *fmlist) { + char *audio_fm = *(fmlist++); + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Trying to force audio codec driver family %s...\n", + audio_fm); + for (status = CODECS_STATUS__MAX; + status >= CODECS_STATUS__MIN; --status) + if (init_audio(sh_audio, NULL, audio_fm, status, &selected)) + break; + } + } + if (!sh_audio->initialized) + for (status = CODECS_STATUS__MAX; status >= CODECS_STATUS__MIN; + --status) + if (init_audio(sh_audio, NULL, NULL, status, &selected)) + break; + } + } + stringset_free(&selected); + + if (!sh_audio->initialized) { + mp_tmsg(MSGT_DECAUDIO, MSGL_ERR, "Cannot find codec for audio format 0x%X.\n", + sh_audio->format); + return 0; // failed + } + + mp_tmsg(MSGT_DECAUDIO, MSGL_INFO, "Selected audio codec: %s [%s]\n", + sh_audio->codecname ? sh_audio->codecname : sh_audio->codec->info, + sh_audio->ad_driver->info->print_name ? + sh_audio->ad_driver->info->print_name : + sh_audio->ad_driver->info->short_name); + mp_tmsg(MSGT_DECAUDIO, MSGL_V, + "Audio codecs.conf entry: %s (%s) afm: %s\n", + sh_audio->codec->name, sh_audio->codec->info, sh_audio->codec->drv); + mp_msg(MSGT_DECAUDIO, MSGL_V, + "AUDIO: %d Hz, %d ch, %s, %3.1f kbit/%3.2f%% (ratio: %d->%d)\n", + sh_audio->samplerate, sh_audio->channels, + af_fmt2str_short(sh_audio->sample_format), + sh_audio->i_bps * 8 * 0.001, + ((float) sh_audio->i_bps / sh_audio->o_bps) * 100.0, + sh_audio->i_bps, sh_audio->o_bps); + mp_msg(MSGT_IDENTIFY, MSGL_INFO, + "ID_AUDIO_BITRATE=%d\nID_AUDIO_RATE=%d\n" "ID_AUDIO_NCH=%d\n", + sh_audio->i_bps * 8, sh_audio->samplerate, sh_audio->channels); + + return 1; // success +} + +void uninit_audio(sh_audio_t *sh_audio) +{ + if (sh_audio->afilter) { + mp_msg(MSGT_DECAUDIO, MSGL_V, "Uninit audio filters...\n"); + af_uninit(sh_audio->afilter); + free(sh_audio->afilter); + sh_audio->afilter = NULL; + } + if (sh_audio->initialized) { + mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Uninit audio: %s\n", + sh_audio->codec->drv); + sh_audio->ad_driver->uninit(sh_audio); + sh_audio->initialized = 0; + } + av_freep(&sh_audio->a_buffer); + av_freep(&sh_audio->a_in_buffer); +} + + +int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate, + int *out_samplerate, int *out_channels, int *out_format) +{ + struct af_stream *afs = sh_audio->afilter; + if (!afs) { + afs = calloc(1, sizeof(struct af_stream)); + afs->opts = sh_audio->opts; + } + // input format: same as codec's output format: + afs->input.rate = in_samplerate; + afs->input.nch = sh_audio->channels; + afs->input.format = sh_audio->sample_format; + af_fix_parameters(&(afs->input)); + + // output format: same as ao driver's input format (if missing, fallback to input) + afs->output.rate = *out_samplerate; + afs->output.nch = *out_channels; + afs->output.format = *out_format; + af_fix_parameters(&(afs->output)); + + // filter config: + memcpy(&afs->cfg, &af_cfg, sizeof(struct af_cfg)); + + mp_tmsg(MSGT_DECAUDIO, MSGL_V, "Building audio filter chain for %dHz/%dch/%s -> %dHz/%dch/%s...\n", + afs->input.rate, afs->input.nch, + af_fmt2str_short(afs->input.format), afs->output.rate, + afs->output.nch, af_fmt2str_short(afs->output.format)); + + // let's autoprobe it! + if (0 != af_init(afs)) { + sh_audio->afilter = NULL; + free(afs); + return 0; // failed :( + } + + *out_samplerate = afs->output.rate; + *out_channels = afs->output.nch; + *out_format = afs->output.format; + + // ok! + sh_audio->afilter = (void *) afs; + return 1; +} + +static void set_min_out_buffer_size(struct bstr *outbuf, int len) +{ + size_t oldlen = talloc_get_size(outbuf->start); + if (oldlen < len) { + assert(outbuf->start); // talloc context should be already set + mp_msg(MSGT_DECAUDIO, MSGL_V, "Increasing filtered audio buffer size " + "from %zd to %d\n", oldlen, len); + outbuf->start = talloc_realloc_size(NULL, outbuf->start, len); + } +} + +static int filter_n_bytes(sh_audio_t *sh, struct bstr *outbuf, int len) +{ + assert(len-1 + sh->audio_out_minsize <= sh->a_buffer_size); + + int error = 0; + + // Decode more bytes if needed + int old_samplerate = sh->samplerate; + int old_channels = sh->channels; + int old_sample_format = sh->sample_format; + while (sh->a_buffer_len < len) { + unsigned char *buf = sh->a_buffer + sh->a_buffer_len; + int minlen = len - sh->a_buffer_len; + int maxlen = sh->a_buffer_size - sh->a_buffer_len; + int ret = sh->ad_driver->decode_audio(sh, buf, minlen, maxlen); + int format_change = sh->samplerate != old_samplerate + || sh->channels != old_channels + || sh->sample_format != old_sample_format; + if (ret <= 0 || format_change) { + error = format_change ? -2 : -1; + // samples from format-changing call get discarded too + len = sh->a_buffer_len; + break; + } + sh->a_buffer_len += ret; + } + + // Filter + struct mp_audio filter_input = { + .audio = sh->a_buffer, + .len = len, + .rate = sh->samplerate, + .nch = sh->channels, + .format = sh->sample_format + }; + af_fix_parameters(&filter_input); + struct mp_audio *filter_output = af_play(sh->afilter, &filter_input); + if (!filter_output) + return -1; + set_min_out_buffer_size(outbuf, outbuf->len + filter_output->len); + memcpy(outbuf->start + outbuf->len, filter_output->audio, + filter_output->len); + outbuf->len += filter_output->len; + + // remove processed data from decoder buffer: + sh->a_buffer_len -= len; + memmove(sh->a_buffer, sh->a_buffer + len, sh->a_buffer_len); + + return error; +} + +/* Try to get at least minlen decoded+filtered bytes in outbuf + * (total length including possible existing data). + * Return 0 on success, -1 on error/EOF (not distinguished). + * In the former case outbuf->len is always >= minlen on return. + * In case of EOF/error it might or might not be. + * Outbuf.start must be talloc-allocated, and will be reallocated + * if needed to fit all filter output. */ +int decode_audio(sh_audio_t *sh_audio, struct bstr *outbuf, int minlen) +{ + // Indicates that a filter seems to be buffering large amounts of data + int huge_filter_buffer = 0; + // Decoded audio must be cut at boundaries of this many bytes + int unitsize = sh_audio->channels * sh_audio->samplesize * 16; + + /* Filter output size will be about filter_multiplier times input size. + * If some filter buffers audio in big blocks this might only hold + * as average over time. */ + double filter_multiplier = af_calc_filter_multiplier(sh_audio->afilter); + + /* If the decoder set audio_out_minsize then it can do the equivalent of + * "while (output_len < target_len) output_len += audio_out_minsize;", + * so we must guarantee there is at least audio_out_minsize-1 bytes + * more space in the output buffer than the minimum length we try to + * decode. */ + int max_decode_len = sh_audio->a_buffer_size - sh_audio->audio_out_minsize; + if (!unitsize) + return -1; + max_decode_len -= max_decode_len % unitsize; + + while (outbuf->len < minlen) { + int declen = (minlen - outbuf->len) / filter_multiplier + + (unitsize << 5); // some extra for possible filter buffering + if (huge_filter_buffer) + /* Some filter must be doing significant buffering if the estimated + * input length didn't produce enough output from filters. + * Feed the filters 2k bytes at a time until we have enough output. + * Very small amounts could make filtering inefficient while large + * amounts can make MPlayer demux the file unnecessarily far ahead + * to get audio data and buffer video frames in memory while doing + * so. However the performance impact of either is probably not too + * significant as long as the value is not completely insane. */ + declen = 2000; + declen -= declen % unitsize; + if (declen > max_decode_len) + declen = max_decode_len; + else + /* if this iteration does not fill buffer, we must have lots + * of buffering in filters */ + huge_filter_buffer = 1; + int res = filter_n_bytes(sh_audio, outbuf, declen); + if (res < 0) + return res; + } + return 0; +} + +void decode_audio_prepend_bytes(struct bstr *outbuf, int count, int byte) +{ + set_min_out_buffer_size(outbuf, outbuf->len + count); + memmove(outbuf->start + count, outbuf->start, outbuf->len); + memset(outbuf->start, byte, count); + outbuf->len += count; +} + + +void resync_audio_stream(sh_audio_t *sh_audio) +{ + sh_audio->a_in_buffer_len = 0; // clear audio input buffer + sh_audio->pts = MP_NOPTS_VALUE; + if (!sh_audio->initialized) + return; + sh_audio->ad_driver->control(sh_audio, ADCTRL_RESYNC_STREAM, NULL); +} + +void skip_audio_frame(sh_audio_t *sh_audio) +{ + if (!sh_audio->initialized) + return; + if (sh_audio->ad_driver->control(sh_audio, ADCTRL_SKIP_FRAME, NULL) == + CONTROL_TRUE) + return; + // default skip code: + ds_fill_buffer(sh_audio->ds); // skip block +} diff --git a/audio/decode/dec_audio.h b/audio/decode/dec_audio.h new file mode 100644 index 0000000000..0d4baf0666 --- /dev/null +++ b/audio/decode/dec_audio.h @@ -0,0 +1,38 @@ +/* + * This file is part of MPlayer. + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPLAYER_DEC_AUDIO_H +#define MPLAYER_DEC_AUDIO_H + +#include "libmpdemux/stheader.h" + +struct bstr; + +// dec_audio.c: +void afm_help(void); +int init_best_audio_codec(sh_audio_t *sh_audio, char** audio_codec_list, char** audio_fm_list); +int decode_audio(sh_audio_t *sh_audio, struct bstr *outbuf, int minlen); +void decode_audio_prepend_bytes(struct bstr *outbuf, int count, int byte); +void resync_audio_stream(sh_audio_t *sh_audio); +void skip_audio_frame(sh_audio_t *sh_audio); +void uninit_audio(sh_audio_t *sh_audio); + +int init_audio_filters(sh_audio_t *sh_audio, int in_samplerate, + int *out_samplerate, int *out_channels, int *out_format); + +#endif /* MPLAYER_DEC_AUDIO_H */ |