1 /*
2 * Packed Animation File demuxer
3 * Copyright (c) 2012 Paul B Mahol
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 #include "libavutil/channel_layout.h"
23 #include "libavcodec/paf.h"
24 #include "avformat.h"
25 #include "internal.h"
26
27 #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
28
29 typedef struct PAFDemuxContext {
30 uint32_t buffer_size;
31 uint32_t frame_blks;
32 uint32_t nb_frames;
33 uint32_t start_offset;
34 uint32_t preload_count;
35 uint32_t max_video_blks;
36 uint32_t max_audio_blks;
37
38 uint32_t current_frame;
39 uint32_t current_frame_count;
40 uint32_t current_frame_block;
41
42 uint32_t *blocks_count_table;
43 uint32_t *frames_offset_table;
44 uint32_t *blocks_offset_table;
45
46 uint8_t *video_frame;
47 int video_size;
48
49 uint8_t *audio_frame;
50 uint8_t *temp_audio_frame;
51 int audio_size;
52
53 int got_audio;
54 } PAFDemuxContext;
55
read_probe(const AVProbeData * p)56 static int read_probe(const AVProbeData *p)
57 {
58 if ((p->buf_size >= strlen(MAGIC)) &&
59 !memcmp(p->buf, MAGIC, strlen(MAGIC)))
60 return AVPROBE_SCORE_MAX;
61 return 0;
62 }
63
read_close(AVFormatContext * s)64 static int read_close(AVFormatContext *s)
65 {
66 PAFDemuxContext *p = s->priv_data;
67
68 av_freep(&p->blocks_count_table);
69 av_freep(&p->frames_offset_table);
70 av_freep(&p->blocks_offset_table);
71 av_freep(&p->video_frame);
72 av_freep(&p->audio_frame);
73 av_freep(&p->temp_audio_frame);
74
75 return 0;
76 }
77
read_table(AVFormatContext * s,uint32_t * table,uint32_t count)78 static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
79 {
80 int i;
81
82 for (i = 0; i < count; i++) {
83 if (avio_feof(s->pb))
84 return AVERROR_INVALIDDATA;
85 table[i] = avio_rl32(s->pb);
86 }
87
88 avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
89 return 0;
90 }
91
read_header(AVFormatContext * s)92 static int read_header(AVFormatContext *s)
93 {
94 PAFDemuxContext *p = s->priv_data;
95 AVIOContext *pb = s->pb;
96 AVStream *ast, *vst;
97 int frame_ms, ret = 0;
98
99 avio_skip(pb, 132);
100
101 vst = avformat_new_stream(s, 0);
102 if (!vst)
103 return AVERROR(ENOMEM);
104
105 vst->start_time = 0;
106 vst->nb_frames =
107 vst->duration =
108 p->nb_frames = avio_rl32(pb);
109 frame_ms = avio_rl32(pb);
110 if (frame_ms < 1)
111 return AVERROR_INVALIDDATA;
112
113 vst->codecpar->width = avio_rl32(pb);
114 vst->codecpar->height = avio_rl32(pb);
115 avio_skip(pb, 4);
116
117 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
118 vst->codecpar->codec_tag = 0;
119 vst->codecpar->codec_id = AV_CODEC_ID_PAF_VIDEO;
120 avpriv_set_pts_info(vst, 64, frame_ms, 1000);
121
122 ast = avformat_new_stream(s, 0);
123 if (!ast)
124 return AVERROR(ENOMEM);
125
126 ast->start_time = 0;
127 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
128 ast->codecpar->codec_tag = 0;
129 ast->codecpar->codec_id = AV_CODEC_ID_PAF_AUDIO;
130 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
131 ast->codecpar->sample_rate = 22050;
132 avpriv_set_pts_info(ast, 64, 1, 22050);
133
134 p->buffer_size = avio_rl32(pb);
135 p->preload_count = avio_rl32(pb);
136 p->frame_blks = avio_rl32(pb);
137 p->start_offset = avio_rl32(pb);
138 p->max_video_blks = avio_rl32(pb);
139 p->max_audio_blks = avio_rl32(pb);
140
141 if (avio_feof(pb))
142 return AVERROR_INVALIDDATA;
143
144 if (p->buffer_size < 175 ||
145 p->max_audio_blks < 2 ||
146 p->max_video_blks < 1 ||
147 p->frame_blks < 1 ||
148 p->nb_frames < 1 ||
149 p->preload_count < 1 ||
150 p->buffer_size > 2048 ||
151 p->max_video_blks > 2048 ||
152 p->max_audio_blks > 2048 ||
153 p->nb_frames > INT_MAX / sizeof(uint32_t) ||
154 p->frame_blks > INT_MAX / sizeof(uint32_t))
155 return AVERROR_INVALIDDATA;
156
157 p->blocks_count_table = av_malloc_array(p->nb_frames,
158 sizeof(*p->blocks_count_table));
159 p->frames_offset_table = av_malloc_array(p->nb_frames,
160 sizeof(*p->frames_offset_table));
161 p->blocks_offset_table = av_malloc_array(p->frame_blks,
162 sizeof(*p->blocks_offset_table));
163
164 p->video_size = p->max_video_blks * p->buffer_size;
165 p->video_frame = av_mallocz(p->video_size);
166
167 p->audio_size = p->max_audio_blks * p->buffer_size;
168 p->audio_frame = av_mallocz(p->audio_size);
169 p->temp_audio_frame = av_mallocz(p->audio_size);
170
171 if (!p->blocks_count_table ||
172 !p->frames_offset_table ||
173 !p->blocks_offset_table ||
174 !p->video_frame ||
175 !p->audio_frame ||
176 !p->temp_audio_frame)
177 return AVERROR(ENOMEM);
178
179 avio_seek(pb, p->buffer_size, SEEK_SET);
180
181 ret = read_table(s, p->blocks_count_table, p->nb_frames);
182 if (ret < 0)
183 return ret;
184 ret = read_table(s, p->frames_offset_table, p->nb_frames);
185 if (ret < 0)
186 return ret;
187 ret = read_table(s, p->blocks_offset_table, p->frame_blks);
188 if (ret < 0)
189 return ret;
190
191 p->got_audio = 0;
192 p->current_frame = 0;
193 p->current_frame_block = 0;
194
195 avio_seek(pb, p->start_offset, SEEK_SET);
196
197 return 0;
198 }
199
read_packet(AVFormatContext * s,AVPacket * pkt)200 static int read_packet(AVFormatContext *s, AVPacket *pkt)
201 {
202 PAFDemuxContext *p = s->priv_data;
203 AVIOContext *pb = s->pb;
204 uint32_t count, offset;
205 int size, i, ret;
206
207 if (p->current_frame >= p->nb_frames)
208 return AVERROR_EOF;
209
210 if (avio_feof(pb))
211 return AVERROR_EOF;
212
213 if (p->got_audio) {
214 if ((ret = av_new_packet(pkt, p->audio_size)) < 0)
215 return ret;
216
217 memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
218 pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
219 pkt->flags |= AV_PKT_FLAG_KEY;
220 pkt->stream_index = 1;
221 p->got_audio = 0;
222 return pkt->size;
223 }
224
225 count = (p->current_frame == 0) ? p->preload_count
226 : p->blocks_count_table[p->current_frame - 1];
227 for (i = 0; i < count; i++) {
228 if (p->current_frame_block >= p->frame_blks)
229 return AVERROR_INVALIDDATA;
230
231 offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
232 if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
233 if (offset > p->audio_size - p->buffer_size)
234 return AVERROR_INVALIDDATA;
235
236 avio_read(pb, p->audio_frame + offset, p->buffer_size);
237 if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
238 memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
239 p->got_audio = 1;
240 }
241 } else {
242 if (offset > p->video_size - p->buffer_size)
243 return AVERROR_INVALIDDATA;
244
245 avio_read(pb, p->video_frame + offset, p->buffer_size);
246 }
247 p->current_frame_block++;
248 }
249
250 if (p->frames_offset_table[p->current_frame] >= p->video_size)
251 return AVERROR_INVALIDDATA;
252
253 size = p->video_size - p->frames_offset_table[p->current_frame];
254
255 if ((ret = av_new_packet(pkt, size)) < 0)
256 return ret;
257
258 pkt->stream_index = 0;
259 pkt->duration = 1;
260 memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
261 if (pkt->data[0] & 0x20)
262 pkt->flags |= AV_PKT_FLAG_KEY;
263 p->current_frame++;
264
265 return pkt->size;
266 }
267
268 const AVInputFormat ff_paf_demuxer = {
269 .name = "paf",
270 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
271 .priv_data_size = sizeof(PAFDemuxContext),
272 .flags_internal = FF_FMT_INIT_CLEANUP,
273 .read_probe = read_probe,
274 .read_header = read_header,
275 .read_packet = read_packet,
276 .read_close = read_close,
277 };
278