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