annotate gifdec.c @ 1432:4f4bebc36aad libavformat

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