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.

420 lines
16KB

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