2
* Packed Animation File demuxer
3
* Copyright (c) 2012 Paul B Mahol
5
* This file is part of Libav.
7
* Libav 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
* Libav 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 Libav; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "libavutil/channel_layout.h"
27
#define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
28
#define PAF_SOUND_SAMPLES 2205
29
#define PAF_SOUND_FRAME_SIZE ((256 + PAF_SOUND_SAMPLES) * 2)
35
uint32_t start_offset;
36
uint32_t preload_count;
37
uint32_t max_video_blks;
38
uint32_t max_audio_blks;
40
uint32_t current_frame;
41
uint32_t current_frame_count;
42
uint32_t current_frame_block;
44
uint32_t *blocks_count_table;
45
uint32_t *frames_offset_table;
46
uint32_t *blocks_offset_table;
52
uint8_t *temp_audio_frame;
58
static int read_probe(AVProbeData *p)
60
if ((p->buf_size >= strlen(MAGIC)) &&
61
!memcmp(p->buf, MAGIC, strlen(MAGIC)))
62
return AVPROBE_SCORE_MAX;
66
static int read_close(AVFormatContext *s)
68
PAFDemuxContext *p = s->priv_data;
70
av_freep(&p->blocks_count_table);
71
av_freep(&p->frames_offset_table);
72
av_freep(&p->blocks_offset_table);
73
av_freep(&p->video_frame);
74
av_freep(&p->audio_frame);
75
av_freep(&p->temp_audio_frame);
80
static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
84
for (i = 0; i < count; i++)
85
table[i] = avio_rl32(s->pb);
87
avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
90
static int read_header(AVFormatContext *s)
92
PAFDemuxContext *p = s->priv_data;
93
AVIOContext *pb = s->pb;
99
vst = avformat_new_stream(s, 0);
101
return AVERROR(ENOMEM);
106
p->nb_frames = avio_rl32(pb);
109
vst->codec->width = avio_rl32(pb);
110
vst->codec->height = avio_rl32(pb);
113
vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
114
vst->codec->codec_tag = 0;
115
vst->codec->codec_id = AV_CODEC_ID_PAF_VIDEO;
116
avpriv_set_pts_info(vst, 64, 1, 10);
118
ast = avformat_new_stream(s, 0);
120
return AVERROR(ENOMEM);
123
ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
124
ast->codec->codec_tag = 0;
125
ast->codec->codec_id = AV_CODEC_ID_PAF_AUDIO;
126
ast->codec->channels = 2;
127
ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
128
ast->codec->sample_rate = 22050;
129
avpriv_set_pts_info(ast, 64, 1, 22050);
131
p->buffer_size = avio_rl32(pb);
132
p->preload_count = avio_rl32(pb);
133
p->frame_blks = avio_rl32(pb);
134
p->start_offset = avio_rl32(pb);
135
p->max_video_blks = avio_rl32(pb);
136
p->max_audio_blks = avio_rl32(pb);
137
if (p->buffer_size < 175 ||
138
p->max_audio_blks < 2 ||
139
p->max_video_blks < 1 ||
142
p->preload_count < 1 ||
143
p->buffer_size > 2048 ||
144
p->max_video_blks > 2048 ||
145
p->max_audio_blks > 2048 ||
146
p->nb_frames > INT_MAX / sizeof(uint32_t) ||
147
p->frame_blks > INT_MAX / sizeof(uint32_t))
148
return AVERROR_INVALIDDATA;
150
p->blocks_count_table = av_mallocz(p->nb_frames *
151
sizeof(*p->blocks_count_table));
152
p->frames_offset_table = av_mallocz(p->nb_frames *
153
sizeof(*p->frames_offset_table));
154
p->blocks_offset_table = av_mallocz(p->frame_blks *
155
sizeof(*p->blocks_offset_table));
157
p->video_size = p->max_video_blks * p->buffer_size;
158
p->video_frame = av_mallocz(p->video_size);
160
p->audio_size = p->max_audio_blks * p->buffer_size;
161
p->audio_frame = av_mallocz(p->audio_size);
162
p->temp_audio_frame = av_mallocz(p->audio_size);
164
if (!p->blocks_count_table ||
165
!p->frames_offset_table ||
166
!p->blocks_offset_table ||
169
!p->temp_audio_frame) {
170
ret = AVERROR(ENOMEM);
174
avio_seek(pb, p->buffer_size, SEEK_SET);
176
read_table(s, p->blocks_count_table, p->nb_frames);
177
read_table(s, p->frames_offset_table, p->nb_frames);
178
read_table(s, p->blocks_offset_table, p->frame_blks);
181
p->current_frame = 0;
182
p->current_frame_block = 0;
184
avio_seek(pb, p->start_offset, SEEK_SET);
194
static int read_packet(AVFormatContext *s, AVPacket *pkt)
196
PAFDemuxContext *p = s->priv_data;
197
AVIOContext *pb = s->pb;
198
uint32_t count, offset;
201
if (p->current_frame >= p->nb_frames || pb->eof_reached)
205
if (av_new_packet(pkt, p->audio_size) < 0)
206
return AVERROR(ENOMEM);
208
memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
209
pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
210
pkt->flags |= AV_PKT_FLAG_KEY;
211
pkt->stream_index = 1;
216
count = (p->current_frame == 0) ? p->preload_count
217
: p->blocks_count_table[p->current_frame - 1];
218
for (i = 0; i < count; i++) {
219
if (p->current_frame_block >= p->frame_blks)
220
return AVERROR_INVALIDDATA;
222
offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
223
if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
224
if (offset > p->audio_size - p->buffer_size)
225
return AVERROR_INVALIDDATA;
227
avio_read(pb, p->audio_frame + offset, p->buffer_size);
228
if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
229
memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
233
if (offset > p->video_size - p->buffer_size)
234
return AVERROR_INVALIDDATA;
236
avio_read(pb, p->video_frame + offset, p->buffer_size);
238
p->current_frame_block++;
241
if (p->frames_offset_table[p->current_frame] >= p->video_size)
242
return AVERROR_INVALIDDATA;
244
size = p->video_size - p->frames_offset_table[p->current_frame];
246
if (av_new_packet(pkt, size) < 0)
247
return AVERROR(ENOMEM);
249
pkt->stream_index = 0;
251
memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
252
if (pkt->data[0] & 0x20)
253
pkt->flags |= AV_PKT_FLAG_KEY;
259
AVInputFormat ff_paf_demuxer = {
261
.long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
262
.priv_data_size = sizeof(PAFDemuxContext),
263
.read_probe = read_probe,
264
.read_header = read_header,
265
.read_packet = read_packet,
266
.read_close = read_close,