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 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 int is_number(const char *str)
  124. {
  125. while (*str >= '0' && *str <= '9') str++;
  126. return !*str;
  127. }
  128. static AVMetadataTag* get_date_tag(AVMetadata *m, const char *tag)
  129. {
  130. AVMetadataTag *t;
  131. if ((t = av_metadata_get(m, tag, NULL, AV_METADATA_MATCH_CASE)) &&
  132. strlen(t->value) == 4 && is_number(t->value))
  133. return t;
  134. return NULL;
  135. }
  136. static void merge_date(AVMetadata **m)
  137. {
  138. AVMetadataTag *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_metadata_set2(m, "TYER", NULL, 0);
  145. av_metadata_set2(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_metadata_set2(m, "TDAT", NULL, 0);
  151. av_metadata_set2(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_metadata_set2(m, "TIME", NULL, 0);
  157. av_metadata_set2(m, "TIM", NULL, 0);
  158. finish:
  159. if (date[0])
  160. av_metadata_set2(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;
  167. int taghdrlen;
  168. const char *reason;
  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_seek(s->pb, get_size(s->pb, 4), SEEK_CUR);
  193. while (len >= taghdrlen) {
  194. unsigned int tflags;
  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. len -= taghdrlen + tlen;
  211. if (len < 0)
  212. break;
  213. next = avio_tell(s->pb) + tlen;
  214. if (tflags & ID3v2_FLAG_DATALEN) {
  215. avio_rb32(s->pb);
  216. tlen -= 4;
  217. }
  218. if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
  219. av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
  220. avio_seek(s->pb, tlen, SEEK_CUR);
  221. } else if (tag[0] == 'T') {
  222. if (unsync || tunsync) {
  223. int i, j;
  224. av_fast_malloc(&buffer, &buffer_size, tlen);
  225. for (i = 0, j = 0; i < tlen; i++, j++) {
  226. buffer[j] = avio_r8(s->pb);
  227. if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
  228. /* Unsynchronised byte, skip it */
  229. j--;
  230. }
  231. }
  232. ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
  233. read_ttag(s, &pb, j, tag);
  234. } else {
  235. read_ttag(s, s->pb, tlen, tag);
  236. }
  237. }
  238. else if (!tag[0]) {
  239. if (tag[1])
  240. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  241. avio_seek(s->pb, tlen, SEEK_CUR);
  242. break;
  243. }
  244. /* Skip to end of tag */
  245. avio_seek(s->pb, next, SEEK_SET);
  246. }
  247. if (len > 0) {
  248. /* Skip padding */
  249. avio_seek(s->pb, len, SEEK_CUR);
  250. }
  251. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  252. avio_seek(s->pb, 10, SEEK_CUR);
  253. av_free(buffer);
  254. return;
  255. error:
  256. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  257. avio_seek(s->pb, len, SEEK_CUR);
  258. av_free(buffer);
  259. }
  260. void ff_id3v2_read(AVFormatContext *s, const char *magic)
  261. {
  262. int len, ret;
  263. uint8_t buf[ID3v2_HEADER_SIZE];
  264. int found_header;
  265. int64_t off;
  266. do {
  267. /* save the current offset in case there's nothing to read/skip */
  268. off = avio_tell(s->pb);
  269. ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
  270. if (ret != ID3v2_HEADER_SIZE)
  271. break;
  272. found_header = ff_id3v2_match(buf, magic);
  273. if (found_header) {
  274. /* parse ID3v2 header */
  275. len = ((buf[6] & 0x7f) << 21) |
  276. ((buf[7] & 0x7f) << 14) |
  277. ((buf[8] & 0x7f) << 7) |
  278. (buf[9] & 0x7f);
  279. ff_id3v2_parse(s, len, buf[3], buf[5]);
  280. } else {
  281. avio_seek(s->pb, off, SEEK_SET);
  282. }
  283. } while (found_header);
  284. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  285. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_2_metadata_conv);
  286. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  287. merge_date(&s->metadata);
  288. }
  289. const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
  290. { "TALB", "album"},
  291. { "TCOM", "composer"},
  292. { "TCON", "genre"},
  293. { "TCOP", "copyright"},
  294. { "TENC", "encoded_by"},
  295. { "TIT2", "title"},
  296. { "TLAN", "language"},
  297. { "TPE1", "artist"},
  298. { "TPE2", "album_artist"},
  299. { "TPE3", "performer"},
  300. { "TPOS", "disc"},
  301. { "TPUB", "publisher"},
  302. { "TRCK", "track"},
  303. { "TSSE", "encoder"},
  304. { 0 }
  305. };
  306. const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
  307. { "TDRL", "date"},
  308. { "TDRC", "date"},
  309. { "TDEN", "creation_time"},
  310. { "TSOA", "album-sort"},
  311. { "TSOP", "artist-sort"},
  312. { "TSOT", "title-sort"},
  313. { 0 }
  314. };
  315. const AVMetadataConv ff_id3v2_2_metadata_conv[] = {
  316. { "TAL", "album"},
  317. { "TCO", "genre"},
  318. { "TT2", "title"},
  319. { "TEN", "encoded_by"},
  320. { "TP1", "artist"},
  321. { "TP2", "album_artist"},
  322. { "TP3", "performer"},
  323. { "TRK", "track"},
  324. { 0 }
  325. };
  326. const char ff_id3v2_tags[][4] = {
  327. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  328. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  329. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  330. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  331. { 0 },
  332. };
  333. const char ff_id3v2_4_tags[][4] = {
  334. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  335. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  336. { 0 },
  337. };
  338. const char ff_id3v2_3_tags[][4] = {
  339. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  340. { 0 },
  341. };