00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00043 #include "avformat.h"
00044 #include "internal.h"
00045 #include "libavutil/intreadwrite.h"
00046 #include "libavutil/des.h"
00047 #include "pcm.h"
00048 #include "riff.h"
00049 #include "id3v2.h"
00050
00051 #define EA3_HEADER_SIZE 96
00052 #define ID3v2_EA3_MAGIC "ea3"
00053 #define OMA_ENC_HEADER_SIZE 16
00054
00055 enum {
00056 OMA_CODECID_ATRAC3 = 0,
00057 OMA_CODECID_ATRAC3P = 1,
00058 OMA_CODECID_MP3 = 3,
00059 OMA_CODECID_LPCM = 4,
00060 OMA_CODECID_WMA = 5,
00061 };
00062
00063 static const AVCodecTag codec_oma_tags[] = {
00064 { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 },
00065 { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P },
00066 { CODEC_ID_MP3, OMA_CODECID_MP3 },
00067 { CODEC_ID_PCM_S16BE, OMA_CODECID_LPCM },
00068 };
00069
00070 static const uint64_t leaf_table[] = {
00071 0xd79e8283acea4620, 0x7a9762f445afd0d8,
00072 0x354d60a60b8c79f1, 0x584e1cde00b07aee,
00073 0x1573cd93da7df623, 0x47f98d79620dd535
00074 };
00075
00076 typedef struct OMAContext {
00077 uint64_t content_start;
00078 int encrypted;
00079 uint16_t k_size;
00080 uint16_t e_size;
00081 uint16_t i_size;
00082 uint16_t s_size;
00083 uint32_t rid;
00084 uint8_t r_val[24];
00085 uint8_t n_val[24];
00086 uint8_t m_val[8];
00087 uint8_t s_val[8];
00088 uint8_t sm_val[8];
00089 uint8_t e_val[8];
00090 uint8_t iv[8];
00091 struct AVDES av_des;
00092 } OMAContext;
00093
00094 static void hex_log(AVFormatContext *s, int level, const char *name, const uint8_t *value, int len)
00095 {
00096 char buf[33];
00097 len = FFMIN(len, 16);
00098 if (av_log_get_level() < level)
00099 return;
00100 ff_data_to_hex(buf, value, len, 1);
00101 buf[len<<1] = '\0';
00102 av_log(s, level, "%s: %s\n", name, buf);
00103 }
00104
00105 static int kset(AVFormatContext *s, const uint8_t *r_val, const uint8_t *n_val, int len)
00106 {
00107 OMAContext *oc = s->priv_data;
00108
00109 if (!r_val && !n_val)
00110 return -1;
00111
00112 len = FFMIN(len, 16);
00113
00114
00115 if (r_val) {
00116 if (r_val != oc->r_val) {
00117 memset(oc->r_val, 0, 24);
00118 memcpy(oc->r_val, r_val, len);
00119 }
00120 memcpy(&oc->r_val[16], r_val, 8);
00121 }
00122 if (n_val) {
00123 if (n_val != oc->n_val) {
00124 memset(oc->n_val, 0, 24);
00125 memcpy(oc->n_val, n_val, len);
00126 }
00127 memcpy(&oc->n_val[16], n_val, 8);
00128 }
00129
00130 return 0;
00131 }
00132
00133 static int rprobe(AVFormatContext *s, uint8_t *enc_header, const uint8_t *r_val)
00134 {
00135 OMAContext *oc = s->priv_data;
00136 unsigned int pos;
00137 struct AVDES av_des;
00138
00139 if (!enc_header || !r_val)
00140 return -1;
00141
00142
00143 av_des_init(&av_des, r_val, 192, 1);
00144 av_des_crypt(&av_des, oc->m_val, &enc_header[48], 1, NULL, 1);
00145
00146
00147 av_des_init(&av_des, oc->m_val, 64, 0);
00148 av_des_crypt(&av_des, oc->s_val, NULL, 1, NULL, 0);
00149
00150
00151 pos = OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size;
00152 av_des_init(&av_des, oc->s_val, 64, 0);
00153 av_des_mac(&av_des, oc->sm_val, &enc_header[pos], (oc->i_size >> 3));
00154
00155 pos += oc->i_size;
00156
00157 return memcmp(&enc_header[pos], oc->sm_val, 8) ? -1 : 0;
00158 }
00159
00160 static int nprobe(AVFormatContext *s, uint8_t *enc_header, int size, const uint8_t *n_val)
00161 {
00162 OMAContext *oc = s->priv_data;
00163 uint32_t pos, taglen, datalen;
00164 struct AVDES av_des;
00165
00166 if (!enc_header || !n_val)
00167 return -1;
00168
00169 pos = OMA_ENC_HEADER_SIZE + oc->k_size;
00170 if (!memcmp(&enc_header[pos], "EKB ", 4))
00171 pos += 32;
00172
00173 if (AV_RB32(&enc_header[pos]) != oc->rid)
00174 av_log(s, AV_LOG_DEBUG, "Mismatching RID\n");
00175
00176 taglen = AV_RB32(&enc_header[pos+32]);
00177 datalen = AV_RB32(&enc_header[pos+36]) >> 4;
00178
00179 if(taglen + (((uint64_t)datalen)<<4) + 44 > size)
00180 return -1;
00181
00182 pos += 44 + taglen;
00183
00184 av_des_init(&av_des, n_val, 192, 1);
00185 while (datalen-- > 0) {
00186 av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1);
00187 kset(s, oc->r_val, NULL, 16);
00188 if (!rprobe(s, enc_header, oc->r_val))
00189 return 0;
00190 pos += 16;
00191 }
00192
00193 return -1;
00194 }
00195
00196 static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
00197 {
00198 OMAContext *oc = s->priv_data;
00199 ID3v2ExtraMetaGEOB *geob = NULL;
00200 uint8_t *gdata;
00201
00202 oc->encrypted = 1;
00203 av_log(s, AV_LOG_INFO, "File is encrypted\n");
00204
00205
00206 while (em) {
00207 if (!strcmp(em->tag, "GEOB") &&
00208 (geob = em->data) &&
00209 (!strcmp(geob->description, "OMG_LSI") ||
00210 !strcmp(geob->description, "OMG_BKLSI"))) {
00211 break;
00212 }
00213 em = em->next;
00214 }
00215 if (!em) {
00216 av_log(s, AV_LOG_ERROR, "No encryption header found\n");
00217 return -1;
00218 }
00219
00220 if (geob->datasize < 64) {
00221 av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize);
00222 return -1;
00223 }
00224
00225 gdata = geob->data;
00226
00227 if (AV_RB16(gdata) != 1)
00228 av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n");
00229
00230 oc->k_size = AV_RB16(&gdata[2]);
00231 oc->e_size = AV_RB16(&gdata[4]);
00232 oc->i_size = AV_RB16(&gdata[6]);
00233 oc->s_size = AV_RB16(&gdata[8]);
00234
00235 if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING ", 12)) {
00236 av_log(s, AV_LOG_ERROR, "Invalid encryption header\n");
00237 return -1;
00238 }
00239 oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
00240 av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
00241
00242 memcpy(oc->iv, &header[0x58], 8);
00243 hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
00244
00245 hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
00246
00247 if (s->keylen > 0) {
00248 kset(s, s->key, s->key, s->keylen);
00249 }
00250 if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
00251 rprobe(s, gdata, oc->r_val) < 0 &&
00252 nprobe(s, gdata, geob->datasize, oc->n_val) < 0) {
00253 int i;
00254 for (i = 0; i < sizeof(leaf_table); i += 2) {
00255 uint8_t buf[16];
00256 AV_WL64(buf, leaf_table[i]);
00257 AV_WL64(&buf[8], leaf_table[i+1]);
00258 kset(s, buf, buf, 16);
00259 if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, geob->datasize, oc->n_val))
00260 break;
00261 }
00262 if (i >= sizeof(leaf_table)) {
00263 av_log(s, AV_LOG_ERROR, "Invalid key\n");
00264 return -1;
00265 }
00266 }
00267
00268
00269 av_des_init(&oc->av_des, oc->m_val, 64, 0);
00270 av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
00271 hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
00272
00273
00274 av_des_init(&oc->av_des, oc->e_val, 64, 1);
00275
00276 return 0;
00277 }
00278
00279 static int oma_read_header(AVFormatContext *s,
00280 AVFormatParameters *ap)
00281 {
00282 static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
00283 int ret, framesize, jsflag, samplerate;
00284 uint32_t codec_params;
00285 int16_t eid;
00286 uint8_t buf[EA3_HEADER_SIZE];
00287 uint8_t *edata;
00288 AVStream *st;
00289 ID3v2ExtraMeta *extra_meta = NULL;
00290 OMAContext *oc = s->priv_data;
00291
00292 ff_id3v2_read_all(s, ID3v2_EA3_MAGIC, &extra_meta);
00293 ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
00294 if (ret < EA3_HEADER_SIZE)
00295 return -1;
00296
00297 if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
00298 av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
00299 return -1;
00300 }
00301
00302 oc->content_start = avio_tell(s->pb);
00303
00304
00305 eid = AV_RB16(&buf[6]);
00306 if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) {
00307 ff_id3v2_free_extra_meta(&extra_meta);
00308 return -1;
00309 }
00310
00311 ff_id3v2_free_extra_meta(&extra_meta);
00312
00313 codec_params = AV_RB24(&buf[33]);
00314
00315 st = avformat_new_stream(s, NULL);
00316 if (!st)
00317 return AVERROR(ENOMEM);
00318
00319 st->start_time = 0;
00320 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00321 st->codec->codec_tag = buf[32];
00322 st->codec->codec_id = ff_codec_get_id(codec_oma_tags, st->codec->codec_tag);
00323
00324 switch (buf[32]) {
00325 case OMA_CODECID_ATRAC3:
00326 samplerate = srate_tab[(codec_params >> 13) & 7]*100;
00327 if (samplerate != 44100)
00328 av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
00329 samplerate);
00330
00331 framesize = (codec_params & 0x3FF) * 8;
00332 jsflag = (codec_params >> 17) & 1;
00333 st->codec->channels = 2;
00334 st->codec->sample_rate = samplerate;
00335 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00336
00337
00338 st->codec->extradata_size = 14;
00339 edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
00340 if (!edata)
00341 return AVERROR(ENOMEM);
00342
00343 st->codec->extradata = edata;
00344 AV_WL16(&edata[0], 1);
00345 AV_WL32(&edata[2], samplerate);
00346 AV_WL16(&edata[6], jsflag);
00347 AV_WL16(&edata[8], jsflag);
00348 AV_WL16(&edata[10], 1);
00349
00350
00351 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00352 break;
00353 case OMA_CODECID_ATRAC3P:
00354 st->codec->channels = (codec_params >> 10) & 7;
00355 framesize = ((codec_params & 0x3FF) * 8) + 8;
00356 st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
00357 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00358 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00359 av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
00360 break;
00361 case OMA_CODECID_MP3:
00362 st->need_parsing = AVSTREAM_PARSE_FULL;
00363 framesize = 1024;
00364 break;
00365 case OMA_CODECID_LPCM:
00366
00367 st->codec->channels = 2;
00368 st->codec->sample_rate = 44100;
00369 framesize = 1024;
00370
00371 st->codec->bit_rate = st->codec->sample_rate * 32;
00372 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
00373 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00374 break;
00375 default:
00376 av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
00377 return -1;
00378 }
00379
00380 st->codec->block_align = framesize;
00381
00382 return 0;
00383 }
00384
00385
00386 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
00387 {
00388 OMAContext *oc = s->priv_data;
00389 int packet_size = s->streams[0]->codec->block_align;
00390 int ret = av_get_packet(s->pb, pkt, packet_size);
00391
00392 if (ret <= 0)
00393 return AVERROR(EIO);
00394
00395 pkt->stream_index = 0;
00396
00397 if (oc->encrypted) {
00398
00399 av_des_crypt(&oc->av_des, pkt->data, pkt->data, (packet_size >> 3), oc->iv, 1);
00400 }
00401
00402 return ret;
00403 }
00404
00405 static int oma_read_probe(AVProbeData *p)
00406 {
00407 const uint8_t *buf;
00408 unsigned tag_len = 0;
00409
00410 buf = p->buf;
00411
00412 if (p->buf_size < ID3v2_HEADER_SIZE ||
00413 !ff_id3v2_match(buf, ID3v2_EA3_MAGIC) ||
00414 buf[3] != 3 ||
00415 buf[4])
00416 return 0;
00417
00418 tag_len = ff_id3v2_tag_len(buf);
00419
00420
00421 if (p->buf_size < tag_len + 5)
00422
00423 return AVPROBE_SCORE_MAX / 2;
00424
00425 buf += tag_len;
00426
00427 if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
00428 return AVPROBE_SCORE_MAX;
00429 else
00430 return 0;
00431 }
00432
00433 static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00434 {
00435 OMAContext *oc = s->priv_data;
00436
00437 pcm_read_seek(s, stream_index, timestamp, flags);
00438
00439 if (oc->encrypted) {
00440
00441 int64_t pos = avio_tell(s->pb);
00442 if (pos < oc->content_start)
00443 memset(oc->iv, 0, 8);
00444 else {
00445 if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) {
00446 memset(oc->iv, 0, 8);
00447 return -1;
00448 }
00449 }
00450 }
00451
00452 return 0;
00453 }
00454
00455 AVInputFormat ff_oma_demuxer = {
00456 .name = "oma",
00457 .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
00458 .priv_data_size = sizeof(OMAContext),
00459 .read_probe = oma_read_probe,
00460 .read_header = oma_read_header,
00461 .read_packet = oma_read_packet,
00462 .read_seek = oma_read_seek,
00463 .flags = AVFMT_GENERIC_INDEX,
00464 .extensions = "oma,omg,aa3",
00465 .codec_tag = (const AVCodecTag* const []){codec_oma_tags, 0},
00466 };
00467