This fixes error handling for BeOS, removing the need for some ifdefs.
[libav.git] / libavformat / gifdec.c
1 /*
2 * GIF demuxer
3 * Copyright (c) 2003 Fabrice Bellard.
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21 #include "avformat.h"
22
23 //#define DEBUG
24
25 #define MAXBITS 12
26 #define SIZTABLE (1<<MAXBITS)
27
28 #define GCE_DISPOSAL_NONE 0
29 #define GCE_DISPOSAL_INPLACE 1
30 #define GCE_DISPOSAL_BACKGROUND 2
31 #define GCE_DISPOSAL_RESTORE 3
32
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;
44
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;
49
50 /* LZW compatible decoder */
51 ByteIOContext *f;
52 int eob_reached;
53 uint8_t *pbuf, *ebuf;
54 int bbits;
55 unsigned int bbuf;
56
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];
70
71 /* aux buffers */
72 uint8_t global_palette[256 * 3];
73 uint8_t local_palette[256 * 3];
74 uint8_t buf[256];
75 } GifState;
76
77
78 static const uint8_t gif87a_sig[6] = "GIF87a";
79 static const uint8_t gif89a_sig[6] = "GIF89a";
80
81 static const uint16_t mask[17] =
82 {
83 0x0000, 0x0001, 0x0003, 0x0007,
84 0x000F, 0x001F, 0x003F, 0x007F,
85 0x00FF, 0x01FF, 0x03FF, 0x07FF,
86 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
87 };
88
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)
94 {
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;
98
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;
132 }
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++;
140 }
141 }
142 return 0;
143 }
144
145 static void GLZWDecodeInit(GifState * s, int csize)
146 {
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;
153
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;
164 }
165
166 /* XXX: optimize */
167 static inline int GetCode(GifState * s)
168 {
169 int c, sizbuf;
170 uint8_t *ptr;
171
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;
183 }
184 }
185 ptr = s->pbuf;
186 }
187 s->bbuf |= ptr[0] << s->bbits;
188 ptr++;
189 s->pbuf = ptr;
190 s->bbits += 8;
191 }
192 c = s->bbuf & s->curmask;
193 s->bbuf >>= s->cursize;
194 s->bbits -= s->cursize;
195 return c;
196 }
197
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)
202 {
203 int l, c, code, oc, fc;
204 uint8_t *sp;
205
206 if (s->end_code < 0)
207 return 0;
208
209 l = len;
210 sp = s->sp;
211 oc = s->oc;
212 fc = s->fc;
213
214 while (sp > s->stack) {
215 *buf++ = *(--sp);
216 if ((--l) == 0)
217 goto the_end;
218 }
219
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;
234 }
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;
247 }
248 while (code >= s->newcodes) {
249 *sp++ = s->suffix[code];
250 code = s->prefix[code];
251 }
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;
257 }
258 if (s->slot >= s->top_slot) {
259 if (s->cursize < MAXBITS) {
260 s->top_slot <<= 1;
261 s->curmask = mask[++s->cursize];
262 }
263 }
264 while (sp > s->stack) {
265 *buf++ = *(--sp);
266 if ((--l) == 0)
267 goto the_end;
268 }
269 }
270 }
271 the_end:
272 s->sp = sp;
273 s->oc = oc;
274 s->fc = fc;
275 return len - l;
276 }
277
278 static int gif_read_image(GifState *s)
279 {
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;
284
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
296
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;
303 }
304
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);
309
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;
322 }
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;
329 }
330
331 /* now get the image data */
332 s->f = f;
333 code_size = get_byte(f);
334 GLZWDecodeInit(s, code_size);
335
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++;
355 }
356 } else {
357 GLZWDecode(s, ptr, width);
358 }
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 = 4;
368 if (pass == 0)
369 ptr = ptr1 + linesize * 4;
370 else
371 ptr = ptr1 + linesize * 2;
372 pass++;
373 }
374 break;
375 case 2:
376 y1 += 4;
377 ptr += linesize * 4;
378 if (y1 >= height) {
379 y1 = 1;
380 ptr = ptr1 + linesize;
381 pass++;
382 }
383 break;
384 case 3:
385 y1 += 2;
386 ptr += linesize * 2;
387 break;
388 }
389 } else {
390 ptr += linesize;
391 }
392 }
393 av_free(line);
394
395 /* read the garbage data until end marker is found */
396 while (!s->eob_reached)
397 GetCode(s);
398 return 0;
399 }
400
401 static int gif_read_extension(GifState *s)
402 {
403 ByteIOContext *f = s->f;
404 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
405
406 /* extension */
407 ext_code = get_byte(f);
408 ext_len = get_byte(f);
409 #ifdef DEBUG
410 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
411 #endif
412 switch(ext_code) {
413 case 0xf9:
414 if (ext_len != 4)
415 goto discard_ext;
416 s->transparent_color_index = -1;
417 gce_flags = get_byte(f);
418 s->gce_delay = get_le16(f);
419 gce_transparent_index = get_byte(f);
420 if (gce_flags & 0x01)
421 s->transparent_color_index = gce_transparent_index;
422 else
423 s->transparent_color_index = -1;
424 s->gce_disposal = (gce_flags >> 2) & 0x7;
425 #ifdef DEBUG
426 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
427 gce_flags, s->gce_delay,
428 s->transparent_color_index, s->gce_disposal);
429 #endif
430 ext_len = get_byte(f);
431 break;
432 }
433
434 /* NOTE: many extension blocks can come after */
435 discard_ext:
436 while (ext_len != 0) {
437 for (i = 0; i < ext_len; i++)
438 get_byte(f);
439 ext_len = get_byte(f);
440 #ifdef DEBUG
441 printf("gif: ext_len1=%d\n", ext_len);
442 #endif
443 }
444 return 0;
445 }
446
447 static int gif_read_header1(GifState *s)
448 {
449 ByteIOContext *f = s->f;
450 uint8_t sig[6];
451 int ret, v, n;
452 int has_global_palette;
453
454 /* read gif signature */
455 ret = get_buffer(f, sig, 6);
456 if (ret != 6)
457 return -1;
458 if (memcmp(sig, gif87a_sig, 6) != 0 &&
459 memcmp(sig, gif89a_sig, 6) != 0)
460 return -1;
461
462 /* read screen header */
463 s->transparent_color_index = -1;
464 s->screen_width = get_le16(f);
465 s->screen_height = get_le16(f);
466 if( (unsigned)s->screen_width > 32767
467 || (unsigned)s->screen_height > 32767){
468 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
469 return -1;
470 }
471
472 v = get_byte(f);
473 s->color_resolution = ((v & 0x70) >> 4) + 1;
474 has_global_palette = (v & 0x80);
475 s->bits_per_pixel = (v & 0x07) + 1;
476 s->background_color_index = get_byte(f);
477 get_byte(f); /* ignored */
478 #ifdef DEBUG
479 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
480 s->screen_width, s->screen_height, s->bits_per_pixel,
481 has_global_palette);
482 #endif
483 if (has_global_palette) {
484 n = 1 << s->bits_per_pixel;
485 get_buffer(f, s->global_palette, n * 3);
486 }
487 return 0;
488 }
489
490 static int gif_parse_next_image(GifState *s)
491 {
492 ByteIOContext *f = s->f;
493 int ret, code;
494
495 for (;;) {
496 code = url_fgetc(f);
497 #ifdef DEBUG
498 printf("gif: code=%02x '%c'\n", code, code);
499 #endif
500 switch (code) {
501 case ',':
502 if (gif_read_image(s) < 0)
503 return AVERROR_IO;
504 ret = 0;
505 goto the_end;
506 case ';':
507 /* end of image */
508 ret = AVERROR_IO;
509 goto the_end;
510 case '!':
511 if (gif_read_extension(s) < 0)
512 return AVERROR_IO;
513 break;
514 case EOF:
515 default:
516 /* error or errneous EOF */
517 ret = AVERROR_IO;
518 goto the_end;
519 }
520 }
521 the_end:
522 return ret;
523 }
524
525 static int gif_read_header(AVFormatContext * s1,
526 AVFormatParameters * ap)
527 {
528 GifState *s = s1->priv_data;
529 ByteIOContext *f = &s1->pb;
530 AVStream *st;
531
532 s->f = f;
533 if (gif_read_header1(s) < 0)
534 return -1;
535
536 /* allocate image buffer */
537 s->image_linesize = s->screen_width * 3;
538 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
539 if (!s->image_buf)
540 return AVERROR(ENOMEM);
541 s->pix_fmt = PIX_FMT_RGB24;
542 /* now we are ready: build format streams */
543 st = av_new_stream(s1, 0);
544 if (!st)
545 return -1;
546
547 st->codec->codec_type = CODEC_TYPE_VIDEO;
548 st->codec->codec_id = CODEC_ID_RAWVIDEO;
549 st->codec->time_base.den = 5;
550 st->codec->time_base.num = 1;
551 /* XXX: check if screen size is always valid */
552 st->codec->width = s->screen_width;
553 st->codec->height = s->screen_height;
554 st->codec->pix_fmt = PIX_FMT_RGB24;
555 return 0;
556 }
557
558 static int gif_read_packet(AVFormatContext * s1,
559 AVPacket * pkt)
560 {
561 GifState *s = s1->priv_data;
562 int ret;
563
564 ret = gif_parse_next_image(s);
565 if (ret < 0)
566 return ret;
567
568 /* XXX: avoid copying */
569 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
570 return AVERROR_IO;
571 }
572 pkt->stream_index = 0;
573 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
574 return 0;
575 }
576
577 static int gif_read_close(AVFormatContext *s1)
578 {
579 GifState *s = s1->priv_data;
580 av_free(s->image_buf);
581 return 0;
582 }
583
584 AVInputFormat gif_demuxer =
585 {
586 "gif",
587 "gif format",
588 sizeof(GifState),
589 gif_video_probe,
590 gif_read_header,
591 gif_read_packet,
592 gif_read_close,
593 };