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.

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