2012-03-27 19:27:53 +00:00
|
|
|
/*
|
|
|
|
* Packed Animation File demuxer
|
|
|
|
* Copyright (c) 2012 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
|
|
|
|
*/
|
|
|
|
|
2012-11-13 13:25:19 +00:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-03-27 19:27:53 +00:00
|
|
|
#include "libavcodec/paf.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
#include "internal.h"
|
|
|
|
|
|
|
|
#define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
|
|
|
|
|
2014-09-22 07:19:33 +00:00
|
|
|
typedef struct PAFDemuxContext {
|
2012-03-27 19:27:53 +00:00
|
|
|
uint32_t buffer_size;
|
|
|
|
uint32_t frame_blks;
|
|
|
|
uint32_t nb_frames;
|
|
|
|
uint32_t start_offset;
|
|
|
|
uint32_t preload_count;
|
|
|
|
uint32_t max_video_blks;
|
|
|
|
uint32_t max_audio_blks;
|
|
|
|
|
|
|
|
uint32_t current_frame;
|
|
|
|
uint32_t current_frame_count;
|
|
|
|
uint32_t current_frame_block;
|
|
|
|
|
|
|
|
uint32_t *blocks_count_table;
|
|
|
|
uint32_t *frames_offset_table;
|
|
|
|
uint32_t *blocks_offset_table;
|
|
|
|
|
|
|
|
uint8_t *video_frame;
|
2014-03-21 16:28:47 +00:00
|
|
|
int video_size;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
uint8_t *audio_frame;
|
|
|
|
uint8_t *temp_audio_frame;
|
2014-03-21 16:28:47 +00:00
|
|
|
int audio_size;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
2014-03-21 16:28:47 +00:00
|
|
|
int got_audio;
|
2012-03-27 19:27:53 +00:00
|
|
|
} PAFDemuxContext;
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int read_probe(const AVProbeData *p)
|
2012-03-27 19:27:53 +00:00
|
|
|
{
|
|
|
|
if ((p->buf_size >= strlen(MAGIC)) &&
|
|
|
|
!memcmp(p->buf, MAGIC, strlen(MAGIC)))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_close(AVFormatContext *s)
|
|
|
|
{
|
|
|
|
PAFDemuxContext *p = s->priv_data;
|
|
|
|
|
|
|
|
av_freep(&p->blocks_count_table);
|
|
|
|
av_freep(&p->frames_offset_table);
|
|
|
|
av_freep(&p->blocks_offset_table);
|
|
|
|
av_freep(&p->video_frame);
|
|
|
|
av_freep(&p->audio_frame);
|
|
|
|
av_freep(&p->temp_audio_frame);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-24 18:30:48 +00:00
|
|
|
static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
|
2012-03-27 19:27:53 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-10-24 18:30:48 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (avio_feof(s->pb))
|
|
|
|
return AVERROR_INVALIDDATA;
|
2012-03-27 19:27:53 +00:00
|
|
|
table[i] = avio_rl32(s->pb);
|
2020-10-24 18:30:48 +00:00
|
|
|
}
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
|
2020-10-24 18:30:48 +00:00
|
|
|
return 0;
|
2012-03-27 19:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_header(AVFormatContext *s)
|
|
|
|
{
|
2014-03-21 16:28:47 +00:00
|
|
|
PAFDemuxContext *p = s->priv_data;
|
2012-03-27 19:27:53 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
AVStream *ast, *vst;
|
2019-02-22 04:59:04 +00:00
|
|
|
int frame_ms, ret = 0;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
avio_skip(pb, 132);
|
|
|
|
|
|
|
|
vst = avformat_new_stream(s, 0);
|
|
|
|
if (!vst)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
vst->start_time = 0;
|
|
|
|
vst->nb_frames =
|
|
|
|
vst->duration =
|
2014-03-21 16:28:47 +00:00
|
|
|
p->nb_frames = avio_rl32(pb);
|
2019-02-22 04:59:04 +00:00
|
|
|
frame_ms = avio_rl32(pb);
|
|
|
|
if (frame_ms < 1)
|
|
|
|
return AVERROR_INVALIDDATA;
|
2014-03-21 16:28:47 +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
|
|
|
vst->codecpar->width = avio_rl32(pb);
|
|
|
|
vst->codecpar->height = avio_rl32(pb);
|
2012-03-27 19:27:53 +00:00
|
|
|
avio_skip(pb, 4);
|
2014-03-21 16:28:47 +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
|
|
|
vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
vst->codecpar->codec_tag = 0;
|
|
|
|
vst->codecpar->codec_id = AV_CODEC_ID_PAF_VIDEO;
|
2019-02-22 04:59:04 +00:00
|
|
|
avpriv_set_pts_info(vst, 64, frame_ms, 1000);
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
ast = avformat_new_stream(s, 0);
|
|
|
|
if (!ast)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2014-03-21 16:28:47 +00:00
|
|
|
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_tag = 0;
|
|
|
|
ast->codecpar->codec_id = AV_CODEC_ID_PAF_AUDIO;
|
2017-03-31 16:05:19 +00:00
|
|
|
ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
|
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->sample_rate = 22050;
|
2012-03-27 19:27:53 +00:00
|
|
|
avpriv_set_pts_info(ast, 64, 1, 22050);
|
|
|
|
|
|
|
|
p->buffer_size = avio_rl32(pb);
|
|
|
|
p->preload_count = avio_rl32(pb);
|
|
|
|
p->frame_blks = avio_rl32(pb);
|
|
|
|
p->start_offset = avio_rl32(pb);
|
|
|
|
p->max_video_blks = avio_rl32(pb);
|
|
|
|
p->max_audio_blks = avio_rl32(pb);
|
2021-01-19 23:11:34 +00:00
|
|
|
|
|
|
|
if (avio_feof(pb))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-03-27 19:27:53 +00:00
|
|
|
if (p->buffer_size < 175 ||
|
|
|
|
p->max_audio_blks < 2 ||
|
|
|
|
p->max_video_blks < 1 ||
|
|
|
|
p->frame_blks < 1 ||
|
|
|
|
p->nb_frames < 1 ||
|
|
|
|
p->preload_count < 1 ||
|
|
|
|
p->buffer_size > 2048 ||
|
|
|
|
p->max_video_blks > 2048 ||
|
|
|
|
p->max_audio_blks > 2048 ||
|
|
|
|
p->nb_frames > INT_MAX / sizeof(uint32_t) ||
|
|
|
|
p->frame_blks > INT_MAX / sizeof(uint32_t))
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2021-01-19 23:32:22 +00:00
|
|
|
p->blocks_count_table = av_malloc_array(p->nb_frames,
|
2014-03-21 16:28:47 +00:00
|
|
|
sizeof(*p->blocks_count_table));
|
2021-01-19 23:32:22 +00:00
|
|
|
p->frames_offset_table = av_malloc_array(p->nb_frames,
|
2014-03-21 16:28:47 +00:00
|
|
|
sizeof(*p->frames_offset_table));
|
2021-01-19 23:32:22 +00:00
|
|
|
p->blocks_offset_table = av_malloc_array(p->frame_blks,
|
2014-03-21 16:28:47 +00:00
|
|
|
sizeof(*p->blocks_offset_table));
|
2012-03-27 19:27:53 +00:00
|
|
|
|
2014-03-21 16:28:47 +00:00
|
|
|
p->video_size = p->max_video_blks * p->buffer_size;
|
|
|
|
p->video_frame = av_mallocz(p->video_size);
|
2012-03-27 19:27:53 +00:00
|
|
|
|
2014-03-21 16:28:47 +00:00
|
|
|
p->audio_size = p->max_audio_blks * p->buffer_size;
|
|
|
|
p->audio_frame = av_mallocz(p->audio_size);
|
|
|
|
p->temp_audio_frame = av_mallocz(p->audio_size);
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
if (!p->blocks_count_table ||
|
|
|
|
!p->frames_offset_table ||
|
|
|
|
!p->blocks_offset_table ||
|
|
|
|
!p->video_frame ||
|
|
|
|
!p->audio_frame ||
|
2020-03-21 17:31:06 +00:00
|
|
|
!p->temp_audio_frame)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
avio_seek(pb, p->buffer_size, SEEK_SET);
|
|
|
|
|
2020-10-24 18:30:48 +00:00
|
|
|
ret = read_table(s, p->blocks_count_table, p->nb_frames);
|
|
|
|
if (ret < 0)
|
2020-03-21 17:31:06 +00:00
|
|
|
return ret;
|
2020-10-24 18:30:48 +00:00
|
|
|
ret = read_table(s, p->frames_offset_table, p->nb_frames);
|
|
|
|
if (ret < 0)
|
2020-03-21 17:31:06 +00:00
|
|
|
return ret;
|
2020-10-24 18:30:48 +00:00
|
|
|
ret = read_table(s, p->blocks_offset_table, p->frame_blks);
|
|
|
|
if (ret < 0)
|
2020-03-21 17:31:06 +00:00
|
|
|
return ret;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
p->got_audio = 0;
|
|
|
|
p->current_frame = 0;
|
|
|
|
p->current_frame_block = 0;
|
|
|
|
|
|
|
|
avio_seek(pb, p->start_offset, SEEK_SET);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s, AVPacket *pkt)
|
|
|
|
{
|
2014-03-21 16:28:47 +00:00
|
|
|
PAFDemuxContext *p = s->priv_data;
|
2012-03-27 19:27:53 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
|
|
|
uint32_t count, offset;
|
2019-12-10 21:59:53 +00:00
|
|
|
int size, i, ret;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
if (p->current_frame >= p->nb_frames)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(pb))
|
2012-03-27 19:27:53 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
if (p->got_audio) {
|
2019-12-10 21:59:53 +00:00
|
|
|
if ((ret = av_new_packet(pkt, p->audio_size)) < 0)
|
|
|
|
return ret;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
|
2012-08-03 15:13:45 +00:00
|
|
|
pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
|
2012-03-27 19:27:53 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
pkt->stream_index = 1;
|
|
|
|
p->got_audio = 0;
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2014-03-21 16:28:47 +00:00
|
|
|
count = (p->current_frame == 0) ? p->preload_count
|
|
|
|
: p->blocks_count_table[p->current_frame - 1];
|
2012-03-27 19:27:53 +00:00
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
if (p->current_frame_block >= p->frame_blks)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2012-10-14 02:01:41 +00:00
|
|
|
offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
|
|
|
|
if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
|
2012-03-27 19:27:53 +00:00
|
|
|
if (offset > p->audio_size - p->buffer_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_read(pb, p->audio_frame + offset, p->buffer_size);
|
|
|
|
if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
|
|
|
|
memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
|
|
|
|
p->got_audio = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (offset > p->video_size - p->buffer_size)
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
avio_read(pb, p->video_frame + offset, p->buffer_size);
|
|
|
|
}
|
|
|
|
p->current_frame_block++;
|
|
|
|
}
|
|
|
|
|
2013-08-09 11:23:10 +00:00
|
|
|
if (p->frames_offset_table[p->current_frame] >= p->video_size)
|
2012-03-27 19:27:53 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
2013-08-09 11:23:10 +00:00
|
|
|
size = p->video_size - p->frames_offset_table[p->current_frame];
|
|
|
|
|
2019-12-10 21:59:53 +00:00
|
|
|
if ((ret = av_new_packet(pkt, size)) < 0)
|
|
|
|
return ret;
|
2012-03-27 19:27:53 +00:00
|
|
|
|
|
|
|
pkt->stream_index = 0;
|
|
|
|
pkt->duration = 1;
|
|
|
|
memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
|
2012-07-08 20:35:34 +00:00
|
|
|
if (pkt->data[0] & 0x20)
|
2014-03-21 16:28:47 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2012-03-27 19:27:53 +00:00
|
|
|
p->current_frame++;
|
|
|
|
|
|
|
|
return pkt->size;
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVInputFormat ff_paf_demuxer = {
|
2012-03-27 19:27:53 +00:00
|
|
|
.name = "paf",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
|
|
|
|
.priv_data_size = sizeof(PAFDemuxContext),
|
2020-03-21 17:31:06 +00:00
|
|
|
.flags_internal = FF_FMT_INIT_CLEANUP,
|
2012-03-27 19:27:53 +00:00
|
|
|
.read_probe = read_probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
|
|
|
.read_close = read_close,
|
|
|
|
};
|