2013-05-10 21:52:10 +02:00
|
|
|
/*
|
|
|
|
* RSD demuxer
|
|
|
|
* Copyright (c) 2013 James Almer
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavcodec/bytestream.h"
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "avio.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
static const AVCodecTag rsd_tags[] = {
|
2015-10-18 13:45:10 +02:00
|
|
|
{ AV_CODEC_ID_ADPCM_PSX, MKTAG('V','A','G',' ') },
|
2015-11-19 20:00:16 +01:00
|
|
|
{ AV_CODEC_ID_ADPCM_THP_LE, MKTAG('G','A','D','P') },
|
2015-10-16 00:09:02 +02:00
|
|
|
{ AV_CODEC_ID_ADPCM_THP, MKTAG('W','A','D','P') },
|
2013-05-10 21:53:50 +02:00
|
|
|
{ AV_CODEC_ID_ADPCM_IMA_RAD, MKTAG('R','A','D','P') },
|
2015-10-11 18:33:57 +02:00
|
|
|
{ AV_CODEC_ID_ADPCM_IMA_WAV, MKTAG('X','A','D','P') },
|
2013-05-10 21:52:10 +02:00
|
|
|
{ AV_CODEC_ID_PCM_S16BE, MKTAG('P','C','M','B') },
|
|
|
|
{ AV_CODEC_ID_PCM_S16LE, MKTAG('P','C','M',' ') },
|
2015-11-10 21:29:25 +01:00
|
|
|
{ AV_CODEC_ID_XMA2, MKTAG('X','M','A',' ') },
|
2013-05-10 21:52:10 +02:00
|
|
|
{ AV_CODEC_ID_NONE, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint32_t rsd_unsupported_tags[] = {
|
|
|
|
MKTAG('O','G','G',' '),
|
|
|
|
};
|
|
|
|
|
|
|
|
static int rsd_probe(AVProbeData *p)
|
|
|
|
{
|
2013-11-06 11:48:49 +01:00
|
|
|
if (memcmp(p->buf, "RSD", 3) || p->buf[3] - '0' < 2 || p->buf[3] - '0' > 6)
|
|
|
|
return 0;
|
|
|
|
if (AV_RL32(p->buf + 8) > 256 || !AV_RL32(p->buf + 8))
|
2013-11-10 13:10:23 +01:00
|
|
|
return AVPROBE_SCORE_MAX / 8;
|
2013-11-06 11:48:49 +01:00
|
|
|
if (AV_RL32(p->buf + 16) > 8*48000 || !AV_RL32(p->buf + 16))
|
2013-11-10 13:10:23 +01:00
|
|
|
return AVPROBE_SCORE_MAX / 8;
|
2013-11-06 11:51:16 +01:00
|
|
|
return AVPROBE_SCORE_MAX;
|
2013-05-10 21:52:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rsd_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
AVIOContext *pb = s->pb;
|
2015-10-16 21:09:10 +02:00
|
|
|
int i, ret, version, start = 0x800;
|
2016-04-10 21:58:15 +02:00
|
|
|
AVCodecParameters *par;
|
2013-05-10 21:52:10 +02:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
avio_skip(pb, 3); // "RSD"
|
|
|
|
version = avio_r8(pb) - '0';
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
par = st->codecpar;
|
|
|
|
par->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
par->codec_tag = avio_rl32(pb);
|
|
|
|
par->codec_id = ff_codec_get_id(rsd_tags, par->codec_tag);
|
|
|
|
if (!par->codec_id) {
|
2014-12-20 00:17:43 +01:00
|
|
|
char tag_buf[32];
|
2013-05-10 21:52:10 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
av_get_codec_tag_string(tag_buf, sizeof(tag_buf), par->codec_tag);
|
2013-05-10 21:52:10 +02:00
|
|
|
for (i=0; i < FF_ARRAY_ELEMS(rsd_unsupported_tags); i++) {
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_tag == rsd_unsupported_tags[i]) {
|
2013-05-10 21:52:10 +02:00
|
|
|
avpriv_request_sample(s, "Codec tag: %s", tag_buf);
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
av_log(s, AV_LOG_ERROR, "Unknown codec tag: %s\n", tag_buf);
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
par->channels = avio_rl32(pb);
|
|
|
|
if (!par->channels)
|
2013-05-10 21:52:10 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(pb, 4); // Bit depth
|
2016-04-10 21:58:15 +02:00
|
|
|
par->sample_rate = avio_rl32(pb);
|
|
|
|
if (!par->sample_rate)
|
2013-05-10 21:52:10 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(pb, 4); // Unknown
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
switch (par->codec_id) {
|
2015-11-10 21:29:25 +01:00
|
|
|
case AV_CODEC_ID_XMA2:
|
2016-04-10 21:58:15 +02:00
|
|
|
par->block_align = 2048;
|
|
|
|
ff_alloc_extradata(par, 34);
|
|
|
|
if (!par->extradata)
|
2015-11-09 10:31:24 +01:00
|
|
|
return AVERROR(ENOMEM);
|
2016-04-10 21:58:15 +02:00
|
|
|
memset(par->extradata, 0, 34);
|
2015-11-09 10:31:24 +01:00
|
|
|
break;
|
2015-10-18 13:45:10 +02:00
|
|
|
case AV_CODEC_ID_ADPCM_PSX:
|
2016-04-10 21:58:15 +02:00
|
|
|
par->block_align = 16 * par->channels;
|
2015-10-18 13:45:10 +02:00
|
|
|
if (pb->seekable)
|
2016-04-10 21:58:15 +02:00
|
|
|
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start);
|
2015-10-18 13:45:10 +02:00
|
|
|
break;
|
2013-05-10 21:53:50 +02:00
|
|
|
case AV_CODEC_ID_ADPCM_IMA_RAD:
|
2016-04-10 21:58:15 +02:00
|
|
|
par->block_align = 20 * par->channels;
|
2013-05-10 21:53:50 +02:00
|
|
|
if (pb->seekable)
|
2016-04-10 21:58:15 +02:00
|
|
|
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start);
|
2013-05-10 21:53:50 +02:00
|
|
|
break;
|
2015-10-11 18:33:57 +02:00
|
|
|
case AV_CODEC_ID_ADPCM_IMA_WAV:
|
|
|
|
if (version == 2)
|
|
|
|
start = avio_rl32(pb);
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
par->bits_per_coded_sample = 4;
|
|
|
|
par->block_align = 36 * par->channels;
|
2015-10-11 18:33:57 +02:00
|
|
|
if (pb->seekable)
|
2016-04-10 21:58:15 +02:00
|
|
|
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start);
|
2015-10-11 18:33:57 +02:00
|
|
|
break;
|
2015-11-19 20:00:16 +01:00
|
|
|
case AV_CODEC_ID_ADPCM_THP_LE:
|
2015-11-19 20:06:01 +01:00
|
|
|
/* RSD3GADP is mono, so only alloc enough memory
|
|
|
|
to store the coeff table for a single channel. */
|
2013-05-10 21:52:10 +02:00
|
|
|
|
2015-11-19 20:06:01 +01:00
|
|
|
start = avio_rl32(pb);
|
2015-10-16 00:09:02 +02:00
|
|
|
|
2016-04-14 18:21:08 +02:00
|
|
|
if ((ret = ff_get_extradata(s, par, s->pb, 32)) < 0)
|
2015-11-19 20:06:01 +01:00
|
|
|
return ret;
|
2015-11-19 20:00:16 +01:00
|
|
|
if (pb->seekable)
|
2016-04-10 21:58:15 +02:00
|
|
|
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start);
|
2015-11-19 20:00:16 +01:00
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_ADPCM_THP:
|
2016-04-10 21:58:15 +02:00
|
|
|
par->block_align = 8 * par->channels;
|
2015-11-19 20:06:01 +01:00
|
|
|
avio_skip(s->pb, 0x1A4 - avio_tell(s->pb));
|
2013-05-10 21:52:10 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if ((ret = ff_alloc_extradata(st->codecpar, 32 * par->channels)) < 0)
|
2015-11-19 20:06:01 +01:00
|
|
|
return ret;
|
2013-05-10 21:52:10 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
for (i = 0; i < par->channels; i++) {
|
|
|
|
avio_read(s->pb, st->codecpar->extradata + 32 * i, 32);
|
2015-11-19 20:06:01 +01:00
|
|
|
avio_skip(s->pb, 8);
|
|
|
|
}
|
2013-05-10 21:52:10 +02:00
|
|
|
if (pb->seekable)
|
2016-04-10 21:58:15 +02:00
|
|
|
st->duration = (avio_size(pb) - start) / (8 * par->channels) * 14;
|
2013-05-10 21:52:10 +02:00
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_PCM_S16LE:
|
|
|
|
case AV_CODEC_ID_PCM_S16BE:
|
|
|
|
if (version != 4)
|
|
|
|
start = avio_rl32(pb);
|
|
|
|
|
|
|
|
if (pb->seekable)
|
2016-04-10 21:58:15 +02:00
|
|
|
st->duration = (avio_size(pb) - start) / 2 / par->channels;
|
2013-05-10 21:52:10 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(pb, start - avio_tell(pb));
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_XMA2) {
|
2015-11-09 10:31:24 +01:00
|
|
|
avio_skip(pb, avio_rb32(pb) + avio_rb32(pb));
|
|
|
|
st->duration = avio_rb32(pb);
|
|
|
|
}
|
2013-05-10 21:52:10 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
avpriv_set_pts_info(st, 64, 1, par->sample_rate);
|
2013-05-10 21:52:10 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsd_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2016-04-10 21:58:15 +02:00
|
|
|
AVCodecParameters *par = s->streams[0]->codecpar;
|
2013-05-10 21:52:10 +02:00
|
|
|
int ret, size = 1024;
|
2015-11-20 20:51:22 +01:00
|
|
|
int64_t pos;
|
2013-05-10 21:52:10 +02:00
|
|
|
|
2014-08-07 22:12:41 +02:00
|
|
|
if (avio_feof(s->pb))
|
2013-05-10 21:52:10 +02:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2015-11-20 20:51:22 +01:00
|
|
|
pos = avio_tell(s->pb);
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_ADPCM_IMA_RAD ||
|
|
|
|
par->codec_id == AV_CODEC_ID_ADPCM_PSX ||
|
|
|
|
par->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
|
|
|
|
par->codec_id == AV_CODEC_ID_XMA2) {
|
|
|
|
ret = av_get_packet(s->pb, pkt, par->block_align);
|
|
|
|
} else if (par->codec_tag == MKTAG('W','A','D','P') &&
|
|
|
|
par->channels > 1) {
|
2015-10-16 00:09:02 +02:00
|
|
|
int i, ch;
|
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
ret = av_new_packet(pkt, par->block_align);
|
2015-10-17 12:11:16 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-16 00:09:02 +02:00
|
|
|
for (i = 0; i < 4; i++) {
|
2016-04-10 21:58:15 +02:00
|
|
|
for (ch = 0; ch < par->channels; ch++) {
|
2015-10-16 00:09:02 +02:00
|
|
|
pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
|
|
|
|
pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
|
|
|
|
}
|
2013-05-10 21:52:10 +02:00
|
|
|
}
|
2015-10-16 00:09:02 +02:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2013-05-10 21:52:10 +02:00
|
|
|
}
|
2015-10-16 00:09:02 +02:00
|
|
|
|
2016-04-10 21:58:15 +02:00
|
|
|
if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
|
2015-11-20 20:51:22 +01:00
|
|
|
pkt->duration = (pkt->data[0] >> 2) * 512;
|
|
|
|
|
|
|
|
pkt->pos = pos;
|
2013-05-10 21:52:10 +02:00
|
|
|
pkt->stream_index = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVInputFormat ff_rsd_demuxer = {
|
|
|
|
.name = "rsd",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("GameCube RSD"),
|
|
|
|
.read_probe = rsd_probe,
|
|
|
|
.read_header = rsd_read_header,
|
|
|
|
.read_packet = rsd_read_packet,
|
|
|
|
.extensions = "rsd",
|
|
|
|
.codec_tag = (const AVCodecTag* const []){rsd_tags, 0},
|
2015-11-20 20:51:22 +01:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2013-05-10 21:52:10 +02:00
|
|
|
};
|