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