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.

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