3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2002 Francois Revol
5 * Copyright (c) 2006 Baptiste Coudurier
7 * first version by Francois Revol <revol@free.fr>
9 * This file is part of Libav.
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.
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.
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
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).
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
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.
45 #include "bytestream.h"
49 /* The GIF format uses reversed order for bitstreams... */
50 /* at least they don't use PDP_ENDIAN :) */
51 #define BITSTREAM_WRITER_LE
55 typedef struct GIFContext
{
61 static int gif_image_write_header(AVCodecContext
*avctx
,
62 uint8_t **bytestream
, uint32_t *palette
)
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
);
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 */
76 /* the global palette */
79 bytestream_put_be24(bytestream
, v
);
85 static int gif_image_write_image(AVCodecContext
*avctx
,
86 uint8_t **bytestream
, uint8_t *end
,
87 const uint8_t *buf
, int linesize
)
89 GIFContext
*s
= avctx
->priv_data
;
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 */
102 bytestream_put_byte(bytestream
, 0x08);
104 ff_lzw_encode_init(s
->lzw
, s
->buf
, avctx
->width
*avctx
->height
,
105 12, FF_LZW_GIF
, put_bits
);
108 for (height
= avctx
->height
; height
--;) {
109 len
+= ff_lzw_encode(s
->lzw
, ptr
, avctx
->width
);
112 len
+= ff_lzw_encode_flush(s
->lzw
, flush_put_bits
);
116 int size
= FFMIN(255, len
);
117 bytestream_put_byte(bytestream
, size
);
118 if (end
- *bytestream
< size
)
120 bytestream_put_buffer(bytestream
, ptr
, size
);
124 bytestream_put_byte(bytestream
, 0x00); /* end of image block */
125 bytestream_put_byte(bytestream
, 0x3b);
129 static av_cold
int gif_encode_init(AVCodecContext
*avctx
)
131 GIFContext
*s
= avctx
->priv_data
;
133 #if FF_API_CODED_FRAME
134 FF_DISABLE_DEPRECATION_WARNINGS
135 avctx
->coded_frame
->pict_type
= AV_PICTURE_TYPE_I
;
136 avctx
->coded_frame
->key_frame
= 1;
137 FF_ENABLE_DEPRECATION_WARNINGS
140 s
->lzw
= av_mallocz(ff_lzw_encode_state_size
);
142 return AVERROR(ENOMEM
);
143 s
->buf
= av_malloc(avctx
->width
*avctx
->height
*2);
145 return AVERROR(ENOMEM
);
149 /* better than nothing gif encoder */
150 static int gif_encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
151 const AVFrame
*pict
, int *got_packet
)
153 uint8_t *outbuf_ptr
, *end
;
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");
160 outbuf_ptr
= pkt
->data
;
161 end
= pkt
->data
+ pkt
->size
;
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]);
166 pkt
->size
= outbuf_ptr
- pkt
->data
;
167 pkt
->flags
|= AV_PKT_FLAG_KEY
;
173 static int gif_encode_close(AVCodecContext
*avctx
)
175 GIFContext
*s
= avctx
->priv_data
;
182 AVCodec ff_gif_encoder
= {
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
),
188 .init
= gif_encode_init
,
189 .encode2
= gif_encode_frame
,
190 .close
= gif_encode_close
,
191 .pix_fmts
= (const enum AVPixelFormat
[]){
192 AV_PIX_FMT_RGB8
, AV_PIX_FMT_BGR8
, AV_PIX_FMT_RGB4_BYTE
, AV_PIX_FMT_BGR4_BYTE
,
193 AV_PIX_FMT_GRAY8
, AV_PIX_FMT_PAL8
, AV_PIX_FMT_NONE