forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutputFile.cpp
More file actions
180 lines (144 loc) · 5.34 KB
/
Copy pathOutputFile.cpp
File metadata and controls
180 lines (144 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "OutputFile.hpp"
#include <AvTranscoder/util.hpp>
#include <stdexcept>
namespace avtranscoder
{
OutputFile::OutputFile( const std::string& filename )
: _formatContext( AV_OPT_FLAG_ENCODING_PARAM )
, _outputStreams()
, _frameCount()
, _filename( filename )
, _previousProcessedStreamDuration( 0.0 )
{
_formatContext.setOutputFormat( _filename );
_formatContext.openRessource( _filename, AVIO_FLAG_WRITE );
}
OutputFile::~OutputFile()
{
for( std::vector< OutputStream* >::iterator it = _outputStreams.begin(); it != _outputStreams.end(); ++it )
{
delete (*it);
}
}
IOutputStream& OutputFile::addVideoStream( const VideoCodec& videoDesc )
{
AVStream& stream = _formatContext.addAVStream( videoDesc.getAVCodec() );
stream.codec->width = videoDesc.getAVCodecContext().width;
stream.codec->height = videoDesc.getAVCodecContext().height;
stream.codec->bit_rate = videoDesc.getAVCodecContext().bit_rate;
stream.codec->pix_fmt = videoDesc.getAVCodecContext().pix_fmt;
stream.codec->profile = videoDesc.getAVCodecContext().profile;
stream.codec->level = videoDesc.getAVCodecContext().level;
// need to set the time_base on the AVCodecContext and the AVStream
// compensating the frame rate with the ticks_per_frame and keeping
// a coherent reading speed.
av_reduce(
&stream.codec->time_base.num,
&stream.codec->time_base.den,
videoDesc.getAVCodecContext().time_base.num * videoDesc.getAVCodecContext().ticks_per_frame,
videoDesc.getAVCodecContext().time_base.den,
INT_MAX );
stream.time_base = stream.codec->time_base;
OutputStream* avOutputStream = new OutputStream( *this, _formatContext.getNbStreams() - 1 );
_outputStreams.push_back( avOutputStream );
return *_outputStreams.back();
}
IOutputStream& OutputFile::addAudioStream( const AudioCodec& audioDesc )
{
AVStream& stream = _formatContext.addAVStream( audioDesc.getAVCodec() );
stream.codec->sample_rate = audioDesc.getAVCodecContext().sample_rate;
stream.codec->channels = audioDesc.getAVCodecContext().channels;
stream.codec->sample_fmt = audioDesc.getAVCodecContext().sample_fmt;
OutputStream* avOutputStream = new OutputStream( *this, _formatContext.getNbStreams() - 1 );
_outputStreams.push_back( avOutputStream );
return *_outputStreams.back();
}
IOutputStream& OutputFile::addDataStream( const DataCodec& dataDesc )
{
_formatContext.addAVStream( dataDesc.getAVCodec() );
OutputStream* avOutputStream = new OutputStream( *this, _formatContext.getNbStreams() - 1 );
_outputStreams.push_back( avOutputStream );
return *_outputStreams.back();
}
IOutputStream& OutputFile::getStream( const size_t streamId )
{
if( streamId >= _outputStreams.size() )
throw std::runtime_error( "unable to get output stream (out of range)" );
return *_outputStreams.at( streamId );
}
bool OutputFile::beginWrap( )
{
_formatContext.writeHeader();
_frameCount.clear();
_frameCount.resize( _outputStreams.size(), 0 );
return true;
}
IOutputStream::EWrappingStatus OutputFile::wrap( const CodedData& data, const size_t streamId )
{
if( ! data.getSize() )
return IOutputStream::eWrappingSuccess;
LOG_DEBUG( "Wrap on stream " << streamId << " (" << data.getSize() << " bytes for frame " << _frameCount.at( streamId ) << ")" )
AVPacket packet;
av_init_packet( &packet );
packet.stream_index = streamId;
packet.data = (uint8_t*)data.getData();
packet.size = data.getSize();
_formatContext.writeFrame( packet );
// free packet.side_data, set packet.data to NULL and packet.size to 0
av_free_packet( &packet );
double currentStreamDuration = _outputStreams.at( streamId )->getStreamDuration();
if( currentStreamDuration < _previousProcessedStreamDuration )
{
// if the current stream is strictly shorter than the previous, wait for more data
return IOutputStream::eWrappingWaitingForData;
}
_previousProcessedStreamDuration = currentStreamDuration;
_frameCount.at( streamId )++;
return IOutputStream::eWrappingSuccess;
}
bool OutputFile::endWrap( )
{
_formatContext.writeTrailer();
_formatContext.closeRessource();
return true;
}
void OutputFile::addMetadata( const PropertiesMap& dataMap )
{
for( PropertiesMap::const_iterator it = dataMap.begin(); it != dataMap.end(); ++it )
{
addMetadata( it->first, it->second );
}
}
void OutputFile::addMetadata( const std::string& key, const std::string& value )
{
_formatContext.addMetaData( key, value );
}
void OutputFile::setProfile( const ProfileLoader::Profile& profile )
{
// check if output format indicated is valid with the filename extension
if( ! matchFormat( profile.find( constants::avProfileFormat )->second, _filename ) )
{
throw std::runtime_error( "Invalid format according to the file extension." );
}
// set output format
_formatContext.setOutputFormat( _filename, profile.find( constants::avProfileFormat )->second );
// set format options
for( ProfileLoader::Profile::const_iterator it = profile.begin(); it != profile.end(); ++it )
{
if( (*it).first == constants::avProfileIdentificator ||
(*it).first == constants::avProfileIdentificatorHuman ||
(*it).first == constants::avProfileType ||
(*it).first == constants::avProfileFormat )
continue;
try
{
Option& formatOption = _formatContext.getOption( (*it).first );
formatOption.setString( (*it).second );
}
catch( std::exception& e )
{
LOG_WARN( "OutputFile - can't set option " << (*it).first << " to " << (*it).second << ": " << e.what() )
}
}
}
}