id3v2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) 2003 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * ID3v2 header parser
  23. *
  24. * Specifications available at:
  25. * http://id3.org/Developer_Information
  26. */
  27. #include "id3v2.h"
  28. #include "id3v1.h"
  29. #include "libavutil/avstring.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/dict.h"
  32. #include "avio_internal.h"
  33. int ff_id3v2_match(const uint8_t *buf, const char * magic)
  34. {
  35. return buf[0] == magic[0] &&
  36. buf[1] == magic[1] &&
  37. buf[2] == magic[2] &&
  38. buf[3] != 0xff &&
  39. buf[4] != 0xff &&
  40. (buf[6] & 0x80) == 0 &&
  41. (buf[7] & 0x80) == 0 &&
  42. (buf[8] & 0x80) == 0 &&
  43. (buf[9] & 0x80) == 0;
  44. }
  45. int ff_id3v2_tag_len(const uint8_t * buf)
  46. {
  47. int len = ((buf[6] & 0x7f) << 21) +
  48. ((buf[7] & 0x7f) << 14) +
  49. ((buf[8] & 0x7f) << 7) +
  50. (buf[9] & 0x7f) +
  51. ID3v2_HEADER_SIZE;
  52. if (buf[5] & 0x10)
  53. len += ID3v2_HEADER_SIZE;
  54. return len;
  55. }
  56. static unsigned int get_size(AVIOContext *s, int len)
  57. {
  58. int v = 0;
  59. while (len--)
  60. v = (v << 7) + (avio_r8(s) & 0x7F);
  61. return v;
  62. }
  63. static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
  64. {
  65. char *q, dst[512];
  66. const char *val = NULL;
  67. int len, dstlen = sizeof(dst) - 1;
  68. unsigned genre;
  69. unsigned int (*get)(AVIOContext*) = avio_rb16;
  70. dst[0] = 0;
  71. if (taglen < 1)
  72. return;
  73. taglen--; /* account for encoding type byte */
  74. switch (avio_r8(pb)) { /* encoding type */
  75. case ID3v2_ENCODING_ISO8859:
  76. q = dst;
  77. while (taglen-- && q - dst < dstlen - 7) {
  78. uint8_t tmp;
  79. PUT_UTF8(avio_r8(pb), tmp, *q++ = tmp;)
  80. }
  81. *q = 0;
  82. break;
  83. case ID3v2_ENCODING_UTF16BOM:
  84. taglen -= 2;
  85. switch (avio_rb16(pb)) {
  86. case 0xfffe:
  87. get = avio_rl16;
  88. case 0xfeff:
  89. break;
  90. default:
  91. av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
  92. return;
  93. }
  94. // fall-through
  95. case ID3v2_ENCODING_UTF16BE:
  96. q = dst;
  97. while (taglen > 1 && q - dst < dstlen - 7) {
  98. uint32_t ch;
  99. uint8_t tmp;
  100. GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(pb) : 0), break;)
  101. PUT_UTF8(ch, tmp, *q++ = tmp;)
  102. }
  103. *q = 0;
  104. break;
  105. case ID3v2_ENCODING_UTF8:
  106. len = FFMIN(taglen, dstlen);
  107. avio_read(pb, dst, len);
  108. dst[len] = 0;
  109. break;
  110. default:
  111. av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s.\n", key);
  112. }
  113. if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
  114. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  115. && genre <= ID3v1_GENRE_MAX)
  116. val = ff_id3v1_genre_str[genre];
  117. else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  118. /* dst now contains two 0-terminated strings */
  119. dst[dstlen] = 0;
  120. len = strlen(dst);
  121. key = dst;
  122. val = dst + FFMIN(len + 1, dstlen);
  123. }
  124. else if (*dst)
  125. val = dst;
  126. if (val)
  127. av_dict_set(&s->metadata, key, val, AV_DICT_DONT_OVERWRITE);
  128. }
  129. static int is_number(const char *str)
  130. {
  131. while (*str >= '0' && *str <= '9') str++;
  132. return !*str;
  133. }
  134. static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
  135. {
  136. AVDictionaryEntry *t;
  137. if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
  138. strlen(t->value) == 4 && is_number(t->value))
  139. return t;
  140. return NULL;
  141. }
  142. static void merge_date(AVDictionary **m)
  143. {
  144. AVDictionaryEntry *t;
  145. char date[17] = {0}; // YYYY-MM-DD hh:mm
  146. if (!(t = get_date_tag(*m, "TYER")) &&
  147. !(t = get_date_tag(*m, "TYE")))
  148. return;
  149. av_strlcpy(date, t->value, 5);
  150. av_dict_set(m, "TYER", NULL, 0);
  151. av_dict_set(m, "TYE", NULL, 0);
  152. if (!(t = get_date_tag(*m, "TDAT")) &&
  153. !(t = get_date_tag(*m, "TDA")))
  154. goto finish;
  155. snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
  156. av_dict_set(m, "TDAT", NULL, 0);
  157. av_dict_set(m, "TDA", NULL, 0);
  158. if (!(t = get_date_tag(*m, "TIME")) &&
  159. !(t = get_date_tag(*m, "TIM")))
  160. goto finish;
  161. snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
  162. av_dict_set(m, "TIME", NULL, 0);
  163. av_dict_set(m, "TIM", NULL, 0);
  164. finish:
  165. if (date[0])
  166. av_dict_set(m, "date", date, 0);
  167. }
  168. static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  169. {
  170. int isv34, unsync;
  171. unsigned tlen;
  172. char tag[5];
  173. int64_t next, end = avio_tell(s->pb) + len;
  174. int taghdrlen;
  175. const char *reason = NULL;
  176. AVIOContext pb;
  177. unsigned char *buffer = NULL;
  178. int buffer_size = 0;
  179. switch (version) {
  180. case 2:
  181. if (flags & 0x40) {
  182. reason = "compression";
  183. goto error;
  184. }
  185. isv34 = 0;
  186. taghdrlen = 6;
  187. break;
  188. case 3:
  189. case 4:
  190. isv34 = 1;
  191. taghdrlen = 10;
  192. break;
  193. default:
  194. reason = "version";
  195. goto error;
  196. }
  197. unsync = flags & 0x80;
  198. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  199. int extlen = get_size(s->pb, 4);
  200. if (version == 4)
  201. extlen -= 4; // in v2.4 the length includes the length field we just read
  202. if (extlen < 0) {
  203. reason = "invalid extended header length";
  204. goto error;
  205. }
  206. avio_skip(s->pb, extlen);
  207. }
  208. while (len >= taghdrlen) {
  209. unsigned int tflags = 0;
  210. int tunsync = 0;
  211. if (isv34) {
  212. avio_read(s->pb, tag, 4);
  213. tag[4] = 0;
  214. if(version==3){
  215. tlen = avio_rb32(s->pb);
  216. }else
  217. tlen = get_size(s->pb, 4);
  218. tflags = avio_rb16(s->pb);
  219. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  220. } else {
  221. avio_read(s->pb, tag, 3);
  222. tag[3] = 0;
  223. tlen = avio_rb24(s->pb);
  224. }
  225. if (tlen > (1<<28) || !tlen)
  226. break;
  227. len -= taghdrlen + tlen;
  228. if (len < 0)
  229. break;
  230. next = avio_tell(s->pb) + tlen;
  231. if (tflags & ID3v2_FLAG_DATALEN) {
  232. if (tlen < 4)
  233. break;
  234. avio_rb32(s->pb);
  235. tlen -= 4;
  236. }
  237. if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
  238. av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
  239. avio_skip(s->pb, tlen);
  240. } else if (tag[0] == 'T') {
  241. if (unsync || tunsync) {
  242. int i, j;
  243. av_fast_malloc(&buffer, &buffer_size, tlen);
  244. if (!buffer) {
  245. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  246. goto seek;
  247. }
  248. for (i = 0, j = 0; i < tlen; i++, j++) {
  249. buffer[j] = avio_r8(s->pb);
  250. if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
  251. /* Unsynchronised byte, skip it */
  252. j--;
  253. }
  254. }
  255. ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
  256. read_ttag(s, &pb, j, tag);
  257. } else {
  258. read_ttag(s, s->pb, tlen, tag);
  259. }
  260. }
  261. else if (!tag[0]) {
  262. if (tag[1])
  263. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  264. avio_skip(s->pb, tlen);
  265. break;
  266. }
  267. /* Skip to end of tag */
  268. seek:
  269. avio_seek(s->pb, next, SEEK_SET);
  270. }
  271. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  272. end += 10;
  273. error:
  274. if (reason)
  275. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  276. avio_seek(s->pb, end, SEEK_SET);
  277. av_free(buffer);
  278. return;
  279. }
  280. void ff_id3v2_read(AVFormatContext *s, const char *magic)
  281. {
  282. int len, ret;
  283. uint8_t buf[ID3v2_HEADER_SIZE];
  284. int found_header;
  285. int64_t off;
  286. do {
  287. /* save the current offset in case there's nothing to read/skip */
  288. off = avio_tell(s->pb);
  289. ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
  290. if (ret != ID3v2_HEADER_SIZE)
  291. break;
  292. found_header = ff_id3v2_match(buf, magic);
  293. if (found_header) {
  294. /* parse ID3v2 header */
  295. len = ((buf[6] & 0x7f) << 21) |
  296. ((buf[7] & 0x7f) << 14) |
  297. ((buf[8] & 0x7f) << 7) |
  298. (buf[9] & 0x7f);
  299. ff_id3v2_parse(s, len, buf[3], buf[5]);
  300. } else {
  301. avio_seek(s->pb, off, SEEK_SET);
  302. }
  303. } while (found_header);
  304. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  305. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_2_metadata_conv);
  306. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  307. merge_date(&s->metadata);
  308. }
  309. const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
  310. { "TALB", "album"},
  311. { "TCOM", "composer"},
  312. { "TCON", "genre"},
  313. { "TCOP", "copyright"},
  314. { "TENC", "encoded_by"},
  315. { "TIT2", "title"},
  316. { "TLAN", "language"},
  317. { "TPE1", "artist"},
  318. { "TPE2", "album_artist"},
  319. { "TPE3", "performer"},
  320. { "TPOS", "disc"},
  321. { "TPUB", "publisher"},
  322. { "TRCK", "track"},
  323. { "TSSE", "encoder"},
  324. { 0 }
  325. };
  326. const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
  327. { "TDRL", "date"},
  328. { "TDRC", "date"},
  329. { "TDEN", "creation_time"},
  330. { "TSOA", "album-sort"},
  331. { "TSOP", "artist-sort"},
  332. { "TSOT", "title-sort"},
  333. { 0 }
  334. };
  335. const AVMetadataConv ff_id3v2_2_metadata_conv[] = {
  336. { "TAL", "album"},
  337. { "TCO", "genre"},
  338. { "TT2", "title"},
  339. { "TEN", "encoded_by"},
  340. { "TP1", "artist"},
  341. { "TP2", "album_artist"},
  342. { "TP3", "performer"},
  343. { "TRK", "track"},
  344. { 0 }
  345. };
  346. const char ff_id3v2_tags[][4] = {
  347. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  348. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  349. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  350. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  351. { 0 },
  352. };
  353. const char ff_id3v2_4_tags[][4] = {
  354. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  355. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  356. { 0 },
  357. };
  358. const char ff_id3v2_3_tags[][4] = {
  359. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  360. { 0 },
  361. };