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.

333 lines
9.7KB

  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. av_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. if (y1 >= height) {
  113. y1 = pass ? 2 : 4;
  114. ptr = ptr1 + linesize * y1;
  115. pass++;
  116. }
  117. break;
  118. case 2:
  119. y1 += 4;
  120. ptr += linesize * 4;
  121. if (y1 >= height) {
  122. y1 = 1;
  123. ptr = ptr1 + linesize;
  124. pass++;
  125. }
  126. break;
  127. case 3:
  128. y1 += 2;
  129. ptr += linesize * 2;
  130. break;
  131. }
  132. } else {
  133. ptr += linesize;
  134. }
  135. }
  136. /* read the garbage data until end marker is found */
  137. ff_lzw_decode_tail(s->lzw);
  138. bytestream2_skip(&s->gb, ff_lzw_size_read(s->lzw));
  139. return 0;
  140. }
  141. static int gif_read_extension(GifState *s)
  142. {
  143. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  144. /* extension */
  145. ext_code = bytestream2_get_byte(&s->gb);
  146. ext_len = bytestream2_get_byte(&s->gb);
  147. av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  148. switch(ext_code) {
  149. case 0xf9:
  150. if (ext_len != 4)
  151. goto discard_ext;
  152. s->transparent_color_index = -1;
  153. gce_flags = bytestream2_get_byte(&s->gb);
  154. s->gce_delay = bytestream2_get_le16(&s->gb);
  155. gce_transparent_index = bytestream2_get_byte(&s->gb);
  156. if (gce_flags & 0x01)
  157. s->transparent_color_index = gce_transparent_index;
  158. else
  159. s->transparent_color_index = -1;
  160. s->gce_disposal = (gce_flags >> 2) & 0x7;
  161. av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  162. gce_flags, s->gce_delay,
  163. s->transparent_color_index, s->gce_disposal);
  164. ext_len = bytestream2_get_byte(&s->gb);
  165. break;
  166. }
  167. /* NOTE: many extension blocks can come after */
  168. discard_ext:
  169. while (ext_len != 0) {
  170. for (i = 0; i < ext_len; i++)
  171. bytestream2_get_byte(&s->gb);
  172. ext_len = bytestream2_get_byte(&s->gb);
  173. av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
  174. }
  175. return 0;
  176. }
  177. static int gif_read_header1(GifState *s)
  178. {
  179. uint8_t sig[6];
  180. int v, n;
  181. int has_global_palette;
  182. if (bytestream2_get_bytes_left(&s->gb) < 13)
  183. return AVERROR_INVALIDDATA;
  184. /* read gif signature */
  185. bytestream2_get_buffer(&s->gb, sig, 6);
  186. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  187. memcmp(sig, gif89a_sig, 6) != 0)
  188. return AVERROR_INVALIDDATA;
  189. /* read screen header */
  190. s->transparent_color_index = -1;
  191. s->screen_width = bytestream2_get_le16(&s->gb);
  192. s->screen_height = bytestream2_get_le16(&s->gb);
  193. if( (unsigned)s->screen_width > 32767
  194. || (unsigned)s->screen_height > 32767){
  195. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  196. return AVERROR_INVALIDDATA;
  197. }
  198. v = bytestream2_get_byte(&s->gb);
  199. s->color_resolution = ((v & 0x70) >> 4) + 1;
  200. has_global_palette = (v & 0x80);
  201. s->bits_per_pixel = (v & 0x07) + 1;
  202. s->background_color_index = bytestream2_get_byte(&s->gb);
  203. bytestream2_get_byte(&s->gb); /* ignored */
  204. av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  205. s->screen_width, s->screen_height, s->bits_per_pixel,
  206. has_global_palette);
  207. if (has_global_palette) {
  208. n = 1 << s->bits_per_pixel;
  209. if (bytestream2_get_bytes_left(&s->gb) < n * 3)
  210. return AVERROR_INVALIDDATA;
  211. bytestream2_get_buffer(&s->gb, s->global_palette, n * 3);
  212. }
  213. return 0;
  214. }
  215. static int gif_parse_next_image(GifState *s, AVFrame *frame)
  216. {
  217. while (bytestream2_get_bytes_left(&s->gb) > 0) {
  218. int code = bytestream2_get_byte(&s->gb);
  219. int ret;
  220. av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
  221. switch (code) {
  222. case ',':
  223. return gif_read_image(s, frame);
  224. case '!':
  225. if ((ret = gif_read_extension(s)) < 0)
  226. return ret;
  227. break;
  228. case ';':
  229. /* end of image */
  230. default:
  231. /* error or erroneous EOF */
  232. return AVERROR_INVALIDDATA;
  233. }
  234. }
  235. return AVERROR_INVALIDDATA;
  236. }
  237. static av_cold int gif_decode_init(AVCodecContext *avctx)
  238. {
  239. GifState *s = avctx->priv_data;
  240. s->avctx = avctx;
  241. ff_lzw_decode_open(&s->lzw);
  242. return 0;
  243. }
  244. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  245. AVPacket *avpkt)
  246. {
  247. const uint8_t *buf = avpkt->data;
  248. int buf_size = avpkt->size;
  249. GifState *s = avctx->priv_data;
  250. AVFrame *picture = data;
  251. int ret;
  252. bytestream2_init(&s->gb, buf, buf_size);
  253. if ((ret = gif_read_header1(s)) < 0)
  254. return ret;
  255. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  256. if ((ret = ff_set_dimensions(avctx, s->screen_width, s->screen_height)) < 0)
  257. return ret;
  258. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
  259. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  260. return ret;
  261. }
  262. s->image_palette = (uint32_t *)picture->data[1];
  263. ret = gif_parse_next_image(s, picture);
  264. if (ret < 0)
  265. return ret;
  266. *got_frame = 1;
  267. return bytestream2_tell(&s->gb);
  268. }
  269. static av_cold int gif_decode_close(AVCodecContext *avctx)
  270. {
  271. GifState *s = avctx->priv_data;
  272. ff_lzw_decode_close(&s->lzw);
  273. return 0;
  274. }
  275. AVCodec ff_gif_decoder = {
  276. .name = "gif",
  277. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  278. .type = AVMEDIA_TYPE_VIDEO,
  279. .id = AV_CODEC_ID_GIF,
  280. .priv_data_size = sizeof(GifState),
  281. .init = gif_decode_init,
  282. .close = gif_decode_close,
  283. .decode = gif_decode_frame,
  284. .capabilities = CODEC_CAP_DR1,
  285. };