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.

1362 lines
40KB

  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_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
  688. AVFormatContext *s)
  689. {
  690. int pos = url_ftell(pb);
  691. put_be32(pb, 0); /* size */
  692. put_tag(pb, "hdlr");
  693. put_be32(pb, 0);
  694. put_be32(pb, 0);
  695. put_tag(pb, "mdir");
  696. put_tag(pb, "appl");
  697. put_be32(pb, 0);
  698. put_be32(pb, 0);
  699. put_be16(pb, 0);
  700. return updateSize(pb, pos);
  701. }
  702. /* helper function to write a data tag with the specified string as data */
  703. static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
  704. AVFormatContext *s, const char *data)
  705. {
  706. int pos = url_ftell(pb);
  707. put_be32(pb, 0); /* size */
  708. put_tag(pb, "data");
  709. put_be32(pb, 1);
  710. put_be32(pb, 0);
  711. put_buffer(pb, data, strlen(data));
  712. return updateSize(pb, pos);
  713. }
  714. /* iTunes name of the song/movie */
  715. static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
  716. AVFormatContext *s)
  717. {
  718. int size = 0;
  719. if ( s->title[0] ) {
  720. int pos = url_ftell(pb);
  721. put_be32(pb, 0); /* size */
  722. put_tag(pb, "\251nam");
  723. mov_write_string_data_tag(pb, mov, s, s->title);
  724. size = updateSize(pb, pos);
  725. }
  726. return size;
  727. }
  728. /* iTunes name of the artist/performer */
  729. static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
  730. AVFormatContext *s)
  731. {
  732. int size = 0;
  733. if ( s->author[0] ) {
  734. int pos = url_ftell(pb);
  735. put_be32(pb, 0); /* size */
  736. put_tag(pb, "\251ART");
  737. // we use the author here as this is the only thing that we have...
  738. mov_write_string_data_tag(pb, mov, s, s->author);
  739. size = updateSize(pb, pos);
  740. }
  741. return size;
  742. }
  743. /* iTunes name of the writer */
  744. static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
  745. AVFormatContext *s)
  746. {
  747. int size = 0;
  748. if ( s->author[0] ) {
  749. int pos = url_ftell(pb);
  750. put_be32(pb, 0); /* size */
  751. put_tag(pb, "\251wrt");
  752. mov_write_string_data_tag(pb, mov, s, s->author);
  753. size = updateSize(pb, pos);
  754. }
  755. return size;
  756. }
  757. /* iTunes name of the album */
  758. static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
  759. AVFormatContext *s)
  760. {
  761. int size = 0;
  762. if ( s->album[0] ) {
  763. int pos = url_ftell(pb);
  764. put_be32(pb, 0); /* size */
  765. put_tag(pb, "\251alb");
  766. mov_write_string_data_tag(pb, mov, s, s->album);
  767. size = updateSize(pb, pos);
  768. }
  769. return size;
  770. }
  771. /* iTunes year */
  772. static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
  773. AVFormatContext *s)
  774. {
  775. char year[5];
  776. int size = 0;
  777. if ( s->year ) {
  778. int pos = url_ftell(pb);
  779. put_be32(pb, 0); /* size */
  780. put_tag(pb, "\251day");
  781. snprintf(year, 5, "%04d", s->year);
  782. mov_write_string_data_tag(pb, mov, s, year);
  783. size = updateSize(pb, pos);
  784. }
  785. return size;
  786. }
  787. /* iTunes tool used to create the file */
  788. static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
  789. AVFormatContext *s)
  790. {
  791. int pos = url_ftell(pb);
  792. put_be32(pb, 0); /* size */
  793. put_tag(pb, "\251too");
  794. mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
  795. return updateSize(pb, pos);
  796. }
  797. /* iTunes comment */
  798. static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
  799. AVFormatContext *s)
  800. {
  801. int size = 0;
  802. if ( s->comment[0] ) {
  803. int pos = url_ftell(pb);
  804. put_be32(pb, 0); /* size */
  805. put_tag(pb, "\251cmt");
  806. mov_write_string_data_tag(pb, mov, s, s->comment);
  807. size = updateSize(pb, pos);
  808. }
  809. return size;
  810. }
  811. /* iTunes custom genre */
  812. static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
  813. AVFormatContext *s)
  814. {
  815. int size = 0;
  816. if ( s->genre[0] ) {
  817. int pos = url_ftell(pb);
  818. put_be32(pb, 0); /* size */
  819. put_tag(pb, "\251gen");
  820. mov_write_string_data_tag(pb, mov, s, s->genre);
  821. size = updateSize(pb, pos);
  822. }
  823. return size;
  824. }
  825. /* iTunes track number */
  826. static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
  827. AVFormatContext *s)
  828. {
  829. int size = 0;
  830. if ( s->track ) {
  831. int pos = url_ftell(pb);
  832. put_be32(pb, 0); /* size */
  833. put_tag(pb, "trkn");
  834. {
  835. int pos = url_ftell(pb);
  836. put_be32(pb, 0); /* size */
  837. put_tag(pb, "data");
  838. put_be32(pb, 0); // 8 bytes empty
  839. put_be32(pb, 0);
  840. put_be16(pb, 0); // empty
  841. put_be16(pb, s->track); // track number
  842. put_be16(pb, 0); // total track number
  843. put_be16(pb, 0); // empty
  844. updateSize(pb, pos);
  845. }
  846. size = updateSize(pb, pos);
  847. }
  848. return size;
  849. }
  850. /* iTunes meta data list */
  851. static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
  852. AVFormatContext *s)
  853. {
  854. int pos = url_ftell(pb);
  855. put_be32(pb, 0); /* size */
  856. put_tag(pb, "ilst");
  857. mov_write_nam_tag(pb, mov, s);
  858. mov_write_ART_tag(pb, mov, s);
  859. mov_write_wrt_tag(pb, mov, s);
  860. mov_write_alb_tag(pb, mov, s);
  861. mov_write_day_tag(pb, mov, s);
  862. mov_write_too_tag(pb, mov, s);
  863. mov_write_cmt_tag(pb, mov, s);
  864. mov_write_gen_tag(pb, mov, s);
  865. mov_write_trkn_tag(pb, mov, s);
  866. return updateSize(pb, pos);
  867. }
  868. /* iTunes meta data tag */
  869. static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
  870. AVFormatContext *s)
  871. {
  872. int size = 0;
  873. // only save meta tag if required
  874. if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
  875. s->comment[0] || s->genre[0] || s->track ) {
  876. int pos = url_ftell(pb);
  877. put_be32(pb, 0); /* size */
  878. put_tag(pb, "meta");
  879. put_be32(pb, 0);
  880. mov_write_itunes_hdlr_tag(pb, mov, s);
  881. mov_write_ilst_tag(pb, mov, s);
  882. size = updateSize(pb, pos);
  883. }
  884. return size;
  885. }
  886. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
  887. AVFormatContext *s)
  888. {
  889. int pos = url_ftell(pb);
  890. int i;
  891. put_be32(pb, 0); /* size */
  892. put_tag(pb, "udta");
  893. /* iTunes meta data */
  894. mov_write_meta_tag(pb, mov, s);
  895. /* Requirements */
  896. for (i=0; i<MAX_STREAMS; i++) {
  897. if(mov->tracks[i].entry <= 0) continue;
  898. if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
  899. mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
  900. int pos = url_ftell(pb);
  901. put_be32(pb, 0); /* size */
  902. put_tag(pb, "\251req");
  903. put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
  904. put_be16(pb, 0);
  905. put_buffer(pb, "QuickTime 6.0 or greater",
  906. sizeof("QuickTime 6.0 or greater") - 1);
  907. updateSize(pb, pos);
  908. break;
  909. }
  910. }
  911. /* Encoder */
  912. if(!(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
  913. {
  914. int pos = url_ftell(pb);
  915. put_be32(pb, 0); /* size */
  916. put_tag(pb, "\251enc");
  917. put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
  918. put_be16(pb, 0);
  919. put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
  920. updateSize(pb, pos);
  921. }
  922. if( s->title[0] )
  923. {
  924. int pos = url_ftell(pb);
  925. put_be32(pb, 0); /* size */
  926. put_tag(pb, "\251nam");
  927. put_be16(pb, strlen(s->title)); /* string length */
  928. put_be16(pb, 0);
  929. put_buffer(pb, s->title, strlen(s->title));
  930. updateSize(pb, pos);
  931. }
  932. if( s->author[0] )
  933. {
  934. int pos = url_ftell(pb);
  935. put_be32(pb, 0); /* size */
  936. put_tag(pb, /*"\251aut"*/ "\251day" );
  937. put_be16(pb, strlen(s->author)); /* string length */
  938. put_be16(pb, 0);
  939. put_buffer(pb, s->author, strlen(s->author));
  940. updateSize(pb, pos);
  941. }
  942. if( s->comment[0] )
  943. {
  944. int pos = url_ftell(pb);
  945. put_be32(pb, 0); /* size */
  946. put_tag(pb, "\251des");
  947. put_be16(pb, strlen(s->comment)); /* string length */
  948. put_be16(pb, 0);
  949. put_buffer(pb, s->comment, strlen(s->comment));
  950. updateSize(pb, pos);
  951. }
  952. return updateSize(pb, pos);
  953. }
  954. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  955. AVFormatContext *s)
  956. {
  957. int pos, i;
  958. pos = url_ftell(pb);
  959. put_be32(pb, 0); /* size placeholder*/
  960. put_tag(pb, "moov");
  961. mov->timescale = globalTimescale;
  962. for (i=0; i<MAX_STREAMS; i++) {
  963. if(mov->tracks[i].entry <= 0) continue;
  964. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  965. mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
  966. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
  967. }
  968. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  969. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  970. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  971. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  972. mov->tracks[i].timescale = 8000;
  973. }
  974. else {
  975. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  976. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  977. }
  978. }
  979. mov->tracks[i].trackDuration =
  980. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  981. mov->tracks[i].time = mov->time;
  982. mov->tracks[i].trackID = i+1;
  983. }
  984. mov_write_mvhd_tag(pb, mov);
  985. //mov_write_iods_tag(pb, mov);
  986. for (i=0; i<MAX_STREAMS; i++) {
  987. if(mov->tracks[i].entry > 0) {
  988. mov_write_trak_tag(pb, &(mov->tracks[i]));
  989. }
  990. }
  991. mov_write_udta_tag(pb, mov, s);
  992. return updateSize(pb, pos);
  993. }
  994. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  995. {
  996. mov->mdat_pos = url_ftell(pb);
  997. put_be32(pb, 0); /* size placeholder*/
  998. put_tag(pb, "mdat");
  999. return 0;
  1000. }
  1001. /* TODO: This needs to be more general */
  1002. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  1003. {
  1004. MOVContext *mov = s->priv_data;
  1005. put_be32(pb, 0x14 ); /* size */
  1006. put_tag(pb, "ftyp");
  1007. if ( mov->mode == MODE_3GP )
  1008. put_tag(pb, "3gp4");
  1009. else
  1010. put_tag(pb, "isom");
  1011. put_be32(pb, 0x200 );
  1012. if ( mov->mode == MODE_3GP )
  1013. put_tag(pb, "3gp4");
  1014. else
  1015. put_tag(pb, "mp41");
  1016. return 0x14;
  1017. }
  1018. static int mov_write_header(AVFormatContext *s)
  1019. {
  1020. ByteIOContext *pb = &s->pb;
  1021. MOVContext *mov = s->priv_data;
  1022. int i;
  1023. for(i=0; i<s->nb_streams; i++){
  1024. AVCodecContext *c= &s->streams[i]->codec;
  1025. if (c->codec_type == CODEC_TYPE_VIDEO){
  1026. if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
  1027. if(!codec_get_tag(codec_bmp_tags, c->codec_id))
  1028. return -1;
  1029. else
  1030. av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
  1031. }
  1032. }else if(c->codec_type == CODEC_TYPE_AUDIO){
  1033. if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
  1034. if(!codec_get_tag(codec_wav_tags, c->codec_id))
  1035. return -1;
  1036. else
  1037. av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
  1038. }
  1039. }
  1040. }
  1041. /* Default mode == MP4 */
  1042. mov->mode = MODE_MP4;
  1043. if (s->oformat != NULL) {
  1044. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  1045. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  1046. if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
  1047. mov_write_ftyp_tag(pb,s);
  1048. }
  1049. for (i=0; i<MAX_STREAMS; i++) {
  1050. mov->tracks[i].mode = mov->mode;
  1051. }
  1052. put_flush_packet(pb);
  1053. return 0;
  1054. }
  1055. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  1056. {
  1057. MOVContext *mov = s->priv_data;
  1058. ByteIOContext *pb = &s->pb;
  1059. AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
  1060. MOVTrack* trk = &mov->tracks[pkt->stream_index];
  1061. int cl, id;
  1062. unsigned int samplesInChunk = 0;
  1063. int size= pkt->size;
  1064. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  1065. if (!size) return 0; /* Discard 0 sized packets */
  1066. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  1067. samplesInChunk = 1;
  1068. }
  1069. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  1070. if( enc->codec_id == CODEC_ID_AMR_NB) {
  1071. /* We must find out how many AMR blocks there are in one packet */
  1072. static uint16_t packed_size[16] =
  1073. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  1074. int len = 0;
  1075. while (len < size && samplesInChunk < 100) {
  1076. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  1077. samplesInChunk++;
  1078. }
  1079. }
  1080. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  1081. samplesInChunk = size/enc->channels;
  1082. }
  1083. else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
  1084. samplesInChunk = size/(2*enc->channels);
  1085. }
  1086. else {
  1087. samplesInChunk = 1;
  1088. }
  1089. }
  1090. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  1091. && trk->vosLen == 0) {
  1092. // assert(enc->extradata_size);
  1093. trk->vosLen = enc->extradata_size;
  1094. trk->vosData = av_malloc(trk->vosLen);
  1095. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  1096. }
  1097. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  1098. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  1099. if (trk->ents_allocated <= trk->entry) {
  1100. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  1101. if (!trk->cluster)
  1102. return -1;
  1103. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  1104. if (!trk->cluster[cl])
  1105. return -1;
  1106. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  1107. }
  1108. if (mov->mdat_written == 0) {
  1109. mov_write_mdat_tag(pb, mov);
  1110. mov->mdat_written = 1;
  1111. mov->time = s->timestamp;
  1112. }
  1113. trk->cluster[cl][id].pos = url_ftell(pb);
  1114. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  1115. trk->cluster[cl][id].size = size;
  1116. trk->cluster[cl][id].entries = samplesInChunk;
  1117. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  1118. trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  1119. if(trk->cluster[cl][id].key_frame)
  1120. trk->hasKeyframes = 1;
  1121. }
  1122. trk->enc = enc;
  1123. trk->entry++;
  1124. trk->sampleCount += samplesInChunk;
  1125. trk->mdat_size += size;
  1126. put_buffer(pb, pkt->data, size);
  1127. put_flush_packet(pb);
  1128. return 0;
  1129. }
  1130. static int mov_write_trailer(AVFormatContext *s)
  1131. {
  1132. MOVContext *mov = s->priv_data;
  1133. ByteIOContext *pb = &s->pb;
  1134. int res = 0;
  1135. int i, j;
  1136. offset_t moov_pos = url_ftell(pb);
  1137. /* Write size of mdat tag */
  1138. for (i=0, j=0; i<MAX_STREAMS; i++) {
  1139. if(mov->tracks[i].ents_allocated > 0) {
  1140. j += mov->tracks[i].mdat_size;
  1141. }
  1142. }
  1143. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  1144. put_be32(pb, j+8);
  1145. url_fseek(pb, moov_pos, SEEK_SET);
  1146. mov_write_moov_tag(pb, mov, s);
  1147. for (i=0; i<MAX_STREAMS; i++) {
  1148. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  1149. av_free(mov->tracks[i].cluster[j]);
  1150. }
  1151. av_free(mov->tracks[i].cluster);
  1152. if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
  1153. mov->tracks[i].cluster = NULL;
  1154. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  1155. }
  1156. put_flush_packet(pb);
  1157. return res;
  1158. }
  1159. static AVOutputFormat mov_oformat = {
  1160. "mov",
  1161. "mov format",
  1162. NULL,
  1163. "mov",
  1164. sizeof(MOVContext),
  1165. CODEC_ID_AAC,
  1166. CODEC_ID_MPEG4,
  1167. mov_write_header,
  1168. mov_write_packet,
  1169. mov_write_trailer,
  1170. };
  1171. static AVOutputFormat _3gp_oformat = {
  1172. "3gp",
  1173. "3gp format",
  1174. NULL,
  1175. "3gp",
  1176. sizeof(MOVContext),
  1177. CODEC_ID_AMR_NB,
  1178. CODEC_ID_H263,
  1179. mov_write_header,
  1180. mov_write_packet,
  1181. mov_write_trailer,
  1182. };
  1183. static AVOutputFormat mp4_oformat = {
  1184. "mp4",
  1185. "mp4 format",
  1186. "application/mp4",
  1187. "mp4,m4a",
  1188. sizeof(MOVContext),
  1189. CODEC_ID_AAC,
  1190. CODEC_ID_MPEG4,
  1191. mov_write_header,
  1192. mov_write_packet,
  1193. mov_write_trailer,
  1194. };
  1195. int movenc_init(void)
  1196. {
  1197. av_register_output_format(&mov_oformat);
  1198. av_register_output_format(&_3gp_oformat);
  1199. av_register_output_format(&mp4_oformat);
  1200. return 0;
  1201. }