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.

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