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.

3185 lines
109KB

  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 Libav.
  8. *
  9. * Libav 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. * Libav 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 Libav; 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 "internal.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/intfloat.h"
  35. #include "libavutil/mathematics.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/dict.h"
  38. #include "rtpenc.h"
  39. #include "mov_chan.h"
  40. #undef NDEBUG
  41. #include <assert.h>
  42. static const AVOption options[] = {
  43. { "movflags", "MOV muxer flags", offsetof(MOVMuxContext, flags), AV_OPT_TYPE_FLAGS, {.dbl = 0}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  44. { "rtphint", "Add RTP hint tracks", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_RTP_HINT}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  45. { "empty_moov", "Make the initial moov atom empty (not supported by QuickTime)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_EMPTY_MOOV}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  46. { "frag_keyframe", "Fragment at video keyframes", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_KEYFRAME}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  47. { "separate_moof", "Write separate moof/mdat atoms for each track", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_SEPARATE_MOOF}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  48. { "frag_custom", "Flush fragments on caller requests", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_FRAG_CUSTOM}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  49. { "isml", "Create a live smooth streaming feed (for pushing to a publishing point)", 0, AV_OPT_TYPE_CONST, {.dbl = FF_MOV_FLAG_ISML}, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "movflags" },
  50. FF_RTP_FLAG_OPTS(MOVMuxContext, rtp_flags),
  51. { "skip_iods", "Skip writing iods atom.", offsetof(MOVMuxContext, iods_skip), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_ENCODING_PARAM},
  52. { "iods_audio_profile", "iods audio profile atom.", offsetof(MOVMuxContext, iods_audio_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
  53. { "iods_video_profile", "iods video profile atom.", offsetof(MOVMuxContext, iods_video_profile), AV_OPT_TYPE_INT, {.dbl = -1}, -1, 255, AV_OPT_FLAG_ENCODING_PARAM},
  54. { "frag_duration", "Maximum fragment duration", offsetof(MOVMuxContext, max_fragment_duration), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  55. { "frag_size", "Maximum fragment size", offsetof(MOVMuxContext, max_fragment_size), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  56. { "ism_lookahead", "Number of lookahead entries for ISM files", offsetof(MOVMuxContext, ism_lookahead), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM},
  57. { NULL },
  58. };
  59. #define MOV_CLASS(flavor)\
  60. static const AVClass flavor ## _muxer_class = {\
  61. .class_name = #flavor " muxer",\
  62. .item_name = av_default_item_name,\
  63. .option = options,\
  64. .version = LIBAVUTIL_VERSION_INT,\
  65. };
  66. //FIXME support 64 bit variant with wide placeholders
  67. static int64_t update_size(AVIOContext *pb, int64_t pos)
  68. {
  69. int64_t curpos = avio_tell(pb);
  70. avio_seek(pb, pos, SEEK_SET);
  71. avio_wb32(pb, curpos - pos); /* rewrite size */
  72. avio_seek(pb, curpos, SEEK_SET);
  73. return curpos - pos;
  74. }
  75. /* Chunk offset atom */
  76. static int mov_write_stco_tag(AVIOContext *pb, MOVTrack *track)
  77. {
  78. int i;
  79. int mode64 = 0; // use 32 bit size variant if possible
  80. int64_t pos = avio_tell(pb);
  81. avio_wb32(pb, 0); /* size */
  82. if (pos > UINT32_MAX) {
  83. mode64 = 1;
  84. ffio_wfourcc(pb, "co64");
  85. } else
  86. ffio_wfourcc(pb, "stco");
  87. avio_wb32(pb, 0); /* version & flags */
  88. avio_wb32(pb, track->entry); /* entry count */
  89. for (i=0; i<track->entry; i++) {
  90. if(mode64 == 1)
  91. avio_wb64(pb, track->cluster[i].pos);
  92. else
  93. avio_wb32(pb, track->cluster[i].pos);
  94. }
  95. return update_size(pb, pos);
  96. }
  97. /* Sample size atom */
  98. static int mov_write_stsz_tag(AVIOContext *pb, MOVTrack *track)
  99. {
  100. int equalChunks = 1;
  101. int i, j, entries = 0, tst = -1, oldtst = -1;
  102. int64_t pos = avio_tell(pb);
  103. avio_wb32(pb, 0); /* size */
  104. ffio_wfourcc(pb, "stsz");
  105. avio_wb32(pb, 0); /* version & flags */
  106. for (i=0; i<track->entry; i++) {
  107. tst = track->cluster[i].size/track->cluster[i].entries;
  108. if(oldtst != -1 && tst != oldtst) {
  109. equalChunks = 0;
  110. }
  111. oldtst = tst;
  112. entries += track->cluster[i].entries;
  113. }
  114. if (equalChunks && track->entry) {
  115. int sSize = track->entry ? track->cluster[0].size/track->cluster[0].entries : 0;
  116. sSize = FFMAX(1, sSize); // adpcm mono case could make sSize == 0
  117. avio_wb32(pb, sSize); // sample size
  118. avio_wb32(pb, entries); // sample count
  119. }
  120. else {
  121. avio_wb32(pb, 0); // sample size
  122. avio_wb32(pb, entries); // sample count
  123. for (i=0; i<track->entry; i++) {
  124. for (j=0; j<track->cluster[i].entries; j++) {
  125. avio_wb32(pb, track->cluster[i].size /
  126. track->cluster[i].entries);
  127. }
  128. }
  129. }
  130. return update_size(pb, pos);
  131. }
  132. /* Sample to chunk atom */
  133. static int mov_write_stsc_tag(AVIOContext *pb, MOVTrack *track)
  134. {
  135. int index = 0, oldval = -1, i;
  136. int64_t entryPos, curpos;
  137. int64_t pos = avio_tell(pb);
  138. avio_wb32(pb, 0); /* size */
  139. ffio_wfourcc(pb, "stsc");
  140. avio_wb32(pb, 0); // version & flags
  141. entryPos = avio_tell(pb);
  142. avio_wb32(pb, track->entry); // entry count
  143. for (i=0; i<track->entry; i++) {
  144. if (oldval != track->cluster[i].samples_in_chunk)
  145. {
  146. avio_wb32(pb, i+1); // first chunk
  147. avio_wb32(pb, track->cluster[i].samples_in_chunk); // samples per chunk
  148. avio_wb32(pb, 0x1); // sample description index
  149. oldval = track->cluster[i].samples_in_chunk;
  150. index++;
  151. }
  152. }
  153. curpos = avio_tell(pb);
  154. avio_seek(pb, entryPos, SEEK_SET);
  155. avio_wb32(pb, index); // rewrite size
  156. avio_seek(pb, curpos, SEEK_SET);
  157. return update_size(pb, pos);
  158. }
  159. /* Sync sample atom */
  160. static int mov_write_stss_tag(AVIOContext *pb, MOVTrack *track, uint32_t flag)
  161. {
  162. int64_t curpos, entryPos;
  163. int i, index = 0;
  164. int64_t pos = avio_tell(pb);
  165. avio_wb32(pb, 0); // size
  166. ffio_wfourcc(pb, flag == MOV_SYNC_SAMPLE ? "stss" : "stps");
  167. avio_wb32(pb, 0); // version & flags
  168. entryPos = avio_tell(pb);
  169. avio_wb32(pb, track->entry); // entry count
  170. for (i=0; i<track->entry; i++) {
  171. if (track->cluster[i].flags & flag) {
  172. avio_wb32(pb, i+1);
  173. index++;
  174. }
  175. }
  176. curpos = avio_tell(pb);
  177. avio_seek(pb, entryPos, SEEK_SET);
  178. avio_wb32(pb, index); // rewrite size
  179. avio_seek(pb, curpos, SEEK_SET);
  180. return update_size(pb, pos);
  181. }
  182. static int mov_write_amr_tag(AVIOContext *pb, MOVTrack *track)
  183. {
  184. avio_wb32(pb, 0x11); /* size */
  185. if (track->mode == MODE_MOV) ffio_wfourcc(pb, "samr");
  186. else ffio_wfourcc(pb, "damr");
  187. ffio_wfourcc(pb, "FFMP");
  188. avio_w8(pb, 0); /* decoder version */
  189. avio_wb16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  190. avio_w8(pb, 0x00); /* Mode change period (no restriction) */
  191. avio_w8(pb, 0x01); /* Frames per sample */
  192. return 0x11;
  193. }
  194. static int mov_write_ac3_tag(AVIOContext *pb, MOVTrack *track)
  195. {
  196. GetBitContext gbc;
  197. PutBitContext pbc;
  198. uint8_t buf[3];
  199. int fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
  200. if (track->vos_len < 7)
  201. return -1;
  202. avio_wb32(pb, 11);
  203. ffio_wfourcc(pb, "dac3");
  204. init_get_bits(&gbc, track->vos_data + 4, (track->vos_len - 4) * 8);
  205. fscod = get_bits(&gbc, 2);
  206. frmsizecod = get_bits(&gbc, 6);
  207. bsid = get_bits(&gbc, 5);
  208. bsmod = get_bits(&gbc, 3);
  209. acmod = get_bits(&gbc, 3);
  210. if (acmod == 2) {
  211. skip_bits(&gbc, 2); // dsurmod
  212. } else {
  213. if ((acmod & 1) && acmod != 1)
  214. skip_bits(&gbc, 2); // cmixlev
  215. if (acmod & 4)
  216. skip_bits(&gbc, 2); // surmixlev
  217. }
  218. lfeon = get_bits1(&gbc);
  219. init_put_bits(&pbc, buf, sizeof(buf));
  220. put_bits(&pbc, 2, fscod);
  221. put_bits(&pbc, 5, bsid);
  222. put_bits(&pbc, 3, bsmod);
  223. put_bits(&pbc, 3, acmod);
  224. put_bits(&pbc, 1, lfeon);
  225. put_bits(&pbc, 5, frmsizecod>>1); // bit_rate_code
  226. put_bits(&pbc, 5, 0); // reserved
  227. flush_put_bits(&pbc);
  228. avio_write(pb, buf, sizeof(buf));
  229. return 11;
  230. }
  231. /**
  232. * This function writes extradata "as is".
  233. * Extradata must be formatted like a valid atom (with size and tag).
  234. */
  235. static int mov_write_extradata_tag(AVIOContext *pb, MOVTrack *track)
  236. {
  237. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  238. return track->enc->extradata_size;
  239. }
  240. static int mov_write_enda_tag(AVIOContext *pb)
  241. {
  242. avio_wb32(pb, 10);
  243. ffio_wfourcc(pb, "enda");
  244. avio_wb16(pb, 1); /* little endian */
  245. return 10;
  246. }
  247. static void put_descr(AVIOContext *pb, int tag, unsigned int size)
  248. {
  249. int i = 3;
  250. avio_w8(pb, tag);
  251. for(; i>0; i--)
  252. avio_w8(pb, (size>>(7*i)) | 0x80);
  253. avio_w8(pb, size & 0x7F);
  254. }
  255. static int mov_write_esds_tag(AVIOContext *pb, MOVTrack *track) // Basic
  256. {
  257. int64_t pos = avio_tell(pb);
  258. int decoder_specific_info_len = track->vos_len ? 5 + track->vos_len : 0;
  259. avio_wb32(pb, 0); // size
  260. ffio_wfourcc(pb, "esds");
  261. avio_wb32(pb, 0); // Version
  262. // ES descriptor
  263. put_descr(pb, 0x03, 3 + 5+13 + decoder_specific_info_len + 5+1);
  264. avio_wb16(pb, track->track_id);
  265. avio_w8(pb, 0x00); // flags (= no flags)
  266. // DecoderConfig descriptor
  267. put_descr(pb, 0x04, 13 + decoder_specific_info_len);
  268. // Object type indication
  269. if ((track->enc->codec_id == CODEC_ID_MP2 ||
  270. track->enc->codec_id == CODEC_ID_MP3) &&
  271. track->enc->sample_rate > 24000)
  272. avio_w8(pb, 0x6B); // 11172-3
  273. else
  274. avio_w8(pb, ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
  275. // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
  276. // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
  277. if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  278. avio_w8(pb, 0x15); // flags (= Audiostream)
  279. else
  280. avio_w8(pb, 0x11); // flags (= Visualstream)
  281. avio_w8(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
  282. avio_wb16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
  283. avio_wb32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
  284. if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
  285. avio_wb32(pb, 0); // vbr
  286. else
  287. avio_wb32(pb, track->enc->rc_max_rate); // avg bitrate
  288. if (track->vos_len) {
  289. // DecoderSpecific info descriptor
  290. put_descr(pb, 0x05, track->vos_len);
  291. avio_write(pb, track->vos_data, track->vos_len);
  292. }
  293. // SL descriptor
  294. put_descr(pb, 0x06, 1);
  295. avio_w8(pb, 0x02);
  296. return update_size(pb, pos);
  297. }
  298. static int mov_pcm_le_gt16(enum CodecID codec_id)
  299. {
  300. return codec_id == CODEC_ID_PCM_S24LE ||
  301. codec_id == CODEC_ID_PCM_S32LE ||
  302. codec_id == CODEC_ID_PCM_F32LE ||
  303. codec_id == CODEC_ID_PCM_F64LE;
  304. }
  305. static int mov_write_ms_tag(AVIOContext *pb, MOVTrack *track)
  306. {
  307. int64_t pos = avio_tell(pb);
  308. avio_wb32(pb, 0);
  309. avio_wl32(pb, track->tag); // store it byteswapped
  310. track->enc->codec_tag = av_bswap16(track->tag >> 16);
  311. ff_put_wav_header(pb, track->enc);
  312. return update_size(pb, pos);
  313. }
  314. static int mov_write_wfex_tag(AVIOContext *pb, MOVTrack *track)
  315. {
  316. int64_t pos = avio_tell(pb);
  317. avio_wb32(pb, 0);
  318. ffio_wfourcc(pb, "wfex");
  319. ff_put_wav_header(pb, track->enc);
  320. return update_size(pb, pos);
  321. }
  322. static int mov_write_chan_tag(AVIOContext *pb, MOVTrack *track)
  323. {
  324. uint32_t layout_tag, bitmap;
  325. int64_t pos = avio_tell(pb);
  326. layout_tag = ff_mov_get_channel_layout_tag(track->enc->codec_id,
  327. track->enc->channel_layout,
  328. &bitmap);
  329. if (!layout_tag) {
  330. av_log(track->enc, AV_LOG_WARNING, "not writing 'chan' tag due to "
  331. "lack of channel information\n");
  332. return 0;
  333. }
  334. avio_wb32(pb, 0); // Size
  335. ffio_wfourcc(pb, "chan"); // Type
  336. avio_w8(pb, 0); // Version
  337. avio_wb24(pb, 0); // Flags
  338. avio_wb32(pb, layout_tag); // mChannelLayoutTag
  339. avio_wb32(pb, bitmap); // mChannelBitmap
  340. avio_wb32(pb, 0); // mNumberChannelDescriptions
  341. return update_size(pb, pos);
  342. }
  343. static int mov_write_wave_tag(AVIOContext *pb, MOVTrack *track)
  344. {
  345. int64_t pos = avio_tell(pb);
  346. avio_wb32(pb, 0); /* size */
  347. ffio_wfourcc(pb, "wave");
  348. avio_wb32(pb, 12); /* size */
  349. ffio_wfourcc(pb, "frma");
  350. avio_wl32(pb, track->tag);
  351. if (track->enc->codec_id == CODEC_ID_AAC) {
  352. /* useless atom needed by mplayer, ipod, not needed by quicktime */
  353. avio_wb32(pb, 12); /* size */
  354. ffio_wfourcc(pb, "mp4a");
  355. avio_wb32(pb, 0);
  356. mov_write_esds_tag(pb, track);
  357. } else if (mov_pcm_le_gt16(track->enc->codec_id)) {
  358. mov_write_enda_tag(pb);
  359. } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
  360. mov_write_amr_tag(pb, track);
  361. } else if (track->enc->codec_id == CODEC_ID_AC3) {
  362. mov_write_chan_tag(pb, track);
  363. mov_write_ac3_tag(pb, track);
  364. } else if (track->enc->codec_id == CODEC_ID_ALAC) {
  365. mov_write_extradata_tag(pb, track);
  366. } else if (track->enc->codec_id == CODEC_ID_ADPCM_MS ||
  367. track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV) {
  368. mov_write_ms_tag(pb, track);
  369. }
  370. avio_wb32(pb, 8); /* size */
  371. avio_wb32(pb, 0); /* null tag */
  372. return update_size(pb, pos);
  373. }
  374. static int mov_write_glbl_tag(AVIOContext *pb, MOVTrack *track)
  375. {
  376. avio_wb32(pb, track->vos_len + 8);
  377. ffio_wfourcc(pb, "glbl");
  378. avio_write(pb, track->vos_data, track->vos_len);
  379. return 8 + track->vos_len;
  380. }
  381. /**
  382. * Compute flags for 'lpcm' tag.
  383. * See CoreAudioTypes and AudioStreamBasicDescription at Apple.
  384. */
  385. static int mov_get_lpcm_flags(enum CodecID codec_id)
  386. {
  387. switch (codec_id) {
  388. case CODEC_ID_PCM_F32BE:
  389. case CODEC_ID_PCM_F64BE:
  390. return 11;
  391. case CODEC_ID_PCM_F32LE:
  392. case CODEC_ID_PCM_F64LE:
  393. return 9;
  394. case CODEC_ID_PCM_U8:
  395. return 10;
  396. case CODEC_ID_PCM_S16BE:
  397. case CODEC_ID_PCM_S24BE:
  398. case CODEC_ID_PCM_S32BE:
  399. return 14;
  400. case CODEC_ID_PCM_S8:
  401. case CODEC_ID_PCM_S16LE:
  402. case CODEC_ID_PCM_S24LE:
  403. case CODEC_ID_PCM_S32LE:
  404. return 12;
  405. default:
  406. return 0;
  407. }
  408. }
  409. static int mov_write_audio_tag(AVIOContext *pb, MOVTrack *track)
  410. {
  411. int64_t pos = avio_tell(pb);
  412. int version = 0;
  413. uint32_t tag = track->tag;
  414. if (track->mode == MODE_MOV) {
  415. if (mov_get_lpcm_flags(track->enc->codec_id))
  416. tag = AV_RL32("lpcm");
  417. version = 2;
  418. }
  419. avio_wb32(pb, 0); /* size */
  420. avio_wl32(pb, tag); // store it byteswapped
  421. avio_wb32(pb, 0); /* Reserved */
  422. avio_wb16(pb, 0); /* Reserved */
  423. avio_wb16(pb, 1); /* Data-reference index, XXX == 1 */
  424. /* SoundDescription */
  425. avio_wb16(pb, version); /* Version */
  426. avio_wb16(pb, 0); /* Revision level */
  427. avio_wb32(pb, 0); /* Reserved */
  428. if (version == 2) {
  429. avio_wb16(pb, 3);
  430. avio_wb16(pb, 16);
  431. avio_wb16(pb, 0xfffe);
  432. avio_wb16(pb, 0);
  433. avio_wb32(pb, 0x00010000);
  434. avio_wb32(pb, 72);
  435. avio_wb64(pb, av_double2int(track->enc->sample_rate));
  436. avio_wb32(pb, track->enc->channels);
  437. avio_wb32(pb, 0x7F000000);
  438. avio_wb32(pb, av_get_bits_per_sample(track->enc->codec_id));
  439. avio_wb32(pb, mov_get_lpcm_flags(track->enc->codec_id));
  440. avio_wb32(pb, track->sample_size);
  441. avio_wb32(pb, track->audio_vbr ? track->enc->frame_size : 1);
  442. } else {
  443. /* reserved for mp4/3gp */
  444. avio_wb16(pb, 2);
  445. avio_wb16(pb, 16);
  446. avio_wb16(pb, 0);
  447. avio_wb16(pb, 0); /* packet size (= 0) */
  448. avio_wb16(pb, track->enc->sample_rate);
  449. avio_wb16(pb, 0); /* Reserved */
  450. }
  451. if(track->mode == MODE_MOV &&
  452. (track->enc->codec_id == CODEC_ID_AAC ||
  453. track->enc->codec_id == CODEC_ID_AC3 ||
  454. track->enc->codec_id == CODEC_ID_AMR_NB ||
  455. track->enc->codec_id == CODEC_ID_ALAC ||
  456. track->enc->codec_id == CODEC_ID_ADPCM_MS ||
  457. track->enc->codec_id == CODEC_ID_ADPCM_IMA_WAV ||
  458. mov_pcm_le_gt16(track->enc->codec_id)))
  459. mov_write_wave_tag(pb, track);
  460. else if(track->tag == MKTAG('m','p','4','a'))
  461. mov_write_esds_tag(pb, track);
  462. else if(track->enc->codec_id == CODEC_ID_AMR_NB)
  463. mov_write_amr_tag(pb, track);
  464. else if(track->enc->codec_id == CODEC_ID_AC3)
  465. mov_write_ac3_tag(pb, track);
  466. else if(track->enc->codec_id == CODEC_ID_ALAC)
  467. mov_write_extradata_tag(pb, track);
  468. else if (track->enc->codec_id == CODEC_ID_WMAPRO)
  469. mov_write_wfex_tag(pb, track);
  470. else if (track->vos_len > 0)
  471. mov_write_glbl_tag(pb, track);
  472. return update_size(pb, pos);
  473. }
  474. static int mov_write_d263_tag(AVIOContext *pb)
  475. {
  476. avio_wb32(pb, 0xf); /* size */
  477. ffio_wfourcc(pb, "d263");
  478. ffio_wfourcc(pb, "FFMP");
  479. avio_w8(pb, 0); /* decoder version */
  480. /* FIXME use AVCodecContext level/profile, when encoder will set values */
  481. avio_w8(pb, 0xa); /* level */
  482. avio_w8(pb, 0); /* profile */
  483. return 0xf;
  484. }
  485. /* TODO: No idea about these values */
  486. static int mov_write_svq3_tag(AVIOContext *pb)
  487. {
  488. avio_wb32(pb, 0x15);
  489. ffio_wfourcc(pb, "SMI ");
  490. ffio_wfourcc(pb, "SEQH");
  491. avio_wb32(pb, 0x5);
  492. avio_wb32(pb, 0xe2c0211d);
  493. avio_wb32(pb, 0xc0000000);
  494. avio_w8(pb, 0);
  495. return 0x15;
  496. }
  497. static int mov_write_avcc_tag(AVIOContext *pb, MOVTrack *track)
  498. {
  499. int64_t pos = avio_tell(pb);
  500. avio_wb32(pb, 0);
  501. ffio_wfourcc(pb, "avcC");
  502. ff_isom_write_avcc(pb, track->vos_data, track->vos_len);
  503. return update_size(pb, pos);
  504. }
  505. /* also used by all avid codecs (dv, imx, meridien) and their variants */
  506. static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
  507. {
  508. int i;
  509. avio_wb32(pb, 24); /* size */
  510. ffio_wfourcc(pb, "ACLR");
  511. ffio_wfourcc(pb, "ACLR");
  512. ffio_wfourcc(pb, "0001");
  513. avio_wb32(pb, 2); /* yuv range: full 1 / normal 2 */
  514. avio_wb32(pb, 0); /* unknown */
  515. avio_wb32(pb, 24); /* size */
  516. ffio_wfourcc(pb, "APRG");
  517. ffio_wfourcc(pb, "APRG");
  518. ffio_wfourcc(pb, "0001");
  519. avio_wb32(pb, 1); /* unknown */
  520. avio_wb32(pb, 0); /* unknown */
  521. avio_wb32(pb, 120); /* size */
  522. ffio_wfourcc(pb, "ARES");
  523. ffio_wfourcc(pb, "ARES");
  524. ffio_wfourcc(pb, "0001");
  525. avio_wb32(pb, AV_RB32(track->vos_data + 0x28)); /* dnxhd cid, some id ? */
  526. avio_wb32(pb, track->enc->width);
  527. /* values below are based on samples created with quicktime and avid codecs */
  528. if (track->vos_data[5] & 2) { // interlaced
  529. avio_wb32(pb, track->enc->height/2);
  530. avio_wb32(pb, 2); /* unknown */
  531. avio_wb32(pb, 0); /* unknown */
  532. avio_wb32(pb, 4); /* unknown */
  533. } else {
  534. avio_wb32(pb, track->enc->height);
  535. avio_wb32(pb, 1); /* unknown */
  536. avio_wb32(pb, 0); /* unknown */
  537. if (track->enc->height == 1080)
  538. avio_wb32(pb, 5); /* unknown */
  539. else
  540. avio_wb32(pb, 6); /* unknown */
  541. }
  542. /* padding */
  543. for (i = 0; i < 10; i++)
  544. avio_wb64(pb, 0);
  545. /* extra padding for stsd needed */
  546. avio_wb32(pb, 0);
  547. return 0;
  548. }
  549. static int mp4_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  550. {
  551. int tag = track->enc->codec_tag;
  552. if (!ff_codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
  553. return 0;
  554. if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
  555. else if (track->enc->codec_id == CODEC_ID_AC3) tag = MKTAG('a','c','-','3');
  556. else if (track->enc->codec_id == CODEC_ID_DIRAC) tag = MKTAG('d','r','a','c');
  557. else if (track->enc->codec_id == CODEC_ID_MOV_TEXT) tag = MKTAG('t','x','3','g');
  558. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
  559. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
  560. return tag;
  561. }
  562. static const AVCodecTag codec_ipod_tags[] = {
  563. { CODEC_ID_H264, MKTAG('a','v','c','1') },
  564. { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  565. { CODEC_ID_AAC, MKTAG('m','p','4','a') },
  566. { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
  567. { CODEC_ID_AC3, MKTAG('a','c','-','3') },
  568. { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  569. { CODEC_ID_MOV_TEXT, MKTAG('t','e','x','t') },
  570. { CODEC_ID_NONE, 0 },
  571. };
  572. static int ipod_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  573. {
  574. int tag = track->enc->codec_tag;
  575. // keep original tag for subs, ipod supports both formats
  576. if (!(track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE &&
  577. (tag == MKTAG('t','x','3','g') ||
  578. tag == MKTAG('t','e','x','t'))))
  579. tag = ff_codec_get_tag(codec_ipod_tags, track->enc->codec_id);
  580. if (!av_match_ext(s->filename, "m4a") && !av_match_ext(s->filename, "m4v"))
  581. av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
  582. "Quicktime/Ipod might not play the file\n");
  583. return tag;
  584. }
  585. static int mov_get_dv_codec_tag(AVFormatContext *s, MOVTrack *track)
  586. {
  587. int tag;
  588. if (track->enc->width == 720) /* SD */
  589. if (track->enc->height == 480) /* NTSC */
  590. if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
  591. else tag = MKTAG('d','v','c',' ');
  592. else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
  593. else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
  594. else tag = MKTAG('d','v','p','p');
  595. else if (track->enc->height == 720) /* HD 720 line */
  596. if (track->enc->time_base.den == 50) tag = MKTAG('d','v','h','q');
  597. else tag = MKTAG('d','v','h','p');
  598. else if (track->enc->height == 1080) /* HD 1080 line */
  599. if (track->enc->time_base.den == 25) tag = MKTAG('d','v','h','5');
  600. else tag = MKTAG('d','v','h','6');
  601. else {
  602. av_log(s, AV_LOG_ERROR, "unsupported height for dv codec\n");
  603. return 0;
  604. }
  605. return tag;
  606. }
  607. static const struct {
  608. enum PixelFormat pix_fmt;
  609. uint32_t tag;
  610. unsigned bps;
  611. } mov_pix_fmt_tags[] = {
  612. { PIX_FMT_YUYV422, MKTAG('y','u','v','s'), 0 },
  613. { PIX_FMT_UYVY422, MKTAG('2','v','u','y'), 0 },
  614. { PIX_FMT_RGB555BE,MKTAG('r','a','w',' '), 16 },
  615. { PIX_FMT_RGB555LE,MKTAG('L','5','5','5'), 16 },
  616. { PIX_FMT_RGB565LE,MKTAG('L','5','6','5'), 16 },
  617. { PIX_FMT_RGB565BE,MKTAG('B','5','6','5'), 16 },
  618. { PIX_FMT_GRAY16BE,MKTAG('b','1','6','g'), 16 },
  619. { PIX_FMT_RGB24, MKTAG('r','a','w',' '), 24 },
  620. { PIX_FMT_BGR24, MKTAG('2','4','B','G'), 24 },
  621. { PIX_FMT_ARGB, MKTAG('r','a','w',' '), 32 },
  622. { PIX_FMT_BGRA, MKTAG('B','G','R','A'), 32 },
  623. { PIX_FMT_RGBA, MKTAG('R','G','B','A'), 32 },
  624. { PIX_FMT_ABGR, MKTAG('A','B','G','R'), 32 },
  625. { PIX_FMT_RGB48BE, MKTAG('b','4','8','r'), 48 },
  626. };
  627. static int mov_get_rawvideo_codec_tag(AVFormatContext *s, MOVTrack *track)
  628. {
  629. int tag = track->enc->codec_tag;
  630. int i;
  631. for (i = 0; i < FF_ARRAY_ELEMS(mov_pix_fmt_tags); i++) {
  632. if (track->enc->pix_fmt == mov_pix_fmt_tags[i].pix_fmt) {
  633. tag = mov_pix_fmt_tags[i].tag;
  634. track->enc->bits_per_coded_sample = mov_pix_fmt_tags[i].bps;
  635. break;
  636. }
  637. }
  638. return tag;
  639. }
  640. static int mov_get_codec_tag(AVFormatContext *s, MOVTrack *track)
  641. {
  642. int tag = track->enc->codec_tag;
  643. if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
  644. (track->enc->codec_id == CODEC_ID_DVVIDEO ||
  645. track->enc->codec_id == CODEC_ID_RAWVIDEO ||
  646. track->enc->codec_id == CODEC_ID_H263 ||
  647. av_get_bits_per_sample(track->enc->codec_id)))) { // pcm audio
  648. if (track->enc->codec_id == CODEC_ID_DVVIDEO)
  649. tag = mov_get_dv_codec_tag(s, track);
  650. else if (track->enc->codec_id == CODEC_ID_RAWVIDEO)
  651. tag = mov_get_rawvideo_codec_tag(s, track);
  652. else if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  653. tag = ff_codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
  654. if (!tag) { // if no mac fcc found, try with Microsoft tags
  655. tag = ff_codec_get_tag(ff_codec_bmp_tags, track->enc->codec_id);
  656. if (tag)
  657. av_log(s, AV_LOG_WARNING, "Using MS style video codec tag, "
  658. "the file may be unplayable!\n");
  659. }
  660. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  661. tag = ff_codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
  662. if (!tag) { // if no mac fcc found, try with Microsoft tags
  663. int ms_tag = ff_codec_get_tag(ff_codec_wav_tags, track->enc->codec_id);
  664. if (ms_tag) {
  665. tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
  666. av_log(s, AV_LOG_WARNING, "Using MS style audio codec tag, "
  667. "the file may be unplayable!\n");
  668. }
  669. }
  670. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  671. tag = ff_codec_get_tag(ff_codec_movsubtitle_tags, track->enc->codec_id);
  672. }
  673. return tag;
  674. }
  675. static const AVCodecTag codec_3gp_tags[] = {
  676. { CODEC_ID_H263, MKTAG('s','2','6','3') },
  677. { CODEC_ID_H264, MKTAG('a','v','c','1') },
  678. { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  679. { CODEC_ID_AAC, MKTAG('m','p','4','a') },
  680. { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
  681. { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
  682. { CODEC_ID_MOV_TEXT, MKTAG('t','x','3','g') },
  683. { CODEC_ID_NONE, 0 },
  684. };
  685. static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
  686. {
  687. int tag = track->enc->codec_tag;
  688. if (track->mode == MODE_MP4 || track->mode == MODE_PSP)
  689. tag = mp4_get_codec_tag(s, track);
  690. else if (track->mode == MODE_ISM) {
  691. tag = mp4_get_codec_tag(s, track);
  692. if (!tag && track->enc->codec_id == CODEC_ID_WMAPRO)
  693. tag = MKTAG('w', 'm', 'a', ' ');
  694. } else if (track->mode == MODE_IPOD)
  695. tag = ipod_get_codec_tag(s, track);
  696. else if (track->mode & MODE_3GP)
  697. tag = ff_codec_get_tag(codec_3gp_tags, track->enc->codec_id);
  698. else
  699. tag = mov_get_codec_tag(s, track);
  700. return tag;
  701. }
  702. /** Write uuid atom.
  703. * Needed to make file play in iPods running newest firmware
  704. * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
  705. */
  706. static int mov_write_uuid_tag_ipod(AVIOContext *pb)
  707. {
  708. avio_wb32(pb, 28);
  709. ffio_wfourcc(pb, "uuid");
  710. avio_wb32(pb, 0x6b6840f2);
  711. avio_wb32(pb, 0x5f244fc5);
  712. avio_wb32(pb, 0xba39a51b);
  713. avio_wb32(pb, 0xcf0323f3);
  714. avio_wb32(pb, 0x0);
  715. return 28;
  716. }
  717. static const uint16_t fiel_data[] = {
  718. 0x0000, 0x0100, 0x0201, 0x0206, 0x0209, 0x020e
  719. };
  720. static int mov_write_fiel_tag(AVIOContext *pb, MOVTrack *track)
  721. {
  722. unsigned mov_field_order = 0;
  723. if (track->enc->field_order < FF_ARRAY_ELEMS(fiel_data))
  724. mov_field_order = fiel_data[track->enc->field_order];
  725. else
  726. return 0;
  727. avio_wb32(pb, 10);
  728. ffio_wfourcc(pb, "fiel");
  729. avio_wb16(pb, mov_field_order);
  730. return 10;
  731. }
  732. static int mov_write_subtitle_tag(AVIOContext *pb, MOVTrack *track)
  733. {
  734. int64_t pos = avio_tell(pb);
  735. avio_wb32(pb, 0); /* size */
  736. avio_wl32(pb, track->tag); // store it byteswapped
  737. avio_wb32(pb, 0); /* Reserved */
  738. avio_wb16(pb, 0); /* Reserved */
  739. avio_wb16(pb, 1); /* Data-reference index */
  740. if (track->enc->extradata_size)
  741. avio_write(pb, track->enc->extradata, track->enc->extradata_size);
  742. return update_size(pb, pos);
  743. }
  744. static int mov_write_pasp_tag(AVIOContext *pb, MOVTrack *track)
  745. {
  746. AVRational sar;
  747. av_reduce(&sar.num, &sar.den, track->enc->sample_aspect_ratio.num,
  748. track->enc->sample_aspect_ratio.den, INT_MAX);
  749. avio_wb32(pb, 16);
  750. ffio_wfourcc(pb, "pasp");
  751. avio_wb32(pb, sar.num);
  752. avio_wb32(pb, sar.den);
  753. return 16;
  754. }
  755. static int mov_write_video_tag(AVIOContext *pb, MOVTrack *track)
  756. {
  757. int64_t pos = avio_tell(pb);
  758. char compressor_name[32];
  759. avio_wb32(pb, 0); /* size */
  760. avio_wl32(pb, track->tag); // store it byteswapped
  761. avio_wb32(pb, 0); /* Reserved */
  762. avio_wb16(pb, 0); /* Reserved */
  763. avio_wb16(pb, 1); /* Data-reference index */
  764. avio_wb16(pb, 0); /* Codec stream version */
  765. avio_wb16(pb, 0); /* Codec stream revision (=0) */
  766. if (track->mode == MODE_MOV) {
  767. ffio_wfourcc(pb, "FFMP"); /* Vendor */
  768. if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
  769. avio_wb32(pb, 0); /* Temporal Quality */
  770. avio_wb32(pb, 0x400); /* Spatial Quality = lossless*/
  771. } else {
  772. avio_wb32(pb, 0x200); /* Temporal Quality = normal */
  773. avio_wb32(pb, 0x200); /* Spatial Quality = normal */
  774. }
  775. } else {
  776. avio_wb32(pb, 0); /* Reserved */
  777. avio_wb32(pb, 0); /* Reserved */
  778. avio_wb32(pb, 0); /* Reserved */
  779. }
  780. avio_wb16(pb, track->enc->width); /* Video width */
  781. avio_wb16(pb, track->height); /* Video height */
  782. avio_wb32(pb, 0x00480000); /* Horizontal resolution 72dpi */
  783. avio_wb32(pb, 0x00480000); /* Vertical resolution 72dpi */
  784. avio_wb32(pb, 0); /* Data size (= 0) */
  785. avio_wb16(pb, 1); /* Frame count (= 1) */
  786. memset(compressor_name,0,32);
  787. /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
  788. if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
  789. av_strlcpy(compressor_name,track->enc->codec->name,32);
  790. avio_w8(pb, strlen(compressor_name));
  791. avio_write(pb, compressor_name, 31);
  792. if (track->mode == MODE_MOV && track->enc->bits_per_coded_sample)
  793. avio_wb16(pb, track->enc->bits_per_coded_sample);
  794. else
  795. avio_wb16(pb, 0x18); /* Reserved */
  796. avio_wb16(pb, 0xffff); /* Reserved */
  797. if(track->tag == MKTAG('m','p','4','v'))
  798. mov_write_esds_tag(pb, track);
  799. else if(track->enc->codec_id == CODEC_ID_H263)
  800. mov_write_d263_tag(pb);
  801. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  802. mov_write_svq3_tag(pb);
  803. else if(track->enc->codec_id == CODEC_ID_DNXHD)
  804. mov_write_avid_tag(pb, track);
  805. else if(track->enc->codec_id == CODEC_ID_H264) {
  806. mov_write_avcc_tag(pb, track);
  807. if(track->mode == MODE_IPOD)
  808. mov_write_uuid_tag_ipod(pb);
  809. } else if (track->enc->field_order != AV_FIELD_UNKNOWN)
  810. mov_write_fiel_tag(pb, track);
  811. else if (track->vos_len > 0)
  812. mov_write_glbl_tag(pb, track);
  813. if (track->enc->sample_aspect_ratio.den && track->enc->sample_aspect_ratio.num &&
  814. track->enc->sample_aspect_ratio.den != track->enc->sample_aspect_ratio.num) {
  815. mov_write_pasp_tag(pb, track);
  816. }
  817. return update_size(pb, pos);
  818. }
  819. static int mov_write_rtp_tag(AVIOContext *pb, MOVTrack *track)
  820. {
  821. int64_t pos = avio_tell(pb);
  822. avio_wb32(pb, 0); /* size */
  823. ffio_wfourcc(pb, "rtp ");
  824. avio_wb32(pb, 0); /* Reserved */
  825. avio_wb16(pb, 0); /* Reserved */
  826. avio_wb16(pb, 1); /* Data-reference index */
  827. avio_wb16(pb, 1); /* Hint track version */
  828. avio_wb16(pb, 1); /* Highest compatible version */
  829. avio_wb32(pb, track->max_packet_size); /* Max packet size */
  830. avio_wb32(pb, 12); /* size */
  831. ffio_wfourcc(pb, "tims");
  832. avio_wb32(pb, track->timescale);
  833. return update_size(pb, pos);
  834. }
  835. static int mov_write_stsd_tag(AVIOContext *pb, MOVTrack *track)
  836. {
  837. int64_t pos = avio_tell(pb);
  838. avio_wb32(pb, 0); /* size */
  839. ffio_wfourcc(pb, "stsd");
  840. avio_wb32(pb, 0); /* version & flags */
  841. avio_wb32(pb, 1); /* entry count */
  842. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  843. mov_write_video_tag(pb, track);
  844. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  845. mov_write_audio_tag(pb, track);
  846. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)
  847. mov_write_subtitle_tag(pb, track);
  848. else if (track->enc->codec_tag == MKTAG('r','t','p',' '))
  849. mov_write_rtp_tag(pb, track);
  850. return update_size(pb, pos);
  851. }
  852. static int mov_write_ctts_tag(AVIOContext *pb, MOVTrack *track)
  853. {
  854. MOVStts *ctts_entries;
  855. uint32_t entries = 0;
  856. uint32_t atom_size;
  857. int i;
  858. ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
  859. ctts_entries[0].count = 1;
  860. ctts_entries[0].duration = track->cluster[0].cts;
  861. for (i=1; i<track->entry; i++) {
  862. if (track->cluster[i].cts == ctts_entries[entries].duration) {
  863. ctts_entries[entries].count++; /* compress */
  864. } else {
  865. entries++;
  866. ctts_entries[entries].duration = track->cluster[i].cts;
  867. ctts_entries[entries].count = 1;
  868. }
  869. }
  870. entries++; /* last one */
  871. atom_size = 16 + (entries * 8);
  872. avio_wb32(pb, atom_size); /* size */
  873. ffio_wfourcc(pb, "ctts");
  874. avio_wb32(pb, 0); /* version & flags */
  875. avio_wb32(pb, entries); /* entry count */
  876. for (i=0; i<entries; i++) {
  877. avio_wb32(pb, ctts_entries[i].count);
  878. avio_wb32(pb, ctts_entries[i].duration);
  879. }
  880. av_free(ctts_entries);
  881. return atom_size;
  882. }
  883. /* Time to sample atom */
  884. static int mov_write_stts_tag(AVIOContext *pb, MOVTrack *track)
  885. {
  886. MOVStts *stts_entries;
  887. uint32_t entries = -1;
  888. uint32_t atom_size;
  889. int i;
  890. if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO && !track->audio_vbr) {
  891. stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
  892. stts_entries[0].count = track->sample_count;
  893. stts_entries[0].duration = 1;
  894. entries = 1;
  895. } else {
  896. stts_entries = track->entry ?
  897. av_malloc(track->entry * sizeof(*stts_entries)) : /* worst case */
  898. NULL;
  899. for (i=0; i<track->entry; i++) {
  900. int64_t duration = i + 1 == track->entry ?
  901. track->track_duration - track->cluster[i].dts + track->start_dts : /* readjusting */
  902. track->cluster[i+1].dts - track->cluster[i].dts;
  903. if (i && duration == stts_entries[entries].duration) {
  904. stts_entries[entries].count++; /* compress */
  905. } else {
  906. entries++;
  907. stts_entries[entries].duration = duration;
  908. stts_entries[entries].count = 1;
  909. }
  910. }
  911. entries++; /* last one */
  912. }
  913. atom_size = 16 + (entries * 8);
  914. avio_wb32(pb, atom_size); /* size */
  915. ffio_wfourcc(pb, "stts");
  916. avio_wb32(pb, 0); /* version & flags */
  917. avio_wb32(pb, entries); /* entry count */
  918. for (i=0; i<entries; i++) {
  919. avio_wb32(pb, stts_entries[i].count);
  920. avio_wb32(pb, stts_entries[i].duration);
  921. }
  922. av_free(stts_entries);
  923. return atom_size;
  924. }
  925. static int mov_write_dref_tag(AVIOContext *pb)
  926. {
  927. avio_wb32(pb, 28); /* size */
  928. ffio_wfourcc(pb, "dref");
  929. avio_wb32(pb, 0); /* version & flags */
  930. avio_wb32(pb, 1); /* entry count */
  931. avio_wb32(pb, 0xc); /* size */
  932. ffio_wfourcc(pb, "url ");
  933. avio_wb32(pb, 1); /* version & flags */
  934. return 28;
  935. }
  936. static int mov_write_stbl_tag(AVIOContext *pb, MOVTrack *track)
  937. {
  938. int64_t pos = avio_tell(pb);
  939. avio_wb32(pb, 0); /* size */
  940. ffio_wfourcc(pb, "stbl");
  941. mov_write_stsd_tag(pb, track);
  942. mov_write_stts_tag(pb, track);
  943. if ((track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  944. track->enc->codec_tag == MKTAG('r','t','p',' ')) &&
  945. track->has_keyframes && track->has_keyframes < track->entry)
  946. mov_write_stss_tag(pb, track, MOV_SYNC_SAMPLE);
  947. if (track->mode == MODE_MOV && track->flags & MOV_TRACK_STPS)
  948. mov_write_stss_tag(pb, track, MOV_PARTIAL_SYNC_SAMPLE);
  949. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  950. track->flags & MOV_TRACK_CTTS)
  951. mov_write_ctts_tag(pb, track);
  952. mov_write_stsc_tag(pb, track);
  953. mov_write_stsz_tag(pb, track);
  954. mov_write_stco_tag(pb, track);
  955. return update_size(pb, pos);
  956. }
  957. static int mov_write_dinf_tag(AVIOContext *pb)
  958. {
  959. int64_t pos = avio_tell(pb);
  960. avio_wb32(pb, 0); /* size */
  961. ffio_wfourcc(pb, "dinf");
  962. mov_write_dref_tag(pb);
  963. return update_size(pb, pos);
  964. }
  965. static int mov_write_nmhd_tag(AVIOContext *pb)
  966. {
  967. avio_wb32(pb, 12);
  968. ffio_wfourcc(pb, "nmhd");
  969. avio_wb32(pb, 0);
  970. return 12;
  971. }
  972. static int mov_write_gmhd_tag(AVIOContext *pb)
  973. {
  974. avio_wb32(pb, 0x20); /* size */
  975. ffio_wfourcc(pb, "gmhd");
  976. avio_wb32(pb, 0x18); /* gmin size */
  977. ffio_wfourcc(pb, "gmin");/* generic media info */
  978. avio_wb32(pb, 0); /* version & flags */
  979. avio_wb16(pb, 0x40); /* graphics mode = */
  980. avio_wb16(pb, 0x8000); /* opColor (r?) */
  981. avio_wb16(pb, 0x8000); /* opColor (g?) */
  982. avio_wb16(pb, 0x8000); /* opColor (b?) */
  983. avio_wb16(pb, 0); /* balance */
  984. avio_wb16(pb, 0); /* reserved */
  985. return 0x20;
  986. }
  987. static int mov_write_smhd_tag(AVIOContext *pb)
  988. {
  989. avio_wb32(pb, 16); /* size */
  990. ffio_wfourcc(pb, "smhd");
  991. avio_wb32(pb, 0); /* version & flags */
  992. avio_wb16(pb, 0); /* reserved (balance, normally = 0) */
  993. avio_wb16(pb, 0); /* reserved */
  994. return 16;
  995. }
  996. static int mov_write_vmhd_tag(AVIOContext *pb)
  997. {
  998. avio_wb32(pb, 0x14); /* size (always 0x14) */
  999. ffio_wfourcc(pb, "vmhd");
  1000. avio_wb32(pb, 0x01); /* version & flags */
  1001. avio_wb64(pb, 0); /* reserved (graphics mode = copy) */
  1002. return 0x14;
  1003. }
  1004. static int mov_write_hdlr_tag(AVIOContext *pb, MOVTrack *track)
  1005. {
  1006. const char *hdlr, *descr = NULL, *hdlr_type = NULL;
  1007. int64_t pos = avio_tell(pb);
  1008. if (!track) { /* no media --> data handler */
  1009. hdlr = "dhlr";
  1010. hdlr_type = "url ";
  1011. descr = "DataHandler";
  1012. } else {
  1013. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  1014. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1015. hdlr_type = "vide";
  1016. descr = "VideoHandler";
  1017. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  1018. hdlr_type = "soun";
  1019. descr = "SoundHandler";
  1020. } else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1021. if (track->tag == MKTAG('t','x','3','g')) hdlr_type = "sbtl";
  1022. else hdlr_type = "text";
  1023. descr = "SubtitleHandler";
  1024. } else if (track->enc->codec_tag == MKTAG('r','t','p',' ')) {
  1025. hdlr_type = "hint";
  1026. descr = "HintHandler";
  1027. }
  1028. }
  1029. avio_wb32(pb, 0); /* size */
  1030. ffio_wfourcc(pb, "hdlr");
  1031. avio_wb32(pb, 0); /* Version & flags */
  1032. avio_write(pb, hdlr, 4); /* handler */
  1033. ffio_wfourcc(pb, hdlr_type); /* handler type */
  1034. avio_wb32(pb ,0); /* reserved */
  1035. avio_wb32(pb ,0); /* reserved */
  1036. avio_wb32(pb ,0); /* reserved */
  1037. if (!track || track->mode == MODE_MOV)
  1038. avio_w8(pb, strlen(descr)); /* pascal string */
  1039. avio_write(pb, descr, strlen(descr)); /* handler description */
  1040. if (track && track->mode != MODE_MOV)
  1041. avio_w8(pb, 0); /* c string */
  1042. return update_size(pb, pos);
  1043. }
  1044. static int mov_write_hmhd_tag(AVIOContext *pb)
  1045. {
  1046. /* This atom must be present, but leaving the values at zero
  1047. * seems harmless. */
  1048. avio_wb32(pb, 28); /* size */
  1049. ffio_wfourcc(pb, "hmhd");
  1050. avio_wb32(pb, 0); /* version, flags */
  1051. avio_wb16(pb, 0); /* maxPDUsize */
  1052. avio_wb16(pb, 0); /* avgPDUsize */
  1053. avio_wb32(pb, 0); /* maxbitrate */
  1054. avio_wb32(pb, 0); /* avgbitrate */
  1055. avio_wb32(pb, 0); /* reserved */
  1056. return 28;
  1057. }
  1058. static int mov_write_minf_tag(AVIOContext *pb, MOVTrack *track)
  1059. {
  1060. int64_t pos = avio_tell(pb);
  1061. avio_wb32(pb, 0); /* size */
  1062. ffio_wfourcc(pb, "minf");
  1063. if(track->enc->codec_type == AVMEDIA_TYPE_VIDEO)
  1064. mov_write_vmhd_tag(pb);
  1065. else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1066. mov_write_smhd_tag(pb);
  1067. else if (track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE) {
  1068. if (track->tag == MKTAG('t','e','x','t')) mov_write_gmhd_tag(pb);
  1069. else mov_write_nmhd_tag(pb);
  1070. } else if (track->tag == MKTAG('r','t','p',' ')) {
  1071. mov_write_hmhd_tag(pb);
  1072. }
  1073. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  1074. mov_write_hdlr_tag(pb, NULL);
  1075. mov_write_dinf_tag(pb);
  1076. mov_write_stbl_tag(pb, track);
  1077. return update_size(pb, pos);
  1078. }
  1079. static int mov_write_mdhd_tag(AVIOContext *pb, MOVTrack *track)
  1080. {
  1081. int version = track->track_duration < INT32_MAX ? 0 : 1;
  1082. (version == 1) ? avio_wb32(pb, 44) : avio_wb32(pb, 32); /* size */
  1083. ffio_wfourcc(pb, "mdhd");
  1084. avio_w8(pb, version);
  1085. avio_wb24(pb, 0); /* flags */
  1086. if (version == 1) {
  1087. avio_wb64(pb, track->time);
  1088. avio_wb64(pb, track->time);
  1089. } else {
  1090. avio_wb32(pb, track->time); /* creation time */
  1091. avio_wb32(pb, track->time); /* modification time */
  1092. }
  1093. avio_wb32(pb, track->timescale); /* time scale (sample rate for audio) */
  1094. (version == 1) ? avio_wb64(pb, track->track_duration) : avio_wb32(pb, track->track_duration); /* duration */
  1095. avio_wb16(pb, track->language); /* language */
  1096. avio_wb16(pb, 0); /* reserved (quality) */
  1097. if(version!=0 && track->mode == MODE_MOV){
  1098. av_log(NULL, AV_LOG_ERROR,
  1099. "FATAL error, file duration too long for timebase, this file will not be\n"
  1100. "playable with quicktime. Choose a different timebase or a different\n"
  1101. "container format\n");
  1102. }
  1103. return 32;
  1104. }
  1105. static int mov_write_mdia_tag(AVIOContext *pb, MOVTrack *track)
  1106. {
  1107. int64_t pos = avio_tell(pb);
  1108. avio_wb32(pb, 0); /* size */
  1109. ffio_wfourcc(pb, "mdia");
  1110. mov_write_mdhd_tag(pb, track);
  1111. mov_write_hdlr_tag(pb, track);
  1112. mov_write_minf_tag(pb, track);
  1113. return update_size(pb, pos);
  1114. }
  1115. static int mov_write_tkhd_tag(AVIOContext *pb, MOVTrack *track, AVStream *st)
  1116. {
  1117. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1118. track->timescale, AV_ROUND_UP);
  1119. int version = duration < INT32_MAX ? 0 : 1;
  1120. (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */
  1121. ffio_wfourcc(pb, "tkhd");
  1122. avio_w8(pb, version);
  1123. avio_wb24(pb, 0xf); /* flags (track enabled) */
  1124. if (version == 1) {
  1125. avio_wb64(pb, track->time);
  1126. avio_wb64(pb, track->time);
  1127. } else {
  1128. avio_wb32(pb, track->time); /* creation time */
  1129. avio_wb32(pb, track->time); /* modification time */
  1130. }
  1131. avio_wb32(pb, track->track_id); /* track-id */
  1132. avio_wb32(pb, 0); /* reserved */
  1133. (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);
  1134. avio_wb32(pb, 0); /* reserved */
  1135. avio_wb32(pb, 0); /* reserved */
  1136. avio_wb16(pb, 0); /* layer */
  1137. avio_wb16(pb, st ? st->codec->codec_type : 0); /* alternate group) */
  1138. /* Volume, only for audio */
  1139. if(track->enc->codec_type == AVMEDIA_TYPE_AUDIO)
  1140. avio_wb16(pb, 0x0100);
  1141. else
  1142. avio_wb16(pb, 0);
  1143. avio_wb16(pb, 0); /* reserved */
  1144. /* Matrix structure */
  1145. avio_wb32(pb, 0x00010000); /* reserved */
  1146. avio_wb32(pb, 0x0); /* reserved */
  1147. avio_wb32(pb, 0x0); /* reserved */
  1148. avio_wb32(pb, 0x0); /* reserved */
  1149. avio_wb32(pb, 0x00010000); /* reserved */
  1150. avio_wb32(pb, 0x0); /* reserved */
  1151. avio_wb32(pb, 0x0); /* reserved */
  1152. avio_wb32(pb, 0x0); /* reserved */
  1153. avio_wb32(pb, 0x40000000); /* reserved */
  1154. /* Track width and height, for visual only */
  1155. if(st && (track->enc->codec_type == AVMEDIA_TYPE_VIDEO ||
  1156. track->enc->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
  1157. if(track->mode == MODE_MOV) {
  1158. avio_wb32(pb, track->enc->width << 16);
  1159. avio_wb32(pb, track->height << 16);
  1160. } else {
  1161. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1162. if(!sample_aspect_ratio || track->height != track->enc->height)
  1163. sample_aspect_ratio = 1;
  1164. avio_wb32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  1165. avio_wb32(pb, track->height*0x10000);
  1166. }
  1167. }
  1168. else {
  1169. avio_wb32(pb, 0);
  1170. avio_wb32(pb, 0);
  1171. }
  1172. return 0x5c;
  1173. }
  1174. static int mov_write_tapt_tag(AVIOContext *pb, MOVTrack *track)
  1175. {
  1176. int32_t width = av_rescale(track->enc->sample_aspect_ratio.num, track->enc->width,
  1177. track->enc->sample_aspect_ratio.den);
  1178. int64_t pos = avio_tell(pb);
  1179. avio_wb32(pb, 0); /* size */
  1180. ffio_wfourcc(pb, "tapt");
  1181. avio_wb32(pb, 20);
  1182. ffio_wfourcc(pb, "clef");
  1183. avio_wb32(pb, 0);
  1184. avio_wb32(pb, width << 16);
  1185. avio_wb32(pb, track->enc->height << 16);
  1186. avio_wb32(pb, 20);
  1187. ffio_wfourcc(pb, "enof");
  1188. avio_wb32(pb, 0);
  1189. avio_wb32(pb, track->enc->width << 16);
  1190. avio_wb32(pb, track->enc->height << 16);
  1191. return update_size(pb, pos);
  1192. }
  1193. // This box seems important for the psp playback ... without it the movie seems to hang
  1194. static int mov_write_edts_tag(AVIOContext *pb, MOVTrack *track)
  1195. {
  1196. int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,
  1197. track->timescale, AV_ROUND_UP);
  1198. int version = duration < INT32_MAX ? 0 : 1;
  1199. int entry_size, entry_count, size;
  1200. int64_t delay, start_ct = track->cluster[0].cts;
  1201. delay = av_rescale_rnd(track->cluster[0].dts + start_ct, MOV_TIMESCALE,
  1202. track->timescale, AV_ROUND_DOWN);
  1203. version |= delay < INT32_MAX ? 0 : 1;
  1204. entry_size = (version == 1) ? 20 : 12;
  1205. entry_count = 1 + (delay > 0);
  1206. size = 24 + entry_count * entry_size;
  1207. /* write the atom data */
  1208. avio_wb32(pb, size);
  1209. ffio_wfourcc(pb, "edts");
  1210. avio_wb32(pb, size - 8);
  1211. ffio_wfourcc(pb, "elst");
  1212. avio_w8(pb, version);
  1213. avio_wb24(pb, 0); /* flags */
  1214. avio_wb32(pb, entry_count);
  1215. if (delay > 0) { /* add an empty edit to delay presentation */
  1216. if (version == 1) {
  1217. avio_wb64(pb, delay);
  1218. avio_wb64(pb, -1);
  1219. } else {
  1220. avio_wb32(pb, delay);
  1221. avio_wb32(pb, -1);
  1222. }
  1223. avio_wb32(pb, 0x00010000);
  1224. }
  1225. /* duration */
  1226. if (version == 1) {
  1227. avio_wb64(pb, duration);
  1228. avio_wb64(pb, start_ct);
  1229. } else {
  1230. avio_wb32(pb, duration);
  1231. avio_wb32(pb, start_ct);
  1232. }
  1233. avio_wb32(pb, 0x00010000);
  1234. return size;
  1235. }
  1236. static int mov_write_tref_tag(AVIOContext *pb, MOVTrack *track)
  1237. {
  1238. avio_wb32(pb, 20); // size
  1239. ffio_wfourcc(pb, "tref");
  1240. avio_wb32(pb, 12); // size (subatom)
  1241. avio_wl32(pb, track->tref_tag);
  1242. avio_wb32(pb, track->tref_id);
  1243. return 20;
  1244. }
  1245. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  1246. static int mov_write_uuid_tag_psp(AVIOContext *pb, MOVTrack *mov)
  1247. {
  1248. avio_wb32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  1249. ffio_wfourcc(pb, "uuid");
  1250. ffio_wfourcc(pb, "USMT");
  1251. avio_wb32(pb, 0x21d24fce);
  1252. avio_wb32(pb, 0xbb88695c);
  1253. avio_wb32(pb, 0xfac9c740);
  1254. avio_wb32(pb, 0x1c); // another size here!
  1255. ffio_wfourcc(pb, "MTDT");
  1256. avio_wb32(pb, 0x00010012);
  1257. avio_wb32(pb, 0x0a);
  1258. avio_wb32(pb, 0x55c40000);
  1259. avio_wb32(pb, 0x1);
  1260. avio_wb32(pb, 0x0);
  1261. return 0x34;
  1262. }
  1263. static int mov_write_udta_sdp(AVIOContext *pb, AVFormatContext *ctx, int index)
  1264. {
  1265. char buf[1000] = "";
  1266. int len;
  1267. ff_sdp_write_media(buf, sizeof(buf), ctx->streams[0]->codec, NULL, NULL, 0, 0, ctx);
  1268. av_strlcatf(buf, sizeof(buf), "a=control:streamid=%d\r\n", index);
  1269. len = strlen(buf);
  1270. avio_wb32(pb, len + 24);
  1271. ffio_wfourcc(pb, "udta");
  1272. avio_wb32(pb, len + 16);
  1273. ffio_wfourcc(pb, "hnti");
  1274. avio_wb32(pb, len + 8);
  1275. ffio_wfourcc(pb, "sdp ");
  1276. avio_write(pb, buf, len);
  1277. return len + 24;
  1278. }
  1279. static int mov_write_trak_tag(AVIOContext *pb, MOVMuxContext *mov,
  1280. MOVTrack *track, AVStream *st)
  1281. {
  1282. int64_t pos = avio_tell(pb);
  1283. avio_wb32(pb, 0); /* size */
  1284. ffio_wfourcc(pb, "trak");
  1285. mov_write_tkhd_tag(pb, track, st);
  1286. if (track->mode == MODE_PSP || track->flags & MOV_TRACK_CTTS ||
  1287. (track->entry && track->cluster[0].dts)) {
  1288. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  1289. mov_write_edts_tag(pb, track); // PSP Movies require edts box
  1290. }
  1291. if (track->tref_tag)
  1292. mov_write_tref_tag(pb, track);
  1293. mov_write_mdia_tag(pb, track);
  1294. if (track->mode == MODE_PSP)
  1295. mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
  1296. if (track->tag == MKTAG('r','t','p',' '))
  1297. mov_write_udta_sdp(pb, track->rtp_ctx, track->track_id);
  1298. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO && track->mode == MODE_MOV) {
  1299. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  1300. if (0.0 != sample_aspect_ratio && 1.0 != sample_aspect_ratio)
  1301. mov_write_tapt_tag(pb, track);
  1302. };
  1303. return update_size(pb, pos);
  1304. }
  1305. static int mov_write_iods_tag(AVIOContext *pb, MOVMuxContext *mov)
  1306. {
  1307. int i, has_audio = 0, has_video = 0;
  1308. int64_t pos = avio_tell(pb);
  1309. int audio_profile = mov->iods_audio_profile;
  1310. int video_profile = mov->iods_video_profile;
  1311. for (i = 0; i < mov->nb_streams; i++) {
  1312. if(mov->tracks[i].entry > 0) {
  1313. has_audio |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_AUDIO;
  1314. has_video |= mov->tracks[i].enc->codec_type == AVMEDIA_TYPE_VIDEO;
  1315. }
  1316. }
  1317. if (audio_profile < 0)
  1318. audio_profile = 0xFF - has_audio;
  1319. if (video_profile < 0)
  1320. video_profile = 0xFF - has_video;
  1321. avio_wb32(pb, 0x0); /* size */
  1322. ffio_wfourcc(pb, "iods");
  1323. avio_wb32(pb, 0); /* version & flags */
  1324. put_descr(pb, 0x10, 7);
  1325. avio_wb16(pb, 0x004f);
  1326. avio_w8(pb, 0xff);
  1327. avio_w8(pb, 0xff);
  1328. avio_w8(pb, audio_profile);
  1329. avio_w8(pb, video_profile);
  1330. avio_w8(pb, 0xff);
  1331. return update_size(pb, pos);
  1332. }
  1333. static int mov_write_trex_tag(AVIOContext *pb, MOVTrack *track)
  1334. {
  1335. avio_wb32(pb, 0x20); /* size */
  1336. ffio_wfourcc(pb, "trex");
  1337. avio_wb32(pb, 0); /* version & flags */
  1338. avio_wb32(pb, track->track_id); /* track ID */
  1339. avio_wb32(pb, 1); /* default sample description index */
  1340. avio_wb32(pb, 0); /* default sample duration */
  1341. avio_wb32(pb, 0); /* default sample size */
  1342. avio_wb32(pb, 0); /* default sample flags */
  1343. return 0;
  1344. }
  1345. static int mov_write_mvex_tag(AVIOContext *pb, MOVMuxContext *mov)
  1346. {
  1347. int64_t pos = avio_tell(pb);
  1348. int i;
  1349. avio_wb32(pb, 0x0); /* size */
  1350. ffio_wfourcc(pb, "mvex");
  1351. for (i = 0; i < mov->nb_streams; i++)
  1352. mov_write_trex_tag(pb, &mov->tracks[i]);
  1353. return update_size(pb, pos);
  1354. }
  1355. static int mov_write_mvhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  1356. {
  1357. int max_track_id = 1, i;
  1358. int64_t max_track_len_temp, max_track_len = 0;
  1359. int version;
  1360. for (i=0; i<mov->nb_streams; i++) {
  1361. if(mov->tracks[i].entry > 0) {
  1362. max_track_len_temp = av_rescale_rnd(mov->tracks[i].track_duration,
  1363. MOV_TIMESCALE,
  1364. mov->tracks[i].timescale,
  1365. AV_ROUND_UP);
  1366. if (max_track_len < max_track_len_temp)
  1367. max_track_len = max_track_len_temp;
  1368. if (max_track_id < mov->tracks[i].track_id)
  1369. max_track_id = mov->tracks[i].track_id;
  1370. }
  1371. }
  1372. version = max_track_len < UINT32_MAX ? 0 : 1;
  1373. (version == 1) ? avio_wb32(pb, 120) : avio_wb32(pb, 108); /* size */
  1374. ffio_wfourcc(pb, "mvhd");
  1375. avio_w8(pb, version);
  1376. avio_wb24(pb, 0); /* flags */
  1377. if (version == 1) {
  1378. avio_wb64(pb, mov->time);
  1379. avio_wb64(pb, mov->time);
  1380. } else {
  1381. avio_wb32(pb, mov->time); /* creation time */
  1382. avio_wb32(pb, mov->time); /* modification time */
  1383. }
  1384. avio_wb32(pb, MOV_TIMESCALE);
  1385. (version == 1) ? avio_wb64(pb, max_track_len) : avio_wb32(pb, max_track_len); /* duration of longest track */
  1386. avio_wb32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  1387. avio_wb16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  1388. avio_wb16(pb, 0); /* reserved */
  1389. avio_wb32(pb, 0); /* reserved */
  1390. avio_wb32(pb, 0); /* reserved */
  1391. /* Matrix structure */
  1392. avio_wb32(pb, 0x00010000); /* reserved */
  1393. avio_wb32(pb, 0x0); /* reserved */
  1394. avio_wb32(pb, 0x0); /* reserved */
  1395. avio_wb32(pb, 0x0); /* reserved */
  1396. avio_wb32(pb, 0x00010000); /* reserved */
  1397. avio_wb32(pb, 0x0); /* reserved */
  1398. avio_wb32(pb, 0x0); /* reserved */
  1399. avio_wb32(pb, 0x0); /* reserved */
  1400. avio_wb32(pb, 0x40000000); /* reserved */
  1401. avio_wb32(pb, 0); /* reserved (preview time) */
  1402. avio_wb32(pb, 0); /* reserved (preview duration) */
  1403. avio_wb32(pb, 0); /* reserved (poster time) */
  1404. avio_wb32(pb, 0); /* reserved (selection time) */
  1405. avio_wb32(pb, 0); /* reserved (selection duration) */
  1406. avio_wb32(pb, 0); /* reserved (current time) */
  1407. avio_wb32(pb, max_track_id + 1); /* Next track id */
  1408. return 0x6c;
  1409. }
  1410. static int mov_write_itunes_hdlr_tag(AVIOContext *pb, MOVMuxContext *mov,
  1411. AVFormatContext *s)
  1412. {
  1413. avio_wb32(pb, 33); /* size */
  1414. ffio_wfourcc(pb, "hdlr");
  1415. avio_wb32(pb, 0);
  1416. avio_wb32(pb, 0);
  1417. ffio_wfourcc(pb, "mdir");
  1418. ffio_wfourcc(pb, "appl");
  1419. avio_wb32(pb, 0);
  1420. avio_wb32(pb, 0);
  1421. avio_w8(pb, 0);
  1422. return 33;
  1423. }
  1424. /* helper function to write a data tag with the specified string as data */
  1425. static int mov_write_string_data_tag(AVIOContext *pb, const char *data, int lang, int long_style)
  1426. {
  1427. if(long_style){
  1428. int size = 16 + strlen(data);
  1429. avio_wb32(pb, size); /* size */
  1430. ffio_wfourcc(pb, "data");
  1431. avio_wb32(pb, 1);
  1432. avio_wb32(pb, 0);
  1433. avio_write(pb, data, strlen(data));
  1434. return size;
  1435. }else{
  1436. if (!lang)
  1437. lang = ff_mov_iso639_to_lang("und", 1);
  1438. avio_wb16(pb, strlen(data)); /* string length */
  1439. avio_wb16(pb, lang);
  1440. avio_write(pb, data, strlen(data));
  1441. return strlen(data) + 4;
  1442. }
  1443. }
  1444. static int mov_write_string_tag(AVIOContext *pb, const char *name, const char *value, int lang, int long_style){
  1445. int size = 0;
  1446. if (value && value[0]) {
  1447. int64_t pos = avio_tell(pb);
  1448. avio_wb32(pb, 0); /* size */
  1449. ffio_wfourcc(pb, name);
  1450. mov_write_string_data_tag(pb, value, lang, long_style);
  1451. size = update_size(pb, pos);
  1452. }
  1453. return size;
  1454. }
  1455. static int mov_write_string_metadata(AVFormatContext *s, AVIOContext *pb,
  1456. const char *name, const char *tag,
  1457. int long_style)
  1458. {
  1459. int l, lang = 0, len, len2;
  1460. AVDictionaryEntry *t, *t2 = NULL;
  1461. char tag2[16];
  1462. if (!(t = av_dict_get(s->metadata, tag, NULL, 0)))
  1463. return 0;
  1464. len = strlen(t->key);
  1465. snprintf(tag2, sizeof(tag2), "%s-", tag);
  1466. while ((t2 = av_dict_get(s->metadata, tag2, t2, AV_DICT_IGNORE_SUFFIX))) {
  1467. len2 = strlen(t2->key);
  1468. if (len2 == len+4 && !strcmp(t->value, t2->value)
  1469. && (l=ff_mov_iso639_to_lang(&t2->key[len2-3], 1)) >= 0) {
  1470. lang = l;
  1471. break;
  1472. }
  1473. }
  1474. return mov_write_string_tag(pb, name, t->value, lang, long_style);
  1475. }
  1476. /* iTunes track number */
  1477. static int mov_write_trkn_tag(AVIOContext *pb, MOVMuxContext *mov,
  1478. AVFormatContext *s)
  1479. {
  1480. AVDictionaryEntry *t = av_dict_get(s->metadata, "track", NULL, 0);
  1481. int size = 0, track = t ? atoi(t->value) : 0;
  1482. if (track) {
  1483. avio_wb32(pb, 32); /* size */
  1484. ffio_wfourcc(pb, "trkn");
  1485. avio_wb32(pb, 24); /* size */
  1486. ffio_wfourcc(pb, "data");
  1487. avio_wb32(pb, 0); // 8 bytes empty
  1488. avio_wb32(pb, 0);
  1489. avio_wb16(pb, 0); // empty
  1490. avio_wb16(pb, track); // track number
  1491. avio_wb16(pb, 0); // total track number
  1492. avio_wb16(pb, 0); // empty
  1493. size = 32;
  1494. }
  1495. return size;
  1496. }
  1497. /* iTunes meta data list */
  1498. static int mov_write_ilst_tag(AVIOContext *pb, MOVMuxContext *mov,
  1499. AVFormatContext *s)
  1500. {
  1501. int64_t pos = avio_tell(pb);
  1502. avio_wb32(pb, 0); /* size */
  1503. ffio_wfourcc(pb, "ilst");
  1504. mov_write_string_metadata(s, pb, "\251nam", "title" , 1);
  1505. mov_write_string_metadata(s, pb, "\251ART", "artist" , 1);
  1506. mov_write_string_metadata(s, pb, "aART", "album_artist", 1);
  1507. mov_write_string_metadata(s, pb, "\251wrt", "composer" , 1);
  1508. mov_write_string_metadata(s, pb, "\251alb", "album" , 1);
  1509. mov_write_string_metadata(s, pb, "\251day", "date" , 1);
  1510. mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 0, 1);
  1511. mov_write_string_metadata(s, pb, "\251cmt", "comment" , 1);
  1512. mov_write_string_metadata(s, pb, "\251gen", "genre" , 1);
  1513. mov_write_string_metadata(s, pb, "\251cpy", "copyright", 1);
  1514. mov_write_string_metadata(s, pb, "\251grp", "grouping" , 1);
  1515. mov_write_string_metadata(s, pb, "\251lyr", "lyrics" , 1);
  1516. mov_write_string_metadata(s, pb, "desc", "description",1);
  1517. mov_write_string_metadata(s, pb, "ldes", "synopsis" , 1);
  1518. mov_write_string_metadata(s, pb, "tvsh", "show" , 1);
  1519. mov_write_string_metadata(s, pb, "tven", "episode_id",1);
  1520. mov_write_string_metadata(s, pb, "tvnn", "network" , 1);
  1521. mov_write_trkn_tag(pb, mov, s);
  1522. return update_size(pb, pos);
  1523. }
  1524. /* iTunes meta data tag */
  1525. static int mov_write_meta_tag(AVIOContext *pb, MOVMuxContext *mov,
  1526. AVFormatContext *s)
  1527. {
  1528. int size = 0;
  1529. int64_t pos = avio_tell(pb);
  1530. avio_wb32(pb, 0); /* size */
  1531. ffio_wfourcc(pb, "meta");
  1532. avio_wb32(pb, 0);
  1533. mov_write_itunes_hdlr_tag(pb, mov, s);
  1534. mov_write_ilst_tag(pb, mov, s);
  1535. size = update_size(pb, pos);
  1536. return size;
  1537. }
  1538. static int utf8len(const uint8_t *b)
  1539. {
  1540. int len=0;
  1541. int val;
  1542. while(*b){
  1543. GET_UTF8(val, *b++, return -1;)
  1544. len++;
  1545. }
  1546. return len;
  1547. }
  1548. static int ascii_to_wc(AVIOContext *pb, const uint8_t *b)
  1549. {
  1550. int val;
  1551. while(*b){
  1552. GET_UTF8(val, *b++, return -1;)
  1553. avio_wb16(pb, val);
  1554. }
  1555. avio_wb16(pb, 0x00);
  1556. return 0;
  1557. }
  1558. static uint16_t language_code(const char *str)
  1559. {
  1560. return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
  1561. }
  1562. static int mov_write_3gp_udta_tag(AVIOContext *pb, AVFormatContext *s,
  1563. const char *tag, const char *str)
  1564. {
  1565. int64_t pos = avio_tell(pb);
  1566. AVDictionaryEntry *t = av_dict_get(s->metadata, str, NULL, 0);
  1567. if (!t || !utf8len(t->value))
  1568. return 0;
  1569. avio_wb32(pb, 0); /* size */
  1570. ffio_wfourcc(pb, tag); /* type */
  1571. avio_wb32(pb, 0); /* version + flags */
  1572. if (!strcmp(tag, "yrrc"))
  1573. avio_wb16(pb, atoi(t->value));
  1574. else {
  1575. avio_wb16(pb, language_code("eng")); /* language */
  1576. avio_write(pb, t->value, strlen(t->value)+1); /* UTF8 string value */
  1577. if (!strcmp(tag, "albm") &&
  1578. (t = av_dict_get(s->metadata, "track", NULL, 0)))
  1579. avio_w8(pb, atoi(t->value));
  1580. }
  1581. return update_size(pb, pos);
  1582. }
  1583. static int mov_write_chpl_tag(AVIOContext *pb, AVFormatContext *s)
  1584. {
  1585. int64_t pos = avio_tell(pb);
  1586. int i, nb_chapters = FFMIN(s->nb_chapters, 255);
  1587. avio_wb32(pb, 0); // size
  1588. ffio_wfourcc(pb, "chpl");
  1589. avio_wb32(pb, 0x01000000); // version + flags
  1590. avio_wb32(pb, 0); // unknown
  1591. avio_w8(pb, nb_chapters);
  1592. for (i = 0; i < nb_chapters; i++) {
  1593. AVChapter *c = s->chapters[i];
  1594. AVDictionaryEntry *t;
  1595. avio_wb64(pb, av_rescale_q(c->start, c->time_base, (AVRational){1,10000000}));
  1596. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  1597. int len = FFMIN(strlen(t->value), 255);
  1598. avio_w8(pb, len);
  1599. avio_write(pb, t->value, len);
  1600. } else
  1601. avio_w8(pb, 0);
  1602. }
  1603. return update_size(pb, pos);
  1604. }
  1605. static int mov_write_udta_tag(AVIOContext *pb, MOVMuxContext *mov,
  1606. AVFormatContext *s)
  1607. {
  1608. AVIOContext *pb_buf;
  1609. int i, ret, size;
  1610. uint8_t *buf;
  1611. for (i = 0; i < s->nb_streams; i++)
  1612. if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
  1613. return 0;
  1614. }
  1615. ret = avio_open_dyn_buf(&pb_buf);
  1616. if(ret < 0)
  1617. return ret;
  1618. if (mov->mode & MODE_3GP) {
  1619. mov_write_3gp_udta_tag(pb_buf, s, "perf", "artist");
  1620. mov_write_3gp_udta_tag(pb_buf, s, "titl", "title");
  1621. mov_write_3gp_udta_tag(pb_buf, s, "auth", "author");
  1622. mov_write_3gp_udta_tag(pb_buf, s, "gnre", "genre");
  1623. mov_write_3gp_udta_tag(pb_buf, s, "dscp", "comment");
  1624. mov_write_3gp_udta_tag(pb_buf, s, "albm", "album");
  1625. mov_write_3gp_udta_tag(pb_buf, s, "cprt", "copyright");
  1626. mov_write_3gp_udta_tag(pb_buf, s, "yrrc", "date");
  1627. } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
  1628. mov_write_string_metadata(s, pb_buf, "\251ART", "artist" , 0);
  1629. mov_write_string_metadata(s, pb_buf, "\251nam", "title" , 0);
  1630. mov_write_string_metadata(s, pb_buf, "\251aut", "author" , 0);
  1631. mov_write_string_metadata(s, pb_buf, "\251alb", "album" , 0);
  1632. mov_write_string_metadata(s, pb_buf, "\251day", "date" , 0);
  1633. mov_write_string_metadata(s, pb_buf, "\251swr", "encoder" , 0);
  1634. mov_write_string_metadata(s, pb_buf, "\251des", "comment" , 0);
  1635. mov_write_string_metadata(s, pb_buf, "\251gen", "genre" , 0);
  1636. mov_write_string_metadata(s, pb_buf, "\251cpy", "copyright" , 0);
  1637. } else {
  1638. /* iTunes meta data */
  1639. mov_write_meta_tag(pb_buf, mov, s);
  1640. }
  1641. if (s->nb_chapters)
  1642. mov_write_chpl_tag(pb_buf, s);
  1643. if ((size = avio_close_dyn_buf(pb_buf, &buf)) > 0) {
  1644. avio_wb32(pb, size+8);
  1645. ffio_wfourcc(pb, "udta");
  1646. avio_write(pb, buf, size);
  1647. }
  1648. av_free(buf);
  1649. return 0;
  1650. }
  1651. static void mov_write_psp_udta_tag(AVIOContext *pb,
  1652. const char *str, const char *lang, int type)
  1653. {
  1654. int len = utf8len(str)+1;
  1655. if(len<=0)
  1656. return;
  1657. avio_wb16(pb, len*2+10); /* size */
  1658. avio_wb32(pb, type); /* type */
  1659. avio_wb16(pb, language_code(lang)); /* language */
  1660. avio_wb16(pb, 0x01); /* ? */
  1661. ascii_to_wc(pb, str);
  1662. }
  1663. static int mov_write_uuidusmt_tag(AVIOContext *pb, AVFormatContext *s)
  1664. {
  1665. AVDictionaryEntry *title = av_dict_get(s->metadata, "title", NULL, 0);
  1666. int64_t pos, pos2;
  1667. if (title) {
  1668. pos = avio_tell(pb);
  1669. avio_wb32(pb, 0); /* size placeholder*/
  1670. ffio_wfourcc(pb, "uuid");
  1671. ffio_wfourcc(pb, "USMT");
  1672. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  1673. avio_wb32(pb, 0xbb88695c);
  1674. avio_wb32(pb, 0xfac9c740);
  1675. pos2 = avio_tell(pb);
  1676. avio_wb32(pb, 0); /* size placeholder*/
  1677. ffio_wfourcc(pb, "MTDT");
  1678. avio_wb16(pb, 4);
  1679. // ?
  1680. avio_wb16(pb, 0x0C); /* size */
  1681. avio_wb32(pb, 0x0B); /* type */
  1682. avio_wb16(pb, language_code("und")); /* language */
  1683. avio_wb16(pb, 0x0); /* ? */
  1684. avio_wb16(pb, 0x021C); /* data */
  1685. mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
  1686. mov_write_psp_udta_tag(pb, title->value, "eng", 0x01);
  1687. // snprintf(dt,32,"%04d/%02d/%02d %02d:%02d:%02d",t_st->tm_year+1900,t_st->tm_mon+1,t_st->tm_mday,t_st->tm_hour,t_st->tm_min,t_st->tm_sec);
  1688. mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
  1689. update_size(pb, pos2);
  1690. return update_size(pb, pos);
  1691. }
  1692. return 0;
  1693. }
  1694. static int mov_write_moov_tag(AVIOContext *pb, MOVMuxContext *mov,
  1695. AVFormatContext *s)
  1696. {
  1697. int i;
  1698. int64_t pos = avio_tell(pb);
  1699. avio_wb32(pb, 0); /* size placeholder*/
  1700. ffio_wfourcc(pb, "moov");
  1701. for (i=0; i<mov->nb_streams; i++) {
  1702. if (mov->tracks[i].entry <= 0 && !(mov->flags & FF_MOV_FLAG_FRAGMENT))
  1703. continue;
  1704. mov->tracks[i].time = mov->time;
  1705. mov->tracks[i].track_id = i+1;
  1706. }
  1707. if (mov->chapter_track)
  1708. for (i=0; i<s->nb_streams; i++) {
  1709. mov->tracks[i].tref_tag = MKTAG('c','h','a','p');
  1710. mov->tracks[i].tref_id = mov->tracks[mov->chapter_track].track_id;
  1711. }
  1712. for (i = 0; i < mov->nb_streams; i++) {
  1713. if (mov->tracks[i].tag == MKTAG('r','t','p',' ')) {
  1714. mov->tracks[i].tref_tag = MKTAG('h','i','n','t');
  1715. mov->tracks[i].tref_id =
  1716. mov->tracks[mov->tracks[i].src_track].track_id;
  1717. }
  1718. }
  1719. mov_write_mvhd_tag(pb, mov);
  1720. if (mov->mode != MODE_MOV && !mov->iods_skip)
  1721. mov_write_iods_tag(pb, mov);
  1722. for (i=0; i<mov->nb_streams; i++) {
  1723. if (mov->tracks[i].entry > 0 || mov->flags & FF_MOV_FLAG_FRAGMENT) {
  1724. mov_write_trak_tag(pb, mov, &(mov->tracks[i]), i < s->nb_streams ? s->streams[i] : NULL);
  1725. }
  1726. }
  1727. if (mov->flags & FF_MOV_FLAG_FRAGMENT)
  1728. mov_write_mvex_tag(pb, mov); /* QuickTime requires trak to precede this */
  1729. if (mov->mode == MODE_PSP)
  1730. mov_write_uuidusmt_tag(pb, s);
  1731. else
  1732. mov_write_udta_tag(pb, mov, s);
  1733. return update_size(pb, pos);
  1734. }
  1735. static void param_write_int(AVIOContext *pb, const char *name, int value)
  1736. {
  1737. avio_printf(pb, "<param name=\"%s\" value=\"%d\" valuetype=\"data\"/>\n", name, value);
  1738. }
  1739. static void param_write_string(AVIOContext *pb, const char *name, const char *value)
  1740. {
  1741. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, value);
  1742. }
  1743. static void param_write_hex(AVIOContext *pb, const char *name, const uint8_t *value, int len)
  1744. {
  1745. char buf[150];
  1746. len = FFMIN(sizeof(buf)/2 - 1, len);
  1747. ff_data_to_hex(buf, value, len, 0);
  1748. buf[2*len] = '\0';
  1749. avio_printf(pb, "<param name=\"%s\" value=\"%s\" valuetype=\"data\"/>\n", name, buf);
  1750. }
  1751. static void write_h264_extradata(AVIOContext *pb, AVCodecContext *enc)
  1752. {
  1753. uint16_t sps_size, pps_size, len;
  1754. char buf[150];
  1755. sps_size = AV_RB16(&enc->extradata[6]);
  1756. if (11 + sps_size > enc->extradata_size)
  1757. return;
  1758. pps_size = AV_RB16(&enc->extradata[9 + sps_size]);
  1759. if (11 + sps_size + pps_size > enc->extradata_size)
  1760. return;
  1761. len = FFMIN(sizeof(buf)/2 - 1, sps_size);
  1762. ff_data_to_hex(buf, &enc->extradata[8], len, 0);
  1763. buf[2*len] = '\0';
  1764. avio_printf(pb, "<param name=\"CodecPrivateData\" value=\"00000001%s", buf);
  1765. len = FFMIN(sizeof(buf)/2 - 1, pps_size);
  1766. ff_data_to_hex(buf, &enc->extradata[11 + sps_size], len, 0);
  1767. buf[2*len] = '\0';
  1768. avio_printf(pb, "00000001%s\" valuetype=\"data\"/>\n", buf);
  1769. }
  1770. static int mov_write_isml_manifest(AVIOContext *pb, MOVMuxContext *mov)
  1771. {
  1772. int64_t pos = avio_tell(pb);
  1773. int i;
  1774. const uint8_t uuid[] = {
  1775. 0xa5, 0xd4, 0x0b, 0x30, 0xe8, 0x14, 0x11, 0xdd,
  1776. 0xba, 0x2f, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66
  1777. };
  1778. avio_wb32(pb, 0);
  1779. ffio_wfourcc(pb, "uuid");
  1780. avio_write(pb, uuid, sizeof(uuid));
  1781. avio_wb32(pb, 0);
  1782. avio_printf(pb, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  1783. avio_printf(pb, "<smil xmlns=\"http://www.w3.org/2001/SMIL20/Language\">\n");
  1784. avio_printf(pb, "<head>\n");
  1785. avio_printf(pb, "<meta name=\"creator\" content=\"%s\" />\n",
  1786. LIBAVFORMAT_IDENT);
  1787. avio_printf(pb, "</head>\n");
  1788. avio_printf(pb, "<body>\n");
  1789. avio_printf(pb, "<switch>\n");
  1790. for (i = 0; i < mov->nb_streams; i++) {
  1791. MOVTrack *track = &mov->tracks[i];
  1792. const char *type;
  1793. /* track->track_id is initialized in write_moov, and thus isn't known
  1794. * here yet */
  1795. int track_id = i + 1;
  1796. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1797. type = "video";
  1798. } else if (track->enc->codec_type == AVMEDIA_TYPE_AUDIO) {
  1799. type = "audio";
  1800. } else {
  1801. continue;
  1802. }
  1803. avio_printf(pb, "<%s systemBitrate=\"%d\">\n", type,
  1804. track->enc->bit_rate);
  1805. param_write_int(pb, "systemBitrate", track->enc->bit_rate);
  1806. param_write_int(pb, "trackID", track_id);
  1807. if (track->enc->codec_type == AVMEDIA_TYPE_VIDEO) {
  1808. if (track->enc->codec_id == CODEC_ID_H264 &&
  1809. track->enc->extradata_size >= 11 &&
  1810. track->enc->extradata[0] == 1) {
  1811. write_h264_extradata(pb, track->enc);
  1812. } else {
  1813. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  1814. track->enc->extradata_size);
  1815. }
  1816. if (track->enc->codec_id == CODEC_ID_H264) {
  1817. param_write_string(pb, "FourCC", "H264");
  1818. } else if (track->enc->codec_id == CODEC_ID_VC1) {
  1819. param_write_string(pb, "FourCC", "WVC1");
  1820. }
  1821. param_write_int(pb, "MaxWidth", track->enc->width);
  1822. param_write_int(pb, "MaxHeight", track->enc->height);
  1823. param_write_int(pb, "DisplayWidth", track->enc->width);
  1824. param_write_int(pb, "DisplayHeight", track->enc->height);
  1825. } else {
  1826. if (track->enc->codec_id == CODEC_ID_AAC) {
  1827. param_write_string(pb, "FourCC", "AACL");
  1828. } else if (track->enc->codec_id == CODEC_ID_WMAPRO) {
  1829. param_write_string(pb, "FourCC", "WMAP");
  1830. }
  1831. param_write_hex(pb, "CodecPrivateData", track->enc->extradata,
  1832. track->enc->extradata_size);
  1833. param_write_int(pb, "AudioTag", ff_codec_get_tag(ff_codec_wav_tags,
  1834. track->enc->codec_id));
  1835. param_write_int(pb, "Channels", track->enc->channels);
  1836. param_write_int(pb, "SamplingRate", track->enc->sample_rate);
  1837. param_write_int(pb, "BitsPerSample", 16);
  1838. param_write_int(pb, "PacketSize", track->enc->block_align ?
  1839. track->enc->block_align : 4);
  1840. }
  1841. avio_printf(pb, "</%s>\n", type);
  1842. }
  1843. avio_printf(pb, "</switch>\n");
  1844. avio_printf(pb, "</body>\n");
  1845. avio_printf(pb, "</smil>\n");
  1846. return update_size(pb, pos);
  1847. }
  1848. static int mov_write_mfhd_tag(AVIOContext *pb, MOVMuxContext *mov)
  1849. {
  1850. avio_wb32(pb, 16);
  1851. ffio_wfourcc(pb, "mfhd");
  1852. avio_wb32(pb, 0);
  1853. avio_wb32(pb, mov->fragments);
  1854. return 0;
  1855. }
  1856. static int mov_write_tfhd_tag(AVIOContext *pb, MOVTrack *track,
  1857. int64_t moof_offset)
  1858. {
  1859. int64_t pos = avio_tell(pb);
  1860. /* default-sample-size + default-sample-duration + base-data-offset */
  1861. uint32_t flags = 0x19;
  1862. if (!track->entry) {
  1863. flags |= 0x010000; /* duration-is-empty */
  1864. } else {
  1865. flags |= 0x20; /* default-sample-flags-present */
  1866. }
  1867. /* Don't set a default sample size when creating data for silverlight,
  1868. * the player refuses to play files with that set. */
  1869. if (track->mode == MODE_ISM)
  1870. flags &= ~0x10;
  1871. avio_wb32(pb, 0); /* size placeholder */
  1872. ffio_wfourcc(pb, "tfhd");
  1873. avio_w8(pb, 0); /* version */
  1874. avio_wb24(pb, flags);
  1875. avio_wb32(pb, track->track_id); /* track-id */
  1876. if (flags & 0x01)
  1877. avio_wb64(pb, moof_offset);
  1878. if (flags & 0x08) {
  1879. track->default_duration = track->audio_vbr ? track->enc->frame_size : 1;
  1880. avio_wb32(pb, track->default_duration);
  1881. }
  1882. if (flags & 0x10) {
  1883. track->default_size = track->entry ? track->cluster[0].size : 1;
  1884. avio_wb32(pb, track->default_size);
  1885. } else
  1886. track->default_size = -1;
  1887. if (flags & 0x20) {
  1888. track->default_sample_flags =
  1889. track->enc->codec_type == AVMEDIA_TYPE_VIDEO ?
  1890. 0x01010000 : 0x02000000;
  1891. avio_wb32(pb, track->default_sample_flags);
  1892. }
  1893. return update_size(pb, pos);
  1894. }
  1895. static uint32_t get_sample_flags(MOVTrack *track, MOVIentry *entry)
  1896. {
  1897. return entry->flags & MOV_SYNC_SAMPLE ? 0x02000000 : 0x01010000;
  1898. }
  1899. static int mov_write_trun_tag(AVIOContext *pb, MOVTrack *track)
  1900. {
  1901. int64_t pos = avio_tell(pb);
  1902. uint32_t flags = 1; /* data-offset-present */
  1903. int i;
  1904. for (i = 0; i < track->entry; i++) {
  1905. int64_t duration = i + 1 == track->entry ?
  1906. track->track_duration - track->cluster[i].dts + track->start_dts :
  1907. track->cluster[i + 1].dts - track->cluster[i].dts;
  1908. if (duration != track->default_duration)
  1909. flags |= 0x100; /* sample-duration-present */
  1910. if (track->cluster[i].size != track->default_size)
  1911. flags |= 0x200; /* sample-size-present */
  1912. if (i > 0 && get_sample_flags(track, &track->cluster[i]) != track->default_sample_flags)
  1913. flags |= 0x400; /* sample-flags-present */
  1914. }
  1915. if (!(flags & 0x400))
  1916. flags |= 0x4; /* first-sample-flags-present */
  1917. if (track->flags & MOV_TRACK_CTTS)
  1918. flags |= 0x800; /* sample-composition-time-offsets-present */
  1919. avio_wb32(pb, 0); /* size placeholder */
  1920. ffio_wfourcc(pb, "trun");
  1921. avio_w8(pb, 0); /* version */
  1922. avio_wb24(pb, flags);
  1923. avio_wb32(pb, track->entry); /* sample count */
  1924. track->moof_size_offset = avio_tell(pb);
  1925. avio_wb32(pb, 0); /* data offset */
  1926. if (flags & 0x4) /* first sample flags */
  1927. avio_wb32(pb, get_sample_flags(track, &track->cluster[0]));
  1928. for (i = 0; i < track->entry; i++) {
  1929. int64_t duration = i + 1 == track->entry ?
  1930. track->track_duration - track->cluster[i].dts + track->start_dts :
  1931. track->cluster[i + 1].dts - track->cluster[i].dts;
  1932. if (flags & 0x100)
  1933. avio_wb32(pb, duration);
  1934. if (flags & 0x200)
  1935. avio_wb32(pb, track->cluster[i].size);
  1936. if (flags & 0x400)
  1937. avio_wb32(pb, get_sample_flags(track, &track->cluster[i]));
  1938. if (flags & 0x800)
  1939. avio_wb32(pb, track->cluster[i].cts);
  1940. }
  1941. return update_size(pb, pos);
  1942. }
  1943. static int mov_write_tfxd_tag(AVIOContext *pb, MOVTrack *track)
  1944. {
  1945. int64_t pos = avio_tell(pb);
  1946. const uint8_t uuid[] = {
  1947. 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
  1948. 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
  1949. };
  1950. avio_wb32(pb, 0); /* size placeholder */
  1951. ffio_wfourcc(pb, "uuid");
  1952. avio_write(pb, uuid, sizeof(uuid));
  1953. avio_w8(pb, 1);
  1954. avio_wb24(pb, 0);
  1955. avio_wb64(pb, track->frag_start);
  1956. avio_wb64(pb, track->start_dts + track->track_duration -
  1957. track->cluster[0].dts);
  1958. return update_size(pb, pos);
  1959. }
  1960. static int mov_write_tfrf_tag(AVIOContext *pb, MOVMuxContext *mov,
  1961. MOVTrack *track, int entry)
  1962. {
  1963. int n = track->nb_frag_info - 1 - entry, i;
  1964. int size = 8 + 16 + 4 + 1 + 16*n;
  1965. const uint8_t uuid[] = {
  1966. 0xd4, 0x80, 0x7e, 0xf2, 0xca, 0x39, 0x46, 0x95,
  1967. 0x8e, 0x54, 0x26, 0xcb, 0x9e, 0x46, 0xa7, 0x9f
  1968. };
  1969. if (entry < 0)
  1970. return 0;
  1971. avio_seek(pb, track->frag_info[entry].tfrf_offset, SEEK_SET);
  1972. avio_wb32(pb, size);
  1973. ffio_wfourcc(pb, "uuid");
  1974. avio_write(pb, uuid, sizeof(uuid));
  1975. avio_w8(pb, 1);
  1976. avio_wb24(pb, 0);
  1977. avio_w8(pb, n);
  1978. for (i = 0; i < n; i++) {
  1979. int index = entry + 1 + i;
  1980. avio_wb64(pb, track->frag_info[index].time);
  1981. avio_wb64(pb, track->frag_info[index].duration);
  1982. }
  1983. if (n < mov->ism_lookahead) {
  1984. int free_size = 16*(mov->ism_lookahead - n);
  1985. avio_wb32(pb, free_size);
  1986. ffio_wfourcc(pb, "free");
  1987. for (i = 0; i < free_size - 8; i++)
  1988. avio_w8(pb, 0);
  1989. }
  1990. return 0;
  1991. }
  1992. static int mov_write_tfrf_tags(AVIOContext *pb, MOVMuxContext *mov,
  1993. MOVTrack *track)
  1994. {
  1995. int64_t pos = avio_tell(pb);
  1996. int i;
  1997. for (i = 0; i < mov->ism_lookahead; i++) {
  1998. /* Update the tfrf tag for the last ism_lookahead fragments,
  1999. * nb_frag_info - 1 is the next fragment to be written. */
  2000. mov_write_tfrf_tag(pb, mov, track, track->nb_frag_info - 2 - i);
  2001. }
  2002. avio_seek(pb, pos, SEEK_SET);
  2003. return 0;
  2004. }
  2005. static int mov_write_traf_tag(AVIOContext *pb, MOVMuxContext *mov,
  2006. MOVTrack *track, int64_t moof_offset)
  2007. {
  2008. int64_t pos = avio_tell(pb);
  2009. avio_wb32(pb, 0); /* size placeholder */
  2010. ffio_wfourcc(pb, "traf");
  2011. mov_write_tfhd_tag(pb, track, moof_offset);
  2012. mov_write_trun_tag(pb, track);
  2013. if (mov->mode == MODE_ISM) {
  2014. mov_write_tfxd_tag(pb, track);
  2015. if (mov->ism_lookahead) {
  2016. int i, size = 16 + 4 + 1 + 16*mov->ism_lookahead;
  2017. track->tfrf_offset = avio_tell(pb);
  2018. avio_wb32(pb, 8 + size);
  2019. ffio_wfourcc(pb, "free");
  2020. for (i = 0; i < size; i++)
  2021. avio_w8(pb, 0);
  2022. }
  2023. }
  2024. return update_size(pb, pos);
  2025. }
  2026. static int mov_write_moof_tag(AVIOContext *pb, MOVMuxContext *mov, int tracks)
  2027. {
  2028. int64_t pos = avio_tell(pb), end;
  2029. int i, moof_size;
  2030. avio_wb32(pb, 0); /* size placeholder */
  2031. ffio_wfourcc(pb, "moof");
  2032. mov_write_mfhd_tag(pb, mov);
  2033. for (i = 0; i < mov->nb_streams; i++) {
  2034. MOVTrack *track = &mov->tracks[i];
  2035. if (tracks >= 0 && i != tracks)
  2036. continue;
  2037. if (!track->entry)
  2038. continue;
  2039. mov_write_traf_tag(pb, mov, track, pos);
  2040. }
  2041. end = avio_tell(pb);
  2042. moof_size = end - pos;
  2043. for (i = 0; i < mov->nb_streams; i++) {
  2044. MOVTrack *track = &mov->tracks[i];
  2045. if (tracks >= 0 && i != tracks)
  2046. continue;
  2047. if (!track->entry)
  2048. continue;
  2049. avio_seek(pb, mov->tracks[i].moof_size_offset, SEEK_SET);
  2050. avio_wb32(pb, moof_size + 8 + mov->tracks[i].data_offset);
  2051. }
  2052. avio_seek(pb, end, SEEK_SET);
  2053. return update_size(pb, pos);
  2054. }
  2055. static int mov_write_tfra_tag(AVIOContext *pb, MOVTrack *track)
  2056. {
  2057. int64_t pos = avio_tell(pb);
  2058. int i;
  2059. avio_wb32(pb, 0); /* size placeholder */
  2060. ffio_wfourcc(pb, "tfra");
  2061. avio_w8(pb, 1); /* version */
  2062. avio_wb24(pb, 0);
  2063. avio_wb32(pb, track->track_id);
  2064. avio_wb32(pb, 0); /* length of traf/trun/sample num */
  2065. avio_wb32(pb, track->nb_frag_info);
  2066. for (i = 0; i < track->nb_frag_info; i++) {
  2067. avio_wb64(pb, track->frag_info[i].time);
  2068. avio_wb64(pb, track->frag_info[i].offset);
  2069. avio_w8(pb, 1); /* traf number */
  2070. avio_w8(pb, 1); /* trun number */
  2071. avio_w8(pb, 1); /* sample number */
  2072. }
  2073. return update_size(pb, pos);
  2074. }
  2075. static int mov_write_mfra_tag(AVIOContext *pb, MOVMuxContext *mov)
  2076. {
  2077. int64_t pos = avio_tell(pb);
  2078. int i;
  2079. avio_wb32(pb, 0); /* size placeholder */
  2080. ffio_wfourcc(pb, "mfra");
  2081. /* An empty mfra atom is enough to indicate to the publishing point that
  2082. * the stream has ended. */
  2083. if (mov->flags & FF_MOV_FLAG_ISML)
  2084. return update_size(pb, pos);
  2085. for (i = 0; i < mov->nb_streams; i++) {
  2086. MOVTrack *track = &mov->tracks[i];
  2087. if (track->nb_frag_info)
  2088. mov_write_tfra_tag(pb, track);
  2089. }
  2090. avio_wb32(pb, 16);
  2091. ffio_wfourcc(pb, "mfro");
  2092. avio_wb32(pb, 0); /* version + flags */
  2093. avio_wb32(pb, avio_tell(pb) + 4 - pos);
  2094. return update_size(pb, pos);
  2095. }
  2096. static int mov_write_mdat_tag(AVIOContext *pb, MOVMuxContext *mov)
  2097. {
  2098. avio_wb32(pb, 8); // placeholder for extended size field (64 bit)
  2099. ffio_wfourcc(pb, mov->mode == MODE_MOV ? "wide" : "free");
  2100. mov->mdat_pos = avio_tell(pb);
  2101. avio_wb32(pb, 0); /* size placeholder*/
  2102. ffio_wfourcc(pb, "mdat");
  2103. return 0;
  2104. }
  2105. /* TODO: This needs to be more general */
  2106. static int mov_write_ftyp_tag(AVIOContext *pb, AVFormatContext *s)
  2107. {
  2108. MOVMuxContext *mov = s->priv_data;
  2109. int64_t pos = avio_tell(pb);
  2110. int has_h264 = 0, has_video = 0;
  2111. int minor = 0x200;
  2112. int i;
  2113. for (i = 0; i < s->nb_streams; i++) {
  2114. AVStream *st = s->streams[i];
  2115. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  2116. has_video = 1;
  2117. if (st->codec->codec_id == CODEC_ID_H264)
  2118. has_h264 = 1;
  2119. }
  2120. avio_wb32(pb, 0); /* size */
  2121. ffio_wfourcc(pb, "ftyp");
  2122. if (mov->mode == MODE_3GP) {
  2123. ffio_wfourcc(pb, has_h264 ? "3gp6" : "3gp4");
  2124. minor = has_h264 ? 0x100 : 0x200;
  2125. } else if (mov->mode & MODE_3G2) {
  2126. ffio_wfourcc(pb, has_h264 ? "3g2b" : "3g2a");
  2127. minor = has_h264 ? 0x20000 : 0x10000;
  2128. }else if (mov->mode == MODE_PSP)
  2129. ffio_wfourcc(pb, "MSNV");
  2130. else if (mov->mode == MODE_MP4)
  2131. ffio_wfourcc(pb, "isom");
  2132. else if (mov->mode == MODE_IPOD)
  2133. ffio_wfourcc(pb, has_video ? "M4V ":"M4A ");
  2134. else if (mov->mode == MODE_ISM)
  2135. ffio_wfourcc(pb, "isml");
  2136. else
  2137. ffio_wfourcc(pb, "qt ");
  2138. avio_wb32(pb, minor);
  2139. if(mov->mode == MODE_MOV)
  2140. ffio_wfourcc(pb, "qt ");
  2141. else if (mov->mode == MODE_ISM) {
  2142. ffio_wfourcc(pb, "piff");
  2143. ffio_wfourcc(pb, "iso2");
  2144. } else {
  2145. ffio_wfourcc(pb, "isom");
  2146. ffio_wfourcc(pb, "iso2");
  2147. if(has_h264)
  2148. ffio_wfourcc(pb, "avc1");
  2149. }
  2150. if (mov->mode == MODE_3GP)
  2151. ffio_wfourcc(pb, has_h264 ? "3gp6":"3gp4");
  2152. else if (mov->mode & MODE_3G2)
  2153. ffio_wfourcc(pb, has_h264 ? "3g2b":"3g2a");
  2154. else if (mov->mode == MODE_PSP)
  2155. ffio_wfourcc(pb, "MSNV");
  2156. else if (mov->mode == MODE_MP4)
  2157. ffio_wfourcc(pb, "mp41");
  2158. return update_size(pb, pos);
  2159. }
  2160. static void mov_write_uuidprof_tag(AVIOContext *pb, AVFormatContext *s)
  2161. {
  2162. AVCodecContext *video_codec = s->streams[0]->codec;
  2163. AVCodecContext *audio_codec = s->streams[1]->codec;
  2164. int audio_rate = audio_codec->sample_rate;
  2165. int frame_rate = ((video_codec->time_base.den) * (0x10000))/ (video_codec->time_base.num);
  2166. int audio_kbitrate = audio_codec->bit_rate / 1000;
  2167. int video_kbitrate = FFMIN(video_codec->bit_rate / 1000, 800 - audio_kbitrate);
  2168. avio_wb32(pb, 0x94); /* size */
  2169. ffio_wfourcc(pb, "uuid");
  2170. ffio_wfourcc(pb, "PROF");
  2171. avio_wb32(pb, 0x21d24fce); /* 96 bit UUID */
  2172. avio_wb32(pb, 0xbb88695c);
  2173. avio_wb32(pb, 0xfac9c740);
  2174. avio_wb32(pb, 0x0); /* ? */
  2175. avio_wb32(pb, 0x3); /* 3 sections ? */
  2176. avio_wb32(pb, 0x14); /* size */
  2177. ffio_wfourcc(pb, "FPRF");
  2178. avio_wb32(pb, 0x0); /* ? */
  2179. avio_wb32(pb, 0x0); /* ? */
  2180. avio_wb32(pb, 0x0); /* ? */
  2181. avio_wb32(pb, 0x2c); /* size */
  2182. ffio_wfourcc(pb, "APRF");/* audio */
  2183. avio_wb32(pb, 0x0);
  2184. avio_wb32(pb, 0x2); /* TrackID */
  2185. ffio_wfourcc(pb, "mp4a");
  2186. avio_wb32(pb, 0x20f);
  2187. avio_wb32(pb, 0x0);
  2188. avio_wb32(pb, audio_kbitrate);
  2189. avio_wb32(pb, audio_kbitrate);
  2190. avio_wb32(pb, audio_rate);
  2191. avio_wb32(pb, audio_codec->channels);
  2192. avio_wb32(pb, 0x34); /* size */
  2193. ffio_wfourcc(pb, "VPRF"); /* video */
  2194. avio_wb32(pb, 0x0);
  2195. avio_wb32(pb, 0x1); /* TrackID */
  2196. if (video_codec->codec_id == CODEC_ID_H264) {
  2197. ffio_wfourcc(pb, "avc1");
  2198. avio_wb16(pb, 0x014D);
  2199. avio_wb16(pb, 0x0015);
  2200. } else {
  2201. ffio_wfourcc(pb, "mp4v");
  2202. avio_wb16(pb, 0x0000);
  2203. avio_wb16(pb, 0x0103);
  2204. }
  2205. avio_wb32(pb, 0x0);
  2206. avio_wb32(pb, video_kbitrate);
  2207. avio_wb32(pb, video_kbitrate);
  2208. avio_wb32(pb, frame_rate);
  2209. avio_wb32(pb, frame_rate);
  2210. avio_wb16(pb, video_codec->width);
  2211. avio_wb16(pb, video_codec->height);
  2212. avio_wb32(pb, 0x010001); /* ? */
  2213. }
  2214. static int mov_parse_mpeg2_frame(AVPacket *pkt, uint32_t *flags)
  2215. {
  2216. uint32_t c = -1;
  2217. int i, closed_gop = 0;
  2218. for (i = 0; i < pkt->size - 4; i++) {
  2219. c = (c<<8) + pkt->data[i];
  2220. if (c == 0x1b8) { // gop
  2221. closed_gop = pkt->data[i+4]>>6 & 0x01;
  2222. } else if (c == 0x100) { // pic
  2223. int temp_ref = (pkt->data[i+1]<<2) | (pkt->data[i+2]>>6);
  2224. if (!temp_ref || closed_gop) // I picture is not reordered
  2225. *flags = MOV_SYNC_SAMPLE;
  2226. else
  2227. *flags = MOV_PARTIAL_SYNC_SAMPLE;
  2228. break;
  2229. }
  2230. }
  2231. return 0;
  2232. }
  2233. static int mov_flush_fragment(AVFormatContext *s)
  2234. {
  2235. MOVMuxContext *mov = s->priv_data;
  2236. int i, first_track = -1;
  2237. int64_t mdat_size = 0;
  2238. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT))
  2239. return 0;
  2240. if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) && mov->fragments == 0) {
  2241. int64_t pos = avio_tell(s->pb);
  2242. for (i = 0; i < mov->nb_streams; i++)
  2243. if (!mov->tracks[i].entry)
  2244. break;
  2245. /* Don't write the initial moov unless all tracks have data */
  2246. if (i < mov->nb_streams)
  2247. return 0;
  2248. avio_seek(s->pb, mov->mdat_pos, SEEK_SET);
  2249. avio_wb32(s->pb, mov->mdat_size + 8);
  2250. avio_seek(s->pb, pos, SEEK_SET);
  2251. mov_write_moov_tag(s->pb, mov, s);
  2252. mov->fragments++;
  2253. mov->mdat_size = 0;
  2254. for (i = 0; i < mov->nb_streams; i++) {
  2255. if (mov->tracks[i].entry)
  2256. mov->tracks[i].frag_start += mov->tracks[i].start_dts +
  2257. mov->tracks[i].track_duration -
  2258. mov->tracks[i].cluster[0].dts;
  2259. mov->tracks[i].entry = 0;
  2260. }
  2261. avio_flush(s->pb);
  2262. return 0;
  2263. }
  2264. for (i = 0; i < mov->nb_streams; i++) {
  2265. MOVTrack *track = &mov->tracks[i];
  2266. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF)
  2267. track->data_offset = 0;
  2268. else
  2269. track->data_offset = mdat_size;
  2270. if (!track->mdat_buf)
  2271. continue;
  2272. mdat_size += avio_tell(track->mdat_buf);
  2273. if (first_track < 0)
  2274. first_track = i;
  2275. }
  2276. if (!mdat_size)
  2277. return 0;
  2278. for (i = 0; i < mov->nb_streams; i++) {
  2279. MOVTrack *track = &mov->tracks[i];
  2280. int buf_size, write_moof = 1, moof_tracks = -1;
  2281. uint8_t *buf;
  2282. int64_t duration = 0;
  2283. if (track->entry)
  2284. duration = track->start_dts + track->track_duration -
  2285. track->cluster[0].dts;
  2286. if (mov->flags & FF_MOV_FLAG_SEPARATE_MOOF) {
  2287. if (!track->mdat_buf)
  2288. continue;
  2289. mdat_size = avio_tell(track->mdat_buf);
  2290. moof_tracks = i;
  2291. } else {
  2292. write_moof = i == first_track;
  2293. }
  2294. if (write_moof) {
  2295. MOVFragmentInfo *info;
  2296. avio_flush(s->pb);
  2297. track->nb_frag_info++;
  2298. track->frag_info = av_realloc(track->frag_info,
  2299. sizeof(*track->frag_info) *
  2300. track->nb_frag_info);
  2301. info = &track->frag_info[track->nb_frag_info - 1];
  2302. info->offset = avio_tell(s->pb);
  2303. info->time = mov->tracks[i].frag_start;
  2304. info->duration = duration;
  2305. mov_write_tfrf_tags(s->pb, mov, track);
  2306. mov_write_moof_tag(s->pb, mov, moof_tracks);
  2307. info->tfrf_offset = track->tfrf_offset;
  2308. mov->fragments++;
  2309. avio_wb32(s->pb, mdat_size + 8);
  2310. ffio_wfourcc(s->pb, "mdat");
  2311. }
  2312. if (track->entry)
  2313. track->frag_start += duration;
  2314. track->entry = 0;
  2315. if (!track->mdat_buf)
  2316. continue;
  2317. buf_size = avio_close_dyn_buf(track->mdat_buf, &buf);
  2318. track->mdat_buf = NULL;
  2319. avio_write(s->pb, buf, buf_size);
  2320. av_free(buf);
  2321. }
  2322. mov->mdat_size = 0;
  2323. avio_flush(s->pb);
  2324. return 0;
  2325. }
  2326. static int mov_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
  2327. {
  2328. MOVMuxContext *mov = s->priv_data;
  2329. AVIOContext *pb = s->pb;
  2330. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  2331. AVCodecContext *enc = trk->enc;
  2332. unsigned int samples_in_chunk = 0;
  2333. int size= pkt->size;
  2334. uint8_t *reformatted_data = NULL;
  2335. if (!s->pb->seekable && !(mov->flags & FF_MOV_FLAG_EMPTY_MOOV))
  2336. return 0; /* Can't handle that */
  2337. if (!size) return 0; /* Discard 0 sized packets */
  2338. if ((mov->max_fragment_duration && trk->entry &&
  2339. av_rescale_q(pkt->dts - trk->cluster[0].dts,
  2340. s->streams[pkt->stream_index]->time_base,
  2341. AV_TIME_BASE_Q) >= mov->max_fragment_duration) ||
  2342. (mov->max_fragment_size && mov->mdat_size + size >= mov->max_fragment_size) ||
  2343. (mov->flags & FF_MOV_FLAG_FRAG_KEYFRAME &&
  2344. enc->codec_type == AVMEDIA_TYPE_VIDEO &&
  2345. trk->entry && pkt->flags & AV_PKT_FLAG_KEY)) {
  2346. mov_flush_fragment(s);
  2347. }
  2348. if (mov->flags & FF_MOV_FLAG_FRAGMENT && mov->fragments > 0) {
  2349. if (!trk->mdat_buf) {
  2350. int ret;
  2351. if ((ret = avio_open_dyn_buf(&trk->mdat_buf)) < 0)
  2352. return ret;
  2353. }
  2354. pb = trk->mdat_buf;
  2355. }
  2356. if (enc->codec_id == CODEC_ID_AMR_NB) {
  2357. /* We must find out how many AMR blocks there are in one packet */
  2358. static uint16_t packed_size[16] =
  2359. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 1};
  2360. int len = 0;
  2361. while (len < size && samples_in_chunk < 100) {
  2362. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  2363. samples_in_chunk++;
  2364. }
  2365. if (samples_in_chunk > 1) {
  2366. av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
  2367. return -1;
  2368. }
  2369. } else if (trk->sample_size)
  2370. samples_in_chunk = size / trk->sample_size;
  2371. else
  2372. samples_in_chunk = 1;
  2373. /* copy extradata if it exists */
  2374. if (trk->vos_len == 0 && enc->extradata_size > 0) {
  2375. trk->vos_len = enc->extradata_size;
  2376. trk->vos_data = av_malloc(trk->vos_len);
  2377. memcpy(trk->vos_data, enc->extradata, trk->vos_len);
  2378. }
  2379. if (enc->codec_id == CODEC_ID_H264 && trk->vos_len > 0 && *(uint8_t *)trk->vos_data != 1) {
  2380. /* from x264 or from bytestream h264 */
  2381. /* nal reformating needed */
  2382. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams) {
  2383. ff_avc_parse_nal_units_buf(pkt->data, &reformatted_data,
  2384. &size);
  2385. avio_write(pb, reformatted_data, size);
  2386. } else {
  2387. size = ff_avc_parse_nal_units(pb, pkt->data, pkt->size);
  2388. }
  2389. } else {
  2390. avio_write(pb, pkt->data, size);
  2391. }
  2392. if ((enc->codec_id == CODEC_ID_DNXHD ||
  2393. enc->codec_id == CODEC_ID_AC3) && !trk->vos_len) {
  2394. /* copy frame to create needed atoms */
  2395. trk->vos_len = size;
  2396. trk->vos_data = av_malloc(size);
  2397. if (!trk->vos_data)
  2398. return AVERROR(ENOMEM);
  2399. memcpy(trk->vos_data, pkt->data, size);
  2400. }
  2401. if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
  2402. trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
  2403. if (!trk->cluster)
  2404. return -1;
  2405. }
  2406. trk->cluster[trk->entry].pos = avio_tell(pb) - size;
  2407. trk->cluster[trk->entry].samples_in_chunk = samples_in_chunk;
  2408. trk->cluster[trk->entry].size = size;
  2409. trk->cluster[trk->entry].entries = samples_in_chunk;
  2410. trk->cluster[trk->entry].dts = pkt->dts;
  2411. if (!trk->entry && trk->start_dts != AV_NOPTS_VALUE) {
  2412. /* First packet of a new fragment. We already wrote the duration
  2413. * of the last packet of the previous fragment based on track_duration,
  2414. * which might not exactly match our dts. Therefore adjust the dts
  2415. * of this packet to be what the previous packets duration implies. */
  2416. trk->cluster[trk->entry].dts = trk->start_dts + trk->track_duration;
  2417. }
  2418. if (trk->start_dts == AV_NOPTS_VALUE)
  2419. trk->start_dts = pkt->dts;
  2420. trk->track_duration = pkt->dts - trk->start_dts + pkt->duration;
  2421. if (pkt->pts == AV_NOPTS_VALUE) {
  2422. av_log(s, AV_LOG_WARNING, "pts has no value\n");
  2423. pkt->pts = pkt->dts;
  2424. }
  2425. if (pkt->dts != pkt->pts)
  2426. trk->flags |= MOV_TRACK_CTTS;
  2427. trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
  2428. trk->cluster[trk->entry].flags = 0;
  2429. if (pkt->flags & AV_PKT_FLAG_KEY) {
  2430. if (mov->mode == MODE_MOV && enc->codec_id == CODEC_ID_MPEG2VIDEO &&
  2431. trk->entry > 0) { // force sync sample for the first key frame
  2432. mov_parse_mpeg2_frame(pkt, &trk->cluster[trk->entry].flags);
  2433. if (trk->cluster[trk->entry].flags & MOV_PARTIAL_SYNC_SAMPLE)
  2434. trk->flags |= MOV_TRACK_STPS;
  2435. } else {
  2436. trk->cluster[trk->entry].flags = MOV_SYNC_SAMPLE;
  2437. }
  2438. if (trk->cluster[trk->entry].flags & MOV_SYNC_SAMPLE)
  2439. trk->has_keyframes++;
  2440. }
  2441. trk->entry++;
  2442. trk->sample_count += samples_in_chunk;
  2443. mov->mdat_size += size;
  2444. avio_flush(pb);
  2445. if (trk->hint_track >= 0 && trk->hint_track < mov->nb_streams)
  2446. ff_mov_add_hinted_packet(s, pkt, trk->hint_track, trk->entry,
  2447. reformatted_data, size);
  2448. av_free(reformatted_data);
  2449. return 0;
  2450. }
  2451. int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  2452. {
  2453. if (!pkt) {
  2454. mov_flush_fragment(s);
  2455. return 1;
  2456. } else {
  2457. return mov_write_packet_internal(s, pkt);
  2458. }
  2459. }
  2460. // QuickTime chapters involve an additional text track with the chapter names
  2461. // as samples, and a tref pointing from the other tracks to the chapter one.
  2462. static void mov_create_chapter_track(AVFormatContext *s, int tracknum)
  2463. {
  2464. MOVMuxContext *mov = s->priv_data;
  2465. MOVTrack *track = &mov->tracks[tracknum];
  2466. AVPacket pkt = { .stream_index = tracknum, .flags = AV_PKT_FLAG_KEY };
  2467. int i, len;
  2468. track->mode = mov->mode;
  2469. track->tag = MKTAG('t','e','x','t');
  2470. track->timescale = MOV_TIMESCALE;
  2471. track->enc = avcodec_alloc_context3(NULL);
  2472. track->enc->codec_type = AVMEDIA_TYPE_SUBTITLE;
  2473. for (i = 0; i < s->nb_chapters; i++) {
  2474. AVChapter *c = s->chapters[i];
  2475. AVDictionaryEntry *t;
  2476. int64_t end = av_rescale_q(c->end, c->time_base, (AVRational){1,MOV_TIMESCALE});
  2477. pkt.pts = pkt.dts = av_rescale_q(c->start, c->time_base, (AVRational){1,MOV_TIMESCALE});
  2478. pkt.duration = end - pkt.dts;
  2479. if ((t = av_dict_get(c->metadata, "title", NULL, 0))) {
  2480. len = strlen(t->value);
  2481. pkt.size = len+2;
  2482. pkt.data = av_malloc(pkt.size);
  2483. AV_WB16(pkt.data, len);
  2484. memcpy(pkt.data+2, t->value, len);
  2485. ff_mov_write_packet(s, &pkt);
  2486. av_freep(&pkt.data);
  2487. }
  2488. }
  2489. }
  2490. static int mov_write_header(AVFormatContext *s)
  2491. {
  2492. AVIOContext *pb = s->pb;
  2493. MOVMuxContext *mov = s->priv_data;
  2494. AVDictionaryEntry *t;
  2495. int i, hint_track = 0;
  2496. /* Non-seekable output is ok if EMPTY_MOOV is set, or if using the ismv
  2497. * format (which sets EMPTY_MOOV later in this function). If ism_lookahead
  2498. * is enabled, we don't support non-seekable output at all. */
  2499. if (!s->pb->seekable &&
  2500. ((!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV) &&
  2501. !(s->oformat && !strcmp(s->oformat->name, "ismv")))
  2502. || mov->ism_lookahead)) {
  2503. av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
  2504. return -1;
  2505. }
  2506. /* Default mode == MP4 */
  2507. mov->mode = MODE_MP4;
  2508. if (s->oformat != NULL) {
  2509. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  2510. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
  2511. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  2512. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  2513. else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
  2514. else if (!strcmp("ismv",s->oformat->name)) mov->mode = MODE_ISM;
  2515. mov_write_ftyp_tag(pb,s);
  2516. if (mov->mode == MODE_PSP) {
  2517. if (s->nb_streams != 2) {
  2518. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  2519. return -1;
  2520. }
  2521. mov_write_uuidprof_tag(pb,s);
  2522. }
  2523. }
  2524. mov->nb_streams = s->nb_streams;
  2525. if (mov->mode & (MODE_MOV|MODE_IPOD) && s->nb_chapters)
  2526. mov->chapter_track = mov->nb_streams++;
  2527. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  2528. /* Add hint tracks for each audio and video stream */
  2529. hint_track = mov->nb_streams;
  2530. for (i = 0; i < s->nb_streams; i++) {
  2531. AVStream *st = s->streams[i];
  2532. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  2533. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  2534. mov->nb_streams++;
  2535. }
  2536. }
  2537. }
  2538. mov->tracks = av_mallocz(mov->nb_streams*sizeof(*mov->tracks));
  2539. if (!mov->tracks)
  2540. return AVERROR(ENOMEM);
  2541. for(i=0; i<s->nb_streams; i++){
  2542. AVStream *st= s->streams[i];
  2543. MOVTrack *track= &mov->tracks[i];
  2544. AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL,0);
  2545. track->enc = st->codec;
  2546. track->language = ff_mov_iso639_to_lang(lang?lang->value:"und", mov->mode!=MODE_MOV);
  2547. if (track->language < 0)
  2548. track->language = 0;
  2549. track->mode = mov->mode;
  2550. track->tag = mov_find_codec_tag(s, track);
  2551. if (!track->tag) {
  2552. av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
  2553. "codec not currently supported in container\n", i);
  2554. goto error;
  2555. }
  2556. /* If hinting of this track is enabled by a later hint track,
  2557. * this is updated. */
  2558. track->hint_track = -1;
  2559. track->start_dts = AV_NOPTS_VALUE;
  2560. if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
  2561. if (track->tag == MKTAG('m','x','3','p') || track->tag == MKTAG('m','x','3','n') ||
  2562. track->tag == MKTAG('m','x','4','p') || track->tag == MKTAG('m','x','4','n') ||
  2563. track->tag == MKTAG('m','x','5','p') || track->tag == MKTAG('m','x','5','n')) {
  2564. if (st->codec->width != 720 || (st->codec->height != 608 && st->codec->height != 512)) {
  2565. av_log(s, AV_LOG_ERROR, "D-10/IMX must use 720x608 or 720x512 video resolution\n");
  2566. goto error;
  2567. }
  2568. track->height = track->tag>>24 == 'n' ? 486 : 576;
  2569. }
  2570. track->timescale = st->codec->time_base.den;
  2571. if (track->mode == MODE_MOV && track->timescale > 100000)
  2572. av_log(s, AV_LOG_WARNING,
  2573. "WARNING codec timebase is very high. If duration is too long,\n"
  2574. "file may not be playable by quicktime. Specify a shorter timebase\n"
  2575. "or choose different container.\n");
  2576. }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO){
  2577. track->timescale = st->codec->sample_rate;
  2578. /* set sample_size for PCM and ADPCM */
  2579. if (av_get_bits_per_sample(st->codec->codec_id)) {
  2580. if (!st->codec->block_align) {
  2581. av_log(s, AV_LOG_ERROR, "track %d: codec block align is not set\n", i);
  2582. goto error;
  2583. }
  2584. track->sample_size = st->codec->block_align;
  2585. }
  2586. /* set audio_vbr for compressed audio */
  2587. if (av_get_bits_per_sample(st->codec->codec_id) < 8) {
  2588. if (!st->codec->frame_size && track->mode == MODE_MOV) {
  2589. av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
  2590. goto error;
  2591. }
  2592. track->audio_vbr = 1;
  2593. }
  2594. if (track->mode != MODE_MOV) {
  2595. if (track->timescale > UINT16_MAX) {
  2596. av_log(s, AV_LOG_ERROR, "track %d: output format does not support "
  2597. "sample rate %dhz\n", i, track->timescale);
  2598. goto error;
  2599. }
  2600. if (track->enc->codec_id == CODEC_ID_MP3 && track->timescale < 16000) {
  2601. av_log(s, AV_LOG_ERROR, "track %d: muxing mp3 at %dhz is not supported\n",
  2602. i, track->enc->sample_rate);
  2603. goto error;
  2604. }
  2605. }
  2606. }else if(st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE){
  2607. track->timescale = st->codec->time_base.den;
  2608. }
  2609. if (!track->height)
  2610. track->height = st->codec->height;
  2611. /* The ism specific timescale isn't mandatory, but is assumed by
  2612. * some tools, such as mp4split. */
  2613. if (mov->mode == MODE_ISM)
  2614. track->timescale = 10000000;
  2615. avpriv_set_pts_info(st, 64, 1, track->timescale);
  2616. /* copy extradata if it exists */
  2617. if (st->codec->extradata_size) {
  2618. track->vos_len = st->codec->extradata_size;
  2619. track->vos_data = av_malloc(track->vos_len);
  2620. memcpy(track->vos_data, st->codec->extradata, track->vos_len);
  2621. }
  2622. }
  2623. if (mov->mode == MODE_ISM) {
  2624. /* If no fragmentation options have been set, set a default. */
  2625. if (!(mov->flags & (FF_MOV_FLAG_FRAG_KEYFRAME |
  2626. FF_MOV_FLAG_FRAG_CUSTOM)) &&
  2627. !mov->max_fragment_duration && !mov->max_fragment_size)
  2628. mov->max_fragment_duration = 5000000;
  2629. mov->flags |= FF_MOV_FLAG_EMPTY_MOOV | FF_MOV_FLAG_SEPARATE_MOOF;
  2630. }
  2631. /* Set the FRAGMENT flag if any of the fragmentation methods are
  2632. * enabled. */
  2633. if (mov->max_fragment_duration || mov->max_fragment_size ||
  2634. mov->flags & (FF_MOV_FLAG_EMPTY_MOOV |
  2635. FF_MOV_FLAG_FRAG_KEYFRAME |
  2636. FF_MOV_FLAG_FRAG_CUSTOM))
  2637. mov->flags |= FF_MOV_FLAG_FRAGMENT;
  2638. if (!(mov->flags & FF_MOV_FLAG_EMPTY_MOOV))
  2639. mov_write_mdat_tag(pb, mov);
  2640. if (t = av_dict_get(s->metadata, "creation_time", NULL, 0))
  2641. mov->time = ff_iso8601_to_unix_time(t->value);
  2642. if (mov->time)
  2643. mov->time += 0x7C25B080; // 1970 based -> 1904 based
  2644. if (mov->chapter_track)
  2645. mov_create_chapter_track(s, mov->chapter_track);
  2646. if (mov->flags & FF_MOV_FLAG_RTP_HINT) {
  2647. /* Initialize the hint tracks for each audio and video stream */
  2648. for (i = 0; i < s->nb_streams; i++) {
  2649. AVStream *st = s->streams[i];
  2650. if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
  2651. st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
  2652. ff_mov_init_hinting(s, hint_track, i);
  2653. hint_track++;
  2654. }
  2655. }
  2656. }
  2657. avio_flush(pb);
  2658. if (mov->flags & FF_MOV_FLAG_ISML)
  2659. mov_write_isml_manifest(pb, mov);
  2660. if (mov->flags & FF_MOV_FLAG_EMPTY_MOOV) {
  2661. mov_write_moov_tag(pb, mov, s);
  2662. mov->fragments++;
  2663. }
  2664. return 0;
  2665. error:
  2666. av_freep(&mov->tracks);
  2667. return -1;
  2668. }
  2669. static int mov_write_trailer(AVFormatContext *s)
  2670. {
  2671. MOVMuxContext *mov = s->priv_data;
  2672. AVIOContext *pb = s->pb;
  2673. int res = 0;
  2674. int i;
  2675. int64_t moov_pos = avio_tell(pb);
  2676. if (!(mov->flags & FF_MOV_FLAG_FRAGMENT)) {
  2677. /* Write size of mdat tag */
  2678. if (mov->mdat_size + 8 <= UINT32_MAX) {
  2679. avio_seek(pb, mov->mdat_pos, SEEK_SET);
  2680. avio_wb32(pb, mov->mdat_size + 8);
  2681. } else {
  2682. /* overwrite 'wide' placeholder atom */
  2683. avio_seek(pb, mov->mdat_pos - 8, SEEK_SET);
  2684. /* special value: real atom size will be 64 bit value after
  2685. * tag field */
  2686. avio_wb32(pb, 1);
  2687. ffio_wfourcc(pb, "mdat");
  2688. avio_wb64(pb, mov->mdat_size + 16);
  2689. }
  2690. avio_seek(pb, moov_pos, SEEK_SET);
  2691. mov_write_moov_tag(pb, mov, s);
  2692. } else {
  2693. mov_flush_fragment(s);
  2694. mov_write_mfra_tag(pb, mov);
  2695. }
  2696. if (mov->chapter_track)
  2697. av_freep(&mov->tracks[mov->chapter_track].enc);
  2698. for (i=0; i<mov->nb_streams; i++) {
  2699. if (mov->tracks[i].tag == MKTAG('r','t','p',' '))
  2700. ff_mov_close_hinting(&mov->tracks[i]);
  2701. av_freep(&mov->tracks[i].cluster);
  2702. av_freep(&mov->tracks[i].frag_info);
  2703. if (mov->tracks[i].vos_len)
  2704. av_free(mov->tracks[i].vos_data);
  2705. }
  2706. avio_flush(pb);
  2707. av_freep(&mov->tracks);
  2708. return res;
  2709. }
  2710. #if CONFIG_MOV_MUXER
  2711. MOV_CLASS(mov)
  2712. AVOutputFormat ff_mov_muxer = {
  2713. .name = "mov",
  2714. .long_name = NULL_IF_CONFIG_SMALL("MOV format"),
  2715. .extensions = "mov",
  2716. .priv_data_size = sizeof(MOVMuxContext),
  2717. .audio_codec = CODEC_ID_AAC,
  2718. #if CONFIG_LIBX264_ENCODER
  2719. .video_codec = CODEC_ID_H264,
  2720. #else
  2721. .video_codec = CODEC_ID_MPEG4,
  2722. #endif
  2723. .write_header = mov_write_header,
  2724. .write_packet = ff_mov_write_packet,
  2725. .write_trailer = mov_write_trailer,
  2726. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2727. .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
  2728. .priv_class = &mov_muxer_class,
  2729. };
  2730. #endif
  2731. #if CONFIG_TGP_MUXER
  2732. MOV_CLASS(tgp)
  2733. AVOutputFormat ff_tgp_muxer = {
  2734. .name = "3gp",
  2735. .long_name = NULL_IF_CONFIG_SMALL("3GP format"),
  2736. .extensions = "3gp",
  2737. .priv_data_size = sizeof(MOVMuxContext),
  2738. .audio_codec = CODEC_ID_AMR_NB,
  2739. .video_codec = CODEC_ID_H263,
  2740. .write_header = mov_write_header,
  2741. .write_packet = ff_mov_write_packet,
  2742. .write_trailer = mov_write_trailer,
  2743. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2744. .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
  2745. .priv_class = &tgp_muxer_class,
  2746. };
  2747. #endif
  2748. #if CONFIG_MP4_MUXER
  2749. MOV_CLASS(mp4)
  2750. AVOutputFormat ff_mp4_muxer = {
  2751. .name = "mp4",
  2752. .long_name = NULL_IF_CONFIG_SMALL("MP4 format"),
  2753. .mime_type = "application/mp4",
  2754. .extensions = "mp4",
  2755. .priv_data_size = sizeof(MOVMuxContext),
  2756. .audio_codec = CODEC_ID_AAC,
  2757. #if CONFIG_LIBX264_ENCODER
  2758. .video_codec = CODEC_ID_H264,
  2759. #else
  2760. .video_codec = CODEC_ID_MPEG4,
  2761. #endif
  2762. .write_header = mov_write_header,
  2763. .write_packet = ff_mov_write_packet,
  2764. .write_trailer = mov_write_trailer,
  2765. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2766. .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
  2767. .priv_class = &mp4_muxer_class,
  2768. };
  2769. #endif
  2770. #if CONFIG_PSP_MUXER
  2771. MOV_CLASS(psp)
  2772. AVOutputFormat ff_psp_muxer = {
  2773. .name = "psp",
  2774. .long_name = NULL_IF_CONFIG_SMALL("PSP MP4 format"),
  2775. .extensions = "mp4,psp",
  2776. .priv_data_size = sizeof(MOVMuxContext),
  2777. .audio_codec = CODEC_ID_AAC,
  2778. #if CONFIG_LIBX264_ENCODER
  2779. .video_codec = CODEC_ID_H264,
  2780. #else
  2781. .video_codec = CODEC_ID_MPEG4,
  2782. #endif
  2783. .write_header = mov_write_header,
  2784. .write_packet = ff_mov_write_packet,
  2785. .write_trailer = mov_write_trailer,
  2786. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2787. .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
  2788. .priv_class = &psp_muxer_class,
  2789. };
  2790. #endif
  2791. #if CONFIG_TG2_MUXER
  2792. MOV_CLASS(tg2)
  2793. AVOutputFormat ff_tg2_muxer = {
  2794. .name = "3g2",
  2795. .long_name = NULL_IF_CONFIG_SMALL("3GP2 format"),
  2796. .extensions = "3g2",
  2797. .priv_data_size = sizeof(MOVMuxContext),
  2798. .audio_codec = CODEC_ID_AMR_NB,
  2799. .video_codec = CODEC_ID_H263,
  2800. .write_header = mov_write_header,
  2801. .write_packet = ff_mov_write_packet,
  2802. .write_trailer = mov_write_trailer,
  2803. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2804. .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
  2805. .priv_class = &tg2_muxer_class,
  2806. };
  2807. #endif
  2808. #if CONFIG_IPOD_MUXER
  2809. MOV_CLASS(ipod)
  2810. AVOutputFormat ff_ipod_muxer = {
  2811. .name = "ipod",
  2812. .long_name = NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
  2813. .mime_type = "application/mp4",
  2814. .extensions = "m4v,m4a",
  2815. .priv_data_size = sizeof(MOVMuxContext),
  2816. .audio_codec = CODEC_ID_AAC,
  2817. .video_codec = CODEC_ID_H264,
  2818. .write_header = mov_write_header,
  2819. .write_packet = ff_mov_write_packet,
  2820. .write_trailer = mov_write_trailer,
  2821. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2822. .codec_tag = (const AVCodecTag* const []){codec_ipod_tags, 0},
  2823. .priv_class = &ipod_muxer_class,
  2824. };
  2825. #endif
  2826. #if CONFIG_ISMV_MUXER
  2827. MOV_CLASS(ismv)
  2828. AVOutputFormat ff_ismv_muxer = {
  2829. .name = "ismv",
  2830. .long_name = NULL_IF_CONFIG_SMALL("ISMV/ISMA (Smooth Streaming) format"),
  2831. .mime_type = "application/mp4",
  2832. .extensions = "ismv,isma",
  2833. .priv_data_size = sizeof(MOVMuxContext),
  2834. .audio_codec = CODEC_ID_AAC,
  2835. .video_codec = CODEC_ID_H264,
  2836. .write_header = mov_write_header,
  2837. .write_packet = ff_mov_write_packet,
  2838. .write_trailer = mov_write_trailer,
  2839. .flags = AVFMT_GLOBALHEADER | AVFMT_ALLOW_FLUSH,
  2840. .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
  2841. .priv_class = &ismv_muxer_class,
  2842. };
  2843. #endif