forked from mikrosimage/avTranscoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatContext.cpp
More file actions
181 lines (159 loc) · 4.32 KB
/
Copy pathFormatContext.cpp
File metadata and controls
181 lines (159 loc) · 4.32 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
#include "FormatContext.hpp"
#include <stdexcept>
#include <sstream>
namespace avtranscoder
{
FormatContext::FormatContext( const std::string& filename, int req_flags )
: _avFormatContext( NULL )
, _options()
, _isOpen( false )
{
int ret = avformat_open_input( &_avFormatContext, filename.c_str(), NULL, NULL );
if( ret < 0 )
{
std::string msg = "unable to open file ";
msg += filename;
msg += ": ";
msg += getDescriptionFromErrorCode( ret );
throw std::ios_base::failure( msg );
}
_isOpen = true;
loadOptions( _options, _avFormatContext, req_flags );
}
FormatContext::FormatContext( int req_flags )
: _avFormatContext( NULL )
, _options()
, _isOpen( false )
{
_avFormatContext = avformat_alloc_context();
loadOptions( _options, _avFormatContext, req_flags );
}
FormatContext::~FormatContext()
{
if( ! _avFormatContext )
return;
if( _isOpen )
avformat_close_input( &_avFormatContext );
else
avformat_free_context( _avFormatContext );
_avFormatContext = NULL;
}
void FormatContext::findStreamInfo( AVDictionary** options )
{
int err = avformat_find_stream_info( _avFormatContext, options );
if( err < 0 )
{
throw std::ios_base::failure( "unable to find stream informations: " + getDescriptionFromErrorCode( err ) );
}
}
void FormatContext::openRessource( const std::string& url, int flags )
{
if( ( _avFormatContext->flags & AVFMT_NOFILE ) == AVFMT_NOFILE )
return;
int err = avio_open2( &_avFormatContext->pb, url.c_str(), flags, NULL, NULL );
if( err < 0 )
{
throw std::ios_base::failure( "error when opening output format: " + getDescriptionFromErrorCode( err ) );
}
}
void FormatContext::closeRessource()
{
if( ( _avFormatContext->flags & AVFMT_NOFILE ) == AVFMT_NOFILE )
return;
int err = avio_close( _avFormatContext->pb );
if( err < 0 )
{
throw std::ios_base::failure( "error when close output format: " + getDescriptionFromErrorCode( err ) );
}
}
void FormatContext::writeHeader( AVDictionary** options )
{
int ret = avformat_write_header( _avFormatContext, options );
if( ret != 0 )
{
throw std::runtime_error( "could not write header: " + getDescriptionFromErrorCode( ret ) );
}
}
void FormatContext::writeFrame( AVPacket& packet, bool interleaved )
{
int ret = 0;
if( interleaved )
ret = av_interleaved_write_frame( _avFormatContext, &packet );
else
{
// returns 1 if flushed and there is no more data to flush
ret = av_write_frame( _avFormatContext, &packet );
}
if( ret < 0 )
{
throw std::runtime_error( "error when writting packet in stream: " + getDescriptionFromErrorCode( ret ) );
}
}
void FormatContext::writeTrailer()
{
int ret = av_write_trailer( _avFormatContext );
if( ret != 0 )
{
throw std::runtime_error( "could not write trailer: " + getDescriptionFromErrorCode( ret ) );
}
}
void FormatContext::addMetaData( const std::string& key, const std::string& value )
{
int ret = av_dict_set( &_avFormatContext->metadata, key.c_str(), value.c_str(), 0 );
if( ret < 0 )
{
LOG_ERROR( getDescriptionFromErrorCode( ret ) )
}
}
AVStream& FormatContext::addAVStream( const AVCodec& avCodec )
{
// Need const_cast<AVCodec*> for libav versions < 54.34.
AVStream* stream = avformat_new_stream( _avFormatContext, const_cast<AVCodec*>(&avCodec) );
if( stream == NULL )
{
throw std::runtime_error( "unable to add new video stream" );
}
return *stream;
}
std::vector<Option> FormatContext::getOptions()
{
std::vector<Option> optionsArray;
for( OptionMap::iterator it = _options.begin(); it != _options.end(); ++it )
{
optionsArray.push_back( it->second );
}
return optionsArray;
}
AVStream& FormatContext::getAVStream( size_t index ) const
{
if( index >= getNbStreams() )
{
std::stringstream msg;
msg << "Can't acces stream at index ";
msg << index;
throw std::runtime_error( msg.str() );
}
return *_avFormatContext->streams[index];
}
void FormatContext::setOutputFormat( const std::string& filename, const std::string& shortName, const std::string& mimeType )
{
AVOutputFormat* oformat = av_guess_format( shortName.c_str(), filename.c_str(), mimeType.c_str() );
if( ! oformat )
{
std::string msg( "unable to find format for " );
msg += filename;
if( ! shortName.empty() )
{
msg += ", ";
msg += shortName;
}
if( ! mimeType.empty() )
{
msg += ", ";
msg += mimeType;
}
throw std::ios_base::failure( msg );
}
_avFormatContext->oformat = oformat;
}
}