d3710c51 |
/*
* 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[] = { |
bb1d3f10 |
{ AV_CODEC_ID_ADPCM_PSX, MKTAG('V','A','G',' ') }, |
01c63e69 |
{ AV_CODEC_ID_ADPCM_THP_LE, MKTAG('G','A','D','P') }, |
3a638901 |
{ AV_CODEC_ID_ADPCM_THP, MKTAG('W','A','D','P') }, |
67fad0d2 |
{ AV_CODEC_ID_ADPCM_IMA_RAD, MKTAG('R','A','D','P') }, |
a9922613 |
{ AV_CODEC_ID_ADPCM_IMA_WAV, MKTAG('X','A','D','P') }, |
d3710c51 |
{ AV_CODEC_ID_PCM_S16BE, MKTAG('P','C','M','B') },
{ AV_CODEC_ID_PCM_S16LE, MKTAG('P','C','M',' ') }, |
2905c512 |
{ AV_CODEC_ID_XMA2, MKTAG('X','M','A',' ') }, |
d3710c51 |
{ AV_CODEC_ID_NONE, 0 },
};
static const uint32_t rsd_unsupported_tags[] = {
MKTAG('O','G','G',' '),
};
|
4d8875ec |
static int rsd_probe(const AVProbeData *p) |
d3710c51 |
{ |
50a3c4c5 |
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)) |
eb968023 |
return AVPROBE_SCORE_MAX / 8; |
50a3c4c5 |
if (AV_RL32(p->buf + 16) > 8*48000 || !AV_RL32(p->buf + 16)) |
eb968023 |
return AVPROBE_SCORE_MAX / 8; |
7b769764 |
return AVPROBE_SCORE_MAX; |
d3710c51 |
}
static int rsd_read_header(AVFormatContext *s)
{
AVIOContext *pb = s->pb; |
8297f560 |
int i, ret, version, start = 0x800; |
6f69f7a8 |
AVCodecParameters *par; |
d3710c51 |
AVStream *st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);
avio_skip(pb, 3); // "RSD"
version = avio_r8(pb) - '0';
|
6f69f7a8 |
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) { |
cd4d6cba |
const char *tag_buf = av_fourcc2str(par->codec_tag); |
d3710c51 |
for (i=0; i < FF_ARRAY_ELEMS(rsd_unsupported_tags); i++) { |
6f69f7a8 |
if (par->codec_tag == rsd_unsupported_tags[i]) { |
d3710c51 |
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;
}
|
6f69f7a8 |
par->channels = avio_rl32(pb); |
ee5f0f1d |
if (par->channels <= 0 || par->channels > INT_MAX / 36) {
av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", par->channels); |
d3710c51 |
return AVERROR_INVALIDDATA; |
ee5f0f1d |
} |
d3710c51 |
avio_skip(pb, 4); // Bit depth |
6f69f7a8 |
par->sample_rate = avio_rl32(pb);
if (!par->sample_rate) |
d3710c51 |
return AVERROR_INVALIDDATA;
avio_skip(pb, 4); // Unknown
|
6f69f7a8 |
switch (par->codec_id) { |
2905c512 |
case AV_CODEC_ID_XMA2: |
6f69f7a8 |
par->block_align = 2048; |
c1e439d7 |
if ((ret = ff_alloc_extradata(par, 34)) < 0)
return ret; |
6f69f7a8 |
memset(par->extradata, 0, 34); |
0cfd4a99 |
break; |
bb1d3f10 |
case AV_CODEC_ID_ADPCM_PSX: |
6f69f7a8 |
par->block_align = 16 * par->channels; |
4de591e6 |
if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
6f69f7a8 |
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start); |
bb1d3f10 |
break; |
67fad0d2 |
case AV_CODEC_ID_ADPCM_IMA_RAD: |
6f69f7a8 |
par->block_align = 20 * par->channels; |
4de591e6 |
if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
6f69f7a8 |
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start); |
67fad0d2 |
break; |
a9922613 |
case AV_CODEC_ID_ADPCM_IMA_WAV:
if (version == 2)
start = avio_rl32(pb);
|
6f69f7a8 |
par->bits_per_coded_sample = 4;
par->block_align = 36 * par->channels; |
4de591e6 |
if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
6f69f7a8 |
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start); |
a9922613 |
break; |
01c63e69 |
case AV_CODEC_ID_ADPCM_THP_LE: |
183827e6 |
/* RSD3GADP is mono, so only alloc enough memory
to store the coeff table for a single channel. */ |
d3710c51 |
|
183827e6 |
start = avio_rl32(pb); |
3a638901 |
|
323b8c95 |
if ((ret = ff_get_extradata(s, par, s->pb, 32)) < 0) |
183827e6 |
return ret; |
4de591e6 |
if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
6f69f7a8 |
st->duration = av_get_audio_frame_duration2(par, avio_size(pb) - start); |
01c63e69 |
break;
case AV_CODEC_ID_ADPCM_THP: |
6f69f7a8 |
par->block_align = 8 * par->channels; |
183827e6 |
avio_skip(s->pb, 0x1A4 - avio_tell(s->pb)); |
d3710c51 |
|
6f69f7a8 |
if ((ret = ff_alloc_extradata(st->codecpar, 32 * par->channels)) < 0) |
183827e6 |
return ret; |
d3710c51 |
|
6f69f7a8 |
for (i = 0; i < par->channels; i++) {
avio_read(s->pb, st->codecpar->extradata + 32 * i, 32); |
183827e6 |
avio_skip(s->pb, 8);
} |
4de591e6 |
if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
6f69f7a8 |
st->duration = (avio_size(pb) - start) / (8 * par->channels) * 14; |
d3710c51 |
break;
case AV_CODEC_ID_PCM_S16LE:
case AV_CODEC_ID_PCM_S16BE:
if (version != 4)
start = avio_rl32(pb);
|
4de591e6 |
if (pb->seekable & AVIO_SEEKABLE_NORMAL) |
6f69f7a8 |
st->duration = (avio_size(pb) - start) / 2 / par->channels; |
d3710c51 |
break;
}
avio_skip(pb, start - avio_tell(pb)); |
6f69f7a8 |
if (par->codec_id == AV_CODEC_ID_XMA2) { |
0cfd4a99 |
avio_skip(pb, avio_rb32(pb) + avio_rb32(pb));
st->duration = avio_rb32(pb);
} |
d3710c51 |
|
6f69f7a8 |
avpriv_set_pts_info(st, 64, 1, par->sample_rate); |
d3710c51 |
return 0;
}
static int rsd_read_packet(AVFormatContext *s, AVPacket *pkt)
{ |
6f69f7a8 |
AVCodecParameters *par = s->streams[0]->codecpar; |
d3710c51 |
int ret, size = 1024; |
b885d9ce |
int64_t pos; |
d3710c51 |
|
d34ec64a |
if (avio_feof(s->pb)) |
d3710c51 |
return AVERROR_EOF;
|
b885d9ce |
pos = avio_tell(s->pb); |
6f69f7a8 |
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) { |
3a638901 |
int i, ch;
|
6f69f7a8 |
ret = av_new_packet(pkt, par->block_align); |
92a26261 |
if (ret < 0)
return ret; |
3a638901 |
for (i = 0; i < 4; i++) { |
6f69f7a8 |
for (ch = 0; ch < par->channels; ch++) { |
3a638901 |
pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
} |
d3710c51 |
} |
3a638901 |
ret = 0;
} else {
ret = av_get_packet(s->pb, pkt, size); |
d3710c51 |
} |
3a638901 |
|
6f69f7a8 |
if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1) |
b885d9ce |
pkt->duration = (pkt->data[0] >> 2) * 512;
pkt->pos = pos; |
d3710c51 |
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}, |
b885d9ce |
.flags = AVFMT_GENERIC_INDEX, |
d3710c51 |
}; |