id3v2.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * ID3v2 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 "id3v2.h"
  22. #include "id3v1.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "metadata.h"
  26. int ff_id3v2_match(const uint8_t *buf, const char * magic)
  27. {
  28. return buf[0] == magic[0] &&
  29. buf[1] == magic[1] &&
  30. buf[2] == magic[2] &&
  31. buf[3] != 0xff &&
  32. buf[4] != 0xff &&
  33. (buf[6] & 0x80) == 0 &&
  34. (buf[7] & 0x80) == 0 &&
  35. (buf[8] & 0x80) == 0 &&
  36. (buf[9] & 0x80) == 0;
  37. }
  38. int ff_id3v2_tag_len(const uint8_t * buf)
  39. {
  40. int len = ((buf[6] & 0x7f) << 21) +
  41. ((buf[7] & 0x7f) << 14) +
  42. ((buf[8] & 0x7f) << 7) +
  43. (buf[9] & 0x7f) +
  44. ID3v2_HEADER_SIZE;
  45. if (buf[5] & 0x10)
  46. len += ID3v2_HEADER_SIZE;
  47. return len;
  48. }
  49. static unsigned int get_size(ByteIOContext *s, int len)
  50. {
  51. int v = 0;
  52. while (len--)
  53. v = (v << 7) + (get_byte(s) & 0x7F);
  54. return v;
  55. }
  56. static void read_ttag(AVFormatContext *s, ByteIOContext *pb, int taglen, const char *key)
  57. {
  58. char *q, dst[512];
  59. const char *val = NULL;
  60. int len, dstlen = sizeof(dst) - 1;
  61. unsigned genre;
  62. unsigned int (*get)(ByteIOContext*) = get_be16;
  63. dst[0] = 0;
  64. if (taglen < 1)
  65. return;
  66. taglen--; /* account for encoding type byte */
  67. switch (get_byte(pb)) { /* encoding type */
  68. case ID3v2_ENCODING_ISO8859:
  69. q = dst;
  70. while (taglen-- && q - dst < dstlen - 7) {
  71. uint8_t tmp;
  72. PUT_UTF8(get_byte(pb), tmp, *q++ = tmp;)
  73. }
  74. *q = 0;
  75. break;
  76. case ID3v2_ENCODING_UTF16BOM:
  77. taglen -= 2;
  78. switch (get_be16(pb)) {
  79. case 0xfffe:
  80. get = get_le16;
  81. case 0xfeff:
  82. break;
  83. default:
  84. av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
  85. return;
  86. }
  87. // fall-through
  88. case ID3v2_ENCODING_UTF16BE:
  89. q = dst;
  90. while (taglen > 1 && q - dst < dstlen - 7) {
  91. uint32_t ch;
  92. uint8_t tmp;
  93. GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(pb) : 0), break;)
  94. PUT_UTF8(ch, tmp, *q++ = tmp;)
  95. }
  96. *q = 0;
  97. break;
  98. case ID3v2_ENCODING_UTF8:
  99. len = FFMIN(taglen, dstlen);
  100. get_buffer(pb, dst, len);
  101. dst[len] = 0;
  102. break;
  103. default:
  104. av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s\n.", key);
  105. }
  106. if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
  107. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  108. && genre <= ID3v1_GENRE_MAX)
  109. val = ff_id3v1_genre_str[genre];
  110. else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  111. /* dst now contains two 0-terminated strings */
  112. dst[dstlen] = 0;
  113. len = strlen(dst);
  114. key = dst;
  115. val = dst + FFMIN(len + 1, dstlen);
  116. }
  117. else if (*dst)
  118. val = dst;
  119. if (val)
  120. av_metadata_set2(&s->metadata, key, val, AV_METADATA_DONT_OVERWRITE);
  121. }
  122. static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  123. {
  124. int isv34, tlen, unsync;
  125. char tag[5];
  126. int64_t next;
  127. int taghdrlen;
  128. const char *reason;
  129. ByteIOContext pb;
  130. unsigned char *buffer = NULL;
  131. int buffer_size = 0;
  132. switch (version) {
  133. case 2:
  134. if (flags & 0x40) {
  135. reason = "compression";
  136. goto error;
  137. }
  138. isv34 = 0;
  139. taghdrlen = 6;
  140. break;
  141. case 3:
  142. case 4:
  143. isv34 = 1;
  144. taghdrlen = 10;
  145. break;
  146. default:
  147. reason = "version";
  148. goto error;
  149. }
  150. unsync = flags & 0x80;
  151. if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
  152. url_fskip(s->pb, get_size(s->pb, 4));
  153. while (len >= taghdrlen) {
  154. unsigned int tflags;
  155. int tunsync = 0;
  156. if (isv34) {
  157. get_buffer(s->pb, tag, 4);
  158. tag[4] = 0;
  159. if(version==3){
  160. tlen = get_be32(s->pb);
  161. }else
  162. tlen = get_size(s->pb, 4);
  163. tflags = get_be16(s->pb);
  164. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  165. } else {
  166. get_buffer(s->pb, tag, 3);
  167. tag[3] = 0;
  168. tlen = get_be24(s->pb);
  169. }
  170. len -= taghdrlen + tlen;
  171. if (len < 0)
  172. break;
  173. next = url_ftell(s->pb) + tlen;
  174. if (tflags & ID3v2_FLAG_DATALEN) {
  175. get_be32(s->pb);
  176. tlen -= 4;
  177. }
  178. if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
  179. av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
  180. url_fskip(s->pb, tlen);
  181. } else if (tag[0] == 'T') {
  182. if (unsync || tunsync) {
  183. int i, j;
  184. av_fast_malloc(&buffer, &buffer_size, tlen);
  185. for (i = 0, j = 0; i < tlen; i++, j++) {
  186. buffer[j] = get_byte(s->pb);
  187. if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
  188. /* Unsynchronised byte, skip it */
  189. j--;
  190. }
  191. }
  192. init_put_byte(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
  193. read_ttag(s, &pb, j, tag);
  194. } else {
  195. read_ttag(s, s->pb, tlen, tag);
  196. }
  197. }
  198. else if (!tag[0]) {
  199. if (tag[1])
  200. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  201. url_fskip(s->pb, tlen);
  202. break;
  203. }
  204. /* Skip to end of tag */
  205. url_fseek(s->pb, next, SEEK_SET);
  206. }
  207. if (len > 0) {
  208. /* Skip padding */
  209. url_fskip(s->pb, len);
  210. }
  211. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  212. url_fskip(s->pb, 10);
  213. av_free(buffer);
  214. return;
  215. error:
  216. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  217. url_fskip(s->pb, len);
  218. av_free(buffer);
  219. }
  220. void ff_id3v2_read(AVFormatContext *s, const char *magic)
  221. {
  222. int len, ret;
  223. uint8_t buf[ID3v2_HEADER_SIZE];
  224. int found_header;
  225. int64_t off;
  226. do {
  227. /* save the current offset in case there's nothing to read/skip */
  228. off = url_ftell(s->pb);
  229. ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
  230. if (ret != ID3v2_HEADER_SIZE)
  231. break;
  232. found_header = ff_id3v2_match(buf, magic);
  233. if (found_header) {
  234. /* parse ID3v2 header */
  235. len = ((buf[6] & 0x7f) << 21) |
  236. ((buf[7] & 0x7f) << 14) |
  237. ((buf[8] & 0x7f) << 7) |
  238. (buf[9] & 0x7f);
  239. ff_id3v2_parse(s, len, buf[3], buf[5]);
  240. } else {
  241. url_fseek(s->pb, off, SEEK_SET);
  242. }
  243. } while (found_header);
  244. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  245. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_2_metadata_conv);
  246. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  247. }
  248. const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
  249. { "TALB", "album"},
  250. { "TCOM", "composer"},
  251. { "TCON", "genre"},
  252. { "TCOP", "copyright"},
  253. { "TENC", "encoded_by"},
  254. { "TIT2", "title"},
  255. { "TLAN", "language"},
  256. { "TPE1", "artist"},
  257. { "TPE2", "album_artist"},
  258. { "TPE3", "performer"},
  259. { "TPOS", "disc"},
  260. { "TPUB", "publisher"},
  261. { "TRCK", "track"},
  262. { "TSSE", "encoder"},
  263. { 0 }
  264. };
  265. const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
  266. { "TDRL", "date"},
  267. { "TDRC", "date"},
  268. { "TDEN", "creation_time"},
  269. { "TSOA", "album-sort"},
  270. { "TSOP", "artist-sort"},
  271. { "TSOT", "title-sort"},
  272. { 0 }
  273. };
  274. const AVMetadataConv ff_id3v2_2_metadata_conv[] = {
  275. { "TAL", "album"},
  276. { "TCO", "genre"},
  277. { "TT2", "title"},
  278. { "TEN", "encoded_by"},
  279. { "TP1", "artist"},
  280. { "TP2", "album_artist"},
  281. { "TP3", "performer"},
  282. { "TRK", "track"},
  283. { 0 }
  284. };
  285. const char ff_id3v2_tags[][4] = {
  286. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  287. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  288. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  289. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  290. { 0 },
  291. };
  292. const char ff_id3v2_4_tags[][4] = {
  293. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  294. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  295. { 0 },
  296. };
  297. const char ff_id3v2_3_tags[][4] = {
  298. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  299. { 0 },
  300. };