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.

1366 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 = track->enc->codec_tag;
  229. if (!tag)
  230. tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
  231. // if no mac fcc found, try with Microsoft tags
  232. if (!tag)
  233. {
  234. int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
  235. tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
  236. }
  237. put_le32(pb, tag); // store it byteswapped
  238. put_be32(pb, 0); /* Reserved */
  239. put_be16(pb, 0); /* Reserved */
  240. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  241. /* SoundDescription */
  242. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  243. put_be16(pb, 1); /* Version 1 */
  244. else
  245. put_be16(pb, 0); /* Version 0 */
  246. put_be16(pb, 0); /* Revision level */
  247. put_be32(pb, 0); /* Reserved */
  248. put_be16(pb, track->enc->channels); /* Number of channels */
  249. /* TODO: Currently hard-coded to 16-bit, there doesn't seem
  250. to be a good way to get number of bits of audio */
  251. put_be16(pb, 0x10); /* Reserved */
  252. if(track->enc->codec_id == CODEC_ID_AAC ||
  253. track->enc->codec_id == CODEC_ID_MP3)
  254. {
  255. put_be16(pb, 0xfffe); /* compression ID (vbr)*/
  256. }
  257. else
  258. {
  259. put_be16(pb, 0); /* compression ID (= 0) */
  260. }
  261. put_be16(pb, 0); /* packet size (= 0) */
  262. put_be16(pb, track->timescale); /* Time scale */
  263. put_be16(pb, 0); /* Reserved */
  264. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  265. {
  266. /* SoundDescription V1 extended info */
  267. put_be32(pb, track->enc->frame_size); /* Samples per packet */
  268. put_be32(pb, 1536); /* Bytes per packet */
  269. put_be32(pb, 2); /* Bytes per frame */
  270. put_be32(pb, 2); /* Bytes per sample */
  271. }
  272. if(track->enc->codec_id == CODEC_ID_AAC) {
  273. if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
  274. else mov_write_esds_tag(pb, track);
  275. }
  276. if(track->enc->codec_id == CODEC_ID_AMR_NB)
  277. mov_write_damr_tag(pb);
  278. return updateSize (pb, pos);
  279. }
  280. static int mov_write_d263_tag(ByteIOContext *pb)
  281. {
  282. put_be32(pb, 0xf); /* size */
  283. put_tag(pb, "d263");
  284. put_tag(pb, "FFMP");
  285. put_be16(pb, 0x0a);
  286. put_byte(pb, 0);
  287. return 0xf;
  288. }
  289. /* TODO: No idea about these values */
  290. static int mov_write_svq3_tag(ByteIOContext *pb)
  291. {
  292. put_be32(pb, 0x15);
  293. put_tag(pb, "SMI ");
  294. put_tag(pb, "SEQH");
  295. put_be32(pb, 0x5);
  296. put_be32(pb, 0xe2c0211d);
  297. put_be32(pb, 0xc0000000);
  298. put_byte(pb, 0);
  299. return 0x15;
  300. }
  301. static unsigned int descrLength(unsigned int len)
  302. {
  303. if (len < 0x00000080)
  304. return 2 + len;
  305. else if (len < 0x00004000)
  306. return 3 + len;
  307. else if(len < 0x00200000)
  308. return 4 + len;
  309. else
  310. return 5 + len;
  311. }
  312. static void putDescr(ByteIOContext *pb, int tag, int size)
  313. {
  314. uint32_t len;
  315. uint8_t vals[4];
  316. len = size;
  317. vals[3] = (uint8_t)(len & 0x7f);
  318. len >>= 7;
  319. vals[2] = (uint8_t)((len & 0x7f) | 0x80);
  320. len >>= 7;
  321. vals[1] = (uint8_t)((len & 0x7f) | 0x80);
  322. len >>= 7;
  323. vals[0] = (uint8_t)((len & 0x7f) | 0x80);
  324. put_byte(pb, tag); // DescriptorTag
  325. if (size < 0x00000080)
  326. {
  327. put_byte(pb, vals[3]);
  328. }
  329. else if (size < 0x00004000)
  330. {
  331. put_byte(pb, vals[2]);
  332. put_byte(pb, vals[3]);
  333. }
  334. else if (size < 0x00200000)
  335. {
  336. put_byte(pb, vals[1]);
  337. put_byte(pb, vals[2]);
  338. put_byte(pb, vals[3]);
  339. }
  340. else if (size < 0x10000000)
  341. {
  342. put_byte(pb, vals[0]);
  343. put_byte(pb, vals[1]);
  344. put_byte(pb, vals[2]);
  345. put_byte(pb, vals[3]);
  346. }
  347. }
  348. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
  349. {
  350. int decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  351. int pos = url_ftell(pb);
  352. put_be32(pb, 0); // size
  353. put_tag(pb, "esds");
  354. put_be32(pb, 0); // Version
  355. // ES descriptor
  356. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  357. descrLength(1));
  358. put_be16(pb, 0x0001); // ID (= 1)
  359. put_byte(pb, 0x00); // flags (= no flags)
  360. // DecoderConfig descriptor
  361. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  362. if(track->enc->codec_id == CODEC_ID_AAC)
  363. put_byte(pb, 0x40); // Object type indication
  364. else if(track->enc->codec_id == CODEC_ID_MPEG4)
  365. put_byte(pb, 0x20); // Object type indication (Visual 14496-2)
  366. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  367. put_byte(pb, 0x15); // flags (= Audiostream)
  368. else
  369. put_byte(pb, 0x11); // flags (= Visualstream)
  370. put_byte(pb, 0x0); // Buffersize DB (24 bits)
  371. put_be16(pb, 0x0dd2); // Buffersize DB
  372. // TODO: find real values for these
  373. put_be32(pb, 0x0002e918); // maxbitrate
  374. put_be32(pb, 0x00017e6b); // avg bitrate
  375. if (track->vosLen)
  376. {
  377. // DecoderSpecific info descriptor
  378. putDescr(pb, 0x05, track->vosLen);
  379. put_buffer(pb, track->vosData, track->vosLen);
  380. }
  381. // SL descriptor
  382. putDescr(pb, 0x06, 1);
  383. put_byte(pb, 0x02);
  384. return updateSize (pb, pos);
  385. }
  386. const CodecTag codec_movvideo_tags[] = {
  387. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
  388. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
  389. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  390. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
  391. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
  392. { 0, 0 },
  393. };
  394. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
  395. {
  396. int pos = url_ftell(pb);
  397. int tag;
  398. put_be32(pb, 0); /* size */
  399. tag = track->enc->codec_tag;
  400. if (!tag)
  401. tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
  402. // if no mac fcc found, try with Microsoft tags
  403. if (!tag)
  404. tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
  405. put_le32(pb, tag); // store it byteswapped
  406. put_be32(pb, 0); /* Reserved */
  407. put_be16(pb, 0); /* Reserved */
  408. put_be16(pb, 1); /* Data-reference index */
  409. put_be32(pb, 0); /* Reserved (= 02000c) */
  410. put_be32(pb, 0); /* Reserved ("SVis")*/
  411. put_be32(pb, 0); /* Reserved */
  412. put_be32(pb, 0); /* Reserved (400)*/
  413. put_be16(pb, track->enc->width); /* Video width */
  414. put_be16(pb, track->enc->height); /* Video height */
  415. put_be32(pb, 0x00480000); /* Reserved */
  416. put_be32(pb, 0x00480000); /* Reserved */
  417. put_be32(pb, 0); /* Data size (= 0) */
  418. put_be16(pb, 1); /* Frame count (= 1) */
  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_be32(pb, 0); /* Reserved */
  424. put_be32(pb, 0); /* Reserved */
  425. put_be32(pb, 0); /* Reserved */
  426. put_be32(pb, 0); /* Reserved */
  427. put_be16(pb, 0x18); /* Reserved */
  428. put_be16(pb, 0xffff); /* Reserved */
  429. if(track->enc->codec_id == CODEC_ID_MPEG4)
  430. mov_write_esds_tag(pb, track);
  431. else if(track->enc->codec_id == CODEC_ID_H263)
  432. mov_write_d263_tag(pb);
  433. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  434. mov_write_svq3_tag(pb);
  435. return updateSize (pb, pos);
  436. }
  437. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
  438. {
  439. int pos = url_ftell(pb);
  440. put_be32(pb, 0); /* size */
  441. put_tag(pb, "stsd");
  442. put_be32(pb, 0); /* version & flags */
  443. put_be32(pb, 1); /* entry count */
  444. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  445. mov_write_video_tag(pb, track);
  446. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  447. mov_write_audio_tag(pb, track);
  448. return updateSize(pb, pos);
  449. }
  450. /* TODO?: Currently all samples/frames seem to have same duration */
  451. /* Time to sample atom */
  452. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
  453. {
  454. put_be32(pb, 0x18); /* size */
  455. put_tag(pb, "stts");
  456. put_be32(pb, 0); /* version & flags */
  457. put_be32(pb, 1); /* entry count */
  458. put_be32(pb, track->sampleCount); /* sample count */
  459. put_be32(pb, track->sampleDuration); /* sample duration */
  460. return 0x18;
  461. }
  462. static int mov_write_dref_tag(ByteIOContext *pb)
  463. {
  464. put_be32(pb, 28); /* size */
  465. put_tag(pb, "dref");
  466. put_be32(pb, 0); /* version & flags */
  467. put_be32(pb, 1); /* entry count */
  468. put_be32(pb, 0xc); /* size */
  469. put_tag(pb, "url ");
  470. put_be32(pb, 1); /* version & flags */
  471. return 28;
  472. }
  473. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
  474. {
  475. int pos = url_ftell(pb);
  476. put_be32(pb, 0); /* size */
  477. put_tag(pb, "stbl");
  478. mov_write_stsd_tag(pb, track);
  479. mov_write_stts_tag(pb, track);
  480. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  481. track->hasKeyframes)
  482. mov_write_stss_tag(pb, track);
  483. mov_write_stsc_tag(pb, track);
  484. mov_write_stsz_tag(pb, track);
  485. mov_write_stco_tag(pb, track);
  486. return updateSize(pb, pos);
  487. }
  488. static int mov_write_dinf_tag(ByteIOContext *pb)
  489. {
  490. int pos = url_ftell(pb);
  491. put_be32(pb, 0); /* size */
  492. put_tag(pb, "dinf");
  493. mov_write_dref_tag(pb);
  494. return updateSize(pb, pos);
  495. }
  496. static int mov_write_smhd_tag(ByteIOContext *pb)
  497. {
  498. put_be32(pb, 16); /* size */
  499. put_tag(pb, "smhd");
  500. put_be32(pb, 0); /* version & flags */
  501. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  502. put_be16(pb, 0); /* reserved */
  503. return 16;
  504. }
  505. static int mov_write_vmhd_tag(ByteIOContext *pb)
  506. {
  507. put_be32(pb, 0x14); /* size (always 0x14) */
  508. put_tag(pb, "vmhd");
  509. put_be32(pb, 0x01); /* version & flags */
  510. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  511. return 0x14;
  512. }
  513. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
  514. {
  515. char *descr, *hdlr, *hdlr_type;
  516. int pos = url_ftell(pb);
  517. if (!track) { /* no media --> data handler */
  518. hdlr = "dhlr";
  519. hdlr_type = "url ";
  520. descr = "DataHandler";
  521. } else {
  522. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  523. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  524. hdlr_type = "vide";
  525. descr = "VideoHandler";
  526. } else {
  527. hdlr_type = "soun";
  528. descr = "SoundHandler";
  529. }
  530. }
  531. put_be32(pb, 0); /* size */
  532. put_tag(pb, "hdlr");
  533. put_be32(pb, 0); /* Version & flags */
  534. put_buffer(pb, hdlr, 4); /* handler */
  535. put_tag(pb, hdlr_type); /* handler type */
  536. put_be32(pb ,0); /* reserved */
  537. put_be32(pb ,0); /* reserved */
  538. put_be32(pb ,0); /* reserved */
  539. put_byte(pb, strlen(descr)); /* string counter */
  540. put_buffer(pb, descr, strlen(descr)); /* handler description */
  541. return updateSize(pb, pos);
  542. }
  543. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
  544. {
  545. int pos = url_ftell(pb);
  546. put_be32(pb, 0); /* size */
  547. put_tag(pb, "minf");
  548. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  549. mov_write_vmhd_tag(pb);
  550. else
  551. mov_write_smhd_tag(pb);
  552. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  553. mov_write_hdlr_tag(pb, NULL);
  554. mov_write_dinf_tag(pb);
  555. mov_write_stbl_tag(pb, track);
  556. return updateSize(pb, pos);
  557. }
  558. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
  559. {
  560. put_be32(pb, 32); /* size */
  561. put_tag(pb, "mdhd");
  562. put_be32(pb, 0); /* Version & flags */
  563. put_be32(pb, track->time); /* creation time */
  564. put_be32(pb, track->time); /* modification time */
  565. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  566. put_be32(pb, track->trackDuration); /* duration */
  567. put_be16(pb, 0); /* language, 0 = english */
  568. put_be16(pb, 0); /* reserved (quality) */
  569. return 32;
  570. }
  571. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
  572. {
  573. int pos = url_ftell(pb);
  574. put_be32(pb, 0); /* size */
  575. put_tag(pb, "mdia");
  576. mov_write_mdhd_tag(pb, track);
  577. mov_write_hdlr_tag(pb, track);
  578. mov_write_minf_tag(pb, track);
  579. return updateSize(pb, pos);
  580. }
  581. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
  582. {
  583. int64_t maxTrackLenTemp;
  584. put_be32(pb, 0x5c); /* size (always 0x5c) */
  585. put_tag(pb, "tkhd");
  586. put_be32(pb, 0xf); /* version & flags (track enabled) */
  587. put_be32(pb, track->time); /* creation time */
  588. put_be32(pb, track->time); /* modification time */
  589. put_be32(pb, track->trackID); /* track-id */
  590. put_be32(pb, 0); /* reserved */
  591. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)track->trackDuration)/(int64_t)track->timescale;
  592. put_be32(pb, (long)maxTrackLenTemp); /* duration */
  593. put_be32(pb, 0); /* reserved */
  594. put_be32(pb, 0); /* reserved */
  595. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  596. /* Volume, only for audio */
  597. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  598. put_be16(pb, 0x0100);
  599. else
  600. put_be16(pb, 0);
  601. put_be16(pb, 0); /* reserved */
  602. /* Matrix structure */
  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, 0x00010000); /* reserved */
  608. put_be32(pb, 0x0); /* reserved */
  609. put_be32(pb, 0x0); /* reserved */
  610. put_be32(pb, 0x0); /* reserved */
  611. put_be32(pb, 0x40000000); /* reserved */
  612. /* Track width and height, for visual only */
  613. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  614. double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
  615. if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
  616. put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  617. put_be32(pb, track->enc->height*0x10000);
  618. }
  619. else {
  620. put_be32(pb, 0);
  621. put_be32(pb, 0);
  622. }
  623. return 0x5c;
  624. }
  625. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
  626. {
  627. int pos = url_ftell(pb);
  628. put_be32(pb, 0); /* size */
  629. put_tag(pb, "trak");
  630. mov_write_tkhd_tag(pb, track);
  631. mov_write_mdia_tag(pb, track);
  632. return updateSize(pb, pos);
  633. }
  634. /* TODO: Not sorted out, but not necessary either */
  635. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  636. {
  637. put_be32(pb, 0x15); /* size */
  638. put_tag(pb, "iods");
  639. put_be32(pb, 0); /* version & flags */
  640. put_be16(pb, 0x1007);
  641. put_byte(pb, 0);
  642. put_be16(pb, 0x4fff);
  643. put_be16(pb, 0xfffe);
  644. put_be16(pb, 0x01ff);
  645. return 0x15;
  646. }
  647. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  648. {
  649. int maxTrackID = 1, maxTrackLen = 0, i;
  650. int64_t maxTrackLenTemp;
  651. put_be32(pb, 0x6c); /* size (always 0x6c) */
  652. put_tag(pb, "mvhd");
  653. put_be32(pb, 0); /* version & flags */
  654. put_be32(pb, mov->time); /* creation time */
  655. put_be32(pb, mov->time); /* modification time */
  656. put_be32(pb, mov->timescale); /* timescale */
  657. for (i=0; i<MAX_STREAMS; i++) {
  658. if(mov->tracks[i].entry > 0) {
  659. maxTrackLenTemp = ((int64_t)globalTimescale*(int64_t)mov->tracks[i].trackDuration)/(int64_t)mov->tracks[i].timescale;
  660. if(maxTrackLen < maxTrackLenTemp)
  661. maxTrackLen = maxTrackLenTemp;
  662. if(maxTrackID < mov->tracks[i].trackID)
  663. maxTrackID = mov->tracks[i].trackID;
  664. }
  665. }
  666. put_be32(pb, maxTrackLen); /* duration of longest track */
  667. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  668. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  669. put_be16(pb, 0); /* reserved */
  670. put_be32(pb, 0); /* reserved */
  671. put_be32(pb, 0); /* reserved */
  672. /* Matrix structure */
  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, 0x00010000); /* reserved */
  678. put_be32(pb, 0x0); /* reserved */
  679. put_be32(pb, 0x0); /* reserved */
  680. put_be32(pb, 0x0); /* reserved */
  681. put_be32(pb, 0x40000000); /* reserved */
  682. put_be32(pb, 0); /* reserved (preview time) */
  683. put_be32(pb, 0); /* reserved (preview duration) */
  684. put_be32(pb, 0); /* reserved (poster time) */
  685. put_be32(pb, 0); /* reserved (selection time) */
  686. put_be32(pb, 0); /* reserved (selection duration) */
  687. put_be32(pb, 0); /* reserved (current time) */
  688. put_be32(pb, maxTrackID+1); /* Next track id */
  689. return 0x6c;
  690. }
  691. static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
  692. AVFormatContext *s)
  693. {
  694. int pos = url_ftell(pb);
  695. put_be32(pb, 0); /* size */
  696. put_tag(pb, "hdlr");
  697. put_be32(pb, 0);
  698. put_be32(pb, 0);
  699. put_tag(pb, "mdir");
  700. put_tag(pb, "appl");
  701. put_be32(pb, 0);
  702. put_be32(pb, 0);
  703. put_be16(pb, 0);
  704. return updateSize(pb, pos);
  705. }
  706. /* helper function to write a data tag with the specified string as data */
  707. static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
  708. AVFormatContext *s, const char *data)
  709. {
  710. int pos = url_ftell(pb);
  711. put_be32(pb, 0); /* size */
  712. put_tag(pb, "data");
  713. put_be32(pb, 1);
  714. put_be32(pb, 0);
  715. put_buffer(pb, data, strlen(data));
  716. return updateSize(pb, pos);
  717. }
  718. /* iTunes name of the song/movie */
  719. static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
  720. AVFormatContext *s)
  721. {
  722. int size = 0;
  723. if ( s->title[0] ) {
  724. int pos = url_ftell(pb);
  725. put_be32(pb, 0); /* size */
  726. put_tag(pb, "\251nam");
  727. mov_write_string_data_tag(pb, mov, s, s->title);
  728. size = updateSize(pb, pos);
  729. }
  730. return size;
  731. }
  732. /* iTunes name of the artist/performer */
  733. static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
  734. AVFormatContext *s)
  735. {
  736. int size = 0;
  737. if ( s->author[0] ) {
  738. int pos = url_ftell(pb);
  739. put_be32(pb, 0); /* size */
  740. put_tag(pb, "\251ART");
  741. // we use the author here as this is the only thing that we have...
  742. mov_write_string_data_tag(pb, mov, s, s->author);
  743. size = updateSize(pb, pos);
  744. }
  745. return size;
  746. }
  747. /* iTunes name of the writer */
  748. static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
  749. AVFormatContext *s)
  750. {
  751. int size = 0;
  752. if ( s->author[0] ) {
  753. int pos = url_ftell(pb);
  754. put_be32(pb, 0); /* size */
  755. put_tag(pb, "\251wrt");
  756. mov_write_string_data_tag(pb, mov, s, s->author);
  757. size = updateSize(pb, pos);
  758. }
  759. return size;
  760. }
  761. /* iTunes name of the album */
  762. static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
  763. AVFormatContext *s)
  764. {
  765. int size = 0;
  766. if ( s->album[0] ) {
  767. int pos = url_ftell(pb);
  768. put_be32(pb, 0); /* size */
  769. put_tag(pb, "\251alb");
  770. mov_write_string_data_tag(pb, mov, s, s->album);
  771. size = updateSize(pb, pos);
  772. }
  773. return size;
  774. }
  775. /* iTunes year */
  776. static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
  777. AVFormatContext *s)
  778. {
  779. char year[5];
  780. int size = 0;
  781. if ( s->year ) {
  782. int pos = url_ftell(pb);
  783. put_be32(pb, 0); /* size */
  784. put_tag(pb, "\251day");
  785. snprintf(year, 5, "%04d", s->year);
  786. mov_write_string_data_tag(pb, mov, s, year);
  787. size = updateSize(pb, pos);
  788. }
  789. return size;
  790. }
  791. /* iTunes tool used to create the file */
  792. static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
  793. AVFormatContext *s)
  794. {
  795. int pos = url_ftell(pb);
  796. put_be32(pb, 0); /* size */
  797. put_tag(pb, "\251too");
  798. mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
  799. return updateSize(pb, pos);
  800. }
  801. /* iTunes comment */
  802. static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
  803. AVFormatContext *s)
  804. {
  805. int size = 0;
  806. if ( s->comment[0] ) {
  807. int pos = url_ftell(pb);
  808. put_be32(pb, 0); /* size */
  809. put_tag(pb, "\251cmt");
  810. mov_write_string_data_tag(pb, mov, s, s->comment);
  811. size = updateSize(pb, pos);
  812. }
  813. return size;
  814. }
  815. /* iTunes custom genre */
  816. static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
  817. AVFormatContext *s)
  818. {
  819. int size = 0;
  820. if ( s->genre[0] ) {
  821. int pos = url_ftell(pb);
  822. put_be32(pb, 0); /* size */
  823. put_tag(pb, "\251gen");
  824. mov_write_string_data_tag(pb, mov, s, s->genre);
  825. size = updateSize(pb, pos);
  826. }
  827. return size;
  828. }
  829. /* iTunes track number */
  830. static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
  831. AVFormatContext *s)
  832. {
  833. int size = 0;
  834. if ( s->track ) {
  835. int pos = url_ftell(pb);
  836. put_be32(pb, 0); /* size */
  837. put_tag(pb, "trkn");
  838. {
  839. int pos = url_ftell(pb);
  840. put_be32(pb, 0); /* size */
  841. put_tag(pb, "data");
  842. put_be32(pb, 0); // 8 bytes empty
  843. put_be32(pb, 0);
  844. put_be16(pb, 0); // empty
  845. put_be16(pb, s->track); // track number
  846. put_be16(pb, 0); // total track number
  847. put_be16(pb, 0); // empty
  848. updateSize(pb, pos);
  849. }
  850. size = updateSize(pb, pos);
  851. }
  852. return size;
  853. }
  854. /* iTunes meta data list */
  855. static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
  856. AVFormatContext *s)
  857. {
  858. int pos = url_ftell(pb);
  859. put_be32(pb, 0); /* size */
  860. put_tag(pb, "ilst");
  861. mov_write_nam_tag(pb, mov, s);
  862. mov_write_ART_tag(pb, mov, s);
  863. mov_write_wrt_tag(pb, mov, s);
  864. mov_write_alb_tag(pb, mov, s);
  865. mov_write_day_tag(pb, mov, s);
  866. mov_write_too_tag(pb, mov, s);
  867. mov_write_cmt_tag(pb, mov, s);
  868. mov_write_gen_tag(pb, mov, s);
  869. mov_write_trkn_tag(pb, mov, s);
  870. return updateSize(pb, pos);
  871. }
  872. /* iTunes meta data tag */
  873. static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
  874. AVFormatContext *s)
  875. {
  876. int size = 0;
  877. // only save meta tag if required
  878. if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
  879. s->comment[0] || s->genre[0] || s->track ) {
  880. int pos = url_ftell(pb);
  881. put_be32(pb, 0); /* size */
  882. put_tag(pb, "meta");
  883. put_be32(pb, 0);
  884. mov_write_itunes_hdlr_tag(pb, mov, s);
  885. mov_write_ilst_tag(pb, mov, s);
  886. size = updateSize(pb, pos);
  887. }
  888. return size;
  889. }
  890. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
  891. AVFormatContext *s)
  892. {
  893. int pos = url_ftell(pb);
  894. int i;
  895. put_be32(pb, 0); /* size */
  896. put_tag(pb, "udta");
  897. /* iTunes meta data */
  898. mov_write_meta_tag(pb, mov, s);
  899. /* Requirements */
  900. for (i=0; i<MAX_STREAMS; i++) {
  901. if(mov->tracks[i].entry <= 0) continue;
  902. if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
  903. mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
  904. int pos = url_ftell(pb);
  905. put_be32(pb, 0); /* size */
  906. put_tag(pb, "\251req");
  907. put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
  908. put_be16(pb, 0);
  909. put_buffer(pb, "QuickTime 6.0 or greater",
  910. sizeof("QuickTime 6.0 or greater") - 1);
  911. updateSize(pb, pos);
  912. break;
  913. }
  914. }
  915. /* Encoder */
  916. if(!(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
  917. {
  918. int pos = url_ftell(pb);
  919. put_be32(pb, 0); /* size */
  920. put_tag(pb, "\251enc");
  921. put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
  922. put_be16(pb, 0);
  923. put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
  924. updateSize(pb, pos);
  925. }
  926. if( s->title[0] )
  927. {
  928. int pos = url_ftell(pb);
  929. put_be32(pb, 0); /* size */
  930. put_tag(pb, "\251nam");
  931. put_be16(pb, strlen(s->title)); /* string length */
  932. put_be16(pb, 0);
  933. put_buffer(pb, s->title, strlen(s->title));
  934. updateSize(pb, pos);
  935. }
  936. if( s->author[0] )
  937. {
  938. int pos = url_ftell(pb);
  939. put_be32(pb, 0); /* size */
  940. put_tag(pb, /*"\251aut"*/ "\251day" );
  941. put_be16(pb, strlen(s->author)); /* string length */
  942. put_be16(pb, 0);
  943. put_buffer(pb, s->author, strlen(s->author));
  944. updateSize(pb, pos);
  945. }
  946. if( s->comment[0] )
  947. {
  948. int pos = url_ftell(pb);
  949. put_be32(pb, 0); /* size */
  950. put_tag(pb, "\251des");
  951. put_be16(pb, strlen(s->comment)); /* string length */
  952. put_be16(pb, 0);
  953. put_buffer(pb, s->comment, strlen(s->comment));
  954. updateSize(pb, pos);
  955. }
  956. return updateSize(pb, pos);
  957. }
  958. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  959. AVFormatContext *s)
  960. {
  961. int pos, i;
  962. pos = url_ftell(pb);
  963. put_be32(pb, 0); /* size placeholder*/
  964. put_tag(pb, "moov");
  965. mov->timescale = globalTimescale;
  966. for (i=0; i<MAX_STREAMS; i++) {
  967. if(mov->tracks[i].entry <= 0) continue;
  968. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  969. mov->tracks[i].timescale = mov->tracks[i].enc->frame_rate;
  970. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_rate_base;
  971. }
  972. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  973. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  974. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  975. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  976. mov->tracks[i].timescale = 8000;
  977. }
  978. else {
  979. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  980. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  981. }
  982. }
  983. mov->tracks[i].trackDuration =
  984. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  985. mov->tracks[i].time = mov->time;
  986. mov->tracks[i].trackID = i+1;
  987. }
  988. mov_write_mvhd_tag(pb, mov);
  989. //mov_write_iods_tag(pb, mov);
  990. for (i=0; i<MAX_STREAMS; i++) {
  991. if(mov->tracks[i].entry > 0) {
  992. mov_write_trak_tag(pb, &(mov->tracks[i]));
  993. }
  994. }
  995. mov_write_udta_tag(pb, mov, s);
  996. return updateSize(pb, pos);
  997. }
  998. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  999. {
  1000. mov->mdat_pos = url_ftell(pb);
  1001. put_be32(pb, 0); /* size placeholder*/
  1002. put_tag(pb, "mdat");
  1003. return 0;
  1004. }
  1005. /* TODO: This needs to be more general */
  1006. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  1007. {
  1008. MOVContext *mov = s->priv_data;
  1009. put_be32(pb, 0x14 ); /* size */
  1010. put_tag(pb, "ftyp");
  1011. if ( mov->mode == MODE_3GP )
  1012. put_tag(pb, "3gp4");
  1013. else
  1014. put_tag(pb, "isom");
  1015. put_be32(pb, 0x200 );
  1016. if ( mov->mode == MODE_3GP )
  1017. put_tag(pb, "3gp4");
  1018. else
  1019. put_tag(pb, "mp41");
  1020. return 0x14;
  1021. }
  1022. static int mov_write_header(AVFormatContext *s)
  1023. {
  1024. ByteIOContext *pb = &s->pb;
  1025. MOVContext *mov = s->priv_data;
  1026. int i;
  1027. for(i=0; i<s->nb_streams; i++){
  1028. AVCodecContext *c= &s->streams[i]->codec;
  1029. if (c->codec_type == CODEC_TYPE_VIDEO){
  1030. if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
  1031. if(!codec_get_tag(codec_bmp_tags, c->codec_id))
  1032. return -1;
  1033. else
  1034. av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
  1035. }
  1036. }else if(c->codec_type == CODEC_TYPE_AUDIO){
  1037. if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
  1038. if(!codec_get_tag(codec_wav_tags, c->codec_id))
  1039. return -1;
  1040. else
  1041. av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
  1042. }
  1043. }
  1044. }
  1045. /* Default mode == MP4 */
  1046. mov->mode = MODE_MP4;
  1047. if (s->oformat != NULL) {
  1048. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  1049. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  1050. if ( mov->mode == MODE_3GP || mov->mode == MODE_MP4 )
  1051. mov_write_ftyp_tag(pb,s);
  1052. }
  1053. for (i=0; i<MAX_STREAMS; i++) {
  1054. mov->tracks[i].mode = mov->mode;
  1055. }
  1056. put_flush_packet(pb);
  1057. return 0;
  1058. }
  1059. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  1060. {
  1061. MOVContext *mov = s->priv_data;
  1062. ByteIOContext *pb = &s->pb;
  1063. AVCodecContext *enc = &s->streams[pkt->stream_index]->codec;
  1064. MOVTrack* trk = &mov->tracks[pkt->stream_index];
  1065. int cl, id;
  1066. unsigned int samplesInChunk = 0;
  1067. int size= pkt->size;
  1068. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  1069. if (!size) return 0; /* Discard 0 sized packets */
  1070. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  1071. samplesInChunk = 1;
  1072. }
  1073. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  1074. if( enc->codec_id == CODEC_ID_AMR_NB) {
  1075. /* We must find out how many AMR blocks there are in one packet */
  1076. static uint16_t packed_size[16] =
  1077. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  1078. int len = 0;
  1079. while (len < size && samplesInChunk < 100) {
  1080. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  1081. samplesInChunk++;
  1082. }
  1083. }
  1084. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  1085. samplesInChunk = size/enc->channels;
  1086. }
  1087. else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
  1088. samplesInChunk = size/(2*enc->channels);
  1089. }
  1090. else {
  1091. samplesInChunk = 1;
  1092. }
  1093. }
  1094. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  1095. && trk->vosLen == 0) {
  1096. // assert(enc->extradata_size);
  1097. trk->vosLen = enc->extradata_size;
  1098. trk->vosData = av_malloc(trk->vosLen);
  1099. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  1100. }
  1101. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  1102. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  1103. if (trk->ents_allocated <= trk->entry) {
  1104. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  1105. if (!trk->cluster)
  1106. return -1;
  1107. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  1108. if (!trk->cluster[cl])
  1109. return -1;
  1110. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  1111. }
  1112. if (mov->mdat_written == 0) {
  1113. mov_write_mdat_tag(pb, mov);
  1114. mov->mdat_written = 1;
  1115. mov->time = s->timestamp;
  1116. }
  1117. trk->cluster[cl][id].pos = url_ftell(pb);
  1118. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  1119. trk->cluster[cl][id].size = size;
  1120. trk->cluster[cl][id].entries = samplesInChunk;
  1121. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  1122. trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  1123. if(trk->cluster[cl][id].key_frame)
  1124. trk->hasKeyframes = 1;
  1125. }
  1126. trk->enc = enc;
  1127. trk->entry++;
  1128. trk->sampleCount += samplesInChunk;
  1129. trk->mdat_size += size;
  1130. put_buffer(pb, pkt->data, size);
  1131. put_flush_packet(pb);
  1132. return 0;
  1133. }
  1134. static int mov_write_trailer(AVFormatContext *s)
  1135. {
  1136. MOVContext *mov = s->priv_data;
  1137. ByteIOContext *pb = &s->pb;
  1138. int res = 0;
  1139. int i, j;
  1140. offset_t moov_pos = url_ftell(pb);
  1141. /* Write size of mdat tag */
  1142. for (i=0, j=0; i<MAX_STREAMS; i++) {
  1143. if(mov->tracks[i].ents_allocated > 0) {
  1144. j += mov->tracks[i].mdat_size;
  1145. }
  1146. }
  1147. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  1148. put_be32(pb, j+8);
  1149. url_fseek(pb, moov_pos, SEEK_SET);
  1150. mov_write_moov_tag(pb, mov, s);
  1151. for (i=0; i<MAX_STREAMS; i++) {
  1152. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  1153. av_free(mov->tracks[i].cluster[j]);
  1154. }
  1155. av_free(mov->tracks[i].cluster);
  1156. if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
  1157. mov->tracks[i].cluster = NULL;
  1158. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  1159. }
  1160. put_flush_packet(pb);
  1161. return res;
  1162. }
  1163. static AVOutputFormat mov_oformat = {
  1164. "mov",
  1165. "mov format",
  1166. NULL,
  1167. "mov",
  1168. sizeof(MOVContext),
  1169. CODEC_ID_AAC,
  1170. CODEC_ID_MPEG4,
  1171. mov_write_header,
  1172. mov_write_packet,
  1173. mov_write_trailer,
  1174. };
  1175. static AVOutputFormat _3gp_oformat = {
  1176. "3gp",
  1177. "3gp format",
  1178. NULL,
  1179. "3gp",
  1180. sizeof(MOVContext),
  1181. CODEC_ID_AMR_NB,
  1182. CODEC_ID_H263,
  1183. mov_write_header,
  1184. mov_write_packet,
  1185. mov_write_trailer,
  1186. };
  1187. static AVOutputFormat mp4_oformat = {
  1188. "mp4",
  1189. "mp4 format",
  1190. "application/mp4",
  1191. "mp4,m4a",
  1192. sizeof(MOVContext),
  1193. CODEC_ID_AAC,
  1194. CODEC_ID_MPEG4,
  1195. mov_write_header,
  1196. mov_write_packet,
  1197. mov_write_trailer,
  1198. };
  1199. int movenc_init(void)
  1200. {
  1201. av_register_output_format(&mov_oformat);
  1202. av_register_output_format(&_3gp_oformat);
  1203. av_register_output_format(&mp4_oformat);
  1204. return 0;
  1205. }