2013-05-10 19:52:10 +00: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 11:45:10 +00:00
|
|
|
{ AV_CODEC_ID_ADPCM_PSX, MKTAG('V','A','G',' ') },
|
2015-11-19 19:00:16 +00:00
|
|
|
{ AV_CODEC_ID_ADPCM_THP_LE, MKTAG('G','A','D','P') },
|
2015-10-15 22:09:02 +00:00
|
|
|
{ AV_CODEC_ID_ADPCM_THP, MKTAG('W','A','D','P') },
|
2013-05-10 19:53:50 +00:00
|
|
|
{ AV_CODEC_ID_ADPCM_IMA_RAD, MKTAG('R','A','D','P') },
|
2015-10-11 16:33:57 +00:00
|
|
|
{ AV_CODEC_ID_ADPCM_IMA_WAV, MKTAG('X','A','D','P') },
|
2013-05-10 19:52:10 +00:00
|
|
|
{ AV_CODEC_ID_PCM_S16BE, MKTAG('P','C','M','B') },
|
|
|
|
{ AV_CODEC_ID_PCM_S16LE, MKTAG('P','C','M',' ') },
|
2015-11-10 20:29:25 +00:00
|
|
|
{ AV_CODEC_ID_XMA2, MKTAG('X','M','A',' ') },
|
2013-05-10 19:52:10 +00: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 10:48:49 +00: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 12:10:23 +00:00
|
|
|
return AVPROBE_SCORE_MAX / 8;
|
2013-11-06 10:48:49 +00:00
|
|
|
if (AV_RL32(p->buf + 16) > 8*48000 || !AV_RL32(p->buf + 16))
|
2013-11-10 12:10:23 +00:00
|
|
|
return AVPROBE_SCORE_MAX / 8;
|
2013-11-06 10:51:16 +00:00
|
|
|
return AVPROBE_SCORE_MAX;
|
2013-05-10 19:52:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rsd_read_header(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
AVIOContext *pb = s->pb;
|
2015-10-16 19:09:10 +00:00
|
|
|
int i, ret, version, start = 0x800;
|
2013-05-10 19:52:10 +00:00
|
|
|
AVCodecContext *codec;
|
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
avio_skip(pb, 3); // "RSD"
|
|
|
|
version = avio_r8(pb) - '0';
|
|
|
|
|
|
|
|
codec = st->codec;
|
|
|
|
codec->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
codec->codec_tag = avio_rl32(pb);
|
|
|
|
codec->codec_id = ff_codec_get_id(rsd_tags, codec->codec_tag);
|
|
|
|
if (!codec->codec_id) {
|
2014-12-19 23:17:43 +00:00
|
|
|
char tag_buf[32];
|
2013-05-10 19:52:10 +00:00
|
|
|
|
|
|
|
av_get_codec_tag_string(tag_buf, sizeof(tag_buf), codec->codec_tag);
|
|
|
|
for (i=0; i < FF_ARRAY_ELEMS(rsd_unsupported_tags); i++) {
|
|
|
|
if (codec->codec_tag == rsd_unsupported_tags[i]) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
codec->channels = avio_rl32(pb);
|
|
|
|
if (!codec->channels)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(pb, 4); // Bit depth
|
|
|
|
codec->sample_rate = avio_rl32(pb);
|
|
|
|
if (!codec->sample_rate)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_skip(pb, 4); // Unknown
|
|
|
|
|
|
|
|
switch (codec->codec_id) {
|
2015-11-10 20:29:25 +00:00
|
|
|
case AV_CODEC_ID_XMA2:
|
2015-11-09 09:31:24 +00:00
|
|
|
codec->block_align = 2048;
|
2015-11-10 20:29:25 +00:00
|
|
|
ff_alloc_extradata(codec, 34);
|
2015-11-09 09:31:24 +00:00
|
|
|
if (!codec->extradata)
|
|
|
|
return AVERROR(ENOMEM);
|
2015-11-10 20:29:25 +00:00
|
|
|
memset(codec->extradata, 0, 34);
|
2015-11-09 09:31:24 +00:00
|
|
|
break;
|
2015-10-18 11:45:10 +00:00
|
|
|
case AV_CODEC_ID_ADPCM_PSX:
|
|
|
|
codec->block_align = 16 * codec->channels;
|
|
|
|
if (pb->seekable)
|
|
|
|
st->duration = av_get_audio_frame_duration(codec, avio_size(pb) - start);
|
|
|
|
break;
|
2013-05-10 19:53:50 +00:00
|
|
|
case AV_CODEC_ID_ADPCM_IMA_RAD:
|
|
|
|
codec->block_align = 20 * codec->channels;
|
|
|
|
if (pb->seekable)
|
|
|
|
st->duration = av_get_audio_frame_duration(codec, avio_size(pb) - start);
|
|
|
|
break;
|
2015-10-11 16:33:57 +00:00
|
|
|
case AV_CODEC_ID_ADPCM_IMA_WAV:
|
|
|
|
if (version == 2)
|
|
|
|
start = avio_rl32(pb);
|
|
|
|
|
|
|
|
codec->bits_per_coded_sample = 4;
|
|
|
|
codec->block_align = 36 * codec->channels;
|
|
|
|
if (pb->seekable)
|
|
|
|
st->duration = av_get_audio_frame_duration(codec, avio_size(pb) - start);
|
|
|
|
break;
|
2015-11-19 19:00:16 +00:00
|
|
|
case AV_CODEC_ID_ADPCM_THP_LE:
|
2015-11-19 19:06:01 +00:00
|
|
|
/* RSD3GADP is mono, so only alloc enough memory
|
|
|
|
to store the coeff table for a single channel. */
|
2013-05-10 19:52:10 +00:00
|
|
|
|
2015-11-19 19:06:01 +00:00
|
|
|
start = avio_rl32(pb);
|
2015-10-15 22:09:02 +00:00
|
|
|
|
2015-11-19 19:06:01 +00:00
|
|
|
if ((ret = ff_get_extradata(codec, s->pb, 32)) < 0)
|
|
|
|
return ret;
|
2015-11-19 19:00:16 +00:00
|
|
|
if (pb->seekable)
|
|
|
|
st->duration = av_get_audio_frame_duration(codec, avio_size(pb) - start);
|
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_ADPCM_THP:
|
2015-11-19 19:06:01 +00:00
|
|
|
codec->block_align = 8 * codec->channels;
|
|
|
|
avio_skip(s->pb, 0x1A4 - avio_tell(s->pb));
|
2013-05-10 19:52:10 +00:00
|
|
|
|
2015-11-19 19:06:01 +00:00
|
|
|
if ((ret = ff_alloc_extradata(st->codec, 32 * st->codec->channels)) < 0)
|
|
|
|
return ret;
|
2013-05-10 19:52:10 +00:00
|
|
|
|
2015-11-19 19:06:01 +00:00
|
|
|
for (i = 0; i < st->codec->channels; i++) {
|
|
|
|
avio_read(s->pb, st->codec->extradata + 32 * i, 32);
|
|
|
|
avio_skip(s->pb, 8);
|
|
|
|
}
|
2013-05-10 19:52:10 +00:00
|
|
|
if (pb->seekable)
|
2015-10-15 22:09:02 +00:00
|
|
|
st->duration = (avio_size(pb) - start) / (8 * st->codec->channels) * 14;
|
2013-05-10 19:52:10 +00:00
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_PCM_S16LE:
|
|
|
|
case AV_CODEC_ID_PCM_S16BE:
|
|
|
|
if (version != 4)
|
|
|
|
start = avio_rl32(pb);
|
|
|
|
|
|
|
|
if (pb->seekable)
|
|
|
|
st->duration = (avio_size(pb) - start) / 2 / codec->channels;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(pb, start - avio_tell(pb));
|
2015-11-10 20:29:25 +00:00
|
|
|
if (codec->codec_id == AV_CODEC_ID_XMA2) {
|
2015-11-09 09:31:24 +00:00
|
|
|
avio_skip(pb, avio_rb32(pb) + avio_rb32(pb));
|
|
|
|
st->duration = avio_rb32(pb);
|
|
|
|
}
|
2013-05-10 19:52:10 +00:00
|
|
|
|
|
|
|
avpriv_set_pts_info(st, 64, 1, codec->sample_rate);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsd_read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2013-05-10 19:53:50 +00:00
|
|
|
AVCodecContext *codec = s->streams[0]->codec;
|
2013-05-10 19:52:10 +00:00
|
|
|
int ret, size = 1024;
|
2015-11-20 19:51:22 +00:00
|
|
|
int64_t pos;
|
2013-05-10 19:52:10 +00:00
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(s->pb))
|
2013-05-10 19:52:10 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2015-11-20 19:51:22 +00:00
|
|
|
pos = avio_tell(s->pb);
|
2015-10-11 16:33:57 +00:00
|
|
|
if (codec->codec_id == AV_CODEC_ID_ADPCM_IMA_RAD ||
|
2015-10-18 11:45:10 +00:00
|
|
|
codec->codec_id == AV_CODEC_ID_ADPCM_PSX ||
|
2015-11-09 09:31:24 +00:00
|
|
|
codec->codec_id == AV_CODEC_ID_ADPCM_IMA_WAV ||
|
2015-11-10 20:29:25 +00:00
|
|
|
codec->codec_id == AV_CODEC_ID_XMA2) {
|
2013-05-10 19:53:50 +00:00
|
|
|
ret = av_get_packet(s->pb, pkt, codec->block_align);
|
2015-10-15 22:09:02 +00:00
|
|
|
} else if (codec->codec_tag == MKTAG('W','A','D','P') &&
|
|
|
|
codec->channels > 1) {
|
|
|
|
int i, ch;
|
|
|
|
|
2015-10-17 10:11:16 +00:00
|
|
|
ret = av_new_packet(pkt, codec->block_align);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2015-10-15 22:09:02 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
|
|
|
for (ch = 0; ch < codec->channels; ch++) {
|
|
|
|
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 19:52:10 +00:00
|
|
|
}
|
2015-10-15 22:09:02 +00:00
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = av_get_packet(s->pb, pkt, size);
|
2013-05-10 19:52:10 +00:00
|
|
|
}
|
2015-10-15 22:09:02 +00:00
|
|
|
|
2015-11-20 19:51:22 +00:00
|
|
|
if (codec->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
|
|
|
|
pkt->duration = (pkt->data[0] >> 2) * 512;
|
|
|
|
|
|
|
|
pkt->pos = pos;
|
2013-05-10 19:52:10 +00: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 19:51:22 +00:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2013-05-10 19:52:10 +00:00
|
|
|
};
|