annotate libmpdemux/demux_fli.c @ 3104:829637d529e9

fixed divide-by-0 bug in FLI demuxer
author melanson
date Sun, 25 Nov 2001 00:56:41 +0000
parents 637e540831b9
children f951f3be126c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3101
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
1 /*
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
2 FLI file parser for the MPlayer program
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
3 by Mike Melanson
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
4 */
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
5
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
6 #include <stdio.h>
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
7 #include <stdlib.h>
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
8 #include <unistd.h>
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
9
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
10 #include "config.h"
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
11 #include "mp_msg.h"
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
12 #include "help_mp.h"
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
13
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
14 #include "stream.h"
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
15 #include "demuxer.h"
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
16 #include "stheader.h"
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
17
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
18 typedef struct _fli_frames_t {
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
19 int num_frames;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
20 int current_frame;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
21 off_t *filepos;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
22 unsigned int *frame_size;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
23 } fli_frames_t;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
24
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
25 // return value:
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
26 // 0 = EOF or no stream found
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
27 // 1 = successfully read a packet
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
28 int demux_fli_fill_buffer(demuxer_t *demuxer){
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
29 fli_frames_t *frames = (fli_frames_t *)demuxer->priv;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
30
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
31 // see if the end has been reached
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
32 if (frames->current_frame == frames->num_frames)
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
33 return 0;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
34
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
35 // fetch the frame from the file
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
36 // first, position the file properly since ds_read_packet() doesn't
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
37 // seem to do it, even though it takes a file offset as a parameter
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
38 stream_seek(demuxer->stream, frames->filepos[frames->current_frame]);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
39 ds_read_packet(demuxer->video,
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
40 demuxer->stream,
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
41 frames->frame_size[frames->current_frame],
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
42 0, /* not sure what pts is for */
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
43 frames->filepos[frames->current_frame],
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
44 0 /* what flags? */
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
45 );
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
46
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
47 // get the next frame ready
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
48 frames->current_frame++;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
49
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
50 return 1;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
51 }
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
52
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
53 demuxer_t* demux_open_fli(demuxer_t* demuxer){
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
54 sh_video_t *sh_video = NULL;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
55 fli_frames_t *frames = (fli_frames_t *)malloc(sizeof(fli_frames_t));
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
56 int frame_number;
3104
829637d529e9 fixed divide-by-0 bug in FLI demuxer
melanson
parents: 3101
diff changeset
57 int speed;
3101
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
58
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
59 // go back to the beginning
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
60 stream_reset(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
61 stream_seek(demuxer->stream, 0);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
62 demuxer->movi_start = 128;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
63 demuxer->movi_end = stream_read_dword_le(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
64
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
65 // skip the magic number
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
66 stream_skip(demuxer->stream, 2);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
67
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
68 // fetch the number of frames
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
69 frames->num_frames = stream_read_word_le(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
70 frames->current_frame = 0;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
71
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
72 // allocate enough entries for the indices
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
73 frames->filepos = (off_t *)malloc(frames->num_frames * sizeof(off_t));
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
74 frames->frame_size = (int *)malloc(frames->num_frames * sizeof(int));
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
75
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
76 // create a new video stream header
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
77 sh_video = new_sh_video(demuxer, 0);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
78
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
79 // make sure the demuxer knows about the new video stream header
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
80 // (even though new_sh_video() ought to take care of it)
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
81 demuxer->video->sh = sh_video;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
82
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
83 // make sure that the video demuxer stream header knows about its
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
84 // parent video demuxer stream (this is getting wacky), or else
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
85 // video_read_properties() will choke
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
86 sh_video->ds = demuxer->video;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
87
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
88 // custom fourcc for internal MPlayer use
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
89 sh_video->format = mmioFOURCC('F', 'L', 'I', 'C');
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
90
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
91 sh_video->disp_w = stream_read_word_le(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
92 sh_video->disp_h = stream_read_word_le(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
93
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
94 // skip the video depth and flags
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
95 stream_skip(demuxer->stream, 4);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
96
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
97 // get the speed
3104
829637d529e9 fixed divide-by-0 bug in FLI demuxer
melanson
parents: 3101
diff changeset
98 speed = stream_read_word_le(demuxer->stream);
829637d529e9 fixed divide-by-0 bug in FLI demuxer
melanson
parents: 3101
diff changeset
99 if (speed == 0)
829637d529e9 fixed divide-by-0 bug in FLI demuxer
melanson
parents: 3101
diff changeset
100 speed = 1;
829637d529e9 fixed divide-by-0 bug in FLI demuxer
melanson
parents: 3101
diff changeset
101 sh_video->fps = 1000 / speed;
3101
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
102 sh_video->frametime = 1/sh_video->fps;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
103
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
104 // build the frame index
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
105 stream_seek(demuxer->stream, demuxer->movi_start);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
106 frame_number = 0;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
107 while ((!stream_eof(demuxer->stream)) && (frame_number < frames->num_frames))
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
108 {
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
109 frames->filepos[frame_number] = stream_tell(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
110 frames->frame_size[frame_number] = stream_read_dword_le(demuxer->stream);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
111 stream_skip(demuxer->stream, frames->frame_size[frame_number] - 4);
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
112 frame_number++;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
113 }
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
114
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
115 demuxer->priv = frames;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
116
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
117 return demuxer;
637e540831b9 mostly complete support for loading and decoding FLI/FLC animations
melanson
parents:
diff changeset
118 }