You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

564 lines
18KB

  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. //#define DEBUG
  24. #include "libavutil/imgutils.h"
  25. #include "libavutil/opt.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #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
  33. #define GIF_TRAILER 0x3b
  34. #define GIF_EXTENSION_INTRODUCER 0x21
  35. #define GIF_IMAGE_SEPARATOR 0x2c
  36. #define GIF_GCE_EXT_LABEL 0xf9
  37. /* This value is intentionally set to "transparent white" color.
  38. * It is much better to have white background instead of black
  39. * when gif image converted to format which not support transparency.
  40. */
  41. #define GIF_TRANSPARENT_COLOR 0x00ffffff
  42. typedef struct GifState {
  43. const AVClass *class;
  44. AVFrame picture;
  45. int screen_width;
  46. int screen_height;
  47. int has_global_palette;
  48. int bits_per_pixel;
  49. uint32_t bg_color;
  50. int background_color_index;
  51. int transparent_color_index;
  52. int color_resolution;
  53. /* intermediate buffer for storing color indices
  54. * obtained from lzw-encoded data stream */
  55. uint8_t *idx_line;
  56. /* after the frame is displayed, the disposal method is used */
  57. int gce_prev_disposal;
  58. int gce_disposal;
  59. /* rectangle describing area that must be disposed */
  60. int gce_l, gce_t, gce_w, gce_h;
  61. /* depending on disposal method we store either part of the image
  62. * drawn on the canvas or background color that
  63. * should be used upon disposal */
  64. uint32_t * stored_img;
  65. int stored_bg_color;
  66. /* LZW compatible decoder */
  67. const uint8_t *bytestream;
  68. const uint8_t *bytestream_end;
  69. LZWState *lzw;
  70. /* aux buffers */
  71. uint32_t global_palette[256];
  72. uint32_t local_palette[256];
  73. AVCodecContext *avctx;
  74. int keyframe;
  75. int trans_color; /**< color value that is used instead of transparent color */
  76. } GifState;
  77. static const uint8_t gif87a_sig[6] = "GIF87a";
  78. static const uint8_t gif89a_sig[6] = "GIF89a";
  79. static void gif_read_palette(const uint8_t **buf, uint32_t *pal, int nb)
  80. {
  81. const uint8_t *pal_end = *buf + nb * 3;
  82. for (; *buf < pal_end; *buf += 3, pal++)
  83. *pal = (0xffu << 24) | AV_RB24(*buf);
  84. }
  85. static void gif_fill(AVFrame *picture, uint32_t color)
  86. {
  87. uint32_t *p = (uint32_t *)picture->data[0];
  88. uint32_t *p_end = p + (picture->linesize[0] / sizeof(uint32_t)) * picture->height;
  89. for (; p < p_end; p++)
  90. *p = color;
  91. }
  92. static void gif_fill_rect(AVFrame *picture, uint32_t color, int l, int t, int w, int h)
  93. {
  94. const int linesize = picture->linesize[0] / sizeof(uint32_t);
  95. const uint32_t *py = (uint32_t *)picture->data[0] + t * linesize;
  96. const uint32_t *pr, *pb = py + (t + h) * linesize;
  97. uint32_t *px;
  98. for (; py < pb; py += linesize) {
  99. px = (uint32_t *)py + l;
  100. pr = px + w;
  101. for (; px < pr; px++)
  102. *px = color;
  103. }
  104. }
  105. static void gif_copy_img_rect(const uint32_t *src, uint32_t *dst,
  106. int linesize, int l, int t, int w, int h)
  107. {
  108. const int y_start = t * linesize;
  109. const uint32_t *src_px, *src_pr,
  110. *src_py = src + y_start,
  111. *dst_py = dst + y_start;
  112. const uint32_t *src_pb = src_py + (t + h) * linesize;
  113. uint32_t *dst_px;
  114. for (; src_py < src_pb; src_py += linesize, dst_py += linesize) {
  115. src_px = src_py + l;
  116. dst_px = (uint32_t *)dst_py + l;
  117. src_pr = src_px + w;
  118. for (; src_px < src_pr; src_px++, dst_px++)
  119. *dst_px = *src_px;
  120. }
  121. }
  122. static int gif_read_image(GifState *s)
  123. {
  124. int left, top, width, height, bits_per_pixel, code_size, flags;
  125. int is_interleaved, has_local_palette, y, pass, y1, linesize, pal_size;
  126. uint32_t *ptr, *pal, *px, *pr, *ptr1;
  127. int ret;
  128. uint8_t *idx;
  129. /* At least 9 bytes of Image Descriptor. */
  130. if (s->bytestream_end < s->bytestream + 9)
  131. return AVERROR_INVALIDDATA;
  132. left = bytestream_get_le16(&s->bytestream);
  133. top = bytestream_get_le16(&s->bytestream);
  134. width = bytestream_get_le16(&s->bytestream);
  135. height = bytestream_get_le16(&s->bytestream);
  136. flags = bytestream_get_byte(&s->bytestream);
  137. is_interleaved = flags & 0x40;
  138. has_local_palette = flags & 0x80;
  139. bits_per_pixel = (flags & 0x07) + 1;
  140. av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  141. if (has_local_palette) {
  142. pal_size = 1 << bits_per_pixel;
  143. if (s->bytestream_end < s->bytestream + pal_size * 3)
  144. return AVERROR_INVALIDDATA;
  145. gif_read_palette(&s->bytestream, s->local_palette, pal_size);
  146. pal = s->local_palette;
  147. } else {
  148. if (!s->has_global_palette) {
  149. av_log(s->avctx, AV_LOG_FATAL, "picture doesn't have either global or local palette.\n");
  150. return AVERROR_INVALIDDATA;
  151. }
  152. pal = s->global_palette;
  153. }
  154. if (s->keyframe) {
  155. if (s->transparent_color_index == -1 && s->has_global_palette) {
  156. /* transparency wasn't set before the first frame, fill with background color */
  157. gif_fill(&s->picture, s->bg_color);
  158. } else {
  159. /* otherwise fill with transparent color.
  160. * this is necessary since by default picture filled with 0x80808080. */
  161. gif_fill(&s->picture, s->trans_color);
  162. }
  163. }
  164. /* verify that all the image is inside the screen dimensions */
  165. if (left + width > s->screen_width ||
  166. top + height > s->screen_height)
  167. return AVERROR(EINVAL);
  168. /* process disposal method */
  169. if (s->gce_prev_disposal == GCE_DISPOSAL_BACKGROUND) {
  170. gif_fill_rect(&s->picture, s->stored_bg_color, s->gce_l, s->gce_t, s->gce_w, s->gce_h);
  171. } else if (s->gce_prev_disposal == GCE_DISPOSAL_RESTORE) {
  172. gif_copy_img_rect(s->stored_img, (uint32_t *)s->picture.data[0],
  173. s->picture.linesize[0] / sizeof(uint32_t), s->gce_l, s->gce_t, s->gce_w, s->gce_h);
  174. }
  175. s->gce_prev_disposal = s->gce_disposal;
  176. if (s->gce_disposal != GCE_DISPOSAL_NONE) {
  177. s->gce_l = left; s->gce_t = top;
  178. s->gce_w = width; s->gce_h = height;
  179. if (s->gce_disposal == GCE_DISPOSAL_BACKGROUND) {
  180. if (s->background_color_index == s->transparent_color_index)
  181. s->stored_bg_color = s->trans_color;
  182. else
  183. s->stored_bg_color = s->bg_color;
  184. } else if (s->gce_disposal == GCE_DISPOSAL_RESTORE) {
  185. if (!s->stored_img) {
  186. s->stored_img = av_malloc(s->picture.linesize[0] * s->picture.height);
  187. if (!s->stored_img)
  188. return AVERROR(ENOMEM);
  189. }
  190. gif_copy_img_rect((uint32_t *)s->picture.data[0], s->stored_img,
  191. s->picture.linesize[0] / sizeof(uint32_t), left, top, width, height);
  192. }
  193. }
  194. /* Expect at least 2 bytes: 1 for lzw code size and 1 for block size. */
  195. if (s->bytestream_end < s->bytestream + 2)
  196. return AVERROR_INVALIDDATA;
  197. /* now get the image data */
  198. code_size = bytestream_get_byte(&s->bytestream);
  199. if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
  200. s->bytestream_end - s->bytestream, FF_LZW_GIF)) < 0) {
  201. av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
  202. return ret;
  203. }
  204. /* read all the image */
  205. linesize = s->picture.linesize[0] / sizeof(uint32_t);
  206. ptr1 = (uint32_t *)s->picture.data[0] + top * linesize + left;
  207. ptr = ptr1;
  208. pass = 0;
  209. y1 = 0;
  210. for (y = 0; y < height; y++) {
  211. if (ff_lzw_decode(s->lzw, s->idx_line, width) == 0)
  212. goto decode_tail;
  213. pr = ptr + width;
  214. for (px = ptr, idx = s->idx_line; px < pr; px++, idx++) {
  215. if (*idx != s->transparent_color_index)
  216. *px = pal[*idx];
  217. }
  218. if (is_interleaved) {
  219. switch(pass) {
  220. default:
  221. case 0:
  222. case 1:
  223. y1 += 8;
  224. ptr += linesize * 8;
  225. if (y1 >= height) {
  226. y1 = pass ? 2 : 4;
  227. ptr = ptr1 + linesize * y1;
  228. pass++;
  229. }
  230. break;
  231. case 2:
  232. y1 += 4;
  233. ptr += linesize * 4;
  234. if (y1 >= height) {
  235. y1 = 1;
  236. ptr = ptr1 + linesize;
  237. pass++;
  238. }
  239. break;
  240. case 3:
  241. y1 += 2;
  242. ptr += linesize * 2;
  243. break;
  244. }
  245. } else {
  246. ptr += linesize;
  247. }
  248. }
  249. decode_tail:
  250. /* read the garbage data until end marker is found */
  251. ff_lzw_decode_tail(s->lzw);
  252. s->bytestream = ff_lzw_cur_ptr(s->lzw);
  253. /* Graphic Control Extension's scope is single frame.
  254. * Remove its influence. */
  255. s->transparent_color_index = -1;
  256. s->gce_disposal = GCE_DISPOSAL_NONE;
  257. return 0;
  258. }
  259. static int gif_read_extension(GifState *s)
  260. {
  261. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  262. /* There must be at least 2 bytes:
  263. * 1 for extension label and 1 for extension length. */
  264. if (s->bytestream_end < s->bytestream + 2)
  265. return AVERROR_INVALIDDATA;
  266. ext_code = bytestream_get_byte(&s->bytestream);
  267. ext_len = bytestream_get_byte(&s->bytestream);
  268. av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
  269. switch(ext_code) {
  270. case GIF_GCE_EXT_LABEL:
  271. if (ext_len != 4)
  272. goto discard_ext;
  273. /* We need at least 5 bytes more: 4 is for extension body
  274. * and 1 for next block size. */
  275. if (s->bytestream_end < s->bytestream + 5)
  276. return AVERROR_INVALIDDATA;
  277. s->transparent_color_index = -1;
  278. gce_flags = bytestream_get_byte(&s->bytestream);
  279. bytestream_get_le16(&s->bytestream); // delay during which the frame is shown
  280. gce_transparent_index = bytestream_get_byte(&s->bytestream);
  281. if (gce_flags & 0x01)
  282. s->transparent_color_index = gce_transparent_index;
  283. else
  284. s->transparent_color_index = -1;
  285. s->gce_disposal = (gce_flags >> 2) & 0x7;
  286. av_dlog(s->avctx, "gce_flags=%x tcolor=%d disposal=%d\n",
  287. gce_flags,
  288. s->transparent_color_index, s->gce_disposal);
  289. if (s->gce_disposal > 3) {
  290. s->gce_disposal = GCE_DISPOSAL_NONE;
  291. av_dlog(s->avctx, "invalid value in gce_disposal (%d). Using default value of 0.\n", ext_len);
  292. }
  293. ext_len = bytestream_get_byte(&s->bytestream);
  294. break;
  295. }
  296. /* NOTE: many extension blocks can come after */
  297. discard_ext:
  298. while (ext_len != 0) {
  299. /* There must be at least ext_len bytes and 1 for next block size byte. */
  300. if (s->bytestream_end < s->bytestream + ext_len + 1)
  301. return AVERROR_INVALIDDATA;
  302. for (i = 0; i < ext_len; i++)
  303. bytestream_get_byte(&s->bytestream);
  304. ext_len = bytestream_get_byte(&s->bytestream);
  305. av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
  306. }
  307. return 0;
  308. }
  309. static int gif_read_header1(GifState *s)
  310. {
  311. uint8_t sig[6];
  312. int v, n;
  313. int background_color_index;
  314. if (s->bytestream_end < s->bytestream + 13)
  315. return AVERROR_INVALIDDATA;
  316. /* read gif signature */
  317. bytestream_get_buffer(&s->bytestream, sig, 6);
  318. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  319. memcmp(sig, gif89a_sig, 6) != 0)
  320. return AVERROR_INVALIDDATA;
  321. /* read screen header */
  322. s->transparent_color_index = -1;
  323. s->screen_width = bytestream_get_le16(&s->bytestream);
  324. s->screen_height = bytestream_get_le16(&s->bytestream);
  325. if( (unsigned)s->screen_width > 32767
  326. || (unsigned)s->screen_height > 32767){
  327. av_log(s->avctx, AV_LOG_ERROR, "picture size too large\n");
  328. return AVERROR_INVALIDDATA;
  329. }
  330. s->idx_line = av_malloc(s->screen_width);
  331. if (!s->idx_line)
  332. return AVERROR(ENOMEM);
  333. v = bytestream_get_byte(&s->bytestream);
  334. s->color_resolution = ((v & 0x70) >> 4) + 1;
  335. s->has_global_palette = (v & 0x80);
  336. s->bits_per_pixel = (v & 0x07) + 1;
  337. background_color_index = bytestream_get_byte(&s->bytestream);
  338. bytestream_get_byte(&s->bytestream); /* ignored */
  339. av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  340. s->screen_width, s->screen_height, s->bits_per_pixel,
  341. s->has_global_palette);
  342. if (s->has_global_palette) {
  343. s->background_color_index = background_color_index;
  344. n = 1 << s->bits_per_pixel;
  345. if (s->bytestream_end < s->bytestream + n * 3)
  346. return AVERROR_INVALIDDATA;
  347. gif_read_palette(&s->bytestream, s->global_palette, n);
  348. s->bg_color = s->global_palette[s->background_color_index];
  349. } else
  350. s->background_color_index = -1;
  351. return 0;
  352. }
  353. static int gif_parse_next_image(GifState *s, int *got_picture)
  354. {
  355. int ret;
  356. *got_picture = sizeof(AVPicture);
  357. while (s->bytestream < s->bytestream_end) {
  358. int code = bytestream_get_byte(&s->bytestream);
  359. av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
  360. switch (code) {
  361. case GIF_IMAGE_SEPARATOR:
  362. return gif_read_image(s);
  363. case GIF_EXTENSION_INTRODUCER:
  364. if ((ret = gif_read_extension(s)) < 0)
  365. return ret;
  366. break;
  367. case GIF_TRAILER:
  368. /* end of image */
  369. *got_picture = 0;
  370. return 0;
  371. default:
  372. /* erroneous block label */
  373. return AVERROR_INVALIDDATA;
  374. }
  375. }
  376. return AVERROR_EOF;
  377. }
  378. static av_cold int gif_decode_init(AVCodecContext *avctx)
  379. {
  380. GifState *s = avctx->priv_data;
  381. s->avctx = avctx;
  382. avctx->pix_fmt = AV_PIX_FMT_RGB32;
  383. avcodec_get_frame_defaults(&s->picture);
  384. avctx->coded_frame= &s->picture;
  385. s->picture.data[0] = NULL;
  386. ff_lzw_decode_open(&s->lzw);
  387. return 0;
  388. }
  389. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_picture, AVPacket *avpkt)
  390. {
  391. const uint8_t *buf = avpkt->data;
  392. int buf_size = avpkt->size;
  393. GifState *s = avctx->priv_data;
  394. AVFrame *picture = data;
  395. int ret;
  396. s->picture.pts = avpkt->pts;
  397. s->picture.pkt_pts = avpkt->pts;
  398. s->picture.pkt_dts = avpkt->dts;
  399. s->picture.pkt_duration = avpkt->duration;
  400. s->bytestream = buf;
  401. s->bytestream_end = buf + buf_size;
  402. if (buf_size >= 6) {
  403. s->keyframe = memcmp(s->bytestream, gif87a_sig, 6) == 0 ||
  404. memcmp(s->bytestream, gif89a_sig, 6) == 0;
  405. } else {
  406. s->keyframe = 0;
  407. }
  408. if (s->keyframe) {
  409. if ((ret = gif_read_header1(s)) < 0)
  410. return ret;
  411. if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
  412. return ret;
  413. avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
  414. if (s->picture.data[0])
  415. avctx->release_buffer(avctx, &s->picture);
  416. if ((ret = avctx->get_buffer(avctx, &s->picture)) < 0) {
  417. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  418. return ret;
  419. }
  420. s->picture.pict_type = AV_PICTURE_TYPE_I;
  421. s->picture.key_frame = 1;
  422. } else {
  423. if ((ret = avctx->reget_buffer(avctx, &s->picture)) < 0) {
  424. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  425. return ret;
  426. }
  427. s->picture.pict_type = AV_PICTURE_TYPE_P;
  428. s->picture.key_frame = 0;
  429. }
  430. ret = gif_parse_next_image(s, got_picture);
  431. if (ret < 0)
  432. return ret;
  433. else if (*got_picture)
  434. *picture = s->picture;
  435. return s->bytestream - buf;
  436. }
  437. static av_cold int gif_decode_close(AVCodecContext *avctx)
  438. {
  439. GifState *s = avctx->priv_data;
  440. ff_lzw_decode_close(&s->lzw);
  441. if(s->picture.data[0])
  442. avctx->release_buffer(avctx, &s->picture);
  443. av_freep(&s->idx_line);
  444. av_freep(&s->stored_img);
  445. return 0;
  446. }
  447. static const AVOption options[] = {
  448. { "trans_color", "color value (ARGB) that is used instead of transparent color",
  449. offsetof(GifState, trans_color), AV_OPT_TYPE_INT,
  450. {.i64 = GIF_TRANSPARENT_COLOR}, 0, 0xffffffff,
  451. AV_OPT_FLAG_DECODING_PARAM|AV_OPT_FLAG_VIDEO_PARAM },
  452. { NULL },
  453. };
  454. static const AVClass decoder_class = {
  455. .class_name = "gif decoder",
  456. .item_name = av_default_item_name,
  457. .option = options,
  458. .version = LIBAVUTIL_VERSION_INT,
  459. .category = AV_CLASS_CATEGORY_DECODER,
  460. };
  461. AVCodec ff_gif_decoder = {
  462. .name = "gif",
  463. .type = AVMEDIA_TYPE_VIDEO,
  464. .id = AV_CODEC_ID_GIF,
  465. .priv_data_size = sizeof(GifState),
  466. .init = gif_decode_init,
  467. .close = gif_decode_close,
  468. .decode = gif_decode_frame,
  469. .capabilities = CODEC_CAP_DR1,
  470. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  471. .priv_class = &decoder_class,
  472. };