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