FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 PAFDemuxContext {
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 
107  vst->codecpar->width = avio_rl32(pb);
108  vst->codecpar->height = avio_rl32(pb);
109  avio_skip(pb, 4);
110 
112  vst->codecpar->codec_tag = 0;
114  avpriv_set_pts_info(vst, 64, 1, 10);
115 
116  ast = avformat_new_stream(s, 0);
117  if (!ast)
118  return AVERROR(ENOMEM);
119 
120  ast->start_time = 0;
122  ast->codecpar->codec_tag = 0;
124  ast->codecpar->channels = 2;
126  ast->codecpar->sample_rate = 22050;
127  avpriv_set_pts_info(ast, 64, 1, 22050);
128 
129  p->buffer_size = avio_rl32(pb);
130  p->preload_count = avio_rl32(pb);
131  p->frame_blks = avio_rl32(pb);
132  p->start_offset = avio_rl32(pb);
133  p->max_video_blks = avio_rl32(pb);
134  p->max_audio_blks = avio_rl32(pb);
135  if (p->buffer_size < 175 ||
136  p->max_audio_blks < 2 ||
137  p->max_video_blks < 1 ||
138  p->frame_blks < 1 ||
139  p->nb_frames < 1 ||
140  p->preload_count < 1 ||
141  p->buffer_size > 2048 ||
142  p->max_video_blks > 2048 ||
143  p->max_audio_blks > 2048 ||
144  p->nb_frames > INT_MAX / sizeof(uint32_t) ||
145  p->frame_blks > INT_MAX / sizeof(uint32_t))
146  return AVERROR_INVALIDDATA;
147 
149  sizeof(*p->blocks_count_table));
151  sizeof(*p->frames_offset_table));
153  sizeof(*p->blocks_offset_table));
154 
157 
161 
162  if (!p->blocks_count_table ||
163  !p->frames_offset_table ||
164  !p->blocks_offset_table ||
165  !p->video_frame ||
166  !p->audio_frame ||
167  !p->temp_audio_frame) {
168  ret = AVERROR(ENOMEM);
169  goto fail;
170  }
171 
172  avio_seek(pb, p->buffer_size, SEEK_SET);
173 
177 
178  p->got_audio = 0;
179  p->current_frame = 0;
180  p->current_frame_block = 0;
181 
182  avio_seek(pb, p->start_offset, SEEK_SET);
183 
184  return 0;
185 
186 fail:
187  read_close(s);
188 
189  return ret;
190 }
191 
193 {
194  PAFDemuxContext *p = s->priv_data;
195  AVIOContext *pb = s->pb;
196  uint32_t count, offset;
197  int size, i;
198 
199  if (p->current_frame >= p->nb_frames)
200  return AVERROR_EOF;
201 
202  if (avio_feof(pb))
203  return AVERROR_EOF;
204 
205  if (p->got_audio) {
206  if (av_new_packet(pkt, p->audio_size) < 0)
207  return AVERROR(ENOMEM);
208 
209  memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
211  pkt->flags |= AV_PKT_FLAG_KEY;
212  pkt->stream_index = 1;
213  p->got_audio = 0;
214  return pkt->size;
215  }
216 
217  count = (p->current_frame == 0) ? p->preload_count
218  : p->blocks_count_table[p->current_frame - 1];
219  for (i = 0; i < count; i++) {
220  if (p->current_frame_block >= p->frame_blks)
221  return AVERROR_INVALIDDATA;
222 
223  offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
224  if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
225  if (offset > p->audio_size - p->buffer_size)
226  return AVERROR_INVALIDDATA;
227 
228  avio_read(pb, p->audio_frame + offset, p->buffer_size);
229  if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
230  memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
231  p->got_audio = 1;
232  }
233  } else {
234  if (offset > p->video_size - p->buffer_size)
235  return AVERROR_INVALIDDATA;
236 
237  avio_read(pb, p->video_frame + offset, p->buffer_size);
238  }
239  p->current_frame_block++;
240  }
241 
243  return AVERROR_INVALIDDATA;
244 
245  size = p->video_size - p->frames_offset_table[p->current_frame];
246 
247  if (av_new_packet(pkt, size) < 0)
248  return AVERROR(ENOMEM);
249 
250  pkt->stream_index = 0;
251  pkt->duration = 1;
252  memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
253  if (pkt->data[0] & 0x20)
254  pkt->flags |= AV_PKT_FLAG_KEY;
255  p->current_frame++;
256 
257  return pkt->size;
258 }
259 
261  .name = "paf",
262  .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
263  .priv_data_size = sizeof(PAFDemuxContext),
268 };
const char * s
Definition: avisynth_c.h:768
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int read_probe(AVProbeData *p)
Definition: paf.c:56
#define PAF_SOUND_FRAME_SIZE
Definition: paf.h:26
uint32_t * blocks_offset_table
Definition: paf.c:44
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:4737
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:4152
int size
Definition: avcodec.h:1680
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:244
int got_audio
Definition: paf.c:53
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static AVPacket pkt
#define AV_CH_LAYOUT_STEREO
static int read_header(AVFormatContext *s)
Definition: paf.c:88
uint32_t current_frame_block
Definition: paf.c:40
uint32_t nb_frames
Definition: paf.c:32
Format I/O context.
Definition: avformat.h:1349
uint32_t current_frame_count
Definition: paf.c:39
static int read_close(AVFormatContext *s)
Definition: paf.c:64
uint32_t * frames_offset_table
Definition: paf.c:43
uint8_t
int width
Video only.
Definition: avcodec.h:4218
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1697
AVInputFormat ff_paf_demuxer
Definition: paf.c:260
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4367
uint8_t * audio_frame
Definition: paf.c:49
uint8_t * data
Definition: avcodec.h:1679
#define AVERROR_EOF
End of file.
Definition: error.h:55
ptrdiff_t size
Definition: opengl_enc.c:101
uint32_t current_frame
Definition: paf.c:38
#define FFALIGN(x, a)
Definition: macros.h:48
uint64_t channel_layout
Audio only.
Definition: avcodec.h:4254
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1711
#define U(x)
Definition: vp56_arith.h:37
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:86
static int read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: paf.c:192
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
#define AVERROR(e)
Definition: error.h:43
static const struct endianess table[]
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:4148
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
GLsizei count
Definition: opengl_enc.c:109
#define fail()
Definition: checkasm.h:109
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1685
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:464
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:463
uint32_t max_video_blks
Definition: paf.c:35
uint8_t * video_frame
Definition: paf.c:46
audio channel layout utility functions
uint8_t * temp_audio_frame
Definition: paf.c:50
Stream structure.
Definition: avformat.h:889
uint32_t start_offset
Definition: paf.c:33
AVIOContext * pb
I/O context.
Definition: avformat.h:1391
uint32_t buffer_size
Definition: paf.c:30
int video_size
Definition: paf.c:47
#define PAF_SOUND_SAMPLES
Definition: paf.h:25
This structure contains the data a format has to probe a file.
Definition: avformat.h:461
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:946
int sample_rate
Audio only.
Definition: avcodec.h:4262
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:473
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:936
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:948
uint32_t preload_count
Definition: paf.c:34
uint32_t max_audio_blks
Definition: paf.c:36
void * priv_data
Format private data.
Definition: avformat.h:1377
int channels
Audio only.
Definition: avcodec.h:4258
#define MAGIC
Definition: paf.c:27
uint32_t * blocks_count_table
Definition: paf.c:42
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:664
static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
Definition: paf.c:78
AVCodecParameters * codecpar
Definition: avformat.h:1252
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:356
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: avcodec.h:4156
int stream_index
Definition: avcodec.h:1681
This structure stores compressed data.
Definition: avcodec.h:1656
int audio_size
Definition: paf.c:51
uint32_t frame_blks
Definition: paf.c:31
for(j=16;j >0;--j)