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  * - uses only a global standard palette
29  * - tested with IE 5.0, Opera for BeOS, NetPositive (BeOS), and Mozilla (BeOS).
30  *
31  * Reference documents:
32  * http://www.goice.co.jp/member/mo/formats/gif.html
33  * http://astronomy.swin.edu.au/pbourke/dataformats/gif/
34  * http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/GIF89a.txt
35  */
36 
37 #include "avcodec.h"
38 #include "bytestream.h"
39 #include "internal.h"
40 #include "lzw.h"
41 
42 /* The GIF format uses reversed order for bitstreams... */
43 /* at least they don't use PDP_ENDIAN :) */
44 #define BITSTREAM_WRITER_LE
45 
46 #include "put_bits.h"
47 
48 typedef struct {
52 } GIFContext;
53 
54 /* GIF header */
56  uint8_t **bytestream, uint32_t *palette)
57 {
58  int i;
59  unsigned int v, smallest_alpha = 0xFF, alpha_component = 0;
60 
61  bytestream_put_buffer(bytestream, "GIF", 3);
62  bytestream_put_buffer(bytestream, "89a", 3);
63  bytestream_put_le16(bytestream, avctx->width);
64  bytestream_put_le16(bytestream, avctx->height);
65 
66  bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
67  bytestream_put_byte(bytestream, 0x1f); /* background color index */
68  bytestream_put_byte(bytestream, 0); /* aspect ratio */
69 
70  /* the global palette */
71  for(i=0;i<256;i++) {
72  v = palette[i];
73  bytestream_put_be24(bytestream, v);
74  if (v >> 24 < smallest_alpha) {
75  smallest_alpha = v >> 24;
76  alpha_component = i;
77  }
78  }
79 
80  if (smallest_alpha < 128) {
81  bytestream_put_byte(bytestream, 0x21); /* Extension Introducer */
82  bytestream_put_byte(bytestream, 0xf9); /* Graphic Control Label */
83  bytestream_put_byte(bytestream, 0x04); /* block length */
84  bytestream_put_byte(bytestream, 0x01); /* Transparent Color Flag */
85  bytestream_put_le16(bytestream, 0x00); /* no delay */
86  bytestream_put_byte(bytestream, alpha_component);
87  bytestream_put_byte(bytestream, 0x00);
88  }
89 
90  return 0;
91 }
92 
94  uint8_t **bytestream, uint8_t *end,
95  const uint8_t *buf, int linesize)
96 {
97  GIFContext *s = avctx->priv_data;
98  int len = 0, height;
99  const uint8_t *ptr;
100  /* image block */
101 
102  bytestream_put_byte(bytestream, 0x2c);
103  bytestream_put_le16(bytestream, 0);
104  bytestream_put_le16(bytestream, 0);
105  bytestream_put_le16(bytestream, avctx->width);
106  bytestream_put_le16(bytestream, avctx->height);
107  bytestream_put_byte(bytestream, 0x00); /* flags */
108  /* no local clut */
109 
110  bytestream_put_byte(bytestream, 0x08);
111 
112  ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
113  12, FF_LZW_GIF, put_bits);
114 
115  ptr = buf;
116  for (height = avctx->height; height--;) {
117  len += ff_lzw_encode(s->lzw, ptr, avctx->width);
118  ptr += linesize;
119  }
121 
122  ptr = s->buf;
123  while (len > 0) {
124  int size = FFMIN(255, len);
125  bytestream_put_byte(bytestream, size);
126  if (end - *bytestream < size)
127  return -1;
128  bytestream_put_buffer(bytestream, ptr, size);
129  ptr += size;
130  len -= size;
131  }
132  bytestream_put_byte(bytestream, 0x00); /* end of image block */
133  bytestream_put_byte(bytestream, 0x3b);
134  return 0;
135 }
136 
138 {
139  GIFContext *s = avctx->priv_data;
140 
141  if (avctx->width > 65535 || avctx->height > 65535) {
142  av_log(avctx, AV_LOG_ERROR, "GIF does not support resolutions above 65535x65535\n");
143  return -1;
144  }
145 
146  avctx->coded_frame = &s->picture;
148  if (!s->lzw)
149  return AVERROR(ENOMEM);
150  s->buf = av_malloc(avctx->width*avctx->height*2);
151  if (!s->buf)
152  return AVERROR(ENOMEM);
153  return 0;
154 }
155 
156 /* better than nothing gif encoder */
158  const AVFrame *pict, int *got_packet)
159 {
160  GIFContext *s = avctx->priv_data;
161  AVFrame *const p = &s->picture;
162  uint8_t *outbuf_ptr, *end;
163  int ret;
164 
165  if ((ret = ff_alloc_packet2(avctx, pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0)
166  return ret;
167  outbuf_ptr = pkt->data;
168  end = pkt->data + pkt->size;
169 
170  *p = *pict;
172  p->key_frame = 1;
173  gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
174  gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
175 
176  pkt->size = outbuf_ptr - pkt->data;
177  pkt->flags |= AV_PKT_FLAG_KEY;
178  *got_packet = 1;
179 
180  return 0;
181 }
182 
184 {
185  GIFContext *s = avctx->priv_data;
186 
187  av_freep(&s->lzw);
188  av_freep(&s->buf);
189  return 0;
190 }
191 
193  .name = "gif",
194  .type = AVMEDIA_TYPE_VIDEO,
195  .id = AV_CODEC_ID_GIF,
196  .priv_data_size = sizeof(GIFContext),
198  .encode2 = gif_encode_frame,
200  .pix_fmts = (const enum AVPixelFormat[]){
203  },
204  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
205 };