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.

619 lines
15KB

  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. if (pd->buf_size < 24 ||
  85. memcmp(pd->buf, gif89a_sig, 6) != 0)
  86. return 0;
  87. p_end = pd->buf + pd->buf_size;
  88. p = pd->buf + 6;
  89. bits_per_pixel = (p[4] & 0x07) + 1;
  90. has_global_palette = (p[4] & 0x80);
  91. p += 7;
  92. if (has_global_palette)
  93. p += (1 << bits_per_pixel) * 3;
  94. for(;;) {
  95. if (p >= p_end)
  96. return 0;
  97. if (*p != '!')
  98. break;
  99. p++;
  100. if (p >= p_end)
  101. return 0;
  102. ext_code = *p++;
  103. /* if GCE extension found: it is likely to be an animation */
  104. if (ext_code == 0xf9)
  105. return AVPROBE_SCORE_MAX;
  106. for(;;) {
  107. if (p >= p_end)
  108. return 0;
  109. ext_len = *p++;
  110. if (ext_len == 0)
  111. break;
  112. p += ext_len;
  113. }
  114. }
  115. return 0;
  116. }
  117. static int gif_image_probe(AVProbeData * pd)
  118. {
  119. if (pd->buf_size >= 24 &&
  120. (memcmp(pd->buf, gif87a_sig, 6) == 0 ||
  121. memcmp(pd->buf, gif89a_sig, 6) == 0))
  122. return AVPROBE_SCORE_MAX - 1;
  123. else
  124. return 0;
  125. }
  126. static void GLZWDecodeInit(GifState * s, int csize)
  127. {
  128. /* read buffer */
  129. s->eob_reached = 0;
  130. s->pbuf = s->buf;
  131. s->ebuf = s->buf;
  132. s->bbuf = 0;
  133. s->bbits = 0;
  134. /* decoder */
  135. s->codesize = csize;
  136. s->cursize = s->codesize + 1;
  137. s->curmask = mask[s->cursize];
  138. s->top_slot = 1 << s->cursize;
  139. s->clear_code = 1 << s->codesize;
  140. s->end_code = s->clear_code + 1;
  141. s->slot = s->newcodes = s->clear_code + 2;
  142. s->oc = s->fc = 0;
  143. s->sp = s->stack;
  144. }
  145. /* XXX: optimize */
  146. static inline int GetCode(GifState * s)
  147. {
  148. int c, sizbuf;
  149. uint8_t *ptr;
  150. while (s->bbits < s->cursize) {
  151. ptr = s->pbuf;
  152. if (ptr >= s->ebuf) {
  153. if (!s->eob_reached) {
  154. sizbuf = get_byte(s->f);
  155. s->ebuf = s->buf + sizbuf;
  156. s->pbuf = s->buf;
  157. if (sizbuf > 0) {
  158. get_buffer(s->f, s->buf, sizbuf);
  159. } else {
  160. s->eob_reached = 1;
  161. }
  162. }
  163. ptr = s->pbuf;
  164. }
  165. s->bbuf |= ptr[0] << s->bbits;
  166. ptr++;
  167. s->pbuf = ptr;
  168. s->bbits += 8;
  169. }
  170. c = s->bbuf & s->curmask;
  171. s->bbuf >>= s->cursize;
  172. s->bbits -= s->cursize;
  173. return c;
  174. }
  175. /* NOTE: the algorithm here is inspired from the LZW GIF decoder
  176. written by Steven A. Bennett in 1987. */
  177. /* return the number of byte decoded */
  178. static int GLZWDecode(GifState * s, uint8_t * buf, int len)
  179. {
  180. int l, c, code, oc, fc;
  181. uint8_t *sp;
  182. if (s->end_code < 0)
  183. return 0;
  184. l = len;
  185. sp = s->sp;
  186. oc = s->oc;
  187. fc = s->fc;
  188. while (sp > s->stack) {
  189. *buf++ = *(--sp);
  190. if ((--l) == 0)
  191. goto the_end;
  192. }
  193. for (;;) {
  194. c = GetCode(s);
  195. if (c == s->end_code) {
  196. s->end_code = -1;
  197. break;
  198. } else if (c == s->clear_code) {
  199. s->cursize = s->codesize + 1;
  200. s->curmask = mask[s->cursize];
  201. s->slot = s->newcodes;
  202. s->top_slot = 1 << s->cursize;
  203. while ((c = GetCode(s)) == s->clear_code);
  204. if (c == s->end_code) {
  205. s->end_code = -1;
  206. break;
  207. }
  208. /* test error */
  209. if (c >= s->slot)
  210. c = 0;
  211. fc = oc = c;
  212. *buf++ = c;
  213. if ((--l) == 0)
  214. break;
  215. } else {
  216. code = c;
  217. if (code >= s->slot) {
  218. *sp++ = fc;
  219. code = oc;
  220. }
  221. while (code >= s->newcodes) {
  222. *sp++ = s->suffix[code];
  223. code = s->prefix[code];
  224. }
  225. *sp++ = code;
  226. if (s->slot < s->top_slot) {
  227. s->suffix[s->slot] = fc = code;
  228. s->prefix[s->slot++] = oc;
  229. oc = c;
  230. }
  231. if (s->slot >= s->top_slot) {
  232. if (s->cursize < MAXBITS) {
  233. s->top_slot <<= 1;
  234. s->curmask = mask[++s->cursize];
  235. }
  236. }
  237. while (sp > s->stack) {
  238. *buf++ = *(--sp);
  239. if ((--l) == 0)
  240. goto the_end;
  241. }
  242. }
  243. }
  244. the_end:
  245. s->sp = sp;
  246. s->oc = oc;
  247. s->fc = fc;
  248. return len - l;
  249. }
  250. static int gif_read_image(GifState *s)
  251. {
  252. ByteIOContext *f = s->f;
  253. int left, top, width, height, bits_per_pixel, code_size, flags;
  254. int is_interleaved, has_local_palette, y, x, pass, y1, linesize, n, i;
  255. uint8_t *ptr, *line, *d, *spal, *palette, *sptr, *ptr1;
  256. left = get_le16(f);
  257. top = get_le16(f);
  258. width = get_le16(f);
  259. height = get_le16(f);
  260. flags = get_byte(f);
  261. is_interleaved = flags & 0x40;
  262. has_local_palette = flags & 0x80;
  263. bits_per_pixel = (flags & 0x07) + 1;
  264. #ifdef DEBUG
  265. printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  266. #endif
  267. if (has_local_palette) {
  268. get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
  269. palette = s->local_palette;
  270. } else {
  271. palette = s->global_palette;
  272. bits_per_pixel = s->bits_per_pixel;
  273. }
  274. /* verify that all the image is inside the screen dimensions */
  275. if (left + width > s->screen_width ||
  276. top + height > s->screen_height)
  277. return -EINVAL;
  278. /* build the palette */
  279. if (s->pix_fmt == PIX_FMT_RGB24) {
  280. line = av_malloc(width);
  281. if (!line)
  282. return -ENOMEM;
  283. } else {
  284. n = (1 << bits_per_pixel);
  285. spal = palette;
  286. for(i = 0; i < n; i++) {
  287. s->image_palette[i] = (0xff << 24) |
  288. (spal[0] << 16) | (spal[1] << 8) | (spal[2]);
  289. spal += 3;
  290. }
  291. for(; i < 256; i++)
  292. s->image_palette[i] = (0xff << 24);
  293. line = NULL;
  294. }
  295. /* now get the image data */
  296. s->f = f;
  297. code_size = get_byte(f);
  298. GLZWDecodeInit(s, code_size);
  299. /* read all the image */
  300. linesize = s->image_linesize;
  301. ptr1 = s->image_buf + top * linesize + (left * 3);
  302. ptr = ptr1;
  303. pass = 0;
  304. y1 = 0;
  305. for (y = 0; y < height; y++) {
  306. if (s->pix_fmt == PIX_FMT_RGB24) {
  307. /* transcode to RGB24 */
  308. GLZWDecode(s, line, width);
  309. d = ptr;
  310. sptr = line;
  311. for(x = 0; x < width; x++) {
  312. spal = palette + sptr[0] * 3;
  313. d[0] = spal[0];
  314. d[1] = spal[1];
  315. d[2] = spal[2];
  316. d += 3;
  317. sptr++;
  318. }
  319. } else {
  320. GLZWDecode(s, ptr, width);
  321. }
  322. if (is_interleaved) {
  323. switch(pass) {
  324. default:
  325. case 0:
  326. case 1:
  327. y1 += 8;
  328. ptr += linesize * 8;
  329. if (y1 >= height) {
  330. y1 = 4;
  331. if (pass == 0)
  332. ptr = ptr1 + linesize * 4;
  333. else
  334. ptr = ptr1 + linesize * 2;
  335. pass++;
  336. }
  337. break;
  338. case 2:
  339. y1 += 4;
  340. ptr += linesize * 4;
  341. if (y1 >= height) {
  342. y1 = 1;
  343. ptr = ptr1 + linesize;
  344. pass++;
  345. }
  346. break;
  347. case 3:
  348. y1 += 2;
  349. ptr += linesize * 2;
  350. break;
  351. }
  352. } else {
  353. ptr += linesize;
  354. }
  355. }
  356. av_free(line);
  357. /* read the garbage data until end marker is found */
  358. while (!s->eob_reached)
  359. GetCode(s);
  360. return 0;
  361. }
  362. static int gif_read_extension(GifState *s)
  363. {
  364. ByteIOContext *f = s->f;
  365. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  366. /* extension */
  367. ext_code = get_byte(f);
  368. ext_len = get_byte(f);
  369. #ifdef DEBUG
  370. printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  371. #endif
  372. switch(ext_code) {
  373. case 0xf9:
  374. if (ext_len != 4)
  375. goto discard_ext;
  376. s->transparent_color_index = -1;
  377. gce_flags = get_byte(f);
  378. s->gce_delay = get_le16(f);
  379. gce_transparent_index = get_byte(f);
  380. if (gce_flags & 0x01)
  381. s->transparent_color_index = gce_transparent_index;
  382. else
  383. s->transparent_color_index = -1;
  384. s->gce_disposal = (gce_flags >> 2) & 0x7;
  385. #ifdef DEBUG
  386. printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  387. gce_flags, s->gce_delay,
  388. s->transparent_color_index, s->gce_disposal);
  389. #endif
  390. ext_len = get_byte(f);
  391. break;
  392. }
  393. /* NOTE: many extension blocks can come after */
  394. discard_ext:
  395. while (ext_len != 0) {
  396. for (i = 0; i < ext_len; i++)
  397. get_byte(f);
  398. ext_len = get_byte(f);
  399. #ifdef DEBUG
  400. printf("gif: ext_len1=%d\n", ext_len);
  401. #endif
  402. }
  403. return 0;
  404. }
  405. static int gif_read_header1(GifState *s)
  406. {
  407. ByteIOContext *f = s->f;
  408. uint8_t sig[6];
  409. int ret, v, n;
  410. int has_global_palette;
  411. /* read gif signature */
  412. ret = get_buffer(f, sig, 6);
  413. if (ret != 6)
  414. return -1;
  415. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  416. memcmp(sig, gif89a_sig, 6) != 0)
  417. return -1;
  418. /* read screen header */
  419. s->transparent_color_index = -1;
  420. s->screen_width = get_le16(f);
  421. s->screen_height = get_le16(f);
  422. v = get_byte(f);
  423. s->color_resolution = ((v & 0x70) >> 4) + 1;
  424. has_global_palette = (v & 0x80);
  425. s->bits_per_pixel = (v & 0x07) + 1;
  426. s->background_color_index = get_byte(f);
  427. get_byte(f); /* ignored */
  428. #ifdef DEBUG
  429. printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  430. s->screen_width, s->screen_height, s->bits_per_pixel,
  431. has_global_palette);
  432. #endif
  433. if (has_global_palette) {
  434. n = 1 << s->bits_per_pixel;
  435. get_buffer(f, s->global_palette, n * 3);
  436. }
  437. return 0;
  438. }
  439. static int gif_parse_next_image(GifState *s)
  440. {
  441. ByteIOContext *f = s->f;
  442. int ret, code;
  443. for (;;) {
  444. code = url_fgetc(f);
  445. #ifdef DEBUG
  446. printf("gif: code=%02x '%c'\n", code, code);
  447. #endif
  448. switch (code) {
  449. case ',':
  450. if (gif_read_image(s) < 0)
  451. return -EIO;
  452. ret = 0;
  453. goto the_end;
  454. case ';':
  455. /* end of image */
  456. ret = -EIO;
  457. goto the_end;
  458. case '!':
  459. if (gif_read_extension(s) < 0)
  460. return -EIO;
  461. break;
  462. case EOF:
  463. default:
  464. /* error or errneous EOF */
  465. ret = -EIO;
  466. goto the_end;
  467. }
  468. }
  469. the_end:
  470. return ret;
  471. }
  472. static int gif_read_header(AVFormatContext * s1,
  473. AVFormatParameters * ap)
  474. {
  475. GifState *s = s1->priv_data;
  476. ByteIOContext *f = &s1->pb;
  477. AVStream *st;
  478. s->f = f;
  479. if (gif_read_header1(s) < 0)
  480. return -1;
  481. /* allocate image buffer */
  482. s->image_linesize = s->screen_width * 3;
  483. s->image_buf = av_malloc(s->screen_height * s->image_linesize);
  484. if (!s->image_buf)
  485. return -ENOMEM;
  486. s->pix_fmt = PIX_FMT_RGB24;
  487. /* now we are ready: build format streams */
  488. st = av_new_stream(s1, 0);
  489. if (!st)
  490. return -1;
  491. st->codec.codec_type = CODEC_TYPE_VIDEO;
  492. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  493. st->codec.frame_rate = 5;
  494. st->codec.frame_rate_base = 1;
  495. /* XXX: check if screen size is always valid */
  496. st->codec.width = s->screen_width;
  497. st->codec.height = s->screen_height;
  498. st->codec.pix_fmt = PIX_FMT_RGB24;
  499. return 0;
  500. }
  501. static int gif_read_packet(AVFormatContext * s1,
  502. AVPacket * pkt)
  503. {
  504. GifState *s = s1->priv_data;
  505. int ret;
  506. ret = gif_parse_next_image(s);
  507. if (ret < 0)
  508. return ret;
  509. /* XXX: avoid copying */
  510. if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
  511. return -EIO;
  512. }
  513. pkt->stream_index = 0;
  514. memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
  515. return 0;
  516. }
  517. static int gif_read_close(AVFormatContext *s1)
  518. {
  519. GifState *s = s1->priv_data;
  520. av_free(s->image_buf);
  521. return 0;
  522. }
  523. /* read gif as image */
  524. static int gif_read(ByteIOContext *f,
  525. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  526. {
  527. GifState s1, *s = &s1;
  528. AVImageInfo info1, *info = &info1;
  529. int ret;
  530. memset(s, 0, sizeof(GifState));
  531. s->f = f;
  532. if (gif_read_header1(s) < 0)
  533. return -1;
  534. info->width = s->screen_width;
  535. info->height = s->screen_height;
  536. info->pix_fmt = PIX_FMT_PAL8;
  537. ret = alloc_cb(opaque, info);
  538. if (ret)
  539. return ret;
  540. s->image_buf = info->pict.data[0];
  541. s->image_linesize = info->pict.linesize[0];
  542. s->image_palette = (uint32_t *)info->pict.data[1];
  543. if (gif_parse_next_image(s) < 0)
  544. return -1;
  545. return 0;
  546. }
  547. AVInputFormat gif_iformat =
  548. {
  549. "gif",
  550. "gif format",
  551. sizeof(GifState),
  552. gif_video_probe,
  553. gif_read_header,
  554. gif_read_packet,
  555. gif_read_close,
  556. };
  557. AVImageFormat gif_image_format = {
  558. "gif",
  559. "gif",
  560. gif_image_probe,
  561. gif_read,
  562. (1 << PIX_FMT_PAL8),
  563. gif_write,
  564. };