id3v1.c 6.2 KB

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