id3v2.c 11 KB

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