Statistics
| Branch: | Revision:

ffmpeg / libavformat / avienc.c @ 04d2540c

History | View | Annotate | Download (20.7 KB)

1 de6d9b64 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * AVI muxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2000 Fabrice Bellard
4 de6d9b64 Fabrice Bellard
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 19720f15 Fabrice Bellard
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 de6d9b64 Fabrice Bellard
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 de6d9b64 Fabrice Bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 19720f15 Fabrice Bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16 de6d9b64 Fabrice Bellard
 *
17 19720f15 Fabrice Bellard
 * You should have received a copy of the GNU Lesser General Public
18 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 de6d9b64 Fabrice Bellard
 */
21
#include "avformat.h"
22
#include "avi.h"
23 9d9f4119 Måns Rullgård
#include "riff.h"
24 04d2540c Anton Khirnov
#include "libavutil/intreadwrite.h"
25 de6d9b64 Fabrice Bellard
26
/*
27 115329f1 Diego Biurrun
 * TODO:
28 de6d9b64 Fabrice Bellard
 *  - fill all fields if non streamed (nb_frames for example)
29
 */
30
31 e738cee9 Roman Shaposhnik
typedef struct AVIIentry {
32 de6d9b64 Fabrice Bellard
    unsigned int flags, pos, len;
33 e738cee9 Roman Shaposhnik
} AVIIentry;
34
35
#define AVI_INDEX_CLUSTER_SIZE 16384
36
37
typedef struct AVIIndex {
38 bc5c918e Diego Biurrun
    int64_t     indx_start;
39 e738cee9 Roman Shaposhnik
    int         entry;
40
    int         ents_allocated;
41
    AVIIentry** cluster;
42 de6d9b64 Fabrice Bellard
} AVIIndex;
43
44
typedef struct {
45 bc5c918e Diego Biurrun
    int64_t riff_start, movi_list, odml_list;
46 b4f0e04c Michael Niedermayer
    int64_t frames_hdr_all;
47 e738cee9 Roman Shaposhnik
    int riff_id;
48 de6d9b64 Fabrice Bellard
} AVIContext;
49
50 b4f0e04c Michael Niedermayer
typedef struct  {
51
    int64_t frames_hdr_strm;
52
    int audio_strm_length;
53
    int packet_count;
54 12f1b1fe Michael Niedermayer
    int entry;
55 b4f0e04c Michael Niedermayer
56
    AVIIndex indexes;
57
} AVIStream ;
58
59 115329f1 Diego Biurrun
static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
60 e738cee9 Roman Shaposhnik
{
61
    int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
62
    int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
63
    return &idx->cluster[cl][id];
64
}
65
66 b4f0e04c Michael Niedermayer
static int64_t avi_start_new_riff(AVFormatContext *s, ByteIOContext *pb,
67 bc5c918e Diego Biurrun
                                  const char* riff_tag, const char* list_tag)
68 ce9fce63 Roman Shaposhnik
{
69 b4f0e04c Michael Niedermayer
    AVIContext *avi= s->priv_data;
70 bc5c918e Diego Biurrun
    int64_t loff;
71 e738cee9 Roman Shaposhnik
    int i;
72 115329f1 Diego Biurrun
73 e738cee9 Roman Shaposhnik
    avi->riff_id++;
74 b4f0e04c Michael Niedermayer
    for (i=0; i<s->nb_streams; i++){
75
        AVIStream *avist= s->streams[i]->priv_data;
76
        avist->indexes.entry = 0;
77
    }
78 115329f1 Diego Biurrun
79 1a40491e Daniel Verkamp
    avi->riff_start = ff_start_tag(pb, "RIFF");
80 ce9fce63 Roman Shaposhnik
    put_tag(pb, riff_tag);
81 1a40491e Daniel Verkamp
    loff = ff_start_tag(pb, "LIST");
82 ce9fce63 Roman Shaposhnik
    put_tag(pb, list_tag);
83
    return loff;
84
}
85
86 191e8ca7 Måns Rullgård
static char* avi_stream2fourcc(char* tag, int index, enum CodecType type)
87 e738cee9 Roman Shaposhnik
{
88
    tag[0] = '0';
89
    tag[1] = '0' + index;
90
    if (type == CODEC_TYPE_VIDEO) {
91
        tag[2] = 'd';
92
        tag[3] = 'c';
93 2a04d6e7 Reimar Döffinger
    } else if (type == CODEC_TYPE_SUBTITLE) {
94
        // note: this is not an official code
95
        tag[2] = 's';
96
        tag[3] = 'b';
97 e738cee9 Roman Shaposhnik
    } else {
98
        tag[2] = 'w';
99
        tag[3] = 'b';
100
    }
101
    tag[4] = '\0';
102
    return tag;
103
}
104
105 57060f89 David Conrad
static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
106
{
107
    int len = strlen(str);
108
    if (len > 0) {
109
        len++;
110
        put_tag(pb, tag);
111
        put_le32(pb, len);
112
        put_strz(pb, str);
113
        if (len & 1)
114
            put_byte(pb, 0);
115
    }
116
}
117
118 5bce834e Brian Brice
static int avi_write_counters(AVFormatContext* s, int riff_id)
119
{
120 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
121 5bce834e Brian Brice
    AVIContext *avi = s->priv_data;
122
    int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
123 bc5c918e Diego Biurrun
    int64_t file_size;
124 5bce834e Brian Brice
    AVCodecContext* stream;
125
126
    file_size = url_ftell(pb);
127
    for(n = 0; n < s->nb_streams; n++) {
128 b4f0e04c Michael Niedermayer
        AVIStream *avist= s->streams[n]->priv_data;
129
130
        assert(avist->frames_hdr_strm);
131 5bce834e Brian Brice
        stream = s->streams[n]->codec;
132 b4f0e04c Michael Niedermayer
        url_fseek(pb, avist->frames_hdr_strm, SEEK_SET);
133 5bce834e Brian Brice
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
134
        if(au_ssize == 0) {
135 b4f0e04c Michael Niedermayer
            put_le32(pb, avist->packet_count);
136 5bce834e Brian Brice
        } else {
137 b4f0e04c Michael Niedermayer
            put_le32(pb, avist->audio_strm_length / au_ssize);
138 5bce834e Brian Brice
        }
139
        if(stream->codec_type == CODEC_TYPE_VIDEO)
140 b4f0e04c Michael Niedermayer
            nb_frames = FFMAX(nb_frames, avist->packet_count);
141 5bce834e Brian Brice
    }
142
    if(riff_id == 1) {
143
        assert(avi->frames_hdr_all);
144
        url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
145
        put_le32(pb, nb_frames);
146
    }
147
    url_fseek(pb, file_size, SEEK_SET);
148
149
    return 0;
150
}
151
152 de6d9b64 Fabrice Bellard
static int avi_write_header(AVFormatContext *s)
153
{
154 c9a65ca8 Fabrice Bellard
    AVIContext *avi = s->priv_data;
155 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
156 86bec9a1 Juanjo
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
157 de6d9b64 Fabrice Bellard
    AVCodecContext *stream, *video_enc;
158 bc5c918e Diego Biurrun
    int64_t list1, list2, strh, strf;
159 04d2540c Anton Khirnov
    AVMetadataTag *t = NULL;
160 de6d9b64 Fabrice Bellard
161 b4f0e04c Michael Niedermayer
    for(n=0;n<s->nb_streams;n++) {
162
        s->streams[n]->priv_data= av_mallocz(sizeof(AVIStream));
163
        if(!s->streams[n]->priv_data)
164
            return AVERROR(ENOMEM);
165
    }
166
167 de6d9b64 Fabrice Bellard
    /* header list */
168 e738cee9 Roman Shaposhnik
    avi->riff_id = 0;
169 b4f0e04c Michael Niedermayer
    list1 = avi_start_new_riff(s, pb, "AVI ", "hdrl");
170 de6d9b64 Fabrice Bellard
171
    /* avi header */
172
    put_tag(pb, "avih");
173
    put_le32(pb, 14 * 4);
174
    bitrate = 0;
175
176
    video_enc = NULL;
177
    for(n=0;n<s->nb_streams;n++) {
178 01f4895c Michael Niedermayer
        stream = s->streams[n]->codec;
179 de6d9b64 Fabrice Bellard
        bitrate += stream->bit_rate;
180
        if (stream->codec_type == CODEC_TYPE_VIDEO)
181
            video_enc = stream;
182
    }
183 115329f1 Diego Biurrun
184 de6d9b64 Fabrice Bellard
    nb_frames = 0;
185
186 850742d7 Andriy Rysin
    if(video_enc){
187 8da9266c Måns Rullgård
        put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
188 850742d7 Andriy Rysin
    } else {
189 bb270c08 Diego Biurrun
        put_le32(pb, 0);
190 850742d7 Andriy Rysin
    }
191 de6d9b64 Fabrice Bellard
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
192
    put_le32(pb, 0); /* padding */
193 a2a2668d Alex Beregszaszi
    if (url_is_streamed(pb))
194 bb270c08 Diego Biurrun
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
195 a2a2668d Alex Beregszaszi
    else
196 bb270c08 Diego Biurrun
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
197 86bec9a1 Juanjo
    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
198 de6d9b64 Fabrice Bellard
    put_le32(pb, nb_frames); /* nb frames, filled later */
199
    put_le32(pb, 0); /* initial frame */
200
    put_le32(pb, s->nb_streams); /* nb streams */
201
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
202 115329f1 Diego Biurrun
    if(video_enc){
203 98740f5a Michael Niedermayer
        put_le32(pb, video_enc->width);
204
        put_le32(pb, video_enc->height);
205 850742d7 Andriy Rysin
    } else {
206 bb270c08 Diego Biurrun
        put_le32(pb, 0);
207
        put_le32(pb, 0);
208 115329f1 Diego Biurrun
    }
209 de6d9b64 Fabrice Bellard
    put_le32(pb, 0); /* reserved */
210
    put_le32(pb, 0); /* reserved */
211
    put_le32(pb, 0); /* reserved */
212
    put_le32(pb, 0); /* reserved */
213 115329f1 Diego Biurrun
214 de6d9b64 Fabrice Bellard
    /* stream list */
215
    for(i=0;i<n;i++) {
216 b4f0e04c Michael Niedermayer
        AVIStream *avist= s->streams[i]->priv_data;
217 1a40491e Daniel Verkamp
        list2 = ff_start_tag(pb, "LIST");
218 de6d9b64 Fabrice Bellard
        put_tag(pb, "strl");
219 115329f1 Diego Biurrun
220 01f4895c Michael Niedermayer
        stream = s->streams[i]->codec;
221 de6d9b64 Fabrice Bellard
222
        /* stream generic header */
223 1a40491e Daniel Verkamp
        strh = ff_start_tag(pb, "strh");
224 de6d9b64 Fabrice Bellard
        switch(stream->codec_type) {
225 2a04d6e7 Reimar Döffinger
        case CODEC_TYPE_SUBTITLE:
226
            // XSUB subtitles behave like video tracks, other subtitles
227
            // are not (yet) supported.
228
            if (stream->codec_id != CODEC_ID_XSUB) break;
229 1d92cc2b Michael Niedermayer
        case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
230
        case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
231
//        case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
232
        case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
233 de6d9b64 Fabrice Bellard
        }
234 2a04d6e7 Reimar Döffinger
        if(stream->codec_type == CODEC_TYPE_VIDEO ||
235
           stream->codec_id == CODEC_ID_XSUB)
236 1d92cc2b Michael Niedermayer
            put_le32(pb, stream->codec_tag);
237
        else
238
            put_le32(pb, 1);
239
        put_le32(pb, 0); /* flags */
240
        put_le16(pb, 0); /* priority */
241
        put_le16(pb, 0); /* language */
242
        put_le32(pb, 0); /* initial frame */
243
244
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
245
246
        put_le32(pb, au_scale); /* scale */
247
        put_le32(pb, au_byterate); /* rate */
248
        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
249
250
        put_le32(pb, 0); /* start */
251 b4f0e04c Michael Niedermayer
        avist->frames_hdr_strm = url_ftell(pb); /* remember this offset to fill later */
252 bb270c08 Diego Biurrun
        if (url_is_streamed(pb))
253
            put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
254
        else
255
            put_le32(pb, 0); /* length, XXX: filled later */
256 115329f1 Diego Biurrun
257 1d92cc2b Michael Niedermayer
        /* suggested buffer size */ //FIXME set at the end to largest chunk
258
        if(stream->codec_type == CODEC_TYPE_VIDEO)
259 115329f1 Diego Biurrun
            put_le32(pb, 1024 * 1024);
260 1d92cc2b Michael Niedermayer
        else if(stream->codec_type == CODEC_TYPE_AUDIO)
261 115329f1 Diego Biurrun
            put_le32(pb, 12 * 1024);
262 1d92cc2b Michael Niedermayer
        else
263 115329f1 Diego Biurrun
            put_le32(pb, 0);
264 1d92cc2b Michael Niedermayer
        put_le32(pb, -1); /* quality */
265
        put_le32(pb, au_ssize); /* sample size */
266
        put_le32(pb, 0);
267
        put_le16(pb, stream->width);
268
        put_le16(pb, stream->height);
269 1a40491e Daniel Verkamp
        ff_end_tag(pb, strh);
270 de6d9b64 Fabrice Bellard
271 1d92cc2b Michael Niedermayer
      if(stream->codec_type != CODEC_TYPE_DATA){
272 1a40491e Daniel Verkamp
        strf = ff_start_tag(pb, "strf");
273 de6d9b64 Fabrice Bellard
        switch(stream->codec_type) {
274 2a04d6e7 Reimar Döffinger
        case CODEC_TYPE_SUBTITLE:
275
            // XSUB subtitles behave like video tracks, other subtitles
276
            // are not (yet) supported.
277
            if (stream->codec_id != CODEC_ID_XSUB) break;
278 de6d9b64 Fabrice Bellard
        case CODEC_TYPE_VIDEO:
279 1a40491e Daniel Verkamp
            ff_put_bmp_header(pb, stream, ff_codec_bmp_tags, 0);
280 de6d9b64 Fabrice Bellard
            break;
281
        case CODEC_TYPE_AUDIO:
282 1a40491e Daniel Verkamp
            if (ff_put_wav_header(pb, stream) < 0) {
283 46a3d068 Fabrice Bellard
                return -1;
284
            }
285 de6d9b64 Fabrice Bellard
            break;
286 53cafac0 Philip Gladstone
        default:
287 71c32f19 Michael Niedermayer
            return -1;
288 de6d9b64 Fabrice Bellard
        }
289 1a40491e Daniel Verkamp
        ff_end_tag(pb, strf);
290 04d2540c Anton Khirnov
        if ((t = av_metadata_get(s->streams[i]->metadata, "strn", NULL, 0))) {
291
            avi_write_info_tag(s->pb, t->key, t->value);
292
            t = NULL;
293
        }
294
        //FIXME a limitation of metadata conversion system
295
        else if ((t = av_metadata_get(s->streams[i]->metadata, "INAM", NULL, 0))) {
296
            avi_write_info_tag(s->pb, "strn", t->value);
297
            t = NULL;
298
        }
299 1d92cc2b Michael Niedermayer
      }
300 115329f1 Diego Biurrun
301 bb270c08 Diego Biurrun
        if (!url_is_streamed(pb)) {
302
            unsigned char tag[5];
303
            int j;
304 115329f1 Diego Biurrun
305
            /* Starting to lay out AVI OpenDML master index.
306 bb270c08 Diego Biurrun
             * We want to make it JUNK entry for now, since we'd
307
             * like to get away without making AVI an OpenDML one
308
             * for compatibility reasons.
309
             */
310 b4f0e04c Michael Niedermayer
            avist->indexes.entry = avist->indexes.ents_allocated = 0;
311
            avist->indexes.indx_start = ff_start_tag(pb, "JUNK");
312 bb270c08 Diego Biurrun
            put_le16(pb, 4);        /* wLongsPerEntry */
313
            put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
314
            put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
315
            put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
316
            put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
317
                                    /* dwChunkId */
318
            put_le64(pb, 0);        /* dwReserved[3]
319
            put_le32(pb, 0);           Must be 0.    */
320
            for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
321
                 put_le64(pb, 0);
322 b4f0e04c Michael Niedermayer
            ff_end_tag(pb, avist->indexes.indx_start);
323 bb270c08 Diego Biurrun
        }
324 115329f1 Diego Biurrun
325 5b087549 Michael Niedermayer
        if(   stream->codec_type == CODEC_TYPE_VIDEO
326 59729451 Aurelien Jacobs
           && s->streams[i]->sample_aspect_ratio.num>0
327
           && s->streams[i]->sample_aspect_ratio.den>0){
328 1a40491e Daniel Verkamp
            int vprp= ff_start_tag(pb, "vprp");
329 59729451 Aurelien Jacobs
            AVRational dar = av_mul_q(s->streams[i]->sample_aspect_ratio,
330 5b087549 Michael Niedermayer
                                      (AVRational){stream->width, stream->height});
331
            int num, den;
332
            av_reduce(&num, &den, dar.num, dar.den, 0xFFFF);
333
334
            put_le32(pb, 0); //video format  = unknown
335
            put_le32(pb, 0); //video standard= unknown
336
            put_le32(pb, lrintf(1.0/av_q2d(stream->time_base)));
337
            put_le32(pb, stream->width );
338
            put_le32(pb, stream->height);
339
            put_le16(pb, den);
340 61559562 Michael Niedermayer
            put_le16(pb, num);
341 5b087549 Michael Niedermayer
            put_le32(pb, stream->width );
342
            put_le32(pb, stream->height);
343
            put_le32(pb, 1); //progressive FIXME
344
345
            put_le32(pb, stream->height);
346
            put_le32(pb, stream->width );
347
            put_le32(pb, stream->height);
348
            put_le32(pb, stream->width );
349
            put_le32(pb, 0);
350
            put_le32(pb, 0);
351
352
            put_le32(pb, 0);
353
            put_le32(pb, 0);
354 1a40491e Daniel Verkamp
            ff_end_tag(pb, vprp);
355 5b087549 Michael Niedermayer
        }
356
357 1a40491e Daniel Verkamp
        ff_end_tag(pb, list2);
358 de6d9b64 Fabrice Bellard
    }
359 115329f1 Diego Biurrun
360 e738cee9 Roman Shaposhnik
    if (!url_is_streamed(pb)) {
361
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
362 1a40491e Daniel Verkamp
        avi->odml_list = ff_start_tag(pb, "JUNK");
363 e738cee9 Roman Shaposhnik
        put_tag(pb, "odml");
364
        put_tag(pb, "dmlh");
365
        put_le32(pb, 248);
366
        for (i = 0; i < 248; i+= 4)
367
             put_le32(pb, 0);
368 1a40491e Daniel Verkamp
        ff_end_tag(pb, avi->odml_list);
369 e738cee9 Roman Shaposhnik
    }
370 de6d9b64 Fabrice Bellard
371 1a40491e Daniel Verkamp
    ff_end_tag(pb, list1);
372 115329f1 Diego Biurrun
373 1a40491e Daniel Verkamp
    list2 = ff_start_tag(pb, "LIST");
374 57060f89 David Conrad
    put_tag(pb, "INFO");
375 04d2540c Anton Khirnov
    for (i = 0; *ff_avi_tags[i]; i++) {
376
        if ((t = av_metadata_get(s->metadata, ff_avi_tags[i], NULL, AV_METADATA_MATCH_CASE)))
377
            avi_write_info_tag(s->pb, t->key, t->value);
378
    }
379 57060f89 David Conrad
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
380
        avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
381 1a40491e Daniel Verkamp
    ff_end_tag(pb, list2);
382 57060f89 David Conrad
383
    /* some padding for easier tag editing */
384 1a40491e Daniel Verkamp
    list2 = ff_start_tag(pb, "JUNK");
385 57060f89 David Conrad
    for (i = 0; i < 1016; i += 4)
386
        put_le32(pb, 0);
387 1a40491e Daniel Verkamp
    ff_end_tag(pb, list2);
388 57060f89 David Conrad
389 1a40491e Daniel Verkamp
    avi->movi_list = ff_start_tag(pb, "LIST");
390 de6d9b64 Fabrice Bellard
    put_tag(pb, "movi");
391
392
    put_flush_packet(pb);
393
394
    return 0;
395
}
396
397 e738cee9 Roman Shaposhnik
static int avi_write_ix(AVFormatContext *s)
398
{
399 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
400 e738cee9 Roman Shaposhnik
    AVIContext *avi = s->priv_data;
401 191e8ca7 Måns Rullgård
    char tag[5];
402
    char ix_tag[] = "ix00";
403 e738cee9 Roman Shaposhnik
    int i, j;
404 115329f1 Diego Biurrun
405 9eb40bcf Michael Niedermayer
    assert(!url_is_streamed(pb));
406 a2a2668d Alex Beregszaszi
407 e738cee9 Roman Shaposhnik
    if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
408
        return -1;
409 115329f1 Diego Biurrun
410 e738cee9 Roman Shaposhnik
    for (i=0;i<s->nb_streams;i++) {
411 b4f0e04c Michael Niedermayer
        AVIStream *avist= s->streams[i]->priv_data;
412 bc5c918e Diego Biurrun
         int64_t ix, pos;
413 115329f1 Diego Biurrun
414 bb270c08 Diego Biurrun
         avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
415
         ix_tag[3] = '0' + i;
416 115329f1 Diego Biurrun
417 bb270c08 Diego Biurrun
         /* Writing AVI OpenDML leaf index chunk */
418
         ix = url_ftell(pb);
419
         put_tag(pb, &ix_tag[0]);     /* ix?? */
420 b4f0e04c Michael Niedermayer
         put_le32(pb, avist->indexes.entry * 8 + 24);
421 bb270c08 Diego Biurrun
                                      /* chunk size */
422 e738cee9 Roman Shaposhnik
         put_le16(pb, 2);             /* wLongsPerEntry */
423 bb270c08 Diego Biurrun
         put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */
424
         put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
425 b4f0e04c Michael Niedermayer
         put_le32(pb, avist->indexes.entry);
426 bb270c08 Diego Biurrun
                                      /* nEntriesInUse */
427
         put_tag(pb, &tag[0]);        /* dwChunkId */
428
         put_le64(pb, avi->movi_list);/* qwBaseOffset */
429
         put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
430 e738cee9 Roman Shaposhnik
431 b4f0e04c Michael Niedermayer
         for (j=0; j<avist->indexes.entry; j++) {
432
             AVIIentry* ie = avi_get_ientry(&avist->indexes, j);
433 bb270c08 Diego Biurrun
             put_le32(pb, ie->pos + 8);
434
             put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
435
                          (ie->flags & 0x10 ? 0 : 0x80000000));
436 e738cee9 Roman Shaposhnik
         }
437 bb270c08 Diego Biurrun
         put_flush_packet(pb);
438 e738cee9 Roman Shaposhnik
         pos = url_ftell(pb);
439 115329f1 Diego Biurrun
440 bb270c08 Diego Biurrun
         /* Updating one entry in the AVI OpenDML master index */
441 b4f0e04c Michael Niedermayer
         url_fseek(pb, avist->indexes.indx_start - 8, SEEK_SET);
442 bb270c08 Diego Biurrun
         put_tag(pb, "indx");                 /* enabling this entry */
443
         url_fskip(pb, 8);
444
         put_le32(pb, avi->riff_id);          /* nEntriesInUse */
445
         url_fskip(pb, 16*avi->riff_id);
446
         put_le64(pb, ix);                    /* qwOffset */
447
         put_le32(pb, pos - ix);              /* dwSize */
448 b4f0e04c Michael Niedermayer
         put_le32(pb, avist->indexes.entry); /* dwDuration */
449 bb270c08 Diego Biurrun
450
         url_fseek(pb, pos, SEEK_SET);
451 e738cee9 Roman Shaposhnik
    }
452
    return 0;
453
}
454
455
static int avi_write_idx1(AVFormatContext *s)
456 ce9fce63 Roman Shaposhnik
{
457 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
458 ce9fce63 Roman Shaposhnik
    AVIContext *avi = s->priv_data;
459 bc5c918e Diego Biurrun
    int64_t idx_chunk;
460 5bce834e Brian Brice
    int i;
461 191e8ca7 Måns Rullgård
    char tag[5];
462 ce9fce63 Roman Shaposhnik
463
    if (!url_is_streamed(pb)) {
464 12f1b1fe Michael Niedermayer
        AVIStream *avist;
465 bb270c08 Diego Biurrun
        AVIIentry* ie = 0, *tie;
466
        int empty, stream_id = -1;
467
468 1a40491e Daniel Verkamp
        idx_chunk = ff_start_tag(pb, "idx1");
469 12f1b1fe Michael Niedermayer
        for(i=0; i<s->nb_streams; i++){
470
            avist= s->streams[i]->priv_data;
471
            avist->entry=0;
472
        }
473
474 bb270c08 Diego Biurrun
        do {
475
            empty = 1;
476
            for (i=0; i<s->nb_streams; i++) {
477 12f1b1fe Michael Niedermayer
                avist= s->streams[i]->priv_data;
478
                 if (avist->indexes.entry <= avist->entry)
479 bb270c08 Diego Biurrun
                     continue;
480
481 12f1b1fe Michael Niedermayer
                 tie = avi_get_ientry(&avist->indexes, avist->entry);
482 bb270c08 Diego Biurrun
                 if (empty || tie->pos < ie->pos) {
483
                     ie = tie;
484
                     stream_id = i;
485
                 }
486
                 empty = 0;
487
            }
488
            if (!empty) {
489 12f1b1fe Michael Niedermayer
                avist= s->streams[stream_id]->priv_data;
490 bb270c08 Diego Biurrun
                avi_stream2fourcc(&tag[0], stream_id,
491
                                  s->streams[stream_id]->codec->codec_type);
492
                put_tag(pb, &tag[0]);
493
                put_le32(pb, ie->flags);
494 e738cee9 Roman Shaposhnik
                put_le32(pb, ie->pos);
495
                put_le32(pb, ie->len);
496 12f1b1fe Michael Niedermayer
                avist->entry++;
497 bb270c08 Diego Biurrun
            }
498
        } while (!empty);
499 1a40491e Daniel Verkamp
        ff_end_tag(pb, idx_chunk);
500 ce9fce63 Roman Shaposhnik
501 5bce834e Brian Brice
        avi_write_counters(s, avi->riff_id);
502 ce9fce63 Roman Shaposhnik
    }
503
    return 0;
504
}
505
506 e928649b Michael Niedermayer
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
507 de6d9b64 Fabrice Bellard
{
508
    AVIContext *avi = s->priv_data;
509 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
510 de6d9b64 Fabrice Bellard
    unsigned char tag[5];
511 e928649b Michael Niedermayer
    unsigned int flags=0;
512
    const int stream_index= pkt->stream_index;
513 b4f0e04c Michael Niedermayer
    AVIStream *avist= s->streams[stream_index]->priv_data;
514 01f4895c Michael Niedermayer
    AVCodecContext *enc= s->streams[stream_index]->codec;
515 e928649b Michael Niedermayer
    int size= pkt->size;
516 ce9fce63 Roman Shaposhnik
517 949b1a13 Steve L'Homme
//    av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
518 b4f0e04c Michael Niedermayer
    while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avist->packet_count){
519 50c3dd32 Michael Niedermayer
        AVPacket empty_packet;
520
521
        av_init_packet(&empty_packet);
522
        empty_packet.size= 0;
523
        empty_packet.data= NULL;
524
        empty_packet.stream_index= stream_index;
525
        avi_write_packet(s, &empty_packet);
526 949b1a13 Steve L'Homme
//        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
527 50c3dd32 Michael Niedermayer
    }
528 b4f0e04c Michael Niedermayer
    avist->packet_count++;
529 50c3dd32 Michael Niedermayer
530 a2a2668d Alex Beregszaszi
    // Make sure to put an OpenDML chunk when the file size exceeds the limits
531 115329f1 Diego Biurrun
    if (!url_is_streamed(pb) &&
532 bb270c08 Diego Biurrun
        (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
533 115329f1 Diego Biurrun
534 e738cee9 Roman Shaposhnik
        avi_write_ix(s);
535 1a40491e Daniel Verkamp
        ff_end_tag(pb, avi->movi_list);
536 115329f1 Diego Biurrun
537 bb270c08 Diego Biurrun
        if (avi->riff_id == 1)
538
            avi_write_idx1(s);
539 e738cee9 Roman Shaposhnik
540 1a40491e Daniel Verkamp
        ff_end_tag(pb, avi->riff_start);
541 b4f0e04c Michael Niedermayer
        avi->movi_list = avi_start_new_riff(s, pb, "AVIX", "movi");
542 ce9fce63 Roman Shaposhnik
    }
543 115329f1 Diego Biurrun
544 e738cee9 Roman Shaposhnik
    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
545 e928649b Michael Niedermayer
    if(pkt->flags&PKT_FLAG_KEY)
546
        flags = 0x10;
547 e738cee9 Roman Shaposhnik
    if (enc->codec_type == CODEC_TYPE_AUDIO) {
548 b4f0e04c Michael Niedermayer
       avist->audio_strm_length += size;
549 e928649b Michael Niedermayer
    }
550 de6d9b64 Fabrice Bellard
551 899681cd Björn Axelsson
    if (!url_is_streamed(s->pb)) {
552 b4f0e04c Michael Niedermayer
        AVIIndex* idx = &avist->indexes;
553 bb270c08 Diego Biurrun
        int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
554
        int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
555 e738cee9 Roman Shaposhnik
        if (idx->ents_allocated <= idx->entry) {
556 bb270c08 Diego Biurrun
            idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
557
            if (!idx->cluster)
558
                return -1;
559 e738cee9 Roman Shaposhnik
            idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
560 bb270c08 Diego Biurrun
            if (!idx->cluster[cl])
561
                return -1;
562
            idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
563
        }
564 115329f1 Diego Biurrun
565 bb270c08 Diego Biurrun
        idx->cluster[cl][id].flags = flags;
566 e738cee9 Roman Shaposhnik
        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
567
        idx->cluster[cl][id].len = size;
568 bb270c08 Diego Biurrun
        idx->entry++;
569 de6d9b64 Fabrice Bellard
    }
570 115329f1 Diego Biurrun
571 de6d9b64 Fabrice Bellard
    put_buffer(pb, tag, 4);
572
    put_le32(pb, size);
573 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, size);
574 de6d9b64 Fabrice Bellard
    if (size & 1)
575
        put_byte(pb, 0);
576
577
    put_flush_packet(pb);
578
    return 0;
579
}
580
581
static int avi_write_trailer(AVFormatContext *s)
582
{
583
    AVIContext *avi = s->priv_data;
584 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
585 ce9fce63 Roman Shaposhnik
    int res = 0;
586 e738cee9 Roman Shaposhnik
    int i, j, n, nb_frames;
587 bc5c918e Diego Biurrun
    int64_t file_size;
588 e738cee9 Roman Shaposhnik
589 5c4e1928 Michael Niedermayer
    if (!url_is_streamed(pb)){
590
        if (avi->riff_id == 1) {
591 1a40491e Daniel Verkamp
            ff_end_tag(pb, avi->movi_list);
592 5c4e1928 Michael Niedermayer
            res = avi_write_idx1(s);
593 1a40491e Daniel Verkamp
            ff_end_tag(pb, avi->riff_start);
594 5c4e1928 Michael Niedermayer
        } else {
595
            avi_write_ix(s);
596 1a40491e Daniel Verkamp
            ff_end_tag(pb, avi->movi_list);
597
            ff_end_tag(pb, avi->riff_start);
598 5c4e1928 Michael Niedermayer
599
            file_size = url_ftell(pb);
600
            url_fseek(pb, avi->odml_list - 8, SEEK_SET);
601
            put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
602
            url_fskip(pb, 16);
603
604
            for (n=nb_frames=0;n<s->nb_streams;n++) {
605
                AVCodecContext *stream = s->streams[n]->codec;
606 b4f0e04c Michael Niedermayer
                AVIStream *avist= s->streams[n]->priv_data;
607
608 5c4e1928 Michael Niedermayer
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
609 b4f0e04c Michael Niedermayer
                    if (nb_frames < avist->packet_count)
610
                        nb_frames = avist->packet_count;
611 5c4e1928 Michael Niedermayer
                } else {
612
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
613 b4f0e04c Michael Niedermayer
                        nb_frames += avist->packet_count;
614 5c4e1928 Michael Niedermayer
                    }
615 86bec9a1 Juanjo
                }
616
            }
617 5c4e1928 Michael Niedermayer
            put_le32(pb, nb_frames);
618
            url_fseek(pb, file_size, SEEK_SET);
619 5bce834e Brian Brice
620 5c4e1928 Michael Niedermayer
            avi_write_counters(s, avi->riff_id);
621
        }
622 a2a2668d Alex Beregszaszi
    }
623 de6d9b64 Fabrice Bellard
    put_flush_packet(pb);
624 e738cee9 Roman Shaposhnik
625 b4f0e04c Michael Niedermayer
    for (i=0; i<s->nb_streams; i++) {
626
         AVIStream *avist= s->streams[i]->priv_data;
627
         for (j=0; j<avist->indexes.ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
628
              av_free(avist->indexes.cluster[j]);
629
         av_freep(&avist->indexes.cluster);
630
         avist->indexes.ents_allocated = avist->indexes.entry = 0;
631 e738cee9 Roman Shaposhnik
    }
632 115329f1 Diego Biurrun
633 ce9fce63 Roman Shaposhnik
    return res;
634 de6d9b64 Fabrice Bellard
}
635
636 ff70e601 Måns Rullgård
AVOutputFormat avi_muxer = {
637 de6d9b64 Fabrice Bellard
    "avi",
638 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("AVI format"),
639 de6d9b64 Fabrice Bellard
    "video/x-msvideo",
640
    "avi",
641 c9a65ca8 Fabrice Bellard
    sizeof(AVIContext),
642 de6d9b64 Fabrice Bellard
    CODEC_ID_MP2,
643 22cf0455 Michael Niedermayer
    CODEC_ID_MPEG4,
644 de6d9b64 Fabrice Bellard
    avi_write_header,
645
    avi_write_packet,
646
    avi_write_trailer,
647 1a40491e Daniel Verkamp
    .codec_tag= (const AVCodecTag* const []){ff_codec_bmp_tags, ff_codec_wav_tags, 0},
648 25918212 Michael Niedermayer
    .flags= AVFMT_VARIABLE_FPS,
649 04d2540c Anton Khirnov
    .metadata_conv = ff_avi_metadata_conv,
650 de6d9b64 Fabrice Bellard
};