2010-01-10 07:47:50 +02:00
|
|
|
/*
|
|
|
|
* Deluxe Paint Animation demuxer
|
|
|
|
* Copyright (c) 2009 Peter Ross
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 17:45:34 +03:00
|
|
|
* @file
|
2010-01-10 07:47:50 +02:00
|
|
|
* Deluxe Paint Animation demuxer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
2022-05-06 21:28:57 +02:00
|
|
|
#include "demux.h"
|
2011-11-29 21:28:15 +03:00
|
|
|
#include "internal.h"
|
2010-01-10 07:47:50 +02:00
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct Page {
|
2010-01-10 07:47:50 +02:00
|
|
|
int base_record;
|
|
|
|
unsigned int nb_records;
|
|
|
|
int size;
|
|
|
|
} Page;
|
|
|
|
|
2014-09-22 10:19:33 +03:00
|
|
|
typedef struct AnmDemuxContext {
|
2010-07-27 18:54:26 +03:00
|
|
|
unsigned int nb_pages; /**< total pages in file */
|
|
|
|
unsigned int nb_records; /**< total records in file */
|
2010-01-10 07:47:50 +02:00
|
|
|
int page_table_offset;
|
2010-07-27 18:54:26 +03:00
|
|
|
#define MAX_PAGES 256 /**< Deluxe Paint hardcoded value */
|
|
|
|
Page pt[MAX_PAGES]; /**< page table */
|
|
|
|
int page; /**< current page (or AVERROR_xxx code) */
|
|
|
|
int record; /**< current record (with in page) */
|
2010-01-10 07:47:50 +02:00
|
|
|
} AnmDemuxContext;
|
|
|
|
|
|
|
|
#define LPF_TAG MKTAG('L','P','F',' ')
|
|
|
|
#define ANIM_TAG MKTAG('A','N','I','M')
|
|
|
|
|
2019-03-21 02:18:37 +02:00
|
|
|
static int probe(const AVProbeData *p)
|
2010-01-10 07:47:50 +02:00
|
|
|
{
|
|
|
|
/* verify tags and video dimensions */
|
|
|
|
if (AV_RL32(&p->buf[0]) == LPF_TAG &&
|
|
|
|
AV_RL32(&p->buf[16]) == ANIM_TAG &&
|
|
|
|
AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return page containing the requested record or AVERROR_XXX
|
|
|
|
*/
|
|
|
|
static int find_record(const AnmDemuxContext *anm, int record)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (record >= anm->nb_records)
|
|
|
|
return AVERROR_EOF;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_PAGES; i++) {
|
|
|
|
const Page *p = &anm->pt[i];
|
|
|
|
if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2012-01-12 15:20:36 +03:00
|
|
|
static int read_header(AVFormatContext *s)
|
2010-01-10 07:47:50 +02:00
|
|
|
{
|
|
|
|
AnmDemuxContext *anm = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2010-01-10 07:47:50 +02:00
|
|
|
AVStream *st;
|
|
|
|
int i, ret;
|
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 4); /* magic number */
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_rl16(pb) != MAX_PAGES) {
|
2013-02-26 01:54:28 +03:00
|
|
|
avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
|
2012-12-23 20:10:05 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-01-10 07:47:50 +02:00
|
|
|
}
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
anm->nb_pages = avio_rl16(pb);
|
|
|
|
anm->nb_records = avio_rl32(pb);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 2); /* max records per page */
|
2011-02-21 17:43:01 +02:00
|
|
|
anm->page_table_offset = avio_rl16(pb);
|
|
|
|
if (avio_rl32(pb) != ANIM_TAG)
|
2010-01-10 07:47:50 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
/* video stream */
|
2011-06-18 12:43:24 +03:00
|
|
|
st = avformat_new_stream(s, NULL);
|
2010-01-10 07:47:50 +02:00
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
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 21:42:52 +03:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
st->codecpar->codec_id = AV_CODEC_ID_ANM;
|
|
|
|
st->codecpar->codec_tag = 0; /* no fourcc */
|
|
|
|
st->codecpar->width = avio_rl16(pb);
|
|
|
|
st->codecpar->height = avio_rl16(pb);
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(pb) != 0)
|
2010-01-10 07:47:50 +02:00
|
|
|
goto invalid;
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 1); /* frame rate multiplier info */
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
/* ignore last delta record (used for looping) */
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(pb)) /* has_last_delta */
|
2010-01-10 07:47:50 +02:00
|
|
|
anm->nb_records = FFMAX(anm->nb_records - 1, 0);
|
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 1); /* last_delta_valid */
|
2010-01-10 07:47:50 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(pb) != 0)
|
2010-01-10 07:47:50 +02:00
|
|
|
goto invalid;
|
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(pb) != 1)
|
2010-01-10 07:47:50 +02:00
|
|
|
goto invalid;
|
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 1); /* other recs per frame */
|
2010-01-10 07:47:50 +02:00
|
|
|
|
2011-02-21 17:43:01 +02:00
|
|
|
if (avio_r8(pb) != 1)
|
2010-01-10 07:47:50 +02:00
|
|
|
goto invalid;
|
|
|
|
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 32); /* record_types */
|
2011-02-21 17:43:01 +02:00
|
|
|
st->nb_frames = avio_rl32(pb);
|
2011-11-29 21:28:15 +03:00
|
|
|
avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 58);
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
/* color cycling and palette data */
|
2019-01-02 17:28:46 +02:00
|
|
|
ret = ff_get_extradata(s, st->codecpar, s->pb, 16*8 + 4*256);
|
2010-01-10 07:47:50 +02:00
|
|
|
if (ret < 0)
|
2011-10-01 01:44:57 +03:00
|
|
|
return ret;
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
/* read page table */
|
2011-02-28 15:57:54 +02:00
|
|
|
ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
|
2010-01-10 07:47:50 +02:00
|
|
|
if (ret < 0)
|
2011-10-01 01:44:57 +03:00
|
|
|
return ret;
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
for (i = 0; i < MAX_PAGES; i++) {
|
|
|
|
Page *p = &anm->pt[i];
|
2011-02-21 17:43:01 +02:00
|
|
|
p->base_record = avio_rl16(pb);
|
|
|
|
p->nb_records = avio_rl16(pb);
|
|
|
|
p->size = avio_rl16(pb);
|
2010-01-10 07:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find page of first frame */
|
|
|
|
anm->page = find_record(anm, 0);
|
|
|
|
if (anm->page < 0) {
|
2011-10-01 01:44:57 +03:00
|
|
|
return anm->page;
|
2010-01-10 07:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
anm->record = -1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
invalid:
|
2013-02-26 01:54:28 +03:00
|
|
|
avpriv_request_sample(s, "Invalid header element");
|
2012-12-24 17:09:58 +03:00
|
|
|
return AVERROR_PATCHWELCOME;
|
2010-01-10 07:47:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AnmDemuxContext *anm = s->priv_data;
|
2011-02-20 12:04:12 +02:00
|
|
|
AVIOContext *pb = s->pb;
|
2010-01-10 07:47:50 +02:00
|
|
|
Page *p;
|
|
|
|
int tmp, record_size;
|
|
|
|
|
2014-08-07 23:12:41 +03:00
|
|
|
if (avio_feof(s->pb))
|
2023-03-27 09:47:39 +02:00
|
|
|
return AVERROR_EOF;
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
if (anm->page < 0)
|
2010-02-12 13:31:35 +02:00
|
|
|
return anm->page;
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
repeat:
|
|
|
|
p = &anm->pt[anm->page];
|
|
|
|
|
|
|
|
/* parse page header */
|
|
|
|
if (anm->record < 0) {
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
|
2011-03-15 10:14:38 +02:00
|
|
|
avio_skip(pb, 8 + 2*p->nb_records);
|
2010-01-10 07:47:50 +02:00
|
|
|
anm->record = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we have fetched all records in this page, then find the
|
|
|
|
next page and repeat */
|
|
|
|
if (anm->record >= p->nb_records) {
|
|
|
|
anm->page = find_record(anm, p->base_record + p->nb_records);
|
|
|
|
if (anm->page < 0)
|
|
|
|
return anm->page;
|
|
|
|
anm->record = -1;
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fetch record size */
|
2011-03-03 21:11:45 +02:00
|
|
|
tmp = avio_tell(pb);
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
|
2010-01-10 07:47:50 +02:00
|
|
|
8 + anm->record * 2, SEEK_SET);
|
2011-02-21 17:43:01 +02:00
|
|
|
record_size = avio_rl16(pb);
|
2011-02-28 15:57:54 +02:00
|
|
|
avio_seek(pb, tmp, SEEK_SET);
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
/* fetch record */
|
|
|
|
pkt->size = av_get_packet(s->pb, pkt, record_size);
|
|
|
|
if (pkt->size < 0)
|
|
|
|
return pkt->size;
|
|
|
|
if (p->base_record + anm->record == 0)
|
2010-03-31 15:29:58 +03:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2010-01-10 07:47:50 +02:00
|
|
|
|
|
|
|
anm->record++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 19:45:24 +02:00
|
|
|
const AVInputFormat ff_anm_demuxer = {
|
2011-07-16 23:18:12 +03:00
|
|
|
.name = "anm",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
|
|
|
|
.priv_data_size = sizeof(AnmDemuxContext),
|
|
|
|
.read_probe = probe,
|
|
|
|
.read_header = read_header,
|
|
|
|
.read_packet = read_packet,
|
2010-01-10 07:47:50 +02:00
|
|
|
};
|