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.

6286 lines
227KB

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