Skip to content

Commit a73fd30

Browse files
simpify API using InputStream
1 parent 534c6a7 commit a73fd30

13 files changed

+243
-223
lines changed

app/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Import( 'AvTranscoder' )
2-
Import( 'AvTranscoder_jar' )
2+
#Import( 'AvTranscoder_jar' )
33

44
env = Environment().Clone()
55

app/avMeta/avMeta.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ void displayMetadatas( const char* filename )
2323
size_t keyWidth = 32;
2424

2525

26-
InputFile input;
27-
input.setup( filename ).analyse();
26+
InputFile input( filename );
27+
input.analyse();
2828
std::cout << std::left;
2929
std::cout << separator << " Wrapper " << separator << std::endl;
3030
std::cout << std::setw( keyWidth ) << "filename" << ": " << input.getProperties().filename << std::endl;

app/avTranscoder/avTranscoder.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
1919
{
2020
using namespace avtranscoder;
2121

22-
InputFile input;
23-
input.setup( inputfilename ).analyse();
24-
25-
// init video decoders
26-
InputStreamVideo inputStreamVideo; // take the first video stream per default
27-
2822
av_log_set_level( AV_LOG_FATAL );
2923
av_log_set_level( AV_LOG_DEBUG );
30-
3124

32-
inputStreamVideo.setup( inputfilename, 0 );
25+
InputFile inputFile( inputfilename );
26+
inputFile.analyse();
27+
28+
// init video decoders
29+
InputStreamVideo inputStreamVideo( inputFile.getStream( 0 ) );
3330

3431
//dVideo.set( key, value );
3532

@@ -51,8 +48,8 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
5148
oPixel.setBitsPerPixel( 16 );
5249

5350
ImageDesc imageDesc;
54-
imageDesc.setWidth ( input.getProperties().videoStreams.at(0).width );
55-
imageDesc.setHeight( input.getProperties().videoStreams.at(0).height );
51+
imageDesc.setWidth ( inputFile.getProperties().videoStreams.at(0).width );
52+
imageDesc.setHeight( inputFile.getProperties().videoStreams.at(0).height );
5653
imageDesc.setPixel ( oPixel );
5754

5855
Image sourceImage( imageDesc );
@@ -100,10 +97,7 @@ void transcodeVideo( const char* inputfilename, const char* outputFilename )
10097
exit( -1 );
10198
}
10299

103-
InputFile inputFile;
104-
inputFile.setup( inputfilename );
105-
106-
of.addVideoStream( inputFile.getVideoDesc( 0 ) );
100+
of.addVideoStream( inputFile.getStream( 0 ).getVideoDesc() );
107101
/*of.addAudioStream();
108102
of.addAudioStream();
109103
of.addAudioStream();

src/AvTranscoder/InputFile.cpp

Lines changed: 30 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,11 @@ extern "C" {
2525
namespace avtranscoder
2626
{
2727

28-
InputFile::InputFile()
28+
InputFile::InputFile( const std::string& filename )
2929
: m_formatContext ( NULL )
30-
, m_stream ( NULL )
31-
, m_filename ( "" )
32-
, m_packetCount ( 0 )
30+
, m_filename ( filename )
3331
{
3432
av_register_all(); // Warning: should be called only once
35-
}
36-
37-
InputFile::~InputFile()
38-
{
39-
if( m_formatContext != NULL )
40-
{
41-
avformat_close_input( &m_formatContext );
42-
m_formatContext = NULL;
43-
}
44-
}
45-
46-
InputFile& InputFile::setup( const std::string& filename )
47-
{
48-
m_filename = filename;
49-
assert( m_formatContext == NULL );
50-
5133
if( avformat_open_input( &m_formatContext, m_filename.c_str(), NULL, NULL ) < 0 )
5234
{
5335
throw std::runtime_error( "unable to open file" );
@@ -60,26 +42,35 @@ InputFile& InputFile::setup( const std::string& filename )
6042
m_formatContext = NULL;
6143
throw std::runtime_error( "unable to find stream informations" );
6244
}
45+
}
6346

64-
return *this;
47+
InputFile::~InputFile()
48+
{
49+
if( m_formatContext != NULL )
50+
{
51+
avformat_close_input( &m_formatContext );
52+
m_formatContext = NULL;
53+
}
6554
}
6655

6756
InputFile& InputFile::analyse()
6857
{
69-
properties.filename = m_formatContext->filename;
70-
properties.formatName = m_formatContext->iformat->name;
71-
properties.formatLongName = m_formatContext->iformat->long_name;
72-
properties.streamsCount = m_formatContext->nb_streams;
73-
properties.programsCount = m_formatContext->nb_programs;
74-
properties.startTime = 1.0 * (uint)m_formatContext->start_time / AV_TIME_BASE;
75-
properties.duration = 1.0 * m_formatContext->duration / AV_TIME_BASE;
76-
properties.bitRate = m_formatContext->bit_rate;
77-
properties.packetSize = m_formatContext->packet_size;
58+
assert( m_formatContext != NULL );
59+
60+
m_properties.filename = m_formatContext->filename;
61+
m_properties.formatName = m_formatContext->iformat->name;
62+
m_properties.formatLongName = m_formatContext->iformat->long_name;
63+
m_properties.streamsCount = m_formatContext->nb_streams;
64+
m_properties.programsCount = m_formatContext->nb_programs;
65+
m_properties.startTime = 1.0 * (uint)m_formatContext->start_time / AV_TIME_BASE;
66+
m_properties.duration = 1.0 * m_formatContext->duration / AV_TIME_BASE;
67+
m_properties.bitRate = m_formatContext->bit_rate;
68+
m_properties.packetSize = m_formatContext->packet_size;
7869

7970
AVDictionaryEntry *tag = NULL;
8071
while( ( tag = av_dict_get( m_formatContext->metadata, "", tag, AV_DICT_IGNORE_SUFFIX ) ) )
8172
{
82-
properties.metadatas.push_back( std::pair<std::string, std::string>( tag->key, tag->value ) );
73+
m_properties.metadatas.push_back( std::pair<std::string, std::string>( tag->key, tag->value ) );
8374
}
8475

8576
for( size_t streamId = 0; streamId < m_formatContext->nb_streams; streamId++ )
@@ -88,32 +79,32 @@ InputFile& InputFile::analyse()
8879
{
8980
case AVMEDIA_TYPE_VIDEO:
9081
{
91-
properties.videoStreams.push_back( videoStreamInfo( m_formatContext, streamId ) );
82+
m_properties.videoStreams.push_back( videoStreamInfo( m_formatContext, streamId ) );
9283
break;
9384
}
9485
case AVMEDIA_TYPE_AUDIO:
9586
{
96-
properties.audioStreams.push_back( audioStreamInfo( m_formatContext, streamId ) );
87+
m_properties.audioStreams.push_back( audioStreamInfo( m_formatContext, streamId ) );
9788
break;
9889
}
9990
case AVMEDIA_TYPE_DATA:
10091
{
101-
properties.dataStreams.push_back( dataStreamInfo( m_formatContext, streamId ) );
92+
m_properties.dataStreams.push_back( dataStreamInfo( m_formatContext, streamId ) );
10293
break;
10394
}
10495
case AVMEDIA_TYPE_SUBTITLE:
10596
{
106-
properties.subtitleStreams.push_back( subtitleStreamInfo( m_formatContext, streamId ) );
97+
m_properties.subtitleStreams.push_back( subtitleStreamInfo( m_formatContext, streamId ) );
10798
break;
10899
}
109100
case AVMEDIA_TYPE_ATTACHMENT:
110101
{
111-
properties.attachementStreams.push_back( attachementStreamInfo( m_formatContext, streamId ) );
102+
m_properties.attachementStreams.push_back( attachementStreamInfo( m_formatContext, streamId ) );
112103
break;
113104
}
114105
case AVMEDIA_TYPE_UNKNOWN:
115106
{
116-
properties.unknownStreams.push_back( unknownStreamInfo( m_formatContext, streamId ) );
107+
m_properties.unknownStreams.push_back( unknownStreamInfo( m_formatContext, streamId ) );
117108
break;
118109
}
119110
case AVMEDIA_TYPE_NB:
@@ -127,98 +118,9 @@ InputFile& InputFile::analyse()
127118
return *this;
128119
}
129120

130-
VideoDesc InputFile::getVideoDesc( size_t videoStreamId )
121+
InputStream InputFile::getStream( size_t index )
131122
{
132-
int selectedStream = -1;
133-
size_t videoStreamCount = 0;
134-
135-
for( size_t streamId = 0; streamId < m_formatContext->nb_streams; streamId++ )
136-
{
137-
if( m_formatContext->streams[streamId]->codec->codec_type == AVMEDIA_TYPE_VIDEO )
138-
{
139-
if( videoStreamCount == videoStreamId )
140-
{
141-
selectedStream = streamId;
142-
}
143-
videoStreamCount++;
144-
}
145-
}
146-
147-
if( selectedStream == -1 )
148-
{
149-
throw std::runtime_error( "unable to find video stream " );
150-
}
151-
152-
AVCodecContext* codecContext = m_formatContext->streams[selectedStream]->codec;
153-
154-
VideoDesc desc( codecContext->codec_id );
155-
156-
desc.setImageParameters( codecContext->width, codecContext->height, codecContext->pix_fmt );
157-
desc.setTimeBase( codecContext->time_base.num, codecContext->time_base.den );
158-
159-
return desc;
160-
}
161-
162-
AudioDesc InputFile::getAudioDesc( size_t audioStreamId )
163-
{
164-
int selectedStream = -1;
165-
size_t audioStreamCount = 0;
166-
167-
for( size_t streamId = 0; streamId < m_formatContext->nb_streams; streamId++ )
168-
{
169-
if( m_formatContext->streams[streamId]->codec->codec_type == AVMEDIA_TYPE_AUDIO )
170-
{
171-
if( audioStreamCount == audioStreamId )
172-
{
173-
selectedStream = streamId;
174-
}
175-
audioStreamCount++;
176-
}
177-
}
178-
179-
if( selectedStream == -1 )
180-
{
181-
throw std::runtime_error( "unable to find audio stream " );
182-
}
183-
184-
AVCodecContext* codecContext = m_formatContext->streams[selectedStream]->codec;
185-
186-
AudioDesc desc( codecContext->codec_id );
187-
188-
return desc;
189-
}
190-
191-
bool InputFile::unwrap( DataStream& data, const size_t streamIndex )
192-
{
193-
AVPacket packet;
194-
av_init_packet( &packet );
195-
196-
readNextPacket( packet, streamIndex );
197-
198-
data.getBuffer().resize( packet.size );
199-
memcpy( data.getPtr(), packet.data, packet.size );
200-
201-
av_free_packet( &packet );
202-
203-
return true;
204-
}
205-
206-
bool InputFile::readNextPacket( AVPacket& packet, const size_t streamIndex )
207-
{
208-
while( 1 )
209-
{
210-
int ret = av_read_frame( m_formatContext, &packet );
211-
if( ret < 0 ) // error or end of file
212-
{
213-
return false;
214-
}
215-
216-
// We only read one stream and skip others
217-
if( packet.stream_index == (int)streamIndex )
218-
{
219-
return true;
220-
}
221-
}
123+
InputStream inputStream( m_filename, index );
222124
}
223125

224126
}

src/AvTranscoder/InputFile.hpp

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

911
#include <string>
1012
#include <vector>
@@ -21,35 +23,21 @@ namespace avtranscoder
2123
class InputFile
2224
{
2325
public:
24-
InputFile();
26+
InputFile( const std::string& filename );
2527
~InputFile();
2628

27-
InputFile& setup( const std::string& file );
28-
2929
// *** Metadatas section ***
3030
// run the analyse on the file after a setup.
3131
InputFile& analyse();
3232
// get properties on the file
33-
const Properties& getProperties() const { return properties; }
34-
35-
36-
VideoDesc getVideoDesc( size_t videoStreamId );
37-
AudioDesc getAudioDesc( size_t audioStreamId );
38-
33+
const Properties& getProperties() const { return m_properties; }
3934

40-
bool unwrap( DataStream& data, const size_t streamIndex );
41-
42-
protected:
43-
bool readNextPacket( AVPacket& packet, const size_t streamIndex );
35+
InputStream getStream( size_t index );
4436

4537
protected:
4638
AVFormatContext* m_formatContext;
47-
AVStream* m_stream;
48-
49-
Properties properties;
50-
39+
Properties m_properties;
5140
std::string m_filename;
52-
size_t m_packetCount;
5341
};
5442

5543
}

0 commit comments

Comments
 (0)