FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/anm.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 84 102 82.4%
Functions: 4 4 100.0%
Branches: 38 58 65.5%

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