Statistics
| Branch: | Revision:

ffmpeg / libavformat / avienc.c @ e356fc57

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