Metadata.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. #include "Metadata.h"
  2. #include "main.h"
  3. #include "api__in_mp3.h"
  4. #include "LAMEInfo.h"
  5. #include "AACFrame.h"
  6. #include "config.h"
  7. #include "LAMEInfo.h"
  8. #include <shlwapi.h>
  9. #include <assert.h>
  10. #include <foundation/error.h>
  11. #include <strsafe.h>
  12. #define INFO_READ_SIZE 32768
  13. Metadata::Metadata( CGioFile *_file, const wchar_t *_filename )
  14. {
  15. if ( !PathIsURL( _filename ) )
  16. filename = _wcsdup( _filename );
  17. ReadTags( _file );
  18. if ( bitrate = _file->GetAvgVBRBitrate() * 1000 )
  19. {
  20. length_ms = _file->m_vbr_ms;
  21. vbr = _file->m_vbr_flag || _file->m_vbr_hdr;
  22. }
  23. }
  24. void GetFileDescription(const wchar_t *file, CGioFile &_file, wchar_t *data, size_t datalen);
  25. void GetAudioInfo(const wchar_t *filename, CGioFile *file, int *len, int *channels, int *bitrate, int *vbr, int *sr);
  26. int Metadata::Open(const wchar_t *_filename)
  27. {
  28. if ( filename && *filename )
  29. free( filename );
  30. filename = _wcsdup(_filename);
  31. if (file.Open(filename, INFO_READ_SIZE/1024) != NErr_Success)
  32. return 1;
  33. GetAudioInfo(filename, &file, &length_ms, &channels, &bitrate, &vbr, &sampleRate);
  34. ReadTags(&file);
  35. file.Close();
  36. return METADATA_SUCCESS;
  37. }
  38. Metadata::~Metadata()
  39. {
  40. if (filename)
  41. {
  42. free(filename);
  43. filename=0;
  44. }
  45. }
  46. void Metadata::ReadTags(CGioFile *_file)
  47. {
  48. // Process ID3v1
  49. if (config_parse_id3v1)
  50. {
  51. void *id3v1_data = _file->GetID3v1();
  52. if (id3v1_data)
  53. id3v1.Decode(id3v1_data);
  54. }
  55. if (config_parse_id3v2)
  56. {
  57. uint32_t len = 0;
  58. void *id3v2_data = _file->GetID3v2(&len);
  59. if (id3v2_data)
  60. id3v2.Decode(id3v2_data, len);
  61. }
  62. if (config_parse_lyrics3)
  63. {
  64. uint32_t len = 0;
  65. void *lyrics3_data = _file->GetLyrics3(&len);
  66. if (lyrics3_data)
  67. lyrics3.Decode(lyrics3_data, len);
  68. }
  69. if (config_parse_apev2)
  70. {
  71. uint32_t len = 0;
  72. void *apev2_data = _file->GetAPEv2(&len);
  73. if (apev2_data)
  74. apev2.Decode(apev2_data, len);
  75. }
  76. }
  77. static int ID3Write(const wchar_t *filename, HANDLE infile, DWORD offset, void *data, DWORD len)
  78. {
  79. wchar_t tempFile[MAX_PATH] = {0};
  80. StringCchCopyW(tempFile, MAX_PATH, filename);
  81. PathRemoveExtension(tempFile);
  82. StringCchCatW(tempFile, MAX_PATH, L".tmp");
  83. // check to make sure the filename was actually different!
  84. // benski> TODO: we should just try to mangle the filename more rather than totally bail out
  85. if (!_wcsicmp(tempFile, filename))
  86. return SAVE_ERROR_CANT_OPEN_TEMPFILE;
  87. // TODO: overlapped I/O
  88. HANDLE outfile = CreateFile(tempFile, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, 0, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, 0);
  89. if (outfile != INVALID_HANDLE_VALUE)
  90. {
  91. DWORD written=0;
  92. if (data && len)
  93. WriteFile(outfile, data, len, &written, NULL);
  94. SetFilePointer(infile, offset, 0, FILE_BEGIN);
  95. DWORD read=0;
  96. do
  97. {
  98. char data[4096] = {0};
  99. written = read = 0;
  100. ReadFile(infile, data, 4096, &read, NULL);
  101. if (read) WriteFile(outfile, data, read, &written, NULL);
  102. }
  103. while (read != 0);
  104. CloseHandle(outfile);
  105. CloseHandle(infile);
  106. if (!MoveFile(tempFile, filename))
  107. {
  108. if (!CopyFile(tempFile, filename, FALSE))
  109. {
  110. DeleteFile(tempFile);
  111. return SAVE_ERROR_ERROR_OVERWRITING;
  112. }
  113. DeleteFile(tempFile);
  114. }
  115. return SAVE_SUCCESS;
  116. }
  117. return SAVE_ERROR_CANT_OPEN_TEMPFILE;
  118. }
  119. bool Metadata::IsDirty()
  120. {
  121. return id3v1.IsDirty() || id3v2.IsDirty() || lyrics3.IsDirty() || apev2.IsDirty();
  122. }
  123. int Metadata::Save()
  124. {
  125. if (!IsDirty())
  126. return SAVE_SUCCESS;
  127. int err=SAVE_SUCCESS;
  128. if (GetFileAttributes(filename)&FILE_ATTRIBUTE_READONLY)
  129. return SAVE_ERROR_READONLY;
  130. HANDLE metadataFile = CreateFile(filename, GENERIC_WRITE|GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
  131. if (metadataFile == INVALID_HANDLE_VALUE)
  132. return SAVE_ERROR_OPENING_FILE;
  133. if (file.Open(filename, INFO_READ_SIZE/1024) != NErr_Success)
  134. {
  135. CloseHandle(metadataFile);
  136. return SAVE_ERROR_OPENING_FILE;
  137. }
  138. bool strippedID3v1=false; // this flag will get set to true when we remove ID3v1 as a side effect of removing APEv2 or Lyrics3 (or ID3v2.4 end-tag if/when we implement)
  139. bool strippedLyrics3=false;
  140. /* Strip APEv2 */
  141. if (config_parse_apev2 && config_write_apev2 && apev2.IsDirty())
  142. {
  143. uint32_t len = 0;
  144. void *apev2_data = file.GetAPEv2(&len);
  145. if (apev2_data)
  146. {
  147. uint32_t lyrics3_len = 0;
  148. void *lyrics3_data = file.GetLyrics3(&lyrics3_len);
  149. if (lyrics3_data)
  150. SetFilePointer(metadataFile, -(LONG)(len + 15 + lyrics3_len + (file.GetID3v1()?128:0)), NULL, FILE_END);
  151. else
  152. SetFilePointer(metadataFile, -(LONG)(len + (file.GetID3v1()?128:0)), NULL, FILE_END);
  153. SetEndOfFile(metadataFile);
  154. strippedLyrics3=true;
  155. strippedID3v1=true;
  156. }
  157. }
  158. /* Strip Lyrics3 tag */
  159. if (!strippedLyrics3 && config_parse_lyrics3 && lyrics3.IsDirty())
  160. {
  161. uint32_t len = 0;
  162. void *lyrics3_data = file.GetLyrics3(&len);
  163. if (lyrics3_data)
  164. {
  165. SetFilePointer(metadataFile, -(LONG)(len + 15 + (file.GetID3v1()?128:0)), NULL, FILE_END);
  166. SetEndOfFile(metadataFile);
  167. strippedID3v1=true;
  168. }
  169. }
  170. /* Strip ID3v1(.1) tag */
  171. if (!strippedID3v1 /* if we stripped lyrics3 tag, then we ended up stripping id3v1 also */
  172. && config_parse_id3v1 && config_write_id3v1 && id3v1.IsDirty())
  173. {
  174. if (file.GetID3v1()) // see if we have ID3v1
  175. {
  176. SetFilePointer(metadataFile, -128, NULL, FILE_END);
  177. SetEndOfFile(metadataFile);
  178. }
  179. }
  180. /* Write APEv2 */
  181. if (config_parse_apev2 && config_write_apev2 && apev2.IsDirty() && apev2.HasData())
  182. {
  183. switch(config_apev2_header)
  184. {
  185. case ADD_HEADER:
  186. apev2.SetFlags(APEv2::FLAG_HEADER_HAS_HEADER, APEv2::FLAG_HEADER_HAS_HEADER);
  187. break;
  188. case REMOVE_HEADER:
  189. apev2.SetFlags(0, APEv2::FLAG_HEADER_HAS_HEADER);
  190. break;
  191. }
  192. size_t apev2_len = apev2.EncodeSize();
  193. void *apev2_data = malloc(apev2_len);
  194. if (apev2_data && apev2.Encode(apev2_data, apev2_len) == APEv2::APEV2_SUCCESS)
  195. {
  196. SetFilePointer(metadataFile, 0, NULL, FILE_END);
  197. DWORD bytesWritten=0;
  198. WriteFile(metadataFile, apev2_data, (DWORD)apev2_len, &bytesWritten, 0);
  199. free(apev2_data);
  200. apev2_data = 0;
  201. if (bytesWritten != apev2_len)
  202. {
  203. err=SAVE_APEV2_WRITE_ERROR;
  204. goto fail;
  205. }
  206. }
  207. else
  208. {
  209. free(apev2_data);
  210. apev2_data = 0;
  211. err=SAVE_APEV2_WRITE_ERROR;
  212. goto fail;
  213. }
  214. }
  215. /* Write Lyrics3 */
  216. if (strippedLyrics3) /* if we need to rewrite it because we stripped it (e.g. removing an APEv2 tag)*/
  217. {
  218. /* since we don't modify lyrics3 (yet) we'll just rewrite the original binary data */
  219. uint32_t len = 0;
  220. void *lyrics3_data = file.GetLyrics3(&len);
  221. if (lyrics3_data)
  222. {
  223. SetFilePointer(metadataFile, 0, NULL, FILE_END);
  224. DWORD bytesWritten=0;
  225. WriteFile(metadataFile, lyrics3_data, len, &bytesWritten, NULL);
  226. if (bytesWritten != len)
  227. {
  228. err=SAVE_LYRICS3_WRITE_ERROR;
  229. goto fail;
  230. }
  231. char temp[7] = {0};
  232. StringCchPrintfA(temp, 7, "%06u", len);
  233. bytesWritten = 0;
  234. WriteFile(metadataFile, temp, 6, &bytesWritten, NULL);
  235. if (bytesWritten != 6)
  236. {
  237. err=SAVE_LYRICS3_WRITE_ERROR;
  238. goto fail;
  239. }
  240. bytesWritten = 0;
  241. WriteFile(metadataFile, "LYRICS200", 9, &bytesWritten, NULL);
  242. if (bytesWritten != 9)
  243. {
  244. err=SAVE_LYRICS3_WRITE_ERROR;
  245. goto fail;
  246. }
  247. }
  248. }
  249. /* Write ID3v1 */
  250. if (config_parse_id3v1 && config_write_id3v1 && id3v1.IsDirty())
  251. {
  252. uint8_t id3v1_data[128] = {0};
  253. if (id3v1.Encode(id3v1_data) == METADATA_SUCCESS)
  254. {
  255. SetFilePointer(metadataFile, 0, NULL, FILE_END);
  256. DWORD bytesWritten=0;
  257. WriteFile(metadataFile, id3v1_data, 128, &bytesWritten, NULL);
  258. if (bytesWritten != 128)
  259. {
  260. err=SAVE_ID3V1_WRITE_ERROR;
  261. goto fail;
  262. }
  263. }
  264. }
  265. else if (strippedID3v1)
  266. {
  267. /** if we stripped lyrics3 or apev2 but didn't modify id3v1 (or are configured not to use it),
  268. ** we need to rewrite it back to the original data
  269. **/
  270. void *id3v1_data=file.GetID3v1();
  271. if (id3v1_data)
  272. {
  273. SetFilePointer(metadataFile, 0, NULL, FILE_END);
  274. DWORD bytesWritten=0;
  275. WriteFile(metadataFile, id3v1_data, 128, &bytesWritten, NULL);
  276. if (bytesWritten != 128)
  277. {
  278. err=SAVE_ID3V1_WRITE_ERROR;
  279. goto fail;
  280. }
  281. }
  282. }
  283. /* Write ID3v2 */
  284. if (config_parse_id3v2 && config_write_id3v2 && id3v2.IsDirty())
  285. {
  286. uint32_t oldlen=0;
  287. void *old_id3v2_data = file.GetID3v2(&oldlen);
  288. id3v2.id3v2.SetPadding(false); // turn off padding to see if we can get away with non re-writing the file
  289. uint32_t newlen = id3v2.EncodeSize();
  290. if (old_id3v2_data && !newlen) // there's an old tag, but no new tag
  291. {
  292. err = ID3Write(filename, metadataFile, oldlen, 0, 0);
  293. if (err == SAVE_SUCCESS)
  294. metadataFile = INVALID_HANDLE_VALUE; // ID3Write returns true if it closed the handle
  295. else
  296. goto fail;
  297. }
  298. else if (!old_id3v2_data && !newlen) // no old tag, no new tag.. easy :)
  299. {
  300. }
  301. else
  302. {
  303. id3v2.id3v2.SetPadding(true);
  304. if (newlen <= oldlen) // if we can fit in the old tag
  305. {
  306. if (oldlen != newlen)
  307. id3v2.id3v2.ForcePading(oldlen-newlen); // pad out the rest of the tag
  308. else
  309. id3v2.id3v2.SetPadding(false);
  310. assert(id3v2.EncodeSize() == oldlen);
  311. newlen = oldlen;
  312. uint8_t *new_id3v2_data = (uint8_t *)calloc(newlen, sizeof(uint8_t));
  313. if (new_id3v2_data && id3v2.Encode(new_id3v2_data, newlen) == METADATA_SUCCESS)
  314. {
  315. // TODO: deal with files with multiple starting id3v2 tags
  316. SetFilePointer(metadataFile, 0, NULL, FILE_BEGIN);
  317. DWORD bytesWritten=0;
  318. WriteFile(metadataFile, new_id3v2_data, newlen, &bytesWritten, NULL);
  319. free(new_id3v2_data);
  320. new_id3v2_data = 0;
  321. if (bytesWritten != newlen)
  322. {
  323. err = SAVE_ID3V2_WRITE_ERROR;
  324. goto fail;
  325. }
  326. }
  327. else
  328. {
  329. free(new_id3v2_data);
  330. new_id3v2_data = 0;
  331. err = SAVE_ID3V2_WRITE_ERROR;
  332. goto fail;
  333. }
  334. }
  335. else // otherwise we have to pad out the start
  336. {
  337. newlen = id3v2.EncodeSize();
  338. uint8_t *new_id3v2_data = (uint8_t *)calloc(newlen, sizeof(uint8_t));
  339. if (new_id3v2_data && id3v2.Encode(new_id3v2_data, newlen) == METADATA_SUCCESS)
  340. {
  341. // TODO: deal with files with multiple starting id3v2 tags
  342. SetFilePointer(metadataFile, 0, NULL, FILE_BEGIN);
  343. DWORD bytesWritten=0;
  344. err = ID3Write(filename, metadataFile, oldlen, new_id3v2_data, newlen);
  345. free(new_id3v2_data);
  346. new_id3v2_data = 0;
  347. if (err == SAVE_SUCCESS)
  348. metadataFile = INVALID_HANDLE_VALUE; // ID3Write returns true if it closed the handle
  349. else
  350. goto fail;
  351. }
  352. else
  353. {
  354. free(new_id3v2_data);
  355. new_id3v2_data = 0;
  356. err = SAVE_ID3V2_WRITE_ERROR;
  357. goto fail;
  358. }
  359. }
  360. }
  361. }
  362. fail:
  363. file.Close();
  364. if (metadataFile != INVALID_HANDLE_VALUE)
  365. CloseHandle(metadataFile);
  366. return err;
  367. }
  368. int Metadata::GetExtendedData(const char *tag, wchar_t *data, int dataLen)
  369. {
  370. int understood=0;
  371. switch (id3v2.GetString(tag, data, dataLen))
  372. {
  373. case -1:
  374. data[0]=0;
  375. understood=1;
  376. break;
  377. case 1:
  378. return 1;
  379. }
  380. switch (apev2.GetString(tag, data, dataLen))
  381. {
  382. case -1:
  383. data[0]=0;
  384. understood=1;
  385. break;
  386. case 1:
  387. return 1;
  388. }
  389. switch (lyrics3.GetString(tag, data, dataLen))
  390. {
  391. case -1:
  392. data[0]=0;
  393. understood=1;
  394. break;
  395. case 1:
  396. return 1;
  397. }
  398. switch (id3v1.GetString(tag, data, dataLen))
  399. {
  400. case -1:
  401. data[0]=0;
  402. understood=1;
  403. break;
  404. case 1:
  405. return 1;
  406. }
  407. switch (GetString(tag, data, dataLen))
  408. {
  409. case -1:
  410. data[0]=0;
  411. understood=1;
  412. break;
  413. case 1:
  414. return 1;
  415. }
  416. return understood;
  417. }
  418. int Metadata::SetExtendedData(const char *tag, const wchar_t *data)
  419. {
  420. int understood=0;
  421. if (config_create_id3v2 || id3v2.HasData())
  422. understood |= id3v2.SetString(tag, data);
  423. if (config_create_apev2 || apev2.HasData())
  424. understood |= apev2.SetString(tag, data);
  425. if (config_create_id3v1 || id3v1.HasData())
  426. understood |= id3v1.SetString(tag, data);
  427. return understood;
  428. }
  429. int Metadata::GetString(const char *tag, wchar_t *data, int dataLen)
  430. {
  431. if (!_stricmp(tag, "formatinformation"))
  432. {
  433. data[0]=0;
  434. if (filename)
  435. {
  436. if (file.Open(filename, INFO_READ_SIZE/1024) == NErr_Success)
  437. GetFileDescription(filename, file, data, dataLen);
  438. file.Close();
  439. }
  440. }
  441. else if (!_stricmp(tag, "length"))
  442. {
  443. StringCchPrintfW(data, dataLen, L"%d", length_ms);
  444. }
  445. else if (!_stricmp(tag, "stereo"))
  446. {
  447. StringCchPrintfW(data, dataLen, L"%d", channels==2);
  448. }
  449. else if (!_stricmp(tag, "vbr"))
  450. {
  451. StringCchPrintfW(data, dataLen, L"%d", vbr);
  452. }
  453. else if (!_stricmp(tag, "bitrate"))
  454. {
  455. StringCchPrintfW(data, dataLen, L"%d", bitrate/1000);
  456. }
  457. else if (!_stricmp(tag, "gain"))
  458. {
  459. StringCchPrintfW(data, dataLen, L"%-+.2f dB", file.GetGain());
  460. }
  461. else if (!_stricmp(tag, "pregap"))
  462. {
  463. if (file.prepad)
  464. {
  465. StringCchPrintfW(data, dataLen, L"%u", file.prepad);
  466. return 1;
  467. }
  468. return -1;
  469. }
  470. else if (!_stricmp(tag, "postgap"))
  471. {
  472. if (file.prepad) // yes, we check for this because postpad could legitimately be 0
  473. {
  474. StringCchPrintfW(data, dataLen, L"%u", file.postpad);
  475. return 1;
  476. }
  477. return -1;
  478. }
  479. else if (!_stricmp(tag, "numsamples"))
  480. {
  481. if (file.m_vbr_samples)
  482. {
  483. StringCchPrintfW(data, dataLen, L"%I64u", file.m_vbr_samples);
  484. return 1;
  485. }
  486. return -1;
  487. }
  488. else if (!_stricmp(tag, "endoffset"))
  489. {
  490. if (file.m_vbr_frames)
  491. {
  492. int totalFrames = file.m_vbr_frames;
  493. if (totalFrames > 8)
  494. {
  495. int seekPoint = 0;
  496. // we're using m_vbr_bytes here instead of file.ContentLength(), because we're already trusting the other LAME header info
  497. #define MAX_SIZE_8_FRAMES (1448 * 8) // mp3 frames won't be ever be any bigger than this (320kbps 32000Hz + padding)
  498. if (file.m_vbr_bytes > MAX_SIZE_8_FRAMES)
  499. seekPoint = (int)(file.m_vbr_bytes - MAX_SIZE_8_FRAMES);
  500. else
  501. seekPoint = 0;
  502. size_t offsets[8] = {0};
  503. size_t offsetsRead = 0;
  504. size_t offsetPosition = 0;
  505. unsigned char header[6] = {0};
  506. MPEGFrame frame;
  507. if (file.Open(filename, INFO_READ_SIZE/1024) != NErr_Success)
  508. return -1;
  509. // first we need to sync
  510. while (1)
  511. {
  512. file.SetCurrentPosition(seekPoint, CGioFile::GIO_FILE_BEGIN);
  513. int read = 0;
  514. file.Read(header, 6, &read);
  515. if (read != 6)
  516. break;
  517. frame.ReadBuffer(header);
  518. if (frame.IsSync() && frame.GetLayer() == 3)
  519. {
  520. // make sure this isn't false sync - see if we can get another sync...
  521. int nextPoint = seekPoint + frame.FrameSize();
  522. file.SetCurrentPosition(nextPoint, CGioFile::GIO_FILE_BEGIN);
  523. file.Read(header, 6, &read);
  524. if (read != 6) // must be EOF
  525. break;
  526. frame.ReadBuffer(header);
  527. if (frame.IsSync() && frame.GetLayer() == 3)
  528. break;
  529. }
  530. seekPoint++;
  531. }
  532. while (1)
  533. {
  534. file.SetCurrentPosition(seekPoint, CGioFile::GIO_FILE_BEGIN);
  535. int read = 0;
  536. file.Read(header, 6, &read);
  537. if (read != 6)
  538. break;
  539. frame.ReadBuffer(header);
  540. if (frame.IsSync() && frame.GetLayer() == 3)
  541. {
  542. offsets[offsetPosition] = seekPoint;
  543. offsetPosition = (offsetPosition + 1) % 8;
  544. offsetsRead++;
  545. seekPoint += frame.FrameSize();
  546. }
  547. else
  548. break;
  549. }
  550. if (offsetsRead >= 8)
  551. {
  552. StringCchPrintfW(data, dataLen, L"%I32d", offsets[offsetPosition] + file.m_vbr_frame_len);
  553. file.Close();
  554. return 1;
  555. }
  556. file.Close();
  557. }
  558. }
  559. return -1;
  560. }
  561. else
  562. return 0;
  563. return 1;
  564. }
  565. int fixAACCBRbitrate(int br);