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.

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