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.

501 lines
14KB

  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. #define MAXBITS 12
  26. #define SIZTABLE (1<<MAXBITS)
  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. uint8_t *bytestream;
  46. int eob_reached;
  47. uint8_t *pbuf, *ebuf;
  48. int bbits;
  49. unsigned int bbuf;
  50. int cursize; /* The current code size */
  51. int curmask;
  52. int codesize;
  53. int clear_code;
  54. int end_code;
  55. int newcodes; /* First available code */
  56. int top_slot; /* Highest code for current size */
  57. int slot; /* Last read code */
  58. int fc, oc;
  59. uint8_t *sp;
  60. uint8_t stack[SIZTABLE];
  61. uint8_t suffix[SIZTABLE];
  62. uint16_t prefix[SIZTABLE];
  63. /* aux buffers */
  64. uint8_t global_palette[256 * 3];
  65. uint8_t local_palette[256 * 3];
  66. uint8_t buf[256];
  67. } GifState;
  68. static const uint8_t gif87a_sig[6] = "GIF87a";
  69. static const uint8_t gif89a_sig[6] = "GIF89a";
  70. static const uint16_t mask[17] =
  71. {
  72. 0x0000, 0x0001, 0x0003, 0x0007,
  73. 0x000F, 0x001F, 0x003F, 0x007F,
  74. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  75. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
  76. };
  77. static void GLZWDecodeInit(GifState * s, int csize)
  78. {
  79. /* read buffer */
  80. s->eob_reached = 0;
  81. s->pbuf = s->buf;
  82. s->ebuf = s->buf;
  83. s->bbuf = 0;
  84. s->bbits = 0;
  85. /* decoder */
  86. s->codesize = csize;
  87. s->cursize = s->codesize + 1;
  88. s->curmask = mask[s->cursize];
  89. s->top_slot = 1 << s->cursize;
  90. s->clear_code = 1 << s->codesize;
  91. s->end_code = s->clear_code + 1;
  92. s->slot = s->newcodes = s->clear_code + 2;
  93. s->oc = s->fc = 0;
  94. s->sp = s->stack;
  95. }
  96. /* XXX: optimize */
  97. static inline int GetCode(GifState * s)
  98. {
  99. int c, sizbuf;
  100. uint8_t *ptr;
  101. while (s->bbits < s->cursize) {
  102. ptr = s->pbuf;
  103. if (ptr >= s->ebuf) {
  104. if (!s->eob_reached) {
  105. sizbuf = bytestream_get_byte(&s->bytestream);
  106. s->ebuf = s->buf + sizbuf;
  107. s->pbuf = s->buf;
  108. if (sizbuf > 0) {
  109. bytestream_get_buffer(&s->bytestream, s->buf, sizbuf);
  110. } else {
  111. s->eob_reached = 1;
  112. }
  113. }
  114. ptr = s->pbuf;
  115. }
  116. s->bbuf |= ptr[0] << s->bbits;
  117. ptr++;
  118. s->pbuf = ptr;
  119. s->bbits += 8;
  120. }
  121. c = s->bbuf & s->curmask;
  122. s->bbuf >>= s->cursize;
  123. s->bbits -= s->cursize;
  124. return c;
  125. }
  126. /* NOTE: the algorithm here is inspired from the LZW GIF decoder
  127. written by Steven A. Bennett in 1987. */
  128. /* return the number of byte decoded */
  129. static int GLZWDecode(GifState * s, uint8_t * buf, int len)
  130. {
  131. int l, c, code, oc, fc;
  132. uint8_t *sp;
  133. if (s->end_code < 0)
  134. return 0;
  135. l = len;
  136. sp = s->sp;
  137. oc = s->oc;
  138. fc = s->fc;
  139. while (sp > s->stack) {
  140. *buf++ = *(--sp);
  141. if ((--l) == 0)
  142. goto the_end;
  143. }
  144. for (;;) {
  145. c = GetCode(s);
  146. if (c == s->end_code) {
  147. s->end_code = -1;
  148. break;
  149. } else if (c == s->clear_code) {
  150. s->cursize = s->codesize + 1;
  151. s->curmask = mask[s->cursize];
  152. s->slot = s->newcodes;
  153. s->top_slot = 1 << s->cursize;
  154. while ((c = GetCode(s)) == s->clear_code);
  155. if (c == s->end_code) {
  156. s->end_code = -1;
  157. break;
  158. }
  159. /* test error */
  160. if (c >= s->slot)
  161. c = 0;
  162. fc = oc = c;
  163. *buf++ = c;
  164. if ((--l) == 0)
  165. break;
  166. } else {
  167. code = c;
  168. if (code >= s->slot) {
  169. *sp++ = fc;
  170. code = oc;
  171. }
  172. while (code >= s->newcodes) {
  173. *sp++ = s->suffix[code];
  174. code = s->prefix[code];
  175. }
  176. *sp++ = code;
  177. if (s->slot < s->top_slot) {
  178. s->suffix[s->slot] = fc = code;
  179. s->prefix[s->slot++] = oc;
  180. oc = c;
  181. }
  182. if (s->slot >= s->top_slot) {
  183. if (s->cursize < MAXBITS) {
  184. s->top_slot <<= 1;
  185. s->curmask = mask[++s->cursize];
  186. }
  187. }
  188. while (sp > s->stack) {
  189. *buf++ = *(--sp);
  190. if ((--l) == 0)
  191. goto the_end;
  192. }
  193. }
  194. }
  195. the_end:
  196. s->sp = sp;
  197. s->oc = oc;
  198. s->fc = fc;
  199. return len - l;
  200. }
  201. static int gif_read_image(GifState *s)
  202. {
  203. int left, top, width, height, bits_per_pixel, code_size, flags;
  204. int is_interleaved, has_local_palette, y, pass, y1, linesize, n, i;
  205. uint8_t *ptr, *line, *spal, *palette, *ptr1;
  206. left = bytestream_get_le16(&s->bytestream);
  207. top = bytestream_get_le16(&s->bytestream);
  208. width = bytestream_get_le16(&s->bytestream);
  209. height = bytestream_get_le16(&s->bytestream);
  210. flags = bytestream_get_byte(&s->bytestream);
  211. is_interleaved = flags & 0x40;
  212. has_local_palette = flags & 0x80;
  213. bits_per_pixel = (flags & 0x07) + 1;
  214. #ifdef DEBUG
  215. dprintf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  216. #endif
  217. if (has_local_palette) {
  218. bytestream_get_buffer(&s->bytestream, s->local_palette, 3 * (1 << bits_per_pixel));
  219. palette = s->local_palette;
  220. } else {
  221. palette = s->global_palette;
  222. bits_per_pixel = s->bits_per_pixel;
  223. }
  224. /* verify that all the image is inside the screen dimensions */
  225. if (left + width > s->screen_width ||
  226. top + height > s->screen_height)
  227. return -EINVAL;
  228. /* build the palette */
  229. n = (1 << bits_per_pixel);
  230. spal = palette;
  231. for(i = 0; i < n; i++) {
  232. s->image_palette[i] = (0xff << 24) |
  233. (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
  234. spal += 3;
  235. }
  236. for(; i < 256; i++)
  237. s->image_palette[i] = (0xff << 24);
  238. /* handle transparency */
  239. if (s->transparent_color_index >= 0)
  240. s->image_palette[s->transparent_color_index] = 0;
  241. line = NULL;
  242. /* now get the image data */
  243. code_size = bytestream_get_byte(&s->bytestream);
  244. GLZWDecodeInit(s, code_size);
  245. /* read all the image */
  246. linesize = s->picture.linesize[0];
  247. ptr1 = s->picture.data[0] + top * linesize + (left * 3);
  248. ptr = ptr1;
  249. pass = 0;
  250. y1 = 0;
  251. for (y = 0; y < height; y++) {
  252. GLZWDecode(s, ptr, width);
  253. if (is_interleaved) {
  254. switch(pass) {
  255. default:
  256. case 0:
  257. case 1:
  258. y1 += 8;
  259. ptr += linesize * 8;
  260. if (y1 >= height) {
  261. y1 = 4;
  262. if (pass == 0)
  263. ptr = ptr1 + linesize * 4;
  264. else
  265. ptr = ptr1 + linesize * 2;
  266. pass++;
  267. }
  268. break;
  269. case 2:
  270. y1 += 4;
  271. ptr += linesize * 4;
  272. if (y1 >= height) {
  273. y1 = 1;
  274. ptr = ptr1 + linesize;
  275. pass++;
  276. }
  277. break;
  278. case 3:
  279. y1 += 2;
  280. ptr += linesize * 2;
  281. break;
  282. }
  283. } else {
  284. ptr += linesize;
  285. }
  286. }
  287. av_free(line);
  288. /* read the garbage data until end marker is found */
  289. while (!s->eob_reached)
  290. GetCode(s);
  291. return 0;
  292. }
  293. static int gif_read_extension(GifState *s)
  294. {
  295. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  296. /* extension */
  297. ext_code = bytestream_get_byte(&s->bytestream);
  298. ext_len = bytestream_get_byte(&s->bytestream);
  299. #ifdef DEBUG
  300. dprintf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  301. #endif
  302. switch(ext_code) {
  303. case 0xf9:
  304. if (ext_len != 4)
  305. goto discard_ext;
  306. s->transparent_color_index = -1;
  307. gce_flags = bytestream_get_byte(&s->bytestream);
  308. s->gce_delay = bytestream_get_le16(&s->bytestream);
  309. gce_transparent_index = bytestream_get_byte(&s->bytestream);
  310. if (gce_flags & 0x01)
  311. s->transparent_color_index = gce_transparent_index;
  312. else
  313. s->transparent_color_index = -1;
  314. s->gce_disposal = (gce_flags >> 2) & 0x7;
  315. #ifdef DEBUG
  316. dprintf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  317. gce_flags, s->gce_delay,
  318. s->transparent_color_index, s->gce_disposal);
  319. #endif
  320. ext_len = bytestream_get_byte(&s->bytestream);
  321. break;
  322. }
  323. /* NOTE: many extension blocks can come after */
  324. discard_ext:
  325. while (ext_len != 0) {
  326. for (i = 0; i < ext_len; i++)
  327. bytestream_get_byte(&s->bytestream);
  328. ext_len = bytestream_get_byte(&s->bytestream);
  329. #ifdef DEBUG
  330. dprintf("gif: ext_len1=%d\n", ext_len);
  331. #endif
  332. }
  333. return 0;
  334. }
  335. static int gif_read_header1(GifState *s)
  336. {
  337. uint8_t sig[6];
  338. int v, n;
  339. int has_global_palette;
  340. /* read gif signature */
  341. bytestream_get_buffer(&s->bytestream, sig, 6);
  342. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  343. memcmp(sig, gif89a_sig, 6) != 0)
  344. return -1;
  345. /* read screen header */
  346. s->transparent_color_index = -1;
  347. s->screen_width = bytestream_get_le16(&s->bytestream);
  348. s->screen_height = bytestream_get_le16(&s->bytestream);
  349. if( (unsigned)s->screen_width > 32767
  350. || (unsigned)s->screen_height > 32767){
  351. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  352. return -1;
  353. }
  354. v = bytestream_get_byte(&s->bytestream);
  355. s->color_resolution = ((v & 0x70) >> 4) + 1;
  356. has_global_palette = (v & 0x80);
  357. s->bits_per_pixel = (v & 0x07) + 1;
  358. s->background_color_index = bytestream_get_byte(&s->bytestream);
  359. bytestream_get_byte(&s->bytestream); /* ignored */
  360. #ifdef DEBUG
  361. dprintf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  362. s->screen_width, s->screen_height, s->bits_per_pixel,
  363. has_global_palette);
  364. #endif
  365. if (has_global_palette) {
  366. n = 1 << s->bits_per_pixel;
  367. bytestream_get_buffer(&s->bytestream, s->global_palette, n * 3);
  368. }
  369. return 0;
  370. }
  371. static int gif_parse_next_image(GifState *s)
  372. {
  373. int ret, code;
  374. for (;;) {
  375. code = bytestream_get_byte(&s->bytestream);
  376. #ifdef DEBUG
  377. dprintf("gif: code=%02x '%c'\n", code, code);
  378. #endif
  379. switch (code) {
  380. case ',':
  381. if (gif_read_image(s) < 0)
  382. return -1;
  383. ret = 0;
  384. goto the_end;
  385. case ';':
  386. /* end of image */
  387. ret = -1;
  388. goto the_end;
  389. case '!':
  390. if (gif_read_extension(s) < 0)
  391. return -1;
  392. break;
  393. case EOF:
  394. default:
  395. /* error or errneous EOF */
  396. ret = -1;
  397. goto the_end;
  398. }
  399. }
  400. the_end:
  401. return ret;
  402. }
  403. static int gif_decode_init(AVCodecContext *avctx)
  404. {
  405. GifState *s = avctx->priv_data;
  406. avcodec_get_frame_defaults(&s->picture);
  407. avctx->coded_frame= &s->picture;
  408. s->picture.data[0] = NULL;
  409. return 0;
  410. }
  411. static int gif_decode_frame(AVCodecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
  412. {
  413. GifState *s = avctx->priv_data;
  414. AVFrame *picture = data;
  415. int ret;
  416. s->bytestream = buf;
  417. if (gif_read_header1(s) < 0)
  418. return -1;
  419. avctx->pix_fmt = PIX_FMT_PAL8;
  420. if (avcodec_check_dimensions(avctx, s->screen_width, s->screen_height))
  421. return -1;
  422. avcodec_set_dimensions(avctx, s->screen_width, s->screen_height);
  423. if (s->picture.data[0])
  424. avctx->release_buffer(avctx, &s->picture);
  425. if (avctx->get_buffer(avctx, &s->picture) < 0) {
  426. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  427. return -1;
  428. }
  429. s->image_palette = (uint32_t *)s->picture.data[1];
  430. ret = gif_parse_next_image(s);
  431. if (ret < 0)
  432. return ret;
  433. *picture = s->picture;
  434. *data_size = sizeof(AVPicture);
  435. return 0;
  436. }
  437. static int gif_decode_close(AVCodecContext *avctx)
  438. {
  439. GifState *s = avctx->priv_data;
  440. if(s->picture.data[0])
  441. avctx->release_buffer(avctx, &s->picture);
  442. return 0;
  443. }
  444. AVCodec gif_decoder = {
  445. "gif",
  446. CODEC_TYPE_VIDEO,
  447. CODEC_ID_GIF,
  448. sizeof(GifState),
  449. gif_decode_init,
  450. NULL,
  451. gif_decode_close,
  452. gif_decode_frame,
  453. };