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.

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