Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ 0871ae1a

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