~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/movenc.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include "riff.h"
24
24
#include "avio.h"
25
25
#include "isom.h"
 
26
#include "avc.h"
26
27
 
27
28
#undef NDEBUG
28
29
#include <assert.h>
36
37
#define MODE_PSP 3 // example working PSP command line:
37
38
// ffmpeg -i testinput.avi  -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
38
39
#define MODE_3G2 4
 
40
#define MODE_IPOD 5
39
41
 
40
42
typedef struct MOVIentry {
41
43
    unsigned int flags, size;
59
61
    int         hasBframes;
60
62
    int         language;
61
63
    int         trackID;
62
 
    int         tag;
 
64
    int         tag; ///< stsd fourcc
63
65
    AVCodecContext *enc;
64
66
 
65
67
    int         vosLen;
78
80
    MOVTrack tracks[MAX_STREAMS];
79
81
} MOVContext;
80
82
 
81
 
//FIXME supprt 64bit varaint with wide placeholders
 
83
//FIXME support 64 bit variant with wide placeholders
82
84
static offset_t updateSize (ByteIOContext *pb, offset_t pos)
83
85
{
84
86
    offset_t curpos = url_ftell(pb);
140
142
        put_be32(pb, 0); // sample size
141
143
        put_be32(pb, entries); // sample count
142
144
        for (i=0; i<track->entry; i++) {
143
 
            for ( j=0; j<track->cluster[i].entries; j++) {
 
145
            for (j=0; j<track->cluster[i].entries; j++) {
144
146
                put_be32(pb, track->cluster[i].size /
145
147
                         track->cluster[i].entries);
146
148
            }
217
219
    return 0x11;
218
220
}
219
221
 
 
222
/**
 
223
 * This function writes extradata "as is".
 
224
 * Extradata must be formated like a valid atom (with size and tag)
 
225
 */
 
226
static int mov_write_extradata_tag(ByteIOContext *pb, MOVTrack *track)
 
227
{
 
228
    put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
 
229
    return track->enc->extradata_size;
 
230
}
 
231
 
220
232
static int mov_write_enda_tag(ByteIOContext *pb)
221
233
{
222
234
    put_be32(pb, 10);
246
258
    offset_t pos = url_ftell(pb);
247
259
    int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
248
260
 
249
 
    put_be32(pb, 0);               // size
 
261
    put_be32(pb, 0); // size
250
262
    put_tag(pb, "esds");
251
 
    put_be32(pb, 0);               // Version
 
263
    put_be32(pb, 0); // Version
252
264
 
253
265
    // ES descriptor
254
266
    putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
255
267
             descrLength(1));
256
268
    put_be16(pb, track->trackID);
257
 
    put_byte(pb, 0x00);            // flags (= no flags)
 
269
    put_byte(pb, 0x00); // flags (= no flags)
258
270
 
259
271
    // DecoderConfig descriptor
260
272
    putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
261
273
 
262
274
    // Object type indication
263
 
    put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
 
275
    put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
264
276
 
265
277
    // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
266
278
    // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
267
279
    if(track->enc->codec_type == CODEC_TYPE_AUDIO)
268
 
        put_byte(pb, 0x15);            // flags (= Audiostream)
 
280
        put_byte(pb, 0x15); // flags (= Audiostream)
269
281
    else
270
 
        put_byte(pb, 0x11);            // flags (= Visualstream)
271
 
 
272
 
    put_byte(pb,  track->enc->rc_buffer_size>>(3+16));             // Buffersize DB (24 bits)
273
 
    put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF);          // Buffersize DB
274
 
 
275
 
    put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate));     // maxbitrate  (FIXME should be max rate in any 1 sec window)
 
282
        put_byte(pb, 0x11); // flags (= Visualstream)
 
283
 
 
284
    put_byte(pb,  track->enc->rc_buffer_size>>(3+16));    // Buffersize DB (24 bits)
 
285
    put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
 
286
 
 
287
    put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
276
288
    if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
277
 
        put_be32(pb, 0);     // vbr
 
289
        put_be32(pb, 0); // vbr
278
290
    else
279
 
        put_be32(pb, track->enc->rc_max_rate);     // avg bitrate
 
291
        put_be32(pb, track->enc->rc_max_rate); // avg bitrate
280
292
 
281
 
    if (track->vosLen)
282
 
    {
 
293
    if (track->vosLen) {
283
294
        // DecoderSpecific info descriptor
284
295
        putDescr(pb, 0x05, track->vosLen);
285
296
        put_buffer(pb, track->vosData, track->vosLen);
286
297
    }
287
298
 
288
 
 
289
299
    // SL descriptor
290
300
    putDescr(pb, 0x06, 1);
291
301
    put_byte(pb, 0x02);
314
324
        mov_write_enda_tag(pb);
315
325
    } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
316
326
        mov_write_amr_tag(pb, track);
 
327
    } else if (track->enc->codec_id == CODEC_ID_ALAC) {
 
328
        mov_write_extradata_tag(pb, track);
317
329
    }
318
330
 
319
331
    put_be32(pb, 8);     /* size */
322
334
    return updateSize (pb, pos);
323
335
}
324
336
 
325
 
static const AVCodecTag codec_movaudio_tags[] = {
326
 
    { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
327
 
    { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
328
 
    { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
329
 
    { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
330
 
    { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
331
 
    { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
332
 
    { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
333
 
    { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
334
 
    { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
335
 
    { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
336
 
    { CODEC_ID_PCM_S24BE, MKTAG('i', 'n', '2', '4') },
337
 
    { CODEC_ID_PCM_S24LE, MKTAG('i', 'n', '2', '4') },
338
 
    { CODEC_ID_PCM_S32BE, MKTAG('i', 'n', '3', '2') },
339
 
    { CODEC_ID_PCM_S32LE, MKTAG('i', 'n', '3', '2') },
340
 
    { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
341
 
    { CODEC_ID_NONE, 0 },
342
 
};
 
337
static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack* track)
 
338
{
 
339
    put_be32(pb, track->vosLen+8);
 
340
    put_tag(pb, "glbl");
 
341
    put_buffer(pb, track->vosData, track->vosLen);
 
342
    return 8+track->vosLen;
 
343
}
343
344
 
344
345
static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
345
346
{
360
361
    put_be16(pb, 0); /* Revision level */
361
362
    put_be32(pb, 0); /* Reserved */
362
363
 
363
 
    put_be16(pb, track->mode == MODE_MOV ? track->enc->channels : 2); /* Number of channels */
364
 
    /* FIXME 8 bit for 'raw ' in mov */
365
 
    put_be16(pb, 16); /* Reserved */
 
364
    if (track->mode == MODE_MOV) {
 
365
        put_be16(pb, track->enc->channels);
 
366
        if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
 
367
            track->enc->codec_id == CODEC_ID_PCM_S8)
 
368
            put_be16(pb, 8); /* bits per sample */
 
369
        else
 
370
            put_be16(pb, 16);
 
371
        put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
 
372
    } else { /* reserved for mp4/3gp */
 
373
        put_be16(pb, 2);
 
374
        put_be16(pb, 16);
 
375
        put_be16(pb, 0);
 
376
    }
366
377
 
367
 
    put_be16(pb, track->mode == MODE_MOV && track->audio_vbr ? -2 : 0); /* compression ID */
368
378
    put_be16(pb, 0); /* packet size (= 0) */
369
379
    put_be16(pb, track->timescale); /* Time scale */
370
380
    put_be16(pb, 0); /* Reserved */
380
390
       (track->enc->codec_id == CODEC_ID_AAC ||
381
391
        track->enc->codec_id == CODEC_ID_AMR_NB ||
382
392
        track->enc->codec_id == CODEC_ID_PCM_S24LE ||
383
 
        track->enc->codec_id == CODEC_ID_PCM_S32LE))
 
393
        track->enc->codec_id == CODEC_ID_PCM_S32LE ||
 
394
        track->enc->codec_id == CODEC_ID_ALAC))
384
395
        mov_write_wave_tag(pb, track);
385
 
    else if(track->enc->codec_id == CODEC_ID_AAC)
 
396
    else if(track->tag == MKTAG('m','p','4','a'))
386
397
        mov_write_esds_tag(pb, track);
387
398
    else if(track->enc->codec_id == CODEC_ID_AMR_NB)
388
399
        mov_write_amr_tag(pb, track);
 
400
    else if(track->vosLen > 0)
 
401
        mov_write_glbl_tag(pb, track);
389
402
 
390
403
    return updateSize (pb, pos);
391
404
}
415
428
    return 0x15;
416
429
}
417
430
 
418
 
static uint8_t *avc_find_startcode( uint8_t *p, uint8_t *end )
419
 
{
420
 
    uint8_t *a = p + 4 - ((int)p & 3);
421
 
 
422
 
    for( end -= 3; p < a && p < end; p++ ) {
423
 
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
424
 
            return p;
425
 
    }
426
 
 
427
 
    for( end -= 3; p < end; p += 4 ) {
428
 
        uint32_t x = *(uint32_t*)p;
429
 
//      if( (x - 0x01000100) & (~x) & 0x80008000 ) // little endian
430
 
//      if( (x - 0x00010001) & (~x) & 0x00800080 ) // big endian
431
 
        if( (x - 0x01010101) & (~x) & 0x80808080 ) { // generic
432
 
            if( p[1] == 0 ) {
433
 
                if( p[0] == 0 && p[2] == 1 )
434
 
                    return p-1;
435
 
                if( p[2] == 0 && p[3] == 1 )
436
 
                    return p;
437
 
            }
438
 
            if( p[3] == 0 ) {
439
 
                if( p[2] == 0 && p[4] == 1 )
440
 
                    return p+1;
441
 
                if( p[4] == 0 && p[5] == 1 )
442
 
                    return p+2;
443
 
            }
444
 
        }
445
 
    }
446
 
 
447
 
    for( end += 3; p < end; p++ ) {
448
 
        if( p[0] == 0 && p[1] == 0 && p[2] == 1 )
449
 
            return p;
450
 
    }
451
 
 
452
 
    return end + 3;
453
 
}
454
 
 
455
 
static void avc_parse_nal_units(uint8_t **buf, int *size)
456
 
{
457
 
    ByteIOContext pb;
458
 
    uint8_t *p = *buf;
459
 
    uint8_t *end = p + *size;
460
 
    uint8_t *nal_start, *nal_end;
461
 
 
462
 
    url_open_dyn_buf(&pb);
463
 
    nal_start = avc_find_startcode(p, end);
464
 
    while (nal_start < end) {
465
 
        while(!*(nal_start++));
466
 
        nal_end = avc_find_startcode(nal_start, end);
467
 
        put_be32(&pb, nal_end - nal_start);
468
 
        put_buffer(&pb, nal_start, nal_end - nal_start);
469
 
        nal_start = nal_end;
470
 
    }
471
 
    av_freep(buf);
472
 
    *size = url_close_dyn_buf(&pb, buf);
473
 
}
474
 
 
475
431
static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
476
432
{
477
433
    offset_t pos = url_ftell(pb);
478
434
 
479
435
    put_be32(pb, 0);
480
436
    put_tag(pb, "avcC");
481
 
    if (track->vosLen > 6) {
482
 
        /* check for h264 start code */
483
 
        if (AV_RB32(track->vosData) == 0x00000001) {
484
 
            uint8_t *buf, *end;
485
 
            uint32_t sps_size=0, pps_size=0;
486
 
            uint8_t *sps=0, *pps=0;
487
 
 
488
 
            avc_parse_nal_units(&track->vosData, &track->vosLen);
489
 
            buf = track->vosData;
490
 
            end = track->vosData + track->vosLen;
491
 
 
492
 
            /* look for sps and pps */
493
 
            while (buf < end) {
494
 
                unsigned int size;
495
 
                uint8_t nal_type;
496
 
                size = AV_RB32(buf);
497
 
                nal_type = buf[4] & 0x1f;
498
 
                if (nal_type == 7) { /* SPS */
499
 
                    sps = buf + 4;
500
 
                    sps_size = size;
501
 
                } else if (nal_type == 8) { /* PPS */
502
 
                    pps = buf + 4;
503
 
                    pps_size = size;
504
 
                }
505
 
                buf += size + 4;
506
 
            }
507
 
            assert(sps);
508
 
            assert(pps);
509
 
 
510
 
            put_byte(pb, 1); /* version */
511
 
            put_byte(pb, sps[1]); /* profile */
512
 
            put_byte(pb, sps[2]); /* profile compat */
513
 
            put_byte(pb, sps[3]); /* level */
514
 
            put_byte(pb, 0xff); /* 6 bits reserved (111111) + 2 bits nal size length - 1 (11) */
515
 
            put_byte(pb, 0xe1); /* 3 bits reserved (111) + 5 bits number of sps (00001) */
516
 
 
517
 
            put_be16(pb, sps_size);
518
 
            put_buffer(pb, sps, sps_size);
519
 
            put_byte(pb, 1); /* number of pps */
520
 
            put_be16(pb, pps_size);
521
 
            put_buffer(pb, pps, pps_size);
522
 
        } else {
523
 
            put_buffer(pb, track->vosData, track->vosLen);
524
 
        }
 
437
    ff_isom_write_avcc(pb, track->vosData, track->vosLen);
 
438
    return updateSize(pb, pos);
 
439
}
 
440
 
 
441
/* also used by all avid codecs (dv, imx, meridien) and their variants */
 
442
static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
 
443
{
 
444
    int i;
 
445
    put_be32(pb, 24); /* size */
 
446
    put_tag(pb, "ACLR");
 
447
    put_tag(pb, "ACLR");
 
448
    put_tag(pb, "0001");
 
449
    put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
 
450
    put_be32(pb, 0); /* unknown */
 
451
 
 
452
    put_be32(pb, 24); /* size */
 
453
    put_tag(pb, "APRG");
 
454
    put_tag(pb, "APRG");
 
455
    put_tag(pb, "0001");
 
456
    put_be32(pb, 1); /* unknown */
 
457
    put_be32(pb, 0); /* unknown */
 
458
 
 
459
    put_be32(pb, 120); /* size */
 
460
    put_tag(pb, "ARES");
 
461
    put_tag(pb, "ARES");
 
462
    put_tag(pb, "0001");
 
463
    put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
 
464
    put_be32(pb, track->enc->width);
 
465
    /* values below are based on samples created with quicktime and avid codecs */
 
466
    if (track->vosData[5] & 2) { // interlaced
 
467
        put_be32(pb, track->enc->height/2);
 
468
        put_be32(pb, 2); /* unknown */
 
469
        put_be32(pb, 0); /* unknown */
 
470
        put_be32(pb, 4); /* unknown */
 
471
    } else {
 
472
        put_be32(pb, track->enc->height);
 
473
        put_be32(pb, 1); /* unknown */
 
474
        put_be32(pb, 0); /* unknown */
 
475
        if (track->enc->height == 1080)
 
476
            put_be32(pb, 5); /* unknown */
 
477
        else
 
478
            put_be32(pb, 6); /* unknown */
525
479
    }
526
 
    return updateSize(pb, pos);
 
480
    /* padding */
 
481
    for (i = 0; i < 10; i++)
 
482
        put_be64(pb, 0);
 
483
 
 
484
    /* extra padding for stsd needed */
 
485
    put_be32(pb, 0);
 
486
    return 0;
527
487
}
528
488
 
529
 
static const AVCodecTag codec_movvideo_tags[] = {
530
 
    { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
531
 
    { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
532
 
    { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
533
 
    { CODEC_ID_H263, MKTAG('h', '2', '6', '3') },
534
 
    { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
535
 
    { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
536
 
    /* special handling in mov_find_video_codec_tag */
537
 
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') }, /* DV NTSC */
538
 
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', 'p') }, /* DV PAL */
539
 
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'p', 'p') }, /* DVCPRO PAL */
540
 
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'n') }, /* DVCPRO50 NTSC */
541
 
    { CODEC_ID_DVVIDEO, MKTAG('d', 'v', '5', 'p') }, /* DVCPRO50 PAL */
542
 
    { CODEC_ID_NONE, 0 },
543
 
};
544
 
 
545
 
static int mov_find_video_codec_tag(AVFormatContext *s, MOVTrack *track)
 
489
static const AVCodecTag codec_3gp_tags[] = {
 
490
    { CODEC_ID_H263,   MKTAG('s','2','6','3') },
 
491
    { CODEC_ID_H264,   MKTAG('a','v','c','1') },
 
492
    { CODEC_ID_MPEG4,  MKTAG('m','p','4','v') },
 
493
    { CODEC_ID_AAC,    MKTAG('m','p','4','a') },
 
494
    { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
 
495
    { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
 
496
};
 
497
 
 
498
static const AVCodecTag mov_pix_fmt_tags[] = {
 
499
    { PIX_FMT_YUYV422, MKTAG('y','u','v','s') },
 
500
    { PIX_FMT_UYVY422, MKTAG('2','v','u','y') },
 
501
    { PIX_FMT_BGR555,  MKTAG('r','a','w',' ') },
 
502
    { PIX_FMT_RGB24,   MKTAG('r','a','w',' ') },
 
503
    { PIX_FMT_BGR32_1, MKTAG('r','a','w',' ') },
 
504
};
 
505
 
 
506
static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
546
507
{
547
508
    int tag = track->enc->codec_tag;
548
 
    if (!tag) {
 
509
    if (track->mode == MODE_MP4 || track->mode == MODE_PSP || track->mode == MODE_IPOD) {
 
510
        if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
 
511
            return 0;
 
512
        if (track->enc->codec_id == CODEC_ID_H264)           tag = MKTAG('a','v','c','1');
 
513
        else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
 
514
        else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
 
515
    } else if (track->mode == MODE_3GP || track->mode == MODE_3G2) {
 
516
        tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
 
517
    } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
 
518
                        (tag == MKTAG('d','v','c','p') ||
 
519
                         track->enc->codec_id == CODEC_ID_RAWVIDEO))) {
549
520
        if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
550
 
            if (track->enc->height == 480) { /* NTSC */
551
 
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
552
 
                    tag = MKTAG('d', 'v', '5', 'n');
553
 
                else
554
 
                    tag = MKTAG('d', 'v', 'c', ' ');
555
 
            } else { /* assume PAL */
556
 
                if (track->enc->pix_fmt == PIX_FMT_YUV422P)
557
 
                    tag = MKTAG('d', 'v', '5', 'p');
558
 
                else if (track->enc->pix_fmt == PIX_FMT_YUV420P)
559
 
                    tag = MKTAG('d', 'v', 'c', 'p');
560
 
                else
561
 
                    tag = MKTAG('d', 'v', 'p', 'p');
 
521
            if (track->enc->height == 480) /* NTSC */
 
522
                if  (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
 
523
                else                                         tag = MKTAG('d','v','c',' ');
 
524
            else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
 
525
            else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
 
526
            else                                             tag = MKTAG('d','v','p','p');
 
527
        } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
 
528
            tag = codec_get_tag(mov_pix_fmt_tags, track->enc->pix_fmt);
 
529
            if (!tag) // restore tag
 
530
                tag = track->enc->codec_tag;
 
531
        } else {
 
532
            if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
 
533
                tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
 
534
                if (!tag) { // if no mac fcc found, try with Microsoft tags
 
535
                    tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
 
536
                    if (tag)
 
537
                        av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
 
538
                               "the file may be unplayable!\n");
 
539
                }
 
540
            } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
 
541
                tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
 
542
                if (!tag) { // if no mac fcc found, try with Microsoft tags
 
543
                    int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
 
544
                    if (ms_tag) {
 
545
                        tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
 
546
                        av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
 
547
                               "the file may be unplayable!\n");
 
548
                    }
 
549
                }
562
550
            }
563
 
        } else if (track->enc->codec_id == CODEC_ID_H263) {
564
 
            if (track->mode == MODE_MOV)
565
 
                tag = MKTAG('h', '2', '6', '3');
566
 
            else
567
 
                tag = MKTAG('s', '2', '6', '3');
568
 
        } else {
569
 
            tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
570
 
        }
571
 
    }
572
 
    // if no mac fcc found, try with Microsoft tags
573
 
    if (!tag) {
574
 
        tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
575
 
        if (tag) {
576
 
            av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
577
 
        }
578
 
    }
579
 
    assert(tag);
 
551
        }
 
552
    }
580
553
    return tag;
581
554
}
582
555
 
583
 
static int mov_find_audio_codec_tag(AVFormatContext *s, MOVTrack *track)
 
556
/** Write uuid atom.
 
557
 * Needed to make file play in iPods running newest firmware
 
558
 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
 
559
 */
 
560
static int mov_write_uuid_tag_ipod(ByteIOContext *pb)
584
561
{
585
 
    int tag = track->enc->codec_tag;
586
 
    if (!tag) {
587
 
        tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
588
 
    }
589
 
    // if no mac fcc found, try with Microsoft tags
590
 
    if (!tag) {
591
 
        int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
592
 
        if (ms_tag) {
593
 
            tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
594
 
            av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
595
 
        }
596
 
    }
597
 
    assert(tag);
598
 
    return tag;
 
562
    put_be32(pb, 28);
 
563
    put_tag(pb, "uuid");
 
564
    put_be32(pb, 0x6b6840f2);
 
565
    put_be32(pb, 0x5f244fc5);
 
566
    put_be32(pb, 0xba39a51b);
 
567
    put_be32(pb, 0xcf0323f3);
 
568
    put_be32(pb, 0x0);
 
569
    return 28;
599
570
}
600
571
 
601
572
static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
639
610
    put_byte(pb, strlen(compressor_name));
640
611
    put_buffer(pb, compressor_name, 31);
641
612
 
642
 
    put_be16(pb, 0x18); /* Reserved */
 
613
    if (track->mode == MODE_MOV && track->enc->bits_per_sample)
 
614
        put_be16(pb, track->enc->bits_per_sample);
 
615
    else
 
616
        put_be16(pb, 0x18); /* Reserved */
643
617
    put_be16(pb, 0xffff); /* Reserved */
644
 
    if(track->enc->codec_id == CODEC_ID_MPEG4)
 
618
    if(track->tag == MKTAG('m','p','4','v'))
645
619
        mov_write_esds_tag(pb, track);
646
620
    else if(track->enc->codec_id == CODEC_ID_H263)
647
621
        mov_write_d263_tag(pb);
648
622
    else if(track->enc->codec_id == CODEC_ID_SVQ3)
649
623
        mov_write_svq3_tag(pb);
650
 
    else if(track->enc->codec_id == CODEC_ID_H264)
 
624
    else if(track->enc->codec_id == CODEC_ID_DNXHD)
 
625
        mov_write_avid_tag(pb, track);
 
626
    else if(track->enc->codec_id == CODEC_ID_H264) {
651
627
        mov_write_avcc_tag(pb, track);
 
628
        if(track->mode == MODE_IPOD)
 
629
            mov_write_uuid_tag_ipod(pb);
 
630
    } else if(track->vosLen > 0)
 
631
        mov_write_glbl_tag(pb, track);
652
632
 
653
633
    return updateSize (pb, pos);
654
634
}
669
649
 
670
650
static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
671
651
{
672
 
    Time2Sample *ctts_entries;
 
652
    MOV_stts_t *ctts_entries;
673
653
    uint32_t entries = 0;
674
654
    uint32_t atom_size;
675
655
    int i;
703
683
/* Time to sample atom */
704
684
static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
705
685
{
706
 
    Time2Sample *stts_entries;
 
686
    MOV_stts_t *stts_entries;
707
687
    uint32_t entries = -1;
708
688
    uint32_t atom_size;
709
689
    int i;
764
744
    mov_write_stsd_tag(pb, track);
765
745
    mov_write_stts_tag(pb, track);
766
746
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
767
 
        track->hasKeyframes < track->entry)
 
747
        track->hasKeyframes && track->hasKeyframes < track->entry)
768
748
        mov_write_stss_tag(pb, track);
769
749
    if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
770
750
        track->hasBframes)
871
851
    (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
872
852
    put_be16(pb, track->language); /* language */
873
853
    put_be16(pb, 0); /* reserved (quality) */
 
854
 
 
855
    if(version!=0 && track->mode == MODE_MOV){
 
856
        av_log(NULL, AV_LOG_ERROR,
 
857
            "FATAL error, file duration too long for timebase, this file will not be\n"
 
858
            "playable with quicktime. Choose a different timebase or a different\n"
 
859
            "container format\n");
 
860
    }
 
861
 
874
862
    return 32;
875
863
}
876
864
 
929
917
    /* Track width and height, for visual only */
930
918
    if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
931
919
        double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
932
 
        if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
 
920
        if(!sample_aspect_ratio) sample_aspect_ratio = 1;
933
921
        put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
934
922
        put_be32(pb, track->enc->height*0x10000);
935
923
    }
1101
1089
 
1102
1090
static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
1103
1091
    int size = 0;
1104
 
    if ( value && value[0] ) {
 
1092
    if (value && value[0]) {
1105
1093
        offset_t pos = url_ftell(pb);
1106
1094
        put_be32(pb, 0); /* size */
1107
1095
        put_tag(pb, name);
1127
1115
                              AVFormatContext *s)
1128
1116
{
1129
1117
    int size = 0;
1130
 
    if ( s->track ) {
 
1118
    if (s->track) {
1131
1119
        offset_t pos = url_ftell(pb);
1132
1120
        put_be32(pb, 0); /* size */
1133
1121
        put_tag(pb, "trkn");
1175
1163
    int size = 0;
1176
1164
 
1177
1165
    // only save meta tag if required
1178
 
    if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
1179
 
         s->comment[0] || s->genre[0] || s->track ) {
 
1166
    if (s->title[0] || s->author[0] || s->album[0] || s->year ||
 
1167
         s->comment[0] || s->genre[0] || s->track) {
1180
1168
        offset_t pos = url_ftell(pb);
1181
1169
        put_be32(pb, 0); /* size */
1182
1170
        put_tag(pb, "meta");
1191
1179
static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
1192
1180
                              AVFormatContext *s)
1193
1181
{
1194
 
    offset_t pos = url_ftell(pb);
1195
 
    int i;
1196
 
 
1197
 
    put_be32(pb, 0); /* size */
1198
 
    put_tag(pb, "udta");
1199
 
 
1200
 
    /* iTunes meta data */
1201
 
    mov_write_meta_tag(pb, mov, s);
1202
 
 
1203
 
  if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff isnt valid in mp4
 
1182
    int i, req = 0;
 
1183
 
1204
1184
    /* Requirements */
1205
1185
    for (i=0; i<mov->nb_streams; i++) {
1206
1186
        if(mov->tracks[i].entry <= 0) continue;
1207
1187
        if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
1208
1188
            mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
1209
 
            mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
 
1189
            req = 1;
1210
1190
            break;
1211
1191
        }
1212
1192
    }
1213
1193
 
1214
 
    mov_write_string_tag(pb, "\251nam", s->title         , 0);
1215
 
    mov_write_string_tag(pb, "\251aut", s->author        , 0);
1216
 
    mov_write_string_tag(pb, "\251alb", s->album         , 0);
1217
 
    mov_write_day_tag(pb, s->year, 0);
1218
 
    if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
1219
 
        mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
1220
 
    mov_write_string_tag(pb, "\251des", s->comment       , 0);
1221
 
    mov_write_string_tag(pb, "\251gen", s->genre         , 0);
1222
 
  }
1223
 
 
1224
 
    return updateSize(pb, pos);
 
1194
    if (s->title[0]   || s->author[0] || s->album[0] || s->year ||
 
1195
        s->comment[0] || s->genre[0]  || s->track ||
 
1196
       (mov->mode == MODE_MOV &&
 
1197
      ((mov->tracks[0].enc && !mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT) || req))) {
 
1198
        offset_t pos = url_ftell(pb);
 
1199
 
 
1200
        put_be32(pb, 0); /* size */
 
1201
        put_tag(pb, "udta");
 
1202
 
 
1203
        /* iTunes meta data */
 
1204
        mov_write_meta_tag(pb, mov, s);
 
1205
 
 
1206
        if(mov->mode == MODE_MOV){ // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
 
1207
            /* Requirements */
 
1208
            if (req)
 
1209
                mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
 
1210
 
 
1211
            mov_write_string_tag(pb, "\251nam", s->title         , 0);
 
1212
            mov_write_string_tag(pb, "\251aut", s->author        , 0);
 
1213
            mov_write_string_tag(pb, "\251alb", s->album         , 0);
 
1214
            mov_write_day_tag(pb, s->year, 0);
 
1215
            if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
 
1216
                mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
 
1217
            mov_write_string_tag(pb, "\251des", s->comment       , 0);
 
1218
            mov_write_string_tag(pb, "\251gen", s->genre         , 0);
 
1219
        }
 
1220
 
 
1221
        return updateSize(pb, pos);
 
1222
    }
 
1223
 
 
1224
    return 0;
1225
1225
}
1226
1226
 
1227
 
static int utf8len(uint8_t *b){
 
1227
static int utf8len(const uint8_t *b){
1228
1228
    int len=0;
1229
1229
    int val;
1230
1230
    while(*b){
1234
1234
    return len;
1235
1235
}
1236
1236
 
1237
 
static int ascii_to_wc (ByteIOContext *pb, uint8_t *b)
 
1237
static int ascii_to_wc (ByteIOContext *pb, const uint8_t *b)
1238
1238
{
1239
1239
    int val;
1240
1240
    while(*b){
1261
1261
        put_be32(pb, 0); /* size placeholder*/
1262
1262
        put_tag(pb, "uuid");
1263
1263
        put_tag(pb, "USMT");
1264
 
        put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1265
 
        put_be32(pb, 0xbb88695c );
1266
 
        put_be32(pb, 0xfac9c740 );
 
1264
        put_be32(pb, 0x21d24fce); /* 96 bit UUID */
 
1265
        put_be32(pb, 0xbb88695c);
 
1266
        put_be32(pb, 0xfac9c740);
1267
1267
        size += 24;
1268
1268
 
1269
1269
        put_be32(pb, 0); /* size placeholder*/
1354
1354
 
1355
1355
    if (mov->mode == MODE_PSP)
1356
1356
        mov_write_uuidusmt_tag(pb, s);
1357
 
    else
 
1357
    else if (mov->mode != MODE_3GP && mov->mode != MODE_3G2)
1358
1358
        mov_write_udta_tag(pb, mov, s);
1359
1359
 
1360
1360
    return updateSize(pb, pos);
1376
1376
{
1377
1377
    MOVContext *mov = s->priv_data;
1378
1378
 
1379
 
    put_be32(pb, 0x14 ); /* size */
 
1379
    put_be32(pb, 0x14); /* size */
1380
1380
    put_tag(pb, "ftyp");
1381
1381
 
1382
 
    if ( mov->mode == MODE_3GP )
 
1382
    if (mov->mode == MODE_3GP)
1383
1383
        put_tag(pb, "3gp4");
1384
 
    else if ( mov->mode == MODE_3G2 )
 
1384
    else if (mov->mode == MODE_3G2)
1385
1385
        put_tag(pb, "3g2a");
1386
 
    else if ( mov->mode == MODE_PSP )
 
1386
    else if (mov->mode == MODE_PSP)
1387
1387
        put_tag(pb, "MSNV");
1388
 
    else if ( mov->mode == MODE_MP4 )
 
1388
    else if (mov->mode == MODE_MP4 || mov->mode == MODE_IPOD)
1389
1389
        put_tag(pb, "isom");
1390
1390
    else
1391
1391
        put_tag(pb, "qt  ");
1392
1392
 
1393
 
    put_be32(pb, 0x200 );
 
1393
    put_be32(pb, 0x200);
1394
1394
 
1395
 
    if ( mov->mode == MODE_3GP )
 
1395
    if (mov->mode == MODE_3GP)
1396
1396
        put_tag(pb, "3gp4");
1397
 
    else if ( mov->mode == MODE_3G2 )
 
1397
    else if (mov->mode == MODE_3G2)
1398
1398
        put_tag(pb, "3g2a");
1399
 
    else if ( mov->mode == MODE_PSP )
 
1399
    else if (mov->mode == MODE_PSP)
1400
1400
        put_tag(pb, "MSNV");
1401
 
    else if ( mov->mode == MODE_MP4 )
 
1401
    else if (mov->mode == MODE_MP4 || mov->mode == MODE_IPOD)
1402
1402
        put_tag(pb, "mp41");
1403
1403
    else
1404
1404
        put_tag(pb, "qt  ");
1413
1413
    int audio_kbitrate= AudioCodec->bit_rate / 1000;
1414
1414
    int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
1415
1415
 
1416
 
    put_be32(pb, 0x94 ); /* size */
 
1416
    put_be32(pb, 0x94); /* size */
1417
1417
    put_tag(pb, "uuid");
1418
1418
    put_tag(pb, "PROF");
1419
1419
 
1420
 
    put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
1421
 
    put_be32(pb, 0xbb88695c );
1422
 
    put_be32(pb, 0xfac9c740 );
1423
 
 
1424
 
    put_be32(pb, 0x0 );  /* ? */
1425
 
    put_be32(pb, 0x3 );  /* 3 sections ? */
1426
 
 
1427
 
    put_be32(pb, 0x14 ); /* size */
 
1420
    put_be32(pb, 0x21d24fce); /* 96 bit UUID */
 
1421
    put_be32(pb, 0xbb88695c);
 
1422
    put_be32(pb, 0xfac9c740);
 
1423
 
 
1424
    put_be32(pb, 0x0);  /* ? */
 
1425
    put_be32(pb, 0x3);  /* 3 sections ? */
 
1426
 
 
1427
    put_be32(pb, 0x14); /* size */
1428
1428
    put_tag(pb, "FPRF");
1429
 
    put_be32(pb, 0x0 );  /* ? */
1430
 
    put_be32(pb, 0x0 );  /* ? */
1431
 
    put_be32(pb, 0x0 );  /* ? */
 
1429
    put_be32(pb, 0x0);  /* ? */
 
1430
    put_be32(pb, 0x0);  /* ? */
 
1431
    put_be32(pb, 0x0);  /* ? */
1432
1432
 
1433
 
    put_be32(pb, 0x2c );  /* size */
 
1433
    put_be32(pb, 0x2c);  /* size */
1434
1434
    put_tag(pb, "APRF");   /* audio */
1435
 
    put_be32(pb, 0x0 );
1436
 
    put_be32(pb, 0x2 );   /* TrackID */
 
1435
    put_be32(pb, 0x0);
 
1436
    put_be32(pb, 0x2);   /* TrackID */
1437
1437
    put_tag(pb, "mp4a");
1438
 
    put_be32(pb, 0x20f );
1439
 
    put_be32(pb, 0x0 );
1440
 
    put_be32(pb, audio_kbitrate);
1441
 
    put_be32(pb, audio_kbitrate);
1442
 
    put_be32(pb, AudioRate );
1443
 
    put_be32(pb, AudioCodec->channels );
 
1438
    put_be32(pb, 0x20f);
 
1439
    put_be32(pb, 0x0);
 
1440
    put_be32(pb, audio_kbitrate);
 
1441
    put_be32(pb, audio_kbitrate);
 
1442
    put_be32(pb, AudioRate);
 
1443
    put_be32(pb, AudioCodec->channels);
1444
1444
 
1445
 
    put_be32(pb, 0x34 );  /* size */
 
1445
    put_be32(pb, 0x34);  /* size */
1446
1446
    put_tag(pb, "VPRF");   /* video */
1447
 
    put_be32(pb, 0x0 );
1448
 
    put_be32(pb, 0x1 );    /* TrackID */
 
1447
    put_be32(pb, 0x0);
 
1448
    put_be32(pb, 0x1);    /* TrackID */
1449
1449
    if (VideoCodec->codec_id == CODEC_ID_H264) {
1450
1450
        put_tag(pb, "avc1");
1451
 
        put_be16(pb, 0x014D );
1452
 
        put_be16(pb, 0x0015 );
 
1451
        put_be16(pb, 0x014D);
 
1452
        put_be16(pb, 0x0015);
1453
1453
    } else {
1454
1454
        put_tag(pb, "mp4v");
1455
 
        put_be16(pb, 0x0000 );
1456
 
        put_be16(pb, 0x0103 );
 
1455
        put_be16(pb, 0x0000);
 
1456
        put_be16(pb, 0x0103);
1457
1457
    }
1458
 
    put_be32(pb, 0x0 );
 
1458
    put_be32(pb, 0x0);
1459
1459
    put_be32(pb, video_kbitrate);
1460
1460
    put_be32(pb, video_kbitrate);
1461
1461
    put_be32(pb, FrameRate);
1467
1467
 
1468
1468
static int mov_write_header(AVFormatContext *s)
1469
1469
{
1470
 
    ByteIOContext *pb = &s->pb;
 
1470
    ByteIOContext *pb = s->pb;
1471
1471
    MOVContext *mov = s->priv_data;
1472
1472
    int i;
1473
1473
 
 
1474
    if (url_is_streamed(s->pb)) {
 
1475
        av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
 
1476
        return -1;
 
1477
    }
 
1478
 
1474
1479
    /* Default mode == MP4 */
1475
1480
    mov->mode = MODE_MP4;
1476
1481
 
1479
1484
        else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
1480
1485
        else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
1481
1486
        else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
 
1487
        else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
1482
1488
 
1483
1489
        mov_write_ftyp_tag(pb,s);
1484
 
        if ( mov->mode == MODE_PSP ) {
1485
 
            if ( s->nb_streams != 2 ) {
 
1490
        if (mov->mode == MODE_PSP) {
 
1491
            if (s->nb_streams != 2) {
1486
1492
                av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
1487
1493
                return -1;
1488
1494
            }
1497
1503
        track->enc = st->codec;
1498
1504
        track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
1499
1505
        track->mode = mov->mode;
 
1506
        track->tag = mov_find_codec_tag(s, track);
 
1507
        if (!track->tag) {
 
1508
            av_log(s, AV_LOG_ERROR, "track %d: could not find tag for codec\n", i);
 
1509
            return -1;
 
1510
        }
1500
1511
        if(st->codec->codec_type == CODEC_TYPE_VIDEO){
1501
 
            track->tag = mov_find_video_codec_tag(s, track);
1502
1512
            track->timescale = st->codec->time_base.den;
1503
1513
            av_set_pts_info(st, 64, 1, st->codec->time_base.den);
 
1514
            if (track->mode == MODE_MOV && track->timescale > 100000)
 
1515
                av_log(s, AV_LOG_WARNING,
 
1516
                       "WARNING codec timebase is very high. If duration is too long,\n"
 
1517
                       "file may not be playable by quicktime. Specify a shorter timebase\n"
 
1518
                       "or choose different container.\n");
1504
1519
        }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
1505
 
            track->tag = mov_find_audio_codec_tag(s, track);
1506
1520
            track->timescale = st->codec->sample_rate;
1507
1521
            av_set_pts_info(st, 64, 1, st->codec->sample_rate);
1508
 
            switch(track->enc->codec_id){
1509
 
            case CODEC_ID_MP3:
1510
 
            case CODEC_ID_AAC:
1511
 
            case CODEC_ID_AMR_NB:
1512
 
            case CODEC_ID_AMR_WB:
 
1522
            if(!st->codec->frame_size){
 
1523
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
 
1524
                return -1;
 
1525
            }else if(st->codec->frame_size > 1){ /* assume compressed audio */
1513
1526
                track->audio_vbr = 1;
1514
 
                break;
1515
 
            default:
 
1527
            }else{
1516
1528
                track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
1517
1529
            }
1518
 
            if (!st->codec->frame_size) {
1519
 
                av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
1520
 
                return -1;
1521
 
            }
1522
1530
        }
1523
1531
    }
1524
1532
 
1534
1542
static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
1535
1543
{
1536
1544
    MOVContext *mov = s->priv_data;
1537
 
    ByteIOContext *pb = &s->pb;
 
1545
    ByteIOContext *pb = s->pb;
1538
1546
    MOVTrack *trk = &mov->tracks[pkt->stream_index];
1539
1547
    AVCodecContext *enc = trk->enc;
1540
1548
    unsigned int samplesInChunk = 0;
1541
1549
    int size= pkt->size;
1542
1550
 
1543
 
    if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
 
1551
    if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
1544
1552
    if (!size) return 0; /* Discard 0 sized packets */
1545
1553
 
1546
1554
    if (enc->codec_id == CODEC_ID_AMR_NB) {
1554
1562
            samplesInChunk++;
1555
1563
        }
1556
1564
        if(samplesInChunk > 1){
1557
 
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, inplement a AVParser for it\n");
 
1565
            av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
1558
1566
            return -1;
1559
1567
        }
1560
1568
    } else if (trk->sampleSize)
1572
1580
    if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
1573
1581
        /* from x264 or from bytestream h264 */
1574
1582
        /* nal reformating needed */
1575
 
        avc_parse_nal_units(&pkt->data, &pkt->size);
 
1583
        int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
 
1584
        if (ret < 0)
 
1585
            return ret;
1576
1586
        assert(pkt->size);
1577
1587
        size = pkt->size;
 
1588
    } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
 
1589
        /* copy frame header to create needed atoms */
 
1590
        if (size < 640)
 
1591
            return -1;
 
1592
        trk->vosLen = 640;
 
1593
        trk->vosData = av_malloc(trk->vosLen);
 
1594
        memcpy(trk->vosData, pkt->data, 640);
1578
1595
    }
1579
1596
 
1580
1597
    if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
1611
1628
static int mov_write_trailer(AVFormatContext *s)
1612
1629
{
1613
1630
    MOVContext *mov = s->priv_data;
1614
 
    ByteIOContext *pb = &s->pb;
 
1631
    ByteIOContext *pb = s->pb;
1615
1632
    int res = 0;
1616
1633
    int i;
1617
1634
 
1635
1652
    for (i=0; i<mov->nb_streams; i++) {
1636
1653
        av_freep(&mov->tracks[i].cluster);
1637
1654
 
1638
 
        if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
 
1655
        if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
1639
1656
 
1640
1657
    }
1641
1658
 
1657
1674
    mov_write_packet,
1658
1675
    mov_write_trailer,
1659
1676
    .flags = AVFMT_GLOBALHEADER,
 
1677
    .codec_tag = (const AVCodecTag*[]){codec_movvideo_tags, codec_movaudio_tags, 0},
1660
1678
};
1661
1679
#endif
1662
1680
#ifdef CONFIG_TGP_MUXER
1672
1690
    mov_write_packet,
1673
1691
    mov_write_trailer,
1674
1692
    .flags = AVFMT_GLOBALHEADER,
 
1693
    .codec_tag = (const AVCodecTag*[]){codec_3gp_tags, 0},
1675
1694
};
1676
1695
#endif
1677
1696
#ifdef CONFIG_MP4_MUXER
1687
1706
    mov_write_packet,
1688
1707
    mov_write_trailer,
1689
1708
    .flags = AVFMT_GLOBALHEADER,
 
1709
    .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
1690
1710
};
1691
1711
#endif
1692
1712
#ifdef CONFIG_PSP_MUXER
1702
1722
    mov_write_packet,
1703
1723
    mov_write_trailer,
1704
1724
    .flags = AVFMT_GLOBALHEADER,
 
1725
    .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
1705
1726
};
1706
1727
#endif
1707
1728
#ifdef CONFIG_TG2_MUXER
1717
1738
    mov_write_packet,
1718
1739
    mov_write_trailer,
1719
1740
    .flags = AVFMT_GLOBALHEADER,
 
1741
    .codec_tag = (const AVCodecTag*[]){codec_3gp_tags, 0},
 
1742
};
 
1743
#endif
 
1744
#ifdef CONFIG_IPOD_MUXER
 
1745
AVOutputFormat ipod_muxer = {
 
1746
    "ipod",
 
1747
    "iPod H.264 mp4 format",
 
1748
    "application/mp4",
 
1749
    NULL,
 
1750
    sizeof(MOVContext),
 
1751
    CODEC_ID_AAC,
 
1752
    CODEC_ID_H264,
 
1753
    mov_write_header,
 
1754
    mov_write_packet,
 
1755
    mov_write_trailer,
 
1756
    .flags = AVFMT_GLOBALHEADER,
 
1757
    .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
1720
1758
};
1721
1759
#endif