movenc.c 82.2 KB
Newer Older
1
/*
2
 * MOV, 3GP, MP4 muxer
3 4
 * Copyright (c) 2003 Thomas Raivio
 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
5
 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
6
 *
7
 * This file is part of Libav.
8
 *
9
 * Libav is free software; you can redistribute it and/or
10 11
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * Libav is distributed in the hope that it will be useful,
15 16 17 18 19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with Libav; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
23

24
#include "movenc.h"
25
#include "avformat.h"
26
#include "avio_internal.h"
27
#include "riff.h"
28
#include "avio.h"
29
#include "isom.h"
30
#include "avc.h"
31
#include "libavcodec/get_bits.h"
32
#include "libavcodec/put_bits.h"
33 34
#include "internal.h"
#include "libavutil/avstring.h"
35
#include "libavutil/intfloat_readwrite.h"
36
#include "libavutil/mathematics.h"
37
#include "libavutil/opt.h"
38
#include "libavutil/dict.h"
39
#include "rtpenc.h"
40

41 42 43
#undef NDEBUG
#include <assert.h>

44
static const AVOption options[] = {
45 46
    { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), FF_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
    { "rtphint", "Add RTP hint tracks", 0, FF_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
47
    FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
48 49 50 51 52 53 54 55 56 57
    { NULL },
};

static const AVClass mov_muxer_class = {
    .class_name = "MOV/3GP/MP4/3G2 muxer",
    .item_name  = av_default_item_name,
    .option     = options,
    .version    = LIBAVUTIL_VERSION_INT,
};

Diego Biurrun's avatar
Diego Biurrun committed
58
//FIXME support 64 bit variant with wide placeholders
59
static int64_t updateSize(AVIOContext *pb, int64_t pos)
60
{
61
    int64_t curpos = avio_tell(pb);
62
    avio_seek(pb, pos, SEEK_SET);
63
    avio_wb32(pb, curpos - pos); /* rewrite size */
64
    avio_seek(pb, curpos, SEEK_SET);
65 66

    return curpos - pos;
67 68
}

69
/* Chunk offset atom */
70
static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
71 72
{
    int i;
Benjamin Larsson's avatar
Benjamin Larsson committed
73
    int mode64 = 0; //   use 32 bit size variant if possible
74
    int64_t pos = avio_tell(pb);
75
    avio_wb32(pb, 0); /* size */
76 77
    if (pos > UINT32_MAX) {
        mode64 = 1;
78
        ffio_wfourcc(pb, "co64");
79
    } else
80
        ffio_wfourcc(pb, "stco");
81 82
    avio_wb32(pb, 0); /* version & flags */
    avio_wb32(pb, track->entry); /* entry count */
83
    for (i=0; i<track->entry; i++) {
84
        if(mode64 == 1)
85
            avio_wb64(pb, track->cluster[i].pos);
86
        else
87
            avio_wb32(pb, track->cluster[i].pos);
88
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
89
    return updateSize(pb, pos);
90 91
}

92
/* Sample size atom */
93
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
94
{
95
    int equalChunks = 1;
96
    int i, j, entries = 0, tst = -1, oldtst = -1;
97

98
    int64_t pos = avio_tell(pb);
99
    avio_wb32(pb, 0); /* size */
100
    ffio_wfourcc(pb, "stsz");
101
    avio_wb32(pb, 0); /* version & flags */
102

103
    for (i=0; i<track->entry; i++) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
104
        tst = track->cluster[i].size/track->cluster[i].entries;
105 106
        if(oldtst != -1 && tst != oldtst) {
            equalChunks = 0;
107 108
        }
        oldtst = tst;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
109
        entries += track->cluster[i].entries;
110
    }
111
    if (equalChunks) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
112
        int sSize = track->cluster[0].size/track->cluster[0].entries;
Alex Converse's avatar
Alex Converse committed
113
        sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
114 115
        avio_wb32(pb, sSize); // sample size
        avio_wb32(pb, entries); // sample count
116
    }
117
    else {
118 119
        avio_wb32(pb, 0); // sample size
        avio_wb32(pb, entries); // sample count
120
        for (i=0; i<track->entry; i++) {
121
            for (j=0; j<track->cluster[i].entries; j++) {
122
                avio_wb32(pb, track->cluster[i].size /
Baptiste Coudurier's avatar
Baptiste Coudurier committed
123
                         track->cluster[i].entries);
124
            }
125 126
        }
    }
Baptiste Coudurier's avatar
Baptiste Coudurier committed
127
    return updateSize(pb, pos);
128 129
}

130
/* Sample to chunk atom */
131
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
132
{
133
    int index = 0, oldval = -1, i;
134
    int64_t entryPos, curpos;
135

136
    int64_t pos = avio_tell(pb);
137
    avio_wb32(pb, 0); /* size */
138
    ffio_wfourcc(pb, "stsc");
139
    avio_wb32(pb, 0); // version & flags
140
    entryPos = avio_tell(pb);
141
    avio_wb32(pb, track->entry); // entry count
142
    for (i=0; i<track->entry; i++) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
143
        if(oldval != track->cluster[i].samplesInChunk)
144
        {
145 146 147
            avio_wb32(pb, i+1); // first chunk
            avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk
            avio_wb32(pb, 0x1); // sample description index
Baptiste Coudurier's avatar
Baptiste Coudurier committed
148
            oldval = track->cluster[i].samplesInChunk;
149
            index++;
150 151
        }
    }
152
    curpos = avio_tell(pb);
153
    avio_seek(pb, entryPos, SEEK_SET);
154
    avio_wb32(pb, index); // rewrite size
155
    avio_seek(pb, curpos, SEEK_SET);
156

Baptiste Coudurier's avatar
Baptiste Coudurier committed
157
    return updateSize(pb, pos);
158 159
}

160
/* Sync sample atom */
161
static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
162
{
163
    int64_t curpos, entryPos;
164
    int i, index = 0;
165
    int64_t pos = avio_tell(pb);
166
    avio_wb32(pb, 0); // size
167
    ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
168
    avio_wb32(pb, 0); // version & flags
169
    entryPos = avio_tell(pb);
170
    avio_wb32(pb, track->entry); // entry count
171
    for (i=0; i<track->entry; i++) {
172
        if (track->cluster[i].flags & flag) {
173
            avio_wb32(pb, i+1);
174 175 176
            index++;
        }
    }
177
    curpos = avio_tell(pb);
178
    avio_seek(pb, entryPos, SEEK_SET);
179
    avio_wb32(pb, index); // rewrite size
180
    avio_seek(pb, curpos, SEEK_SET);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
181
    return updateSize(pb, pos);
182 183
}

184
static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
185
{
186
    avio_wb32(pb, 0x11); /* size */
187 188 189
    if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
    else                         ffio_wfourcc(pb, "damr");
    ffio_wfourcc(pb, "FFMP");
190
    avio_w8(pb, 0); /* decoder version */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
191

192 193 194
    avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
    avio_w8(pb, 0x00); /* Mode change period (no restriction) */
    avio_w8(pb, 0x01); /* Frames per sample */
Baptiste Coudurier's avatar
Baptiste Coudurier committed
195 196 197
    return 0x11;
}

198
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
199 200 201 202 203 204 205 206 207
{
    GetBitContext gbc;
    PutBitContext pbc;
    uint8_t buf[3];
    int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;

    if (track->vosLen < 7)
        return -1;

208
    avio_wb32(pb, 11);
209
    ffio_wfourcc(pb, "dac3");
210

211
    init_get_bits(&gbc, track->vosData+4, (track->vosLen-4) * 8);
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
    fscod      = get_bits(&gbc, 2);
    frmsizecod = get_bits(&gbc, 6);
    bsid       = get_bits(&gbc, 5);
    bsmod      = get_bits(&gbc, 3);
    acmod      = get_bits(&gbc, 3);
    if (acmod == 2) {
        skip_bits(&gbc, 2); // dsurmod
    } else {
        if ((acmod & 1) && acmod != 1)
            skip_bits(&gbc, 2); // cmixlev
        if (acmod & 4)
            skip_bits(&gbc, 2); // surmixlev
    }
    lfeon = get_bits1(&gbc);

    init_put_bits(&pbc, buf, sizeof(buf));
    put_bits(&pbc, 2, fscod);
    put_bits(&pbc, 5, bsid);
    put_bits(&pbc, 3, bsmod);
    put_bits(&pbc, 3, acmod);
    put_bits(&pbc, 1, lfeon);
    put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
    put_bits(&pbc, 5, 0); // reserved

    flush_put_bits(&pbc);
237
    avio_write(pb, buf, sizeof(buf));
238 239 240 241

    return 11;
}

Baptiste Coudurier's avatar
Baptiste Coudurier committed
242 243 244 245
/**
 * This function writes extradata "as is".
 * Extradata must be formated like a valid atom (with size and tag)
 */
246
static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
247
{
248
    avio_write(pb, track->enc->extradata, track->enc->extradata_size);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
249 250 251
    return track->enc->extradata_size;
}

252
static int mov_write_enda_tag(AVIOContext *pb)
253
{
254
    avio_wb32(pb, 10);
255
    ffio_wfourcc(pb, "enda");
256
    avio_wb16(pb, 1); /* little endian */
257 258 259
    return 10;
}

260
static void putDescr(AVIOContext *pb, int tag, unsigned int size)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
261
{
262
    int i = 3;
263
    avio_w8(pb, tag);
Michael Niedermayer's avatar
Michael Niedermayer committed
264
    for(; i>0; i--)
265 266
        avio_w8(pb, (size>>(7*i)) | 0x80);
    avio_w8(pb, size & 0x7F);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
267 268
}

269
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
Baptiste Coudurier's avatar
Baptiste Coudurier committed
270
{
271
    int64_t pos = avio_tell(pb);
272
    int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
273

274
    avio_wb32(pb, 0); // size
275
    ffio_wfourcc(pb, "esds");
276
    avio_wb32(pb, 0); // Version
Baptiste Coudurier's avatar
Baptiste Coudurier committed
277 278

    // ES descriptor
279
    putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
280 281
    avio_wb16(pb, track->trackID);
    avio_w8(pb, 0x00); // flags (= no flags)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
282 283 284 285 286

    // DecoderConfig descriptor
    putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);

    // Object type indication
287 288
    if ((track->enc->codec_id == CODEC_ID_MP2 ||
         track->enc->codec_id == CODEC_ID_MP3) &&
289
        track->enc->sample_rate > 24000)
290
        avio_w8(pb, 0x6B); // 11172-3
291
    else
292
        avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
Baptiste Coudurier's avatar
Baptiste Coudurier committed
293 294 295

    // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
    // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
296
    if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
297
        avio_w8(pb, 0x15); // flags (= Audiostream)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
298
    else
299
        avio_w8(pb, 0x11); // flags (= Visualstream)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
300

301 302
    avio_w8(pb,  track->enc->rc_buffer_size>>(3+16));      // Buffersize DB (24 bits)
    avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
Baptiste Coudurier's avatar
Baptiste Coudurier committed
303

304
    avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
305
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
306
        avio_wb32(pb, 0); // vbr
Baptiste Coudurier's avatar
Baptiste Coudurier committed
307
    else
308
        avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
Baptiste Coudurier's avatar
Baptiste Coudurier committed
309

310
    if (track->vosLen) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
311 312
        // DecoderSpecific info descriptor
        putDescr(pb, 0x05, track->vosLen);
313
        avio_write(pb, track->vosData, track->vosLen);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
314 315 316 317
    }

    // SL descriptor
    putDescr(pb, 0x06, 1);
318
    avio_w8(pb, 0x02);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
319
    return updateSize(pb, pos);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
320 321
}

322 323 324 325 326 327 328 329
static int mov_pcm_le_gt16(enum CodecID codec_id)
{
    return codec_id == CODEC_ID_PCM_S24LE ||
           codec_id == CODEC_ID_PCM_S32LE ||
           codec_id == CODEC_ID_PCM_F32LE ||
           codec_id == CODEC_ID_PCM_F64LE;
}

330
static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
331
{
332
    int64_t pos = avio_tell(pb);
333 334
    avio_wb32(pb, 0);
    avio_wl32(pb, track->tag); // store it byteswapped
335
    track->enc->codec_tag = av_bswap16(track->tag >> 16);
336 337 338 339
    ff_put_wav_header(pb, track->enc);
    return updateSize(pb, pos);
}

340
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
341
{
342
    int64_t pos = avio_tell(pb);
343

344
    avio_wb32(pb, 0);     /* size */
345
    ffio_wfourcc(pb, "wave");
346

347
    avio_wb32(pb, 12);    /* size */
348
    ffio_wfourcc(pb, "frma");
349
    avio_wl32(pb, track->tag);
350

351
    if (track->enc->codec_id == CODEC_ID_AAC) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
352
        /* useless atom needed by mplayer, ipod, not needed by quicktime */
353
        avio_wb32(pb, 12); /* size */
354
        ffio_wfourcc(pb, "mp4a");
355
        avio_wb32(pb, 0);
356
        mov_write_esds_tag(pb, track);
357
    } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
358
        mov_write_enda_tag(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
359
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
360
        mov_write_amr_tag(pb, track);
361 362
    } else if (track->enc->codec_id == CODEC_ID_AC3) {
        mov_write_ac3_tag(pb, track);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
363 364
    } else if (track->enc->codec_id == CODEC_ID_ALAC) {
        mov_write_extradata_tag(pb, track);
365 366 367
    } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
               track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
        mov_write_ms_tag(pb, track);
368
    }
369

370 371
    avio_wb32(pb, 8);     /* size */
    avio_wb32(pb, 0);     /* null tag */
372

Baptiste Coudurier's avatar
Baptiste Coudurier committed
373
    return updateSize(pb, pos);
374 375
}

376
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
377
{
378
    avio_wb32(pb, track->vosLen+8);
379
    ffio_wfourcc(pb, "glbl");
380
    avio_write(pb, track->vosData, track->vosLen);
381 382 383
    return 8+track->vosLen;
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/**
 * Compute flags for 'lpcm' tag.
 * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
 */
static int mov_get_lpcm_flags(enum CodecID codec_id)
{
    switch (codec_id) {
    case CODEC_ID_PCM_F32BE:
    case CODEC_ID_PCM_F64BE:
        return 11;
    case CODEC_ID_PCM_F32LE:
    case CODEC_ID_PCM_F64LE:
        return 9;
    case CODEC_ID_PCM_U8:
        return 10;
    case CODEC_ID_PCM_S16BE:
    case CODEC_ID_PCM_S24BE:
    case CODEC_ID_PCM_S32BE:
        return 14;
    case CODEC_ID_PCM_S8:
    case CODEC_ID_PCM_S16LE:
    case CODEC_ID_PCM_S24LE:
    case CODEC_ID_PCM_S32LE:
        return 12;
    default:
        return 0;
    }
}

413
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
414
{
415
    int64_t pos = avio_tell(pb);
416 417 418 419 420 421 422 423
    int version = 0;
    uint32_t tag = track->tag;

    if (track->mode == MODE_MOV) {
        if (track->timescale > UINT16_MAX) {
            if (mov_get_lpcm_flags(track->enc->codec_id))
                tag = AV_RL32("lpcm");
            version = 2;
424 425 426
        } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
                   track->enc->codec_id == CODEC_ID_ADPCM_MS ||
                   track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
427 428 429
            version = 1;
        }
    }
430

431 432 433 434 435
    avio_wb32(pb, 0); /* size */
    avio_wl32(pb, tag); // store it byteswapped
    avio_wb32(pb, 0); /* Reserved */
    avio_wb16(pb, 0); /* Reserved */
    avio_wb16(pb, 1); /* Data-reference index, XXX  == 1 */
436

437
    /* SoundDescription */
438 439 440
    avio_wb16(pb, version); /* Version */
    avio_wb16(pb, 0); /* Revision level */
    avio_wb32(pb, 0); /* Reserved */
441

442
    if (version == 2) {
443 444 445 446 447 448 449 450 451 452 453 454 455
        avio_wb16(pb, 3);
        avio_wb16(pb, 16);
        avio_wb16(pb, 0xfffe);
        avio_wb16(pb, 0);
        avio_wb32(pb, 0x00010000);
        avio_wb32(pb, 72);
        avio_wb64(pb, av_dbl2int(track->timescale));
        avio_wb32(pb, track->enc->channels);
        avio_wb32(pb, 0x7F000000);
        avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
        avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
        avio_wb32(pb, track->sampleSize);
        avio_wb32(pb, track->enc->frame_size);
456
    } else {
457
        if (track->mode == MODE_MOV) {
458
            avio_wb16(pb, track->enc->channels);
459 460
            if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
                track->enc->codec_id == CODEC_ID_PCM_S8)
461
                avio_wb16(pb, 8); /* bits per sample */
462
            else
463 464
                avio_wb16(pb, 16);
            avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
465
        } else { /* reserved for mp4/3gp */
466 467 468
            avio_wb16(pb, 2);
            avio_wb16(pb, 16);
            avio_wb16(pb, 0);
469
        }
470

471 472 473
        avio_wb16(pb, 0); /* packet size (= 0) */
        avio_wb16(pb, track->timescale); /* Time scale */
        avio_wb16(pb, 0); /* Reserved */
474
    }
475

476
    if(version == 1) { /* SoundDescription V1 extended info */
477 478 479 480
        avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
        avio_wb32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
        avio_wb32(pb, track->sampleSize); /* Bytes per frame */
        avio_wb32(pb, 2); /* Bytes per sample */
481 482
    }

483 484
    if(track->mode == MODE_MOV &&
       (track->enc->codec_id == CODEC_ID_AAC ||
485
        track->enc->codec_id == CODEC_ID_AC3 ||
486
        track->enc->codec_id == CODEC_ID_AMR_NB ||
487
        track->enc->codec_id == CODEC_ID_ALAC ||
488 489
        track->enc->codec_id == CODEC_ID_ADPCM_MS ||
        track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
490
        mov_pcm_le_gt16(track->enc->codec_id)))
491
        mov_write_wave_tag(pb, track);
492
    else if(track->tag == MKTAG('m','p','4','a'))
Baptiste Coudurier's avatar
Baptiste Coudurier committed
493 494
        mov_write_esds_tag(pb, track);
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
495
        mov_write_amr_tag(pb, track);
496 497
    else if(track->enc->codec_id == CODEC_ID_AC3)
        mov_write_ac3_tag(pb, track);
498
    else if(track->enc->codec_id == CODEC_ID_ALAC)
499
        mov_write_extradata_tag(pb, track);
500 501
    else if(track->vosLen > 0)
        mov_write_glbl_tag(pb, track);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
502

Baptiste Coudurier's avatar
Baptiste Coudurier committed
503
    return updateSize(pb, pos);
504 505
}

506
static int mov_write_d263_tag(AVIOContext *pb)
507
{
508
    avio_wb32(pb, 0xf); /* size */
509 510
    ffio_wfourcc(pb, "d263");
    ffio_wfourcc(pb, "FFMP");
511
    avio_w8(pb, 0); /* decoder version */
512
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
513 514
    avio_w8(pb, 0xa); /* level */
    avio_w8(pb, 0); /* profile */
515 516 517
    return 0xf;
}

518
/* TODO: No idea about these values */
519
static int mov_write_svq3_tag(AVIOContext *pb)
520
{
521
    avio_wb32(pb, 0x15);
522 523
    ffio_wfourcc(pb, "SMI ");
    ffio_wfourcc(pb, "SEQH");
524 525 526 527
    avio_wb32(pb, 0x5);
    avio_wb32(pb, 0xe2c0211d);
    avio_wb32(pb, 0xc0000000);
    avio_w8(pb, 0);
528
    return 0x15;
529 530
}

531
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
532
{
533
    int64_t pos = avio_tell(pb);
534

535
    avio_wb32(pb, 0);
536
    ffio_wfourcc(pb, "avcC");
537
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
538 539 540
    return updateSize(pb, pos);
}

541
/* also used by all avid codecs (dv, imx, meridien) and their variants */
542
static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
543 544
{
    int i;
545
    avio_wb32(pb, 24); /* size */
546 547 548
    ffio_wfourcc(pb, "ACLR");
    ffio_wfourcc(pb, "ACLR");
    ffio_wfourcc(pb, "0001");
549
    avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
550
    avio_wb32(pb, 0); /* unknown */
551

552
    avio_wb32(pb, 24); /* size */
553 554 555
    ffio_wfourcc(pb, "APRG");
    ffio_wfourcc(pb, "APRG");
    ffio_wfourcc(pb, "0001");
556 557
    avio_wb32(pb, 1); /* unknown */
    avio_wb32(pb, 0); /* unknown */
558

559
    avio_wb32(pb, 120); /* size */
560 561 562
    ffio_wfourcc(pb, "ARES");
    ffio_wfourcc(pb, "ARES");
    ffio_wfourcc(pb, "0001");
563 564
    avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
    avio_wb32(pb, track->enc->width);
565 566
    /* values below are based on samples created with quicktime and avid codecs */
    if (track->vosData[5] & 2) { // interlaced
567 568 569 570
        avio_wb32(pb, track->enc->height/2);
        avio_wb32(pb, 2); /* unknown */
        avio_wb32(pb, 0); /* unknown */
        avio_wb32(pb, 4); /* unknown */
571
    } else {
572 573 574
        avio_wb32(pb, track->enc->height);
        avio_wb32(pb, 1); /* unknown */
        avio_wb32(pb, 0); /* unknown */
575
        if (track->enc->height == 1080)
576
            avio_wb32(pb, 5); /* unknown */
577
        else
578
            avio_wb32(pb, 6); /* unknown */
579 580 581
    }
    /* padding */
    for (i = 0; i < 10; i++)
582
        avio_wb64(pb, 0);
583 584

    /* extra padding for stsd needed */
585
    avio_wb32(pb, 0);
586 587 588
    return 0;
}

589
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
590
{
Baptiste Coudurier's avatar
Baptiste Coudurier committed
591
    int tag = track->enc->codec_tag;
592

593
    if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
594 595 596 597 598 599
        return 0;

    if      (track->enc->codec_id == CODEC_ID_H264)      tag = MKTAG('a','v','c','1');
    else if (track->enc->codec_id == CODEC_ID_AC3)       tag = MKTAG('a','c','-','3');
    else if (track->enc->codec_id == CODEC_ID_DIRAC)     tag = MKTAG('d','r','a','c');
    else if (track->enc->codec_id == CODEC_ID_MOV_TEXT)  tag = MKTAG('t','x','3','g');
600 601
    else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
    else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
602 603 604 605

    return tag;
}

606 607 608 609 610 611 612 613 614 615 616
static const AVCodecTag codec_ipod_tags[] = {
    { CODEC_ID_H264,   MKTAG('a','v','c','1') },
    { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
    { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
    { CODEC_ID_ALAC,   MKTAG('a','l','a','c') },
    { CODEC_ID_AC3,    MKTAG('a','c','-','3') },
    { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
    { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
    { CODEC_ID_NONE, 0 },
};

617 618 619 620
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;

621
    // keep original tag for subs, ipod supports both formats
622
    if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
623
        (tag == MKTAG('t','x','3','g') ||
624
         tag == MKTAG('t','e','x','t'))))
625
        tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
626

627
    if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
628 629
        av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
               "Quicktime/Ipod might not play the file\n");
630 631 632 633 634 635 636 637

    return tag;
}

static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag;

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
    if (track->enc->width == 720) /* SD */
        if (track->enc->height == 480) /* NTSC */
            if  (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
            else                                         tag = MKTAG('d','v','c',' ');
        else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
        else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
        else                                             tag = MKTAG('d','v','p','p');
    else if (track->enc->height == 720) /* HD 720 line */
        if  (track->enc->time_base.den == 50)            tag = MKTAG('d','v','h','q');
        else                                             tag = MKTAG('d','v','h','p');
    else if (track->enc->height == 1080) /* HD 1080 line */
        if  (track->enc->time_base.den == 25)            tag = MKTAG('d','v','h','5');
        else                                             tag = MKTAG('d','v','h','6');
    else {
        av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
        return 0;
    }
655 656 657 658

    return tag;
}

659 660 661 662 663 664 665
static const struct {
    enum PixelFormat pix_fmt;
    uint32_t tag;
    unsigned bps;
} mov_pix_fmt_tags[] = {
    { PIX_FMT_YUYV422, MKTAG('y','u','v','s'),  0 },
    { PIX_FMT_UYVY422, MKTAG('2','v','u','y'),  0 },
666
    { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
667 668 669
    { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
    { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
    { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
670
    { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
671
    { PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
672
    { PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
673
    { PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
674
    { PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
675
    { PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
676
    { PIX_FMT_ABGR,    MKTAG('A','B','G','R'), 32 },
677
    { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
678 679
};

680 681 682
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;
683 684 685 686 687 688 689 690 691
    int i;

    for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
        if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
            tag = mov_pix_fmt_tags[i].tag;
            track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
            break;
        }
    }
692 693 694 695 696 697 698 699 700

    return tag;
}

static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;

    if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
701
                 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
702
                  track->enc->codec_id == CODEC_ID_RAWVIDEO ||
703
                  track->enc->codec_id == CODEC_ID_H263 ||
704
                  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
705
        if (track->enc->codec_id == CODEC_ID_DVVIDEO)
706
            tag = mov_get_dv_codec_tag(s, track);
Baptiste Coudurier's avatar