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.

339 lines
10.0KB

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