forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpegVideoCursor.h
More file actions
214 lines (178 loc) · 5.26 KB
/
ffmpegVideoCursor.h
File metadata and controls
214 lines (178 loc) · 5.26 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
/**
* 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 ffmpegVideoCursor.h
* @author jyelon
* @date 2007-08-01
*/
#ifndef FFMPEGVIDEOCURSOR_H
#define FFMPEGVIDEOCURSOR_H
#include "pandabase.h"
#include "movieVideoCursor.h"
#include "texture.h"
#include "pointerTo.h"
#include "ffmpegVirtualFile.h"
#include "genericThread.h"
#include "threadPriority.h"
#include "pmutex.h"
#include "reMutex.h"
#include "conditionVar.h"
#include "pdeque.h"
class FfmpegVideo;
struct AVFormatContext;
struct AVCodecContext;
struct AVStream;
struct AVPacket;
struct AVFrame;
struct SwsContext;
/**
*
*/
class EXPCL_FFMPEG FfmpegVideoCursor : public MovieVideoCursor {
private:
FfmpegVideoCursor();
void init_from(FfmpegVideo *src);
PUBLISHED:
FfmpegVideoCursor(FfmpegVideo *src);
virtual ~FfmpegVideoCursor();
void set_max_readahead_frames(int max_readahead_frames);
int get_max_readahead_frames() const;
void set_thread_priority(ThreadPriority thread_priority);
ThreadPriority get_thread_priority() const;
void start_thread();
BLOCKING void stop_thread();
bool is_thread_started() const;
public:
virtual bool set_time(double timestamp, int loop_count);
virtual PT(Buffer) fetch_buffer();
public:
// Nested class must be public for PT(FfmpegBuffer) to work correctly.
class EXPCL_FFMPEG FfmpegBuffer : public Buffer {
public:
ALLOC_DELETED_CHAIN(FfmpegBuffer);
INLINE FfmpegBuffer(size_t block_size, double video_timebase);
virtual int compare_timestamp(const Buffer *other) const;
virtual double get_timestamp() const;
int _begin_frame;
int _end_frame;
double _video_timebase;
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
Buffer::init_type();
register_type(_type_handle, "FfmpegVideoCursor::FfmpegBuffer",
Buffer::get_class_type());
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
};
protected:
virtual PT(Buffer) make_new_buffer();
private:
bool open_stream();
void close_stream();
void cleanup();
Filename _filename;
std::string _sync_name;
int _max_readahead_frames;
ThreadPriority _thread_priority;
PT(GenericThread) _thread;
int _pixel_format;
// This global Mutex protects calls to avcodec_opencloseetc.
static ReMutex _av_lock;
// Protects _readahead_frames and all the immediately following members.
Mutex _lock;
// Condition: the thread has something to do.
ConditionVar _action_cvar;
typedef pdeque<PT(FfmpegBuffer) > Buffers;
Buffers _readahead_frames;
enum ThreadStatus {
TS_stopped,
TS_wait,
TS_readahead,
TS_seek,
TS_seeking,
TS_shutdown,
};
ThreadStatus _thread_status;
int _seek_frame;
int _current_frame;
PT(FfmpegBuffer) _current_frame_buffer;
private:
// The following functions will be called in the sub-thread.
static void st_thread_main(void *self);
void thread_main();
bool do_poll();
PT(FfmpegBuffer) do_alloc_frame();
void do_clear_all_frames();
bool fetch_packet(int default_frame);
bool do_fetch_packet(int default_frame);
void fetch_frame(int frame);
void decode_frame(int &finished);
void do_decode_frame(int &finished);
void seek(int frame, bool backward);
void do_seek(int frame, bool backward);
int binary_seek(int min_frame, int max_frame, int target_frame, int num_iterations);
void advance_to_frame(int frame);
void reset_stream();
void export_frame(FfmpegBuffer *buffer);
// The following data members will be accessed by the sub-thread.
AVPacket *_packet;
int _packet_frame;
AVFormatContext *_format_ctx;
AVCodecContext *_video_ctx;
SwsContext *_convert_ctx;
FfmpegVirtualFile _ffvfile;
int _video_index;
double _video_timebase;
AVFrame *_frame;
AVFrame *_frame_out;
int _initial_dts;
double _min_fseek;
int _begin_frame;
int _end_frame;
bool _frame_ready;
bool _eof_known;
int _eof_frame;
// PStat collectors.
static PStatCollector _fetch_buffer_pcollector;
static PStatCollector _seek_pcollector;
static PStatCollector _export_frame_pcollector;
public:
static void register_with_read_factory();
virtual void write_datagram(BamWriter *manager, Datagram &dg);
virtual void finalize(BamReader *manager);
protected:
static TypedWritable *make_from_bam(const FactoryParams ¶ms);
void fillin(DatagramIterator &scan, BamReader *manager);
public:
static TypeHandle get_class_type() {
return _type_handle;
}
static void init_type() {
MovieVideoCursor::init_type();
register_type(_type_handle, "FfmpegVideoCursor",
MovieVideoCursor::get_class_type());
FfmpegBuffer::init_type();
}
virtual TypeHandle get_type() const {
return get_class_type();
}
virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
private:
static TypeHandle _type_handle;
friend class FfmpegVideo;
};
#include "ffmpegVideoCursor.I"
#endif // FFMPEGVIDEO_H