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.

1789 lines
57KB

  1. /*
  2. * MOV, 3GP, MP4 muxer
  3. * Copyright (c) 2003 Thomas Raivio.
  4. * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avformat.h"
  23. #include "riff.h"
  24. #include "avio.h"
  25. #include "isom.h"
  26. #include "avc.h"
  27. #undef NDEBUG
  28. #include <assert.h>
  29. #define MOV_INDEX_CLUSTER_SIZE 16384
  30. #define globalTimescale 1000
  31. #define MODE_MP4 0x01
  32. #define MODE_MOV 0x02
  33. #define MODE_3GP 0x04
  34. #define MODE_PSP 0x08 // example working PSP command line:
  35. // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
  36. #define MODE_3G2 0x10
  37. #define MODE_IPOD 0x20
  38. typedef struct MOVIentry {
  39. unsigned int flags, size;
  40. uint64_t pos;
  41. unsigned int samplesInChunk;
  42. char key_frame;
  43. unsigned int entries;
  44. int64_t cts;
  45. int64_t dts;
  46. } MOVIentry;
  47. typedef struct MOVIndex {
  48. int mode;
  49. int entry;
  50. long timescale;
  51. long time;
  52. int64_t trackDuration;
  53. long sampleCount;
  54. long sampleSize;
  55. int hasKeyframes;
  56. int hasBframes;
  57. int language;
  58. int trackID;
  59. int tag; ///< stsd fourcc
  60. AVCodecContext *enc;
  61. int vosLen;
  62. uint8_t *vosData;
  63. MOVIentry *cluster;
  64. int audio_vbr;
  65. } MOVTrack;
  66. typedef struct MOVContext {
  67. int mode;
  68. int64_t time;
  69. int nb_streams;
  70. offset_t mdat_pos;
  71. uint64_t mdat_size;
  72. long timescale;
  73. MOVTrack tracks[MAX_STREAMS];
  74. } MOVContext;
  75. //FIXME support 64 bit variant with wide placeholders
  76. static offset_t updateSize(ByteIOContext *pb, offset_t pos)
  77. {
  78. offset_t curpos = url_ftell(pb);
  79. url_fseek(pb, pos, SEEK_SET);
  80. put_be32(pb, curpos - pos); /* rewrite size */
  81. url_fseek(pb, curpos, SEEK_SET);
  82. return curpos - pos;
  83. }
  84. /* Chunk offset atom */
  85. static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack *track)
  86. {
  87. int i;
  88. int mode64 = 0; // use 32 bit size variant if possible
  89. offset_t pos = url_ftell(pb);
  90. put_be32(pb, 0); /* size */
  91. if (pos > UINT32_MAX) {
  92. mode64 = 1;
  93. put_tag(pb, "co64");
  94. } else
  95. put_tag(pb, "stco");
  96. put_be32(pb, 0); /* version & flags */
  97. put_be32(pb, track->entry); /* entry count */
  98. for (i=0; i<track->entry; i++) {
  99. if(mode64 == 1)
  100. put_be64(pb, track->cluster[i].pos);
  101. else
  102. put_be32(pb, track->cluster[i].pos);
  103. }
  104. return updateSize(pb, pos);
  105. }
  106. /* Sample size atom */
  107. static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack *track)
  108. {
  109. int equalChunks = 1;
  110. int i, j, entries = 0, tst = -1, oldtst = -1;
  111. offset_t pos = url_ftell(pb);
  112. put_be32(pb, 0); /* size */
  113. put_tag(pb, "stsz");
  114. put_be32(pb, 0); /* version & flags */
  115. for (i=0; i<track->entry; i++) {
  116. tst = track->cluster[i].size/track->cluster[i].entries;
  117. if(oldtst != -1 && tst != oldtst) {
  118. equalChunks = 0;
  119. }
  120. oldtst = tst;
  121. entries += track->cluster[i].entries;
  122. }
  123. if (equalChunks) {
  124. int sSize = track->cluster[0].size/track->cluster[0].entries;
  125. put_be32(pb, sSize); // sample size
  126. put_be32(pb, entries); // sample count
  127. }
  128. else {
  129. put_be32(pb, 0); // sample size
  130. put_be32(pb, entries); // sample count
  131. for (i=0; i<track->entry; i++) {
  132. for (j=0; j<track->cluster[i].entries; j++) {
  133. put_be32(pb, track->cluster[i].size /
  134. track->cluster[i].entries);
  135. }
  136. }
  137. }
  138. return updateSize(pb, pos);
  139. }
  140. /* Sample to chunk atom */
  141. static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack *track)
  142. {
  143. int index = 0, oldval = -1, i;
  144. offset_t entryPos, curpos;
  145. offset_t pos = url_ftell(pb);
  146. put_be32(pb, 0); /* size */
  147. put_tag(pb, "stsc");
  148. put_be32(pb, 0); // version & flags
  149. entryPos = url_ftell(pb);
  150. put_be32(pb, track->entry); // entry count
  151. for (i=0; i<track->entry; i++) {
  152. if(oldval != track->cluster[i].samplesInChunk)
  153. {
  154. put_be32(pb, i+1); // first chunk
  155. put_be32(pb, track->cluster[i].samplesInChunk); // samples per chunk
  156. put_be32(pb, 0x1); // sample description index
  157. oldval = track->cluster[i].samplesInChunk;
  158. index++;
  159. }
  160. }
  161. curpos = url_ftell(pb);
  162. url_fseek(pb, entryPos, SEEK_SET);
  163. put_be32(pb, index); // rewrite size
  164. url_fseek(pb, curpos, SEEK_SET);
  165. return updateSize(pb, pos);
  166. }
  167. /* Sync sample atom */
  168. static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack *track)
  169. {
  170. offset_t curpos, entryPos;
  171. int i, index = 0;
  172. offset_t pos = url_ftell(pb);
  173. put_be32(pb, 0); // size
  174. put_tag(pb, "stss");
  175. put_be32(pb, 0); // version & flags
  176. entryPos = url_ftell(pb);
  177. put_be32(pb, track->entry); // entry count
  178. for (i=0; i<track->entry; i++) {
  179. if(track->cluster[i].key_frame == 1) {
  180. put_be32(pb, i+1);
  181. index++;
  182. }
  183. }
  184. curpos = url_ftell(pb);
  185. url_fseek(pb, entryPos, SEEK_SET);
  186. put_be32(pb, index); // rewrite size
  187. url_fseek(pb, curpos, SEEK_SET);
  188. return updateSize(pb, pos);
  189. }
  190. static int mov_write_amr_tag(ByteIOContext *pb, MOVTrack *track)
  191. {
  192. put_be32(pb, 0x11); /* size */
  193. if (track->mode == MODE_MOV) put_tag(pb, "samr");
  194. else put_tag(pb, "damr");
  195. put_tag(pb, "FFMP");
  196. put_byte(pb, 0); /* decoder version */
  197. put_be16(pb, 0x81FF); /* Mode set (all modes for AMR_NB) */
  198. put_byte(pb, 0x00); /* Mode change period (no restriction) */
  199. put_byte(pb, 0x01); /* Frames per sample */
  200. return 0x11;
  201. }
  202. /**
  203. * This function writes extradata "as is".
  204. * Extradata must be formated like a valid atom (with size and tag)
  205. */
  206. static int mov_write_extradata_tag(ByteIOContext *pb, MOVTrack *track)
  207. {
  208. put_buffer(pb, track->enc->extradata, track->enc->extradata_size);
  209. return track->enc->extradata_size;
  210. }
  211. static int mov_write_enda_tag(ByteIOContext *pb)
  212. {
  213. put_be32(pb, 10);
  214. put_tag(pb, "enda");
  215. put_be16(pb, 1); /* little endian */
  216. return 10;
  217. }
  218. static unsigned int descrLength(unsigned int len)
  219. {
  220. int i;
  221. for(i=1; len>>(7*i); i++);
  222. return len + 1 + i;
  223. }
  224. static void putDescr(ByteIOContext *pb, int tag, unsigned int size)
  225. {
  226. int i= descrLength(size) - size - 2;
  227. put_byte(pb, tag);
  228. for(; i>0; i--)
  229. put_byte(pb, (size>>(7*i)) | 0x80);
  230. put_byte(pb, size & 0x7F);
  231. }
  232. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack *track) // Basic
  233. {
  234. offset_t pos = url_ftell(pb);
  235. int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  236. put_be32(pb, 0); // size
  237. put_tag(pb, "esds");
  238. put_be32(pb, 0); // Version
  239. // ES descriptor
  240. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  241. descrLength(1));
  242. put_be16(pb, track->trackID);
  243. put_byte(pb, 0x00); // flags (= no flags)
  244. // DecoderConfig descriptor
  245. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  246. // Object type indication
  247. put_byte(pb, codec_get_tag(ff_mp4_obj_type, track->enc->codec_id));
  248. // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
  249. // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
  250. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  251. put_byte(pb, 0x15); // flags (= Audiostream)
  252. else
  253. put_byte(pb, 0x11); // flags (= Visualstream)
  254. put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
  255. put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
  256. put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
  257. if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
  258. put_be32(pb, 0); // vbr
  259. else
  260. put_be32(pb, track->enc->rc_max_rate); // avg bitrate
  261. if (track->vosLen) {
  262. // DecoderSpecific info descriptor
  263. putDescr(pb, 0x05, track->vosLen);
  264. put_buffer(pb, track->vosData, track->vosLen);
  265. }
  266. // SL descriptor
  267. putDescr(pb, 0x06, 1);
  268. put_byte(pb, 0x02);
  269. return updateSize(pb, pos);
  270. }
  271. static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack *track)
  272. {
  273. offset_t pos = url_ftell(pb);
  274. put_be32(pb, 0); /* size */
  275. put_tag(pb, "wave");
  276. put_be32(pb, 12); /* size */
  277. put_tag(pb, "frma");
  278. put_le32(pb, track->tag);
  279. if (track->enc->codec_id == CODEC_ID_AAC) {
  280. /* useless atom needed by mplayer, ipod, not needed by quicktime */
  281. put_be32(pb, 12); /* size */
  282. put_tag(pb, "mp4a");
  283. put_be32(pb, 0);
  284. mov_write_esds_tag(pb, track);
  285. } else if (track->enc->codec_id == CODEC_ID_PCM_S24LE ||
  286. track->enc->codec_id == CODEC_ID_PCM_S32LE) {
  287. mov_write_enda_tag(pb);
  288. } else if (track->enc->codec_id == CODEC_ID_AMR_NB) {
  289. mov_write_amr_tag(pb, track);
  290. } else if (track->enc->codec_id == CODEC_ID_ALAC) {
  291. mov_write_extradata_tag(pb, track);
  292. }
  293. put_be32(pb, 8); /* size */
  294. put_be32(pb, 0); /* null tag */
  295. return updateSize(pb, pos);
  296. }
  297. static int mov_write_glbl_tag(ByteIOContext *pb, MOVTrack *track)
  298. {
  299. put_be32(pb, track->vosLen+8);
  300. put_tag(pb, "glbl");
  301. put_buffer(pb, track->vosData, track->vosLen);
  302. return 8+track->vosLen;
  303. }
  304. static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack *track)
  305. {
  306. offset_t pos = url_ftell(pb);
  307. int version = track->mode == MODE_MOV &&
  308. (track->audio_vbr ||
  309. track->enc->codec_id == CODEC_ID_PCM_S32LE ||
  310. track->enc->codec_id == CODEC_ID_PCM_S24LE);
  311. put_be32(pb, 0); /* size */
  312. put_le32(pb, track->tag); // store it byteswapped
  313. put_be32(pb, 0); /* Reserved */
  314. put_be16(pb, 0); /* Reserved */
  315. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  316. /* SoundDescription */
  317. put_be16(pb, version); /* Version */
  318. put_be16(pb, 0); /* Revision level */
  319. put_be32(pb, 0); /* Reserved */
  320. if (track->mode == MODE_MOV) {
  321. put_be16(pb, track->enc->channels);
  322. if (track->enc->codec_id == CODEC_ID_PCM_U8 ||
  323. track->enc->codec_id == CODEC_ID_PCM_S8)
  324. put_be16(pb, 8); /* bits per sample */
  325. else
  326. put_be16(pb, 16);
  327. put_be16(pb, track->audio_vbr ? -2 : 0); /* compression ID */
  328. } else { /* reserved for mp4/3gp */
  329. put_be16(pb, 2);
  330. put_be16(pb, 16);
  331. put_be16(pb, 0);
  332. }
  333. put_be16(pb, 0); /* packet size (= 0) */
  334. put_be16(pb, track->timescale); /* Time scale */
  335. put_be16(pb, 0); /* Reserved */
  336. if(version == 1) { /* SoundDescription V1 extended info */
  337. put_be32(pb, track->enc->frame_size); /* Samples per packet */
  338. put_be32(pb, track->sampleSize / track->enc->channels); /* Bytes per packet */
  339. put_be32(pb, track->sampleSize); /* Bytes per frame */
  340. put_be32(pb, 2); /* Bytes per sample */
  341. }
  342. if(track->mode == MODE_MOV &&
  343. (track->enc->codec_id == CODEC_ID_AAC ||
  344. track->enc->codec_id == CODEC_ID_AMR_NB ||
  345. track->enc->codec_id == CODEC_ID_PCM_S24LE ||
  346. track->enc->codec_id == CODEC_ID_PCM_S32LE ||
  347. track->enc->codec_id == CODEC_ID_ALAC))
  348. mov_write_wave_tag(pb, track);
  349. else if(track->tag == MKTAG('m','p','4','a'))
  350. mov_write_esds_tag(pb, track);
  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_ALAC)
  354. mov_write_extradata_tag(pb, track);
  355. else if(track->vosLen > 0)
  356. mov_write_glbl_tag(pb, track);
  357. return updateSize(pb, pos);
  358. }
  359. static int mov_write_d263_tag(ByteIOContext *pb)
  360. {
  361. put_be32(pb, 0xf); /* size */
  362. put_tag(pb, "d263");
  363. put_tag(pb, "FFMP");
  364. put_byte(pb, 0); /* decoder version */
  365. /* FIXME use AVCodecContext level/profile, when encoder will set values */
  366. put_byte(pb, 0xa); /* level */
  367. put_byte(pb, 0); /* profile */
  368. return 0xf;
  369. }
  370. /* TODO: No idea about these values */
  371. static int mov_write_svq3_tag(ByteIOContext *pb)
  372. {
  373. put_be32(pb, 0x15);
  374. put_tag(pb, "SMI ");
  375. put_tag(pb, "SEQH");
  376. put_be32(pb, 0x5);
  377. put_be32(pb, 0xe2c0211d);
  378. put_be32(pb, 0xc0000000);
  379. put_byte(pb, 0);
  380. return 0x15;
  381. }
  382. static int mov_write_avcc_tag(ByteIOContext *pb, MOVTrack *track)
  383. {
  384. offset_t pos = url_ftell(pb);
  385. put_be32(pb, 0);
  386. put_tag(pb, "avcC");
  387. ff_isom_write_avcc(pb, track->vosData, track->vosLen);
  388. return updateSize(pb, pos);
  389. }
  390. /* also used by all avid codecs (dv, imx, meridien) and their variants */
  391. static int mov_write_avid_tag(ByteIOContext *pb, MOVTrack *track)
  392. {
  393. int i;
  394. put_be32(pb, 24); /* size */
  395. put_tag(pb, "ACLR");
  396. put_tag(pb, "ACLR");
  397. put_tag(pb, "0001");
  398. put_be32(pb, 1); /* yuv 1 / rgb 2 ? */
  399. put_be32(pb, 0); /* unknown */
  400. put_be32(pb, 24); /* size */
  401. put_tag(pb, "APRG");
  402. put_tag(pb, "APRG");
  403. put_tag(pb, "0001");
  404. put_be32(pb, 1); /* unknown */
  405. put_be32(pb, 0); /* unknown */
  406. put_be32(pb, 120); /* size */
  407. put_tag(pb, "ARES");
  408. put_tag(pb, "ARES");
  409. put_tag(pb, "0001");
  410. put_be32(pb, AV_RB32(track->vosData + 0x28)); /* dnxhd cid, some id ? */
  411. put_be32(pb, track->enc->width);
  412. /* values below are based on samples created with quicktime and avid codecs */
  413. if (track->vosData[5] & 2) { // interlaced
  414. put_be32(pb, track->enc->height/2);
  415. put_be32(pb, 2); /* unknown */
  416. put_be32(pb, 0); /* unknown */
  417. put_be32(pb, 4); /* unknown */
  418. } else {
  419. put_be32(pb, track->enc->height);
  420. put_be32(pb, 1); /* unknown */
  421. put_be32(pb, 0); /* unknown */
  422. if (track->enc->height == 1080)
  423. put_be32(pb, 5); /* unknown */
  424. else
  425. put_be32(pb, 6); /* unknown */
  426. }
  427. /* padding */
  428. for (i = 0; i < 10; i++)
  429. put_be64(pb, 0);
  430. /* extra padding for stsd needed */
  431. put_be32(pb, 0);
  432. return 0;
  433. }
  434. static const AVCodecTag codec_3gp_tags[] = {
  435. { CODEC_ID_H263, MKTAG('s','2','6','3') },
  436. { CODEC_ID_H264, MKTAG('a','v','c','1') },
  437. { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  438. { CODEC_ID_AAC, MKTAG('m','p','4','a') },
  439. { CODEC_ID_AMR_NB, MKTAG('s','a','m','r') },
  440. { CODEC_ID_AMR_WB, MKTAG('s','a','w','b') },
  441. };
  442. static const AVCodecTag mov_pix_fmt_tags[] = {
  443. { PIX_FMT_YUYV422, MKTAG('y','u','v','s') },
  444. { PIX_FMT_UYVY422, MKTAG('2','v','u','y') },
  445. { PIX_FMT_BGR555, MKTAG('r','a','w',' ') },
  446. { PIX_FMT_RGB24, MKTAG('r','a','w',' ') },
  447. { PIX_FMT_BGR32_1, MKTAG('r','a','w',' ') },
  448. };
  449. static const AVCodecTag codec_ipod_tags[] = {
  450. { CODEC_ID_H264, MKTAG('a','v','c','1') },
  451. { CODEC_ID_MPEG4, MKTAG('m','p','4','v') },
  452. { CODEC_ID_AAC, MKTAG('m','p','4','a') },
  453. { CODEC_ID_ALAC, MKTAG('a','l','a','c') },
  454. };
  455. static int mov_find_codec_tag(AVFormatContext *s, MOVTrack *track)
  456. {
  457. int tag = track->enc->codec_tag;
  458. if (track->mode == MODE_MP4 || track->mode == MODE_PSP) {
  459. if (!codec_get_tag(ff_mp4_obj_type, track->enc->codec_id))
  460. return 0;
  461. if (track->enc->codec_id == CODEC_ID_H264) tag = MKTAG('a','v','c','1');
  462. else if (track->enc->codec_type == CODEC_TYPE_VIDEO) tag = MKTAG('m','p','4','v');
  463. else if (track->enc->codec_type == CODEC_TYPE_AUDIO) tag = MKTAG('m','p','4','a');
  464. } else if (track->mode == MODE_IPOD) {
  465. tag = codec_get_tag(codec_ipod_tags, track->enc->codec_id);
  466. if (!match_ext(s->filename, "m4a") && !match_ext(s->filename, "m4v"))
  467. av_log(s, AV_LOG_WARNING, "Warning, extension is not .m4a nor .m4v "
  468. "Quicktime/Ipod might not play the file\n");
  469. } else if (track->mode & MODE_3GP) {
  470. tag = codec_get_tag(codec_3gp_tags, track->enc->codec_id);
  471. } else if (!tag || (track->enc->strict_std_compliance >= FF_COMPLIANCE_NORMAL &&
  472. (tag == MKTAG('d','v','c','p') ||
  473. track->enc->codec_id == CODEC_ID_RAWVIDEO))) {
  474. if (track->enc->codec_id == CODEC_ID_DVVIDEO) {
  475. if (track->enc->height == 480) /* NTSC */
  476. if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','n');
  477. else tag = MKTAG('d','v','c',' ');
  478. else if (track->enc->pix_fmt == PIX_FMT_YUV422P) tag = MKTAG('d','v','5','p');
  479. else if (track->enc->pix_fmt == PIX_FMT_YUV420P) tag = MKTAG('d','v','c','p');
  480. else tag = MKTAG('d','v','p','p');
  481. } else if (track->enc->codec_id == CODEC_ID_RAWVIDEO) {
  482. tag = codec_get_tag(mov_pix_fmt_tags, track->enc->pix_fmt);
  483. if (!tag) // restore tag
  484. tag = track->enc->codec_tag;
  485. } else {
  486. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  487. tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
  488. if (!tag) { // if no mac fcc found, try with Microsoft tags
  489. tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
  490. if (tag)
  491. av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, "
  492. "the file may be unplayable!\n");
  493. }
  494. } else if (track->enc->codec_type == CODEC_TYPE_AUDIO) {
  495. tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
  496. if (!tag) { // if no mac fcc found, try with Microsoft tags
  497. int ms_tag = codec_get_tag(codec_wav_tags, track->enc->codec_id);
  498. if (ms_tag) {
  499. tag = MKTAG('m', 's', ((ms_tag >> 8) & 0xff), (ms_tag & 0xff));
  500. av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, "
  501. "the file may be unplayable!\n");
  502. }
  503. }
  504. }
  505. }
  506. }
  507. return tag;
  508. }
  509. /** Write uuid atom.
  510. * Needed to make file play in iPods running newest firmware
  511. * goes after avcC atom in moov.trak.mdia.minf.stbl.stsd.avc1
  512. */
  513. static int mov_write_uuid_tag_ipod(ByteIOContext *pb)
  514. {
  515. put_be32(pb, 28);
  516. put_tag(pb, "uuid");
  517. put_be32(pb, 0x6b6840f2);
  518. put_be32(pb, 0x5f244fc5);
  519. put_be32(pb, 0xba39a51b);
  520. put_be32(pb, 0xcf0323f3);
  521. put_be32(pb, 0x0);
  522. return 28;
  523. }
  524. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack *track)
  525. {
  526. offset_t pos = url_ftell(pb);
  527. char compressor_name[32];
  528. put_be32(pb, 0); /* size */
  529. put_le32(pb, track->tag); // store it byteswapped
  530. put_be32(pb, 0); /* Reserved */
  531. put_be16(pb, 0); /* Reserved */
  532. put_be16(pb, 1); /* Data-reference index */
  533. put_be16(pb, 0); /* Codec stream version */
  534. put_be16(pb, 0); /* Codec stream revision (=0) */
  535. if (track->mode == MODE_MOV) {
  536. put_tag(pb, "FFMP"); /* Vendor */
  537. if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
  538. put_be32(pb, 0); /* Temporal Quality */
  539. put_be32(pb, 0x400); /* Spatial Quality = lossless*/
  540. } else {
  541. put_be32(pb, 0x200); /* Temporal Quality = normal */
  542. put_be32(pb, 0x200); /* Spatial Quality = normal */
  543. }
  544. } else {
  545. put_be32(pb, 0); /* Reserved */
  546. put_be32(pb, 0); /* Reserved */
  547. put_be32(pb, 0); /* Reserved */
  548. }
  549. put_be16(pb, track->enc->width); /* Video width */
  550. put_be16(pb, track->enc->height); /* Video height */
  551. put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
  552. put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
  553. put_be32(pb, 0); /* Data size (= 0) */
  554. put_be16(pb, 1); /* Frame count (= 1) */
  555. memset(compressor_name,0,32);
  556. /* FIXME not sure, ISO 14496-1 draft where it shall be set to 0 */
  557. if (track->mode == MODE_MOV && track->enc->codec && track->enc->codec->name)
  558. strncpy(compressor_name,track->enc->codec->name,31);
  559. put_byte(pb, strlen(compressor_name));
  560. put_buffer(pb, compressor_name, 31);
  561. if (track->mode == MODE_MOV && track->enc->bits_per_sample)
  562. put_be16(pb, track->enc->bits_per_sample);
  563. else
  564. put_be16(pb, 0x18); /* Reserved */
  565. put_be16(pb, 0xffff); /* Reserved */
  566. if(track->tag == MKTAG('m','p','4','v'))
  567. mov_write_esds_tag(pb, track);
  568. else if(track->enc->codec_id == CODEC_ID_H263)
  569. mov_write_d263_tag(pb);
  570. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  571. mov_write_svq3_tag(pb);
  572. else if(track->enc->codec_id == CODEC_ID_DNXHD)
  573. mov_write_avid_tag(pb, track);
  574. else if(track->enc->codec_id == CODEC_ID_H264) {
  575. mov_write_avcc_tag(pb, track);
  576. if(track->mode == MODE_IPOD)
  577. mov_write_uuid_tag_ipod(pb);
  578. } else if(track->vosLen > 0)
  579. mov_write_glbl_tag(pb, track);
  580. return updateSize(pb, pos);
  581. }
  582. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack *track)
  583. {
  584. offset_t pos = url_ftell(pb);
  585. put_be32(pb, 0); /* size */
  586. put_tag(pb, "stsd");
  587. put_be32(pb, 0); /* version & flags */
  588. put_be32(pb, 1); /* entry count */
  589. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  590. mov_write_video_tag(pb, track);
  591. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  592. mov_write_audio_tag(pb, track);
  593. return updateSize(pb, pos);
  594. }
  595. static int mov_write_ctts_tag(ByteIOContext *pb, MOVTrack *track)
  596. {
  597. MOV_stts_t *ctts_entries;
  598. uint32_t entries = 0;
  599. uint32_t atom_size;
  600. int i;
  601. ctts_entries = av_malloc((track->entry + 1) * sizeof(*ctts_entries)); /* worst case */
  602. ctts_entries[0].count = 1;
  603. ctts_entries[0].duration = track->cluster[0].cts;
  604. for (i=1; i<track->entry; i++) {
  605. if (track->cluster[i].cts == ctts_entries[entries].duration) {
  606. ctts_entries[entries].count++; /* compress */
  607. } else {
  608. entries++;
  609. ctts_entries[entries].duration = track->cluster[i].cts;
  610. ctts_entries[entries].count = 1;
  611. }
  612. }
  613. entries++; /* last one */
  614. atom_size = 16 + (entries * 8);
  615. put_be32(pb, atom_size); /* size */
  616. put_tag(pb, "ctts");
  617. put_be32(pb, 0); /* version & flags */
  618. put_be32(pb, entries); /* entry count */
  619. for (i=0; i<entries; i++) {
  620. put_be32(pb, ctts_entries[i].count);
  621. put_be32(pb, ctts_entries[i].duration);
  622. }
  623. av_free(ctts_entries);
  624. return atom_size;
  625. }
  626. /* Time to sample atom */
  627. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack *track)
  628. {
  629. MOV_stts_t *stts_entries;
  630. uint32_t entries = -1;
  631. uint32_t atom_size;
  632. int i;
  633. if (track->enc->codec_type == CODEC_TYPE_AUDIO && !track->audio_vbr) {
  634. stts_entries = av_malloc(sizeof(*stts_entries)); /* one entry */
  635. stts_entries[0].count = track->sampleCount;
  636. stts_entries[0].duration = 1;
  637. entries = 1;
  638. } else {
  639. stts_entries = av_malloc(track->entry * sizeof(*stts_entries)); /* worst case */
  640. for (i=0; i<track->entry; i++) {
  641. int64_t duration = i + 1 == track->entry ?
  642. track->trackDuration - track->cluster[i].dts + track->cluster[0].dts : /* readjusting */
  643. track->cluster[i+1].dts - track->cluster[i].dts;
  644. if (i && duration == stts_entries[entries].duration) {
  645. stts_entries[entries].count++; /* compress */
  646. } else {
  647. entries++;
  648. stts_entries[entries].duration = duration;
  649. stts_entries[entries].count = 1;
  650. }
  651. }
  652. entries++; /* last one */
  653. }
  654. atom_size = 16 + (entries * 8);
  655. put_be32(pb, atom_size); /* size */
  656. put_tag(pb, "stts");
  657. put_be32(pb, 0); /* version & flags */
  658. put_be32(pb, entries); /* entry count */
  659. for (i=0; i<entries; i++) {
  660. put_be32(pb, stts_entries[i].count);
  661. put_be32(pb, stts_entries[i].duration);
  662. }
  663. av_free(stts_entries);
  664. return atom_size;
  665. }
  666. static int mov_write_dref_tag(ByteIOContext *pb)
  667. {
  668. put_be32(pb, 28); /* size */
  669. put_tag(pb, "dref");
  670. put_be32(pb, 0); /* version & flags */
  671. put_be32(pb, 1); /* entry count */
  672. put_be32(pb, 0xc); /* size */
  673. put_tag(pb, "url ");
  674. put_be32(pb, 1); /* version & flags */
  675. return 28;
  676. }
  677. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack *track)
  678. {
  679. offset_t pos = url_ftell(pb);
  680. put_be32(pb, 0); /* size */
  681. put_tag(pb, "stbl");
  682. mov_write_stsd_tag(pb, track);
  683. mov_write_stts_tag(pb, track);
  684. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  685. track->hasKeyframes && track->hasKeyframes < track->entry)
  686. mov_write_stss_tag(pb, track);
  687. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  688. track->hasBframes)
  689. mov_write_ctts_tag(pb, track);
  690. mov_write_stsc_tag(pb, track);
  691. mov_write_stsz_tag(pb, track);
  692. mov_write_stco_tag(pb, track);
  693. return updateSize(pb, pos);
  694. }
  695. static int mov_write_dinf_tag(ByteIOContext *pb)
  696. {
  697. offset_t pos = url_ftell(pb);
  698. put_be32(pb, 0); /* size */
  699. put_tag(pb, "dinf");
  700. mov_write_dref_tag(pb);
  701. return updateSize(pb, pos);
  702. }
  703. static int mov_write_smhd_tag(ByteIOContext *pb)
  704. {
  705. put_be32(pb, 16); /* size */
  706. put_tag(pb, "smhd");
  707. put_be32(pb, 0); /* version & flags */
  708. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  709. put_be16(pb, 0); /* reserved */
  710. return 16;
  711. }
  712. static int mov_write_vmhd_tag(ByteIOContext *pb)
  713. {
  714. put_be32(pb, 0x14); /* size (always 0x14) */
  715. put_tag(pb, "vmhd");
  716. put_be32(pb, 0x01); /* version & flags */
  717. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  718. return 0x14;
  719. }
  720. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack *track)
  721. {
  722. const char *descr, *hdlr, *hdlr_type;
  723. offset_t pos = url_ftell(pb);
  724. if (!track) { /* no media --> data handler */
  725. hdlr = "dhlr";
  726. hdlr_type = "url ";
  727. descr = "DataHandler";
  728. } else {
  729. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  730. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  731. hdlr_type = "vide";
  732. descr = "VideoHandler";
  733. } else {
  734. hdlr_type = "soun";
  735. descr = "SoundHandler";
  736. }
  737. }
  738. put_be32(pb, 0); /* size */
  739. put_tag(pb, "hdlr");
  740. put_be32(pb, 0); /* Version & flags */
  741. put_buffer(pb, hdlr, 4); /* handler */
  742. put_tag(pb, hdlr_type); /* handler type */
  743. put_be32(pb ,0); /* reserved */
  744. put_be32(pb ,0); /* reserved */
  745. put_be32(pb ,0); /* reserved */
  746. put_byte(pb, strlen(descr)); /* string counter */
  747. put_buffer(pb, descr, strlen(descr)); /* handler description */
  748. return updateSize(pb, pos);
  749. }
  750. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack *track)
  751. {
  752. offset_t pos = url_ftell(pb);
  753. put_be32(pb, 0); /* size */
  754. put_tag(pb, "minf");
  755. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  756. mov_write_vmhd_tag(pb);
  757. else
  758. mov_write_smhd_tag(pb);
  759. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  760. mov_write_hdlr_tag(pb, NULL);
  761. mov_write_dinf_tag(pb);
  762. mov_write_stbl_tag(pb, track);
  763. return updateSize(pb, pos);
  764. }
  765. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack *track)
  766. {
  767. int version = track->trackDuration < INT32_MAX ? 0 : 1;
  768. (version == 1) ? put_be32(pb, 44) : put_be32(pb, 32); /* size */
  769. put_tag(pb, "mdhd");
  770. put_byte(pb, version);
  771. put_be24(pb, 0); /* flags */
  772. if (version == 1) {
  773. put_be64(pb, track->time);
  774. put_be64(pb, track->time);
  775. } else {
  776. put_be32(pb, track->time); /* creation time */
  777. put_be32(pb, track->time); /* modification time */
  778. }
  779. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  780. (version == 1) ? put_be64(pb, track->trackDuration) : put_be32(pb, track->trackDuration); /* duration */
  781. put_be16(pb, track->language); /* language */
  782. put_be16(pb, 0); /* reserved (quality) */
  783. if(version!=0 && track->mode == MODE_MOV){
  784. av_log(NULL, AV_LOG_ERROR,
  785. "FATAL error, file duration too long for timebase, this file will not be\n"
  786. "playable with quicktime. Choose a different timebase or a different\n"
  787. "container format\n");
  788. }
  789. return 32;
  790. }
  791. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack *track)
  792. {
  793. offset_t pos = url_ftell(pb);
  794. put_be32(pb, 0); /* size */
  795. put_tag(pb, "mdia");
  796. mov_write_mdhd_tag(pb, track);
  797. mov_write_hdlr_tag(pb, track);
  798. mov_write_minf_tag(pb, track);
  799. return updateSize(pb, pos);
  800. }
  801. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
  802. {
  803. int64_t duration = av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP);
  804. int version = duration < INT32_MAX ? 0 : 1;
  805. (version == 1) ? put_be32(pb, 104) : put_be32(pb, 92); /* size */
  806. put_tag(pb, "tkhd");
  807. put_byte(pb, version);
  808. put_be24(pb, 0xf); /* flags (track enabled) */
  809. if (version == 1) {
  810. put_be64(pb, track->time);
  811. put_be64(pb, track->time);
  812. } else {
  813. put_be32(pb, track->time); /* creation time */
  814. put_be32(pb, track->time); /* modification time */
  815. }
  816. put_be32(pb, track->trackID); /* track-id */
  817. put_be32(pb, 0); /* reserved */
  818. (version == 1) ? put_be64(pb, duration) : put_be32(pb, duration);
  819. put_be32(pb, 0); /* reserved */
  820. put_be32(pb, 0); /* reserved */
  821. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  822. /* Volume, only for audio */
  823. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  824. put_be16(pb, 0x0100);
  825. else
  826. put_be16(pb, 0);
  827. put_be16(pb, 0); /* reserved */
  828. /* Matrix structure */
  829. put_be32(pb, 0x00010000); /* reserved */
  830. put_be32(pb, 0x0); /* reserved */
  831. put_be32(pb, 0x0); /* reserved */
  832. put_be32(pb, 0x0); /* reserved */
  833. put_be32(pb, 0x00010000); /* reserved */
  834. put_be32(pb, 0x0); /* reserved */
  835. put_be32(pb, 0x0); /* reserved */
  836. put_be32(pb, 0x0); /* reserved */
  837. put_be32(pb, 0x40000000); /* reserved */
  838. /* Track width and height, for visual only */
  839. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  840. double sample_aspect_ratio = av_q2d(st->sample_aspect_ratio);
  841. if(!sample_aspect_ratio) sample_aspect_ratio = 1;
  842. put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  843. put_be32(pb, track->enc->height*0x10000);
  844. }
  845. else {
  846. put_be32(pb, 0);
  847. put_be32(pb, 0);
  848. }
  849. return 0x5c;
  850. }
  851. // This box seems important for the psp playback ... without it the movie seems to hang
  852. static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
  853. {
  854. put_be32(pb, 0x24); /* size */
  855. put_tag(pb, "edts");
  856. put_be32(pb, 0x1c); /* size */
  857. put_tag(pb, "elst");
  858. put_be32(pb, 0x0);
  859. put_be32(pb, 0x1);
  860. put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
  861. put_be32(pb, track->cluster[0].cts); /* first pts is cts since dts is 0 */
  862. put_be32(pb, 0x00010000);
  863. return 0x24;
  864. }
  865. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  866. static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
  867. {
  868. put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  869. put_tag(pb, "uuid");
  870. put_tag(pb, "USMT");
  871. put_be32(pb, 0x21d24fce);
  872. put_be32(pb, 0xbb88695c);
  873. put_be32(pb, 0xfac9c740);
  874. put_be32(pb, 0x1c); // another size here!
  875. put_tag(pb, "MTDT");
  876. put_be32(pb, 0x00010012);
  877. put_be32(pb, 0x0a);
  878. put_be32(pb, 0x55c40000);
  879. put_be32(pb, 0x1);
  880. put_be32(pb, 0x0);
  881. return 0x34;
  882. }
  883. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack *track, AVStream *st)
  884. {
  885. offset_t pos = url_ftell(pb);
  886. put_be32(pb, 0); /* size */
  887. put_tag(pb, "trak");
  888. mov_write_tkhd_tag(pb, track, st);
  889. if (track->mode == MODE_PSP || track->hasBframes)
  890. mov_write_edts_tag(pb, track); // PSP Movies require edts box
  891. mov_write_mdia_tag(pb, track);
  892. if (track->mode == MODE_PSP)
  893. mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
  894. return updateSize(pb, pos);
  895. }
  896. #if 0
  897. /* TODO: Not sorted out, but not necessary either */
  898. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  899. {
  900. put_be32(pb, 0x15); /* size */
  901. put_tag(pb, "iods");
  902. put_be32(pb, 0); /* version & flags */
  903. put_be16(pb, 0x1007);
  904. put_byte(pb, 0);
  905. put_be16(pb, 0x4fff);
  906. put_be16(pb, 0xfffe);
  907. put_be16(pb, 0x01ff);
  908. return 0x15;
  909. }
  910. #endif
  911. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  912. {
  913. int maxTrackID = 1, i;
  914. int64_t maxTrackLenTemp, maxTrackLen = 0;
  915. int version;
  916. for (i=0; i<mov->nb_streams; i++) {
  917. if(mov->tracks[i].entry > 0) {
  918. maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
  919. if(maxTrackLen < maxTrackLenTemp)
  920. maxTrackLen = maxTrackLenTemp;
  921. if(maxTrackID < mov->tracks[i].trackID)
  922. maxTrackID = mov->tracks[i].trackID;
  923. }
  924. }
  925. version = maxTrackLen < UINT32_MAX ? 0 : 1;
  926. (version == 1) ? put_be32(pb, 120) : put_be32(pb, 108); /* size */
  927. put_tag(pb, "mvhd");
  928. put_byte(pb, version);
  929. put_be24(pb, 0); /* flags */
  930. if (version == 1) {
  931. put_be64(pb, mov->time);
  932. put_be64(pb, mov->time);
  933. } else {
  934. put_be32(pb, mov->time); /* creation time */
  935. put_be32(pb, mov->time); /* modification time */
  936. }
  937. put_be32(pb, mov->timescale); /* timescale */
  938. (version == 1) ? put_be64(pb, maxTrackLen) : put_be32(pb, maxTrackLen); /* duration of longest track */
  939. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  940. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  941. put_be16(pb, 0); /* reserved */
  942. put_be32(pb, 0); /* reserved */
  943. put_be32(pb, 0); /* reserved */
  944. /* Matrix structure */
  945. put_be32(pb, 0x00010000); /* reserved */
  946. put_be32(pb, 0x0); /* reserved */
  947. put_be32(pb, 0x0); /* reserved */
  948. put_be32(pb, 0x0); /* reserved */
  949. put_be32(pb, 0x00010000); /* reserved */
  950. put_be32(pb, 0x0); /* reserved */
  951. put_be32(pb, 0x0); /* reserved */
  952. put_be32(pb, 0x0); /* reserved */
  953. put_be32(pb, 0x40000000); /* reserved */
  954. put_be32(pb, 0); /* reserved (preview time) */
  955. put_be32(pb, 0); /* reserved (preview duration) */
  956. put_be32(pb, 0); /* reserved (poster time) */
  957. put_be32(pb, 0); /* reserved (selection time) */
  958. put_be32(pb, 0); /* reserved (selection duration) */
  959. put_be32(pb, 0); /* reserved (current time) */
  960. put_be32(pb, maxTrackID+1); /* Next track id */
  961. return 0x6c;
  962. }
  963. static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext *mov,
  964. AVFormatContext *s)
  965. {
  966. offset_t pos = url_ftell(pb);
  967. put_be32(pb, 0); /* size */
  968. put_tag(pb, "hdlr");
  969. put_be32(pb, 0);
  970. put_be32(pb, 0);
  971. put_tag(pb, "mdir");
  972. put_tag(pb, "appl");
  973. put_be32(pb, 0);
  974. put_be32(pb, 0);
  975. put_be16(pb, 0);
  976. return updateSize(pb, pos);
  977. }
  978. /* helper function to write a data tag with the specified string as data */
  979. static int mov_write_string_data_tag(ByteIOContext *pb, const char *data, int long_style)
  980. {
  981. if(long_style){
  982. offset_t pos = url_ftell(pb);
  983. put_be32(pb, 0); /* size */
  984. put_tag(pb, "data");
  985. put_be32(pb, 1);
  986. put_be32(pb, 0);
  987. put_buffer(pb, data, strlen(data));
  988. return updateSize(pb, pos);
  989. }else{
  990. put_be16(pb, strlen(data)); /* string length */
  991. put_be16(pb, 0);
  992. put_buffer(pb, data, strlen(data));
  993. return strlen(data) + 4;
  994. }
  995. }
  996. static int mov_write_string_tag(ByteIOContext *pb, const char *name, const char *value, int long_style){
  997. int size = 0;
  998. if (value && value[0]) {
  999. offset_t pos = url_ftell(pb);
  1000. put_be32(pb, 0); /* size */
  1001. put_tag(pb, name);
  1002. mov_write_string_data_tag(pb, value, long_style);
  1003. size= updateSize(pb, pos);
  1004. }
  1005. return size;
  1006. }
  1007. /* iTunes year */
  1008. static int mov_write_day_tag(ByteIOContext *pb, int year, int long_style)
  1009. {
  1010. if(year){
  1011. char year_str[5];
  1012. snprintf(year_str, sizeof(year_str), "%04d", year);
  1013. return mov_write_string_tag(pb, "\251day", year_str, long_style);
  1014. }else
  1015. return 0;
  1016. }
  1017. /* iTunes track number */
  1018. static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext *mov,
  1019. AVFormatContext *s)
  1020. {
  1021. int size = 0;
  1022. if (s->track) {
  1023. offset_t pos = url_ftell(pb);
  1024. put_be32(pb, 0); /* size */
  1025. put_tag(pb, "trkn");
  1026. {
  1027. offset_t pos = url_ftell(pb);
  1028. put_be32(pb, 0); /* size */
  1029. put_tag(pb, "data");
  1030. put_be32(pb, 0); // 8 bytes empty
  1031. put_be32(pb, 0);
  1032. put_be16(pb, 0); // empty
  1033. put_be16(pb, s->track); // track number
  1034. put_be16(pb, 0); // total track number
  1035. put_be16(pb, 0); // empty
  1036. updateSize(pb, pos);
  1037. }
  1038. size = updateSize(pb, pos);
  1039. }
  1040. return size;
  1041. }
  1042. /* iTunes meta data list */
  1043. static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext *mov,
  1044. AVFormatContext *s)
  1045. {
  1046. offset_t pos = url_ftell(pb);
  1047. put_be32(pb, 0); /* size */
  1048. put_tag(pb, "ilst");
  1049. mov_write_string_tag(pb, "\251nam", s->title , 1);
  1050. mov_write_string_tag(pb, "\251ART", s->author , 1);
  1051. mov_write_string_tag(pb, "\251wrt", s->author , 1);
  1052. mov_write_string_tag(pb, "\251alb", s->album , 1);
  1053. mov_write_day_tag(pb, s->year ,1);
  1054. mov_write_string_tag(pb, "\251too", LIBAVFORMAT_IDENT, 1);
  1055. mov_write_string_tag(pb, "\251cmt", s->comment , 1);
  1056. mov_write_string_tag(pb, "\251gen", s->genre , 1);
  1057. mov_write_trkn_tag(pb, mov, s);
  1058. return updateSize(pb, pos);
  1059. }
  1060. /* iTunes meta data tag */
  1061. static int mov_write_meta_tag(ByteIOContext *pb, MOVContext *mov,
  1062. AVFormatContext *s)
  1063. {
  1064. int size = 0;
  1065. // only save meta tag if required
  1066. if (s->title[0] || s->author[0] || s->album[0] || s->year ||
  1067. s->comment[0] || s->genre[0] || s->track) {
  1068. offset_t pos = url_ftell(pb);
  1069. put_be32(pb, 0); /* size */
  1070. put_tag(pb, "meta");
  1071. put_be32(pb, 0);
  1072. mov_write_itunes_hdlr_tag(pb, mov, s);
  1073. mov_write_ilst_tag(pb, mov, s);
  1074. size = updateSize(pb, pos);
  1075. }
  1076. return size;
  1077. }
  1078. static int utf8len(const uint8_t *b)
  1079. {
  1080. int len=0;
  1081. int val;
  1082. while(*b){
  1083. GET_UTF8(val, *b++, return -1;)
  1084. len++;
  1085. }
  1086. return len;
  1087. }
  1088. static int ascii_to_wc(ByteIOContext *pb, const uint8_t *b)
  1089. {
  1090. int val;
  1091. while(*b){
  1092. GET_UTF8(val, *b++, return -1;)
  1093. put_be16(pb, val);
  1094. }
  1095. put_be16(pb, 0x00);
  1096. return 0;
  1097. }
  1098. static uint16_t language_code(const char *str)
  1099. {
  1100. return (((str[0]-0x60) & 0x1F) << 10) + (((str[1]-0x60) & 0x1F) << 5) + ((str[2]-0x60) & 0x1F);
  1101. }
  1102. static int mov_write_3gp_udta_tag(ByteIOContext *pb, AVFormatContext *s,
  1103. const char *tag, const char *str)
  1104. {
  1105. offset_t pos = url_ftell(pb);
  1106. if (!utf8len(str))
  1107. return 0;
  1108. put_be32(pb, 0); /* size */
  1109. put_tag (pb, tag); /* type */
  1110. put_be32(pb, 0); /* version + flags */
  1111. if (!strcmp(tag, "yrrc"))
  1112. put_be16(pb, s->year);
  1113. else {
  1114. put_be16(pb, language_code("eng")); /* language */
  1115. ascii_to_wc(pb, str);
  1116. if (!strcmp(tag, "albm") && s->year)
  1117. put_byte(pb, s->year);
  1118. }
  1119. return updateSize(pb, pos);
  1120. }
  1121. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext *mov,
  1122. AVFormatContext *s)
  1123. {
  1124. int i;
  1125. int bitexact = 0;
  1126. for (i = 0; i < s->nb_streams; i++)
  1127. if (mov->tracks[i].enc->flags & CODEC_FLAG_BITEXACT) {
  1128. bitexact = 1;
  1129. break;
  1130. }
  1131. if (!bitexact && (s->title[0] || s->author[0] || s->album[0] || s->year ||
  1132. s->comment[0] || s->genre[0] || s->track)) {
  1133. offset_t pos = url_ftell(pb);
  1134. put_be32(pb, 0); /* size */
  1135. put_tag(pb, "udta");
  1136. if (mov->mode & MODE_3GP) {
  1137. mov_write_3gp_udta_tag(pb, s, "titl", s->title);
  1138. mov_write_3gp_udta_tag(pb, s, "auth", s->author);
  1139. mov_write_3gp_udta_tag(pb, s, "gnre", s->genre);
  1140. mov_write_3gp_udta_tag(pb, s, "dscp", s->comment);
  1141. mov_write_3gp_udta_tag(pb, s, "albm", s->album);
  1142. mov_write_3gp_udta_tag(pb, s, "yrrc", "nil");
  1143. } else if (mov->mode == MODE_MOV) { // the title field breaks gtkpod with mp4 and my suspicion is that stuff is not valid in mp4
  1144. mov_write_string_tag(pb, "\251nam", s->title , 0);
  1145. mov_write_string_tag(pb, "\251aut", s->author , 0);
  1146. mov_write_string_tag(pb, "\251alb", s->album , 0);
  1147. mov_write_day_tag(pb, s->year, 0);
  1148. mov_write_string_tag(pb, "\251enc", LIBAVFORMAT_IDENT, 0);
  1149. mov_write_string_tag(pb, "\251des", s->comment , 0);
  1150. mov_write_string_tag(pb, "\251gen", s->genre , 0);
  1151. } else {
  1152. /* iTunes meta data */
  1153. mov_write_meta_tag(pb, mov, s);
  1154. }
  1155. return updateSize(pb, pos);
  1156. }
  1157. return 0;
  1158. }
  1159. static void mov_write_psp_udta_tag(ByteIOContext *pb,
  1160. const char *str, const char *lang, int type)
  1161. {
  1162. int len = utf8len(str)+1;
  1163. if(len<=0)
  1164. return;
  1165. put_be16(pb, len*2+10); /* size */
  1166. put_be32(pb, type); /* type */
  1167. put_be16(pb, language_code(lang)); /* language */
  1168. put_be16(pb, 0x01); /* ? */
  1169. ascii_to_wc(pb, str);
  1170. }
  1171. static int mov_write_uuidusmt_tag(ByteIOContext *pb, AVFormatContext *s)
  1172. {
  1173. offset_t pos, pos2;
  1174. if (s->title[0]) {
  1175. pos = url_ftell(pb);
  1176. put_be32(pb, 0); /* size placeholder*/
  1177. put_tag(pb, "uuid");
  1178. put_tag(pb, "USMT");
  1179. put_be32(pb, 0x21d24fce); /* 96 bit UUID */
  1180. put_be32(pb, 0xbb88695c);
  1181. put_be32(pb, 0xfac9c740);
  1182. pos2 = url_ftell(pb);
  1183. put_be32(pb, 0); /* size placeholder*/
  1184. put_tag(pb, "MTDT");
  1185. put_be16(pb, 4);
  1186. // ?
  1187. put_be16(pb, 0x0C); /* size */
  1188. put_be32(pb, 0x0B); /* type */
  1189. put_be16(pb, language_code("und")); /* language */
  1190. put_be16(pb, 0x0); /* ? */
  1191. put_be16(pb, 0x021C); /* data */
  1192. mov_write_psp_udta_tag(pb, LIBAVCODEC_IDENT, "eng", 0x04);
  1193. mov_write_psp_udta_tag(pb, s->title, "eng", 0x01);
  1194. // 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);
  1195. mov_write_psp_udta_tag(pb, "2006/04/01 11:11:11", "und", 0x03);
  1196. updateSize(pb, pos2);
  1197. return updateSize(pb, pos);
  1198. }
  1199. return 0;
  1200. }
  1201. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  1202. AVFormatContext *s)
  1203. {
  1204. int i;
  1205. offset_t pos = url_ftell(pb);
  1206. put_be32(pb, 0); /* size placeholder*/
  1207. put_tag(pb, "moov");
  1208. mov->timescale = globalTimescale;
  1209. for (i=0; i<mov->nb_streams; i++) {
  1210. if(mov->tracks[i].entry <= 0) continue;
  1211. mov->tracks[i].time = mov->time;
  1212. mov->tracks[i].trackID = i+1;
  1213. }
  1214. mov_write_mvhd_tag(pb, mov);
  1215. //mov_write_iods_tag(pb, mov);
  1216. for (i=0; i<mov->nb_streams; i++) {
  1217. if(mov->tracks[i].entry > 0) {
  1218. mov_write_trak_tag(pb, &(mov->tracks[i]), s->streams[i]);
  1219. }
  1220. }
  1221. if (mov->mode == MODE_PSP)
  1222. mov_write_uuidusmt_tag(pb, s);
  1223. else
  1224. mov_write_udta_tag(pb, mov, s);
  1225. return updateSize(pb, pos);
  1226. }
  1227. static int mov_write_mdat_tag(ByteIOContext *pb, MOVContext *mov)
  1228. {
  1229. put_be32(pb, 8); // placeholder for extended size field (64 bit)
  1230. put_tag(pb, mov->mode == MODE_MOV ? "wide" : "free");
  1231. mov->mdat_pos = url_ftell(pb);
  1232. put_be32(pb, 0); /* size placeholder*/
  1233. put_tag(pb, "mdat");
  1234. return 0;
  1235. }
  1236. /* TODO: This needs to be more general */
  1237. static int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  1238. {
  1239. MOVContext *mov = s->priv_data;
  1240. offset_t pos = url_ftell(pb);
  1241. int has_h264 = 0, has_video = 0;
  1242. int minor = 0x200;
  1243. int i;
  1244. for (i = 0; i < s->nb_streams; i++) {
  1245. AVStream *st = s->streams[i];
  1246. if (st->codec->codec_type == CODEC_TYPE_VIDEO)
  1247. has_video = 1;
  1248. if (st->codec->codec_id == CODEC_ID_H264)
  1249. has_h264 = 1;
  1250. }
  1251. put_be32(pb, 0); /* size */
  1252. put_tag(pb, "ftyp");
  1253. if (mov->mode == MODE_3GP) {
  1254. put_tag(pb, has_h264 ? "3gp6" : "3gp4");
  1255. minor = has_h264 ? 0x100 : 0x200;
  1256. } else if (mov->mode & MODE_3G2) {
  1257. put_tag(pb, has_h264 ? "3g2b" : "3g2a");
  1258. minor = has_h264 ? 0x20000 : 0x10000;
  1259. }else if (mov->mode == MODE_PSP)
  1260. put_tag(pb, "MSNV");
  1261. else if (mov->mode == MODE_MP4)
  1262. put_tag(pb, "isom");
  1263. else if (mov->mode == MODE_IPOD)
  1264. put_tag(pb, has_video ? "M4V ":"M4A ");
  1265. else
  1266. put_tag(pb, "qt ");
  1267. put_be32(pb, minor);
  1268. if(mov->mode == MODE_MOV)
  1269. put_tag(pb, "qt ");
  1270. else{
  1271. put_tag(pb, "isom");
  1272. put_tag(pb, "iso2");
  1273. if(has_h264)
  1274. put_tag(pb, "avc1");
  1275. }
  1276. if (mov->mode == MODE_3GP)
  1277. put_tag(pb, has_h264 ? "3gp6":"3gp4");
  1278. else if (mov->mode & MODE_3G2)
  1279. put_tag(pb, has_h264 ? "3g2b":"3g2a");
  1280. else if (mov->mode == MODE_PSP)
  1281. put_tag(pb, "MSNV");
  1282. else if (mov->mode == MODE_MP4)
  1283. put_tag(pb, "mp41");
  1284. return updateSize(pb, pos);
  1285. }
  1286. static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
  1287. {
  1288. AVCodecContext *VideoCodec = s->streams[0]->codec;
  1289. AVCodecContext *AudioCodec = s->streams[1]->codec;
  1290. int AudioRate = AudioCodec->sample_rate;
  1291. int FrameRate = ((VideoCodec->time_base.den) * (0x10000))/ (VideoCodec->time_base.num);
  1292. int audio_kbitrate= AudioCodec->bit_rate / 1000;
  1293. int video_kbitrate= FFMIN(VideoCodec->bit_rate / 1000, 800 - audio_kbitrate);
  1294. put_be32(pb, 0x94); /* size */
  1295. put_tag(pb, "uuid");
  1296. put_tag(pb, "PROF");
  1297. put_be32(pb, 0x21d24fce); /* 96 bit UUID */
  1298. put_be32(pb, 0xbb88695c);
  1299. put_be32(pb, 0xfac9c740);
  1300. put_be32(pb, 0x0); /* ? */
  1301. put_be32(pb, 0x3); /* 3 sections ? */
  1302. put_be32(pb, 0x14); /* size */
  1303. put_tag(pb, "FPRF");
  1304. put_be32(pb, 0x0); /* ? */
  1305. put_be32(pb, 0x0); /* ? */
  1306. put_be32(pb, 0x0); /* ? */
  1307. put_be32(pb, 0x2c); /* size */
  1308. put_tag(pb, "APRF"); /* audio */
  1309. put_be32(pb, 0x0);
  1310. put_be32(pb, 0x2); /* TrackID */
  1311. put_tag(pb, "mp4a");
  1312. put_be32(pb, 0x20f);
  1313. put_be32(pb, 0x0);
  1314. put_be32(pb, audio_kbitrate);
  1315. put_be32(pb, audio_kbitrate);
  1316. put_be32(pb, AudioRate);
  1317. put_be32(pb, AudioCodec->channels);
  1318. put_be32(pb, 0x34); /* size */
  1319. put_tag(pb, "VPRF"); /* video */
  1320. put_be32(pb, 0x0);
  1321. put_be32(pb, 0x1); /* TrackID */
  1322. if (VideoCodec->codec_id == CODEC_ID_H264) {
  1323. put_tag(pb, "avc1");
  1324. put_be16(pb, 0x014D);
  1325. put_be16(pb, 0x0015);
  1326. } else {
  1327. put_tag(pb, "mp4v");
  1328. put_be16(pb, 0x0000);
  1329. put_be16(pb, 0x0103);
  1330. }
  1331. put_be32(pb, 0x0);
  1332. put_be32(pb, video_kbitrate);
  1333. put_be32(pb, video_kbitrate);
  1334. put_be32(pb, FrameRate);
  1335. put_be32(pb, FrameRate);
  1336. put_be16(pb, VideoCodec->width);
  1337. put_be16(pb, VideoCodec->height);
  1338. put_be32(pb, 0x010001); /* ? */
  1339. }
  1340. static int mov_write_header(AVFormatContext *s)
  1341. {
  1342. ByteIOContext *pb = s->pb;
  1343. MOVContext *mov = s->priv_data;
  1344. int i;
  1345. if (url_is_streamed(s->pb)) {
  1346. av_log(s, AV_LOG_ERROR, "muxer does not support non seekable output\n");
  1347. return -1;
  1348. }
  1349. /* Default mode == MP4 */
  1350. mov->mode = MODE_MP4;
  1351. if (s->oformat != NULL) {
  1352. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  1353. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3GP|MODE_3G2;
  1354. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  1355. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  1356. else if (!strcmp("ipod",s->oformat->name)) mov->mode = MODE_IPOD;
  1357. mov_write_ftyp_tag(pb,s);
  1358. if (mov->mode == MODE_PSP) {
  1359. if (s->nb_streams != 2) {
  1360. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  1361. return -1;
  1362. }
  1363. mov_write_uuidprof_tag(pb,s);
  1364. }
  1365. }
  1366. for(i=0; i<s->nb_streams; i++){
  1367. AVStream *st= s->streams[i];
  1368. MOVTrack *track= &mov->tracks[i];
  1369. track->enc = st->codec;
  1370. track->language = ff_mov_iso639_to_lang(st->language, mov->mode != MODE_MOV);
  1371. track->mode = mov->mode;
  1372. track->tag = mov_find_codec_tag(s, track);
  1373. if (!track->tag) {
  1374. av_log(s, AV_LOG_ERROR, "track %d: could not find tag, "
  1375. "codec not currently supported in container\n", i);
  1376. return -1;
  1377. }
  1378. if(st->codec->codec_type == CODEC_TYPE_VIDEO){
  1379. track->timescale = st->codec->time_base.den;
  1380. av_set_pts_info(st, 64, 1, st->codec->time_base.den);
  1381. if (track->mode == MODE_MOV && track->timescale > 100000)
  1382. av_log(s, AV_LOG_WARNING,
  1383. "WARNING codec timebase is very high. If duration is too long,\n"
  1384. "file may not be playable by quicktime. Specify a shorter timebase\n"
  1385. "or choose different container.\n");
  1386. }else if(st->codec->codec_type == CODEC_TYPE_AUDIO){
  1387. track->timescale = st->codec->sample_rate;
  1388. av_set_pts_info(st, 64, 1, st->codec->sample_rate);
  1389. if(!st->codec->frame_size){
  1390. av_log(s, AV_LOG_ERROR, "track %d: codec frame size is not set\n", i);
  1391. return -1;
  1392. }else if(st->codec->frame_size > 1){ /* assume compressed audio */
  1393. track->audio_vbr = 1;
  1394. }else{
  1395. track->sampleSize = (av_get_bits_per_sample(st->codec->codec_id) >> 3) * st->codec->channels;
  1396. }
  1397. }
  1398. }
  1399. mov_write_mdat_tag(pb, mov);
  1400. mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
  1401. mov->nb_streams = s->nb_streams;
  1402. put_flush_packet(pb);
  1403. return 0;
  1404. }
  1405. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  1406. {
  1407. MOVContext *mov = s->priv_data;
  1408. ByteIOContext *pb = s->pb;
  1409. MOVTrack *trk = &mov->tracks[pkt->stream_index];
  1410. AVCodecContext *enc = trk->enc;
  1411. unsigned int samplesInChunk = 0;
  1412. int size= pkt->size;
  1413. if (url_is_streamed(s->pb)) return 0; /* Can't handle that */
  1414. if (!size) return 0; /* Discard 0 sized packets */
  1415. if (enc->codec_id == CODEC_ID_AMR_NB) {
  1416. /* We must find out how many AMR blocks there are in one packet */
  1417. static uint16_t packed_size[16] =
  1418. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  1419. int len = 0;
  1420. while (len < size && samplesInChunk < 100) {
  1421. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  1422. samplesInChunk++;
  1423. }
  1424. if(samplesInChunk > 1){
  1425. av_log(s, AV_LOG_ERROR, "fatal error, input is not a single packet, implement a AVParser for it\n");
  1426. return -1;
  1427. }
  1428. } else if (trk->sampleSize)
  1429. samplesInChunk = size/trk->sampleSize;
  1430. else
  1431. samplesInChunk = 1;
  1432. /* copy extradata if it exists */
  1433. if (trk->vosLen == 0 && enc->extradata_size > 0) {
  1434. trk->vosLen = enc->extradata_size;
  1435. trk->vosData = av_malloc(trk->vosLen);
  1436. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  1437. }
  1438. if (enc->codec_id == CODEC_ID_H264 && trk->vosLen > 0 && *(uint8_t *)trk->vosData != 1) {
  1439. /* from x264 or from bytestream h264 */
  1440. /* nal reformating needed */
  1441. int ret = ff_avc_parse_nal_units(pkt->data, &pkt->data, &pkt->size);
  1442. if (ret < 0)
  1443. return ret;
  1444. assert(pkt->size);
  1445. size = pkt->size;
  1446. } else if (enc->codec_id == CODEC_ID_DNXHD && !trk->vosLen) {
  1447. /* copy frame to create needed atoms */
  1448. trk->vosLen = size;
  1449. trk->vosData = av_malloc(size);
  1450. if (!trk->vosData)
  1451. return AVERROR(ENOMEM);
  1452. memcpy(trk->vosData, pkt->data, size);
  1453. }
  1454. if (!(trk->entry % MOV_INDEX_CLUSTER_SIZE)) {
  1455. trk->cluster = av_realloc(trk->cluster, (trk->entry + MOV_INDEX_CLUSTER_SIZE) * sizeof(*trk->cluster));
  1456. if (!trk->cluster)
  1457. return -1;
  1458. }
  1459. trk->cluster[trk->entry].pos = url_ftell(pb);
  1460. trk->cluster[trk->entry].samplesInChunk = samplesInChunk;
  1461. trk->cluster[trk->entry].size = size;
  1462. trk->cluster[trk->entry].entries = samplesInChunk;
  1463. trk->cluster[trk->entry].dts = pkt->dts;
  1464. trk->trackDuration = pkt->dts - trk->cluster[0].dts + pkt->duration;
  1465. if (pkt->pts == AV_NOPTS_VALUE) {
  1466. av_log(s, AV_LOG_WARNING, "pts has no value\n");
  1467. pkt->pts = pkt->dts;
  1468. }
  1469. if (pkt->dts != pkt->pts)
  1470. trk->hasBframes = 1;
  1471. trk->cluster[trk->entry].cts = pkt->pts - pkt->dts;
  1472. trk->cluster[trk->entry].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  1473. if(trk->cluster[trk->entry].key_frame)
  1474. trk->hasKeyframes++;
  1475. trk->entry++;
  1476. trk->sampleCount += samplesInChunk;
  1477. mov->mdat_size += size;
  1478. put_buffer(pb, pkt->data, size);
  1479. put_flush_packet(pb);
  1480. return 0;
  1481. }
  1482. static int mov_write_trailer(AVFormatContext *s)
  1483. {
  1484. MOVContext *mov = s->priv_data;
  1485. ByteIOContext *pb = s->pb;
  1486. int res = 0;
  1487. int i;
  1488. offset_t moov_pos = url_ftell(pb);
  1489. /* Write size of mdat tag */
  1490. if (mov->mdat_size+8 <= UINT32_MAX) {
  1491. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  1492. put_be32(pb, mov->mdat_size+8);
  1493. } else {
  1494. /* overwrite 'wide' placeholder atom */
  1495. url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
  1496. put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
  1497. put_tag(pb, "mdat");
  1498. put_be64(pb, mov->mdat_size+16);
  1499. }
  1500. url_fseek(pb, moov_pos, SEEK_SET);
  1501. mov_write_moov_tag(pb, mov, s);
  1502. for (i=0; i<mov->nb_streams; i++) {
  1503. av_freep(&mov->tracks[i].cluster);
  1504. if(mov->tracks[i].vosLen) av_free(mov->tracks[i].vosData);
  1505. }
  1506. put_flush_packet(pb);
  1507. return res;
  1508. }
  1509. #ifdef CONFIG_MOV_MUXER
  1510. AVOutputFormat mov_muxer = {
  1511. "mov",
  1512. NULL_IF_CONFIG_SMALL("MOV format"),
  1513. NULL,
  1514. "mov",
  1515. sizeof(MOVContext),
  1516. CODEC_ID_AAC,
  1517. CODEC_ID_MPEG4,
  1518. mov_write_header,
  1519. mov_write_packet,
  1520. mov_write_trailer,
  1521. .flags = AVFMT_GLOBALHEADER,
  1522. .codec_tag = (const AVCodecTag* const []){codec_movvideo_tags, codec_movaudio_tags, 0},
  1523. };
  1524. #endif
  1525. #ifdef CONFIG_TGP_MUXER
  1526. AVOutputFormat tgp_muxer = {
  1527. "3gp",
  1528. NULL_IF_CONFIG_SMALL("3GP format"),
  1529. NULL,
  1530. "3gp",
  1531. sizeof(MOVContext),
  1532. CODEC_ID_AMR_NB,
  1533. CODEC_ID_H263,
  1534. mov_write_header,
  1535. mov_write_packet,
  1536. mov_write_trailer,
  1537. .flags = AVFMT_GLOBALHEADER,
  1538. .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
  1539. };
  1540. #endif
  1541. #ifdef CONFIG_MP4_MUXER
  1542. AVOutputFormat mp4_muxer = {
  1543. "mp4",
  1544. NULL_IF_CONFIG_SMALL("MP4 format"),
  1545. "application/mp4",
  1546. "mp4",
  1547. sizeof(MOVContext),
  1548. CODEC_ID_AAC,
  1549. CODEC_ID_MPEG4,
  1550. mov_write_header,
  1551. mov_write_packet,
  1552. mov_write_trailer,
  1553. .flags = AVFMT_GLOBALHEADER,
  1554. .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
  1555. };
  1556. #endif
  1557. #ifdef CONFIG_PSP_MUXER
  1558. AVOutputFormat psp_muxer = {
  1559. "psp",
  1560. NULL_IF_CONFIG_SMALL("PSP MP4 format"),
  1561. NULL,
  1562. "mp4,psp",
  1563. sizeof(MOVContext),
  1564. CODEC_ID_AAC,
  1565. CODEC_ID_MPEG4,
  1566. mov_write_header,
  1567. mov_write_packet,
  1568. mov_write_trailer,
  1569. .flags = AVFMT_GLOBALHEADER,
  1570. .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
  1571. };
  1572. #endif
  1573. #ifdef CONFIG_TG2_MUXER
  1574. AVOutputFormat tg2_muxer = {
  1575. "3g2",
  1576. NULL_IF_CONFIG_SMALL("3GP2 format"),
  1577. NULL,
  1578. "3g2",
  1579. sizeof(MOVContext),
  1580. CODEC_ID_AMR_NB,
  1581. CODEC_ID_H263,
  1582. mov_write_header,
  1583. mov_write_packet,
  1584. mov_write_trailer,
  1585. .flags = AVFMT_GLOBALHEADER,
  1586. .codec_tag = (const AVCodecTag* const []){codec_3gp_tags, 0},
  1587. };
  1588. #endif
  1589. #ifdef CONFIG_IPOD_MUXER
  1590. AVOutputFormat ipod_muxer = {
  1591. "ipod",
  1592. NULL_IF_CONFIG_SMALL("iPod H.264 MP4 format"),
  1593. "application/mp4",
  1594. "m4v,m4a",
  1595. sizeof(MOVContext),
  1596. CODEC_ID_AAC,
  1597. CODEC_ID_H264,
  1598. mov_write_header,
  1599. mov_write_packet,
  1600. mov_write_trailer,
  1601. .flags = AVFMT_GLOBALHEADER,
  1602. .codec_tag = (const AVCodecTag* const []){ff_mp4_obj_type, 0},
  1603. };
  1604. #endif