gif.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Animated GIF muxer
  3. * Copyright (c) 2000 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. /*
  22. * First version by Francois Revol revol@free.fr
  23. *
  24. * Features and limitations:
  25. * - currently no compression is performed,
  26. * in fact the size of the data is 9/8 the size of the image in 8bpp
  27. * - uses only a global standard palette
  28. * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
  29. *
  30. * Reference documents:
  31. * http://www.goice.co.jp/member/mo/formats/gif.html
  32. * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
  33. * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
  34. *
  35. * this url claims to have an LZW algorithm not covered by Unisys patent:
  36. * http://www.msg.net/utility/whirlgif/gifencod.html
  37. * could help reduce the size of the files _a lot_...
  38. * some sites mentions an RLE type compression also.
  39. */
  40. #include "avformat.h"
  41. /* The GIF format uses reversed order for bitstreams... */
  42. /* at least they don't use PDP_ENDIAN :) */
  43. #define BITSTREAM_WRITER_LE
  44. #include "libavcodec/put_bits.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. /* GIF header */
  99. static int gif_image_write_header(AVIOContext *pb,
  100. int width, int height, int loop_count,
  101. uint32_t *palette)
  102. {
  103. int i;
  104. unsigned int v;
  105. avio_write(pb, "GIF", 3);
  106. avio_write(pb, "89a", 3);
  107. avio_wl16(pb, width);
  108. avio_wl16(pb, height);
  109. avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
  110. avio_w8(pb, 0x1f); /* background color index */
  111. avio_w8(pb, 0); /* aspect ratio */
  112. /* the global palette */
  113. if (!palette) {
  114. avio_write(pb, (const unsigned char *)gif_clut, 216*3);
  115. for(i=0;i<((256-216)*3);i++)
  116. avio_w8(pb, 0);
  117. } else {
  118. for(i=0;i<256;i++) {
  119. v = palette[i];
  120. avio_w8(pb, (v >> 16) & 0xff);
  121. avio_w8(pb, (v >> 8) & 0xff);
  122. avio_w8(pb, (v) & 0xff);
  123. }
  124. }
  125. /* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
  126. see http://members.aol.com/royalef/gifabout.htm#net-extension
  127. byte 1 : 33 (hex 0x21) GIF Extension code
  128. byte 2 : 255 (hex 0xFF) Application Extension Label
  129. byte 3 : 11 (hex (0x0B) Length of Application Block
  130. (eleven bytes of data to follow)
  131. bytes 4 to 11 : "NETSCAPE"
  132. bytes 12 to 14 : "2.0"
  133. byte 15 : 3 (hex 0x03) Length of Data Sub-Block
  134. (three bytes of data to follow)
  135. byte 16 : 1 (hex 0x01)
  136. bytes 17 to 18 : 0 to 65535, an unsigned integer in
  137. lo-hi byte format. This indicate the
  138. number of iterations the loop should
  139. be executed.
  140. bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
  141. */
  142. /* application extension header */
  143. #ifdef GIF_ADD_APP_HEADER
  144. if (loop_count >= 0 && loop_count <= 65535) {
  145. avio_w8(pb, 0x21);
  146. avio_w8(pb, 0xff);
  147. avio_w8(pb, 0x0b);
  148. avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1); // bytes 4 to 14
  149. avio_w8(pb, 0x03); // byte 15
  150. avio_w8(pb, 0x01); // byte 16
  151. avio_wl16(pb, (uint16_t)loop_count);
  152. avio_w8(pb, 0x00); // byte 19
  153. }
  154. #endif
  155. return 0;
  156. }
  157. /* this is maybe slow, but allows for extensions */
  158. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  159. {
  160. return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
  161. }
  162. static int gif_image_write_image(AVIOContext *pb,
  163. int x1, int y1, int width, int height,
  164. const uint8_t *buf, int linesize, int pix_fmt)
  165. {
  166. PutBitContext p;
  167. uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
  168. int i, left, w, v;
  169. const uint8_t *ptr;
  170. /* image block */
  171. avio_w8(pb, 0x2c);
  172. avio_wl16(pb, x1);
  173. avio_wl16(pb, y1);
  174. avio_wl16(pb, width);
  175. avio_wl16(pb, height);
  176. avio_w8(pb, 0x00); /* flags */
  177. /* no local clut */
  178. avio_w8(pb, 0x08);
  179. left= width * height;
  180. init_put_bits(&p, buffer, 130);
  181. /*
  182. * the thing here is the bitstream is written as little packets, with a size byte before
  183. * but it's still the same bitstream between packets (no flush !)
  184. */
  185. ptr = buf;
  186. w = width;
  187. while(left>0) {
  188. put_bits(&p, 9, 0x0100); /* clear code */
  189. for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
  190. if (pix_fmt == PIX_FMT_RGB24) {
  191. v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
  192. ptr+=3;
  193. } else {
  194. v = *ptr++;
  195. }
  196. put_bits(&p, 9, v);
  197. if (--w == 0) {
  198. w = width;
  199. buf += linesize;
  200. ptr = buf;
  201. }
  202. }
  203. if(left<=GIF_CHUNKS) {
  204. put_bits(&p, 9, 0x101); /* end of stream */
  205. flush_put_bits(&p);
  206. }
  207. if(put_bits_ptr(&p) - p.buf > 0) {
  208. avio_w8(pb, put_bits_ptr(&p) - p.buf); /* byte count of the packet */
  209. avio_write(pb, p.buf, put_bits_ptr(&p) - p.buf); /* the actual buffer */
  210. p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
  211. }
  212. left-=GIF_CHUNKS;
  213. }
  214. avio_w8(pb, 0x00); /* end of image block */
  215. return 0;
  216. }
  217. typedef struct {
  218. int64_t time, file_time;
  219. uint8_t buffer[100]; /* data chunks */
  220. } GIFContext;
  221. static int gif_write_header(AVFormatContext *s)
  222. {
  223. GIFContext *gif = s->priv_data;
  224. AVIOContext *pb = s->pb;
  225. AVCodecContext *enc, *video_enc;
  226. int i, width, height, loop_count /*, rate*/;
  227. /* XXX: do we reject audio streams or just ignore them ?
  228. if(s->nb_streams > 1)
  229. return -1;
  230. */
  231. gif->time = 0;
  232. gif->file_time = 0;
  233. video_enc = NULL;
  234. for(i=0;i<s->nb_streams;i++) {
  235. enc = s->streams[i]->codec;
  236. if (enc->codec_type != AVMEDIA_TYPE_AUDIO)
  237. video_enc = enc;
  238. }
  239. if (!video_enc) {
  240. av_free(gif);
  241. return -1;
  242. } else {
  243. width = video_enc->width;
  244. height = video_enc->height;
  245. loop_count = s->loop_output;
  246. // rate = video_enc->time_base.den;
  247. }
  248. if (video_enc->pix_fmt != PIX_FMT_RGB24) {
  249. av_log(s, AV_LOG_ERROR, "ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
  250. return AVERROR(EIO);
  251. }
  252. gif_image_write_header(pb, width, height, loop_count, NULL);
  253. avio_flush(s->pb);
  254. return 0;
  255. }
  256. static int gif_write_video(AVFormatContext *s,
  257. AVCodecContext *enc, const uint8_t *buf, int size)
  258. {
  259. AVIOContext *pb = s->pb;
  260. int jiffies;
  261. /* graphic control extension block */
  262. avio_w8(pb, 0x21);
  263. avio_w8(pb, 0xf9);
  264. avio_w8(pb, 0x04); /* block size */
  265. avio_w8(pb, 0x04); /* flags */
  266. /* 1 jiffy is 1/70 s */
  267. /* the delay_time field indicates the number of jiffies - 1 */
  268. /* XXX: should use delay, in order to be more accurate */
  269. /* instead of using the same rounded value each time */
  270. /* XXX: don't even remember if I really use it for now */
  271. jiffies = (70*enc->time_base.num/enc->time_base.den) - 1;
  272. avio_wl16(pb, jiffies);
  273. avio_w8(pb, 0x1f); /* transparent color index */
  274. avio_w8(pb, 0x00);
  275. gif_image_write_image(pb, 0, 0, enc->width, enc->height,
  276. buf, enc->width * 3, PIX_FMT_RGB24);
  277. avio_flush(s->pb);
  278. return 0;
  279. }
  280. static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
  281. {
  282. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  283. if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
  284. return 0; /* just ignore audio */
  285. else
  286. return gif_write_video(s, codec, pkt->data, pkt->size);
  287. }
  288. static int gif_write_trailer(AVFormatContext *s)
  289. {
  290. AVIOContext *pb = s->pb;
  291. avio_w8(pb, 0x3b);
  292. avio_flush(s->pb);
  293. return 0;
  294. }
  295. AVOutputFormat ff_gif_muxer = {
  296. "gif",
  297. NULL_IF_CONFIG_SMALL("GIF Animation"),
  298. "image/gif",
  299. "gif",
  300. sizeof(GIFContext),
  301. CODEC_ID_NONE,
  302. CODEC_ID_RAWVIDEO,
  303. gif_write_header,
  304. gif_write_packet,
  305. gif_write_trailer,
  306. };