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.

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