Statistics
| Branch: | Revision:

ffmpeg / libav / avienc.c @ 58f26ba9

History | View | Annotate | Download (9.66 KB)

1
/*
2
 * AVI encoder.
3
 * Copyright (c) 2000 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include "avformat.h"
20
#include "avi.h"
21

    
22
/*
23
 * TODO: 
24
 *  - fill all fields if non streamed (nb_frames for example)
25
 */
26

    
27
typedef struct AVIIndex {
28
    unsigned char tag[4];
29
    unsigned int flags, pos, len;
30
    struct AVIIndex *next;
31
} AVIIndex;
32

    
33
typedef struct {
34
    offset_t movi_list;
35
    AVIIndex *first, *last;
36
} AVIContext;
37

    
38
offset_t start_tag(ByteIOContext *pb, char *tag)
39
{
40
    put_tag(pb, tag);
41
    put_le32(pb, 0);
42
    return url_ftell(pb);
43
}
44

    
45
void end_tag(ByteIOContext *pb, offset_t start)
46
{
47
    offset_t pos;
48

    
49
    pos = url_ftell(pb);
50
    url_fseek(pb, start - 4, SEEK_SET);
51
    put_le32(pb, (UINT32)(pos - start));
52
    url_fseek(pb, pos, SEEK_SET);
53
}
54

    
55
/* Note: when encoding, the first matching tag is used, so order is
56
   important if multiple tags possible for a given codec. */
57
CodecTag codec_bmp_tags[] = {
58
    { CODEC_ID_H263, MKTAG('U', '2', '6', '3') },
59
    { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
60
    { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
61
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X') },
62
    { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x') },
63
    { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
64
    { CODEC_ID_MSMPEG4, MKTAG('D', 'I', 'V', '3') }, /* default signature when using MSMPEG4 */
65
    { CODEC_ID_MSMPEG4, MKTAG('M', 'P', '4', '3') }, 
66
    { 0, 0 },
67
};
68

    
69
CodecTag codec_wav_tags[] = {
70
    { CODEC_ID_MP2, 0x55 },
71
    { CODEC_ID_MP2, 0x50 },
72
    { CODEC_ID_AC3, 0x2000 },
73
    { CODEC_ID_PCM, 0x01 },
74
    { 0, 0 },
75
};
76

    
77

    
78
unsigned int codec_get_tag(CodecTag *tags, int id)
79
{
80
    while (tags->id != 0) {
81
        if (tags->id == id)
82
            return tags->tag;
83
        tags++;
84
    }
85
    return 0;
86
}
87

    
88
int codec_get_id(CodecTag *tags, unsigned int tag)
89
{
90
    while (tags->id != 0) {
91
        if (tags->tag == tag)
92
            return tags->id;
93
        tags++;
94
    }
95
    return 0;
96
}
97

    
98
unsigned int codec_get_bmp_tag(int id)
99
{
100
    return codec_get_tag(codec_bmp_tags, id);
101
}
102

    
103
/* BITMAPINFOHEADER header */
104
void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc)
105
{
106
    put_le32(pb, 40); /* size */
107
    put_le32(pb, enc->width);
108
    put_le32(pb, enc->height);
109
    put_le16(pb, 1); /* planes */
110
    put_le16(pb, 24); /* depth */
111
    /* compression type */
112
    put_le32(pb, codec_get_bmp_tag(enc->codec_id));
113
    put_le32(pb, enc->width * enc->height * 3);
114
    put_le32(pb, 0);
115
    put_le32(pb, 0);
116
    put_le32(pb, 0);
117
    put_le32(pb, 0);
118
}
119

    
120
/* WAVEFORMATEX header */
121
void put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
122
{
123
    int tag;
124

    
125
    tag = codec_get_tag(codec_wav_tags, enc->codec_id);
126

    
127
    put_le16(pb, tag); 
128
    put_le16(pb, enc->channels);
129
    put_le32(pb, enc->sample_rate);
130
    put_le32(pb, enc->bit_rate / 8);
131
    put_le16(pb, 1); /* block align */
132
    put_le16(pb, 16); /* bits per sample */
133
    put_le16(pb, 0); /* wav_extra_size */
134
}
135

    
136
static int avi_write_header(AVFormatContext *s)
137
{
138
    AVIContext *avi;
139
    ByteIOContext *pb = &s->pb;
140
    int bitrate, n, i, nb_frames;
141
    AVCodecContext *stream, *video_enc;
142
    offset_t list1, list2, strh, strf;
143

    
144
    avi = malloc(sizeof(AVIContext));
145
    if (!avi)
146
        return -1;
147
    memset(avi, 0, sizeof(AVIContext));
148
    s->priv_data = avi;
149

    
150
    put_tag(pb, "RIFF");
151
    put_le32(pb, 0); /* file length */
152
    put_tag(pb, "AVI ");
153

    
154
    /* header list */
155
    list1 = start_tag(pb, "LIST");
156
    put_tag(pb, "hdrl");
157

    
158
    /* avi header */
159
    put_tag(pb, "avih");
160
    put_le32(pb, 14 * 4);
161
    bitrate = 0;
162

    
163
    video_enc = NULL;
164
    for(n=0;n<s->nb_streams;n++) {
165
        stream = &s->streams[n]->codec;
166
        bitrate += stream->bit_rate;
167
        if (stream->codec_type == CODEC_TYPE_VIDEO)
168
            video_enc = stream;
169
    }
170
    
171
    if (!video_enc) {
172
        free(avi);
173
        return -1;
174
    }
175
    nb_frames = 0;
176

    
177
    put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
178
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
179
    put_le32(pb, 0); /* padding */
180
    put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
181
    put_le32(pb, nb_frames); /* nb frames, filled later */
182
    put_le32(pb, 0); /* initial frame */
183
    put_le32(pb, s->nb_streams); /* nb streams */
184
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
185
    put_le32(pb, video_enc->width);
186
    put_le32(pb, video_enc->height);
187
    put_le32(pb, 0); /* reserved */
188
    put_le32(pb, 0); /* reserved */
189
    put_le32(pb, 0); /* reserved */
190
    put_le32(pb, 0); /* reserved */
191
    
192
    /* stream list */
193
    for(i=0;i<n;i++) {
194
        list2 = start_tag(pb, "LIST");
195
        put_tag(pb, "strl");
196
    
197
        stream = &s->streams[i]->codec;
198

    
199
        /* stream generic header */
200
        strh = start_tag(pb, "strh");
201
        switch(stream->codec_type) {
202
        case CODEC_TYPE_VIDEO:
203
            put_tag(pb, "vids");
204
            put_le32(pb, codec_get_bmp_tag(stream->codec_id));
205
            put_le32(pb, 0); /* flags */
206
            put_le16(pb, 0); /* priority */
207
            put_le16(pb, 0); /* language */
208
            put_le32(pb, 0); /* initial frame */
209
            put_le32(pb, 1000); /* scale */
210
            put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
211
            put_le32(pb, 0); /* start */
212
            put_le32(pb, nb_frames); /* length, XXX: fill later */
213
            put_le32(pb, 1024 * 1024); /* suggested buffer size */
214
            put_le32(pb, 10000); /* quality */
215
            put_le32(pb, stream->width * stream->height * 3); /* sample size */
216
            put_le16(pb, 0);
217
            put_le16(pb, 0);
218
            put_le16(pb, stream->width);
219
            put_le16(pb, stream->height);
220
            break;
221
        case CODEC_TYPE_AUDIO:
222
            put_tag(pb, "auds");
223
            put_le32(pb, 0);
224
            put_le32(pb, 0); /* flags */
225
            put_le16(pb, 0); /* priority */
226
            put_le16(pb, 0); /* language */
227
            put_le32(pb, 0); /* initial frame */
228
            put_le32(pb, 1); /* scale */
229
            put_le32(pb, stream->bit_rate / 8); /* rate */
230
            put_le32(pb, 0); /* start */
231
            put_le32(pb, 0); /* length, XXX: filled later */
232
            put_le32(pb, 12 * 1024); /* suggested buffer size */
233
            put_le32(pb, -1); /* quality */
234
            put_le32(pb, 1); /* sample size */
235
            put_le32(pb, 0);
236
            put_le32(pb, 0);
237
            break;
238
        }
239
        end_tag(pb, strh);
240

    
241
        strf = start_tag(pb, "strf");
242
        switch(stream->codec_type) {
243
        case CODEC_TYPE_VIDEO:
244
            put_bmp_header(pb, stream);
245
            break;
246
        case CODEC_TYPE_AUDIO:
247
            put_wav_header(pb, stream);
248
            break;
249
        }
250
        end_tag(pb, strf);
251
        end_tag(pb, list2);
252
    }
253

    
254
    end_tag(pb, list1);
255
    
256
    avi->movi_list = start_tag(pb, "LIST");
257
    avi->first = NULL;
258
    avi->last = NULL;
259
    put_tag(pb, "movi");
260

    
261
    put_flush_packet(pb);
262

    
263
    return 0;
264
}
265

    
266
static int avi_write_packet(AVFormatContext *s, int stream_index,
267
                            UINT8 *buf, int size)
268
{
269
    AVIContext *avi = s->priv_data;
270
    ByteIOContext *pb = &s->pb;
271
    AVIIndex *idx;
272
    unsigned char tag[5];
273
    unsigned int flags;
274
    AVCodecContext *enc;
275
    
276
    enc = &s->streams[stream_index]->codec;
277

    
278
    tag[0] = '0';
279
    tag[1] = '0' + stream_index;
280
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
281
        tag[2] = 'd';
282
        tag[3] = 'c';
283
        flags = enc->key_frame ? 0x10 : 0x00;
284
    } else {
285
        tag[2] = 'w';
286
        tag[3] = 'b';
287
        flags = 0x10;
288
    }
289

    
290
    if (!url_is_streamed(&s->pb)) {
291
        idx = malloc(sizeof(AVIIndex));
292
        memcpy(idx->tag, tag, 4);
293
        idx->flags = flags;
294
        idx->pos = url_ftell(pb) - avi->movi_list;
295
        idx->len = size;
296
        idx->next = NULL;
297
        if (!avi->last)
298
            avi->first = idx;
299
        else
300
            avi->last->next = idx;
301
        avi->last = idx;
302
    }
303
    
304
    put_buffer(pb, tag, 4);
305
    put_le32(pb, size);
306
    put_buffer(pb, buf, size);
307
    if (size & 1)
308
        put_byte(pb, 0);
309

    
310
    put_flush_packet(pb);
311
    return 0;
312
}
313

    
314
static int avi_write_trailer(AVFormatContext *s)
315
{
316
    ByteIOContext *pb = &s->pb;
317
    AVIContext *avi = s->priv_data;
318
    offset_t file_size, idx_chunk;
319
    AVIIndex *idx;
320

    
321
    if (!url_is_streamed(&s->pb)) {
322
        end_tag(pb, avi->movi_list);
323

    
324
        idx_chunk = start_tag(pb, "idx1");
325
        idx = avi->first;
326
        while (idx != NULL) {
327
            put_buffer(pb, idx->tag, 4);
328
            put_le32(pb, idx->flags);
329
            put_le32(pb, idx->pos);
330
            put_le32(pb, idx->len);
331
            idx = idx->next;
332
        }
333
        end_tag(pb, idx_chunk);
334
        
335
        /* update file size */
336
        file_size = url_ftell(pb);
337
        url_fseek(pb, 4, SEEK_SET);
338
        put_le32(pb, (UINT32)(file_size - 8));
339
        url_fseek(pb, file_size, SEEK_SET);
340
    }
341
    put_flush_packet(pb);
342

    
343
    free(avi);
344
    return 0;
345
}
346

    
347
AVFormat avi_format = {
348
    "avi",
349
    "avi format",
350
    "video/x-msvideo",
351
    "avi",
352
    CODEC_ID_MP2,
353
    CODEC_ID_MSMPEG4,
354
    avi_write_header,
355
    avi_write_packet,
356
    avi_write_trailer,
357

    
358
    avi_read_header,
359
    avi_read_packet,
360
    avi_read_close,
361
};