• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/anm.c

Go to the documentation of this file.
00001 /*
00002  * Deluxe Paint Animation demuxer
00003  * Copyright (c) 2009 Peter Ross
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "libavutil/intreadwrite.h"
00028 #include "avformat.h"
00029 #include "internal.h"
00030 
00031 typedef struct {
00032     int base_record;
00033     unsigned int nb_records;
00034     int size;
00035 } Page;
00036 
00037 typedef struct {
00038     unsigned int nb_pages;    
00039     unsigned int nb_records;  
00040     int page_table_offset;
00041 #define MAX_PAGES  256        
00042     Page pt[MAX_PAGES];       
00043     int page;                 
00044     int record;               
00045 } AnmDemuxContext;
00046 
00047 #define LPF_TAG  MKTAG('L','P','F',' ')
00048 #define ANIM_TAG MKTAG('A','N','I','M')
00049 
00050 static int probe(AVProbeData *p)
00051 {
00052     /* verify tags and video dimensions */
00053     if (AV_RL32(&p->buf[0])  == LPF_TAG &&
00054         AV_RL32(&p->buf[16]) == ANIM_TAG &&
00055         AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
00056         return AVPROBE_SCORE_MAX;
00057     return 0;
00058 }
00059 
00063 static int find_record(const AnmDemuxContext *anm, int record)
00064 {
00065     int i;
00066 
00067     if (record >= anm->nb_records)
00068         return AVERROR_EOF;
00069 
00070     for (i = 0; i < MAX_PAGES; i++) {
00071         const Page *p = &anm->pt[i];
00072         if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
00073             return i;
00074     }
00075 
00076     return AVERROR_INVALIDDATA;
00077 }
00078 
00079 static int read_header(AVFormatContext *s,
00080                        AVFormatParameters *ap)
00081 {
00082     AnmDemuxContext *anm = s->priv_data;
00083     AVIOContext *pb = s->pb;
00084     AVStream *st;
00085     int i, ret;
00086 
00087     avio_skip(pb, 4); /* magic number */
00088     if (avio_rl16(pb) != MAX_PAGES) {
00089         av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
00090         return AVERROR_INVALIDDATA;
00091     }
00092 
00093     anm->nb_pages   = avio_rl16(pb);
00094     anm->nb_records = avio_rl32(pb);
00095     avio_skip(pb, 2); /* max records per page */
00096     anm->page_table_offset = avio_rl16(pb);
00097     if (avio_rl32(pb) != ANIM_TAG)
00098         return AVERROR_INVALIDDATA;
00099 
00100     /* video stream */
00101     st = avformat_new_stream(s, NULL);
00102     if (!st)
00103         return AVERROR(ENOMEM);
00104     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00105     st->codec->codec_id   = CODEC_ID_ANM;
00106     st->codec->codec_tag  = 0; /* no fourcc */
00107     st->codec->width      = avio_rl16(pb);
00108     st->codec->height     = avio_rl16(pb);
00109     if (avio_r8(pb) != 0)
00110         goto invalid;
00111     avio_skip(pb, 1); /* frame rate multiplier info */
00112 
00113     /* ignore last delta record (used for looping) */
00114     if (avio_r8(pb))  /* has_last_delta */
00115         anm->nb_records = FFMAX(anm->nb_records - 1, 0);
00116 
00117     avio_skip(pb, 1); /* last_delta_valid */
00118 
00119     if (avio_r8(pb) != 0)
00120         goto invalid;
00121 
00122     if (avio_r8(pb) != 1)
00123         goto invalid;
00124 
00125     avio_skip(pb, 1); /* other recs per frame */
00126 
00127     if (avio_r8(pb) != 1)
00128         goto invalid;
00129 
00130     avio_skip(pb, 32); /* record_types */
00131     st->nb_frames = avio_rl32(pb);
00132     avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
00133     avio_skip(pb, 58);
00134 
00135     /* color cycling and palette data */
00136     st->codec->extradata_size = 16*8 + 4*256;
00137     st->codec->extradata      = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00138     if (!st->codec->extradata)
00139         return AVERROR(ENOMEM);
00140 
00141     ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00142     if (ret < 0)
00143         return ret;
00144 
00145     /* read page table */
00146     ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
00147     if (ret < 0)
00148         return ret;
00149 
00150     for (i = 0; i < MAX_PAGES; i++) {
00151         Page *p = &anm->pt[i];
00152         p->base_record = avio_rl16(pb);
00153         p->nb_records  = avio_rl16(pb);
00154         p->size        = avio_rl16(pb);
00155     }
00156 
00157     /* find page of first frame */
00158     anm->page = find_record(anm, 0);
00159     if (anm->page < 0)
00160         return anm->page;
00161 
00162     anm->record = -1;
00163     return 0;
00164 
00165 invalid:
00166     av_log_ask_for_sample(s, NULL);
00167     return AVERROR_INVALIDDATA;
00168 }
00169 
00170 static int read_packet(AVFormatContext *s,
00171                        AVPacket *pkt)
00172 {
00173     AnmDemuxContext *anm = s->priv_data;
00174     AVIOContext *pb = s->pb;
00175     Page *p;
00176     int tmp, record_size;
00177 
00178     if (url_feof(s->pb))
00179         return AVERROR(EIO);
00180 
00181     if (anm->page < 0)
00182         return anm->page;
00183 
00184 repeat:
00185     p = &anm->pt[anm->page];
00186 
00187     /* parse page header */
00188     if (anm->record < 0) {
00189         avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
00190         avio_skip(pb, 8 + 2*p->nb_records);
00191         anm->record = 0;
00192     }
00193 
00194     /* if we have fetched all records in this page, then find the
00195        next page and repeat */
00196     if (anm->record >= p->nb_records) {
00197         anm->page = find_record(anm, p->base_record + p->nb_records);
00198         if (anm->page < 0)
00199             return anm->page;
00200         anm->record = -1;
00201         goto repeat;
00202     }
00203 
00204     /* fetch record size */
00205     tmp = avio_tell(pb);
00206     avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
00207               8 + anm->record * 2, SEEK_SET);
00208     record_size = avio_rl16(pb);
00209     avio_seek(pb, tmp, SEEK_SET);
00210 
00211     /* fetch record */
00212     pkt->size = av_get_packet(s->pb, pkt, record_size);
00213     if (pkt->size < 0)
00214         return pkt->size;
00215     if (p->base_record + anm->record == 0)
00216         pkt->flags |= AV_PKT_FLAG_KEY;
00217 
00218     anm->record++;
00219     return 0;
00220 }
00221 
00222 AVInputFormat ff_anm_demuxer = {
00223     .name           = "anm",
00224     .long_name      = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
00225     .priv_data_size = sizeof(AnmDemuxContext),
00226     .read_probe     = probe,
00227     .read_header    = read_header,
00228     .read_packet    = read_packet,
00229 };
Generated on Fri Feb 1 2013 14:34:30 for FFmpeg by doxygen 1.7.1