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.

5681 lines
203KB

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