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.

180 lines
5.5KB

  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. #include "lzw.h"
  45. /* The GIF format uses reversed order for bitstreams... */
  46. /* at least they don't use PDP_ENDIAN :) */
  47. #define BITSTREAM_WRITER_LE
  48. #include "put_bits.h"
  49. typedef struct {
  50. AVFrame picture;
  51. LZWState *lzw;
  52. uint8_t *buf;
  53. } GIFContext;
  54. /* GIF header */
  55. static int gif_image_write_header(AVCodecContext *avctx,
  56. uint8_t **bytestream, uint32_t *palette)
  57. {
  58. int i;
  59. unsigned int v;
  60. bytestream_put_buffer(bytestream, "GIF", 3);
  61. bytestream_put_buffer(bytestream, "89a", 3);
  62. bytestream_put_le16(bytestream, avctx->width);
  63. bytestream_put_le16(bytestream, avctx->height);
  64. bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
  65. bytestream_put_byte(bytestream, 0x1f); /* background color index */
  66. bytestream_put_byte(bytestream, 0); /* aspect ratio */
  67. /* the global palette */
  68. for(i=0;i<256;i++) {
  69. v = palette[i];
  70. bytestream_put_be24(bytestream, v);
  71. }
  72. return 0;
  73. }
  74. static int gif_image_write_image(AVCodecContext *avctx,
  75. uint8_t **bytestream, uint8_t *end,
  76. const uint8_t *buf, int linesize)
  77. {
  78. GIFContext *s = avctx->priv_data;
  79. int len, height;
  80. const uint8_t *ptr;
  81. /* image block */
  82. bytestream_put_byte(bytestream, 0x2c);
  83. bytestream_put_le16(bytestream, 0);
  84. bytestream_put_le16(bytestream, 0);
  85. bytestream_put_le16(bytestream, avctx->width);
  86. bytestream_put_le16(bytestream, avctx->height);
  87. bytestream_put_byte(bytestream, 0x00); /* flags */
  88. /* no local clut */
  89. bytestream_put_byte(bytestream, 0x08);
  90. ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
  91. 12, FF_LZW_GIF, put_bits);
  92. ptr = buf;
  93. for (height = avctx->height; height--;) {
  94. len += ff_lzw_encode(s->lzw, ptr, avctx->width);
  95. ptr += linesize;
  96. }
  97. len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
  98. ptr = s->buf;
  99. while (len > 0) {
  100. int size = FFMIN(255, len);
  101. bytestream_put_byte(bytestream, size);
  102. if (end - *bytestream < size)
  103. return -1;
  104. bytestream_put_buffer(bytestream, ptr, size);
  105. ptr += size;
  106. len -= size;
  107. }
  108. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  109. bytestream_put_byte(bytestream, 0x3b);
  110. return 0;
  111. }
  112. static av_cold int gif_encode_init(AVCodecContext *avctx)
  113. {
  114. GIFContext *s = avctx->priv_data;
  115. avctx->coded_frame = &s->picture;
  116. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  117. if (!s->lzw)
  118. return AVERROR(ENOMEM);
  119. s->buf = av_malloc(avctx->width*avctx->height*2);
  120. if (!s->buf)
  121. return AVERROR(ENOMEM);
  122. return 0;
  123. }
  124. /* better than nothing gif encoder */
  125. static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
  126. {
  127. GIFContext *s = avctx->priv_data;
  128. AVFrame *pict = data;
  129. AVFrame *const p = (AVFrame *)&s->picture;
  130. uint8_t *outbuf_ptr = outbuf;
  131. uint8_t *end = outbuf + buf_size;
  132. *p = *pict;
  133. p->pict_type = FF_I_TYPE;
  134. p->key_frame = 1;
  135. gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
  136. gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
  137. return outbuf_ptr - outbuf;
  138. }
  139. static int gif_encode_close(AVCodecContext *avctx)
  140. {
  141. GIFContext *s = avctx->priv_data;
  142. av_freep(&s->lzw);
  143. av_freep(&s->buf);
  144. return 0;
  145. }
  146. AVCodec gif_encoder = {
  147. "gif",
  148. AVMEDIA_TYPE_VIDEO,
  149. CODEC_ID_GIF,
  150. sizeof(GIFContext),
  151. gif_encode_init,
  152. gif_encode_frame,
  153. gif_encode_close,
  154. .pix_fmts= (const 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},
  155. .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  156. };