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.

4340 lines
152KB

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