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.

594 lines
16KB

  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 = 4;
  339. if (pass == 0)
  340. ptr = ptr1 + linesize * 4;
  341. else
  342. ptr = ptr1 + linesize * 2;
  343. pass++;
  344. }
  345. break;
  346. case 2:
  347. y1 += 4;
  348. ptr += linesize * 4;
  349. if (y1 >= height) {
  350. y1 = 1;
  351. ptr = ptr1 + linesize;
  352. pass++;
  353. }
  354. break;
  355. case 3:
  356. y1 += 2;
  357. ptr += linesize * 2;
  358. break;
  359. }
  360. } else {
  361. ptr += linesize;
  362. }
  363. }
  364. av_free(line);
  365. /* read the garbage data until end marker is found */
  366. while (!s->eob_reached)
  367. GetCode(s);
  368. return 0;
  369. }
  370. static int gif_read_extension(GifState *s)
  371. {
  372. ByteIOContext *f = s->f;
  373. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  374. /* extension */
  375. ext_code = get_byte(f);
  376. ext_len = get_byte(f);
  377. #ifdef DEBUG
  378. printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  379. #endif
  380. switch(ext_code) {
  381. case 0xf9:
  382. if (ext_len != 4)
  383. goto discard_ext;
  384. s->transparent_color_index = -1;
  385. gce_flags = get_byte(f);
  386. s->gce_delay = get_le16(f);
  387. gce_transparent_index = get_byte(f);
  388. if (gce_flags & 0x01)
  389. s->transparent_color_index = gce_transparent_index;
  390. else
  391. s->transparent_color_index = -1;
  392. s->gce_disposal = (gce_flags >> 2) & 0x7;
  393. #ifdef DEBUG
  394. printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  395. gce_flags, s->gce_delay,
  396. s->transparent_color_index, s->gce_disposal);
  397. #endif
  398. ext_len = get_byte(f);
  399. break;
  400. }
  401. /* NOTE: many extension blocks can come after */
  402. discard_ext:
  403. while (ext_len != 0) {
  404. for (i = 0; i < ext_len; i++)
  405. get_byte(f);
  406. ext_len = get_byte(f);
  407. #ifdef DEBUG
  408. printf("gif: ext_len1=%d\n", ext_len);
  409. #endif
  410. }
  411. return 0;
  412. }
  413. static int gif_read_header1(GifState *s)
  414. {
  415. ByteIOContext *f = s->f;
  416. uint8_t sig[6];
  417. int ret, v, n;
  418. int has_global_palette;
  419. /* read gif signature */
  420. ret = get_buffer(f, sig, 6);
  421. if (ret != 6)
  422. return -1;
  423. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  424. memcmp(sig, gif89a_sig, 6) != 0)
  425. return -1;
  426. /* read screen header */
  427. s->transparent_color_index = -1;
  428. s->screen_width = get_le16(f);
  429. s->screen_height = get_le16(f);
  430. if( (unsigned)s->screen_width > 32767
  431. || (unsigned)s->screen_height > 32767){
  432. av_log(NULL, AV_LOG_ERROR, "picture size too large\n");
  433. return -1;
  434. }
  435. v = get_byte(f);
  436. s->color_resolution = ((v & 0x70) >> 4) + 1;
  437. has_global_palette = (v & 0x80);
  438. s->bits_per_pixel = (v & 0x07) + 1;
  439. s->background_color_index = get_byte(f);
  440. get_byte(f); /* ignored */
  441. #ifdef DEBUG
  442. printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  443. s->screen_width, s->screen_height, s->bits_per_pixel,
  444. has_global_palette);
  445. #endif
  446. if (has_global_palette) {
  447. n = 1 << s->bits_per_pixel;
  448. get_buffer(f, s->global_palette, n * 3);
  449. }
  450. return 0;
  451. }
  452. static int gif_parse_next_image(GifState *s)
  453. {
  454. ByteIOContext *f = s->f;
  455. int ret, code;
  456. for (;;) {
  457. code = url_fgetc(f);
  458. #ifdef DEBUG
  459. printf("gif: code=%02x '%c'\n", code, code);
  460. #endif
  461. switch (code) {
  462. case ',':
  463. if (gif_read_image(s) < 0)
  464. return AVERROR(EIO);
  465. ret = 0;
  466. goto the_end;
  467. case ';':
  468. /* end of image */
  469. ret = AVERROR(EIO);
  470. goto the_end;
  471. case '!':
  472. if (gif_read_extension(s) < 0)
  473. return AVERROR(EIO);
  474. break;
  475. case EOF:
  476. default:
  477. /* error or errneous EOF */
  478. ret = AVERROR(EIO);
  479. goto the_end;
  480. }
  481. }
  482. the_end:
  483. return ret;
  484. }
  485. static int gif_read_header(AVFormatContext * s1,
  486. AVFormatParameters * ap)
  487. {
  488. GifState *s = s1->priv_data;
  489. ByteIOContext *f = &s1->pb;
  490. AVStream *st;
  491. s->f = f;
  492. if (gif_read_header1(s) < 0)
  493. return -1;
  494. /* allocate image buffer */
  495. s->image_linesize = s->screen_width * 3;
  496. s->image_buf = av_malloc(s->screen_height * s->image_linesize);
  497. if (!s->image_buf)
  498. return AVERROR(ENOMEM);
  499. s->pix_fmt = PIX_FMT_RGB24;
  500. /* now we are ready: build format streams */
  501. st = av_new_stream(s1, 0);
  502. if (!st)
  503. return -1;
  504. st->codec->codec_type = CODEC_TYPE_VIDEO;
  505. st->codec->codec_id = CODEC_ID_RAWVIDEO;
  506. st->codec->time_base.den = 5;
  507. st->codec->time_base.num = 1;
  508. /* XXX: check if screen size is always valid */
  509. st->codec->width = s->screen_width;
  510. st->codec->height = s->screen_height;
  511. st->codec->pix_fmt = PIX_FMT_RGB24;
  512. return 0;
  513. }
  514. static int gif_read_packet(AVFormatContext * s1,
  515. AVPacket * pkt)
  516. {
  517. GifState *s = s1->priv_data;
  518. int ret;
  519. ret = gif_parse_next_image(s);
  520. if (ret < 0)
  521. return ret;
  522. /* XXX: avoid copying */
  523. if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
  524. return AVERROR(EIO);
  525. }
  526. pkt->stream_index = 0;
  527. memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
  528. return 0;
  529. }
  530. static int gif_read_close(AVFormatContext *s1)
  531. {
  532. GifState *s = s1->priv_data;
  533. av_free(s->image_buf);
  534. return 0;
  535. }
  536. AVInputFormat gif_demuxer =
  537. {
  538. "gif",
  539. "gif format",
  540. sizeof(GifState),
  541. gif_video_probe,
  542. gif_read_header,
  543. gif_read_packet,
  544. gif_read_close,
  545. };