gif.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. #include "libavutil/log.h"
  42. #include "libavutil/opt.h"
  43. /* The GIF format uses reversed order for bitstreams... */
  44. /* at least they don't use PDP_ENDIAN :) */
  45. #define BITSTREAM_WRITER_LE
  46. #include "libavcodec/put_bits.h"
  47. /* bitstream minipacket size */
  48. #define GIF_CHUNKS 100
  49. /* slows down the decoding (and some browsers don't like it) */
  50. /* update on the 'some browsers don't like it issue from above:
  51. * this was probably due to missing 'Data Sub-block Terminator'
  52. * (byte 19) in the app_header */
  53. #define GIF_ADD_APP_HEADER // required to enable looping of animated gif
  54. typedef struct {
  55. unsigned char r;
  56. unsigned char g;
  57. unsigned char b;
  58. } rgb_triplet;
  59. /* we use the standard 216 color palette */
  60. /* this script was used to create the palette:
  61. * for r in 00 33 66 99 cc ff; do
  62. * for g in 00 33 66 99 cc ff; do
  63. * echo -n " "
  64. * for b in 00 33 66 99 cc ff; do
  65. * echo -n "{ 0x$r, 0x$g, 0x$b }, "
  66. * done
  67. * echo ""
  68. * done
  69. * done
  70. */
  71. static const rgb_triplet gif_clut[216] = {
  72. { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
  73. { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
  74. { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
  75. { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
  76. { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
  77. { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
  78. { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
  79. { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
  80. { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
  81. { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
  82. { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
  83. { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
  84. { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
  85. { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
  86. { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
  87. { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
  88. { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
  89. { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
  90. { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
  91. { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
  92. { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
  93. { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
  94. { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
  95. { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
  96. { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
  97. { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
  98. { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
  99. { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
  100. { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
  101. { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
  102. { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
  103. { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
  104. { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
  105. { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
  106. { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
  107. { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
  108. };
  109. /* GIF header */
  110. static int gif_image_write_header(AVIOContext *pb, int width, int height,
  111. int loop_count, uint32_t *palette)
  112. {
  113. int i;
  114. unsigned int v;
  115. avio_write(pb, "GIF", 3);
  116. avio_write(pb, "89a", 3);
  117. avio_wl16(pb, width);
  118. avio_wl16(pb, height);
  119. avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
  120. avio_w8(pb, 0x1f); /* background color index */
  121. avio_w8(pb, 0); /* aspect ratio */
  122. /* the global palette */
  123. if (!palette) {
  124. avio_write(pb, (const unsigned char *)gif_clut, 216 * 3);
  125. for (i = 0; i < ((256 - 216) * 3); i++)
  126. avio_w8(pb, 0);
  127. } else {
  128. for (i = 0; i < 256; i++) {
  129. v = palette[i];
  130. avio_w8(pb, (v >> 16) & 0xff);
  131. avio_w8(pb, (v >> 8) & 0xff);
  132. avio_w8(pb, (v) & 0xff);
  133. }
  134. }
  135. /* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated
  136. * GIF, see http://members.aol.com/royalef/gifabout.htm#net-extension
  137. *
  138. * byte 1 : 33 (hex 0x21) GIF Extension code
  139. * byte 2 : 255 (hex 0xFF) Application Extension Label
  140. * byte 3 : 11 (hex (0x0B) Length of Application Block
  141. * (eleven bytes of data to follow)
  142. * bytes 4 to 11 : "NETSCAPE"
  143. * bytes 12 to 14 : "2.0"
  144. * byte 15 : 3 (hex 0x03) Length of Data Sub-Block
  145. * (three bytes of data to follow)
  146. * byte 16 : 1 (hex 0x01)
  147. * bytes 17 to 18 : 0 to 65535, an unsigned integer in
  148. * lo-hi byte format. This indicate the
  149. * number of iterations the loop should
  150. * be executed.
  151. * bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
  152. */
  153. /* application extension header */
  154. #ifdef GIF_ADD_APP_HEADER
  155. if (loop_count >= 0 && loop_count <= 65535) {
  156. avio_w8(pb, 0x21);
  157. avio_w8(pb, 0xff);
  158. avio_w8(pb, 0x0b);
  159. // bytes 4 to 14
  160. avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
  161. avio_w8(pb, 0x03); // byte 15
  162. avio_w8(pb, 0x01); // byte 16
  163. avio_wl16(pb, (uint16_t)loop_count);
  164. avio_w8(pb, 0x00); // byte 19
  165. }
  166. #endif
  167. return 0;
  168. }
  169. /* this is maybe slow, but allows for extensions */
  170. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  171. {
  172. return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
  173. }
  174. static int gif_image_write_image(AVIOContext *pb,
  175. int x1, int y1, int width, int height,
  176. const uint8_t *buf, int linesize, int pix_fmt)
  177. {
  178. PutBitContext p;
  179. uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
  180. int i, left, w, v;
  181. const uint8_t *ptr;
  182. /* image block */
  183. avio_w8(pb, 0x2c);
  184. avio_wl16(pb, x1);
  185. avio_wl16(pb, y1);
  186. avio_wl16(pb, width);
  187. avio_wl16(pb, height);
  188. avio_w8(pb, 0x00); /* flags */
  189. /* no local clut */
  190. avio_w8(pb, 0x08);
  191. left = width * height;
  192. init_put_bits(&p, buffer, 130);
  193. /*
  194. * the thing here is the bitstream is written as little packets, with a size
  195. * byte before but it's still the same bitstream between packets (no flush !)
  196. */
  197. ptr = buf;
  198. w = width;
  199. while (left > 0) {
  200. put_bits(&p, 9, 0x0100); /* clear code */
  201. for (i = (left < GIF_CHUNKS) ? left : GIF_CHUNKS; i; i--) {
  202. if (pix_fmt == PIX_FMT_RGB24) {
  203. v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
  204. ptr += 3;
  205. } else {
  206. v = *ptr++;
  207. }
  208. put_bits(&p, 9, v);
  209. if (--w == 0) {
  210. w = width;
  211. buf += linesize;
  212. ptr = buf;
  213. }
  214. }
  215. if (left <= GIF_CHUNKS) {
  216. put_bits(&p, 9, 0x101); /* end of stream */
  217. flush_put_bits(&p);
  218. }
  219. if (put_bits_ptr(&p) - p.buf > 0) {
  220. avio_w8(pb, put_bits_ptr(&p) - p.buf); /* byte count of the packet */
  221. avio_write(pb, p.buf, put_bits_ptr(&p) - p.buf); /* the actual buffer */
  222. p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
  223. }
  224. left -= GIF_CHUNKS;
  225. }
  226. avio_w8(pb, 0x00); /* end of image block */
  227. return 0;
  228. }
  229. typedef struct {
  230. AVClass *class; /** Class for private options. */
  231. int64_t time, file_time;
  232. uint8_t buffer[100]; /* data chunks */
  233. int loop;
  234. } GIFContext;
  235. static int gif_write_header(AVFormatContext *s)
  236. {
  237. GIFContext *gif = s->priv_data;
  238. AVIOContext *pb = s->pb;
  239. AVCodecContext *enc, *video_enc;
  240. int i, width, height /*, rate*/;
  241. /* XXX: do we reject audio streams or just ignore them ?
  242. * if (s->nb_streams > 1)
  243. * return -1;
  244. */
  245. gif->time = 0;
  246. gif->file_time = 0;
  247. video_enc = NULL;
  248. for (i = 0; i < s->nb_streams; i++) {
  249. enc = s->streams[i]->codec;
  250. if (enc->codec_type != AVMEDIA_TYPE_AUDIO)
  251. video_enc = enc;
  252. }
  253. if (!video_enc) {
  254. av_free(gif);
  255. return -1;
  256. } else {
  257. width = video_enc->width;
  258. height = video_enc->height;
  259. // rate = video_enc->time_base.den;
  260. }
  261. if (video_enc->pix_fmt != PIX_FMT_RGB24) {
  262. av_log(s, AV_LOG_ERROR,
  263. "ERROR: gif only handles the rgb24 pixel format. Use -pix_fmt rgb24.\n");
  264. return AVERROR(EIO);
  265. }
  266. gif_image_write_header(pb, width, height, gif->loop, NULL);
  267. avio_flush(s->pb);
  268. return 0;
  269. }
  270. static int gif_write_video(AVFormatContext *s, AVCodecContext *enc,
  271. const uint8_t *buf, int size)
  272. {
  273. AVIOContext *pb = s->pb;
  274. int jiffies;
  275. /* graphic control extension block */
  276. avio_w8(pb, 0x21);
  277. avio_w8(pb, 0xf9);
  278. avio_w8(pb, 0x04); /* block size */
  279. avio_w8(pb, 0x04); /* flags */
  280. /* 1 jiffy is 1/70 s */
  281. /* the delay_time field indicates the number of jiffies - 1 */
  282. /* XXX: should use delay, in order to be more accurate */
  283. /* instead of using the same rounded value each time */
  284. /* XXX: don't even remember if I really use it for now */
  285. jiffies = (70 * enc->time_base.num / enc->time_base.den) - 1;
  286. avio_wl16(pb, jiffies);
  287. avio_w8(pb, 0x1f); /* transparent color index */
  288. avio_w8(pb, 0x00);
  289. gif_image_write_image(pb, 0, 0, enc->width, enc->height,
  290. buf, enc->width * 3, PIX_FMT_RGB24);
  291. avio_flush(s->pb);
  292. return 0;
  293. }
  294. static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
  295. {
  296. AVCodecContext *codec = s->streams[pkt->stream_index]->codec;
  297. if (codec->codec_type == AVMEDIA_TYPE_AUDIO)
  298. return 0; /* just ignore audio */
  299. else
  300. return gif_write_video(s, codec, pkt->data, pkt->size);
  301. }
  302. static int gif_write_trailer(AVFormatContext *s)
  303. {
  304. AVIOContext *pb = s->pb;
  305. avio_w8(pb, 0x3b);
  306. return 0;
  307. }
  308. #define OFFSET(x) offsetof(GIFContext, x)
  309. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  310. static const AVOption options[] = {
  311. { "loop", "Number of times to loop the output.", OFFSET(loop),
  312. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, ENC },
  313. { NULL },
  314. };
  315. static const AVClass gif_muxer_class = {
  316. .class_name = "GIF muxer",
  317. .item_name = av_default_item_name,
  318. .version = LIBAVUTIL_VERSION_INT,
  319. .option = options,
  320. };
  321. AVOutputFormat ff_gif_muxer = {
  322. .name = "gif",
  323. .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
  324. .mime_type = "image/gif",
  325. .extensions = "gif",
  326. .priv_data_size = sizeof(GIFContext),
  327. .audio_codec = AV_CODEC_ID_NONE,
  328. .video_codec = AV_CODEC_ID_RAWVIDEO,
  329. .write_header = gif_write_header,
  330. .write_packet = gif_write_packet,
  331. .write_trailer = gif_write_trailer,
  332. .priv_class = &gif_muxer_class,
  333. };