forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputFile.cpp
More file actions
222 lines (196 loc) · 5.56 KB
/
Copy pathInputFile.cpp
File metadata and controls
222 lines (196 loc) · 5.56 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "InputFile.hpp"
#include <AvTranscoder/mediaProperty/util.hpp>
#include <AvTranscoder/mediaProperty/VideoProperties.hpp>
#include <AvTranscoder/mediaProperty/AudioProperties.hpp>
#include <AvTranscoder/mediaProperty/DataProperties.hpp>
#include <AvTranscoder/mediaProperty/SubtitleProperties.hpp>
#include <AvTranscoder/mediaProperty/AttachementProperties.hpp>
#include <AvTranscoder/mediaProperty/UnknownProperties.hpp>
#include <AvTranscoder/progress/NoDisplayProgress.hpp>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavutil/avutil.h>
#include <libavutil/pixdesc.h>
}
#include <stdexcept>
#include <sstream>
namespace avtranscoder
{
InputFile::InputFile( const std::string& filename )
: _formatContext( filename, AV_OPT_FLAG_DECODING_PARAM )
, _properties( _formatContext )
, _filename( filename )
, _inputStreams()
{
_formatContext.findStreamInfo();
// Analyse header
NoDisplayProgress p;
analyse( p, eAnalyseLevelHeader );
// Create streams
for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); ++streamIndex )
{
_inputStreams.push_back( new InputStream( *this, streamIndex ) );
}
}
InputFile::~InputFile()
{
for( std::vector< InputStream* >::iterator it = _inputStreams.begin(); it != _inputStreams.end(); ++it )
{
delete (*it);
}
}
void InputFile::analyse( IProgress& progress, const EAnalyseLevel level )
{
_properties.clearStreamProperties();
if( level > eAnalyseLevelHeader )
seekAtFrame( 0 );
for( size_t streamIndex = 0; streamIndex < _formatContext.getNbStreams(); streamIndex++ )
{
switch( _formatContext.getAVStream( streamIndex ).codec->codec_type )
{
case AVMEDIA_TYPE_VIDEO:
{
VideoProperties properties( _formatContext, streamIndex, progress, level );
_properties.getVideoProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_AUDIO:
{
AudioProperties properties( _formatContext, streamIndex );
_properties.getAudioProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_DATA:
{
DataProperties properties( _formatContext, streamIndex );
_properties.getDataProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_SUBTITLE:
{
SubtitleProperties properties( _formatContext, streamIndex );
_properties.getSubtitleProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_ATTACHMENT:
{
AttachementProperties properties( _formatContext, streamIndex );
_properties.getAttachementProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_UNKNOWN:
{
UnknownProperties properties( _formatContext, streamIndex );
_properties.getUnknownPropertiesProperties().push_back( properties );
break;
}
case AVMEDIA_TYPE_NB:
{
break;
}
}
}
if( level > eAnalyseLevelHeader )
seekAtFrame( 0 );
}
FileProperties InputFile::analyseFile( const std::string& filename, IProgress& progress, const EAnalyseLevel level )
{
InputFile file( filename );
file.analyse( progress, level );
return file.getProperties();
}
bool InputFile::readNextPacket( CodedData& data, const size_t streamIndex )
{
bool nextPacketFound = false;
while( ! nextPacketFound )
{
int ret = av_read_frame( &_formatContext.getAVFormatContext(), &data.getAVPacket() );
if( ret < 0 ) // error or end of file
{
return false;
}
// if the packet stream is the expected one
// return the packet data
int packetStreamIndex = data.getAVPacket().stream_index;
if( packetStreamIndex == (int)streamIndex )
{
nextPacketFound = true;
}
// else add the packet data to the stream cache
else
{
_inputStreams.at( packetStreamIndex )->addPacket( data.getAVPacket() );
data.clear();
}
}
return true;
}
void InputFile::seekAtFrame( const size_t frame )
{
uint64_t position = frame / getFps() * AV_TIME_BASE;
seek( position );
}
void InputFile::seekAtTime( const double time )
{
uint64_t position = time * AV_TIME_BASE;
seek( position );
}
void InputFile::seek( uint64_t position )
{
if( (int)_formatContext.getStartTime() != AV_NOPTS_VALUE )
position += _formatContext.getStartTime();
if( av_seek_frame( &_formatContext.getAVFormatContext(), -1, position, AVSEEK_FLAG_BACKWARD ) < 0 )
{
LOG_ERROR( "Error when seek at " << position << " (in AV_TIME_BASE units) in file" )
}
for( std::vector<InputStream*>::iterator it = _inputStreams.begin(); it != _inputStreams.end(); ++it )
{
(*it)->clearBuffering();
}
}
void InputFile::activateStream( const size_t streamIndex, bool activate )
{
getStream( streamIndex ).activate( activate );
}
InputStream& InputFile::getStream( size_t index )
{
try
{
return *_inputStreams.at( index );
}
catch( const std::out_of_range& e )
{
std::stringstream msg;
msg << getFilename();
msg << " has no stream at index ";
msg << index;
throw std::runtime_error( msg.str() );
}
}
double InputFile::getFps()
{
double fps = 1;
if( _properties.getNbVideoStreams() )
fps = _properties.getVideoProperties().at( 0 ).getFps();
return fps;
}
void InputFile::setProfile( const ProfileLoader::Profile& profile )
{
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 )
continue;
try
{
Option& formatOption = _formatContext.getOption( (*it).first );
formatOption.setString( (*it).second );
}
catch( std::exception& e )
{
LOG_WARN( "InputFile - can't set option " << (*it).first << " to " << (*it).second << ": " << e.what() )
}
}
}
}