You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3265 lines
112KB

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