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.

328 lines
9.5KB

  1. /*
  2. * GIF decoder
  3. * Copyright (c) 2003 Fabrice Bellard
  4. * Copyright (c) 2006 Baptiste Coudurier
  5. *
  6. * This file is part of Libav.
  7. *
  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.
  12. *
  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.
  17. *
  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
  21. */
  22. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "lzw.h"
  27. #define GCE_DISPOSAL_NONE 0
  28. #define GCE_DISPOSAL_INPLACE 1
  29. #define GCE_DISPOSAL_BACKGROUND 2
  30. #define GCE_DISPOSAL_RESTORE 3
  31. typedef struct GifState {
  32. int screen_width;
  33. int screen_height;
  34. int bits_per_pixel;
  35. int background_color_index;
  36. int transparent_color_index;
  37. int color_resolution;
  38. uint32_t *image_palette;
  39. /* after the frame is displayed, the disposal method is used */
  40. int gce_disposal;
  41. /* delay during which the frame is shown */
  42. int gce_delay;
  43. /* LZW compatible decoder */
  44. GetByteContext gb;
  45. LZWState *lzw;
  46. /* aux buffers */
  47. uint8_t global_palette[256 * 3];
  48. uint8_t local_palette[256 * 3];
  49. AVCodecContext* avctx;
  50. } GifState;
  51. static const uint8_t gif87a_sig[6] = "GIF87a";
  52. static const uint8_t gif89a_sig[6] = "GIF89a";
  53. static int gif_read_image(GifState *s, AVFrame *frame)
  54. {
  55. int left, top, width, height, bits_per_pixel, code_size, flags;
  56. int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
  57. uint8_t *ptr, *spal, *palette, *ptr1;
  58. left = bytestream2_get_le16(&s->gb);
  59. top = bytestream2_get_le16(&s->gb);
  60. width = bytestream2_get_le16(&s->gb);
  61. height = bytestream2_get_le16(&s->gb);
  62. flags = bytestream2_get_byte(&s->gb);
  63. is_interleaved = flags & 0x40;
  64. has_local_palette = flags & 0x80;
  65. bits_per_pixel = (flags & 0x07) + 1;
  66. ff_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  67. if (has_local_palette) {
  68. bytestream2_get_buffer(&s->gb, s->local_palette, 3 * (1 << bits_per_pixel));
  69. palette = s->local_palette;
  70. } else {
  71. palette = s->global_palette;
  72. bits_per_pixel = s->bits_per_pixel;
  73. }
  74. /* verify that all the image is inside the screen dimensions */
  75. if (left + width > s->screen_width ||
  76. top + height > s->screen_height ||
  77. !width || !height) {
  78. av_log(s->avctx, AV_LOG_ERROR, "Invalid image dimensions.\n");
  79. return AVERROR_INVALIDDATA;
  80. }
  81. /* build the palette */
  82. n = (1 << bits_per_pixel);
  83. spal = palette;
  84. for(i = 0; i < n; i++) {
  85. s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
  86. spal += 3;
  87. }
  88. for(; i < 256; i++)
  89. s->image_palette[i] = (0xffu << 24);
  90. /* handle transparency */
  91. if (s->transparent_color_index >= 0)
  92. s->image_palette[s->transparent_color_index] = 0;
  93. /* now get the image data */
  94. code_size = bytestream2_get_byte(&s->gb);
  95. ff_lzw_decode_init(s->lzw, code_size, s->gb.buffer,
  96. bytestream2_get_bytes_left(&s->gb), FF_LZW_GIF);
  97. /* read all the image */
  98. linesize = frame->linesize[0];
  99. ptr1 = frame->data[0] + top * linesize + left;
  100. ptr = ptr1;
  101. pass = 0;
  102. y1 = 0;
  103. for (y = 0; y < height; y++) {
  104. ff_lzw_decode(s->lzw, ptr, width);
  105. if (is_interleaved) {
  106. switch(pass) {
  107. default:
  108. case 0:
  109. case 1:
  110. y1 += 8;
  111. ptr += linesize * 8;
  112. break;
  113. case 2:
  114. y1 += 4;
  115. ptr += linesize * 4;
  116. break;
  117. case 3:
  118. y1 += 2;
  119. ptr += linesize * 2;
  120. break;
  121. }
  122. while (y1 >= height) {
  123. y1 = 4 >> pass;
  124. ptr = ptr1 + linesize * y1;
  125. pass++;
  126. }
  127. } else {
  128. ptr += linesize;
  129. }
  130. }
  131. /* read the garbage data until end marker is found */
  132. ff_lzw_decode_tail(s->lzw);
  133. bytestream2_skip(&s->gb, ff_lzw_size_read(s->lzw));
  134. return 0;
  135. }
  136. static int gif_read_extension(GifState *s)
  137. {
  138. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  139. /* extension */
  140. ext_code = bytestream2_get_byte(&s->gb);
  141. ext_len = bytestream2_get_byte(&s->gb);
  142. ff_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  143. switch(ext_code) {
  144. case 0xf9:
  145. if (ext_len != 4)
  146. goto discard_ext;
  147. s->transparent_color_index = -1;
  148. gce_flags = bytestream2_get_byte(&s->gb);
  149. s->gce_delay = bytestream2_get_le16(&s->gb);
  150. gce_transparent_index = bytestream2_get_byte(&s->gb);
  151. if (gce_flags & 0x01)
  152. s->transparent_color_index = gce_transparent_index;
  153. else
  154. s->transparent_color_index = -1;
  155. s->gce_disposal = (gce_flags >> 2) & 0x7;
  156. ff_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  157. gce_flags, s->gce_delay,
  158. s->transparent_color_index, s->gce_disposal);
  159. ext_len = bytestream2_get_byte(&s->gb);
  160. break;
  161. }
  162. /* NOTE: many extension blocks can come after */
  163. discard_ext:
  164. while (ext_len != 0) {
  165. for (i = 0; i < ext_len; i++)
  166. bytestream2_get_byte(&s->gb);
  167. ext_len = bytestream2_get_byte(&s->gb);
  168. ff_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
  169. }
  170. return 0;
  171. }
  172. static int gif_read_header1(GifState *s)
  173. {
  174. uint8_t sig[6];
  175. int v, n;
  176. int has_global_palette;
  177. if (bytestream2_get_bytes_left(&s->gb) < 13)
  178. return AVERROR_INVALIDDATA;
  179. /* read gif signature */
  180. bytestream2_get_buffer(&s->gb, sig, 6);
  181. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  182. memcmp(sig, gif89a_sig, 6) != 0)
  183. return AVERROR_INVALIDDATA;
  184. /* read screen header */
  185. s->transparent_color_index = -1;
  186. s->screen_width = bytestream2_get_le16(&s->gb);
  187. s->screen_height = bytestream2_get_le16(&s->gb);
  188. if( (unsigned)s->screen_width > 32767
  189. || (unsigned)s->screen_height > 32767){
  190. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  191. return AVERROR_INVALIDDATA;
  192. }
  193. v = bytestream2_get_byte(&s->gb);
  194. s->color_resolution = ((v & 0x70) >> 4) + 1;
  195. has_global_palette = (v & 0x80);
  196. s->bits_per_pixel = (v & 0x07) + 1;
  197. s->background_color_index = bytestream2_get_byte(&s->gb);
  198. bytestream2_get_byte(&s->gb); /* ignored */
  199. ff_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  200. s->screen_width, s->screen_height, s->bits_per_pixel,
  201. has_global_palette);
  202. if (has_global_palette) {
  203. n = 1 << s->bits_per_pixel;
  204. if (bytestream2_get_bytes_left(&s->gb) < n * 3)
  205. return AVERROR_INVALIDDATA;
  206. bytestream2_get_buffer(&s->gb, s->global_palette, n * 3);
  207. }
  208. return 0;
  209. }
  210. static int gif_parse_next_image(GifState *s, AVFrame *frame)
  211. {
  212. while (bytestream2_get_bytes_left(&s->gb) > 0) {
  213. int code = bytestream2_get_byte(&s->gb);
  214. int ret;
  215. ff_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
  216. switch (code) {
  217. case ',':
  218. return gif_read_image(s, frame);
  219. case '!':
  220. if ((ret = gif_read_extension(s)) < 0)
  221. return ret;
  222. break;
  223. case ';':
  224. /* end of image */
  225. default:
  226. /* error or erroneous EOF */
  227. return AVERROR_INVALIDDATA;
  228. }
  229. }
  230. return AVERROR_INVALIDDATA;
  231. }
  232. static av_cold int gif_decode_init(AVCodecContext *avctx)
  233. {
  234. GifState *s = avctx->priv_data;
  235. s->avctx = avctx;
  236. ff_lzw_decode_open(&s->lzw);
  237. return 0;
  238. }
  239. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  240. AVPacket *avpkt)
  241. {
  242. const uint8_t *buf = avpkt->data;
  243. int buf_size = avpkt->size;
  244. GifState *s = avctx->priv_data;
  245. AVFrame *picture = data;
  246. int ret;
  247. bytestream2_init(&s->gb, buf, buf_size);
  248. if ((ret = gif_read_header1(s)) < 0)
  249. return ret;
  250. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  251. if ((ret = ff_set_dimensions(avctx, s->screen_width, s->screen_height)) < 0)
  252. return ret;
  253. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
  254. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  255. return ret;
  256. }
  257. s->image_palette = (uint32_t *)picture->data[1];
  258. ret = gif_parse_next_image(s, picture);
  259. if (ret < 0)
  260. return ret;
  261. *got_frame = 1;
  262. return bytestream2_tell(&s->gb);
  263. }
  264. static av_cold int gif_decode_close(AVCodecContext *avctx)
  265. {
  266. GifState *s = avctx->priv_data;
  267. ff_lzw_decode_close(&s->lzw);
  268. return 0;
  269. }
  270. AVCodec ff_gif_decoder = {
  271. .name = "gif",
  272. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  273. .type = AVMEDIA_TYPE_VIDEO,
  274. .id = AV_CODEC_ID_GIF,
  275. .priv_data_size = sizeof(GifState),
  276. .init = gif_decode_init,
  277. .close = gif_decode_close,
  278. .decode = gif_decode_frame,
  279. .capabilities = AV_CODEC_CAP_DR1,
  280. };