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.

5671 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. if (st) {
  2054. if (mov->per_stream_grouping)
  2055. group = st->index;
  2056. else
  2057. group = st->codec->codec_type;
  2058. }
  2059. if (track->flags & MOV_TRACK_ENABLED)
  2060. flags |= MOV_TKHD_FLAG_ENABLED;
  2061. if (track->mode == MODE_ISM)
  2062. version = 1;
  2063. (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
  2064. ffio_wfourcc(pb, "tkhd");
  2065. avio_w8(pb, version);
  2066. avio_wb24(pb, flags);
  2067. if (version == 1) {
  2068. avio_wb64(pb, track->time);
  2069. avio_wb64(pb, track->time);
  2070. } else {
  2071. avio_wb32(pb, track->time); /* creation time */
  2072. avio_wb32(pb, track->time); /* modification time */
  2073. }
  2074. avio_wb32(pb, track->track_id); /* track-id */
  2075. avio_wb32(pb, 0); /* reserved */
  2076. if (!track->entry && mov->mode == MODE_ISM)
  2077. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  2078. else if (!track->entry)
  2079. (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);
  2080. else
  2081. (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
  2082. avio_wb32(pb, 0); /* reserved */
  2083. avio_wb32(pb, 0); /* reserved */
  2084. avio_wb16(pb, 0); /* layer */
  2085. avio_wb16(pb, group); /* alternate group) */
  2086. /* Volume, only for audio */
  2087. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  2088. avio_wb16(pb, 0x0100);
  2089. else
  2090. avio_wb16(pb, 0);
  2091. avio_wb16(pb, 0); /* reserved */
  2092. /* Matrix structure */
  2093. if (st && st->metadata) {
  2094. AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
  2095. rotation = (rot && rot->value) ? atoi(rot->value) : 0;
  2096. }
  2097. if (rotation == 90) {
  2098. write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
  2099. } else if (rotation == 180) {
  2100. write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
  2101. } else if (rotation == 270) {
  2102. write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
  2103. } else {
  2104. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  2105. }
  2106. /* Track width and height, for visual only */
  2107. if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  2108. track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
  2109. if (track->mode == MODE_MOV) {
  2110. avio_wb32(pb, track->enc->width << 16);
  2111. avio_wb32(pb, track->height << 16);
  2112. } else {
  2113. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  2114. if (!sample_aspect_ratio || track->height != track->enc->height)
  2115. sample_aspect_ratio = 1;
  2116. avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
  2117. avio_wb32(pb, track->height * 0x10000);
  2118. }
  2119. } else {
  2120. avio_wb32(pb, 0);
  2121. avio_wb32(pb, 0);
  2122. }
  2123. return 0x5c;
  2124. }
  2125. static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
  2126. {
  2127. int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
  2128. track->enc->sample_aspect_ratio.den);
  2129. int64_t pos = avio_tell(pb);
  2130. avio_wb32(pb, 0); /* size */
  2131. ffio_wfourcc(pb, "tapt");
  2132. avio_wb32(pb, 20);
  2133. ffio_wfourcc(pb, "clef");
  2134. avio_wb32(pb, 0);
  2135. avio_wb32(pb, width << 16);
  2136. avio_wb32(pb, track->enc->height << 16);
  2137. avio_wb32(pb, 20);
  2138. ffio_wfourcc(pb, "prof");
  2139. avio_wb32(pb, 0);
  2140. avio_wb32(pb, width << 16);
  2141. avio_wb32(pb, track->enc->height << 16);
  2142. avio_wb32(pb, 20);
  2143. ffio_wfourcc(pb, "enof");
  2144. avio_wb32(pb, 0);
  2145. avio_wb32(pb, track->enc->width << 16);
  2146. avio_wb32(pb, track->enc->height << 16);
  2147. return update_size(pb, pos);
  2148. }
  2149. // This box seems important for the psp playback ... without it the movie seems to hang
  2150. static int mov_write_edts_tag(AVIOContext *pb, MOVMuxContext *mov,
  2151. MOVTrack *track)
  2152. {
  2153. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  2154. track->timescale, AV_ROUND_UP);
  2155. int version = duration < INT32_MAX ? 0 : 1;
  2156. int entry_size, entry_count, size;
  2157. int64_t delay, start_ct = track->start_cts;
  2158. int64_t start_dts = track->start_dts;
  2159. if (track->entry) {
  2160. if (start_dts != track->cluster[0].dts || start_ct != track->cluster[0].cts) {
  2161. av_log(mov->fc, AV_LOG_DEBUG,
  2162. "EDTS using dts:%"PRId64" cts:%d instead of dts:%"PRId64" cts:%"PRId64" tid:%d\n",
  2163. track->cluster[0].dts, track->cluster[0].cts,
  2164. start_dts, start_ct, track->track_id);
  2165. start_dts = track->cluster[0].dts;
  2166. start_ct = track->cluster[0].cts;
  2167. }
  2168. }
  2169. delay = av_rescale_rnd(start_dts + start_ct, MOV_TIMESCALE,
  2170. track->timescale, AV_ROUND_DOWN);
  2171. version |= delay < INT32_MAX ? 0 : 1;
  2172. entry_size = (version == 1) ? 20 : 12;
  2173. entry_count = 1 + (delay > 0);
  2174. size = 24 + entry_count * entry_size;
  2175. /* write the atom data */
  2176. avio_wb32(pb, size);
  2177. ffio_wfourcc(pb, "edts");
  2178. avio_wb32(pb, size - 8);
  2179. ffio_wfourcc(pb, "elst");
  2180. avio_w8(pb, version);
  2181. avio_wb24(pb, 0); /* flags */
  2182. avio_wb32(pb, entry_count);
  2183. if (delay > 0) { /* add an empty edit to delay presentation */
  2184. /* In the positive delay case, the delay includes the cts
  2185. * offset, and the second edit list entry below trims out
  2186. * the same amount from the actual content. This makes sure
  2187. * that the offsetted last sample is included in the edit
  2188. * list duration as well. */
  2189. if (version == 1) {
  2190. avio_wb64(pb, delay);
  2191. avio_wb64(pb, -1);
  2192. } else {
  2193. avio_wb32(pb, delay);
  2194. avio_wb32(pb, -1);
  2195. }
  2196. avio_wb32(pb, 0x00010000);
  2197. } else {
  2198. /* Avoid accidentally ending up with start_ct = -1 which has got a
  2199. * special meaning. Normally start_ct should end up positive or zero
  2200. * here, but use FFMIN in case dts is a a small positive integer
  2201. * rounded to 0 when represented in MOV_TIMESCALE units. */
  2202. av_assert0(av_rescale_rnd(start_dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
  2203. start_ct = -FFMIN(start_dts, 0);
  2204. /* Note, this delay is calculated from the pts of the first sample,
  2205. * ensuring that we don't reduce the duration for cases with
  2206. * dts<0 pts=0. */
  2207. duration += delay;
  2208. }
  2209. /* For fragmented files, we don't know the full length yet. Setting
  2210. * duration to 0 allows us to only specify the offset, including
  2211. * the rest of the content (from all future fragments) without specifying
  2212. * an explicit duration. */
  2213. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  2214. duration = 0;
  2215. /* duration */
  2216. if (version == 1) {
  2217. avio_wb64(pb, duration);
  2218. avio_wb64(pb, start_ct);
  2219. } else {
  2220. avio_wb32(pb, duration);
  2221. avio_wb32(pb, start_ct);
  2222. }
  2223. avio_wb32(pb, 0x00010000);
  2224. return size;
  2225. }
  2226. static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
  2227. {
  2228. avio_wb32(pb, 20); // size
  2229. ffio_wfourcc(pb, "tref");
  2230. avio_wb32(pb, 12); // size (subatom)
  2231. avio_wl32(pb, track->tref_tag);
  2232. avio_wb32(pb, track->tref_id);
  2233. return 20;
  2234. }
  2235. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  2236. static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
  2237. {
  2238. avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  2239. ffio_wfourcc(pb, "uuid");
  2240. ffio_wfourcc(pb, "USMT");
  2241. avio_wb32(pb, 0x21d24fce);
  2242. avio_wb32(pb, 0xbb88695c);
  2243. avio_wb32(pb, 0xfac9c740);
  2244. avio_wb32(pb, 0x1c); // another size here!
  2245. ffio_wfourcc(pb, "MTDT");
  2246. avio_wb32(pb, 0x00010012);
  2247. avio_wb32(pb, 0x0a);
  2248. avio_wb32(pb, 0x55c40000);
  2249. avio_wb32(pb, 0x1);
  2250. avio_wb32(pb, 0x0);
  2251. return 0x34;
  2252. }
  2253. static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
  2254. {
  2255. AVFormatContext *ctx = track->rtp_ctx;
  2256. char buf[1000] = "";
  2257. int len;
  2258. ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
  2259. NULL, NULL, 0, 0, ctx);
  2260. av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
  2261. len = strlen(buf);
  2262. avio_wb32(pb, len + 24);
  2263. ffio_wfourcc(pb, "udta");
  2264. avio_wb32(pb, len + 16);
  2265. ffio_wfourcc(pb, "hnti");
  2266. avio_wb32(pb, len + 8);
  2267. ffio_wfourcc(pb, "sdp ");
  2268. avio_write(pb, buf, len);
  2269. return len + 24;
  2270. }
  2271. static int mov_write_track_metadata(AVIOContext *pb, AVStream *st,
  2272. const char *tag, const char *str)
  2273. {
  2274. int64_t pos = avio_tell(pb);
  2275. AVDictionaryEntry *t = av_dict_get(st->metadata, str, NULL, 0);
  2276. if (!t || !utf8len(t->value))
  2277. return 0;
  2278. avio_wb32(pb, 0); /* size */
  2279. ffio_wfourcc(pb, tag); /* type */
  2280. avio_write(pb, t->value, strlen(t->value)); /* UTF8 string value */
  2281. return update_size(pb, pos);
  2282. }
  2283. static int mov_write_track_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
  2284. AVStream *st)
  2285. {
  2286. AVIOContext *pb_buf;
  2287. int ret, size;
  2288. uint8_t *buf;
  2289. if (!st || mov->fc->flags & AVFMT_FLAG_BITEXACT)
  2290. return 0;
  2291. ret = avio_open_dyn_buf(&pb_buf);
  2292. if (ret < 0)
  2293. return ret;
  2294. if (mov->mode & MODE_MP4)
  2295. mov_write_track_metadata(pb_buf, st, "name", "title");
  2296. if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
  2297. avio_wb32(pb, size + 8);
  2298. ffio_wfourcc(pb, "udta");
  2299. avio_write(pb, buf, size);
  2300. }
  2301. av_free(buf);
  2302. return 0;
  2303. }
  2304. static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
  2305. MOVTrack *track, AVStream *st)
  2306. {
  2307. int64_t pos = avio_tell(pb);
  2308. int entry_backup = track->entry;
  2309. int chunk_backup = track->chunkCount;
  2310. int ret;
  2311. /* If we want to have an empty moov, but some samples already have been
  2312. * buffered (delay_moov), pretend that no samples have been written yet. */
  2313. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV)
  2314. track->chunkCount = track->entry = 0;
  2315. avio_wb32(pb, 0); /* size */
  2316. ffio_wfourcc(pb, "trak");
  2317. mov_write_tkhd_tag(pb, mov, track, st);
  2318. av_assert2(mov->use_editlist >= 0);
  2319. if (track->start_dts != AV_NOPTS_VALUE) {
  2320. if (mov->use_editlist)
  2321. mov_write_edts_tag(pb, mov, track); // PSP Movies and several other cases require edts box
  2322. else if ((track->entry && track->cluster[0].dts) || track->mode == MODE_PSP || is_clcp_track(track))
  2323. av_log(mov->fc, AV_LOG_WARNING,
  2324. "Not writing any edit list even though one would have been required\n");
  2325. }
  2326. if (track->tref_tag)
  2327. mov_write_tref_tag(pb, track);
  2328. if ((ret = mov_write_mdia_tag(pb, mov, track)) < 0)
  2329. return ret;
  2330. if (track->mode == MODE_PSP)
  2331. mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
  2332. if (track->tag == MKTAG('r','t','p',' '))
  2333. mov_write_udta_sdp(pb, track);
  2334. if (track->mode == MODE_MOV) {
  2335. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2336. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  2337. if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio) {
  2338. mov_write_tapt_tag(pb, track);
  2339. }
  2340. }
  2341. if (is_clcp_track(track) && st->sample_aspect_ratio.num) {
  2342. mov_write_tapt_tag(pb, track);
  2343. }
  2344. }
  2345. mov_write_track_udta_tag(pb, mov, st);
  2346. track->entry = entry_backup;
  2347. track->chunkCount = chunk_backup;
  2348. return update_size(pb, pos);
  2349. }
  2350. static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
  2351. {
  2352. int i, has_audio = 0, has_video = 0;
  2353. int64_t pos = avio_tell(pb);
  2354. int audio_profile = mov->iods_audio_profile;
  2355. int video_profile = mov->iods_video_profile;
  2356. for (i = 0; i < mov->nb_streams; i++) {
  2357. if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  2358. has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
  2359. has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
  2360. }
  2361. }
  2362. if (audio_profile < 0)
  2363. audio_profile = 0xFF - has_audio;
  2364. if (video_profile < 0)
  2365. video_profile = 0xFF - has_video;
  2366. avio_wb32(pb, 0x0); /* size */
  2367. ffio_wfourcc(pb, "iods");
  2368. avio_wb32(pb, 0); /* version & flags */
  2369. put_descr(pb, 0x10, 7);
  2370. avio_wb16(pb, 0x004f);
  2371. avio_w8(pb, 0xff);
  2372. avio_w8(pb, 0xff);
  2373. avio_w8(pb, audio_profile);
  2374. avio_w8(pb, video_profile);
  2375. avio_w8(pb, 0xff);
  2376. return update_size(pb, pos);
  2377. }
  2378. static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
  2379. {
  2380. avio_wb32(pb, 0x20); /* size */
  2381. ffio_wfourcc(pb, "trex");
  2382. avio_wb32(pb, 0); /* version & flags */
  2383. avio_wb32(pb, track->track_id); /* track ID */
  2384. avio_wb32(pb, 1); /* default sample description index */
  2385. avio_wb32(pb, 0); /* default sample duration */
  2386. avio_wb32(pb, 0); /* default sample size */
  2387. avio_wb32(pb, 0); /* default sample flags */
  2388. return 0;
  2389. }
  2390. static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
  2391. {
  2392. int64_t pos = avio_tell(pb);
  2393. int i;
  2394. avio_wb32(pb, 0x0); /* size */
  2395. ffio_wfourcc(pb, "mvex");
  2396. for (i = 0; i < mov->nb_streams; i++)
  2397. mov_write_trex_tag(pb, &mov->tracks[i]);
  2398. return update_size(pb, pos);
  2399. }
  2400. static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  2401. {
  2402. int max_track_id = 1, i;
  2403. int64_t max_track_len = 0;
  2404. int version;
  2405. for (i = 0; i < mov->nb_streams; i++) {
  2406. if (mov->tracks[i].entry > 0 && mov->tracks[i].timescale) {
  2407. int64_t max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
  2408. MOV_TIMESCALE,
  2409. mov->tracks[i].timescale,
  2410. AV_ROUND_UP);
  2411. if (max_track_len < max_track_len_temp)
  2412. max_track_len = max_track_len_temp;
  2413. if (max_track_id < mov->tracks[i].track_id)
  2414. max_track_id = mov->tracks[i].track_id;
  2415. }
  2416. }
  2417. /* If using delay_moov, make sure the output is the same as if no
  2418. * samples had been written yet. */
  2419. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  2420. max_track_len = 0;
  2421. max_track_id = 1;
  2422. }
  2423. version = max_track_len < UINT32_MAX ? 0 : 1;
  2424. avio_wb32(pb, version == 1 ? 120 : 108); /* size */
  2425. ffio_wfourcc(pb, "mvhd");
  2426. avio_w8(pb, version);
  2427. avio_wb24(pb, 0); /* flags */
  2428. if (version == 1) {
  2429. avio_wb64(pb, mov->time);
  2430. avio_wb64(pb, mov->time);
  2431. } else {
  2432. avio_wb32(pb, mov->time); /* creation time */
  2433. avio_wb32(pb, mov->time); /* modification time */
  2434. }
  2435. avio_wb32(pb, MOV_TIMESCALE);
  2436. (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
  2437. avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  2438. avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  2439. avio_wb16(pb, 0); /* reserved */
  2440. avio_wb32(pb, 0); /* reserved */
  2441. avio_wb32(pb, 0); /* reserved */
  2442. /* Matrix structure */
  2443. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  2444. avio_wb32(pb, 0); /* reserved (preview time) */
  2445. avio_wb32(pb, 0); /* reserved (preview duration) */
  2446. avio_wb32(pb, 0); /* reserved (poster time) */
  2447. avio_wb32(pb, 0); /* reserved (selection time) */
  2448. avio_wb32(pb, 0); /* reserved (selection duration) */
  2449. avio_wb32(pb, 0); /* reserved (current time) */
  2450. avio_wb32(pb, max_track_id + 1); /* Next track id */
  2451. return 0x6c;
  2452. }
  2453. static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
  2454. AVFormatContext *s)
  2455. {
  2456. avio_wb32(pb, 33); /* size */
  2457. ffio_wfourcc(pb, "hdlr");
  2458. avio_wb32(pb, 0);
  2459. avio_wb32(pb, 0);
  2460. ffio_wfourcc(pb, "mdir");
  2461. ffio_wfourcc(pb, "appl");
  2462. avio_wb32(pb, 0);
  2463. avio_wb32(pb, 0);
  2464. avio_w8(pb, 0);
  2465. return 33;
  2466. }
  2467. /* helper function to write a data tag with the specified string as data */
  2468. static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
  2469. {
  2470. if (long_style) {
  2471. int size = 16 + strlen(data);
  2472. avio_wb32(pb, size); /* size */
  2473. ffio_wfourcc(pb, "data");
  2474. avio_wb32(pb, 1);
  2475. avio_wb32(pb, 0);
  2476. avio_write(pb, data, strlen(data));
  2477. return size;
  2478. } else {
  2479. if (!lang)
  2480. lang = ff_mov_iso639_to_lang("und", 1);
  2481. avio_wb16(pb, strlen(data)); /* string length */
  2482. avio_wb16(pb, lang);
  2483. avio_write(pb, data, strlen(data));
  2484. return strlen(data) + 4;
  2485. }
  2486. }
  2487. static int mov_write_string_tag(AVIOContext *pb, const char *name,
  2488. const char *value, int lang, int long_style)
  2489. {
  2490. int size = 0;
  2491. if (value && value[0]) {
  2492. int64_t pos = avio_tell(pb);
  2493. avio_wb32(pb, 0); /* size */
  2494. ffio_wfourcc(pb, name);
  2495. mov_write_string_data_tag(pb, value, lang, long_style);
  2496. size = update_size(pb, pos);
  2497. }
  2498. return size;
  2499. }
  2500. static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
  2501. const char *name, const char *tag,
  2502. int long_style)
  2503. {
  2504. int l, lang = 0, len, len2;
  2505. AVDictionaryEntry *t, *t2 = NULL;
  2506. char tag2[16];
  2507. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  2508. return 0;
  2509. len = strlen(t->key);
  2510. snprintf(tag2, sizeof(tag2), "%s-", tag);
  2511. while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
  2512. len2 = strlen(t2->key);
  2513. if (len2 == len + 4 && !strcmp(t->value, t2->value)
  2514. && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
  2515. lang = l;
  2516. break;
  2517. }
  2518. }
  2519. return mov_write_string_tag(pb, name, t->value, lang, long_style);
  2520. }
  2521. /* iTunes bpm number */
  2522. static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
  2523. {
  2524. AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
  2525. int size = 0, tmpo = t ? atoi(t->value) : 0;
  2526. if (tmpo) {
  2527. size = 26;
  2528. avio_wb32(pb, size);
  2529. ffio_wfourcc(pb, "tmpo");
  2530. avio_wb32(pb, size-8); /* size */
  2531. ffio_wfourcc(pb, "data");
  2532. avio_wb32(pb, 0x15); //type specifier
  2533. avio_wb32(pb, 0);
  2534. avio_wb16(pb, tmpo); // data
  2535. }
  2536. return size;
  2537. }
  2538. /* iTunes track or disc number */
  2539. static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
  2540. AVFormatContext *s, int disc)
  2541. {
  2542. AVDictionaryEntry *t = av_dict_get(s->metadata,
  2543. disc ? "disc" : "track",
  2544. NULL, 0);
  2545. int size = 0, track = t ? atoi(t->value) : 0;
  2546. if (track) {
  2547. int tracks = 0;
  2548. char *slash = strchr(t->value, '/');
  2549. if (slash)
  2550. tracks = atoi(slash + 1);
  2551. avio_wb32(pb, 32); /* size */
  2552. ffio_wfourcc(pb, disc ? "disk" : "trkn");
  2553. avio_wb32(pb, 24); /* size */
  2554. ffio_wfourcc(pb, "data");
  2555. avio_wb32(pb, 0); // 8 bytes empty
  2556. avio_wb32(pb, 0);
  2557. avio_wb16(pb, 0); // empty
  2558. avio_wb16(pb, track); // track / disc number
  2559. avio_wb16(pb, tracks); // total track / disc number
  2560. avio_wb16(pb, 0); // empty
  2561. size = 32;
  2562. }
  2563. return size;
  2564. }
  2565. static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
  2566. const char *name, const char *tag,
  2567. int len)
  2568. {
  2569. AVDictionaryEntry *t = NULL;
  2570. uint8_t num;
  2571. int size = 24 + len;
  2572. if (len != 1 && len != 4)
  2573. return -1;
  2574. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  2575. return 0;
  2576. num = atoi(t->value);
  2577. avio_wb32(pb, size);
  2578. ffio_wfourcc(pb, name);
  2579. avio_wb32(pb, size - 8);
  2580. ffio_wfourcc(pb, "data");
  2581. avio_wb32(pb, 0x15);
  2582. avio_wb32(pb, 0);
  2583. if (len==4) avio_wb32(pb, num);
  2584. else avio_w8 (pb, num);
  2585. return size;
  2586. }
  2587. /* iTunes meta data list */
  2588. static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
  2589. AVFormatContext *s)
  2590. {
  2591. int64_t pos = avio_tell(pb);
  2592. avio_wb32(pb, 0); /* size */
  2593. ffio_wfourcc(pb, "ilst");
  2594. mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
  2595. mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
  2596. mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
  2597. mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
  2598. mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
  2599. mov_write_string_metadata(s, pb, "\251day", "date" , 1);
  2600. if (!mov->exact &&
  2601. !mov_write_string_metadata(s, pb, "\251too", "encoding_tool", 1))
  2602. mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
  2603. mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
  2604. mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
  2605. mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
  2606. mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
  2607. mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
  2608. mov_write_string_metadata(s, pb, "desc", "description",1);
  2609. mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
  2610. mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
  2611. mov_write_string_metadata(s, pb, "tven", "episode_id",1);
  2612. mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
  2613. mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
  2614. mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
  2615. mov_write_int8_metadata (s, pb, "stik", "media_type",1);
  2616. mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
  2617. mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
  2618. mov_write_int8_metadata (s, pb, "cpil", "compilation", 1);
  2619. mov_write_trkn_tag(pb, mov, s, 0); // track number
  2620. mov_write_trkn_tag(pb, mov, s, 1); // disc number
  2621. mov_write_tmpo_tag(pb, s);
  2622. return update_size(pb, pos);
  2623. }
  2624. /* iTunes meta data tag */
  2625. static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
  2626. AVFormatContext *s)
  2627. {
  2628. int size = 0;
  2629. int64_t pos = avio_tell(pb);
  2630. avio_wb32(pb, 0); /* size */
  2631. ffio_wfourcc(pb, "meta");
  2632. avio_wb32(pb, 0);
  2633. mov_write_itunes_hdlr_tag(pb, mov, s);
  2634. mov_write_ilst_tag(pb, mov, s);
  2635. size = update_size(pb, pos);
  2636. return size;
  2637. }
  2638. static int mov_write_raw_metadata_tag(AVFormatContext *s, AVIOContext *pb,
  2639. const char *name, const char *key)
  2640. {
  2641. int len;
  2642. AVDictionaryEntry *t;
  2643. if (!(t = av_dict_get(s->metadata, key, NULL, 0)))
  2644. return 0;
  2645. len = strlen(t->value);
  2646. if (len > 0) {
  2647. int size = len + 8;
  2648. avio_wb32(pb, size);
  2649. ffio_wfourcc(pb, name);
  2650. avio_write(pb, t->value, len);
  2651. return size;
  2652. }
  2653. return 0;
  2654. }
  2655. static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
  2656. {
  2657. int val;
  2658. while (*b) {
  2659. GET_UTF8(val, *b++, return -1;)
  2660. avio_wb16(pb, val);
  2661. }
  2662. avio_wb16(pb, 0x00);
  2663. return 0;
  2664. }
  2665. static uint16_t language_code(const char *str)
  2666. {
  2667. return (((str[0] - 0x60) & 0x1F) << 10) +
  2668. (((str[1] - 0x60) & 0x1F) << 5) +
  2669. (( str[2] - 0x60) & 0x1F);
  2670. }
  2671. static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
  2672. const char *tag, const char *str)
  2673. {
  2674. int64_t pos = avio_tell(pb);
  2675. AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
  2676. if (!t || !utf8len(t->value))
  2677. return 0;
  2678. avio_wb32(pb, 0); /* size */
  2679. ffio_wfourcc(pb, tag); /* type */
  2680. avio_wb32(pb, 0); /* version + flags */
  2681. if (!strcmp(tag, "yrrc"))
  2682. avio_wb16(pb, atoi(t->value));
  2683. else {
  2684. avio_wb16(pb, language_code("eng")); /* language */
  2685. avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
  2686. if (!strcmp(tag, "albm") &&
  2687. (t = av_dict_get(s->metadata, "track", NULL, 0)))
  2688. avio_w8(pb, atoi(t->value));
  2689. }
  2690. return update_size(pb, pos);
  2691. }
  2692. static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
  2693. {
  2694. int64_t pos = avio_tell(pb);
  2695. int i, nb_chapters = FFMIN(s->nb_chapters, 255);
  2696. avio_wb32(pb, 0); // size
  2697. ffio_wfourcc(pb, "chpl");
  2698. avio_wb32(pb, 0x01000000); // version + flags
  2699. avio_wb32(pb, 0); // unknown
  2700. avio_w8(pb, nb_chapters);
  2701. for (i = 0; i < nb_chapters; i++) {
  2702. AVChapter *c = s->chapters[i];
  2703. AVDictionaryEntry *t;
  2704. avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
  2705. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  2706. int len = FFMIN(strlen(t->value), 255);
  2707. avio_w8(pb, len);
  2708. avio_write(pb, t->value, len);
  2709. } else
  2710. avio_w8(pb, 0);
  2711. }
  2712. return update_size(pb, pos);
  2713. }
  2714. static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
  2715. AVFormatContext *s)
  2716. {
  2717. AVIOContext *pb_buf;
  2718. int ret, size;
  2719. uint8_t *buf;
  2720. ret = avio_open_dyn_buf(&pb_buf);
  2721. if (ret < 0)
  2722. return ret;
  2723. if (mov->mode & MODE_3GP) {
  2724. mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
  2725. mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
  2726. mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
  2727. mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
  2728. mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
  2729. mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
  2730. mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
  2731. mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
  2732. } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
  2733. mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
  2734. mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
  2735. mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
  2736. mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
  2737. mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
  2738. mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
  2739. // currently ignored by mov.c
  2740. mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
  2741. // add support for libquicktime, this atom is also actually read by mov.c
  2742. mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
  2743. mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
  2744. mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
  2745. mov_write_raw_metadata_tag(s, pb_buf, "XMP_", "xmp");
  2746. } else {
  2747. /* iTunes meta data */
  2748. mov_write_meta_tag(pb_buf, mov, s);
  2749. }
  2750. if (s->nb_chapters && !(mov->flags & FF_MOV_FLAG_DISABLE_CHPL))
  2751. mov_write_chpl_tag(pb_buf, s);
  2752. if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
  2753. avio_wb32(pb, size + 8);
  2754. ffio_wfourcc(pb, "udta");
  2755. avio_write(pb, buf, size);
  2756. }
  2757. av_free(buf);
  2758. return 0;
  2759. }
  2760. static void mov_write_psp_udta_tag(AVIOContext *pb,
  2761. const char *str, const char *lang, int type)
  2762. {
  2763. int len = utf8len(str) + 1;
  2764. if (len <= 0)
  2765. return;
  2766. avio_wb16(pb, len * 2 + 10); /* size */
  2767. avio_wb32(pb, type); /* type */
  2768. avio_wb16(pb, language_code(lang)); /* language */
  2769. avio_wb16(pb, 0x01); /* ? */
  2770. ascii_to_wc(pb, str);
  2771. }
  2772. static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
  2773. {
  2774. MOVMuxContext *mov = s->priv_data;
  2775. AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
  2776. int64_t pos, pos2;
  2777. if (title) {
  2778. pos = avio_tell(pb);
  2779. avio_wb32(pb, 0); /* size placeholder*/
  2780. ffio_wfourcc(pb, "uuid");
  2781. ffio_wfourcc(pb, "USMT");
  2782. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2783. avio_wb32(pb, 0xbb88695c);
  2784. avio_wb32(pb, 0xfac9c740);
  2785. pos2 = avio_tell(pb);
  2786. avio_wb32(pb, 0); /* size placeholder*/
  2787. ffio_wfourcc(pb, "MTDT");
  2788. avio_wb16(pb, 4);
  2789. // ?
  2790. avio_wb16(pb, 0x0C); /* size */
  2791. avio_wb32(pb, 0x0B); /* type */
  2792. avio_wb16(pb, language_code("und")); /* language */
  2793. avio_wb16(pb, 0x0); /* ? */
  2794. avio_wb16(pb, 0x021C); /* data */
  2795. if (!mov->exact)
  2796. mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
  2797. mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
  2798. mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
  2799. update_size(pb, pos2);
  2800. return update_size(pb, pos);
  2801. }
  2802. return 0;
  2803. }
  2804. static void build_chunks(MOVTrack *trk)
  2805. {
  2806. int i;
  2807. MOVIentry *chunk = &trk->cluster[0];
  2808. uint64_t chunkSize = chunk->size;
  2809. chunk->chunkNum = 1;
  2810. if (trk->chunkCount)
  2811. return;
  2812. trk->chunkCount = 1;
  2813. for (i = 1; i<trk->entry; i++){
  2814. if (chunk->pos + chunkSize == trk->cluster[i].pos &&
  2815. chunkSize + trk->cluster[i].size < (1<<20)){
  2816. chunkSize += trk->cluster[i].size;
  2817. chunk->samples_in_chunk += trk->cluster[i].entries;
  2818. } else {
  2819. trk->cluster[i].chunkNum = chunk->chunkNum+1;
  2820. chunk=&trk->cluster[i];
  2821. chunkSize = chunk->size;
  2822. trk->chunkCount++;
  2823. }
  2824. }
  2825. }
  2826. static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
  2827. AVFormatContext *s)
  2828. {
  2829. int i;
  2830. int64_t pos = avio_tell(pb);
  2831. avio_wb32(pb, 0); /* size placeholder*/
  2832. ffio_wfourcc(pb, "moov");
  2833. for (i = 0; i < mov->nb_streams; i++) {
  2834. if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2835. continue;
  2836. mov->tracks[i].time = mov->time;
  2837. mov->tracks[i].track_id = i + 1;
  2838. if (mov->tracks[i].entry)
  2839. build_chunks(&mov->tracks[i]);
  2840. }
  2841. if (mov->chapter_track)
  2842. for (i = 0; i < s->nb_streams; i++) {
  2843. mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
  2844. mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
  2845. }
  2846. for (i = 0; i < mov->nb_streams; i++) {
  2847. if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
  2848. mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
  2849. mov->tracks[i].tref_id =
  2850. mov->tracks[mov->tracks[i].src_track].track_id;
  2851. }
  2852. }
  2853. for (i = 0; i < mov->nb_streams; i++) {
  2854. if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
  2855. int src_trk = mov->tracks[i].src_track;
  2856. mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
  2857. mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
  2858. //src_trk may have a different timescale than the tmcd track
  2859. mov->tracks[i].track_duration = av_rescale(mov->tracks[src_trk].track_duration,
  2860. mov->tracks[i].timescale,
  2861. mov->tracks[src_trk].timescale);
  2862. }
  2863. }
  2864. mov_write_mvhd_tag(pb, mov);
  2865. if (mov->mode != MODE_MOV && !mov->iods_skip)
  2866. mov_write_iods_tag(pb, mov);
  2867. for (i = 0; i < mov->nb_streams; i++) {
  2868. if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2869. int ret = mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
  2870. if (ret < 0)
  2871. return ret;
  2872. }
  2873. }
  2874. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  2875. mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
  2876. if (mov->mode == MODE_PSP)
  2877. mov_write_uuidusmt_tag(pb, s);
  2878. else
  2879. mov_write_udta_tag(pb, mov, s);
  2880. return update_size(pb, pos);
  2881. }
  2882. static void param_write_int(AVIOContext *pb, const char *name, int value)
  2883. {
  2884. avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
  2885. }
  2886. static void param_write_string(AVIOContext *pb, const char *name, const char *value)
  2887. {
  2888. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
  2889. }
  2890. static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
  2891. {
  2892. char buf[150];
  2893. len = FFMIN(sizeof(buf) / 2 - 1, len);
  2894. ff_data_to_hex(buf, value, len, 0);
  2895. buf[2 * len] = '\0';
  2896. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
  2897. }
  2898. static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
  2899. {
  2900. int64_t pos = avio_tell(pb);
  2901. int i;
  2902. static const uint8_t uuid[] = {
  2903. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  2904. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  2905. };
  2906. avio_wb32(pb, 0);
  2907. ffio_wfourcc(pb, "uuid");
  2908. avio_write(pb, uuid, sizeof(uuid));
  2909. avio_wb32(pb, 0);
  2910. avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  2911. avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  2912. avio_printf(pb, "<head>\n");
  2913. if (!mov->exact)
  2914. avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
  2915. LIBAVFORMAT_IDENT);
  2916. avio_printf(pb, "</head>\n");
  2917. avio_printf(pb, "<body>\n");
  2918. avio_printf(pb, "<switch>\n");
  2919. for (i = 0; i < mov->nb_streams; i++) {
  2920. MOVTrack *track = &mov->tracks[i];
  2921. const char *type;
  2922. /* track->track_id is initialized in write_moov, and thus isn't known
  2923. * here yet */
  2924. int track_id = i + 1;
  2925. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2926. type = "video";
  2927. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  2928. type = "audio";
  2929. } else {
  2930. continue;
  2931. }
  2932. avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
  2933. track->enc->bit_rate);
  2934. param_write_int(pb, "systemBitrate", track->enc->bit_rate);
  2935. param_write_int(pb, "trackID", track_id);
  2936. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2937. if (track->enc->codec_id == AV_CODEC_ID_H264) {
  2938. uint8_t *ptr;
  2939. int size = track->enc->extradata_size;
  2940. if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
  2941. &size)) {
  2942. param_write_hex(pb, "CodecPrivateData",
  2943. ptr ? ptr : track->enc->extradata,
  2944. size);
  2945. av_free(ptr);
  2946. }
  2947. param_write_string(pb, "FourCC", "H264");
  2948. } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
  2949. param_write_string(pb, "FourCC", "WVC1");
  2950. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2951. track->enc->extradata_size);
  2952. }
  2953. param_write_int(pb, "MaxWidth", track->enc->width);
  2954. param_write_int(pb, "MaxHeight", track->enc->height);
  2955. param_write_int(pb, "DisplayWidth", track->enc->width);
  2956. param_write_int(pb, "DisplayHeight", track->enc->height);
  2957. } else {
  2958. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  2959. param_write_string(pb, "FourCC", "AACL");
  2960. } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
  2961. param_write_string(pb, "FourCC", "WMAP");
  2962. }
  2963. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2964. track->enc->extradata_size);
  2965. param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
  2966. track->enc->codec_id));
  2967. param_write_int(pb, "Channels", track->enc->channels);
  2968. param_write_int(pb, "SamplingRate", track->enc->sample_rate);
  2969. param_write_int(pb, "BitsPerSample", 16);
  2970. param_write_int(pb, "PacketSize", track->enc->block_align ?
  2971. track->enc->block_align : 4);
  2972. }
  2973. avio_printf(pb, "</%s>\n", type);
  2974. }
  2975. avio_printf(pb, "</switch>\n");
  2976. avio_printf(pb, "</body>\n");
  2977. avio_printf(pb, "</smil>\n");
  2978. return update_size(pb, pos);
  2979. }
  2980. static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  2981. {
  2982. avio_wb32(pb, 16);
  2983. ffio_wfourcc(pb, "mfhd");
  2984. avio_wb32(pb, 0);
  2985. avio_wb32(pb, mov->fragments);
  2986. return 0;
  2987. }
  2988. static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
  2989. {
  2990. return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
  2991. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
  2992. }
  2993. static int mov_write_tfhd_tag(AVIOContext *pb, MOVMuxContext *mov,
  2994. MOVTrack *track, int64_t moof_offset)
  2995. {
  2996. int64_t pos = avio_tell(pb);
  2997. uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  2998. MOV_TFHD_BASE_DATA_OFFSET;
  2999. if (!track->entry) {
  3000. flags |= MOV_TFHD_DURATION_IS_EMPTY;
  3001. } else {
  3002. flags |= MOV_TFHD_DEFAULT_FLAGS;
  3003. }
  3004. if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET)
  3005. flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
  3006. if (mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) {
  3007. flags &= ~MOV_TFHD_BASE_DATA_OFFSET;
  3008. flags |= MOV_TFHD_DEFAULT_BASE_IS_MOOF;
  3009. }
  3010. /* Don't set a default sample size, the silverlight player refuses
  3011. * to play files with that set. Don't set a default sample duration,
  3012. * WMP freaks out if it is set. Don't set a base data offset, PIFF
  3013. * file format says it MUST NOT be set. */
  3014. if (track->mode == MODE_ISM)
  3015. flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  3016. MOV_TFHD_BASE_DATA_OFFSET);
  3017. avio_wb32(pb, 0); /* size placeholder */
  3018. ffio_wfourcc(pb, "tfhd");
  3019. avio_w8(pb, 0); /* version */
  3020. avio_wb24(pb, flags);
  3021. avio_wb32(pb, track->track_id); /* track-id */
  3022. if (flags & MOV_TFHD_BASE_DATA_OFFSET)
  3023. avio_wb64(pb, moof_offset);
  3024. if (flags & MOV_TFHD_DEFAULT_DURATION) {
  3025. track->default_duration = get_cluster_duration(track, 0);
  3026. avio_wb32(pb, track->default_duration);
  3027. }
  3028. if (flags & MOV_TFHD_DEFAULT_SIZE) {
  3029. track->default_size = track->entry ? track->cluster[0].size : 1;
  3030. avio_wb32(pb, track->default_size);
  3031. } else
  3032. track->default_size = -1;
  3033. if (flags & MOV_TFHD_DEFAULT_FLAGS) {
  3034. /* Set the default flags based on the second sample, if available.
  3035. * If the first sample is different, that can be signaled via a separate field. */
  3036. if (track->entry > 1)
  3037. track->default_sample_flags = get_sample_flags(track, &track->cluster[1]);
  3038. else
  3039. track->default_sample_flags =
  3040. track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  3041. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
  3042. MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
  3043. avio_wb32(pb, track->default_sample_flags);
  3044. }
  3045. return update_size(pb, pos);
  3046. }
  3047. static int mov_write_trun_tag(AVIOContext *pb, MOVMuxContext *mov,
  3048. MOVTrack *track, int moof_size,
  3049. int first, int end)
  3050. {
  3051. int64_t pos = avio_tell(pb);
  3052. uint32_t flags = MOV_TRUN_DATA_OFFSET;
  3053. int i;
  3054. for (i = first; i < end; i++) {
  3055. if (get_cluster_duration(track, i) != track->default_duration)
  3056. flags |= MOV_TRUN_SAMPLE_DURATION;
  3057. if (track->cluster[i].size != track->default_size)
  3058. flags |= MOV_TRUN_SAMPLE_SIZE;
  3059. if (i > first && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
  3060. flags |= MOV_TRUN_SAMPLE_FLAGS;
  3061. }
  3062. if (!(flags & MOV_TRUN_SAMPLE_FLAGS) && track->entry > 0 &&
  3063. get_sample_flags(track, &track->cluster[0]) != track->default_sample_flags)
  3064. flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
  3065. if (track->flags & MOV_TRACK_CTTS)
  3066. flags |= MOV_TRUN_SAMPLE_CTS;
  3067. avio_wb32(pb, 0); /* size placeholder */
  3068. ffio_wfourcc(pb, "trun");
  3069. avio_w8(pb, 0); /* version */
  3070. avio_wb24(pb, flags);
  3071. avio_wb32(pb, end - first); /* sample count */
  3072. if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
  3073. !(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF) &&
  3074. !mov->first_trun)
  3075. avio_wb32(pb, 0); /* Later tracks follow immediately after the previous one */
  3076. else
  3077. avio_wb32(pb, moof_size + 8 + track->data_offset +
  3078. track->cluster[first].pos); /* data offset */
  3079. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
  3080. avio_wb32(pb, get_sample_flags(track, &track->cluster[first]));
  3081. for (i = first; i < end; i++) {
  3082. if (flags & MOV_TRUN_SAMPLE_DURATION)
  3083. avio_wb32(pb, get_cluster_duration(track, i));
  3084. if (flags & MOV_TRUN_SAMPLE_SIZE)
  3085. avio_wb32(pb, track->cluster[i].size);
  3086. if (flags & MOV_TRUN_SAMPLE_FLAGS)
  3087. avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
  3088. if (flags & MOV_TRUN_SAMPLE_CTS)
  3089. avio_wb32(pb, track->cluster[i].cts);
  3090. }
  3091. mov->first_trun = 0;
  3092. return update_size(pb, pos);
  3093. }
  3094. static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
  3095. {
  3096. int64_t pos = avio_tell(pb);
  3097. static const uint8_t uuid[] = {
  3098. 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
  3099. 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
  3100. };
  3101. avio_wb32(pb, 0); /* size placeholder */
  3102. ffio_wfourcc(pb, "uuid");
  3103. avio_write(pb, uuid, sizeof(uuid));
  3104. avio_w8(pb, 1);
  3105. avio_wb24(pb, 0);
  3106. avio_wb64(pb, track->start_dts + track->frag_start +
  3107. track->cluster[0].cts);
  3108. avio_wb64(pb, track->end_pts -
  3109. (track->cluster[0].dts + track->cluster[0].cts));
  3110. return update_size(pb, pos);
  3111. }
  3112. static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
  3113. MOVTrack *track, int entry)
  3114. {
  3115. int n = track->nb_frag_info - 1 - entry, i;
  3116. int size = 8 + 16 + 4 + 1 + 16*n;
  3117. static const uint8_t uuid[] = {
  3118. 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
  3119. 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
  3120. };
  3121. if (entry < 0)
  3122. return 0;
  3123. avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
  3124. avio_wb32(pb, size);
  3125. ffio_wfourcc(pb, "uuid");
  3126. avio_write(pb, uuid, sizeof(uuid));
  3127. avio_w8(pb, 1);
  3128. avio_wb24(pb, 0);
  3129. avio_w8(pb, n);
  3130. for (i = 0; i < n; i++) {
  3131. int index = entry + 1 + i;
  3132. avio_wb64(pb, track->frag_info[index].time);
  3133. avio_wb64(pb, track->frag_info[index].duration);
  3134. }
  3135. if (n < mov->ism_lookahead) {
  3136. int free_size = 16 * (mov->ism_lookahead - n);
  3137. avio_wb32(pb, free_size);
  3138. ffio_wfourcc(pb, "free");
  3139. ffio_fill(pb, 0, free_size - 8);
  3140. }
  3141. return 0;
  3142. }
  3143. static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
  3144. MOVTrack *track)
  3145. {
  3146. int64_t pos = avio_tell(pb);
  3147. int i;
  3148. for (i = 0; i < mov->ism_lookahead; i++) {
  3149. /* Update the tfrf tag for the last ism_lookahead fragments,
  3150. * nb_frag_info - 1 is the next fragment to be written. */
  3151. mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
  3152. }
  3153. avio_seek(pb, pos, SEEK_SET);
  3154. return 0;
  3155. }
  3156. static int mov_add_tfra_entries(AVIOContext *pb, MOVMuxContext *mov, int tracks,
  3157. int size)
  3158. {
  3159. int i;
  3160. for (i = 0; i < mov->nb_streams; i++) {
  3161. MOVTrack *track = &mov->tracks[i];
  3162. MOVFragmentInfo *info;
  3163. if ((tracks >= 0 && i != tracks) || !track->entry)
  3164. continue;
  3165. track->nb_frag_info++;
  3166. if (track->nb_frag_info >= track->frag_info_capacity) {
  3167. unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
  3168. if (av_reallocp_array(&track->frag_info,
  3169. new_capacity,
  3170. sizeof(*track->frag_info)))
  3171. return AVERROR(ENOMEM);
  3172. track->frag_info_capacity = new_capacity;
  3173. }
  3174. info = &track->frag_info[track->nb_frag_info - 1];
  3175. info->offset = avio_tell(pb);
  3176. info->size = size;
  3177. // Try to recreate the original pts for the first packet
  3178. // from the fields we have stored
  3179. info->time = track->start_dts + track->frag_start +
  3180. track->cluster[0].cts;
  3181. info->duration = track->end_pts -
  3182. (track->cluster[0].dts + track->cluster[0].cts);
  3183. // If the pts is less than zero, we will have trimmed
  3184. // away parts of the media track using an edit list,
  3185. // and the corresponding start presentation time is zero.
  3186. if (info->time < 0) {
  3187. info->duration += info->time;
  3188. info->time = 0;
  3189. }
  3190. info->tfrf_offset = 0;
  3191. mov_write_tfrf_tags(pb, mov, track);
  3192. }
  3193. return 0;
  3194. }
  3195. static int mov_write_tfdt_tag(AVIOContext *pb, MOVTrack *track)
  3196. {
  3197. int64_t pos = avio_tell(pb);
  3198. avio_wb32(pb, 0); /* size */
  3199. ffio_wfourcc(pb, "tfdt");
  3200. avio_w8(pb, 1); /* version */
  3201. avio_wb24(pb, 0);
  3202. avio_wb64(pb, track->frag_start);
  3203. return update_size(pb, pos);
  3204. }
  3205. static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
  3206. MOVTrack *track, int64_t moof_offset,
  3207. int moof_size)
  3208. {
  3209. int64_t pos = avio_tell(pb);
  3210. int i, start = 0;
  3211. avio_wb32(pb, 0); /* size placeholder */
  3212. ffio_wfourcc(pb, "traf");
  3213. mov_write_tfhd_tag(pb, mov, track, moof_offset);
  3214. if (mov->mode != MODE_ISM)
  3215. mov_write_tfdt_tag(pb, track);
  3216. for (i = 1; i < track->entry; i++) {
  3217. if (track->cluster[i].pos != track->cluster[i - 1].pos + track->cluster[i - 1].size) {
  3218. mov_write_trun_tag(pb, mov, track, moof_size, start, i);
  3219. start = i;
  3220. }
  3221. }
  3222. mov_write_trun_tag(pb, mov, track, moof_size, start, track->entry);
  3223. if (mov->mode == MODE_ISM) {
  3224. mov_write_tfxd_tag(pb, track);
  3225. if (mov->ism_lookahead) {
  3226. int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
  3227. if (track->nb_frag_info > 0) {
  3228. MOVFragmentInfo *info = &track->frag_info[track->nb_frag_info - 1];
  3229. if (!info->tfrf_offset)
  3230. info->tfrf_offset = avio_tell(pb);
  3231. }
  3232. avio_wb32(pb, 8 + size);
  3233. ffio_wfourcc(pb, "free");
  3234. for (i = 0; i < size; i++)
  3235. avio_w8(pb, 0);
  3236. }
  3237. }
  3238. return update_size(pb, pos);
  3239. }
  3240. static int mov_write_moof_tag_internal(AVIOContext *pb, MOVMuxContext *mov,
  3241. int tracks, int moof_size)
  3242. {
  3243. int64_t pos = avio_tell(pb);
  3244. int i;
  3245. avio_wb32(pb, 0); /* size placeholder */
  3246. ffio_wfourcc(pb, "moof");
  3247. mov->first_trun = 1;
  3248. mov_write_mfhd_tag(pb, mov);
  3249. for (i = 0; i < mov->nb_streams; i++) {
  3250. MOVTrack *track = &mov->tracks[i];
  3251. if (tracks >= 0 && i != tracks)
  3252. continue;
  3253. if (!track->entry)
  3254. continue;
  3255. mov_write_traf_tag(pb, mov, track, pos, moof_size);
  3256. }
  3257. return update_size(pb, pos);
  3258. }
  3259. static int mov_write_sidx_tag(AVIOContext *pb,
  3260. MOVTrack *track, int ref_size, int total_sidx_size)
  3261. {
  3262. int64_t pos = avio_tell(pb), offset_pos, end_pos;
  3263. int64_t presentation_time, duration, offset;
  3264. int starts_with_SAP, i, entries;
  3265. if (track->entry) {
  3266. entries = 1;
  3267. presentation_time = track->start_dts + track->frag_start +
  3268. track->cluster[0].cts;
  3269. duration = track->end_pts -
  3270. (track->cluster[0].dts + track->cluster[0].cts);
  3271. starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;
  3272. // pts<0 should be cut away using edts
  3273. if (presentation_time < 0) {
  3274. duration += presentation_time;
  3275. presentation_time = 0;
  3276. }
  3277. } else {
  3278. entries = track->nb_frag_info;
  3279. presentation_time = track->frag_info[0].time;
  3280. }
  3281. avio_wb32(pb, 0); /* size */
  3282. ffio_wfourcc(pb, "sidx");
  3283. avio_w8(pb, 1); /* version */
  3284. avio_wb24(pb, 0);
  3285. avio_wb32(pb, track->track_id); /* reference_ID */
  3286. avio_wb32(pb, track->timescale); /* timescale */
  3287. avio_wb64(pb, presentation_time); /* earliest_presentation_time */
  3288. offset_pos = avio_tell(pb);
  3289. avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */
  3290. avio_wb16(pb, 0); /* reserved */
  3291. avio_wb16(pb, entries); /* reference_count */
  3292. for (i = 0; i < entries; i++) {
  3293. if (!track->entry) {
  3294. if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {
  3295. av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");
  3296. }
  3297. duration = track->frag_info[i].duration;
  3298. ref_size = track->frag_info[i].size;
  3299. starts_with_SAP = 1;
  3300. }
  3301. avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */
  3302. avio_wb32(pb, duration); /* subsegment_duration */
  3303. avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */
  3304. }
  3305. end_pos = avio_tell(pb);
  3306. offset = pos + total_sidx_size - end_pos;
  3307. avio_seek(pb, offset_pos, SEEK_SET);
  3308. avio_wb64(pb, offset);
  3309. avio_seek(pb, end_pos, SEEK_SET);
  3310. return update_size(pb, pos);
  3311. }
  3312. static int mov_write_sidx_tags(AVIOContext *pb, MOVMuxContext *mov,
  3313. int tracks, int ref_size)
  3314. {
  3315. int i, round, ret;
  3316. AVIOContext *avio_buf;
  3317. int total_size = 0;
  3318. for (round = 0; round < 2; round++) {
  3319. // First run one round to calculate the total size of all
  3320. // sidx atoms.
  3321. // This would be much simpler if we'd only write one sidx
  3322. // atom, for the first track in the moof.
  3323. if (round == 0) {
  3324. if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
  3325. return ret;
  3326. } else {
  3327. avio_buf = pb;
  3328. }
  3329. for (i = 0; i < mov->nb_streams; i++) {
  3330. MOVTrack *track = &mov->tracks[i];
  3331. if (tracks >= 0 && i != tracks)
  3332. continue;
  3333. // When writing a sidx for the full file, entry is 0, but
  3334. // we want to include all tracks. ref_size is 0 in this case,
  3335. // since we read it from frag_info instead.
  3336. if (!track->entry && ref_size > 0)
  3337. continue;
  3338. total_size -= mov_write_sidx_tag(avio_buf, track, ref_size,
  3339. total_size);
  3340. }
  3341. if (round == 0)
  3342. total_size = ffio_close_null_buf(avio_buf);
  3343. }
  3344. return 0;
  3345. }
  3346. static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks,
  3347. int64_t mdat_size)
  3348. {
  3349. AVIOContext *avio_buf;
  3350. int ret, moof_size;
  3351. if ((ret = ffio_open_null_buf(&avio_buf)) < 0)
  3352. return ret;
  3353. mov_write_moof_tag_internal(avio_buf, mov, tracks, 0);
  3354. moof_size = ffio_close_null_buf(avio_buf);
  3355. if (mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_FASTSTART))
  3356. mov_write_sidx_tags(pb, mov, tracks, moof_size + 8 + mdat_size);
  3357. if ((ret = mov_add_tfra_entries(pb, mov, tracks, moof_size + 8 + mdat_size)) < 0)
  3358. return ret;
  3359. return mov_write_moof_tag_internal(pb, mov, tracks, moof_size);
  3360. }
  3361. static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
  3362. {
  3363. int64_t pos = avio_tell(pb);
  3364. int i;
  3365. avio_wb32(pb, 0); /* size placeholder */
  3366. ffio_wfourcc(pb, "tfra");
  3367. avio_w8(pb, 1); /* version */
  3368. avio_wb24(pb, 0);
  3369. avio_wb32(pb, track->track_id);
  3370. avio_wb32(pb, 0); /* length of traf/trun/sample num */
  3371. avio_wb32(pb, track->nb_frag_info);
  3372. for (i = 0; i < track->nb_frag_info; i++) {
  3373. avio_wb64(pb, track->frag_info[i].time);
  3374. avio_wb64(pb, track->frag_info[i].offset + track->data_offset);
  3375. avio_w8(pb, 1); /* traf number */
  3376. avio_w8(pb, 1); /* trun number */
  3377. avio_w8(pb, 1); /* sample number */
  3378. }
  3379. return update_size(pb, pos);
  3380. }
  3381. static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
  3382. {
  3383. int64_t pos = avio_tell(pb);
  3384. int i;
  3385. avio_wb32(pb, 0); /* size placeholder */
  3386. ffio_wfourcc(pb, "mfra");
  3387. /* An empty mfra atom is enough to indicate to the publishing point that
  3388. * the stream has ended. */
  3389. if (mov->flags & FF_MOV_FLAG_ISML)
  3390. return update_size(pb, pos);
  3391. for (i = 0; i < mov->nb_streams; i++) {
  3392. MOVTrack *track = &mov->tracks[i];
  3393. if (track->nb_frag_info)
  3394. mov_write_tfra_tag(pb, track);
  3395. }
  3396. avio_wb32(pb, 16);
  3397. ffio_wfourcc(pb, "mfro");
  3398. avio_wb32(pb, 0); /* version + flags */
  3399. avio_wb32(pb, avio_tell(pb) + 4 - pos);
  3400. return update_size(pb, pos);
  3401. }
  3402. static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
  3403. {
  3404. avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
  3405. ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
  3406. mov->mdat_pos = avio_tell(pb);
  3407. avio_wb32(pb, 0); /* size placeholder*/
  3408. ffio_wfourcc(pb, "mdat");
  3409. return 0;
  3410. }
  3411. /* TODO: This needs to be more general */
  3412. static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
  3413. {
  3414. MOVMuxContext *mov = s->priv_data;
  3415. int64_t pos = avio_tell(pb);
  3416. int has_h264 = 0, has_video = 0;
  3417. int minor = 0x200;
  3418. int i;
  3419. for (i = 0; i < s->nb_streams; i++) {
  3420. AVStream *st = s->streams[i];
  3421. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  3422. has_video = 1;
  3423. if (st->codec->codec_id == AV_CODEC_ID_H264)
  3424. has_h264 = 1;
  3425. }
  3426. avio_wb32(pb, 0); /* size */
  3427. ffio_wfourcc(pb, "ftyp");
  3428. if (mov->major_brand && strlen(mov->major_brand) >= 4)
  3429. ffio_wfourcc(pb, mov->major_brand);
  3430. else if (mov->mode == MODE_3GP) {
  3431. ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
  3432. minor = has_h264 ? 0x100 : 0x200;
  3433. } else if (mov->mode & MODE_3G2) {
  3434. ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
  3435. minor = has_h264 ? 0x20000 : 0x10000;
  3436. } else if (mov->mode == MODE_PSP)
  3437. ffio_wfourcc(pb, "MSNV");
  3438. else if (mov->mode == MODE_MP4 && mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
  3439. ffio_wfourcc(pb, "iso5"); // Required when using default-base-is-moof
  3440. else if (mov->mode == MODE_MP4)
  3441. ffio_wfourcc(pb, "isom");
  3442. else if (mov->mode == MODE_IPOD)
  3443. ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
  3444. else if (mov->mode == MODE_ISM)
  3445. ffio_wfourcc(pb, "isml");
  3446. else if (mov->mode == MODE_F4V)
  3447. ffio_wfourcc(pb, "f4v ");
  3448. else
  3449. ffio_wfourcc(pb, "qt ");
  3450. avio_wb32(pb, minor);
  3451. if (mov->mode == MODE_MOV)
  3452. ffio_wfourcc(pb, "qt ");
  3453. else if (mov->mode == MODE_ISM) {
  3454. ffio_wfourcc(pb, "piff");
  3455. } else if (!(mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)) {
  3456. ffio_wfourcc(pb, "isom");
  3457. ffio_wfourcc(pb, "iso2");
  3458. if (has_h264)
  3459. ffio_wfourcc(pb, "avc1");
  3460. }
  3461. // We add tfdt atoms when fragmenting, signal this with the iso6 compatible
  3462. // brand. This is compatible with users that don't understand tfdt.
  3463. if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->mode != MODE_ISM)
  3464. ffio_wfourcc(pb, "iso6");
  3465. if (mov->mode == MODE_3GP)
  3466. ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
  3467. else if (mov->mode & MODE_3G2)
  3468. ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
  3469. else if (mov->mode == MODE_PSP)
  3470. ffio_wfourcc(pb, "MSNV");
  3471. else if (mov->mode == MODE_MP4)
  3472. ffio_wfourcc(pb, "mp41");
  3473. if (mov->flags & FF_MOV_FLAG_DASH && mov->flags & FF_MOV_FLAG_FASTSTART)
  3474. ffio_wfourcc(pb, "dash");
  3475. return update_size(pb, pos);
  3476. }
  3477. static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
  3478. {
  3479. AVStream *video_st = s->streams[0];
  3480. AVCodecContext *video_codec = s->streams[0]->codec;
  3481. AVCodecContext *audio_codec = s->streams[1]->codec;
  3482. int audio_rate = audio_codec->sample_rate;
  3483. // TODO: should be avg_frame_rate
  3484. int frame_rate = ((video_st->time_base.den) * (0x10000)) / (video_st->time_base.num);
  3485. int audio_kbitrate = audio_codec->bit_rate / 1000;
  3486. int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
  3487. avio_wb32(pb, 0x94); /* size */
  3488. ffio_wfourcc(pb, "uuid");
  3489. ffio_wfourcc(pb, "PROF");
  3490. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  3491. avio_wb32(pb, 0xbb88695c);
  3492. avio_wb32(pb, 0xfac9c740);
  3493. avio_wb32(pb, 0x0); /* ? */
  3494. avio_wb32(pb, 0x3); /* 3 sections ? */
  3495. avio_wb32(pb, 0x14); /* size */
  3496. ffio_wfourcc(pb, "FPRF");
  3497. avio_wb32(pb, 0x0); /* ? */
  3498. avio_wb32(pb, 0x0); /* ? */
  3499. avio_wb32(pb, 0x0); /* ? */
  3500. avio_wb32(pb, 0x2c); /* size */
  3501. ffio_wfourcc(pb, "APRF"); /* audio */
  3502. avio_wb32(pb, 0x0);
  3503. avio_wb32(pb, 0x2); /* TrackID */
  3504. ffio_wfourcc(pb, "mp4a");
  3505. avio_wb32(pb, 0x20f);
  3506. avio_wb32(pb, 0x0);
  3507. avio_wb32(pb, audio_kbitrate);
  3508. avio_wb32(pb, audio_kbitrate);
  3509. avio_wb32(pb, audio_rate);
  3510. avio_wb32(pb, audio_codec->channels);
  3511. avio_wb32(pb, 0x34); /* size */
  3512. ffio_wfourcc(pb, "VPRF"); /* video */
  3513. avio_wb32(pb, 0x0);
  3514. avio_wb32(pb, 0x1); /* TrackID */
  3515. if (video_codec->codec_id == AV_CODEC_ID_H264) {
  3516. ffio_wfourcc(pb, "avc1");
  3517. avio_wb16(pb, 0x014D);
  3518. avio_wb16(pb, 0x0015);
  3519. } else {
  3520. ffio_wfourcc(pb, "mp4v");
  3521. avio_wb16(pb, 0x0000);
  3522. avio_wb16(pb, 0x0103);
  3523. }
  3524. avio_wb32(pb, 0x0);
  3525. avio_wb32(pb, video_kbitrate);
  3526. avio_wb32(pb, video_kbitrate);
  3527. avio_wb32(pb, frame_rate);
  3528. avio_wb32(pb, frame_rate);
  3529. avio_wb16(pb, video_codec->width);
  3530. avio_wb16(pb, video_codec->height);
  3531. avio_wb32(pb, 0x010001); /* ? */
  3532. }
  3533. static int mov_write_identification(AVIOContext *pb, AVFormatContext *s)
  3534. {
  3535. MOVMuxContext *mov = s->priv_data;
  3536. int i;
  3537. mov_write_ftyp_tag(pb,s);
  3538. if (mov->mode == MODE_PSP) {
  3539. int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
  3540. for (i = 0; i < s->nb_streams; i++) {
  3541. AVStream *st = s->streams[i];
  3542. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  3543. video_streams_nb++;
  3544. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  3545. audio_streams_nb++;
  3546. else
  3547. other_streams_nb++;
  3548. }
  3549. if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
  3550. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  3551. return AVERROR(EINVAL);
  3552. }
  3553. mov_write_uuidprof_tag(pb, s);
  3554. }
  3555. return 0;
  3556. }
  3557. static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
  3558. {
  3559. uint32_t c = -1;
  3560. int i, closed_gop = 0;
  3561. for (i = 0; i < pkt->size - 4; i++) {
  3562. c = (c << 8) + pkt->data[i];
  3563. if (c == 0x1b8) { // gop
  3564. closed_gop = pkt->data[i + 4] >> 6 & 0x01;
  3565. } else if (c == 0x100) { // pic
  3566. int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
  3567. if (!temp_ref || closed_gop) // I picture is not reordered
  3568. *flags = MOV_SYNC_SAMPLE;
  3569. else
  3570. *flags = MOV_PARTIAL_SYNC_SAMPLE;
  3571. break;
  3572. }
  3573. }
  3574. return 0;
  3575. }
  3576. static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk)
  3577. {
  3578. const uint8_t *start, *next, *end = pkt->data + pkt->size;
  3579. int seq = 0, entry = 0;
  3580. int key = pkt->flags & AV_PKT_FLAG_KEY;
  3581. start = find_next_marker(pkt->data, end);
  3582. for (next = start; next < end; start = next) {
  3583. next = find_next_marker(start + 4, end);
  3584. switch (AV_RB32(start)) {
  3585. case VC1_CODE_SEQHDR:
  3586. seq = 1;
  3587. break;
  3588. case VC1_CODE_ENTRYPOINT:
  3589. entry = 1;
  3590. break;
  3591. case VC1_CODE_SLICE:
  3592. trk->vc1_info.slices = 1;
  3593. break;
  3594. }
  3595. }
  3596. if (!trk->entry && trk->vc1_info.first_packet_seen)
  3597. trk->vc1_info.first_frag_written = 1;
  3598. if (!trk->entry && !trk->vc1_info.first_frag_written) {
  3599. /* First packet in first fragment */
  3600. trk->vc1_info.first_packet_seq = seq;
  3601. trk->vc1_info.first_packet_entry = entry;
  3602. trk->vc1_info.first_packet_seen = 1;
  3603. } else if ((seq && !trk->vc1_info.packet_seq) ||
  3604. (entry && !trk->vc1_info.packet_entry)) {
  3605. int i;
  3606. for (i = 0; i < trk->entry; i++)
  3607. trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
  3608. trk->has_keyframes = 0;
  3609. if (seq)
  3610. trk->vc1_info.packet_seq = 1;
  3611. if (entry)
  3612. trk->vc1_info.packet_entry = 1;
  3613. if (!trk->vc1_info.first_frag_written) {
  3614. /* First fragment */
  3615. if ((!seq || trk->vc1_info.first_packet_seq) &&
  3616. (!entry || trk->vc1_info.first_packet_entry)) {
  3617. /* First packet had the same headers as this one, readd the
  3618. * sync sample flag. */
  3619. trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
  3620. trk->has_keyframes = 1;
  3621. }
  3622. }
  3623. }
  3624. if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
  3625. key = seq && entry;
  3626. else if (trk->vc1_info.packet_seq)
  3627. key = seq;
  3628. else if (trk->vc1_info.packet_entry)
  3629. key = entry;
  3630. if (key) {
  3631. trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
  3632. trk->has_keyframes++;
  3633. }
  3634. }
  3635. static int mov_flush_fragment_interleaving(AVFormatContext *s, MOVTrack *track)
  3636. {
  3637. MOVMuxContext *mov = s->priv_data;
  3638. int ret, buf_size;
  3639. uint8_t *buf;
  3640. int i, offset;
  3641. if (!track->mdat_buf)
  3642. return 0;
  3643. if (!mov->mdat_buf) {
  3644. if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
  3645. return ret;
  3646. }
  3647. buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
  3648. track->mdat_buf = NULL;
  3649. offset = avio_tell(mov->mdat_buf);
  3650. avio_write(mov->mdat_buf, buf, buf_size);
  3651. av_free(buf);
  3652. for (i = track->entries_flushed; i < track->entry; i++)
  3653. track->cluster[i].pos += offset;
  3654. track->entries_flushed = track->entry;
  3655. return 0;
  3656. }
  3657. static int mov_flush_fragment(AVFormatContext *s)
  3658. {
  3659. MOVMuxContext *mov = s->priv_data;
  3660. int i, first_track = -1;
  3661. int64_t mdat_size = 0;
  3662. int ret;
  3663. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  3664. return 0;
  3665. for (i = 0; i < mov->nb_streams; i++) {
  3666. MOVTrack *track = &mov->tracks[i];
  3667. if (track->entry <= 1)
  3668. continue;
  3669. // Sample durations are calculated as the diff of dts values,
  3670. // but for the last sample in a fragment, we don't know the dts
  3671. // of the first sample in the next fragment, so we have to rely
  3672. // on what was set as duration in the AVPacket. Not all callers
  3673. // set this though, so we might want to replace it with an
  3674. // estimate if it currently is zero.
  3675. if (get_cluster_duration(track, track->entry - 1) != 0)
  3676. continue;
  3677. // Use the duration (i.e. dts diff) of the second last sample for
  3678. // the last one. This is a wild guess (and fatal if it turns out
  3679. // to be too long), but probably the best we can do - having a zero
  3680. // duration is bad as well.
  3681. track->track_duration += get_cluster_duration(track, track->entry - 2);
  3682. track->end_pts += get_cluster_duration(track, track->entry - 2);
  3683. if (!mov->missing_duration_warned) {
  3684. av_log(s, AV_LOG_WARNING,
  3685. "Estimating the duration of the last packet in a "
  3686. "fragment, consider setting the duration field in "
  3687. "AVPacket instead.\n");
  3688. mov->missing_duration_warned = 1;
  3689. }
  3690. }
  3691. if (!mov->moov_written) {
  3692. int64_t pos = avio_tell(s->pb);
  3693. uint8_t *buf;
  3694. int buf_size, moov_size;
  3695. for (i = 0; i < mov->nb_streams; i++)
  3696. if (!mov->tracks[i].entry)
  3697. break;
  3698. /* Don't write the initial moov unless all tracks have data */
  3699. if (i < mov->nb_streams)
  3700. return 0;
  3701. moov_size = get_moov_size(s);
  3702. for (i = 0; i < mov->nb_streams; i++)
  3703. mov->tracks[i].data_offset = pos + moov_size + 8;
  3704. if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
  3705. mov_write_identification(s->pb, s);
  3706. if ((ret = mov_write_moov_tag(s->pb, mov, s)) < 0)
  3707. return ret;
  3708. if (mov->flags & FF_MOV_FLAG_DELAY_MOOV) {
  3709. if (mov->flags & FF_MOV_FLAG_FASTSTART)
  3710. mov->reserved_moov_pos = avio_tell(s->pb);
  3711. avio_flush(s->pb);
  3712. mov->moov_written = 1;
  3713. return 0;
  3714. }
  3715. buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
  3716. mov->mdat_buf = NULL;
  3717. avio_wb32(s->pb, buf_size + 8);
  3718. ffio_wfourcc(s->pb, "mdat");
  3719. avio_write(s->pb, buf, buf_size);
  3720. av_free(buf);
  3721. mov->moov_written = 1;
  3722. mov->mdat_size = 0;
  3723. for (i = 0; i < mov->nb_streams; i++) {
  3724. if (mov->tracks[i].entry)
  3725. mov->tracks[i].frag_start += mov->tracks[i].start_dts +
  3726. mov->tracks[i].track_duration -
  3727. mov->tracks[i].cluster[0].dts;
  3728. mov->tracks[i].entry = 0;
  3729. }
  3730. avio_flush(s->pb);
  3731. return 0;
  3732. }
  3733. if (mov->frag_interleave) {
  3734. for (i = 0; i < mov->nb_streams; i++) {
  3735. MOVTrack *track = &mov->tracks[i];
  3736. int ret;
  3737. if ((ret = mov_flush_fragment_interleaving(s, track)) < 0)
  3738. return ret;
  3739. }
  3740. if (!mov->mdat_buf)
  3741. return 0;
  3742. mdat_size = avio_tell(mov->mdat_buf);
  3743. }
  3744. for (i = 0; i < mov->nb_streams; i++) {
  3745. MOVTrack *track = &mov->tracks[i];
  3746. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF || mov->frag_interleave)
  3747. track->data_offset = 0;
  3748. else
  3749. track->data_offset = mdat_size;
  3750. if (!track->entry)
  3751. continue;
  3752. if (track->mdat_buf)
  3753. mdat_size += avio_tell(track->mdat_buf);
  3754. if (first_track < 0)
  3755. first_track = i;
  3756. }
  3757. if (!mdat_size)
  3758. return 0;
  3759. for (i = 0; i < mov->nb_streams; i++) {
  3760. MOVTrack *track = &mov->tracks[i];
  3761. int buf_size, write_moof = 1, moof_tracks = -1;
  3762. uint8_t *buf;
  3763. int64_t duration = 0;
  3764. if (track->entry)
  3765. duration = track->start_dts + track->track_duration -
  3766. track->cluster[0].dts;
  3767. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
  3768. if (!track->mdat_buf)
  3769. continue;
  3770. mdat_size = avio_tell(track->mdat_buf);
  3771. moof_tracks = i;
  3772. } else {
  3773. write_moof = i == first_track;
  3774. }
  3775. if (write_moof) {
  3776. avio_flush(s->pb);
  3777. mov_write_moof_tag(s->pb, mov, moof_tracks, mdat_size);
  3778. mov->fragments++;
  3779. avio_wb32(s->pb, mdat_size + 8);
  3780. ffio_wfourcc(s->pb, "mdat");
  3781. }
  3782. if (track->entry)
  3783. track->frag_start += duration;
  3784. track->entry = 0;
  3785. track->entries_flushed = 0;
  3786. if (!mov->frag_interleave) {
  3787. if (!track->mdat_buf)
  3788. continue;
  3789. buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
  3790. track->mdat_buf = NULL;
  3791. } else {
  3792. if (!mov->mdat_buf)
  3793. continue;
  3794. buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
  3795. mov->mdat_buf = NULL;
  3796. }
  3797. avio_write(s->pb, buf, buf_size);
  3798. av_free(buf);
  3799. }
  3800. mov->mdat_size = 0;
  3801. avio_flush(s->pb);
  3802. return 0;
  3803. }
  3804. static int mov_auto_flush_fragment(AVFormatContext *s)
  3805. {
  3806. MOVMuxContext *mov = s->priv_data;
  3807. int had_moov = mov->moov_written;
  3808. int ret = mov_flush_fragment(s);
  3809. if (ret < 0)
  3810. return ret;
  3811. // If using delay_moov, the first flush only wrote the moov,
  3812. // not the actual moof+mdat pair, thus flush once again.
  3813. if (!had_moov && mov->flags & FF_MOV_FLAG_DELAY_MOOV)
  3814. ret = mov_flush_fragment(s);
  3815. return ret;
  3816. }
  3817. int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  3818. {
  3819. MOVMuxContext *mov = s->priv_data;
  3820. AVIOContext *pb = s->pb;
  3821. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  3822. AVCodecContext *enc = trk->enc;
  3823. unsigned int samples_in_chunk = 0;
  3824. int size = pkt->size, ret = 0;
  3825. uint8_t *reformatted_data = NULL;
  3826. if (trk->entry) {
  3827. int64_t duration = pkt->dts - trk->cluster[trk->entry - 1].dts;
  3828. if (duration < 0 || duration > INT_MAX) {
  3829. av_log(s, AV_LOG_ERROR, "Application provided duration: %"PRId64" / timestamp: %"PRId64" is out of range for mov/mp4 format\n",
  3830. duration, pkt->dts
  3831. );
  3832. pkt->dts = trk->cluster[trk->entry - 1].dts + 1;
  3833. pkt->pts = AV_NOPTS_VALUE;
  3834. }
  3835. if (pkt->duration < 0) {
  3836. av_log(s, AV_LOG_ERROR, "Application provided duration: %d is invalid\n", pkt->duration);
  3837. return AVERROR(EINVAL);
  3838. }
  3839. }
  3840. if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
  3841. int ret;
  3842. if (mov->moov_written || mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  3843. if (mov->frag_interleave && mov->fragments > 0) {
  3844. if (trk->entry - trk->entries_flushed >= mov->frag_interleave) {
  3845. if ((ret = mov_flush_fragment_interleaving(s, trk)) < 0)
  3846. return ret;
  3847. }
  3848. }
  3849. if (!trk->mdat_buf) {
  3850. if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
  3851. return ret;
  3852. }
  3853. pb = trk->mdat_buf;
  3854. } else {
  3855. if (!mov->mdat_buf) {
  3856. if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
  3857. return ret;
  3858. }
  3859. pb = mov->mdat_buf;
  3860. }
  3861. }
  3862. if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
  3863. /* We must find out how many AMR blocks there are in one packet */
  3864. static uint16_t packed_size[16] =
  3865. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
  3866. int len = 0;
  3867. while (len < size && samples_in_chunk < 100) {
  3868. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  3869. samples_in_chunk++;
  3870. }
  3871. if (samples_in_chunk > 1) {
  3872. av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
  3873. return -1;
  3874. }
  3875. } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  3876. enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  3877. samples_in_chunk = enc->frame_size;
  3878. } else if (trk->sample_size)
  3879. samples_in_chunk = size / trk->sample_size;
  3880. else
  3881. samples_in_chunk = 1;
  3882. /* copy extradata if it exists */
  3883. if (trk->vos_len == 0 && enc->extradata_size > 0 &&
  3884. !TAG_IS_AVCI(trk->tag) &&
  3885. (enc->codec_id != AV_CODEC_ID_DNXHD)) {
  3886. trk->vos_len = enc->extradata_size;
  3887. trk->vos_data = av_malloc(trk->vos_len);
  3888. if (!trk->vos_data) {
  3889. ret = AVERROR(ENOMEM);
  3890. goto err;
  3891. }
  3892. memcpy(trk->vos_data, enc->extradata, trk->vos_len);
  3893. }
  3894. if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
  3895. (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
  3896. if (!s->streams[pkt->stream_index]->nb_frames) {
  3897. av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
  3898. "use the audio bitstream filter 'aac_adtstoasc' to fix it "
  3899. "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
  3900. return -1;
  3901. }
  3902. av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
  3903. }
  3904. if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1 && !TAG_IS_AVCI(trk->tag)) {
  3905. /* from x264 or from bytestream h264 */
  3906. /* nal reformating needed */
  3907. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
  3908. ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
  3909. &size);
  3910. avio_write(pb, reformatted_data, size);
  3911. } else {
  3912. size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
  3913. }
  3914. } else if (enc->codec_id == AV_CODEC_ID_HEVC && trk->vos_len > 6 &&
  3915. (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
  3916. /* extradata is Annex B, assume the bitstream is too and convert it */
  3917. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
  3918. ff_hevc_annexb2mp4_buf(pkt->data, &reformatted_data, &size, 0, NULL);
  3919. avio_write(pb, reformatted_data, size);
  3920. } else {
  3921. size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
  3922. }
  3923. #if CONFIG_AC3_PARSER
  3924. } else if (enc->codec_id == AV_CODEC_ID_EAC3) {
  3925. size = handle_eac3(mov, pkt, trk);
  3926. if (size < 0)
  3927. return size;
  3928. else if (!size)
  3929. goto end;
  3930. avio_write(pb, pkt->data, size);
  3931. #endif
  3932. } else {
  3933. avio_write(pb, pkt->data, size);
  3934. }
  3935. if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
  3936. enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
  3937. /* copy frame to create needed atoms */
  3938. trk->vos_len = size;
  3939. trk->vos_data = av_malloc(size);
  3940. if (!trk->vos_data) {
  3941. ret = AVERROR(ENOMEM);
  3942. goto err;
  3943. }
  3944. memcpy(trk->vos_data, pkt->data, size);
  3945. }
  3946. if (trk->entry >= trk->cluster_capacity) {
  3947. unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
  3948. if (av_reallocp_array(&trk->cluster, new_capacity,
  3949. sizeof(*trk->cluster))) {
  3950. ret = AVERROR(ENOMEM);
  3951. goto err;
  3952. }
  3953. trk->cluster_capacity = new_capacity;
  3954. }
  3955. trk->cluster[trk->entry].pos = avio_tell(pb) - size;
  3956. trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
  3957. trk->cluster[trk->entry].chunkNum = 0;
  3958. trk->cluster[trk->entry].size = size;
  3959. trk->cluster[trk->entry].entries = samples_in_chunk;
  3960. trk->cluster[trk->entry].dts = pkt->dts;
  3961. if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
  3962. if (!trk->frag_discont) {
  3963. /* First packet of a new fragment. We already wrote the duration
  3964. * of the last packet of the previous fragment based on track_duration,
  3965. * which might not exactly match our dts. Therefore adjust the dts
  3966. * of this packet to be what the previous packets duration implies. */
  3967. trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
  3968. /* We also may have written the pts and the corresponding duration
  3969. * in sidx/tfrf/tfxd tags; make sure the sidx pts and duration match up with
  3970. * the next fragment. This means the cts of the first sample must
  3971. * be the same in all fragments. */
  3972. if ((mov->flags & FF_MOV_FLAG_DASH && !(mov->flags & FF_MOV_FLAG_FASTSTART)) ||
  3973. mov->mode == MODE_ISM)
  3974. pkt->pts = pkt->dts + trk->end_pts - trk->cluster[trk->entry].dts;
  3975. } else {
  3976. /* New fragment, but discontinuous from previous fragments.
  3977. * Pretend the duration sum of the earlier fragments is
  3978. * pkt->dts - trk->start_dts. */
  3979. trk->frag_start = pkt->dts - trk->start_dts;
  3980. trk->end_pts = AV_NOPTS_VALUE;
  3981. trk->frag_discont = 0;
  3982. }
  3983. }
  3984. if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !mov->use_editlist &&
  3985. s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO) {
  3986. /* Not using edit lists and shifting the first track to start from zero.
  3987. * If the other streams start from a later timestamp, we won't be able
  3988. * to signal the difference in starting time without an edit list.
  3989. * Thus move the timestamp for this first sample to 0, increasing
  3990. * its duration instead. */
  3991. trk->cluster[trk->entry].dts = trk->start_dts = 0;
  3992. }
  3993. if (trk->start_dts == AV_NOPTS_VALUE) {
  3994. trk->start_dts = pkt->dts;
  3995. if (trk->frag_discont) {
  3996. /* Pretend the whole stream started at dts=0, with earlier fragments
  3997. * already written, with a duration summing up to pkt->dts. */
  3998. trk->frag_start = pkt->dts;
  3999. trk->start_dts = 0;
  4000. trk->frag_discont = 0;
  4001. } else if (pkt->dts && mov->moov_written)
  4002. av_log(s, AV_LOG_WARNING,
  4003. "Track %d starts with a nonzero dts %"PRId64", while the moov "
  4004. "already has been written. Set the delay_moov flag to handle "
  4005. "this case.\n",
  4006. pkt->stream_index, pkt->dts);
  4007. }
  4008. trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
  4009. trk->last_sample_is_subtitle_end = 0;
  4010. if (pkt->pts == AV_NOPTS_VALUE) {
  4011. av_log(s, AV_LOG_WARNING, "pts has no value\n");
  4012. pkt->pts = pkt->dts;
  4013. }
  4014. if (pkt->dts != pkt->pts)
  4015. trk->flags |= MOV_TRACK_CTTS;
  4016. trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
  4017. trk->cluster[trk->entry].flags = 0;
  4018. if (trk->start_cts == AV_NOPTS_VALUE)
  4019. trk->start_cts = pkt->pts - pkt->dts;
  4020. if (trk->end_pts == AV_NOPTS_VALUE)
  4021. trk->end_pts = trk->cluster[trk->entry].dts +
  4022. trk->cluster[trk->entry].cts + pkt->duration;
  4023. else
  4024. trk->end_pts = FFMAX(trk->end_pts, trk->cluster[trk->entry].dts +
  4025. trk->cluster[trk->entry].cts +
  4026. pkt->duration);
  4027. if (enc->codec_id == AV_CODEC_ID_VC1) {
  4028. mov_parse_vc1_frame(pkt, trk);
  4029. } else if (pkt->flags & AV_PKT_FLAG_KEY) {
  4030. if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  4031. trk->entry > 0) { // force sync sample for the first key frame
  4032. mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
  4033. if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
  4034. trk->flags |= MOV_TRACK_STPS;
  4035. } else {
  4036. trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
  4037. }
  4038. if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
  4039. trk->has_keyframes++;
  4040. }
  4041. trk->entry++;
  4042. trk->sample_count += samples_in_chunk;
  4043. mov->mdat_size += size;
  4044. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
  4045. ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
  4046. reformatted_data, size);
  4047. end:
  4048. err:
  4049. av_free(reformatted_data);
  4050. return ret;
  4051. }
  4052. static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
  4053. {
  4054. MOVMuxContext *mov = s->priv_data;
  4055. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  4056. AVCodecContext *enc = trk->enc;
  4057. int64_t frag_duration = 0;
  4058. int size = pkt->size;
  4059. if (!pkt->size)
  4060. return 0; /* Discard 0 sized packets */
  4061. if (mov->flags & FF_MOV_FLAG_FRAG_DISCONT) {
  4062. int i;
  4063. for (i = 0; i < s->nb_streams; i++)
  4064. mov->tracks[i].frag_discont = 1;
  4065. mov->flags &= ~FF_MOV_FLAG_FRAG_DISCONT;
  4066. }
  4067. if (trk->entry && pkt->stream_index < s->nb_streams)
  4068. frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
  4069. s->streams[pkt->stream_index]->time_base,
  4070. AV_TIME_BASE_Q);
  4071. if ((mov->max_fragment_duration &&
  4072. frag_duration >= mov->max_fragment_duration) ||
  4073. (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
  4074. (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
  4075. enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  4076. trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
  4077. if (frag_duration >= mov->min_fragment_duration) {
  4078. // Set the duration of this track to line up with the next
  4079. // sample in this track. This avoids relying on AVPacket
  4080. // duration, but only helps for this particular track, not
  4081. // for the other ones that are flushed at the same time.
  4082. trk->track_duration = pkt->dts - trk->start_dts;
  4083. trk->end_pts = pkt->pts;
  4084. mov_auto_flush_fragment(s);
  4085. }
  4086. }
  4087. return ff_mov_write_packet(s, pkt);
  4088. }
  4089. static int mov_write_subtitle_end_packet(AVFormatContext *s,
  4090. int stream_index,
  4091. int64_t dts) {
  4092. AVPacket end;
  4093. uint8_t data[2] = {0};
  4094. int ret;
  4095. av_init_packet(&end);
  4096. end.size = sizeof(data);
  4097. end.data = data;
  4098. end.pts = dts;
  4099. end.dts = dts;
  4100. end.duration = 0;
  4101. end.stream_index = stream_index;
  4102. ret = mov_write_single_packet(s, &end);
  4103. av_free_packet(&end);
  4104. return ret;
  4105. }
  4106. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  4107. {
  4108. if (!pkt) {
  4109. mov_flush_fragment(s);
  4110. return 1;
  4111. } else {
  4112. int i;
  4113. MOVMuxContext *mov = s->priv_data;
  4114. if (!pkt->size) return 0; /* Discard 0 sized packets */
  4115. /*
  4116. * Subtitles require special handling.
  4117. *
  4118. * 1) For full complaince, every track must have a sample at
  4119. * dts == 0, which is rarely true for subtitles. So, as soon
  4120. * as we see any packet with dts > 0, write an empty subtitle
  4121. * at dts == 0 for any subtitle track with no samples in it.
  4122. *
  4123. * 2) For each subtitle track, check if the current packet's
  4124. * dts is past the duration of the last subtitle sample. If
  4125. * so, we now need to write an end sample for that subtitle.
  4126. *
  4127. * This must be done conditionally to allow for subtitles that
  4128. * immediately replace each other, in which case an end sample
  4129. * is not needed, and is, in fact, actively harmful.
  4130. *
  4131. * 3) See mov_write_trailer for how the final end sample is
  4132. * handled.
  4133. */
  4134. for (i = 0; i < mov->nb_streams; i++) {
  4135. MOVTrack *trk = &mov->tracks[i];
  4136. int ret;
  4137. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  4138. trk->track_duration < pkt->dts &&
  4139. (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
  4140. ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
  4141. if (ret < 0) return ret;
  4142. trk->last_sample_is_subtitle_end = 1;
  4143. }
  4144. }
  4145. return mov_write_single_packet(s, pkt);
  4146. }
  4147. }
  4148. // QuickTime chapters involve an additional text track with the chapter names
  4149. // as samples, and a tref pointing from the other tracks to the chapter one.
  4150. static int mov_create_chapter_track(AVFormatContext *s, int tracknum)
  4151. {
  4152. AVIOContext *pb;
  4153. MOVMuxContext *mov = s->priv_data;
  4154. MOVTrack *track = &mov->tracks[tracknum];
  4155. AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
  4156. int i, len;
  4157. track->mode = mov->mode;
  4158. track->tag = MKTAG('t','e','x','t');
  4159. track->timescale = MOV_TIMESCALE;
  4160. track->enc = avcodec_alloc_context3(NULL);
  4161. if (!track->enc)
  4162. return AVERROR(ENOMEM);
  4163. track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  4164. #if 0
  4165. // These properties are required to make QT recognize the chapter track
  4166. uint8_t chapter_properties[43] = { 0, 0, 0, 0, 0, 0, 0, 1, };
  4167. if (ff_alloc_extradata(track->enc, sizeof(chapter_properties)))
  4168. return AVERROR(ENOMEM);
  4169. memcpy(track->enc->extradata, chapter_properties, sizeof(chapter_properties));
  4170. #else
  4171. if (avio_open_dyn_buf(&pb) >= 0) {
  4172. int size;
  4173. uint8_t *buf;
  4174. /* Stub header (usually for Quicktime chapter track) */
  4175. // TextSampleEntry
  4176. avio_wb32(pb, 0x01); // displayFlags
  4177. avio_w8(pb, 0x00); // horizontal justification
  4178. avio_w8(pb, 0x00); // vertical justification
  4179. avio_w8(pb, 0x00); // bgColourRed
  4180. avio_w8(pb, 0x00); // bgColourGreen
  4181. avio_w8(pb, 0x00); // bgColourBlue
  4182. avio_w8(pb, 0x00); // bgColourAlpha
  4183. // BoxRecord
  4184. avio_wb16(pb, 0x00); // defTextBoxTop
  4185. avio_wb16(pb, 0x00); // defTextBoxLeft
  4186. avio_wb16(pb, 0x00); // defTextBoxBottom
  4187. avio_wb16(pb, 0x00); // defTextBoxRight
  4188. // StyleRecord
  4189. avio_wb16(pb, 0x00); // startChar
  4190. avio_wb16(pb, 0x00); // endChar
  4191. avio_wb16(pb, 0x01); // fontID
  4192. avio_w8(pb, 0x00); // fontStyleFlags
  4193. avio_w8(pb, 0x00); // fontSize
  4194. avio_w8(pb, 0x00); // fgColourRed
  4195. avio_w8(pb, 0x00); // fgColourGreen
  4196. avio_w8(pb, 0x00); // fgColourBlue
  4197. avio_w8(pb, 0x00); // fgColourAlpha
  4198. // FontTableBox
  4199. avio_wb32(pb, 0x0D); // box size
  4200. ffio_wfourcc(pb, "ftab"); // box atom name
  4201. avio_wb16(pb, 0x01); // entry count
  4202. // FontRecord
  4203. avio_wb16(pb, 0x01); // font ID
  4204. avio_w8(pb, 0x00); // font name length
  4205. if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
  4206. track->enc->extradata = buf;
  4207. track->enc->extradata_size = size;
  4208. } else {
  4209. av_freep(&buf);
  4210. }
  4211. }
  4212. #endif
  4213. for (i = 0; i < s->nb_chapters; i++) {
  4214. AVChapter *c = s->chapters[i];
  4215. AVDictionaryEntry *t;
  4216. int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
  4217. pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
  4218. pkt.duration = end - pkt.dts;
  4219. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  4220. const char encd[12] = {
  4221. 0x00, 0x00, 0x00, 0x0C,
  4222. 'e', 'n', 'c', 'd',
  4223. 0x00, 0x00, 0x01, 0x00 };
  4224. len = strlen(t->value);
  4225. pkt.size = len + 2 + 12;
  4226. pkt.data = av_malloc(pkt.size);
  4227. if (!pkt.data)
  4228. return AVERROR(ENOMEM);
  4229. AV_WB16(pkt.data, len);
  4230. memcpy(pkt.data + 2, t->value, len);
  4231. memcpy(pkt.data + len + 2, encd, sizeof(encd));
  4232. ff_mov_write_packet(s, &pkt);
  4233. av_freep(&pkt.data);
  4234. }
  4235. }
  4236. return 0;
  4237. }
  4238. static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
  4239. {
  4240. int ret;
  4241. MOVMuxContext *mov = s->priv_data;
  4242. MOVTrack *track = &mov->tracks[index];
  4243. AVStream *src_st = s->streams[src_index];
  4244. AVTimecode tc;
  4245. AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
  4246. AVRational rate = find_fps(s, src_st);
  4247. /* compute the frame number */
  4248. ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
  4249. if (ret < 0)
  4250. return ret;
  4251. /* tmcd track based on video stream */
  4252. track->mode = mov->mode;
  4253. track->tag = MKTAG('t','m','c','d');
  4254. track->src_track = src_index;
  4255. track->timescale = mov->tracks[src_index].timescale;
  4256. if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
  4257. track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
  4258. /* set st to src_st for metadata access*/
  4259. track->st = src_st;
  4260. /* encode context: tmcd data stream */
  4261. track->enc = avcodec_alloc_context3(NULL);
  4262. if (!track->enc)
  4263. return AVERROR(ENOMEM);
  4264. track->enc->codec_type = AVMEDIA_TYPE_DATA;
  4265. track->enc->codec_tag = track->tag;
  4266. track->enc->time_base = av_inv_q(rate);
  4267. /* the tmcd track just contains one packet with the frame number */
  4268. pkt.data = av_malloc(pkt.size);
  4269. if (!pkt.data)
  4270. return AVERROR(ENOMEM);
  4271. AV_WB32(pkt.data, tc.start);
  4272. ret = ff_mov_write_packet(s, &pkt);
  4273. av_free(pkt.data);
  4274. return ret;
  4275. }
  4276. /*
  4277. * st->disposition controls the "enabled" flag in the tkhd tag.
  4278. * QuickTime will not play a track if it is not enabled. So make sure
  4279. * that one track of each type (audio, video, subtitle) is enabled.
  4280. *
  4281. * Subtitles are special. For audio and video, setting "enabled" also
  4282. * makes the track "default" (i.e. it is rendered when played). For
  4283. * subtitles, an "enabled" subtitle is not rendered by default, but
  4284. * if no subtitle is enabled, the subtitle menu in QuickTime will be
  4285. * empty!
  4286. */
  4287. static void enable_tracks(AVFormatContext *s)
  4288. {
  4289. MOVMuxContext *mov = s->priv_data;
  4290. int i;
  4291. int enabled[AVMEDIA_TYPE_NB];
  4292. int first[AVMEDIA_TYPE_NB];
  4293. for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
  4294. enabled[i] = 0;
  4295. first[i] = -1;
  4296. }
  4297. for (i = 0; i < s->nb_streams; i++) {
  4298. AVStream *st = s->streams[i];
  4299. if (st->codec->codec_type <= AVMEDIA_TYPE_UNKNOWN ||
  4300. st->codec->codec_type >= AVMEDIA_TYPE_NB)
  4301. continue;
  4302. if (first[st->codec->codec_type] < 0)
  4303. first[st->codec->codec_type] = i;
  4304. if (st->disposition & AV_DISPOSITION_DEFAULT) {
  4305. mov->tracks[i].flags |= MOV_TRACK_ENABLED;
  4306. enabled[st->codec->codec_type]++;
  4307. }
  4308. }
  4309. for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
  4310. switch (i) {
  4311. case AVMEDIA_TYPE_VIDEO:
  4312. case AVMEDIA_TYPE_AUDIO:
  4313. case AVMEDIA_TYPE_SUBTITLE:
  4314. if (enabled[i] > 1)
  4315. mov->per_stream_grouping = 1;
  4316. if (!enabled[i] && first[i] >= 0)
  4317. mov->tracks[first[i]].flags |= MOV_TRACK_ENABLED;
  4318. break;
  4319. }
  4320. }
  4321. }
  4322. static void mov_free(AVFormatContext *s)
  4323. {
  4324. MOVMuxContext *mov = s->priv_data;
  4325. int i;
  4326. if (mov->chapter_track) {
  4327. if (mov->tracks[mov->chapter_track].enc)
  4328. av_freep(&mov->tracks[mov->chapter_track].enc->extradata);
  4329. av_freep(&mov->tracks[mov->chapter_track].enc);
  4330. }
  4331. for (i = 0; i < mov->nb_streams; i++) {
  4332. if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
  4333. ff_mov_close_hinting(&mov->tracks[i]);
  4334. else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
  4335. av_freep(&mov->tracks[i].enc);
  4336. av_freep(&mov->tracks[i].cluster);
  4337. av_freep(&mov->tracks[i].frag_info);
  4338. if (mov->tracks[i].vos_len)
  4339. av_freep(&mov->tracks[i].vos_data);
  4340. }
  4341. av_freep(&mov->tracks);
  4342. }
  4343. static uint32_t rgb_to_yuv(uint32_t rgb)
  4344. {
  4345. uint8_t r, g, b;
  4346. int y, cb, cr;
  4347. r = (rgb >> 16) & 0xFF;
  4348. g = (rgb >> 8) & 0xFF;
  4349. b = (rgb ) & 0xFF;
  4350. y = av_clip_uint8( 16. + 0.257 * r + 0.504 * g + 0.098 * b);
  4351. cb = av_clip_uint8(128. - 0.148 * r - 0.291 * g + 0.439 * b);
  4352. cr = av_clip_uint8(128. + 0.439 * r - 0.368 * g - 0.071 * b);
  4353. return (y << 16) | (cr << 8) | cb;
  4354. }
  4355. static int mov_create_dvd_sub_decoder_specific_info(MOVTrack *track,
  4356. AVStream *st)
  4357. {
  4358. int i, width = 720, height = 480;
  4359. int have_palette = 0, have_size = 0;
  4360. uint32_t palette[16];
  4361. char *cur = st->codec->extradata;
  4362. while (cur && *cur) {
  4363. if (strncmp("palette:", cur, 8) == 0) {
  4364. int i, count;
  4365. count = sscanf(cur + 8,
  4366. "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
  4367. "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
  4368. "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32", "
  4369. "%06"PRIx32", %06"PRIx32", %06"PRIx32", %06"PRIx32"",
  4370. &palette[ 0], &palette[ 1], &palette[ 2], &palette[ 3],
  4371. &palette[ 4], &palette[ 5], &palette[ 6], &palette[ 7],
  4372. &palette[ 8], &palette[ 9], &palette[10], &palette[11],
  4373. &palette[12], &palette[13], &palette[14], &palette[15]);
  4374. for (i = 0; i < count; i++) {
  4375. palette[i] = rgb_to_yuv(palette[i]);
  4376. }
  4377. have_palette = 1;
  4378. } else if (!strncmp("size:", cur, 5)) {
  4379. sscanf(cur + 5, "%dx%d", &width, &height);
  4380. have_size = 1;
  4381. }
  4382. if (have_palette && have_size)
  4383. break;
  4384. cur += strcspn(cur, "\n\r");
  4385. cur += strspn(cur, "\n\r");
  4386. }
  4387. if (have_palette) {
  4388. track->vos_data = av_malloc(16*4);
  4389. if (!track->vos_data)
  4390. return AVERROR(ENOMEM);
  4391. for (i = 0; i < 16; i++) {
  4392. AV_WB32(track->vos_data + i * 4, palette[i]);
  4393. }
  4394. track->vos_len = 16 * 4;
  4395. }
  4396. st->codec->width = width;
  4397. st->codec->height = track->height = height;
  4398. return 0;
  4399. }
  4400. static int mov_write_header(AVFormatContext *s)
  4401. {
  4402. AVIOContext *pb = s->pb;
  4403. MOVMuxContext *mov = s->priv_data;
  4404. AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  4405. int i, ret, hint_track = 0, tmcd_track = 0;
  4406. mov->fc = s;
  4407. /* Default mode == MP4 */
  4408. mov->mode = MODE_MP4;
  4409. if (s->oformat) {
  4410. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  4411. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
  4412. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  4413. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  4414. else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
  4415. else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
  4416. else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
  4417. }
  4418. if (s->flags & AVFMT_FLAG_BITEXACT)
  4419. mov->exact = 1;
  4420. if (mov->flags & FF_MOV_FLAG_DELAY_MOOV)
  4421. mov->flags |= FF_MOV_FLAG_EMPTY_MOOV;
  4422. /* Set the FRAGMENT flag if any of the fragmentation methods are
  4423. * enabled. */
  4424. if (mov->max_fragment_duration || mov->max_fragment_size ||
  4425. mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
  4426. FF_MOV_FLAG_FRAG_KEYFRAME |
  4427. FF_MOV_FLAG_FRAG_CUSTOM))
  4428. mov->flags |= FF_MOV_FLAG_FRAGMENT;
  4429. /* Set other implicit flags immediately */
  4430. if (mov->mode == MODE_ISM)
  4431. mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
  4432. FF_MOV_FLAG_FRAGMENT;
  4433. if (mov->flags & FF_MOV_FLAG_DASH)
  4434. mov->flags |= FF_MOV_FLAG_FRAGMENT | FF_MOV_FLAG_EMPTY_MOOV |
  4435. FF_MOV_FLAG_DEFAULT_BASE_MOOF;
  4436. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  4437. mov->reserved_moov_size = -1;
  4438. }
  4439. if (mov->use_editlist < 0) {
  4440. mov->use_editlist = 1;
  4441. if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
  4442. !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
  4443. // If we can avoid needing an edit list by shifting the
  4444. // tracks, prefer that over (trying to) write edit lists
  4445. // in fragmented output.
  4446. if (s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO ||
  4447. s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_MAKE_ZERO)
  4448. mov->use_editlist = 0;
  4449. }
  4450. }
  4451. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
  4452. !(mov->flags & FF_MOV_FLAG_DELAY_MOOV) && mov->use_editlist)
  4453. av_log(s, AV_LOG_WARNING, "No meaningful edit list will be written when using empty_moov without delay_moov\n");
  4454. if (!mov->use_editlist && s->avoid_negative_ts == AVFMT_AVOID_NEG_TS_AUTO)
  4455. s->avoid_negative_ts = AVFMT_AVOID_NEG_TS_MAKE_ZERO;
  4456. /* Clear the omit_tfhd_offset flag if default_base_moof is set;
  4457. * if the latter is set that's enough and omit_tfhd_offset doesn't
  4458. * add anything extra on top of that. */
  4459. if (mov->flags & FF_MOV_FLAG_OMIT_TFHD_OFFSET &&
  4460. mov->flags & FF_MOV_FLAG_DEFAULT_BASE_MOOF)
  4461. mov->flags &= ~FF_MOV_FLAG_OMIT_TFHD_OFFSET;
  4462. if (mov->frag_interleave &&
  4463. mov->flags & (FF_MOV_FLAG_OMIT_TFHD_OFFSET | FF_MOV_FLAG_SEPARATE_MOOF)) {
  4464. av_log(s, AV_LOG_ERROR,
  4465. "Sample interleaving in fragments is mutually exclusive with "
  4466. "omit_tfhd_offset and separate_moof\n");
  4467. return AVERROR(EINVAL);
  4468. }
  4469. /* Non-seekable output is ok if using fragmentation. If ism_lookahead
  4470. * is enabled, we don't support non-seekable output at all. */
  4471. if (!s->pb->seekable &&
  4472. (!(mov->flags & FF_MOV_FLAG_FRAGMENT) || mov->ism_lookahead)) {
  4473. av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
  4474. return AVERROR(EINVAL);
  4475. }
  4476. if (!(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
  4477. if ((ret = mov_write_identification(pb, s)) < 0)
  4478. return ret;
  4479. }
  4480. mov->nb_streams = s->nb_streams;
  4481. if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters)
  4482. mov->chapter_track = mov->nb_streams++;
  4483. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  4484. /* Add hint tracks for each audio and video stream */
  4485. hint_track = mov->nb_streams;
  4486. for (i = 0; i < s->nb_streams; i++) {
  4487. AVStream *st = s->streams[i];
  4488. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  4489. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  4490. mov->nb_streams++;
  4491. }
  4492. }
  4493. }
  4494. if (mov->mode == MODE_MOV) {
  4495. tmcd_track = mov->nb_streams;
  4496. /* +1 tmcd track for each video stream with a timecode */
  4497. for (i = 0; i < s->nb_streams; i++) {
  4498. AVStream *st = s->streams[i];
  4499. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  4500. (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
  4501. mov->nb_meta_tmcd++;
  4502. }
  4503. /* check if there is already a tmcd track to remux */
  4504. if (mov->nb_meta_tmcd) {
  4505. for (i = 0; i < s->nb_streams; i++) {
  4506. AVStream *st = s->streams[i];
  4507. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  4508. av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
  4509. "so timecode metadata are now ignored\n");
  4510. mov->nb_meta_tmcd = 0;
  4511. }
  4512. }
  4513. }
  4514. mov->nb_streams += mov->nb_meta_tmcd;
  4515. }
  4516. // Reserve an extra stream for chapters for the case where chapters
  4517. // are written in the trailer
  4518. mov->tracks = av_mallocz_array((mov->nb_streams + 1), sizeof(*mov->tracks));
  4519. if (!mov->tracks)
  4520. return AVERROR(ENOMEM);
  4521. for (i = 0; i < s->nb_streams; i++) {
  4522. AVStream *st= s->streams[i];
  4523. MOVTrack *track= &mov->tracks[i];
  4524. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
  4525. track->st = st;
  4526. track->enc = st->codec;
  4527. track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
  4528. if (track->language < 0)
  4529. track->language = 0;
  4530. track->mode = mov->mode;
  4531. track->tag = mov_find_codec_tag(s, track);
  4532. if (!track->tag) {
  4533. av_log(s, AV_LOG_ERROR, "Could not find tag for codec %s in stream #%d, "
  4534. "codec not currently supported in container\n",
  4535. avcodec_get_name(st->codec->codec_id), i);
  4536. ret = AVERROR(EINVAL);
  4537. goto error;
  4538. }
  4539. /* If hinting of this track is enabled by a later hint track,
  4540. * this is updated. */
  4541. track->hint_track = -1;
  4542. track->start_dts = AV_NOPTS_VALUE;
  4543. track->start_cts = AV_NOPTS_VALUE;
  4544. track->end_pts = AV_NOPTS_VALUE;
  4545. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  4546. if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
  4547. track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
  4548. track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
  4549. if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
  4550. av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
  4551. ret = AVERROR(EINVAL);
  4552. goto error;
  4553. }
  4554. track->height = track->tag >> 24 == 'n' ? 486 : 576;
  4555. }
  4556. if (mov->video_track_timescale) {
  4557. track->timescale = mov->video_track_timescale;
  4558. } else {
  4559. track->timescale = st->time_base.den;
  4560. while(track->timescale < 10000)
  4561. track->timescale *= 2;
  4562. }
  4563. if (st->codec->width > 65535 || st->codec->height > 65535) {
  4564. av_log(s, AV_LOG_ERROR, "Resolution %dx%d too large for mov/mp4\n", st->codec->width, st->codec->height);
  4565. ret = AVERROR(EINVAL);
  4566. goto error;
  4567. }
  4568. if (track->mode == MODE_MOV && track->timescale > 100000)
  4569. av_log(s, AV_LOG_WARNING,
  4570. "WARNING codec timebase is very high. If duration is too long,\n"
  4571. "file may not be playable by quicktime. Specify a shorter timebase\n"
  4572. "or choose different container.\n");
  4573. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  4574. track->timescale = st->codec->sample_rate;
  4575. if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
  4576. av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
  4577. track->audio_vbr = 1;
  4578. }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
  4579. st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  4580. st->codec->codec_id == AV_CODEC_ID_ILBC){
  4581. if (!st->codec->block_align) {
  4582. av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
  4583. ret = AVERROR(EINVAL);
  4584. goto error;
  4585. }
  4586. track->sample_size = st->codec->block_align;
  4587. }else if (st->codec->frame_size > 1){ /* assume compressed audio */
  4588. track->audio_vbr = 1;
  4589. }else{
  4590. track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
  4591. }
  4592. if (st->codec->codec_id == AV_CODEC_ID_ILBC ||
  4593. st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_QT) {
  4594. track->audio_vbr = 1;
  4595. }
  4596. if (track->mode != MODE_MOV &&
  4597. track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
  4598. av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
  4599. i, track->enc->sample_rate);
  4600. ret = AVERROR(EINVAL);
  4601. goto error;
  4602. }
  4603. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  4604. track->timescale = st->time_base.den;
  4605. } else if (st->codec->codec_type == AVMEDIA_TYPE_DATA) {
  4606. track->timescale = st->time_base.den;
  4607. } else {
  4608. track->timescale = MOV_TIMESCALE;
  4609. }
  4610. if (!track->height)
  4611. track->height = st->codec->height;
  4612. /* The ism specific timescale isn't mandatory, but is assumed by
  4613. * some tools, such as mp4split. */
  4614. if (mov->mode == MODE_ISM)
  4615. track->timescale = 10000000;
  4616. avpriv_set_pts_info(st, 64, 1, track->timescale);
  4617. /* copy extradata if it exists */
  4618. if (st->codec->extradata_size) {
  4619. if (st->codec->codec_id == AV_CODEC_ID_DVD_SUBTITLE)
  4620. mov_create_dvd_sub_decoder_specific_info(track, st);
  4621. else if (!TAG_IS_AVCI(track->tag) && st->codec->codec_id != AV_CODEC_ID_DNXHD) {
  4622. track->vos_len = st->codec->extradata_size;
  4623. track->vos_data = av_malloc(track->vos_len);
  4624. if (!track->vos_data) {
  4625. ret = AVERROR(ENOMEM);
  4626. goto error;
  4627. }
  4628. memcpy(track->vos_data, st->codec->extradata, track->vos_len);
  4629. }
  4630. }
  4631. }
  4632. for (i = 0; i < s->nb_streams; i++) {
  4633. int j;
  4634. AVStream *st= s->streams[i];
  4635. MOVTrack *track= &mov->tracks[i];
  4636. if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
  4637. track->enc->channel_layout != AV_CH_LAYOUT_MONO)
  4638. continue;
  4639. for (j = 0; j < s->nb_streams; j++) {
  4640. AVStream *stj= s->streams[j];
  4641. MOVTrack *trackj= &mov->tracks[j];
  4642. if (j == i)
  4643. continue;
  4644. if (stj->codec->codec_type != AVMEDIA_TYPE_AUDIO ||
  4645. trackj->enc->channel_layout != AV_CH_LAYOUT_MONO ||
  4646. trackj->language != track->language ||
  4647. trackj->tag != track->tag
  4648. )
  4649. continue;
  4650. track->multichannel_as_mono++;
  4651. }
  4652. }
  4653. enable_tracks(s);
  4654. if (mov->reserved_moov_size){
  4655. mov->reserved_moov_pos= avio_tell(pb);
  4656. if (mov->reserved_moov_size > 0)
  4657. avio_skip(pb, mov->reserved_moov_size);
  4658. }
  4659. if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
  4660. /* If no fragmentation options have been set, set a default. */
  4661. if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
  4662. FF_MOV_FLAG_FRAG_CUSTOM)) &&
  4663. !mov->max_fragment_duration && !mov->max_fragment_size)
  4664. mov->flags |= FF_MOV_FLAG_FRAG_KEYFRAME;
  4665. } else {
  4666. if (mov->flags & FF_MOV_FLAG_FASTSTART)
  4667. mov->reserved_moov_pos = avio_tell(pb);
  4668. mov_write_mdat_tag(pb, mov);
  4669. }
  4670. if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
  4671. mov->time = ff_iso8601_to_unix_time(t->value);
  4672. if (mov->time)
  4673. mov->time += 0x7C25B080; // 1970 based -> 1904 based
  4674. if (mov->chapter_track)
  4675. if ((ret = mov_create_chapter_track(s, mov->chapter_track)) < 0)
  4676. goto error;
  4677. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  4678. /* Initialize the hint tracks for each audio and video stream */
  4679. for (i = 0; i < s->nb_streams; i++) {
  4680. AVStream *st = s->streams[i];
  4681. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  4682. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  4683. if ((ret = ff_mov_init_hinting(s, hint_track, i)) < 0)
  4684. goto error;
  4685. hint_track++;
  4686. }
  4687. }
  4688. }
  4689. if (mov->nb_meta_tmcd) {
  4690. /* Initialize the tmcd tracks */
  4691. for (i = 0; i < s->nb_streams; i++) {
  4692. AVStream *st = s->streams[i];
  4693. t = global_tcr;
  4694. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  4695. if (!t)
  4696. t = av_dict_get(st->metadata, "timecode", NULL, 0);
  4697. if (!t)
  4698. continue;
  4699. if ((ret = mov_create_timecode_track(s, tmcd_track, i, t->value)) < 0)
  4700. goto error;
  4701. tmcd_track++;
  4702. }
  4703. }
  4704. }
  4705. avio_flush(pb);
  4706. if (mov->flags & FF_MOV_FLAG_ISML)
  4707. mov_write_isml_manifest(pb, mov);
  4708. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV &&
  4709. !(mov->flags & FF_MOV_FLAG_DELAY_MOOV)) {
  4710. if ((ret = mov_write_moov_tag(pb, mov, s)) < 0)
  4711. return ret;
  4712. mov->moov_written = 1;
  4713. if (mov->flags & FF_MOV_FLAG_FASTSTART)
  4714. mov->reserved_moov_pos = avio_tell(pb);
  4715. }
  4716. return 0;
  4717. error:
  4718. mov_free(s);
  4719. return ret;
  4720. }
  4721. static int get_moov_size(AVFormatContext *s)
  4722. {
  4723. int ret;
  4724. AVIOContext *moov_buf;
  4725. MOVMuxContext *mov = s->priv_data;
  4726. if ((ret = ffio_open_null_buf(&moov_buf)) < 0)
  4727. return ret;
  4728. if ((ret = mov_write_moov_tag(moov_buf, mov, s)) < 0)
  4729. return ret;
  4730. return ffio_close_null_buf(moov_buf);
  4731. }
  4732. static int get_sidx_size(AVFormatContext *s)
  4733. {
  4734. int ret;
  4735. AVIOContext *buf;
  4736. MOVMuxContext *mov = s->priv_data;
  4737. if ((ret = ffio_open_null_buf(&buf)) < 0)
  4738. return ret;
  4739. mov_write_sidx_tags(buf, mov, -1, 0);
  4740. return ffio_close_null_buf(buf);
  4741. }
  4742. /*
  4743. * This function gets the moov size if moved to the top of the file: the chunk
  4744. * offset table can switch between stco (32-bit entries) to co64 (64-bit
  4745. * entries) when the moov is moved to the beginning, so the size of the moov
  4746. * would change. It also updates the chunk offset tables.
  4747. */
  4748. static int compute_moov_size(AVFormatContext *s)
  4749. {
  4750. int i, moov_size, moov_size2;
  4751. MOVMuxContext *mov = s->priv_data;
  4752. moov_size = get_moov_size(s);
  4753. if (moov_size < 0)
  4754. return moov_size;
  4755. for (i = 0; i < mov->nb_streams; i++)
  4756. mov->tracks[i].data_offset += moov_size;
  4757. moov_size2 = get_moov_size(s);
  4758. if (moov_size2 < 0)
  4759. return moov_size2;
  4760. /* if the size changed, we just switched from stco to co64 and need to
  4761. * update the offsets */
  4762. if (moov_size2 != moov_size)
  4763. for (i = 0; i < mov->nb_streams; i++)
  4764. mov->tracks[i].data_offset += moov_size2 - moov_size;
  4765. return moov_size2;
  4766. }
  4767. static int compute_sidx_size(AVFormatContext *s)
  4768. {
  4769. int i, sidx_size;
  4770. MOVMuxContext *mov = s->priv_data;
  4771. sidx_size = get_sidx_size(s);
  4772. if (sidx_size < 0)
  4773. return sidx_size;
  4774. for (i = 0; i < mov->nb_streams; i++)
  4775. mov->tracks[i].data_offset += sidx_size;
  4776. return sidx_size;
  4777. }
  4778. static int shift_data(AVFormatContext *s)
  4779. {
  4780. int ret = 0, moov_size;
  4781. MOVMuxContext *mov = s->priv_data;
  4782. int64_t pos, pos_end = avio_tell(s->pb);
  4783. uint8_t *buf, *read_buf[2];
  4784. int read_buf_id = 0;
  4785. int read_size[2];
  4786. AVIOContext *read_pb;
  4787. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  4788. moov_size = compute_sidx_size(s);
  4789. else
  4790. moov_size = compute_moov_size(s);
  4791. if (moov_size < 0)
  4792. return moov_size;
  4793. buf = av_malloc(moov_size * 2);
  4794. if (!buf)
  4795. return AVERROR(ENOMEM);
  4796. read_buf[0] = buf;
  4797. read_buf[1] = buf + moov_size;
  4798. /* Shift the data: the AVIO context of the output can only be used for
  4799. * writing, so we re-open the same output, but for reading. It also avoids
  4800. * a read/seek/write/seek back and forth. */
  4801. avio_flush(s->pb);
  4802. ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
  4803. if (ret < 0) {
  4804. av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
  4805. "the second pass (faststart)\n", s->filename);
  4806. goto end;
  4807. }
  4808. /* mark the end of the shift to up to the last data we wrote, and get ready
  4809. * for writing */
  4810. pos_end = avio_tell(s->pb);
  4811. avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
  4812. /* start reading at where the new moov will be placed */
  4813. avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
  4814. pos = avio_tell(read_pb);
  4815. #define READ_BLOCK do { \
  4816. read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
  4817. read_buf_id ^= 1; \
  4818. } while (0)
  4819. /* shift data by chunk of at most moov_size */
  4820. READ_BLOCK;
  4821. do {
  4822. int n;
  4823. READ_BLOCK;
  4824. n = read_size[read_buf_id];
  4825. if (n <= 0)
  4826. break;
  4827. avio_write(s->pb, read_buf[read_buf_id], n);
  4828. pos += n;
  4829. } while (pos < pos_end);
  4830. avio_close(read_pb);
  4831. end:
  4832. av_free(buf);
  4833. return ret;
  4834. }
  4835. static int mov_write_trailer(AVFormatContext *s)
  4836. {
  4837. MOVMuxContext *mov = s->priv_data;
  4838. AVIOContext *pb = s->pb;
  4839. int res = 0;
  4840. int i;
  4841. int64_t moov_pos;
  4842. /*
  4843. * Before actually writing the trailer, make sure that there are no
  4844. * dangling subtitles, that need a terminating sample.
  4845. */
  4846. for (i = 0; i < mov->nb_streams; i++) {
  4847. MOVTrack *trk = &mov->tracks[i];
  4848. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  4849. !trk->last_sample_is_subtitle_end) {
  4850. mov_write_subtitle_end_packet(s, i, trk->track_duration);
  4851. trk->last_sample_is_subtitle_end = 1;
  4852. }
  4853. }
  4854. // If there were no chapters when the header was written, but there
  4855. // are chapters now, write them in the trailer. This only works
  4856. // when we are not doing fragments.
  4857. if (!mov->chapter_track && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  4858. if (mov->mode & (MODE_MP4|MODE_MOV|MODE_IPOD) && s->nb_chapters) {
  4859. mov->chapter_track = mov->nb_streams++;
  4860. if ((res = mov_create_chapter_track(s, mov->chapter_track)) < 0)
  4861. goto error;
  4862. }
  4863. }
  4864. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  4865. moov_pos = avio_tell(pb);
  4866. /* Write size of mdat tag */
  4867. if (mov->mdat_size + 8 <= UINT32_MAX) {
  4868. avio_seek(pb, mov->mdat_pos, SEEK_SET);
  4869. avio_wb32(pb, mov->mdat_size + 8);
  4870. } else {
  4871. /* overwrite 'wide' placeholder atom */
  4872. avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
  4873. /* special value: real atom size will be 64 bit value after
  4874. * tag field */
  4875. avio_wb32(pb, 1);
  4876. ffio_wfourcc(pb, "mdat");
  4877. avio_wb64(pb, mov->mdat_size + 16);
  4878. }
  4879. avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
  4880. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  4881. av_log(s, AV_LOG_INFO, "Starting second pass: moving the moov atom to the beginning of the file\n");
  4882. res = shift_data(s);
  4883. if (res == 0) {
  4884. avio_seek(pb, mov->reserved_moov_pos, SEEK_SET);
  4885. if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
  4886. goto error;
  4887. }
  4888. } else if (mov->reserved_moov_size > 0) {
  4889. int64_t size;
  4890. if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
  4891. goto error;
  4892. size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
  4893. if (size < 8){
  4894. av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
  4895. res = AVERROR(EINVAL);
  4896. goto error;
  4897. }
  4898. avio_wb32(pb, size);
  4899. ffio_wfourcc(pb, "free");
  4900. ffio_fill(pb, 0, size - 8);
  4901. avio_seek(pb, moov_pos, SEEK_SET);
  4902. } else {
  4903. if ((res = mov_write_moov_tag(pb, mov, s)) < 0)
  4904. goto error;
  4905. }
  4906. res = 0;
  4907. } else {
  4908. mov_auto_flush_fragment(s);
  4909. for (i = 0; i < mov->nb_streams; i++)
  4910. mov->tracks[i].data_offset = 0;
  4911. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  4912. av_log(s, AV_LOG_INFO, "Starting second pass: inserting sidx atoms\n");
  4913. res = shift_data(s);
  4914. if (res == 0) {
  4915. int64_t end = avio_tell(pb);
  4916. avio_seek(pb, mov->reserved_moov_pos, SEEK_SET);
  4917. mov_write_sidx_tags(pb, mov, -1, 0);
  4918. avio_seek(pb, end, SEEK_SET);
  4919. mov_write_mfra_tag(pb, mov);
  4920. }
  4921. } else {
  4922. mov_write_mfra_tag(pb, mov);
  4923. }
  4924. }
  4925. error:
  4926. mov_free(s);
  4927. return res;
  4928. }
  4929. #if CONFIG_MOV_MUXER
  4930. MOV_CLASS(mov)
  4931. AVOutputFormat ff_mov_muxer = {
  4932. .name = "mov",
  4933. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  4934. .extensions = "mov",
  4935. .priv_data_size = sizeof(MOVMuxContext),
  4936. .audio_codec = AV_CODEC_ID_AAC,
  4937. .video_codec = CONFIG_LIBX264_ENCODER ?
  4938. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  4939. .write_header = mov_write_header,
  4940. .write_packet = mov_write_packet,
  4941. .write_trailer = mov_write_trailer,
  4942. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  4943. .codec_tag = (const AVCodecTag* const []){
  4944. ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
  4945. },
  4946. .priv_class = &mov_muxer_class,
  4947. };
  4948. #endif
  4949. #if CONFIG_TGP_MUXER
  4950. MOV_CLASS(tgp)
  4951. AVOutputFormat ff_tgp_muxer = {
  4952. .name = "3gp",
  4953. .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
  4954. .extensions = "3gp",
  4955. .priv_data_size = sizeof(MOVMuxContext),
  4956. .audio_codec = AV_CODEC_ID_AMR_NB,
  4957. .video_codec = AV_CODEC_ID_H263,
  4958. .write_header = mov_write_header,
  4959. .write_packet = mov_write_packet,
  4960. .write_trailer = mov_write_trailer,
  4961. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  4962. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  4963. .priv_class = &tgp_muxer_class,
  4964. };
  4965. #endif
  4966. #if CONFIG_MP4_MUXER
  4967. MOV_CLASS(mp4)
  4968. AVOutputFormat ff_mp4_muxer = {
  4969. .name = "mp4",
  4970. .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
  4971. .mime_type = "application/mp4",
  4972. .extensions = "mp4",
  4973. .priv_data_size = sizeof(MOVMuxContext),
  4974. .audio_codec = AV_CODEC_ID_AAC,
  4975. .video_codec = CONFIG_LIBX264_ENCODER ?
  4976. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  4977. .write_header = mov_write_header,
  4978. .write_packet = mov_write_packet,
  4979. .write_trailer = mov_write_trailer,
  4980. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  4981. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  4982. .priv_class = &mp4_muxer_class,
  4983. };
  4984. #endif
  4985. #if CONFIG_PSP_MUXER
  4986. MOV_CLASS(psp)
  4987. AVOutputFormat ff_psp_muxer = {
  4988. .name = "psp",
  4989. .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
  4990. .extensions = "mp4,psp",
  4991. .priv_data_size = sizeof(MOVMuxContext),
  4992. .audio_codec = AV_CODEC_ID_AAC,
  4993. .video_codec = CONFIG_LIBX264_ENCODER ?
  4994. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  4995. .write_header = mov_write_header,
  4996. .write_packet = mov_write_packet,
  4997. .write_trailer = mov_write_trailer,
  4998. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  4999. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  5000. .priv_class = &psp_muxer_class,
  5001. };
  5002. #endif
  5003. #if CONFIG_TG2_MUXER
  5004. MOV_CLASS(tg2)
  5005. AVOutputFormat ff_tg2_muxer = {
  5006. .name = "3g2",
  5007. .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
  5008. .extensions = "3g2",
  5009. .priv_data_size = sizeof(MOVMuxContext),
  5010. .audio_codec = AV_CODEC_ID_AMR_NB,
  5011. .video_codec = AV_CODEC_ID_H263,
  5012. .write_header = mov_write_header,
  5013. .write_packet = mov_write_packet,
  5014. .write_trailer = mov_write_trailer,
  5015. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  5016. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  5017. .priv_class = &tg2_muxer_class,
  5018. };
  5019. #endif
  5020. #if CONFIG_IPOD_MUXER
  5021. MOV_CLASS(ipod)
  5022. AVOutputFormat ff_ipod_muxer = {
  5023. .name = "ipod",
  5024. .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
  5025. .mime_type = "application/mp4",
  5026. .extensions = "m4v,m4a",
  5027. .priv_data_size = sizeof(MOVMuxContext),
  5028. .audio_codec = AV_CODEC_ID_AAC,
  5029. .video_codec = AV_CODEC_ID_H264,
  5030. .write_header = mov_write_header,
  5031. .write_packet = mov_write_packet,
  5032. .write_trailer = mov_write_trailer,
  5033. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  5034. .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
  5035. .priv_class = &ipod_muxer_class,
  5036. };
  5037. #endif
  5038. #if CONFIG_ISMV_MUXER
  5039. MOV_CLASS(ismv)
  5040. AVOutputFormat ff_ismv_muxer = {
  5041. .name = "ismv",
  5042. .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
  5043. .mime_type = "application/mp4",
  5044. .extensions = "ismv,isma",
  5045. .priv_data_size = sizeof(MOVMuxContext),
  5046. .audio_codec = AV_CODEC_ID_AAC,
  5047. .video_codec = AV_CODEC_ID_H264,
  5048. .write_header = mov_write_header,
  5049. .write_packet = mov_write_packet,
  5050. .write_trailer = mov_write_trailer,
  5051. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  5052. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  5053. .priv_class = &ismv_muxer_class,
  5054. };
  5055. #endif
  5056. #if CONFIG_F4V_MUXER
  5057. MOV_CLASS(f4v)
  5058. AVOutputFormat ff_f4v_muxer = {
  5059. .name = "f4v",
  5060. .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
  5061. .mime_type = "application/f4v",
  5062. .extensions = "f4v",
  5063. .priv_data_size = sizeof(MOVMuxContext),
  5064. .audio_codec = AV_CODEC_ID_AAC,
  5065. .video_codec = AV_CODEC_ID_H264,
  5066. .write_header = mov_write_header,
  5067. .write_packet = mov_write_packet,
  5068. .write_trailer = mov_write_trailer,
  5069. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  5070. .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
  5071. .priv_class = &f4v_muxer_class,
  5072. };
  5073. #endif