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.

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