gif.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * GIF encoder.
  3. * Copyright (c) 2000 Fabrice Bellard.
  4. * Copyright (c) 2002 Francois Revol.
  5. * Copyright (c) 2006 Baptiste Coudurier.
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * First version by Francois Revol revol@free.fr
  25. *
  26. * Features and limitations:
  27. * - currently no compression is performed,
  28. * in fact the size of the data is 9/8 the size of the image in 8bpp
  29. * - uses only a global standard palette
  30. * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
  31. *
  32. * Reference documents:
  33. * http://www.goice.co.jp/member/mo/formats/gif.html
  34. * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
  35. * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
  36. *
  37. * this url claims to have an LZW algorithm not covered by Unisys patent:
  38. * http://www.msg.net/utility/whirlgif/gifencod.html
  39. * could help reduce the size of the files _a lot_...
  40. * some sites mentions an RLE type compression also.
  41. */
  42. #include "avcodec.h"
  43. #include "bytestream.h"
  44. #include "bitstream.h"
  45. /* bitstream minipacket size */
  46. #define GIF_CHUNKS 100
  47. /* slows down the decoding (and some browsers don't like it) */
  48. /* update on the 'some browsers don't like it issue from above: this was probably due to missing 'Data Sub-block Terminator' (byte 19) in the app_header */
  49. #define GIF_ADD_APP_HEADER // required to enable looping of animated gif
  50. typedef struct {
  51. unsigned char r;
  52. unsigned char g;
  53. unsigned char b;
  54. } rgb_triplet;
  55. /* we use the standard 216 color palette */
  56. /* this script was used to create the palette:
  57. * 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
  58. * echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
  59. */
  60. static const rgb_triplet gif_clut[216] = {
  61. { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
  62. { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
  63. { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
  64. { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
  65. { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
  66. { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
  67. { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
  68. { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
  69. { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
  70. { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
  71. { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
  72. { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
  73. { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
  74. { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
  75. { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
  76. { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
  77. { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
  78. { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
  79. { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
  80. { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
  81. { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
  82. { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
  83. { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
  84. { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
  85. { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
  86. { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
  87. { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
  88. { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
  89. { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
  90. { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
  91. { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
  92. { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
  93. { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
  94. { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
  95. { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
  96. { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
  97. };
  98. /* The GIF format uses reversed order for bitstreams... */
  99. /* at least they don't use PDP_ENDIAN :) */
  100. /* so we 'extend' PutBitContext. hmmm, OOP :) */
  101. /* seems this thing changed slightly since I wrote it... */
  102. #ifdef ALT_BITSTREAM_WRITER
  103. # error no ALT_BITSTREAM_WRITER support for now
  104. #endif
  105. static void gif_put_bits_rev(PutBitContext *s, int n, unsigned int value)
  106. {
  107. unsigned int bit_buf;
  108. int bit_cnt;
  109. // printf("put_bits=%d %x\n", n, value);
  110. assert(n == 32 || value < (1U << n));
  111. bit_buf = s->bit_buf;
  112. bit_cnt = 32 - s->bit_left; /* XXX:lazyness... was = s->bit_cnt; */
  113. // printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
  114. /* XXX: optimize */
  115. if (n < (32-bit_cnt)) {
  116. bit_buf |= value << (bit_cnt);
  117. bit_cnt+=n;
  118. } else {
  119. bit_buf |= value << (bit_cnt);
  120. *s->buf_ptr = bit_buf & 0xff;
  121. s->buf_ptr[1] = (bit_buf >> 8) & 0xff;
  122. s->buf_ptr[2] = (bit_buf >> 16) & 0xff;
  123. s->buf_ptr[3] = (bit_buf >> 24) & 0xff;
  124. //printf("bitbuf = %08x\n", bit_buf);
  125. s->buf_ptr+=4;
  126. if (s->buf_ptr >= s->buf_end)
  127. puts("bit buffer overflow !!"); // should never happen ! who got rid of the callback ???
  128. // flush_buffer_rev(s);
  129. bit_cnt=bit_cnt + n - 32;
  130. if (bit_cnt == 0) {
  131. bit_buf = 0;
  132. } else {
  133. bit_buf = value >> (n - bit_cnt);
  134. }
  135. }
  136. s->bit_buf = bit_buf;
  137. s->bit_left = 32 - bit_cnt;
  138. }
  139. /* pad the end of the output stream with zeros */
  140. static void gif_flush_put_bits_rev(PutBitContext *s)
  141. {
  142. while (s->bit_left < 32) {
  143. /* XXX: should test end of buffer */
  144. *s->buf_ptr++=s->bit_buf & 0xff;
  145. s->bit_buf>>=8;
  146. s->bit_left+=8;
  147. }
  148. // flush_buffer_rev(s);
  149. s->bit_left=32;
  150. s->bit_buf=0;
  151. }
  152. /* !RevPutBitContext */
  153. /* GIF header */
  154. static int gif_image_write_header(uint8_t **bytestream,
  155. int width, int height, int loop_count,
  156. uint32_t *palette)
  157. {
  158. int i;
  159. unsigned int v;
  160. bytestream_put_buffer(bytestream, "GIF", 3);
  161. bytestream_put_buffer(bytestream, "89a", 3);
  162. bytestream_put_le16(bytestream, width);
  163. bytestream_put_le16(bytestream, height);
  164. bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
  165. bytestream_put_byte(bytestream, 0x1f); /* background color index */
  166. bytestream_put_byte(bytestream, 0); /* aspect ratio */
  167. /* the global palette */
  168. if (!palette) {
  169. bytestream_put_buffer(bytestream, (const unsigned char *)gif_clut, 216*3);
  170. for(i=0;i<((256-216)*3);i++)
  171. bytestream_put_byte(bytestream, 0);
  172. } else {
  173. for(i=0;i<256;i++) {
  174. v = palette[i];
  175. bytestream_put_byte(bytestream, (v >> 16) & 0xff);
  176. bytestream_put_byte(bytestream, (v >> 8) & 0xff);
  177. bytestream_put_byte(bytestream, (v) & 0xff);
  178. }
  179. }
  180. /* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
  181. see http://members.aol.com/royalef/gifabout.htm#net-extension
  182. byte 1 : 33 (hex 0x21) GIF Extension code
  183. byte 2 : 255 (hex 0xFF) Application Extension Label
  184. byte 3 : 11 (hex (0x0B) Length of Application Block
  185. (eleven bytes of data to follow)
  186. bytes 4 to 11 : "NETSCAPE"
  187. bytes 12 to 14 : "2.0"
  188. byte 15 : 3 (hex 0x03) Length of Data Sub-Block
  189. (three bytes of data to follow)
  190. byte 16 : 1 (hex 0x01)
  191. bytes 17 to 18 : 0 to 65535, an unsigned integer in
  192. lo-hi byte format. This indicate the
  193. number of iterations the loop should
  194. be executed.
  195. bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
  196. */
  197. /* application extension header */
  198. #ifdef GIF_ADD_APP_HEADER
  199. if (loop_count >= 0 && loop_count <= 65535) {
  200. bytestream_put_byte(bytestream, 0x21);
  201. bytestream_put_byte(bytestream, 0xff);
  202. bytestream_put_byte(bytestream, 0x0b);
  203. bytestream_put_buffer(bytestream, "NETSCAPE2.0", 11); // bytes 4 to 14
  204. bytestream_put_byte(bytestream, 0x03); // byte 15
  205. bytestream_put_byte(bytestream, 0x01); // byte 16
  206. bytestream_put_le16(bytestream, (uint16_t)loop_count);
  207. bytestream_put_byte(bytestream, 0x00); // byte 19
  208. }
  209. #endif
  210. return 0;
  211. }
  212. /* this is maybe slow, but allows for extensions */
  213. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  214. {
  215. return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
  216. }
  217. static int gif_image_write_image(uint8_t **bytestream,
  218. int x1, int y1, int width, int height,
  219. const uint8_t *buf, int linesize, int pix_fmt)
  220. {
  221. PutBitContext p;
  222. uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
  223. int i, left, w, v;
  224. const uint8_t *ptr;
  225. /* image block */
  226. bytestream_put_byte(bytestream, 0x2c);
  227. bytestream_put_le16(bytestream, x1);
  228. bytestream_put_le16(bytestream, y1);
  229. bytestream_put_le16(bytestream, width);
  230. bytestream_put_le16(bytestream, height);
  231. bytestream_put_byte(bytestream, 0x00); /* flags */
  232. /* no local clut */
  233. bytestream_put_byte(bytestream, 0x08);
  234. left= width * height;
  235. init_put_bits(&p, buffer, 130);
  236. /*
  237. * the thing here is the bitstream is written as little packets, with a size byte before
  238. * but it's still the same bitstream between packets (no flush !)
  239. */
  240. ptr = buf;
  241. w = width;
  242. while(left>0) {
  243. gif_put_bits_rev(&p, 9, 0x0100); /* clear code */
  244. for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
  245. if (pix_fmt == PIX_FMT_RGB24) {
  246. v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
  247. ptr+=3;
  248. } else {
  249. v = *ptr++;
  250. }
  251. gif_put_bits_rev(&p, 9, v);
  252. if (--w == 0) {
  253. w = width;
  254. buf += linesize;
  255. ptr = buf;
  256. }
  257. }
  258. if(left<=GIF_CHUNKS) {
  259. gif_put_bits_rev(&p, 9, 0x101); /* end of stream */
  260. gif_flush_put_bits_rev(&p);
  261. }
  262. if(pbBufPtr(&p) - p.buf > 0) {
  263. bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf); /* byte count of the packet */
  264. bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
  265. p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
  266. }
  267. left-=GIF_CHUNKS;
  268. }
  269. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  270. bytestream_put_byte(bytestream, 0x3b);
  271. return 0;
  272. }
  273. typedef struct {
  274. int64_t time, file_time;
  275. uint8_t buffer[100]; /* data chunks */
  276. AVFrame picture;
  277. } GIFContext;
  278. static int gif_encode_init(AVCodecContext *avctx)
  279. {
  280. GIFContext *s = avctx->priv_data;
  281. avctx->coded_frame = &s->picture;
  282. return 0;
  283. }
  284. /* better than nothing gif encoder */
  285. static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
  286. {
  287. GIFContext *s = avctx->priv_data;
  288. AVFrame *pict = data;
  289. AVFrame *const p = (AVFrame *)&s->picture;
  290. uint8_t *outbuf_ptr = outbuf;
  291. *p = *pict;
  292. p->pict_type = FF_I_TYPE;
  293. p->key_frame = 1;
  294. gif_image_write_header(&outbuf_ptr, avctx->width, avctx->height, -1, (uint32_t *)pict->data[1]);
  295. gif_image_write_image(&outbuf_ptr, 0, 0, avctx->width, avctx->height, pict->data[0], pict->linesize[0], PIX_FMT_PAL8);
  296. return outbuf_ptr - outbuf;
  297. }
  298. AVCodec gif_encoder = {
  299. "gif",
  300. CODEC_TYPE_VIDEO,
  301. CODEC_ID_GIF,
  302. sizeof(GIFContext),
  303. gif_encode_init,
  304. gif_encode_frame,
  305. NULL, //encode_end,
  306. .pix_fmts= (enum PixelFormat[]){PIX_FMT_PAL8, -1},
  307. };