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 if ( OMA_ENC_HEADER_SIZE + oc->k_size + oc->e_size + oc->i_size + 8 > geob->datasize
00223 || OMA_ENC_HEADER_SIZE + 48 > geob->datasize
00224 ) {
00225 av_log(s, AV_LOG_ERROR, "Too little GEOB data\n");
00226 return AVERROR_INVALIDDATA;
00227 }
00228 oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
00229 av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
00230
00231 memcpy(oc->iv, &header[0x58], 8);
00232 hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
00233
00234 hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
00235
00236 if (s->keylen > 0) {
00237 kset(s, s->key, s->key, s->keylen);
00238 }
00239 if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
00240 rprobe(s, gdata, oc->r_val) < 0 &&
00241 nprobe(s, gdata, geob->datasize, oc->n_val) < 0) {
00242 int i;
00243 for (i = 0; i < FF_ARRAY_ELEMS(leaf_table); i += 2) {
00244 uint8_t buf[16];
00245 AV_WL64(buf, leaf_table[i]);
00246 AV_WL64(&buf[8], leaf_table[i+1]);
00247 kset(s, buf, buf, 16);
00248 if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, geob->datasize, oc->n_val))
00249 break;
00250 }
00251 if (i >= FF_ARRAY_ELEMS(leaf_table)) {
00252 av_log(s, AV_LOG_ERROR, "Invalid key\n");
00253 return -1;
00254 }
00255 }
00256
00257
00258 av_des_init(&oc->av_des, oc->m_val, 64, 0);
00259 av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
00260 hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
00261
00262
00263 av_des_init(&oc->av_des, oc->e_val, 64, 1);
00264
00265 return 0;
00266 }
00267
00268 static int oma_read_header(AVFormatContext *s)
00269 {
00270 int ret, framesize, jsflag, samplerate;
00271 uint32_t codec_params;
00272 int16_t eid;
00273 uint8_t buf[EA3_HEADER_SIZE];
00274 uint8_t *edata;
00275 AVStream *st;
00276 ID3v2ExtraMeta *extra_meta = NULL;
00277 OMAContext *oc = s->priv_data;
00278
00279 ff_id3v2_read(s, ID3v2_EA3_MAGIC, &extra_meta);
00280 ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
00281 if (ret < EA3_HEADER_SIZE)
00282 return -1;
00283
00284 if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
00285 av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
00286 return -1;
00287 }
00288
00289 oc->content_start = avio_tell(s->pb);
00290
00291
00292 eid = AV_RB16(&buf[6]);
00293 if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) {
00294 ff_id3v2_free_extra_meta(&extra_meta);
00295 return -1;
00296 }
00297
00298 ff_id3v2_free_extra_meta(&extra_meta);
00299
00300 codec_params = AV_RB24(&buf[33]);
00301
00302 st = avformat_new_stream(s, NULL);
00303 if (!st)
00304 return AVERROR(ENOMEM);
00305
00306 st->start_time = 0;
00307 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00308 st->codec->codec_tag = buf[32];
00309 st->codec->codec_id = ff_codec_get_id(ff_oma_codec_tags, st->codec->codec_tag);
00310
00311 switch (buf[32]) {
00312 case OMA_CODECID_ATRAC3:
00313 samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
00314 if (samplerate != 44100)
00315 av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
00316 samplerate);
00317
00318 framesize = (codec_params & 0x3FF) * 8;
00319 jsflag = (codec_params >> 17) & 1;
00320 st->codec->channels = 2;
00321 st->codec->sample_rate = samplerate;
00322 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00323
00324
00325 st->codec->extradata_size = 14;
00326 edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
00327 if (!edata)
00328 return AVERROR(ENOMEM);
00329
00330 st->codec->extradata = edata;
00331 AV_WL16(&edata[0], 1);
00332 AV_WL32(&edata[2], samplerate);
00333 AV_WL16(&edata[6], jsflag);
00334 AV_WL16(&edata[8], jsflag);
00335 AV_WL16(&edata[10], 1);
00336
00337
00338 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00339 break;
00340 case OMA_CODECID_ATRAC3P:
00341 st->codec->channels = (codec_params >> 10) & 7;
00342 framesize = ((codec_params & 0x3FF) * 8) + 8;
00343 st->codec->sample_rate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
00344 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00345 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00346 av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
00347 break;
00348 case OMA_CODECID_MP3:
00349 st->need_parsing = AVSTREAM_PARSE_FULL;
00350 framesize = 1024;
00351 break;
00352 case OMA_CODECID_LPCM:
00353
00354 st->codec->channels = 2;
00355 st->codec->sample_rate = 44100;
00356 framesize = 1024;
00357
00358 st->codec->bit_rate = st->codec->sample_rate * 32;
00359 st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
00360 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00361 break;
00362 default:
00363 av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
00364 return -1;
00365 }
00366
00367 st->codec->block_align = framesize;
00368
00369 return 0;
00370 }
00371
00372
00373 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
00374 {
00375 OMAContext *oc = s->priv_data;
00376 int packet_size = s->streams[0]->codec->block_align;
00377 int ret = av_get_packet(s->pb, pkt, packet_size);
00378
00379 if (ret <= 0)
00380 return AVERROR(EIO);
00381
00382 pkt->stream_index = 0;
00383
00384 if (oc->encrypted) {
00385
00386 av_des_crypt(&oc->av_des, pkt->data, pkt->data, (ret >> 3), oc->iv, 1);
00387 }
00388
00389 return ret;
00390 }
00391
00392 static int oma_read_probe(AVProbeData *p)
00393 {
00394 const uint8_t *buf;
00395 unsigned tag_len = 0;
00396
00397 buf = p->buf;
00398
00399 if (p->buf_size < ID3v2_HEADER_SIZE ||
00400 !ff_id3v2_match(buf, ID3v2_EA3_MAGIC) ||
00401 buf[3] != 3 ||
00402 buf[4])
00403 return 0;
00404
00405 tag_len = ff_id3v2_tag_len(buf);
00406
00407
00408 if (p->buf_size < tag_len + 5)
00409
00410 return AVPROBE_SCORE_MAX / 2;
00411
00412 buf += tag_len;
00413
00414 if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
00415 return AVPROBE_SCORE_MAX;
00416 else
00417 return 0;
00418 }
00419
00420 static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00421 {
00422 OMAContext *oc = s->priv_data;
00423
00424 ff_pcm_read_seek(s, stream_index, timestamp, flags);
00425
00426 if (oc->encrypted) {
00427
00428 int64_t pos = avio_tell(s->pb);
00429 if (pos < oc->content_start)
00430 memset(oc->iv, 0, 8);
00431 else {
00432 if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) {
00433 memset(oc->iv, 0, 8);
00434 return -1;
00435 }
00436 }
00437 }
00438
00439 return 0;
00440 }
00441
00442 AVInputFormat ff_oma_demuxer = {
00443 .name = "oma",
00444 .long_name = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
00445 .priv_data_size = sizeof(OMAContext),
00446 .read_probe = oma_read_probe,
00447 .read_header = oma_read_header,
00448 .read_packet = oma_read_packet,
00449 .read_seek = oma_read_seek,
00450 .flags = AVFMT_GENERIC_INDEX,
00451 .extensions = "oma,omg,aa3",
00452 .codec_tag = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
00453 };