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.

388 lines
11KB

  1. /*
  2. * ID3v2 header parser
  3. * Copyright (c) 2003 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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 "libavutil/dict.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_dict_set(&s->metadata, key, val, AV_DICT_DONT_OVERWRITE);
  122. }
  123. static int is_number(const char *str)
  124. {
  125. while (*str >= '0' && *str <= '9') str++;
  126. return !*str;
  127. }
  128. static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
  129. {
  130. AVDictionaryEntry *t;
  131. if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
  132. strlen(t->value) == 4 && is_number(t->value))
  133. return t;
  134. return NULL;
  135. }
  136. static void merge_date(AVDictionary **m)
  137. {
  138. AVDictionaryEntry *t;
  139. char date[17] = {0}; // YYYY-MM-DD hh:mm
  140. if (!(t = get_date_tag(*m, "TYER")) &&
  141. !(t = get_date_tag(*m, "TYE")))
  142. return;
  143. av_strlcpy(date, t->value, 5);
  144. av_dict_set(m, "TYER", NULL, 0);
  145. av_dict_set(m, "TYE", NULL, 0);
  146. if (!(t = get_date_tag(*m, "TDAT")) &&
  147. !(t = get_date_tag(*m, "TDA")))
  148. goto finish;
  149. snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
  150. av_dict_set(m, "TDAT", NULL, 0);
  151. av_dict_set(m, "TDA", NULL, 0);
  152. if (!(t = get_date_tag(*m, "TIME")) &&
  153. !(t = get_date_tag(*m, "TIM")))
  154. goto finish;
  155. snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
  156. av_dict_set(m, "TIME", NULL, 0);
  157. av_dict_set(m, "TIM", NULL, 0);
  158. finish:
  159. if (date[0])
  160. av_dict_set(m, "date", date, 0);
  161. }
  162. static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  163. {
  164. int isv34, tlen, unsync;
  165. char tag[5];
  166. int64_t next, end = avio_tell(s->pb) + len;
  167. int taghdrlen;
  168. const char *reason = NULL;
  169. AVIOContext pb;
  170. unsigned char *buffer = NULL;
  171. int buffer_size = 0;
  172. switch (version) {
  173. case 2:
  174. if (flags & 0x40) {
  175. reason = "compression";
  176. goto error;
  177. }
  178. isv34 = 0;
  179. taghdrlen = 6;
  180. break;
  181. case 3:
  182. case 4:
  183. isv34 = 1;
  184. taghdrlen = 10;
  185. break;
  186. default:
  187. reason = "version";
  188. goto error;
  189. }
  190. unsync = flags & 0x80;
  191. if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
  192. avio_skip(s->pb, get_size(s->pb, 4));
  193. while (len >= taghdrlen) {
  194. unsigned int tflags = 0;
  195. int tunsync = 0;
  196. if (isv34) {
  197. avio_read(s->pb, tag, 4);
  198. tag[4] = 0;
  199. if(version==3){
  200. tlen = avio_rb32(s->pb);
  201. }else
  202. tlen = get_size(s->pb, 4);
  203. tflags = avio_rb16(s->pb);
  204. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  205. } else {
  206. avio_read(s->pb, tag, 3);
  207. tag[3] = 0;
  208. tlen = avio_rb24(s->pb);
  209. }
  210. if (tlen <= 0 || tlen > len - taghdrlen) {
  211. av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
  212. break;
  213. }
  214. len -= taghdrlen + tlen;
  215. next = avio_tell(s->pb) + tlen;
  216. if (tflags & ID3v2_FLAG_DATALEN) {
  217. avio_rb32(s->pb);
  218. tlen -= 4;
  219. }
  220. if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
  221. av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
  222. avio_skip(s->pb, tlen);
  223. } else if (tag[0] == 'T') {
  224. if (unsync || tunsync) {
  225. int i, j;
  226. av_fast_malloc(&buffer, &buffer_size, tlen);
  227. if (!buffer) {
  228. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  229. goto seek;
  230. }
  231. for (i = 0, j = 0; i < tlen; i++, j++) {
  232. buffer[j] = avio_r8(s->pb);
  233. if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
  234. /* Unsynchronised byte, skip it */
  235. j--;
  236. }
  237. }
  238. ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
  239. read_ttag(s, &pb, j, tag);
  240. } else {
  241. read_ttag(s, s->pb, tlen, tag);
  242. }
  243. }
  244. else if (!tag[0]) {
  245. if (tag[1])
  246. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  247. avio_skip(s->pb, tlen);
  248. break;
  249. }
  250. /* Skip to end of tag */
  251. seek:
  252. avio_seek(s->pb, next, SEEK_SET);
  253. }
  254. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  255. end += 10;
  256. error:
  257. if (reason)
  258. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  259. avio_seek(s->pb, end, SEEK_SET);
  260. av_free(buffer);
  261. return;
  262. }
  263. void ff_id3v2_read(AVFormatContext *s, const char *magic)
  264. {
  265. int len, ret;
  266. uint8_t buf[ID3v2_HEADER_SIZE];
  267. int found_header;
  268. int64_t off;
  269. do {
  270. /* save the current offset in case there's nothing to read/skip */
  271. off = avio_tell(s->pb);
  272. ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
  273. if (ret != ID3v2_HEADER_SIZE)
  274. break;
  275. found_header = ff_id3v2_match(buf, magic);
  276. if (found_header) {
  277. /* parse ID3v2 header */
  278. len = ((buf[6] & 0x7f) << 21) |
  279. ((buf[7] & 0x7f) << 14) |
  280. ((buf[8] & 0x7f) << 7) |
  281. (buf[9] & 0x7f);
  282. ff_id3v2_parse(s, len, buf[3], buf[5]);
  283. } else {
  284. avio_seek(s->pb, off, SEEK_SET);
  285. }
  286. } while (found_header);
  287. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  288. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_2_metadata_conv);
  289. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  290. merge_date(&s->metadata);
  291. }
  292. const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
  293. { "TALB", "album"},
  294. { "TCOM", "composer"},
  295. { "TCON", "genre"},
  296. { "TCOP", "copyright"},
  297. { "TENC", "encoded_by"},
  298. { "TIT2", "title"},
  299. { "TLAN", "language"},
  300. { "TPE1", "artist"},
  301. { "TPE2", "album_artist"},
  302. { "TPE3", "performer"},
  303. { "TPOS", "disc"},
  304. { "TPUB", "publisher"},
  305. { "TRCK", "track"},
  306. { "TSSE", "encoder"},
  307. { 0 }
  308. };
  309. const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
  310. { "TDRL", "date"},
  311. { "TDRC", "date"},
  312. { "TDEN", "creation_time"},
  313. { "TSOA", "album-sort"},
  314. { "TSOP", "artist-sort"},
  315. { "TSOT", "title-sort"},
  316. { 0 }
  317. };
  318. const AVMetadataConv ff_id3v2_2_metadata_conv[] = {
  319. { "TAL", "album"},
  320. { "TCO", "genre"},
  321. { "TT2", "title"},
  322. { "TEN", "encoded_by"},
  323. { "TP1", "artist"},
  324. { "TP2", "album_artist"},
  325. { "TP3", "performer"},
  326. { "TRK", "track"},
  327. { 0 }
  328. };
  329. const char ff_id3v2_tags[][4] = {
  330. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  331. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  332. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  333. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  334. { 0 },
  335. };
  336. const char ff_id3v2_4_tags[][4] = {
  337. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  338. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  339. { 0 },
  340. };
  341. const char ff_id3v2_3_tags[][4] = {
  342. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  343. { 0 },
  344. };