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.

5257 lines
187KB

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