anm.c 6.38 KB
Newer Older
1 2 3 4
/*
 * Deluxe Paint Animation demuxer
 * Copyright (c) 2009 Peter Ross
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9 10 11
 * 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.
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19 20 21 22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24 25 26 27 28
 * Deluxe Paint Animation demuxer
 */

#include "libavutil/intreadwrite.h"
#include "avformat.h"
29
#include "internal.h"
30

31
typedef struct Page {
32 33 34 35 36
    int base_record;
    unsigned int nb_records;
    int size;
} Page;

37
typedef struct AnmDemuxContext {
38 39
    unsigned int nb_pages;    /**< total pages in file */
    unsigned int nb_records;  /**< total records in file */
40
    int page_table_offset;
41 42 43 44
#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) */
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
} 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;
}

79
static int read_header(AVFormatContext *s)
80 81
{
    AnmDemuxContext *anm = s->priv_data;
82
    AVIOContext *pb = s->pb;
83 84 85
    AVStream *st;
    int i, ret;

86
    avio_skip(pb, 4); /* magic number */
87
    if (avio_rl16(pb) != MAX_PAGES) {
88
        avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
89
        return AVERROR_PATCHWELCOME;
90 91
    }

92 93
    anm->nb_pages   = avio_rl16(pb);
    anm->nb_records = avio_rl32(pb);
94
    avio_skip(pb, 2); /* max records per page */
95 96
    anm->page_table_offset = avio_rl16(pb);
    if (avio_rl32(pb) != ANIM_TAG)
97 98 99
        return AVERROR_INVALIDDATA;

    /* video stream */
100
    st = avformat_new_stream(s, NULL);
101 102
    if (!st)
        return AVERROR(ENOMEM);
103 104 105 106 107
    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);
108
    if (avio_r8(pb) != 0)
109
        goto invalid;
110
    avio_skip(pb, 1); /* frame rate multiplier info */
111 112

    /* ignore last delta record (used for looping) */
113
    if (avio_r8(pb))  /* has_last_delta */
114 115
        anm->nb_records = FFMAX(anm->nb_records - 1, 0);

116
    avio_skip(pb, 1); /* last_delta_valid */
117

118
    if (avio_r8(pb) != 0)
119 120
        goto invalid;

121
    if (avio_r8(pb) != 1)
122 123
        goto invalid;

124
    avio_skip(pb, 1); /* other recs per frame */
125

126
    if (avio_r8(pb) != 1)
127 128
        goto invalid;

129
    avio_skip(pb, 32); /* record_types */
130
    st->nb_frames = avio_rl32(pb);
131
    avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
132
    avio_skip(pb, 58);
133 134

    /* color cycling and palette data */
135 136 137
    st->codecpar->extradata_size = 16*8 + 4*256;
    st->codecpar->extradata      = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
    if (!st->codecpar->extradata) {
138
        return AVERROR(ENOMEM);
139
    }
140
    ret = avio_read(pb, st->codecpar->extradata, st->codecpar->extradata_size);
141
    if (ret < 0)
142
        return ret;
143 144

    /* read page table */
145
    ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
146
    if (ret < 0)
147
        return ret;
148 149 150

    for (i = 0; i < MAX_PAGES; i++) {
        Page *p = &anm->pt[i];
151 152 153
        p->base_record = avio_rl16(pb);
        p->nb_records  = avio_rl16(pb);
        p->size        = avio_rl16(pb);
154 155 156 157 158
    }

    /* find page of first frame */
    anm->page = find_record(anm, 0);
    if (anm->page < 0) {
159
        return anm->page;
160 161 162 163 164 165
    }

    anm->record = -1;
    return 0;

invalid:
166
    avpriv_request_sample(s, "Invalid header element");
167
    return AVERROR_PATCHWELCOME;
168 169 170 171 172 173
}

static int read_packet(AVFormatContext *s,
                       AVPacket *pkt)
{
    AnmDemuxContext *anm = s->priv_data;
174
    AVIOContext *pb = s->pb;
175 176 177
    Page *p;
    int tmp, record_size;

Anton Khirnov's avatar
Anton Khirnov committed
178
    if (s->pb->eof_reached)
179 180 181
        return AVERROR(EIO);

    if (anm->page < 0)
182
        return anm->page;
183 184 185 186 187 188

repeat:
    p = &anm->pt[anm->page];

    /* parse page header */
    if (anm->record < 0) {
189
        avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
190
        avio_skip(pb, 8 + 2*p->nb_records);
191 192 193 194 195 196 197 198 199 200 201 202 203 204
        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 */
205
    tmp = avio_tell(pb);
206
    avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
207
              8 + anm->record * 2, SEEK_SET);
208
    record_size = avio_rl16(pb);
209
    avio_seek(pb, tmp, SEEK_SET);
210 211 212 213 214 215

    /* 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)
216
        pkt->flags |= AV_PKT_FLAG_KEY;
217 218 219 220 221

    anm->record++;
    return 0;
}

222
AVInputFormat ff_anm_demuxer = {
223 224 225 226 227 228
    .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,
229
};