• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavformat/omadec.c

Go to the documentation of this file.
00001 /*
00002  * Sony OpenMG (OMA) demuxer
00003  *
00004  * Copyright (c) 2008 Maxim Poliakovski
00005  *               2008 Benjamin Larsson
00006  *               2011 David Goldwich
00007  *
00008  * This file is part of Libav.
00009  *
00010  * Libav is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * Libav is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with Libav; if not, write to the Free Software
00022  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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     /* use first 64 bits in the third round again */
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     /* m_val */
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     /* s_val */
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     /* sm_val */
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, 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     pos += 44 + taglen;
00163 
00164     av_des_init(&av_des, n_val, 192, 1);
00165     while (datalen-- > 0) {
00166         av_des_crypt(&av_des, oc->r_val, &enc_header[pos], 2, NULL, 1);
00167         kset(s, oc->r_val, NULL, 16);
00168         if (!rprobe(s, enc_header, oc->r_val))
00169             return 0;
00170         pos += 16;
00171     }
00172 
00173     return -1;
00174 }
00175 
00176 static int decrypt_init(AVFormatContext *s, ID3v2ExtraMeta *em, uint8_t *header)
00177 {
00178     OMAContext *oc = s->priv_data;
00179     ID3v2ExtraMetaGEOB *geob = NULL;
00180     uint8_t *gdata;
00181 
00182     oc->encrypted = 1;
00183     av_log(s, AV_LOG_INFO, "File is encrypted\n");
00184 
00185     /* find GEOB metadata */
00186     while (em) {
00187         if (!strcmp(em->tag, "GEOB") &&
00188             (geob = em->data) &&
00189             (!strcmp(geob->description, "OMG_LSI") ||
00190              !strcmp(geob->description, "OMG_BKLSI"))) {
00191             break;
00192         }
00193         em = em->next;
00194     }
00195     if (!em) {
00196         av_log(s, AV_LOG_ERROR, "No encryption header found\n");
00197         return -1;
00198     }
00199 
00200     if (geob->datasize < 64) {
00201         av_log(s, AV_LOG_ERROR, "Invalid GEOB data size: %u\n", geob->datasize);
00202         return -1;
00203     }
00204 
00205     gdata = geob->data;
00206 
00207     if (AV_RB16(gdata) != 1)
00208         av_log(s, AV_LOG_WARNING, "Unknown version in encryption header\n");
00209 
00210     oc->k_size = AV_RB16(&gdata[2]);
00211     oc->e_size = AV_RB16(&gdata[4]);
00212     oc->i_size = AV_RB16(&gdata[6]);
00213     oc->s_size = AV_RB16(&gdata[8]);
00214 
00215     if (memcmp(&gdata[OMA_ENC_HEADER_SIZE], "KEYRING     ", 12)) {
00216         av_log(s, AV_LOG_ERROR, "Invalid encryption header\n");
00217         return -1;
00218     }
00219     oc->rid = AV_RB32(&gdata[OMA_ENC_HEADER_SIZE + 28]);
00220     av_log(s, AV_LOG_DEBUG, "RID: %.8x\n", oc->rid);
00221 
00222     memcpy(oc->iv, &header[0x58], 8);
00223     hex_log(s, AV_LOG_DEBUG, "IV", oc->iv, 8);
00224 
00225     hex_log(s, AV_LOG_DEBUG, "CBC-MAC", &gdata[OMA_ENC_HEADER_SIZE+oc->k_size+oc->e_size+oc->i_size], 8);
00226 
00227     if (s->keylen > 0) {
00228         kset(s, s->key, s->key, s->keylen);
00229     }
00230     if (!memcmp(oc->r_val, (const uint8_t[8]){0}, 8) ||
00231         rprobe(s, gdata, oc->r_val) < 0 &&
00232         nprobe(s, gdata, oc->n_val) < 0) {
00233         int i;
00234         for (i = 0; i < FF_ARRAY_ELEMS(leaf_table); i += 2) {
00235             uint8_t buf[16];
00236             AV_WL64(buf, leaf_table[i]);
00237             AV_WL64(&buf[8], leaf_table[i+1]);
00238             kset(s, buf, buf, 16);
00239             if (!rprobe(s, gdata, oc->r_val) || !nprobe(s, gdata, oc->n_val))
00240                 break;
00241         }
00242         if (i >= sizeof(leaf_table)) {
00243             av_log(s, AV_LOG_ERROR, "Invalid key\n");
00244             return -1;
00245         }
00246     }
00247 
00248     /* e_val */
00249     av_des_init(&oc->av_des, oc->m_val, 64, 0);
00250     av_des_crypt(&oc->av_des, oc->e_val, &gdata[OMA_ENC_HEADER_SIZE + 40], 1, NULL, 0);
00251     hex_log(s, AV_LOG_DEBUG, "EK", oc->e_val, 8);
00252 
00253     /* init e_val */
00254     av_des_init(&oc->av_des, oc->e_val, 64, 1);
00255 
00256     return 0;
00257 }
00258 
00259 static int oma_read_header(AVFormatContext *s,
00260                            AVFormatParameters *ap)
00261 {
00262     int     ret, framesize, jsflag, samplerate;
00263     uint32_t codec_params;
00264     int16_t eid;
00265     uint8_t buf[EA3_HEADER_SIZE];
00266     uint8_t *edata;
00267     AVStream *st;
00268     ID3v2ExtraMeta *extra_meta = NULL;
00269     OMAContext *oc = s->priv_data;
00270 
00271     ff_id3v2_read_all(s, ID3v2_EA3_MAGIC, &extra_meta);
00272     ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
00273     if (ret < EA3_HEADER_SIZE)
00274         return -1;
00275 
00276     if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
00277         av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
00278         return -1;
00279     }
00280 
00281     oc->content_start = avio_tell(s->pb);
00282 
00283     /* encrypted file */
00284     eid = AV_RB16(&buf[6]);
00285     if (eid != -1 && eid != -128 && decrypt_init(s, extra_meta, buf) < 0) {
00286         ff_id3v2_free_extra_meta(&extra_meta);
00287         return -1;
00288     }
00289 
00290     ff_id3v2_free_extra_meta(&extra_meta);
00291 
00292     codec_params = AV_RB24(&buf[33]);
00293 
00294     st = avformat_new_stream(s, NULL);
00295     if (!st)
00296         return AVERROR(ENOMEM);
00297 
00298     st->start_time = 0;
00299     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00300     st->codec->codec_tag   = buf[32];
00301     st->codec->codec_id    = ff_codec_get_id(ff_oma_codec_tags, st->codec->codec_tag);
00302 
00303     switch (buf[32]) {
00304         case OMA_CODECID_ATRAC3:
00305             samplerate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
00306             if (samplerate != 44100)
00307                 av_log_ask_for_sample(s, "Unsupported sample rate: %d\n",
00308                                       samplerate);
00309 
00310             framesize = (codec_params & 0x3FF) * 8;
00311             jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
00312             st->codec->channels    = 2;
00313             st->codec->sample_rate = samplerate;
00314             st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
00315 
00316             /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
00317             st->codec->extradata_size = 14;
00318             edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
00319             if (!edata)
00320                 return AVERROR(ENOMEM);
00321 
00322             st->codec->extradata = edata;
00323             AV_WL16(&edata[0],  1);             // always 1
00324             AV_WL32(&edata[2],  samplerate);    // samples rate
00325             AV_WL16(&edata[6],  jsflag);        // coding mode
00326             AV_WL16(&edata[8],  jsflag);        // coding mode
00327             AV_WL16(&edata[10], 1);             // always 1
00328             // AV_WL16(&edata[12], 0);          // always 0
00329 
00330             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00331             break;
00332         case OMA_CODECID_ATRAC3P:
00333             st->codec->channels = (codec_params >> 10) & 7;
00334             framesize = ((codec_params & 0x3FF) * 8) + 8;
00335             st->codec->sample_rate = ff_oma_srate_tab[(codec_params >> 13) & 7]*100;
00336             st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
00337             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00338             av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
00339             break;
00340         case OMA_CODECID_MP3:
00341             st->need_parsing = AVSTREAM_PARSE_FULL;
00342             framesize = 1024;
00343             break;
00344         case OMA_CODECID_LPCM:
00345             /* PCM 44.1 kHz 16 bit stereo big-endian */
00346             st->codec->channels = 2;
00347             st->codec->sample_rate = 44100;
00348             framesize = 1024;
00349             /* bit rate = sample rate x PCM block align (= 4) x 8 */
00350             st->codec->bit_rate = st->codec->sample_rate * 32;
00351             st->codec->bits_per_coded_sample = av_get_bits_per_sample(st->codec->codec_id);
00352             avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
00353             break;
00354         default:
00355             av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
00356             return -1;
00357     }
00358 
00359     st->codec->block_align = framesize;
00360 
00361     return 0;
00362 }
00363 
00364 
00365 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
00366 {
00367     OMAContext *oc = s->priv_data;
00368     int packet_size = s->streams[0]->codec->block_align;
00369     int ret = av_get_packet(s->pb, pkt, packet_size);
00370 
00371     if (ret <= 0)
00372         return AVERROR(EIO);
00373 
00374     pkt->stream_index = 0;
00375 
00376     if (oc->encrypted) {
00377         /* previous unencrypted block saved in IV for the next packet (CBC mode) */
00378         av_des_crypt(&oc->av_des, pkt->data, pkt->data, (packet_size >> 3), oc->iv, 1);
00379     }
00380 
00381     return ret;
00382 }
00383 
00384 static int oma_read_probe(AVProbeData *p)
00385 {
00386     const uint8_t *buf;
00387     unsigned tag_len = 0;
00388 
00389     buf = p->buf;
00390 
00391     if (p->buf_size < ID3v2_HEADER_SIZE ||
00392         !ff_id3v2_match(buf, ID3v2_EA3_MAGIC) ||
00393         buf[3] != 3 || // version must be 3
00394         buf[4]) // flags byte zero
00395         return 0;
00396 
00397     tag_len = ff_id3v2_tag_len(buf);
00398 
00399     /* This check cannot overflow as tag_len has at most 28 bits */
00400     if (p->buf_size < tag_len + 5)
00401         /* EA3 header comes late, might be outside of the probe buffer */
00402         return AVPROBE_SCORE_MAX / 2;
00403 
00404     buf += tag_len;
00405 
00406     if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
00407         return AVPROBE_SCORE_MAX;
00408     else
00409         return 0;
00410 }
00411 
00412 static int oma_read_seek(struct AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
00413 {
00414     OMAContext *oc = s->priv_data;
00415 
00416     pcm_read_seek(s, stream_index, timestamp, flags);
00417 
00418     if (oc->encrypted) {
00419         /* readjust IV for CBC */
00420         int64_t pos = avio_tell(s->pb);
00421         if (pos < oc->content_start)
00422             memset(oc->iv, 0, 8);
00423         else {
00424             if (avio_seek(s->pb, -8, SEEK_CUR) < 0 || avio_read(s->pb, oc->iv, 8) < 8) {
00425                 memset(oc->iv, 0, 8);
00426                 return -1;
00427             }
00428         }
00429     }
00430 
00431     return 0;
00432 }
00433 
00434 AVInputFormat ff_oma_demuxer = {
00435     .name           = "oma",
00436     .long_name      = NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
00437     .priv_data_size = sizeof(OMAContext),
00438     .read_probe     = oma_read_probe,
00439     .read_header    = oma_read_header,
00440     .read_packet    = oma_read_packet,
00441     .read_seek      = oma_read_seek,
00442     .flags          = AVFMT_GENERIC_INDEX,
00443     .extensions     = "oma,omg,aa3",
00444     .codec_tag      = (const AVCodecTag* const []){ff_oma_codec_tags, 0},
00445 };
00446 
Generated on Thu Jan 24 2013 17:08:55 for Libav by doxygen 1.7.1