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.

1596 lines
48KB

  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. #define MODE_PSP 3 // example working PSP command line:
  31. // ffmpeg -i testinput.avi -f psp -r 14.985 -s 320x240 -b 768 -ar 24000 -ab 32 M4V00001.MP4
  32. #define MODE_3G2 4
  33. typedef struct MOVIentry {
  34. unsigned int flags, size;
  35. uint64_t pos;
  36. unsigned int samplesInChunk;
  37. char key_frame;
  38. unsigned int entries;
  39. } MOVIentry;
  40. typedef struct MOVIndex {
  41. int mode;
  42. int entry;
  43. uint64_t mdat_size;
  44. int ents_allocated;
  45. long timescale;
  46. long time;
  47. long trackDuration;
  48. long sampleCount;
  49. long sampleDuration;
  50. int hasKeyframes;
  51. int language;
  52. int trackID;
  53. AVCodecContext *enc;
  54. int vosLen;
  55. uint8_t *vosData;
  56. MOVIentry** cluster;
  57. } MOVTrack;
  58. typedef struct MOVContext {
  59. int mode;
  60. long time;
  61. int nb_streams;
  62. int mdat_written;
  63. offset_t mdat_pos;
  64. long timescale;
  65. MOVTrack tracks[MAX_STREAMS];
  66. } MOVContext;
  67. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track);
  68. /* output language code from iso639 language name */
  69. extern int ff_mov_iso639_to_lang(const char *lang, int mp4);
  70. const CodecTag ff_mov_obj_type[] = {
  71. { CODEC_ID_MPEG4 , 32 },
  72. { CODEC_ID_AAC , 64 },
  73. { CODEC_ID_MPEG1VIDEO, 106 },
  74. { CODEC_ID_MPEG2VIDEO, 96 },//mpeg2 profiles
  75. { CODEC_ID_MP2 , 107 },//FIXME mpeg2 mpeg audio -> 105
  76. { CODEC_ID_MP3 , 107 },//FIXME mpeg2 mpeg audio -> 105
  77. { CODEC_ID_H264 , 33 },
  78. { CODEC_ID_H263 , 242 },
  79. { CODEC_ID_H261 , 243 },
  80. { CODEC_ID_MJPEG , 108 },
  81. { CODEC_ID_PCM_S16LE , 224 },
  82. { CODEC_ID_VORBIS , 225 },
  83. { CODEC_ID_AC3 , 226 },
  84. { CODEC_ID_PCM_ALAW , 227 },
  85. { CODEC_ID_PCM_MULAW , 228 },
  86. { CODEC_ID_PCM_S16BE , 230 },
  87. { 0,0 },
  88. };
  89. //FIXME supprt 64bit varaint with wide placeholders
  90. static offset_t updateSize (ByteIOContext *pb, offset_t pos)
  91. {
  92. offset_t curpos = url_ftell(pb);
  93. url_fseek(pb, pos, SEEK_SET);
  94. put_be32(pb, curpos - pos); /* rewrite size */
  95. url_fseek(pb, curpos, SEEK_SET);
  96. return curpos - pos;
  97. }
  98. /* Chunk offset atom */
  99. static int mov_write_stco_tag(ByteIOContext *pb, MOVTrack* track)
  100. {
  101. int i;
  102. int mode64 = 0; // use 32 bit size variant if possible
  103. offset_t pos = url_ftell(pb);
  104. put_be32(pb, 0); /* size */
  105. if (pos > UINT32_MAX) {
  106. mode64 = 1;
  107. put_tag(pb, "co64");
  108. } else
  109. put_tag(pb, "stco");
  110. put_be32(pb, 0); /* version & flags */
  111. put_be32(pb, track->entry); /* entry count */
  112. for (i=0; i<track->entry; i++) {
  113. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  114. int id = i % MOV_INDEX_CLUSTER_SIZE;
  115. if(mode64 == 1)
  116. put_be64(pb, track->cluster[cl][id].pos);
  117. else
  118. put_be32(pb, track->cluster[cl][id].pos);
  119. }
  120. return updateSize (pb, pos);
  121. }
  122. /* Sample size atom */
  123. static int mov_write_stsz_tag(ByteIOContext *pb, MOVTrack* track)
  124. {
  125. int equalChunks = 1;
  126. int i, j, entries = 0, tst = -1, oldtst = -1;
  127. offset_t pos = url_ftell(pb);
  128. put_be32(pb, 0); /* size */
  129. put_tag(pb, "stsz");
  130. put_be32(pb, 0); /* version & flags */
  131. for (i=0; i<track->entry; i++) {
  132. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  133. int id = i % MOV_INDEX_CLUSTER_SIZE;
  134. tst = track->cluster[cl][id].size/track->cluster[cl][id].entries;
  135. if(oldtst != -1 && tst != oldtst) {
  136. equalChunks = 0;
  137. }
  138. oldtst = tst;
  139. entries += track->cluster[cl][id].entries;
  140. }
  141. if (equalChunks) {
  142. int sSize = track->cluster[0][0].size/track->cluster[0][0].entries;
  143. put_be32(pb, sSize); // sample size
  144. put_be32(pb, entries); // sample count
  145. }
  146. else {
  147. put_be32(pb, 0); // sample size
  148. put_be32(pb, entries); // sample count
  149. for (i=0; i<track->entry; i++) {
  150. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  151. int id = i % MOV_INDEX_CLUSTER_SIZE;
  152. for ( j=0; j<track->cluster[cl][id].entries; j++) {
  153. put_be32(pb, track->cluster[cl][id].size /
  154. track->cluster[cl][id].entries);
  155. }
  156. }
  157. }
  158. return updateSize (pb, pos);
  159. }
  160. /* Sample to chunk atom */
  161. static int mov_write_stsc_tag(ByteIOContext *pb, MOVTrack* track)
  162. {
  163. int index = 0, oldval = -1, i;
  164. offset_t entryPos, curpos;
  165. offset_t pos = url_ftell(pb);
  166. put_be32(pb, 0); /* size */
  167. put_tag(pb, "stsc");
  168. put_be32(pb, 0); // version & flags
  169. entryPos = url_ftell(pb);
  170. put_be32(pb, track->entry); // entry count
  171. for (i=0; i<track->entry; i++) {
  172. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  173. int id = i % MOV_INDEX_CLUSTER_SIZE;
  174. if(oldval != track->cluster[cl][id].samplesInChunk)
  175. {
  176. put_be32(pb, i+1); // first chunk
  177. put_be32(pb, track->cluster[cl][id].samplesInChunk); // samples per chunk
  178. put_be32(pb, 0x1); // sample description index
  179. oldval = track->cluster[cl][id].samplesInChunk;
  180. index++;
  181. }
  182. }
  183. curpos = url_ftell(pb);
  184. url_fseek(pb, entryPos, SEEK_SET);
  185. put_be32(pb, index); // rewrite size
  186. url_fseek(pb, curpos, SEEK_SET);
  187. return updateSize (pb, pos);
  188. }
  189. /* Sync sample atom */
  190. static int mov_write_stss_tag(ByteIOContext *pb, MOVTrack* track)
  191. {
  192. offset_t curpos, entryPos;
  193. int i, index = 0;
  194. offset_t pos = url_ftell(pb);
  195. put_be32(pb, 0); // size
  196. put_tag(pb, "stss");
  197. put_be32(pb, 0); // version & flags
  198. entryPos = url_ftell(pb);
  199. put_be32(pb, track->entry); // entry count
  200. for (i=0; i<track->entry; i++) {
  201. int cl = i / MOV_INDEX_CLUSTER_SIZE;
  202. int id = i % MOV_INDEX_CLUSTER_SIZE;
  203. if(track->cluster[cl][id].key_frame == 1) {
  204. put_be32(pb, i+1);
  205. index++;
  206. }
  207. }
  208. curpos = url_ftell(pb);
  209. url_fseek(pb, entryPos, SEEK_SET);
  210. put_be32(pb, index); // rewrite size
  211. url_fseek(pb, curpos, SEEK_SET);
  212. return updateSize (pb, pos);
  213. }
  214. static int mov_write_damr_tag(ByteIOContext *pb)
  215. {
  216. put_be32(pb, 0x11); /* size */
  217. put_tag(pb, "damr");
  218. put_tag(pb, "FFMP");
  219. put_byte(pb, 0);
  220. put_be16(pb, 0x80); /* Mode set (all modes for AMR_NB) */
  221. put_be16(pb, 0xa); /* Mode change period (no restriction) */
  222. //put_be16(pb, 0x81ff); /* Mode set (all modes for AMR_NB) */
  223. //put_be16(pb, 1); /* Mode change period (no restriction) */
  224. return 0x11;
  225. }
  226. static int mov_write_wave_tag(ByteIOContext *pb, MOVTrack* track)
  227. {
  228. offset_t pos = url_ftell(pb);
  229. put_be32(pb, 0); /* size */
  230. put_tag(pb, "wave");
  231. put_be32(pb, 12); /* size */
  232. put_tag(pb, "frma");
  233. put_tag(pb, "mp4a");
  234. put_be32(pb, 12); /* size */
  235. put_tag(pb, "mp4a");
  236. put_be32(pb, 0);
  237. mov_write_esds_tag(pb, track);
  238. put_be32(pb, 12); /* size */
  239. put_tag(pb, "srcq");
  240. put_be32(pb, 0x40);
  241. put_be32(pb, 8); /* size */
  242. put_be32(pb, 0); /* null tag */
  243. return updateSize (pb, pos);
  244. }
  245. static const CodecTag codec_movaudio_tags[] = {
  246. { CODEC_ID_PCM_MULAW, MKTAG('u', 'l', 'a', 'w') },
  247. { CODEC_ID_PCM_ALAW, MKTAG('a', 'l', 'a', 'w') },
  248. { CODEC_ID_ADPCM_IMA_QT, MKTAG('i', 'm', 'a', '4') },
  249. { CODEC_ID_MACE3, MKTAG('M', 'A', 'C', '3') },
  250. { CODEC_ID_MACE6, MKTAG('M', 'A', 'C', '6') },
  251. { CODEC_ID_AAC, MKTAG('m', 'p', '4', 'a') },
  252. { CODEC_ID_AMR_NB, MKTAG('s', 'a', 'm', 'r') },
  253. { CODEC_ID_AMR_WB, MKTAG('s', 'a', 'w', 'b') },
  254. { CODEC_ID_PCM_S16BE, MKTAG('t', 'w', 'o', 's') },
  255. { CODEC_ID_PCM_S16LE, MKTAG('s', 'o', 'w', 't') },
  256. { CODEC_ID_MP3, MKTAG('.', 'm', 'p', '3') },
  257. { 0, 0 },
  258. };
  259. static int mov_write_audio_tag(ByteIOContext *pb, MOVTrack* track)
  260. {
  261. offset_t pos = url_ftell(pb);
  262. int tag;
  263. put_be32(pb, 0); /* size */
  264. tag = track->enc->codec_tag;
  265. if (!tag)
  266. tag = codec_get_tag(codec_movaudio_tags, track->enc->codec_id);
  267. // if no mac fcc found, try with Microsoft tags
  268. if (!tag)
  269. {
  270. int tmp = codec_get_tag(codec_wav_tags, track->enc->codec_id);
  271. tag = MKTAG('m', 's', ((tmp >> 8) & 0xff), (tmp & 0xff));
  272. }
  273. put_le32(pb, tag); // store it byteswapped
  274. put_be32(pb, 0); /* Reserved */
  275. put_be16(pb, 0); /* Reserved */
  276. put_be16(pb, 1); /* Data-reference index, XXX == 1 */
  277. /* SoundDescription */
  278. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  279. put_be16(pb, 1); /* Version 1 */
  280. else
  281. put_be16(pb, 0); /* Version 0 */
  282. put_be16(pb, 0); /* Revision level */
  283. put_be32(pb, 0); /* Reserved */
  284. put_be16(pb, track->enc->channels); /* Number of channels */
  285. /* TODO: Currently hard-coded to 16-bit, there doesn't seem
  286. to be a good way to get number of bits of audio */
  287. put_be16(pb, 0x10); /* Reserved */
  288. if(track->enc->codec_id == CODEC_ID_AAC ||
  289. track->enc->codec_id == CODEC_ID_MP3)
  290. {
  291. put_be16(pb, 0xfffe); /* compression ID (vbr)*/
  292. }
  293. else
  294. {
  295. put_be16(pb, 0); /* compression ID (= 0) */
  296. }
  297. put_be16(pb, 0); /* packet size (= 0) */
  298. put_be16(pb, track->timescale); /* Time scale */
  299. put_be16(pb, 0); /* Reserved */
  300. if(track->mode == MODE_MOV && track->enc->codec_id == CODEC_ID_AAC)
  301. {
  302. /* SoundDescription V1 extended info */
  303. put_be32(pb, track->enc->frame_size); /* Samples per packet */
  304. put_be32(pb, 1536); /* Bytes per packet */
  305. put_be32(pb, 2); /* Bytes per frame */
  306. put_be32(pb, 2); /* Bytes per sample */
  307. }
  308. if(track->enc->codec_id == CODEC_ID_AAC) {
  309. if( track->mode == MODE_MOV ) mov_write_wave_tag(pb, track);
  310. else mov_write_esds_tag(pb, track);
  311. }
  312. if(track->enc->codec_id == CODEC_ID_AMR_NB)
  313. mov_write_damr_tag(pb);
  314. return updateSize (pb, pos);
  315. }
  316. static int mov_write_d263_tag(ByteIOContext *pb)
  317. {
  318. put_be32(pb, 0xf); /* size */
  319. put_tag(pb, "d263");
  320. put_tag(pb, "FFMP");
  321. put_be16(pb, 0x0a);
  322. put_byte(pb, 0);
  323. return 0xf;
  324. }
  325. /* TODO: No idea about these values */
  326. static int mov_write_svq3_tag(ByteIOContext *pb)
  327. {
  328. put_be32(pb, 0x15);
  329. put_tag(pb, "SMI ");
  330. put_tag(pb, "SEQH");
  331. put_be32(pb, 0x5);
  332. put_be32(pb, 0xe2c0211d);
  333. put_be32(pb, 0xc0000000);
  334. put_byte(pb, 0);
  335. return 0x15;
  336. }
  337. static unsigned int descrLength(unsigned int len)
  338. {
  339. if (len < 0x00000080)
  340. return 2 + len;
  341. else if (len < 0x00004000)
  342. return 3 + len;
  343. else if(len < 0x00200000)
  344. return 4 + len;
  345. else
  346. return 5 + len;
  347. }
  348. static void putDescr(ByteIOContext *pb, int tag, int size)
  349. {
  350. uint32_t len;
  351. uint8_t vals[4];
  352. len = size;
  353. vals[3] = (uint8_t)(len & 0x7f);
  354. len >>= 7;
  355. vals[2] = (uint8_t)((len & 0x7f) | 0x80);
  356. len >>= 7;
  357. vals[1] = (uint8_t)((len & 0x7f) | 0x80);
  358. len >>= 7;
  359. vals[0] = (uint8_t)((len & 0x7f) | 0x80);
  360. put_byte(pb, tag); // DescriptorTag
  361. if (size < 0x00000080)
  362. {
  363. put_byte(pb, vals[3]);
  364. }
  365. else if (size < 0x00004000)
  366. {
  367. put_byte(pb, vals[2]);
  368. put_byte(pb, vals[3]);
  369. }
  370. else if (size < 0x00200000)
  371. {
  372. put_byte(pb, vals[1]);
  373. put_byte(pb, vals[2]);
  374. put_byte(pb, vals[3]);
  375. }
  376. else if (size < 0x10000000)
  377. {
  378. put_byte(pb, vals[0]);
  379. put_byte(pb, vals[1]);
  380. put_byte(pb, vals[2]);
  381. put_byte(pb, vals[3]);
  382. }
  383. }
  384. static int mov_write_esds_tag(ByteIOContext *pb, MOVTrack* track) // Basic
  385. {
  386. int decoderSpecificInfoLen;
  387. offset_t pos = url_ftell(pb);
  388. void *vosDataBackup=track->vosData;
  389. int vosLenBackup=track->vosLen;
  390. // we should be able to have these passed in, via vosData, then we wouldn't need to attack this routine at all
  391. static const char PSPAACData[]={0x13,0x10};
  392. static const char PSPMP4Data[]={0x00,0x00,0x01,0xB0,0x03,0x00,0x00,0x01,0xB5,0x09,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x20,0x00,0x84,0x5D,0x4C,0x28,0x50,0x20,0xF0,0xA3,0x1F };
  393. if (track->mode == MODE_PSP) // fails on psp if this is not here
  394. {
  395. if (track->enc->codec_id == CODEC_ID_AAC)
  396. {
  397. track->vosLen = 2;
  398. track->vosData = (uint8_t *) PSPAACData;
  399. }
  400. if (track->enc->codec_id == CODEC_ID_MPEG4)
  401. {
  402. track->vosLen = 28;
  403. track->vosData = (uint8_t *) PSPMP4Data;
  404. }
  405. }
  406. decoderSpecificInfoLen = track->vosLen ? descrLength(track->vosLen):0;
  407. put_be32(pb, 0); // size
  408. put_tag(pb, "esds");
  409. put_be32(pb, 0); // Version
  410. // ES descriptor
  411. putDescr(pb, 0x03, 3 + descrLength(13 + decoderSpecificInfoLen) +
  412. descrLength(1));
  413. put_be16(pb, track->trackID);
  414. put_byte(pb, 0x00); // flags (= no flags)
  415. // DecoderConfig descriptor
  416. putDescr(pb, 0x04, 13 + decoderSpecificInfoLen);
  417. // Object type indication
  418. put_byte(pb, codec_get_tag(ff_mov_obj_type, track->enc->codec_id));
  419. // the following fields is made of 6 bits to identify the streamtype (4 for video, 5 for audio)
  420. // plus 1 bit to indicate upstream and 1 bit set to 1 (reserved)
  421. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  422. put_byte(pb, 0x15); // flags (= Audiostream)
  423. else
  424. put_byte(pb, 0x11); // flags (= Visualstream)
  425. put_byte(pb, track->enc->rc_buffer_size>>(3+16)); // Buffersize DB (24 bits)
  426. put_be16(pb, (track->enc->rc_buffer_size>>3)&0xFFFF); // Buffersize DB
  427. put_be32(pb, FFMAX(track->enc->bit_rate, track->enc->rc_max_rate)); // maxbitrate (FIXME should be max rate in any 1 sec window)
  428. if(track->enc->rc_max_rate != track->enc->rc_min_rate || track->enc->rc_min_rate==0)
  429. put_be32(pb, 0); // vbr
  430. else
  431. put_be32(pb, track->enc->rc_max_rate); // avg bitrate
  432. if (track->vosLen)
  433. {
  434. // DecoderSpecific info descriptor
  435. putDescr(pb, 0x05, track->vosLen);
  436. put_buffer(pb, track->vosData, track->vosLen);
  437. }
  438. track->vosData = vosDataBackup;
  439. track->vosLen = vosLenBackup;
  440. // SL descriptor
  441. putDescr(pb, 0x06, 1);
  442. put_byte(pb, 0x02);
  443. return updateSize (pb, pos);
  444. }
  445. static const CodecTag codec_movvideo_tags[] = {
  446. { CODEC_ID_SVQ1, MKTAG('S', 'V', 'Q', '1') },
  447. { CODEC_ID_SVQ3, MKTAG('S', 'V', 'Q', '3') },
  448. { CODEC_ID_MPEG4, MKTAG('m', 'p', '4', 'v') },
  449. { CODEC_ID_H263, MKTAG('s', '2', '6', '3') },
  450. { CODEC_ID_H264, MKTAG('a', 'v', 'c', '1') },
  451. { CODEC_ID_DVVIDEO, MKTAG('d', 'v', 'c', ' ') },
  452. { 0, 0 },
  453. };
  454. static int mov_write_video_tag(ByteIOContext *pb, MOVTrack* track)
  455. {
  456. offset_t pos = url_ftell(pb);
  457. char compressor_name[32];
  458. int tag;
  459. put_be32(pb, 0); /* size */
  460. tag = track->enc->codec_tag;
  461. if (!tag)
  462. tag = codec_get_tag(codec_movvideo_tags, track->enc->codec_id);
  463. // if no mac fcc found, try with Microsoft tags
  464. if (!tag)
  465. tag = codec_get_tag(codec_bmp_tags, track->enc->codec_id);
  466. put_le32(pb, tag); // store it byteswapped
  467. put_be32(pb, 0); /* Reserved */
  468. put_be16(pb, 0); /* Reserved */
  469. put_be16(pb, 1); /* Data-reference index */
  470. put_be16(pb, 0); /* Codec stream version */
  471. put_be16(pb, 0); /* Codec stream revision (=0) */
  472. put_tag(pb, "FFMP"); /* Vendor */
  473. if(track->enc->codec_id == CODEC_ID_RAWVIDEO) {
  474. put_be32(pb, 0); /* Temporal Quality */
  475. put_be32(pb, 0x400); /* Spatial Quality = lossless*/
  476. } else {
  477. put_be32(pb, 0x200); /* Temporal Quality = normal */
  478. put_be32(pb, 0x200); /* Spatial Quality = normal */
  479. }
  480. put_be16(pb, track->enc->width); /* Video width */
  481. put_be16(pb, track->enc->height); /* Video height */
  482. put_be32(pb, 0x00480000); /* Horizontal resolution 72dpi */
  483. put_be32(pb, 0x00480000); /* Vertical resolution 72dpi */
  484. put_be32(pb, 0); /* Data size (= 0) */
  485. put_be16(pb, 1); /* Frame count (= 1) */
  486. memset(compressor_name,0,32);
  487. if (track->enc->codec && track->enc->codec->name)
  488. strncpy(compressor_name,track->enc->codec->name,31);
  489. put_byte(pb, strlen(compressor_name));
  490. put_buffer(pb, compressor_name, 31);
  491. put_be16(pb, 0x18); /* Reserved */
  492. put_be16(pb, 0xffff); /* Reserved */
  493. if(track->enc->codec_id == CODEC_ID_MPEG4)
  494. mov_write_esds_tag(pb, track);
  495. else if(track->enc->codec_id == CODEC_ID_H263)
  496. mov_write_d263_tag(pb);
  497. else if(track->enc->codec_id == CODEC_ID_SVQ3)
  498. mov_write_svq3_tag(pb);
  499. return updateSize (pb, pos);
  500. }
  501. static int mov_write_stsd_tag(ByteIOContext *pb, MOVTrack* track)
  502. {
  503. offset_t pos = url_ftell(pb);
  504. put_be32(pb, 0); /* size */
  505. put_tag(pb, "stsd");
  506. put_be32(pb, 0); /* version & flags */
  507. put_be32(pb, 1); /* entry count */
  508. if (track->enc->codec_type == CODEC_TYPE_VIDEO)
  509. mov_write_video_tag(pb, track);
  510. else if (track->enc->codec_type == CODEC_TYPE_AUDIO)
  511. mov_write_audio_tag(pb, track);
  512. return updateSize(pb, pos);
  513. }
  514. /* TODO: */
  515. /* Time to sample atom */
  516. static int mov_write_stts_tag(ByteIOContext *pb, MOVTrack* track)
  517. {
  518. put_be32(pb, 0x18); /* size */
  519. put_tag(pb, "stts");
  520. put_be32(pb, 0); /* version & flags */
  521. put_be32(pb, 1); /* entry count */
  522. put_be32(pb, track->sampleCount); /* sample count */
  523. put_be32(pb, track->sampleDuration); /* sample duration */
  524. return 0x18;
  525. }
  526. static int mov_write_dref_tag(ByteIOContext *pb)
  527. {
  528. put_be32(pb, 28); /* size */
  529. put_tag(pb, "dref");
  530. put_be32(pb, 0); /* version & flags */
  531. put_be32(pb, 1); /* entry count */
  532. put_be32(pb, 0xc); /* size */
  533. put_tag(pb, "url ");
  534. put_be32(pb, 1); /* version & flags */
  535. return 28;
  536. }
  537. static int mov_write_stbl_tag(ByteIOContext *pb, MOVTrack* track)
  538. {
  539. offset_t pos = url_ftell(pb);
  540. put_be32(pb, 0); /* size */
  541. put_tag(pb, "stbl");
  542. mov_write_stsd_tag(pb, track);
  543. mov_write_stts_tag(pb, track);
  544. if (track->enc->codec_type == CODEC_TYPE_VIDEO &&
  545. track->hasKeyframes)
  546. mov_write_stss_tag(pb, track);
  547. mov_write_stsc_tag(pb, track);
  548. mov_write_stsz_tag(pb, track);
  549. mov_write_stco_tag(pb, track);
  550. return updateSize(pb, pos);
  551. }
  552. static int mov_write_dinf_tag(ByteIOContext *pb)
  553. {
  554. offset_t pos = url_ftell(pb);
  555. put_be32(pb, 0); /* size */
  556. put_tag(pb, "dinf");
  557. mov_write_dref_tag(pb);
  558. return updateSize(pb, pos);
  559. }
  560. static int mov_write_smhd_tag(ByteIOContext *pb)
  561. {
  562. put_be32(pb, 16); /* size */
  563. put_tag(pb, "smhd");
  564. put_be32(pb, 0); /* version & flags */
  565. put_be16(pb, 0); /* reserved (balance, normally = 0) */
  566. put_be16(pb, 0); /* reserved */
  567. return 16;
  568. }
  569. static int mov_write_vmhd_tag(ByteIOContext *pb)
  570. {
  571. put_be32(pb, 0x14); /* size (always 0x14) */
  572. put_tag(pb, "vmhd");
  573. put_be32(pb, 0x01); /* version & flags */
  574. put_be64(pb, 0); /* reserved (graphics mode = copy) */
  575. return 0x14;
  576. }
  577. static int mov_write_hdlr_tag(ByteIOContext *pb, MOVTrack* track)
  578. {
  579. char *descr, *hdlr, *hdlr_type;
  580. offset_t pos = url_ftell(pb);
  581. if (!track) { /* no media --> data handler */
  582. hdlr = "dhlr";
  583. hdlr_type = "url ";
  584. descr = "DataHandler";
  585. } else {
  586. hdlr = (track->mode == MODE_MOV) ? "mhlr" : "\0\0\0\0";
  587. if (track->enc->codec_type == CODEC_TYPE_VIDEO) {
  588. hdlr_type = "vide";
  589. descr = "VideoHandler";
  590. } else {
  591. hdlr_type = "soun";
  592. descr = "SoundHandler";
  593. }
  594. }
  595. put_be32(pb, 0); /* size */
  596. put_tag(pb, "hdlr");
  597. put_be32(pb, 0); /* Version & flags */
  598. put_buffer(pb, hdlr, 4); /* handler */
  599. put_tag(pb, hdlr_type); /* handler type */
  600. put_be32(pb ,0); /* reserved */
  601. put_be32(pb ,0); /* reserved */
  602. put_be32(pb ,0); /* reserved */
  603. put_byte(pb, strlen(descr)); /* string counter */
  604. put_buffer(pb, descr, strlen(descr)); /* handler description */
  605. return updateSize(pb, pos);
  606. }
  607. static int mov_write_minf_tag(ByteIOContext *pb, MOVTrack* track)
  608. {
  609. offset_t pos = url_ftell(pb);
  610. put_be32(pb, 0); /* size */
  611. put_tag(pb, "minf");
  612. if(track->enc->codec_type == CODEC_TYPE_VIDEO)
  613. mov_write_vmhd_tag(pb);
  614. else
  615. mov_write_smhd_tag(pb);
  616. if (track->mode == MODE_MOV) /* FIXME: Why do it for MODE_MOV only ? */
  617. mov_write_hdlr_tag(pb, NULL);
  618. mov_write_dinf_tag(pb);
  619. mov_write_stbl_tag(pb, track);
  620. return updateSize(pb, pos);
  621. }
  622. static int mov_write_mdhd_tag(ByteIOContext *pb, MOVTrack* track)
  623. {
  624. put_be32(pb, 32); /* size */
  625. put_tag(pb, "mdhd");
  626. put_be32(pb, 0); /* Version & flags */
  627. put_be32(pb, track->time); /* creation time */
  628. put_be32(pb, track->time); /* modification time */
  629. put_be32(pb, track->timescale); /* time scale (sample rate for audio) */
  630. put_be32(pb, track->trackDuration); /* duration */
  631. put_be16(pb, track->language); /* language */
  632. put_be16(pb, 0); /* reserved (quality) */
  633. return 32;
  634. }
  635. static int mov_write_mdia_tag(ByteIOContext *pb, MOVTrack* track)
  636. {
  637. offset_t pos = url_ftell(pb);
  638. put_be32(pb, 0); /* size */
  639. put_tag(pb, "mdia");
  640. mov_write_mdhd_tag(pb, track);
  641. mov_write_hdlr_tag(pb, track);
  642. mov_write_minf_tag(pb, track);
  643. return updateSize(pb, pos);
  644. }
  645. static int mov_write_tkhd_tag(ByteIOContext *pb, MOVTrack* track)
  646. {
  647. put_be32(pb, 0x5c); /* size (always 0x5c) */
  648. put_tag(pb, "tkhd");
  649. put_be32(pb, 0xf); /* version & flags (track enabled) */
  650. put_be32(pb, track->time); /* creation time */
  651. put_be32(pb, track->time); /* modification time */
  652. put_be32(pb, track->trackID); /* track-id */
  653. put_be32(pb, 0); /* reserved */
  654. put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration */
  655. put_be32(pb, 0); /* reserved */
  656. put_be32(pb, 0); /* reserved */
  657. put_be32(pb, 0x0); /* reserved (Layer & Alternate group) */
  658. /* Volume, only for audio */
  659. if(track->enc->codec_type == CODEC_TYPE_AUDIO)
  660. put_be16(pb, 0x0100);
  661. else
  662. put_be16(pb, 0);
  663. put_be16(pb, 0); /* reserved */
  664. /* Matrix structure */
  665. put_be32(pb, 0x00010000); /* reserved */
  666. put_be32(pb, 0x0); /* reserved */
  667. put_be32(pb, 0x0); /* reserved */
  668. put_be32(pb, 0x0); /* reserved */
  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, 0x40000000); /* reserved */
  674. /* Track width and height, for visual only */
  675. if(track->enc->codec_type == CODEC_TYPE_VIDEO) {
  676. double sample_aspect_ratio = av_q2d(track->enc->sample_aspect_ratio);
  677. if( !sample_aspect_ratio ) sample_aspect_ratio = 1;
  678. put_be32(pb, sample_aspect_ratio * track->enc->width*0x10000);
  679. put_be32(pb, track->enc->height*0x10000);
  680. }
  681. else {
  682. put_be32(pb, 0);
  683. put_be32(pb, 0);
  684. }
  685. return 0x5c;
  686. }
  687. // This box seems important for the psp playback ... without it the movie seems to hang
  688. static int mov_write_edts_tag(ByteIOContext *pb, MOVTrack *track)
  689. {
  690. put_be32(pb, 0x24); /* size */
  691. put_tag(pb, "edts");
  692. put_be32(pb, 0x1c); /* size */
  693. put_tag(pb, "elst");
  694. put_be32(pb, 0x0);
  695. put_be32(pb, 0x1);
  696. put_be32(pb, av_rescale_rnd(track->trackDuration, globalTimescale, track->timescale, AV_ROUND_UP)); /* duration ... doesn't seem to effect psp */
  697. put_be32(pb, 0x0);
  698. put_be32(pb, 0x00010000);
  699. return 0x24;
  700. }
  701. // goes at the end of each track! ... Critical for PSP playback ("Incompatible data" without it)
  702. static int mov_write_uuid_tag_psp(ByteIOContext *pb, MOVTrack *mov)
  703. {
  704. put_be32(pb, 0x34); /* size ... reports as 28 in mp4box! */
  705. put_tag(pb, "uuid");
  706. put_tag(pb, "USMT");
  707. put_be32(pb, 0x21d24fce);
  708. put_be32(pb, 0xbb88695c);
  709. put_be32(pb, 0xfac9c740);
  710. put_be32(pb, 0x1c); // another size here!
  711. put_tag(pb, "MTDT");
  712. put_be32(pb, 0x00010012);
  713. put_be32(pb, 0x0a);
  714. put_be32(pb, 0x55c40000);
  715. put_be32(pb, 0x1);
  716. put_be32(pb, 0x0);
  717. return 0x34;
  718. }
  719. static int mov_write_trak_tag(ByteIOContext *pb, MOVTrack* track)
  720. {
  721. offset_t pos = url_ftell(pb);
  722. put_be32(pb, 0); /* size */
  723. put_tag(pb, "trak");
  724. mov_write_tkhd_tag(pb, track);
  725. if (track->mode == MODE_PSP)
  726. mov_write_edts_tag(pb, track); // PSP Movies require edts box
  727. mov_write_mdia_tag(pb, track);
  728. if (track->mode == MODE_PSP)
  729. mov_write_uuid_tag_psp(pb,track); // PSP Movies require this uuid box
  730. return updateSize(pb, pos);
  731. }
  732. #if 0
  733. /* TODO: Not sorted out, but not necessary either */
  734. static int mov_write_iods_tag(ByteIOContext *pb, MOVContext *mov)
  735. {
  736. put_be32(pb, 0x15); /* size */
  737. put_tag(pb, "iods");
  738. put_be32(pb, 0); /* version & flags */
  739. put_be16(pb, 0x1007);
  740. put_byte(pb, 0);
  741. put_be16(pb, 0x4fff);
  742. put_be16(pb, 0xfffe);
  743. put_be16(pb, 0x01ff);
  744. return 0x15;
  745. }
  746. #endif
  747. static int mov_write_mvhd_tag(ByteIOContext *pb, MOVContext *mov)
  748. {
  749. int maxTrackID = 1, i;
  750. int64_t maxTrackLenTemp, maxTrackLen = 0;
  751. put_be32(pb, 0x6c); /* size (always 0x6c) */
  752. put_tag(pb, "mvhd");
  753. put_be32(pb, 0); /* version & flags */
  754. put_be32(pb, mov->time); /* creation time */
  755. put_be32(pb, mov->time); /* modification time */
  756. put_be32(pb, mov->timescale); /* timescale */
  757. for (i=0; i<MAX_STREAMS; i++) {
  758. if(mov->tracks[i].entry > 0) {
  759. maxTrackLenTemp = av_rescale_rnd(mov->tracks[i].trackDuration, globalTimescale, mov->tracks[i].timescale, AV_ROUND_UP);
  760. if(maxTrackLen < maxTrackLenTemp)
  761. maxTrackLen = maxTrackLenTemp;
  762. if(maxTrackID < mov->tracks[i].trackID)
  763. maxTrackID = mov->tracks[i].trackID;
  764. }
  765. }
  766. put_be32(pb, maxTrackLen); /* duration of longest track */
  767. put_be32(pb, 0x00010000); /* reserved (preferred rate) 1.0 = normal */
  768. put_be16(pb, 0x0100); /* reserved (preferred volume) 1.0 = normal */
  769. put_be16(pb, 0); /* reserved */
  770. put_be32(pb, 0); /* reserved */
  771. put_be32(pb, 0); /* reserved */
  772. /* Matrix structure */
  773. put_be32(pb, 0x00010000); /* reserved */
  774. put_be32(pb, 0x0); /* reserved */
  775. put_be32(pb, 0x0); /* reserved */
  776. put_be32(pb, 0x0); /* reserved */
  777. put_be32(pb, 0x00010000); /* reserved */
  778. put_be32(pb, 0x0); /* reserved */
  779. put_be32(pb, 0x0); /* reserved */
  780. put_be32(pb, 0x0); /* reserved */
  781. put_be32(pb, 0x40000000); /* reserved */
  782. put_be32(pb, 0); /* reserved (preview time) */
  783. put_be32(pb, 0); /* reserved (preview duration) */
  784. put_be32(pb, 0); /* reserved (poster time) */
  785. put_be32(pb, 0); /* reserved (selection time) */
  786. put_be32(pb, 0); /* reserved (selection duration) */
  787. put_be32(pb, 0); /* reserved (current time) */
  788. put_be32(pb, maxTrackID+1); /* Next track id */
  789. return 0x6c;
  790. }
  791. static int mov_write_itunes_hdlr_tag(ByteIOContext *pb, MOVContext* mov,
  792. AVFormatContext *s)
  793. {
  794. offset_t pos = url_ftell(pb);
  795. put_be32(pb, 0); /* size */
  796. put_tag(pb, "hdlr");
  797. put_be32(pb, 0);
  798. put_be32(pb, 0);
  799. put_tag(pb, "mdir");
  800. put_tag(pb, "appl");
  801. put_be32(pb, 0);
  802. put_be32(pb, 0);
  803. put_be16(pb, 0);
  804. return updateSize(pb, pos);
  805. }
  806. /* helper function to write a data tag with the specified string as data */
  807. static int mov_write_string_data_tag(ByteIOContext *pb, MOVContext* mov,
  808. AVFormatContext *s, const char *data)
  809. {
  810. offset_t pos = url_ftell(pb);
  811. put_be32(pb, 0); /* size */
  812. put_tag(pb, "data");
  813. put_be32(pb, 1);
  814. put_be32(pb, 0);
  815. put_buffer(pb, data, strlen(data));
  816. return updateSize(pb, pos);
  817. }
  818. /* iTunes name of the song/movie */
  819. static int mov_write_nam_tag(ByteIOContext *pb, MOVContext* mov,
  820. AVFormatContext *s)
  821. {
  822. int size = 0;
  823. if ( s->title[0] ) {
  824. offset_t pos = url_ftell(pb);
  825. put_be32(pb, 0); /* size */
  826. put_tag(pb, "\251nam");
  827. mov_write_string_data_tag(pb, mov, s, s->title);
  828. size = updateSize(pb, pos);
  829. }
  830. return size;
  831. }
  832. /* iTunes name of the artist/performer */
  833. static int mov_write_ART_tag(ByteIOContext *pb, MOVContext* mov,
  834. AVFormatContext *s)
  835. {
  836. int size = 0;
  837. if ( s->author[0] ) {
  838. offset_t pos = url_ftell(pb);
  839. put_be32(pb, 0); /* size */
  840. put_tag(pb, "\251ART");
  841. // we use the author here as this is the only thing that we have...
  842. mov_write_string_data_tag(pb, mov, s, s->author);
  843. size = updateSize(pb, pos);
  844. }
  845. return size;
  846. }
  847. /* iTunes name of the writer */
  848. static int mov_write_wrt_tag(ByteIOContext *pb, MOVContext* mov,
  849. AVFormatContext *s)
  850. {
  851. int size = 0;
  852. if ( s->author[0] ) {
  853. offset_t pos = url_ftell(pb);
  854. put_be32(pb, 0); /* size */
  855. put_tag(pb, "\251wrt");
  856. mov_write_string_data_tag(pb, mov, s, s->author);
  857. size = updateSize(pb, pos);
  858. }
  859. return size;
  860. }
  861. /* iTunes name of the album */
  862. static int mov_write_alb_tag(ByteIOContext *pb, MOVContext* mov,
  863. AVFormatContext *s)
  864. {
  865. int size = 0;
  866. if ( s->album[0] ) {
  867. offset_t pos = url_ftell(pb);
  868. put_be32(pb, 0); /* size */
  869. put_tag(pb, "\251alb");
  870. mov_write_string_data_tag(pb, mov, s, s->album);
  871. size = updateSize(pb, pos);
  872. }
  873. return size;
  874. }
  875. /* iTunes year */
  876. static int mov_write_day_tag(ByteIOContext *pb, MOVContext* mov,
  877. AVFormatContext *s)
  878. {
  879. char year[5];
  880. int size = 0;
  881. if ( s->year ) {
  882. offset_t pos = url_ftell(pb);
  883. put_be32(pb, 0); /* size */
  884. put_tag(pb, "\251day");
  885. snprintf(year, 5, "%04d", s->year);
  886. mov_write_string_data_tag(pb, mov, s, year);
  887. size = updateSize(pb, pos);
  888. }
  889. return size;
  890. }
  891. /* iTunes tool used to create the file */
  892. static int mov_write_too_tag(ByteIOContext *pb, MOVContext* mov,
  893. AVFormatContext *s)
  894. {
  895. offset_t pos = url_ftell(pb);
  896. put_be32(pb, 0); /* size */
  897. put_tag(pb, "\251too");
  898. mov_write_string_data_tag(pb, mov, s, LIBAVFORMAT_IDENT);
  899. return updateSize(pb, pos);
  900. }
  901. /* iTunes comment */
  902. static int mov_write_cmt_tag(ByteIOContext *pb, MOVContext* mov,
  903. AVFormatContext *s)
  904. {
  905. int size = 0;
  906. if ( s->comment[0] ) {
  907. offset_t pos = url_ftell(pb);
  908. put_be32(pb, 0); /* size */
  909. put_tag(pb, "\251cmt");
  910. mov_write_string_data_tag(pb, mov, s, s->comment);
  911. size = updateSize(pb, pos);
  912. }
  913. return size;
  914. }
  915. /* iTunes custom genre */
  916. static int mov_write_gen_tag(ByteIOContext *pb, MOVContext* mov,
  917. AVFormatContext *s)
  918. {
  919. int size = 0;
  920. if ( s->genre[0] ) {
  921. offset_t pos = url_ftell(pb);
  922. put_be32(pb, 0); /* size */
  923. put_tag(pb, "\251gen");
  924. mov_write_string_data_tag(pb, mov, s, s->genre);
  925. size = updateSize(pb, pos);
  926. }
  927. return size;
  928. }
  929. /* iTunes track number */
  930. static int mov_write_trkn_tag(ByteIOContext *pb, MOVContext* mov,
  931. AVFormatContext *s)
  932. {
  933. int size = 0;
  934. if ( s->track ) {
  935. offset_t pos = url_ftell(pb);
  936. put_be32(pb, 0); /* size */
  937. put_tag(pb, "trkn");
  938. {
  939. offset_t pos = url_ftell(pb);
  940. put_be32(pb, 0); /* size */
  941. put_tag(pb, "data");
  942. put_be32(pb, 0); // 8 bytes empty
  943. put_be32(pb, 0);
  944. put_be16(pb, 0); // empty
  945. put_be16(pb, s->track); // track number
  946. put_be16(pb, 0); // total track number
  947. put_be16(pb, 0); // empty
  948. updateSize(pb, pos);
  949. }
  950. size = updateSize(pb, pos);
  951. }
  952. return size;
  953. }
  954. /* iTunes meta data list */
  955. static int mov_write_ilst_tag(ByteIOContext *pb, MOVContext* mov,
  956. AVFormatContext *s)
  957. {
  958. offset_t pos = url_ftell(pb);
  959. put_be32(pb, 0); /* size */
  960. put_tag(pb, "ilst");
  961. mov_write_nam_tag(pb, mov, s);
  962. mov_write_ART_tag(pb, mov, s);
  963. mov_write_wrt_tag(pb, mov, s);
  964. mov_write_alb_tag(pb, mov, s);
  965. mov_write_day_tag(pb, mov, s);
  966. mov_write_too_tag(pb, mov, s);
  967. mov_write_cmt_tag(pb, mov, s);
  968. mov_write_gen_tag(pb, mov, s);
  969. mov_write_trkn_tag(pb, mov, s);
  970. return updateSize(pb, pos);
  971. }
  972. /* iTunes meta data tag */
  973. static int mov_write_meta_tag(ByteIOContext *pb, MOVContext* mov,
  974. AVFormatContext *s)
  975. {
  976. int size = 0;
  977. // only save meta tag if required
  978. if ( s->title[0] || s->author[0] || s->album[0] || s->year ||
  979. s->comment[0] || s->genre[0] || s->track ) {
  980. offset_t pos = url_ftell(pb);
  981. put_be32(pb, 0); /* size */
  982. put_tag(pb, "meta");
  983. put_be32(pb, 0);
  984. mov_write_itunes_hdlr_tag(pb, mov, s);
  985. mov_write_ilst_tag(pb, mov, s);
  986. size = updateSize(pb, pos);
  987. }
  988. return size;
  989. }
  990. static int mov_write_udta_tag(ByteIOContext *pb, MOVContext* mov,
  991. AVFormatContext *s)
  992. {
  993. offset_t pos = url_ftell(pb);
  994. int i;
  995. put_be32(pb, 0); /* size */
  996. put_tag(pb, "udta");
  997. /* iTunes meta data */
  998. mov_write_meta_tag(pb, mov, s);
  999. /* Requirements */
  1000. for (i=0; i<MAX_STREAMS; i++) {
  1001. if(mov->tracks[i].entry <= 0) continue;
  1002. if (mov->tracks[i].enc->codec_id == CODEC_ID_AAC ||
  1003. mov->tracks[i].enc->codec_id == CODEC_ID_MPEG4) {
  1004. offset_t pos = url_ftell(pb);
  1005. put_be32(pb, 0); /* size */
  1006. put_tag(pb, "\251req");
  1007. put_be16(pb, sizeof("QuickTime 6.0 or greater") - 1);
  1008. put_be16(pb, 0);
  1009. put_buffer(pb, "QuickTime 6.0 or greater",
  1010. sizeof("QuickTime 6.0 or greater") - 1);
  1011. updateSize(pb, pos);
  1012. break;
  1013. }
  1014. }
  1015. /* Encoder */
  1016. if(mov->tracks[0].enc && !(mov->tracks[0].enc->flags & CODEC_FLAG_BITEXACT))
  1017. {
  1018. offset_t pos = url_ftell(pb);
  1019. put_be32(pb, 0); /* size */
  1020. put_tag(pb, "\251enc");
  1021. put_be16(pb, sizeof(LIBAVFORMAT_IDENT) - 1); /* string length */
  1022. put_be16(pb, 0);
  1023. put_buffer(pb, LIBAVFORMAT_IDENT, sizeof(LIBAVFORMAT_IDENT) - 1);
  1024. updateSize(pb, pos);
  1025. }
  1026. if( s->title[0] )
  1027. {
  1028. offset_t pos = url_ftell(pb);
  1029. put_be32(pb, 0); /* size */
  1030. put_tag(pb, "\251nam");
  1031. put_be16(pb, strlen(s->title)); /* string length */
  1032. put_be16(pb, 0);
  1033. put_buffer(pb, s->title, strlen(s->title));
  1034. updateSize(pb, pos);
  1035. }
  1036. if( s->author[0] )
  1037. {
  1038. offset_t pos = url_ftell(pb);
  1039. put_be32(pb, 0); /* size */
  1040. put_tag(pb, /*"\251aut"*/ "\251day" );
  1041. put_be16(pb, strlen(s->author)); /* string length */
  1042. put_be16(pb, 0);
  1043. put_buffer(pb, s->author, strlen(s->author));
  1044. updateSize(pb, pos);
  1045. }
  1046. if( s->comment[0] )
  1047. {
  1048. offset_t pos = url_ftell(pb);
  1049. put_be32(pb, 0); /* size */
  1050. put_tag(pb, "\251des");
  1051. put_be16(pb, strlen(s->comment)); /* string length */
  1052. put_be16(pb, 0);
  1053. put_buffer(pb, s->comment, strlen(s->comment));
  1054. updateSize(pb, pos);
  1055. }
  1056. return updateSize(pb, pos);
  1057. }
  1058. static int mov_write_moov_tag(ByteIOContext *pb, MOVContext *mov,
  1059. AVFormatContext *s)
  1060. {
  1061. int i;
  1062. offset_t pos = url_ftell(pb);
  1063. put_be32(pb, 0); /* size placeholder*/
  1064. put_tag(pb, "moov");
  1065. mov->timescale = globalTimescale;
  1066. for (i=0; i<MAX_STREAMS; i++) {
  1067. if(mov->tracks[i].entry <= 0) continue;
  1068. if(mov->tracks[i].enc->codec_type == CODEC_TYPE_VIDEO) {
  1069. mov->tracks[i].timescale = mov->tracks[i].enc->time_base.den;
  1070. mov->tracks[i].sampleDuration = mov->tracks[i].enc->time_base.num;
  1071. }
  1072. else if(mov->tracks[i].enc->codec_type == CODEC_TYPE_AUDIO) {
  1073. /* If AMR, track timescale = 8000, AMR_WB = 16000 */
  1074. if(mov->tracks[i].enc->codec_id == CODEC_ID_AMR_NB) {
  1075. mov->tracks[i].sampleDuration = 160; // Bytes per chunk
  1076. mov->tracks[i].timescale = 8000;
  1077. }
  1078. else {
  1079. mov->tracks[i].timescale = mov->tracks[i].enc->sample_rate;
  1080. mov->tracks[i].sampleDuration = mov->tracks[i].enc->frame_size;
  1081. }
  1082. }
  1083. mov->tracks[i].trackDuration =
  1084. mov->tracks[i].sampleCount * mov->tracks[i].sampleDuration;
  1085. mov->tracks[i].time = mov->time;
  1086. mov->tracks[i].trackID = i+1;
  1087. }
  1088. mov_write_mvhd_tag(pb, mov);
  1089. //mov_write_iods_tag(pb, mov);
  1090. for (i=0; i<MAX_STREAMS; i++) {
  1091. if(mov->tracks[i].entry > 0) {
  1092. mov_write_trak_tag(pb, &(mov->tracks[i]));
  1093. }
  1094. }
  1095. mov_write_udta_tag(pb, mov, s);
  1096. return updateSize(pb, pos);
  1097. }
  1098. int mov_write_mdat_tag(ByteIOContext *pb, MOVContext* mov)
  1099. {
  1100. put_be32(pb, 8); // placeholder for extended size field (64 bit)
  1101. put_tag(pb, "wide");
  1102. mov->mdat_pos = url_ftell(pb);
  1103. put_be32(pb, 0); /* size placeholder*/
  1104. put_tag(pb, "mdat");
  1105. return 0;
  1106. }
  1107. /* TODO: This needs to be more general */
  1108. int mov_write_ftyp_tag(ByteIOContext *pb, AVFormatContext *s)
  1109. {
  1110. MOVContext *mov = s->priv_data;
  1111. put_be32(pb, 0x14 ); /* size */
  1112. put_tag(pb, "ftyp");
  1113. if ( mov->mode == MODE_3GP )
  1114. put_tag(pb, "3gp4");
  1115. else if ( mov->mode == MODE_3G2 )
  1116. put_tag(pb, "3g2a");
  1117. else if ( mov->mode == MODE_PSP )
  1118. put_tag(pb, "MSNV");
  1119. else
  1120. put_tag(pb, "isom");
  1121. put_be32(pb, 0x200 );
  1122. if ( mov->mode == MODE_3GP )
  1123. put_tag(pb, "3gp4");
  1124. else if ( mov->mode == MODE_3G2 )
  1125. put_tag(pb, "3g2a");
  1126. else if ( mov->mode == MODE_PSP )
  1127. put_tag(pb, "MSNV");
  1128. else
  1129. put_tag(pb, "mp41");
  1130. return 0x14;
  1131. }
  1132. static void mov_write_uuidprof_tag(ByteIOContext *pb, AVFormatContext *s)
  1133. {
  1134. int AudioRate = s->streams[1]->codec->sample_rate;
  1135. int FrameRate = ((s->streams[0]->codec->time_base.den) * (0x10000))/ (s->streams[0]->codec->time_base.num);
  1136. //printf("audiorate = %d\n",AudioRate);
  1137. //printf("framerate = %d / %d = 0x%x\n",s->streams[0]->codec->time_base.den,s->streams[0]->codec->time_base.num,FrameRate);
  1138. put_be32(pb, 0x94 ); /* size */
  1139. put_tag(pb, "uuid");
  1140. put_tag(pb, "PROF");
  1141. put_be32(pb, 0x21d24fce ); /* 96 bit UUID */
  1142. put_be32(pb, 0xbb88695c );
  1143. put_be32(pb, 0xfac9c740 );
  1144. put_be32(pb, 0x0 ); /* ? */
  1145. put_be32(pb, 0x3 ); /* 3 sections ? */
  1146. put_be32(pb, 0x14 ); /* size */
  1147. put_tag(pb, "FPRF");
  1148. put_be32(pb, 0x0 ); /* ? */
  1149. put_be32(pb, 0x0 ); /* ? */
  1150. put_be32(pb, 0x0 ); /* ? */
  1151. put_be32(pb, 0x2c ); /* size */
  1152. put_tag(pb, "APRF"); /* audio */
  1153. put_be32(pb, 0x0 );
  1154. put_be32(pb, 0x2 );
  1155. put_tag(pb, "mp4a");
  1156. put_be32(pb, 0x20f );
  1157. put_be32(pb, 0x0 );
  1158. put_be32(pb, 0x40 );
  1159. put_be32(pb, 0x40 );
  1160. put_be32(pb, AudioRate ); //24000 ... audio rate?
  1161. put_be32(pb, 0x2 );
  1162. put_be32(pb, 0x34 ); /* size */
  1163. put_tag(pb, "VPRF"); /* video */
  1164. put_be32(pb, 0x0 );
  1165. put_be32(pb, 0x1 );
  1166. put_tag(pb, "mp4v");
  1167. put_be32(pb, 0x103 );
  1168. put_be32(pb, 0x0 );
  1169. put_be32(pb, 0xc0 );
  1170. put_be32(pb, 0xc0 );
  1171. put_be32(pb, FrameRate); // was 0xefc29
  1172. put_be32(pb, FrameRate ); // was 0xefc29
  1173. put_be16(pb, s->streams[0]->codec->width);
  1174. put_be16(pb, s->streams[0]->codec->height);
  1175. put_be32(pb, 0x010001 );
  1176. }
  1177. static int mov_write_header(AVFormatContext *s)
  1178. {
  1179. ByteIOContext *pb = &s->pb;
  1180. MOVContext *mov = s->priv_data;
  1181. int i;
  1182. for(i=0; i<s->nb_streams; i++){
  1183. AVCodecContext *c= s->streams[i]->codec;
  1184. if (c->codec_type == CODEC_TYPE_VIDEO){
  1185. if (!codec_get_tag(codec_movvideo_tags, c->codec_id)){
  1186. if(!codec_get_tag(codec_bmp_tags, c->codec_id))
  1187. return -1;
  1188. else
  1189. av_log(s, AV_LOG_INFO, "Warning, using MS style video codec tag, the file may be unplayable!\n");
  1190. }
  1191. }else if(c->codec_type == CODEC_TYPE_AUDIO){
  1192. if (!codec_get_tag(codec_movaudio_tags, c->codec_id)){
  1193. if(!codec_get_tag(codec_wav_tags, c->codec_id))
  1194. return -1;
  1195. else
  1196. av_log(s, AV_LOG_INFO, "Warning, using MS style audio codec tag, the file may be unplayable!\n");
  1197. }
  1198. }
  1199. /* don't know yet if mp4 or not */
  1200. mov->tracks[i].language = ff_mov_iso639_to_lang(s->streams[i]->language, 1);
  1201. }
  1202. /* Default mode == MP4 */
  1203. mov->mode = MODE_MP4;
  1204. if (s->oformat != NULL) {
  1205. if (!strcmp("3gp", s->oformat->name)) mov->mode = MODE_3GP;
  1206. else if (!strcmp("3g2", s->oformat->name)) mov->mode = MODE_3G2;
  1207. else if (!strcmp("mov", s->oformat->name)) mov->mode = MODE_MOV;
  1208. else if (!strcmp("psp", s->oformat->name)) mov->mode = MODE_PSP;
  1209. if ( mov->mode == MODE_3GP || mov->mode == MODE_3G2 ||
  1210. mov->mode == MODE_MP4 || mov->mode == MODE_PSP )
  1211. mov_write_ftyp_tag(pb,s);
  1212. if ( mov->mode == MODE_PSP ) {
  1213. if ( s->nb_streams != 2 ) {
  1214. av_log(s, AV_LOG_ERROR, "PSP mode need one video and one audio stream\n");
  1215. return -1;
  1216. }
  1217. mov_write_uuidprof_tag(pb,s);
  1218. }
  1219. }
  1220. for (i=0; i<MAX_STREAMS; i++) {
  1221. mov->tracks[i].mode = mov->mode;
  1222. }
  1223. put_flush_packet(pb);
  1224. return 0;
  1225. }
  1226. static int mov_write_packet(AVFormatContext *s, AVPacket *pkt)
  1227. {
  1228. MOVContext *mov = s->priv_data;
  1229. ByteIOContext *pb = &s->pb;
  1230. AVCodecContext *enc = s->streams[pkt->stream_index]->codec;
  1231. MOVTrack* trk = &mov->tracks[pkt->stream_index];
  1232. int cl, id;
  1233. unsigned int samplesInChunk = 0;
  1234. int size= pkt->size;
  1235. if (url_is_streamed(&s->pb)) return 0; /* Can't handle that */
  1236. if (!size) return 0; /* Discard 0 sized packets */
  1237. if (enc->codec_type == CODEC_TYPE_VIDEO ) {
  1238. samplesInChunk = 1;
  1239. }
  1240. else if (enc->codec_type == CODEC_TYPE_AUDIO ) {
  1241. if( enc->codec_id == CODEC_ID_AMR_NB) {
  1242. /* We must find out how many AMR blocks there are in one packet */
  1243. static uint16_t packed_size[16] =
  1244. {13, 14, 16, 18, 20, 21, 27, 32, 6, 0, 0, 0, 0, 0, 0, 0};
  1245. int len = 0;
  1246. while (len < size && samplesInChunk < 100) {
  1247. len += packed_size[(pkt->data[len] >> 3) & 0x0F];
  1248. samplesInChunk++;
  1249. }
  1250. }
  1251. else if(enc->codec_id == CODEC_ID_PCM_ALAW) {
  1252. samplesInChunk = size/enc->channels;
  1253. }
  1254. else if(enc->codec_id == CODEC_ID_PCM_S16BE || enc->codec_id == CODEC_ID_PCM_S16LE) {
  1255. samplesInChunk = size/(2*enc->channels);
  1256. }
  1257. else {
  1258. samplesInChunk = 1;
  1259. }
  1260. }
  1261. if ((enc->codec_id == CODEC_ID_MPEG4 || enc->codec_id == CODEC_ID_AAC)
  1262. && trk->vosLen == 0) {
  1263. // assert(enc->extradata_size);
  1264. trk->vosLen = enc->extradata_size;
  1265. trk->vosData = av_malloc(trk->vosLen);
  1266. memcpy(trk->vosData, enc->extradata, trk->vosLen);
  1267. }
  1268. cl = trk->entry / MOV_INDEX_CLUSTER_SIZE;
  1269. id = trk->entry % MOV_INDEX_CLUSTER_SIZE;
  1270. if (trk->ents_allocated <= trk->entry) {
  1271. trk->cluster = av_realloc(trk->cluster, (cl+1)*sizeof(void*));
  1272. if (!trk->cluster)
  1273. return -1;
  1274. trk->cluster[cl] = av_malloc(MOV_INDEX_CLUSTER_SIZE*sizeof(MOVIentry));
  1275. if (!trk->cluster[cl])
  1276. return -1;
  1277. trk->ents_allocated += MOV_INDEX_CLUSTER_SIZE;
  1278. }
  1279. if (mov->mdat_written == 0) {
  1280. mov_write_mdat_tag(pb, mov);
  1281. mov->mdat_written = 1;
  1282. mov->time = s->timestamp + 0x7C25B080; //1970 based -> 1904 based
  1283. }
  1284. trk->cluster[cl][id].pos = url_ftell(pb);
  1285. trk->cluster[cl][id].samplesInChunk = samplesInChunk;
  1286. trk->cluster[cl][id].size = size;
  1287. trk->cluster[cl][id].entries = samplesInChunk;
  1288. if(enc->codec_type == CODEC_TYPE_VIDEO) {
  1289. trk->cluster[cl][id].key_frame = !!(pkt->flags & PKT_FLAG_KEY);
  1290. if(trk->cluster[cl][id].key_frame)
  1291. trk->hasKeyframes = 1;
  1292. }
  1293. trk->enc = enc;
  1294. trk->entry++;
  1295. trk->sampleCount += samplesInChunk;
  1296. trk->mdat_size += size;
  1297. put_buffer(pb, pkt->data, size);
  1298. put_flush_packet(pb);
  1299. return 0;
  1300. }
  1301. static int mov_write_trailer(AVFormatContext *s)
  1302. {
  1303. MOVContext *mov = s->priv_data;
  1304. ByteIOContext *pb = &s->pb;
  1305. int res = 0;
  1306. int i;
  1307. uint64_t j;
  1308. offset_t moov_pos = url_ftell(pb);
  1309. /* Write size of mdat tag */
  1310. for (i=0, j=0; i<MAX_STREAMS; i++) {
  1311. if(mov->tracks[i].ents_allocated > 0) {
  1312. j += mov->tracks[i].mdat_size;
  1313. }
  1314. }
  1315. if (j+8 <= UINT32_MAX) {
  1316. url_fseek(pb, mov->mdat_pos, SEEK_SET);
  1317. put_be32(pb, j+8);
  1318. } else {
  1319. /* overwrite 'wide' placeholder atom */
  1320. url_fseek(pb, mov->mdat_pos - 8, SEEK_SET);
  1321. put_be32(pb, 1); /* special value: real atom size will be 64 bit value after tag field */
  1322. put_tag(pb, "mdat");
  1323. put_be64(pb, j+16);
  1324. }
  1325. url_fseek(pb, moov_pos, SEEK_SET);
  1326. mov_write_moov_tag(pb, mov, s);
  1327. for (i=0; i<MAX_STREAMS; i++) {
  1328. for (j=0; j<mov->tracks[i].ents_allocated/MOV_INDEX_CLUSTER_SIZE; j++) {
  1329. av_free(mov->tracks[i].cluster[j]);
  1330. }
  1331. av_free(mov->tracks[i].cluster);
  1332. if( mov->tracks[i].vosLen ) av_free( mov->tracks[i].vosData );
  1333. mov->tracks[i].cluster = NULL;
  1334. mov->tracks[i].ents_allocated = mov->tracks[i].entry = 0;
  1335. }
  1336. put_flush_packet(pb);
  1337. return res;
  1338. }
  1339. static AVOutputFormat mov_oformat = {
  1340. "mov",
  1341. "mov format",
  1342. NULL,
  1343. "mov",
  1344. sizeof(MOVContext),
  1345. CODEC_ID_AAC,
  1346. CODEC_ID_MPEG4,
  1347. mov_write_header,
  1348. mov_write_packet,
  1349. mov_write_trailer,
  1350. .flags = AVFMT_GLOBALHEADER,
  1351. };
  1352. static AVOutputFormat _3gp_oformat = {
  1353. "3gp",
  1354. "3gp format",
  1355. NULL,
  1356. "3gp",
  1357. sizeof(MOVContext),
  1358. CODEC_ID_AMR_NB,
  1359. CODEC_ID_H263,
  1360. mov_write_header,
  1361. mov_write_packet,
  1362. mov_write_trailer,
  1363. .flags = AVFMT_GLOBALHEADER,
  1364. };
  1365. static AVOutputFormat mp4_oformat = {
  1366. "mp4",
  1367. "mp4 format",
  1368. "application/mp4",
  1369. "mp4,m4a",
  1370. sizeof(MOVContext),
  1371. CODEC_ID_AAC,
  1372. CODEC_ID_MPEG4,
  1373. mov_write_header,
  1374. mov_write_packet,
  1375. mov_write_trailer,
  1376. .flags = AVFMT_GLOBALHEADER,
  1377. };
  1378. static AVOutputFormat psp_oformat = {
  1379. "psp",
  1380. "psp mp4 format",
  1381. NULL,
  1382. "mp4,psp",
  1383. sizeof(MOVContext),
  1384. CODEC_ID_AAC,
  1385. CODEC_ID_MPEG4,
  1386. mov_write_header,
  1387. mov_write_packet,
  1388. mov_write_trailer,
  1389. // .flags = AVFMT_GLOBALHEADER,
  1390. };
  1391. static AVOutputFormat _3g2_oformat = {
  1392. "3g2",
  1393. "3gp2 format",
  1394. NULL,
  1395. "3g2",
  1396. sizeof(MOVContext),
  1397. CODEC_ID_AMR_NB,
  1398. CODEC_ID_H263,
  1399. mov_write_header,
  1400. mov_write_packet,
  1401. mov_write_trailer,
  1402. .flags = AVFMT_GLOBALHEADER,
  1403. };
  1404. int movenc_init(void)
  1405. {
  1406. av_register_output_format(&mov_oformat);
  1407. av_register_output_format(&_3gp_oformat);
  1408. av_register_output_format(&mp4_oformat);
  1409. av_register_output_format(&psp_oformat);
  1410. av_register_output_format(&_3g2_oformat);
  1411. return 0;
  1412. }