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.

206 lines
6.4KB

  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. * - uses only a global standard palette
  28. * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
  29. *
  30. * Reference documents:
  31. * http://www.goice.co.jp/member/mo/formats/gif.html
  32. * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
  33. * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
  34. */
  35. #include "avcodec.h"
  36. #include "bytestream.h"
  37. #include "internal.h"
  38. #include "lzw.h"
  39. /* The GIF format uses reversed order for bitstreams... */
  40. /* at least they don't use PDP_ENDIAN :) */
  41. #define BITSTREAM_WRITER_LE
  42. #include "put_bits.h"
  43. typedef struct {
  44. AVFrame picture;
  45. LZWState *lzw;
  46. uint8_t *buf;
  47. } GIFContext;
  48. /* GIF header */
  49. static int gif_image_write_header(AVCodecContext *avctx,
  50. uint8_t **bytestream, uint32_t *palette)
  51. {
  52. int i;
  53. unsigned int v, smallest_alpha = 0xFF, alpha_component = 0;
  54. bytestream_put_buffer(bytestream, "GIF", 3);
  55. bytestream_put_buffer(bytestream, "89a", 3);
  56. bytestream_put_le16(bytestream, avctx->width);
  57. bytestream_put_le16(bytestream, avctx->height);
  58. bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
  59. bytestream_put_byte(bytestream, 0x1f); /* background color index */
  60. bytestream_put_byte(bytestream, 0); /* aspect ratio */
  61. /* the global palette */
  62. for(i=0;i<256;i++) {
  63. v = palette[i];
  64. bytestream_put_be24(bytestream, v);
  65. if (v >> 24 < smallest_alpha) {
  66. smallest_alpha = v >> 24;
  67. alpha_component = i;
  68. }
  69. }
  70. if (smallest_alpha < 128) {
  71. bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
  72. bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
  73. bytestream_put_byte(bytestream, 0x04); /* block length */
  74. bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
  75. bytestream_put_le16(bytestream, 0x00); /* no delay */
  76. bytestream_put_byte(bytestream, alpha_component);
  77. bytestream_put_byte(bytestream, 0x00);
  78. }
  79. return 0;
  80. }
  81. static int gif_image_write_image(AVCodecContext *avctx,
  82. uint8_t **bytestream, uint8_t *end,
  83. const uint8_t *buf, int linesize)
  84. {
  85. GIFContext *s = avctx->priv_data;
  86. int len = 0, height;
  87. const uint8_t *ptr;
  88. /* image block */
  89. bytestream_put_byte(bytestream, 0x2c);
  90. bytestream_put_le16(bytestream, 0);
  91. bytestream_put_le16(bytestream, 0);
  92. bytestream_put_le16(bytestream, avctx->width);
  93. bytestream_put_le16(bytestream, avctx->height);
  94. bytestream_put_byte(bytestream, 0x00); /* flags */
  95. /* no local clut */
  96. bytestream_put_byte(bytestream, 0x08);
  97. ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
  98. 12, FF_LZW_GIF, put_bits);
  99. ptr = buf;
  100. for (height = avctx->height; height--;) {
  101. len += ff_lzw_encode(s->lzw, ptr, avctx->width);
  102. ptr += linesize;
  103. }
  104. len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
  105. ptr = s->buf;
  106. while (len > 0) {
  107. int size = FFMIN(255, len);
  108. bytestream_put_byte(bytestream, size);
  109. if (end - *bytestream < size)
  110. return -1;
  111. bytestream_put_buffer(bytestream, ptr, size);
  112. ptr += size;
  113. len -= size;
  114. }
  115. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  116. bytestream_put_byte(bytestream, 0x3b);
  117. return 0;
  118. }
  119. static av_cold int gif_encode_init(AVCodecContext *avctx)
  120. {
  121. GIFContext *s = avctx->priv_data;
  122. if (avctx->width > 65535 || avctx->height > 65535) {
  123. av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
  124. return -1;
  125. }
  126. avctx->coded_frame = &s->picture;
  127. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  128. if (!s->lzw)
  129. return AVERROR(ENOMEM);
  130. s->buf = av_malloc(avctx->width*avctx->height*2);
  131. if (!s->buf)
  132. return AVERROR(ENOMEM);
  133. return 0;
  134. }
  135. /* better than nothing gif encoder */
  136. static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  137. const AVFrame *pict, int *got_packet)
  138. {
  139. GIFContext *s = avctx->priv_data;
  140. AVFrame *const p = &s->picture;
  141. uint8_t *outbuf_ptr, *end;
  142. int ret;
  143. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
  144. return ret;
  145. outbuf_ptr = pkt->data;
  146. end = pkt->data + pkt->size;
  147. *p = *pict;
  148. p->pict_type = AV_PICTURE_TYPE_I;
  149. p->key_frame = 1;
  150. gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
  151. gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
  152. pkt->size = outbuf_ptr - pkt->data;
  153. pkt->flags |= AV_PKT_FLAG_KEY;
  154. *got_packet = 1;
  155. return 0;
  156. }
  157. static int gif_encode_close(AVCodecContext *avctx)
  158. {
  159. GIFContext *s = avctx->priv_data;
  160. av_freep(&s->lzw);
  161. av_freep(&s->buf);
  162. return 0;
  163. }
  164. AVCodec ff_gif_encoder = {
  165. .name = "gif",
  166. .type = AVMEDIA_TYPE_VIDEO,
  167. .id = AV_CODEC_ID_GIF,
  168. .priv_data_size = sizeof(GIFContext),
  169. .init = gif_encode_init,
  170. .encode2 = gif_encode_frame,
  171. .close = gif_encode_close,
  172. .pix_fmts = (const enum AVPixelFormat[]){
  173. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  174. AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
  175. },
  176. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  177. };