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.

301 lines
8.2KB

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