mirror of https://github.com/FFmpeg/FFmpeg.git
* jamrial/adpcm: ADPCM IMA Radical decoder RedSpark demuxer RSD demuxer adpcm_thp: Allow the use of extradata for the adpcm table ADP demuxer ADPCM DTK decoder Merged-by: Michael Niedermayer <michaelni@gmx.at>pull/18/head
commit
cbcc5cbbd8
22 changed files with 614 additions and 5 deletions
@ -0,0 +1,91 @@ |
||||
/*
|
||||
* ADP 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 "libavutil/channel_layout.h" |
||||
#include "libavutil/intreadwrite.h" |
||||
#include "avformat.h" |
||||
#include "internal.h" |
||||
|
||||
static int adp_probe(AVProbeData *p) |
||||
{ |
||||
int i; |
||||
|
||||
if (p->buf_size < 32) |
||||
return 0; |
||||
|
||||
for (i = 0; i < p->buf_size - 3; i+=32) |
||||
if (p->buf[i] != p->buf[i+2] || p->buf[i+1] != p->buf[i+3]) |
||||
return 0; |
||||
|
||||
return p->buf_size < 260 ? 1 : AVPROBE_SCORE_MAX / 4; |
||||
} |
||||
|
||||
static int adp_read_header(AVFormatContext *s) |
||||
{ |
||||
AVStream *st; |
||||
|
||||
st = avformat_new_stream(s, NULL); |
||||
if (!st) |
||||
return AVERROR(ENOMEM); |
||||
|
||||
st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
||||
st->codec->codec_id = AV_CODEC_ID_ADPCM_DTK; |
||||
st->codec->channel_layout = AV_CH_LAYOUT_STEREO; |
||||
st->codec->channels = 2; |
||||
st->codec->sample_rate = 48000; |
||||
st->start_time = 0; |
||||
if (s->pb->seekable) |
||||
st->duration = av_get_audio_frame_duration(st->codec, avio_size(s->pb)); |
||||
|
||||
avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int adp_read_packet(AVFormatContext *s, AVPacket *pkt) |
||||
{ |
||||
int ret, size = 1024; |
||||
|
||||
if (url_feof(s->pb)) |
||||
return AVERROR_EOF; |
||||
|
||||
ret = av_get_packet(s->pb, pkt, size); |
||||
|
||||
if (ret != size) { |
||||
if (ret < 0) { |
||||
av_free_packet(pkt); |
||||
return ret; |
||||
} |
||||
av_shrink_packet(pkt, ret); |
||||
} |
||||
pkt->stream_index = 0; |
||||
|
||||
return ret; |
||||
} |
||||
|
||||
AVInputFormat ff_adp_demuxer = { |
||||
.name = "adp", |
||||
.long_name = NULL_IF_CONFIG_SMALL("ADP"), |
||||
.read_probe = adp_probe, |
||||
.read_header = adp_read_header, |
||||
.read_packet = adp_read_packet, |
||||
.extensions = "adp,dtk", |
||||
}; |
@ -0,0 +1,157 @@ |
||||
/*
|
||||
* RedSpark 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" |
||||
|
||||
#define HEADER_SIZE 4096 |
||||
|
||||
typedef struct RedSparkContext { |
||||
int samples_count; |
||||
} RedSparkContext; |
||||
|
||||
static int redspark_probe(AVProbeData *p) |
||||
{ |
||||
uint32_t key, data; |
||||
uint8_t header[8]; |
||||
|
||||
/* Decrypt first 8 bytes of the header */ |
||||
data = AV_RB32(p->buf); |
||||
data = data ^ (key = data ^ 0x52656453); |
||||
AV_WB32(header, data); |
||||
key = (key << 11) | (key >> 21); |
||||
|
||||
data = AV_RB32(p->buf + 4) ^ (((key << 3) | (key >> 29)) + key); |
||||
AV_WB32(header + 4, data); |
||||
|
||||
if (AV_RB64(header) == AV_RB64("RedSpark")) |
||||
return AVPROBE_SCORE_MAX; |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int redspark_read_header(AVFormatContext *s) |
||||
{ |
||||
AVIOContext *pb = s->pb; |
||||
RedSparkContext *redspark = s->priv_data; |
||||
AVCodecContext *codec; |
||||
GetByteContext gbc; |
||||
int i, coef_off; |
||||
uint32_t key, data; |
||||
uint8_t *header, *pbc; |
||||
AVStream *st; |
||||
|
||||
st = avformat_new_stream(s, NULL); |
||||
if (!st) |
||||
return AVERROR(ENOMEM); |
||||
codec = st->codec; |
||||
|
||||
header = av_malloc(HEADER_SIZE); |
||||
if (!header) |
||||
return AVERROR(ENOMEM); |
||||
pbc = header; |
||||
|
||||
/* Decrypt header */ |
||||
data = avio_rb32(pb); |
||||
data = data ^ (key = data ^ 0x52656453); |
||||
bytestream_put_be32(&pbc, data); |
||||
key = (key << 11) | (key >> 21); |
||||
|
||||
for (i = 4; i < HEADER_SIZE; i += 4) { |
||||
data = avio_rb32(pb) ^ (key = ((key << 3) | (key >> 29)) + key); |
||||
bytestream_put_be32(&pbc, data); |
||||
} |
||||
|
||||
codec->codec_id = AV_CODEC_ID_ADPCM_THP; |
||||
codec->codec_type = AVMEDIA_TYPE_AUDIO; |
||||
|
||||
bytestream2_init(&gbc, header, HEADER_SIZE); |
||||
bytestream2_seek(&gbc, 0x3c, SEEK_SET); |
||||
codec->sample_rate = bytestream2_get_be32u(&gbc); |
||||
if (codec->sample_rate <= 0 || codec->sample_rate > 96000) { |
||||
av_log(s, AV_LOG_ERROR, "Invalid sample rate: %d\n", codec->sample_rate); |
||||
return AVERROR_INVALIDDATA; |
||||
} |
||||
|
||||
st->duration = bytestream2_get_be32u(&gbc) * 14; |
||||
redspark->samples_count = 0; |
||||
bytestream2_skipu(&gbc, 10); |
||||
codec->channels = bytestream2_get_byteu(&gbc); |
||||
if (!codec->channels) |
||||
return AVERROR_INVALIDDATA; |
||||
|
||||
coef_off = 0x54 + codec->channels * 8; |
||||
if (bytestream2_get_byteu(&gbc)) // Loop flag
|
||||
coef_off += 16; |
||||
|
||||
codec->extradata_size = 32 * codec->channels; |
||||
codec->extradata = av_malloc(codec->extradata_size); |
||||
if (!codec->extradata) |
||||
return AVERROR(ENOMEM); |
||||
|
||||
/* Get the ADPCM table */ |
||||
bytestream2_seek(&gbc, coef_off, SEEK_SET); |
||||
for (i = 0; i < codec->channels; i++) { |
||||
if (bytestream2_get_bufferu(&gbc, codec->extradata + i * 32, 32) != 32) |
||||
return AVERROR_INVALIDDATA; |
||||
bytestream2_skipu(&gbc, 14); |
||||
} |
||||
|
||||
avpriv_set_pts_info(st, 64, 1, codec->sample_rate); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int redspark_read_packet(AVFormatContext *s, AVPacket *pkt) |
||||
{ |
||||
AVCodecContext *codec = s->streams[0]->codec; |
||||
RedSparkContext *redspark = s->priv_data; |
||||
uint32_t size = 8 * codec->channels; |
||||
int ret; |
||||
|
||||
if (url_feof(s->pb) || redspark->samples_count == s->streams[0]->duration) |
||||
return AVERROR_EOF; |
||||
|
||||
ret = av_get_packet(s->pb, pkt, size); |
||||
if (ret != size) { |
||||
av_free_packet(pkt); |
||||
return AVERROR(EIO); |
||||
} |
||||
|
||||
pkt->duration = 14; |
||||
redspark->samples_count += pkt->duration; |
||||
pkt->stream_index = 0; |
||||
|
||||
return ret; |
||||
} |
||||
|
||||
AVInputFormat ff_redspark_demuxer = { |
||||
.name = "redspark", |
||||
.long_name = NULL_IF_CONFIG_SMALL("RedSpark"), |
||||
.priv_data_size = sizeof(RedSparkContext), |
||||
.read_probe = redspark_probe, |
||||
.read_header = redspark_read_header, |
||||
.read_packet = redspark_read_packet, |
||||
.extensions = "rsd", |
||||
}; |
@ -0,0 +1,170 @@ |
||||
/*
|
||||
* 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[] = { |
||||
{ AV_CODEC_ID_ADPCM_THP, MKTAG('G','A','D','P') }, |
||||
{ AV_CODEC_ID_ADPCM_IMA_RAD, MKTAG('R','A','D','P') }, |
||||
{ AV_CODEC_ID_PCM_S16BE, MKTAG('P','C','M','B') }, |
||||
{ AV_CODEC_ID_PCM_S16LE, MKTAG('P','C','M',' ') }, |
||||
{ AV_CODEC_ID_NONE, 0 }, |
||||
}; |
||||
|
||||
static const uint32_t rsd_unsupported_tags[] = { |
||||
MKTAG('O','G','G',' '), |
||||
MKTAG('V','A','G',' '), |
||||
MKTAG('W','A','D','P'), |
||||
MKTAG('X','A','D','P'), |
||||
MKTAG('X','M','A',' '), |
||||
}; |
||||
|
||||
static int rsd_probe(AVProbeData *p) |
||||
{ |
||||
if (!memcmp(p->buf, "RSD", 3) && |
||||
p->buf[3] - '0' >= 2 && p->buf[3] - '0' <= 6) |
||||
return AVPROBE_SCORE_EXTENSION; |
||||
return 0; |
||||
} |
||||
|
||||
static int rsd_read_header(AVFormatContext *s) |
||||
{ |
||||
AVIOContext *pb = s->pb; |
||||
int i, version, start = 0x800; |
||||
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) { |
||||
char tag_buf[5]; |
||||
|
||||
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) { |
||||
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; |
||||
case AV_CODEC_ID_ADPCM_THP: |
||||
/* RSD3GADP is mono, so only alloc enough memory
|
||||
to store the coeff table for a single channel. */ |
||||
|
||||
codec->extradata_size = 32; |
||||
codec->extradata = av_malloc(codec->extradata_size); |
||||
if (!codec->extradata) |
||||
return AVERROR(ENOMEM); |
||||
|
||||
start = avio_rl32(pb); |
||||
|
||||
if (avio_read(s->pb, codec->extradata, 32) != 32) |
||||
return AVERROR_INVALIDDATA; |
||||
|
||||
for (i = 0; i < 16; i++) |
||||
AV_WB16(codec->extradata + i * 2, AV_RL16(codec->extradata + i * 2)); |
||||
|
||||
if (pb->seekable) |
||||
st->duration = (avio_size(pb) - start) / 8 * 14; |
||||
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)); |
||||
|
||||
avpriv_set_pts_info(st, 64, 1, codec->sample_rate); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
static int rsd_read_packet(AVFormatContext *s, AVPacket *pkt) |
||||
{ |
||||
AVCodecContext *codec = s->streams[0]->codec; |
||||
int ret, size = 1024; |
||||
|
||||
if (url_feof(s->pb)) |
||||
return AVERROR_EOF; |
||||
|
||||
if (codec->codec_id == AV_CODEC_ID_ADPCM_IMA_RAD) |
||||
ret = av_get_packet(s->pb, pkt, codec->block_align); |
||||
else |
||||
ret = av_get_packet(s->pb, pkt, size); |
||||
|
||||
if (ret != size) { |
||||
if (ret < 0) { |
||||
av_free_packet(pkt); |
||||
return ret; |
||||
} |
||||
av_shrink_packet(pkt, ret); |
||||
} |
||||
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}, |
||||
}; |
@ -0,0 +1,33 @@ |
||||
#tb 0: 1/48000 |
||||
0, 0, 0, 896, 3584, 0xdae789d5 |
||||
0, 896, 896, 896, 3584, 0x168ed9b6 |
||||
0, 1792, 1792, 896, 3584, 0x8920c8d5 |
||||
0, 2688, 2688, 896, 3584, 0xaf0a3245 |
||||
0, 3584, 3584, 896, 3584, 0x884ee935 |
||||
0, 4480, 4480, 896, 3584, 0xe6a832ad |
||||
0, 5376, 5376, 896, 3584, 0x1fa12ea2 |
||||
0, 6272, 6272, 896, 3584, 0xf119198c |
||||
0, 7168, 7168, 896, 3584, 0x0a6dbf72 |
||||
0, 8064, 8064, 896, 3584, 0xd3467881 |
||||
0, 8960, 8960, 896, 3584, 0x25d504ec |
||||
0, 9856, 9856, 896, 3584, 0x452730c9 |
||||
0, 10752, 10752, 896, 3584, 0x42b92ff1 |
||||
0, 11648, 11648, 896, 3584, 0x85c67bf3 |
||||
0, 12544, 12544, 896, 3584, 0xab4d99e9 |
||||
0, 13440, 13440, 896, 3584, 0xe5bfc4da |
||||
0, 14336, 14336, 896, 3584, 0x7a5210e9 |
||||
0, 15232, 15232, 896, 3584, 0x5265fcd3 |
||||
0, 16128, 16128, 896, 3584, 0x76531427 |
||||
0, 17024, 17024, 896, 3584, 0xb2b8d7ab |
||||
0, 17920, 17920, 896, 3584, 0x05a453e8 |
||||
0, 18816, 18816, 896, 3584, 0x742c45bb |
||||
0, 19712, 19712, 896, 3584, 0x57aaee3b |
||||
0, 20608, 20608, 896, 3584, 0x997bf703 |
||||
0, 21504, 21504, 896, 3584, 0xe2d14b13 |
||||
0, 22400, 22400, 896, 3584, 0xdafbdd2f |
||||
0, 23296, 23296, 896, 3584, 0x448cec3a |
||||
0, 24192, 24192, 896, 3584, 0xe6f6fb9c |
||||
0, 25088, 25088, 896, 3584, 0x0310276a |
||||
0, 25984, 25984, 896, 3584, 0x44bf04e9 |
||||
0, 26880, 26880, 896, 3584, 0xe2105d33 |
||||
0, 27776, 27776, 896, 3584, 0x08b7d5e0 |
@ -0,0 +1 @@ |
||||
495f0ae514c28c6bdcbd40811a17e2a5 |
@ -0,0 +1 @@ |
||||
CRC=0xc0fd1aa2 |
@ -0,0 +1 @@ |
||||
CRC=0x7b7807d8 |
Loading…
Reference in new issue