-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathDemuxer.cpp
More file actions
305 lines (257 loc) · 7.29 KB
/
Demuxer.cpp
File metadata and controls
305 lines (257 loc) · 7.29 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "Demuxer.h"
#include "FFmpegException.h"
#include "CodecDeducer.h"
#include <string>
using namespace std;
namespace ffmpegcpp
{
Demuxer::Demuxer(const char* fileName)
: Demuxer(fileName, NULL, NULL)
{
}
Demuxer::Demuxer(const char* fileName, AVInputFormat* inputFormat, AVDictionary *format_opts)
{
this->fileName = fileName;
// open input file, and allocate format context
int ret;
if ((ret = avformat_open_input(&containerContext, fileName, inputFormat, &format_opts)) < 0)
{
CleanUp();
throw FFmpegException("Failed to open input container " + string(fileName), ret);
}
// retrieve stream information
if (ret = (avformat_find_stream_info(containerContext, NULL)) < 0)
{
CleanUp();
throw FFmpegException("Failed to read streams from " + string(fileName), ret);
}
inputStreams = new InputStream*[containerContext->nb_streams];
for (int i = 0; i < containerContext->nb_streams; ++i)
{
inputStreams[i] = nullptr;
}
// initialize packet, set data to NULL, let the demuxer fill it
pkt = av_packet_alloc();
if (!pkt)
{
CleanUp();
throw FFmpegException("Failed to create packet for input stream");
}
av_init_packet(pkt);
pkt->data = NULL;
pkt->size = 0;
}
Demuxer::~Demuxer()
{
CleanUp();
}
void Demuxer::CleanUp()
{
if (inputStreams != nullptr)
{
for (int i = 0; i < containerContext->nb_streams; ++i)
{
if (inputStreams[i] != nullptr)
{
delete inputStreams[i];
}
}
delete inputStreams;
inputStreams = nullptr;
}
if (containerContext != nullptr)
{
avformat_close_input(&containerContext);
containerContext = nullptr;
}
if (pkt != nullptr)
{
av_packet_free(&pkt);
pkt = nullptr;
}
}
void Demuxer::DecodeBestAudioStream(FrameSink* frameSink)
{
int ret = av_find_best_stream(containerContext, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (ret < 0)
{
throw FFmpegException("Could not find " + string(av_get_media_type_string(AVMEDIA_TYPE_AUDIO)) + " stream in input file " + fileName, ret);
}
int streamIndex = ret;
return DecodeAudioStream(streamIndex, frameSink);
}
void Demuxer::DecodeBestVideoStream(FrameSink* frameSink)
{
int ret = av_find_best_stream(containerContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (ret < 0)
{
throw FFmpegException("Could not find " + string(av_get_media_type_string(AVMEDIA_TYPE_VIDEO)) + " stream in input file " + fileName, ret);
}
int streamIndex = ret;
return DecodeVideoStream(streamIndex, frameSink);
}
void Demuxer::DecodeAudioStream(int streamIndex, FrameSink* frameSink)
{
// each input stream can only be used once
if (inputStreams[streamIndex] != nullptr)
{
throw FFmpegException("That stream is already tied to a frame sink, you cannot process the same stream multiple times");
}
// create the stream
InputStream* inputStream = GetInputStream(streamIndex);
inputStream->Open(frameSink);
// remember and return
inputStreams[streamIndex] = inputStream;
}
void Demuxer::DecodeVideoStream(int streamIndex, FrameSink* frameSink)
{
// each input stream can only be used once
if (inputStreams[streamIndex] != nullptr)
{
throw FFmpegException("That stream is already tied to a frame sink, you cannot process the same stream multiple times");
}
// create the stream
InputStream* inputStream = GetInputStream(streamIndex);
inputStream->Open(frameSink);
// remember and return
inputStreams[streamIndex] = inputStream;
}
InputStream* Demuxer::GetInputStream(int streamIndex)
{
// already exists
if (inputStreams[streamIndex] != nullptr) return inputStreams[streamIndex];
// The stream doesn't exist but we already processed all our frames, so it makes no sense
// to add it anymore.
if (IsDone()) return nullptr;
AVStream* stream = containerContext->streams[streamIndex];
AVCodec* codec = CodecDeducer::DeduceDecoder(stream->codecpar->codec_id);
if (codec == nullptr) return nullptr; // no codec found - we can't really do anything with this stream!
switch (codec->type)
{
case AVMEDIA_TYPE_VIDEO:
inputStreams[streamIndex] = new VideoInputStream(containerContext, stream);
break;
case AVMEDIA_TYPE_AUDIO:
inputStreams[streamIndex] = new AudioInputStream(containerContext, stream);
break;
}
// return the created stream
return inputStreams[streamIndex];
}
InputStream* Demuxer::GetInputStreamById(int streamId)
{
// map the stream id to an index by going over all the streams and comparing the id
for (int i = 0; i < containerContext->nb_streams; ++i)
{
AVStream* stream = containerContext->streams[i];
if (stream->id == streamId) return GetInputStream(i);
}
// no match found
return nullptr;
}
void Demuxer::PreparePipeline()
{
bool allPrimed = false;
do
{
Step();
// see if all input streams are primed
allPrimed = true;
for (int i = 0; i < containerContext->nb_streams; ++i)
{
InputStream* stream = inputStreams[i];
if (stream != nullptr)
{
if (!stream->IsPrimed()) allPrimed = false;
}
}
} while (!allPrimed && !IsDone());
}
bool Demuxer::IsDone()
{
return done;
}
void Demuxer::Step()
{
// read frames from the file
int ret = av_read_frame(containerContext, pkt);
// EOF
if (ret == AVERROR_EOF)
{
pkt->data = NULL;
pkt->size = 0;
for (int i = 0; i < containerContext->nb_streams; ++i)
{
InputStream* stream = inputStreams[i];
if (stream != nullptr)
{
pkt->stream_index = i;
DecodePacket();
stream->Close();
}
}
done = true;
return;
}
// not ready yet
if (ret == AVERROR(EAGAIN)) return;
// error
if (ret < 0)
{
throw FFmpegException("Error during demuxing", ret);
}
// decode the finished packet
DecodePacket();
}
void Demuxer::DecodePacket()
{
int streamIndex = pkt->stream_index;
InputStream* inputStream = inputStreams[streamIndex];
if (inputStream != nullptr)
{
inputStream->DecodePacket(pkt);
}
// We need to unref the packet here because packets might pass by here
// that don't have a stream attached to them. We want to dismiss them!
av_packet_unref(pkt);
}
ContainerInfo Demuxer::GetInfo()
{
ContainerInfo info;
// general data
// the duration is calculated like this... why?
int64_t duration = containerContext->duration + (containerContext->duration <= INT64_MAX - 5000 ? 5000 : 0);
info.durationInMicroSeconds = duration;
info.durationInSeconds = (float)info.durationInMicroSeconds / AV_TIME_BASE;
info.start = (float)containerContext->start_time / AV_TIME_BASE;
info.bitRate = containerContext->bit_rate;
info.format = containerContext->iformat;
// go over all streams and get their info
for (int i = 0; i < containerContext->nb_streams; ++i)
{
InputStream* stream = GetInputStream(i);
if (stream == nullptr) continue; // no valid stream
stream->AddStreamInfo(&info);
}
return info;
}
int Demuxer::GetFrameCount(int streamId)
{
// Make sure all streams exist, so we can query them later.
for (int i = 0; i < containerContext->nb_streams; ++i)
{
GetInputStream(i);
}
// Process the entire container so we can know how many frames are in each
while (!IsDone())
{
Step();
}
// Return the right stream's frame count.
return GetInputStreamById(streamId)->GetFramesProcessed();
}
const char* Demuxer::GetFileName()
{
return fileName;
}
}