Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 1afdcdf1

History | View | Annotate | Download (53.2 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 6e6d6dc0 Michael Niedermayer
//FIXME supprt 64bit varaint 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
    put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
264
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 7caf0cc6 Michael Niedermayer
static const AVCodecTag codec_movaudio_tags[] = {
326 8cc7a34d Alex Beregszaszi
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
327
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
328
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
329
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
330
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
331
    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
332
    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
333 11bec294 Jan Kratochvil
    { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
334 8cc7a34d Alex Beregszaszi
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
335
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
336 fcef991a Baptiste Coudurier
    { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
337
    { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
338
    { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
339
    { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
340 8cc7a34d Alex Beregszaszi
    { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
341 8e321619 Baptiste Coudurier
    { CODEC_ID_NONE, 0 },
342 8cc7a34d Alex Beregszaszi
};
343
344 f578f938 Thomas Raivio
static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
345 1cb5f7fd Michael Niedermayer
{
346 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
347 f45b744c Baptiste Coudurier
    int version = track->mode == MODE_MOV &&
348
        (track->audio_vbr ||
349
         track->enc->codec_id == CODEC_ID_PCM_S32LE ||
350
         track->enc->codec_id == CODEC_ID_PCM_S24LE);
351 115329f1 Diego Biurrun
352 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
353 039627cf Baptiste Coudurier
    put_le32(pb, track->tag); // store it byteswapped
354 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* Reserved */
355
    put_be16(pb, 0); /* Reserved */
356
    put_be16(pb, 1); /* Data-reference index, XXX  == 1 */
357 69dde1ad Gildas Bazin
358 e45ccf79 Gildas Bazin
    /* SoundDescription */
359 fcef991a Baptiste Coudurier
    put_be16(pb, version); /* Version */
360 e45ccf79 Gildas Bazin
    put_be16(pb, 0); /* Revision level */
361 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* Reserved */
362
363 86ca5710 Baptiste Coudurier
    put_be16(pb, track->mode == MODE_MOV ? track->enc->channels : 2); /* Number of channels */
364 80f05e79 Baptiste Coudurier
    /* FIXME 8 bit for 'raw ' in mov */
365
    put_be16(pb, 16); /* Reserved */
366 ef19c7eb Gildas Bazin
367 f45b744c Baptiste Coudurier
    put_be16(pb, track->mode == MODE_MOV && track->audio_vbr ? -2 : 0); /* compression ID */
368 9a4d9388 Roman Shaposhnik
    put_be16(pb, 0); /* packet size (= 0) */
369 1cb5f7fd Michael Niedermayer
    put_be16(pb, track->timescale); /* Time scale */
370
    put_be16(pb, 0); /* Reserved */
371
372 5cb49ca1 Baptiste Coudurier
    if(version == 1) { /* SoundDescription V1 extended info */
373
        put_be32(pb, track->enc->frame_size); /* Samples per packet */
374 9a39cd35 Michael Niedermayer
        put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
375 5cb49ca1 Baptiste Coudurier
        put_be32(pb, track->sampleSize); /* Bytes per frame */
376 aa1c1c61 Baptiste Coudurier
        put_be32(pb, 2); /* Bytes per sample */
377 69dde1ad Gildas Bazin
    }
378
379 6dd19fff Baptiste Coudurier
    if(track->mode == MODE_MOV &&
380
       (track->enc->codec_id == CODEC_ID_AAC ||
381
        track->enc->codec_id == CODEC_ID_AMR_NB ||
382
        track->enc->codec_id == CODEC_ID_PCM_S24LE ||
383
        track->enc->codec_id == CODEC_ID_PCM_S32LE))
384 fcef991a Baptiste Coudurier
        mov_write_wave_tag(pb, track);
385 3a72cbd9 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_AAC)
386
        mov_write_esds_tag(pb, track);
387
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
388 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
389 3a72cbd9 Baptiste Coudurier
390 6e6d6dc0 Michael Niedermayer
    return updateSize (pb, pos);
391 1cb5f7fd Michael Niedermayer
}
392
393 6e6d6dc0 Michael Niedermayer
static int mov_write_d263_tag(ByteIOContext *pb)
394 1cb5f7fd Michael Niedermayer
{
395
    put_be32(pb, 0xf); /* size */
396
    put_tag(pb, "d263");
397
    put_tag(pb, "FFMP");
398 87494ea0 Baptiste Coudurier
    put_byte(pb, 0); /* decoder version */
399
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
400
    put_byte(pb, 0xa); /* level */
401
    put_byte(pb, 0); /* profile */
402 1cb5f7fd Michael Niedermayer
    return 0xf;
403
}
404
405 f578f938 Thomas Raivio
/* TODO: No idea about these values */
406
static int mov_write_svq3_tag(ByteIOContext *pb)
407 1cb5f7fd Michael Niedermayer
{
408 f578f938 Thomas Raivio
    put_be32(pb, 0x15);
409
    put_tag(pb, "SMI ");
410
    put_tag(pb, "SEQH");
411
    put_be32(pb, 0x5);
412
    put_be32(pb, 0xe2c0211d);
413
    put_be32(pb, 0xc0000000);
414 115329f1 Diego Biurrun
    put_byte(pb, 0);
415 f578f938 Thomas Raivio
    return 0x15;
416 1cb5f7fd Michael Niedermayer
}
417
418 c1b8e6d8 Baptiste Coudurier
static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
419
{
420
    uint8_t *a = p + 4 - ((int)p & 3);
421
422
    for( end -= 3; p < a && p < end; p++ ) {
423
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
424
            return p;
425
    }
426
427
    for( end -= 3; p < end; p += 4 ) {
428
        uint32_t x = *(uint32_t*)p;
429
//      if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
430
//      if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
431
        if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
432
            if( p[1] == 0 ) {
433
                if( p[0] == 0 && p[2] == 1 )
434 2c95f59f Baptiste Coudurier
                    return p-1;
435 c1b8e6d8 Baptiste Coudurier
                if( p[2] == 0 && p[3] == 1 )
436 2c95f59f Baptiste Coudurier
                    return p;
437 c1b8e6d8 Baptiste Coudurier
            }
438
            if( p[3] == 0 ) {
439
                if( p[2] == 0 && p[4] == 1 )
440 2c95f59f Baptiste Coudurier
                    return p+1;
441 c1b8e6d8 Baptiste Coudurier
                if( p[4] == 0 && p[5] == 1 )
442 2c95f59f Baptiste Coudurier
                    return p+2;
443 c1b8e6d8 Baptiste Coudurier
            }
444
        }
445
    }
446
447
    for( end += 3; p < end; p++ ) {
448
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
449
            return p;
450
    }
451
452
    return end + 3;
453
}
454
455
static void avc_parse_nal_units(uint8_t **buf, int *size)
456
{
457
    ByteIOContext pb;
458
    uint8_t *p = *buf;
459
    uint8_t *end = p + *size;
460
    uint8_t *nal_start, *nal_end;
461
462
    url_open_dyn_buf(&pb);
463
    nal_start = avc_find_startcode(p, end);
464
    while (nal_start < end) {
465
        while(!*(nal_start++));
466
        nal_end = avc_find_startcode(nal_start, end);
467
        put_be32(&pb, nal_end - nal_start);
468
        put_buffer(&pb, nal_start, nal_end - nal_start);
469
        nal_start = nal_end;
470
    }
471
    av_freep(buf);
472
    *size = url_close_dyn_buf(&pb, buf);
473
}
474
475
static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
476
{
477
    offset_t pos = url_ftell(pb);
478
479
    put_be32(pb, 0);
480
    put_tag(pb, "avcC");
481
    if (track->vosLen > 6) {
482
        /* check for h264 start code */
483 fead30d4 Alex Beregszaszi
        if (AV_RB32(track->vosData) == 0x00000001) {
484 c1b8e6d8 Baptiste Coudurier
            uint8_t *buf, *end;
485
            uint32_t sps_size=0, pps_size=0;
486
            uint8_t *sps=0, *pps=0;
487
488
            avc_parse_nal_units(&track->vosData, &track->vosLen);
489
            buf = track->vosData;
490
            end = track->vosData + track->vosLen;
491
492
            /* look for sps and pps */
493
            while (buf < end) {
494
                unsigned int size;
495
                uint8_t nal_type;
496 fead30d4 Alex Beregszaszi
                size = AV_RB32(buf);
497 c1b8e6d8 Baptiste Coudurier
                nal_type = buf[4] & 0x1f;
498
                if (nal_type == 7) { /* SPS */
499
                    sps = buf + 4;
500
                    sps_size = size;
501
                } else if (nal_type == 8) { /* PPS */
502
                    pps = buf + 4;
503
                    pps_size = size;
504
                }
505
                buf += size + 4;
506
            }
507
            assert(sps);
508
            assert(pps);
509 611fd605 Baptiste Coudurier
510
            put_byte(pb, 1); /* version */
511
            put_byte(pb, sps[1]); /* profile */
512
            put_byte(pb, sps[2]); /* profile compat */
513
            put_byte(pb, sps[3]); /* level */
514
            put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
515
            put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
516
517 c1b8e6d8 Baptiste Coudurier
            put_be16(pb, sps_size);
518
            put_buffer(pb, sps, sps_size);
519
            put_byte(pb, 1); /* number of pps */
520
            put_be16(pb, pps_size);
521
            put_buffer(pb, pps, pps_size);
522
        } else {
523
            put_buffer(pb, track->vosData, track->vosLen);
524
        }
525
    }
526
    return updateSize(pb, pos);
527
}
528
529 7caf0cc6 Michael Niedermayer
static const AVCodecTag codec_movvideo_tags[] = {
530 8cc7a34d Alex Beregszaszi
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
531
    { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
532
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
533 2c5ad5fd Baptiste Coudurier
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') },
534 8cc7a34d Alex Beregszaszi
    { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
535 0c1e0bab Erik Slagter
    { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
536 8e321619 Baptiste Coudurier
    /* special handling in mov_find_video_codec_tag */
537
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
538
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
539
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL */
540
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC */
541
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL */
542
    { CODEC_ID_NONE, 0 },
543 8cc7a34d Alex Beregszaszi
};
544
545 039627cf Baptiste Coudurier
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
546 8e321619 Baptiste Coudurier
{
547 039627cf Baptiste Coudurier
    int tag = track->enc->codec_tag;
548 8e321619 Baptiste Coudurier
    if (!tag) {
549
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
550
            if (track->enc->height == 480) { /* NTSC */
551
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
552
                    tag = MKTAG('d', 'v', '5', 'n');
553
                else
554
                    tag = MKTAG('d', 'v', 'c', ' ');
555
            } else { /* assume PAL */
556
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
557
                    tag = MKTAG('d', 'v', '5', 'p');
558
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
559
                    tag = MKTAG('d', 'v', 'c', 'p');
560 3cdc7eb4 Baptiste Coudurier
                else
561
                    tag = MKTAG('d', 'v', 'p', 'p');
562 8e321619 Baptiste Coudurier
            }
563 2c5ad5fd Baptiste Coudurier
        } else if (track->enc->codec_id == CODEC_ID_H263) {
564
            if (track->mode == MODE_MOV)
565
                tag = MKTAG('h', '2', '6', '3');
566
            else
567
                tag = MKTAG('s', '2', '6', '3');
568 8e321619 Baptiste Coudurier
        } else {
569
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
570
        }
571
    }
572
    // if no mac fcc found, try with Microsoft tags
573 039627cf Baptiste Coudurier
    if (!tag) {
574 8e321619 Baptiste Coudurier
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
575 039627cf Baptiste Coudurier
        if (tag) {
576
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
577
        }
578
    }
579
    assert(tag);
580
    return tag;
581
}
582
583
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
584
{
585
    int tag = track->enc->codec_tag;
586
    if (!tag) {
587
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
588
    }
589
    // if no mac fcc found, try with Microsoft tags
590
    if (!tag) {
591
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
592
        if (ms_tag) {
593
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
594
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
595
        }
596
    }
597 8e321619 Baptiste Coudurier
    assert(tag);
598
    return tag;
599
}
600
601 f578f938 Thomas Raivio
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
602 1cb5f7fd Michael Niedermayer
{
603 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
604 53ffdd14 Roine Gustafsson
    char compressor_name[32];
605 8cc7a34d Alex Beregszaszi
606 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
607 039627cf Baptiste Coudurier
    put_le32(pb, track->tag); // store it byteswapped
608 f578f938 Thomas Raivio
    put_be32(pb, 0); /* Reserved */
609
    put_be16(pb, 0); /* Reserved */
610
    put_be16(pb, 1); /* Data-reference index */
611
612 53ffdd14 Roine Gustafsson
    put_be16(pb, 0); /* Codec stream version */
613
    put_be16(pb, 0); /* Codec stream revision (=0) */
614 5997ed78 Baptiste Coudurier
    if (track->mode == MODE_MOV) {
615 915282e5 Baptiste Coudurier
        put_tag(pb, "FFMP"); /* Vendor */
616
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
617
            put_be32(pb, 0); /* Temporal Quality */
618
            put_be32(pb, 0x400); /* Spatial Quality = lossless*/
619
        } else {
620
            put_be32(pb, 0x200); /* Temporal Quality = normal */
621
            put_be32(pb, 0x200); /* Spatial Quality = normal */
622
        }
623 5997ed78 Baptiste Coudurier
    } else {
624
        put_be32(pb, 0); /* Reserved */
625
        put_be32(pb, 0); /* Reserved */
626
        put_be32(pb, 0); /* Reserved */
627
    }
628 f578f938 Thomas Raivio
    put_be16(pb, track->enc->width); /* Video width */
629
    put_be16(pb, track->enc->height); /* Video height */
630 53ffdd14 Roine Gustafsson
    put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
631
    put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
632 9a4d9388 Roman Shaposhnik
    put_be32(pb, 0); /* Data size (= 0) */
633
    put_be16(pb, 1); /* Frame count (= 1) */
634 115329f1 Diego Biurrun
635 53ffdd14 Roine Gustafsson
    memset(compressor_name,0,32);
636 5997ed78 Baptiste Coudurier
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
637
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
638 53ffdd14 Roine Gustafsson
        strncpy(compressor_name,track->enc->codec->name,31);
639 25cf9062 François Revol
    put_byte(pb, strlen(compressor_name));
640 53ffdd14 Roine Gustafsson
    put_buffer(pb, compressor_name, 31);
641 115329f1 Diego Biurrun
642 f578f938 Thomas Raivio
    put_be16(pb, 0x18); /* Reserved */
643
    put_be16(pb, 0xffff); /* Reserved */
644
    if(track->enc->codec_id == CODEC_ID_MPEG4)
645
        mov_write_esds_tag(pb, track);
646
    else if(track->enc->codec_id == CODEC_ID_H263)
647
        mov_write_d263_tag(pb);
648
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
649 115329f1 Diego Biurrun
        mov_write_svq3_tag(pb);
650 c1b8e6d8 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_H264)
651
        mov_write_avcc_tag(pb, track);
652 f578f938 Thomas Raivio
653
    return updateSize (pb, pos);
654 1cb5f7fd Michael Niedermayer
}
655
656 6e6d6dc0 Michael Niedermayer
static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
657 1cb5f7fd Michael Niedermayer
{
658 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
659 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
660
    put_tag(pb, "stsd");
661
    put_be32(pb, 0); /* version & flags */
662
    put_be32(pb, 1); /* entry count */
663 f578f938 Thomas Raivio
    if (track->enc->codec_type == CODEC_TYPE_VIDEO)
664
        mov_write_video_tag(pb, track);
665
    else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
666
        mov_write_audio_tag(pb, track);
667 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
668 1cb5f7fd Michael Niedermayer
}
669
670 b4712e3c Baptiste Coudurier
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
671
{
672
    Time2Sample *ctts_entries;
673
    uint32_t entries = 0;
674
    uint32_t atom_size;
675
    int i;
676
677
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
678
    ctts_entries[0].count = 1;
679 0aec3c5c Baptiste Coudurier
    ctts_entries[0].duration = track->cluster[0].cts;
680 b4712e3c Baptiste Coudurier
    for (i=1; i<track->entry; i++) {
681 0aec3c5c Baptiste Coudurier
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
682 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count++; /* compress */
683
        } else {
684
            entries++;
685 0aec3c5c Baptiste Coudurier
            ctts_entries[entries].duration = track->cluster[i].cts;
686 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count = 1;
687
        }
688
    }
689
    entries++; /* last one */
690
    atom_size = 16 + (entries * 8);
691
    put_be32(pb, atom_size); /* size */
692
    put_tag(pb, "ctts");
693
    put_be32(pb, 0); /* version & flags */
694
    put_be32(pb, entries); /* entry count */
695
    for (i=0; i<entries; i++) {
696
        put_be32(pb, ctts_entries[i].count);
697
        put_be32(pb, ctts_entries[i].duration);
698
    }
699
    av_free(ctts_entries);
700
    return atom_size;
701
}
702
703 e45ccf79 Gildas Bazin
/* Time to sample atom */
704 6e6d6dc0 Michael Niedermayer
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
705 1cb5f7fd Michael Niedermayer
{
706 aa90239f Baptiste Coudurier
    Time2Sample *stts_entries;
707
    uint32_t entries = -1;
708
    uint32_t atom_size;
709
    int i;
710
711
    if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
712
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
713
        stts_entries[0].count = track->sampleCount;
714
        stts_entries[0].duration = 1;
715
        entries = 1;
716
    } else {
717
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
718
        for (i=0; i<track->entry; i++) {
719
            int64_t duration = i + 1 == track->entry ?
720
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
721
                track->cluster[i+1].dts - track->cluster[i].dts;
722
            if (i && duration == stts_entries[entries].duration) {
723
                stts_entries[entries].count++; /* compress */
724
            } else {
725
                entries++;
726
                stts_entries[entries].duration = duration;
727
                stts_entries[entries].count = 1;
728
            }
729
        }
730
        entries++; /* last one */
731
    }
732
    atom_size = 16 + (entries * 8);
733
    put_be32(pb, atom_size); /* size */
734 1cb5f7fd Michael Niedermayer
    put_tag(pb, "stts");
735
    put_be32(pb, 0); /* version & flags */
736 aa90239f Baptiste Coudurier
    put_be32(pb, entries); /* entry count */
737
    for (i=0; i<entries; i++) {
738
        put_be32(pb, stts_entries[i].count);
739
        put_be32(pb, stts_entries[i].duration);
740
    }
741
    av_free(stts_entries);
742
    return atom_size;
743 1cb5f7fd Michael Niedermayer
}
744
745 6e6d6dc0 Michael Niedermayer
static int mov_write_dref_tag(ByteIOContext *pb)
746 1cb5f7fd Michael Niedermayer
{
747
    put_be32(pb, 28); /* size */
748
    put_tag(pb, "dref");
749
    put_be32(pb, 0); /* version & flags */
750
    put_be32(pb, 1); /* entry count */
751
752
    put_be32(pb, 0xc); /* size */
753
    put_tag(pb, "url ");
754
    put_be32(pb, 1); /* version & flags */
755
756
    return 28;
757
}
758
759 6e6d6dc0 Michael Niedermayer
static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
760 1cb5f7fd Michael Niedermayer
{
761 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
762 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
763
    put_tag(pb, "stbl");
764 6e6d6dc0 Michael Niedermayer
    mov_write_stsd_tag(pb, track);
765
    mov_write_stts_tag(pb, track);
766 f578f938 Thomas Raivio
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
767 32ba6fb1 Baptiste Coudurier
        track->hasKeyframes < track->entry)
768 f578f938 Thomas Raivio
        mov_write_stss_tag(pb, track);
769 b4712e3c Baptiste Coudurier
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
770
        track->hasBframes)
771
        mov_write_ctts_tag(pb, track);
772 6e6d6dc0 Michael Niedermayer
    mov_write_stsc_tag(pb, track);
773
    mov_write_stsz_tag(pb, track);
774
    mov_write_stco_tag(pb, track);
775
    return updateSize(pb, pos);
776 1cb5f7fd Michael Niedermayer
}
777
778 6e6d6dc0 Michael Niedermayer
static int mov_write_dinf_tag(ByteIOContext *pb)
779 1cb5f7fd Michael Niedermayer
{
780 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
781 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
782
    put_tag(pb, "dinf");
783 6e6d6dc0 Michael Niedermayer
    mov_write_dref_tag(pb);
784
    return updateSize(pb, pos);
785 1cb5f7fd Michael Niedermayer
}
786
787 6e6d6dc0 Michael Niedermayer
static int mov_write_smhd_tag(ByteIOContext *pb)
788 1cb5f7fd Michael Niedermayer
{
789
    put_be32(pb, 16); /* size */
790
    put_tag(pb, "smhd");
791
    put_be32(pb, 0); /* version & flags */
792
    put_be16(pb, 0); /* reserved (balance, normally = 0) */
793
    put_be16(pb, 0); /* reserved */
794
    return 16;
795
}
796
797 6e6d6dc0 Michael Niedermayer
static int mov_write_vmhd_tag(ByteIOContext *pb)
798 1cb5f7fd Michael Niedermayer
{
799
    put_be32(pb, 0x14); /* size (always 0x14) */
800
    put_tag(pb, "vmhd");
801
    put_be32(pb, 0x01); /* version & flags */
802
    put_be64(pb, 0); /* reserved (graphics mode = copy) */
803
    return 0x14;
804
}
805
806 6e6d6dc0 Michael Niedermayer
static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
807 1cb5f7fd Michael Niedermayer
{
808 a97c1e93 Stefan Huehner
    const char *descr, *hdlr, *hdlr_type;
809 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
810 115329f1 Diego Biurrun
811 9a4d9388 Roman Shaposhnik
    if (!track) { /* no media --> data handler */
812 bb270c08 Diego Biurrun
        hdlr = "dhlr";
813
        hdlr_type = "url ";
814
        descr = "DataHandler";
815 9a4d9388 Roman Shaposhnik
    } else {
816 bb270c08 Diego Biurrun
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
817
        if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
818
            hdlr_type = "vide";
819
            descr = "VideoHandler";
820
        } else {
821
            hdlr_type = "soun";
822
            descr = "SoundHandler";
823
        }
824 9a4d9388 Roman Shaposhnik
    }
825 115329f1 Diego Biurrun
826 f578f938 Thomas Raivio
    put_be32(pb, 0); /* size */
827 1cb5f7fd Michael Niedermayer
    put_tag(pb, "hdlr");
828
    put_be32(pb, 0); /* Version & flags */
829 906b578f Roman Shaposhnik
    put_buffer(pb, hdlr, 4); /* handler */
830 9a4d9388 Roman Shaposhnik
    put_tag(pb, hdlr_type); /* handler type */
831 f578f938 Thomas Raivio
    put_be32(pb ,0); /* reserved */
832
    put_be32(pb ,0); /* reserved */
833
    put_be32(pb ,0); /* reserved */
834 9a4d9388 Roman Shaposhnik
    put_byte(pb, strlen(descr)); /* string counter */
835
    put_buffer(pb, descr, strlen(descr)); /* handler description */
836
    return updateSize(pb, pos);
837
}
838
839
static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
840
{
841 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
842 9a4d9388 Roman Shaposhnik
    put_be32(pb, 0); /* size */
843
    put_tag(pb, "minf");
844 1cb5f7fd Michael Niedermayer
    if(track->enc->codec_type == CODEC_TYPE_VIDEO)
845 9a4d9388 Roman Shaposhnik
        mov_write_vmhd_tag(pb);
846 1cb5f7fd Michael Niedermayer
    else
847 9a4d9388 Roman Shaposhnik
        mov_write_smhd_tag(pb);
848
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
849
        mov_write_hdlr_tag(pb, NULL);
850
    mov_write_dinf_tag(pb);
851
    mov_write_stbl_tag(pb, track);
852 f578f938 Thomas Raivio
    return updateSize(pb, pos);
853 1cb5f7fd Michael Niedermayer
}
854
855 6e6d6dc0 Michael Niedermayer
static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
856 1cb5f7fd Michael Niedermayer
{
857 a3a80ddc Baptiste Coudurier
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
858
859
    (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
860 1cb5f7fd Michael Niedermayer
    put_tag(pb, "mdhd");
861 a3a80ddc Baptiste Coudurier
    put_byte(pb, version);
862
    put_be24(pb, 0); /* flags */
863
    if (version == 1) {
864
        put_be64(pb, track->time);
865
        put_be64(pb, track->time);
866
    } else {
867
        put_be32(pb, track->time); /* creation time */
868
        put_be32(pb, track->time); /* modification time */
869
    }
870 115329f1 Diego Biurrun
    put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
871 a3a80ddc Baptiste Coudurier
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
872 ab561df9 François Revol
    put_be16(pb, track->language); /* language */
873 1cb5f7fd Michael Niedermayer
    put_be16(pb, 0); /* reserved (quality) */
874
    return 32;
875
}
876
877 6e6d6dc0 Michael Niedermayer
static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
878 1cb5f7fd Michael Niedermayer
{
879 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
880 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
881
    put_tag(pb, "mdia");
882 6e6d6dc0 Michael Niedermayer
    mov_write_mdhd_tag(pb, track);
883
    mov_write_hdlr_tag(pb, track);
884
    mov_write_minf_tag(pb, track);
885
    return updateSize(pb, pos);
886 1cb5f7fd Michael Niedermayer
}
887
888 6e6d6dc0 Michael Niedermayer
static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
889 1cb5f7fd Michael Niedermayer
{
890 a3a80ddc Baptiste Coudurier
    int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
891
    int version = duration < INT32_MAX ? 0 : 1;
892
893
    (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
894 1cb5f7fd Michael Niedermayer
    put_tag(pb, "tkhd");
895 a3a80ddc Baptiste Coudurier
    put_byte(pb, version);
896
    put_be24(pb, 0xf); /* flags (track enabled) */
897
    if (version == 1) {
898
        put_be64(pb, track->time);
899
        put_be64(pb, track->time);
900
    } else {
901
        put_be32(pb, track->time); /* creation time */
902
        put_be32(pb, track->time); /* modification time */
903
    }
904 1cb5f7fd Michael Niedermayer
    put_be32(pb, track->trackID); /* track-id */
905
    put_be32(pb, 0); /* reserved */
906 a3a80ddc Baptiste Coudurier
    (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
907 1cb5f7fd Michael Niedermayer
908
    put_be32(pb, 0); /* reserved */
909
    put_be32(pb, 0); /* reserved */
910
    put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
911
    /* Volume, only for audio */
912
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
913
        put_be16(pb, 0x0100);
914
    else
915
        put_be16(pb, 0);
916
    put_be16(pb, 0); /* reserved */
917
918
    /* Matrix structure */
919
    put_be32(pb, 0x00010000); /* reserved */
920
    put_be32(pb, 0x0); /* reserved */
921
    put_be32(pb, 0x0); /* reserved */
922
    put_be32(pb, 0x0); /* reserved */
923
    put_be32(pb, 0x00010000); /* reserved */
924
    put_be32(pb, 0x0); /* reserved */
925
    put_be32(pb, 0x0); /* reserved */
926
    put_be32(pb, 0x0); /* reserved */
927
    put_be32(pb, 0x40000000); /* reserved */
928
929
    /* Track width and height, for visual only */
930
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
931 69dde1ad Gildas Bazin
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
932
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
933
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
934 f578f938 Thomas Raivio
        put_be32(pb, track->enc->height*0x10000);
935 1cb5f7fd Michael Niedermayer
    }
936
    else {
937
        put_be32(pb, 0);
938
        put_be32(pb, 0);
939
    }
940
    return 0x5c;
941
}
942
943 8af18154 tjcannell@blueyonder.co.uk
// This box seems important for the psp playback ... without it the movie seems to hang
944
static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
945
{
946
    put_be32(pb, 0x24); /* size  */
947
    put_tag(pb, "edts");
948
    put_be32(pb, 0x1c); /* size  */
949
    put_tag(pb, "elst");
950
    put_be32(pb, 0x0);
951
    put_be32(pb, 0x1);
952
953 83fa2ef8 Michael Niedermayer
    put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration   ... doesn't seem to effect psp */
954 8af18154 tjcannell@blueyonder.co.uk
955 72289140 Baptiste Coudurier
    put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
956 8af18154 tjcannell@blueyonder.co.uk
    put_be32(pb, 0x00010000);
957
    return 0x24;
958
}
959
960
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
961
static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
962
{
963
    put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
964
    put_tag(pb, "uuid");
965
    put_tag(pb, "USMT");
966
    put_be32(pb, 0x21d24fce);
967
    put_be32(pb, 0xbb88695c);
968
    put_be32(pb, 0xfac9c740);
969
    put_be32(pb, 0x1c);     // another size here!
970
    put_tag(pb, "MTDT");
971
    put_be32(pb, 0x00010012);
972
    put_be32(pb, 0x0a);
973
    put_be32(pb, 0x55c40000);
974
    put_be32(pb, 0x1);
975
    put_be32(pb, 0x0);
976
    return 0x34;
977
}
978
979 6e6d6dc0 Michael Niedermayer
static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
980 1cb5f7fd Michael Niedermayer
{
981 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
982 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size */
983
    put_tag(pb, "trak");
984 6e6d6dc0 Michael Niedermayer
    mov_write_tkhd_tag(pb, track);
985 c6e722e5 Baptiste Coudurier
    if (track->mode == MODE_PSP || track->hasBframes)
986 8af18154 tjcannell@blueyonder.co.uk
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
987 6e6d6dc0 Michael Niedermayer
    mov_write_mdia_tag(pb, track);
988 115329f1 Diego Biurrun
    if (track->mode == MODE_PSP)
989 8af18154 tjcannell@blueyonder.co.uk
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
990 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
991 1cb5f7fd Michael Niedermayer
}
992
993 88730be6 Måns Rullgård
#if 0
994 1cb5f7fd Michael Niedermayer
/* TODO: Not sorted out, but not necessary either */
995 6e6d6dc0 Michael Niedermayer
static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
996 1cb5f7fd Michael Niedermayer
{
997
    put_be32(pb, 0x15); /* size */
998
    put_tag(pb, "iods");
999
    put_be32(pb, 0);    /* version & flags */
1000
    put_be16(pb, 0x1007);
1001
    put_byte(pb, 0);
1002
    put_be16(pb, 0x4fff);
1003
    put_be16(pb, 0xfffe);
1004
    put_be16(pb, 0x01ff);
1005
    return 0x15;
1006
}
1007 88730be6 Måns Rullgård
#endif
1008 1cb5f7fd Michael Niedermayer
1009 6e6d6dc0 Michael Niedermayer
static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
1010 1cb5f7fd Michael Niedermayer
{
1011 b29af723 Michael Niedermayer
    int maxTrackID = 1, i;
1012
    int64_t maxTrackLenTemp, maxTrackLen = 0;
1013 a3a80ddc Baptiste Coudurier
    int version;
1014 1cb5f7fd Michael Niedermayer
1015 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1016 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1017 83fa2ef8 Michael Niedermayer
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
1018 f578f938 Thomas Raivio
            if(maxTrackLen < maxTrackLenTemp)
1019
                maxTrackLen = maxTrackLenTemp;
1020 1cb5f7fd Michael Niedermayer
            if(maxTrackID < mov->tracks[i].trackID)
1021
                maxTrackID = mov->tracks[i].trackID;
1022
        }
1023
    }
1024 a3a80ddc Baptiste Coudurier
1025
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
1026
    (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
1027
    put_tag(pb, "mvhd");
1028
    put_byte(pb, version);
1029
    put_be24(pb, 0); /* flags */
1030
    if (version == 1) {
1031
        put_be64(pb, mov->time);
1032
        put_be64(pb, mov->time);
1033
    } else {
1034
        put_be32(pb, mov->time); /* creation time */
1035
        put_be32(pb, mov->time); /* modification time */
1036
    }
1037
    put_be32(pb, mov->timescale); /* timescale */
1038
    (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
1039 1cb5f7fd Michael Niedermayer
1040
    put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1041
    put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1042
    put_be16(pb, 0); /* reserved */
1043
    put_be32(pb, 0); /* reserved */
1044
    put_be32(pb, 0); /* reserved */
1045
1046
    /* Matrix structure */
1047
    put_be32(pb, 0x00010000); /* reserved */
1048
    put_be32(pb, 0x0); /* reserved */
1049
    put_be32(pb, 0x0); /* reserved */
1050
    put_be32(pb, 0x0); /* reserved */
1051
    put_be32(pb, 0x00010000); /* reserved */
1052
    put_be32(pb, 0x0); /* reserved */
1053
    put_be32(pb, 0x0); /* reserved */
1054
    put_be32(pb, 0x0); /* reserved */
1055
    put_be32(pb, 0x40000000); /* reserved */
1056
1057
    put_be32(pb, 0); /* reserved (preview time) */
1058
    put_be32(pb, 0); /* reserved (preview duration) */
1059
    put_be32(pb, 0); /* reserved (poster time) */
1060
    put_be32(pb, 0); /* reserved (selection time) */
1061
    put_be32(pb, 0); /* reserved (selection duration) */
1062
    put_be32(pb, 0); /* reserved (current time) */
1063
    put_be32(pb, maxTrackID+1); /* Next track id */
1064
    return 0x6c;
1065
}
1066
1067 b6c50eb1 Patrice Bensoussan
static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
1068
                                     AVFormatContext *s)
1069
{
1070 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1071 b6c50eb1 Patrice Bensoussan
    put_be32(pb, 0); /* size */
1072
    put_tag(pb, "hdlr");
1073
    put_be32(pb, 0);
1074
    put_be32(pb, 0);
1075
    put_tag(pb, "mdir");
1076
    put_tag(pb, "appl");
1077
    put_be32(pb, 0);
1078
    put_be32(pb, 0);
1079
    put_be16(pb, 0);
1080
    return updateSize(pb, pos);
1081
}
1082
1083
/* helper function to write a data tag with the specified string as data */
1084 efda3395 Michael Niedermayer
static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
1085 b6c50eb1 Patrice Bensoussan
{
1086 efda3395 Michael Niedermayer
    if(long_style){
1087 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1088 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1089 efda3395 Michael Niedermayer
        put_tag(pb, "data");
1090
        put_be32(pb, 1);
1091
        put_be32(pb, 0);
1092
        put_buffer(pb, data, strlen(data));
1093
        return updateSize(pb, pos);
1094
    }else{
1095
        put_be16(pb, strlen(data)); /* string length */
1096
        put_be16(pb, 0);
1097
        put_buffer(pb, data, strlen(data));
1098
        return strlen(data) + 4;
1099 b6c50eb1 Patrice Bensoussan
    }
1100
}
1101
1102 a97c1e93 Stefan Huehner
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1103 b6c50eb1 Patrice Bensoussan
    int size = 0;
1104 efda3395 Michael Niedermayer
    if ( value && value[0] ) {
1105 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1106 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1107 efda3395 Michael Niedermayer
        put_tag(pb, name);
1108
        mov_write_string_data_tag(pb, value, long_style);
1109
        size= updateSize(pb, pos);
1110 b6c50eb1 Patrice Bensoussan
    }
1111
    return size;
1112
}
1113
1114
/* iTunes year */
1115 efda3395 Michael Niedermayer
static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
1116 b6c50eb1 Patrice Bensoussan
{
1117 efda3395 Michael Niedermayer
    if(year){
1118
        char year_str[5];
1119
        snprintf(year_str, sizeof(year_str), "%04d", year);
1120
        return mov_write_string_tag(pb, "\251day", year_str, long_style);
1121
    }else
1122
        return 0;
1123 b6c50eb1 Patrice Bensoussan
}
1124
1125
/* iTunes track number */
1126
static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
1127
                              AVFormatContext *s)
1128
{
1129
    int size = 0;
1130
    if ( s->track ) {
1131 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1132 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1133
        put_tag(pb, "trkn");
1134
        {
1135 b29af723 Michael Niedermayer
            offset_t pos = url_ftell(pb);
1136 b6c50eb1 Patrice Bensoussan
            put_be32(pb, 0); /* size */
1137
            put_tag(pb, "data");
1138
            put_be32(pb, 0);        // 8 bytes empty
1139
            put_be32(pb, 0);
1140
            put_be16(pb, 0);        // empty
1141
            put_be16(pb, s->track); // track number
1142
            put_be16(pb, 0);        // total track number
1143
            put_be16(pb, 0);        // empty
1144
            updateSize(pb, pos);
1145
        }
1146
        size = updateSize(pb, pos);
1147
    }
1148
    return size;
1149
}
1150
1151
/* iTunes meta data list */
1152
static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
1153
                              AVFormatContext *s)
1154
{
1155 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1156 b6c50eb1 Patrice Bensoussan
    put_be32(pb, 0); /* size */
1157
    put_tag(pb, "ilst");
1158 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251nam", s->title         , 1);
1159
    mov_write_string_tag(pb, "\251ART", s->author        , 1);
1160
    mov_write_string_tag(pb, "\251wrt", s->author        , 1);
1161
    mov_write_string_tag(pb, "\251alb", s->album         , 1);
1162
    mov_write_day_tag(pb, s->year ,1);
1163 6f276720 Michael Niedermayer
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1164
        mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
1165 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251cmt", s->comment       , 1);
1166
    mov_write_string_tag(pb, "\251gen", s->genre         , 1);
1167 b6c50eb1 Patrice Bensoussan
    mov_write_trkn_tag(pb, mov, s);
1168
    return updateSize(pb, pos);
1169
}
1170
1171
/* iTunes meta data tag */
1172
static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
1173
                              AVFormatContext *s)
1174
{
1175
    int size = 0;
1176
1177
    // only save meta tag if required
1178 115329f1 Diego Biurrun
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1179 b6c50eb1 Patrice Bensoussan
         s->comment[0] || s->genre[0] || s->track ) {
1180 b29af723 Michael Niedermayer
        offset_t pos = url_ftell(pb);
1181 b6c50eb1 Patrice Bensoussan
        put_be32(pb, 0); /* size */
1182
        put_tag(pb, "meta");
1183
        put_be32(pb, 0);
1184
        mov_write_itunes_hdlr_tag(pb, mov, s);
1185
        mov_write_ilst_tag(pb, mov, s);
1186
        size = updateSize(pb, pos);
1187
    }
1188
    return size;
1189
}
1190 115329f1 Diego Biurrun
1191 69dde1ad Gildas Bazin
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1192
                              AVFormatContext *s)
1193
{
1194 b29af723 Michael Niedermayer
    offset_t pos = url_ftell(pb);
1195 69dde1ad Gildas Bazin
    int i;
1196
1197
    put_be32(pb, 0); /* size */
1198
    put_tag(pb, "udta");
1199
1200 b6c50eb1 Patrice Bensoussan
    /* iTunes meta data */
1201
    mov_write_meta_tag(pb, mov, s);
1202
1203 ed90d370 Michael Niedermayer
  if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
1204 69dde1ad Gildas Bazin
    /* Requirements */
1205 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1206 69dde1ad Gildas Bazin
        if(mov->tracks[i].entry <= 0) continue;
1207
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1208
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1209 efda3395 Michael Niedermayer
            mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
1210 69dde1ad Gildas Bazin
            break;
1211
        }
1212
    }
1213
1214 efda3395 Michael Niedermayer
    mov_write_string_tag(pb, "\251nam", s->title         , 0);
1215
    mov_write_string_tag(pb, "\251aut", s->author        , 0);
1216
    mov_write_string_tag(pb, "\251alb", s->album         , 0);
1217
    mov_write_day_tag(pb, s->year, 0);
1218 501866a1 Roine Gustafsson
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1219 efda3395 Michael Niedermayer
        mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1220
    mov_write_string_tag(pb, "\251des", s->comment       , 0);
1221
    mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1222 ed90d370 Michael Niedermayer
  }
1223 69dde1ad Gildas Bazin
1224
    return updateSize(pb, pos);
1225
}
1226
1227 79e5a53c Michael Niedermayer
static int utf8len(uint8_t *b){
1228
    int len=0;
1229
    int val;
1230
    while(*b){
1231
        GET_UTF8(val, *b++, return -1;)
1232
        len++;
1233
    }
1234
    return len;
1235
}
1236 dcfdb046 Benjamin Larsson
1237 79e5a53c Michael Niedermayer
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
1238 dcfdb046 Benjamin Larsson
{
1239 79e5a53c Michael Niedermayer
    int val;
1240
    while(*b){
1241
        GET_UTF8(val, *b++, return -1;)
1242
        put_be16(pb, val);
1243 dcfdb046 Benjamin Larsson
    }
1244
    put_be16(pb, 0x00);
1245 79e5a53c Michael Niedermayer
    return 0;
1246 dcfdb046 Benjamin Larsson
}
1247
1248 a97c1e93 Stefan Huehner
static uint16_t language_code (const char *str)
1249 dcfdb046 Benjamin Larsson
{
1250 7cf0e16f Michael Niedermayer
    return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
1251 dcfdb046 Benjamin Larsson
}
1252
1253
static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
1254
{
1255
    size_t len, size;
1256
    offset_t pos, curpos;
1257
1258
    size = 0;
1259
    if (s->title[0]) {
1260
        pos = url_ftell(pb);
1261
        put_be32(pb, 0); /* size placeholder*/
1262
        put_tag(pb, "uuid");
1263
        put_tag(pb, "USMT");
1264
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1265
        put_be32(pb, 0xbb88695c );
1266
        put_be32(pb, 0xfac9c740 );
1267
        size += 24;
1268
1269
        put_be32(pb, 0); /* size placeholder*/
1270
        put_tag(pb, "MTDT");
1271 7cf0e16f Michael Niedermayer
        put_be16(pb, 4);
1272 dcfdb046 Benjamin Larsson
        size += 10;
1273
1274 7cf0e16f Michael Niedermayer
        // ?
1275
        put_be16(pb, 0x0C);                 /* size */
1276
        put_be32(pb, 0x0B);                 /* type */
1277
        put_be16(pb, language_code("und")); /* language */
1278
        put_be16(pb, 0x0);                  /* ? */
1279
        put_be16(pb, 0x021C);               /* data */
1280
        size += 12;
1281
1282
        // Encoder
1283 79e5a53c Michael Niedermayer
        len = utf8len(LIBAVCODEC_IDENT)+1;
1284
        if(len<=0)
1285
            goto not_utf8;
1286 7cf0e16f Michael Niedermayer
        put_be16(pb, len*2+10);             /* size */
1287
        put_be32(pb, 0x04);                 /* type */
1288
        put_be16(pb, language_code("eng")); /* language */
1289
        put_be16(pb, 0x01);                 /* ? */
1290 79e5a53c Michael Niedermayer
        ascii_to_wc(pb, LIBAVCODEC_IDENT);
1291 7cf0e16f Michael Niedermayer
        size += len*2+10;
1292
1293 dcfdb046 Benjamin Larsson
        // Title
1294 79e5a53c Michael Niedermayer
        len = utf8len(s->title)+1;
1295
        if(len<=0)
1296
            goto not_utf8;
1297 dcfdb046 Benjamin Larsson
        put_be16(pb, len*2+10);             /* size */
1298
        put_be32(pb, 0x01);                 /* type */
1299 7cf0e16f Michael Niedermayer
        put_be16(pb, language_code("eng")); /* language */
1300 dcfdb046 Benjamin Larsson
        put_be16(pb, 0x01);                 /* ? */
1301 79e5a53c Michael Niedermayer
        ascii_to_wc (pb, s->title);
1302 dcfdb046 Benjamin Larsson
        size += len*2+10;
1303
1304 7cf0e16f Michael Niedermayer
        // Date
1305
//        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);
1306 79e5a53c Michael Niedermayer
        len = utf8len("2006/04/01 11:11:11")+1;
1307
        if(len<=0)
1308
            goto not_utf8;
1309 7cf0e16f Michael Niedermayer
        put_be16(pb, len*2+10);    /* size */
1310
        put_be32(pb, 0x03);        /* type */
1311
        put_be16(pb, language_code("und")); /* language */
1312
        put_be16(pb, 0x01);        /* ? */
1313 79e5a53c Michael Niedermayer
        ascii_to_wc (pb, "2006/04/01 11:11:11");
1314 7cf0e16f Michael Niedermayer
        size += len*2+10;
1315
1316 dcfdb046 Benjamin Larsson
        // size
1317
        curpos = url_ftell(pb);
1318
        url_fseek(pb, pos, SEEK_SET);
1319
        put_be32(pb, size);
1320
        url_fseek(pb, pos+24, SEEK_SET);
1321
        put_be32(pb, size-24);
1322
        url_fseek(pb, curpos, SEEK_SET);
1323
    }
1324
1325
    return size;
1326 79e5a53c Michael Niedermayer
not_utf8:
1327
    av_log(s, AV_LOG_ERROR, "not utf8\n");
1328
    return -1;
1329 dcfdb046 Benjamin Larsson
}
1330
1331 69dde1ad Gildas Bazin
static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
1332
                              AVFormatContext *s)
1333 1cb5f7fd Michael Niedermayer
{
1334 b29af723 Michael Niedermayer
    int i;
1335
    offset_t pos = url_ftell(pb);
1336 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size placeholder*/
1337
    put_tag(pb, "moov");
1338
    mov->timescale = globalTimescale;
1339
1340 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1341 e45ccf79 Gildas Bazin
        if(mov->tracks[i].entry <= 0) continue;
1342
1343
        mov->tracks[i].time = mov->time;
1344
        mov->tracks[i].trackID = i+1;
1345 1cb5f7fd Michael Niedermayer
    }
1346
1347 6e6d6dc0 Michael Niedermayer
    mov_write_mvhd_tag(pb, mov);
1348
    //mov_write_iods_tag(pb, mov);
1349 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1350 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1351 6e6d6dc0 Michael Niedermayer
            mov_write_trak_tag(pb, &(mov->tracks[i]));
1352 1cb5f7fd Michael Niedermayer
        }
1353
    }
1354
1355 dcfdb046 Benjamin Larsson
    if (mov->mode == MODE_PSP)
1356
        mov_write_uuidusmt_tag(pb, s);
1357
    else
1358 e344c1ea Steve L'Homme
        mov_write_udta_tag(pb, mov, s);
1359 69dde1ad Gildas Bazin
1360 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
1361 1cb5f7fd Michael Niedermayer
}
1362
1363 7b49ce2e Stefan Huehner
static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
1364 1cb5f7fd Michael Niedermayer
{
1365 b29af723 Michael Niedermayer
    put_be32(pb, 8);    // placeholder for extended size field (64 bit)
1366 5340a0bc Baptiste Coudurier
    put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
1367 b29af723 Michael Niedermayer
1368 115329f1 Diego Biurrun
    mov->mdat_pos = url_ftell(pb);
1369 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0); /* size placeholder*/
1370
    put_tag(pb, "mdat");
1371
    return 0;
1372
}
1373
1374
/* TODO: This needs to be more general */
1375 dcfdb046 Benjamin Larsson
static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
1376 1cb5f7fd Michael Niedermayer
{
1377 69dde1ad Gildas Bazin
    MOVContext *mov = s->priv_data;
1378
1379 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0x14 ); /* size */
1380
    put_tag(pb, "ftyp");
1381 e45ccf79 Gildas Bazin
1382 69dde1ad Gildas Bazin
    if ( mov->mode == MODE_3GP )
1383 e45ccf79 Gildas Bazin
        put_tag(pb, "3gp4");
1384 8536ab89 tjraivio@cc.hut.fi
    else if ( mov->mode == MODE_3G2 )
1385
        put_tag(pb, "3g2a");
1386 8af18154 tjcannell@blueyonder.co.uk
    else if ( mov->mode == MODE_PSP )
1387
        put_tag(pb, "MSNV");
1388 26f86eaf Baptiste Coudurier
    else if ( mov->mode == MODE_MP4 )
1389 e45ccf79 Gildas Bazin
        put_tag(pb, "isom");
1390 26f86eaf Baptiste Coudurier
    else
1391
        put_tag(pb, "qt  ");
1392 e45ccf79 Gildas Bazin
1393 1cb5f7fd Michael Niedermayer
    put_be32(pb, 0x200 );
1394 e45ccf79 Gildas Bazin
1395 69dde1ad Gildas Bazin
    if ( mov->mode == MODE_3GP )
1396 e45ccf79 Gildas Bazin
        put_tag(pb, "3gp4");
1397 8536ab89 tjraivio@cc.hut.fi
    else if ( mov->mode == MODE_3G2 )
1398
        put_tag(pb, "3g2a");
1399 8af18154 tjcannell@blueyonder.co.uk
    else if ( mov->mode == MODE_PSP )
1400
        put_tag(pb, "MSNV");
1401 26f86eaf Baptiste Coudurier
    else if ( mov->mode == MODE_MP4 )
1402 e45ccf79 Gildas Bazin
        put_tag(pb, "mp41");
1403 26f86eaf Baptiste Coudurier
    else
1404
        put_tag(pb, "qt  ");
1405 1cb5f7fd Michael Niedermayer
}
1406
1407 0c716ab7 François Revol
static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
1408 8af18154 tjcannell@blueyonder.co.uk
{
1409 dcfdb046 Benjamin Larsson
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1410
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1411
    int AudioRate = AudioCodec->sample_rate;
1412
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1413 9e282ba3 Michael Niedermayer
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1414
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1415 8af18154 tjcannell@blueyonder.co.uk
1416
    put_be32(pb, 0x94 ); /* size */
1417
    put_tag(pb, "uuid");
1418
    put_tag(pb, "PROF");
1419
1420
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1421
    put_be32(pb, 0xbb88695c );
1422
    put_be32(pb, 0xfac9c740 );
1423
1424
    put_be32(pb, 0x0 );  /* ? */
1425
    put_be32(pb, 0x3 );  /* 3 sections ? */
1426
1427
    put_be32(pb, 0x14 ); /* size */
1428
    put_tag(pb, "FPRF");
1429
    put_be32(pb, 0x0 );  /* ? */
1430
    put_be32(pb, 0x0 );  /* ? */
1431
    put_be32(pb, 0x0 );  /* ? */
1432
1433
    put_be32(pb, 0x2c );  /* size */
1434
    put_tag(pb, "APRF");   /* audio */
1435
    put_be32(pb, 0x0 );
1436 dcfdb046 Benjamin Larsson
    put_be32(pb, 0x2 );   /* TrackID */
1437 8af18154 tjcannell@blueyonder.co.uk
    put_tag(pb, "mp4a");
1438
    put_be32(pb, 0x20f );
1439
    put_be32(pb, 0x0 );
1440 9e282ba3 Michael Niedermayer
    put_be32(pb, audio_kbitrate);
1441
    put_be32(pb, audio_kbitrate);
1442 dcfdb046 Benjamin Larsson
    put_be32(pb, AudioRate );
1443
    put_be32(pb, AudioCodec->channels );
1444 8af18154 tjcannell@blueyonder.co.uk
1445
    put_be32(pb, 0x34 );  /* size */
1446
    put_tag(pb, "VPRF");   /* video */
1447
    put_be32(pb, 0x0 );
1448 dcfdb046 Benjamin Larsson
    put_be32(pb, 0x1 );    /* TrackID */
1449 87b041e0 Michael Niedermayer
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1450
        put_tag(pb, "avc1");
1451
        put_be16(pb, 0x014D );
1452
        put_be16(pb, 0x0015 );
1453
    } else {
1454
        put_tag(pb, "mp4v");
1455
        put_be16(pb, 0x0000 );
1456
        put_be16(pb, 0x0103 );
1457
    }
1458 8af18154 tjcannell@blueyonder.co.uk
    put_be32(pb, 0x0 );
1459 9e282ba3 Michael Niedermayer
    put_be32(pb, video_kbitrate);
1460
    put_be32(pb, video_kbitrate);
1461 dcfdb046 Benjamin Larsson
    put_be32(pb, FrameRate);
1462
    put_be32(pb, FrameRate);
1463
    put_be16(pb, VideoCodec->width);
1464
    put_be16(pb, VideoCodec->height);
1465
    put_be32(pb, 0x010001); /* ? */
1466 8af18154 tjcannell@blueyonder.co.uk
}
1467
1468 1cb5f7fd Michael Niedermayer
static int mov_write_header(AVFormatContext *s)
1469
{
1470
    ByteIOContext *pb = &s->pb;
1471 69dde1ad Gildas Bazin
    MOVContext *mov = s->priv_data;
1472
    int i;
1473 1cb5f7fd Michael Niedermayer
1474 1522767d Baptiste Coudurier
    /* Default mode == MP4 */
1475
    mov->mode = MODE_MP4;
1476
1477
    if (s->oformat != NULL) {
1478
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
1479
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1480
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1481
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
1482
1483 26f86eaf Baptiste Coudurier
        mov_write_ftyp_tag(pb,s);
1484 1522767d Baptiste Coudurier
        if ( mov->mode == MODE_PSP ) {
1485
            if ( s->nb_streams != 2 ) {
1486
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1487
                return -1;
1488
            }
1489
            mov_write_uuidprof_tag(pb,s);
1490
        }
1491
    }
1492
1493 ffdd57d4 Michael Niedermayer
    for(i=0; i<s->nb_streams; i++){
1494 039627cf Baptiste Coudurier
        AVStream *st= s->streams[i];
1495
        MOVTrack *track= &mov->tracks[i];
1496
1497
        track->enc = st->codec;
1498 2c5ad5fd Baptiste Coudurier
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1499
        track->mode = mov->mode;
1500 039627cf Baptiste Coudurier
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1501
            track->tag = mov_find_video_codec_tag(s, track);
1502 ffb512e3 Baptiste Coudurier
            track->timescale = st->codec->time_base.den;
1503 039627cf Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
1504
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1505
            track->tag = mov_find_audio_codec_tag(s, track);
1506 ffb512e3 Baptiste Coudurier
            track->timescale = st->codec->sample_rate;
1507 039627cf Baptiste Coudurier
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1508 aa90239f Baptiste Coudurier
            switch(track->enc->codec_id){
1509
            case CODEC_ID_MP3:
1510
            case CODEC_ID_AAC:
1511
            case CODEC_ID_AMR_NB:
1512
            case CODEC_ID_AMR_WB:
1513
                track->audio_vbr = 1;
1514
                break;
1515
            default:
1516
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1517
            }
1518 b596102e Baptiste Coudurier
            if (!st->codec->frame_size) {
1519
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1520
                return -1;
1521
            }
1522 ffb512e3 Baptiste Coudurier
        }
1523 f578f938 Thomas Raivio
    }
1524
1525 18c697ac Baptiste Coudurier
    mov_write_mdat_tag(pb, mov);
1526
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
1527 42fb4148 Baptiste Coudurier
    mov->nb_streams = s->nb_streams;
1528 18c697ac Baptiste Coudurier
1529 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
1530
1531
    return 0;
1532
}
1533
1534 e928649b Michael Niedermayer
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1535 1cb5f7fd Michael Niedermayer
{
1536
    MOVContext *mov = s->priv_data;
1537
    ByteIOContext *pb = &s->pb;
1538 039627cf Baptiste Coudurier
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1539
    AVCodecContext *enc = trk->enc;
1540 e45ccf79 Gildas Bazin
    unsigned int samplesInChunk = 0;
1541 e928649b Michael Niedermayer
    int size= pkt->size;
1542 1cb5f7fd Michael Niedermayer
1543 e45ccf79 Gildas Bazin
    if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
1544
    if (!size) return 0; /* Discard 0 sized packets */
1545 1cb5f7fd Michael Niedermayer
1546 06fcf56c Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1547 5cb49ca1 Baptiste Coudurier
        /* We must find out how many AMR blocks there are in one packet */
1548
        static uint16_t packed_size[16] =
1549
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1550
        int len = 0;
1551
1552
        while (len < size && samplesInChunk < 100) {
1553
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1554
            samplesInChunk++;
1555 1cb5f7fd Michael Niedermayer
        }
1556 2111e3f9 Michael Niedermayer
        if(samplesInChunk > 1){
1557
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
1558
            return -1;
1559
        }
1560 5cb49ca1 Baptiste Coudurier
    } else if (trk->sampleSize)
1561
        samplesInChunk = size/trk->sampleSize;
1562
    else
1563 5616f85d Baptiste Coudurier
        samplesInChunk = 1;
1564 e45ccf79 Gildas Bazin
1565 5616f85d Baptiste Coudurier
    /* copy extradata if it exists */
1566
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1567 e45ccf79 Gildas Bazin
        trk->vosLen = enc->extradata_size;
1568
        trk->vosData = av_malloc(trk->vosLen);
1569
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1570
    }
1571
1572 5616f85d Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1573
        /* from x264 or from bytestream h264 */
1574
        /* nal reformating needed */
1575
        avc_parse_nal_units(&pkt->data, &pkt->size);
1576
        assert(pkt->size);
1577
        size = pkt->size;
1578 c1b8e6d8 Baptiste Coudurier
    }
1579
1580 0aec3c5c Baptiste Coudurier
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1581
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1582 e45ccf79 Gildas Bazin
        if (!trk->cluster)
1583
            return -1;
1584
    }
1585
1586 0aec3c5c Baptiste Coudurier
    trk->cluster[trk->entry].pos = url_ftell(pb);
1587
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
1588
    trk->cluster[trk->entry].size = size;
1589
    trk->cluster[trk->entry].entries = samplesInChunk;
1590 aa90239f Baptiste Coudurier
    trk->cluster[trk->entry].dts = pkt->dts;
1591
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
1592
1593 e45ccf79 Gildas Bazin
    if(enc->codec_type == CODEC_TYPE_VIDEO) {
1594 b4712e3c Baptiste Coudurier
        if (pkt->dts != pkt->pts)
1595
            trk->hasBframes = 1;
1596 0aec3c5c Baptiste Coudurier
        trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
1597
        trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
1598
        if(trk->cluster[trk->entry].key_frame)
1599 32ba6fb1 Baptiste Coudurier
            trk->hasKeyframes++;
1600 1cb5f7fd Michael Niedermayer
    }
1601 e45ccf79 Gildas Bazin
    trk->entry++;
1602
    trk->sampleCount += samplesInChunk;
1603 039627cf Baptiste Coudurier
    mov->mdat_size += size;
1604 e45ccf79 Gildas Bazin
1605 e928649b Michael Niedermayer
    put_buffer(pb, pkt->data, size);
1606 1cb5f7fd Michael Niedermayer
1607
    put_flush_packet(pb);
1608
    return 0;
1609
}
1610
1611
static int mov_write_trailer(AVFormatContext *s)
1612
{
1613
    MOVContext *mov = s->priv_data;
1614
    ByteIOContext *pb = &s->pb;
1615
    int res = 0;
1616 0aec3c5c Baptiste Coudurier
    int i;
1617 1cb5f7fd Michael Niedermayer
1618 69dde1ad Gildas Bazin
    offset_t moov_pos = url_ftell(pb);
1619 1cb5f7fd Michael Niedermayer
1620
    /* Write size of mdat tag */
1621 039627cf Baptiste Coudurier
    if (mov->mdat_size+8 <= UINT32_MAX) {
1622 b29af723 Michael Niedermayer
        url_fseek(pb, mov->mdat_pos, SEEK_SET);
1623 039627cf Baptiste Coudurier
        put_be32(pb, mov->mdat_size+8);
1624 b29af723 Michael Niedermayer
    } else {
1625
        /* overwrite 'wide' placeholder atom */
1626
        url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
1627
        put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
1628
        put_tag(pb, "mdat");
1629 039627cf Baptiste Coudurier
        put_be64(pb, mov->mdat_size+16);
1630 b29af723 Michael Niedermayer
    }
1631 69dde1ad Gildas Bazin
    url_fseek(pb, moov_pos, SEEK_SET);
1632 1cb5f7fd Michael Niedermayer
1633 69dde1ad Gildas Bazin
    mov_write_moov_tag(pb, mov, s);
1634 1cb5f7fd Michael Niedermayer
1635 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1636 0aec3c5c Baptiste Coudurier
        av_freep(&mov->tracks[i].cluster);
1637
1638 ec7d0d2e Gildas Bazin
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
1639
1640 1cb5f7fd Michael Niedermayer
    }
1641 69dde1ad Gildas Bazin
1642 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
1643
1644
    return res;
1645
}
1646
1647 ff70e601 Måns Rullgård
#ifdef CONFIG_MOV_MUXER
1648
AVOutputFormat mov_muxer = {
1649 1cb5f7fd Michael Niedermayer
    "mov",
1650
    "mov format",
1651
    NULL,
1652
    "mov",
1653
    sizeof(MOVContext),
1654 69dde1ad Gildas Bazin
    CODEC_ID_AAC,
1655 2187d948 Michael Niedermayer
    CODEC_ID_MPEG4,
1656 1cb5f7fd Michael Niedermayer
    mov_write_header,
1657
    mov_write_packet,
1658
    mov_write_trailer,
1659 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1660 1cb5f7fd Michael Niedermayer
};
1661 ff70e601 Måns Rullgård
#endif
1662
#ifdef CONFIG_TGP_MUXER
1663
AVOutputFormat tgp_muxer = {
1664 1cb5f7fd Michael Niedermayer
    "3gp",
1665
    "3gp format",
1666
    NULL,
1667
    "3gp",
1668
    sizeof(MOVContext),
1669
    CODEC_ID_AMR_NB,
1670
    CODEC_ID_H263,
1671
    mov_write_header,
1672
    mov_write_packet,
1673
    mov_write_trailer,
1674 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1675 1cb5f7fd Michael Niedermayer
};
1676 ff70e601 Måns Rullgård
#endif
1677
#ifdef CONFIG_MP4_MUXER
1678
AVOutputFormat mp4_muxer = {
1679 1cb5f7fd Michael Niedermayer
    "mp4",
1680
    "mp4 format",
1681 4cb3f3b6 Dan Christiansen
    "application/mp4",
1682
    "mp4,m4a",
1683 1cb5f7fd Michael Niedermayer
    sizeof(MOVContext),
1684
    CODEC_ID_AAC,
1685
    CODEC_ID_MPEG4,
1686
    mov_write_header,
1687
    mov_write_packet,
1688
    mov_write_trailer,
1689 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1690 1cb5f7fd Michael Niedermayer
};
1691 ff70e601 Måns Rullgård
#endif
1692
#ifdef CONFIG_PSP_MUXER
1693
AVOutputFormat psp_muxer = {
1694 8af18154 tjcannell@blueyonder.co.uk
    "psp",
1695
    "psp mp4 format",
1696
    NULL,
1697
    "mp4,psp",
1698
    sizeof(MOVContext),
1699
    CODEC_ID_AAC,
1700
    CODEC_ID_MPEG4,
1701
    mov_write_header,
1702
    mov_write_packet,
1703
    mov_write_trailer,
1704 dcfdb046 Benjamin Larsson
    .flags = AVFMT_GLOBALHEADER,
1705 8af18154 tjcannell@blueyonder.co.uk
};
1706 ff70e601 Måns Rullgård
#endif
1707
#ifdef CONFIG_TG2_MUXER
1708
AVOutputFormat tg2_muxer = {
1709 8536ab89 tjraivio@cc.hut.fi
    "3g2",
1710
    "3gp2 format",
1711
    NULL,
1712
    "3g2",
1713
    sizeof(MOVContext),
1714
    CODEC_ID_AMR_NB,
1715
    CODEC_ID_H263,
1716
    mov_write_header,
1717
    mov_write_packet,
1718
    mov_write_trailer,
1719 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
1720 8536ab89 tjraivio@cc.hut.fi
};
1721 ff70e601 Måns Rullgård
#endif