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.

1111 lines
32KB

  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. static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
  211. {
  212. int pos = url_ftell(pb);
  213. put_be32(pb, 0); /* size */
  214. if(track->enc->codec_id == CODEC_ID_PCM_MULAW)
  215. put_tag(pb, "ulaw");
  216. else if(track->enc->codec_id == CODEC_ID_PCM_ALAW)
  217. put_tag(pb, "alaw");
  218. else if(track->enc->codec_id == CODEC_ID_ADPCM_IMA_QT)
  219. put_tag(pb, "ima4");
  220. else if(track->enc->codec_id == CODEC_ID_MACE3)
  221. put_tag(pb, "MAC3");
  222. else if(track->enc->codec_id == CODEC_ID_MACE6)
  223. put_tag(pb, "MAC6");
  224. else if(track->enc->codec_id == CODEC_ID_AAC)
  225. put_tag(pb, "mp4a");
  226. else if(track->enc->codec_id == CODEC_ID_AMR_NB)
  227. put_tag(pb, "samr");
  228. else if(track->enc->codec_id == CODEC_ID_PCM_S16BE)
  229. put_tag(pb, "twos");
  230. else if(track->enc->codec_id == CODEC_ID_PCM_S16LE)
  231. put_tag(pb, "sowt");
  232. else
  233. put_tag(pb, " ");
  234. put_be32(pb, 0); /* Reserved */
  235. put_be16(pb, 0); /* Reserved */
  236. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  237. /* SoundDescription */
  238. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  239. put_be16(pb, 1); /* Version 1 */
  240. else
  241. put_be16(pb, 0); /* Version 0 */
  242. put_be16(pb, 0); /* Revision level */
  243. put_be32(pb, 0); /* Reserved */
  244. put_be16(pb, track->enc->channels); /* Number of channels */
  245. /* TODO: Currently hard-coded to 16-bit, there doesn't seem
  246. to be a good way to get number of bits of audio */
  247. put_be16(pb, 0x10); /* Reserved */
  248. put_be16(pb, 0); /* compression ID (= 0) */
  249. put_be16(pb, 0); /* packet size (= 0) */
  250. put_be16(pb, track->timescale); /* Time scale */
  251. put_be16(pb, 0); /* Reserved */
  252. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  253. {
  254. /* SoundDescription V1 extended info */
  255. put_be32(pb, track->enc->frame_size); /* Samples per packet */
  256. put_be32(pb, 1536); /* Bytes per packet */
  257. put_be32(pb, 2); /* Bytes per frame */
  258. put_be32(pb, 2); /* Bytes per sample */
  259. }
  260. if(track->enc->codec_id == CODEC_ID_AAC) {
  261. if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
  262. else mov_write_esds_tag(pb, track);
  263. }
  264. if(track->enc->codec_id == CODEC_ID_AMR_NB)
  265. mov_write_damr_tag(pb);
  266. return updateSize (pb, pos);
  267. }
  268. static int mov_write_d263_tag(ByteIOContext *pb)
  269. {
  270. put_be32(pb, 0xf); /* size */
  271. put_tag(pb, "d263");
  272. put_tag(pb, "FFMP");
  273. put_be16(pb, 0x0a);
  274. put_byte(pb, 0);
  275. return 0xf;
  276. }
  277. /* TODO: No idea about these values */
  278. static int mov_write_svq3_tag(ByteIOContext *pb)
  279. {
  280. put_be32(pb, 0x15);
  281. put_tag(pb, "SMI ");
  282. put_tag(pb, "SEQH");
  283. put_be32(pb, 0x5);
  284. put_be32(pb, 0xe2c0211d);
  285. put_be32(pb, 0xc0000000);
  286. put_byte(pb, 0);
  287. return 0x15;
  288. }
  289. static unsigned int descrLength(unsigned int len)
  290. {
  291. if (len < 0x00000080)
  292. return 2 + len;
  293. else if (len < 0x00004000)
  294. return 3 + len;
  295. else if(len < 0x00200000)
  296. return 4 + len;
  297. else
  298. return 5 + len;
  299. }
  300. static void putDescr(ByteIOContext *pb, int tag, int size)
  301. {
  302. uint32_t len;
  303. uint8_t vals[4];
  304. len = size;
  305. vals[3] = (uint8_t)(len & 0x7f);
  306. len >>= 7;
  307. vals[2] = (uint8_t)((len & 0x7f) | 0x80);
  308. len >>= 7;
  309. vals[1] = (uint8_t)((len & 0x7f) | 0x80);
  310. len >>= 7;
  311. vals[0] = (uint8_t)((len & 0x7f) | 0x80);
  312. put_byte(pb, tag); // DescriptorTag
  313. if (size < 0x00000080)
  314. {
  315. put_byte(pb, vals[3]);
  316. }
  317. else if (size < 0x00004000)
  318. {
  319. put_byte(pb, vals[2]);
  320. put_byte(pb, vals[3]);
  321. }
  322. else if (size < 0x00200000)
  323. {
  324. put_byte(pb, vals[1]);
  325. put_byte(pb, vals[2]);
  326. put_byte(pb, vals[3]);
  327. }
  328. else if (size < 0x10000000)
  329. {
  330. put_byte(pb, vals[0]);
  331. put_byte(pb, vals[1]);
  332. put_byte(pb, vals[2]);
  333. put_byte(pb, vals[3]);
  334. }
  335. }
  336. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
  337. {
  338. int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  339. int pos = url_ftell(pb);
  340. put_be32(pb, 0); // size
  341. put_tag(pb, "esds");
  342. put_be32(pb, 0); // Version
  343. // ES descriptor
  344. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  345. descrLength(1));
  346. put_be16(pb, 0x0001); // ID (= 1)
  347. put_byte(pb, 0x00); // flags (= no flags)
  348. // DecoderConfig descriptor
  349. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  350. if(track->enc->codec_id == CODEC_ID_AAC)
  351. put_byte(pb, 0x40); // Object type indication
  352. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  353. put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
  354. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  355. put_byte(pb, 0x15); // flags (= Audiostream)
  356. else
  357. put_byte(pb, 0x11); // flags (= Visualstream)
  358. put_byte(pb, 0x0); // Buffersize DB (24 bits)
  359. put_be16(pb, 0x0dd2); // Buffersize DB
  360. // TODO: find real values for these
  361. put_be32(pb, 0x0002e918); // maxbitrate
  362. put_be32(pb, 0x00017e6b); // avg bitrate
  363. if (track->vosLen)
  364. {
  365. // DecoderSpecific info descriptor
  366. putDescr(pb, 0x05, track->vosLen);
  367. put_buffer(pb, track->vosData, track->vosLen);
  368. }
  369. // SL descriptor
  370. putDescr(pb, 0x06, 1);
  371. put_byte(pb, 0x02);
  372. return updateSize (pb, pos);
  373. }
  374. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
  375. {
  376. int pos = url_ftell(pb);
  377. put_be32(pb, 0); /* size */
  378. if(track->enc->codec_id == CODEC_ID_SVQ1)
  379. put_tag(pb, "SVQ1");
  380. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  381. put_tag(pb, "SVQ3");
  382. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  383. put_tag(pb, "mp4v");
  384. else if(track->enc->codec_id == CODEC_ID_H263)
  385. put_tag(pb, "s263");
  386. else if(track->enc->codec_id == CODEC_ID_DVVIDEO)
  387. put_tag(pb, "dvc ");
  388. else
  389. put_tag(pb, " "); /* Unknown tag */
  390. put_be32(pb, 0); /* Reserved */
  391. put_be16(pb, 0); /* Reserved */
  392. put_be16(pb, 1); /* Data-reference index */
  393. put_be32(pb, 0); /* Reserved (= 02000c) */
  394. put_be32(pb, 0); /* Reserved ("SVis")*/
  395. put_be32(pb, 0); /* Reserved */
  396. put_be32(pb, 0); /* Reserved (400)*/
  397. put_be16(pb, track->enc->width); /* Video width */
  398. put_be16(pb, track->enc->height); /* Video height */
  399. put_be32(pb, 0x00480000); /* Reserved */
  400. put_be32(pb, 0x00480000); /* Reserved */
  401. put_be32(pb, 0); /* Data size (= 0) */
  402. put_be16(pb, 1); /* Frame count (= 1) */
  403. put_be32(pb, 0); /* Reserved */
  404. put_be32(pb, 0); /* Reserved */
  405. put_be32(pb, 0); /* Reserved */
  406. put_be32(pb, 0); /* Reserved */
  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_be16(pb, 0x18); /* Reserved */
  412. put_be16(pb, 0xffff); /* Reserved */
  413. if(track->enc->codec_id == CODEC_ID_MPEG4)
  414. mov_write_esds_tag(pb, track);
  415. else if(track->enc->codec_id == CODEC_ID_H263)
  416. mov_write_d263_tag(pb);
  417. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  418. mov_write_svq3_tag(pb);
  419. return updateSize (pb, pos);
  420. }
  421. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
  422. {
  423. int pos = url_ftell(pb);
  424. put_be32(pb, 0); /* size */
  425. put_tag(pb, "stsd");
  426. put_be32(pb, 0); /* version & flags */
  427. put_be32(pb, 1); /* entry count */
  428. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  429. mov_write_video_tag(pb, track);
  430. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  431. mov_write_audio_tag(pb, track);
  432. return updateSize(pb, pos);
  433. }
  434. /* TODO?: Currently all samples/frames seem to have same duration */
  435. /* Time to sample atom */
  436. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
  437. {
  438. put_be32(pb, 0x18); /* size */
  439. put_tag(pb, "stts");
  440. put_be32(pb, 0); /* version & flags */
  441. put_be32(pb, 1); /* entry count */
  442. put_be32(pb, track->sampleCount); /* sample count */
  443. put_be32(pb, track->sampleDuration); /* sample duration */
  444. return 0x18;
  445. }
  446. static int mov_write_dref_tag(ByteIOContext *pb)
  447. {
  448. put_be32(pb, 28); /* size */
  449. put_tag(pb, "dref");
  450. put_be32(pb, 0); /* version & flags */
  451. put_be32(pb, 1); /* entry count */
  452. put_be32(pb, 0xc); /* size */
  453. put_tag(pb, "url ");
  454. put_be32(pb, 1); /* version & flags */
  455. return 28;
  456. }
  457. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
  458. {
  459. int pos = url_ftell(pb);
  460. put_be32(pb, 0); /* size */
  461. put_tag(pb, "stbl");
  462. mov_write_stsd_tag(pb, track);
  463. mov_write_stts_tag(pb, track);
  464. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  465. track->hasKeyframes)
  466. mov_write_stss_tag(pb, track);
  467. mov_write_stsc_tag(pb, track);
  468. mov_write_stsz_tag(pb, track);
  469. mov_write_stco_tag(pb, track);
  470. return updateSize(pb, pos);
  471. }
  472. static int mov_write_dinf_tag(ByteIOContext *pb)
  473. {
  474. int pos = url_ftell(pb);
  475. put_be32(pb, 0); /* size */
  476. put_tag(pb, "dinf");
  477. mov_write_dref_tag(pb);
  478. return updateSize(pb, pos);
  479. }
  480. static int mov_write_smhd_tag(ByteIOContext *pb)
  481. {
  482. put_be32(pb, 16); /* size */
  483. put_tag(pb, "smhd");
  484. put_be32(pb, 0); /* version & flags */
  485. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  486. put_be16(pb, 0); /* reserved */
  487. return 16;
  488. }
  489. static int mov_write_vmhd_tag(ByteIOContext *pb)
  490. {
  491. put_be32(pb, 0x14); /* size (always 0x14) */
  492. put_tag(pb, "vmhd");
  493. put_be32(pb, 0x01); /* version & flags */
  494. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  495. return 0x14;
  496. }
  497. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
  498. {
  499. char *descr, *hdlr, *hdlr_type;
  500. int pos = url_ftell(pb);
  501. if (!track) { /* no media --> data handler */
  502. hdlr = "dhlr";
  503. hdlr_type = "url ";
  504. descr = "DataHandler";
  505. } else {
  506. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  507. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  508. hdlr_type = "vide";
  509. descr = "VideoHandler";
  510. } else {
  511. hdlr_type = "soun";
  512. descr = "SoundHandler";
  513. }
  514. }
  515. put_be32(pb, 0); /* size */
  516. put_tag(pb, "hdlr");
  517. put_be32(pb, 0); /* Version & flags */
  518. put_buffer(pb, hdlr, 4); /* handler */
  519. put_tag(pb, hdlr_type); /* handler type */
  520. put_be32(pb ,0); /* reserved */
  521. put_be32(pb ,0); /* reserved */
  522. put_be32(pb ,0); /* reserved */
  523. put_byte(pb, strlen(descr)); /* string counter */
  524. put_buffer(pb, descr, strlen(descr)); /* handler description */
  525. return updateSize(pb, pos);
  526. }
  527. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
  528. {
  529. int pos = url_ftell(pb);
  530. put_be32(pb, 0); /* size */
  531. put_tag(pb, "minf");
  532. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  533. mov_write_vmhd_tag(pb);
  534. else
  535. mov_write_smhd_tag(pb);
  536. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  537. mov_write_hdlr_tag(pb, NULL);
  538. mov_write_dinf_tag(pb);
  539. mov_write_stbl_tag(pb, track);
  540. return updateSize(pb, pos);
  541. }
  542. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
  543. {
  544. put_be32(pb, 32); /* size */
  545. put_tag(pb, "mdhd");
  546. put_be32(pb, 0); /* Version & flags */
  547. put_be32(pb, track->time); /* creation time */
  548. put_be32(pb, track->time); /* modification time */
  549. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  550. put_be32(pb, track->trackDuration); /* duration */
  551. put_be16(pb, 0); /* language, 0 = english */
  552. put_be16(pb, 0); /* reserved (quality) */
  553. return 32;
  554. }
  555. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
  556. {
  557. int pos = url_ftell(pb);
  558. put_be32(pb, 0); /* size */
  559. put_tag(pb, "mdia");
  560. mov_write_mdhd_tag(pb, track);
  561. mov_write_hdlr_tag(pb, track);
  562. mov_write_minf_tag(pb, track);
  563. return updateSize(pb, pos);
  564. }
  565. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
  566. {
  567. int64_t maxTrackLenTemp;
  568. put_be32(pb, 0x5c); /* size (always 0x5c) */
  569. put_tag(pb, "tkhd");
  570. put_be32(pb, 0xf); /* version & flags (track enabled) */
  571. put_be32(pb, track->time); /* creation time */
  572. put_be32(pb, track->time); /* modification time */
  573. put_be32(pb, track->trackID); /* track-id */
  574. put_be32(pb, 0); /* reserved */
  575. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
  576. put_be32(pb, (long)maxTrackLenTemp); /* duration */
  577. put_be32(pb, 0); /* reserved */
  578. put_be32(pb, 0); /* reserved */
  579. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  580. /* Volume, only for audio */
  581. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  582. put_be16(pb, 0x0100);
  583. else
  584. put_be16(pb, 0);
  585. put_be16(pb, 0); /* reserved */
  586. /* Matrix structure */
  587. put_be32(pb, 0x00010000); /* reserved */
  588. put_be32(pb, 0x0); /* reserved */
  589. put_be32(pb, 0x0); /* reserved */
  590. put_be32(pb, 0x0); /* reserved */
  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, 0x40000000); /* reserved */
  596. /* Track width and height, for visual only */
  597. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  598. double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
  599. if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
  600. put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  601. put_be32(pb, track->enc->height*0x10000);
  602. }
  603. else {
  604. put_be32(pb, 0);
  605. put_be32(pb, 0);
  606. }
  607. return 0x5c;
  608. }
  609. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
  610. {
  611. int pos = url_ftell(pb);
  612. put_be32(pb, 0); /* size */
  613. put_tag(pb, "trak");
  614. mov_write_tkhd_tag(pb, track);
  615. mov_write_mdia_tag(pb, track);
  616. return updateSize(pb, pos);
  617. }
  618. /* TODO: Not sorted out, but not necessary either */
  619. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  620. {
  621. put_be32(pb, 0x15); /* size */
  622. put_tag(pb, "iods");
  623. put_be32(pb, 0); /* version & flags */
  624. put_be16(pb, 0x1007);
  625. put_byte(pb, 0);
  626. put_be16(pb, 0x4fff);
  627. put_be16(pb, 0xfffe);
  628. put_be16(pb, 0x01ff);
  629. return 0x15;
  630. }
  631. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  632. {
  633. int maxTrackID = 1, maxTrackLen = 0, i;
  634. int64_t maxTrackLenTemp;
  635. put_be32(pb, 0x6c); /* size (always 0x6c) */
  636. put_tag(pb, "mvhd");
  637. put_be32(pb, 0); /* version & flags */
  638. put_be32(pb, mov->time); /* creation time */
  639. put_be32(pb, mov->time); /* modification time */
  640. put_be32(pb, mov->timescale); /* timescale */
  641. for (i=0; i<MAX_STREAMS; i++) {
  642. if(mov->tracks[i].entry > 0) {
  643. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
  644. if(maxTrackLen < maxTrackLenTemp)
  645. maxTrackLen = maxTrackLenTemp;
  646. if(maxTrackID < mov->tracks[i].trackID)
  647. maxTrackID = mov->tracks[i].trackID;
  648. }
  649. }
  650. put_be32(pb, maxTrackLen); /* duration of longest track */
  651. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  652. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  653. put_be16(pb, 0); /* reserved */
  654. put_be32(pb, 0); /* reserved */
  655. put_be32(pb, 0); /* reserved */
  656. /* Matrix structure */
  657. put_be32(pb, 0x00010000); /* reserved */
  658. put_be32(pb, 0x0); /* reserved */
  659. put_be32(pb, 0x0); /* reserved */
  660. put_be32(pb, 0x0); /* reserved */
  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, 0x40000000); /* reserved */
  666. put_be32(pb, 0); /* reserved (preview time) */
  667. put_be32(pb, 0); /* reserved (preview duration) */
  668. put_be32(pb, 0); /* reserved (poster time) */
  669. put_be32(pb, 0); /* reserved (selection time) */
  670. put_be32(pb, 0); /* reserved (selection duration) */
  671. put_be32(pb, 0); /* reserved (current time) */
  672. put_be32(pb, maxTrackID+1); /* Next track id */
  673. return 0x6c;
  674. }
  675. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
  676. AVFormatContext *s)
  677. {
  678. int pos = url_ftell(pb);
  679. int i;
  680. put_be32(pb, 0); /* size */
  681. put_tag(pb, "udta");
  682. /* Requirements */
  683. for (i=0; i<MAX_STREAMS; i++) {
  684. if(mov->tracks[i].entry <= 0) continue;
  685. if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
  686. mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
  687. int pos = url_ftell(pb);
  688. put_be32(pb, 0); /* size */
  689. put_tag(pb, "\251req");
  690. put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
  691. put_be16(pb, 0);
  692. put_buffer(pb, "QuickTime 6.0 or greater",
  693. sizeof("QuickTime 6.0 or greater") - 1);
  694. updateSize(pb, pos);
  695. break;
  696. }
  697. }
  698. /* Encoder */
  699. {
  700. int pos = url_ftell(pb);
  701. put_be32(pb, 0); /* size */
  702. put_tag(pb, "\251enc");
  703. put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
  704. put_be16(pb, 0);
  705. put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
  706. updateSize(pb, pos);
  707. }
  708. if( s->title[0] )
  709. {
  710. int pos = url_ftell(pb);
  711. put_be32(pb, 0); /* size */
  712. put_tag(pb, "\251nam");
  713. put_be16(pb, strlen(s->title)); /* string length */
  714. put_be16(pb, 0);
  715. put_buffer(pb, s->title, strlen(s->title));
  716. updateSize(pb, pos);
  717. }
  718. if( s->author[0] )
  719. {
  720. int pos = url_ftell(pb);
  721. put_be32(pb, 0); /* size */
  722. put_tag(pb, /*"\251aut"*/ "\251day" );
  723. put_be16(pb, strlen(s->author)); /* string length */
  724. put_be16(pb, 0);
  725. put_buffer(pb, s->author, strlen(s->author));
  726. updateSize(pb, pos);
  727. }
  728. if( s->comment[0] )
  729. {
  730. int pos = url_ftell(pb);
  731. put_be32(pb, 0); /* size */
  732. put_tag(pb, "\251des");
  733. put_be16(pb, strlen(s->comment)); /* string length */
  734. put_be16(pb, 0);
  735. put_buffer(pb, s->comment, strlen(s->comment));
  736. updateSize(pb, pos);
  737. }
  738. return updateSize(pb, pos);
  739. }
  740. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  741. AVFormatContext *s)
  742. {
  743. int pos, i;
  744. pos = url_ftell(pb);
  745. put_be32(pb, 0); /* size placeholder*/
  746. put_tag(pb, "moov");
  747. mov->timescale = globalTimescale;
  748. for (i=0; i<MAX_STREAMS; i++) {
  749. if(mov->tracks[i].entry <= 0) continue;
  750. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  751. mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
  752. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
  753. }
  754. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  755. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  756. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  757. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  758. mov->tracks[i].timescale = 8000;
  759. }
  760. else {
  761. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  762. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  763. }
  764. }
  765. mov->tracks[i].trackDuration =
  766. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  767. mov->tracks[i].time = mov->time;
  768. mov->tracks[i].trackID = i+1;
  769. }
  770. mov_write_mvhd_tag(pb, mov);
  771. //mov_write_iods_tag(pb, mov);
  772. for (i=0; i<MAX_STREAMS; i++) {
  773. if(mov->tracks[i].entry > 0) {
  774. mov_write_trak_tag(pb, &(mov->tracks[i]));
  775. }
  776. }
  777. mov_write_udta_tag(pb, mov, s);
  778. return updateSize(pb, pos);
  779. }
  780. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  781. {
  782. mov->mdat_pos = url_ftell(pb);
  783. put_be32(pb, 0); /* size placeholder*/
  784. put_tag(pb, "mdat");
  785. return 0;
  786. }
  787. /* TODO: This needs to be more general */
  788. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  789. {
  790. MOVContext *mov = s->priv_data;
  791. put_be32(pb, 0x14 ); /* size */
  792. put_tag(pb, "ftyp");
  793. if ( mov->mode == MODE_3GP )
  794. put_tag(pb, "3gp4");
  795. else
  796. put_tag(pb, "isom");
  797. put_be32(pb, 0x200 );
  798. if ( mov->mode == MODE_3GP )
  799. put_tag(pb, "3gp4");
  800. else
  801. put_tag(pb, "mp41");
  802. return 0x14;
  803. }
  804. static int mov_write_header(AVFormatContext *s)
  805. {
  806. ByteIOContext *pb = &s->pb;
  807. MOVContext *mov = s->priv_data;
  808. int i;
  809. /* Default mode == MP4 */
  810. mov->mode = MODE_MP4;
  811. if (s->oformat != NULL) {
  812. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  813. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  814. if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
  815. mov_write_ftyp_tag(pb,s);
  816. }
  817. for (i=0; i<MAX_STREAMS; i++) {
  818. mov->tracks[i].mode = mov->mode;
  819. }
  820. put_flush_packet(pb);
  821. return 0;
  822. }
  823. static int Timestamp(void) {
  824. return 1067949799U+(24107*86400); //its the modification time of this line :)
  825. }
  826. static int mov_write_packet(AVFormatContext *s, int stream_index,
  827. const uint8_t *buf, int size, int64_t pts)
  828. {
  829. MOVContext *mov = s->priv_data;
  830. ByteIOContext *pb = &s->pb;
  831. AVCodecContext *enc = &s->streams[stream_index]->codec;
  832. MOVTrack* trk = &mov->tracks[stream_index];
  833. int cl, id;
  834. unsigned int samplesInChunk = 0;
  835. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  836. if (!size) return 0; /* Discard 0 sized packets */
  837. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  838. samplesInChunk = 1;
  839. }
  840. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  841. if( enc->codec_id == CODEC_ID_AMR_NB) {
  842. /* We must find out how many AMR blocks there are in one packet */
  843. static uint16_t packed_size[16] =
  844. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  845. int len = 0;
  846. while (len < size && samplesInChunk < 100) {
  847. len += packed_size[(buf[len] >> 3) & 0x0F];
  848. samplesInChunk++;
  849. }
  850. }
  851. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  852. samplesInChunk = size/enc->channels;
  853. }
  854. else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
  855. samplesInChunk = size/(2*enc->channels);
  856. }
  857. else {
  858. samplesInChunk = 1;
  859. }
  860. }
  861. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  862. && trk->vosLen == 0) {
  863. assert(enc->extradata_size);
  864. trk->vosLen = enc->extradata_size;
  865. trk->vosData = av_malloc(trk->vosLen);
  866. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  867. }
  868. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  869. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  870. if (trk->ents_allocated <= trk->entry) {
  871. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  872. if (!trk->cluster)
  873. return -1;
  874. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  875. if (!trk->cluster[cl])
  876. return -1;
  877. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  878. }
  879. if (mov->mdat_written == 0) {
  880. mov_write_mdat_tag(pb, mov);
  881. mov->mdat_written = 1;
  882. mov->time = Timestamp();
  883. }
  884. trk->cluster[cl][id].pos = url_ftell(pb);
  885. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  886. trk->cluster[cl][id].size = size;
  887. trk->cluster[cl][id].entries = samplesInChunk;
  888. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  889. trk->cluster[cl][id].key_frame = enc->coded_frame->key_frame;
  890. if(enc->coded_frame->pict_type == FF_I_TYPE)
  891. trk->hasKeyframes = 1;
  892. }
  893. trk->enc = enc;
  894. trk->entry++;
  895. trk->sampleCount += samplesInChunk;
  896. trk->mdat_size += size;
  897. put_buffer(pb, buf, size);
  898. put_flush_packet(pb);
  899. return 0;
  900. }
  901. static int mov_write_trailer(AVFormatContext *s)
  902. {
  903. MOVContext *mov = s->priv_data;
  904. ByteIOContext *pb = &s->pb;
  905. int res = 0;
  906. int i, j;
  907. offset_t moov_pos = url_ftell(pb);
  908. /* Write size of mdat tag */
  909. for (i=0, j=0; i<MAX_STREAMS; i++) {
  910. if(mov->tracks[i].ents_allocated > 0) {
  911. j += mov->tracks[i].mdat_size;
  912. }
  913. }
  914. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  915. put_be32(pb, j+8);
  916. url_fseek(pb, moov_pos, SEEK_SET);
  917. mov_write_moov_tag(pb, mov, s);
  918. for (i=0; i<MAX_STREAMS; i++) {
  919. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  920. av_free(mov->tracks[i].cluster[j]);
  921. }
  922. av_free(mov->tracks[i].cluster);
  923. if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
  924. mov->tracks[i].cluster = NULL;
  925. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  926. }
  927. put_flush_packet(pb);
  928. return res;
  929. }
  930. static AVOutputFormat mov_oformat = {
  931. "mov",
  932. "mov format",
  933. NULL,
  934. "mov",
  935. sizeof(MOVContext),
  936. CODEC_ID_AAC,
  937. CODEC_ID_MPEG4,
  938. mov_write_header,
  939. mov_write_packet,
  940. mov_write_trailer,
  941. };
  942. static AVOutputFormat _3gp_oformat = {
  943. "3gp",
  944. "3gp format",
  945. NULL,
  946. "3gp",
  947. sizeof(MOVContext),
  948. CODEC_ID_AMR_NB,
  949. CODEC_ID_H263,
  950. mov_write_header,
  951. mov_write_packet,
  952. mov_write_trailer,
  953. };
  954. static AVOutputFormat mp4_oformat = {
  955. "mp4",
  956. "mp4 format",
  957. "application/mp4",
  958. "mp4,m4a",
  959. sizeof(MOVContext),
  960. CODEC_ID_AAC,
  961. CODEC_ID_MPEG4,
  962. mov_write_header,
  963. mov_write_packet,
  964. mov_write_trailer,
  965. };
  966. int movenc_init(void)
  967. {
  968. av_register_output_format(&mov_oformat);
  969. av_register_output_format(&_3gp_oformat);
  970. av_register_output_format(&mp4_oformat);
  971. return 0;
  972. }