Statistics
| Branch: | Revision:

ffmpeg / libavformat / avienc.c @ 890d2799

History | View | Annotate | Download (18 KB)

1 de6d9b64 Fabrice Bellard
/*
2 7fbde343 Aurelien Jacobs
 * AVI muxer
3 19720f15 Fabrice Bellard
 * 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 de6d9b64 Fabrice Bellard
25
/*
26 115329f1 Diego Biurrun
 * TODO:
27 de6d9b64 Fabrice Bellard
 *  - fill all fields if non streamed (nb_frames for example)
28
 */
29
30 ff70e601 Måns Rullgård
#ifdef CONFIG_AVI_MUXER
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
    offset_t    indx_start;
39
    int         entry;
40
    int         ents_allocated;
41
    AVIIentry** cluster;
42 de6d9b64 Fabrice Bellard
} AVIIndex;
43
44
typedef struct {
45 ce9fce63 Roman Shaposhnik
    offset_t riff_start, movi_list, odml_list;
46
    offset_t frames_hdr_all, frames_hdr_strm[MAX_STREAMS];
47 86bec9a1 Juanjo
    int audio_strm_length[MAX_STREAMS];
48 e738cee9 Roman Shaposhnik
    int riff_id;
49 50c3dd32 Michael Niedermayer
    int packet_count[MAX_STREAMS];
50 e738cee9 Roman Shaposhnik
51
    AVIIndex indexes[MAX_STREAMS];
52 de6d9b64 Fabrice Bellard
} AVIContext;
53
54 115329f1 Diego Biurrun
static inline AVIIentry* avi_get_ientry(AVIIndex* idx, int ent_id)
55 e738cee9 Roman Shaposhnik
{
56
    int cl = ent_id / AVI_INDEX_CLUSTER_SIZE;
57
    int id = ent_id % AVI_INDEX_CLUSTER_SIZE;
58
    return &idx->cluster[cl][id];
59
}
60
61 115329f1 Diego Biurrun
static offset_t avi_start_new_riff(AVIContext *avi, ByteIOContext *pb,
62 ce9fce63 Roman Shaposhnik
                                   const char* riff_tag, const char* list_tag)
63
{
64
    offset_t loff;
65 e738cee9 Roman Shaposhnik
    int i;
66 115329f1 Diego Biurrun
67 e738cee9 Roman Shaposhnik
    avi->riff_id++;
68
    for (i=0; i<MAX_STREAMS; i++)
69
         avi->indexes[i].entry = 0;
70 115329f1 Diego Biurrun
71 ce9fce63 Roman Shaposhnik
    avi->riff_start = start_tag(pb, "RIFF");
72
    put_tag(pb, riff_tag);
73
    loff = start_tag(pb, "LIST");
74
    put_tag(pb, list_tag);
75
    return loff;
76
}
77
78 191e8ca7 Måns Rullgård
static char* avi_stream2fourcc(char* tag, int index, enum CodecType type)
79 e738cee9 Roman Shaposhnik
{
80
    tag[0] = '0';
81
    tag[1] = '0' + index;
82
    if (type == CODEC_TYPE_VIDEO) {
83
        tag[2] = 'd';
84
        tag[3] = 'c';
85
    } else {
86
        tag[2] = 'w';
87
        tag[3] = 'b';
88
    }
89
    tag[4] = '\0';
90
    return tag;
91
}
92
93 57060f89 David Conrad
static void avi_write_info_tag(ByteIOContext *pb, const char *tag, const char *str)
94
{
95
    int len = strlen(str);
96
    if (len > 0) {
97
        len++;
98
        put_tag(pb, tag);
99
        put_le32(pb, len);
100
        put_strz(pb, str);
101
        if (len & 1)
102
            put_byte(pb, 0);
103
    }
104
}
105
106 5bce834e Brian Brice
static int avi_write_counters(AVFormatContext* s, int riff_id)
107
{
108 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
109 5bce834e Brian Brice
    AVIContext *avi = s->priv_data;
110
    int n, au_byterate, au_ssize, au_scale, nb_frames = 0;
111
    offset_t file_size;
112
    AVCodecContext* stream;
113
114
    file_size = url_ftell(pb);
115
    for(n = 0; n < s->nb_streams; n++) {
116
        assert(avi->frames_hdr_strm[n]);
117
        stream = s->streams[n]->codec;
118
        url_fseek(pb, avi->frames_hdr_strm[n], SEEK_SET);
119
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
120
        if(au_ssize == 0) {
121
            put_le32(pb, avi->packet_count[n]);
122
        } else {
123
            put_le32(pb, avi->audio_strm_length[n] / au_ssize);
124
        }
125
        if(stream->codec_type == CODEC_TYPE_VIDEO)
126
            nb_frames = FFMAX(nb_frames, avi->packet_count[n]);
127
    }
128
    if(riff_id == 1) {
129
        assert(avi->frames_hdr_all);
130
        url_fseek(pb, avi->frames_hdr_all, SEEK_SET);
131
        put_le32(pb, nb_frames);
132
    }
133
    url_fseek(pb, file_size, SEEK_SET);
134
135
    return 0;
136
}
137
138 de6d9b64 Fabrice Bellard
static int avi_write_header(AVFormatContext *s)
139
{
140 c9a65ca8 Fabrice Bellard
    AVIContext *avi = s->priv_data;
141 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
142 86bec9a1 Juanjo
    int bitrate, n, i, nb_frames, au_byterate, au_ssize, au_scale;
143 de6d9b64 Fabrice Bellard
    AVCodecContext *stream, *video_enc;
144
    offset_t list1, list2, strh, strf;
145
146
    /* header list */
147 e738cee9 Roman Shaposhnik
    avi->riff_id = 0;
148 ce9fce63 Roman Shaposhnik
    list1 = avi_start_new_riff(avi, pb, "AVI ", "hdrl");
149 de6d9b64 Fabrice Bellard
150
    /* avi header */
151
    put_tag(pb, "avih");
152
    put_le32(pb, 14 * 4);
153
    bitrate = 0;
154
155
    video_enc = NULL;
156
    for(n=0;n<s->nb_streams;n++) {
157 01f4895c Michael Niedermayer
        stream = s->streams[n]->codec;
158 de6d9b64 Fabrice Bellard
        bitrate += stream->bit_rate;
159
        if (stream->codec_type == CODEC_TYPE_VIDEO)
160
            video_enc = stream;
161
    }
162 115329f1 Diego Biurrun
163 de6d9b64 Fabrice Bellard
    nb_frames = 0;
164
165 850742d7 Andriy Rysin
    if(video_enc){
166 8da9266c Måns Rullgård
        put_le32(pb, (uint32_t)(INT64_C(1000000) * video_enc->time_base.num / video_enc->time_base.den));
167 850742d7 Andriy Rysin
    } else {
168 bb270c08 Diego Biurrun
        put_le32(pb, 0);
169 850742d7 Andriy Rysin
    }
170 de6d9b64 Fabrice Bellard
    put_le32(pb, bitrate / 8); /* XXX: not quite exact */
171
    put_le32(pb, 0); /* padding */
172 a2a2668d Alex Beregszaszi
    if (url_is_streamed(pb))
173 bb270c08 Diego Biurrun
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_ISINTERLEAVED); /* flags */
174 a2a2668d Alex Beregszaszi
    else
175 bb270c08 Diego Biurrun
        put_le32(pb, AVIF_TRUSTCKTYPE | AVIF_HASINDEX | AVIF_ISINTERLEAVED); /* flags */
176 86bec9a1 Juanjo
    avi->frames_hdr_all = url_ftell(pb); /* remember this offset to fill later */
177 de6d9b64 Fabrice Bellard
    put_le32(pb, nb_frames); /* nb frames, filled later */
178
    put_le32(pb, 0); /* initial frame */
179
    put_le32(pb, s->nb_streams); /* nb streams */
180
    put_le32(pb, 1024 * 1024); /* suggested buffer size */
181 115329f1 Diego Biurrun
    if(video_enc){
182 98740f5a Michael Niedermayer
        put_le32(pb, video_enc->width);
183
        put_le32(pb, video_enc->height);
184 850742d7 Andriy Rysin
    } else {
185 bb270c08 Diego Biurrun
        put_le32(pb, 0);
186
        put_le32(pb, 0);
187 115329f1 Diego Biurrun
    }
188 de6d9b64 Fabrice Bellard
    put_le32(pb, 0); /* reserved */
189
    put_le32(pb, 0); /* reserved */
190
    put_le32(pb, 0); /* reserved */
191
    put_le32(pb, 0); /* reserved */
192 115329f1 Diego Biurrun
193 de6d9b64 Fabrice Bellard
    /* stream list */
194
    for(i=0;i<n;i++) {
195
        list2 = start_tag(pb, "LIST");
196
        put_tag(pb, "strl");
197 115329f1 Diego Biurrun
198 01f4895c Michael Niedermayer
        stream = s->streams[i]->codec;
199 de6d9b64 Fabrice Bellard
200
        /* stream generic header */
201
        strh = start_tag(pb, "strh");
202
        switch(stream->codec_type) {
203 1d92cc2b Michael Niedermayer
        case CODEC_TYPE_VIDEO: put_tag(pb, "vids"); break;
204
        case CODEC_TYPE_AUDIO: put_tag(pb, "auds"); break;
205
//        case CODEC_TYPE_TEXT : put_tag(pb, "txts"); break;
206
        case CODEC_TYPE_DATA : put_tag(pb, "dats"); break;
207 de6d9b64 Fabrice Bellard
        }
208 1d92cc2b Michael Niedermayer
        if(stream->codec_type == CODEC_TYPE_VIDEO)
209
            put_le32(pb, stream->codec_tag);
210
        else
211
            put_le32(pb, 1);
212
        put_le32(pb, 0); /* flags */
213
        put_le16(pb, 0); /* priority */
214
        put_le16(pb, 0); /* language */
215
        put_le32(pb, 0); /* initial frame */
216
217
        ff_parse_specific_params(stream, &au_byterate, &au_ssize, &au_scale);
218
219
        put_le32(pb, au_scale); /* scale */
220
        put_le32(pb, au_byterate); /* rate */
221
        av_set_pts_info(s->streams[i], 64, au_scale, au_byterate);
222
223
        put_le32(pb, 0); /* start */
224
        avi->frames_hdr_strm[i] = url_ftell(pb); /* remember this offset to fill later */
225 bb270c08 Diego Biurrun
        if (url_is_streamed(pb))
226
            put_le32(pb, AVI_MAX_RIFF_SIZE); /* FIXME: this may be broken, but who cares */
227
        else
228
            put_le32(pb, 0); /* length, XXX: filled later */
229 115329f1 Diego Biurrun
230 1d92cc2b Michael Niedermayer
        /* suggested buffer size */ //FIXME set at the end to largest chunk
231
        if(stream->codec_type == CODEC_TYPE_VIDEO)
232 115329f1 Diego Biurrun
            put_le32(pb, 1024 * 1024);
233 1d92cc2b Michael Niedermayer
        else if(stream->codec_type == CODEC_TYPE_AUDIO)
234 115329f1 Diego Biurrun
            put_le32(pb, 12 * 1024);
235 1d92cc2b Michael Niedermayer
        else
236 115329f1 Diego Biurrun
            put_le32(pb, 0);
237 1d92cc2b Michael Niedermayer
        put_le32(pb, -1); /* quality */
238
        put_le32(pb, au_ssize); /* sample size */
239
        put_le32(pb, 0);
240
        put_le16(pb, stream->width);
241
        put_le16(pb, stream->height);
242 de6d9b64 Fabrice Bellard
        end_tag(pb, strh);
243
244 1d92cc2b Michael Niedermayer
      if(stream->codec_type != CODEC_TYPE_DATA){
245 de6d9b64 Fabrice Bellard
        strf = start_tag(pb, "strf");
246
        switch(stream->codec_type) {
247
        case CODEC_TYPE_VIDEO:
248 bb270c08 Diego Biurrun
            put_bmp_header(pb, stream, codec_bmp_tags, 0);
249 de6d9b64 Fabrice Bellard
            break;
250
        case CODEC_TYPE_AUDIO:
251 46a3d068 Fabrice Bellard
            if (put_wav_header(pb, stream) < 0) {
252 1ea4f593 Fabrice Bellard
                av_free(avi);
253 46a3d068 Fabrice Bellard
                return -1;
254
            }
255 de6d9b64 Fabrice Bellard
            break;
256 53cafac0 Philip Gladstone
        default:
257 71c32f19 Michael Niedermayer
            return -1;
258 de6d9b64 Fabrice Bellard
        }
259
        end_tag(pb, strf);
260 1d92cc2b Michael Niedermayer
      }
261 115329f1 Diego Biurrun
262 bb270c08 Diego Biurrun
        if (!url_is_streamed(pb)) {
263
            unsigned char tag[5];
264
            int j;
265 115329f1 Diego Biurrun
266
            /* Starting to lay out AVI OpenDML master index.
267 bb270c08 Diego Biurrun
             * We want to make it JUNK entry for now, since we'd
268
             * like to get away without making AVI an OpenDML one
269
             * for compatibility reasons.
270
             */
271
            avi->indexes[i].entry = avi->indexes[i].ents_allocated = 0;
272
            avi->indexes[i].indx_start = start_tag(pb, "JUNK");
273
            put_le16(pb, 4);        /* wLongsPerEntry */
274
            put_byte(pb, 0);        /* bIndexSubType (0 == frame index) */
275
            put_byte(pb, 0);        /* bIndexType (0 == AVI_INDEX_OF_INDEXES) */
276
            put_le32(pb, 0);        /* nEntriesInUse (will fill out later on) */
277
            put_tag(pb, avi_stream2fourcc(&tag[0], i, stream->codec_type));
278
                                    /* dwChunkId */
279
            put_le64(pb, 0);        /* dwReserved[3]
280
            put_le32(pb, 0);           Must be 0.    */
281
            for (j=0; j < AVI_MASTER_INDEX_SIZE * 2; j++)
282
                 put_le64(pb, 0);
283
            end_tag(pb, avi->indexes[i].indx_start);
284
        }
285 115329f1 Diego Biurrun
286 de6d9b64 Fabrice Bellard
        end_tag(pb, list2);
287
    }
288 115329f1 Diego Biurrun
289 e738cee9 Roman Shaposhnik
    if (!url_is_streamed(pb)) {
290
        /* AVI could become an OpenDML one, if it grows beyond 2Gb range */
291
        avi->odml_list = start_tag(pb, "JUNK");
292
        put_tag(pb, "odml");
293
        put_tag(pb, "dmlh");
294
        put_le32(pb, 248);
295
        for (i = 0; i < 248; i+= 4)
296
             put_le32(pb, 0);
297
        end_tag(pb, avi->odml_list);
298
    }
299 de6d9b64 Fabrice Bellard
300
    end_tag(pb, list1);
301 115329f1 Diego Biurrun
302 57060f89 David Conrad
    list2 = start_tag(pb, "LIST");
303
    put_tag(pb, "INFO");
304
    avi_write_info_tag(pb, "INAM", s->title);
305
    avi_write_info_tag(pb, "IART", s->author);
306
    avi_write_info_tag(pb, "ICOP", s->copyright);
307
    avi_write_info_tag(pb, "ICMT", s->comment);
308
    avi_write_info_tag(pb, "IPRD", s->album);
309
    avi_write_info_tag(pb, "IGNR", s->genre);
310 2eb291c4 Panagiotis Issaris
    if (s->track) {
311
        char str_track[4];
312
        snprintf(str_track, 4, "%d", s->track);
313
        avi_write_info_tag(pb, "IPRT", str_track);
314
    }
315 57060f89 David Conrad
    if(!(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT))
316
        avi_write_info_tag(pb, "ISFT", LIBAVFORMAT_IDENT);
317
    end_tag(pb, list2);
318
319
    /* some padding for easier tag editing */
320
    list2 = start_tag(pb, "JUNK");
321
    for (i = 0; i < 1016; i += 4)
322
        put_le32(pb, 0);
323
    end_tag(pb, list2);
324
325 de6d9b64 Fabrice Bellard
    avi->movi_list = start_tag(pb, "LIST");
326
    put_tag(pb, "movi");
327
328
    put_flush_packet(pb);
329
330
    return 0;
331
}
332
333 e738cee9 Roman Shaposhnik
static int avi_write_ix(AVFormatContext *s)
334
{
335 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
336 e738cee9 Roman Shaposhnik
    AVIContext *avi = s->priv_data;
337 191e8ca7 Måns Rullgård
    char tag[5];
338
    char ix_tag[] = "ix00";
339 e738cee9 Roman Shaposhnik
    int i, j;
340 115329f1 Diego Biurrun
341 9eb40bcf Michael Niedermayer
    assert(!url_is_streamed(pb));
342 a2a2668d Alex Beregszaszi
343 e738cee9 Roman Shaposhnik
    if (avi->riff_id > AVI_MASTER_INDEX_SIZE)
344
        return -1;
345 115329f1 Diego Biurrun
346 e738cee9 Roman Shaposhnik
    for (i=0;i<s->nb_streams;i++) {
347 bb270c08 Diego Biurrun
         offset_t ix, pos;
348 115329f1 Diego Biurrun
349 bb270c08 Diego Biurrun
         avi_stream2fourcc(&tag[0], i, s->streams[i]->codec->codec_type);
350
         ix_tag[3] = '0' + i;
351 115329f1 Diego Biurrun
352 bb270c08 Diego Biurrun
         /* Writing AVI OpenDML leaf index chunk */
353
         ix = url_ftell(pb);
354
         put_tag(pb, &ix_tag[0]);     /* ix?? */
355
         put_le32(pb, avi->indexes[i].entry * 8 + 24);
356
                                      /* chunk size */
357 e738cee9 Roman Shaposhnik
         put_le16(pb, 2);             /* wLongsPerEntry */
358 bb270c08 Diego Biurrun
         put_byte(pb, 0);             /* bIndexSubType (0 == frame index) */
359
         put_byte(pb, 1);             /* bIndexType (1 == AVI_INDEX_OF_CHUNKS) */
360
         put_le32(pb, avi->indexes[i].entry);
361
                                      /* nEntriesInUse */
362
         put_tag(pb, &tag[0]);        /* dwChunkId */
363
         put_le64(pb, avi->movi_list);/* qwBaseOffset */
364
         put_le32(pb, 0);             /* dwReserved_3 (must be 0) */
365 e738cee9 Roman Shaposhnik
366
         for (j=0; j<avi->indexes[i].entry; j++) {
367
             AVIIentry* ie = avi_get_ientry(&avi->indexes[i], j);
368 bb270c08 Diego Biurrun
             put_le32(pb, ie->pos + 8);
369
             put_le32(pb, ((uint32_t)ie->len & ~0x80000000) |
370
                          (ie->flags & 0x10 ? 0 : 0x80000000));
371 e738cee9 Roman Shaposhnik
         }
372 bb270c08 Diego Biurrun
         put_flush_packet(pb);
373 e738cee9 Roman Shaposhnik
         pos = url_ftell(pb);
374 115329f1 Diego Biurrun
375 bb270c08 Diego Biurrun
         /* Updating one entry in the AVI OpenDML master index */
376
         url_fseek(pb, avi->indexes[i].indx_start - 8, SEEK_SET);
377
         put_tag(pb, "indx");                 /* enabling this entry */
378
         url_fskip(pb, 8);
379
         put_le32(pb, avi->riff_id);          /* nEntriesInUse */
380
         url_fskip(pb, 16*avi->riff_id);
381
         put_le64(pb, ix);                    /* qwOffset */
382
         put_le32(pb, pos - ix);              /* dwSize */
383
         put_le32(pb, avi->indexes[i].entry); /* dwDuration */
384
385
         url_fseek(pb, pos, SEEK_SET);
386 e738cee9 Roman Shaposhnik
    }
387
    return 0;
388
}
389
390
static int avi_write_idx1(AVFormatContext *s)
391 ce9fce63 Roman Shaposhnik
{
392 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
393 ce9fce63 Roman Shaposhnik
    AVIContext *avi = s->priv_data;
394 5bce834e Brian Brice
    offset_t idx_chunk;
395
    int i;
396 191e8ca7 Måns Rullgård
    char tag[5];
397 ce9fce63 Roman Shaposhnik
398
    if (!url_is_streamed(pb)) {
399 bb270c08 Diego Biurrun
        AVIIentry* ie = 0, *tie;
400
        int entry[MAX_STREAMS];
401
        int empty, stream_id = -1;
402
403
        idx_chunk = start_tag(pb, "idx1");
404
        memset(&entry[0], 0, sizeof(entry));
405
        do {
406
            empty = 1;
407
            for (i=0; i<s->nb_streams; i++) {
408
                 if (avi->indexes[i].entry <= entry[i])
409
                     continue;
410
411
                 tie = avi_get_ientry(&avi->indexes[i], entry[i]);
412
                 if (empty || tie->pos < ie->pos) {
413
                     ie = tie;
414
                     stream_id = i;
415
                 }
416
                 empty = 0;
417
            }
418
            if (!empty) {
419
                avi_stream2fourcc(&tag[0], stream_id,
420
                                  s->streams[stream_id]->codec->codec_type);
421
                put_tag(pb, &tag[0]);
422
                put_le32(pb, ie->flags);
423 e738cee9 Roman Shaposhnik
                put_le32(pb, ie->pos);
424
                put_le32(pb, ie->len);
425 bb270c08 Diego Biurrun
                entry[stream_id]++;
426
            }
427
        } while (!empty);
428
        end_tag(pb, idx_chunk);
429 ce9fce63 Roman Shaposhnik
430 5bce834e Brian Brice
        avi_write_counters(s, avi->riff_id);
431 ce9fce63 Roman Shaposhnik
    }
432
    return 0;
433
}
434
435 e928649b Michael Niedermayer
static int avi_write_packet(AVFormatContext *s, AVPacket *pkt)
436 de6d9b64 Fabrice Bellard
{
437
    AVIContext *avi = s->priv_data;
438 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
439 de6d9b64 Fabrice Bellard
    unsigned char tag[5];
440 e928649b Michael Niedermayer
    unsigned int flags=0;
441
    const int stream_index= pkt->stream_index;
442 01f4895c Michael Niedermayer
    AVCodecContext *enc= s->streams[stream_index]->codec;
443 e928649b Michael Niedermayer
    int size= pkt->size;
444 ce9fce63 Roman Shaposhnik
445 949b1a13 Steve L'Homme
//    av_log(s, AV_LOG_DEBUG, "%"PRId64" %d %d\n", pkt->dts, avi->packet_count[stream_index], stream_index);
446 16543599 Michael Niedermayer
    while(enc->block_align==0 && pkt->dts != AV_NOPTS_VALUE && pkt->dts > avi->packet_count[stream_index]){
447 50c3dd32 Michael Niedermayer
        AVPacket empty_packet;
448
449
        av_init_packet(&empty_packet);
450
        empty_packet.size= 0;
451
        empty_packet.data= NULL;
452
        empty_packet.stream_index= stream_index;
453
        avi_write_packet(s, &empty_packet);
454 949b1a13 Steve L'Homme
//        av_log(s, AV_LOG_DEBUG, "dup %"PRId64" %d\n", pkt->dts, avi->packet_count[stream_index]);
455 50c3dd32 Michael Niedermayer
    }
456
    avi->packet_count[stream_index]++;
457
458 a2a2668d Alex Beregszaszi
    // Make sure to put an OpenDML chunk when the file size exceeds the limits
459 115329f1 Diego Biurrun
    if (!url_is_streamed(pb) &&
460 bb270c08 Diego Biurrun
        (url_ftell(pb) - avi->riff_start > AVI_MAX_RIFF_SIZE)) {
461 115329f1 Diego Biurrun
462 e738cee9 Roman Shaposhnik
        avi_write_ix(s);
463
        end_tag(pb, avi->movi_list);
464 115329f1 Diego Biurrun
465 bb270c08 Diego Biurrun
        if (avi->riff_id == 1)
466
            avi_write_idx1(s);
467 e738cee9 Roman Shaposhnik
468 bb270c08 Diego Biurrun
        end_tag(pb, avi->riff_start);
469
        avi->movi_list = avi_start_new_riff(avi, pb, "AVIX", "movi");
470 ce9fce63 Roman Shaposhnik
    }
471 115329f1 Diego Biurrun
472 e738cee9 Roman Shaposhnik
    avi_stream2fourcc(&tag[0], stream_index, enc->codec_type);
473 e928649b Michael Niedermayer
    if(pkt->flags&PKT_FLAG_KEY)
474
        flags = 0x10;
475 e738cee9 Roman Shaposhnik
    if (enc->codec_type == CODEC_TYPE_AUDIO) {
476 86bec9a1 Juanjo
       avi->audio_strm_length[stream_index] += size;
477 e928649b Michael Niedermayer
    }
478 de6d9b64 Fabrice Bellard
479 899681cd Björn Axelsson
    if (!url_is_streamed(s->pb)) {
480 e738cee9 Roman Shaposhnik
        AVIIndex* idx = &avi->indexes[stream_index];
481 bb270c08 Diego Biurrun
        int cl = idx->entry / AVI_INDEX_CLUSTER_SIZE;
482
        int id = idx->entry % AVI_INDEX_CLUSTER_SIZE;
483 e738cee9 Roman Shaposhnik
        if (idx->ents_allocated <= idx->entry) {
484 bb270c08 Diego Biurrun
            idx->cluster = av_realloc(idx->cluster, (cl+1)*sizeof(void*));
485
            if (!idx->cluster)
486
                return -1;
487 e738cee9 Roman Shaposhnik
            idx->cluster[cl] = av_malloc(AVI_INDEX_CLUSTER_SIZE*sizeof(AVIIentry));
488 bb270c08 Diego Biurrun
            if (!idx->cluster[cl])
489
                return -1;
490
            idx->ents_allocated += AVI_INDEX_CLUSTER_SIZE;
491
        }
492 115329f1 Diego Biurrun
493 bb270c08 Diego Biurrun
        idx->cluster[cl][id].flags = flags;
494 e738cee9 Roman Shaposhnik
        idx->cluster[cl][id].pos = url_ftell(pb) - avi->movi_list;
495
        idx->cluster[cl][id].len = size;
496 bb270c08 Diego Biurrun
        idx->entry++;
497 de6d9b64 Fabrice Bellard
    }
498 115329f1 Diego Biurrun
499 de6d9b64 Fabrice Bellard
    put_buffer(pb, tag, 4);
500
    put_le32(pb, size);
501 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, size);
502 de6d9b64 Fabrice Bellard
    if (size & 1)
503
        put_byte(pb, 0);
504
505
    put_flush_packet(pb);
506
    return 0;
507
}
508
509
static int avi_write_trailer(AVFormatContext *s)
510
{
511
    AVIContext *avi = s->priv_data;
512 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
513 ce9fce63 Roman Shaposhnik
    int res = 0;
514 e738cee9 Roman Shaposhnik
    int i, j, n, nb_frames;
515
    offset_t file_size;
516
517 5c4e1928 Michael Niedermayer
    if (!url_is_streamed(pb)){
518
        if (avi->riff_id == 1) {
519
            end_tag(pb, avi->movi_list);
520
            res = avi_write_idx1(s);
521
            end_tag(pb, avi->riff_start);
522
        } else {
523
            avi_write_ix(s);
524
            end_tag(pb, avi->movi_list);
525
            end_tag(pb, avi->riff_start);
526
527
            file_size = url_ftell(pb);
528
            url_fseek(pb, avi->odml_list - 8, SEEK_SET);
529
            put_tag(pb, "LIST"); /* Making this AVI OpenDML one */
530
            url_fskip(pb, 16);
531
532
            for (n=nb_frames=0;n<s->nb_streams;n++) {
533
                AVCodecContext *stream = s->streams[n]->codec;
534
                if (stream->codec_type == CODEC_TYPE_VIDEO) {
535
                    if (nb_frames < avi->packet_count[n])
536
                        nb_frames = avi->packet_count[n];
537
                } else {
538
                    if (stream->codec_id == CODEC_ID_MP2 || stream->codec_id == CODEC_ID_MP3) {
539
                        nb_frames += avi->packet_count[n];
540
                    }
541 86bec9a1 Juanjo
                }
542
            }
543 5c4e1928 Michael Niedermayer
            put_le32(pb, nb_frames);
544
            url_fseek(pb, file_size, SEEK_SET);
545 5bce834e Brian Brice
546 5c4e1928 Michael Niedermayer
            avi_write_counters(s, avi->riff_id);
547
        }
548 a2a2668d Alex Beregszaszi
    }
549 de6d9b64 Fabrice Bellard
    put_flush_packet(pb);
550 e738cee9 Roman Shaposhnik
551
    for (i=0; i<MAX_STREAMS; i++) {
552 bb270c08 Diego Biurrun
         for (j=0; j<avi->indexes[i].ents_allocated/AVI_INDEX_CLUSTER_SIZE; j++)
553 e738cee9 Roman Shaposhnik
              av_free(avi->indexes[i].cluster[j]);
554 bb270c08 Diego Biurrun
         av_free(avi->indexes[i].cluster);
555
         avi->indexes[i].cluster = NULL;
556
         avi->indexes[i].ents_allocated = avi->indexes[i].entry = 0;
557 e738cee9 Roman Shaposhnik
    }
558 115329f1 Diego Biurrun
559 ce9fce63 Roman Shaposhnik
    return res;
560 de6d9b64 Fabrice Bellard
}
561
562 ff70e601 Måns Rullgård
AVOutputFormat avi_muxer = {
563 de6d9b64 Fabrice Bellard
    "avi",
564
    "avi format",
565
    "video/x-msvideo",
566
    "avi",
567 c9a65ca8 Fabrice Bellard
    sizeof(AVIContext),
568 de6d9b64 Fabrice Bellard
    CODEC_ID_MP2,
569 22cf0455 Michael Niedermayer
    CODEC_ID_MPEG4,
570 de6d9b64 Fabrice Bellard
    avi_write_header,
571
    avi_write_packet,
572
    avi_write_trailer,
573 6c77805f Michael Niedermayer
    .codec_tag= (const AVCodecTag*[]){codec_bmp_tags, codec_wav_tags, 0},
574 de6d9b64 Fabrice Bellard
};
575 ff70e601 Måns Rullgård
#endif //CONFIG_AVI_MUXER