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.

973 lines
28KB

  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 "libavutil/avstring.h"
  32. #include "libavutil/dict.h"
  33. #include "libavutil/intreadwrite.h"
  34. #include "avio_internal.h"
  35. #include "internal.h"
  36. #include "id3v1.h"
  37. #include "id3v2.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,
  248. AVDictionary **metadata, const char *key)
  249. {
  250. uint8_t *dst;
  251. int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
  252. unsigned genre;
  253. if (taglen < 1)
  254. return;
  255. encoding = avio_r8(pb);
  256. taglen--; /* account for encoding type byte */
  257. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  258. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  259. return;
  260. }
  261. if (!(strcmp(key, "TCON") && strcmp(key, "TCO")) &&
  262. (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1) &&
  263. genre <= ID3v1_GENRE_MAX) {
  264. av_freep(&dst);
  265. dst = av_strdup(ff_id3v1_genre_str[genre]);
  266. } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  267. /* dst now contains the key, need to get value */
  268. key = dst;
  269. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  270. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  271. av_freep(&key);
  272. return;
  273. }
  274. dict_flags |= AV_DICT_DONT_STRDUP_KEY;
  275. } else if (!*dst)
  276. av_freep(&dst);
  277. if (dst)
  278. av_dict_set(metadata, key, dst, dict_flags);
  279. }
  280. /**
  281. * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
  282. */
  283. static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen,
  284. char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
  285. {
  286. ID3v2ExtraMetaGEOB *geob_data = NULL;
  287. ID3v2ExtraMeta *new_extra = NULL;
  288. char encoding;
  289. unsigned int len;
  290. if (taglen < 1)
  291. return;
  292. geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
  293. if (!geob_data) {
  294. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n",
  295. sizeof(ID3v2ExtraMetaGEOB));
  296. return;
  297. }
  298. new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
  299. if (!new_extra) {
  300. av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n",
  301. sizeof(ID3v2ExtraMeta));
  302. goto fail;
  303. }
  304. /* read encoding type byte */
  305. encoding = avio_r8(pb);
  306. taglen--;
  307. /* read MIME type (always ISO-8859) */
  308. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type,
  309. &taglen) < 0 ||
  310. taglen <= 0)
  311. goto fail;
  312. /* read file name */
  313. if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0 ||
  314. taglen <= 0)
  315. goto fail;
  316. /* read content description */
  317. if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0 ||
  318. taglen < 0)
  319. goto fail;
  320. if (taglen) {
  321. /* save encapsulated binary data */
  322. geob_data->data = av_malloc(taglen);
  323. if (!geob_data->data) {
  324. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
  325. goto fail;
  326. }
  327. if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
  328. av_log(s, AV_LOG_WARNING,
  329. "Error reading GEOB frame, data truncated.\n");
  330. geob_data->datasize = len;
  331. } else {
  332. geob_data->data = NULL;
  333. geob_data->datasize = 0;
  334. }
  335. /* add data to the list */
  336. new_extra->tag = "GEOB";
  337. new_extra->data = geob_data;
  338. new_extra->next = *extra_meta;
  339. *extra_meta = new_extra;
  340. return;
  341. fail:
  342. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
  343. free_geobtag(geob_data);
  344. av_free(new_extra);
  345. return;
  346. }
  347. static int is_number(const char *str)
  348. {
  349. while (*str >= '0' && *str <= '9')
  350. str++;
  351. return !*str;
  352. }
  353. static AVDictionaryEntry *get_date_tag(AVDictionary *m, const char *tag)
  354. {
  355. AVDictionaryEntry *t;
  356. if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
  357. strlen(t->value) == 4 && is_number(t->value))
  358. return t;
  359. return NULL;
  360. }
  361. static void merge_date(AVDictionary **m)
  362. {
  363. AVDictionaryEntry *t;
  364. char date[17] = { 0 }; // YYYY-MM-DD hh:mm
  365. if (!(t = get_date_tag(*m, "TYER")) &&
  366. !(t = get_date_tag(*m, "TYE")))
  367. return;
  368. av_strlcpy(date, t->value, 5);
  369. av_dict_set(m, "TYER", NULL, 0);
  370. av_dict_set(m, "TYE", NULL, 0);
  371. if (!(t = get_date_tag(*m, "TDAT")) &&
  372. !(t = get_date_tag(*m, "TDA")))
  373. goto finish;
  374. snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
  375. av_dict_set(m, "TDAT", NULL, 0);
  376. av_dict_set(m, "TDA", NULL, 0);
  377. if (!(t = get_date_tag(*m, "TIME")) &&
  378. !(t = get_date_tag(*m, "TIM")))
  379. goto finish;
  380. snprintf(date + 10, sizeof(date) - 10,
  381. " %.2s:%.2s", t->value, t->value + 2);
  382. av_dict_set(m, "TIME", NULL, 0);
  383. av_dict_set(m, "TIM", NULL, 0);
  384. finish:
  385. if (date[0])
  386. av_dict_set(m, "date", date, 0);
  387. }
  388. static void free_apic(void *obj)
  389. {
  390. ID3v2ExtraMetaAPIC *apic = obj;
  391. av_buffer_unref(&apic->buf);
  392. av_freep(&apic->description);
  393. av_freep(&apic);
  394. }
  395. static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen,
  396. char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
  397. {
  398. int enc, pic_type;
  399. char mimetype[64];
  400. const CodecMime *mime = ff_id3v2_mime_tags;
  401. enum AVCodecID id = AV_CODEC_ID_NONE;
  402. ID3v2ExtraMetaAPIC *apic = NULL;
  403. ID3v2ExtraMeta *new_extra = NULL;
  404. int64_t end = avio_tell(pb) + taglen;
  405. if (taglen <= 4)
  406. goto fail;
  407. new_extra = av_mallocz(sizeof(*new_extra));
  408. apic = av_mallocz(sizeof(*apic));
  409. if (!new_extra || !apic)
  410. goto fail;
  411. enc = avio_r8(pb);
  412. taglen--;
  413. /* mimetype */
  414. if (isv34) {
  415. taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
  416. } else {
  417. avio_read(pb, mimetype, 3);
  418. mimetype[3] = 0;
  419. }
  420. while (mime->id != AV_CODEC_ID_NONE) {
  421. if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
  422. id = mime->id;
  423. break;
  424. }
  425. mime++;
  426. }
  427. if (id == AV_CODEC_ID_NONE) {
  428. av_log(s, AV_LOG_WARNING,
  429. "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
  430. goto fail;
  431. }
  432. apic->id = id;
  433. /* picture type */
  434. pic_type = avio_r8(pb);
  435. taglen--;
  436. if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
  437. av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n",
  438. pic_type);
  439. pic_type = 0;
  440. }
  441. apic->type = ff_id3v2_picture_types[pic_type];
  442. /* description and picture data */
  443. if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
  444. av_log(s, AV_LOG_ERROR,
  445. "Error decoding attached picture description.\n");
  446. goto fail;
  447. }
  448. apic->buf = av_buffer_alloc(taglen + FF_INPUT_BUFFER_PADDING_SIZE);
  449. if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
  450. goto fail;
  451. memset(apic->buf->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  452. new_extra->tag = "APIC";
  453. new_extra->data = apic;
  454. new_extra->next = *extra_meta;
  455. *extra_meta = new_extra;
  456. return;
  457. fail:
  458. if (apic)
  459. free_apic(apic);
  460. av_freep(&new_extra);
  461. avio_seek(pb, end, SEEK_SET);
  462. }
  463. static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len, char *ttag, ID3v2ExtraMeta **extra_meta, int isv34)
  464. {
  465. AVRational time_base = {1, 1000};
  466. uint32_t start, end;
  467. AVChapter *chapter;
  468. uint8_t *dst = NULL;
  469. int taglen;
  470. char tag[5];
  471. if (!s) {
  472. /* We should probably just put the chapter data to extra_meta here
  473. * and do the AVFormatContext-needing part in a separate
  474. * ff_id3v2_parse_apic()-like function. */
  475. av_log(NULL, AV_LOG_DEBUG, "No AVFormatContext, skipped ID3 chapter data\n");
  476. return;
  477. }
  478. if (decode_str(s, pb, 0, &dst, &len) < 0)
  479. return;
  480. if (len < 16)
  481. return;
  482. start = avio_rb32(pb);
  483. end = avio_rb32(pb);
  484. avio_skip(pb, 8);
  485. chapter = avpriv_new_chapter(s, s->nb_chapters + 1, time_base, start, end, dst);
  486. if (!chapter) {
  487. av_free(dst);
  488. return;
  489. }
  490. len -= 16;
  491. while (len > 10) {
  492. if (avio_read(pb, tag, 4) < 4)
  493. goto end;
  494. tag[4] = 0;
  495. taglen = avio_rb32(pb);
  496. avio_skip(pb, 2);
  497. len -= 10;
  498. if (taglen < 0 || taglen > len)
  499. goto end;
  500. if (tag[0] == 'T')
  501. read_ttag(s, pb, taglen, &chapter->metadata, tag);
  502. else
  503. avio_skip(pb, taglen);
  504. len -= taglen;
  505. }
  506. ff_metadata_conv(&chapter->metadata, NULL, ff_id3v2_34_metadata_conv);
  507. ff_metadata_conv(&chapter->metadata, NULL, ff_id3v2_4_metadata_conv);
  508. end:
  509. av_free(dst);
  510. }
  511. static void free_priv(void *obj)
  512. {
  513. ID3v2ExtraMetaPRIV *priv = obj;
  514. av_freep(&priv->owner);
  515. av_freep(&priv->data);
  516. av_freep(&priv);
  517. }
  518. static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen,
  519. char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
  520. {
  521. ID3v2ExtraMeta *meta;
  522. ID3v2ExtraMetaPRIV *priv;
  523. meta = av_mallocz(sizeof(*meta));
  524. priv = av_mallocz(sizeof(*priv));
  525. if (!meta || !priv)
  526. goto fail;
  527. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &priv->owner, &taglen) < 0)
  528. goto fail;
  529. priv->data = av_malloc(taglen);
  530. if (!priv->data)
  531. goto fail;
  532. priv->datasize = taglen;
  533. if (avio_read(pb, priv->data, priv->datasize) != priv->datasize)
  534. goto fail;
  535. meta->tag = "PRIV";
  536. meta->data = priv;
  537. meta->next = *extra_meta;
  538. *extra_meta = meta;
  539. return;
  540. fail:
  541. if (priv)
  542. free_priv(priv);
  543. av_freep(&meta);
  544. }
  545. typedef struct ID3v2EMFunc {
  546. const char *tag3;
  547. const char *tag4;
  548. void (*read)(AVFormatContext *, AVIOContext *, int, char *,
  549. ID3v2ExtraMeta **, int isv34);
  550. void (*free)(void *obj);
  551. } ID3v2EMFunc;
  552. static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
  553. { "GEO", "GEOB", read_geobtag, free_geobtag },
  554. { "PIC", "APIC", read_apic, free_apic },
  555. { "CHAP","CHAP", read_chapter, NULL },
  556. { "PRIV","PRIV", read_priv, free_priv },
  557. { NULL }
  558. };
  559. /**
  560. * Get the corresponding ID3v2EMFunc struct for a tag.
  561. * @param isv34 Determines if v2.2 or v2.3/4 strings are used
  562. * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
  563. */
  564. static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
  565. {
  566. int i = 0;
  567. while (id3v2_extra_meta_funcs[i].tag3) {
  568. if (tag && !memcmp(tag,
  569. (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
  570. id3v2_extra_meta_funcs[i].tag3),
  571. (isv34 ? 4 : 3)))
  572. return &id3v2_extra_meta_funcs[i];
  573. i++;
  574. }
  575. return NULL;
  576. }
  577. static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata,
  578. AVFormatContext *s, int len, uint8_t version,
  579. uint8_t flags, ID3v2ExtraMeta **extra_meta)
  580. {
  581. int isv34, unsync;
  582. unsigned tlen;
  583. char tag[5];
  584. int64_t next, end = avio_tell(pb) + len;
  585. int taghdrlen;
  586. const char *reason = NULL;
  587. AVIOContext pb_local;
  588. AVIOContext *pbx;
  589. unsigned char *buffer = NULL;
  590. int buffer_size = 0;
  591. const ID3v2EMFunc *extra_func = NULL;
  592. unsigned char *uncompressed_buffer = NULL;
  593. int uncompressed_buffer_size = 0;
  594. av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
  595. switch (version) {
  596. case 2:
  597. if (flags & 0x40) {
  598. reason = "compression";
  599. goto error;
  600. }
  601. isv34 = 0;
  602. taghdrlen = 6;
  603. break;
  604. case 3:
  605. case 4:
  606. isv34 = 1;
  607. taghdrlen = 10;
  608. break;
  609. default:
  610. reason = "version";
  611. goto error;
  612. }
  613. unsync = flags & 0x80;
  614. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  615. int extlen = get_size(pb, 4);
  616. if (version == 4)
  617. /* In v2.4 the length includes the length field we just read. */
  618. extlen -= 4;
  619. if (extlen < 0) {
  620. reason = "invalid extended header length";
  621. goto error;
  622. }
  623. avio_skip(pb, extlen);
  624. len -= extlen + 4;
  625. if (len < 0) {
  626. reason = "extended header too long.";
  627. goto error;
  628. }
  629. }
  630. while (len >= taghdrlen) {
  631. unsigned int tflags = 0;
  632. int tunsync = 0;
  633. int tcomp = 0;
  634. int tencr = 0;
  635. unsigned long dlen;
  636. if (isv34) {
  637. if (avio_read(pb, tag, 4) < 4)
  638. break;
  639. tag[4] = 0;
  640. if (version == 3) {
  641. tlen = avio_rb32(pb);
  642. } else
  643. tlen = get_size(pb, 4);
  644. tflags = avio_rb16(pb);
  645. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  646. } else {
  647. if (avio_read(pb, tag, 3) < 3)
  648. break;
  649. tag[3] = 0;
  650. tlen = avio_rb24(pb);
  651. }
  652. if (tlen > (1<<28))
  653. break;
  654. len -= taghdrlen + tlen;
  655. if (len < 0)
  656. break;
  657. next = avio_tell(pb) + tlen;
  658. if (!tlen) {
  659. if (tag[0])
  660. av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
  661. tag);
  662. continue;
  663. }
  664. if (tflags & ID3v2_FLAG_DATALEN) {
  665. if (tlen < 4)
  666. break;
  667. dlen = avio_rb32(pb);
  668. tlen -= 4;
  669. } else
  670. dlen = tlen;
  671. tcomp = tflags & ID3v2_FLAG_COMPRESSION;
  672. tencr = tflags & ID3v2_FLAG_ENCRYPTION;
  673. /* skip encrypted tags and, if no zlib, compressed tags */
  674. if (tencr || (!CONFIG_ZLIB && tcomp)) {
  675. const char *type;
  676. if (!tcomp)
  677. type = "encrypted";
  678. else if (!tencr)
  679. type = "compressed";
  680. else
  681. type = "encrypted and compressed";
  682. av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
  683. avio_skip(pb, tlen);
  684. /* check for text tag or supported special meta tag */
  685. } else if (tag[0] == 'T' ||
  686. (extra_meta &&
  687. (extra_func = get_extra_meta_func(tag, isv34)))) {
  688. pbx = pb;
  689. if (unsync || tunsync || tcomp) {
  690. av_fast_malloc(&buffer, &buffer_size, tlen);
  691. if (!buffer) {
  692. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  693. goto seek;
  694. }
  695. }
  696. if (unsync || tunsync) {
  697. int64_t end = avio_tell(pb) + tlen;
  698. uint8_t *b;
  699. b = buffer;
  700. while (avio_tell(pb) < end && b - buffer < tlen && !pb->eof_reached) {
  701. *b++ = avio_r8(pb);
  702. if (*(b - 1) == 0xff && avio_tell(pb) < end - 1 &&
  703. b - buffer < tlen &&
  704. !pb->eof_reached ) {
  705. uint8_t val = avio_r8(pb);
  706. *b++ = val ? val : avio_r8(pb);
  707. }
  708. }
  709. ffio_init_context(&pb_local, buffer, b - buffer, 0, NULL, NULL, NULL,
  710. NULL);
  711. tlen = b - buffer;
  712. pbx = &pb_local; // read from sync buffer
  713. }
  714. #if CONFIG_ZLIB
  715. if (tcomp) {
  716. int err;
  717. av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
  718. av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
  719. if (!uncompressed_buffer) {
  720. av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
  721. goto seek;
  722. }
  723. if (!(unsync || tunsync)) {
  724. err = avio_read(pb, buffer, tlen);
  725. if (err < 0) {
  726. av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
  727. goto seek;
  728. }
  729. tlen = err;
  730. }
  731. err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
  732. if (err != Z_OK) {
  733. av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
  734. goto seek;
  735. }
  736. ffio_init_context(&pb_local, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
  737. tlen = dlen;
  738. pbx = &pb_local; // read from sync buffer
  739. }
  740. #endif
  741. if (tag[0] == 'T')
  742. /* parse text tag */
  743. read_ttag(s, pbx, tlen, metadata, tag);
  744. else
  745. /* parse special meta tag */
  746. extra_func->read(s, pbx, tlen, tag, extra_meta, isv34);
  747. } else if (!tag[0]) {
  748. if (tag[1])
  749. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
  750. avio_skip(pb, tlen);
  751. break;
  752. }
  753. /* Skip to end of tag */
  754. seek:
  755. avio_seek(pb, next, SEEK_SET);
  756. }
  757. /* Footer preset, always 10 bytes, skip over it */
  758. if (version == 4 && flags & 0x10)
  759. end += 10;
  760. error:
  761. if (reason)
  762. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
  763. version, reason);
  764. avio_seek(pb, end, SEEK_SET);
  765. av_free(buffer);
  766. av_free(uncompressed_buffer);
  767. return;
  768. }
  769. static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata,
  770. AVFormatContext *s, const char *magic,
  771. ID3v2ExtraMeta **extra_meta)
  772. {
  773. int len, ret;
  774. uint8_t buf[ID3v2_HEADER_SIZE];
  775. int found_header;
  776. int64_t off;
  777. do {
  778. /* save the current offset in case there's nothing to read/skip */
  779. off = avio_tell(pb);
  780. ret = avio_read(pb, buf, ID3v2_HEADER_SIZE);
  781. if (ret != ID3v2_HEADER_SIZE) {
  782. avio_seek(pb, off, SEEK_SET);
  783. break;
  784. }
  785. found_header = ff_id3v2_match(buf, magic);
  786. if (found_header) {
  787. /* parse ID3v2 header */
  788. len = ((buf[6] & 0x7f) << 21) |
  789. ((buf[7] & 0x7f) << 14) |
  790. ((buf[8] & 0x7f) << 7) |
  791. (buf[9] & 0x7f);
  792. id3v2_parse(pb, metadata, s, len, buf[3], buf[5], extra_meta);
  793. } else {
  794. avio_seek(pb, off, SEEK_SET);
  795. }
  796. } while (found_header);
  797. ff_metadata_conv(metadata, NULL, ff_id3v2_34_metadata_conv);
  798. ff_metadata_conv(metadata, NULL, id3v2_2_metadata_conv);
  799. ff_metadata_conv(metadata, NULL, ff_id3v2_4_metadata_conv);
  800. merge_date(metadata);
  801. }
  802. void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata,
  803. const char *magic, ID3v2ExtraMeta **extra_meta)
  804. {
  805. id3v2_read_internal(pb, metadata, NULL, magic, extra_meta);
  806. }
  807. void ff_id3v2_read(AVFormatContext *s, const char *magic,
  808. ID3v2ExtraMeta **extra_meta)
  809. {
  810. id3v2_read_internal(s->pb, &s->metadata, s, magic, extra_meta);
  811. }
  812. void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
  813. {
  814. ID3v2ExtraMeta *current = *extra_meta, *next;
  815. const ID3v2EMFunc *extra_func;
  816. while (current) {
  817. if ((extra_func = get_extra_meta_func(current->tag, 1)))
  818. extra_func->free(current->data);
  819. next = current->next;
  820. av_freep(&current);
  821. current = next;
  822. }
  823. }
  824. int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
  825. {
  826. ID3v2ExtraMeta *cur;
  827. for (cur = *extra_meta; cur; cur = cur->next) {
  828. ID3v2ExtraMetaAPIC *apic;
  829. AVStream *st;
  830. if (strcmp(cur->tag, "APIC"))
  831. continue;
  832. apic = cur->data;
  833. if (!(st = avformat_new_stream(s, NULL)))
  834. return AVERROR(ENOMEM);
  835. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  836. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  837. st->codec->codec_id = apic->id;
  838. av_dict_set(&st->metadata, "title", apic->description, 0);
  839. av_dict_set(&st->metadata, "comment", apic->type, 0);
  840. av_init_packet(&st->attached_pic);
  841. st->attached_pic.buf = apic->buf;
  842. st->attached_pic.data = apic->buf->data;
  843. st->attached_pic.size = apic->buf->size - FF_INPUT_BUFFER_PADDING_SIZE;
  844. st->attached_pic.stream_index = st->index;
  845. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  846. apic->buf = NULL;
  847. }
  848. return 0;
  849. }