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.

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