Libav
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 Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21 
23 
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 #define PAF_SOUND_SAMPLES 2205
29 #define PAF_SOUND_FRAME_SIZE ((256 + PAF_SOUND_SAMPLES) * 2)
30 
31 typedef struct {
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;
37  uint32_t max_video_blks;
38  uint32_t max_audio_blks;
39 
40  uint32_t current_frame;
43 
44  uint32_t *blocks_count_table;
47 
50 
54 
55  int got_audio;
57 
58 static int read_probe(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 
73  av_freep(&p->video_frame);
74  av_freep(&p->audio_frame);
76 
77  return 0;
78 }
79 
80 static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
81 {
82  int i;
83 
84  for (i = 0; i < count; i++)
85  table[i] = avio_rl32(s->pb);
86 
87  avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
88 }
89 
91 {
92  PAFDemuxContext *p = s->priv_data;
93  AVIOContext *pb = s->pb;
94  AVStream *ast, *vst;
95  int ret = 0;
96 
97  avio_skip(pb, 132);
98 
99  vst = avformat_new_stream(s, 0);
100  if (!vst)
101  return AVERROR(ENOMEM);
102 
103  vst->start_time = 0;
104  vst->nb_frames =
105  vst->duration =
106  p->nb_frames = avio_rl32(pb);
107  avio_skip(pb, 4);
108 
109  vst->codec->width = avio_rl32(pb);
110  vst->codec->height = avio_rl32(pb);
111  avio_skip(pb, 4);
112 
114  vst->codec->codec_tag = 0;
116  avpriv_set_pts_info(vst, 64, 1, 10);
117 
118  ast = avformat_new_stream(s, 0);
119  if (!ast)
120  return AVERROR(ENOMEM);
121 
122  ast->start_time = 0;
124  ast->codec->codec_tag = 0;
126  ast->codec->channels = 2;
128  ast->codec->sample_rate = 22050;
129  avpriv_set_pts_info(ast, 64, 1, 22050);
130 
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 ||
140  p->frame_blks < 1 ||
141  p->nb_frames < 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;
149 
151  sizeof(*p->blocks_count_table));
153  sizeof(*p->frames_offset_table));
155  sizeof(*p->blocks_offset_table));
156 
159 
163 
164  if (!p->blocks_count_table ||
165  !p->frames_offset_table ||
166  !p->blocks_offset_table ||
167  !p->video_frame ||
168  !p->audio_frame ||
169  !p->temp_audio_frame) {
170  ret = AVERROR(ENOMEM);
171  goto fail;
172  }
173 
174  avio_seek(pb, p->buffer_size, SEEK_SET);
175 
179 
180  p->got_audio = 0;
181  p->current_frame = 0;
182  p->current_frame_block = 0;
183 
184  avio_seek(pb, p->start_offset, SEEK_SET);
185 
186  return 0;
187 
188 fail:
189  read_close(s);
190 
191  return ret;
192 }
193 
195 {
196  PAFDemuxContext *p = s->priv_data;
197  AVIOContext *pb = s->pb;
198  uint32_t count, offset;
199  int size, i;
200 
201  if (p->current_frame >= p->nb_frames || pb->eof_reached)
202  return AVERROR_EOF;
203 
204  if (p->got_audio) {
205  if (av_new_packet(pkt, p->audio_size) < 0)
206  return AVERROR(ENOMEM);
207 
208  memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
210  pkt->flags |= AV_PKT_FLAG_KEY;
211  pkt->stream_index = 1;
212  p->got_audio = 0;
213  return pkt->size;
214  }
215 
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;
221 
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;
226 
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);
230  p->got_audio = 1;
231  }
232  } else {
233  if (offset > p->video_size - p->buffer_size)
234  return AVERROR_INVALIDDATA;
235 
236  avio_read(pb, p->video_frame + offset, p->buffer_size);
237  }
238  p->current_frame_block++;
239  }
240 
242  return AVERROR_INVALIDDATA;
243 
244  size = p->video_size - p->frames_offset_table[p->current_frame];
245 
246  if (av_new_packet(pkt, size) < 0)
247  return AVERROR(ENOMEM);
248 
249  pkt->stream_index = 0;
250  pkt->duration = 1;
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;
254  p->current_frame++;
255 
256  return pkt->size;
257 }
258 
260  .name = "paf",
261  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
262  .priv_data_size = sizeof(PAFDemuxContext),
267 };
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int read_probe(AVProbeData *p)
Definition: paf.c:58
int size
uint32_t * blocks_offset_table
Definition: paf.c:46
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.
Definition: utils.c:2829
Definition: vf_drawbox.c:37
int size
Definition: avcodec.h:974
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
int got_audio
Definition: paf.c:55
#define AV_CH_LAYOUT_STEREO
static int read_header(AVFormatContext *s)
Definition: paf.c:90
uint32_t current_frame_block
Definition: paf.c:42
uint32_t nb_frames
Definition: paf.c:34
#define FFALIGN(x, a)
Definition: common.h:62
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:922
uint32_t current_frame_count
Definition: paf.c:41
static int read_close(AVFormatContext *s)
Definition: paf.c:66
uint32_t * frames_offset_table
Definition: paf.c:45
uint8_t
#define PAF_SOUND_SAMPLES
Definition: paf.c:28
AVInputFormat ff_paf_demuxer
Definition: paf.c:259
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
uint8_t * audio_frame
Definition: paf.c:51
uint8_t * data
Definition: avcodec.h:973
#define AVERROR_EOF
End of file.
Definition: error.h:51
uint32_t current_frame
Definition: paf.c:40
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: paf.c:194
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:398
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
uint32_t max_video_blks
Definition: paf.c:37
uint8_t * video_frame
Definition: paf.c:48
audio channel layout utility functions
int width
picture width / height.
Definition: avcodec.h:1229
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
uint8_t * temp_audio_frame
Definition: paf.c:52
Stream structure.
Definition: avformat.h:699
uint32_t start_offset
Definition: paf.c:35
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
uint32_t buffer_size
Definition: paf.c:32
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:1082
int video_size
Definition: paf.c:49
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:756
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:758
uint32_t preload_count
Definition: paf.c:36
int eof_reached
true if eof reached
Definition: avio.h:96
int channels
number of audio channels
Definition: avcodec.h:1808
uint32_t max_audio_blks
Definition: paf.c:38
void * priv_data
Format private data.
Definition: avformat.h:950
#define PAF_SOUND_FRAME_SIZE
Definition: paf.c:29
#define MAGIC
Definition: paf.c:27
uint32_t * blocks_count_table
Definition: paf.c:44
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
Definition: paf.c:80
int stream_index
Definition: avcodec.h:975
This structure stores compressed data.
Definition: avcodec.h:950
int audio_size
Definition: paf.c:53
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
uint32_t frame_blks
Definition: paf.c:33
for(j=16;j >0;--j)