* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
- #include "id3v2.h"
- #include "id3v1.h"
+/**
+ * @file
+ * ID3v2 header parser
+ *
+ * Specifications available at:
+ * http://id3.org/Developer_Information
+ */
+
+#include "config.h"
+
+#if CONFIG_ZLIB
+#include <zlib.h>
+#endif
+
#include "libavutil/avstring.h"
- #include "libavutil/intreadwrite.h"
#include "libavutil/dict.h"
+ #include "libavutil/intreadwrite.h"
#include "avio_internal.h"
#include "internal.h"
+ #include "id3v1.h"
+ #include "id3v2.h"
const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
- { "TALB", "album"},
- { "TCOM", "composer"},
- { "TCON", "genre"},
- { "TCOP", "copyright"},
- { "TENC", "encoded_by"},
- { "TIT2", "title"},
- { "TLAN", "language"},
- { "TPE1", "artist"},
- { "TPE2", "album_artist"},
- { "TPE3", "performer"},
- { "TPOS", "disc"},
- { "TPUB", "publisher"},
- { "TRCK", "track"},
- { "TSSE", "encoder"},
+ { "TALB", "album" },
+ { "TCOM", "composer" },
+ { "TCON", "genre" },
+ { "TCOP", "copyright" },
+ { "TENC", "encoded_by" },
+ { "TIT2", "title" },
+ { "TLAN", "language" },
+ { "TPE1", "artist" },
+ { "TPE2", "album_artist" },
+ { "TPE3", "performer" },
+ { "TPOS", "disc" },
+ { "TPUB", "publisher" },
+ { "TRCK", "track" },
+ { "TSSE", "encoder" },
{ 0 }
};
static const ID3v2EMFunc id3v2_extra_meta_funcs[] = {
{ "GEO", "GEOB", read_geobtag, free_geobtag },
- { "PIC", "APIC", read_apic, free_apic },
- { "CHAP","CHAP", read_chapter, NULL },
+ { "PIC", "APIC", read_apic, free_apic },
++ { "CHAP","CHAP", read_chapter, NULL },
{ NULL }
};
AVIOContext pb;
AVIOContext *pbx;
unsigned char *buffer = NULL;
- int buffer_size = 0;
+ int buffer_size = 0;
- const ID3v2EMFunc *extra_func;
+ const ID3v2EMFunc *extra_func = NULL;
+ unsigned char *uncompressed_buffer = NULL;
+ int uncompressed_buffer_size = 0;
+
+ av_log(s, AV_LOG_DEBUG, "id3v2 ver:%d flags:%02X len:%d\n", version, flags, len);
switch (version) {
case 2:
while (len >= taghdrlen) {
unsigned int tflags = 0;
- int tunsync = 0;
- int tcomp = 0;
- int tencr = 0;
+ int tunsync = 0;
++ int tcomp = 0;
++ int tencr = 0;
+ unsigned long dlen;
if (isv34) {
avio_read(s->pb, tag, 4);
} else {
avio_read(s->pb, tag, 3);
tag[3] = 0;
- tlen = avio_rb24(s->pb);
+ tlen = avio_rb24(s->pb);
}
- if (tlen < 0 || tlen > len - taghdrlen) {
- av_log(s, AV_LOG_WARNING,
- "Invalid size in frame %s, skipping the rest of tag.\n",
- tag);
+ if (tlen > (1<<28))
break;
- }
len -= taghdrlen + tlen;
+
+ if (len < 0)
+ break;
+
next = avio_tell(s->pb) + tlen;
if (!tlen) {
}
if (tflags & ID3v2_FLAG_DATALEN) {
- avio_rb32(s->pb);
+ if (tlen < 4)
+ break;
+ dlen = avio_rb32(s->pb);
tlen -= 4;
- }
+ } else
+ dlen = tlen;
+
+ tcomp = tflags & ID3v2_FLAG_COMPRESSION;
+ tencr = tflags & ID3v2_FLAG_ENCRYPTION;
+
+ /* skip encrypted tags and, if no zlib, compressed tags */
+ if (tencr || (!CONFIG_ZLIB && tcomp)) {
+ const char *type;
+ if (!tcomp)
+ type = "encrypted";
+ else if (!tencr)
+ type = "compressed";
+ else
+ type = "encrypted and compressed";
- if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
- av_log(s, AV_LOG_WARNING,
- "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
+ av_log(s, AV_LOG_WARNING, "Skipping %s ID3v2 frame %s.\n", type, tag);
avio_skip(s->pb, tlen);
/* check for text tag or supported special meta tag */
- } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)))) {
+ } else if (tag[0] == 'T' ||
+ (extra_meta &&
+ (extra_func = get_extra_meta_func(tag, isv34)))) {
- if (unsync || tunsync) {
- int64_t end = avio_tell(s->pb) + tlen;
- uint8_t *b;
+ pbx = s->pb;
+
+ if (unsync || tunsync || tcomp) {
av_fast_malloc(&buffer, &buffer_size, tlen);
if (!buffer) {
av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", tlen);
*b++ = val ? val : avio_r8(s->pb);
}
}
- ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL, NULL);
+ ffio_init_context(&pb, buffer, b - buffer, 0, NULL, NULL, NULL,
+ NULL);
tlen = b - buffer;
- pbx = &pb; // read from sync buffer
+ pbx = &pb; // read from sync buffer
- } else {
- pbx = s->pb; // read straight from input
}
+
+#if CONFIG_ZLIB
+ if (tcomp) {
+ int err;
+
+ av_log(s, AV_LOG_DEBUG, "Compresssed frame %s tlen=%d dlen=%ld\n", tag, tlen, dlen);
+
+ av_fast_malloc(&uncompressed_buffer, &uncompressed_buffer_size, dlen);
+ if (!uncompressed_buffer) {
+ av_log(s, AV_LOG_ERROR, "Failed to alloc %ld bytes\n", dlen);
+ goto seek;
+ }
+
+ if (!(unsync || tunsync)) {
+ err = avio_read(s->pb, buffer, tlen);
+ if (err < 0) {
+ av_log(s, AV_LOG_ERROR, "Failed to read compressed tag\n");
+ goto seek;
+ }
+ tlen = err;
+ }
+
+ err = uncompress(uncompressed_buffer, &dlen, buffer, tlen);
+ if (err != Z_OK) {
+ av_log(s, AV_LOG_ERROR, "Failed to uncompress tag: %d\n", err);
+ goto seek;
+ }
+ ffio_init_context(&pb, uncompressed_buffer, dlen, 0, NULL, NULL, NULL, NULL);
+ tlen = dlen;
+ pbx = &pb; // read from sync buffer
+ }
+#endif
if (tag[0] == 'T')
/* parse text tag */
read_ttag(s, pbx, tlen, tag);
else
/* parse special meta tag */
extra_func->read(s, pbx, tlen, tag, extra_meta);
- }
- else if (!tag[0]) {
+ } else if (!tag[0]) {
if (tag[1])
- av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding");
+ av_log(s, AV_LOG_WARNING, "invalid frame id, assuming padding\n");
avio_skip(s->pb, tlen);
break;
}
avio_seek(s->pb, next, SEEK_SET);
}
- if (version == 4 && flags & 0x10) /* Footer preset, always 10 bytes, skip over it */
+ /* Footer preset, always 10 bytes, skip over it */
+ if (version == 4 && flags & 0x10)
end += 10;
- error:
+ error:
if (reason)
- av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n", version, reason);
+ av_log(s, AV_LOG_INFO, "ID3v2.%d tag skipped, cannot handle %s\n",
+ version, reason);
avio_seek(s->pb, end, SEEK_SET);
av_free(buffer);
+ av_free(uncompressed_buffer);
return;
}