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.

319 lines
8.9KB

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