annotate gifdec.c @ 483:f4e9e55f703c libavformat

cvs diff before cvs commit
author melanson
date Sat, 19 Jun 2004 14:51:46 +0000
parents 0fdc96c2f2fe
children 0b52743104ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
1 /*
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
2 * GIF decoder
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
4 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
8 * version 2 of the License, or (at your option) any later version.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
9 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
13 * Lesser General Public License for more details.
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
14 *
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
18 */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
19 #include "avformat.h"
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
20
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
21 int gif_write(ByteIOContext *pb, AVImageInfo *info);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
22
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
23 //#define DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
24
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
25 #define MAXBITS 12
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
26 #define SIZTABLE (1<<MAXBITS)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
27
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
28 #define GCE_DISPOSAL_NONE 0
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
29 #define GCE_DISPOSAL_INPLACE 1
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
30 #define GCE_DISPOSAL_BACKGROUND 2
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
31 #define GCE_DISPOSAL_RESTORE 3
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
32
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
33 typedef struct GifState {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
34 int screen_width;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
35 int screen_height;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
36 int bits_per_pixel;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
37 int background_color_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
38 int transparent_color_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
39 int color_resolution;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
40 uint8_t *image_buf;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
41 int image_linesize;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
42 uint32_t *image_palette;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
43 int pix_fmt;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
44
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
45 /* after the frame is displayed, the disposal method is used */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
46 int gce_disposal;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
47 /* delay during which the frame is shown */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
48 int gce_delay;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
49
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
50 /* LZW compatible decoder */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
51 ByteIOContext *f;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
52 int eob_reached;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
53 uint8_t *pbuf, *ebuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
54 int bbits;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
55 unsigned int bbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
56
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
57 int cursize; /* The current code size */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
58 int curmask;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
59 int codesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
60 int clear_code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
61 int end_code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
62 int newcodes; /* First available code */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
63 int top_slot; /* Highest code for current size */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
64 int slot; /* Last read code */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
65 int fc, oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
66 uint8_t *sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
67 uint8_t stack[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
68 uint8_t suffix[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
69 uint16_t prefix[SIZTABLE];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
70
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
71 /* aux buffers */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
72 uint8_t global_palette[256 * 3];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
73 uint8_t local_palette[256 * 3];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
74 uint8_t buf[256];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
75 } GifState;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
76
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
77
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
78 static const uint8_t gif87a_sig[6] = "GIF87a";
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
79 static const uint8_t gif89a_sig[6] = "GIF89a";
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
80
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
81 static const uint16_t mask[17] =
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
82 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
83 0x0000, 0x0001, 0x0003, 0x0007,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
84 0x000F, 0x001F, 0x003F, 0x007F,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
85 0x00FF, 0x01FF, 0x03FF, 0x07FF,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
86 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
87 };
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
88
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
89 /* Probe gif video format or gif image format. The current heuristic
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
90 supposes the gif87a is always a single image. For gif89a, we
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
91 consider it as a video only if a GCE extension is present in the
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
92 first kilobyte. */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
93 static int gif_video_probe(AVProbeData * pd)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
94 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
95 const uint8_t *p, *p_end;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
96 int bits_per_pixel, has_global_palette, ext_code, ext_len;
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
97 int gce_flags, gce_disposal;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
98
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
99 if (pd->buf_size < 24 ||
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
100 memcmp(pd->buf, gif89a_sig, 6) != 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
101 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
102 p_end = pd->buf + pd->buf_size;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
103 p = pd->buf + 6;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
104 bits_per_pixel = (p[4] & 0x07) + 1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
105 has_global_palette = (p[4] & 0x80);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
106 p += 7;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
107 if (has_global_palette)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
108 p += (1 << bits_per_pixel) * 3;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
109 for(;;) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
110 if (p >= p_end)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
111 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
112 if (*p != '!')
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
113 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
114 p++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
115 if (p >= p_end)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
116 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
117 ext_code = *p++;
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
118 if (p >= p_end)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
119 return 0;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
120 ext_len = *p++;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
121 if (ext_code == 0xf9) {
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
122 if (p >= p_end)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
123 return 0;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
124 /* if GCE extension found with gce_disposal != 0: it is
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
125 likely to be an animation */
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
126 gce_flags = *p++;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
127 gce_disposal = (gce_flags >> 2) & 0x7;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
128 if (gce_disposal != 0)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
129 return AVPROBE_SCORE_MAX;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
130 else
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
131 return 0;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
132 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
133 for(;;) {
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
134 if (ext_len == 0)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
135 break;
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
136 p += ext_len;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
137 if (p >= p_end)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
138 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
139 ext_len = *p++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
140 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
141 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
142 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
143 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
144
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
145 static int gif_image_probe(AVProbeData * pd)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
146 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
147 if (pd->buf_size >= 24 &&
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
148 (memcmp(pd->buf, gif87a_sig, 6) == 0 ||
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
149 memcmp(pd->buf, gif89a_sig, 6) == 0))
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
150 return AVPROBE_SCORE_MAX - 1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
151 else
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
152 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
153 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
154
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
155
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
156 static void GLZWDecodeInit(GifState * s, int csize)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
157 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
158 /* read buffer */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
159 s->eob_reached = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
160 s->pbuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
161 s->ebuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
162 s->bbuf = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
163 s->bbits = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
164
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
165 /* decoder */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
166 s->codesize = csize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
167 s->cursize = s->codesize + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
168 s->curmask = mask[s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
169 s->top_slot = 1 << s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
170 s->clear_code = 1 << s->codesize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
171 s->end_code = s->clear_code + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
172 s->slot = s->newcodes = s->clear_code + 2;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
173 s->oc = s->fc = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
174 s->sp = s->stack;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
175 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
176
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
177 /* XXX: optimize */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
178 static inline int GetCode(GifState * s)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
179 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
180 int c, sizbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
181 uint8_t *ptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
182
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
183 while (s->bbits < s->cursize) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
184 ptr = s->pbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
185 if (ptr >= s->ebuf) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
186 if (!s->eob_reached) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
187 sizbuf = get_byte(s->f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
188 s->ebuf = s->buf + sizbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
189 s->pbuf = s->buf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
190 if (sizbuf > 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
191 get_buffer(s->f, s->buf, sizbuf);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
192 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
193 s->eob_reached = 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
194 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
195 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
196 ptr = s->pbuf;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
197 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
198 s->bbuf |= ptr[0] << s->bbits;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
199 ptr++;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
200 s->pbuf = ptr;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
201 s->bbits += 8;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
202 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
203 c = s->bbuf & s->curmask;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
204 s->bbuf >>= s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
205 s->bbits -= s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
206 return c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
207 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
208
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
209 /* NOTE: the algorithm here is inspired from the LZW GIF decoder
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
210 written by Steven A. Bennett in 1987. */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
211 /* return the number of byte decoded */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
212 static int GLZWDecode(GifState * s, uint8_t * buf, int len)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
213 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
214 int l, c, code, oc, fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
215 uint8_t *sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
216
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
217 if (s->end_code < 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
218 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
219
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
220 l = len;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
221 sp = s->sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
222 oc = s->oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
223 fc = s->fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
224
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
225 while (sp > s->stack) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
226 *buf++ = *(--sp);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
227 if ((--l) == 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
228 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
229 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
230
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
231 for (;;) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
232 c = GetCode(s);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
233 if (c == s->end_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
234 s->end_code = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
235 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
236 } else if (c == s->clear_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
237 s->cursize = s->codesize + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
238 s->curmask = mask[s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
239 s->slot = s->newcodes;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
240 s->top_slot = 1 << s->cursize;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
241 while ((c = GetCode(s)) == s->clear_code);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
242 if (c == s->end_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
243 s->end_code = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
244 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
245 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
246 /* test error */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
247 if (c >= s->slot)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
248 c = 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
249 fc = oc = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
250 *buf++ = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
251 if ((--l) == 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
252 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
253 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
254 code = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
255 if (code >= s->slot) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
256 *sp++ = fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
257 code = oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
258 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
259 while (code >= s->newcodes) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
260 *sp++ = s->suffix[code];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
261 code = s->prefix[code];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
262 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
263 *sp++ = code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
264 if (s->slot < s->top_slot) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
265 s->suffix[s->slot] = fc = code;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
266 s->prefix[s->slot++] = oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
267 oc = c;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
268 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
269 if (s->slot >= s->top_slot) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
270 if (s->cursize < MAXBITS) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
271 s->top_slot <<= 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
272 s->curmask = mask[++s->cursize];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
273 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
274 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
275 while (sp > s->stack) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
276 *buf++ = *(--sp);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
277 if ((--l) == 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
278 goto the_end;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
279 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
280 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
281 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
282 the_end:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
283 s->sp = sp;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
284 s->oc = oc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
285 s->fc = fc;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
286 return len - l;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
287 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
288
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
289 static int gif_read_image(GifState *s)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
290 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
291 ByteIOContext *f = s->f;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
292 int left, top, width, height, bits_per_pixel, code_size, flags;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
293 int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
294 uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
295
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
296 left = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
297 top = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
298 width = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
299 height = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
300 flags = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
301 is_interleaved = flags & 0x40;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
302 has_local_palette = flags & 0x80;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
303 bits_per_pixel = (flags & 0x07) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
304 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
305 printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
306 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
307
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
308 if (has_local_palette) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
309 get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
310 palette = s->local_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
311 } else {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
312 palette = s->global_palette;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
313 bits_per_pixel = s->bits_per_pixel;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
314 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
315
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
316 /* verify that all the image is inside the screen dimensions */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
317 if (left + width > s->screen_width ||
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
318 top + height > s->screen_height)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
319 return -EINVAL;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
320
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
321 /* build the palette */
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
322 if (s->pix_fmt == PIX_FMT_RGB24) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
323 line = av_malloc(width);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
324 if (!line)
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
325 return -ENOMEM;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
326 } else {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
327 n = (1 << bits_per_pixel);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
328 spal = palette;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
329 for(i = 0; i < n; i++) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
330 s->image_palette[i] = (0xff << 24) |
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
331 (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
332 spal += 3;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
333 }
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
334 for(; i < 256; i++)
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
335 s->image_palette[i] = (0xff << 24);
116
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
336 /* handle transparency */
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
337 if (s->transparent_color_index >= 0)
6026a7f8ff78 transparency support
bellard
parents: 85
diff changeset
338 s->image_palette[s->transparent_color_index] = 0;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
339 line = NULL;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
340 }
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
341
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
342 /* now get the image data */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
343 s->f = f;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
344 code_size = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
345 GLZWDecodeInit(s, code_size);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
346
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
347 /* read all the image */
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
348 linesize = s->image_linesize;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
349 ptr1 = s->image_buf + top * linesize + (left * 3);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
350 ptr = ptr1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
351 pass = 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
352 y1 = 0;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
353 for (y = 0; y < height; y++) {
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
354 if (s->pix_fmt == PIX_FMT_RGB24) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
355 /* transcode to RGB24 */
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
356 GLZWDecode(s, line, width);
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
357 d = ptr;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
358 sptr = line;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
359 for(x = 0; x < width; x++) {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
360 spal = palette + sptr[0] * 3;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
361 d[0] = spal[0];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
362 d[1] = spal[1];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
363 d[2] = spal[2];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
364 d += 3;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
365 sptr++;
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
366 }
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
367 } else {
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
368 GLZWDecode(s, ptr, width);
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
369 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
370 if (is_interleaved) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
371 switch(pass) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
372 default:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
373 case 0:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
374 case 1:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
375 y1 += 8;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
376 ptr += linesize * 8;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
377 if (y1 >= height) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
378 y1 = 4;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
379 if (pass == 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
380 ptr = ptr1 + linesize * 4;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
381 else
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
382 ptr = ptr1 + linesize * 2;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
383 pass++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
384 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
385 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
386 case 2:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
387 y1 += 4;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
388 ptr += linesize * 4;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
389 if (y1 >= height) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
390 y1 = 1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
391 ptr = ptr1 + linesize;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
392 pass++;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
393 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
394 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
395 case 3:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
396 y1 += 2;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
397 ptr += linesize * 2;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
398 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
399 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
400 } else {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
401 ptr += linesize;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
402 }
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
403 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
404 av_free(line);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
405
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
406 /* read the garbage data until end marker is found */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
407 while (!s->eob_reached)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
408 GetCode(s);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
409 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
410 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
411
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
412 static int gif_read_extension(GifState *s)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
413 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
414 ByteIOContext *f = s->f;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
415 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
416
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
417 /* extension */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
418 ext_code = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
419 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
420 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
421 printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
422 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
423 switch(ext_code) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
424 case 0xf9:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
425 if (ext_len != 4)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
426 goto discard_ext;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
427 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
428 gce_flags = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
429 s->gce_delay = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
430 gce_transparent_index = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
431 if (gce_flags & 0x01)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
432 s->transparent_color_index = gce_transparent_index;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
433 else
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
434 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
435 s->gce_disposal = (gce_flags >> 2) & 0x7;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
436 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
437 printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
438 gce_flags, s->gce_delay,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
439 s->transparent_color_index, s->gce_disposal);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
440 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
441 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
442 break;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
443 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
444
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
445 /* NOTE: many extension blocks can come after */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
446 discard_ext:
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
447 while (ext_len != 0) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
448 for (i = 0; i < ext_len; i++)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
449 get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
450 ext_len = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
451 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
452 printf("gif: ext_len1=%d\n", ext_len);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
453 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
454 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
455 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
456 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
457
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
458 static int gif_read_header1(GifState *s)
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
459 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
460 ByteIOContext *f = s->f;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
461 uint8_t sig[6];
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
462 int ret, v, n;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
463 int has_global_palette;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
464
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
465 /* read gif signature */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
466 ret = get_buffer(f, sig, 6);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
467 if (ret != 6)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
468 return -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
469 if (memcmp(sig, gif87a_sig, 6) != 0 &&
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
470 memcmp(sig, gif89a_sig, 6) != 0)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
471 return -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
472
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
473 /* read screen header */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
474 s->transparent_color_index = -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
475 s->screen_width = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
476 s->screen_height = get_le16(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
477 v = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
478 s->color_resolution = ((v & 0x70) >> 4) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
479 has_global_palette = (v & 0x80);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
480 s->bits_per_pixel = (v & 0x07) + 1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
481 s->background_color_index = get_byte(f);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
482 get_byte(f); /* ignored */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
483 #ifdef DEBUG
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
484 printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
485 s->screen_width, s->screen_height, s->bits_per_pixel,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
486 has_global_palette);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
487 #endif
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
488 if (has_global_palette) {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
489 n = 1 << s->bits_per_pixel;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
490 get_buffer(f, s->global_palette, n * 3);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
491 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
492 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
493 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
494
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
495 static int gif_parse_next_image(GifState *s)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
496 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
497 ByteIOContext *f = s->f;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
498 int ret, code;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
499
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
500 for (;;) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
501 code = url_fgetc(f);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
502 #ifdef DEBUG
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
503 printf("gif: code=%02x '%c'\n", code, code);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
504 #endif
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
505 switch (code) {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
506 case ',':
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
507 if (gif_read_image(s) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 116
diff changeset
508 return AVERROR_IO;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
509 ret = 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
510 goto the_end;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
511 case ';':
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
512 /* end of image */
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 116
diff changeset
513 ret = AVERROR_IO;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
514 goto the_end;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
515 case '!':
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
516 if (gif_read_extension(s) < 0)
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 116
diff changeset
517 return AVERROR_IO;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
518 break;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
519 case EOF:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
520 default:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
521 /* error or errneous EOF */
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 116
diff changeset
522 ret = AVERROR_IO;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
523 goto the_end;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
524 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
525 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
526 the_end:
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
527 return ret;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
528 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
529
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
530 static int gif_read_header(AVFormatContext * s1,
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
531 AVFormatParameters * ap)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
532 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
533 GifState *s = s1->priv_data;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
534 ByteIOContext *f = &s1->pb;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
535 AVStream *st;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
536
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
537 s->f = f;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
538 if (gif_read_header1(s) < 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
539 return -1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
540
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
541 /* allocate image buffer */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
542 s->image_linesize = s->screen_width * 3;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
543 s->image_buf = av_malloc(s->screen_height * s->image_linesize);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
544 if (!s->image_buf)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
545 return -ENOMEM;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
546 s->pix_fmt = PIX_FMT_RGB24;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
547 /* now we are ready: build format streams */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
548 st = av_new_stream(s1, 0);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
549 if (!st)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
550 return -1;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
551
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
552 st->codec.codec_type = CODEC_TYPE_VIDEO;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
553 st->codec.codec_id = CODEC_ID_RAWVIDEO;
85
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 60
diff changeset
554 st->codec.frame_rate = 5;
25062c9b1f86 per context frame_rate_base, this should finally fix frame_rate related av sync issues
michaelni
parents: 60
diff changeset
555 st->codec.frame_rate_base = 1;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
556 /* XXX: check if screen size is always valid */
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
557 st->codec.width = s->screen_width;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
558 st->codec.height = s->screen_height;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
559 st->codec.pix_fmt = PIX_FMT_RGB24;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
560 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
561 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
562
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
563 static int gif_read_packet(AVFormatContext * s1,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
564 AVPacket * pkt)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
565 {
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
566 GifState *s = s1->priv_data;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
567 int ret;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
568
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
569 ret = gif_parse_next_image(s);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
570 if (ret < 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
571 return ret;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
572
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
573 /* XXX: avoid copying */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
574 if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
482
0fdc96c2f2fe sweeping change from -EIO -> AVERROR_IO
melanson
parents: 116
diff changeset
575 return AVERROR_IO;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
576 }
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
577 pkt->stream_index = 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
578 memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
579 return 0;
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
580 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
581
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
582 static int gif_read_close(AVFormatContext *s1)
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
583 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
584 GifState *s = s1->priv_data;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
585 av_free(s->image_buf);
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
586 return 0;
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
587 }
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
588
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
589 /* read gif as image */
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
590 static int gif_read(ByteIOContext *f,
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
591 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
592 {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
593 GifState s1, *s = &s1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
594 AVImageInfo info1, *info = &info1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
595 int ret;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
596
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
597 memset(s, 0, sizeof(GifState));
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
598 s->f = f;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
599 if (gif_read_header1(s) < 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
600 return -1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
601 info->width = s->screen_width;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
602 info->height = s->screen_height;
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
603 info->pix_fmt = PIX_FMT_PAL8;
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
604 ret = alloc_cb(opaque, info);
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
605 if (ret)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
606 return ret;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
607 s->image_buf = info->pict.data[0];
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
608 s->image_linesize = info->pict.linesize[0];
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
609 s->image_palette = (uint32_t *)info->pict.data[1];
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
610
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
611 if (gif_parse_next_image(s) < 0)
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
612 return -1;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
613 return 0;
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
614 }
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
615
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
616 AVInputFormat gif_iformat =
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
617 {
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
618 "gif",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
619 "gif format",
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
620 sizeof(GifState),
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
621 gif_video_probe,
46
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
622 gif_read_header,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
623 gif_read_packet,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
624 gif_read_close,
890e9121a54d added animated GIF decoder (pts and various disposal handling are missing)
bellard
parents:
diff changeset
625 };
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
626
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
627 AVImageFormat gif_image_format = {
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
628 "gif",
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
629 "gif",
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
630 gif_image_probe,
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
631 gif_read,
60
c89672a5f24e added 8 bit palette support for non animated GIF
bellard
parents: 50
diff changeset
632 (1 << PIX_FMT_PAL8),
50
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
633 gif_write,
658be43f6a0d added automatic GIF/animated GIF probing - added GIF as an image format too - added interlaced gif support
bellard
parents: 46
diff changeset
634 };