1
0

mp4meta.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is MPEG4IP.
  13. *
  14. * The Initial Developer of the Original Code is Cisco Systems Inc.
  15. * Portions created by Cisco Systems Inc. are
  16. * Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. * M. Bakker mbakker at nero.com
  20. *
  21. * Apple iTunes Metadata handling
  22. */
  23. /**
  24. The iTunes tagging seems to support any tag field name
  25. but there are some predefined fields, also known from the QuickTime format
  26. predefined fields (the ones I know of until now):
  27. - ©nam : Name of the song/movie (string)
  28. - ©ART : Name of the artist/performer (string)
  29. - aART : Album artist
  30. - ©wrt : Name of the writer (string)
  31. - ©alb : Name of the album (string)
  32. - ©day : Year (4 bytes, e.g. "2003") (string)
  33. - ©too : Tool(s) used to create the file (string)
  34. - ©cmt : Comment (string)
  35. - ©gen : Custom genre (string)
  36. - ©grp : Grouping (string)
  37. - trkn : Tracknumber (8 byte string)
  38. 16 bit: empty
  39. 16 bit: tracknumber
  40. 16 bit: total tracks on album
  41. 16 bit: empty
  42. - disk : Disknumber (8 byte string)
  43. 16 bit: empty
  44. 16 bit: disknumber
  45. 16 bit: total number of disks
  46. 16 bit: empty
  47. - gnre : Genre (16 bit genre) (ID3v1 index + 1)
  48. - cpil : Part of a compilation (1 byte, 1 or 0)
  49. - tmpo : Tempo in BPM (16 bit)
  50. - covr : Cover art (xx bytes binary data)
  51. - ---- : Free form metadata, can have any name and any data
  52. - pgap : gapless - 8 bit boolean
  53. - apID : purchaser name.
  54. - cprt : copyright
  55. - purd : purchase date.
  56. **/
  57. #include "mp4common.h"
  58. bool MP4File::GetMetadataByIndex(u_int32_t index,
  59. char** ppName,
  60. u_int8_t** ppValue, u_int32_t* pValueSize)
  61. {
  62. char s[256];
  63. snprintf(s, 256, "moov.udta.meta.ilst.*[%u].data.metadata", index);
  64. GetBytesProperty(s, ppValue, pValueSize);
  65. snprintf(s, 256, "moov.udta.meta.ilst.*[%u]", index);
  66. MP4Atom* pParent = m_pRootAtom->FindAtomMP4(s);
  67. if (pParent == NULL) return false;
  68. /* check for free form tagfield */
  69. if (memcmp(*ppName, "----", 4) == 0)
  70. {
  71. u_int8_t* pV;
  72. u_int32_t VSize = 0;
  73. char *pN;
  74. snprintf(s, 256, "moov.udta.meta.ilst.*[%u].name.metadata", index);
  75. GetBytesProperty(s, &pV, &VSize);
  76. pN = (char*)malloc((VSize+1)*sizeof(char));
  77. if (pN != NULL) {
  78. memset(pN, 0, (VSize+1)*sizeof(char));
  79. memcpy(pN, pV, VSize*sizeof(char));
  80. }
  81. free(pV);
  82. *ppName = pN;
  83. } else {
  84. *ppName = _strdup(pParent->GetType());
  85. }
  86. return true;
  87. }
  88. bool MP4File::CreateMetadataAtom(const char* name)
  89. {
  90. char s[256];
  91. char t[256];
  92. snprintf(t, 256, "udta.meta.ilst.%s.data", name);
  93. snprintf(s, 256, "moov.udta.meta.ilst.%s.data", name);
  94. (void)AddDescendantAtoms("moov", t);
  95. MP4Atom *pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  96. if (!pMetaAtom)
  97. return false;
  98. /* some fields need special flags set */
  99. if ((uint8_t)name[0] == 0251 || ATOMID(name) == ATOMID("aART"))
  100. {
  101. pMetaAtom->SetFlags(0x1);
  102. } else if ((memcmp(name, "cpil", 4) == 0) || (memcmp(name, "tmpo", 4) == 0)) {
  103. pMetaAtom->SetFlags(0x15);
  104. }
  105. MP4Atom *pHdlrAtom = m_pRootAtom->FindAtomMP4("moov.udta.meta.hdlr");
  106. MP4StringProperty *pStringProperty = NULL;
  107. MP4BytesProperty *pBytesProperty = NULL;
  108. ASSERT(pHdlrAtom);
  109. ASSERT(pHdlrAtom->FindProperty("hdlr.handlerType",
  110. (MP4Property**)&pStringProperty));
  111. ASSERT(pStringProperty);
  112. pStringProperty->SetValue("mdir");
  113. u_int8_t val[12];
  114. memset(val, 0, 12*sizeof(u_int8_t));
  115. val[0] = 0x61;
  116. val[1] = 0x70;
  117. val[2] = 0x70;
  118. val[3] = 0x6c;
  119. ASSERT(pHdlrAtom->FindProperty("hdlr.reserved2",
  120. (MP4Property**)&pBytesProperty));
  121. ASSERT(pBytesProperty);
  122. pBytesProperty->SetReadOnly(false);
  123. pBytesProperty->SetValue(val, 12);
  124. pBytesProperty->SetReadOnly(true);
  125. return true;
  126. }
  127. bool MP4File::DeleteMetadataAtom(const char* name, bool try_udta)
  128. {
  129. MP4Atom *pMetaAtom = NULL;
  130. char s[256];
  131. snprintf(s, 256, "moov.udta.meta.ilst.%s", name);
  132. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  133. if (pMetaAtom == NULL && try_udta) {
  134. snprintf(s, 256, "moov.udta.%s", name);
  135. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  136. }
  137. /* if it exists, delete it */
  138. if (pMetaAtom)
  139. {
  140. MP4Atom *pParent = pMetaAtom->GetParentAtom();
  141. pParent->DeleteChildAtom(pMetaAtom);
  142. delete pMetaAtom;
  143. return true;
  144. }
  145. return false;
  146. }
  147. bool MP4File::SetMetadataString (const char *atom, const char *value)
  148. {
  149. char atomstring[40];
  150. MP4Atom *pMetaAtom;
  151. MP4BytesProperty *pMetadataProperty = NULL;
  152. snprintf(atomstring, 40, "moov.udta.meta.ilst.%s.data", atom);
  153. pMetaAtom = m_pRootAtom->FindAtomMP4(atomstring);
  154. if (!pMetaAtom)
  155. {
  156. if (!CreateMetadataAtom(atom))
  157. return false;
  158. pMetaAtom = m_pRootAtom->FindAtomMP4(atomstring);
  159. if (pMetaAtom == NULL) return false;
  160. }
  161. ASSERT(pMetaAtom->FindProperty("data.metadata",
  162. (MP4Property**)&pMetadataProperty));
  163. ASSERT(pMetadataProperty);
  164. pMetadataProperty->SetValue((u_int8_t*)value, (u_int32_t)strlen(value));
  165. return true;
  166. }
  167. bool MP4File::GetMetadataString (const char *atom, char **value, bool try_udta)
  168. {
  169. unsigned char *val = NULL;
  170. u_int32_t valSize = 0;
  171. char atomstring[60];
  172. snprintf(atomstring, 60, "moov.udta.meta.ilst.%s.data.metadata", atom);
  173. *value = NULL;
  174. if (try_udta == false) {
  175. GetBytesProperty(atomstring, (u_int8_t**)&val, &valSize);
  176. } else {
  177. bool got_it = false;
  178. try {
  179. GetBytesProperty(atomstring, (u_int8_t**)&val, &valSize);
  180. got_it = true;
  181. }
  182. catch (MP4Error* e) {
  183. delete e;
  184. }
  185. if (got_it == false) {
  186. snprintf(atomstring, 60, "moov.udta.%s.metadata", atom);
  187. GetBytesProperty(atomstring, (u_int8_t**)&val, &valSize);
  188. }
  189. }
  190. if (valSize > 0)
  191. {
  192. *value = (char*)malloc((valSize+1)*sizeof(char));
  193. if (*value == NULL) {
  194. free(val);
  195. return false;
  196. }
  197. memcpy(*value, val, valSize*sizeof(unsigned char));
  198. free(val);
  199. (*value)[valSize] = '\0';
  200. return true;
  201. }
  202. return false;
  203. }
  204. bool MP4File::SetMetadataTrack(u_int16_t track, u_int16_t totalTracks)
  205. {
  206. unsigned char t[9];
  207. const char *s = "moov.udta.meta.ilst.trkn.data";
  208. MP4BytesProperty *pMetadataProperty = NULL;
  209. MP4Atom *pMetaAtom = NULL;
  210. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  211. if (!pMetaAtom)
  212. {
  213. if (!CreateMetadataAtom("trkn"))
  214. return false;
  215. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  216. if (pMetaAtom == NULL) return false;
  217. }
  218. memset(t, 0, 9*sizeof(unsigned char));
  219. t[2] = (unsigned char)(track>>8)&0xFF;
  220. t[3] = (unsigned char)(track)&0xFF;
  221. t[4] = (unsigned char)(totalTracks>>8)&0xFF;
  222. t[5] = (unsigned char)(totalTracks)&0xFF;
  223. ASSERT(pMetaAtom->FindProperty("data.metadata",
  224. (MP4Property**)&pMetadataProperty));
  225. ASSERT(pMetadataProperty);
  226. pMetadataProperty->SetValue((u_int8_t*)t, 8);
  227. return true;
  228. }
  229. bool MP4File::GetMetadataTrack(u_int16_t* track, u_int16_t* totalTracks)
  230. {
  231. unsigned char *val = NULL;
  232. u_int32_t valSize = 0;
  233. const char *s = "moov.udta.meta.ilst.trkn.data.metadata";
  234. *track = 0;
  235. *totalTracks = 0;
  236. GetBytesProperty(s, (u_int8_t**)&val, &valSize);
  237. if (valSize == 8) {
  238. *track = (u_int16_t)(val[3]);
  239. *track += (u_int16_t)(val[2]<<8);
  240. *totalTracks = (u_int16_t)(val[5]);
  241. *totalTracks += (u_int16_t)(val[4]<<8);
  242. CHECK_AND_FREE(val);
  243. return true;
  244. }
  245. CHECK_AND_FREE(val);
  246. return false;
  247. }
  248. bool MP4File::SetMetadataDisk(u_int16_t disk, u_int16_t totalDisks)
  249. {
  250. unsigned char t[7];
  251. const char *s = "moov.udta.meta.ilst.disk.data";
  252. MP4BytesProperty *pMetadataProperty = NULL;
  253. MP4Atom *pMetaAtom = NULL;
  254. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  255. if (!pMetaAtom)
  256. {
  257. if (!CreateMetadataAtom("disk"))
  258. return false;
  259. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  260. if (pMetaAtom == NULL) return false;
  261. }
  262. memset(t, 0, 7*sizeof(unsigned char));
  263. t[2] = (unsigned char)(disk>>8)&0xFF;
  264. t[3] = (unsigned char)(disk)&0xFF;
  265. t[4] = (unsigned char)(totalDisks>>8)&0xFF;
  266. t[5] = (unsigned char)(totalDisks)&0xFF;
  267. ASSERT(pMetaAtom->FindProperty("data.metadata",
  268. (MP4Property**)&pMetadataProperty));
  269. ASSERT(pMetadataProperty);
  270. pMetadataProperty->SetValue((u_int8_t*)t, 6);
  271. return true;
  272. }
  273. bool MP4File::GetMetadataDisk(u_int16_t* disk, u_int16_t* totalDisks)
  274. {
  275. unsigned char *val = NULL;
  276. u_int32_t valSize = 0;
  277. const char *s = "moov.udta.meta.ilst.disk.data.metadata";
  278. *disk = 0;
  279. *totalDisks = 0;
  280. GetBytesProperty(s, (u_int8_t**)&val, &valSize);
  281. if (valSize == 6 || valSize == 8) {
  282. *disk = (u_int16_t)(val[3]);
  283. *disk += (u_int16_t)(val[2]<<8);
  284. *totalDisks = (u_int16_t)(val[5]);
  285. *totalDisks += (u_int16_t)(val[4]<<8);
  286. free(val);
  287. return true;
  288. }
  289. CHECK_AND_FREE(val);
  290. return true;
  291. }
  292. static const char* ID3v1GenreList[] = {
  293. "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk",
  294. "Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies",
  295. "Other", "Pop", "R&B", "Rap", "Reggae", "Rock",
  296. "Techno", "Industrial", "Alternative", "Ska", "Death Metal", "Pranks",
  297. "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop", "Vocal", "Jazz+Funk",
  298. "Fusion", "Trance", "Classical", "Instrumental", "Acid", "House",
  299. "Game", "Sound Clip", "Gospel", "Noise", "Alt Rock", "Bass",
  300. "Soul", "Punk", "Space", "Meditative", "Instrumental Pop", "Instrumental Rock",
  301. "Ethnic", "Gothic", "Darkwave", "Techno-Industrial", "Electronic", "Pop-Folk",
  302. "Eurodance", "Dream", "Southern Rock", "Comedy", "Cult", "Gangsta",
  303. "Top 40", "Christian Rap", "Pop/Funk", "Jungle", "Native American", "Cabaret",
  304. "New Wave", "Psychedelic", "Rave", "Showtunes", "Trailer", "Lo-Fi",
  305. "Tribal", "Acid Punk", "Acid Jazz", "Polka", "Retro", "Musical",
  306. "Rock & Roll", "Hard Rock", "Folk", "Folk-Rock", "National Folk", "Swing",
  307. "Fast-Fusion", "Bebop", "Latin", "Revival", "Celtic", "Bluegrass", "Avantgarde",
  308. "Gothic Rock", "Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock", "Big Band",
  309. "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech", "Chanson",
  310. "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass", "Primus",
  311. "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
  312. "Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet",
  313. "Punk Rock", "Drum Solo", "A capella", "Euro-House", "Dance Hall",
  314. "Goa", "Drum & Bass", "Club House", "Hardcore", "Terror",
  315. "Indie", "BritPop", "Afro-Punk", "Polsk Punk", "Beat",
  316. "Christian Gangsta", "Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian",
  317. "Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop",
  318. "SynthPop", "Abstract", "Art Rock", "Baroque", "Bhangra", "Big Beat", "Breakbeat", "Chillout", "Downtempo", "Dub", "EBM", "Eclectic", "Electro",
  319. "Electroclash", "Emo", "Experimental", "Garage", "Global", "IDM", "Illbient", "Industro-Goth", "Jam Band", "Krautrock", "Leftfield", "Lounge",
  320. "Math Rock", "New Romantic", "Nu-Breakz", "Post-Punk", "Post-Rock", "Psytrance", "Shoegaze", "Space Rock", "Trop Rock", "World Music", "Neoclassical",
  321. "Audiobook", "Audio Theatre", "Neue Deutsche Welle", "Podcast", "Indie Rock", "G-Funk", "Dubstep", "Garage Rock", "Psybient", "Glam Rock", "Dream Pop",
  322. "Merseybeat", "K-Pop", "Chiptune", "Grime", "Grindcore", "Indietronic", "Indietronica", "Jazz Rock", "Jazz Fusion", "Post-Punk Revival", "Electronica",
  323. "Psychill", "Ethnotronic", "Americana", "Ambient Dub", "Digital Dub", "Chillwave", "Stoner Rock", "Slowcore", "Softcore", "Flamenco", "Hi-NRG", "Ethereal",
  324. "Drone", "Doom Metal", "Doom Jazz", "Mainstream", "Glitch", "Balearic", "Modern Classical", "Mod", "Contemporary Classical", "Psybreaks", "Psystep",
  325. "Psydub", "Chillstep", "Berlin School", "Future Jazz", "Djent", "Musique Concrète", "Electroacoustic", "Folktronica", "Texas Country", "Red Dirt",
  326. "Arabic", "Asian", "Bachata", "Bollywood", "Cajun", "Calypso", "Creole", "Darkstep", "Jewish", "Reggaeton", "Smooth Jazz", "Soca", "Spiritual",
  327. "Turntablism", "Zouk","Neofolk", "Nu Jazz", "Psychobilly", "Rockabilly", "Schlager & Volksmusik",
  328. };
  329. void GenreToString(char** GenreStr, const int genre)
  330. {
  331. if (genre > 0 &&
  332. genre <= (int)(sizeof(ID3v1GenreList)/sizeof(*ID3v1GenreList)))
  333. {
  334. uint len = (uint)strlen(ID3v1GenreList[genre-1])+1;
  335. *GenreStr = (char*)malloc(len);
  336. if (*GenreStr == NULL) return;
  337. // no need for strncpy; enough was malloced
  338. strcpy(*GenreStr, ID3v1GenreList[genre-1]);
  339. return;
  340. }
  341. *GenreStr = (char*)malloc(2*sizeof(char));
  342. if (*GenreStr == NULL) return;
  343. memset(*GenreStr, 0, 2*sizeof(char));
  344. return;
  345. }
  346. int StringToGenre(const char* GenreStr)
  347. {
  348. unsigned int i;
  349. for (i = 0; i < sizeof(ID3v1GenreList)/sizeof(*ID3v1GenreList); i++)
  350. {
  351. if (strcasecmp(GenreStr, ID3v1GenreList[i]) == 0)
  352. return i+1;
  353. }
  354. return 0;
  355. }
  356. bool MP4File::SetMetadataGenre(const char* value)
  357. {
  358. u_int16_t genreIndex = 0;
  359. unsigned char t[3];
  360. MP4BytesProperty *pMetadataProperty = NULL;
  361. MP4Atom *pMetaAtom = NULL;
  362. genreIndex = StringToGenre(value);
  363. const char *s = "moov.udta.meta.ilst.gnre.data";
  364. const char *sroot = "moov.udta.meta.ilst.gnre";
  365. const char *s2 = "moov.udta.meta.ilst.\251gen.data";
  366. const char *s2root = "moov.udta.meta.ilst.\251gen";
  367. if (genreIndex != 0)
  368. {
  369. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  370. if (!pMetaAtom)
  371. {
  372. if (!CreateMetadataAtom("gnre"))
  373. return false;
  374. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  375. if (pMetaAtom == NULL) return false;
  376. }
  377. memset(t, 0, 3*sizeof(unsigned char));
  378. t[0] = (unsigned char)(genreIndex>>8)&0xFF;
  379. t[1] = (unsigned char)(genreIndex)&0xFF;
  380. ASSERT(pMetaAtom->FindProperty("data.metadata",
  381. (MP4Property**)&pMetadataProperty));
  382. ASSERT(pMetadataProperty);
  383. pMetadataProperty->SetValue((u_int8_t*)t, 2);
  384. // remove other style of genre atom, if this one is added
  385. pMetaAtom = m_pRootAtom->FindAtomMP4(s2root);
  386. if (pMetaAtom != NULL) {
  387. MP4Atom *pParent = pMetaAtom->GetParentAtom();
  388. if (pParent != NULL) {
  389. pParent->DeleteChildAtom(pMetaAtom);
  390. delete pMetaAtom;
  391. }
  392. }
  393. (void)DeleteMetadataAtom( "\251gen" );
  394. return true;
  395. } else {
  396. pMetaAtom = m_pRootAtom->FindAtomMP4(s2);
  397. if (!pMetaAtom)
  398. {
  399. if (!CreateMetadataAtom("\251gen"))
  400. return false;
  401. pMetaAtom = m_pRootAtom->FindAtomMP4(s2);
  402. }
  403. ASSERT(pMetaAtom->FindProperty("data.metadata",
  404. (MP4Property**)&pMetadataProperty));
  405. ASSERT(pMetadataProperty);
  406. pMetadataProperty->SetValue((u_int8_t*)value, (u_int32_t)strlen(value));
  407. // remove other gnre atom if this one is entered
  408. pMetaAtom = m_pRootAtom->FindAtomMP4(sroot);
  409. if (pMetaAtom != NULL) {
  410. MP4Atom *pParent = pMetaAtom->GetParentAtom();
  411. pParent->DeleteChildAtom(pMetaAtom);
  412. delete pMetaAtom;
  413. }
  414. return true;
  415. }
  416. return false;
  417. }
  418. bool MP4File::GetMetadataGenre(char** value)
  419. {
  420. u_int16_t genreIndex = 0;
  421. unsigned char *val = NULL;
  422. u_int32_t valSize = 0;
  423. const char *t = "moov.udta.meta.ilst.gnre";
  424. const char *s = "moov.udta.meta.ilst.gnre.data.metadata";
  425. *value = NULL;
  426. MP4Atom *gnre = FindAtomMP4File(t);
  427. if (gnre)
  428. {
  429. GetBytesProperty(s, (u_int8_t**)&val, &valSize);
  430. if (valSize != 2) {
  431. CHECK_AND_FREE(val);
  432. return false;
  433. }
  434. genreIndex = (u_int16_t)(val[1]);
  435. genreIndex += (u_int16_t)(val[0]<<8);
  436. GenreToString(value, genreIndex);
  437. // (void)DeleteMetadataAtom( "gnre" );
  438. free(val);
  439. return true;
  440. } else {
  441. const char *s2 = "moov.udta.meta.ilst.\251gen.data.metadata";
  442. val = NULL;
  443. valSize = 0;
  444. GetBytesProperty(s2, (u_int8_t**)&val, &valSize);
  445. if (valSize > 0)
  446. {
  447. *value = (char*)malloc((valSize+1)*sizeof(unsigned char));
  448. if (*value != NULL) {
  449. memset(*value, 0, (valSize+1)*sizeof(unsigned char));
  450. memcpy(*value, val, valSize*sizeof(unsigned char));
  451. }
  452. free(val);
  453. return true;
  454. } else {
  455. CHECK_AND_FREE(val);
  456. }
  457. }
  458. return false;
  459. }
  460. bool MP4File::DeleteMetadataGenre()
  461. {
  462. bool val1 = DeleteMetadataAtom("\251gen");
  463. bool val2 = DeleteMetadataAtom("gnre");
  464. return val1 || val2;
  465. }
  466. bool MP4File::SetMetadataTempo(u_int16_t tempo)
  467. {
  468. unsigned char t[3];
  469. const char *s = "moov.udta.meta.ilst.tmpo.data";
  470. MP4BytesProperty *pMetadataProperty = NULL;
  471. MP4Atom *pMetaAtom = NULL;
  472. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  473. if (!pMetaAtom)
  474. {
  475. if (!CreateMetadataAtom("tmpo"))
  476. return false;
  477. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  478. if (pMetaAtom == NULL) return false;
  479. }
  480. memset(t, 0, 3*sizeof(unsigned char));
  481. t[0] = (unsigned char)(tempo>>8)&0xFF;
  482. t[1] = (unsigned char)(tempo)&0xFF;
  483. ASSERT(pMetaAtom->FindProperty("data.metadata",
  484. (MP4Property**)&pMetadataProperty));
  485. ASSERT(pMetadataProperty);
  486. pMetadataProperty->SetValue((u_int8_t*)t, 2);
  487. return true;
  488. }
  489. bool MP4File::GetMetadataTempo(u_int16_t* tempo)
  490. {
  491. unsigned char *val = NULL;
  492. u_int32_t valSize = 0;
  493. const char *s = "moov.udta.meta.ilst.tmpo.data.metadata";
  494. *tempo = 0;
  495. GetBytesProperty(s, (u_int8_t**)&val, &valSize);
  496. if (valSize != 2) {
  497. CHECK_AND_FREE(val);
  498. return false;
  499. }
  500. *tempo = (u_int16_t)(val[1]);
  501. *tempo += (u_int16_t)(val[0]<<8);
  502. free(val);
  503. return true;
  504. }
  505. bool MP4File::SetMetadataUint8 (const char *atom, uint8_t value)
  506. {
  507. char atompath[36];
  508. MP4BytesProperty *pMetadataProperty = NULL;
  509. MP4Atom *pMetaAtom = NULL;
  510. snprintf(atompath, 36, "moov.udta.meta.ilst.%s.data", atom);
  511. pMetaAtom = m_pRootAtom->FindAtomMP4(atompath);
  512. if (pMetaAtom == NULL) {
  513. if (!CreateMetadataAtom(atom))
  514. return false;
  515. pMetaAtom = m_pRootAtom->FindAtomMP4(atompath);
  516. if (pMetaAtom == NULL) return false;
  517. }
  518. ASSERT(pMetaAtom->FindProperty("data.metadata",
  519. (MP4Property**)&pMetadataProperty));
  520. ASSERT(pMetadataProperty);
  521. pMetadataProperty->SetValue(&value, 1);
  522. return true;
  523. }
  524. bool MP4File::GetMetadataUint8(const char *atom, u_int8_t* retvalue)
  525. {
  526. unsigned char *val = NULL;
  527. u_int32_t valSize = 0;
  528. char atompath[80];
  529. snprintf(atompath, 80, "moov.udta.meta.ilst.%s.data.metadata", atom);
  530. *retvalue = 0;
  531. GetBytesProperty(atompath, (u_int8_t**)&val, &valSize);
  532. if (valSize != 1) {
  533. CHECK_AND_FREE(val);
  534. return false;
  535. }
  536. *retvalue = val[0];
  537. free(val);
  538. return true;
  539. }
  540. bool MP4File::SetMetadataCoverArt(u_int8_t *coverArt, u_int32_t size, int flags)
  541. {
  542. const char *s = "moov.udta.meta.ilst.covr.data";
  543. MP4BytesProperty *pMetadataProperty = NULL;
  544. MP4Atom *pMetaAtom = NULL;
  545. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  546. if (!pMetaAtom)
  547. {
  548. if (!CreateMetadataAtom("covr"))
  549. return false;
  550. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  551. if (pMetaAtom == NULL) return false;
  552. }
  553. pMetaAtom->SetFlags(flags);
  554. ASSERT(pMetaAtom->FindProperty("data.metadata",
  555. (MP4Property**)&pMetadataProperty));
  556. ASSERT(pMetadataProperty);
  557. pMetadataProperty->SetValue(coverArt, size);
  558. return true;
  559. }
  560. bool MP4File::GetMetadataCoverArt(u_int8_t **coverArt, u_int32_t *size,
  561. uint32_t index)
  562. {
  563. char buffer[256];
  564. if (size == NULL || coverArt == NULL) return false;
  565. if (index > 0 && index > GetMetadataCoverArtCount()) return false;
  566. snprintf(buffer, 256, "moov.udta.meta.ilst.covr.data[%d].metadata", index);
  567. *coverArt = NULL;
  568. *size = 0;
  569. GetBytesProperty(buffer, coverArt, size);
  570. if (*size == 0)
  571. return false;
  572. return true;
  573. }
  574. u_int32_t MP4File::GetMetadataCoverArtCount (void)
  575. {
  576. MP4Atom *pMetaAtom = m_pRootAtom->FindAtomMP4("moov.udta.meta.ilst.covr");
  577. if (!pMetaAtom)
  578. return 0;
  579. return pMetaAtom->GetNumberOfChildAtoms();
  580. }
  581. bool MP4File::SetMetadataFreeForm (const char *name,
  582. const u_int8_t* pValue,
  583. u_int32_t valueSize,
  584. const char *owner)
  585. {
  586. MP4Atom *pMetaAtom = NULL;
  587. MP4BytesProperty *pMetadataProperty = NULL;
  588. char s[256];
  589. int i = 0;
  590. size_t nameLen = strlen(name);
  591. size_t ownerLen = owner != NULL ? strlen(owner) : 0;
  592. while (1)
  593. {
  594. MP4BytesProperty *pMetadataProperty;
  595. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].name", i);
  596. MP4Atom *pTagAtom = m_pRootAtom->FindAtomMP4(s);
  597. if (!pTagAtom)
  598. break;
  599. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].mean", i);
  600. MP4Atom *pMeanAtom = m_pRootAtom->FindAtomMP4(s);
  601. if (pTagAtom->FindProperty("name.metadata",
  602. (MP4Property**)&pMetadataProperty) &&
  603. pMetadataProperty) {
  604. u_int8_t* pV;
  605. u_int32_t VSize = 0;
  606. pMetadataProperty->GetValue(&pV, &VSize);
  607. if (VSize == nameLen && memcmp(pV, name, VSize) == 0) {
  608. u_int8_t* pOwner=0;
  609. u_int32_t ownerSize = 0;
  610. if (pMeanAtom &&
  611. pMeanAtom->FindProperty("mean.metadata",
  612. (MP4Property**)&pMetadataProperty) &&
  613. pMetadataProperty) {
  614. pMetadataProperty->GetValue(&pOwner, &ownerSize);
  615. }
  616. if (owner == NULL||
  617. (pOwner &&
  618. ownerLen == ownerSize &&
  619. !memcmp(owner, pOwner, ownerSize))) {
  620. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].data.metadata", i);
  621. SetBytesProperty(s, pValue, valueSize);
  622. CHECK_AND_FREE(pV);
  623. CHECK_AND_FREE(pOwner);
  624. return true;
  625. }
  626. CHECK_AND_FREE(pOwner);
  627. }
  628. CHECK_AND_FREE(pV);
  629. }
  630. i++;
  631. }
  632. /* doesn't exist yet, create it */
  633. char t[256];
  634. snprintf(t, 256, "udta.meta.ilst.----[%u]", i);
  635. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].data", i);
  636. AddDescendantAtoms("moov", t);
  637. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  638. if (!pMetaAtom)
  639. return false;
  640. pMetaAtom->SetFlags(0x1);
  641. MP4Atom *pHdlrAtom = m_pRootAtom->FindAtomMP4("moov.udta.meta.hdlr");
  642. MP4StringProperty *pStringProperty = NULL;
  643. MP4BytesProperty *pBytesProperty = NULL;
  644. ASSERT(pHdlrAtom);
  645. ASSERT(pHdlrAtom->FindProperty("hdlr.handlerType",
  646. (MP4Property**)&pStringProperty));
  647. ASSERT(pStringProperty);
  648. pStringProperty->SetValue("mdir");
  649. u_int8_t val[12];
  650. memset(val, 0, 12*sizeof(u_int8_t));
  651. val[0] = 0x61;
  652. val[1] = 0x70;
  653. val[2] = 0x70;
  654. val[3] = 0x6c;
  655. ASSERT(pHdlrAtom->FindProperty("hdlr.reserved2",
  656. (MP4Property**)&pBytesProperty));
  657. ASSERT(pBytesProperty);
  658. pBytesProperty->SetReadOnly(false);
  659. pBytesProperty->SetValue(val, 12);
  660. pBytesProperty->SetReadOnly(true);
  661. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  662. ASSERT(pMetaAtom);
  663. ASSERT(pMetaAtom->FindProperty("data.metadata",
  664. (MP4Property**)&pMetadataProperty));
  665. ASSERT(pMetadataProperty);
  666. pMetadataProperty->SetValue(pValue, valueSize);
  667. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].name", i);
  668. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  669. ASSERT(pMetaAtom->FindProperty("name.metadata",
  670. (MP4Property**)&pMetadataProperty));
  671. ASSERT(pMetadataProperty);
  672. pMetadataProperty->SetValue((const u_int8_t*)name, (u_int32_t)strlen(name));
  673. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].mean", i);
  674. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  675. ASSERT(pMetaAtom->FindProperty("mean.metadata",
  676. (MP4Property**)&pMetadataProperty));
  677. ASSERT(pMetadataProperty);
  678. if (!owner || !*owner)
  679. pMetadataProperty->SetValue((u_int8_t*)"com.apple.iTunes", 16); /* com.apple.iTunes is the default*/
  680. else
  681. pMetadataProperty->SetValue((const u_int8_t*)owner, (u_int32_t)strlen((const char *)owner));
  682. return true;
  683. }
  684. bool MP4File::GetMetadataFreeForm(const char *name,
  685. u_int8_t** ppValue,
  686. u_int32_t *pValueSize,
  687. const char *owner)
  688. {
  689. char s[256];
  690. int i = 0;
  691. *ppValue = NULL;
  692. *pValueSize = 0;
  693. size_t nameLen = strlen(name);
  694. size_t ownerLen = owner?strlen(owner):0;
  695. while (1)
  696. {
  697. MP4BytesProperty *pMetadataProperty;
  698. snprintf(s, 256,"moov.udta.meta.ilst.----[%u].name", i);
  699. MP4Atom *pTagAtom = m_pRootAtom->FindAtomMP4(s);
  700. snprintf(s, 256,"moov.udta.meta.ilst.----[%u].mean", i);
  701. MP4Atom *pMeanAtom = m_pRootAtom->FindAtomMP4(s);
  702. if (!pTagAtom)
  703. return false;
  704. if (pTagAtom->FindProperty("name.metadata",
  705. (MP4Property**)&pMetadataProperty) &&
  706. pMetadataProperty) {
  707. u_int8_t* pV;
  708. u_int32_t VSize = 0;
  709. pMetadataProperty->GetValue(&pV, &VSize);
  710. if (VSize == nameLen && !memcmp(pV, name, VSize)) {
  711. u_int8_t* pOwner=0;
  712. u_int32_t ownerSize = 0;
  713. if (pMeanAtom && pMeanAtom->FindProperty("mean.metadata",
  714. (MP4Property**)&pMetadataProperty) &&
  715. pMetadataProperty) {
  716. pMetadataProperty->GetValue(&pOwner, &ownerSize);
  717. }
  718. if (!owner || (pOwner && ownerLen == ownerSize && !memcmp(owner, pOwner, ownerSize))) {
  719. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].data.metadata", i);
  720. GetBytesProperty(s, ppValue, pValueSize);
  721. CHECK_AND_FREE(pV);
  722. CHECK_AND_FREE(pOwner);
  723. return true;
  724. }
  725. CHECK_AND_FREE(pOwner);
  726. }
  727. CHECK_AND_FREE(pV);
  728. }
  729. i++;
  730. }
  731. }
  732. bool MP4File::DeleteMetadataFreeForm(const char *name, const char *owner)
  733. {
  734. char s[256];
  735. int i = 0;
  736. size_t nameLen = strlen(name);
  737. size_t ownerLen = owner?strlen(owner):0;
  738. while (1)
  739. {
  740. MP4BytesProperty *pMetadataProperty;
  741. snprintf(s, 256, "moov.udta.meta.ilst.----[%u].name", i);
  742. MP4Atom *pTagAtom = m_pRootAtom->FindAtomMP4(s);
  743. snprintf(s, 256,"moov.udta.meta.ilst.----[%u].mean", i);
  744. MP4Atom *pMeanAtom = m_pRootAtom->FindAtomMP4(s);
  745. if (!pTagAtom)
  746. return false;
  747. if (pTagAtom->FindProperty("name.metadata",
  748. (MP4Property**)&pMetadataProperty) &&
  749. pMetadataProperty) {
  750. u_int8_t* pV;
  751. u_int32_t VSize = 0;
  752. pMetadataProperty->GetValue(&pV, &VSize);
  753. if (VSize != 0)
  754. {
  755. if (VSize == nameLen && !memcmp(pV, name, VSize))
  756. {
  757. u_int8_t* pOwner=0;
  758. u_int32_t ownerSize = 0;
  759. if (pMeanAtom && pMeanAtom->FindProperty("mean.metadata",
  760. (MP4Property**)&pMetadataProperty) &&
  761. pMetadataProperty)
  762. {
  763. pMetadataProperty->GetValue(&pOwner, &ownerSize);
  764. }
  765. if (!owner || (pOwner && ownerLen == ownerSize && !memcmp(owner, pOwner, ownerSize)))
  766. {
  767. snprintf(s, 256, "----[%u]", i);
  768. CHECK_AND_FREE(pOwner);
  769. CHECK_AND_FREE(pV);
  770. return DeleteMetadataAtom(s);
  771. }
  772. CHECK_AND_FREE(pOwner);
  773. }
  774. }
  775. CHECK_AND_FREE(pV);
  776. }
  777. i++;
  778. }
  779. }
  780. bool MP4File::MetadataDelete()
  781. {
  782. MP4Atom *pMetaAtom = NULL;
  783. char s[256];
  784. snprintf(s, 256, "moov.udta.meta");
  785. pMetaAtom = m_pRootAtom->FindAtomMP4(s);
  786. /* if it exists, delete it */
  787. if (pMetaAtom)
  788. {
  789. MP4Atom *pParent = pMetaAtom->GetParentAtom();
  790. pParent->DeleteChildAtom(pMetaAtom);
  791. delete pMetaAtom;
  792. return true;
  793. }
  794. return false;
  795. }