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.

5653 lines
202KB

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