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.

591 lines
15KB

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