diff --git a/Makefile.am b/Makefile.am index a961565..97f17b5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -40,7 +40,7 @@ libstb_hal_test_LDADD += \ @AVFORMAT_LIBS@ \ @AVUTIL_LIBS@ \ @AVCODEC_LIBS@ \ - @AVRESAMPLE_LIBS@ \ + @SWRESAMPLE_LIBS@ \ @SWSCALE_LIBS@ endif if BOXTYPE_SPARK diff --git a/configure.ac b/configure.ac index ce2766a..88dbf02 100644 --- a/configure.ac +++ b/configure.ac @@ -29,7 +29,7 @@ if test x$BOXTYPE = xgeneric; then # don't know which version is exactly needed here... PKG_CHECK_MODULES([AVUTIL], [libavutil]) PKG_CHECK_MODULES([SWSCALE], [libswscale]) - PKG_CHECK_MODULES([AVRESAMPLE], [libavresample]) + PKG_CHECK_MODULES([SWRESAMPLE], [libswresample]) fi AC_OUTPUT([ Makefile diff --git a/generic-pc/audio.cpp b/generic-pc/audio.cpp index 284bc1e..e314601 100644 --- a/generic-pc/audio.cpp +++ b/generic-pc/audio.cpp @@ -35,8 +35,13 @@ extern "C" { #include #include #ifdef MARTII -#include -#include +#define USE_LIBSWRESAMPLE +# ifdef USE_LIBSWRESAMPLE +# include +# else +# include +# endif +# include #endif } #ifdef MARTII @@ -291,7 +296,11 @@ void cAudio::run() // ao_device *adevice; // ao_sample_format sformat; #ifdef MARTII +#ifdef USE_LIBSWRESAMPLE + SwrContext *swr = NULL; +#else AVAudioResampleContext *avr = NULL; +#endif int e; #endif @@ -364,6 +373,20 @@ void cAudio::run() fprintf(stderr, "\n"); #endif #ifdef MARTII +# ifdef USE_LIBSWRESAMPLE + swr = swr_alloc(); + av_opt_set_int(swr, "in_channel_layout", c->channel_layout, 0); + av_opt_set_int(swr, "out_channel_layout", c->channel_layout, 0); + av_opt_set_int(swr, "in_sample_rate", c->sample_rate, 0); + av_opt_set_int(swr, "out_sample_rate", c->sample_rate, 0); + av_opt_set_int(swr, "in_sample_fmt", c->sample_fmt, 0); + av_opt_set_int(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); + e = swr_init(swr); + if (e < 0) { + lt_info("%s: swr_init failed: %d\n", __func__, -e); + goto out2; + } +# else avr = avresample_alloc_context(); av_opt_set_int(avr, "in_channel_layout", c->channel_layout, 0); av_opt_set_int(avr, "out_channel_layout", c->channel_layout, 0); @@ -376,6 +399,7 @@ void cAudio::run() lt_info("%s: avresample_open failed: %d\n", __func__, -e); goto out2; } +# endif #endif lt_info("codec params: sample_fmt %d sample_rate %d channels %d\n", c->sample_fmt, c->sample_rate, c->channels); @@ -389,6 +413,17 @@ void cAudio::run() lt_debug("%s: pts 0x%" PRIx64 " %3f\n", __func__, curr_pts, curr_pts/90000.0); #ifdef MARTII uint8_t *output = NULL; +# ifdef USE_LIBSWRESAMPLE + int out_samples = av_rescale_rnd(swr_get_delay(swr, c->sample_rate) + frame->nb_samples, + c->sample_rate, c->sample_rate, AV_ROUND_UP); + e = av_samples_alloc(&output, NULL, c->channels, out_samples, AV_SAMPLE_FMT_S16, 1); + if (e < 0) { + lt_info("%s: av_samples_alloc failed: %d\n", __func__, -e); + goto out2; + } + out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **) &frame->data[0], frame->nb_samples); +# else + uint8_t *output = NULL; int out_linesize; int out_samples = avresample_available(avr) + av_rescale_rnd(avresample_get_delay(avr) + frame->nb_samples, @@ -400,6 +435,7 @@ void cAudio::run() } out_samples = avresample_convert(avr, &output, out_linesize, out_samples, &frame->data[0], frame->linesize[0], frame->nb_samples); +# endif ao_play(adevice, (char *)output, out_samples * 2 /* 16 bit */ * c->channels); av_freep(&output); #else @@ -414,10 +450,16 @@ void cAudio::run() avcodec_close(c); out: #ifdef MARTII +# ifdef USE_LIBSWRESAMPLE + if (swr) { + swr_free(&swr); + } +# else if (avr) { avresample_close(avr); avresample_free(&avr); } +# endif #endif avformat_close_input(&avfc); av_free(pIOCtx->buffer); diff --git a/libeplayer3/Makefile.am b/libeplayer3/Makefile.am index e8fcc82..515152b 100644 --- a/libeplayer3/Makefile.am +++ b/libeplayer3/Makefile.am @@ -29,7 +29,7 @@ AM_CFLAGS += \ #libeplayer3_la_LIBADD = -lpthread -lavformat -lavcodec -lavutil -lz -lass -lm -lpng -LIBEPLAYER3_LIBS = libeplayer3.la -lpthread -lavformat -lavcodec -lavutil -lavresample -lz -lass -lm -lpng +LIBEPLAYER3_LIBS = libeplayer3.la -lpthread -lavformat -lavcodec -lavutil -lswresample -lz -lass -lm -lpng bin_PROGRAMS = eplayer3 meta eplayer3_SOURCES = tools/eplayer2.c eplayer3_LDADD = $(LIBEPLAYER3_LIBS) diff --git a/libeplayer3/container/container_ffmpeg.c b/libeplayer3/container/container_ffmpeg.c index 426226f..46f502d 100644 --- a/libeplayer3/container/container_ffmpeg.c +++ b/libeplayer3/container/container_ffmpeg.c @@ -43,8 +43,13 @@ #include #include #ifdef MARTII -#include -#include +#define USE_LIBSWRESAMPLE +# ifdef USE_LIBSWRESAMPLE +# include +# else +# include +# endif +# include #endif #include "common.h" @@ -416,7 +421,11 @@ static void FFMPEGThread(Context_t *context) { AudioVideoOut_t avOut; #ifdef MARTII +#ifdef USE_LIBSWRESAMPLE + SwrContext *swr = NULL; +#else AVAudioResampleContext *avr = NULL; +#endif AVFrame *decoded_frame = NULL; int out_sample_rate = 44100; uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO; @@ -746,14 +755,22 @@ static void FFMPEGThread(Context_t *context) { continue; int e; +#ifdef USE_LIBSWRESAMPLE + if (!swr) { +#else if (!avr) { +#endif int rates[] = { 48000, 96000, 192000, 44100, 88200, 176400, 0 }; int *rate = rates; int in_rate = c->sample_rate; while (*rate && ((*rate / in_rate) * in_rate != *rate) && (in_rate / *rate) * *rate != in_rate) rate++; out_sample_rate = *rate ? *rate : 44100; +#ifdef USE_LIBSWRESAMPLE + swr = swr_alloc(); +#else avr = avresample_alloc_context(); +#endif #if 1 if (c->channel_layout == 0) { @@ -765,6 +782,23 @@ static void FFMPEGThread(Context_t *context) { // player2 won't play mono out_channel_layout = (c->channel_layout == AV_CH_LAYOUT_MONO) ? AV_CH_LAYOUT_STEREO : c->channel_layout; +#ifdef USE_LIBSWRESAMPLE + av_opt_set_int(swr, "in_channel_layout", c->channel_layout, 0); + av_opt_set_int(swr, "out_channel_layout", out_channel_layout, 0); + av_opt_set_int(swr, "in_sample_rate", c->sample_rate, 0); + av_opt_set_int(swr, "out_sample_rate", out_sample_rate, 0); + av_opt_set_int(swr, "in_sample_fmt", c->sample_fmt, 0); + av_opt_set_int(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0); + + e = swr_init(swr); + if (e < 0) { + fprintf(stderr, "swr_init: %d (icl=%d ocl=%d isr=%d osr=%d isf=%d osf=%d\n", + -e, + (int)c->channel_layout, (int)out_channel_layout, c->sample_rate, out_sample_rate, c->sample_fmt, AV_SAMPLE_FMT_S16); + swr_free(&swr); + swr = NULL; + } +#else av_opt_set_int(avr, "in_channel_layout", c->channel_layout, 0); av_opt_set_int(avr, "out_channel_layout", out_channel_layout, 0); av_opt_set_int(avr, "in_sample_rate", c->sample_rate, 0); @@ -780,9 +814,21 @@ static void FFMPEGThread(Context_t *context) { avresample_free(&avr); avr = NULL; } +#endif } uint8_t *output = NULL; +# ifdef USE_LIBSWRESAMPLE + int in_samples = decoded_frame->nb_samples; + int out_samples = av_rescale_rnd(swr_get_delay(swr, c->sample_rate) + in_samples, out_sample_rate, c->sample_rate, AV_ROUND_UP); + e = av_samples_alloc(&output, NULL, 2, out_samples, AV_SAMPLE_FMT_S16, 1); + if (e < 0) { + fprintf(stderr, "av_samples_alloc: %d\n", -e); + continue; + } + + out_samples = swr_convert(swr, &output, out_samples, (const uint8_t **) &decoded_frame->data[0], in_samples); +#else int in_samples = decoded_frame->nb_samples; int out_linesize; int out_samples = avresample_available(avr) @@ -795,6 +841,7 @@ static void FFMPEGThread(Context_t *context) { out_samples = avresample_convert(avr, &output, out_linesize, out_samples, &decoded_frame->data[0], decoded_frame->linesize[0], in_samples); +#endif pcmPrivateData_t extradata; extradata.uSampleRate = out_sample_rate; @@ -1105,11 +1152,17 @@ static void FFMPEGThread(Context_t *context) { } /* while */ #ifdef MARTII +# ifdef USE_LIBSWRESAMPLE + if (swr) { + swr_free(&swr); + } +# else if (avr) { avresample_close(avr); avresample_free(&avr); avcodec_free_frame(&decoded_frame); } +# endif #else // Freeing the allocated buffer for softdecoding if (samples != NULL) {