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.

308 lines
8.4KB

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