libavformat/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 "internal.h"
26 
27 #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
28 
29 typedef struct {
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;
41 
42  uint32_t *blocks_count_table;
45 
48 
52 
53  int got_audio;
55 
56 static int read_probe(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 
65 {
66  PAFDemuxContext *p = s->priv_data;
67 
71  av_freep(&p->video_frame);
72  av_freep(&p->audio_frame);
74 
75  return 0;
76 }
77 
78 static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
79 {
80  int i;
81 
82  for (i = 0; i < count; i++)
83  table[i] = avio_rl32(s->pb);
84 
85  avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
86 }
87 
89 {
90  PAFDemuxContext *p = s->priv_data;
91  AVIOContext *pb = s->pb;
92  AVStream *ast, *vst;
93  int ret = 0;
94 
95  avio_skip(pb, 132);
96 
97  vst = avformat_new_stream(s, 0);
98  if (!vst)
99  return AVERROR(ENOMEM);
100 
101  vst->start_time = 0;
102  vst->nb_frames =
103  vst->duration =
104  p->nb_frames = avio_rl32(pb);
105  avio_skip(pb, 4);
106  vst->codec->width = avio_rl32(pb);
107  vst->codec->height = avio_rl32(pb);
108  avio_skip(pb, 4);
110  vst->codec->codec_tag = 0;
112  avpriv_set_pts_info(vst, 64, 1, 10);
113 
114  ast = avformat_new_stream(s, 0);
115  if (!ast)
116  return AVERROR(ENOMEM);
117 
118  ast->start_time = 0;
120  ast->codec->codec_tag = 0;
122  ast->codec->channels = 2;
124  ast->codec->sample_rate = 22050;
125  avpriv_set_pts_info(ast, 64, 1, 22050);
126 
127  p->buffer_size = avio_rl32(pb);
128  p->preload_count = avio_rl32(pb);
129  p->frame_blks = avio_rl32(pb);
130  p->start_offset = avio_rl32(pb);
131  p->max_video_blks = avio_rl32(pb);
132  p->max_audio_blks = avio_rl32(pb);
133  if (p->buffer_size < 175 ||
134  p->max_audio_blks < 2 ||
135  p->max_video_blks < 1 ||
136  p->frame_blks < 1 ||
137  p->nb_frames < 1 ||
138  p->preload_count < 1 ||
139  p->buffer_size > 2048 ||
140  p->max_video_blks > 2048 ||
141  p->max_audio_blks > 2048 ||
142  p->nb_frames > INT_MAX / sizeof(uint32_t) ||
143  p->frame_blks > INT_MAX / sizeof(uint32_t))
144  return AVERROR_INVALIDDATA;
145 
146  p->blocks_count_table = av_mallocz(p->nb_frames * sizeof(uint32_t));
147  p->frames_offset_table = av_mallocz(p->nb_frames * sizeof(uint32_t));
148  p->blocks_offset_table = av_mallocz(p->frame_blks * sizeof(uint32_t));
149 
152 
156 
157  if (!p->blocks_count_table ||
158  !p->frames_offset_table ||
159  !p->blocks_offset_table ||
160  !p->video_frame ||
161  !p->audio_frame ||
162  !p->temp_audio_frame) {
163  ret = AVERROR(ENOMEM);
164  goto fail;
165  }
166 
167  avio_seek(pb, p->buffer_size, SEEK_SET);
168 
172 
173  p->got_audio = 0;
174  p->current_frame = 0;
175  p->current_frame_block = 0;
176 
177  avio_seek(pb, p->start_offset, SEEK_SET);
178 
179  return 0;
180 
181 fail:
182  read_close(s);
183 
184  return ret;
185 }
186 
188 {
189  PAFDemuxContext *p = s->priv_data;
190  AVIOContext *pb = s->pb;
191  uint32_t count, offset;
192  int size, i;
193 
194  if (p->current_frame >= p->nb_frames)
195  return AVERROR_EOF;
196 
197  if (url_feof(pb))
198  return AVERROR_EOF;
199 
200  if (p->got_audio) {
201  if (av_new_packet(pkt, p->audio_size) < 0)
202  return AVERROR(ENOMEM);
203 
204  memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
206  pkt->flags |= AV_PKT_FLAG_KEY;
207  pkt->stream_index = 1;
208  p->got_audio = 0;
209  return pkt->size;
210  }
211 
212  count = (p->current_frame == 0) ? p->preload_count : p->blocks_count_table[p->current_frame - 1];
213  for (i = 0; i < count; i++) {
214  if (p->current_frame_block >= p->frame_blks)
215  return AVERROR_INVALIDDATA;
216 
217  offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
218  if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
219  if (offset > p->audio_size - p->buffer_size)
220  return AVERROR_INVALIDDATA;
221 
222  avio_read(pb, p->audio_frame + offset, p->buffer_size);
223  if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
224  memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
225  p->got_audio = 1;
226  }
227  } else {
228  if (offset > p->video_size - p->buffer_size)
229  return AVERROR_INVALIDDATA;
230 
231  avio_read(pb, p->video_frame + offset, p->buffer_size);
232  }
233  p->current_frame_block++;
234  }
235 
236  size = p->video_size - p->frames_offset_table[p->current_frame];
237  if (size < 1)
238  return AVERROR_INVALIDDATA;
239 
240  if (av_new_packet(pkt, size) < 0)
241  return AVERROR(ENOMEM);
242 
243  pkt->stream_index = 0;
244  pkt->duration = 1;
245  memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
246  if (pkt->data[0] & 0x20)
247  pkt->flags |= AV_PKT_FLAG_KEY;
248  p->current_frame++;
249 
250  return pkt->size;
251 }
252 
254  .name = "paf",
255  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
256  .priv_data_size = sizeof(PAFDemuxContext),
261 };
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define MAGIC
#define PAF_SOUND_FRAME_SIZE
Definition: paf.h:26
uint32_t * blocks_offset_table
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
#define AV_CH_LAYOUT_STEREO
uint32_t current_frame_block
#define FFALIGN(x, a)
Definition: common.h:63
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:944
uint32_t current_frame_count
uint32_t * frames_offset_table
uint8_t
static AVPacket pkt
Definition: demuxing.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
static int read_header(AVFormatContext *s)
uint8_t * audio_frame
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
uint32_t current_frame
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define U(x)
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:579
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static int read_packet(AVFormatContext *s, AVPacket *pkt)
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
int size
int flags
A combination of AV_PKT_FLAG values.
uint64_t channel_layout
Audio channel layout.
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
uint32_t max_video_blks
uint8_t * video_frame
audio channel layout utility functions
ret
Definition: avfilter.c:821
int width
picture width / height.
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
static int read_probe(AVProbeData *p)
uint8_t * temp_audio_frame
Stream structure.
Definition: avformat.h:643
uint32_t start_offset
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
uint32_t buffer_size
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
synthesis window for stochastic i
static int read_close(AVFormatContext *s)
#define PAF_SOUND_SAMPLES
Definition: paf.h:25
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:698
uint32_t preload_count
AVInputFormat ff_paf_demuxer
int channels
number of audio channels
uint32_t max_audio_blks
void * priv_data
Format private data.
Definition: avformat.h:964
uint32_t * blocks_count_table
void INT64 INT64 count
Definition: avisynth_c.h:594
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
This structure stores compressed data.
for(j=16;j >0;--j)