Rename var: val -> energy
[FFMpeg-mirror/DVCPRO-HD.git] / libavformat / gifdec.c
blob82a80c8dcf48ff68f3ee81d259c8d7c945f25023
1 /*
2 * GIF demuxer
3 * Copyright (c) 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "avformat.h"
23 //#define DEBUG
25 #define MAXBITS 12
26 #define SIZTABLE (1<<MAXBITS)
28 #define GCE_DISPOSAL_NONE 0
29 #define GCE_DISPOSAL_INPLACE 1
30 #define GCE_DISPOSAL_BACKGROUND 2
31 #define GCE_DISPOSAL_RESTORE 3
33 typedef struct GifState {
34 int screen_width;
35 int screen_height;
36 int bits_per_pixel;
37 int background_color_index;
38 int transparent_color_index;
39 int color_resolution;
40 uint8_t *image_buf;
41 int image_linesize;
42 uint32_t *image_palette;
43 int pix_fmt;
45 /* after the frame is displayed, the disposal method is used */
46 int gce_disposal;
47 /* delay during which the frame is shown */
48 int gce_delay;
50 /* LZW compatible decoder */
51 ByteIOContext *f;
52 int eob_reached;
53 uint8_t *pbuf, *ebuf;
54 int bbits;
55 unsigned int bbuf;
57 int cursize; /* The current code size */
58 int curmask;
59 int codesize;
60 int clear_code;
61 int end_code;
62 int newcodes; /* First available code */
63 int top_slot; /* Highest code for current size */
64 int slot; /* Last read code */
65 int fc, oc;
66 uint8_t *sp;
67 uint8_t stack[SIZTABLE];
68 uint8_t suffix[SIZTABLE];
69 uint16_t prefix[SIZTABLE];
71 /* aux buffers */
72 uint8_t global_palette[256 * 3];
73 uint8_t local_palette[256 * 3];
74 uint8_t buf[256];
75 } GifState;
78 static const uint8_t gif87a_sig[6] = "GIF87a";
79 static const uint8_t gif89a_sig[6] = "GIF89a";
81 static const uint16_t mask[17] =
83 0x0000, 0x0001, 0x0003, 0x0007,
84 0x000F, 0x001F, 0x003F, 0x007F,
85 0x00FF, 0x01FF, 0x03FF, 0x07FF,
86 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
89 /* Probe gif video format or gif image format. The current heuristic
90 supposes the gif87a is always a single image. For gif89a, we
91 consider it as a video only if a GCE extension is present in the
92 first kilobyte. */
93 static int gif_video_probe(AVProbeData * pd)
95 const uint8_t *p, *p_end;
96 int bits_per_pixel, has_global_palette, ext_code, ext_len;
97 int gce_flags, gce_disposal;
99 if (pd->buf_size < 24 ||
100 memcmp(pd->buf, gif89a_sig, 6) != 0)
101 return 0;
102 p_end = pd->buf + pd->buf_size;
103 p = pd->buf + 6;
104 bits_per_pixel = (p[4] & 0x07) + 1;
105 has_global_palette = (p[4] & 0x80);
106 p += 7;
107 if (has_global_palette)
108 p += (1 << bits_per_pixel) * 3;
109 for(;;) {
110 if (p >= p_end)
111 return 0;
112 if (*p != '!')
113 break;
114 p++;
115 if (p >= p_end)
116 return 0;
117 ext_code = *p++;
118 if (p >= p_end)
119 return 0;
120 ext_len = *p++;
121 if (ext_code == 0xf9) {
122 if (p >= p_end)
123 return 0;
124 /* if GCE extension found with gce_disposal != 0: it is
125 likely to be an animation */
126 gce_flags = *p++;
127 gce_disposal = (gce_flags >> 2) & 0x7;
128 if (gce_disposal != 0)
129 return AVPROBE_SCORE_MAX;
130 else
131 return 0;
133 for(;;) {
134 if (ext_len == 0)
135 break;
136 p += ext_len;
137 if (p >= p_end)
138 return 0;
139 ext_len = *p++;
142 return 0;
145 static void GLZWDecodeInit(GifState * s, int csize)
147 /* read buffer */
148 s->eob_reached = 0;
149 s->pbuf = s->buf;
150 s->ebuf = s->buf;
151 s->bbuf = 0;
152 s->bbits = 0;
154 /* decoder */
155 s->codesize = csize;
156 s->cursize = s->codesize + 1;
157 s->curmask = mask[s->cursize];
158 s->top_slot = 1 << s->cursize;
159 s->clear_code = 1 << s->codesize;
160 s->end_code = s->clear_code + 1;
161 s->slot = s->newcodes = s->clear_code + 2;
162 s->oc = s->fc = 0;
163 s->sp = s->stack;
166 /* XXX: optimize */
167 static inline int GetCode(GifState * s)
169 int c, sizbuf;
170 uint8_t *ptr;
172 while (s->bbits < s->cursize) {
173 ptr = s->pbuf;
174 if (ptr >= s->ebuf) {
175 if (!s->eob_reached) {
176 sizbuf = get_byte(s->f);
177 s->ebuf = s->buf + sizbuf;
178 s->pbuf = s->buf;
179 if (sizbuf > 0) {
180 get_buffer(s->f, s->buf, sizbuf);
181 } else {
182 s->eob_reached = 1;
185 ptr = s->pbuf;
187 s->bbuf |= ptr[0] << s->bbits;
188 ptr++;
189 s->pbuf = ptr;
190 s->bbits += 8;
192 c = s->bbuf & s->curmask;
193 s->bbuf >>= s->cursize;
194 s->bbits -= s->cursize;
195 return c;
198 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
199 written by Steven A. Bennett in 1987. */
200 /* return the number of byte decoded */
201 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
203 int l, c, code, oc, fc;
204 uint8_t *sp;
206 if (s->end_code < 0)
207 return 0;
209 l = len;
210 sp = s->sp;
211 oc = s->oc;
212 fc = s->fc;
214 while (sp > s->stack) {
215 *buf++ = *(--sp);
216 if ((--l) == 0)
217 goto the_end;
220 for (;;) {
221 c = GetCode(s);
222 if (c == s->end_code) {
223 s->end_code = -1;
224 break;
225 } else if (c == s->clear_code) {
226 s->cursize = s->codesize + 1;
227 s->curmask = mask[s->cursize];
228 s->slot = s->newcodes;
229 s->top_slot = 1 << s->cursize;
230 while ((c = GetCode(s)) == s->clear_code);
231 if (c == s->end_code) {
232 s->end_code = -1;
233 break;
235 /* test error */
236 if (c >= s->slot)
237 c = 0;
238 fc = oc = c;
239 *buf++ = c;
240 if ((--l) == 0)
241 break;
242 } else {
243 code = c;
244 if (code >= s->slot) {
245 *sp++ = fc;
246 code = oc;
248 while (code >= s->newcodes) {
249 *sp++ = s->suffix[code];
250 code = s->prefix[code];
252 *sp++ = code;
253 if (s->slot < s->top_slot) {
254 s->suffix[s->slot] = fc = code;
255 s->prefix[s->slot++] = oc;
256 oc = c;
258 if (s->slot >= s->top_slot) {
259 if (s->cursize < MAXBITS) {
260 s->top_slot <<= 1;
261 s->curmask = mask[++s->cursize];
264 while (sp > s->stack) {
265 *buf++ = *(--sp);
266 if ((--l) == 0)
267 goto the_end;
271 the_end:
272 s->sp = sp;
273 s->oc = oc;
274 s->fc = fc;
275 return len - l;
278 static int gif_read_image(GifState *s)
280 ByteIOContext *f = s->f;
281 int left, top, width, height, bits_per_pixel, code_size, flags;
282 int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
283 uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
285 left = get_le16(f);
286 top = get_le16(f);
287 width = get_le16(f);
288 height = get_le16(f);
289 flags = get_byte(f);
290 is_interleaved = flags & 0x40;
291 has_local_palette = flags & 0x80;
292 bits_per_pixel = (flags & 0x07) + 1;
293 #ifdef DEBUG
294 printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
295 #endif
297 if (has_local_palette) {
298 get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
299 palette = s->local_palette;
300 } else {
301 palette = s->global_palette;
302 bits_per_pixel = s->bits_per_pixel;
305 /* verify that all the image is inside the screen dimensions */
306 if (left + width > s->screen_width ||
307 top + height > s->screen_height)
308 return AVERROR(EINVAL);
310 /* build the palette */
311 if (s->pix_fmt == PIX_FMT_RGB24) {
312 line = av_malloc(width);
313 if (!line)
314 return AVERROR(ENOMEM);
315 } else {
316 n = (1 << bits_per_pixel);
317 spal = palette;
318 for(i = 0; i < n; i++) {
319 s->image_palette[i] = (0xff << 24) |
320 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
321 spal += 3;
323 for(; i < 256; i++)
324 s->image_palette[i] = (0xff << 24);
325 /* handle transparency */
326 if (s->transparent_color_index >= 0)
327 s->image_palette[s->transparent_color_index] = 0;
328 line = NULL;
331 /* now get the image data */
332 s->f = f;
333 code_size = get_byte(f);
334 GLZWDecodeInit(s, code_size);
336 /* read all the image */
337 linesize = s->image_linesize;
338 ptr1 = s->image_buf + top * linesize + (left * 3);
339 ptr = ptr1;
340 pass = 0;
341 y1 = 0;
342 for (y = 0; y < height; y++) {
343 if (s->pix_fmt == PIX_FMT_RGB24) {
344 /* transcode to RGB24 */
345 GLZWDecode(s, line, width);
346 d = ptr;
347 sptr = line;
348 for(x = 0; x < width; x++) {
349 spal = palette + sptr[0] * 3;
350 d[0] = spal[0];
351 d[1] = spal[1];
352 d[2] = spal[2];
353 d += 3;
354 sptr++;
356 } else {
357 GLZWDecode(s, ptr, width);
359 if (is_interleaved) {
360 switch(pass) {
361 default:
362 case 0:
363 case 1:
364 y1 += 8;
365 ptr += linesize * 8;
366 if (y1 >= height) {
367 y1 = pass == 0 ? 4 : 2;
368 ptr = ptr1 + linesize * y1;
369 pass++;
371 break;
372 case 2:
373 y1 += 4;
374 ptr += linesize * 4;
375 if (y1 >= height) {
376 y1 = 1;
377 ptr = ptr1 + linesize;
378 pass++;
380 break;
381 case 3:
382 y1 += 2;
383 ptr += linesize * 2;
384 break;
386 } else {
387 ptr += linesize;
390 av_free(line);
392 /* read the garbage data until end marker is found */
393 while (!s->eob_reached)
394 GetCode(s);
395 return 0;
398 static int gif_read_extension(GifState *s)
400 ByteIOContext *f = s->f;
401 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
403 /* extension */
404 ext_code = get_byte(f);
405 ext_len = get_byte(f);
406 #ifdef DEBUG
407 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
408 #endif
409 switch(ext_code) {
410 case 0xf9:
411 if (ext_len != 4)
412 goto discard_ext;
413 s->transparent_color_index = -1;
414 gce_flags = get_byte(f);
415 s->gce_delay = get_le16(f);
416 gce_transparent_index = get_byte(f);
417 if (gce_flags & 0x01)
418 s->transparent_color_index = gce_transparent_index;
419 else
420 s->transparent_color_index = -1;
421 s->gce_disposal = (gce_flags >> 2) & 0x7;
422 #ifdef DEBUG
423 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
424 gce_flags, s->gce_delay,
425 s->transparent_color_index, s->gce_disposal);
426 #endif
427 ext_len = get_byte(f);
428 break;
431 /* NOTE: many extension blocks can come after */
432 discard_ext:
433 while (ext_len != 0) {
434 for (i = 0; i < ext_len; i++)
435 get_byte(f);
436 ext_len = get_byte(f);
437 #ifdef DEBUG
438 printf("gif: ext_len1=%d\n", ext_len);
439 #endif
441 return 0;
444 static int gif_read_header1(GifState *s)
446 ByteIOContext *f = s->f;
447 uint8_t sig[6];
448 int ret, v, n;
449 int has_global_palette;
451 /* read gif signature */
452 ret = get_buffer(f, sig, 6);
453 if (ret != 6)
454 return -1;
455 if (memcmp(sig, gif87a_sig, 6) != 0 &&
456 memcmp(sig, gif89a_sig, 6) != 0)
457 return -1;
459 /* read screen header */
460 s->transparent_color_index = -1;
461 s->screen_width = get_le16(f);
462 s->screen_height = get_le16(f);
463 if( (unsigned)s->screen_width > 32767
464 || (unsigned)s->screen_height > 32767){
465 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
466 return -1;
469 v = get_byte(f);
470 s->color_resolution = ((v & 0x70) >> 4) + 1;
471 has_global_palette = (v & 0x80);
472 s->bits_per_pixel = (v & 0x07) + 1;
473 s->background_color_index = get_byte(f);
474 get_byte(f); /* ignored */
475 #ifdef DEBUG
476 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
477 s->screen_width, s->screen_height, s->bits_per_pixel,
478 has_global_palette);
479 #endif
480 if (has_global_palette) {
481 n = 1 << s->bits_per_pixel;
482 get_buffer(f, s->global_palette, n * 3);
484 return 0;
487 static int gif_parse_next_image(GifState *s)
489 ByteIOContext *f = s->f;
490 int ret, code;
492 for (;;) {
493 code = url_fgetc(f);
494 #ifdef DEBUG
495 printf("gif: code=%02x '%c'\n", code, code);
496 #endif
497 switch (code) {
498 case ',':
499 if (gif_read_image(s) < 0)
500 return AVERROR(EIO);
501 ret = 0;
502 goto the_end;
503 case ';':
504 /* end of image */
505 ret = AVERROR(EIO);
506 goto the_end;
507 case '!':
508 if (gif_read_extension(s) < 0)
509 return AVERROR(EIO);
510 break;
511 case EOF:
512 default:
513 /* error or errneous EOF */
514 ret = AVERROR(EIO);
515 goto the_end;
518 the_end:
519 return ret;
522 static int gif_read_header(AVFormatContext * s1,
523 AVFormatParameters * ap)
525 GifState *s = s1->priv_data;
526 ByteIOContext *f = s1->pb;
527 AVStream *st;
529 s->f = f;
530 if (gif_read_header1(s) < 0)
531 return -1;
533 /* allocate image buffer */
534 s->image_linesize = s->screen_width * 3;
535 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
536 if (!s->image_buf)
537 return AVERROR(ENOMEM);
538 s->pix_fmt = PIX_FMT_RGB24;
539 /* now we are ready: build format streams */
540 st = av_new_stream(s1, 0);
541 if (!st)
542 return -1;
544 st->codec->codec_type = CODEC_TYPE_VIDEO;
545 st->codec->codec_id = CODEC_ID_RAWVIDEO;
546 st->codec->time_base.den = 5;
547 st->codec->time_base.num = 1;
548 /* XXX: check if screen size is always valid */
549 st->codec->width = s->screen_width;
550 st->codec->height = s->screen_height;
551 st->codec->pix_fmt = PIX_FMT_RGB24;
552 return 0;
555 static int gif_read_packet(AVFormatContext * s1,
556 AVPacket * pkt)
558 GifState *s = s1->priv_data;
559 int ret;
561 ret = gif_parse_next_image(s);
562 if (ret < 0)
563 return ret;
565 /* XXX: avoid copying */
566 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
567 return AVERROR(EIO);
569 pkt->stream_index = 0;
570 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
571 return 0;
574 static int gif_read_close(AVFormatContext *s1)
576 GifState *s = s1->priv_data;
577 av_free(s->image_buf);
578 return 0;
581 AVInputFormat gif_demuxer =
583 "gif",
584 "gif format",
585 sizeof(GifState),
586 gif_video_probe,
587 gif_read_header,
588 gif_read_packet,
589 gif_read_close,