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.

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