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.

986 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. { "TCMP", "compilation" },
  57. { "TDRL", "date" },
  58. { "TDRC", "date" },
  59. { "TDEN", "creation_time" },
  60. { "TSOA", "album-sort" },
  61. { "TSOP", "artist-sort" },
  62. { "TSOT", "title-sort" },
  63. { 0 }
  64. };
  65. static const AVMetadataConv id3v2_2_metadata_conv[] = {
  66. { "TAL", "album" },
  67. { "TCO", "genre" },
  68. { "TCP", "compilation" },
  69. { "TT2", "title" },
  70. { "TEN", "encoded_by" },
  71. { "TP1", "artist" },
  72. { "TP2", "album_artist" },
  73. { "TP3", "performer" },
  74. { "TRK", "track" },
  75. { 0 }
  76. };
  77. const char ff_id3v2_tags[][4] = {
  78. "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDLY", "TENC", "TEXT",
  79. "TFLT", "TIT1", "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED",
  80. "TOAL", "TOFN", "TOLY", "TOPE", "TOWN", "TPE1", "TPE2", "TPE3",
  81. "TPE4", "TPOS", "TPUB", "TRCK", "TRSN", "TRSO", "TSRC", "TSSE",
  82. { 0 },
  83. };
  84. const char ff_id3v2_4_tags[][4] = {
  85. "TDEN", "TDOR", "TDRC", "TDRL", "TDTG", "TIPL", "TMCL", "TMOO",
  86. "TPRO", "TSOA", "TSOP", "TSOT", "TSST",
  87. { 0 },
  88. };
  89. const char ff_id3v2_3_tags[][4] = {
  90. "TDAT", "TIME", "TORY", "TRDA", "TSIZ", "TYER",
  91. { 0 },
  92. };
  93. const char *ff_id3v2_picture_types[21] = {
  94. "Other",
  95. "32x32 pixels 'file icon'",
  96. "Other file icon",
  97. "Cover (front)",
  98. "Cover (back)",
  99. "Leaflet page",
  100. "Media (e.g. label side of CD)",
  101. "Lead artist/lead performer/soloist",
  102. "Artist/performer",
  103. "Conductor",
  104. "Band/Orchestra",
  105. "Composer",
  106. "Lyricist/text writer",
  107. "Recording Location",
  108. "During recording",
  109. "During performance",
  110. "Movie/video screen capture",
  111. "A bright coloured fish",
  112. "Illustration",
  113. "Band/artist logotype",
  114. "Publisher/Studio logotype",
  115. };
  116. const CodecMime ff_id3v2_mime_tags[] = {
  117. { "image/gif", AV_CODEC_ID_GIF },
  118. { "image/jpeg", AV_CODEC_ID_MJPEG },
  119. { "image/jpg", AV_CODEC_ID_MJPEG },
  120. { "image/png", AV_CODEC_ID_PNG },
  121. { "image/tiff", AV_CODEC_ID_TIFF },
  122. { "image/bmp", AV_CODEC_ID_BMP },
  123. { "JPG", AV_CODEC_ID_MJPEG }, /* ID3v2.2 */
  124. { "PNG", AV_CODEC_ID_PNG }, /* ID3v2.2 */
  125. { "", AV_CODEC_ID_NONE },
  126. };
  127. int ff_id3v2_match(const uint8_t *buf, const char *magic)
  128. {
  129. return buf[0] == magic[0] &&
  130. buf[1] == magic[1] &&
  131. buf[2] == magic[2] &&
  132. buf[3] != 0xff &&
  133. buf[4] != 0xff &&
  134. (buf[6] & 0x80) == 0 &&
  135. (buf[7] & 0x80) == 0 &&
  136. (buf[8] & 0x80) == 0 &&
  137. (buf[9] & 0x80) == 0;
  138. }
  139. int ff_id3v2_tag_len(const uint8_t *buf)
  140. {
  141. int len = ((buf[6] & 0x7f) << 21) +
  142. ((buf[7] & 0x7f) << 14) +
  143. ((buf[8] & 0x7f) << 7) +
  144. (buf[9] & 0x7f) +
  145. ID3v2_HEADER_SIZE;
  146. if (buf[5] & 0x10)
  147. len += ID3v2_HEADER_SIZE;
  148. return len;
  149. }
  150. static unsigned int get_size(AVIOContext *s, int len)
  151. {
  152. int v = 0;
  153. while (len--)
  154. v = (v << 7) + (avio_r8(s) & 0x7F);
  155. return v;
  156. }
  157. /**
  158. * Free GEOB type extra metadata.
  159. */
  160. static void free_geobtag(void *obj)
  161. {
  162. ID3v2ExtraMetaGEOB *geob = obj;
  163. av_free(geob->mime_type);
  164. av_free(geob->file_name);
  165. av_free(geob->description);
  166. av_free(geob->data);
  167. av_free(geob);
  168. }
  169. /**
  170. * Decode characters to UTF-8 according to encoding type. The decoded buffer is
  171. * always null terminated. Stop reading when either *maxread bytes are read from
  172. * pb or U+0000 character is found.
  173. *
  174. * @param dst Pointer where the address of the buffer with the decoded bytes is
  175. * stored. Buffer must be freed by caller.
  176. * @param maxread Pointer to maximum number of characters to read from the
  177. * AVIOContext. After execution the value is decremented by the number of bytes
  178. * actually read.
  179. * @returns 0 if no error occurred, dst is uninitialized on error
  180. */
  181. static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
  182. uint8_t **dst, int *maxread)
  183. {
  184. int ret;
  185. uint8_t tmp;
  186. uint32_t ch = 1;
  187. int left = *maxread;
  188. unsigned int (*get)(AVIOContext*) = avio_rb16;
  189. AVIOContext *dynbuf;
  190. if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
  191. av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
  192. return ret;
  193. }
  194. switch (encoding) {
  195. case ID3v2_ENCODING_ISO8859:
  196. while (left && ch) {
  197. ch = avio_r8(pb);
  198. PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
  199. left--;
  200. }
  201. break;
  202. case ID3v2_ENCODING_UTF16BOM:
  203. if ((left -= 2) < 0) {
  204. av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
  205. avio_close_dyn_buf(dynbuf, dst);
  206. av_freep(dst);
  207. return AVERROR_INVALIDDATA;
  208. }
  209. switch (avio_rb16(pb)) {
  210. case 0xfffe:
  211. get = avio_rl16;
  212. case 0xfeff:
  213. break;
  214. default:
  215. av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
  216. avio_close_dyn_buf(dynbuf, dst);
  217. av_freep(dst);
  218. *maxread = left;
  219. return AVERROR_INVALIDDATA;
  220. }
  221. // fall-through
  222. case ID3v2_ENCODING_UTF16BE:
  223. while ((left > 1) && ch) {
  224. GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
  225. PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
  226. }
  227. if (left < 0)
  228. left += 2; /* did not read last char from pb */
  229. break;
  230. case ID3v2_ENCODING_UTF8:
  231. while (left && ch) {
  232. ch = avio_r8(pb);
  233. avio_w8(dynbuf, ch);
  234. left--;
  235. }
  236. break;
  237. default:
  238. av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
  239. }
  240. if (ch)
  241. avio_w8(dynbuf, 0);
  242. avio_close_dyn_buf(dynbuf, dst);
  243. *maxread = left;
  244. return 0;
  245. }
  246. /**
  247. * Parse a text tag.
  248. */
  249. static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen,
  250. AVDictionary **metadata, const char *key)
  251. {
  252. uint8_t *dst;
  253. int encoding, dict_flags = AV_DICT_DONT_OVERWRITE | AV_DICT_DONT_STRDUP_VAL;
  254. unsigned genre;
  255. if (taglen < 1)
  256. return;
  257. encoding = avio_r8(pb);
  258. taglen--; /* account for encoding type byte */
  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. return;
  262. }
  263. if (!(strcmp(key, "TCON") && strcmp(key, "TCO")) &&
  264. (sscanf(dst, "(%d)", &genre) == 1 || sscanf(dst, "%d", &genre) == 1) &&
  265. genre <= ID3v1_GENRE_MAX) {
  266. av_freep(&dst);
  267. dst = av_strdup(ff_id3v1_genre_str[genre]);
  268. } else if (!(strcmp(key, "TXXX") && strcmp(key, "TXX"))) {
  269. /* dst now contains the key, need to get value */
  270. key = dst;
  271. if (decode_str(s, pb, encoding, &dst, &taglen) < 0) {
  272. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
  273. av_freep(&key);
  274. return;
  275. }
  276. dict_flags |= AV_DICT_DONT_STRDUP_KEY;
  277. } else if (!*dst)
  278. av_freep(&dst);
  279. if (dst)
  280. av_dict_set(metadata, key, dst, dict_flags);
  281. }
  282. /**
  283. * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
  284. */
  285. static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen,
  286. char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
  287. {
  288. ID3v2ExtraMetaGEOB *geob_data = NULL;
  289. ID3v2ExtraMeta *new_extra = NULL;
  290. char encoding;
  291. unsigned int len;
  292. if (taglen < 1)
  293. return;
  294. geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
  295. if (!geob_data) {
  296. av_log(s, AV_LOG_ERROR, "Failed to alloc %"SIZE_SPECIFIER" bytes\n",
  297. sizeof(ID3v2ExtraMetaGEOB));
  298. return;
  299. }
  300. new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
  301. if (!new_extra) {
  302. av_log(s, AV_LOG_ERROR, "Failed to alloc %"SIZE_SPECIFIER" bytes\n",
  303. sizeof(ID3v2ExtraMeta));
  304. goto fail;
  305. }
  306. /* read encoding type byte */
  307. encoding = avio_r8(pb);
  308. taglen--;
  309. /* read MIME type (always ISO-8859) */
  310. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type,
  311. &taglen) < 0 ||
  312. taglen <= 0)
  313. goto fail;
  314. /* read file name */
  315. if (decode_str(s, pb, encoding, &geob_data->file_name, &taglen) < 0 ||
  316. taglen <= 0)
  317. goto fail;
  318. /* read content description */
  319. if (decode_str(s, pb, encoding, &geob_data->description, &taglen) < 0 ||
  320. taglen < 0)
  321. goto fail;
  322. if (taglen) {
  323. /* save encapsulated binary data */
  324. geob_data->data = av_malloc(taglen);
  325. if (!geob_data->data) {
  326. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
  327. goto fail;
  328. }
  329. if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
  330. av_log(s, AV_LOG_WARNING,
  331. "Error reading GEOB frame, data truncated.\n");
  332. geob_data->datasize = len;
  333. } else {
  334. geob_data->data = NULL;
  335. geob_data->datasize = 0;
  336. }
  337. /* add data to the list */
  338. new_extra->tag = "GEOB";
  339. new_extra->data = geob_data;
  340. new_extra->next = *extra_meta;
  341. *extra_meta = new_extra;
  342. return;
  343. fail:
  344. av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
  345. free_geobtag(geob_data);
  346. av_free(new_extra);
  347. return;
  348. }
  349. static int is_number(const char *str)
  350. {
  351. while (*str >= '0' && *str <= '9')
  352. str++;
  353. return !*str;
  354. }
  355. static AVDictionaryEntry *get_date_tag(AVDictionary *m, const char *tag)
  356. {
  357. AVDictionaryEntry *t;
  358. if ((t = av_dict_get(m, tag, NULL, AV_DICT_MATCH_CASE)) &&
  359. strlen(t->value) == 4 && is_number(t->value))
  360. return t;
  361. return NULL;
  362. }
  363. static void merge_date(AVDictionary **m)
  364. {
  365. AVDictionaryEntry *t;
  366. char date[17] = { 0 }; // YYYY-MM-DD hh:mm
  367. if (!(t = get_date_tag(*m, "TYER")) &&
  368. !(t = get_date_tag(*m, "TYE")))
  369. return;
  370. av_strlcpy(date, t->value, 5);
  371. av_dict_set(m, "TYER", NULL, 0);
  372. av_dict_set(m, "TYE", NULL, 0);
  373. if (!(t = get_date_tag(*m, "TDAT")) &&
  374. !(t = get_date_tag(*m, "TDA")))
  375. goto finish;
  376. snprintf(date + 4, sizeof(date) - 4, "-%.2s-%.2s", t->value + 2, t->value);
  377. av_dict_set(m, "TDAT", NULL, 0);
  378. av_dict_set(m, "TDA", NULL, 0);
  379. if (!(t = get_date_tag(*m, "TIME")) &&
  380. !(t = get_date_tag(*m, "TIM")))
  381. goto finish;
  382. snprintf(date + 10, sizeof(date) - 10,
  383. " %.2s:%.2s", t->value, t->value + 2);
  384. av_dict_set(m, "TIME", NULL, 0);
  385. av_dict_set(m, "TIM", NULL, 0);
  386. finish:
  387. if (date[0])
  388. av_dict_set(m, "date", date, 0);
  389. }
  390. static void free_apic(void *obj)
  391. {
  392. ID3v2ExtraMetaAPIC *apic = obj;
  393. av_buffer_unref(&apic->buf);
  394. av_freep(&apic->description);
  395. av_freep(&apic);
  396. }
  397. static void read_apic(AVFormatContext *s, AVIOContext *pb, int taglen,
  398. char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
  399. {
  400. int enc, pic_type;
  401. char mimetype[64];
  402. const CodecMime *mime = ff_id3v2_mime_tags;
  403. enum AVCodecID id = AV_CODEC_ID_NONE;
  404. ID3v2ExtraMetaAPIC *apic = NULL;
  405. ID3v2ExtraMeta *new_extra = NULL;
  406. int64_t end = avio_tell(pb) + taglen;
  407. if (taglen <= 4)
  408. goto fail;
  409. new_extra = av_mallocz(sizeof(*new_extra));
  410. apic = av_mallocz(sizeof(*apic));
  411. if (!new_extra || !apic)
  412. goto fail;
  413. enc = avio_r8(pb);
  414. taglen--;
  415. /* mimetype */
  416. if (isv34) {
  417. taglen -= avio_get_str(pb, taglen, mimetype, sizeof(mimetype));
  418. } else {
  419. avio_read(pb, mimetype, 3);
  420. mimetype[3] = 0;
  421. }
  422. while (mime->id != AV_CODEC_ID_NONE) {
  423. if (!av_strncasecmp(mime->str, mimetype, sizeof(mimetype))) {
  424. id = mime->id;
  425. break;
  426. }
  427. mime++;
  428. }
  429. if (id == AV_CODEC_ID_NONE) {
  430. av_log(s, AV_LOG_WARNING,
  431. "Unknown attached picture mimetype: %s, skipping.\n", mimetype);
  432. goto fail;
  433. }
  434. apic->id = id;
  435. /* picture type */
  436. pic_type = avio_r8(pb);
  437. taglen--;
  438. if (pic_type < 0 || pic_type >= FF_ARRAY_ELEMS(ff_id3v2_picture_types)) {
  439. av_log(s, AV_LOG_WARNING, "Unknown attached picture type %d.\n",
  440. pic_type);
  441. pic_type = 0;
  442. }
  443. apic->type = ff_id3v2_picture_types[pic_type];
  444. /* description and picture data */
  445. if (decode_str(s, pb, enc, &apic->description, &taglen) < 0) {
  446. av_log(s, AV_LOG_ERROR,
  447. "Error decoding attached picture description.\n");
  448. goto fail;
  449. }
  450. apic->buf = av_buffer_alloc(taglen + FF_INPUT_BUFFER_PADDING_SIZE);
  451. if (!apic->buf || !taglen || avio_read(pb, apic->buf->data, taglen) != taglen)
  452. goto fail;
  453. memset(apic->buf->data + taglen, 0, FF_INPUT_BUFFER_PADDING_SIZE);
  454. new_extra->tag = "APIC";
  455. new_extra->data = apic;
  456. new_extra->next = *extra_meta;
  457. *extra_meta = new_extra;
  458. return;
  459. fail:
  460. if (apic)
  461. free_apic(apic);
  462. av_freep(&new_extra);
  463. avio_seek(pb, end, SEEK_SET);
  464. }
  465. static void read_chapter(AVFormatContext *s, AVIOContext *pb, int len, char *ttag, ID3v2ExtraMeta **extra_meta, int isv34)
  466. {
  467. AVRational time_base = {1, 1000};
  468. uint32_t start, end;
  469. AVChapter *chapter;
  470. uint8_t *dst = NULL;
  471. int taglen;
  472. char tag[5];
  473. if (!s) {
  474. /* We should probably just put the chapter data to extra_meta here
  475. * and do the AVFormatContext-needing part in a separate
  476. * ff_id3v2_parse_apic()-like function. */
  477. av_log(NULL, AV_LOG_DEBUG, "No AVFormatContext, skipped ID3 chapter data\n");
  478. return;
  479. }
  480. if (decode_str(s, pb, 0, &dst, &len) < 0)
  481. return;
  482. if (len < 16)
  483. return;
  484. start = avio_rb32(pb);
  485. end = avio_rb32(pb);
  486. avio_skip(pb, 8);
  487. chapter = avpriv_new_chapter(s, s->nb_chapters + 1, time_base, start, end, dst);
  488. if (!chapter) {
  489. av_free(dst);
  490. return;
  491. }
  492. len -= 16;
  493. while (len > 10) {
  494. if (avio_read(pb, tag, 4) < 4)
  495. goto end;
  496. tag[4] = 0;
  497. taglen = avio_rb32(pb);
  498. avio_skip(pb, 2);
  499. len -= 10;
  500. if (taglen < 0 || taglen > len)
  501. goto end;
  502. if (tag[0] == 'T')
  503. read_ttag(s, pb, taglen, &chapter->metadata, tag);
  504. else
  505. avio_skip(pb, taglen);
  506. len -= taglen;
  507. }
  508. ff_metadata_conv(&chapter->metadata, NULL, ff_id3v2_34_metadata_conv);
  509. ff_metadata_conv(&chapter->metadata, NULL, ff_id3v2_4_metadata_conv);
  510. end:
  511. av_free(dst);
  512. }
  513. static void free_priv(void *obj)
  514. {
  515. ID3v2ExtraMetaPRIV *priv = obj;
  516. av_freep(&priv->owner);
  517. av_freep(&priv->data);
  518. av_freep(&priv);
  519. }
  520. static void read_priv(AVFormatContext *s, AVIOContext *pb, int taglen,
  521. char *tag, ID3v2ExtraMeta **extra_meta, int isv34)
  522. {
  523. ID3v2ExtraMeta *meta;
  524. ID3v2ExtraMetaPRIV *priv;
  525. meta = av_mallocz(sizeof(*meta));
  526. priv = av_mallocz(sizeof(*priv));
  527. if (!meta || !priv)
  528. goto fail;
  529. if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &priv->owner, &taglen) < 0)
  530. goto fail;
  531. priv->data = av_malloc(taglen);
  532. if (!priv->data)
  533. goto fail;
  534. priv->datasize = taglen;
  535. if (avio_read(pb, priv->data, priv->datasize) != priv->datasize)
  536. goto fail;
  537. meta->tag = "PRIV";
  538. meta->data = priv;
  539. meta->next = *extra_meta;
  540. *extra_meta = meta;
  541. return;
  542. fail:
  543. if (priv)
  544. free_priv(priv);
  545. av_freep(&meta);
  546. }
  547. typedef struct ID3v2EMFunc {
  548. const char *tag3;
  549. const char *tag4;
  550. void (*read)(AVFormatContext *, AVIOContext *, int, char *,
  551. ID3v2ExtraMeta **, int isv34);
  552. void (*free)(void *obj);
  553. } ID3v2EMFunc;
  554. static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
  555. { "GEO", "GEOB", read_geobtag, free_geobtag },
  556. { "PIC", "APIC", read_apic, free_apic },
  557. { "CHAP","CHAP", read_chapter, NULL },
  558. { "PRIV","PRIV", read_priv, free_priv },
  559. { NULL }
  560. };
  561. /**
  562. * Get the corresponding ID3v2EMFunc struct for a tag.
  563. * @param isv34 Determines if v2.2 or v2.3/4 strings are used
  564. * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
  565. */
  566. static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
  567. {
  568. int i = 0;
  569. while (id3v2_extra_meta_funcs[i].tag3) {
  570. if (tag && !memcmp(tag,
  571. (isv34 ? id3v2_extra_meta_funcs[i].tag4 :
  572. id3v2_extra_meta_funcs[i].tag3),
  573. (isv34 ? 4 : 3)))
  574. return &id3v2_extra_meta_funcs[i];
  575. i++;
  576. }
  577. return NULL;
  578. }
  579. static void id3v2_parse(AVIOContext *pb, AVDictionary **metadata,
  580. AVFormatContext *s, int len, uint8_t version,
  581. uint8_t flags, ID3v2ExtraMeta **extra_meta)
  582. {
  583. int isv34, unsync;
  584. unsigned tlen;
  585. char tag[5];
  586. int64_t next, end = avio_tell(pb) + len;
  587. int taghdrlen;
  588. const char *reason = NULL;
  589. AVIOContext pb_local;
  590. AVIOContext *pbx;
  591. unsigned char *buffer = NULL;
  592. int buffer_size = 0;
  593. const ID3v2EMFunc *extra_func = NULL;
  594. unsigned char *uncompressed_buffer = NULL;
  595. int uncompressed_buffer_size = 0;
  596. av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
  597. switch (version) {
  598. case 2:
  599. if (flags & 0x40) {
  600. reason = "compression";
  601. goto error;
  602. }
  603. isv34 = 0;
  604. taghdrlen = 6;
  605. break;
  606. case 3:
  607. case 4:
  608. isv34 = 1;
  609. taghdrlen = 10;
  610. break;
  611. default:
  612. reason = "version";
  613. goto error;
  614. }
  615. unsync = flags & 0x80;
  616. if (isv34 && flags & 0x40) { /* Extended header present, just skip over it */
  617. int extlen = get_size(pb, 4);
  618. if (version == 4)
  619. /* In v2.4 the length includes the length field we just read. */
  620. extlen -= 4;
  621. if (extlen < 0) {
  622. reason = "invalid extended header length";
  623. goto error;
  624. }
  625. avio_skip(pb, extlen);
  626. len -= extlen + 4;
  627. if (len < 0) {
  628. reason = "extended header too long.";
  629. goto error;
  630. }
  631. }
  632. while (len >= taghdrlen) {
  633. unsigned int tflags = 0;
  634. int tunsync = 0;
  635. int tcomp = 0;
  636. int tencr = 0;
  637. unsigned long dlen;
  638. if (isv34) {
  639. if (avio_read(pb, tag, 4) < 4)
  640. break;
  641. tag[4] = 0;
  642. if (version == 3) {
  643. tlen = avio_rb32(pb);
  644. } else
  645. tlen = get_size(pb, 4);
  646. tflags = avio_rb16(pb);
  647. tunsync = tflags & ID3v2_FLAG_UNSYNCH;
  648. } else {
  649. if (avio_read(pb, tag, 3) < 3)
  650. break;
  651. tag[3] = 0;
  652. tlen = avio_rb24(pb);
  653. }
  654. if (tlen > (1<<28))
  655. break;
  656. len -= taghdrlen + tlen;
  657. if (len < 0)
  658. break;
  659. next = avio_tell(pb) + tlen;
  660. if (!tlen) {
  661. if (tag[0])
  662. av_log(s, AV_LOG_DEBUG, "Invalid empty frame %s, skipping.\n",
  663. tag);
  664. continue;
  665. }
  666. if (tflags & ID3v2_FLAG_DATALEN) {
  667. if (tlen < 4)
  668. break;
  669. dlen = avio_rb32(pb);
  670. tlen -= 4;
  671. } else
  672. dlen = tlen;
  673. tcomp = tflags & ID3v2_FLAG_COMPRESSION;
  674. tencr = tflags & ID3v2_FLAG_ENCRYPTION;
  675. /* skip encrypted tags and, if no zlib, compressed tags */
  676. if (tencr || (!CONFIG_ZLIB && tcomp)) {
  677. const char *type;
  678. if (!tcomp)
  679. type = "encrypted";
  680. else if (!tencr)
  681. type = "compressed";
  682. else
  683. type = "encrypted and compressed";
  684. av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
  685. avio_skip(pb, tlen);
  686. /* check for text tag or supported special meta tag */
  687. } else if (tag[0] == 'T' ||
  688. (extra_meta &&
  689. (extra_func = get_extra_meta_func(tag, isv34)))) {
  690. pbx = pb;
  691. if (unsync || tunsync || tcomp) {
  692. av_fast_malloc(&buffer, &buffer_size, tlen);
  693. if (!buffer) {
  694. av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
  695. goto seek;
  696. }
  697. }
  698. if (unsync || tunsync) {
  699. int64_t end = avio_tell(pb) + tlen;
  700. uint8_t *b;
  701. b = buffer;
  702. while (avio_tell(pb) < end && b - buffer < tlen && !pb->eof_reached) {
  703. *b++ = avio_r8(pb);
  704. if (*(b - 1) == 0xff && avio_tell(pb) < end - 1 &&
  705. b - buffer < tlen &&
  706. !pb->eof_reached ) {
  707. uint8_t val = avio_r8(pb);
  708. *b++ = val ? val : avio_r8(pb);
  709. }
  710. }
  711. ffio_init_context(&pb_local, buffer, b - buffer, 0, NULL, NULL, NULL,
  712. NULL);
  713. tlen = b - buffer;
  714. pbx = &pb_local; // read from sync buffer
  715. }
  716. #if CONFIG_ZLIB
  717. if (tcomp) {
  718. int err;
  719. av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
  720. av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
  721. if (!uncompressed_buffer) {
  722. av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
  723. goto seek;
  724. }
  725. if (!(unsync || tunsync)) {
  726. err = avio_read(pb, buffer, tlen);
  727. if (err < 0) {
  728. av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
  729. goto seek;
  730. }
  731. tlen = err;
  732. }
  733. err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
  734. if (err != Z_OK) {
  735. av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
  736. goto seek;
  737. }
  738. ffio_init_context(&pb_local, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
  739. tlen = dlen;
  740. pbx = &pb_local; // read from sync buffer
  741. }
  742. #endif
  743. if (tag[0] == 'T')
  744. /* parse text tag */
  745. read_ttag(s, pbx, tlen, metadata, tag);
  746. else
  747. /* parse special meta tag */
  748. extra_func->read(s, pbx, tlen, tag, extra_meta, isv34);
  749. } else if (!tag[0]) {
  750. if (tag[1])
  751. av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
  752. avio_skip(pb, tlen);
  753. break;
  754. }
  755. /* Skip to end of tag */
  756. seek:
  757. avio_seek(pb, next, SEEK_SET);
  758. }
  759. /* Footer preset, always 10 bytes, skip over it */
  760. if (version == 4 && flags & 0x10)
  761. end += 10;
  762. error:
  763. if (reason)
  764. av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
  765. version, reason);
  766. avio_seek(pb, end, SEEK_SET);
  767. av_free(buffer);
  768. av_free(uncompressed_buffer);
  769. return;
  770. }
  771. static void id3v2_read_internal(AVIOContext *pb, AVDictionary **metadata,
  772. AVFormatContext *s, const char *magic,
  773. ID3v2ExtraMeta **extra_meta, int64_t max_search_size)
  774. {
  775. int len, ret;
  776. uint8_t buf[ID3v2_HEADER_SIZE];
  777. int found_header;
  778. int64_t start, off;
  779. if (max_search_size && max_search_size < ID3v2_HEADER_SIZE)
  780. return;
  781. start = avio_tell(pb);
  782. do {
  783. /* save the current offset in case there's nothing to read/skip */
  784. off = avio_tell(pb);
  785. if (max_search_size && off - start >= max_search_size - ID3v2_HEADER_SIZE) {
  786. avio_seek(pb, off, SEEK_SET);
  787. break;
  788. }
  789. ret = avio_read(pb, buf, ID3v2_HEADER_SIZE);
  790. if (ret != ID3v2_HEADER_SIZE) {
  791. avio_seek(pb, off, SEEK_SET);
  792. break;
  793. }
  794. found_header = ff_id3v2_match(buf, magic);
  795. if (found_header) {
  796. /* parse ID3v2 header */
  797. len = ((buf[6] & 0x7f) << 21) |
  798. ((buf[7] & 0x7f) << 14) |
  799. ((buf[8] & 0x7f) << 7) |
  800. (buf[9] & 0x7f);
  801. id3v2_parse(pb, metadata, s, len, buf[3], buf[5], extra_meta);
  802. } else {
  803. avio_seek(pb, off, SEEK_SET);
  804. }
  805. } while (found_header);
  806. ff_metadata_conv(metadata, NULL, ff_id3v2_34_metadata_conv);
  807. ff_metadata_conv(metadata, NULL, id3v2_2_metadata_conv);
  808. ff_metadata_conv(metadata, NULL, ff_id3v2_4_metadata_conv);
  809. merge_date(metadata);
  810. }
  811. void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata,
  812. const char *magic, ID3v2ExtraMeta **extra_meta)
  813. {
  814. id3v2_read_internal(pb, metadata, NULL, magic, extra_meta, 0);
  815. }
  816. void ff_id3v2_read(AVFormatContext *s, const char *magic,
  817. ID3v2ExtraMeta **extra_meta, unsigned int max_search_size)
  818. {
  819. id3v2_read_internal(s->pb, &s->metadata, s, magic, extra_meta, max_search_size);
  820. }
  821. void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
  822. {
  823. ID3v2ExtraMeta *current = *extra_meta, *next;
  824. const ID3v2EMFunc *extra_func;
  825. while (current) {
  826. if ((extra_func = get_extra_meta_func(current->tag, 1)))
  827. extra_func->free(current->data);
  828. next = current->next;
  829. av_freep(&current);
  830. current = next;
  831. }
  832. *extra_meta = NULL;
  833. }
  834. int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta **extra_meta)
  835. {
  836. ID3v2ExtraMeta *cur;
  837. for (cur = *extra_meta; cur; cur = cur->next) {
  838. ID3v2ExtraMetaAPIC *apic;
  839. AVStream *st;
  840. if (strcmp(cur->tag, "APIC"))
  841. continue;
  842. apic = cur->data;
  843. if (!(st = avformat_new_stream(s, NULL)))
  844. return AVERROR(ENOMEM);
  845. st->disposition |= AV_DISPOSITION_ATTACHED_PIC;
  846. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  847. st->codec->codec_id = apic->id;
  848. av_dict_set(&st->metadata, "title", apic->description, 0);
  849. av_dict_set(&st->metadata, "comment", apic->type, 0);
  850. av_init_packet(&st->attached_pic);
  851. st->attached_pic.buf = apic->buf;
  852. st->attached_pic.data = apic->buf->data;
  853. st->attached_pic.size = apic->buf->size - FF_INPUT_BUFFER_PADDING_SIZE;
  854. st->attached_pic.stream_index = st->index;
  855. st->attached_pic.flags |= AV_PKT_FLAG_KEY;
  856. apic->buf = NULL;
  857. }
  858. return 0;
  859. }