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.

202 lines
6.3KB

  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 FFmpeg.
  10. *
  11. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. * - 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. static int gif_image_write_image(AVCodecContext *avctx,
  49. uint8_t **bytestream, uint8_t *end,
  50. const uint32_t *palette,
  51. const uint8_t *buf, int linesize)
  52. {
  53. GIFContext *s = avctx->priv_data;
  54. int len = 0, height;
  55. const uint8_t *ptr;
  56. /* Mark one colour as transparent if the input palette contains at least
  57. * one colour that is more than 50% transparent. */
  58. if (palette) {
  59. unsigned i, smallest_alpha = 0xFF, alpha_component = 0;
  60. for (i = 0; i < AVPALETTE_COUNT; i++) {
  61. const uint32_t v = palette[i];
  62. if (v >> 24 < smallest_alpha) {
  63. smallest_alpha = v >> 24;
  64. alpha_component = i;
  65. }
  66. }
  67. if (smallest_alpha < 128) {
  68. bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
  69. bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
  70. bytestream_put_byte(bytestream, 0x04); /* block length */
  71. bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
  72. bytestream_put_le16(bytestream, 0x00); /* no delay */
  73. bytestream_put_byte(bytestream, alpha_component);
  74. bytestream_put_byte(bytestream, 0x00);
  75. }
  76. }
  77. /* image block */
  78. bytestream_put_byte(bytestream, 0x2c);
  79. bytestream_put_le16(bytestream, 0);
  80. bytestream_put_le16(bytestream, 0);
  81. bytestream_put_le16(bytestream, avctx->width);
  82. bytestream_put_le16(bytestream, avctx->height);
  83. if (!palette) {
  84. bytestream_put_byte(bytestream, 0x00); /* flags */
  85. } else {
  86. unsigned i;
  87. bytestream_put_byte(bytestream, 1<<7 | 0x7); /* flags */
  88. for (i = 0; i < AVPALETTE_COUNT; i++) {
  89. const uint32_t v = palette[i];
  90. bytestream_put_be24(bytestream, v);
  91. }
  92. }
  93. bytestream_put_byte(bytestream, 0x08);
  94. ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
  95. 12, FF_LZW_GIF, put_bits);
  96. ptr = buf;
  97. for (height = avctx->height; height--;) {
  98. len += ff_lzw_encode(s->lzw, ptr, avctx->width);
  99. ptr += linesize;
  100. }
  101. len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
  102. ptr = s->buf;
  103. while (len > 0) {
  104. int size = FFMIN(255, len);
  105. bytestream_put_byte(bytestream, size);
  106. if (end - *bytestream < size)
  107. return -1;
  108. bytestream_put_buffer(bytestream, ptr, size);
  109. ptr += size;
  110. len -= size;
  111. }
  112. bytestream_put_byte(bytestream, 0x00); /* end of image block */
  113. return 0;
  114. }
  115. static av_cold int gif_encode_init(AVCodecContext *avctx)
  116. {
  117. GIFContext *s = avctx->priv_data;
  118. if (avctx->width > 65535 || avctx->height > 65535) {
  119. av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
  120. return -1;
  121. }
  122. avctx->coded_frame = &s->picture;
  123. s->lzw = av_mallocz(ff_lzw_encode_state_size);
  124. if (!s->lzw)
  125. return AVERROR(ENOMEM);
  126. s->buf = av_malloc(avctx->width*avctx->height*2);
  127. if (!s->buf)
  128. return AVERROR(ENOMEM);
  129. return 0;
  130. }
  131. /* better than nothing gif encoder */
  132. static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  133. const AVFrame *pict, int *got_packet)
  134. {
  135. GIFContext *s = avctx->priv_data;
  136. AVFrame *const p = &s->picture;
  137. uint8_t *outbuf_ptr, *end;
  138. const uint32_t *palette = NULL;
  139. int ret;
  140. if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
  141. return ret;
  142. outbuf_ptr = pkt->data;
  143. end = pkt->data + pkt->size;
  144. *p = *pict;
  145. p->pict_type = AV_PICTURE_TYPE_I;
  146. p->key_frame = 1;
  147. if (avctx->pix_fmt == AV_PIX_FMT_PAL8)
  148. palette = (uint32_t*)p->data[1];
  149. gif_image_write_image(avctx, &outbuf_ptr, end, palette, pict->data[0], pict->linesize[0]);
  150. pkt->size = outbuf_ptr - pkt->data;
  151. pkt->flags |= AV_PKT_FLAG_KEY;
  152. *got_packet = 1;
  153. return 0;
  154. }
  155. static int gif_encode_close(AVCodecContext *avctx)
  156. {
  157. GIFContext *s = avctx->priv_data;
  158. av_freep(&s->lzw);
  159. av_freep(&s->buf);
  160. return 0;
  161. }
  162. AVCodec ff_gif_encoder = {
  163. .name = "gif",
  164. .type = AVMEDIA_TYPE_VIDEO,
  165. .id = AV_CODEC_ID_GIF,
  166. .priv_data_size = sizeof(GIFContext),
  167. .init = gif_encode_init,
  168. .encode2 = gif_encode_frame,
  169. .close = gif_encode_close,
  170. .pix_fmts = (const enum AVPixelFormat[]){
  171. AV_PIX_FMT_RGB8, AV_PIX_FMT_BGR8, AV_PIX_FMT_RGB4_BYTE, AV_PIX_FMT_BGR4_BYTE,
  172. AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, AV_PIX_FMT_NONE
  173. },
  174. .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
  175. };