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.

231 lines
6.9KB

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