00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 ea3_taglen = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f);
00082
00083 EA3_pos = ea3_taglen + 10;
00084 if (buf[5] & 0x10)
00085 EA3_pos += 10;
00086
00087 url_fseek(s->pb, EA3_pos, SEEK_SET);
00088 ret = get_buffer(s->pb, buf, EA3_HEADER_SIZE);
00089 if (ret != EA3_HEADER_SIZE)
00090 return -1;
00091
00092 if (memcmp(buf, (const uint8_t[]){'E', 'A', '3'},3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) {
00093 av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n");
00094 return -1;
00095 }
00096
00097 eid = AV_RB16(&buf[6]);
00098 if (eid != -1 && eid != -128) {
00099 av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid);
00100 return -1;
00101 }
00102
00103 codec_params = AV_RB24(&buf[33]);
00104
00105 st = av_new_stream(s, 0);
00106 if (!st)
00107 return AVERROR(ENOMEM);
00108
00109 st->start_time = 0;
00110 st->codec->codec_type = CODEC_TYPE_AUDIO;
00111 st->codec->codec_tag = buf[32];
00112 st->codec->codec_id = codec_get_id(codec_oma_tags, st->codec->codec_tag);
00113
00114 switch (buf[32]) {
00115 case OMA_CODECID_ATRAC3:
00116 samplerate = srate_tab[(codec_params >> 13) & 7]*100;
00117 if (samplerate != 44100)
00118 av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate);
00119
00120 framesize = (codec_params & 0x3FF) * 8;
00121 jsflag = (codec_params >> 17) & 1;
00122 st->codec->channels = 2;
00123 st->codec->sample_rate = samplerate;
00124 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00125
00126
00127 st->codec->extradata_size = 14;
00128 edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE);
00129 if (!edata)
00130 return AVERROR(ENOMEM);
00131
00132 st->codec->extradata = edata;
00133 AV_WL16(&edata[0], 1);
00134 AV_WL32(&edata[2], samplerate);
00135 AV_WL16(&edata[6], jsflag);
00136 AV_WL16(&edata[8], jsflag);
00137 AV_WL16(&edata[10], 1);
00138
00139
00140 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00141 break;
00142 case OMA_CODECID_ATRAC3P:
00143 st->codec->channels = (codec_params >> 10) & 7;
00144 framesize = ((codec_params & 0x3FF) * 8) + 8;
00145 st->codec->sample_rate = srate_tab[(codec_params >> 13) & 7]*100;
00146 st->codec->bit_rate = st->codec->sample_rate * framesize * 8 / 1024;
00147 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
00148 av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n");
00149 break;
00150 case OMA_CODECID_MP3:
00151 st->need_parsing = AVSTREAM_PARSE_FULL;
00152 framesize = 1024;
00153 break;
00154 default:
00155 av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]);
00156 return -1;
00157 break;
00158 }
00159
00160 st->codec->block_align = framesize;
00161 url_fseek(s->pb, EA3_pos + EA3_HEADER_SIZE, SEEK_SET);
00162
00163 return 0;
00164 }
00165
00166
00167 static int oma_read_packet(AVFormatContext *s, AVPacket *pkt)
00168 {
00169 int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align);
00170
00171 pkt->stream_index = 0;
00172 if (ret <= 0)
00173 return AVERROR(EIO);
00174
00175 return ret;
00176 }
00177
00178 static int oma_read_probe(AVProbeData *p)
00179 {
00180 if (!memcmp(p->buf, (const uint8_t[]){'e', 'a', '3', 3, 0},5))
00181 return AVPROBE_SCORE_MAX;
00182 else
00183 return 0;
00184 }
00185
00186
00187 AVInputFormat oma_demuxer = {
00188 "oma",
00189 NULL_IF_CONFIG_SMALL("Sony OpenMG audio"),
00190 0,
00191 oma_read_probe,
00192 oma_read_header,
00193 oma_read_packet,
00194 0,
00195 pcm_read_seek,
00196 .flags= AVFMT_GENERIC_INDEX,
00197 .extensions = "oma,aa3",
00198 .codec_tag= (const AVCodecTag* const []){codec_oma_tags, 0},
00199 };
00200