annotate gifdec.c @ 12460:7106d2c8f142 libavcodec

Fix vp3_draw_horiz_band to not produce completely chaotical values that result in overdrawing areas again and again if s->flipped_image is false.
author reimar
date Mon, 06 Sep 2010 19:21:13 +0000
parents 914f484bb476
children ffb3668ff7af
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
1 /*
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
2 * GIF decoder
8629
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 7040
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard
04423b2f6e0b cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 7040
diff changeset
4 * Copyright (c) 2006 Baptiste Coudurier
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
5 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
6 * This file is part of FFmpeg.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
7 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
12 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
16 * Lesser General Public License for more details.
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
17 *
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
21 */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
22
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
23 //#define DEBUG
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
24
12372
914f484bb476 Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents: 11560
diff changeset
25 #include "libavcore/imgutils.h"
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
26 #include "avcodec.h"
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
27 #include "bytestream.h"
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
28 #include "lzw.h"
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
29
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
30 #define GCE_DISPOSAL_NONE 0
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
31 #define GCE_DISPOSAL_INPLACE 1
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
32 #define GCE_DISPOSAL_BACKGROUND 2
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
33 #define GCE_DISPOSAL_RESTORE 3
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
34
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
35 typedef struct GifState {
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
36 AVFrame picture;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
37 int screen_width;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
38 int screen_height;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
39 int bits_per_pixel;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
40 int background_color_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
41 int transparent_color_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
42 int color_resolution;
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
43 uint32_t *image_palette;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
44
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
45 /* after the frame is displayed, the disposal method is used */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
46 int gce_disposal;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
47 /* delay during which the frame is shown */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
48 int gce_delay;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
49
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
50 /* LZW compatible decoder */
6218
michael
parents: 5089
diff changeset
51 const uint8_t *bytestream;
michael
parents: 5089
diff changeset
52 const uint8_t *bytestream_end;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
53 LZWState *lzw;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
54
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
55 /* aux buffers */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
56 uint8_t global_palette[256 * 3];
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
57 uint8_t local_palette[256 * 3];
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
58
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
59 AVCodecContext* avctx;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
60 } GifState;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
61
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
62 static const uint8_t gif87a_sig[6] = "GIF87a";
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
63 static const uint8_t gif89a_sig[6] = "GIF89a";
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
64
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
65 static int gif_read_image(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
66 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
67 int left, top, width, height, bits_per_pixel, code_size, flags;
4057
ba767c63a07f remove unused variables
bcoudurier
parents: 4056
diff changeset
68 int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
4144
dc52a253f51b remove useless line variable
bcoudurier
parents: 4143
diff changeset
69 uint8_t *ptr, *spal, *palette, *ptr1;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
70
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
71 left = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
72 top = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
73 width = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
74 height = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
75 flags = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
76 is_interleaved = flags & 0x40;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
77 has_local_palette = flags & 0x80;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
78 bits_per_pixel = (flags & 0x07) + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
79 #ifdef DEBUG
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
80 dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
81 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
82
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
83 if (has_local_palette) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
84 bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
85 palette = s->local_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
86 } else {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
87 palette = s->global_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
88 bits_per_pixel = s->bits_per_pixel;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
89 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
90
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
91 /* verify that all the image is inside the screen dimensions */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
92 if (left + width > s->screen_width ||
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
93 top + height > s->screen_height)
4520
9bf957e669f0 This fixes error handling for BeOS, removing the need for some ifdefs.
mmu_man
parents: 4144
diff changeset
94 return AVERROR(EINVAL);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
95
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
96 /* build the palette */
4060
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
97 n = (1 << bits_per_pixel);
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
98 spal = palette;
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
99 for(i = 0; i < n; i++) {
5089
bff60ecc02f9 Use AV_xx throughout libavcodec
ramiro
parents: 4874
diff changeset
100 s->image_palette[i] = (0xff << 24) | AV_RB24(spal);
4060
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
101 spal += 3;
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
102 }
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
103 for(; i < 256; i++)
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
104 s->image_palette[i] = (0xff << 24);
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
105 /* handle transparency */
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
106 if (s->transparent_color_index >= 0)
e46fa0f9192a indention
bcoudurier
parents: 4059
diff changeset
107 s->image_palette[s->transparent_color_index] = 0;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
108
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
109 /* now get the image data */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
110 code_size = bytestream_get_byte(&s->bytestream);
4729
8342af7feb90 pass correct buffer size to lzw decode init
bcoudurier
parents: 4720
diff changeset
111 ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
8342af7feb90 pass correct buffer size to lzw decode init
bcoudurier
parents: 4720
diff changeset
112 s->bytestream_end - s->bytestream, FF_LZW_GIF);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
113
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
114 /* read all the image */
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
115 linesize = s->picture.linesize[0];
4143
a8e3a116b41f fix decoding position
bcoudurier
parents: 4080
diff changeset
116 ptr1 = s->picture.data[0] + top * linesize + left;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
117 ptr = ptr1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
118 pass = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
119 y1 = 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
120 for (y = 0; y < height; y++) {
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
121 ff_lzw_decode(s->lzw, ptr, width);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
122 if (is_interleaved) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
123 switch(pass) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
124 default:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
125 case 0:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
126 case 1:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
127 y1 += 8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
128 ptr += linesize * 8;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
129 if (y1 >= height) {
6462
745a4216b352 fix interlaced decoding, original patch from reimar
bcoudurier
parents: 6218
diff changeset
130 y1 = pass ? 2 : 4;
745a4216b352 fix interlaced decoding, original patch from reimar
bcoudurier
parents: 6218
diff changeset
131 ptr = ptr1 + linesize * y1;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
132 pass++;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
133 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
134 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
135 case 2:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
136 y1 += 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
137 ptr += linesize * 4;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
138 if (y1 >= height) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
139 y1 = 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
140 ptr = ptr1 + linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
141 pass++;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
142 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
143 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
144 case 3:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
145 y1 += 2;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
146 ptr += linesize * 2;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
147 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
148 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
149 } else {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
150 ptr += linesize;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
151 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
152 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
153 /* read the garbage data until end marker is found */
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
154 ff_lzw_decode_tail(s->lzw);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
155 s->bytestream = ff_lzw_cur_ptr(s->lzw);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
156 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
157 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
158
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
159 static int gif_read_extension(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
160 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
161 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
162
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
163 /* extension */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
164 ext_code = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
165 ext_len = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
166 #ifdef DEBUG
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
167 dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
168 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
169 switch(ext_code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
170 case 0xf9:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
171 if (ext_len != 4)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
172 goto discard_ext;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
173 s->transparent_color_index = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
174 gce_flags = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
175 s->gce_delay = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
176 gce_transparent_index = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
177 if (gce_flags & 0x01)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
178 s->transparent_color_index = gce_transparent_index;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
179 else
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
180 s->transparent_color_index = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
181 s->gce_disposal = (gce_flags >> 2) & 0x7;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
182 #ifdef DEBUG
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
183 dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
184 gce_flags, s->gce_delay,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
185 s->transparent_color_index, s->gce_disposal);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
186 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
187 ext_len = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
188 break;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
189 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
190
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
191 /* NOTE: many extension blocks can come after */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
192 discard_ext:
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
193 while (ext_len != 0) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
194 for (i = 0; i < ext_len; i++)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
195 bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
196 ext_len = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
197 #ifdef DEBUG
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
198 dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
199 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
200 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
201 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
202 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
203
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
204 static int gif_read_header1(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
205 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
206 uint8_t sig[6];
4057
ba767c63a07f remove unused variables
bcoudurier
parents: 4056
diff changeset
207 int v, n;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
208 int has_global_palette;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
209
4718
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
210 if (s->bytestream_end < s->bytestream + 13)
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
211 return -1;
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
212
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
213 /* read gif signature */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
214 bytestream_get_buffer(&s->bytestream, sig, 6);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
215 if (memcmp(sig, gif87a_sig, 6) != 0 &&
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
216 memcmp(sig, gif89a_sig, 6) != 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
217 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
218
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
219 /* read screen header */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
220 s->transparent_color_index = -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
221 s->screen_width = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
222 s->screen_height = bytestream_get_le16(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
223 if( (unsigned)s->screen_width > 32767
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
224 || (unsigned)s->screen_height > 32767){
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
225 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
226 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
227 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
228
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
229 v = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
230 s->color_resolution = ((v & 0x70) >> 4) + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
231 has_global_palette = (v & 0x80);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
232 s->bits_per_pixel = (v & 0x07) + 1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
233 s->background_color_index = bytestream_get_byte(&s->bytestream);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
234 bytestream_get_byte(&s->bytestream); /* ignored */
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
235 #ifdef DEBUG
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
236 dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
237 s->screen_width, s->screen_height, s->bits_per_pixel,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
238 has_global_palette);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
239 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
240 if (has_global_palette) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
241 n = 1 << s->bits_per_pixel;
4718
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
242 if (s->bytestream_end < s->bytestream + n * 3)
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
243 return -1;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
244 bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
245 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
246 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
247 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
248
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
249 static int gif_parse_next_image(GifState *s)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
250 {
4718
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
251 while (s->bytestream < s->bytestream_end) {
4717
dd9ad0da0a51 simplify
bcoudurier
parents: 4652
diff changeset
252 int code = bytestream_get_byte(&s->bytestream);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
253 #ifdef DEBUG
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
254 dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
255 #endif
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
256 switch (code) {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
257 case ',':
4874
f06463413858 simplify, patch by Mark Cox, melbournemark plus ffmpeg minus devel gmail com
bcoudurier
parents: 4868
diff changeset
258 return gif_read_image(s);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
259 case '!':
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
260 if (gif_read_extension(s) < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
261 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
262 break;
4874
f06463413858 simplify, patch by Mark Cox, melbournemark plus ffmpeg minus devel gmail com
bcoudurier
parents: 4868
diff changeset
263 case ';':
f06463413858 simplify, patch by Mark Cox, melbournemark plus ffmpeg minus devel gmail com
bcoudurier
parents: 4868
diff changeset
264 /* end of image */
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
265 default:
4868
diego
parents: 4730
diff changeset
266 /* error or erroneous EOF */
4717
dd9ad0da0a51 simplify
bcoudurier
parents: 4652
diff changeset
267 return -1;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
268 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
269 }
4720
7775ba23c931 return error if loop has ended before decoding image
bcoudurier
parents: 4719
diff changeset
270 return -1;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
271 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
272
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6462
diff changeset
273 static av_cold int gif_decode_init(AVCodecContext *avctx)
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
274 {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
275 GifState *s = avctx->priv_data;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
276
4652
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
277 s->avctx = avctx;
6679d37a3338 Give context to dprintf
mbardiaux
parents: 4520
diff changeset
278
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
279 avcodec_get_frame_defaults(&s->picture);
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
280 avctx->coded_frame= &s->picture;
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
281 s->picture.data[0] = NULL;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
282 ff_lzw_decode_open(&s->lzw);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
283 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
284 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
285
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8629
diff changeset
286 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
287 {
9355
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8629
diff changeset
288 const uint8_t *buf = avpkt->data;
54bc8a2727b0 Implement avcodec_decode_video2(), _audio3() and _subtitle2() which takes an
rbultje
parents: 8629
diff changeset
289 int buf_size = avpkt->size;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
290 GifState *s = avctx->priv_data;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
291 AVFrame *picture = data;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
292 int ret;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
293
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
294 s->bytestream = buf;
4718
9962405d2d18 add some length checks
bcoudurier
parents: 4717
diff changeset
295 s->bytestream_end = buf + buf_size;
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
296 if (gif_read_header1(s) < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
297 return -1;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
298
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
299 avctx->pix_fmt = PIX_FMT_PAL8;
12372
914f484bb476 Remove use of the deprecated function avcodec_check_dimensions(), use
stefano
parents: 11560
diff changeset
300 if (av_check_image_size(s->screen_width, s->screen_height, 0, avctx))
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
301 return -1;
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
302 avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
303
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
304 if (s->picture.data[0])
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
305 avctx->release_buffer(avctx, &s->picture);
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
306 if (avctx->get_buffer(avctx, &s->picture) < 0) {
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
307 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
308 return -1;
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
309 }
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
310 s->image_palette = (uint32_t *)s->picture.data[1];
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
311 ret = gif_parse_next_image(s);
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
312 if (ret < 0)
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
313 return ret;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
314
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
315 *picture = s->picture;
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
316 *data_size = sizeof(AVPicture);
4719
524ef62965bf actually return bytes read
bcoudurier
parents: 4718
diff changeset
317 return s->bytestream - buf;
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
318 }
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
319
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 6462
diff changeset
320 static av_cold int gif_decode_close(AVCodecContext *avctx)
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
321 {
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
322 GifState *s = avctx->priv_data;
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
323
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents: 4060
diff changeset
324 ff_lzw_decode_close(&s->lzw);
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
325 if(s->picture.data[0])
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
326 avctx->release_buffer(avctx, &s->picture);
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
327 return 0;
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
328 }
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
329
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
330 AVCodec gif_decoder = {
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
331 "gif",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 9734
diff changeset
332 AVMEDIA_TYPE_VIDEO,
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
333 CODEC_ID_GIF,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
334 sizeof(GifState),
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
335 gif_decode_init,
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
336 NULL,
4058
7f976c7cce97 use avctx->get_buffer
bcoudurier
parents: 4057
diff changeset
337 gif_decode_close,
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
338 gif_decode_frame,
9734
a7a5ffa5774b The GIF decoder uses get_buffer, so set DR1.
bcoudurier
parents: 9355
diff changeset
339 CODEC_CAP_DR1,
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6712
diff changeset
340 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
4054
f20c7bb12103 change gif demuxer to gif decoder
bcoudurier
parents:
diff changeset
341 };