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.

3408 lines
117KB

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