Statistics
| Branch: | Revision:

ffmpeg / libav / avienc.c @ 41568e4a

History | View | Annotate | Download (13.3 KB)

1 de6d9b64 Fabrice Bellard
/*
2
 * AVI encoder.
3 19720f15 Fabrice Bellard
 * Copyright (c) 2000 Fabrice Bellard.
4 de6d9b64 Fabrice Bellard
 *
5 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 *
10 19720f15 Fabrice Bellard
 * This library is distributed in the hope that it will be useful,
11 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14 de6d9b64 Fabrice Bellard
 *
15 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
 */
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 86bec9a1 Juanjo
    offset_t movi_list, frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
35
    int audio_strm_length[MAX_STREAMS];
36 de6d9b64 Fabrice Bellard
    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 8be1c656 Fabrice Bellard
    put_le32(pb, (UINT32)(pos - start));
53 de6d9b64 Fabrice Bellard
    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 95c79a24 Juanjo
    { CODEC_ID_H263, MKTAG('H', '2', '6', '3') },
60
    { CODEC_ID_H263P, MKTAG('H', '2', '6', '3') },
61 de6d9b64 Fabrice Bellard
    { CODEC_ID_H263I, MKTAG('I', '2', '6', '3') }, /* intel h263 */
62
    { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
63 2727c35e Philip Gladstone
    { CODEC_ID_MPEG4, MKTAG('D', 'I', 'V', 'X'), invalid_asf: 1 },
64
    { CODEC_ID_MPEG4, MKTAG('d', 'i', 'v', 'x'), invalid_asf: 1},
65
    { CODEC_ID_MPEG4, MKTAG('D', 'X', '5', '0'), invalid_asf: 1 },
66
    { CODEC_ID_MPEG4, MKTAG('X', 'V', 'I', 'D'), invalid_asf: 1 },
67
    { CODEC_ID_MPEG4, MKTAG('x', 'v', 'i', 'd'), invalid_asf: 1 },
68
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 's'), invalid_asf: 1 },
69 390dffc5 Michael Niedermayer
    { CODEC_ID_MPEG4, MKTAG('M', 'P', '4', 'S') },
70
    { CODEC_ID_MPEG4, MKTAG('M', '4', 'S', '2') },
71
    { CODEC_ID_MPEG4, MKTAG('m', '4', 's', '2') },
72 58f26ba9 Fabrice Bellard
    { CODEC_ID_MPEG4, MKTAG(0x04, 0, 0, 0) }, /* some broken avi use this */
73 2727c35e Philip Gladstone
    { CODEC_ID_MSMPEG4V3, MKTAG('D', 'I', 'V', '3'), invalid_asf: 1 }, /* default signature when using MSMPEG4 */
74 bb3debab Gert Vervoort
    { CODEC_ID_MSMPEG4V3, MKTAG('M', 'P', '4', '3') }, 
75
    { CODEC_ID_MSMPEG4V2, MKTAG('M', 'P', '4', '2') }, 
76 9b030d9d Michael Niedermayer
    { CODEC_ID_MSMPEG4V1, MKTAG('M', 'P', 'G', '4') }, 
77
    { CODEC_ID_WMV1, MKTAG('W', 'M', 'V', '1') }, 
78 020fcc94 Fabrice Bellard
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'l') }, 
79
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 's', 'd') }, 
80 f20dca40 Fabrice Bellard
    { CODEC_ID_DVVIDEO, MKTAG('D', 'V', 'S', 'D') }, 
81 020fcc94 Fabrice Bellard
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'h', 'd') }, 
82 1cbc2890 Fabrice Bellard
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '1') }, 
83
    { CODEC_ID_MPEG1VIDEO, MKTAG('m', 'p', 'g', '2') }, 
84
    { CODEC_ID_MPEG1VIDEO, MKTAG('P', 'I', 'M', '1') }, 
85 bb15f7fd Alex Beregszaszi
    { CODEC_ID_MJPEG, MKTAG('M', 'J', 'P', 'G') },
86 de6d9b64 Fabrice Bellard
    { 0, 0 },
87
};
88
89 a266644f Zdenek Kabelac
unsigned int codec_get_tag(const CodecTag *tags, int id)
90 de6d9b64 Fabrice Bellard
{
91
    while (tags->id != 0) {
92
        if (tags->id == id)
93
            return tags->tag;
94
        tags++;
95
    }
96
    return 0;
97
}
98
99 2727c35e Philip Gladstone
static unsigned int codec_get_asf_tag(const CodecTag *tags, int id)
100
{
101
    while (tags->id != 0) {
102
        if (!tags->invalid_asf && tags->id == id)
103
            return tags->tag;
104
        tags++;
105
    }
106
    return 0;
107
}
108
109 a266644f Zdenek Kabelac
int codec_get_id(const CodecTag *tags, unsigned int tag)
110 de6d9b64 Fabrice Bellard
{
111
    while (tags->id != 0) {
112
        if (tags->tag == tag)
113
            return tags->id;
114
        tags++;
115
    }
116
    return 0;
117
}
118
119
unsigned int codec_get_bmp_tag(int id)
120
{
121
    return codec_get_tag(codec_bmp_tags, id);
122
}
123
124
/* BITMAPINFOHEADER header */
125 2727c35e Philip Gladstone
void put_bmp_header(ByteIOContext *pb, AVCodecContext *enc, CodecTag *tags, int for_asf)
126 de6d9b64 Fabrice Bellard
{
127
    put_le32(pb, 40); /* size */
128
    put_le32(pb, enc->width);
129
    put_le32(pb, enc->height);
130
    put_le16(pb, 1); /* planes */
131
    put_le16(pb, 24); /* depth */
132
    /* compression type */
133 2727c35e Philip Gladstone
    put_le32(pb, for_asf ? codec_get_asf_tag(tags, enc->codec_id) : codec_get_tag(tags, enc->codec_id));
134 de6d9b64 Fabrice Bellard
    put_le32(pb, enc->width * enc->height * 3);
135
    put_le32(pb, 0);
136
    put_le32(pb, 0);
137
    put_le32(pb, 0);
138
    put_le32(pb, 0);
139
}
140
141 86bec9a1 Juanjo
void parse_specific_params(AVCodecContext *stream, int *au_byterate, int *au_ssize, int *au_scale)
142
{
143
    switch(stream->codec_id) {
144
    case CODEC_ID_PCM_S16LE:
145
       *au_scale = *au_ssize = 2*stream->channels;
146
       *au_byterate = *au_ssize * stream->sample_rate;
147
        break;
148
    case CODEC_ID_PCM_U8:
149
    case CODEC_ID_PCM_ALAW:
150
    case CODEC_ID_PCM_MULAW:
151
        *au_scale = *au_ssize = stream->channels;
152
        *au_byterate = *au_ssize * stream->sample_rate;
153
        break;
154
    case CODEC_ID_MP2:
155
        *au_ssize = 1;
156
        *au_scale = 1;
157
        *au_byterate = stream->bit_rate / 8;
158 5798368b Juanjo
    case CODEC_ID_MP3LAME:
159
        *au_ssize = 1;
160
        *au_scale = 1;
161
        *au_byterate = stream->bit_rate / 8;    
162 86bec9a1 Juanjo
    default:
163
        *au_ssize = 1;
164
        *au_scale = 1; 
165
        *au_byterate = stream->bit_rate / 8;
166
        break;
167
    }
168
}
169
170 de6d9b64 Fabrice Bellard
static int avi_write_header(AVFormatContext *s)
171
{
172 c9a65ca8 Fabrice Bellard
    AVIContext *avi = s->priv_data;
173 de6d9b64 Fabrice Bellard
    ByteIOContext *pb = &s->pb;
174 86bec9a1 Juanjo
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
175 de6d9b64 Fabrice Bellard
    AVCodecContext *stream, *video_enc;
176
    offset_t list1, list2, strh, strf;
177
178
    put_tag(pb, "RIFF");
179
    put_le32(pb, 0); /* file length */
180
    put_tag(pb, "AVI ");
181
182
    /* header list */
183
    list1 = start_tag(pb, "LIST");
184
    put_tag(pb, "hdrl");
185
186
    /* avi header */
187
    put_tag(pb, "avih");
188
    put_le32(pb, 14 * 4);
189
    bitrate = 0;
190
191
    video_enc = NULL;
192
    for(n=0;n<s->nb_streams;n++) {
193
        stream = &s->streams[n]->codec;
194
        bitrate += stream->bit_rate;
195
        if (stream->codec_type == CODEC_TYPE_VIDEO)
196
            video_enc = stream;
197
    }
198
    
199
    if (!video_enc) {
200 1ea4f593 Fabrice Bellard
        av_free(avi);
201 de6d9b64 Fabrice Bellard
        return -1;
202
    }
203
    nb_frames = 0;
204
205 8be1c656 Fabrice Bellard
    put_le32(pb, (UINT32)(INT64_C(1000000) * FRAME_RATE_BASE / video_enc->frame_rate));
206 de6d9b64 Fabrice Bellard
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
207
    put_le32(pb, 0); /* padding */
208
    put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
209 86bec9a1 Juanjo
    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
210 de6d9b64 Fabrice Bellard
    put_le32(pb, nb_frames); /* nb frames, filled later */
211
    put_le32(pb, 0); /* initial frame */
212
    put_le32(pb, s->nb_streams); /* nb streams */
213
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
214
    put_le32(pb, video_enc->width);
215
    put_le32(pb, video_enc->height);
216
    put_le32(pb, 0); /* reserved */
217
    put_le32(pb, 0); /* reserved */
218
    put_le32(pb, 0); /* reserved */
219
    put_le32(pb, 0); /* reserved */
220
    
221
    /* stream list */
222
    for(i=0;i<n;i++) {
223
        list2 = start_tag(pb, "LIST");
224
        put_tag(pb, "strl");
225
    
226
        stream = &s->streams[i]->codec;
227
228
        /* stream generic header */
229
        strh = start_tag(pb, "strh");
230
        switch(stream->codec_type) {
231
        case CODEC_TYPE_VIDEO:
232
            put_tag(pb, "vids");
233
            put_le32(pb, codec_get_bmp_tag(stream->codec_id));
234
            put_le32(pb, 0); /* flags */
235
            put_le16(pb, 0); /* priority */
236
            put_le16(pb, 0); /* language */
237
            put_le32(pb, 0); /* initial frame */
238
            put_le32(pb, 1000); /* scale */
239
            put_le32(pb, (1000 * stream->frame_rate) / FRAME_RATE_BASE); /* rate */
240
            put_le32(pb, 0); /* start */
241 86bec9a1 Juanjo
            avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
242 de6d9b64 Fabrice Bellard
            put_le32(pb, nb_frames); /* length, XXX: fill later */
243
            put_le32(pb, 1024 * 1024); /* suggested buffer size */
244 86bec9a1 Juanjo
            put_le32(pb, -1); /* quality */
245 de6d9b64 Fabrice Bellard
            put_le32(pb, stream->width * stream->height * 3); /* sample size */
246
            put_le16(pb, 0);
247
            put_le16(pb, 0);
248
            put_le16(pb, stream->width);
249
            put_le16(pb, stream->height);
250
            break;
251
        case CODEC_TYPE_AUDIO:
252
            put_tag(pb, "auds");
253 86bec9a1 Juanjo
            put_le32(pb, 1); /* tag */
254 de6d9b64 Fabrice Bellard
            put_le32(pb, 0); /* flags */
255
            put_le16(pb, 0); /* priority */
256
            put_le16(pb, 0); /* language */
257
            put_le32(pb, 0); /* initial frame */
258 86bec9a1 Juanjo
            parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
259
            put_le32(pb, au_scale); /* scale */
260
            put_le32(pb, au_byterate); /* rate */
261 de6d9b64 Fabrice Bellard
            put_le32(pb, 0); /* start */
262 86bec9a1 Juanjo
            avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
263 de6d9b64 Fabrice Bellard
            put_le32(pb, 0); /* length, XXX: filled later */
264
            put_le32(pb, 12 * 1024); /* suggested buffer size */
265
            put_le32(pb, -1); /* quality */
266 86bec9a1 Juanjo
            put_le32(pb, au_ssize); /* sample size */
267 de6d9b64 Fabrice Bellard
            put_le32(pb, 0);
268
            put_le32(pb, 0);
269
            break;
270 53cafac0 Philip Gladstone
        default:
271 42343f7e Philip Gladstone
            av_abort();
272 de6d9b64 Fabrice Bellard
        }
273
        end_tag(pb, strh);
274
275
        strf = start_tag(pb, "strf");
276
        switch(stream->codec_type) {
277
        case CODEC_TYPE_VIDEO:
278 2727c35e Philip Gladstone
            put_bmp_header(pb, stream, codec_bmp_tags, 0);
279 de6d9b64 Fabrice Bellard
            break;
280
        case CODEC_TYPE_AUDIO:
281 46a3d068 Fabrice Bellard
            if (put_wav_header(pb, stream) < 0) {
282 1ea4f593 Fabrice Bellard
                av_free(avi);
283 46a3d068 Fabrice Bellard
                return -1;
284
            }
285 de6d9b64 Fabrice Bellard
            break;
286 53cafac0 Philip Gladstone
        default:
287 42343f7e Philip Gladstone
            av_abort();
288 de6d9b64 Fabrice Bellard
        }
289
        end_tag(pb, strf);
290
        end_tag(pb, list2);
291
    }
292
293
    end_tag(pb, list1);
294
    
295
    avi->movi_list = start_tag(pb, "LIST");
296
    avi->first = NULL;
297
    avi->last = NULL;
298
    put_tag(pb, "movi");
299
300
    put_flush_packet(pb);
301
302
    return 0;
303
}
304
305
static int avi_write_packet(AVFormatContext *s, int stream_index,
306 10bb7023 Juanjo
                            UINT8 *buf, int size, int force_pts)
307 de6d9b64 Fabrice Bellard
{
308
    AVIContext *avi = s->priv_data;
309
    ByteIOContext *pb = &s->pb;
310
    AVIIndex *idx;
311
    unsigned char tag[5];
312
    unsigned int flags;
313
    AVCodecContext *enc;
314
    
315
    enc = &s->streams[stream_index]->codec;
316
317
    tag[0] = '0';
318
    tag[1] = '0' + stream_index;
319
    if (enc->codec_type == CODEC_TYPE_VIDEO) {
320
        tag[2] = 'd';
321
        tag[3] = 'c';
322
        flags = enc->key_frame ? 0x10 : 0x00;
323
    } else {
324
        tag[2] = 'w';
325
        tag[3] = 'b';
326
        flags = 0x10;
327
    }
328 86bec9a1 Juanjo
    if (enc->codec_type == CODEC_TYPE_AUDIO) 
329
       avi->audio_strm_length[stream_index] += size;
330 de6d9b64 Fabrice Bellard
331
    if (!url_is_streamed(&s->pb)) {
332 1ea4f593 Fabrice Bellard
        idx = av_malloc(sizeof(AVIIndex));
333 de6d9b64 Fabrice Bellard
        memcpy(idx->tag, tag, 4);
334
        idx->flags = flags;
335
        idx->pos = url_ftell(pb) - avi->movi_list;
336
        idx->len = size;
337
        idx->next = NULL;
338
        if (!avi->last)
339
            avi->first = idx;
340
        else
341
            avi->last->next = idx;
342
        avi->last = idx;
343
    }
344
    
345
    put_buffer(pb, tag, 4);
346
    put_le32(pb, size);
347
    put_buffer(pb, buf, size);
348
    if (size & 1)
349
        put_byte(pb, 0);
350
351
    put_flush_packet(pb);
352
    return 0;
353
}
354
355
static int avi_write_trailer(AVFormatContext *s)
356
{
357
    ByteIOContext *pb = &s->pb;
358
    AVIContext *avi = s->priv_data;
359
    offset_t file_size, idx_chunk;
360 86bec9a1 Juanjo
    int n, nb_frames, au_byterate, au_ssize, au_scale;
361
    AVCodecContext *stream;
362 de6d9b64 Fabrice Bellard
    AVIIndex *idx;
363
364
    if (!url_is_streamed(&s->pb)) {
365
        end_tag(pb, avi->movi_list);
366
367
        idx_chunk = start_tag(pb, "idx1");
368
        idx = avi->first;
369
        while (idx != NULL) {
370
            put_buffer(pb, idx->tag, 4);
371
            put_le32(pb, idx->flags);
372
            put_le32(pb, idx->pos);
373
            put_le32(pb, idx->len);
374
            idx = idx->next;
375
        }
376
        end_tag(pb, idx_chunk);
377
        
378
        /* update file size */
379
        file_size = url_ftell(pb);
380
        url_fseek(pb, 4, SEEK_SET);
381 8be1c656 Fabrice Bellard
        put_le32(pb, (UINT32)(file_size - 8));
382 86bec9a1 Juanjo
383
        /* Fill in frame/sample counters */
384
        nb_frames = 0;
385
        for(n=0;n<s->nb_streams;n++) {
386
            if (avi->frames_hdr_strm[n] != 0) {
387
                stream = &s->streams[n]->codec;
388
                url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
389
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
390
                    put_le32(pb, stream->frame_number); 
391
                    if (nb_frames < stream->frame_number)
392
                        nb_frames = stream->frame_number;
393
                } else {
394 5798368b Juanjo
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3LAME) {
395 86bec9a1 Juanjo
                        put_le32(pb, stream->frame_number);
396
                        nb_frames += stream->frame_number;
397
                    } else {
398
                        parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
399
                        put_le32(pb, avi->audio_strm_length[n] / au_ssize);
400
                    }
401
                }
402
            }
403
       }
404
       if (avi->frames_hdr_all != 0) {
405
           url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
406
           put_le32(pb, nb_frames); 
407
       }
408 de6d9b64 Fabrice Bellard
        url_fseek(pb, file_size, SEEK_SET);
409
    }
410
    put_flush_packet(pb);
411
    return 0;
412
}
413
414 c9a65ca8 Fabrice Bellard
static AVOutputFormat avi_oformat = {
415 de6d9b64 Fabrice Bellard
    "avi",
416
    "avi format",
417
    "video/x-msvideo",
418
    "avi",
419 c9a65ca8 Fabrice Bellard
    sizeof(AVIContext),
420 de6d9b64 Fabrice Bellard
    CODEC_ID_MP2,
421
    CODEC_ID_MSMPEG4,
422
    avi_write_header,
423
    avi_write_packet,
424
    avi_write_trailer,
425
};
426 c9a65ca8 Fabrice Bellard
427
int avienc_init(void)
428
{
429
    av_register_output_format(&avi_oformat);
430
    return 0;
431
}