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.

340 lines
9.6KB

  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 "avcodec.h"
  24. #include "bytestream.h"
  25. #include "lzw.h"
  26. #define GCE_DISPOSAL_NONE 0
  27. #define GCE_DISPOSAL_INPLACE 1
  28. #define GCE_DISPOSAL_BACKGROUND 2
  29. #define GCE_DISPOSAL_RESTORE 3
  30. typedef struct GifState {
  31. AVFrame picture;
  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. uint8_t *bytestream;
  45. LZWState *lzw;
  46. /* aux buffers */
  47. uint8_t global_palette[256 * 3];
  48. uint8_t local_palette[256 * 3];
  49. } GifState;
  50. static const uint8_t gif87a_sig[6] = "GIF87a";
  51. static const uint8_t gif89a_sig[6] = "GIF89a";
  52. static int gif_read_image(GifState *s)
  53. {
  54. int left, top, width, height, bits_per_pixel, code_size, flags;
  55. int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
  56. uint8_t *ptr, *spal, *palette, *ptr1;
  57. left = bytestream_get_le16(&s->bytestream);
  58. top = bytestream_get_le16(&s->bytestream);
  59. width = bytestream_get_le16(&s->bytestream);
  60. height = bytestream_get_le16(&s->bytestream);
  61. flags = bytestream_get_byte(&s->bytestream);
  62. is_interleaved = flags & 0x40;
  63. has_local_palette = flags & 0x80;
  64. bits_per_pixel = (flags & 0x07) + 1;
  65. #ifdef DEBUG
  66. dprintf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  67. #endif
  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] = (0xff << 24) |
  84. (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
  85. spal += 3;
  86. }
  87. for(; i < 256; i++)
  88. s->image_palette[i] = (0xff << 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. //TODO: add proper data size
  95. ff_lzw_decode_init(s->lzw, code_size, s->bytestream, 0, 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 = 4;
  113. if (pass == 0)
  114. ptr = ptr1 + linesize * 4;
  115. else
  116. ptr = ptr1 + linesize * 2;
  117. pass++;
  118. }
  119. break;
  120. case 2:
  121. y1 += 4;
  122. ptr += linesize * 4;
  123. if (y1 >= height) {
  124. y1 = 1;
  125. ptr = ptr1 + linesize;
  126. pass++;
  127. }
  128. break;
  129. case 3:
  130. y1 += 2;
  131. ptr += linesize * 2;
  132. break;
  133. }
  134. } else {
  135. ptr += linesize;
  136. }
  137. }
  138. /* read the garbage data until end marker is found */
  139. ff_lzw_decode_tail(s->lzw);
  140. s->bytestream = ff_lzw_cur_ptr(s->lzw);
  141. return 0;
  142. }
  143. static int gif_read_extension(GifState *s)
  144. {
  145. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  146. /* extension */
  147. ext_code = bytestream_get_byte(&s->bytestream);
  148. ext_len = bytestream_get_byte(&s->bytestream);
  149. #ifdef DEBUG
  150. dprintf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  151. #endif
  152. switch(ext_code) {
  153. case 0xf9:
  154. if (ext_len != 4)
  155. goto discard_ext;
  156. s->transparent_color_index = -1;
  157. gce_flags = bytestream_get_byte(&s->bytestream);
  158. s->gce_delay = bytestream_get_le16(&s->bytestream);
  159. gce_transparent_index = bytestream_get_byte(&s->bytestream);
  160. if (gce_flags & 0x01)
  161. s->transparent_color_index = gce_transparent_index;
  162. else
  163. s->transparent_color_index = -1;
  164. s->gce_disposal = (gce_flags >> 2) & 0x7;
  165. #ifdef DEBUG
  166. dprintf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  167. gce_flags, s->gce_delay,
  168. s->transparent_color_index, s->gce_disposal);
  169. #endif
  170. ext_len = bytestream_get_byte(&s->bytestream);
  171. break;
  172. }
  173. /* NOTE: many extension blocks can come after */
  174. discard_ext:
  175. while (ext_len != 0) {
  176. for (i = 0; i < ext_len; i++)
  177. bytestream_get_byte(&s->bytestream);
  178. ext_len = bytestream_get_byte(&s->bytestream);
  179. #ifdef DEBUG
  180. dprintf("gif: ext_len1=%d\n", ext_len);
  181. #endif
  182. }
  183. return 0;
  184. }
  185. static int gif_read_header1(GifState *s)
  186. {
  187. uint8_t sig[6];
  188. int v, n;
  189. int has_global_palette;
  190. /* read gif signature */
  191. bytestream_get_buffer(&s->bytestream, sig, 6);
  192. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  193. memcmp(sig, gif89a_sig, 6) != 0)
  194. return -1;
  195. /* read screen header */
  196. s->transparent_color_index = -1;
  197. s->screen_width = bytestream_get_le16(&s->bytestream);
  198. s->screen_height = bytestream_get_le16(&s->bytestream);
  199. if( (unsigned)s->screen_width > 32767
  200. || (unsigned)s->screen_height > 32767){
  201. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  202. return -1;
  203. }
  204. v = bytestream_get_byte(&s->bytestream);
  205. s->color_resolution = ((v & 0x70) >> 4) + 1;
  206. has_global_palette = (v & 0x80);
  207. s->bits_per_pixel = (v & 0x07) + 1;
  208. s->background_color_index = bytestream_get_byte(&s->bytestream);
  209. bytestream_get_byte(&s->bytestream); /* ignored */
  210. #ifdef DEBUG
  211. dprintf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  212. s->screen_width, s->screen_height, s->bits_per_pixel,
  213. has_global_palette);
  214. #endif
  215. if (has_global_palette) {
  216. n = 1 << s->bits_per_pixel;
  217. bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
  218. }
  219. return 0;
  220. }
  221. static int gif_parse_next_image(GifState *s)
  222. {
  223. int ret, code;
  224. for (;;) {
  225. code = bytestream_get_byte(&s->bytestream);
  226. #ifdef DEBUG
  227. dprintf("gif: code=%02x '%c'\n", code, code);
  228. #endif
  229. switch (code) {
  230. case ',':
  231. if (gif_read_image(s) < 0)
  232. return -1;
  233. ret = 0;
  234. goto the_end;
  235. case ';':
  236. /* end of image */
  237. ret = -1;
  238. goto the_end;
  239. case '!':
  240. if (gif_read_extension(s) < 0)
  241. return -1;
  242. break;
  243. case EOF:
  244. default:
  245. /* error or errneous EOF */
  246. ret = -1;
  247. goto the_end;
  248. }
  249. }
  250. the_end:
  251. return ret;
  252. }
  253. static int gif_decode_init(AVCodecContext *avctx)
  254. {
  255. GifState *s = avctx->priv_data;
  256. avcodec_get_frame_defaults(&s->picture);
  257. avctx->coded_frame= &s->picture;
  258. s->picture.data[0] = NULL;
  259. ff_lzw_decode_open(&s->lzw);
  260. return 0;
  261. }
  262. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  263. {
  264. GifState *s = avctx->priv_data;
  265. AVFrame *picture = data;
  266. int ret;
  267. s->bytestream = buf;
  268. if (gif_read_header1(s) < 0)
  269. return -1;
  270. avctx->pix_fmt = PIX_FMT_PAL8;
  271. if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
  272. return -1;
  273. avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
  274. if (s->picture.data[0])
  275. avctx->release_buffer(avctx, &s->picture);
  276. if (avctx->get_buffer(avctx, &s->picture) < 0) {
  277. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  278. return -1;
  279. }
  280. s->image_palette = (uint32_t *)s->picture.data[1];
  281. ret = gif_parse_next_image(s);
  282. if (ret < 0)
  283. return ret;
  284. *picture = s->picture;
  285. *data_size = sizeof(AVPicture);
  286. return 0;
  287. }
  288. static int gif_decode_close(AVCodecContext *avctx)
  289. {
  290. GifState *s = avctx->priv_data;
  291. ff_lzw_decode_close(&s->lzw);
  292. if(s->picture.data[0])
  293. avctx->release_buffer(avctx, &s->picture);
  294. return 0;
  295. }
  296. AVCodec gif_decoder = {
  297. "gif",
  298. CODEC_TYPE_VIDEO,
  299. CODEC_ID_GIF,
  300. sizeof(GifState),
  301. gif_decode_init,
  302. NULL,
  303. gif_decode_close,
  304. gif_decode_frame,
  305. };