Skip to content

Commit 3f1998d

Browse files
update to support libavresample or libswresample
1 parent e560daa commit 3f1998d

File tree

6 files changed

+158
-99
lines changed

6 files changed

+158
-99
lines changed

SConstruct

Lines changed: 100 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
EnsureSConsVersion(2, 3, 0)
22

33
import os
4+
import sys
45
import ConfigParser
56

67
config = ConfigParser.RawConfigParser()
78

89
config.read( [
9-
'scons.cfg',
10+
'scons.cfg',
1011
] )
1112

1213
icommonInclude = []
@@ -16,24 +17,24 @@ installPrefix = "/usr/local"
1617
splitChar = ";"
1718

1819
if config.has_section( 'COMMON' ):
19-
if( config.has_option( 'COMMON', 'inc' ) ):
20-
commonInclude.append( config.get( 'COMMON', 'inc' ).split( splitChar ) )
21-
if( config.has_option( 'COMMON', 'libdir' ) ):
22-
commonLibDir.append( config.get( 'COMMON', 'libdir' ).split( splitChar ) )
23-
if( config.has_option( 'COMMON', 'prefix' ) ):
24-
installPrefix = config.get( 'COMMON', 'prefix' )
20+
if( config.has_option( 'COMMON', 'inc' ) ):
21+
commonInclude.append( config.get( 'COMMON', 'inc' ).split( splitChar ) )
22+
if( config.has_option( 'COMMON', 'libdir' ) ):
23+
commonLibDir.append( config.get( 'COMMON', 'libdir' ).split( splitChar ) )
24+
if( config.has_option( 'COMMON', 'prefix' ) ):
25+
installPrefix = config.get( 'COMMON', 'prefix' )
2526

2627
if not config.has_section( 'LIBAV' ):
27-
print "missing LIBAV section in scons.cfg file configuration"
28-
sys.exit( -1 )
28+
print "missing LIBAV section in scons.cfg file configuration"
29+
sys.exit( -1 )
2930

3031
if not config.has_section( 'JAVA' ):
31-
print "missing JAVA section in scons.cfg file configuration"
32-
sys.exit( -1 )
32+
print "missing JAVA section in scons.cfg file configuration"
33+
sys.exit( -1 )
3334

3435
if not config.has_section( 'PYTHON' ):
35-
print "missing PYTHON section in scons.cfg file configuration"
36-
sys.exit( -1 )
36+
print "missing PYTHON section in scons.cfg file configuration"
37+
sys.exit( -1 )
3738

3839

3940
javaInclude = config.get( 'JAVA', 'inc' ).split( splitChar )
@@ -45,77 +46,101 @@ env = Environment().Clone()
4546
envJava = Environment().Clone()
4647
envPy = Environment().Clone()
4748

48-
# C++ environment
49+
conf = Configure( env )
50+
51+
resampleLibraryFlag = '-DAV_RESAMPLE_LIBRARY'
52+
resampleLibraryName = 'avresample'
53+
54+
if not conf.CheckLibWithHeader('avutil', 'libavutil/avutil.h', 'c'):
55+
sys.exit( 0 )
56+
57+
if not conf.CheckLibWithHeader('avformat', 'libavformat/avformat.h', 'c'):
58+
sys.exit( 0 )
4959

60+
if not conf.CheckLibWithHeader('avcodec', 'libavcodec/avcodec.h', 'c'):
61+
sys.exit( 0 )
62+
63+
if not conf.CheckLibWithHeader('swscale', 'libswscale/swscale.h', 'c'):
64+
sys.exit( 0 )
65+
66+
if not conf.CheckLibWithHeader('avresample', 'libavresample/avresample.h', 'c'):
67+
if conf.CheckLibWithHeader('swresample', 'libswresample/swresample.h', 'c'):
68+
resampleLibraryFlag = '-DFF_RESAMPLE_LIBRARY'
69+
resampleLibraryName = 'swresample'
70+
71+
# C++ environment
5072
env.Append(
51-
CPPPATH = [
52-
libavInclude,
53-
"#src",
54-
],
55-
CXXFLAGS = [
56-
'-Wall',
57-
'-fPIC',
58-
],
59-
LIBPATH = [
60-
libavLibDir,
61-
"#src",
62-
"#build/src"
63-
],
73+
CPPPATH = [
74+
libavInclude,
75+
"#src",
76+
],
77+
CXXFLAGS = [
78+
'-Wall',
79+
'-fPIC',
80+
resampleLibraryFlag,
81+
],
82+
LIBPATH = [
83+
libavLibDir,
84+
"#src",
85+
"#build/src"
86+
],
6487
)
6588

6689
# Java environment
6790
envJava.Replace(
68-
CPPPATH = [
69-
javaInclude,
70-
libavInclude,
71-
".",
72-
],
73-
SWIGCXXFILESUFFIX= '_wrapJava$CXXFILESUFFIX',
74-
CXXFLAGS = [
75-
'-Wall',
76-
],
77-
SWIGFLAGS = [
78-
'-java',
79-
'-c++',
80-
'-fcompact',
81-
],
82-
LINKFLAGS = [
83-
],
84-
LIBPATH = [
85-
libavLibDir,
86-
"#src",
87-
],
88-
JARCHDIR = env.Dir('#build/src/AvTranscoder').get_abspath(),
91+
CPPPATH = [
92+
javaInclude,
93+
libavInclude,
94+
".",
95+
],
96+
SWIGCXXFILESUFFIX= '_wrapJava$CXXFILESUFFIX',
97+
CXXFLAGS = [
98+
'-Wall',
99+
resampleLibraryFlag,
100+
],
101+
SWIGFLAGS = [
102+
'-java',
103+
'-c++',
104+
'-fcompact',
105+
],
106+
LINKFLAGS = [
107+
],
108+
LIBPATH = [
109+
libavLibDir,
110+
"#src",
111+
],
112+
JARCHDIR = env.Dir('#build/src/AvTranscoder').get_abspath(),
89113
)
90114

91115
envJava.Append(
92-
SWIGPATH = envJava['CPPPATH'],
93-
SWIGFLAGS = [ '-package', 'org.AvTranscoder' ],
94-
)
116+
SWIGPATH = envJava['CPPPATH'],
117+
SWIGFLAGS = [ '-package', 'org.AvTranscoder' ],
118+
)
95119

96120
# Python environment
97121
envPy.Replace(
98-
CPPPATH = [
99-
pyInclude,
100-
libavInclude,
101-
".",
102-
],
103-
SWIGCXXFILESUFFIX= '_wrapPython$CXXFILESUFFIX',
104-
SHLIBPREFIX= '_',
105-
CXXFLAGS = [
106-
'-Wall',
107-
],
108-
SWIGFLAGS = [
109-
'-python',
110-
'-c++',
111-
'-fcompact',
112-
],
113-
LINKFLAGS = [
114-
],
115-
LIBPATH = [
116-
libavLibDir,
117-
"#src",
118-
],
122+
CPPPATH = [
123+
pyInclude,
124+
libavInclude,
125+
".",
126+
],
127+
SWIGCXXFILESUFFIX= '_wrapPython$CXXFILESUFFIX',
128+
SHLIBPREFIX= '_',
129+
CXXFLAGS = [
130+
'-Wall',
131+
resampleLibraryFlag,
132+
],
133+
SWIGFLAGS = [
134+
'-python',
135+
'-c++',
136+
'-fcompact',
137+
],
138+
LINKFLAGS = [
139+
],
140+
LIBPATH = [
141+
libavLibDir,
142+
"#src",
143+
],
119144
)
120145

121146
envPy.Append( SWIGPATH = envPy['CPPPATH'] )
@@ -124,11 +149,12 @@ Export( "env" )
124149
Export( "envJava" )
125150
Export( "envPy" )
126151
Export( "installPrefix" )
152+
Export( "resampleLibraryName" )
127153

128154
VariantDir( 'build/src', 'src', duplicate = 0 )
129155
VariantDir( 'build/app', 'app', duplicate = 0 )
130156

131157
SConscript( [
132-
'build/src/SConscript',
133-
'build/app/SConscript',
158+
'build/src/SConscript',
159+
'build/app/SConscript',
134160
] )

app/SConscript

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Import( 'AvTranscoder' )
55
Import( 'sAvTranscoder' )
66
#Import( 'AvTranscoder_jar' )
77
Import( 'installPrefix' )
8+
Import( "resampleLibraryName" )
89

910
env = Environment().Clone()
1011

@@ -19,7 +20,7 @@ avinfo = env.Program(
1920
'avformat',
2021
'avcodec',
2122
'swscale',
22-
'swresample',
23+
resampleLibraryName,
2324
]
2425
)
2526

@@ -32,7 +33,7 @@ avmeta = env.Program(
3233
'avformat',
3334
'avcodec',
3435
'swscale',
35-
'swresample',
36+
resampleLibraryName,
3637
]
3738
)
3839

@@ -45,7 +46,7 @@ avtransform = env.Program(
4546
'avformat',
4647
'avcodec',
4748
'swscale',
48-
'swresample',
49+
resampleLibraryName,
4950
]
5051
)
5152

@@ -58,7 +59,7 @@ avprocessor = env.Program(
5859
'avformat',
5960
'avcodec',
6061
'swscale',
61-
'swresample',
62+
resampleLibraryName,
6263
],
6364
CXXFLAGS = [
6465
'-std=c++0x'
@@ -74,8 +75,7 @@ audioRewrapper = env.Program(
7475
'avformat',
7576
'avcodec',
7677
'swscale',
77-
'swresample',
78-
'swresample',
78+
resampleLibraryName,
7979
]
8080
)
8181

@@ -89,7 +89,7 @@ if platform.system() != 'Windows':
8989
'avformat',
9090
'avcodec',
9191
'swscale',
92-
'swresample',
92+
resampleLibraryName,
9393
'GL',
9494
'glut',
9595
]
@@ -104,7 +104,7 @@ if platform.system() != 'Windows':
104104
'avformat',
105105
'avcodec',
106106
'swscale',
107-
'swresample',
107+
resampleLibraryName,
108108
],
109109
CXXFLAGS = [
110110
'-std=c++0x'
@@ -118,7 +118,9 @@ avprofiles = env.Program(
118118
sAvTranscoder,
119119
'avutil',
120120
'avformat',
121-
'avcodec'
121+
'avcodec',
122+
'swscale',
123+
resampleLibraryName,
122124
],
123125
CXXFLAGS = [
124126
'-std=c++0x'

src/AvTranscoder/AudioTransform.cpp

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,21 @@ extern "C" {
88
#endif
99
#include <libavcodec/avcodec.h>
1010
#include <libavutil/opt.h>
11-
#include <libswresample/swresample.h>
11+
12+
#ifdef AV_RESAMPLE_LIBRARY
13+
#include <libavresample/avresample.h>
14+
#define AllocResampleContext avresample_alloc_context
15+
#define FreeResampleContext avresample_free
16+
#define InitResampleContext avresample_open
17+
#define SetSampleFormat av_opt_set_int
18+
#else
19+
#include <libswresample/swresample.h>
20+
#define AllocResampleContext swr_alloc
21+
#define FreeResampleContext swr_free
22+
#define InitResampleContext swr_init
23+
#define SetSampleFormat av_opt_set_sample_fmt
24+
#endif
25+
1226
#if LIBAVCODEC_VERSION_MAJOR > 54
1327
#include <libavutil/frame.h>
1428
#endif
@@ -27,21 +41,23 @@ AudioTransform::AudioTransform()
2741

2842
bool AudioTransform::init( const AudioFrame& src, const AudioFrame& dst )
2943
{
30-
_audioConvertContext = swr_alloc();
31-
44+
_audioConvertContext = AllocResampleContext();
45+
3246
if( !_audioConvertContext )
3347
{
3448
throw std::runtime_error( "unable to create audio convert context" );
3549
}
3650

37-
swr_alloc_set_opts( _audioConvertContext,
38-
av_get_default_channel_layout( dst.desc().getChannels() ), dst.desc().getSampleFormat(), av_get_default_channel_layout( dst.desc().getSampleRate() ),
39-
av_get_default_channel_layout( src.desc().getChannels() ), src.desc().getSampleFormat(), av_get_default_channel_layout( src.desc().getSampleRate() ),
40-
0, NULL);
51+
av_opt_set_int( _audioConvertContext, "in_channel_layout", av_get_default_channel_layout( src.desc().getChannels() ), 0 );
52+
av_opt_set_int( _audioConvertContext, "out_channel_layout", av_get_default_channel_layout( dst.desc().getChannels() ), 0 );
53+
av_opt_set_int( _audioConvertContext, "in_sample_rate", src.desc().getSampleRate(), 0 );
54+
av_opt_set_int( _audioConvertContext, "out_sample_rate", dst.desc().getSampleRate(), 0 );
55+
SetSampleFormat( _audioConvertContext, "in_sample_fmt", src.desc().getSampleFormat(), 0 );
56+
SetSampleFormat( _audioConvertContext, "out_sample_fmt", dst.desc().getSampleFormat(), 0 );
4157

42-
if( swr_init( _audioConvertContext ) < 0 )
58+
if( InitResampleContext( _audioConvertContext ) < 0 )
4359
{
44-
swr_free( &_audioConvertContext );
60+
FreeResampleContext( &_audioConvertContext );
4561
throw std::runtime_error( "unable to open audio convert context" );
4662
}
4763

@@ -62,8 +78,12 @@ void AudioTransform::convert( const AudioFrame& src, AudioFrame& dst )
6278
const unsigned char* srcData = src.getPtr();
6379
unsigned char* dstData = dst.getPtr();
6480

81+
#ifdef AV_RESAMPLE_LIBRARY
82+
avresample_convert( _audioConvertContext, (uint8_t**)&dstData, 0, dst.getSize(), (uint8_t**)&srcData, 0, src.getSize() );
83+
#else
6584
swr_convert( _audioConvertContext, &dstData, dst.getSize(), &srcData, src.getSize() );
66-
85+
#endif
86+
6787
dst.setNbSamples( src.getNbSamples() );
6888
}
6989

0 commit comments

Comments
 (0)