-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathFormatContext.cpp
More file actions
211 lines (187 loc) · 5.9 KB
/
Copy pathFormatContext.cpp
File metadata and controls
211 lines (187 loc) · 5.9 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
#include "FormatContext.hpp"
#include <stdexcept>
#include <sstream>
namespace avtranscoder
{
FormatContext::FormatContext(const std::string& filename, int req_flags, AVDictionary** options)
: _avFormatContext(NULL)
, _avStreamAllocated()
, _flags(req_flags)
, _options()
, _isOpen(false)
{
const int ret = avformat_open_input(&_avFormatContext, filename.c_str(), NULL, options);
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);
// when demuxing, priv_data of AVFormatContext is set by avformat_open_input()
if(_avFormatContext->iformat->priv_class)
loadOptions(_options, _avFormatContext->priv_data, req_flags);
}
FormatContext::FormatContext(int req_flags)
: _avFormatContext(NULL)
, _avStreamAllocated()
, _flags(req_flags)
, _options()
, _isOpen(false)
{
_avFormatContext = avformat_alloc_context();
loadOptions(_options, _avFormatContext, req_flags);
}
FormatContext::~FormatContext()
{
if(!_avFormatContext)
return;
// free the format context
if(_isOpen)
avformat_close_input(&_avFormatContext);
avformat_free_context(_avFormatContext);
_avFormatContext = NULL;
}
void FormatContext::findStreamInfo(AVDictionary** options)
{
const 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;
const 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;
const 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)
{
const int ret = avformat_write_header(_avFormatContext, options);
if(ret != 0)
{
throw std::runtime_error("Could not write header: " + getDescriptionFromErrorCode(ret));
}
// when muxing, priv_data of AVFormatContext is set by avformat_write_header()
if(_avFormatContext->oformat->priv_class)
loadOptions(_options, _avFormatContext->priv_data, _flags);
}
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()
{
const 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)
{
const 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");
}
_avStreamAllocated.push_back(stream);
return *stream;
}
bool FormatContext::seek(const uint64_t position, const int flag)
{
LOG_INFO("Seek in '" << _avFormatContext->url << "' at " << position << " with flag '" << flag << "'")
const int err = av_seek_frame(_avFormatContext, -1, position, flag);
if(err < 0)
{
LOG_ERROR("Error when seek at " << position << " (in AV_TIME_BASE units) in file")
LOG_ERROR(getDescriptionFromErrorCode(err))
return false;
}
return true;
}
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::setFilename(const std::string& filename)
{
_avFormatContext->url = (char*)av_malloc(filename.size());
strcpy(_avFormatContext->url, filename.c_str());
}
void FormatContext::setOutputFormat(const std::string& filename, const std::string& shortName, const std::string& mimeType)
{
const 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 += ", formatName = ";
msg += shortName;
}
if(!mimeType.empty())
{
msg += ", mimeType = ";
msg += mimeType;
}
throw std::ios_base::failure(msg);
}
_avFormatContext->oformat = oformat;
}
}