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.

342 lines
9.9KB

  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 "libavcore/imgutils.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "lzw.h"
  27. #define GCE_DISPOSAL_NONE 0
  28. #define GCE_DISPOSAL_INPLACE 1
  29. #define GCE_DISPOSAL_BACKGROUND 2
  30. #define GCE_DISPOSAL_RESTORE 3
  31. typedef struct GifState {
  32. AVFrame picture;
  33. int screen_width;
  34. int screen_height;
  35. int bits_per_pixel;
  36. int background_color_index;
  37. int transparent_color_index;
  38. int color_resolution;
  39. uint32_t *image_palette;
  40. /* after the frame is displayed, the disposal method is used */
  41. int gce_disposal;
  42. /* delay during which the frame is shown */
  43. int gce_delay;
  44. /* LZW compatible decoder */
  45. const uint8_t *bytestream;
  46. const uint8_t *bytestream_end;
  47. LZWState *lzw;
  48. /* aux buffers */
  49. uint8_t global_palette[256 * 3];
  50. uint8_t local_palette[256 * 3];
  51. AVCodecContext* avctx;
  52. } GifState;
  53. static const uint8_t gif87a_sig[6] = "GIF87a";
  54. static const uint8_t gif89a_sig[6] = "GIF89a";
  55. static int gif_read_image(GifState *s)
  56. {
  57. int left, top, width, height, bits_per_pixel, code_size, flags;
  58. int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
  59. uint8_t *ptr, *spal, *palette, *ptr1;
  60. left = bytestream_get_le16(&s->bytestream);
  61. top = bytestream_get_le16(&s->bytestream);
  62. width = bytestream_get_le16(&s->bytestream);
  63. height = bytestream_get_le16(&s->bytestream);
  64. flags = bytestream_get_byte(&s->bytestream);
  65. is_interleaved = flags & 0x40;
  66. has_local_palette = flags & 0x80;
  67. bits_per_pixel = (flags & 0x07) + 1;
  68. #ifdef DEBUG
  69. dprintf(s->avctx, "gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  70. #endif
  71. if (has_local_palette) {
  72. bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
  73. palette = s->local_palette;
  74. } else {
  75. palette = s->global_palette;
  76. bits_per_pixel = s->bits_per_pixel;
  77. }
  78. /* verify that all the image is inside the screen dimensions */
  79. if (left + width > s->screen_width ||
  80. top + height > s->screen_height)
  81. return AVERROR(EINVAL);
  82. /* build the palette */
  83. n = (1 << bits_per_pixel);
  84. spal = palette;
  85. for(i = 0; i < n; i++) {
  86. s->image_palette[i] = (0xff << 24) | AV_RB24(spal);
  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 = pass ? 2 : 4;
  115. ptr = ptr1 + linesize * y1;
  116. pass++;
  117. }
  118. break;
  119. case 2:
  120. y1 += 4;
  121. ptr += linesize * 4;
  122. if (y1 >= height) {
  123. y1 = 1;
  124. ptr = ptr1 + linesize;
  125. pass++;
  126. }
  127. break;
  128. case 3:
  129. y1 += 2;
  130. ptr += linesize * 2;
  131. break;
  132. }
  133. } else {
  134. ptr += linesize;
  135. }
  136. }
  137. /* read the garbage data until end marker is found */
  138. ff_lzw_decode_tail(s->lzw);
  139. s->bytestream = ff_lzw_cur_ptr(s->lzw);
  140. return 0;
  141. }
  142. static int gif_read_extension(GifState *s)
  143. {
  144. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  145. /* extension */
  146. ext_code = bytestream_get_byte(&s->bytestream);
  147. ext_len = bytestream_get_byte(&s->bytestream);
  148. #ifdef DEBUG
  149. dprintf(s->avctx, "gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  150. #endif
  151. switch(ext_code) {
  152. case 0xf9:
  153. if (ext_len != 4)
  154. goto discard_ext;
  155. s->transparent_color_index = -1;
  156. gce_flags = bytestream_get_byte(&s->bytestream);
  157. s->gce_delay = bytestream_get_le16(&s->bytestream);
  158. gce_transparent_index = bytestream_get_byte(&s->bytestream);
  159. if (gce_flags & 0x01)
  160. s->transparent_color_index = gce_transparent_index;
  161. else
  162. s->transparent_color_index = -1;
  163. s->gce_disposal = (gce_flags >> 2) & 0x7;
  164. #ifdef DEBUG
  165. dprintf(s->avctx, "gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  166. gce_flags, s->gce_delay,
  167. s->transparent_color_index, s->gce_disposal);
  168. #endif
  169. ext_len = bytestream_get_byte(&s->bytestream);
  170. break;
  171. }
  172. /* NOTE: many extension blocks can come after */
  173. discard_ext:
  174. while (ext_len != 0) {
  175. for (i = 0; i < ext_len; i++)
  176. bytestream_get_byte(&s->bytestream);
  177. ext_len = bytestream_get_byte(&s->bytestream);
  178. #ifdef DEBUG
  179. dprintf(s->avctx, "gif: ext_len1=%d\n", ext_len);
  180. #endif
  181. }
  182. return 0;
  183. }
  184. static int gif_read_header1(GifState *s)
  185. {
  186. uint8_t sig[6];
  187. int v, n;
  188. int has_global_palette;
  189. if (s->bytestream_end < s->bytestream + 13)
  190. return -1;
  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. if (s->bytestream_end < s->bytestream + n * 3)
  219. return -1;
  220. bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
  221. }
  222. return 0;
  223. }
  224. static int gif_parse_next_image(GifState *s)
  225. {
  226. while (s->bytestream < s->bytestream_end) {
  227. int code = bytestream_get_byte(&s->bytestream);
  228. #ifdef DEBUG
  229. dprintf(s->avctx, "gif: code=%02x '%c'\n", code, code);
  230. #endif
  231. switch (code) {
  232. case ',':
  233. return gif_read_image(s);
  234. case '!':
  235. if (gif_read_extension(s) < 0)
  236. return -1;
  237. break;
  238. case ';':
  239. /* end of image */
  240. default:
  241. /* error or erroneous EOF */
  242. return -1;
  243. }
  244. }
  245. return -1;
  246. }
  247. static av_cold int gif_decode_init(AVCodecContext *avctx)
  248. {
  249. GifState *s = avctx->priv_data;
  250. s->avctx = avctx;
  251. avcodec_get_frame_defaults(&s->picture);
  252. avctx->coded_frame= &s->picture;
  253. s->picture.data[0] = NULL;
  254. ff_lzw_decode_open(&s->lzw);
  255. return 0;
  256. }
  257. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  258. {
  259. const uint8_t *buf = avpkt->data;
  260. int buf_size = avpkt->size;
  261. GifState *s = avctx->priv_data;
  262. AVFrame *picture = data;
  263. int ret;
  264. s->bytestream = buf;
  265. s->bytestream_end = buf + buf_size;
  266. if (gif_read_header1(s) < 0)
  267. return -1;
  268. avctx->pix_fmt = PIX_FMT_PAL8;
  269. if (av_check_image_size(s->screen_width, s->screen_height, 0, avctx))
  270. return -1;
  271. avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
  272. if (s->picture.data[0])
  273. avctx->release_buffer(avctx, &s->picture);
  274. if (avctx->get_buffer(avctx, &s->picture) < 0) {
  275. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  276. return -1;
  277. }
  278. s->image_palette = (uint32_t *)s->picture.data[1];
  279. ret = gif_parse_next_image(s);
  280. if (ret < 0)
  281. return ret;
  282. *picture = s->picture;
  283. *data_size = sizeof(AVPicture);
  284. return s->bytestream - buf;
  285. }
  286. static av_cold int gif_decode_close(AVCodecContext *avctx)
  287. {
  288. GifState *s = avctx->priv_data;
  289. ff_lzw_decode_close(&s->lzw);
  290. if(s->picture.data[0])
  291. avctx->release_buffer(avctx, &s->picture);
  292. return 0;
  293. }
  294. AVCodec gif_decoder = {
  295. "gif",
  296. AVMEDIA_TYPE_VIDEO,
  297. CODEC_ID_GIF,
  298. sizeof(GifState),
  299. gif_decode_init,
  300. NULL,
  301. gif_decode_close,
  302. gif_decode_frame,
  303. CODEC_CAP_DR1,
  304. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  305. };