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.

213 lines
6.7KB

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