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
10KB

  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 "libavutil/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. int ret;
  61. left = bytestream_get_le16(&s->bytestream);
  62. top = bytestream_get_le16(&s->bytestream);
  63. width = bytestream_get_le16(&s->bytestream);
  64. height = bytestream_get_le16(&s->bytestream);
  65. flags = bytestream_get_byte(&s->bytestream);
  66. is_interleaved = flags & 0x40;
  67. has_local_palette = flags & 0x80;
  68. bits_per_pixel = (flags & 0x07) + 1;
  69. av_dlog(s->avctx, "image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  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] = (0xffu << 24) | AV_RB24(spal);
  86. spal += 3;
  87. }
  88. for(; i < 256; i++)
  89. s->image_palette[i] = (0xffu << 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. if ((ret = ff_lzw_decode_init(s->lzw, code_size, s->bytestream,
  96. s->bytestream_end - s->bytestream, FF_LZW_GIF)) < 0) {
  97. av_log(s->avctx, AV_LOG_ERROR, "LZW init failed\n");
  98. return ret;
  99. }
  100. /* read all the image */
  101. linesize = s->picture.linesize[0];
  102. ptr1 = s->picture.data[0] + top * linesize + left;
  103. ptr = ptr1;
  104. pass = 0;
  105. y1 = 0;
  106. for (y = 0; y < height; y++) {
  107. ff_lzw_decode(s->lzw, ptr, width);
  108. if (is_interleaved) {
  109. switch(pass) {
  110. default:
  111. case 0:
  112. case 1:
  113. y1 += 8;
  114. ptr += linesize * 8;
  115. if (y1 >= height) {
  116. y1 = pass ? 2 : 4;
  117. ptr = ptr1 + linesize * y1;
  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. av_dlog(s->avctx, "ext_code=0x%x len=%d\n", ext_code, ext_len);
  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. av_dlog(s->avctx, "gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  165. gce_flags, s->gce_delay,
  166. s->transparent_color_index, s->gce_disposal);
  167. ext_len = bytestream_get_byte(&s->bytestream);
  168. break;
  169. }
  170. /* NOTE: many extension blocks can come after */
  171. discard_ext:
  172. while (ext_len != 0) {
  173. for (i = 0; i < ext_len; i++)
  174. bytestream_get_byte(&s->bytestream);
  175. ext_len = bytestream_get_byte(&s->bytestream);
  176. av_dlog(s->avctx, "ext_len1=%d\n", ext_len);
  177. }
  178. return 0;
  179. }
  180. static int gif_read_header1(GifState *s)
  181. {
  182. uint8_t sig[6];
  183. int v, n;
  184. int has_global_palette;
  185. if (s->bytestream_end < s->bytestream + 13)
  186. return AVERROR_INVALIDDATA;
  187. /* read gif signature */
  188. bytestream_get_buffer(&s->bytestream, sig, 6);
  189. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  190. memcmp(sig, gif89a_sig, 6) != 0)
  191. return AVERROR_INVALIDDATA;
  192. /* read screen header */
  193. s->transparent_color_index = -1;
  194. s->screen_width = bytestream_get_le16(&s->bytestream);
  195. s->screen_height = bytestream_get_le16(&s->bytestream);
  196. if( (unsigned)s->screen_width > 32767
  197. || (unsigned)s->screen_height > 32767){
  198. av_log(s->avctx, AV_LOG_ERROR, "picture size too large\n");
  199. return AVERROR_INVALIDDATA;
  200. }
  201. v = bytestream_get_byte(&s->bytestream);
  202. s->color_resolution = ((v & 0x70) >> 4) + 1;
  203. has_global_palette = (v & 0x80);
  204. s->bits_per_pixel = (v & 0x07) + 1;
  205. s->background_color_index = bytestream_get_byte(&s->bytestream);
  206. bytestream_get_byte(&s->bytestream); /* ignored */
  207. av_dlog(s->avctx, "screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  208. s->screen_width, s->screen_height, s->bits_per_pixel,
  209. has_global_palette);
  210. if (has_global_palette) {
  211. n = 1 << s->bits_per_pixel;
  212. if (s->bytestream_end < s->bytestream + n * 3)
  213. return AVERROR_INVALIDDATA;
  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. av_dlog(s->avctx, "code=%02x '%c'\n", code, code);
  223. switch (code) {
  224. case ',':
  225. return gif_read_image(s);
  226. case '!':
  227. if (gif_read_extension(s) < 0)
  228. return -1;
  229. break;
  230. case ';':
  231. /* end of image */
  232. default:
  233. /* error or erroneous EOF */
  234. return -1;
  235. }
  236. }
  237. return -1;
  238. }
  239. static av_cold int gif_decode_init(AVCodecContext *avctx)
  240. {
  241. GifState *s = avctx->priv_data;
  242. s->avctx = avctx;
  243. avcodec_get_frame_defaults(&s->picture);
  244. avctx->coded_frame= &s->picture;
  245. s->picture.data[0] = NULL;
  246. ff_lzw_decode_open(&s->lzw);
  247. return 0;
  248. }
  249. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
  250. {
  251. const uint8_t *buf = avpkt->data;
  252. int buf_size = avpkt->size;
  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 ((ret = gif_read_header1(s)) < 0)
  259. return ret;
  260. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  261. if (av_image_check_size(s->screen_width, s->screen_height, 0, avctx))
  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 ((ret = avctx->get_buffer(avctx, &s->picture)) < 0) {
  267. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  268. return ret;
  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 ff_gif_decoder = {
  287. .name = "gif",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .id = AV_CODEC_ID_GIF,
  290. .priv_data_size = sizeof(GifState),
  291. .init = gif_decode_init,
  292. .close = gif_decode_close,
  293. .decode = gif_decode_frame,
  294. .capabilities = CODEC_CAP_DR1,
  295. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  296. };