00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/channel_layout.h"
00023 #include "libavcodec/paf.h"
00024 #include "avformat.h"
00025 #include "internal.h"
00026
00027 #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
00028
00029 typedef struct {
00030 uint32_t buffer_size;
00031 uint32_t frame_blks;
00032 uint32_t nb_frames;
00033 uint32_t start_offset;
00034 uint32_t preload_count;
00035 uint32_t max_video_blks;
00036 uint32_t max_audio_blks;
00037
00038 uint32_t current_frame;
00039 uint32_t current_frame_count;
00040 uint32_t current_frame_block;
00041
00042 uint32_t *blocks_count_table;
00043 uint32_t *frames_offset_table;
00044 uint32_t *blocks_offset_table;
00045
00046 uint8_t *video_frame;
00047 int video_size;
00048
00049 uint8_t *audio_frame;
00050 uint8_t *temp_audio_frame;
00051 int audio_size;
00052
00053 int got_audio;
00054 } PAFDemuxContext;
00055
00056 static int read_probe(AVProbeData *p)
00057 {
00058 if ((p->buf_size >= strlen(MAGIC)) &&
00059 !memcmp(p->buf, MAGIC, strlen(MAGIC)))
00060 return AVPROBE_SCORE_MAX;
00061 return 0;
00062 }
00063
00064 static int read_close(AVFormatContext *s)
00065 {
00066 PAFDemuxContext *p = s->priv_data;
00067
00068 av_freep(&p->blocks_count_table);
00069 av_freep(&p->frames_offset_table);
00070 av_freep(&p->blocks_offset_table);
00071 av_freep(&p->video_frame);
00072 av_freep(&p->audio_frame);
00073 av_freep(&p->temp_audio_frame);
00074
00075 return 0;
00076 }
00077
00078 static void read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
00079 {
00080 int i;
00081
00082 for (i = 0; i < count; i++)
00083 table[i] = avio_rl32(s->pb);
00084
00085 avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
00086 }
00087
00088 static int read_header(AVFormatContext *s)
00089 {
00090 PAFDemuxContext *p = s->priv_data;
00091 AVIOContext *pb = s->pb;
00092 AVStream *ast, *vst;
00093 int ret = 0;
00094
00095 avio_skip(pb, 132);
00096
00097 vst = avformat_new_stream(s, 0);
00098 if (!vst)
00099 return AVERROR(ENOMEM);
00100
00101 vst->start_time = 0;
00102 vst->nb_frames =
00103 vst->duration =
00104 p->nb_frames = avio_rl32(pb);
00105 avio_skip(pb, 4);
00106 vst->codec->width = avio_rl32(pb);
00107 vst->codec->height = avio_rl32(pb);
00108 avio_skip(pb, 4);
00109 vst->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00110 vst->codec->codec_tag = 0;
00111 vst->codec->codec_id = AV_CODEC_ID_PAF_VIDEO;
00112 avpriv_set_pts_info(vst, 64, 1, 10);
00113
00114 ast = avformat_new_stream(s, 0);
00115 if (!ast)
00116 return AVERROR(ENOMEM);
00117
00118 ast->start_time = 0;
00119 ast->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00120 ast->codec->codec_tag = 0;
00121 ast->codec->codec_id = AV_CODEC_ID_PAF_AUDIO;
00122 ast->codec->channels = 2;
00123 ast->codec->channel_layout = AV_CH_LAYOUT_STEREO;
00124 ast->codec->sample_rate = 22050;
00125 avpriv_set_pts_info(ast, 64, 1, 22050);
00126
00127 p->buffer_size = avio_rl32(pb);
00128 p->preload_count = avio_rl32(pb);
00129 p->frame_blks = avio_rl32(pb);
00130 p->start_offset = avio_rl32(pb);
00131 p->max_video_blks = avio_rl32(pb);
00132 p->max_audio_blks = avio_rl32(pb);
00133 if (p->buffer_size < 175 ||
00134 p->max_audio_blks < 2 ||
00135 p->max_video_blks < 1 ||
00136 p->frame_blks < 1 ||
00137 p->nb_frames < 1 ||
00138 p->preload_count < 1 ||
00139 p->buffer_size > 2048 ||
00140 p->max_video_blks > 2048 ||
00141 p->max_audio_blks > 2048 ||
00142 p->nb_frames > INT_MAX / sizeof(uint32_t) ||
00143 p->frame_blks > INT_MAX / sizeof(uint32_t))
00144 return AVERROR_INVALIDDATA;
00145
00146 p->blocks_count_table = av_mallocz(p->nb_frames * sizeof(uint32_t));
00147 p->frames_offset_table = av_mallocz(p->nb_frames * sizeof(uint32_t));
00148 p->blocks_offset_table = av_mallocz(p->frame_blks * sizeof(uint32_t));
00149
00150 p->video_size = p->max_video_blks * p->buffer_size;
00151 p->video_frame = av_mallocz(p->video_size);
00152
00153 p->audio_size = p->max_audio_blks * p->buffer_size;
00154 p->audio_frame = av_mallocz(p->audio_size);
00155 p->temp_audio_frame = av_mallocz(p->audio_size);
00156
00157 if (!p->blocks_count_table ||
00158 !p->frames_offset_table ||
00159 !p->blocks_offset_table ||
00160 !p->video_frame ||
00161 !p->audio_frame ||
00162 !p->temp_audio_frame) {
00163 ret = AVERROR(ENOMEM);
00164 goto fail;
00165 }
00166
00167 avio_seek(pb, p->buffer_size, SEEK_SET);
00168
00169 read_table(s, p->blocks_count_table, p->nb_frames);
00170 read_table(s, p->frames_offset_table, p->nb_frames);
00171 read_table(s, p->blocks_offset_table, p->frame_blks);
00172
00173 p->got_audio = 0;
00174 p->current_frame = 0;
00175 p->current_frame_block = 0;
00176
00177 avio_seek(pb, p->start_offset, SEEK_SET);
00178
00179 return 0;
00180
00181 fail:
00182 read_close(s);
00183
00184 return ret;
00185 }
00186
00187 static int read_packet(AVFormatContext *s, AVPacket *pkt)
00188 {
00189 PAFDemuxContext *p = s->priv_data;
00190 AVIOContext *pb = s->pb;
00191 uint32_t count, offset;
00192 int size, i;
00193
00194 if (p->current_frame >= p->nb_frames)
00195 return AVERROR_EOF;
00196
00197 if (url_feof(pb))
00198 return AVERROR_EOF;
00199
00200 if (p->got_audio) {
00201 if (av_new_packet(pkt, p->audio_size) < 0)
00202 return AVERROR(ENOMEM);
00203
00204 memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
00205 pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
00206 pkt->flags |= AV_PKT_FLAG_KEY;
00207 pkt->stream_index = 1;
00208 p->got_audio = 0;
00209 return pkt->size;
00210 }
00211
00212 count = (p->current_frame == 0) ? p->preload_count : p->blocks_count_table[p->current_frame - 1];
00213 for (i = 0; i < count; i++) {
00214 if (p->current_frame_block >= p->frame_blks)
00215 return AVERROR_INVALIDDATA;
00216
00217 offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
00218 if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
00219 if (offset > p->audio_size - p->buffer_size)
00220 return AVERROR_INVALIDDATA;
00221
00222 avio_read(pb, p->audio_frame + offset, p->buffer_size);
00223 if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
00224 memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
00225 p->got_audio = 1;
00226 }
00227 } else {
00228 if (offset > p->video_size - p->buffer_size)
00229 return AVERROR_INVALIDDATA;
00230
00231 avio_read(pb, p->video_frame + offset, p->buffer_size);
00232 }
00233 p->current_frame_block++;
00234 }
00235
00236 size = p->video_size - p->frames_offset_table[p->current_frame];
00237 if (size < 1)
00238 return AVERROR_INVALIDDATA;
00239
00240 if (av_new_packet(pkt, size) < 0)
00241 return AVERROR(ENOMEM);
00242
00243 pkt->stream_index = 0;
00244 pkt->duration = 1;
00245 memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
00246 if (pkt->data[0] & 0x20)
00247 pkt->flags |= AV_PKT_FLAG_KEY;
00248 p->current_frame++;
00249
00250 return pkt->size;
00251 }
00252
00253 AVInputFormat ff_paf_demuxer = {
00254 .name = "paf",
00255 .long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
00256 .priv_data_size = sizeof(PAFDemuxContext),
00257 .read_probe = read_probe,
00258 .read_header = read_header,
00259 .read_packet = read_packet,
00260 .read_close = read_close,
00261 };