Mercurial > mplayer.hg
annotate libmpdemux/demux_gif.c @ 9367:f107fcf6fdea
10l
author | jonas |
---|---|
date | Sun, 09 Feb 2003 13:13:00 +0000 |
parents | fef4f1a9de5e |
children | 93375ee56629 |
rev | line source |
---|---|
9133 | 1 /* |
2 GIF file parser for MPlayer | |
3 by Joey Parrish | |
4 */ | |
5 | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include <unistd.h> | |
9 | |
10 #include "config.h" | |
11 | |
12 #ifdef HAVE_GIF | |
13 | |
14 #include "mp_msg.h" | |
15 #include "help_mp.h" | |
16 | |
17 #include "stream.h" | |
18 #include "demuxer.h" | |
19 #include "stheader.h" | |
20 | |
21 #include <gif_lib.h> | |
22 static int current_pts = 0; | |
23 static unsigned char *pallete = NULL; | |
24 | |
25 #define GIF_SIGNATURE (('G' << 16) | ('I' << 8) | 'F') | |
26 | |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
27 int my_read_gif(GifFileType *gif, uint8_t *buf, int len) { |
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
28 return stream_read(gif->UserData, buf, len); |
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
29 } |
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
30 |
9133 | 31 int gif_check_file(demuxer_t *demuxer) |
32 { | |
33 if (stream_read_int24(demuxer->stream) == GIF_SIGNATURE) | |
34 return 1; | |
35 return 0; | |
36 } | |
37 | |
38 int demux_gif_fill_buffer(demuxer_t *demuxer) | |
39 { | |
40 GifFileType *gif = (GifFileType *)demuxer->priv; | |
41 sh_video_t *sh_video = (sh_video_t *)demuxer->video->sh; | |
42 GifRecordType type = UNDEFINED_RECORD_TYPE; | |
43 int len = 0; | |
44 demux_packet_t *dp = NULL; | |
45 ColorMapObject *effective_map = NULL; | |
46 char *buf = NULL; | |
47 | |
48 while (type != IMAGE_DESC_RECORD_TYPE) { | |
49 if (DGifGetRecordType(gif, &type) == GIF_ERROR) { | |
50 PrintGifError(); | |
51 return 0; // oops | |
52 } | |
53 if (type == TERMINATE_RECORD_TYPE) | |
54 return 0; // eof | |
55 if (type == SCREEN_DESC_RECORD_TYPE) { | |
56 if (DGifGetScreenDesc(gif) == GIF_ERROR) { | |
57 PrintGifError(); | |
58 return 0; // oops | |
59 } | |
60 } | |
61 if (type == EXTENSION_RECORD_TYPE) { | |
62 int code; | |
63 unsigned char *p = NULL; | |
64 if (DGifGetExtension(gif, &code, &p) == GIF_ERROR) { | |
65 PrintGifError(); | |
66 return 0; // oops | |
67 } | |
68 if (code == 0xF9) { | |
69 int frametime = 0; | |
70 if (p[0] == 4) // is the length correct? | |
71 frametime = (p[1] << 8) | p[2]; // set the time, centiseconds | |
72 current_pts += frametime; | |
73 } else if ((code == 0xFE) && (verbose)) { // comment extension | |
74 // print iff verbose | |
75 printf("GIF comment: "); | |
76 while (p != NULL) { | |
77 int length = p[0]; | |
78 char *comments = p + 1; | |
79 comments[length] = 0; | |
80 printf("%s", comments); | |
81 if (DGifGetExtensionNext(gif, &p) == GIF_ERROR) { | |
82 PrintGifError(); | |
83 return 0; // oops | |
84 } | |
85 } | |
86 printf("\n"); | |
87 } | |
88 while (p != NULL) { | |
89 if (DGifGetExtensionNext(gif, &p) == GIF_ERROR) { | |
90 PrintGifError(); | |
91 return 0; // oops | |
92 } | |
93 } | |
94 } | |
95 } | |
96 | |
97 if (DGifGetImageDesc(gif) == GIF_ERROR) { | |
98 PrintGifError(); | |
99 return 0; // oops | |
100 } | |
101 | |
102 len = gif->Image.Width * gif->Image.Height; | |
103 dp = new_demux_packet(len); | |
104 buf = malloc(len); | |
105 memset(buf, 0, len); | |
106 memset(dp->buffer, 0, len); | |
107 | |
108 if (DGifGetLine(gif, buf, len) == GIF_ERROR) { | |
109 PrintGifError(); | |
110 return 0; // oops | |
111 } | |
112 | |
113 effective_map = gif->Image.ColorMap; | |
114 if (effective_map == NULL) effective_map = gif->SColorMap; | |
115 | |
116 { | |
117 int y; | |
118 | |
119 // copy the pallete | |
120 for (y = 0; y < 256; y++) { | |
121 pallete[(y * 4) + 0] = effective_map->Colors[y].Blue; | |
122 pallete[(y * 4) + 1] = effective_map->Colors[y].Green; | |
123 pallete[(y * 4) + 2] = effective_map->Colors[y].Red; | |
124 pallete[(y * 4) + 3] = 0; | |
125 } | |
126 | |
127 for (y = 0; y < gif->Image.Height; y++) { | |
128 unsigned char *drow = dp->buffer; | |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
129 unsigned char *gbuf = buf + (y * gif->Image.Width); |
9133 | 130 |
131 drow += gif->Image.Width * (y + gif->Image.Top); | |
132 drow += gif->Image.Left; | |
133 | |
134 memcpy(drow, gbuf, gif->Image.Width); | |
135 } | |
136 } | |
137 | |
138 free(buf); | |
139 | |
140 demuxer->video->dpos++; | |
141 dp->pts = ((float)current_pts) / 100; | |
142 dp->pos = stream_tell(demuxer->stream); | |
143 ds_add_packet(demuxer->video, dp); | |
144 | |
145 return 1; | |
146 } | |
147 | |
148 demuxer_t* demux_open_gif(demuxer_t* demuxer) | |
149 { | |
150 sh_video_t *sh_video = NULL; | |
151 GifFileType *gif = NULL; | |
152 | |
153 current_pts = 0; | |
154 demuxer->seekable = 0; // FIXME | |
155 | |
156 // go back to the beginning | |
9345 | 157 stream_seek(demuxer->stream,demuxer->stream->start_pos); |
9133 | 158 |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
159 gif = DGifOpen(demuxer->stream, my_read_gif); |
9133 | 160 if (!gif) { |
161 PrintGifError(); | |
162 return NULL; | |
163 } | |
164 | |
165 // create a new video stream header | |
166 sh_video = new_sh_video(demuxer, 0); | |
167 | |
168 // make sure the demuxer knows about the new video stream header | |
169 // (even though new_sh_video() ought to take care of it) | |
170 demuxer->video->sh = sh_video; | |
171 | |
172 // make sure that the video demuxer stream header knows about its | |
173 // parent video demuxer stream (this is getting wacky), or else | |
174 // video_read_properties() will choke | |
175 sh_video->ds = demuxer->video; | |
176 | |
177 sh_video->disp_w = gif->SWidth; | |
178 sh_video->disp_h = gif->SHeight; | |
179 | |
180 sh_video->format = mmioFOURCC(8, 'R', 'G', 'B'); | |
181 | |
182 sh_video->fps = 5.0f; | |
183 sh_video->frametime = 1.0f / sh_video->fps; | |
184 | |
185 sh_video->bih = malloc(sizeof(BITMAPINFOHEADER) + (256 * 4)); | |
186 sh_video->bih->biCompression = sh_video->format; | |
187 sh_video->bih->biBitCount = 8; | |
188 sh_video->bih->biPlanes = 2; | |
189 pallete = (unsigned char *)(sh_video->bih + 1); | |
190 | |
191 demuxer->priv = gif; | |
192 | |
193 return demuxer; | |
194 } | |
195 | |
196 void demux_close_gif(demuxer_t* demuxer) | |
197 { | |
198 GifFileType *gif = (GifFileType *)demuxer->priv; | |
199 | |
200 if(!gif) | |
201 return; | |
202 | |
203 if (DGifCloseFile(gif) == GIF_ERROR) | |
204 PrintGifError(); | |
205 | |
206 demuxer->stream->fd = 0; | |
207 demuxer->priv = NULL; | |
208 } | |
209 #endif /* HAVE_GIF */ |