00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00046 #include "avformat.h"
00047 #include "libavutil/intreadwrite.h"
00048 #include "pcm.h"
00049 #include "riff.h"
00050 #include "id3v2.h"
00051
00052 #define EA3_HEADER_SIZE 96
00053
00054 enum {
00055 OMA_CODECID_ATRAC3 = 0,
00056 OMA_CODECID_ATRAC3P = 1,
00057 OMA_CODECID_MP3 = 3,
00058 OMA_CODECID_LPCM = 4,
00059 OMA_CODECID_WMA = 5,
00060 };
00061
00062 static const AVCodecTag codec_oma_tags[] = {
00063 { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 },
00064 { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P },
00065 { CODEC_ID_MP3, OMA_CODECID_MP3 },
00066 };
00067
00068 #define ID3v2_EA3_MAGIC "ea3"
00069
00070 static int oma_read_header(AVFormatContext *s,
00071 AVFormatParameters *ap)
00072 {
00073 static const uint16_t srate_tab[6] = {320,441,480,882,960,0};
00074 int ret, framesize, jsflag, samplerate;
00075 uint32_t codec_params;
00076 int16_t eid;
00077 uint8_t buf[EA3_HEADER_SIZE];
00078 uint8_t *edata;
00079 AVStream *st;
00080
00081 ff_id3v2_read(s, ID3v2_EA3_MAGIC);
00082 ret = avio_read(s->pb, buf, EA3_HEADER_SIZE);
00083
00084 if (memcmp(buf, ((const uint8_t[]){'E', 'A', '3'}),3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
00085 av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
00086 return -1;
00087 }
00088
00089 eid = AV_RB16(&buf[6]);
00090 if (eid != -1 && eid != -128) {
00091 av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
00092 return -1;
00093 }
00094
00095 codec_params = AV_RB24(&buf[33]);
00096
00097 st = av_new_stream(s, 0);
00098 if (!st)
00099 return AVERROR(ENOMEM);
00100
00101 st->start_time = 0;
00102 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00103 st->codec->codec_tag = buf[32];
00104 st->codec->codec_id = ff_codec_get_id(codec_oma_tags, st->codec->codec_tag);
00105
00106 switch (buf[32]) {
00107 case OMA_CODECID_ATRAC3:
00108 samplerate = srate_tab[(codec_params >> 13) & 7]*100;
00109 if (samplerate != 44100)
00110 av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate);
00111
00112 framesize = (codec_params & 0x3FF) * 8;
00113 jsflag = (codec_params >> 17) & 1;
00114 st->codec->channels = 2;
00115 st->codec->sample_rate = samplerate;
00116 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00117
00118
00119 st->codec->extradata_size = 14;
00120 edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
00121 if (!edata)
00122 return AVERROR(ENOMEM);
00123
00124 st->codec->extradata = edata;
00125 AV_WL16(&edata[0], 1);
00126 AV_WL32(&edata[2], samplerate);
00127 AV_WL16(&edata[6], jsflag);
00128 AV_WL16(&edata[8], jsflag);
00129 AV_WL16(&edata[10], 1);
00130
00131
00132 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00133 break;
00134 case OMA_CODECID_ATRAC3P:
00135 st->codec->channels = (codec_params >> 10) & 7;
00136 framesize = ((codec_params & 0x3FF) * 8) + 8;
00137 st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
00138 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00139 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00140 av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
00141 break;
00142 case OMA_CODECID_MP3:
00143 st->need_parsing = AVSTREAM_PARSE_FULL;
00144 framesize = 1024;
00145 break;
00146 default:
00147 av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
00148 return -1;
00149 break;
00150 }
00151
00152 st->codec->block_align = framesize;
00153
00154 return 0;
00155 }
00156
00157
00158 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
00159 {
00160 int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
00161
00162 pkt->stream_index = 0;
00163 if (ret <= 0)
00164 return AVERROR(EIO);
00165
00166 return ret;
00167 }
00168
00169 static int oma_read_probe(AVProbeData *p)
00170 {
00171 const uint8_t *buf;
00172 unsigned tag_len = 0;
00173
00174 buf = p->buf;
00175
00176 if (ff_id3v2_match(buf, ID3v2_EA3_MAGIC) && buf[3] == 3 && !buf[4])
00177 tag_len = ff_id3v2_tag_len(buf);
00178
00179
00180 if (p->buf_size < tag_len + 5)
00181 return 0;
00182
00183 buf += tag_len;
00184
00185 if (!memcmp(buf, "EA3", 3) && !buf[4] && buf[5] == EA3_HEADER_SIZE)
00186 return AVPROBE_SCORE_MAX;
00187 else
00188 return 0;
00189 }
00190
00191
00192 AVInputFormat ff_oma_demuxer = {
00193 "oma",
00194 NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
00195 0,
00196 oma_read_probe,
00197 oma_read_header,
00198 oma_read_packet,
00199 0,
00200 pcm_read_seek,
00201 .flags= AVFMT_GENERIC_INDEX,
00202 .extensions = "oma,aa3",
00203 .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
00204 };
00205