~ubuntu-branches/debian/sid/mplayer/sid

« back to all changes in this revision

Viewing changes to libavformat/gif.c

  • Committer: Bazaar Package Importer
  • Author(s): A Mennucc1
  • Date: 2009-03-23 10:05:45 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090323100545-x8h79obawnnte7kk
Tags: 1.0~rc2+svn20090303-5
debian/control : move docbook-xml,docbook-xsl,xsltproc from 
Build-Depends-Indep to Build-Depends, since they are needed to run
configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Animated GIF muxer
3
 
 * Copyright (c) 2000 Fabrice Bellard.
 
3
 * Copyright (c) 2000 Fabrice Bellard
4
4
 *
5
5
 * This file is part of FFmpeg.
6
6
 *
40
40
 */
41
41
 
42
42
#include "avformat.h"
43
 
#include "bitstream.h"
 
43
 
 
44
/* The GIF format uses reversed order for bitstreams... */
 
45
/* at least they don't use PDP_ENDIAN :) */
 
46
#define BITSTREAM_WRITER_LE
 
47
 
 
48
#include "libavcodec/bitstream.h"
44
49
 
45
50
/* bitstream minipacket size */
46
51
#define GIF_CHUNKS 100
101
106
    { 0xff, 0xff, 0x00 }, { 0xff, 0xff, 0x33 }, { 0xff, 0xff, 0x66 }, { 0xff, 0xff, 0x99 }, { 0xff, 0xff, 0xcc }, { 0xff, 0xff, 0xff },
102
107
};
103
108
 
104
 
/* The GIF format uses reversed order for bitstreams... */
105
 
/* at least they don't use PDP_ENDIAN :) */
106
 
/* so we 'extend' PutBitContext. hmmm, OOP :) */
107
 
/* seems this thing changed slightly since I wrote it... */
108
 
 
109
 
#ifdef ALT_BITSTREAM_WRITER
110
 
# error no ALT_BITSTREAM_WRITER support for now
111
 
#endif
112
 
 
113
 
static void gif_put_bits_rev(PutBitContext *s, int n, unsigned int value)
114
 
{
115
 
    unsigned int bit_buf;
116
 
    int bit_cnt;
117
 
 
118
 
    //    printf("put_bits=%d %x\n", n, value);
119
 
    assert(n == 32 || value < (1U << n));
120
 
 
121
 
    bit_buf = s->bit_buf;
122
 
    bit_cnt = 32 - s->bit_left; /* XXX:lazyness... was = s->bit_cnt; */
123
 
 
124
 
    //    printf("n=%d value=%x cnt=%d buf=%x\n", n, value, bit_cnt, bit_buf);
125
 
    /* XXX: optimize */
126
 
    if (n < (32-bit_cnt)) {
127
 
        bit_buf |= value << (bit_cnt);
128
 
        bit_cnt+=n;
129
 
    } else {
130
 
        bit_buf |= value << (bit_cnt);
131
 
 
132
 
        *s->buf_ptr = bit_buf & 0xff;
133
 
        s->buf_ptr[1] = (bit_buf >> 8) & 0xff;
134
 
        s->buf_ptr[2] = (bit_buf >> 16) & 0xff;
135
 
        s->buf_ptr[3] = (bit_buf >> 24) & 0xff;
136
 
 
137
 
        //printf("bitbuf = %08x\n", bit_buf);
138
 
        s->buf_ptr+=4;
139
 
        if (s->buf_ptr >= s->buf_end)
140
 
            puts("bit buffer overflow !!"); // should never happen ! who got rid of the callback ???
141
 
//            flush_buffer_rev(s);
142
 
        bit_cnt=bit_cnt + n - 32;
143
 
        if (bit_cnt == 0) {
144
 
            bit_buf = 0;
145
 
        } else {
146
 
            bit_buf = value >> (n - bit_cnt);
147
 
        }
148
 
    }
149
 
 
150
 
    s->bit_buf = bit_buf;
151
 
    s->bit_left = 32 - bit_cnt;
152
 
}
153
 
 
154
 
/* pad the end of the output stream with zeros */
155
 
static void gif_flush_put_bits_rev(PutBitContext *s)
156
 
{
157
 
    while (s->bit_left < 32) {
158
 
        /* XXX: should test end of buffer */
159
 
        *s->buf_ptr++=s->bit_buf & 0xff;
160
 
        s->bit_buf>>=8;
161
 
        s->bit_left+=8;
162
 
    }
163
 
//    flush_buffer_rev(s);
164
 
    s->bit_left=32;
165
 
    s->bit_buf=0;
166
 
}
167
 
 
168
 
/* !RevPutBitContext */
169
 
 
170
109
/* GIF header */
171
110
static int gif_image_write_header(ByteIOContext *pb,
172
111
                                  int width, int height, int loop_count,
236
175
/* this is maybe slow, but allows for extensions */
237
176
static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
238
177
{
239
 
    return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
 
178
    return (((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6);
240
179
}
241
180
 
242
181
 
272
211
    w = width;
273
212
    while(left>0) {
274
213
 
275
 
        gif_put_bits_rev(&p, 9, 0x0100); /* clear code */
 
214
        put_bits(&p, 9, 0x0100); /* clear code */
276
215
 
277
216
        for(i=(left<GIF_CHUNKS)?left:GIF_CHUNKS;i;i--) {
278
217
            if (pix_fmt == PIX_FMT_RGB24) {
281
220
            } else {
282
221
                v = *ptr++;
283
222
            }
284
 
            gif_put_bits_rev(&p, 9, v);
 
223
            put_bits(&p, 9, v);
285
224
            if (--w == 0) {
286
225
                w = width;
287
226
                buf += linesize;
290
229
        }
291
230
 
292
231
        if(left<=GIF_CHUNKS) {
293
 
            gif_put_bits_rev(&p, 9, 0x101); /* end of stream */
294
 
            gif_flush_put_bits_rev(&p);
 
232
            put_bits(&p, 9, 0x101); /* end of stream */
 
233
            flush_put_bits(&p);
295
234
        }
296
235
        if(pbBufPtr(&p) - p.buf > 0) {
297
236
            put_byte(pb, pbBufPtr(&p) - p.buf); /* byte count of the packet */
313
252
static int gif_write_header(AVFormatContext *s)
314
253
{
315
254
    GIFContext *gif = s->priv_data;
316
 
    ByteIOContext *pb = &s->pb;
 
255
    ByteIOContext *pb = s->pb;
317
256
    AVCodecContext *enc, *video_enc;
318
257
    int i, width, height, loop_count /*, rate*/;
319
258
 
348
287
 
349
288
    gif_image_write_header(pb, width, height, loop_count, NULL);
350
289
 
351
 
    put_flush_packet(&s->pb);
 
290
    put_flush_packet(s->pb);
352
291
    return 0;
353
292
}
354
293
 
355
294
static int gif_write_video(AVFormatContext *s,
356
295
                           AVCodecContext *enc, const uint8_t *buf, int size)
357
296
{
358
 
    ByteIOContext *pb = &s->pb;
 
297
    ByteIOContext *pb = s->pb;
359
298
    GIFContext *gif = s->priv_data;
360
299
    int jiffies;
361
300
    int64_t delay;
383
322
    gif_image_write_image(pb, 0, 0, enc->width, enc->height,
384
323
                          buf, enc->width * 3, PIX_FMT_RGB24);
385
324
 
386
 
    put_flush_packet(&s->pb);
 
325
    put_flush_packet(s->pb);
387
326
    return 0;
388
327
}
389
328
 
398
337
 
399
338
static int gif_write_trailer(AVFormatContext *s)
400
339
{
401
 
    ByteIOContext *pb = &s->pb;
 
340
    ByteIOContext *pb = s->pb;
402
341
 
403
342
    put_byte(pb, 0x3b);
404
 
    put_flush_packet(&s->pb);
 
343
    put_flush_packet(s->pb);
405
344
    return 0;
406
345
}
407
346
 
408
347
AVOutputFormat gif_muxer = {
409
348
    "gif",
410
 
    "GIF Animation",
 
349
    NULL_IF_CONFIG_SMALL("GIF Animation"),
411
350
    "image/gif",
412
351
    "gif",
413
352
    sizeof(GIFContext),