2002-04-08 15:32:01 +03:00
|
|
|
/*
|
2006-10-23 11:57:54 +03:00
|
|
|
* Animated GIF muxer
|
2009-01-19 17:46:40 +02:00
|
|
|
* Copyright (c) 2000 Fabrice Bellard
|
2002-04-08 15:32:01 +03:00
|
|
|
*
|
2013-04-10 20:26:49 +03:00
|
|
|
* first version by Francois Revol <revol@free.fr>
|
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2002-05-26 01:34:32 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2006-10-07 18:30:46 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2002-04-08 15:32:01 +03:00
|
|
|
*
|
2006-10-07 18:30:46 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2002-04-08 15:32:01 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2002-05-26 01:34:32 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2002-04-08 15:32:01 +03:00
|
|
|
*
|
2002-05-26 01:34:32 +03:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2006-10-07 18:30:46 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2006-01-13 00:43:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-04-08 15:32:01 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avformat.h"
|
2013-04-18 16:09:37 +03:00
|
|
|
#include "internal.h"
|
2023-01-27 16:06:00 +02:00
|
|
|
#include "mux.h"
|
2013-04-17 05:20:48 +03:00
|
|
|
#include "libavutil/imgutils.h"
|
2011-06-18 10:47:46 +03:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/opt.h"
|
2018-12-11 12:52:17 +02:00
|
|
|
#include "libavcodec/bytestream.h"
|
2018-12-10 19:36:11 +02:00
|
|
|
#include "libavcodec/gif.h"
|
2009-01-14 02:27:43 +02:00
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct GIFContext {
|
2013-05-23 16:51:11 +03:00
|
|
|
AVClass *class;
|
2011-06-18 10:47:46 +03:00
|
|
|
int loop;
|
2013-04-20 01:11:04 +03:00
|
|
|
int last_delay;
|
2013-04-19 05:02:12 +03:00
|
|
|
int duration;
|
2018-12-11 12:52:17 +02:00
|
|
|
int64_t last_pos;
|
|
|
|
int have_end;
|
|
|
|
AVPacket *prev_pkt;
|
2003-02-04 00:52:38 +02:00
|
|
|
} GIFContext;
|
|
|
|
|
2024-03-20 01:51:30 +02:00
|
|
|
static av_cold int gif_init(AVFormatContext *s)
|
2003-02-04 00:52:38 +02:00
|
|
|
{
|
2013-04-18 16:09:37 +03:00
|
|
|
avpriv_set_pts_info(s->streams[0], 64, 1, 100);
|
2003-02-04 00:52:38 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-04 18:08:14 +02:00
|
|
|
static int gif_parse_packet(AVFormatContext *s, const uint8_t *data, int size)
|
2013-04-19 05:02:12 +03:00
|
|
|
{
|
2018-12-11 12:52:17 +02:00
|
|
|
GetByteContext gb;
|
|
|
|
int x;
|
|
|
|
|
|
|
|
bytestream2_init(&gb, data, size);
|
|
|
|
|
|
|
|
while (bytestream2_get_bytes_left(&gb) > 0) {
|
|
|
|
x = bytestream2_get_byte(&gb);
|
|
|
|
if (x != GIF_EXTENSION_INTRODUCER)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
x = bytestream2_get_byte(&gb);
|
|
|
|
while (x != GIF_GCE_EXT_LABEL && bytestream2_get_bytes_left(&gb) > 0) {
|
|
|
|
int block_size = bytestream2_get_byte(&gb);
|
|
|
|
if (!block_size)
|
|
|
|
break;
|
|
|
|
bytestream2_skip(&gb, block_size);
|
|
|
|
}
|
2013-04-18 04:35:20 +03:00
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
if (x == GIF_GCE_EXT_LABEL)
|
|
|
|
return bytestream2_tell(&gb) + 2;
|
2013-04-18 04:35:20 +03:00
|
|
|
}
|
2003-02-04 00:52:38 +02:00
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gif_get_delay(GIFContext *gif, AVPacket *prev, AVPacket *new)
|
|
|
|
{
|
2013-04-19 05:02:12 +03:00
|
|
|
if (new && new->pts != AV_NOPTS_VALUE)
|
2018-12-11 12:52:17 +02:00
|
|
|
gif->duration = av_clip_uint16(new->pts - prev->pts);
|
2013-04-20 01:11:04 +03:00
|
|
|
else if (!new && gif->last_delay >= 0)
|
|
|
|
gif->duration = gif->last_delay;
|
2023-05-20 12:33:29 +02:00
|
|
|
else if (prev->duration)
|
|
|
|
gif->duration = prev->duration;
|
2013-04-18 16:09:37 +03:00
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
return gif->duration;
|
2002-04-08 15:32:01 +03:00
|
|
|
}
|
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
static int gif_write_packet(AVFormatContext *s, AVPacket *new_pkt)
|
2013-04-19 13:11:10 +03:00
|
|
|
{
|
|
|
|
GIFContext *gif = s->priv_data;
|
2018-12-11 12:52:17 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
AVPacket *pkt = gif->prev_pkt;
|
2013-04-19 13:11:10 +03:00
|
|
|
|
|
|
|
if (!gif->prev_pkt) {
|
2017-09-23 07:18:47 +02:00
|
|
|
gif->prev_pkt = av_packet_alloc();
|
2013-04-19 13:11:10 +03:00
|
|
|
if (!gif->prev_pkt)
|
|
|
|
return AVERROR(ENOMEM);
|
2018-12-11 12:52:17 +02:00
|
|
|
return av_packet_ref(gif->prev_pkt, new_pkt);
|
|
|
|
}
|
|
|
|
|
|
|
|
gif->last_pos = avio_tell(pb);
|
|
|
|
if (pkt->size > 0)
|
|
|
|
gif->have_end = pkt->data[pkt->size - 1] == GIF_TRAILER;
|
2015-01-23 17:34:14 +02:00
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
if (!gif->last_pos) {
|
|
|
|
int delay_pos;
|
|
|
|
int off = 13;
|
|
|
|
|
|
|
|
if (pkt->size < 13)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
if (pkt->data[10] & 0x80)
|
|
|
|
off += 3 * (1 << ((pkt->data[10] & 0x07) + 1));
|
|
|
|
|
|
|
|
if (pkt->size < off + 2)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
avio_write(pb, pkt->data, off);
|
|
|
|
|
|
|
|
if (pkt->data[off] == GIF_EXTENSION_INTRODUCER && pkt->data[off + 1] == 0xff)
|
|
|
|
off += 19;
|
|
|
|
|
|
|
|
if (pkt->size <= off)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
/* "NETSCAPE EXTENSION" for looped animation GIF */
|
|
|
|
if (gif->loop >= 0) {
|
|
|
|
avio_w8(pb, GIF_EXTENSION_INTRODUCER); /* GIF Extension code */
|
|
|
|
avio_w8(pb, GIF_APP_EXT_LABEL); /* Application Extension Label */
|
|
|
|
avio_w8(pb, 0x0b); /* Length of Application Block */
|
|
|
|
avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
|
|
|
|
avio_w8(pb, 0x03); /* Length of Data Sub-Block */
|
|
|
|
avio_w8(pb, 0x01);
|
|
|
|
avio_wl16(pb, (uint16_t)gif->loop);
|
|
|
|
avio_w8(pb, 0x00); /* Data Sub-block Terminator */
|
2015-01-23 17:34:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
delay_pos = gif_parse_packet(s, pkt->data + off, pkt->size - off);
|
|
|
|
if (delay_pos > 0 && delay_pos < pkt->size - off - 2) {
|
|
|
|
avio_write(pb, pkt->data + off, delay_pos);
|
|
|
|
avio_wl16(pb, gif_get_delay(gif, pkt, new_pkt));
|
|
|
|
avio_write(pb, pkt->data + off + delay_pos + 2, pkt->size - off - delay_pos - 2);
|
|
|
|
} else {
|
|
|
|
avio_write(pb, pkt->data + off, pkt->size - off);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int delay_pos = gif_parse_packet(s, pkt->data, pkt->size);
|
|
|
|
|
|
|
|
if (delay_pos > 0 && delay_pos < pkt->size - 2) {
|
|
|
|
avio_write(pb, pkt->data, delay_pos);
|
|
|
|
avio_wl16(pb, gif_get_delay(gif, pkt, new_pkt));
|
|
|
|
avio_write(pb, pkt->data + delay_pos + 2, pkt->size - delay_pos - 2);
|
|
|
|
} else {
|
|
|
|
avio_write(pb, pkt->data, pkt->size);
|
|
|
|
}
|
2013-04-19 13:11:10 +03:00
|
|
|
}
|
2018-12-11 12:52:17 +02:00
|
|
|
|
|
|
|
av_packet_unref(gif->prev_pkt);
|
|
|
|
if (new_pkt)
|
|
|
|
return av_packet_ref(gif->prev_pkt, new_pkt);
|
|
|
|
|
|
|
|
return 0;
|
2013-04-19 13:11:10 +03:00
|
|
|
}
|
|
|
|
|
2002-04-08 15:32:01 +03:00
|
|
|
static int gif_write_trailer(AVFormatContext *s)
|
|
|
|
{
|
2013-04-19 05:02:12 +03:00
|
|
|
GIFContext *gif = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2002-04-08 15:32:01 +03:00
|
|
|
|
2019-05-29 10:57:18 +02:00
|
|
|
if (!gif->prev_pkt)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2018-12-11 12:52:17 +02:00
|
|
|
gif_write_packet(s, NULL);
|
|
|
|
|
|
|
|
if (!gif->have_end)
|
|
|
|
avio_w8(pb, GIF_TRAILER);
|
|
|
|
av_packet_free(&gif->prev_pkt);
|
2012-09-09 22:35:23 +03:00
|
|
|
|
2002-04-08 15:32:01 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-18 10:47:46 +03:00
|
|
|
#define OFFSET(x) offsetof(GIFContext, x)
|
|
|
|
#define ENC AV_OPT_FLAG_ENCODING_PARAM
|
|
|
|
static const AVOption options[] = {
|
2013-07-06 03:27:56 +03:00
|
|
|
{ "loop", "Number of times to loop the output: -1 - no loop, 0 - infinite loop", OFFSET(loop),
|
|
|
|
AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 65535, ENC },
|
2013-10-19 21:24:58 +03:00
|
|
|
{ "final_delay", "Force delay (in centiseconds) after the last frame", OFFSET(last_delay),
|
2013-04-20 01:11:04 +03:00
|
|
|
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
|
2011-06-18 10:47:46 +03:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass gif_muxer_class = {
|
|
|
|
.class_name = "GIF muxer",
|
2024-01-19 14:33:28 +02:00
|
|
|
.item_name = av_default_item_name,
|
2011-06-18 10:47:46 +03:00
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.option = options,
|
|
|
|
};
|
|
|
|
|
2023-01-27 16:06:00 +02:00
|
|
|
const FFOutputFormat ff_gif_muxer = {
|
|
|
|
.p.name = "gif",
|
|
|
|
.p.long_name = NULL_IF_CONFIG_SMALL("CompuServe Graphics Interchange Format (GIF)"),
|
|
|
|
.p.mime_type = "image/gif",
|
|
|
|
.p.extensions = "gif",
|
2012-02-19 21:24:45 +03:00
|
|
|
.priv_data_size = sizeof(GIFContext),
|
2023-01-27 16:06:00 +02:00
|
|
|
.p.audio_codec = AV_CODEC_ID_NONE,
|
|
|
|
.p.video_codec = AV_CODEC_ID_GIF,
|
2024-03-19 22:40:54 +02:00
|
|
|
.p.subtitle_codec = AV_CODEC_ID_NONE,
|
2024-03-20 00:28:36 +02:00
|
|
|
.flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
|
|
|
|
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
|
2024-03-20 01:51:30 +02:00
|
|
|
.init = gif_init,
|
2012-02-19 21:24:45 +03:00
|
|
|
.write_packet = gif_write_packet,
|
|
|
|
.write_trailer = gif_write_trailer,
|
2023-01-27 16:06:00 +02:00
|
|
|
.p.priv_class = &gif_muxer_class,
|
|
|
|
.p.flags = AVFMT_VARIABLE_FPS,
|
2002-04-08 15:32:01 +03:00
|
|
|
};
|