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.

217 lines
5.7KB

  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. dst[0] = 0;
  78. if (taglen < 1)
  79. return;
  80. taglen--; /* account for encoding type byte */
  81. switch (get_byte(s->pb)) { /* encoding type */
  82. case 0: /* ISO-8859-1 (0 - 255 maps directly into unicode) */
  83. q = dst;
  84. while (taglen--) {
  85. uint8_t tmp;
  86. PUT_UTF8(get_byte(s->pb), tmp, if (q - dst < dstlen - 1) *q++ = tmp;)
  87. }
  88. *q = '\0';
  89. break;
  90. case 3: /* UTF-8 */
  91. len = FFMIN(taglen, dstlen - 1);
  92. get_buffer(s->pb, dst, len);
  93. dst[len] = 0;
  94. break;
  95. }
  96. if (!strcmp(key, "genre")
  97. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  98. && genre <= ID3v1_GENRE_MAX)
  99. av_strlcpy(dst, ff_id3v1_genre_str[genre], sizeof(dst));
  100. if (*dst)
  101. av_metadata_set(&s->metadata, key, dst);
  102. }
  103. void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
  104. {
  105. int isv34, tlen;
  106. uint32_t tag;
  107. int64_t next;
  108. int taghdrlen;
  109. const char *reason;
  110. switch (version) {
  111. case 2:
  112. if (flags & 0x40) {
  113. reason = "compression";
  114. goto error;
  115. }
  116. isv34 = 0;
  117. taghdrlen = 6;
  118. break;
  119. case 3:
  120. case 4:
  121. isv34 = 1;
  122. taghdrlen = 10;
  123. break;
  124. default:
  125. reason = "version";
  126. goto error;
  127. }
  128. if (flags & 0x80) {
  129. reason = "unsynchronization";
  130. goto error;
  131. }
  132. if (isv34 && flags & 0x40) /* Extended header present, just skip over it */
  133. url_fskip(s->pb, get_size(s->pb, 4));
  134. while (len >= taghdrlen) {
  135. if (isv34) {
  136. tag = get_be32(s->pb);
  137. if(version==3){
  138. tlen = get_be32(s->pb);
  139. }else
  140. tlen = get_size(s->pb, 4);
  141. get_be16(s->pb); /* flags */
  142. } else {
  143. tag = get_be24(s->pb);
  144. tlen = get_be24(s->pb);
  145. }
  146. len -= taghdrlen + tlen;
  147. if (len < 0)
  148. break;
  149. next = url_ftell(s->pb) + tlen;
  150. switch (tag) {
  151. case MKBETAG('T', 'I', 'T', '2'):
  152. case MKBETAG(0, 'T', 'T', '2'):
  153. read_ttag(s, tlen, "title");
  154. break;
  155. case MKBETAG('T', 'P', 'E', '1'):
  156. case MKBETAG(0, 'T', 'P', '1'):
  157. read_ttag(s, tlen, "author");
  158. break;
  159. case MKBETAG('T', 'A', 'L', 'B'):
  160. case MKBETAG(0, 'T', 'A', 'L'):
  161. read_ttag(s, tlen, "album");
  162. break;
  163. case MKBETAG('T', 'C', 'O', 'N'):
  164. case MKBETAG(0, 'T', 'C', 'O'):
  165. read_ttag(s, tlen, "genre");
  166. break;
  167. case MKBETAG('T', 'C', 'O', 'P'):
  168. case MKBETAG(0, 'T', 'C', 'R'):
  169. read_ttag(s, tlen, "copyright");
  170. break;
  171. case MKBETAG('T', 'R', 'C', 'K'):
  172. case MKBETAG(0, 'T', 'R', 'K'):
  173. read_ttag(s, tlen, "track");
  174. break;
  175. case 0:
  176. /* padding, skip to end */
  177. url_fskip(s->pb, len);
  178. len = 0;
  179. continue;
  180. }
  181. /* Skip to end of tag */
  182. url_fseek(s->pb, next, SEEK_SET);
  183. }
  184. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  185. url_fskip(s->pb, 10);
  186. return;
  187. error:
  188. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  189. url_fskip(s->pb, len);
  190. }