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.

645 lines
17KB

  1. /*
  2. * GIF decoder
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. int gif_write(ByteIOContext *pb, AVImageInfo *info);
  23. //#define DEBUG
  24. #define MAXBITS 12
  25. #define SIZTABLE (1<<MAXBITS)
  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. int screen_width;
  32. int screen_height;
  33. int bits_per_pixel;
  34. int background_color_index;
  35. int transparent_color_index;
  36. int color_resolution;
  37. uint8_t *image_buf;
  38. int image_linesize;
  39. uint32_t *image_palette;
  40. int pix_fmt;
  41. /* after the frame is displayed, the disposal method is used */
  42. int gce_disposal;
  43. /* delay during which the frame is shown */
  44. int gce_delay;
  45. /* LZW compatible decoder */
  46. ByteIOContext *f;
  47. int eob_reached;
  48. uint8_t *pbuf, *ebuf;
  49. int bbits;
  50. unsigned int bbuf;
  51. int cursize; /* The current code size */
  52. int curmask;
  53. int codesize;
  54. int clear_code;
  55. int end_code;
  56. int newcodes; /* First available code */
  57. int top_slot; /* Highest code for current size */
  58. int slot; /* Last read code */
  59. int fc, oc;
  60. uint8_t *sp;
  61. uint8_t stack[SIZTABLE];
  62. uint8_t suffix[SIZTABLE];
  63. uint16_t prefix[SIZTABLE];
  64. /* aux buffers */
  65. uint8_t global_palette[256 * 3];
  66. uint8_t local_palette[256 * 3];
  67. uint8_t buf[256];
  68. } GifState;
  69. static const uint8_t gif87a_sig[6] = "GIF87a";
  70. static const uint8_t gif89a_sig[6] = "GIF89a";
  71. static const uint16_t mask[17] =
  72. {
  73. 0x0000, 0x0001, 0x0003, 0x0007,
  74. 0x000F, 0x001F, 0x003F, 0x007F,
  75. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  76. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
  77. };
  78. /* Probe gif video format or gif image format. The current heuristic
  79. supposes the gif87a is always a single image. For gif89a, we
  80. consider it as a video only if a GCE extension is present in the
  81. first kilobyte. */
  82. static int gif_video_probe(AVProbeData * pd)
  83. {
  84. const uint8_t *p, *p_end;
  85. int bits_per_pixel, has_global_palette, ext_code, ext_len;
  86. int gce_flags, gce_disposal;
  87. if (pd->buf_size < 24 ||
  88. memcmp(pd->buf, gif89a_sig, 6) != 0)
  89. return 0;
  90. p_end = pd->buf + pd->buf_size;
  91. p = pd->buf + 6;
  92. bits_per_pixel = (p[4] & 0x07) + 1;
  93. has_global_palette = (p[4] & 0x80);
  94. p += 7;
  95. if (has_global_palette)
  96. p += (1 << bits_per_pixel) * 3;
  97. for(;;) {
  98. if (p >= p_end)
  99. return 0;
  100. if (*p != '!')
  101. break;
  102. p++;
  103. if (p >= p_end)
  104. return 0;
  105. ext_code = *p++;
  106. if (p >= p_end)
  107. return 0;
  108. ext_len = *p++;
  109. if (ext_code == 0xf9) {
  110. if (p >= p_end)
  111. return 0;
  112. /* if GCE extension found with gce_disposal != 0: it is
  113. likely to be an animation */
  114. gce_flags = *p++;
  115. gce_disposal = (gce_flags >> 2) & 0x7;
  116. if (gce_disposal != 0)
  117. return AVPROBE_SCORE_MAX;
  118. else
  119. return 0;
  120. }
  121. for(;;) {
  122. if (ext_len == 0)
  123. break;
  124. p += ext_len;
  125. if (p >= p_end)
  126. return 0;
  127. ext_len = *p++;
  128. }
  129. }
  130. return 0;
  131. }
  132. static int gif_image_probe(AVProbeData * pd)
  133. {
  134. if (pd->buf_size >= 24 &&
  135. (memcmp(pd->buf, gif87a_sig, 6) == 0 ||
  136. memcmp(pd->buf, gif89a_sig, 6) == 0))
  137. return AVPROBE_SCORE_MAX - 1;
  138. else
  139. return 0;
  140. }
  141. static void GLZWDecodeInit(GifState * s, int csize)
  142. {
  143. /* read buffer */
  144. s->eob_reached = 0;
  145. s->pbuf = s->buf;
  146. s->ebuf = s->buf;
  147. s->bbuf = 0;
  148. s->bbits = 0;
  149. /* decoder */
  150. s->codesize = csize;
  151. s->cursize = s->codesize + 1;
  152. s->curmask = mask[s->cursize];
  153. s->top_slot = 1 << s->cursize;
  154. s->clear_code = 1 << s->codesize;
  155. s->end_code = s->clear_code + 1;
  156. s->slot = s->newcodes = s->clear_code + 2;
  157. s->oc = s->fc = 0;
  158. s->sp = s->stack;
  159. }
  160. /* XXX: optimize */
  161. static inline int GetCode(GifState * s)
  162. {
  163. int c, sizbuf;
  164. uint8_t *ptr;
  165. while (s->bbits < s->cursize) {
  166. ptr = s->pbuf;
  167. if (ptr >= s->ebuf) {
  168. if (!s->eob_reached) {
  169. sizbuf = get_byte(s->f);
  170. s->ebuf = s->buf + sizbuf;
  171. s->pbuf = s->buf;
  172. if (sizbuf > 0) {
  173. get_buffer(s->f, s->buf, sizbuf);
  174. } else {
  175. s->eob_reached = 1;
  176. }
  177. }
  178. ptr = s->pbuf;
  179. }
  180. s->bbuf |= ptr[0] << s->bbits;
  181. ptr++;
  182. s->pbuf = ptr;
  183. s->bbits += 8;
  184. }
  185. c = s->bbuf & s->curmask;
  186. s->bbuf >>= s->cursize;
  187. s->bbits -= s->cursize;
  188. return c;
  189. }
  190. /* NOTE: the algorithm here is inspired from the LZW GIF decoder
  191. written by Steven A. Bennett in 1987. */
  192. /* return the number of byte decoded */
  193. static int GLZWDecode(GifState * s, uint8_t * buf, int len)
  194. {
  195. int l, c, code, oc, fc;
  196. uint8_t *sp;
  197. if (s->end_code < 0)
  198. return 0;
  199. l = len;
  200. sp = s->sp;
  201. oc = s->oc;
  202. fc = s->fc;
  203. while (sp > s->stack) {
  204. *buf++ = *(--sp);
  205. if ((--l) == 0)
  206. goto the_end;
  207. }
  208. for (;;) {
  209. c = GetCode(s);
  210. if (c == s->end_code) {
  211. s->end_code = -1;
  212. break;
  213. } else if (c == s->clear_code) {
  214. s->cursize = s->codesize + 1;
  215. s->curmask = mask[s->cursize];
  216. s->slot = s->newcodes;
  217. s->top_slot = 1 << s->cursize;
  218. while ((c = GetCode(s)) == s->clear_code);
  219. if (c == s->end_code) {
  220. s->end_code = -1;
  221. break;
  222. }
  223. /* test error */
  224. if (c >= s->slot)
  225. c = 0;
  226. fc = oc = c;
  227. *buf++ = c;
  228. if ((--l) == 0)
  229. break;
  230. } else {
  231. code = c;
  232. if (code >= s->slot) {
  233. *sp++ = fc;
  234. code = oc;
  235. }
  236. while (code >= s->newcodes) {
  237. *sp++ = s->suffix[code];
  238. code = s->prefix[code];
  239. }
  240. *sp++ = code;
  241. if (s->slot < s->top_slot) {
  242. s->suffix[s->slot] = fc = code;
  243. s->prefix[s->slot++] = oc;
  244. oc = c;
  245. }
  246. if (s->slot >= s->top_slot) {
  247. if (s->cursize < MAXBITS) {
  248. s->top_slot <<= 1;
  249. s->curmask = mask[++s->cursize];
  250. }
  251. }
  252. while (sp > s->stack) {
  253. *buf++ = *(--sp);
  254. if ((--l) == 0)
  255. goto the_end;
  256. }
  257. }
  258. }
  259. the_end:
  260. s->sp = sp;
  261. s->oc = oc;
  262. s->fc = fc;
  263. return len - l;
  264. }
  265. static int gif_read_image(GifState *s)
  266. {
  267. ByteIOContext *f = s->f;
  268. int left, top, width, height, bits_per_pixel, code_size, flags;
  269. int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
  270. uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
  271. left = get_le16(f);
  272. top = get_le16(f);
  273. width = get_le16(f);
  274. height = get_le16(f);
  275. flags = get_byte(f);
  276. is_interleaved = flags & 0x40;
  277. has_local_palette = flags & 0x80;
  278. bits_per_pixel = (flags & 0x07) + 1;
  279. #ifdef DEBUG
  280. printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  281. #endif
  282. if (has_local_palette) {
  283. get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
  284. palette = s->local_palette;
  285. } else {
  286. palette = s->global_palette;
  287. bits_per_pixel = s->bits_per_pixel;
  288. }
  289. /* verify that all the image is inside the screen dimensions */
  290. if (left + width > s->screen_width ||
  291. top + height > s->screen_height)
  292. return -EINVAL;
  293. /* build the palette */
  294. if (s->pix_fmt == PIX_FMT_RGB24) {
  295. line = av_malloc(width);
  296. if (!line)
  297. return -ENOMEM;
  298. } else {
  299. n = (1 << bits_per_pixel);
  300. spal = palette;
  301. for(i = 0; i < n; i++) {
  302. s->image_palette[i] = (0xff << 24) |
  303. (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
  304. spal += 3;
  305. }
  306. for(; i < 256; i++)
  307. s->image_palette[i] = (0xff << 24);
  308. /* handle transparency */
  309. if (s->transparent_color_index >= 0)
  310. s->image_palette[s->transparent_color_index] = 0;
  311. line = NULL;
  312. }
  313. /* now get the image data */
  314. s->f = f;
  315. code_size = get_byte(f);
  316. GLZWDecodeInit(s, code_size);
  317. /* read all the image */
  318. linesize = s->image_linesize;
  319. ptr1 = s->image_buf + top * linesize + (left * 3);
  320. ptr = ptr1;
  321. pass = 0;
  322. y1 = 0;
  323. for (y = 0; y < height; y++) {
  324. if (s->pix_fmt == PIX_FMT_RGB24) {
  325. /* transcode to RGB24 */
  326. GLZWDecode(s, line, width);
  327. d = ptr;
  328. sptr = line;
  329. for(x = 0; x < width; x++) {
  330. spal = palette + sptr[0] * 3;
  331. d[0] = spal[0];
  332. d[1] = spal[1];
  333. d[2] = spal[2];
  334. d += 3;
  335. sptr++;
  336. }
  337. } else {
  338. GLZWDecode(s, ptr, width);
  339. }
  340. if (is_interleaved) {
  341. switch(pass) {
  342. default:
  343. case 0:
  344. case 1:
  345. y1 += 8;
  346. ptr += linesize * 8;
  347. if (y1 >= height) {
  348. y1 = 4;
  349. if (pass == 0)
  350. ptr = ptr1 + linesize * 4;
  351. else
  352. ptr = ptr1 + linesize * 2;
  353. pass++;
  354. }
  355. break;
  356. case 2:
  357. y1 += 4;
  358. ptr += linesize * 4;
  359. if (y1 >= height) {
  360. y1 = 1;
  361. ptr = ptr1 + linesize;
  362. pass++;
  363. }
  364. break;
  365. case 3:
  366. y1 += 2;
  367. ptr += linesize * 2;
  368. break;
  369. }
  370. } else {
  371. ptr += linesize;
  372. }
  373. }
  374. av_free(line);
  375. /* read the garbage data until end marker is found */
  376. while (!s->eob_reached)
  377. GetCode(s);
  378. return 0;
  379. }
  380. static int gif_read_extension(GifState *s)
  381. {
  382. ByteIOContext *f = s->f;
  383. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  384. /* extension */
  385. ext_code = get_byte(f);
  386. ext_len = get_byte(f);
  387. #ifdef DEBUG
  388. printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  389. #endif
  390. switch(ext_code) {
  391. case 0xf9:
  392. if (ext_len != 4)
  393. goto discard_ext;
  394. s->transparent_color_index = -1;
  395. gce_flags = get_byte(f);
  396. s->gce_delay = get_le16(f);
  397. gce_transparent_index = get_byte(f);
  398. if (gce_flags & 0x01)
  399. s->transparent_color_index = gce_transparent_index;
  400. else
  401. s->transparent_color_index = -1;
  402. s->gce_disposal = (gce_flags >> 2) & 0x7;
  403. #ifdef DEBUG
  404. printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  405. gce_flags, s->gce_delay,
  406. s->transparent_color_index, s->gce_disposal);
  407. #endif
  408. ext_len = get_byte(f);
  409. break;
  410. }
  411. /* NOTE: many extension blocks can come after */
  412. discard_ext:
  413. while (ext_len != 0) {
  414. for (i = 0; i < ext_len; i++)
  415. get_byte(f);
  416. ext_len = get_byte(f);
  417. #ifdef DEBUG
  418. printf("gif: ext_len1=%d\n", ext_len);
  419. #endif
  420. }
  421. return 0;
  422. }
  423. static int gif_read_header1(GifState *s)
  424. {
  425. ByteIOContext *f = s->f;
  426. uint8_t sig[6];
  427. int ret, v, n;
  428. int has_global_palette;
  429. /* read gif signature */
  430. ret = get_buffer(f, sig, 6);
  431. if (ret != 6)
  432. return -1;
  433. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  434. memcmp(sig, gif89a_sig, 6) != 0)
  435. return -1;
  436. /* read screen header */
  437. s->transparent_color_index = -1;
  438. s->screen_width = get_le16(f);
  439. s->screen_height = get_le16(f);
  440. if( (unsigned)s->screen_width > 32767
  441. || (unsigned)s->screen_height > 32767){
  442. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  443. return -1;
  444. }
  445. v = get_byte(f);
  446. s->color_resolution = ((v & 0x70) >> 4) + 1;
  447. has_global_palette = (v & 0x80);
  448. s->bits_per_pixel = (v & 0x07) + 1;
  449. s->background_color_index = get_byte(f);
  450. get_byte(f); /* ignored */
  451. #ifdef DEBUG
  452. printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  453. s->screen_width, s->screen_height, s->bits_per_pixel,
  454. has_global_palette);
  455. #endif
  456. if (has_global_palette) {
  457. n = 1 << s->bits_per_pixel;
  458. get_buffer(f, s->global_palette, n * 3);
  459. }
  460. return 0;
  461. }
  462. static int gif_parse_next_image(GifState *s)
  463. {
  464. ByteIOContext *f = s->f;
  465. int ret, code;
  466. for (;;) {
  467. code = url_fgetc(f);
  468. #ifdef DEBUG
  469. printf("gif: code=%02x '%c'\n", code, code);
  470. #endif
  471. switch (code) {
  472. case ',':
  473. if (gif_read_image(s) < 0)
  474. return AVERROR_IO;
  475. ret = 0;
  476. goto the_end;
  477. case ';':
  478. /* end of image */
  479. ret = AVERROR_IO;
  480. goto the_end;
  481. case '!':
  482. if (gif_read_extension(s) < 0)
  483. return AVERROR_IO;
  484. break;
  485. case EOF:
  486. default:
  487. /* error or errneous EOF */
  488. ret = AVERROR_IO;
  489. goto the_end;
  490. }
  491. }
  492. the_end:
  493. return ret;
  494. }
  495. static int gif_read_header(AVFormatContext * s1,
  496. AVFormatParameters * ap)
  497. {
  498. GifState *s = s1->priv_data;
  499. ByteIOContext *f = &s1->pb;
  500. AVStream *st;
  501. s->f = f;
  502. if (gif_read_header1(s) < 0)
  503. return -1;
  504. /* allocate image buffer */
  505. s->image_linesize = s->screen_width * 3;
  506. s->image_buf = av_malloc(s->screen_height * s->image_linesize);
  507. if (!s->image_buf)
  508. return -ENOMEM;
  509. s->pix_fmt = PIX_FMT_RGB24;
  510. /* now we are ready: build format streams */
  511. st = av_new_stream(s1, 0);
  512. if (!st)
  513. return -1;
  514. st->codec->codec_type = CODEC_TYPE_VIDEO;
  515. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  516. st->codec->time_base.den = 5;
  517. st->codec->time_base.num = 1;
  518. /* XXX: check if screen size is always valid */
  519. st->codec->width = s->screen_width;
  520. st->codec->height = s->screen_height;
  521. st->codec->pix_fmt = PIX_FMT_RGB24;
  522. return 0;
  523. }
  524. static int gif_read_packet(AVFormatContext * s1,
  525. AVPacket * pkt)
  526. {
  527. GifState *s = s1->priv_data;
  528. int ret;
  529. ret = gif_parse_next_image(s);
  530. if (ret < 0)
  531. return ret;
  532. /* XXX: avoid copying */
  533. if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
  534. return AVERROR_IO;
  535. }
  536. pkt->stream_index = 0;
  537. memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
  538. return 0;
  539. }
  540. static int gif_read_close(AVFormatContext *s1)
  541. {
  542. GifState *s = s1->priv_data;
  543. av_free(s->image_buf);
  544. return 0;
  545. }
  546. /* read gif as image */
  547. static int gif_read(ByteIOContext *f,
  548. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  549. {
  550. GifState s1, *s = &s1;
  551. AVImageInfo info1, *info = &info1;
  552. int ret;
  553. memset(s, 0, sizeof(GifState));
  554. s->f = f;
  555. if (gif_read_header1(s) < 0)
  556. return -1;
  557. info->width = s->screen_width;
  558. info->height = s->screen_height;
  559. info->pix_fmt = PIX_FMT_PAL8;
  560. ret = alloc_cb(opaque, info);
  561. if (ret)
  562. return ret;
  563. s->image_buf = info->pict.data[0];
  564. s->image_linesize = info->pict.linesize[0];
  565. s->image_palette = (uint32_t *)info->pict.data[1];
  566. if (gif_parse_next_image(s) < 0)
  567. return -1;
  568. return 0;
  569. }
  570. AVInputFormat gif_demuxer =
  571. {
  572. "gif",
  573. "gif format",
  574. sizeof(GifState),
  575. gif_video_probe,
  576. gif_read_header,
  577. gif_read_packet,
  578. gif_read_close,
  579. };
  580. AVImageFormat gif_image_format = {
  581. "gif",
  582. "gif",
  583. gif_image_probe,
  584. gif_read,
  585. (1 << PIX_FMT_PAL8),
  586. #ifdef CONFIG_GIF_MUXER
  587. gif_write,
  588. #endif
  589. };