1/* 2 * MOV, 3GP, MP4 muxer 3 * Copyright (c) 2003 Thomas Raivio 4 * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org> 5 * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com> 6 * 7 * This file is part of Libav. 8 * 9 * Libav is free software; you can redistribute it and/or 10 * modify it under the terms of the GNU Lesser General Public 11 * License as published by the Free Software Foundation; either 12 * version 2.1 of the License, or (at your option) any later version. 13 * 14 * Libav is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public 20 * License along with Libav; if not, write to the Free Software 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 */ 23 24#include "movenc.h" 25#include "avformat.h" 26#include "avio_internal.h" 27#include "riff.h" 28#include "avio.h" 29#include "isom.h" 30#include "avc.h" 31#include "libavcodec/get_bits.h" 32#include "libavcodec/put_bits.h" 33#include "internal.h" 34#include "libavutil/avstring.h" 35#include "libavutil/intfloat.h" 36#include "libavutil/mathematics.h" 37#include "libavutil/opt.h" 38#include "libavutil/dict.h" 39#include "rtpenc.h" 40#include "mov_chan.h" 41 42#undef NDEBUG 43#include <assert.h> 44 45static const AVOption options[] = { 46 { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" }, 47 { "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" }, 48 FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags), 49 { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM}, 50 { "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}, 51 { "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}, 52 { NULL }, 53}; 54 55#define MOV_CLASS(flavor)\ 56static const AVClass flavor ## _muxer_class = {\ 57 .class_name = #flavor " muxer",\ 58 .item_name = av_default_item_name,\ 59 .option = options,\ 60 .version = LIBAVUTIL_VERSION_INT,\ 61}; 62 63//FIXME support 64 bit variant with wide placeholders 64static int64_t updateSize(AVIOContext *pb, int64_t pos) 65{ 66 int64_t curpos = avio_tell(pb); 67 avio_seek(pb, pos, SEEK_SET); 68 avio_wb32(pb, curpos - pos); /* rewrite size */ 69 avio_seek(pb, curpos, SEEK_SET); 70 71 return curpos - pos; 72} 73 74/* Chunk offset atom */ 75static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track) 76{ 77 int i; 78 int mode64 = 0; // use 32 bit size variant if possible 79 int64_t pos = avio_tell(pb); 80 avio_wb32(pb, 0); /* size */ 81 if (pos > UINT32_MAX) { 82 mode64 = 1; 83 ffio_wfourcc(pb, "co64"); 84 } else 85 ffio_wfourcc(pb, "stco"); 86 avio_wb32(pb, 0); /* version & flags */ 87 avio_wb32(pb, track->entry); /* entry count */ 88 for (i=0; i<track->entry; i++) { 89 if(mode64 == 1) 90 avio_wb64(pb, track->cluster[i].pos); 91 else 92 avio_wb32(pb, track->cluster[i].pos); 93 } 94 return updateSize(pb, pos); 95} 96 97/* Sample size atom */ 98static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track) 99{ 100 int equalChunks = 1; 101 int i, j, entries = 0, tst = -1, oldtst = -1; 102 103 int64_t pos = avio_tell(pb); 104 avio_wb32(pb, 0); /* size */ 105 ffio_wfourcc(pb, "stsz"); 106 avio_wb32(pb, 0); /* version & flags */ 107 108 for (i=0; i<track->entry; i++) { 109 tst = track->cluster[i].size/track->cluster[i].entries; 110 if(oldtst != -1 && tst != oldtst) { 111 equalChunks = 0; 112 } 113 oldtst = tst; 114 entries += track->cluster[i].entries; 115 } 116 if (equalChunks) { 117 int sSize = track->cluster[0].size/track->cluster[0].entries; 118 sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0 119 avio_wb32(pb, sSize); // sample size 120 avio_wb32(pb, entries); // sample count 121 } 122 else { 123 avio_wb32(pb, 0); // sample size 124 avio_wb32(pb, entries); // sample count 125 for (i=0; i<track->entry; i++) { 126 for (j=0; j<track->cluster[i].entries; j++) { 127 avio_wb32(pb, track->cluster[i].size / 128 track->cluster[i].entries); 129 } 130 } 131 } 132 return updateSize(pb, pos); 133} 134 135/* Sample to chunk atom */ 136static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track) 137{ 138 int index = 0, oldval = -1, i; 139 int64_t entryPos, curpos; 140 141 int64_t pos = avio_tell(pb); 142 avio_wb32(pb, 0); /* size */ 143 ffio_wfourcc(pb, "stsc"); 144 avio_wb32(pb, 0); // version & flags 145 entryPos = avio_tell(pb); 146 avio_wb32(pb, track->entry); // entry count 147 for (i=0; i<track->entry; i++) { 148 if(oldval != track->cluster[i].samplesInChunk) 149 { 150 avio_wb32(pb, i+1); // first chunk 151 avio_wb32(pb, track->cluster[i].samplesInChunk); // samples per chunk 152 avio_wb32(pb, 0x1); // sample description index 153 oldval = track->cluster[i].samplesInChunk; 154 index++; 155 } 156 } 157 curpos = avio_tell(pb); 158 avio_seek(pb, entryPos, SEEK_SET); 159 avio_wb32(pb, index); // rewrite size 160 avio_seek(pb, curpos, SEEK_SET); 161 162 return updateSize(pb, pos); 163} 164 165/* Sync sample atom */ 166static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag) 167{ 168 int64_t curpos, entryPos; 169 int i, index = 0; 170 int64_t pos = avio_tell(pb); 171 avio_wb32(pb, 0); // size 172 ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps"); 173 avio_wb32(pb, 0); // version & flags 174 entryPos = avio_tell(pb); 175 avio_wb32(pb, track->entry); // entry count 176 for (i=0; i<track->entry; i++) { 177 if (track->cluster[i].flags & flag) { 178 avio_wb32(pb, i+1); 179 index++; 180 } 181 } 182 curpos = avio_tell(pb); 183 avio_seek(pb, entryPos, SEEK_SET); 184 avio_wb32(pb, index); // rewrite size 185 avio_seek(pb, curpos, SEEK_SET); 186 return updateSize(pb, pos); 187} 188 189static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track) 190{ 191 avio_wb32(pb, 0x11); /* size */ 192 if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr"); 193 else ffio_wfourcc(pb, "damr"); 194 ffio_wfourcc(pb, "FFMP"); 195 avio_w8(pb, 0); /* decoder version */ 196 197 avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */ 198 avio_w8(pb, 0x00); /* Mode change period (no restriction) */ 199 avio_w8(pb, 0x01); /* Frames per sample */ 200 return 0x11; 201} 202 203static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track) 204{ 205 GetBitContext gbc; 206 PutBitContext pbc; 207 uint8_t buf[3]; 208 int fscod, bsid, bsmod, acmod, lfeon, frmsizecod; 209 210 if (track->vosLen < 7) 211 return -1; 212 213 avio_wb32(pb, 11); 214 ffio_wfourcc(pb, "dac3"); 215 216 init_get_bits(&gbc, track->vosData+4, (track->vosLen-4) * 8); 217 fscod = get_bits(&gbc, 2); 218 frmsizecod = get_bits(&gbc, 6); 219 bsid = get_bits(&gbc, 5); 220 bsmod = get_bits(&gbc, 3); 221 acmod = get_bits(&gbc, 3); 222 if (acmod == 2) { 223 skip_bits(&gbc, 2); // dsurmod 224 } else { 225 if ((acmod & 1) && acmod != 1) 226 skip_bits(&gbc, 2); // cmixlev 227 if (acmod & 4) 228 skip_bits(&gbc, 2); // surmixlev 229 } 230 lfeon = get_bits1(&gbc); 231 232 init_put_bits(&pbc, buf, sizeof(buf)); 233 put_bits(&pbc, 2, fscod); 234 put_bits(&pbc, 5, bsid); 235 put_bits(&pbc, 3, bsmod); 236 put_bits(&pbc, 3, acmod); 237 put_bits(&pbc, 1, lfeon); 238 put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code 239 put_bits(&pbc, 5, 0); // reserved 240 241 flush_put_bits(&pbc); 242 avio_write(pb, buf, sizeof(buf)); 243 244 return 11; 245} 246 247/** 248 * This function writes extradata "as is". 249 * Extradata must be formatted like a valid atom (with size and tag). 250 */ 251static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track) 252{ 253 avio_write(pb, track->enc->extradata, track->enc->extradata_size); 254 return track->enc->extradata_size; 255} 256 257static int mov_write_enda_tag(AVIOContext *pb) 258{ 259 avio_wb32(pb, 10); 260 ffio_wfourcc(pb, "enda"); 261 avio_wb16(pb, 1); /* little endian */ 262 return 10; 263} 264 265static void putDescr(AVIOContext *pb, int tag, unsigned int size) 266{ 267 int i = 3; 268 avio_w8(pb, tag); 269 for(; i>0; i--) 270 avio_w8(pb, (size>>(7*i)) | 0x80); 271 avio_w8(pb, size & 0x7F); 272} 273 274static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic 275{ 276 int64_t pos = avio_tell(pb); 277 int decoderSpecificInfoLen = track->vosLen ? 5+track->vosLen : 0; 278 279 avio_wb32(pb, 0); // size 280 ffio_wfourcc(pb, "esds"); 281 avio_wb32(pb, 0); // Version 282 283 // ES descriptor 284 putDescr(pb, 0x03, 3 + 5+13 + decoderSpecificInfoLen + 5+1); 285 avio_wb16(pb, track->trackID); 286 avio_w8(pb, 0x00); // flags (= no flags) 287 288 // DecoderConfig descriptor 289 putDescr(pb, 0x04, 13 + decoderSpecificInfoLen); 290 291 // Object type indication 292 if ((track->enc->codec_id == CODEC_ID_MP2 || 293 track->enc->codec_id == CODEC_ID_MP3) && 294 track->enc->sample_rate > 24000) 295 avio_w8(pb, 0x6B); // 11172-3 296 else 297 avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id)); 298 299 // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio) 300 // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved) 301 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO) 302 avio_w8(pb, 0x15); // flags (= Audiostream) 303 else 304 avio_w8(pb, 0x11); // flags (= Visualstream) 305 306 avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits) 307 avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB 308 309 avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window) 310 if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0) 311 avio_wb32(pb, 0); // vbr 312 else 313 avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate 314 315 if (track->vosLen) { 316 // DecoderSpecific info descriptor 317 putDescr(pb, 0x05, track->vosLen); 318 avio_write(pb, track->vosData, track->vosLen); 319 } 320 321 // SL descriptor 322 putDescr(pb, 0x06, 1); 323 avio_w8(pb, 0x02); 324 return updateSize(pb, pos); 325} 326 327static int mov_pcm_le_gt16(enum CodecID codec_id) 328{ 329 return codec_id == CODEC_ID_PCM_S24LE || 330 codec_id == CODEC_ID_PCM_S32LE || 331 codec_id == CODEC_ID_PCM_F32LE || 332 codec_id == CODEC_ID_PCM_F64LE; 333} 334 335static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track) 336{ 337 int64_t pos = avio_tell(pb); 338 avio_wb32(pb, 0); 339 avio_wl32(pb, track->tag); // store it byteswapped 340 track->enc->codec_tag = av_bswap16(track->tag >> 16); 341 ff_put_wav_header(pb, track->enc); 342 return updateSize(pb, pos); 343} 344 345static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track) 346{ 347 uint32_t layout_tag, bitmap; 348 int64_t pos = avio_tell(pb); 349 350 layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id, 351 track->enc->channel_layout, 352 &bitmap); 353 if (!layout_tag) { 354 av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to " 355 "lack of channel information\n"); 356 return 0; 357 } 358 359 avio_wb32(pb, 0); // Size 360 ffio_wfourcc(pb, "chan"); // Type 361 avio_w8(pb, 0); // Version 362 avio_wb24(pb, 0); // Flags 363 avio_wb32(pb, layout_tag); // mChannelLayoutTag 364 avio_wb32(pb, bitmap); // mChannelBitmap 365 avio_wb32(pb, 0); // mNumberChannelDescriptions 366 367 return updateSize(pb, pos); 368} 369 370static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track) 371{ 372 int64_t pos = avio_tell(pb); 373 374 avio_wb32(pb, 0); /* size */ 375 ffio_wfourcc(pb, "wave"); 376 377 avio_wb32(pb, 12); /* size */ 378 ffio_wfourcc(pb, "frma"); 379 avio_wl32(pb, track->tag); 380 381 if (track->enc->codec_id == CODEC_ID_AAC) { 382 /* useless atom needed by mplayer, ipod, not needed by quicktime */ 383 avio_wb32(pb, 12); /* size */ 384 ffio_wfourcc(pb, "mp4a"); 385 avio_wb32(pb, 0); 386 mov_write_esds_tag(pb, track); 387 } else if (mov_pcm_le_gt16(track->enc->codec_id)) { 388 mov_write_enda_tag(pb); 389 } else if (track->enc->codec_id == CODEC_ID_AMR_NB) { 390 mov_write_amr_tag(pb, track); 391 } else if (track->enc->codec_id == CODEC_ID_AC3) { 392 mov_write_chan_tag(pb, track); 393 mov_write_ac3_tag(pb, track); 394 } else if (track->enc->codec_id == CODEC_ID_ALAC) { 395 mov_write_extradata_tag(pb, track); 396 } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS || 397 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) { 398 mov_write_ms_tag(pb, track); 399 } 400 401 avio_wb32(pb, 8); /* size */ 402 avio_wb32(pb, 0); /* null tag */ 403 404 return updateSize(pb, pos); 405} 406 407static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track) 408{ 409 avio_wb32(pb, track->vosLen+8); 410 ffio_wfourcc(pb, "glbl"); 411 avio_write(pb, track->vosData, track->vosLen); 412 return 8+track->vosLen; 413} 414 415/** 416 * Compute flags for 'lpcm' tag. 417 * See CoreAudioTypes and AudioStreamBasicDescription at Apple. 418 */ 419static int mov_get_lpcm_flags(enum CodecID codec_id) 420{ 421 switch (codec_id) { 422 case CODEC_ID_PCM_F32BE: 423 case CODEC_ID_PCM_F64BE: 424 return 11; 425 case CODEC_ID_PCM_F32LE: 426 case CODEC_ID_PCM_F64LE: 427 return 9; 428 case CODEC_ID_PCM_U8: 429 return 10; 430 case CODEC_ID_PCM_S16BE: 431 case CODEC_ID_PCM_S24BE: 432 case CODEC_ID_PCM_S32BE: 433 return 14; 434 case CODEC_ID_PCM_S8: 435 case CODEC_ID_PCM_S16LE: 436 case CODEC_ID_PCM_S24LE: 437 case CODEC_ID_PCM_S32LE: 438 return 12; 439 default: 440 return 0; 441 } 442} 443 444static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track) 445{ 446 int64_t pos = avio_tell(pb); 447 int version = 0; 448 uint32_t tag = track->tag; 449 450 if (track->mode == MODE_MOV) { 451 if (mov_get_lpcm_flags(track->enc->codec_id)) 452 tag = AV_RL32("lpcm"); 453 version = 2; 454 } 455 456 avio_wb32(pb, 0); /* size */ 457 avio_wl32(pb, tag); // store it byteswapped 458 avio_wb32(pb, 0); /* Reserved */ 459 avio_wb16(pb, 0); /* Reserved */ 460 avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */ 461 462 /* SoundDescription */ 463 avio_wb16(pb, version); /* Version */ 464 avio_wb16(pb, 0); /* Revision level */ 465 avio_wb32(pb, 0); /* Reserved */ 466 467 if (version == 2) { 468 avio_wb16(pb, 3); 469 avio_wb16(pb, 16); 470 avio_wb16(pb, 0xfffe); 471 avio_wb16(pb, 0); 472 avio_wb32(pb, 0x00010000); 473 avio_wb32(pb, 72); 474 avio_wb64(pb, av_double2int(track->timescale)); 475 avio_wb32(pb, track->enc->channels); 476 avio_wb32(pb, 0x7F000000); 477 avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id)); 478 avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id)); 479 avio_wb32(pb, track->sampleSize); 480 avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1); 481 } else { 482 /* reserved for mp4/3gp */ 483 avio_wb16(pb, 2); 484 avio_wb16(pb, 16); 485 avio_wb16(pb, 0); 486 487 avio_wb16(pb, 0); /* packet size (= 0) */ 488 avio_wb16(pb, track->timescale); /* Time scale */ 489 avio_wb16(pb, 0); /* Reserved */ 490 } 491 492 if(track->mode == MODE_MOV && 493 (track->enc->codec_id == CODEC_ID_AAC || 494 track->enc->codec_id == CODEC_ID_AC3 || 495 track->enc->codec_id == CODEC_ID_AMR_NB || 496 track->enc->codec_id == CODEC_ID_ALAC || 497 track->enc->codec_id == CODEC_ID_ADPCM_MS || 498 track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV || 499 mov_pcm_le_gt16(track->enc->codec_id))) 500 mov_write_wave_tag(pb, track); 501 else if(track->tag == MKTAG('m','p','4','a')) 502 mov_write_esds_tag(pb, track); 503 else if(track->enc->codec_id == CODEC_ID_AMR_NB) 504 mov_write_amr_tag(pb, track); 505 else if(track->enc->codec_id == CODEC_ID_AC3) 506 mov_write_ac3_tag(pb, track); 507 else if(track->enc->codec_id == CODEC_ID_ALAC) 508 mov_write_extradata_tag(pb, track); 509 else if(track->vosLen > 0) 510 mov_write_glbl_tag(pb, track); 511 512 return updateSize(pb, pos); 513} 514 515static int mov_write_d263_tag(AVIOContext *pb) 516{ 517 avio_wb32(pb, 0xf); /* size */ 518 ffio_wfourcc(pb, "d263"); 519 ffio_wfourcc(pb, "FFMP"); 520 avio_w8(pb, 0); /* decoder version */ 521 /* FIXME use AVCodecContext level/profile, when encoder will set values */ 522 avio_w8(pb, 0xa); /* level */ 523 avio_w8(pb, 0); /* profile */ 524 return 0xf; 525} 526 527/* TODO: No idea about these values */ 528static int mov_write_svq3_tag(AVIOContext *pb) 529{ 530 avio_wb32(pb, 0x15); 531 ffio_wfourcc(pb, "SMI "); 532 ffio_wfourcc(pb, "SEQH"); 533 avio_wb32(pb, 0x5); 534 avio_wb32(pb, 0xe2c0211d); 535 avio_wb32(pb, 0xc0000000); 536 avio_w8(pb, 0); 537 return 0x15; 538} 539 540static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track) 541{ 542 int64_t pos = avio_tell(pb); 543 544 avio_wb32(pb, 0); 545 ffio_wfourcc(pb, "avcC"); 546 ff_isom_write_avcc(pb, track->vosData, track->vosLen); 547 return updateSize(pb, pos); 548} 549 550/* also used by all avid codecs (dv, imx, meridien) and their variants */ 551static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track) 552{ 553 int i; 554 avio_wb32(pb, 24); /* size */ 555 ffio_wfourcc(pb, "ACLR"); 556 ffio_wfourcc(pb, "ACLR"); 557 ffio_wfourcc(pb, "0001"); 558 avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */ 559 avio_wb32(pb, 0); /* unknown */ 560 561 avio_wb32(pb, 24); /* size */ 562 ffio_wfourcc(pb, "APRG"); 563 ffio_wfourcc(pb, "APRG"); 564 ffio_wfourcc(pb, "0001"); 565 avio_wb32(pb, 1); /* unknown */ 566 avio_wb32(pb, 0); /* unknown */ 567 568 avio_wb32(pb, 120); /* size */ 569 ffio_wfourcc(pb, "ARES"); 570 ffio_wfourcc(pb, "ARES"); 571 ffio_wfourcc(pb, "0001"); 572 avio_wb32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */ 573 avio_wb32(pb, track->enc->width); 574 /* values below are based on samples created with quicktime and avid codecs */ 575 if (track->vosData[5] & 2) { // interlaced 576 avio_wb32(pb, track->enc->height/2); 577 avio_wb32(pb, 2); /* unknown */ 578 avio_wb32(pb, 0); /* unknown */ 579 avio_wb32(pb, 4); /* unknown */ 580 } else { 581 avio_wb32(pb, track->enc->height); 582 avio_wb32(pb, 1); /* unknown */ 583 avio_wb32(pb, 0); /* unknown */ 584 if (track->enc->height == 1080) 585 avio_wb32(pb, 5); /* unknown */ 586 else 587 avio_wb32(pb, 6); /* unknown */ 588 } 589 /* padding */ 590 for (i = 0; i < 10; i++) 591 avio_wb64(pb, 0); 592 593 /* extra padding for stsd needed */ 594 avio_wb32(pb, 0); 595 return 0; 596} 597 598static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track) 599{ 600 int tag = track->enc->codec_tag; 601 602 if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id)) 603 return 0; 604 605 if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1'); 606 else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3'); 607 else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c'); 608 else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g'); 609 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v'); 610 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a'); 611 612 return tag; 613} 614 615static const AVCodecTag codec_ipod_tags[] = { 616 { CODEC_ID_H264, MKTAG('a','v','c','1') }, 617 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') }, 618 { CODEC_ID_AAC, MKTAG('m','p','4','a') }, 619 { CODEC_ID_ALAC, MKTAG('a','l','a','c') }, 620 { CODEC_ID_AC3, MKTAG('a','c','-','3') }, 621 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') }, 622 { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') }, 623 { CODEC_ID_NONE, 0 }, 624}; 625 626static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track) 627{ 628 int tag = track->enc->codec_tag; 629 630 // keep original tag for subs, ipod supports both formats 631 if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE && 632 (tag == MKTAG('t','x','3','g') || 633 tag == MKTAG('t','e','x','t')))) 634 tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id); 635 636 if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v")) 637 av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v " 638 "Quicktime/Ipod might not play the file\n"); 639 640 return tag; 641} 642 643static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track) 644{ 645 int tag; 646 647 if (track->enc->width == 720) /* SD */ 648 if (track->enc->height == 480) /* NTSC */ 649 if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n'); 650 else tag = MKTAG('d','v','c',' '); 651 else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p'); 652 else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p'); 653 else tag = MKTAG('d','v','p','p'); 654 else if (track->enc->height == 720) /* HD 720 line */ 655 if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q'); 656 else tag = MKTAG('d','v','h','p'); 657 else if (track->enc->height == 1080) /* HD 1080 line */ 658 if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5'); 659 else tag = MKTAG('d','v','h','6'); 660 else { 661 av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n"); 662 return 0; 663 } 664 665 return tag; 666} 667 668static const struct { 669 enum PixelFormat pix_fmt; 670 uint32_t tag; 671 unsigned bps; 672} mov_pix_fmt_tags[] = { 673 { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 }, 674 { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 }, 675 { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 }, 676 { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 }, 677 { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 }, 678 { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 }, 679 { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 }, 680 { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 }, 681 { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 }, 682 { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 }, 683 { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 }, 684 { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 }, 685 { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 }, 686 { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 }, 687}; 688 689static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track) 690{ 691 int tag = track->enc->codec_tag; 692 int i; 693 694 for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) { 695 if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) { 696 tag = mov_pix_fmt_tags[i].tag; 697 track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps; 698 break; 699 } 700 } 701 702 return tag; 703} 704 705static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track) 706{ 707 int tag = track->enc->codec_tag; 708 709 if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL && 710 (track->enc->codec_id == CODEC_ID_DVVIDEO || 711 track->enc->codec_id == CODEC_ID_RAWVIDEO || 712 track->enc->codec_id == CODEC_ID_H263 || 713 av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio 714 if (track->enc->codec_id == CODEC_ID_DVVIDEO) 715 tag = mov_get_dv_codec_tag(s, track); 716 else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) 717 tag = mov_get_rawvideo_codec_tag(s, track); 718 else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) { 719 tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id); 720 if (!tag) { // if no mac fcc found, try with Microsoft tags 721 tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id); 722 if (tag) 723 av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, " 724 "the file may be unplayable!\n"); 725 } 726 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) { 727 tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id); 728 if (!tag) { // if no mac fcc found, try with Microsoft tags 729 int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id); 730 if (ms_tag) { 731 tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff)); 732 av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, " 733 "the file may be unplayable!\n"); 734 } 735 } 736 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) 737 tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id); 738 } 739 740 return tag; 741} 742 743static const AVCodecTag codec_3gp_tags[] = { 744 { CODEC_ID_H263, MKTAG('s','2','6','3') }, 745 { CODEC_ID_H264, MKTAG('a','v','c','1') }, 746 { CODEC_ID_MPEG4, MKTAG('m','p','4','v') }, 747 { CODEC_ID_AAC, MKTAG('m','p','4','a') }, 748 { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') }, 749 { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') }, 750 { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') }, 751 { CODEC_ID_NONE, 0 }, 752}; 753 754static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track) 755{ 756 int tag = track->enc->codec_tag; 757 758 if (track->mode == MODE_MP4 || track->mode == MODE_PSP) 759 tag = mp4_get_codec_tag(s, track); 760 else if (track->mode == MODE_IPOD) 761 tag = ipod_get_codec_tag(s, track); 762 else if (track->mode & MODE_3GP) 763 tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id); 764 else 765 tag = mov_get_codec_tag(s, track); 766 767 return tag; 768} 769 770/** Write uuid atom. 771 * Needed to make file play in iPods running newest firmware 772 * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1 773 */ 774static int mov_write_uuid_tag_ipod(AVIOContext *pb) 775{ 776 avio_wb32(pb, 28); 777 ffio_wfourcc(pb, "uuid"); 778 avio_wb32(pb, 0x6b6840f2); 779 avio_wb32(pb, 0x5f244fc5); 780 avio_wb32(pb, 0xba39a51b); 781 avio_wb32(pb, 0xcf0323f3); 782 avio_wb32(pb, 0x0); 783 return 28; 784} 785 786static const uint16_t fiel_data[] = { 787 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e 788}; 789 790static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track) 791{ 792 unsigned mov_field_order = 0; 793 if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data)) 794 mov_field_order = fiel_data[track->enc->field_order]; 795 else 796 return 0; 797 avio_wb32(pb, 10); 798 ffio_wfourcc(pb, "fiel"); 799 avio_wb16(pb, mov_field_order); 800 return 10; 801} 802 803static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track) 804{ 805 int64_t pos = avio_tell(pb); 806 avio_wb32(pb, 0); /* size */ 807 avio_wl32(pb, track->tag); // store it byteswapped 808 avio_wb32(pb, 0); /* Reserved */ 809 avio_wb16(pb, 0); /* Reserved */ 810 avio_wb16(pb, 1); /* Data-reference index */ 811 812 if (track->enc->extradata_size) 813 avio_write(pb, track->enc->extradata, track->enc->extradata_size); 814 815 return updateSize(pb, pos); 816} 817 818static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track) 819{ 820 AVRational sar; 821 av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num, 822 track->enc->sample_aspect_ratio.den, INT_MAX); 823 824 avio_wb32(pb, 16); 825 ffio_wfourcc(pb, "pasp"); 826 avio_wb32(pb, sar.num); 827 avio_wb32(pb, sar.den); 828 return 16; 829} 830 831static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track) 832{ 833 int64_t pos = avio_tell(pb); 834 char compressor_name[32]; 835 836 avio_wb32(pb, 0); /* size */ 837 avio_wl32(pb, track->tag); // store it byteswapped 838 avio_wb32(pb, 0); /* Reserved */ 839 avio_wb16(pb, 0); /* Reserved */ 840 avio_wb16(pb, 1); /* Data-reference index */ 841 842 avio_wb16(pb, 0); /* Codec stream version */ 843 avio_wb16(pb, 0); /* Codec stream revision (=0) */ 844 if (track->mode == MODE_MOV) { 845 ffio_wfourcc(pb, "FFMP"); /* Vendor */ 846 if(track->enc->codec_id == CODEC_ID_RAWVIDEO) { 847 avio_wb32(pb, 0); /* Temporal Quality */ 848 avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/ 849 } else { 850 avio_wb32(pb, 0x200); /* Temporal Quality = normal */ 851 avio_wb32(pb, 0x200); /* Spatial Quality = normal */ 852 } 853 } else { 854 avio_wb32(pb, 0); /* Reserved */ 855 avio_wb32(pb, 0); /* Reserved */ 856 avio_wb32(pb, 0); /* Reserved */ 857 } 858 avio_wb16(pb, track->enc->width); /* Video width */ 859 avio_wb16(pb, track->height); /* Video height */ 860 avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */ 861 avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */ 862 avio_wb32(pb, 0); /* Data size (= 0) */ 863 avio_wb16(pb, 1); /* Frame count (= 1) */ 864 865 memset(compressor_name,0,32); 866 /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */ 867 if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name) 868 av_strlcpy(compressor_name,track->enc->codec->name,32); 869 avio_w8(pb, strlen(compressor_name)); 870 avio_write(pb, compressor_name, 31); 871 872 if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample) 873 avio_wb16(pb, track->enc->bits_per_coded_sample); 874 else 875 avio_wb16(pb, 0x18); /* Reserved */ 876 avio_wb16(pb, 0xffff); /* Reserved */ 877 if(track->tag == MKTAG('m','p','4','v')) 878 mov_write_esds_tag(pb, track); 879 else if(track->enc->codec_id == CODEC_ID_H263) 880 mov_write_d263_tag(pb); 881 else if(track->enc->codec_id == CODEC_ID_SVQ3) 882 mov_write_svq3_tag(pb); 883 else if(track->enc->codec_id == CODEC_ID_DNXHD) 884 mov_write_avid_tag(pb, track); 885 else if(track->enc->codec_id == CODEC_ID_H264) { 886 mov_write_avcc_tag(pb, track); 887 if(track->mode == MODE_IPOD) 888 mov_write_uuid_tag_ipod(pb); 889 } else if (track->enc->field_order != AV_FIELD_UNKNOWN) 890 mov_write_fiel_tag(pb, track); 891 else if(track->vosLen > 0) 892 mov_write_glbl_tag(pb, track); 893 894 if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num && 895 track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) { 896 mov_write_pasp_tag(pb, track); 897 } 898 899 return updateSize(pb, pos); 900} 901 902static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track) 903{ 904 int64_t pos = avio_tell(pb); 905 avio_wb32(pb, 0); /* size */ 906 ffio_wfourcc(pb, "rtp "); 907 avio_wb32(pb, 0); /* Reserved */ 908 avio_wb16(pb, 0); /* Reserved */ 909 avio_wb16(pb, 1); /* Data-reference index */ 910 911 avio_wb16(pb, 1); /* Hint track version */ 912 avio_wb16(pb, 1); /* Highest compatible version */ 913 avio_wb32(pb, track->max_packet_size); /* Max packet size */ 914 915 avio_wb32(pb, 12); /* size */ 916 ffio_wfourcc(pb, "tims"); 917 avio_wb32(pb, track->timescale); 918 919 return updateSize(pb, pos); 920} 921 922static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track) 923{ 924 int64_t pos = avio_tell(pb); 925 avio_wb32(pb, 0); /* size */ 926 ffio_wfourcc(pb, "stsd"); 927 avio_wb32(pb, 0); /* version & flags */ 928 avio_wb32(pb, 1); /* entry count */ 929 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) 930 mov_write_video_tag(pb, track); 931 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) 932 mov_write_audio_tag(pb, track); 933 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) 934 mov_write_subtitle_tag(pb, track); 935 else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) 936 mov_write_rtp_tag(pb, track); 937 return updateSize(pb, pos); 938} 939 940static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track) 941{ 942 MOVStts *ctts_entries; 943 uint32_t entries = 0; 944 uint32_t atom_size; 945 int i; 946 947 ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */ 948 ctts_entries[0].count = 1; 949 ctts_entries[0].duration = track->cluster[0].cts; 950 for (i=1; i<track->entry; i++) { 951 if (track->cluster[i].cts == ctts_entries[entries].duration) { 952 ctts_entries[entries].count++; /* compress */ 953 } else { 954 entries++; 955 ctts_entries[entries].duration = track->cluster[i].cts; 956 ctts_entries[entries].count = 1; 957 } 958 } 959 entries++; /* last one */ 960 atom_size = 16 + (entries * 8); 961 avio_wb32(pb, atom_size); /* size */ 962 ffio_wfourcc(pb, "ctts"); 963 avio_wb32(pb, 0); /* version & flags */ 964 avio_wb32(pb, entries); /* entry count */ 965 for (i=0; i<entries; i++) { 966 avio_wb32(pb, ctts_entries[i].count); 967 avio_wb32(pb, ctts_entries[i].duration); 968 } 969 av_free(ctts_entries); 970 return atom_size; 971} 972 973/* Time to sample atom */ 974static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track) 975{ 976 MOVStts *stts_entries; 977 uint32_t entries = -1; 978 uint32_t atom_size; 979 int i; 980 981 if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) { 982 stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */ 983 stts_entries[0].count = track->sampleCount; 984 stts_entries[0].duration = 1; 985 entries = 1; 986 } else { 987 stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */ 988 for (i=0; i<track->entry; i++) { 989 int64_t duration = i + 1 == track->entry ? 990 track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */ 991 track->cluster[i+1].dts - track->cluster[i].dts; 992 if (i && duration == stts_entries[entries].duration) { 993 stts_entries[entries].count++; /* compress */ 994 } else { 995 entries++; 996 stts_entries[entries].duration = duration; 997 stts_entries[entries].count = 1; 998 } 999 } 1000 entries++; /* last one */ 1001 } 1002 atom_size = 16 + (entries * 8); 1003 avio_wb32(pb, atom_size); /* size */ 1004 ffio_wfourcc(pb, "stts"); 1005 avio_wb32(pb, 0); /* version & flags */ 1006 avio_wb32(pb, entries); /* entry count */ 1007 for (i=0; i<entries; i++) { 1008 avio_wb32(pb, stts_entries[i].count); 1009 avio_wb32(pb, stts_entries[i].duration); 1010 } 1011 av_free(stts_entries); 1012 return atom_size; 1013} 1014 1015static int mov_write_dref_tag(AVIOContext *pb) 1016{ 1017 avio_wb32(pb, 28); /* size */ 1018 ffio_wfourcc(pb, "dref"); 1019 avio_wb32(pb, 0); /* version & flags */ 1020 avio_wb32(pb, 1); /* entry count */ 1021 1022 avio_wb32(pb, 0xc); /* size */ 1023 ffio_wfourcc(pb, "url "); 1024 avio_wb32(pb, 1); /* version & flags */ 1025 1026 return 28; 1027} 1028 1029static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track) 1030{ 1031 int64_t pos = avio_tell(pb); 1032 avio_wb32(pb, 0); /* size */ 1033 ffio_wfourcc(pb, "stbl"); 1034 mov_write_stsd_tag(pb, track); 1035 mov_write_stts_tag(pb, track); 1036 if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO || 1037 track->enc->codec_tag == MKTAG('r','t','p',' ')) && 1038 track->hasKeyframes && track->hasKeyframes < track->entry) 1039 mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE); 1040 if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS) 1041 mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE); 1042 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && 1043 track->flags & MOV_TRACK_CTTS) 1044 mov_write_ctts_tag(pb, track); 1045 mov_write_stsc_tag(pb, track); 1046 mov_write_stsz_tag(pb, track); 1047 mov_write_stco_tag(pb, track); 1048 return updateSize(pb, pos); 1049} 1050 1051static int mov_write_dinf_tag(AVIOContext *pb) 1052{ 1053 int64_t pos = avio_tell(pb); 1054 avio_wb32(pb, 0); /* size */ 1055 ffio_wfourcc(pb, "dinf"); 1056 mov_write_dref_tag(pb); 1057 return updateSize(pb, pos); 1058} 1059 1060static int mov_write_nmhd_tag(AVIOContext *pb) 1061{ 1062 avio_wb32(pb, 12); 1063 ffio_wfourcc(pb, "nmhd"); 1064 avio_wb32(pb, 0); 1065 return 12; 1066} 1067 1068static int mov_write_gmhd_tag(AVIOContext *pb) 1069{ 1070 avio_wb32(pb, 0x20); /* size */ 1071 ffio_wfourcc(pb, "gmhd"); 1072 avio_wb32(pb, 0x18); /* gmin size */ 1073 ffio_wfourcc(pb, "gmin");/* generic media info */ 1074 avio_wb32(pb, 0); /* version & flags */ 1075 avio_wb16(pb, 0x40); /* graphics mode = */ 1076 avio_wb16(pb, 0x8000); /* opColor (r?) */ 1077 avio_wb16(pb, 0x8000); /* opColor (g?) */ 1078 avio_wb16(pb, 0x8000); /* opColor (b?) */ 1079 avio_wb16(pb, 0); /* balance */ 1080 avio_wb16(pb, 0); /* reserved */ 1081 return 0x20; 1082} 1083 1084static int mov_write_smhd_tag(AVIOContext *pb) 1085{ 1086 avio_wb32(pb, 16); /* size */ 1087 ffio_wfourcc(pb, "smhd"); 1088 avio_wb32(pb, 0); /* version & flags */ 1089 avio_wb16(pb, 0); /* reserved (balance, normally = 0) */ 1090 avio_wb16(pb, 0); /* reserved */ 1091 return 16; 1092} 1093 1094static int mov_write_vmhd_tag(AVIOContext *pb) 1095{ 1096 avio_wb32(pb, 0x14); /* size (always 0x14) */ 1097 ffio_wfourcc(pb, "vmhd"); 1098 avio_wb32(pb, 0x01); /* version & flags */ 1099 avio_wb64(pb, 0); /* reserved (graphics mode = copy) */ 1100 return 0x14; 1101} 1102 1103static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track) 1104{ 1105 const char *hdlr, *descr = NULL, *hdlr_type = NULL; 1106 int64_t pos = avio_tell(pb); 1107 1108 if (!track) { /* no media --> data handler */ 1109 hdlr = "dhlr"; 1110 hdlr_type = "url "; 1111 descr = "DataHandler"; 1112 } else { 1113 hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0"; 1114 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) { 1115 hdlr_type = "vide"; 1116 descr = "VideoHandler"; 1117 } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) { 1118 hdlr_type = "soun"; 1119 descr = "SoundHandler"; 1120 } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) { 1121 if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl"; 1122 else hdlr_type = "text"; 1123 descr = "SubtitleHandler"; 1124 } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) { 1125 hdlr_type = "hint"; 1126 descr = "HintHandler"; 1127 } 1128 } 1129 1130 avio_wb32(pb, 0); /* size */ 1131 ffio_wfourcc(pb, "hdlr"); 1132 avio_wb32(pb, 0); /* Version & flags */ 1133 avio_write(pb, hdlr, 4); /* handler */ 1134 ffio_wfourcc(pb, hdlr_type); /* handler type */ 1135 avio_wb32(pb ,0); /* reserved */ 1136 avio_wb32(pb ,0); /* reserved */ 1137 avio_wb32(pb ,0); /* reserved */ 1138 if (!track || track->mode == MODE_MOV) 1139 avio_w8(pb, strlen(descr)); /* pascal string */ 1140 avio_write(pb, descr, strlen(descr)); /* handler description */ 1141 if (track && track->mode != MODE_MOV) 1142 avio_w8(pb, 0); /* c string */ 1143 return updateSize(pb, pos); 1144} 1145 1146static int mov_write_hmhd_tag(AVIOContext *pb) 1147{ 1148 /* This atom must be present, but leaving the values at zero 1149 * seems harmless. */ 1150 avio_wb32(pb, 28); /* size */ 1151 ffio_wfourcc(pb, "hmhd"); 1152 avio_wb32(pb, 0); /* version, flags */ 1153 avio_wb16(pb, 0); /* maxPDUsize */ 1154 avio_wb16(pb, 0); /* avgPDUsize */ 1155 avio_wb32(pb, 0); /* maxbitrate */ 1156 avio_wb32(pb, 0); /* avgbitrate */ 1157 avio_wb32(pb, 0); /* reserved */ 1158 return 28; 1159} 1160 1161static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track) 1162{ 1163 int64_t pos = avio_tell(pb); 1164 avio_wb32(pb, 0); /* size */ 1165 ffio_wfourcc(pb, "minf"); 1166 if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO) 1167 mov_write_vmhd_tag(pb); 1168 else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) 1169 mov_write_smhd_tag(pb); 1170 else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) { 1171 if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb); 1172 else mov_write_nmhd_tag(pb); 1173 } else if (track->tag == MKTAG('r','t','p',' ')) { 1174 mov_write_hmhd_tag(pb); 1175 } 1176 if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */ 1177 mov_write_hdlr_tag(pb, NULL); 1178 mov_write_dinf_tag(pb); 1179 mov_write_stbl_tag(pb, track); 1180 return updateSize(pb, pos); 1181} 1182 1183static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track) 1184{ 1185 int version = track->trackDuration < INT32_MAX ? 0 : 1; 1186 1187 (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */ 1188 ffio_wfourcc(pb, "mdhd"); 1189 avio_w8(pb, version); 1190 avio_wb24(pb, 0); /* flags */ 1191 if (version == 1) { 1192 avio_wb64(pb, track->time); 1193 avio_wb64(pb, track->time); 1194 } else { 1195 avio_wb32(pb, track->time); /* creation time */ 1196 avio_wb32(pb, track->time); /* modification time */ 1197 } 1198 avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */ 1199 (version == 1) ? avio_wb64(pb, track->trackDuration) : avio_wb32(pb, track->trackDuration); /* duration */ 1200 avio_wb16(pb, track->language); /* language */ 1201 avio_wb16(pb, 0); /* reserved (quality) */ 1202 1203 if(version!=0 && track->mode == MODE_MOV){ 1204 av_log(NULL, AV_LOG_ERROR, 1205 "FATAL error, file duration too long for timebase, this file will not be\n" 1206 "playable with quicktime. Choose a different timebase or a different\n" 1207 "container format\n"); 1208 } 1209 1210 return 32; 1211} 1212 1213static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track) 1214{ 1215 int64_t pos = avio_tell(pb); 1216 avio_wb32(pb, 0); /* size */ 1217 ffio_wfourcc(pb, "mdia"); 1218 mov_write_mdhd_tag(pb, track); 1219 mov_write_hdlr_tag(pb, track); 1220 mov_write_minf_tag(pb, track); 1221 return updateSize(pb, pos); 1222} 1223 1224static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st) 1225{ 1226 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE, 1227 track->timescale, AV_ROUND_UP); 1228 int version = duration < INT32_MAX ? 0 : 1; 1229 1230 (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */ 1231 ffio_wfourcc(pb, "tkhd"); 1232 avio_w8(pb, version); 1233 avio_wb24(pb, 0xf); /* flags (track enabled) */ 1234 if (version == 1) { 1235 avio_wb64(pb, track->time); 1236 avio_wb64(pb, track->time); 1237 } else { 1238 avio_wb32(pb, track->time); /* creation time */ 1239 avio_wb32(pb, track->time); /* modification time */ 1240 } 1241 avio_wb32(pb, track->trackID); /* track-id */ 1242 avio_wb32(pb, 0); /* reserved */ 1243 (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration); 1244 1245 avio_wb32(pb, 0); /* reserved */ 1246 avio_wb32(pb, 0); /* reserved */ 1247 avio_wb16(pb, 0); /* layer */ 1248 avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */ 1249 /* Volume, only for audio */ 1250 if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO) 1251 avio_wb16(pb, 0x0100); 1252 else 1253 avio_wb16(pb, 0); 1254 avio_wb16(pb, 0); /* reserved */ 1255 1256 /* Matrix structure */ 1257 avio_wb32(pb, 0x00010000); /* reserved */ 1258 avio_wb32(pb, 0x0); /* reserved */ 1259 avio_wb32(pb, 0x0); /* reserved */ 1260 avio_wb32(pb, 0x0); /* reserved */ 1261 avio_wb32(pb, 0x00010000); /* reserved */ 1262 avio_wb32(pb, 0x0); /* reserved */ 1263 avio_wb32(pb, 0x0); /* reserved */ 1264 avio_wb32(pb, 0x0); /* reserved */ 1265 avio_wb32(pb, 0x40000000); /* reserved */ 1266 1267 /* Track width and height, for visual only */ 1268 if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO || 1269 track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) { 1270 if(track->mode == MODE_MOV) { 1271 avio_wb32(pb, track->enc->width << 16); 1272 avio_wb32(pb, track->height << 16); 1273 } else { 1274 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio); 1275 if(!sample_aspect_ratio || track->height != track->enc->height) 1276 sample_aspect_ratio = 1; 1277 avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000); 1278 avio_wb32(pb, track->height*0x10000); 1279 } 1280 } 1281 else { 1282 avio_wb32(pb, 0); 1283 avio_wb32(pb, 0); 1284 } 1285 return 0x5c; 1286} 1287 1288static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track) 1289{ 1290 int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width, 1291 track->enc->sample_aspect_ratio.den); 1292 1293 int64_t pos = avio_tell(pb); 1294 1295 avio_wb32(pb, 0); /* size */ 1296 ffio_wfourcc(pb, "tapt"); 1297 1298 avio_wb32(pb, 20); 1299 ffio_wfourcc(pb, "clef"); 1300 avio_wb32(pb, 0); 1301 avio_wb32(pb, width << 16); 1302 avio_wb32(pb, track->enc->height << 16); 1303 1304 avio_wb32(pb, 20); 1305 ffio_wfourcc(pb, "enof"); 1306 avio_wb32(pb, 0); 1307 avio_wb32(pb, track->enc->width << 16); 1308 avio_wb32(pb, track->enc->height << 16); 1309 1310 return updateSize(pb, pos); 1311} 1312 1313// This box seems important for the psp playback ... without it the movie seems to hang 1314static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track) 1315{ 1316 int64_t duration = av_rescale_rnd(track->trackDuration, MOV_TIMESCALE, 1317 track->timescale, AV_ROUND_UP); 1318 int version = duration < INT32_MAX ? 0 : 1; 1319 int entry_size, entry_count, size; 1320 int64_t delay, start_ct = track->cluster[0].cts; 1321 delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE, 1322 track->timescale, AV_ROUND_DOWN); 1323 version |= delay < INT32_MAX ? 0 : 1; 1324 1325 entry_size = (version == 1) ? 20 : 12; 1326 entry_count = 1 + (delay > 0); 1327 size = 24 + entry_count * entry_size; 1328 1329 /* write the atom data */ 1330 avio_wb32(pb, size); 1331 ffio_wfourcc(pb, "edts"); 1332 avio_wb32(pb, size - 8); 1333 ffio_wfourcc(pb, "elst"); 1334 avio_w8(pb, version); 1335 avio_wb24(pb, 0); /* flags */ 1336 1337 avio_wb32(pb, entry_count); 1338 if (delay > 0) { /* add an empty edit to delay presentation */ 1339 if (version == 1) { 1340 avio_wb64(pb, delay); 1341 avio_wb64(pb, -1); 1342 } else { 1343 avio_wb32(pb, delay); 1344 avio_wb32(pb, -1); 1345 } 1346 avio_wb32(pb, 0x00010000); 1347 } 1348 1349 /* duration */ 1350 if (version == 1) { 1351 avio_wb64(pb, duration); 1352 avio_wb64(pb, start_ct); 1353 } else { 1354 avio_wb32(pb, duration); 1355 avio_wb32(pb, start_ct); 1356 } 1357 avio_wb32(pb, 0x00010000); 1358 return size; 1359} 1360 1361static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track) 1362{ 1363 avio_wb32(pb, 20); // size 1364 ffio_wfourcc(pb, "tref"); 1365 avio_wb32(pb, 12); // size (subatom) 1366 avio_wl32(pb, track->tref_tag); 1367 avio_wb32(pb, track->tref_id); 1368 return 20; 1369} 1370 1371// goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it) 1372static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov) 1373{ 1374 avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */ 1375 ffio_wfourcc(pb, "uuid"); 1376 ffio_wfourcc(pb, "USMT"); 1377 avio_wb32(pb, 0x21d24fce); 1378 avio_wb32(pb, 0xbb88695c); 1379 avio_wb32(pb, 0xfac9c740); 1380 avio_wb32(pb, 0x1c); // another size here! 1381 ffio_wfourcc(pb, "MTDT"); 1382 avio_wb32(pb, 0x00010012); 1383 avio_wb32(pb, 0x0a); 1384 avio_wb32(pb, 0x55c40000); 1385 avio_wb32(pb, 0x1); 1386 avio_wb32(pb, 0x0); 1387 return 0x34; 1388} 1389 1390static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index) 1391{ 1392 char buf[1000] = ""; 1393 int len; 1394 1395 ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx); 1396 av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index); 1397 len = strlen(buf); 1398 1399 avio_wb32(pb, len + 24); 1400 ffio_wfourcc(pb, "udta"); 1401 avio_wb32(pb, len + 16); 1402 ffio_wfourcc(pb, "hnti"); 1403 avio_wb32(pb, len + 8); 1404 ffio_wfourcc(pb, "sdp "); 1405 avio_write(pb, buf, len); 1406 return len + 24; 1407} 1408 1409static int mov_write_trak_tag(AVIOContext *pb, MOVTrack *track, AVStream *st) 1410{ 1411 int64_t pos = avio_tell(pb); 1412 avio_wb32(pb, 0); /* size */ 1413 ffio_wfourcc(pb, "trak"); 1414 mov_write_tkhd_tag(pb, track, st); 1415 if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS || track->cluster[0].dts) 1416 mov_write_edts_tag(pb, track); // PSP Movies require edts box 1417 if (track->tref_tag) 1418 mov_write_tref_tag(pb, track); 1419 mov_write_mdia_tag(pb, track); 1420 if (track->mode == MODE_PSP) 1421 mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box 1422 if (track->tag == MKTAG('r','t','p',' ')) 1423 mov_write_udta_sdp(pb, track->rtp_ctx, track->trackID); 1424 if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) { 1425 double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio); 1426 if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio) 1427 mov_write_tapt_tag(pb, track); 1428 }; 1429 return updateSize(pb, pos); 1430} 1431 1432static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov) 1433{ 1434 int i, has_audio = 0, has_video = 0; 1435 int64_t pos = avio_tell(pb); 1436 int audio_profile = mov->iods_audio_profile; 1437 int video_profile = mov->iods_video_profile; 1438 for (i = 0; i < mov->nb_streams; i++) { 1439 if(mov->tracks[i].entry > 0) { 1440 has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO; 1441 has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO; 1442 } 1443 } 1444 if (audio_profile < 0) 1445 audio_profile = 0xFF - has_audio; 1446 if (video_profile < 0) 1447 video_profile = 0xFF - has_video; 1448 avio_wb32(pb, 0x0); /* size */ 1449 ffio_wfourcc(pb, "iods"); 1450 avio_wb32(pb, 0); /* version & flags */ 1451 putDescr(pb, 0x10, 7); 1452 avio_wb16(pb, 0x004f); 1453 avio_w8(pb, 0xff); 1454 avio_w8(pb, 0xff); 1455 avio_w8(pb, audio_profile); 1456 avio_w8(pb, video_profile); 1457 avio_w8(pb, 0xff); 1458 return updateSize(pb, pos); 1459} 1460 1461static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov) 1462{ 1463 int maxTrackID = 1, i; 1464 int64_t maxTrackLenTemp, maxTrackLen = 0; 1465 int version; 1466 1467 for (i=0; i<mov->nb_streams; i++) { 1468 if(mov->tracks[i].entry > 0) { 1469 maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, 1470 MOV_TIMESCALE, 1471 mov->tracks[i].timescale, 1472 AV_ROUND_UP); 1473 if(maxTrackLen < maxTrackLenTemp) 1474 maxTrackLen = maxTrackLenTemp; 1475 if(maxTrackID < mov->tracks[i].trackID) 1476 maxTrackID = mov->tracks[i].trackID; 1477 } 1478 } 1479 1480 version = maxTrackLen < UINT32_MAX ? 0 : 1; 1481 (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */ 1482 ffio_wfourcc(pb, "mvhd"); 1483 avio_w8(pb, version); 1484 avio_wb24(pb, 0); /* flags */ 1485 if (version == 1) { 1486 avio_wb64(pb, mov->time); 1487 avio_wb64(pb, mov->time); 1488 } else { 1489 avio_wb32(pb, mov->time); /* creation time */ 1490 avio_wb32(pb, mov->time); /* modification time */ 1491 } 1492 avio_wb32(pb, MOV_TIMESCALE); 1493 (version == 1) ? avio_wb64(pb, maxTrackLen) : avio_wb32(pb, maxTrackLen); /* duration of longest track */ 1494 1495 avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */ 1496 avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */ 1497 avio_wb16(pb, 0); /* reserved */ 1498 avio_wb32(pb, 0); /* reserved */ 1499 avio_wb32(pb, 0); /* reserved */ 1500 1501 /* Matrix structure */ 1502 avio_wb32(pb, 0x00010000); /* reserved */ 1503 avio_wb32(pb, 0x0); /* reserved */ 1504 avio_wb32(pb, 0x0); /* reserved */ 1505 avio_wb32(pb, 0x0); /* reserved */ 1506 avio_wb32(pb, 0x00010000); /* reserved */ 1507 avio_wb32(pb, 0x0); /* reserved */ 1508 avio_wb32(pb, 0x0); /* reserved */ 1509 avio_wb32(pb, 0x0); /* reserved */ 1510 avio_wb32(pb, 0x40000000); /* reserved */ 1511 1512 avio_wb32(pb, 0); /* reserved (preview time) */ 1513 avio_wb32(pb, 0); /* reserved (preview duration) */ 1514 avio_wb32(pb, 0); /* reserved (poster time) */ 1515 avio_wb32(pb, 0); /* reserved (selection time) */ 1516 avio_wb32(pb, 0); /* reserved (selection duration) */ 1517 avio_wb32(pb, 0); /* reserved (current time) */ 1518 avio_wb32(pb, maxTrackID+1); /* Next track id */ 1519 return 0x6c; 1520} 1521 1522static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov, 1523 AVFormatContext *s) 1524{ 1525 avio_wb32(pb, 33); /* size */ 1526 ffio_wfourcc(pb, "hdlr"); 1527 avio_wb32(pb, 0); 1528 avio_wb32(pb, 0); 1529 ffio_wfourcc(pb, "mdir"); 1530 ffio_wfourcc(pb, "appl"); 1531 avio_wb32(pb, 0); 1532 avio_wb32(pb, 0); 1533 avio_w8(pb, 0); 1534 return 33; 1535} 1536 1537/* helper function to write a data tag with the specified string as data */ 1538static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style) 1539{ 1540 if(long_style){ 1541 int size = 16 + strlen(data); 1542 avio_wb32(pb, size); /* size */ 1543 ffio_wfourcc(pb, "data"); 1544 avio_wb32(pb, 1); 1545 avio_wb32(pb, 0); 1546 avio_write(pb, data, strlen(data)); 1547 return size; 1548 }else{ 1549 if (!lang) 1550 lang = ff_mov_iso639_to_lang("und", 1); 1551 avio_wb16(pb, strlen(data)); /* string length */ 1552 avio_wb16(pb, lang); 1553 avio_write(pb, data, strlen(data)); 1554 return strlen(data) + 4; 1555 } 1556} 1557 1558static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){ 1559 int size = 0; 1560 if (value && value[0]) { 1561 int64_t pos = avio_tell(pb); 1562 avio_wb32(pb, 0); /* size */ 1563 ffio_wfourcc(pb, name); 1564 mov_write_string_data_tag(pb, value, lang, long_style); 1565 size= updateSize(pb, pos); 1566 } 1567 return size; 1568} 1569 1570static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb, 1571 const char *name, const char *tag, 1572 int long_style) 1573{ 1574 int l, lang = 0, len, len2; 1575 AVDictionaryEntry *t, *t2 = NULL; 1576 char tag2[16]; 1577 1578 if (!(t = av_dict_get(s->metadata, tag, NULL, 0))) 1579 return 0; 1580 1581 len = strlen(t->key); 1582 snprintf(tag2, sizeof(tag2), "%s-", tag); 1583 while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) { 1584 len2 = strlen(t2->key); 1585 if (len2 == len+4 && !strcmp(t->value, t2->value) 1586 && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) { 1587 lang = l; 1588 break; 1589 } 1590 } 1591 return mov_write_string_tag(pb, name, t->value, lang, long_style); 1592} 1593 1594/* iTunes track number */ 1595static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov, 1596 AVFormatContext *s) 1597{ 1598 AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0); 1599 int size = 0, track = t ? atoi(t->value) : 0; 1600 if (track) { 1601 avio_wb32(pb, 32); /* size */ 1602 ffio_wfourcc(pb, "trkn"); 1603 avio_wb32(pb, 24); /* size */ 1604 ffio_wfourcc(pb, "data"); 1605 avio_wb32(pb, 0); // 8 bytes empty 1606 avio_wb32(pb, 0); 1607 avio_wb16(pb, 0); // empty 1608 avio_wb16(pb, track); // track number 1609 avio_wb16(pb, 0); // total track number 1610 avio_wb16(pb, 0); // empty 1611 size = 32; 1612 } 1613 return size; 1614} 1615 1616/* iTunes meta data list */ 1617static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov, 1618 AVFormatContext *s) 1619{ 1620 int64_t pos = avio_tell(pb); 1621 avio_wb32(pb, 0); /* size */ 1622 ffio_wfourcc(pb, "ilst"); 1623 mov_write_string_metadata(s, pb, "\251nam", "title" , 1); 1624 mov_write_string_metadata(s, pb, "\251ART", "artist" , 1); 1625 mov_write_string_metadata(s, pb, "aART", "album_artist", 1); 1626 mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1); 1627 mov_write_string_metadata(s, pb, "\251alb", "album" , 1); 1628 mov_write_string_metadata(s, pb, "\251day", "date" , 1); 1629 mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1); 1630 mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1); 1631 mov_write_string_metadata(s, pb, "\251gen", "genre" , 1); 1632 mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1); 1633 mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1); 1634 mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1); 1635 mov_write_string_metadata(s, pb, "desc", "description",1); 1636 mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1); 1637 mov_write_string_metadata(s, pb, "tvsh", "show" , 1); 1638 mov_write_string_metadata(s, pb, "tven", "episode_id",1); 1639 mov_write_string_metadata(s, pb, "tvnn", "network" , 1); 1640 mov_write_trkn_tag(pb, mov, s); 1641 return updateSize(pb, pos); 1642} 1643 1644/* iTunes meta data tag */ 1645static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov, 1646 AVFormatContext *s) 1647{ 1648 int size = 0; 1649 int64_t pos = avio_tell(pb); 1650 avio_wb32(pb, 0); /* size */ 1651 ffio_wfourcc(pb, "meta"); 1652 avio_wb32(pb, 0); 1653 mov_write_itunes_hdlr_tag(pb, mov, s); 1654 mov_write_ilst_tag(pb, mov, s); 1655 size = updateSize(pb, pos); 1656 return size; 1657} 1658 1659static int utf8len(const uint8_t *b) 1660{ 1661 int len=0; 1662 int val; 1663 while(*b){ 1664 GET_UTF8(val, *b++, return -1;) 1665 len++; 1666 } 1667 return len; 1668} 1669 1670static int ascii_to_wc(AVIOContext *pb, const uint8_t *b) 1671{ 1672 int val; 1673 while(*b){ 1674 GET_UTF8(val, *b++, return -1;) 1675 avio_wb16(pb, val); 1676 } 1677 avio_wb16(pb, 0x00); 1678 return 0; 1679} 1680 1681static uint16_t language_code(const char *str) 1682{ 1683 return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F); 1684} 1685 1686static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s, 1687 const char *tag, const char *str) 1688{ 1689 int64_t pos = avio_tell(pb); 1690 AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0); 1691 if (!t || !utf8len(t->value)) 1692 return 0; 1693 avio_wb32(pb, 0); /* size */ 1694 ffio_wfourcc(pb, tag); /* type */ 1695 avio_wb32(pb, 0); /* version + flags */ 1696 if (!strcmp(tag, "yrrc")) 1697 avio_wb16(pb, atoi(t->value)); 1698 else { 1699 avio_wb16(pb, language_code("eng")); /* language */ 1700 avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */ 1701 if (!strcmp(tag, "albm") && 1702 (t = av_dict_get(s->metadata, "track", NULL, 0))) 1703 avio_w8(pb, atoi(t->value)); 1704 } 1705 return updateSize(pb, pos); 1706} 1707 1708static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s) 1709{ 1710 int64_t pos = avio_tell(pb); 1711 int i, nb_chapters = FFMIN(s->nb_chapters, 255); 1712 1713 avio_wb32(pb, 0); // size 1714 ffio_wfourcc(pb, "chpl"); 1715 avio_wb32(pb, 0x01000000); // version + flags 1716 avio_wb32(pb, 0); // unknown 1717 avio_w8(pb, nb_chapters); 1718 1719 for (i = 0; i < nb_chapters; i++) { 1720 AVChapter *c = s->chapters[i]; 1721 AVDictionaryEntry *t; 1722 avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000})); 1723 1724 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) { 1725 int len = FFMIN(strlen(t->value), 255); 1726 avio_w8(pb, len); 1727 avio_write(pb, t->value, len); 1728 } else 1729 avio_w8(pb, 0); 1730 } 1731 return updateSize(pb, pos); 1732} 1733 1734static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov, 1735 AVFormatContext *s) 1736{ 1737 AVIOContext *pb_buf; 1738 int i, ret, size; 1739 uint8_t *buf; 1740 1741 for (i = 0; i < s->nb_streams; i++) 1742 if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) { 1743 return 0; 1744 } 1745 1746 ret = avio_open_dyn_buf(&pb_buf); 1747 if(ret < 0) 1748 return ret; 1749 1750 if (mov->mode & MODE_3GP) { 1751 mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist"); 1752 mov_write_3gp_udta_tag(pb_buf, s, "titl", "title"); 1753 mov_write_3gp_udta_tag(pb_buf, s, "auth", "author"); 1754 mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre"); 1755 mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment"); 1756 mov_write_3gp_udta_tag(pb_buf, s, "albm", "album"); 1757 mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright"); 1758 mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date"); 1759 } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4 1760 mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0); 1761 mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0); 1762 mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0); 1763 mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0); 1764 mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0); 1765 mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0); 1766 mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0); 1767 mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0); 1768 mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0); 1769 } else { 1770 /* iTunes meta data */ 1771 mov_write_meta_tag(pb_buf, mov, s); 1772 } 1773 1774 if (s->nb_chapters) 1775 mov_write_chpl_tag(pb_buf, s); 1776 1777 if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) { 1778 avio_wb32(pb, size+8); 1779 ffio_wfourcc(pb, "udta"); 1780 avio_write(pb, buf, size); 1781 } 1782 av_free(buf); 1783 1784 return 0; 1785} 1786 1787static void mov_write_psp_udta_tag(AVIOContext *pb, 1788 const char *str, const char *lang, int type) 1789{ 1790 int len = utf8len(str)+1; 1791 if(len<=0) 1792 return; 1793 avio_wb16(pb, len*2+10); /* size */ 1794 avio_wb32(pb, type); /* type */ 1795 avio_wb16(pb, language_code(lang)); /* language */ 1796 avio_wb16(pb, 0x01); /* ? */ 1797 ascii_to_wc(pb, str); 1798} 1799 1800static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s) 1801{ 1802 AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0); 1803 int64_t pos, pos2; 1804 1805 if (title) { 1806 pos = avio_tell(pb); 1807 avio_wb32(pb, 0); /* size placeholder*/ 1808 ffio_wfourcc(pb, "uuid"); 1809 ffio_wfourcc(pb, "USMT"); 1810 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */ 1811 avio_wb32(pb, 0xbb88695c); 1812 avio_wb32(pb, 0xfac9c740); 1813 1814 pos2 = avio_tell(pb); 1815 avio_wb32(pb, 0); /* size placeholder*/ 1816 ffio_wfourcc(pb, "MTDT"); 1817 avio_wb16(pb, 4); 1818 1819 // ? 1820 avio_wb16(pb, 0x0C); /* size */ 1821 avio_wb32(pb, 0x0B); /* type */ 1822 avio_wb16(pb, language_code("und")); /* language */ 1823 avio_wb16(pb, 0x0); /* ? */ 1824 avio_wb16(pb, 0x021C); /* data */ 1825 1826 mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04); 1827 mov_write_psp_udta_tag(pb, title->value, "eng", 0x01); 1828// snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec); 1829 mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03); 1830 1831 updateSize(pb, pos2); 1832 return updateSize(pb, pos); 1833 } 1834 1835 return 0; 1836} 1837 1838static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov, 1839 AVFormatContext *s) 1840{ 1841 int i; 1842 int64_t pos = avio_tell(pb); 1843 avio_wb32(pb, 0); /* size placeholder*/ 1844 ffio_wfourcc(pb, "moov"); 1845 1846 for (i=0; i<mov->nb_streams; i++) { 1847 if(mov->tracks[i].entry <= 0) continue; 1848 1849 mov->tracks[i].time = mov->time; 1850 mov->tracks[i].trackID = i+1; 1851 } 1852 1853 if (mov->chapter_track) 1854 for (i=0; i<s->nb_streams; i++) { 1855 mov->tracks[i].tref_tag = MKTAG('c','h','a','p'); 1856 mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].trackID; 1857 } 1858 for (i = 0; i < mov->nb_streams; i++) { 1859 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) { 1860 mov->tracks[i].tref_tag = MKTAG('h','i','n','t'); 1861 mov->tracks[i].tref_id = 1862 mov->tracks[mov->tracks[i].src_track].trackID; 1863 } 1864 } 1865 1866 mov_write_mvhd_tag(pb, mov); 1867 if (mov->mode != MODE_MOV && !mov->iods_skip) 1868 mov_write_iods_tag(pb, mov); 1869 for (i=0; i<mov->nb_streams; i++) { 1870 if(mov->tracks[i].entry > 0) { 1871 mov_write_trak_tag(pb, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL); 1872 } 1873 } 1874 1875 if (mov->mode == MODE_PSP) 1876 mov_write_uuidusmt_tag(pb, s); 1877 else 1878 mov_write_udta_tag(pb, mov, s); 1879 1880 return updateSize(pb, pos); 1881} 1882 1883static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov) 1884{ 1885 avio_wb32(pb, 8); // placeholder for extended size field (64 bit) 1886 ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free"); 1887 1888 mov->mdat_pos = avio_tell(pb); 1889 avio_wb32(pb, 0); /* size placeholder*/ 1890 ffio_wfourcc(pb, "mdat"); 1891 return 0; 1892} 1893 1894/* TODO: This needs to be more general */ 1895static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s) 1896{ 1897 MOVMuxContext *mov = s->priv_data; 1898 int64_t pos = avio_tell(pb); 1899 int has_h264 = 0, has_video = 0; 1900 int minor = 0x200; 1901 int i; 1902 1903 for (i = 0; i < s->nb_streams; i++) { 1904 AVStream *st = s->streams[i]; 1905 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) 1906 has_video = 1; 1907 if (st->codec->codec_id == CODEC_ID_H264) 1908 has_h264 = 1; 1909 } 1910 1911 avio_wb32(pb, 0); /* size */ 1912 ffio_wfourcc(pb, "ftyp"); 1913 1914 if (mov->mode == MODE_3GP) { 1915 ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4"); 1916 minor = has_h264 ? 0x100 : 0x200; 1917 } else if (mov->mode & MODE_3G2) { 1918 ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a"); 1919 minor = has_h264 ? 0x20000 : 0x10000; 1920 }else if (mov->mode == MODE_PSP) 1921 ffio_wfourcc(pb, "MSNV"); 1922 else if (mov->mode == MODE_MP4) 1923 ffio_wfourcc(pb, "isom"); 1924 else if (mov->mode == MODE_IPOD) 1925 ffio_wfourcc(pb, has_video ? "M4V ":"M4A "); 1926 else 1927 ffio_wfourcc(pb, "qt "); 1928 1929 avio_wb32(pb, minor); 1930 1931 if(mov->mode == MODE_MOV) 1932 ffio_wfourcc(pb, "qt "); 1933 else{ 1934 ffio_wfourcc(pb, "isom"); 1935 ffio_wfourcc(pb, "iso2"); 1936 if(has_h264) 1937 ffio_wfourcc(pb, "avc1"); 1938 } 1939 1940 if (mov->mode == MODE_3GP) 1941 ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4"); 1942 else if (mov->mode & MODE_3G2) 1943 ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a"); 1944 else if (mov->mode == MODE_PSP) 1945 ffio_wfourcc(pb, "MSNV"); 1946 else if (mov->mode == MODE_MP4) 1947 ffio_wfourcc(pb, "mp41"); 1948 return updateSize(pb, pos); 1949} 1950 1951static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s) 1952{ 1953 AVCodecContext *VideoCodec = s->streams[0]->codec; 1954 AVCodecContext *AudioCodec = s->streams[1]->codec; 1955 int AudioRate = AudioCodec->sample_rate; 1956 int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num); 1957 int audio_kbitrate= AudioCodec->bit_rate / 1000; 1958 int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate); 1959 1960 avio_wb32(pb, 0x94); /* size */ 1961 ffio_wfourcc(pb, "uuid"); 1962 ffio_wfourcc(pb, "PROF"); 1963 1964 avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */ 1965 avio_wb32(pb, 0xbb88695c); 1966 avio_wb32(pb, 0xfac9c740); 1967 1968 avio_wb32(pb, 0x0); /* ? */ 1969 avio_wb32(pb, 0x3); /* 3 sections ? */ 1970 1971 avio_wb32(pb, 0x14); /* size */ 1972 ffio_wfourcc(pb, "FPRF"); 1973 avio_wb32(pb, 0x0); /* ? */ 1974 avio_wb32(pb, 0x0); /* ? */ 1975 avio_wb32(pb, 0x0); /* ? */ 1976 1977 avio_wb32(pb, 0x2c); /* size */ 1978 ffio_wfourcc(pb, "APRF");/* audio */ 1979 avio_wb32(pb, 0x0); 1980 avio_wb32(pb, 0x2); /* TrackID */ 1981 ffio_wfourcc(pb, "mp4a"); 1982 avio_wb32(pb, 0x20f); 1983 avio_wb32(pb, 0x0); 1984 avio_wb32(pb, audio_kbitrate); 1985 avio_wb32(pb, audio_kbitrate); 1986 avio_wb32(pb, AudioRate); 1987 avio_wb32(pb, AudioCodec->channels); 1988 1989 avio_wb32(pb, 0x34); /* size */ 1990 ffio_wfourcc(pb, "VPRF"); /* video */ 1991 avio_wb32(pb, 0x0); 1992 avio_wb32(pb, 0x1); /* TrackID */ 1993 if (VideoCodec->codec_id == CODEC_ID_H264) { 1994 ffio_wfourcc(pb, "avc1"); 1995 avio_wb16(pb, 0x014D); 1996 avio_wb16(pb, 0x0015); 1997 } else { 1998 ffio_wfourcc(pb, "mp4v"); 1999 avio_wb16(pb, 0x0000); 2000 avio_wb16(pb, 0x0103); 2001 } 2002 avio_wb32(pb, 0x0); 2003 avio_wb32(pb, video_kbitrate); 2004 avio_wb32(pb, video_kbitrate); 2005 avio_wb32(pb, FrameRate); 2006 avio_wb32(pb, FrameRate); 2007 avio_wb16(pb, VideoCodec->width); 2008 avio_wb16(pb, VideoCodec->height); 2009 avio_wb32(pb, 0x010001); /* ? */ 2010} 2011 2012static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags) 2013{ 2014 uint32_t c = -1; 2015 int i, closed_gop = 0; 2016 2017 for (i = 0; i < pkt->size - 4; i++) { 2018 c = (c<<8) + pkt->data[i]; 2019 if (c == 0x1b8) { // gop 2020 closed_gop = pkt->data[i+4]>>6 & 0x01; 2021 } else if (c == 0x100) { // pic 2022 int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6); 2023 if (!temp_ref || closed_gop) // I picture is not reordered 2024 *flags = MOV_SYNC_SAMPLE; 2025 else 2026 *flags = MOV_PARTIAL_SYNC_SAMPLE; 2027 break; 2028 } 2029 } 2030 return 0; 2031} 2032 2033int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt) 2034{ 2035 MOVMuxContext *mov = s->priv_data; 2036 AVIOContext *pb = s->pb; 2037 MOVTrack *trk = &mov->tracks[pkt->stream_index]; 2038 AVCodecContext *enc = trk->enc; 2039 unsigned int samplesInChunk = 0; 2040 int size= pkt->size; 2041 uint8_t *reformatted_data = NULL; 2042 2043 if (!s->pb->seekable) return 0; /* Can't handle that */ 2044 if (!size) return 0; /* Discard 0 sized packets */ 2045 2046 if (enc->codec_id == CODEC_ID_AMR_NB) { 2047 /* We must find out how many AMR blocks there are in one packet */ 2048 static uint16_t packed_size[16] = 2049 {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1}; 2050 int len = 0; 2051 2052 while (len < size && samplesInChunk < 100) { 2053 len += packed_size[(pkt->data[len] >> 3) & 0x0F]; 2054 samplesInChunk++; 2055 } 2056 if(samplesInChunk > 1){ 2057 av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n"); 2058 return -1; 2059 } 2060 } else if (trk->sampleSize) 2061 samplesInChunk = size/trk->sampleSize; 2062 else 2063 samplesInChunk = 1; 2064 2065 /* copy extradata if it exists */ 2066 if (trk->vosLen == 0 && enc->extradata_size > 0) { 2067 trk->vosLen = enc->extradata_size; 2068 trk->vosData = av_malloc(trk->vosLen); 2069 memcpy(trk->vosData, enc->extradata, trk->vosLen); 2070 } 2071 2072 if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) { 2073 /* from x264 or from bytestream h264 */ 2074 /* nal reformating needed */ 2075 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) { 2076 ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data, 2077 &size); 2078 avio_write(pb, reformatted_data, size); 2079 } else { 2080 size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size); 2081 } 2082 } else { 2083 avio_write(pb, pkt->data, size); 2084 } 2085 2086 if ((enc->codec_id == CODEC_ID_DNXHD || 2087 enc->codec_id == CODEC_ID_AC3) && !trk->vosLen) { 2088 /* copy frame to create needed atoms */ 2089 trk->vosLen = size; 2090 trk->vosData = av_malloc(size); 2091 if (!trk->vosData) 2092 return AVERROR(ENOMEM); 2093 memcpy(trk->vosData, pkt->data, size); 2094 } 2095 2096 if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) { 2097 trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster)); 2098 if (!trk->cluster) 2099 return -1; 2100 } 2101 2102 trk->cluster[trk->entry].pos = avio_tell(pb) - size; 2103 trk->cluster[trk->entry].samplesInChunk = samplesInChunk; 2104 trk->cluster[trk->entry].size = size; 2105 trk->cluster[trk->entry].entries = samplesInChunk; 2106 trk->cluster[trk->entry].dts = pkt->dts; 2107 trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration; 2108 2109 if (pkt->pts == AV_NOPTS_VALUE) { 2110 av_log(s, AV_LOG_WARNING, "pts has no value\n"); 2111 pkt->pts = pkt->dts; 2112 } 2113 if (pkt->dts != pkt->pts) 2114 trk->flags |= MOV_TRACK_CTTS; 2115 trk->cluster[trk->entry].cts = pkt->pts - pkt->dts; 2116 trk->cluster[trk->entry].flags = 0; 2117 if (pkt->flags & AV_PKT_FLAG_KEY) { 2118 if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO && 2119 trk->entry > 0) { // force sync sample for the first key frame 2120 mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags); 2121 if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE) 2122 trk->flags |= MOV_TRACK_STPS; 2123 } else { 2124 trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE; 2125 } 2126 if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE) 2127 trk->hasKeyframes++; 2128 } 2129 trk->entry++; 2130 trk->sampleCount += samplesInChunk; 2131 mov->mdat_size += size; 2132 2133 avio_flush(pb); 2134 2135 if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) 2136 ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry, 2137 reformatted_data, size); 2138 av_free(reformatted_data); 2139 return 0; 2140} 2141 2142// QuickTime chapters involve an additional text track with the chapter names 2143// as samples, and a tref pointing from the other tracks to the chapter one. 2144static void mov_create_chapter_track(AVFormatContext *s, int tracknum) 2145{ 2146 MOVMuxContext *mov = s->priv_data; 2147 MOVTrack *track = &mov->tracks[tracknum]; 2148 AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY }; 2149 int i, len; 2150 2151 track->mode = mov->mode; 2152 track->tag = MKTAG('t','e','x','t'); 2153 track->timescale = MOV_TIMESCALE; 2154 track->enc = avcodec_alloc_context3(NULL); 2155 track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE; 2156 2157 for (i = 0; i < s->nb_chapters; i++) { 2158 AVChapter *c = s->chapters[i]; 2159 AVDictionaryEntry *t; 2160 2161 int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE}); 2162 pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE}); 2163 pkt.duration = end - pkt.dts; 2164 2165 if ((t = av_dict_get(c->metadata, "title", NULL, 0))) { 2166 len = strlen(t->value); 2167 pkt.size = len+2; 2168 pkt.data = av_malloc(pkt.size); 2169 AV_WB16(pkt.data, len); 2170 memcpy(pkt.data+2, t->value, len); 2171 ff_mov_write_packet(s, &pkt); 2172 av_freep(&pkt.data); 2173 } 2174 } 2175} 2176 2177static int mov_write_header(AVFormatContext *s) 2178{ 2179 AVIOContext *pb = s->pb; 2180 MOVMuxContext *mov = s->priv_data; 2181 AVDictionaryEntry *t; 2182 int i, hint_track = 0; 2183 2184 if (!s->pb->seekable) { 2185 av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n"); 2186 return -1; 2187 } 2188 2189 /* Default mode == MP4 */ 2190 mov->mode = MODE_MP4; 2191 2192 if (s->oformat != NULL) { 2193 if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP; 2194 else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2; 2195 else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV; 2196 else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP; 2197 else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD; 2198 2199 mov_write_ftyp_tag(pb,s); 2200 if (mov->mode == MODE_PSP) { 2201 if (s->nb_streams != 2) { 2202 av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n"); 2203 return -1; 2204 } 2205 mov_write_uuidprof_tag(pb,s); 2206 } 2207 } 2208 2209 mov->nb_streams = s->nb_streams; 2210 if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters) 2211 mov->chapter_track = mov->nb_streams++; 2212 2213#if FF_API_FLAG_RTP_HINT 2214 if (s->flags & AVFMT_FLAG_RTP_HINT) { 2215 av_log(s, AV_LOG_WARNING, "The RTP_HINT flag is deprecated, enable it " 2216 "via the -movflags rtphint muxer option " 2217 "instead.\n"); 2218 mov->flags |= FF_MOV_FLAG_RTP_HINT; 2219 } 2220#endif 2221 if (mov->flags & FF_MOV_FLAG_RTP_HINT) { 2222 /* Add hint tracks for each audio and video stream */ 2223 hint_track = mov->nb_streams; 2224 for (i = 0; i < s->nb_streams; i++) { 2225 AVStream *st = s->streams[i]; 2226 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO || 2227 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 2228 mov->nb_streams++; 2229 } 2230 } 2231 } 2232 2233 mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks)); 2234 if (!mov->tracks) 2235 return AVERROR(ENOMEM); 2236 2237 for(i=0; i<s->nb_streams; i++){ 2238 AVStream *st= s->streams[i]; 2239 MOVTrack *track= &mov->tracks[i]; 2240 AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0); 2241 2242 track->enc = st->codec; 2243 track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV); 2244 if (track->language < 0) 2245 track->language = 0; 2246 track->mode = mov->mode; 2247 track->tag = mov_find_codec_tag(s, track); 2248 if (!track->tag) { 2249 av_log(s, AV_LOG_ERROR, "track %d: could not find tag, " 2250 "codec not currently supported in container\n", i); 2251 goto error; 2252 } 2253 /* If hinting of this track is enabled by a later hint track, 2254 * this is updated. */ 2255 track->hint_track = -1; 2256 if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){ 2257 if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') || 2258 track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') || 2259 track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) { 2260 if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) { 2261 av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n"); 2262 goto error; 2263 } 2264 track->height = track->tag>>24 == 'n' ? 486 : 576; 2265 } 2266 track->timescale = st->codec->time_base.den; 2267 if (track->mode == MODE_MOV && track->timescale > 100000) 2268 av_log(s, AV_LOG_WARNING, 2269 "WARNING codec timebase is very high. If duration is too long,\n" 2270 "file may not be playable by quicktime. Specify a shorter timebase\n" 2271 "or choose different container.\n"); 2272 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){ 2273 track->timescale = st->codec->sample_rate; 2274 /* set sampleSize for PCM and ADPCM */ 2275 if (av_get_bits_per_sample(st->codec->codec_id)) { 2276 if (!st->codec->block_align) { 2277 av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i); 2278 goto error; 2279 } 2280 track->sampleSize = st->codec->block_align; 2281 } 2282 /* set audio_vbr for compressed audio */ 2283 if (av_get_bits_per_sample(st->codec->codec_id) < 8) { 2284 if (!st->codec->frame_size) { 2285 av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i); 2286 goto error; 2287 } 2288 track->audio_vbr = 1; 2289 } 2290 if (track->mode != MODE_MOV) { 2291 if (track->timescale > UINT16_MAX) { 2292 av_log(s, AV_LOG_ERROR, "track %d: output format does not support " 2293 "sample rate %dhz\n", i, track->timescale); 2294 goto error; 2295 } 2296 if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) { 2297 av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n", 2298 i, track->enc->sample_rate); 2299 goto error; 2300 } 2301 } 2302 }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){ 2303 track->timescale = st->codec->time_base.den; 2304 } 2305 if (!track->height) 2306 track->height = st->codec->height; 2307 2308 avpriv_set_pts_info(st, 64, 1, track->timescale); 2309 } 2310 2311 mov_write_mdat_tag(pb, mov); 2312 2313#if FF_API_TIMESTAMP 2314 if (s->timestamp) 2315 mov->time = s->timestamp; 2316 else 2317#endif 2318 if (t = av_dict_get(s->metadata, "creation_time", NULL, 0)) 2319 mov->time = ff_iso8601_to_unix_time(t->value); 2320 if (mov->time) 2321 mov->time += 0x7C25B080; // 1970 based -> 1904 based 2322 2323 if (mov->chapter_track) 2324 mov_create_chapter_track(s, mov->chapter_track); 2325 2326 if (mov->flags & FF_MOV_FLAG_RTP_HINT) { 2327 /* Initialize the hint tracks for each audio and video stream */ 2328 for (i = 0; i < s->nb_streams; i++) { 2329 AVStream *st = s->streams[i]; 2330 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO || 2331 st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { 2332 ff_mov_init_hinting(s, hint_track, i); 2333 hint_track++; 2334 } 2335 } 2336 } 2337 2338 avio_flush(pb); 2339 2340 return 0; 2341 error: 2342 av_freep(&mov->tracks); 2343 return -1; 2344} 2345 2346static int mov_write_trailer(AVFormatContext *s) 2347{ 2348 MOVMuxContext *mov = s->priv_data; 2349 AVIOContext *pb = s->pb; 2350 int res = 0; 2351 int i; 2352 2353 int64_t moov_pos = avio_tell(pb); 2354 2355 /* Write size of mdat tag */ 2356 if (mov->mdat_size+8 <= UINT32_MAX) { 2357 avio_seek(pb, mov->mdat_pos, SEEK_SET); 2358 avio_wb32(pb, mov->mdat_size+8); 2359 } else { 2360 /* overwrite 'wide' placeholder atom */ 2361 avio_seek(pb, mov->mdat_pos - 8, SEEK_SET); 2362 avio_wb32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */ 2363 ffio_wfourcc(pb, "mdat"); 2364 avio_wb64(pb, mov->mdat_size+16); 2365 } 2366 avio_seek(pb, moov_pos, SEEK_SET); 2367 2368 mov_write_moov_tag(pb, mov, s); 2369 2370 if (mov->chapter_track) 2371 av_freep(&mov->tracks[mov->chapter_track].enc); 2372 2373 for (i=0; i<mov->nb_streams; i++) { 2374 if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) 2375 ff_mov_close_hinting(&mov->tracks[i]); 2376 av_freep(&mov->tracks[i].cluster); 2377 2378 if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData); 2379 2380 } 2381 2382 avio_flush(pb); 2383 2384 av_freep(&mov->tracks); 2385 2386 return res; 2387} 2388 2389#if CONFIG_MOV_MUXER 2390MOV_CLASS(mov) 2391AVOutputFormat ff_mov_muxer = { 2392 .name = "mov", 2393 .long_name = NULL_IF_CONFIG_SMALL("MOV format"), 2394 .extensions = "mov", 2395 .priv_data_size = sizeof(MOVMuxContext), 2396 .audio_codec = CODEC_ID_AAC, 2397#if CONFIG_LIBX264_ENCODER 2398 .video_codec = CODEC_ID_H264, 2399#else 2400 .video_codec = CODEC_ID_MPEG4, 2401#endif 2402 .write_header = mov_write_header, 2403 .write_packet = ff_mov_write_packet, 2404 .write_trailer = mov_write_trailer, 2405 .flags = AVFMT_GLOBALHEADER, 2406 .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0}, 2407 .priv_class = &mov_muxer_class, 2408}; 2409#endif 2410#if CONFIG_TGP_MUXER 2411MOV_CLASS(tgp) 2412AVOutputFormat ff_tgp_muxer = { 2413 .name = "3gp", 2414 .long_name = NULL_IF_CONFIG_SMALL("3GP format"), 2415 .extensions = "3gp", 2416 .priv_data_size = sizeof(MOVMuxContext), 2417 .audio_codec = CODEC_ID_AMR_NB, 2418 .video_codec = CODEC_ID_H263, 2419 .write_header = mov_write_header, 2420 .write_packet = ff_mov_write_packet, 2421 .write_trailer = mov_write_trailer, 2422 .flags = AVFMT_GLOBALHEADER, 2423 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0}, 2424 .priv_class = &tgp_muxer_class, 2425}; 2426#endif 2427#if CONFIG_MP4_MUXER 2428MOV_CLASS(mp4) 2429AVOutputFormat ff_mp4_muxer = { 2430 .name = "mp4", 2431 .long_name = NULL_IF_CONFIG_SMALL("MP4 format"), 2432 .mime_type = "application/mp4", 2433 .extensions = "mp4", 2434 .priv_data_size = sizeof(MOVMuxContext), 2435 .audio_codec = CODEC_ID_AAC, 2436#if CONFIG_LIBX264_ENCODER 2437 .video_codec = CODEC_ID_H264, 2438#else 2439 .video_codec = CODEC_ID_MPEG4, 2440#endif 2441 .write_header = mov_write_header, 2442 .write_packet = ff_mov_write_packet, 2443 .write_trailer = mov_write_trailer, 2444 .flags = AVFMT_GLOBALHEADER, 2445 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0}, 2446 .priv_class = &mp4_muxer_class, 2447}; 2448#endif 2449#if CONFIG_PSP_MUXER 2450MOV_CLASS(psp) 2451AVOutputFormat ff_psp_muxer = { 2452 .name = "psp", 2453 .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 format"), 2454 .extensions = "mp4,psp", 2455 .priv_data_size = sizeof(MOVMuxContext), 2456 .audio_codec = CODEC_ID_AAC, 2457#if CONFIG_LIBX264_ENCODER 2458 .video_codec = CODEC_ID_H264, 2459#else 2460 .video_codec = CODEC_ID_MPEG4, 2461#endif 2462 .write_header = mov_write_header, 2463 .write_packet = ff_mov_write_packet, 2464 .write_trailer = mov_write_trailer, 2465 .flags = AVFMT_GLOBALHEADER, 2466 .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0}, 2467 .priv_class = &psp_muxer_class, 2468}; 2469#endif 2470#if CONFIG_TG2_MUXER 2471MOV_CLASS(tg2) 2472AVOutputFormat ff_tg2_muxer = { 2473 .name = "3g2", 2474 .long_name = NULL_IF_CONFIG_SMALL("3GP2 format"), 2475 .extensions = "3g2", 2476 .priv_data_size = sizeof(MOVMuxContext), 2477 .audio_codec = CODEC_ID_AMR_NB, 2478 .video_codec = CODEC_ID_H263, 2479 .write_header = mov_write_header, 2480 .write_packet = ff_mov_write_packet, 2481 .write_trailer = mov_write_trailer, 2482 .flags = AVFMT_GLOBALHEADER, 2483 .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0}, 2484 .priv_class = &tg2_muxer_class, 2485}; 2486#endif 2487#if CONFIG_IPOD_MUXER 2488MOV_CLASS(ipod) 2489AVOutputFormat ff_ipod_muxer = { 2490 .name = "ipod", 2491 .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"), 2492 .mime_type = "application/mp4", 2493 .extensions = "m4v,m4a", 2494 .priv_data_size = sizeof(MOVMuxContext), 2495 .audio_codec = CODEC_ID_AAC, 2496 .video_codec = CODEC_ID_H264, 2497 .write_header = mov_write_header, 2498 .write_packet = ff_mov_write_packet, 2499 .write_trailer = mov_write_trailer, 2500 .flags = AVFMT_GLOBALHEADER, 2501 .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0}, 2502 .priv_class = &ipod_muxer_class, 2503}; 2504#endif 2505