You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
6.3KB

  1. /*
  2. * ID3v1 header parser
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "id3v1.h"
  22. #include "libavcodec/avcodec.h"
  23. #include "libavutil/dict.h"
  24. /* See Genre List at http://id3.org/id3v2.3.0 */
  25. const char * const ff_id3v1_genre_str[ID3v1_GENRE_MAX + 1] = {
  26. [0] = "Blues",
  27. [1] = "Classic Rock",
  28. [2] = "Country",
  29. [3] = "Dance",
  30. [4] = "Disco",
  31. [5] = "Funk",
  32. [6] = "Grunge",
  33. [7] = "Hip-Hop",
  34. [8] = "Jazz",
  35. [9] = "Metal",
  36. [10] = "New Age",
  37. [11] = "Oldies",
  38. [12] = "Other",
  39. [13] = "Pop",
  40. [14] = "R&B",
  41. [15] = "Rap",
  42. [16] = "Reggae",
  43. [17] = "Rock",
  44. [18] = "Techno",
  45. [19] = "Industrial",
  46. [20] = "Alternative",
  47. [21] = "Ska",
  48. [22] = "Death Metal",
  49. [23] = "Pranks",
  50. [24] = "Soundtrack",
  51. [25] = "Euro-Techno",
  52. [26] = "Ambient",
  53. [27] = "Trip-Hop",
  54. [28] = "Vocal",
  55. [29] = "Jazz+Funk",
  56. [30] = "Fusion",
  57. [31] = "Trance",
  58. [32] = "Classical",
  59. [33] = "Instrumental",
  60. [34] = "Acid",
  61. [35] = "House",
  62. [36] = "Game",
  63. [37] = "Sound Clip",
  64. [38] = "Gospel",
  65. [39] = "Noise",
  66. [40] = "AlternRock",
  67. [41] = "Bass",
  68. [42] = "Soul",
  69. [43] = "Punk",
  70. [44] = "Space",
  71. [45] = "Meditative",
  72. [46] = "Instrumental Pop",
  73. [47] = "Instrumental Rock",
  74. [48] = "Ethnic",
  75. [49] = "Gothic",
  76. [50] = "Darkwave",
  77. [51] = "Techno-Industrial",
  78. [52] = "Electronic",
  79. [53] = "Pop-Folk",
  80. [54] = "Eurodance",
  81. [55] = "Dream",
  82. [56] = "Southern Rock",
  83. [57] = "Comedy",
  84. [58] = "Cult",
  85. [59] = "Gangsta",
  86. [60] = "Top 40",
  87. [61] = "Christian Rap",
  88. [62] = "Pop/Funk",
  89. [63] = "Jungle",
  90. [64] = "Native American",
  91. [65] = "Cabaret",
  92. [66] = "New Wave",
  93. [67] = "Psychadelic", /* sic, the misspelling is used in the specification */
  94. [68] = "Rave",
  95. [69] = "Showtunes",
  96. [70] = "Trailer",
  97. [71] = "Lo-Fi",
  98. [72] = "Tribal",
  99. [73] = "Acid Punk",
  100. [74] = "Acid Jazz",
  101. [75] = "Polka",
  102. [76] = "Retro",
  103. [77] = "Musical",
  104. [78] = "Rock & Roll",
  105. [79] = "Hard Rock",
  106. [80] = "Folk",
  107. [81] = "Folk-Rock",
  108. [82] = "National Folk",
  109. [83] = "Swing",
  110. [84] = "Fast Fusion",
  111. [85] = "Bebob",
  112. [86] = "Latin",
  113. [87] = "Revival",
  114. [88] = "Celtic",
  115. [89] = "Bluegrass",
  116. [90] = "Avantgarde",
  117. [91] = "Gothic Rock",
  118. [92] = "Progressive Rock",
  119. [93] = "Psychedelic Rock",
  120. [94] = "Symphonic Rock",
  121. [95] = "Slow Rock",
  122. [96] = "Big Band",
  123. [97] = "Chorus",
  124. [98] = "Easy Listening",
  125. [99] = "Acoustic",
  126. [100] = "Humour",
  127. [101] = "Speech",
  128. [102] = "Chanson",
  129. [103] = "Opera",
  130. [104] = "Chamber Music",
  131. [105] = "Sonata",
  132. [106] = "Symphony",
  133. [107] = "Booty Bass",
  134. [108] = "Primus",
  135. [109] = "Porn Groove",
  136. [110] = "Satire",
  137. [111] = "Slow Jam",
  138. [112] = "Club",
  139. [113] = "Tango",
  140. [114] = "Samba",
  141. [115] = "Folklore",
  142. [116] = "Ballad",
  143. [117] = "Power Ballad",
  144. [118] = "Rhythmic Soul",
  145. [119] = "Freestyle",
  146. [120] = "Duet",
  147. [121] = "Punk Rock",
  148. [122] = "Drum Solo",
  149. [123] = "A capella",
  150. [124] = "Euro-House",
  151. [125] = "Dance Hall",
  152. [126] = "Goa",
  153. [127] = "Drum & Bass",
  154. [128] = "Club-House",
  155. [129] = "Hardcore",
  156. [130] = "Terror",
  157. [131] = "Indie",
  158. [132] = "BritPop",
  159. [133] = "Negerpunk",
  160. [134] = "Polsk Punk",
  161. [135] = "Beat",
  162. [136] = "Christian Gangsta",
  163. [137] = "Heavy Metal",
  164. [138] = "Black Metal",
  165. [139] = "Crossover",
  166. [140] = "Contemporary Christian",
  167. [141] = "Christian Rock",
  168. [142] = "Merengue",
  169. [143] = "Salsa",
  170. [144] = "Thrash Metal",
  171. [145] = "Anime",
  172. [146] = "JPop",
  173. [147] = "SynthPop",
  174. };
  175. static void get_string(AVFormatContext *s, const char *key,
  176. const uint8_t *buf, int buf_size)
  177. {
  178. int i, c;
  179. char *q, str[512];
  180. q = str;
  181. for(i = 0; i < buf_size; i++) {
  182. c = buf[i];
  183. if (c == '\0')
  184. break;
  185. if ((q - str) >= sizeof(str) - 1)
  186. break;
  187. *q++ = c;
  188. }
  189. *q = '\0';
  190. if (*str)
  191. av_dict_set(&s->metadata, key, str, 0);
  192. }
  193. /**
  194. * Parse an ID3v1 tag
  195. *
  196. * @param buf ID3v1_TAG_SIZE long buffer containing the tag
  197. */
  198. static int parse_tag(AVFormatContext *s, const uint8_t *buf)
  199. {
  200. char str[5];
  201. int genre;
  202. if (!(buf[0] == 'T' &&
  203. buf[1] == 'A' &&
  204. buf[2] == 'G'))
  205. return -1;
  206. get_string(s, "title", buf + 3, 30);
  207. get_string(s, "artist", buf + 33, 30);
  208. get_string(s, "album", buf + 63, 30);
  209. get_string(s, "date", buf + 93, 4);
  210. get_string(s, "comment", buf + 97, 30);
  211. if (buf[125] == 0 && buf[126] != 0) {
  212. snprintf(str, sizeof(str), "%d", buf[126]);
  213. av_dict_set(&s->metadata, "track", str, 0);
  214. }
  215. genre = buf[127];
  216. if (genre <= ID3v1_GENRE_MAX)
  217. av_dict_set(&s->metadata, "genre", ff_id3v1_genre_str[genre], 0);
  218. return 0;
  219. }
  220. void ff_id3v1_read(AVFormatContext *s)
  221. {
  222. int ret;
  223. uint8_t buf[ID3v1_TAG_SIZE];
  224. int64_t filesize, position = avio_tell(s->pb);
  225. if (s->pb->seekable) {
  226. /* XXX: change that */
  227. filesize = avio_size(s->pb);
  228. if (filesize > 128) {
  229. avio_seek(s->pb, filesize - 128, SEEK_SET);
  230. ret = avio_read(s->pb, buf, ID3v1_TAG_SIZE);
  231. if (ret == ID3v1_TAG_SIZE) {
  232. parse_tag(s, buf);
  233. }
  234. avio_seek(s->pb, position, SEEK_SET);
  235. }
  236. }
  237. }