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.

953 lines
28KB

  1. /*
  2. * MOV, 3GP, MP4 encoder.
  3. * Copyright (c) 2003 Thomas Raivio.
  4. * Enhancements by Gildas Bazin <gbazin@netcourrier.com>
  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. #include <time.h>
  24. #undef NDEBUG
  25. #include <assert.h>
  26. #define MOV_INDEX_CLUSTER_SIZE 16384
  27. #define globalTimescale 1000
  28. typedef struct MOVIentry {
  29. unsigned int flags, pos, size;
  30. unsigned int samplesInChunk;
  31. char key_frame;
  32. unsigned int entries;
  33. } MOVIentry;
  34. typedef struct MOVIndex {
  35. int entry;
  36. int mdat_size;
  37. int ents_allocated;
  38. long timescale;
  39. long time;
  40. long trackDuration;
  41. long sampleCount;
  42. long sampleDuration;
  43. int hasKeyframes;
  44. int trackID;
  45. AVCodecContext *enc;
  46. int vosLen;
  47. uint8_t *vosData;
  48. MOVIentry** cluster;
  49. } MOVTrack;
  50. typedef struct {
  51. long time;
  52. int nb_streams;
  53. int mdat_written;
  54. offset_t mdat_pos;
  55. offset_t movi_list;
  56. long timescale;
  57. MOVTrack tracks[MAX_STREAMS];
  58. } MOVContext;
  59. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
  60. //FIXME supprt 64bit varaint with wide placeholders
  61. static int updateSize (ByteIOContext *pb, int pos)
  62. {
  63. long curpos = url_ftell(pb);
  64. url_fseek(pb, pos, SEEK_SET);
  65. put_be32(pb, curpos - pos); /* rewrite size */
  66. url_fseek(pb, curpos, SEEK_SET);
  67. return curpos - pos;
  68. }
  69. /* Chunk offset atom */
  70. static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
  71. {
  72. int i;
  73. int pos = url_ftell(pb);
  74. put_be32(pb, 0); /* size */
  75. put_tag(pb, "stco");
  76. put_be32(pb, 0); /* version & flags */
  77. put_be32(pb, track->entry); /* entry count */
  78. for (i=0; i<track->entry; i++) {
  79. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  80. int id = i % MOV_INDEX_CLUSTER_SIZE;
  81. put_be32(pb, track->cluster[cl][id].pos);
  82. }
  83. return updateSize (pb, pos);
  84. }
  85. /* Sample size atom */
  86. static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
  87. {
  88. int equalChunks = 1;
  89. int i, j, entries = 0, tst = -1, oldtst = -1;
  90. int pos = url_ftell(pb);
  91. put_be32(pb, 0); /* size */
  92. put_tag(pb, "stsz");
  93. put_be32(pb, 0); /* version & flags */
  94. for (i=0; i<track->entry; i++) {
  95. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  96. int id = i % MOV_INDEX_CLUSTER_SIZE;
  97. tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
  98. if(oldtst != -1 && tst != oldtst) {
  99. equalChunks = 0;
  100. }
  101. oldtst = tst;
  102. entries += track->cluster[cl][id].entries;
  103. }
  104. if (equalChunks) {
  105. int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
  106. put_be32(pb, sSize); // sample size
  107. put_be32(pb, entries); // sample count
  108. }
  109. else {
  110. put_be32(pb, 0); // sample size
  111. put_be32(pb, entries); // sample count
  112. for (i=0; i<track->entry; i++) {
  113. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  114. int id = i % MOV_INDEX_CLUSTER_SIZE;
  115. for ( j=0; j<track->cluster[cl][id].entries; j++) {
  116. put_be32(pb, track->cluster[cl][id].size /
  117. track->cluster[cl][id].entries);
  118. }
  119. }
  120. }
  121. return updateSize (pb, pos);
  122. }
  123. /* Sample to chunk atom */
  124. static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
  125. {
  126. int index = 0, oldval = -1, i, entryPos, curpos;
  127. int pos = url_ftell(pb);
  128. put_be32(pb, 0); /* size */
  129. put_tag(pb, "stsc");
  130. put_be32(pb, 0); // version & flags
  131. entryPos = url_ftell(pb);
  132. put_be32(pb, track->entry); // entry count
  133. for (i=0; i<track->entry; i++) {
  134. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  135. int id = i % MOV_INDEX_CLUSTER_SIZE;
  136. if(oldval != track->cluster[cl][id].samplesInChunk)
  137. {
  138. put_be32(pb, i+1); // first chunk
  139. put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
  140. put_be32(pb, 0x1); // sample description index
  141. oldval = track->cluster[cl][id].samplesInChunk;
  142. index++;
  143. }
  144. }
  145. curpos = url_ftell(pb);
  146. url_fseek(pb, entryPos, SEEK_SET);
  147. put_be32(pb, index); // rewrite size
  148. url_fseek(pb, curpos, SEEK_SET);
  149. return updateSize (pb, pos);
  150. }
  151. /* Sync sample atom */
  152. static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
  153. {
  154. long curpos;
  155. int i, index = 0, entryPos;
  156. int pos = url_ftell(pb);
  157. put_be32(pb, 0); // size
  158. put_tag(pb, "stss");
  159. put_be32(pb, 0); // version & flags
  160. entryPos = url_ftell(pb);
  161. put_be32(pb, track->entry); // entry count
  162. for (i=0; i<track->entry; i++) {
  163. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  164. int id = i % MOV_INDEX_CLUSTER_SIZE;
  165. if(track->cluster[cl][id].key_frame == 1) {
  166. put_be32(pb, i+1);
  167. index++;
  168. }
  169. }
  170. curpos = url_ftell(pb);
  171. url_fseek(pb, entryPos, SEEK_SET);
  172. put_be32(pb, index); // rewrite size
  173. url_fseek(pb, curpos, SEEK_SET);
  174. return updateSize (pb, pos);
  175. }
  176. static int mov_write_damr_tag(ByteIOContext *pb)
  177. {
  178. put_be32(pb, 0x11); /* size */
  179. put_tag(pb, "damr");
  180. put_tag(pb, "FFMP");
  181. put_byte(pb, 0);
  182. put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
  183. put_be16(pb, 0xa); /* Mode change period (no restriction) */
  184. //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
  185. //put_be16(pb, 1); /* Mode change period (no restriction) */
  186. return 0x11;
  187. }
  188. static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
  189. {
  190. int pos = url_ftell(pb);
  191. put_be32(pb, 0); /* size */
  192. if(track->enc->codec_id == CODEC_ID_PCM_MULAW)
  193. put_tag(pb, "ulaw");
  194. else if(track->enc->codec_id == CODEC_ID_PCM_ALAW)
  195. put_tag(pb, "alaw");
  196. else if(track->enc->codec_id == CODEC_ID_ADPCM_IMA_QT)
  197. put_tag(pb, "ima4");
  198. else if(track->enc->codec_id == CODEC_ID_MACE3)
  199. put_tag(pb, "MAC3");
  200. else if(track->enc->codec_id == CODEC_ID_MACE6)
  201. put_tag(pb, "MAC6");
  202. else if(track->enc->codec_id == CODEC_ID_AAC)
  203. put_tag(pb, "mp4a");
  204. else if(track->enc->codec_id == CODEC_ID_AMR_NB)
  205. put_tag(pb, "samr");
  206. else
  207. put_tag(pb, " ");
  208. put_be32(pb, 0); /* Reserved */
  209. put_be16(pb, 0); /* Reserved */
  210. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  211. /* SoundDescription */
  212. put_be16(pb, 0); /* Version */
  213. put_be16(pb, 0); /* Revision level */
  214. put_be32(pb, 0); /* Reserved */
  215. put_be16(pb, track->enc->channels); /* Number of channels */
  216. /* TODO: Currently hard-coded to 16-bit, there doesn't seem
  217. to be a good way to get number of bits of audio */
  218. put_be16(pb, 0x10); /* Reserved */
  219. put_be16(pb, 0); /* compression ID (= 0) */
  220. put_be16(pb, 0); /* packet size (= 0) */
  221. put_be16(pb, track->timescale); /* Time scale */
  222. put_be16(pb, 0); /* Reserved */
  223. if(track->enc->codec_id == CODEC_ID_AAC)
  224. mov_write_esds_tag(pb, track);
  225. if(track->enc->codec_id == CODEC_ID_AMR_NB)
  226. mov_write_damr_tag(pb);
  227. return updateSize (pb, pos);
  228. }
  229. static int mov_write_d263_tag(ByteIOContext *pb)
  230. {
  231. put_be32(pb, 0xf); /* size */
  232. put_tag(pb, "d263");
  233. put_tag(pb, "FFMP");
  234. put_be16(pb, 0x0a);
  235. put_byte(pb, 0);
  236. return 0xf;
  237. }
  238. /* TODO: No idea about these values */
  239. static int mov_write_svq3_tag(ByteIOContext *pb)
  240. {
  241. put_be32(pb, 0x15);
  242. put_tag(pb, "SMI ");
  243. put_tag(pb, "SEQH");
  244. put_be32(pb, 0x5);
  245. put_be32(pb, 0xe2c0211d);
  246. put_be32(pb, 0xc0000000);
  247. put_byte(pb, 0);
  248. return 0x15;
  249. }
  250. static unsigned int descrLength(unsigned int len)
  251. {
  252. if (len < 0x00000080)
  253. return 2 + len;
  254. else if (len < 0x00004000)
  255. return 3 + len;
  256. else if(len < 0x00200000)
  257. return 4 + len;
  258. else
  259. return 5 + len;
  260. }
  261. static void putDescr(ByteIOContext *pb, int tag, int size)
  262. {
  263. uint32_t len;
  264. uint8_t vals[4];
  265. len = size;
  266. vals[3] = (uint8_t)(len & 0x7f);
  267. len >>= 7;
  268. vals[2] = (uint8_t)((len & 0x7f) | 0x80);
  269. len >>= 7;
  270. vals[1] = (uint8_t)((len & 0x7f) | 0x80);
  271. len >>= 7;
  272. vals[0] = (uint8_t)((len & 0x7f) | 0x80);
  273. put_byte(pb, tag); // DescriptorTag
  274. if (size < 0x00000080)
  275. {
  276. put_byte(pb, vals[3]);
  277. }
  278. else if (size < 0x00004000)
  279. {
  280. put_byte(pb, vals[2]);
  281. put_byte(pb, vals[3]);
  282. }
  283. else if (size < 0x00200000)
  284. {
  285. put_byte(pb, vals[1]);
  286. put_byte(pb, vals[2]);
  287. put_byte(pb, vals[3]);
  288. }
  289. else if (size < 0x10000000)
  290. {
  291. put_byte(pb, vals[0]);
  292. put_byte(pb, vals[1]);
  293. put_byte(pb, vals[2]);
  294. put_byte(pb, vals[3]);
  295. }
  296. }
  297. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
  298. {
  299. int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  300. int pos = url_ftell(pb);
  301. put_be32(pb, 0); // size
  302. put_tag(pb, "esds");
  303. put_be32(pb, 0); // Version
  304. // ES descriptor
  305. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  306. descrLength(1));
  307. put_be16(pb, 0x0001); // ID (= 1)
  308. put_byte(pb, 0x00); // flags (= no flags)
  309. // DecoderConfig descriptor
  310. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  311. if(track->enc->codec_id == CODEC_ID_AAC)
  312. put_byte(pb, 0x40); // Object type indication
  313. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  314. put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
  315. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  316. put_byte(pb, 0x15); // flags (= Audiostream)
  317. else
  318. put_byte(pb, 0x11); // flags (= Visualstream)
  319. put_byte(pb, 0x0); // Buffersize DB (24 bits)
  320. put_be16(pb, 0x0dd2); // Buffersize DB
  321. // TODO: find real values for these
  322. put_be32(pb, 0x0002e918); // maxbitrate
  323. put_be32(pb, 0x00017e6b); // avg bitrate
  324. if (track->vosLen)
  325. {
  326. // DecoderSpecific info descriptor
  327. putDescr(pb, 0x05, track->vosLen);
  328. put_buffer(pb, track->vosData, track->vosLen);
  329. }
  330. // SL descriptor
  331. putDescr(pb, 0x06, descrLength(1));
  332. put_byte(pb, 0x02);
  333. return updateSize (pb, pos);
  334. }
  335. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
  336. {
  337. int pos = url_ftell(pb);
  338. put_be32(pb, 0); /* size */
  339. if(track->enc->codec_id == CODEC_ID_SVQ1)
  340. put_tag(pb, "SVQ1");
  341. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  342. put_tag(pb, "SVQ3");
  343. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  344. put_tag(pb, "mp4v");
  345. else if(track->enc->codec_id == CODEC_ID_H263)
  346. put_tag(pb, "s263");
  347. else
  348. put_tag(pb, " "); /* Unknown tag */
  349. put_be32(pb, 0); /* Reserved */
  350. put_be16(pb, 0); /* Reserved */
  351. put_be16(pb, 1); /* Data-reference index */
  352. put_be32(pb, 0); /* Reserved (= 02000c) */
  353. put_be32(pb, 0); /* Reserved ("SVis")*/
  354. put_be32(pb, 0); /* Reserved */
  355. put_be32(pb, 0); /* Reserved (400)*/
  356. put_be16(pb, track->enc->width); /* Video width */
  357. put_be16(pb, track->enc->height); /* Video height */
  358. put_be32(pb, 0x00480000); /* Reserved */
  359. put_be32(pb, 0x00480000); /* Reserved */
  360. put_be32(pb, 0); /* Reserved */
  361. put_be32(pb, 0); /* Reserved */
  362. put_be32(pb, 0); /* Reserved */
  363. put_be32(pb, 0); /* Reserved */
  364. put_be32(pb, 0); /* Reserved */
  365. put_be32(pb, 0); /* Reserved */
  366. put_be16(pb, 0); /* Reserved */
  367. put_be32(pb, 0); /* Reserved */
  368. put_be32(pb, 0); /* Reserved */
  369. put_be32(pb, 0); /* Reserved */
  370. put_be16(pb, 0x18); /* Reserved */
  371. put_be16(pb, 0xffff); /* Reserved */
  372. if(track->enc->codec_id == CODEC_ID_MPEG4)
  373. mov_write_esds_tag(pb, track);
  374. else if(track->enc->codec_id == CODEC_ID_H263)
  375. mov_write_d263_tag(pb);
  376. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  377. mov_write_svq3_tag(pb);
  378. return updateSize (pb, pos);
  379. }
  380. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
  381. {
  382. int pos = url_ftell(pb);
  383. put_be32(pb, 0); /* size */
  384. put_tag(pb, "stsd");
  385. put_be32(pb, 0); /* version & flags */
  386. put_be32(pb, 1); /* entry count */
  387. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  388. mov_write_video_tag(pb, track);
  389. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  390. mov_write_audio_tag(pb, track);
  391. return updateSize(pb, pos);
  392. }
  393. /* TODO?: Currently all samples/frames seem to have same duration */
  394. /* Time to sample atom */
  395. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
  396. {
  397. put_be32(pb, 0x18); /* size */
  398. put_tag(pb, "stts");
  399. put_be32(pb, 0); /* version & flags */
  400. put_be32(pb, 1); /* entry count */
  401. put_be32(pb, track->sampleCount); /* sample count */
  402. put_be32(pb, track->sampleDuration); /* sample duration */
  403. return 0x18;
  404. }
  405. static int mov_write_dref_tag(ByteIOContext *pb)
  406. {
  407. put_be32(pb, 28); /* size */
  408. put_tag(pb, "dref");
  409. put_be32(pb, 0); /* version & flags */
  410. put_be32(pb, 1); /* entry count */
  411. put_be32(pb, 0xc); /* size */
  412. put_tag(pb, "url ");
  413. put_be32(pb, 1); /* version & flags */
  414. return 28;
  415. }
  416. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
  417. {
  418. int pos = url_ftell(pb);
  419. put_be32(pb, 0); /* size */
  420. put_tag(pb, "stbl");
  421. mov_write_stsd_tag(pb, track);
  422. mov_write_stts_tag(pb, track);
  423. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  424. track->hasKeyframes)
  425. mov_write_stss_tag(pb, track);
  426. mov_write_stsc_tag(pb, track);
  427. mov_write_stsz_tag(pb, track);
  428. mov_write_stco_tag(pb, track);
  429. return updateSize(pb, pos);
  430. }
  431. static int mov_write_dinf_tag(ByteIOContext *pb)
  432. {
  433. int pos = url_ftell(pb);
  434. put_be32(pb, 0); /* size */
  435. put_tag(pb, "dinf");
  436. mov_write_dref_tag(pb);
  437. return updateSize(pb, pos);
  438. }
  439. static int mov_write_smhd_tag(ByteIOContext *pb)
  440. {
  441. put_be32(pb, 16); /* size */
  442. put_tag(pb, "smhd");
  443. put_be32(pb, 0); /* version & flags */
  444. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  445. put_be16(pb, 0); /* reserved */
  446. return 16;
  447. }
  448. static int mov_write_vmhd_tag(ByteIOContext *pb)
  449. {
  450. put_be32(pb, 0x14); /* size (always 0x14) */
  451. put_tag(pb, "vmhd");
  452. put_be32(pb, 0x01); /* version & flags */
  453. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  454. return 0x14;
  455. }
  456. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
  457. {
  458. int pos = url_ftell(pb);
  459. put_be32(pb, 0); /* size */
  460. put_tag(pb, "minf");
  461. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  462. mov_write_vmhd_tag(pb);
  463. else
  464. mov_write_smhd_tag(pb);
  465. mov_write_dinf_tag(pb);
  466. mov_write_stbl_tag(pb, track);
  467. return updateSize(pb, pos);
  468. }
  469. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
  470. {
  471. char *str;
  472. int pos = url_ftell(pb);
  473. put_be32(pb, 0); /* size */
  474. put_tag(pb, "hdlr");
  475. put_be32(pb, 0); /* Version & flags */
  476. put_be32(pb, 0); /* reserved */
  477. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  478. put_tag(pb, "vide"); /* handler type */
  479. else
  480. put_tag(pb, "soun"); /* handler type */
  481. put_be32(pb ,0); /* reserved */
  482. put_be32(pb ,0); /* reserved */
  483. put_be32(pb ,0); /* reserved */
  484. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  485. str = "VideoHandler";
  486. else
  487. str = "SoundHandler";
  488. put_byte(pb, strlen(str)); /* string counter */
  489. put_buffer(pb, str, strlen(str));
  490. return updateSize(pb, pos);
  491. }
  492. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
  493. {
  494. put_be32(pb, 32); /* size */
  495. put_tag(pb, "mdhd");
  496. put_be32(pb, 0); /* Version & flags */
  497. put_be32(pb, track->time); /* creation time */
  498. put_be32(pb, track->time); /* modification time */
  499. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  500. put_be32(pb, track->trackDuration); /* duration */
  501. put_be16(pb, 0); /* language, 0 = english */
  502. put_be16(pb, 0); /* reserved (quality) */
  503. return 32;
  504. }
  505. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
  506. {
  507. int pos = url_ftell(pb);
  508. put_be32(pb, 0); /* size */
  509. put_tag(pb, "mdia");
  510. mov_write_mdhd_tag(pb, track);
  511. mov_write_hdlr_tag(pb, track);
  512. mov_write_minf_tag(pb, track);
  513. return updateSize(pb, pos);
  514. }
  515. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
  516. {
  517. int64_t maxTrackLenTemp;
  518. put_be32(pb, 0x5c); /* size (always 0x5c) */
  519. put_tag(pb, "tkhd");
  520. put_be32(pb, 0xf); /* version & flags (track enabled) */
  521. put_be32(pb, track->time); /* creation time */
  522. put_be32(pb, track->time); /* modification time */
  523. put_be32(pb, track->trackID); /* track-id */
  524. put_be32(pb, 0); /* reserved */
  525. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
  526. put_be32(pb, (long)maxTrackLenTemp); /* duration */
  527. put_be32(pb, 0); /* reserved */
  528. put_be32(pb, 0); /* reserved */
  529. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  530. /* Volume, only for audio */
  531. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  532. put_be16(pb, 0x0100);
  533. else
  534. put_be16(pb, 0);
  535. put_be16(pb, 0); /* reserved */
  536. /* Matrix structure */
  537. put_be32(pb, 0x00010000); /* reserved */
  538. put_be32(pb, 0x0); /* reserved */
  539. put_be32(pb, 0x0); /* reserved */
  540. put_be32(pb, 0x0); /* reserved */
  541. put_be32(pb, 0x00010000); /* reserved */
  542. put_be32(pb, 0x0); /* reserved */
  543. put_be32(pb, 0x0); /* reserved */
  544. put_be32(pb, 0x0); /* reserved */
  545. put_be32(pb, 0x40000000); /* reserved */
  546. /* Track width and height, for visual only */
  547. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  548. put_be32(pb, track->enc->width*0x10000);
  549. put_be32(pb, track->enc->height*0x10000);
  550. }
  551. else {
  552. put_be32(pb, 0);
  553. put_be32(pb, 0);
  554. }
  555. return 0x5c;
  556. }
  557. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
  558. {
  559. int pos = url_ftell(pb);
  560. put_be32(pb, 0); /* size */
  561. put_tag(pb, "trak");
  562. mov_write_tkhd_tag(pb, track);
  563. mov_write_mdia_tag(pb, track);
  564. return updateSize(pb, pos);
  565. }
  566. /* TODO: Not sorted out, but not necessary either */
  567. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  568. {
  569. put_be32(pb, 0x15); /* size */
  570. put_tag(pb, "iods");
  571. put_be32(pb, 0); /* version & flags */
  572. put_be16(pb, 0x1007);
  573. put_byte(pb, 0);
  574. put_be16(pb, 0x4fff);
  575. put_be16(pb, 0xfffe);
  576. put_be16(pb, 0x01ff);
  577. return 0x15;
  578. }
  579. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  580. {
  581. int maxTrackID = 1, maxTrackLen = 0, i;
  582. int64_t maxTrackLenTemp;
  583. put_be32(pb, 0x6c); /* size (always 0x6c) */
  584. put_tag(pb, "mvhd");
  585. put_be32(pb, 0); /* version & flags */
  586. put_be32(pb, mov->time); /* creation time */
  587. put_be32(pb, mov->time); /* modification time */
  588. put_be32(pb, mov->timescale); /* timescale */
  589. for (i=0; i<MAX_STREAMS; i++) {
  590. if(mov->tracks[i].entry > 0) {
  591. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
  592. if(maxTrackLen < maxTrackLenTemp)
  593. maxTrackLen = maxTrackLenTemp;
  594. if(maxTrackID < mov->tracks[i].trackID)
  595. maxTrackID = mov->tracks[i].trackID;
  596. }
  597. }
  598. put_be32(pb, maxTrackLen); /* duration of longest track */
  599. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  600. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  601. put_be16(pb, 0); /* reserved */
  602. put_be32(pb, 0); /* reserved */
  603. put_be32(pb, 0); /* reserved */
  604. /* Matrix structure */
  605. put_be32(pb, 0x00010000); /* reserved */
  606. put_be32(pb, 0x0); /* reserved */
  607. put_be32(pb, 0x0); /* reserved */
  608. put_be32(pb, 0x0); /* reserved */
  609. put_be32(pb, 0x00010000); /* reserved */
  610. put_be32(pb, 0x0); /* reserved */
  611. put_be32(pb, 0x0); /* reserved */
  612. put_be32(pb, 0x0); /* reserved */
  613. put_be32(pb, 0x40000000); /* reserved */
  614. put_be32(pb, 0); /* reserved (preview time) */
  615. put_be32(pb, 0); /* reserved (preview duration) */
  616. put_be32(pb, 0); /* reserved (poster time) */
  617. put_be32(pb, 0); /* reserved (selection time) */
  618. put_be32(pb, 0); /* reserved (selection duration) */
  619. put_be32(pb, 0); /* reserved (current time) */
  620. put_be32(pb, maxTrackID+1); /* Next track id */
  621. return 0x6c;
  622. }
  623. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov)
  624. {
  625. int pos, i;
  626. pos = url_ftell(pb);
  627. put_be32(pb, 0); /* size placeholder*/
  628. put_tag(pb, "moov");
  629. mov->timescale = globalTimescale;
  630. for (i=0; i<MAX_STREAMS; i++) {
  631. if(mov->tracks[i].entry <= 0) continue;
  632. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  633. mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
  634. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
  635. }
  636. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  637. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  638. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  639. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  640. mov->tracks[i].timescale = 8000;
  641. }
  642. else {
  643. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  644. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  645. }
  646. }
  647. mov->tracks[i].trackDuration =
  648. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  649. mov->tracks[i].time = mov->time;
  650. mov->tracks[i].trackID = i+1;
  651. }
  652. mov_write_mvhd_tag(pb, mov);
  653. //mov_write_iods_tag(pb, mov);
  654. for (i=0; i<MAX_STREAMS; i++) {
  655. if(mov->tracks[i].entry > 0) {
  656. mov_write_trak_tag(pb, &(mov->tracks[i]));
  657. }
  658. }
  659. return updateSize(pb, pos);
  660. }
  661. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  662. {
  663. mov->mdat_pos = url_ftell(pb);
  664. put_be32(pb, 0); /* size placeholder*/
  665. put_tag(pb, "mdat");
  666. return 0;
  667. }
  668. /* TODO: This needs to be more general */
  669. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  670. {
  671. put_be32(pb, 0x14 ); /* size */
  672. put_tag(pb, "ftyp");
  673. if (!strcmp("3gp", s->oformat->name))
  674. put_tag(pb, "3gp4");
  675. else
  676. put_tag(pb, "isom");
  677. put_be32(pb, 0x200 );
  678. if (!strcmp("3gp", s->oformat->name))
  679. put_tag(pb, "3gp4");
  680. else
  681. put_tag(pb, "mp41");
  682. return 0x14;
  683. }
  684. static int mov_write_header(AVFormatContext *s)
  685. {
  686. ByteIOContext *pb = &s->pb;
  687. if(s->oformat != NULL) {
  688. if(!strcmp("3gp", s->oformat->name) || !strcmp("mp4", s->oformat->name))
  689. mov_write_ftyp_tag(pb,s);
  690. }
  691. put_flush_packet(pb);
  692. return 0;
  693. }
  694. static int Timestamp() {
  695. time_t ltime;
  696. time ( &ltime );
  697. return ltime+(24107*86400);
  698. }
  699. static int mov_write_packet(AVFormatContext *s, int stream_index,
  700. const uint8_t *buf, int size, int64_t pts)
  701. {
  702. MOVContext *mov = s->priv_data;
  703. ByteIOContext *pb = &s->pb;
  704. AVCodecContext *enc = &s->streams[stream_index]->codec;
  705. MOVTrack* trk = &mov->tracks[stream_index];
  706. int cl, id;
  707. unsigned int samplesInChunk = 0;
  708. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  709. if (!size) return 0; /* Discard 0 sized packets */
  710. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  711. samplesInChunk = 1;
  712. }
  713. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  714. if( enc->codec_id == CODEC_ID_AMR_NB) {
  715. /* We must find out how many AMR blocks there are in one packet */
  716. static uint16_t packed_size[16] =
  717. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  718. int len = 0;
  719. while (len < size && samplesInChunk < 100) {
  720. len += packed_size[(buf[len] >> 3) & 0x0F];
  721. samplesInChunk++;
  722. }
  723. }
  724. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  725. samplesInChunk = size/enc->channels;
  726. }
  727. else {
  728. samplesInChunk = 1;
  729. }
  730. }
  731. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  732. && trk->vosLen == 0) {
  733. assert(enc->extradata_size);
  734. trk->vosLen = enc->extradata_size;
  735. trk->vosData = av_malloc(trk->vosLen);
  736. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  737. }
  738. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  739. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  740. if (trk->ents_allocated <= trk->entry) {
  741. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  742. if (!trk->cluster)
  743. return -1;
  744. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  745. if (!trk->cluster[cl])
  746. return -1;
  747. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  748. }
  749. if (mov->mdat_written == 0) {
  750. mov_write_mdat_tag(pb, mov);
  751. mov->mdat_written = 1;
  752. mov->time = Timestamp();
  753. }
  754. trk->cluster[cl][id].pos = url_ftell(pb) - mov->movi_list;
  755. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  756. trk->cluster[cl][id].size = size;
  757. trk->cluster[cl][id].entries = samplesInChunk;
  758. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  759. trk->cluster[cl][id].key_frame = enc->coded_frame->key_frame;
  760. if(enc->coded_frame->pict_type == FF_I_TYPE)
  761. trk->hasKeyframes = 1;
  762. }
  763. trk->enc = enc;
  764. trk->entry++;
  765. trk->sampleCount += samplesInChunk;
  766. trk->mdat_size += size;
  767. put_buffer(pb, buf, size);
  768. put_flush_packet(pb);
  769. return 0;
  770. }
  771. static int mov_write_trailer(AVFormatContext *s)
  772. {
  773. MOVContext *mov = s->priv_data;
  774. ByteIOContext *pb = &s->pb;
  775. int res = 0;
  776. int i, j;
  777. offset_t file_size;
  778. file_size = url_ftell(pb);
  779. j = 0;
  780. /* Write size of mdat tag */
  781. for (i=0; i<MAX_STREAMS; i++) {
  782. if(mov->tracks[i].ents_allocated > 0) {
  783. j += mov->tracks[i].mdat_size;
  784. }
  785. }
  786. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  787. put_be32(pb, j+8);
  788. url_fseek(pb, file_size, SEEK_SET);
  789. mov_write_moov_tag(pb, mov);
  790. for (i=0; i<MAX_STREAMS; i++) {
  791. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  792. av_free(mov->tracks[i].cluster[j]);
  793. }
  794. av_free(mov->tracks[i].cluster);
  795. mov->tracks[i].cluster = NULL;
  796. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  797. }
  798. put_flush_packet(pb);
  799. return res;
  800. }
  801. static AVOutputFormat mov_oformat = {
  802. "mov",
  803. "mov format",
  804. NULL,
  805. "mov",
  806. sizeof(MOVContext),
  807. CODEC_ID_PCM_ALAW,
  808. CODEC_ID_MPEG4,
  809. mov_write_header,
  810. mov_write_packet,
  811. mov_write_trailer,
  812. };
  813. static AVOutputFormat _3gp_oformat = {
  814. "3gp",
  815. "3gp format",
  816. NULL,
  817. "3gp",
  818. sizeof(MOVContext),
  819. CODEC_ID_AMR_NB,
  820. CODEC_ID_H263,
  821. mov_write_header,
  822. mov_write_packet,
  823. mov_write_trailer,
  824. };
  825. static AVOutputFormat mp4_oformat = {
  826. "mp4",
  827. "mp4 format",
  828. "application/mp4",
  829. "mp4,m4a",
  830. sizeof(MOVContext),
  831. CODEC_ID_AAC,
  832. CODEC_ID_MPEG4,
  833. mov_write_header,
  834. mov_write_packet,
  835. mov_write_trailer,
  836. };
  837. int movenc_init(void)
  838. {
  839. av_register_output_format(&mov_oformat);
  840. av_register_output_format(&_3gp_oformat);
  841. av_register_output_format(&mp4_oformat);
  842. return 0;
  843. }