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.

1146 lines
33KB

  1. /*
  2. * MOV, 3GP, MP4 encoder.
  3. * Copyright (c) 2003 Thomas Raivio.
  4. * Copyright (c) 2004 Gildas Bazin <gbazin at videolan dot org>.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "avformat.h"
  21. #include "avi.h"
  22. #include "avio.h"
  23. #undef NDEBUG
  24. #include <assert.h>
  25. #define MOV_INDEX_CLUSTER_SIZE 16384
  26. #define globalTimescale 1000
  27. #define MODE_MP4 0
  28. #define MODE_MOV 1
  29. #define MODE_3GP 2
  30. typedef struct MOVIentry {
  31. unsigned int flags, pos, size;
  32. unsigned int samplesInChunk;
  33. char key_frame;
  34. unsigned int entries;
  35. } MOVIentry;
  36. typedef struct MOVIndex {
  37. int mode;
  38. int entry;
  39. int mdat_size;
  40. int ents_allocated;
  41. long timescale;
  42. long time;
  43. long trackDuration;
  44. long sampleCount;
  45. long sampleDuration;
  46. int hasKeyframes;
  47. int trackID;
  48. AVCodecContext *enc;
  49. int vosLen;
  50. uint8_t *vosData;
  51. MOVIentry** cluster;
  52. } MOVTrack;
  53. typedef struct {
  54. int mode;
  55. long time;
  56. int nb_streams;
  57. int mdat_written;
  58. offset_t mdat_pos;
  59. long timescale;
  60. MOVTrack tracks[MAX_STREAMS];
  61. } MOVContext;
  62. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
  63. //FIXME supprt 64bit varaint with wide placeholders
  64. static int updateSize (ByteIOContext *pb, int pos)
  65. {
  66. long curpos = url_ftell(pb);
  67. url_fseek(pb, pos, SEEK_SET);
  68. put_be32(pb, curpos - pos); /* rewrite size */
  69. url_fseek(pb, curpos, SEEK_SET);
  70. return curpos - pos;
  71. }
  72. /* Chunk offset atom */
  73. static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
  74. {
  75. int i;
  76. int pos = url_ftell(pb);
  77. put_be32(pb, 0); /* size */
  78. put_tag(pb, "stco");
  79. put_be32(pb, 0); /* version & flags */
  80. put_be32(pb, track->entry); /* entry count */
  81. for (i=0; i<track->entry; i++) {
  82. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  83. int id = i % MOV_INDEX_CLUSTER_SIZE;
  84. put_be32(pb, track->cluster[cl][id].pos);
  85. }
  86. return updateSize (pb, pos);
  87. }
  88. /* Sample size atom */
  89. static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
  90. {
  91. int equalChunks = 1;
  92. int i, j, entries = 0, tst = -1, oldtst = -1;
  93. int pos = url_ftell(pb);
  94. put_be32(pb, 0); /* size */
  95. put_tag(pb, "stsz");
  96. put_be32(pb, 0); /* version & flags */
  97. for (i=0; i<track->entry; i++) {
  98. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  99. int id = i % MOV_INDEX_CLUSTER_SIZE;
  100. tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
  101. if(oldtst != -1 && tst != oldtst) {
  102. equalChunks = 0;
  103. }
  104. oldtst = tst;
  105. entries += track->cluster[cl][id].entries;
  106. }
  107. if (equalChunks) {
  108. int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
  109. put_be32(pb, sSize); // sample size
  110. put_be32(pb, entries); // sample count
  111. }
  112. else {
  113. put_be32(pb, 0); // sample size
  114. put_be32(pb, entries); // sample count
  115. for (i=0; i<track->entry; i++) {
  116. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  117. int id = i % MOV_INDEX_CLUSTER_SIZE;
  118. for ( j=0; j<track->cluster[cl][id].entries; j++) {
  119. put_be32(pb, track->cluster[cl][id].size /
  120. track->cluster[cl][id].entries);
  121. }
  122. }
  123. }
  124. return updateSize (pb, pos);
  125. }
  126. /* Sample to chunk atom */
  127. static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
  128. {
  129. int index = 0, oldval = -1, i, entryPos, curpos;
  130. int pos = url_ftell(pb);
  131. put_be32(pb, 0); /* size */
  132. put_tag(pb, "stsc");
  133. put_be32(pb, 0); // version & flags
  134. entryPos = url_ftell(pb);
  135. put_be32(pb, track->entry); // entry count
  136. for (i=0; i<track->entry; i++) {
  137. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  138. int id = i % MOV_INDEX_CLUSTER_SIZE;
  139. if(oldval != track->cluster[cl][id].samplesInChunk)
  140. {
  141. put_be32(pb, i+1); // first chunk
  142. put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
  143. put_be32(pb, 0x1); // sample description index
  144. oldval = track->cluster[cl][id].samplesInChunk;
  145. index++;
  146. }
  147. }
  148. curpos = url_ftell(pb);
  149. url_fseek(pb, entryPos, SEEK_SET);
  150. put_be32(pb, index); // rewrite size
  151. url_fseek(pb, curpos, SEEK_SET);
  152. return updateSize (pb, pos);
  153. }
  154. /* Sync sample atom */
  155. static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
  156. {
  157. long curpos;
  158. int i, index = 0, entryPos;
  159. int pos = url_ftell(pb);
  160. put_be32(pb, 0); // size
  161. put_tag(pb, "stss");
  162. put_be32(pb, 0); // version & flags
  163. entryPos = url_ftell(pb);
  164. put_be32(pb, track->entry); // entry count
  165. for (i=0; i<track->entry; i++) {
  166. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  167. int id = i % MOV_INDEX_CLUSTER_SIZE;
  168. if(track->cluster[cl][id].key_frame == 1) {
  169. put_be32(pb, i+1);
  170. index++;
  171. }
  172. }
  173. curpos = url_ftell(pb);
  174. url_fseek(pb, entryPos, SEEK_SET);
  175. put_be32(pb, index); // rewrite size
  176. url_fseek(pb, curpos, SEEK_SET);
  177. return updateSize (pb, pos);
  178. }
  179. static int mov_write_damr_tag(ByteIOContext *pb)
  180. {
  181. put_be32(pb, 0x11); /* size */
  182. put_tag(pb, "damr");
  183. put_tag(pb, "FFMP");
  184. put_byte(pb, 0);
  185. put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
  186. put_be16(pb, 0xa); /* Mode change period (no restriction) */
  187. //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
  188. //put_be16(pb, 1); /* Mode change period (no restriction) */
  189. return 0x11;
  190. }
  191. static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
  192. {
  193. int pos = url_ftell(pb);
  194. put_be32(pb, 0); /* size */
  195. put_tag(pb, "wave");
  196. put_be32(pb, 12); /* size */
  197. put_tag(pb, "frma");
  198. put_tag(pb, "mp4a");
  199. put_be32(pb, 12); /* size */
  200. put_tag(pb, "mp4a");
  201. put_be32(pb, 0);
  202. mov_write_esds_tag(pb, track);
  203. put_be32(pb, 12); /* size */
  204. put_tag(pb, "srcq");
  205. put_be32(pb, 0x40);
  206. put_be32(pb, 8); /* size */
  207. put_be32(pb, 0); /* null tag */
  208. return updateSize (pb, pos);
  209. }
  210. const CodecTag codec_movaudio_tags[] = {
  211. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
  212. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
  213. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
  214. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
  215. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
  216. { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
  217. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
  218. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
  219. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
  220. { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
  221. { 0, 0 },
  222. };
  223. static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
  224. {
  225. int pos = url_ftell(pb);
  226. int tag;
  227. put_be32(pb, 0); /* size */
  228. tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
  229. // if no mac fcc found, try with Microsoft tags
  230. if (!tag)
  231. {
  232. int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
  233. tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
  234. }
  235. put_le32(pb, tag); // store it byteswapped
  236. put_be32(pb, 0); /* Reserved */
  237. put_be16(pb, 0); /* Reserved */
  238. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  239. /* SoundDescription */
  240. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  241. put_be16(pb, 1); /* Version 1 */
  242. else
  243. put_be16(pb, 0); /* Version 0 */
  244. put_be16(pb, 0); /* Revision level */
  245. put_be32(pb, 0); /* Reserved */
  246. put_be16(pb, track->enc->channels); /* Number of channels */
  247. /* TODO: Currently hard-coded to 16-bit, there doesn't seem
  248. to be a good way to get number of bits of audio */
  249. put_be16(pb, 0x10); /* Reserved */
  250. if(track->enc->codec_id == CODEC_ID_AAC ||
  251. track->enc->codec_id == CODEC_ID_MP3)
  252. {
  253. put_be16(pb, 0xfffe); /* compression ID (vbr)*/
  254. }
  255. else
  256. {
  257. put_be16(pb, 0); /* compression ID (= 0) */
  258. }
  259. put_be16(pb, 0); /* packet size (= 0) */
  260. put_be16(pb, track->timescale); /* Time scale */
  261. put_be16(pb, 0); /* Reserved */
  262. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  263. {
  264. /* SoundDescription V1 extended info */
  265. put_be32(pb, track->enc->frame_size); /* Samples per packet */
  266. put_be32(pb, 1536); /* Bytes per packet */
  267. put_be32(pb, 2); /* Bytes per frame */
  268. put_be32(pb, 2); /* Bytes per sample */
  269. }
  270. if(track->enc->codec_id == CODEC_ID_AAC) {
  271. if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
  272. else mov_write_esds_tag(pb, track);
  273. }
  274. if(track->enc->codec_id == CODEC_ID_AMR_NB)
  275. mov_write_damr_tag(pb);
  276. return updateSize (pb, pos);
  277. }
  278. static int mov_write_d263_tag(ByteIOContext *pb)
  279. {
  280. put_be32(pb, 0xf); /* size */
  281. put_tag(pb, "d263");
  282. put_tag(pb, "FFMP");
  283. put_be16(pb, 0x0a);
  284. put_byte(pb, 0);
  285. return 0xf;
  286. }
  287. /* TODO: No idea about these values */
  288. static int mov_write_svq3_tag(ByteIOContext *pb)
  289. {
  290. put_be32(pb, 0x15);
  291. put_tag(pb, "SMI ");
  292. put_tag(pb, "SEQH");
  293. put_be32(pb, 0x5);
  294. put_be32(pb, 0xe2c0211d);
  295. put_be32(pb, 0xc0000000);
  296. put_byte(pb, 0);
  297. return 0x15;
  298. }
  299. static unsigned int descrLength(unsigned int len)
  300. {
  301. if (len < 0x00000080)
  302. return 2 + len;
  303. else if (len < 0x00004000)
  304. return 3 + len;
  305. else if(len < 0x00200000)
  306. return 4 + len;
  307. else
  308. return 5 + len;
  309. }
  310. static void putDescr(ByteIOContext *pb, int tag, int size)
  311. {
  312. uint32_t len;
  313. uint8_t vals[4];
  314. len = size;
  315. vals[3] = (uint8_t)(len & 0x7f);
  316. len >>= 7;
  317. vals[2] = (uint8_t)((len & 0x7f) | 0x80);
  318. len >>= 7;
  319. vals[1] = (uint8_t)((len & 0x7f) | 0x80);
  320. len >>= 7;
  321. vals[0] = (uint8_t)((len & 0x7f) | 0x80);
  322. put_byte(pb, tag); // DescriptorTag
  323. if (size < 0x00000080)
  324. {
  325. put_byte(pb, vals[3]);
  326. }
  327. else if (size < 0x00004000)
  328. {
  329. put_byte(pb, vals[2]);
  330. put_byte(pb, vals[3]);
  331. }
  332. else if (size < 0x00200000)
  333. {
  334. put_byte(pb, vals[1]);
  335. put_byte(pb, vals[2]);
  336. put_byte(pb, vals[3]);
  337. }
  338. else if (size < 0x10000000)
  339. {
  340. put_byte(pb, vals[0]);
  341. put_byte(pb, vals[1]);
  342. put_byte(pb, vals[2]);
  343. put_byte(pb, vals[3]);
  344. }
  345. }
  346. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
  347. {
  348. int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  349. int pos = url_ftell(pb);
  350. put_be32(pb, 0); // size
  351. put_tag(pb, "esds");
  352. put_be32(pb, 0); // Version
  353. // ES descriptor
  354. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  355. descrLength(1));
  356. put_be16(pb, 0x0001); // ID (= 1)
  357. put_byte(pb, 0x00); // flags (= no flags)
  358. // DecoderConfig descriptor
  359. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  360. if(track->enc->codec_id == CODEC_ID_AAC)
  361. put_byte(pb, 0x40); // Object type indication
  362. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  363. put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
  364. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  365. put_byte(pb, 0x15); // flags (= Audiostream)
  366. else
  367. put_byte(pb, 0x11); // flags (= Visualstream)
  368. put_byte(pb, 0x0); // Buffersize DB (24 bits)
  369. put_be16(pb, 0x0dd2); // Buffersize DB
  370. // TODO: find real values for these
  371. put_be32(pb, 0x0002e918); // maxbitrate
  372. put_be32(pb, 0x00017e6b); // avg bitrate
  373. if (track->vosLen)
  374. {
  375. // DecoderSpecific info descriptor
  376. putDescr(pb, 0x05, track->vosLen);
  377. put_buffer(pb, track->vosData, track->vosLen);
  378. }
  379. // SL descriptor
  380. putDescr(pb, 0x06, 1);
  381. put_byte(pb, 0x02);
  382. return updateSize (pb, pos);
  383. }
  384. const CodecTag codec_movvideo_tags[] = {
  385. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
  386. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
  387. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  388. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
  389. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
  390. { 0, 0 },
  391. };
  392. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
  393. {
  394. int pos = url_ftell(pb);
  395. int tag;
  396. put_be32(pb, 0); /* size */
  397. tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
  398. // if no mac fcc found, try with Microsoft tags
  399. if (!tag)
  400. tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
  401. put_le32(pb, tag); // store it byteswapped
  402. put_be32(pb, 0); /* Reserved */
  403. put_be16(pb, 0); /* Reserved */
  404. put_be16(pb, 1); /* Data-reference index */
  405. put_be32(pb, 0); /* Reserved (= 02000c) */
  406. put_be32(pb, 0); /* Reserved ("SVis")*/
  407. put_be32(pb, 0); /* Reserved */
  408. put_be32(pb, 0); /* Reserved (400)*/
  409. put_be16(pb, track->enc->width); /* Video width */
  410. put_be16(pb, track->enc->height); /* Video height */
  411. put_be32(pb, 0x00480000); /* Reserved */
  412. put_be32(pb, 0x00480000); /* Reserved */
  413. put_be32(pb, 0); /* Data size (= 0) */
  414. put_be16(pb, 1); /* Frame count (= 1) */
  415. put_be32(pb, 0); /* Reserved */
  416. put_be32(pb, 0); /* Reserved */
  417. put_be32(pb, 0); /* Reserved */
  418. put_be32(pb, 0); /* Reserved */
  419. put_be32(pb, 0); /* Reserved */
  420. put_be32(pb, 0); /* Reserved */
  421. put_be32(pb, 0); /* Reserved */
  422. put_be32(pb, 0); /* Reserved */
  423. put_be16(pb, 0x18); /* Reserved */
  424. put_be16(pb, 0xffff); /* Reserved */
  425. if(track->enc->codec_id == CODEC_ID_MPEG4)
  426. mov_write_esds_tag(pb, track);
  427. else if(track->enc->codec_id == CODEC_ID_H263)
  428. mov_write_d263_tag(pb);
  429. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  430. mov_write_svq3_tag(pb);
  431. return updateSize (pb, pos);
  432. }
  433. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
  434. {
  435. int pos = url_ftell(pb);
  436. put_be32(pb, 0); /* size */
  437. put_tag(pb, "stsd");
  438. put_be32(pb, 0); /* version & flags */
  439. put_be32(pb, 1); /* entry count */
  440. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  441. mov_write_video_tag(pb, track);
  442. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  443. mov_write_audio_tag(pb, track);
  444. return updateSize(pb, pos);
  445. }
  446. /* TODO?: Currently all samples/frames seem to have same duration */
  447. /* Time to sample atom */
  448. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
  449. {
  450. put_be32(pb, 0x18); /* size */
  451. put_tag(pb, "stts");
  452. put_be32(pb, 0); /* version & flags */
  453. put_be32(pb, 1); /* entry count */
  454. put_be32(pb, track->sampleCount); /* sample count */
  455. put_be32(pb, track->sampleDuration); /* sample duration */
  456. return 0x18;
  457. }
  458. static int mov_write_dref_tag(ByteIOContext *pb)
  459. {
  460. put_be32(pb, 28); /* size */
  461. put_tag(pb, "dref");
  462. put_be32(pb, 0); /* version & flags */
  463. put_be32(pb, 1); /* entry count */
  464. put_be32(pb, 0xc); /* size */
  465. put_tag(pb, "url ");
  466. put_be32(pb, 1); /* version & flags */
  467. return 28;
  468. }
  469. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
  470. {
  471. int pos = url_ftell(pb);
  472. put_be32(pb, 0); /* size */
  473. put_tag(pb, "stbl");
  474. mov_write_stsd_tag(pb, track);
  475. mov_write_stts_tag(pb, track);
  476. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  477. track->hasKeyframes)
  478. mov_write_stss_tag(pb, track);
  479. mov_write_stsc_tag(pb, track);
  480. mov_write_stsz_tag(pb, track);
  481. mov_write_stco_tag(pb, track);
  482. return updateSize(pb, pos);
  483. }
  484. static int mov_write_dinf_tag(ByteIOContext *pb)
  485. {
  486. int pos = url_ftell(pb);
  487. put_be32(pb, 0); /* size */
  488. put_tag(pb, "dinf");
  489. mov_write_dref_tag(pb);
  490. return updateSize(pb, pos);
  491. }
  492. static int mov_write_smhd_tag(ByteIOContext *pb)
  493. {
  494. put_be32(pb, 16); /* size */
  495. put_tag(pb, "smhd");
  496. put_be32(pb, 0); /* version & flags */
  497. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  498. put_be16(pb, 0); /* reserved */
  499. return 16;
  500. }
  501. static int mov_write_vmhd_tag(ByteIOContext *pb)
  502. {
  503. put_be32(pb, 0x14); /* size (always 0x14) */
  504. put_tag(pb, "vmhd");
  505. put_be32(pb, 0x01); /* version & flags */
  506. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  507. return 0x14;
  508. }
  509. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
  510. {
  511. char *descr, *hdlr, *hdlr_type;
  512. int pos = url_ftell(pb);
  513. if (!track) { /* no media --> data handler */
  514. hdlr = "dhlr";
  515. hdlr_type = "url ";
  516. descr = "DataHandler";
  517. } else {
  518. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  519. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  520. hdlr_type = "vide";
  521. descr = "VideoHandler";
  522. } else {
  523. hdlr_type = "soun";
  524. descr = "SoundHandler";
  525. }
  526. }
  527. put_be32(pb, 0); /* size */
  528. put_tag(pb, "hdlr");
  529. put_be32(pb, 0); /* Version & flags */
  530. put_buffer(pb, hdlr, 4); /* handler */
  531. put_tag(pb, hdlr_type); /* handler type */
  532. put_be32(pb ,0); /* reserved */
  533. put_be32(pb ,0); /* reserved */
  534. put_be32(pb ,0); /* reserved */
  535. put_byte(pb, strlen(descr)); /* string counter */
  536. put_buffer(pb, descr, strlen(descr)); /* handler description */
  537. return updateSize(pb, pos);
  538. }
  539. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
  540. {
  541. int pos = url_ftell(pb);
  542. put_be32(pb, 0); /* size */
  543. put_tag(pb, "minf");
  544. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  545. mov_write_vmhd_tag(pb);
  546. else
  547. mov_write_smhd_tag(pb);
  548. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  549. mov_write_hdlr_tag(pb, NULL);
  550. mov_write_dinf_tag(pb);
  551. mov_write_stbl_tag(pb, track);
  552. return updateSize(pb, pos);
  553. }
  554. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
  555. {
  556. put_be32(pb, 32); /* size */
  557. put_tag(pb, "mdhd");
  558. put_be32(pb, 0); /* Version & flags */
  559. put_be32(pb, track->time); /* creation time */
  560. put_be32(pb, track->time); /* modification time */
  561. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  562. put_be32(pb, track->trackDuration); /* duration */
  563. put_be16(pb, 0); /* language, 0 = english */
  564. put_be16(pb, 0); /* reserved (quality) */
  565. return 32;
  566. }
  567. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
  568. {
  569. int pos = url_ftell(pb);
  570. put_be32(pb, 0); /* size */
  571. put_tag(pb, "mdia");
  572. mov_write_mdhd_tag(pb, track);
  573. mov_write_hdlr_tag(pb, track);
  574. mov_write_minf_tag(pb, track);
  575. return updateSize(pb, pos);
  576. }
  577. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
  578. {
  579. int64_t maxTrackLenTemp;
  580. put_be32(pb, 0x5c); /* size (always 0x5c) */
  581. put_tag(pb, "tkhd");
  582. put_be32(pb, 0xf); /* version & flags (track enabled) */
  583. put_be32(pb, track->time); /* creation time */
  584. put_be32(pb, track->time); /* modification time */
  585. put_be32(pb, track->trackID); /* track-id */
  586. put_be32(pb, 0); /* reserved */
  587. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
  588. put_be32(pb, (long)maxTrackLenTemp); /* duration */
  589. put_be32(pb, 0); /* reserved */
  590. put_be32(pb, 0); /* reserved */
  591. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  592. /* Volume, only for audio */
  593. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  594. put_be16(pb, 0x0100);
  595. else
  596. put_be16(pb, 0);
  597. put_be16(pb, 0); /* reserved */
  598. /* Matrix structure */
  599. put_be32(pb, 0x00010000); /* reserved */
  600. put_be32(pb, 0x0); /* reserved */
  601. put_be32(pb, 0x0); /* reserved */
  602. put_be32(pb, 0x0); /* reserved */
  603. put_be32(pb, 0x00010000); /* reserved */
  604. put_be32(pb, 0x0); /* reserved */
  605. put_be32(pb, 0x0); /* reserved */
  606. put_be32(pb, 0x0); /* reserved */
  607. put_be32(pb, 0x40000000); /* reserved */
  608. /* Track width and height, for visual only */
  609. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  610. double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
  611. if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
  612. put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  613. put_be32(pb, track->enc->height*0x10000);
  614. }
  615. else {
  616. put_be32(pb, 0);
  617. put_be32(pb, 0);
  618. }
  619. return 0x5c;
  620. }
  621. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
  622. {
  623. int pos = url_ftell(pb);
  624. put_be32(pb, 0); /* size */
  625. put_tag(pb, "trak");
  626. mov_write_tkhd_tag(pb, track);
  627. mov_write_mdia_tag(pb, track);
  628. return updateSize(pb, pos);
  629. }
  630. /* TODO: Not sorted out, but not necessary either */
  631. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  632. {
  633. put_be32(pb, 0x15); /* size */
  634. put_tag(pb, "iods");
  635. put_be32(pb, 0); /* version & flags */
  636. put_be16(pb, 0x1007);
  637. put_byte(pb, 0);
  638. put_be16(pb, 0x4fff);
  639. put_be16(pb, 0xfffe);
  640. put_be16(pb, 0x01ff);
  641. return 0x15;
  642. }
  643. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  644. {
  645. int maxTrackID = 1, maxTrackLen = 0, i;
  646. int64_t maxTrackLenTemp;
  647. put_be32(pb, 0x6c); /* size (always 0x6c) */
  648. put_tag(pb, "mvhd");
  649. put_be32(pb, 0); /* version & flags */
  650. put_be32(pb, mov->time); /* creation time */
  651. put_be32(pb, mov->time); /* modification time */
  652. put_be32(pb, mov->timescale); /* timescale */
  653. for (i=0; i<MAX_STREAMS; i++) {
  654. if(mov->tracks[i].entry > 0) {
  655. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
  656. if(maxTrackLen < maxTrackLenTemp)
  657. maxTrackLen = maxTrackLenTemp;
  658. if(maxTrackID < mov->tracks[i].trackID)
  659. maxTrackID = mov->tracks[i].trackID;
  660. }
  661. }
  662. put_be32(pb, maxTrackLen); /* duration of longest track */
  663. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  664. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  665. put_be16(pb, 0); /* reserved */
  666. put_be32(pb, 0); /* reserved */
  667. put_be32(pb, 0); /* reserved */
  668. /* Matrix structure */
  669. put_be32(pb, 0x00010000); /* reserved */
  670. put_be32(pb, 0x0); /* reserved */
  671. put_be32(pb, 0x0); /* reserved */
  672. put_be32(pb, 0x0); /* reserved */
  673. put_be32(pb, 0x00010000); /* reserved */
  674. put_be32(pb, 0x0); /* reserved */
  675. put_be32(pb, 0x0); /* reserved */
  676. put_be32(pb, 0x0); /* reserved */
  677. put_be32(pb, 0x40000000); /* reserved */
  678. put_be32(pb, 0); /* reserved (preview time) */
  679. put_be32(pb, 0); /* reserved (preview duration) */
  680. put_be32(pb, 0); /* reserved (poster time) */
  681. put_be32(pb, 0); /* reserved (selection time) */
  682. put_be32(pb, 0); /* reserved (selection duration) */
  683. put_be32(pb, 0); /* reserved (current time) */
  684. put_be32(pb, maxTrackID+1); /* Next track id */
  685. return 0x6c;
  686. }
  687. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
  688. AVFormatContext *s)
  689. {
  690. int pos = url_ftell(pb);
  691. int i;
  692. put_be32(pb, 0); /* size */
  693. put_tag(pb, "udta");
  694. /* Requirements */
  695. for (i=0; i<MAX_STREAMS; i++) {
  696. if(mov->tracks[i].entry <= 0) continue;
  697. if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
  698. mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
  699. int pos = url_ftell(pb);
  700. put_be32(pb, 0); /* size */
  701. put_tag(pb, "\251req");
  702. put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
  703. put_be16(pb, 0);
  704. put_buffer(pb, "QuickTime 6.0 or greater",
  705. sizeof("QuickTime 6.0 or greater") - 1);
  706. updateSize(pb, pos);
  707. break;
  708. }
  709. }
  710. /* Encoder */
  711. if(!(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
  712. {
  713. int pos = url_ftell(pb);
  714. put_be32(pb, 0); /* size */
  715. put_tag(pb, "\251enc");
  716. put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
  717. put_be16(pb, 0);
  718. put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
  719. updateSize(pb, pos);
  720. }
  721. if( s->title[0] )
  722. {
  723. int pos = url_ftell(pb);
  724. put_be32(pb, 0); /* size */
  725. put_tag(pb, "\251nam");
  726. put_be16(pb, strlen(s->title)); /* string length */
  727. put_be16(pb, 0);
  728. put_buffer(pb, s->title, strlen(s->title));
  729. updateSize(pb, pos);
  730. }
  731. if( s->author[0] )
  732. {
  733. int pos = url_ftell(pb);
  734. put_be32(pb, 0); /* size */
  735. put_tag(pb, /*"\251aut"*/ "\251day" );
  736. put_be16(pb, strlen(s->author)); /* string length */
  737. put_be16(pb, 0);
  738. put_buffer(pb, s->author, strlen(s->author));
  739. updateSize(pb, pos);
  740. }
  741. if( s->comment[0] )
  742. {
  743. int pos = url_ftell(pb);
  744. put_be32(pb, 0); /* size */
  745. put_tag(pb, "\251des");
  746. put_be16(pb, strlen(s->comment)); /* string length */
  747. put_be16(pb, 0);
  748. put_buffer(pb, s->comment, strlen(s->comment));
  749. updateSize(pb, pos);
  750. }
  751. return updateSize(pb, pos);
  752. }
  753. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  754. AVFormatContext *s)
  755. {
  756. int pos, i;
  757. pos = url_ftell(pb);
  758. put_be32(pb, 0); /* size placeholder*/
  759. put_tag(pb, "moov");
  760. mov->timescale = globalTimescale;
  761. for (i=0; i<MAX_STREAMS; i++) {
  762. if(mov->tracks[i].entry <= 0) continue;
  763. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  764. mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
  765. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
  766. }
  767. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  768. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  769. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  770. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  771. mov->tracks[i].timescale = 8000;
  772. }
  773. else {
  774. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  775. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  776. }
  777. }
  778. mov->tracks[i].trackDuration =
  779. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  780. mov->tracks[i].time = mov->time;
  781. mov->tracks[i].trackID = i+1;
  782. }
  783. mov_write_mvhd_tag(pb, mov);
  784. //mov_write_iods_tag(pb, mov);
  785. for (i=0; i<MAX_STREAMS; i++) {
  786. if(mov->tracks[i].entry > 0) {
  787. mov_write_trak_tag(pb, &(mov->tracks[i]));
  788. }
  789. }
  790. mov_write_udta_tag(pb, mov, s);
  791. return updateSize(pb, pos);
  792. }
  793. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  794. {
  795. mov->mdat_pos = url_ftell(pb);
  796. put_be32(pb, 0); /* size placeholder*/
  797. put_tag(pb, "mdat");
  798. return 0;
  799. }
  800. /* TODO: This needs to be more general */
  801. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  802. {
  803. MOVContext *mov = s->priv_data;
  804. put_be32(pb, 0x14 ); /* size */
  805. put_tag(pb, "ftyp");
  806. if ( mov->mode == MODE_3GP )
  807. put_tag(pb, "3gp4");
  808. else
  809. put_tag(pb, "isom");
  810. put_be32(pb, 0x200 );
  811. if ( mov->mode == MODE_3GP )
  812. put_tag(pb, "3gp4");
  813. else
  814. put_tag(pb, "mp41");
  815. return 0x14;
  816. }
  817. static int mov_write_header(AVFormatContext *s)
  818. {
  819. ByteIOContext *pb = &s->pb;
  820. MOVContext *mov = s->priv_data;
  821. int i;
  822. for(i=0; i<s->nb_streams; i++){
  823. AVCodecContext *c= &s->streams[i]->codec;
  824. if (c->codec_type == CODEC_TYPE_VIDEO){
  825. if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
  826. if(!codec_get_tag(codec_bmp_tags, c->codec_id))
  827. return -1;
  828. else
  829. av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
  830. }
  831. }else if(c->codec_type == CODEC_TYPE_AUDIO){
  832. if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
  833. if(!codec_get_tag(codec_wav_tags, c->codec_id))
  834. return -1;
  835. else
  836. av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
  837. }
  838. }
  839. }
  840. /* Default mode == MP4 */
  841. mov->mode = MODE_MP4;
  842. if (s->oformat != NULL) {
  843. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  844. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  845. if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
  846. mov_write_ftyp_tag(pb,s);
  847. }
  848. for (i=0; i<MAX_STREAMS; i++) {
  849. mov->tracks[i].mode = mov->mode;
  850. }
  851. put_flush_packet(pb);
  852. return 0;
  853. }
  854. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  855. {
  856. MOVContext *mov = s->priv_data;
  857. ByteIOContext *pb = &s->pb;
  858. AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
  859. MOVTrack* trk = &mov->tracks[pkt->stream_index];
  860. int cl, id;
  861. unsigned int samplesInChunk = 0;
  862. int size= pkt->size;
  863. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  864. if (!size) return 0; /* Discard 0 sized packets */
  865. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  866. samplesInChunk = 1;
  867. }
  868. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  869. if( enc->codec_id == CODEC_ID_AMR_NB) {
  870. /* We must find out how many AMR blocks there are in one packet */
  871. static uint16_t packed_size[16] =
  872. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  873. int len = 0;
  874. while (len < size && samplesInChunk < 100) {
  875. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  876. samplesInChunk++;
  877. }
  878. }
  879. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  880. samplesInChunk = size/enc->channels;
  881. }
  882. else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
  883. samplesInChunk = size/(2*enc->channels);
  884. }
  885. else {
  886. samplesInChunk = 1;
  887. }
  888. }
  889. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  890. && trk->vosLen == 0) {
  891. // assert(enc->extradata_size);
  892. trk->vosLen = enc->extradata_size;
  893. trk->vosData = av_malloc(trk->vosLen);
  894. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  895. }
  896. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  897. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  898. if (trk->ents_allocated <= trk->entry) {
  899. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  900. if (!trk->cluster)
  901. return -1;
  902. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  903. if (!trk->cluster[cl])
  904. return -1;
  905. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  906. }
  907. if (mov->mdat_written == 0) {
  908. mov_write_mdat_tag(pb, mov);
  909. mov->mdat_written = 1;
  910. mov->time = s->timestamp;
  911. }
  912. trk->cluster[cl][id].pos = url_ftell(pb);
  913. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  914. trk->cluster[cl][id].size = size;
  915. trk->cluster[cl][id].entries = samplesInChunk;
  916. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  917. trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  918. if(trk->cluster[cl][id].key_frame)
  919. trk->hasKeyframes = 1;
  920. }
  921. trk->enc = enc;
  922. trk->entry++;
  923. trk->sampleCount += samplesInChunk;
  924. trk->mdat_size += size;
  925. put_buffer(pb, pkt->data, size);
  926. put_flush_packet(pb);
  927. return 0;
  928. }
  929. static int mov_write_trailer(AVFormatContext *s)
  930. {
  931. MOVContext *mov = s->priv_data;
  932. ByteIOContext *pb = &s->pb;
  933. int res = 0;
  934. int i, j;
  935. offset_t moov_pos = url_ftell(pb);
  936. /* Write size of mdat tag */
  937. for (i=0, j=0; i<MAX_STREAMS; i++) {
  938. if(mov->tracks[i].ents_allocated > 0) {
  939. j += mov->tracks[i].mdat_size;
  940. }
  941. }
  942. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  943. put_be32(pb, j+8);
  944. url_fseek(pb, moov_pos, SEEK_SET);
  945. mov_write_moov_tag(pb, mov, s);
  946. for (i=0; i<MAX_STREAMS; i++) {
  947. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  948. av_free(mov->tracks[i].cluster[j]);
  949. }
  950. av_free(mov->tracks[i].cluster);
  951. if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
  952. mov->tracks[i].cluster = NULL;
  953. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  954. }
  955. put_flush_packet(pb);
  956. return res;
  957. }
  958. static AVOutputFormat mov_oformat = {
  959. "mov",
  960. "mov format",
  961. NULL,
  962. "mov",
  963. sizeof(MOVContext),
  964. CODEC_ID_AAC,
  965. CODEC_ID_MPEG4,
  966. mov_write_header,
  967. mov_write_packet,
  968. mov_write_trailer,
  969. };
  970. static AVOutputFormat _3gp_oformat = {
  971. "3gp",
  972. "3gp format",
  973. NULL,
  974. "3gp",
  975. sizeof(MOVContext),
  976. CODEC_ID_AMR_NB,
  977. CODEC_ID_H263,
  978. mov_write_header,
  979. mov_write_packet,
  980. mov_write_trailer,
  981. };
  982. static AVOutputFormat mp4_oformat = {
  983. "mp4",
  984. "mp4 format",
  985. "application/mp4",
  986. "mp4,m4a",
  987. sizeof(MOVContext),
  988. CODEC_ID_AAC,
  989. CODEC_ID_MPEG4,
  990. mov_write_header,
  991. mov_write_packet,
  992. mov_write_trailer,
  993. };
  994. int movenc_init(void)
  995. {
  996. av_register_output_format(&mov_oformat);
  997. av_register_output_format(&_3gp_oformat);
  998. av_register_output_format(&mp4_oformat);
  999. return 0;
  1000. }