Statistics
| Branch: | Revision:

ffmpeg / libav / avienc.c @ 9b030d9d

History | View | Annotate | Download (12.1 KB)

1
/*
2
 * AVI encoder.
3
 * Copyright (c) 2000 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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, frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
35
    int audio_strm_length[MAX_STREAMS];
36
    AVIIndex *first, *last;
37
} AVIContext;
38

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

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

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

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

    
74
unsigned int codec_get_tag(const CodecTag *tags, int id)
75
{
76
    while (tags->id != 0) {
77
        if (tags->id == id)
78
            return tags->tag;
79
        tags++;
80
    }
81
    return 0;
82
}
83

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

    
94
unsigned int codec_get_bmp_tag(int id)
95
{
96
    return codec_get_tag(codec_bmp_tags, id);
97
}
98

    
99
/* BITMAPINFOHEADER header */
100
void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, CodecTag *tags)
101
{
102
    put_le32(pb, 40); /* size */
103
    put_le32(pb, enc->width);
104
    put_le32(pb, enc->height);
105
    put_le16(pb, 1); /* planes */
106
    put_le16(pb, 24); /* depth */
107
    /* compression type */
108
    put_le32(pb, codec_get_tag(tags, enc->codec_id));
109
    put_le32(pb, enc->width * enc->height * 3);
110
    put_le32(pb, 0);
111
    put_le32(pb, 0);
112
    put_le32(pb, 0);
113
    put_le32(pb, 0);
114
}
115

    
116
void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
117
{
118
    switch(stream->codec_id) {
119
    case CODEC_ID_PCM_S16LE:
120
       *au_scale = *au_ssize = 2*stream->channels;
121
       *au_byterate = *au_ssize * stream->sample_rate;
122
        break;
123
    case CODEC_ID_PCM_U8:
124
    case CODEC_ID_PCM_ALAW:
125
    case CODEC_ID_PCM_MULAW:
126
        *au_scale = *au_ssize = stream->channels;
127
        *au_byterate = *au_ssize * stream->sample_rate;
128
        break;
129
    case CODEC_ID_MP2:
130
        *au_ssize = 1;
131
        *au_scale = 1;
132
        *au_byterate = stream->bit_rate / 8;
133
    case CODEC_ID_MP3LAME:
134
        *au_ssize = 1;
135
        *au_scale = 1;
136
        *au_byterate = stream->bit_rate / 8;    
137
    default:
138
        *au_ssize = 1;
139
        *au_scale = 1; 
140
        *au_byterate = stream->bit_rate / 8;
141
        break;
142
    }
143
}
144

    
145
static int avi_write_header(AVFormatContext *s)
146
{
147
    AVIContext *avi = s->priv_data;
148
    ByteIOContext *pb = &s->pb;
149
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
150
    AVCodecContext *stream, *video_enc;
151
    offset_t list1, list2, strh, strf;
152

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

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

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

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

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

    
203
        /* stream generic header */
204
        strh = start_tag(pb, "strh");
205
        switch(stream->codec_type) {
206
        case CODEC_TYPE_VIDEO:
207
            put_tag(pb, "vids");
208
            put_le32(pb, codec_get_bmp_tag(stream->codec_id));
209
            put_le32(pb, 0); /* flags */
210
            put_le16(pb, 0); /* priority */
211
            put_le16(pb, 0); /* language */
212
            put_le32(pb, 0); /* initial frame */
213
            put_le32(pb, 1000); /* scale */
214
            put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
215
            put_le32(pb, 0); /* start */
216
            avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
217
            put_le32(pb, nb_frames); /* length, XXX: fill later */
218
            put_le32(pb, 1024 * 1024); /* suggested buffer size */
219
            put_le32(pb, -1); /* 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, 1); /* tag */
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
            parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
234
            put_le32(pb, au_scale); /* scale */
235
            put_le32(pb, au_byterate); /* rate */
236
            put_le32(pb, 0); /* start */
237
            avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
238
            put_le32(pb, 0); /* length, XXX: filled later */
239
            put_le32(pb, 12 * 1024); /* suggested buffer size */
240
            put_le32(pb, -1); /* quality */
241
            put_le32(pb, au_ssize); /* sample size */
242
            put_le32(pb, 0);
243
            put_le32(pb, 0);
244
            break;
245
        default:
246
            abort();
247
        }
248
        end_tag(pb, strh);
249

    
250
        strf = start_tag(pb, "strf");
251
        switch(stream->codec_type) {
252
        case CODEC_TYPE_VIDEO:
253
            put_bmp_header(pb, stream, codec_bmp_tags);
254
            break;
255
        case CODEC_TYPE_AUDIO:
256
            if (put_wav_header(pb, stream) < 0) {
257
                av_free(avi);
258
                return -1;
259
            }
260
            break;
261
        default:
262
            abort();
263
        }
264
        end_tag(pb, strf);
265
        end_tag(pb, list2);
266
    }
267

    
268
    end_tag(pb, list1);
269
    
270
    avi->movi_list = start_tag(pb, "LIST");
271
    avi->first = NULL;
272
    avi->last = NULL;
273
    put_tag(pb, "movi");
274

    
275
    put_flush_packet(pb);
276

    
277
    return 0;
278
}
279

    
280
static int avi_write_packet(AVFormatContext *s, int stream_index,
281
                            UINT8 *buf, int size, int force_pts)
282
{
283
    AVIContext *avi = s->priv_data;
284
    ByteIOContext *pb = &s->pb;
285
    AVIIndex *idx;
286
    unsigned char tag[5];
287
    unsigned int flags;
288
    AVCodecContext *enc;
289
    
290
    enc = &s->streams[stream_index]->codec;
291

    
292
    tag[0] = '0';
293
    tag[1] = '0' + stream_index;
294
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
295
        tag[2] = 'd';
296
        tag[3] = 'c';
297
        flags = enc->key_frame ? 0x10 : 0x00;
298
    } else {
299
        tag[2] = 'w';
300
        tag[3] = 'b';
301
        flags = 0x10;
302
    }
303
    if (enc->codec_type == CODEC_TYPE_AUDIO) 
304
       avi->audio_strm_length[stream_index] += size;
305

    
306
    if (!url_is_streamed(&s->pb)) {
307
        idx = av_malloc(sizeof(AVIIndex));
308
        memcpy(idx->tag, tag, 4);
309
        idx->flags = flags;
310
        idx->pos = url_ftell(pb) - avi->movi_list;
311
        idx->len = size;
312
        idx->next = NULL;
313
        if (!avi->last)
314
            avi->first = idx;
315
        else
316
            avi->last->next = idx;
317
        avi->last = idx;
318
    }
319
    
320
    put_buffer(pb, tag, 4);
321
    put_le32(pb, size);
322
    put_buffer(pb, buf, size);
323
    if (size & 1)
324
        put_byte(pb, 0);
325

    
326
    put_flush_packet(pb);
327
    return 0;
328
}
329

    
330
static int avi_write_trailer(AVFormatContext *s)
331
{
332
    ByteIOContext *pb = &s->pb;
333
    AVIContext *avi = s->priv_data;
334
    offset_t file_size, idx_chunk;
335
    int n, nb_frames, au_byterate, au_ssize, au_scale;
336
    AVCodecContext *stream;
337
    AVIIndex *idx;
338

    
339
    if (!url_is_streamed(&s->pb)) {
340
        end_tag(pb, avi->movi_list);
341

    
342
        idx_chunk = start_tag(pb, "idx1");
343
        idx = avi->first;
344
        while (idx != NULL) {
345
            put_buffer(pb, idx->tag, 4);
346
            put_le32(pb, idx->flags);
347
            put_le32(pb, idx->pos);
348
            put_le32(pb, idx->len);
349
            idx = idx->next;
350
        }
351
        end_tag(pb, idx_chunk);
352
        
353
        /* update file size */
354
        file_size = url_ftell(pb);
355
        url_fseek(pb, 4, SEEK_SET);
356
        put_le32(pb, (UINT32)(file_size - 8));
357

    
358
        /* Fill in frame/sample counters */
359
        nb_frames = 0;
360
        for(n=0;n<s->nb_streams;n++) {
361
            if (avi->frames_hdr_strm[n] != 0) {
362
                stream = &s->streams[n]->codec;
363
                url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
364
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
365
                    put_le32(pb, stream->frame_number); 
366
                    if (nb_frames < stream->frame_number)
367
                        nb_frames = stream->frame_number;
368
                } else {
369
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
370
                        put_le32(pb, stream->frame_number);
371
                        nb_frames += stream->frame_number;
372
                    } else {
373
                        parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
374
                        put_le32(pb, avi->audio_strm_length[n] / au_ssize);
375
                    }
376
                }
377
            }
378
       }
379
       if (avi->frames_hdr_all != 0) {
380
           url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
381
           put_le32(pb, nb_frames); 
382
       }
383
        url_fseek(pb, file_size, SEEK_SET);
384
    }
385
    put_flush_packet(pb);
386
    return 0;
387
}
388

    
389
static AVOutputFormat avi_oformat = {
390
    "avi",
391
    "avi format",
392
    "video/x-msvideo",
393
    "avi",
394
    sizeof(AVIContext),
395
    CODEC_ID_MP2,
396
    CODEC_ID_MSMPEG4,
397
    avi_write_header,
398
    avi_write_packet,
399
    avi_write_trailer,
400
};
401

    
402
int avienc_init(void)
403
{
404
    av_register_output_format(&avi_oformat);
405
    return 0;
406
}