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.

4392 lines
155KB

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