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.

3169 lines
108KB

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