gifdec.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. 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)
  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. #ifdef DEBUG
  68. dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  69. #endif
  70. if (has_local_palette) {
  71. bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
  72. palette = s->local_palette;
  73. } else {
  74. palette = s->global_palette;
  75. bits_per_pixel = s->bits_per_pixel;
  76. }
  77. /* verify that all the image is inside the screen dimensions */
  78. if (left + width > s->screen_width ||
  79. top + height > s->screen_height)
  80. return AVERROR(EINVAL);
  81. /* build the palette */
  82. n = (1 << bits_per_pixel);
  83. spal = palette;
  84. for(i = 0; i < n; i++) {
  85. s->image_palette[i] = (0xff << 24) |
  86. (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
  87. spal += 3;
  88. }
  89. for(; i < 256; i++)
  90. s->image_palette[i] = (0xff << 24);
  91. /* handle transparency */
  92. if (s->transparent_color_index >= 0)
  93. s->image_palette[s->transparent_color_index] = 0;
  94. /* now get the image data */
  95. code_size = bytestream_get_byte(&s->bytestream);
  96. ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
  97. s->bytestream_end - s->bytestream, FF_LZW_GIF);
  98. /* read all the image */
  99. linesize = s->picture.linesize[0];
  100. ptr1 = s->picture.data[0] + top * linesize + left;
  101. ptr = ptr1;
  102. pass = 0;
  103. y1 = 0;
  104. for (y = 0; y < height; y++) {
  105. ff_lzw_decode(s->lzw, ptr, width);
  106. if (is_interleaved) {
  107. switch(pass) {
  108. default:
  109. case 0:
  110. case 1:
  111. y1 += 8;
  112. ptr += linesize * 8;
  113. if (y1 >= height) {
  114. y1 = 4;
  115. if (pass == 0)
  116. ptr = ptr1 + linesize * 4;
  117. else
  118. ptr = ptr1 + linesize * 2;
  119. pass++;
  120. }
  121. break;
  122. case 2:
  123. y1 += 4;
  124. ptr += linesize * 4;
  125. if (y1 >= height) {
  126. y1 = 1;
  127. ptr = ptr1 + linesize;
  128. pass++;
  129. }
  130. break;
  131. case 3:
  132. y1 += 2;
  133. ptr += linesize * 2;
  134. break;
  135. }
  136. } else {
  137. ptr += linesize;
  138. }
  139. }
  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(s->avctx, "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(s->avctx, "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(s->avctx, "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. if (s->bytestream_end < s->bytestream + 13)
  193. return -1;
  194. /* read gif signature */
  195. bytestream_get_buffer(&s->bytestream, sig, 6);
  196. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  197. memcmp(sig, gif89a_sig, 6) != 0)
  198. return -1;
  199. /* read screen header */
  200. s->transparent_color_index = -1;
  201. s->screen_width = bytestream_get_le16(&s->bytestream);
  202. s->screen_height = bytestream_get_le16(&s->bytestream);
  203. if( (unsigned)s->screen_width > 32767
  204. || (unsigned)s->screen_height > 32767){
  205. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  206. return -1;
  207. }
  208. v = bytestream_get_byte(&s->bytestream);
  209. s->color_resolution = ((v & 0x70) >> 4) + 1;
  210. has_global_palette = (v & 0x80);
  211. s->bits_per_pixel = (v & 0x07) + 1;
  212. s->background_color_index = bytestream_get_byte(&s->bytestream);
  213. bytestream_get_byte(&s->bytestream); /* ignored */
  214. #ifdef DEBUG
  215. dprintf(s->avctx, "gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  216. s->screen_width, s->screen_height, s->bits_per_pixel,
  217. has_global_palette);
  218. #endif
  219. if (has_global_palette) {
  220. n = 1 << s->bits_per_pixel;
  221. if (s->bytestream_end < s->bytestream + n * 3)
  222. return -1;
  223. bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
  224. }
  225. return 0;
  226. }
  227. static int gif_parse_next_image(GifState *s)
  228. {
  229. while (s->bytestream < s->bytestream_end) {
  230. int code = bytestream_get_byte(&s->bytestream);
  231. #ifdef DEBUG
  232. dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
  233. #endif
  234. switch (code) {
  235. case ',':
  236. if (gif_read_image(s) < 0)
  237. return -1;
  238. return 0;
  239. case ';':
  240. /* end of image */
  241. return -1;
  242. case '!':
  243. if (gif_read_extension(s) < 0)
  244. return -1;
  245. break;
  246. default:
  247. /* error or errneous EOF */
  248. return -1;
  249. }
  250. }
  251. return -1;
  252. }
  253. static int gif_decode_init(AVCodecContext *avctx)
  254. {
  255. GifState *s = avctx->priv_data;
  256. s->avctx = avctx;
  257. avcodec_get_frame_defaults(&s->picture);
  258. avctx->coded_frame= &s->picture;
  259. s->picture.data[0] = NULL;
  260. ff_lzw_decode_open(&s->lzw);
  261. return 0;
  262. }
  263. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  264. {
  265. GifState *s = avctx->priv_data;
  266. AVFrame *picture = data;
  267. int ret;
  268. s->bytestream = buf;
  269. s->bytestream_end = buf + buf_size;
  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 s->bytestream - buf;
  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. };