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.

4129 lines
144KB

  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 "movenc.h"
  24. #include "avformat.h"
  25. #include "avio_internal.h"
  26. #include "riff.h"
  27. #include "avio.h"
  28. #include "isom.h"
  29. #include "avc.h"
  30. #include "libavcodec/get_bits.h"
  31. #include "libavcodec/put_bits.h"
  32. #include "libavcodec/vc1.h"
  33. #include "internal.h"
  34. #include "libavutil/avstring.h"
  35. #include "libavutil/intfloat.h"
  36. #include "libavutil/mathematics.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/dict.h"
  39. #include "rtpenc.h"
  40. #include "mov_chan.h"
  41. #undef NDEBUG
  42. #include <assert.h>
  43. static const AVOption options[] = {
  44. { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  45. { "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" },
  46. { "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 },
  47. { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.i64 = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  48. { "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" },
  49. { "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" },
  50. { "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" },
  51. { "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" },
  52. { "faststart", "Run a second pass to put the moov 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" },
  53. FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
  54. { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  55. { "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},
  56. { "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},
  57. { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  58. { "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},
  59. { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  60. { "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},
  61. { "use_editlist", "use edit list", offsetof(MOVMuxContext, use_editlist), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, AV_OPT_FLAG_ENCODING_PARAM},
  62. { "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},
  63. { NULL },
  64. };
  65. #define MOV_CLASS(flavor)\
  66. static const AVClass flavor ## _muxer_class = {\
  67. .class_name = #flavor " muxer",\
  68. .item_name = av_default_item_name,\
  69. .option = options,\
  70. .version = LIBAVUTIL_VERSION_INT,\
  71. };
  72. //FIXME support 64 bit variant with wide placeholders
  73. static int64_t update_size(AVIOContext *pb, int64_t pos)
  74. {
  75. int64_t curpos = avio_tell(pb);
  76. avio_seek(pb, pos, SEEK_SET);
  77. avio_wb32(pb, curpos - pos); /* rewrite size */
  78. avio_seek(pb, curpos, SEEK_SET);
  79. return curpos - pos;
  80. }
  81. static int supports_edts(MOVMuxContext *mov)
  82. {
  83. // EDTS with fragments is tricky as we don't know the duration when its written
  84. return (mov->use_editlist<0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT)) || mov->use_editlist>0;
  85. }
  86. static int is_co64_required(const MOVTrack *track)
  87. {
  88. int i;
  89. for (i = 0; i < track->entry; i++) {
  90. if (!track->cluster[i].chunkNum)
  91. continue;
  92. if (track->cluster[i].pos + track->data_offset > UINT32_MAX)
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. /* Chunk offset atom */
  98. static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
  99. {
  100. int i;
  101. int mode64 = is_co64_required(track); // use 32 bit size variant if possible
  102. int64_t pos = avio_tell(pb);
  103. avio_wb32(pb, 0); /* size */
  104. if (mode64)
  105. ffio_wfourcc(pb, "co64");
  106. else
  107. ffio_wfourcc(pb, "stco");
  108. avio_wb32(pb, 0); /* version & flags */
  109. avio_wb32(pb, track->chunkCount); /* entry count */
  110. for (i = 0; i < track->entry; i++) {
  111. if (!track->cluster[i].chunkNum)
  112. continue;
  113. if (mode64 == 1)
  114. avio_wb64(pb, track->cluster[i].pos + track->data_offset);
  115. else
  116. avio_wb32(pb, track->cluster[i].pos + track->data_offset);
  117. }
  118. return update_size(pb, pos);
  119. }
  120. /* Sample size atom */
  121. static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
  122. {
  123. int equalChunks = 1;
  124. int i, j, entries = 0, tst = -1, oldtst = -1;
  125. int64_t pos = avio_tell(pb);
  126. avio_wb32(pb, 0); /* size */
  127. ffio_wfourcc(pb, "stsz");
  128. avio_wb32(pb, 0); /* version & flags */
  129. for (i = 0; i < track->entry; i++) {
  130. tst = track->cluster[i].size / track->cluster[i].entries;
  131. if (oldtst != -1 && tst != oldtst)
  132. equalChunks = 0;
  133. oldtst = tst;
  134. entries += track->cluster[i].entries;
  135. }
  136. if (equalChunks && track->entry) {
  137. int sSize = track->entry ? track->cluster[0].size / track->cluster[0].entries : 0;
  138. sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
  139. avio_wb32(pb, sSize); // sample size
  140. avio_wb32(pb, entries); // sample count
  141. } else {
  142. avio_wb32(pb, 0); // sample size
  143. avio_wb32(pb, entries); // sample count
  144. for (i = 0; i < track->entry; i++) {
  145. for (j = 0; j < track->cluster[i].entries; j++) {
  146. avio_wb32(pb, track->cluster[i].size /
  147. track->cluster[i].entries);
  148. }
  149. }
  150. }
  151. return update_size(pb, pos);
  152. }
  153. /* Sample to chunk atom */
  154. static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
  155. {
  156. int index = 0, oldval = -1, i;
  157. int64_t entryPos, curpos;
  158. int64_t pos = avio_tell(pb);
  159. avio_wb32(pb, 0); /* size */
  160. ffio_wfourcc(pb, "stsc");
  161. avio_wb32(pb, 0); // version & flags
  162. entryPos = avio_tell(pb);
  163. avio_wb32(pb, track->chunkCount); // entry count
  164. for (i = 0; i < track->entry; i++) {
  165. if (oldval != track->cluster[i].samples_in_chunk && track->cluster[i].chunkNum) {
  166. avio_wb32(pb, track->cluster[i].chunkNum); // first chunk
  167. avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
  168. avio_wb32(pb, 0x1); // sample description index
  169. oldval = track->cluster[i].samples_in_chunk;
  170. index++;
  171. }
  172. }
  173. curpos = avio_tell(pb);
  174. avio_seek(pb, entryPos, SEEK_SET);
  175. avio_wb32(pb, index); // rewrite size
  176. avio_seek(pb, curpos, SEEK_SET);
  177. return update_size(pb, pos);
  178. }
  179. /* Sync sample atom */
  180. static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
  181. {
  182. int64_t curpos, entryPos;
  183. int i, index = 0;
  184. int64_t pos = avio_tell(pb);
  185. avio_wb32(pb, 0); // size
  186. ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
  187. avio_wb32(pb, 0); // version & flags
  188. entryPos = avio_tell(pb);
  189. avio_wb32(pb, track->entry); // entry count
  190. for (i = 0; i < track->entry; i++) {
  191. if (track->cluster[i].flags & flag) {
  192. avio_wb32(pb, i + 1);
  193. index++;
  194. }
  195. }
  196. curpos = avio_tell(pb);
  197. avio_seek(pb, entryPos, SEEK_SET);
  198. avio_wb32(pb, index); // rewrite size
  199. avio_seek(pb, curpos, SEEK_SET);
  200. return update_size(pb, pos);
  201. }
  202. static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
  203. {
  204. avio_wb32(pb, 0x11); /* size */
  205. if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
  206. else ffio_wfourcc(pb, "damr");
  207. ffio_wfourcc(pb, "FFMP");
  208. avio_w8(pb, 0); /* decoder version */
  209. avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  210. avio_w8(pb, 0x00); /* Mode change period (no restriction) */
  211. avio_w8(pb, 0x01); /* Frames per sample */
  212. return 0x11;
  213. }
  214. static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
  215. {
  216. GetBitContext gbc;
  217. PutBitContext pbc;
  218. uint8_t buf[3];
  219. int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
  220. if (track->vos_len < 7)
  221. return -1;
  222. avio_wb32(pb, 11);
  223. ffio_wfourcc(pb, "dac3");
  224. init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
  225. fscod = get_bits(&gbc, 2);
  226. frmsizecod = get_bits(&gbc, 6);
  227. bsid = get_bits(&gbc, 5);
  228. bsmod = get_bits(&gbc, 3);
  229. acmod = get_bits(&gbc, 3);
  230. if (acmod == 2) {
  231. skip_bits(&gbc, 2); // dsurmod
  232. } else {
  233. if ((acmod & 1) && acmod != 1)
  234. skip_bits(&gbc, 2); // cmixlev
  235. if (acmod & 4)
  236. skip_bits(&gbc, 2); // surmixlev
  237. }
  238. lfeon = get_bits1(&gbc);
  239. init_put_bits(&pbc, buf, sizeof(buf));
  240. put_bits(&pbc, 2, fscod);
  241. put_bits(&pbc, 5, bsid);
  242. put_bits(&pbc, 3, bsmod);
  243. put_bits(&pbc, 3, acmod);
  244. put_bits(&pbc, 1, lfeon);
  245. put_bits(&pbc, 5, frmsizecod >> 1); // bit_rate_code
  246. put_bits(&pbc, 5, 0); // reserved
  247. flush_put_bits(&pbc);
  248. avio_write(pb, buf, sizeof(buf));
  249. return 11;
  250. }
  251. /**
  252. * This function writes extradata "as is".
  253. * Extradata must be formatted like a valid atom (with size and tag).
  254. */
  255. static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
  256. {
  257. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  258. return track->enc->extradata_size;
  259. }
  260. static int mov_write_enda_tag(AVIOContext *pb)
  261. {
  262. avio_wb32(pb, 10);
  263. ffio_wfourcc(pb, "enda");
  264. avio_wb16(pb, 1); /* little endian */
  265. return 10;
  266. }
  267. static int mov_write_enda_tag_be(AVIOContext *pb)
  268. {
  269. avio_wb32(pb, 10);
  270. ffio_wfourcc(pb, "enda");
  271. avio_wb16(pb, 0); /* big endian */
  272. return 10;
  273. }
  274. static void put_descr(AVIOContext *pb, int tag, unsigned int size)
  275. {
  276. int i = 3;
  277. avio_w8(pb, tag);
  278. for (; i > 0; i--)
  279. avio_w8(pb, (size >> (7 * i)) | 0x80);
  280. avio_w8(pb, size & 0x7F);
  281. }
  282. static unsigned compute_avg_bitrate(MOVTrack *track)
  283. {
  284. uint64_t size = 0;
  285. int i;
  286. if (!track->track_duration)
  287. return 0;
  288. for (i = 0; i < track->entry; i++)
  289. size += track->cluster[i].size;
  290. return size * 8 * track->timescale / track->track_duration;
  291. }
  292. static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
  293. {
  294. int64_t pos = avio_tell(pb);
  295. int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
  296. unsigned avg_bitrate;
  297. avio_wb32(pb, 0); // size
  298. ffio_wfourcc(pb, "esds");
  299. avio_wb32(pb, 0); // Version
  300. // ES descriptor
  301. put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
  302. avio_wb16(pb, track->track_id);
  303. avio_w8(pb, 0x00); // flags (= no flags)
  304. // DecoderConfig descriptor
  305. put_descr(pb, 0x04, 13 + decoder_specific_info_len);
  306. // Object type indication
  307. if ((track->enc->codec_id == AV_CODEC_ID_MP2 ||
  308. track->enc->codec_id == AV_CODEC_ID_MP3) &&
  309. track->enc->sample_rate > 24000)
  310. avio_w8(pb, 0x6B); // 11172-3
  311. else
  312. avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
  313. // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
  314. // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
  315. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  316. avio_w8(pb, 0x15); // flags (= Audiostream)
  317. else
  318. avio_w8(pb, 0x11); // flags (= Visualstream)
  319. avio_wb24(pb, track->enc->rc_buffer_size >> 3); // Buffersize DB
  320. avg_bitrate = compute_avg_bitrate(track);
  321. // maxbitrate (FIXME should be max rate in any 1 sec window)
  322. avio_wb32(pb, FFMAX3(track->enc->bit_rate, track->enc->rc_max_rate, avg_bitrate));
  323. avio_wb32(pb, avg_bitrate);
  324. if (track->vos_len) {
  325. // DecoderSpecific info descriptor
  326. put_descr(pb, 0x05, track->vos_len);
  327. avio_write(pb, track->vos_data, track->vos_len);
  328. }
  329. // SL descriptor
  330. put_descr(pb, 0x06, 1);
  331. avio_w8(pb, 0x02);
  332. return update_size(pb, pos);
  333. }
  334. static int mov_pcm_le_gt16(enum AVCodecID codec_id)
  335. {
  336. return codec_id == AV_CODEC_ID_PCM_S24LE ||
  337. codec_id == AV_CODEC_ID_PCM_S32LE ||
  338. codec_id == AV_CODEC_ID_PCM_F32LE ||
  339. codec_id == AV_CODEC_ID_PCM_F64LE;
  340. }
  341. static int mov_pcm_be_gt16(enum AVCodecID codec_id)
  342. {
  343. return codec_id == AV_CODEC_ID_PCM_S24BE ||
  344. codec_id == AV_CODEC_ID_PCM_S32BE ||
  345. codec_id == AV_CODEC_ID_PCM_F32BE ||
  346. codec_id == AV_CODEC_ID_PCM_F64BE;
  347. }
  348. static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
  349. {
  350. int64_t pos = avio_tell(pb);
  351. avio_wb32(pb, 0);
  352. avio_wl32(pb, track->tag); // store it byteswapped
  353. track->enc->codec_tag = av_bswap16(track->tag >> 16);
  354. ff_put_wav_header(pb, track->enc);
  355. return update_size(pb, pos);
  356. }
  357. static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
  358. {
  359. int64_t pos = avio_tell(pb);
  360. avio_wb32(pb, 0);
  361. ffio_wfourcc(pb, "wfex");
  362. ff_put_wav_header(pb, track->enc);
  363. return update_size(pb, pos);
  364. }
  365. static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
  366. {
  367. uint32_t layout_tag, bitmap;
  368. int64_t pos = avio_tell(pb);
  369. layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
  370. track->enc->channel_layout,
  371. &bitmap);
  372. if (!layout_tag) {
  373. av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
  374. "lack of channel information\n");
  375. return 0;
  376. }
  377. avio_wb32(pb, 0); // Size
  378. ffio_wfourcc(pb, "chan"); // Type
  379. avio_w8(pb, 0); // Version
  380. avio_wb24(pb, 0); // Flags
  381. avio_wb32(pb, layout_tag); // mChannelLayoutTag
  382. avio_wb32(pb, bitmap); // mChannelBitmap
  383. avio_wb32(pb, 0); // mNumberChannelDescriptions
  384. return update_size(pb, pos);
  385. }
  386. static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
  387. {
  388. int64_t pos = avio_tell(pb);
  389. avio_wb32(pb, 0); /* size */
  390. ffio_wfourcc(pb, "wave");
  391. if (track->enc->codec_id != AV_CODEC_ID_QDM2) {
  392. avio_wb32(pb, 12); /* size */
  393. ffio_wfourcc(pb, "frma");
  394. avio_wl32(pb, track->tag);
  395. }
  396. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  397. /* useless atom needed by mplayer, ipod, not needed by quicktime */
  398. avio_wb32(pb, 12); /* size */
  399. ffio_wfourcc(pb, "mp4a");
  400. avio_wb32(pb, 0);
  401. mov_write_esds_tag(pb, track);
  402. } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
  403. mov_write_enda_tag(pb);
  404. } else if (mov_pcm_be_gt16(track->enc->codec_id)) {
  405. mov_write_enda_tag_be(pb);
  406. } else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB) {
  407. mov_write_amr_tag(pb, track);
  408. } else if (track->enc->codec_id == AV_CODEC_ID_AC3) {
  409. mov_write_ac3_tag(pb, track);
  410. } else if (track->enc->codec_id == AV_CODEC_ID_ALAC ||
  411. track->enc->codec_id == AV_CODEC_ID_QDM2) {
  412. mov_write_extradata_tag(pb, track);
  413. } else if (track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  414. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  415. mov_write_ms_tag(pb, track);
  416. }
  417. avio_wb32(pb, 8); /* size */
  418. avio_wb32(pb, 0); /* null tag */
  419. return update_size(pb, pos);
  420. }
  421. static int mov_write_dvc1_structs(MOVTrack *track, uint8_t *buf)
  422. {
  423. uint8_t *unescaped;
  424. const uint8_t *start, *next, *end = track->vos_data + track->vos_len;
  425. int unescaped_size, seq_found = 0;
  426. int level = 0, interlace = 0;
  427. int packet_seq = track->vc1_info.packet_seq;
  428. int packet_entry = track->vc1_info.packet_entry;
  429. int slices = track->vc1_info.slices;
  430. PutBitContext pbc;
  431. if (track->start_dts == AV_NOPTS_VALUE) {
  432. /* No packets written yet, vc1_info isn't authoritative yet. */
  433. /* Assume inline sequence and entry headers. This will be
  434. * overwritten at the end if the file is seekable. */
  435. packet_seq = packet_entry = 1;
  436. }
  437. unescaped = av_mallocz(track->vos_len + FF_INPUT_BUFFER_PADDING_SIZE);
  438. if (!unescaped)
  439. return AVERROR(ENOMEM);
  440. start = find_next_marker(track->vos_data, end);
  441. for (next = start; next < end; start = next) {
  442. GetBitContext gb;
  443. int size;
  444. next = find_next_marker(start + 4, end);
  445. size = next - start - 4;
  446. if (size <= 0)
  447. continue;
  448. unescaped_size = vc1_unescape_buffer(start + 4, size, unescaped);
  449. init_get_bits(&gb, unescaped, 8 * unescaped_size);
  450. if (AV_RB32(start) == VC1_CODE_SEQHDR) {
  451. int profile = get_bits(&gb, 2);
  452. if (profile != PROFILE_ADVANCED) {
  453. av_free(unescaped);
  454. return AVERROR(ENOSYS);
  455. }
  456. seq_found = 1;
  457. level = get_bits(&gb, 3);
  458. /* chromaformat, frmrtq_postproc, bitrtq_postproc, postprocflag,
  459. * width, height */
  460. skip_bits_long(&gb, 2 + 3 + 5 + 1 + 2*12);
  461. skip_bits(&gb, 1); /* broadcast */
  462. interlace = get_bits1(&gb);
  463. skip_bits(&gb, 4); /* tfcntrflag, finterpflag, reserved, psf */
  464. }
  465. }
  466. if (!seq_found) {
  467. av_free(unescaped);
  468. return AVERROR(ENOSYS);
  469. }
  470. init_put_bits(&pbc, buf, 7);
  471. /* VC1DecSpecStruc */
  472. put_bits(&pbc, 4, 12); /* profile - advanced */
  473. put_bits(&pbc, 3, level);
  474. put_bits(&pbc, 1, 0); /* reserved */
  475. /* VC1AdvDecSpecStruc */
  476. put_bits(&pbc, 3, level);
  477. put_bits(&pbc, 1, 0); /* cbr */
  478. put_bits(&pbc, 6, 0); /* reserved */
  479. put_bits(&pbc, 1, !interlace); /* no interlace */
  480. put_bits(&pbc, 1, !packet_seq); /* no multiple seq */
  481. put_bits(&pbc, 1, !packet_entry); /* no multiple entry */
  482. put_bits(&pbc, 1, !slices); /* no slice code */
  483. put_bits(&pbc, 1, 0); /* no bframe */
  484. put_bits(&pbc, 1, 0); /* reserved */
  485. put_bits32(&pbc, track->enc->time_base.den); /* framerate */
  486. flush_put_bits(&pbc);
  487. av_free(unescaped);
  488. return 0;
  489. }
  490. static int mov_write_dvc1_tag(AVIOContext *pb, MOVTrack *track)
  491. {
  492. uint8_t buf[7] = { 0 };
  493. int ret;
  494. if ((ret = mov_write_dvc1_structs(track, buf)) < 0)
  495. return ret;
  496. avio_wb32(pb, track->vos_len + 8 + sizeof(buf));
  497. ffio_wfourcc(pb, "dvc1");
  498. track->vc1_info.struct_offset = avio_tell(pb);
  499. avio_write(pb, buf, sizeof(buf));
  500. avio_write(pb, track->vos_data, track->vos_len);
  501. return 0;
  502. }
  503. static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
  504. {
  505. avio_wb32(pb, track->vos_len + 8);
  506. ffio_wfourcc(pb, "glbl");
  507. avio_write(pb, track->vos_data, track->vos_len);
  508. return 8 + track->vos_len;
  509. }
  510. /**
  511. * Compute flags for 'lpcm' tag.
  512. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  513. */
  514. static int mov_get_lpcm_flags(enum AVCodecID codec_id)
  515. {
  516. switch (codec_id) {
  517. case AV_CODEC_ID_PCM_F32BE:
  518. case AV_CODEC_ID_PCM_F64BE:
  519. return 11;
  520. case AV_CODEC_ID_PCM_F32LE:
  521. case AV_CODEC_ID_PCM_F64LE:
  522. return 9;
  523. case AV_CODEC_ID_PCM_U8:
  524. return 10;
  525. case AV_CODEC_ID_PCM_S16BE:
  526. case AV_CODEC_ID_PCM_S24BE:
  527. case AV_CODEC_ID_PCM_S32BE:
  528. return 14;
  529. case AV_CODEC_ID_PCM_S8:
  530. case AV_CODEC_ID_PCM_S16LE:
  531. case AV_CODEC_ID_PCM_S24LE:
  532. case AV_CODEC_ID_PCM_S32LE:
  533. return 12;
  534. default:
  535. return 0;
  536. }
  537. }
  538. static int get_cluster_duration(MOVTrack *track, int cluster_idx)
  539. {
  540. int64_t next_dts;
  541. if (cluster_idx >= track->entry)
  542. return 0;
  543. if (cluster_idx + 1 == track->entry)
  544. next_dts = track->track_duration + track->start_dts;
  545. else
  546. next_dts = track->cluster[cluster_idx + 1].dts;
  547. return next_dts - track->cluster[cluster_idx].dts;
  548. }
  549. static int get_samples_per_packet(MOVTrack *track)
  550. {
  551. int i, first_duration;
  552. // return track->enc->frame_size;
  553. /* use 1 for raw PCM */
  554. if (!track->audio_vbr)
  555. return 1;
  556. /* check to see if duration is constant for all clusters */
  557. if (!track->entry)
  558. return 0;
  559. first_duration = get_cluster_duration(track, 0);
  560. for (i = 1; i < track->entry; i++) {
  561. if (get_cluster_duration(track, i) != first_duration)
  562. return 0;
  563. }
  564. return first_duration;
  565. }
  566. static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
  567. {
  568. int64_t pos = avio_tell(pb);
  569. int version = 0;
  570. uint32_t tag = track->tag;
  571. if (track->mode == MODE_MOV) {
  572. if (track->timescale > UINT16_MAX) {
  573. if (mov_get_lpcm_flags(track->enc->codec_id))
  574. tag = AV_RL32("lpcm");
  575. version = 2;
  576. } else if (track->audio_vbr || mov_pcm_le_gt16(track->enc->codec_id) ||
  577. mov_pcm_be_gt16(track->enc->codec_id) ||
  578. track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  579. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  580. track->enc->codec_id == AV_CODEC_ID_QDM2) {
  581. version = 1;
  582. }
  583. }
  584. avio_wb32(pb, 0); /* size */
  585. avio_wl32(pb, tag); // store it byteswapped
  586. avio_wb32(pb, 0); /* Reserved */
  587. avio_wb16(pb, 0); /* Reserved */
  588. avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
  589. /* SoundDescription */
  590. avio_wb16(pb, version); /* Version */
  591. avio_wb16(pb, 0); /* Revision level */
  592. avio_wb32(pb, 0); /* Reserved */
  593. if (version == 2) {
  594. avio_wb16(pb, 3);
  595. avio_wb16(pb, 16);
  596. avio_wb16(pb, 0xfffe);
  597. avio_wb16(pb, 0);
  598. avio_wb32(pb, 0x00010000);
  599. avio_wb32(pb, 72);
  600. avio_wb64(pb, av_double2int(track->enc->sample_rate));
  601. avio_wb32(pb, track->enc->channels);
  602. avio_wb32(pb, 0x7F000000);
  603. avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
  604. avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
  605. avio_wb32(pb, track->sample_size);
  606. avio_wb32(pb, get_samples_per_packet(track));
  607. } else {
  608. if (track->mode == MODE_MOV) {
  609. avio_wb16(pb, track->enc->channels);
  610. if (track->enc->codec_id == AV_CODEC_ID_PCM_U8 ||
  611. track->enc->codec_id == AV_CODEC_ID_PCM_S8)
  612. avio_wb16(pb, 8); /* bits per sample */
  613. else
  614. avio_wb16(pb, 16);
  615. avio_wb16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
  616. } else { /* reserved for mp4/3gp */
  617. avio_wb16(pb, 2);
  618. avio_wb16(pb, 16);
  619. avio_wb16(pb, 0);
  620. }
  621. avio_wb16(pb, 0); /* packet size (= 0) */
  622. avio_wb16(pb, track->enc->sample_rate <= UINT16_MAX ?
  623. track->enc->sample_rate : 0);
  624. avio_wb16(pb, 0); /* Reserved */
  625. }
  626. if (version == 1) { /* SoundDescription V1 extended info */
  627. if (mov_pcm_le_gt16(track->enc->codec_id) ||
  628. mov_pcm_be_gt16(track->enc->codec_id))
  629. avio_wb32(pb, 1); /* must be 1 for uncompressed formats */
  630. else
  631. avio_wb32(pb, track->enc->frame_size); /* Samples per packet */
  632. avio_wb32(pb, track->sample_size / track->enc->channels); /* Bytes per packet */
  633. avio_wb32(pb, track->sample_size); /* Bytes per frame */
  634. avio_wb32(pb, 2); /* Bytes per sample */
  635. }
  636. if (track->mode == MODE_MOV &&
  637. (track->enc->codec_id == AV_CODEC_ID_AAC ||
  638. track->enc->codec_id == AV_CODEC_ID_AC3 ||
  639. track->enc->codec_id == AV_CODEC_ID_AMR_NB ||
  640. track->enc->codec_id == AV_CODEC_ID_ALAC ||
  641. track->enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  642. track->enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  643. track->enc->codec_id == AV_CODEC_ID_QDM2 ||
  644. (mov_pcm_le_gt16(track->enc->codec_id) && version==1) ||
  645. (mov_pcm_be_gt16(track->enc->codec_id) && version==1)))
  646. mov_write_wave_tag(pb, track);
  647. else if (track->tag == MKTAG('m','p','4','a'))
  648. mov_write_esds_tag(pb, track);
  649. else if (track->enc->codec_id == AV_CODEC_ID_AMR_NB)
  650. mov_write_amr_tag(pb, track);
  651. else if (track->enc->codec_id == AV_CODEC_ID_AC3)
  652. mov_write_ac3_tag(pb, track);
  653. else if (track->enc->codec_id == AV_CODEC_ID_ALAC)
  654. mov_write_extradata_tag(pb, track);
  655. else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO)
  656. mov_write_wfex_tag(pb, track);
  657. else if (track->vos_len > 0)
  658. mov_write_glbl_tag(pb, track);
  659. if (track->mode == MODE_MOV && track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  660. mov_write_chan_tag(pb, track);
  661. return update_size(pb, pos);
  662. }
  663. static int mov_write_d263_tag(AVIOContext *pb)
  664. {
  665. avio_wb32(pb, 0xf); /* size */
  666. ffio_wfourcc(pb, "d263");
  667. ffio_wfourcc(pb, "FFMP");
  668. avio_w8(pb, 0); /* decoder version */
  669. /* FIXME use AVCodecContext level/profile, when encoder will set values */
  670. avio_w8(pb, 0xa); /* level */
  671. avio_w8(pb, 0); /* profile */
  672. return 0xf;
  673. }
  674. static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
  675. {
  676. int64_t pos = avio_tell(pb);
  677. avio_wb32(pb, 0);
  678. ffio_wfourcc(pb, "avcC");
  679. ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
  680. return update_size(pb, pos);
  681. }
  682. /* also used by all avid codecs (dv, imx, meridien) and their variants */
  683. static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
  684. {
  685. int i;
  686. avio_wb32(pb, 24); /* size */
  687. ffio_wfourcc(pb, "ACLR");
  688. ffio_wfourcc(pb, "ACLR");
  689. ffio_wfourcc(pb, "0001");
  690. avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
  691. avio_wb32(pb, 0); /* unknown */
  692. avio_wb32(pb, 24); /* size */
  693. ffio_wfourcc(pb, "APRG");
  694. ffio_wfourcc(pb, "APRG");
  695. ffio_wfourcc(pb, "0001");
  696. avio_wb32(pb, 1); /* unknown */
  697. avio_wb32(pb, 0); /* unknown */
  698. avio_wb32(pb, 120); /* size */
  699. ffio_wfourcc(pb, "ARES");
  700. ffio_wfourcc(pb, "ARES");
  701. ffio_wfourcc(pb, "0001");
  702. avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
  703. avio_wb32(pb, track->enc->width);
  704. /* values below are based on samples created with quicktime and avid codecs */
  705. if (track->vos_data[5] & 2) { // interlaced
  706. avio_wb32(pb, track->enc->height / 2);
  707. avio_wb32(pb, 2); /* unknown */
  708. avio_wb32(pb, 0); /* unknown */
  709. avio_wb32(pb, 4); /* unknown */
  710. } else {
  711. avio_wb32(pb, track->enc->height);
  712. avio_wb32(pb, 1); /* unknown */
  713. avio_wb32(pb, 0); /* unknown */
  714. if (track->enc->height == 1080)
  715. avio_wb32(pb, 5); /* unknown */
  716. else
  717. avio_wb32(pb, 6); /* unknown */
  718. }
  719. /* padding */
  720. for (i = 0; i < 10; i++)
  721. avio_wb64(pb, 0);
  722. /* extra padding for stsd needed */
  723. avio_wb32(pb, 0);
  724. return 0;
  725. }
  726. static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  727. {
  728. int tag = track->enc->codec_tag;
  729. if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
  730. return 0;
  731. if (track->enc->codec_id == AV_CODEC_ID_H264) tag = MKTAG('a','v','c','1');
  732. else if (track->enc->codec_id == AV_CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
  733. else if (track->enc->codec_id == AV_CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
  734. else if (track->enc->codec_id == AV_CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
  735. else if (track->enc->codec_id == AV_CODEC_ID_VC1) tag = MKTAG('v','c','-','1');
  736. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
  737. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
  738. return tag;
  739. }
  740. static const AVCodecTag codec_ipod_tags[] = {
  741. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  742. { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  743. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  744. { AV_CODEC_ID_ALAC, MKTAG('a','l','a','c') },
  745. { AV_CODEC_ID_AC3, MKTAG('a','c','-','3') },
  746. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  747. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
  748. { AV_CODEC_ID_NONE, 0 },
  749. };
  750. static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  751. {
  752. int tag = track->enc->codec_tag;
  753. // keep original tag for subs, ipod supports both formats
  754. if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  755. (tag == MKTAG('t', 'x', '3', 'g') ||
  756. tag == MKTAG('t', 'e', 'x', 't'))))
  757. tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
  758. if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
  759. av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
  760. "Quicktime/Ipod might not play the file\n");
  761. return tag;
  762. }
  763. static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
  764. {
  765. int tag;
  766. if (track->enc->width == 720) { /* SD */
  767. if (track->enc->height == 480) { /* NTSC */
  768. if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
  769. else tag = MKTAG('d','v','c',' ');
  770. }else if (track->enc->pix_fmt == AV_PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
  771. else if (track->enc->pix_fmt == AV_PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
  772. else tag = MKTAG('d','v','p','p');
  773. } else if (track->enc->height == 720) { /* HD 720 line */
  774. if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
  775. else tag = MKTAG('d','v','h','p');
  776. } else if (track->enc->height == 1080) { /* HD 1080 line */
  777. if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
  778. else tag = MKTAG('d','v','h','6');
  779. } else {
  780. av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
  781. return 0;
  782. }
  783. return tag;
  784. }
  785. static const struct {
  786. enum AVPixelFormat pix_fmt;
  787. uint32_t tag;
  788. unsigned bps;
  789. } mov_pix_fmt_tags[] = {
  790. { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','2'), 0 },
  791. { AV_PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
  792. { AV_PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
  793. { AV_PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
  794. { AV_PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
  795. { AV_PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
  796. { AV_PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
  797. { AV_PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
  798. { AV_PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
  799. { AV_PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
  800. { AV_PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
  801. { AV_PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
  802. { AV_PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
  803. { AV_PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
  804. { AV_PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
  805. };
  806. static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
  807. {
  808. int tag = track->enc->codec_tag;
  809. int i;
  810. for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
  811. if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
  812. tag = mov_pix_fmt_tags[i].tag;
  813. track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
  814. if (track->enc->codec_tag == mov_pix_fmt_tags[i].tag)
  815. break;
  816. }
  817. }
  818. return tag;
  819. }
  820. static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  821. {
  822. int tag = track->enc->codec_tag;
  823. if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
  824. (track->enc->codec_id == AV_CODEC_ID_DVVIDEO ||
  825. track->enc->codec_id == AV_CODEC_ID_RAWVIDEO ||
  826. track->enc->codec_id == AV_CODEC_ID_H263 ||
  827. av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
  828. if (track->enc->codec_id == AV_CODEC_ID_DVVIDEO)
  829. tag = mov_get_dv_codec_tag(s, track);
  830. else if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO)
  831. tag = mov_get_rawvideo_codec_tag(s, track);
  832. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  833. tag = ff_codec_get_tag(ff_codec_movvideo_tags, track->enc->codec_id);
  834. if (!tag) { // if no mac fcc found, try with Microsoft tags
  835. tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
  836. if (tag)
  837. av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
  838. "the file may be unplayable!\n");
  839. }
  840. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  841. tag = ff_codec_get_tag(ff_codec_movaudio_tags, track->enc->codec_id);
  842. if (!tag) { // if no mac fcc found, try with Microsoft tags
  843. int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
  844. if (ms_tag) {
  845. tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
  846. av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
  847. "the file may be unplayable!\n");
  848. }
  849. }
  850. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  851. tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
  852. }
  853. return tag;
  854. }
  855. static const AVCodecTag codec_3gp_tags[] = {
  856. { AV_CODEC_ID_H263, MKTAG('s','2','6','3') },
  857. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  858. { AV_CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  859. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  860. { AV_CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
  861. { AV_CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
  862. { AV_CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  863. { AV_CODEC_ID_NONE, 0 },
  864. };
  865. static const AVCodecTag codec_f4v_tags[] = { // XXX: add GIF/PNG/JPEG?
  866. { AV_CODEC_ID_MP3, MKTAG('.','m','p','3') },
  867. { AV_CODEC_ID_AAC, MKTAG('m','p','4','a') },
  868. { AV_CODEC_ID_H264, MKTAG('a','v','c','1') },
  869. { AV_CODEC_ID_VP6F, MKTAG('V','P','6','F') },
  870. { AV_CODEC_ID_NONE, 0 },
  871. };
  872. static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
  873. {
  874. int tag;
  875. if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
  876. tag = mp4_get_codec_tag(s, track);
  877. else if (track->mode == MODE_ISM) {
  878. tag = mp4_get_codec_tag(s, track);
  879. if (!tag && track->enc->codec_id == AV_CODEC_ID_WMAPRO)
  880. tag = MKTAG('w', 'm', 'a', ' ');
  881. } else if (track->mode == MODE_IPOD)
  882. tag = ipod_get_codec_tag(s, track);
  883. else if (track->mode & MODE_3GP)
  884. tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
  885. else if (track->mode & MODE_F4V)
  886. tag = ff_codec_get_tag(codec_f4v_tags, track->enc->codec_id);
  887. else
  888. tag = mov_get_codec_tag(s, track);
  889. return tag;
  890. }
  891. /** Write uuid atom.
  892. * Needed to make file play in iPods running newest firmware
  893. * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
  894. */
  895. static int mov_write_uuid_tag_ipod(AVIOContext *pb)
  896. {
  897. avio_wb32(pb, 28);
  898. ffio_wfourcc(pb, "uuid");
  899. avio_wb32(pb, 0x6b6840f2);
  900. avio_wb32(pb, 0x5f244fc5);
  901. avio_wb32(pb, 0xba39a51b);
  902. avio_wb32(pb, 0xcf0323f3);
  903. avio_wb32(pb, 0x0);
  904. return 28;
  905. }
  906. static const uint16_t fiel_data[] = {
  907. 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
  908. };
  909. static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
  910. {
  911. unsigned mov_field_order = 0;
  912. if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
  913. mov_field_order = fiel_data[track->enc->field_order];
  914. else
  915. return 0;
  916. avio_wb32(pb, 10);
  917. ffio_wfourcc(pb, "fiel");
  918. avio_wb16(pb, mov_field_order);
  919. return 10;
  920. }
  921. static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
  922. {
  923. int64_t pos = avio_tell(pb);
  924. avio_wb32(pb, 0); /* size */
  925. avio_wl32(pb, track->tag); // store it byteswapped
  926. avio_wb32(pb, 0); /* Reserved */
  927. avio_wb16(pb, 0); /* Reserved */
  928. avio_wb16(pb, 1); /* Data-reference index */
  929. if (track->enc->extradata_size)
  930. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  931. return update_size(pb, pos);
  932. }
  933. static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
  934. {
  935. AVRational sar;
  936. av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
  937. track->enc->sample_aspect_ratio.den, INT_MAX);
  938. avio_wb32(pb, 16);
  939. ffio_wfourcc(pb, "pasp");
  940. avio_wb32(pb, sar.num);
  941. avio_wb32(pb, sar.den);
  942. return 16;
  943. }
  944. static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
  945. {
  946. int64_t pos = avio_tell(pb);
  947. char compressor_name[32] = { 0 };
  948. avio_wb32(pb, 0); /* size */
  949. avio_wl32(pb, track->tag); // store it byteswapped
  950. avio_wb32(pb, 0); /* Reserved */
  951. avio_wb16(pb, 0); /* Reserved */
  952. avio_wb16(pb, 1); /* Data-reference index */
  953. avio_wb16(pb, 0); /* Codec stream version */
  954. avio_wb16(pb, 0); /* Codec stream revision (=0) */
  955. if (track->mode == MODE_MOV) {
  956. ffio_wfourcc(pb, "FFMP"); /* Vendor */
  957. if (track->enc->codec_id == AV_CODEC_ID_RAWVIDEO) {
  958. avio_wb32(pb, 0); /* Temporal Quality */
  959. avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
  960. } else {
  961. avio_wb32(pb, 0x200); /* Temporal Quality = normal */
  962. avio_wb32(pb, 0x200); /* Spatial Quality = normal */
  963. }
  964. } else {
  965. avio_wb32(pb, 0); /* Reserved */
  966. avio_wb32(pb, 0); /* Reserved */
  967. avio_wb32(pb, 0); /* Reserved */
  968. }
  969. avio_wb16(pb, track->enc->width); /* Video width */
  970. avio_wb16(pb, track->height); /* Video height */
  971. avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
  972. avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
  973. avio_wb32(pb, 0); /* Data size (= 0) */
  974. avio_wb16(pb, 1); /* Frame count (= 1) */
  975. /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
  976. if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
  977. av_strlcpy(compressor_name, track->enc->codec->name, 32);
  978. avio_w8(pb, strlen(compressor_name));
  979. avio_write(pb, compressor_name, 31);
  980. if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
  981. avio_wb16(pb, track->enc->bits_per_coded_sample);
  982. else
  983. avio_wb16(pb, 0x18); /* Reserved */
  984. avio_wb16(pb, 0xffff); /* Reserved */
  985. if (track->tag == MKTAG('m','p','4','v'))
  986. mov_write_esds_tag(pb, track);
  987. else if (track->enc->codec_id == AV_CODEC_ID_H263)
  988. mov_write_d263_tag(pb);
  989. else if (track->enc->codec_id == AV_CODEC_ID_AVUI ||
  990. track->enc->codec_id == AV_CODEC_ID_SVQ3) {
  991. mov_write_extradata_tag(pb, track);
  992. avio_wb32(pb, 0);
  993. } else if (track->enc->codec_id == AV_CODEC_ID_DNXHD)
  994. mov_write_avid_tag(pb, track);
  995. else if (track->enc->codec_id == AV_CODEC_ID_H264) {
  996. mov_write_avcc_tag(pb, track);
  997. if (track->mode == MODE_IPOD)
  998. mov_write_uuid_tag_ipod(pb);
  999. } else if (track->enc->codec_id == AV_CODEC_ID_VC1 && track->vos_len > 0)
  1000. mov_write_dvc1_tag(pb, track);
  1001. else if (track->vos_len > 0)
  1002. mov_write_glbl_tag(pb, track);
  1003. if (track->enc->codec_id != AV_CODEC_ID_H264 &&
  1004. track->enc->codec_id != AV_CODEC_ID_MPEG4 &&
  1005. track->enc->codec_id != AV_CODEC_ID_DNXHD)
  1006. if (track->enc->field_order != AV_FIELD_UNKNOWN)
  1007. mov_write_fiel_tag(pb, track);
  1008. if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
  1009. track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
  1010. mov_write_pasp_tag(pb, track);
  1011. }
  1012. return update_size(pb, pos);
  1013. }
  1014. static int mov_write_tmcd_tag(AVIOContext *pb, MOVTrack *track)
  1015. {
  1016. int64_t pos = avio_tell(pb);
  1017. int frame_duration = av_rescale(track->timescale, track->enc->time_base.num, track->enc->time_base.den);
  1018. int nb_frames = 1.0/av_q2d(track->enc->time_base) + 0.5;
  1019. if (nb_frames > 255) {
  1020. av_log(NULL, AV_LOG_ERROR, "fps %d is too large\n", nb_frames);
  1021. return AVERROR(EINVAL);
  1022. }
  1023. avio_wb32(pb, 0); /* size */
  1024. ffio_wfourcc(pb, "tmcd"); /* Data format */
  1025. avio_wb32(pb, 0); /* Reserved */
  1026. avio_wb32(pb, 1); /* Data reference index */
  1027. avio_wb32(pb, 0); /* Flags */
  1028. avio_wb32(pb, track->timecode_flags); /* Flags (timecode) */
  1029. avio_wb32(pb, track->timescale); /* Timescale */
  1030. avio_wb32(pb, frame_duration); /* Frame duration */
  1031. avio_w8(pb, nb_frames); /* Number of frames */
  1032. avio_wb24(pb, 0); /* Reserved */
  1033. /* TODO: source reference string */
  1034. return update_size(pb, pos);
  1035. }
  1036. static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
  1037. {
  1038. int64_t pos = avio_tell(pb);
  1039. avio_wb32(pb, 0); /* size */
  1040. ffio_wfourcc(pb, "rtp ");
  1041. avio_wb32(pb, 0); /* Reserved */
  1042. avio_wb16(pb, 0); /* Reserved */
  1043. avio_wb16(pb, 1); /* Data-reference index */
  1044. avio_wb16(pb, 1); /* Hint track version */
  1045. avio_wb16(pb, 1); /* Highest compatible version */
  1046. avio_wb32(pb, track->max_packet_size); /* Max packet size */
  1047. avio_wb32(pb, 12); /* size */
  1048. ffio_wfourcc(pb, "tims");
  1049. avio_wb32(pb, track->timescale);
  1050. return update_size(pb, pos);
  1051. }
  1052. static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
  1053. {
  1054. int64_t pos = avio_tell(pb);
  1055. avio_wb32(pb, 0); /* size */
  1056. ffio_wfourcc(pb, "stsd");
  1057. avio_wb32(pb, 0); /* version & flags */
  1058. avio_wb32(pb, 1); /* entry count */
  1059. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1060. mov_write_video_tag(pb, track);
  1061. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1062. mov_write_audio_tag(pb, track);
  1063. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  1064. mov_write_subtitle_tag(pb, track);
  1065. else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
  1066. mov_write_rtp_tag(pb, track);
  1067. else if (track->enc->codec_tag == MKTAG('t','m','c','d'))
  1068. mov_write_tmcd_tag(pb, track);
  1069. return update_size(pb, pos);
  1070. }
  1071. static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
  1072. {
  1073. MOVStts *ctts_entries;
  1074. uint32_t entries = 0;
  1075. uint32_t atom_size;
  1076. int i;
  1077. ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
  1078. ctts_entries[0].count = 1;
  1079. ctts_entries[0].duration = track->cluster[0].cts;
  1080. for (i = 1; i < track->entry; i++) {
  1081. if (track->cluster[i].cts == ctts_entries[entries].duration) {
  1082. ctts_entries[entries].count++; /* compress */
  1083. } else {
  1084. entries++;
  1085. ctts_entries[entries].duration = track->cluster[i].cts;
  1086. ctts_entries[entries].count = 1;
  1087. }
  1088. }
  1089. entries++; /* last one */
  1090. atom_size = 16 + (entries * 8);
  1091. avio_wb32(pb, atom_size); /* size */
  1092. ffio_wfourcc(pb, "ctts");
  1093. avio_wb32(pb, 0); /* version & flags */
  1094. avio_wb32(pb, entries); /* entry count */
  1095. for (i = 0; i < entries; i++) {
  1096. avio_wb32(pb, ctts_entries[i].count);
  1097. avio_wb32(pb, ctts_entries[i].duration);
  1098. }
  1099. av_free(ctts_entries);
  1100. return atom_size;
  1101. }
  1102. /* Time to sample atom */
  1103. static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
  1104. {
  1105. MOVStts *stts_entries;
  1106. uint32_t entries = -1;
  1107. uint32_t atom_size;
  1108. int i;
  1109. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
  1110. stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
  1111. stts_entries[0].count = track->sample_count;
  1112. stts_entries[0].duration = 1;
  1113. entries = 1;
  1114. } else {
  1115. stts_entries = track->entry ?
  1116. av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
  1117. NULL;
  1118. for (i = 0; i < track->entry; i++) {
  1119. int duration = get_cluster_duration(track, i);
  1120. if (i && duration == stts_entries[entries].duration) {
  1121. stts_entries[entries].count++; /* compress */
  1122. } else {
  1123. entries++;
  1124. stts_entries[entries].duration = duration;
  1125. stts_entries[entries].count = 1;
  1126. }
  1127. }
  1128. entries++; /* last one */
  1129. }
  1130. atom_size = 16 + (entries * 8);
  1131. avio_wb32(pb, atom_size); /* size */
  1132. ffio_wfourcc(pb, "stts");
  1133. avio_wb32(pb, 0); /* version & flags */
  1134. avio_wb32(pb, entries); /* entry count */
  1135. for (i = 0; i < entries; i++) {
  1136. avio_wb32(pb, stts_entries[i].count);
  1137. avio_wb32(pb, stts_entries[i].duration);
  1138. }
  1139. av_free(stts_entries);
  1140. return atom_size;
  1141. }
  1142. static int mov_write_dref_tag(AVIOContext *pb)
  1143. {
  1144. avio_wb32(pb, 28); /* size */
  1145. ffio_wfourcc(pb, "dref");
  1146. avio_wb32(pb, 0); /* version & flags */
  1147. avio_wb32(pb, 1); /* entry count */
  1148. avio_wb32(pb, 0xc); /* size */
  1149. //FIXME add the alis and rsrc atom
  1150. ffio_wfourcc(pb, "url ");
  1151. avio_wb32(pb, 1); /* version & flags */
  1152. return 28;
  1153. }
  1154. static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
  1155. {
  1156. int64_t pos = avio_tell(pb);
  1157. avio_wb32(pb, 0); /* size */
  1158. ffio_wfourcc(pb, "stbl");
  1159. mov_write_stsd_tag(pb, track);
  1160. mov_write_stts_tag(pb, track);
  1161. if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1162. track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
  1163. track->has_keyframes && track->has_keyframes < track->entry)
  1164. mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
  1165. if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
  1166. mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
  1167. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  1168. track->flags & MOV_TRACK_CTTS)
  1169. mov_write_ctts_tag(pb, track);
  1170. mov_write_stsc_tag(pb, track);
  1171. mov_write_stsz_tag(pb, track);
  1172. mov_write_stco_tag(pb, track);
  1173. return update_size(pb, pos);
  1174. }
  1175. static int mov_write_dinf_tag(AVIOContext *pb)
  1176. {
  1177. int64_t pos = avio_tell(pb);
  1178. avio_wb32(pb, 0); /* size */
  1179. ffio_wfourcc(pb, "dinf");
  1180. mov_write_dref_tag(pb);
  1181. return update_size(pb, pos);
  1182. }
  1183. static int mov_write_nmhd_tag(AVIOContext *pb)
  1184. {
  1185. avio_wb32(pb, 12);
  1186. ffio_wfourcc(pb, "nmhd");
  1187. avio_wb32(pb, 0);
  1188. return 12;
  1189. }
  1190. static int mov_write_tcmi_tag(AVIOContext *pb, MOVTrack *track)
  1191. {
  1192. int64_t pos = avio_tell(pb);
  1193. const char *font = "Lucida Grande";
  1194. avio_wb32(pb, 0); /* size */
  1195. ffio_wfourcc(pb, "tcmi"); /* timecode media information atom */
  1196. avio_wb32(pb, 0); /* version & flags */
  1197. avio_wb16(pb, 0); /* text font */
  1198. avio_wb16(pb, 0); /* text face */
  1199. avio_wb16(pb, 12); /* text size */
  1200. avio_wb16(pb, 0); /* (unknown, not in the QT specs...) */
  1201. avio_wb16(pb, 0x0000); /* text color (red) */
  1202. avio_wb16(pb, 0x0000); /* text color (green) */
  1203. avio_wb16(pb, 0x0000); /* text color (blue) */
  1204. avio_wb16(pb, 0xffff); /* background color (red) */
  1205. avio_wb16(pb, 0xffff); /* background color (green) */
  1206. avio_wb16(pb, 0xffff); /* background color (blue) */
  1207. avio_w8(pb, strlen(font)); /* font len (part of the pascal string) */
  1208. avio_write(pb, font, strlen(font)); /* font name */
  1209. return update_size(pb, pos);
  1210. }
  1211. static int mov_write_gmhd_tag(AVIOContext *pb, MOVTrack *track)
  1212. {
  1213. int64_t pos = avio_tell(pb);
  1214. avio_wb32(pb, 0); /* size */
  1215. ffio_wfourcc(pb, "gmhd");
  1216. avio_wb32(pb, 0x18); /* gmin size */
  1217. ffio_wfourcc(pb, "gmin");/* generic media info */
  1218. avio_wb32(pb, 0); /* version & flags */
  1219. avio_wb16(pb, 0x40); /* graphics mode = */
  1220. avio_wb16(pb, 0x8000); /* opColor (r?) */
  1221. avio_wb16(pb, 0x8000); /* opColor (g?) */
  1222. avio_wb16(pb, 0x8000); /* opColor (b?) */
  1223. avio_wb16(pb, 0); /* balance */
  1224. avio_wb16(pb, 0); /* reserved */
  1225. /*
  1226. * This special text atom is required for
  1227. * Apple Quicktime chapters. The contents
  1228. * don't appear to be documented, so the
  1229. * bytes are copied verbatim.
  1230. */
  1231. if (track->tag != MKTAG('c','6','0','8')) {
  1232. avio_wb32(pb, 0x2C); /* size */
  1233. ffio_wfourcc(pb, "text");
  1234. avio_wb16(pb, 0x01);
  1235. avio_wb32(pb, 0x00);
  1236. avio_wb32(pb, 0x00);
  1237. avio_wb32(pb, 0x00);
  1238. avio_wb32(pb, 0x01);
  1239. avio_wb32(pb, 0x00);
  1240. avio_wb32(pb, 0x00);
  1241. avio_wb32(pb, 0x00);
  1242. avio_wb32(pb, 0x00004000);
  1243. avio_wb16(pb, 0x0000);
  1244. }
  1245. if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
  1246. int64_t tmcd_pos = avio_tell(pb);
  1247. avio_wb32(pb, 0); /* size */
  1248. ffio_wfourcc(pb, "tmcd");
  1249. mov_write_tcmi_tag(pb, track);
  1250. update_size(pb, tmcd_pos);
  1251. }
  1252. return update_size(pb, pos);
  1253. }
  1254. static int mov_write_smhd_tag(AVIOContext *pb)
  1255. {
  1256. avio_wb32(pb, 16); /* size */
  1257. ffio_wfourcc(pb, "smhd");
  1258. avio_wb32(pb, 0); /* version & flags */
  1259. avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
  1260. avio_wb16(pb, 0); /* reserved */
  1261. return 16;
  1262. }
  1263. static int mov_write_vmhd_tag(AVIOContext *pb)
  1264. {
  1265. avio_wb32(pb, 0x14); /* size (always 0x14) */
  1266. ffio_wfourcc(pb, "vmhd");
  1267. avio_wb32(pb, 0x01); /* version & flags */
  1268. avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
  1269. return 0x14;
  1270. }
  1271. static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
  1272. {
  1273. const char *hdlr, *descr = NULL, *hdlr_type = NULL;
  1274. int64_t pos = avio_tell(pb);
  1275. if (!track) { /* no media --> data handler */
  1276. hdlr = "dhlr";
  1277. hdlr_type = "url ";
  1278. descr = "DataHandler";
  1279. } else {
  1280. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  1281. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1282. hdlr_type = "vide";
  1283. descr = "VideoHandler";
  1284. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  1285. hdlr_type = "soun";
  1286. descr = "SoundHandler";
  1287. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1288. if (track->tag == MKTAG('c','6','0','8')) {
  1289. hdlr_type = "clcp";
  1290. descr = "ClosedCaptionHandler";
  1291. } else {
  1292. if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
  1293. else hdlr_type = "text";
  1294. descr = "SubtitleHandler";
  1295. }
  1296. } else if (track->enc->codec_tag == MKTAG('t','m','c','d')) {
  1297. hdlr_type = "tmcd";
  1298. descr = "TimeCodeHandler";
  1299. } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
  1300. hdlr_type = "hint";
  1301. descr = "HintHandler";
  1302. } else {
  1303. hdlr = "dhlr";
  1304. hdlr_type = "url ";
  1305. descr = "DataHandler";
  1306. }
  1307. }
  1308. avio_wb32(pb, 0); /* size */
  1309. ffio_wfourcc(pb, "hdlr");
  1310. avio_wb32(pb, 0); /* Version & flags */
  1311. avio_write(pb, hdlr, 4); /* handler */
  1312. ffio_wfourcc(pb, hdlr_type); /* handler type */
  1313. avio_wb32(pb, 0); /* reserved */
  1314. avio_wb32(pb, 0); /* reserved */
  1315. avio_wb32(pb, 0); /* reserved */
  1316. if (!track || track->mode == MODE_MOV)
  1317. avio_w8(pb, strlen(descr)); /* pascal string */
  1318. avio_write(pb, descr, strlen(descr)); /* handler description */
  1319. if (track && track->mode != MODE_MOV)
  1320. avio_w8(pb, 0); /* c string */
  1321. return update_size(pb, pos);
  1322. }
  1323. static int mov_write_hmhd_tag(AVIOContext *pb)
  1324. {
  1325. /* This atom must be present, but leaving the values at zero
  1326. * seems harmless. */
  1327. avio_wb32(pb, 28); /* size */
  1328. ffio_wfourcc(pb, "hmhd");
  1329. avio_wb32(pb, 0); /* version, flags */
  1330. avio_wb16(pb, 0); /* maxPDUsize */
  1331. avio_wb16(pb, 0); /* avgPDUsize */
  1332. avio_wb32(pb, 0); /* maxbitrate */
  1333. avio_wb32(pb, 0); /* avgbitrate */
  1334. avio_wb32(pb, 0); /* reserved */
  1335. return 28;
  1336. }
  1337. static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
  1338. {
  1339. int64_t pos = avio_tell(pb);
  1340. avio_wb32(pb, 0); /* size */
  1341. ffio_wfourcc(pb, "minf");
  1342. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1343. mov_write_vmhd_tag(pb);
  1344. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1345. mov_write_smhd_tag(pb);
  1346. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1347. if (track->tag == MKTAG('t','e','x','t') || track->tag == MKTAG('c','6','0','8')) {
  1348. mov_write_gmhd_tag(pb, track);
  1349. } else {
  1350. mov_write_nmhd_tag(pb);
  1351. }
  1352. } else if (track->tag == MKTAG('t','m','c','d')) {
  1353. mov_write_gmhd_tag(pb, track);
  1354. } else if (track->tag == MKTAG('r','t','p',' ')) {
  1355. mov_write_hmhd_tag(pb);
  1356. }
  1357. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  1358. mov_write_hdlr_tag(pb, NULL);
  1359. mov_write_dinf_tag(pb);
  1360. mov_write_stbl_tag(pb, track);
  1361. return update_size(pb, pos);
  1362. }
  1363. static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
  1364. {
  1365. int version = track->track_duration < INT32_MAX ? 0 : 1;
  1366. if (track->mode == MODE_ISM)
  1367. version = 1;
  1368. (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
  1369. ffio_wfourcc(pb, "mdhd");
  1370. avio_w8(pb, version);
  1371. avio_wb24(pb, 0); /* flags */
  1372. if (version == 1) {
  1373. avio_wb64(pb, track->time);
  1374. avio_wb64(pb, track->time);
  1375. } else {
  1376. avio_wb32(pb, track->time); /* creation time */
  1377. avio_wb32(pb, track->time); /* modification time */
  1378. }
  1379. avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
  1380. if (!track->entry)
  1381. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  1382. else
  1383. (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
  1384. avio_wb16(pb, track->language); /* language */
  1385. avio_wb16(pb, 0); /* reserved (quality) */
  1386. if (version != 0 && track->mode == MODE_MOV) {
  1387. av_log(NULL, AV_LOG_ERROR,
  1388. "FATAL error, file duration too long for timebase, this file will not be\n"
  1389. "playable with quicktime. Choose a different timebase or a different\n"
  1390. "container format\n");
  1391. }
  1392. return 32;
  1393. }
  1394. static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
  1395. {
  1396. int64_t pos = avio_tell(pb);
  1397. avio_wb32(pb, 0); /* size */
  1398. ffio_wfourcc(pb, "mdia");
  1399. mov_write_mdhd_tag(pb, track);
  1400. mov_write_hdlr_tag(pb, track);
  1401. mov_write_minf_tag(pb, track);
  1402. return update_size(pb, pos);
  1403. }
  1404. /* transformation matrix
  1405. |a b u|
  1406. |c d v|
  1407. |tx ty w| */
  1408. static void write_matrix(AVIOContext *pb, int16_t a, int16_t b, int16_t c,
  1409. int16_t d, int16_t tx, int16_t ty)
  1410. {
  1411. avio_wb32(pb, a << 16); /* 16.16 format */
  1412. avio_wb32(pb, b << 16); /* 16.16 format */
  1413. avio_wb32(pb, 0); /* u in 2.30 format */
  1414. avio_wb32(pb, c << 16); /* 16.16 format */
  1415. avio_wb32(pb, d << 16); /* 16.16 format */
  1416. avio_wb32(pb, 0); /* v in 2.30 format */
  1417. avio_wb32(pb, tx << 16); /* 16.16 format */
  1418. avio_wb32(pb, ty << 16); /* 16.16 format */
  1419. avio_wb32(pb, 1 << 30); /* w in 2.30 format */
  1420. }
  1421. static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
  1422. {
  1423. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1424. track->timescale, AV_ROUND_UP);
  1425. int version = duration < INT32_MAX ? 0 : 1;
  1426. int rotation = 0;
  1427. if (track->mode == MODE_ISM)
  1428. version = 1;
  1429. (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
  1430. ffio_wfourcc(pb, "tkhd");
  1431. avio_w8(pb, version);
  1432. avio_wb24(pb, track->secondary ? 0x2 : 0xf); /* flags (first track enabled) */
  1433. if (version == 1) {
  1434. avio_wb64(pb, track->time);
  1435. avio_wb64(pb, track->time);
  1436. } else {
  1437. avio_wb32(pb, track->time); /* creation time */
  1438. avio_wb32(pb, track->time); /* modification time */
  1439. }
  1440. avio_wb32(pb, track->track_id); /* track-id */
  1441. avio_wb32(pb, 0); /* reserved */
  1442. if (!track->entry)
  1443. (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);
  1444. else
  1445. (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
  1446. avio_wb32(pb, 0); /* reserved */
  1447. avio_wb32(pb, 0); /* reserved */
  1448. avio_wb16(pb, 0); /* layer */
  1449. avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
  1450. /* Volume, only for audio */
  1451. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1452. avio_wb16(pb, 0x0100);
  1453. else
  1454. avio_wb16(pb, 0);
  1455. avio_wb16(pb, 0); /* reserved */
  1456. /* Matrix structure */
  1457. if (st && st->metadata) {
  1458. AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);
  1459. rotation = (rot && rot->value) ? atoi(rot->value) : 0;
  1460. }
  1461. if (rotation == 90) {
  1462. write_matrix(pb, 0, 1, -1, 0, track->enc->height, 0);
  1463. } else if (rotation == 180) {
  1464. write_matrix(pb, -1, 0, 0, -1, track->enc->width, track->enc->height);
  1465. } else if (rotation == 270) {
  1466. write_matrix(pb, 0, -1, 1, 0, 0, track->enc->width);
  1467. } else {
  1468. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  1469. }
  1470. /* Track width and height, for visual only */
  1471. if (st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1472. track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
  1473. if (track->mode == MODE_MOV) {
  1474. avio_wb32(pb, track->enc->width << 16);
  1475. avio_wb32(pb, track->height << 16);
  1476. } else {
  1477. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1478. if (!sample_aspect_ratio || track->height != track->enc->height)
  1479. sample_aspect_ratio = 1;
  1480. avio_wb32(pb, sample_aspect_ratio * track->enc->width * 0x10000);
  1481. avio_wb32(pb, track->height * 0x10000);
  1482. }
  1483. } else {
  1484. avio_wb32(pb, 0);
  1485. avio_wb32(pb, 0);
  1486. }
  1487. return 0x5c;
  1488. }
  1489. static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
  1490. {
  1491. int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
  1492. track->enc->sample_aspect_ratio.den);
  1493. int64_t pos = avio_tell(pb);
  1494. avio_wb32(pb, 0); /* size */
  1495. ffio_wfourcc(pb, "tapt");
  1496. avio_wb32(pb, 20);
  1497. ffio_wfourcc(pb, "clef");
  1498. avio_wb32(pb, 0);
  1499. avio_wb32(pb, width << 16);
  1500. avio_wb32(pb, track->enc->height << 16);
  1501. avio_wb32(pb, 20);
  1502. ffio_wfourcc(pb, "prof");
  1503. avio_wb32(pb, 0);
  1504. avio_wb32(pb, width << 16);
  1505. avio_wb32(pb, track->enc->height << 16);
  1506. avio_wb32(pb, 20);
  1507. ffio_wfourcc(pb, "enof");
  1508. avio_wb32(pb, 0);
  1509. avio_wb32(pb, track->enc->width << 16);
  1510. avio_wb32(pb, track->enc->height << 16);
  1511. return update_size(pb, pos);
  1512. }
  1513. // This box seems important for the psp playback ... without it the movie seems to hang
  1514. static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
  1515. {
  1516. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1517. track->timescale, AV_ROUND_UP);
  1518. int version = duration < INT32_MAX ? 0 : 1;
  1519. int entry_size, entry_count, size;
  1520. int64_t delay, start_ct = track->cluster[0].cts;
  1521. delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
  1522. track->timescale, AV_ROUND_DOWN);
  1523. version |= delay < INT32_MAX ? 0 : 1;
  1524. entry_size = (version == 1) ? 20 : 12;
  1525. entry_count = 1 + (delay > 0);
  1526. size = 24 + entry_count * entry_size;
  1527. /* write the atom data */
  1528. avio_wb32(pb, size);
  1529. ffio_wfourcc(pb, "edts");
  1530. avio_wb32(pb, size - 8);
  1531. ffio_wfourcc(pb, "elst");
  1532. avio_w8(pb, version);
  1533. avio_wb24(pb, 0); /* flags */
  1534. avio_wb32(pb, entry_count);
  1535. if (delay > 0) { /* add an empty edit to delay presentation */
  1536. if (version == 1) {
  1537. avio_wb64(pb, delay);
  1538. avio_wb64(pb, -1);
  1539. } else {
  1540. avio_wb32(pb, delay);
  1541. avio_wb32(pb, -1);
  1542. }
  1543. avio_wb32(pb, 0x00010000);
  1544. } else {
  1545. av_assert0(av_rescale_rnd(track->cluster[0].dts, MOV_TIMESCALE, track->timescale, AV_ROUND_DOWN) <= 0);
  1546. start_ct = -FFMIN(track->cluster[0].dts, 0); //FFMIN needed due to rounding
  1547. duration += delay;
  1548. }
  1549. /* duration */
  1550. if (version == 1) {
  1551. avio_wb64(pb, duration);
  1552. avio_wb64(pb, start_ct);
  1553. } else {
  1554. avio_wb32(pb, duration);
  1555. avio_wb32(pb, start_ct);
  1556. }
  1557. avio_wb32(pb, 0x00010000);
  1558. return size;
  1559. }
  1560. static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
  1561. {
  1562. avio_wb32(pb, 20); // size
  1563. ffio_wfourcc(pb, "tref");
  1564. avio_wb32(pb, 12); // size (subatom)
  1565. avio_wl32(pb, track->tref_tag);
  1566. avio_wb32(pb, track->tref_id);
  1567. return 20;
  1568. }
  1569. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  1570. static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
  1571. {
  1572. avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  1573. ffio_wfourcc(pb, "uuid");
  1574. ffio_wfourcc(pb, "USMT");
  1575. avio_wb32(pb, 0x21d24fce);
  1576. avio_wb32(pb, 0xbb88695c);
  1577. avio_wb32(pb, 0xfac9c740);
  1578. avio_wb32(pb, 0x1c); // another size here!
  1579. ffio_wfourcc(pb, "MTDT");
  1580. avio_wb32(pb, 0x00010012);
  1581. avio_wb32(pb, 0x0a);
  1582. avio_wb32(pb, 0x55c40000);
  1583. avio_wb32(pb, 0x1);
  1584. avio_wb32(pb, 0x0);
  1585. return 0x34;
  1586. }
  1587. static int mov_write_udta_sdp(AVIOContext *pb, MOVTrack *track)
  1588. {
  1589. AVFormatContext *ctx = track->rtp_ctx;
  1590. char buf[1000] = "";
  1591. int len;
  1592. ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0], track->src_track,
  1593. NULL, NULL, 0, 0, ctx);
  1594. av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", track->track_id);
  1595. len = strlen(buf);
  1596. avio_wb32(pb, len + 24);
  1597. ffio_wfourcc(pb, "udta");
  1598. avio_wb32(pb, len + 16);
  1599. ffio_wfourcc(pb, "hnti");
  1600. avio_wb32(pb, len + 8);
  1601. ffio_wfourcc(pb, "sdp ");
  1602. avio_write(pb, buf, len);
  1603. return len + 24;
  1604. }
  1605. static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
  1606. MOVTrack *track, AVStream *st)
  1607. {
  1608. int64_t pos = avio_tell(pb);
  1609. avio_wb32(pb, 0); /* size */
  1610. ffio_wfourcc(pb, "trak");
  1611. mov_write_tkhd_tag(pb, track, st);
  1612. if (supports_edts(mov))
  1613. mov_write_edts_tag(pb, track); // PSP Movies and several other cases require edts box
  1614. if (track->tref_tag)
  1615. mov_write_tref_tag(pb, track);
  1616. mov_write_mdia_tag(pb, track);
  1617. if (track->mode == MODE_PSP)
  1618. mov_write_uuid_tag_psp(pb, track); // PSP Movies require this uuid box
  1619. if (track->tag == MKTAG('r','t','p',' '))
  1620. mov_write_udta_sdp(pb, track);
  1621. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
  1622. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1623. if (st->sample_aspect_ratio.num && 1.0 != sample_aspect_ratio)
  1624. mov_write_tapt_tag(pb, track);
  1625. }
  1626. return update_size(pb, pos);
  1627. }
  1628. static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
  1629. {
  1630. int i, has_audio = 0, has_video = 0;
  1631. int64_t pos = avio_tell(pb);
  1632. int audio_profile = mov->iods_audio_profile;
  1633. int video_profile = mov->iods_video_profile;
  1634. for (i = 0; i < mov->nb_streams; i++) {
  1635. if (mov->tracks[i].entry > 0) {
  1636. has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
  1637. has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
  1638. }
  1639. }
  1640. if (audio_profile < 0)
  1641. audio_profile = 0xFF - has_audio;
  1642. if (video_profile < 0)
  1643. video_profile = 0xFF - has_video;
  1644. avio_wb32(pb, 0x0); /* size */
  1645. ffio_wfourcc(pb, "iods");
  1646. avio_wb32(pb, 0); /* version & flags */
  1647. put_descr(pb, 0x10, 7);
  1648. avio_wb16(pb, 0x004f);
  1649. avio_w8(pb, 0xff);
  1650. avio_w8(pb, 0xff);
  1651. avio_w8(pb, audio_profile);
  1652. avio_w8(pb, video_profile);
  1653. avio_w8(pb, 0xff);
  1654. return update_size(pb, pos);
  1655. }
  1656. static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
  1657. {
  1658. avio_wb32(pb, 0x20); /* size */
  1659. ffio_wfourcc(pb, "trex");
  1660. avio_wb32(pb, 0); /* version & flags */
  1661. avio_wb32(pb, track->track_id); /* track ID */
  1662. avio_wb32(pb, 1); /* default sample description index */
  1663. avio_wb32(pb, 0); /* default sample duration */
  1664. avio_wb32(pb, 0); /* default sample size */
  1665. avio_wb32(pb, 0); /* default sample flags */
  1666. return 0;
  1667. }
  1668. static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
  1669. {
  1670. int64_t pos = avio_tell(pb);
  1671. int i;
  1672. avio_wb32(pb, 0x0); /* size */
  1673. ffio_wfourcc(pb, "mvex");
  1674. for (i = 0; i < mov->nb_streams; i++)
  1675. mov_write_trex_tag(pb, &mov->tracks[i]);
  1676. return update_size(pb, pos);
  1677. }
  1678. static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  1679. {
  1680. int max_track_id = 1, i;
  1681. int64_t max_track_len_temp, max_track_len = 0;
  1682. int version;
  1683. for (i = 0; i < mov->nb_streams; i++) {
  1684. if (mov->tracks[i].entry > 0) {
  1685. max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
  1686. MOV_TIMESCALE,
  1687. mov->tracks[i].timescale,
  1688. AV_ROUND_UP);
  1689. if (max_track_len < max_track_len_temp)
  1690. max_track_len = max_track_len_temp;
  1691. if (max_track_id < mov->tracks[i].track_id)
  1692. max_track_id = mov->tracks[i].track_id;
  1693. }
  1694. }
  1695. version = max_track_len < UINT32_MAX ? 0 : 1;
  1696. (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
  1697. ffio_wfourcc(pb, "mvhd");
  1698. avio_w8(pb, version);
  1699. avio_wb24(pb, 0); /* flags */
  1700. if (version == 1) {
  1701. avio_wb64(pb, mov->time);
  1702. avio_wb64(pb, mov->time);
  1703. } else {
  1704. avio_wb32(pb, mov->time); /* creation time */
  1705. avio_wb32(pb, mov->time); /* modification time */
  1706. }
  1707. avio_wb32(pb, MOV_TIMESCALE);
  1708. (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
  1709. avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  1710. avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  1711. avio_wb16(pb, 0); /* reserved */
  1712. avio_wb32(pb, 0); /* reserved */
  1713. avio_wb32(pb, 0); /* reserved */
  1714. /* Matrix structure */
  1715. write_matrix(pb, 1, 0, 0, 1, 0, 0);
  1716. avio_wb32(pb, 0); /* reserved (preview time) */
  1717. avio_wb32(pb, 0); /* reserved (preview duration) */
  1718. avio_wb32(pb, 0); /* reserved (poster time) */
  1719. avio_wb32(pb, 0); /* reserved (selection time) */
  1720. avio_wb32(pb, 0); /* reserved (selection duration) */
  1721. avio_wb32(pb, 0); /* reserved (current time) */
  1722. avio_wb32(pb, max_track_id + 1); /* Next track id */
  1723. return 0x6c;
  1724. }
  1725. static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
  1726. AVFormatContext *s)
  1727. {
  1728. avio_wb32(pb, 33); /* size */
  1729. ffio_wfourcc(pb, "hdlr");
  1730. avio_wb32(pb, 0);
  1731. avio_wb32(pb, 0);
  1732. ffio_wfourcc(pb, "mdir");
  1733. ffio_wfourcc(pb, "appl");
  1734. avio_wb32(pb, 0);
  1735. avio_wb32(pb, 0);
  1736. avio_w8(pb, 0);
  1737. return 33;
  1738. }
  1739. /* helper function to write a data tag with the specified string as data */
  1740. static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
  1741. {
  1742. if (long_style) {
  1743. int size = 16 + strlen(data);
  1744. avio_wb32(pb, size); /* size */
  1745. ffio_wfourcc(pb, "data");
  1746. avio_wb32(pb, 1);
  1747. avio_wb32(pb, 0);
  1748. avio_write(pb, data, strlen(data));
  1749. return size;
  1750. } else {
  1751. if (!lang)
  1752. lang = ff_mov_iso639_to_lang("und", 1);
  1753. avio_wb16(pb, strlen(data)); /* string length */
  1754. avio_wb16(pb, lang);
  1755. avio_write(pb, data, strlen(data));
  1756. return strlen(data) + 4;
  1757. }
  1758. }
  1759. static int mov_write_string_tag(AVIOContext *pb, const char *name,
  1760. const char *value, int lang, int long_style)
  1761. {
  1762. int size = 0;
  1763. if (value && value[0]) {
  1764. int64_t pos = avio_tell(pb);
  1765. avio_wb32(pb, 0); /* size */
  1766. ffio_wfourcc(pb, name);
  1767. mov_write_string_data_tag(pb, value, lang, long_style);
  1768. size = update_size(pb, pos);
  1769. }
  1770. return size;
  1771. }
  1772. static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
  1773. const char *name, const char *tag,
  1774. int long_style)
  1775. {
  1776. int l, lang = 0, len, len2;
  1777. AVDictionaryEntry *t, *t2 = NULL;
  1778. char tag2[16];
  1779. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  1780. return 0;
  1781. len = strlen(t->key);
  1782. snprintf(tag2, sizeof(tag2), "%s-", tag);
  1783. while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
  1784. len2 = strlen(t2->key);
  1785. if (len2 == len + 4 && !strcmp(t->value, t2->value)
  1786. && (l = ff_mov_iso639_to_lang(&t2->key[len2 - 3], 1)) >= 0) {
  1787. lang = l;
  1788. break;
  1789. }
  1790. }
  1791. return mov_write_string_tag(pb, name, t->value, lang, long_style);
  1792. }
  1793. /* iTunes bpm number */
  1794. static int mov_write_tmpo_tag(AVIOContext *pb, AVFormatContext *s)
  1795. {
  1796. AVDictionaryEntry *t = av_dict_get(s->metadata, "tmpo", NULL, 0);
  1797. int size = 0, tmpo = t ? atoi(t->value) : 0;
  1798. if (tmpo) {
  1799. size = 26;
  1800. avio_wb32(pb, size);
  1801. ffio_wfourcc(pb, "tmpo");
  1802. avio_wb32(pb, size-8); /* size */
  1803. ffio_wfourcc(pb, "data");
  1804. avio_wb32(pb, 0x15); //type specifier
  1805. avio_wb32(pb, 0);
  1806. avio_wb16(pb, tmpo); // data
  1807. }
  1808. return size;
  1809. }
  1810. /* iTunes track or disc number */
  1811. static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
  1812. AVFormatContext *s, int disc)
  1813. {
  1814. AVDictionaryEntry *t = av_dict_get(s->metadata,
  1815. disc ? "disc" : "track",
  1816. NULL, 0);
  1817. int size = 0, track = t ? atoi(t->value) : 0;
  1818. if (track) {
  1819. int tracks = 0;
  1820. char *slash = strchr(t->value, '/');
  1821. if (slash)
  1822. tracks = atoi(slash + 1);
  1823. avio_wb32(pb, 32); /* size */
  1824. ffio_wfourcc(pb, disc ? "disk" : "trkn");
  1825. avio_wb32(pb, 24); /* size */
  1826. ffio_wfourcc(pb, "data");
  1827. avio_wb32(pb, 0); // 8 bytes empty
  1828. avio_wb32(pb, 0);
  1829. avio_wb16(pb, 0); // empty
  1830. avio_wb16(pb, track); // track / disc number
  1831. avio_wb16(pb, tracks); // total track / disc number
  1832. avio_wb16(pb, 0); // empty
  1833. size = 32;
  1834. }
  1835. return size;
  1836. }
  1837. static int mov_write_int8_metadata(AVFormatContext *s, AVIOContext *pb,
  1838. const char *name, const char *tag,
  1839. int len)
  1840. {
  1841. AVDictionaryEntry *t = NULL;
  1842. uint8_t num;
  1843. int size = 24 + len;
  1844. if (len != 1 && len != 4)
  1845. return -1;
  1846. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  1847. return 0;
  1848. num = atoi(t->value);
  1849. avio_wb32(pb, size);
  1850. ffio_wfourcc(pb, name);
  1851. avio_wb32(pb, size - 8);
  1852. ffio_wfourcc(pb, "data");
  1853. avio_wb32(pb, 0x15);
  1854. avio_wb32(pb, 0);
  1855. if (len==4) avio_wb32(pb, num);
  1856. else avio_w8 (pb, num);
  1857. return size;
  1858. }
  1859. /* iTunes meta data list */
  1860. static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
  1861. AVFormatContext *s)
  1862. {
  1863. int64_t pos = avio_tell(pb);
  1864. avio_wb32(pb, 0); /* size */
  1865. ffio_wfourcc(pb, "ilst");
  1866. mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
  1867. mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
  1868. mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
  1869. mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
  1870. mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
  1871. mov_write_string_metadata(s, pb, "\251day", "date" , 1);
  1872. mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
  1873. mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
  1874. mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
  1875. mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
  1876. mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
  1877. mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
  1878. mov_write_string_metadata(s, pb, "desc", "description",1);
  1879. mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
  1880. mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
  1881. mov_write_string_metadata(s, pb, "tven", "episode_id",1);
  1882. mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
  1883. mov_write_int8_metadata (s, pb, "tves", "episode_sort",4);
  1884. mov_write_int8_metadata (s, pb, "tvsn", "season_number",4);
  1885. mov_write_int8_metadata (s, pb, "stik", "media_type",1);
  1886. mov_write_int8_metadata (s, pb, "hdvd", "hd_video", 1);
  1887. mov_write_int8_metadata (s, pb, "pgap", "gapless_playback",1);
  1888. mov_write_trkn_tag(pb, mov, s, 0); // track number
  1889. mov_write_trkn_tag(pb, mov, s, 1); // disc number
  1890. mov_write_tmpo_tag(pb, s);
  1891. return update_size(pb, pos);
  1892. }
  1893. /* iTunes meta data tag */
  1894. static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
  1895. AVFormatContext *s)
  1896. {
  1897. int size = 0;
  1898. int64_t pos = avio_tell(pb);
  1899. avio_wb32(pb, 0); /* size */
  1900. ffio_wfourcc(pb, "meta");
  1901. avio_wb32(pb, 0);
  1902. mov_write_itunes_hdlr_tag(pb, mov, s);
  1903. mov_write_ilst_tag(pb, mov, s);
  1904. size = update_size(pb, pos);
  1905. return size;
  1906. }
  1907. static int utf8len(const uint8_t *b)
  1908. {
  1909. int len = 0;
  1910. int val;
  1911. while (*b) {
  1912. GET_UTF8(val, *b++, return -1;)
  1913. len++;
  1914. }
  1915. return len;
  1916. }
  1917. static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
  1918. {
  1919. int val;
  1920. while (*b) {
  1921. GET_UTF8(val, *b++, return -1;)
  1922. avio_wb16(pb, val);
  1923. }
  1924. avio_wb16(pb, 0x00);
  1925. return 0;
  1926. }
  1927. static uint16_t language_code(const char *str)
  1928. {
  1929. return (((str[0] - 0x60) & 0x1F) << 10) +
  1930. (((str[1] - 0x60) & 0x1F) << 5) +
  1931. (( str[2] - 0x60) & 0x1F);
  1932. }
  1933. static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
  1934. const char *tag, const char *str)
  1935. {
  1936. int64_t pos = avio_tell(pb);
  1937. AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
  1938. if (!t || !utf8len(t->value))
  1939. return 0;
  1940. avio_wb32(pb, 0); /* size */
  1941. ffio_wfourcc(pb, tag); /* type */
  1942. avio_wb32(pb, 0); /* version + flags */
  1943. if (!strcmp(tag, "yrrc"))
  1944. avio_wb16(pb, atoi(t->value));
  1945. else {
  1946. avio_wb16(pb, language_code("eng")); /* language */
  1947. avio_write(pb, t->value, strlen(t->value) + 1); /* UTF8 string value */
  1948. if (!strcmp(tag, "albm") &&
  1949. (t = av_dict_get(s->metadata, "track", NULL, 0)))
  1950. avio_w8(pb, atoi(t->value));
  1951. }
  1952. return update_size(pb, pos);
  1953. }
  1954. static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
  1955. {
  1956. int64_t pos = avio_tell(pb);
  1957. int i, nb_chapters = FFMIN(s->nb_chapters, 255);
  1958. avio_wb32(pb, 0); // size
  1959. ffio_wfourcc(pb, "chpl");
  1960. avio_wb32(pb, 0x01000000); // version + flags
  1961. avio_wb32(pb, 0); // unknown
  1962. avio_w8(pb, nb_chapters);
  1963. for (i = 0; i < nb_chapters; i++) {
  1964. AVChapter *c = s->chapters[i];
  1965. AVDictionaryEntry *t;
  1966. avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
  1967. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  1968. int len = FFMIN(strlen(t->value), 255);
  1969. avio_w8(pb, len);
  1970. avio_write(pb, t->value, len);
  1971. } else
  1972. avio_w8(pb, 0);
  1973. }
  1974. return update_size(pb, pos);
  1975. }
  1976. static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
  1977. AVFormatContext *s)
  1978. {
  1979. AVIOContext *pb_buf;
  1980. int i, ret, size;
  1981. uint8_t *buf;
  1982. for (i = 0; i < s->nb_streams; i++)
  1983. if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
  1984. return 0;
  1985. }
  1986. ret = avio_open_dyn_buf(&pb_buf);
  1987. if (ret < 0)
  1988. return ret;
  1989. if (mov->mode & MODE_3GP) {
  1990. mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
  1991. mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
  1992. mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
  1993. mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
  1994. mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
  1995. mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
  1996. mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
  1997. mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
  1998. } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
  1999. mov_write_string_metadata(s, pb_buf, "\251ART", "artist", 0);
  2000. mov_write_string_metadata(s, pb_buf, "\251nam", "title", 0);
  2001. mov_write_string_metadata(s, pb_buf, "\251aut", "author", 0);
  2002. mov_write_string_metadata(s, pb_buf, "\251alb", "album", 0);
  2003. mov_write_string_metadata(s, pb_buf, "\251day", "date", 0);
  2004. mov_write_string_metadata(s, pb_buf, "\251swr", "encoder", 0);
  2005. // currently ignored by mov.c
  2006. mov_write_string_metadata(s, pb_buf, "\251des", "comment", 0);
  2007. // add support for libquicktime, this atom is also actually read by mov.c
  2008. mov_write_string_metadata(s, pb_buf, "\251cmt", "comment", 0);
  2009. mov_write_string_metadata(s, pb_buf, "\251gen", "genre", 0);
  2010. mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright", 0);
  2011. } else {
  2012. /* iTunes meta data */
  2013. mov_write_meta_tag(pb_buf, mov, s);
  2014. }
  2015. if (s->nb_chapters)
  2016. mov_write_chpl_tag(pb_buf, s);
  2017. if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
  2018. avio_wb32(pb, size + 8);
  2019. ffio_wfourcc(pb, "udta");
  2020. avio_write(pb, buf, size);
  2021. }
  2022. av_free(buf);
  2023. return 0;
  2024. }
  2025. static void mov_write_psp_udta_tag(AVIOContext *pb,
  2026. const char *str, const char *lang, int type)
  2027. {
  2028. int len = utf8len(str) + 1;
  2029. if (len <= 0)
  2030. return;
  2031. avio_wb16(pb, len * 2 + 10); /* size */
  2032. avio_wb32(pb, type); /* type */
  2033. avio_wb16(pb, language_code(lang)); /* language */
  2034. avio_wb16(pb, 0x01); /* ? */
  2035. ascii_to_wc(pb, str);
  2036. }
  2037. static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
  2038. {
  2039. AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
  2040. int64_t pos, pos2;
  2041. if (title) {
  2042. pos = avio_tell(pb);
  2043. avio_wb32(pb, 0); /* size placeholder*/
  2044. ffio_wfourcc(pb, "uuid");
  2045. ffio_wfourcc(pb, "USMT");
  2046. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2047. avio_wb32(pb, 0xbb88695c);
  2048. avio_wb32(pb, 0xfac9c740);
  2049. pos2 = avio_tell(pb);
  2050. avio_wb32(pb, 0); /* size placeholder*/
  2051. ffio_wfourcc(pb, "MTDT");
  2052. avio_wb16(pb, 4);
  2053. // ?
  2054. avio_wb16(pb, 0x0C); /* size */
  2055. avio_wb32(pb, 0x0B); /* type */
  2056. avio_wb16(pb, language_code("und")); /* language */
  2057. avio_wb16(pb, 0x0); /* ? */
  2058. avio_wb16(pb, 0x021C); /* data */
  2059. mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
  2060. mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
  2061. mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
  2062. update_size(pb, pos2);
  2063. return update_size(pb, pos);
  2064. }
  2065. return 0;
  2066. }
  2067. static void build_chunks(MOVTrack *trk)
  2068. {
  2069. int i;
  2070. MOVIentry *chunk = &trk->cluster[0];
  2071. uint64_t chunkSize = chunk->size;
  2072. chunk->chunkNum = 1;
  2073. if (trk->chunkCount)
  2074. return;
  2075. trk->chunkCount = 1;
  2076. for (i = 1; i<trk->entry; i++){
  2077. if (chunk->pos + chunkSize == trk->cluster[i].pos &&
  2078. chunkSize + trk->cluster[i].size < (1<<20)){
  2079. chunkSize += trk->cluster[i].size;
  2080. chunk->samples_in_chunk += trk->cluster[i].entries;
  2081. } else {
  2082. trk->cluster[i].chunkNum = chunk->chunkNum+1;
  2083. chunk=&trk->cluster[i];
  2084. chunkSize = chunk->size;
  2085. trk->chunkCount++;
  2086. }
  2087. }
  2088. }
  2089. static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
  2090. AVFormatContext *s)
  2091. {
  2092. int i;
  2093. int64_t pos = avio_tell(pb);
  2094. int not_first[AVMEDIA_TYPE_NB]={0};
  2095. avio_wb32(pb, 0); /* size placeholder*/
  2096. ffio_wfourcc(pb, "moov");
  2097. for (i = 0; i < mov->nb_streams; i++) {
  2098. if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2099. continue;
  2100. mov->tracks[i].time = mov->time;
  2101. mov->tracks[i].track_id = i + 1;
  2102. if (mov->tracks[i].entry)
  2103. build_chunks(&mov->tracks[i]);
  2104. }
  2105. if (mov->chapter_track)
  2106. for (i = 0; i < s->nb_streams; i++) {
  2107. mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
  2108. mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
  2109. }
  2110. for (i = 0; i < mov->nb_streams; i++) {
  2111. if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
  2112. mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
  2113. mov->tracks[i].tref_id =
  2114. mov->tracks[mov->tracks[i].src_track].track_id;
  2115. }
  2116. }
  2117. for (i = 0; i < mov->nb_streams; i++) {
  2118. if (mov->tracks[i].tag == MKTAG('t','m','c','d')) {
  2119. int src_trk = mov->tracks[i].src_track;
  2120. mov->tracks[src_trk].tref_tag = mov->tracks[i].tag;
  2121. mov->tracks[src_trk].tref_id = mov->tracks[i].track_id;
  2122. mov->tracks[i].track_duration = mov->tracks[src_trk].track_duration;
  2123. }
  2124. }
  2125. mov_write_mvhd_tag(pb, mov);
  2126. if (mov->mode != MODE_MOV && !mov->iods_skip)
  2127. mov_write_iods_tag(pb, mov);
  2128. for (i = 0; i < mov->nb_streams; i++) {
  2129. if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2130. if (i < s->nb_streams){
  2131. int codec_type= s->streams[i]->codec->codec_type;
  2132. if (codec_type==AVMEDIA_TYPE_AUDIO || codec_type==AVMEDIA_TYPE_SUBTITLE){
  2133. mov->tracks[i].secondary= not_first[codec_type];
  2134. not_first[codec_type]= 1;
  2135. }
  2136. }
  2137. mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
  2138. }
  2139. }
  2140. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  2141. mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
  2142. if (mov->mode == MODE_PSP)
  2143. mov_write_uuidusmt_tag(pb, s);
  2144. else
  2145. mov_write_udta_tag(pb, mov, s);
  2146. return update_size(pb, pos);
  2147. }
  2148. static void param_write_int(AVIOContext *pb, const char *name, int value)
  2149. {
  2150. avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
  2151. }
  2152. static void param_write_string(AVIOContext *pb, const char *name, const char *value)
  2153. {
  2154. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
  2155. }
  2156. static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
  2157. {
  2158. char buf[150];
  2159. len = FFMIN(sizeof(buf) / 2 - 1, len);
  2160. ff_data_to_hex(buf, value, len, 0);
  2161. buf[2 * len] = '\0';
  2162. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
  2163. }
  2164. static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
  2165. {
  2166. int64_t pos = avio_tell(pb);
  2167. int i;
  2168. static const uint8_t uuid[] = {
  2169. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  2170. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  2171. };
  2172. avio_wb32(pb, 0);
  2173. ffio_wfourcc(pb, "uuid");
  2174. avio_write(pb, uuid, sizeof(uuid));
  2175. avio_wb32(pb, 0);
  2176. avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  2177. avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  2178. avio_printf(pb, "<head>\n");
  2179. avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
  2180. LIBAVFORMAT_IDENT);
  2181. avio_printf(pb, "</head>\n");
  2182. avio_printf(pb, "<body>\n");
  2183. avio_printf(pb, "<switch>\n");
  2184. for (i = 0; i < mov->nb_streams; i++) {
  2185. MOVTrack *track = &mov->tracks[i];
  2186. const char *type;
  2187. /* track->track_id is initialized in write_moov, and thus isn't known
  2188. * here yet */
  2189. int track_id = i + 1;
  2190. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2191. type = "video";
  2192. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  2193. type = "audio";
  2194. } else {
  2195. continue;
  2196. }
  2197. avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
  2198. track->enc->bit_rate);
  2199. param_write_int(pb, "systemBitrate", track->enc->bit_rate);
  2200. param_write_int(pb, "trackID", track_id);
  2201. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  2202. if (track->enc->codec_id == AV_CODEC_ID_H264) {
  2203. uint8_t *ptr;
  2204. int size = track->enc->extradata_size;
  2205. if (!ff_avc_write_annexb_extradata(track->enc->extradata, &ptr,
  2206. &size)) {
  2207. param_write_hex(pb, "CodecPrivateData",
  2208. ptr ? ptr : track->enc->extradata,
  2209. size);
  2210. av_free(ptr);
  2211. }
  2212. param_write_string(pb, "FourCC", "H264");
  2213. } else if (track->enc->codec_id == AV_CODEC_ID_VC1) {
  2214. param_write_string(pb, "FourCC", "WVC1");
  2215. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2216. track->enc->extradata_size);
  2217. }
  2218. param_write_int(pb, "MaxWidth", track->enc->width);
  2219. param_write_int(pb, "MaxHeight", track->enc->height);
  2220. param_write_int(pb, "DisplayWidth", track->enc->width);
  2221. param_write_int(pb, "DisplayHeight", track->enc->height);
  2222. } else {
  2223. if (track->enc->codec_id == AV_CODEC_ID_AAC) {
  2224. param_write_string(pb, "FourCC", "AACL");
  2225. } else if (track->enc->codec_id == AV_CODEC_ID_WMAPRO) {
  2226. param_write_string(pb, "FourCC", "WMAP");
  2227. }
  2228. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  2229. track->enc->extradata_size);
  2230. param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
  2231. track->enc->codec_id));
  2232. param_write_int(pb, "Channels", track->enc->channels);
  2233. param_write_int(pb, "SamplingRate", track->enc->sample_rate);
  2234. param_write_int(pb, "BitsPerSample", 16);
  2235. param_write_int(pb, "PacketSize", track->enc->block_align ?
  2236. track->enc->block_align : 4);
  2237. }
  2238. avio_printf(pb, "</%s>\n", type);
  2239. }
  2240. avio_printf(pb, "</switch>\n");
  2241. avio_printf(pb, "</body>\n");
  2242. avio_printf(pb, "</smil>\n");
  2243. return update_size(pb, pos);
  2244. }
  2245. static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  2246. {
  2247. avio_wb32(pb, 16);
  2248. ffio_wfourcc(pb, "mfhd");
  2249. avio_wb32(pb, 0);
  2250. avio_wb32(pb, mov->fragments);
  2251. return 0;
  2252. }
  2253. static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
  2254. int64_t moof_offset)
  2255. {
  2256. int64_t pos = avio_tell(pb);
  2257. uint32_t flags = MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  2258. MOV_TFHD_BASE_DATA_OFFSET;
  2259. if (!track->entry) {
  2260. flags |= MOV_TFHD_DURATION_IS_EMPTY;
  2261. } else {
  2262. flags |= MOV_TFHD_DEFAULT_FLAGS;
  2263. }
  2264. /* Don't set a default sample size, the silverlight player refuses
  2265. * to play files with that set. Don't set a default sample duration,
  2266. * WMP freaks out if it is set. Don't set a base data offset, PIFF
  2267. * file format says it MUST NOT be set. */
  2268. if (track->mode == MODE_ISM)
  2269. flags &= ~(MOV_TFHD_DEFAULT_SIZE | MOV_TFHD_DEFAULT_DURATION |
  2270. MOV_TFHD_BASE_DATA_OFFSET);
  2271. avio_wb32(pb, 0); /* size placeholder */
  2272. ffio_wfourcc(pb, "tfhd");
  2273. avio_w8(pb, 0); /* version */
  2274. avio_wb24(pb, flags);
  2275. avio_wb32(pb, track->track_id); /* track-id */
  2276. if (flags & MOV_TFHD_BASE_DATA_OFFSET)
  2277. avio_wb64(pb, moof_offset);
  2278. if (flags & MOV_TFHD_DEFAULT_DURATION) {
  2279. track->default_duration = get_cluster_duration(track, 0);
  2280. avio_wb32(pb, track->default_duration);
  2281. }
  2282. if (flags & MOV_TFHD_DEFAULT_SIZE) {
  2283. track->default_size = track->entry ? track->cluster[0].size : 1;
  2284. avio_wb32(pb, track->default_size);
  2285. } else
  2286. track->default_size = -1;
  2287. if (flags & MOV_TFHD_DEFAULT_FLAGS) {
  2288. track->default_sample_flags =
  2289. track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  2290. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC) :
  2291. MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO;
  2292. avio_wb32(pb, track->default_sample_flags);
  2293. }
  2294. return update_size(pb, pos);
  2295. }
  2296. static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
  2297. {
  2298. return entry->flags & MOV_SYNC_SAMPLE ? MOV_FRAG_SAMPLE_FLAG_DEPENDS_NO :
  2299. (MOV_FRAG_SAMPLE_FLAG_DEPENDS_YES | MOV_FRAG_SAMPLE_FLAG_IS_NON_SYNC);
  2300. }
  2301. static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
  2302. {
  2303. int64_t pos = avio_tell(pb);
  2304. uint32_t flags = MOV_TRUN_DATA_OFFSET;
  2305. int i;
  2306. for (i = 0; i < track->entry; i++) {
  2307. if (get_cluster_duration(track, i) != track->default_duration)
  2308. flags |= MOV_TRUN_SAMPLE_DURATION;
  2309. if (track->cluster[i].size != track->default_size)
  2310. flags |= MOV_TRUN_SAMPLE_SIZE;
  2311. if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
  2312. flags |= MOV_TRUN_SAMPLE_FLAGS;
  2313. }
  2314. if (!(flags & MOV_TRUN_SAMPLE_FLAGS))
  2315. flags |= MOV_TRUN_FIRST_SAMPLE_FLAGS;
  2316. if (track->flags & MOV_TRACK_CTTS)
  2317. flags |= MOV_TRUN_SAMPLE_CTS;
  2318. avio_wb32(pb, 0); /* size placeholder */
  2319. ffio_wfourcc(pb, "trun");
  2320. avio_w8(pb, 0); /* version */
  2321. avio_wb24(pb, flags);
  2322. avio_wb32(pb, track->entry); /* sample count */
  2323. track->moof_size_offset = avio_tell(pb);
  2324. avio_wb32(pb, 0); /* data offset */
  2325. if (flags & MOV_TRUN_FIRST_SAMPLE_FLAGS)
  2326. avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
  2327. for (i = 0; i < track->entry; i++) {
  2328. if (flags & MOV_TRUN_SAMPLE_DURATION)
  2329. avio_wb32(pb, get_cluster_duration(track, i));
  2330. if (flags & MOV_TRUN_SAMPLE_SIZE)
  2331. avio_wb32(pb, track->cluster[i].size);
  2332. if (flags & MOV_TRUN_SAMPLE_FLAGS)
  2333. avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
  2334. if (flags & MOV_TRUN_SAMPLE_CTS)
  2335. avio_wb32(pb, track->cluster[i].cts);
  2336. }
  2337. return update_size(pb, pos);
  2338. }
  2339. static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
  2340. {
  2341. int64_t pos = avio_tell(pb);
  2342. static const uint8_t uuid[] = {
  2343. 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
  2344. 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
  2345. };
  2346. avio_wb32(pb, 0); /* size placeholder */
  2347. ffio_wfourcc(pb, "uuid");
  2348. avio_write(pb, uuid, sizeof(uuid));
  2349. avio_w8(pb, 1);
  2350. avio_wb24(pb, 0);
  2351. avio_wb64(pb, track->frag_start);
  2352. avio_wb64(pb, track->start_dts + track->track_duration -
  2353. track->cluster[0].dts);
  2354. return update_size(pb, pos);
  2355. }
  2356. static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2357. MOVTrack *track, int entry)
  2358. {
  2359. int n = track->nb_frag_info - 1 - entry, i;
  2360. int size = 8 + 16 + 4 + 1 + 16*n;
  2361. static const uint8_t uuid[] = {
  2362. 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
  2363. 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
  2364. };
  2365. if (entry < 0)
  2366. return 0;
  2367. avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
  2368. avio_wb32(pb, size);
  2369. ffio_wfourcc(pb, "uuid");
  2370. avio_write(pb, uuid, sizeof(uuid));
  2371. avio_w8(pb, 1);
  2372. avio_wb24(pb, 0);
  2373. avio_w8(pb, n);
  2374. for (i = 0; i < n; i++) {
  2375. int index = entry + 1 + i;
  2376. avio_wb64(pb, track->frag_info[index].time);
  2377. avio_wb64(pb, track->frag_info[index].duration);
  2378. }
  2379. if (n < mov->ism_lookahead) {
  2380. int free_size = 16 * (mov->ism_lookahead - n);
  2381. avio_wb32(pb, free_size);
  2382. ffio_wfourcc(pb, "free");
  2383. ffio_fill(pb, 0, free_size - 8);
  2384. }
  2385. return 0;
  2386. }
  2387. static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
  2388. MOVTrack *track)
  2389. {
  2390. int64_t pos = avio_tell(pb);
  2391. int i;
  2392. for (i = 0; i < mov->ism_lookahead; i++) {
  2393. /* Update the tfrf tag for the last ism_lookahead fragments,
  2394. * nb_frag_info - 1 is the next fragment to be written. */
  2395. mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
  2396. }
  2397. avio_seek(pb, pos, SEEK_SET);
  2398. return 0;
  2399. }
  2400. static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2401. MOVTrack *track, int64_t moof_offset)
  2402. {
  2403. int64_t pos = avio_tell(pb);
  2404. avio_wb32(pb, 0); /* size placeholder */
  2405. ffio_wfourcc(pb, "traf");
  2406. mov_write_tfhd_tag(pb, track, moof_offset);
  2407. mov_write_trun_tag(pb, track);
  2408. if (mov->mode == MODE_ISM) {
  2409. mov_write_tfxd_tag(pb, track);
  2410. if (mov->ism_lookahead) {
  2411. int i, size = 16 + 4 + 1 + 16 * mov->ism_lookahead;
  2412. track->tfrf_offset = avio_tell(pb);
  2413. avio_wb32(pb, 8 + size);
  2414. ffio_wfourcc(pb, "free");
  2415. for (i = 0; i < size; i++)
  2416. avio_w8(pb, 0);
  2417. }
  2418. }
  2419. return update_size(pb, pos);
  2420. }
  2421. static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
  2422. {
  2423. int64_t pos = avio_tell(pb), end;
  2424. int i, moof_size;
  2425. avio_wb32(pb, 0); /* size placeholder */
  2426. ffio_wfourcc(pb, "moof");
  2427. mov_write_mfhd_tag(pb, mov);
  2428. for (i = 0; i < mov->nb_streams; i++) {
  2429. MOVTrack *track = &mov->tracks[i];
  2430. if (tracks >= 0 && i != tracks)
  2431. continue;
  2432. if (!track->entry)
  2433. continue;
  2434. mov_write_traf_tag(pb, mov, track, pos);
  2435. }
  2436. end = avio_tell(pb);
  2437. moof_size = end - pos;
  2438. for (i = 0; i < mov->nb_streams; i++) {
  2439. MOVTrack *track = &mov->tracks[i];
  2440. if (tracks >= 0 && i != tracks)
  2441. continue;
  2442. if (!track->entry)
  2443. continue;
  2444. avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
  2445. avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
  2446. }
  2447. avio_seek(pb, end, SEEK_SET);
  2448. return update_size(pb, pos);
  2449. }
  2450. static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
  2451. {
  2452. int64_t pos = avio_tell(pb);
  2453. int i;
  2454. avio_wb32(pb, 0); /* size placeholder */
  2455. ffio_wfourcc(pb, "tfra");
  2456. avio_w8(pb, 1); /* version */
  2457. avio_wb24(pb, 0);
  2458. avio_wb32(pb, track->track_id);
  2459. avio_wb32(pb, 0); /* length of traf/trun/sample num */
  2460. avio_wb32(pb, track->nb_frag_info);
  2461. for (i = 0; i < track->nb_frag_info; i++) {
  2462. avio_wb64(pb, track->frag_info[i].time);
  2463. avio_wb64(pb, track->frag_info[i].offset);
  2464. avio_w8(pb, 1); /* traf number */
  2465. avio_w8(pb, 1); /* trun number */
  2466. avio_w8(pb, 1); /* sample number */
  2467. }
  2468. return update_size(pb, pos);
  2469. }
  2470. static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
  2471. {
  2472. int64_t pos = avio_tell(pb);
  2473. int i;
  2474. avio_wb32(pb, 0); /* size placeholder */
  2475. ffio_wfourcc(pb, "mfra");
  2476. /* An empty mfra atom is enough to indicate to the publishing point that
  2477. * the stream has ended. */
  2478. if (mov->flags & FF_MOV_FLAG_ISML)
  2479. return update_size(pb, pos);
  2480. for (i = 0; i < mov->nb_streams; i++) {
  2481. MOVTrack *track = &mov->tracks[i];
  2482. if (track->nb_frag_info)
  2483. mov_write_tfra_tag(pb, track);
  2484. }
  2485. avio_wb32(pb, 16);
  2486. ffio_wfourcc(pb, "mfro");
  2487. avio_wb32(pb, 0); /* version + flags */
  2488. avio_wb32(pb, avio_tell(pb) + 4 - pos);
  2489. return update_size(pb, pos);
  2490. }
  2491. static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
  2492. {
  2493. avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
  2494. ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
  2495. mov->mdat_pos = avio_tell(pb);
  2496. avio_wb32(pb, 0); /* size placeholder*/
  2497. ffio_wfourcc(pb, "mdat");
  2498. return 0;
  2499. }
  2500. /* TODO: This needs to be more general */
  2501. static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
  2502. {
  2503. MOVMuxContext *mov = s->priv_data;
  2504. int64_t pos = avio_tell(pb);
  2505. int has_h264 = 0, has_video = 0;
  2506. int minor = 0x200;
  2507. int i;
  2508. for (i = 0; i < s->nb_streams; i++) {
  2509. AVStream *st = s->streams[i];
  2510. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  2511. has_video = 1;
  2512. if (st->codec->codec_id == AV_CODEC_ID_H264)
  2513. has_h264 = 1;
  2514. }
  2515. avio_wb32(pb, 0); /* size */
  2516. ffio_wfourcc(pb, "ftyp");
  2517. if (mov->mode == MODE_3GP) {
  2518. ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
  2519. minor = has_h264 ? 0x100 : 0x200;
  2520. } else if (mov->mode & MODE_3G2) {
  2521. ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
  2522. minor = has_h264 ? 0x20000 : 0x10000;
  2523. } else if (mov->mode == MODE_PSP)
  2524. ffio_wfourcc(pb, "MSNV");
  2525. else if (mov->mode == MODE_MP4)
  2526. ffio_wfourcc(pb, "isom");
  2527. else if (mov->mode == MODE_IPOD)
  2528. ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
  2529. else if (mov->mode == MODE_ISM)
  2530. ffio_wfourcc(pb, "isml");
  2531. else if (mov->mode == MODE_F4V)
  2532. ffio_wfourcc(pb, "f4v ");
  2533. else
  2534. ffio_wfourcc(pb, "qt ");
  2535. avio_wb32(pb, minor);
  2536. if (mov->mode == MODE_MOV)
  2537. ffio_wfourcc(pb, "qt ");
  2538. else if (mov->mode == MODE_ISM) {
  2539. ffio_wfourcc(pb, "piff");
  2540. ffio_wfourcc(pb, "iso2");
  2541. } else {
  2542. ffio_wfourcc(pb, "isom");
  2543. ffio_wfourcc(pb, "iso2");
  2544. if (has_h264)
  2545. ffio_wfourcc(pb, "avc1");
  2546. }
  2547. if (mov->mode == MODE_3GP)
  2548. ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
  2549. else if (mov->mode & MODE_3G2)
  2550. ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
  2551. else if (mov->mode == MODE_PSP)
  2552. ffio_wfourcc(pb, "MSNV");
  2553. else if (mov->mode == MODE_MP4)
  2554. ffio_wfourcc(pb, "mp41");
  2555. return update_size(pb, pos);
  2556. }
  2557. static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
  2558. {
  2559. AVCodecContext *video_codec = s->streams[0]->codec;
  2560. AVCodecContext *audio_codec = s->streams[1]->codec;
  2561. int audio_rate = audio_codec->sample_rate;
  2562. int frame_rate = ((video_codec->time_base.den) * (0x10000)) / (video_codec->time_base.num);
  2563. int audio_kbitrate = audio_codec->bit_rate / 1000;
  2564. int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
  2565. avio_wb32(pb, 0x94); /* size */
  2566. ffio_wfourcc(pb, "uuid");
  2567. ffio_wfourcc(pb, "PROF");
  2568. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2569. avio_wb32(pb, 0xbb88695c);
  2570. avio_wb32(pb, 0xfac9c740);
  2571. avio_wb32(pb, 0x0); /* ? */
  2572. avio_wb32(pb, 0x3); /* 3 sections ? */
  2573. avio_wb32(pb, 0x14); /* size */
  2574. ffio_wfourcc(pb, "FPRF");
  2575. avio_wb32(pb, 0x0); /* ? */
  2576. avio_wb32(pb, 0x0); /* ? */
  2577. avio_wb32(pb, 0x0); /* ? */
  2578. avio_wb32(pb, 0x2c); /* size */
  2579. ffio_wfourcc(pb, "APRF"); /* audio */
  2580. avio_wb32(pb, 0x0);
  2581. avio_wb32(pb, 0x2); /* TrackID */
  2582. ffio_wfourcc(pb, "mp4a");
  2583. avio_wb32(pb, 0x20f);
  2584. avio_wb32(pb, 0x0);
  2585. avio_wb32(pb, audio_kbitrate);
  2586. avio_wb32(pb, audio_kbitrate);
  2587. avio_wb32(pb, audio_rate);
  2588. avio_wb32(pb, audio_codec->channels);
  2589. avio_wb32(pb, 0x34); /* size */
  2590. ffio_wfourcc(pb, "VPRF"); /* video */
  2591. avio_wb32(pb, 0x0);
  2592. avio_wb32(pb, 0x1); /* TrackID */
  2593. if (video_codec->codec_id == AV_CODEC_ID_H264) {
  2594. ffio_wfourcc(pb, "avc1");
  2595. avio_wb16(pb, 0x014D);
  2596. avio_wb16(pb, 0x0015);
  2597. } else {
  2598. ffio_wfourcc(pb, "mp4v");
  2599. avio_wb16(pb, 0x0000);
  2600. avio_wb16(pb, 0x0103);
  2601. }
  2602. avio_wb32(pb, 0x0);
  2603. avio_wb32(pb, video_kbitrate);
  2604. avio_wb32(pb, video_kbitrate);
  2605. avio_wb32(pb, frame_rate);
  2606. avio_wb32(pb, frame_rate);
  2607. avio_wb16(pb, video_codec->width);
  2608. avio_wb16(pb, video_codec->height);
  2609. avio_wb32(pb, 0x010001); /* ? */
  2610. }
  2611. static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
  2612. {
  2613. uint32_t c = -1;
  2614. int i, closed_gop = 0;
  2615. for (i = 0; i < pkt->size - 4; i++) {
  2616. c = (c << 8) + pkt->data[i];
  2617. if (c == 0x1b8) { // gop
  2618. closed_gop = pkt->data[i + 4] >> 6 & 0x01;
  2619. } else if (c == 0x100) { // pic
  2620. int temp_ref = (pkt->data[i + 1] << 2) | (pkt->data[i + 2] >> 6);
  2621. if (!temp_ref || closed_gop) // I picture is not reordered
  2622. *flags = MOV_SYNC_SAMPLE;
  2623. else
  2624. *flags = MOV_PARTIAL_SYNC_SAMPLE;
  2625. break;
  2626. }
  2627. }
  2628. return 0;
  2629. }
  2630. static void mov_parse_vc1_frame(AVPacket *pkt, MOVTrack *trk, int fragment)
  2631. {
  2632. const uint8_t *start, *next, *end = pkt->data + pkt->size;
  2633. int seq = 0, entry = 0;
  2634. int key = pkt->flags & AV_PKT_FLAG_KEY;
  2635. start = find_next_marker(pkt->data, end);
  2636. for (next = start; next < end; start = next) {
  2637. next = find_next_marker(start + 4, end);
  2638. switch (AV_RB32(start)) {
  2639. case VC1_CODE_SEQHDR:
  2640. seq = 1;
  2641. break;
  2642. case VC1_CODE_ENTRYPOINT:
  2643. entry = 1;
  2644. break;
  2645. case VC1_CODE_SLICE:
  2646. trk->vc1_info.slices = 1;
  2647. break;
  2648. }
  2649. }
  2650. if (!trk->entry && !fragment) {
  2651. /* First packet in first fragment */
  2652. trk->vc1_info.first_packet_seq = seq;
  2653. trk->vc1_info.first_packet_entry = entry;
  2654. } else if ((seq && !trk->vc1_info.packet_seq) ||
  2655. (entry && !trk->vc1_info.packet_entry)) {
  2656. int i;
  2657. for (i = 0; i < trk->entry; i++)
  2658. trk->cluster[i].flags &= ~MOV_SYNC_SAMPLE;
  2659. trk->has_keyframes = 0;
  2660. if (seq)
  2661. trk->vc1_info.packet_seq = 1;
  2662. if (entry)
  2663. trk->vc1_info.packet_entry = 1;
  2664. if (!fragment) {
  2665. /* First fragment */
  2666. if ((!seq || trk->vc1_info.first_packet_seq) &&
  2667. (!entry || trk->vc1_info.first_packet_entry)) {
  2668. /* First packet had the same headers as this one, readd the
  2669. * sync sample flag. */
  2670. trk->cluster[0].flags |= MOV_SYNC_SAMPLE;
  2671. trk->has_keyframes = 1;
  2672. }
  2673. }
  2674. }
  2675. if (trk->vc1_info.packet_seq && trk->vc1_info.packet_entry)
  2676. key = seq && entry;
  2677. else if (trk->vc1_info.packet_seq)
  2678. key = seq;
  2679. else if (trk->vc1_info.packet_entry)
  2680. key = entry;
  2681. if (key) {
  2682. trk->cluster[trk->entry].flags |= MOV_SYNC_SAMPLE;
  2683. trk->has_keyframes++;
  2684. }
  2685. }
  2686. static int get_moov_size(AVFormatContext *s)
  2687. {
  2688. int ret;
  2689. uint8_t *buf;
  2690. AVIOContext *moov_buf;
  2691. MOVMuxContext *mov = s->priv_data;
  2692. if ((ret = avio_open_dyn_buf(&moov_buf)) < 0)
  2693. return ret;
  2694. mov_write_moov_tag(moov_buf, mov, s);
  2695. ret = avio_close_dyn_buf(moov_buf, &buf);
  2696. av_free(buf);
  2697. return ret;
  2698. }
  2699. static int mov_flush_fragment(AVFormatContext *s)
  2700. {
  2701. MOVMuxContext *mov = s->priv_data;
  2702. int i, first_track = -1;
  2703. int64_t mdat_size = 0;
  2704. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2705. return 0;
  2706. if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
  2707. int64_t pos = avio_tell(s->pb);
  2708. uint8_t *buf;
  2709. int buf_size, moov_size;
  2710. for (i = 0; i < mov->nb_streams; i++)
  2711. if (!mov->tracks[i].entry)
  2712. break;
  2713. /* Don't write the initial moov unless all tracks have data */
  2714. if (i < mov->nb_streams)
  2715. return 0;
  2716. moov_size = get_moov_size(s);
  2717. for (i = 0; i < mov->nb_streams; i++)
  2718. mov->tracks[i].data_offset = pos + moov_size + 8;
  2719. mov_write_moov_tag(s->pb, mov, s);
  2720. buf_size = avio_close_dyn_buf(mov->mdat_buf, &buf);
  2721. mov->mdat_buf = NULL;
  2722. avio_wb32(s->pb, buf_size + 8);
  2723. ffio_wfourcc(s->pb, "mdat");
  2724. avio_write(s->pb, buf, buf_size);
  2725. av_free(buf);
  2726. mov->fragments++;
  2727. mov->mdat_size = 0;
  2728. for (i = 0; i < mov->nb_streams; i++) {
  2729. if (mov->tracks[i].entry)
  2730. mov->tracks[i].frag_start += mov->tracks[i].start_dts +
  2731. mov->tracks[i].track_duration -
  2732. mov->tracks[i].cluster[0].dts;
  2733. mov->tracks[i].entry = 0;
  2734. }
  2735. avio_flush(s->pb);
  2736. return 0;
  2737. }
  2738. for (i = 0; i < mov->nb_streams; i++) {
  2739. MOVTrack *track = &mov->tracks[i];
  2740. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
  2741. track->data_offset = 0;
  2742. else
  2743. track->data_offset = mdat_size;
  2744. if (!track->mdat_buf)
  2745. continue;
  2746. mdat_size += avio_tell(track->mdat_buf);
  2747. if (first_track < 0)
  2748. first_track = i;
  2749. }
  2750. if (!mdat_size)
  2751. return 0;
  2752. for (i = 0; i < mov->nb_streams; i++) {
  2753. MOVTrack *track = &mov->tracks[i];
  2754. int buf_size, write_moof = 1, moof_tracks = -1;
  2755. uint8_t *buf;
  2756. int64_t duration = 0;
  2757. if (track->entry)
  2758. duration = track->start_dts + track->track_duration -
  2759. track->cluster[0].dts;
  2760. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
  2761. if (!track->mdat_buf)
  2762. continue;
  2763. mdat_size = avio_tell(track->mdat_buf);
  2764. moof_tracks = i;
  2765. } else {
  2766. write_moof = i == first_track;
  2767. }
  2768. if (write_moof) {
  2769. MOVFragmentInfo *info;
  2770. avio_flush(s->pb);
  2771. track->nb_frag_info++;
  2772. if (track->nb_frag_info >= track->frag_info_capacity) {
  2773. unsigned new_capacity = track->nb_frag_info + MOV_FRAG_INFO_ALLOC_INCREMENT;
  2774. if (av_reallocp_array(&track->frag_info,
  2775. new_capacity,
  2776. sizeof(*track->frag_info)))
  2777. return AVERROR(ENOMEM);
  2778. track->frag_info_capacity = new_capacity;
  2779. }
  2780. info = &track->frag_info[track->nb_frag_info - 1];
  2781. info->offset = avio_tell(s->pb);
  2782. info->time = mov->tracks[i].frag_start;
  2783. info->duration = duration;
  2784. mov_write_tfrf_tags(s->pb, mov, track);
  2785. mov_write_moof_tag(s->pb, mov, moof_tracks);
  2786. info->tfrf_offset = track->tfrf_offset;
  2787. mov->fragments++;
  2788. avio_wb32(s->pb, mdat_size + 8);
  2789. ffio_wfourcc(s->pb, "mdat");
  2790. }
  2791. if (track->entry)
  2792. track->frag_start += duration;
  2793. track->entry = 0;
  2794. if (!track->mdat_buf)
  2795. continue;
  2796. buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
  2797. track->mdat_buf = NULL;
  2798. avio_write(s->pb, buf, buf_size);
  2799. av_free(buf);
  2800. }
  2801. mov->mdat_size = 0;
  2802. avio_flush(s->pb);
  2803. return 0;
  2804. }
  2805. int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  2806. {
  2807. MOVMuxContext *mov = s->priv_data;
  2808. AVIOContext *pb = s->pb;
  2809. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  2810. AVCodecContext *enc = trk->enc;
  2811. unsigned int samples_in_chunk = 0;
  2812. int size = pkt->size;
  2813. uint8_t *reformatted_data = NULL;
  2814. if (mov->flags & FF_MOV_FLAG_FRAGMENT) {
  2815. int ret;
  2816. if (mov->fragments > 0) {
  2817. if (!trk->mdat_buf) {
  2818. if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
  2819. return ret;
  2820. }
  2821. pb = trk->mdat_buf;
  2822. } else {
  2823. if (!mov->mdat_buf) {
  2824. if ((ret = avio_open_dyn_buf(&mov->mdat_buf)) < 0)
  2825. return ret;
  2826. }
  2827. pb = mov->mdat_buf;
  2828. }
  2829. }
  2830. if (enc->codec_id == AV_CODEC_ID_AMR_NB) {
  2831. /* We must find out how many AMR blocks there are in one packet */
  2832. static uint16_t packed_size[16] =
  2833. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
  2834. int len = 0;
  2835. while (len < size && samples_in_chunk < 100) {
  2836. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  2837. samples_in_chunk++;
  2838. }
  2839. if (samples_in_chunk > 1) {
  2840. av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
  2841. return -1;
  2842. }
  2843. } else if (enc->codec_id == AV_CODEC_ID_ADPCM_MS ||
  2844. enc->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV) {
  2845. samples_in_chunk = enc->frame_size;
  2846. } else if (trk->sample_size)
  2847. samples_in_chunk = size / trk->sample_size;
  2848. else
  2849. samples_in_chunk = 1;
  2850. /* copy extradata if it exists */
  2851. if (trk->vos_len == 0 && enc->extradata_size > 0) {
  2852. trk->vos_len = enc->extradata_size;
  2853. trk->vos_data = av_malloc(trk->vos_len);
  2854. memcpy(trk->vos_data, enc->extradata, trk->vos_len);
  2855. }
  2856. if (enc->codec_id == AV_CODEC_ID_AAC && pkt->size > 2 &&
  2857. (AV_RB16(pkt->data) & 0xfff0) == 0xfff0) {
  2858. if (!s->streams[pkt->stream_index]->nb_frames) {
  2859. av_log(s, AV_LOG_ERROR, "Malformed AAC bitstream detected: "
  2860. "use audio bitstream filter 'aac_adtstoasc' to fix it "
  2861. "('-bsf:a aac_adtstoasc' option with ffmpeg)\n");
  2862. return -1;
  2863. }
  2864. av_log(s, AV_LOG_WARNING, "aac bitstream error\n");
  2865. }
  2866. if (enc->codec_id == AV_CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
  2867. /* from x264 or from bytestream h264 */
  2868. /* nal reformating needed */
  2869. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
  2870. ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
  2871. &size);
  2872. avio_write(pb, reformatted_data, size);
  2873. } else {
  2874. size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
  2875. }
  2876. } else {
  2877. avio_write(pb, pkt->data, size);
  2878. }
  2879. if ((enc->codec_id == AV_CODEC_ID_DNXHD ||
  2880. enc->codec_id == AV_CODEC_ID_AC3) && !trk->vos_len) {
  2881. /* copy frame to create needed atoms */
  2882. trk->vos_len = size;
  2883. trk->vos_data = av_malloc(size);
  2884. if (!trk->vos_data)
  2885. return AVERROR(ENOMEM);
  2886. memcpy(trk->vos_data, pkt->data, size);
  2887. }
  2888. if (trk->entry >= trk->cluster_capacity) {
  2889. unsigned new_capacity = 2 * (trk->entry + MOV_INDEX_CLUSTER_SIZE);
  2890. if (av_reallocp_array(&trk->cluster, new_capacity,
  2891. sizeof(*trk->cluster)))
  2892. return AVERROR(ENOMEM);
  2893. trk->cluster_capacity = new_capacity;
  2894. }
  2895. trk->cluster[trk->entry].pos = avio_tell(pb) - size;
  2896. trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
  2897. trk->cluster[trk->entry].chunkNum = 0;
  2898. trk->cluster[trk->entry].size = size;
  2899. trk->cluster[trk->entry].entries = samples_in_chunk;
  2900. trk->cluster[trk->entry].dts = pkt->dts;
  2901. if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
  2902. /* First packet of a new fragment. We already wrote the duration
  2903. * of the last packet of the previous fragment based on track_duration,
  2904. * which might not exactly match our dts. Therefore adjust the dts
  2905. * of this packet to be what the previous packets duration implies. */
  2906. trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
  2907. }
  2908. if (!trk->entry && trk->start_dts == AV_NOPTS_VALUE && !supports_edts(mov)) {
  2909. trk->cluster[trk->entry].dts = trk->start_dts = 0;
  2910. }
  2911. if (trk->start_dts == AV_NOPTS_VALUE)
  2912. trk->start_dts = pkt->dts;
  2913. trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
  2914. trk->last_sample_is_subtitle_end = 0;
  2915. if (pkt->pts == AV_NOPTS_VALUE) {
  2916. av_log(s, AV_LOG_WARNING, "pts has no value\n");
  2917. pkt->pts = pkt->dts;
  2918. }
  2919. if (pkt->dts != pkt->pts)
  2920. trk->flags |= MOV_TRACK_CTTS;
  2921. trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
  2922. trk->cluster[trk->entry].flags = 0;
  2923. if (enc->codec_id == AV_CODEC_ID_VC1) {
  2924. mov_parse_vc1_frame(pkt, trk, mov->fragments);
  2925. } else if (pkt->flags & AV_PKT_FLAG_KEY) {
  2926. if (mov->mode == MODE_MOV && enc->codec_id == AV_CODEC_ID_MPEG2VIDEO &&
  2927. trk->entry > 0) { // force sync sample for the first key frame
  2928. mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
  2929. if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
  2930. trk->flags |= MOV_TRACK_STPS;
  2931. } else {
  2932. trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
  2933. }
  2934. if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
  2935. trk->has_keyframes++;
  2936. }
  2937. trk->entry++;
  2938. trk->sample_count += samples_in_chunk;
  2939. mov->mdat_size += size;
  2940. avio_flush(pb);
  2941. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
  2942. ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
  2943. reformatted_data, size);
  2944. av_free(reformatted_data);
  2945. return 0;
  2946. }
  2947. static int mov_write_single_packet(AVFormatContext *s, AVPacket *pkt)
  2948. {
  2949. MOVMuxContext *mov = s->priv_data;
  2950. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  2951. AVCodecContext *enc = trk->enc;
  2952. int64_t frag_duration = 0;
  2953. int size = pkt->size;
  2954. if (!pkt->size)
  2955. return 0; /* Discard 0 sized packets */
  2956. if (trk->entry && pkt->stream_index < s->nb_streams)
  2957. frag_duration = av_rescale_q(pkt->dts - trk->cluster[0].dts,
  2958. s->streams[pkt->stream_index]->time_base,
  2959. AV_TIME_BASE_Q);
  2960. if ((mov->max_fragment_duration &&
  2961. frag_duration >= mov->max_fragment_duration) ||
  2962. (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
  2963. (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
  2964. enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  2965. trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
  2966. if (frag_duration >= mov->min_fragment_duration)
  2967. mov_flush_fragment(s);
  2968. }
  2969. return ff_mov_write_packet(s, pkt);
  2970. }
  2971. static int mov_write_subtitle_end_packet(AVFormatContext *s,
  2972. int stream_index,
  2973. int64_t dts) {
  2974. AVPacket end;
  2975. uint8_t data[2] = {0};
  2976. int ret;
  2977. av_init_packet(&end);
  2978. end.size = sizeof(data);
  2979. end.data = data;
  2980. end.pts = dts;
  2981. end.dts = dts;
  2982. end.duration = 0;
  2983. end.stream_index = stream_index;
  2984. ret = mov_write_single_packet(s, &end);
  2985. av_free_packet(&end);
  2986. return ret;
  2987. }
  2988. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  2989. {
  2990. if (!pkt) {
  2991. mov_flush_fragment(s);
  2992. return 1;
  2993. } else {
  2994. int i;
  2995. MOVMuxContext *mov = s->priv_data;
  2996. if (!pkt->size) return 0; /* Discard 0 sized packets */
  2997. /*
  2998. * Subtitles require special handling.
  2999. *
  3000. * 1) For full complaince, every track must have a sample at
  3001. * dts == 0, which is rarely true for subtitles. So, as soon
  3002. * as we see any packet with dts > 0, write an empty subtitle
  3003. * at dts == 0 for any subtitle track with no samples in it.
  3004. *
  3005. * 2) For each subtitle track, check if the current packet's
  3006. * dts is past the duration of the last subtitle sample. If
  3007. * so, we now need to write an end sample for that subtitle.
  3008. *
  3009. * This must be done conditionally to allow for subtitles that
  3010. * immediately replace each other, in which case an end sample
  3011. * is not needed, and is, in fact, actively harmful.
  3012. *
  3013. * 3) See mov_write_trailer for how the final end sample is
  3014. * handled.
  3015. */
  3016. for (i = 0; i < mov->nb_streams; i++) {
  3017. MOVTrack *trk = &mov->tracks[i];
  3018. int ret;
  3019. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  3020. trk->track_duration < pkt->dts &&
  3021. (trk->entry == 0 || !trk->last_sample_is_subtitle_end)) {
  3022. ret = mov_write_subtitle_end_packet(s, i, trk->track_duration);
  3023. if (ret < 0) return ret;
  3024. trk->last_sample_is_subtitle_end = 1;
  3025. }
  3026. }
  3027. return mov_write_single_packet(s, pkt);
  3028. }
  3029. }
  3030. // QuickTime chapters involve an additional text track with the chapter names
  3031. // as samples, and a tref pointing from the other tracks to the chapter one.
  3032. static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
  3033. {
  3034. AVIOContext *pb;
  3035. MOVMuxContext *mov = s->priv_data;
  3036. MOVTrack *track = &mov->tracks[tracknum];
  3037. AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
  3038. int i, len;
  3039. track->mode = mov->mode;
  3040. track->tag = MKTAG('t','e','x','t');
  3041. track->timescale = MOV_TIMESCALE;
  3042. track->enc = avcodec_alloc_context3(NULL);
  3043. track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  3044. if (avio_open_dyn_buf(&pb) >= 0) {
  3045. int size;
  3046. uint8_t *buf;
  3047. /* Stub header (usually for Quicktime chapter track) */
  3048. // TextSampleEntry
  3049. avio_wb32(pb, 0x01); // displayFlags
  3050. avio_w8(pb, 0x00); // horizontal justification
  3051. avio_w8(pb, 0x00); // vertical justification
  3052. avio_w8(pb, 0x00); // bgColourRed
  3053. avio_w8(pb, 0x00); // bgColourGreen
  3054. avio_w8(pb, 0x00); // bgColourBlue
  3055. avio_w8(pb, 0x00); // bgColourAlpha
  3056. // BoxRecord
  3057. avio_wb16(pb, 0x00); // defTextBoxTop
  3058. avio_wb16(pb, 0x00); // defTextBoxLeft
  3059. avio_wb16(pb, 0x00); // defTextBoxBottom
  3060. avio_wb16(pb, 0x00); // defTextBoxRight
  3061. // StyleRecord
  3062. avio_wb16(pb, 0x00); // startChar
  3063. avio_wb16(pb, 0x00); // endChar
  3064. avio_wb16(pb, 0x01); // fontID
  3065. avio_w8(pb, 0x00); // fontStyleFlags
  3066. avio_w8(pb, 0x00); // fontSize
  3067. avio_w8(pb, 0x00); // fgColourRed
  3068. avio_w8(pb, 0x00); // fgColourGreen
  3069. avio_w8(pb, 0x00); // fgColourBlue
  3070. avio_w8(pb, 0x00); // fgColourAlpha
  3071. // FontTableBox
  3072. avio_wb32(pb, 0x0D); // box size
  3073. ffio_wfourcc(pb, "ftab"); // box atom name
  3074. avio_wb16(pb, 0x01); // entry count
  3075. // FontRecord
  3076. avio_wb16(pb, 0x01); // font ID
  3077. avio_w8(pb, 0x00); // font name length
  3078. if ((size = avio_close_dyn_buf(pb, &buf)) > 0) {
  3079. track->enc->extradata = buf;
  3080. track->enc->extradata_size = size;
  3081. } else {
  3082. av_free(&buf);
  3083. }
  3084. }
  3085. for (i = 0; i < s->nb_chapters; i++) {
  3086. AVChapter *c = s->chapters[i];
  3087. AVDictionaryEntry *t;
  3088. int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
  3089. pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
  3090. pkt.duration = end - pkt.dts;
  3091. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  3092. len = strlen(t->value);
  3093. pkt.size = len + 2;
  3094. pkt.data = av_malloc(pkt.size);
  3095. AV_WB16(pkt.data, len);
  3096. memcpy(pkt.data + 2, t->value, len);
  3097. ff_mov_write_packet(s, &pkt);
  3098. av_freep(&pkt.data);
  3099. }
  3100. }
  3101. }
  3102. static int mov_create_timecode_track(AVFormatContext *s, int index, int src_index, const char *tcstr)
  3103. {
  3104. int ret;
  3105. MOVMuxContext *mov = s->priv_data;
  3106. MOVTrack *track = &mov->tracks[index];
  3107. AVStream *src_st = s->streams[src_index];
  3108. AVTimecode tc;
  3109. AVPacket pkt = {.stream_index = index, .flags = AV_PKT_FLAG_KEY, .size = 4};
  3110. AVRational rate = {src_st->codec->time_base.den, src_st->codec->time_base.num};
  3111. /* if the codec time base makes no sense, try to fallback on stream frame rate */
  3112. if (av_timecode_check_frame_rate(rate) < 0) {
  3113. av_log(s, AV_LOG_DEBUG, "timecode: tbc=%d/%d invalid, fallback on %d/%d\n",
  3114. rate.num, rate.den, src_st->avg_frame_rate.num, src_st->avg_frame_rate.den);
  3115. rate = src_st->avg_frame_rate;
  3116. }
  3117. /* compute the frame number */
  3118. ret = av_timecode_init_from_string(&tc, rate, tcstr, s);
  3119. if (ret < 0)
  3120. return ret;
  3121. /* tmcd track based on video stream */
  3122. track->mode = mov->mode;
  3123. track->tag = MKTAG('t','m','c','d');
  3124. track->src_track = src_index;
  3125. track->timescale = mov->tracks[src_index].timescale;
  3126. if (tc.flags & AV_TIMECODE_FLAG_DROPFRAME)
  3127. track->timecode_flags |= MOV_TIMECODE_FLAG_DROPFRAME;
  3128. /* encode context: tmcd data stream */
  3129. track->enc = avcodec_alloc_context3(NULL);
  3130. track->enc->codec_type = AVMEDIA_TYPE_DATA;
  3131. track->enc->codec_tag = track->tag;
  3132. track->enc->time_base = src_st->codec->time_base;
  3133. /* the tmcd track just contains one packet with the frame number */
  3134. pkt.data = av_malloc(pkt.size);
  3135. AV_WB32(pkt.data, tc.start);
  3136. ret = ff_mov_write_packet(s, &pkt);
  3137. av_free(pkt.data);
  3138. return ret;
  3139. }
  3140. static int mov_write_header(AVFormatContext *s)
  3141. {
  3142. AVIOContext *pb = s->pb;
  3143. MOVMuxContext *mov = s->priv_data;
  3144. AVDictionaryEntry *t, *global_tcr = av_dict_get(s->metadata, "timecode", NULL, 0);
  3145. int i, hint_track = 0, tmcd_track = 0;
  3146. /* Set the FRAGMENT flag if any of the fragmentation methods are
  3147. * enabled. */
  3148. if (mov->max_fragment_duration || mov->max_fragment_size ||
  3149. mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
  3150. FF_MOV_FLAG_FRAG_KEYFRAME |
  3151. FF_MOV_FLAG_FRAG_CUSTOM))
  3152. mov->flags |= FF_MOV_FLAG_FRAGMENT;
  3153. /* faststart: moov at the beginning of the file, if supported */
  3154. if (mov->flags & FF_MOV_FLAG_FASTSTART) {
  3155. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  3156. mov->flags &= ~FF_MOV_FLAG_FASTSTART;
  3157. else
  3158. mov->reserved_moov_size = -1;
  3159. }
  3160. if (!supports_edts(mov) && s->avoid_negative_ts < 0) {
  3161. s->avoid_negative_ts = 1;
  3162. }
  3163. /* Non-seekable output is ok if using fragmentation. If ism_lookahead
  3164. * is enabled, we don't support non-seekable output at all. */
  3165. if (!s->pb->seekable &&
  3166. ((!(mov->flags & FF_MOV_FLAG_FRAGMENT) &&
  3167. strcmp(s->oformat->name, "ismv"))
  3168. || mov->ism_lookahead)) {
  3169. av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
  3170. return -1;
  3171. }
  3172. /* Default mode == MP4 */
  3173. mov->mode = MODE_MP4;
  3174. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  3175. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
  3176. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  3177. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  3178. else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
  3179. else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
  3180. else if (!strcmp("f4v", s->oformat->name)) mov->mode = MODE_F4V;
  3181. mov_write_ftyp_tag(pb,s);
  3182. if (mov->mode == MODE_PSP) {
  3183. int video_streams_nb = 0, audio_streams_nb = 0, other_streams_nb = 0;
  3184. for (i = 0; i < s->nb_streams; i++) {
  3185. AVStream *st = s->streams[i];
  3186. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  3187. video_streams_nb++;
  3188. else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  3189. audio_streams_nb++;
  3190. else
  3191. other_streams_nb++;
  3192. }
  3193. if (video_streams_nb != 1 || audio_streams_nb != 1 || other_streams_nb) {
  3194. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  3195. return -1;
  3196. }
  3197. mov_write_uuidprof_tag(pb, s);
  3198. }
  3199. mov->nb_streams = s->nb_streams;
  3200. if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
  3201. mov->chapter_track = mov->nb_streams++;
  3202. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  3203. /* Add hint tracks for each audio and video stream */
  3204. hint_track = mov->nb_streams;
  3205. for (i = 0; i < s->nb_streams; i++) {
  3206. AVStream *st = s->streams[i];
  3207. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  3208. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3209. mov->nb_streams++;
  3210. }
  3211. }
  3212. }
  3213. if (mov->mode == MODE_MOV) {
  3214. tmcd_track = mov->nb_streams;
  3215. /* +1 tmcd track for each video stream with a timecode */
  3216. for (i = 0; i < s->nb_streams; i++) {
  3217. AVStream *st = s->streams[i];
  3218. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
  3219. (global_tcr || av_dict_get(st->metadata, "timecode", NULL, 0)))
  3220. mov->nb_meta_tmcd++;
  3221. }
  3222. /* check if there is already a tmcd track to remux */
  3223. if (mov->nb_meta_tmcd) {
  3224. for (i = 0; i < s->nb_streams; i++) {
  3225. AVStream *st = s->streams[i];
  3226. if (st->codec->codec_tag == MKTAG('t','m','c','d')) {
  3227. av_log(s, AV_LOG_WARNING, "You requested a copy of the original timecode track "
  3228. "so timecode metadata are now ignored\n");
  3229. mov->nb_meta_tmcd = 0;
  3230. }
  3231. }
  3232. }
  3233. mov->nb_streams += mov->nb_meta_tmcd;
  3234. }
  3235. mov->tracks = av_mallocz(mov->nb_streams * sizeof(*mov->tracks));
  3236. if (!mov->tracks)
  3237. return AVERROR(ENOMEM);
  3238. for (i = 0; i < s->nb_streams; i++) {
  3239. AVStream *st= s->streams[i];
  3240. MOVTrack *track= &mov->tracks[i];
  3241. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
  3242. track->enc = st->codec;
  3243. track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
  3244. if (track->language < 0)
  3245. track->language = 0;
  3246. track->mode = mov->mode;
  3247. track->tag = mov_find_codec_tag(s, track);
  3248. if (!track->tag) {
  3249. av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
  3250. "codec not currently supported in container\n", i);
  3251. goto error;
  3252. }
  3253. /* If hinting of this track is enabled by a later hint track,
  3254. * this is updated. */
  3255. track->hint_track = -1;
  3256. track->start_dts = AV_NOPTS_VALUE;
  3257. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3258. if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
  3259. track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
  3260. track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
  3261. if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
  3262. av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
  3263. goto error;
  3264. }
  3265. track->height = track->tag >> 24 == 'n' ? 486 : 576;
  3266. }
  3267. if (mov->video_track_timescale) {
  3268. track->timescale = mov->video_track_timescale;
  3269. } else {
  3270. track->timescale = st->codec->time_base.den;
  3271. while(track->timescale < 10000)
  3272. track->timescale *= 2;
  3273. }
  3274. if (track->mode == MODE_MOV && track->timescale > 100000)
  3275. av_log(s, AV_LOG_WARNING,
  3276. "WARNING codec timebase is very high. If duration is too long,\n"
  3277. "file may not be playable by quicktime. Specify a shorter timebase\n"
  3278. "or choose different container.\n");
  3279. } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3280. track->timescale = st->codec->sample_rate;
  3281. if (!st->codec->frame_size && !av_get_bits_per_sample(st->codec->codec_id)) {
  3282. av_log(s, AV_LOG_WARNING, "track %d: codec frame size is not set\n", i);
  3283. track->audio_vbr = 1;
  3284. }else if (st->codec->codec_id == AV_CODEC_ID_ADPCM_MS ||
  3285. st->codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
  3286. st->codec->codec_id == AV_CODEC_ID_ILBC){
  3287. if (!st->codec->block_align) {
  3288. av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set for adpcm\n", i);
  3289. goto error;
  3290. }
  3291. track->sample_size = st->codec->block_align;
  3292. }else if (st->codec->frame_size > 1){ /* assume compressed audio */
  3293. track->audio_vbr = 1;
  3294. }else{
  3295. track->sample_size = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
  3296. }
  3297. if (track->mode != MODE_MOV &&
  3298. track->enc->codec_id == AV_CODEC_ID_MP3 && track->timescale < 16000) {
  3299. av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
  3300. i, track->enc->sample_rate);
  3301. goto error;
  3302. }
  3303. } else if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  3304. track->timescale = st->codec->time_base.den;
  3305. }else{
  3306. track->timescale = MOV_TIMESCALE;
  3307. }
  3308. if (!track->height)
  3309. track->height = st->codec->height;
  3310. /* The ism specific timescale isn't mandatory, but is assumed by
  3311. * some tools, such as mp4split. */
  3312. if (mov->mode == MODE_ISM)
  3313. track->timescale = 10000000;
  3314. avpriv_set_pts_info(st, 64, 1, track->timescale);
  3315. /* copy extradata if it exists */
  3316. if (st->codec->extradata_size) {
  3317. track->vos_len = st->codec->extradata_size;
  3318. track->vos_data = av_malloc(track->vos_len);
  3319. memcpy(track->vos_data, st->codec->extradata, track->vos_len);
  3320. }
  3321. }
  3322. if (mov->mode == MODE_ISM) {
  3323. /* If no fragmentation options have been set, set a default. */
  3324. if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
  3325. FF_MOV_FLAG_FRAG_CUSTOM)) &&
  3326. !mov->max_fragment_duration && !mov->max_fragment_size)
  3327. mov->max_fragment_duration = 5000000;
  3328. mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF |
  3329. FF_MOV_FLAG_FRAGMENT;
  3330. }
  3331. if (mov->reserved_moov_size){
  3332. mov->reserved_moov_pos= avio_tell(pb);
  3333. if (mov->reserved_moov_size > 0)
  3334. avio_skip(pb, mov->reserved_moov_size);
  3335. }
  3336. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  3337. mov_write_mdat_tag(pb, mov);
  3338. if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
  3339. mov->time = ff_iso8601_to_unix_time(t->value);
  3340. if (mov->time)
  3341. mov->time += 0x7C25B080; // 1970 based -> 1904 based
  3342. if (mov->chapter_track)
  3343. mov_create_chapter_track(s, mov->chapter_track);
  3344. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  3345. /* Initialize the hint tracks for each audio and video stream */
  3346. for (i = 0; i < s->nb_streams; i++) {
  3347. AVStream *st = s->streams[i];
  3348. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  3349. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  3350. if (ff_mov_init_hinting(s, hint_track, i) < 0)
  3351. goto error;
  3352. hint_track++;
  3353. }
  3354. }
  3355. }
  3356. if (mov->nb_meta_tmcd) {
  3357. /* Initialize the tmcd tracks */
  3358. for (i = 0; i < s->nb_streams; i++) {
  3359. AVStream *st = s->streams[i];
  3360. t = global_tcr;
  3361. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
  3362. if (!t)
  3363. t = av_dict_get(st->metadata, "timecode", NULL, 0);
  3364. if (!t)
  3365. continue;
  3366. if (mov_create_timecode_track(s, tmcd_track, i, t->value) < 0)
  3367. goto error;
  3368. tmcd_track++;
  3369. }
  3370. }
  3371. }
  3372. avio_flush(pb);
  3373. if (mov->flags & FF_MOV_FLAG_ISML)
  3374. mov_write_isml_manifest(pb, mov);
  3375. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  3376. mov_write_moov_tag(pb, mov, s);
  3377. mov->fragments++;
  3378. }
  3379. return 0;
  3380. error:
  3381. av_freep(&mov->tracks);
  3382. return -1;
  3383. }
  3384. /**
  3385. * This function gets the moov size if moved to the top of the file: the chunk
  3386. * offset table can switch between stco (32-bit entries) to co64 (64-bit
  3387. * entries) when the moov is moved to the top, so the size of the moov would
  3388. * change. It also updates the chunk offset tables.
  3389. */
  3390. static int compute_moov_size(AVFormatContext *s)
  3391. {
  3392. int i, moov_size, moov_size2;
  3393. MOVMuxContext *mov = s->priv_data;
  3394. moov_size = get_moov_size(s);
  3395. if (moov_size < 0)
  3396. return moov_size;
  3397. for (i = 0; i < mov->nb_streams; i++)
  3398. mov->tracks[i].data_offset += moov_size;
  3399. moov_size2 = get_moov_size(s);
  3400. if (moov_size2 < 0)
  3401. return moov_size2;
  3402. /* if the size changed, we just switched from stco to co64 and needs to
  3403. * update the offsets */
  3404. if (moov_size2 != moov_size)
  3405. for (i = 0; i < mov->nb_streams; i++)
  3406. mov->tracks[i].data_offset += moov_size2 - moov_size;
  3407. return moov_size2;
  3408. }
  3409. static int shift_data(AVFormatContext *s)
  3410. {
  3411. int ret = 0, moov_size;
  3412. MOVMuxContext *mov = s->priv_data;
  3413. int64_t pos, pos_end = avio_tell(s->pb);
  3414. uint8_t *buf, *read_buf[2];
  3415. int read_buf_id = 0;
  3416. int read_size[2];
  3417. AVIOContext *read_pb;
  3418. moov_size = compute_moov_size(s);
  3419. if (moov_size < 0)
  3420. return moov_size;
  3421. buf = av_malloc(moov_size * 2);
  3422. if (!buf)
  3423. return AVERROR(ENOMEM);
  3424. read_buf[0] = buf;
  3425. read_buf[1] = buf + moov_size;
  3426. /* Shift the data: the AVIO context of the output can only be used for
  3427. * writing, so we re-open the same output, but for reading. It also avoids
  3428. * a read/seek/write/seek back and forth. */
  3429. avio_flush(s->pb);
  3430. ret = avio_open(&read_pb, s->filename, AVIO_FLAG_READ);
  3431. if (ret < 0) {
  3432. av_log(s, AV_LOG_ERROR, "Unable to re-open %s output file for "
  3433. "the second pass (faststart)\n", s->filename);
  3434. goto end;
  3435. }
  3436. /* mark the end of the shift to up to the last data we wrote, and get ready
  3437. * for writing */
  3438. pos_end = avio_tell(s->pb);
  3439. avio_seek(s->pb, mov->reserved_moov_pos + moov_size, SEEK_SET);
  3440. /* start reading at where the new moov will be placed */
  3441. avio_seek(read_pb, mov->reserved_moov_pos, SEEK_SET);
  3442. pos = avio_tell(read_pb);
  3443. #define READ_BLOCK do { \
  3444. read_size[read_buf_id] = avio_read(read_pb, read_buf[read_buf_id], moov_size); \
  3445. read_buf_id ^= 1; \
  3446. } while (0)
  3447. /* shift data by chunk of at most moov_size */
  3448. READ_BLOCK;
  3449. do {
  3450. int n;
  3451. READ_BLOCK;
  3452. n = read_size[read_buf_id];
  3453. if (n <= 0)
  3454. break;
  3455. avio_write(s->pb, read_buf[read_buf_id], n);
  3456. pos += n;
  3457. } while (pos < pos_end);
  3458. avio_close(read_pb);
  3459. end:
  3460. av_free(buf);
  3461. return ret;
  3462. }
  3463. static int mov_write_trailer(AVFormatContext *s)
  3464. {
  3465. MOVMuxContext *mov = s->priv_data;
  3466. AVIOContext *pb = s->pb;
  3467. int64_t moov_pos;
  3468. int res = 0;
  3469. int i;
  3470. /*
  3471. * Before actually writing the trailer, make sure that there are no
  3472. * dangling subtitles, that need a terminating sample.
  3473. */
  3474. for (i = 0; i < mov->nb_streams; i++) {
  3475. MOVTrack *trk = &mov->tracks[i];
  3476. if (trk->enc->codec_id == AV_CODEC_ID_MOV_TEXT &&
  3477. !trk->last_sample_is_subtitle_end) {
  3478. mov_write_subtitle_end_packet(s, i, trk->track_duration);
  3479. trk->last_sample_is_subtitle_end = 1;
  3480. }
  3481. }
  3482. moov_pos = avio_tell(pb);
  3483. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  3484. /* Write size of mdat tag */
  3485. if (mov->mdat_size + 8 <= UINT32_MAX) {
  3486. avio_seek(pb, mov->mdat_pos, SEEK_SET);
  3487. avio_wb32(pb, mov->mdat_size + 8);
  3488. } else {
  3489. /* overwrite 'wide' placeholder atom */
  3490. avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
  3491. /* special value: real atom size will be 64 bit value after
  3492. * tag field */
  3493. avio_wb32(pb, 1);
  3494. ffio_wfourcc(pb, "mdat");
  3495. avio_wb64(pb, mov->mdat_size + 16);
  3496. }
  3497. avio_seek(pb, mov->reserved_moov_size > 0 ? mov->reserved_moov_pos : moov_pos, SEEK_SET);
  3498. if (mov->reserved_moov_size == -1) {
  3499. av_log(s, AV_LOG_INFO, "Starting second pass: moving header on top of the file\n");
  3500. res = shift_data(s);
  3501. if (res == 0) {
  3502. avio_seek(s->pb, mov->reserved_moov_pos, SEEK_SET);
  3503. mov_write_moov_tag(pb, mov, s);
  3504. }
  3505. } else if (mov->reserved_moov_size > 0) {
  3506. int64_t size;
  3507. mov_write_moov_tag(pb, mov, s);
  3508. size = mov->reserved_moov_size - (avio_tell(pb) - mov->reserved_moov_pos);
  3509. if (size < 8){
  3510. av_log(s, AV_LOG_ERROR, "reserved_moov_size is too small, needed %"PRId64" additional\n", 8-size);
  3511. return -1;
  3512. }
  3513. avio_wb32(pb, size);
  3514. ffio_wfourcc(pb, "free");
  3515. for (i = 0; i < size; i++)
  3516. avio_w8(pb, 0);
  3517. avio_seek(pb, moov_pos, SEEK_SET);
  3518. } else {
  3519. mov_write_moov_tag(pb, mov, s);
  3520. }
  3521. } else {
  3522. mov_flush_fragment(s);
  3523. mov_write_mfra_tag(pb, mov);
  3524. }
  3525. if (mov->chapter_track)
  3526. av_freep(&mov->tracks[mov->chapter_track].enc);
  3527. for (i = 0; i < mov->nb_streams; i++) {
  3528. if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
  3529. ff_mov_close_hinting(&mov->tracks[i]);
  3530. else if (mov->tracks[i].tag == MKTAG('t','m','c','d') && mov->nb_meta_tmcd)
  3531. av_freep(&mov->tracks[i].enc);
  3532. if (mov->flags & FF_MOV_FLAG_FRAGMENT &&
  3533. mov->tracks[i].vc1_info.struct_offset && s->pb->seekable) {
  3534. int64_t off = avio_tell(pb);
  3535. uint8_t buf[7];
  3536. if (mov_write_dvc1_structs(&mov->tracks[i], buf) >= 0) {
  3537. avio_seek(pb, mov->tracks[i].vc1_info.struct_offset, SEEK_SET);
  3538. avio_write(pb, buf, 7);
  3539. avio_seek(pb, off, SEEK_SET);
  3540. }
  3541. }
  3542. av_freep(&mov->tracks[i].cluster);
  3543. av_freep(&mov->tracks[i].frag_info);
  3544. if (mov->tracks[i].vos_len)
  3545. av_free(mov->tracks[i].vos_data);
  3546. }
  3547. av_freep(&mov->tracks);
  3548. return res;
  3549. }
  3550. #if CONFIG_MOV_MUXER
  3551. MOV_CLASS(mov)
  3552. AVOutputFormat ff_mov_muxer = {
  3553. .name = "mov",
  3554. .long_name = NULL_IF_CONFIG_SMALL("QuickTime / MOV"),
  3555. .extensions = "mov",
  3556. .priv_data_size = sizeof(MOVMuxContext),
  3557. .audio_codec = AV_CODEC_ID_AAC,
  3558. .video_codec = CONFIG_LIBX264_ENCODER ?
  3559. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3560. .write_header = mov_write_header,
  3561. .write_packet = mov_write_packet,
  3562. .write_trailer = mov_write_trailer,
  3563. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3564. .codec_tag = (const AVCodecTag* const []){
  3565. ff_codec_movvideo_tags, ff_codec_movaudio_tags, 0
  3566. },
  3567. .priv_class = &mov_muxer_class,
  3568. };
  3569. #endif
  3570. #if CONFIG_TGP_MUXER
  3571. MOV_CLASS(tgp)
  3572. AVOutputFormat ff_tgp_muxer = {
  3573. .name = "3gp",
  3574. .long_name = NULL_IF_CONFIG_SMALL("3GP (3GPP file format)"),
  3575. .extensions = "3gp",
  3576. .priv_data_size = sizeof(MOVMuxContext),
  3577. .audio_codec = AV_CODEC_ID_AMR_NB,
  3578. .video_codec = AV_CODEC_ID_H263,
  3579. .write_header = mov_write_header,
  3580. .write_packet = mov_write_packet,
  3581. .write_trailer = mov_write_trailer,
  3582. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3583. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  3584. .priv_class = &tgp_muxer_class,
  3585. };
  3586. #endif
  3587. #if CONFIG_MP4_MUXER
  3588. MOV_CLASS(mp4)
  3589. AVOutputFormat ff_mp4_muxer = {
  3590. .name = "mp4",
  3591. .long_name = NULL_IF_CONFIG_SMALL("MP4 (MPEG-4 Part 14)"),
  3592. .mime_type = "application/mp4",
  3593. .extensions = "mp4",
  3594. .priv_data_size = sizeof(MOVMuxContext),
  3595. .audio_codec = AV_CODEC_ID_AAC,
  3596. .video_codec = CONFIG_LIBX264_ENCODER ?
  3597. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3598. .write_header = mov_write_header,
  3599. .write_packet = mov_write_packet,
  3600. .write_trailer = mov_write_trailer,
  3601. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3602. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3603. .priv_class = &mp4_muxer_class,
  3604. };
  3605. #endif
  3606. #if CONFIG_PSP_MUXER
  3607. MOV_CLASS(psp)
  3608. AVOutputFormat ff_psp_muxer = {
  3609. .name = "psp",
  3610. .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 (MPEG-4 Part 14)"),
  3611. .extensions = "mp4,psp",
  3612. .priv_data_size = sizeof(MOVMuxContext),
  3613. .audio_codec = AV_CODEC_ID_AAC,
  3614. .video_codec = CONFIG_LIBX264_ENCODER ?
  3615. AV_CODEC_ID_H264 : AV_CODEC_ID_MPEG4,
  3616. .write_header = mov_write_header,
  3617. .write_packet = mov_write_packet,
  3618. .write_trailer = mov_write_trailer,
  3619. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3620. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3621. .priv_class = &psp_muxer_class,
  3622. };
  3623. #endif
  3624. #if CONFIG_TG2_MUXER
  3625. MOV_CLASS(tg2)
  3626. AVOutputFormat ff_tg2_muxer = {
  3627. .name = "3g2",
  3628. .long_name = NULL_IF_CONFIG_SMALL("3GP2 (3GPP2 file format)"),
  3629. .extensions = "3g2",
  3630. .priv_data_size = sizeof(MOVMuxContext),
  3631. .audio_codec = AV_CODEC_ID_AMR_NB,
  3632. .video_codec = AV_CODEC_ID_H263,
  3633. .write_header = mov_write_header,
  3634. .write_packet = mov_write_packet,
  3635. .write_trailer = mov_write_trailer,
  3636. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3637. .codec_tag = (const AVCodecTag* const []){ codec_3gp_tags, 0 },
  3638. .priv_class = &tg2_muxer_class,
  3639. };
  3640. #endif
  3641. #if CONFIG_IPOD_MUXER
  3642. MOV_CLASS(ipod)
  3643. AVOutputFormat ff_ipod_muxer = {
  3644. .name = "ipod",
  3645. .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 (MPEG-4 Part 14)"),
  3646. .mime_type = "application/mp4",
  3647. .extensions = "m4v,m4a",
  3648. .priv_data_size = sizeof(MOVMuxContext),
  3649. .audio_codec = AV_CODEC_ID_AAC,
  3650. .video_codec = AV_CODEC_ID_H264,
  3651. .write_header = mov_write_header,
  3652. .write_packet = mov_write_packet,
  3653. .write_trailer = mov_write_trailer,
  3654. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH | AVFMT_TS_NEGATIVE,
  3655. .codec_tag = (const AVCodecTag* const []){ codec_ipod_tags, 0 },
  3656. .priv_class = &ipod_muxer_class,
  3657. };
  3658. #endif
  3659. #if CONFIG_ISMV_MUXER
  3660. MOV_CLASS(ismv)
  3661. AVOutputFormat ff_ismv_muxer = {
  3662. .name = "ismv",
  3663. .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming)"),
  3664. .mime_type = "application/mp4",
  3665. .extensions = "ismv,isma",
  3666. .priv_data_size = sizeof(MOVMuxContext),
  3667. .audio_codec = AV_CODEC_ID_AAC,
  3668. .video_codec = AV_CODEC_ID_H264,
  3669. .write_header = mov_write_header,
  3670. .write_packet = mov_write_packet,
  3671. .write_trailer = mov_write_trailer,
  3672. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3673. .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
  3674. .priv_class = &ismv_muxer_class,
  3675. };
  3676. #endif
  3677. #if CONFIG_F4V_MUXER
  3678. MOV_CLASS(f4v)
  3679. AVOutputFormat ff_f4v_muxer = {
  3680. .name = "f4v",
  3681. .long_name = NULL_IF_CONFIG_SMALL("F4V Adobe Flash Video"),
  3682. .mime_type = "application/f4v",
  3683. .extensions = "f4v",
  3684. .priv_data_size = sizeof(MOVMuxContext),
  3685. .audio_codec = AV_CODEC_ID_AAC,
  3686. .video_codec = AV_CODEC_ID_H264,
  3687. .write_header = mov_write_header,
  3688. .write_packet = mov_write_packet,
  3689. .write_trailer = mov_write_trailer,
  3690. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  3691. .codec_tag = (const AVCodecTag* const []){ codec_f4v_tags, 0 },
  3692. .priv_class = &f4v_muxer_class,
  3693. };
  3694. #endif