FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gif.c
Go to the documentation of this file.
1 /*
2  * GIF encoder.
3  * Copyright (c) 2000 Fabrice Bellard
4  * Copyright (c) 2002 Francois Revol
5  * Copyright (c) 2006 Baptiste Coudurier
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 /*
25  * First version by Francois Revol revol@free.fr
26  *
27  * Features and limitations:
28  * - currently no compression is performed,
29  * in fact the size of the data is 9/8 the size of the image in 8bpp
30  * - uses only a global standard palette
31  * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
32  *
33  * Reference documents:
34  * http://www.goice.co.jp/member/mo/formats/gif.html
35  * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
36  * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
37  *
38  * this url claims to have an LZW algorithm not covered by Unisys patent:
39  * http://www.msg.net/utility/whirlgif/gifencod.html
40  * could help reduce the size of the files _a lot_...
41  * some sites mentions an RLE type compression also.
42  */
43 
44 #include "avcodec.h"
45 #include "bytestream.h"
46 #include "internal.h"
47 #include "lzw.h"
48 
49 /* The GIF format uses reversed order for bitstreams... */
50 /* at least they don't use PDP_ENDIAN :) */
51 #define BITSTREAM_WRITER_LE
52 
53 #include "put_bits.h"
54 
55 typedef struct {
59 } GIFContext;
60 
61 /* GIF header */
63  uint8_t **bytestream, uint32_t *palette)
64 {
65  int i;
66  unsigned int v, smallest_alpha = 0xFF, alpha_component = 0;
67 
68  bytestream_put_buffer(bytestream, "GIF", 3);
69  bytestream_put_buffer(bytestream, "89a", 3);
70  bytestream_put_le16(bytestream, avctx->width);
71  bytestream_put_le16(bytestream, avctx->height);
72 
73  bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
74  bytestream_put_byte(bytestream, 0x1f); /* background color index */
75  bytestream_put_byte(bytestream, 0); /* aspect ratio */
76 
77  /* the global palette */
78  for(i=0;i<256;i++) {
79  v = palette[i];
80  bytestream_put_be24(bytestream, v);
81  if (v >> 24 < smallest_alpha) {
82  smallest_alpha = v >> 24;
83  alpha_component = i;
84  }
85  }
86 
87  if (smallest_alpha < 128) {
88  bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
89  bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
90  bytestream_put_byte(bytestream, 0x04); /* block length */
91  bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
92  bytestream_put_le16(bytestream, 0x00); /* no delay */
93  bytestream_put_byte(bytestream, alpha_component);
94  bytestream_put_byte(bytestream, 0x00);
95  }
96 
97  return 0;
98 }
99 
101  uint8_t **bytestream, uint8_t *end,
102  const uint8_t *buf, int linesize)
103 {
104  GIFContext *s = avctx->priv_data;
105  int len = 0, height;
106  const uint8_t *ptr;
107  /* image block */
108 
109  bytestream_put_byte(bytestream, 0x2c);
110  bytestream_put_le16(bytestream, 0);
111  bytestream_put_le16(bytestream, 0);
112  bytestream_put_le16(bytestream, avctx->width);
113  bytestream_put_le16(bytestream, avctx->height);
114  bytestream_put_byte(bytestream, 0x00); /* flags */
115  /* no local clut */
116 
117  bytestream_put_byte(bytestream, 0x08);
118 
119  ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
120  12, FF_LZW_GIF, put_bits);
121 
122  ptr = buf;
123  for (height = avctx->height; height--;) {
124  len += ff_lzw_encode(s->lzw, ptr, avctx->width);
125  ptr += linesize;
126  }
128 
129  ptr = s->buf;
130  while (len > 0) {
131  int size = FFMIN(255, len);
132  bytestream_put_byte(bytestream, size);
133  if (end - *bytestream < size)
134  return -1;
135  bytestream_put_buffer(bytestream, ptr, size);
136  ptr += size;
137  len -= size;
138  }
139  bytestream_put_byte(bytestream, 0x00); /* end of image block */
140  bytestream_put_byte(bytestream, 0x3b);
141  return 0;
142 }
143 
145 {
146  GIFContext *s = avctx->priv_data;
147 
148  if (avctx->width > 65535 || avctx->height > 65535) {
149  av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
150  return -1;
151  }
152 
153  avctx->coded_frame = &s->picture;
155  if (!s->lzw)
156  return AVERROR(ENOMEM);
157  s->buf = av_malloc(avctx->width*avctx->height*2);
158  if (!s->buf)
159  return AVERROR(ENOMEM);
160  return 0;
161 }
162 
163 /* better than nothing gif encoder */
165  const AVFrame *pict, int *got_packet)
166 {
167  GIFContext *s = avctx->priv_data;
168  AVFrame *const p = &s->picture;
169  uint8_t *outbuf_ptr, *end;
170  int ret;
171 
172  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
173  return ret;
174  outbuf_ptr = pkt->data;
175  end = pkt->data + pkt->size;
176 
177  *p = *pict;
179  p->key_frame = 1;
180  gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
181  gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
182 
183  pkt->size = outbuf_ptr - pkt->data;
184  pkt->flags |= AV_PKT_FLAG_KEY;
185  *got_packet = 1;
186 
187  return 0;
188 }
189 
191 {
192  GIFContext *s = avctx->priv_data;
193 
194  av_freep(&s->lzw);
195  av_freep(&s->buf);
196  return 0;
197 }
198 
200  .name = "gif",
201  .type = AVMEDIA_TYPE_VIDEO,
202  .id = AV_CODEC_ID_GIF,
203  .priv_data_size = sizeof(GIFContext),
205  .encode2 = gif_encode_frame,
207  .pix_fmts = (const enum AVPixelFormat[]){
210  },
211  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
212 };