Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ a23c9c4a

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