00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
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     
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 {
00081     AnmDemuxContext *anm = s->priv_data;
00082     AVIOContext *pb = s->pb;
00083     AVStream *st;
00084     int i, ret;
00085 
00086     avio_skip(pb, 4); 
00087     if (avio_rl16(pb) != MAX_PAGES) {
00088         av_log_ask_for_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES) "\n");
00089         return AVERROR_INVALIDDATA;
00090     }
00091 
00092     anm->nb_pages   = avio_rl16(pb);
00093     anm->nb_records = avio_rl32(pb);
00094     avio_skip(pb, 2); 
00095     anm->page_table_offset = avio_rl16(pb);
00096     if (avio_rl32(pb) != ANIM_TAG)
00097         return AVERROR_INVALIDDATA;
00098 
00099     
00100     st = avformat_new_stream(s, NULL);
00101     if (!st)
00102         return AVERROR(ENOMEM);
00103     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00104     st->codec->codec_id   = AV_CODEC_ID_ANM;
00105     st->codec->codec_tag  = 0; 
00106     st->codec->width      = avio_rl16(pb);
00107     st->codec->height     = avio_rl16(pb);
00108     if (avio_r8(pb) != 0)
00109         goto invalid;
00110     avio_skip(pb, 1); 
00111 
00112     
00113     if (avio_r8(pb))  
00114         anm->nb_records = FFMAX(anm->nb_records - 1, 0);
00115 
00116     avio_skip(pb, 1); 
00117 
00118     if (avio_r8(pb) != 0)
00119         goto invalid;
00120 
00121     if (avio_r8(pb) != 1)
00122         goto invalid;
00123 
00124     avio_skip(pb, 1); 
00125 
00126     if (avio_r8(pb) != 1)
00127         goto invalid;
00128 
00129     avio_skip(pb, 32); 
00130     st->nb_frames = avio_rl32(pb);
00131     avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
00132     avio_skip(pb, 58);
00133 
00134     
00135     st->codec->extradata_size = 16*8 + 4*256;
00136     st->codec->extradata      = av_mallocz(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
00137     if (!st->codec->extradata)
00138         return AVERROR(ENOMEM);
00139 
00140     ret = avio_read(pb, st->codec->extradata, st->codec->extradata_size);
00141     if (ret < 0)
00142         return ret;
00143 
00144     
00145     ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
00146     if (ret < 0)
00147         return ret;
00148 
00149     for (i = 0; i < MAX_PAGES; i++) {
00150         Page *p = &anm->pt[i];
00151         p->base_record = avio_rl16(pb);
00152         p->nb_records  = avio_rl16(pb);
00153         p->size        = avio_rl16(pb);
00154     }
00155 
00156     
00157     anm->page = find_record(anm, 0);
00158     if (anm->page < 0)
00159         return anm->page;
00160 
00161     anm->record = -1;
00162     return 0;
00163 
00164 invalid:
00165     av_log_ask_for_sample(s, NULL);
00166     return AVERROR_INVALIDDATA;
00167 }
00168 
00169 static int read_packet(AVFormatContext *s,
00170                        AVPacket *pkt)
00171 {
00172     AnmDemuxContext *anm = s->priv_data;
00173     AVIOContext *pb = s->pb;
00174     Page *p;
00175     int tmp, record_size;
00176 
00177     if (url_feof(s->pb))
00178         return AVERROR(EIO);
00179 
00180     if (anm->page < 0)
00181         return anm->page;
00182 
00183 repeat:
00184     p = &anm->pt[anm->page];
00185 
00186     
00187     if (anm->record < 0) {
00188         avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
00189         avio_skip(pb, 8 + 2*p->nb_records);
00190         anm->record = 0;
00191     }
00192 
00193     
00194 
00195     if (anm->record >= p->nb_records) {
00196         anm->page = find_record(anm, p->base_record + p->nb_records);
00197         if (anm->page < 0)
00198             return anm->page;
00199         anm->record = -1;
00200         goto repeat;
00201     }
00202 
00203     
00204     tmp = avio_tell(pb);
00205     avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
00206               8 + anm->record * 2, SEEK_SET);
00207     record_size = avio_rl16(pb);
00208     avio_seek(pb, tmp, SEEK_SET);
00209 
00210     
00211     pkt->size = av_get_packet(s->pb, pkt, record_size);
00212     if (pkt->size < 0)
00213         return pkt->size;
00214     if (p->base_record + anm->record == 0)
00215         pkt->flags |= AV_PKT_FLAG_KEY;
00216 
00217     anm->record++;
00218     return 0;
00219 }
00220 
00221 AVInputFormat ff_anm_demuxer = {
00222     .name           = "anm",
00223     .long_name      = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
00224     .priv_data_size = sizeof(AnmDemuxContext),
00225     .read_probe     = probe,
00226     .read_header    = read_header,
00227     .read_packet    = read_packet,
00228 };