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.

815 lines
23KB

  1. /*
  2. * Copyright (c) 2003 Fabrice Bellard
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * ID3v2 header parser
  23. *
  24. * Specifications available at:
  25. * http://id3.org/Developer_Information
  26. */
  27. #include "config.h"
  28. #if CONFIG_ZLIB
  29. #include <zlib.h>
  30. #endif
  31. #include "id3v2.h"
  32. #include "id3v1.h"
  33. #include "libavutil/avstring.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/dict.h"
  36. #include "avio_internal.h"
  37. #include "internal.h"
  38. const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
  39. { "TALB", "album"},
  40. { "TCOM", "composer"},
  41. { "TCON", "genre"},
  42. { "TCOP", "copyright"},
  43. { "TENC", "encoded_by"},
  44. { "TIT2", "title"},
  45. { "TLAN", "language"},
  46. { "TPE1", "artist"},
  47. { "TPE2", "album_artist"},
  48. { "TPE3", "performer"},
  49. { "TPOS", "disc"},
  50. { "TPUB", "publisher"},
  51. { "TRCK", "track"},
  52. { "TSSE", "encoder"},
  53. { 0 }
  54. };
  55. const AVMetadataConv ff_id3v2_4_metadata_conv[] = {
  56. { "TDRL", "date"},
  57. { "TDRC", "date"},
  58. { "TDEN", "creation_time"},
  59. { "TSOA", "album-sort"},
  60. { "TSOP", "artist-sort"},
  61. { "TSOT", "title-sort"},
  62. { 0 }
  63. };
  64. static const AVMetadataConv id3v2_2_metadata_conv[] = {
  65. { "TAL", "album"},
  66. { "TCO", "genre"},
  67. { "TT2", "title"},
  68. { "TEN", "encoded_by"},
  69. { "TP1", "artist"},
  70. { "TP2", "album_artist"},
  71. { "TP3", "performer"},
  72. { "TRK", "track"},
  73. { 0 }
  74. };
  75. const char ff_id3v2_tags[][4] = {
  76. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  77. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  78. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  79. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  80. { 0 },
  81. };
  82. const char ff_id3v2_4_tags[][4] = {
  83. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  84. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  85. { 0 },
  86. };
  87. const char ff_id3v2_3_tags[][4] = {
  88. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  89. { 0 },
  90. };
  91. const char *ff_id3v2_picture_types[21] = {
  92. "Other",
  93. "32x32 pixels 'file icon'",
  94. "Other file icon",
  95. "Cover (front)",
  96. "Cover (back)",
  97. "Leaflet page",
  98. "Media (e.g. label side of CD)",
  99. "Lead artist/lead performer/soloist",
  100. "Artist/performer",
  101. "Conductor",
  102. "Band/Orchestra",
  103. "Composer",
  104. "Lyricist/text writer",
  105. "Recording Location",
  106. "During recording",
  107. "During performance",
  108. "Movie/video screen capture",
  109. "A bright coloured fish",
  110. "Illustration",
  111. "Band/artist logotype",
  112. "Publisher/Studio logotype",
  113. };
  114. const CodecMime ff_id3v2_mime_tags[] = {
  115. {"image/gif" , CODEC_ID_GIF},
  116. {"image/jpeg", CODEC_ID_MJPEG},
  117. {"image/jpg", CODEC_ID_MJPEG},
  118. {"image/png" , CODEC_ID_PNG},
  119. {"image/tiff", CODEC_ID_TIFF},
  120. {"", CODEC_ID_NONE},
  121. };
  122. int ff_id3v2_match(const uint8_t *buf, const char * magic)
  123. {
  124. return buf[0] == magic[0] &&
  125. buf[1] == magic[1] &&
  126. buf[2] == magic[2] &&
  127. buf[3] != 0xff &&
  128. buf[4] != 0xff &&
  129. (buf[6] & 0x80) == 0 &&
  130. (buf[7] & 0x80) == 0 &&
  131. (buf[8] & 0x80) == 0 &&
  132. (buf[9] & 0x80) == 0;
  133. }
  134. int ff_id3v2_tag_len(const uint8_t * buf)
  135. {
  136. int len = ((buf[6] & 0x7f) << 21) +
  137. ((buf[7] & 0x7f) << 14) +
  138. ((buf[8] & 0x7f) << 7) +
  139. (buf[9] & 0x7f) +
  140. ID3v2_HEADER_SIZE;
  141. if (buf[5] & 0x10)
  142. len += ID3v2_HEADER_SIZE;
  143. return len;
  144. }
  145. static unsigned int get_size(AVIOContext *s, int len)
  146. {
  147. int v = 0;
  148. while (len--)
  149. v = (v << 7) + (avio_r8(s) & 0x7F);
  150. return v;
  151. }
  152. /**
  153. * Free GEOB type extra metadata.
  154. */
  155. static void free_geobtag(void *obj)
  156. {
  157. ID3v2ExtraMetaGEOB *geob = obj;
  158. av_free(geob->mime_type);
  159. av_free(geob->file_name);
  160. av_free(geob->description);
  161. av_free(geob->data);
  162. av_free(geob);
  163. }
  164. /**
  165. * Decode characters to UTF-8 according to encoding type. The decoded buffer is
  166. * always null terminated. Stop reading when either *maxread bytes are read from
  167. * pb or U+0000 character is found.
  168. *
  169. * @param dst Pointer where the address of the buffer with the decoded bytes is
  170. * stored. Buffer must be freed by caller.
  171. * @param maxread Pointer to maximum number of characters to read from the
  172. * AVIOContext. After execution the value is decremented by the number of bytes
  173. * actually read.
  174. * @returns 0 if no error occurred, dst is uninitialized on error
  175. */
  176. static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
  177. uint8_t **dst, int *maxread)
  178. {
  179. int ret;
  180. uint8_t tmp;
  181. uint32_t ch = 1;
  182. int left = *maxread;
  183. unsigned int (*get)(AVIOContext*) = avio_rb16;
  184. AVIOContext *dynbuf;
  185. if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
  186. av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
  187. return ret;
  188. }
  189. switch (encoding) {
  190. case ID3v2_ENCODING_ISO8859:
  191. while (left && ch) {
  192. ch = avio_r8(pb);
  193. PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
  194. left--;
  195. }
  196. break;
  197. case ID3v2_ENCODING_UTF16BOM:
  198. if ((left -= 2) < 0) {
  199. av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
  200. avio_close_dyn_buf(dynbuf, dst);
  201. av_freep(dst);
  202. return AVERROR_INVALIDDATA;
  203. }
  204. switch (avio_rb16(pb)) {
  205. case 0xfffe:
  206. get = avio_rl16;
  207. case 0xfeff:
  208. break;
  209. default:
  210. av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
  211. avio_close_dyn_buf(dynbuf, dst);
  212. av_freep(dst);
  213. *maxread = left;
  214. return AVERROR_INVALIDDATA;
  215. }
  216. // fall-through
  217. case ID3v2_ENCODING_UTF16BE:
  218. while ((left > 1) && ch) {
  219. GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
  220. PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
  221. }
  222. if (left < 0)
  223. left += 2; /* did not read last char from pb */
  224. break;
  225. case ID3v2_ENCODING_UTF8:
  226. while (left && ch) {
  227. ch = avio_r8(pb);
  228. avio_w8(dynbuf, ch);
  229. left--;
  230. }
  231. break;
  232. default:
  233. av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
  234. }
  235. if (ch)
  236. avio_w8(dynbuf, 0);
  237. avio_close_dyn_buf(dynbuf, dst);
  238. *maxread = left;
  239. return 0;
  240. }
  241. /**
  242. * Parse a text tag.
  243. */
  244. static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
  245. {
  246. uint8_t *dst;
  247. int encoding, dict_flags = AV_DICT_DONT_OVERWRITE;
  248. unsigned genre;
  249. if (taglen < 1)
  250. return;
  251. encoding = avio_r8(pb);
  252. taglen--; /* account for encoding type byte */
  253. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  254. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  255. return;
  256. }
  257. if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
  258. && (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1)
  259. && genre <= ID3v1_GENRE_MAX) {
  260. av_freep(&dst);
  261. dst = ff_id3v1_genre_str[genre];
  262. } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  263. /* dst now contains the key, need to get value */
  264. key = dst;
  265. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  266. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  267. av_freep(&key);
  268. return;
  269. }
  270. dict_flags |= AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_STRDUP_KEY;
  271. }
  272. else if (*dst)
  273. dict_flags |= AV_DICT_DONT_STRDUP_VAL;
  274. if (dst)
  275. av_dict_set(&s->metadata, key, dst, dict_flags);
  276. }
  277. /**
  278. * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
  279. */
  280. static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
  281. {
  282. ID3v2ExtraMetaGEOB *geob_data = NULL;
  283. ID3v2ExtraMeta *new_extra = NULL;
  284. char encoding;
  285. unsigned int len;
  286. if (taglen < 1)
  287. return;
  288. geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
  289. if (!geob_data) {
  290. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
  291. return;
  292. }
  293. new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
  294. if (!new_extra) {
  295. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
  296. goto fail;
  297. }
  298. /* read encoding type byte */
  299. encoding = avio_r8(pb);
  300. taglen--;
  301. /* read MIME type (always ISO-8859) */
  302. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, &taglen) < 0
  303. || taglen <= 0)
  304. goto fail;
  305. /* read file name */
  306. if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0
  307. || taglen <= 0)
  308. goto fail;
  309. /* read content description */
  310. if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0
  311. || taglen < 0)
  312. goto fail;
  313. if (taglen) {
  314. /* save encapsulated binary data */
  315. geob_data->data = av_malloc(taglen);
  316. if (!geob_data->data) {
  317. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
  318. goto fail;
  319. }
  320. if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
  321. av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
  322. geob_data->datasize = len;
  323. } else {
  324. geob_data->data = NULL;
  325. geob_data->datasize = 0;
  326. }
  327. /* add data to the list */
  328. new_extra->tag = "GEOB";
  329. new_extra->data = geob_data;
  330. new_extra->next = *extra_meta;
  331. *extra_meta = new_extra;
  332. return;
  333. fail:
  334. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
  335. free_geobtag(geob_data);
  336. av_free(new_extra);
  337. return;
  338. }
  339. static int is_number(const char *str)
  340. {
  341. while (*str >= '0' && *str <= '9') 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, " %.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_freep(&apic->data);
  382. av_freep(&apic->description);
  383. av_freep(&apic);
  384. }
  385. static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
  386. {
  387. int enc, pic_type;
  388. char mimetype[64];
  389. const CodecMime *mime = ff_id3v2_mime_tags;
  390. enum CodecID id = CODEC_ID_NONE;
  391. ID3v2ExtraMetaAPIC *apic = NULL;
  392. ID3v2ExtraMeta *new_extra = NULL;
  393. int64_t end = avio_tell(pb) + taglen;
  394. if (taglen <= 4)
  395. goto fail;
  396. new_extra = av_mallocz(sizeof(*new_extra));
  397. apic = av_mallocz(sizeof(*apic));
  398. if (!new_extra || !apic)
  399. goto fail;
  400. enc = avio_r8(pb);
  401. taglen--;
  402. /* mimetype */
  403. taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
  404. while (mime->id != CODEC_ID_NONE) {
  405. if (!strncmp(mime->str, mimetype, sizeof(mimetype))) {
  406. id = mime->id;
  407. break;
  408. }
  409. mime++;
  410. }
  411. if (id == CODEC_ID_NONE) {
  412. av_log(s, AV_LOG_WARNING, "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
  413. goto fail;
  414. }
  415. apic->id = id;
  416. /* picture type */
  417. pic_type = avio_r8(pb);
  418. taglen--;
  419. if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
  420. av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n", pic_type);
  421. pic_type = 0;
  422. }
  423. apic->type = ff_id3v2_picture_types[pic_type];
  424. /* description and picture data */
  425. if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
  426. av_log(s, AV_LOG_ERROR, "Error decoding attached picture description.\n");
  427. goto fail;
  428. }
  429. apic->len = taglen;
  430. apic->data = av_malloc(taglen);
  431. if (!apic->data || avio_read(pb, apic->data, taglen) != taglen)
  432. goto fail;
  433. new_extra->tag = "APIC";
  434. new_extra->data = apic;
  435. new_extra->next = *extra_meta;
  436. *extra_meta = new_extra;
  437. return;
  438. fail:
  439. if (apic)
  440. free_apic(apic);
  441. av_freep(&new_extra);
  442. avio_seek(pb, end, SEEK_SET);
  443. }
  444. typedef struct ID3v2EMFunc {
  445. const char *tag3;
  446. const char *tag4;
  447. void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
  448. void (*free)(void *obj);
  449. } ID3v2EMFunc;
  450. static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
  451. { "GEO", "GEOB", read_geobtag, free_geobtag },
  452. { "PIC", "APIC", read_apic, free_apic },
  453. { NULL }
  454. };
  455. /**
  456. * Get the corresponding ID3v2EMFunc struct for a tag.
  457. * @param isv34 Determines if v2.2 or v2.3/4 strings are used
  458. * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
  459. */
  460. static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
  461. {
  462. int i = 0;
  463. while (id3v2_extra_meta_funcs[i].tag3) {
  464. if (tag && !memcmp(tag,
  465. (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
  466. id3v2_extra_meta_funcs[i].tag3),
  467. (isv34 ? 4 : 3)))
  468. return &id3v2_extra_meta_funcs[i];
  469. i++;
  470. }
  471. return NULL;
  472. }
  473. static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
  474. {
  475. int isv34, unsync;
  476. unsigned tlen;
  477. char tag[5];
  478. int64_t next, end = avio_tell(s->pb) + len;
  479. int taghdrlen;
  480. const char *reason = NULL;
  481. AVIOContext pb;
  482. AVIOContext *pbx;
  483. unsigned char *buffer = NULL;
  484. int buffer_size = 0;
  485. const ID3v2EMFunc *extra_func = NULL;
  486. unsigned char *compressed_buffer = NULL;
  487. int compressed_buffer_size = 0;
  488. switch (version) {
  489. case 2:
  490. if (flags & 0x40) {
  491. reason = "compression";
  492. goto error;
  493. }
  494. isv34 = 0;
  495. taghdrlen = 6;
  496. break;
  497. case 3:
  498. case 4:
  499. isv34 = 1;
  500. taghdrlen = 10;
  501. break;
  502. default:
  503. reason = "version";
  504. goto error;
  505. }
  506. unsync = flags & 0x80;
  507. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  508. int extlen = get_size(s->pb, 4);
  509. if (version == 4)
  510. extlen -= 4; // in v2.4 the length includes the length field we just read
  511. if (extlen < 0) {
  512. reason = "invalid extended header length";
  513. goto error;
  514. }
  515. avio_skip(s->pb, extlen);
  516. len -= extlen + 4;
  517. if (len < 0) {
  518. reason = "extended header too long.";
  519. goto error;
  520. }
  521. }
  522. while (len >= taghdrlen) {
  523. unsigned int tflags = 0;
  524. int tunsync = 0;
  525. int tcomp = 0;
  526. int tencr = 0;
  527. unsigned long dlen;
  528. if (isv34) {
  529. avio_read(s->pb, tag, 4);
  530. tag[4] = 0;
  531. if(version==3){
  532. tlen = avio_rb32(s->pb);
  533. }else
  534. tlen = get_size(s->pb, 4);
  535. tflags = avio_rb16(s->pb);
  536. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  537. } else {
  538. avio_read(s->pb, tag, 3);
  539. tag[3] = 0;
  540. tlen = avio_rb24(s->pb);
  541. }
  542. if (tlen > (1<<28))
  543. break;
  544. len -= taghdrlen + tlen;
  545. if (len < 0)
  546. break;
  547. next = avio_tell(s->pb) + tlen;
  548. if (!tlen) {
  549. if (tag[0])
  550. av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n", tag);
  551. continue;
  552. }
  553. if (tflags & ID3v2_FLAG_DATALEN) {
  554. if (tlen < 4)
  555. break;
  556. dlen = avio_rb32(s->pb);
  557. tlen -= 4;
  558. } else
  559. dlen = tlen;
  560. tcomp = tflags & ID3v2_FLAG_COMPRESSION;
  561. tencr = tflags & ID3v2_FLAG_ENCRYPTION;
  562. /* skip encrypted tags and, if no zlib, compressed tags */
  563. if (tencr || (!CONFIG_ZLIB && tcomp)) {
  564. const char *type;
  565. if (!tcomp)
  566. type = "encrypted";
  567. else if (!tencr)
  568. type = "compressed";
  569. else
  570. type = "encrypted and compressed";
  571. av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
  572. avio_skip(s->pb, tlen);
  573. /* check for text tag or supported special meta tag */
  574. } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
  575. if (unsync || tunsync || tcomp) {
  576. int i, j;
  577. av_fast_malloc(&buffer, &buffer_size, dlen);
  578. if (!buffer) {
  579. av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
  580. goto seek;
  581. }
  582. #if CONFIG_ZLIB
  583. if (tcomp) {
  584. int n, err;
  585. av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
  586. av_fast_malloc(&compressed_buffer, &compressed_buffer_size, tlen);
  587. if (!compressed_buffer) {
  588. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  589. goto seek;
  590. }
  591. n = avio_read(s->pb, compressed_buffer, tlen);
  592. if (n < 0) {
  593. av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
  594. goto seek;
  595. }
  596. err = uncompress(buffer, &dlen, compressed_buffer, n);
  597. if (err != Z_OK) {
  598. av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
  599. goto seek;
  600. }
  601. }
  602. #endif
  603. for (i = 0, j = 0; i < dlen; i++, j++) {
  604. if (!tcomp)
  605. buffer[j] = avio_r8(s->pb);
  606. if (j > 0 && !buffer[j] && buffer[j - 1] == 0xff) {
  607. /* Unsynchronised byte, skip it */
  608. j--;
  609. }
  610. }
  611. ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
  612. tlen = j;
  613. pbx = &pb; // read from sync buffer
  614. } else {
  615. pbx = s->pb; // read straight from input
  616. }
  617. if (tag[0] == 'T')
  618. /* parse text tag */
  619. read_ttag(s, pbx, tlen, tag);
  620. else
  621. /* parse special meta tag */
  622. extra_func->read(s, pbx, tlen, tag, extra_meta);
  623. }
  624. else if (!tag[0]) {
  625. if (tag[1])
  626. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
  627. avio_skip(s->pb, tlen);
  628. break;
  629. }
  630. /* Skip to end of tag */
  631. seek:
  632. avio_seek(s->pb, next, SEEK_SET);
  633. }
  634. if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
  635. end += 10;
  636. error:
  637. if (reason)
  638. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
  639. avio_seek(s->pb, end, SEEK_SET);
  640. av_free(buffer);
  641. av_free(compressed_buffer);
  642. return;
  643. }
  644. void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
  645. {
  646. int len, ret;
  647. uint8_t buf[ID3v2_HEADER_SIZE];
  648. int found_header;
  649. int64_t off;
  650. do {
  651. /* save the current offset in case there's nothing to read/skip */
  652. off = avio_tell(s->pb);
  653. ret = avio_read(s->pb, buf, ID3v2_HEADER_SIZE);
  654. if (ret != ID3v2_HEADER_SIZE)
  655. break;
  656. found_header = ff_id3v2_match(buf, magic);
  657. if (found_header) {
  658. /* parse ID3v2 header */
  659. len = ((buf[6] & 0x7f) << 21) |
  660. ((buf[7] & 0x7f) << 14) |
  661. ((buf[8] & 0x7f) << 7) |
  662. (buf[9] & 0x7f);
  663. ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
  664. } else {
  665. avio_seek(s->pb, off, SEEK_SET);
  666. }
  667. } while (found_header);
  668. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_34_metadata_conv);
  669. ff_metadata_conv(&s->metadata, NULL, id3v2_2_metadata_conv);
  670. ff_metadata_conv(&s->metadata, NULL, ff_id3v2_4_metadata_conv);
  671. merge_date(&s->metadata);
  672. }
  673. void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
  674. {
  675. ID3v2ExtraMeta *current = *extra_meta, *next;
  676. const ID3v2EMFunc *extra_func;
  677. while (current) {
  678. if ((extra_func = get_extra_meta_func(current->tag, 1)))
  679. extra_func->free(current->data);
  680. next = current->next;
  681. av_freep(&current);
  682. current = next;
  683. }
  684. }
  685. int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
  686. {
  687. ID3v2ExtraMeta *cur;
  688. for (cur = *extra_meta; cur; cur = cur->next) {
  689. ID3v2ExtraMetaAPIC *apic;
  690. AVStream *st;
  691. if (strcmp(cur->tag, "APIC"))
  692. continue;
  693. apic = cur->data;
  694. if (!(st = avformat_new_stream(s, NULL)))
  695. return AVERROR(ENOMEM);
  696. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  697. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  698. st->codec->codec_id = apic->id;
  699. av_dict_set(&st->metadata, "title", apic->description, 0);
  700. av_dict_set(&st->metadata, "comment", apic->type, 0);
  701. av_init_packet(&st->attached_pic);
  702. st->attached_pic.data = apic->data;
  703. st->attached_pic.size = apic->len;
  704. st->attached_pic.destruct = av_destruct_packet;
  705. st->attached_pic.stream_index = st->index;
  706. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  707. apic->data = NULL;
  708. apic->len = 0;
  709. }
  710. return 0;
  711. }