movenc.c 82.3 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.h"
36
#include "libavutil/mathematics.h"
37
#include "libavutil/opt.h"
38
#include "libavutil/dict.h"
39
#include "rtpenc.h"
40
#include "mov_chan.h"
41

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

45
static const AVOption options[] = {
46 47
    { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
    { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
48
    FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
49 50 51
    { NULL },
};

52 53 54 55 56 57
#define MOV_CLASS(flavor)\
static const AVClass flavor ## _muxer_class = {\
    .class_name = #flavor " muxer",\
    .item_name  = av_default_item_name,\
    .option     = options,\
    .version    = LIBAVUTIL_VERSION_INT,\
58 59
};

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

    return curpos - pos;
69 70
}

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

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

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

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

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

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

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

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

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

194 195 196
    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
197 198 199
    return 0x11;
}

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

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

210
    avio_wb32(pb, 11);
211
    ffio_wfourcc(pb, "dac3");
212

213
    init_get_bits(&gbc, track->vosData+4, (track->vosLen-4) * 8);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    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);
239
    avio_write(pb, buf, sizeof(buf));
240 241 242 243

    return 11;
}

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

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

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

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

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

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

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

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

    // 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)
298
    if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
299
        avio_w8(pb, 0x15); // flags (= Audiostream)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
300
    else
301
        avio_w8(pb, 0x11); // flags (= Visualstream)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
302

303 304
    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
305

306
    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
307
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
308
        avio_wb32(pb, 0); // vbr
Baptiste Coudurier's avatar
Baptiste Coudurier committed
309
    else
310
        avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
Baptiste Coudurier's avatar
Baptiste Coudurier committed
311

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

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

324 325 326 327 328 329 330 331
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;
}

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

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
{
    uint32_t layout_tag, bitmap;
    int64_t pos = avio_tell(pb);

    layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
                                               track->enc->channel_layout,
                                               &bitmap);
    if (!layout_tag) {
        av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
               "lack of channel information\n");
        return 0;
    }

    avio_wb32(pb, 0);           // Size
    ffio_wfourcc(pb, "chan");   // Type
    avio_w8(pb, 0);             // Version
    avio_wb24(pb, 0);           // Flags
    avio_wb32(pb, layout_tag);  // mChannelLayoutTag
    avio_wb32(pb, bitmap);      // mChannelBitmap
    avio_wb32(pb, 0);           // mNumberChannelDescriptions

    return updateSize(pb, pos);
}

367
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
368
{
369
    int64_t pos = avio_tell(pb);
370

371
    avio_wb32(pb, 0);     /* size */
372
    ffio_wfourcc(pb, "wave");
373

374
    avio_wb32(pb, 12);    /* size */
375
    ffio_wfourcc(pb, "frma");
376
    avio_wl32(pb, track->tag);
377

378
    if (track->enc->codec_id == CODEC_ID_AAC) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
379
        /* useless atom needed by mplayer, ipod, not needed by quicktime */
380
        avio_wb32(pb, 12); /* size */
381
        ffio_wfourcc(pb, "mp4a");
382
        avio_wb32(pb, 0);
383
        mov_write_esds_tag(pb, track);
384
    } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
385
        mov_write_enda_tag(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
386
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
387
        mov_write_amr_tag(pb, track);
388
    } else if (track->enc->codec_id == CODEC_ID_AC3) {
389
        mov_write_chan_tag(pb, track);
390
        mov_write_ac3_tag(pb, track);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
391 392
    } else if (track->enc->codec_id == CODEC_ID_ALAC) {
        mov_write_extradata_tag(pb, track);
393 394 395
    } 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);
396
    }
397

398 399
    avio_wb32(pb, 8);     /* size */
    avio_wb32(pb, 0);     /* null tag */
400

Baptiste Coudurier's avatar
Baptiste Coudurier committed
401
    return updateSize(pb, pos);
402 403
}

404
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
405
{
406
    avio_wb32(pb, track->vosLen+8);
407
    ffio_wfourcc(pb, "glbl");
408
    avio_write(pb, track->vosData, track->vosLen);
409 410 411
    return 8+track->vosLen;
}

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
/**
 * 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;
    }
}

441
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
442
{
443
    int64_t pos = avio_tell(pb);
444 445 446 447
    int version = 0;
    uint32_t tag = track->tag;

    if (track->mode == MODE_MOV) {
448 449 450
        if (mov_get_lpcm_flags(track->enc->codec_id))
            tag = AV_RL32("lpcm");
        version = 2;
451
    }
452

453 454 455 456 457
    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 */
458

459
    /* SoundDescription */
460 461 462
    avio_wb16(pb, version); /* Version */
    avio_wb16(pb, 0); /* Revision level */
    avio_wb32(pb, 0); /* Reserved */
463

464
    if (version == 2) {
465 466 467 468 469 470
        avio_wb16(pb, 3);
        avio_wb16(pb, 16);
        avio_wb16(pb, 0xfffe);
        avio_wb16(pb, 0);
        avio_wb32(pb, 0x00010000);
        avio_wb32(pb, 72);
471
        avio_wb64(pb, av_double2int(track->timescale));
472 473 474 475 476
        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);
477
        avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1);
478
    } else {
479 480 481 482
        /* reserved for mp4/3gp */
        avio_wb16(pb, 2);
        avio_wb16(pb, 16);
        avio_wb16(pb, 0);
483

484 485 486
        avio_wb16(pb, 0); /* packet size (= 0) */
        avio_wb16(pb, track->timescale); /* Time scale */
        avio_wb16(pb, 0); /* Reserved */
487
    }
488

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

Baptiste Coudurier's avatar
Baptiste Coudurier committed
509
    return updateSize(pb, pos);
510 511
}

512
static int mov_write_d263_tag(AVIOContext *pb)
513
{
514
    avio_wb32(pb, 0xf); /* size */
515 516
    ffio_wfourcc(pb, "d263");
    ffio_wfourcc(pb, "FFMP");
517
    avio_w8(pb, 0); /* decoder version */
518
    /* FIXME use AVCodecContext level/profile, when encoder will set values */
519 520
    avio_w8(pb, 0xa); /* level */
    avio_w8(pb, 0); /* profile */
521 522 523
    return 0xf;
}

524
/* TODO: No idea about these values */
525
static int mov_write_svq3_tag(AVIOContext *pb)
526
{
527
    avio_wb32(pb, 0x15);
528 529
    ffio_wfourcc(pb, "SMI ");
    ffio_wfourcc(pb, "SEQH");
530 531 532 533
    avio_wb32(pb, 0x5);
    avio_wb32(pb, 0xe2c0211d);
    avio_wb32(pb, 0xc0000000);
    avio_w8(pb, 0);
534
    return 0x15;
535 536
}

537
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
538
{
539
    int64_t pos = avio_tell(pb);
540

541
    avio_wb32(pb, 0);
542
    ffio_wfourcc(pb, "avcC");
543
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
544 545 546
    return updateSize(pb, pos);
}

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

558
    avio_wb32(pb, 24); /* size */
559 560 561
    ffio_wfourcc(pb, "APRG");
    ffio_wfourcc(pb, "APRG");
    ffio_wfourcc(pb, "0001");
562 563
    avio_wb32(pb, 1); /* unknown */
    avio_wb32(pb, 0); /* unknown */
564

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

    /* extra padding for stsd needed */
591
    avio_wb32(pb, 0);
592 593 594
    return 0;
}

595
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
596
{
Baptiste Coudurier's avatar
Baptiste Coudurier committed
597
    int tag = track->enc->codec_tag;
598

599
    if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
600 601 602 603 604 605
        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');
606 607
    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');
608 609 610 611

    return tag;
}

612 613 614 615 616 617 618 619 620 621 622
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 },
};

623 624 625 626
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;

627
    // keep original tag for subs, ipod supports both formats
628
    if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
629
        (tag == MKTAG('t','x','3','g') ||
630
         tag == MKTAG('t','e','x','t'))))
631
        tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
632

633
    if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
634 635
        av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
               "Quicktime/Ipod might not play the file\n");
636 637 638 639 640 641 642 643

    return tag;
}

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

644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
    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;
    }
661 662 663 664

    return tag;
}

665 666 667 668 669 670 671
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 },
672
    { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
673 674 675
    { 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 },
676
    { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
677
    { PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
678
    { PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
679
    { PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
680
    { PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
681
    { PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
682
    { PIX_FMT_ABGR,    MKTAG('A','B','G','R'), 32 },
683
    { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
684 685
};

686 687 688
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;
689 690 691 692 693 694 695 696 697
    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;
        }
    }
698 699 700 701 702 703 704 705 706

    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 &&
707
                 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
708
                  track->enc->codec_id == CODEC_ID_RAWVIDEO ||
709
                  track->enc->codec_id == CODEC_ID_H263 ||
710
                  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
711
        if (track->enc->codec_id == CODEC_ID_DVVIDEO)
712
            tag = mov_get_dv_codec_tag