Skip to content

Commit 6fec788

Browse files
create Transcoder class to simplify user API
1 parent 1c558ed commit 6fec788

File tree

14 files changed

+311
-41
lines changed

14 files changed

+311
-41
lines changed

app/SConscript

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,21 @@ avtransform = env.Program(
3333
]
3434
)
3535

36+
avprocessor = env.Program(
37+
'avprocessor',
38+
Glob( 'genericProcessor/*.cpp' ),
39+
LIBS = [
40+
sAvTranscoder,
41+
'avutil',
42+
'avformat',
43+
'avcodec',
44+
'swscale',
45+
],
46+
CXXFLAGS = [
47+
'-std=c++0x'
48+
],
49+
)
50+
3651
audioRewrapper = env.Program(
3752
'audioWrap',
3853
Glob( 'audioRewrapper/*.cpp' ),
@@ -49,5 +64,6 @@ env.Depends( avmeta, sAvTranscoder )
4964
env.Depends( audioRewrapper, sAvTranscoder )
5065

5166
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), avmeta ) )
67+
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), avprocessor ) )
5268
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), avtransform ) )
5369
env.Alias( "install", env.Install(os.path.join( installPrefix, "bin" ), audioRewrapper ) )

app/audioRewrapper/audioRewrapper.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@ void rewrapAudio( const char* inputfilename, const char* outputFilename )
1212
using namespace avtranscoder;
1313

1414
av_log_set_level( AV_LOG_FATAL );
15-
//av_log_set_level( AV_LOG_DEBUG );
15+
av_log_set_level( AV_LOG_DEBUG );
1616

1717
InputFile inputFile( inputfilename );
1818
inputFile.analyse();
1919

20-
21-
// init audio decoders
22-
InputStreamAudio inputStreamAudio( inputFile.getStream( 0 ) );
23-
2420
OutputFile outputFile( outputFilename );
2521

2622
outputFile.setup();
2723

2824
outputFile.addAudioStream( inputFile.getStream( 0 ).getAudioDesc() );
2925

30-
DataStreamDesc dataDesc;
31-
DataStream data( dataDesc );
26+
DataStream data;
3227

3328
// Encodage/transcodage
3429
std::cout << "start re-wrapping" << std::endl;
@@ -65,8 +60,7 @@ void transcodeAudio( const char* inputfilename, const char* outputFilename )
6560

6661
outputFile.addAudioStream( inputFile.getStream( 0 ).getAudioDesc() );
6762

68-
DataStreamDesc dataDesc;
69-
DataStream data( dataDesc );
63+
DataStream data;
7064

7165
// Transcoding
7266
std::cout << "start transcoding" << std::endl;
@@ -98,9 +92,15 @@ int main( int argc, char** argv )
9892
}
9993

10094
std::cout << "start ..." << std::endl;
101-
102-
rewrapAudio( argv[1], argv[2] );
103-
transcodeAudio( argv[1], argv[2] );
10495

96+
try
97+
{
98+
//rewrapAudio( argv[1], argv[2] );
99+
transcodeAudio( argv[1], argv[2] );
100+
}
101+
catch( std::exception &e )
102+
{
103+
std::cout << "[ERROR] " << e.what() << std::endl;
104+
}
105105
std::cout << "end ..." << std::endl;
106-
}
106+
}

app/avTranscoder/avTranscoder.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,8 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
7373
exit( -1 );
7474
}
7575

76-
DataStreamDesc dataStreamDesc;
77-
7876
Image imageToEncode( sourceImage );
79-
DataStream codedImage( dataStreamDesc );
77+
DataStream codedImage;
8078

8179

8280
// OutputStreamAudio osAudioLeft ( ); // "AudioStreamEncoder" / "AudioOutputStream" ?
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
#include <AvTranscoder/Transcoder.hpp>
3+
4+
#include <iostream>
5+
#include <vector>
6+
#include <fstream>
7+
#include <sstream>
8+
#include <cstdlib>
9+
10+
11+
void parseConfigFile( const std::string& configFilename, avtranscoder::Transcoder::StreamsDefinition& streams )
12+
{
13+
std::ifstream configFile( configFilename.c_str(), std::ifstream::in );
14+
15+
std::string line;
16+
while( std::getline( configFile, line ) )
17+
{
18+
std::istringstream is_line( line );
19+
std::string filename;
20+
if( std::getline( is_line, filename, '=' ) )
21+
{
22+
std::string streamId;
23+
if( std::getline( is_line, streamId ) )
24+
{
25+
std::cout << filename << "( " << streamId << " )" << std::endl;
26+
streams.push_back( std::pair< std::string, int >( filename, atoi( streamId.c_str() ) ) );
27+
}
28+
}
29+
}
30+
31+
configFile.close();
32+
}
33+
34+
int main( int argc, char** argv )
35+
{
36+
if( argc != 3 )
37+
{
38+
std::cout << "avprocessor require an input config file and an output media filename" << std::endl;
39+
return( -1 );
40+
}
41+
42+
av_log_set_level( AV_LOG_FATAL );
43+
44+
try
45+
{
46+
std::cout << "start ..." << std::endl;
47+
48+
std::string inputConfigFile( argv[1] );
49+
std::string outputFile( argv[2] );
50+
51+
avtranscoder::Transcoder::StreamsDefinition streams;
52+
53+
parseConfigFile( inputConfigFile, streams );
54+
55+
avtranscoder::Transcoder transcoder( outputFile );
56+
57+
transcoder.add( streams );
58+
59+
// video re-wrapping or transcoding if necessary
60+
transcoder.process();
61+
62+
std::cout << "end ..." << std::endl;
63+
}
64+
catch( std::exception& e )
65+
{
66+
std::cerr << "ERROR: during process, an error occured:" << std::endl << e.what() << std::endl;
67+
}
68+
69+
}

src/AvTranscoder/DatasStructures/DataStreamDesc.hpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,15 @@ extern "C" {
1818
namespace avtranscoder
1919
{
2020

21-
class DataStreamDesc
22-
{
23-
public:
24-
DataStreamDesc()
25-
{};
26-
27-
private:
28-
};
29-
30-
3121
class DataStream
3222
{
3323
public:
3424
typedef std::vector< unsigned char > DataBuffer;
3525

36-
37-
DataStream( const DataStreamDesc& ref )
26+
DataStream( )
3827
: m_dataBuffer( 0, 0 )
39-
, m_dataStreamDesc( ref )
4028
{ }
4129

42-
const DataStreamDesc& desc() const { return m_dataStreamDesc; }
4330
DataBuffer& getBuffer() { return m_dataBuffer; }
4431
unsigned char* getPtr() { return &m_dataBuffer[0]; }
4532
#ifndef SWIG
@@ -49,7 +36,6 @@ class DataStream
4936

5037
private:
5138
DataBuffer m_dataBuffer;
52-
const DataStreamDesc m_dataStreamDesc;
5339
};
5440

5541
}

src/AvTranscoder/DatasStructures/VideoDesc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void VideoDesc::initCodecContext( )
9595
{
9696
if( m_codec == NULL )
9797
{
98-
throw std::runtime_error( "unknown audio codec" );
98+
throw std::runtime_error( "unknown video codec" );
9999
}
100100

101101
if( ( m_codecContext = avcodec_alloc_context3( m_codec ) ) == NULL )

src/AvTranscoder/InputFile.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ InputFile::InputFile( const std::string& filename )
3232
av_register_all(); // Warning: should be called only once
3333
if( avformat_open_input( &m_formatContext, m_filename.c_str(), NULL, NULL ) < 0 )
3434
{
35-
throw std::runtime_error( "unable to open file" );
35+
std::string msg = "unable to open file: ";
36+
msg += m_filename;
37+
throw std::runtime_error( msg );
3638
}
3739

3840
// update format context informations from streams

src/AvTranscoder/InputStream.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ VideoDesc InputStream::getVideoDesc() const
8787

8888
if( m_formatContext->streams[m_streamIndex]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
8989
{
90-
return VideoDesc( AV_CODEC_ID_NONE );
90+
throw std::runtime_error( "unable to get video descriptor on non-video stream" );
9191
}
9292

9393
AVCodecContext* codecContext = m_formatContext->streams[m_streamIndex]->codec;
@@ -107,7 +107,7 @@ AudioDesc InputStream::getAudioDesc() const
107107

108108
if( m_formatContext->streams[m_streamIndex]->codec->codec_type != AVMEDIA_TYPE_AUDIO )
109109
{
110-
return AudioDesc( AV_CODEC_ID_NONE );
110+
throw std::runtime_error( "unable to get audio descriptor on non-audio stream" );
111111
}
112112

113113
AVCodecContext* codecContext = m_formatContext->streams[m_streamIndex]->codec;

src/AvTranscoder/InputStreamAudio.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
2424
, m_frame ( NULL )
2525
, m_selectedStream( -1 )
2626
{
27-
av_register_all();
27+
avcodec_register_all();
2828

2929
m_codec = avcodec_find_decoder( m_inputStream.getAudioDesc().getAudioCodecId() );
3030
if( m_codec == NULL )
@@ -42,7 +42,10 @@ InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
4242

4343
if( ret < 0 || m_codecContext == NULL || m_codec == NULL )
4444
{
45-
throw std::runtime_error( "unable open audio codec" );
45+
avcodec_close( m_codecContext );
46+
std::string msg = "unable open audio codec: ";
47+
msg += m_codecContext->codec_name;
48+
throw std::runtime_error( msg );
4649
}
4750

4851
#if LIBAVCODEC_VERSION_MAJOR > 54

src/AvTranscoder/Metadatas/DataStreamProperties.hpp

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,73 @@ extern "C" {
1111
#include <libavutil/pixdesc.h>
1212
}
1313

14+
#include <bitset>
15+
1416
namespace avtranscoder
1517
{
1618

17-
DataProperties dataStreamInfo( const AVFormatContext* formatContext, const size_t index )
19+
void detectAncillaryData( AVFormatContext* formatContext, const int index )
20+
{
21+
AVPacket pkt;
22+
av_init_packet( &pkt );
23+
24+
bool detection = false;
25+
26+
while( ! av_read_frame( formatContext, &pkt ) )
27+
{
28+
if( pkt.stream_index == index )
29+
{
30+
std::cout << "start detect packet" << std::endl;
31+
size_t offset = 0;
32+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
33+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
34+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
35+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
36+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
37+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
38+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
39+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
40+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
41+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
42+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
43+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
44+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
45+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
46+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
47+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
48+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
49+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
50+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
51+
std::cout << offset << " - " << (int) pkt.data[ offset ] << " | " << std::bitset<8>( pkt.data[ offset ] ) << std::endl; offset++;
52+
53+
54+
unsigned short numberOfLines = (unsigned int) ( pkt.data[0] << 8 ) + pkt.data[1];
55+
56+
std::cout << "[data] number of lines " << numberOfLines << std::endl;
57+
58+
detection = true;
59+
}
60+
61+
av_free_packet( &pkt );
62+
63+
if( detection )
64+
break;
65+
}
66+
}
67+
68+
DataProperties dataStreamInfo( AVFormatContext* formatContext, const size_t index )
1869
{
1970
DataProperties dp;
20-
dp.streamId = index;
71+
dp.streamId = index;
72+
73+
AVCodecContext* codec_context = formatContext->streams[index]->codec;
74+
75+
// dp.codecName = codec_context->codec_name;
76+
// dp.codecLongName = codec_context->codec_name;
77+
// dp.codecId = codec_context->codec_id;
78+
79+
//detectAncillaryData( formatContext, index );
80+
2181
return dp;
2282
}
2383

src/AvTranscoder/OutputFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void OutputFile::addVideoStream( const VideoDesc& videoDesc )
8282
// to move in endSetup
8383
if( avformat_write_header( formatContext, NULL ) != 0 )
8484
{
85-
throw std::runtime_error( "could not write header" );
85+
throw std::runtime_error( "add video stream: could not write header" );
8686
}
8787
}
8888

@@ -102,7 +102,7 @@ void OutputFile::addAudioStream( const AudioDesc& audioDesc )
102102
// to move in endSetup
103103
if( avformat_write_header( formatContext, NULL ) != 0 )
104104
{
105-
throw std::runtime_error( "could not write header" );
105+
throw std::runtime_error( "add audio stream: could not write header" );
106106
}
107107
}
108108

0 commit comments

Comments
 (0)