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

libavformat/oma.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  *
00007  * This file is part of FFmpeg.
00008  *
00009  * FFmpeg is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * FFmpeg is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with FFmpeg; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00022  */
00023 
00045 #include "avformat.h"
00046 #include "libavutil/intreadwrite.h"
00047 #include "raw.h"
00048 #include "riff.h"
00049 
00050 #define EA3_HEADER_SIZE 96
00051 
00052 enum {
00053     OMA_CODECID_ATRAC3  = 0,
00054     OMA_CODECID_ATRAC3P = 1,
00055     OMA_CODECID_MP3     = 3,
00056     OMA_CODECID_LPCM    = 4,
00057     OMA_CODECID_WMA     = 5,
00058 };
00059 
00060 static const AVCodecTag codec_oma_tags[] = {
00061     { CODEC_ID_ATRAC3,  OMA_CODECID_ATRAC3 },
00062     { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P },
00063     { CODEC_ID_MP3,     OMA_CODECID_MP3 },
00064 };
00065 
00066 static int oma_read_header(AVFormatContext *s,
00067                            AVFormatParameters *ap)
00068 {
00069     static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
00070     int     ret, ea3_taglen, EA3_pos, framesize, jsflag, samplerate;
00071     uint32_t codec_params;
00072     int16_t eid;
00073     uint8_t buf[EA3_HEADER_SIZE];
00074     uint8_t *edata;
00075     AVStream *st;
00076 
00077     ret = get_buffer(s->pb, buf, 10);
00078     if (ret != 10)
00079         return -1;
00080 
00081     if(!memcmp(buf, "ea3", 3)) {
00082         ea3_taglen = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f);
00083 
00084         EA3_pos = ea3_taglen + 10;
00085         if (buf[5] & 0x10)
00086             EA3_pos += 10;
00087 
00088         url_fseek(s->pb, EA3_pos, SEEK_SET);
00089         ret = get_buffer(s->pb, buf, EA3_HEADER_SIZE);
00090         if (ret != EA3_HEADER_SIZE)
00091             return -1;
00092     } else {
00093         ret = get_buffer(s->pb, buf + 10, EA3_HEADER_SIZE - 10);
00094         EA3_pos = 0;
00095     }
00096 
00097     if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
00098         av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
00099         return -1;
00100     }
00101 
00102     eid = AV_RB16(&buf[6]);
00103     if (eid != -1 && eid != -128) {
00104         av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
00105         return -1;
00106     }
00107 
00108     codec_params = AV_RB24(&buf[33]);
00109 
00110     st = av_new_stream(s, 0);
00111     if (!st)
00112         return AVERROR(ENOMEM);
00113 
00114     st->start_time = 0;
00115     st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
00116     st->codec->codec_tag   = buf[32];
00117     st->codec->codec_id    = ff_codec_get_id(codec_oma_tags, st->codec->codec_tag);
00118 
00119     switch (buf[32]) {
00120         case OMA_CODECID_ATRAC3:
00121             samplerate = srate_tab[(codec_params >> 13) & 7]*100;
00122             if (samplerate != 44100)
00123                 av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate);
00124 
00125             framesize = (codec_params & 0x3FF) * 8;
00126             jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */
00127             st->codec->channels    = 2;
00128             st->codec->sample_rate = samplerate;
00129             st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
00130 
00131             /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */
00132             st->codec->extradata_size = 14;
00133             edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
00134             if (!edata)
00135                 return AVERROR(ENOMEM);
00136 
00137             st->codec->extradata = edata;
00138             AV_WL16(&edata[0],  1);             // always 1
00139             AV_WL32(&edata[2],  samplerate);    // samples rate
00140             AV_WL16(&edata[6],  jsflag);        // coding mode
00141             AV_WL16(&edata[8],  jsflag);        // coding mode
00142             AV_WL16(&edata[10], 1);             // always 1
00143             // AV_WL16(&edata[12], 0);          // always 0
00144 
00145             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00146             break;
00147         case OMA_CODECID_ATRAC3P:
00148             st->codec->channels = (codec_params >> 10) & 7;
00149             framesize = ((codec_params & 0x3FF) * 8) + 8;
00150             st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
00151             st->codec->bit_rate    = st->codec->sample_rate * framesize * 8 / 1024;
00152             av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00153             av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
00154             break;
00155         case OMA_CODECID_MP3:
00156             st->need_parsing = AVSTREAM_PARSE_FULL;
00157             framesize = 1024;
00158             break;
00159         default:
00160             av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
00161             return -1;
00162             break;
00163     }
00164 
00165     st->codec->block_align = framesize;
00166     url_fseek(s->pb, EA3_pos + EA3_HEADER_SIZE, SEEK_SET);
00167 
00168     return 0;
00169 }
00170 
00171 
00172 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
00173 {
00174     int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
00175 
00176     pkt->stream_index = 0;
00177     if (ret <= 0)
00178         return AVERROR(EIO);
00179 
00180     return ret;
00181 }
00182 
00183 static int oma_read_probe(AVProbeData *p)
00184 {
00185     if (!memcmp(p->buf, ((const uint8_t[]){'e', 'a', '3', 3, 0}), 5) ||
00186         (!memcmp(p->buf, "EA3", 3) &&
00187          !p->buf[4] && p->buf[5] == EA3_HEADER_SIZE))
00188         return AVPROBE_SCORE_MAX;
00189     else
00190         return 0;
00191 }
00192 
00193 
00194 AVInputFormat oma_demuxer = {
00195     "oma",
00196     NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
00197     0,
00198     oma_read_probe,
00199     oma_read_header,
00200     oma_read_packet,
00201     0,
00202     pcm_read_seek,
00203     .flags= AVFMT_GENERIC_INDEX,
00204     .extensions = "oma,aa3",
00205     .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
00206 };
00207 

Generated on Fri Sep 16 2011 17:17:50 for FFmpeg by  doxygen 1.7.1