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.

341 lines
9.8KB

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