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.

194 lines
6.2KB

  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, smallest_alpha = 0xFF, alpha_component = 0;
  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. if (v >> 24 < smallest_alpha) {
  72. smallest_alpha = v >> 24;
  73. alpha_component = i;
  74. }
  75. }
  76. if (smallest_alpha < 128) {
  77. bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
  78. bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
  79. bytestream_put_byte(bytestream, 0x04); /* block length */
  80. bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
  81. bytestream_put_le16(bytestream, 0x00); /* no delay */
  82. bytestream_put_byte(bytestream, alpha_component);
  83. bytestream_put_byte(bytestream, 0x00);
  84. }
  85. return 0;
  86. }
  87. static int gif_image_write_image(AVCodecContext *avctx,
  88. uint8_t **bytestream, uint8_t *end,
  89. const uint8_t *buf, int linesize)
  90. {
  91. GIFContext *s = avctx->priv_data;
  92. int len = 0, height;
  93. const uint8_t *ptr;
  94. /* image block */
  95. bytestream_put_byte(bytestream, 0x2c);
  96. bytestream_put_le16(bytestream, 0);
  97. bytestream_put_le16(bytestream, 0);
  98. bytestream_put_le16(bytestream, avctx->width);
  99. bytestream_put_le16(bytestream, avctx->height);
  100. bytestream_put_byte(bytestream, 0x00); /* flags */
  101. /* no local clut */
  102. bytestream_put_byte(bytestream, 0x08);
  103. ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
  104. 12, FF_LZW_GIF, put_bits);
  105. ptr = buf;
  106. for (height = avctx->height; height--;) {
  107. len += ff_lzw_encode(s->lzw, ptr, avctx->width);
  108. ptr += linesize;
  109. }
  110. len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
  111. ptr = s->buf;
  112. while (len > 0) {
  113. int size = FFMIN(255, len);
  114. bytestream_put_byte(bytestream, size);
  115. if (end - *bytestream < size)
  116. return -1;
  117. bytestream_put_buffer(bytestream, ptr, size);
  118. ptr += size;
  119. len -= size;
  120. }
  121. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  122. bytestream_put_byte(bytestream, 0x3b);
  123. return 0;
  124. }
  125. static av_cold int gif_encode_init(AVCodecContext *avctx)
  126. {
  127. GIFContext *s = avctx->priv_data;
  128. avctx->coded_frame = &s->picture;
  129. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  130. if (!s->lzw)
  131. return AVERROR(ENOMEM);
  132. s->buf = av_malloc(avctx->width*avctx->height*2);
  133. if (!s->buf)
  134. return AVERROR(ENOMEM);
  135. return 0;
  136. }
  137. /* better than nothing gif encoder */
  138. static int gif_encode_frame(AVCodecContext *avctx, unsigned char *outbuf, int buf_size, void *data)
  139. {
  140. GIFContext *s = avctx->priv_data;
  141. AVFrame *pict = data;
  142. AVFrame *const p = (AVFrame *)&s->picture;
  143. uint8_t *outbuf_ptr = outbuf;
  144. uint8_t *end = outbuf + buf_size;
  145. *p = *pict;
  146. p->pict_type = AV_PICTURE_TYPE_I;
  147. p->key_frame = 1;
  148. gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
  149. gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
  150. return outbuf_ptr - outbuf;
  151. }
  152. static int gif_encode_close(AVCodecContext *avctx)
  153. {
  154. GIFContext *s = avctx->priv_data;
  155. av_freep(&s->lzw);
  156. av_freep(&s->buf);
  157. return 0;
  158. }
  159. AVCodec ff_gif_encoder = {
  160. .name = "gif",
  161. .type = AVMEDIA_TYPE_VIDEO,
  162. .id = CODEC_ID_GIF,
  163. .priv_data_size = sizeof(GIFContext),
  164. .init = gif_encode_init,
  165. .encode = gif_encode_frame,
  166. .close = gif_encode_close,
  167. .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},
  168. .long_name= NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  169. };