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.

344 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 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. 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)
  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 = bytestream_get_le16(&s->bytestream);
  59. top = bytestream_get_le16(&s->bytestream);
  60. width = bytestream_get_le16(&s->bytestream);
  61. height = bytestream_get_le16(&s->bytestream);
  62. flags = bytestream_get_byte(&s->bytestream);
  63. is_interleaved = flags & 0x40;
  64. has_local_palette = flags & 0x80;
  65. bits_per_pixel = (flags & 0x07) + 1;
  66. #ifdef DEBUG
  67. dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  68. #endif
  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] = (0xff << 24) |
  85. (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
  86. spal += 3;
  87. }
  88. for(; i < 256; i++)
  89. s->image_palette[i] = (0xff << 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 = 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;
  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. /* read the garbage data until end marker is found */
  140. ff_lzw_decode_tail(s->lzw);
  141. s->bytestream = ff_lzw_cur_ptr(s->lzw);
  142. return 0;
  143. }
  144. static int gif_read_extension(GifState *s)
  145. {
  146. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  147. /* extension */
  148. ext_code = bytestream_get_byte(&s->bytestream);
  149. ext_len = bytestream_get_byte(&s->bytestream);
  150. #ifdef DEBUG
  151. dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  152. #endif
  153. switch(ext_code) {
  154. case 0xf9:
  155. if (ext_len != 4)
  156. goto discard_ext;
  157. s->transparent_color_index = -1;
  158. gce_flags = bytestream_get_byte(&s->bytestream);
  159. s->gce_delay = bytestream_get_le16(&s->bytestream);
  160. gce_transparent_index = bytestream_get_byte(&s->bytestream);
  161. if (gce_flags & 0x01)
  162. s->transparent_color_index = gce_transparent_index;
  163. else
  164. s->transparent_color_index = -1;
  165. s->gce_disposal = (gce_flags >> 2) & 0x7;
  166. #ifdef DEBUG
  167. dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  168. gce_flags, s->gce_delay,
  169. s->transparent_color_index, s->gce_disposal);
  170. #endif
  171. ext_len = bytestream_get_byte(&s->bytestream);
  172. break;
  173. }
  174. /* NOTE: many extension blocks can come after */
  175. discard_ext:
  176. while (ext_len != 0) {
  177. for (i = 0; i < ext_len; i++)
  178. bytestream_get_byte(&s->bytestream);
  179. ext_len = bytestream_get_byte(&s->bytestream);
  180. #ifdef DEBUG
  181. dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
  182. #endif
  183. }
  184. return 0;
  185. }
  186. static int gif_read_header1(GifState *s)
  187. {
  188. uint8_t sig[6];
  189. int v, n;
  190. int has_global_palette;
  191. /* read gif signature */
  192. bytestream_get_buffer(&s->bytestream, sig, 6);
  193. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  194. memcmp(sig, gif89a_sig, 6) != 0)
  195. return -1;
  196. /* read screen header */
  197. s->transparent_color_index = -1;
  198. s->screen_width = bytestream_get_le16(&s->bytestream);
  199. s->screen_height = bytestream_get_le16(&s->bytestream);
  200. if( (unsigned)s->screen_width > 32767
  201. || (unsigned)s->screen_height > 32767){
  202. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  203. return -1;
  204. }
  205. v = bytestream_get_byte(&s->bytestream);
  206. s->color_resolution = ((v & 0x70) >> 4) + 1;
  207. has_global_palette = (v & 0x80);
  208. s->bits_per_pixel = (v & 0x07) + 1;
  209. s->background_color_index = bytestream_get_byte(&s->bytestream);
  210. bytestream_get_byte(&s->bytestream); /* ignored */
  211. #ifdef DEBUG
  212. dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  213. s->screen_width, s->screen_height, s->bits_per_pixel,
  214. has_global_palette);
  215. #endif
  216. if (has_global_palette) {
  217. n = 1 << s->bits_per_pixel;
  218. bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
  219. }
  220. return 0;
  221. }
  222. static int gif_parse_next_image(GifState *s)
  223. {
  224. int ret, code;
  225. for (;;) {
  226. code = bytestream_get_byte(&s->bytestream);
  227. #ifdef DEBUG
  228. dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
  229. #endif
  230. switch (code) {
  231. case ',':
  232. if (gif_read_image(s) < 0)
  233. return -1;
  234. ret = 0;
  235. goto the_end;
  236. case ';':
  237. /* end of image */
  238. ret = -1;
  239. goto the_end;
  240. case '!':
  241. if (gif_read_extension(s) < 0)
  242. return -1;
  243. break;
  244. case EOF:
  245. default:
  246. /* error or errneous EOF */
  247. ret = -1;
  248. goto the_end;
  249. }
  250. }
  251. the_end:
  252. return ret;
  253. }
  254. static int gif_decode_init(AVCodecContext *avctx)
  255. {
  256. GifState *s = avctx->priv_data;
  257. s->avctx = avctx;
  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. };