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 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; /* get stereo coding mode, 1 for joint-stereo */ 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 /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */ 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); // always 1 00134 AV_WL32(&edata[2], samplerate); // samples rate 00135 AV_WL16(&edata[6], jsflag); // coding mode 00136 AV_WL16(&edata[8], jsflag); // coding mode 00137 AV_WL16(&edata[10], 1); // always 1 00138 // AV_WL16(&edata[12], 0); // always 0 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