summaryrefslogtreecommitdiff
path: root/libavformat
diff options
context:
space:
mode:
Diffstat (limited to 'libavformat')
-rw-r--r--libavformat/id3v2.c270
-rw-r--r--libavformat/id3v2.h38
-rw-r--r--libavformat/mov.c2
-rw-r--r--libavformat/mpegts.c2
-rw-r--r--libavformat/oma.c269
-rw-r--r--libavformat/swfdec.c5
6 files changed, 529 insertions, 57 deletions
diff --git a/libavformat/id3v2.c b/libavformat/id3v2.c
index 930ab5c870..29ba1abc5e 100644
--- a/libavformat/id3v2.c
+++ b/libavformat/id3v2.c
@@ -66,63 +66,129 @@ static unsigned int get_size(AVIOContext *s, int len)
return v;
}
-static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
+/**
+ * Free GEOB type extra metadata.
+ */
+static void free_geobtag(ID3v2ExtraMetaGEOB *geob)
{
- char *q, dst[512];
- const char *val = NULL;
- int len, dstlen = sizeof(dst) - 1;
- unsigned genre;
- unsigned int (*get)(AVIOContext*) = avio_rb16;
+ av_free(geob->mime_type);
+ av_free(geob->file_name);
+ av_free(geob->description);
+ av_free(geob->data);
+ av_free(geob);
+}
- dst[0] = 0;
- if (taglen < 1)
- return;
+/**
+ * Decode characters to UTF-8 according to encoding type. The decoded buffer is
+ * always null terminated.
+ *
+ * @param dst Pointer where the address of the buffer with the decoded bytes is
+ * stored. Buffer must be freed by caller.
+ * @param dstlen Pointer to an int where the length of the decoded string
+ * is stored (in bytes, incl. null termination)
+ * @param maxread Pointer to maximum number of characters to read from the
+ * AVIOContext. After execution the value is decremented by the number of bytes
+ * actually read.
+ * @seeknull If true, decoding stops after the first U+0000 character found, if
+ * there is any before maxread is reached
+ * @returns 0 if no error occured, dst is uninitialized on error
+ */
+static int decode_str(AVFormatContext *s, AVIOContext *pb, int encoding,
+ uint8_t **dst, int *dstlen, int *maxread, const int seeknull)
+{
+ int len, ret;
+ uint8_t tmp;
+ uint32_t ch = 1;
+ int left = *maxread;
+ unsigned int (*get)(AVIOContext*) = avio_rb16;
+ AVIOContext *dynbuf;
- taglen--; /* account for encoding type byte */
+ if ((ret = avio_open_dyn_buf(&dynbuf)) < 0) {
+ av_log(s, AV_LOG_ERROR, "Error opening memory stream\n");
+ return ret;
+ }
- switch (avio_r8(pb)) { /* encoding type */
+ switch (encoding) {
case ID3v2_ENCODING_ISO8859:
- q = dst;
- while (taglen-- && q - dst < dstlen - 7) {
- uint8_t tmp;
- PUT_UTF8(avio_r8(pb), tmp, *q++ = tmp;)
+ while (left && (!seeknull || ch)) {
+ ch = avio_r8(pb);
+ PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
+ left--;
}
- *q = 0;
break;
case ID3v2_ENCODING_UTF16BOM:
- taglen -= 2;
+ if ((left -= 2) < 0) {
+ av_log(s, AV_LOG_ERROR, "Cannot read BOM value, input too short\n");
+ avio_close_dyn_buf(dynbuf, (uint8_t **)dst);
+ av_freep(dst);
+ return AVERROR_INVALIDDATA;
+ }
switch (avio_rb16(pb)) {
case 0xfffe:
get = avio_rl16;
case 0xfeff:
break;
default:
- av_log(s, AV_LOG_ERROR, "Incorrect BOM value in tag %s.\n", key);
- return;
+ av_log(s, AV_LOG_ERROR, "Incorrect BOM value\n");
+ avio_close_dyn_buf(dynbuf, (uint8_t **)dst);
+ av_freep(dst);
+ *maxread = left;
+ return AVERROR_INVALIDDATA;
}
// fall-through
case ID3v2_ENCODING_UTF16BE:
- q = dst;
- while (taglen > 1 && q - dst < dstlen - 7) {
- uint32_t ch;
- uint8_t tmp;
-
- GET_UTF16(ch, ((taglen -= 2) >= 0 ? get(pb) : 0), break;)
- PUT_UTF8(ch, tmp, *q++ = tmp;)
+ while ((left > 1) && (!seeknull || ch)) {
+ GET_UTF16(ch, ((left -= 2) >= 0 ? get(pb) : 0), break;)
+ PUT_UTF8(ch, tmp, avio_w8(dynbuf, tmp);)
}
- *q = 0;
+ if (left < 0)
+ left += 2; /* did not read last char from pb */
break;
case ID3v2_ENCODING_UTF8:
- len = FFMIN(taglen, dstlen);
- avio_read(pb, dst, len);
- dst[len] = 0;
+ while (left && (!seeknull || ch)) {
+ ch = avio_r8(pb);
+ avio_w8(dynbuf, ch);
+ left--;
+ }
break;
default:
- av_log(s, AV_LOG_WARNING, "Unknown encoding in tag %s.\n", key);
+ av_log(s, AV_LOG_WARNING, "Unknown encoding\n");
+ }
+
+ if (ch)
+ avio_w8(dynbuf, 0);
+
+ len = avio_close_dyn_buf(dynbuf, (uint8_t **)dst);
+ if (dstlen)
+ *dstlen = len;
+
+ *maxread = left;
+
+ return 0;
+}
+
+/**
+ * Parse a text tag.
+ */
+static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const char *key)
+{
+ uint8_t *dst;
+ const char *val = NULL;
+ int len, dstlen;
+ unsigned genre;
+
+ if (taglen < 1)
+ return;
+
+ taglen--; /* account for encoding type byte */
+
+ if (decode_str(s, pb, avio_r8(pb), &dst, &dstlen, &taglen, 0) < 0) {
+ av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", key);
+ return;
}
if (!(strcmp(key, "TCON") && strcmp(key, "TCO"))
@@ -141,6 +207,82 @@ static void read_ttag(AVFormatContext *s, AVIOContext *pb, int taglen, const cha
if (val)
av_dict_set(&s->metadata, key, val, AV_DICT_DONT_OVERWRITE);
+
+ av_free(dst);
+}
+
+/**
+ * Parse GEOB tag into a ID3v2ExtraMetaGEOB struct.
+ */
+static void read_geobtag(AVFormatContext *s, AVIOContext *pb, int taglen, char *tag, ID3v2ExtraMeta **extra_meta)
+{
+ ID3v2ExtraMetaGEOB *geob_data = NULL;
+ ID3v2ExtraMeta *new_extra = NULL;
+ char encoding;
+ unsigned int len;
+
+ if (taglen < 1)
+ return;
+
+ geob_data = av_mallocz(sizeof(ID3v2ExtraMetaGEOB));
+ if (!geob_data) {
+ av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMetaGEOB));
+ return;
+ }
+
+ new_extra = av_mallocz(sizeof(ID3v2ExtraMeta));
+ if (!new_extra) {
+ av_log(s, AV_LOG_ERROR, "Failed to alloc %zu bytes\n", sizeof(ID3v2ExtraMeta));
+ goto fail;
+ }
+
+ /* read encoding type byte */
+ encoding = avio_r8(pb);
+ taglen--;
+
+ /* read MIME type (always ISO-8859) */
+ if (decode_str(s, pb, ID3v2_ENCODING_ISO8859, &geob_data->mime_type, NULL, &taglen, 1) < 0
+ || taglen <= 0)
+ goto fail;
+
+ /* read file name */
+ if (decode_str(s, pb, encoding, &geob_data->file_name, NULL, &taglen, 1) < 0
+ || taglen <= 0)
+ goto fail;
+
+ /* read content description */
+ if (decode_str(s, pb, encoding, &geob_data->description, NULL, &taglen, 1) < 0
+ || taglen < 0)
+ goto fail;
+
+ if (taglen) {
+ /* save encapsulated binary data */
+ geob_data->data = av_malloc(taglen);
+ if (!geob_data->data) {
+ av_log(s, AV_LOG_ERROR, "Failed to alloc %d bytes\n", taglen);
+ goto fail;
+ }
+ if ((len = avio_read(pb, geob_data->data, taglen)) < taglen)
+ av_log(s, AV_LOG_WARNING, "Error reading GEOB frame, data truncated.\n");
+ geob_data->datasize = len;
+ } else {
+ geob_data->data = NULL;
+ geob_data->datasize = 0;
+ }
+
+ /* add data to the list */
+ new_extra->tag = "GEOB";
+ new_extra->data = geob_data;
+ new_extra->next = *extra_meta;
+ *extra_meta = new_extra;
+
+ return;
+
+fail:
+ av_log(s, AV_LOG_ERROR, "Error reading frame %s, skipped\n", tag);
+ free_geobtag(geob_data);
+ av_free(new_extra);
+ return;
}
static int is_number(const char *str)
@@ -189,7 +331,27 @@ finish:
av_dict_set(m, "date", date, 0);
}
-static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags)
+/**
+ * Get the corresponding ID3v2EMFunc struct for a tag.
+ * @param isv34 Determines if v2.2 or v2.3/4 strings are used
+ * @return A pointer to the ID3v2EMFunc struct if found, NULL otherwise.
+ */
+static const ID3v2EMFunc *get_extra_meta_func(const char *tag, int isv34)
+{
+ int i = 0;
+ while (ff_id3v2_extra_meta_funcs[i].tag3) {
+ if (!memcmp(tag,
+ (isv34 ?
+ ff_id3v2_extra_meta_funcs[i].tag4 :
+ ff_id3v2_extra_meta_funcs[i].tag3),
+ (isv34 ? 4 : 3)))
+ return &ff_id3v2_extra_meta_funcs[i];
+ i++;
+ }
+ return NULL;
+}
+
+static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t flags, ID3v2ExtraMeta **extra_meta)
{
int isv34, unsync;
unsigned tlen;
@@ -198,8 +360,10 @@ static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t
int taghdrlen;
const char *reason = NULL;
AVIOContext pb;
+ AVIOContext *pbx;
unsigned char *buffer = NULL;
int buffer_size = 0;
+ void (*extra_func)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta**) = NULL;
switch (version) {
case 2:
@@ -264,7 +428,8 @@ static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t
if (tflags & (ID3v2_FLAG_ENCRYPTION | ID3v2_FLAG_COMPRESSION)) {
av_log(s, AV_LOG_WARNING, "Skipping encrypted/compressed ID3v2 frame %s.\n", tag);
avio_skip(s->pb, tlen);
- } else if (tag[0] == 'T') {
+ /* check for text tag or supported special meta tag */
+ } else if (tag[0] == 'T' || (extra_meta && (extra_func = get_extra_meta_func(tag, isv34)->read))) {
if (unsync || tunsync) {
int i, j;
av_fast_malloc(&buffer, &buffer_size, tlen);
@@ -280,10 +445,17 @@ static void ff_id3v2_parse(AVFormatContext *s, int len, uint8_t version, uint8_t
}
}
ffio_init_context(&pb, buffer, j, 0, NULL, NULL, NULL, NULL);
- read_ttag(s, &pb, j, tag);
+ tlen = j;
+ pbx = &pb; // read from sync buffer
} else {
- read_ttag(s, s->pb, tlen, tag);
+ pbx = s->pb; // read straight from input
}
+ if (tag[0] == 'T')
+ /* parse text tag */
+ read_ttag(s, pbx, tlen, tag);
+ else
+ /* parse special meta tag */
+ extra_func(s, pbx, tlen, tag, extra_meta);
}
else if (!tag[0]) {
if (tag[1])
@@ -307,7 +479,7 @@ seek:
return;
}
-void ff_id3v2_read(AVFormatContext *s, const char *magic)
+void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
{
int len, ret;
uint8_t buf[ID3v2_HEADER_SIZE];
@@ -327,7 +499,7 @@ void ff_id3v2_read(AVFormatContext *s, const char *magic)
((buf[7] & 0x7f) << 14) |
((buf[8] & 0x7f) << 7) |
(buf[9] & 0x7f);
- ff_id3v2_parse(s, len, buf[3], buf[5]);
+ ff_id3v2_parse(s, len, buf[3], buf[5], extra_meta);
} else {
avio_seek(s->pb, off, SEEK_SET);
}
@@ -338,6 +510,30 @@ void ff_id3v2_read(AVFormatContext *s, const char *magic)
merge_date(&s->metadata);
}
+void ff_id3v2_read(AVFormatContext *s, const char *magic)
+{
+ ff_id3v2_read_all(s, magic, NULL);
+}
+
+void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
+{
+ ID3v2ExtraMeta *current = *extra_meta, *next;
+ void (*free_func)(ID3v2ExtraMeta*);
+
+ while (current) {
+ if ((free_func = get_extra_meta_func(current->tag, 1)->free))
+ free_func(current->data);
+ next = current->next;
+ av_freep(&current);
+ current = next;
+ }
+}
+
+const ID3v2EMFunc ff_id3v2_extra_meta_funcs[] = {
+ { "GEO", "GEOB", read_geobtag, free_geobtag },
+ { NULL }
+};
+
const AVMetadataConv ff_id3v2_34_metadata_conv[] = {
{ "TALB", "album"},
{ "TCOM", "composer"},
diff --git a/libavformat/id3v2.h b/libavformat/id3v2.h
index e429001385..a30a74f5fd 100644
--- a/libavformat/id3v2.h
+++ b/libavformat/id3v2.h
@@ -45,6 +45,27 @@ enum ID3v2Encoding {
ID3v2_ENCODING_UTF8 = 3,
};
+typedef struct ID3v2ExtraMeta {
+ const char *tag;
+ void *data;
+ struct ID3v2ExtraMeta *next;
+} ID3v2ExtraMeta;
+
+typedef struct ID3v2ExtraMetaGEOB {
+ uint32_t datasize;
+ uint8_t *mime_type;
+ uint8_t *file_name;
+ uint8_t *description;
+ uint8_t *data;
+} ID3v2ExtraMetaGEOB;
+
+typedef struct ID3v2EMFunc {
+ const char *tag3;
+ const char *tag4;
+ void (*read)(AVFormatContext*, AVIOContext*, int, char*, ID3v2ExtraMeta **);
+ void (*free)();
+} ID3v2EMFunc;
+
/**
* Detect ID3v2 Header.
* @param buf must be ID3v2_HEADER_SIZE byte long
@@ -61,10 +82,25 @@ int ff_id3v2_match(const uint8_t *buf, const char *magic);
int ff_id3v2_tag_len(const uint8_t *buf);
/**
- * Read an ID3v2 tag
+ * Read an ID3v2 tag (text tags only)
*/
void ff_id3v2_read(AVFormatContext *s, const char *magic);
+/**
+ * Read an ID3v2 tag, including supported extra metadata (currently only GEOB)
+ * @param extra_meta If not NULL, extra metadata is parsed into a list of
+ * ID3v2ExtraMeta structs and *extra_meta points to the head of the list
+ */
+void ff_id3v2_read_all(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta);
+
+/**
+ * Free memory allocated parsing special (non-text) metadata.
+ * @param extra_meta Pointer to a pointer to the head of a ID3v2ExtraMeta list, *extra_meta is set to NULL.
+ */
+void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta);
+
+extern const ID3v2EMFunc ff_id3v2_extra_meta_funcs[];
+
extern const AVMetadataConv ff_id3v2_34_metadata_conv[];
extern const AVMetadataConv ff_id3v2_4_metadata_conv[];
extern const AVMetadataConv ff_id3v2_2_metadata_conv[];
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 70cd776496..5f91e7d50c 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2629,8 +2629,6 @@ static int mov_read_close(AVFormatContext *s)
av_freep(&sc->drefs);
if (sc->pb && sc->pb != s->pb)
avio_close(sc->pb);
-
- av_freep(&st->codec->palctrl);
}
if (mov->dv_demux) {
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index d3f40aa892..02f0d56e5a 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -1444,7 +1444,7 @@ static int handle_packets(MpegTSContext *ts, int nb_packets)
if (avio_tell(s->pb) != ts->last_pos) {
int i;
-// av_dlog("Skipping after seek\n");
+ av_dlog(ts->stream, "Skipping after seek\n");
/* seek detected, flush pes buffer */
for (i = 0; i < NB_PID_MAX; i++) {
if (ts->pids[i]) {
diff --git a/libavformat/oma.c b/libavformat/oma.c
index 1ab30688c6..09fa2ca985 100644
--- a/libavformat/oma.c
+++ b/libavformat/oma.c
@@ -3,6 +3,7 @@
*
* Copyright (c) 2008 Maxim Poliakovski
* 2008 Benjamin Larsson
+ * 2011 David Goldwich
*
* This file is part of FFmpeg.
*
@@ -36,20 +37,19 @@
* - Sound data organized in packets follow the EA3 header
* (can be encrypted using the Sony DRM!).
*
- * LIMITATIONS: This version supports only plain (unencrypted) OMA files.
- * If any DRM-protected (encrypted) file is encountered you will get the
- * corresponding error message. Try to remove the encryption using any
- * Sony software (for example SonicStage).
* CODEC SUPPORT: Only ATRAC3 codec is currently supported!
*/
#include "avformat.h"
#include "libavutil/intreadwrite.h"
+#include "libavutil/des.h"
#include "pcm.h"
#include "riff.h"
#include "id3v2.h"
#define EA3_HEADER_SIZE 96
+#define ID3v2_EA3_MAGIC "ea3"
+#define OMA_ENC_HEADER_SIZE 16
enum {
OMA_CODECID_ATRAC3 = 0,
@@ -65,7 +65,211 @@ static const AVCodecTag codec_oma_tags[] = {
{ CODEC_ID_MP3, OMA_CODECID_MP3 },
};
-#define ID3v2_EA3_MAGIC "ea3"
+static const uint64_t leaf_table[] = {
+ 0xd79e8283acea4620, 0x7a9762f445afd0d8,
+ 0x354d60a60b8c79f1, 0x584e1cde00b07aee,
+ 0x1573cd93da7df623, 0x47f98d79620dd535
+};
+
+typedef struct OMAContext {
+ uint64_t content_start;
+ int encrypted;
+ uint16_t k_size;
+ uint16_t e_size;
+ uint16_t i_size;
+ uint16_t s_size;
+ uint32_t rid;
+ uint8_t r_val[24];
+ uint8_t n_val[24];
+ uint8_t m_val[8];
+ uint8_t s_val[8];
+ uint8_t sm_val[8];
+ uint8_t e_val[8];
+ uint8_t iv[8];
+ struct AVDES av_des;
+} OMAContext;
+
+static void hex_log(AVFormatContext *s, int level, const char *name, const uint8_t *value, int len)
+{
+ char buf[33];
+ len = FFMIN(len, 16);
+ if (av_log_get_level() < level)
+ return;
+ ff_data_to_hex(buf, value, len, 1);
+ buf[len<<1] = '\0';
+ av_log(s, level, "%s: %s\n", name, buf);
+}
+
+static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, int len)
+{
+ OMAContext *oc = s->priv_data;
+
+ if (!r_val && !n_val)
+ return -1;
+
+ len = FFMIN(len, 16);
+
+ /* use first 64 bits in the third round again */
+ if (r_val) {
+ if (r_val != oc->r_val) {
+ memset(oc->r_val, 0, 24);
+ memcpy(oc->r_val, r_val, len);
+ }
+ memcpy(&oc->r_val[16], r_val, 8);
+ }
+ if (n_val) {
+ if (n_val != oc->n_val) {
+ memset(oc->n_val, 0, 24);
+ memcpy(oc->n_val, n_val, len);
+ }
+ memcpy(&oc->n_val[16], n_val, 8);
+ }
+
+ return 0;
+}
+
+static int rprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *r_val)
+{
+ OMAContext *oc = s->priv_data;
+ unsigned int pos;
+ struct AVDES av_des;
+
+ if (!enc_header || !r_val)
+ return -1;
+
+ /* m_val */
+ av_des_init(&av_des, r_val, 192, 1);
+ av_des_crypt(&av_des, oc->m_val, &enc_header[48], 1, NULL, 1);
+
+ /* s_val */
+ av_des_init(&av_des, oc->m_val, 64, 0);
+ av_des_crypt(&av_des, oc->s_val, NULL, 1, NULL, 0);
+
+ /* sm_val */
+ pos = OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size;
+ av_des_init(&av_des, oc->s_val, 64, 0);
+ av_des_mac(&av_des, oc->sm_val, &enc_header[pos], (oc->i_size >> 3));
+
+ pos += oc->i_size;
+
+ return memcmp(&enc_header[pos], oc->sm_val, 8) ? -1 : 0;
+}
+
+static int nprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *n_val)
+{
+ OMAContext *oc = s->priv_data;
+ uint32_t pos, taglen, datalen;
+ struct AVDES av_des;
+
+ if (!enc_header || !n_val)
+ return -1;
+
+ pos = OMA_ENC_HEADER_SIZE + oc->k_size;
+ if (!memcmp(&enc_header[pos], "EKB ", 4))
+ pos += 32;
+
+ if (AV_RB32(&enc_header[pos]) != oc->rid)
+ av_log(s, AV_LOG_DEBUG, "Mismatching RID\n");
+
+ taglen = AV_RB32(&enc_header[pos+32]);
+ datalen = AV_RB32(&enc_header[pos+36]) >> 4;
+
+ pos += 44 + taglen;
+
+ av_des_init(&av_des, n_val, 192, 1);
+ while (datalen-- > 0) {
+ av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1);
+ kset(s, oc->r_val, NULL, 16);
+ if (!rprobe(s, enc_header, oc->r_val))
+ return 0;
+ pos += 16;
+ }
+
+ return -1;
+}
+
+static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
+{
+ OMAContext *oc = s->priv_data;
+ ID3v2ExtraMetaGEOB *geob = NULL;
+ uint8_t *gdata;
+
+ oc->encrypted = 1;
+ av_log(s, AV_LOG_INFO, "File is encrypted\n");
+
+ /* find GEOB metadata */
+ while (em) {
+ if (!strcmp(em->tag, "GEOB") &&
+ (geob = em->data) &&
+ !strcmp(geob->description, "OMG_LSI") ||
+ !strcmp(geob->description, "OMG_BKLSI")) {
+ break;
+ }
+ em = em->next;
+ }
+ if (!em) {
+ av_log(s, AV_LOG_ERROR, "No encryption header found\n");
+ return -1;
+ }
+
+ if (geob->datasize < 64) {
+ av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize);
+ return -1;
+ }
+
+ gdata = geob->data;
+
+ if (AV_RB16(gdata) != 1)
+ av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n");
+
+ oc->k_size = AV_RB16(&gdata[2]);
+ oc->e_size = AV_RB16(&gdata[4]);
+ oc->i_size = AV_RB16(&gdata[6]);
+ oc->s_size = AV_RB16(&gdata[8]);
+
+ if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING ", 12)) {
+ av_log(s, AV_LOG_ERROR, "Invalid encryption header\n");
+ return -1;
+ }
+ oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
+ av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
+
+ memcpy(oc->iv, &header[0x58], 8);
+ hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
+
+ hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
+
+ if (s->keylen > 0) {
+ kset(s, s->key, s->key, s->keylen);
+ }
+ if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
+ rprobe(s, gdata, oc->r_val) < 0 &&
+ nprobe(s, gdata, oc->n_val) < 0) {
+ int i;
+ for (i = 0; i < sizeof(leaf_table); i += 2) {
+ uint8_t buf[16];
+ AV_WL64(buf, leaf_table[i]);
+ AV_WL64(&buf[8], leaf_table[i+1]);
+ kset(s, buf, buf, 16);
+ if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, oc->n_val))
+ break;
+ }
+ if (i >= sizeof(leaf_table)) {
+ av_log(s, AV_LOG_ERROR, "Invalid key\n");
+ return -1;
+ }
+ }
+
+ /* e_val */
+ av_des_init(&oc->av_des, oc->m_val, 64, 0);
+ av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
+ hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
+
+ /* init e_val */
+ av_des_init(&oc->av_des, oc->e_val, 64, 1);
+
+ return 0;
+}
static int oma_read_header(AVFormatContext *s,
AVFormatParameters *ap)
@@ -77,8 +281,10 @@ static int oma_read_header(AVFormatContext *s,
uint8_t buf[EA3_HEADER_SIZE];
uint8_t *edata;
AVStream *st;
+ ID3v2ExtraMeta *extra_meta = NULL;
+ OMAContext *oc = s->priv_data;
- ff_id3v2_read(s, ID3v2_EA3_MAGIC);
+ ff_id3v2_read_all(s, ID3v2_EA3_MAGIC, &extra_meta);
ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
if (ret < EA3_HEADER_SIZE)
return -1;
@@ -88,12 +294,17 @@ static int oma_read_header(AVFormatContext *s,
return -1;
}
+ oc->content_start = avio_tell(s->pb);
+
+ /* encrypted file */
eid = AV_RB16(&buf[6]);
- if (eid != -1 && eid != -128) {
- av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
+ if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) {
+ ff_id3v2_free_extra_meta(&extra_meta);
return -1;
}
+ ff_id3v2_free_extra_meta(&extra_meta);
+
codec_params = AV_RB24(&buf[33]);
st = av_new_stream(s, 0);
@@ -159,12 +370,20 @@ static int oma_read_header(AVFormatContext *s,
static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
{
- int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
+ OMAContext *oc = s->priv_data;
+ int packet_size = s->streams[0]->codec->block_align;
+ int ret = av_get_packet(s->pb, pkt, packet_size);
- pkt->stream_index = 0;
if (ret <= 0)
return AVERROR(EIO);
+ pkt->stream_index = 0;
+
+ if (oc->encrypted) {
+ /* previous unencrypted block saved in IV for the next packet (CBC mode) */
+ av_des_crypt(&oc->av_des, pkt->data, pkt->data, (packet_size >> 3), oc->iv, 1);
+ }
+
return ret;
}
@@ -190,16 +409,38 @@ static int oma_read_probe(AVProbeData *p)
return 0;
}
+static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
+{
+ OMAContext *oc = s->priv_data;
+
+ pcm_read_seek(s, stream_index, timestamp, flags);
+
+ if (oc->encrypted) {
+ /* readjust IV for CBC */
+ int64_t pos = avio_tell(s->pb);
+ if (pos < oc->content_start)
+ memset(oc->iv, 0, 8);
+ else {
+ if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) {
+ memset(oc->iv, 0, 8);
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+}
AVInputFormat ff_oma_demuxer = {
.name = "oma",
.long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
+ .priv_data_size = sizeof(OMAContext),
.read_probe = oma_read_probe,
.read_header = oma_read_header,
.read_packet = oma_read_packet,
- .read_seek = pcm_read_seek,
- .flags= AVFMT_GENERIC_INDEX,
- .extensions = "oma,aa3",
- .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
+ .read_seek = oma_read_seek,
+ .flags = AVFMT_GENERIC_INDEX,
+ .extensions = "oma,omg,aa3",
+ .codec_tag = (const AVCodecTag* const []){codec_oma_tags, 0},
};
diff --git a/libavformat/swfdec.c b/libavformat/swfdec.c
index d399cc3a5d..7bbf494e99 100644
--- a/libavformat/swfdec.c
+++ b/libavformat/swfdec.c
@@ -136,8 +136,9 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
ast->need_parsing = AVSTREAM_PARSE_FULL;
sample_rate_code= (v>>2) & 3;
if (!sample_rate_code)
- return AVERROR(EIO);
- ast->codec->sample_rate = 11025 << (sample_rate_code-1);
+ ast->codec->sample_rate = 5512;
+ else
+ ast->codec->sample_rate = 11025 << (sample_rate_code-1);
av_set_pts_info(ast, 64, 1, ast->codec->sample_rate);
len -= 4;
} else if (tag == TAG_VIDEOFRAME) {