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.

749 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 | AV_DICT_DONT_STRDUP_VAL;
  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 = av_strdup(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_KEY;
  264. } else if (!*dst)
  265. av_freep(&dst);
  266. if (dst)
  267. av_dict_set(&s->metadata, key, dst, dict_flags);
  268. }
  269. /**
  270. * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
  271. */
  272. static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
  273. {
  274. ID3v2ExtraMetaGEOB *geob_data = NULL;
  275. ID3v2ExtraMeta *new_extra = NULL;
  276. char encoding;
  277. unsigned int len;
  278. if (taglen < 1)
  279. return;
  280. geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
  281. if (!geob_data) {
  282. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
  283. return;
  284. }
  285. new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
  286. if (!new_extra) {
  287. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
  288. goto fail;
  289. }
  290. /* read encoding type byte */
  291. encoding = avio_r8(pb);
  292. taglen--;
  293. /* read MIME type (always ISO-8859) */
  294. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
  295. || taglen <= 0)
  296. goto fail;
  297. /* read file name */
  298. if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
  299. || taglen <= 0)
  300. goto fail;
  301. /* read content description */
  302. if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
  303. || taglen < 0)
  304. goto fail;
  305. if (taglen) {
  306. /* save encapsulated binary data */
  307. geob_data->data = av_malloc(taglen);
  308. if (!geob_data->data) {
  309. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
  310. goto fail;
  311. }
  312. if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
  313. av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
  314. geob_data->datasize = len;
  315. } else {
  316. geob_data->data = NULL;
  317. geob_data->datasize = 0;
  318. }
  319. /* add data to the list */
  320. new_extra->tag = "GEOB";
  321. new_extra->data = geob_data;
  322. new_extra->next = *extra_meta;
  323. *extra_meta = new_extra;
  324. return;
  325. fail:
  326. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
  327. free_geobtag(geob_data);
  328. av_free(new_extra);
  329. return;
  330. }
  331. static int is_number(const char *str)
  332. {
  333. while (*str >= '0' && *str <= '9') str++;
  334. return !*str;
  335. }
  336. static AVDictionaryEntry* get_date_tag(AVDictionary *m, const char *tag)
  337. {
  338. AVDictionaryEntry *t;
  339. if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
  340. strlen(t->value) == 4 && is_number(t->value))
  341. return t;
  342. return NULL;
  343. }
  344. static void merge_date(AVDictionary **m)
  345. {
  346. AVDictionaryEntry *t;
  347. char date[17] = {0}; // YYYY-MM-DD hh:mm
  348. if (!(t = get_date_tag(*m, "TYER")) &&
  349. !(t = get_date_tag(*m, "TYE")))
  350. return;
  351. av_strlcpy(date, t->value, 5);
  352. av_dict_set(m, "TYER", NULL, 0);
  353. av_dict_set(m, "TYE", NULL, 0);
  354. if (!(t = get_date_tag(*m, "TDAT")) &&
  355. !(t = get_date_tag(*m, "TDA")))
  356. goto finish;
  357. snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
  358. av_dict_set(m, "TDAT", NULL, 0);
  359. av_dict_set(m, "TDA", NULL, 0);
  360. if (!(t = get_date_tag(*m, "TIME")) &&
  361. !(t = get_date_tag(*m, "TIM")))
  362. goto finish;
  363. snprintf(date + 10, sizeof(date) - 10, " %.2s:%.2s", t->value, t->value + 2);
  364. av_dict_set(m, "TIME", NULL, 0);
  365. av_dict_set(m, "TIM", NULL, 0);
  366. finish:
  367. if (date[0])
  368. av_dict_set(m, "date", date, 0);
  369. }
  370. static void free_apic(void *obj)
  371. {
  372. ID3v2ExtraMetaAPIC *apic = obj;
  373. av_freep(&apic->data);
  374. av_freep(&apic->description);
  375. av_freep(&apic);
  376. }
  377. static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
  378. {
  379. int enc, pic_type;
  380. char mimetype[64];
  381. const CodecMime *mime = ff_id3v2_mime_tags;
  382. enum AVCodecID id = AV_CODEC_ID_NONE;
  383. ID3v2ExtraMetaAPIC *apic = NULL;
  384. ID3v2ExtraMeta *new_extra = NULL;
  385. int64_t end = avio_tell(pb) + taglen;
  386. if (taglen <= 4)
  387. goto fail;
  388. new_extra = av_mallocz(sizeof(*new_extra));
  389. apic = av_mallocz(sizeof(*apic));
  390. if (!new_extra || !apic)
  391. goto fail;
  392. enc = avio_r8(pb);
  393. taglen--;
  394. /* mimetype */
  395. taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
  396. while (mime->id != AV_CODEC_ID_NONE) {
  397. if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
  398. id = mime->id;
  399. break;
  400. }
  401. mime++;
  402. }
  403. if (id == AV_CODEC_ID_NONE) {
  404. av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
  405. goto fail;
  406. }
  407. apic->id = id;
  408. /* picture type */
  409. pic_type = avio_r8(pb);
  410. taglen--;
  411. if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
  412. av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n", pic_type);
  413. pic_type = 0;
  414. }
  415. apic->type = ff_id3v2_picture_types[pic_type];
  416. /* description and picture data */
  417. if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
  418. av_log(s, AV_LOG_ERROR, "Error decoding attached picture description.\n");
  419. goto fail;
  420. }
  421. apic->len = taglen;
  422. apic->data = av_malloc(taglen);
  423. if (!apic->data || avio_read(pb, apic->data, taglen) != taglen)
  424. goto fail;
  425. new_extra->tag = "APIC";
  426. new_extra->data = apic;
  427. new_extra->next = *extra_meta;
  428. *extra_meta = new_extra;
  429. return;
  430. fail:
  431. if (apic)
  432. free_apic(apic);
  433. av_freep(&new_extra);
  434. avio_seek(pb, end, SEEK_SET);
  435. }
  436. typedef struct ID3v2EMFunc {
  437. const char *tag3;
  438. const char *tag4;
  439. void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
  440. void (*free)(void *obj);
  441. } ID3v2EMFunc;
  442. static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
  443. { "GEO", "GEOB", read_geobtag, free_geobtag },
  444. { "PIC", "APIC", read_apic, free_apic },
  445. { NULL }
  446. };
  447. /**
  448. * Get the corresponding ID3v2EMFunc struct for a tag.
  449. * @param isv34 Determines if v2.2 or v2.3/4 strings are used
  450. * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
  451. */
  452. static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
  453. {
  454. int i = 0;
  455. while (id3v2_extra_meta_funcs[i].tag3) {
  456. if (!memcmp(tag,
  457. (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
  458. id3v2_extra_meta_funcs[i].tag3),
  459. (isv34 ? 4 : 3)))
  460. return &id3v2_extra_meta_funcs[i];
  461. i++;
  462. }
  463. return NULL;
  464. }
  465. static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
  466. {
  467. int isv34, tlen, unsync;
  468. char tag[5];
  469. int64_t next, end = avio_tell(s->pb) + len;
  470. int taghdrlen;
  471. const char *reason = NULL;
  472. AVIOContext pb;
  473. AVIOContext *pbx;
  474. unsigned char *buffer = NULL;
  475. int buffer_size = 0;
  476. const ID3v2EMFunc *extra_func;
  477. switch (version) {
  478. case 2:
  479. if (flags & 0x40) {
  480. reason = "compression";
  481. goto error;
  482. }
  483. isv34 = 0;
  484. taghdrlen = 6;
  485. break;
  486. case 3:
  487. case 4:
  488. isv34 = 1;
  489. taghdrlen = 10;
  490. break;
  491. default:
  492. reason = "version";
  493. goto error;
  494. }
  495. unsync = flags & 0x80;
  496. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  497. int extlen = get_size(s->pb, 4);
  498. if (version == 4)
  499. extlen -= 4; // in v2.4 the length includes the length field we just read
  500. if (extlen < 0) {
  501. reason = "invalid extended header length";
  502. goto error;
  503. }
  504. avio_skip(s->pb, extlen);
  505. }
  506. while (len >= taghdrlen) {
  507. unsigned int tflags = 0;
  508. int tunsync = 0;
  509. if (isv34) {
  510. avio_read(s->pb, tag, 4);
  511. tag[4] = 0;
  512. if(version==3){
  513. tlen = avio_rb32(s->pb);
  514. }else
  515. tlen = get_size(s->pb, 4);
  516. tflags = avio_rb16(s->pb);
  517. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  518. } else {
  519. avio_read(s->pb, tag, 3);
  520. tag[3] = 0;
  521. tlen = avio_rb24(s->pb);
  522. }
  523. if (tlen < 0 || tlen > len - taghdrlen) {
  524. av_log(s, AV_LOG_WARNING, "Invalid size in frame %s, skipping the rest of tag.\n", tag);
  525. break;
  526. }
  527. len -= taghdrlen + tlen;
  528. next = avio_tell(s->pb) + tlen;
  529. if (!tlen) {
  530. if (tag[0])
  531. av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
  532. continue;
  533. }
  534. if (tflags & ID3v2_FLAG_DATALEN) {
  535. avio_rb32(s->pb);
  536. tlen -= 4;
  537. }
  538. if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
  539. av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
  540. avio_skip(s->pb, tlen);
  541. /* check for text tag or supported special meta tag */
  542. } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
  543. if (unsync || tunsync) {
  544. int64_t end = avio_tell(s->pb) + tlen;
  545. uint8_t *b;
  546. av_fast_malloc(&buffer, &buffer_size, tlen);
  547. if (!buffer) {
  548. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  549. goto seek;
  550. }
  551. b = buffer;
  552. while (avio_tell(s->pb) < end) {
  553. *b++ = avio_r8(s->pb);
  554. if (*(b - 1) == 0xff && avio_tell(s->pb) < end - 1) {
  555. uint8_t val = avio_r8(s->pb);
  556. *b++ = val ? val : avio_r8(s->pb);
  557. }
  558. }
  559. ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
  560. tlen = b - buffer;
  561. pbx = &pb; // read from sync buffer
  562. } else {
  563. pbx = s->pb; // read straight from input
  564. }
  565. if (tag[0] == 'T')
  566. /* parse text tag */
  567. read_ttag(s, pbx, tlen, tag);
  568. else
  569. /* parse special meta tag */
  570. extra_func->read(s, pbx, tlen, tag, extra_meta);
  571. }
  572. else if (!tag[0]) {
  573. if (tag[1])
  574. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  575. avio_skip(s->pb, tlen);
  576. break;
  577. }
  578. /* Skip to end of tag */
  579. seek:
  580. avio_seek(s->pb, next, SEEK_SET);
  581. }
  582. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  583. end += 10;
  584. error:
  585. if (reason)
  586. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  587. avio_seek(s->pb, end, SEEK_SET);
  588. av_free(buffer);
  589. return;
  590. }
  591. void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
  592. {
  593. int len, ret;
  594. uint8_t buf[ID3v2_HEADER_SIZE];
  595. int found_header;
  596. int64_t off;
  597. do {
  598. /* save the current offset in case there's nothing to read/skip */
  599. off = avio_tell(s->pb);
  600. ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
  601. if (ret != ID3v2_HEADER_SIZE)
  602. break;
  603. found_header = ff_id3v2_match(buf, magic);
  604. if (found_header) {
  605. /* parse ID3v2 header */
  606. len = ((buf[6] & 0x7f) << 21) |
  607. ((buf[7] & 0x7f) << 14) |
  608. ((buf[8] & 0x7f) << 7) |
  609. (buf[9] & 0x7f);
  610. ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
  611. } else {
  612. avio_seek(s->pb, off, SEEK_SET);
  613. }
  614. } while (found_header);
  615. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  616. ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
  617. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  618. merge_date(&s->metadata);
  619. }
  620. void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
  621. {
  622. ID3v2ExtraMeta *current = *extra_meta, *next;
  623. const ID3v2EMFunc *extra_func;
  624. while (current) {
  625. if ((extra_func = get_extra_meta_func(current->tag, 1)))
  626. extra_func->free(current->data);
  627. next = current->next;
  628. av_freep(&current);
  629. current = next;
  630. }
  631. }
  632. int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
  633. {
  634. ID3v2ExtraMeta *cur;
  635. for (cur = *extra_meta; cur; cur = cur->next) {
  636. ID3v2ExtraMetaAPIC *apic;
  637. AVStream *st;
  638. if (strcmp(cur->tag, "APIC"))
  639. continue;
  640. apic = cur->data;
  641. if (!(st = avformat_new_stream(s, NULL)))
  642. return AVERROR(ENOMEM);
  643. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  644. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  645. st->codec->codec_id = apic->id;
  646. av_dict_set(&st->metadata, "title", apic->description, 0);
  647. av_dict_set(&st->metadata, "comment", apic->type, 0);
  648. av_init_packet(&st->attached_pic);
  649. st->attached_pic.data = apic->data;
  650. st->attached_pic.size = apic->len;
  651. st->attached_pic.destruct = av_destruct_packet;
  652. st->attached_pic.stream_index = st->index;
  653. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  654. apic->data = NULL;
  655. apic->len = 0;
  656. }
  657. return 0;
  658. }