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.

746 lines
21KB

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