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.

5230 lines
185KB

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