lavf: replace AVStream.codec with AVStream.codecpar
[libav.git] / libavformat / anm.c
CommitLineData
e6565055
PR
1/*
2 * Deluxe Paint Animation demuxer
3 * Copyright (c) 2009 Peter Ross
4 *
2912e87a 5 * This file is part of Libav.
e6565055 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
e6565055
PR
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
e6565055
PR
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
e6565055
PR
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
e6565055
PR
24 * Deluxe Paint Animation demuxer
25 */
26
27#include "libavutil/intreadwrite.h"
28#include "avformat.h"
c3f9ebf7 29#include "internal.h"
e6565055 30
daf8cf35 31typedef struct Page {
e6565055
PR
32 int base_record;
33 unsigned int nb_records;
34 int size;
35} Page;
36
daf8cf35 37typedef struct AnmDemuxContext {
a20df858
MN
38 unsigned int nb_pages; /**< total pages in file */
39 unsigned int nb_records; /**< total records in file */
e6565055 40 int page_table_offset;
a20df858
MN
41#define MAX_PAGES 256 /**< Deluxe Paint hardcoded value */
42 Page pt[MAX_PAGES]; /**< page table */
43 int page; /**< current page (or AVERROR_xxx code) */
44 int record; /**< current record (with in page) */
e6565055
PR
45} AnmDemuxContext;
46
47#define LPF_TAG MKTAG('L','P','F',' ')
48#define ANIM_TAG MKTAG('A','N','I','M')
49
50static int probe(AVProbeData *p)
51{
52 /* verify tags and video dimensions */
53 if (AV_RL32(&p->buf[0]) == LPF_TAG &&
54 AV_RL32(&p->buf[16]) == ANIM_TAG &&
55 AV_RL16(&p->buf[20]) && AV_RL16(&p->buf[22]))
56 return AVPROBE_SCORE_MAX;
57 return 0;
58}
59
60/**
61 * @return page containing the requested record or AVERROR_XXX
62 */
63static int find_record(const AnmDemuxContext *anm, int record)
64{
65 int i;
66
67 if (record >= anm->nb_records)
68 return AVERROR_EOF;
69
70 for (i = 0; i < MAX_PAGES; i++) {
71 const Page *p = &anm->pt[i];
72 if (p->nb_records > 0 && record >= p->base_record && record < p->base_record + p->nb_records)
73 return i;
74 }
75
76 return AVERROR_INVALIDDATA;
77}
78
6e9651d1 79static int read_header(AVFormatContext *s)
e6565055
PR
80{
81 AnmDemuxContext *anm = s->priv_data;
ae628ec1 82 AVIOContext *pb = s->pb;
e6565055
PR
83 AVStream *st;
84 int i, ret;
85
45a8a02a 86 avio_skip(pb, 4); /* magic number */
b7effd4e 87 if (avio_rl16(pb) != MAX_PAGES) {
1ecdf891 88 avpriv_request_sample(s, "max_pages != " AV_STRINGIFY(MAX_PAGES));
f3298f12 89 return AVERROR_PATCHWELCOME;
e6565055
PR
90 }
91
b7effd4e
AK
92 anm->nb_pages = avio_rl16(pb);
93 anm->nb_records = avio_rl32(pb);
45a8a02a 94 avio_skip(pb, 2); /* max records per page */
b7effd4e
AK
95 anm->page_table_offset = avio_rl16(pb);
96 if (avio_rl32(pb) != ANIM_TAG)
e6565055
PR
97 return AVERROR_INVALIDDATA;
98
99 /* video stream */
3b3bbdd3 100 st = avformat_new_stream(s, NULL);
e6565055
PR
101 if (!st)
102 return AVERROR(ENOMEM);
9200514a
AK
103 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
104 st->codecpar->codec_id = AV_CODEC_ID_ANM;
105 st->codecpar->codec_tag = 0; /* no fourcc */
106 st->codecpar->width = avio_rl16(pb);
107 st->codecpar->height = avio_rl16(pb);
b7effd4e 108 if (avio_r8(pb) != 0)
e6565055 109 goto invalid;
45a8a02a 110 avio_skip(pb, 1); /* frame rate multiplier info */
e6565055
PR
111
112 /* ignore last delta record (used for looping) */
b7effd4e 113 if (avio_r8(pb)) /* has_last_delta */
e6565055
PR
114 anm->nb_records = FFMAX(anm->nb_records - 1, 0);
115
45a8a02a 116 avio_skip(pb, 1); /* last_delta_valid */
e6565055 117
b7effd4e 118 if (avio_r8(pb) != 0)
e6565055
PR
119 goto invalid;
120
b7effd4e 121 if (avio_r8(pb) != 1)
e6565055
PR
122 goto invalid;
123
45a8a02a 124 avio_skip(pb, 1); /* other recs per frame */
e6565055 125
b7effd4e 126 if (avio_r8(pb) != 1)
e6565055
PR
127 goto invalid;
128
45a8a02a 129 avio_skip(pb, 32); /* record_types */
b7effd4e 130 st->nb_frames = avio_rl32(pb);
c3f9ebf7 131 avpriv_set_pts_info(st, 64, 1, avio_rl16(pb));
45a8a02a 132 avio_skip(pb, 58);
e6565055
PR
133
134 /* color cycling and palette data */
9200514a
AK
135 st->codecpar->extradata_size = 16*8 + 4*256;
136 st->codecpar->extradata = av_mallocz(st->codecpar->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
137 if (!st->codecpar->extradata) {
8f10f1a6 138 return AVERROR(ENOMEM);
e6565055 139 }
9200514a 140 ret = avio_read(pb, st->codecpar->extradata, st->codecpar->extradata_size);
e6565055 141 if (ret < 0)
8f10f1a6 142 return ret;
e6565055
PR
143
144 /* read page table */
6b4aa5da 145 ret = avio_seek(pb, anm->page_table_offset, SEEK_SET);
e6565055 146 if (ret < 0)
8f10f1a6 147 return ret;
e6565055
PR
148
149 for (i = 0; i < MAX_PAGES; i++) {
150 Page *p = &anm->pt[i];
b7effd4e
AK
151 p->base_record = avio_rl16(pb);
152 p->nb_records = avio_rl16(pb);
153 p->size = avio_rl16(pb);
e6565055
PR
154 }
155
156 /* find page of first frame */
157 anm->page = find_record(anm, 0);
158 if (anm->page < 0) {
8f10f1a6 159 return anm->page;
e6565055
PR
160 }
161
162 anm->record = -1;
163 return 0;
164
165invalid:
1ecdf891 166 avpriv_request_sample(s, "Invalid header element");
8f10f1a6 167 return AVERROR_PATCHWELCOME;
e6565055
PR
168}
169
170static int read_packet(AVFormatContext *s,
171 AVPacket *pkt)
172{
173 AnmDemuxContext *anm = s->priv_data;
ae628ec1 174 AVIOContext *pb = s->pb;
e6565055
PR
175 Page *p;
176 int tmp, record_size;
177
66e5b1df 178 if (s->pb->eof_reached)
e6565055
PR
179 return AVERROR(EIO);
180
181 if (anm->page < 0)
cbd3441e 182 return anm->page;
e6565055
PR
183
184repeat:
185 p = &anm->pt[anm->page];
186
187 /* parse page header */
188 if (anm->record < 0) {
6b4aa5da 189 avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16), SEEK_SET);
45a8a02a 190 avio_skip(pb, 8 + 2*p->nb_records);
e6565055
PR
191 anm->record = 0;
192 }
193
194 /* if we have fetched all records in this page, then find the
195 next page and repeat */
196 if (anm->record >= p->nb_records) {
197 anm->page = find_record(anm, p->base_record + p->nb_records);
198 if (anm->page < 0)
199 return anm->page;
200 anm->record = -1;
201 goto repeat;
202 }
203
204 /* fetch record size */
a2704c97 205 tmp = avio_tell(pb);
6b4aa5da 206 avio_seek(pb, anm->page_table_offset + MAX_PAGES*6 + (anm->page<<16) +
e6565055 207 8 + anm->record * 2, SEEK_SET);
b7effd4e 208 record_size = avio_rl16(pb);
6b4aa5da 209 avio_seek(pb, tmp, SEEK_SET);
e6565055
PR
210
211 /* fetch record */
212 pkt->size = av_get_packet(s->pb, pkt, record_size);
213 if (pkt->size < 0)
214 return pkt->size;
215 if (p->base_record + anm->record == 0)
cc947f04 216 pkt->flags |= AV_PKT_FLAG_KEY;
e6565055
PR
217
218 anm->record++;
219 return 0;
220}
221
c6610a21 222AVInputFormat ff_anm_demuxer = {
dfc2c4d9
AK
223 .name = "anm",
224 .long_name = NULL_IF_CONFIG_SMALL("Deluxe Paint Animation"),
225 .priv_data_size = sizeof(AnmDemuxContext),
226 .read_probe = probe,
227 .read_header = read_header,
228 .read_packet = read_packet,
e6565055 229};