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.

559 lines
17KB

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