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.

220 lines
6.5KB

  1. /*
  2. * Animated GIF muxer
  3. * Copyright (c) 2000 Fabrice Bellard
  4. *
  5. * first version by Francois Revol <revol@free.fr>
  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. #include "avformat.h"
  24. #include "internal.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/imgutils.h"
  27. #include "libavutil/log.h"
  28. #include "libavutil/opt.h"
  29. static int gif_image_write_header(AVIOContext *pb, int width, int height,
  30. int loop_count, uint32_t *palette)
  31. {
  32. int i;
  33. avio_write(pb, "GIF", 3);
  34. avio_write(pb, "89a", 3);
  35. avio_wl16(pb, width);
  36. avio_wl16(pb, height);
  37. if (palette) {
  38. avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
  39. avio_w8(pb, 0x1f); /* background color index */
  40. avio_w8(pb, 0); /* aspect ratio */
  41. for (i = 0; i < 256; i++) {
  42. const uint32_t v = palette[i] & 0xffffff;
  43. avio_wb24(pb, v);
  44. }
  45. } else {
  46. avio_w8(pb, 0); /* flags */
  47. avio_w8(pb, 0); /* background color index */
  48. avio_w8(pb, 0); /* aspect ratio */
  49. }
  50. /* "NETSCAPE EXTENSION" for looped animation GIF */
  51. avio_w8(pb, 0x21); /* GIF Extension code */
  52. avio_w8(pb, 0xff); /* Application Extension Label */
  53. avio_w8(pb, 0x0b); /* Length of Application Block */
  54. avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
  55. avio_w8(pb, 0x03); /* Length of Data Sub-Block */
  56. avio_w8(pb, 0x01);
  57. avio_wl16(pb, (uint16_t)loop_count);
  58. avio_w8(pb, 0x00); /* Data Sub-block Terminator */
  59. return 0;
  60. }
  61. typedef struct {
  62. AVClass *class;
  63. int loop;
  64. int last_delay;
  65. AVPacket *prev_pkt;
  66. int duration;
  67. } GIFContext;
  68. static int gif_write_header(AVFormatContext *s)
  69. {
  70. GIFContext *gif = s->priv_data;
  71. AVIOContext *pb = s->pb;
  72. AVCodecContext *video_enc;
  73. int width, height;
  74. uint32_t palette[AVPALETTE_COUNT];
  75. if (s->nb_streams != 1 ||
  76. s->streams[0]->codec->codec_type != AVMEDIA_TYPE_VIDEO ||
  77. s->streams[0]->codec->codec_id != AV_CODEC_ID_GIF) {
  78. av_log(s, AV_LOG_ERROR,
  79. "GIF muxer supports only a single video GIF stream.\n");
  80. return AVERROR(EINVAL);
  81. }
  82. video_enc = s->streams[0]->codec;
  83. width = video_enc->width;
  84. height = video_enc->height;
  85. avpriv_set_pts_info(s->streams[0], 64, 1, 100);
  86. if (avpriv_set_systematic_pal2(palette, video_enc->pix_fmt) < 0) {
  87. av_assert0(video_enc->pix_fmt == AV_PIX_FMT_PAL8);
  88. gif_image_write_header(pb, width, height, gif->loop, NULL);
  89. } else {
  90. gif_image_write_header(pb, width, height, gif->loop, palette);
  91. }
  92. avio_flush(s->pb);
  93. return 0;
  94. }
  95. static int flush_packet(AVFormatContext *s, AVPacket *new)
  96. {
  97. GIFContext *gif = s->priv_data;
  98. int size;
  99. AVIOContext *pb = s->pb;
  100. uint8_t flags = 0x4, transparent_color_index = 0x1f;
  101. const uint32_t *palette;
  102. AVPacket *pkt = gif->prev_pkt;
  103. if (!pkt)
  104. return 0;
  105. /* Mark one colour as transparent if the input palette contains at least
  106. * one colour that is more than 50% transparent. */
  107. palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
  108. if (palette && size != AVPALETTE_SIZE) {
  109. av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. if (palette) {
  113. unsigned i, smallest_alpha = 0xff;
  114. for (i = 0; i < AVPALETTE_COUNT; i++) {
  115. const uint32_t v = palette[i];
  116. if (v >> 24 < smallest_alpha) {
  117. smallest_alpha = v >> 24;
  118. transparent_color_index = i;
  119. }
  120. }
  121. if (smallest_alpha < 128)
  122. flags |= 0x1; /* Transparent Color Flag */
  123. }
  124. if (new && new->pts != AV_NOPTS_VALUE)
  125. gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
  126. else if (!new && gif->last_delay >= 0)
  127. gif->duration = gif->last_delay;
  128. /* graphic control extension block */
  129. avio_w8(pb, 0x21);
  130. avio_w8(pb, 0xf9);
  131. avio_w8(pb, 0x04); /* block size */
  132. avio_w8(pb, flags);
  133. avio_wl16(pb, gif->duration);
  134. avio_w8(pb, transparent_color_index);
  135. avio_w8(pb, 0x00);
  136. avio_write(pb, pkt->data, pkt->size);
  137. av_free_packet(gif->prev_pkt);
  138. if (new)
  139. av_copy_packet(gif->prev_pkt, new);
  140. return 0;
  141. }
  142. static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
  143. {
  144. GIFContext *gif = s->priv_data;
  145. if (!gif->prev_pkt) {
  146. gif->prev_pkt = av_malloc(sizeof(*gif->prev_pkt));
  147. if (!gif->prev_pkt)
  148. return AVERROR(ENOMEM);
  149. return av_copy_packet(gif->prev_pkt, pkt);
  150. }
  151. return flush_packet(s, pkt);
  152. }
  153. static int gif_write_trailer(AVFormatContext *s)
  154. {
  155. GIFContext *gif = s->priv_data;
  156. AVIOContext *pb = s->pb;
  157. flush_packet(s, NULL);
  158. av_freep(&gif->prev_pkt);
  159. avio_w8(pb, 0x3b);
  160. return 0;
  161. }
  162. #define OFFSET(x) offsetof(GIFContext, x)
  163. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  164. static const AVOption options[] = {
  165. { "loop", "Number of times to loop the output.", OFFSET(loop),
  166. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 65535, ENC },
  167. { "final_delay", "Force delay (in ms) after the last frame", OFFSET(last_delay),
  168. AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
  169. { NULL },
  170. };
  171. static const AVClass gif_muxer_class = {
  172. .class_name = "GIF muxer",
  173. .item_name = av_default_item_name,
  174. .version = LIBAVUTIL_VERSION_INT,
  175. .option = options,
  176. };
  177. AVOutputFormat ff_gif_muxer = {
  178. .name = "gif",
  179. .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
  180. .mime_type = "image/gif",
  181. .extensions = "gif",
  182. .priv_data_size = sizeof(GIFContext),
  183. .audio_codec = AV_CODEC_ID_NONE,
  184. .video_codec = AV_CODEC_ID_GIF,
  185. .write_header = gif_write_header,
  186. .write_packet = gif_write_packet,
  187. .write_trailer = gif_write_trailer,
  188. .priv_class = &gif_muxer_class,
  189. .flags = AVFMT_VARIABLE_FPS,
  190. };