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.

194 lines
5.1KB

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