gif.c 15 KB

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