Skip to content

Commit bec1c1b

Browse files
stating increase read performances
1 parent 96cdd4e commit bec1c1b

File tree

7 files changed

+53
-60
lines changed

7 files changed

+53
-60
lines changed

src/AvTranscoder/InputFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ InputFile::InputFile( const std::string& filename )
4747

4848
for( size_t streamIndex = 0; streamIndex < m_formatContext->nb_streams; ++streamIndex )
4949
{
50-
m_inputStreams.push_back( InputStream( m_filename, streamIndex ) );
50+
m_inputStreams.push_back( InputStream( this, streamIndex ) );
5151
}
5252
}
5353

src/AvTranscoder/InputFile.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "DatasStructures/AudioDesc.hpp"
66
#include "DatasStructures/VideoDesc.hpp"
77
#include "Metadatas/MediaMetadatasStructures.hpp"
8-
#include "InputStream.hpp"
9-
108

119
#include <string>
1210
#include <vector>
@@ -20,6 +18,8 @@ class AVCodecContext;
2018
namespace avtranscoder
2119
{
2220

21+
class InputStream;
22+
2323
class AvExport InputFile
2424
{
2525
public:
@@ -36,6 +36,8 @@ class AvExport InputFile
3636

3737
::avtranscoder::InputStream& getStream( size_t index );
3838

39+
AVFormatContext* getFormatContext() const { return m_formatContext; }
40+
3941
protected:
4042
AVFormatContext* m_formatContext;
4143
Properties m_properties;
@@ -45,4 +47,6 @@ class AvExport InputFile
4547

4648
}
4749

50+
#include "InputStream.hpp"
51+
4852
#endif

src/AvTranscoder/InputStream.cpp

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,15 @@ extern "C" {
1717
namespace avtranscoder
1818
{
1919

20-
InputStream::InputStream( const std::string& filename, const size_t streamIndex )
21-
: m_formatContext( NULL )
20+
InputStream::InputStream( const InputFile* inputFile, const size_t streamIndex )
21+
: m_inputFile( inputFile )
2222
, m_packetDuration( 0 )
2323
, m_streamIndex( streamIndex )
2424
{
25-
init( filename );
2625
};
2726

2827
InputStream::~InputStream( )
2928
{
30-
if( m_formatContext != NULL )
31-
{
32-
avformat_close_input( &m_formatContext );
33-
m_formatContext = NULL;
34-
}
3529
}
3630

3731
bool InputStream::readNextPacket( DataStream& data )
@@ -61,10 +55,10 @@ bool InputStream::readNextPacket( DataStream& data )
6155

6256
bool InputStream::readNextPacket( AVPacket& packet ) const
6357
{
64-
assert( m_formatContext != NULL );
58+
assert( m_inputFile->getFormatContext() != NULL );
6559
while( 1 )
6660
{
67-
int ret = av_read_frame( m_formatContext, &packet );
61+
int ret = av_read_frame( m_inputFile->getFormatContext(), &packet );
6862
if( ret < 0 ) // error or end of file
6963
{
7064
return false;
@@ -85,15 +79,15 @@ bool InputStream::readNextPacket( AVPacket& packet ) const
8579

8680
VideoDesc InputStream::getVideoDesc() const
8781
{
88-
assert( m_formatContext != NULL );
89-
assert( m_streamIndex <= m_formatContext->nb_streams );
82+
assert( m_inputFile->getFormatContext() != NULL );
83+
assert( m_streamIndex <= m_inputFile->getFormatContext()->nb_streams );
9084

91-
if( m_formatContext->streams[m_streamIndex]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
85+
if( m_inputFile->getFormatContext()->streams[m_streamIndex]->codec->codec_type != AVMEDIA_TYPE_VIDEO )
9286
{
9387
throw std::runtime_error( "unable to get video descriptor on non-video stream" );
9488
}
9589

96-
AVCodecContext* codecContext = m_formatContext->streams[m_streamIndex]->codec;
90+
AVCodecContext* codecContext = m_inputFile->getFormatContext()->streams[m_streamIndex]->codec;
9791

9892
VideoDesc desc( codecContext->codec_id );
9993

@@ -105,15 +99,15 @@ VideoDesc InputStream::getVideoDesc() const
10599

106100
AudioDesc InputStream::getAudioDesc() const
107101
{
108-
assert( m_formatContext != NULL );
109-
assert( m_streamIndex <= m_formatContext->nb_streams );
102+
assert( m_inputFile->getFormatContext() != NULL );
103+
assert( m_streamIndex <= m_inputFile->getFormatContext()->nb_streams );
110104

111-
if( m_formatContext->streams[m_streamIndex]->codec->codec_type != AVMEDIA_TYPE_AUDIO )
105+
if( m_inputFile->getFormatContext()->streams[m_streamIndex]->codec->codec_type != AVMEDIA_TYPE_AUDIO )
112106
{
113107
throw std::runtime_error( "unable to get audio descriptor on non-audio stream" );
114108
}
115109

116-
AVCodecContext* codecContext = m_formatContext->streams[m_streamIndex]->codec;
110+
AVCodecContext* codecContext = m_inputFile->getFormatContext()->streams[m_streamIndex]->codec;
117111

118112
AudioDesc desc( codecContext->codec_id );
119113

@@ -125,37 +119,12 @@ AudioDesc InputStream::getAudioDesc() const
125119

126120
double InputStream::getDuration() const
127121
{
128-
return 1.0 * m_formatContext->duration / AV_TIME_BASE;
122+
return 1.0 * m_inputFile->getFormatContext()->duration / AV_TIME_BASE;
129123
}
130124

131125
double InputStream::getPacketDuration() const
132126
{
133-
return m_packetDuration * av_q2d( m_formatContext->streams[m_streamIndex]->time_base );
134-
}
135-
136-
void InputStream::init( const std::string& filename )
137-
{
138-
av_register_all(); // Warning: should be called only once
139-
if( avformat_open_input( &m_formatContext, filename.c_str(), NULL, NULL ) < 0 )
140-
{
141-
throw std::runtime_error( "unable to open file" );
142-
}
143-
144-
// update format context informations from streams
145-
if( avformat_find_stream_info( m_formatContext, NULL ) < 0 )
146-
{
147-
avformat_close_input( &m_formatContext );
148-
m_formatContext = NULL;
149-
throw std::runtime_error( "unable to find stream informations" );
150-
}
151-
152-
assert( m_formatContext != NULL );
153-
154-
if( m_formatContext->streams[m_streamIndex]->codec->codec_type == AVMEDIA_TYPE_AUDIO )
155-
{
156-
// TODO: get dynamic buffer size in relation with video stream
157-
m_formatContext->streams[m_streamIndex]->codec->block_align = 3 * 1920; // 24 bits per audio sample at 25fps
158-
}
127+
return m_packetDuration * av_q2d( m_inputFile->getFormatContext()->streams[m_streamIndex]->time_base );
159128
}
160129

161130
}

src/AvTranscoder/InputStream.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ extern "C" {
1515
#include <string>
1616
#include <iostream>
1717

18+
#include "InputFile.hpp"
19+
1820
namespace avtranscoder
1921
{
2022

2123
class AvExport InputStream
2224
{
2325
public:
24-
InputStream( const std::string& filename, const size_t streamIndex );
26+
InputStream( const InputFile* inputFile, const size_t streamIndex );
2527
~InputStream( );
2628

2729
InputStream( const InputStream& inputStream )
28-
: m_formatContext( NULL )
30+
: m_inputFile( inputStream.m_inputFile )
2931
, m_streamIndex( inputStream.m_streamIndex )
3032
{
31-
init( inputStream.m_formatContext->filename );
3233
}
3334

3435
size_t getStreamIndex() const { return m_streamIndex; }
@@ -45,12 +46,10 @@ class AvExport InputStream
4546
// protected:
4647
bool readNextPacket( AVPacket& packet ) const;
4748

48-
4949
private:
50-
void init( const std::string& filename );
50+
const InputFile* m_inputFile;
51+
std::vector<DataStream> m_streamCache;
5152

52-
private:
53-
AVFormatContext* m_formatContext;
5453
int m_packetDuration;
5554
size_t m_streamIndex;
5655
};

src/AvTranscoder/InputStreamAudio.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
2626
{
2727
avcodec_register_all();
2828

29+
std::cout << "Audio codec Id : " << m_inputStream.getAudioDesc().getAudioCodecId() << std::endl;
30+
2931
m_codec = avcodec_find_decoder( m_inputStream.getAudioDesc().getAudioCodecId() );
3032
if( m_codec == NULL )
3133
{
@@ -38,13 +40,26 @@ InputStreamAudio::InputStreamAudio( const InputStream& inputStream )
3840
throw std::runtime_error( "unable to find context for codec" );
3941
}
4042

43+
std::cout << "Audio codec Id : " << m_codecContext->codec_id << std::endl;
44+
std::cout << "Audio codec Id : " << m_codec->long_name << std::endl;
45+
4146
int ret = avcodec_open2( m_codecContext, m_codec, NULL );
4247

48+
std::cout << "ret value : " << ret << std::endl;
49+
4350
if( ret < 0 || m_codecContext == NULL || m_codec == NULL )
4451
{
45-
avcodec_close( m_codecContext );
4652
std::string msg = "unable open audio codec: ";
47-
msg += m_codecContext->codec_name;
53+
msg += m_codec->long_name;
54+
msg += " (";
55+
msg += m_codec->name;
56+
msg += ")";
57+
avcodec_close( m_codecContext );
58+
59+
char err[250];
60+
61+
av_make_error_string( err, 250, ret );
62+
std::cout << err << std::endl;
4863
throw std::runtime_error( msg );
4964
}
5065

src/AvTranscoder/InputStreamVideo.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,13 @@ InputStreamVideo::InputStreamVideo( const InputStream& inputStream )
4545

4646
if( ret < 0 || m_codecContext == NULL || m_codec == NULL )
4747
{
48-
throw std::runtime_error( "unable open video codec" );
48+
std::string msg = "unable open video codec: ";
49+
msg += m_codec->long_name;
50+
msg += " (";
51+
msg += m_codec->name;
52+
msg += ")";
53+
avcodec_close( m_codecContext );
54+
throw std::runtime_error( msg );
4955
}
5056

5157
#if LIBAVCODEC_VERSION_MAJOR > 54

src/AvTranscoder/Transcoder.tcc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ void Transcoder::add( const std::string& filename, const size_t streamIndex )
2121
{
2222
case AVMEDIA_TYPE_VIDEO:
2323
{
24-
_inputStreams.push_back( avtranscoder::InputStream( filename, streamIndex ) );
24+
_inputStreams.push_back( inputFile.getStream( streamIndex ) );
2525
_outputFile.addVideoStream( _inputStreams.back().getVideoDesc() );
2626
break;
2727
}
2828
case AVMEDIA_TYPE_AUDIO:
2929
{
30-
_inputStreams.push_back( avtranscoder::InputStream( filename, streamIndex ) );
30+
_inputStreams.push_back( inputFile.getStream( streamIndex ) );
3131
_outputFile.addAudioStream( _inputStreams.back().getAudioDesc() );
3232
break;
3333
}

0 commit comments

Comments
 (0)