libmp4mux.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. /*****************************************************************************
  2. * libmp4mux.c: mp4/mov muxer
  3. *****************************************************************************
  4. * Copyright (C) 2001, 2002, 2003, 2006, 20115 VLC authors and VideoLAN
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Gildas Bazin <gbazin at videolan dot org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software Foundation,
  21. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  22. *****************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. # include "config.h"
  25. #endif
  26. #include "libmp4mux.h"
  27. #include "../demux/mp4/libmp4.h" /* flags */
  28. #include "../packetizer/hevc_nal.h"
  29. #include "../packetizer/h264_nal.h" /* h264_AnnexB_get_spspps */
  30. #include "../packetizer/hxxx_nal.h"
  31. #include <vlc_es.h>
  32. #include <vlc_iso_lang.h>
  33. #include <vlc_bits.h>
  34. #include <assert.h>
  35. #include <time.h>
  36. bool mp4mux_trackinfo_Init(mp4mux_trackinfo_t *p_stream, unsigned i_id,
  37. uint32_t i_timescale)
  38. {
  39. memset(p_stream, 0, sizeof(*p_stream));
  40. p_stream->i_track_id = i_id;
  41. p_stream->i_timescale = i_timescale;
  42. p_stream->i_entry_count = 0;
  43. p_stream->i_entry_max = 1000;
  44. p_stream->entry = calloc(p_stream->i_entry_max, sizeof(mp4mux_entry_t));
  45. if(!p_stream->entry)
  46. return false;
  47. es_format_Init(&p_stream->fmt, 0, 0);
  48. return true;
  49. }
  50. void mp4mux_trackinfo_Clear(mp4mux_trackinfo_t *p_stream)
  51. {
  52. es_format_Clean(&p_stream->fmt);
  53. if (p_stream->a52_frame)
  54. block_Release(p_stream->a52_frame);
  55. free(p_stream->entry);
  56. free(p_stream->p_edits);
  57. }
  58. bo_t *box_new(const char *fcc)
  59. {
  60. bo_t *box = malloc(sizeof(*box));
  61. if (!box)
  62. return NULL;
  63. if(!bo_init(box, 1024))
  64. {
  65. bo_free(box);
  66. return NULL;
  67. }
  68. bo_add_32be (box, 0);
  69. bo_add_fourcc(box, fcc);
  70. return box;
  71. }
  72. bo_t *box_full_new(const char *fcc, uint8_t v, uint32_t f)
  73. {
  74. bo_t *box = box_new(fcc);
  75. if (!box)
  76. return NULL;
  77. bo_add_8 (box, v);
  78. bo_add_24be (box, f);
  79. return box;
  80. }
  81. void box_fix(bo_t *box, uint32_t i_size)
  82. {
  83. bo_set_32be(box, 0, i_size);
  84. }
  85. void box_gather (bo_t *box, bo_t *box2)
  86. {
  87. if(box2 && box2->b && box && box->b)
  88. {
  89. box_fix(box2, box2->b->i_buffer);
  90. size_t i_offset = box->b->i_buffer;
  91. box->b = block_Realloc(box->b, 0, box->b->i_buffer + box2->b->i_buffer);
  92. if(likely(box->b))
  93. memcpy(&box->b->p_buffer[i_offset], box2->b->p_buffer, box2->b->i_buffer);
  94. }
  95. bo_free(box2);
  96. }
  97. static inline void bo_add_mp4_tag_descr(bo_t *box, uint8_t tag, uint32_t size)
  98. {
  99. bo_add_8(box, tag);
  100. for (int i = 3; i>0; i--)
  101. bo_add_8(box, (size>>(7*i)) | 0x80);
  102. bo_add_8(box, size & 0x7F);
  103. }
  104. static int64_t get_timestamp(void)
  105. {
  106. int64_t i_timestamp = time(NULL);
  107. i_timestamp += 2082844800; // MOV/MP4 start date is 1/1/1904
  108. // 208284480 is (((1970 - 1904) * 365) + 17) * 24 * 60 * 60
  109. return i_timestamp;
  110. }
  111. /****************************************************************************/
  112. static void matrix_apply_rotation(es_format_t *fmt, uint32_t mvhd_matrix[9])
  113. {
  114. enum video_orientation_t orientation = ORIENT_NORMAL;
  115. if (fmt->i_cat == VIDEO_ES)
  116. orientation = fmt->video.orientation;
  117. #define ATAN(a, b) \
  118. do { \
  119. mvhd_matrix[1] = ((uint32_t)(a)) << 16; \
  120. mvhd_matrix[0] = ((uint32_t)(b)) << 16; \
  121. } while(0)
  122. switch (orientation) {
  123. case ORIENT_ROTATED_90: ATAN( 1, 0); break;
  124. case ORIENT_ROTATED_180: ATAN( 0, -1); break;
  125. case ORIENT_ROTATED_270: ATAN(-1, 0); break;
  126. default: ATAN( 0, 1); break;
  127. }
  128. mvhd_matrix[3] = mvhd_matrix[0] ? 0 : 0x10000;
  129. mvhd_matrix[4] = mvhd_matrix[1] ? 0 : 0x10000;
  130. }
  131. static void AddEdit(bo_t *elst,
  132. int64_t i_movie_scaled_duration,
  133. int64_t i_media_scaled_time,
  134. bool b_64_ext)
  135. {
  136. if(b_64_ext)
  137. {
  138. bo_add_64be(elst, i_movie_scaled_duration);
  139. bo_add_64be(elst, i_media_scaled_time);
  140. }
  141. else
  142. {
  143. bo_add_32be(elst, i_movie_scaled_duration);
  144. bo_add_32be(elst, i_media_scaled_time);
  145. }
  146. bo_add_16be(elst, 1);
  147. bo_add_16be(elst, 0);
  148. }
  149. static bo_t *GetEDTS( mp4mux_trackinfo_t *p_track, uint32_t i_movietimescale, bool b_64_ext)
  150. {
  151. if(p_track->i_edits_count == 0)
  152. return NULL;
  153. bo_t *edts = box_new("edts");
  154. bo_t *elst = box_full_new("elst", b_64_ext ? 1 : 0, 0);
  155. if(!elst || !edts)
  156. {
  157. bo_free(elst);
  158. bo_free(edts);
  159. return NULL;
  160. }
  161. uint32_t i_total_edits = p_track->i_edits_count;
  162. for(unsigned i=0; i<p_track->i_edits_count; i++)
  163. {
  164. /* !WARN! media time must start sample time 0, we need a -1 edit for start offsets */
  165. if(p_track->p_edits[i].i_start_offset)
  166. i_total_edits++;
  167. }
  168. bo_add_32be(elst, i_total_edits);
  169. for(unsigned i=0; i<p_track->i_edits_count; i++)
  170. {
  171. if(p_track->p_edits[i].i_start_offset)
  172. {
  173. AddEdit(elst,
  174. p_track->p_edits[i].i_start_offset * i_movietimescale / CLOCK_FREQ,
  175. -1,
  176. b_64_ext);
  177. }
  178. /* !WARN AGAIN! Uses different Timescales ! */
  179. AddEdit(elst,
  180. p_track->p_edits[i].i_duration * i_movietimescale / CLOCK_FREQ,
  181. p_track->p_edits[i].i_start_time * p_track->i_timescale / CLOCK_FREQ,
  182. b_64_ext);
  183. }
  184. box_gather(edts, elst);
  185. return edts;
  186. }
  187. static bo_t *GetESDS(mp4mux_trackinfo_t *p_track)
  188. {
  189. bo_t *esds;
  190. /* */
  191. int i_decoder_specific_info_size = (p_track->fmt.i_extra > 0) ? 5 + p_track->fmt.i_extra : 0;
  192. esds = box_full_new("esds", 0, 0);
  193. if(!esds)
  194. return NULL;
  195. /* Compute Max bitrate */
  196. int64_t i_bitrate_avg = 0;
  197. int64_t i_bitrate_max = 0;
  198. /* Compute avg/max bitrate */
  199. for (unsigned i = 0; i < p_track->i_entry_count; i++) {
  200. i_bitrate_avg += p_track->entry[i].i_size;
  201. if (p_track->entry[i].i_length > 0) {
  202. int64_t i_bitrate = INT64_C(8000000) * p_track->entry[i].i_size / p_track->entry[i].i_length;
  203. if (i_bitrate > i_bitrate_max)
  204. i_bitrate_max = i_bitrate;
  205. }
  206. }
  207. if (p_track->i_read_duration > 0)
  208. i_bitrate_avg = INT64_C(8000000) * i_bitrate_avg / p_track->i_read_duration;
  209. else
  210. i_bitrate_avg = 0;
  211. if (i_bitrate_max <= 1)
  212. i_bitrate_max = 0x7fffffff;
  213. /* ES_Descr */
  214. bo_add_mp4_tag_descr(esds, 0x03, 3 + 5 + 13 + i_decoder_specific_info_size + 5 + 1);
  215. bo_add_16be(esds, p_track->i_track_id);
  216. bo_add_8 (esds, 0x1f); // flags=0|streamPriority=0x1f
  217. /* DecoderConfigDescr */
  218. bo_add_mp4_tag_descr(esds, 0x04, 13 + i_decoder_specific_info_size);
  219. int i_object_type_indication;
  220. switch(p_track->fmt.i_codec)
  221. {
  222. case VLC_CODEC_MP4V:
  223. i_object_type_indication = 0x20;
  224. break;
  225. case VLC_CODEC_MPGV:
  226. if(p_track->fmt.i_original_fourcc == VLC_CODEC_MP1V)
  227. {
  228. i_object_type_indication = 0x6b;
  229. break;
  230. }
  231. case VLC_CODEC_MP2V:
  232. /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
  233. i_object_type_indication = 0x65;
  234. break;
  235. case VLC_CODEC_MP1V:
  236. /* MPEG-I=0x6b, MPEG-II = 0x60 -> 0x65 */
  237. i_object_type_indication = 0x6b;
  238. break;
  239. case VLC_CODEC_MP4A:
  240. /* FIXME for mpeg2-aac == 0x66->0x68 */
  241. i_object_type_indication = 0x40;
  242. break;
  243. case VLC_CODEC_MPGA:
  244. i_object_type_indication =
  245. p_track->fmt.audio.i_rate < 32000 ? 0x69 : 0x6b;
  246. break;
  247. case VLC_CODEC_DTS:
  248. i_object_type_indication = 0xa9;
  249. break;
  250. default:
  251. i_object_type_indication = 0x00;
  252. break;
  253. }
  254. int i_stream_type = p_track->fmt.i_cat == VIDEO_ES ? 0x04 : 0x05;
  255. bo_add_8 (esds, i_object_type_indication);
  256. bo_add_8 (esds, (i_stream_type << 2) | 1);
  257. bo_add_24be(esds, 1024 * 1024); // bufferSizeDB
  258. bo_add_32be(esds, i_bitrate_max); // maxBitrate
  259. bo_add_32be(esds, i_bitrate_avg); // avgBitrate
  260. if (p_track->fmt.i_extra > 0) {
  261. /* DecoderSpecificInfo */
  262. bo_add_mp4_tag_descr(esds, 0x05, p_track->fmt.i_extra);
  263. for (int i = 0; i < p_track->fmt.i_extra; i++)
  264. bo_add_8(esds, ((uint8_t*)p_track->fmt.p_extra)[i]);
  265. }
  266. /* SL_Descr mandatory */
  267. bo_add_mp4_tag_descr(esds, 0x06, 1);
  268. bo_add_8 (esds, 0x02); // sl_predefined
  269. return esds;
  270. }
  271. static bo_t *GetWaveTag(mp4mux_trackinfo_t *p_track)
  272. {
  273. bo_t *wave;
  274. bo_t *box;
  275. wave = box_new("wave");
  276. if(wave)
  277. {
  278. box = box_new("frma");
  279. if(box)
  280. {
  281. bo_add_fourcc(box, "mp4a");
  282. box_gather(wave, box);
  283. }
  284. box = box_new("mp4a");
  285. if(box)
  286. {
  287. bo_add_32be(box, 0);
  288. box_gather(wave, box);
  289. }
  290. box = GetESDS(p_track);
  291. box_gather(wave, box);
  292. box = box_new("srcq");
  293. if(box)
  294. {
  295. bo_add_32be(box, 0x40);
  296. box_gather(wave, box);
  297. }
  298. /* wazza ? */
  299. bo_add_32be(wave, 8); /* new empty box */
  300. bo_add_32be(wave, 0); /* box label */
  301. }
  302. return wave;
  303. }
  304. static bo_t *GetDec3Tag(es_format_t *p_fmt, block_t *a52_frame)
  305. {
  306. if (!a52_frame)
  307. return NULL;
  308. bs_t s;
  309. bs_write_init(&s, a52_frame->p_buffer, sizeof(a52_frame->i_buffer));
  310. bs_skip(&s, 16); // syncword
  311. uint8_t fscod, bsid, bsmod, acmod, lfeon, strmtyp;
  312. bsmod = 0;
  313. strmtyp = bs_read(&s, 2);
  314. if (strmtyp & 0x1) // dependent or reserved stream
  315. return NULL;
  316. if (bs_read(&s, 3) != 0x0) // substreamid: we don't support more than 1 stream
  317. return NULL;
  318. int numblkscod;
  319. bs_skip(&s, 11); // frmsizecod
  320. fscod = bs_read(&s, 2);
  321. if (fscod == 0x03) {
  322. bs_skip(&s, 2); // fscod2
  323. numblkscod = 3;
  324. } else {
  325. numblkscod = bs_read(&s, 2);
  326. }
  327. acmod = bs_read(&s, 3);
  328. lfeon = bs_read1(&s);
  329. bsid = bs_read(&s, 5);
  330. bs_skip(&s, 5); // dialnorm
  331. if (bs_read1(&s)) // compre
  332. bs_skip(&s, 5); // compr
  333. if (acmod == 0) {
  334. bs_skip(&s, 5); // dialnorm2
  335. if (bs_read1(&s)) // compr2e
  336. bs_skip(&s, 8); // compr2
  337. }
  338. if (strmtyp == 0x1) // dependent stream XXX: unsupported
  339. if (bs_read1(&s)) // chanmape
  340. bs_skip(&s, 16); // chanmap
  341. /* we have to skip mixing info to read bsmod */
  342. if (bs_read1(&s)) { // mixmdate
  343. if (acmod > 0x2) // 2+ channels
  344. bs_skip(&s, 2); // dmixmod
  345. if ((acmod & 0x1) && (acmod > 0x2)) // 3 front channels
  346. bs_skip(&s, 3 + 3); // ltrtcmixlev + lorocmixlev
  347. if (acmod & 0x4) // surround channel
  348. bs_skip(&s, 3 + 3); // ltrsurmixlev + lorosurmixlev
  349. if (lfeon)
  350. if (bs_read1(&s))
  351. bs_skip(&s, 5); // lfemixlevcod
  352. if (strmtyp == 0) { // independent stream
  353. if (bs_read1(&s)) // pgmscle
  354. bs_skip(&s, 6); // pgmscl
  355. if (acmod == 0x0) // dual mono
  356. if (bs_read1(&s)) // pgmscl2e
  357. bs_skip(&s, 6); // pgmscl2
  358. if (bs_read1(&s)) // extpgmscle
  359. bs_skip(&s, 6); // extpgmscl
  360. uint8_t mixdef = bs_read(&s, 2);
  361. if (mixdef == 0x1)
  362. bs_skip(&s, 5);
  363. else if (mixdef == 0x2)
  364. bs_skip(&s, 12);
  365. else if (mixdef == 0x3) {
  366. uint8_t mixdeflen = bs_read(&s, 5);
  367. bs_skip(&s, 8 * (mixdeflen + 2));
  368. }
  369. if (acmod < 0x2) { // mono or dual mono
  370. if (bs_read1(&s)) // paninfoe
  371. bs_skip(&s, 14); // paninfo
  372. if (acmod == 0) // dual mono
  373. if (bs_read1(&s)) // paninfo2e
  374. bs_skip(&s, 14); // paninfo2
  375. }
  376. if (bs_read1(&s)) { // frmmixcfginfoe
  377. static const int blocks[4] = { 1, 2, 3, 6 };
  378. int number_of_blocks = blocks[numblkscod];
  379. if (number_of_blocks == 1)
  380. bs_skip(&s, 5); // blkmixcfginfo[0]
  381. else for (int i = 0; i < number_of_blocks; i++)
  382. if (bs_read1(&s)) // blkmixcfginfoe
  383. bs_skip(&s, 5); // blkmixcfginfo[i]
  384. }
  385. }
  386. }
  387. if (bs_read1(&s)) // infomdate
  388. bsmod = bs_read(&s, 3);
  389. uint8_t mp4_eac3_header[5] = {0};
  390. bs_init(&s, mp4_eac3_header, sizeof(mp4_eac3_header));
  391. int data_rate = p_fmt->i_bitrate / 1000;
  392. bs_write(&s, 13, data_rate);
  393. bs_write(&s, 3, 0); // num_ind_sub - 1
  394. bs_write(&s, 2, fscod);
  395. bs_write(&s, 5, bsid);
  396. bs_write(&s, 5, bsmod);
  397. bs_write(&s, 3, acmod);
  398. bs_write(&s, 1, lfeon);
  399. bs_write(&s, 3, 0); // reserved
  400. bs_write(&s, 4, 0); // num_dep_sub
  401. bs_write(&s, 1, 0); // reserved
  402. bo_t *dec3 = box_new("dec3");
  403. if(dec3)
  404. bo_add_mem(dec3, sizeof(mp4_eac3_header), mp4_eac3_header);
  405. return dec3;
  406. }
  407. static bo_t *GetDac3Tag(block_t *a52_frame)
  408. {
  409. if (!a52_frame)
  410. return NULL;
  411. bo_t *dac3 = box_new("dac3");
  412. if(!dac3)
  413. return NULL;
  414. bs_t s;
  415. bs_init(&s, a52_frame->p_buffer, sizeof(a52_frame->i_buffer));
  416. uint8_t fscod, bsid, bsmod, acmod, lfeon, frmsizecod;
  417. bs_skip(&s, 16 + 16); // syncword + crc
  418. fscod = bs_read(&s, 2);
  419. frmsizecod = bs_read(&s, 6);
  420. bsid = bs_read(&s, 5);
  421. bsmod = bs_read(&s, 3);
  422. acmod = bs_read(&s, 3);
  423. if (acmod == 2)
  424. bs_skip(&s, 2); // dsurmod
  425. else {
  426. if ((acmod & 1) && acmod != 1)
  427. bs_skip(&s, 2); // cmixlev
  428. if (acmod & 4)
  429. bs_skip(&s, 2); // surmixlev
  430. }
  431. lfeon = bs_read1(&s);
  432. uint8_t mp4_a52_header[3];
  433. bs_init(&s, mp4_a52_header, sizeof(mp4_a52_header));
  434. bs_write(&s, 2, fscod);
  435. bs_write(&s, 5, bsid);
  436. bs_write(&s, 3, bsmod);
  437. bs_write(&s, 3, acmod);
  438. bs_write(&s, 1, lfeon);
  439. bs_write(&s, 5, frmsizecod >> 1); // bit_rate_code
  440. bs_write(&s, 5, 0); // reserved
  441. bo_add_mem(dac3, sizeof(mp4_a52_header), mp4_a52_header);
  442. return dac3;
  443. }
  444. static bo_t *GetDamrTag(es_format_t *p_fmt)
  445. {
  446. bo_t *damr = box_new("damr");
  447. if(!damr)
  448. return NULL;
  449. bo_add_fourcc(damr, "REFC");
  450. bo_add_8(damr, 0);
  451. if (p_fmt->i_codec == VLC_CODEC_AMR_NB)
  452. bo_add_16be(damr, 0x81ff); /* Mode set (all modes for AMR_NB) */
  453. else
  454. bo_add_16be(damr, 0x83ff); /* Mode set (all modes for AMR_WB) */
  455. bo_add_16be(damr, 0x1); /* Mode change period (no restriction) */
  456. return damr;
  457. }
  458. static bo_t *GetD263Tag(void)
  459. {
  460. bo_t *d263 = box_new("d263");
  461. if(!d263)
  462. return NULL;
  463. bo_add_fourcc(d263, "VLC ");
  464. bo_add_16be(d263, 0xa);
  465. bo_add_8(d263, 0);
  466. return d263;
  467. }
  468. static void hevcParseVPS(uint8_t * p_buffer, size_t i_buffer, uint8_t *general,
  469. uint8_t * numTemporalLayer, bool * temporalIdNested)
  470. {
  471. if(i_buffer < 19)
  472. return;
  473. bs_t bs;
  474. bs_init(&bs, p_buffer, i_buffer);
  475. unsigned i_bitflow = 0;
  476. bs.p_fwpriv = &i_bitflow;
  477. bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
  478. /* first two bytes are the NAL header, 3rd and 4th are:
  479. vps_video_parameter_set_id(4)
  480. vps_reserved_3_2bis(2)
  481. vps_max_layers_minus1(6)
  482. vps_max_sub_layers_minus1(3)
  483. vps_temporal_id_nesting_flags
  484. */
  485. bs_skip( &bs, 16 + 4 + 2 + 6 );
  486. *numTemporalLayer = bs_read( &bs, 3 ) + 1;
  487. *temporalIdNested = bs_read1( &bs );
  488. /* 5th & 6th are reserved 0xffff */
  489. bs_skip( &bs, 16 );
  490. /* copy the first 12 bytes of profile tier */
  491. for(unsigned i=0; i<12; i++)
  492. general[i] = bs_read( &bs, 8 );
  493. }
  494. static inline void hevc_skip_profile_tiers_level( bs_t * bs, int32_t max_sub_layer_minus1 )
  495. {
  496. uint8_t sub_layer_profile_present_flag[8];
  497. uint8_t sub_layer_level_present_flag[8];
  498. /* skipping useless fields of the VPS see https://www.itu.int/rec/dologin_pub.asp?lang=e&id=T-REC-H.265-201304-I!!PDF-E&type=item */
  499. bs_skip( bs, 2 + 1 + 5 + 32 + 1 + 1 + 1 + 1 + 44 + 8 );
  500. for( int32_t i = 0; i < max_sub_layer_minus1; i++ )
  501. {
  502. sub_layer_profile_present_flag[i] = bs_read1( bs );
  503. sub_layer_level_present_flag[i] = bs_read1( bs );
  504. }
  505. if(max_sub_layer_minus1 > 0)
  506. bs_skip( bs, (8 - max_sub_layer_minus1) * 2 );
  507. for( int32_t i = 0; i < max_sub_layer_minus1; i++ )
  508. {
  509. if( sub_layer_profile_present_flag[i] )
  510. bs_skip( bs, 2 + 1 + 5 + 32 + 1 + 1 + 1 + 1 + 44 );
  511. if( sub_layer_level_present_flag[i] )
  512. bs_skip( bs, 8 );
  513. }
  514. }
  515. static void hevcParseSPS(uint8_t * p_buffer, size_t i_buffer, uint8_t * chroma_idc,
  516. uint8_t *bit_depth_luma_minus8, uint8_t *bit_depth_chroma_minus8)
  517. {
  518. if(i_buffer < 2)
  519. return;
  520. bs_t bs;
  521. bs_init(&bs, p_buffer + 2, i_buffer - 2);
  522. unsigned i_bitflow = 0;
  523. bs.p_fwpriv = &i_bitflow;
  524. bs.pf_forward = hxxx_bsfw_ep3b_to_rbsp; /* Does the emulated 3bytes conversion to rbsp */
  525. /* skip vps id */
  526. bs_skip(&bs, 4);
  527. uint32_t sps_max_sublayer_minus1 = bs_read(&bs, 3);
  528. /* skip nesting flag */
  529. bs_skip(&bs, 1);
  530. hevc_skip_profile_tiers_level(&bs, sps_max_sublayer_minus1);
  531. /* skip sps id */
  532. (void) bs_read_ue( &bs );
  533. *chroma_idc = bs_read_ue(&bs);
  534. if (*chroma_idc == 3)
  535. bs_skip(&bs, 1);
  536. /* skip width and heigh */
  537. (void) bs_read_ue( &bs );
  538. (void) bs_read_ue( &bs );
  539. uint32_t conformance_window_flag = bs_read1(&bs);
  540. if (conformance_window_flag) {
  541. /* skip offsets*/
  542. (void) bs_read_ue(&bs);
  543. (void) bs_read_ue(&bs);
  544. (void) bs_read_ue(&bs);
  545. (void) bs_read_ue(&bs);
  546. }
  547. *bit_depth_luma_minus8 = bs_read_ue(&bs);
  548. *bit_depth_chroma_minus8 = bs_read_ue(&bs);
  549. }
  550. static bo_t *GetHvcCTag(es_format_t *p_fmt, bool b_completeness)
  551. {
  552. /* Generate hvcC box matching iso/iec 14496-15 3rd edition */
  553. bo_t *hvcC = box_new("hvcC");
  554. if(!hvcC || !p_fmt->i_extra)
  555. return hvcC;
  556. struct nal {
  557. size_t i_buffer;
  558. uint8_t * p_buffer;
  559. };
  560. struct nal rg_vps[HEVC_VPS_ID_MAX + 1], rg_sps[HEVC_SPS_ID_MAX + 1],
  561. rg_pps[HEVC_VPS_ID_MAX + 1], *p_sei = NULL, *p_nal = NULL;
  562. uint8_t i_vps = 0, i_sps = 0, i_pps = 0, i_num_arrays = 0;
  563. size_t i_sei = 0;
  564. uint8_t * p_buffer = p_fmt->p_extra;
  565. size_t i_buffer = p_fmt->i_extra;
  566. /* Extradata is already an HEVCDecoderConfigurationRecord */
  567. if(hevc_ishvcC(p_buffer, i_buffer))
  568. {
  569. (void) bo_add_mem(hvcC, i_buffer, p_buffer);
  570. return hvcC;
  571. }
  572. uint8_t general_configuration[12] = {0};
  573. uint8_t i_numTemporalLayer = 0;
  574. uint8_t i_chroma_idc = 1;
  575. uint8_t i_bit_depth_luma_minus8 = 0;
  576. uint8_t i_bit_depth_chroma_minus8 = 0;
  577. bool b_temporalIdNested = false;
  578. uint32_t cmp = 0xFFFFFFFF;
  579. while (i_buffer) {
  580. /* look for start code 0X0000001 */
  581. while (i_buffer) {
  582. cmp = (cmp << 8) | *p_buffer;
  583. if((cmp ^ UINT32_C(0x100)) <= UINT32_C(0xFF))
  584. break;
  585. p_buffer++;
  586. i_buffer--;
  587. }
  588. if (p_nal)
  589. p_nal->i_buffer = p_buffer - p_nal->p_buffer - ((i_buffer)?3:0);
  590. switch (hevc_getNALType(p_buffer)) {
  591. case HEVC_NAL_VPS:
  592. if(i_vps > HEVC_VPS_ID_MAX)
  593. break;
  594. p_nal = &rg_vps[i_vps++];
  595. p_nal->p_buffer = p_buffer;
  596. /* Only keep the general profile from the first VPS
  597. * if there are several (this shouldn't happen so soon) */
  598. if (i_vps == 1) {
  599. hevcParseVPS(p_buffer, i_buffer, general_configuration,
  600. &i_numTemporalLayer, &b_temporalIdNested);
  601. i_num_arrays++;
  602. }
  603. break;
  604. case HEVC_NAL_SPS: {
  605. if(i_sps > HEVC_SPS_ID_MAX)
  606. break;
  607. p_nal = &rg_sps[i_sps++];
  608. p_nal->p_buffer = p_buffer;
  609. if (i_sps == 1 && i_buffer > 15) {
  610. /* Get Chroma_idc and bitdepths */
  611. hevcParseSPS(p_buffer, i_buffer, &i_chroma_idc,
  612. &i_bit_depth_luma_minus8, &i_bit_depth_chroma_minus8);
  613. i_num_arrays++;
  614. }
  615. break;
  616. }
  617. case HEVC_NAL_PPS: {
  618. if(i_pps > HEVC_PPS_ID_MAX)
  619. break;
  620. p_nal = &rg_pps[i_pps++];
  621. p_nal->p_buffer = p_buffer;
  622. if (i_pps == 1)
  623. i_num_arrays++;
  624. break;
  625. }
  626. case HEVC_NAL_PREF_SEI:
  627. case HEVC_NAL_SUFF_SEI: {
  628. struct nal * p_tmp = realloc(p_sei, sizeof(struct nal) * (i_sei + 1));
  629. if (!p_tmp)
  630. break;
  631. p_sei = p_tmp;
  632. p_nal = &p_sei[i_sei++];
  633. p_nal->p_buffer = p_buffer;
  634. if(i_sei == 1)
  635. i_num_arrays++;
  636. break;
  637. }
  638. default:
  639. p_nal = NULL;
  640. break;
  641. }
  642. }
  643. bo_add_8(hvcC, 0x01);
  644. bo_add_mem(hvcC, 12, general_configuration);
  645. /* Don't set min spatial segmentation */
  646. bo_add_16be(hvcC, 0xF000);
  647. /* Don't set parallelism type since segmentation isn't set */
  648. bo_add_8(hvcC, 0xFC);
  649. bo_add_8(hvcC, (0xFC | (i_chroma_idc & 0x03)));
  650. bo_add_8(hvcC, (0xF8 | (i_bit_depth_luma_minus8 & 0x07)));
  651. bo_add_8(hvcC, (0xF8 | (i_bit_depth_chroma_minus8 & 0x07)));
  652. /* Don't set framerate */
  653. bo_add_16be(hvcC, 0x0000);
  654. /* Force NAL size of 4 bytes that replace the startcode */
  655. bo_add_8(hvcC, (((i_numTemporalLayer & 0x07) << 3) |
  656. (b_temporalIdNested << 2) | 0x03));
  657. bo_add_8(hvcC, i_num_arrays);
  658. if (i_vps)
  659. {
  660. /* Write VPS */
  661. bo_add_8(hvcC, HEVC_NAL_VPS | (b_completeness ? 0x80 : 0));
  662. bo_add_16be(hvcC, i_vps);
  663. for (uint8_t i = 0; i < i_vps; i++) {
  664. p_nal = &rg_vps[i];
  665. bo_add_16be(hvcC, p_nal->i_buffer);
  666. bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
  667. }
  668. }
  669. if (i_sps) {
  670. /* Write SPS */
  671. bo_add_8(hvcC, HEVC_NAL_SPS | (b_completeness ? 0x80 : 0));
  672. bo_add_16be(hvcC, i_sps);
  673. for (uint8_t i = 0; i < i_sps; i++) {
  674. p_nal = &rg_sps[i];
  675. bo_add_16be(hvcC, p_nal->i_buffer);
  676. bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
  677. }
  678. }
  679. if (i_pps) {
  680. /* Write PPS */
  681. bo_add_8(hvcC, HEVC_NAL_PPS | (b_completeness ? 0x80 : 0));
  682. bo_add_16be(hvcC, i_pps);
  683. for (uint8_t i = 0; i < i_pps; i++) {
  684. p_nal = &rg_pps[i];
  685. bo_add_16be(hvcC, p_nal->i_buffer);
  686. bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
  687. }
  688. }
  689. if (i_sei) {
  690. /* Write SEI */
  691. bo_add_8(hvcC, HEVC_NAL_PREF_SEI | (b_completeness ? 0x80 : 0));
  692. bo_add_16be(hvcC, i_sei);
  693. for (size_t i = 0; i < i_sei; i++) {
  694. p_nal = &p_sei[i];
  695. bo_add_16be(hvcC, p_nal->i_buffer);
  696. bo_add_mem(hvcC, p_nal->i_buffer, p_nal->p_buffer);
  697. }
  698. }
  699. free(p_sei);
  700. return hvcC;
  701. }
  702. static bo_t *GetWaveFormatExTag(es_format_t *p_fmt, const char *tag)
  703. {
  704. bo_t *box = box_new(tag);
  705. if(!box)
  706. return NULL;
  707. uint16_t wFormatTag;
  708. fourcc_to_wf_tag(p_fmt->i_codec, &wFormatTag);
  709. bo_add_16le(box, wFormatTag); //wFormatTag
  710. bo_add_16le(box, p_fmt->audio.i_channels); //nChannels
  711. bo_add_32le(box, p_fmt->audio.i_rate); //nSamplesPerSec
  712. bo_add_32le(box, p_fmt->i_bitrate / 8); //nAvgBytesPerSec
  713. bo_add_16le(box, p_fmt->audio.i_blockalign); //nBlockAlign
  714. bo_add_16le(box, p_fmt->audio.i_bitspersample); //wBitsPerSample
  715. bo_add_16le(box, p_fmt->i_extra); //cbSize
  716. bo_add_mem(box, p_fmt->i_extra, p_fmt->p_extra);
  717. return box;
  718. }
  719. static bo_t *GetxxxxTag(es_format_t *p_fmt, const char *tag)
  720. {
  721. bo_t *box = box_new(tag);
  722. if(!box)
  723. return NULL;
  724. bo_add_mem(box, p_fmt->i_extra, p_fmt->p_extra);
  725. return box;
  726. }
  727. static bo_t *GetAvcCTag(es_format_t *p_fmt)
  728. {
  729. bo_t *avcC = box_new("avcC");/* FIXME use better value */
  730. if(!avcC)
  731. return NULL;
  732. const uint8_t *p_sps, *p_pps, *p_ext;
  733. size_t i_sps_size, i_pps_size, i_ext_size;
  734. if(! h264_AnnexB_get_spspps(p_fmt->p_extra, p_fmt->i_extra,
  735. &p_sps, &i_sps_size,
  736. &p_pps, &i_pps_size,
  737. &p_ext, &i_ext_size ) )
  738. {
  739. p_sps = p_pps = p_ext = NULL;
  740. i_sps_size = i_pps_size = i_ext_size = 0;
  741. }
  742. bo_add_8(avcC, 1); /* configuration version */
  743. bo_add_8(avcC, i_sps_size > 3 ? p_sps[1] : PROFILE_H264_MAIN);
  744. bo_add_8(avcC, i_sps_size > 3 ? p_sps[2] : 64);
  745. bo_add_8(avcC, i_sps_size > 3 ? p_sps[3] : 30); /* level, 5.1 */
  746. bo_add_8(avcC, 0xff); /* 0b11111100 | lengthsize = 0x11 */
  747. bo_add_8(avcC, 0xe0 | (i_sps_size > 0 ? 1 : 0)); /* 0b11100000 | sps_count */
  748. if (i_sps_size > 0) {
  749. bo_add_16be(avcC, i_sps_size);
  750. bo_add_mem(avcC, i_sps_size, p_sps);
  751. }
  752. bo_add_8(avcC, (i_pps_size > 0 ? 1 : 0)); /* pps_count */
  753. if (i_pps_size > 0) {
  754. bo_add_16be(avcC, i_pps_size);
  755. bo_add_mem(avcC, i_pps_size, p_pps);
  756. }
  757. if( i_sps_size > 3 &&
  758. (p_sps[1] == PROFILE_H264_HIGH ||
  759. p_sps[1] == PROFILE_H264_HIGH_10 ||
  760. p_sps[1] == PROFILE_H264_HIGH_422 ||
  761. p_sps[1] == PROFILE_H264_HIGH_444 ||
  762. p_sps[1] == PROFILE_H264_HIGH_444_PREDICTIVE) )
  763. {
  764. h264_sequence_parameter_set_t *p_spsdata = h264_decode_sps( p_sps, i_sps_size, true );
  765. if( p_spsdata )
  766. {
  767. uint8_t data[3];
  768. if( h264_get_chroma_luma( p_spsdata, &data[0], &data[1], &data[2]) )
  769. {
  770. bo_add_8(avcC, 0xFC | data[0]);
  771. bo_add_8(avcC, 0xF8 | (data[1] - 8));
  772. bo_add_8(avcC, 0xF8 | (data[2] - 8));
  773. bo_add_8(avcC, (i_ext_size > 0 ? 1 : 0));
  774. if (i_ext_size > 0) {
  775. bo_add_16be(avcC, i_ext_size);
  776. bo_add_mem(avcC, i_ext_size, p_ext);
  777. }
  778. }
  779. h264_release_sps( p_spsdata );
  780. }
  781. }
  782. return avcC;
  783. }
  784. /* TODO: No idea about these values */
  785. static bo_t *GetSVQ3Tag(es_format_t *p_fmt)
  786. {
  787. bo_t *smi = box_new("SMI ");
  788. if(!smi)
  789. return NULL;
  790. if (p_fmt->i_extra > 0x4e) {
  791. uint8_t *p_end = &((uint8_t*)p_fmt->p_extra)[p_fmt->i_extra];
  792. uint8_t *p = &((uint8_t*)p_fmt->p_extra)[0x46];
  793. while (p + 8 < p_end) {
  794. int i_size = GetDWBE(p);
  795. if (i_size <= 1) /* FIXME handle 1 as long size */
  796. break;
  797. if (!strncmp((const char *)&p[4], "SMI ", 4)) {
  798. bo_add_mem(smi, p_end - p - 8, &p[8]);
  799. return smi;
  800. }
  801. p += i_size;
  802. }
  803. }
  804. /* Create a dummy one in fallback */
  805. bo_add_fourcc(smi, "SEQH");
  806. bo_add_32be(smi, 0x5);
  807. bo_add_32be(smi, 0xe2c0211d);
  808. bo_add_8(smi, 0xc0);
  809. return smi;
  810. }
  811. static bo_t *GetUdtaTag(mp4mux_trackinfo_t **pp_tracks, unsigned int i_tracks)
  812. {
  813. bo_t *udta = box_new("udta");
  814. if (!udta)
  815. return NULL;
  816. /* Requirements */
  817. for (unsigned int i = 0; i < i_tracks; i++) {
  818. mp4mux_trackinfo_t *p_stream = pp_tracks[i];
  819. vlc_fourcc_t codec = p_stream->fmt.i_codec;
  820. if (codec == VLC_CODEC_MP4V || codec == VLC_CODEC_MP4A) {
  821. bo_t *box = box_new("\251req");
  822. if(!box)
  823. break;
  824. /* String length */
  825. bo_add_16be(box, sizeof("QuickTime 6.0 or greater") - 1);
  826. bo_add_16be(box, 0);
  827. bo_add_mem(box, sizeof("QuickTime 6.0 or greater") - 1,
  828. (uint8_t *)"QuickTime 6.0 or greater");
  829. box_gather(udta, box);
  830. break;
  831. }
  832. }
  833. /* Encoder */
  834. {
  835. bo_t *box = box_new("\251enc");
  836. if(box)
  837. {
  838. /* String length */
  839. bo_add_16be(box, sizeof(PACKAGE_STRING " stream output") - 1);
  840. bo_add_16be(box, 0);
  841. bo_add_mem(box, sizeof(PACKAGE_STRING " stream output") - 1,
  842. (uint8_t*)PACKAGE_STRING " stream output");
  843. box_gather(udta, box);
  844. }
  845. }
  846. #if 0
  847. /* Misc atoms */
  848. vlc_meta_t *p_meta = p_mux->p_sout->p_meta;
  849. if (p_meta) {
  850. #define ADD_META_BOX(type, box_string) { \
  851. bo_t *box = NULL; \
  852. if (vlc_meta_Get(p_meta, vlc_meta_##type)) \
  853. box = box_new("\251" box_string); \
  854. if (box) { \
  855. bo_add_16be(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type))); \
  856. bo_add_16be(box, 0); \
  857. bo_add_mem(box, strlen(vlc_meta_Get(p_meta, vlc_meta_##type)), \
  858. (uint8_t*)(vlc_meta_Get(p_meta, vlc_meta_##type))); \
  859. box_gather(udta, box); \
  860. } }
  861. ADD_META_BOX(Title, "nam");
  862. ADD_META_BOX(Artist, "ART");
  863. ADD_META_BOX(Genre, "gen");
  864. ADD_META_BOX(Copyright, "cpy");
  865. ADD_META_BOX(Description, "des");
  866. ADD_META_BOX(Date, "day");
  867. ADD_META_BOX(URL, "url");
  868. #undef ADD_META_BOX
  869. }
  870. #endif
  871. return udta;
  872. }
  873. static bo_t *GetSounBox(vlc_object_t *p_obj, mp4mux_trackinfo_t *p_track, bool b_mov)
  874. {
  875. VLC_UNUSED(p_obj);
  876. bool b_descr = true;
  877. vlc_fourcc_t codec = p_track->fmt.i_codec;
  878. char fcc[4];
  879. if (codec == VLC_CODEC_MPGA) {
  880. if (b_mov) {
  881. b_descr = false;
  882. memcpy(fcc, ".mp3", 4);
  883. } else
  884. memcpy(fcc, "mp4a", 4);
  885. } else if (codec == VLC_CODEC_A52) {
  886. memcpy(fcc, "ac-3", 4);
  887. } else if (codec == VLC_CODEC_EAC3) {
  888. memcpy(fcc, "ec-3", 4);
  889. } else if (codec == VLC_CODEC_DTS) {
  890. memcpy(fcc, "DTS ", 4);
  891. } else if (codec == VLC_CODEC_WMAP) {
  892. memcpy(fcc, "wma ", 4);
  893. } else
  894. vlc_fourcc_to_char(codec, fcc);
  895. bo_t *soun = box_new(fcc);
  896. if(!soun)
  897. return NULL;
  898. for (int i = 0; i < 6; i++)
  899. bo_add_8(soun, 0); // reserved;
  900. bo_add_16be(soun, 1); // data-reference-index
  901. /* SoundDescription */
  902. if (b_mov && codec == VLC_CODEC_MP4A)
  903. bo_add_16be(soun, 1); // version 1;
  904. else
  905. bo_add_16be(soun, 0); // version 0;
  906. bo_add_16be(soun, 0); // revision level (0)
  907. bo_add_32be(soun, 0); // vendor
  908. // channel-count
  909. bo_add_16be(soun, p_track->fmt.audio.i_channels);
  910. // sample size
  911. bo_add_16be(soun, p_track->fmt.audio.i_bitspersample ?
  912. p_track->fmt.audio.i_bitspersample : 16);
  913. bo_add_16be(soun, -2); // compression id
  914. bo_add_16be(soun, 0); // packet size (0)
  915. bo_add_16be(soun, p_track->fmt.audio.i_rate); // sampleratehi
  916. bo_add_16be(soun, 0); // sampleratelo
  917. /* Extended data for SoundDescription V1 */
  918. if (b_mov && p_track->fmt.i_codec == VLC_CODEC_MP4A) {
  919. /* samples per packet */
  920. bo_add_32be(soun, p_track->fmt.audio.i_frame_length);
  921. bo_add_32be(soun, 1536); /* bytes per packet */
  922. bo_add_32be(soun, 2); /* bytes per frame */
  923. /* bytes per sample */
  924. bo_add_32be(soun, 2 /*p_fmt->audio.i_bitspersample/8 */);
  925. }
  926. /* Add an ES Descriptor */
  927. if (b_descr) {
  928. bo_t *box;
  929. if (b_mov && codec == VLC_CODEC_MP4A)
  930. box = GetWaveTag(p_track);
  931. else if (codec == VLC_CODEC_AMR_NB)
  932. box = GetDamrTag(&p_track->fmt);
  933. else if (codec == VLC_CODEC_A52)
  934. box = GetDac3Tag(p_track->a52_frame);
  935. else if (codec == VLC_CODEC_EAC3)
  936. box = GetDec3Tag(&p_track->fmt, p_track->a52_frame);
  937. else if (codec == VLC_CODEC_WMAP)
  938. box = GetWaveFormatExTag(&p_track->fmt, "wfex");
  939. else
  940. box = GetESDS(p_track);
  941. if (box)
  942. box_gather(soun, box);
  943. }
  944. return soun;
  945. }
  946. static bo_t *GetVideBox(vlc_object_t *p_obj, mp4mux_trackinfo_t *p_track, bool b_mov)
  947. {
  948. VLC_UNUSED(p_obj);
  949. VLC_UNUSED(b_mov);
  950. char fcc[4];
  951. switch(p_track->fmt.i_codec)
  952. {
  953. case VLC_CODEC_MP4V:
  954. case VLC_CODEC_MPGV: memcpy(fcc, "mp4v", 4); break;
  955. case VLC_CODEC_MJPG: memcpy(fcc, "mjpa", 4); break;
  956. case VLC_CODEC_SVQ1: memcpy(fcc, "SVQ1", 4); break;
  957. case VLC_CODEC_SVQ3: memcpy(fcc, "SVQ3", 4); break;
  958. case VLC_CODEC_H263: memcpy(fcc, "s263", 4); break;
  959. case VLC_CODEC_H264: memcpy(fcc, "avc1", 4); break;
  960. case VLC_CODEC_VC1 : memcpy(fcc, "vc-1", 4); break;
  961. /* FIXME: find a way to know if no non-VCL units are in the stream (->hvc1)
  962. * see 14496-15 8.4.1.1.1 */
  963. case VLC_CODEC_HEVC: memcpy(fcc, "hev1", 4); break;
  964. case VLC_CODEC_YV12: memcpy(fcc, "yv12", 4); break;
  965. case VLC_CODEC_YUYV: memcpy(fcc, "yuy2", 4); break;
  966. default:
  967. vlc_fourcc_to_char(p_track->fmt.i_codec, fcc);
  968. break;
  969. }
  970. bo_t *vide = box_new(fcc);
  971. if(!vide)
  972. return NULL;
  973. for (int i = 0; i < 6; i++)
  974. bo_add_8(vide, 0); // reserved;
  975. bo_add_16be(vide, 1); // data-reference-index
  976. bo_add_16be(vide, 0); // predefined;
  977. bo_add_16be(vide, 0); // reserved;
  978. for (int i = 0; i < 3; i++)
  979. bo_add_32be(vide, 0); // predefined;
  980. bo_add_16be(vide, p_track->fmt.video.i_width); // i_width
  981. bo_add_16be(vide, p_track->fmt.video.i_height); // i_height
  982. bo_add_32be(vide, 0x00480000); // h 72dpi
  983. bo_add_32be(vide, 0x00480000); // v 72dpi
  984. bo_add_32be(vide, 0); // data size, always 0
  985. bo_add_16be(vide, 1); // frames count per sample
  986. // compressor name;
  987. for (int i = 0; i < 32; i++)
  988. bo_add_8(vide, 0);
  989. bo_add_16be(vide, 0x18); // depth
  990. bo_add_16be(vide, 0xffff); // predefined
  991. /* add an ES Descriptor */
  992. switch(p_track->fmt.i_codec)
  993. {
  994. case VLC_CODEC_MP4V:
  995. case VLC_CODEC_MPGV:
  996. box_gather(vide, GetESDS(p_track));
  997. break;
  998. case VLC_CODEC_H263:
  999. box_gather(vide, GetD263Tag());
  1000. break;
  1001. case VLC_CODEC_SVQ3:
  1002. box_gather(vide, GetSVQ3Tag(&p_track->fmt));
  1003. break;
  1004. case VLC_CODEC_H264:
  1005. box_gather(vide, GetAvcCTag(&p_track->fmt));
  1006. break;
  1007. case VLC_CODEC_VC1:
  1008. box_gather(vide, GetxxxxTag(&p_track->fmt, "dvc1"));
  1009. break;
  1010. case VLC_CODEC_HEVC:
  1011. /* Write HvcC without forcing VPS/SPS/PPS/SEI array_completeness */
  1012. box_gather(vide, GetHvcCTag(&p_track->fmt, false));
  1013. break;
  1014. }
  1015. return vide;
  1016. }
  1017. static bo_t *GetTextBox(void)
  1018. {
  1019. bo_t *text = box_new("text");
  1020. if(!text)
  1021. return NULL;
  1022. for (int i = 0; i < 6; i++)
  1023. bo_add_8(text, 0); // reserved;
  1024. bo_add_16be(text, 1); // data-reference-index
  1025. bo_add_32be(text, 0); // display flags
  1026. bo_add_32be(text, 0); // justification
  1027. for (int i = 0; i < 3; i++)
  1028. bo_add_16be(text, 0); // back ground color
  1029. bo_add_16be(text, 0); // box text
  1030. bo_add_16be(text, 0); // box text
  1031. bo_add_16be(text, 0); // box text
  1032. bo_add_16be(text, 0); // box text
  1033. bo_add_64be(text, 0); // reserved
  1034. for (int i = 0; i < 3; i++)
  1035. bo_add_16be(text, 0xff); // foreground color
  1036. bo_add_8 (text, 9);
  1037. bo_add_mem(text, 9, (uint8_t*)"Helvetica");
  1038. return text;
  1039. }
  1040. static int64_t GetScaledEntryDuration( const mp4mux_entry_t *p_entry, uint32_t i_timescale,
  1041. mtime_t *pi_total_mtime, int64_t *pi_total_scaled )
  1042. {
  1043. const mtime_t i_totalscaledtototalmtime = *pi_total_scaled * CLOCK_FREQ / i_timescale;
  1044. const mtime_t i_diff = *pi_total_mtime - i_totalscaledtototalmtime;
  1045. /* Ensure to compensate the drift due to loss from time, and from scale, conversions */
  1046. int64_t i_scaled = (p_entry->i_length + i_diff) * i_timescale / CLOCK_FREQ;
  1047. *pi_total_mtime += p_entry->i_length;
  1048. *pi_total_scaled += i_scaled;
  1049. return i_scaled;
  1050. }
  1051. static bo_t *GetStblBox(vlc_object_t *p_obj, mp4mux_trackinfo_t *p_track, bool b_mov, bool b_stco64)
  1052. {
  1053. /* sample description */
  1054. bo_t *stsd = box_full_new("stsd", 0, 0);
  1055. if(!stsd)
  1056. return NULL;
  1057. bo_add_32be(stsd, 1);
  1058. if (p_track->fmt.i_cat == AUDIO_ES)
  1059. box_gather(stsd, GetSounBox(p_obj, p_track, b_mov));
  1060. else if (p_track->fmt.i_cat == VIDEO_ES)
  1061. box_gather(stsd, GetVideBox(p_obj, p_track, b_mov));
  1062. else if (p_track->fmt.i_cat == SPU_ES)
  1063. box_gather(stsd, GetTextBox());
  1064. /* chunk offset table */
  1065. bo_t *stco;
  1066. if (b_stco64) {
  1067. /* 64 bits version */
  1068. stco = box_full_new("co64", 0, 0);
  1069. } else {
  1070. /* 32 bits version */
  1071. stco = box_full_new("stco", 0, 0);
  1072. }
  1073. if(!stco)
  1074. {
  1075. bo_free(stsd);
  1076. return NULL;
  1077. }
  1078. bo_add_32be(stco, 0); // entry-count (fixed latter)
  1079. /* sample to chunk table */
  1080. bo_t *stsc = box_full_new("stsc", 0, 0);
  1081. if(!stsc)
  1082. {
  1083. bo_free(stco);
  1084. bo_free(stsd);
  1085. return NULL;
  1086. }
  1087. bo_add_32be(stsc, 0); // entry-count (fixed latter)
  1088. unsigned i_chunk = 0;
  1089. unsigned i_stsc_last_val = 0, i_stsc_entries = 0;
  1090. for (unsigned i = 0; i < p_track->i_entry_count; i_chunk++) {
  1091. mp4mux_entry_t *entry = p_track->entry;
  1092. int i_first = i;
  1093. if (b_stco64)
  1094. bo_add_64be(stco, entry[i].i_pos);
  1095. else
  1096. bo_add_32be(stco, entry[i].i_pos);
  1097. for (; i < p_track->i_entry_count; i++)
  1098. if (i >= p_track->i_entry_count - 1 ||
  1099. entry[i].i_pos + entry[i].i_size != entry[i+1].i_pos) {
  1100. i++;
  1101. break;
  1102. }
  1103. /* Add entry to the stsc table */
  1104. if (i_stsc_last_val != i - i_first) {
  1105. bo_add_32be(stsc, 1 + i_chunk); // first-chunk
  1106. bo_add_32be(stsc, i - i_first) ; // samples-per-chunk
  1107. bo_add_32be(stsc, 1); // sample-descr-index
  1108. i_stsc_last_val = i - i_first;
  1109. i_stsc_entries++;
  1110. }
  1111. }
  1112. /* Fix stco entry count */
  1113. bo_swap_32be(stco, 12, i_chunk);
  1114. if(p_obj)
  1115. msg_Dbg(p_obj, "created %d chunks (stco)", i_chunk);
  1116. /* Fix stsc entry count */
  1117. bo_swap_32be(stsc, 12, i_stsc_entries );
  1118. /* add stts */
  1119. bo_t *stts = box_full_new("stts", 0, 0);
  1120. if(!stts)
  1121. {
  1122. bo_free(stsd);
  1123. bo_free(stco);
  1124. bo_free(stsc);
  1125. return NULL;
  1126. }
  1127. bo_add_32be(stts, 0); // entry-count (fixed latter)
  1128. mtime_t i_total_mtime = 0;
  1129. int64_t i_total_scaled = 0;
  1130. unsigned i_index = 0;
  1131. for (unsigned i = 0; i < p_track->i_entry_count; i_index++) {
  1132. int i_first = i;
  1133. int64_t i_scaled = GetScaledEntryDuration(&p_track->entry[i], p_track->i_timescale,
  1134. &i_total_mtime, &i_total_scaled);
  1135. for (unsigned j=i+1; j < p_track->i_entry_count; j++)
  1136. {
  1137. mtime_t i_total_mtime_next = i_total_mtime;
  1138. int64_t i_total_scaled_next = i_total_scaled;
  1139. int64_t i_scalednext = GetScaledEntryDuration(&p_track->entry[j], p_track->i_timescale,
  1140. &i_total_mtime_next, &i_total_scaled_next);
  1141. if( i_scalednext != i_scaled )
  1142. break;
  1143. i_total_mtime = i_total_mtime_next;
  1144. i_total_scaled = i_total_scaled_next;
  1145. i = j;
  1146. }
  1147. bo_add_32be(stts, ++i - i_first); // sample-count
  1148. bo_add_32be(stts, i_scaled); // sample-delta
  1149. }
  1150. bo_swap_32be(stts, 12, i_index);
  1151. //msg_Dbg(p_obj, "total sout duration %"PRId64" reconverted from scaled %"PRId64,
  1152. // i_total_mtime, i_total_scaled * CLOCK_FREQ / p_track->i_timescale );
  1153. /* composition time handling */
  1154. bo_t *ctts = NULL;
  1155. if ( p_track->b_hasbframes && (ctts = box_full_new("ctts", 0, 0)) )
  1156. {
  1157. bo_add_32be(ctts, 0);
  1158. i_index = 0;
  1159. for (unsigned i = 0; i < p_track->i_entry_count; i_index++)
  1160. {
  1161. int i_first = i;
  1162. mtime_t i_offset = p_track->entry[i].i_pts_dts;
  1163. for (; i < p_track->i_entry_count; ++i)
  1164. if (i == p_track->i_entry_count || p_track->entry[i].i_pts_dts != i_offset)
  1165. break;
  1166. bo_add_32be(ctts, i - i_first); // sample-count
  1167. bo_add_32be(ctts, i_offset * p_track->i_timescale / CLOCK_FREQ ); // sample-offset
  1168. }
  1169. bo_swap_32be(ctts, 12, i_index);
  1170. }
  1171. bo_t *stsz = box_full_new("stsz", 0, 0);
  1172. if(!stsz)
  1173. {
  1174. bo_free(stsd);
  1175. bo_free(stco);
  1176. bo_free(stts);
  1177. return NULL;
  1178. }
  1179. int i_size = 0;
  1180. for (unsigned i = 0; i < p_track->i_entry_count; i++)
  1181. {
  1182. if ( i == 0 )
  1183. i_size = p_track->entry[i].i_size;
  1184. else if ( p_track->entry[i].i_size != i_size )
  1185. {
  1186. i_size = 0;
  1187. break;
  1188. }
  1189. }
  1190. bo_add_32be(stsz, i_size); // sample-size
  1191. bo_add_32be(stsz, p_track->i_entry_count); // sample-count
  1192. if ( i_size == 0 ) // all samples have different size
  1193. {
  1194. for (unsigned i = 0; i < p_track->i_entry_count; i++)
  1195. bo_add_32be(stsz, p_track->entry[i].i_size); // sample-size
  1196. }
  1197. /* create stss table */
  1198. bo_t *stss = NULL;
  1199. i_index = 0;
  1200. if ( p_track->fmt.i_cat == VIDEO_ES || p_track->fmt.i_cat == AUDIO_ES )
  1201. {
  1202. mtime_t i_interval = -1;
  1203. for (unsigned i = 0; i < p_track->i_entry_count; i++)
  1204. {
  1205. if ( i_interval != -1 )
  1206. {
  1207. i_interval += p_track->entry[i].i_length + p_track->entry[i].i_pts_dts;
  1208. if ( i_interval < CLOCK_FREQ * 2 )
  1209. continue;
  1210. }
  1211. if (p_track->entry[i].i_flags & BLOCK_FLAG_TYPE_I) {
  1212. if (stss == NULL) {
  1213. stss = box_full_new("stss", 0, 0);
  1214. if(!stss)
  1215. break;
  1216. bo_add_32be(stss, 0); /* fixed later */
  1217. }
  1218. bo_add_32be(stss, 1 + i);
  1219. i_index++;
  1220. i_interval = 0;
  1221. }
  1222. }
  1223. }
  1224. if (stss)
  1225. bo_swap_32be(stss, 12, i_index);
  1226. /* Now gather all boxes into stbl */
  1227. bo_t *stbl = box_new("stbl");
  1228. if(!stbl)
  1229. {
  1230. bo_free(stsd);
  1231. bo_free(stco);
  1232. bo_free(stts);
  1233. bo_free(stsz);
  1234. bo_free(stss);
  1235. bo_free(ctts);
  1236. return NULL;
  1237. }
  1238. box_gather(stbl, stsd);
  1239. box_gather(stbl, stts);
  1240. if (stss)
  1241. box_gather(stbl, stss);
  1242. if (ctts)
  1243. box_gather(stbl, ctts);
  1244. box_gather(stbl, stsc);
  1245. box_gather(stbl, stsz);
  1246. p_track->i_stco_pos = stbl->b->i_buffer + 16;
  1247. box_gather(stbl, stco);
  1248. return stbl;
  1249. }
  1250. bo_t * mp4mux_GetMoovBox(vlc_object_t *p_obj, mp4mux_trackinfo_t **pp_tracks, unsigned int i_tracks,
  1251. int64_t i_movie_duration,
  1252. bool b_fragmented, bool b_mov, bool b_64_ext, bool b_stco64 )
  1253. {
  1254. bo_t *moov, *mvhd;
  1255. uint32_t i_movie_timescale = 90000;
  1256. int64_t i_timestamp = get_timestamp();
  1257. moov = box_new("moov");
  1258. if(!moov)
  1259. return NULL;
  1260. /* Create general info */
  1261. if( i_movie_duration == 0 && !b_fragmented )
  1262. {
  1263. for (unsigned int i = 0; i < i_tracks; i++) {
  1264. mp4mux_trackinfo_t *p_stream = pp_tracks[i];
  1265. i_movie_duration = __MAX(i_movie_duration, p_stream->i_read_duration);
  1266. }
  1267. if(p_obj)
  1268. msg_Dbg(p_obj, "movie duration %"PRId64"s", i_movie_duration / CLOCK_FREQ);
  1269. i_movie_duration = i_movie_duration * i_movie_timescale / CLOCK_FREQ;
  1270. }
  1271. /* *** add /moov/mvhd *** */
  1272. if (!b_64_ext) {
  1273. mvhd = box_full_new("mvhd", 0, 0);
  1274. if(!mvhd)
  1275. {
  1276. bo_free(moov);
  1277. return NULL;
  1278. }
  1279. bo_add_32be(mvhd, i_timestamp); // creation time
  1280. bo_add_32be(mvhd, i_timestamp); // modification time
  1281. bo_add_32be(mvhd, i_movie_timescale); // timescale
  1282. bo_add_32be(mvhd, i_movie_duration); // duration
  1283. } else {
  1284. mvhd = box_full_new("mvhd", 1, 0);
  1285. if(!mvhd)
  1286. {
  1287. bo_free(moov);
  1288. return NULL;
  1289. }
  1290. bo_add_64be(mvhd, i_timestamp); // creation time
  1291. bo_add_64be(mvhd, i_timestamp); // modification time
  1292. bo_add_32be(mvhd, i_movie_timescale); // timescale
  1293. bo_add_64be(mvhd, i_movie_duration); // duration
  1294. }
  1295. bo_add_32be(mvhd, 0x10000); // rate
  1296. bo_add_16be(mvhd, 0x100); // volume
  1297. bo_add_16be(mvhd, 0); // reserved
  1298. for (int i = 0; i < 2; i++)
  1299. bo_add_32be(mvhd, 0); // reserved
  1300. uint32_t mvhd_matrix[9] = { 0x10000, 0, 0, 0, 0x10000, 0, 0, 0, 0x40000000 };
  1301. for (int i = 0; i < 9; i++)
  1302. bo_add_32be(mvhd, mvhd_matrix[i]);// matrix
  1303. for (int i = 0; i < 6; i++)
  1304. bo_add_32be(mvhd, 0); // pre-defined
  1305. /* Next available track id */
  1306. bo_add_32be(mvhd, (i_tracks) ? pp_tracks[i_tracks -1]->i_track_id + 1: 1); // next-track-id
  1307. box_gather(moov, mvhd);
  1308. for (unsigned int i_trak = 0; i_trak < i_tracks; i_trak++) {
  1309. mp4mux_trackinfo_t *p_stream = pp_tracks[i_trak];
  1310. mtime_t i_stream_duration;
  1311. if ( !b_fragmented )
  1312. i_stream_duration = p_stream->i_read_duration * i_movie_timescale / CLOCK_FREQ;
  1313. else
  1314. i_stream_duration = 0;
  1315. /* *** add /moov/trak *** */
  1316. bo_t *trak = box_new("trak");
  1317. if(!trak)
  1318. continue;
  1319. /* *** add /moov/trak/tkhd *** */
  1320. bo_t *tkhd;
  1321. if (!b_64_ext) {
  1322. if (b_mov)
  1323. tkhd = box_full_new("tkhd", 0, 0x0f);
  1324. else
  1325. tkhd = box_full_new("tkhd", 0, 1);
  1326. if(!tkhd)
  1327. {
  1328. bo_free(trak);
  1329. continue;
  1330. }
  1331. bo_add_32be(tkhd, i_timestamp); // creation time
  1332. bo_add_32be(tkhd, i_timestamp); // modification time
  1333. bo_add_32be(tkhd, p_stream->i_track_id);
  1334. bo_add_32be(tkhd, 0); // reserved 0
  1335. bo_add_32be(tkhd, i_stream_duration); // duration
  1336. } else {
  1337. if (b_mov)
  1338. tkhd = box_full_new("tkhd", 1, 0x0f);
  1339. else
  1340. tkhd = box_full_new("tkhd", 1, 1);
  1341. if(!tkhd)
  1342. {
  1343. bo_free(trak);
  1344. continue;
  1345. }
  1346. bo_add_64be(tkhd, i_timestamp); // creation time
  1347. bo_add_64be(tkhd, i_timestamp); // modification time
  1348. bo_add_32be(tkhd, p_stream->i_track_id);
  1349. bo_add_32be(tkhd, 0); // reserved 0
  1350. bo_add_64be(tkhd, i_stream_duration); // duration
  1351. }
  1352. for (int i = 0; i < 2; i++)
  1353. bo_add_32be(tkhd, 0); // reserved
  1354. bo_add_16be(tkhd, 0); // layer
  1355. bo_add_16be(tkhd, 0); // pre-defined
  1356. // volume
  1357. bo_add_16be(tkhd, p_stream->fmt.i_cat == AUDIO_ES ? 0x100 : 0);
  1358. bo_add_16be(tkhd, 0); // reserved
  1359. matrix_apply_rotation(&p_stream->fmt, mvhd_matrix);
  1360. for (int i = 0; i < 9; i++)
  1361. bo_add_32be(tkhd, mvhd_matrix[i]); // matrix
  1362. if (p_stream->fmt.i_cat == AUDIO_ES) {
  1363. bo_add_32be(tkhd, 0); // width (presentation)
  1364. bo_add_32be(tkhd, 0); // height(presentation)
  1365. } else if (p_stream->fmt.i_cat == VIDEO_ES) {
  1366. int i_width = p_stream->fmt.video.i_width << 16;
  1367. if (p_stream->fmt.video.i_sar_num > 0 && p_stream->fmt.video.i_sar_den > 0) {
  1368. i_width = (int64_t)p_stream->fmt.video.i_sar_num *
  1369. ((int64_t)p_stream->fmt.video.i_width << 16) /
  1370. p_stream->fmt.video.i_sar_den;
  1371. }
  1372. // width (presentation)
  1373. bo_add_32be(tkhd, i_width);
  1374. // height(presentation)
  1375. bo_add_32be(tkhd, p_stream->fmt.video.i_height << 16);
  1376. } else {
  1377. int i_width = 320 << 16;
  1378. int i_height = 200;
  1379. for (unsigned int i = 0; i < i_tracks; i++) {
  1380. mp4mux_trackinfo_t *tk = pp_tracks[i];
  1381. if (tk->fmt.i_cat == VIDEO_ES) {
  1382. if (tk->fmt.video.i_sar_num > 0 &&
  1383. tk->fmt.video.i_sar_den > 0)
  1384. i_width = (int64_t)tk->fmt.video.i_sar_num *
  1385. ((int64_t)tk->fmt.video.i_width << 16) /
  1386. tk->fmt.video.i_sar_den;
  1387. else
  1388. i_width = tk->fmt.video.i_width << 16;
  1389. i_height = tk->fmt.video.i_height;
  1390. break;
  1391. }
  1392. }
  1393. bo_add_32be(tkhd, i_width); // width (presentation)
  1394. bo_add_32be(tkhd, i_height << 16); // height(presentation)
  1395. }
  1396. box_gather(trak, tkhd);
  1397. /* *** add /moov/trak/edts and elst */
  1398. bo_t *edts = GetEDTS(p_stream, i_movie_timescale, b_64_ext);
  1399. if(edts)
  1400. box_gather(trak, edts);
  1401. /* *** add /moov/trak/mdia *** */
  1402. bo_t *mdia = box_new("mdia");
  1403. if(!mdia)
  1404. {
  1405. bo_free(trak);
  1406. continue;
  1407. }
  1408. /* media header */
  1409. bo_t *mdhd;
  1410. if (!b_64_ext) {
  1411. mdhd = box_full_new("mdhd", 0, 0);
  1412. if(!mdhd)
  1413. {
  1414. bo_free(mdia);
  1415. bo_free(trak);
  1416. continue;
  1417. }
  1418. bo_add_32be(mdhd, i_timestamp); // creation time
  1419. bo_add_32be(mdhd, i_timestamp); // modification time
  1420. bo_add_32be(mdhd, p_stream->i_timescale); // timescale
  1421. bo_add_32be(mdhd, i_stream_duration * p_stream->i_timescale / i_movie_timescale); // duration
  1422. } else {
  1423. mdhd = box_full_new("mdhd", 1, 0);
  1424. if(!mdhd)
  1425. {
  1426. bo_free(mdia);
  1427. bo_free(trak);
  1428. continue;
  1429. }
  1430. bo_add_64be(mdhd, i_timestamp); // creation time
  1431. bo_add_64be(mdhd, i_timestamp); // modification time
  1432. bo_add_32be(mdhd, p_stream->i_timescale); // timescale
  1433. bo_add_64be(mdhd, i_stream_duration * p_stream->i_timescale / i_movie_timescale); // duration
  1434. }
  1435. if (p_stream->fmt.psz_language) {
  1436. char *psz = p_stream->fmt.psz_language;
  1437. const iso639_lang_t *pl = NULL;
  1438. uint16_t lang = 0x0;
  1439. if (strlen(psz) == 2)
  1440. pl = GetLang_1(psz);
  1441. else if (strlen(psz) == 3) {
  1442. pl = GetLang_2B(psz);
  1443. if (!strcmp(pl->psz_iso639_1, "??"))
  1444. pl = GetLang_2T(psz);
  1445. }
  1446. if (pl && strcmp(pl->psz_iso639_1, "??"))
  1447. lang = ((pl->psz_iso639_2T[0] - 0x60) << 10) |
  1448. ((pl->psz_iso639_2T[1] - 0x60) << 5) |
  1449. ((pl->psz_iso639_2T[2] - 0x60));
  1450. bo_add_16be(mdhd, lang); // language
  1451. } else
  1452. bo_add_16be(mdhd, 0 ); // language
  1453. bo_add_16be(mdhd, 0 ); // predefined
  1454. box_gather(mdia, mdhd);
  1455. /* handler reference */
  1456. bo_t *hdlr = box_full_new("hdlr", 0, 0);
  1457. if(!hdlr)
  1458. {
  1459. bo_free(mdia);
  1460. bo_free(trak);
  1461. continue;
  1462. }
  1463. if (b_mov)
  1464. bo_add_fourcc(hdlr, "mhlr"); // media handler
  1465. else
  1466. bo_add_32be(hdlr, 0);
  1467. if (p_stream->fmt.i_cat == AUDIO_ES)
  1468. bo_add_fourcc(hdlr, "soun");
  1469. else if (p_stream->fmt.i_cat == VIDEO_ES)
  1470. bo_add_fourcc(hdlr, "vide");
  1471. else if (p_stream->fmt.i_cat == SPU_ES)
  1472. bo_add_fourcc(hdlr, "text");
  1473. bo_add_32be(hdlr, 0); // reserved
  1474. bo_add_32be(hdlr, 0); // reserved
  1475. bo_add_32be(hdlr, 0); // reserved
  1476. if (b_mov)
  1477. bo_add_8(hdlr, 12); /* Pascal string for .mov */
  1478. if (p_stream->fmt.i_cat == AUDIO_ES)
  1479. bo_add_mem(hdlr, 12, (uint8_t*)"SoundHandler");
  1480. else if (p_stream->fmt.i_cat == VIDEO_ES)
  1481. bo_add_mem(hdlr, 12, (uint8_t*)"VideoHandler");
  1482. else
  1483. bo_add_mem(hdlr, 12, (uint8_t*)"Text Handler");
  1484. if (!b_mov)
  1485. bo_add_8(hdlr, 0); /* asciiz string for .mp4, yes that's BRAIN DAMAGED F**K MP4 */
  1486. box_gather(mdia, hdlr);
  1487. /* minf*/
  1488. bo_t *minf = box_new("minf");
  1489. if(!minf)
  1490. {
  1491. bo_free(mdia);
  1492. bo_free(trak);
  1493. continue;
  1494. }
  1495. /* add smhd|vmhd */
  1496. if (p_stream->fmt.i_cat == AUDIO_ES) {
  1497. bo_t *smhd = box_full_new("smhd", 0, 0);
  1498. if(smhd)
  1499. {
  1500. bo_add_16be(smhd, 0); // balance
  1501. bo_add_16be(smhd, 0); // reserved
  1502. box_gather(minf, smhd);
  1503. }
  1504. } else if (p_stream->fmt.i_cat == VIDEO_ES) {
  1505. bo_t *vmhd = box_full_new("vmhd", 0, 1);
  1506. if(vmhd)
  1507. {
  1508. bo_add_16be(vmhd, 0); // graphicsmode
  1509. for (int i = 0; i < 3; i++)
  1510. bo_add_16be(vmhd, 0); // opcolor
  1511. box_gather(minf, vmhd);
  1512. }
  1513. } else if (p_stream->fmt.i_cat == SPU_ES) {
  1514. bo_t *gmin = box_full_new("gmin", 0, 1);
  1515. if(gmin)
  1516. {
  1517. bo_add_16be(gmin, 0); // graphicsmode
  1518. for (int i = 0; i < 3; i++)
  1519. bo_add_16be(gmin, 0); // opcolor
  1520. bo_add_16be(gmin, 0); // balance
  1521. bo_add_16be(gmin, 0); // reserved
  1522. bo_t *gmhd = box_new("gmhd");
  1523. if(gmhd)
  1524. {
  1525. box_gather(gmhd, gmin);
  1526. box_gather(minf, gmhd);
  1527. }
  1528. else bo_free(gmin);
  1529. }
  1530. }
  1531. /* dinf */
  1532. bo_t *dref = box_full_new("dref", 0, 0);
  1533. if(dref)
  1534. {
  1535. bo_add_32be(dref, 1);
  1536. bo_t *url = box_full_new("url ", 0, 0x01);
  1537. if(url)
  1538. box_gather(dref, url);
  1539. bo_t *dinf = box_new("dinf");
  1540. if(dinf)
  1541. {
  1542. box_gather(dinf, dref);
  1543. /* append dinf to mdia */
  1544. box_gather(minf, dinf);
  1545. }
  1546. else bo_free(dref);
  1547. }
  1548. /* add stbl */
  1549. bo_t *stbl;
  1550. if ( b_fragmented )
  1551. {
  1552. uint32_t i_backup = p_stream->i_entry_count;
  1553. p_stream->i_entry_count = 0;
  1554. stbl = GetStblBox(p_obj, p_stream, b_mov, b_stco64);
  1555. p_stream->i_entry_count = i_backup;
  1556. }
  1557. else
  1558. stbl = GetStblBox(p_obj, p_stream, b_mov, b_stco64);
  1559. /* append stbl to minf */
  1560. p_stream->i_stco_pos += minf->b->i_buffer;
  1561. box_gather(minf, stbl);
  1562. /* append minf to mdia */
  1563. p_stream->i_stco_pos += mdia->b->i_buffer;
  1564. box_gather(mdia, minf);
  1565. /* append mdia to trak */
  1566. p_stream->i_stco_pos += trak->b->i_buffer;
  1567. box_gather(trak, mdia);
  1568. /* append trak to moov */
  1569. p_stream->i_stco_pos += moov->b->i_buffer;
  1570. box_gather(moov, trak);
  1571. }
  1572. /* Add user data tags */
  1573. box_gather(moov, GetUdtaTag(pp_tracks, i_tracks));
  1574. if ( b_fragmented )
  1575. {
  1576. bo_t *mvex = box_new("mvex");
  1577. if( mvex )
  1578. {
  1579. if( i_movie_duration )
  1580. {
  1581. bo_t *mehd = box_full_new("mehd", b_64_ext ? 1 : 0, 0);
  1582. if(mehd)
  1583. {
  1584. if(b_64_ext)
  1585. bo_add_64be(mehd, i_movie_duration * i_movie_timescale / CLOCK_FREQ);
  1586. else
  1587. bo_add_32be(mehd, i_movie_duration * i_movie_timescale / CLOCK_FREQ);
  1588. box_gather(mvex, mehd);
  1589. }
  1590. }
  1591. for (unsigned int i_trak = 0; mvex && i_trak < i_tracks; i_trak++)
  1592. {
  1593. mp4mux_trackinfo_t *p_stream = pp_tracks[i_trak];
  1594. /* Try to find some defaults */
  1595. if ( p_stream->i_entry_count )
  1596. {
  1597. // FIXME: find highest occurence
  1598. p_stream->i_trex_default_length = p_stream->entry[0].i_length;
  1599. p_stream->i_trex_default_size = p_stream->entry[0].i_size;
  1600. }
  1601. /* *** add /mvex/trex *** */
  1602. bo_t *trex = box_full_new("trex", 0, 0);
  1603. bo_add_32be(trex, p_stream->i_track_id);
  1604. bo_add_32be(trex, 1); // sample desc index
  1605. bo_add_32be(trex, (uint64_t)p_stream->i_trex_default_length * p_stream->i_timescale / CLOCK_FREQ); // sample duration
  1606. bo_add_32be(trex, p_stream->i_trex_default_size); // sample size
  1607. bo_add_32be(trex, 0); // sample flags
  1608. box_gather(mvex, trex);
  1609. }
  1610. box_gather(moov, mvex);
  1611. }
  1612. }
  1613. if(moov->b)
  1614. box_fix(moov, moov->b->i_buffer);
  1615. return moov;
  1616. }
  1617. bo_t *mp4mux_GetFtyp(vlc_fourcc_t major, uint32_t minor, vlc_fourcc_t extra[], size_t i_fourcc)
  1618. {
  1619. bo_t *box = box_new("ftyp");
  1620. if(box)
  1621. {
  1622. bo_add_fourcc(box, &major);
  1623. bo_add_32be (box, minor);
  1624. for(size_t i=0; i<i_fourcc; i++)
  1625. bo_add_fourcc(box, &extra[i]);
  1626. if(!box->b)
  1627. {
  1628. free(box);
  1629. return NULL;
  1630. }
  1631. box_fix(box, box->b->i_buffer);
  1632. }
  1633. return box;
  1634. }
  1635. bool mp4mux_CanMux(vlc_object_t *p_obj, const es_format_t *p_fmt)
  1636. {
  1637. switch(p_fmt->i_codec)
  1638. {
  1639. case VLC_CODEC_A52:
  1640. case VLC_CODEC_DTS:
  1641. case VLC_CODEC_EAC3:
  1642. case VLC_CODEC_MP4A:
  1643. case VLC_CODEC_MP4V:
  1644. case VLC_CODEC_MPGA:
  1645. case VLC_CODEC_MP3:
  1646. case VLC_CODEC_MPGV:
  1647. case VLC_CODEC_MP2V:
  1648. case VLC_CODEC_MP1V:
  1649. case VLC_CODEC_MJPG:
  1650. case VLC_CODEC_MJPGB:
  1651. case VLC_CODEC_SVQ1:
  1652. case VLC_CODEC_SVQ3:
  1653. case VLC_CODEC_H263:
  1654. case VLC_CODEC_AMR_NB:
  1655. case VLC_CODEC_AMR_WB:
  1656. case VLC_CODEC_YV12:
  1657. case VLC_CODEC_YUYV:
  1658. case VLC_CODEC_VC1:
  1659. case VLC_CODEC_WMAP:
  1660. break;
  1661. case VLC_CODEC_H264:
  1662. if(!p_fmt->i_extra && p_obj)
  1663. msg_Warn(p_obj, "H264 muxing from AnnexB source will set an incorrect default profile");
  1664. break;
  1665. case VLC_CODEC_HEVC:
  1666. if(!p_fmt->i_extra && p_obj)
  1667. msg_Err(p_obj, "HEVC muxing from AnnexB source is unsupported");
  1668. return false;
  1669. case VLC_CODEC_SUBT:
  1670. if(p_obj)
  1671. msg_Warn(p_obj, "subtitle track added like in .mov (even when creating .mp4)");
  1672. break;
  1673. default:
  1674. return false;
  1675. }
  1676. return true;
  1677. }