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.

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