id3v2.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. int ff_id3v2_match(const uint8_t *buf)
  25. {
  26. return buf[0] == 'I' &&
  27. buf[1] == 'D' &&
  28. buf[2] == '3' &&
  29. buf[3] != 0xff &&
  30. buf[4] != 0xff &&
  31. (buf[6] & 0x80) == 0 &&
  32. (buf[7] & 0x80) == 0 &&
  33. (buf[8] & 0x80) == 0 &&
  34. (buf[9] & 0x80) == 0;
  35. }
  36. int ff_id3v2_tag_len(const uint8_t * buf)
  37. {
  38. int len = ((buf[6] & 0x7f) << 21) +
  39. ((buf[7] & 0x7f) << 14) +
  40. ((buf[8] & 0x7f) << 7) +
  41. (buf[9] & 0x7f) +
  42. ID3v2_HEADER_SIZE;
  43. if (buf[5] & 0x10)
  44. len += ID3v2_HEADER_SIZE;
  45. return len;
  46. }
  47. void ff_id3v2_read(AVFormatContext *s)
  48. {
  49. int len, ret;
  50. uint8_t buf[ID3v2_HEADER_SIZE];
  51. ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
  52. if (ret != ID3v2_HEADER_SIZE)
  53. return;
  54. if (ff_id3v2_match(buf)) {
  55. /* parse ID3v2 header */
  56. len = ((buf[6] & 0x7f) << 21) |
  57. ((buf[7] & 0x7f) << 14) |
  58. ((buf[8] & 0x7f) << 7) |
  59. (buf[9] & 0x7f);
  60. ff_id3v2_parse(s, len, buf[3], buf[5]);
  61. } else {
  62. url_fseek(s->pb, 0, SEEK_SET);
  63. }
  64. }
  65. static unsigned int get_size(ByteIOContext *s, int len)
  66. {
  67. int v = 0;
  68. while (len--)
  69. v = (v << 7) + (get_byte(s) & 0x7F);
  70. return v;
  71. }
  72. static void read_ttag(AVFormatContext *s, int taglen, const char *key)
  73. {
  74. char *q, dst[512];
  75. const char *val = NULL;
  76. int len, dstlen = sizeof(dst) - 1;
  77. unsigned genre;
  78. unsigned int (*get)(ByteIOContext*) = get_be16;
  79. dst[0] = 0;
  80. if (taglen < 1)
  81. return;
  82. taglen--; /* account for encoding type byte */
  83. switch (get_byte(s->pb)) { /* encoding type */
  84. case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
  85. q = dst;
  86. while (taglen-- && q - dst < dstlen - 7) {
  87. uint8_t tmp;
  88. PUT_UTF8(get_byte(s->pb), tmp, *q++ = tmp;)
  89. }
  90. *q = 0;
  91. break;
  92. case 1: /* UTF-16 with BOM */
  93. taglen -= 2;
  94. switch (get_be16(s->pb)) {
  95. case 0xfffe:
  96. get = get_le16;
  97. case 0xfeff:
  98. break;
  99. default:
  100. av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
  101. return;
  102. }
  103. // fall-through
  104. case 2: /* UTF-16BE without BOM */
  105. q = dst;
  106. while (taglen > 1 && q - dst < dstlen - 7) {
  107. uint32_t ch;
  108. uint8_t tmp;
  109. GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(s->pb) : 0), break;)
  110. PUT_UTF8(ch, tmp, *q++ = tmp;)
  111. }
  112. *q = 0;
  113. break;
  114. case 3: /* UTF-8 */
  115. len = FFMIN(taglen, dstlen - 1);
  116. get_buffer(s->pb, dst, len);
  117. dst[len] = 0;
  118. break;
  119. default:
  120. av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s\n.", key);
  121. }
  122. if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
  123. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  124. && genre <= ID3v1_GENRE_MAX)
  125. val = ff_id3v1_genre_str[genre];
  126. else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  127. /* dst now contains two 0-terminated strings */
  128. dst[dstlen] = 0;
  129. len = strlen(dst);
  130. key = dst;
  131. val = dst + FFMIN(len + 1, dstlen);
  132. }
  133. else if (*dst)
  134. val = dst;
  135. if (val)
  136. av_metadata_set(&s->metadata, key, val);
  137. }
  138. void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  139. {
  140. int isv34, tlen;
  141. char tag[5];
  142. int64_t next;
  143. int taghdrlen;
  144. const char *reason;
  145. switch (version) {
  146. case 2:
  147. if (flags & 0x40) {
  148. reason = "compression";
  149. goto error;
  150. }
  151. isv34 = 0;
  152. taghdrlen = 6;
  153. break;
  154. case 3:
  155. case 4:
  156. isv34 = 1;
  157. taghdrlen = 10;
  158. break;
  159. default:
  160. reason = "version";
  161. goto error;
  162. }
  163. if (flags & 0x80) {
  164. reason = "unsynchronization";
  165. goto error;
  166. }
  167. if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
  168. url_fskip(s->pb, get_size(s->pb, 4));
  169. while (len >= taghdrlen) {
  170. if (isv34) {
  171. get_buffer(s->pb, tag, 4);
  172. tag[4] = 0;
  173. if(version==3){
  174. tlen = get_be32(s->pb);
  175. }else
  176. tlen = get_size(s->pb, 4);
  177. get_be16(s->pb); /* flags */
  178. } else {
  179. get_buffer(s->pb, tag, 3);
  180. tag[3] = 0;
  181. tlen = get_be24(s->pb);
  182. }
  183. len -= taghdrlen + tlen;
  184. if (len < 0)
  185. break;
  186. next = url_ftell(s->pb) + tlen;
  187. if (tag[0] == 'T')
  188. read_ttag(s, tlen, tag);
  189. else if (!tag[0]) {
  190. if (tag[1])
  191. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  192. url_fskip(s->pb, len);
  193. break;
  194. }
  195. /* Skip to end of tag */
  196. url_fseek(s->pb, next, SEEK_SET);
  197. }
  198. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  199. url_fskip(s->pb, 10);
  200. return;
  201. error:
  202. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  203. url_fskip(s->pb, len);
  204. }
  205. const AVMetadataConv ff_id3v2_metadata_conv[] = {
  206. { "TALB", "album"},
  207. { "TAL", "album"},
  208. { "TCOM", "composer"},
  209. { "TCON", "genre"},
  210. { "TCO", "genre"},
  211. { "TCOP", "copyright"},
  212. { "TDRL", "date"},
  213. { "TDRC", "date"},
  214. { "TENC", "encoded_by"},
  215. { "TEN", "encoded_by"},
  216. { "TIT2", "title"},
  217. { "TT2", "title"},
  218. { "TLAN", "language"},
  219. { "TPE1", "artist"},
  220. { "TP1", "artist"},
  221. { "TPE2", "album_artist"},
  222. { "TP2", "album_artist"},
  223. { "TPE3", "performer"},
  224. { "TP3", "performer"},
  225. { "TPOS", "disc"},
  226. { "TPUB", "publisher"},
  227. { "TRCK", "track"},
  228. { "TRK", "track"},
  229. { "TSOA", "album-sort"},
  230. { "TSOP", "artist-sort"},
  231. { "TSOT", "title-sort"},
  232. { "TSSE", "encoder"},
  233. { 0 }
  234. };
  235. const char ff_id3v2_tags[][4] = {
  236. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDEN", "TDLY", "TDOR", "TDRC",
  237. "TDRL", "TDTG", "TENC", "TEXT", "TFLT", "TIPL", "TIT1", "TIT2", "TIT3",
  238. "TKEY", "TLAN", "TLEN", "TMCL", "TMED", "TMOO", "TOAL", "TOFN", "TOLY",
  239. "TOPE", "TOWN", "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPRO", "TPUB",
  240. "TRCK", "TRSN", "TRSO", "TSOA", "TSOP", "TSOT", "TSRC", "TSSE", "TSST",
  241. { 0 },
  242. };