forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpegVirtualFile.cxx
More file actions
304 lines (259 loc) · 7.23 KB
/
ffmpegVirtualFile.cxx
File metadata and controls
304 lines (259 loc) · 7.23 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
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file ffmpegVirtualFile.cxx
* @author jyelon
* @date 2007-07-02
*/
#include "pandabase.h"
#include "config_ffmpeg.h"
#include "ffmpegVirtualFile.h"
#include "virtualFileSystem.h"
using std::streampos;
using std::streamsize;
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
#ifndef AVSEEK_SIZE
#define AVSEEK_SIZE 0x10000
#endif
/**
*
*/
FfmpegVirtualFile::
FfmpegVirtualFile() :
_io_context(nullptr),
_format_context(nullptr),
_in(nullptr),
_owns_in(false),
_buffer_size(ffmpeg_read_buffer_size)
{
}
/**
*
*/
FfmpegVirtualFile::
~FfmpegVirtualFile() {
close();
}
/**
* Opens the movie file via Panda's VFS. Returns true on success, false on
* failure. If successful, use get_format_context() to get the open file
* handle.
*/
bool FfmpegVirtualFile::
open_vfs(const Filename &filename) {
close();
if (ffmpeg_cat.is_debug()) {
ffmpeg_cat.debug()
<< "ffmpeg open_vfs(" << filename << ")\n";
}
VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
Filename fname = filename;
fname.set_binary();
PT(VirtualFile) vfile = vfs->get_file(fname);
if (vfile == nullptr) {
return false;
}
_in = vfile->open_read_file(true);
if (_in == nullptr) {
return false;
}
_owns_in = true;
_start = 0;
_size = vfile->get_file_size(_in);
// NOTE: The AVIO system owns the buffer after allocation and may realloc it
// internally. Therefore, when we're done with the buffer, we use
// _io_context->buffer to deallocate it rather than holding on to this
// pointer.
unsigned char *buffer = (unsigned char*) av_malloc(_buffer_size);
_io_context = avio_alloc_context(buffer, _buffer_size, 0, (void*) this,
&read_packet, nullptr, &seek);
_format_context = avformat_alloc_context();
_format_context->pb = _io_context;
// Now we can open the stream.
int result =
avformat_open_input(&_format_context, "", nullptr, nullptr);
if (result < 0) {
close();
return false;
}
return true;
}
/**
* Opens the movie file directly from a file on disk (does not go through the
* VFS). Returns true on success, false on failure. If successful, use
* get_format_context() to get the open file handle.
*/
bool FfmpegVirtualFile::
open_subfile(const SubfileInfo &info) {
close();
Filename fname = info.get_filename();
fname.set_binary();
if (!fname.open_read(_file_in)) {
return false;
}
if (ffmpeg_cat.is_debug()) {
ffmpeg_cat.debug()
<< "ffmpeg open_subfile(" << fname << ")\n";
}
_in = &_file_in;
_owns_in = false;
_start = info.get_start();
_size = info.get_size();
_in->seekg(_start);
// NOTE: The AVIO system owns the buffer after allocation and may realloc it
// internally. Therefore, when we're done with the buffer, we use
// _io_context->buffer to deallocate it rather than holding on to this
// pointer.
unsigned char *buffer = (unsigned char*) av_malloc(_buffer_size);
_io_context = avio_alloc_context(buffer, _buffer_size, 0, (void*) this,
&read_packet, nullptr, &seek);
_format_context = avformat_alloc_context();
_format_context->pb = _io_context;
// Now we can open the stream.
int result =
avformat_open_input(&_format_context, fname.c_str(), nullptr, nullptr);
if (result < 0) {
close();
return false;
}
return true;
}
/**
* Explicitly closes the opened file. This is also called implicitly by the
* destructor if necessary.
*/
void FfmpegVirtualFile::
close() {
if (_format_context != nullptr) {
avformat_close_input(&_format_context);
}
if (_io_context != nullptr) {
if (_io_context->buffer != nullptr) {
av_free(_io_context->buffer);
}
av_free(_io_context);
_io_context = nullptr;
}
if (_owns_in) {
nassertv(_in != nullptr);
VirtualFileSystem::close_read_file(_in);
_owns_in = false;
}
_in = nullptr;
}
/**
* Should be called at startup to attach the appropriate hooks between Panda
* and FFMpeg.
*/
void FfmpegVirtualFile::
register_protocol() {
static bool initialized = false;
if (initialized) {
return;
}
// Here's a good place to call this global ffmpeg initialization function.
// However, ffmpeg (but not libav) deprecated this, hence this check.
#if LIBAVFORMAT_VERSION_MICRO < 100 || LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
av_register_all();
#endif
// And this one.
avformat_network_init();
// Let's also register the logging to Panda's notify callback.
av_log_set_callback(&log_callback);
}
/**
* A callback to read a virtual file.
*/
int FfmpegVirtualFile::
read_packet(void *opaque, uint8_t *buf, int size) {
streampos ssize = (streampos)size;
FfmpegVirtualFile *self = (FfmpegVirtualFile *) opaque;
std::istream *in = self->_in;
// Since we may be simulating a subset of the opened stream, don't allow it
// to read past the "end".
streampos remaining = self->_start + (streampos)self->_size - in->tellg();
if (remaining < ssize) {
if (remaining <= 0) {
return AVERROR_EOF;
}
ssize = remaining;
}
in->read((char *)buf, ssize);
streamsize gc = in->gcount();
in->clear();
return (int)gc;
}
/**
* A callback to change the read position on an istream.
*/
int64_t FfmpegVirtualFile::
seek(void *opaque, int64_t pos, int whence) {
FfmpegVirtualFile *self = (FfmpegVirtualFile *) opaque;
std::istream *in = self->_in;
switch (whence) {
case SEEK_SET:
in->seekg(self->_start + (streampos)pos, std::ios::beg);
break;
case SEEK_CUR:
in->seekg(pos, std::ios::cur);
break;
case SEEK_END:
// For seeks relative to the end, we actually compute the end based on
// _start + _size, and then use ios::beg.
in->seekg(self->_start + (streampos)self->_size + (streampos)pos, std::ios::beg);
break;
case AVSEEK_SIZE:
return self->_size;
default:
ffmpeg_cat.error()
<< "Illegal parameter to seek in FfmpegVirtualFile\n";
in->clear();
return -1;
}
in->clear();
return in->tellg() - self->_start;
}
/**
* These callbacks are made when ffmpeg wants to write a log entry; it
* redirects into Panda's notify.
*/
void FfmpegVirtualFile::
log_callback(void *ptr, int level, const char *fmt, va_list v1) {
NotifySeverity severity;
#ifdef AV_LOG_PANIC
if (level <= AV_LOG_PANIC) {
severity = NS_fatal;
} else
#endif
if (level <= AV_LOG_ERROR) {
severity = NS_error;
#ifdef AV_LOG_WARNING
} else if (level <= AV_LOG_WARNING) {
severity = NS_warning;
#endif
} else if (level <= AV_LOG_INFO) {
severity = NS_info;
#ifdef AV_LOG_VERBOSE
} else if (level <= AV_LOG_VERBOSE) {
severity = NS_debug;
#endif
} else /* level <= AV_LOG_DEBUG */ {
severity = NS_spam;
}
if (ffmpeg_cat.is_on(severity)) {
static const size_t buffer_size = 4096;
char *buffer = (char *)alloca(buffer_size);
vsnprintf(buffer, buffer_size, fmt, v1);
nassertv(strlen(buffer) < buffer_size);
ffmpeg_cat.out(severity, true)
<< buffer;
}
}