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.

331 lines
9.8KB

  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. const uint8_t *bytestream;
  45. const uint8_t *bytestream_end;
  46. LZWState *lzw;
  47. /* aux buffers */
  48. uint8_t global_palette[256 * 3];
  49. uint8_t local_palette[256 * 3];
  50. AVCodecContext* avctx;
  51. } GifState;
  52. static const uint8_t gif87a_sig[6] = "GIF87a";
  53. static const uint8_t gif89a_sig[6] = "GIF89a";
  54. static int gif_read_image(GifState *s, AVFrame *frame)
  55. {
  56. int left, top, width, height, bits_per_pixel, code_size, flags;
  57. int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
  58. uint8_t *ptr, *spal, *palette, *ptr1;
  59. left = bytestream_get_le16(&s->bytestream);
  60. top = bytestream_get_le16(&s->bytestream);
  61. width = bytestream_get_le16(&s->bytestream);
  62. height = bytestream_get_le16(&s->bytestream);
  63. flags = bytestream_get_byte(&s->bytestream);
  64. is_interleaved = flags & 0x40;
  65. has_local_palette = flags & 0x80;
  66. bits_per_pixel = (flags & 0x07) + 1;
  67. av_dlog(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  68. if (has_local_palette) {
  69. bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
  70. palette = s->local_palette;
  71. } else {
  72. palette = s->global_palette;
  73. bits_per_pixel = s->bits_per_pixel;
  74. }
  75. /* verify that all the image is inside the screen dimensions */
  76. if (left + width > s->screen_width ||
  77. top + height > s->screen_height)
  78. return AVERROR(EINVAL);
  79. /* build the palette */
  80. n = (1 << bits_per_pixel);
  81. spal = palette;
  82. for(i = 0; i < n; i++) {
  83. s->image_palette[i] = (0xffu << 24) | AV_RB24(spal);
  84. spal += 3;
  85. }
  86. for(; i < 256; i++)
  87. s->image_palette[i] = (0xffu << 24);
  88. /* handle transparency */
  89. if (s->transparent_color_index >= 0)
  90. s->image_palette[s->transparent_color_index] = 0;
  91. /* now get the image data */
  92. code_size = bytestream_get_byte(&s->bytestream);
  93. ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
  94. s->bytestream_end - s->bytestream, FF_LZW_GIF);
  95. /* read all the image */
  96. linesize = frame->linesize[0];
  97. ptr1 = frame->data[0] + top * linesize + left;
  98. ptr = ptr1;
  99. pass = 0;
  100. y1 = 0;
  101. for (y = 0; y < height; y++) {
  102. ff_lzw_decode(s->lzw, ptr, width);
  103. if (is_interleaved) {
  104. switch(pass) {
  105. default:
  106. case 0:
  107. case 1:
  108. y1 += 8;
  109. ptr += linesize * 8;
  110. if (y1 >= height) {
  111. y1 = pass ? 2 : 4;
  112. ptr = ptr1 + linesize * y1;
  113. pass++;
  114. }
  115. break;
  116. case 2:
  117. y1 += 4;
  118. ptr += linesize * 4;
  119. if (y1 >= height) {
  120. y1 = 1;
  121. ptr = ptr1 + linesize;
  122. pass++;
  123. }
  124. break;
  125. case 3:
  126. y1 += 2;
  127. ptr += linesize * 2;
  128. break;
  129. }
  130. } else {
  131. ptr += linesize;
  132. }
  133. }
  134. /* read the garbage data until end marker is found */
  135. ff_lzw_decode_tail(s->lzw);
  136. s->bytestream = ff_lzw_cur_ptr(s->lzw);
  137. return 0;
  138. }
  139. static int gif_read_extension(GifState *s)
  140. {
  141. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  142. /* extension */
  143. ext_code = bytestream_get_byte(&s->bytestream);
  144. ext_len = bytestream_get_byte(&s->bytestream);
  145. av_dlog(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  146. switch(ext_code) {
  147. case 0xf9:
  148. if (ext_len != 4)
  149. goto discard_ext;
  150. s->transparent_color_index = -1;
  151. gce_flags = bytestream_get_byte(&s->bytestream);
  152. s->gce_delay = bytestream_get_le16(&s->bytestream);
  153. gce_transparent_index = bytestream_get_byte(&s->bytestream);
  154. if (gce_flags & 0x01)
  155. s->transparent_color_index = gce_transparent_index;
  156. else
  157. s->transparent_color_index = -1;
  158. s->gce_disposal = (gce_flags >> 2) & 0x7;
  159. av_dlog(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  160. gce_flags, s->gce_delay,
  161. s->transparent_color_index, s->gce_disposal);
  162. ext_len = bytestream_get_byte(&s->bytestream);
  163. break;
  164. }
  165. /* NOTE: many extension blocks can come after */
  166. discard_ext:
  167. while (ext_len != 0) {
  168. for (i = 0; i < ext_len; i++)
  169. bytestream_get_byte(&s->bytestream);
  170. ext_len = bytestream_get_byte(&s->bytestream);
  171. av_dlog(s->avctx, "gif: ext_len1=%d\n", ext_len);
  172. }
  173. return 0;
  174. }
  175. static int gif_read_header1(GifState *s)
  176. {
  177. uint8_t sig[6];
  178. int v, n;
  179. int has_global_palette;
  180. if (s->bytestream_end < s->bytestream + 13)
  181. return AVERROR_INVALIDDATA;
  182. /* read gif signature */
  183. bytestream_get_buffer(&s->bytestream, sig, 6);
  184. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  185. memcmp(sig, gif89a_sig, 6) != 0)
  186. return AVERROR_INVALIDDATA;
  187. /* read screen header */
  188. s->transparent_color_index = -1;
  189. s->screen_width = bytestream_get_le16(&s->bytestream);
  190. s->screen_height = bytestream_get_le16(&s->bytestream);
  191. if( (unsigned)s->screen_width > 32767
  192. || (unsigned)s->screen_height > 32767){
  193. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  194. return AVERROR_INVALIDDATA;
  195. }
  196. v = bytestream_get_byte(&s->bytestream);
  197. s->color_resolution = ((v & 0x70) >> 4) + 1;
  198. has_global_palette = (v & 0x80);
  199. s->bits_per_pixel = (v & 0x07) + 1;
  200. s->background_color_index = bytestream_get_byte(&s->bytestream);
  201. bytestream_get_byte(&s->bytestream); /* ignored */
  202. av_dlog(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  203. s->screen_width, s->screen_height, s->bits_per_pixel,
  204. has_global_palette);
  205. if (has_global_palette) {
  206. n = 1 << s->bits_per_pixel;
  207. if (s->bytestream_end < s->bytestream + n * 3)
  208. return AVERROR_INVALIDDATA;
  209. bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
  210. }
  211. return 0;
  212. }
  213. static int gif_parse_next_image(GifState *s, AVFrame *frame)
  214. {
  215. while (s->bytestream < s->bytestream_end) {
  216. int code = bytestream_get_byte(&s->bytestream);
  217. int ret;
  218. av_dlog(s->avctx, "gif: code=%02x '%c'\n", code, code);
  219. switch (code) {
  220. case ',':
  221. return gif_read_image(s, frame);
  222. case '!':
  223. if ((ret = gif_read_extension(s)) < 0)
  224. return ret;
  225. break;
  226. case ';':
  227. /* end of image */
  228. default:
  229. /* error or erroneous EOF */
  230. return AVERROR_INVALIDDATA;
  231. }
  232. }
  233. return AVERROR_INVALIDDATA;
  234. }
  235. static av_cold int gif_decode_init(AVCodecContext *avctx)
  236. {
  237. GifState *s = avctx->priv_data;
  238. s->avctx = avctx;
  239. ff_lzw_decode_open(&s->lzw);
  240. return 0;
  241. }
  242. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  243. AVPacket *avpkt)
  244. {
  245. const uint8_t *buf = avpkt->data;
  246. int buf_size = avpkt->size;
  247. GifState *s = avctx->priv_data;
  248. AVFrame *picture = data;
  249. int ret;
  250. s->bytestream = buf;
  251. s->bytestream_end = buf + buf_size;
  252. if ((ret = gif_read_header1(s)) < 0)
  253. return ret;
  254. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  255. if ((ret = av_image_check_size(s->screen_width, s->screen_height, 0, avctx)) < 0)
  256. return ret;
  257. avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
  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 s->bytestream - buf;
  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. .type = AVMEDIA_TYPE_VIDEO,
  278. .id = AV_CODEC_ID_GIF,
  279. .priv_data_size = sizeof(GifState),
  280. .init = gif_decode_init,
  281. .close = gif_decode_close,
  282. .decode = gif_decode_frame,
  283. .capabilities = CODEC_CAP_DR1,
  284. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  285. };