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.

1137 lines
33KB

  1. /*
  2. * MOV, 3GP, MP4 encoder.
  3. * Copyright (c) 2003 Thomas Raivio.
  4. * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "avformat.h"
  21. #include "avi.h"
  22. #include "avio.h"
  23. #undef NDEBUG
  24. #include <assert.h>
  25. #define MOV_INDEX_CLUSTER_SIZE 16384
  26. #define globalTimescale 1000
  27. #define MODE_MP4 0
  28. #define MODE_MOV 1
  29. #define MODE_3GP 2
  30. typedef struct MOVIentry {
  31. unsigned int flags, pos, size;
  32. unsigned int samplesInChunk;
  33. char key_frame;
  34. unsigned int entries;
  35. } MOVIentry;
  36. typedef struct MOVIndex {
  37. int mode;
  38. int entry;
  39. int mdat_size;
  40. int ents_allocated;
  41. long timescale;
  42. long time;
  43. long trackDuration;
  44. long sampleCount;
  45. long sampleDuration;
  46. int hasKeyframes;
  47. int trackID;
  48. AVCodecContext *enc;
  49. int vosLen;
  50. uint8_t *vosData;
  51. MOVIentry** cluster;
  52. } MOVTrack;
  53. typedef struct {
  54. int mode;
  55. long time;
  56. int nb_streams;
  57. int mdat_written;
  58. offset_t mdat_pos;
  59. long timescale;
  60. MOVTrack tracks[MAX_STREAMS];
  61. } MOVContext;
  62. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
  63. //FIXME supprt 64bit varaint with wide placeholders
  64. static int updateSize (ByteIOContext *pb, int pos)
  65. {
  66. long curpos = url_ftell(pb);
  67. url_fseek(pb, pos, SEEK_SET);
  68. put_be32(pb, curpos - pos); /* rewrite size */
  69. url_fseek(pb, curpos, SEEK_SET);
  70. return curpos - pos;
  71. }
  72. /* Chunk offset atom */
  73. static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
  74. {
  75. int i;
  76. int pos = url_ftell(pb);
  77. put_be32(pb, 0); /* size */
  78. put_tag(pb, "stco");
  79. put_be32(pb, 0); /* version & flags */
  80. put_be32(pb, track->entry); /* entry count */
  81. for (i=0; i<track->entry; i++) {
  82. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  83. int id = i % MOV_INDEX_CLUSTER_SIZE;
  84. put_be32(pb, track->cluster[cl][id].pos);
  85. }
  86. return updateSize (pb, pos);
  87. }
  88. /* Sample size atom */
  89. static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
  90. {
  91. int equalChunks = 1;
  92. int i, j, entries = 0, tst = -1, oldtst = -1;
  93. int pos = url_ftell(pb);
  94. put_be32(pb, 0); /* size */
  95. put_tag(pb, "stsz");
  96. put_be32(pb, 0); /* version & flags */
  97. for (i=0; i<track->entry; i++) {
  98. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  99. int id = i % MOV_INDEX_CLUSTER_SIZE;
  100. tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
  101. if(oldtst != -1 && tst != oldtst) {
  102. equalChunks = 0;
  103. }
  104. oldtst = tst;
  105. entries += track->cluster[cl][id].entries;
  106. }
  107. if (equalChunks) {
  108. int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
  109. put_be32(pb, sSize); // sample size
  110. put_be32(pb, entries); // sample count
  111. }
  112. else {
  113. put_be32(pb, 0); // sample size
  114. put_be32(pb, entries); // sample count
  115. for (i=0; i<track->entry; i++) {
  116. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  117. int id = i % MOV_INDEX_CLUSTER_SIZE;
  118. for ( j=0; j<track->cluster[cl][id].entries; j++) {
  119. put_be32(pb, track->cluster[cl][id].size /
  120. track->cluster[cl][id].entries);
  121. }
  122. }
  123. }
  124. return updateSize (pb, pos);
  125. }
  126. /* Sample to chunk atom */
  127. static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
  128. {
  129. int index = 0, oldval = -1, i, entryPos, curpos;
  130. int pos = url_ftell(pb);
  131. put_be32(pb, 0); /* size */
  132. put_tag(pb, "stsc");
  133. put_be32(pb, 0); // version & flags
  134. entryPos = url_ftell(pb);
  135. put_be32(pb, track->entry); // entry count
  136. for (i=0; i<track->entry; i++) {
  137. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  138. int id = i % MOV_INDEX_CLUSTER_SIZE;
  139. if(oldval != track->cluster[cl][id].samplesInChunk)
  140. {
  141. put_be32(pb, i+1); // first chunk
  142. put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
  143. put_be32(pb, 0x1); // sample description index
  144. oldval = track->cluster[cl][id].samplesInChunk;
  145. index++;
  146. }
  147. }
  148. curpos = url_ftell(pb);
  149. url_fseek(pb, entryPos, SEEK_SET);
  150. put_be32(pb, index); // rewrite size
  151. url_fseek(pb, curpos, SEEK_SET);
  152. return updateSize (pb, pos);
  153. }
  154. /* Sync sample atom */
  155. static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
  156. {
  157. long curpos;
  158. int i, index = 0, entryPos;
  159. int pos = url_ftell(pb);
  160. put_be32(pb, 0); // size
  161. put_tag(pb, "stss");
  162. put_be32(pb, 0); // version & flags
  163. entryPos = url_ftell(pb);
  164. put_be32(pb, track->entry); // entry count
  165. for (i=0; i<track->entry; i++) {
  166. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  167. int id = i % MOV_INDEX_CLUSTER_SIZE;
  168. if(track->cluster[cl][id].key_frame == 1) {
  169. put_be32(pb, i+1);
  170. index++;
  171. }
  172. }
  173. curpos = url_ftell(pb);
  174. url_fseek(pb, entryPos, SEEK_SET);
  175. put_be32(pb, index); // rewrite size
  176. url_fseek(pb, curpos, SEEK_SET);
  177. return updateSize (pb, pos);
  178. }
  179. static int mov_write_damr_tag(ByteIOContext *pb)
  180. {
  181. put_be32(pb, 0x11); /* size */
  182. put_tag(pb, "damr");
  183. put_tag(pb, "FFMP");
  184. put_byte(pb, 0);
  185. put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
  186. put_be16(pb, 0xa); /* Mode change period (no restriction) */
  187. //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
  188. //put_be16(pb, 1); /* Mode change period (no restriction) */
  189. return 0x11;
  190. }
  191. static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
  192. {
  193. int pos = url_ftell(pb);
  194. put_be32(pb, 0); /* size */
  195. put_tag(pb, "wave");
  196. put_be32(pb, 12); /* size */
  197. put_tag(pb, "frma");
  198. put_tag(pb, "mp4a");
  199. put_be32(pb, 12); /* size */
  200. put_tag(pb, "mp4a");
  201. put_be32(pb, 0);
  202. mov_write_esds_tag(pb, track);
  203. put_be32(pb, 12); /* size */
  204. put_tag(pb, "srcq");
  205. put_be32(pb, 0x40);
  206. put_be32(pb, 8); /* size */
  207. put_be32(pb, 0); /* null tag */
  208. return updateSize (pb, pos);
  209. }
  210. const CodecTag codec_movaudio_tags[] = {
  211. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
  212. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
  213. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
  214. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
  215. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
  216. { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
  217. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
  218. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
  219. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
  220. { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
  221. { 0, 0 },
  222. };
  223. static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
  224. {
  225. int pos = url_ftell(pb);
  226. int tag;
  227. put_be32(pb, 0); /* size */
  228. tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
  229. // if no mac fcc found, try with Microsoft tags
  230. if (!tag)
  231. {
  232. int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
  233. tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
  234. }
  235. put_le32(pb, tag); // store it byteswapped
  236. put_be32(pb, 0); /* Reserved */
  237. put_be16(pb, 0); /* Reserved */
  238. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  239. /* SoundDescription */
  240. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  241. put_be16(pb, 1); /* Version 1 */
  242. else
  243. put_be16(pb, 0); /* Version 0 */
  244. put_be16(pb, 0); /* Revision level */
  245. put_be32(pb, 0); /* Reserved */
  246. put_be16(pb, track->enc->channels); /* Number of channels */
  247. /* TODO: Currently hard-coded to 16-bit, there doesn't seem
  248. to be a good way to get number of bits of audio */
  249. put_be16(pb, 0x10); /* Reserved */
  250. put_be16(pb, 0); /* compression ID (= 0) */
  251. put_be16(pb, 0); /* packet size (= 0) */
  252. put_be16(pb, track->timescale); /* Time scale */
  253. put_be16(pb, 0); /* Reserved */
  254. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  255. {
  256. /* SoundDescription V1 extended info */
  257. put_be32(pb, track->enc->frame_size); /* Samples per packet */
  258. put_be32(pb, 1536); /* Bytes per packet */
  259. put_be32(pb, 2); /* Bytes per frame */
  260. put_be32(pb, 2); /* Bytes per sample */
  261. }
  262. if(track->enc->codec_id == CODEC_ID_AAC) {
  263. if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
  264. else mov_write_esds_tag(pb, track);
  265. }
  266. if(track->enc->codec_id == CODEC_ID_AMR_NB)
  267. mov_write_damr_tag(pb);
  268. return updateSize (pb, pos);
  269. }
  270. static int mov_write_d263_tag(ByteIOContext *pb)
  271. {
  272. put_be32(pb, 0xf); /* size */
  273. put_tag(pb, "d263");
  274. put_tag(pb, "FFMP");
  275. put_be16(pb, 0x0a);
  276. put_byte(pb, 0);
  277. return 0xf;
  278. }
  279. /* TODO: No idea about these values */
  280. static int mov_write_svq3_tag(ByteIOContext *pb)
  281. {
  282. put_be32(pb, 0x15);
  283. put_tag(pb, "SMI ");
  284. put_tag(pb, "SEQH");
  285. put_be32(pb, 0x5);
  286. put_be32(pb, 0xe2c0211d);
  287. put_be32(pb, 0xc0000000);
  288. put_byte(pb, 0);
  289. return 0x15;
  290. }
  291. static unsigned int descrLength(unsigned int len)
  292. {
  293. if (len < 0x00000080)
  294. return 2 + len;
  295. else if (len < 0x00004000)
  296. return 3 + len;
  297. else if(len < 0x00200000)
  298. return 4 + len;
  299. else
  300. return 5 + len;
  301. }
  302. static void putDescr(ByteIOContext *pb, int tag, int size)
  303. {
  304. uint32_t len;
  305. uint8_t vals[4];
  306. len = size;
  307. vals[3] = (uint8_t)(len & 0x7f);
  308. len >>= 7;
  309. vals[2] = (uint8_t)((len & 0x7f) | 0x80);
  310. len >>= 7;
  311. vals[1] = (uint8_t)((len & 0x7f) | 0x80);
  312. len >>= 7;
  313. vals[0] = (uint8_t)((len & 0x7f) | 0x80);
  314. put_byte(pb, tag); // DescriptorTag
  315. if (size < 0x00000080)
  316. {
  317. put_byte(pb, vals[3]);
  318. }
  319. else if (size < 0x00004000)
  320. {
  321. put_byte(pb, vals[2]);
  322. put_byte(pb, vals[3]);
  323. }
  324. else if (size < 0x00200000)
  325. {
  326. put_byte(pb, vals[1]);
  327. put_byte(pb, vals[2]);
  328. put_byte(pb, vals[3]);
  329. }
  330. else if (size < 0x10000000)
  331. {
  332. put_byte(pb, vals[0]);
  333. put_byte(pb, vals[1]);
  334. put_byte(pb, vals[2]);
  335. put_byte(pb, vals[3]);
  336. }
  337. }
  338. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
  339. {
  340. int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  341. int pos = url_ftell(pb);
  342. put_be32(pb, 0); // size
  343. put_tag(pb, "esds");
  344. put_be32(pb, 0); // Version
  345. // ES descriptor
  346. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  347. descrLength(1));
  348. put_be16(pb, 0x0001); // ID (= 1)
  349. put_byte(pb, 0x00); // flags (= no flags)
  350. // DecoderConfig descriptor
  351. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  352. if(track->enc->codec_id == CODEC_ID_AAC)
  353. put_byte(pb, 0x40); // Object type indication
  354. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  355. put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
  356. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  357. put_byte(pb, 0x15); // flags (= Audiostream)
  358. else
  359. put_byte(pb, 0x11); // flags (= Visualstream)
  360. put_byte(pb, 0x0); // Buffersize DB (24 bits)
  361. put_be16(pb, 0x0dd2); // Buffersize DB
  362. // TODO: find real values for these
  363. put_be32(pb, 0x0002e918); // maxbitrate
  364. put_be32(pb, 0x00017e6b); // avg bitrate
  365. if (track->vosLen)
  366. {
  367. // DecoderSpecific info descriptor
  368. putDescr(pb, 0x05, track->vosLen);
  369. put_buffer(pb, track->vosData, track->vosLen);
  370. }
  371. // SL descriptor
  372. putDescr(pb, 0x06, 1);
  373. put_byte(pb, 0x02);
  374. return updateSize (pb, pos);
  375. }
  376. const CodecTag codec_movvideo_tags[] = {
  377. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
  378. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
  379. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  380. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
  381. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
  382. { 0, 0 },
  383. };
  384. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
  385. {
  386. int pos = url_ftell(pb);
  387. int tag;
  388. put_be32(pb, 0); /* size */
  389. tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
  390. // if no mac fcc found, try with Microsoft tags
  391. if (!tag)
  392. tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
  393. put_le32(pb, tag); // store it byteswapped
  394. put_be32(pb, 0); /* Reserved */
  395. put_be16(pb, 0); /* Reserved */
  396. put_be16(pb, 1); /* Data-reference index */
  397. put_be32(pb, 0); /* Reserved (= 02000c) */
  398. put_be32(pb, 0); /* Reserved ("SVis")*/
  399. put_be32(pb, 0); /* Reserved */
  400. put_be32(pb, 0); /* Reserved (400)*/
  401. put_be16(pb, track->enc->width); /* Video width */
  402. put_be16(pb, track->enc->height); /* Video height */
  403. put_be32(pb, 0x00480000); /* Reserved */
  404. put_be32(pb, 0x00480000); /* Reserved */
  405. put_be32(pb, 0); /* Data size (= 0) */
  406. put_be16(pb, 1); /* Frame count (= 1) */
  407. put_be32(pb, 0); /* Reserved */
  408. put_be32(pb, 0); /* Reserved */
  409. put_be32(pb, 0); /* Reserved */
  410. put_be32(pb, 0); /* Reserved */
  411. put_be32(pb, 0); /* Reserved */
  412. put_be32(pb, 0); /* Reserved */
  413. put_be32(pb, 0); /* Reserved */
  414. put_be32(pb, 0); /* Reserved */
  415. put_be16(pb, 0x18); /* Reserved */
  416. put_be16(pb, 0xffff); /* Reserved */
  417. if(track->enc->codec_id == CODEC_ID_MPEG4)
  418. mov_write_esds_tag(pb, track);
  419. else if(track->enc->codec_id == CODEC_ID_H263)
  420. mov_write_d263_tag(pb);
  421. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  422. mov_write_svq3_tag(pb);
  423. return updateSize (pb, pos);
  424. }
  425. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
  426. {
  427. int pos = url_ftell(pb);
  428. put_be32(pb, 0); /* size */
  429. put_tag(pb, "stsd");
  430. put_be32(pb, 0); /* version & flags */
  431. put_be32(pb, 1); /* entry count */
  432. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  433. mov_write_video_tag(pb, track);
  434. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  435. mov_write_audio_tag(pb, track);
  436. return updateSize(pb, pos);
  437. }
  438. /* TODO?: Currently all samples/frames seem to have same duration */
  439. /* Time to sample atom */
  440. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
  441. {
  442. put_be32(pb, 0x18); /* size */
  443. put_tag(pb, "stts");
  444. put_be32(pb, 0); /* version & flags */
  445. put_be32(pb, 1); /* entry count */
  446. put_be32(pb, track->sampleCount); /* sample count */
  447. put_be32(pb, track->sampleDuration); /* sample duration */
  448. return 0x18;
  449. }
  450. static int mov_write_dref_tag(ByteIOContext *pb)
  451. {
  452. put_be32(pb, 28); /* size */
  453. put_tag(pb, "dref");
  454. put_be32(pb, 0); /* version & flags */
  455. put_be32(pb, 1); /* entry count */
  456. put_be32(pb, 0xc); /* size */
  457. put_tag(pb, "url ");
  458. put_be32(pb, 1); /* version & flags */
  459. return 28;
  460. }
  461. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
  462. {
  463. int pos = url_ftell(pb);
  464. put_be32(pb, 0); /* size */
  465. put_tag(pb, "stbl");
  466. mov_write_stsd_tag(pb, track);
  467. mov_write_stts_tag(pb, track);
  468. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  469. track->hasKeyframes)
  470. mov_write_stss_tag(pb, track);
  471. mov_write_stsc_tag(pb, track);
  472. mov_write_stsz_tag(pb, track);
  473. mov_write_stco_tag(pb, track);
  474. return updateSize(pb, pos);
  475. }
  476. static int mov_write_dinf_tag(ByteIOContext *pb)
  477. {
  478. int pos = url_ftell(pb);
  479. put_be32(pb, 0); /* size */
  480. put_tag(pb, "dinf");
  481. mov_write_dref_tag(pb);
  482. return updateSize(pb, pos);
  483. }
  484. static int mov_write_smhd_tag(ByteIOContext *pb)
  485. {
  486. put_be32(pb, 16); /* size */
  487. put_tag(pb, "smhd");
  488. put_be32(pb, 0); /* version & flags */
  489. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  490. put_be16(pb, 0); /* reserved */
  491. return 16;
  492. }
  493. static int mov_write_vmhd_tag(ByteIOContext *pb)
  494. {
  495. put_be32(pb, 0x14); /* size (always 0x14) */
  496. put_tag(pb, "vmhd");
  497. put_be32(pb, 0x01); /* version & flags */
  498. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  499. return 0x14;
  500. }
  501. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
  502. {
  503. char *descr, *hdlr, *hdlr_type;
  504. int pos = url_ftell(pb);
  505. if (!track) { /* no media --> data handler */
  506. hdlr = "dhlr";
  507. hdlr_type = "url ";
  508. descr = "DataHandler";
  509. } else {
  510. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  511. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  512. hdlr_type = "vide";
  513. descr = "VideoHandler";
  514. } else {
  515. hdlr_type = "soun";
  516. descr = "SoundHandler";
  517. }
  518. }
  519. put_be32(pb, 0); /* size */
  520. put_tag(pb, "hdlr");
  521. put_be32(pb, 0); /* Version & flags */
  522. put_buffer(pb, hdlr, 4); /* handler */
  523. put_tag(pb, hdlr_type); /* handler type */
  524. put_be32(pb ,0); /* reserved */
  525. put_be32(pb ,0); /* reserved */
  526. put_be32(pb ,0); /* reserved */
  527. put_byte(pb, strlen(descr)); /* string counter */
  528. put_buffer(pb, descr, strlen(descr)); /* handler description */
  529. return updateSize(pb, pos);
  530. }
  531. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
  532. {
  533. int pos = url_ftell(pb);
  534. put_be32(pb, 0); /* size */
  535. put_tag(pb, "minf");
  536. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  537. mov_write_vmhd_tag(pb);
  538. else
  539. mov_write_smhd_tag(pb);
  540. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  541. mov_write_hdlr_tag(pb, NULL);
  542. mov_write_dinf_tag(pb);
  543. mov_write_stbl_tag(pb, track);
  544. return updateSize(pb, pos);
  545. }
  546. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
  547. {
  548. put_be32(pb, 32); /* size */
  549. put_tag(pb, "mdhd");
  550. put_be32(pb, 0); /* Version & flags */
  551. put_be32(pb, track->time); /* creation time */
  552. put_be32(pb, track->time); /* modification time */
  553. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  554. put_be32(pb, track->trackDuration); /* duration */
  555. put_be16(pb, 0); /* language, 0 = english */
  556. put_be16(pb, 0); /* reserved (quality) */
  557. return 32;
  558. }
  559. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
  560. {
  561. int pos = url_ftell(pb);
  562. put_be32(pb, 0); /* size */
  563. put_tag(pb, "mdia");
  564. mov_write_mdhd_tag(pb, track);
  565. mov_write_hdlr_tag(pb, track);
  566. mov_write_minf_tag(pb, track);
  567. return updateSize(pb, pos);
  568. }
  569. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
  570. {
  571. int64_t maxTrackLenTemp;
  572. put_be32(pb, 0x5c); /* size (always 0x5c) */
  573. put_tag(pb, "tkhd");
  574. put_be32(pb, 0xf); /* version & flags (track enabled) */
  575. put_be32(pb, track->time); /* creation time */
  576. put_be32(pb, track->time); /* modification time */
  577. put_be32(pb, track->trackID); /* track-id */
  578. put_be32(pb, 0); /* reserved */
  579. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
  580. put_be32(pb, (long)maxTrackLenTemp); /* duration */
  581. put_be32(pb, 0); /* reserved */
  582. put_be32(pb, 0); /* reserved */
  583. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  584. /* Volume, only for audio */
  585. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  586. put_be16(pb, 0x0100);
  587. else
  588. put_be16(pb, 0);
  589. put_be16(pb, 0); /* reserved */
  590. /* Matrix structure */
  591. put_be32(pb, 0x00010000); /* reserved */
  592. put_be32(pb, 0x0); /* reserved */
  593. put_be32(pb, 0x0); /* reserved */
  594. put_be32(pb, 0x0); /* reserved */
  595. put_be32(pb, 0x00010000); /* reserved */
  596. put_be32(pb, 0x0); /* reserved */
  597. put_be32(pb, 0x0); /* reserved */
  598. put_be32(pb, 0x0); /* reserved */
  599. put_be32(pb, 0x40000000); /* reserved */
  600. /* Track width and height, for visual only */
  601. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  602. double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
  603. if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
  604. put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  605. put_be32(pb, track->enc->height*0x10000);
  606. }
  607. else {
  608. put_be32(pb, 0);
  609. put_be32(pb, 0);
  610. }
  611. return 0x5c;
  612. }
  613. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
  614. {
  615. int pos = url_ftell(pb);
  616. put_be32(pb, 0); /* size */
  617. put_tag(pb, "trak");
  618. mov_write_tkhd_tag(pb, track);
  619. mov_write_mdia_tag(pb, track);
  620. return updateSize(pb, pos);
  621. }
  622. /* TODO: Not sorted out, but not necessary either */
  623. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  624. {
  625. put_be32(pb, 0x15); /* size */
  626. put_tag(pb, "iods");
  627. put_be32(pb, 0); /* version & flags */
  628. put_be16(pb, 0x1007);
  629. put_byte(pb, 0);
  630. put_be16(pb, 0x4fff);
  631. put_be16(pb, 0xfffe);
  632. put_be16(pb, 0x01ff);
  633. return 0x15;
  634. }
  635. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  636. {
  637. int maxTrackID = 1, maxTrackLen = 0, i;
  638. int64_t maxTrackLenTemp;
  639. put_be32(pb, 0x6c); /* size (always 0x6c) */
  640. put_tag(pb, "mvhd");
  641. put_be32(pb, 0); /* version & flags */
  642. put_be32(pb, mov->time); /* creation time */
  643. put_be32(pb, mov->time); /* modification time */
  644. put_be32(pb, mov->timescale); /* timescale */
  645. for (i=0; i<MAX_STREAMS; i++) {
  646. if(mov->tracks[i].entry > 0) {
  647. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
  648. if(maxTrackLen < maxTrackLenTemp)
  649. maxTrackLen = maxTrackLenTemp;
  650. if(maxTrackID < mov->tracks[i].trackID)
  651. maxTrackID = mov->tracks[i].trackID;
  652. }
  653. }
  654. put_be32(pb, maxTrackLen); /* duration of longest track */
  655. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  656. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  657. put_be16(pb, 0); /* reserved */
  658. put_be32(pb, 0); /* reserved */
  659. put_be32(pb, 0); /* reserved */
  660. /* Matrix structure */
  661. put_be32(pb, 0x00010000); /* reserved */
  662. put_be32(pb, 0x0); /* reserved */
  663. put_be32(pb, 0x0); /* reserved */
  664. put_be32(pb, 0x0); /* reserved */
  665. put_be32(pb, 0x00010000); /* reserved */
  666. put_be32(pb, 0x0); /* reserved */
  667. put_be32(pb, 0x0); /* reserved */
  668. put_be32(pb, 0x0); /* reserved */
  669. put_be32(pb, 0x40000000); /* reserved */
  670. put_be32(pb, 0); /* reserved (preview time) */
  671. put_be32(pb, 0); /* reserved (preview duration) */
  672. put_be32(pb, 0); /* reserved (poster time) */
  673. put_be32(pb, 0); /* reserved (selection time) */
  674. put_be32(pb, 0); /* reserved (selection duration) */
  675. put_be32(pb, 0); /* reserved (current time) */
  676. put_be32(pb, maxTrackID+1); /* Next track id */
  677. return 0x6c;
  678. }
  679. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
  680. AVFormatContext *s)
  681. {
  682. int pos = url_ftell(pb);
  683. int i;
  684. put_be32(pb, 0); /* size */
  685. put_tag(pb, "udta");
  686. /* Requirements */
  687. for (i=0; i<MAX_STREAMS; i++) {
  688. if(mov->tracks[i].entry <= 0) continue;
  689. if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
  690. mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
  691. int pos = url_ftell(pb);
  692. put_be32(pb, 0); /* size */
  693. put_tag(pb, "\251req");
  694. put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
  695. put_be16(pb, 0);
  696. put_buffer(pb, "QuickTime 6.0 or greater",
  697. sizeof("QuickTime 6.0 or greater") - 1);
  698. updateSize(pb, pos);
  699. break;
  700. }
  701. }
  702. /* Encoder */
  703. if(!(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
  704. {
  705. int pos = url_ftell(pb);
  706. put_be32(pb, 0); /* size */
  707. put_tag(pb, "\251enc");
  708. put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
  709. put_be16(pb, 0);
  710. put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
  711. updateSize(pb, pos);
  712. }
  713. if( s->title[0] )
  714. {
  715. int pos = url_ftell(pb);
  716. put_be32(pb, 0); /* size */
  717. put_tag(pb, "\251nam");
  718. put_be16(pb, strlen(s->title)); /* string length */
  719. put_be16(pb, 0);
  720. put_buffer(pb, s->title, strlen(s->title));
  721. updateSize(pb, pos);
  722. }
  723. if( s->author[0] )
  724. {
  725. int pos = url_ftell(pb);
  726. put_be32(pb, 0); /* size */
  727. put_tag(pb, /*"\251aut"*/ "\251day" );
  728. put_be16(pb, strlen(s->author)); /* string length */
  729. put_be16(pb, 0);
  730. put_buffer(pb, s->author, strlen(s->author));
  731. updateSize(pb, pos);
  732. }
  733. if( s->comment[0] )
  734. {
  735. int pos = url_ftell(pb);
  736. put_be32(pb, 0); /* size */
  737. put_tag(pb, "\251des");
  738. put_be16(pb, strlen(s->comment)); /* string length */
  739. put_be16(pb, 0);
  740. put_buffer(pb, s->comment, strlen(s->comment));
  741. updateSize(pb, pos);
  742. }
  743. return updateSize(pb, pos);
  744. }
  745. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  746. AVFormatContext *s)
  747. {
  748. int pos, i;
  749. pos = url_ftell(pb);
  750. put_be32(pb, 0); /* size placeholder*/
  751. put_tag(pb, "moov");
  752. mov->timescale = globalTimescale;
  753. for (i=0; i<MAX_STREAMS; i++) {
  754. if(mov->tracks[i].entry <= 0) continue;
  755. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  756. mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
  757. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
  758. }
  759. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  760. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  761. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  762. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  763. mov->tracks[i].timescale = 8000;
  764. }
  765. else {
  766. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  767. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  768. }
  769. }
  770. mov->tracks[i].trackDuration =
  771. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  772. mov->tracks[i].time = mov->time;
  773. mov->tracks[i].trackID = i+1;
  774. }
  775. mov_write_mvhd_tag(pb, mov);
  776. //mov_write_iods_tag(pb, mov);
  777. for (i=0; i<MAX_STREAMS; i++) {
  778. if(mov->tracks[i].entry > 0) {
  779. mov_write_trak_tag(pb, &(mov->tracks[i]));
  780. }
  781. }
  782. mov_write_udta_tag(pb, mov, s);
  783. return updateSize(pb, pos);
  784. }
  785. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  786. {
  787. mov->mdat_pos = url_ftell(pb);
  788. put_be32(pb, 0); /* size placeholder*/
  789. put_tag(pb, "mdat");
  790. return 0;
  791. }
  792. /* TODO: This needs to be more general */
  793. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  794. {
  795. MOVContext *mov = s->priv_data;
  796. put_be32(pb, 0x14 ); /* size */
  797. put_tag(pb, "ftyp");
  798. if ( mov->mode == MODE_3GP )
  799. put_tag(pb, "3gp4");
  800. else
  801. put_tag(pb, "isom");
  802. put_be32(pb, 0x200 );
  803. if ( mov->mode == MODE_3GP )
  804. put_tag(pb, "3gp4");
  805. else
  806. put_tag(pb, "mp41");
  807. return 0x14;
  808. }
  809. static int mov_write_header(AVFormatContext *s)
  810. {
  811. ByteIOContext *pb = &s->pb;
  812. MOVContext *mov = s->priv_data;
  813. int i;
  814. for(i=0; i<s->nb_streams; i++){
  815. AVCodecContext *c= &s->streams[i]->codec;
  816. if (c->codec_type == CODEC_TYPE_VIDEO){
  817. if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
  818. if(!codec_get_tag(codec_bmp_tags, c->codec_id))
  819. return -1;
  820. else
  821. av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
  822. }
  823. }else if(c->codec_type == CODEC_TYPE_AUDIO){
  824. if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
  825. if(!codec_get_tag(codec_wav_tags, c->codec_id))
  826. return -1;
  827. else
  828. av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
  829. }
  830. }
  831. }
  832. /* Default mode == MP4 */
  833. mov->mode = MODE_MP4;
  834. if (s->oformat != NULL) {
  835. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  836. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  837. if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
  838. mov_write_ftyp_tag(pb,s);
  839. }
  840. for (i=0; i<MAX_STREAMS; i++) {
  841. mov->tracks[i].mode = mov->mode;
  842. }
  843. put_flush_packet(pb);
  844. return 0;
  845. }
  846. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  847. {
  848. MOVContext *mov = s->priv_data;
  849. ByteIOContext *pb = &s->pb;
  850. AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
  851. MOVTrack* trk = &mov->tracks[pkt->stream_index];
  852. int cl, id;
  853. unsigned int samplesInChunk = 0;
  854. int size= pkt->size;
  855. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  856. if (!size) return 0; /* Discard 0 sized packets */
  857. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  858. samplesInChunk = 1;
  859. }
  860. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  861. if( enc->codec_id == CODEC_ID_AMR_NB) {
  862. /* We must find out how many AMR blocks there are in one packet */
  863. static uint16_t packed_size[16] =
  864. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  865. int len = 0;
  866. while (len < size && samplesInChunk < 100) {
  867. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  868. samplesInChunk++;
  869. }
  870. }
  871. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  872. samplesInChunk = size/enc->channels;
  873. }
  874. else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
  875. samplesInChunk = size/(2*enc->channels);
  876. }
  877. else {
  878. samplesInChunk = 1;
  879. }
  880. }
  881. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  882. && trk->vosLen == 0) {
  883. // assert(enc->extradata_size);
  884. trk->vosLen = enc->extradata_size;
  885. trk->vosData = av_malloc(trk->vosLen);
  886. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  887. }
  888. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  889. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  890. if (trk->ents_allocated <= trk->entry) {
  891. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  892. if (!trk->cluster)
  893. return -1;
  894. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  895. if (!trk->cluster[cl])
  896. return -1;
  897. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  898. }
  899. if (mov->mdat_written == 0) {
  900. mov_write_mdat_tag(pb, mov);
  901. mov->mdat_written = 1;
  902. mov->time = s->timestamp;
  903. }
  904. trk->cluster[cl][id].pos = url_ftell(pb);
  905. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  906. trk->cluster[cl][id].size = size;
  907. trk->cluster[cl][id].entries = samplesInChunk;
  908. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  909. trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  910. if(trk->cluster[cl][id].key_frame)
  911. trk->hasKeyframes = 1;
  912. }
  913. trk->enc = enc;
  914. trk->entry++;
  915. trk->sampleCount += samplesInChunk;
  916. trk->mdat_size += size;
  917. put_buffer(pb, pkt->data, size);
  918. put_flush_packet(pb);
  919. return 0;
  920. }
  921. static int mov_write_trailer(AVFormatContext *s)
  922. {
  923. MOVContext *mov = s->priv_data;
  924. ByteIOContext *pb = &s->pb;
  925. int res = 0;
  926. int i, j;
  927. offset_t moov_pos = url_ftell(pb);
  928. /* Write size of mdat tag */
  929. for (i=0, j=0; i<MAX_STREAMS; i++) {
  930. if(mov->tracks[i].ents_allocated > 0) {
  931. j += mov->tracks[i].mdat_size;
  932. }
  933. }
  934. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  935. put_be32(pb, j+8);
  936. url_fseek(pb, moov_pos, SEEK_SET);
  937. mov_write_moov_tag(pb, mov, s);
  938. for (i=0; i<MAX_STREAMS; i++) {
  939. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  940. av_free(mov->tracks[i].cluster[j]);
  941. }
  942. av_free(mov->tracks[i].cluster);
  943. if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
  944. mov->tracks[i].cluster = NULL;
  945. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  946. }
  947. put_flush_packet(pb);
  948. return res;
  949. }
  950. static AVOutputFormat mov_oformat = {
  951. "mov",
  952. "mov format",
  953. NULL,
  954. "mov",
  955. sizeof(MOVContext),
  956. CODEC_ID_AAC,
  957. CODEC_ID_MPEG4,
  958. mov_write_header,
  959. mov_write_packet,
  960. mov_write_trailer,
  961. };
  962. static AVOutputFormat _3gp_oformat = {
  963. "3gp",
  964. "3gp format",
  965. NULL,
  966. "3gp",
  967. sizeof(MOVContext),
  968. CODEC_ID_AMR_NB,
  969. CODEC_ID_H263,
  970. mov_write_header,
  971. mov_write_packet,
  972. mov_write_trailer,
  973. };
  974. static AVOutputFormat mp4_oformat = {
  975. "mp4",
  976. "mp4 format",
  977. "application/mp4",
  978. "mp4,m4a",
  979. sizeof(MOVContext),
  980. CODEC_ID_AAC,
  981. CODEC_ID_MPEG4,
  982. mov_write_header,
  983. mov_write_packet,
  984. mov_write_trailer,
  985. };
  986. int movenc_init(void)
  987. {
  988. av_register_output_format(&mov_oformat);
  989. av_register_output_format(&_3gp_oformat);
  990. av_register_output_format(&mp4_oformat);
  991. return 0;
  992. }