gif.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /* GIF header */
  51. static int gif_image_write_header(uint8_t **bytestream,
  52. int width, int height,
  53. uint32_t *palette)
  54. {
  55. int i;
  56. unsigned int v;
  57. bytestream_put_buffer(bytestream, "GIF", 3);
  58. bytestream_put_buffer(bytestream, "89a", 3);
  59. bytestream_put_le16(bytestream, width);
  60. bytestream_put_le16(bytestream, height);
  61. bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
  62. bytestream_put_byte(bytestream, 0x1f); /* background color index */
  63. bytestream_put_byte(bytestream, 0); /* aspect ratio */
  64. /* the global palette */
  65. for(i=0;i<256;i++) {
  66. v = palette[i];
  67. bytestream_put_be24(bytestream, v);
  68. }
  69. return 0;
  70. }
  71. static int gif_image_write_image(uint8_t **bytestream,
  72. int x1, int y1, int width, int height,
  73. const uint8_t *buf, int linesize, int pix_fmt)
  74. {
  75. PutBitContext p;
  76. uint8_t buffer[200]; /* 100 * 9 / 8 = 113 */
  77. int i, left, w;
  78. const uint8_t *ptr;
  79. /* image block */
  80. bytestream_put_byte(bytestream, 0x2c);
  81. bytestream_put_le16(bytestream, x1);
  82. bytestream_put_le16(bytestream, y1);
  83. bytestream_put_le16(bytestream, width);
  84. bytestream_put_le16(bytestream, height);
  85. bytestream_put_byte(bytestream, 0x00); /* flags */
  86. /* no local clut */
  87. bytestream_put_byte(bytestream, 0x08);
  88. left= width * height;
  89. init_put_bits(&p, buffer, 130);
  90. /*
  91. * the thing here is the bitstream is written as little packets, with a size byte before
  92. * but it's still the same bitstream between packets (no flush !)
  93. */
  94. ptr = buf;
  95. w = width;
  96. while(left>0) {
  97. put_bits(&p, 9, 0x0100); /* clear code */
  98. for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
  99. put_bits(&p, 9, *ptr++);
  100. if (--w == 0) {
  101. w = width;
  102. buf += linesize;
  103. ptr = buf;
  104. }
  105. }
  106. if(left<=GIF_CHUNKS) {
  107. put_bits(&p, 9, 0x101); /* end of stream */
  108. flush_put_bits(&p);
  109. }
  110. if(pbBufPtr(&p) - p.buf > 0) {
  111. bytestream_put_byte(bytestream, pbBufPtr(&p) - p.buf); /* byte count of the packet */
  112. bytestream_put_buffer(bytestream, p.buf, pbBufPtr(&p) - p.buf); /* the actual buffer */
  113. p.buf_ptr = p.buf; /* dequeue the bytes off the bitstream */
  114. }
  115. left-=GIF_CHUNKS;
  116. }
  117. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  118. bytestream_put_byte(bytestream, 0x3b);
  119. return 0;
  120. }
  121. typedef struct {
  122. AVFrame picture;
  123. } GIFContext;
  124. static av_cold int gif_encode_init(AVCodecContext *avctx)
  125. {
  126. GIFContext *s = avctx->priv_data;
  127. avctx->coded_frame = &s->picture;
  128. return 0;
  129. }
  130. /* better than nothing gif encoder */
  131. static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
  132. {
  133. GIFContext *s = avctx->priv_data;
  134. AVFrame *pict = data;
  135. AVFrame *const p = (AVFrame *)&s->picture;
  136. uint8_t *outbuf_ptr = outbuf;
  137. *p = *pict;
  138. p->pict_type = FF_I_TYPE;
  139. p->key_frame = 1;
  140. gif_image_write_header(&outbuf_ptr, avctx->width, avctx->height, (uint32_t *)pict->data[1]);
  141. gif_image_write_image(&outbuf_ptr, 0, 0, avctx->width, avctx->height, pict->data[0], pict->linesize[0], PIX_FMT_PAL8);
  142. return outbuf_ptr - outbuf;
  143. }
  144. AVCodec gif_encoder = {
  145. "gif",
  146. CODEC_TYPE_VIDEO,
  147. CODEC_ID_GIF,
  148. sizeof(GIFContext),
  149. gif_encode_init,
  150. gif_encode_frame,
  151. NULL, //encode_end,
  152. .pix_fmts= (enum PixelFormat[]){PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, PIX_FMT_GRAY8, PIX_FMT_PAL8, PIX_FMT_NONE},
  153. .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  154. };