Skip to content

Commit 71f1d0d

Browse files
author
Clement Champetier
committed
Merge branch 'develop' of https://github.com/mikrosimage/avTranscoder into fix_memory_leaks
Conflicts: src/AvTranscoder/file/OutputFile.hpp
2 parents 0816e65 + ced56b1 commit 71f1d0d

17 files changed

+178
-91
lines changed

app/avProcessor/avProcessor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <AvTranscoder/transcoder/Transcoder.hpp>
2+
#include <AvTranscoder/file/OutputFile.hpp>
23
#include <AvTranscoder/progress/ConsoleProgress.hpp>
34

45
#include <iostream>

src/AvTranscoder/decoder/AudioDecoder.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,29 @@ bool AudioDecoder::decodeNextFrame()
161161
return true;
162162
}
163163

164+
void AudioDecoder::setProfile( const ProfileLoader::Profile& profile )
165+
{
166+
// set threads if not in profile
167+
if( ! profile.count( "threads" ) )
168+
_inputStream->getAudioCodec().getOption( "threads" ).setString( "auto" );
169+
170+
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
171+
{
172+
if( (*it).first == constants::avProfileIdentificator ||
173+
(*it).first == constants::avProfileIdentificatorHuman ||
174+
(*it).first == constants::avProfileType )
175+
continue;
176+
177+
try
178+
{
179+
Option& decodeOption = _inputStream->getAudioCodec().getOption( (*it).first );
180+
decodeOption.setString( (*it).second );
181+
}
182+
catch( std::exception& e )
183+
{
184+
std::cout << "[AudioDecoder] warning - can't set option " << (*it).first << " to " << (*it).second << ": " << e.what() << std::endl;
185+
}
186+
}
187+
}
188+
164189
}

src/AvTranscoder/decoder/AudioDecoder.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define _AV_TRANSCODER_ESSENCE_STREAM_AV_INPUT_AUDIO_HPP_
33

44
#include "IDecoder.hpp"
5+
#include <AvTranscoder/ProfileLoader.hpp>
56

67
struct AVFrame;
78

@@ -21,6 +22,8 @@ class AvExport AudioDecoder : public IDecoder
2122
bool decodeNextFrame( Frame& frameBuffer );
2223
bool decodeNextFrame( Frame& frameBuffer, const size_t subStreamIndex );
2324

25+
void setProfile( const ProfileLoader::Profile& profile );
26+
2427
private:
2528
bool decodeNextFrame();
2629

src/AvTranscoder/decoder/VideoDecoder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ void VideoDecoder::flushDecoder()
119119

120120
void VideoDecoder::setProfile( const ProfileLoader::Profile& profile )
121121
{
122+
// set threads if not in profile
123+
if( ! profile.count( "threads" ) )
124+
_inputStream->getVideoCodec().getOption( "threads" ).setString( "auto" );
125+
122126
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
123127
{
124128
if( (*it).first == constants::avProfileIdentificator ||

src/AvTranscoder/encoder/AudioEncoder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ void AudioEncoder::setProfile( const ProfileLoader::Profile& profile, const Audi
150150
// set sampleRate, number of channels, sample format
151151
_codec.setAudioParameters( frameDesc );
152152

153+
// set threads if not in profile
154+
if( ! profile.count( "threads" ) )
155+
_codec.getOption( "threads" ).setString( "auto" );
156+
153157
// set encoder options
154158
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
155159
{

src/AvTranscoder/encoder/VideoEncoder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ void VideoEncoder::setProfile( const ProfileLoader::Profile& profile, const avtr
133133
// set width, height, pixel format, fps
134134
_codec.setImageParameters( frameDesc );
135135

136+
// set threads if not in profile
137+
if( ! profile.count( "threads" ) )
138+
_codec.getOption( "threads" ).setString( "auto" );
139+
136140
// set encoder options
137141
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
138142
{

src/AvTranscoder/file/IOutputFile.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifndef _AV_TRANSCODER_FILE_IOUTPUT_FILE_HPP_
2+
#define _AV_TRANSCODER_FILE_IOUTPUT_FILE_HPP_
3+
4+
#include <AvTranscoder/common.hpp>
5+
6+
#include <AvTranscoder/codec/VideoCodec.hpp>
7+
#include <AvTranscoder/codec/AudioCodec.hpp>
8+
#include <AvTranscoder/codec/DataCodec.hpp>
9+
10+
#include <AvTranscoder/stream/OutputStream.hpp>
11+
#include <AvTranscoder/ProfileLoader.hpp>
12+
13+
#include <string>
14+
#include <exception>
15+
16+
namespace avtranscoder
17+
{
18+
19+
/**
20+
* @brief IOutputfile is the interface to wrap and write medias.
21+
* It can be overloaded to integrate custom wrapper.
22+
**/
23+
class AvExport IOutputFile
24+
{
25+
public:
26+
virtual ~IOutputFile() {};
27+
28+
/**
29+
* @brief Initialize the wrapper
30+
**/
31+
virtual bool setup() = 0;
32+
33+
/**
34+
* @brief Add a video output stream
35+
* @note call setup() before adding any stream
36+
* @param videoCodec description of output stream
37+
**/
38+
virtual IOutputStream& addVideoStream( const VideoCodec& videoCodec )
39+
{
40+
throw std::logic_error("function is not implemented");
41+
}
42+
43+
/**
44+
* @brief Add an audio output stream
45+
* @note call setup() before adding any stream
46+
* @param audioCodec description of output stream
47+
**/
48+
virtual IOutputStream& addAudioStream( const AudioCodec& audioCodec )
49+
{
50+
throw std::logic_error("function is not implemented");
51+
}
52+
53+
/**
54+
* @brief Add a data output stream
55+
* @note call setup() before adding any stream
56+
* @param dataCodec description of output stream
57+
**/
58+
virtual IOutputStream& addDataStream( const DataCodec& dataCodec )
59+
{
60+
throw std::logic_error("function is not implemented");
61+
}
62+
63+
/**
64+
* @brief Write the header of file (if necessary)
65+
**/
66+
virtual bool beginWrap() = 0;
67+
68+
/**
69+
* @brief Wrap a packet of data in the output ressource
70+
* @param data coded packet information for the current stream
71+
* @param streamId refers to the stream in output ressource
72+
**/
73+
virtual IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamIndex ) = 0;
74+
75+
/**
76+
* @brief Write the footer of file (if necessary)
77+
**/
78+
virtual bool endWrap() = 0;
79+
80+
/**
81+
* @brief Get the output stream
82+
* @param streamId select the output stream
83+
* @return the output stream reference
84+
**/
85+
virtual IOutputStream& getStream( const size_t streamIndex ) = 0;
86+
};
87+
88+
}
89+
90+
#endif

src/AvTranscoder/file/OutputFile.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const si
120120

121121
av_free_packet( &packet );
122122

123-
double currentStreamDuration = getProgressDuration( 0 );
123+
// get duration of stream 0
124+
double currentStreamDuration = _outputStreams.at( 0 )->getStreamDuration();
124125
if( currentStreamDuration < _previousProcessedStreamDuration )
125126
{
126127
// if the current stream is strictly shorter than the previous, wait for more data
@@ -202,10 +203,4 @@ void OutputFile::setProfile( const ProfileLoader::Profile& profile )
202203
}
203204
}
204205

205-
double OutputFile::getProgressDuration( const size_t streamIndex )
206-
{
207-
AVStream& outputStream = _formatContext.getAVStream( streamIndex );
208-
return av_q2d( outputStream.time_base ) * outputStream.cur_dts;
209-
}
210-
211206
}

src/AvTranscoder/file/OutputFile.hpp

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
#ifndef _AV_TRANSCODER_FILE_OUTPUT_FILE_HPP_
22
#define _AV_TRANSCODER_FILE_OUTPUT_FILE_HPP_
33

4-
#include <AvTranscoder/common.hpp>
4+
#include <AvTranscoder/file/IOutputFile.hpp>
5+
56
#include <AvTranscoder/mediaProperty/util.hpp>
67
#include <AvTranscoder/file/FormatContext.hpp>
78

8-
#include <AvTranscoder/codec/VideoCodec.hpp>
9-
#include <AvTranscoder/codec/AudioCodec.hpp>
10-
#include <AvTranscoder/codec/DataCodec.hpp>
11-
12-
#include <AvTranscoder/stream/OutputStream.hpp>
13-
#include <AvTranscoder/ProfileLoader.hpp>
14-
15-
#include <string>
169
#include <vector>
1710

1811
namespace avtranscoder
1912
{
2013

2114
/**
22-
* @brief Outputfile is a simple C++ API to wrap and write medias.\n
23-
* the default implentation use avformat wrapper frome the LibAV/FFMpeg.\n
24-
* It can be overloaded to integrate custom wrapper.
15+
* @brief Outputfile is the default implentation of wrapper which uses LibAV/FFMpeg.
2516
**/
26-
class AvExport OutputFile
17+
class AvExport OutputFile : public IOutputFile
2718
{
2819
public:
2920
/**
@@ -32,79 +23,40 @@ class AvExport OutputFile
3223
**/
3324
OutputFile( const std::string& filename = "" );
3425

35-
virtual ~OutputFile();
26+
~OutputFile();
3627

3728
/**
3829
* @brief Initialize the OutputFile, create format context to wrap essences into output file.
3930
* @note call this before adding streams using addVideoStream() or addAudioStream()
4031
* @exception ios_base::failure launched if unable to guess format or open output
4132
**/
42-
virtual bool setup();
43-
44-
/**
45-
* @brief Add an video output stream using the description.
46-
* @note call setup() before adding any stream
47-
* @param videoDesc description of output stream
48-
**/
49-
virtual IOutputStream& addVideoStream( const VideoCodec& videoDesc );
50-
51-
/**
52-
* @brief Add an audio output stream using the description.
53-
* @note call setup() before adding any stream
54-
* @param audioDesc description of output stream
55-
**/
56-
virtual IOutputStream& addAudioStream( const AudioCodec& audioDesc );
57-
58-
/**
59-
* @brief Add an data output stream using the description.
60-
* @note call setup() before adding any stream
61-
* @param dataDesc description of output stream
62-
**/
63-
virtual IOutputStream& addDataStream( const DataCodec& dataDesc );
64-
65-
/**
66-
* @brief get the output stream description.
67-
* @param streamId select the output stream
68-
* @return the output stream reference
69-
**/
70-
virtual IOutputStream& getStream( const size_t streamId );
33+
bool setup();
7134

72-
/**
73-
* @brief Initialise the wrapping
74-
* @note this method write the header of file if necessary
75-
**/
76-
virtual bool beginWrap( );
35+
IOutputStream& addVideoStream( const VideoCodec& videoDesc );
36+
IOutputStream& addAudioStream( const AudioCodec& audioDesc );
37+
IOutputStream& addDataStream( const DataCodec& dataDesc );
7738

78-
/**
79-
* @brief Wrap a packet of data in the output ressource
80-
* @param data coded packet information for the current stream
81-
* @param streamId refers to the stream in output ressource
82-
**/
83-
virtual IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
39+
bool beginWrap();
40+
IOutputStream::EWrappingStatus wrap( const CodedData& data, const size_t streamId );
41+
bool endWrap();
8442

85-
/**
86-
* @brief Finalize the end of the wrapping
87-
* @note this method write the footer of file if necessary
88-
**/
89-
virtual bool endWrap( );
90-
91-
/**
92-
* @brief Set the format of the output file
93-
* @param desc: the profile of the output format
94-
*/
95-
virtual void setProfile( const ProfileLoader::Profile& profile );
96-
9743
/**
9844
* @brief Add metadata to the output file.
9945
* @note Depending on the format, you are not sure to find your metadata after the transcode.
10046
*/
101-
virtual void addMetadata( const PropertiesMap& dataMap );
102-
virtual void addMetadata( const std::string& key, const std::string& value );
47+
void addMetadata( const PropertiesMap& dataMap );
48+
void addMetadata( const std::string& key, const std::string& value );
49+
50+
IOutputStream& getStream( const size_t streamId );
51+
FormatContext& getFormatContext() { return _formatContext; }
10352

104-
virtual void setVerbose( bool verbose = false ){ _verbose = verbose; }
53+
/**
54+
* @brief Set the format of the output file
55+
* @param profile: the profile of the output format
56+
*/
57+
void setProfile( const ProfileLoader::Profile& profile );
10558

106-
/// By default get the duration, in seconds, of the first output stream
107-
virtual double getProgressDuration( const size_t streamIndex = 0 );
59+
void setVerbose( bool verbose = false ){ _verbose = verbose; }
10860

10961
private:
11062
FormatContext _formatContext;

src/AvTranscoder/file/file.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#include <AvTranscoder/file/util.hpp>
33
#include <AvTranscoder/file/FormatContext.hpp>
44
#include <AvTranscoder/file/InputFile.hpp>
5+
#include <AvTranscoder/file/IOutputFile.hpp>
56
#include <AvTranscoder/file/OutputFile.hpp>
67
%}
78

89
%include <AvTranscoder/file/util.hpp>
910
%include <AvTranscoder/file/FormatContext.hpp>
1011
%include <AvTranscoder/file/InputFile.hpp>
12+
%include <AvTranscoder/file/IOutputFile.hpp>
1113
%include <AvTranscoder/file/OutputFile.hpp>

src/AvTranscoder/stream/IOutputStream.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AvExport IOutputStream
2525
virtual ~IOutputStream() {};
2626

2727
virtual size_t getStreamIndex() const = 0;
28+
virtual double getStreamDuration() const = 0;
2829

2930
virtual EWrappingStatus wrap( const CodedData& data ) = 0;
3031
};

src/AvTranscoder/stream/OutputStream.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ OutputStream::OutputStream( OutputFile& outputFile, const size_t streamIndex )
1414
{
1515
}
1616

17+
double OutputStream::getStreamDuration() const
18+
{
19+
AVStream& outputStream = _outputFile->getFormatContext().getAVStream( _streamIndex );
20+
return av_q2d( outputStream.time_base ) * outputStream.cur_dts;
21+
}
22+
1723
IOutputStream::EWrappingStatus OutputStream::wrap( const CodedData& data )
1824
{
1925
assert( _outputFile != NULL );

src/AvTranscoder/stream/OutputStream.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AvExport OutputStream : public IOutputStream
1414
OutputStream( OutputFile& outputFile, const size_t streamIndex );
1515

1616
size_t getStreamIndex() const { return _streamIndex; }
17+
double getStreamDuration() const;
1718

1819
IOutputStream::EWrappingStatus wrap( const CodedData& data );
1920

src/AvTranscoder/transcoder/StreamTranscoder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace avtranscoder
2222

2323
StreamTranscoder::StreamTranscoder(
2424
IInputStream& inputStream,
25-
OutputFile& outputFile
25+
IOutputFile& outputFile
2626
)
2727
: _inputStream( &inputStream )
2828
, _outputStream( NULL )
@@ -66,7 +66,7 @@ StreamTranscoder::StreamTranscoder(
6666

6767
StreamTranscoder::StreamTranscoder(
6868
IInputStream& inputStream,
69-
OutputFile& outputFile,
69+
IOutputFile& outputFile,
7070
const ProfileLoader::Profile& profile,
7171
const int subStreamIndex,
7272
const size_t offset
@@ -163,7 +163,7 @@ StreamTranscoder::StreamTranscoder(
163163

164164
StreamTranscoder::StreamTranscoder(
165165
const ICodec& inputCodec,
166-
OutputFile& outputFile,
166+
IOutputFile& outputFile,
167167
const ProfileLoader::Profile& profile
168168
)
169169
: _inputStream( NULL )

0 commit comments

Comments
 (0)