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.

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