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 "libavcodec/vc1.h"
00034 #include "internal.h"
00035 #include "libavutil/avstring.h"
00036 #include "libavutil/intfloat.h"
00037 #include "libavutil/mathematics.h"
00038 #include "libavutil/opt.h"
00039 #include "libavutil/dict.h"
00040 #include "rtpenc.h"
00041 #include "mov_chan.h"
00042
00043 #undef NDEBUG
00044 #include <assert.h>
00045
00046 static const AVOption options[] = {
00047 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00048 { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00049 { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
00050 { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00051 { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00052 { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00053 { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00054 { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00055 { "faststart", "Run a second pass to put the moov at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
00056 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
00057 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
00058 { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
00059 { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
00060 { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00061 { "min_frag_duration", "Minimum fragment duration", offsetof(MOVMuxContext, min_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00062 { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00063 { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
00064 { NULL },
00065 };
00066
00067 #define MOV_CLASS(flavor)\
00068 static const AVClass flavor ## _muxer_class = {\
00069 .class_name = #flavor " muxer",\
00070 .item_name = av_default_item_name,\
00071 .option = options,\
00072 .version = LIBAVUTIL_VERSION_INT,\
00073 };
00074
00075
00076 static int64_t update_size(AVIOContext *pb, int64_t pos)
00077 {
00078 int64_t curpos = avio_tell(pb);
00079 avio_seek(pb, pos, SEEK_SET);
00080 avio_wb32(pb, curpos - pos);
00081 avio_seek(pb, curpos, SEEK_SET);
00082
00083 return curpos - pos;
00084 }
00085
00086 static int is_co64_required(const MOVTrack *track)
00087 {
00088 int i;
00089
00090 for (i = 0; i < track->entry; i++) {
00091 if (!track->cluster[i].chunkNum)
00092 continue;
00093 if (track->cluster[i].pos + track->data_offset > UINT32_MAX)
00094 return 1;
00095 }
00096 return 0;
00097 }
00098
00099
00100 static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
00101 {
00102 int i;
00103 int mode64 = is_co64_required(track);
00104 int64_t pos = avio_tell(pb);
00105 avio_wb32(pb, 0);
00106 if (mode64)
00107 ffio_wfourcc(pb, "co64");
00108 else
00109 ffio_wfourcc(pb, "stco");
00110 avio_wb32(pb, 0);
00111 avio_wb32(pb, track->chunkCount);
00112 for (i=0; i<track->entry; i++) {
00113 if(!track->cluster[i].chunkNum)
00114 continue;
00115 if(mode64 == 1)
00116 avio_wb64(pb, track->cluster[i].pos + track->data_offset);
00117 else
00118 avio_wb32(pb, track->cluster[i].pos + track->data_offset);
00119 }
00120 return update_size(pb, pos);
00121 }
00122
00123
00124 static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
00125 {
00126 int equalChunks = 1;
00127 int i, j, entries = 0, tst = -1, oldtst = -1;
00128
00129 int64_t pos = avio_tell(pb);
00130 avio_wb32(pb, 0);
00131 ffio_wfourcc(pb, "stsz");
00132 avio_wb32(pb, 0);
00133
00134 for (i=0; i<track->entry; i++) {
00135 tst = track->cluster[i].size/track->cluster[i].entries;
00136 if(oldtst != -1 && tst != oldtst) {
00137 equalChunks = 0;
00138 }
00139 oldtst = tst;
00140 entries += track->cluster[i].entries;
00141 }
00142 if (equalChunks && track->entry) {
00143 int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
00144 sSize = FFMAX(1, sSize);
00145 avio_wb32(pb, sSize);
00146 avio_wb32(pb, entries);
00147 }
00148 else {
00149 avio_wb32(pb, 0);
00150 avio_wb32(pb, entries);
00151 for (i=0; i<track->entry; i++) {
00152 for (j=0; j<track->cluster[i].entries; j++) {
00153 avio_wb32(pb, track->cluster[i].size /
00154 track->cluster[i].entries);
00155 }
00156 }
00157 }
00158 return update_size(pb, pos);
00159 }
00160
00161
00162 static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
00163 {
00164 int index = 0, oldval = -1, i;
00165 int64_t entryPos, curpos;
00166
00167 int64_t pos = avio_tell(pb);
00168 avio_wb32(pb, 0);
00169 ffio_wfourcc(pb, "stsc");
00170 avio_wb32(pb, 0);
00171 entryPos = avio_tell(pb);
00172 avio_wb32(pb, track->chunkCount);
00173 for (i=0; i<track->entry; i++) {
00174 if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum)
00175 {
00176 avio_wb32(pb, track->cluster[i].chunkNum);
00177 avio_wb32(pb, track->cluster[i].samples_in_chunk);
00178 avio_wb32(pb, 0x1);
00179 oldval = track->cluster[i].samples_in_chunk;
00180 index++;
00181 }
00182 }
00183 curpos = avio_tell(pb);
00184 avio_seek(pb, entryPos, SEEK_SET);
00185 avio_wb32(pb, index);
00186 avio_seek(pb, curpos, SEEK_SET);
00187
00188 return update_size(pb, pos);
00189 }
00190
00191
00192 static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
00193 {
00194 int64_t curpos, entryPos;
00195 int i, index = 0;
00196 int64_t pos = avio_tell(pb);
00197 avio_wb32(pb, 0);
00198 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
00199 avio_wb32(pb, 0);
00200 entryPos = avio_tell(pb);
00201 avio_wb32(pb, track->entry);
00202 for (i=0; i<track->entry; i++) {
00203 if (track->cluster[i].flags & flag) {
00204 avio_wb32(pb, i+1);
00205 index++;
00206 }
00207 }
00208 curpos = avio_tell(pb);
00209 avio_seek(pb, entryPos, SEEK_SET);
00210 avio_wb32(pb, index);
00211 avio_seek(pb, curpos, SEEK_SET);
00212 return update_size(pb, pos);
00213 }
00214
00215 static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
00216 {
00217 avio_wb32(pb, 0x11);
00218 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
00219 else ffio_wfourcc(pb, "damr");
00220 ffio_wfourcc(pb, "FFMP");
00221 avio_w8(pb, 0);
00222
00223 avio_wb16(pb, 0x81FF);
00224 avio_w8(pb, 0x00);
00225 avio_w8(pb, 0x01);
00226 return 0x11;
00227 }
00228
00229 static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
00230 {
00231 GetBitContext gbc;
00232 PutBitContext pbc;
00233 uint8_t buf[3];
00234 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
00235
00236 if (track->vos_len < 7)
00237 return -1;
00238
00239 avio_wb32(pb, 11);
00240 ffio_wfourcc(pb, "dac3");
00241
00242 init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
00243 fscod = get_bits(&gbc, 2);
00244 frmsizecod = get_bits(&gbc, 6);
00245 bsid = get_bits(&gbc, 5);
00246 bsmod = get_bits(&gbc, 3);
00247 acmod = get_bits(&gbc, 3);
00248 if (acmod == 2) {
00249 skip_bits(&gbc, 2);
00250 } else {
00251 if ((acmod & 1) && acmod != 1)
00252 skip_bits(&gbc, 2);
00253 if (acmod & 4)
00254 skip_bits(&gbc, 2);
00255 }
00256 lfeon = get_bits1(&gbc);
00257
00258 init_put_bits(&pbc, buf, sizeof(buf));
00259 put_bits(&pbc, 2, fscod);
00260 put_bits(&pbc, 5, bsid);
00261 put_bits(&pbc, 3, bsmod);
00262 put_bits(&pbc, 3, acmod);
00263 put_bits(&pbc, 1, lfeon);
00264 put_bits(&pbc, 5, frmsizecod>>1);
00265 put_bits(&pbc, 5, 0);
00266
00267 flush_put_bits(&pbc);
00268 avio_write(pb, buf, sizeof(buf));
00269
00270 return 11;
00271 }
00272
00277 static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
00278 {
00279 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
00280 return track->enc->extradata_size;
00281 }
00282
00283 static int mov_write_enda_tag(AVIOContext *pb)
00284 {
00285 avio_wb32(pb, 10);
00286 ffio_wfourcc(pb, "enda");
00287 avio_wb16(pb, 1);
00288 return 10;
00289 }
00290
00291 static void put_descr(AVIOContext *pb, int tag, unsigned int size)
00292 {
00293 int i = 3;
00294 avio_w8(pb, tag);
00295 for(; i>0; i--)
00296 avio_w8(pb, (size>>(7*i)) | 0x80);
00297 avio_w8(pb, size & 0x7F);
00298 }
00299
00300 static unsigned compute_avg_bitrate(MOVTrack *track)
00301 {
00302 uint64_t size = 0;
00303 int i;
00304 if (!track->track_duration)
00305 return 0;
00306 for (i = 0; i < track->entry; i++)
00307 size += track->cluster[i].size;
00308 return size * 8 * track->timescale / track->track_duration;
00309 }
00310
00311 static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track)
00312 {
00313 int64_t pos = avio_tell(pb);
00314 int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
00315 unsigned avg_bitrate;
00316
00317 avio_wb32(pb, 0);
00318 ffio_wfourcc(pb, "esds");
00319 avio_wb32(pb, 0);
00320
00321
00322 put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
00323 avio_wb16(pb, track->track_id);
00324 avio_w8(pb, 0x00);
00325
00326
00327 put_descr(pb, 0x04, 13 + decoder_specific_info_len);
00328
00329
00330 if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
00331 track->enc->codec_id == AV_CODEC_ID_MP3) &&
00332 track->enc->sample_rate > 24000)
00333 avio_w8(pb, 0x6B);
00334 else
00335 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
00336
00337
00338
00339 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
00340 avio_w8(pb, 0x15);
00341 else
00342 avio_w8(pb, 0x11);
00343
00344 avio_w8(pb, track->enc->rc_buffer_size>>(3+16));
00345 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF);
00346
00347 avg_bitrate = compute_avg_bitrate(track);
00348
00349 avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
00350 avio_wb32(pb, avg_bitrate);
00351
00352 if (track->vos_len) {
00353
00354 put_descr(pb, 0x05, track->vos_len);
00355 avio_write(pb, track->vos_data, track->vos_len);
00356 }
00357
00358
00359 put_descr(pb, 0x06, 1);
00360 avio_w8(pb, 0x02);
00361 return update_size(pb, pos);
00362 }
00363
00364 static int mov_pcm_le_gt16(enum AVCodecID codec_id)
00365 {
00366 return codec_id == AV_CODEC_ID_PCM_S24LE ||
00367 codec_id == AV_CODEC_ID_PCM_S32LE ||
00368 codec_id == AV_CODEC_ID_PCM_F32LE ||
00369 codec_id == AV_CODEC_ID_PCM_F64LE;
00370 }
00371
00372 static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
00373 {
00374 int64_t pos = avio_tell(pb);
00375 avio_wb32(pb, 0);
00376 avio_wl32(pb, track->tag);
00377 track->enc->codec_tag = av_bswap16(track->tag >> 16);
00378 ff_put_wav_header(pb, track->enc);
00379 return update_size(pb, pos);
00380 }
00381
00382 static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
00383 {
00384 int64_t pos = avio_tell(pb);
00385 avio_wb32(pb, 0);
00386 ffio_wfourcc(pb, "wfex");
00387 ff_put_wav_header(pb, track->enc);
00388 return update_size(pb, pos);
00389 }
00390
00391 static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
00392 {
00393 uint32_t layout_tag, bitmap;
00394 int64_t pos = avio_tell(pb);
00395
00396 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
00397 track->enc->channel_layout,
00398 &bitmap);
00399 if (!layout_tag) {
00400 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
00401 "lack of channel information\n");
00402 return 0;
00403 }
00404
00405 avio_wb32(pb, 0);
00406 ffio_wfourcc(pb, "chan");
00407 avio_w8(pb, 0);
00408 avio_wb24(pb, 0);
00409 avio_wb32(pb, layout_tag);
00410 avio_wb32(pb, bitmap);
00411 avio_wb32(pb, 0);
00412
00413 return update_size(pb, pos);
00414 }
00415
00416 static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
00417 {
00418 int64_t pos = avio_tell(pb);
00419
00420 avio_wb32(pb, 0);
00421 ffio_wfourcc(pb, "wave");
00422
00423 if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
00424 avio_wb32(pb, 12);
00425 ffio_wfourcc(pb, "frma");
00426 avio_wl32(pb, track->tag);
00427 }
00428
00429 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
00430
00431 avio_wb32(pb, 12);
00432 ffio_wfourcc(pb, "mp4a");
00433 avio_wb32(pb, 0);
00434 mov_write_esds_tag(pb, track);
00435 } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
00436 mov_write_enda_tag(pb);
00437 } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
00438 mov_write_amr_tag(pb, track);
00439 } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
00440 mov_write_ac3_tag(pb, track);
00441 } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
00442 track->enc->codec_id == AV_CODEC_ID_QDM2) {
00443 mov_write_extradata_tag(pb, track);
00444 } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
00445 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
00446 mov_write_ms_tag(pb, track);
00447 }
00448
00449 avio_wb32(pb, 8);
00450 avio_wb32(pb, 0);
00451
00452 return update_size(pb, pos);
00453 }
00454
00455 static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
00456 {
00457 uint8_t *unescaped;
00458 const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
00459 int unescaped_size, seq_found = 0;
00460 int level = 0, interlace = 0;
00461 int packet_seq = track->vc1_info.packet_seq;
00462 int packet_entry = track->vc1_info.packet_entry;
00463 int slices = track->vc1_info.slices;
00464 PutBitContext pbc;
00465
00466 if (track->start_dts == AV_NOPTS_VALUE) {
00467
00468
00469
00470 packet_seq = packet_entry = 1;
00471 }
00472
00473 unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
00474 if (!unescaped)
00475 return AVERROR(ENOMEM);
00476 start = find_next_marker(track->vos_data, end);
00477 for (next = start; next < end; start = next) {
00478 GetBitContext gb;
00479 int size;
00480 next = find_next_marker(start + 4, end);
00481 size = next - start - 4;
00482 if (size <= 0)
00483 continue;
00484 unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
00485 init_get_bits(&gb, unescaped, 8 * unescaped_size);
00486 if (AV_RB32(start) == VC1_CODE_SEQHDR) {
00487 int profile = get_bits(&gb, 2);
00488 if (profile != PROFILE_ADVANCED) {
00489 av_free(unescaped);
00490 return AVERROR(ENOSYS);
00491 }
00492 seq_found = 1;
00493 level = get_bits(&gb, 3);
00494
00495
00496 skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
00497 skip_bits(&gb, 1);
00498 interlace = get_bits1(&gb);
00499 skip_bits(&gb, 4);
00500 }
00501 }
00502 if (!seq_found) {
00503 av_free(unescaped);
00504 return AVERROR(ENOSYS);
00505 }
00506
00507 init_put_bits(&pbc, buf, 7);
00508
00509 put_bits(&pbc, 4, 12);
00510 put_bits(&pbc, 3, level);
00511 put_bits(&pbc, 1, 0);
00512
00513 put_bits(&pbc, 3, level);
00514 put_bits(&pbc, 1, 0);
00515 put_bits(&pbc, 6, 0);
00516 put_bits(&pbc, 1, !interlace);
00517 put_bits(&pbc, 1, !packet_seq);
00518 put_bits(&pbc, 1, !packet_entry);
00519 put_bits(&pbc, 1, !slices);
00520 put_bits(&pbc, 1, 0);
00521 put_bits(&pbc, 1, 0);
00522 put_bits32(&pbc, track->enc->time_base.den);
00523 flush_put_bits(&pbc);
00524
00525 av_free(unescaped);
00526
00527 return 0;
00528 }
00529
00530 static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
00531 {
00532 uint8_t buf[7] = { 0 };
00533 int ret;
00534
00535 if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
00536 return ret;
00537
00538 avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
00539 ffio_wfourcc(pb, "dvc1");
00540 track->vc1_info.struct_offset = avio_tell(pb);
00541 avio_write(pb, buf, sizeof(buf));
00542 avio_write(pb, track->vos_data, track->vos_len);
00543
00544 return 0;
00545 }
00546
00547 static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
00548 {
00549 avio_wb32(pb, track->vos_len + 8);
00550 ffio_wfourcc(pb, "glbl");
00551 avio_write(pb, track->vos_data, track->vos_len);
00552 return 8 + track->vos_len;
00553 }
00554
00559 static int mov_get_lpcm_flags(enum AVCodecID codec_id)
00560 {
00561 switch (codec_id) {
00562 case AV_CODEC_ID_PCM_F32BE:
00563 case AV_CODEC_ID_PCM_F64BE:
00564 return 11;
00565 case AV_CODEC_ID_PCM_F32LE:
00566 case AV_CODEC_ID_PCM_F64LE:
00567 return 9;
00568 case AV_CODEC_ID_PCM_U8:
00569 return 10;
00570 case AV_CODEC_ID_PCM_S16BE:
00571 case AV_CODEC_ID_PCM_S24BE:
00572 case AV_CODEC_ID_PCM_S32BE:
00573 return 14;
00574 case AV_CODEC_ID_PCM_S8:
00575 case AV_CODEC_ID_PCM_S16LE:
00576 case AV_CODEC_ID_PCM_S24LE:
00577 case AV_CODEC_ID_PCM_S32LE:
00578 return 12;
00579 default:
00580 return 0;
00581 }
00582 }
00583
00584 static int get_cluster_duration(MOVTrack *track, int cluster_idx)
00585 {
00586 int64_t next_dts;
00587
00588 if (cluster_idx >= track->entry)
00589 return 0;
00590
00591 if (cluster_idx + 1 == track->entry)
00592 next_dts = track->track_duration + track->start_dts;
00593 else
00594 next_dts = track->cluster[cluster_idx + 1].dts;
00595
00596 return next_dts - track->cluster[cluster_idx].dts;
00597 }
00598
00599 static int get_samples_per_packet(MOVTrack *track)
00600 {
00601 int i, first_duration;
00602
00603
00604
00605
00606 if (!track->audio_vbr)
00607 return 1;
00608
00609
00610 if (!track->entry)
00611 return 0;
00612 first_duration = get_cluster_duration(track, 0);
00613 for (i = 1; i < track->entry; i++) {
00614 if (get_cluster_duration(track, i) != first_duration)
00615 return 0;
00616 }
00617 return first_duration;
00618 }
00619
00620 static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
00621 {
00622 int64_t pos = avio_tell(pb);
00623 int version = 0;
00624 uint32_t tag = track->tag;
00625
00626 if (track->mode == MODE_MOV) {
00627 if (track->timescale > UINT16_MAX) {
00628 if (mov_get_lpcm_flags(track->enc->codec_id))
00629 tag = AV_RL32("lpcm");
00630 version = 2;
00631 } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
00632 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
00633 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
00634 track->enc->codec_id == AV_CODEC_ID_QDM2) {
00635 version = 1;
00636 }
00637 }
00638
00639 avio_wb32(pb, 0);
00640 avio_wl32(pb, tag);
00641 avio_wb32(pb, 0);
00642 avio_wb16(pb, 0);
00643 avio_wb16(pb, 1);
00644
00645
00646 avio_wb16(pb, version);
00647 avio_wb16(pb, 0);
00648 avio_wb32(pb, 0);
00649
00650 if (version == 2) {
00651 avio_wb16(pb, 3);
00652 avio_wb16(pb, 16);
00653 avio_wb16(pb, 0xfffe);
00654 avio_wb16(pb, 0);
00655 avio_wb32(pb, 0x00010000);
00656 avio_wb32(pb, 72);
00657 avio_wb64(pb, av_double2int(track->enc->sample_rate));
00658 avio_wb32(pb, track->enc->channels);
00659 avio_wb32(pb, 0x7F000000);
00660 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
00661 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
00662 avio_wb32(pb, track->sample_size);
00663 avio_wb32(pb, get_samples_per_packet(track));
00664 } else {
00665 if (track->mode == MODE_MOV) {
00666 avio_wb16(pb, track->enc->channels);
00667 if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
00668 track->enc->codec_id == AV_CODEC_ID_PCM_S8)
00669 avio_wb16(pb, 8);
00670 else
00671 avio_wb16(pb, 16);
00672 avio_wb16(pb, track->audio_vbr ? -2 : 0);
00673 } else {
00674 avio_wb16(pb, 2);
00675 avio_wb16(pb, 16);
00676 avio_wb16(pb, 0);
00677 }
00678
00679 avio_wb16(pb, 0);
00680 avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
00681 track->enc->sample_rate : 0);
00682 avio_wb16(pb, 0);
00683 }
00684
00685 if(version == 1) {
00686 avio_wb32(pb, track->enc->frame_size);
00687 avio_wb32(pb, track->sample_size / track->enc->channels);
00688 avio_wb32(pb, track->sample_size);
00689 avio_wb32(pb, 2);
00690 }
00691
00692 if(track->mode == MODE_MOV &&
00693 (track->enc->codec_id == AV_CODEC_ID_AAC ||
00694 track->enc->codec_id == AV_CODEC_ID_AC3 ||
00695 track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
00696 track->enc->codec_id == AV_CODEC_ID_ALAC ||
00697 track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
00698 track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
00699 track->enc->codec_id == AV_CODEC_ID_QDM2 ||
00700 (mov_pcm_le_gt16(track->enc->codec_id) && version==1)))
00701 mov_write_wave_tag(pb, track);
00702 else if(track->tag == MKTAG('m','p','4','a'))
00703 mov_write_esds_tag(pb, track);
00704 else if(track->enc->codec_id == AV_CODEC_ID_AMR_NB)
00705 mov_write_amr_tag(pb, track);
00706 else if(track->enc->codec_id == AV_CODEC_ID_AC3)
00707 mov_write_ac3_tag(pb, track);
00708 else if(track->enc->codec_id == AV_CODEC_ID_ALAC)
00709 mov_write_extradata_tag(pb, track);
00710 else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
00711 mov_write_wfex_tag(pb, track);
00712 else if (track->vos_len > 0)
00713 mov_write_glbl_tag(pb, track);
00714
00715 if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
00716 mov_write_chan_tag(pb, track);
00717
00718 return update_size(pb, pos);
00719 }
00720
00721 static int mov_write_d263_tag(AVIOContext *pb)
00722 {
00723 avio_wb32(pb, 0xf);
00724 ffio_wfourcc(pb, "d263");
00725 ffio_wfourcc(pb, "FFMP");
00726 avio_w8(pb, 0);
00727
00728 avio_w8(pb, 0xa);
00729 avio_w8(pb, 0);
00730 return 0xf;
00731 }
00732
00733 static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
00734 {
00735 int64_t pos = avio_tell(pb);
00736
00737 avio_wb32(pb, 0);
00738 ffio_wfourcc(pb, "avcC");
00739 ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
00740 return update_size(pb, pos);
00741 }
00742
00743
00744 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
00745 {
00746 int i;
00747 avio_wb32(pb, 24);
00748 ffio_wfourcc(pb, "ACLR");
00749 ffio_wfourcc(pb, "ACLR");
00750 ffio_wfourcc(pb, "0001");
00751 avio_wb32(pb, 2);
00752 avio_wb32(pb, 0);
00753
00754 avio_wb32(pb, 24);
00755 ffio_wfourcc(pb, "APRG");
00756 ffio_wfourcc(pb, "APRG");
00757 ffio_wfourcc(pb, "0001");
00758 avio_wb32(pb, 1);
00759 avio_wb32(pb, 0);
00760
00761 avio_wb32(pb, 120);
00762 ffio_wfourcc(pb, "ARES");
00763 ffio_wfourcc(pb, "ARES");
00764 ffio_wfourcc(pb, "0001");
00765 avio_wb32(pb, AV_RB32(track->vos_data + 0x28));
00766 avio_wb32(pb, track->enc->width);
00767
00768 if (track->vos_data[5] & 2) {
00769 avio_wb32(pb, track->enc->height/2);
00770 avio_wb32(pb, 2);
00771 avio_wb32(pb, 0);
00772 avio_wb32(pb, 4);
00773 } else {
00774 avio_wb32(pb, track->enc->height);
00775 avio_wb32(pb, 1);
00776 avio_wb32(pb, 0);
00777 if (track->enc->height == 1080)
00778 avio_wb32(pb, 5);
00779 else
00780 avio_wb32(pb, 6);
00781 }
00782
00783 for (i = 0; i < 10; i++)
00784 avio_wb64(pb, 0);
00785
00786
00787 avio_wb32(pb, 0);
00788 return 0;
00789 }
00790
00791 static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
00792 {
00793 int tag = track->enc->codec_tag;
00794
00795 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
00796 return 0;
00797
00798 if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
00799 else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
00800 else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
00801 else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
00802 else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
00803 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
00804 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
00805
00806 return tag;
00807 }
00808
00809 static const AVCodecTag codec_ipod_tags[] = {
00810 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
00811 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
00812 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
00813 { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
00814 { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
00815 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
00816 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
00817 { AV_CODEC_ID_NONE, 0 },
00818 };
00819
00820 static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
00821 {
00822 int tag = track->enc->codec_tag;
00823
00824
00825 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
00826 (tag == MKTAG('t','x','3','g') ||
00827 tag == MKTAG('t','e','x','t'))))
00828 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
00829
00830 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
00831 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
00832 "Quicktime/Ipod might not play the file\n");
00833
00834 return tag;
00835 }
00836
00837 static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
00838 {
00839 int tag;
00840
00841 if (track->enc->width == 720)
00842 if (track->enc->height == 480)
00843 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
00844 else tag = MKTAG('d','v','c',' ');
00845 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
00846 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
00847 else tag = MKTAG('d','v','p','p');
00848 else if (track->enc->height == 720)
00849 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
00850 else tag = MKTAG('d','v','h','p');
00851 else if (track->enc->height == 1080)
00852 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
00853 else tag = MKTAG('d','v','h','6');
00854 else {
00855 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
00856 return 0;
00857 }
00858
00859 return tag;
00860 }
00861
00862 static const struct {
00863 enum PixelFormat pix_fmt;
00864 uint32_t tag;
00865 unsigned bps;
00866 } mov_pix_fmt_tags[] = {
00867 { PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
00868 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
00869 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
00870 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
00871 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
00872 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
00873 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
00874 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
00875 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
00876 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
00877 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
00878 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
00879 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
00880 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
00881 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
00882 };
00883
00884 static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
00885 {
00886 int tag = track->enc->codec_tag;
00887 int i;
00888
00889 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
00890 if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag && track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
00891 tag = mov_pix_fmt_tags[i].tag;
00892 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
00893 break;
00894 }
00895 }
00896
00897 return tag;
00898 }
00899
00900 static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
00901 {
00902 int tag = track->enc->codec_tag;
00903
00904 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
00905 (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
00906 track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
00907 track->enc->codec_id == AV_CODEC_ID_H263 ||
00908 av_get_bits_per_sample(track->enc->codec_id)))) {
00909 if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
00910 tag = mov_get_dv_codec_tag(s, track);
00911 else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
00912 tag = mov_get_rawvideo_codec_tag(s, track);
00913 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
00914 tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
00915 if (!tag) {
00916 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
00917 if (tag)
00918 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
00919 "the file may be unplayable!\n");
00920 }
00921 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
00922 tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
00923 if (!tag) {
00924 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
00925 if (ms_tag) {
00926 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
00927 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
00928 "the file may be unplayable!\n");
00929 }
00930 }
00931 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
00932 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
00933 }
00934
00935 return tag;
00936 }
00937
00938 static const AVCodecTag codec_3gp_tags[] = {
00939 { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
00940 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
00941 { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
00942 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
00943 { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
00944 { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
00945 { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
00946 { AV_CODEC_ID_NONE, 0 },
00947 };
00948
00949 static const AVCodecTag codec_f4v_tags[] = {
00950 { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
00951 { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
00952 { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
00953 { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
00954 { AV_CODEC_ID_NONE, 0 },
00955 };
00956
00957 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
00958 {
00959 int tag;
00960
00961 if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
00962 tag = mp4_get_codec_tag(s, track);
00963 else if (track->mode == MODE_ISM) {
00964 tag = mp4_get_codec_tag(s, track);
00965 if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
00966 tag = MKTAG('w', 'm', 'a', ' ');
00967 } else if (track->mode == MODE_IPOD)
00968 tag = ipod_get_codec_tag(s, track);
00969 else if (track->mode & MODE_3GP)
00970 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
00971 else if (track->mode & MODE_F4V)
00972 tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
00973 else
00974 tag = mov_get_codec_tag(s, track);
00975
00976 return tag;
00977 }
00978
00983 static int mov_write_uuid_tag_ipod(AVIOContext *pb)
00984 {
00985 avio_wb32(pb, 28);
00986 ffio_wfourcc(pb, "uuid");
00987 avio_wb32(pb, 0x6b6840f2);
00988 avio_wb32(pb, 0x5f244fc5);
00989 avio_wb32(pb, 0xba39a51b);
00990 avio_wb32(pb, 0xcf0323f3);
00991 avio_wb32(pb, 0x0);
00992 return 28;
00993 }
00994
00995 static const uint16_t fiel_data[] = {
00996 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
00997 };
00998
00999 static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
01000 {
01001 unsigned mov_field_order = 0;
01002 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
01003 mov_field_order = fiel_data[track->enc->field_order];
01004 else
01005 return 0;
01006 avio_wb32(pb, 10);
01007 ffio_wfourcc(pb, "fiel");
01008 avio_wb16(pb, mov_field_order);
01009 return 10;
01010 }
01011
01012 static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
01013 {
01014 int64_t pos = avio_tell(pb);
01015 avio_wb32(pb, 0);
01016 avio_wl32(pb, track->tag);
01017 avio_wb32(pb, 0);
01018 avio_wb16(pb, 0);
01019 avio_wb16(pb, 1);
01020
01021 if (track->enc->extradata_size)
01022 avio_write(pb, track->enc->extradata, track->enc->extradata_size);
01023
01024 return update_size(pb, pos);
01025 }
01026
01027 static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
01028 {
01029 AVRational sar;
01030 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
01031 track->enc->sample_aspect_ratio.den, INT_MAX);
01032
01033 avio_wb32(pb, 16);
01034 ffio_wfourcc(pb, "pasp");
01035 avio_wb32(pb, sar.num);
01036 avio_wb32(pb, sar.den);
01037 return 16;
01038 }
01039
01040 static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
01041 {
01042 int64_t pos = avio_tell(pb);
01043 char compressor_name[32] = { 0 };
01044
01045 avio_wb32(pb, 0);
01046 avio_wl32(pb, track->tag);
01047 avio_wb32(pb, 0);
01048 avio_wb16(pb, 0);
01049 avio_wb16(pb, 1);
01050
01051 avio_wb16(pb, 0);
01052 avio_wb16(pb, 0);
01053 if (track->mode == MODE_MOV) {
01054 ffio_wfourcc(pb, "FFMP");
01055 if(track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
01056 avio_wb32(pb, 0);
01057 avio_wb32(pb, 0x400);
01058 } else {
01059 avio_wb32(pb, 0x200);
01060 avio_wb32(pb, 0x200);
01061 }
01062 } else {
01063 avio_wb32(pb, 0);
01064 avio_wb32(pb, 0);
01065 avio_wb32(pb, 0);
01066 }
01067 avio_wb16(pb, track->enc->width);
01068 avio_wb16(pb, track->height);
01069 avio_wb32(pb, 0x00480000);
01070 avio_wb32(pb, 0x00480000);
01071 avio_wb32(pb, 0);
01072 avio_wb16(pb, 1);
01073
01074
01075 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
01076 av_strlcpy(compressor_name,track->enc->codec->name,32);
01077 avio_w8(pb, strlen(compressor_name));
01078 avio_write(pb, compressor_name, 31);
01079
01080 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
01081 avio_wb16(pb, track->enc->bits_per_coded_sample);
01082 else
01083 avio_wb16(pb, 0x18);
01084 avio_wb16(pb, 0xffff);
01085 if(track->tag == MKTAG('m','p','4','v'))
01086 mov_write_esds_tag(pb, track);
01087 else if(track->enc->codec_id == AV_CODEC_ID_H263)
01088 mov_write_d263_tag(pb);
01089 else if(track->enc->codec_id == AV_CODEC_ID_AVUI ||
01090 track->enc->codec_id == AV_CODEC_ID_SVQ3) {
01091 mov_write_extradata_tag(pb, track);
01092 avio_wb32(pb, 0);
01093 } else if(track->enc->codec_id == AV_CODEC_ID_DNXHD)
01094 mov_write_avid_tag(pb, track);
01095 else if(track->enc->codec_id == AV_CODEC_ID_H264) {
01096 mov_write_avcc_tag(pb, track);
01097 if(track->mode == MODE_IPOD)
01098 mov_write_uuid_tag_ipod(pb);
01099 } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
01100 mov_write_fiel_tag(pb, track);
01101 else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
01102 mov_write_dvc1_tag(pb, track);
01103 else if (track->vos_len > 0)
01104 mov_write_glbl_tag(pb, track);
01105
01106 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
01107 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
01108 mov_write_pasp_tag(pb, track);
01109 }
01110
01111 return update_size(pb, pos);
01112 }
01113
01114 static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
01115 {
01116 int64_t pos = avio_tell(pb);
01117 int frame_duration = track->enc->time_base.num;
01118 int nb_frames = (track->timescale + frame_duration/2) / frame_duration;
01119
01120 avio_wb32(pb, 0);
01121 ffio_wfourcc(pb, "tmcd");
01122 avio_wb32(pb, 0);
01123 avio_wb32(pb, 1);
01124 avio_wb32(pb, 0);
01125 avio_wb32(pb, track->timecode_flags);
01126 avio_wb32(pb, track->timescale);
01127 avio_wb32(pb, frame_duration);
01128 avio_w8(pb, nb_frames);
01129 avio_wb24(pb, 0);
01130
01131 return update_size(pb, pos);
01132 }
01133
01134 static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
01135 {
01136 int64_t pos = avio_tell(pb);
01137 avio_wb32(pb, 0);
01138 ffio_wfourcc(pb, "rtp ");
01139 avio_wb32(pb, 0);
01140 avio_wb16(pb, 0);
01141 avio_wb16(pb, 1);
01142
01143 avio_wb16(pb, 1);
01144 avio_wb16(pb, 1);
01145 avio_wb32(pb, track->max_packet_size);
01146
01147 avio_wb32(pb, 12);
01148 ffio_wfourcc(pb, "tims");
01149 avio_wb32(pb, track->timescale);
01150
01151 return update_size(pb, pos);
01152 }
01153
01154 static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
01155 {
01156 int64_t pos = avio_tell(pb);
01157 avio_wb32(pb, 0);
01158 ffio_wfourcc(pb, "stsd");
01159 avio_wb32(pb, 0);
01160 avio_wb32(pb, 1);
01161 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
01162 mov_write_video_tag(pb, track);
01163 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
01164 mov_write_audio_tag(pb, track);
01165 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
01166 mov_write_subtitle_tag(pb, track);
01167 else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
01168 mov_write_rtp_tag(pb, track);
01169 else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
01170 mov_write_tmcd_tag(pb, track);
01171 return update_size(pb, pos);
01172 }
01173
01174 static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
01175 {
01176 MOVStts *ctts_entries;
01177 uint32_t entries = 0;
01178 uint32_t atom_size;
01179 int i;
01180
01181 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
01182 ctts_entries[0].count = 1;
01183 ctts_entries[0].duration = track->cluster[0].cts;
01184 for (i=1; i<track->entry; i++) {
01185 if (track->cluster[i].cts == ctts_entries[entries].duration) {
01186 ctts_entries[entries].count++;
01187 } else {
01188 entries++;
01189 ctts_entries[entries].duration = track->cluster[i].cts;
01190 ctts_entries[entries].count = 1;
01191 }
01192 }
01193 entries++;
01194 atom_size = 16 + (entries * 8);
01195 avio_wb32(pb, atom_size);
01196 ffio_wfourcc(pb, "ctts");
01197 avio_wb32(pb, 0);
01198 avio_wb32(pb, entries);
01199 for (i=0; i<entries; i++) {
01200 avio_wb32(pb, ctts_entries[i].count);
01201 avio_wb32(pb, ctts_entries[i].duration);
01202 }
01203 av_free(ctts_entries);
01204 return atom_size;
01205 }
01206
01207
01208 static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
01209 {
01210 MOVStts *stts_entries;
01211 uint32_t entries = -1;
01212 uint32_t atom_size;
01213 int i;
01214
01215 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
01216 stts_entries = av_malloc(sizeof(*stts_entries));
01217 stts_entries[0].count = track->sample_count;
01218 stts_entries[0].duration = 1;
01219 entries = 1;
01220 } else {
01221 stts_entries = track->entry ?
01222 av_malloc(track->entry * sizeof(*stts_entries)) :
01223 NULL;
01224 for (i=0; i<track->entry; i++) {
01225 int duration = get_cluster_duration(track, i);
01226 if (i && duration == stts_entries[entries].duration) {
01227 stts_entries[entries].count++;
01228 } else {
01229 entries++;
01230 stts_entries[entries].duration = duration;
01231 stts_entries[entries].count = 1;
01232 }
01233 }
01234 entries++;
01235 }
01236 atom_size = 16 + (entries * 8);
01237 avio_wb32(pb, atom_size);
01238 ffio_wfourcc(pb, "stts");
01239 avio_wb32(pb, 0);
01240 avio_wb32(pb, entries);
01241 for (i=0; i<entries; i++) {
01242 avio_wb32(pb, stts_entries[i].count);
01243 avio_wb32(pb, stts_entries[i].duration);
01244 }
01245 av_free(stts_entries);
01246 return atom_size;
01247 }
01248
01249 static int mov_write_dref_tag(AVIOContext *pb)
01250 {
01251 avio_wb32(pb, 28);
01252 ffio_wfourcc(pb, "dref");
01253 avio_wb32(pb, 0);
01254 avio_wb32(pb, 1);
01255
01256 avio_wb32(pb, 0xc);
01257
01258 ffio_wfourcc(pb, "url ");
01259 avio_wb32(pb, 1);
01260
01261 return 28;
01262 }
01263
01264 static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
01265 {
01266 int64_t pos = avio_tell(pb);
01267 avio_wb32(pb, 0);
01268 ffio_wfourcc(pb, "stbl");
01269 mov_write_stsd_tag(pb, track);
01270 mov_write_stts_tag(pb, track);
01271 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
01272 track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
01273 track->has_keyframes && track->has_keyframes < track->entry)
01274 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
01275 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
01276 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
01277 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
01278 track->flags & MOV_TRACK_CTTS)
01279 mov_write_ctts_tag(pb, track);
01280 mov_write_stsc_tag(pb, track);
01281 mov_write_stsz_tag(pb, track);
01282 mov_write_stco_tag(pb, track);
01283 return update_size(pb, pos);
01284 }
01285
01286 static int mov_write_dinf_tag(AVIOContext *pb)
01287 {
01288 int64_t pos = avio_tell(pb);
01289 avio_wb32(pb, 0);
01290 ffio_wfourcc(pb, "dinf");
01291 mov_write_dref_tag(pb);
01292 return update_size(pb, pos);
01293 }
01294
01295 static int mov_write_nmhd_tag(AVIOContext *pb)
01296 {
01297 avio_wb32(pb, 12);
01298 ffio_wfourcc(pb, "nmhd");
01299 avio_wb32(pb, 0);
01300 return 12;
01301 }
01302
01303 static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
01304 {
01305 int64_t pos = avio_tell(pb);
01306 const char *font = "Lucida Grande";
01307 avio_wb32(pb, 0);
01308 ffio_wfourcc(pb, "tcmi");
01309 avio_wb32(pb, 0);
01310 avio_wb16(pb, 0);
01311 avio_wb16(pb, 0);
01312 avio_wb16(pb, 12);
01313 avio_wb16(pb, 0);
01314 avio_wb16(pb, 0x0000);
01315 avio_wb16(pb, 0x0000);
01316 avio_wb16(pb, 0x0000);
01317 avio_wb16(pb, 0xffff);
01318 avio_wb16(pb, 0xffff);
01319 avio_wb16(pb, 0xffff);
01320 avio_w8(pb, strlen(font));
01321 avio_write(pb, font, strlen(font));
01322 return update_size(pb, pos);
01323 }
01324
01325 static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
01326 {
01327 int64_t pos = avio_tell(pb);
01328 avio_wb32(pb, 0);
01329 ffio_wfourcc(pb, "gmhd");
01330 avio_wb32(pb, 0x18);
01331 ffio_wfourcc(pb, "gmin");
01332 avio_wb32(pb, 0);
01333 avio_wb16(pb, 0x40);
01334 avio_wb16(pb, 0x8000);
01335 avio_wb16(pb, 0x8000);
01336 avio_wb16(pb, 0x8000);
01337 avio_wb16(pb, 0);
01338 avio_wb16(pb, 0);
01339
01340
01341
01342
01343
01344
01345
01346 avio_wb32(pb, 0x2C);
01347 ffio_wfourcc(pb, "text");
01348 avio_wb16(pb, 0x01);
01349 avio_wb32(pb, 0x00);
01350 avio_wb32(pb, 0x00);
01351 avio_wb32(pb, 0x00);
01352 avio_wb32(pb, 0x01);
01353 avio_wb32(pb, 0x00);
01354 avio_wb32(pb, 0x00);
01355 avio_wb32(pb, 0x00);
01356 avio_wb32(pb, 0x00004000);
01357 avio_wb16(pb, 0x0000);
01358
01359 if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
01360 int64_t tmcd_pos = avio_tell(pb);
01361 avio_wb32(pb, 0);
01362 ffio_wfourcc(pb, "tmcd");
01363 mov_write_tcmi_tag(pb, track);
01364 update_size(pb, tmcd_pos);
01365 }
01366 return update_size(pb, pos);
01367 }
01368
01369 static int mov_write_smhd_tag(AVIOContext *pb)
01370 {
01371 avio_wb32(pb, 16);
01372 ffio_wfourcc(pb, "smhd");
01373 avio_wb32(pb, 0);
01374 avio_wb16(pb, 0);
01375 avio_wb16(pb, 0);
01376 return 16;
01377 }
01378
01379 static int mov_write_vmhd_tag(AVIOContext *pb)
01380 {
01381 avio_wb32(pb, 0x14);
01382 ffio_wfourcc(pb, "vmhd");
01383 avio_wb32(pb, 0x01);
01384 avio_wb64(pb, 0);
01385 return 0x14;
01386 }
01387
01388 static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
01389 {
01390 const char *hdlr, *descr = NULL, *hdlr_type = NULL;
01391 int64_t pos = avio_tell(pb);
01392
01393 if (!track) {
01394 hdlr = "dhlr";
01395 hdlr_type = "url ";
01396 descr = "DataHandler";
01397 } else {
01398 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
01399 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
01400 hdlr_type = "vide";
01401 descr = "VideoHandler";
01402 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
01403 hdlr_type = "soun";
01404 descr = "SoundHandler";
01405 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
01406 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
01407 else hdlr_type = "text";
01408 descr = "SubtitleHandler";
01409 } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
01410 hdlr_type = "tmcd";
01411 descr = "TimeCodeHandler";
01412 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
01413 hdlr_type = "hint";
01414 descr = "HintHandler";
01415 } else {
01416 hdlr = "dhlr";
01417 hdlr_type = "url ";
01418 descr = "DataHandler";
01419 }
01420 }
01421
01422 avio_wb32(pb, 0);
01423 ffio_wfourcc(pb, "hdlr");
01424 avio_wb32(pb, 0);
01425 avio_write(pb, hdlr, 4);
01426 ffio_wfourcc(pb, hdlr_type);
01427 avio_wb32(pb ,0);
01428 avio_wb32(pb ,0);
01429 avio_wb32(pb ,0);
01430 if (!track || track->mode == MODE_MOV)
01431 avio_w8(pb, strlen(descr));
01432 avio_write(pb, descr, strlen(descr));
01433 if (track && track->mode != MODE_MOV)
01434 avio_w8(pb, 0);
01435 return update_size(pb, pos);
01436 }
01437
01438 static int mov_write_hmhd_tag(AVIOContext *pb)
01439 {
01440
01441
01442 avio_wb32(pb, 28);
01443 ffio_wfourcc(pb, "hmhd");
01444 avio_wb32(pb, 0);
01445 avio_wb16(pb, 0);
01446 avio_wb16(pb, 0);
01447 avio_wb32(pb, 0);
01448 avio_wb32(pb, 0);
01449 avio_wb32(pb, 0);
01450 return 28;
01451 }
01452
01453 static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
01454 {
01455 int64_t pos = avio_tell(pb);
01456 avio_wb32(pb, 0);
01457 ffio_wfourcc(pb, "minf");
01458 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
01459 mov_write_vmhd_tag(pb);
01460 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
01461 mov_write_smhd_tag(pb);
01462 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
01463 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb, track);
01464 else mov_write_nmhd_tag(pb);
01465 } else if (track->tag == MKTAG('t','m','c','d')) {
01466 mov_write_gmhd_tag(pb, track);
01467 } else if (track->tag == MKTAG('r','t','p',' ')) {
01468 mov_write_hmhd_tag(pb);
01469 }
01470 if (track->mode == MODE_MOV)
01471 mov_write_hdlr_tag(pb, NULL);
01472 mov_write_dinf_tag(pb);
01473 mov_write_stbl_tag(pb, track);
01474 return update_size(pb, pos);
01475 }
01476
01477 static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
01478 {
01479 int version = track->track_duration < INT32_MAX ? 0 : 1;
01480
01481 if (track->mode == MODE_ISM)
01482 version = 1;
01483
01484 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32);
01485 ffio_wfourcc(pb, "mdhd");
01486 avio_w8(pb, version);
01487 avio_wb24(pb, 0);
01488 if (version == 1) {
01489 avio_wb64(pb, track->time);
01490 avio_wb64(pb, track->time);
01491 } else {
01492 avio_wb32(pb, track->time);
01493 avio_wb32(pb, track->time);
01494 }
01495 avio_wb32(pb, track->timescale);
01496 if (!track->entry)
01497 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
01498 else
01499 (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration);
01500 avio_wb16(pb, track->language);
01501 avio_wb16(pb, 0);
01502
01503 if(version!=0 && track->mode == MODE_MOV){
01504 av_log(NULL, AV_LOG_ERROR,
01505 "FATAL error, file duration too long for timebase, this file will not be\n"
01506 "playable with quicktime. Choose a different timebase or a different\n"
01507 "container format\n");
01508 }
01509
01510 return 32;
01511 }
01512
01513 static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
01514 {
01515 int64_t pos = avio_tell(pb);
01516 avio_wb32(pb, 0);
01517 ffio_wfourcc(pb, "mdia");
01518 mov_write_mdhd_tag(pb, track);
01519 mov_write_hdlr_tag(pb, track);
01520 mov_write_minf_tag(pb, track);
01521 return update_size(pb, pos);
01522 }
01523
01524
01525
01526
01527
01528 static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
01529 int16_t d, int16_t tx, int16_t ty)
01530 {
01531 avio_wb32(pb, a << 16);
01532 avio_wb32(pb, b << 16);
01533 avio_wb32(pb, 0);
01534 avio_wb32(pb, c << 16);
01535 avio_wb32(pb, d << 16);
01536 avio_wb32(pb, 0);
01537 avio_wb32(pb, tx << 16);
01538 avio_wb32(pb, ty << 16);
01539 avio_wb32(pb, 1 << 30);
01540 }
01541
01542 static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
01543 {
01544 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
01545 track->timescale, AV_ROUND_UP);
01546 int version = duration < INT32_MAX ? 0 : 1;
01547 int rotation = 0;
01548
01549 if (track->mode == MODE_ISM)
01550 version = 1;
01551
01552 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92);
01553 ffio_wfourcc(pb, "tkhd");
01554 avio_w8(pb, version);
01555 avio_wb24(pb, 0xf);
01556 if (version == 1) {
01557 avio_wb64(pb, track->time);
01558 avio_wb64(pb, track->time);
01559 } else {
01560 avio_wb32(pb, track->time);
01561 avio_wb32(pb, track->time);
01562 }
01563 avio_wb32(pb, track->track_id);
01564 avio_wb32(pb, 0);
01565 if (!track->entry)
01566 (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
01567 else
01568 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
01569
01570 avio_wb32(pb, 0);
01571 avio_wb32(pb, 0);
01572 avio_wb16(pb, 0);
01573 avio_wb16(pb, st ? st->codec->codec_type : 0);
01574
01575 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
01576 avio_wb16(pb, 0x0100);
01577 else
01578 avio_wb16(pb, 0);
01579 avio_wb16(pb, 0);
01580
01581
01582 if (st && st->metadata) {
01583 AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
01584 rotation = (rot && rot->value) ? atoi(rot->value) : 0;
01585 }
01586 if (rotation == 90) {
01587 write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
01588 } else if (rotation == 180) {
01589 write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
01590 } else if (rotation == 270) {
01591 write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
01592 } else {
01593 write_matrix(pb, 1, 0, 0, 1, 0, 0);
01594 }
01595
01596 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
01597 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
01598 if(track->mode == MODE_MOV) {
01599 avio_wb32(pb, track->enc->width << 16);
01600 avio_wb32(pb, track->height << 16);
01601 } else {
01602 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
01603 if(!sample_aspect_ratio || track->height != track->enc->height)
01604 sample_aspect_ratio = 1;
01605 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
01606 avio_wb32(pb, track->height*0x10000);
01607 }
01608 }
01609 else {
01610 avio_wb32(pb, 0);
01611 avio_wb32(pb, 0);
01612 }
01613 return 0x5c;
01614 }
01615
01616 static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
01617 {
01618 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
01619 track->enc->sample_aspect_ratio.den);
01620
01621 int64_t pos = avio_tell(pb);
01622
01623 avio_wb32(pb, 0);
01624 ffio_wfourcc(pb, "tapt");
01625
01626 avio_wb32(pb, 20);
01627 ffio_wfourcc(pb, "clef");
01628 avio_wb32(pb, 0);
01629 avio_wb32(pb, width << 16);
01630 avio_wb32(pb, track->enc->height << 16);
01631
01632 avio_wb32(pb, 20);
01633 ffio_wfourcc(pb, "enof");
01634 avio_wb32(pb, 0);
01635 avio_wb32(pb, track->enc->width << 16);
01636 avio_wb32(pb, track->enc->height << 16);
01637
01638 return update_size(pb, pos);
01639 }
01640
01641
01642 static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
01643 {
01644 int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
01645 track->timescale, AV_ROUND_UP);
01646 int version = duration < INT32_MAX ? 0 : 1;
01647 int entry_size, entry_count, size;
01648 int64_t delay, start_ct = track->cluster[0].cts;
01649 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
01650 track->timescale, AV_ROUND_DOWN);
01651 version |= delay < INT32_MAX ? 0 : 1;
01652
01653 entry_size = (version == 1) ? 20 : 12;
01654 entry_count = 1 + (delay > 0);
01655 size = 24 + entry_count * entry_size;
01656
01657
01658 avio_wb32(pb, size);
01659 ffio_wfourcc(pb, "edts");
01660 avio_wb32(pb, size - 8);
01661 ffio_wfourcc(pb, "elst");
01662 avio_w8(pb, version);
01663 avio_wb24(pb, 0);
01664
01665 avio_wb32(pb, entry_count);
01666 if (delay > 0) {
01667 if (version == 1) {
01668 avio_wb64(pb, delay);
01669 avio_wb64(pb, -1);
01670 } else {
01671 avio_wb32(pb, delay);
01672 avio_wb32(pb, -1);
01673 }
01674 avio_wb32(pb, 0x00010000);
01675 }
01676
01677
01678 if (version == 1) {
01679 avio_wb64(pb, duration);
01680 avio_wb64(pb, start_ct);
01681 } else {
01682 avio_wb32(pb, duration);
01683 avio_wb32(pb, start_ct);
01684 }
01685 avio_wb32(pb, 0x00010000);
01686 return size;
01687 }
01688
01689 static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
01690 {
01691 avio_wb32(pb, 20);
01692 ffio_wfourcc(pb, "tref");
01693 avio_wb32(pb, 12);
01694 avio_wl32(pb, track->tref_tag);
01695 avio_wb32(pb, track->tref_id);
01696 return 20;
01697 }
01698
01699
01700 static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
01701 {
01702 avio_wb32(pb, 0x34);
01703 ffio_wfourcc(pb, "uuid");
01704 ffio_wfourcc(pb, "USMT");
01705 avio_wb32(pb, 0x21d24fce);
01706 avio_wb32(pb, 0xbb88695c);
01707 avio_wb32(pb, 0xfac9c740);
01708 avio_wb32(pb, 0x1c);
01709 ffio_wfourcc(pb, "MTDT");
01710 avio_wb32(pb, 0x00010012);
01711 avio_wb32(pb, 0x0a);
01712 avio_wb32(pb, 0x55c40000);
01713 avio_wb32(pb, 0x1);
01714 avio_wb32(pb, 0x0);
01715 return 0x34;
01716 }
01717
01718 static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
01719 {
01720 char buf[1000] = "";
01721 int len;
01722
01723 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
01724 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
01725 len = strlen(buf);
01726
01727 avio_wb32(pb, len + 24);
01728 ffio_wfourcc(pb, "udta");
01729 avio_wb32(pb, len + 16);
01730 ffio_wfourcc(pb, "hnti");
01731 avio_wb32(pb, len + 8);
01732 ffio_wfourcc(pb, "sdp ");
01733 avio_write(pb, buf, len);
01734 return len + 24;
01735 }
01736
01737 static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
01738 MOVTrack *track, AVStream *st)
01739 {
01740 int64_t pos = avio_tell(pb);
01741 avio_wb32(pb, 0);
01742 ffio_wfourcc(pb, "trak");
01743 mov_write_tkhd_tag(pb, track, st);
01744 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
01745 mov_write_edts_tag(pb, track);
01746 if (track->tref_tag)
01747 mov_write_tref_tag(pb, track);
01748 mov_write_mdia_tag(pb, track);
01749 if (track->mode == MODE_PSP)
01750 mov_write_uuid_tag_psp(pb,track);
01751 if (track->tag == MKTAG('r','t','p',' '))
01752 mov_write_udta_sdp(pb, track->rtp_ctx, track->track_id);
01753 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
01754 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
01755 if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
01756 mov_write_tapt_tag(pb, track);
01757 };
01758 return update_size(pb, pos);
01759 }
01760
01761 static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
01762 {
01763 int i, has_audio = 0, has_video = 0;
01764 int64_t pos = avio_tell(pb);
01765 int audio_profile = mov->iods_audio_profile;
01766 int video_profile = mov->iods_video_profile;
01767 for (i = 0; i < mov->nb_streams; i++) {
01768 if(mov->tracks[i].entry > 0) {
01769 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
01770 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
01771 }
01772 }
01773 if (audio_profile < 0)
01774 audio_profile = 0xFF - has_audio;
01775 if (video_profile < 0)
01776 video_profile = 0xFF - has_video;
01777 avio_wb32(pb, 0x0);
01778 ffio_wfourcc(pb, "iods");
01779 avio_wb32(pb, 0);
01780 put_descr(pb, 0x10, 7);
01781 avio_wb16(pb, 0x004f);
01782 avio_w8(pb, 0xff);
01783 avio_w8(pb, 0xff);
01784 avio_w8(pb, audio_profile);
01785 avio_w8(pb, video_profile);
01786 avio_w8(pb, 0xff);
01787 return update_size(pb, pos);
01788 }
01789
01790 static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
01791 {
01792 avio_wb32(pb, 0x20);
01793 ffio_wfourcc(pb, "trex");
01794 avio_wb32(pb, 0);
01795 avio_wb32(pb, track->track_id);
01796 avio_wb32(pb, 1);
01797 avio_wb32(pb, 0);
01798 avio_wb32(pb, 0);
01799 avio_wb32(pb, 0);
01800 return 0;
01801 }
01802
01803 static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
01804 {
01805 int64_t pos = avio_tell(pb);
01806 int i;
01807 avio_wb32(pb, 0x0);
01808 ffio_wfourcc(pb, "mvex");
01809 for (i = 0; i < mov->nb_streams; i++)
01810 mov_write_trex_tag(pb, &mov->tracks[i]);
01811 return update_size(pb, pos);
01812 }
01813
01814 static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
01815 {
01816 int max_track_id = 1, i;
01817 int64_t max_track_len_temp, max_track_len = 0;
01818 int version;
01819
01820 for (i=0; i<mov->nb_streams; i++) {
01821 if(mov->tracks[i].entry > 0) {
01822 max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
01823 MOV_TIMESCALE,
01824 mov->tracks[i].timescale,
01825 AV_ROUND_UP);
01826 if (max_track_len < max_track_len_temp)
01827 max_track_len = max_track_len_temp;
01828 if (max_track_id < mov->tracks[i].track_id)
01829 max_track_id = mov->tracks[i].track_id;
01830 }
01831 }
01832
01833 version = max_track_len < UINT32_MAX ? 0 : 1;
01834 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108);
01835 ffio_wfourcc(pb, "mvhd");
01836 avio_w8(pb, version);
01837 avio_wb24(pb, 0);
01838 if (version == 1) {
01839 avio_wb64(pb, mov->time);
01840 avio_wb64(pb, mov->time);
01841 } else {
01842 avio_wb32(pb, mov->time);
01843 avio_wb32(pb, mov->time);
01844 }
01845 avio_wb32(pb, MOV_TIMESCALE);
01846 (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len);
01847
01848 avio_wb32(pb, 0x00010000);
01849 avio_wb16(pb, 0x0100);
01850 avio_wb16(pb, 0);
01851 avio_wb32(pb, 0);
01852 avio_wb32(pb, 0);
01853
01854
01855 write_matrix(pb, 1, 0, 0, 1, 0, 0);
01856
01857 avio_wb32(pb, 0);
01858 avio_wb32(pb, 0);
01859 avio_wb32(pb, 0);
01860 avio_wb32(pb, 0);
01861 avio_wb32(pb, 0);
01862 avio_wb32(pb, 0);
01863 avio_wb32(pb, max_track_id + 1);
01864 return 0x6c;
01865 }
01866
01867 static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
01868 AVFormatContext *s)
01869 {
01870 avio_wb32(pb, 33);
01871 ffio_wfourcc(pb, "hdlr");
01872 avio_wb32(pb, 0);
01873 avio_wb32(pb, 0);
01874 ffio_wfourcc(pb, "mdir");
01875 ffio_wfourcc(pb, "appl");
01876 avio_wb32(pb, 0);
01877 avio_wb32(pb, 0);
01878 avio_w8(pb, 0);
01879 return 33;
01880 }
01881
01882
01883 static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
01884 {
01885 if(long_style){
01886 int size = 16 + strlen(data);
01887 avio_wb32(pb, size);
01888 ffio_wfourcc(pb, "data");
01889 avio_wb32(pb, 1);
01890 avio_wb32(pb, 0);
01891 avio_write(pb, data, strlen(data));
01892 return size;
01893 }else{
01894 if (!lang)
01895 lang = ff_mov_iso639_to_lang("und", 1);
01896 avio_wb16(pb, strlen(data));
01897 avio_wb16(pb, lang);
01898 avio_write(pb, data, strlen(data));
01899 return strlen(data) + 4;
01900 }
01901 }
01902
01903 static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
01904 int size = 0;
01905 if (value && value[0]) {
01906 int64_t pos = avio_tell(pb);
01907 avio_wb32(pb, 0);
01908 ffio_wfourcc(pb, name);
01909 mov_write_string_data_tag(pb, value, lang, long_style);
01910 size = update_size(pb, pos);
01911 }
01912 return size;
01913 }
01914
01915 static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
01916 const char *name, const char *tag,
01917 int long_style)
01918 {
01919 int l, lang = 0, len, len2;
01920 AVDictionaryEntry *t, *t2 = NULL;
01921 char tag2[16];
01922
01923 if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
01924 return 0;
01925
01926 len = strlen(t->key);
01927 snprintf(tag2, sizeof(tag2), "%s-", tag);
01928 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
01929 len2 = strlen(t2->key);
01930 if (len2 == len+4 && !strcmp(t->value, t2->value)
01931 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
01932 lang = l;
01933 break;
01934 }
01935 }
01936 return mov_write_string_tag(pb, name, t->value, lang, long_style);
01937 }
01938
01939
01940 static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
01941 {
01942 AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
01943 int size = 0, tmpo = t ? atoi(t->value) : 0;
01944 if (tmpo) {
01945 size = 26;
01946 avio_wb32(pb, size);
01947 ffio_wfourcc(pb, "tmpo");
01948 avio_wb32(pb, size-8);
01949 ffio_wfourcc(pb, "data");
01950 avio_wb32(pb, 0x15);
01951 avio_wb32(pb, 0);
01952 avio_wb16(pb, tmpo);
01953 }
01954 return size;
01955 }
01956
01957
01958 static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
01959 AVFormatContext *s)
01960 {
01961 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
01962 int size = 0, track = t ? atoi(t->value) : 0;
01963 if (track) {
01964 avio_wb32(pb, 32);
01965 ffio_wfourcc(pb, "trkn");
01966 avio_wb32(pb, 24);
01967 ffio_wfourcc(pb, "data");
01968 avio_wb32(pb, 0);
01969 avio_wb32(pb, 0);
01970 avio_wb16(pb, 0);
01971 avio_wb16(pb, track);
01972 avio_wb16(pb, 0);
01973 avio_wb16(pb, 0);
01974 size = 32;
01975 }
01976 return size;
01977 }
01978
01979
01980 static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
01981 AVFormatContext *s)
01982 {
01983 int64_t pos = avio_tell(pb);
01984 avio_wb32(pb, 0);
01985 ffio_wfourcc(pb, "ilst");
01986 mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
01987 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
01988 mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
01989 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
01990 mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
01991 mov_write_string_metadata(s, pb, "\251day", "date" , 1);
01992 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
01993 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
01994 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
01995 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
01996 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
01997 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
01998 mov_write_string_metadata(s, pb, "desc", "description",1);
01999 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
02000 mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
02001 mov_write_string_metadata(s, pb, "tven", "episode_id",1);
02002 mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
02003 mov_write_trkn_tag(pb, mov, s);
02004 mov_write_tmpo_tag(pb, s);
02005 return update_size(pb, pos);
02006 }
02007
02008
02009 static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
02010 AVFormatContext *s)
02011 {
02012 int size = 0;
02013 int64_t pos = avio_tell(pb);
02014 avio_wb32(pb, 0);
02015 ffio_wfourcc(pb, "meta");
02016 avio_wb32(pb, 0);
02017 mov_write_itunes_hdlr_tag(pb, mov, s);
02018 mov_write_ilst_tag(pb, mov, s);
02019 size = update_size(pb, pos);
02020 return size;
02021 }
02022
02023 static int utf8len(const uint8_t *b)
02024 {
02025 int len=0;
02026 int val;
02027 while(*b){
02028 GET_UTF8(val, *b++, return -1;)
02029 len++;
02030 }
02031 return len;
02032 }
02033
02034 static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
02035 {
02036 int val;
02037 while(*b){
02038 GET_UTF8(val, *b++, return -1;)
02039 avio_wb16(pb, val);
02040 }
02041 avio_wb16(pb, 0x00);
02042 return 0;
02043 }
02044
02045 static uint16_t language_code(const char *str)
02046 {
02047 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
02048 }
02049
02050 static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
02051 const char *tag, const char *str)
02052 {
02053 int64_t pos = avio_tell(pb);
02054 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
02055 if (!t || !utf8len(t->value))
02056 return 0;
02057 avio_wb32(pb, 0);
02058 ffio_wfourcc(pb, tag);
02059 avio_wb32(pb, 0);
02060 if (!strcmp(tag, "yrrc"))
02061 avio_wb16(pb, atoi(t->value));
02062 else {
02063 avio_wb16(pb, language_code("eng"));
02064 avio_write(pb, t->value, strlen(t->value)+1);
02065 if (!strcmp(tag, "albm") &&
02066 (t = av_dict_get(s->metadata, "track", NULL, 0)))
02067 avio_w8(pb, atoi(t->value));
02068 }
02069 return update_size(pb, pos);
02070 }
02071
02072 static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
02073 {
02074 int64_t pos = avio_tell(pb);
02075 int i, nb_chapters = FFMIN(s->nb_chapters, 255);
02076
02077 avio_wb32(pb, 0);
02078 ffio_wfourcc(pb, "chpl");
02079 avio_wb32(pb, 0x01000000);
02080 avio_wb32(pb, 0);
02081 avio_w8(pb, nb_chapters);
02082
02083 for (i = 0; i < nb_chapters; i++) {
02084 AVChapter *c = s->chapters[i];
02085 AVDictionaryEntry *t;
02086 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
02087
02088 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
02089 int len = FFMIN(strlen(t->value), 255);
02090 avio_w8(pb, len);
02091 avio_write(pb, t->value, len);
02092 } else
02093 avio_w8(pb, 0);
02094 }
02095 return update_size(pb, pos);
02096 }
02097
02098 static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
02099 AVFormatContext *s)
02100 {
02101 AVIOContext *pb_buf;
02102 int i, ret, size;
02103 uint8_t *buf;
02104
02105 for (i = 0; i < s->nb_streams; i++)
02106 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
02107 return 0;
02108 }
02109
02110 ret = avio_open_dyn_buf(&pb_buf);
02111 if(ret < 0)
02112 return ret;
02113
02114 if (mov->mode & MODE_3GP) {
02115 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
02116 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
02117 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
02118 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
02119 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
02120 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
02121 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
02122 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
02123 } else if (mov->mode == MODE_MOV) {
02124 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
02125 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
02126 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
02127 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
02128 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
02129 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
02130
02131 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
02132
02133 mov_write_string_metadata(s, pb_buf, "\251cmt", "comment" , 0);
02134 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
02135 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
02136 } else {
02137
02138 mov_write_meta_tag(pb_buf, mov, s);
02139 }
02140
02141 if (s->nb_chapters)
02142 mov_write_chpl_tag(pb_buf, s);
02143
02144 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
02145 avio_wb32(pb, size+8);
02146 ffio_wfourcc(pb, "udta");
02147 avio_write(pb, buf, size);
02148 }
02149 av_free(buf);
02150
02151 return 0;
02152 }
02153
02154 static void mov_write_psp_udta_tag(AVIOContext *pb,
02155 const char *str, const char *lang, int type)
02156 {
02157 int len = utf8len(str)+1;
02158 if(len<=0)
02159 return;
02160 avio_wb16(pb, len*2+10);
02161 avio_wb32(pb, type);
02162 avio_wb16(pb, language_code(lang));
02163 avio_wb16(pb, 0x01);
02164 ascii_to_wc(pb, str);
02165 }
02166
02167 static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
02168 {
02169 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
02170 int64_t pos, pos2;
02171
02172 if (title) {
02173 pos = avio_tell(pb);
02174 avio_wb32(pb, 0);
02175 ffio_wfourcc(pb, "uuid");
02176 ffio_wfourcc(pb, "USMT");
02177 avio_wb32(pb, 0x21d24fce);
02178 avio_wb32(pb, 0xbb88695c);
02179 avio_wb32(pb, 0xfac9c740);
02180
02181 pos2 = avio_tell(pb);
02182 avio_wb32(pb, 0);
02183 ffio_wfourcc(pb, "MTDT");
02184 avio_wb16(pb, 4);
02185
02186
02187 avio_wb16(pb, 0x0C);
02188 avio_wb32(pb, 0x0B);
02189 avio_wb16(pb, language_code("und"));
02190 avio_wb16(pb, 0x0);
02191 avio_wb16(pb, 0x021C);
02192
02193 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
02194 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
02195
02196 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
02197
02198 update_size(pb, pos2);
02199 return update_size(pb, pos);
02200 }
02201
02202 return 0;
02203 }
02204
02205 static void build_chunks(MOVTrack *trk)
02206 {
02207 int i;
02208 MOVIentry *chunk= &trk->cluster[0];
02209 uint64_t chunkSize = chunk->size;
02210 chunk->chunkNum= 1;
02211 if (trk->chunkCount)
02212 return;
02213 trk->chunkCount= 1;
02214 for(i=1; i<trk->entry; i++){
02215 if(chunk->pos + chunkSize == trk->cluster[i].pos &&
02216 chunkSize + trk->cluster[i].size < (1<<20)){
02217 chunkSize += trk->cluster[i].size;
02218 chunk->samples_in_chunk += trk->cluster[i].entries;
02219 }else{
02220 trk->cluster[i].chunkNum = chunk->chunkNum+1;
02221 chunk=&trk->cluster[i];
02222 chunkSize = chunk->size;
02223 trk->chunkCount++;
02224 }
02225 }
02226 }
02227
02228 static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
02229 AVFormatContext *s)
02230 {
02231 int i;
02232 int64_t pos = avio_tell(pb);
02233 avio_wb32(pb, 0);
02234 ffio_wfourcc(pb, "moov");
02235
02236 for (i=0; i<mov->nb_streams; i++) {
02237 if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
02238 continue;
02239
02240 mov->tracks[i].time = mov->time;
02241 mov->tracks[i].track_id = i+1;
02242
02243 if (mov->tracks[i].entry)
02244 build_chunks(&mov->tracks[i]);
02245 }
02246
02247 if (mov->chapter_track)
02248 for (i=0; i<s->nb_streams; i++) {
02249 mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
02250 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
02251 }
02252 for (i = 0; i < mov->nb_streams; i++) {
02253 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
02254 mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
02255 mov->tracks[i].tref_id =
02256 mov->tracks[mov->tracks[i].src_track].track_id;
02257 }
02258 }
02259 for (i = 0; i < mov->nb_streams; i++) {
02260 if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
02261 int src_trk = mov->tracks[i].src_track;
02262 mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
02263 mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
02264 mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
02265 }
02266 }
02267
02268 mov_write_mvhd_tag(pb, mov);
02269 if (mov->mode != MODE_MOV && !mov->iods_skip)
02270 mov_write_iods_tag(pb, mov);
02271 for (i=0; i<mov->nb_streams; i++) {
02272 if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
02273 mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
02274 }
02275 }
02276 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
02277 mov_write_mvex_tag(pb, mov);
02278
02279 if (mov->mode == MODE_PSP)
02280 mov_write_uuidusmt_tag(pb, s);
02281 else
02282 mov_write_udta_tag(pb, mov, s);
02283
02284 return update_size(pb, pos);
02285 }
02286
02287 static void param_write_int(AVIOContext *pb, const char *name, int value)
02288 {
02289 avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
02290 }
02291
02292 static void param_write_string(AVIOContext *pb, const char *name, const char *value)
02293 {
02294 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
02295 }
02296
02297 static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
02298 {
02299 char buf[150];
02300 len = FFMIN(sizeof(buf)/2 - 1, len);
02301 ff_data_to_hex(buf, value, len, 0);
02302 buf[2*len] = '\0';
02303 avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
02304 }
02305
02306 static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
02307 {
02308 int64_t pos = avio_tell(pb);
02309 int i;
02310 const uint8_t uuid[] = {
02311 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
02312 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
02313 };
02314
02315 avio_wb32(pb, 0);
02316 ffio_wfourcc(pb, "uuid");
02317 avio_write(pb, uuid, sizeof(uuid));
02318 avio_wb32(pb, 0);
02319
02320 avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
02321 avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
02322 avio_printf(pb, "<head>\n");
02323 avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
02324 LIBAVFORMAT_IDENT);
02325 avio_printf(pb, "</head>\n");
02326 avio_printf(pb, "<body>\n");
02327 avio_printf(pb, "<switch>\n");
02328 for (i = 0; i < mov->nb_streams; i++) {
02329 MOVTrack *track = &mov->tracks[i];
02330 const char *type;
02331
02332
02333 int track_id = i + 1;
02334
02335 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
02336 type = "video";
02337 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
02338 type = "audio";
02339 } else {
02340 continue;
02341 }
02342 avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
02343 track->enc->bit_rate);
02344 param_write_int(pb, "systemBitrate", track->enc->bit_rate);
02345 param_write_int(pb, "trackID", track_id);
02346 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
02347 if (track->enc->codec_id == AV_CODEC_ID_H264) {
02348 uint8_t *ptr;
02349 int size = track->enc->extradata_size;
02350 if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
02351 &size)) {
02352 param_write_hex(pb, "CodecPrivateData",
02353 ptr ? ptr : track->enc->extradata,
02354 size);
02355 av_free(ptr);
02356 }
02357 param_write_string(pb, "FourCC", "H264");
02358 } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
02359 param_write_string(pb, "FourCC", "WVC1");
02360 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
02361 track->enc->extradata_size);
02362 }
02363 param_write_int(pb, "MaxWidth", track->enc->width);
02364 param_write_int(pb, "MaxHeight", track->enc->height);
02365 param_write_int(pb, "DisplayWidth", track->enc->width);
02366 param_write_int(pb, "DisplayHeight", track->enc->height);
02367 } else {
02368 if (track->enc->codec_id == AV_CODEC_ID_AAC) {
02369 param_write_string(pb, "FourCC", "AACL");
02370 } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
02371 param_write_string(pb, "FourCC", "WMAP");
02372 }
02373 param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
02374 track->enc->extradata_size);
02375 param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
02376 track->enc->codec_id));
02377 param_write_int(pb, "Channels", track->enc->channels);
02378 param_write_int(pb, "SamplingRate", track->enc->sample_rate);
02379 param_write_int(pb, "BitsPerSample", 16);
02380 param_write_int(pb, "PacketSize", track->enc->block_align ?
02381 track->enc->block_align : 4);
02382 }
02383 avio_printf(pb, "</%s>\n", type);
02384 }
02385 avio_printf(pb, "</switch>\n");
02386 avio_printf(pb, "</body>\n");
02387 avio_printf(pb, "</smil>\n");
02388
02389 return update_size(pb, pos);
02390 }
02391
02392 static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
02393 {
02394 avio_wb32(pb, 16);
02395 ffio_wfourcc(pb, "mfhd");
02396 avio_wb32(pb, 0);
02397 avio_wb32(pb, mov->fragments);
02398 return 0;
02399 }
02400
02401 static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
02402 int64_t moof_offset)
02403 {
02404 int64_t pos = avio_tell(pb);
02405 uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
02406 MOV_TFHD_BASE_DATA_OFFSET;
02407 if (!track->entry) {
02408 flags |= MOV_TFHD_DURATION_IS_EMPTY;
02409 } else {
02410 flags |= MOV_TFHD_DEFAULT_FLAGS;
02411 }
02412
02413
02414
02415
02416 if (track->mode == MODE_ISM)
02417 flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION);
02418
02419 avio_wb32(pb, 0);
02420 ffio_wfourcc(pb, "tfhd");
02421 avio_w8(pb, 0);
02422 avio_wb24(pb, flags);
02423
02424 avio_wb32(pb, track->track_id);
02425 if (flags & MOV_TFHD_BASE_DATA_OFFSET)
02426 avio_wb64(pb, moof_offset);
02427 if (flags & MOV_TFHD_DEFAULT_DURATION) {
02428 track->default_duration = get_cluster_duration(track, 0);
02429 avio_wb32(pb, track->default_duration);
02430 }
02431 if (flags & MOV_TFHD_DEFAULT_SIZE) {
02432 track->default_size = track->entry ? track->cluster[0].size : 1;
02433 avio_wb32(pb, track->default_size);
02434 } else
02435 track->default_size = -1;
02436
02437 if (flags & MOV_TFHD_DEFAULT_FLAGS) {
02438 track->default_sample_flags =
02439 track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
02440 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
02441 MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
02442 avio_wb32(pb, track->default_sample_flags);
02443 }
02444
02445 return update_size(pb, pos);
02446 }
02447
02448 static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
02449 {
02450 return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
02451 (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
02452 }
02453
02454 static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
02455 {
02456 int64_t pos = avio_tell(pb);
02457 uint32_t flags = MOV_TRUN_DATA_OFFSET;
02458 int i;
02459
02460 for (i = 0; i < track->entry; i++) {
02461 if (get_cluster_duration(track, i) != track->default_duration)
02462 flags |= MOV_TRUN_SAMPLE_DURATION;
02463 if (track->cluster[i].size != track->default_size)
02464 flags |= MOV_TRUN_SAMPLE_SIZE;
02465 if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
02466 flags |= MOV_TRUN_SAMPLE_FLAGS;
02467 }
02468 if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
02469 flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
02470 if (track->flags & MOV_TRACK_CTTS)
02471 flags |= MOV_TRUN_SAMPLE_CTS;
02472
02473 avio_wb32(pb, 0);
02474 ffio_wfourcc(pb, "trun");
02475 avio_w8(pb, 0);
02476 avio_wb24(pb, flags);
02477
02478 avio_wb32(pb, track->entry);
02479 track->moof_size_offset = avio_tell(pb);
02480 avio_wb32(pb, 0);
02481 if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
02482 avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
02483
02484 for (i = 0; i < track->entry; i++) {
02485 if (flags & MOV_TRUN_SAMPLE_DURATION)
02486 avio_wb32(pb, get_cluster_duration(track, i));
02487 if (flags & MOV_TRUN_SAMPLE_SIZE)
02488 avio_wb32(pb, track->cluster[i].size);
02489 if (flags & MOV_TRUN_SAMPLE_FLAGS)
02490 avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
02491 if (flags & MOV_TRUN_SAMPLE_CTS)
02492 avio_wb32(pb, track->cluster[i].cts);
02493 }
02494
02495 return update_size(pb, pos);
02496 }
02497
02498 static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
02499 {
02500 int64_t pos = avio_tell(pb);
02501 const uint8_t uuid[] = {
02502 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
02503 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
02504 };
02505
02506 avio_wb32(pb, 0);
02507 ffio_wfourcc(pb, "uuid");
02508 avio_write(pb, uuid, sizeof(uuid));
02509 avio_w8(pb, 1);
02510 avio_wb24(pb, 0);
02511 avio_wb64(pb, track->frag_start);
02512 avio_wb64(pb, track->start_dts + track->track_duration -
02513 track->cluster[0].dts);
02514
02515 return update_size(pb, pos);
02516 }
02517
02518 static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
02519 MOVTrack *track, int entry)
02520 {
02521 int n = track->nb_frag_info - 1 - entry, i;
02522 int size = 8 + 16 + 4 + 1 + 16*n;
02523 const uint8_t uuid[] = {
02524 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
02525 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
02526 };
02527
02528 if (entry < 0)
02529 return 0;
02530
02531 avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
02532 avio_wb32(pb, size);
02533 ffio_wfourcc(pb, "uuid");
02534 avio_write(pb, uuid, sizeof(uuid));
02535 avio_w8(pb, 1);
02536 avio_wb24(pb, 0);
02537 avio_w8(pb, n);
02538 for (i = 0; i < n; i++) {
02539 int index = entry + 1 + i;
02540 avio_wb64(pb, track->frag_info[index].time);
02541 avio_wb64(pb, track->frag_info[index].duration);
02542 }
02543 if (n < mov->ism_lookahead) {
02544 int free_size = 16*(mov->ism_lookahead - n);
02545 avio_wb32(pb, free_size);
02546 ffio_wfourcc(pb, "free");
02547 for (i = 0; i < free_size - 8; i++)
02548 avio_w8(pb, 0);
02549 }
02550
02551 return 0;
02552 }
02553
02554 static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
02555 MOVTrack *track)
02556 {
02557 int64_t pos = avio_tell(pb);
02558 int i;
02559 for (i = 0; i < mov->ism_lookahead; i++) {
02560
02561
02562 mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
02563 }
02564 avio_seek(pb, pos, SEEK_SET);
02565 return 0;
02566 }
02567
02568 static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
02569 MOVTrack *track, int64_t moof_offset)
02570 {
02571 int64_t pos = avio_tell(pb);
02572 avio_wb32(pb, 0);
02573 ffio_wfourcc(pb, "traf");
02574
02575 mov_write_tfhd_tag(pb, track, moof_offset);
02576 mov_write_trun_tag(pb, track);
02577 if (mov->mode == MODE_ISM) {
02578 mov_write_tfxd_tag(pb, track);
02579
02580 if (mov->ism_lookahead) {
02581 int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
02582
02583 track->tfrf_offset = avio_tell(pb);
02584 avio_wb32(pb, 8 + size);
02585 ffio_wfourcc(pb, "free");
02586 for (i = 0; i < size; i++)
02587 avio_w8(pb, 0);
02588 }
02589 }
02590
02591 return update_size(pb, pos);
02592 }
02593
02594 static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
02595 {
02596 int64_t pos = avio_tell(pb), end;
02597 int i, moof_size;
02598
02599 avio_wb32(pb, 0);
02600 ffio_wfourcc(pb, "moof");
02601
02602 mov_write_mfhd_tag(pb, mov);
02603 for (i = 0; i < mov->nb_streams; i++) {
02604 MOVTrack *track = &mov->tracks[i];
02605 if (tracks >= 0 && i != tracks)
02606 continue;
02607 if (!track->entry)
02608 continue;
02609 mov_write_traf_tag(pb, mov, track, pos);
02610 }
02611
02612 end = avio_tell(pb);
02613 moof_size = end - pos;
02614 for (i = 0; i < mov->nb_streams; i++) {
02615 MOVTrack *track = &mov->tracks[i];
02616 if (tracks >= 0 && i != tracks)
02617 continue;
02618 if (!track->entry)
02619 continue;
02620 avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
02621 avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
02622 }
02623 avio_seek(pb, end, SEEK_SET);
02624
02625 return update_size(pb, pos);
02626 }
02627
02628 static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
02629 {
02630 int64_t pos = avio_tell(pb);
02631 int i;
02632
02633 avio_wb32(pb, 0);
02634 ffio_wfourcc(pb, "tfra");
02635 avio_w8(pb, 1);
02636 avio_wb24(pb, 0);
02637
02638 avio_wb32(pb, track->track_id);
02639 avio_wb32(pb, 0);
02640 avio_wb32(pb, track->nb_frag_info);
02641 for (i = 0; i < track->nb_frag_info; i++) {
02642 avio_wb64(pb, track->frag_info[i].time);
02643 avio_wb64(pb, track->frag_info[i].offset);
02644 avio_w8(pb, 1);
02645 avio_w8(pb, 1);
02646 avio_w8(pb, 1);
02647 }
02648
02649 return update_size(pb, pos);
02650 }
02651
02652 static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
02653 {
02654 int64_t pos = avio_tell(pb);
02655 int i;
02656
02657 avio_wb32(pb, 0);
02658 ffio_wfourcc(pb, "mfra");
02659
02660
02661 if (mov->flags & FF_MOV_FLAG_ISML)
02662 return update_size(pb, pos);
02663
02664 for (i = 0; i < mov->nb_streams; i++) {
02665 MOVTrack *track = &mov->tracks[i];
02666 if (track->nb_frag_info)
02667 mov_write_tfra_tag(pb, track);
02668 }
02669
02670 avio_wb32(pb, 16);
02671 ffio_wfourcc(pb, "mfro");
02672 avio_wb32(pb, 0);
02673 avio_wb32(pb, avio_tell(pb) + 4 - pos);
02674
02675 return update_size(pb, pos);
02676 }
02677
02678 static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
02679 {
02680 avio_wb32(pb, 8);
02681 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
02682
02683 mov->mdat_pos = avio_tell(pb);
02684 avio_wb32(pb, 0);
02685 ffio_wfourcc(pb, "mdat");
02686 return 0;
02687 }
02688
02689
02690 static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
02691 {
02692 MOVMuxContext *mov = s->priv_data;
02693 int64_t pos = avio_tell(pb);
02694 int has_h264 = 0, has_video = 0;
02695 int minor = 0x200;
02696 int i;
02697
02698 for (i = 0; i < s->nb_streams; i++) {
02699 AVStream *st = s->streams[i];
02700 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
02701 has_video = 1;
02702 if (st->codec->codec_id == AV_CODEC_ID_H264)
02703 has_h264 = 1;
02704 }
02705
02706 avio_wb32(pb, 0);
02707 ffio_wfourcc(pb, "ftyp");
02708
02709 if (mov->mode == MODE_3GP) {
02710 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
02711 minor = has_h264 ? 0x100 : 0x200;
02712 } else if (mov->mode & MODE_3G2) {
02713 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
02714 minor = has_h264 ? 0x20000 : 0x10000;
02715 }else if (mov->mode == MODE_PSP)
02716 ffio_wfourcc(pb, "MSNV");
02717 else if (mov->mode == MODE_MP4)
02718 ffio_wfourcc(pb, "isom");
02719 else if (mov->mode == MODE_IPOD)
02720 ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
02721 else if (mov->mode == MODE_ISM)
02722 ffio_wfourcc(pb, "isml");
02723 else if (mov->mode == MODE_F4V)
02724 ffio_wfourcc(pb, "f4v ");
02725 else
02726 ffio_wfourcc(pb, "qt ");
02727
02728 avio_wb32(pb, minor);
02729
02730 if(mov->mode == MODE_MOV)
02731 ffio_wfourcc(pb, "qt ");
02732 else if (mov->mode == MODE_ISM) {
02733 ffio_wfourcc(pb, "piff");
02734 ffio_wfourcc(pb, "iso2");
02735 } else {
02736 ffio_wfourcc(pb, "isom");
02737 ffio_wfourcc(pb, "iso2");
02738 if(has_h264)
02739 ffio_wfourcc(pb, "avc1");
02740 }
02741
02742 if (mov->mode == MODE_3GP)
02743 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
02744 else if (mov->mode & MODE_3G2)
02745 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
02746 else if (mov->mode == MODE_PSP)
02747 ffio_wfourcc(pb, "MSNV");
02748 else if (mov->mode == MODE_MP4)
02749 ffio_wfourcc(pb, "mp41");
02750 return update_size(pb, pos);
02751 }
02752
02753 static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
02754 {
02755 AVCodecContext *video_codec = s->streams[0]->codec;
02756 AVCodecContext *audio_codec = s->streams[1]->codec;
02757 int audio_rate = audio_codec->sample_rate;
02758 int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
02759 int audio_kbitrate = audio_codec->bit_rate / 1000;
02760 int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
02761
02762 avio_wb32(pb, 0x94);
02763 ffio_wfourcc(pb, "uuid");
02764 ffio_wfourcc(pb, "PROF");
02765
02766 avio_wb32(pb, 0x21d24fce);
02767 avio_wb32(pb, 0xbb88695c);
02768 avio_wb32(pb, 0xfac9c740);
02769
02770 avio_wb32(pb, 0x0);
02771 avio_wb32(pb, 0x3);
02772
02773 avio_wb32(pb, 0x14);
02774 ffio_wfourcc(pb, "FPRF");
02775 avio_wb32(pb, 0x0);
02776 avio_wb32(pb, 0x0);
02777 avio_wb32(pb, 0x0);
02778
02779 avio_wb32(pb, 0x2c);
02780 ffio_wfourcc(pb, "APRF");
02781 avio_wb32(pb, 0x0);
02782 avio_wb32(pb, 0x2);
02783 ffio_wfourcc(pb, "mp4a");
02784 avio_wb32(pb, 0x20f);
02785 avio_wb32(pb, 0x0);
02786 avio_wb32(pb, audio_kbitrate);
02787 avio_wb32(pb, audio_kbitrate);
02788 avio_wb32(pb, audio_rate);
02789 avio_wb32(pb, audio_codec->channels);
02790
02791 avio_wb32(pb, 0x34);
02792 ffio_wfourcc(pb, "VPRF");
02793 avio_wb32(pb, 0x0);
02794 avio_wb32(pb, 0x1);
02795 if (video_codec->codec_id == AV_CODEC_ID_H264) {
02796 ffio_wfourcc(pb, "avc1");
02797 avio_wb16(pb, 0x014D);
02798 avio_wb16(pb, 0x0015);
02799 } else {
02800 ffio_wfourcc(pb, "mp4v");
02801 avio_wb16(pb, 0x0000);
02802 avio_wb16(pb, 0x0103);
02803 }
02804 avio_wb32(pb, 0x0);
02805 avio_wb32(pb, video_kbitrate);
02806 avio_wb32(pb, video_kbitrate);
02807 avio_wb32(pb, frame_rate);
02808 avio_wb32(pb, frame_rate);
02809 avio_wb16(pb, video_codec->width);
02810 avio_wb16(pb, video_codec->height);
02811 avio_wb32(pb, 0x010001);
02812 }
02813
02814 static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
02815 {
02816 uint32_t c = -1;
02817 int i, closed_gop = 0;
02818
02819 for (i = 0; i < pkt->size - 4; i++) {
02820 c = (c<<8) + pkt->data[i];
02821 if (c == 0x1b8) {
02822 closed_gop = pkt->data[i+4]>>6 & 0x01;
02823 } else if (c == 0x100) {
02824 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
02825 if (!temp_ref || closed_gop)
02826 *flags = MOV_SYNC_SAMPLE;
02827 else
02828 *flags = MOV_PARTIAL_SYNC_SAMPLE;
02829 break;
02830 }
02831 }
02832 return 0;
02833 }
02834
02835 static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
02836 {
02837 const uint8_t *start, *next, *end = pkt->data + pkt->size;
02838 int seq = 0, entry = 0;
02839 int key = pkt->flags & AV_PKT_FLAG_KEY;
02840 start = find_next_marker(pkt->data, end);
02841 for (next = start; next < end; start = next) {
02842 next = find_next_marker(start + 4, end);
02843 switch (AV_RB32(start)) {
02844 case VC1_CODE_SEQHDR:
02845 seq = 1;
02846 break;
02847 case VC1_CODE_ENTRYPOINT:
02848 entry = 1;
02849 break;
02850 case VC1_CODE_SLICE:
02851 trk->vc1_info.slices = 1;
02852 break;
02853 }
02854 }
02855 if (!trk->entry && !fragment) {
02856
02857 trk->vc1_info.first_packet_seq = seq;
02858 trk->vc1_info.first_packet_entry = entry;
02859 } else if ((seq && !trk->vc1_info.packet_seq) ||
02860 (entry && !trk->vc1_info.packet_entry)) {
02861 int i;
02862 for (i = 0; i < trk->entry; i++)
02863 trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
02864 trk->has_keyframes = 0;
02865 if (seq)
02866 trk->vc1_info.packet_seq = 1;
02867 if (entry)
02868 trk->vc1_info.packet_entry = 1;
02869 if (!fragment) {
02870
02871 if ((!seq || trk->vc1_info.first_packet_seq) &&
02872 (!entry || trk->vc1_info.first_packet_entry)) {
02873
02874
02875 trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
02876 trk->has_keyframes = 1;
02877 }
02878 }
02879 }
02880 if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
02881 key = seq && entry;
02882 else if (trk->vc1_info.packet_seq)
02883 key = seq;
02884 else if (trk->vc1_info.packet_entry)
02885 key = entry;
02886 if (key) {
02887 trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
02888 trk->has_keyframes++;
02889 }
02890 }
02891
02892 static int get_moov_size(AVFormatContext *s)
02893 {
02894 int ret;
02895 uint8_t *buf;
02896 AVIOContext *moov_buf;
02897 MOVMuxContext *mov = s->priv_data;
02898
02899 if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
02900 return ret;
02901 mov_write_moov_tag(moov_buf, mov, s);
02902 ret = avio_close_dyn_buf(moov_buf, &buf);
02903 av_free(buf);
02904 return ret;
02905 }
02906
02907 static int mov_flush_fragment(AVFormatContext *s)
02908 {
02909 MOVMuxContext *mov = s->priv_data;
02910 int i, first_track = -1;
02911 int64_t mdat_size = 0;
02912
02913 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
02914 return 0;
02915
02916 if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
02917 int64_t pos = avio_tell(s->pb);
02918 uint8_t *buf;
02919 int buf_size, moov_size;
02920
02921 for (i = 0; i < mov->nb_streams; i++)
02922 if (!mov->tracks[i].entry)
02923 break;
02924
02925 if (i < mov->nb_streams)
02926 return 0;
02927
02928 moov_size = get_moov_size(s);
02929 for (i = 0; i < mov->nb_streams; i++)
02930 mov->tracks[i].data_offset = pos + moov_size + 8;
02931
02932 mov_write_moov_tag(s->pb, mov, s);
02933
02934 buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
02935 mov->mdat_buf = NULL;
02936 avio_wb32(s->pb, buf_size + 8);
02937 ffio_wfourcc(s->pb, "mdat");
02938 avio_write(s->pb, buf, buf_size);
02939 av_free(buf);
02940
02941 mov->fragments++;
02942 mov->mdat_size = 0;
02943 for (i = 0; i < mov->nb_streams; i++) {
02944 if (mov->tracks[i].entry)
02945 mov->tracks[i].frag_start += mov->tracks[i].start_dts +
02946 mov->tracks[i].track_duration -
02947 mov->tracks[i].cluster[0].dts;
02948 mov->tracks[i].entry = 0;
02949 }
02950 avio_flush(s->pb);
02951 return 0;
02952 }
02953
02954 for (i = 0; i < mov->nb_streams; i++) {
02955 MOVTrack *track = &mov->tracks[i];
02956 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
02957 track->data_offset = 0;
02958 else
02959 track->data_offset = mdat_size;
02960 if (!track->mdat_buf)
02961 continue;
02962 mdat_size += avio_tell(track->mdat_buf);
02963 if (first_track < 0)
02964 first_track = i;
02965 }
02966
02967 if (!mdat_size)
02968 return 0;
02969
02970 for (i = 0; i < mov->nb_streams; i++) {
02971 MOVTrack *track = &mov->tracks[i];
02972 int buf_size, write_moof = 1, moof_tracks = -1;
02973 uint8_t *buf;
02974 int64_t duration = 0;
02975
02976 if (track->entry)
02977 duration = track->start_dts + track->track_duration -
02978 track->cluster[0].dts;
02979 if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
02980 if (!track->mdat_buf)
02981 continue;
02982 mdat_size = avio_tell(track->mdat_buf);
02983 moof_tracks = i;
02984 } else {
02985 write_moof = i == first_track;
02986 }
02987
02988 if (write_moof) {
02989 MOVFragmentInfo *info;
02990 avio_flush(s->pb);
02991 track->nb_frag_info++;
02992 track->frag_info = av_realloc(track->frag_info,
02993 sizeof(*track->frag_info) *
02994 track->nb_frag_info);
02995 info = &track->frag_info[track->nb_frag_info - 1];
02996 info->offset = avio_tell(s->pb);
02997 info->time = mov->tracks[i].frag_start;
02998 info->duration = duration;
02999 mov_write_tfrf_tags(s->pb, mov, track);
03000
03001 mov_write_moof_tag(s->pb, mov, moof_tracks);
03002 info->tfrf_offset = track->tfrf_offset;
03003 mov->fragments++;
03004
03005 avio_wb32(s->pb, mdat_size + 8);
03006 ffio_wfourcc(s->pb, "mdat");
03007 }
03008
03009 if (track->entry)
03010 track->frag_start += duration;
03011 track->entry = 0;
03012 if (!track->mdat_buf)
03013 continue;
03014 buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
03015 track->mdat_buf = NULL;
03016
03017 avio_write(s->pb, buf, buf_size);
03018 av_free(buf);
03019 }
03020
03021 mov->mdat_size = 0;
03022
03023 avio_flush(s->pb);
03024 return 0;
03025 }
03026
03027 int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
03028 {
03029 MOVMuxContext *mov = s->priv_data;
03030 AVIOContext *pb = s->pb;
03031 MOVTrack *trk = &mov->tracks[pkt->stream_index];
03032 AVCodecContext *enc = trk->enc;
03033 unsigned int samples_in_chunk = 0;
03034 int size= pkt->size;
03035 uint8_t *reformatted_data = NULL;
03036
03037 if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
03038 int ret;
03039 if (mov->fragments > 0) {
03040 if (!trk->mdat_buf) {
03041 if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
03042 return ret;
03043 }
03044 pb = trk->mdat_buf;
03045 } else {
03046 if (!mov->mdat_buf) {
03047 if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
03048 return ret;
03049 }
03050 pb = mov->mdat_buf;
03051 }
03052 }
03053
03054 if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
03055
03056 static uint16_t packed_size[16] =
03057 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
03058 int len = 0;
03059
03060 while (len < size && samples_in_chunk < 100) {
03061 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
03062 samples_in_chunk++;
03063 }
03064 if (samples_in_chunk > 1) {
03065 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
03066 return -1;
03067 }
03068 } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
03069 enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
03070 samples_in_chunk = enc->frame_size;
03071 } else if (trk->sample_size)
03072 samples_in_chunk = size / trk->sample_size;
03073 else
03074 samples_in_chunk = 1;
03075
03076
03077 if (trk->vos_len == 0 && enc->extradata_size > 0) {
03078 trk->vos_len = enc->extradata_size;
03079 trk->vos_data = av_malloc(trk->vos_len);
03080 memcpy(trk->vos_data, enc->extradata, trk->vos_len);
03081 }
03082
03083 if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
03084
03085
03086 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
03087 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
03088 &size);
03089 avio_write(pb, reformatted_data, size);
03090 } else {
03091 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
03092 }
03093 } else if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
03094 (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
03095 av_log(s, AV_LOG_ERROR, "malformated aac bitstream, use -absf aac_adtstoasc\n");
03096 return -1;
03097 } else {
03098 avio_write(pb, pkt->data, size);
03099 }
03100
03101 if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
03102 enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
03103
03104 trk->vos_len = size;
03105 trk->vos_data = av_malloc(size);
03106 if (!trk->vos_data)
03107 return AVERROR(ENOMEM);
03108 memcpy(trk->vos_data, pkt->data, size);
03109 }
03110
03111 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
03112 trk->cluster = av_realloc_f(trk->cluster, sizeof(*trk->cluster), (trk->entry + MOV_INDEX_CLUSTER_SIZE));
03113 if (!trk->cluster)
03114 return -1;
03115 }
03116
03117 trk->cluster[trk->entry].pos = avio_tell(pb) - size;
03118 trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
03119 trk->cluster[trk->entry].chunkNum = 0;
03120 trk->cluster[trk->entry].size = size;
03121 trk->cluster[trk->entry].entries = samples_in_chunk;
03122 trk->cluster[trk->entry].dts = pkt->dts;
03123 if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
03124
03125
03126
03127
03128 trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
03129 }
03130 if (trk->start_dts == AV_NOPTS_VALUE)
03131 trk->start_dts = pkt->dts;
03132 trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
03133 trk->last_sample_is_subtitle_end = 0;
03134
03135 if (pkt->pts == AV_NOPTS_VALUE) {
03136 av_log(s, AV_LOG_WARNING, "pts has no value\n");
03137 pkt->pts = pkt->dts;
03138 }
03139 if (pkt->dts != pkt->pts)
03140 trk->flags |= MOV_TRACK_CTTS;
03141 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
03142 trk->cluster[trk->entry].flags = 0;
03143 if (enc->codec_id == AV_CODEC_ID_VC1) {
03144 mov_parse_vc1_frame(pkt, trk, mov->fragments);
03145 } else if (pkt->flags & AV_PKT_FLAG_KEY) {
03146 if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
03147 trk->entry > 0) {
03148 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
03149 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
03150 trk->flags |= MOV_TRACK_STPS;
03151 } else {
03152 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
03153 }
03154 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
03155 trk->has_keyframes++;
03156 }
03157 trk->entry++;
03158 trk->sample_count += samples_in_chunk;
03159 mov->mdat_size += size;
03160
03161 avio_flush(pb);
03162
03163 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
03164 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
03165 reformatted_data, size);
03166 av_free(reformatted_data);
03167 return 0;
03168 }
03169
03170 static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
03171 {
03172 MOVMuxContext *mov = s->priv_data;
03173 MOVTrack *trk = &mov->tracks[pkt->stream_index];
03174 AVCodecContext *enc = trk->enc;
03175 int64_t frag_duration = 0;
03176 int size = pkt->size;
03177
03178 if (!pkt->size) return 0;
03179
03180 if (trk->entry && pkt->stream_index < s->nb_streams)
03181 frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
03182 s->streams[pkt->stream_index]->time_base,
03183 AV_TIME_BASE_Q);
03184 if ((mov->max_fragment_duration &&
03185 frag_duration >= mov->max_fragment_duration) ||
03186 (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
03187 (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
03188 enc->codec_type == AVMEDIA_TYPE_VIDEO &&
03189 trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
03190 if (frag_duration >= mov->min_fragment_duration)
03191 mov_flush_fragment(s);
03192 }
03193
03194 return ff_mov_write_packet(s, pkt);
03195 }
03196
03197 static int mov_write_subtitle_end_packet(AVFormatContext *s,
03198 int stream_index,
03199 int64_t dts) {
03200 AVPacket end;
03201 uint8_t data[2] = {0};
03202 int ret;
03203
03204 av_init_packet(&end);
03205 end.size = sizeof(data);
03206 end.data = data;
03207 end.pts = dts;
03208 end.dts = dts;
03209 end.duration = 0;
03210 end.stream_index = stream_index;
03211
03212 ret = mov_write_single_packet(s, &end);
03213 av_free_packet(&end);
03214
03215 return ret;
03216 }
03217
03218 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
03219 {
03220 if (!pkt) {
03221 mov_flush_fragment(s);
03222 return 1;
03223 } else {
03224 int i;
03225 MOVMuxContext *mov = s->priv_data;
03226
03227 if (!pkt->size) return 0;
03228
03229
03230
03231
03232
03233
03234
03235
03236
03237
03238
03239
03240
03241
03242
03243
03244
03245
03246
03247
03248 for (i = 0; i < mov->nb_streams; i++) {
03249 MOVTrack *trk = &mov->tracks[i];
03250 int ret;
03251
03252 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
03253 trk->track_duration < pkt->dts &&
03254 (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
03255 ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
03256 if (ret < 0) return ret;
03257 trk->last_sample_is_subtitle_end = 1;
03258 }
03259 }
03260
03261 return mov_write_single_packet(s, pkt);
03262 }
03263 }
03264
03265
03266
03267 static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
03268 {
03269 AVIOContext *pb;
03270
03271 MOVMuxContext *mov = s->priv_data;
03272 MOVTrack *track = &mov->tracks[tracknum];
03273 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
03274 int i, len;
03275
03276 track->mode = mov->mode;
03277 track->tag = MKTAG('t','e','x','t');
03278 track->timescale = MOV_TIMESCALE;
03279 track->enc = avcodec_alloc_context3(NULL);
03280 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
03281
03282 if (avio_open_dyn_buf(&pb) >= 0) {
03283 int size;
03284 uint8_t *buf;
03285
03286
03287
03288 avio_wb32(pb, 0x01);
03289 avio_w8(pb, 0x00);
03290 avio_w8(pb, 0x00);
03291 avio_w8(pb, 0x00);
03292 avio_w8(pb, 0x00);
03293 avio_w8(pb, 0x00);
03294 avio_w8(pb, 0x00);
03295
03296 avio_wb16(pb, 0x00);
03297 avio_wb16(pb, 0x00);
03298 avio_wb16(pb, 0x00);
03299 avio_wb16(pb, 0x00);
03300
03301 avio_wb16(pb, 0x00);
03302 avio_wb16(pb, 0x00);
03303 avio_wb16(pb, 0x01);
03304 avio_w8(pb, 0x00);
03305 avio_w8(pb, 0x00);
03306 avio_w8(pb, 0x00);
03307 avio_w8(pb, 0x00);
03308 avio_w8(pb, 0x00);
03309 avio_w8(pb, 0x00);
03310
03311 avio_wb32(pb, 0x0D);
03312 ffio_wfourcc(pb, "ftab");
03313 avio_wb16(pb, 0x01);
03314
03315 avio_wb16(pb, 0x01);
03316 avio_w8(pb, 0x00);
03317
03318 if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
03319 track->enc->extradata = buf;
03320 track->enc->extradata_size = size;
03321 } else {
03322 av_free(&buf);
03323 }
03324 }
03325
03326 for (i = 0; i < s->nb_chapters; i++) {
03327 AVChapter *c = s->chapters[i];
03328 AVDictionaryEntry *t;
03329
03330 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
03331 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
03332 pkt.duration = end - pkt.dts;
03333
03334 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
03335 len = strlen(t->value);
03336 pkt.size = len+2;
03337 pkt.data = av_malloc(pkt.size);
03338 AV_WB16(pkt.data, len);
03339 memcpy(pkt.data+2, t->value, len);
03340 ff_mov_write_packet(s, &pkt);
03341 av_freep(&pkt.data);
03342 }
03343 }
03344 }
03345
03346 static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
03347 {
03348 int ret;
03349 MOVMuxContext *mov = s->priv_data;
03350 MOVTrack *track = &mov->tracks[index];
03351 AVStream *src_st = s->streams[src_index];
03352 AVTimecode tc;
03353 AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
03354 AVRational rate = {src_st->codec->time_base.den, src_st->codec->time_base.num};
03355
03356
03357 if (av_timecode_check_frame_rate(rate) < 0) {
03358 av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
03359 rate.num, rate.den, src_st->avg_frame_rate.num, src_st->avg_frame_rate.den);
03360 rate = src_st->avg_frame_rate;
03361 }
03362
03363
03364 ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
03365 if (ret < 0)
03366 return ret;
03367
03368
03369 track->mode = mov->mode;
03370 track->tag = MKTAG('t','m','c','d');
03371 track->src_track = src_index;
03372 track->timescale = src_st->codec->time_base.den;
03373 if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
03374 track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
03375
03376
03377 track->enc = avcodec_alloc_context3(NULL);
03378 track->enc->codec_type = AVMEDIA_TYPE_DATA;
03379 track->enc->codec_tag = track->tag;
03380 track->enc->time_base = src_st->codec->time_base;
03381
03382
03383 pkt.data = av_malloc(pkt.size);
03384 AV_WB32(pkt.data, tc.start);
03385 ret = ff_mov_write_packet(s, &pkt);
03386 av_free(pkt.data);
03387 return ret;
03388 }
03389
03390 static int mov_write_header(AVFormatContext *s)
03391 {
03392 AVIOContext *pb = s->pb;
03393 MOVMuxContext *mov = s->priv_data;
03394 AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
03395 int i, hint_track = 0, tmcd_track = 0;
03396
03397
03398
03399 if (mov->max_fragment_duration || mov->max_fragment_size ||
03400 mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
03401 FF_MOV_FLAG_FRAG_KEYFRAME |
03402 FF_MOV_FLAG_FRAG_CUSTOM))
03403 mov->flags |= FF_MOV_FLAG_FRAGMENT;
03404
03405
03406 if (mov->flags & FF_MOV_FLAG_FASTSTART) {
03407 if (mov->flags & FF_MOV_FLAG_FRAGMENT)
03408 mov->flags &= ~FF_MOV_FLAG_FASTSTART;
03409 else
03410 mov->reserved_moov_size = -1;
03411 }
03412
03413
03414
03415 if (!s->pb->seekable &&
03416 ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
03417 !(s->oformat && !strcmp(s->oformat->name, "ismv")))
03418 || mov->ism_lookahead)) {
03419 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
03420 return -1;
03421 }
03422
03423
03424 mov->mode = MODE_MP4;
03425
03426 if (s->oformat != NULL) {
03427 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
03428 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
03429 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
03430 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
03431 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
03432 else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
03433 else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
03434
03435 mov_write_ftyp_tag(pb,s);
03436 if (mov->mode == MODE_PSP) {
03437 if (s->nb_streams != 2) {
03438 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
03439 return -1;
03440 }
03441 mov_write_uuidprof_tag(pb,s);
03442 }
03443 }
03444
03445 mov->nb_streams = s->nb_streams;
03446 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
03447 mov->chapter_track = mov->nb_streams++;
03448
03449 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
03450
03451 hint_track = mov->nb_streams;
03452 for (i = 0; i < s->nb_streams; i++) {
03453 AVStream *st = s->streams[i];
03454 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
03455 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
03456 mov->nb_streams++;
03457 }
03458 }
03459 }
03460
03461 if (mov->mode == MODE_MOV) {
03462 tmcd_track = mov->nb_streams;
03463
03464
03465 for (i = 0; i < s->nb_streams; i++) {
03466 AVStream *st = s->streams[i];
03467 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
03468 (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
03469 mov->nb_meta_tmcd++;
03470 }
03471
03472
03473 if (mov->nb_meta_tmcd) {
03474 for (i = 0; i < s->nb_streams; i++) {
03475 AVStream *st = s->streams[i];
03476 if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
03477 av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
03478 "so timecode metadata are now ignored\n");
03479 mov->nb_meta_tmcd = 0;
03480 }
03481 }
03482 }
03483
03484 mov->nb_streams += mov->nb_meta_tmcd;
03485 }
03486
03487 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
03488 if (!mov->tracks)
03489 return AVERROR(ENOMEM);
03490
03491 for(i=0; i<s->nb_streams; i++){
03492 AVStream *st= s->streams[i];
03493 MOVTrack *track= &mov->tracks[i];
03494 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
03495
03496 track->enc = st->codec;
03497 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
03498 if (track->language < 0)
03499 track->language = 0;
03500 track->mode = mov->mode;
03501 track->tag = mov_find_codec_tag(s, track);
03502 if (!track->tag) {
03503 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
03504 "codec not currently supported in container\n", i);
03505 goto error;
03506 }
03507
03508
03509 track->hint_track = -1;
03510 track->start_dts = AV_NOPTS_VALUE;
03511 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
03512 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
03513 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
03514 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
03515 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
03516 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
03517 goto error;
03518 }
03519 track->height = track->tag>>24 == 'n' ? 486 : 576;
03520 }
03521 track->timescale = st->codec->time_base.den;
03522 if (track->mode == MODE_MOV && track->timescale > 100000)
03523 av_log(s, AV_LOG_WARNING,
03524 "WARNING codec timebase is very high. If duration is too long,\n"
03525 "file may not be playable by quicktime. Specify a shorter timebase\n"
03526 "or choose different container.\n");
03527 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
03528 track->timescale = st->codec->sample_rate;
03529 if(!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
03530 av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
03531 track->audio_vbr = 1;
03532 }else if(st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
03533 st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
03534 st->codec->codec_id == AV_CODEC_ID_ILBC){
03535 if (!st->codec->block_align) {
03536 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
03537 goto error;
03538 }
03539 track->sample_size = st->codec->block_align;
03540 }else if(st->codec->frame_size > 1){
03541 track->audio_vbr = 1;
03542 }else{
03543 track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
03544 }
03545 if (track->mode != MODE_MOV &&
03546 track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
03547 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
03548 i, track->enc->sample_rate);
03549 goto error;
03550 }
03551 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
03552 track->timescale = st->codec->time_base.den;
03553 }else{
03554 track->timescale = MOV_TIMESCALE;
03555 }
03556 if (!track->height)
03557 track->height = st->codec->height;
03558
03559
03560 if (mov->mode == MODE_ISM)
03561 track->timescale = 10000000;
03562
03563 avpriv_set_pts_info(st, 64, 1, track->timescale);
03564
03565
03566 if (st->codec->extradata_size) {
03567 track->vos_len = st->codec->extradata_size;
03568 track->vos_data = av_malloc(track->vos_len);
03569 memcpy(track->vos_data, st->codec->extradata, track->vos_len);
03570 }
03571 }
03572
03573 if (mov->mode == MODE_ISM) {
03574
03575 if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
03576 FF_MOV_FLAG_FRAG_CUSTOM)) &&
03577 !mov->max_fragment_duration && !mov->max_fragment_size)
03578 mov->max_fragment_duration = 5000000;
03579 mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
03580 FF_MOV_FLAG_FRAGMENT;
03581 }
03582
03583 if(mov->reserved_moov_size){
03584 mov->reserved_moov_pos= avio_tell(pb);
03585 if (mov->reserved_moov_size > 0)
03586 avio_skip(pb, mov->reserved_moov_size);
03587 }
03588
03589 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
03590 mov_write_mdat_tag(pb, mov);
03591
03592 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
03593 mov->time = ff_iso8601_to_unix_time(t->value);
03594 if (mov->time)
03595 mov->time += 0x7C25B080;
03596
03597 if (mov->chapter_track)
03598 mov_create_chapter_track(s, mov->chapter_track);
03599
03600 if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
03601
03602 for (i = 0; i < s->nb_streams; i++) {
03603 AVStream *st = s->streams[i];
03604 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
03605 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
03606 ff_mov_init_hinting(s, hint_track, i);
03607 hint_track++;
03608 }
03609 }
03610 }
03611
03612 if (mov->nb_meta_tmcd) {
03613
03614 for (i = 0; i < s->nb_streams; i++) {
03615 AVStream *st = s->streams[i];
03616 t = global_tcr;
03617
03618 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
03619 if (!t)
03620 t = av_dict_get(st->metadata, "timecode", NULL, 0);
03621 if (!t)
03622 continue;
03623 if (mov_create_timecode_track(s, tmcd_track, i, t->value) < 0)
03624 goto error;
03625 tmcd_track++;
03626 }
03627 }
03628 }
03629
03630 avio_flush(pb);
03631
03632 if (mov->flags & FF_MOV_FLAG_ISML)
03633 mov_write_isml_manifest(pb, mov);
03634
03635 if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
03636 mov_write_moov_tag(pb, mov, s);
03637 mov->fragments++;
03638 }
03639
03640 return 0;
03641 error:
03642 av_freep(&mov->tracks);
03643 return -1;
03644 }
03645
03652 static int compute_moov_size(AVFormatContext *s)
03653 {
03654 int i, moov_size, moov_size2;
03655 MOVMuxContext *mov = s->priv_data;
03656
03657 moov_size = get_moov_size(s);
03658 if (moov_size < 0)
03659 return moov_size;
03660
03661 for (i = 0; i < mov->nb_streams; i++)
03662 mov->tracks[i].data_offset += moov_size;
03663
03664 moov_size2 = get_moov_size(s);
03665 if (moov_size2 < 0)
03666 return moov_size2;
03667
03668
03669
03670 if (moov_size2 != moov_size)
03671 for (i = 0; i < mov->nb_streams; i++)
03672 mov->tracks[i].data_offset += moov_size2 - moov_size;
03673
03674 return moov_size2;
03675 }
03676
03677 static int shift_data(AVFormatContext *s)
03678 {
03679 int ret = 0, moov_size;
03680 MOVMuxContext *mov = s->priv_data;
03681 int64_t pos, pos_end = avio_tell(s->pb);
03682 uint8_t *buf, *read_buf[2];
03683 int read_buf_id = 0;
03684 int read_size[2];
03685 AVIOContext *read_pb;
03686
03687 moov_size = compute_moov_size(s);
03688 if (moov_size < 0)
03689 return moov_size;
03690
03691 buf = av_malloc(moov_size * 2);
03692 if (!buf)
03693 return AVERROR(ENOMEM);
03694 read_buf[0] = buf;
03695 read_buf[1] = buf + moov_size;
03696
03697
03698
03699
03700 avio_flush(s->pb);
03701 ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
03702 if (ret < 0) {
03703 av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
03704 "the second pass (faststart)\n", s->filename);
03705 goto end;
03706 }
03707
03708
03709
03710 pos_end = avio_tell(s->pb);
03711 avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
03712
03713
03714 avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
03715 pos = avio_tell(read_pb);
03716
03717 #define READ_BLOCK do { \
03718 read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
03719 read_buf_id ^= 1; \
03720 } while (0)
03721
03722
03723 READ_BLOCK;
03724 do {
03725 int n;
03726 READ_BLOCK;
03727 n = read_size[read_buf_id];
03728 if (n <= 0)
03729 break;
03730 avio_write(s->pb, read_buf[read_buf_id], n);
03731 pos += n;
03732 } while (pos < pos_end);
03733 avio_close(read_pb);
03734
03735 end:
03736 av_free(buf);
03737 return ret;
03738 }
03739
03740 static int mov_write_trailer(AVFormatContext *s)
03741 {
03742 MOVMuxContext *mov = s->priv_data;
03743 AVIOContext *pb = s->pb;
03744 int64_t moov_pos;
03745 int res = 0;
03746 int i;
03747
03748
03749
03750
03751
03752 for (i = 0; i < mov->nb_streams; i++) {
03753 MOVTrack *trk = &mov->tracks[i];
03754 if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
03755 !trk->last_sample_is_subtitle_end) {
03756 mov_write_subtitle_end_packet(s, i, trk->track_duration);
03757 trk->last_sample_is_subtitle_end = 1;
03758 }
03759 }
03760
03761 moov_pos = avio_tell(pb);
03762
03763 if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
03764
03765 if (mov->mdat_size + 8 <= UINT32_MAX) {
03766 avio_seek(pb, mov->mdat_pos, SEEK_SET);
03767 avio_wb32(pb, mov->mdat_size + 8);
03768 } else {
03769
03770 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
03771
03772
03773 avio_wb32(pb, 1);
03774 ffio_wfourcc(pb, "mdat");
03775 avio_wb64(pb, mov->mdat_size + 16);
03776 }
03777 avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
03778
03779 if (mov->reserved_moov_size == -1) {
03780 av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
03781 res = shift_data(s);
03782 if (res == 0) {
03783 avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
03784 mov_write_moov_tag(pb, mov, s);
03785 }
03786 } else if (mov->reserved_moov_size > 0) {
03787 int64_t size;
03788 mov_write_moov_tag(pb, mov, s);
03789 size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
03790 if(size < 8){
03791 av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
03792 return -1;
03793 }
03794 avio_wb32(pb, size);
03795 ffio_wfourcc(pb, "free");
03796 for(i=0; i<size; i++)
03797 avio_w8(pb, 0);
03798 avio_seek(pb, moov_pos, SEEK_SET);
03799 } else {
03800 mov_write_moov_tag(pb, mov, s);
03801 }
03802 } else {
03803 mov_flush_fragment(s);
03804 mov_write_mfra_tag(pb, mov);
03805 }
03806
03807 if (mov->chapter_track)
03808 av_freep(&mov->tracks[mov->chapter_track].enc);
03809
03810 for (i=0; i<mov->nb_streams; i++) {
03811 if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
03812 ff_mov_close_hinting(&mov->tracks[i]);
03813 else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
03814 av_freep(&mov->tracks[i].enc);
03815 if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
03816 mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
03817 int64_t off = avio_tell(pb);
03818 uint8_t buf[7];
03819 if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
03820 avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
03821 avio_write(pb, buf, 7);
03822 avio_seek(pb, off, SEEK_SET);
03823 }
03824 }
03825 av_freep(&mov->tracks[i].cluster);
03826 av_freep(&mov->tracks[i].frag_info);
03827
03828 if (mov->tracks[i].vos_len)
03829 av_free(mov->tracks[i].vos_data);
03830
03831 }
03832
03833 av_freep(&mov->tracks);
03834
03835 return res;
03836 }
03837
03838 #if CONFIG_MOV_MUXER
03839 MOV_CLASS(mov)
03840 AVOutputFormat ff_mov_muxer = {
03841 .name = "mov",
03842 .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
03843 .extensions = "mov",
03844 .priv_data_size = sizeof(MOVMuxContext),
03845 .audio_codec = AV_CODEC_ID_AAC,
03846 .video_codec = CONFIG_LIBX264_ENCODER ?
03847 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
03848 .write_header = mov_write_header,
03849 .write_packet = mov_write_packet,
03850 .write_trailer = mov_write_trailer,
03851 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03852 .codec_tag = (const AVCodecTag* const []){
03853 ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
03854 },
03855 .priv_class = &mov_muxer_class,
03856 };
03857 #endif
03858 #if CONFIG_TGP_MUXER
03859 MOV_CLASS(tgp)
03860 AVOutputFormat ff_tgp_muxer = {
03861 .name = "3gp",
03862 .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
03863 .extensions = "3gp",
03864 .priv_data_size = sizeof(MOVMuxContext),
03865 .audio_codec = AV_CODEC_ID_AMR_NB,
03866 .video_codec = AV_CODEC_ID_H263,
03867 .write_header = mov_write_header,
03868 .write_packet = mov_write_packet,
03869 .write_trailer = mov_write_trailer,
03870 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03871 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
03872 .priv_class = &tgp_muxer_class,
03873 };
03874 #endif
03875 #if CONFIG_MP4_MUXER
03876 MOV_CLASS(mp4)
03877 AVOutputFormat ff_mp4_muxer = {
03878 .name = "mp4",
03879 .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
03880 .mime_type = "application/mp4",
03881 .extensions = "mp4",
03882 .priv_data_size = sizeof(MOVMuxContext),
03883 .audio_codec = AV_CODEC_ID_AAC,
03884 .video_codec = CONFIG_LIBX264_ENCODER ?
03885 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
03886 .write_header = mov_write_header,
03887 .write_packet = mov_write_packet,
03888 .write_trailer = mov_write_trailer,
03889 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03890 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
03891 .priv_class = &mp4_muxer_class,
03892 };
03893 #endif
03894 #if CONFIG_PSP_MUXER
03895 MOV_CLASS(psp)
03896 AVOutputFormat ff_psp_muxer = {
03897 .name = "psp",
03898 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
03899 .extensions = "mp4,psp",
03900 .priv_data_size = sizeof(MOVMuxContext),
03901 .audio_codec = AV_CODEC_ID_AAC,
03902 .video_codec = CONFIG_LIBX264_ENCODER ?
03903 AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
03904 .write_header = mov_write_header,
03905 .write_packet = mov_write_packet,
03906 .write_trailer = mov_write_trailer,
03907 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03908 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
03909 .priv_class = &psp_muxer_class,
03910 };
03911 #endif
03912 #if CONFIG_TG2_MUXER
03913 MOV_CLASS(tg2)
03914 AVOutputFormat ff_tg2_muxer = {
03915 .name = "3g2",
03916 .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
03917 .extensions = "3g2",
03918 .priv_data_size = sizeof(MOVMuxContext),
03919 .audio_codec = AV_CODEC_ID_AMR_NB,
03920 .video_codec = AV_CODEC_ID_H263,
03921 .write_header = mov_write_header,
03922 .write_packet = mov_write_packet,
03923 .write_trailer = mov_write_trailer,
03924 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03925 .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
03926 .priv_class = &tg2_muxer_class,
03927 };
03928 #endif
03929 #if CONFIG_IPOD_MUXER
03930 MOV_CLASS(ipod)
03931 AVOutputFormat ff_ipod_muxer = {
03932 .name = "ipod",
03933 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
03934 .mime_type = "application/mp4",
03935 .extensions = "m4v,m4a",
03936 .priv_data_size = sizeof(MOVMuxContext),
03937 .audio_codec = AV_CODEC_ID_AAC,
03938 .video_codec = AV_CODEC_ID_H264,
03939 .write_header = mov_write_header,
03940 .write_packet = mov_write_packet,
03941 .write_trailer = mov_write_trailer,
03942 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03943 .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
03944 .priv_class = &ipod_muxer_class,
03945 };
03946 #endif
03947 #if CONFIG_ISMV_MUXER
03948 MOV_CLASS(ismv)
03949 AVOutputFormat ff_ismv_muxer = {
03950 .name = "ismv",
03951 .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
03952 .mime_type = "application/mp4",
03953 .extensions = "ismv,isma",
03954 .priv_data_size = sizeof(MOVMuxContext),
03955 .audio_codec = AV_CODEC_ID_AAC,
03956 .video_codec = AV_CODEC_ID_H264,
03957 .write_header = mov_write_header,
03958 .write_packet = mov_write_packet,
03959 .write_trailer = mov_write_trailer,
03960 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03961 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
03962 .priv_class = &ismv_muxer_class,
03963 };
03964 #endif
03965 #if CONFIG_F4V_MUXER
03966 MOV_CLASS(f4v)
03967 AVOutputFormat ff_f4v_muxer = {
03968 .name = "f4v",
03969 .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
03970 .mime_type = "application/f4v",
03971 .extensions = "f4v",
03972 .priv_data_size = sizeof(MOVMuxContext),
03973 .audio_codec = AV_CODEC_ID_AAC,
03974 .video_codec = AV_CODEC_ID_H264,
03975 .write_header = mov_write_header,
03976 .write_packet = mov_write_packet,
03977 .write_trailer = mov_write_trailer,
03978 .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
03979 .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
03980 .priv_class = &f4v_muxer_class,
03981 };
03982 #endif