id3v2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 "config.h"
  28. #if CONFIG_ZLIB
  29. #include <zlib.h>
  30. #endif
  31. #include "id3v2.h"
  32. #include "id3v1.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/dict.h"
  36. #include "avio_internal.h"
  37. const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
  38. { "TALB", "album"},
  39. { "TCOM", "composer"},
  40. { "TCON", "genre"},
  41. { "TCOP", "copyright"},
  42. { "TENC", "encoded_by"},
  43. { "TIT2", "title"},
  44. { "TLAN", "language"},
  45. { "TPE1", "artist"},
  46. { "TPE2", "album_artist"},
  47. { "TPE3", "performer"},
  48. { "TPOS", "disc"},
  49. { "TPUB", "publisher"},
  50. { "TRCK", "track"},
  51. { "TSSE", "encoder"},
  52. { 0 }
  53. };
  54. const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
  55. { "TDRL", "date"},
  56. { "TDRC", "date"},
  57. { "TDEN", "creation_time"},
  58. { "TSOA", "album-sort"},
  59. { "TSOP", "artist-sort"},
  60. { "TSOT", "title-sort"},
  61. { 0 }
  62. };
  63. static const AVMetadataConv id3v2_2_metadata_conv[] = {
  64. { "TAL", "album"},
  65. { "TCO", "genre"},
  66. { "TT2", "title"},
  67. { "TEN", "encoded_by"},
  68. { "TP1", "artist"},
  69. { "TP2", "album_artist"},
  70. { "TP3", "performer"},
  71. { "TRK", "track"},
  72. { 0 }
  73. };
  74. const char ff_id3v2_tags[][4] = {
  75. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  76. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  77. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  78. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  79. { 0 },
  80. };
  81. const char ff_id3v2_4_tags[][4] = {
  82. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  83. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  84. { 0 },
  85. };
  86. const char ff_id3v2_3_tags[][4] = {
  87. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  88. { 0 },
  89. };
  90. int ff_id3v2_match(const uint8_t *buf, const char * magic)
  91. {
  92. return buf[0] == magic[0] &&
  93. buf[1] == magic[1] &&
  94. buf[2] == magic[2] &&
  95. buf[3] != 0xff &&
  96. buf[4] != 0xff &&
  97. (buf[6] & 0x80) == 0 &&
  98. (buf[7] & 0x80) == 0 &&
  99. (buf[8] & 0x80) == 0 &&
  100. (buf[9] & 0x80) == 0;
  101. }
  102. int ff_id3v2_tag_len(const uint8_t * buf)
  103. {
  104. int len = ((buf[6] & 0x7f) << 21) +
  105. ((buf[7] & 0x7f) << 14) +
  106. ((buf[8] & 0x7f) << 7) +
  107. (buf[9] & 0x7f) +
  108. ID3v2_HEADER_SIZE;
  109. if (buf[5] & 0x10)
  110. len += ID3v2_HEADER_SIZE;
  111. return len;
  112. }
  113. static unsigned int get_size(AVIOContext *s, int len)
  114. {
  115. int v = 0;
  116. while (len--)
  117. v = (v << 7) + (avio_r8(s) & 0x7F);
  118. return v;
  119. }
  120. /**
  121. * Free GEOB type extra metadata.
  122. */
  123. static void free_geobtag(void *obj)
  124. {
  125. ID3v2ExtraMetaGEOB *geob = obj;
  126. av_free(geob->mime_type);
  127. av_free(geob->file_name);
  128. av_free(geob->description);
  129. av_free(geob->data);
  130. av_free(geob);
  131. }
  132. /**
  133. * Decode characters to UTF-8 according to encoding type. The decoded buffer is
  134. * always null terminated. Stop reading when either *maxread bytes are read from
  135. * pb or U+0000 character is found.
  136. *
  137. * @param dst Pointer where the address of the buffer with the decoded bytes is
  138. * stored. Buffer must be freed by caller.
  139. * @param maxread Pointer to maximum number of characters to read from the
  140. * AVIOContext. After execution the value is decremented by the number of bytes
  141. * actually read.
  142. * @returns 0 if no error occurred, dst is uninitialized on error
  143. */
  144. static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
  145. uint8_t **dst, int *maxread)
  146. {
  147. int ret;
  148. uint8_t tmp;
  149. uint32_t ch = 1;
  150. int left = *maxread;
  151. unsigned int (*get)(AVIOContext*) = avio_rb16;
  152. AVIOContext *dynbuf;
  153. if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
  154. av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
  155. return ret;
  156. }
  157. switch (encoding) {
  158. case ID3v2_ENCODING_ISO8859:
  159. while (left && ch) {
  160. ch = avio_r8(pb);
  161. PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
  162. left--;
  163. }
  164. break;
  165. case ID3v2_ENCODING_UTF16BOM:
  166. if ((left -= 2) < 0) {
  167. av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
  168. avio_close_dyn_buf(dynbuf, dst);
  169. av_freep(dst);
  170. return AVERROR_INVALIDDATA;
  171. }
  172. switch (avio_rb16(pb)) {
  173. case 0xfffe:
  174. get = avio_rl16;
  175. case 0xfeff:
  176. break;
  177. default:
  178. av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
  179. avio_close_dyn_buf(dynbuf, dst);
  180. av_freep(dst);
  181. *maxread = left;
  182. return AVERROR_INVALIDDATA;
  183. }
  184. // fall-through
  185. case ID3v2_ENCODING_UTF16BE:
  186. while ((left > 1) && ch) {
  187. GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
  188. PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
  189. }
  190. if (left < 0)
  191. left += 2; /* did not read last char from pb */
  192. break;
  193. case ID3v2_ENCODING_UTF8:
  194. while (left && ch) {
  195. ch = avio_r8(pb);
  196. avio_w8(dynbuf, ch);
  197. left--;
  198. }
  199. break;
  200. default:
  201. av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
  202. }
  203. if (ch)
  204. avio_w8(dynbuf, 0);
  205. avio_close_dyn_buf(dynbuf, dst);
  206. *maxread = left;
  207. return 0;
  208. }
  209. /**
  210. * Parse a text tag.
  211. */
  212. static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
  213. {
  214. uint8_t *dst;
  215. int encoding, dict_flags = AV_DICT_DONT_OVERWRITE;
  216. unsigned genre;
  217. if (taglen < 1)
  218. return;
  219. encoding = avio_r8(pb);
  220. taglen--; /* account for encoding type byte */
  221. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  222. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  223. return;
  224. }
  225. if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
  226. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  227. && genre <= ID3v1_GENRE_MAX) {
  228. av_freep(&dst);
  229. dst = ff_id3v1_genre_str[genre];
  230. } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  231. /* dst now contains the key, need to get value */
  232. key = dst;
  233. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  234. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  235. av_freep(&key);
  236. return;
  237. }
  238. dict_flags |= AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_STRDUP_KEY;
  239. }
  240. else if (*dst)
  241. dict_flags |= AV_DICT_DONT_STRDUP_VAL;
  242. if (dst)
  243. av_dict_set(&s->metadata, key, dst, dict_flags);
  244. }
  245. /**
  246. * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
  247. */
  248. static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
  249. {
  250. ID3v2ExtraMetaGEOB *geob_data = NULL;
  251. ID3v2ExtraMeta *new_extra = NULL;
  252. char encoding;
  253. unsigned int len;
  254. if (taglen < 1)
  255. return;
  256. geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
  257. if (!geob_data) {
  258. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
  259. return;
  260. }
  261. new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
  262. if (!new_extra) {
  263. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
  264. goto fail;
  265. }
  266. /* read encoding type byte */
  267. encoding = avio_r8(pb);
  268. taglen--;
  269. /* read MIME type (always ISO-8859) */
  270. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
  271. || taglen <= 0)
  272. goto fail;
  273. /* read file name */
  274. if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
  275. || taglen <= 0)
  276. goto fail;
  277. /* read content description */
  278. if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
  279. || taglen < 0)
  280. goto fail;
  281. if (taglen) {
  282. /* save encapsulated binary data */
  283. geob_data->data = av_malloc(taglen);
  284. if (!geob_data->data) {
  285. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
  286. goto fail;
  287. }
  288. if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
  289. av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
  290. geob_data->datasize = len;
  291. } else {
  292. geob_data->data = NULL;
  293. geob_data->datasize = 0;
  294. }
  295. /* add data to the list */
  296. new_extra->tag = "GEOB";
  297. new_extra->data = geob_data;
  298. new_extra->next = *extra_meta;
  299. *extra_meta = new_extra;
  300. return;
  301. fail:
  302. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
  303. free_geobtag(geob_data);
  304. av_free(new_extra);
  305. return;
  306. }
  307. static int is_number(const char *str)
  308. {
  309. while (*str >= '0' && *str <= '9') str++;
  310. return !*str;
  311. }
  312. static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
  313. {
  314. AVDictionaryEntry *t;
  315. if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
  316. strlen(t->value) == 4 && is_number(t->value))
  317. return t;
  318. return NULL;
  319. }
  320. static void merge_date(AVDictionary **m)
  321. {
  322. AVDictionaryEntry *t;
  323. char date[17] = {0}; // YYYY-MM-DD hh:mm
  324. if (!(t = get_date_tag(*m, "TYER")) &&
  325. !(t = get_date_tag(*m, "TYE")))
  326. return;
  327. av_strlcpy(date, t->value, 5);
  328. av_dict_set(m, "TYER", NULL, 0);
  329. av_dict_set(m, "TYE", NULL, 0);
  330. if (!(t = get_date_tag(*m, "TDAT")) &&
  331. !(t = get_date_tag(*m, "TDA")))
  332. goto finish;
  333. snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
  334. av_dict_set(m, "TDAT", NULL, 0);
  335. av_dict_set(m, "TDA", NULL, 0);
  336. if (!(t = get_date_tag(*m, "TIME")) &&
  337. !(t = get_date_tag(*m, "TIM")))
  338. goto finish;
  339. snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
  340. av_dict_set(m, "TIME", NULL, 0);
  341. av_dict_set(m, "TIM", NULL, 0);
  342. finish:
  343. if (date[0])
  344. av_dict_set(m, "date", date, 0);
  345. }
  346. typedef struct ID3v2EMFunc {
  347. const char *tag3;
  348. const char *tag4;
  349. void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
  350. void (*free)(void *obj);
  351. } ID3v2EMFunc;
  352. static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
  353. { "GEO", "GEOB", read_geobtag, free_geobtag },
  354. { NULL }
  355. };
  356. /**
  357. * Get the corresponding ID3v2EMFunc struct for a tag.
  358. * @param isv34 Determines if v2.2 or v2.3/4 strings are used
  359. * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
  360. */
  361. static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
  362. {
  363. int i = 0;
  364. while (id3v2_extra_meta_funcs[i].tag3) {
  365. if (tag && !memcmp(tag,
  366. (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
  367. id3v2_extra_meta_funcs[i].tag3),
  368. (isv34 ? 4 : 3)))
  369. return &id3v2_extra_meta_funcs[i];
  370. i++;
  371. }
  372. return NULL;
  373. }
  374. static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
  375. {
  376. int isv34, unsync;
  377. unsigned tlen;
  378. char tag[5];
  379. int64_t next, end = avio_tell(s->pb) + len;
  380. int taghdrlen;
  381. const char *reason = NULL;
  382. AVIOContext pb;
  383. AVIOContext *pbx;
  384. unsigned char *buffer = NULL;
  385. int buffer_size = 0;
  386. const ID3v2EMFunc *extra_func = NULL;
  387. unsigned char *compressed_buffer = NULL;
  388. int compressed_buffer_size = 0;
  389. switch (version) {
  390. case 2:
  391. if (flags & 0x40) {
  392. reason = "compression";
  393. goto error;
  394. }
  395. isv34 = 0;
  396. taghdrlen = 6;
  397. break;
  398. case 3:
  399. case 4:
  400. isv34 = 1;
  401. taghdrlen = 10;
  402. break;
  403. default:
  404. reason = "version";
  405. goto error;
  406. }
  407. unsync = flags & 0x80;
  408. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  409. int extlen = get_size(s->pb, 4);
  410. if (version == 4)
  411. extlen -= 4; // in v2.4 the length includes the length field we just read
  412. if (extlen < 0) {
  413. reason = "invalid extended header length";
  414. goto error;
  415. }
  416. avio_skip(s->pb, extlen);
  417. len -= extlen + 4;
  418. if (len < 0) {
  419. reason = "extended header too long.";
  420. goto error;
  421. }
  422. }
  423. while (len >= taghdrlen) {
  424. unsigned int tflags = 0;
  425. int tunsync = 0;
  426. int tcomp = 0;
  427. int tencr = 0;
  428. unsigned long dlen;
  429. if (isv34) {
  430. avio_read(s->pb, tag, 4);
  431. tag[4] = 0;
  432. if(version==3){
  433. tlen = avio_rb32(s->pb);
  434. }else
  435. tlen = get_size(s->pb, 4);
  436. tflags = avio_rb16(s->pb);
  437. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  438. } else {
  439. avio_read(s->pb, tag, 3);
  440. tag[3] = 0;
  441. tlen = avio_rb24(s->pb);
  442. }
  443. if (tlen > (1<<28))
  444. break;
  445. len -= taghdrlen + tlen;
  446. if (len < 0)
  447. break;
  448. next = avio_tell(s->pb) + tlen;
  449. if (!tlen) {
  450. if (tag[0])
  451. av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
  452. continue;
  453. }
  454. if (tflags & ID3v2_FLAG_DATALEN) {
  455. if (tlen < 4)
  456. break;
  457. dlen = avio_rb32(s->pb);
  458. tlen -= 4;
  459. } else
  460. dlen = tlen;
  461. tcomp = tflags & ID3v2_FLAG_COMPRESSION;
  462. tencr = tflags & ID3v2_FLAG_ENCRYPTION;
  463. /* skip encrypted tags and, if no zlib, compressed tags */
  464. if (tencr || (!CONFIG_ZLIB && tcomp)) {
  465. const char *type;
  466. if (!tcomp)
  467. type = "encrypted";
  468. else if (!tencr)
  469. type = "compressed";
  470. else
  471. type = "encrypted and compressed";
  472. av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
  473. avio_skip(s->pb, tlen);
  474. /* check for text tag or supported special meta tag */
  475. } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
  476. if (unsync || tunsync || tcomp) {
  477. int64_t end = avio_tell(s->pb) + tlen;
  478. uint8_t *b;
  479. av_fast_malloc(&buffer, &buffer_size, dlen);
  480. if (!buffer) {
  481. av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
  482. goto seek;
  483. }
  484. b = buffer;
  485. #if CONFIG_ZLIB
  486. if (tcomp) {
  487. int n, err;
  488. av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
  489. av_fast_malloc(&compressed_buffer, &compressed_buffer_size, tlen);
  490. if (!compressed_buffer) {
  491. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  492. goto seek;
  493. }
  494. n = avio_read(s->pb, compressed_buffer, tlen);
  495. if (n < 0) {
  496. av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
  497. goto seek;
  498. }
  499. err = uncompress(buffer, &dlen, compressed_buffer, n);
  500. if (err != Z_OK) {
  501. av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
  502. goto seek;
  503. }
  504. b += dlen;
  505. }
  506. #endif
  507. if (unsync || tunsync) {
  508. if (tcomp) {
  509. av_log_ask_for_sample(s, "tcomp with unsync\n");
  510. goto seek;
  511. }
  512. while (avio_tell(s->pb) < end && !s->pb->eof_reached) {
  513. *b++ = avio_r8(s->pb);
  514. if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1 &&
  515. !s->pb->eof_reached) {
  516. uint8_t val = avio_r8(s->pb);
  517. *b++ = val ? val : avio_r8(s->pb);
  518. }
  519. }
  520. }
  521. ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
  522. tlen = b - buffer;
  523. pbx = &pb; // read from sync buffer
  524. } else {
  525. pbx = s->pb; // read straight from input
  526. }
  527. if (tag[0] == 'T')
  528. /* parse text tag */
  529. read_ttag(s, pbx, tlen, tag);
  530. else
  531. /* parse special meta tag */
  532. extra_func->read(s, pbx, tlen, tag, extra_meta);
  533. }
  534. else if (!tag[0]) {
  535. if (tag[1])
  536. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  537. avio_skip(s->pb, tlen);
  538. break;
  539. }
  540. /* Skip to end of tag */
  541. seek:
  542. avio_seek(s->pb, next, SEEK_SET);
  543. }
  544. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  545. end += 10;
  546. error:
  547. if (reason)
  548. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  549. avio_seek(s->pb, end, SEEK_SET);
  550. av_free(buffer);
  551. av_free(compressed_buffer);
  552. return;
  553. }
  554. void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
  555. {
  556. int len, ret;
  557. uint8_t buf[ID3v2_HEADER_SIZE];
  558. int found_header;
  559. int64_t off;
  560. do {
  561. /* save the current offset in case there's nothing to read/skip */
  562. off = avio_tell(s->pb);
  563. ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
  564. if (ret != ID3v2_HEADER_SIZE)
  565. break;
  566. found_header = ff_id3v2_match(buf, magic);
  567. if (found_header) {
  568. /* parse ID3v2 header */
  569. len = ((buf[6] & 0x7f) << 21) |
  570. ((buf[7] & 0x7f) << 14) |
  571. ((buf[8] & 0x7f) << 7) |
  572. (buf[9] & 0x7f);
  573. ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
  574. } else {
  575. avio_seek(s->pb, off, SEEK_SET);
  576. }
  577. } while (found_header);
  578. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  579. ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
  580. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  581. merge_date(&s->metadata);
  582. }
  583. void ff_id3v2_read(AVFormatContext *s, const char *magic)
  584. {
  585. ff_id3v2_read_all(s, magic, NULL);
  586. }
  587. void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
  588. {
  589. ID3v2ExtraMeta *current = *extra_meta, *next;
  590. const ID3v2EMFunc *extra_func;
  591. while (current) {
  592. if ((extra_func = get_extra_meta_func(current->tag, 1)))
  593. extra_func->free(current->data);
  594. next = current->next;
  595. av_freep(&current);
  596. current = next;
  597. }
  598. }