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.

245 lines
6.5KB

  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. int ff_id3v2_match(const uint8_t *buf)
  25. {
  26. return buf[0] == 'I' &&
  27. buf[1] == 'D' &&
  28. buf[2] == '3' &&
  29. buf[3] != 0xff &&
  30. buf[4] != 0xff &&
  31. (buf[6] & 0x80) == 0 &&
  32. (buf[7] & 0x80) == 0 &&
  33. (buf[8] & 0x80) == 0 &&
  34. (buf[9] & 0x80) == 0;
  35. }
  36. int ff_id3v2_tag_len(const uint8_t * buf)
  37. {
  38. int len = ((buf[6] & 0x7f) << 21) +
  39. ((buf[7] & 0x7f) << 14) +
  40. ((buf[8] & 0x7f) << 7) +
  41. (buf[9] & 0x7f) +
  42. ID3v2_HEADER_SIZE;
  43. if (buf[5] & 0x10)
  44. len += ID3v2_HEADER_SIZE;
  45. return len;
  46. }
  47. void ff_id3v2_read(AVFormatContext *s)
  48. {
  49. int len, ret;
  50. uint8_t buf[ID3v2_HEADER_SIZE];
  51. ret = get_buffer(s->pb, buf, ID3v2_HEADER_SIZE);
  52. if (ret != ID3v2_HEADER_SIZE)
  53. return;
  54. if (ff_id3v2_match(buf)) {
  55. /* parse ID3v2 header */
  56. len = ((buf[6] & 0x7f) << 21) |
  57. ((buf[7] & 0x7f) << 14) |
  58. ((buf[8] & 0x7f) << 7) |
  59. (buf[9] & 0x7f);
  60. ff_id3v2_parse(s, len, buf[3], buf[5]);
  61. } else {
  62. url_fseek(s->pb, 0, SEEK_SET);
  63. }
  64. }
  65. static unsigned int get_size(ByteIOContext *s, int len)
  66. {
  67. int v = 0;
  68. while (len--)
  69. v = (v << 7) + (get_byte(s) & 0x7F);
  70. return v;
  71. }
  72. static void read_ttag(AVFormatContext *s, int taglen, const char *key)
  73. {
  74. char *q, dst[512];
  75. int len, dstlen = sizeof(dst) - 1;
  76. unsigned genre;
  77. unsigned int (*get)(ByteIOContext*) = get_be16;
  78. dst[0] = 0;
  79. if (taglen < 1)
  80. return;
  81. taglen--; /* account for encoding type byte */
  82. switch (get_byte(s->pb)) { /* encoding type */
  83. case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
  84. q = dst;
  85. while (taglen-- && q - dst < dstlen - 7) {
  86. uint8_t tmp;
  87. PUT_UTF8(get_byte(s->pb), tmp, *q++ = tmp;)
  88. }
  89. *q = 0;
  90. break;
  91. case 1: /* UTF-16 with BOM */
  92. taglen -= 2;
  93. switch (get_be16(s->pb)) {
  94. case 0xfffe:
  95. get = get_le16;
  96. case 0xfeff:
  97. break;
  98. default:
  99. av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
  100. return;
  101. }
  102. // fall-through
  103. case 2: /* UTF-16BE without BOM */
  104. q = dst;
  105. while (taglen > 1 && q - dst < dstlen - 7) {
  106. uint32_t ch;
  107. uint8_t tmp;
  108. GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(s->pb) : 0), break;)
  109. PUT_UTF8(ch, tmp, *q++ = tmp;)
  110. }
  111. *q = 0;
  112. break;
  113. case 3: /* UTF-8 */
  114. len = FFMIN(taglen, dstlen - 1);
  115. get_buffer(s->pb, dst, len);
  116. dst[len] = 0;
  117. break;
  118. default:
  119. av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s\n.", key);
  120. }
  121. if (!strcmp(key, "genre")
  122. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  123. && genre <= ID3v1_GENRE_MAX)
  124. av_strlcpy(dst, ff_id3v1_genre_str[genre], sizeof(dst));
  125. if (*dst)
  126. av_metadata_set(&s->metadata, key, dst);
  127. }
  128. void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  129. {
  130. int isv34, tlen;
  131. uint32_t tag;
  132. int64_t next;
  133. int taghdrlen;
  134. const char *reason;
  135. switch (version) {
  136. case 2:
  137. if (flags & 0x40) {
  138. reason = "compression";
  139. goto error;
  140. }
  141. isv34 = 0;
  142. taghdrlen = 6;
  143. break;
  144. case 3:
  145. case 4:
  146. isv34 = 1;
  147. taghdrlen = 10;
  148. break;
  149. default:
  150. reason = "version";
  151. goto error;
  152. }
  153. if (flags & 0x80) {
  154. reason = "unsynchronization";
  155. goto error;
  156. }
  157. if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
  158. url_fskip(s->pb, get_size(s->pb, 4));
  159. while (len >= taghdrlen) {
  160. if (isv34) {
  161. tag = get_be32(s->pb);
  162. if(version==3){
  163. tlen = get_be32(s->pb);
  164. }else
  165. tlen = get_size(s->pb, 4);
  166. get_be16(s->pb); /* flags */
  167. } else {
  168. tag = get_be24(s->pb);
  169. tlen = get_be24(s->pb);
  170. }
  171. len -= taghdrlen + tlen;
  172. if (len < 0)
  173. break;
  174. next = url_ftell(s->pb) + tlen;
  175. switch (tag) {
  176. case MKBETAG('T', 'I', 'T', '2'):
  177. case MKBETAG(0, 'T', 'T', '2'):
  178. read_ttag(s, tlen, "title");
  179. break;
  180. case MKBETAG('T', 'P', 'E', '1'):
  181. case MKBETAG(0, 'T', 'P', '1'):
  182. read_ttag(s, tlen, "author");
  183. break;
  184. case MKBETAG('T', 'A', 'L', 'B'):
  185. case MKBETAG(0, 'T', 'A', 'L'):
  186. read_ttag(s, tlen, "album");
  187. break;
  188. case MKBETAG('T', 'C', 'O', 'N'):
  189. case MKBETAG(0, 'T', 'C', 'O'):
  190. read_ttag(s, tlen, "genre");
  191. break;
  192. case MKBETAG('T', 'C', 'O', 'P'):
  193. case MKBETAG(0, 'T', 'C', 'R'):
  194. read_ttag(s, tlen, "copyright");
  195. break;
  196. case MKBETAG('T', 'R', 'C', 'K'):
  197. case MKBETAG(0, 'T', 'R', 'K'):
  198. read_ttag(s, tlen, "track");
  199. break;
  200. case 0:
  201. /* padding, skip to end */
  202. url_fskip(s->pb, len);
  203. len = 0;
  204. continue;
  205. }
  206. /* Skip to end of tag */
  207. url_fseek(s->pb, next, SEEK_SET);
  208. }
  209. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  210. url_fskip(s->pb, 10);
  211. return;
  212. error:
  213. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  214. url_fskip(s->pb, len);
  215. }