2012-03-19 02:55:26 +00:00
|
|
|
/*
|
|
|
|
* LucasArts Smush demuxer
|
|
|
|
* Copyright (c) 2006 Cyril Zorin
|
|
|
|
*
|
|
|
|
* 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/intreadwrite.h"
|
2014-03-11 15:24:55 +00:00
|
|
|
|
2012-03-19 02:55:26 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "avio.h"
|
2014-03-11 15:24:55 +00:00
|
|
|
#include "internal.h"
|
2012-03-19 02:55:26 +00:00
|
|
|
|
2014-03-11 15:24:55 +00:00
|
|
|
typedef struct SMUSHContext {
|
2012-03-19 02:55:26 +00:00
|
|
|
int version;
|
|
|
|
int audio_stream_index;
|
|
|
|
int video_stream_index;
|
|
|
|
} SMUSHContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int smush_read_probe(const AVProbeData *p)
|
2012-03-19 02:55:26 +00:00
|
|
|
{
|
2014-03-11 15:24:55 +00:00
|
|
|
if (((AV_RL32(p->buf) == MKTAG('S', 'A', 'N', 'M') &&
|
2012-11-04 16:40:21 +00:00
|
|
|
AV_RL32(p->buf + 8) == MKTAG('S', 'H', 'D', 'R')) ||
|
2014-03-11 15:24:55 +00:00
|
|
|
(AV_RL32(p->buf) == MKTAG('A', 'N', 'I', 'M') &&
|
2012-11-04 16:40:21 +00:00
|
|
|
AV_RL32(p->buf + 8) == MKTAG('A', 'H', 'D', 'R')))) {
|
2012-03-19 02:55:26 +00:00
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smush_read_header(AVFormatContext *ctx)
|
|
|
|
{
|
|
|
|
SMUSHContext *smush = ctx->priv_data;
|
|
|
|
AVIOContext *pb = ctx->pb;
|
|
|
|
AVStream *vst, *ast;
|
|
|
|
uint32_t magic, nframes, size, subversion, i;
|
|
|
|
uint32_t width = 0, height = 0, got_audio = 0, read = 0;
|
|
|
|
uint32_t sample_rate, channels, palette[256];
|
2019-12-10 21:59:53 +00:00
|
|
|
int ret;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
magic = avio_rb32(pb);
|
|
|
|
avio_skip(pb, 4); // skip movie size
|
|
|
|
|
|
|
|
if (magic == MKBETAG('A', 'N', 'I', 'M')) {
|
|
|
|
if (avio_rb32(pb) != MKBETAG('A', 'H', 'D', 'R'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
size = avio_rb32(pb);
|
|
|
|
if (size < 3 * 256 + 6)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
smush->version = 0;
|
|
|
|
subversion = avio_rl16(pb);
|
|
|
|
nframes = avio_rl16(pb);
|
2014-03-11 15:24:55 +00:00
|
|
|
if (!nframes)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
avio_skip(pb, 2); // skip pad
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
palette[i] = avio_rb24(pb);
|
|
|
|
|
|
|
|
avio_skip(pb, size - (3 * 256 + 6));
|
2014-03-11 15:24:55 +00:00
|
|
|
} else if (magic == MKBETAG('S', 'A', 'N', 'M')) {
|
2012-03-19 02:55:26 +00:00
|
|
|
if (avio_rb32(pb) != MKBETAG('S', 'H', 'D', 'R'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
size = avio_rb32(pb);
|
|
|
|
if (size < 14)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
smush->version = 1;
|
2014-03-11 15:24:55 +00:00
|
|
|
subversion = avio_rl16(pb);
|
2012-03-19 02:55:26 +00:00
|
|
|
nframes = avio_rl32(pb);
|
2014-03-11 15:24:55 +00:00
|
|
|
if (!nframes)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-03-19 02:55:26 +00:00
|
|
|
avio_skip(pb, 2); // skip pad
|
|
|
|
width = avio_rl16(pb);
|
|
|
|
height = avio_rl16(pb);
|
|
|
|
avio_skip(pb, 2); // skip pad
|
|
|
|
avio_skip(pb, size - 14);
|
|
|
|
|
|
|
|
if (avio_rb32(pb) != MKBETAG('F', 'L', 'H', 'D'))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
size = avio_rb32(pb);
|
|
|
|
while (!got_audio && ((read + 8) < size)) {
|
|
|
|
uint32_t sig, chunk_size;
|
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(pb))
|
2012-03-19 02:55:26 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
sig = avio_rb32(pb);
|
|
|
|
chunk_size = avio_rb32(pb);
|
2014-03-11 15:24:55 +00:00
|
|
|
read += 8;
|
2012-03-19 02:55:26 +00:00
|
|
|
switch (sig) {
|
|
|
|
case MKBETAG('W', 'a', 'v', 'e'):
|
|
|
|
got_audio = 1;
|
|
|
|
sample_rate = avio_rl32(pb);
|
2014-03-11 15:24:55 +00:00
|
|
|
if (!sample_rate)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
channels = avio_rl32(pb);
|
|
|
|
if (!channels)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-03-19 02:55:26 +00:00
|
|
|
avio_skip(pb, chunk_size - 8);
|
|
|
|
read += chunk_size;
|
|
|
|
break;
|
|
|
|
case MKBETAG('B', 'l', '1', '6'):
|
|
|
|
case MKBETAG('A', 'N', 'N', 'O'):
|
|
|
|
avio_skip(pb, chunk_size);
|
|
|
|
read += chunk_size;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
avio_skip(pb, size - read);
|
|
|
|
} else {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Wrong magic\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
vst = avformat_new_stream(ctx, 0);
|
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
smush->video_stream_index = vst->index;
|
|
|
|
|
2014-03-11 15:24:55 +00:00
|
|
|
avpriv_set_pts_info(vst, 64, 1, 15);
|
|
|
|
|
2012-03-19 02:55:26 +00:00
|
|
|
vst->start_time = 0;
|
|
|
|
vst->duration =
|
|
|
|
vst->nb_frames = nframes;
|
2014-03-11 15:24:55 +00:00
|
|
|
vst->avg_frame_rate = av_inv_q(vst->time_base);
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_SANM;
|
|
|
|
vst->codecpar->codec_tag = 0;
|
|
|
|
vst->codecpar->width = width;
|
|
|
|
vst->codecpar->height = height;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
if (!smush->version) {
|
2019-12-10 21:59:53 +00:00
|
|
|
if ((ret = ff_alloc_extradata(vst->codecpar, 1024 + 2)) < 0)
|
|
|
|
return ret;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
AV_WL16(vst->codecpar->extradata, subversion);
|
2012-03-19 02:55:26 +00:00
|
|
|
for (i = 0; i < 256; i++)
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
AV_WL32(vst->codecpar->extradata + 2 + i * 4, palette[i]);
|
2012-03-19 02:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (got_audio) {
|
|
|
|
ast = avformat_new_stream(ctx, 0);
|
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
smush->audio_stream_index = ast->index;
|
|
|
|
|
|
|
|
ast->start_time = 0;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
ast->codecpar->codec_id = AV_CODEC_ID_ADPCM_VIMA;
|
|
|
|
ast->codecpar->codec_tag = 0;
|
|
|
|
ast->codecpar->sample_rate = sample_rate;
|
2024-04-25 09:18:18 +00:00
|
|
|
ast->codecpar->ch_layout.nb_channels = channels;
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
|
2012-03-19 02:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int smush_read_packet(AVFormatContext *ctx, AVPacket *pkt)
|
|
|
|
{
|
|
|
|
SMUSHContext *smush = ctx->priv_data;
|
|
|
|
AVIOContext *pb = ctx->pb;
|
|
|
|
int done = 0;
|
2014-03-11 15:24:55 +00:00
|
|
|
int ret;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
uint32_t sig, size;
|
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(pb))
|
2012-03-19 02:55:26 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2014-03-11 15:24:55 +00:00
|
|
|
sig = avio_rb32(pb);
|
|
|
|
size = avio_rb32(pb);
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
switch (sig) {
|
|
|
|
case MKBETAG('F', 'R', 'M', 'E'):
|
|
|
|
if (smush->version)
|
|
|
|
break;
|
2014-03-11 15:24:55 +00:00
|
|
|
if ((ret = av_get_packet(pb, pkt, size)) < 0)
|
|
|
|
return ret;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
pkt->stream_index = smush->video_stream_index;
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
case MKBETAG('B', 'l', '1', '6'):
|
2014-03-11 15:24:55 +00:00
|
|
|
if ((ret = av_get_packet(pb, pkt, size)) < 0)
|
|
|
|
return ret;
|
2012-03-19 02:55:26 +00:00
|
|
|
|
|
|
|
pkt->stream_index = smush->video_stream_index;
|
|
|
|
pkt->duration = 1;
|
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
case MKBETAG('W', 'a', 'v', 'e'):
|
2012-06-21 12:09:01 +00:00
|
|
|
if (size < 13)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-11-17 19:58:54 +00:00
|
|
|
if (av_get_packet(pb, pkt, size) < 13)
|
2012-03-19 02:55:26 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
pkt->stream_index = smush->audio_stream_index;
|
2012-07-08 20:47:34 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2014-03-11 15:24:55 +00:00
|
|
|
pkt->duration = AV_RB32(pkt->data);
|
2012-06-21 12:09:01 +00:00
|
|
|
if (pkt->duration == 0xFFFFFFFFu)
|
|
|
|
pkt->duration = AV_RB32(pkt->data + 8);
|
2012-03-19 02:55:26 +00:00
|
|
|
done = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
avio_skip(pb, size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-04-25 09:18:18 +00:00
|
|
|
const AVInputFormat ff_smush_demuxer = {
|
2012-03-19 02:55:26 +00:00
|
|
|
.name = "smush",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("LucasArts Smush"),
|
|
|
|
.priv_data_size = sizeof(SMUSHContext),
|
|
|
|
.read_probe = smush_read_probe,
|
|
|
|
.read_header = smush_read_header,
|
|
|
|
.read_packet = smush_read_packet,
|
|
|
|
};
|