replace libavresample with libswresample

This commit is contained in:
martii
2013-06-01 13:47:47 +02:00
parent 8b533c3c59
commit 8a68eb3f15
5 changed files with 102 additions and 7 deletions

View File

@@ -40,7 +40,7 @@ libstb_hal_test_LDADD += \
@AVFORMAT_LIBS@ \ @AVFORMAT_LIBS@ \
@AVUTIL_LIBS@ \ @AVUTIL_LIBS@ \
@AVCODEC_LIBS@ \ @AVCODEC_LIBS@ \
@AVRESAMPLE_LIBS@ \ @SWRESAMPLE_LIBS@ \
@SWSCALE_LIBS@ @SWSCALE_LIBS@
endif endif
if BOXTYPE_SPARK if BOXTYPE_SPARK

View File

@@ -29,7 +29,7 @@ if test x$BOXTYPE = xgeneric; then
# don't know which version is exactly needed here... # don't know which version is exactly needed here...
PKG_CHECK_MODULES([AVUTIL], [libavutil]) PKG_CHECK_MODULES([AVUTIL], [libavutil])
PKG_CHECK_MODULES([SWSCALE], [libswscale]) PKG_CHECK_MODULES([SWSCALE], [libswscale])
PKG_CHECK_MODULES([AVRESAMPLE], [libavresample]) PKG_CHECK_MODULES([SWRESAMPLE], [libswresample])
fi fi
AC_OUTPUT([ AC_OUTPUT([
Makefile Makefile

View File

@@ -35,7 +35,12 @@ extern "C" {
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#include <ao/ao.h> #include <ao/ao.h>
#ifdef MARTII #ifdef MARTII
#define USE_LIBSWRESAMPLE
# ifdef USE_LIBSWRESAMPLE
# include <libswresample/swresample.h>
# else
# include <libavresample/avresample.h> # include <libavresample/avresample.h>
# endif
# include <libavutil/opt.h> # include <libavutil/opt.h>
#endif #endif
} }
@@ -291,7 +296,11 @@ void cAudio::run()
// ao_device *adevice; // ao_device *adevice;
// ao_sample_format sformat; // ao_sample_format sformat;
#ifdef MARTII #ifdef MARTII
#ifdef USE_LIBSWRESAMPLE
SwrContext *swr = NULL;
#else
AVAudioResampleContext *avr = NULL; AVAudioResampleContext *avr = NULL;
#endif
int e; int e;
#endif #endif
@@ -364,6 +373,20 @@ void cAudio::run()
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif #endif
#ifdef MARTII #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(); avr = avresample_alloc_context();
av_opt_set_int(avr, "in_channel_layout", c->channel_layout, 0); av_opt_set_int(avr, "in_channel_layout", c->channel_layout, 0);
av_opt_set_int(avr, "out_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); lt_info("%s: avresample_open failed: %d\n", __func__, -e);
goto out2; goto out2;
} }
# endif
#endif #endif
lt_info("codec params: sample_fmt %d sample_rate %d channels %d\n", lt_info("codec params: sample_fmt %d sample_rate %d channels %d\n",
c->sample_fmt, c->sample_rate, c->channels); 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); lt_debug("%s: pts 0x%" PRIx64 " %3f\n", __func__, curr_pts, curr_pts/90000.0);
#ifdef MARTII #ifdef MARTII
uint8_t *output = NULL; 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_linesize;
int out_samples = avresample_available(avr) int out_samples = avresample_available(avr)
+ av_rescale_rnd(avresample_get_delay(avr) + frame->nb_samples, + 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, out_samples = avresample_convert(avr, &output, out_linesize, out_samples,
&frame->data[0], frame->linesize[0], frame->nb_samples); &frame->data[0], frame->linesize[0], frame->nb_samples);
# endif
ao_play(adevice, (char *)output, out_samples * 2 /* 16 bit */ * c->channels); ao_play(adevice, (char *)output, out_samples * 2 /* 16 bit */ * c->channels);
av_freep(&output); av_freep(&output);
#else #else
@@ -414,10 +450,16 @@ void cAudio::run()
avcodec_close(c); avcodec_close(c);
out: out:
#ifdef MARTII #ifdef MARTII
# ifdef USE_LIBSWRESAMPLE
if (swr) {
swr_free(&swr);
}
# else
if (avr) { if (avr) {
avresample_close(avr); avresample_close(avr);
avresample_free(&avr); avresample_free(&avr);
} }
# endif
#endif #endif
avformat_close_input(&avfc); avformat_close_input(&avfc);
av_free(pIOCtx->buffer); av_free(pIOCtx->buffer);

View File

@@ -29,7 +29,7 @@ AM_CFLAGS += \
#libeplayer3_la_LIBADD = -lpthread -lavformat -lavcodec -lavutil -lz -lass -lm -lpng #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 bin_PROGRAMS = eplayer3 meta
eplayer3_SOURCES = tools/eplayer2.c eplayer3_SOURCES = tools/eplayer2.c
eplayer3_LDADD = $(LIBEPLAYER3_LIBS) eplayer3_LDADD = $(LIBEPLAYER3_LIBS)

View File

@@ -43,7 +43,12 @@
#include <libavutil/avutil.h> #include <libavutil/avutil.h>
#include <libavformat/avformat.h> #include <libavformat/avformat.h>
#ifdef MARTII #ifdef MARTII
#define USE_LIBSWRESAMPLE
# ifdef USE_LIBSWRESAMPLE
# include <libswresample/swresample.h>
# else
# include <libavresample/avresample.h> # include <libavresample/avresample.h>
# endif
# include <libavutil/opt.h> # include <libavutil/opt.h>
#endif #endif
@@ -416,7 +421,11 @@ static void FFMPEGThread(Context_t *context) {
AudioVideoOut_t avOut; AudioVideoOut_t avOut;
#ifdef MARTII #ifdef MARTII
#ifdef USE_LIBSWRESAMPLE
SwrContext *swr = NULL;
#else
AVAudioResampleContext *avr = NULL; AVAudioResampleContext *avr = NULL;
#endif
AVFrame *decoded_frame = NULL; AVFrame *decoded_frame = NULL;
int out_sample_rate = 44100; int out_sample_rate = 44100;
uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO; uint64_t out_channel_layout = AV_CH_LAYOUT_STEREO;
@@ -746,14 +755,22 @@ static void FFMPEGThread(Context_t *context) {
continue; continue;
int e; int e;
#ifdef USE_LIBSWRESAMPLE
if (!swr) {
#else
if (!avr) { if (!avr) {
#endif
int rates[] = { 48000, 96000, 192000, 44100, 88200, 176400, 0 }; int rates[] = { 48000, 96000, 192000, 44100, 88200, 176400, 0 };
int *rate = rates; int *rate = rates;
int in_rate = c->sample_rate; int in_rate = c->sample_rate;
while (*rate && ((*rate / in_rate) * in_rate != *rate) && (in_rate / *rate) * *rate != in_rate) while (*rate && ((*rate / in_rate) * in_rate != *rate) && (in_rate / *rate) * *rate != in_rate)
rate++; rate++;
out_sample_rate = *rate ? *rate : 44100; out_sample_rate = *rate ? *rate : 44100;
#ifdef USE_LIBSWRESAMPLE
swr = swr_alloc();
#else
avr = avresample_alloc_context(); avr = avresample_alloc_context();
#endif
#if 1 #if 1
if (c->channel_layout == 0) { if (c->channel_layout == 0) {
@@ -765,6 +782,23 @@ static void FFMPEGThread(Context_t *context) {
// player2 won't play mono // player2 won't play mono
out_channel_layout = (c->channel_layout == AV_CH_LAYOUT_MONO) ? AV_CH_LAYOUT_STEREO : c->channel_layout; 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, "in_channel_layout", c->channel_layout, 0);
av_opt_set_int(avr, "out_channel_layout", out_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); 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); avresample_free(&avr);
avr = NULL; avr = NULL;
} }
#endif
} }
uint8_t *output = NULL; 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 in_samples = decoded_frame->nb_samples;
int out_linesize; int out_linesize;
int out_samples = avresample_available(avr) 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, out_samples = avresample_convert(avr, &output, out_linesize, out_samples,
&decoded_frame->data[0], decoded_frame->linesize[0], in_samples); &decoded_frame->data[0], decoded_frame->linesize[0], in_samples);
#endif
pcmPrivateData_t extradata; pcmPrivateData_t extradata;
extradata.uSampleRate = out_sample_rate; extradata.uSampleRate = out_sample_rate;
@@ -1105,11 +1152,17 @@ static void FFMPEGThread(Context_t *context) {
} /* while */ } /* while */
#ifdef MARTII #ifdef MARTII
# ifdef USE_LIBSWRESAMPLE
if (swr) {
swr_free(&swr);
}
# else
if (avr) { if (avr) {
avresample_close(avr); avresample_close(avr);
avresample_free(&avr); avresample_free(&avr);
avcodec_free_frame(&decoded_frame); avcodec_free_frame(&decoded_frame);
} }
# endif
#else #else
// Freeing the allocated buffer for softdecoding // Freeing the allocated buffer for softdecoding
if (samples != NULL) { if (samples != NULL) {