FFmpeg
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 "libavcodec/paf.h"
24 #include "avformat.h"
25 #include "demux.h"
26 #include "internal.h"
27 
28 #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
29 
30 typedef struct PAFDemuxContext {
31  uint32_t buffer_size;
32  uint32_t frame_blks;
33  uint32_t nb_frames;
34  uint32_t start_offset;
35  uint32_t preload_count;
36  uint32_t max_video_blks;
37  uint32_t max_audio_blks;
38 
39  uint32_t current_frame;
42 
43  uint32_t *blocks_count_table;
46 
47  uint8_t *video_frame;
49 
50  uint8_t *audio_frame;
51  uint8_t *temp_audio_frame;
53 
54  int got_audio;
56 
57 static int read_probe(const AVProbeData *p)
58 {
59  if ((p->buf_size >= strlen(MAGIC)) &&
60  !memcmp(p->buf, MAGIC, strlen(MAGIC)))
61  return AVPROBE_SCORE_MAX;
62  return 0;
63 }
64 
66 {
67  PAFDemuxContext *p = s->priv_data;
68 
72  av_freep(&p->video_frame);
73  av_freep(&p->audio_frame);
75 
76  return 0;
77 }
78 
79 static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
80 {
81  int i;
82 
83  for (i = 0; i < count; i++) {
84  if (avio_feof(s->pb))
85  return AVERROR_INVALIDDATA;
86  table[i] = avio_rl32(s->pb);
87  }
88 
89  avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
90  return 0;
91 }
92 
94 {
95  PAFDemuxContext *p = s->priv_data;
96  AVIOContext *pb = s->pb;
97  AVStream *ast, *vst;
98  int frame_ms, ret = 0;
99 
100  avio_skip(pb, 132);
101 
102  vst = avformat_new_stream(s, 0);
103  if (!vst)
104  return AVERROR(ENOMEM);
105 
106  vst->start_time = 0;
107  vst->nb_frames =
108  vst->duration =
109  p->nb_frames = avio_rl32(pb);
110  frame_ms = avio_rl32(pb);
111  if (frame_ms < 1)
112  return AVERROR_INVALIDDATA;
113 
114  vst->codecpar->width = avio_rl32(pb);
115  vst->codecpar->height = avio_rl32(pb);
116  avio_skip(pb, 4);
117 
119  vst->codecpar->codec_tag = 0;
121  avpriv_set_pts_info(vst, 64, frame_ms, 1000);
122 
123  ast = avformat_new_stream(s, 0);
124  if (!ast)
125  return AVERROR(ENOMEM);
126 
127  ast->start_time = 0;
129  ast->codecpar->codec_tag = 0;
132  ast->codecpar->sample_rate = 22050;
133  avpriv_set_pts_info(ast, 64, 1, 22050);
134 
135  p->buffer_size = avio_rl32(pb);
136  p->preload_count = avio_rl32(pb);
137  p->frame_blks = avio_rl32(pb);
138  p->start_offset = avio_rl32(pb);
139  p->max_video_blks = avio_rl32(pb);
140  p->max_audio_blks = avio_rl32(pb);
141 
142  if (avio_feof(pb))
143  return AVERROR_INVALIDDATA;
144 
145  if (p->buffer_size < 175 ||
146  p->max_audio_blks < 2 ||
147  p->max_video_blks < 1 ||
148  p->frame_blks < 1 ||
149  p->nb_frames < 1 ||
150  p->preload_count < 1 ||
151  p->buffer_size > 2048 ||
152  p->max_video_blks > 2048 ||
153  p->max_audio_blks > 2048 ||
154  p->nb_frames > INT_MAX / sizeof(uint32_t) ||
155  p->frame_blks > INT_MAX / sizeof(uint32_t))
156  return AVERROR_INVALIDDATA;
157 
159  sizeof(*p->blocks_count_table));
161  sizeof(*p->frames_offset_table));
163  sizeof(*p->blocks_offset_table));
164 
167 
171 
172  if (!p->blocks_count_table ||
173  !p->frames_offset_table ||
174  !p->blocks_offset_table ||
175  !p->video_frame ||
176  !p->audio_frame ||
177  !p->temp_audio_frame)
178  return AVERROR(ENOMEM);
179 
180  avio_seek(pb, p->buffer_size, SEEK_SET);
181 
183  if (ret < 0)
184  return ret;
186  if (ret < 0)
187  return ret;
189  if (ret < 0)
190  return ret;
191 
192  p->got_audio = 0;
193  p->current_frame = 0;
194  p->current_frame_block = 0;
195 
196  avio_seek(pb, p->start_offset, SEEK_SET);
197 
198  return 0;
199 }
200 
202 {
203  PAFDemuxContext *p = s->priv_data;
204  AVIOContext *pb = s->pb;
205  uint32_t count, offset;
206  int size, i, ret;
207 
208  if (p->current_frame >= p->nb_frames)
209  return AVERROR_EOF;
210 
211  if (avio_feof(pb))
212  return AVERROR_EOF;
213 
214  if (p->got_audio) {
215  if ((ret = av_new_packet(pkt, p->audio_size)) < 0)
216  return ret;
217 
218  memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
221  pkt->stream_index = 1;
222  p->got_audio = 0;
223  return pkt->size;
224  }
225 
226  count = (p->current_frame == 0) ? p->preload_count
227  : p->blocks_count_table[p->current_frame - 1];
228  for (i = 0; i < count; i++) {
229  if (p->current_frame_block >= p->frame_blks)
230  return AVERROR_INVALIDDATA;
231 
232  offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
233  if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
234  if (offset > p->audio_size - p->buffer_size)
235  return AVERROR_INVALIDDATA;
236 
237  avio_read(pb, p->audio_frame + offset, p->buffer_size);
238  if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
239  memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
240  p->got_audio = 1;
241  }
242  } else {
243  if (offset > p->video_size - p->buffer_size)
244  return AVERROR_INVALIDDATA;
245 
246  avio_read(pb, p->video_frame + offset, p->buffer_size);
247  }
248  p->current_frame_block++;
249  }
250 
252  return AVERROR_INVALIDDATA;
253 
255 
256  if ((ret = av_new_packet(pkt, size)) < 0)
257  return ret;
258 
259  pkt->stream_index = 0;
260  pkt->duration = 1;
261  memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
262  if (pkt->data[0] & 0x20)
264  p->current_frame++;
265 
266  return pkt->size;
267 }
268 
270  .p.name = "paf",
271  .p.long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
272  .priv_data_size = sizeof(PAFDemuxContext),
273  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
278 };
PAFDemuxContext::frame_blks
uint32_t frame_blks
Definition: paf.c:32
read_header
static int read_header(AVFormatContext *s)
Definition: paf.c:93
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
read_probe
static int read_probe(const AVProbeData *p)
Definition: paf.c:57
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:379
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
PAFDemuxContext::frames_offset_table
uint32_t * frames_offset_table
Definition: paf.c:44
PAF_SOUND_FRAME_SIZE
#define PAF_SOUND_FRAME_SIZE
Definition: paf.h:26
PAFDemuxContext::current_frame_count
uint32_t current_frame_count
Definition: paf.c:40
AVPacket::data
uint8_t * data
Definition: packet.h:522
PAFDemuxContext::current_frame_block
uint32_t current_frame_block
Definition: paf.c:41
table
static const uint16_t table[]
Definition: prosumer.c:205
read_close
static int read_close(AVFormatContext *s)
Definition: paf.c:65
PAFDemuxContext::blocks_count_table
uint32_t * blocks_count_table
Definition: paf.c:43
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:540
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:59
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:577
paf.h
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
PAFDemuxContext::max_audio_blks
uint32_t max_audio_blks
Definition: paf.c:37
avpriv_set_pts_info
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:853
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:802
PAFDemuxContext::nb_frames
uint32_t nb_frames
Definition: paf.c:33
pkt
AVPacket * pkt
Definition: movenc.c:59
PAFDemuxContext::blocks_offset_table
uint32_t * blocks_offset_table
Definition: paf.c:45
s
#define s(width, name)
Definition: cbs_vp9.c:198
PAFDemuxContext::max_video_blks
uint32_t max_video_blks
Definition: paf.c:36
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:134
PAFDemuxContext::video_size
int video_size
Definition: paf.c:48
PAFDemuxContext::got_audio
int got_audio
Definition: paf.c:54
FF_INFMT_FLAG_INIT_CLEANUP
#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
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:766
PAFDemuxContext::current_frame
uint32_t current_frame
Definition: paf.c:39
PAFDemuxContext
Definition: paf.c:30
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
PAFDemuxContext::video_frame
uint8_t * video_frame
Definition: paf.c:47
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:804
read_packet
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: paf.c:201
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:729
PAFDemuxContext::temp_audio_frame
uint8_t * temp_audio_frame
Definition: paf.c:51
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:523
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:303
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
size
int size
Definition: twinvq_data.h:10344
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:41
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:528
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
PAF_SOUND_SAMPLES
#define PAF_SOUND_SAMPLES
Definition: paf.h:25
AVCodecParameters::height
int height
Definition: codec_par.h:135
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
AV_CODEC_ID_PAF_VIDEO
@ AV_CODEC_ID_PAF_VIDEO
Definition: codec_id.h:231
PAFDemuxContext::preload_count
uint32_t preload_count
Definition: paf.c:35
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
demux.h
MAGIC
#define MAGIC
Definition: paf.c:28
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:743
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:230
ff_paf_demuxer
const FFInputFormat ff_paf_demuxer
Definition: paf.c:269
read_table
static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
Definition: paf.c:79
avformat.h
U
#define U(x)
Definition: vpx_arith.h:37
channel_layout.h
PAFDemuxContext::buffer_size
uint32_t buffer_size
Definition: paf.c:31
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:611
AVPacket::stream_index
int stream_index
Definition: packet.h:524
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:317
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
PAFDemuxContext::start_offset
uint32_t start_offset
Definition: paf.c:34
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:499
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
FFInputFormat
Definition: demux.h:37
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:792
PAFDemuxContext::audio_frame
uint8_t * audio_frame
Definition: paf.c:50
AV_CODEC_ID_PAF_AUDIO
@ AV_CODEC_ID_PAF_AUDIO
Definition: codec_id.h:504
PAFDemuxContext::audio_size
int audio_size
Definition: paf.c:52
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:345