00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "movenc.h"
00025 #include "avformat.h"
00026 #include "avio_internal.h"
00027 #include "riff.h"
00028 #include "avio.h"
00029 #include "isom.h"
00030 #include "avc.h"
00031 #include "libavcodec/get_bits.h"
00032 #include "libavcodec/put_bits.h"
00033 #include "internal.h"
00034 #include "libavutil/avstring.h"
00035 #include "libavutil/intfloat_readwrite.h"
00036 #include "libavutil/mathematics.h"
00037 #include "libavutil/opt.h"
00038 #include "libavutil/dict.h"
00039 #include "rtpenc.h"
00040 #include "mov_chan.h"
00041
00042 #undef NDEBUG
00043 #include <assert.h>
00044
00045 static const AVOption options[] = {
00046 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00047 { "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" },
00048 { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
00049 { "frag_size", "maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
00050 { "frag_duration", "maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
00051 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
00052 { NULL },
00053 };
00054
00055 #define MOV_CLASS(flavor)\
00056 static const AVClass flavor ## _muxer_class = {\
00057 .class_name = #flavor " muxer",\
00058 .item_name = av_default_item_name,\
00059 .option = options,\
00060 .version = LIBAVUTIL_VERSION_INT,\
00061 };
00062
00063 static int update_first_fragment(AVFormatContext *s);
00064
00065
00066 static int64_t updateSize(AVIOContext *pb, int64_t pos)
00067 {
00068 int64_t curpos = avio_tell(pb);
00069 avio_seek(pb, pos, SEEK_SET);
00070 avio_wb32(pb, curpos - pos);
00071 avio_seek(pb, curpos, SEEK_SET);
00072
00073 return curpos - pos;
00074 }
00075
00076
00077 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
00078 {
00079 int i;
00080 int mode64 = 0;
00081 int64_t pos = avio_tell(pb);
00082 avio_wb32(pb, 0);
00083 if (pos > UINT32_MAX) {
00084 mode64 = 1;
00085 ffio_wfourcc(pb, "co64");
00086 } else
00087 ffio_wfourcc(pb, "stco");
00088 avio_wb32(pb, 0);
00089 avio_wb32(pb, track->chunkCount);
00090 for (i=0; i<track->entry; i++) {
00091 if(!track->cluster[i].chunkNum)
00092 continue;
00093 if(mode64 == 1)
00094 avio_wb64(pb, track->cluster[i].pos);
00095 else
00096 avio_wb32(pb, track->cluster[i].pos);
00097 }
00098 return updateSize(pb, pos);
00099 }
00100
00101
00102 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
00103 {
00104 int equalChunks = 1;
00105 int i, j, entries = 0, tst = -1, oldtst = -1;
00106
00107 int64_t pos = avio_tell(pb);
00108 avio_wb32(pb, 0);
00109 ffio_wfourcc(pb, "stsz");
00110 avio_wb32(pb, 0);
00111
00112 for (i=0; i<track->entry; i++) {
00113 tst = track->cluster[i].size/track->cluster[i].entries;
00114 if(oldtst != -1 && tst != oldtst) {
00115 equalChunks = 0;
00116 }
00117 oldtst = tst;
00118 entries += track->cluster[i].entries;
00119 }
00120 if (equalChunks) {
00121 int sSize = track->cluster[0].size/track->cluster[0].entries;
00122 sSize = FFMAX(1, sSize);
00123 avio_wb32(pb, sSize);
00124 avio_wb32(pb, entries);
00125 }
00126 else {
00127 avio_wb32(pb, 0);
00128 avio_wb32(pb, entries);
00129 for (i=0; i<track->entry; i++) {
00130 for (j=0; j<track->cluster[i].entries; j++) {
00131 avio_wb32(pb, track->cluster[i].size /
00132 track->cluster[i].entries);
00133 }
00134 }
00135 }
00136 return updateSize(pb, pos);
00137 }
00138
00139
00140 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
00141 {
00142 int index = 0, oldval = -1, i;
00143 int64_t entryPos, curpos;
00144
00145 int64_t pos = avio_tell(pb);
00146 avio_wb32(pb, 0);
00147 ffio_wfourcc(pb, "stsc");
00148 avio_wb32(pb, 0);
00149 entryPos = avio_tell(pb);
00150 avio_wb32(pb, track->chunkCount);
00151 for (i=0; i<track->entry; i++) {
00152 if(oldval != track->cluster[i].samplesInChunk && track->cluster[i].chunkNum)
00153 {
00154 avio_wb32(pb, track->cluster[i].chunkNum);
00155 avio_wb32(pb, track->cluster[i].samplesInChunk);
00156 avio_wb32(pb, 0x1);
00157 oldval = track->cluster[i].samplesInChunk;
00158 index++;
00159 }
00160 }
00161 curpos = avio_tell(pb);
00162 avio_seek(pb, entryPos, SEEK_SET);
00163 avio_wb32(pb, index);
00164 avio_seek(pb, curpos, SEEK_SET);
00165
00166 return updateSize(pb, pos);
00167 }
00168
00169
00170 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
00171 {
00172 int64_t curpos, entryPos;
00173 int i, index = 0;
00174 int64_t pos = avio_tell(pb);
00175 avio_wb32(pb, 0);
00176 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
00177 avio_wb32(pb, 0);
00178 entryPos = avio_tell(pb);
00179 avio_wb32(pb, track->entry);
00180 for (i=0; i<track->entry; i++) {
00181 if (track->cluster[i].flags & flag) {
00182 avio_wb32(pb, i+1);
00183 index++;
00184 }
00185 }
00186 curpos = avio_tell(pb);
00187 avio_seek(pb, entryPos, SEEK_SET);
00188 avio_wb32(pb, index);
00189 avio_seek(pb, curpos, SEEK_SET);
00190 return updateSize(pb, pos);
00191 }
00192
00193 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
00194 {
00195 avio_wb32(pb, 0x11);
00196 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
00197 else ffio_wfourcc(pb, "damr");
00198 ffio_wfourcc(pb, "FFMP");
00199 avio_w8(pb, 0);
00200
00201 avio_wb16(pb, 0x81FF);
00202 avio_w8(pb, 0x00);
00203 avio_w8(pb, 0x01);
00204 return 0x11;
00205 }
00206
00207 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
00208 {
00209 GetBitContext gbc;
00210 PutBitContext pbc;
00211 uint8_t buf[3];
00212 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
00213
00214 if (track->vosLen < 7)
00215 return -1;
00216
00217 avio_wb32(pb, 11);
00218 ffio_wfourcc(pb, "dac3");
00219
00220 init_get_bits(&gbc, track->vosData+4, (track->vosLen-4) * 8);
00221 fscod = get_bits(&gbc, 2);
00222 frmsizecod = get_bits(&gbc, 6);
00223 bsid = get_bits(&gbc, 5);
00224 bsmod = get_bits(&gbc, 3);
00225 acmod = get_bits(&gbc, 3);
00226 if (acmod == 2) {
00227 skip_bits(&gbc, 2);
00228 } else {
00229 if ((acmod & 1) && acmod != 1)
00230 skip_bits(&gbc, 2);
00231 if (acmod & 4)
00232 skip_bits(&gbc, 2);
00233 }
00234 lfeon = get_bits1(&gbc);
00235
00236 init_put_bits(&pbc, buf, sizeof(buf));
00237 put_bits(&pbc, 2, fscod);
00238 put_bits(&pbc, 5, bsid);
00239 put_bits(&pbc, 3, bsmod);
00240 put_bits(&pbc, 3, acmod);
00241 put_bits(&pbc, 1, lfeon);
00242 put_bits(&pbc, 5, frmsizecod>>1);
00243 put_bits(&pbc, 5, 0);
00244
00245 flush_put_bits(&pbc);
00246 avio_write(pb, buf, sizeof(buf));
00247
00248 return 11;
00249 }
00250
00255 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
00256 {
00257 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
00258 return track->enc->extradata_size;
00259 }
00260
00261 static int mov_write_enda_tag(AVIOContext *pb)
00262 {
00263 avio_wb32(pb, 10);
00264 ffio_wfourcc(pb, "enda");
00265 avio_wb16(pb, 1);
00266 return 10;
00267 }
00268
00269 static void putDescr(AVIOContext *pb, int tag, unsigned int size)
00270 {
00271 int i = 3;
00272 avio_w8(pb, tag);
00273 for(; i>0; i--)
00274 avio_w8(pb, (size>>(7*i)) | 0x80);
00275 avio_w8(pb, size & 0x7F);
00276 }
00277
00278 static unsigned compute_avg_bitrate(MOVTrack *track)
00279 {
00280 uint64_t size = 0;
00281 int i;
00282 for (i = 0; i < track->entry; i++)
00283 size += track->cluster[i].size;
00284 return size * 8 * track->timescale / track->trackDuration;
00285 }
00286
00287 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
00288 {
00289 int64_t pos = avio_tell(pb);
00290 int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0;
00291 unsigned avg_bitrate;
00292
00293 avio_wb32(pb, 0);
00294 ffio_wfourcc(pb, "esds");
00295 avio_wb32(pb, 0);
00296
00297
00298 putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1);
00299 avio_wb16(pb, track->trackID);
00300 avio_w8(pb, 0x00);
00301
00302
00303 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
00304
00305
00306 if ((track->enc->codec_id == CODEC_ID_MP2 ||
00307 track->enc->codec_id == CODEC_ID_MP3) &&
00308 track->enc->sample_rate > 24000)
00309 avio_w8(pb, 0x6B);
00310 else
00311 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
00312
00313
00314
00315 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
00316 avio_w8(pb, 0x15);
00317 else
00318 avio_w8(pb, 0x11);
00319
00320 avio_w8(pb, track->enc->rc_buffer_size>>(3+16));
00321 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF);
00322
00323 avg_bitrate = compute_avg_bitrate(track);
00324
00325 avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
00326 avio_wb32(pb, avg_bitrate);
00327
00328 if (track->vosLen) {
00329
00330 putDescr(pb, 0x05, track->vosLen);
00331 avio_write(pb, track->vosData, track->vosLen);
00332 }
00333
00334
00335 putDescr(pb, 0x06, 1);
00336 avio_w8(pb, 0x02);
00337 return updateSize(pb, pos);
00338 }
00339
00340 static int mov_pcm_le_gt16(enum CodecID codec_id)
00341 {
00342 return codec_id == CODEC_ID_PCM_S24LE ||
00343 codec_id == CODEC_ID_PCM_S32LE ||
00344 codec_id == CODEC_ID_PCM_F32LE ||
00345 codec_id == CODEC_ID_PCM_F64LE;
00346 }
00347
00348 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
00349 {
00350 int64_t pos = avio_tell(pb);
00351 avio_wb32(pb, 0);
00352 avio_wl32(pb, track->tag);
00353 track->enc->codec_tag = av_bswap16(track->tag >> 16);
00354 ff_put_wav_header(pb, track->enc);
00355 return updateSize(pb, pos);
00356 }
00357
00358 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
00359 {
00360 uint32_t layout_tag, bitmap;
00361 int64_t pos = avio_tell(pb);
00362
00363 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
00364 track->enc->channel_layout,
00365 &bitmap);
00366 if (!layout_tag) {
00367 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
00368 "lack of channel information\n");
00369 return 0;
00370 }
00371
00372 avio_wb32(pb, 0);
00373 ffio_wfourcc(pb, "chan");
00374 avio_w8(pb, 0);
00375 avio_wb24(pb, 0);
00376 avio_wb32(pb, layout_tag);
00377 avio_wb32(pb, bitmap);
00378 avio_wb32(pb, 0);
00379
00380 return updateSize(pb, pos);
00381 }
00382
00383 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
00384 {
00385 int64_t pos = avio_tell(pb);
00386
00387 avio_wb32(pb, 0);
00388 ffio_wfourcc(pb, "wave");
00389
00390 avio_wb32(pb, 12);
00391 ffio_wfourcc(pb, "frma");
00392 avio_wl32(pb, track->tag);
00393
00394 if (track->enc->codec_id == CODEC_ID_AAC) {
00395
00396 avio_wb32(pb, 12);
00397 ffio_wfourcc(pb, "mp4a");
00398 avio_wb32(pb, 0);
00399 mov_write_esds_tag(pb, track);
00400 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
00401 mov_write_enda_tag(pb);
00402 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
00403 mov_write_amr_tag(pb, track);
00404 } else if (track->enc->codec_id == CODEC_ID_AC3) {
00405 mov_write_chan_tag(pb, track);
00406 mov_write_ac3_tag(pb, track);
00407 } else if (track->enc->codec_id == CODEC_ID_ALAC) {
00408 mov_write_extradata_tag(pb, track);
00409 } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
00410 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
00411 mov_write_ms_tag(pb, track);
00412 }
00413
00414 avio_wb32(pb, 8);
00415 avio_wb32(pb, 0);
00416
00417 return updateSize(pb, pos);
00418 }
00419
00420 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
00421 {
00422 avio_wb32(pb, track->vosLen+8);
00423 ffio_wfourcc(pb, "glbl");
00424 avio_write(pb, track->vosData, track->vosLen);
00425 return 8+track->vosLen;
00426 }
00427
00432 static int mov_get_lpcm_flags(enum CodecID codec_id)
00433 {
00434 switch (codec_id) {
00435 case CODEC_ID_PCM_F32BE:
00436 case CODEC_ID_PCM_F64BE:
00437 return 11;
00438 case CODEC_ID_PCM_F32LE:
00439 case CODEC_ID_PCM_F64LE:
00440 return 9;
00441 case CODEC_ID_PCM_U8:
00442 return 10;
00443 case CODEC_ID_PCM_S16BE:
00444 case CODEC_ID_PCM_S24BE:
00445 case CODEC_ID_PCM_S32BE:
00446 return 14;
00447 case CODEC_ID_PCM_S8:
00448 case CODEC_ID_PCM_S16LE:
00449 case CODEC_ID_PCM_S24LE:
00450 case CODEC_ID_PCM_S32LE:
00451 return 12;
00452 default:
00453 return 0;
00454 }
00455 }
00456
00457 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
00458 {
00459 int64_t pos = avio_tell(pb);
00460 int version = 0;
00461 uint32_t tag = track->tag;
00462
00463 if (track->mode == MODE_MOV) {
00464 if (mov_get_lpcm_flags(track->enc->codec_id))
00465 tag = AV_RL32("lpcm");
00466 version = 2;
00467 }
00468
00469 avio_wb32(pb, 0);
00470 avio_wl32(pb, tag);
00471 avio_wb32(pb, 0);
00472 avio_wb16(pb, 0);
00473 avio_wb16(pb, 1);
00474
00475
00476 avio_wb16(pb, version);
00477 avio_wb16(pb, 0);
00478 avio_wb32(pb, 0);
00479
00480 if (version == 2) {
00481 avio_wb16(pb, 3);
00482 avio_wb16(pb, 16);
00483 avio_wb16(pb, 0xfffe);
00484 avio_wb16(pb, 0);
00485 avio_wb32(pb, 0x00010000);
00486 avio_wb32(pb, 72);
00487 avio_wb64(pb, av_dbl2int(track->timescale));
00488 avio_wb32(pb, track->enc->channels);
00489 avio_wb32(pb, 0x7F000000);
00490 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
00491 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
00492 avio_wb32(pb, track->sampleSize);
00493 avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1);
00494 } else {
00495
00496 avio_wb16(pb, 2);
00497 avio_wb16(pb, 16);
00498 avio_wb16(pb, 0);
00499
00500 avio_wb16(pb, 0);
00501 avio_wb16(pb, track->timescale);
00502 avio_wb16(pb, 0);
00503 }
00504
00505 if(track->mode == MODE_MOV &&
00506 (track->enc->codec_id == CODEC_ID_AAC ||
00507 track->enc->codec_id == CODEC_ID_AC3 ||
00508 track->enc->codec_id == CODEC_ID_AMR_NB ||
00509 track->enc->codec_id == CODEC_ID_ALAC ||
00510 track->enc->codec_id == CODEC_ID_ADPCM_MS ||
00511 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
00512 mov_pcm_le_gt16(track->enc->codec_id)))
00513 mov_write_wave_tag(pb, track);
00514 else if(track->tag == MKTAG('m','p','4','a'))
00515 mov_write_esds_tag(pb, track);
00516 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
00517 mov_write_amr_tag(pb, track);
00518 else if(track->enc->codec_id == CODEC_ID_AC3)
00519 mov_write_ac3_tag(pb, track);
00520 else if(track->enc->codec_id == CODEC_ID_ALAC)
00521 mov_write_extradata_tag(pb, track);
00522 else if(track->vosLen > 0)
00523 mov_write_glbl_tag(pb, track);
00524
00525 return updateSize(pb, pos);
00526 }
00527
00528 static int mov_write_d263_tag(AVIOContext *pb)
00529 {
00530 avio_wb32(pb, 0xf);
00531 ffio_wfourcc(pb, "d263");
00532 ffio_wfourcc(pb, "FFMP");
00533 avio_w8(pb, 0);
00534
00535 avio_w8(pb, 0xa);
00536 avio_w8(pb, 0);
00537 return 0xf;
00538 }
00539
00540
00541 static int mov_write_svq3_tag(AVIOContext *pb)
00542 {
00543 avio_wb32(pb, 0x15);
00544 ffio_wfourcc(pb, "SMI ");
00545 ffio_wfourcc(pb, "SEQH");
00546 avio_wb32(pb, 0x5);
00547 avio_wb32(pb, 0xe2c0211d);
00548 avio_wb32(pb, 0xc0000000);
00549 avio_w8(pb, 0);
00550 return 0x15;
00551 }
00552
00553 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
00554 {
00555 int64_t pos = avio_tell(pb);
00556
00557 avio_wb32(pb, 0);
00558 ffio_wfourcc(pb, "avcC");
00559 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
00560 return updateSize(pb, pos);
00561 }
00562
00563
00564 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
00565 {
00566 int i;
00567 avio_wb32(pb, 24);
00568 ffio_wfourcc(pb, "ACLR");
00569 ffio_wfourcc(pb, "ACLR");
00570 ffio_wfourcc(pb, "0001");
00571 avio_wb32(pb, 2);
00572 avio_wb32(pb, 0);
00573
00574 avio_wb32(pb, 24);
00575 ffio_wfourcc(pb, "APRG");
00576 ffio_wfourcc(pb, "APRG");
00577 ffio_wfourcc(pb, "0001");
00578 avio_wb32(pb, 1);
00579 avio_wb32(pb, 0);
00580
00581 avio_wb32(pb, 120);
00582 ffio_wfourcc(pb, "ARES");
00583 ffio_wfourcc(pb, "ARES");
00584 ffio_wfourcc(pb, "0001");
00585 avio_wb32(pb, AV_RB32(track->vosData + 0x28));
00586 avio_wb32(pb, track->enc->width);
00587
00588 if (track->vosData[5] & 2) {
00589 avio_wb32(pb, track->enc->height/2);
00590 avio_wb32(pb, 2);
00591 avio_wb32(pb, 0);
00592 avio_wb32(pb, 4);
00593 } else {
00594 avio_wb32(pb, track->enc->height);
00595 avio_wb32(pb, 1);
00596 avio_wb32(pb, 0);
00597 if (track->enc->height == 1080)
00598 avio_wb32(pb, 5);
00599 else
00600 avio_wb32(pb, 6);
00601 }
00602
00603 for (i = 0; i < 10; i++)
00604 avio_wb64(pb, 0);
00605
00606
00607 avio_wb32(pb, 0);
00608 return 0;
00609 }
00610
00611 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
00612 {
00613 int tag = track->enc->codec_tag;
00614
00615 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
00616 return 0;
00617
00618 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
00619 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
00620 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
00621 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
00622 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
00623 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
00624
00625 return tag;
00626 }
00627
00628 static const AVCodecTag codec_ipod_tags[] = {
00629 { CODEC_ID_H264, MKTAG('a','v','c','1') },
00630 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
00631 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
00632 { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
00633 { CODEC_ID_AC3, MKTAG('a','c','-','3') },
00634 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
00635 { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
00636 { CODEC_ID_NONE, 0 },
00637 };
00638
00639 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
00640 {
00641 int tag = track->enc->codec_tag;
00642
00643
00644 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
00645 (tag == MKTAG('t','x','3','g') ||
00646 tag == MKTAG('t','e','x','t'))))
00647 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
00648
00649 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
00650 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
00651 "Quicktime/Ipod might not play the file\n");
00652
00653 return tag;
00654 }
00655
00656 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
00657 {
00658 int tag;
00659
00660 if (track->enc->width == 720)
00661 if (track->enc->height == 480)
00662 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
00663 else tag = MKTAG('d','v','c',' ');
00664 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
00665 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
00666 else tag = MKTAG('d','v','p','p');
00667 else if (track->enc->height == 720)
00668 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
00669 else tag = MKTAG('d','v','h','p');
00670 else if (track->enc->height == 1080)
00671 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
00672 else tag = MKTAG('d','v','h','6');
00673 else {
00674 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
00675 return 0;
00676 }
00677
00678 return tag;
00679 }
00680
00681 static const struct {
00682 enum PixelFormat pix_fmt;
00683 uint32_t tag;
00684 unsigned bps;
00685 } mov_pix_fmt_tags[] = {
00686 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
00687 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
00688 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
00689 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
00690 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
00691 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
00692 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
00693 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
00694 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
00695 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
00696 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
00697 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
00698 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
00699 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
00700 };
00701
00702 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
00703 {
00704 int tag = track->enc->codec_tag;
00705 int i;
00706
00707 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
00708 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
00709 tag = mov_pix_fmt_tags[i].tag;
00710 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
00711 break;
00712 }
00713 }
00714
00715 return tag;
00716 }
00717
00718 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
00719 {
00720 int tag = track->enc->codec_tag;
00721
00722 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
00723 (track->enc->codec_id == CODEC_ID_DVVIDEO ||
00724 track->enc->codec_id == CODEC_ID_RAWVIDEO ||
00725 track->enc->codec_id == CODEC_ID_H263 ||
00726 av_get_bits_per_sample(track->enc->codec_id)))) {
00727 if (track->enc->codec_id == CODEC_ID_DVVIDEO)
00728 tag = mov_get_dv_codec_tag(s, track);
00729 else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
00730 tag = mov_get_rawvideo_codec_tag(s, track);
00731 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00732 tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
00733 if (!tag) {
00734 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
00735 if (tag)
00736 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
00737 "the file may be unplayable!\n");
00738 }
00739 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00740 tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
00741 if (!tag) {
00742 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
00743 if (ms_tag) {
00744 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
00745 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
00746 "the file may be unplayable!\n");
00747 }
00748 }
00749 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
00750 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
00751 }
00752
00753 return tag;
00754 }
00755
00756 static const AVCodecTag codec_3gp_tags[] = {
00757 { CODEC_ID_H263, MKTAG('s','2','6','3') },
00758 { CODEC_ID_H264, MKTAG('a','v','c','1') },
00759 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
00760 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
00761 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
00762 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
00763 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
00764 { CODEC_ID_NONE, 0 },
00765 };
00766
00767 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
00768 {
00769 int tag = track->enc->codec_tag;
00770
00771 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
00772 tag = mp4_get_codec_tag(s, track);
00773 else if (track->mode == MODE_IPOD)
00774 tag = ipod_get_codec_tag(s, track);
00775 else if (track->mode & MODE_3GP)
00776 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
00777 else
00778 tag = mov_get_codec_tag(s, track);
00779
00780 return tag;
00781 }
00782
00787 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
00788 {
00789 avio_wb32(pb, 28);
00790 ffio_wfourcc(pb, "uuid");
00791 avio_wb32(pb, 0x6b6840f2);
00792 avio_wb32(pb, 0x5f244fc5);
00793 avio_wb32(pb, 0xba39a51b);
00794 avio_wb32(pb, 0xcf0323f3);
00795 avio_wb32(pb, 0x0);
00796 return 28;
00797 }
00798
00799 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
00800 {
00801 int64_t pos = avio_tell(pb);
00802 avio_wb32(pb, 0);
00803 avio_wl32(pb, track->tag);
00804 avio_wb32(pb, 0);
00805 avio_wb16(pb, 0);
00806 avio_wb16(pb, 1);
00807
00808 if (track->enc->extradata_size)
00809 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
00810
00811 return updateSize(pb, pos);
00812 }
00813
00814 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
00815 {
00816 AVRational sar;
00817 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
00818 track->enc->sample_aspect_ratio.den, INT_MAX);
00819
00820 avio_wb32(pb, 16);
00821 ffio_wfourcc(pb, "pasp");
00822 avio_wb32(pb, sar.num);
00823 avio_wb32(pb, sar.den);
00824 return 16;
00825 }
00826
00827 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
00828 {
00829 int64_t pos = avio_tell(pb);
00830 char compressor_name[32];
00831
00832 avio_wb32(pb, 0);
00833 avio_wl32(pb, track->tag);
00834 avio_wb32(pb, 0);
00835 avio_wb16(pb, 0);
00836 avio_wb16(pb, 1);
00837
00838 avio_wb16(pb, 0);
00839 avio_wb16(pb, 0);
00840 if (track->mode == MODE_MOV) {
00841 ffio_wfourcc(pb, "FFMP");
00842 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
00843 avio_wb32(pb, 0);
00844 avio_wb32(pb, 0x400);
00845 } else {
00846 avio_wb32(pb, 0x200);
00847 avio_wb32(pb, 0x200);
00848 }
00849 } else {
00850 avio_wb32(pb, 0);
00851 avio_wb32(pb, 0);
00852 avio_wb32(pb, 0);
00853 }
00854 avio_wb16(pb, track->enc->width);
00855 avio_wb16(pb, track->height);
00856 avio_wb32(pb, 0x00480000);
00857 avio_wb32(pb, 0x00480000);
00858 avio_wb32(pb, 0);
00859 avio_wb16(pb, 1);
00860
00861 memset(compressor_name,0,32);
00862
00863 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
00864 av_strlcpy(compressor_name,track->enc->codec->name,32);
00865 avio_w8(pb, strlen(compressor_name));
00866 avio_write(pb, compressor_name, 31);
00867
00868 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
00869 avio_wb16(pb, track->enc->bits_per_coded_sample);
00870 else
00871 avio_wb16(pb, 0x18);
00872 avio_wb16(pb, 0xffff);
00873 if(track->tag == MKTAG('m','p','4','v'))
00874 mov_write_esds_tag(pb, track);
00875 else if(track->enc->codec_id == CODEC_ID_H263)
00876 mov_write_d263_tag(pb);
00877 else if(track->enc->codec_id == CODEC_ID_SVQ3)
00878 mov_write_svq3_tag(pb);
00879 else if(track->enc->codec_id == CODEC_ID_DNXHD)
00880 mov_write_avid_tag(pb, track);
00881 else if(track->enc->codec_id == CODEC_ID_H264) {
00882 mov_write_avcc_tag(pb, track);
00883 if(track->mode == MODE_IPOD)
00884 mov_write_uuid_tag_ipod(pb);
00885 } else if(track->vosLen > 0)
00886 mov_write_glbl_tag(pb, track);
00887
00888 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
00889 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
00890 mov_write_pasp_tag(pb, track);
00891 }
00892
00893 return updateSize(pb, pos);
00894 }
00895
00896 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
00897 {
00898 int64_t pos = avio_tell(pb);
00899 avio_wb32(pb, 0);
00900 ffio_wfourcc(pb, "rtp ");
00901 avio_wb32(pb, 0);
00902 avio_wb16(pb, 0);
00903 avio_wb16(pb, 1);
00904
00905 avio_wb16(pb, 1);
00906 avio_wb16(pb, 1);
00907 avio_wb32(pb, track->max_packet_size);
00908
00909 avio_wb32(pb, 12);
00910 ffio_wfourcc(pb, "tims");
00911 avio_wb32(pb, track->timescale);
00912
00913 return updateSize(pb, pos);
00914 }
00915
00916 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
00917 {
00918 int64_t pos = avio_tell(pb);
00919 avio_wb32(pb, 0);
00920 ffio_wfourcc(pb, "stsd");
00921 avio_wb32(pb, 0);
00922 avio_wb32(pb, 1);
00923 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
00924 mov_write_video_tag(pb, track);
00925 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
00926 mov_write_audio_tag(pb, track);
00927 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
00928 mov_write_subtitle_tag(pb, track);
00929 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
00930 mov_write_rtp_tag(pb, track);
00931 return updateSize(pb, pos);
00932 }
00933
00934 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
00935 {
00936 MOVStts *ctts_entries;
00937 uint32_t entries = 0;
00938 uint32_t atom_size;
00939 int i;
00940
00941 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
00942 ctts_entries[0].count = 1;
00943 ctts_entries[0].duration = track->cluster[0].cts;
00944 for (i=1; i<track->entry; i++) {
00945 if (track->cluster[i].cts == ctts_entries[entries].duration) {
00946 ctts_entries[entries].count++;
00947 } else {
00948 entries++;
00949 ctts_entries[entries].duration = track->cluster[i].cts;
00950 ctts_entries[entries].count = 1;
00951 }
00952 }
00953 entries++;
00954 atom_size = 16 + (entries * 8);
00955 avio_wb32(pb, atom_size);
00956 ffio_wfourcc(pb, "ctts");
00957 avio_wb32(pb, 0);
00958 avio_wb32(pb, entries);
00959 for (i=0; i<entries; i++) {
00960 avio_wb32(pb, ctts_entries[i].count);
00961 avio_wb32(pb, ctts_entries[i].duration);
00962 }
00963 av_free(ctts_entries);
00964 return atom_size;
00965 }
00966
00967
00968 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
00969 {
00970 MOVStts *stts_entries;
00971 uint32_t entries = -1;
00972 uint32_t atom_size;
00973 int i;
00974
00975 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
00976 stts_entries = av_malloc(sizeof(*stts_entries));
00977 stts_entries[0].count = track->sampleCount;
00978 stts_entries[0].duration = 1;
00979 entries = 1;
00980 } else {
00981 stts_entries = av_malloc(track->entry * sizeof(*stts_entries));
00982 for (i=0; i<track->entry; i++) {
00983 int64_t duration = i + 1 == track->entry ?
00984 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts :
00985 track->cluster[i+1].dts - track->cluster[i].dts;
00986 if (i && duration == stts_entries[entries].duration) {
00987 stts_entries[entries].count++;
00988 } else {
00989 entries++;
00990 stts_entries[entries].duration = duration;
00991 stts_entries[entries].count = 1;
00992 }
00993 }
00994 entries++;
00995 }
00996 atom_size = 16 + (entries * 8);
00997 avio_wb32(pb, atom_size);
00998 ffio_wfourcc(pb, "stts");
00999 avio_wb32(pb, 0);
01000 avio_wb32(pb, entries);
01001 for (i=0; i<entries; i++) {
01002 avio_wb32(pb, stts_entries[i].count);
01003 avio_wb32(pb, stts_entries[i].duration);
01004 }
01005 av_free(stts_entries);
01006 return atom_size;
01007 }
01008
01009 static int mov_write_dref_tag(AVIOContext *pb)
01010 {
01011 avio_wb32(pb, 28);
01012 ffio_wfourcc(pb, "dref");
01013 avio_wb32(pb, 0);
01014 avio_wb32(pb, 1);
01015
01016 avio_wb32(pb, 0xc);
01017 ffio_wfourcc(pb, "url ");
01018 avio_wb32(pb, 1);
01019
01020 return 28;
01021 }
01022
01023 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
01024 {
01025 int64_t pos = avio_tell(pb);
01026 avio_wb32(pb, 0);
01027 ffio_wfourcc(pb, "stbl");
01028 mov_write_stsd_tag(pb, track);
01029 mov_write_stts_tag(pb, track);
01030 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
01031 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
01032 track->hasKeyframes && track->hasKeyframes < track->entry)
01033 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
01034 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
01035 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
01036 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
01037 track->flags & MOV_TRACK_CTTS)
01038 mov_write_ctts_tag(pb, track);
01039 mov_write_stsc_tag(pb, track);
01040 mov_write_stsz_tag(pb, track);
01041 mov_write_stco_tag(pb, track);
01042 return updateSize(pb, pos);
01043 }
01044
01045 static int mov_write_dinf_tag(AVIOContext *pb)
01046 {
01047 int64_t pos = avio_tell(pb);
01048 avio_wb32(pb, 0);
01049 ffio_wfourcc(pb, "dinf");
01050 mov_write_dref_tag(pb);
01051 return updateSize(pb, pos);
01052 }
01053
01054 static int mov_write_nmhd_tag(AVIOContext *pb)
01055 {
01056 avio_wb32(pb, 12);
01057 ffio_wfourcc(pb, "nmhd");
01058 avio_wb32(pb, 0);
01059 return 12;
01060 }
01061
01062 static int mov_write_gmhd_tag(AVIOContext *pb)
01063 {
01064 avio_wb32(pb, 0x20);
01065 ffio_wfourcc(pb, "gmhd");
01066 avio_wb32(pb, 0x18);
01067 ffio_wfourcc(pb, "gmin");
01068 avio_wb32(pb, 0);
01069 avio_wb16(pb, 0x40);
01070 avio_wb16(pb, 0x8000);
01071 avio_wb16(pb, 0x8000);
01072 avio_wb16(pb, 0x8000);
01073 avio_wb16(pb, 0);
01074 avio_wb16(pb, 0);
01075 return 0x20;
01076 }
01077
01078 static int mov_write_smhd_tag(AVIOContext *pb)
01079 {
01080 avio_wb32(pb, 16);
01081 ffio_wfourcc(pb, "smhd");
01082 avio_wb32(pb, 0);
01083 avio_wb16(pb, 0);
01084 avio_wb16(pb, 0);
01085 return 16;
01086 }
01087
01088 static int mov_write_vmhd_tag(AVIOContext *pb)
01089 {
01090 avio_wb32(pb, 0x14);
01091 ffio_wfourcc(pb, "vmhd");
01092 avio_wb32(pb, 0x01);
01093 avio_wb64(pb, 0);
01094 return 0x14;
01095 }
01096
01097 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
01098 {
01099 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
01100 int64_t pos = avio_tell(pb);
01101
01102 if (!track) {
01103 hdlr = "dhlr";
01104 hdlr_type = "url ";
01105 descr = "DataHandler";
01106 } else {
01107 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
01108 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01109 hdlr_type = "vide";
01110 descr = "VideoHandler";
01111 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
01112 hdlr_type = "soun";
01113 descr = "SoundHandler";
01114 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
01115 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
01116 else hdlr_type = "text";
01117 descr = "SubtitleHandler";
01118 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
01119 hdlr_type = "hint";
01120 descr = "HintHandler";
01121 }
01122 }
01123
01124 avio_wb32(pb, 0);
01125 ffio_wfourcc(pb, "hdlr");
01126 avio_wb32(pb, 0);
01127 avio_write(pb, hdlr, 4);
01128 ffio_wfourcc(pb, hdlr_type);
01129 avio_wb32(pb ,0);
01130 avio_wb32(pb ,0);
01131 avio_wb32(pb ,0);
01132 if (!track || track->mode == MODE_MOV)
01133 avio_w8(pb, strlen(descr));
01134 avio_write(pb, descr, strlen(descr));
01135 if (track && track->mode != MODE_MOV)
01136 avio_w8(pb, 0);
01137 return updateSize(pb, pos);
01138 }
01139
01140 static int mov_write_hmhd_tag(AVIOContext *pb)
01141 {
01142
01143
01144 avio_wb32(pb, 28);
01145 ffio_wfourcc(pb, "hmhd");
01146 avio_wb32(pb, 0);
01147 avio_wb16(pb, 0);
01148 avio_wb16(pb, 0);
01149 avio_wb32(pb, 0);
01150 avio_wb32(pb, 0);
01151 avio_wb32(pb, 0);
01152 return 28;
01153 }
01154
01155 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
01156 {
01157 int64_t pos = avio_tell(pb);
01158 avio_wb32(pb, 0);
01159 ffio_wfourcc(pb, "minf");
01160 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
01161 mov_write_vmhd_tag(pb);
01162 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
01163 mov_write_smhd_tag(pb);
01164 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
01165 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
01166 else mov_write_nmhd_tag(pb);
01167 } else if (track->tag == MKTAG('r','t','p',' ')) {
01168 mov_write_hmhd_tag(pb);
01169 }
01170 if (track->mode == MODE_MOV)
01171 mov_write_hdlr_tag(pb, NULL);
01172 mov_write_dinf_tag(pb);
01173 mov_write_stbl_tag(pb, track);
01174 return updateSize(pb, pos);
01175 }
01176
01177 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
01178 {
01179 int version = track->trackDuration < INT32_MAX ? 0 : 1;
01180
01181 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32);
01182 ffio_wfourcc(pb, "mdhd");
01183 avio_w8(pb, version);
01184 avio_wb24(pb, 0);
01185 if (version == 1) {
01186 avio_wb64(pb, track->time);
01187 avio_wb64(pb, track->time);
01188 } else {
01189 avio_wb32(pb, track->time);
01190 avio_wb32(pb, track->time);
01191 }
01192 avio_wb32(pb, track->timescale);
01193 (version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration);
01194 avio_wb16(pb, track->language);
01195 avio_wb16(pb, 0);
01196
01197 if(version!=0 && track->mode == MODE_MOV){
01198 av_log(NULL, AV_LOG_ERROR,
01199 "FATAL error, file duration too long for timebase, this file will not be\n"
01200 "playable with quicktime. Choose a different timebase or a different\n"
01201 "container format\n");
01202 }
01203
01204 return 32;
01205 }
01206
01207 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
01208 {
01209 int64_t pos = avio_tell(pb);
01210 avio_wb32(pb, 0);
01211 ffio_wfourcc(pb, "mdia");
01212 mov_write_mdhd_tag(pb, track);
01213 mov_write_hdlr_tag(pb, track);
01214 mov_write_minf_tag(pb, track);
01215 return updateSize(pb, pos);
01216 }
01217
01218 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
01219 {
01220 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
01221 track->timescale, AV_ROUND_UP);
01222 int version = duration < INT32_MAX ? 0 : 1;
01223
01224 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92);
01225 ffio_wfourcc(pb, "tkhd");
01226 avio_w8(pb, version);
01227 avio_wb24(pb, 0xf);
01228 if (version == 1) {
01229 avio_wb64(pb, track->time);
01230 avio_wb64(pb, track->time);
01231 } else {
01232 avio_wb32(pb, track->time);
01233 avio_wb32(pb, track->time);
01234 }
01235 avio_wb32(pb, track->trackID);
01236 avio_wb32(pb, 0);
01237 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
01238
01239 avio_wb32(pb, 0);
01240 avio_wb32(pb, 0);
01241 avio_wb16(pb, 0);
01242 avio_wb16(pb, st ? st->codec->codec_type : 0);
01243
01244 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
01245 avio_wb16(pb, 0x0100);
01246 else
01247 avio_wb16(pb, 0);
01248 avio_wb16(pb, 0);
01249
01250
01251 avio_wb32(pb, 0x00010000);
01252 avio_wb32(pb, 0x0);
01253 avio_wb32(pb, 0x0);
01254 avio_wb32(pb, 0x0);
01255 avio_wb32(pb, 0x00010000);
01256 avio_wb32(pb, 0x0);
01257 avio_wb32(pb, 0x0);
01258 avio_wb32(pb, 0x0);
01259 avio_wb32(pb, 0x40000000);
01260
01261
01262 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
01263 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
01264 if(track->mode == MODE_MOV) {
01265 avio_wb32(pb, track->enc->width << 16);
01266 avio_wb32(pb, track->height << 16);
01267 } else {
01268 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
01269 if(!sample_aspect_ratio || track->height != track->enc->height)
01270 sample_aspect_ratio = 1;
01271 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
01272 avio_wb32(pb, track->height*0x10000);
01273 }
01274 }
01275 else {
01276 avio_wb32(pb, 0);
01277 avio_wb32(pb, 0);
01278 }
01279 return 0x5c;
01280 }
01281
01282 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
01283 {
01284 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
01285 track->enc->sample_aspect_ratio.den);
01286
01287 int64_t pos = avio_tell(pb);
01288
01289 avio_wb32(pb, 0);
01290 ffio_wfourcc(pb, "tapt");
01291
01292 avio_wb32(pb, 20);
01293 ffio_wfourcc(pb, "clef");
01294 avio_wb32(pb, 0);
01295 avio_wb32(pb, width << 16);
01296 avio_wb32(pb, track->enc->height << 16);
01297
01298 avio_wb32(pb, 20);
01299 ffio_wfourcc(pb, "enof");
01300 avio_wb32(pb, 0);
01301 avio_wb32(pb, track->enc->width << 16);
01302 avio_wb32(pb, track->enc->height << 16);
01303
01304 return updateSize(pb, pos);
01305 };
01306
01307
01308 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
01309 {
01310 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE,
01311 track->timescale, AV_ROUND_UP);
01312 int version = duration < INT32_MAX ? 0 : 1;
01313 int entry_size, entry_count, size;
01314 int64_t delay, start_ct = track->cluster[0].cts;
01315 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
01316 track->timescale, AV_ROUND_DOWN);
01317 version |= delay < INT32_MAX ? 0 : 1;
01318
01319 entry_size = (version == 1) ? 20 : 12;
01320 entry_count = 1 + (delay > 0);
01321 size = 24 + entry_count * entry_size;
01322
01323
01324 avio_wb32(pb, size);
01325 ffio_wfourcc(pb, "edts");
01326 avio_wb32(pb, size - 8);
01327 ffio_wfourcc(pb, "elst");
01328 avio_w8(pb, version);
01329 avio_wb24(pb, 0);
01330
01331 avio_wb32(pb, entry_count);
01332 if (delay > 0) {
01333 if (version == 1) {
01334 avio_wb64(pb, delay);
01335 avio_wb64(pb, -1);
01336 } else {
01337 avio_wb32(pb, delay);
01338 avio_wb32(pb, -1);
01339 }
01340 avio_wb32(pb, 0x00010000);
01341 }
01342
01343
01344 if (version == 1) {
01345 avio_wb64(pb, duration);
01346 avio_wb64(pb, start_ct);
01347 } else {
01348 avio_wb32(pb, duration);
01349 avio_wb32(pb, start_ct);
01350 }
01351 avio_wb32(pb, 0x00010000);
01352 return size;
01353 }
01354
01355 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
01356 {
01357 avio_wb32(pb, 20);
01358 ffio_wfourcc(pb, "tref");
01359 avio_wb32(pb, 12);
01360 avio_wl32(pb, track->tref_tag);
01361 avio_wb32(pb, track->tref_id);
01362 return 20;
01363 }
01364
01365
01366 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
01367 {
01368 avio_wb32(pb, 0x34);
01369 ffio_wfourcc(pb, "uuid");
01370 ffio_wfourcc(pb, "USMT");
01371 avio_wb32(pb, 0x21d24fce);
01372 avio_wb32(pb, 0xbb88695c);
01373 avio_wb32(pb, 0xfac9c740);
01374 avio_wb32(pb, 0x1c);
01375 ffio_wfourcc(pb, "MTDT");
01376 avio_wb32(pb, 0x00010012);
01377 avio_wb32(pb, 0x0a);
01378 avio_wb32(pb, 0x55c40000);
01379 avio_wb32(pb, 0x1);
01380 avio_wb32(pb, 0x0);
01381 return 0x34;
01382 }
01383
01384 static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
01385 {
01386 char buf[1000] = "";
01387 int len;
01388
01389 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
01390 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
01391 len = strlen(buf);
01392
01393 avio_wb32(pb, len + 24);
01394 ffio_wfourcc(pb, "udta");
01395 avio_wb32(pb, len + 16);
01396 ffio_wfourcc(pb, "hnti");
01397 avio_wb32(pb, len + 8);
01398 ffio_wfourcc(pb, "sdp ");
01399 avio_write(pb, buf, len);
01400 return len + 24;
01401 }
01402
01403 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov, MOVTrack *track, AVStream *st)
01404 {
01405 int64_t pos = avio_tell(pb);
01406 avio_wb32(pb, 0);
01407 ffio_wfourcc(pb, "trak");
01408 mov_write_tkhd_tag(pb, track, st);
01409 if(!mov->fragments)
01410 mov_write_edts_tag(pb, track);
01411 if (track->tref_tag)
01412 mov_write_tref_tag(pb, track);
01413 mov_write_mdia_tag(pb, track);
01414 if (track->mode == MODE_PSP)
01415 mov_write_uuid_tag_psp(pb,track);
01416 if (track->tag == MKTAG('r','t','p',' '))
01417 mov_write_udta_sdp(pb, track->rtp_ctx, track->trackID);
01418 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
01419 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
01420 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
01421 mov_write_tapt_tag(pb, track);
01422 };
01423 return updateSize(pb, pos);
01424 }
01425
01426 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
01427 {
01428 int64_t pos = avio_tell(pb);
01429 int flags=1;
01430
01431 avio_wb32(pb, 0);
01432 ffio_wfourcc(pb, "tfhd");
01433 avio_w8(pb, 0);
01434 avio_wb24(pb, flags);
01435 avio_wb32(pb, track->trackID);
01436
01437 track->base_data_offset_pos= avio_tell(pb);
01438 if (flags & 0x01) avio_wb64(pb, 0);
01439
01440 return updateSize(pb, pos);
01441 }
01442
01443 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
01444 {
01445 int64_t pos = avio_tell(pb);
01446 int sample_count= track->entry - track->cluster_write_index;
01447 int tr_flags=0;
01448 int i;
01449
01450 tr_flags |= 0xF00;
01451
01452 avio_wb32(pb, 0);
01453 ffio_wfourcc(pb, "trun");
01454 avio_w8(pb, 0);
01455 avio_wb24(pb, tr_flags);
01456 avio_wb32(pb, sample_count);
01457 if(tr_flags&1) avio_wb32(pb, 0);
01458
01459 for(i=track->cluster_write_index; i<track->entry; i++){
01460 int64_t duration = i + 1 == track->entry ?
01461 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts :
01462 track->cluster[i+1].dts - track->cluster[i].dts;
01463
01464 if(tr_flags&0x100) avio_wb32(pb, duration);
01465 if(tr_flags&0x200) avio_wb32(pb, track->cluster[i].size);
01466 if(tr_flags&0x400) avio_wb32(pb, (track->cluster[i].flags&MOV_SYNC_SAMPLE) ? 0x02000000 : 0x01010000);
01467 if(tr_flags&0x800) avio_wb32(pb, track->cluster[i].cts);
01468 }
01469
01470 return updateSize(pb, pos);
01471 }
01472
01473 static int mov_write_traf_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
01474 {
01475 int64_t pos = avio_tell(pb);
01476 avio_wb32(pb, 0);
01477 ffio_wfourcc(pb, "traf");
01478 mov_write_tfhd_tag(pb, track, st);
01479 mov_write_trun_tag(pb, track);
01480 return updateSize(pb, pos);
01481 }
01482
01483 #if 0
01484
01485 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
01486 {
01487 avio_wb32(pb, 0x15);
01488 ffio_wfourcc(pb, "iods");
01489 avio_wb32(pb, 0);
01490 avio_wb16(pb, 0x1007);
01491 avio_w8(pb, 0);
01492 avio_wb16(pb, 0x4fff);
01493 avio_wb16(pb, 0xfffe);
01494 avio_wb16(pb, 0x01ff);
01495 return 0x15;
01496 }
01497 #endif
01498
01499 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
01500 {
01501 int maxTrackID = 1, i;
01502 int64_t maxTrackLenTemp, maxTrackLen = 0;
01503 int version;
01504
01505 for (i=0; i<mov->nb_streams; i++) {
01506 if(mov->tracks[i].entry > 0) {
01507 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration,
01508 MOV_TIMESCALE,
01509 mov->tracks[i].timescale,
01510 AV_ROUND_UP);
01511 if(maxTrackLen < maxTrackLenTemp)
01512 maxTrackLen = maxTrackLenTemp;
01513 if(maxTrackID < mov->tracks[i].trackID)
01514 maxTrackID = mov->tracks[i].trackID;
01515 }
01516 }
01517
01518 version = maxTrackLen < UINT32_MAX ? 0 : 1;
01519 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108);
01520 ffio_wfourcc(pb, "mvhd");
01521 avio_w8(pb, version);
01522 avio_wb24(pb, 0);
01523 if (version == 1) {
01524 avio_wb64(pb, mov->time);
01525 avio_wb64(pb, mov->time);
01526 } else {
01527 avio_wb32(pb, mov->time);
01528 avio_wb32(pb, mov->time);
01529 }
01530 avio_wb32(pb, MOV_TIMESCALE);
01531 (version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen);
01532
01533 avio_wb32(pb, 0x00010000);
01534 avio_wb16(pb, 0x0100);
01535 avio_wb16(pb, 0);
01536 avio_wb32(pb, 0);
01537 avio_wb32(pb, 0);
01538
01539
01540 avio_wb32(pb, 0x00010000);
01541 avio_wb32(pb, 0x0);
01542 avio_wb32(pb, 0x0);
01543 avio_wb32(pb, 0x0);
01544 avio_wb32(pb, 0x00010000);
01545 avio_wb32(pb, 0x0);
01546 avio_wb32(pb, 0x0);
01547 avio_wb32(pb, 0x0);
01548 avio_wb32(pb, 0x40000000);
01549
01550 avio_wb32(pb, 0);
01551 avio_wb32(pb, 0);
01552 avio_wb32(pb, 0);
01553 avio_wb32(pb, 0);
01554 avio_wb32(pb, 0);
01555 avio_wb32(pb, 0);
01556 avio_wb32(pb, maxTrackID+1);
01557 return 0x6c;
01558 }
01559
01560 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
01561 AVFormatContext *s)
01562 {
01563 avio_wb32(pb, 33);
01564 ffio_wfourcc(pb, "hdlr");
01565 avio_wb32(pb, 0);
01566 avio_wb32(pb, 0);
01567 ffio_wfourcc(pb, "mdir");
01568 ffio_wfourcc(pb, "appl");
01569 avio_wb32(pb, 0);
01570 avio_wb32(pb, 0);
01571 avio_w8(pb, 0);
01572 return 33;
01573 }
01574
01575
01576 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
01577 {
01578 if(long_style){
01579 int size = 16 + strlen(data);
01580 avio_wb32(pb, size);
01581 ffio_wfourcc(pb, "data");
01582 avio_wb32(pb, 1);
01583 avio_wb32(pb, 0);
01584 avio_write(pb, data, strlen(data));
01585 return size;
01586 }else{
01587 if (!lang)
01588 lang = ff_mov_iso639_to_lang("und", 1);
01589 avio_wb16(pb, strlen(data));
01590 avio_wb16(pb, lang);
01591 avio_write(pb, data, strlen(data));
01592 return strlen(data) + 4;
01593 }
01594 }
01595
01596 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
01597 int size = 0;
01598 if (value && value[0]) {
01599 int64_t pos = avio_tell(pb);
01600 avio_wb32(pb, 0);
01601 ffio_wfourcc(pb, name);
01602 mov_write_string_data_tag(pb, value, lang, long_style);
01603 size= updateSize(pb, pos);
01604 }
01605 return size;
01606 }
01607
01608 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
01609 const char *name, const char *tag,
01610 int long_style)
01611 {
01612 int l, lang = 0, len, len2;
01613 AVDictionaryEntry *t, *t2 = NULL;
01614 char tag2[16];
01615
01616 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
01617 return 0;
01618
01619 len = strlen(t->key);
01620 snprintf(tag2, sizeof(tag2), "%s-", tag);
01621 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
01622 len2 = strlen(t2->key);
01623 if (len2 == len+4 && !strcmp(t->value, t2->value)
01624 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
01625 lang = l;
01626 break;
01627 }
01628 }
01629 return mov_write_string_tag(pb, name, t->value, lang, long_style);
01630 }
01631
01632
01633 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
01634 AVFormatContext *s)
01635 {
01636 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
01637 int size = 0, track = t ? atoi(t->value) : 0;
01638 if (track) {
01639 avio_wb32(pb, 32);
01640 ffio_wfourcc(pb, "trkn");
01641 avio_wb32(pb, 24);
01642 ffio_wfourcc(pb, "data");
01643 avio_wb32(pb, 0);
01644 avio_wb32(pb, 0);
01645 avio_wb16(pb, 0);
01646 avio_wb16(pb, track);
01647 avio_wb16(pb, 0);
01648 avio_wb16(pb, 0);
01649 size = 32;
01650 }
01651 return size;
01652 }
01653
01654
01655 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
01656 AVFormatContext *s)
01657 {
01658 int64_t pos = avio_tell(pb);
01659 avio_wb32(pb, 0);
01660 ffio_wfourcc(pb, "ilst");
01661 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
01662 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
01663 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
01664 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
01665 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
01666 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
01667 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
01668 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
01669 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
01670 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
01671 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
01672 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
01673 mov_write_string_metadata(s, pb, "desc", "description",1);
01674 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
01675 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
01676 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
01677 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
01678 mov_write_trkn_tag(pb, mov, s);
01679 return updateSize(pb, pos);
01680 }
01681
01682
01683 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
01684 AVFormatContext *s)
01685 {
01686 int size = 0;
01687 int64_t pos = avio_tell(pb);
01688 avio_wb32(pb, 0);
01689 ffio_wfourcc(pb, "meta");
01690 avio_wb32(pb, 0);
01691 mov_write_itunes_hdlr_tag(pb, mov, s);
01692 mov_write_ilst_tag(pb, mov, s);
01693 size = updateSize(pb, pos);
01694 return size;
01695 }
01696
01697 static int utf8len(const uint8_t *b)
01698 {
01699 int len=0;
01700 int val;
01701 while(*b){
01702 GET_UTF8(val, *b++, return -1;)
01703 len++;
01704 }
01705 return len;
01706 }
01707
01708 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
01709 {
01710 int val;
01711 while(*b){
01712 GET_UTF8(val, *b++, return -1;)
01713 avio_wb16(pb, val);
01714 }
01715 avio_wb16(pb, 0x00);
01716 return 0;
01717 }
01718
01719 static uint16_t language_code(const char *str)
01720 {
01721 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
01722 }
01723
01724 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
01725 const char *tag, const char *str)
01726 {
01727 int64_t pos = avio_tell(pb);
01728 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
01729 if (!t || !utf8len(t->value))
01730 return 0;
01731 avio_wb32(pb, 0);
01732 ffio_wfourcc(pb, tag);
01733 avio_wb32(pb, 0);
01734 if (!strcmp(tag, "yrrc"))
01735 avio_wb16(pb, atoi(t->value));
01736 else {
01737 avio_wb16(pb, language_code("eng"));
01738 avio_write(pb, t->value, strlen(t->value)+1);
01739 if (!strcmp(tag, "albm") &&
01740 (t = av_dict_get(s->metadata, "track", NULL, 0)))
01741 avio_w8(pb, atoi(t->value));
01742 }
01743 return updateSize(pb, pos);
01744 }
01745
01746 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
01747 {
01748 int64_t pos = avio_tell(pb);
01749 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
01750
01751 avio_wb32(pb, 0);
01752 ffio_wfourcc(pb, "chpl");
01753 avio_wb32(pb, 0x01000000);
01754 avio_wb32(pb, 0);
01755 avio_w8(pb, nb_chapters);
01756
01757 for (i = 0; i < nb_chapters; i++) {
01758 AVChapter *c = s->chapters[i];
01759 AVDictionaryEntry *t;
01760 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
01761
01762 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
01763 int len = FFMIN(strlen(t->value), 255);
01764 avio_w8(pb, len);
01765 avio_write(pb, t->value, len);
01766 } else
01767 avio_w8(pb, 0);
01768 }
01769 return updateSize(pb, pos);
01770 }
01771
01772 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
01773 AVFormatContext *s)
01774 {
01775 AVIOContext *pb_buf;
01776 int i, ret, size;
01777 uint8_t *buf;
01778
01779 for (i = 0; i < s->nb_streams; i++)
01780 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
01781 return 0;
01782 }
01783
01784 ret = avio_open_dyn_buf(&pb_buf);
01785 if(ret < 0)
01786 return ret;
01787
01788 if (mov->mode & MODE_3GP) {
01789 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
01790 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
01791 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
01792 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
01793 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
01794 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
01795 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
01796 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
01797 } else if (mov->mode == MODE_MOV) {
01798 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
01799 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
01800 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
01801 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
01802 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
01803 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
01804 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
01805 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
01806 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
01807 } else {
01808
01809 mov_write_meta_tag(pb_buf, mov, s);
01810 }
01811
01812 if (s->nb_chapters)
01813 mov_write_chpl_tag(pb_buf, s);
01814
01815 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
01816 avio_wb32(pb, size+8);
01817 ffio_wfourcc(pb, "udta");
01818 avio_write(pb, buf, size);
01819 }
01820 av_free(buf);
01821
01822 return 0;
01823 }
01824
01825 static void mov_write_psp_udta_tag(AVIOContext *pb,
01826 const char *str, const char *lang, int type)
01827 {
01828 int len = utf8len(str)+1;
01829 if(len<=0)
01830 return;
01831 avio_wb16(pb, len*2+10);
01832 avio_wb32(pb, type);
01833 avio_wb16(pb, language_code(lang));
01834 avio_wb16(pb, 0x01);
01835 ascii_to_wc(pb, str);
01836 }
01837
01838 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
01839 {
01840 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
01841 int64_t pos, pos2;
01842
01843 if (title) {
01844 pos = avio_tell(pb);
01845 avio_wb32(pb, 0);
01846 ffio_wfourcc(pb, "uuid");
01847 ffio_wfourcc(pb, "USMT");
01848 avio_wb32(pb, 0x21d24fce);
01849 avio_wb32(pb, 0xbb88695c);
01850 avio_wb32(pb, 0xfac9c740);
01851
01852 pos2 = avio_tell(pb);
01853 avio_wb32(pb, 0);
01854 ffio_wfourcc(pb, "MTDT");
01855 avio_wb16(pb, 4);
01856
01857
01858 avio_wb16(pb, 0x0C);
01859 avio_wb32(pb, 0x0B);
01860 avio_wb16(pb, language_code("und"));
01861 avio_wb16(pb, 0x0);
01862 avio_wb16(pb, 0x021C);
01863
01864 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
01865 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
01866
01867 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
01868
01869 updateSize(pb, pos2);
01870 return updateSize(pb, pos);
01871 }
01872
01873 return 0;
01874 }
01875
01876 static void build_chunks(MOVTrack *trk)
01877 {
01878 int i;
01879 MOVIentry *chunk= &trk->cluster[0];
01880 uint64_t chunkSize = chunk->size;
01881 chunk->chunkNum= 1;
01882 trk->chunkCount= 1;
01883 for(i=1; i<trk->entry; i++){
01884 if(chunk->pos + chunkSize == trk->cluster[i].pos){
01885 chunkSize += trk->cluster[i].size;
01886 chunk->samplesInChunk += trk->cluster[i].entries;
01887 }else{
01888 trk->cluster[i].chunkNum = chunk->chunkNum+1;
01889 chunk=&trk->cluster[i];
01890 chunkSize = chunk->size;
01891 trk->chunkCount++;
01892 }
01893 }
01894 }
01895
01896 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
01897 {
01898 int64_t pos = avio_tell(pb);
01899 avio_wb32(pb, 0);
01900 ffio_wfourcc(pb, "trex");
01901
01902 avio_w8(pb, 0);
01903 avio_wb24(pb, 0);
01904 avio_wb32(pb, track->trackID);
01905 avio_wb32(pb, 1);
01906 avio_wb32(pb, 1);
01907 avio_wb32(pb, 1);
01908 avio_wb32(pb, 1<<16);
01909 return updateSize(pb, pos);
01910 }
01911
01912 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov,
01913 AVFormatContext *s)
01914 {
01915 int i;
01916 int64_t pos = avio_tell(pb);
01917 avio_wb32(pb, 0);
01918 ffio_wfourcc(pb, "mvex");
01919
01920 for (i=0; i<mov->nb_streams; i++) {
01921 if(mov->tracks[i].entry > 0) {
01922 mov_write_trex_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
01923 }
01924 }
01925
01926 return updateSize(pb, pos);
01927 }
01928
01929 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
01930 AVFormatContext *s)
01931 {
01932 int i;
01933 int64_t pos = avio_tell(pb);
01934 avio_wb32(pb, 0);
01935 ffio_wfourcc(pb, "moov");
01936
01937 for (i=0; i<mov->nb_streams; i++) {
01938 if(mov->tracks[i].entry <= 0) continue;
01939
01940 mov->tracks[i].time = mov->time;
01941 mov->tracks[i].trackID = i+1;
01942
01943 build_chunks(&mov->tracks[i]);
01944 }
01945
01946 if (mov->chapter_track)
01947 for (i=0; i<s->nb_streams; i++) {
01948 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
01949 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID;
01950 }
01951 for (i = 0; i < mov->nb_streams; i++) {
01952 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
01953 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
01954 mov->tracks[i].tref_id =
01955 mov->tracks[mov->tracks[i].src_track].trackID;
01956 }
01957 }
01958
01959 mov_write_mvhd_tag(pb, mov);
01960
01961 for (i=0; i<mov->nb_streams; i++) {
01962 if(mov->tracks[i].entry > 0) {
01963 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
01964 }
01965 }
01966
01967 if(mov->fragments)
01968 mov_write_mvex_tag(pb, mov, s);
01969
01970 if (mov->mode == MODE_PSP)
01971 mov_write_uuidusmt_tag(pb, s);
01972 else
01973 mov_write_udta_tag(pb, mov, s);
01974
01975 return updateSize(pb, pos);
01976 }
01977
01978 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
01979 {
01980 int i;
01981 int64_t pos = avio_tell(pb);
01982 avio_wb32(pb, 0);
01983 ffio_wfourcc(pb, "mfhd");
01984 avio_wb32(pb, 0);
01985 avio_wb32(pb, mov->frag_seq_num++);
01986
01987 return updateSize(pb, pos);
01988 }
01989
01990 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov,
01991 AVFormatContext *s)
01992 {
01993 int i;
01994 int64_t pos = avio_tell(pb);
01995 avio_wb32(pb, 0);
01996 ffio_wfourcc(pb, "moof");
01997
01998 mov_write_mfhd_tag(pb, mov);
01999 for (i=0; i<mov->nb_streams; i++) {
02000 if(mov->tracks[i].entry > 0) {
02001 mov_write_traf_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
02002 }
02003 }
02004
02005 return updateSize(pb, pos);
02006 }
02007
02008 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
02009 {
02010 avio_wb32(pb, 8);
02011 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
02012
02013 mov->mdat_pos = avio_tell(pb);
02014 avio_wb32(pb, 0);
02015 ffio_wfourcc(pb, "mdat");
02016 return 0;
02017 }
02018
02019
02020 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
02021 {
02022 MOVMuxContext *mov = s->priv_data;
02023 int64_t pos = avio_tell(pb);
02024 int has_h264 = 0, has_video = 0;
02025 int minor = 0x200;
02026 int i;
02027
02028 for (i = 0; i < s->nb_streams; i++) {
02029 AVStream *st = s->streams[i];
02030 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
02031 has_video = 1;
02032 if (st->codec->codec_id == CODEC_ID_H264)
02033 has_h264 = 1;
02034 }
02035
02036 avio_wb32(pb, 0);
02037 ffio_wfourcc(pb, "ftyp");
02038
02039 if (mov->mode == MODE_3GP) {
02040 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
02041 minor = has_h264 ? 0x100 : 0x200;
02042 } else if (mov->mode & MODE_3G2) {
02043 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
02044 minor = has_h264 ? 0x20000 : 0x10000;
02045 }else if (mov->mode == MODE_PSP)
02046 ffio_wfourcc(pb, "MSNV");
02047 else if (mov->mode == MODE_MP4)
02048 ffio_wfourcc(pb, "isom");
02049 else if (mov->mode == MODE_IPOD)
02050 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
02051 else
02052 ffio_wfourcc(pb, "qt ");
02053
02054 avio_wb32(pb, minor);
02055
02056 if(mov->mode == MODE_MOV)
02057 ffio_wfourcc(pb, "qt ");
02058 else{
02059 ffio_wfourcc(pb, "isom");
02060 ffio_wfourcc(pb, "iso2");
02061 if(has_h264)
02062 ffio_wfourcc(pb, "avc1");
02063 }
02064
02065 if (mov->mode == MODE_3GP)
02066 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
02067 else if (mov->mode & MODE_3G2)
02068 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
02069 else if (mov->mode == MODE_PSP)
02070 ffio_wfourcc(pb, "MSNV");
02071 else if (mov->mode == MODE_MP4)
02072 ffio_wfourcc(pb, "mp41");
02073 return updateSize(pb, pos);
02074 }
02075
02076 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
02077 {
02078 AVCodecContext *VideoCodec = s->streams[0]->codec;
02079 AVCodecContext *AudioCodec = s->streams[1]->codec;
02080 int AudioRate = AudioCodec->sample_rate;
02081 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
02082 int audio_kbitrate= AudioCodec->bit_rate / 1000;
02083 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
02084
02085 avio_wb32(pb, 0x94);
02086 ffio_wfourcc(pb, "uuid");
02087 ffio_wfourcc(pb, "PROF");
02088
02089 avio_wb32(pb, 0x21d24fce);
02090 avio_wb32(pb, 0xbb88695c);
02091 avio_wb32(pb, 0xfac9c740);
02092
02093 avio_wb32(pb, 0x0);
02094 avio_wb32(pb, 0x3);
02095
02096 avio_wb32(pb, 0x14);
02097 ffio_wfourcc(pb, "FPRF");
02098 avio_wb32(pb, 0x0);
02099 avio_wb32(pb, 0x0);
02100 avio_wb32(pb, 0x0);
02101
02102 avio_wb32(pb, 0x2c);
02103 ffio_wfourcc(pb, "APRF");
02104 avio_wb32(pb, 0x0);
02105 avio_wb32(pb, 0x2);
02106 ffio_wfourcc(pb, "mp4a");
02107 avio_wb32(pb, 0x20f);
02108 avio_wb32(pb, 0x0);
02109 avio_wb32(pb, audio_kbitrate);
02110 avio_wb32(pb, audio_kbitrate);
02111 avio_wb32(pb, AudioRate);
02112 avio_wb32(pb, AudioCodec->channels);
02113
02114 avio_wb32(pb, 0x34);
02115 ffio_wfourcc(pb, "VPRF");
02116 avio_wb32(pb, 0x0);
02117 avio_wb32(pb, 0x1);
02118 if (VideoCodec->codec_id == CODEC_ID_H264) {
02119 ffio_wfourcc(pb, "avc1");
02120 avio_wb16(pb, 0x014D);
02121 avio_wb16(pb, 0x0015);
02122 } else {
02123 ffio_wfourcc(pb, "mp4v");
02124 avio_wb16(pb, 0x0000);
02125 avio_wb16(pb, 0x0103);
02126 }
02127 avio_wb32(pb, 0x0);
02128 avio_wb32(pb, video_kbitrate);
02129 avio_wb32(pb, video_kbitrate);
02130 avio_wb32(pb, FrameRate);
02131 avio_wb32(pb, FrameRate);
02132 avio_wb16(pb, VideoCodec->width);
02133 avio_wb16(pb, VideoCodec->height);
02134 avio_wb32(pb, 0x010001);
02135 }
02136
02137 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
02138 {
02139 uint32_t c = -1;
02140 int i, closed_gop = 0;
02141
02142 for (i = 0; i < pkt->size - 4; i++) {
02143 c = (c<<8) + pkt->data[i];
02144 if (c == 0x1b8) {
02145 closed_gop = pkt->data[i+4]>>6 & 0x01;
02146 } else if (c == 0x100) {
02147 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
02148 if (!temp_ref || closed_gop)
02149 *flags = MOV_SYNC_SAMPLE;
02150 else
02151 *flags = MOV_PARTIAL_SYNC_SAMPLE;
02152 break;
02153 }
02154 }
02155 return 0;
02156 }
02157
02158 static int flush_cluster_buffer(AVFormatContext *s){
02159 MOVMuxContext *mov = s->priv_data;
02160 int i, j;
02161 int has_data=0;
02162
02163 for (i=0; i<mov->nb_streams; i++){
02164 MOVTrack *track= &mov->tracks[i];
02165 if(track->entry != track->cluster_write_index)
02166 has_data=1;
02167 }
02168
02169 if(mov->frag_seq_num==0){
02170 update_first_fragment(s);
02171 mov->frag_seq_num++;
02172 }else if(has_data) {
02173 mov_write_moof_tag(s->pb, mov, s);
02174 mov_write_mdat_tag(s->pb, mov);
02175 for (i=0; i<mov->nb_streams; i++){
02176 MOVTrack *track= &mov->tracks[i];
02177 if(track->entry > 0) {
02178 int64_t pos= avio_tell(s->pb);
02179 avio_seek(s->pb, track->base_data_offset_pos, SEEK_SET);
02180 avio_wb64(s->pb, pos);
02181 avio_seek(s->pb, pos, SEEK_SET);
02182 for(j=track->cluster_write_index; j<track->entry; j++){
02183 avio_write(s->pb, track->cluster[j].data, track->cluster[j].size);
02184 av_freep(&track->cluster[j].data);
02185 }
02186 }
02187 }
02188 updateSize(s->pb, mov->mdat_pos);
02189 }
02190 mov->mdat_size = 0;
02191 for (i=0; i<mov->nb_streams; i++) {
02192 MOVTrack *track= &mov->tracks[i];
02193 track->cluster_write_index= track->entry;
02194 }
02195 avio_flush(s->pb);
02196
02197 return 0;
02198 }
02199
02200 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
02201 {
02202 MOVMuxContext *mov = s->priv_data;
02203 AVIOContext *pb = s->pb;
02204 MOVTrack *trk = &mov->tracks[pkt->stream_index];
02205 AVStream *st = s->streams[pkt->stream_index];
02206 AVCodecContext *enc = trk->enc;
02207 unsigned int samplesInChunk = 0;
02208 int size= pkt->size;
02209
02210 if (!s->pb->seekable) return 0;
02211 if (!size) return 0;
02212
02213 if (mov->fragments && trk->entry > trk->cluster_write_index &&
02214 ( mov->max_fragment_duration && av_rescale_q(pkt->dts - trk->cluster[ trk->cluster_write_index ].dts, st->time_base, AV_TIME_BASE_Q) >= mov->max_fragment_duration
02215 || mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size)
02216 ){
02217 flush_cluster_buffer(s);
02218 }
02219
02220 if (enc->codec_id == CODEC_ID_AMR_NB) {
02221
02222 static uint16_t packed_size[16] =
02223 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
02224 int len = 0;
02225
02226 while (len < size && samplesInChunk < 100) {
02227 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
02228 samplesInChunk++;
02229 }
02230 if(samplesInChunk > 1){
02231 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
02232 return -1;
02233 }
02234 } else if (trk->sampleSize)
02235 samplesInChunk = size/trk->sampleSize;
02236 else
02237 samplesInChunk = 1;
02238
02239
02240 if (trk->vosLen == 0 && enc->extradata_size > 0) {
02241 trk->vosLen = enc->extradata_size;
02242 trk->vosData = av_malloc(trk->vosLen);
02243 memcpy(trk->vosData, enc->extradata, trk->vosLen);
02244 }
02245
02246 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
02247 trk->cluster = av_realloc_f(trk->cluster, sizeof(*trk->cluster), (trk->entry + MOV_INDEX_CLUSTER_SIZE));
02248 if (!trk->cluster)
02249 return -1;
02250 }
02251
02252 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
02253
02254
02255 if(mov->frag_seq_num>0){
02256 uint8_t *buf=NULL;
02257 size= pkt->size;
02258 if(ff_avc_parse_nal_units_buf(pkt->data, &buf, &size) < 0){
02259 av_log(s, AV_LOG_ERROR, "malformated H264 bitstream\n");
02260 return -1;
02261 }
02262 trk->cluster[trk->entry].data= buf;
02263 }else
02264 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
02265 } else if (enc->codec_id == CODEC_ID_AAC && pkt->size > 2 &&
02266 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
02267 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
02268 return -1;
02269 } else if (mov->frag_seq_num>0){
02270 trk->cluster[trk->entry].data = av_malloc(size);
02271 if (!trk->cluster[trk->entry].data)
02272 return AVERROR(ENOMEM);
02273 memcpy(trk->cluster[trk->entry].data, pkt->data, size);
02274 } else {
02275 avio_write(pb, pkt->data, size);
02276 }
02277
02278 if ((enc->codec_id == CODEC_ID_DNXHD ||
02279 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) {
02280
02281 trk->vosLen = size;
02282 trk->vosData = av_malloc(size);
02283 if (!trk->vosData)
02284 return AVERROR(ENOMEM);
02285 memcpy(trk->vosData, pkt->data, size);
02286 }
02287
02288 trk->cluster[trk->entry].pos = avio_tell(pb) - (mov->frag_seq_num==0 ? size : 0);
02289 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
02290 trk->cluster[trk->entry].chunkNum = 0;
02291 trk->cluster[trk->entry].size = size;
02292 trk->cluster[trk->entry].entries = samplesInChunk;
02293 trk->cluster[trk->entry].dts = pkt->dts;
02294 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
02295
02296 if (pkt->pts == AV_NOPTS_VALUE) {
02297 av_log(s, AV_LOG_WARNING, "pts has no value\n");
02298 pkt->pts = pkt->dts;
02299 }
02300 if (pkt->dts != pkt->pts)
02301 trk->flags |= MOV_TRACK_CTTS;
02302 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
02303 trk->cluster[trk->entry].flags = 0;
02304 if (pkt->flags & AV_PKT_FLAG_KEY) {
02305 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
02306 trk->entry > 0) {
02307 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
02308 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
02309 trk->flags |= MOV_TRACK_STPS;
02310 } else {
02311 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
02312 }
02313 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
02314 trk->hasKeyframes++;
02315 }
02316 trk->entry++;
02317 trk->sampleCount += samplesInChunk;
02318 mov->mdat_size += size;
02319
02320 avio_flush(pb);
02321
02322 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
02323 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry);
02324 return 0;
02325 }
02326
02327
02328
02329 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
02330 {
02331 MOVMuxContext *mov = s->priv_data;
02332 MOVTrack *track = &mov->tracks[tracknum];
02333 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
02334 int i, len;
02335
02336 track->mode = mov->mode;
02337 track->tag = MKTAG('t','e','x','t');
02338 track->timescale = MOV_TIMESCALE;
02339 track->enc = avcodec_alloc_context3(NULL);
02340 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
02341
02342 for (i = 0; i < s->nb_chapters; i++) {
02343 AVChapter *c = s->chapters[i];
02344 AVDictionaryEntry *t;
02345
02346 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
02347 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
02348 pkt.duration = end - pkt.dts;
02349
02350 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
02351 len = strlen(t->value);
02352 pkt.size = len+2;
02353 pkt.data = av_malloc(pkt.size);
02354 AV_WB16(pkt.data, len);
02355 memcpy(pkt.data+2, t->value, len);
02356 ff_mov_write_packet(s, &pkt);
02357 av_freep(&pkt.data);
02358 }
02359 }
02360 }
02361
02362 static int mov_write_header(AVFormatContext *s)
02363 {
02364 AVIOContext *pb = s->pb;
02365 MOVMuxContext *mov = s->priv_data;
02366 AVDictionaryEntry *t;
02367 int i, hint_track = 0;
02368
02369 if (!s->pb->seekable) {
02370 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
02371 return -1;
02372 }
02373
02374
02375 mov->mode = MODE_MP4;
02376
02377 if(mov->max_fragment_duration || mov->max_fragment_size){
02378 mov->fragments= 1;
02379 }
02380
02381 if (s->oformat != NULL) {
02382 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
02383 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
02384 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
02385 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
02386 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
02387
02388 mov_write_ftyp_tag(pb,s);
02389 if (mov->mode == MODE_PSP) {
02390 if (s->nb_streams != 2) {
02391 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
02392 return -1;
02393 }
02394 mov_write_uuidprof_tag(pb,s);
02395 }
02396 }
02397
02398 mov->nb_streams = s->nb_streams;
02399 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
02400 mov->chapter_track = mov->nb_streams++;
02401
02402 #if FF_API_FLAG_RTP_HINT
02403 if (s->flags & AVFMT_FLAG_RTP_HINT) {
02404 av_log(s, AV_LOG_WARNING, "The RTP_HINT flag is deprecated, enable it "
02405 "via the -movflags rtphint muxer option "
02406 "instead.\n");
02407 mov->flags |= FF_MOV_FLAG_RTP_HINT;
02408 }
02409 #endif
02410 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
02411
02412 hint_track = mov->nb_streams;
02413 for (i = 0; i < s->nb_streams; i++) {
02414 AVStream *st = s->streams[i];
02415 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
02416 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
02417 mov->nb_streams++;
02418 }
02419 }
02420 }
02421
02422 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
02423 if (!mov->tracks)
02424 return AVERROR(ENOMEM);
02425
02426 for(i=0; i<s->nb_streams; i++){
02427 AVStream *st= s->streams[i];
02428 MOVTrack *track= &mov->tracks[i];
02429 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
02430
02431 track->enc = st->codec;
02432 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
02433 if (track->language < 0)
02434 track->language = 0;
02435 track->mode = mov->mode;
02436 track->tag = mov_find_codec_tag(s, track);
02437 if (!track->tag) {
02438 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
02439 "codec not currently supported in container\n", i);
02440 goto error;
02441 }
02442
02443
02444 track->hint_track = -1;
02445 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
02446 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
02447 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
02448 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
02449 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
02450 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
02451 goto error;
02452 }
02453 track->height = track->tag>>24 == 'n' ? 486 : 576;
02454 }
02455 track->timescale = st->codec->time_base.den;
02456 if (track->mode == MODE_MOV && track->timescale > 100000)
02457 av_log(s, AV_LOG_WARNING,
02458 "WARNING codec timebase is very high. If duration is too long,\n"
02459 "file may not be playable by quicktime. Specify a shorter timebase\n"
02460 "or choose different container.\n");
02461 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
02462 track->timescale = st->codec->sample_rate;
02463
02464 if (av_get_bits_per_sample(st->codec->codec_id)) {
02465 if (!st->codec->block_align) {
02466 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
02467 goto error;
02468 }
02469 track->sampleSize = st->codec->block_align;
02470 }
02471
02472 if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
02473 if (!st->codec->frame_size) {
02474 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
02475 goto error;
02476 }
02477 track->audio_vbr = 1;
02478 }
02479 if (track->mode != MODE_MOV) {
02480 if (track->timescale > UINT16_MAX) {
02481 av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
02482 "sample rate %dhz\n", i, track->timescale);
02483 goto error;
02484 }
02485 if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
02486 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
02487 i, track->enc->sample_rate);
02488 goto error;
02489 }
02490 }
02491 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
02492 track->timescale = st->codec->time_base.den;
02493 }
02494 if (!track->height)
02495 track->height = st->codec->height;
02496
02497 avpriv_set_pts_info(st, 64, 1, track->timescale);
02498 }
02499
02500 if(mov->reserved_moov_size){
02501 mov->reserved_moov_pos= avio_tell(pb);
02502 avio_skip(pb, mov->reserved_moov_size);
02503 }
02504
02505 mov_write_mdat_tag(pb, mov);
02506
02507 #if FF_API_TIMESTAMP
02508 if (s->timestamp)
02509 mov->time = s->timestamp;
02510 else
02511 #endif
02512 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
02513 mov->time = ff_iso8601_to_unix_time(t->value);
02514 mov->time += 0x7C25B080;
02515
02516 if (mov->chapter_track)
02517 mov_create_chapter_track(s, mov->chapter_track);
02518
02519 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
02520
02521 for (i = 0; i < s->nb_streams; i++) {
02522 AVStream *st = s->streams[i];
02523 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
02524 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
02525 ff_mov_init_hinting(s, hint_track, i);
02526 hint_track++;
02527 }
02528 }
02529 }
02530
02531 avio_flush(pb);
02532
02533 return 0;
02534 error:
02535 av_freep(&mov->tracks);
02536 return -1;
02537 }
02538
02539 static int update_first_fragment(AVFormatContext *s)
02540 {
02541 MOVMuxContext *mov = s->priv_data;
02542 AVIOContext *pb = s->pb;
02543 int res = 0;
02544 int i;
02545
02546 int64_t moov_pos = avio_tell(pb);
02547
02548
02549 if (mov->mdat_size+8 <= UINT32_MAX) {
02550 avio_seek(pb, mov->mdat_pos, SEEK_SET);
02551 avio_wb32(pb, mov->mdat_size+8);
02552 } else {
02553
02554 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
02555 avio_wb32(pb, 1);
02556 ffio_wfourcc(pb, "mdat");
02557 avio_wb64(pb, mov->mdat_size+16);
02558 }
02559 avio_seek(pb, mov->reserved_moov_size ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
02560
02561 mov_write_moov_tag(pb, mov, s);
02562 if(mov->reserved_moov_size){
02563 int64_t size= mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
02564 if(size < 8){
02565 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
02566 return -1;
02567 }
02568 avio_wb32(pb, size);
02569 ffio_wfourcc(pb, "free");
02570 for(i=0; i<size; i++)
02571 avio_w8(pb, 0);
02572 avio_seek(pb, moov_pos, SEEK_SET);
02573 }
02574
02575 return 0;
02576 }
02577
02578 static int mov_write_trailer(AVFormatContext *s)
02579 {
02580 MOVMuxContext *mov = s->priv_data;
02581 AVIOContext *pb = s->pb;
02582 int res = 0;
02583 int i;
02584
02585 flush_cluster_buffer(s);
02586
02587 if (mov->chapter_track)
02588 av_freep(&mov->tracks[mov->chapter_track].enc);
02589
02590 for (i=0; i<mov->nb_streams; i++) {
02591 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
02592 ff_mov_close_hinting(&mov->tracks[i]);
02593 av_freep(&mov->tracks[i].cluster);
02594
02595 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
02596
02597 }
02598
02599 avio_flush(pb);
02600
02601 av_freep(&mov->tracks);
02602
02603 return res;
02604 }
02605
02606 #if CONFIG_MOV_MUXER
02607 MOV_CLASS(mov)
02608 AVOutputFormat ff_mov_muxer = {
02609 .name = "mov",
02610 .long_name = NULL_IF_CONFIG_SMALL("MOV format"),
02611 .extensions = "mov",
02612 .priv_data_size = sizeof(MOVMuxContext),
02613 .audio_codec = CODEC_ID_AAC,
02614 #if CONFIG_LIBX264_ENCODER
02615 .video_codec = CODEC_ID_H264,
02616 #else
02617 .video_codec = CODEC_ID_MPEG4,
02618 #endif
02619 .write_header = mov_write_header,
02620 .write_packet = ff_mov_write_packet,
02621 .write_trailer = mov_write_trailer,
02622 .flags = AVFMT_GLOBALHEADER,
02623 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
02624 .priv_class = &mov_muxer_class,
02625 };
02626 #endif
02627 #if CONFIG_TGP_MUXER
02628 MOV_CLASS(tgp)
02629 AVOutputFormat ff_tgp_muxer = {
02630 .name = "3gp",
02631 .long_name = NULL_IF_CONFIG_SMALL("3GP format"),
02632 .extensions = "3gp",
02633 .priv_data_size = sizeof(MOVMuxContext),
02634 .audio_codec = CODEC_ID_AMR_NB,
02635 .video_codec = CODEC_ID_H263,
02636 .write_header = mov_write_header,
02637 .write_packet = ff_mov_write_packet,
02638 .write_trailer = mov_write_trailer,
02639 .flags = AVFMT_GLOBALHEADER,
02640 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
02641 .priv_class = &tgp_muxer_class,
02642 };
02643 #endif
02644 #if CONFIG_MP4_MUXER
02645 MOV_CLASS(mp4)
02646 AVOutputFormat ff_mp4_muxer = {
02647 .name = "mp4",
02648 .long_name = NULL_IF_CONFIG_SMALL("MP4 format"),
02649 .mime_type = "application/mp4",
02650 .extensions = "mp4",
02651 .priv_data_size = sizeof(MOVMuxContext),
02652 .audio_codec = CODEC_ID_AAC,
02653 #if CONFIG_LIBX264_ENCODER
02654 .video_codec = CODEC_ID_H264,
02655 #else
02656 .video_codec = CODEC_ID_MPEG4,
02657 #endif
02658 .write_header = mov_write_header,
02659 .write_packet = ff_mov_write_packet,
02660 .write_trailer = mov_write_trailer,
02661 .flags = AVFMT_GLOBALHEADER,
02662 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
02663 .priv_class = &mp4_muxer_class,
02664 };
02665 #endif
02666 #if CONFIG_PSP_MUXER
02667 MOV_CLASS(psp)
02668 AVOutputFormat ff_psp_muxer = {
02669 .name = "psp",
02670 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 format"),
02671 .extensions = "mp4,psp",
02672 .priv_data_size = sizeof(MOVMuxContext),
02673 .audio_codec = CODEC_ID_AAC,
02674 #if CONFIG_LIBX264_ENCODER
02675 .video_codec = CODEC_ID_H264,
02676 #else
02677 .video_codec = CODEC_ID_MPEG4,
02678 #endif
02679 .write_header = mov_write_header,
02680 .write_packet = ff_mov_write_packet,
02681 .write_trailer = mov_write_trailer,
02682 .flags = AVFMT_GLOBALHEADER,
02683 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
02684 .priv_class = &psp_muxer_class,
02685 };
02686 #endif
02687 #if CONFIG_TG2_MUXER
02688 MOV_CLASS(tg2)
02689 AVOutputFormat ff_tg2_muxer = {
02690 .name = "3g2",
02691 .long_name = NULL_IF_CONFIG_SMALL("3GP2 format"),
02692 .extensions = "3g2",
02693 .priv_data_size = sizeof(MOVMuxContext),
02694 .audio_codec = CODEC_ID_AMR_NB,
02695 .video_codec = CODEC_ID_H263,
02696 .write_header = mov_write_header,
02697 .write_packet = ff_mov_write_packet,
02698 .write_trailer = mov_write_trailer,
02699 .flags = AVFMT_GLOBALHEADER,
02700 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
02701 .priv_class = &tg2_muxer_class,
02702 };
02703 #endif
02704 #if CONFIG_IPOD_MUXER
02705 MOV_CLASS(ipod)
02706 AVOutputFormat ff_ipod_muxer = {
02707 .name = "ipod",
02708 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
02709 .mime_type = "application/mp4",
02710 .extensions = "m4v,m4a",
02711 .priv_data_size = sizeof(MOVMuxContext),
02712 .audio_codec = CODEC_ID_AAC,
02713 .video_codec = CODEC_ID_H264,
02714 .write_header = mov_write_header,
02715 .write_packet = ff_mov_write_packet,
02716 .write_trailer = mov_write_trailer,
02717 .flags = AVFMT_GLOBALHEADER,
02718 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
02719 .priv_class = &ipod_muxer_class,
02720 };
02721 #endif