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.

289 lines
13KB

  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. /* The GIF format uses reversed order for bitstreams... */
  45. /* at least they don't use PDP_ENDIAN :) */
  46. #define BITSTREAM_WRITER_LE
  47. #include "bitstream.h"
  48. /* bitstream minipacket size */
  49. #define GIF_CHUNKS 100
  50. /* slows down the decoding (and some browsers don't like it) */
  51. /* 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 */
  52. #define GIF_ADD_APP_HEADER // required to enable looping of animated gif
  53. typedef struct {
  54. unsigned char r;
  55. unsigned char g;
  56. unsigned char b;
  57. } rgb_triplet;
  58. /* we use the standard 216 color palette */
  59. /* this script was used to create the palette:
  60. * 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
  61. * echo -n "{ 0x$r, 0x$g, 0x$b }, "; done; echo ""; done; done
  62. */
  63. static const rgb_triplet gif_clut[216] = {
  64. { 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x33 }, { 0x00, 0x00, 0x66 }, { 0x00, 0x00, 0x99 }, { 0x00, 0x00, 0xcc }, { 0x00, 0x00, 0xff },
  65. { 0x00, 0x33, 0x00 }, { 0x00, 0x33, 0x33 }, { 0x00, 0x33, 0x66 }, { 0x00, 0x33, 0x99 }, { 0x00, 0x33, 0xcc }, { 0x00, 0x33, 0xff },
  66. { 0x00, 0x66, 0x00 }, { 0x00, 0x66, 0x33 }, { 0x00, 0x66, 0x66 }, { 0x00, 0x66, 0x99 }, { 0x00, 0x66, 0xcc }, { 0x00, 0x66, 0xff },
  67. { 0x00, 0x99, 0x00 }, { 0x00, 0x99, 0x33 }, { 0x00, 0x99, 0x66 }, { 0x00, 0x99, 0x99 }, { 0x00, 0x99, 0xcc }, { 0x00, 0x99, 0xff },
  68. { 0x00, 0xcc, 0x00 }, { 0x00, 0xcc, 0x33 }, { 0x00, 0xcc, 0x66 }, { 0x00, 0xcc, 0x99 }, { 0x00, 0xcc, 0xcc }, { 0x00, 0xcc, 0xff },
  69. { 0x00, 0xff, 0x00 }, { 0x00, 0xff, 0x33 }, { 0x00, 0xff, 0x66 }, { 0x00, 0xff, 0x99 }, { 0x00, 0xff, 0xcc }, { 0x00, 0xff, 0xff },
  70. { 0x33, 0x00, 0x00 }, { 0x33, 0x00, 0x33 }, { 0x33, 0x00, 0x66 }, { 0x33, 0x00, 0x99 }, { 0x33, 0x00, 0xcc }, { 0x33, 0x00, 0xff },
  71. { 0x33, 0x33, 0x00 }, { 0x33, 0x33, 0x33 }, { 0x33, 0x33, 0x66 }, { 0x33, 0x33, 0x99 }, { 0x33, 0x33, 0xcc }, { 0x33, 0x33, 0xff },
  72. { 0x33, 0x66, 0x00 }, { 0x33, 0x66, 0x33 }, { 0x33, 0x66, 0x66 }, { 0x33, 0x66, 0x99 }, { 0x33, 0x66, 0xcc }, { 0x33, 0x66, 0xff },
  73. { 0x33, 0x99, 0x00 }, { 0x33, 0x99, 0x33 }, { 0x33, 0x99, 0x66 }, { 0x33, 0x99, 0x99 }, { 0x33, 0x99, 0xcc }, { 0x33, 0x99, 0xff },
  74. { 0x33, 0xcc, 0x00 }, { 0x33, 0xcc, 0x33 }, { 0x33, 0xcc, 0x66 }, { 0x33, 0xcc, 0x99 }, { 0x33, 0xcc, 0xcc }, { 0x33, 0xcc, 0xff },
  75. { 0x33, 0xff, 0x00 }, { 0x33, 0xff, 0x33 }, { 0x33, 0xff, 0x66 }, { 0x33, 0xff, 0x99 }, { 0x33, 0xff, 0xcc }, { 0x33, 0xff, 0xff },
  76. { 0x66, 0x00, 0x00 }, { 0x66, 0x00, 0x33 }, { 0x66, 0x00, 0x66 }, { 0x66, 0x00, 0x99 }, { 0x66, 0x00, 0xcc }, { 0x66, 0x00, 0xff },
  77. { 0x66, 0x33, 0x00 }, { 0x66, 0x33, 0x33 }, { 0x66, 0x33, 0x66 }, { 0x66, 0x33, 0x99 }, { 0x66, 0x33, 0xcc }, { 0x66, 0x33, 0xff },
  78. { 0x66, 0x66, 0x00 }, { 0x66, 0x66, 0x33 }, { 0x66, 0x66, 0x66 }, { 0x66, 0x66, 0x99 }, { 0x66, 0x66, 0xcc }, { 0x66, 0x66, 0xff },
  79. { 0x66, 0x99, 0x00 }, { 0x66, 0x99, 0x33 }, { 0x66, 0x99, 0x66 }, { 0x66, 0x99, 0x99 }, { 0x66, 0x99, 0xcc }, { 0x66, 0x99, 0xff },
  80. { 0x66, 0xcc, 0x00 }, { 0x66, 0xcc, 0x33 }, { 0x66, 0xcc, 0x66 }, { 0x66, 0xcc, 0x99 }, { 0x66, 0xcc, 0xcc }, { 0x66, 0xcc, 0xff },
  81. { 0x66, 0xff, 0x00 }, { 0x66, 0xff, 0x33 }, { 0x66, 0xff, 0x66 }, { 0x66, 0xff, 0x99 }, { 0x66, 0xff, 0xcc }, { 0x66, 0xff, 0xff },
  82. { 0x99, 0x00, 0x00 }, { 0x99, 0x00, 0x33 }, { 0x99, 0x00, 0x66 }, { 0x99, 0x00, 0x99 }, { 0x99, 0x00, 0xcc }, { 0x99, 0x00, 0xff },
  83. { 0x99, 0x33, 0x00 }, { 0x99, 0x33, 0x33 }, { 0x99, 0x33, 0x66 }, { 0x99, 0x33, 0x99 }, { 0x99, 0x33, 0xcc }, { 0x99, 0x33, 0xff },
  84. { 0x99, 0x66, 0x00 }, { 0x99, 0x66, 0x33 }, { 0x99, 0x66, 0x66 }, { 0x99, 0x66, 0x99 }, { 0x99, 0x66, 0xcc }, { 0x99, 0x66, 0xff },
  85. { 0x99, 0x99, 0x00 }, { 0x99, 0x99, 0x33 }, { 0x99, 0x99, 0x66 }, { 0x99, 0x99, 0x99 }, { 0x99, 0x99, 0xcc }, { 0x99, 0x99, 0xff },
  86. { 0x99, 0xcc, 0x00 }, { 0x99, 0xcc, 0x33 }, { 0x99, 0xcc, 0x66 }, { 0x99, 0xcc, 0x99 }, { 0x99, 0xcc, 0xcc }, { 0x99, 0xcc, 0xff },
  87. { 0x99, 0xff, 0x00 }, { 0x99, 0xff, 0x33 }, { 0x99, 0xff, 0x66 }, { 0x99, 0xff, 0x99 }, { 0x99, 0xff, 0xcc }, { 0x99, 0xff, 0xff },
  88. { 0xcc, 0x00, 0x00 }, { 0xcc, 0x00, 0x33 }, { 0xcc, 0x00, 0x66 }, { 0xcc, 0x00, 0x99 }, { 0xcc, 0x00, 0xcc }, { 0xcc, 0x00, 0xff },
  89. { 0xcc, 0x33, 0x00 }, { 0xcc, 0x33, 0x33 }, { 0xcc, 0x33, 0x66 }, { 0xcc, 0x33, 0x99 }, { 0xcc, 0x33, 0xcc }, { 0xcc, 0x33, 0xff },
  90. { 0xcc, 0x66, 0x00 }, { 0xcc, 0x66, 0x33 }, { 0xcc, 0x66, 0x66 }, { 0xcc, 0x66, 0x99 }, { 0xcc, 0x66, 0xcc }, { 0xcc, 0x66, 0xff },
  91. { 0xcc, 0x99, 0x00 }, { 0xcc, 0x99, 0x33 }, { 0xcc, 0x99, 0x66 }, { 0xcc, 0x99, 0x99 }, { 0xcc, 0x99, 0xcc }, { 0xcc, 0x99, 0xff },
  92. { 0xcc, 0xcc, 0x00 }, { 0xcc, 0xcc, 0x33 }, { 0xcc, 0xcc, 0x66 }, { 0xcc, 0xcc, 0x99 }, { 0xcc, 0xcc, 0xcc }, { 0xcc, 0xcc, 0xff },
  93. { 0xcc, 0xff, 0x00 }, { 0xcc, 0xff, 0x33 }, { 0xcc, 0xff, 0x66 }, { 0xcc, 0xff, 0x99 }, { 0xcc, 0xff, 0xcc }, { 0xcc, 0xff, 0xff },
  94. { 0xff, 0x00, 0x00 }, { 0xff, 0x00, 0x33 }, { 0xff, 0x00, 0x66 }, { 0xff, 0x00, 0x99 }, { 0xff, 0x00, 0xcc }, { 0xff, 0x00, 0xff },
  95. { 0xff, 0x33, 0x00 }, { 0xff, 0x33, 0x33 }, { 0xff, 0x33, 0x66 }, { 0xff, 0x33, 0x99 }, { 0xff, 0x33, 0xcc }, { 0xff, 0x33, 0xff },
  96. { 0xff, 0x66, 0x00 }, { 0xff, 0x66, 0x33 }, { 0xff, 0x66, 0x66 }, { 0xff, 0x66, 0x99 }, { 0xff, 0x66, 0xcc }, { 0xff, 0x66, 0xff },
  97. { 0xff, 0x99, 0x00 }, { 0xff, 0x99, 0x33 }, { 0xff, 0x99, 0x66 }, { 0xff, 0x99, 0x99 }, { 0xff, 0x99, 0xcc }, { 0xff, 0x99, 0xff },
  98. { 0xff, 0xcc, 0x00 }, { 0xff, 0xcc, 0x33 }, { 0xff, 0xcc, 0x66 }, { 0xff, 0xcc, 0x99 }, { 0xff, 0xcc, 0xcc }, { 0xff, 0xcc, 0xff },
  99. { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
  100. };
  101. /* GIF header */
  102. static int gif_image_write_header(uint8_t **bytestream,
  103. int width, int height, int loop_count,
  104. uint32_t *palette)
  105. {
  106. int i;
  107. unsigned int v;
  108. bytestream_put_buffer(bytestream, "GIF", 3);
  109. bytestream_put_buffer(bytestream, "89a", 3);
  110. bytestream_put_le16(bytestream, width);
  111. bytestream_put_le16(bytestream, height);
  112. bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
  113. bytestream_put_byte(bytestream, 0x1f); /* background color index */
  114. bytestream_put_byte(bytestream, 0); /* aspect ratio */
  115. /* the global palette */
  116. if (!palette) {
  117. bytestream_put_buffer(bytestream, (const unsigned char *)gif_clut, 216*3);
  118. for(i=0;i<((256-216)*3);i++)
  119. bytestream_put_byte(bytestream, 0);
  120. } else {
  121. for(i=0;i<256;i++) {
  122. v = palette[i];
  123. bytestream_put_be24(bytestream, v);
  124. }
  125. }
  126. /* update: this is the 'NETSCAPE EXTENSION' that allows for looped animated gif
  127. see http://members.aol.com/royalef/gifabout.htm#net-extension
  128. byte 1 : 33 (hex 0x21) GIF Extension code
  129. byte 2 : 255 (hex 0xFF) Application Extension Label
  130. byte 3 : 11 (hex (0x0B) Length of Application Block
  131. (eleven bytes of data to follow)
  132. bytes 4 to 11 : "NETSCAPE"
  133. bytes 12 to 14 : "2.0"
  134. byte 15 : 3 (hex 0x03) Length of Data Sub-Block
  135. (three bytes of data to follow)
  136. byte 16 : 1 (hex 0x01)
  137. bytes 17 to 18 : 0 to 65535, an unsigned integer in
  138. lo-hi byte format. This indicate the
  139. number of iterations the loop should
  140. be executed.
  141. bytes 19 : 0 (hex 0x00) a Data Sub-block Terminator
  142. */
  143. /* application extension header */
  144. #ifdef GIF_ADD_APP_HEADER
  145. if (loop_count >= 0 && loop_count <= 65535) {
  146. bytestream_put_byte(bytestream, 0x21);
  147. bytestream_put_byte(bytestream, 0xff);
  148. bytestream_put_byte(bytestream, 0x0b);
  149. bytestream_put_buffer(bytestream, "NETSCAPE2.0", 11); // bytes 4 to 14
  150. bytestream_put_byte(bytestream, 0x03); // byte 15
  151. bytestream_put_byte(bytestream, 0x01); // byte 16
  152. bytestream_put_le16(bytestream, (uint16_t)loop_count);
  153. bytestream_put_byte(bytestream, 0x00); // byte 19
  154. }
  155. #endif
  156. return 0;
  157. }
  158. /* this is maybe slow, but allows for extensions */
  159. static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
  160. {
  161. return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
  162. }
  163. static int gif_image_write_image(uint8_t **bytestream,
  164. int x1, int y1, int width, int height,
  165. const uint8_t *buf, int linesize, int pix_fmt)
  166. {
  167. PutBitContext p;
  168. uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
  169. int i, left, w, v;
  170. const uint8_t *ptr;
  171. /* image block */
  172. bytestream_put_byte(bytestream, 0x2c);
  173. bytestream_put_le16(bytestream, x1);
  174. bytestream_put_le16(bytestream, y1);
  175. bytestream_put_le16(bytestream, width);
  176. bytestream_put_le16(bytestream, height);
  177. bytestream_put_byte(bytestream, 0x00); /* flags */
  178. /* no local clut */
  179. bytestream_put_byte(bytestream, 0x08);
  180. left= width * height;
  181. init_put_bits(&p, buffer, 130);
  182. /*
  183. * the thing here is the bitstream is written as little packets, with a size byte before
  184. * but it's still the same bitstream between packets (no flush !)
  185. */
  186. ptr = buf;
  187. w = width;
  188. while(left>0) {
  189. put_bits(&p, 9, 0x0100); /* clear code */
  190. for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
  191. if (pix_fmt == PIX_FMT_RGB24) {
  192. v = gif_clut_index(ptr[0], ptr[1], ptr[2]);
  193. ptr+=3;
  194. } else {
  195. v = *ptr++;
  196. }
  197. put_bits(&p, 9, v);
  198. if (--w == 0) {
  199. w = width;
  200. buf += linesize;
  201. ptr = buf;
  202. }
  203. }
  204. if(left<=GIF_CHUNKS) {
  205. put_bits(&p, 9, 0x101); /* end of stream */
  206. flush_put_bits(&p);
  207. }
  208. if(pbBufPtr(&p) - p.buf > 0) {
  209. bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf); /* byte count of the packet */
  210. bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
  211. p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
  212. }
  213. left-=GIF_CHUNKS;
  214. }
  215. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  216. bytestream_put_byte(bytestream, 0x3b);
  217. return 0;
  218. }
  219. typedef struct {
  220. int64_t time, file_time;
  221. uint8_t buffer[100]; /* data chunks */
  222. AVFrame picture;
  223. } GIFContext;
  224. static av_cold int gif_encode_init(AVCodecContext *avctx)
  225. {
  226. GIFContext *s = avctx->priv_data;
  227. avctx->coded_frame = &s->picture;
  228. return 0;
  229. }
  230. /* better than nothing gif encoder */
  231. static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
  232. {
  233. GIFContext *s = avctx->priv_data;
  234. AVFrame *pict = data;
  235. AVFrame *const p = (AVFrame *)&s->picture;
  236. uint8_t *outbuf_ptr = outbuf;
  237. *p = *pict;
  238. p->pict_type = FF_I_TYPE;
  239. p->key_frame = 1;
  240. gif_image_write_header(&outbuf_ptr, avctx->width, avctx->height, -1, (uint32_t *)pict->data[1]);
  241. gif_image_write_image(&outbuf_ptr, 0, 0, avctx->width, avctx->height, pict->data[0], pict->linesize[0], PIX_FMT_PAL8);
  242. return outbuf_ptr - outbuf;
  243. }
  244. AVCodec gif_encoder = {
  245. "gif",
  246. CODEC_TYPE_VIDEO,
  247. CODEC_ID_GIF,
  248. sizeof(GIFContext),
  249. gif_encode_init,
  250. gif_encode_frame,
  251. NULL, //encode_end,
  252. .pix_fmts= (enum PixelFormat[]){PIX_FMT_PAL8, PIX_FMT_NONE},
  253. .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  254. };