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.

398 lines
14KB

  1. /*
  2. * Animated GIF encoder
  3. * Copyright (c) 2000 Gerard Lantau.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. * First version by Francois Revol revol@free.fr
  21. *
  22. * Features and limitations:
  23. * - currently no compression is performed,
  24. * in fact the size of the data is 9/8 the size of the image in 8bpp
  25. * - uses only a global standard palette
  26. * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
  27. *
  28. * Reference documents:
  29. * http://www.goice.co.jp/member/mo/formats/gif.html
  30. * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
  31. * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
  32. *
  33. * this url claims to have an LZW algorithm not covered by Unisys patent:
  34. * http://www.msg.net/utility/whirlgif/gifencod.html
  35. * could help reduce the size of the files _a lot_...
  36. * some sites mentions an RLE type compression also.
  37. */
  38. #include "avformat.h"
  39. /* bitstream minipacket size */
  40. #define GIF_CHUNKS 100
  41. /* slows down the decoding (and some browsers doesn't like it) */
  42. /* #define GIF_ADD_APP_HEADER */
  43. typedef struct {
  44. unsigned char r;
  45. unsigned char g;
  46. unsigned char b;
  47. } rgb_triplet;
  48. /* we use the standard 216 color palette */
  49. /* this script was used to create the palette:
  50. * for r in 00 33 66 99 cc ff; do for g in 00 33 66 99 cc ff; do echo -n " "; for b in 00 33 66 99 cc ff; do
  51. * echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
  52. */
  53. const rgb_triplet gif_clut[216] = {
  54. { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
  55. { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
  56. { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
  57. { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
  58. { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
  59. { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
  60. { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
  61. { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
  62. { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
  63. { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
  64. { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
  65. { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
  66. { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
  67. { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
  68. { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
  69. { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
  70. { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
  71. { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
  72. { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
  73. { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
  74. { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
  75. { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
  76. { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
  77. { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
  78. { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
  79. { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
  80. { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
  81. { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
  82. { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
  83. { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
  84. { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
  85. { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
  86. { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
  87. { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
  88. { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
  89. { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
  90. };
  91. /* The GIF format uses reversed order for bitstreams... */
  92. /* at least they don't use PDP_ENDIAN :) */
  93. /* so we 'extend' PutBitContext. hmmm, OOP :) */
  94. /* seems this thing changed slightly since I wrote it... */
  95. #ifdef ALT_BITSTREAM_WRITER
  96. # error no ALT_BITSTREAM_WRITER support for now
  97. #endif
  98. void init_put_bits_rev(PutBitContext *s,
  99. UINT8 *buffer, int buffer_size,
  100. void *opaque,
  101. void (*write_data)(void *, UINT8 *, int))
  102. {
  103. init_put_bits(s, buffer, buffer_size, opaque, write_data);
  104. }
  105. void put_bits_rev(PutBitContext *s, int n, unsigned int value)
  106. {
  107. unsigned int bit_buf;
  108. int bit_cnt;
  109. #ifdef STATS
  110. st_out_bit_counts[st_current_index] += n;
  111. #endif
  112. // printf("put_bits=%d %x\n", n, value);
  113. assert(n == 32 || value < (1U << n));
  114. bit_buf = s->bit_buf;
  115. bit_cnt = 32 - s->bit_left; /* XXX:lazyness... was = s->bit_cnt; */
  116. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  117. /* XXX: optimize */
  118. if (n < (32-bit_cnt)) {
  119. bit_buf |= value << (bit_cnt);
  120. bit_cnt+=n;
  121. } else {
  122. bit_buf |= value << (bit_cnt);
  123. *s->buf_ptr = bit_buf & 0xff;
  124. s->buf_ptr[1] = (bit_buf >> 8) & 0xff;
  125. s->buf_ptr[2] = (bit_buf >> 16) & 0xff;
  126. s->buf_ptr[3] = (bit_buf >> 24) & 0xff;
  127. //printf("bitbuf = %08x\n", bit_buf);
  128. s->buf_ptr+=4;
  129. if (s->buf_ptr >= s->buf_end)
  130. puts("bit buffer overflow !!"); // should never happen ! who got rid of the callback ???
  131. // flush_buffer_rev(s);
  132. bit_cnt=bit_cnt + n - 32;
  133. if (bit_cnt == 0) {
  134. bit_buf = 0;
  135. } else {
  136. bit_buf = value >> (n - bit_cnt);
  137. }
  138. }
  139. s->bit_buf = bit_buf;
  140. s->bit_left = 32 - bit_cnt;
  141. }
  142. /* return the number of bits output */
  143. INT64 get_bit_count_rev(PutBitContext *s)
  144. {
  145. return get_bit_count(s);
  146. }
  147. void align_put_bits_rev(PutBitContext *s)
  148. {
  149. align_put_bits(s);
  150. }
  151. /* pad the end of the output stream with zeros */
  152. void flush_put_bits_rev(PutBitContext *s)
  153. {
  154. while (s->bit_left < 32) {
  155. /* XXX: should test end of buffer */
  156. *s->buf_ptr++=s->bit_buf & 0xff;
  157. s->bit_buf>>=8;
  158. s->bit_left+=8;
  159. }
  160. // flush_buffer_rev(s);
  161. s->bit_left=32;
  162. s->bit_buf=0;
  163. }
  164. /* !RevPutBitContext */
  165. typedef struct {
  166. UINT8 buffer[100]; /* data chunks */
  167. INT64 time, file_time;
  168. } GIFContext;
  169. static int gif_write_header(AVFormatContext *s)
  170. {
  171. GIFContext *gif = s->priv_data;
  172. ByteIOContext *pb = &s->pb;
  173. AVCodecContext *enc, *video_enc;
  174. int i, width, height, rate;
  175. /* XXX: do we reject audio streams or just ignore them ?
  176. if(s->nb_streams > 1)
  177. return -1;
  178. */
  179. gif->time = 0;
  180. gif->file_time = 0;
  181. video_enc = NULL;
  182. for(i=0;i<s->nb_streams;i++) {
  183. enc = &s->streams[i]->codec;
  184. if (enc->codec_type != CODEC_TYPE_AUDIO)
  185. video_enc = enc;
  186. }
  187. if (!video_enc) {
  188. av_free(gif);
  189. return -1;
  190. } else {
  191. width = video_enc->width;
  192. height = video_enc->height;
  193. rate = video_enc->frame_rate;
  194. }
  195. /* XXX: is it allowed ? seems to work so far... */
  196. video_enc->pix_fmt = PIX_FMT_RGB24;
  197. /* GIF header */
  198. put_tag(pb, "GIF");
  199. put_tag(pb, "89a");
  200. put_le16(pb, width);
  201. put_le16(pb, height);
  202. put_byte(pb, 0xf7); /* flags: global clut, 256 entries */
  203. put_byte(pb, 0x1f); /* background color index */
  204. put_byte(pb, 0); /* aspect ratio */
  205. /* the global palette */
  206. put_buffer(pb, (unsigned char *)gif_clut, 216*3);
  207. for(i=0;i<((256-216)*3);i++)
  208. put_byte(pb, 0);
  209. /* application extension header */
  210. /* XXX: not really sure what to put in here... */
  211. #ifdef GIF_ADD_APP_HEADER
  212. put_byte(pb, 0x21);
  213. put_byte(pb, 0xff);
  214. put_byte(pb, 0x0b);
  215. put_tag(pb, "NETSCAPE2.0");
  216. put_byte(pb, 0x03);
  217. put_byte(pb, 0x01);
  218. put_byte(pb, 0x00);
  219. put_byte(pb, 0x00);
  220. #endif
  221. put_flush_packet(&s->pb);
  222. return 0;
  223. }
  224. /* this is maybe slow, but allows for extensions */
  225. static inline unsigned char gif_clut_index(rgb_triplet *clut, UINT8 r, UINT8 g, UINT8 b)
  226. {
  227. return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
  228. }
  229. /* chunk writer callback */
  230. /* !!! XXX:deprecated
  231. static void gif_put_chunk(void *pbctx, UINT8 *buffer, int count)
  232. {
  233. ByteIOContext *pb = (ByteIOContext *)pbctx;
  234. put_byte(pb, (UINT8)count);
  235. put_buffer(pb, buffer, count);
  236. }
  237. */
  238. static int gif_write_video(AVFormatContext *s,
  239. AVCodecContext *enc, UINT8 *buf, int size)
  240. {
  241. ByteIOContext *pb = &s->pb;
  242. GIFContext *gif = s->priv_data;
  243. int i, left, jiffies;
  244. INT64 delay;
  245. PutBitContext p;
  246. UINT8 buffer[200]; /* 100 * 9 / 8 = 113 */
  247. /* graphic control extension block */
  248. put_byte(pb, 0x21);
  249. put_byte(pb, 0xf9);
  250. put_byte(pb, 0x04); /* block size */
  251. put_byte(pb, 0x04); /* flags */
  252. /* 1 jiffy is 1/70 s */
  253. /* the delay_time field indicates the number of jiffies - 1 */
  254. delay = gif->file_time - gif->time;
  255. /* XXX: should use delay, in order to be more accurate */
  256. /* instead of using the same rounded value each time */
  257. /* XXX: don't even remember if I really use it for now */
  258. jiffies = (70*FRAME_RATE_BASE/enc->frame_rate) - 1;
  259. put_le16(pb, jiffies);
  260. put_byte(pb, 0x1f); /* transparent color index */
  261. put_byte(pb, 0x00);
  262. /* image block */
  263. put_byte(pb, 0x2c);
  264. put_le16(pb, 0);
  265. put_le16(pb, 0);
  266. put_le16(pb, enc->width);
  267. put_le16(pb, enc->height);
  268. put_byte(pb, 0x00); /* flags */
  269. /* no local clut */
  270. put_byte(pb, 0x08);
  271. left=size/3;
  272. /* XXX:deprecated */
  273. /*init_put_bits_rev(&p, buffer, sizeof(buf), (void *)pb, gif_put_chunk); *//* mmm found a but in my code: s/sizeof(buf)/150/ */
  274. init_put_bits_rev(&p, buffer, 130, NULL, NULL);
  275. /*
  276. * the thing here is the bitstream is written as little packets, with a size byte before
  277. * but it's still the same bitstream between packets (no flush !)
  278. */
  279. while(left>0) {
  280. put_bits_rev(&p, 9, 0x0100); /* clear code */
  281. for(i=0;i<GIF_CHUNKS;i++) {
  282. put_bits_rev(&p, 9, gif_clut_index(NULL, *buf, buf[1], buf[2]));
  283. buf+=3;
  284. }
  285. if(left<=GIF_CHUNKS) {
  286. put_bits_rev(&p, 9, 0x101); /* end of stream */
  287. flush_put_bits_rev(&p);
  288. }
  289. if(pbBufPtr(&p) - p.buf > 0) {
  290. put_byte(pb, pbBufPtr(&p) - p.buf); /* byte count of the packet */
  291. put_buffer(pb, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
  292. p.data_out_size += pbBufPtr(&p) - p.buf;
  293. p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
  294. }
  295. if(left<=GIF_CHUNKS) {
  296. put_byte(pb, 0x00); /* end of image block */
  297. }
  298. left-=GIF_CHUNKS;
  299. }
  300. put_flush_packet(&s->pb);
  301. return 0;
  302. }
  303. static int gif_write_packet(AVFormatContext *s, int stream_index,
  304. UINT8 *buf, int size, int force_pts)
  305. {
  306. AVCodecContext *codec = &s->streams[stream_index]->codec;
  307. if (codec->codec_type == CODEC_TYPE_AUDIO)
  308. return 0; /* just ignore audio */
  309. else
  310. return gif_write_video(s, codec, buf, size);
  311. }
  312. static int gif_write_trailer(AVFormatContext *s)
  313. {
  314. ByteIOContext *pb = &s->pb;
  315. put_byte(pb, 0x3b);
  316. put_flush_packet(&s->pb);
  317. return 0;
  318. }
  319. static AVOutputFormat gif_oformat = {
  320. "gif",
  321. "GIF Animation",
  322. "image/gif",
  323. "gif",
  324. sizeof(GIFContext),
  325. CODEC_ID_NONE,
  326. CODEC_ID_RAWVIDEO,
  327. gif_write_header,
  328. gif_write_packet,
  329. gif_write_trailer,
  330. };
  331. int gif_init(void)
  332. {
  333. av_register_output_format(&gif_oformat);
  334. return 0;
  335. }