Debian Codegraph @

addons/video/video.c

raw
1 /* This is just a quick hack. But good enough for our use of displaying
2 * a short intro video when our game starts up - so might as well share
3 * it.
4 *
5 * Known bugs:
6 *
7 * - Only very crude synching. Audio is slightly delayed and some
8 * videos seem to constantly drift off and then the audio gets all
9 * distorted...
10 *
11 * - Seeking/Pausing doesn't really work.
12 *
13 * - Memory leaks. Easy to fix but don't have time right now.
14 *
15 * Missing features:
16 *
17 * - Stream information. For example allow selection of one of several
18 * audio streams or subtitle overlay streams.
19 *
20 * - Non audio/video streams. For example something like:
21 * ALLEGRO_USTR *al_get_video_subtitle(float *x, float *y);
22 *
23 * - Buffering. Right now buffering is hardcoded to a fixed size which
24 * seemed enough for streaming 720p from disk in my tests. Obviously
25 * when streaming higher bandwidth or from a source with high
26 * fluctuation like an internet stream this won't work at all.
27 *
28 * - Provide an audio stream for the audio. Then could use this to
29 * stream audio files. Right now opening an .mp3 with the video
30 * addon will play it but only with the video API instead of Allegro's
31 * normal audio streaming API...
32 *
33 * - Audio/Video sync. For a game user-controlled sync is probably not
34 * too important as it can just ship with a properly synchronizeded
35 * video. However right now the audio delay is completely ignored.
36 *
37 * - Additional drivers. Also redo the API a bit so not everything
38 * has to be done by the driver.
39 */
40 #define ALLEGRO_INTERNAL_UNSTABLE
41
42 #include "allegro5/allegro5.h"
43 #include "allegro5/allegro_video.h"
44 #include "allegro5/internal/aintern.h"
45 #include "allegro5/internal/aintern_dtor.h"
46 #include "allegro5/internal/aintern_exitfunc.h"
47 #include "allegro5/internal/aintern_system.h"
48 #include "allegro5/internal/aintern_video.h"
49 #include "allegro5/internal/aintern_video_cfg.h"
50 #include "allegro5/internal/aintern_vector.h"
51
52 ALLEGRO_DEBUG_CHANNEL("video")
53
54
55 /* globals */
56 static bool video_inited = false;
57
58 typedef struct VideoHandler {
59 const char *extension;
60 ALLEGRO_VIDEO_INTERFACE *vtable;
61 ALLEGRO_VIDEO_IDENTIFIER_FUNCTION identifier;
62 } VideoHandler;
63
64 static _AL_VECTOR handlers = _AL_VECTOR_INITIALIZER(VideoHandler);
65
66 static const char* identify_video(ALLEGRO_FILE *f)
67 {
68 size_t i;
69 for (i = 0; i < _al_vector_size(&handlers); i++) {
70 VideoHandler *l = _al_vector_ref(&handlers, i);
71 if (l->identifier(f)) {
72 return l->extension;
73 }
74 }
75 return NULL;
76 }
77
78 static ALLEGRO_VIDEO_INTERFACE *find_handler(const char *extension)
79 {
80 size_t i;
81 for (i = 0; i < _al_vector_size(&handlers); i++) {
82 VideoHandler *l = _al_vector_ref(&handlers, i);
83 if (0 == _al_stricmp(extension, l->extension)) {
84 return l->vtable;
85 }
86 }
87 return NULL;
88 }
89
90 static void add_handler(const char *extension, ALLEGRO_VIDEO_INTERFACE *vtable,
91 ALLEGRO_VIDEO_IDENTIFIER_FUNCTION identifier)
92 {
93 VideoHandler *v = _al_vector_alloc_back(&handlers);
94 v->extension = extension;
95 v->vtable = vtable;
96 v->identifier = identifier;
97 }
98
99 /* Function: al_open_video
100 */
101 ALLEGRO_VIDEO *al_open_video(char const *filename)
102 {
103 ALLEGRO_VIDEO *video;
104 const char *ext;
105 video = al_calloc(1, sizeof *video);
106
107 ASSERT(filename);
108 ext = al_identify_video(filename);
109 if (!ext) {
110 ext = strrchr(filename, '.');
111 if (!ext) {
112 ALLEGRO_ERROR("Could not identify video %s!\n", filename);
113 }
114 }
115
116 video->vtable = find_handler(ext);
117 if (video->vtable == NULL) {
118 ALLEGRO_ERROR("No handler for video extension %s - "
119 "therefore not trying to load %s.\n", ext, filename);
120 al_free(video);
121 return NULL;
122 }
123
124 video->file = al_fopen(filename, "rb");
125
126 if (!video->vtable->open_video(video)) {
127 ALLEGRO_ERROR("Could not open %s.\n", filename);
128 al_free(video);
129 return NULL;
130 }
131
132 al_init_user_event_source(&video->es);
133 video->es_inited = true;
134 video->dtor_item = _al_register_destructor(_al_dtor_list, "video", video, (void (*)(void *)) al_close_video);
135
136 return video;
137 }
138
139 /* Function: al_open_video_f
140 */
141 ALLEGRO_VIDEO* al_open_video_f(ALLEGRO_FILE *fp, const char *ident)
142 {
143 ALLEGRO_VIDEO *video;
144 const char *ext;
145 video = al_calloc(1, sizeof *video);
146
147 ASSERT(fp);
148 if (!ident) {
149 ext = al_identify_video_f(fp);
150 al_fseek(fp, 0, ALLEGRO_SEEK_SET); // rewind to zero after al_identify_video_f
151 }
152 else
153 ext = ident;
154 if (!ext) {
155 ALLEGRO_ERROR("Could not identify video file from file interface!\n");
156 }
157
158 video->vtable = find_handler(ext);
159 if (video->vtable == NULL) {
160 ALLEGRO_ERROR("No handler for video extension %s - "
161 "therefore not trying to load video file from file interface.\n", ext);
162 al_free(video);
163 return NULL;
164 }
165
166 video->file = fp;
167
168 if (!video->vtable->open_video(video)) {
169 ALLEGRO_ERROR("Could not open video from from file interface.\n");
170 al_free(video);
171 return NULL;
172 }
173
174 al_init_user_event_source(&video->es);
175 video->es_inited = true;
176 video->dtor_item = _al_register_destructor(_al_dtor_list, "video", video, (void (*)(void*)) al_close_video);
177
178 return video;
179 }
180
181 /* Function: al_close_video
182 */
183 void al_close_video(ALLEGRO_VIDEO *video)
184 {
185 if (video) {
186 video->vtable->close_video(video);
187 if (video->es_inited) {
188 al_destroy_user_event_source(&video->es);
189 }
190 _al_unregister_destructor(_al_dtor_list, video->dtor_item);
191 al_free(video);
192 }
193 }
194
195 /* Function: al_get_video_event_source
196 */
197 ALLEGRO_EVENT_SOURCE *al_get_video_event_source(ALLEGRO_VIDEO *video)
198 {
199 ASSERT(video);
200 return &video->es;
201 }
202
203 /* Function: al_start_video
204 */
205 void al_start_video(ALLEGRO_VIDEO *video, ALLEGRO_MIXER *mixer)
206 {
207 ASSERT(video);
208
209 /* XXX why is this not just a parameter? */
210 video->mixer = mixer;
211 video->playing = true;
212 /* Destruction is handled by al_close_video. */
213 _al_push_destructor_owner();
214 video->vtable->start_video(video);
215 _al_pop_destructor_owner();
216 }
217
218 /* Function: al_start_video_with_voice
219 */
220 void al_start_video_with_voice(ALLEGRO_VIDEO *video, ALLEGRO_VOICE *voice)
221 {
222 ASSERT(video);
223
224 /* XXX why is voice not just a parameter? */
225 video->voice = voice;
226 video->vtable->start_video(video);
227 }
228
229 /* Function: al_set_video_playing
230 */
231 void al_set_video_playing(ALLEGRO_VIDEO *video, bool play)
232 {
233 ASSERT(video);
234
235 if (play != video->playing) {
236 video->playing = play;
237 video->vtable->set_video_playing(video);
238 }
239 }
240
241 /* Function: al_is_video_playing
242 */
243 bool al_is_video_playing(ALLEGRO_VIDEO *video)
244 {
245 ASSERT(video);
246
247 return video->playing;
248 }
249
250 /* Function: al_get_video_frame
251 */
252 ALLEGRO_BITMAP *al_get_video_frame(ALLEGRO_VIDEO *video)
253 {
254 ASSERT(video);
255
256 video->vtable->update_video(video);
257 return video->current_frame;
258 }
259
260 /* Function: al_get_video_position
261 */
262 double al_get_video_position(ALLEGRO_VIDEO *video, ALLEGRO_VIDEO_POSITION_TYPE which)
263 {
264 ASSERT(video);
265
266 if (which == ALLEGRO_VIDEO_POSITION_VIDEO_DECODE)
267 return video->video_position;
268 if (which == ALLEGRO_VIDEO_POSITION_AUDIO_DECODE)
269 return video->audio_position;
270 return video->position;
271 }
272
273 /* Function: al_seek_video
274 */
275 bool al_seek_video(ALLEGRO_VIDEO *video, double pos_in_seconds)
276 {
277 ASSERT(video);
278
279 return video->vtable->seek_video(video, pos_in_seconds);
280 }
281
282 /* Function: al_get_video_audio_rate
283 */
284 double al_get_video_audio_rate(ALLEGRO_VIDEO *video)
285 {
286 ASSERT(video);
287 return video->audio_rate;
288 }
289
290 /* Function: al_get_video_fps
291 */
292 double al_get_video_fps(ALLEGRO_VIDEO *video)
293 {
294 ASSERT(video);
295 return video->fps;
296 }
297
298 /* Function: al_get_video_scaled_width
299 */
300 float al_get_video_scaled_width(ALLEGRO_VIDEO *video)
301 {
302 ASSERT(video);
303 return video->scaled_width;
304 }
305
306 /* Function: al_get_video_scaled_height
307 */
308 float al_get_video_scaled_height(ALLEGRO_VIDEO *video)
309 {
310 ASSERT(video);
311 return video->scaled_height;
312 }
313
314 /* Function: al_init_video_addon
315 */
316 bool al_init_video_addon(void)
317 {
318 if (video_inited)
319 return true;
320
321 #ifdef ALLEGRO_CFG_VIDEO_HAVE_OGV
322 add_handler(".ogv", _al_video_ogv_vtable(), _al_video_identify_ogv);
323 #endif
324
325 if (_al_vector_size(&handlers) == 0) {
326 ALLEGRO_WARN("No video handlers available!\n");
327 return false;
328 }
329
330 _al_add_exit_func(al_shutdown_video_addon, "al_shutdown_video_addon");
331 video_inited = true;
332 return true;
333 }
334
335
336 /* Function: al_is_video_addon_initialized
337 */
338 bool al_is_video_addon_initialized(void)
339 {
340 return video_inited;
341 }
342
343
344 /* Function: al_shutdown_video_addon
345 */
346 void al_shutdown_video_addon(void)
347 {
348 if (!video_inited)
349 return;
350
351 _al_vector_free(&handlers);
352
353 video_inited = false;
354 }
355
356
357 /* Function: al_get_allegro_video_version
358 */
359 uint32_t al_get_allegro_video_version(void)
360 {
361 return ALLEGRO_VERSION_INT;
362 }
363
364
365 /* Function: al_identify_video_f
366 */
367 char const *al_identify_video_f(ALLEGRO_FILE *fp)
368 {
369 return identify_video(fp);
370 }
371
372
373 /* Function: al_identify_video
374 */
375 char const *al_identify_video(char const *filename)
376 {
377 char const *ext;
378 ALLEGRO_FILE *fp = al_fopen(filename, "rb");
379 if (!fp)
380 return NULL;
381 ext = identify_video(fp);
382 al_fclose(fp);
383 return ext;
384 }
385
386 /* The returned width and height are always greater than or equal to the frame
387 * width and height. */
388 void _al_compute_scaled_dimensions(int frame_w, int frame_h, float aspect_ratio,
389 float *scaled_w, float *scaled_h)
390 {
391 if (aspect_ratio > 1.0) {
392 *scaled_w = frame_h * aspect_ratio;
393 *scaled_h = frame_h;
394 }
395 else {
396 *scaled_w = frame_w;
397 *scaled_h = frame_w / aspect_ratio;
398 }
399 }
400
401 /* vim: set sts=3 sw=3 et: */