Directory: | ../../../ffmpeg/ |
---|---|
File: | src/libavcodec/gifdec.c |
Date: | 2022-07-05 19:52:29 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 201 | 272 | 73.9% |
Branches: | 89 | 139 | 64.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * GIF decoder | ||
3 | * Copyright (c) 2003 Fabrice Bellard | ||
4 | * Copyright (c) 2006 Baptiste Coudurier | ||
5 | * Copyright (c) 2012 Vitaliy E Sugrobov | ||
6 | * | ||
7 | * This file is part of FFmpeg. | ||
8 | * | ||
9 | * FFmpeg is free software; you can redistribute it and/or | ||
10 | * modify it under the terms of the GNU Lesser General Public | ||
11 | * License as published by the Free Software Foundation; either | ||
12 | * version 2.1 of the License, or (at your option) any later version. | ||
13 | * | ||
14 | * FFmpeg is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
17 | * Lesser General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU Lesser General Public | ||
20 | * License along with FFmpeg; if not, write to the Free Software | ||
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
22 | */ | ||
23 | |||
24 | #include "libavutil/imgutils.h" | ||
25 | #include "libavutil/opt.h" | ||
26 | #include "avcodec.h" | ||
27 | #include "bytestream.h" | ||
28 | #include "codec_internal.h" | ||
29 | #include "internal.h" | ||
30 | #include "lzw.h" | ||
31 | #include "gif.h" | ||
32 | |||
33 | /* This value is intentionally set to "transparent white" color. | ||
34 | * It is much better to have white background instead of black | ||
35 | * when gif image converted to format which not support transparency. | ||
36 | */ | ||
37 | #define GIF_TRANSPARENT_COLOR 0x00ffffff | ||
38 | |||
39 | typedef struct GifState { | ||
40 | const AVClass *class; | ||
41 | AVFrame *frame; | ||
42 | int screen_width; | ||
43 | int screen_height; | ||
44 | int has_global_palette; | ||
45 | int bits_per_pixel; | ||
46 | uint32_t bg_color; | ||
47 | int background_color_index; | ||
48 | int transparent_color_index; | ||
49 | int color_resolution; | ||
50 | /* intermediate buffer for storing color indices | ||
51 | * obtained from lzw-encoded data stream */ | ||
52 | uint8_t *idx_line; | ||
53 | int idx_line_size; | ||
54 | |||
55 | /* after the frame is displayed, the disposal method is used */ | ||
56 | int gce_prev_disposal; | ||
57 | int gce_disposal; | ||
58 | /* rectangle describing area that must be disposed */ | ||
59 | int gce_l, gce_t, gce_w, gce_h; | ||
60 | /* depending on disposal method we store either part of the image | ||
61 | * drawn on the canvas or background color that | ||
62 | * should be used upon disposal */ | ||
63 | uint32_t * stored_img; | ||
64 | int stored_img_size; | ||
65 | int stored_bg_color; | ||
66 | |||
67 | GetByteContext gb; | ||
68 | LZWState *lzw; | ||
69 | |||
70 | /* aux buffers */ | ||
71 | uint32_t global_palette[256]; | ||
72 | uint32_t local_palette[256]; | ||
73 | |||
74 | AVCodecContext *avctx; | ||
75 | int keyframe; | ||
76 | int keyframe_ok; | ||
77 | int trans_color; /**< color value that is used instead of transparent color */ | ||
78 | } GifState; | ||
79 | |||
80 | 1219 | static void gif_read_palette(GifState *s, uint32_t *pal, int nb) | |
81 | { | ||
82 | int i; | ||
83 | |||
84 |
2/2✓ Branch 0 taken 299328 times.
✓ Branch 1 taken 1219 times.
|
300547 | for (i = 0; i < nb; i++, pal++) |
85 | 299328 | *pal = (0xffu << 24) | bytestream2_get_be24u(&s->gb); | |
86 | 1219 | } | |
87 | |||
88 | 13 | static void gif_fill(AVFrame *picture, uint32_t color) | |
89 | { | ||
90 | 13 | uint32_t *p = (uint32_t *)picture->data[0]; | |
91 | 13 | uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height; | |
92 | |||
93 |
2/2✓ Branch 0 taken 727184 times.
✓ Branch 1 taken 13 times.
|
727197 | for (; p < p_end; p++) |
94 | 727184 | *p = color; | |
95 | 13 | } | |
96 | |||
97 | 6 | static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h) | |
98 | { | ||
99 | 6 | const int linesize = picture->linesize[0] / sizeof(uint32_t); | |
100 | 6 | const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize; | |
101 | 6 | const uint32_t *pr, *pb = py + h * linesize; | |
102 | uint32_t *px; | ||
103 | |||
104 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 6 times.
|
222 | for (; py < pb; py += linesize) { |
105 | 216 | px = (uint32_t *)py + l; | |
106 | 216 | pr = px + w; | |
107 | |||
108 |
2/2✓ Branch 0 taken 15552 times.
✓ Branch 1 taken 216 times.
|
15768 | for (; px < pr; px++) |
109 | 15552 | *px = color; | |
110 | } | ||
111 | 6 | } | |
112 | |||
113 | 3 | static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst, | |
114 | int linesize, int l, int t, int w, int h) | ||
115 | { | ||
116 | 3 | const int y_start = t * linesize; | |
117 | const uint32_t *src_px, | ||
118 | 3 | *src_py = src + y_start, | |
119 | 3 | *dst_py = dst + y_start; | |
120 | 3 | const uint32_t *src_pb = src_py + h * linesize; | |
121 | uint32_t *dst_px; | ||
122 | |||
123 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 3 times.
|
183 | for (; src_py < src_pb; src_py += linesize, dst_py += linesize) { |
124 | 180 | src_px = src_py + l; | |
125 | 180 | dst_px = (uint32_t *)dst_py + l; | |
126 | |||
127 | 180 | memcpy(dst_px, src_px, w * sizeof(uint32_t)); | |
128 | } | ||
129 | 3 | } | |
130 | |||
131 | 1294 | static int gif_read_image(GifState *s, AVFrame *frame) | |
132 | { | ||
133 | int left, top, width, height, bits_per_pixel, code_size, flags, pw; | ||
134 | int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size, lzwed_len; | ||
135 | uint32_t *ptr, *pal, *px, *pr, *ptr1; | ||
136 | int ret; | ||
137 | uint8_t *idx; | ||
138 | |||
139 | /* At least 9 bytes of Image Descriptor. */ | ||
140 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1294 times.
|
1294 | if (bytestream2_get_bytes_left(&s->gb) < 9) |
141 | ✗ | return AVERROR_INVALIDDATA; | |
142 | |||
143 | 1294 | left = bytestream2_get_le16u(&s->gb); | |
144 | 1294 | top = bytestream2_get_le16u(&s->gb); | |
145 | 1294 | width = bytestream2_get_le16u(&s->gb); | |
146 | 1294 | height = bytestream2_get_le16u(&s->gb); | |
147 | 1294 | flags = bytestream2_get_byteu(&s->gb); | |
148 | 1294 | is_interleaved = flags & 0x40; | |
149 | 1294 | has_local_palette = flags & 0x80; | |
150 | 1294 | bits_per_pixel = (flags & 0x07) + 1; | |
151 | |||
152 | ff_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height); | ||
153 | |||
154 |
2/2✓ Branch 0 taken 1213 times.
✓ Branch 1 taken 81 times.
|
1294 | if (has_local_palette) { |
155 | 1213 | pal_size = 1 << bits_per_pixel; | |
156 | |||
157 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1213 times.
|
1213 | if (bytestream2_get_bytes_left(&s->gb) < pal_size * 3) |
158 | ✗ | return AVERROR_INVALIDDATA; | |
159 | |||
160 | 1213 | gif_read_palette(s, s->local_palette, pal_size); | |
161 | 1213 | pal = s->local_palette; | |
162 | } else { | ||
163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 81 times.
|
81 | if (!s->has_global_palette) { |
164 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "picture doesn't have either global or local palette.\n"); | |
165 | ✗ | return AVERROR_INVALIDDATA; | |
166 | } | ||
167 | |||
168 | 81 | pal = s->global_palette; | |
169 | } | ||
170 | |||
171 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1281 times.
|
1294 | if (s->keyframe) { |
172 |
4/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 7 times.
|
13 | if (s->transparent_color_index == -1 && s->has_global_palette) { |
173 | /* transparency wasn't set before the first frame, fill with background color */ | ||
174 | 5 | gif_fill(frame, s->bg_color); | |
175 | } else { | ||
176 | /* otherwise fill with transparent color. | ||
177 | * this is necessary since by default picture filled with 0x80808080. */ | ||
178 | 8 | gif_fill(frame, s->trans_color); | |
179 | } | ||
180 | } | ||
181 | |||
182 | /* verify that all the image is inside the screen dimensions */ | ||
183 |
2/4✓ Branch 0 taken 1294 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1294 times.
|
1294 | if (!width || width > s->screen_width) { |
184 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid image width: %d, truncating.\n", width); | |
185 | ✗ | width = s->screen_width; | |
186 | } | ||
187 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (left >= s->screen_width) { |
188 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid left position: %d.\n", left); | |
189 | ✗ | return AVERROR_INVALIDDATA; | |
190 | } | ||
191 |
2/4✓ Branch 0 taken 1294 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1294 times.
|
1294 | if (!height || height > s->screen_height) { |
192 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Invalid image height: %d, truncating.\n", height); | |
193 | ✗ | height = s->screen_height; | |
194 | } | ||
195 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (top >= s->screen_height) { |
196 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "Invalid top position: %d.\n", top); | |
197 | ✗ | return AVERROR_INVALIDDATA; | |
198 | } | ||
199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (left + width > s->screen_width) { |
200 | /* width must be kept around to avoid lzw vs line desync */ | ||
201 | ✗ | pw = s->screen_width - left; | |
202 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Image too wide by %d, truncating.\n", | |
203 | ✗ | left + width - s->screen_width); | |
204 | } else { | ||
205 | 1294 | pw = width; | |
206 | } | ||
207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (top + height > s->screen_height) { |
208 | /* we don't care about the extra invisible lines */ | ||
209 | ✗ | av_log(s->avctx, AV_LOG_WARNING, "Image too high by %d, truncating.\n", | |
210 | ✗ | top + height - s->screen_height); | |
211 | ✗ | height = s->screen_height - top; | |
212 | } | ||
213 | |||
214 | /* process disposal method */ | ||
215 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1288 times.
|
1294 | if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) { |
216 | 6 | gif_fill_rect(frame, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h); | |
217 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1287 times.
|
1288 | } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) { |
218 | 1 | gif_copy_img_rect(s->stored_img, (uint32_t *)frame->data[0], | |
219 | 1 | frame->linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h); | |
220 | } | ||
221 | |||
222 | 1294 | s->gce_prev_disposal = s->gce_disposal; | |
223 | |||
224 |
2/2✓ Branch 0 taken 1293 times.
✓ Branch 1 taken 1 times.
|
1294 | if (s->gce_disposal != GCE_DISPOSAL_NONE) { |
225 | 1293 | s->gce_l = left; s->gce_t = top; | |
226 | 1293 | s->gce_w = pw; s->gce_h = height; | |
227 | |||
228 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1285 times.
|
1293 | if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) { |
229 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (s->transparent_color_index >= 0) |
230 | 8 | s->stored_bg_color = s->trans_color; | |
231 | else | ||
232 | ✗ | s->stored_bg_color = s->bg_color; | |
233 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1283 times.
|
1285 | } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) { |
234 | 2 | av_fast_malloc(&s->stored_img, &s->stored_img_size, frame->linesize[0] * frame->height); | |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!s->stored_img) |
236 | ✗ | return AVERROR(ENOMEM); | |
237 | |||
238 | 2 | gif_copy_img_rect((uint32_t *)frame->data[0], s->stored_img, | |
239 | 2 | frame->linesize[0] / sizeof(uint32_t), left, top, pw, height); | |
240 | } | ||
241 | } | ||
242 | |||
243 | /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */ | ||
244 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1294 times.
|
1294 | if (bytestream2_get_bytes_left(&s->gb) < 2) |
245 | ✗ | return AVERROR_INVALIDDATA; | |
246 | |||
247 | /* now get the image data */ | ||
248 | 1294 | code_size = bytestream2_get_byteu(&s->gb); | |
249 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 1294 times.
|
1294 | if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer, |
250 | bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF)) < 0) { | ||
251 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n"); | |
252 | ✗ | return ret; | |
253 | } | ||
254 | |||
255 | /* read all the image */ | ||
256 | 1294 | linesize = frame->linesize[0] / sizeof(uint32_t); | |
257 | 1294 | ptr1 = (uint32_t *)frame->data[0] + top * linesize + left; | |
258 | 1294 | ptr = ptr1; | |
259 | 1294 | pass = 0; | |
260 | 1294 | y1 = 0; | |
261 |
2/2✓ Branch 0 taken 37973 times.
✓ Branch 1 taken 1294 times.
|
39267 | for (y = 0; y < height; y++) { |
262 | 37973 | int count = ff_lzw_decode(s->lzw, s->idx_line, width); | |
263 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37973 times.
|
37973 | if (count != width) { |
264 | ✗ | if (count) | |
265 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "LZW decode failed\n"); | |
266 | ✗ | goto decode_tail; | |
267 | } | ||
268 | |||
269 | 37973 | pr = ptr + pw; | |
270 | |||
271 |
2/2✓ Branch 0 taken 5328599 times.
✓ Branch 1 taken 37973 times.
|
5366572 | for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) { |
272 |
2/2✓ Branch 0 taken 3988325 times.
✓ Branch 1 taken 1340274 times.
|
5328599 | if (*idx != s->transparent_color_index) |
273 | 3988325 | *px = pal[*idx]; | |
274 | } | ||
275 | |||
276 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 37973 times.
|
37973 | if (is_interleaved) { |
277 | ✗ | switch(pass) { | |
278 | ✗ | default: | |
279 | case 0: | ||
280 | case 1: | ||
281 | ✗ | y1 += 8; | |
282 | ✗ | ptr += linesize * 8; | |
283 | ✗ | break; | |
284 | ✗ | case 2: | |
285 | ✗ | y1 += 4; | |
286 | ✗ | ptr += linesize * 4; | |
287 | ✗ | break; | |
288 | ✗ | case 3: | |
289 | ✗ | y1 += 2; | |
290 | ✗ | ptr += linesize * 2; | |
291 | ✗ | break; | |
292 | } | ||
293 | ✗ | while (y1 >= height) { | |
294 | ✗ | y1 = 4 >> pass; | |
295 | ✗ | ptr = ptr1 + linesize * y1; | |
296 | ✗ | pass++; | |
297 | } | ||
298 | } else { | ||
299 | 37973 | ptr += linesize; | |
300 | } | ||
301 | } | ||
302 | |||
303 | 1294 | decode_tail: | |
304 | /* read the garbage data until end marker is found */ | ||
305 | 1294 | lzwed_len = ff_lzw_decode_tail(s->lzw); | |
306 | 1294 | bytestream2_skipu(&s->gb, lzwed_len); | |
307 | |||
308 | /* Graphic Control Extension's scope is single frame. | ||
309 | * Remove its influence. */ | ||
310 | 1294 | s->transparent_color_index = -1; | |
311 | 1294 | s->gce_disposal = GCE_DISPOSAL_NONE; | |
312 | |||
313 | 1294 | return 0; | |
314 | } | ||
315 | |||
316 | 1303 | static int gif_read_extension(GifState *s) | |
317 | { | ||
318 | int ext_code, ext_len, gce_flags, gce_transparent_index; | ||
319 | |||
320 | /* There must be at least 2 bytes: | ||
321 | * 1 for extension label and 1 for extension length. */ | ||
322 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1303 times.
|
1303 | if (bytestream2_get_bytes_left(&s->gb) < 2) |
323 | ✗ | return AVERROR_INVALIDDATA; | |
324 | |||
325 | 1303 | ext_code = bytestream2_get_byteu(&s->gb); | |
326 | 1303 | ext_len = bytestream2_get_byteu(&s->gb); | |
327 | |||
328 | ff_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len); | ||
329 | |||
330 |
2/2✓ Branch 0 taken 1294 times.
✓ Branch 1 taken 9 times.
|
1303 | switch(ext_code) { |
331 | 1294 | case GIF_GCE_EXT_LABEL: | |
332 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (ext_len != 4) |
333 | ✗ | goto discard_ext; | |
334 | |||
335 | /* We need at least 5 bytes more: 4 is for extension body | ||
336 | * and 1 for next block size. */ | ||
337 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1294 times.
|
1294 | if (bytestream2_get_bytes_left(&s->gb) < 5) |
338 | ✗ | return AVERROR_INVALIDDATA; | |
339 | |||
340 | 1294 | gce_flags = bytestream2_get_byteu(&s->gb); | |
341 | 1294 | bytestream2_skipu(&s->gb, 2); // delay during which the frame is shown | |
342 | 1294 | gce_transparent_index = bytestream2_get_byteu(&s->gb); | |
343 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1248 times.
|
1294 | if (gce_flags & 0x01) |
344 | 46 | s->transparent_color_index = gce_transparent_index; | |
345 | else | ||
346 | 1248 | s->transparent_color_index = -1; | |
347 | 1294 | s->gce_disposal = (gce_flags >> 2) & 0x7; | |
348 | |||
349 | ff_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n", | ||
350 | gce_flags, | ||
351 | s->transparent_color_index, s->gce_disposal); | ||
352 | |||
353 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (s->gce_disposal > 3) { |
354 | ✗ | s->gce_disposal = GCE_DISPOSAL_NONE; | |
355 | ff_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len); | ||
356 | } | ||
357 | |||
358 | 1294 | ext_len = bytestream2_get_byteu(&s->gb); | |
359 | 1294 | break; | |
360 | } | ||
361 | |||
362 | /* NOTE: many extension blocks can come after */ | ||
363 | 1303 | discard_ext: | |
364 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1303 times.
|
1330 | while (ext_len) { |
365 | /* There must be at least ext_len bytes and 1 for next block size byte. */ | ||
366 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 27 times.
|
27 | if (bytestream2_get_bytes_left(&s->gb) < ext_len + 1) |
367 | ✗ | return AVERROR_INVALIDDATA; | |
368 | |||
369 | 27 | bytestream2_skipu(&s->gb, ext_len); | |
370 | 27 | ext_len = bytestream2_get_byteu(&s->gb); | |
371 | |||
372 | ff_dlog(s->avctx, "ext_len1=%d\n", ext_len); | ||
373 | } | ||
374 | 1303 | return 0; | |
375 | } | ||
376 | |||
377 | 13 | static int gif_read_header1(GifState *s) | |
378 | { | ||
379 | uint8_t sig[6]; | ||
380 | int v, n; | ||
381 | int background_color_index; | ||
382 | |||
383 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if (bytestream2_get_bytes_left(&s->gb) < 13) |
384 | ✗ | return AVERROR_INVALIDDATA; | |
385 | |||
386 | /* read gif signature */ | ||
387 | 13 | bytestream2_get_bufferu(&s->gb, sig, 6); | |
388 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
13 | if (memcmp(sig, gif87a_sig, 6) && |
389 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | memcmp(sig, gif89a_sig, 6)) |
390 | ✗ | return AVERROR_INVALIDDATA; | |
391 | |||
392 | /* read screen header */ | ||
393 | 13 | s->transparent_color_index = -1; | |
394 | 13 | s->screen_width = bytestream2_get_le16u(&s->gb); | |
395 | 13 | s->screen_height = bytestream2_get_le16u(&s->gb); | |
396 | |||
397 | 13 | v = bytestream2_get_byteu(&s->gb); | |
398 | 13 | s->color_resolution = ((v & 0x70) >> 4) + 1; | |
399 | 13 | s->has_global_palette = (v & 0x80); | |
400 | 13 | s->bits_per_pixel = (v & 0x07) + 1; | |
401 | 13 | background_color_index = bytestream2_get_byteu(&s->gb); | |
402 | 13 | n = bytestream2_get_byteu(&s->gb); | |
403 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (n) { |
404 | ✗ | s->avctx->sample_aspect_ratio.num = n + 15; | |
405 | ✗ | s->avctx->sample_aspect_ratio.den = 64; | |
406 | } | ||
407 | |||
408 | ff_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n", | ||
409 | s->screen_width, s->screen_height, s->bits_per_pixel, | ||
410 | s->has_global_palette); | ||
411 | |||
412 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
|
13 | if (s->has_global_palette) { |
413 | 6 | s->background_color_index = background_color_index; | |
414 | 6 | n = 1 << s->bits_per_pixel; | |
415 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
|
6 | if (bytestream2_get_bytes_left(&s->gb) < n * 3) |
416 | ✗ | return AVERROR_INVALIDDATA; | |
417 | |||
418 | 6 | gif_read_palette(s, s->global_palette, n); | |
419 | 6 | s->bg_color = s->global_palette[s->background_color_index]; | |
420 | } else | ||
421 | 7 | s->background_color_index = -1; | |
422 | |||
423 | 13 | return 0; | |
424 | } | ||
425 | |||
426 | 1294 | static int gif_parse_next_image(GifState *s, AVFrame *frame) | |
427 | { | ||
428 |
1/2✓ Branch 1 taken 2597 times.
✗ Branch 2 not taken.
|
2597 | while (bytestream2_get_bytes_left(&s->gb) > 0) { |
429 | 2597 | int code = bytestream2_get_byte(&s->gb); | |
430 | int ret; | ||
431 | |||
432 | 2597 | av_log(s->avctx, AV_LOG_DEBUG, "code=%02x '%c'\n", code, code); | |
433 | |||
434 |
2/4✓ Branch 0 taken 1294 times.
✓ Branch 1 taken 1303 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2597 | switch (code) { |
435 | 1294 | case GIF_IMAGE_SEPARATOR: | |
436 | 1294 | return gif_read_image(s, frame); | |
437 | 1303 | case GIF_EXTENSION_INTRODUCER: | |
438 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1303 times.
|
1303 | if ((ret = gif_read_extension(s)) < 0) |
439 | ✗ | return ret; | |
440 | 1303 | break; | |
441 | ✗ | case GIF_TRAILER: | |
442 | /* end of image */ | ||
443 | ✗ | return AVERROR_EOF; | |
444 | ✗ | default: | |
445 | /* erroneous block label */ | ||
446 | ✗ | return AVERROR_INVALIDDATA; | |
447 | } | ||
448 | } | ||
449 | ✗ | return AVERROR_EOF; | |
450 | } | ||
451 | |||
452 | 28 | static av_cold int gif_decode_init(AVCodecContext *avctx) | |
453 | { | ||
454 | 28 | GifState *s = avctx->priv_data; | |
455 | |||
456 | 28 | s->avctx = avctx; | |
457 | |||
458 | 28 | avctx->pix_fmt = AV_PIX_FMT_RGB32; | |
459 | 28 | s->frame = av_frame_alloc(); | |
460 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!s->frame) |
461 | ✗ | return AVERROR(ENOMEM); | |
462 | 28 | ff_lzw_decode_open(&s->lzw); | |
463 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
|
28 | if (!s->lzw) |
464 | ✗ | return AVERROR(ENOMEM); | |
465 | 28 | return 0; | |
466 | } | ||
467 | |||
468 | 1294 | static int gif_decode_frame(AVCodecContext *avctx, AVFrame *rframe, | |
469 | int *got_frame, AVPacket *avpkt) | ||
470 | { | ||
471 | 1294 | GifState *s = avctx->priv_data; | |
472 | int ret; | ||
473 | |||
474 | 1294 | bytestream2_init(&s->gb, avpkt->data, avpkt->size); | |
475 | |||
476 | 1294 | s->frame->pts = avpkt->pts; | |
477 | 1294 | s->frame->pkt_dts = avpkt->dts; | |
478 | 1294 | s->frame->pkt_duration = avpkt->duration; | |
479 | |||
480 |
1/2✓ Branch 0 taken 1294 times.
✗ Branch 1 not taken.
|
1294 | if (avpkt->size >= 6) { |
481 |
1/2✓ Branch 0 taken 1294 times.
✗ Branch 1 not taken.
|
2588 | s->keyframe = memcmp(avpkt->data, gif87a_sig, 6) == 0 || |
482 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1281 times.
|
1294 | memcmp(avpkt->data, gif89a_sig, 6) == 0; |
483 | } else { | ||
484 | ✗ | s->keyframe = 0; | |
485 | } | ||
486 | |||
487 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1281 times.
|
1294 | if (s->keyframe) { |
488 | 13 | s->keyframe_ok = 0; | |
489 | 13 | s->gce_prev_disposal = GCE_DISPOSAL_NONE; | |
490 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if ((ret = gif_read_header1(s)) < 0) |
491 | ✗ | return ret; | |
492 | |||
493 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if ((ret = ff_set_dimensions(avctx, s->screen_width, s->screen_height)) < 0) |
494 | ✗ | return ret; | |
495 | |||
496 | 13 | av_frame_unref(s->frame); | |
497 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
|
13 | if ((ret = ff_get_buffer(avctx, s->frame, 0)) < 0) |
498 | ✗ | return ret; | |
499 | |||
500 | 13 | av_fast_malloc(&s->idx_line, &s->idx_line_size, s->screen_width); | |
501 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (!s->idx_line) |
502 | ✗ | return AVERROR(ENOMEM); | |
503 | |||
504 | 13 | s->frame->pict_type = AV_PICTURE_TYPE_I; | |
505 | 13 | s->frame->key_frame = 1; | |
506 | 13 | s->keyframe_ok = 1; | |
507 | } else { | ||
508 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1281 times.
|
1281 | if (!s->keyframe_ok) { |
509 | ✗ | av_log(avctx, AV_LOG_ERROR, "cannot decode frame without keyframe\n"); | |
510 | ✗ | return AVERROR_INVALIDDATA; | |
511 | } | ||
512 | |||
513 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1281 times.
|
1281 | if ((ret = ff_reget_buffer(avctx, s->frame, 0)) < 0) |
514 | ✗ | return ret; | |
515 | |||
516 | 1281 | s->frame->pict_type = AV_PICTURE_TYPE_P; | |
517 | 1281 | s->frame->key_frame = 0; | |
518 | } | ||
519 | |||
520 | 1294 | ret = gif_parse_next_image(s, s->frame); | |
521 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1294 times.
|
1294 | if (ret < 0) |
522 | ✗ | return ret; | |
523 | |||
524 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1294 times.
|
1294 | if ((ret = av_frame_ref(rframe, s->frame)) < 0) |
525 | ✗ | return ret; | |
526 | 1294 | *got_frame = 1; | |
527 | |||
528 | 1294 | return bytestream2_tell(&s->gb); | |
529 | } | ||
530 | |||
531 | 28 | static av_cold int gif_decode_close(AVCodecContext *avctx) | |
532 | { | ||
533 | 28 | GifState *s = avctx->priv_data; | |
534 | |||
535 | 28 | ff_lzw_decode_close(&s->lzw); | |
536 | 28 | av_frame_free(&s->frame); | |
537 | 28 | av_freep(&s->idx_line); | |
538 | 28 | av_freep(&s->stored_img); | |
539 | |||
540 | 28 | return 0; | |
541 | } | ||
542 | |||
543 | static const AVOption options[] = { | ||
544 | { "trans_color", "color value (ARGB) that is used instead of transparent color", | ||
545 | offsetof(GifState, trans_color), AV_OPT_TYPE_INT, | ||
546 | {.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff, | ||
547 | AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM }, | ||
548 | { NULL }, | ||
549 | }; | ||
550 | |||
551 | static const AVClass decoder_class = { | ||
552 | .class_name = "gif decoder", | ||
553 | .item_name = av_default_item_name, | ||
554 | .option = options, | ||
555 | .version = LIBAVUTIL_VERSION_INT, | ||
556 | .category = AV_CLASS_CATEGORY_DECODER, | ||
557 | }; | ||
558 | |||
559 | const FFCodec ff_gif_decoder = { | ||
560 | .p.name = "gif", | ||
561 | .p.long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"), | ||
562 | .p.type = AVMEDIA_TYPE_VIDEO, | ||
563 | .p.id = AV_CODEC_ID_GIF, | ||
564 | .priv_data_size = sizeof(GifState), | ||
565 | .init = gif_decode_init, | ||
566 | .close = gif_decode_close, | ||
567 | FF_CODEC_DECODE_CB(gif_decode_frame), | ||
568 | .p.capabilities = AV_CODEC_CAP_DR1, | ||
569 | .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | | ||
570 | FF_CODEC_CAP_INIT_CLEANUP, | ||
571 | .p.priv_class = &decoder_class, | ||
572 | }; | ||
573 |