00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023 #include "riff.h"
00024 #include "avio.h"
00025 #include "isom.h"
00026 #include "avc.h"
00027
00028 #undef NDEBUG
00029 #include <assert.h>
00030
00031 #define MOV_INDEX_CLUSTER_SIZE 16384
00032 #define globalTimescale 1000
00033
00034 #define MODE_MP4 0
00035 #define MODE_MOV 1
00036 #define MODE_3GP 2
00037 #define MODE_PSP 3 // example working PSP command line:
00038
00039 #define MODE_3G2 4
00040
00041 typedef struct MOVIentry {
00042 unsigned int flags, size;
00043 uint64_t pos;
00044 unsigned int samplesInChunk;
00045 char key_frame;
00046 unsigned int entries;
00047 int64_t cts;
00048 int64_t dts;
00049 } MOVIentry;
00050
00051 typedef struct MOVIndex {
00052 int mode;
00053 int entry;
00054 long timescale;
00055 long time;
00056 int64_t trackDuration;
00057 long sampleCount;
00058 long sampleSize;
00059 int hasKeyframes;
00060 int hasBframes;
00061 int language;
00062 int trackID;
00063 int tag;
00064 AVCodecContext *enc;
00065
00066 int vosLen;
00067 uint8_t *vosData;
00068 MOVIentry *cluster;
00069 int audio_vbr;
00070 } MOVTrack;
00071
00072 typedef struct MOVContext {
00073 int mode;
00074 int64_t time;
00075 int nb_streams;
00076 offset_t mdat_pos;
00077 uint64_t mdat_size;
00078 long timescale;
00079 MOVTrack tracks[MAX_STREAMS];
00080 } MOVContext;
00081
00082
00083 static offset_t updateSize (ByteIOContext *pb, offset_t pos)
00084 {
00085 offset_t curpos = url_ftell(pb);
00086 url_fseek(pb, pos, SEEK_SET);
00087 put_be32(pb, curpos - pos);
00088 url_fseek(pb, curpos, SEEK_SET);
00089
00090 return curpos - pos;
00091 }
00092
00093
00094 static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
00095 {
00096 int i;
00097 int mode64 = 0;
00098 offset_t pos = url_ftell(pb);
00099 put_be32(pb, 0);
00100 if (pos > UINT32_MAX) {
00101 mode64 = 1;
00102 put_tag(pb, "co64");
00103 } else
00104 put_tag(pb, "stco");
00105 put_be32(pb, 0);
00106 put_be32(pb, track->entry);
00107 for (i=0; i<track->entry; i++) {
00108 if(mode64 == 1)
00109 put_be64(pb, track->cluster[i].pos);
00110 else
00111 put_be32(pb, track->cluster[i].pos);
00112 }
00113 return updateSize (pb, pos);
00114 }
00115
00116
00117 static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
00118 {
00119 int equalChunks = 1;
00120 int i, j, entries = 0, tst = -1, oldtst = -1;
00121
00122 offset_t pos = url_ftell(pb);
00123 put_be32(pb, 0);
00124 put_tag(pb, "stsz");
00125 put_be32(pb, 0);
00126
00127 for (i=0; i<track->entry; i++) {
00128 tst = track->cluster[i].size/track->cluster[i].entries;
00129 if(oldtst != -1 && tst != oldtst) {
00130 equalChunks = 0;
00131 }
00132 oldtst = tst;
00133 entries += track->cluster[i].entries;
00134 }
00135 if (equalChunks) {
00136 int sSize = track->cluster[0].size/track->cluster[0].entries;
00137 put_be32(pb, sSize);
00138 put_be32(pb, entries);
00139 }
00140 else {
00141 put_be32(pb, 0);
00142 put_be32(pb, entries);
00143 for (i=0; i<track->entry; i++) {
00144 for ( j=0; j<track->cluster[i].entries; j++) {
00145 put_be32(pb, track->cluster[i].size /
00146 track->cluster[i].entries);
00147 }
00148 }
00149 }
00150 return updateSize (pb, pos);
00151 }
00152
00153
00154 static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
00155 {
00156 int index = 0, oldval = -1, i;
00157 offset_t entryPos, curpos;
00158
00159 offset_t pos = url_ftell(pb);
00160 put_be32(pb, 0);
00161 put_tag(pb, "stsc");
00162 put_be32(pb, 0);
00163 entryPos = url_ftell(pb);
00164 put_be32(pb, track->entry);
00165 for (i=0; i<track->entry; i++) {
00166 if(oldval != track->cluster[i].samplesInChunk)
00167 {
00168 put_be32(pb, i+1);
00169 put_be32(pb, track->cluster[i].samplesInChunk);
00170 put_be32(pb, 0x1);
00171 oldval = track->cluster[i].samplesInChunk;
00172 index++;
00173 }
00174 }
00175 curpos = url_ftell(pb);
00176 url_fseek(pb, entryPos, SEEK_SET);
00177 put_be32(pb, index);
00178 url_fseek(pb, curpos, SEEK_SET);
00179
00180 return updateSize (pb, pos);
00181 }
00182
00183
00184 static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
00185 {
00186 offset_t curpos, entryPos;
00187 int i, index = 0;
00188 offset_t pos = url_ftell(pb);
00189 put_be32(pb, 0);
00190 put_tag(pb, "stss");
00191 put_be32(pb, 0);
00192 entryPos = url_ftell(pb);
00193 put_be32(pb, track->entry);
00194 for (i=0; i<track->entry; i++) {
00195 if(track->cluster[i].key_frame == 1) {
00196 put_be32(pb, i+1);
00197 index++;
00198 }
00199 }
00200 curpos = url_ftell(pb);
00201 url_fseek(pb, entryPos, SEEK_SET);
00202 put_be32(pb, index);
00203 url_fseek(pb, curpos, SEEK_SET);
00204 return updateSize (pb, pos);
00205 }
00206
00207 static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
00208 {
00209 put_be32(pb, 0x11);
00210 if (track->mode == MODE_MOV) put_tag(pb, "samr");
00211 else put_tag(pb, "damr");
00212 put_tag(pb, "FFMP");
00213 put_byte(pb, 0);
00214
00215 put_be16(pb, 0x81FF);
00216 put_byte(pb, 0x00);
00217 put_byte(pb, 0x01);
00218 return 0x11;
00219 }
00220
00221 static int mov_write_enda_tag(ByteIOContext *pb)
00222 {
00223 put_be32(pb, 10);
00224 put_tag(pb, "enda");
00225 put_be16(pb, 1);
00226 return 10;
00227 }
00228
00229 static unsigned int descrLength(unsigned int len)
00230 {
00231 int i;
00232 for(i=1; len>>(7*i); i++);
00233 return len + 1 + i;
00234 }
00235
00236 static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
00237 {
00238 int i= descrLength(size) - size - 2;
00239 put_byte(pb, tag);
00240 for(; i>0; i--)
00241 put_byte(pb, (size>>(7*i)) | 0x80);
00242 put_byte(pb, size & 0x7F);
00243 }
00244
00245 static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track)
00246 {
00247 offset_t pos = url_ftell(pb);
00248 int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
00249
00250 put_be32(pb, 0);
00251 put_tag(pb, "esds");
00252 put_be32(pb, 0);
00253
00254
00255 putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
00256 descrLength(1));
00257 put_be16(pb, track->trackID);
00258 put_byte(pb, 0x00);
00259
00260
00261 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
00262
00263
00264 put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
00265
00266
00267
00268 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
00269 put_byte(pb, 0x15);
00270 else
00271 put_byte(pb, 0x11);
00272
00273 put_byte(pb, track->enc->rc_buffer_size>>(3+16));
00274 put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF);
00275
00276 put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate));
00277 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
00278 put_be32(pb, 0);
00279 else
00280 put_be32(pb, track->enc->rc_max_rate);
00281
00282 if (track->vosLen)
00283 {
00284
00285 putDescr(pb, 0x05, track->vosLen);
00286 put_buffer(pb, track->vosData, track->vosLen);
00287 }
00288
00289
00290
00291 putDescr(pb, 0x06, 1);
00292 put_byte(pb, 0x02);
00293 return updateSize (pb, pos);
00294 }
00295
00296 static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
00297 {
00298 offset_t pos = url_ftell(pb);
00299
00300 put_be32(pb, 0);
00301 put_tag(pb, "wave");
00302
00303 put_be32(pb, 12);
00304 put_tag(pb, "frma");
00305 put_le32(pb, track->tag);
00306
00307 if (track->enc->codec_id == CODEC_ID_AAC) {
00308
00309 put_be32(pb, 12);
00310 put_tag(pb, "mp4a");
00311 put_be32(pb, 0);
00312 mov_write_esds_tag(pb, track);
00313 } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
00314 track->enc->codec_id == CODEC_ID_PCM_S32LE) {
00315 mov_write_enda_tag(pb);
00316 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
00317 mov_write_amr_tag(pb, track);
00318 }
00319
00320 put_be32(pb, 8);
00321 put_be32(pb, 0);
00322
00323 return updateSize (pb, pos);
00324 }
00325
00326 static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack* track)
00327 {
00328 put_be32(pb, track->vosLen+8);
00329 put_tag(pb, "glbl");
00330 put_buffer(pb, track->vosData, track->vosLen);
00331 return 8+track->vosLen;
00332 }
00333
00334 static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
00335 {
00336 offset_t pos = url_ftell(pb);
00337 int version = track->mode == MODE_MOV &&
00338 (track->audio_vbr ||
00339 track->enc->codec_id == CODEC_ID_PCM_S32LE ||
00340 track->enc->codec_id == CODEC_ID_PCM_S24LE);
00341
00342 put_be32(pb, 0);
00343 put_le32(pb, track->tag);
00344 put_be32(pb, 0);
00345 put_be16(pb, 0);
00346 put_be16(pb, 1);
00347
00348
00349 put_be16(pb, version);
00350 put_be16(pb, 0);
00351 put_be32(pb, 0);
00352
00353 if (track->mode == MODE_MOV) {
00354 put_be16(pb, track->enc->channels);
00355 if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
00356 track->enc->codec_id == CODEC_ID_PCM_S8)
00357 put_be16(pb, 8);
00358 else
00359 put_be16(pb, 16);
00360 put_be16(pb, track->audio_vbr ? -2 : 0);
00361 } else {
00362 put_be16(pb, 2);
00363 put_be16(pb, 16);
00364 put_be16(pb, 0);
00365 }
00366
00367 put_be16(pb, 0);
00368 put_be16(pb, track->timescale);
00369 put_be16(pb, 0);
00370
00371 if(version == 1) {
00372 put_be32(pb, track->enc->frame_size);
00373 put_be32(pb, track->sampleSize / track->enc->channels);
00374 put_be32(pb, track->sampleSize);
00375 put_be32(pb, 2);
00376 }
00377
00378 if(track->mode == MODE_MOV &&
00379 (track->enc->codec_id == CODEC_ID_AAC ||
00380 track->enc->codec_id == CODEC_ID_AMR_NB ||
00381 track->enc->codec_id == CODEC_ID_PCM_S24LE ||
00382 track->enc->codec_id == CODEC_ID_PCM_S32LE))
00383 mov_write_wave_tag(pb, track);
00384 else if(track->tag == MKTAG('m','p','4','a'))
00385 mov_write_esds_tag(pb, track);
00386 else if(track->enc->codec_id == CODEC_ID_AMR_NB)
00387 mov_write_amr_tag(pb, track);
00388 else if(track->vosLen > 0)
00389 mov_write_glbl_tag(pb, track);
00390
00391 return updateSize (pb, pos);
00392 }
00393
00394 static int mov_write_d263_tag(ByteIOContext *pb)
00395 {
00396 put_be32(pb, 0xf);
00397 put_tag(pb, "d263");
00398 put_tag(pb, "FFMP");
00399 put_byte(pb, 0);
00400
00401 put_byte(pb, 0xa);
00402 put_byte(pb, 0);
00403 return 0xf;
00404 }
00405
00406
00407 static int mov_write_svq3_tag(ByteIOContext *pb)
00408 {
00409 put_be32(pb, 0x15);
00410 put_tag(pb, "SMI ");
00411 put_tag(pb, "SEQH");
00412 put_be32(pb, 0x5);
00413 put_be32(pb, 0xe2c0211d);
00414 put_be32(pb, 0xc0000000);
00415 put_byte(pb, 0);
00416 return 0x15;
00417 }
00418
00419 static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
00420 {
00421 offset_t pos = url_ftell(pb);
00422
00423 put_be32(pb, 0);
00424 put_tag(pb, "avcC");
00425 ff_isom_write_avcc(pb, track->vosData, track->vosLen);
00426 return updateSize(pb, pos);
00427 }
00428
00429
00430 static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
00431 {
00432 int i;
00433 put_be32(pb, 24);
00434 put_tag(pb, "ACLR");
00435 put_tag(pb, "ACLR");
00436 put_tag(pb, "0001");
00437 put_be32(pb, 1);
00438 put_be32(pb, 0);
00439
00440 put_be32(pb, 24);
00441 put_tag(pb, "APRG");
00442 put_tag(pb, "APRG");
00443 put_tag(pb, "0001");
00444 put_be32(pb, 1);
00445 put_be32(pb, 0);
00446
00447 put_be32(pb, 120);
00448 put_tag(pb, "ARES");
00449 put_tag(pb, "ARES");
00450 put_tag(pb, "0001");
00451 put_be32(pb, AV_RB32(track->vosData + 0x28));
00452 put_be32(pb, track->enc->width);
00453
00454 if (track->vosData[5] & 2) {
00455 put_be32(pb, track->enc->height/2);
00456 put_be32(pb, 2);
00457 put_be32(pb, 0);
00458 put_be32(pb, 4);
00459 } else {
00460 put_be32(pb, track->enc->height);
00461 put_be32(pb, 1);
00462 put_be32(pb, 0);
00463 if (track->enc->height == 1080)
00464 put_be32(pb, 5);
00465 else
00466 put_be32(pb, 6);
00467 }
00468
00469 for (i = 0; i < 10; i++)
00470 put_be64(pb, 0);
00471
00472
00473 put_be32(pb, 0);
00474 return 0;
00475 }
00476
00477 static const AVCodecTag codec_3gp_tags[] = {
00478 { CODEC_ID_H263, MKTAG('s','2','6','3') },
00479 { CODEC_ID_H264, MKTAG('a','v','c','1') },
00480 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
00481 { CODEC_ID_AAC, MKTAG('m','p','4','a') },
00482 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
00483 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
00484 };
00485
00486 static const AVCodecTag mov_pix_fmt_tags[] = {
00487 { PIX_FMT_YUYV422, MKTAG('y','u','v','s') },
00488 { PIX_FMT_UYVY422, MKTAG('2','v','u','y') },
00489 { PIX_FMT_BGR555, MKTAG('r','a','w',' ') },
00490 { PIX_FMT_RGB24, MKTAG('r','a','w',' ') },
00491 { PIX_FMT_BGR32_1, MKTAG('r','a','w',' ') },
00492 };
00493
00494 static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
00495 {
00496 int tag = track->enc->codec_tag;
00497 if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
00498 if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
00499 return 0;
00500 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
00501 else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
00502 else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
00503 } else if (track->mode == MODE_3GP || track->mode == MODE_3G2) {
00504 tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
00505 } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
00506 (tag == MKTAG('d','v','c','p') ||
00507 track->enc->codec_id == CODEC_ID_RAWVIDEO))) {
00508 if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
00509 if (track->enc->height == 480)
00510 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
00511 else tag = MKTAG('d','v','c',' ');
00512 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
00513 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
00514 else tag = MKTAG('d','v','p','p');
00515 } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
00516 tag = codec_get_tag(mov_pix_fmt_tags, track->enc->pix_fmt);
00517 if (!tag)
00518 tag = track->enc->codec_tag;
00519 } else {
00520 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
00521 tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
00522 if (!tag) {
00523 tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
00524 if (tag)
00525 av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
00526 "the file may be unplayable!\n");
00527 }
00528 } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
00529 tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
00530 if (!tag) {
00531 int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
00532 if (ms_tag) {
00533 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
00534 av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
00535 "the file may be unplayable!\n");
00536 }
00537 }
00538 }
00539 }
00540 }
00541 return tag;
00542 }
00543
00544 static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
00545 {
00546 offset_t pos = url_ftell(pb);
00547 char compressor_name[32];
00548
00549 put_be32(pb, 0);
00550 put_le32(pb, track->tag);
00551 put_be32(pb, 0);
00552 put_be16(pb, 0);
00553 put_be16(pb, 1);
00554
00555 put_be16(pb, 0);
00556 put_be16(pb, 0);
00557 if (track->mode == MODE_MOV) {
00558 put_tag(pb, "FFMP");
00559 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
00560 put_be32(pb, 0);
00561 put_be32(pb, 0x400);
00562 } else {
00563 put_be32(pb, 0x200);
00564 put_be32(pb, 0x200);
00565 }
00566 } else {
00567 put_be32(pb, 0);
00568 put_be32(pb, 0);
00569 put_be32(pb, 0);
00570 }
00571 put_be16(pb, track->enc->width);
00572 put_be16(pb, track->enc->height);
00573 put_be32(pb, 0x00480000);
00574 put_be32(pb, 0x00480000);
00575 put_be32(pb, 0);
00576 put_be16(pb, 1);
00577
00578 memset(compressor_name,0,32);
00579
00580 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
00581 strncpy(compressor_name,track->enc->codec->name,31);
00582 put_byte(pb, strlen(compressor_name));
00583 put_buffer(pb, compressor_name, 31);
00584
00585 if (track->mode == MODE_MOV && track->enc->bits_per_sample)
00586 put_be16(pb, track->enc->bits_per_sample);
00587 else
00588 put_be16(pb, 0x18);
00589 put_be16(pb, 0xffff);
00590 if(track->tag == MKTAG('m','p','4','v'))
00591 mov_write_esds_tag(pb, track);
00592 else if(track->enc->codec_id == CODEC_ID_H263)
00593 mov_write_d263_tag(pb);
00594 else if(track->enc->codec_id == CODEC_ID_SVQ3)
00595 mov_write_svq3_tag(pb);
00596 else if(track->enc->codec_id == CODEC_ID_H264)
00597 mov_write_avcc_tag(pb, track);
00598 else if(track->enc->codec_id == CODEC_ID_DNXHD)
00599 mov_write_avid_tag(pb, track);
00600 else if(track->vosLen > 0)
00601 mov_write_glbl_tag(pb, track);
00602
00603 return updateSize (pb, pos);
00604 }
00605
00606 static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
00607 {
00608 offset_t pos = url_ftell(pb);
00609 put_be32(pb, 0);
00610 put_tag(pb, "stsd");
00611 put_be32(pb, 0);
00612 put_be32(pb, 1);
00613 if (track->enc->codec_type == CODEC_TYPE_VIDEO)
00614 mov_write_video_tag(pb, track);
00615 else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
00616 mov_write_audio_tag(pb, track);
00617 return updateSize(pb, pos);
00618 }
00619
00620 static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack* track)
00621 {
00622 MOV_stts_t *ctts_entries;
00623 uint32_t entries = 0;
00624 uint32_t atom_size;
00625 int i;
00626
00627 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries));
00628 ctts_entries[0].count = 1;
00629 ctts_entries[0].duration = track->cluster[0].cts;
00630 for (i=1; i<track->entry; i++) {
00631 if (track->cluster[i].cts == ctts_entries[entries].duration) {
00632 ctts_entries[entries].count++;
00633 } else {
00634 entries++;
00635 ctts_entries[entries].duration = track->cluster[i].cts;
00636 ctts_entries[entries].count = 1;
00637 }
00638 }
00639 entries++;
00640 atom_size = 16 + (entries * 8);
00641 put_be32(pb, atom_size);
00642 put_tag(pb, "ctts");
00643 put_be32(pb, 0);
00644 put_be32(pb, entries);
00645 for (i=0; i<entries; i++) {
00646 put_be32(pb, ctts_entries[i].count);
00647 put_be32(pb, ctts_entries[i].duration);
00648 }
00649 av_free(ctts_entries);
00650 return atom_size;
00651 }
00652
00653
00654 static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
00655 {
00656 MOV_stts_t *stts_entries;
00657 uint32_t entries = -1;
00658 uint32_t atom_size;
00659 int i;
00660
00661 if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
00662 stts_entries = av_malloc(sizeof(*stts_entries));
00663 stts_entries[0].count = track->sampleCount;
00664 stts_entries[0].duration = 1;
00665 entries = 1;
00666 } else {
00667 stts_entries = av_malloc(track->entry * sizeof(*stts_entries));
00668 for (i=0; i<track->entry; i++) {
00669 int64_t duration = i + 1 == track->entry ?
00670 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts :
00671 track->cluster[i+1].dts - track->cluster[i].dts;
00672 if (i && duration == stts_entries[entries].duration) {
00673 stts_entries[entries].count++;
00674 } else {
00675 entries++;
00676 stts_entries[entries].duration = duration;
00677 stts_entries[entries].count = 1;
00678 }
00679 }
00680 entries++;
00681 }
00682 atom_size = 16 + (entries * 8);
00683 put_be32(pb, atom_size);
00684 put_tag(pb, "stts");
00685 put_be32(pb, 0);
00686 put_be32(pb, entries);
00687 for (i=0; i<entries; i++) {
00688 put_be32(pb, stts_entries[i].count);
00689 put_be32(pb, stts_entries[i].duration);
00690 }
00691 av_free(stts_entries);
00692 return atom_size;
00693 }
00694
00695 static int mov_write_dref_tag(ByteIOContext *pb)
00696 {
00697 put_be32(pb, 28);
00698 put_tag(pb, "dref");
00699 put_be32(pb, 0);
00700 put_be32(pb, 1);
00701
00702 put_be32(pb, 0xc);
00703 put_tag(pb, "url ");
00704 put_be32(pb, 1);
00705
00706 return 28;
00707 }
00708
00709 static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
00710 {
00711 offset_t pos = url_ftell(pb);
00712 put_be32(pb, 0);
00713 put_tag(pb, "stbl");
00714 mov_write_stsd_tag(pb, track);
00715 mov_write_stts_tag(pb, track);
00716 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
00717 track->hasKeyframes < track->entry)
00718 mov_write_stss_tag(pb, track);
00719 if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
00720 track->hasBframes)
00721 mov_write_ctts_tag(pb, track);
00722 mov_write_stsc_tag(pb, track);
00723 mov_write_stsz_tag(pb, track);
00724 mov_write_stco_tag(pb, track);
00725 return updateSize(pb, pos);
00726 }
00727
00728 static int mov_write_dinf_tag(ByteIOContext *pb)
00729 {
00730 offset_t pos = url_ftell(pb);
00731 put_be32(pb, 0);
00732 put_tag(pb, "dinf");
00733 mov_write_dref_tag(pb);
00734 return updateSize(pb, pos);
00735 }
00736
00737 static int mov_write_smhd_tag(ByteIOContext *pb)
00738 {
00739 put_be32(pb, 16);
00740 put_tag(pb, "smhd");
00741 put_be32(pb, 0);
00742 put_be16(pb, 0);
00743 put_be16(pb, 0);
00744 return 16;
00745 }
00746
00747 static int mov_write_vmhd_tag(ByteIOContext *pb)
00748 {
00749 put_be32(pb, 0x14);
00750 put_tag(pb, "vmhd");
00751 put_be32(pb, 0x01);
00752 put_be64(pb, 0);
00753 return 0x14;
00754 }
00755
00756 static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
00757 {
00758 const char *descr, *hdlr, *hdlr_type;
00759 offset_t pos = url_ftell(pb);
00760
00761 if (!track) {
00762 hdlr = "dhlr";
00763 hdlr_type = "url ";
00764 descr = "DataHandler";
00765 } else {
00766 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
00767 if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
00768 hdlr_type = "vide";
00769 descr = "VideoHandler";
00770 } else {
00771 hdlr_type = "soun";
00772 descr = "SoundHandler";
00773 }
00774 }
00775
00776 put_be32(pb, 0);
00777 put_tag(pb, "hdlr");
00778 put_be32(pb, 0);
00779 put_buffer(pb, hdlr, 4);
00780 put_tag(pb, hdlr_type);
00781 put_be32(pb ,0);
00782 put_be32(pb ,0);
00783 put_be32(pb ,0);
00784 put_byte(pb, strlen(descr));
00785 put_buffer(pb, descr, strlen(descr));
00786 return updateSize(pb, pos);
00787 }
00788
00789 static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
00790 {
00791 offset_t pos = url_ftell(pb);
00792 put_be32(pb, 0);
00793 put_tag(pb, "minf");
00794 if(track->enc->codec_type == CODEC_TYPE_VIDEO)
00795 mov_write_vmhd_tag(pb);
00796 else
00797 mov_write_smhd_tag(pb);
00798 if (track->mode == MODE_MOV)
00799 mov_write_hdlr_tag(pb, NULL);
00800 mov_write_dinf_tag(pb);
00801 mov_write_stbl_tag(pb, track);
00802 return updateSize(pb, pos);
00803 }
00804
00805 static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
00806 {
00807 int version = track->trackDuration < INT32_MAX ? 0 : 1;
00808
00809 (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32);
00810 put_tag(pb, "mdhd");
00811 put_byte(pb, version);
00812 put_be24(pb, 0);
00813 if (version == 1) {
00814 put_be64(pb, track->time);
00815 put_be64(pb, track->time);
00816 } else {
00817 put_be32(pb, track->time);
00818 put_be32(pb, track->time);
00819 }
00820 put_be32(pb, track->timescale);
00821 (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration);
00822 put_be16(pb, track->language);
00823 put_be16(pb, 0);
00824
00825 if(version!=0 && track->mode == MODE_MOV){
00826 av_log(NULL, AV_LOG_ERROR,
00827 "FATAL error, file duration too long for timebase, this file will not be\n"
00828 "playable with quicktime. Choose a different timebase or a different\n"
00829 "container format\n");
00830 }
00831
00832 return 32;
00833 }
00834
00835 static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
00836 {
00837 offset_t pos = url_ftell(pb);
00838 put_be32(pb, 0);
00839 put_tag(pb, "mdia");
00840 mov_write_mdhd_tag(pb, track);
00841 mov_write_hdlr_tag(pb, track);
00842 mov_write_minf_tag(pb, track);
00843 return updateSize(pb, pos);
00844 }
00845
00846 static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
00847 {
00848 int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
00849 int version = duration < INT32_MAX ? 0 : 1;
00850
00851 (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92);
00852 put_tag(pb, "tkhd");
00853 put_byte(pb, version);
00854 put_be24(pb, 0xf);
00855 if (version == 1) {
00856 put_be64(pb, track->time);
00857 put_be64(pb, track->time);
00858 } else {
00859 put_be32(pb, track->time);
00860 put_be32(pb, track->time);
00861 }
00862 put_be32(pb, track->trackID);
00863 put_be32(pb, 0);
00864 (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
00865
00866 put_be32(pb, 0);
00867 put_be32(pb, 0);
00868 put_be32(pb, 0x0);
00869
00870 if(track->enc->codec_type == CODEC_TYPE_AUDIO)
00871 put_be16(pb, 0x0100);
00872 else
00873 put_be16(pb, 0);
00874 put_be16(pb, 0);
00875
00876
00877 put_be32(pb, 0x00010000);
00878 put_be32(pb, 0x0);
00879 put_be32(pb, 0x0);
00880 put_be32(pb, 0x0);
00881 put_be32(pb, 0x00010000);
00882 put_be32(pb, 0x0);
00883 put_be32(pb, 0x0);
00884 put_be32(pb, 0x0);
00885 put_be32(pb, 0x40000000);
00886
00887
00888 if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
00889 double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
00890 if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
00891 put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
00892 put_be32(pb, track->enc->height*0x10000);
00893 }
00894 else {
00895 put_be32(pb, 0);
00896 put_be32(pb, 0);
00897 }
00898 return 0x5c;
00899 }
00900
00901
00902 static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
00903 {
00904 put_be32(pb, 0x24);
00905 put_tag(pb, "edts");
00906 put_be32(pb, 0x1c);
00907 put_tag(pb, "elst");
00908 put_be32(pb, 0x0);
00909 put_be32(pb, 0x1);
00910
00911 put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP));
00912
00913 put_be32(pb, track->cluster[0].cts);
00914 put_be32(pb, 0x00010000);
00915 return 0x24;
00916 }
00917
00918
00919 static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
00920 {
00921 put_be32(pb, 0x34);
00922 put_tag(pb, "uuid");
00923 put_tag(pb, "USMT");
00924 put_be32(pb, 0x21d24fce);
00925 put_be32(pb, 0xbb88695c);
00926 put_be32(pb, 0xfac9c740);
00927 put_be32(pb, 0x1c);
00928 put_tag(pb, "MTDT");
00929 put_be32(pb, 0x00010012);
00930 put_be32(pb, 0x0a);
00931 put_be32(pb, 0x55c40000);
00932 put_be32(pb, 0x1);
00933 put_be32(pb, 0x0);
00934 return 0x34;
00935 }
00936
00937 static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
00938 {
00939 offset_t pos = url_ftell(pb);
00940 put_be32(pb, 0);
00941 put_tag(pb, "trak");
00942 mov_write_tkhd_tag(pb, track);
00943 if (track->mode == MODE_PSP || track->hasBframes)
00944 mov_write_edts_tag(pb, track);
00945 mov_write_mdia_tag(pb, track);
00946 if (track->mode == MODE_PSP)
00947 mov_write_uuid_tag_psp(pb,track);
00948 return updateSize(pb, pos);
00949 }
00950
00951 #if 0
00952
00953 static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
00954 {
00955 put_be32(pb, 0x15);
00956 put_tag(pb, "iods");
00957 put_be32(pb, 0);
00958 put_be16(pb, 0x1007);
00959 put_byte(pb, 0);
00960 put_be16(pb, 0x4fff);
00961 put_be16(pb, 0xfffe);
00962 put_be16(pb, 0x01ff);
00963 return 0x15;
00964 }
00965 #endif
00966
00967 static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
00968 {
00969 int maxTrackID = 1, i;
00970 int64_t maxTrackLenTemp, maxTrackLen = 0;
00971 int version;
00972
00973 for (i=0; i<mov->nb_streams; i++) {
00974 if(mov->tracks[i].entry > 0) {
00975 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
00976 if(maxTrackLen < maxTrackLenTemp)
00977 maxTrackLen = maxTrackLenTemp;
00978 if(maxTrackID < mov->tracks[i].trackID)
00979 maxTrackID = mov->tracks[i].trackID;
00980 }
00981 }
00982
00983 version = maxTrackLen < UINT32_MAX ? 0 : 1;
00984 (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108);
00985 put_tag(pb, "mvhd");
00986 put_byte(pb, version);
00987 put_be24(pb, 0);
00988 if (version == 1) {
00989 put_be64(pb, mov->time);
00990 put_be64(pb, mov->time);
00991 } else {
00992 put_be32(pb, mov->time);
00993 put_be32(pb, mov->time);
00994 }
00995 put_be32(pb, mov->timescale);
00996 (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen);
00997
00998 put_be32(pb, 0x00010000);
00999 put_be16(pb, 0x0100);
01000 put_be16(pb, 0);
01001 put_be32(pb, 0);
01002 put_be32(pb, 0);
01003
01004
01005 put_be32(pb, 0x00010000);
01006 put_be32(pb, 0x0);
01007 put_be32(pb, 0x0);
01008 put_be32(pb, 0x0);
01009 put_be32(pb, 0x00010000);
01010 put_be32(pb, 0x0);
01011 put_be32(pb, 0x0);
01012 put_be32(pb, 0x0);
01013 put_be32(pb, 0x40000000);
01014
01015 put_be32(pb, 0);
01016 put_be32(pb, 0);
01017 put_be32(pb, 0);
01018 put_be32(pb, 0);
01019 put_be32(pb, 0);
01020 put_be32(pb, 0);
01021 put_be32(pb, maxTrackID+1);
01022 return 0x6c;
01023 }
01024
01025 static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
01026 AVFormatContext *s)
01027 {
01028 offset_t pos = url_ftell(pb);
01029 put_be32(pb, 0);
01030 put_tag(pb, "hdlr");
01031 put_be32(pb, 0);
01032 put_be32(pb, 0);
01033 put_tag(pb, "mdir");
01034 put_tag(pb, "appl");
01035 put_be32(pb, 0);
01036 put_be32(pb, 0);
01037 put_be16(pb, 0);
01038 return updateSize(pb, pos);
01039 }
01040
01041
01042 static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
01043 {
01044 if(long_style){
01045 offset_t pos = url_ftell(pb);
01046 put_be32(pb, 0);
01047 put_tag(pb, "data");
01048 put_be32(pb, 1);
01049 put_be32(pb, 0);
01050 put_buffer(pb, data, strlen(data));
01051 return updateSize(pb, pos);
01052 }else{
01053 put_be16(pb, strlen(data));
01054 put_be16(pb, 0);
01055 put_buffer(pb, data, strlen(data));
01056 return strlen(data) + 4;
01057 }
01058 }
01059
01060 static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
01061 int size = 0;
01062 if ( value && value[0] ) {
01063 offset_t pos = url_ftell(pb);
01064 put_be32(pb, 0);
01065 put_tag(pb, name);
01066 mov_write_string_data_tag(pb, value, long_style);
01067 size= updateSize(pb, pos);
01068 }
01069 return size;
01070 }
01071
01072
01073 static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
01074 {
01075 if(year){
01076 char year_str[5];
01077 snprintf(year_str, sizeof(year_str), "%04d", year);
01078 return mov_write_string_tag(pb, "\251day", year_str, long_style);
01079 }else
01080 return 0;
01081 }
01082
01083
01084 static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
01085 AVFormatContext *s)
01086 {
01087 int size = 0;
01088 if ( s->track ) {
01089 offset_t pos = url_ftell(pb);
01090 put_be32(pb, 0);
01091 put_tag(pb, "trkn");
01092 {
01093 offset_t pos = url_ftell(pb);
01094 put_be32(pb, 0);
01095 put_tag(pb, "data");
01096 put_be32(pb, 0);
01097 put_be32(pb, 0);
01098 put_be16(pb, 0);
01099 put_be16(pb, s->track);
01100 put_be16(pb, 0);
01101 put_be16(pb, 0);
01102 updateSize(pb, pos);
01103 }
01104 size = updateSize(pb, pos);
01105 }
01106 return size;
01107 }
01108
01109
01110 static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
01111 AVFormatContext *s)
01112 {
01113 offset_t pos = url_ftell(pb);
01114 put_be32(pb, 0);
01115 put_tag(pb, "ilst");
01116 mov_write_string_tag(pb, "\251nam", s->title , 1);
01117 mov_write_string_tag(pb, "\251ART", s->author , 1);
01118 mov_write_string_tag(pb, "\251wrt", s->author , 1);
01119 mov_write_string_tag(pb, "\251alb", s->album , 1);
01120 mov_write_day_tag(pb, s->year ,1);
01121 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
01122 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
01123 mov_write_string_tag(pb, "\251cmt", s->comment , 1);
01124 mov_write_string_tag(pb, "\251gen", s->genre , 1);
01125 mov_write_trkn_tag(pb, mov, s);
01126 return updateSize(pb, pos);
01127 }
01128
01129
01130 static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
01131 AVFormatContext *s)
01132 {
01133 int size = 0;
01134
01135
01136 if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
01137 s->comment[0] || s->genre[0] || s->track ) {
01138 offset_t pos = url_ftell(pb);
01139 put_be32(pb, 0);
01140 put_tag(pb, "meta");
01141 put_be32(pb, 0);
01142 mov_write_itunes_hdlr_tag(pb, mov, s);
01143 mov_write_ilst_tag(pb, mov, s);
01144 size = updateSize(pb, pos);
01145 }
01146 return size;
01147 }
01148
01149 static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
01150 AVFormatContext *s)
01151 {
01152 int i, req = 0;
01153
01154
01155 for (i=0; i<mov->nb_streams; i++) {
01156 if(mov->tracks[i].entry <= 0) continue;
01157 if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
01158 mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
01159 req = 1;
01160 break;
01161 }
01162 }
01163
01164 if (s->title[0] || s->author[0] || s->album[0] || s->year ||
01165 s->comment[0] || s->genre[0] || s->track ||
01166 (mov->mode == MODE_MOV &&
01167 ((mov->tracks[0].enc && !mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT) || req))) {
01168 offset_t pos = url_ftell(pb);
01169
01170 put_be32(pb, 0);
01171 put_tag(pb, "udta");
01172
01173
01174 mov_write_meta_tag(pb, mov, s);
01175
01176 if(mov->mode == MODE_MOV){
01177
01178 if (req)
01179 mov_write_string_tag(pb, "\251req", "QuickTime 6.0 or greater", 0);
01180
01181 mov_write_string_tag(pb, "\251nam", s->title , 0);
01182 mov_write_string_tag(pb, "\251aut", s->author , 0);
01183 mov_write_string_tag(pb, "\251alb", s->album , 0);
01184 mov_write_day_tag(pb, s->year, 0);
01185 if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
01186 mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
01187 mov_write_string_tag(pb, "\251des", s->comment , 0);
01188 mov_write_string_tag(pb, "\251gen", s->genre , 0);
01189 }
01190
01191 return updateSize(pb, pos);
01192 }
01193
01194 return 0;
01195 }
01196
01197 static int utf8len(const uint8_t *b){
01198 int len=0;
01199 int val;
01200 while(*b){
01201 GET_UTF8(val, *b++, return -1;)
01202 len++;
01203 }
01204 return len;
01205 }
01206
01207 static int ascii_to_wc (ByteIOContext *pb, const uint8_t *b)
01208 {
01209 int val;
01210 while(*b){
01211 GET_UTF8(val, *b++, return -1;)
01212 put_be16(pb, val);
01213 }
01214 put_be16(pb, 0x00);
01215 return 0;
01216 }
01217
01218 static uint16_t language_code (const char *str)
01219 {
01220 return ((((str[0]-0x60) & 0x1F)<<10) + (((str[1]-0x60) & 0x1F)<<5) + ((str[2]-0x60) & 0x1F));
01221 }
01222
01223 static int mov_write_uuidusmt_tag (ByteIOContext *pb, AVFormatContext *s)
01224 {
01225 size_t len, size;
01226 offset_t pos, curpos;
01227
01228 size = 0;
01229 if (s->title[0]) {
01230 pos = url_ftell(pb);
01231 put_be32(pb, 0);
01232 put_tag(pb, "uuid");
01233 put_tag(pb, "USMT");
01234 put_be32(pb, 0x21d24fce );
01235 put_be32(pb, 0xbb88695c );
01236 put_be32(pb, 0xfac9c740 );
01237 size += 24;
01238
01239 put_be32(pb, 0);
01240 put_tag(pb, "MTDT");
01241 put_be16(pb, 4);
01242 size += 10;
01243
01244
01245 put_be16(pb, 0x0C);
01246 put_be32(pb, 0x0B);
01247 put_be16(pb, language_code("und"));
01248 put_be16(pb, 0x0);
01249 put_be16(pb, 0x021C);
01250 size += 12;
01251
01252
01253 len = utf8len(LIBAVCODEC_IDENT)+1;
01254 if(len<=0)
01255 goto not_utf8;
01256 put_be16(pb, len*2+10);
01257 put_be32(pb, 0x04);
01258 put_be16(pb, language_code("eng"));
01259 put_be16(pb, 0x01);
01260 ascii_to_wc(pb, LIBAVCODEC_IDENT);
01261 size += len*2+10;
01262
01263
01264 len = utf8len(s->title)+1;
01265 if(len<=0)
01266 goto not_utf8;
01267 put_be16(pb, len*2+10);
01268 put_be32(pb, 0x01);
01269 put_be16(pb, language_code("eng"));
01270 put_be16(pb, 0x01);
01271 ascii_to_wc (pb, s->title);
01272 size += len*2+10;
01273
01274
01275
01276 len = utf8len("2006/04/01 11:11:11")+1;
01277 if(len<=0)
01278 goto not_utf8;
01279 put_be16(pb, len*2+10);
01280 put_be32(pb, 0x03);
01281 put_be16(pb, language_code("und"));
01282 put_be16(pb, 0x01);
01283 ascii_to_wc (pb, "2006/04/01 11:11:11");
01284 size += len*2+10;
01285
01286
01287 curpos = url_ftell(pb);
01288 url_fseek(pb, pos, SEEK_SET);
01289 put_be32(pb, size);
01290 url_fseek(pb, pos+24, SEEK_SET);
01291 put_be32(pb, size-24);
01292 url_fseek(pb, curpos, SEEK_SET);
01293 }
01294
01295 return size;
01296 not_utf8:
01297 av_log(s, AV_LOG_ERROR, "not utf8\n");
01298 return -1;
01299 }
01300
01301 static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
01302 AVFormatContext *s)
01303 {
01304 int i;
01305 offset_t pos = url_ftell(pb);
01306 put_be32(pb, 0);
01307 put_tag(pb, "moov");
01308 mov->timescale = globalTimescale;
01309
01310 for (i=0; i<mov->nb_streams; i++) {
01311 if(mov->tracks[i].entry <= 0) continue;
01312
01313 mov->tracks[i].time = mov->time;
01314 mov->tracks[i].trackID = i+1;
01315 }
01316
01317 mov_write_mvhd_tag(pb, mov);
01318
01319 for (i=0; i<mov->nb_streams; i++) {
01320 if(mov->tracks[i].entry > 0) {
01321 mov_write_trak_tag(pb, &(mov->tracks[i]));
01322 }
01323 }
01324
01325 if (mov->mode == MODE_PSP)
01326 mov_write_uuidusmt_tag(pb, s);
01327 else if (mov->mode != MODE_3GP && mov->mode != MODE_3G2)
01328 mov_write_udta_tag(pb, mov, s);
01329
01330 return updateSize(pb, pos);
01331 }
01332
01333 static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
01334 {
01335 put_be32(pb, 8);
01336 put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
01337
01338 mov->mdat_pos = url_ftell(pb);
01339 put_be32(pb, 0);
01340 put_tag(pb, "mdat");
01341 return 0;
01342 }
01343
01344
01345 static void mov_write_ftyp_tag (ByteIOContext *pb, AVFormatContext *s)
01346 {
01347 MOVContext *mov = s->priv_data;
01348
01349 put_be32(pb, 0x14 );
01350 put_tag(pb, "ftyp");
01351
01352 if ( mov->mode == MODE_3GP )
01353 put_tag(pb, "3gp4");
01354 else if ( mov->mode == MODE_3G2 )
01355 put_tag(pb, "3g2a");
01356 else if ( mov->mode == MODE_PSP )
01357 put_tag(pb, "MSNV");
01358 else if ( mov->mode == MODE_MP4 )
01359 put_tag(pb, "isom");
01360 else
01361 put_tag(pb, "qt ");
01362
01363 put_be32(pb, 0x200 );
01364
01365 if ( mov->mode == MODE_3GP )
01366 put_tag(pb, "3gp4");
01367 else if ( mov->mode == MODE_3G2 )
01368 put_tag(pb, "3g2a");
01369 else if ( mov->mode == MODE_PSP )
01370 put_tag(pb, "MSNV");
01371 else if ( mov->mode == MODE_MP4 )
01372 put_tag(pb, "mp41");
01373 else
01374 put_tag(pb, "qt ");
01375 }
01376
01377 static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
01378 {
01379 AVCodecContext *VideoCodec = s->streams[0]->codec;
01380 AVCodecContext *AudioCodec = s->streams[1]->codec;
01381 int AudioRate = AudioCodec->sample_rate;
01382 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
01383 int audio_kbitrate= AudioCodec->bit_rate / 1000;
01384 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
01385
01386 put_be32(pb, 0x94 );
01387 put_tag(pb, "uuid");
01388 put_tag(pb, "PROF");
01389
01390 put_be32(pb, 0x21d24fce );
01391 put_be32(pb, 0xbb88695c );
01392 put_be32(pb, 0xfac9c740 );
01393
01394 put_be32(pb, 0x0 );
01395 put_be32(pb, 0x3 );
01396
01397 put_be32(pb, 0x14 );
01398 put_tag(pb, "FPRF");
01399 put_be32(pb, 0x0 );
01400 put_be32(pb, 0x0 );
01401 put_be32(pb, 0x0 );
01402
01403 put_be32(pb, 0x2c );
01404 put_tag(pb, "APRF");
01405 put_be32(pb, 0x0 );
01406 put_be32(pb, 0x2 );
01407 put_tag(pb, "mp4a");
01408 put_be32(pb, 0x20f );
01409 put_be32(pb, 0x0 );
01410 put_be32(pb, audio_kbitrate);
01411 put_be32(pb, audio_kbitrate);
01412 put_be32(pb, AudioRate );
01413 put_be32(pb, AudioCodec->channels );
01414
01415 put_be32(pb, 0x34 );
01416 put_tag(pb, "VPRF");
01417 put_be32(pb, 0x0 );
01418 put_be32(pb, 0x1 );
01419 if (VideoCodec->codec_id == CODEC_ID_H264) {
01420 put_tag(pb, "avc1");
01421 put_be16(pb, 0x014D );
01422 put_be16(pb, 0x0015 );
01423 } else {
01424 put_tag(pb, "mp4v");
01425 put_be16(pb, 0x0000 );
01426 put_be16(pb, 0x0103 );
01427 }
01428 put_be32(pb, 0x0 );
01429 put_be32(pb, video_kbitrate);
01430 put_be32(pb, video_kbitrate);
01431 put_be32(pb, FrameRate);
01432 put_be32(pb, FrameRate);
01433 put_be16(pb, VideoCodec->width);
01434 put_be16(pb, VideoCodec->height);
01435 put_be32(pb, 0x010001);
01436 }
01437
01438 static int mov_write_header(AVFormatContext *s)
01439 {
01440 ByteIOContext *pb = s->pb;
01441 MOVContext *mov = s->priv_data;
01442 int i;
01443
01444 if (url_is_streamed(s->pb)) {
01445 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
01446 return -1;
01447 }
01448
01449
01450 mov->mode = MODE_MP4;
01451
01452 if (s->oformat != NULL) {
01453 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
01454 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
01455 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
01456 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
01457
01458 mov_write_ftyp_tag(pb,s);
01459 if ( mov->mode == MODE_PSP ) {
01460 if ( s->nb_streams != 2 ) {
01461 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
01462 return -1;
01463 }
01464 mov_write_uuidprof_tag(pb,s);
01465 }
01466 }
01467
01468 for(i=0; i<s->nb_streams; i++){
01469 AVStream *st= s->streams[i];
01470 MOVTrack *track= &mov->tracks[i];
01471
01472 track->enc = st->codec;
01473 track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
01474 track->mode = mov->mode;
01475 track->tag = mov_find_codec_tag(s, track);
01476 if (!track->tag) {
01477 av_log(s, AV_LOG_ERROR, "track %d: could not find tag for codec\n", i);
01478 return -1;
01479 }
01480 if(st->codec->codec_type == CODEC_TYPE_VIDEO){
01481 track->timescale = st->codec->time_base.den;
01482 av_set_pts_info(st, 64, 1, st->codec->time_base.den);
01483 if (track->mode == MODE_MOV && track->timescale > 100000)
01484 av_log(s, AV_LOG_WARNING,
01485 "WARNING codec timebase is very high. If duration is too long,\n"
01486 "file may not be playable by quicktime. Specify a shorter timebase\n"
01487 "or choose different container.\n");
01488 }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
01489 track->timescale = st->codec->sample_rate;
01490 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
01491 if(!st->codec->frame_size){
01492 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
01493 return -1;
01494 }else if(st->codec->frame_size > 1){
01495 track->audio_vbr = 1;
01496 }else{
01497 track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
01498 }
01499 }
01500 }
01501
01502 mov_write_mdat_tag(pb, mov);
01503 mov->time = s->timestamp + 0x7C25B080;
01504 mov->nb_streams = s->nb_streams;
01505
01506 put_flush_packet(pb);
01507
01508 return 0;
01509 }
01510
01511 static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
01512 {
01513 MOVContext *mov = s->priv_data;
01514 ByteIOContext *pb = s->pb;
01515 MOVTrack *trk = &mov->tracks[pkt->stream_index];
01516 AVCodecContext *enc = trk->enc;
01517 unsigned int samplesInChunk = 0;
01518 int size= pkt->size;
01519
01520 if (url_is_streamed(s->pb)) return 0;
01521 if (!size) return 0;
01522
01523 if (enc->codec_id == CODEC_ID_AMR_NB) {
01524
01525 static uint16_t packed_size[16] =
01526 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
01527 int len = 0;
01528
01529 while (len < size && samplesInChunk < 100) {
01530 len += packed_size[(pkt->data[len] >> 3) & 0x0F];
01531 samplesInChunk++;
01532 }
01533 if(samplesInChunk > 1){
01534 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
01535 return -1;
01536 }
01537 } else if (trk->sampleSize)
01538 samplesInChunk = size/trk->sampleSize;
01539 else
01540 samplesInChunk = 1;
01541
01542
01543 if (trk->vosLen == 0 && enc->extradata_size > 0) {
01544 trk->vosLen = enc->extradata_size;
01545 trk->vosData = av_malloc(trk->vosLen);
01546 memcpy(trk->vosData, enc->extradata, trk->vosLen);
01547 }
01548
01549 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
01550
01551
01552 int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
01553 if (ret < 0)
01554 return ret;
01555 assert(pkt->size);
01556 size = pkt->size;
01557 } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
01558
01559 if (size < 640)
01560 return -1;
01561 trk->vosLen = 640;
01562 trk->vosData = av_malloc(trk->vosLen);
01563 memcpy(trk->vosData, pkt->data, 640);
01564 }
01565
01566 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
01567 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
01568 if (!trk->cluster)
01569 return -1;
01570 }
01571
01572 trk->cluster[trk->entry].pos = url_ftell(pb);
01573 trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
01574 trk->cluster[trk->entry].size = size;
01575 trk->cluster[trk->entry].entries = samplesInChunk;
01576 trk->cluster[trk->entry].dts = pkt->dts;
01577 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
01578
01579 if(enc->codec_type == CODEC_TYPE_VIDEO) {
01580 if (pkt->dts != pkt->pts)
01581 trk->hasBframes = 1;
01582 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
01583 trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
01584 if(trk->cluster[trk->entry].key_frame)
01585 trk->hasKeyframes++;
01586 }
01587 trk->entry++;
01588 trk->sampleCount += samplesInChunk;
01589 mov->mdat_size += size;
01590
01591 put_buffer(pb, pkt->data, size);
01592
01593 put_flush_packet(pb);
01594 return 0;
01595 }
01596
01597 static int mov_write_trailer(AVFormatContext *s)
01598 {
01599 MOVContext *mov = s->priv_data;
01600 ByteIOContext *pb = s->pb;
01601 int res = 0;
01602 int i;
01603
01604 offset_t moov_pos = url_ftell(pb);
01605
01606
01607 if (mov->mdat_size+8 <= UINT32_MAX) {
01608 url_fseek(pb, mov->mdat_pos, SEEK_SET);
01609 put_be32(pb, mov->mdat_size+8);
01610 } else {
01611
01612 url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
01613 put_be32(pb, 1);
01614 put_tag(pb, "mdat");
01615 put_be64(pb, mov->mdat_size+16);
01616 }
01617 url_fseek(pb, moov_pos, SEEK_SET);
01618
01619 mov_write_moov_tag(pb, mov, s);
01620
01621 for (i=0; i<mov->nb_streams; i++) {
01622 av_freep(&mov->tracks[i].cluster);
01623
01624 if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
01625
01626 }
01627
01628 put_flush_packet(pb);
01629
01630 return res;
01631 }
01632
01633 #ifdef CONFIG_MOV_MUXER
01634 AVOutputFormat mov_muxer = {
01635 "mov",
01636 "mov format",
01637 NULL,
01638 "mov",
01639 sizeof(MOVContext),
01640 CODEC_ID_AAC,
01641 CODEC_ID_MPEG4,
01642 mov_write_header,
01643 mov_write_packet,
01644 mov_write_trailer,
01645 .flags = AVFMT_GLOBALHEADER,
01646 .codec_tag = (const AVCodecTag*[]){codec_movvideo_tags, codec_movaudio_tags, 0},
01647 };
01648 #endif
01649 #ifdef CONFIG_TGP_MUXER
01650 AVOutputFormat tgp_muxer = {
01651 "3gp",
01652 "3gp format",
01653 NULL,
01654 "3gp",
01655 sizeof(MOVContext),
01656 CODEC_ID_AMR_NB,
01657 CODEC_ID_H263,
01658 mov_write_header,
01659 mov_write_packet,
01660 mov_write_trailer,
01661 .flags = AVFMT_GLOBALHEADER,
01662 .codec_tag = (const AVCodecTag*[]){codec_3gp_tags, 0},
01663 };
01664 #endif
01665 #ifdef CONFIG_MP4_MUXER
01666 AVOutputFormat mp4_muxer = {
01667 "mp4",
01668 "mp4 format",
01669 "application/mp4",
01670 "mp4,m4a",
01671 sizeof(MOVContext),
01672 CODEC_ID_AAC,
01673 CODEC_ID_MPEG4,
01674 mov_write_header,
01675 mov_write_packet,
01676 mov_write_trailer,
01677 .flags = AVFMT_GLOBALHEADER,
01678 .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
01679 };
01680 #endif
01681 #ifdef CONFIG_PSP_MUXER
01682 AVOutputFormat psp_muxer = {
01683 "psp",
01684 "psp mp4 format",
01685 NULL,
01686 "mp4,psp",
01687 sizeof(MOVContext),
01688 CODEC_ID_AAC,
01689 CODEC_ID_MPEG4,
01690 mov_write_header,
01691 mov_write_packet,
01692 mov_write_trailer,
01693 .flags = AVFMT_GLOBALHEADER,
01694 .codec_tag = (const AVCodecTag*[]){ff_mp4_obj_type, 0},
01695 };
01696 #endif
01697 #ifdef CONFIG_TG2_MUXER
01698 AVOutputFormat tg2_muxer = {
01699 "3g2",
01700 "3gp2 format",
01701 NULL,
01702 "3g2",
01703 sizeof(MOVContext),
01704 CODEC_ID_AMR_NB,
01705 CODEC_ID_H263,
01706 mov_write_header,
01707 mov_write_packet,
01708 mov_write_trailer,
01709 .flags = AVFMT_GLOBALHEADER,
01710 .codec_tag = (const AVCodecTag*[]){codec_3gp_tags, 0},
01711 };
01712 #endif