gifdec.c 9.6 KB

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