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.

784 lines
22KB

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