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.

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