Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 7e1720de

History | View | Annotate | Download (54.7 KB)

1 1cb5f7fd Michael Niedermayer
/*
2 7fbde343 Aurelien Jacobs
 * MOV, 3GP, MP4 muxer
3 1cb5f7fd Michael Niedermayer
 * Copyright (c) 2003 Thomas Raivio.
4 69dde1ad Gildas Bazin
 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
5 1cb5f7fd Michael Niedermayer
 *
6 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9 1cb5f7fd Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
12 1cb5f7fd Michael Niedermayer
 *
13 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
14 1cb5f7fd Michael Niedermayer
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
20 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 1cb5f7fd Michael Niedermayer
 */
22
#include "avformat.h"
23 9d9f4119 Måns Rullgård
#include "riff.h"
24 1cb5f7fd Michael Niedermayer
#include "avio.h"
25 e40ee6a2 Baptiste Coudurier
#include "isom.h"
26 1cb5f7fd Michael Niedermayer
27 6e6d6dc0 Michael Niedermayer
#undef NDEBUG
28
#include <assert.h>
29
30 1cb5f7fd Michael Niedermayer
#define MOV_INDEX_CLUSTER_SIZE 16384
31
#define globalTimescale 1000
32
33 69dde1ad Gildas Bazin
#define MODE_MP4 0
34
#define MODE_MOV 1
35
#define MODE_3GP 2
36 115329f1 Diego Biurrun
#define MODE_PSP 3 // example working PSP command line:
37 8af18154 tjcannell@blueyonder.co.uk
// ffmpeg -i testinput.avi  -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
38 8536ab89 tjraivio@cc.hut.fi
#define MODE_3G2 4
39 69dde1ad Gildas Bazin
40 1cb5f7fd Michael Niedermayer
typedef struct MOVIentry {
41 b29af723 Michael Niedermayer
    unsigned int flags, size;
42
    uint64_t     pos;
43 e45ccf79 Gildas Bazin
    unsigned int samplesInChunk;
44 f578f938 Thomas Raivio
    char         key_frame;
45 1cb5f7fd Michael Niedermayer
    unsigned int entries;
46 b4712e3c Baptiste Coudurier
    int64_t      cts;
47 aa90239f Baptiste Coudurier
    int64_t      dts;
48 1cb5f7fd Michael Niedermayer
} MOVIentry;
49
50
typedef struct MOVIndex {
51 69dde1ad Gildas Bazin
    int         mode;
52 1cb5f7fd Michael Niedermayer
    int         entry;
53
    long        timescale;
54
    long        time;
55 a3a80ddc Baptiste Coudurier
    int64_t     trackDuration;
56 e45ccf79 Gildas Bazin
    long        sampleCount;
57 5cb49ca1 Baptiste Coudurier
    long        sampleSize;
58 f578f938 Thomas Raivio
    int         hasKeyframes;
59 b4712e3c Baptiste Coudurier
    int         hasBframes;
60 ab561df9 François Revol
    int         language;
61 1cb5f7fd Michael Niedermayer
    int         trackID;
62 039627cf Baptiste Coudurier
    int         tag;
63 1cb5f7fd Michael Niedermayer
    AVCodecContext *enc;
64
65
    int         vosLen;
66 6e6d6dc0 Michael Niedermayer
    uint8_t     *vosData;
67 0aec3c5c Baptiste Coudurier
    MOVIentry   *cluster;
68 aa90239f Baptiste Coudurier
    int         audio_vbr;
69 1cb5f7fd Michael Niedermayer
} MOVTrack;
70
71 8af18154 tjcannell@blueyonder.co.uk
typedef struct MOVContext {
72 69dde1ad Gildas Bazin
    int     mode;
73 a3a80ddc Baptiste Coudurier
    int64_t time;
74 1cb5f7fd Michael Niedermayer
    int     nb_streams;
75 f578f938 Thomas Raivio
    offset_t mdat_pos;
76 039627cf Baptiste Coudurier
    uint64_t mdat_size;
77 1cb5f7fd Michael Niedermayer
    long    timescale;
78
    MOVTrack tracks[MAX_STREAMS];
79
} MOVContext;
80
81 90b5b51e Diego Biurrun
//FIXME support 64 bit variant with wide placeholders
82 b29af723 Michael Niedermayer
static offset_t updateSize (ByteIOContext *pb, offset_t pos)
83 1cb5f7fd Michael Niedermayer
{
84 b29af723 Michael Niedermayer
    offset_t curpos = url_ftell(pb);
85 1cb5f7fd Michael Niedermayer
    url_fseek(pb, pos, SEEK_SET);
86 6e6d6dc0 Michael Niedermayer
    put_be32(pb, curpos - pos); /* rewrite size */
87 1cb5f7fd Michael Niedermayer
    url_fseek(pb, curpos, SEEK_SET);
88 6e6d6dc0 Michael Niedermayer
89
    return curpos - pos;
90 1cb5f7fd Michael Niedermayer
}
91
92 e45ccf79 Gildas Bazin
/* Chunk offset atom */
93 6e6d6dc0 Michael Niedermayer
static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
94 1cb5f7fd Michael Niedermayer
{
95
    int i;
96 ed06cf6d Benjamin Larsson
    int mode64 = 0; //   use 32 bit size variant if possible
97 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
98 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
99 b29af723 Michael Niedermayer
    if (pos > UINT32_MAX) {
100
        mode64 = 1;
101
        put_tag(pb, "co64");
102
    } else
103
        put_tag(pb, "stco");
104 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* version & flags */
105
    put_be32(pb, track->entry); /* entry count */
106
    for (i=0; i<track->entry; i++) {
107 b29af723 Michael Niedermayer
        if(mode64 == 1)
108 0aec3c5c Baptiste Coudurier
            put_be64(pb, track->cluster[i].pos);
109 b29af723 Michael Niedermayer
        else
110 0aec3c5c Baptiste Coudurier
            put_be32(pb, track->cluster[i].pos);
111 1cb5f7fd Michael Niedermayer
    }
112 f578f938 Thomas Raivio
    return updateSize (pb, pos);
113 1cb5f7fd Michael Niedermayer
}
114
115 e45ccf79 Gildas Bazin
/* Sample size atom */
116 6e6d6dc0 Michael Niedermayer
static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
117 1cb5f7fd Michael Niedermayer
{
118 f578f938 Thomas Raivio
    int equalChunks = 1;
119 e45ccf79 Gildas Bazin
    int i, j, entries = 0, tst = -1, oldtst = -1;
120 1cb5f7fd Michael Niedermayer
121 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
122 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
123 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stsz");
124
    put_be32(pb, 0); /* version & flags */
125
126 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
127 0aec3c5c Baptiste Coudurier
        tst = track->cluster[i].size/track->cluster[i].entries;
128 e45ccf79 Gildas Bazin
        if(oldtst != -1 && tst != oldtst) {
129
            equalChunks = 0;
130 f578f938 Thomas Raivio
        }
131
        oldtst = tst;
132 0aec3c5c Baptiste Coudurier
        entries += track->cluster[i].entries;
133 f578f938 Thomas Raivio
    }
134 e45ccf79 Gildas Bazin
    if (equalChunks) {
135 0aec3c5c Baptiste Coudurier
        int sSize = track->cluster[0].size/track->cluster[0].entries;
136 115329f1 Diego Biurrun
        put_be32(pb, sSize); // sample size
137 e45ccf79 Gildas Bazin
        put_be32(pb, entries); // sample count
138 1cb5f7fd Michael Niedermayer
    }
139 f578f938 Thomas Raivio
    else {
140 115329f1 Diego Biurrun
        put_be32(pb, 0); // sample size
141
        put_be32(pb, entries); // sample count
142 f578f938 Thomas Raivio
        for (i=0; i<track->entry; i++) {
143 0aec3c5c Baptiste Coudurier
            for ( j=0; j<track->cluster[i].entries; j++) {
144
                put_be32(pb, track->cluster[i].size /
145
                         track->cluster[i].entries);
146 e45ccf79 Gildas Bazin
            }
147 1cb5f7fd Michael Niedermayer
        }
148
    }
149 f578f938 Thomas Raivio
    return updateSize (pb, pos);
150 1cb5f7fd Michael Niedermayer
}
151
152 e45ccf79 Gildas Bazin
/* Sample to chunk atom */
153 6e6d6dc0 Michael Niedermayer
static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
154 1cb5f7fd Michael Niedermayer
{
155 b29af723 Michael Niedermayer
    int index = 0, oldval = -1, i;
156
    offset_t entryPos, curpos;
157 f578f938 Thomas Raivio
158 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
159 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
160 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stsc");
161 115329f1 Diego Biurrun
    put_be32(pb, 0); // version & flags
162 f578f938 Thomas Raivio
    entryPos = url_ftell(pb);
163 115329f1 Diego Biurrun
    put_be32(pb, track->entry); // entry count
164 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
165 0aec3c5c Baptiste Coudurier
        if(oldval != track->cluster[i].samplesInChunk)
166 f578f938 Thomas Raivio
        {
167 115329f1 Diego Biurrun
            put_be32(pb, i+1); // first chunk
168 0aec3c5c Baptiste Coudurier
            put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
169 115329f1 Diego Biurrun
            put_be32(pb, 0x1); // sample description index
170 0aec3c5c Baptiste Coudurier
            oldval = track->cluster[i].samplesInChunk;
171 f578f938 Thomas Raivio
            index++;
172 1cb5f7fd Michael Niedermayer
        }
173
    }
174 f578f938 Thomas Raivio
    curpos = url_ftell(pb);
175
    url_fseek(pb, entryPos, SEEK_SET);
176 115329f1 Diego Biurrun
    put_be32(pb, index); // rewrite size
177 f578f938 Thomas Raivio
    url_fseek(pb, curpos, SEEK_SET);
178 1cb5f7fd Michael Niedermayer
179 f578f938 Thomas Raivio
    return updateSize (pb, pos);
180 1cb5f7fd Michael Niedermayer
}
181
182 e45ccf79 Gildas Bazin
/* Sync sample atom */
183 f578f938 Thomas Raivio
static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
184 1cb5f7fd Michael Niedermayer
{
185 b29af723 Michael Niedermayer
    offset_t curpos, entryPos;
186
    int i, index = 0;
187
    offset_t pos = url_ftell(pb);
188 115329f1 Diego Biurrun
    put_be32(pb, 0); // size
189 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stss");
190 115329f1 Diego Biurrun
    put_be32(pb, 0); // version & flags
191 f578f938 Thomas Raivio
    entryPos = url_ftell(pb);
192 115329f1 Diego Biurrun
    put_be32(pb, track->entry); // entry count
193 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
194 0aec3c5c Baptiste Coudurier
        if(track->cluster[i].key_frame == 1) {
195 f578f938 Thomas Raivio
            put_be32(pb, i+1);
196
            index++;
197
        }
198
    }
199
    curpos = url_ftell(pb);
200
    url_fseek(pb, entryPos, SEEK_SET);
201 115329f1 Diego Biurrun
    put_be32(pb, index); // rewrite size
202 f578f938 Thomas Raivio
    url_fseek(pb, curpos, SEEK_SET);
203
    return updateSize (pb, pos);
204 1cb5f7fd Michael Niedermayer
}
205
206 b7d9da10 Baptiste Coudurier
static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
207 1cb5f7fd Michael Niedermayer
{
208
    put_be32(pb, 0x11); /* size */
209 b7d9da10 Baptiste Coudurier
    if (track->mode == MODE_MOV) put_tag(pb, "samr");
210
    else                         put_tag(pb, "damr");
211 1cb5f7fd Michael Niedermayer
    put_tag(pb, "FFMP");
212 b7d9da10 Baptiste Coudurier
    put_byte(pb, 0); /* decoder version */
213 3a72cbd9 Baptiste Coudurier
214 875f9287 Baptiste Coudurier
    put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
215 b7d9da10 Baptiste Coudurier
    put_byte(pb, 0x00); /* Mode change period (no restriction) */
216
    put_byte(pb, 0x01); /* Frames per sample */
217 3a72cbd9 Baptiste Coudurier
    return 0x11;
218
}
219
220 fcef991a Baptiste Coudurier
static int mov_write_enda_tag(ByteIOContext *pb)
221
{
222
    put_be32(pb, 10);
223
    put_tag(pb, "enda");
224
    put_be16(pb, 1); /* little endian */
225
    return 10;
226
}
227
228 039627cf Baptiste Coudurier
static unsigned int descrLength(unsigned int len)
229
{
230 08db8f18 Michael Niedermayer
    int i;
231
    for(i=1; len>>(7*i); i++);
232
    return len + 1 + i;
233 039627cf Baptiste Coudurier
}
234
235 08db8f18 Michael Niedermayer
static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
236 039627cf Baptiste Coudurier
{
237 08db8f18 Michael Niedermayer
    int i= descrLength(size) - size - 2;
238
    put_byte(pb, tag);
239
    for(; i>0; i--)
240
        put_byte(pb, (size>>(7*i)) | 0x80);
241
    put_byte(pb, size & 0x7F);
242 039627cf Baptiste Coudurier
}
243
244
static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
245
{
246
    offset_t pos = url_ftell(pb);
247 fcef991a Baptiste Coudurier
    int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
248 039627cf Baptiste Coudurier
249
    put_be32(pb, 0);               // size
250
    put_tag(pb, "esds");
251
    put_be32(pb, 0);               // Version
252
253
    // ES descriptor
254
    putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
255
             descrLength(1));
256
    put_be16(pb, track->trackID);
257
    put_byte(pb, 0x00);            // flags (= no flags)
258
259
    // DecoderConfig descriptor
260
    putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
261
262
    // Object type indication
263 ccba88d7 Baptiste Coudurier
    put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
264 039627cf Baptiste Coudurier
265
    // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
266
    // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
267
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
268
        put_byte(pb, 0x15);            // flags (= Audiostream)
269
    else
270
        put_byte(pb, 0x11);            // flags (= Visualstream)
271
272
    put_byte(pb,  track->enc->rc_buffer_size>>(3+16));             // Buffersize DB (24 bits)
273
    put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF);          // Buffersize DB
274
275
    put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate));     // maxbitrate  (FIXME should be max rate in any 1 sec window)
276
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
277
        put_be32(pb, 0);     // vbr
278
    else
279
        put_be32(pb, track->enc->rc_max_rate);     // avg bitrate
280
281
    if (track->vosLen)
282
    {
283
        // DecoderSpecific info descriptor
284
        putDescr(pb, 0x05, track->vosLen);
285
        put_buffer(pb, track->vosData, track->vosLen);
286
    }
287
288
289
    // SL descriptor
290
    putDescr(pb, 0x06, 1);
291
    put_byte(pb, 0x02);
292
    return updateSize (pb, pos);
293
}
294
295 69dde1ad Gildas Bazin
static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
296
{
297 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
298 69dde1ad Gildas Bazin
299
    put_be32(pb, 0);     /* size */
300
    put_tag(pb, "wave");
301
302
    put_be32(pb, 12);    /* size */
303
    put_tag(pb, "frma");
304 fcef991a Baptiste Coudurier
    put_le32(pb, track->tag);
305 69dde1ad Gildas Bazin
306 fcef991a Baptiste Coudurier
    if (track->enc->codec_id == CODEC_ID_AAC) {
307 aa1c1c61 Baptiste Coudurier
        /* useless atom needed by mplayer, ipod, not needed by quicktime */
308
        put_be32(pb, 12); /* size */
309
        put_tag(pb, "mp4a");
310
        put_be32(pb, 0);
311 fcef991a Baptiste Coudurier
        mov_write_esds_tag(pb, track);
312
    } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
313
               track->enc->codec_id == CODEC_ID_PCM_S32LE) {
314
        mov_write_enda_tag(pb);
315 3a72cbd9 Baptiste Coudurier
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
316 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
317 fcef991a Baptiste Coudurier
    }
318 69dde1ad Gildas Bazin
319
    put_be32(pb, 8);     /* size */
320
    put_be32(pb, 0);     /* null tag */
321
322
    return updateSize (pb, pos);
323
}
324
325 f578f938 Thomas Raivio
static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
326 1cb5f7fd Michael Niedermayer
{
327 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
328 f45b744c Baptiste Coudurier
    int version = track->mode == MODE_MOV &&
329
        (track->audio_vbr ||
330
         track->enc->codec_id == CODEC_ID_PCM_S32LE ||
331
         track->enc->codec_id == CODEC_ID_PCM_S24LE);
332 115329f1 Diego Biurrun
333 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
334 039627cf Baptiste Coudurier
    put_le32(pb, track->tag); // store it byteswapped
335 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* Reserved */
336
    put_be16(pb, 0); /* Reserved */
337
    put_be16(pb, 1); /* Data-reference index, XXX  == 1 */
338 69dde1ad Gildas Bazin
339 e45ccf79 Gildas Bazin
    /* SoundDescription */
340 fcef991a Baptiste Coudurier
    put_be16(pb, version); /* Version */
341 e45ccf79 Gildas Bazin
    put_be16(pb, 0); /* Revision level */
342 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* Reserved */
343
344 2260499c Baptiste Coudurier
    if (track->mode == MODE_MOV) {
345
        put_be16(pb, track->enc->channels);
346 10fc840c Baptiste Coudurier
        if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
347
            track->enc->codec_id == CODEC_ID_PCM_S8)
348
            put_be16(pb, 8); /* bits per sample */
349
        else
350
            put_be16(pb, 16);
351 2260499c Baptiste Coudurier
        put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
352
    } else { /* reserved for mp4/3gp */
353
        put_be16(pb, 2);
354
        put_be16(pb, 16);
355
        put_be16(pb, 0);
356
    }
357 ef19c7eb Gildas Bazin
358 9a4d9388 Roman Shaposhnik
    put_be16(pb, 0); /* packet size (= 0) */
359 1cb5f7fd Michael Niedermayer
    put_be16(pb, track->timescale); /* Time scale */
360
    put_be16(pb, 0); /* Reserved */
361
362 5cb49ca1 Baptiste Coudurier
    if(version == 1) { /* SoundDescription V1 extended info */
363
        put_be32(pb, track->enc->frame_size); /* Samples per packet */
364 9a39cd35 Michael Niedermayer
        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
365 5cb49ca1 Baptiste Coudurier
        put_be32(pb, track->sampleSize); /* Bytes per frame */
366 aa1c1c61 Baptiste Coudurier
        put_be32(pb, 2); /* Bytes per sample */
367 69dde1ad Gildas Bazin
    }
368
369 6dd19fff Baptiste Coudurier
    if(track->mode == MODE_MOV &&
370
       (track->enc->codec_id == CODEC_ID_AAC ||
371
        track->enc->codec_id == CODEC_ID_AMR_NB ||
372
        track->enc->codec_id == CODEC_ID_PCM_S24LE ||
373
        track->enc->codec_id == CODEC_ID_PCM_S32LE))
374 fcef991a Baptiste Coudurier
        mov_write_wave_tag(pb, track);
375 3a72cbd9 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_AAC)
376
        mov_write_esds_tag(pb, track);
377
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
378 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
379 3a72cbd9 Baptiste Coudurier
380 6e6d6dc0 Michael Niedermayer
    return updateSize (pb, pos);
381 1cb5f7fd Michael Niedermayer
}
382
383 6e6d6dc0 Michael Niedermayer
static int mov_write_d263_tag(ByteIOContext *pb)
384 1cb5f7fd Michael Niedermayer
{
385
    put_be32(pb, 0xf); /* size */
386
    put_tag(pb, "d263");
387
    put_tag(pb, "FFMP");
388 87494ea0 Baptiste Coudurier
    put_byte(pb, 0); /* decoder version */
389
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
390
    put_byte(pb, 0xa); /* level */
391
    put_byte(pb, 0); /* profile */
392 1cb5f7fd Michael Niedermayer
    return 0xf;
393
}
394
395 f578f938 Thomas Raivio
/* TODO: No idea about these values */
396
static int mov_write_svq3_tag(ByteIOContext *pb)
397 1cb5f7fd Michael Niedermayer
{
398 f578f938 Thomas Raivio
    put_be32(pb, 0x15);
399
    put_tag(pb, "SMI ");
400
    put_tag(pb, "SEQH");
401
    put_be32(pb, 0x5);
402
    put_be32(pb, 0xe2c0211d);
403
    put_be32(pb, 0xc0000000);
404 115329f1 Diego Biurrun
    put_byte(pb, 0);
405 f578f938 Thomas Raivio
    return 0x15;
406 1cb5f7fd Michael Niedermayer
}
407
408 c1b8e6d8 Baptiste Coudurier
static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
409
{
410 4f9e995a Måns Rullgård
    uint8_t *a = p + 4 - ((long)p & 3);
411 c1b8e6d8 Baptiste Coudurier
412
    for( end -= 3; p < a && p < end; p++ ) {
413
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
414
            return p;
415
    }
416
417
    for( end -= 3; p < end; p += 4 ) {
418
        uint32_t x = *(uint32_t*)p;
419
//      if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
420
//      if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
421
        if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
422
            if( p[1] == 0 ) {
423
                if( p[0] == 0 && p[2] == 1 )
424 2c95f59f Baptiste Coudurier
                    return p-1;
425 c1b8e6d8 Baptiste Coudurier
                if( p[2] == 0 && p[3] == 1 )
426 2c95f59f Baptiste Coudurier
                    return p;
427 c1b8e6d8 Baptiste Coudurier
            }
428
            if( p[3] == 0 ) {
429
                if( p[2] == 0 && p[4] == 1 )
430 2c95f59f Baptiste Coudurier
                    return p+1;
431 c1b8e6d8 Baptiste Coudurier
                if( p[4] == 0 && p[5] == 1 )
432 2c95f59f Baptiste Coudurier
                    return p+2;
433 c1b8e6d8 Baptiste Coudurier
            }
434
        }
435
    }
436
437
    for( end += 3; p < end; p++ ) {
438
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
439
            return p;
440
    }
441
442
    return end + 3;
443
}
444
445 899681cd Björn Axelsson
static int avc_parse_nal_units(uint8_t **buf, int *size)
446 c1b8e6d8 Baptiste Coudurier
{
447 899681cd Björn Axelsson
    ByteIOContext *pb;
448 c1b8e6d8 Baptiste Coudurier
    uint8_t *p = *buf;
449
    uint8_t *end = p + *size;
450
    uint8_t *nal_start, *nal_end;
451 899681cd Björn Axelsson
    int ret = url_open_dyn_buf(&pb);
452
    if(ret < 0)
453
        return ret;
454 c1b8e6d8 Baptiste Coudurier
455
    nal_start = avc_find_startcode(p, end);
456
    while (nal_start < end) {
457
        while(!*(nal_start++));
458
        nal_end = avc_find_startcode(nal_start, end);
459 899681cd Björn Axelsson
        put_be32(pb, nal_end - nal_start);
460
        put_buffer(pb, nal_start, nal_end - nal_start);
461 c1b8e6d8 Baptiste Coudurier
        nal_start = nal_end;
462
    }
463
    av_freep(buf);
464 899681cd Björn Axelsson
    *size = url_close_dyn_buf(pb, buf);
465
    return 0;
466 c1b8e6d8 Baptiste Coudurier
}
467
468
static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
469
{
470
    offset_t pos = url_ftell(pb);
471
472
    put_be32(pb, 0);
473
    put_tag(pb, "avcC");
474
    if (track->vosLen > 6) {
475
        /* check for h264 start code */
476 fead30d4 Alex Beregszaszi
        if (AV_RB32(track->vosData) == 0x00000001) {
477 c1b8e6d8 Baptiste Coudurier
            uint8_t *buf, *end;
478
            uint32_t sps_size=0, pps_size=0;
479
            uint8_t *sps=0, *pps=0;
480
481
            avc_parse_nal_units(&track->vosData, &track->vosLen);
482
            buf = track->vosData;
483
            end = track->vosData + track->vosLen;
484
485
            /* look for sps and pps */
486
            while (buf < end) {
487
                unsigned int size;
488
                uint8_t nal_type;
489 fead30d4 Alex Beregszaszi
                size = AV_RB32(buf);
490 c1b8e6d8 Baptiste Coudurier
                nal_type = buf[4] & 0x1f;
491
                if (nal_type == 7) { /* SPS */
492
                    sps = buf + 4;
493
                    sps_size = size;
494
                } else if (nal_type == 8) { /* PPS */
495
                    pps = buf + 4;
496
                    pps_size = size;
497
                }
498
                buf += size + 4;
499
            }
500
            assert(sps);
501
            assert(pps);
502 611fd605 Baptiste Coudurier
503
            put_byte(pb, 1); /* version */
504
            put_byte(pb, sps[1]); /* profile */
505
            put_byte(pb, sps[2]); /* profile compat */
506
            put_byte(pb, sps[3]); /* level */
507
            put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
508
            put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
509
510 c1b8e6d8 Baptiste Coudurier
            put_be16(pb, sps_size);
511
            put_buffer(pb, sps, sps_size);
512
            put_byte(pb, 1); /* number of pps */
513
            put_be16(pb, pps_size);
514
            put_buffer(pb, pps, pps_size);
515
        } else {
516
            put_buffer(pb, track->vosData, track->vosLen);
517
        }
518
    }
519
    return updateSize(pb, pos);
520
}
521
522 fc4cbc16 Baptiste Coudurier
/* also used by all avid codecs (dv, imx, meridien) and their variants */
523
static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
524
{
525
    int i;
526
    put_be32(pb, 24); /* size */
527
    put_tag(pb, "ACLR");
528
    put_tag(pb, "ACLR");
529
    put_tag(pb, "0001");
530
    put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
531
    put_be32(pb, 0); /* unknown */
532
533
    put_be32(pb, 24); /* size */
534
    put_tag(pb, "APRG");
535
    put_tag(pb, "APRG");
536
    put_tag(pb, "0001");
537
    put_be32(pb, 1); /* unknown */
538
    put_be32(pb, 0); /* unknown */
539
540
    put_be32(pb, 120); /* size */
541
    put_tag(pb, "ARES");
542
    put_tag(pb, "ARES");
543
    put_tag(pb, "0001");
544
    put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
545
    put_be32(pb, track->enc->width);
546
    /* values below are based on samples created with quicktime and avid codecs */
547
    if (track->vosData[5] & 2) { // interlaced
548
        put_be32(pb, track->enc->height/2);
549
        put_be32(pb, 2); /* unknown */
550
        put_be32(pb, 0); /* unknown */
551
        put_be32(pb, 4); /* unknown */
552
    } else {
553
        put_be32(pb, track->enc->height);
554
        put_be32(pb, 1); /* unknown */
555
        put_be32(pb, 0); /* unknown */
556
        put_be32(pb, 5); /* unknown */
557
    }
558
    /* padding */
559
    for (i = 0; i < 10; i++)
560
        put_be64(pb, 0);
561
562
    /* extra padding for stsd needed */
563
    put_be32(pb, 0);
564
    return 0;
565
}
566
567 039627cf Baptiste Coudurier
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
568 8e321619 Baptiste Coudurier
{
569 039627cf Baptiste Coudurier
    int tag = track->enc->codec_tag;
570 8e321619 Baptiste Coudurier
    if (!tag) {
571
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
572
            if (track->enc->height == 480) { /* NTSC */
573
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
574
                    tag = MKTAG('d', 'v', '5', 'n');
575
                else
576
                    tag = MKTAG('d', 'v', 'c', ' ');
577
            } else { /* assume PAL */
578
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
579
                    tag = MKTAG('d', 'v', '5', 'p');
580
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
581
                    tag = MKTAG('d', 'v', 'c', 'p');
582 3cdc7eb4 Baptiste Coudurier
                else
583
                    tag = MKTAG('d', 'v', 'p', 'p');
584 8e321619 Baptiste Coudurier
            }
585 2c5ad5fd Baptiste Coudurier
        } else if (track->enc->codec_id == CODEC_ID_H263) {
586
            if (track->mode == MODE_MOV)
587
                tag = MKTAG('h', '2', '6', '3');
588
            else
589
                tag = MKTAG('s', '2', '6', '3');
590 8e321619 Baptiste Coudurier
        } else {
591
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
592
        }
593
    }
594
    // if no mac fcc found, try with Microsoft tags
595 039627cf Baptiste Coudurier
    if (!tag) {
596 8e321619 Baptiste Coudurier
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
597 039627cf Baptiste Coudurier
        if (tag) {
598
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
599
        }
600
    }
601
    assert(tag);
602
    return tag;
603
}
604
605
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
606
{
607
    int tag = track->enc->codec_tag;
608
    if (!tag) {
609
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
610
    }
611
    // if no mac fcc found, try with Microsoft tags
612
    if (!tag) {
613
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
614
        if (ms_tag) {
615
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
616
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
617
        }
618
    }
619 8e321619 Baptiste Coudurier
    assert(tag);
620
    return tag;
621
}
622
623 f578f938 Thomas Raivio
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
624 1cb5f7fd Michael Niedermayer
{
625 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
626 53ffdd14 Roine Gustafsson
    char compressor_name[32];
627 8cc7a34d Alex Beregszaszi
628 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
629 039627cf Baptiste Coudurier
    put_le32(pb, track->tag); // store it byteswapped
630 f578f938 Thomas Raivio
    put_be32(pb, 0); /* Reserved */
631
    put_be16(pb, 0); /* Reserved */
632
    put_be16(pb, 1); /* Data-reference index */
633
634 53ffdd14 Roine Gustafsson
    put_be16(pb, 0); /* Codec stream version */
635
    put_be16(pb, 0); /* Codec stream revision (=0) */
636 5997ed78 Baptiste Coudurier
    if (track->mode == MODE_MOV) {
637 915282e5 Baptiste Coudurier
        put_tag(pb, "FFMP"); /* Vendor */
638
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
639
            put_be32(pb, 0); /* Temporal Quality */
640
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
641
        } else {
642
            put_be32(pb, 0x200); /* Temporal Quality = normal */
643
            put_be32(pb, 0x200); /* Spatial Quality = normal */
644
        }
645 5997ed78 Baptiste Coudurier
    } else {
646
        put_be32(pb, 0); /* Reserved */
647
        put_be32(pb, 0); /* Reserved */
648
        put_be32(pb, 0); /* Reserved */
649
    }
650 f578f938 Thomas Raivio
    put_be16(pb, track->enc->width); /* Video width */
651
    put_be16(pb, track->enc->height); /* Video height */
652 53ffdd14 Roine Gustafsson
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
653
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
654 9a4d9388 Roman Shaposhnik
    put_be32(pb, 0); /* Data size (= 0) */
655
    put_be16(pb, 1); /* Frame count (= 1) */
656 115329f1 Diego Biurrun
657 53ffdd14 Roine Gustafsson
    memset(compressor_name,0,32);
658 5997ed78 Baptiste Coudurier
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
659
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
660 53ffdd14 Roine Gustafsson
        strncpy(compressor_name,track->enc->codec->name,31);
661 25cf9062 François Revol
    put_byte(pb, strlen(compressor_name));
662 53ffdd14 Roine Gustafsson
    put_buffer(pb, compressor_name, 31);
663 115329f1 Diego Biurrun
664 f578f938 Thomas Raivio
    put_be16(pb, 0x18); /* Reserved */
665
    put_be16(pb, 0xffff); /* Reserved */
666
    if(track->enc->codec_id == CODEC_ID_MPEG4)
667
        mov_write_esds_tag(pb, track);
668
    else if(track->enc->codec_id == CODEC_ID_H263)
669
        mov_write_d263_tag(pb);
670
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
671 115329f1 Diego Biurrun
        mov_write_svq3_tag(pb);
672 c1b8e6d8 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_H264)
673
        mov_write_avcc_tag(pb, track);
674 fc4cbc16 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
675
        mov_write_avid_tag(pb, track);
676 f578f938 Thomas Raivio
677
    return updateSize (pb, pos);
678 1cb5f7fd Michael Niedermayer
}
679
680 6e6d6dc0 Michael Niedermayer
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
681 1cb5f7fd Michael Niedermayer
{
682 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
683 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
684
    put_tag(pb, "stsd");
685
    put_be32(pb, 0); /* version & flags */
686
    put_be32(pb, 1); /* entry count */
687 f578f938 Thomas Raivio
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
688
        mov_write_video_tag(pb, track);
689
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
690
        mov_write_audio_tag(pb, track);
691 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
692 1cb5f7fd Michael Niedermayer
}
693
694 b4712e3c Baptiste Coudurier
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
695
{
696 e23848a4 Baptiste Coudurier
    MOV_stts_t *ctts_entries;
697 b4712e3c Baptiste Coudurier
    uint32_t entries = 0;
698
    uint32_t atom_size;
699
    int i;
700
701
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
702
    ctts_entries[0].count = 1;
703 0aec3c5c Baptiste Coudurier
    ctts_entries[0].duration = track->cluster[0].cts;
704 b4712e3c Baptiste Coudurier
    for (i=1; i<track->entry; i++) {
705 0aec3c5c Baptiste Coudurier
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
706 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count++; /* compress */
707
        } else {
708
            entries++;
709 0aec3c5c Baptiste Coudurier
            ctts_entries[entries].duration = track->cluster[i].cts;
710 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count = 1;
711
        }
712
    }
713
    entries++; /* last one */
714
    atom_size = 16 + (entries * 8);
715
    put_be32(pb, atom_size); /* size */
716
    put_tag(pb, "ctts");
717
    put_be32(pb, 0); /* version & flags */
718
    put_be32(pb, entries); /* entry count */
719
    for (i=0; i<entries; i++) {
720
        put_be32(pb, ctts_entries[i].count);
721
        put_be32(pb, ctts_entries[i].duration);
722
    }
723
    av_free(ctts_entries);
724
    return atom_size;
725
}
726
727 e45ccf79 Gildas Bazin
/* Time to sample atom */
728 6e6d6dc0 Michael Niedermayer
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
729 1cb5f7fd Michael Niedermayer
{
730 e23848a4 Baptiste Coudurier
    MOV_stts_t *stts_entries;
731 aa90239f Baptiste Coudurier
    uint32_t entries = -1;
732
    uint32_t atom_size;
733
    int i;
734
735
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
736
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
737
        stts_entries[0].count = track->sampleCount;
738
        stts_entries[0].duration = 1;
739
        entries = 1;
740
    } else {
741
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
742
        for (i=0; i<track->entry; i++) {
743
            int64_t duration = i + 1 == track->entry ?
744
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
745
                track->cluster[i+1].dts - track->cluster[i].dts;
746
            if (i && duration == stts_entries[entries].duration) {
747
                stts_entries[entries].count++; /* compress */
748
            } else {
749
                entries++;
750
                stts_entries[entries].duration = duration;
751
                stts_entries[entries].count = 1;
752
            }
753
        }
754
        entries++; /* last one */
755
    }
756
    atom_size = 16 + (entries * 8);
757
    put_be32(pb, atom_size); /* size */
758 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stts");
759
    put_be32(pb, 0); /* version & flags */
760 aa90239f Baptiste Coudurier
    put_be32(pb, entries); /* entry count */
761
    for (i=0; i<entries; i++) {
762
        put_be32(pb, stts_entries[i].count);
763
        put_be32(pb, stts_entries[i].duration);
764
    }
765
    av_free(stts_entries);
766
    return atom_size;
767 1cb5f7fd Michael Niedermayer
}
768
769 6e6d6dc0 Michael Niedermayer
static int mov_write_dref_tag(ByteIOContext *pb)
770 1cb5f7fd Michael Niedermayer
{
771
    put_be32(pb, 28); /* size */
772
    put_tag(pb, "dref");
773
    put_be32(pb, 0); /* version & flags */
774
    put_be32(pb, 1); /* entry count */
775
776
    put_be32(pb, 0xc); /* size */
777
    put_tag(pb, "url ");
778
    put_be32(pb, 1); /* version & flags */
779
780
    return 28;
781
}
782
783 6e6d6dc0 Michael Niedermayer
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
784 1cb5f7fd Michael Niedermayer
{
785 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
786 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
787
    put_tag(pb, "stbl");
788 6e6d6dc0 Michael Niedermayer
    mov_write_stsd_tag(pb, track);
789
    mov_write_stts_tag(pb, track);
790 f578f938 Thomas Raivio
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
791 32ba6fb1 Baptiste Coudurier
        track->hasKeyframes < track->entry)
792 f578f938 Thomas Raivio
        mov_write_stss_tag(pb, track);
793 b4712e3c Baptiste Coudurier
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
794
        track->hasBframes)
795
        mov_write_ctts_tag(pb, track);
796 6e6d6dc0 Michael Niedermayer
    mov_write_stsc_tag(pb, track);
797
    mov_write_stsz_tag(pb, track);
798
    mov_write_stco_tag(pb, track);
799
    return updateSize(pb, pos);
800 1cb5f7fd Michael Niedermayer
}
801
802 6e6d6dc0 Michael Niedermayer
static int mov_write_dinf_tag(ByteIOContext *pb)
803 1cb5f7fd Michael Niedermayer
{
804 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
805 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
806
    put_tag(pb, "dinf");
807 6e6d6dc0 Michael Niedermayer
    mov_write_dref_tag(pb);
808
    return updateSize(pb, pos);
809 1cb5f7fd Michael Niedermayer
}
810
811 6e6d6dc0 Michael Niedermayer
static int mov_write_smhd_tag(ByteIOContext *pb)
812 1cb5f7fd Michael Niedermayer
{
813
    put_be32(pb, 16); /* size */
814
    put_tag(pb, "smhd");
815
    put_be32(pb, 0); /* version & flags */
816
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
817
    put_be16(pb, 0); /* reserved */
818
    return 16;
819
}
820
821 6e6d6dc0 Michael Niedermayer
static int mov_write_vmhd_tag(ByteIOContext *pb)
822 1cb5f7fd Michael Niedermayer
{
823
    put_be32(pb, 0x14); /* size (always 0x14) */
824
    put_tag(pb, "vmhd");
825
    put_be32(pb, 0x01); /* version & flags */
826
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
827
    return 0x14;
828
}
829
830 6e6d6dc0 Michael Niedermayer
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
831 1cb5f7fd Michael Niedermayer
{
832 a97c1e93 Stefan Huehner
    const char *descr, *hdlr, *hdlr_type;
833 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
834 115329f1 Diego Biurrun
835 9a4d9388 Roman Shaposhnik
    if (!track) { /* no media --> data handler */
836 bb270c08 Diego Biurrun
        hdlr = "dhlr";
837
        hdlr_type = "url ";
838
        descr = "DataHandler";
839 9a4d9388 Roman Shaposhnik
    } else {
840 bb270c08 Diego Biurrun
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
841
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
842
            hdlr_type = "vide";
843
            descr = "VideoHandler";
844
        } else {
845
            hdlr_type = "soun";
846
            descr = "SoundHandler";
847
        }
848 9a4d9388 Roman Shaposhnik
    }
849 115329f1 Diego Biurrun
850 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
851 1cb5f7fd Michael Niedermayer
    put_tag(pb, "hdlr");
852
    put_be32(pb, 0); /* Version & flags */
853 906b578f Roman Shaposhnik
    put_buffer(pb, hdlr, 4); /* handler */
854 9a4d9388 Roman Shaposhnik
    put_tag(pb, hdlr_type); /* handler type */
855 f578f938 Thomas Raivio
    put_be32(pb ,0); /* reserved */
856
    put_be32(pb ,0); /* reserved */
857
    put_be32(pb ,0); /* reserved */
858 9a4d9388 Roman Shaposhnik
    put_byte(pb, strlen(descr)); /* string counter */
859
    put_buffer(pb, descr, strlen(descr)); /* handler description */
860
    return updateSize(pb, pos);
861
}
862
863
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
864
{
865 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
866 9a4d9388 Roman Shaposhnik
    put_be32(pb, 0); /* size */
867
    put_tag(pb, "minf");
868 1cb5f7fd Michael Niedermayer
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
869 9a4d9388 Roman Shaposhnik
        mov_write_vmhd_tag(pb);
870 1cb5f7fd Michael Niedermayer
    else
871 9a4d9388 Roman Shaposhnik
        mov_write_smhd_tag(pb);
872
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
873
        mov_write_hdlr_tag(pb, NULL);
874
    mov_write_dinf_tag(pb);
875
    mov_write_stbl_tag(pb, track);
876 f578f938 Thomas Raivio
    return updateSize(pb, pos);
877 1cb5f7fd Michael Niedermayer
}
878
879 6e6d6dc0 Michael Niedermayer
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
880 1cb5f7fd Michael Niedermayer
{
881 a3a80ddc Baptiste Coudurier
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
882
883
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
884 1cb5f7fd Michael Niedermayer
    put_tag(pb, "mdhd");
885 a3a80ddc Baptiste Coudurier
    put_byte(pb, version);
886
    put_be24(pb, 0); /* flags */
887
    if (version == 1) {
888
        put_be64(pb, track->time);
889
        put_be64(pb, track->time);
890
    } else {
891
        put_be32(pb, track->time); /* creation time */
892
        put_be32(pb, track->time); /* modification time */
893
    }
894 115329f1 Diego Biurrun
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
895 a3a80ddc Baptiste Coudurier
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
896 ab561df9 François Revol
    put_be16(pb, track->language); /* language */
897 1cb5f7fd Michael Niedermayer
    put_be16(pb, 0); /* reserved (quality) */
898 db8f4a92 Michael Niedermayer
899
    if(version!=0 && track->mode == MODE_MOV){
900
        av_log(NULL, AV_LOG_ERROR,
901
            "FATAL error, file duration too long for timebase, this file will not be\n"
902 b1b64c23 Aurelien Jacobs
            "playable with quicktime. Choose a different timebase or a different\n"
903 db8f4a92 Michael Niedermayer
            "container format\n");
904
    }
905
906 1cb5f7fd Michael Niedermayer
    return 32;
907
}
908
909 6e6d6dc0 Michael Niedermayer
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
910 1cb5f7fd Michael Niedermayer
{
911 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
912 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
913
    put_tag(pb, "mdia");
914 6e6d6dc0 Michael Niedermayer
    mov_write_mdhd_tag(pb, track);
915
    mov_write_hdlr_tag(pb, track);
916
    mov_write_minf_tag(pb, track);
917
    return updateSize(pb, pos);
918 1cb5f7fd Michael Niedermayer
}
919
920 6e6d6dc0 Michael Niedermayer
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
921 1cb5f7fd Michael Niedermayer
{
922 a3a80ddc Baptiste Coudurier
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
923
    int version = duration < INT32_MAX ? 0 : 1;
924
925
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
926 1cb5f7fd Michael Niedermayer
    put_tag(pb, "tkhd");
927 a3a80ddc Baptiste Coudurier
    put_byte(pb, version);
928
    put_be24(pb, 0xf); /* flags (track enabled) */
929
    if (version == 1) {
930
        put_be64(pb, track->time);
931
        put_be64(pb, track->time);
932
    } else {
933
        put_be32(pb, track->time); /* creation time */
934
        put_be32(pb, track->time); /* modification time */
935
    }
936 1cb5f7fd Michael Niedermayer
    put_be32(pb, track->trackID); /* track-id */
937
    put_be32(pb, 0); /* reserved */
938 a3a80ddc Baptiste Coudurier
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
939 1cb5f7fd Michael Niedermayer
940
    put_be32(pb, 0); /* reserved */
941
    put_be32(pb, 0); /* reserved */
942
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
943
    /* Volume, only for audio */
944
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
945
        put_be16(pb, 0x0100);
946
    else
947
        put_be16(pb, 0);
948
    put_be16(pb, 0); /* reserved */
949
950
    /* Matrix structure */
951
    put_be32(pb, 0x00010000); /* reserved */
952
    put_be32(pb, 0x0); /* reserved */
953
    put_be32(pb, 0x0); /* reserved */
954
    put_be32(pb, 0x0); /* reserved */
955
    put_be32(pb, 0x00010000); /* reserved */
956
    put_be32(pb, 0x0); /* reserved */
957
    put_be32(pb, 0x0); /* reserved */
958
    put_be32(pb, 0x0); /* reserved */
959
    put_be32(pb, 0x40000000); /* reserved */
960
961
    /* Track width and height, for visual only */
962
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
963 69dde1ad Gildas Bazin
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
964
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
965
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
966 f578f938 Thomas Raivio
        put_be32(pb, track->enc->height*0x10000);
967 1cb5f7fd Michael Niedermayer
    }
968
    else {
969
        put_be32(pb, 0);
970
        put_be32(pb, 0);
971
    }
972
    return 0x5c;
973
}
974
975 8af18154 tjcannell@blueyonder.co.uk
// This box seems important for the psp playback ... without it the movie seems to hang
976
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
977
{
978
    put_be32(pb, 0x24); /* size  */
979
    put_tag(pb, "edts");
980
    put_be32(pb, 0x1c); /* size  */
981
    put_tag(pb, "elst");
982
    put_be32(pb, 0x0);
983
    put_be32(pb, 0x1);
984
985 83fa2ef8 Michael Niedermayer
    put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration   ... doesn't seem to effect psp */
986 8af18154 tjcannell@blueyonder.co.uk
987 72289140 Baptiste Coudurier
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
988 8af18154 tjcannell@blueyonder.co.uk
    put_be32(pb, 0x00010000);
989
    return 0x24;
990
}
991
992
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
993
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
994
{
995
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
996
    put_tag(pb, "uuid");
997
    put_tag(pb, "USMT");
998
    put_be32(pb, 0x21d24fce);
999
    put_be32(pb, 0xbb88695c);
1000
    put_be32(pb, 0xfac9c740);
1001
    put_be32(pb, 0x1c);     // another size here!
1002
    put_tag(pb, "MTDT");
1003
    put_be32(pb, 0x00010012);
1004
    put_be32(pb, 0x0a);
1005
    put_be32(pb, 0x55c40000);
1006
    put_be32(pb, 0x1);
1007
    put_be32(pb, 0x0);
1008
    return 0x34;
1009
}
1010
1011 6e6d6dc0 Michael Niedermayer
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
1012 1cb5f7fd Michael Niedermayer
{
1013 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1014 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
1015
    put_tag(pb, "trak");
1016 6e6d6dc0 Michael Niedermayer
    mov_write_tkhd_tag(pb, track);
1017 c6e722e5 Baptiste Coudurier
    if (track->mode == MODE_PSP || track->hasBframes)
1018 8af18154 tjcannell@blueyonder.co.uk
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
1019 6e6d6dc0 Michael Niedermayer
    mov_write_mdia_tag(pb, track);
1020 115329f1 Diego Biurrun
    if (track->mode == MODE_PSP)
1021 8af18154 tjcannell@blueyonder.co.uk
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
1022 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
1023 1cb5f7fd Michael Niedermayer
}
1024
1025 88730be6 Måns Rullgård
#if 0
1026 1cb5f7fd Michael Niedermayer
/* TODO: Not sorted out, but not necessary either */
1027 6e6d6dc0 Michael Niedermayer
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
1028 1cb5f7fd Michael Niedermayer
{
1029
    put_be32(pb, 0x15); /* size */
1030
    put_tag(pb, "iods");
1031
    put_be32(pb, 0);    /* version & flags */
1032
    put_be16(pb, 0x1007);
1033
    put_byte(pb, 0);
1034
    put_be16(pb, 0x4fff);
1035
    put_be16(pb, 0xfffe);
1036
    put_be16(pb, 0x01ff);
1037
    return 0x15;
1038
}
1039 88730be6 Måns Rullgård
#endif
1040 1cb5f7fd Michael Niedermayer
1041 6e6d6dc0 Michael Niedermayer
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1042 1cb5f7fd Michael Niedermayer
{
1043 b29af723 Michael Niedermayer
    int maxTrackID = 1, i;
1044
    int64_t maxTrackLenTemp, maxTrackLen = 0;
1045 a3a80ddc Baptiste Coudurier
    int version;
1046 1cb5f7fd Michael Niedermayer
1047 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1048 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1049 83fa2ef8 Michael Niedermayer
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1050 f578f938 Thomas Raivio
            if(maxTrackLen < maxTrackLenTemp)
1051
                maxTrackLen = maxTrackLenTemp;
1052 1cb5f7fd Michael Niedermayer
            if(maxTrackID < mov->tracks[i].trackID)
1053
                maxTrackID = mov->tracks[i].trackID;
1054
        }
1055
    }
1056 a3a80ddc Baptiste Coudurier
1057
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
1058
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1059
    put_tag(pb, "mvhd");
1060
    put_byte(pb, version);
1061
    put_be24(pb, 0); /* flags */
1062
    if (version == 1) {
1063
        put_be64(pb, mov->time);
1064
        put_be64(pb, mov->time);
1065
    } else {
1066
        put_be32(pb, mov->time); /* creation time */
1067
        put_be32(pb, mov->time); /* modification time */
1068
    }
1069
    put_be32(pb, mov->timescale); /* timescale */
1070
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1071 1cb5f7fd Michael Niedermayer
1072
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1073
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1074
    put_be16(pb, 0); /* reserved */
1075
    put_be32(pb, 0); /* reserved */
1076
    put_be32(pb, 0); /* reserved */
1077
1078
    /* Matrix structure */
1079
    put_be32(pb, 0x00010000); /* reserved */
1080
    put_be32(pb, 0x0); /* reserved */
1081
    put_be32(pb, 0x0); /* reserved */
1082
    put_be32(pb, 0x0); /* reserved */
1083
    put_be32(pb, 0x00010000); /* reserved */
1084
    put_be32(pb, 0x0); /* reserved */
1085
    put_be32(pb, 0x0); /* reserved */
1086
    put_be32(pb, 0x0); /* reserved */
1087
    put_be32(pb, 0x40000000); /* reserved */
1088
1089
    put_be32(pb, 0); /* reserved (preview time) */
1090
    put_be32(pb, 0); /* reserved (preview duration) */
1091
    put_be32(pb, 0); /* reserved (poster time) */
1092
    put_be32(pb, 0); /* reserved (selection time) */
1093
    put_be32(pb, 0); /* reserved (selection duration) */
1094
    put_be32(pb, 0); /* reserved (current time) */
1095
    put_be32(pb, maxTrackID+1); /* Next track id */
1096
    return 0x6c;
1097
}
1098
1099 b6c50eb1 Patrice Bensoussan
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1100
                                     AVFormatContext *s)
1101
{
1102 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1103 b6c50eb1 Patrice Bensoussan
    put_be32(pb, 0); /* size */
1104
    put_tag(pb, "hdlr");
1105
    put_be32(pb, 0);
1106
    put_be32(pb, 0);
1107
    put_tag(pb, "mdir");
1108
    put_tag(pb, "appl");
1109
    put_be32(pb, 0);
1110
    put_be32(pb, 0);
1111
    put_be16(pb, 0);
1112
    return updateSize(pb, pos);
1113
}
1114
1115
/* helper function to write a data tag with the specified string as data */
1116 efda3395 Michael Niedermayer
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1117 b6c50eb1 Patrice Bensoussan
{
1118 efda3395 Michael Niedermayer
    if(long_style){
1119 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1120 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1121 efda3395 Michael Niedermayer
        put_tag(pb, "data");
1122
        put_be32(pb, 1);
1123
        put_be32(pb, 0);
1124
        put_buffer(pb, data, strlen(data));
1125
        return updateSize(pb, pos);
1126
    }else{
1127
        put_be16(pb, strlen(data)); /* string length */
1128
        put_be16(pb, 0);
1129
        put_buffer(pb, data, strlen(data));
1130
        return strlen(data) + 4;
1131 b6c50eb1 Patrice Bensoussan
    }
1132
}
1133
1134 a97c1e93 Stefan Huehner
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1135 b6c50eb1 Patrice Bensoussan
    int size = 0;
1136 efda3395 Michael Niedermayer
    if ( value && value[0] ) {
1137 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1138 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1139 efda3395 Michael Niedermayer
        put_tag(pb, name);
1140
        mov_write_string_data_tag(pb, value, long_style);
1141
        size= updateSize(pb, pos);
1142 b6c50eb1 Patrice Bensoussan
    }
1143
    return size;
1144
}
1145
1146
/* iTunes year */
1147 efda3395 Michael Niedermayer
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1148 b6c50eb1 Patrice Bensoussan
{
1149 efda3395 Michael Niedermayer
    if(year){
1150
        char year_str[5];
1151
        snprintf(year_str, sizeof(year_str), "%04d", year);
1152
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1153
    }else
1154
        return 0;
1155 b6c50eb1 Patrice Bensoussan
}
1156
1157
/* iTunes track number */
1158
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1159
                              AVFormatContext *s)
1160
{
1161
    int size = 0;
1162
    if ( s->track ) {
1163 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1164 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1165
        put_tag(pb, "trkn");
1166
        {
1167 b29af723 Michael Niedermayer
            offset_t pos = url_ftell(pb);
1168 b6c50eb1 Patrice Bensoussan
            put_be32(pb, 0); /* size */
1169
            put_tag(pb, "data");
1170
            put_be32(pb, 0);        // 8 bytes empty
1171
            put_be32(pb, 0);
1172
            put_be16(pb, 0);        // empty
1173
            put_be16(pb, s->track); // track number
1174
            put_be16(pb, 0);        // total track number
1175
            put_be16(pb, 0);        // empty
1176
            updateSize(pb, pos);
1177
        }
1178
        size = updateSize(pb, pos);
1179
    }
1180
    return size;
1181
}
1182
1183
/* iTunes meta data list */
1184
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1185
                              AVFormatContext *s)
1186
{
1187 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1188 b6c50eb1 Patrice Bensoussan
    put_be32(pb, 0); /* size */
1189
    put_tag(pb, "ilst");
1190 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1191
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1192
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1193
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1194
    mov_write_day_tag(pb, s->year ,1);
1195 6f276720 Michael Niedermayer
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1196
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1197 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1198
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1199 b6c50eb1 Patrice Bensoussan
    mov_write_trkn_tag(pb, mov, s);
1200
    return updateSize(pb, pos);
1201
}
1202
1203
/* iTunes meta data tag */
1204
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1205
                              AVFormatContext *s)
1206
{
1207
    int size = 0;
1208
1209
    // only save meta tag if required
1210 115329f1 Diego Biurrun
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1211 b6c50eb1 Patrice Bensoussan
         s->comment[0] || s->genre[0] || s->track ) {
1212 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1213 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1214
        put_tag(pb, "meta");
1215
        put_be32(pb, 0);
1216
        mov_write_itunes_hdlr_tag(pb, mov, s);
1217
        mov_write_ilst_tag(pb, mov, s);
1218
        size = updateSize(pb, pos);
1219
    }
1220
    return size;
1221
}
1222 115329f1 Diego Biurrun
1223 69dde1ad Gildas Bazin
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1224
                              AVFormatContext *s)
1225
{
1226 89a0d790 Benoit Fouet
    int i, req = 0;
1227
1228
    /* Requirements */
1229
    for (i=0; i<mov->nb_streams; i++) {
1230
        if(mov->tracks[i].entry <= 0) continue;
1231
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1232
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1233
            req = 1;
1234
            break;
1235
        }
1236
    }
1237
1238
    if (s->title[0]   || s->author[0] || s->album[0] || s->year ||
1239
        s->comment[0] || s->genre[0]  || s->track ||
1240
       (mov->mode == MODE_MOV &&
1241
      ((mov->tracks[0].enc && !mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT) || req))) {
1242 cfac9ec7 Benoit Fouet
        offset_t pos = url_ftell(pb);
1243 69dde1ad Gildas Bazin
1244 cfac9ec7 Benoit Fouet
        put_be32(pb, 0); /* size */
1245
        put_tag(pb, "udta");
1246 69dde1ad Gildas Bazin
1247 cfac9ec7 Benoit Fouet
        /* iTunes meta data */
1248
        mov_write_meta_tag(pb, mov, s);
1249 b6c50eb1 Patrice Bensoussan
1250 cfac9ec7 Benoit Fouet
        if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1251
            /* Requirements */
1252
            if (req)
1253 00996920 Baptiste Coudurier
                mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1254 69dde1ad Gildas Bazin
1255 cfac9ec7 Benoit Fouet
            mov_write_string_tag(pb, "\251nam", s->title         , 0);
1256
            mov_write_string_tag(pb, "\251aut", s->author        , 0);
1257
            mov_write_string_tag(pb, "\251alb", s->album         , 0);
1258
            mov_write_day_tag(pb, s->year, 0);
1259
            if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1260
                mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1261
            mov_write_string_tag(pb, "\251des", s->comment       , 0);
1262
            mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1263
        }
1264 69dde1ad Gildas Bazin
1265 cfac9ec7 Benoit Fouet
        return updateSize(pb, pos);
1266 89a0d790 Benoit Fouet
    }
1267
1268
    return 0;
1269 69dde1ad Gildas Bazin
}
1270
1271 79e5a53c Michael Niedermayer
static int utf8len(uint8_t *b){
1272
    int len=0;
1273
    int val;
1274
    while(*b){
1275
        GET_UTF8(val, *b++, return -1;)
1276
        len++;
1277
    }
1278
    return len;
1279
}
1280 dcfdb046 Benjamin Larsson
1281 79e5a53c Michael Niedermayer
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1282 dcfdb046 Benjamin Larsson
{
1283 79e5a53c Michael Niedermayer
    int val;
1284
    while(*b){
1285
        GET_UTF8(val, *b++, return -1;)
1286
        put_be16(pb, val);
1287 dcfdb046 Benjamin Larsson
    }
1288
    put_be16(pb, 0x00);
1289 79e5a53c Michael Niedermayer
    return 0;
1290 dcfdb046 Benjamin Larsson
}
1291
1292 a97c1e93 Stefan Huehner
static uint16_t language_code (const char *str)
1293 dcfdb046 Benjamin Larsson
{
1294 7cf0e16f Michael Niedermayer
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1295 dcfdb046 Benjamin Larsson
}
1296
1297
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1298
{
1299
    size_t len, size;
1300
    offset_t pos, curpos;
1301
1302
    size = 0;
1303
    if (s->title[0]) {
1304
        pos = url_ftell(pb);
1305
        put_be32(pb, 0); /* size placeholder*/
1306
        put_tag(pb, "uuid");
1307
        put_tag(pb, "USMT");
1308
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1309
        put_be32(pb, 0xbb88695c );
1310
        put_be32(pb, 0xfac9c740 );
1311
        size += 24;
1312
1313
        put_be32(pb, 0); /* size placeholder*/
1314
        put_tag(pb, "MTDT");
1315 7cf0e16f Michael Niedermayer
        put_be16(pb, 4);
1316 dcfdb046 Benjamin Larsson
        size += 10;
1317
1318 7cf0e16f Michael Niedermayer
        // ?
1319
        put_be16(pb, 0x0C);                 /* size */
1320
        put_be32(pb, 0x0B);                 /* type */
1321
        put_be16(pb, language_code("und")); /* language */
1322
        put_be16(pb, 0x0);                  /* ? */
1323
        put_be16(pb, 0x021C);               /* data */
1324
        size += 12;
1325
1326
        // Encoder
1327 79e5a53c Michael Niedermayer
        len = utf8len(LIBAVCODEC_IDENT)+1;
1328
        if(len<=0)
1329
            goto not_utf8;
1330 7cf0e16f Michael Niedermayer
        put_be16(pb, len*2+10);             /* size */
1331
        put_be32(pb, 0x04);                 /* type */
1332
        put_be16(pb, language_code("eng")); /* language */
1333
        put_be16(pb, 0x01);                 /* ? */
1334 79e5a53c Michael Niedermayer
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1335 7cf0e16f Michael Niedermayer
        size += len*2+10;
1336
1337 dcfdb046 Benjamin Larsson
        // Title
1338 79e5a53c Michael Niedermayer
        len = utf8len(s->title)+1;
1339
        if(len<=0)
1340
            goto not_utf8;
1341 dcfdb046 Benjamin Larsson
        put_be16(pb, len*2+10);             /* size */
1342
        put_be32(pb, 0x01);                 /* type */
1343 7cf0e16f Michael Niedermayer
        put_be16(pb, language_code("eng")); /* language */
1344 dcfdb046 Benjamin Larsson
        put_be16(pb, 0x01);                 /* ? */
1345 79e5a53c Michael Niedermayer
        ascii_to_wc (pb, s->title);
1346 dcfdb046 Benjamin Larsson
        size += len*2+10;
1347
1348 7cf0e16f Michael Niedermayer
        // Date
1349
//        snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
1350 79e5a53c Michael Niedermayer
        len = utf8len("2006/04/01 11:11:11")+1;
1351
        if(len<=0)
1352
            goto not_utf8;
1353 7cf0e16f Michael Niedermayer
        put_be16(pb, len*2+10);    /* size */
1354
        put_be32(pb, 0x03);        /* type */
1355
        put_be16(pb, language_code("und")); /* language */
1356
        put_be16(pb, 0x01);        /* ? */
1357 79e5a53c Michael Niedermayer
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1358 7cf0e16f Michael Niedermayer
        size += len*2+10;
1359
1360 dcfdb046 Benjamin Larsson
        // size
1361
        curpos = url_ftell(pb);
1362
        url_fseek(pb, pos, SEEK_SET);
1363
        put_be32(pb, size);
1364
        url_fseek(pb, pos+24, SEEK_SET);
1365
        put_be32(pb, size-24);
1366
        url_fseek(pb, curpos, SEEK_SET);
1367
    }
1368
1369
    return size;
1370 79e5a53c Michael Niedermayer
not_utf8:
1371
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1372
    return -1;
1373 dcfdb046 Benjamin Larsson
}
1374
1375 69dde1ad Gildas Bazin
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1376
                              AVFormatContext *s)
1377 1cb5f7fd Michael Niedermayer
{
1378 b29af723 Michael Niedermayer
    int i;
1379
    offset_t pos = url_ftell(pb);
1380 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size placeholder*/
1381
    put_tag(pb, "moov");
1382
    mov->timescale = globalTimescale;
1383
1384 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1385 e45ccf79 Gildas Bazin
        if(mov->tracks[i].entry <= 0) continue;
1386
1387
        mov->tracks[i].time = mov->time;
1388
        mov->tracks[i].trackID = i+1;
1389 1cb5f7fd Michael Niedermayer
    }
1390
1391 6e6d6dc0 Michael Niedermayer
    mov_write_mvhd_tag(pb, mov);
1392
    //mov_write_iods_tag(pb, mov);
1393 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1394 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1395 6e6d6dc0 Michael Niedermayer
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1396 1cb5f7fd Michael Niedermayer
        }
1397
    }
1398
1399 dcfdb046 Benjamin Larsson
    if (mov->mode == MODE_PSP)
1400
        mov_write_uuidusmt_tag(pb, s);
1401 86161cec Baptiste Coudurier
    else if (mov->mode != MODE_3GP && mov->mode != MODE_3G2)
1402 e344c1ea Steve L'Homme
        mov_write_udta_tag(pb, mov, s);
1403 69dde1ad Gildas Bazin
1404 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
1405 1cb5f7fd Michael Niedermayer
}
1406
1407 7b49ce2e Stefan Huehner
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1408 1cb5f7fd Michael Niedermayer
{
1409 b29af723 Michael Niedermayer
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1410 5340a0bc Baptiste Coudurier
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1411 b29af723 Michael Niedermayer
1412 115329f1 Diego Biurrun
    mov->mdat_pos = url_ftell(pb);
1413 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size placeholder*/
1414
    put_tag(pb, "mdat");
1415
    return 0;
1416
}
1417
1418
/* TODO: This needs to be more general */
1419 dcfdb046 Benjamin Larsson
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1420 1cb5f7fd Michael Niedermayer
{
1421 69dde1ad Gildas Bazin
    MOVContext *mov = s->priv_data;
1422
1423 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0x14 ); /* size */
1424
    put_tag(pb, "ftyp");
1425 e45ccf79 Gildas Bazin
1426 69dde1ad Gildas Bazin
    if ( mov->mode == MODE_3GP )
1427 e45ccf79 Gildas Bazin
        put_tag(pb, "3gp4");
1428 8536ab89 tjraivio@cc.hut.fi
    else if ( mov->mode == MODE_3G2 )
1429
        put_tag(pb, "3g2a");
1430 8af18154 tjcannell@blueyonder.co.uk
    else if ( mov->mode == MODE_PSP )
1431
        put_tag(pb, "MSNV");
1432 26f86eaf Baptiste Coudurier
    else if ( mov->mode == MODE_MP4 )
1433 e45ccf79 Gildas Bazin
        put_tag(pb, "isom");
1434 26f86eaf Baptiste Coudurier
    else
1435
        put_tag(pb, "qt  ");
1436 e45ccf79 Gildas Bazin
1437 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0x200 );
1438 e45ccf79 Gildas Bazin
1439 69dde1ad Gildas Bazin
    if ( mov->mode == MODE_3GP )
1440 e45ccf79 Gildas Bazin
        put_tag(pb, "3gp4");
1441 8536ab89 tjraivio@cc.hut.fi
    else if ( mov->mode == MODE_3G2 )
1442
        put_tag(pb, "3g2a");
1443 8af18154 tjcannell@blueyonder.co.uk
    else if ( mov->mode == MODE_PSP )
1444
        put_tag(pb, "MSNV");
1445 26f86eaf Baptiste Coudurier
    else if ( mov->mode == MODE_MP4 )
1446 e45ccf79 Gildas Bazin
        put_tag(pb, "mp41");
1447 26f86eaf Baptiste Coudurier
    else
1448
        put_tag(pb, "qt  ");
1449 1cb5f7fd Michael Niedermayer
}
1450
1451 0c716ab7 François Revol
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1452 8af18154 tjcannell@blueyonder.co.uk
{
1453 dcfdb046 Benjamin Larsson
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1454
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1455
    int AudioRate = AudioCodec->sample_rate;
1456
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1457 9e282ba3 Michael Niedermayer
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1458
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1459 8af18154 tjcannell@blueyonder.co.uk
1460
    put_be32(pb, 0x94 ); /* size */
1461
    put_tag(pb, "uuid");
1462
    put_tag(pb, "PROF");
1463
1464
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1465
    put_be32(pb, 0xbb88695c );
1466
    put_be32(pb, 0xfac9c740 );
1467
1468
    put_be32(pb, 0x0 );  /* ? */
1469
    put_be32(pb, 0x3 );  /* 3 sections ? */
1470
1471
    put_be32(pb, 0x14 ); /* size */
1472
    put_tag(pb, "FPRF");
1473
    put_be32(pb, 0x0 );  /* ? */
1474
    put_be32(pb, 0x0 );  /* ? */
1475
    put_be32(pb, 0x0 );  /* ? */
1476
1477
    put_be32(pb, 0x2c );  /* size */
1478
    put_tag(pb, "APRF");   /* audio */
1479
    put_be32(pb, 0x0 );
1480 dcfdb046 Benjamin Larsson
    put_be32(pb, 0x2 );   /* TrackID */
1481 8af18154 tjcannell@blueyonder.co.uk
    put_tag(pb, "mp4a");
1482
    put_be32(pb, 0x20f );
1483
    put_be32(pb, 0x0 );
1484 9e282ba3 Michael Niedermayer
    put_be32(pb, audio_kbitrate);
1485
    put_be32(pb, audio_kbitrate);
1486 dcfdb046 Benjamin Larsson
    put_be32(pb, AudioRate );
1487
    put_be32(pb, AudioCodec->channels );
1488 8af18154 tjcannell@blueyonder.co.uk
1489
    put_be32(pb, 0x34 );  /* size */
1490
    put_tag(pb, "VPRF");   /* video */
1491
    put_be32(pb, 0x0 );
1492 dcfdb046 Benjamin Larsson
    put_be32(pb, 0x1 );    /* TrackID */
1493 87b041e0 Michael Niedermayer
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1494
        put_tag(pb, "avc1");
1495
        put_be16(pb, 0x014D );
1496
        put_be16(pb, 0x0015 );
1497
    } else {
1498
        put_tag(pb, "mp4v");
1499
        put_be16(pb, 0x0000 );
1500
        put_be16(pb, 0x0103 );
1501
    }
1502 8af18154 tjcannell@blueyonder.co.uk
    put_be32(pb, 0x0 );
1503 9e282ba3 Michael Niedermayer
    put_be32(pb, video_kbitrate);
1504
    put_be32(pb, video_kbitrate);
1505 dcfdb046 Benjamin Larsson
    put_be32(pb, FrameRate);
1506
    put_be32(pb, FrameRate);
1507
    put_be16(pb, VideoCodec->width);
1508
    put_be16(pb, VideoCodec->height);
1509
    put_be32(pb, 0x010001); /* ? */
1510 8af18154 tjcannell@blueyonder.co.uk
}
1511
1512 1cb5f7fd Michael Niedermayer
static int mov_write_header(AVFormatContext *s)
1513
{
1514 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1515 69dde1ad Gildas Bazin
    MOVContext *mov = s->priv_data;
1516
    int i;
1517 1cb5f7fd Michael Niedermayer
1518 899681cd Björn Axelsson
    if (url_is_streamed(s->pb)) {
1519 4f60f34a Baptiste Coudurier
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
1520
        return -1;
1521
    }
1522
1523 1522767d Baptiste Coudurier
    /* Default mode == MP4 */
1524
    mov->mode = MODE_MP4;
1525
1526
    if (s->oformat != NULL) {
1527
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1528
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1529
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1530
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1531
1532 26f86eaf Baptiste Coudurier
        mov_write_ftyp_tag(pb,s);
1533 1522767d Baptiste Coudurier
        if ( mov->mode == MODE_PSP ) {
1534
            if ( s->nb_streams != 2 ) {
1535
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1536
                return -1;
1537
            }
1538
            mov_write_uuidprof_tag(pb,s);
1539
        }
1540
    }
1541
1542 ffdd57d4 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1543 039627cf Baptiste Coudurier
        AVStream *st= s->streams[i];
1544
        MOVTrack *track= &mov->tracks[i];
1545
1546
        track->enc = st->codec;
1547 2c5ad5fd Baptiste Coudurier
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1548
        track->mode = mov->mode;
1549 039627cf Baptiste Coudurier
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1550
            track->tag = mov_find_video_codec_tag(s, track);
1551 ffb512e3 Baptiste Coudurier
            track->timescale = st->codec->time_base.den;
1552 039627cf Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1553 911f9b71 Baptiste Coudurier
            if (track->timescale > 100000)
1554
                av_log(NULL, AV_LOG_WARNING,
1555
                       "WARNING codec timebase is very high. If duration is too long,\n"
1556
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
1557
                       "or choose different container.\n");
1558 039627cf Baptiste Coudurier
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1559
            track->tag = mov_find_audio_codec_tag(s, track);
1560 ffb512e3 Baptiste Coudurier
            track->timescale = st->codec->sample_rate;
1561 039627cf Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1562 4278662d Baptiste Coudurier
            if(!st->codec->frame_size){
1563 b596102e Baptiste Coudurier
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1564
                return -1;
1565 4278662d Baptiste Coudurier
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1566
                track->audio_vbr = 1;
1567
            }else{
1568
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1569 b596102e Baptiste Coudurier
            }
1570 ffb512e3 Baptiste Coudurier
        }
1571 f578f938 Thomas Raivio
    }
1572
1573 18c697ac Baptiste Coudurier
    mov_write_mdat_tag(pb, mov);
1574
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1575 42fb4148 Baptiste Coudurier
    mov->nb_streams = s->nb_streams;
1576 18c697ac Baptiste Coudurier
1577 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
1578
1579
    return 0;
1580
}
1581
1582 e928649b Michael Niedermayer
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1583 1cb5f7fd Michael Niedermayer
{
1584
    MOVContext *mov = s->priv_data;
1585 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1586 039627cf Baptiste Coudurier
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1587
    AVCodecContext *enc = trk->enc;
1588 e45ccf79 Gildas Bazin
    unsigned int samplesInChunk = 0;
1589 e928649b Michael Niedermayer
    int size= pkt->size;
1590 1cb5f7fd Michael Niedermayer
1591 899681cd Björn Axelsson
    if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1592 e45ccf79 Gildas Bazin
    if (!size) return 0; /* Discard 0 sized packets */
1593 1cb5f7fd Michael Niedermayer
1594 06fcf56c Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1595 5cb49ca1 Baptiste Coudurier
        /* We must find out how many AMR blocks there are in one packet */
1596
        static uint16_t packed_size[16] =
1597
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1598
        int len = 0;
1599
1600
        while (len < size && samplesInChunk < 100) {
1601
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1602
            samplesInChunk++;
1603 1cb5f7fd Michael Niedermayer
        }
1604 2111e3f9 Michael Niedermayer
        if(samplesInChunk > 1){
1605 89938d29 Ramiro Polla
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1606 2111e3f9 Michael Niedermayer
            return -1;
1607
        }
1608 5cb49ca1 Baptiste Coudurier
    } else if (trk->sampleSize)
1609
        samplesInChunk = size/trk->sampleSize;
1610
    else
1611 5616f85d Baptiste Coudurier
        samplesInChunk = 1;
1612 e45ccf79 Gildas Bazin
1613 5616f85d Baptiste Coudurier
    /* copy extradata if it exists */
1614
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1615 e45ccf79 Gildas Bazin
        trk->vosLen = enc->extradata_size;
1616
        trk->vosData = av_malloc(trk->vosLen);
1617
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1618
    }
1619
1620 5616f85d Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1621
        /* from x264 or from bytestream h264 */
1622
        /* nal reformating needed */
1623
        avc_parse_nal_units(&pkt->data, &pkt->size);
1624
        assert(pkt->size);
1625
        size = pkt->size;
1626 fc4cbc16 Baptiste Coudurier
    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
1627
        /* copy frame header to create needed atoms */
1628
        if (size < 640)
1629
            return -1;
1630
        trk->vosLen = 640;
1631
        trk->vosData = av_malloc(trk->vosLen);
1632
        memcpy(trk->vosData, pkt->data, 640);
1633 c1b8e6d8 Baptiste Coudurier
    }
1634
1635 0aec3c5c Baptiste Coudurier
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1636
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1637 e45ccf79 Gildas Bazin
        if (!trk->cluster)
1638
            return -1;
1639
    }
1640
1641 0aec3c5c Baptiste Coudurier
    trk->cluster[trk->entry].pos = url_ftell(pb);
1642
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1643
    trk->cluster[trk->entry].size = size;
1644
    trk->cluster[trk->entry].entries = samplesInChunk;
1645 aa90239f Baptiste Coudurier
    trk->cluster[trk->entry].dts = pkt->dts;
1646
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1647
1648 e45ccf79 Gildas Bazin
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1649 b4712e3c Baptiste Coudurier
        if (pkt->dts != pkt->pts)
1650
            trk->hasBframes = 1;
1651 0aec3c5c Baptiste Coudurier
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1652
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1653
        if(trk->cluster[trk->entry].key_frame)
1654 32ba6fb1 Baptiste Coudurier
            trk->hasKeyframes++;
1655 1cb5f7fd Michael Niedermayer
    }
1656 e45ccf79 Gildas Bazin
    trk->entry++;
1657
    trk->sampleCount += samplesInChunk;
1658 039627cf Baptiste Coudurier
    mov->mdat_size += size;
1659 e45ccf79 Gildas Bazin
1660 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, size);
1661 1cb5f7fd Michael Niedermayer
1662
    put_flush_packet(pb);
1663
    return 0;
1664
}
1665
1666
static int mov_write_trailer(AVFormatContext *s)
1667
{
1668
    MOVContext *mov = s->priv_data;
1669 899681cd Björn Axelsson
    ByteIOContext *pb = s->pb;
1670 1cb5f7fd Michael Niedermayer
    int res = 0;
1671 0aec3c5c Baptiste Coudurier
    int i;
1672 1cb5f7fd Michael Niedermayer
1673 69dde1ad Gildas Bazin
    offset_t moov_pos = url_ftell(pb);
1674 1cb5f7fd Michael Niedermayer
1675
    /* Write size of mdat tag */
1676 039627cf Baptiste Coudurier
    if (mov->mdat_size+8 <= UINT32_MAX) {
1677 b29af723 Michael Niedermayer
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1678 039627cf Baptiste Coudurier
        put_be32(pb, mov->mdat_size+8);
1679 b29af723 Michael Niedermayer
    } else {
1680
        /* overwrite 'wide' placeholder atom */
1681
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1682
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1683
        put_tag(pb, "mdat");
1684 039627cf Baptiste Coudurier
        put_be64(pb, mov->mdat_size+16);
1685 b29af723 Michael Niedermayer
    }
1686 69dde1ad Gildas Bazin
    url_fseek(pb, moov_pos, SEEK_SET);
1687 1cb5f7fd Michael Niedermayer
1688 69dde1ad Gildas Bazin
    mov_write_moov_tag(pb, mov, s);
1689 1cb5f7fd Michael Niedermayer
1690 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1691 0aec3c5c Baptiste Coudurier
        av_freep(&mov->tracks[i].cluster);
1692
1693 ec7d0d2e Gildas Bazin
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1694
1695 1cb5f7fd Michael Niedermayer
    }
1696 69dde1ad Gildas Bazin
1697 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
1698
1699
    return res;
1700
}
1701
1702 ff70e601 Måns Rullgård
#ifdef CONFIG_MOV_MUXER
1703
AVOutputFormat mov_muxer = {
1704 1cb5f7fd Michael Niedermayer
    "mov",
1705
    "mov format",
1706
    NULL,
1707
    "mov",
1708
    sizeof(MOVContext),
1709 69dde1ad Gildas Bazin
    CODEC_ID_AAC,
1710 2187d948 Michael Niedermayer
    CODEC_ID_MPEG4,
1711 1cb5f7fd Michael Niedermayer
    mov_write_header,
1712
    mov_write_packet,
1713
    mov_write_trailer,
1714 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1715 1cb5f7fd Michael Niedermayer
};
1716 ff70e601 Måns Rullgård
#endif
1717
#ifdef CONFIG_TGP_MUXER
1718
AVOutputFormat tgp_muxer = {
1719 1cb5f7fd Michael Niedermayer
    "3gp",
1720
    "3gp format",
1721
    NULL,
1722
    "3gp",
1723
    sizeof(MOVContext),
1724
    CODEC_ID_AMR_NB,
1725
    CODEC_ID_H263,
1726
    mov_write_header,
1727
    mov_write_packet,
1728
    mov_write_trailer,
1729 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1730 1cb5f7fd Michael Niedermayer
};
1731 ff70e601 Måns Rullgård
#endif
1732
#ifdef CONFIG_MP4_MUXER
1733
AVOutputFormat mp4_muxer = {
1734 1cb5f7fd Michael Niedermayer
    "mp4",
1735
    "mp4 format",
1736 4cb3f3b6 Dan Christiansen
    "application/mp4",
1737
    "mp4,m4a",
1738 1cb5f7fd Michael Niedermayer
    sizeof(MOVContext),
1739
    CODEC_ID_AAC,
1740
    CODEC_ID_MPEG4,
1741
    mov_write_header,
1742
    mov_write_packet,
1743
    mov_write_trailer,
1744 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1745 1cb5f7fd Michael Niedermayer
};
1746 ff70e601 Måns Rullgård
#endif
1747
#ifdef CONFIG_PSP_MUXER
1748
AVOutputFormat psp_muxer = {
1749 8af18154 tjcannell@blueyonder.co.uk
    "psp",
1750
    "psp mp4 format",
1751
    NULL,
1752
    "mp4,psp",
1753
    sizeof(MOVContext),
1754
    CODEC_ID_AAC,
1755
    CODEC_ID_MPEG4,
1756
    mov_write_header,
1757
    mov_write_packet,
1758
    mov_write_trailer,
1759 dcfdb046 Benjamin Larsson
    .flags = AVFMT_GLOBALHEADER,
1760 8af18154 tjcannell@blueyonder.co.uk
};
1761 ff70e601 Måns Rullgård
#endif
1762
#ifdef CONFIG_TG2_MUXER
1763
AVOutputFormat tg2_muxer = {
1764 8536ab89 tjraivio@cc.hut.fi
    "3g2",
1765
    "3gp2 format",
1766
    NULL,
1767
    "3g2",
1768
    sizeof(MOVContext),
1769
    CODEC_ID_AMR_NB,
1770
    CODEC_ID_H263,
1771
    mov_write_header,
1772
    mov_write_packet,
1773
    mov_write_trailer,
1774 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1775 8536ab89 tjraivio@cc.hut.fi
};
1776 ff70e601 Måns Rullgård
#endif