-
Notifications
You must be signed in to change notification settings - Fork 279
/
video.c
353 lines (299 loc) · 8.14 KB
/
video.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* This is just a quick hack. But good enough for our use of displaying
* a short intro video when our game starts up - so might as well share
* it.
*
* Known bugs:
*
* - Only very crude synching. Audio is slightly delayed and some
* videos seem to constantly drift off and then the audio gets all
* distorted...
*
* - Seeking/Pausing doesn't really work.
*
* - Memory leaks. Easy to fix but don't have time right now.
*
* Missing features:
*
* - Stream information. For example allow selection of one of several
* audio streams or subtitle overlay streams.
*
* - Non audio/video streams. For example something like:
* ALLEGRO_USTR *al_get_video_subtitle(float *x, float *y);
*
* - Buffering. Right now buffering is hardcoded to a fixed size which
* seemed enough for streaming 720p from disk in my tests. Obviously
* when streaming higher bandwidth or from a source with high
* fluctuation like an internet stream this won't work at all.
*
* - Provide an audio stream for the audio. Then could use this to
* stream audio files. Right now opening an .mp3 with the video
* addon will play it but only with the video API instead of Allegro's
* normal audio streaming API...
*
* - Audio/Video sync. For a game user-controlled sync is probably not
* too important as it can just ship with a properly synchronizeded
* video. However right now the audio delay is completely ignored.
*
* - Additional drivers. Also redo the API a bit so not everything
* has to be done by the driver.
*/
#include "allegro5/allegro5.h"
#include "allegro5/allegro_video.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_video.h"
#include "allegro5/internal/aintern_video_cfg.h"
#include "allegro5/internal/aintern_exitfunc.h"
#include "allegro5/internal/aintern_vector.h"
ALLEGRO_DEBUG_CHANNEL("video")
/* globals */
static bool video_inited = false;
typedef struct VideoHandler {
const char *extension;
ALLEGRO_VIDEO_INTERFACE *vtable;
ALLEGRO_VIDEO_IDENTIFIER_FUNCTION identifier;
} VideoHandler;
static _AL_VECTOR handlers = _AL_VECTOR_INITIALIZER(VideoHandler);
static const char* identify_video(ALLEGRO_FILE *f)
{
size_t i;
for (i = 0; i < _al_vector_size(&handlers); i++) {
VideoHandler *l = _al_vector_ref(&handlers, i);
if (l->identifier(f)) {
return l->extension;
}
}
return NULL;
}
static ALLEGRO_VIDEO_INTERFACE *find_handler(const char *extension)
{
size_t i;
for (i = 0; i < _al_vector_size(&handlers); i++) {
VideoHandler *l = _al_vector_ref(&handlers, i);
if (0 == _al_stricmp(extension, l->extension)) {
return l->vtable;
}
}
return NULL;
}
static void add_handler(const char *extension, ALLEGRO_VIDEO_INTERFACE *vtable,
ALLEGRO_VIDEO_IDENTIFIER_FUNCTION identifier)
{
VideoHandler *v = _al_vector_alloc_back(&handlers);
v->extension = extension;
v->vtable = vtable;
v->identifier = identifier;
}
/* Function: al_open_video
*/
ALLEGRO_VIDEO *al_open_video(char const *filename)
{
ALLEGRO_VIDEO *video;
const char *ext;
video = al_calloc(1, sizeof *video);
ASSERT(filename);
ext = al_identify_video(filename);
if (!ext) {
ext = strrchr(filename, '.');
if (!ext) {
ALLEGRO_ERROR("Could not identify video %s!\n", filename);
}
}
video->vtable = find_handler(ext);
if (video->vtable == NULL) {
ALLEGRO_ERROR("No handler for video extension %s - "
"therefore not trying to load %s.\n", ext, filename);
al_free(video);
return NULL;
}
video->filename = al_create_path(filename);
video->playing = true;
if (!video->vtable->open_video(video)) {
ALLEGRO_ERROR("Could not open %s.\n", filename);
al_destroy_path(video->filename);
al_free(video);
return NULL;
}
al_init_user_event_source(&video->es);
video->es_inited = true;
return video;
}
/* Function: al_close_video
*/
void al_close_video(ALLEGRO_VIDEO *video)
{
if (video) {
video->vtable->close_video(video);
if (video->es_inited) {
al_destroy_user_event_source(&video->es);
}
al_destroy_path(video->filename);
al_free(video);
}
}
/* Function: al_get_video_event_source
*/
ALLEGRO_EVENT_SOURCE *al_get_video_event_source(ALLEGRO_VIDEO *video)
{
ASSERT(video);
return &video->es;
}
/* Function: al_start_video
*/
void al_start_video(ALLEGRO_VIDEO *video, ALLEGRO_MIXER *mixer)
{
ASSERT(video);
/* XXX why is this not just a parameter? */
video->mixer = mixer;
video->vtable->start_video(video);
}
/* Function: al_start_video_with_voice
*/
void al_start_video_with_voice(ALLEGRO_VIDEO *video, ALLEGRO_VOICE *voice)
{
ASSERT(video);
/* XXX why is voice not just a parameter? */
video->voice = voice;
video->vtable->start_video(video);
}
/* Function: al_set_video_playing
*/
void al_set_video_playing(ALLEGRO_VIDEO *video, bool play)
{
ASSERT(video);
if (play != video->playing) {
video->playing = play;
video->vtable->set_video_playing(video);
}
}
/* Function: al_is_video_playing
*/
bool al_is_video_playing(ALLEGRO_VIDEO *video)
{
ASSERT(video);
return video->playing;
}
/* Function: al_get_video_frame
*/
ALLEGRO_BITMAP *al_get_video_frame(ALLEGRO_VIDEO *video)
{
ASSERT(video);
video->vtable->update_video(video);
return video->current_frame;
}
/* Function: al_get_video_position
*/
double al_get_video_position(ALLEGRO_VIDEO *video, ALLEGRO_VIDEO_POSITION_TYPE which)
{
ASSERT(video);
if (which == ALLEGRO_VIDEO_POSITION_VIDEO_DECODE)
return video->video_position;
if (which == ALLEGRO_VIDEO_POSITION_AUDIO_DECODE)
return video->audio_position;
return video->position;
}
/* Function: al_seek_video
*/
bool al_seek_video(ALLEGRO_VIDEO *video, double pos_in_seconds)
{
ASSERT(video);
return video->vtable->seek_video(video, pos_in_seconds);
}
/* Function: al_get_video_audio_rate
*/
double al_get_video_audio_rate(ALLEGRO_VIDEO *video)
{
ASSERT(video);
return video->audio_rate;
}
/* Function: al_get_video_fps
*/
double al_get_video_fps(ALLEGRO_VIDEO *video)
{
ASSERT(video);
return video->fps;
}
/* Function: al_get_video_scaled_width
*/
float al_get_video_scaled_width(ALLEGRO_VIDEO *video)
{
ASSERT(video);
return video->scaled_width;
}
/* Function: al_get_video_scaled_height
*/
float al_get_video_scaled_height(ALLEGRO_VIDEO *video)
{
ASSERT(video);
return video->scaled_height;
}
/* Function: al_init_video_addon
*/
bool al_init_video_addon(void)
{
if (video_inited)
return true;
#ifdef ALLEGRO_CFG_VIDEO_HAVE_OGV
add_handler(".ogv", _al_video_ogv_vtable(), _al_video_identify_ogv);
#endif
if (_al_vector_size(&handlers) == 0) {
ALLEGRO_WARN("No video handlers available!\n");
return false;
}
_al_add_exit_func(al_shutdown_video_addon, "al_shutdown_video_addon");
video_inited = true;
return true;
}
/* Function: al_is_video_addon_initialized
*/
bool al_is_video_addon_initialized(void)
{
return video_inited;
}
/* Function: al_shutdown_video_addon
*/
void al_shutdown_video_addon(void)
{
if (!video_inited)
return;
_al_vector_free(&handlers);
video_inited = false;
}
/* Function: al_get_allegro_video_version
*/
uint32_t al_get_allegro_video_version(void)
{
return ALLEGRO_VERSION_INT;
}
/* Function: al_identify_video_f
*/
char const *al_identify_video_f(ALLEGRO_FILE *fp)
{
return identify_video(fp);
}
/* Function: al_identify_video
*/
char const *al_identify_video(char const *filename)
{
char const *ext;
ALLEGRO_FILE *fp = al_fopen(filename, "rb");
if (!fp)
return NULL;
ext = identify_video(fp);
al_fclose(fp);
return ext;
}
/* The returned width and height are always greater than or equal to the frame
* width and height. */
void _al_compute_scaled_dimensions(int frame_w, int frame_h, float aspect_ratio,
float *scaled_w, float *scaled_h)
{
if (aspect_ratio > 1.0) {
*scaled_w = frame_h * aspect_ratio;
*scaled_h = frame_h;
}
else {
*scaled_w = frame_w;
*scaled_h = frame_w / aspect_ratio;
}
}
/* vim: set sts=3 sw=3 et: */