FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gif.c
Go to the documentation of this file.
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 
24 #include "avformat.h"
25 #include "internal.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/log.h"
29 #include "libavutil/opt.h"
30 
31 /* XXX: random value that shouldn't be taken into effect if there is no
32  * transparent color in the palette (the transparency bit will be set to 0) */
33 #define DEFAULT_TRANSPARENCY_INDEX 0x1f
34 
35 static int get_palette_transparency_index(const uint32_t *palette)
36 {
37  int transparent_color_index = -1;
38  unsigned i, smallest_alpha = 0xff;
39 
40  if (!palette)
41  return -1;
42 
43  for (i = 0; i < AVPALETTE_COUNT; i++) {
44  const uint32_t v = palette[i];
45  if (v >> 24 < smallest_alpha) {
46  smallest_alpha = v >> 24;
47  transparent_color_index = i;
48  }
49  }
50  return smallest_alpha < 128 ? transparent_color_index : -1;
51 }
52 
53 static int gif_image_write_header(AVIOContext *pb, const AVCodecContext *avctx,
54  int loop_count, uint32_t *palette)
55 {
56  int i;
57  int64_t aspect = 0;
58  const AVRational sar = avctx->sample_aspect_ratio;
59 
60  if (sar.num > 0 && sar.den > 0) {
61  aspect = sar.num * 64LL / sar.den - 15;
62  if (aspect < 0 || aspect > 255)
63  aspect = 0;
64  }
65 
66  avio_write(pb, "GIF", 3);
67  avio_write(pb, "89a", 3);
68  avio_wl16(pb, avctx->width);
69  avio_wl16(pb, avctx->height);
70 
71  if (palette) {
72  const int bcid = get_palette_transparency_index(palette);
73 
74  avio_w8(pb, 0xf7); /* flags: global clut, 256 entries */
75  avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid); /* background color index */
76  avio_w8(pb, aspect);
77  for (i = 0; i < 256; i++) {
78  const uint32_t v = palette[i] & 0xffffff;
79  avio_wb24(pb, v);
80  }
81  } else {
82  avio_w8(pb, 0); /* flags */
83  avio_w8(pb, 0); /* background color index */
84  avio_w8(pb, aspect);
85  }
86 
87 
88  if (loop_count >= 0 ) {
89  /* "NETSCAPE EXTENSION" for looped animation GIF */
90  avio_w8(pb, 0x21); /* GIF Extension code */
91  avio_w8(pb, 0xff); /* Application Extension Label */
92  avio_w8(pb, 0x0b); /* Length of Application Block */
93  avio_write(pb, "NETSCAPE2.0", sizeof("NETSCAPE2.0") - 1);
94  avio_w8(pb, 0x03); /* Length of Data Sub-Block */
95  avio_w8(pb, 0x01);
96  avio_wl16(pb, (uint16_t)loop_count);
97  avio_w8(pb, 0x00); /* Data Sub-block Terminator */
98  }
99 
100  avio_flush(pb);
101  return 0;
102 }
103 
104 typedef struct GIFContext {
105  AVClass *class;
106  int loop;
109  int duration;
110 } GIFContext;
111 
113 {
114  GIFContext *gif = s->priv_data;
115  AVCodecContext *video_enc;
116  uint32_t palette[AVPALETTE_COUNT];
117 
118  if (s->nb_streams != 1 ||
120  s->streams[0]->codec->codec_id != AV_CODEC_ID_GIF) {
121  av_log(s, AV_LOG_ERROR,
122  "GIF muxer supports only a single video GIF stream.\n");
123  return AVERROR(EINVAL);
124  }
125 
126  video_enc = s->streams[0]->codec;
127 
128  avpriv_set_pts_info(s->streams[0], 64, 1, 100);
129  if (avpriv_set_systematic_pal2(palette, video_enc->pix_fmt) < 0) {
130  av_assert0(video_enc->pix_fmt == AV_PIX_FMT_PAL8);
131  /* delay header writing: we wait for the first palette to put it
132  * globally */
133  } else {
134  gif_image_write_header(s->pb, video_enc, gif->loop, palette);
135  }
136 
137  return 0;
138 }
139 
141 {
142  GIFContext *gif = s->priv_data;
143  int size, bcid;
144  AVIOContext *pb = s->pb;
145  const uint32_t *palette;
146  AVPacket *pkt = gif->prev_pkt;
147 
148  if (!pkt)
149  return 0;
150 
151  /* Mark one colour as transparent if the input palette contains at least
152  * one colour that is more than 50% transparent. */
153  palette = (uint32_t*)av_packet_get_side_data(pkt, AV_PKT_DATA_PALETTE, &size);
154  if (palette && size != AVPALETTE_SIZE) {
155  av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
156  return AVERROR_INVALIDDATA;
157  }
158  bcid = get_palette_transparency_index(palette);
159 
160  if (new && new->pts != AV_NOPTS_VALUE)
161  gif->duration = av_clip_uint16(new->pts - gif->prev_pkt->pts);
162  else if (!new && gif->last_delay >= 0)
163  gif->duration = gif->last_delay;
164 
165  /* graphic control extension block */
166  avio_w8(pb, 0x21);
167  avio_w8(pb, 0xf9);
168  avio_w8(pb, 0x04); /* block size */
169  avio_w8(pb, 1<<2 | (bcid >= 0));
170  avio_wl16(pb, gif->duration);
171  avio_w8(pb, bcid < 0 ? DEFAULT_TRANSPARENCY_INDEX : bcid);
172  avio_w8(pb, 0x00);
173 
174  avio_write(pb, pkt->data, pkt->size);
175 
176  av_free_packet(gif->prev_pkt);
177  if (new)
178  av_copy_packet(gif->prev_pkt, new);
179 
180  return 0;
181 }
182 
184 {
185  GIFContext *gif = s->priv_data;
186  const AVCodecContext *video_enc = s->streams[0]->codec;
187 
188  if (!gif->prev_pkt) {
189  gif->prev_pkt = av_malloc(sizeof(*gif->prev_pkt));
190  if (!gif->prev_pkt)
191  return AVERROR(ENOMEM);
192 
193  /* Write the first palette as global palette */
194  if (video_enc->pix_fmt == AV_PIX_FMT_PAL8) {
195  int size;
197 
198  if (!palette) {
199  av_log(s, AV_LOG_ERROR, "PAL8 packet is missing palette in extradata\n");
200  return AVERROR_INVALIDDATA;
201  }
202  if (size != AVPALETTE_SIZE) {
203  av_log(s, AV_LOG_ERROR, "Invalid palette extradata\n");
204  return AVERROR_INVALIDDATA;
205  }
206  gif_image_write_header(s->pb, video_enc, gif->loop, palette);
207  }
208 
209  return av_copy_packet(gif->prev_pkt, pkt);
210  }
211  return flush_packet(s, pkt);
212 }
213 
215 {
216  GIFContext *gif = s->priv_data;
217  AVIOContext *pb = s->pb;
218 
219  flush_packet(s, NULL);
220  av_freep(&gif->prev_pkt);
221  avio_w8(pb, 0x3b);
222 
223  return 0;
224 }
225 
226 #define OFFSET(x) offsetof(GIFContext, x)
227 #define ENC AV_OPT_FLAG_ENCODING_PARAM
228 static const AVOption options[] = {
229  { "loop", "Number of times to loop the output: -1 - no loop, 0 - infinite loop", OFFSET(loop),
230  AV_OPT_TYPE_INT, { .i64 = 0 }, -1, 65535, ENC },
231  { "final_delay", "Force delay (in centiseconds) after the last frame", OFFSET(last_delay),
232  AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 65535, ENC },
233  { NULL },
234 };
235 
236 static const AVClass gif_muxer_class = {
237  .class_name = "GIF muxer",
238  .item_name = av_default_item_name,
239  .version = LIBAVUTIL_VERSION_INT,
240  .option = options,
241 };
242 
244  .name = "gif",
245  .long_name = NULL_IF_CONFIG_SMALL("GIF Animation"),
246  .mime_type = "image/gif",
247  .extensions = "gif",
248  .priv_data_size = sizeof(GIFContext),
249  .audio_codec = AV_CODEC_ID_NONE,
250  .video_codec = AV_CODEC_ID_GIF,
254  .priv_class = &gif_muxer_class,
256 };
#define NULL
Definition: coverity.c:32
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:417
float v
const char * s
Definition: avisynth_c.h:631
Bytestream IO Context.
Definition: avio.h:111
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:280
AVOption.
Definition: opt.h:255
#define OFFSET(x)
Definition: gif.c:226
misc image utilities
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
static int flush_packet(AVFormatContext *s, AVPacket *new)
Definition: gif.c:140
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4083
int num
numerator
Definition: rational.h:44
int size
Definition: avcodec.h:1424
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1902
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
static int gif_write_header(AVFormatContext *s)
Definition: gif.c:112
static const AVClass gif_muxer_class
Definition: gif.c:236
#define ENC
Definition: gif.c:227
static int get_palette_transparency_index(const uint32_t *palette)
Definition: gif.c:35
static AVPacket pkt
AVOutputFormat ff_gif_muxer
Definition: gif.c:243
Format I/O context.
Definition: avformat.h:1273
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define av_malloc(s)
8 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
AVOptions.
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:152
#define AVPALETTE_SIZE
Definition: pixfmt.h:33
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1341
uint8_t * data
Definition: avcodec.h:1423
ptrdiff_t size
Definition: opengl_enc.c:101
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:178
#define DEFAULT_TRANSPARENCY_INDEX
Definition: gif.c:33
#define av_log(a,...)
static int gif_image_write_header(AVIOContext *pb, const AVCodecContext *avctx, int loop_count, uint32_t *palette)
Definition: gif.c:53
int loop
Definition: gif.c:106
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
Definition: gif.c:42
simple assert() macros that are a bit more flexible than ISO C assert().
AVPacket * prev_pkt
Definition: gif.c:108
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:861
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1329
int void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:198
int width
picture width / height.
Definition: avcodec.h:1681
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:94
void avio_wb24(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:435
const char * name
Definition: avformat.h:513
int duration
Definition: gif.c:109
static int gif_write_trailer(AVFormatContext *s)
Definition: gif.c:214
enum AVMediaType codec_type
Definition: avcodec.h:1510
enum AVCodecID codec_id
Definition: avcodec.h:1519
AVIOContext * pb
I/O context.
Definition: avformat.h:1315
static int loop
Definition: ffplay.c:334
static const AVOption options[]
Definition: gif.c:228
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:156
main external API structure.
Definition: avcodec.h:1502
int av_copy_packet(AVPacket *dst, const AVPacket *src)
Copy packet, including contents.
Definition: avpacket.c:265
int last_delay
Definition: gif.c:107
Describe the class of an AVClass context structure.
Definition: log.h:67
rational number numerator/denominator
Definition: rational.h:43
#define AVPALETTE_COUNT
Definition: pixfmt.h:34
static int flags
Definition: cpu.c:47
int palette
Definition: v4l.c:61
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:475
void * priv_data
Format private data.
Definition: avformat.h:1301
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:493
static int gif_write_packet(AVFormatContext *s, AVPacket *pkt)
Definition: gif.c:183
#define av_freep(p)
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:324
This structure stores compressed data.
Definition: avcodec.h:1400
static int write_packet(AVFormatContext *s1, AVPacket *pkt)
Definition: v4l2enc.c:86
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1416
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:240