Libav
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  * first version by Francois Revol <revol@free.fr>
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
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 GIFContext {
58 } GIFContext;
59 
60 /* GIF header */
62  uint8_t **bytestream, uint32_t *palette)
63 {
64  int i;
65  unsigned int v;
66 
67  bytestream_put_buffer(bytestream, "GIF", 3);
68  bytestream_put_buffer(bytestream, "89a", 3);
69  bytestream_put_le16(bytestream, avctx->width);
70  bytestream_put_le16(bytestream, avctx->height);
71 
72  bytestream_put_byte(bytestream, 0xf7); /* flags: global clut, 256 entries */
73  bytestream_put_byte(bytestream, 0x1f); /* background color index */
74  bytestream_put_byte(bytestream, 0); /* aspect ratio */
75 
76  /* the global palette */
77  for(i=0;i<256;i++) {
78  v = palette[i];
79  bytestream_put_be24(bytestream, v);
80  }
81 
82  return 0;
83 }
84 
86  uint8_t **bytestream, uint8_t *end,
87  const uint8_t *buf, int linesize)
88 {
89  GIFContext *s = avctx->priv_data;
90  int len = 0, height;
91  const uint8_t *ptr;
92  /* image block */
93 
94  bytestream_put_byte(bytestream, 0x2c);
95  bytestream_put_le16(bytestream, 0);
96  bytestream_put_le16(bytestream, 0);
97  bytestream_put_le16(bytestream, avctx->width);
98  bytestream_put_le16(bytestream, avctx->height);
99  bytestream_put_byte(bytestream, 0x00); /* flags */
100  /* no local clut */
101 
102  bytestream_put_byte(bytestream, 0x08);
103 
104  ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
105  12, FF_LZW_GIF, put_bits);
106 
107  ptr = buf;
108  for (height = avctx->height; height--;) {
109  len += ff_lzw_encode(s->lzw, ptr, avctx->width);
110  ptr += linesize;
111  }
113 
114  ptr = s->buf;
115  while (len > 0) {
116  int size = FFMIN(255, len);
117  bytestream_put_byte(bytestream, size);
118  if (end - *bytestream < size)
119  return -1;
120  bytestream_put_buffer(bytestream, ptr, size);
121  ptr += size;
122  len -= size;
123  }
124  bytestream_put_byte(bytestream, 0x00); /* end of image block */
125  bytestream_put_byte(bytestream, 0x3b);
126  return 0;
127 }
128 
130 {
131  GIFContext *s = avctx->priv_data;
132 
133 #if FF_API_CODED_FRAME
136  avctx->coded_frame->key_frame = 1;
138 #endif
139 
141  if (!s->lzw)
142  return AVERROR(ENOMEM);
143  s->buf = av_malloc(avctx->width*avctx->height*2);
144  if (!s->buf)
145  return AVERROR(ENOMEM);
146  return 0;
147 }
148 
149 /* better than nothing gif encoder */
150 static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
151  const AVFrame *pict, int *got_packet)
152 {
153  uint8_t *outbuf_ptr, *end;
154  int ret;
155 
156  if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*7/5 + AV_INPUT_BUFFER_MIN_SIZE)) < 0) {
157  av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
158  return ret;
159  }
160  outbuf_ptr = pkt->data;
161  end = pkt->data + pkt->size;
162 
163  gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
164  gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
165 
166  pkt->size = outbuf_ptr - pkt->data;
167  pkt->flags |= AV_PKT_FLAG_KEY;
168  *got_packet = 1;
169 
170  return 0;
171 }
172 
174 {
175  GIFContext *s = avctx->priv_data;
176 
177  av_freep(&s->lzw);
178  av_freep(&s->buf);
179  return 0;
180 }
181 
183  .name = "gif",
184  .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
185  .type = AVMEDIA_TYPE_VIDEO,
186  .id = AV_CODEC_ID_GIF,
187  .priv_data_size = sizeof(GIFContext),
189  .encode2 = gif_encode_frame,
190  .close = gif_encode_close,
191  .pix_fmts = (const enum AVPixelFormat[]){
194  },
195 };
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
static int gif_image_write_image(AVCodecContext *avctx, uint8_t **bytestream, uint8_t *end, const uint8_t *buf, int linesize)
Definition: gif.c:85
int size
Definition: avcodec.h:1347
static int gif_encode_close(AVCodecContext *avctx)
Definition: gif.c:173
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int ff_lzw_encode(struct LZWEncodeState *s, const uint8_t *inbuf, int insize)
LZW main compress function.
Definition: lzwenc.c:227
AVCodec ff_gif_encoder
Definition: gif.c:182
AVCodec.
Definition: avcodec.h:3120
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:82
uint8_t
#define av_cold
Definition: attributes.h:66
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
uint8_t * data
Definition: avcodec.h:1346
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
Definition: avcodec.h:645
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int ff_lzw_encode_flush(struct LZWEncodeState *s, void(*lzw_flush_put_bits)(struct PutBitContext *))
Definition: lzw.c:46
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
LZWState * lzw
Definition: gif.c:56
#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:148
Definition: gif.c:55
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static void put_bits(PutBitContext *s, int n, unsigned int value)
Write up to 31 bits into a bitstream.
Definition: put_bits.h:134
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
#define FFMIN(a, b)
Definition: common.h:66
Definition: lzw.h:38
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:85
int width
picture width / height.
Definition: avcodec.h:1580
static int gif_image_write_header(AVCodecContext *avctx, uint8_t **bytestream, uint32_t *palette)
Definition: gif.c:61
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:80
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *pict, int *got_packet)
Definition: gif.c:150
uint8_t * buf
Definition: gif.c:57
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
int height
Definition: gxfenc.c:72
LZW decoding routines.
Y , 8bpp.
Definition: pixfmt.h:67
void ff_lzw_encode_init(struct LZWEncodeState *s, uint8_t *outbuf, int outsize, int maxbits, enum FF_LZW_MODES mode, void(*lzw_put_bits)(struct PutBitContext *, int, unsigned int))
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:83
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:83
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
Definition: bytestream.h:365
void * priv_data
Definition: avcodec.h:1451
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int len
static av_cold int gif_encode_init(AVCodecContext *avctx)
Definition: gif.c:129
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
const int ff_lzw_encode_state_size
Definition: lzwenc.c:67
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
bitstream writer API