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.

467 lines
11KB

  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. //#define DEBUG
  21. #define MAXBITS 12
  22. #define SIZTABLE (1<<MAXBITS)
  23. #define GCE_DISPOSAL_NONE 0
  24. #define GCE_DISPOSAL_INPLACE 1
  25. #define GCE_DISPOSAL_BACKGROUND 2
  26. #define GCE_DISPOSAL_RESTORE 3
  27. typedef struct GifState {
  28. int screen_width;
  29. int screen_height;
  30. int bits_per_pixel;
  31. int background_color_index;
  32. int transparent_color_index;
  33. int color_resolution;
  34. int image_count;
  35. uint8_t *image_buf;
  36. /* after the frame is displayed, the disposal method is used */
  37. int gce_disposal;
  38. /* delay during which the frame is shown */
  39. int gce_delay;
  40. /* LZW compatible decoder */
  41. ByteIOContext *f;
  42. int eob_reached;
  43. uint8_t *pbuf, *ebuf;
  44. int bbits;
  45. unsigned int bbuf;
  46. int cursize; /* The current code size */
  47. int curmask;
  48. int codesize;
  49. int clear_code;
  50. int end_code;
  51. int newcodes; /* First available code */
  52. int top_slot; /* Highest code for current size */
  53. int slot; /* Last read code */
  54. int fc, oc;
  55. uint8_t *sp;
  56. uint8_t stack[SIZTABLE];
  57. uint8_t suffix[SIZTABLE];
  58. uint16_t prefix[SIZTABLE];
  59. /* aux buffers */
  60. uint8_t global_palette[256 * 3];
  61. uint8_t local_palette[256 * 3];
  62. uint8_t buf[256];
  63. } GifState;
  64. static const uint8_t gif87a_sig[6] = "GIF87a";
  65. static const uint8_t gif89a_sig[6] = "GIF89a";
  66. static const uint16_t mask[17] =
  67. {
  68. 0x0000, 0x0001, 0x0003, 0x0007,
  69. 0x000F, 0x001F, 0x003F, 0x007F,
  70. 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  71. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
  72. };
  73. static int gif_probe(AVProbeData * pd)
  74. {
  75. if (pd->buf_size >= 24 &&
  76. (memcmp(pd->buf, gif87a_sig, 6) == 0 ||
  77. memcmp(pd->buf, gif89a_sig, 6) == 0))
  78. return AVPROBE_SCORE_MAX;
  79. else
  80. return 0;
  81. }
  82. static void GLZWDecodeInit(GifState * s, int csize)
  83. {
  84. /* read buffer */
  85. s->eob_reached = 0;
  86. s->pbuf = s->buf;
  87. s->ebuf = s->buf;
  88. s->bbuf = 0;
  89. s->bbits = 0;
  90. /* decoder */
  91. s->codesize = csize;
  92. s->cursize = s->codesize + 1;
  93. s->curmask = mask[s->cursize];
  94. s->top_slot = 1 << s->cursize;
  95. s->clear_code = 1 << s->codesize;
  96. s->end_code = s->clear_code + 1;
  97. s->slot = s->newcodes = s->clear_code + 2;
  98. s->oc = s->fc = 0;
  99. s->sp = s->stack;
  100. }
  101. /* XXX: optimize */
  102. static inline int GetCode(GifState * s)
  103. {
  104. int c, sizbuf;
  105. uint8_t *ptr;
  106. while (s->bbits < s->cursize) {
  107. ptr = s->pbuf;
  108. if (ptr >= s->ebuf) {
  109. if (!s->eob_reached) {
  110. sizbuf = get_byte(s->f);
  111. s->ebuf = s->buf + sizbuf;
  112. s->pbuf = s->buf;
  113. if (sizbuf > 0) {
  114. get_buffer(s->f, s->buf, sizbuf);
  115. } else {
  116. s->eob_reached = 1;
  117. }
  118. }
  119. ptr = s->pbuf;
  120. }
  121. s->bbuf |= ptr[0] << s->bbits;
  122. ptr++;
  123. s->pbuf = ptr;
  124. s->bbits += 8;
  125. }
  126. c = s->bbuf & s->curmask;
  127. s->bbuf >>= s->cursize;
  128. s->bbits -= s->cursize;
  129. return c;
  130. }
  131. /* NOTE: the algorithm here is inspired from the LZW GIF decoder
  132. written by Steven A. Bennett in 1987. */
  133. /* return the number of byte decoded */
  134. static int GLZWDecode(GifState * s, uint8_t * buf, int len)
  135. {
  136. int l, c, code, oc, fc;
  137. uint8_t *sp;
  138. if (s->end_code < 0)
  139. return 0;
  140. l = len;
  141. sp = s->sp;
  142. oc = s->oc;
  143. fc = s->fc;
  144. while (sp > s->stack) {
  145. *buf++ = *(--sp);
  146. if ((--l) == 0)
  147. goto the_end;
  148. }
  149. for (;;) {
  150. c = GetCode(s);
  151. if (c == s->end_code) {
  152. s->end_code = -1;
  153. break;
  154. } else if (c == s->clear_code) {
  155. s->cursize = s->codesize + 1;
  156. s->curmask = mask[s->cursize];
  157. s->slot = s->newcodes;
  158. s->top_slot = 1 << s->cursize;
  159. while ((c = GetCode(s)) == s->clear_code);
  160. if (c == s->end_code) {
  161. s->end_code = -1;
  162. break;
  163. }
  164. /* test error */
  165. if (c >= s->slot)
  166. c = 0;
  167. fc = oc = c;
  168. *buf++ = c;
  169. if ((--l) == 0)
  170. break;
  171. } else {
  172. code = c;
  173. if (code >= s->slot) {
  174. *sp++ = fc;
  175. code = oc;
  176. }
  177. while (code >= s->newcodes) {
  178. *sp++ = s->suffix[code];
  179. code = s->prefix[code];
  180. }
  181. *sp++ = code;
  182. if (s->slot < s->top_slot) {
  183. s->suffix[s->slot] = fc = code;
  184. s->prefix[s->slot++] = oc;
  185. oc = c;
  186. }
  187. if (s->slot >= s->top_slot) {
  188. if (s->cursize < MAXBITS) {
  189. s->top_slot <<= 1;
  190. s->curmask = mask[++s->cursize];
  191. }
  192. }
  193. while (sp > s->stack) {
  194. *buf++ = *(--sp);
  195. if ((--l) == 0)
  196. goto the_end;
  197. }
  198. }
  199. }
  200. the_end:
  201. s->sp = sp;
  202. s->oc = oc;
  203. s->fc = fc;
  204. return len - l;
  205. }
  206. static int gif_read_image(AVFormatContext * s1, AVPacket * pkt)
  207. {
  208. GifState *s = s1->priv_data;
  209. int left, top, width, height, bits_per_pixel, code_size, flags;
  210. int is_interleaved, has_local_palette, y, x, linesize;
  211. ByteIOContext *f = &s1->pb;
  212. uint8_t *ptr, *line, *d, *spal, *palette, *sptr;
  213. left = get_le16(f);
  214. top = get_le16(f);
  215. width = get_le16(f);
  216. height = get_le16(f);
  217. flags = get_byte(f);
  218. is_interleaved = flags & 0x40;
  219. has_local_palette = flags & 0x80;
  220. bits_per_pixel = (flags & 0x07) + 1;
  221. #ifdef DEBUG
  222. printf("gif: image x=%d y=%d w=%d h=%d\n", left, top, width, height);
  223. #endif
  224. if (has_local_palette) {
  225. get_buffer(f, s->local_palette, 3 * (1 << bits_per_pixel));
  226. palette = s->local_palette;
  227. } else {
  228. palette = s->global_palette;
  229. }
  230. /* allocate local image (XXX: horrible, needs API change) */
  231. if (s->image_count == 0) {
  232. /* modify screen width/height if invalid */
  233. if (left + width > s->screen_width)
  234. s->screen_width = left + width;
  235. if (top + height > s->screen_height)
  236. s->screen_width = top + height;
  237. s->image_buf = av_malloc(s->screen_width * s->screen_height * 3);
  238. if (!s->image_buf)
  239. return -ENOMEM;
  240. } else {
  241. /* verify that all the image is inside the screen dimensions */
  242. if (left + width > s->screen_width ||
  243. top + height > s->screen_height)
  244. return -EINVAL;
  245. }
  246. s->image_count++;
  247. line = av_malloc(width);
  248. if (!line)
  249. return -ENOMEM;
  250. /* now get the image data */
  251. s->f = f;
  252. code_size = get_byte(f);
  253. GLZWDecodeInit(s, code_size);
  254. /* read all the image and transcode it to RGB24 (horrible) */
  255. linesize = s->screen_width * 3;
  256. ptr = s->image_buf + top * linesize + (left * 3);
  257. for (y = 0; y < height; y++) {
  258. GLZWDecode(s, line, width);
  259. d = ptr;
  260. sptr = line;
  261. for(x = 0; x < width; x++) {
  262. spal = palette + sptr[0] * 3;
  263. d[0] = spal[0];
  264. d[1] = spal[1];
  265. d[2] = spal[2];
  266. d += 3;
  267. sptr++;
  268. }
  269. ptr += linesize;
  270. }
  271. av_free(line);
  272. /* read the garbage data until end marker is found */
  273. while (!s->eob_reached)
  274. GetCode(s);
  275. /* output image */
  276. /* XXX: avoid copying */
  277. if (av_new_packet(pkt, s->screen_width * s->screen_height * 3)) {
  278. av_free(line);
  279. return -EIO;
  280. }
  281. pkt->stream_index = 0;
  282. memcpy(pkt->data, s->image_buf, s->screen_width * s->screen_height * 3);
  283. return 0;
  284. }
  285. static int gif_read_extension(AVFormatContext * s1)
  286. {
  287. GifState *s = s1->priv_data;
  288. ByteIOContext *f = &s1->pb;
  289. int ext_code, ext_len, i, gce_flags, gce_transparent_index;
  290. /* extension */
  291. ext_code = get_byte(f);
  292. ext_len = get_byte(f);
  293. #ifdef DEBUG
  294. printf("gif: ext_code=0x%x len=%d\n", ext_code, ext_len);
  295. #endif
  296. switch(ext_code) {
  297. case 0xf9:
  298. if (ext_len != 4)
  299. goto discard_ext;
  300. s->transparent_color_index = -1;
  301. gce_flags = get_byte(f);
  302. s->gce_delay = get_le16(f);
  303. gce_transparent_index = get_byte(f);
  304. if (gce_flags & 0x01)
  305. s->transparent_color_index = gce_transparent_index;
  306. else
  307. s->transparent_color_index = -1;
  308. s->gce_disposal = (gce_flags >> 2) & 0x7;
  309. #ifdef DEBUG
  310. printf("gif: gce_flags=%x delay=%d tcolor=%d disposal=%d\n",
  311. gce_flags, s->gce_delay,
  312. s->transparent_color_index, s->gce_disposal);
  313. #endif
  314. ext_len = get_byte(f);
  315. break;
  316. }
  317. /* NOTE: many extension blocks can come after */
  318. discard_ext:
  319. while (ext_len != 0) {
  320. for (i = 0; i < ext_len; i++)
  321. get_byte(f);
  322. ext_len = get_byte(f);
  323. #ifdef DEBUG
  324. printf("gif: ext_len1=%d\n", ext_len);
  325. #endif
  326. }
  327. return 0;
  328. }
  329. static int gif_read_header(AVFormatContext * s1,
  330. AVFormatParameters * ap)
  331. {
  332. GifState *s = s1->priv_data;
  333. ByteIOContext *f = &s1->pb;
  334. AVStream *st;
  335. uint8_t sig[6];
  336. int ret, v, n;
  337. int has_global_palette;
  338. /* read gif signature */
  339. ret = get_buffer(f, sig, 6);
  340. if (ret != 6)
  341. return -1;
  342. if (memcmp(sig, gif87a_sig, 6) != 0 &&
  343. memcmp(sig, gif89a_sig, 6) != 0)
  344. return -1;
  345. /* read screen header */
  346. s->transparent_color_index = -1;
  347. s->screen_width = get_le16(f);
  348. s->screen_height = get_le16(f);
  349. v = get_byte(f);
  350. s->color_resolution = ((v & 0x70) >> 4) + 1;
  351. has_global_palette = (v & 0x80);
  352. s->bits_per_pixel = (v & 0x07) + 1;
  353. s->background_color_index = get_byte(f);
  354. get_byte(f); /* ignored */
  355. #ifdef DEBUG
  356. printf("gif: screen_w=%d screen_h=%d bpp=%d global_palette=%d\n",
  357. s->screen_width, s->screen_height, s->bits_per_pixel,
  358. has_global_palette);
  359. #endif
  360. if (has_global_palette) {
  361. n = 1 << s->bits_per_pixel;
  362. get_buffer(f, s->global_palette, n * 3);
  363. }
  364. /* now we are ready: build format streams */
  365. st = av_new_stream(s1, 0);
  366. if (!st)
  367. return -1;
  368. st->codec.codec_type = CODEC_TYPE_VIDEO;
  369. st->codec.codec_id = CODEC_ID_RAWVIDEO;
  370. st->codec.frame_rate = 5 * FRAME_RATE_BASE;
  371. /* XXX: check if screen size is always valid */
  372. st->codec.width = s->screen_width;
  373. st->codec.height = s->screen_height;
  374. st->codec.pix_fmt = PIX_FMT_RGB24;
  375. return 0;
  376. }
  377. static int gif_read_packet(AVFormatContext * s1,
  378. AVPacket * pkt)
  379. {
  380. ByteIOContext *f = &s1->pb;
  381. int ret, code;
  382. for (;;) {
  383. code = url_fgetc(f);
  384. #ifdef DEBUG
  385. printf("gif: code=%02x '%c'\n", code, code);
  386. #endif
  387. switch (code) {
  388. case ',':
  389. if (gif_read_image(s1, pkt) < 0)
  390. return -EIO;
  391. ret = 0;
  392. goto the_end;
  393. case ';':
  394. /* end of image */
  395. ret = -EIO;
  396. goto the_end;
  397. case '!':
  398. if (gif_read_extension(s1) < 0)
  399. return -EIO;
  400. break;
  401. case EOF:
  402. default:
  403. /* error or errneous EOF */
  404. ret = -EIO;
  405. goto the_end;
  406. }
  407. }
  408. the_end:
  409. return ret;
  410. }
  411. static int gif_read_close(AVFormatContext *s1)
  412. {
  413. GifState *s = s1->priv_data;
  414. av_free(s->image_buf);
  415. return 0;
  416. }
  417. AVInputFormat gif_iformat =
  418. {
  419. "gif",
  420. "gif format",
  421. sizeof(GifState),
  422. gif_probe,
  423. gif_read_header,
  424. gif_read_packet,
  425. gif_read_close,
  426. };