From b339f0c6be5f4c15d8136ef180b148550f0071cf Mon Sep 17 00:00:00 2001 From: Benjamin Larsson Date: Sun, 8 Jun 2008 17:12:25 +0000 Subject: Oma demuxer Originally committed as revision 13712 to svn://svn.ffmpeg.org/ffmpeg/trunk --- libavcodec/avcodec.h | 1 + libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/oma.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 193 insertions(+) create mode 100644 libavformat/oma.c diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h index 3772850740..8b6340029f 100644 --- a/libavcodec/avcodec.h +++ b/libavcodec/avcodec.h @@ -298,6 +298,7 @@ enum CodecID { CODEC_ID_WMAVOICE, CODEC_ID_WMAPRO, CODEC_ID_WMALOSSLESS, + CODEC_ID_ATRAC3P, /* subtitle codecs */ CODEC_ID_DVD_SUBTITLE= 0x17000, diff --git a/libavformat/Makefile b/libavformat/Makefile index 378fd9d1c2..07bfcc33f1 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -117,6 +117,7 @@ OBJS-$(CONFIG_OGG_DEMUXER) += oggdec.o \ oggparsevorbis.o \ riff.o OBJS-$(CONFIG_OGG_MUXER) += oggenc.o +OBJS-$(CONFIG_OMA_DEMUXER) += oma.o OBJS-$(CONFIG_PCM_ALAW_DEMUXER) += raw.o OBJS-$(CONFIG_PCM_ALAW_MUXER) += raw.o OBJS-$(CONFIG_PCM_MULAW_DEMUXER) += raw.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 4a3b5f1a42..bcf14bd57c 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -129,6 +129,7 @@ void av_register_all(void) REGISTER_MUXDEMUX (NUT, nut); REGISTER_DEMUXER (NUV, nuv); REGISTER_MUXDEMUX (OGG, ogg); + REGISTER_DEMUXER (OMA, oma); REGISTER_MUXDEMUX (PCM_ALAW, pcm_alaw); REGISTER_MUXDEMUX (PCM_MULAW, pcm_mulaw); REGISTER_MUXDEMUX (PCM_S16BE, pcm_s16be); diff --git a/libavformat/oma.c b/libavformat/oma.c new file mode 100644 index 0000000000..dcfa1d06b8 --- /dev/null +++ b/libavformat/oma.c @@ -0,0 +1,190 @@ +/* + * Sony OpenMG (OMA) demuxer + * + * Copyright (c) 2008 Maxim Poliakovski + * 2008 Benjamin Larsson + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file oma.c + * This is a demuxer for Sony OpenMG Music files + * + * Known file extensions: ".oma", "aa3" + * The format of such files consists of three parts: + * - "ea3" header carrying overall info and metadata. + * - "EA3" header is a Sony-specific header containing information about + * the OpenMG file: codec type (usually ATRAC, can also be MP3 or WMA), + * codec specific info (packet size, sample rate, channels and so on) + * and DRM related info (file encryption, content id). + * - Sound data organized in packets follow the EA3 header + * (can be encrypted using the Sony DRM!). + * + * LIMITATIONS: This version supports only plain (unencrypted) OMA files. + * If any DRM-protected (encrypted) file is encountered you will get the + * corresponding error message. Try to remove the encryption using any + * Sony software (for example SonicStage). + * CODEC SUPPORT: Only ATRAC3 codec is currently supported! + */ + +#include "avformat.h" +#include "libavutil/intreadwrite.h" +#include "raw.h" +#include "riff.h" + +#define EA3_HEADER_SIZE 96 + +enum { + OMA_CODECID_ATRAC3 = 0, + OMA_CODECID_ATRAC3P = 1, +}; + +static const AVCodecTag codec_oma_tags[] = { + { CODEC_ID_ATRAC3, OMA_CODECID_ATRAC3 }, + { CODEC_ID_ATRAC3P, OMA_CODECID_ATRAC3P }, +}; + +static int oma_read_header(AVFormatContext *s, + AVFormatParameters *ap) +{ + static const uint16_t srate_tab[6] = {320,441,480,882,960,0}; + int ret, ea3_taglen, EA3_pos, framesize, jsflag, samplerate, channel_id; + uint32_t codec_params; + int16_t eid; + uint8_t buf[EA3_HEADER_SIZE]; + uint8_t *edata; + AVStream *st; + + ret = get_buffer(s->pb, buf, 10); + if (ret != 10) + return -1; + + ea3_taglen = ((buf[6] & 0x7f) << 21) | ((buf[7] & 0x7f) << 14) | ((buf[8] & 0x7f) << 7) | (buf[9] & 0x7f); + + EA3_pos = ea3_taglen + 10; + if (buf[5] & 0x10) + EA3_pos += 10; + + url_fseek(s->pb, EA3_pos, SEEK_SET); + ret = get_buffer(s->pb, buf, EA3_HEADER_SIZE); + if (ret != EA3_HEADER_SIZE) + return -1; + + if (memcmp(buf, (uint8_t[]){'E', 'A', '3'},3) || buf[4] != 0 || buf[5] != EA3_HEADER_SIZE) { + av_log(s, AV_LOG_ERROR, "Couldn't find the EA3 header !\n"); + return -1; + } + + eid = AV_RB16(&buf[6]); + if (eid != -1 && eid != -128) { + av_log(s, AV_LOG_ERROR, "Encrypted file! Eid: %d\n", eid); + return -1; + } + + codec_params = AV_RB24(&buf[33]); + channel_id = (codec_params >> 10) & 7; + samplerate = srate_tab[(codec_params >> 13) & 7]*100; + + st = av_new_stream(s, 0); + if (!st) + return AVERROR(ENOMEM); + + switch (buf[32]) { + case OMA_CODECID_ATRAC3: + if (samplerate != 44100) + av_log(s, AV_LOG_ERROR, "Unsupported sample rate, send sample file to developers: %d\n", samplerate); + + framesize = (codec_params & 0x3FF) * 8; + jsflag = (codec_params >> 17) & 1; /* get stereo coding mode, 1 for joint-stereo */ + channel_id = 2; + + /* fake the atrac3 extradata (wav format, makes stream copy to wav work) */ + st->codec->extradata_size = 14; + edata = av_mallocz(14 + FF_INPUT_BUFFER_PADDING_SIZE); + if (!edata) + return AVERROR(ENOMEM); + + st->codec->extradata = edata; + AV_WL16(&edata[0], 1); // always 1 + AV_WL32(&edata[2], samplerate); // samples rate + AV_WL16(&edata[6], jsflag); // coding mode + AV_WL16(&edata[8], jsflag); // coding mode + AV_WL16(&edata[10], 1); // always 1 + // AV_WL16(&edata[12], 0); // always 0 + break; + case OMA_CODECID_ATRAC3P: + framesize = ((codec_params & 0x3FF) * 8) + 8; + av_log(s, AV_LOG_ERROR, "Unsupported codec ATRAC3+!\n"); + break; + default: + av_log(s, AV_LOG_ERROR, "Unsupported codec %d!\n",buf[32]); + return -1; + break; + } + + st->codec->codec_type = CODEC_TYPE_AUDIO; + st->codec->codec_tag = buf[32]; + st->codec->codec_id = codec_get_id(codec_oma_tags, st->codec->codec_tag); + st->codec->channels = channel_id; + st->codec->sample_rate = samplerate; + st->codec->bit_rate = samplerate * framesize * 8 / 1024; + st->codec->block_align = framesize; + + st->start_time = 0; + av_set_pts_info(st, 64, 1, st->codec->sample_rate); + + url_fseek(s->pb, EA3_pos + EA3_HEADER_SIZE, SEEK_SET); + + return 0; +} + + +static int oma_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + int ret = av_get_packet(s->pb, pkt, s->streams[0]->codec->block_align); + + pkt->stream_index = 0; + if (ret <= 0) + return AVERROR(EIO); + + return ret; +} + +static int oma_read_probe(AVProbeData *p) +{ + if (!memcmp(p->buf, (uint8_t[]){'e', 'a', '3', 3, 0},5)) + return AVPROBE_SCORE_MAX; + else + return 0; +} + + +AVInputFormat oma_demuxer = { + "oma", + NULL_IF_CONFIG_SMALL("Sony OpenMG audio"), + 0, + oma_read_probe, + oma_read_header, + oma_read_packet, + 0, + pcm_read_seek, + .flags= AVFMT_GENERIC_INDEX, + .extensions = "oma,aa3", + .codec_tag= (const AVCodecTag*[]){codec_oma_tags, 0}, +}; + -- cgit v1.2.3