Mercurial > mplayer.hg
annotate libmpdemux/demux_gif.c @ 21933:266b2a789fdb
version bump (for changes in r21907)
author | kraymer |
---|---|
date | Wed, 17 Jan 2007 14:09:48 +0000 |
parents | 277f0943aa4e |
children | f43d02e9b58b |
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 #include "mp_msg.h" | |
13 #include "help_mp.h" | |
14 | |
15 #include "stream.h" | |
16 #include "demuxer.h" | |
17 #include "stheader.h" | |
18 | |
19 #include <gif_lib.h> | |
21885 | 20 #include "libvo/fastmemcpy.h" |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
21 typedef struct { |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
22 int current_pts; |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
23 unsigned char *palette; |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
24 GifFileType *gif; |
21882
68ebac1f2b8d
Fix crash for gif images that have Top or Left set
reimar
parents:
21881
diff
changeset
|
25 int w, h; |
21891 | 26 int useref; |
21921 | 27 uint8_t *refimg; |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
28 } gif_priv_t; |
9133 | 29 |
30 #define GIF_SIGNATURE (('G' << 16) | ('I' << 8) | 'F') | |
31 | |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
32 #ifndef HAVE_GIF_TVT_HACK |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
33 // not supported by certain versions of the library |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
34 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
|
35 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
|
36 } |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
37 #endif |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
38 |
16175 | 39 static int gif_check_file(demuxer_t *demuxer) |
9133 | 40 { |
41 if (stream_read_int24(demuxer->stream) == GIF_SIGNATURE) | |
16175 | 42 return DEMUXER_TYPE_GIF; |
9133 | 43 return 0; |
44 } | |
45 | |
21920
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
46 static void memcpy_transp_pic(uint8_t *dst, uint8_t *src, int w, int h, |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
47 int dstride, int sstride, int transp, uint8_t trans_col) { |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
48 if (transp) { |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
49 dstride -= w; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
50 sstride -= w; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
51 while (h-- > 0) { |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
52 int wleft = w; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
53 while (wleft-- > 0) { |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
54 if (*src != trans_col) |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
55 *dst = *src; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
56 dst++; src++; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
57 } |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
58 dst += dstride; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
59 src += sstride; |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
60 } |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
61 } else |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
62 memcpy_pic(dst, src, w, h, dstride, sstride); |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
63 } |
65b2a9b3bd35
"Cosmetics" Introduce a memcpy function doing both transparent
reimar
parents:
21896
diff
changeset
|
64 |
16175 | 65 static int demux_gif_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) |
9133 | 66 { |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
67 gif_priv_t *priv = demuxer->priv; |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
68 GifFileType *gif = priv->gif; |
9133 | 69 GifRecordType type = UNDEFINED_RECORD_TYPE; |
70 int len = 0; | |
71 demux_packet_t *dp = NULL; | |
72 ColorMapObject *effective_map = NULL; | |
21921 | 73 uint8_t *buf = NULL; |
21891 | 74 int refmode = 0; |
21895 | 75 int transparency = 0; |
76 uint8_t transparent_col; | |
9133 | 77 |
78 while (type != IMAGE_DESC_RECORD_TYPE) { | |
79 if (DGifGetRecordType(gif, &type) == GIF_ERROR) { | |
80 PrintGifError(); | |
81 return 0; // oops | |
82 } | |
83 if (type == TERMINATE_RECORD_TYPE) | |
84 return 0; // eof | |
85 if (type == SCREEN_DESC_RECORD_TYPE) { | |
86 if (DGifGetScreenDesc(gif) == GIF_ERROR) { | |
87 PrintGifError(); | |
88 return 0; // oops | |
89 } | |
90 } | |
91 if (type == EXTENSION_RECORD_TYPE) { | |
92 int code; | |
93 unsigned char *p = NULL; | |
94 if (DGifGetExtension(gif, &code, &p) == GIF_ERROR) { | |
95 PrintGifError(); | |
96 return 0; // oops | |
97 } | |
98 if (code == 0xF9) { | |
99 int frametime = 0; | |
100 if (p[0] == 4) // is the length correct? | |
21891 | 101 { |
21895 | 102 transparency = p[1] & 1; |
21896 | 103 refmode = (p[1] >> 2) & 3; |
21875
d81cf0be50f0
Frametime was being read from the wrong offset, compare
diego
parents:
18958
diff
changeset
|
104 frametime = (p[3] << 8) | p[2]; // set the time, centiseconds |
21895 | 105 transparent_col = p[4]; |
21891 | 106 } |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
107 priv->current_pts += frametime; |
9133 | 108 } else if ((code == 0xFE) && (verbose)) { // comment extension |
109 // print iff verbose | |
110 printf("GIF comment: "); | |
111 while (p != NULL) { | |
112 int length = p[0]; | |
113 char *comments = p + 1; | |
114 comments[length] = 0; | |
115 printf("%s", comments); | |
116 if (DGifGetExtensionNext(gif, &p) == GIF_ERROR) { | |
117 PrintGifError(); | |
118 return 0; // oops | |
119 } | |
120 } | |
121 printf("\n"); | |
122 } | |
123 while (p != NULL) { | |
124 if (DGifGetExtensionNext(gif, &p) == GIF_ERROR) { | |
125 PrintGifError(); | |
126 return 0; // oops | |
127 } | |
128 } | |
129 } | |
130 } | |
131 | |
132 if (DGifGetImageDesc(gif) == GIF_ERROR) { | |
133 PrintGifError(); | |
134 return 0; // oops | |
135 } | |
136 | |
137 len = gif->Image.Width * gif->Image.Height; | |
21882
68ebac1f2b8d
Fix crash for gif images that have Top or Left set
reimar
parents:
21881
diff
changeset
|
138 dp = new_demux_packet(priv->w * priv->h); |
21890 | 139 buf = calloc(gif->Image.Width, gif->Image.Height); |
21891 | 140 if (priv->useref) |
141 memcpy(dp->buffer, priv->refimg, priv->w * priv->h); | |
142 else | |
143 memset(dp->buffer, gif->SBackGroundColor, priv->w * priv->h); | |
9133 | 144 |
145 if (DGifGetLine(gif, buf, len) == GIF_ERROR) { | |
146 PrintGifError(); | |
147 return 0; // oops | |
148 } | |
149 | |
150 effective_map = gif->Image.ColorMap; | |
151 if (effective_map == NULL) effective_map = gif->SColorMap; | |
152 | |
153 { | |
154 int y; | |
21883 | 155 int cnt = FFMIN(effective_map->ColorCount, 256); |
21884
7484e767c53d
One more bounds check, though IMO the gif lib really should do this.
reimar
parents:
21883
diff
changeset
|
156 int l = FFMAX(FFMIN(gif->Image.Left, priv->w), 0); |
7484e767c53d
One more bounds check, though IMO the gif lib really should do this.
reimar
parents:
21883
diff
changeset
|
157 int t = FFMAX(FFMIN(gif->Image.Top, priv->h), 0); |
7484e767c53d
One more bounds check, though IMO the gif lib really should do this.
reimar
parents:
21883
diff
changeset
|
158 int w = FFMAX(FFMIN(gif->Image.Width, priv->w - l), 0); |
7484e767c53d
One more bounds check, though IMO the gif lib really should do this.
reimar
parents:
21883
diff
changeset
|
159 int h = FFMAX(FFMIN(gif->Image.Height, priv->h - t), 0); |
21885 | 160 unsigned char *dest = dp->buffer + priv->w * t + l; |
9133 | 161 |
21876 | 162 // copy the palette |
21881
a10888bc9758
Fix invalid read for gifs with a palette for less than 256 colors
reimar
parents:
21880
diff
changeset
|
163 for (y = 0; y < cnt; y++) { |
21893 | 164 priv->palette[(y * 4) + 0] = effective_map->Colors[y].Blue; |
165 priv->palette[(y * 4) + 1] = effective_map->Colors[y].Green; | |
166 priv->palette[(y * 4) + 2] = effective_map->Colors[y].Red; | |
167 priv->palette[(y * 4) + 3] = 0; | |
9133 | 168 } |
169 | |
21922 | 170 if (gif->Image.Interlace) { |
171 uint8_t *s = buf; | |
172 memcpy_transp_pic(dest, s, w, h >> 3, | |
173 priv->w << 3, gif->Image.Width, | |
174 transparency, transparent_col); | |
175 s += (h >> 3) * w; | |
176 memcpy_transp_pic(dest + (gif->Image.Width << 2), s, w, h >> 3, | |
177 priv->w << 3, gif->Image.Width, | |
178 transparency, transparent_col); | |
179 s += (h >> 3) * w; | |
180 memcpy_transp_pic(dest + (gif->Image.Width << 1), s, w, h >> 2, | |
181 priv->w << 2, gif->Image.Width, | |
182 transparency, transparent_col); | |
183 s += (h >> 2) * w; | |
184 memcpy_transp_pic(dest + gif->Image.Width, s, w, h >> 1, | |
185 priv->w << 1, gif->Image.Width, | |
186 transparency, transparent_col); | |
187 } else | |
188 memcpy_transp_pic(dest, buf, w, h, priv->w, gif->Image.Width, | |
189 transparency, transparent_col); | |
21892 | 190 |
21893 | 191 if (refmode == 1) memcpy(priv->refimg, dp->buffer, priv->w * priv->h); |
21892 | 192 if (refmode == 2 && priv->useref) { |
193 dest = priv->refimg + priv->w * t + l; | |
194 memset(buf, gif->SBackGroundColor, len); | |
195 memcpy_pic(dest, buf, w, h, priv->w, gif->Image.Width); | |
196 } | |
21894 | 197 if (!(refmode & 2)) priv->useref = refmode & 1; |
9133 | 198 } |
199 | |
200 free(buf); | |
201 | |
202 demuxer->video->dpos++; | |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
203 dp->pts = ((float)priv->current_pts) / 100; |
9133 | 204 dp->pos = stream_tell(demuxer->stream); |
205 ds_add_packet(demuxer->video, dp); | |
206 | |
207 return 1; | |
208 } | |
209 | |
16175 | 210 static demuxer_t* demux_open_gif(demuxer_t* demuxer) |
9133 | 211 { |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
212 gif_priv_t *priv = calloc(1, sizeof(gif_priv_t)); |
9133 | 213 sh_video_t *sh_video = NULL; |
214 GifFileType *gif = NULL; | |
215 | |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
216 priv->current_pts = 0; |
9133 | 217 demuxer->seekable = 0; // FIXME |
218 | |
219 // go back to the beginning | |
9345 | 220 stream_seek(demuxer->stream,demuxer->stream->start_pos); |
9133 | 221 |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
222 #ifdef HAVE_GIF_TVT_HACK |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
223 // without the TVT functionality of libungif, a hard seek must be |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
224 // done to the beginning of the file. this is because libgif is |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
225 // unable to use mplayer's cache, and without this lseek libgif will |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
226 // not read from the beginning of the file and the command will fail. |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
227 // with this hack enabled, you will lose the ability to stream a GIF. |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
228 lseek(demuxer->stream->fd, 0, SEEK_SET); |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
229 gif = DGifOpenFileHandle(demuxer->stream->fd); |
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
230 #else |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
231 gif = DGifOpen(demuxer->stream, my_read_gif); |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
232 #endif |
9133 | 233 if (!gif) { |
234 PrintGifError(); | |
235 return NULL; | |
236 } | |
237 | |
238 // create a new video stream header | |
239 sh_video = new_sh_video(demuxer, 0); | |
240 | |
241 // make sure the demuxer knows about the new video stream header | |
242 // (even though new_sh_video() ought to take care of it) | |
243 demuxer->video->sh = sh_video; | |
244 | |
245 // make sure that the video demuxer stream header knows about its | |
246 // parent video demuxer stream (this is getting wacky), or else | |
247 // video_read_properties() will choke | |
248 sh_video->ds = demuxer->video; | |
249 | |
21887
36cc5a884b6a
Cast SWidth/SHeight to uint16_t, since that's what they actually are.
reimar
parents:
21886
diff
changeset
|
250 sh_video->disp_w = (uint16_t)gif->SWidth; |
36cc5a884b6a
Cast SWidth/SHeight to uint16_t, since that's what they actually are.
reimar
parents:
21886
diff
changeset
|
251 sh_video->disp_h = (uint16_t)gif->SHeight; |
9133 | 252 |
253 sh_video->format = mmioFOURCC(8, 'R', 'G', 'B'); | |
254 | |
255 sh_video->fps = 5.0f; | |
256 sh_video->frametime = 1.0f / sh_video->fps; | |
257 | |
258 sh_video->bih = malloc(sizeof(BITMAPINFOHEADER) + (256 * 4)); | |
259 sh_video->bih->biCompression = sh_video->format; | |
260 sh_video->bih->biBitCount = 8; | |
261 sh_video->bih->biPlanes = 2; | |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
262 priv->palette = (unsigned char *)(sh_video->bih + 1); |
21882
68ebac1f2b8d
Fix crash for gif images that have Top or Left set
reimar
parents:
21881
diff
changeset
|
263 priv->w = sh_video->disp_w; |
68ebac1f2b8d
Fix crash for gif images that have Top or Left set
reimar
parents:
21881
diff
changeset
|
264 priv->h = sh_video->disp_h; |
21891 | 265 priv->refimg = malloc(priv->w * priv->h); |
9133 | 266 |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
267 priv->gif = gif; |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
268 demuxer->priv = priv; |
9133 | 269 |
270 return demuxer; | |
271 } | |
272 | |
16175 | 273 static void demux_close_gif(demuxer_t* demuxer) |
9133 | 274 { |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
275 gif_priv_t *priv = demuxer->priv; |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
276 if (!priv) return; |
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
277 if (priv->gif && DGifCloseFile(priv->gif) == GIF_ERROR) |
9133 | 278 PrintGifError(); |
21891 | 279 free(priv->refimg); |
21880
4e22e06485c6
Move global variables in gif demuxer into priv struct
reimar
parents:
21876
diff
changeset
|
280 free(priv); |
9133 | 281 } |
16175 | 282 |
283 | |
284 demuxer_desc_t demuxer_desc_gif = { | |
285 "GIF demuxer", | |
286 "gif", | |
287 "GIF", | |
288 "Joey Parrish", | |
289 "", | |
290 DEMUXER_TYPE_GIF, | |
291 0, // unsafe autodetect | |
292 gif_check_file, | |
293 demux_gif_fill_buffer, | |
294 demux_open_gif, | |
295 demux_close_gif, | |
296 NULL, | |
297 NULL | |
298 }; |