movenc.c 80.4 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/opt.h"
36

37 38 39
#undef NDEBUG
#include <assert.h>

40
static const AVOption options[] = {
41 42
    { "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" },
43 44 45 46 47 48 49 50 51 52
    { 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
53
//FIXME support 64 bit variant with wide placeholders
54
static int64_t updateSize(AVIOContext *pb, int64_t pos)
55
{
56
    int64_t curpos = avio_tell(pb);
57
    avio_seek(pb, pos, SEEK_SET);
58
    avio_wb32(pb, curpos - pos); /* rewrite size */
59
    avio_seek(pb, curpos, SEEK_SET);
60 61

    return curpos - pos;
62 63
}

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

87
/* Sample size atom */
88
static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
89
{
90
    int equalChunks = 1;
91
    int i, j, entries = 0, tst = -1, oldtst = -1;
92

93
    int64_t pos = avio_tell(pb);
94
    avio_wb32(pb, 0); /* size */
95
    ffio_wfourcc(pb, "stsz");
96
    avio_wb32(pb, 0); /* version & flags */
97

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

125
/* Sample to chunk atom */
126
static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
127
{
128
    int index = 0, oldval = -1, i;
129
    int64_t entryPos, curpos;
130

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

Baptiste Coudurier's avatar
Baptiste Coudurier committed
152
    return updateSize(pb, pos);
153 154
}

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

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

187 188 189
    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
190 191 192
    return 0x11;
}

193
static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
194 195 196 197 198 199 200 201 202
{
    GetBitContext gbc;
    PutBitContext pbc;
    uint8_t buf[3];
    int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;

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

203
    avio_wb32(pb, 11);
204
    ffio_wfourcc(pb, "dac3");
205

206
    init_get_bits(&gbc, track->vosData+4, track->vosLen-4);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    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);
232
    avio_write(pb, buf, sizeof(buf));
233 234 235 236

    return 11;
}

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

247
static int mov_write_enda_tag(AVIOContext *pb)
248
{
249
    avio_wb32(pb, 10);
250
    ffio_wfourcc(pb, "enda");
251
    avio_wb16(pb, 1); /* little endian */
252 253 254
    return 10;
}

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

264
static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
Baptiste Coudurier's avatar
Baptiste Coudurier committed
265
{
266
    int64_t pos = avio_tell(pb);
267
    int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
Baptiste Coudurier's avatar
Baptiste Coudurier committed
268

269
    avio_wb32(pb, 0); // size
270
    ffio_wfourcc(pb, "esds");
271
    avio_wb32(pb, 0); // Version
Baptiste Coudurier's avatar
Baptiste Coudurier committed
272 273

    // ES descriptor
274
    putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
275 276
    avio_wb16(pb, track->trackID);
    avio_w8(pb, 0x00); // flags (= no flags)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
277 278 279 280 281

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

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

    // 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)
291
    if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
292
        avio_w8(pb, 0x15); // flags (= Audiostream)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
293
    else
294
        avio_w8(pb, 0x11); // flags (= Visualstream)
Baptiste Coudurier's avatar
Baptiste Coudurier committed
295

296 297
    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
298

299
    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
300
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
301
        avio_wb32(pb, 0); // vbr
Baptiste Coudurier's avatar
Baptiste Coudurier committed
302
    else
303
        avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
Baptiste Coudurier's avatar
Baptiste Coudurier committed
304

305
    if (track->vosLen) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
306 307
        // DecoderSpecific info descriptor
        putDescr(pb, 0x05, track->vosLen);
308
        avio_write(pb, track->vosData, track->vosLen);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
309 310 311 312
    }

    // SL descriptor
    putDescr(pb, 0x06, 1);
313
    avio_w8(pb, 0x02);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
314
    return updateSize(pb, pos);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
315 316
}

317 318 319 320 321 322 323 324
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;
}

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

335
static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
336
{
337
    int64_t pos = avio_tell(pb);
338

339
    avio_wb32(pb, 0);     /* size */
340
    ffio_wfourcc(pb, "wave");
341

342
    avio_wb32(pb, 12);    /* size */
343
    ffio_wfourcc(pb, "frma");
344
    avio_wl32(pb, track->tag);
345

346
    if (track->enc->codec_id == CODEC_ID_AAC) {
Baptiste Coudurier's avatar
Baptiste Coudurier committed
347
        /* useless atom needed by mplayer, ipod, not needed by quicktime */
348
        avio_wb32(pb, 12); /* size */
349
        ffio_wfourcc(pb, "mp4a");
350
        avio_wb32(pb, 0);
351
        mov_write_esds_tag(pb, track);
352
    } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
353
        mov_write_enda_tag(pb);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
354
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
355
        mov_write_amr_tag(pb, track);
356 357
    } else if (track->enc->codec_id == CODEC_ID_AC3) {
        mov_write_ac3_tag(pb, track);
Baptiste Coudurier's avatar
Baptiste Coudurier committed
358 359
    } else if (track->enc->codec_id == CODEC_ID_ALAC) {
        mov_write_extradata_tag(pb, track);
360 361 362
    } 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);
363
    }
364

365 366
    avio_wb32(pb, 8);     /* size */
    avio_wb32(pb, 0);     /* null tag */
367

Baptiste Coudurier's avatar
Baptiste Coudurier committed
368
    return updateSize(pb, pos);
369 370
}

371
static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
372
{
373
    avio_wb32(pb, track->vosLen+8);
374
    ffio_wfourcc(pb, "glbl");
375
    avio_write(pb, track->vosData, track->vosLen);
376 377 378
    return 8+track->vosLen;
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/**
 * 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;
    }
}

408
static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
409
{
410
    int64_t pos = avio_tell(pb);
411 412 413 414 415 416 417 418
    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;
419 420 421
        } 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) {
422 423 424
            version = 1;
        }
    }
425

426 427 428 429 430
    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 */
431

432
    /* SoundDescription */
433 434 435
    avio_wb16(pb, version); /* Version */
    avio_wb16(pb, 0); /* Revision level */
    avio_wb32(pb, 0); /* Reserved */
436

437
    if (version == 2) {
438 439 440 441 442 443 444 445 446 447 448 449 450
        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);
451
    } else {
452
        if (track->mode == MODE_MOV) {
453
            avio_wb16(pb, track->enc->channels);
454 455
            if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
                track->enc->codec_id == CODEC_ID_PCM_S8)
456
                avio_wb16(pb, 8); /* bits per sample */
457
            else
458 459
                avio_wb16(pb, 16);
            avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
460
        } else { /* reserved for mp4/3gp */
461 462 463
            avio_wb16(pb, 2);
            avio_wb16(pb, 16);
            avio_wb16(pb, 0);
464
        }
465

466 467 468
        avio_wb16(pb, 0); /* packet size (= 0) */
        avio_wb16(pb, track->timescale); /* Time scale */
        avio_wb16(pb, 0); /* Reserved */
469
    }
470

471
    if(version == 1) { /* SoundDescription V1 extended info */
472 473 474 475
        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 */
476 477
    }

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

Baptiste Coudurier's avatar
Baptiste Coudurier committed
498
    return updateSize(pb, pos);
499 500
}

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

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

526
static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
527
{
528
    int64_t pos = avio_tell(pb);
529

530
    avio_wb32(pb, 0);
531
    ffio_wfourcc(pb, "avcC");
532
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
533 534 535
    return updateSize(pb, pos);
}

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

547
    avio_wb32(pb, 24); /* size */
548 549 550
    ffio_wfourcc(pb, "APRG");
    ffio_wfourcc(pb, "APRG");
    ffio_wfourcc(pb, "0001");
551 552
    avio_wb32(pb, 1); /* unknown */
    avio_wb32(pb, 0); /* unknown */
553

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

    /* extra padding for stsd needed */
580
    avio_wb32(pb, 0);
581 582 583
    return 0;
}

584
static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
585
{
Baptiste Coudurier's avatar
Baptiste Coudurier committed
586
    int tag = track->enc->codec_tag;
587

588
    if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
589 590 591 592 593 594
        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');
595 596
    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');
597 598 599 600

    return tag;
}

601 602 603 604 605 606 607 608 609 610 611
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 },
};

612 613 614 615
static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;

616
    // keep original tag for subs, ipod supports both formats
617
    if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
618
        (tag == MKTAG('t','x','3','g') ||
619
         tag == MKTAG('t','e','x','t'))))
620
        tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
621

622
    if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
623 624
        av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
               "Quicktime/Ipod might not play the file\n");
625 626 627 628 629 630 631 632

    return tag;
}

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

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
    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;
    }
650 651 652 653

    return tag;
}

654 655 656 657 658 659 660
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 },
661
    { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
662 663 664
    { 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 },
665
    { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
666
    { PIX_FMT_RGB24,   MKTAG('r','a','w',' '), 24 },
667
    { PIX_FMT_BGR24,   MKTAG('2','4','B','G'), 24 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
668
    { PIX_FMT_ARGB,    MKTAG('r','a','w',' '), 32 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
669
    { PIX_FMT_BGRA,    MKTAG('B','G','R','A'), 32 },
Baptiste Coudurier's avatar
Baptiste Coudurier committed
670
    { PIX_FMT_RGBA,    MKTAG('R','G','B','A'), 32 },
671
    { PIX_FMT_ABGR,    MKTAG('A','B','G','R'), 32 },
672
    { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
673 674
};

675 676 677
static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
{
    int tag = track->enc->codec_tag;
678 679 680 681 682 683 684 685 686
    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;
        }
    }
687 688 689 690 691 692 693 694 695

    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 &&
696
                 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
697
                  track->enc->codec_id == CODEC_ID_RAWVIDEO ||
698
                  track->enc->codec_id == CODEC_ID_H263 ||
699
                  av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
700
        if (track->enc->codec_id == CODEC_ID_DVVIDEO)
701
            tag = mov_get_dv_codec_tag(s, track);
702
        else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
703
            tag = mov_get_rawvideo_codec_tag(s, track);
704
        else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
705
            tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);