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. * first version by Francois Revol <revol@free.fr>
  8. *
  9. * This file is part of Libav.
  10. *
  11. * Libav is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Libav is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Libav; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  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 GIFContext {
  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 = 0, 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. #if FF_API_CODED_FRAME
  116. FF_DISABLE_DEPRECATION_WARNINGS
  117. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  118. avctx->coded_frame->key_frame = 1;
  119. FF_ENABLE_DEPRECATION_WARNINGS
  120. #endif
  121. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  122. if (!s->lzw)
  123. return AVERROR(ENOMEM);
  124. s->buf = av_malloc(avctx->width*avctx->height*2);
  125. if (!s->buf)
  126. return AVERROR(ENOMEM);
  127. return 0;
  128. }
  129. /* better than nothing gif encoder */
  130. static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  131. const AVFrame *pict, int *got_packet)
  132. {
  133. uint8_t *outbuf_ptr, *end;
  134. int ret;
  135. if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*7/5 + AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
  136. av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
  137. return ret;
  138. }
  139. outbuf_ptr = pkt->data;
  140. end = pkt->data + pkt->size;
  141. gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
  142. gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
  143. pkt->size = outbuf_ptr - pkt->data;
  144. pkt->flags |= AV_PKT_FLAG_KEY;
  145. *got_packet = 1;
  146. return 0;
  147. }
  148. static int gif_encode_close(AVCodecContext *avctx)
  149. {
  150. GIFContext *s = avctx->priv_data;
  151. av_freep(&s->lzw);
  152. av_freep(&s->buf);
  153. return 0;
  154. }
  155. AVCodec ff_gif_encoder = {
  156. .name = "gif",
  157. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  158. .type = AVMEDIA_TYPE_VIDEO,
  159. .id = AV_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 AVPixelFormat[]){
  165. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  166. AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
  167. },
  168. };