Mercurial > mplayer.hg
annotate libmpdemux/demux_gif.c @ 20962:a95ed9a69caf
Put files fully owned by my under GPL v2 "or later"
author | reimar |
---|---|
date | Fri, 17 Nov 2006 10:03:33 +0000 |
parents | a8e681ad7c90 |
children | d81cf0be50f0 |
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> | |
20 static int current_pts = 0; | |
21 static unsigned char *pallete = NULL; | |
22 | |
23 #define GIF_SIGNATURE (('G' << 16) | ('I' << 8) | 'F') | |
24 | |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
25 #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
|
26 // 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
|
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 } |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
30 #endif |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
31 |
16175 | 32 static int gif_check_file(demuxer_t *demuxer) |
9133 | 33 { |
34 if (stream_read_int24(demuxer->stream) == GIF_SIGNATURE) | |
16175 | 35 return DEMUXER_TYPE_GIF; |
9133 | 36 return 0; |
37 } | |
38 | |
16175 | 39 static int demux_gif_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds) |
9133 | 40 { |
41 GifFileType *gif = (GifFileType *)demuxer->priv; | |
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 | |
16175 | 148 static demuxer_t* demux_open_gif(demuxer_t* demuxer) |
9133 | 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 |
9463
93375ee56629
gif library incompatibility fixes and prefere libungif over libgif. Patch by Joey Parrish <joey@nicewarrior.org>
alex
parents:
9345
diff
changeset
|
159 #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
|
160 // 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
|
161 // 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
|
162 // 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
|
163 // 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
|
164 // 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
|
165 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
|
166 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
|
167 #else |
9344
bd7b5078475e
1) codecs.conf changed recently and demux_gif no longer needs to spit
arpi
parents:
9133
diff
changeset
|
168 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
|
169 #endif |
9133 | 170 if (!gif) { |
171 PrintGifError(); | |
172 return NULL; | |
173 } | |
174 | |
175 // create a new video stream header | |
176 sh_video = new_sh_video(demuxer, 0); | |
177 | |
178 // make sure the demuxer knows about the new video stream header | |
179 // (even though new_sh_video() ought to take care of it) | |
180 demuxer->video->sh = sh_video; | |
181 | |
182 // make sure that the video demuxer stream header knows about its | |
183 // parent video demuxer stream (this is getting wacky), or else | |
184 // video_read_properties() will choke | |
185 sh_video->ds = demuxer->video; | |
186 | |
187 sh_video->disp_w = gif->SWidth; | |
188 sh_video->disp_h = gif->SHeight; | |
189 | |
190 sh_video->format = mmioFOURCC(8, 'R', 'G', 'B'); | |
191 | |
192 sh_video->fps = 5.0f; | |
193 sh_video->frametime = 1.0f / sh_video->fps; | |
194 | |
195 sh_video->bih = malloc(sizeof(BITMAPINFOHEADER) + (256 * 4)); | |
196 sh_video->bih->biCompression = sh_video->format; | |
197 sh_video->bih->biBitCount = 8; | |
198 sh_video->bih->biPlanes = 2; | |
199 pallete = (unsigned char *)(sh_video->bih + 1); | |
200 | |
201 demuxer->priv = gif; | |
202 | |
203 return demuxer; | |
204 } | |
205 | |
16175 | 206 static void demux_close_gif(demuxer_t* demuxer) |
9133 | 207 { |
208 GifFileType *gif = (GifFileType *)demuxer->priv; | |
209 | |
210 if(!gif) | |
211 return; | |
212 | |
213 if (DGifCloseFile(gif) == GIF_ERROR) | |
214 PrintGifError(); | |
215 | |
216 demuxer->stream->fd = 0; | |
217 demuxer->priv = NULL; | |
218 } | |
16175 | 219 |
220 | |
221 demuxer_desc_t demuxer_desc_gif = { | |
222 "GIF demuxer", | |
223 "gif", | |
224 "GIF", | |
225 "Joey Parrish", | |
226 "", | |
227 DEMUXER_TYPE_GIF, | |
228 0, // unsafe autodetect | |
229 gif_check_file, | |
230 demux_gif_fill_buffer, | |
231 demux_open_gif, | |
232 demux_close_gif, | |
233 NULL, | |
234 NULL | |
235 }; |