Statistics
| Branch: | Revision:

ffmpeg / libav / avienc.c @ e643b32b

History | View | Annotate | Download (9.72 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 <stdlib.h>
20
#include <stdio.h>
21
#include <netinet/in.h>
22
#include <string.h>
23

    
24
#include "avformat.h"
25
#include "avi.h"
26

    
27
/*
28
 * TODO: 
29
 *  - fill all fields if non streamed (nb_frames for example)
30
 */
31

    
32
typedef struct AVIIndex {
33
    unsigned char tag[4];
34
    unsigned int flags, pos, len;
35
    struct AVIIndex *next;
36
} AVIIndex;
37

    
38
typedef struct {
39
    offset_t movi_list;
40
    AVIIndex *first, *last;
41
} AVIContext;
42

    
43
offset_t start_tag(ByteIOContext *pb, char *tag)
44
{
45
    put_tag(pb, tag);
46
    put_le32(pb, 0);
47
    return url_ftell(pb);
48
}
49

    
50
void end_tag(ByteIOContext *pb, offset_t start)
51
{
52
    offset_t pos;
53

    
54
    pos = url_ftell(pb);
55
    url_fseek(pb, start - 4, SEEK_SET);
56
    put_le32(pb, pos - start);
57
    url_fseek(pb, pos, SEEK_SET);
58
}
59

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

    
74
CodecTag codec_wav_tags[] = {
75
    { CODEC_ID_MP2, 0x55 },
76
    { CODEC_ID_MP2, 0x50 },
77
    { CODEC_ID_AC3, 0x2000 },
78
    { CODEC_ID_PCM, 0x01 },
79
    { 0, 0 },
80
};
81

    
82

    
83
unsigned int codec_get_tag(CodecTag *tags, int id)
84
{
85
    while (tags->id != 0) {
86
        if (tags->id == id)
87
            return tags->tag;
88
        tags++;
89
    }
90
    return 0;
91
}
92

    
93
int codec_get_id(CodecTag *tags, unsigned int tag)
94
{
95
    while (tags->id != 0) {
96
        if (tags->tag == tag)
97
            return tags->id;
98
        tags++;
99
    }
100
    return 0;
101
}
102

    
103
unsigned int codec_get_bmp_tag(int id)
104
{
105
    return codec_get_tag(codec_bmp_tags, id);
106
}
107

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

    
125
/* WAVEFORMATEX header */
126
void put_wav_header(ByteIOContext *pb, AVCodecContext *enc)
127
{
128
    int tag;
129

    
130
    tag = codec_get_tag(codec_wav_tags, enc->codec_id);
131

    
132
    put_le16(pb, tag); 
133
    put_le16(pb, enc->channels);
134
    put_le32(pb, enc->sample_rate);
135
    put_le32(pb, enc->bit_rate / 8);
136
    put_le16(pb, 1); /* block align */
137
    put_le16(pb, 16); /* bits per sample */
138
    put_le16(pb, 0); /* wav_extra_size */
139
}
140

    
141
static int avi_write_header(AVFormatContext *s)
142
{
143
    AVIContext *avi;
144
    ByteIOContext *pb = &s->pb;
145
    int bitrate, n, i, nb_frames;
146
    AVCodecContext *stream, *video_enc;
147
    offset_t list1, list2, strh, strf;
148

    
149
    avi = malloc(sizeof(AVIContext));
150
    if (!avi)
151
        return -1;
152
    memset(avi, 0, sizeof(AVIContext));
153
    s->priv_data = avi;
154

    
155
    put_tag(pb, "RIFF");
156
    put_le32(pb, 0); /* file length */
157
    put_tag(pb, "AVI ");
158

    
159
    /* header list */
160
    list1 = start_tag(pb, "LIST");
161
    put_tag(pb, "hdrl");
162

    
163
    /* avi header */
164
    put_tag(pb, "avih");
165
    put_le32(pb, 14 * 4);
166
    bitrate = 0;
167

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

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

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

    
246
        strf = start_tag(pb, "strf");
247
        switch(stream->codec_type) {
248
        case CODEC_TYPE_VIDEO:
249
            put_bmp_header(pb, stream);
250
            break;
251
        case CODEC_TYPE_AUDIO:
252
            put_wav_header(pb, stream);
253
            break;
254
        }
255
        end_tag(pb, strf);
256
        end_tag(pb, list2);
257
    }
258

    
259
    end_tag(pb, list1);
260
    
261
    avi->movi_list = start_tag(pb, "LIST");
262
    avi->first = NULL;
263
    avi->last = NULL;
264
    put_tag(pb, "movi");
265

    
266
    put_flush_packet(pb);
267

    
268
    return 0;
269
}
270

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

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

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

    
315
    put_flush_packet(pb);
316
    return 0;
317
}
318

    
319
static int avi_write_trailer(AVFormatContext *s)
320
{
321
    ByteIOContext *pb = &s->pb;
322
    AVIContext *avi = s->priv_data;
323
    offset_t file_size, idx_chunk;
324
    AVIIndex *idx;
325

    
326
    if (!url_is_streamed(&s->pb)) {
327
        end_tag(pb, avi->movi_list);
328

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

    
348
    free(avi);
349
    return 0;
350
}
351

    
352
AVFormat avi_format = {
353
    "avi",
354
    "avi format",
355
    "video/x-msvideo",
356
    "avi",
357
    CODEC_ID_MP2,
358
    CODEC_ID_MSMPEG4,
359
    avi_write_header,
360
    avi_write_packet,
361
    avi_write_trailer,
362

    
363
    avi_read_header,
364
    avi_read_packet,
365
    avi_read_close,
366
};