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.

855 lines
24KB

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