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.

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