Replace all CODEC_ID_* with AV_CODEC_ID_*
[libav.git] / libavcodec / gif.c
CommitLineData
93481fe5
BC
1/*
2 * GIF encoder.
406792e7
DB
3 * Copyright (c) 2000 Fabrice Bellard
4 * Copyright (c) 2002 Francois Revol
5 * Copyright (c) 2006 Baptiste Coudurier
93481fe5 6 *
2912e87a 7 * This file is part of Libav.
93481fe5 8 *
2912e87a 9 * Libav is free software; you can redistribute it and/or
93481fe5
BC
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 *
2912e87a 14 * Libav is distributed in the hope that it will be useful,
93481fe5
BC
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
2912e87a 20 * License along with Libav; if not, write to the Free Software
93481fe5
BC
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"
577fed3b 46#include "internal.h"
f38e4507 47#include "lzw.h"
6d32ec6c
BC
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
b2755007 53#include "put_bits.h"
93481fe5 54
e4e8632a
BC
55typedef struct {
56 AVFrame picture;
f38e4507
BC
57 LZWState *lzw;
58 uint8_t *buf;
e4e8632a
BC
59} GIFContext;
60
93481fe5 61/* GIF header */
ab3faa8b
BC
62static int gif_image_write_header(AVCodecContext *avctx,
63 uint8_t **bytestream, uint32_t *palette)
93481fe5
BC
64{
65 int i;
66 unsigned int v;
67
68 bytestream_put_buffer(bytestream, "GIF", 3);
69 bytestream_put_buffer(bytestream, "89a", 3);
ab3faa8b
BC
70 bytestream_put_le16(bytestream, avctx->width);
71 bytestream_put_le16(bytestream, avctx->height);
93481fe5
BC
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 */
dacfaf23
DV
78 for(i=0;i<256;i++) {
79 v = palette[i];
80 bytestream_put_be24(bytestream, v);
93481fe5
BC
81 }
82
93481fe5
BC
83 return 0;
84}
85
f38e4507
BC
86static int gif_image_write_image(AVCodecContext *avctx,
87 uint8_t **bytestream, uint8_t *end,
af7436ae 88 const uint8_t *buf, int linesize)
93481fe5 89{
f38e4507 90 GIFContext *s = avctx->priv_data;
c56e7130 91 int len = 0, height;
93481fe5
BC
92 const uint8_t *ptr;
93 /* image block */
94
95 bytestream_put_byte(bytestream, 0x2c);
cc26bf0d
BC
96 bytestream_put_le16(bytestream, 0);
97 bytestream_put_le16(bytestream, 0);
ab3faa8b
BC
98 bytestream_put_le16(bytestream, avctx->width);
99 bytestream_put_le16(bytestream, avctx->height);
93481fe5
BC
100 bytestream_put_byte(bytestream, 0x00); /* flags */
101 /* no local clut */
102
103 bytestream_put_byte(bytestream, 0x08);
104
f38e4507
BC
105 ff_lzw_encode_init(s->lzw, s->buf, avctx->width*avctx->height,
106 12, FF_LZW_GIF, put_bits);
93481fe5 107
93481fe5 108 ptr = buf;
f38e4507
BC
109 for (height = avctx->height; height--;) {
110 len += ff_lzw_encode(s->lzw, ptr, avctx->width);
111 ptr += linesize;
112 }
113 len += ff_lzw_encode_flush(s->lzw, flush_put_bits);
114
115 ptr = s->buf;
116 while (len > 0) {
117 int size = FFMIN(255, len);
118 bytestream_put_byte(bytestream, size);
119 if (end - *bytestream < size)
120 return -1;
121 bytestream_put_buffer(bytestream, ptr, size);
122 ptr += size;
123 len -= size;
93481fe5
BC
124 }
125 bytestream_put_byte(bytestream, 0x00); /* end of image block */
126 bytestream_put_byte(bytestream, 0x3b);
127 return 0;
128}
129
98a6fff9 130static av_cold int gif_encode_init(AVCodecContext *avctx)
93481fe5
BC
131{
132 GIFContext *s = avctx->priv_data;
133
134 avctx->coded_frame = &s->picture;
f38e4507
BC
135 s->lzw = av_mallocz(ff_lzw_encode_state_size);
136 if (!s->lzw)
2874c81c 137 return AVERROR(ENOMEM);
f38e4507
BC
138 s->buf = av_malloc(avctx->width*avctx->height*2);
139 if (!s->buf)
2874c81c 140 return AVERROR(ENOMEM);
93481fe5
BC
141 return 0;
142}
143
144/* better than nothing gif encoder */
577fed3b
AK
145static int gif_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
146 const AVFrame *pict, int *got_packet)
93481fe5
BC
147{
148 GIFContext *s = avctx->priv_data;
562b6c74 149 AVFrame *const p = &s->picture;
577fed3b
AK
150 uint8_t *outbuf_ptr, *end;
151 int ret;
152
153 if ((ret = ff_alloc_packet(pkt, avctx->width*avctx->height*7/5 + FF_MIN_BUFFER_SIZE)) < 0) {
154 av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
155 return ret;
156 }
157 outbuf_ptr = pkt->data;
158 end = pkt->data + pkt->size;
93481fe5
BC
159
160 *p = *pict;
975a1447 161 p->pict_type = AV_PICTURE_TYPE_I;
93481fe5 162 p->key_frame = 1;
ab3faa8b 163 gif_image_write_header(avctx, &outbuf_ptr, (uint32_t *)pict->data[1]);
f38e4507 164 gif_image_write_image(avctx, &outbuf_ptr, end, pict->data[0], pict->linesize[0]);
577fed3b
AK
165
166 pkt->size = outbuf_ptr - pkt->data;
167 pkt->flags |= AV_PKT_FLAG_KEY;
168 *got_packet = 1;
169
170 return 0;
93481fe5
BC
171}
172
f38e4507
BC
173static int gif_encode_close(AVCodecContext *avctx)
174{
175 GIFContext *s = avctx->priv_data;
176
177 av_freep(&s->lzw);
178 av_freep(&s->buf);
179 return 0;
180}
181
d36beb3f 182AVCodec ff_gif_encoder = {
ec6402b7
AK
183 .name = "gif",
184 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 185 .id = AV_CODEC_ID_GIF,
ec6402b7
AK
186 .priv_data_size = sizeof(GIFContext),
187 .init = gif_encode_init,
577fed3b 188 .encode2 = gif_encode_frame,
ec6402b7 189 .close = gif_encode_close,
00c3b67b
MS
190 .pix_fmts = (const enum PixelFormat[]){
191 PIX_FMT_RGB8, PIX_FMT_BGR8, PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE,
192 PIX_FMT_GRAY8, PIX_FMT_PAL8, PIX_FMT_NONE
193 },
194 .long_name = NULL_IF_CONFIG_SMALL("GIF (Graphics Interchange Format)"),
93481fe5 195};