aarch64: Add assembly support for -fsanitize=hwaddress tagged globals.
[libav.git] / libavcodec / gifdec.c
blobf08d5011132f2768e50699135af2c6b3295841a8
1 /*
2 * GIF decoder
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2006 Baptiste Coudurier
6 * This file is part of Libav.
8 * Libav is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * Libav is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "lzw.h"
29 #define GCE_DISPOSAL_NONE 0
30 #define GCE_DISPOSAL_INPLACE 1
31 #define GCE_DISPOSAL_BACKGROUND 2
32 #define GCE_DISPOSAL_RESTORE 3
34 typedef struct GifState {
35 int screen_width;
36 int screen_height;
37 int bits_per_pixel;
38 int background_color_index;
39 int transparent_color_index;
40 int color_resolution;
41 uint32_t *image_palette;
43 /* after the frame is displayed, the disposal method is used */
44 int gce_disposal;
45 /* delay during which the frame is shown */
46 int gce_delay;
48 /* LZW compatible decoder */
49 GetByteContext gb;
50 LZWState *lzw;
52 /* aux buffers */
53 uint8_t global_palette[256 * 3];
54 uint8_t local_palette[256 * 3];
56 AVCodecContext* avctx;
57 } GifState;
59 static const uint8_t gif87a_sig[6] = "GIF87a";
60 static const uint8_t gif89a_sig[6] = "GIF89a";
62 static int gif_read_image(GifState *s, AVFrame *frame)
64 int left, top, width, height, bits_per_pixel, code_size, flags;
65 int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
66 uint8_t *ptr, *spal, *palette, *ptr1;
68 left = bytestream2_get_le16(&s->gb);
69 top = bytestream2_get_le16(&s->gb);
70 width = bytestream2_get_le16(&s->gb);
71 height = bytestream2_get_le16(&s->gb);
72 flags = bytestream2_get_byte(&s->gb);
73 is_interleaved = flags & 0x40;
74 has_local_palette = flags & 0x80;
75 bits_per_pixel = (flags & 0x07) + 1;
77 ff_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
79 if (has_local_palette) {
80 bytestream2_get_buffer(&s->gb, s->local_palette, 3 * (1 << bits_per_pixel));
81 palette = s->local_palette;
82 } else {
83 palette = s->global_palette;
84 bits_per_pixel = s->bits_per_pixel;
87 /* verify that all the image is inside the screen dimensions */
88 if (left + width > s->screen_width ||
89 top + height > s->screen_height ||
90 !width || !height) {
91 av_log(s->avctx, AV_LOG_ERROR, "Invalid image dimensions.\n");
92 return AVERROR_INVALIDDATA;
95 /* build the palette */
96 n = (1 << bits_per_pixel);
97 spal = palette;
98 for(i = 0; i < n; i++) {
99 s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
100 spal += 3;
102 for(; i < 256; i++)
103 s->image_palette[i] = (0xffu << 24);
104 /* handle transparency */
105 if (s->transparent_color_index >= 0)
106 s->image_palette[s->transparent_color_index] = 0;
108 /* now get the image data */
109 code_size = bytestream2_get_byte(&s->gb);
110 ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
111 bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF);
113 /* read all the image */
114 linesize = frame->linesize[0];
115 ptr1 = frame->data[0] + top * linesize + left;
116 ptr = ptr1;
117 pass = 0;
118 y1 = 0;
119 for (y = 0; y < height; y++) {
120 ff_lzw_decode(s->lzw, ptr, width);
121 if (is_interleaved) {
122 switch(pass) {
123 default:
124 case 0:
125 case 1:
126 y1 += 8;
127 ptr += linesize * 8;
128 break;
129 case 2:
130 y1 += 4;
131 ptr += linesize * 4;
132 break;
133 case 3:
134 y1 += 2;
135 ptr += linesize * 2;
136 break;
138 while (y1 >= height) {
139 y1 = 4 >> pass;
140 ptr = ptr1 + linesize * y1;
141 pass++;
143 } else {
144 ptr += linesize;
147 /* read the garbage data until end marker is found */
148 ff_lzw_decode_tail(s->lzw);
150 bytestream2_skip(&s->gb, ff_lzw_size_read(s->lzw));
151 return 0;
154 static int gif_read_extension(GifState *s)
156 int ext_code, ext_len, i, gce_flags, gce_transparent_index;
158 /* extension */
159 ext_code = bytestream2_get_byte(&s->gb);
160 ext_len = bytestream2_get_byte(&s->gb);
162 ff_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
164 switch(ext_code) {
165 case 0xf9:
166 if (ext_len != 4)
167 goto discard_ext;
168 s->transparent_color_index = -1;
169 gce_flags = bytestream2_get_byte(&s->gb);
170 s->gce_delay = bytestream2_get_le16(&s->gb);
171 gce_transparent_index = bytestream2_get_byte(&s->gb);
172 if (gce_flags & 0x01)
173 s->transparent_color_index = gce_transparent_index;
174 else
175 s->transparent_color_index = -1;
176 s->gce_disposal = (gce_flags >> 2) & 0x7;
178 ff_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
179 gce_flags, s->gce_delay,
180 s->transparent_color_index, s->gce_disposal);
182 ext_len = bytestream2_get_byte(&s->gb);
183 break;
186 /* NOTE: many extension blocks can come after */
187 discard_ext:
188 while (ext_len != 0) {
189 for (i = 0; i < ext_len; i++)
190 bytestream2_get_byte(&s->gb);
191 ext_len = bytestream2_get_byte(&s->gb);
193 ff_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
195 return 0;
198 static int gif_read_header1(GifState *s)
200 uint8_t sig[6];
201 int v, n;
202 int has_global_palette;
204 if (bytestream2_get_bytes_left(&s->gb) < 13)
205 return AVERROR_INVALIDDATA;
207 /* read gif signature */
208 bytestream2_get_buffer(&s->gb, sig, 6);
209 if (memcmp(sig, gif87a_sig, 6) != 0 &&
210 memcmp(sig, gif89a_sig, 6) != 0)
211 return AVERROR_INVALIDDATA;
213 /* read screen header */
214 s->transparent_color_index = -1;
215 s->screen_width = bytestream2_get_le16(&s->gb);
216 s->screen_height = bytestream2_get_le16(&s->gb);
217 if( (unsigned)s->screen_width > 32767
218 || (unsigned)s->screen_height > 32767){
219 av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
220 return AVERROR_INVALIDDATA;
223 v = bytestream2_get_byte(&s->gb);
224 s->color_resolution = ((v & 0x70) >> 4) + 1;
225 has_global_palette = (v & 0x80);
226 s->bits_per_pixel = (v & 0x07) + 1;
227 s->background_color_index = bytestream2_get_byte(&s->gb);
228 bytestream2_get_byte(&s->gb); /* ignored */
230 ff_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
231 s->screen_width, s->screen_height, s->bits_per_pixel,
232 has_global_palette);
234 if (has_global_palette) {
235 n = 1 << s->bits_per_pixel;
236 if (bytestream2_get_bytes_left(&s->gb) < n * 3)
237 return AVERROR_INVALIDDATA;
238 bytestream2_get_buffer(&s->gb, s->global_palette, n * 3);
240 return 0;
243 static int gif_parse_next_image(GifState *s, AVFrame *frame)
245 while (bytestream2_get_bytes_left(&s->gb) > 0) {
246 int code = bytestream2_get_byte(&s->gb);
247 int ret;
249 ff_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
251 switch (code) {
252 case ',':
253 return gif_read_image(s, frame);
254 case '!':
255 if ((ret = gif_read_extension(s)) < 0)
256 return ret;
257 break;
258 case ';':
259 /* end of image */
260 default:
261 /* error or erroneous EOF */
262 return AVERROR_INVALIDDATA;
265 return AVERROR_INVALIDDATA;
268 static av_cold int gif_decode_init(AVCodecContext *avctx)
270 GifState *s = avctx->priv_data;
272 s->avctx = avctx;
274 ff_lzw_decode_open(&s->lzw);
275 return 0;
278 static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
279 AVPacket *avpkt)
281 const uint8_t *buf = avpkt->data;
282 int buf_size = avpkt->size;
283 GifState *s = avctx->priv_data;
284 AVFrame *picture = data;
285 int ret;
287 bytestream2_init(&s->gb, buf, buf_size);
288 if ((ret = gif_read_header1(s)) < 0)
289 return ret;
291 avctx->pix_fmt = AV_PIX_FMT_PAL8;
293 if ((ret = ff_set_dimensions(avctx, s->screen_width, s->screen_height)) < 0)
294 return ret;
296 if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
297 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
298 return ret;
300 s->image_palette = (uint32_t *)picture->data[1];
301 ret = gif_parse_next_image(s, picture);
302 if (ret < 0)
303 return ret;
305 *got_frame = 1;
306 return bytestream2_tell(&s->gb);
309 static av_cold int gif_decode_close(AVCodecContext *avctx)
311 GifState *s = avctx->priv_data;
313 ff_lzw_decode_close(&s->lzw);
314 return 0;
317 AVCodec ff_gif_decoder = {
318 .name = "gif",
319 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
320 .type = AVMEDIA_TYPE_VIDEO,
321 .id = AV_CODEC_ID_GIF,
322 .priv_data_size = sizeof(GifState),
323 .init = gif_decode_init,
324 .close = gif_decode_close,
325 .decode = gif_decode_frame,
326 .capabilities = AV_CODEC_CAP_DR1,