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.

196 lines
6.0KB

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