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.

641 lines
16KB

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