diff options
author | Paul B Mahol <onemda@gmail.com> | 2021-02-17 17:19:55 +0100 |
---|---|---|
committer | Paul B Mahol <onemda@gmail.com> | 2021-02-20 17:43:17 +0100 |
commit | 51a9f487ae2a12fa51db916315c88e6f38adca15 (patch) | |
tree | d84c48f6713388ae575072f3cf27001072d04752 /libavformat/imx.c | |
parent | 8651bf8a2ed120bfdf81b4c498b0c2452de54b4e (diff) | |
download | ffmpeg-51a9f487ae2a12fa51db916315c88e6f38adca15.tar.gz |
avformat: add Simbiosis IMX demuxer
Diffstat (limited to 'libavformat/imx.c')
-rw-r--r-- | libavformat/imx.c | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/libavformat/imx.c b/libavformat/imx.c new file mode 100644 index 0000000000..66f18177a5 --- /dev/null +++ b/libavformat/imx.c @@ -0,0 +1,160 @@ +/* + * Simbiosis game demuxer + * + * Copyright (C) 2021 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" +#include "libavutil/intreadwrite.h" +#include "libavutil/avassert.h" +#include "libavutil/internal.h" +#include "argo_asf.h" + +#define IMX_TAG MKTAG('I', 'M', 'A', 'X') + +typedef struct SimbiosisIMXDemuxContext { + uint8_t pal[AVPALETTE_SIZE]; + int pal_changed; +} SimbiosisIMXDemuxContext; + +static int simbiosis_imx_probe(const AVProbeData *p) +{ + if (AV_RL32(p->buf) != IMX_TAG) + return 0; + if (AV_RN32(p->buf+4) == 0) + return 0; + if (AV_RN16(p->buf+8) == 0) + return 0; + if (AV_RL16(p->buf+10) != 0x102) + return 0; + + return AVPROBE_SCORE_EXTENSION + 10; +} + +static int simbiosis_imx_read_header(AVFormatContext *s) +{ + AVIOContext *pb = s->pb; + AVStream *vst, *ast; + int rate; + + vst = avformat_new_stream(s, NULL); + ast = avformat_new_stream(s, NULL); + if (!vst || !ast) + return AVERROR(ENOMEM); + + avio_skip(pb, 4); + + vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; + vst->codecpar->codec_tag = 0; + vst->codecpar->format = AV_PIX_FMT_PAL8; + vst->codecpar->codec_id = AV_CODEC_ID_SIMBIOSIS_IMX; + vst->start_time = 0; + vst->nb_frames = avio_rl32(pb); + rate = avio_rl16(pb); + avio_skip(pb, 12); + + avpriv_set_pts_info(vst, 64, 1, rate); + + ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; + ast->codecpar->codec_tag = 0; + ast->codecpar->codec_id = AV_CODEC_ID_PCM_U8; + ast->codecpar->channels = 1; + ast->codecpar->channel_layout = AV_CH_LAYOUT_MONO; + ast->codecpar->sample_rate = 22050; + ast->start_time = 0; + + avpriv_set_pts_info(ast, 64, 1, 22050); + + return 0; +} + +static int simbiosis_imx_read_packet(AVFormatContext *s, AVPacket *pkt) +{ + AVIOContext *pb = s->pb; + SimbiosisIMXDemuxContext *imx = s->priv_data; + uint32_t chunk_size, chunk_type; + int64_t pos = avio_tell(pb); + int ret, idx = -1; + +retry: + if (avio_feof(pb)) + return AVERROR_EOF; + + chunk_size = avio_rl32(pb); + chunk_type = avio_rl32(pb); + + switch (chunk_type) { + case 0xAAFF: + return AVERROR_EOF; + case 0xAA99: + idx = 1; + break; + case 0xAA97: + idx = 0; + break; + case 0xAA98: + for (int i = 0; i < chunk_size / 3; i++) { + unsigned r = avio_r8(pb) << 18; + unsigned g = avio_r8(pb) << 10; + unsigned b = avio_r8(pb) << 2; + + AV_WL32(imx->pal + i * 4, (0xFFU << 24) | r | g | b); + } + imx->pal_changed = 1; + idx = -1; + break; + default: + return AVERROR_INVALIDDATA; + } + + if (idx == -1) + goto retry; + + ret = av_get_packet(pb, pkt, chunk_size); + if (ret < 0) + return ret; + + if (imx->pal_changed && idx == 0) { + uint8_t *pal = av_packet_new_side_data(pkt, AV_PKT_DATA_PALETTE, + AVPALETTE_SIZE); + if (!pal) + return AVERROR(ENOMEM); + memcpy(pal, imx->pal, AVPALETTE_SIZE); + imx->pal_changed = 0; + pkt->flags |= AV_PKT_FLAG_KEY; + } + + pkt->pos = pos; + pkt->stream_index = idx; + pkt->duration = idx ? chunk_size : 1; + + return ret; +} + +AVInputFormat ff_simbiosis_imx_demuxer = { + .name = "simbiosis_imx", + .long_name = NULL_IF_CONFIG_SMALL("Simbiosis Interactive IMX"), + .priv_data_size = sizeof(SimbiosisIMXDemuxContext), + .read_probe = simbiosis_imx_probe, + .read_header = simbiosis_imx_read_header, + .read_packet = simbiosis_imx_read_packet, + .extensions = "imx", + .flags = AVFMT_GENERIC_INDEX, +}; |