• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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         ret = AVERROR(ENOMEM);
00140         goto fail;
00141     }
00142     ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00143     if (ret < 0)
00144         goto fail;
00145 
00146     /* read page table */
00147     ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
00148     if (ret < 0)
00149         goto fail;
00150 
00151     for (i = 0; i < MAX_PAGES; i++) {
00152         Page *p = &anm->pt[i];
00153         p->base_record = avio_rl16(pb);
00154         p->nb_records  = avio_rl16(pb);
00155         p->size        = avio_rl16(pb);
00156     }
00157 
00158     /* find page of first frame */
00159     anm->page = find_record(anm, 0);
00160     if (anm->page < 0) {
00161         ret = anm->page;
00162         goto fail;
00163     }
00164 
00165     anm->record = -1;
00166     return 0;
00167 
00168 invalid:
00169     av_log_ask_for_sample(s, NULL);
00170     ret = AVERROR_INVALIDDATA;
00171 
00172 fail:
00173     return ret;
00174 }
00175 
00176 static int read_packet(AVFormatContext *s,
00177                        AVPacket *pkt)
00178 {
00179     AnmDemuxContext *anm = s->priv_data;
00180     AVIOContext *pb = s->pb;
00181     Page *p;
00182     int tmp, record_size;
00183 
00184     if (s->pb->eof_reached)
00185         return AVERROR(EIO);
00186 
00187     if (anm->page < 0)
00188         return anm->page;
00189 
00190 repeat:
00191     p = &anm->pt[anm->page];
00192 
00193     /* parse page header */
00194     if (anm->record < 0) {
00195         avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
00196         avio_skip(pb, 8 + 2*p->nb_records);
00197         anm->record = 0;
00198     }
00199 
00200     /* if we have fetched all records in this page, then find the
00201        next page and repeat */
00202     if (anm->record >= p->nb_records) {
00203         anm->page = find_record(anm, p->base_record + p->nb_records);
00204         if (anm->page < 0)
00205             return anm->page;
00206         anm->record = -1;
00207         goto repeat;
00208     }
00209 
00210     /* fetch record size */
00211     tmp = avio_tell(pb);
00212     avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
00213               8 + anm->record * 2, SEEK_SET);
00214     record_size = avio_rl16(pb);
00215     avio_seek(pb, tmp, SEEK_SET);
00216 
00217     /* fetch record */
00218     pkt->size = av_get_packet(s->pb, pkt, record_size);
00219     if (pkt->size < 0)
00220         return pkt->size;
00221     if (p->base_record + anm->record == 0)
00222         pkt->flags |= AV_PKT_FLAG_KEY;
00223 
00224     anm->record++;
00225     return 0;
00226 }
00227 
00228 AVInputFormat ff_anm_demuxer = {
00229     .name           = "anm",
00230     .long_name      = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
00231     .priv_data_size = sizeof(AnmDemuxContext),
00232     .read_probe     = probe,
00233     .read_header    = read_header,
00234     .read_packet    = read_packet,
00235 };
Generated on Sun Apr 22 2012 21:53:59 for Libav by doxygen 1.7.1