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.

223 lines
6.7KB

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