Statistics
| Branch: | Revision:

ffmpeg / libavformat / movenc.c @ fffdee89

History | View | Annotate | Download (78 KB)

1 1cb5f7fd Michael Niedermayer
/*
2 7fbde343 Aurelien Jacobs
 * MOV, 3GP, MP4 muxer
3 406792e7 Diego Biurrun
 * Copyright (c) 2003 Thomas Raivio
4
 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5 7c4502c8 Baptiste Coudurier
 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6 1cb5f7fd Michael Niedermayer
 *
7 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10 1cb5f7fd Michael Niedermayer
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
13 1cb5f7fd Michael Niedermayer
 *
14 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
15 1cb5f7fd Michael Niedermayer
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
21 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 1cb5f7fd Michael Niedermayer
 */
23 4909e517 Baptiste Coudurier
24 f72dad41 Martin Storsjö
#include "movenc.h"
25 1cb5f7fd Michael Niedermayer
#include "avformat.h"
26 0abdb293 Anton Khirnov
#include "avio_internal.h"
27 9d9f4119 Måns Rullgård
#include "riff.h"
28 1cb5f7fd Michael Niedermayer
#include "avio.h"
29 e40ee6a2 Baptiste Coudurier
#include "isom.h"
30 1bd2d763 Aurelien Jacobs
#include "avc.h"
31 9106a698 Stefano Sabatini
#include "libavcodec/get_bits.h"
32 b2755007 Stefano Sabatini
#include "libavcodec/put_bits.h"
33 e977af6f Martin Storsjö
#include "internal.h"
34
#include "libavutil/avstring.h"
35 1cb5f7fd Michael Niedermayer
36 6e6d6dc0 Michael Niedermayer
#undef NDEBUG
37
#include <assert.h>
38
39 90b5b51e Diego Biurrun
//FIXME support 64 bit variant with wide placeholders
40 ae628ec1 Anton Khirnov
static int64_t updateSize(AVIOContext *pb, int64_t pos)
41 1cb5f7fd Michael Niedermayer
{
42 bc5c918e Diego Biurrun
    int64_t curpos = url_ftell(pb);
43 6b4aa5da Anton Khirnov
    avio_seek(pb, pos, SEEK_SET);
44 77eb5504 Anton Khirnov
    avio_wb32(pb, curpos - pos); /* rewrite size */
45 6b4aa5da Anton Khirnov
    avio_seek(pb, curpos, SEEK_SET);
46 6e6d6dc0 Michael Niedermayer
47
    return curpos - pos;
48 1cb5f7fd Michael Niedermayer
}
49
50 e45ccf79 Gildas Bazin
/* Chunk offset atom */
51 ae628ec1 Anton Khirnov
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
52 1cb5f7fd Michael Niedermayer
{
53
    int i;
54 ed06cf6d Benjamin Larsson
    int mode64 = 0; //   use 32 bit size variant if possible
55 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
56 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
57 b29af723 Michael Niedermayer
    if (pos > UINT32_MAX) {
58
        mode64 = 1;
59 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "co64");
60 b29af723 Michael Niedermayer
    } else
61 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "stco");
62 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
63
    avio_wb32(pb, track->entry); /* entry count */
64 1cb5f7fd Michael Niedermayer
    for (i=0; i<track->entry; i++) {
65 b29af723 Michael Niedermayer
        if(mode64 == 1)
66 77eb5504 Anton Khirnov
            avio_wb64(pb, track->cluster[i].pos);
67 b29af723 Michael Niedermayer
        else
68 77eb5504 Anton Khirnov
            avio_wb32(pb, track->cluster[i].pos);
69 1cb5f7fd Michael Niedermayer
    }
70 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
71 1cb5f7fd Michael Niedermayer
}
72
73 e45ccf79 Gildas Bazin
/* Sample size atom */
74 ae628ec1 Anton Khirnov
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
75 1cb5f7fd Michael Niedermayer
{
76 f578f938 Thomas Raivio
    int equalChunks = 1;
77 e45ccf79 Gildas Bazin
    int i, j, entries = 0, tst = -1, oldtst = -1;
78 1cb5f7fd Michael Niedermayer
79 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
80 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
81 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "stsz");
82 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
83 1cb5f7fd Michael Niedermayer
84 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
85 0aec3c5c Baptiste Coudurier
        tst = track->cluster[i].size/track->cluster[i].entries;
86 e45ccf79 Gildas Bazin
        if(oldtst != -1 && tst != oldtst) {
87
            equalChunks = 0;
88 f578f938 Thomas Raivio
        }
89
        oldtst = tst;
90 0aec3c5c Baptiste Coudurier
        entries += track->cluster[i].entries;
91 f578f938 Thomas Raivio
    }
92 e45ccf79 Gildas Bazin
    if (equalChunks) {
93 0aec3c5c Baptiste Coudurier
        int sSize = track->cluster[0].size/track->cluster[0].entries;
94 77eb5504 Anton Khirnov
        avio_wb32(pb, sSize); // sample size
95
        avio_wb32(pb, entries); // sample count
96 1cb5f7fd Michael Niedermayer
    }
97 f578f938 Thomas Raivio
    else {
98 77eb5504 Anton Khirnov
        avio_wb32(pb, 0); // sample size
99
        avio_wb32(pb, entries); // sample count
100 f578f938 Thomas Raivio
        for (i=0; i<track->entry; i++) {
101 91208916 Baptiste Coudurier
            for (j=0; j<track->cluster[i].entries; j++) {
102 77eb5504 Anton Khirnov
                avio_wb32(pb, track->cluster[i].size /
103 0aec3c5c Baptiste Coudurier
                         track->cluster[i].entries);
104 e45ccf79 Gildas Bazin
            }
105 1cb5f7fd Michael Niedermayer
        }
106
    }
107 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
108 1cb5f7fd Michael Niedermayer
}
109
110 e45ccf79 Gildas Bazin
/* Sample to chunk atom */
111 ae628ec1 Anton Khirnov
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
112 1cb5f7fd Michael Niedermayer
{
113 b29af723 Michael Niedermayer
    int index = 0, oldval = -1, i;
114 bc5c918e Diego Biurrun
    int64_t entryPos, curpos;
115 f578f938 Thomas Raivio
116 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
117 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
118 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "stsc");
119 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); // version & flags
120 f578f938 Thomas Raivio
    entryPos = url_ftell(pb);
121 77eb5504 Anton Khirnov
    avio_wb32(pb, track->entry); // entry count
122 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
123 0aec3c5c Baptiste Coudurier
        if(oldval != track->cluster[i].samplesInChunk)
124 f578f938 Thomas Raivio
        {
125 77eb5504 Anton Khirnov
            avio_wb32(pb, i+1); // first chunk
126
            avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk
127
            avio_wb32(pb, 0x1); // sample description index
128 0aec3c5c Baptiste Coudurier
            oldval = track->cluster[i].samplesInChunk;
129 f578f938 Thomas Raivio
            index++;
130 1cb5f7fd Michael Niedermayer
        }
131
    }
132 f578f938 Thomas Raivio
    curpos = url_ftell(pb);
133 6b4aa5da Anton Khirnov
    avio_seek(pb, entryPos, SEEK_SET);
134 77eb5504 Anton Khirnov
    avio_wb32(pb, index); // rewrite size
135 6b4aa5da Anton Khirnov
    avio_seek(pb, curpos, SEEK_SET);
136 1cb5f7fd Michael Niedermayer
137 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
138 1cb5f7fd Michael Niedermayer
}
139
140 e45ccf79 Gildas Bazin
/* Sync sample atom */
141 ae628ec1 Anton Khirnov
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
142 1cb5f7fd Michael Niedermayer
{
143 bc5c918e Diego Biurrun
    int64_t curpos, entryPos;
144 b29af723 Michael Niedermayer
    int i, index = 0;
145 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
146 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); // size
147 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
148 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); // version & flags
149 f578f938 Thomas Raivio
    entryPos = url_ftell(pb);
150 77eb5504 Anton Khirnov
    avio_wb32(pb, track->entry); // entry count
151 f578f938 Thomas Raivio
    for (i=0; i<track->entry; i++) {
152 e1316b19 Baptiste Coudurier
        if (track->cluster[i].flags & flag) {
153 77eb5504 Anton Khirnov
            avio_wb32(pb, i+1);
154 f578f938 Thomas Raivio
            index++;
155
        }
156
    }
157
    curpos = url_ftell(pb);
158 6b4aa5da Anton Khirnov
    avio_seek(pb, entryPos, SEEK_SET);
159 77eb5504 Anton Khirnov
    avio_wb32(pb, index); // rewrite size
160 6b4aa5da Anton Khirnov
    avio_seek(pb, curpos, SEEK_SET);
161 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
162 1cb5f7fd Michael Niedermayer
}
163
164 ae628ec1 Anton Khirnov
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
165 1cb5f7fd Michael Niedermayer
{
166 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x11); /* size */
167 0abdb293 Anton Khirnov
    if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
168
    else                         ffio_wfourcc(pb, "damr");
169
    ffio_wfourcc(pb, "FFMP");
170 77eb5504 Anton Khirnov
    avio_w8(pb, 0); /* decoder version */
171 3a72cbd9 Baptiste Coudurier
172 77eb5504 Anton Khirnov
    avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
173
    avio_w8(pb, 0x00); /* Mode change period (no restriction) */
174
    avio_w8(pb, 0x01); /* Frames per sample */
175 3a72cbd9 Baptiste Coudurier
    return 0x11;
176
}
177
178 ae628ec1 Anton Khirnov
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
179 d4a240cb Baptiste Coudurier
{
180
    GetBitContext gbc;
181
    PutBitContext pbc;
182
    uint8_t buf[3];
183
    int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
184
185
    if (track->vosLen < 7)
186
        return -1;
187
188 77eb5504 Anton Khirnov
    avio_wb32(pb, 11);
189 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "dac3");
190 d4a240cb Baptiste Coudurier
191 544a0f47 Damiano Galassi
    init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
192 d4a240cb Baptiste Coudurier
    fscod      = get_bits(&gbc, 2);
193
    frmsizecod = get_bits(&gbc, 6);
194
    bsid       = get_bits(&gbc, 5);
195
    bsmod      = get_bits(&gbc, 3);
196
    acmod      = get_bits(&gbc, 3);
197
    if (acmod == 2) {
198
        skip_bits(&gbc, 2); // dsurmod
199
    } else {
200
        if ((acmod & 1) && acmod != 1)
201
            skip_bits(&gbc, 2); // cmixlev
202
        if (acmod & 4)
203
            skip_bits(&gbc, 2); // surmixlev
204
    }
205
    lfeon = get_bits1(&gbc);
206
207
    init_put_bits(&pbc, buf, sizeof(buf));
208
    put_bits(&pbc, 2, fscod);
209
    put_bits(&pbc, 5, bsid);
210
    put_bits(&pbc, 3, bsmod);
211
    put_bits(&pbc, 3, acmod);
212
    put_bits(&pbc, 1, lfeon);
213
    put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
214
    put_bits(&pbc, 5, 0); // reserved
215
216
    flush_put_bits(&pbc);
217 77eb5504 Anton Khirnov
    avio_write(pb, buf, sizeof(buf));
218 d4a240cb Baptiste Coudurier
219
    return 11;
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 ae628ec1 Anton Khirnov
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
227 463e7afd Baptiste Coudurier
{
228 77eb5504 Anton Khirnov
    avio_write(pb, track->enc->extradata, track->enc->extradata_size);
229 463e7afd Baptiste Coudurier
    return track->enc->extradata_size;
230
}
231
232 ae628ec1 Anton Khirnov
static int mov_write_enda_tag(AVIOContext *pb)
233 fcef991a Baptiste Coudurier
{
234 77eb5504 Anton Khirnov
    avio_wb32(pb, 10);
235 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "enda");
236 77eb5504 Anton Khirnov
    avio_wb16(pb, 1); /* little endian */
237 fcef991a Baptiste Coudurier
    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 ae628ec1 Anton Khirnov
static void putDescr(AVIOContext *pb, int tag, unsigned int size)
248 039627cf Baptiste Coudurier
{
249 08db8f18 Michael Niedermayer
    int i= descrLength(size) - size - 2;
250 77eb5504 Anton Khirnov
    avio_w8(pb, tag);
251 08db8f18 Michael Niedermayer
    for(; i>0; i--)
252 77eb5504 Anton Khirnov
        avio_w8(pb, (size>>(7*i)) | 0x80);
253
    avio_w8(pb, size & 0x7F);
254 039627cf Baptiste Coudurier
}
255
256 ae628ec1 Anton Khirnov
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
257 039627cf Baptiste Coudurier
{
258 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
259 fcef991a Baptiste Coudurier
    int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
260 039627cf Baptiste Coudurier
261 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); // size
262 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "esds");
263 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); // Version
264 039627cf Baptiste Coudurier
265
    // ES descriptor
266
    putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
267
             descrLength(1));
268 77eb5504 Anton Khirnov
    avio_wb16(pb, track->trackID);
269
    avio_w8(pb, 0x00); // flags (= no flags)
270 039627cf Baptiste Coudurier
271
    // DecoderConfig descriptor
272
    putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
273
274
    // Object type indication
275 dfce888f Baptiste Coudurier
    if ((track->enc->codec_id == CODEC_ID_MP2 ||
276
         track->enc->codec_id == CODEC_ID_MP3) &&
277 c4e02d70 Baptiste Coudurier
        track->enc->sample_rate > 24000)
278 77eb5504 Anton Khirnov
        avio_w8(pb, 0x6B); // 11172-3
279 dfce888f Baptiste Coudurier
    else
280 77eb5504 Anton Khirnov
        avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
281 039627cf Baptiste Coudurier
282
    // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
283
    // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
284 72415b2a Stefano Sabatini
    if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
285 77eb5504 Anton Khirnov
        avio_w8(pb, 0x15); // flags (= Audiostream)
286 039627cf Baptiste Coudurier
    else
287 77eb5504 Anton Khirnov
        avio_w8(pb, 0x11); // flags (= Visualstream)
288 039627cf Baptiste Coudurier
289 77eb5504 Anton Khirnov
    avio_w8(pb,  track->enc->rc_buffer_size>>(3+16));      // Buffersize DB (24 bits)
290
    avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
291 039627cf Baptiste Coudurier
292 77eb5504 Anton Khirnov
    avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
293 039627cf Baptiste Coudurier
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
294 77eb5504 Anton Khirnov
        avio_wb32(pb, 0); // vbr
295 039627cf Baptiste Coudurier
    else
296 77eb5504 Anton Khirnov
        avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
297 039627cf Baptiste Coudurier
298 287d6cfa Baptiste Coudurier
    if (track->vosLen) {
299 039627cf Baptiste Coudurier
        // DecoderSpecific info descriptor
300
        putDescr(pb, 0x05, track->vosLen);
301 77eb5504 Anton Khirnov
        avio_write(pb, track->vosData, track->vosLen);
302 039627cf Baptiste Coudurier
    }
303
304
    // SL descriptor
305
    putDescr(pb, 0x06, 1);
306 77eb5504 Anton Khirnov
    avio_w8(pb, 0x02);
307 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
308 039627cf Baptiste Coudurier
}
309
310 7c4b7d0f Baptiste Coudurier
static int mov_pcm_le_gt16(enum CodecID codec_id)
311
{
312
    return codec_id == CODEC_ID_PCM_S24LE ||
313
           codec_id == CODEC_ID_PCM_S32LE ||
314
           codec_id == CODEC_ID_PCM_F32LE ||
315
           codec_id == CODEC_ID_PCM_F64LE;
316
}
317
318 ae628ec1 Anton Khirnov
static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
319 f2589642 Baptiste Coudurier
{
320
    int64_t pos = url_ftell(pb);
321 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
322
    avio_wl32(pb, track->tag); // store it byteswapped
323 795ed278 Janne Grunau
    track->enc->codec_tag = av_bswap16(track->tag >> 16);
324 f2589642 Baptiste Coudurier
    ff_put_wav_header(pb, track->enc);
325
    return updateSize(pb, pos);
326
}
327
328 ae628ec1 Anton Khirnov
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
329 69dde1ad Gildas Bazin
{
330 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
331 69dde1ad Gildas Bazin
332 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);     /* size */
333 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "wave");
334 69dde1ad Gildas Bazin
335 77eb5504 Anton Khirnov
    avio_wb32(pb, 12);    /* size */
336 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "frma");
337 77eb5504 Anton Khirnov
    avio_wl32(pb, track->tag);
338 69dde1ad Gildas Bazin
339 fcef991a Baptiste Coudurier
    if (track->enc->codec_id == CODEC_ID_AAC) {
340 aa1c1c61 Baptiste Coudurier
        /* useless atom needed by mplayer, ipod, not needed by quicktime */
341 77eb5504 Anton Khirnov
        avio_wb32(pb, 12); /* size */
342 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "mp4a");
343 77eb5504 Anton Khirnov
        avio_wb32(pb, 0);
344 fcef991a Baptiste Coudurier
        mov_write_esds_tag(pb, track);
345 7c4b7d0f Baptiste Coudurier
    } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
346 fcef991a Baptiste Coudurier
        mov_write_enda_tag(pb);
347 3a72cbd9 Baptiste Coudurier
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
348 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
349 d4a240cb Baptiste Coudurier
    } else if (track->enc->codec_id == CODEC_ID_AC3) {
350
        mov_write_ac3_tag(pb, track);
351 463e7afd Baptiste Coudurier
    } else if (track->enc->codec_id == CODEC_ID_ALAC) {
352
        mov_write_extradata_tag(pb, track);
353 f2589642 Baptiste Coudurier
    } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
354
               track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
355
        mov_write_ms_tag(pb, track);
356 fcef991a Baptiste Coudurier
    }
357 69dde1ad Gildas Bazin
358 77eb5504 Anton Khirnov
    avio_wb32(pb, 8);     /* size */
359
    avio_wb32(pb, 0);     /* null tag */
360 69dde1ad Gildas Bazin
361 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
362 69dde1ad Gildas Bazin
}
363
364 ae628ec1 Anton Khirnov
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
365 79e42311 Baptiste Coudurier
{
366 77eb5504 Anton Khirnov
    avio_wb32(pb, track->vosLen+8);
367 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "glbl");
368 77eb5504 Anton Khirnov
    avio_write(pb, track->vosData, track->vosLen);
369 79e42311 Baptiste Coudurier
    return 8+track->vosLen;
370
}
371
372 feaa8d11 Baptiste Coudurier
/**
373
 * Compute flags for 'lpcm' tag.
374
 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
375
 */
376
static int mov_get_lpcm_flags(enum CodecID codec_id)
377
{
378
    switch (codec_id) {
379
    case CODEC_ID_PCM_F32BE:
380
    case CODEC_ID_PCM_F64BE:
381
        return 11;
382
    case CODEC_ID_PCM_F32LE:
383
    case CODEC_ID_PCM_F64LE:
384
        return 9;
385
    case CODEC_ID_PCM_U8:
386
        return 10;
387
    case CODEC_ID_PCM_S16BE:
388
    case CODEC_ID_PCM_S24BE:
389
    case CODEC_ID_PCM_S32BE:
390
        return 14;
391
    case CODEC_ID_PCM_S8:
392
    case CODEC_ID_PCM_S16LE:
393
    case CODEC_ID_PCM_S24LE:
394
    case CODEC_ID_PCM_S32LE:
395
        return 12;
396
    default:
397
        return 0;
398
    }
399
}
400
401 ae628ec1 Anton Khirnov
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
402 1cb5f7fd Michael Niedermayer
{
403 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
404 feaa8d11 Baptiste Coudurier
    int version = 0;
405
    uint32_t tag = track->tag;
406
407
    if (track->mode == MODE_MOV) {
408
        if (track->timescale > UINT16_MAX) {
409
            if (mov_get_lpcm_flags(track->enc->codec_id))
410
                tag = AV_RL32("lpcm");
411
            version = 2;
412 f2589642 Baptiste Coudurier
        } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
413
                   track->enc->codec_id == CODEC_ID_ADPCM_MS ||
414
                   track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
415 feaa8d11 Baptiste Coudurier
            version = 1;
416
        }
417
    }
418 115329f1 Diego Biurrun
419 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
420
    avio_wl32(pb, tag); // store it byteswapped
421
    avio_wb32(pb, 0); /* Reserved */
422
    avio_wb16(pb, 0); /* Reserved */
423
    avio_wb16(pb, 1); /* Data-reference index, XXX  == 1 */
424 69dde1ad Gildas Bazin
425 e45ccf79 Gildas Bazin
    /* SoundDescription */
426 77eb5504 Anton Khirnov
    avio_wb16(pb, version); /* Version */
427
    avio_wb16(pb, 0); /* Revision level */
428
    avio_wb32(pb, 0); /* Reserved */
429 1cb5f7fd Michael Niedermayer
430 feaa8d11 Baptiste Coudurier
    if (version == 2) {
431 77eb5504 Anton Khirnov
        avio_wb16(pb, 3);
432
        avio_wb16(pb, 16);
433
        avio_wb16(pb, 0xfffe);
434
        avio_wb16(pb, 0);
435
        avio_wb32(pb, 0x00010000);
436
        avio_wb32(pb, 72);
437
        avio_wb64(pb, av_dbl2int(track->timescale));
438
        avio_wb32(pb, track->enc->channels);
439
        avio_wb32(pb, 0x7F000000);
440
        avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
441
        avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
442
        avio_wb32(pb, track->sampleSize);
443
        avio_wb32(pb, track->enc->frame_size);
444 feaa8d11 Baptiste Coudurier
    } else {
445 ba7c0ece Baptiste Coudurier
        if (track->mode == MODE_MOV) {
446 77eb5504 Anton Khirnov
            avio_wb16(pb, track->enc->channels);
447 ba7c0ece Baptiste Coudurier
            if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
448
                track->enc->codec_id == CODEC_ID_PCM_S8)
449 77eb5504 Anton Khirnov
                avio_wb16(pb, 8); /* bits per sample */
450 ba7c0ece Baptiste Coudurier
            else
451 77eb5504 Anton Khirnov
                avio_wb16(pb, 16);
452
            avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
453 ba7c0ece Baptiste Coudurier
        } else { /* reserved for mp4/3gp */
454 77eb5504 Anton Khirnov
            avio_wb16(pb, 2);
455
            avio_wb16(pb, 16);
456
            avio_wb16(pb, 0);
457 ba7c0ece Baptiste Coudurier
        }
458 ef19c7eb Gildas Bazin
459 77eb5504 Anton Khirnov
        avio_wb16(pb, 0); /* packet size (= 0) */
460
        avio_wb16(pb, track->timescale); /* Time scale */
461
        avio_wb16(pb, 0); /* Reserved */
462 feaa8d11 Baptiste Coudurier
    }
463 1cb5f7fd Michael Niedermayer
464 5cb49ca1 Baptiste Coudurier
    if(version == 1) { /* SoundDescription V1 extended info */
465 77eb5504 Anton Khirnov
        avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
466
        avio_wb32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
467
        avio_wb32(pb, track->sampleSize); /* Bytes per frame */
468
        avio_wb32(pb, 2); /* Bytes per sample */
469 69dde1ad Gildas Bazin
    }
470
471 6dd19fff Baptiste Coudurier
    if(track->mode == MODE_MOV &&
472
       (track->enc->codec_id == CODEC_ID_AAC ||
473 4ed19420 Kurtnoise
        track->enc->codec_id == CODEC_ID_AC3 ||
474 6dd19fff Baptiste Coudurier
        track->enc->codec_id == CODEC_ID_AMR_NB ||
475 7c4b7d0f Baptiste Coudurier
        track->enc->codec_id == CODEC_ID_ALAC ||
476 f2589642 Baptiste Coudurier
        track->enc->codec_id == CODEC_ID_ADPCM_MS ||
477
        track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
478 7c4b7d0f Baptiste Coudurier
        mov_pcm_le_gt16(track->enc->codec_id)))
479 fcef991a Baptiste Coudurier
        mov_write_wave_tag(pb, track);
480 d0c0a29b Baptiste Coudurier
    else if(track->tag == MKTAG('m','p','4','a'))
481 3a72cbd9 Baptiste Coudurier
        mov_write_esds_tag(pb, track);
482
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
483 b7d9da10 Baptiste Coudurier
        mov_write_amr_tag(pb, track);
484 4ed19420 Kurtnoise
    else if(track->enc->codec_id == CODEC_ID_AC3)
485
        mov_write_ac3_tag(pb, track);
486 efa1fb39 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_ALAC)
487 709c9f8d Baptiste Coudurier
        mov_write_extradata_tag(pb, track);
488 79e42311 Baptiste Coudurier
    else if(track->vosLen > 0)
489
        mov_write_glbl_tag(pb, track);
490 3a72cbd9 Baptiste Coudurier
491 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
492 1cb5f7fd Michael Niedermayer
}
493
494 ae628ec1 Anton Khirnov
static int mov_write_d263_tag(AVIOContext *pb)
495 1cb5f7fd Michael Niedermayer
{
496 77eb5504 Anton Khirnov
    avio_wb32(pb, 0xf); /* size */
497 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "d263");
498
    ffio_wfourcc(pb, "FFMP");
499 77eb5504 Anton Khirnov
    avio_w8(pb, 0); /* decoder version */
500 87494ea0 Baptiste Coudurier
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
501 77eb5504 Anton Khirnov
    avio_w8(pb, 0xa); /* level */
502
    avio_w8(pb, 0); /* profile */
503 1cb5f7fd Michael Niedermayer
    return 0xf;
504
}
505
506 f578f938 Thomas Raivio
/* TODO: No idea about these values */
507 ae628ec1 Anton Khirnov
static int mov_write_svq3_tag(AVIOContext *pb)
508 1cb5f7fd Michael Niedermayer
{
509 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x15);
510 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "SMI ");
511
    ffio_wfourcc(pb, "SEQH");
512 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x5);
513
    avio_wb32(pb, 0xe2c0211d);
514
    avio_wb32(pb, 0xc0000000);
515
    avio_w8(pb, 0);
516 f578f938 Thomas Raivio
    return 0x15;
517 1cb5f7fd Michael Niedermayer
}
518
519 ae628ec1 Anton Khirnov
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
520 6e6eebf9 Aurelien Jacobs
{
521 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
522 6e6eebf9 Aurelien Jacobs
523 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
524 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "avcC");
525 9ab3f71b Aurelien Jacobs
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
526 c1b8e6d8 Baptiste Coudurier
    return updateSize(pb, pos);
527
}
528
529 fc4cbc16 Baptiste Coudurier
/* also used by all avid codecs (dv, imx, meridien) and their variants */
530 ae628ec1 Anton Khirnov
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
531 fc4cbc16 Baptiste Coudurier
{
532
    int i;
533 77eb5504 Anton Khirnov
    avio_wb32(pb, 24); /* size */
534 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "ACLR");
535
    ffio_wfourcc(pb, "ACLR");
536
    ffio_wfourcc(pb, "0001");
537 77eb5504 Anton Khirnov
    avio_wb32(pb, 1); /* yuv 1 / rgb 2 ? */
538
    avio_wb32(pb, 0); /* unknown */
539 fc4cbc16 Baptiste Coudurier
540 77eb5504 Anton Khirnov
    avio_wb32(pb, 24); /* size */
541 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "APRG");
542
    ffio_wfourcc(pb, "APRG");
543
    ffio_wfourcc(pb, "0001");
544 77eb5504 Anton Khirnov
    avio_wb32(pb, 1); /* unknown */
545
    avio_wb32(pb, 0); /* unknown */
546 fc4cbc16 Baptiste Coudurier
547 77eb5504 Anton Khirnov
    avio_wb32(pb, 120); /* size */
548 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "ARES");
549
    ffio_wfourcc(pb, "ARES");
550
    ffio_wfourcc(pb, "0001");
551 77eb5504 Anton Khirnov
    avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
552
    avio_wb32(pb, track->enc->width);
553 fc4cbc16 Baptiste Coudurier
    /* values below are based on samples created with quicktime and avid codecs */
554
    if (track->vosData[5] & 2) { // interlaced
555 77eb5504 Anton Khirnov
        avio_wb32(pb, track->enc->height/2);
556
        avio_wb32(pb, 2); /* unknown */
557
        avio_wb32(pb, 0); /* unknown */
558
        avio_wb32(pb, 4); /* unknown */
559 fc4cbc16 Baptiste Coudurier
    } else {
560 77eb5504 Anton Khirnov
        avio_wb32(pb, track->enc->height);
561
        avio_wb32(pb, 1); /* unknown */
562
        avio_wb32(pb, 0); /* unknown */
563 44fb8ebb Baptiste Coudurier
        if (track->enc->height == 1080)
564 77eb5504 Anton Khirnov
            avio_wb32(pb, 5); /* unknown */
565 44fb8ebb Baptiste Coudurier
        else
566 77eb5504 Anton Khirnov
            avio_wb32(pb, 6); /* unknown */
567 fc4cbc16 Baptiste Coudurier
    }
568
    /* padding */
569
    for (i = 0; i < 10; i++)
570 77eb5504 Anton Khirnov
        avio_wb64(pb, 0);
571 fc4cbc16 Baptiste Coudurier
572
    /* extra padding for stsd needed */
573 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
574 fc4cbc16 Baptiste Coudurier
    return 0;
575
}
576
577 6b600285 Baptiste Coudurier
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
578 8e321619 Baptiste Coudurier
{
579 039627cf Baptiste Coudurier
    int tag = track->enc->codec_tag;
580 2ab57c04 Baptiste Coudurier
581 1a40491e Daniel Verkamp
    if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
582 2ab57c04 Baptiste Coudurier
        return 0;
583
584
    if      (track->enc->codec_id == CODEC_ID_H264)      tag = MKTAG('a','v','c','1');
585
    else if (track->enc->codec_id == CODEC_ID_AC3)       tag = MKTAG('a','c','-','3');
586
    else if (track->enc->codec_id == CODEC_ID_DIRAC)     tag = MKTAG('d','r','a','c');
587
    else if (track->enc->codec_id == CODEC_ID_MOV_TEXT)  tag = MKTAG('t','x','3','g');
588 72415b2a Stefano Sabatini
    else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
589
    else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
590 6b600285 Baptiste Coudurier
591
    return tag;
592
}
593
594 3c8d1447 Baptiste Coudurier
static const AVCodecTag codec_ipod_tags[] = {
595
    { CODEC_ID_H264,   MKTAG('a','v','c','1') },
596
    { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
597
    { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
598
    { CODEC_ID_ALAC,   MKTAG('a','l','a','c') },
599
    { CODEC_ID_AC3,    MKTAG('a','c','-','3') },
600
    { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
601
    { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
602
    { CODEC_ID_NONE, 0 },
603
};
604
605 6b600285 Baptiste Coudurier
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
606
{
607
    int tag = track->enc->codec_tag;
608
609 130ba4b1 Baptiste Coudurier
    // keep original tag for subs, ipod supports both formats
610 72415b2a Stefano Sabatini
    if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
611 2ab57c04 Baptiste Coudurier
        (tag == MKTAG('t','x','3','g') ||
612 b6cb9946 Baptiste Coudurier
         tag == MKTAG('t','e','x','t'))))
613 1a40491e Daniel Verkamp
        tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
614 0872acc4 Baptiste Coudurier
615 ade5b91d Stefano Sabatini
    if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
616 2ab57c04 Baptiste Coudurier
        av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
617
               "Quicktime/Ipod might not play the file\n");
618 6b600285 Baptiste Coudurier
619
    return tag;
620
}
621
622
static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
623
{
624
    int tag;
625
626 2ab57c04 Baptiste Coudurier
    if (track->enc->height == 480) /* NTSC */
627
        if  (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
628
        else                                         tag = MKTAG('d','v','c',' ');
629
    else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
630
    else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
631
    else                                             tag = MKTAG('d','v','p','p');
632 6b600285 Baptiste Coudurier
633
    return tag;
634
}
635
636 3c8d1447 Baptiste Coudurier
static const struct {
637
    enum PixelFormat pix_fmt;
638
    uint32_t tag;
639
    unsigned bps;
640
} mov_pix_fmt_tags[] = {
641
    { PIX_FMT_YUYV422, MKTAG('y','u','v','s'),  0 },
642
    { PIX_FMT_UYVY422, MKTAG('2','v','u','y'),  0 },
643 a7cc89e2 Baptiste Coudurier
    { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
644 f65aad95 Baptiste Coudurier
    { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
645
    { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
646
    { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
647 9a191b3a Baptiste Coudurier
    { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
648 3c8d1447 Baptiste Coudurier
    { PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
649 f65aad95 Baptiste Coudurier
    { PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
650 dfb0471f Baptiste Coudurier
    { PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
651 5b50b8f5 Baptiste Coudurier
    { PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
652 ed88074a Baptiste Coudurier
    { PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
653 a7cc89e2 Baptiste Coudurier
    { PIX_FMT_ABGR,    MKTAG('A','B','G','R'), 32 },
654 9a191b3a Baptiste Coudurier
    { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
655 3c8d1447 Baptiste Coudurier
};
656
657 6b600285 Baptiste Coudurier
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
658
{
659
    int tag = track->enc->codec_tag;
660 2ab57c04 Baptiste Coudurier
    int i;
661
662
    for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
663
        if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
664
            tag = mov_pix_fmt_tags[i].tag;
665
            track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
666
            break;
667
        }
668
    }
669 6b600285 Baptiste Coudurier
670
    return tag;
671
}
672
673
static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
674
{
675
    int tag = track->enc->codec_tag;
676
677
    if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
678 a2b7ed32 Baptiste Coudurier
                 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
679 6b600285 Baptiste Coudurier
                  track->enc->codec_id == CODEC_ID_RAWVIDEO ||
680 10d8eac9 Baptiste Coudurier
                  track->enc->codec_id == CODEC_ID_H263 ||
681 6b600285 Baptiste Coudurier
                  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
682 0872acc4 Baptiste Coudurier
        if (track->enc->codec_id == CODEC_ID_DVVIDEO)
683 6b600285 Baptiste Coudurier
            tag = mov_get_dv_codec_tag(s, track);
684 0872acc4 Baptiste Coudurier
        else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
685 6b600285 Baptiste Coudurier
            tag = mov_get_rawvideo_codec_tag(s, track);
686 72415b2a Stefano Sabatini
        else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
687 1a40491e Daniel Verkamp
            tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
688 08680ab8 Baptiste Coudurier
            if (!tag) { // if no mac fcc found, try with Microsoft tags
689 1a40491e Daniel Verkamp
                tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
690 08680ab8 Baptiste Coudurier
                if (tag)
691
                    av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
692
                           "the file may be unplayable!\n");
693
            }
694 72415b2a Stefano Sabatini
        } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
695 1a40491e Daniel Verkamp
            tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
696 08680ab8 Baptiste Coudurier
            if (!tag) { // if no mac fcc found, try with Microsoft tags
697 1a40491e Daniel Verkamp
                int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
698 08680ab8 Baptiste Coudurier
                if (ms_tag) {
699
                    tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
700
                    av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
701
                           "the file may be unplayable!\n");
702 d97f2144 Baptiste Coudurier
                }
703 08680ab8 Baptiste Coudurier
            }
704 72415b2a Stefano Sabatini
        } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
705 1a40491e Daniel Verkamp
            tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
706 a23c9c4a Baptiste Coudurier
    }
707 2ab57c04 Baptiste Coudurier
708 8e321619 Baptiste Coudurier
    return tag;
709
}
710
711 3c8d1447 Baptiste Coudurier
static const AVCodecTag codec_3gp_tags[] = {
712
    { CODEC_ID_H263,   MKTAG('s','2','6','3') },
713
    { CODEC_ID_H264,   MKTAG('a','v','c','1') },
714
    { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
715
    { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
716
    { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
717
    { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
718
    { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
719
    { CODEC_ID_NONE, 0 },
720
};
721
722 6b600285 Baptiste Coudurier
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
723
{
724
    int tag = track->enc->codec_tag;
725
726
    if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
727
        tag = mp4_get_codec_tag(s, track);
728
    else if (track->mode == MODE_IPOD)
729
        tag = ipod_get_codec_tag(s, track);
730
    else if (track->mode & MODE_3GP)
731 1a40491e Daniel Verkamp
        tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
732 6b600285 Baptiste Coudurier
    else
733
        tag = mov_get_codec_tag(s, track);
734
735
    return tag;
736
}
737
738 aa9f4208 Baptiste Coudurier
/** Write uuid atom.
739
 * Needed to make file play in iPods running newest firmware
740
 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
741
 */
742 ae628ec1 Anton Khirnov
static int mov_write_uuid_tag_ipod(AVIOContext *pb)
743 aa9f4208 Baptiste Coudurier
{
744 77eb5504 Anton Khirnov
    avio_wb32(pb, 28);
745 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "uuid");
746 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x6b6840f2);
747
    avio_wb32(pb, 0x5f244fc5);
748
    avio_wb32(pb, 0xba39a51b);
749
    avio_wb32(pb, 0xcf0323f3);
750
    avio_wb32(pb, 0x0);
751 aa9f4208 Baptiste Coudurier
    return 28;
752
}
753
754 ae628ec1 Anton Khirnov
static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
755 f6204886 David Conrad
{
756
    int64_t pos = url_ftell(pb);
757 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);    /* size */
758
    avio_wl32(pb, track->tag); // store it byteswapped
759
    avio_wb32(pb, 0);    /* Reserved */
760
    avio_wb16(pb, 0);    /* Reserved */
761
    avio_wb16(pb, 1);    /* Data-reference index */
762 f6204886 David Conrad
763
    if (track->enc->extradata_size)
764 77eb5504 Anton Khirnov
        avio_write(pb, track->enc->extradata, track->enc->extradata_size);
765 f6204886 David Conrad
766
    return updateSize(pb, pos);
767
}
768
769 ae628ec1 Anton Khirnov
static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
770 b015be21 Baptiste Coudurier
{
771
    AVRational sar;
772
    av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
773
              track->enc->sample_aspect_ratio.den, INT_MAX);
774
775 77eb5504 Anton Khirnov
    avio_wb32(pb, 16);
776 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "pasp");
777 77eb5504 Anton Khirnov
    avio_wb32(pb, sar.num);
778
    avio_wb32(pb, sar.den);
779 b015be21 Baptiste Coudurier
    return 16;
780
}
781
782 ae628ec1 Anton Khirnov
static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
783 1cb5f7fd Michael Niedermayer
{
784 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
785 53ffdd14 Roine Gustafsson
    char compressor_name[32];
786 8cc7a34d Alex Beregszaszi
787 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
788
    avio_wl32(pb, track->tag); // store it byteswapped
789
    avio_wb32(pb, 0); /* Reserved */
790
    avio_wb16(pb, 0); /* Reserved */
791
    avio_wb16(pb, 1); /* Data-reference index */
792 f578f938 Thomas Raivio
793 77eb5504 Anton Khirnov
    avio_wb16(pb, 0); /* Codec stream version */
794
    avio_wb16(pb, 0); /* Codec stream revision (=0) */
795 5997ed78 Baptiste Coudurier
    if (track->mode == MODE_MOV) {
796 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "FFMP"); /* Vendor */
797 915282e5 Baptiste Coudurier
        if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
798 77eb5504 Anton Khirnov
            avio_wb32(pb, 0); /* Temporal Quality */
799
            avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
800 915282e5 Baptiste Coudurier
        } else {
801 77eb5504 Anton Khirnov
            avio_wb32(pb, 0x200); /* Temporal Quality = normal */
802
            avio_wb32(pb, 0x200); /* Spatial Quality = normal */
803 915282e5 Baptiste Coudurier
        }
804 5997ed78 Baptiste Coudurier
    } else {
805 77eb5504 Anton Khirnov
        avio_wb32(pb, 0); /* Reserved */
806
        avio_wb32(pb, 0); /* Reserved */
807
        avio_wb32(pb, 0); /* Reserved */
808 5997ed78 Baptiste Coudurier
    }
809 77eb5504 Anton Khirnov
    avio_wb16(pb, track->enc->width); /* Video width */
810
    avio_wb16(pb, track->height); /* Video height */
811
    avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
812
    avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
813
    avio_wb32(pb, 0); /* Data size (= 0) */
814
    avio_wb16(pb, 1); /* Frame count (= 1) */
815 115329f1 Diego Biurrun
816 53ffdd14 Roine Gustafsson
    memset(compressor_name,0,32);
817 5997ed78 Baptiste Coudurier
    /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
818
    if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
819 53ffdd14 Roine Gustafsson
        strncpy(compressor_name,track->enc->codec->name,31);
820 77eb5504 Anton Khirnov
    avio_w8(pb, strlen(compressor_name));
821
    avio_write(pb, compressor_name, 31);
822 115329f1 Diego Biurrun
823 dd1c8f3e Luca Abeni
    if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
824 77eb5504 Anton Khirnov
        avio_wb16(pb, track->enc->bits_per_coded_sample);
825 ccac8438 Baptiste Coudurier
    else
826 77eb5504 Anton Khirnov
        avio_wb16(pb, 0x18); /* Reserved */
827
    avio_wb16(pb, 0xffff); /* Reserved */
828 d0c0a29b Baptiste Coudurier
    if(track->tag == MKTAG('m','p','4','v'))
829 f578f938 Thomas Raivio
        mov_write_esds_tag(pb, track);
830
    else if(track->enc->codec_id == CODEC_ID_H263)
831
        mov_write_d263_tag(pb);
832
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
833 115329f1 Diego Biurrun
        mov_write_svq3_tag(pb);
834 7b0f4c9a Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
835
        mov_write_avid_tag(pb, track);
836 aa9f4208 Baptiste Coudurier
    else if(track->enc->codec_id == CODEC_ID_H264) {
837 c1b8e6d8 Baptiste Coudurier
        mov_write_avcc_tag(pb, track);
838 aa9f4208 Baptiste Coudurier
        if(track->mode == MODE_IPOD)
839
            mov_write_uuid_tag_ipod(pb);
840 b64518e9 Baptiste Coudurier
    } else if(track->vosLen > 0)
841 79e42311 Baptiste Coudurier
        mov_write_glbl_tag(pb, track);
842 f578f938 Thomas Raivio
843 d184c86c Maksym Veremeyenko
    if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
844 b015be21 Baptiste Coudurier
        track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
845
        mov_write_pasp_tag(pb, track);
846
    }
847
848 f7635edb Baptiste Coudurier
    return updateSize(pb, pos);
849 1cb5f7fd Michael Niedermayer
}
850
851 ae628ec1 Anton Khirnov
static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
852 e977af6f Martin Storsjö
{
853
    int64_t pos = url_ftell(pb);
854 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
855 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "rtp ");
856 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* Reserved */
857
    avio_wb16(pb, 0); /* Reserved */
858
    avio_wb16(pb, 1); /* Data-reference index */
859 e977af6f Martin Storsjö
860 77eb5504 Anton Khirnov
    avio_wb16(pb, 1); /* Hint track version */
861
    avio_wb16(pb, 1); /* Highest compatible version */
862
    avio_wb32(pb, track->max_packet_size); /* Max packet size */
863 e977af6f Martin Storsjö
864 77eb5504 Anton Khirnov
    avio_wb32(pb, 12); /* size */
865 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "tims");
866 77eb5504 Anton Khirnov
    avio_wb32(pb, track->timescale);
867 e977af6f Martin Storsjö
868
    return updateSize(pb, pos);
869
}
870
871 ae628ec1 Anton Khirnov
static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
872 1cb5f7fd Michael Niedermayer
{
873 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
874 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
875 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "stsd");
876 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
877
    avio_wb32(pb, 1); /* entry count */
878 72415b2a Stefano Sabatini
    if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
879 f578f938 Thomas Raivio
        mov_write_video_tag(pb, track);
880 72415b2a Stefano Sabatini
    else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
881 f578f938 Thomas Raivio
        mov_write_audio_tag(pb, track);
882 72415b2a Stefano Sabatini
    else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
883 f6204886 David Conrad
        mov_write_subtitle_tag(pb, track);
884 e977af6f Martin Storsjö
    else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
885
        mov_write_rtp_tag(pb, track);
886 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
887 1cb5f7fd Michael Niedermayer
}
888
889 ae628ec1 Anton Khirnov
static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
890 b4712e3c Baptiste Coudurier
{
891 c3e92a6c Baptiste Coudurier
    MOVStts *ctts_entries;
892 b4712e3c Baptiste Coudurier
    uint32_t entries = 0;
893
    uint32_t atom_size;
894
    int i;
895
896
    ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
897
    ctts_entries[0].count = 1;
898 0aec3c5c Baptiste Coudurier
    ctts_entries[0].duration = track->cluster[0].cts;
899 b4712e3c Baptiste Coudurier
    for (i=1; i<track->entry; i++) {
900 0aec3c5c Baptiste Coudurier
        if (track->cluster[i].cts == ctts_entries[entries].duration) {
901 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count++; /* compress */
902
        } else {
903
            entries++;
904 0aec3c5c Baptiste Coudurier
            ctts_entries[entries].duration = track->cluster[i].cts;
905 b4712e3c Baptiste Coudurier
            ctts_entries[entries].count = 1;
906
        }
907
    }
908
    entries++; /* last one */
909
    atom_size = 16 + (entries * 8);
910 77eb5504 Anton Khirnov
    avio_wb32(pb, atom_size); /* size */
911 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "ctts");
912 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
913
    avio_wb32(pb, entries); /* entry count */
914 b4712e3c Baptiste Coudurier
    for (i=0; i<entries; i++) {
915 77eb5504 Anton Khirnov
        avio_wb32(pb, ctts_entries[i].count);
916
        avio_wb32(pb, ctts_entries[i].duration);
917 b4712e3c Baptiste Coudurier
    }
918
    av_free(ctts_entries);
919
    return atom_size;
920
}
921
922 e45ccf79 Gildas Bazin
/* Time to sample atom */
923 ae628ec1 Anton Khirnov
static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
924 1cb5f7fd Michael Niedermayer
{
925 c3e92a6c Baptiste Coudurier
    MOVStts *stts_entries;
926 aa90239f Baptiste Coudurier
    uint32_t entries = -1;
927
    uint32_t atom_size;
928
    int i;
929
930 72415b2a Stefano Sabatini
    if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
931 aa90239f Baptiste Coudurier
        stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
932
        stts_entries[0].count = track->sampleCount;
933
        stts_entries[0].duration = 1;
934
        entries = 1;
935
    } else {
936
        stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
937
        for (i=0; i<track->entry; i++) {
938
            int64_t duration = i + 1 == track->entry ?
939
                track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
940
                track->cluster[i+1].dts - track->cluster[i].dts;
941
            if (i && duration == stts_entries[entries].duration) {
942
                stts_entries[entries].count++; /* compress */
943
            } else {
944
                entries++;
945
                stts_entries[entries].duration = duration;
946
                stts_entries[entries].count = 1;
947
            }
948
        }
949
        entries++; /* last one */
950
    }
951
    atom_size = 16 + (entries * 8);
952 77eb5504 Anton Khirnov
    avio_wb32(pb, atom_size); /* size */
953 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "stts");
954 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
955
    avio_wb32(pb, entries); /* entry count */
956 aa90239f Baptiste Coudurier
    for (i=0; i<entries; i++) {
957 77eb5504 Anton Khirnov
        avio_wb32(pb, stts_entries[i].count);
958
        avio_wb32(pb, stts_entries[i].duration);
959 aa90239f Baptiste Coudurier
    }
960
    av_free(stts_entries);
961
    return atom_size;
962 1cb5f7fd Michael Niedermayer
}
963
964 ae628ec1 Anton Khirnov
static int mov_write_dref_tag(AVIOContext *pb)
965 1cb5f7fd Michael Niedermayer
{
966 77eb5504 Anton Khirnov
    avio_wb32(pb, 28); /* size */
967 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "dref");
968 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
969
    avio_wb32(pb, 1); /* entry count */
970 1cb5f7fd Michael Niedermayer
971 77eb5504 Anton Khirnov
    avio_wb32(pb, 0xc); /* size */
972 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "url ");
973 77eb5504 Anton Khirnov
    avio_wb32(pb, 1); /* version & flags */
974 1cb5f7fd Michael Niedermayer
975
    return 28;
976
}
977
978 ae628ec1 Anton Khirnov
static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
979 1cb5f7fd Michael Niedermayer
{
980 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
981 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
982 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "stbl");
983 6e6d6dc0 Michael Niedermayer
    mov_write_stsd_tag(pb, track);
984
    mov_write_stts_tag(pb, track);
985 e977af6f Martin Storsjö
    if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
986
         track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
987 b371539a Baptiste Coudurier
        track->hasKeyframes && track->hasKeyframes < track->entry)
988 e1316b19 Baptiste Coudurier
        mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
989
    if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
990
        mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
991 72415b2a Stefano Sabatini
    if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
992 e1316b19 Baptiste Coudurier
        track->flags & MOV_TRACK_CTTS)
993 b4712e3c Baptiste Coudurier
        mov_write_ctts_tag(pb, track);
994 6e6d6dc0 Michael Niedermayer
    mov_write_stsc_tag(pb, track);
995
    mov_write_stsz_tag(pb, track);
996
    mov_write_stco_tag(pb, track);
997
    return updateSize(pb, pos);
998 1cb5f7fd Michael Niedermayer
}
999
1000 ae628ec1 Anton Khirnov
static int mov_write_dinf_tag(AVIOContext *pb)
1001 1cb5f7fd Michael Niedermayer
{
1002 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1003 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1004 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "dinf");
1005 6e6d6dc0 Michael Niedermayer
    mov_write_dref_tag(pb);
1006
    return updateSize(pb, pos);
1007 1cb5f7fd Michael Niedermayer
}
1008
1009 ae628ec1 Anton Khirnov
static int mov_write_nmhd_tag(AVIOContext *pb)
1010 f6204886 David Conrad
{
1011 77eb5504 Anton Khirnov
    avio_wb32(pb, 12);
1012 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "nmhd");
1013 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
1014 f6204886 David Conrad
    return 12;
1015
}
1016
1017 ae628ec1 Anton Khirnov
static int mov_write_gmhd_tag(AVIOContext *pb)
1018 f6204886 David Conrad
{
1019 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x20);   /* size */
1020 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "gmhd");
1021 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x18);   /* gmin size */
1022 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "gmin");/* generic media info */
1023 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);      /* version & flags */
1024
    avio_wb16(pb, 0x40);   /* graphics mode = */
1025
    avio_wb16(pb, 0x8000); /* opColor (r?) */
1026
    avio_wb16(pb, 0x8000); /* opColor (g?) */
1027
    avio_wb16(pb, 0x8000); /* opColor (b?) */
1028
    avio_wb16(pb, 0);      /* balance */
1029
    avio_wb16(pb, 0);      /* reserved */
1030 f6204886 David Conrad
    return 0x20;
1031
}
1032
1033 ae628ec1 Anton Khirnov
static int mov_write_smhd_tag(AVIOContext *pb)
1034 1cb5f7fd Michael Niedermayer
{
1035 77eb5504 Anton Khirnov
    avio_wb32(pb, 16); /* size */
1036 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "smhd");
1037 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version & flags */
1038
    avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
1039
    avio_wb16(pb, 0); /* reserved */
1040 1cb5f7fd Michael Niedermayer
    return 16;
1041
}
1042
1043 ae628ec1 Anton Khirnov
static int mov_write_vmhd_tag(AVIOContext *pb)
1044 1cb5f7fd Michael Niedermayer
{
1045 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x14); /* size (always 0x14) */
1046 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "vmhd");
1047 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x01); /* version & flags */
1048
    avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
1049 1cb5f7fd Michael Niedermayer
    return 0x14;
1050
}
1051
1052 ae628ec1 Anton Khirnov
static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
1053 1cb5f7fd Michael Niedermayer
{
1054 985688b8 Baptiste Coudurier
    const char *hdlr, *descr = NULL, *hdlr_type = NULL;
1055 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1056 115329f1 Diego Biurrun
1057 9a4d9388 Roman Shaposhnik
    if (!track) { /* no media --> data handler */
1058 bb270c08 Diego Biurrun
        hdlr = "dhlr";
1059
        hdlr_type = "url ";
1060
        descr = "DataHandler";
1061 9a4d9388 Roman Shaposhnik
    } else {
1062 bb270c08 Diego Biurrun
        hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
1063 72415b2a Stefano Sabatini
        if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
1064 bb270c08 Diego Biurrun
            hdlr_type = "vide";
1065
            descr = "VideoHandler";
1066 72415b2a Stefano Sabatini
        } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
1067 bb270c08 Diego Biurrun
            hdlr_type = "soun";
1068
            descr = "SoundHandler";
1069 72415b2a Stefano Sabatini
        } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1070 9f520554 Baptiste Coudurier
            if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
1071 80357cfc Baptiste Coudurier
            else                                      hdlr_type = "text";
1072 f6204886 David Conrad
            descr = "SubtitleHandler";
1073 e977af6f Martin Storsjö
        } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
1074
            hdlr_type = "hint";
1075
            descr = "HintHandler";
1076 bb270c08 Diego Biurrun
        }
1077 9a4d9388 Roman Shaposhnik
    }
1078 115329f1 Diego Biurrun
1079 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1080 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "hdlr");
1081 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* Version & flags */
1082
    avio_write(pb, hdlr, 4); /* handler */
1083 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, hdlr_type); /* handler type */
1084 77eb5504 Anton Khirnov
    avio_wb32(pb ,0); /* reserved */
1085
    avio_wb32(pb ,0); /* reserved */
1086
    avio_wb32(pb ,0); /* reserved */
1087 606d48ce Larbi Joubala
    if (!track || track->mode == MODE_MOV)
1088 77eb5504 Anton Khirnov
        avio_w8(pb, strlen(descr)); /* pascal string */
1089
    avio_write(pb, descr, strlen(descr)); /* handler description */
1090 606d48ce Larbi Joubala
    if (track && track->mode != MODE_MOV)
1091 77eb5504 Anton Khirnov
        avio_w8(pb, 0); /* c string */
1092 9a4d9388 Roman Shaposhnik
    return updateSize(pb, pos);
1093
}
1094
1095 ae628ec1 Anton Khirnov
static int mov_write_hmhd_tag(AVIOContext *pb)
1096 e977af6f Martin Storsjö
{
1097
    /* This atom must be present, but leaving the values at zero
1098
     * seems harmless. */
1099 77eb5504 Anton Khirnov
    avio_wb32(pb, 28); /* size */
1100 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "hmhd");
1101 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* version, flags */
1102
    avio_wb16(pb, 0); /* maxPDUsize */
1103
    avio_wb16(pb, 0); /* avgPDUsize */
1104
    avio_wb32(pb, 0); /* maxbitrate */
1105
    avio_wb32(pb, 0); /* avgbitrate */
1106
    avio_wb32(pb, 0); /* reserved */
1107 e977af6f Martin Storsjö
    return 28;
1108
}
1109
1110 ae628ec1 Anton Khirnov
static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
1111 9a4d9388 Roman Shaposhnik
{
1112 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1113 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1114 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "minf");
1115 72415b2a Stefano Sabatini
    if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
1116 9a4d9388 Roman Shaposhnik
        mov_write_vmhd_tag(pb);
1117 72415b2a Stefano Sabatini
    else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1118 9a4d9388 Roman Shaposhnik
        mov_write_smhd_tag(pb);
1119 72415b2a Stefano Sabatini
    else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
1120 9f520554 Baptiste Coudurier
        if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
1121 80357cfc Baptiste Coudurier
        else                                      mov_write_nmhd_tag(pb);
1122 e977af6f Martin Storsjö
    } else if (track->tag == MKTAG('r','t','p',' ')) {
1123
        mov_write_hmhd_tag(pb);
1124 f6204886 David Conrad
    }
1125 9a4d9388 Roman Shaposhnik
    if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
1126
        mov_write_hdlr_tag(pb, NULL);
1127
    mov_write_dinf_tag(pb);
1128
    mov_write_stbl_tag(pb, track);
1129 f578f938 Thomas Raivio
    return updateSize(pb, pos);
1130 1cb5f7fd Michael Niedermayer
}
1131
1132 ae628ec1 Anton Khirnov
static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
1133 1cb5f7fd Michael Niedermayer
{
1134 a3a80ddc Baptiste Coudurier
    int version = track->trackDuration < INT32_MAX ? 0 : 1;
1135
1136 77eb5504 Anton Khirnov
    (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
1137 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "mdhd");
1138 77eb5504 Anton Khirnov
    avio_w8(pb, version);
1139
    avio_wb24(pb, 0); /* flags */
1140 a3a80ddc Baptiste Coudurier
    if (version == 1) {
1141 77eb5504 Anton Khirnov
        avio_wb64(pb, track->time);
1142
        avio_wb64(pb, track->time);
1143 a3a80ddc Baptiste Coudurier
    } else {
1144 77eb5504 Anton Khirnov
        avio_wb32(pb, track->time); /* creation time */
1145
        avio_wb32(pb, track->time); /* modification time */
1146 a3a80ddc Baptiste Coudurier
    }
1147 77eb5504 Anton Khirnov
    avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
1148
    (version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration); /* duration */
1149
    avio_wb16(pb, track->language); /* language */
1150
    avio_wb16(pb, 0); /* reserved (quality) */
1151 db8f4a92 Michael Niedermayer
1152
    if(version!=0 && track->mode == MODE_MOV){
1153
        av_log(NULL, AV_LOG_ERROR,
1154
            "FATAL error, file duration too long for timebase, this file will not be\n"
1155 b1b64c23 Aurelien Jacobs
            "playable with quicktime. Choose a different timebase or a different\n"
1156 db8f4a92 Michael Niedermayer
            "container format\n");
1157
    }
1158
1159 1cb5f7fd Michael Niedermayer
    return 32;
1160
}
1161
1162 ae628ec1 Anton Khirnov
static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
1163 1cb5f7fd Michael Niedermayer
{
1164 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1165 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1166 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "mdia");
1167 6e6d6dc0 Michael Niedermayer
    mov_write_mdhd_tag(pb, track);
1168
    mov_write_hdlr_tag(pb, track);
1169
    mov_write_minf_tag(pb, track);
1170
    return updateSize(pb, pos);
1171 1cb5f7fd Michael Niedermayer
}
1172
1173 ae628ec1 Anton Khirnov
static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1174 1cb5f7fd Michael Niedermayer
{
1175 729ef3ba Baptiste Coudurier
    int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1176
                                      track->timescale, AV_ROUND_UP);
1177 a3a80ddc Baptiste Coudurier
    int version = duration < INT32_MAX ? 0 : 1;
1178
1179 77eb5504 Anton Khirnov
    (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
1180 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "tkhd");
1181 77eb5504 Anton Khirnov
    avio_w8(pb, version);
1182
    avio_wb24(pb, 0xf); /* flags (track enabled) */
1183 a3a80ddc Baptiste Coudurier
    if (version == 1) {
1184 77eb5504 Anton Khirnov
        avio_wb64(pb, track->time);
1185
        avio_wb64(pb, track->time);
1186 a3a80ddc Baptiste Coudurier
    } else {
1187 77eb5504 Anton Khirnov
        avio_wb32(pb, track->time); /* creation time */
1188
        avio_wb32(pb, track->time); /* modification time */
1189 a3a80ddc Baptiste Coudurier
    }
1190 77eb5504 Anton Khirnov
    avio_wb32(pb, track->trackID); /* track-id */
1191
    avio_wb32(pb, 0); /* reserved */
1192
    (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
1193 1cb5f7fd Michael Niedermayer
1194 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* reserved */
1195
    avio_wb32(pb, 0); /* reserved */
1196
    avio_wb32(pb, 0x0); /* reserved (Layer & Alternate group) */
1197 1cb5f7fd Michael Niedermayer
    /* Volume, only for audio */
1198 72415b2a Stefano Sabatini
    if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
1199 77eb5504 Anton Khirnov
        avio_wb16(pb, 0x0100);
1200 1cb5f7fd Michael Niedermayer
    else
1201 77eb5504 Anton Khirnov
        avio_wb16(pb, 0);
1202
    avio_wb16(pb, 0); /* reserved */
1203 1cb5f7fd Michael Niedermayer
1204
    /* Matrix structure */
1205 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x00010000); /* reserved */
1206
    avio_wb32(pb, 0x0); /* reserved */
1207
    avio_wb32(pb, 0x0); /* reserved */
1208
    avio_wb32(pb, 0x0); /* reserved */
1209
    avio_wb32(pb, 0x00010000); /* reserved */
1210
    avio_wb32(pb, 0x0); /* reserved */
1211
    avio_wb32(pb, 0x0); /* reserved */
1212
    avio_wb32(pb, 0x0); /* reserved */
1213
    avio_wb32(pb, 0x40000000); /* reserved */
1214 1cb5f7fd Michael Niedermayer
1215
    /* Track width and height, for visual only */
1216 ddb63017 David Conrad
    if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
1217
              track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
1218 ea1afa12 Maksym Veremeyenko
        if(track->mode == MODE_MOV) {
1219
            avio_wb32(pb, track->enc->width << 16);
1220 fffdee89 Baptiste Coudurier
            avio_wb32(pb, track->height << 16);
1221 ea1afa12 Maksym Veremeyenko
        } else {
1222 77d207cb Maksym Veremeyenko
            double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1223
            if(!sample_aspect_ratio || track->height != track->enc->height)
1224
                sample_aspect_ratio = 1;
1225
            avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
1226
            avio_wb32(pb, track->height*0x10000);
1227 ea1afa12 Maksym Veremeyenko
        }
1228 1cb5f7fd Michael Niedermayer
    }
1229
    else {
1230 77eb5504 Anton Khirnov
        avio_wb32(pb, 0);
1231
        avio_wb32(pb, 0);
1232 1cb5f7fd Michael Niedermayer
    }
1233
    return 0x5c;
1234
}
1235
1236 13ff92d1 Ronald S. Bultje
static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
1237 ea1afa12 Maksym Veremeyenko
{
1238
    int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
1239
                               track->enc->sample_aspect_ratio.den);
1240
1241
    int64_t pos = url_ftell(pb);
1242
1243
    avio_wb32(pb, 0); /* size */
1244 13ff92d1 Ronald S. Bultje
    ffio_wfourcc(pb, "tapt");
1245 ea1afa12 Maksym Veremeyenko
1246
    avio_wb32(pb, 20);
1247 13ff92d1 Ronald S. Bultje
    ffio_wfourcc(pb, "clef");
1248 ea1afa12 Maksym Veremeyenko
    avio_wb32(pb, 0);
1249
    avio_wb32(pb, width << 16);
1250
    avio_wb32(pb, track->enc->height << 16);
1251
1252
    avio_wb32(pb, 20);
1253 13ff92d1 Ronald S. Bultje
    ffio_wfourcc(pb, "enof");
1254 ea1afa12 Maksym Veremeyenko
    avio_wb32(pb, 0);
1255
    avio_wb32(pb, track->enc->width << 16);
1256
    avio_wb32(pb, track->enc->height << 16);
1257
1258
    return updateSize(pb, pos);
1259
};
1260
1261 8af18154 tjcannell@blueyonder.co.uk
// This box seems important for the psp playback ... without it the movie seems to hang
1262 ae628ec1 Anton Khirnov
static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
1263 8af18154 tjcannell@blueyonder.co.uk
{
1264 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x24); /* size  */
1265 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "edts");
1266 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x1c); /* size  */
1267 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "elst");
1268 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x0);
1269
    avio_wb32(pb, 0x1);
1270 8af18154 tjcannell@blueyonder.co.uk
1271 729ef3ba Baptiste Coudurier
    /* duration   ... doesn't seem to effect psp */
1272 77eb5504 Anton Khirnov
    avio_wb32(pb, av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
1273 729ef3ba Baptiste Coudurier
                                track->timescale, AV_ROUND_UP));
1274 8af18154 tjcannell@blueyonder.co.uk
1275 77eb5504 Anton Khirnov
    avio_wb32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
1276
    avio_wb32(pb, 0x00010000);
1277 8af18154 tjcannell@blueyonder.co.uk
    return 0x24;
1278
}
1279
1280 ae628ec1 Anton Khirnov
static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
1281 ddb63017 David Conrad
{
1282 77eb5504 Anton Khirnov
    avio_wb32(pb, 20);   // size
1283 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "tref");
1284 77eb5504 Anton Khirnov
    avio_wb32(pb, 12);   // size (subatom)
1285
    avio_wl32(pb, track->tref_tag);
1286
    avio_wb32(pb, track->tref_id);
1287 ddb63017 David Conrad
    return 20;
1288
}
1289
1290 8af18154 tjcannell@blueyonder.co.uk
// goes at the end of each track!  ... Critical for PSP playback ("Incompatible data" without it)
1291 ae628ec1 Anton Khirnov
static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
1292 8af18154 tjcannell@blueyonder.co.uk
{
1293 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
1294 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "uuid");
1295
    ffio_wfourcc(pb, "USMT");
1296 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x21d24fce);
1297
    avio_wb32(pb, 0xbb88695c);
1298
    avio_wb32(pb, 0xfac9c740);
1299
    avio_wb32(pb, 0x1c);     // another size here!
1300 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "MTDT");
1301 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x00010012);
1302
    avio_wb32(pb, 0x0a);
1303
    avio_wb32(pb, 0x55c40000);
1304
    avio_wb32(pb, 0x1);
1305
    avio_wb32(pb, 0x0);
1306 8af18154 tjcannell@blueyonder.co.uk
    return 0x34;
1307
}
1308
1309 ae628ec1 Anton Khirnov
static int mov_write_udta_sdp(AVIOContext *pb, AVCodecContext *ctx, int index)
1310 e977af6f Martin Storsjö
{
1311
    char buf[1000] = "";
1312
    int len;
1313
1314 7ad1dc54 Martin Storsjö
    ff_sdp_write_media(buf, sizeof(buf), ctx, NULL, NULL, 0, 0);
1315 e977af6f Martin Storsjö
    av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
1316
    len = strlen(buf);
1317
1318 77eb5504 Anton Khirnov
    avio_wb32(pb, len + 24);
1319 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "udta");
1320 77eb5504 Anton Khirnov
    avio_wb32(pb, len + 16);
1321 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "hnti");
1322 77eb5504 Anton Khirnov
    avio_wb32(pb, len + 8);
1323 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "sdp ");
1324 77eb5504 Anton Khirnov
    avio_write(pb, buf, len);
1325 e977af6f Martin Storsjö
    return len + 24;
1326
}
1327
1328 ae628ec1 Anton Khirnov
static int mov_write_trak_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
1329 1cb5f7fd Michael Niedermayer
{
1330 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1331 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1332 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "trak");
1333 59729451 Aurelien Jacobs
    mov_write_tkhd_tag(pb, track, st);
1334 e1316b19 Baptiste Coudurier
    if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS)
1335 8af18154 tjcannell@blueyonder.co.uk
        mov_write_edts_tag(pb, track);  // PSP Movies require edts box
1336 ddb63017 David Conrad
    if (track->tref_tag)
1337
        mov_write_tref_tag(pb, track);
1338 6e6d6dc0 Michael Niedermayer
    mov_write_mdia_tag(pb, track);
1339 115329f1 Diego Biurrun
    if (track->mode == MODE_PSP)
1340 8af18154 tjcannell@blueyonder.co.uk
        mov_write_uuid_tag_psp(pb,track);  // PSP Movies require this uuid box
1341 e977af6f Martin Storsjö
    if (track->tag == MKTAG('r','t','p',' '))
1342
        mov_write_udta_sdp(pb, track->rtp_ctx->streams[0]->codec, track->trackID);
1343 ea1afa12 Maksym Veremeyenko
    if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
1344
        double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
1345
        if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
1346
            mov_write_tapt_tag(pb, track);
1347
    };
1348 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
1349 1cb5f7fd Michael Niedermayer
}
1350
1351 88730be6 Måns Rullgård
#if 0
1352 1cb5f7fd Michael Niedermayer
/* TODO: Not sorted out, but not necessary either */
1353 ae628ec1 Anton Khirnov
static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
1354 1cb5f7fd Michael Niedermayer
{
1355 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x15); /* size */
1356 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "iods");
1357 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);    /* version & flags */
1358
    avio_wb16(pb, 0x1007);
1359
    avio_w8(pb, 0);
1360
    avio_wb16(pb, 0x4fff);
1361
    avio_wb16(pb, 0xfffe);
1362
    avio_wb16(pb, 0x01ff);
1363 1cb5f7fd Michael Niedermayer
    return 0x15;
1364
}
1365 88730be6 Måns Rullgård
#endif
1366 1cb5f7fd Michael Niedermayer
1367 ae628ec1 Anton Khirnov
static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
1368 1cb5f7fd Michael Niedermayer
{
1369 b29af723 Michael Niedermayer
    int maxTrackID = 1, i;
1370
    int64_t maxTrackLenTemp, maxTrackLen = 0;
1371 a3a80ddc Baptiste Coudurier
    int version;
1372 1cb5f7fd Michael Niedermayer
1373 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1374 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1375 729ef3ba Baptiste Coudurier
            maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration,
1376
                                             MOV_TIMESCALE,
1377
                                             mov->tracks[i].timescale,
1378
                                             AV_ROUND_UP);
1379 f578f938 Thomas Raivio
            if(maxTrackLen < maxTrackLenTemp)
1380
                maxTrackLen = maxTrackLenTemp;
1381 1cb5f7fd Michael Niedermayer
            if(maxTrackID < mov->tracks[i].trackID)
1382
                maxTrackID = mov->tracks[i].trackID;
1383
        }
1384
    }
1385 a3a80ddc Baptiste Coudurier
1386
    version = maxTrackLen < UINT32_MAX ? 0 : 1;
1387 77eb5504 Anton Khirnov
    (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
1388 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "mvhd");
1389 77eb5504 Anton Khirnov
    avio_w8(pb, version);
1390
    avio_wb24(pb, 0); /* flags */
1391 a3a80ddc Baptiste Coudurier
    if (version == 1) {
1392 77eb5504 Anton Khirnov
        avio_wb64(pb, mov->time);
1393
        avio_wb64(pb, mov->time);
1394 a3a80ddc Baptiste Coudurier
    } else {
1395 77eb5504 Anton Khirnov
        avio_wb32(pb, mov->time); /* creation time */
1396
        avio_wb32(pb, mov->time); /* modification time */
1397 a3a80ddc Baptiste Coudurier
    }
1398 77eb5504 Anton Khirnov
    avio_wb32(pb, MOV_TIMESCALE);
1399
    (version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen); /* duration of longest track */
1400 1cb5f7fd Michael Niedermayer
1401 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
1402
    avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
1403
    avio_wb16(pb, 0); /* reserved */
1404
    avio_wb32(pb, 0); /* reserved */
1405
    avio_wb32(pb, 0); /* reserved */
1406 1cb5f7fd Michael Niedermayer
1407
    /* Matrix structure */
1408 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x00010000); /* reserved */
1409
    avio_wb32(pb, 0x0); /* reserved */
1410
    avio_wb32(pb, 0x0); /* reserved */
1411
    avio_wb32(pb, 0x0); /* reserved */
1412
    avio_wb32(pb, 0x00010000); /* reserved */
1413
    avio_wb32(pb, 0x0); /* reserved */
1414
    avio_wb32(pb, 0x0); /* reserved */
1415
    avio_wb32(pb, 0x0); /* reserved */
1416
    avio_wb32(pb, 0x40000000); /* reserved */
1417
1418
    avio_wb32(pb, 0); /* reserved (preview time) */
1419
    avio_wb32(pb, 0); /* reserved (preview duration) */
1420
    avio_wb32(pb, 0); /* reserved (poster time) */
1421
    avio_wb32(pb, 0); /* reserved (selection time) */
1422
    avio_wb32(pb, 0); /* reserved (selection duration) */
1423
    avio_wb32(pb, 0); /* reserved (current time) */
1424
    avio_wb32(pb, maxTrackID+1); /* Next track id */
1425 1cb5f7fd Michael Niedermayer
    return 0x6c;
1426
}
1427
1428 ae628ec1 Anton Khirnov
static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
1429 b6c50eb1 Patrice Bensoussan
                                     AVFormatContext *s)
1430
{
1431 77eb5504 Anton Khirnov
    avio_wb32(pb, 33); /* size */
1432 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "hdlr");
1433 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
1434
    avio_wb32(pb, 0);
1435 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "mdir");
1436
    ffio_wfourcc(pb, "appl");
1437 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
1438
    avio_wb32(pb, 0);
1439
    avio_w8(pb, 0);
1440 6b174197 Tomas Härdin
    return 33;
1441 b6c50eb1 Patrice Bensoussan
}
1442
1443
/* helper function to write a data tag with the specified string as data */
1444 ae628ec1 Anton Khirnov
static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
1445 b6c50eb1 Patrice Bensoussan
{
1446 efda3395 Michael Niedermayer
    if(long_style){
1447 6b174197 Tomas Härdin
        int size = 16 + strlen(data);
1448 77eb5504 Anton Khirnov
        avio_wb32(pb, size); /* size */
1449 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "data");
1450 77eb5504 Anton Khirnov
        avio_wb32(pb, 1);
1451
        avio_wb32(pb, 0);
1452
        avio_write(pb, data, strlen(data));
1453 6b174197 Tomas Härdin
        return size;
1454 efda3395 Michael Niedermayer
    }else{
1455 fe3ab8ad Baptiste Coudurier
        if (!lang)
1456
            lang = ff_mov_iso639_to_lang("und", 1);
1457 77eb5504 Anton Khirnov
        avio_wb16(pb, strlen(data)); /* string length */
1458
        avio_wb16(pb, lang);
1459
        avio_write(pb, data, strlen(data));
1460 efda3395 Michael Niedermayer
        return strlen(data) + 4;
1461 b6c50eb1 Patrice Bensoussan
    }
1462
}
1463
1464 ae628ec1 Anton Khirnov
static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
1465 b6c50eb1 Patrice Bensoussan
    int size = 0;
1466 91208916 Baptiste Coudurier
    if (value && value[0]) {
1467 bc5c918e Diego Biurrun
        int64_t pos = url_ftell(pb);
1468 77eb5504 Anton Khirnov
        avio_wb32(pb, 0); /* size */
1469 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, name);
1470 d9fc9ff3 Aurelien Jacobs
        mov_write_string_data_tag(pb, value, lang, long_style);
1471 efda3395 Michael Niedermayer
        size= updateSize(pb, pos);
1472 b6c50eb1 Patrice Bensoussan
    }
1473
    return size;
1474
}
1475
1476 ae628ec1 Anton Khirnov
static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
1477 1ee2d448 Aurelien Jacobs
                                     const char *name, const char *tag,
1478
                                     int long_style)
1479 b6c50eb1 Patrice Bensoussan
{
1480 d9fc9ff3 Aurelien Jacobs
    int l, lang = 0, len, len2;
1481
    AVMetadataTag *t, *t2 = NULL;
1482
    char tag2[16];
1483 1ee2d448 Aurelien Jacobs
1484
    if (!(t = av_metadata_get(s->metadata, tag, NULL, 0)))
1485 efda3395 Michael Niedermayer
        return 0;
1486 1ee2d448 Aurelien Jacobs
1487 d9fc9ff3 Aurelien Jacobs
    len = strlen(t->key);
1488
    snprintf(tag2, sizeof(tag2), "%s-", tag);
1489
    while ((t2 = av_metadata_get(s->metadata, tag2, t2, AV_METADATA_IGNORE_SUFFIX))) {
1490
        len2 = strlen(t2->key);
1491
        if (len2 == len+4 && !strcmp(t->value, t2->value)
1492 cc255afe Baptiste Coudurier
            && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
1493 d9fc9ff3 Aurelien Jacobs
            lang = l;
1494
            break;
1495
        }
1496
    }
1497
    return mov_write_string_tag(pb, name, t->value, lang, long_style);
1498 b6c50eb1 Patrice Bensoussan
}
1499
1500
/* iTunes track number */
1501 ae628ec1 Anton Khirnov
static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
1502 b6c50eb1 Patrice Bensoussan
                              AVFormatContext *s)
1503
{
1504 1ee2d448 Aurelien Jacobs
    AVMetadataTag *t = av_metadata_get(s->metadata, "track", NULL, 0);
1505
    int size = 0, track = t ? atoi(t->value) : 0;
1506
    if (track) {
1507 77eb5504 Anton Khirnov
        avio_wb32(pb, 32); /* size */
1508 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "trkn");
1509 77eb5504 Anton Khirnov
            avio_wb32(pb, 24); /* size */
1510 0abdb293 Anton Khirnov
            ffio_wfourcc(pb, "data");
1511 77eb5504 Anton Khirnov
            avio_wb32(pb, 0);        // 8 bytes empty
1512
            avio_wb32(pb, 0);
1513
            avio_wb16(pb, 0);        // empty
1514
            avio_wb16(pb, track);    // track number
1515
            avio_wb16(pb, 0);        // total track number
1516
            avio_wb16(pb, 0);        // empty
1517 6b174197 Tomas Härdin
        size = 32;
1518 b6c50eb1 Patrice Bensoussan
    }
1519
    return size;
1520
}
1521
1522
/* iTunes meta data list */
1523 ae628ec1 Anton Khirnov
static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
1524 b6c50eb1 Patrice Bensoussan
                              AVFormatContext *s)
1525
{
1526 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1527 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1528 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "ilst");
1529 1ee2d448 Aurelien Jacobs
    mov_write_string_metadata(s, pb, "\251nam", "title"    , 1);
1530 ea4c4d7f Baptiste Coudurier
    mov_write_string_metadata(s, pb, "\251ART", "artist"   , 1);
1531 2cf9c7ef Mike Melanson
    mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
1532 620af1a1 David Conrad
    mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
1533 1ee2d448 Aurelien Jacobs
    mov_write_string_metadata(s, pb, "\251alb", "album"    , 1);
1534 ca76a119 Anton Khirnov
    mov_write_string_metadata(s, pb, "\251day", "date"     , 1);
1535 d9fc9ff3 Aurelien Jacobs
    mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
1536 1ee2d448 Aurelien Jacobs
    mov_write_string_metadata(s, pb, "\251cmt", "comment"  , 1);
1537
    mov_write_string_metadata(s, pb, "\251gen", "genre"    , 1);
1538
    mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
1539 2cf9c7ef Mike Melanson
    mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
1540
    mov_write_string_metadata(s, pb, "\251lyr", "lyrics"   , 1);
1541 7382902b David Conrad
    mov_write_string_metadata(s, pb, "desc",    "description",1);
1542
    mov_write_string_metadata(s, pb, "ldes",    "synopsis" , 1);
1543
    mov_write_string_metadata(s, pb, "tvsh",    "show"     , 1);
1544
    mov_write_string_metadata(s, pb, "tven",    "episode_id",1);
1545
    mov_write_string_metadata(s, pb, "tvnn",    "network"  , 1);
1546 b6c50eb1 Patrice Bensoussan
    mov_write_trkn_tag(pb, mov, s);
1547
    return updateSize(pb, pos);
1548
}
1549
1550
/* iTunes meta data tag */
1551 ae628ec1 Anton Khirnov
static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
1552 b6c50eb1 Patrice Bensoussan
                              AVFormatContext *s)
1553
{
1554
    int size = 0;
1555 b97fb809 Aurelien Jacobs
    int64_t pos = url_ftell(pb);
1556 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1557 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "meta");
1558 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);
1559 b97fb809 Aurelien Jacobs
    mov_write_itunes_hdlr_tag(pb, mov, s);
1560
    mov_write_ilst_tag(pb, mov, s);
1561
    size = updateSize(pb, pos);
1562 b6c50eb1 Patrice Bensoussan
    return size;
1563
}
1564 115329f1 Diego Biurrun
1565 07b7b06d Baptiste Coudurier
static int utf8len(const uint8_t *b)
1566
{
1567
    int len=0;
1568
    int val;
1569
    while(*b){
1570
        GET_UTF8(val, *b++, return -1;)
1571
        len++;
1572
    }
1573
    return len;
1574
}
1575
1576 ae628ec1 Anton Khirnov
static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
1577 07b7b06d Baptiste Coudurier
{
1578
    int val;
1579
    while(*b){
1580
        GET_UTF8(val, *b++, return -1;)
1581 77eb5504 Anton Khirnov
        avio_wb16(pb, val);
1582 07b7b06d Baptiste Coudurier
    }
1583 77eb5504 Anton Khirnov
    avio_wb16(pb, 0x00);
1584 07b7b06d Baptiste Coudurier
    return 0;
1585
}
1586
1587
static uint16_t language_code(const char *str)
1588
{
1589
    return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
1590
}
1591
1592 ae628ec1 Anton Khirnov
static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
1593 c6e2c6c9 Baptiste Coudurier
                                  const char *tag, const char *str)
1594
{
1595 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1596 1ee2d448 Aurelien Jacobs
    AVMetadataTag *t = av_metadata_get(s->metadata, str, NULL, 0);
1597
    if (!t || !utf8len(t->value))
1598 c6e2c6c9 Baptiste Coudurier
        return 0;
1599 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);   /* size */
1600 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, tag); /* type */
1601 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);   /* version + flags */
1602 c6e2c6c9 Baptiste Coudurier
    if (!strcmp(tag, "yrrc"))
1603 77eb5504 Anton Khirnov
        avio_wb16(pb, atoi(t->value));
1604 c6e2c6c9 Baptiste Coudurier
    else {
1605 77eb5504 Anton Khirnov
        avio_wb16(pb, language_code("eng")); /* language */
1606
        avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
1607 1ee2d448 Aurelien Jacobs
        if (!strcmp(tag, "albm") &&
1608 221ed486 Baptiste Coudurier
            (t = av_metadata_get(s->metadata, "track", NULL, 0)))
1609 77eb5504 Anton Khirnov
            avio_w8(pb, atoi(t->value));
1610 c6e2c6c9 Baptiste Coudurier
    }
1611
    return updateSize(pb, pos);
1612
}
1613
1614 ae628ec1 Anton Khirnov
static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
1615 dc75e4e3 David Conrad
{
1616
    int64_t pos = url_ftell(pb);
1617
    int i, nb_chapters = FFMIN(s->nb_chapters, 255);
1618
1619 77eb5504 Anton Khirnov
    avio_wb32(pb, 0);            // size
1620 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "chpl");
1621 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x01000000);   // version + flags
1622
    avio_wb32(pb, 0);            // unknown
1623
    avio_w8(pb, nb_chapters);
1624 dc75e4e3 David Conrad
1625
    for (i = 0; i < nb_chapters; i++) {
1626
        AVChapter *c = s->chapters[i];
1627
        AVMetadataTag *t;
1628 77eb5504 Anton Khirnov
        avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
1629 dc75e4e3 David Conrad
1630
        if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {
1631
            int len = FFMIN(strlen(t->value), 255);
1632 77eb5504 Anton Khirnov
            avio_w8(pb, len);
1633
            avio_write(pb, t->value, len);
1634 dc75e4e3 David Conrad
        } else
1635 77eb5504 Anton Khirnov
            avio_w8(pb, 0);
1636 dc75e4e3 David Conrad
    }
1637
    return updateSize(pb, pos);
1638
}
1639
1640 ae628ec1 Anton Khirnov
static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
1641 69dde1ad Gildas Bazin
                              AVFormatContext *s)
1642
{
1643 ae628ec1 Anton Khirnov
    AVIOContext *pb_buf;
1644 1ee2d448 Aurelien Jacobs
    int i, ret, size;
1645
    uint8_t *buf;
1646 89a0d790 Benoit Fouet
1647 d1d87dfb Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++)
1648
        if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
1649 1ee2d448 Aurelien Jacobs
            return 0;
1650 d1d87dfb Baptiste Coudurier
        }
1651
1652 1ee2d448 Aurelien Jacobs
    ret = url_open_dyn_buf(&pb_buf);
1653
    if(ret < 0)
1654
        return ret;
1655 69dde1ad Gildas Bazin
1656 a04aec6d Michael Niedermayer
        if (mov->mode & MODE_3GP) {
1657 a4de2b6e Baptiste Coudurier
            mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
1658 1ee2d448 Aurelien Jacobs
            mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
1659
            mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
1660
            mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
1661
            mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
1662
            mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
1663
            mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
1664 ca76a119 Anton Khirnov
            mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
1665 ce072b2d Baptiste Coudurier
        } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
1666 96d1e75a Baptiste Coudurier
            mov_write_string_metadata(s, pb_buf, "\251ART", "artist"     , 0);
1667 1ee2d448 Aurelien Jacobs
            mov_write_string_metadata(s, pb_buf, "\251nam", "title"      , 0);
1668
            mov_write_string_metadata(s, pb_buf, "\251aut", "author"     , 0);
1669
            mov_write_string_metadata(s, pb_buf, "\251alb", "album"      , 0);
1670 ca76a119 Anton Khirnov
            mov_write_string_metadata(s, pb_buf, "\251day", "date"       , 0);
1671 edd33cb6 Baptiste Coudurier
            mov_write_string_metadata(s, pb_buf, "\251swr", "encoder"    , 0);
1672 1ee2d448 Aurelien Jacobs
            mov_write_string_metadata(s, pb_buf, "\251des", "comment"    , 0);
1673
            mov_write_string_metadata(s, pb_buf, "\251gen", "genre"      , 0);
1674
            mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright"  , 0);
1675 c6e2c6c9 Baptiste Coudurier
        } else {
1676 283c9a8e Baptiste Coudurier
            /* iTunes meta data */
1677 1ee2d448 Aurelien Jacobs
            mov_write_meta_tag(pb_buf, mov, s);
1678 c6e2c6c9 Baptiste Coudurier
        }
1679 1ee2d448 Aurelien Jacobs
1680 dc75e4e3 David Conrad
        if (s->nb_chapters)
1681
            mov_write_chpl_tag(pb_buf, s);
1682
1683 1ee2d448 Aurelien Jacobs
    if ((size = url_close_dyn_buf(pb_buf, &buf)) > 0) {
1684 77eb5504 Anton Khirnov
        avio_wb32(pb, size+8);
1685 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "udta");
1686 77eb5504 Anton Khirnov
        avio_write(pb, buf, size);
1687 89a0d790 Benoit Fouet
    }
1688 8b6e0aec Martin Storsjö
    av_free(buf);
1689 89a0d790 Benoit Fouet
1690
    return 0;
1691 69dde1ad Gildas Bazin
}
1692
1693 ae628ec1 Anton Khirnov
static void mov_write_psp_udta_tag(AVIOContext *pb,
1694 20e22af8 Baptiste Coudurier
                                  const char *str, const char *lang, int type)
1695
{
1696
    int len = utf8len(str)+1;
1697
    if(len<=0)
1698 d32d6def Baptiste Coudurier
        return;
1699 77eb5504 Anton Khirnov
    avio_wb16(pb, len*2+10);            /* size */
1700
    avio_wb32(pb, type);                /* type */
1701
    avio_wb16(pb, language_code(lang)); /* language */
1702
    avio_wb16(pb, 0x01);                /* ? */
1703 20e22af8 Baptiste Coudurier
    ascii_to_wc(pb, str);
1704
}
1705
1706 ae628ec1 Anton Khirnov
static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
1707 dcfdb046 Benjamin Larsson
{
1708 1ee2d448 Aurelien Jacobs
    AVMetadataTag *title = av_metadata_get(s->metadata, "title", NULL, 0);
1709 bc5c918e Diego Biurrun
    int64_t pos, pos2;
1710 dcfdb046 Benjamin Larsson
1711 1ee2d448 Aurelien Jacobs
    if (title) {
1712 dcfdb046 Benjamin Larsson
        pos = url_ftell(pb);
1713 77eb5504 Anton Khirnov
        avio_wb32(pb, 0); /* size placeholder*/
1714 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "uuid");
1715
        ffio_wfourcc(pb, "USMT");
1716 77eb5504 Anton Khirnov
        avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1717
        avio_wb32(pb, 0xbb88695c);
1718
        avio_wb32(pb, 0xfac9c740);
1719 dcfdb046 Benjamin Larsson
1720 d32d6def Baptiste Coudurier
        pos2 = url_ftell(pb);
1721 77eb5504 Anton Khirnov
        avio_wb32(pb, 0); /* size placeholder*/
1722 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "MTDT");
1723 77eb5504 Anton Khirnov
        avio_wb16(pb, 4);
1724 dcfdb046 Benjamin Larsson
1725 7cf0e16f Michael Niedermayer
        // ?
1726 77eb5504 Anton Khirnov
        avio_wb16(pb, 0x0C);                 /* size */
1727
        avio_wb32(pb, 0x0B);                 /* type */
1728
        avio_wb16(pb, language_code("und")); /* language */
1729
        avio_wb16(pb, 0x0);                  /* ? */
1730
        avio_wb16(pb, 0x021C);               /* data */
1731 7cf0e16f Michael Niedermayer
1732 d32d6def Baptiste Coudurier
        mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT,      "eng", 0x04);
1733 1ee2d448 Aurelien Jacobs
        mov_write_psp_udta_tag(pb, title->value,          "eng", 0x01);
1734 7cf0e16f Michael Niedermayer
//        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);
1735 d32d6def Baptiste Coudurier
        mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
1736
1737
        updateSize(pb, pos2);
1738
        return updateSize(pb, pos);
1739 dcfdb046 Benjamin Larsson
    }
1740
1741 d32d6def Baptiste Coudurier
    return 0;
1742 dcfdb046 Benjamin Larsson
}
1743
1744 ae628ec1 Anton Khirnov
static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
1745 69dde1ad Gildas Bazin
                              AVFormatContext *s)
1746 1cb5f7fd Michael Niedermayer
{
1747 b29af723 Michael Niedermayer
    int i;
1748 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1749 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size placeholder*/
1750 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "moov");
1751 1cb5f7fd Michael Niedermayer
1752 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1753 e45ccf79 Gildas Bazin
        if(mov->tracks[i].entry <= 0) continue;
1754
1755
        mov->tracks[i].time = mov->time;
1756
        mov->tracks[i].trackID = i+1;
1757 1cb5f7fd Michael Niedermayer
    }
1758
1759 ddb63017 David Conrad
    if (mov->chapter_track)
1760
        for (i=0; i<s->nb_streams; i++) {
1761
            mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
1762
            mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID;
1763
        }
1764 e977af6f Martin Storsjö
    for (i = 0; i < mov->nb_streams; i++) {
1765
        if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
1766
            mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
1767
            mov->tracks[i].tref_id =
1768
                mov->tracks[mov->tracks[i].src_track].trackID;
1769
        }
1770
    }
1771 ddb63017 David Conrad
1772 6e6d6dc0 Michael Niedermayer
    mov_write_mvhd_tag(pb, mov);
1773
    //mov_write_iods_tag(pb, mov);
1774 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
1775 1cb5f7fd Michael Niedermayer
        if(mov->tracks[i].entry > 0) {
1776 ddb63017 David Conrad
            mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
1777 1cb5f7fd Michael Niedermayer
        }
1778
    }
1779
1780 dcfdb046 Benjamin Larsson
    if (mov->mode == MODE_PSP)
1781
        mov_write_uuidusmt_tag(pb, s);
1782 c6e2c6c9 Baptiste Coudurier
    else
1783 e344c1ea Steve L'Homme
        mov_write_udta_tag(pb, mov, s);
1784 69dde1ad Gildas Bazin
1785 6e6d6dc0 Michael Niedermayer
    return updateSize(pb, pos);
1786 1cb5f7fd Michael Niedermayer
}
1787
1788 ae628ec1 Anton Khirnov
static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
1789 1cb5f7fd Michael Niedermayer
{
1790 77eb5504 Anton Khirnov
    avio_wb32(pb, 8);    // placeholder for extended size field (64 bit)
1791 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
1792 b29af723 Michael Niedermayer
1793 115329f1 Diego Biurrun
    mov->mdat_pos = url_ftell(pb);
1794 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size placeholder*/
1795 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "mdat");
1796 1cb5f7fd Michael Niedermayer
    return 0;
1797
}
1798
1799
/* TODO: This needs to be more general */
1800 ae628ec1 Anton Khirnov
static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
1801 1cb5f7fd Michael Niedermayer
{
1802 2d243fb3 Ronald S. Bultje
    MOVMuxContext *mov = s->priv_data;
1803 bc5c918e Diego Biurrun
    int64_t pos = url_ftell(pb);
1804 cd70d17f Baptiste Coudurier
    int has_h264 = 0, has_video = 0;
1805 40f8675c Baptiste Coudurier
    int minor = 0x200;
1806 c55cc074 Baptiste Coudurier
    int i;
1807 69dde1ad Gildas Bazin
1808 83579084 Baptiste Coudurier
    for (i = 0; i < s->nb_streams; i++) {
1809
        AVStream *st = s->streams[i];
1810 72415b2a Stefano Sabatini
        if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
1811 cd70d17f Baptiste Coudurier
            has_video = 1;
1812 83579084 Baptiste Coudurier
        if (st->codec->codec_id == CODEC_ID_H264)
1813
            has_h264 = 1;
1814
    }
1815
1816 77eb5504 Anton Khirnov
    avio_wb32(pb, 0); /* size */
1817 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "ftyp");
1818 e45ccf79 Gildas Bazin
1819 40f8675c Baptiste Coudurier
    if (mov->mode == MODE_3GP) {
1820 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, has_h264 ? "3gp6"  : "3gp4");
1821 40f8675c Baptiste Coudurier
        minor =     has_h264 ?   0x100 :   0x200;
1822
    } else if (mov->mode & MODE_3G2) {
1823 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, has_h264 ? "3g2b"  : "3g2a");
1824 40f8675c Baptiste Coudurier
        minor =     has_h264 ? 0x20000 : 0x10000;
1825
    }else if (mov->mode == MODE_PSP)
1826 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "MSNV");
1827 c55cc074 Baptiste Coudurier
    else if (mov->mode == MODE_MP4)
1828 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "isom");
1829 cd70d17f Baptiste Coudurier
    else if (mov->mode == MODE_IPOD)
1830 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
1831 cd70d17f Baptiste Coudurier
    else
1832 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "qt  ");
1833 e45ccf79 Gildas Bazin
1834 77eb5504 Anton Khirnov
    avio_wb32(pb, minor);
1835 e45ccf79 Gildas Bazin
1836 6ba9ed95 Baptiste Coudurier
    if(mov->mode == MODE_MOV)
1837 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "qt  ");
1838 6ba9ed95 Baptiste Coudurier
    else{
1839 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "isom");
1840
        ffio_wfourcc(pb, "iso2");
1841 83579084 Baptiste Coudurier
        if(has_h264)
1842 0abdb293 Anton Khirnov
            ffio_wfourcc(pb, "avc1");
1843 6ba9ed95 Baptiste Coudurier
    }
1844 ed6e47c2 Michael Niedermayer
1845 91208916 Baptiste Coudurier
    if (mov->mode == MODE_3GP)
1846 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
1847 a04aec6d Michael Niedermayer
    else if (mov->mode & MODE_3G2)
1848 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
1849 91208916 Baptiste Coudurier
    else if (mov->mode == MODE_PSP)
1850 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "MSNV");
1851 c55cc074 Baptiste Coudurier
    else if (mov->mode == MODE_MP4)
1852 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "mp41");
1853 1560b667 Michael Niedermayer
    return updateSize(pb, pos);
1854 1cb5f7fd Michael Niedermayer
}
1855
1856 ae628ec1 Anton Khirnov
static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
1857 8af18154 tjcannell@blueyonder.co.uk
{
1858 dcfdb046 Benjamin Larsson
    AVCodecContext *VideoCodec = s->streams[0]->codec;
1859
    AVCodecContext *AudioCodec = s->streams[1]->codec;
1860
    int AudioRate = AudioCodec->sample_rate;
1861
    int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
1862 9e282ba3 Michael Niedermayer
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1863
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1864 8af18154 tjcannell@blueyonder.co.uk
1865 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x94); /* size */
1866 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "uuid");
1867
    ffio_wfourcc(pb, "PROF");
1868 8af18154 tjcannell@blueyonder.co.uk
1869 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
1870
    avio_wb32(pb, 0xbb88695c);
1871
    avio_wb32(pb, 0xfac9c740);
1872 8af18154 tjcannell@blueyonder.co.uk
1873 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x0);  /* ? */
1874
    avio_wb32(pb, 0x3);  /* 3 sections ? */
1875 8af18154 tjcannell@blueyonder.co.uk
1876 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x14); /* size */
1877 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "FPRF");
1878 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x0);  /* ? */
1879
    avio_wb32(pb, 0x0);  /* ? */
1880
    avio_wb32(pb, 0x0);  /* ? */
1881 8af18154 tjcannell@blueyonder.co.uk
1882 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x2c);  /* size */
1883 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "APRF");/* audio */
1884 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x0);
1885
    avio_wb32(pb, 0x2);   /* TrackID */
1886 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "mp4a");
1887 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x20f);
1888
    avio_wb32(pb, 0x0);
1889
    avio_wb32(pb, audio_kbitrate);
1890
    avio_wb32(pb, audio_kbitrate);
1891
    avio_wb32(pb, AudioRate);
1892
    avio_wb32(pb, AudioCodec->channels);
1893
1894
    avio_wb32(pb, 0x34);  /* size */
1895 0abdb293 Anton Khirnov
    ffio_wfourcc(pb, "VPRF");   /* video */
1896 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x0);
1897
    avio_wb32(pb, 0x1);    /* TrackID */
1898 87b041e0 Michael Niedermayer
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1899 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "avc1");
1900 77eb5504 Anton Khirnov
        avio_wb16(pb, 0x014D);
1901
        avio_wb16(pb, 0x0015);
1902 87b041e0 Michael Niedermayer
    } else {
1903 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "mp4v");
1904 77eb5504 Anton Khirnov
        avio_wb16(pb, 0x0000);
1905
        avio_wb16(pb, 0x0103);
1906 87b041e0 Michael Niedermayer
    }
1907 77eb5504 Anton Khirnov
    avio_wb32(pb, 0x0);
1908
    avio_wb32(pb, video_kbitrate);
1909
    avio_wb32(pb, video_kbitrate);
1910
    avio_wb32(pb, FrameRate);
1911
    avio_wb32(pb, FrameRate);
1912
    avio_wb16(pb, VideoCodec->width);
1913
    avio_wb16(pb, VideoCodec->height);
1914
    avio_wb32(pb, 0x010001); /* ? */
1915 8af18154 tjcannell@blueyonder.co.uk
}
1916
1917 e1316b19 Baptiste Coudurier
static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
1918
{
1919
    uint32_t c = -1;
1920
    int i, closed_gop = 0;
1921
1922
    for (i = 0; i < pkt->size - 4; i++) {
1923
        c = (c<<8) + pkt->data[i];
1924
        if (c == 0x1b8) { // gop
1925
            closed_gop = pkt->data[i+4]>>6 & 0x01;
1926
        } else if (c == 0x100) { // pic
1927
            int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
1928
            if (!temp_ref || closed_gop) // I picture is not reordered
1929
                *flags = MOV_SYNC_SAMPLE;
1930
            else
1931
                *flags = MOV_PARTIAL_SYNC_SAMPLE;
1932
            break;
1933
        }
1934
    }
1935
    return 0;
1936
}
1937
1938 27a826c9 Martin Storsjö
int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1939 1cb5f7fd Michael Niedermayer
{
1940 2d243fb3 Ronald S. Bultje
    MOVMuxContext *mov = s->priv_data;
1941 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
1942 039627cf Baptiste Coudurier
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1943
    AVCodecContext *enc = trk->enc;
1944 e45ccf79 Gildas Bazin
    unsigned int samplesInChunk = 0;
1945 e928649b Michael Niedermayer
    int size= pkt->size;
1946 1cb5f7fd Michael Niedermayer
1947 899681cd Björn Axelsson
    if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1948 e45ccf79 Gildas Bazin
    if (!size) return 0; /* Discard 0 sized packets */
1949 1cb5f7fd Michael Niedermayer
1950 06fcf56c Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1951 5cb49ca1 Baptiste Coudurier
        /* We must find out how many AMR blocks there are in one packet */
1952
        static uint16_t packed_size[16] =
1953
            {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
1954
        int len = 0;
1955
1956
        while (len < size && samplesInChunk < 100) {
1957
            len += packed_size[(pkt->data[len] >> 3) & 0x0F];
1958
            samplesInChunk++;
1959 1cb5f7fd Michael Niedermayer
        }
1960 2111e3f9 Michael Niedermayer
        if(samplesInChunk > 1){
1961 89938d29 Ramiro Polla
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1962 2111e3f9 Michael Niedermayer
            return -1;
1963
        }
1964 f2589642 Baptiste Coudurier
    } else if (enc->codec_id == CODEC_ID_ADPCM_MS ||
1965
               enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
1966
        samplesInChunk = enc->frame_size;
1967 5cb49ca1 Baptiste Coudurier
    } else if (trk->sampleSize)
1968
        samplesInChunk = size/trk->sampleSize;
1969
    else
1970 5616f85d Baptiste Coudurier
        samplesInChunk = 1;
1971 e45ccf79 Gildas Bazin
1972 5616f85d Baptiste Coudurier
    /* copy extradata if it exists */
1973
    if (trk->vosLen == 0 && enc->extradata_size > 0) {
1974 e45ccf79 Gildas Bazin
        trk->vosLen = enc->extradata_size;
1975
        trk->vosData = av_malloc(trk->vosLen);
1976
        memcpy(trk->vosData, enc->extradata, trk->vosLen);
1977
    }
1978
1979 5d2160a0 Baptiste Coudurier
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1980
        /* from x264 or from bytestream h264 */
1981
        /* nal reformating needed */
1982
        size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
1983
    } else {
1984 77eb5504 Anton Khirnov
        avio_write(pb, pkt->data, size);
1985 5d2160a0 Baptiste Coudurier
    }
1986
1987 0a63a676 Luca Abeni
    if ((enc->codec_id == CODEC_ID_DNXHD ||
1988 1fbbedff Baptiste Coudurier
         enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
1989 db568c07 Baptiste Coudurier
        /* copy frame to create needed atoms */
1990
        trk->vosLen = size;
1991
        trk->vosData = av_malloc(size);
1992 e8c4df40 Baptiste Coudurier
        if (!trk->vosData)
1993
            return AVERROR(ENOMEM);
1994 db568c07 Baptiste Coudurier
        memcpy(trk->vosData, pkt->data, size);
1995 c1b8e6d8 Baptiste Coudurier
    }
1996
1997 0aec3c5c Baptiste Coudurier
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1998
        trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
1999 e45ccf79 Gildas Bazin
        if (!trk->cluster)
2000
            return -1;
2001
    }
2002
2003 5d2160a0 Baptiste Coudurier
    trk->cluster[trk->entry].pos = url_ftell(pb) - size;
2004 0aec3c5c Baptiste Coudurier
    trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
2005
    trk->cluster[trk->entry].size = size;
2006
    trk->cluster[trk->entry].entries = samplesInChunk;
2007 aa90239f Baptiste Coudurier
    trk->cluster[trk->entry].dts = pkt->dts;
2008
    trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
2009
2010 96f69e0d Baptiste Coudurier
    if (pkt->pts == AV_NOPTS_VALUE) {
2011
        av_log(s, AV_LOG_WARNING, "pts has no value\n");
2012
        pkt->pts = pkt->dts;
2013
    }
2014 c4f078ff Baptiste Coudurier
    if (pkt->dts != pkt->pts)
2015 e1316b19 Baptiste Coudurier
        trk->flags |= MOV_TRACK_CTTS;
2016 c4f078ff Baptiste Coudurier
    trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
2017 e1316b19 Baptiste Coudurier
    trk->cluster[trk->entry].flags = 0;
2018 cc947f04 Jean-Daniel Dupas
    if (pkt->flags & AV_PKT_FLAG_KEY) {
2019 44b11207 Takashi Mochizuki
        if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
2020
            trk->entry > 0) { // force sync sample for the first key frame
2021 e1316b19 Baptiste Coudurier
            mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
2022
            if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
2023
                trk->flags |= MOV_TRACK_STPS;
2024
        } else {
2025
            trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
2026
        }
2027
        if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
2028
            trk->hasKeyframes++;
2029 ab4752e3 Baptiste Coudurier
    }
2030 e45ccf79 Gildas Bazin
    trk->entry++;
2031
    trk->sampleCount += samplesInChunk;
2032 039627cf Baptiste Coudurier
    mov->mdat_size += size;
2033 e45ccf79 Gildas Bazin
2034 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
2035 e977af6f Martin Storsjö
2036
    if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
2037
        ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry);
2038 1cb5f7fd Michael Niedermayer
    return 0;
2039
}
2040
2041 ddb63017 David Conrad
// QuickTime chapters involve an additional text track with the chapter names
2042
// as samples, and a tref pointing from the other tracks to the chapter one.
2043
static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
2044
{
2045
    MOVMuxContext *mov = s->priv_data;
2046
    MOVTrack *track = &mov->tracks[tracknum];
2047
    AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
2048
    int i, len;
2049
2050
    track->mode = mov->mode;
2051
    track->tag = MKTAG('t','e','x','t');
2052
    track->timescale = MOV_TIMESCALE;
2053
    track->enc = avcodec_alloc_context();
2054
    track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
2055
2056
    for (i = 0; i < s->nb_chapters; i++) {
2057
        AVChapter *c = s->chapters[i];
2058
        AVMetadataTag *t;
2059
2060
        int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
2061
        pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
2062
        pkt.duration = end - pkt.dts;
2063
2064
        if ((t = av_metadata_get(c->metadata, "title", NULL, 0))) {
2065
            len = strlen(t->value);
2066
            pkt.size = len+2;
2067
            pkt.data = av_malloc(pkt.size);
2068
            AV_WB16(pkt.data, len);
2069
            memcpy(pkt.data+2, t->value, len);
2070 27a826c9 Martin Storsjö
            ff_mov_write_packet(s, &pkt);
2071 ddb63017 David Conrad
            av_freep(&pkt.data);
2072
        }
2073
    }
2074
}
2075
2076 1b206f62 David Conrad
static int mov_write_header(AVFormatContext *s)
2077
{
2078 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
2079 1b206f62 David Conrad
    MOVMuxContext *mov = s->priv_data;
2080 e977af6f Martin Storsjö
    int i, hint_track = 0;
2081 1b206f62 David Conrad
2082
    if (url_is_streamed(s->pb)) {
2083
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
2084
        return -1;
2085
    }
2086
2087
    /* Default mode == MP4 */
2088
    mov->mode = MODE_MP4;
2089
2090
    if (s->oformat != NULL) {
2091
        if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
2092
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
2093
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
2094
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
2095
        else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
2096
2097
        mov_write_ftyp_tag(pb,s);
2098
        if (mov->mode == MODE_PSP) {
2099
            if (s->nb_streams != 2) {
2100
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
2101
                return -1;
2102
            }
2103
            mov_write_uuidprof_tag(pb,s);
2104
        }
2105
    }
2106
2107 ddb63017 David Conrad
    mov->nb_streams = s->nb_streams;
2108
    if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
2109
        mov->chapter_track = mov->nb_streams++;
2110
2111 e977af6f Martin Storsjö
    if (s->flags & AVFMT_FLAG_RTP_HINT) {
2112
        /* Add hint tracks for each audio and video stream */
2113
        hint_track = mov->nb_streams;
2114
        for (i = 0; i < s->nb_streams; i++) {
2115
            AVStream *st = s->streams[i];
2116
            if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2117
                st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2118
                mov->nb_streams++;
2119
            }
2120
        }
2121
    }
2122
2123 ddb63017 David Conrad
    mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
2124 1b206f62 David Conrad
    if (!mov->tracks)
2125
        return AVERROR(ENOMEM);
2126
2127
    for(i=0; i<s->nb_streams; i++){
2128
        AVStream *st= s->streams[i];
2129
        MOVTrack *track= &mov->tracks[i];
2130
        AVMetadataTag *lang = av_metadata_get(st->metadata, "language", NULL,0);
2131
2132
        track->enc = st->codec;
2133
        track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
2134
        if (track->language < 0)
2135
            track->language = 0;
2136
        track->mode = mov->mode;
2137
        track->tag = mov_find_codec_tag(s, track);
2138
        if (!track->tag) {
2139
            av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
2140
                   "codec not currently supported in container\n", i);
2141
            goto error;
2142
        }
2143 e977af6f Martin Storsjö
        /* If hinting of this track is enabled by a later hint track,
2144
         * this is updated. */
2145
        track->hint_track = -1;
2146 1b206f62 David Conrad
        if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2147
            if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
2148
                track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
2149
                track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
2150
                if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
2151
                    av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
2152
                    goto error;
2153
                }
2154
                track->height = track->tag>>24 == 'n' ? 486 : 576;
2155
            }
2156
            track->timescale = st->codec->time_base.den;
2157
            if (track->mode == MODE_MOV && track->timescale > 100000)
2158
                av_log(s, AV_LOG_WARNING,
2159
                       "WARNING codec timebase is very high. If duration is too long,\n"
2160
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
2161
                       "or choose different container.\n");
2162
        }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
2163
            track->timescale = st->codec->sample_rate;
2164
            if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
2165
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
2166
                goto error;
2167 f2589642 Baptiste Coudurier
            }else if(st->codec->codec_id == CODEC_ID_ADPCM_MS ||
2168
                     st->codec->codec_id == CODEC_ID_ADPCM_IMA_WAV){
2169
                if (!st->codec->block_align) {
2170
                    av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
2171
                    goto error;
2172
                }
2173
                track->sampleSize = st->codec->block_align;
2174 1b206f62 David Conrad
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
2175
                track->audio_vbr = 1;
2176
            }else{
2177
                st->codec->frame_size = 1;
2178
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
2179
            }
2180
            if (track->mode != MODE_MOV) {
2181
                if (track->timescale > UINT16_MAX) {
2182
                    av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
2183
                           "sample rate %dhz\n", i, track->timescale);
2184
                    goto error;
2185
                }
2186
                if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
2187
                    av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
2188
                           i, track->enc->sample_rate);
2189
                    goto error;
2190
                }
2191
            }
2192
        }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
2193
            track->timescale = st->codec->time_base.den;
2194
        }
2195
        if (!track->height)
2196
            track->height = st->codec->height;
2197
2198
        av_set_pts_info(st, 64, 1, track->timescale);
2199
    }
2200
2201
    mov_write_mdat_tag(pb, mov);
2202
    mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
2203 ddb63017 David Conrad
2204
    if (mov->chapter_track)
2205
        mov_create_chapter_track(s, mov->chapter_track);
2206 1b206f62 David Conrad
2207 e977af6f Martin Storsjö
    if (s->flags & AVFMT_FLAG_RTP_HINT) {
2208
        /* Initialize the hint tracks for each audio and video stream */
2209
        for (i = 0; i < s->nb_streams; i++) {
2210
            AVStream *st = s->streams[i];
2211
            if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2212
                st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2213
                ff_mov_init_hinting(s, hint_track, i);
2214
                hint_track++;
2215
            }
2216
        }
2217
    }
2218
2219 1b206f62 David Conrad
    put_flush_packet(pb);
2220
2221
    return 0;
2222
 error:
2223
    av_freep(&mov->tracks);
2224
    return -1;
2225
}
2226
2227 1cb5f7fd Michael Niedermayer
static int mov_write_trailer(AVFormatContext *s)
2228
{
2229 2d243fb3 Ronald S. Bultje
    MOVMuxContext *mov = s->priv_data;
2230 ae628ec1 Anton Khirnov
    AVIOContext *pb = s->pb;
2231 1cb5f7fd Michael Niedermayer
    int res = 0;
2232 0aec3c5c Baptiste Coudurier
    int i;
2233 1cb5f7fd Michael Niedermayer
2234 bc5c918e Diego Biurrun
    int64_t moov_pos = url_ftell(pb);
2235 1cb5f7fd Michael Niedermayer
2236
    /* Write size of mdat tag */
2237 039627cf Baptiste Coudurier
    if (mov->mdat_size+8 <= UINT32_MAX) {
2238 6b4aa5da Anton Khirnov
        avio_seek(pb, mov->mdat_pos, SEEK_SET);
2239 77eb5504 Anton Khirnov
        avio_wb32(pb, mov->mdat_size+8);
2240 b29af723 Michael Niedermayer
    } else {
2241
        /* overwrite 'wide' placeholder atom */
2242 6b4aa5da Anton Khirnov
        avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
2243 77eb5504 Anton Khirnov
        avio_wb32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
2244 0abdb293 Anton Khirnov
        ffio_wfourcc(pb, "mdat");
2245 77eb5504 Anton Khirnov
        avio_wb64(pb, mov->mdat_size+16);
2246 b29af723 Michael Niedermayer
    }
2247 6b4aa5da Anton Khirnov
    avio_seek(pb, moov_pos, SEEK_SET);
2248 1cb5f7fd Michael Niedermayer
2249 69dde1ad Gildas Bazin
    mov_write_moov_tag(pb, mov, s);
2250 1cb5f7fd Michael Niedermayer
2251 ddb63017 David Conrad
    if (mov->chapter_track)
2252
        av_freep(&mov->tracks[mov->chapter_track].enc);
2253
2254 42fb4148 Baptiste Coudurier
    for (i=0; i<mov->nb_streams; i++) {
2255 e977af6f Martin Storsjö
        if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
2256
            ff_mov_close_hinting(&mov->tracks[i]);
2257 0aec3c5c Baptiste Coudurier
        av_freep(&mov->tracks[i].cluster);
2258
2259 91208916 Baptiste Coudurier
        if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
2260 ec7d0d2e Gildas Bazin
2261 1cb5f7fd Michael Niedermayer
    }
2262 69dde1ad Gildas Bazin
2263 1cb5f7fd Michael Niedermayer
    put_flush_packet(pb);
2264
2265 5c5776e1 Baptiste Coudurier
    av_freep(&mov->tracks);
2266
2267 1cb5f7fd Michael Niedermayer
    return res;
2268
}
2269
2270 b250f9c6 Aurelien Jacobs
#if CONFIG_MOV_MUXER
2271 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_mov_muxer = {
2272 1cb5f7fd Michael Niedermayer
    "mov",
2273 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MOV format"),
2274 1cb5f7fd Michael Niedermayer
    NULL,
2275
    "mov",
2276 2d243fb3 Ronald S. Bultje
    sizeof(MOVMuxContext),
2277 69dde1ad Gildas Bazin
    CODEC_ID_AAC,
2278 2187d948 Michael Niedermayer
    CODEC_ID_MPEG4,
2279 1cb5f7fd Michael Niedermayer
    mov_write_header,
2280 27a826c9 Martin Storsjö
    ff_mov_write_packet,
2281 1cb5f7fd Michael Niedermayer
    mov_write_trailer,
2282 91360ce6 Baptiste Coudurier
    .flags = AVFMT_GLOBALHEADER,
2283 c1854592 Reimar Döffinger
    .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
2284 1cb5f7fd Michael Niedermayer
};
2285 ff70e601 Måns Rullgård
#endif
2286 b250f9c6 Aurelien Jacobs
#if CONFIG_TGP_MUXER
2287 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_tgp_muxer = {
2288 1cb5f7fd Michael Niedermayer
    "3gp",
2289 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("3GP format"),
2290 1cb5f7fd Michael Niedermayer
    NULL,
2291
    "3gp",
2292 2d243fb3 Ronald S. Bultje
    sizeof(MOVMuxContext),
2293 1cb5f7fd Michael Niedermayer
    CODEC_ID_AMR_NB,
2294
    CODEC_ID_H263,
2295
    mov_write_header,
2296 27a826c9 Martin Storsjö
    ff_mov_write_packet,
2297 1cb5f7fd Michael Niedermayer
    mov_write_trailer,
2298 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
2299 c1854592 Reimar Döffinger
    .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2300 1cb5f7fd Michael Niedermayer
};
2301 ff70e601 Måns Rullgård
#endif
2302 b250f9c6 Aurelien Jacobs
#if CONFIG_MP4_MUXER
2303 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_mp4_muxer = {
2304 1cb5f7fd Michael Niedermayer
    "mp4",
2305 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("MP4 format"),
2306 4cb3f3b6 Dan Christiansen
    "application/mp4",
2307 ccec1b69 Baptiste Coudurier
    "mp4",
2308 2d243fb3 Ronald S. Bultje
    sizeof(MOVMuxContext),
2309 1cb5f7fd Michael Niedermayer
    CODEC_ID_AAC,
2310
    CODEC_ID_MPEG4,
2311
    mov_write_header,
2312 27a826c9 Martin Storsjö
    ff_mov_write_packet,
2313 1cb5f7fd Michael Niedermayer
    mov_write_trailer,
2314 91360ce6 Baptiste Coudurier
    .flags = AVFMT_GLOBALHEADER,
2315 c1854592 Reimar Döffinger
    .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2316 1cb5f7fd Michael Niedermayer
};
2317 ff70e601 Måns Rullgård
#endif
2318 b250f9c6 Aurelien Jacobs
#if CONFIG_PSP_MUXER
2319 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_psp_muxer = {
2320 8af18154 tjcannell@blueyonder.co.uk
    "psp",
2321 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("PSP MP4 format"),
2322 8af18154 tjcannell@blueyonder.co.uk
    NULL,
2323
    "mp4,psp",
2324 2d243fb3 Ronald S. Bultje
    sizeof(MOVMuxContext),
2325 8af18154 tjcannell@blueyonder.co.uk
    CODEC_ID_AAC,
2326
    CODEC_ID_MPEG4,
2327
    mov_write_header,
2328 27a826c9 Martin Storsjö
    ff_mov_write_packet,
2329 8af18154 tjcannell@blueyonder.co.uk
    mov_write_trailer,
2330 dcfdb046 Benjamin Larsson
    .flags = AVFMT_GLOBALHEADER,
2331 c1854592 Reimar Döffinger
    .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
2332 8af18154 tjcannell@blueyonder.co.uk
};
2333 ff70e601 Måns Rullgård
#endif
2334 b250f9c6 Aurelien Jacobs
#if CONFIG_TG2_MUXER
2335 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_tg2_muxer = {
2336 8536ab89 tjraivio@cc.hut.fi
    "3g2",
2337 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("3GP2 format"),
2338 8536ab89 tjraivio@cc.hut.fi
    NULL,
2339
    "3g2",
2340 2d243fb3 Ronald S. Bultje
    sizeof(MOVMuxContext),
2341 8536ab89 tjraivio@cc.hut.fi
    CODEC_ID_AMR_NB,
2342
    CODEC_ID_H263,
2343
    mov_write_header,
2344 27a826c9 Martin Storsjö
    ff_mov_write_packet,
2345 8536ab89 tjraivio@cc.hut.fi
    mov_write_trailer,
2346 c64d476c Calcium
    .flags = AVFMT_GLOBALHEADER,
2347 c1854592 Reimar Döffinger
    .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
2348 8536ab89 tjraivio@cc.hut.fi
};
2349 ff70e601 Måns Rullgård
#endif
2350 b250f9c6 Aurelien Jacobs
#if CONFIG_IPOD_MUXER
2351 c6610a21 Diego Elio Pettenò
AVOutputFormat ff_ipod_muxer = {
2352 aa9f4208 Baptiste Coudurier
    "ipod",
2353 bde15e74 Stefano Sabatini
    NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
2354 aa9f4208 Baptiste Coudurier
    "application/mp4",
2355 ccec1b69 Baptiste Coudurier
    "m4v,m4a",
2356 2d243fb3 Ronald S. Bultje
    sizeof(MOVMuxContext),
2357 aa9f4208 Baptiste Coudurier
    CODEC_ID_AAC,
2358
    CODEC_ID_H264,
2359
    mov_write_header,
2360 27a826c9 Martin Storsjö
    ff_mov_write_packet,
2361 aa9f4208 Baptiste Coudurier
    mov_write_trailer,
2362
    .flags = AVFMT_GLOBALHEADER,
2363 d1df4da0 Baptiste Coudurier
    .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
2364 aa9f4208 Baptiste Coudurier
};
2365
#endif