-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathInputFile.cpp
More file actions
186 lines (162 loc) · 4.67 KB
/
Copy pathInputFile.cpp
File metadata and controls
186 lines (162 loc) · 4.67 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
#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>
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();
// 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.extractStreamProperties( progress, level );
}
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, const int flag )
{
uint64_t position = frame / getFps() * AV_TIME_BASE;
_formatContext.seek( position, flag );
}
void InputFile::seekAtTime( const double time, const int flag )
{
uint64_t position = time * AV_TIME_BASE;
_formatContext.seek( position, flag );
}
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() );
}
}
std::string InputFile::getFormatName() const
{
if( _formatContext.getAVInputFormat().name == NULL )
{
LOG_WARN("Unknown demuxer format name of '" << _filename << "'.")
return "";
}
return std::string(_formatContext.getAVInputFormat().name);
}
std::string InputFile::getFormatLongName() const
{
if( _formatContext.getAVInputFormat().long_name == NULL )
{
LOG_WARN("Unknown demuxer format long name of '" << _filename << "'.")
return "";
}
return std::string(_formatContext.getAVInputFormat().long_name);
}
std::string InputFile::getFormatMimeType() const
{
#if LIBAVFORMAT_VERSION_MAJOR <= 55
LOG_WARN("Cannot get mime type format of '" << _filename << "' because your libavformat library has a major version <= 55.")
return "not available";
#else
if( _formatContext.getAVInputFormat().mime_type == NULL )
{
LOG_WARN("Unknown demuxer format mime type of '" << _filename << "'.")
return "";
}
return std::string(_formatContext.getAVInputFormat().mime_type);
#endif
}
double InputFile::getFps()
{
double fps = 1;
if( _properties.getNbVideoStreams() )
fps = _properties.getVideoProperties().at( 0 ).getFps();
return fps;
}
void InputFile::setProfile( const ProfileLoader::Profile& profile )
{
LOG_DEBUG( "Set profile of input file with:\n" << 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() )
}
}
}
}