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.

4345 lines
153KB

  1. /*
  2. * MOV, 3GP, MP4 muxer
  3. * Copyright (c) 2003 Thomas Raivio
  4. * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>
  5. * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "movenc.h"
  24. #include "avformat.h"
  25. #include "avio_internal.h"
  26. #include "riff.h"
  27. #include "avio.h"
  28. #include "isom.h"
  29. #include "avc.h"
  30. #include "libavcodec/get_bits.h"
  31. #include "libavcodec/put_bits.h"
  32. #include "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. { "moov_size", "maximum moov size so it can be placed at the begin", offsetof(MOVMuxContext, reserved_moov_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, 0 },
  47. { "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" },
  48. { "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" },
  49. { "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" },
  50. { "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" },
  51. { "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" },
  52. { "faststart", "Run a second pass to put the index (moov atom) at the beginning of the file", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_FASTSTART}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  53. { "omit_tfhd_offset", "Omit the base data offset in tfhd atoms", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_OMIT_TFHD_OFFSET}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  54. FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
  55. { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  56. { "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},
  57. { "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},
  58. { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  59. { "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},
  60. { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  61. { "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},
  62. { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
  63. { "video_track_timescale", "set timescale of all video tracks", offsetof(MOVMuxContext, video_track_timescale), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  64. { NULL },
  65. };
  66. #define MOV_CLASS(flavor)\
  67. static const AVClass flavor ## _muxer_class = {\
  68. .class_name = #flavor " muxer",\
  69. .item_name = av_default_item_name,\
  70. .option = options,\
  71. .version = LIBAVUTIL_VERSION_INT,\
  72. };
  73. static int get_moov_size(AVFormatContext *s);
  74. //FIXME support 64 bit variant with wide placeholders
  75. static int64_t update_size(AVIOContext *pb, int64_t pos)
  76. {
  77. int64_t curpos = avio_tell(pb);
  78. avio_seek(pb, pos, SEEK_SET);
  79. avio_wb32(pb, curpos - pos); /* rewrite size */
  80. avio_seek(pb, curpos, SEEK_SET);
  81. return curpos - pos;
  82. }
  83. static int supports_edts(MOVMuxContext *mov)
  84. {
  85. // EDTS with fragments is tricky as we don't know the duration when its written
  86. return (mov->use_editlist<0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) || mov->use_editlist>0;
  87. }
  88. static int co64_required(const MOVTrack *track)
  89. {
  90. if (track->entry > 0 && track->cluster[track->entry - 1].pos + track->data_offset > UINT32_MAX)
  91. return 1;
  92. return 0;
  93. }
  94. /* Chunk offset atom */
  95. static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
  96. {
  97. int i;
  98. int mode64 = co64_required(track); // use 32 bit size variant if possible
  99. int64_t pos = avio_tell(pb);
  100. avio_wb32(pb, 0); /* size */
  101. if (mode64)
  102. ffio_wfourcc(pb, "co64");
  103. else
  104. ffio_wfourcc(pb, "stco");
  105. avio_wb32(pb, 0); /* version & flags */
  106. avio_wb32(pb, track->chunkCount); /* entry count */
  107. for (i = 0; i < track->entry; i++) {
  108. if (!track->cluster[i].chunkNum)
  109. continue;
  110. if (mode64 == 1)
  111. avio_wb64(pb, track->cluster[i].pos + track->data_offset);
  112. else
  113. avio_wb32(pb, track->cluster[i].pos + track->data_offset);
  114. }
  115. return update_size(pb, pos);
  116. }
  117. /* Sample size atom */
  118. static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
  119. {
  120. int equalChunks = 1;
  121. int i, j, entries = 0, tst = -1, oldtst = -1;
  122. int64_t pos = avio_tell(pb);
  123. avio_wb32(pb, 0); /* size */
  124. ffio_wfourcc(pb, "stsz");
  125. avio_wb32(pb, 0); /* version & flags */
  126. for (i = 0; i < track->entry; i++) {
  127. tst = track->cluster[i].size / track->cluster[i].entries;
  128. if (oldtst != -1 && tst != oldtst)
  129. equalChunks = 0;
  130. oldtst = tst;
  131. entries += track->cluster[i].entries;
  132. }
  133. if (equalChunks && track->entry) {
  134. int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
  135. sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
  136. avio_wb32(pb, sSize); // sample size
  137. avio_wb32(pb, entries); // sample count
  138. } else {
  139. avio_wb32(pb, 0); // sample size
  140. avio_wb32(pb, entries); // sample count
  141. for (i = 0; i < track->entry; i++) {
  142. for (j = 0; j < track->cluster[i].entries; j++) {
  143. avio_wb32(pb, track->cluster[i].size /
  144. track->cluster[i].entries);
  145. }
  146. }
  147. }
  148. return update_size(pb, pos);
  149. }
  150. /* Sample to chunk atom */
  151. static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
  152. {
  153. int index = 0, oldval = -1, i;
  154. int64_t entryPos, curpos;
  155. int64_t pos = avio_tell(pb);
  156. avio_wb32(pb, 0); /* size */
  157. ffio_wfourcc(pb, "stsc");
  158. avio_wb32(pb, 0); // version & flags
  159. entryPos = avio_tell(pb);
  160. avio_wb32(pb, track->chunkCount); // entry count
  161. for (i = 0; i < track->entry; i++) {
  162. if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
  163. avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
  164. avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
  165. avio_wb32(pb, 0x1); // sample description index
  166. oldval = track->cluster[i].samples_in_chunk;
  167. index++;
  168. }
  169. }
  170. curpos = avio_tell(pb);
  171. avio_seek(pb, entryPos, SEEK_SET);
  172. avio_wb32(pb, index); // rewrite size
  173. avio_seek(pb, curpos, SEEK_SET);
  174. return update_size(pb, pos);
  175. }
  176. /* Sync sample atom */
  177. static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
  178. {
  179. int64_t curpos, entryPos;
  180. int i, index = 0;
  181. int64_t pos = avio_tell(pb);
  182. avio_wb32(pb, 0); // size
  183. ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
  184. avio_wb32(pb, 0); // version & flags
  185. entryPos = avio_tell(pb);
  186. avio_wb32(pb, track->entry); // entry count
  187. for (i = 0; i < track->entry; i++) {
  188. if (track->cluster[i].flags & flag) {
  189. avio_wb32(pb, i + 1);
  190. index++;
  191. }
  192. }
  193. curpos = avio_tell(pb);
  194. avio_seek(pb, entryPos, SEEK_SET);
  195. avio_wb32(pb, index); // rewrite size
  196. avio_seek(pb, curpos, SEEK_SET);
  197. return update_size(pb, pos);
  198. }
  199. static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
  200. {
  201. avio_wb32(pb, 0x11); /* size */
  202. if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
  203. else ffio_wfourcc(pb, "damr");
  204. ffio_wfourcc(pb, "FFMP");
  205. avio_w8(pb, 0); /* decoder version */
  206. avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  207. avio_w8(pb, 0x00); /* Mode change period (no restriction) */
  208. avio_w8(pb, 0x01); /* Frames per sample */
  209. return 0x11;
  210. }
  211. static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
  212. {
  213. GetBitContext gbc;
  214. PutBitContext pbc;
  215. uint8_t buf[3];
  216. int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
  217. if (track->vos_len < 7)
  218. return -1;
  219. avio_wb32(pb, 11);
  220. ffio_wfourcc(pb, "dac3");
  221. init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
  222. fscod = get_bits(&gbc, 2);
  223. frmsizecod = get_bits(&gbc, 6);
  224. bsid = get_bits(&gbc, 5);
  225. bsmod = get_bits(&gbc, 3);
  226. acmod = get_bits(&gbc, 3);
  227. if (acmod == 2) {
  228. skip_bits(&gbc, 2); // dsurmod
  229. } else {
  230. if ((acmod & 1) && acmod != 1)
  231. skip_bits(&gbc, 2); // cmixlev
  232. if (acmod & 4)
  233. skip_bits(&gbc, 2); // surmixlev
  234. }
  235. lfeon = get_bits1(&gbc);
  236. init_put_bits(&pbc, buf, sizeof(buf));
  237. put_bits(&pbc, 2, fscod);
  238. put_bits(&pbc, 5, bsid);
  239. put_bits(&pbc, 3, bsmod);
  240. put_bits(&pbc, 3, acmod);
  241. put_bits(&pbc, 1, lfeon);
  242. put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
  243. put_bits(&pbc, 5, 0); // reserved
  244. flush_put_bits(&pbc);
  245. avio_write(pb, buf, sizeof(buf));
  246. return 11;
  247. }
  248. /**
  249. * This function writes extradata "as is".
  250. * Extradata must be formatted like a valid atom (with size and tag).
  251. */
  252. static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
  253. {
  254. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  255. return track->enc->extradata_size;
  256. }
  257. static int mov_write_enda_tag(AVIOContext *pb)
  258. {
  259. avio_wb32(pb, 10);
  260. ffio_wfourcc(pb, "enda");
  261. avio_wb16(pb, 1); /* little endian */
  262. return 10;
  263. }
  264. static int mov_write_enda_tag_be(AVIOContext *pb)
  265. {
  266. avio_wb32(pb, 10);
  267. ffio_wfourcc(pb, "enda");
  268. avio_wb16(pb, 0); /* big endian */
  269. return 10;
  270. }
  271. static void put_descr(AVIOContext *pb, int tag, unsigned int size)
  272. {
  273. int i = 3;
  274. avio_w8(pb, tag);
  275. for (; i > 0; i--)
  276. avio_w8(pb, (size >> (7 * i)) | 0x80);
  277. avio_w8(pb, size & 0x7F);
  278. }
  279. static unsigned compute_avg_bitrate(MOVTrack *track)
  280. {
  281. uint64_t size = 0;
  282. int i;
  283. if (!track->track_duration)
  284. return 0;
  285. for (i = 0; i < track->entry; i++)
  286. size += track->cluster[i].size;
  287. return size * 8 * track->timescale / track->track_duration;
  288. }
  289. static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
  290. {
  291. int64_t pos = avio_tell(pb);
  292. int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
  293. unsigned avg_bitrate;
  294. avio_wb32(pb, 0); // size
  295. ffio_wfourcc(pb, "esds");
  296. avio_wb32(pb, 0); // Version
  297. // ES descriptor
  298. put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
  299. avio_wb16(pb, track->track_id);
  300. avio_w8(pb, 0x00); // flags (= no flags)
  301. // DecoderConfig descriptor
  302. put_descr(pb, 0x04, 13 + decoder_specific_info_len);
  303. // Object type indication
  304. if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
  305. track->enc->codec_id == AV_CODEC_ID_MP3) &&
  306. track->enc->sample_rate > 24000)
  307. avio_w8(pb, 0x6B); // 11172-3
  308. else
  309. avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
  310. // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
  311. // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
  312. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  313. avio_w8(pb, 0x15); // flags (= Audiostream)
  314. else
  315. avio_w8(pb, 0x11); // flags (= Visualstream)
  316. avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
  317. avg_bitrate = compute_avg_bitrate(track);
  318. // maxbitrate (FIXME should be max rate in any 1 sec window)
  319. avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
  320. avio_wb32(pb, avg_bitrate);
  321. if (track->vos_len) {
  322. // DecoderSpecific info descriptor
  323. put_descr(pb, 0x05, track->vos_len);
  324. avio_write(pb, track->vos_data, track->vos_len);
  325. }
  326. // SL descriptor
  327. put_descr(pb, 0x06, 1);
  328. avio_w8(pb, 0x02);
  329. return update_size(pb, pos);
  330. }
  331. static int mov_pcm_le_gt16(enum AVCodecID codec_id)
  332. {
  333. return codec_id == AV_CODEC_ID_PCM_S24LE ||
  334. codec_id == AV_CODEC_ID_PCM_S32LE ||
  335. codec_id == AV_CODEC_ID_PCM_F32LE ||
  336. codec_id == AV_CODEC_ID_PCM_F64LE;
  337. }
  338. static int mov_pcm_be_gt16(enum AVCodecID codec_id)
  339. {
  340. return codec_id == AV_CODEC_ID_PCM_S24BE ||
  341. codec_id == AV_CODEC_ID_PCM_S32BE ||
  342. codec_id == AV_CODEC_ID_PCM_F32BE ||
  343. codec_id == AV_CODEC_ID_PCM_F64BE;
  344. }
  345. static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
  346. {
  347. int64_t pos = avio_tell(pb);
  348. avio_wb32(pb, 0);
  349. avio_wl32(pb, track->tag); // store it byteswapped
  350. track->enc->codec_tag = av_bswap16(track->tag >> 16);
  351. ff_put_wav_header(pb, track->enc);
  352. return update_size(pb, pos);
  353. }
  354. static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
  355. {
  356. int64_t pos = avio_tell(pb);
  357. avio_wb32(pb, 0);
  358. ffio_wfourcc(pb, "wfex");
  359. ff_put_wav_header(pb, track->enc);
  360. return update_size(pb, pos);
  361. }
  362. static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
  363. {
  364. uint32_t layout_tag, bitmap;
  365. int64_t pos = avio_tell(pb);
  366. layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
  367. track->enc->channel_layout,
  368. &bitmap);
  369. if (!layout_tag) {
  370. av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
  371. "lack of channel information\n");
  372. return 0;
  373. }
  374. avio_wb32(pb, 0); // Size
  375. ffio_wfourcc(pb, "chan"); // Type
  376. avio_w8(pb, 0); // Version
  377. avio_wb24(pb, 0); // Flags
  378. avio_wb32(pb, layout_tag); // mChannelLayoutTag
  379. avio_wb32(pb, bitmap); // mChannelBitmap
  380. avio_wb32(pb, 0); // mNumberChannelDescriptions
  381. return update_size(pb, pos);
  382. }
  383. static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
  384. {
  385. int64_t pos = avio_tell(pb);
  386. avio_wb32(pb, 0); /* size */
  387. ffio_wfourcc(pb, "wave");
  388. if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
  389. avio_wb32(pb, 12); /* size */
  390. ffio_wfourcc(pb, "frma");
  391. avio_wl32(pb, track->tag);
  392. }
  393. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  394. /* useless atom needed by mplayer, ipod, not needed by quicktime */
  395. avio_wb32(pb, 12); /* size */
  396. ffio_wfourcc(pb, "mp4a");
  397. avio_wb32(pb, 0);
  398. mov_write_esds_tag(pb, track);
  399. } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
  400. mov_write_enda_tag(pb);
  401. } else if (mov_pcm_be_gt16(track->enc->codec_id)) {
  402. mov_write_enda_tag_be(pb);
  403. } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
  404. mov_write_amr_tag(pb, track);
  405. } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
  406. mov_write_ac3_tag(pb, track);
  407. } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
  408. track->enc->codec_id == AV_CODEC_ID_QDM2) {
  409. mov_write_extradata_tag(pb, track);
  410. } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  411. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  412. mov_write_ms_tag(pb, track);
  413. }
  414. avio_wb32(pb, 8); /* size */
  415. avio_wb32(pb, 0); /* null tag */
  416. return update_size(pb, pos);
  417. }
  418. static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
  419. {
  420. uint8_t *unescaped;
  421. const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
  422. int unescaped_size, seq_found = 0;
  423. int level = 0, interlace = 0;
  424. int packet_seq = track->vc1_info.packet_seq;
  425. int packet_entry = track->vc1_info.packet_entry;
  426. int slices = track->vc1_info.slices;
  427. PutBitContext pbc;
  428. if (track->start_dts == AV_NOPTS_VALUE) {
  429. /* No packets written yet, vc1_info isn't authoritative yet. */
  430. /* Assume inline sequence and entry headers. This will be
  431. * overwritten at the end if the file is seekable. */
  432. packet_seq = packet_entry = 1;
  433. }
  434. unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
  435. if (!unescaped)
  436. return AVERROR(ENOMEM);
  437. start = find_next_marker(track->vos_data, end);
  438. for (next = start; next < end; start = next) {
  439. GetBitContext gb;
  440. int size;
  441. next = find_next_marker(start + 4, end);
  442. size = next - start - 4;
  443. if (size <= 0)
  444. continue;
  445. unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
  446. init_get_bits(&gb, unescaped, 8 * unescaped_size);
  447. if (AV_RB32(start) == VC1_CODE_SEQHDR) {
  448. int profile = get_bits(&gb, 2);
  449. if (profile != PROFILE_ADVANCED) {
  450. av_free(unescaped);
  451. return AVERROR(ENOSYS);
  452. }
  453. seq_found = 1;
  454. level = get_bits(&gb, 3);
  455. /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
  456. * width, height */
  457. skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
  458. skip_bits(&gb, 1); /* broadcast */
  459. interlace = get_bits1(&gb);
  460. skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
  461. }
  462. }
  463. if (!seq_found) {
  464. av_free(unescaped);
  465. return AVERROR(ENOSYS);
  466. }
  467. init_put_bits(&pbc, buf, 7);
  468. /* VC1DecSpecStruc */
  469. put_bits(&pbc, 4, 12); /* profile - advanced */
  470. put_bits(&pbc, 3, level);
  471. put_bits(&pbc, 1, 0); /* reserved */
  472. /* VC1AdvDecSpecStruc */
  473. put_bits(&pbc, 3, level);
  474. put_bits(&pbc, 1, 0); /* cbr */
  475. put_bits(&pbc, 6, 0); /* reserved */
  476. put_bits(&pbc, 1, !interlace); /* no interlace */
  477. put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
  478. put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
  479. put_bits(&pbc, 1, !slices); /* no slice code */
  480. put_bits(&pbc, 1, 0); /* no bframe */
  481. put_bits(&pbc, 1, 0); /* reserved */
  482. put_bits32(&pbc, track->enc->time_base.den); /* framerate */
  483. flush_put_bits(&pbc);
  484. av_free(unescaped);
  485. return 0;
  486. }
  487. static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
  488. {
  489. uint8_t buf[7] = { 0 };
  490. int ret;
  491. if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
  492. return ret;
  493. avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
  494. ffio_wfourcc(pb, "dvc1");
  495. track->vc1_info.struct_offset = avio_tell(pb);
  496. avio_write(pb, buf, sizeof(buf));
  497. avio_write(pb, track->vos_data, track->vos_len);
  498. return 0;
  499. }
  500. static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
  501. {
  502. avio_wb32(pb, track->vos_len + 8);
  503. ffio_wfourcc(pb, "glbl");
  504. avio_write(pb, track->vos_data, track->vos_len);
  505. return 8 + track->vos_len;
  506. }
  507. /**
  508. * Compute flags for 'lpcm' tag.
  509. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  510. */
  511. static int mov_get_lpcm_flags(enum AVCodecID codec_id)
  512. {
  513. switch (codec_id) {
  514. case AV_CODEC_ID_PCM_F32BE:
  515. case AV_CODEC_ID_PCM_F64BE:
  516. return 11;
  517. case AV_CODEC_ID_PCM_F32LE:
  518. case AV_CODEC_ID_PCM_F64LE:
  519. return 9;
  520. case AV_CODEC_ID_PCM_U8:
  521. return 10;
  522. case AV_CODEC_ID_PCM_S16BE:
  523. case AV_CODEC_ID_PCM_S24BE:
  524. case AV_CODEC_ID_PCM_S32BE:
  525. return 14;
  526. case AV_CODEC_ID_PCM_S8:
  527. case AV_CODEC_ID_PCM_S16LE:
  528. case AV_CODEC_ID_PCM_S24LE:
  529. case AV_CODEC_ID_PCM_S32LE:
  530. return 12;
  531. default:
  532. return 0;
  533. }
  534. }
  535. static int get_cluster_duration(MOVTrack *track, int cluster_idx)
  536. {
  537. int64_t next_dts;
  538. if (cluster_idx >= track->entry)
  539. return 0;
  540. if (cluster_idx + 1 == track->entry)
  541. next_dts = track->track_duration + track->start_dts;
  542. else
  543. next_dts = track->cluster[cluster_idx + 1].dts;
  544. return next_dts - track->cluster[cluster_idx].dts;
  545. }
  546. static int get_samples_per_packet(MOVTrack *track)
  547. {
  548. int i, first_duration;
  549. // return track->enc->frame_size;
  550. /* use 1 for raw PCM */
  551. if (!track->audio_vbr)
  552. return 1;
  553. /* check to see if duration is constant for all clusters */
  554. if (!track->entry)
  555. return 0;
  556. first_duration = get_cluster_duration(track, 0);
  557. for (i = 1; i < track->entry; i++) {
  558. if (get_cluster_duration(track, i) != first_duration)
  559. return 0;
  560. }
  561. return first_duration;
  562. }
  563. static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
  564. {
  565. int64_t pos = avio_tell(pb);
  566. int version = 0;
  567. uint32_t tag = track->tag;
  568. if (track->mode == MODE_MOV) {
  569. if (track->timescale > UINT16_MAX) {
  570. if (mov_get_lpcm_flags(track->enc->codec_id))
  571. tag = AV_RL32("lpcm");
  572. version = 2;
  573. } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
  574. mov_pcm_be_gt16(track->enc->codec_id) ||
  575. track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  576. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  577. track->enc->codec_id == AV_CODEC_ID_QDM2) {
  578. version = 1;
  579. }
  580. }
  581. avio_wb32(pb, 0); /* size */
  582. avio_wl32(pb, tag); // store it byteswapped
  583. avio_wb32(pb, 0); /* Reserved */
  584. avio_wb16(pb, 0); /* Reserved */
  585. avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
  586. /* SoundDescription */
  587. avio_wb16(pb, version); /* Version */
  588. avio_wb16(pb, 0); /* Revision level */
  589. avio_wb32(pb, 0); /* Reserved */
  590. if (version == 2) {
  591. avio_wb16(pb, 3);
  592. avio_wb16(pb, 16);
  593. avio_wb16(pb, 0xfffe);
  594. avio_wb16(pb, 0);
  595. avio_wb32(pb, 0x00010000);
  596. avio_wb32(pb, 72);
  597. avio_wb64(pb, av_double2int(track->enc->sample_rate));
  598. avio_wb32(pb, track->enc->channels);
  599. avio_wb32(pb, 0x7F000000);
  600. avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
  601. avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
  602. avio_wb32(pb, track->sample_size);
  603. avio_wb32(pb, get_samples_per_packet(track));
  604. } else {
  605. if (track->mode == MODE_MOV) {
  606. avio_wb16(pb, track->enc->channels);
  607. if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
  608. track->enc->codec_id == AV_CODEC_ID_PCM_S8)
  609. avio_wb16(pb, 8); /* bits per sample */
  610. else
  611. avio_wb16(pb, 16);
  612. avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
  613. } else { /* reserved for mp4/3gp */
  614. avio_wb16(pb, 2);
  615. avio_wb16(pb, 16);
  616. avio_wb16(pb, 0);
  617. }
  618. avio_wb16(pb, 0); /* packet size (= 0) */
  619. avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
  620. track->enc->sample_rate : 0);
  621. avio_wb16(pb, 0); /* Reserved */
  622. }
  623. if (version == 1) { /* SoundDescription V1 extended info */
  624. if (mov_pcm_le_gt16(track->enc->codec_id) ||
  625. mov_pcm_be_gt16(track->enc->codec_id))
  626. avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
  627. else
  628. avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
  629. avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
  630. avio_wb32(pb, track->sample_size); /* Bytes per frame */
  631. avio_wb32(pb, 2); /* Bytes per sample */
  632. }
  633. if (track->mode == MODE_MOV &&
  634. (track->enc->codec_id == AV_CODEC_ID_AAC ||
  635. track->enc->codec_id == AV_CODEC_ID_AC3 ||
  636. track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
  637. track->enc->codec_id == AV_CODEC_ID_ALAC ||
  638. track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  639. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  640. track->enc->codec_id == AV_CODEC_ID_QDM2 ||
  641. (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
  642. (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
  643. mov_write_wave_tag(pb, track);
  644. else if (track->tag == MKTAG('m','p','4','a'))
  645. mov_write_esds_tag(pb, track);
  646. else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
  647. mov_write_amr_tag(pb, track);
  648. else if (track->enc->codec_id == AV_CODEC_ID_AC3)
  649. mov_write_ac3_tag(pb, track);
  650. else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
  651. mov_write_extradata_tag(pb, track);
  652. else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
  653. mov_write_wfex_tag(pb, track);
  654. else if (track->vos_len > 0)
  655. mov_write_glbl_tag(pb, track);
  656. if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  657. mov_write_chan_tag(pb, track);
  658. return update_size(pb, pos);
  659. }
  660. static int mov_write_d263_tag(AVIOContext *pb)
  661. {
  662. avio_wb32(pb, 0xf); /* size */
  663. ffio_wfourcc(pb, "d263");
  664. ffio_wfourcc(pb, "FFMP");
  665. avio_w8(pb, 0); /* decoder version */
  666. /* FIXME use AVCodecContext level/profile, when encoder will set values */
  667. avio_w8(pb, 0xa); /* level */
  668. avio_w8(pb, 0); /* profile */
  669. return 0xf;
  670. }
  671. static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
  672. {
  673. int64_t pos = avio_tell(pb);
  674. avio_wb32(pb, 0);
  675. ffio_wfourcc(pb, "avcC");
  676. ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
  677. return update_size(pb, pos);
  678. }
  679. /* also used by all avid codecs (dv, imx, meridien) and their variants */
  680. static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
  681. {
  682. int i;
  683. avio_wb32(pb, 24); /* size */
  684. ffio_wfourcc(pb, "ACLR");
  685. ffio_wfourcc(pb, "ACLR");
  686. ffio_wfourcc(pb, "0001");
  687. avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
  688. avio_wb32(pb, 0); /* unknown */
  689. avio_wb32(pb, 24); /* size */
  690. ffio_wfourcc(pb, "APRG");
  691. ffio_wfourcc(pb, "APRG");
  692. ffio_wfourcc(pb, "0001");
  693. avio_wb32(pb, 1); /* unknown */
  694. avio_wb32(pb, 0); /* unknown */
  695. avio_wb32(pb, 120); /* size */
  696. ffio_wfourcc(pb, "ARES");
  697. ffio_wfourcc(pb, "ARES");
  698. ffio_wfourcc(pb, "0001");
  699. avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
  700. avio_wb32(pb, track->enc->width);
  701. /* values below are based on samples created with quicktime and avid codecs */
  702. if (track->vos_data[5] & 2) { // interlaced
  703. avio_wb32(pb, track->enc->height / 2);
  704. avio_wb32(pb, 2); /* unknown */
  705. avio_wb32(pb, 0); /* unknown */
  706. avio_wb32(pb, 4); /* unknown */
  707. } else {
  708. avio_wb32(pb, track->enc->height);
  709. avio_wb32(pb, 1); /* unknown */
  710. avio_wb32(pb, 0); /* unknown */
  711. if (track->enc->height == 1080)
  712. avio_wb32(pb, 5); /* unknown */
  713. else
  714. avio_wb32(pb, 6); /* unknown */
  715. }
  716. /* padding */
  717. for (i = 0; i < 10; i++)
  718. avio_wb64(pb, 0);
  719. /* extra padding for stsd needed */
  720. avio_wb32(pb, 0);
  721. return 0;
  722. }
  723. static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  724. {
  725. int tag = track->enc->codec_tag;
  726. if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
  727. return 0;
  728. if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
  729. else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
  730. else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
  731. else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
  732. else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
  733. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
  734. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
  735. return tag;
  736. }
  737. static const AVCodecTag codec_ipod_tags[] = {
  738. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  739. { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  740. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  741. { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
  742. { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
  743. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  744. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
  745. { AV_CODEC_ID_NONE, 0 },
  746. };
  747. static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  748. {
  749. int tag = track->enc->codec_tag;
  750. // keep original tag for subs, ipod supports both formats
  751. if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  752. (tag == MKTAG('t', 'x', '3', 'g') ||
  753. tag == MKTAG('t', 'e', 'x', 't'))))
  754. tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
  755. if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
  756. av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
  757. "Quicktime/Ipod might not play the file\n");
  758. return tag;
  759. }
  760. static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
  761. {
  762. int tag;
  763. if (track->enc->width == 720) { /* SD */
  764. if (track->enc->height == 480) { /* NTSC */
  765. if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
  766. else tag = MKTAG('d','v','c',' ');
  767. }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
  768. else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
  769. else tag = MKTAG('d','v','p','p');
  770. } else if (track->enc->height == 720) { /* HD 720 line */
  771. if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
  772. else tag = MKTAG('d','v','h','p');
  773. } else if (track->enc->height == 1080) { /* HD 1080 line */
  774. if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
  775. else tag = MKTAG('d','v','h','6');
  776. } else {
  777. av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
  778. return 0;
  779. }
  780. return tag;
  781. }
  782. static AVRational find_fps(AVFormatContext *s, AVStream *st)
  783. {
  784. AVRational rate = {st->codec->time_base.den, st->codec->time_base.num};
  785. /* if the codec time base makes no sense, try to fallback on stream frame rate */
  786. if (av_timecode_check_frame_rate(rate) < 0) {
  787. av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
  788. rate.num, rate.den, st->avg_frame_rate.num, st->avg_frame_rate.den);
  789. rate = st->avg_frame_rate;
  790. }
  791. return rate;
  792. }
  793. static int mov_get_mpeg2_xdcam_codec_tag(AVFormatContext *s, MOVTrack *track)
  794. {
  795. int tag = MKTAG('m', '2', 'v', '1'); //fallback tag
  796. int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
  797. AVStream *st = track->st;
  798. int rate = av_q2d(find_fps(s, st));
  799. if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) {
  800. if (track->enc->width == 1280 && track->enc->height == 720) {
  801. if (!interlaced) {
  802. if (rate == 24) tag = MKTAG('x','d','v','4');
  803. else if (rate == 25) tag = MKTAG('x','d','v','5');
  804. else if (rate == 30) tag = MKTAG('x','d','v','1');
  805. else if (rate == 50) tag = MKTAG('x','d','v','a');
  806. else if (rate == 60) tag = MKTAG('x','d','v','9');
  807. }
  808. } else if (track->enc->width == 1440 && track->enc->height == 1080) {
  809. if (!interlaced) {
  810. if (rate == 24) tag = MKTAG('x','d','v','6');
  811. else if (rate == 25) tag = MKTAG('x','d','v','7');
  812. else if (rate == 30) tag = MKTAG('x','d','v','8');
  813. } else {
  814. if (rate == 25) tag = MKTAG('x','d','v','3');
  815. else if (rate == 30) tag = MKTAG('x','d','v','2');
  816. }
  817. } else if (track->enc->width == 1920 && track->enc->height == 1080) {
  818. if (!interlaced) {
  819. if (rate == 24) tag = MKTAG('x','d','v','d');
  820. else if (rate == 25) tag = MKTAG('x','d','v','e');
  821. else if (rate == 30) tag = MKTAG('x','d','v','f');
  822. } else {
  823. if (rate == 25) tag = MKTAG('x','d','v','c');
  824. else if (rate == 30) tag = MKTAG('x','d','v','b');
  825. }
  826. }
  827. } else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
  828. if (track->enc->width == 1280 && track->enc->height == 720) {
  829. if (!interlaced) {
  830. if (rate == 24) tag = MKTAG('x','d','5','4');
  831. else if (rate == 25) tag = MKTAG('x','d','5','5');
  832. else if (rate == 30) tag = MKTAG('x','d','5','1');
  833. else if (rate == 50) tag = MKTAG('x','d','5','a');
  834. else if (rate == 60) tag = MKTAG('x','d','5','9');
  835. }
  836. } else if (track->enc->width == 1920 && track->enc->height == 1080) {
  837. if (!interlaced) {
  838. if (rate == 24) tag = MKTAG('x','d','5','d');
  839. else if (rate == 25) tag = MKTAG('x','d','5','e');
  840. else if (rate == 30) tag = MKTAG('x','d','5','f');
  841. } else {
  842. if (rate == 25) tag = MKTAG('x','d','5','c');
  843. else if (rate == 30) tag = MKTAG('x','d','5','b');
  844. }
  845. }
  846. }
  847. return tag;
  848. }
  849. static const struct {
  850. enum AVPixelFormat pix_fmt;
  851. uint32_t tag;
  852. unsigned bps;
  853. } mov_pix_fmt_tags[] = {
  854. { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
  855. { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
  856. { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
  857. { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
  858. { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
  859. { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
  860. { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
  861. { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
  862. { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
  863. { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
  864. { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
  865. { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
  866. { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
  867. { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
  868. { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
  869. };
  870. static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
  871. {
  872. int tag = track->enc->codec_tag;
  873. int i;
  874. for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
  875. if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
  876. tag = mov_pix_fmt_tags[i].tag;
  877. track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
  878. if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
  879. break;
  880. }
  881. }
  882. return tag;
  883. }
  884. static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  885. {
  886. int tag = track->enc->codec_tag;
  887. if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
  888. (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
  889. track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
  890. track->enc->codec_id == AV_CODEC_ID_H263 ||
  891. track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
  892. av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
  893. if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
  894. tag = mov_get_dv_codec_tag(s, track);
  895. else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
  896. tag = mov_get_rawvideo_codec_tag(s, track);
  897. else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO)
  898. tag = mov_get_mpeg2_xdcam_codec_tag(s, track);
  899. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  900. tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
  901. if (!tag) { // if no mac fcc found, try with Microsoft tags
  902. tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
  903. if (tag)
  904. av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
  905. "the file may be unplayable!\n");
  906. }
  907. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  908. tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
  909. if (!tag) { // if no mac fcc found, try with Microsoft tags
  910. int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
  911. if (ms_tag) {
  912. tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
  913. av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
  914. "the file may be unplayable!\n");
  915. }
  916. }
  917. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  918. tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
  919. }
  920. return tag;
  921. }
  922. static const AVCodecTag codec_3gp_tags[] = {
  923. { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
  924. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  925. { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  926. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  927. { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
  928. { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
  929. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  930. { AV_CODEC_ID_NONE, 0 },
  931. };
  932. static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
  933. { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
  934. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  935. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  936. { AV_CODEC_ID_VP6A, MKTAG('V','P','6','A') },
  937. { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
  938. { AV_CODEC_ID_NONE, 0 },
  939. };
  940. static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
  941. {
  942. int tag;
  943. if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
  944. tag = mp4_get_codec_tag(s, track);
  945. else if (track->mode == MODE_ISM) {
  946. tag = mp4_get_codec_tag(s, track);
  947. if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
  948. tag = MKTAG('w', 'm', 'a', ' ');
  949. } else if (track->mode == MODE_IPOD)
  950. tag = ipod_get_codec_tag(s, track);
  951. else if (track->mode & MODE_3GP)
  952. tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
  953. else if (track->mode == MODE_F4V)
  954. tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
  955. else
  956. tag = mov_get_codec_tag(s, track);
  957. return tag;
  958. }
  959. /** Write uuid atom.
  960. * Needed to make file play in iPods running newest firmware
  961. * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
  962. */
  963. static int mov_write_uuid_tag_ipod(AVIOContext *pb)
  964. {
  965. avio_wb32(pb, 28);
  966. ffio_wfourcc(pb, "uuid");
  967. avio_wb32(pb, 0x6b6840f2);
  968. avio_wb32(pb, 0x5f244fc5);
  969. avio_wb32(pb, 0xba39a51b);
  970. avio_wb32(pb, 0xcf0323f3);
  971. avio_wb32(pb, 0x0);
  972. return 28;
  973. }
  974. static const uint16_t fiel_data[] = {
  975. 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
  976. };
  977. static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
  978. {
  979. unsigned mov_field_order = 0;
  980. if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
  981. mov_field_order = fiel_data[track->enc->field_order];
  982. else
  983. return 0;
  984. avio_wb32(pb, 10);
  985. ffio_wfourcc(pb, "fiel");
  986. avio_wb16(pb, mov_field_order);
  987. return 10;
  988. }
  989. static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
  990. {
  991. int64_t pos = avio_tell(pb);
  992. avio_wb32(pb, 0); /* size */
  993. avio_wl32(pb, track->tag); // store it byteswapped
  994. avio_wb32(pb, 0); /* Reserved */
  995. avio_wb16(pb, 0); /* Reserved */
  996. avio_wb16(pb, 1); /* Data-reference index */
  997. if (track->enc->extradata_size)
  998. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  999. return update_size(pb, pos);
  1000. }
  1001. static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
  1002. {
  1003. AVRational sar;
  1004. av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
  1005. track->enc->sample_aspect_ratio.den, INT_MAX);
  1006. avio_wb32(pb, 16);
  1007. ffio_wfourcc(pb, "pasp");
  1008. avio_wb32(pb, sar.num);
  1009. avio_wb32(pb, sar.den);
  1010. return 16;
  1011. }
  1012. static void find_compressor(char * compressor_name, int len, MOVTrack *track)
  1013. {
  1014. int xdcam_res = (track->enc->width == 1280 && track->enc->height == 720)
  1015. || (track->enc->width == 1440 && track->enc->height == 1080)
  1016. || (track->enc->width == 1920 && track->enc->height == 1080);
  1017. if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name) {
  1018. av_strlcpy(compressor_name, track->enc->codec->name, 32);
  1019. } else if (track->enc->codec_id == AV_CODEC_ID_MPEG2VIDEO && xdcam_res) {
  1020. int interlaced = track->enc->field_order > AV_FIELD_PROGRESSIVE;
  1021. AVStream *st = track->st;
  1022. int rate = av_q2d(find_fps(NULL, st));
  1023. av_strlcatf(compressor_name, len, "XDCAM");
  1024. if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) {
  1025. av_strlcatf(compressor_name, len, " HD422");
  1026. } else if(track->enc->width == 1440) {
  1027. av_strlcatf(compressor_name, len, " HD");
  1028. } else
  1029. av_strlcatf(compressor_name, len, " EX");
  1030. av_strlcatf(compressor_name, len, " %d%c", track->enc->height, interlaced ? 'i' : 'p');
  1031. av_strlcatf(compressor_name, len, "%d", rate * (interlaced + 1));
  1032. }
  1033. }
  1034. static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
  1035. {
  1036. int64_t pos = avio_tell(pb);
  1037. char compressor_name[32] = { 0 };
  1038. avio_wb32(pb, 0); /* size */
  1039. avio_wl32(pb, track->tag); // store it byteswapped
  1040. avio_wb32(pb, 0); /* Reserved */
  1041. avio_wb16(pb, 0); /* Reserved */
  1042. avio_wb16(pb, 1); /* Data-reference index */
  1043. avio_wb16(pb, 0); /* Codec stream version */
  1044. avio_wb16(pb, 0); /* Codec stream revision (=0) */
  1045. if (track->mode == MODE_MOV) {
  1046. ffio_wfourcc(pb, "FFMP"); /* Vendor */
  1047. if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
  1048. avio_wb32(pb, 0); /* Temporal Quality */
  1049. avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
  1050. } else {
  1051. avio_wb32(pb, 0x200); /* Temporal Quality = normal */
  1052. avio_wb32(pb, 0x200); /* Spatial Quality = normal */
  1053. }
  1054. } else {
  1055. avio_wb32(pb, 0); /* Reserved */
  1056. avio_wb32(pb, 0); /* Reserved */
  1057. avio_wb32(pb, 0); /* Reserved */
  1058. }
  1059. avio_wb16(pb, track->enc->width); /* Video width */
  1060. avio_wb16(pb, track->height); /* Video height */
  1061. avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
  1062. avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
  1063. avio_wb32(pb, 0); /* Data size (= 0) */
  1064. avio_wb16(pb, 1); /* Frame count (= 1) */
  1065. /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
  1066. find_compressor(compressor_name, 32, track);
  1067. avio_w8(pb, strlen(compressor_name));
  1068. avio_write(pb, compressor_name, 31);
  1069. if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
  1070. avio_wb16(pb, track->enc->bits_per_coded_sample);
  1071. else
  1072. avio_wb16(pb, 0x18); /* Reserved */
  1073. avio_wb16(pb, 0xffff); /* Reserved */
  1074. if (track->tag == MKTAG('m','p','4','v'))
  1075. mov_write_esds_tag(pb, track);
  1076. else if (track->enc->codec_id == AV_CODEC_ID_H263)
  1077. mov_write_d263_tag(pb);
  1078. else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
  1079. track->enc->codec_id == AV_CODEC_ID_SVQ3) {
  1080. mov_write_extradata_tag(pb, track);
  1081. avio_wb32(pb, 0);
  1082. } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
  1083. mov_write_avid_tag(pb, track);
  1084. else if (track->enc->codec_id == AV_CODEC_ID_H264) {
  1085. mov_write_avcc_tag(pb, track);
  1086. if (track->mode == MODE_IPOD)
  1087. mov_write_uuid_tag_ipod(pb);
  1088. } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
  1089. mov_write_dvc1_tag(pb, track);
  1090. else if (track->enc->codec_id == AV_CODEC_ID_VP6F ||
  1091. track->enc->codec_id == AV_CODEC_ID_VP6A) {
  1092. /* Don't write any potential extradata here - the cropping
  1093. * is signalled via the normal width/height fields. */
  1094. } else if (track->vos_len > 0)
  1095. mov_write_glbl_tag(pb, track);
  1096. if (track->enc->codec_id != AV_CODEC_ID_H264 &&
  1097. track->enc->codec_id != AV_CODEC_ID_MPEG4 &&
  1098. track->enc->codec_id != AV_CODEC_ID_DNXHD)
  1099. if (track->enc->field_order != AV_FIELD_UNKNOWN)
  1100. mov_write_fiel_tag(pb, track);
  1101. if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
  1102. track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
  1103. mov_write_pasp_tag(pb, track);
  1104. }
  1105. return update_size(pb, pos);
  1106. }
  1107. static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
  1108. {
  1109. int64_t pos = avio_tell(pb);
  1110. avio_wb32(pb, 0); /* size */
  1111. ffio_wfourcc(pb, "rtp ");
  1112. avio_wb32(pb, 0); /* Reserved */
  1113. avio_wb16(pb, 0); /* Reserved */
  1114. avio_wb16(pb, 1); /* Data-reference index */
  1115. avio_wb16(pb, 1); /* Hint track version */
  1116. avio_wb16(pb, 1); /* Highest compatible version */
  1117. avio_wb32(pb, track->max_packet_size); /* Max packet size */
  1118. avio_wb32(pb, 12); /* size */
  1119. ffio_wfourcc(pb, "tims");
  1120. avio_wb32(pb, track->timescale);
  1121. return update_size(pb, pos);
  1122. }
  1123. static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
  1124. {
  1125. int64_t pos = avio_tell(pb);
  1126. #if 1
  1127. int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
  1128. int nb_frames = 1.0/av_q2d(track->enc->time_base) + 0.5;
  1129. if (nb_frames > 255) {
  1130. av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
  1131. return AVERROR(EINVAL);
  1132. }
  1133. avio_wb32(pb, 0); /* size */
  1134. ffio_wfourcc(pb, "tmcd"); /* Data format */
  1135. avio_wb32(pb, 0); /* Reserved */
  1136. avio_wb32(pb, 1); /* Data reference index */
  1137. avio_wb32(pb, 0); /* Flags */
  1138. avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
  1139. avio_wb32(pb, track->timescale); /* Timescale */
  1140. avio_wb32(pb, frame_duration); /* Frame duration */
  1141. avio_w8(pb, nb_frames); /* Number of frames */
  1142. avio_wb24(pb, 0); /* Reserved */
  1143. /* TODO: source reference string */
  1144. #else
  1145. avio_wb32(pb, 0); /* size */
  1146. ffio_wfourcc(pb, "tmcd"); /* Data format */
  1147. avio_wb32(pb, 0); /* Reserved */
  1148. avio_wb32(pb, 1); /* Data reference index */
  1149. if (track->enc->extradata_size)
  1150. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  1151. #endif
  1152. return update_size(pb, pos);
  1153. }
  1154. static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
  1155. {
  1156. int64_t pos = avio_tell(pb);
  1157. avio_wb32(pb, 0); /* size */
  1158. ffio_wfourcc(pb, "stsd");
  1159. avio_wb32(pb, 0); /* version & flags */
  1160. avio_wb32(pb, 1); /* entry count */
  1161. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1162. mov_write_video_tag(pb, track);
  1163. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1164. mov_write_audio_tag(pb, track);
  1165. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  1166. mov_write_subtitle_tag(pb, track);
  1167. else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
  1168. mov_write_rtp_tag(pb, track);
  1169. else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
  1170. mov_write_tmcd_tag(pb, track);
  1171. return update_size(pb, pos);
  1172. }
  1173. static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
  1174. {
  1175. MOVStts *ctts_entries;
  1176. uint32_t entries = 0;
  1177. uint32_t atom_size;
  1178. int i;
  1179. ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
  1180. ctts_entries[0].count = 1;
  1181. ctts_entries[0].duration = track->cluster[0].cts;
  1182. for (i = 1; i < track->entry; i++) {
  1183. if (track->cluster[i].cts == ctts_entries[entries].duration) {
  1184. ctts_entries[entries].count++; /* compress */
  1185. } else {
  1186. entries++;
  1187. ctts_entries[entries].duration = track->cluster[i].cts;
  1188. ctts_entries[entries].count = 1;
  1189. }
  1190. }
  1191. entries++; /* last one */
  1192. atom_size = 16 + (entries * 8);
  1193. avio_wb32(pb, atom_size); /* size */
  1194. ffio_wfourcc(pb, "ctts");
  1195. avio_wb32(pb, 0); /* version & flags */
  1196. avio_wb32(pb, entries); /* entry count */
  1197. for (i = 0; i < entries; i++) {
  1198. avio_wb32(pb, ctts_entries[i].count);
  1199. avio_wb32(pb, ctts_entries[i].duration);
  1200. }
  1201. av_free(ctts_entries);
  1202. return atom_size;
  1203. }
  1204. /* Time to sample atom */
  1205. static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
  1206. {
  1207. MOVStts *stts_entries;
  1208. uint32_t entries = -1;
  1209. uint32_t atom_size;
  1210. int i;
  1211. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
  1212. stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
  1213. stts_entries[0].count = track->sample_count;
  1214. stts_entries[0].duration = 1;
  1215. entries = 1;
  1216. } else {
  1217. stts_entries = track->entry ?
  1218. av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
  1219. NULL;
  1220. for (i = 0; i < track->entry; i++) {
  1221. int duration = get_cluster_duration(track, i);
  1222. if (i && duration == stts_entries[entries].duration) {
  1223. stts_entries[entries].count++; /* compress */
  1224. } else {
  1225. entries++;
  1226. stts_entries[entries].duration = duration;
  1227. stts_entries[entries].count = 1;
  1228. }
  1229. }
  1230. entries++; /* last one */
  1231. }
  1232. atom_size = 16 + (entries * 8);
  1233. avio_wb32(pb, atom_size); /* size */
  1234. ffio_wfourcc(pb, "stts");
  1235. avio_wb32(pb, 0); /* version & flags */
  1236. avio_wb32(pb, entries); /* entry count */
  1237. for (i = 0; i < entries; i++) {
  1238. avio_wb32(pb, stts_entries[i].count);
  1239. avio_wb32(pb, stts_entries[i].duration);
  1240. }
  1241. av_free(stts_entries);
  1242. return atom_size;
  1243. }
  1244. static int mov_write_dref_tag(AVIOContext *pb)
  1245. {
  1246. avio_wb32(pb, 28); /* size */
  1247. ffio_wfourcc(pb, "dref");
  1248. avio_wb32(pb, 0); /* version & flags */
  1249. avio_wb32(pb, 1); /* entry count */
  1250. avio_wb32(pb, 0xc); /* size */
  1251. //FIXME add the alis and rsrc atom
  1252. ffio_wfourcc(pb, "url ");
  1253. avio_wb32(pb, 1); /* version & flags */
  1254. return 28;
  1255. }
  1256. static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
  1257. {
  1258. int64_t pos = avio_tell(pb);
  1259. avio_wb32(pb, 0); /* size */
  1260. ffio_wfourcc(pb, "stbl");
  1261. mov_write_stsd_tag(pb, track);
  1262. mov_write_stts_tag(pb, track);
  1263. if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1264. track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
  1265. track->has_keyframes && track->has_keyframes < track->entry)
  1266. mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
  1267. if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
  1268. mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
  1269. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  1270. track->flags & MOV_TRACK_CTTS)
  1271. mov_write_ctts_tag(pb, track);
  1272. mov_write_stsc_tag(pb, track);
  1273. mov_write_stsz_tag(pb, track);
  1274. mov_write_stco_tag(pb, track);
  1275. return update_size(pb, pos);
  1276. }
  1277. static int mov_write_dinf_tag(AVIOContext *pb)
  1278. {
  1279. int64_t pos = avio_tell(pb);
  1280. avio_wb32(pb, 0); /* size */
  1281. ffio_wfourcc(pb, "dinf");
  1282. mov_write_dref_tag(pb);
  1283. return update_size(pb, pos);
  1284. }
  1285. static int mov_write_nmhd_tag(AVIOContext *pb)
  1286. {
  1287. avio_wb32(pb, 12);
  1288. ffio_wfourcc(pb, "nmhd");
  1289. avio_wb32(pb, 0);
  1290. return 12;
  1291. }
  1292. static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
  1293. {
  1294. int64_t pos = avio_tell(pb);
  1295. const char *font = "Lucida Grande";
  1296. avio_wb32(pb, 0); /* size */
  1297. ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
  1298. avio_wb32(pb, 0); /* version & flags */
  1299. avio_wb16(pb, 0); /* text font */
  1300. avio_wb16(pb, 0); /* text face */
  1301. avio_wb16(pb, 12); /* text size */
  1302. avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
  1303. avio_wb16(pb, 0x0000); /* text color (red) */
  1304. avio_wb16(pb, 0x0000); /* text color (green) */
  1305. avio_wb16(pb, 0x0000); /* text color (blue) */
  1306. avio_wb16(pb, 0xffff); /* background color (red) */
  1307. avio_wb16(pb, 0xffff); /* background color (green) */
  1308. avio_wb16(pb, 0xffff); /* background color (blue) */
  1309. avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
  1310. avio_write(pb, font, strlen(font)); /* font name */
  1311. return update_size(pb, pos);
  1312. }
  1313. static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
  1314. {
  1315. int64_t pos = avio_tell(pb);
  1316. avio_wb32(pb, 0); /* size */
  1317. ffio_wfourcc(pb, "gmhd");
  1318. avio_wb32(pb, 0x18); /* gmin size */
  1319. ffio_wfourcc(pb, "gmin");/* generic media info */
  1320. avio_wb32(pb, 0); /* version & flags */
  1321. avio_wb16(pb, 0x40); /* graphics mode = */
  1322. avio_wb16(pb, 0x8000); /* opColor (r?) */
  1323. avio_wb16(pb, 0x8000); /* opColor (g?) */
  1324. avio_wb16(pb, 0x8000); /* opColor (b?) */
  1325. avio_wb16(pb, 0); /* balance */
  1326. avio_wb16(pb, 0); /* reserved */
  1327. /*
  1328. * This special text atom is required for
  1329. * Apple Quicktime chapters. The contents
  1330. * don't appear to be documented, so the
  1331. * bytes are copied verbatim.
  1332. */
  1333. if (track->tag != MKTAG('c','6','0','8')) {
  1334. avio_wb32(pb, 0x2C); /* size */
  1335. ffio_wfourcc(pb, "text");
  1336. avio_wb16(pb, 0x01);
  1337. avio_wb32(pb, 0x00);
  1338. avio_wb32(pb, 0x00);
  1339. avio_wb32(pb, 0x00);
  1340. avio_wb32(pb, 0x01);
  1341. avio_wb32(pb, 0x00);
  1342. avio_wb32(pb, 0x00);
  1343. avio_wb32(pb, 0x00);
  1344. avio_wb32(pb, 0x00004000);
  1345. avio_wb16(pb, 0x0000);
  1346. }
  1347. if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
  1348. int64_t tmcd_pos = avio_tell(pb);
  1349. avio_wb32(pb, 0); /* size */
  1350. ffio_wfourcc(pb, "tmcd");
  1351. mov_write_tcmi_tag(pb, track);
  1352. update_size(pb, tmcd_pos);
  1353. }
  1354. return update_size(pb, pos);
  1355. }
  1356. static int mov_write_smhd_tag(AVIOContext *pb)
  1357. {
  1358. avio_wb32(pb, 16); /* size */
  1359. ffio_wfourcc(pb, "smhd");
  1360. avio_wb32(pb, 0); /* version & flags */
  1361. avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
  1362. avio_wb16(pb, 0); /* reserved */
  1363. return 16;
  1364. }
  1365. static int mov_write_vmhd_tag(AVIOContext *pb)
  1366. {
  1367. avio_wb32(pb, 0x14); /* size (always 0x14) */
  1368. ffio_wfourcc(pb, "vmhd");
  1369. avio_wb32(pb, 0x01); /* version & flags */
  1370. avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
  1371. return 0x14;
  1372. }
  1373. static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
  1374. {
  1375. const char *hdlr, *descr = NULL, *hdlr_type = NULL;
  1376. int64_t pos = avio_tell(pb);
  1377. hdlr = "dhlr";
  1378. hdlr_type = "url ";
  1379. descr = "DataHandler";
  1380. if (track) {
  1381. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  1382. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1383. hdlr_type = "vide";
  1384. descr = "VideoHandler";
  1385. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  1386. hdlr_type = "soun";
  1387. descr = "SoundHandler";
  1388. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1389. if (track->tag == MKTAG('c','6','0','8')) {
  1390. hdlr_type = "clcp";
  1391. descr = "ClosedCaptionHandler";
  1392. } else {
  1393. if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
  1394. else hdlr_type = "text";
  1395. descr = "SubtitleHandler";
  1396. }
  1397. } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
  1398. hdlr_type = "hint";
  1399. descr = "HintHandler";
  1400. } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
  1401. hdlr_type = "tmcd";
  1402. descr = "TimeCodeHandler";
  1403. } else {
  1404. char tag_buf[32];
  1405. av_get_codec_tag_string(tag_buf, sizeof(tag_buf),
  1406. track->enc->codec_tag);
  1407. av_log(track->enc, AV_LOG_WARNING,
  1408. "Unknown hldr_type for %s / 0x%04X, writing dummy values\n",
  1409. tag_buf, track->enc->codec_tag);
  1410. }
  1411. }
  1412. avio_wb32(pb, 0); /* size */
  1413. ffio_wfourcc(pb, "hdlr");
  1414. avio_wb32(pb, 0); /* Version & flags */
  1415. avio_write(pb, hdlr, 4); /* handler */
  1416. ffio_wfourcc(pb, hdlr_type); /* handler type */
  1417. avio_wb32(pb, 0); /* reserved */
  1418. avio_wb32(pb, 0); /* reserved */
  1419. avio_wb32(pb, 0); /* reserved */
  1420. if (!track || track->mode == MODE_MOV)
  1421. avio_w8(pb, strlen(descr)); /* pascal string */
  1422. avio_write(pb, descr, strlen(descr)); /* handler description */
  1423. if (track && track->mode != MODE_MOV)
  1424. avio_w8(pb, 0); /* c string */
  1425. return update_size(pb, pos);
  1426. }
  1427. static int mov_write_hmhd_tag(AVIOContext *pb)
  1428. {
  1429. /* This atom must be present, but leaving the values at zero
  1430. * seems harmless. */
  1431. avio_wb32(pb, 28); /* size */
  1432. ffio_wfourcc(pb, "hmhd");
  1433. avio_wb32(pb, 0); /* version, flags */
  1434. avio_wb16(pb, 0); /* maxPDUsize */
  1435. avio_wb16(pb, 0); /* avgPDUsize */
  1436. avio_wb32(pb, 0); /* maxbitrate */
  1437. avio_wb32(pb, 0); /* avgbitrate */
  1438. avio_wb32(pb, 0); /* reserved */
  1439. return 28;
  1440. }
  1441. static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
  1442. {
  1443. int64_t pos = avio_tell(pb);
  1444. avio_wb32(pb, 0); /* size */
  1445. ffio_wfourcc(pb, "minf");
  1446. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1447. mov_write_vmhd_tag(pb);
  1448. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1449. mov_write_smhd_tag(pb);
  1450. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1451. if (track->tag == MKTAG('t','e','x','t') || track->tag == MKTAG('c','6','0','8')) {
  1452. mov_write_gmhd_tag(pb, track);
  1453. } else {
  1454. mov_write_nmhd_tag(pb);
  1455. }
  1456. } else if (track->tag == MKTAG('r','t','p',' ')) {
  1457. mov_write_hmhd_tag(pb);
  1458. } else if (track->tag == MKTAG('t','m','c','d')) {
  1459. mov_write_gmhd_tag(pb, track);
  1460. }
  1461. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  1462. mov_write_hdlr_tag(pb, NULL);
  1463. mov_write_dinf_tag(pb);
  1464. mov_write_stbl_tag(pb, track);
  1465. return update_size(pb, pos);
  1466. }
  1467. static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
  1468. {
  1469. int version = track->track_duration < INT32_MAX ? 0 : 1;
  1470. if (track->mode == MODE_ISM)
  1471. version = 1;
  1472. (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
  1473. ffio_wfourcc(pb, "mdhd");
  1474. avio_w8(pb, version);
  1475. avio_wb24(pb, 0); /* flags */
  1476. if (version == 1) {
  1477. avio_wb64(pb, track->time);
  1478. avio_wb64(pb, track->time);
  1479. } else {
  1480. avio_wb32(pb, track->time); /* creation time */
  1481. avio_wb32(pb, track->time); /* modification time */
  1482. }
  1483. avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
  1484. if (!track->entry)
  1485. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  1486. else
  1487. (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
  1488. avio_wb16(pb, track->language); /* language */
  1489. avio_wb16(pb, 0); /* reserved (quality) */
  1490. if (version != 0 && track->mode == MODE_MOV) {
  1491. av_log(NULL, AV_LOG_ERROR,
  1492. "FATAL error, file duration too long for timebase, this file will not be\n"
  1493. "playable with quicktime. Choose a different timebase or a different\n"
  1494. "container format\n");
  1495. }
  1496. return 32;
  1497. }
  1498. static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
  1499. {
  1500. int64_t pos = avio_tell(pb);
  1501. avio_wb32(pb, 0); /* size */
  1502. ffio_wfourcc(pb, "mdia");
  1503. mov_write_mdhd_tag(pb, track);
  1504. mov_write_hdlr_tag(pb, track);
  1505. mov_write_minf_tag(pb, track);
  1506. return update_size(pb, pos);
  1507. }
  1508. /* transformation matrix
  1509. |a b u|
  1510. |c d v|
  1511. |tx ty w| */
  1512. static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
  1513. int16_t d, int16_t tx, int16_t ty)
  1514. {
  1515. avio_wb32(pb, a << 16); /* 16.16 format */
  1516. avio_wb32(pb, b << 16); /* 16.16 format */
  1517. avio_wb32(pb, 0); /* u in 2.30 format */
  1518. avio_wb32(pb, c << 16); /* 16.16 format */
  1519. avio_wb32(pb, d << 16); /* 16.16 format */
  1520. avio_wb32(pb, 0); /* v in 2.30 format */
  1521. avio_wb32(pb, tx << 16); /* 16.16 format */
  1522. avio_wb32(pb, ty << 16); /* 16.16 format */
  1523. avio_wb32(pb, 1 << 30); /* w in 2.30 format */
  1524. }
  1525. static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
  1526. {
  1527. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1528. track->timescale, AV_ROUND_UP);
  1529. int version = duration < INT32_MAX ? 0 : 1;
  1530. int rotation = 0;
  1531. if (track->mode == MODE_ISM)
  1532. version = 1;
  1533. (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
  1534. ffio_wfourcc(pb, "tkhd");
  1535. avio_w8(pb, version);
  1536. avio_wb24(pb, (track->flags & MOV_TRACK_ENABLED) ?
  1537. MOV_TKHD_FLAG_ENABLED | MOV_TKHD_FLAG_IN_MOVIE :
  1538. MOV_TKHD_FLAG_IN_MOVIE);
  1539. if (version == 1) {
  1540. avio_wb64(pb, track->time);
  1541. avio_wb64(pb, track->time);
  1542. } else {
  1543. avio_wb32(pb, track->time); /* creation time */
  1544. avio_wb32(pb, track->time); /* modification time */
  1545. }
  1546. avio_wb32(pb, track->track_id); /* track-id */
  1547. avio_wb32(pb, 0); /* reserved */
  1548. if (!track->entry)
  1549. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  1550. else
  1551. (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
  1552. avio_wb32(pb, 0); /* reserved */
  1553. avio_wb32(pb, 0); /* reserved */
  1554. avio_wb16(pb, 0); /* layer */
  1555. avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
  1556. /* Volume, only for audio */
  1557. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1558. avio_wb16(pb, 0x0100);
  1559. else
  1560. avio_wb16(pb, 0);
  1561. avio_wb16(pb, 0); /* reserved */
  1562. /* Matrix structure */
  1563. if (st && st->metadata) {
  1564. AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
  1565. rotation = (rot && rot->value) ? atoi(rot->value) : 0;
  1566. }
  1567. if (rotation == 90) {
  1568. write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
  1569. } else if (rotation == 180) {
  1570. write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
  1571. } else if (rotation == 270) {
  1572. write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
  1573. } else {
  1574. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  1575. }
  1576. /* Track width and height, for visual only */
  1577. if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1578. track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
  1579. if (track->mode == MODE_MOV) {
  1580. avio_wb32(pb, track->enc->width << 16);
  1581. avio_wb32(pb, track->height << 16);
  1582. } else {
  1583. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1584. if (!sample_aspect_ratio || track->height != track->enc->height)
  1585. sample_aspect_ratio = 1;
  1586. avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
  1587. avio_wb32(pb, track->height * 0x10000);
  1588. }
  1589. } else {
  1590. avio_wb32(pb, 0);
  1591. avio_wb32(pb, 0);
  1592. }
  1593. return 0x5c;
  1594. }
  1595. static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
  1596. {
  1597. int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
  1598. track->enc->sample_aspect_ratio.den);
  1599. int64_t pos = avio_tell(pb);
  1600. avio_wb32(pb, 0); /* size */
  1601. ffio_wfourcc(pb, "tapt");
  1602. avio_wb32(pb, 20);
  1603. ffio_wfourcc(pb, "clef");
  1604. avio_wb32(pb, 0);
  1605. avio_wb32(pb, width << 16);
  1606. avio_wb32(pb, track->enc->height << 16);
  1607. avio_wb32(pb, 20);
  1608. ffio_wfourcc(pb, "prof");
  1609. avio_wb32(pb, 0);
  1610. avio_wb32(pb, width << 16);
  1611. avio_wb32(pb, track->enc->height << 16);
  1612. avio_wb32(pb, 20);
  1613. ffio_wfourcc(pb, "enof");
  1614. avio_wb32(pb, 0);
  1615. avio_wb32(pb, track->enc->width << 16);
  1616. avio_wb32(pb, track->enc->height << 16);
  1617. return update_size(pb, pos);
  1618. }
  1619. // This box seems important for the psp playback ... without it the movie seems to hang
  1620. static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
  1621. {
  1622. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1623. track->timescale, AV_ROUND_UP);
  1624. int version = duration < INT32_MAX ? 0 : 1;
  1625. int entry_size, entry_count, size;
  1626. int64_t delay, start_ct = track->cluster[0].cts;
  1627. delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
  1628. track->timescale, AV_ROUND_DOWN);
  1629. version |= delay < INT32_MAX ? 0 : 1;
  1630. entry_size = (version == 1) ? 20 : 12;
  1631. entry_count = 1 + (delay > 0);
  1632. size = 24 + entry_count * entry_size;
  1633. /* write the atom data */
  1634. avio_wb32(pb, size);
  1635. ffio_wfourcc(pb, "edts");
  1636. avio_wb32(pb, size - 8);
  1637. ffio_wfourcc(pb, "elst");
  1638. avio_w8(pb, version);
  1639. avio_wb24(pb, 0); /* flags */
  1640. avio_wb32(pb, entry_count);
  1641. if (delay > 0) { /* add an empty edit to delay presentation */
  1642. if (version == 1) {
  1643. avio_wb64(pb, delay);
  1644. avio_wb64(pb, -1);
  1645. } else {
  1646. avio_wb32(pb, delay);
  1647. avio_wb32(pb, -1);
  1648. }
  1649. avio_wb32(pb, 0x00010000);
  1650. } else {
  1651. av_assert0(av_rescale_rnd(track->cluster[0].dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
  1652. start_ct = -FFMIN(track->cluster[0].dts, 0); //FFMIN needed due to rounding
  1653. duration += delay;
  1654. }
  1655. /* duration */
  1656. if (version == 1) {
  1657. avio_wb64(pb, duration);
  1658. avio_wb64(pb, start_ct);
  1659. } else {
  1660. avio_wb32(pb, duration);
  1661. avio_wb32(pb, start_ct);
  1662. }
  1663. avio_wb32(pb, 0x00010000);
  1664. return size;
  1665. }
  1666. static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
  1667. {
  1668. avio_wb32(pb, 20); // size
  1669. ffio_wfourcc(pb, "tref");
  1670. avio_wb32(pb, 12); // size (subatom)
  1671. avio_wl32(pb, track->tref_tag);
  1672. avio_wb32(pb, track->tref_id);
  1673. return 20;
  1674. }
  1675. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  1676. static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
  1677. {
  1678. avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  1679. ffio_wfourcc(pb, "uuid");
  1680. ffio_wfourcc(pb, "USMT");
  1681. avio_wb32(pb, 0x21d24fce);
  1682. avio_wb32(pb, 0xbb88695c);
  1683. avio_wb32(pb, 0xfac9c740);
  1684. avio_wb32(pb, 0x1c); // another size here!
  1685. ffio_wfourcc(pb, "MTDT");
  1686. avio_wb32(pb, 0x00010012);
  1687. avio_wb32(pb, 0x0a);
  1688. avio_wb32(pb, 0x55c40000);
  1689. avio_wb32(pb, 0x1);
  1690. avio_wb32(pb, 0x0);
  1691. return 0x34;
  1692. }
  1693. static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
  1694. {
  1695. AVFormatContext *ctx = track->rtp_ctx;
  1696. char buf[1000] = "";
  1697. int len;
  1698. ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
  1699. NULL, NULL, 0, 0, ctx);
  1700. av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
  1701. len = strlen(buf);
  1702. avio_wb32(pb, len + 24);
  1703. ffio_wfourcc(pb, "udta");
  1704. avio_wb32(pb, len + 16);
  1705. ffio_wfourcc(pb, "hnti");
  1706. avio_wb32(pb, len + 8);
  1707. ffio_wfourcc(pb, "sdp ");
  1708. avio_write(pb, buf, len);
  1709. return len + 24;
  1710. }
  1711. static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
  1712. MOVTrack *track, AVStream *st)
  1713. {
  1714. int64_t pos = avio_tell(pb);
  1715. avio_wb32(pb, 0); /* size */
  1716. ffio_wfourcc(pb, "trak");
  1717. mov_write_tkhd_tag(pb, track, st);
  1718. if (supports_edts(mov))
  1719. mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
  1720. if (track->tref_tag)
  1721. mov_write_tref_tag(pb, track);
  1722. mov_write_mdia_tag(pb, track);
  1723. if (track->mode == MODE_PSP)
  1724. mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
  1725. if (track->tag == MKTAG('r','t','p',' '))
  1726. mov_write_udta_sdp(pb, track);
  1727. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
  1728. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1729. if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
  1730. mov_write_tapt_tag(pb, track);
  1731. }
  1732. return update_size(pb, pos);
  1733. }
  1734. static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
  1735. {
  1736. int i, has_audio = 0, has_video = 0;
  1737. int64_t pos = avio_tell(pb);
  1738. int audio_profile = mov->iods_audio_profile;
  1739. int video_profile = mov->iods_video_profile;
  1740. for (i = 0; i < mov->nb_streams; i++) {
  1741. if (mov->tracks[i].entry > 0) {
  1742. has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
  1743. has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
  1744. }
  1745. }
  1746. if (audio_profile < 0)
  1747. audio_profile = 0xFF - has_audio;
  1748. if (video_profile < 0)
  1749. video_profile = 0xFF - has_video;
  1750. avio_wb32(pb, 0x0); /* size */
  1751. ffio_wfourcc(pb, "iods");
  1752. avio_wb32(pb, 0); /* version & flags */
  1753. put_descr(pb, 0x10, 7);
  1754. avio_wb16(pb, 0x004f);
  1755. avio_w8(pb, 0xff);
  1756. avio_w8(pb, 0xff);
  1757. avio_w8(pb, audio_profile);
  1758. avio_w8(pb, video_profile);
  1759. avio_w8(pb, 0xff);
  1760. return update_size(pb, pos);
  1761. }
  1762. static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
  1763. {
  1764. avio_wb32(pb, 0x20); /* size */
  1765. ffio_wfourcc(pb, "trex");
  1766. avio_wb32(pb, 0); /* version & flags */
  1767. avio_wb32(pb, track->track_id); /* track ID */
  1768. avio_wb32(pb, 1); /* default sample description index */
  1769. avio_wb32(pb, 0); /* default sample duration */
  1770. avio_wb32(pb, 0); /* default sample size */
  1771. avio_wb32(pb, 0); /* default sample flags */
  1772. return 0;
  1773. }
  1774. static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
  1775. {
  1776. int64_t pos = avio_tell(pb);
  1777. int i;
  1778. avio_wb32(pb, 0x0); /* size */
  1779. ffio_wfourcc(pb, "mvex");
  1780. for (i = 0; i < mov->nb_streams; i++)
  1781. mov_write_trex_tag(pb, &mov->tracks[i]);
  1782. return update_size(pb, pos);
  1783. }
  1784. static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  1785. {
  1786. int max_track_id = 1, i;
  1787. int64_t max_track_len_temp, max_track_len = 0;
  1788. int version;
  1789. for (i = 0; i < mov->nb_streams; i++) {
  1790. if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
  1791. max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
  1792. MOV_TIMESCALE,
  1793. mov->tracks[i].timescale,
  1794. AV_ROUND_UP);
  1795. if (max_track_len < max_track_len_temp)
  1796. max_track_len = max_track_len_temp;
  1797. if (max_track_id < mov->tracks[i].track_id)
  1798. max_track_id = mov->tracks[i].track_id;
  1799. }
  1800. }
  1801. version = max_track_len < UINT32_MAX ? 0 : 1;
  1802. (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
  1803. ffio_wfourcc(pb, "mvhd");
  1804. avio_w8(pb, version);
  1805. avio_wb24(pb, 0); /* flags */
  1806. if (version == 1) {
  1807. avio_wb64(pb, mov->time);
  1808. avio_wb64(pb, mov->time);
  1809. } else {
  1810. avio_wb32(pb, mov->time); /* creation time */
  1811. avio_wb32(pb, mov->time); /* modification time */
  1812. }
  1813. avio_wb32(pb, MOV_TIMESCALE);
  1814. (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
  1815. avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  1816. avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  1817. avio_wb16(pb, 0); /* reserved */
  1818. avio_wb32(pb, 0); /* reserved */
  1819. avio_wb32(pb, 0); /* reserved */
  1820. /* Matrix structure */
  1821. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  1822. avio_wb32(pb, 0); /* reserved (preview time) */
  1823. avio_wb32(pb, 0); /* reserved (preview duration) */
  1824. avio_wb32(pb, 0); /* reserved (poster time) */
  1825. avio_wb32(pb, 0); /* reserved (selection time) */
  1826. avio_wb32(pb, 0); /* reserved (selection duration) */
  1827. avio_wb32(pb, 0); /* reserved (current time) */
  1828. avio_wb32(pb, max_track_id + 1); /* Next track id */
  1829. return 0x6c;
  1830. }
  1831. static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
  1832. AVFormatContext *s)
  1833. {
  1834. avio_wb32(pb, 33); /* size */
  1835. ffio_wfourcc(pb, "hdlr");
  1836. avio_wb32(pb, 0);
  1837. avio_wb32(pb, 0);
  1838. ffio_wfourcc(pb, "mdir");
  1839. ffio_wfourcc(pb, "appl");
  1840. avio_wb32(pb, 0);
  1841. avio_wb32(pb, 0);
  1842. avio_w8(pb, 0);
  1843. return 33;
  1844. }
  1845. /* helper function to write a data tag with the specified string as data */
  1846. static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
  1847. {
  1848. if (long_style) {
  1849. int size = 16 + strlen(data);
  1850. avio_wb32(pb, size); /* size */
  1851. ffio_wfourcc(pb, "data");
  1852. avio_wb32(pb, 1);
  1853. avio_wb32(pb, 0);
  1854. avio_write(pb, data, strlen(data));
  1855. return size;
  1856. } else {
  1857. if (!lang)
  1858. lang = ff_mov_iso639_to_lang("und", 1);
  1859. avio_wb16(pb, strlen(data)); /* string length */
  1860. avio_wb16(pb, lang);
  1861. avio_write(pb, data, strlen(data));
  1862. return strlen(data) + 4;
  1863. }
  1864. }
  1865. static int mov_write_string_tag(AVIOContext *pb, const char *name,
  1866. const char *value, int lang, int long_style)
  1867. {
  1868. int size = 0;
  1869. if (value && value[0]) {
  1870. int64_t pos = avio_tell(pb);
  1871. avio_wb32(pb, 0); /* size */
  1872. ffio_wfourcc(pb, name);
  1873. mov_write_string_data_tag(pb, value, lang, long_style);
  1874. size = update_size(pb, pos);
  1875. }
  1876. return size;
  1877. }
  1878. static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
  1879. const char *name, const char *tag,
  1880. int long_style)
  1881. {
  1882. int l, lang = 0, len, len2;
  1883. AVDictionaryEntry *t, *t2 = NULL;
  1884. char tag2[16];
  1885. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  1886. return 0;
  1887. len = strlen(t->key);
  1888. snprintf(tag2, sizeof(tag2), "%s-", tag);
  1889. while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
  1890. len2 = strlen(t2->key);
  1891. if (len2 == len + 4 && !strcmp(t->value, t2->value)
  1892. && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
  1893. lang = l;
  1894. break;
  1895. }
  1896. }
  1897. return mov_write_string_tag(pb, name, t->value, lang, long_style);
  1898. }
  1899. /* iTunes bpm number */
  1900. static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
  1901. {
  1902. AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
  1903. int size = 0, tmpo = t ? atoi(t->value) : 0;
  1904. if (tmpo) {
  1905. size = 26;
  1906. avio_wb32(pb, size);
  1907. ffio_wfourcc(pb, "tmpo");
  1908. avio_wb32(pb, size-8); /* size */
  1909. ffio_wfourcc(pb, "data");
  1910. avio_wb32(pb, 0x15); //type specifier
  1911. avio_wb32(pb, 0);
  1912. avio_wb16(pb, tmpo); // data
  1913. }
  1914. return size;
  1915. }
  1916. /* iTunes track or disc number */
  1917. static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
  1918. AVFormatContext *s, int disc)
  1919. {
  1920. AVDictionaryEntry *t = av_dict_get(s->metadata,
  1921. disc ? "disc" : "track",
  1922. NULL, 0);
  1923. int size = 0, track = t ? atoi(t->value) : 0;
  1924. if (track) {
  1925. int tracks = 0;
  1926. char *slash = strchr(t->value, '/');
  1927. if (slash)
  1928. tracks = atoi(slash + 1);
  1929. avio_wb32(pb, 32); /* size */
  1930. ffio_wfourcc(pb, disc ? "disk" : "trkn");
  1931. avio_wb32(pb, 24); /* size */
  1932. ffio_wfourcc(pb, "data");
  1933. avio_wb32(pb, 0); // 8 bytes empty
  1934. avio_wb32(pb, 0);
  1935. avio_wb16(pb, 0); // empty
  1936. avio_wb16(pb, track); // track / disc number
  1937. avio_wb16(pb, tracks); // total track / disc number
  1938. avio_wb16(pb, 0); // empty
  1939. size = 32;
  1940. }
  1941. return size;
  1942. }
  1943. static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
  1944. const char *name, const char *tag,
  1945. int len)
  1946. {
  1947. AVDictionaryEntry *t = NULL;
  1948. uint8_t num;
  1949. int size = 24 + len;
  1950. if (len != 1 && len != 4)
  1951. return -1;
  1952. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  1953. return 0;
  1954. num = atoi(t->value);
  1955. avio_wb32(pb, size);
  1956. ffio_wfourcc(pb, name);
  1957. avio_wb32(pb, size - 8);
  1958. ffio_wfourcc(pb, "data");
  1959. avio_wb32(pb, 0x15);
  1960. avio_wb32(pb, 0);
  1961. if (len==4) avio_wb32(pb, num);
  1962. else avio_w8 (pb, num);
  1963. return size;
  1964. }
  1965. /* iTunes meta data list */
  1966. static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
  1967. AVFormatContext *s)
  1968. {
  1969. int64_t pos = avio_tell(pb);
  1970. avio_wb32(pb, 0); /* size */
  1971. ffio_wfourcc(pb, "ilst");
  1972. mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
  1973. mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
  1974. mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
  1975. mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
  1976. mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
  1977. mov_write_string_metadata(s, pb, "\251day", "date" , 1);
  1978. mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
  1979. mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
  1980. mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
  1981. mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
  1982. mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
  1983. mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
  1984. mov_write_string_metadata(s, pb, "desc", "description",1);
  1985. mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
  1986. mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
  1987. mov_write_string_metadata(s, pb, "tven", "episode_id",1);
  1988. mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
  1989. mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
  1990. mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
  1991. mov_write_int8_metadata (s, pb, "stik", "media_type",1);
  1992. mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
  1993. mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
  1994. mov_write_trkn_tag(pb, mov, s, 0); // track number
  1995. mov_write_trkn_tag(pb, mov, s, 1); // disc number
  1996. mov_write_tmpo_tag(pb, s);
  1997. return update_size(pb, pos);
  1998. }
  1999. /* iTunes meta data tag */
  2000. static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
  2001. AVFormatContext *s)
  2002. {
  2003. int size = 0;
  2004. int64_t pos = avio_tell(pb);
  2005. avio_wb32(pb, 0); /* size */
  2006. ffio_wfourcc(pb, "meta");
  2007. avio_wb32(pb, 0);
  2008. mov_write_itunes_hdlr_tag(pb, mov, s);
  2009. mov_write_ilst_tag(pb, mov, s);
  2010. size = update_size(pb, pos);
  2011. return size;
  2012. }
  2013. static int utf8len(const uint8_t *b)
  2014. {
  2015. int len = 0;
  2016. int val;
  2017. while (*b) {
  2018. GET_UTF8(val, *b++, return -1;)
  2019. len++;
  2020. }
  2021. return len;
  2022. }
  2023. static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
  2024. {
  2025. int val;
  2026. while (*b) {
  2027. GET_UTF8(val, *b++, return -1;)
  2028. avio_wb16(pb, val);
  2029. }
  2030. avio_wb16(pb, 0x00);
  2031. return 0;
  2032. }
  2033. static uint16_t language_code(const char *str)
  2034. {
  2035. return (((str[0] - 0x60) & 0x1F) << 10) +
  2036. (((str[1] - 0x60) & 0x1F) << 5) +
  2037. (( str[2] - 0x60) & 0x1F);
  2038. }
  2039. static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
  2040. const char *tag, const char *str)
  2041. {
  2042. int64_t pos = avio_tell(pb);
  2043. AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
  2044. if (!t || !utf8len(t->value))
  2045. return 0;
  2046. avio_wb32(pb, 0); /* size */
  2047. ffio_wfourcc(pb, tag); /* type */
  2048. avio_wb32(pb, 0); /* version + flags */
  2049. if (!strcmp(tag, "yrrc"))
  2050. avio_wb16(pb, atoi(t->value));
  2051. else {
  2052. avio_wb16(pb, language_code("eng")); /* language */
  2053. avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
  2054. if (!strcmp(tag, "albm") &&
  2055. (t = av_dict_get(s->metadata, "track", NULL, 0)))
  2056. avio_w8(pb, atoi(t->value));
  2057. }
  2058. return update_size(pb, pos);
  2059. }
  2060. static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
  2061. {
  2062. int64_t pos = avio_tell(pb);
  2063. int i, nb_chapters = FFMIN(s->nb_chapters, 255);
  2064. avio_wb32(pb, 0); // size
  2065. ffio_wfourcc(pb, "chpl");
  2066. avio_wb32(pb, 0x01000000); // version + flags
  2067. avio_wb32(pb, 0); // unknown
  2068. avio_w8(pb, nb_chapters);
  2069. for (i = 0; i < nb_chapters; i++) {
  2070. AVChapter *c = s->chapters[i];
  2071. AVDictionaryEntry *t;
  2072. avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
  2073. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  2074. int len = FFMIN(strlen(t->value), 255);
  2075. avio_w8(pb, len);
  2076. avio_write(pb, t->value, len);
  2077. } else
  2078. avio_w8(pb, 0);
  2079. }
  2080. return update_size(pb, pos);
  2081. }
  2082. static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
  2083. AVFormatContext *s)
  2084. {
  2085. AVIOContext *pb_buf;
  2086. int i, ret, size;
  2087. uint8_t *buf;
  2088. for (i = 0; i < s->nb_streams; i++)
  2089. if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
  2090. return 0;
  2091. }
  2092. ret = avio_open_dyn_buf(&pb_buf);
  2093. if (ret < 0)
  2094. return ret;
  2095. if (mov->mode & MODE_3GP) {
  2096. mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
  2097. mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
  2098. mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
  2099. mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
  2100. mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
  2101. mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
  2102. mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
  2103. mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
  2104. } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
  2105. mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
  2106. mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
  2107. mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
  2108. mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
  2109. mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
  2110. mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
  2111. // currently ignored by mov.c
  2112. mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
  2113. // add support for libquicktime, this atom is also actually read by mov.c
  2114. mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
  2115. mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
  2116. mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
  2117. } else {
  2118. /* iTunes meta data */
  2119. mov_write_meta_tag(pb_buf, mov, s);
  2120. }
  2121. if (s->nb_chapters)
  2122. mov_write_chpl_tag(pb_buf, s);
  2123. if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
  2124. avio_wb32(pb, size + 8);
  2125. ffio_wfourcc(pb, "udta");
  2126. avio_write(pb, buf, size);
  2127. }
  2128. av_free(buf);
  2129. return 0;
  2130. }
  2131. static void mov_write_psp_udta_tag(AVIOContext *pb,
  2132. const char *str, const char *lang, int type)
  2133. {
  2134. int len = utf8len(str) + 1;
  2135. if (len <= 0)
  2136. return;
  2137. avio_wb16(pb, len * 2 + 10); /* size */
  2138. avio_wb32(pb, type); /* type */
  2139. avio_wb16(pb, language_code(lang)); /* language */
  2140. avio_wb16(pb, 0x01); /* ? */
  2141. ascii_to_wc(pb, str);
  2142. }
  2143. static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
  2144. {
  2145. AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
  2146. int64_t pos, pos2;
  2147. if (title) {
  2148. pos = avio_tell(pb);
  2149. avio_wb32(pb, 0); /* size placeholder*/
  2150. ffio_wfourcc(pb, "uuid");
  2151. ffio_wfourcc(pb, "USMT");
  2152. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2153. avio_wb32(pb, 0xbb88695c);
  2154. avio_wb32(pb, 0xfac9c740);
  2155. pos2 = avio_tell(pb);
  2156. avio_wb32(pb, 0); /* size placeholder*/
  2157. ffio_wfourcc(pb, "MTDT");
  2158. avio_wb16(pb, 4);
  2159. // ?
  2160. avio_wb16(pb, 0x0C); /* size */
  2161. avio_wb32(pb, 0x0B); /* type */
  2162. avio_wb16(pb, language_code("und")); /* language */
  2163. avio_wb16(pb, 0x0); /* ? */
  2164. avio_wb16(pb, 0x021C); /* data */
  2165. mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
  2166. mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
  2167. mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
  2168. update_size(pb, pos2);
  2169. return update_size(pb, pos);
  2170. }
  2171. return 0;
  2172. }
  2173. static void build_chunks(MOVTrack *trk)
  2174. {
  2175. int i;
  2176. MOVIentry *chunk = &trk->cluster[0];
  2177. uint64_t chunkSize = chunk->size;
  2178. chunk->chunkNum = 1;
  2179. if (trk->chunkCount)
  2180. return;
  2181. trk->chunkCount = 1;
  2182. for (i = 1; i<trk->entry; i++){
  2183. if (chunk->pos + chunkSize == trk->cluster[i].pos &&
  2184. chunkSize + trk->cluster[i].size < (1<<20)){
  2185. chunkSize += trk->cluster[i].size;
  2186. chunk->samples_in_chunk += trk->cluster[i].entries;
  2187. } else {
  2188. trk->cluster[i].chunkNum = chunk->chunkNum+1;
  2189. chunk=&trk->cluster[i];
  2190. chunkSize = chunk->size;
  2191. trk->chunkCount++;
  2192. }
  2193. }
  2194. }
  2195. static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
  2196. AVFormatContext *s)
  2197. {
  2198. int i;
  2199. int64_t pos = avio_tell(pb);
  2200. avio_wb32(pb, 0); /* size placeholder*/
  2201. ffio_wfourcc(pb, "moov");
  2202. for (i = 0; i < mov->nb_streams; i++) {
  2203. if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2204. continue;
  2205. mov->tracks[i].time = mov->time;
  2206. mov->tracks[i].track_id = i + 1;
  2207. if (mov->tracks[i].entry)
  2208. build_chunks(&mov->tracks[i]);
  2209. }
  2210. if (mov->chapter_track)
  2211. for (i = 0; i < s->nb_streams; i++) {
  2212. mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
  2213. mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
  2214. }
  2215. for (i = 0; i < mov->nb_streams; i++) {
  2216. if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
  2217. mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
  2218. mov->tracks[i].tref_id =
  2219. mov->tracks[mov->tracks[i].src_track].track_id;
  2220. }
  2221. }
  2222. for (i = 0; i < mov->nb_streams; i++) {
  2223. if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
  2224. int src_trk = mov->tracks[i].src_track;
  2225. mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
  2226. mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
  2227. mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
  2228. }
  2229. }
  2230. mov_write_mvhd_tag(pb, mov);
  2231. if (mov->mode != MODE_MOV && !mov->iods_skip)
  2232. mov_write_iods_tag(pb, mov);
  2233. for (i = 0; i < mov->nb_streams; i++) {
  2234. if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2235. mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
  2236. }
  2237. }
  2238. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  2239. mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
  2240. if (mov->mode == MODE_PSP)
  2241. mov_write_uuidusmt_tag(pb, s);
  2242. else
  2243. mov_write_udta_tag(pb, mov, s);
  2244. return update_size(pb, pos);
  2245. }
  2246. static void param_write_int(AVIOContext *pb, const char *name, int value)
  2247. {
  2248. avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
  2249. }
  2250. static void param_write_string(AVIOContext *pb, const char *name, const char *value)
  2251. {
  2252. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
  2253. }
  2254. static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
  2255. {
  2256. char buf[150];
  2257. len = FFMIN(sizeof(buf) / 2 - 1, len);
  2258. ff_data_to_hex(buf, value, len, 0);
  2259. buf[2 * len] = '\0';
  2260. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
  2261. }
  2262. static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
  2263. {
  2264. int64_t pos = avio_tell(pb);
  2265. int i;
  2266. static const uint8_t uuid[] = {
  2267. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  2268. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  2269. };
  2270. avio_wb32(pb, 0);
  2271. ffio_wfourcc(pb, "uuid");
  2272. avio_write(pb, uuid, sizeof(uuid));
  2273. avio_wb32(pb, 0);
  2274. avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  2275. avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  2276. avio_printf(pb, "<head>\n");
  2277. avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
  2278. LIBAVFORMAT_IDENT);
  2279. avio_printf(pb, "</head>\n");
  2280. avio_printf(pb, "<body>\n");
  2281. avio_printf(pb, "<switch>\n");
  2282. for (i = 0; i < mov->nb_streams; i++) {
  2283. MOVTrack *track = &mov->tracks[i];
  2284. const char *type;
  2285. /* track->track_id is initialized in write_moov, and thus isn't known
  2286. * here yet */
  2287. int track_id = i + 1;
  2288. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2289. type = "video";
  2290. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  2291. type = "audio";
  2292. } else {
  2293. continue;
  2294. }
  2295. avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
  2296. track->enc->bit_rate);
  2297. param_write_int(pb, "systemBitrate", track->enc->bit_rate);
  2298. param_write_int(pb, "trackID", track_id);
  2299. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2300. if (track->enc->codec_id == AV_CODEC_ID_H264) {
  2301. uint8_t *ptr;
  2302. int size = track->enc->extradata_size;
  2303. if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
  2304. &size)) {
  2305. param_write_hex(pb, "CodecPrivateData",
  2306. ptr ? ptr : track->enc->extradata,
  2307. size);
  2308. av_free(ptr);
  2309. }
  2310. param_write_string(pb, "FourCC", "H264");
  2311. } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
  2312. param_write_string(pb, "FourCC", "WVC1");
  2313. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2314. track->enc->extradata_size);
  2315. }
  2316. param_write_int(pb, "MaxWidth", track->enc->width);
  2317. param_write_int(pb, "MaxHeight", track->enc->height);
  2318. param_write_int(pb, "DisplayWidth", track->enc->width);
  2319. param_write_int(pb, "DisplayHeight", track->enc->height);
  2320. } else {
  2321. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  2322. param_write_string(pb, "FourCC", "AACL");
  2323. } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
  2324. param_write_string(pb, "FourCC", "WMAP");
  2325. }
  2326. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2327. track->enc->extradata_size);
  2328. param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
  2329. track->enc->codec_id));
  2330. param_write_int(pb, "Channels", track->enc->channels);
  2331. param_write_int(pb, "SamplingRate", track->enc->sample_rate);
  2332. param_write_int(pb, "BitsPerSample", 16);
  2333. param_write_int(pb, "PacketSize", track->enc->block_align ?
  2334. track->enc->block_align : 4);
  2335. }
  2336. avio_printf(pb, "</%s>\n", type);
  2337. }
  2338. avio_printf(pb, "</switch>\n");
  2339. avio_printf(pb, "</body>\n");
  2340. avio_printf(pb, "</smil>\n");
  2341. return update_size(pb, pos);
  2342. }
  2343. static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  2344. {
  2345. avio_wb32(pb, 16);
  2346. ffio_wfourcc(pb, "mfhd");
  2347. avio_wb32(pb, 0);
  2348. avio_wb32(pb, mov->fragments);
  2349. return 0;
  2350. }
  2351. static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
  2352. MOVTrack *track, int64_t moof_offset)
  2353. {
  2354. int64_t pos = avio_tell(pb);
  2355. uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  2356. MOV_TFHD_BASE_DATA_OFFSET;
  2357. if (!track->entry) {
  2358. flags |= MOV_TFHD_DURATION_IS_EMPTY;
  2359. } else {
  2360. flags |= MOV_TFHD_DEFAULT_FLAGS;
  2361. }
  2362. if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
  2363. flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
  2364. /* Don't set a default sample size, the silverlight player refuses
  2365. * to play files with that set. Don't set a default sample duration,
  2366. * WMP freaks out if it is set. Don't set a base data offset, PIFF
  2367. * file format says it MUST NOT be set. */
  2368. if (track->mode == MODE_ISM)
  2369. flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  2370. MOV_TFHD_BASE_DATA_OFFSET);
  2371. avio_wb32(pb, 0); /* size placeholder */
  2372. ffio_wfourcc(pb, "tfhd");
  2373. avio_w8(pb, 0); /* version */
  2374. avio_wb24(pb, flags);
  2375. avio_wb32(pb, track->track_id); /* track-id */
  2376. if (flags & MOV_TFHD_BASE_DATA_OFFSET)
  2377. avio_wb64(pb, moof_offset);
  2378. if (flags & MOV_TFHD_DEFAULT_DURATION) {
  2379. track->default_duration = get_cluster_duration(track, 0);
  2380. avio_wb32(pb, track->default_duration);
  2381. }
  2382. if (flags & MOV_TFHD_DEFAULT_SIZE) {
  2383. track->default_size = track->entry ? track->cluster[0].size : 1;
  2384. avio_wb32(pb, track->default_size);
  2385. } else
  2386. track->default_size = -1;
  2387. if (flags & MOV_TFHD_DEFAULT_FLAGS) {
  2388. track->default_sample_flags =
  2389. track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  2390. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
  2391. MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
  2392. avio_wb32(pb, track->default_sample_flags);
  2393. }
  2394. return update_size(pb, pos);
  2395. }
  2396. static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
  2397. {
  2398. return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
  2399. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
  2400. }
  2401. static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
  2402. MOVTrack *track, int moof_size)
  2403. {
  2404. int64_t pos = avio_tell(pb);
  2405. uint32_t flags = MOV_TRUN_DATA_OFFSET;
  2406. int i;
  2407. for (i = 0; i < track->entry; i++) {
  2408. if (get_cluster_duration(track, i) != track->default_duration)
  2409. flags |= MOV_TRUN_SAMPLE_DURATION;
  2410. if (track->cluster[i].size != track->default_size)
  2411. flags |= MOV_TRUN_SAMPLE_SIZE;
  2412. if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
  2413. flags |= MOV_TRUN_SAMPLE_FLAGS;
  2414. }
  2415. if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
  2416. flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
  2417. if (track->flags & MOV_TRACK_CTTS)
  2418. flags |= MOV_TRUN_SAMPLE_CTS;
  2419. avio_wb32(pb, 0); /* size placeholder */
  2420. ffio_wfourcc(pb, "trun");
  2421. avio_w8(pb, 0); /* version */
  2422. avio_wb24(pb, flags);
  2423. avio_wb32(pb, track->entry); /* sample count */
  2424. if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
  2425. !(mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) &&
  2426. track->track_id != 1)
  2427. avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
  2428. else
  2429. avio_wb32(pb, moof_size + 8 + track->data_offset +
  2430. track->cluster[0].pos); /* data offset */
  2431. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
  2432. avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
  2433. for (i = 0; i < track->entry; i++) {
  2434. if (flags & MOV_TRUN_SAMPLE_DURATION)
  2435. avio_wb32(pb, get_cluster_duration(track, i));
  2436. if (flags & MOV_TRUN_SAMPLE_SIZE)
  2437. avio_wb32(pb, track->cluster[i].size);
  2438. if (flags & MOV_TRUN_SAMPLE_FLAGS)
  2439. avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
  2440. if (flags & MOV_TRUN_SAMPLE_CTS)
  2441. avio_wb32(pb, track->cluster[i].cts);
  2442. }
  2443. return update_size(pb, pos);
  2444. }
  2445. static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
  2446. {
  2447. int64_t pos = avio_tell(pb);
  2448. static const uint8_t uuid[] = {
  2449. 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
  2450. 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
  2451. };
  2452. avio_wb32(pb, 0); /* size placeholder */
  2453. ffio_wfourcc(pb, "uuid");
  2454. avio_write(pb, uuid, sizeof(uuid));
  2455. avio_w8(pb, 1);
  2456. avio_wb24(pb, 0);
  2457. avio_wb64(pb, track->frag_start);
  2458. avio_wb64(pb, track->start_dts + track->track_duration -
  2459. track->cluster[0].dts);
  2460. return update_size(pb, pos);
  2461. }
  2462. static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2463. MOVTrack *track, int entry)
  2464. {
  2465. int n = track->nb_frag_info - 1 - entry, i;
  2466. int size = 8 + 16 + 4 + 1 + 16*n;
  2467. static const uint8_t uuid[] = {
  2468. 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
  2469. 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
  2470. };
  2471. if (entry < 0)
  2472. return 0;
  2473. avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
  2474. avio_wb32(pb, size);
  2475. ffio_wfourcc(pb, "uuid");
  2476. avio_write(pb, uuid, sizeof(uuid));
  2477. avio_w8(pb, 1);
  2478. avio_wb24(pb, 0);
  2479. avio_w8(pb, n);
  2480. for (i = 0; i < n; i++) {
  2481. int index = entry + 1 + i;
  2482. avio_wb64(pb, track->frag_info[index].time);
  2483. avio_wb64(pb, track->frag_info[index].duration);
  2484. }
  2485. if (n < mov->ism_lookahead) {
  2486. int free_size = 16 * (mov->ism_lookahead - n);
  2487. avio_wb32(pb, free_size);
  2488. ffio_wfourcc(pb, "free");
  2489. ffio_fill(pb, 0, free_size - 8);
  2490. }
  2491. return 0;
  2492. }
  2493. static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
  2494. MOVTrack *track)
  2495. {
  2496. int64_t pos = avio_tell(pb);
  2497. int i;
  2498. for (i = 0; i < mov->ism_lookahead; i++) {
  2499. /* Update the tfrf tag for the last ism_lookahead fragments,
  2500. * nb_frag_info - 1 is the next fragment to be written. */
  2501. mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
  2502. }
  2503. avio_seek(pb, pos, SEEK_SET);
  2504. return 0;
  2505. }
  2506. static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2507. MOVTrack *track, int64_t moof_offset,
  2508. int moof_size)
  2509. {
  2510. int64_t pos = avio_tell(pb);
  2511. avio_wb32(pb, 0); /* size placeholder */
  2512. ffio_wfourcc(pb, "traf");
  2513. mov_write_tfhd_tag(pb, mov, track, moof_offset);
  2514. mov_write_trun_tag(pb, mov, track, moof_size);
  2515. if (mov->mode == MODE_ISM) {
  2516. mov_write_tfxd_tag(pb, track);
  2517. if (mov->ism_lookahead) {
  2518. int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
  2519. track->tfrf_offset = avio_tell(pb);
  2520. avio_wb32(pb, 8 + size);
  2521. ffio_wfourcc(pb, "free");
  2522. for (i = 0; i < size; i++)
  2523. avio_w8(pb, 0);
  2524. }
  2525. }
  2526. return update_size(pb, pos);
  2527. }
  2528. static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
  2529. int tracks, int moof_size)
  2530. {
  2531. int64_t pos = avio_tell(pb);
  2532. int i;
  2533. avio_wb32(pb, 0); /* size placeholder */
  2534. ffio_wfourcc(pb, "moof");
  2535. mov_write_mfhd_tag(pb, mov);
  2536. for (i = 0; i < mov->nb_streams; i++) {
  2537. MOVTrack *track = &mov->tracks[i];
  2538. if (tracks >= 0 && i != tracks)
  2539. continue;
  2540. if (!track->entry)
  2541. continue;
  2542. mov_write_traf_tag(pb, mov, track, pos, moof_size);
  2543. }
  2544. return update_size(pb, pos);
  2545. }
  2546. static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
  2547. {
  2548. AVIOContext *avio_buf;
  2549. int ret, moof_size;
  2550. if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
  2551. return ret;
  2552. mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
  2553. moof_size = ffio_close_null_buf(avio_buf);
  2554. return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
  2555. }
  2556. static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
  2557. {
  2558. int64_t pos = avio_tell(pb);
  2559. int i;
  2560. avio_wb32(pb, 0); /* size placeholder */
  2561. ffio_wfourcc(pb, "tfra");
  2562. avio_w8(pb, 1); /* version */
  2563. avio_wb24(pb, 0);
  2564. avio_wb32(pb, track->track_id);
  2565. avio_wb32(pb, 0); /* length of traf/trun/sample num */
  2566. avio_wb32(pb, track->nb_frag_info);
  2567. for (i = 0; i < track->nb_frag_info; i++) {
  2568. avio_wb64(pb, track->frag_info[i].time);
  2569. avio_wb64(pb, track->frag_info[i].offset);
  2570. avio_w8(pb, 1); /* traf number */
  2571. avio_w8(pb, 1); /* trun number */
  2572. avio_w8(pb, 1); /* sample number */
  2573. }
  2574. return update_size(pb, pos);
  2575. }
  2576. static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
  2577. {
  2578. int64_t pos = avio_tell(pb);
  2579. int i;
  2580. avio_wb32(pb, 0); /* size placeholder */
  2581. ffio_wfourcc(pb, "mfra");
  2582. /* An empty mfra atom is enough to indicate to the publishing point that
  2583. * the stream has ended. */
  2584. if (mov->flags & FF_MOV_FLAG_ISML)
  2585. return update_size(pb, pos);
  2586. for (i = 0; i < mov->nb_streams; i++) {
  2587. MOVTrack *track = &mov->tracks[i];
  2588. if (track->nb_frag_info)
  2589. mov_write_tfra_tag(pb, track);
  2590. }
  2591. avio_wb32(pb, 16);
  2592. ffio_wfourcc(pb, "mfro");
  2593. avio_wb32(pb, 0); /* version + flags */
  2594. avio_wb32(pb, avio_tell(pb) + 4 - pos);
  2595. return update_size(pb, pos);
  2596. }
  2597. static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
  2598. {
  2599. avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
  2600. ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
  2601. mov->mdat_pos = avio_tell(pb);
  2602. avio_wb32(pb, 0); /* size placeholder*/
  2603. ffio_wfourcc(pb, "mdat");
  2604. return 0;
  2605. }
  2606. /* TODO: This needs to be more general */
  2607. static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
  2608. {
  2609. MOVMuxContext *mov = s->priv_data;
  2610. int64_t pos = avio_tell(pb);
  2611. int has_h264 = 0, has_video = 0;
  2612. int minor = 0x200;
  2613. int i;
  2614. for (i = 0; i < s->nb_streams; i++) {
  2615. AVStream *st = s->streams[i];
  2616. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  2617. has_video = 1;
  2618. if (st->codec->codec_id == AV_CODEC_ID_H264)
  2619. has_h264 = 1;
  2620. }
  2621. avio_wb32(pb, 0); /* size */
  2622. ffio_wfourcc(pb, "ftyp");
  2623. if (mov->mode == MODE_3GP) {
  2624. ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
  2625. minor = has_h264 ? 0x100 : 0x200;
  2626. } else if (mov->mode & MODE_3G2) {
  2627. ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
  2628. minor = has_h264 ? 0x20000 : 0x10000;
  2629. } else if (mov->mode == MODE_PSP)
  2630. ffio_wfourcc(pb, "MSNV");
  2631. else if (mov->mode == MODE_MP4)
  2632. ffio_wfourcc(pb, "isom");
  2633. else if (mov->mode == MODE_IPOD)
  2634. ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
  2635. else if (mov->mode == MODE_ISM)
  2636. ffio_wfourcc(pb, "isml");
  2637. else if (mov->mode == MODE_F4V)
  2638. ffio_wfourcc(pb, "f4v ");
  2639. else
  2640. ffio_wfourcc(pb, "qt ");
  2641. avio_wb32(pb, minor);
  2642. if (mov->mode == MODE_MOV)
  2643. ffio_wfourcc(pb, "qt ");
  2644. else if (mov->mode == MODE_ISM) {
  2645. ffio_wfourcc(pb, "piff");
  2646. ffio_wfourcc(pb, "iso2");
  2647. } else {
  2648. ffio_wfourcc(pb, "isom");
  2649. ffio_wfourcc(pb, "iso2");
  2650. if (has_h264)
  2651. ffio_wfourcc(pb, "avc1");
  2652. }
  2653. if (mov->mode == MODE_3GP)
  2654. ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
  2655. else if (mov->mode & MODE_3G2)
  2656. ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
  2657. else if (mov->mode == MODE_PSP)
  2658. ffio_wfourcc(pb, "MSNV");
  2659. else if (mov->mode == MODE_MP4)
  2660. ffio_wfourcc(pb, "mp41");
  2661. return update_size(pb, pos);
  2662. }
  2663. static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
  2664. {
  2665. AVCodecContext *video_codec = s->streams[0]->codec;
  2666. AVCodecContext *audio_codec = s->streams[1]->codec;
  2667. int audio_rate = audio_codec->sample_rate;
  2668. int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
  2669. int audio_kbitrate = audio_codec->bit_rate / 1000;
  2670. int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
  2671. avio_wb32(pb, 0x94); /* size */
  2672. ffio_wfourcc(pb, "uuid");
  2673. ffio_wfourcc(pb, "PROF");
  2674. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2675. avio_wb32(pb, 0xbb88695c);
  2676. avio_wb32(pb, 0xfac9c740);
  2677. avio_wb32(pb, 0x0); /* ? */
  2678. avio_wb32(pb, 0x3); /* 3 sections ? */
  2679. avio_wb32(pb, 0x14); /* size */
  2680. ffio_wfourcc(pb, "FPRF");
  2681. avio_wb32(pb, 0x0); /* ? */
  2682. avio_wb32(pb, 0x0); /* ? */
  2683. avio_wb32(pb, 0x0); /* ? */
  2684. avio_wb32(pb, 0x2c); /* size */
  2685. ffio_wfourcc(pb, "APRF"); /* audio */
  2686. avio_wb32(pb, 0x0);
  2687. avio_wb32(pb, 0x2); /* TrackID */
  2688. ffio_wfourcc(pb, "mp4a");
  2689. avio_wb32(pb, 0x20f);
  2690. avio_wb32(pb, 0x0);
  2691. avio_wb32(pb, audio_kbitrate);
  2692. avio_wb32(pb, audio_kbitrate);
  2693. avio_wb32(pb, audio_rate);
  2694. avio_wb32(pb, audio_codec->channels);
  2695. avio_wb32(pb, 0x34); /* size */
  2696. ffio_wfourcc(pb, "VPRF"); /* video */
  2697. avio_wb32(pb, 0x0);
  2698. avio_wb32(pb, 0x1); /* TrackID */
  2699. if (video_codec->codec_id == AV_CODEC_ID_H264) {
  2700. ffio_wfourcc(pb, "avc1");
  2701. avio_wb16(pb, 0x014D);
  2702. avio_wb16(pb, 0x0015);
  2703. } else {
  2704. ffio_wfourcc(pb, "mp4v");
  2705. avio_wb16(pb, 0x0000);
  2706. avio_wb16(pb, 0x0103);
  2707. }
  2708. avio_wb32(pb, 0x0);
  2709. avio_wb32(pb, video_kbitrate);
  2710. avio_wb32(pb, video_kbitrate);
  2711. avio_wb32(pb, frame_rate);
  2712. avio_wb32(pb, frame_rate);
  2713. avio_wb16(pb, video_codec->width);
  2714. avio_wb16(pb, video_codec->height);
  2715. avio_wb32(pb, 0x010001); /* ? */
  2716. }
  2717. static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
  2718. {
  2719. uint32_t c = -1;
  2720. int i, closed_gop = 0;
  2721. for (i = 0; i < pkt->size - 4; i++) {
  2722. c = (c << 8) + pkt->data[i];
  2723. if (c == 0x1b8) { // gop
  2724. closed_gop = pkt->data[i + 4] >> 6 & 0x01;
  2725. } else if (c == 0x100) { // pic
  2726. int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
  2727. if (!temp_ref || closed_gop) // I picture is not reordered
  2728. *flags = MOV_SYNC_SAMPLE;
  2729. else
  2730. *flags = MOV_PARTIAL_SYNC_SAMPLE;
  2731. break;
  2732. }
  2733. }
  2734. return 0;
  2735. }
  2736. static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
  2737. {
  2738. const uint8_t *start, *next, *end = pkt->data + pkt->size;
  2739. int seq = 0, entry = 0;
  2740. int key = pkt->flags & AV_PKT_FLAG_KEY;
  2741. start = find_next_marker(pkt->data, end);
  2742. for (next = start; next < end; start = next) {
  2743. next = find_next_marker(start + 4, end);
  2744. switch (AV_RB32(start)) {
  2745. case VC1_CODE_SEQHDR:
  2746. seq = 1;
  2747. break;
  2748. case VC1_CODE_ENTRYPOINT:
  2749. entry = 1;
  2750. break;
  2751. case VC1_CODE_SLICE:
  2752. trk->vc1_info.slices = 1;
  2753. break;
  2754. }
  2755. }
  2756. if (!trk->entry && !fragment) {
  2757. /* First packet in first fragment */
  2758. trk->vc1_info.first_packet_seq = seq;
  2759. trk->vc1_info.first_packet_entry = entry;
  2760. } else if ((seq && !trk->vc1_info.packet_seq) ||
  2761. (entry && !trk->vc1_info.packet_entry)) {
  2762. int i;
  2763. for (i = 0; i < trk->entry; i++)
  2764. trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
  2765. trk->has_keyframes = 0;
  2766. if (seq)
  2767. trk->vc1_info.packet_seq = 1;
  2768. if (entry)
  2769. trk->vc1_info.packet_entry = 1;
  2770. if (!fragment) {
  2771. /* First fragment */
  2772. if ((!seq || trk->vc1_info.first_packet_seq) &&
  2773. (!entry || trk->vc1_info.first_packet_entry)) {
  2774. /* First packet had the same headers as this one, readd the
  2775. * sync sample flag. */
  2776. trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
  2777. trk->has_keyframes = 1;
  2778. }
  2779. }
  2780. }
  2781. if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
  2782. key = seq && entry;
  2783. else if (trk->vc1_info.packet_seq)
  2784. key = seq;
  2785. else if (trk->vc1_info.packet_entry)
  2786. key = entry;
  2787. if (key) {
  2788. trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
  2789. trk->has_keyframes++;
  2790. }
  2791. }
  2792. static int mov_flush_fragment(AVFormatContext *s)
  2793. {
  2794. MOVMuxContext *mov = s->priv_data;
  2795. int i, first_track = -1;
  2796. int64_t mdat_size = 0;
  2797. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2798. return 0;
  2799. if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
  2800. int64_t pos = avio_tell(s->pb);
  2801. uint8_t *buf;
  2802. int buf_size, moov_size;
  2803. for (i = 0; i < mov->nb_streams; i++)
  2804. if (!mov->tracks[i].entry)
  2805. break;
  2806. /* Don't write the initial moov unless all tracks have data */
  2807. if (i < mov->nb_streams)
  2808. return 0;
  2809. moov_size = get_moov_size(s);
  2810. for (i = 0; i < mov->nb_streams; i++)
  2811. mov->tracks[i].data_offset = pos + moov_size + 8;
  2812. mov_write_moov_tag(s->pb, mov, s);
  2813. buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
  2814. mov->mdat_buf = NULL;
  2815. avio_wb32(s->pb, buf_size + 8);
  2816. ffio_wfourcc(s->pb, "mdat");
  2817. avio_write(s->pb, buf, buf_size);
  2818. av_free(buf);
  2819. mov->fragments++;
  2820. mov->mdat_size = 0;
  2821. for (i = 0; i < mov->nb_streams; i++) {
  2822. if (mov->tracks[i].entry)
  2823. mov->tracks[i].frag_start += mov->tracks[i].start_dts +
  2824. mov->tracks[i].track_duration -
  2825. mov->tracks[i].cluster[0].dts;
  2826. mov->tracks[i].entry = 0;
  2827. }
  2828. avio_flush(s->pb);
  2829. return 0;
  2830. }
  2831. for (i = 0; i < mov->nb_streams; i++) {
  2832. MOVTrack *track = &mov->tracks[i];
  2833. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
  2834. track->data_offset = 0;
  2835. else
  2836. track->data_offset = mdat_size;
  2837. if (!track->mdat_buf)
  2838. continue;
  2839. mdat_size += avio_tell(track->mdat_buf);
  2840. if (first_track < 0)
  2841. first_track = i;
  2842. }
  2843. if (!mdat_size)
  2844. return 0;
  2845. for (i = 0; i < mov->nb_streams; i++) {
  2846. MOVTrack *track = &mov->tracks[i];
  2847. int buf_size, write_moof = 1, moof_tracks = -1;
  2848. uint8_t *buf;
  2849. int64_t duration = 0;
  2850. if (track->entry)
  2851. duration = track->start_dts + track->track_duration -
  2852. track->cluster[0].dts;
  2853. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
  2854. if (!track->mdat_buf)
  2855. continue;
  2856. mdat_size = avio_tell(track->mdat_buf);
  2857. moof_tracks = i;
  2858. } else {
  2859. write_moof = i == first_track;
  2860. }
  2861. if (write_moof) {
  2862. MOVFragmentInfo *info;
  2863. avio_flush(s->pb);
  2864. track->nb_frag_info++;
  2865. if (track->nb_frag_info >= track->frag_info_capacity) {
  2866. unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
  2867. if (av_reallocp_array(&track->frag_info,
  2868. new_capacity,
  2869. sizeof(*track->frag_info)))
  2870. return AVERROR(ENOMEM);
  2871. track->frag_info_capacity = new_capacity;
  2872. }
  2873. info = &track->frag_info[track->nb_frag_info - 1];
  2874. info->offset = avio_tell(s->pb);
  2875. info->time = mov->tracks[i].frag_start;
  2876. info->duration = duration;
  2877. mov_write_tfrf_tags(s->pb, mov, track);
  2878. mov_write_moof_tag(s->pb, mov, moof_tracks);
  2879. info->tfrf_offset = track->tfrf_offset;
  2880. mov->fragments++;
  2881. avio_wb32(s->pb, mdat_size + 8);
  2882. ffio_wfourcc(s->pb, "mdat");
  2883. }
  2884. if (track->entry)
  2885. track->frag_start += duration;
  2886. track->entry = 0;
  2887. if (!track->mdat_buf)
  2888. continue;
  2889. buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
  2890. track->mdat_buf = NULL;
  2891. avio_write(s->pb, buf, buf_size);
  2892. av_free(buf);
  2893. }
  2894. mov->mdat_size = 0;
  2895. avio_flush(s->pb);
  2896. return 0;
  2897. }
  2898. int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  2899. {
  2900. MOVMuxContext *mov = s->priv_data;
  2901. AVIOContext *pb = s->pb;
  2902. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  2903. AVCodecContext *enc = trk->enc;
  2904. unsigned int samples_in_chunk = 0;
  2905. int size = pkt->size;
  2906. uint8_t *reformatted_data = NULL;
  2907. if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2908. int ret;
  2909. if (mov->fragments > 0) {
  2910. if (!trk->mdat_buf) {
  2911. if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
  2912. return ret;
  2913. }
  2914. pb = trk->mdat_buf;
  2915. } else {
  2916. if (!mov->mdat_buf) {
  2917. if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
  2918. return ret;
  2919. }
  2920. pb = mov->mdat_buf;
  2921. }
  2922. }
  2923. if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
  2924. /* We must find out how many AMR blocks there are in one packet */
  2925. static uint16_t packed_size[16] =
  2926. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
  2927. int len = 0;
  2928. while (len < size && samples_in_chunk < 100) {
  2929. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  2930. samples_in_chunk++;
  2931. }
  2932. if (samples_in_chunk > 1) {
  2933. av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
  2934. return -1;
  2935. }
  2936. } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  2937. enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  2938. samples_in_chunk = enc->frame_size;
  2939. } else if (trk->sample_size)
  2940. samples_in_chunk = size / trk->sample_size;
  2941. else
  2942. samples_in_chunk = 1;
  2943. /* copy extradata if it exists */
  2944. if (trk->vos_len == 0 && enc->extradata_size > 0) {
  2945. trk->vos_len = enc->extradata_size;
  2946. trk->vos_data = av_malloc(trk->vos_len);
  2947. memcpy(trk->vos_data, enc->extradata, trk->vos_len);
  2948. }
  2949. if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
  2950. (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
  2951. if (!s->streams[pkt->stream_index]->nb_frames) {
  2952. av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
  2953. "use audio bitstream filter 'aac_adtstoasc' to fix it "
  2954. "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
  2955. return -1;
  2956. }
  2957. av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
  2958. }
  2959. if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
  2960. /* from x264 or from bytestream h264 */
  2961. /* nal reformating needed */
  2962. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
  2963. ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
  2964. &size);
  2965. avio_write(pb, reformatted_data, size);
  2966. } else {
  2967. size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
  2968. }
  2969. } else {
  2970. avio_write(pb, pkt->data, size);
  2971. }
  2972. if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
  2973. enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
  2974. /* copy frame to create needed atoms */
  2975. trk->vos_len = size;
  2976. trk->vos_data = av_malloc(size);
  2977. if (!trk->vos_data)
  2978. return AVERROR(ENOMEM);
  2979. memcpy(trk->vos_data, pkt->data, size);
  2980. }
  2981. if (trk->entry >= trk->cluster_capacity) {
  2982. unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
  2983. if (av_reallocp_array(&trk->cluster, new_capacity,
  2984. sizeof(*trk->cluster)))
  2985. return AVERROR(ENOMEM);
  2986. trk->cluster_capacity = new_capacity;
  2987. }
  2988. trk->cluster[trk->entry].pos = avio_tell(pb) - size;
  2989. trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
  2990. trk->cluster[trk->entry].chunkNum = 0;
  2991. trk->cluster[trk->entry].size = size;
  2992. trk->cluster[trk->entry].entries = samples_in_chunk;
  2993. trk->cluster[trk->entry].dts = pkt->dts;
  2994. if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
  2995. /* First packet of a new fragment. We already wrote the duration
  2996. * of the last packet of the previous fragment based on track_duration,
  2997. * which might not exactly match our dts. Therefore adjust the dts
  2998. * of this packet to be what the previous packets duration implies. */
  2999. trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
  3000. }
  3001. if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !supports_edts(mov)) {
  3002. trk->cluster[trk->entry].dts = trk->start_dts = 0;
  3003. }
  3004. if (trk->start_dts == AV_NOPTS_VALUE)
  3005. trk->start_dts = pkt->dts;
  3006. trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
  3007. trk->last_sample_is_subtitle_end = 0;
  3008. if (pkt->pts == AV_NOPTS_VALUE) {
  3009. av_log(s, AV_LOG_WARNING, "pts has no value\n");
  3010. pkt->pts = pkt->dts;
  3011. }
  3012. if (pkt->dts != pkt->pts)
  3013. trk->flags |= MOV_TRACK_CTTS;
  3014. trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
  3015. trk->cluster[trk->entry].flags = 0;
  3016. if (enc->codec_id == AV_CODEC_ID_VC1) {
  3017. mov_parse_vc1_frame(pkt, trk, mov->fragments);
  3018. } else if (pkt->flags & AV_PKT_FLAG_KEY) {
  3019. if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  3020. trk->entry > 0) { // force sync sample for the first key frame
  3021. mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
  3022. if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
  3023. trk->flags |= MOV_TRACK_STPS;
  3024. } else {
  3025. trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
  3026. }
  3027. if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
  3028. trk->has_keyframes++;
  3029. }
  3030. trk->entry++;
  3031. trk->sample_count += samples_in_chunk;
  3032. mov->mdat_size += size;
  3033. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
  3034. ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
  3035. reformatted_data, size);
  3036. av_free(reformatted_data);
  3037. return 0;
  3038. }
  3039. static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
  3040. {
  3041. MOVMuxContext *mov = s->priv_data;
  3042. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  3043. AVCodecContext *enc = trk->enc;
  3044. int64_t frag_duration = 0;
  3045. int size = pkt->size;
  3046. if (!pkt->size)
  3047. return 0; /* Discard 0 sized packets */
  3048. if (trk->entry && pkt->stream_index < s->nb_streams)
  3049. frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
  3050. s->streams[pkt->stream_index]->time_base,
  3051. AV_TIME_BASE_Q);
  3052. if ((mov->max_fragment_duration &&
  3053. frag_duration >= mov->max_fragment_duration) ||
  3054. (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
  3055. (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
  3056. enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  3057. trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
  3058. if (frag_duration >= mov->min_fragment_duration)
  3059. mov_flush_fragment(s);
  3060. }
  3061. return ff_mov_write_packet(s, pkt);
  3062. }
  3063. static int mov_write_subtitle_end_packet(AVFormatContext *s,
  3064. int stream_index,
  3065. int64_t dts) {
  3066. AVPacket end;
  3067. uint8_t data[2] = {0};
  3068. int ret;
  3069. av_init_packet(&end);
  3070. end.size = sizeof(data);
  3071. end.data = data;
  3072. end.pts = dts;
  3073. end.dts = dts;
  3074. end.duration = 0;
  3075. end.stream_index = stream_index;
  3076. ret = mov_write_single_packet(s, &end);
  3077. av_free_packet(&end);
  3078. return ret;
  3079. }
  3080. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  3081. {
  3082. if (!pkt) {
  3083. mov_flush_fragment(s);
  3084. return 1;
  3085. } else {
  3086. int i;
  3087. MOVMuxContext *mov = s->priv_data;
  3088. if (!pkt->size) return 0; /* Discard 0 sized packets */
  3089. /*
  3090. * Subtitles require special handling.
  3091. *
  3092. * 1) For full complaince, every track must have a sample at
  3093. * dts == 0, which is rarely true for subtitles. So, as soon
  3094. * as we see any packet with dts > 0, write an empty subtitle
  3095. * at dts == 0 for any subtitle track with no samples in it.
  3096. *
  3097. * 2) For each subtitle track, check if the current packet's
  3098. * dts is past the duration of the last subtitle sample. If
  3099. * so, we now need to write an end sample for that subtitle.
  3100. *
  3101. * This must be done conditionally to allow for subtitles that
  3102. * immediately replace each other, in which case an end sample
  3103. * is not needed, and is, in fact, actively harmful.
  3104. *
  3105. * 3) See mov_write_trailer for how the final end sample is
  3106. * handled.
  3107. */
  3108. for (i = 0; i < mov->nb_streams; i++) {
  3109. MOVTrack *trk = &mov->tracks[i];
  3110. int ret;
  3111. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  3112. trk->track_duration < pkt->dts &&
  3113. (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
  3114. ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
  3115. if (ret < 0) return ret;
  3116. trk->last_sample_is_subtitle_end = 1;
  3117. }
  3118. }
  3119. return mov_write_single_packet(s, pkt);
  3120. }
  3121. }
  3122. // QuickTime chapters involve an additional text track with the chapter names
  3123. // as samples, and a tref pointing from the other tracks to the chapter one.
  3124. static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
  3125. {
  3126. AVIOContext *pb;
  3127. MOVMuxContext *mov = s->priv_data;
  3128. MOVTrack *track = &mov->tracks[tracknum];
  3129. AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
  3130. int i, len;
  3131. track->mode = mov->mode;
  3132. track->tag = MKTAG('t','e','x','t');
  3133. track->timescale = MOV_TIMESCALE;
  3134. track->enc = avcodec_alloc_context3(NULL);
  3135. if (!track->enc)
  3136. return AVERROR(ENOMEM);
  3137. track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  3138. #if 0
  3139. // These properties are required to make QT recognize the chapter track
  3140. uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
  3141. if (ff_alloc_extradata(track->enc, sizeof(chapter_properties)))
  3142. return AVERROR(ENOMEM);
  3143. memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
  3144. #else
  3145. if (avio_open_dyn_buf(&pb) >= 0) {
  3146. int size;
  3147. uint8_t *buf;
  3148. /* Stub header (usually for Quicktime chapter track) */
  3149. // TextSampleEntry
  3150. avio_wb32(pb, 0x01); // displayFlags
  3151. avio_w8(pb, 0x00); // horizontal justification
  3152. avio_w8(pb, 0x00); // vertical justification
  3153. avio_w8(pb, 0x00); // bgColourRed
  3154. avio_w8(pb, 0x00); // bgColourGreen
  3155. avio_w8(pb, 0x00); // bgColourBlue
  3156. avio_w8(pb, 0x00); // bgColourAlpha
  3157. // BoxRecord
  3158. avio_wb16(pb, 0x00); // defTextBoxTop
  3159. avio_wb16(pb, 0x00); // defTextBoxLeft
  3160. avio_wb16(pb, 0x00); // defTextBoxBottom
  3161. avio_wb16(pb, 0x00); // defTextBoxRight
  3162. // StyleRecord
  3163. avio_wb16(pb, 0x00); // startChar
  3164. avio_wb16(pb, 0x00); // endChar
  3165. avio_wb16(pb, 0x01); // fontID
  3166. avio_w8(pb, 0x00); // fontStyleFlags
  3167. avio_w8(pb, 0x00); // fontSize
  3168. avio_w8(pb, 0x00); // fgColourRed
  3169. avio_w8(pb, 0x00); // fgColourGreen
  3170. avio_w8(pb, 0x00); // fgColourBlue
  3171. avio_w8(pb, 0x00); // fgColourAlpha
  3172. // FontTableBox
  3173. avio_wb32(pb, 0x0D); // box size
  3174. ffio_wfourcc(pb, "ftab"); // box atom name
  3175. avio_wb16(pb, 0x01); // entry count
  3176. // FontRecord
  3177. avio_wb16(pb, 0x01); // font ID
  3178. avio_w8(pb, 0x00); // font name length
  3179. if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
  3180. track->enc->extradata = buf;
  3181. track->enc->extradata_size = size;
  3182. } else {
  3183. av_free(&buf);
  3184. }
  3185. }
  3186. #endif
  3187. for (i = 0; i < s->nb_chapters; i++) {
  3188. AVChapter *c = s->chapters[i];
  3189. AVDictionaryEntry *t;
  3190. int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
  3191. pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
  3192. pkt.duration = end - pkt.dts;
  3193. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  3194. len = strlen(t->value);
  3195. pkt.size = len + 2;
  3196. pkt.data = av_malloc(pkt.size);
  3197. if (!pkt.data)
  3198. return AVERROR(ENOMEM);
  3199. AV_WB16(pkt.data, len);
  3200. memcpy(pkt.data + 2, t->value, len);
  3201. ff_mov_write_packet(s, &pkt);
  3202. av_freep(&pkt.data);
  3203. }
  3204. }
  3205. return 0;
  3206. }
  3207. static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
  3208. {
  3209. int ret;
  3210. MOVMuxContext *mov = s->priv_data;
  3211. MOVTrack *track = &mov->tracks[index];
  3212. AVStream *src_st = s->streams[src_index];
  3213. AVTimecode tc;
  3214. AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
  3215. AVRational rate = find_fps(s, src_st);
  3216. /* compute the frame number */
  3217. ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
  3218. if (ret < 0)
  3219. return ret;
  3220. /* tmcd track based on video stream */
  3221. track->mode = mov->mode;
  3222. track->tag = MKTAG('t','m','c','d');
  3223. track->src_track = src_index;
  3224. track->timescale = mov->tracks[src_index].timescale;
  3225. if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
  3226. track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
  3227. /* encode context: tmcd data stream */
  3228. track->enc = avcodec_alloc_context3(NULL);
  3229. track->enc->codec_type = AVMEDIA_TYPE_DATA;
  3230. track->enc->codec_tag = track->tag;
  3231. track->enc->time_base = av_inv_q(rate);
  3232. /* the tmcd track just contains one packet with the frame number */
  3233. pkt.data = av_malloc(pkt.size);
  3234. AV_WB32(pkt.data, tc.start);
  3235. ret = ff_mov_write_packet(s, &pkt);
  3236. av_free(pkt.data);
  3237. return ret;
  3238. }
  3239. /*
  3240. * st->disposition controls the "enabled" flag in the tkhd tag.
  3241. * QuickTime will not play a track if it is not enabled. So make sure
  3242. * that one track of each type (audio, video, subtitle) is enabled.
  3243. *
  3244. * Subtitles are special. For audio and video, setting "enabled" also
  3245. * makes the track "default" (i.e. it is rendered when played). For
  3246. * subtitles, an "enabled" subtitle is not rendered by default, but
  3247. * if no subtitle is enabled, the subtitle menu in QuickTime will be
  3248. * empty!
  3249. */
  3250. static void enable_tracks(AVFormatContext *s)
  3251. {
  3252. MOVMuxContext *mov = s->priv_data;
  3253. int i;
  3254. uint8_t enabled[AVMEDIA_TYPE_NB];
  3255. int first[AVMEDIA_TYPE_NB];
  3256. for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
  3257. enabled[i] = 0;
  3258. first[i] = -1;
  3259. }
  3260. for (i = 0; i < s->nb_streams; i++) {
  3261. AVStream *st = s->streams[i];
  3262. if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
  3263. st->codec->codec_type >= AVMEDIA_TYPE_NB)
  3264. continue;
  3265. if (first[st->codec->codec_type] < 0)
  3266. first[st->codec->codec_type] = i;
  3267. if (st->disposition & AV_DISPOSITION_DEFAULT) {
  3268. mov->tracks[i].flags |= MOV_TRACK_ENABLED;
  3269. enabled[st->codec->codec_type] = 1;
  3270. }
  3271. }
  3272. for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
  3273. switch (i) {
  3274. case AVMEDIA_TYPE_VIDEO:
  3275. case AVMEDIA_TYPE_AUDIO:
  3276. case AVMEDIA_TYPE_SUBTITLE:
  3277. if (!enabled[i] && first[i] >= 0)
  3278. mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
  3279. break;
  3280. }
  3281. }
  3282. }
  3283. static void mov_free(AVFormatContext *s)
  3284. {
  3285. MOVMuxContext *mov = s->priv_data;
  3286. int i;
  3287. if (mov->chapter_track) {
  3288. if (mov->tracks[mov->chapter_track].enc)
  3289. av_freep(&mov->tracks[mov->chapter_track].enc->extradata);
  3290. av_freep(&mov->tracks[mov->chapter_track].enc);
  3291. }
  3292. for (i = 0; i < mov->nb_streams; i++) {
  3293. if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
  3294. ff_mov_close_hinting(&mov->tracks[i]);
  3295. else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
  3296. av_freep(&mov->tracks[i].enc);
  3297. av_freep(&mov->tracks[i].cluster);
  3298. av_freep(&mov->tracks[i].frag_info);
  3299. if (mov->tracks[i].vos_len)
  3300. av_freep(&mov->tracks[i].vos_data);
  3301. }
  3302. av_freep(&mov->tracks);
  3303. }
  3304. static int mov_write_header(AVFormatContext *s)
  3305. {
  3306. AVIOContext *pb = s->pb;
  3307. MOVMuxContext *mov = s->priv_data;
  3308. AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  3309. int i, hint_track = 0, tmcd_track = 0;
  3310. /* Default mode == MP4 */
  3311. mov->mode = MODE_MP4;
  3312. if (s->oformat != NULL) {
  3313. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  3314. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
  3315. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  3316. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  3317. else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
  3318. else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
  3319. else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
  3320. }
  3321. /* Set the FRAGMENT flag if any of the fragmentation methods are
  3322. * enabled. */
  3323. if (mov->max_fragment_duration || mov->max_fragment_size ||
  3324. mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
  3325. FF_MOV_FLAG_FRAG_KEYFRAME |
  3326. FF_MOV_FLAG_FRAG_CUSTOM))
  3327. mov->flags |= FF_MOV_FLAG_FRAGMENT;
  3328. /* Set other implicit flags immediately */
  3329. if (mov->mode == MODE_ISM)
  3330. mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
  3331. FF_MOV_FLAG_FRAGMENT;
  3332. /* faststart: moov at the beginning of the file, if supported */
  3333. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  3334. if ((mov->flags & FF_MOV_FLAG_FRAGMENT) ||
  3335. (s->flags & AVFMT_FLAG_CUSTOM_IO)) {
  3336. av_log(s, AV_LOG_WARNING, "The faststart flag is incompatible "
  3337. "with fragmentation and custom IO, disabling faststart\n");
  3338. mov->flags &= ~FF_MOV_FLAG_FASTSTART;
  3339. } else
  3340. mov->reserved_moov_size = -1;
  3341. }
  3342. if (!supports_edts(mov) && s->avoid_negative_ts < 0) {
  3343. s->avoid_negative_ts = 1;
  3344. }
  3345. /* Non-seekable output is ok if using fragmentation. If ism_lookahead
  3346. * is enabled, we don't support non-seekable output at all. */
  3347. if (!s->pb->seekable &&
  3348. (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
  3349. av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
  3350. return AVERROR(EINVAL);
  3351. }
  3352. mov_write_ftyp_tag(pb,s);
  3353. if (mov->mode == MODE_PSP) {
  3354. int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
  3355. for (i = 0; i < s->nb_streams; i++) {
  3356. AVStream *st = s->streams[i];
  3357. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  3358. video_streams_nb++;
  3359. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  3360. audio_streams_nb++;
  3361. else
  3362. other_streams_nb++;
  3363. }
  3364. if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
  3365. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  3366. return AVERROR(EINVAL);
  3367. }
  3368. mov_write_uuidprof_tag(pb, s);
  3369. }
  3370. mov->nb_streams = s->nb_streams;
  3371. if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
  3372. mov->chapter_track = mov->nb_streams++;
  3373. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  3374. /* Add hint tracks for each audio and video stream */
  3375. hint_track = mov->nb_streams;
  3376. for (i = 0; i < s->nb_streams; i++) {
  3377. AVStream *st = s->streams[i];
  3378. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  3379. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3380. mov->nb_streams++;
  3381. }
  3382. }
  3383. }
  3384. if (mov->mode == MODE_MOV) {
  3385. tmcd_track = mov->nb_streams;
  3386. /* +1 tmcd track for each video stream with a timecode */
  3387. for (i = 0; i < s->nb_streams; i++) {
  3388. AVStream *st = s->streams[i];
  3389. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  3390. (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
  3391. mov->nb_meta_tmcd++;
  3392. }
  3393. /* check if there is already a tmcd track to remux */
  3394. if (mov->nb_meta_tmcd) {
  3395. for (i = 0; i < s->nb_streams; i++) {
  3396. AVStream *st = s->streams[i];
  3397. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  3398. av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
  3399. "so timecode metadata are now ignored\n");
  3400. mov->nb_meta_tmcd = 0;
  3401. }
  3402. }
  3403. }
  3404. mov->nb_streams += mov->nb_meta_tmcd;
  3405. }
  3406. // Reserve an extra stream for chapters for the case where chapters
  3407. // are written in the trailer
  3408. mov->tracks = av_mallocz((mov->nb_streams + 1) * sizeof(*mov->tracks));
  3409. if (!mov->tracks)
  3410. return AVERROR(ENOMEM);
  3411. for (i = 0; i < s->nb_streams; i++) {
  3412. AVStream *st= s->streams[i];
  3413. MOVTrack *track= &mov->tracks[i];
  3414. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
  3415. track->enc = st->codec;
  3416. track->st = st;
  3417. track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
  3418. if (track->language < 0)
  3419. track->language = 0;
  3420. track->mode = mov->mode;
  3421. track->tag = mov_find_codec_tag(s, track);
  3422. if (!track->tag) {
  3423. av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
  3424. "codec not currently supported in container\n", i);
  3425. goto error;
  3426. }
  3427. /* If hinting of this track is enabled by a later hint track,
  3428. * this is updated. */
  3429. track->hint_track = -1;
  3430. track->start_dts = AV_NOPTS_VALUE;
  3431. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3432. if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
  3433. track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
  3434. track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
  3435. if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
  3436. av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
  3437. goto error;
  3438. }
  3439. track->height = track->tag >> 24 == 'n' ? 486 : 576;
  3440. }
  3441. if (mov->video_track_timescale) {
  3442. track->timescale = mov->video_track_timescale;
  3443. } else {
  3444. track->timescale = st->codec->time_base.den;
  3445. while(track->timescale < 10000)
  3446. track->timescale *= 2;
  3447. }
  3448. if (track->mode == MODE_MOV && track->timescale > 100000)
  3449. av_log(s, AV_LOG_WARNING,
  3450. "WARNING codec timebase is very high. If duration is too long,\n"
  3451. "file may not be playable by quicktime. Specify a shorter timebase\n"
  3452. "or choose different container.\n");
  3453. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3454. track->timescale = st->codec->sample_rate;
  3455. if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
  3456. av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
  3457. track->audio_vbr = 1;
  3458. }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
  3459. st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  3460. st->codec->codec_id == AV_CODEC_ID_ILBC){
  3461. if (!st->codec->block_align) {
  3462. av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
  3463. goto error;
  3464. }
  3465. track->sample_size = st->codec->block_align;
  3466. }else if (st->codec->frame_size > 1){ /* assume compressed audio */
  3467. track->audio_vbr = 1;
  3468. }else{
  3469. track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
  3470. }
  3471. if (st->codec->codec_id == AV_CODEC_ID_ILBC) {
  3472. track->audio_vbr = 1;
  3473. }
  3474. if (track->mode != MODE_MOV &&
  3475. track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
  3476. av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
  3477. i, track->enc->sample_rate);
  3478. goto error;
  3479. }
  3480. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  3481. track->timescale = st->codec->time_base.den;
  3482. } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
  3483. track->timescale = st->codec->time_base.den;
  3484. } else {
  3485. track->timescale = MOV_TIMESCALE;
  3486. }
  3487. if (!track->height)
  3488. track->height = st->codec->height;
  3489. /* The ism specific timescale isn't mandatory, but is assumed by
  3490. * some tools, such as mp4split. */
  3491. if (mov->mode == MODE_ISM)
  3492. track->timescale = 10000000;
  3493. avpriv_set_pts_info(st, 64, 1, track->timescale);
  3494. /* copy extradata if it exists */
  3495. if (st->codec->extradata_size) {
  3496. track->vos_len = st->codec->extradata_size;
  3497. track->vos_data = av_malloc(track->vos_len);
  3498. memcpy(track->vos_data, st->codec->extradata, track->vos_len);
  3499. }
  3500. }
  3501. enable_tracks(s);
  3502. if (mov->mode == MODE_ISM) {
  3503. /* If no fragmentation options have been set, set a default. */
  3504. if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
  3505. FF_MOV_FLAG_FRAG_CUSTOM)) &&
  3506. !mov->max_fragment_duration && !mov->max_fragment_size)
  3507. mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
  3508. }
  3509. if (mov->reserved_moov_size){
  3510. mov->reserved_moov_pos= avio_tell(pb);
  3511. if (mov->reserved_moov_size > 0)
  3512. avio_skip(pb, mov->reserved_moov_size);
  3513. }
  3514. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  3515. if (mov->flags & FF_MOV_FLAG_FASTSTART)
  3516. mov->reserved_moov_pos = avio_tell(pb);
  3517. mov_write_mdat_tag(pb, mov);
  3518. }
  3519. if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
  3520. mov->time = ff_iso8601_to_unix_time(t->value);
  3521. if (mov->time)
  3522. mov->time += 0x7C25B080; // 1970 based -> 1904 based
  3523. if (mov->chapter_track)
  3524. if (mov_create_chapter_track(s, mov->chapter_track) < 0)
  3525. goto error;
  3526. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  3527. /* Initialize the hint tracks for each audio and video stream */
  3528. for (i = 0; i < s->nb_streams; i++) {
  3529. AVStream *st = s->streams[i];
  3530. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  3531. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3532. if (ff_mov_init_hinting(s, hint_track, i) < 0)
  3533. goto error;
  3534. hint_track++;
  3535. }
  3536. }
  3537. }
  3538. if (mov->nb_meta_tmcd) {
  3539. /* Initialize the tmcd tracks */
  3540. for (i = 0; i < s->nb_streams; i++) {
  3541. AVStream *st = s->streams[i];
  3542. t = global_tcr;
  3543. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3544. if (!t)
  3545. t = av_dict_get(st->metadata, "timecode", NULL, 0);
  3546. if (!t)
  3547. continue;
  3548. if (mov_create_timecode_track(s, tmcd_track, i, t->value) < 0)
  3549. goto error;
  3550. tmcd_track++;
  3551. }
  3552. }
  3553. }
  3554. avio_flush(pb);
  3555. if (mov->flags & FF_MOV_FLAG_ISML)
  3556. mov_write_isml_manifest(pb, mov);
  3557. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  3558. mov_write_moov_tag(pb, mov, s);
  3559. mov->fragments++;
  3560. }
  3561. return 0;
  3562. error:
  3563. mov_free(s);
  3564. return -1;
  3565. }
  3566. static int get_moov_size(AVFormatContext *s)
  3567. {
  3568. int ret;
  3569. AVIOContext *moov_buf;
  3570. MOVMuxContext *mov = s->priv_data;
  3571. if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
  3572. return ret;
  3573. mov_write_moov_tag(moov_buf, mov, s);
  3574. return ffio_close_null_buf(moov_buf);
  3575. }
  3576. /*
  3577. * This function gets the moov size if moved to the top of the file: the chunk
  3578. * offset table can switch between stco (32-bit entries) to co64 (64-bit
  3579. * entries) when the moov is moved to the beginning, so the size of the moov
  3580. * would change. It also updates the chunk offset tables.
  3581. */
  3582. static int compute_moov_size(AVFormatContext *s)
  3583. {
  3584. int i, moov_size, moov_size2;
  3585. MOVMuxContext *mov = s->priv_data;
  3586. moov_size = get_moov_size(s);
  3587. if (moov_size < 0)
  3588. return moov_size;
  3589. for (i = 0; i < mov->nb_streams; i++)
  3590. mov->tracks[i].data_offset += moov_size;
  3591. moov_size2 = get_moov_size(s);
  3592. if (moov_size2 < 0)
  3593. return moov_size2;
  3594. /* if the size changed, we just switched from stco to co64 and need to
  3595. * update the offsets */
  3596. if (moov_size2 != moov_size)
  3597. for (i = 0; i < mov->nb_streams; i++)
  3598. mov->tracks[i].data_offset += moov_size2 - moov_size;
  3599. return moov_size2;
  3600. }
  3601. static int shift_data(AVFormatContext *s)
  3602. {
  3603. int ret = 0, moov_size;
  3604. MOVMuxContext *mov = s->priv_data;
  3605. int64_t pos, pos_end = avio_tell(s->pb);
  3606. uint8_t *buf, *read_buf[2];
  3607. int read_buf_id = 0;
  3608. int read_size[2];
  3609. AVIOContext *read_pb;
  3610. moov_size = compute_moov_size(s);
  3611. if (moov_size < 0)
  3612. return moov_size;
  3613. buf = av_malloc(moov_size * 2);
  3614. if (!buf)
  3615. return AVERROR(ENOMEM);
  3616. read_buf[0] = buf;
  3617. read_buf[1] = buf + moov_size;
  3618. /* Shift the data: the AVIO context of the output can only be used for
  3619. * writing, so we re-open the same output, but for reading. It also avoids
  3620. * a read/seek/write/seek back and forth. */
  3621. avio_flush(s->pb);
  3622. ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
  3623. if (ret < 0) {
  3624. av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
  3625. "the second pass (faststart)\n", s->filename);
  3626. goto end;
  3627. }
  3628. /* mark the end of the shift to up to the last data we wrote, and get ready
  3629. * for writing */
  3630. pos_end = avio_tell(s->pb);
  3631. avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
  3632. /* start reading at where the new moov will be placed */
  3633. avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
  3634. pos = avio_tell(read_pb);
  3635. #define READ_BLOCK do { \
  3636. read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
  3637. read_buf_id ^= 1; \
  3638. } while (0)
  3639. /* shift data by chunk of at most moov_size */
  3640. READ_BLOCK;
  3641. do {
  3642. int n;
  3643. READ_BLOCK;
  3644. n = read_size[read_buf_id];
  3645. if (n <= 0)
  3646. break;
  3647. avio_write(s->pb, read_buf[read_buf_id], n);
  3648. pos += n;
  3649. } while (pos < pos_end);
  3650. avio_close(read_pb);
  3651. end:
  3652. av_free(buf);
  3653. return ret;
  3654. }
  3655. static int mov_write_trailer(AVFormatContext *s)
  3656. {
  3657. MOVMuxContext *mov = s->priv_data;
  3658. AVIOContext *pb = s->pb;
  3659. int res = 0;
  3660. int i;
  3661. int64_t moov_pos;
  3662. /*
  3663. * Before actually writing the trailer, make sure that there are no
  3664. * dangling subtitles, that need a terminating sample.
  3665. */
  3666. for (i = 0; i < mov->nb_streams; i++) {
  3667. MOVTrack *trk = &mov->tracks[i];
  3668. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  3669. !trk->last_sample_is_subtitle_end) {
  3670. mov_write_subtitle_end_packet(s, i, trk->track_duration);
  3671. trk->last_sample_is_subtitle_end = 1;
  3672. }
  3673. }
  3674. // If there were no chapters when the header was written, but there
  3675. // are chapters now, write them in the trailer. This only works
  3676. // when we are not doing fragments.
  3677. if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  3678. if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
  3679. mov->chapter_track = mov->nb_streams++;
  3680. if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
  3681. goto error;
  3682. }
  3683. }
  3684. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  3685. moov_pos = avio_tell(pb);
  3686. /* Write size of mdat tag */
  3687. if (mov->mdat_size + 8 <= UINT32_MAX) {
  3688. avio_seek(pb, mov->mdat_pos, SEEK_SET);
  3689. avio_wb32(pb, mov->mdat_size + 8);
  3690. } else {
  3691. /* overwrite 'wide' placeholder atom */
  3692. avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
  3693. /* special value: real atom size will be 64 bit value after
  3694. * tag field */
  3695. avio_wb32(pb, 1);
  3696. ffio_wfourcc(pb, "mdat");
  3697. avio_wb64(pb, mov->mdat_size + 16);
  3698. }
  3699. avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
  3700. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  3701. av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
  3702. res = shift_data(s);
  3703. if (res == 0) {
  3704. avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
  3705. mov_write_moov_tag(pb, mov, s);
  3706. }
  3707. } else if (mov->reserved_moov_size > 0) {
  3708. int64_t size;
  3709. mov_write_moov_tag(pb, mov, s);
  3710. size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
  3711. if (size < 8){
  3712. av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
  3713. return -1;
  3714. }
  3715. avio_wb32(pb, size);
  3716. ffio_wfourcc(pb, "free");
  3717. for (i = 0; i < size; i++)
  3718. avio_w8(pb, 0);
  3719. avio_seek(pb, moov_pos, SEEK_SET);
  3720. } else {
  3721. mov_write_moov_tag(pb, mov, s);
  3722. }
  3723. } else {
  3724. mov_flush_fragment(s);
  3725. mov_write_mfra_tag(pb, mov);
  3726. }
  3727. for (i = 0; i < mov->nb_streams; i++) {
  3728. if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
  3729. mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
  3730. int64_t off = avio_tell(pb);
  3731. uint8_t buf[7];
  3732. if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
  3733. avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
  3734. avio_write(pb, buf, 7);
  3735. avio_seek(pb, off, SEEK_SET);
  3736. }
  3737. }
  3738. }
  3739. error:
  3740. mov_free(s);
  3741. return res;
  3742. }
  3743. #if CONFIG_MOV_MUXER
  3744. MOV_CLASS(mov)
  3745. AVOutputFormat ff_mov_muxer = {
  3746. .name = "mov",
  3747. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  3748. .extensions = "mov",
  3749. .priv_data_size = sizeof(MOVMuxContext),
  3750. .audio_codec = AV_CODEC_ID_AAC,
  3751. .video_codec = CONFIG_LIBX264_ENCODER ?
  3752. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3753. .write_header = mov_write_header,
  3754. .write_packet = mov_write_packet,
  3755. .write_trailer = mov_write_trailer,
  3756. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3757. .codec_tag = (const AVCodecTag* const []){
  3758. ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
  3759. },
  3760. .priv_class = &mov_muxer_class,
  3761. };
  3762. #endif
  3763. #if CONFIG_TGP_MUXER
  3764. MOV_CLASS(tgp)
  3765. AVOutputFormat ff_tgp_muxer = {
  3766. .name = "3gp",
  3767. .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
  3768. .extensions = "3gp",
  3769. .priv_data_size = sizeof(MOVMuxContext),
  3770. .audio_codec = AV_CODEC_ID_AMR_NB,
  3771. .video_codec = AV_CODEC_ID_H263,
  3772. .write_header = mov_write_header,
  3773. .write_packet = mov_write_packet,
  3774. .write_trailer = mov_write_trailer,
  3775. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3776. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  3777. .priv_class = &tgp_muxer_class,
  3778. };
  3779. #endif
  3780. #if CONFIG_MP4_MUXER
  3781. MOV_CLASS(mp4)
  3782. AVOutputFormat ff_mp4_muxer = {
  3783. .name = "mp4",
  3784. .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
  3785. .mime_type = "application/mp4",
  3786. .extensions = "mp4",
  3787. .priv_data_size = sizeof(MOVMuxContext),
  3788. .audio_codec = AV_CODEC_ID_AAC,
  3789. .video_codec = CONFIG_LIBX264_ENCODER ?
  3790. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3791. .write_header = mov_write_header,
  3792. .write_packet = mov_write_packet,
  3793. .write_trailer = mov_write_trailer,
  3794. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3795. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3796. .priv_class = &mp4_muxer_class,
  3797. };
  3798. #endif
  3799. #if CONFIG_PSP_MUXER
  3800. MOV_CLASS(psp)
  3801. AVOutputFormat ff_psp_muxer = {
  3802. .name = "psp",
  3803. .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
  3804. .extensions = "mp4,psp",
  3805. .priv_data_size = sizeof(MOVMuxContext),
  3806. .audio_codec = AV_CODEC_ID_AAC,
  3807. .video_codec = CONFIG_LIBX264_ENCODER ?
  3808. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3809. .write_header = mov_write_header,
  3810. .write_packet = mov_write_packet,
  3811. .write_trailer = mov_write_trailer,
  3812. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3813. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3814. .priv_class = &psp_muxer_class,
  3815. };
  3816. #endif
  3817. #if CONFIG_TG2_MUXER
  3818. MOV_CLASS(tg2)
  3819. AVOutputFormat ff_tg2_muxer = {
  3820. .name = "3g2",
  3821. .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
  3822. .extensions = "3g2",
  3823. .priv_data_size = sizeof(MOVMuxContext),
  3824. .audio_codec = AV_CODEC_ID_AMR_NB,
  3825. .video_codec = AV_CODEC_ID_H263,
  3826. .write_header = mov_write_header,
  3827. .write_packet = mov_write_packet,
  3828. .write_trailer = mov_write_trailer,
  3829. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3830. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  3831. .priv_class = &tg2_muxer_class,
  3832. };
  3833. #endif
  3834. #if CONFIG_IPOD_MUXER
  3835. MOV_CLASS(ipod)
  3836. AVOutputFormat ff_ipod_muxer = {
  3837. .name = "ipod",
  3838. .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
  3839. .mime_type = "application/mp4",
  3840. .extensions = "m4v,m4a",
  3841. .priv_data_size = sizeof(MOVMuxContext),
  3842. .audio_codec = AV_CODEC_ID_AAC,
  3843. .video_codec = AV_CODEC_ID_H264,
  3844. .write_header = mov_write_header,
  3845. .write_packet = mov_write_packet,
  3846. .write_trailer = mov_write_trailer,
  3847. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3848. .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
  3849. .priv_class = &ipod_muxer_class,
  3850. };
  3851. #endif
  3852. #if CONFIG_ISMV_MUXER
  3853. MOV_CLASS(ismv)
  3854. AVOutputFormat ff_ismv_muxer = {
  3855. .name = "ismv",
  3856. .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
  3857. .mime_type = "application/mp4",
  3858. .extensions = "ismv,isma",
  3859. .priv_data_size = sizeof(MOVMuxContext),
  3860. .audio_codec = AV_CODEC_ID_AAC,
  3861. .video_codec = AV_CODEC_ID_H264,
  3862. .write_header = mov_write_header,
  3863. .write_packet = mov_write_packet,
  3864. .write_trailer = mov_write_trailer,
  3865. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3866. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3867. .priv_class = &ismv_muxer_class,
  3868. };
  3869. #endif
  3870. #if CONFIG_F4V_MUXER
  3871. MOV_CLASS(f4v)
  3872. AVOutputFormat ff_f4v_muxer = {
  3873. .name = "f4v",
  3874. .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
  3875. .mime_type = "application/f4v",
  3876. .extensions = "f4v",
  3877. .priv_data_size = sizeof(MOVMuxContext),
  3878. .audio_codec = AV_CODEC_ID_AAC,
  3879. .video_codec = AV_CODEC_ID_H264,
  3880. .write_header = mov_write_header,
  3881. .write_packet = mov_write_packet,
  3882. .write_trailer = mov_write_trailer,
  3883. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3884. .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
  3885. .priv_class = &f4v_muxer_class,
  3886. };
  3887. #endif