2010-01-10 05:47:50 +00:00
|
|
|
/*
|
|
|
|
* Deluxe Paint Animation demuxer
|
|
|
|
* Copyright (c) 2009 Peter Ross
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* This file is part of Libav.
|
2010-01-10 05:47:50 +00:00
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is free software; you can redistribute it and/or
|
2010-01-10 05:47:50 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2011-03-18 17:35:10 +00:00
|
|
|
* Libav is distributed in the hope that it will be useful,
|
2010-01-10 05:47:50 +00:00
|
|
|
* 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
|
2011-03-18 17:35:10 +00:00
|
|
|
* License along with Libav; if not, write to the Free Software
|
2010-01-10 05:47:50 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-04-20 14:45:34 +00:00
|
|
|
* @file
|
2010-01-10 05:47:50 +00:00
|
|
|
* Deluxe Paint Animation demuxer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libavutil/intreadwrite.h"
|
|
|
|
#include "avformat.h"
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int base_record;
|
|
|
|
unsigned int nb_records;
|
|
|
|
int size;
|
|
|
|
} Page;
|
|
|
|
|
|
|
|
typedef struct {
|
2010-07-27 15:54:26 +00:00
|
|
|
unsigned int nb_pages; /**< total pages in file */
|
|
|
|
unsigned int nb_records; /**< total records in file */
|
2010-01-10 05:47:50 +00:00
|
|
|
int page_table_offset;
|
2010-07-27 15:54:26 +00: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 05:47:50 +00:00
|
|
|
} AnmDemuxContext;
|
|
|
|
|
|
|
|
#define LPF_TAG MKTAG('L','P','F',' ')
|
|
|
|
#define ANIM_TAG MKTAG('A','N','I','M')
|
|
|
|
|
|
|
|
static int probe(AVProbeData *p)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_header(AVFormatContext *s,
|
|
|
|
AVFormatParameters *ap)
|
|
|
|
{
|
|
|
|
AnmDemuxContext *anm = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2010-01-10 05:47:50 +00:00
|
|
|
AVStream *st;
|
|
|
|
int i, ret;
|
|
|
|
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 4); /* magic number */
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_rl16(pb) != MAX_PAGES) {
|
2010-01-10 05:47:50 +00:00
|
|
|
av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
anm->nb_pages = avio_rl16(pb);
|
|
|
|
anm->nb_records = avio_rl32(pb);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 2); /* max records per page */
|
2011-02-21 15:43:01 +00:00
|
|
|
anm->page_table_offset = avio_rl16(pb);
|
|
|
|
if (avio_rl32(pb) != ANIM_TAG)
|
2010-01-10 05:47:50 +00:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
/* video stream */
|
|
|
|
st = av_new_stream(s, 0);
|
|
|
|
if (!st)
|
|
|
|
return AVERROR(ENOMEM);
|
2010-03-30 23:30:55 +00:00
|
|
|
st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
2010-01-10 05:47:50 +00:00
|
|
|
st->codec->codec_id = CODEC_ID_ANM;
|
|
|
|
st->codec->codec_tag = 0; /* no fourcc */
|
2011-02-21 15:43:01 +00:00
|
|
|
st->codec->width = avio_rl16(pb);
|
|
|
|
st->codec->height = avio_rl16(pb);
|
|
|
|
if (avio_r8(pb) != 0)
|
2010-01-10 05:47:50 +00:00
|
|
|
goto invalid;
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 1); /* frame rate multiplier info */
|
2010-01-10 05:47:50 +00:00
|
|
|
|
|
|
|
/* ignore last delta record (used for looping) */
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_r8(pb)) /* has_last_delta */
|
2010-01-10 05:47:50 +00:00
|
|
|
anm->nb_records = FFMAX(anm->nb_records - 1, 0);
|
|
|
|
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 1); /* last_delta_valid */
|
2010-01-10 05:47:50 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_r8(pb) != 0)
|
2010-01-10 05:47:50 +00:00
|
|
|
goto invalid;
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_r8(pb) != 1)
|
2010-01-10 05:47:50 +00:00
|
|
|
goto invalid;
|
|
|
|
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 1); /* other recs per frame */
|
2010-01-10 05:47:50 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_r8(pb) != 1)
|
2010-01-10 05:47:50 +00:00
|
|
|
goto invalid;
|
|
|
|
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 32); /* record_types */
|
2011-02-21 15:43:01 +00:00
|
|
|
st->nb_frames = avio_rl32(pb);
|
|
|
|
av_set_pts_info(st, 64, 1, avio_rl16(pb));
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 58);
|
2010-01-10 05:47:50 +00:00
|
|
|
|
|
|
|
/* color cycling and palette data */
|
|
|
|
st->codec->extradata_size = 16*8 + 4*256;
|
|
|
|
st->codec->extradata = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!st->codec->extradata) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto close_and_return;
|
|
|
|
}
|
2011-02-21 15:43:01 +00:00
|
|
|
ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
|
2010-01-10 05:47:50 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto close_and_return;
|
|
|
|
|
|
|
|
/* read page table */
|
2011-02-28 13:57:54 +00:00
|
|
|
ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
|
2010-01-10 05:47:50 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto close_and_return;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_PAGES; i++) {
|
|
|
|
Page *p = &anm->pt[i];
|
2011-02-21 15:43:01 +00:00
|
|
|
p->base_record = avio_rl16(pb);
|
|
|
|
p->nb_records = avio_rl16(pb);
|
|
|
|
p->size = avio_rl16(pb);
|
2010-01-10 05:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* find page of first frame */
|
|
|
|
anm->page = find_record(anm, 0);
|
|
|
|
if (anm->page < 0) {
|
|
|
|
ret = anm->page;
|
|
|
|
goto close_and_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
anm->record = -1;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
av_log_ask_for_sample(s, NULL);
|
|
|
|
ret = AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
close_and_return:
|
|
|
|
av_close_input_stream(s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_packet(AVFormatContext *s,
|
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AnmDemuxContext *anm = s->priv_data;
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2010-01-10 05:47:50 +00:00
|
|
|
Page *p;
|
|
|
|
int tmp, record_size;
|
|
|
|
|
2011-03-07 20:50:25 +00:00
|
|
|
if (s->pb->eof_reached)
|
2010-01-10 05:47:50 +00:00
|
|
|
return AVERROR(EIO);
|
|
|
|
|
|
|
|
if (anm->page < 0)
|
2010-02-12 11:31:35 +00:00
|
|
|
return anm->page;
|
2010-01-10 05:47:50 +00:00
|
|
|
|
|
|
|
repeat:
|
|
|
|
p = &anm->pt[anm->page];
|
|
|
|
|
|
|
|
/* parse page header */
|
|
|
|
if (anm->record < 0) {
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, 8 + 2*p->nb_records);
|
2010-01-10 05:47:50 +00: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 19:11:45 +00:00
|
|
|
tmp = avio_tell(pb);
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
|
2010-01-10 05:47:50 +00:00
|
|
|
8 + anm->record * 2, SEEK_SET);
|
2011-02-21 15:43:01 +00:00
|
|
|
record_size = avio_rl16(pb);
|
2011-02-28 13:57:54 +00:00
|
|
|
avio_seek(pb, tmp, SEEK_SET);
|
2010-01-10 05:47:50 +00: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 12:29:58 +00:00
|
|
|
pkt->flags |= AV_PKT_FLAG_KEY;
|
2010-01-10 05:47:50 +00:00
|
|
|
|
|
|
|
anm->record++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-25 22:03:28 +00:00
|
|
|
AVInputFormat ff_anm_demuxer = {
|
2010-01-10 05:47:50 +00:00
|
|
|
"anm",
|
|
|
|
NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
|
|
|
|
sizeof(AnmDemuxContext),
|
|
|
|
probe,
|
|
|
|
read_header,
|
|
|
|
read_packet,
|
|
|
|
};
|