2 * Packed Animation File demuxer
3 * Copyright (c) 2012 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavcodec/paf.h"
26 #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
32 uint32_t start_offset;
33 uint32_t preload_count;
34 uint32_t max_video_blks;
35 uint32_t max_audio_blks;
37 uint32_t current_frame;
38 uint32_t current_frame_count;
39 uint32_t current_frame_block;
41 uint32_t *blocks_count_table;
42 uint32_t *frames_offset_table;
43 uint32_t *blocks_offset_table;
49 uint8_t *temp_audio_frame;
55 static int read_probe(AVProbeData *p)
57 if ((p->buf_size >= strlen(MAGIC)) &&
58 !memcmp(p->buf, MAGIC, strlen(MAGIC)))
59 return AVPROBE_SCORE_MAX;
63 static int read_close(AVFormatContext *s)
65 PAFDemuxContext *p = s->priv_data;
67 av_freep(&p->blocks_count_table);
68 av_freep(&p->frames_offset_table);
69 av_freep(&p->blocks_offset_table);
70 av_freep(&p->video_frame);
71 av_freep(&p->audio_frame);
72 av_freep(&p->temp_audio_frame);
77 static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
81 for (i = 0; i < count; i++)
82 table[i] = avio_rl32(s->pb);
84 avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
87 static int read_header(AVFormatContext *s)
89 PAFDemuxContext *p = s->priv_data;
90 AVIOContext *pb = s->pb;
96 vst = avformat_new_stream(s, 0);
98 return AVERROR(ENOMEM);
103 p->nb_frames = avio_rl32(pb);
105 vst->codec->width = avio_rl32(pb);
106 vst->codec->height = avio_rl32(pb);
108 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
109 vst->codec->codec_tag = 0;
110 vst->codec->codec_id = AV_CODEC_ID_PAF_VIDEO;
111 avpriv_set_pts_info(vst, 64, 1, 10);
113 ast = avformat_new_stream(s, 0);
115 return AVERROR(ENOMEM);
118 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
119 ast->codec->codec_tag = 0;
120 ast->codec->codec_id = AV_CODEC_ID_PAF_AUDIO;
121 ast->codec->channels = 2;
122 ast->codec->sample_rate = 22050;
123 avpriv_set_pts_info(ast, 64, 1, 22050);
125 p->buffer_size = avio_rl32(pb);
126 p->preload_count = avio_rl32(pb);
127 p->frame_blks = avio_rl32(pb);
128 p->start_offset = avio_rl32(pb);
129 p->max_video_blks = avio_rl32(pb);
130 p->max_audio_blks = avio_rl32(pb);
131 if (p->buffer_size < 175 ||
132 p->max_audio_blks < 2 ||
133 p->max_video_blks < 1 ||
136 p->preload_count < 1 ||
137 p->buffer_size > 2048 ||
138 p->max_video_blks > 2048 ||
139 p->max_audio_blks > 2048 ||
140 p->nb_frames > INT_MAX / sizeof(uint32_t) ||
141 p->frame_blks > INT_MAX / sizeof(uint32_t))
142 return AVERROR_INVALIDDATA;
144 p->blocks_count_table = av_mallocz(p->nb_frames * sizeof(uint32_t));
145 p->frames_offset_table = av_mallocz(p->nb_frames * sizeof(uint32_t));
146 p->blocks_offset_table = av_mallocz(p->frame_blks * sizeof(uint32_t));
148 p->video_size = p->max_video_blks * p->buffer_size;
149 p->video_frame = av_mallocz(p->video_size);
151 p->audio_size = p->max_audio_blks * p->buffer_size;
152 p->audio_frame = av_mallocz(p->audio_size);
153 p->temp_audio_frame = av_mallocz(p->audio_size);
155 if (!p->blocks_count_table ||
156 !p->frames_offset_table ||
157 !p->blocks_offset_table ||
160 !p->temp_audio_frame) {
161 ret = AVERROR(ENOMEM);
165 avio_seek(pb, p->buffer_size, SEEK_SET);
167 read_table(s, p->blocks_count_table, p->nb_frames);
168 read_table(s, p->frames_offset_table, p->nb_frames);
169 read_table(s, p->blocks_offset_table, p->frame_blks);
172 p->current_frame = 0;
173 p->current_frame_block = 0;
175 avio_seek(pb, p->start_offset, SEEK_SET);
185 static int read_packet(AVFormatContext *s, AVPacket *pkt)
187 PAFDemuxContext *p = s->priv_data;
188 AVIOContext *pb = s->pb;
189 uint32_t count, offset;
192 if (p->current_frame >= p->nb_frames)
199 if (av_new_packet(pkt, p->audio_size) < 0)
200 return AVERROR(ENOMEM);
202 memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
203 pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
204 pkt->flags |= AV_PKT_FLAG_KEY;
205 pkt->stream_index = 1;
210 count = (p->current_frame == 0) ? p->preload_count : p->blocks_count_table[p->current_frame - 1];
211 for (i = 0; i < count; i++) {
212 if (p->current_frame_block >= p->frame_blks)
213 return AVERROR_INVALIDDATA;
215 offset = p->blocks_offset_table[p->current_frame_block] & ~(1 << 31);
216 if (p->blocks_offset_table[p->current_frame_block] & (1 << 31)) {
217 if (offset > p->audio_size - p->buffer_size)
218 return AVERROR_INVALIDDATA;
220 avio_read(pb, p->audio_frame + offset, p->buffer_size);
221 if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
222 memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
226 if (offset > p->video_size - p->buffer_size)
227 return AVERROR_INVALIDDATA;
229 avio_read(pb, p->video_frame + offset, p->buffer_size);
231 p->current_frame_block++;
234 size = p->video_size - p->frames_offset_table[p->current_frame];
236 return AVERROR_INVALIDDATA;
238 if (av_new_packet(pkt, size) < 0)
239 return AVERROR(ENOMEM);
241 pkt->stream_index = 0;
243 memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
244 if (pkt->data[0] & 0x20)
245 pkt->flags |= AV_PKT_FLAG_KEY;
251 AVInputFormat ff_paf_demuxer = {
253 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
254 .priv_data_size = sizeof(PAFDemuxContext),
255 .read_probe = read_probe,
256 .read_header = read_header,
257 .read_packet = read_packet,
258 .read_close = read_close,