diff --git a/Changelog b/Changelog index 2f71a6f083..04082a9b96 100644 --- a/Changelog +++ b/Changelog @@ -20,7 +20,7 @@ version : - selectivecolor filter - extensive native AAC encoder improvements - ADPCM PSX decoder -- genh, vag, xvag, ads, msf & svag demuxer +- 3dostr, genh, vag, xvag, ads, msf & svag demuxer - zscale filter - wve demuxer - zero-copy Intel QSV transcoding in ffmpeg diff --git a/doc/general.texi b/doc/general.texi index 942ae63d5e..37929c99f2 100644 --- a/doc/general.texi +++ b/doc/general.texi @@ -223,6 +223,7 @@ library: @multitable @columnfractions .4 .1 .1 .4 @item Name @tab Encoding @tab Decoding @tab Comments +@item 3dostr @tab @tab X @item 4xm @tab @tab X @tab 4X Technologies format, used in some games. @item 8088flex TMV @tab @tab X diff --git a/libavformat/3dostr.c b/libavformat/3dostr.c new file mode 100644 index 0000000000..ac58a6cd80 --- /dev/null +++ b/libavformat/3dostr.c @@ -0,0 +1,168 @@ +/* + * 3DO STR demuxer + * Copyright (c) 2015 Paul B Mahol + * + * 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 "avformat.h" +#include "internal.h" + +static int threedostr_probe(AVProbeData *p) +{ + if (memcmp(p->buf, "CTRL", 4) && + memcmp(p->buf, "SHDR", 4) && + memcmp(p->buf, "SNDS", 4)) + return 0; + + return AVPROBE_SCORE_MAX / 3 * 2; +} + +static int threedostr_read_header(AVFormatContext *s) +{ + unsigned chunk, codec = 0, size, ctrl_size = -1, found_shdr = 0; + AVStream *st; + + while (!avio_feof(s->pb) && !found_shdr) { + chunk = avio_rl32(s->pb); + size = avio_rb32(s->pb); + + if (size < 8) + return AVERROR_INVALIDDATA; + size -= 8; + + switch (chunk) { + case MKTAG('C','T','R','L'): + ctrl_size = size; + break; + case MKTAG('S','N','D','S'): + if (size < 56) + return AVERROR_INVALIDDATA; + avio_skip(s->pb, 8); + if (avio_rl32(s->pb) != MKTAG('S','H','D','R')) + return AVERROR_INVALIDDATA; + avio_skip(s->pb, 24); + + st = avformat_new_stream(s, NULL); + if (!st) + return AVERROR(ENOMEM); + + st->codec->codec_type = AVMEDIA_TYPE_AUDIO; + st->codec->sample_rate = avio_rb32(s->pb); + st->codec->channels = avio_rb32(s->pb); + if (st->codec->channels <= 0) + return AVERROR_INVALIDDATA; + codec = avio_rl32(s->pb); + avio_skip(s->pb, 4); + if (ctrl_size == 20 || ctrl_size == 3 || ctrl_size == -1) + st->duration = (avio_rb32(s->pb) - 1) / st->codec->channels; + else + st->duration = avio_rb32(s->pb) * 16 / st->codec->channels; + size -= 56; + found_shdr = 1; + break; + case MKTAG('S','H','D','R'): + if (size > 0x78) { + avio_skip(s->pb, 0x74); + size -= 0x78; + if (avio_rl32(s->pb) == MKTAG('C','T','R','L') && size > 4) { + ctrl_size = avio_rb32(s->pb); + size -= 4;; + } + } + break; + default: + av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk); + break; + } + + avio_skip(s->pb, size); + } + + switch (codec) { + case MKTAG('S','D','X','2'): + st->codec->codec_id = AV_CODEC_ID_SDX2_DPCM; + st->codec->block_align = 1 * st->codec->channels; + break; + default: + avpriv_request_sample(s, "codec %X", codec); + return AVERROR_PATCHWELCOME; + } + + avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); + + return 0; +} + +static int threedostr_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + unsigned chunk, size, found_ssmp = 0; + AVStream *st = s->streams[0]; + int64_t pos; + int ret = 0; + + while (!found_ssmp) { + if (avio_feof(s->pb)) + return AVERROR_EOF; + + pos = avio_tell(s->pb); + chunk = avio_rl32(s->pb); + size = avio_rb32(s->pb); + + if (!size) + continue; + + if (size < 8) + return AVERROR_INVALIDDATA; + size -= 8; + + switch (chunk) { + case MKTAG('S','N','D','S'): + if (size <= 16) + return AVERROR_INVALIDDATA; + avio_skip(s->pb, 8); + if (avio_rl32(s->pb) != MKTAG('S','S','M','P')) + return AVERROR_INVALIDDATA; + avio_skip(s->pb, 4); + size -= 16; + ret = av_get_packet(s->pb, pkt, size); + pkt->pos = pos; + pkt->stream_index = 0; + pkt->duration = size / st->codec->channels; + size = 0; + found_ssmp = 1; + break; + default: + av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk); + break; + } + + avio_skip(s->pb, size); + } + + return ret; +} + +AVInputFormat ff_threedostr_demuxer = { + .name = "3dostr", + .long_name = NULL_IF_CONFIG_SMALL("3DO STR"), + .read_probe = threedostr_probe, + .read_header = threedostr_read_header, + .read_packet = threedostr_read_packet, + .extensions = "str", + .flags = AVFMT_GENERIC_INDEX, +}; diff --git a/libavformat/Makefile b/libavformat/Makefile index 5ca2843d37..c3d9e18bf1 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -435,6 +435,7 @@ OBJS-$(CONFIG_TAK_DEMUXER) += takdec.o apetag.o img2.o rawdec.o OBJS-$(CONFIG_TEDCAPTIONS_DEMUXER) += tedcaptionsdec.o subtitles.o OBJS-$(CONFIG_TEE_MUXER) += tee.o OBJS-$(CONFIG_THP_DEMUXER) += thp.o +OBJS-$(CONFIG_THREEDOSTR_DEMUXER) += 3dostr.o OBJS-$(CONFIG_TIERTEXSEQ_DEMUXER) += tiertexseq.o OBJS-$(CONFIG_MKVTIMESTAMP_V2_MUXER) += mkvtimestamp_v2.o OBJS-$(CONFIG_TMV_DEMUXER) += tmv.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index 69c02b1060..a67a226890 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -303,6 +303,7 @@ void av_register_all(void) REGISTER_MUXER (TG2, tg2); REGISTER_MUXER (TGP, tgp); REGISTER_DEMUXER (THP, thp); + REGISTER_DEMUXER (THREEDOSTR, threedostr); REGISTER_DEMUXER (TIERTEXSEQ, tiertexseq); REGISTER_MUXER (MKVTIMESTAMP_V2, mkvtimestamp_v2); REGISTER_DEMUXER (TMV, tmv); diff --git a/libavformat/version.h b/libavformat/version.h index c48e3b8239..d4ba0cef78 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -30,7 +30,7 @@ #include "libavutil/version.h" #define LIBAVFORMAT_VERSION_MAJOR 57 -#define LIBAVFORMAT_VERSION_MINOR 11 +#define LIBAVFORMAT_VERSION_MINOR 12 #define LIBAVFORMAT_VERSION_MICRO 100 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \