Skip to content

Commit fe1a073

Browse files
Merge pull request #6 from cchampet/dev_audioProcessing
Add AudioTransform: convert samples
2 parents fea8586 + 887df9c commit fe1a073

File tree

7 files changed

+158
-28
lines changed

7 files changed

+158
-28
lines changed

app/SConscript

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ audioRewrapper = env.Program(
7070
'avformat',
7171
'avcodec',
7272
'swscale',
73+
'swresample',
7374
]
7475
)
7576

app/audioRewrapper/audioRewrapper.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
#include <iomanip>
33

44
#include <AvTranscoder/InputFile.hpp>
5+
#include <AvTranscoder/OutputFile.hpp>
6+
57
#include <AvTranscoder/AvInputStream.hpp>
68
#include <AvTranscoder/InputStreamAudio.hpp>
79
#include <AvTranscoder/OutputStreamAudio.hpp>
810

9-
#include <AvTranscoder/OutputFile.hpp>
11+
#include <AvTranscoder/AudioTransform.hpp>
12+
13+
1014

1115
void rewrapAudio( const char* inputfilename, const char* outputFilename )
1216
{
@@ -52,55 +56,65 @@ void transcodeAudio( const char* inputfilename, const char* outputFilename )
5256
InputFile inputFile( inputfilename );
5357
inputFile.analyse();
5458

59+
OutputFile outputFile( outputFilename );
60+
outputFile.setup();
61+
5562
// init audio decoders
5663
InputStreamAudio inputStreamAudio( inputFile.getStream( 0 ) );
5764
inputFile.getStream( 0 ).setBufferred( true );
58-
59-
OutputFile outputFile( outputFilename );
60-
outputFile.setup();
61-
outputFile.addAudioStream( inputFile.getStream( 0 ).getAudioDesc() );
62-
outputFile.beginWrap();
6365

66+
// init audio encoders
6467
OutputStreamAudio outputStreamAudio;
65-
AudioDesc& audioDesc = outputStreamAudio.getAudioDesc();
66-
audioDesc.setAudioCodec( "pcm_s16le" );
67-
audioDesc.setAudioParameters(
68+
AudioDesc& audioOutputDesc = outputStreamAudio.getAudioDesc();
69+
audioOutputDesc.setAudioCodec( "pcm_s24le" );
70+
audioOutputDesc.setAudioParameters(
6871
inputFile.getStream( 0 ).getAudioDesc().getSampleRate(),
6972
inputFile.getStream( 0 ).getAudioDesc().getChannels(),
70-
inputFile.getStream( 0 ).getAudioDesc().getSampleFormat()
73+
AV_SAMPLE_FMT_S32//,inputFile.getStream( 0 ).getAudioDesc().getSampleFormat()
7174
);
7275

73-
DataStream codedFrame;
74-
7576
if( ! outputStreamAudio.setup( ) )
7677
{
7778
throw std::runtime_error( "error during initialising audio output stream" );
7879
}
7980

80-
// Transcoding
81-
std::cout << "start transcoding" << std::endl;
81+
outputFile.addAudioStream( audioOutputDesc );
82+
outputFile.beginWrap();
83+
84+
// init convert
85+
AudioTransform audioTransform;
86+
87+
DataStream codedFrame;
8288

89+
// start transcoding process
90+
std::cout << "start transcoding" << std::endl;
91+
92+
AudioFrame audioFrameSource( inputFile.getStream( 0 ).getAudioDesc().getFrameDesc() );
93+
AudioFrame audioFrameToEncode( audioOutputDesc.getFrameDesc() );
94+
8395
size_t frame = 0;
84-
AudioFrameDesc audioFrameDesc;
85-
86-
AudioFrame audioFrame( audioFrameDesc );
87-
88-
while( inputStreamAudio.readNextFrame( audioFrame ) )
96+
while( inputStreamAudio.readNextFrame( audioFrameSource ) )
8997
{
90-
std::cout << "\rprocess frame " << (int)frame - 1 << std::endl << std::flush;
98+
std::cout << "\rprocess frame " << (int)frame - 1 << std::flush;
9199

92-
// convert
93-
94-
outputStreamAudio.encodeFrame( audioFrame, codedFrame );
100+
audioTransform.convert( audioFrameSource, audioFrameToEncode );
95101

102+
outputStreamAudio.encodeFrame( audioFrameToEncode, codedFrame );
103+
96104
outputFile.wrap( codedFrame, 0 );
97105

98106
++frame;
107+
// if you want to stop the transcoding process (after 10s at 48,1 KHz)
108+
// if(frame == 10*48100)
109+
// break;
99110
}
100111
std::cout << std::endl;
101112

102113
outputStreamAudio.encodeFrame( codedFrame );
103-
114+
outputFile.wrap( codedFrame, 0 );
115+
116+
// end of transcoding process
117+
104118
outputFile.endWrap();
105119
}
106120

src/AvTranscoder/AudioTransform.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "AudioTransform.hpp"
2+
#include "DatasStructures/AudioFrame.hpp"
3+
#include "common.hpp"
4+
5+
extern "C" {
6+
#ifndef __STDC_CONSTANT_MACROS
7+
#define __STDC_CONSTANT_MACROS
8+
#endif
9+
#include <libavcodec/avcodec.h>
10+
#include <libavutil/opt.h>
11+
#include <libswresample/swresample.h>
12+
#if LIBAVCODEC_VERSION_MAJOR > 54
13+
#include <libavutil/frame.h>
14+
#endif
15+
}
16+
17+
#include <stdexcept>
18+
19+
namespace avtranscoder
20+
{
21+
22+
AudioTransform::AudioTransform()
23+
: m_audioConvertContext( NULL )
24+
, m_isInit ( false )
25+
{
26+
}
27+
28+
bool AudioTransform::init( const AudioFrame& src, const AudioFrame& dst )
29+
{
30+
m_audioConvertContext = swr_alloc();
31+
32+
if( !m_audioConvertContext )
33+
{
34+
throw std::runtime_error( "unable to create audio convert context" );
35+
}
36+
37+
swr_alloc_set_opts( m_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);
41+
42+
if( swr_init( m_audioConvertContext ) < 0 )
43+
{
44+
swr_free( &m_audioConvertContext );
45+
throw std::runtime_error( "unable to open audio convert context" );
46+
}
47+
48+
return true;
49+
}
50+
51+
void AudioTransform::convert( const AudioFrame& src, AudioFrame& dst )
52+
{
53+
if( ! m_isInit )
54+
{
55+
m_isInit = init( src, dst );
56+
m_isInit = true;
57+
}
58+
59+
if( dst.getSize() != src.getSize() )
60+
dst.getBuffer().resize( src.getSize(), 0 );
61+
62+
const unsigned char* srcData = src.getPtr();
63+
unsigned char* dstData = dst.getPtr();
64+
65+
swr_convert( m_audioConvertContext, &dstData, dst.getSize(), &srcData, src.getSize() );
66+
67+
dst.setNbSamples( src.getNbSamples() );
68+
}
69+
70+
}

src/AvTranscoder/AudioTransform.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef _AV_TRANSCODER_AUDIO_TRANSFORM_HPP
2+
#define _AV_TRANSCODER_AUDIO_TRANSFORM_HPP
3+
4+
#include "common.hpp"
5+
6+
class SwrContext;
7+
8+
namespace avtranscoder
9+
{
10+
11+
class AudioFrame;
12+
13+
class AvExport AudioTransform
14+
{
15+
public:
16+
AudioTransform();
17+
18+
void convert( const AudioFrame& src, AudioFrame& dst );
19+
20+
private:
21+
bool init( const AudioFrame& src, const AudioFrame& dst );
22+
23+
SwrContext* m_audioConvertContext;
24+
25+
bool m_isInit;
26+
};
27+
28+
}
29+
30+
#endif

src/AvTranscoder/DatasStructures/AudioDesc.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "AudioDesc.hpp"
2+
#include "AudioFrame.hpp"
23

34
extern "C" {
45
#ifndef __STDC_CONSTANT_MACROS
@@ -209,5 +210,16 @@ const AVSampleFormat AudioDesc::getSampleFormat() const
209210
return m_codecContext->sample_fmt;
210211
}
211212

213+
AudioFrameDesc AudioDesc::getFrameDesc() const
214+
{
215+
AudioFrameDesc audioFrameDesc;
216+
217+
audioFrameDesc.setChannels( m_codecContext->channels );
218+
audioFrameDesc.setSampleRate( m_codecContext->sample_rate );
219+
audioFrameDesc.setSampleFormat( m_codecContext->sample_fmt );
220+
221+
return audioFrameDesc;
222+
}
223+
212224

213225
}

src/AvTranscoder/DatasStructures/AudioDesc.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ extern "C" {
1515
#include <libavcodec/avcodec.h>
1616
}
1717

18+
#include <AvTranscoder/DatasStructures/AudioFrame.hpp>
19+
1820
namespace avtranscoder
1921
{
2022

@@ -48,6 +50,8 @@ class AvExport AudioDesc
4850
AVCodecContext* getCodecContext() const { return m_codecContext; }
4951
#endif
5052

53+
AudioFrameDesc getFrameDesc() const;
54+
5155
private:
5256
void initCodecContext( );
5357

src/AvTranscoder/InputStreamAudio.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,9 @@ bool InputStreamAudio::readNextFrame( AudioFrame& audioFrameBuffer )
140140
if( audioFrameBuffer.getSize() != decodedSize )
141141
audioFrameBuffer.getBuffer().resize( decodedSize, 0 );
142142

143-
unsigned char* dest = audioFrameBuffer.getPtr();
144-
int nb_channels = m_codecContext->channels;
145-
av_samples_copy(&dest, (uint8_t* const* )m_frame->data, 0,
146-
0, m_frame->nb_samples, nb_channels,
143+
unsigned char* dst = audioFrameBuffer.getPtr();
144+
av_samples_copy(&dst, (uint8_t* const* )m_frame->data, 0,
145+
0, m_frame->nb_samples, m_codecContext->channels,
147146
m_codecContext->sample_fmt);
148147
}
149148

0 commit comments

Comments
 (0)