FFmpeg
Loading...
Searching...
No Matches
paf.c
Go to the documentation of this file.
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
23#include "libavutil/mem.h"
24#include "libavcodec/paf.h"
25#include "avformat.h"
26#include "demux.h"
27#include "internal.h"
28
29#define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
30
31typedef struct PAFDemuxContext {
32 uint32_t buffer_size;
33 uint32_t frame_blks;
34 uint32_t nb_frames;
35 uint32_t start_offset;
36 uint32_t preload_count;
39
40 uint32_t current_frame;
43
47
48 uint8_t *video_frame;
50
51 uint8_t *audio_frame;
54
57
58static int read_probe(const AVProbeData *p)
59{
60 if ((p->buf_size >= strlen(MAGIC)) &&
61 !memcmp(p->buf, MAGIC, strlen(MAGIC)))
62 return AVPROBE_SCORE_MAX;
63 return 0;
64}
65
67{
68 PAFDemuxContext *p = s->priv_data;
69
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);
76
77 return 0;
78}
79
80static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
81{
82 int i;
83
84 for (i = 0; i < count; i++) {
85 if (avio_feof(s->pb))
87 table[i] = avio_rl32(s->pb);
88 }
89
90 avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
91 return 0;
92}
93
95{
96 PAFDemuxContext *p = s->priv_data;
97 AVIOContext *pb = s->pb;
98 AVStream *ast, *vst;
99 int frame_ms, ret = 0;
100
101 avio_skip(pb, 132);
102
103 vst = avformat_new_stream(s, 0);
104 if (!vst)
105 return AVERROR(ENOMEM);
106
107 vst->start_time = 0;
108 vst->nb_frames =
109 vst->duration =
110 p->nb_frames = avio_rl32(pb);
111 frame_ms = avio_rl32(pb);
112 if (frame_ms < 1)
113 return AVERROR_INVALIDDATA;
114
115 vst->codecpar->width = avio_rl32(pb);
116 vst->codecpar->height = avio_rl32(pb);
117 avio_skip(pb, 4);
118
120 vst->codecpar->codec_tag = 0;
122 avpriv_set_pts_info(vst, 64, frame_ms, 1000);
123
124 ast = avformat_new_stream(s, 0);
125 if (!ast)
126 return AVERROR(ENOMEM);
127
128 ast->start_time = 0;
130 ast->codecpar->codec_tag = 0;
133 ast->codecpar->sample_rate = 22050;
134 avpriv_set_pts_info(ast, 64, 1, 22050);
135
136 p->buffer_size = avio_rl32(pb);
137 p->preload_count = avio_rl32(pb);
138 p->frame_blks = avio_rl32(pb);
139 p->start_offset = avio_rl32(pb);
140 p->max_video_blks = avio_rl32(pb);
141 p->max_audio_blks = avio_rl32(pb);
142
143 if (avio_feof(pb))
144 return AVERROR_INVALIDDATA;
145
146 if (p->buffer_size < 175 ||
147 p->max_audio_blks < 2 ||
148 p->max_video_blks < 1 ||
149 p->frame_blks < 1 ||
150 p->nb_frames < 1 ||
151 p->preload_count < 1 ||
152 p->buffer_size > 2048 ||
153 p->max_video_blks > 2048 ||
154 p->max_audio_blks > 2048 ||
155 p->nb_frames > INT_MAX / sizeof(uint32_t) ||
156 p->frame_blks > INT_MAX / sizeof(uint32_t))
157 return AVERROR_INVALIDDATA;
158
159 p->blocks_count_table = av_malloc_array(p->nb_frames,
160 sizeof(*p->blocks_count_table));
161 p->frames_offset_table = av_malloc_array(p->nb_frames,
162 sizeof(*p->frames_offset_table));
163 p->blocks_offset_table = av_malloc_array(p->frame_blks,
164 sizeof(*p->blocks_offset_table));
165
166 p->video_size = p->max_video_blks * p->buffer_size;
167 p->video_frame = av_mallocz(p->video_size);
168
169 p->audio_size = p->max_audio_blks * p->buffer_size;
170 p->audio_frame = av_mallocz(p->audio_size);
171 p->temp_audio_frame = av_mallocz(p->audio_size);
172
173 if (!p->blocks_count_table ||
174 !p->frames_offset_table ||
175 !p->blocks_offset_table ||
176 !p->video_frame ||
177 !p->audio_frame ||
178 !p->temp_audio_frame)
179 return AVERROR(ENOMEM);
180
181 avio_seek(pb, p->buffer_size, SEEK_SET);
182
183 ret = read_table(s, p->blocks_count_table, p->nb_frames);
184 if (ret < 0)
185 return ret;
186 ret = read_table(s, p->frames_offset_table, p->nb_frames);
187 if (ret < 0)
188 return ret;
189 ret = read_table(s, p->blocks_offset_table, p->frame_blks);
190 if (ret < 0)
191 return ret;
192
193 p->got_audio = 0;
194 p->current_frame = 0;
195 p->current_frame_block = 0;
196
197 avio_seek(pb, p->start_offset, SEEK_SET);
198
199 return 0;
200}
201
203{
204 PAFDemuxContext *p = s->priv_data;
205 AVIOContext *pb = s->pb;
206 uint32_t count, offset;
207 int size, i, ret;
208
209 if (p->current_frame >= p->nb_frames)
210 return AVERROR_EOF;
211
212 if (avio_feof(pb))
213 return AVERROR_EOF;
214
215 if (p->got_audio) {
216 if ((ret = av_new_packet(pkt, p->audio_size)) < 0)
217 return ret;
218
219 memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
220 pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
221 pkt->flags |= AV_PKT_FLAG_KEY;
222 pkt->stream_index = 1;
223 p->got_audio = 0;
224 return pkt->size;
225 }
226
227 count = (p->current_frame == 0) ? p->preload_count
228 : p->blocks_count_table[p->current_frame - 1];
229 for (i = 0; i < count; i++) {
230 if (p->current_frame_block >= p->frame_blks)
231 return AVERROR_INVALIDDATA;
232
233 offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
234 if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
235 if (offset > p->audio_size - p->buffer_size)
236 return AVERROR_INVALIDDATA;
237
238 avio_read(pb, p->audio_frame + offset, p->buffer_size);
239 if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
240 memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
241 p->got_audio = 1;
242 }
243 } else {
244 if (offset > p->video_size - p->buffer_size)
245 return AVERROR_INVALIDDATA;
246
247 avio_read(pb, p->video_frame + offset, p->buffer_size);
248 }
249 p->current_frame_block++;
250 }
251
252 if (p->frames_offset_table[p->current_frame] >= p->video_size)
253 return AVERROR_INVALIDDATA;
254
255 size = p->video_size - p->frames_offset_table[p->current_frame];
256
257 if ((ret = av_new_packet(pkt, size)) < 0)
258 return ret;
259
260 pkt->stream_index = 0;
261 pkt->duration = 1;
262 memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
263 if (pkt->data[0] & 0x20)
264 pkt->flags |= AV_PKT_FLAG_KEY;
265 p->current_frame++;
266
267 return pkt->size;
268}
269
271 .p.name = "paf",
272 .p.long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
273 .priv_data_size = sizeof(PAFDemuxContext),
274 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
279};
const FFInputFormat ff_paf_demuxer
Definition paf.c:270
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
Public libavutil channel layout APIs header.
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_PAF_VIDEO
Definition codec_id.h:228
@ AV_CODEC_ID_PAF_AUDIO
Definition codec_id.h:517
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AV_CHANNEL_LAYOUT_STEREO
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
unsigned offset
Definition libaomenc.c:763
#define MAGIC
Definition jvdec.c:56
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
Definition paf.c:80
static int read_close(AVFormatContext *s)
Definition paf.c:66
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition paf.c:202
static int read_probe(const AVProbeData *p)
Definition paf.c:58
static int read_header(AVFormatContext *s)
Definition paf.c:94
#define PAF_SOUND_FRAME_SIZE
Definition paf.h:26
#define PAF_SOUND_SAMPLES
Definition paf.h:25
static const uint16_t table[]
Definition prosumer.c:203
An AVChannelLayout holds information about the channel layout of audio data.
int height
The height of the video frame in pixels.
Definition codec_par.h:150
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition codec_par.h:61
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
uint32_t max_audio_blks
Definition paf.c:38
uint8_t * audio_frame
Definition paf.c:51
uint32_t * blocks_count_table
Definition paf.c:44
uint8_t * video_frame
Definition paf.c:48
int audio_size
Definition paf.c:53
uint32_t current_frame_count
Definition paf.c:41
uint32_t preload_count
Definition paf.c:36
int video_size
Definition paf.c:49
uint32_t frame_blks
Definition paf.c:33
uint32_t current_frame_block
Definition paf.c:42
uint32_t * frames_offset_table
Definition paf.c:45
uint8_t * temp_audio_frame
Definition paf.c:52
uint32_t nb_frames
Definition paf.c:34
uint32_t current_frame
Definition paf.c:40
uint32_t buffer_size
Definition paf.c:32
uint32_t * blocks_offset_table
Definition paf.c:46
uint32_t max_video_blks
Definition paf.c:37
int got_audio
Definition paf.c:55
uint32_t start_offset
Definition paf.c:35
#define av_malloc_array(a, b)
#define av_mallocz(s)
#define av_freep(p)
int size