FFmpeg
Loading...
Searching...
No Matches
avs.c
Go to the documentation of this file.
1/*
2 * AVS demuxer.
3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
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
22/**
23 * @file
24 * Argonaut Games' Creature Shock demuxer
25 * @see http://wiki.multimedia.cx/index.php?title=AVS
26 */
27
28#include "avformat.h"
29#include "avio_internal.h"
30#include "demux.h"
31#include "voc.h"
32
33
46
47typedef enum avs_block_type {
48 AVS_NONE = 0x00,
49 AVS_VIDEO = 0x01,
50 AVS_AUDIO = 0x02,
54
55static int avs_probe(const AVProbeData * p)
56{
57 const uint8_t *d;
58
59 d = p->buf;
60 if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
61 /* Ensure the buffer probe scores higher than the extension probe.
62 * This avoids problems with misdetection as AviSynth scripts. */
63 return AVPROBE_SCORE_EXTENSION + 5;
64
65 return 0;
66}
67
69{
70 AvsFormat *avs = s->priv_data;
71
72 s->ctx_flags |= AVFMTCTX_NOHEADER;
73
74 avio_skip(s->pb, 4);
75 avs->width = avio_rl16(s->pb);
76 avs->height = avio_rl16(s->pb);
77 avs->bits_per_sample = avio_rl16(s->pb);
78 avs->fps = avio_rl16(s->pb);
79 avs->nb_frames = avio_rl32(s->pb);
80 avs->remaining_frame_size = 0;
81 avs->remaining_audio_size = 0;
82
83 avs->st_video = avs->st_audio = NULL;
84
85 if (avs->width != 318 || avs->height != 198)
86 av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
87 "when the avs format is supposed to be 318x198 only.\n",
88 avs->width, avs->height);
89
90 return 0;
91}
92
93static int
95 AvsBlockType type, int sub_type, int size,
96 uint8_t * palette, int palette_size)
97{
98 AvsFormat *avs = s->priv_data;
99 int ret;
100
101 ret = av_new_packet(pkt, size + palette_size);
102 if (ret < 0)
103 return ret;
104
105 if (palette_size) {
106 pkt->data[0] = 0x00;
107 pkt->data[1] = 0x03;
108 pkt->data[2] = palette_size & 0xFF;
109 pkt->data[3] = (palette_size >> 8) & 0xFF;
110 memcpy(pkt->data + 4, palette, palette_size - 4);
111 }
112
113 pkt->data[palette_size + 0] = sub_type;
114 pkt->data[palette_size + 1] = type;
115 pkt->data[palette_size + 2] = size & 0xFF;
116 pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
117 ret = ffio_read_size(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
118 if (ret < 0)
119 return ret;
120
121 pkt->size = ret + palette_size;
122 pkt->stream_index = avs->st_video->index;
123 if (sub_type == 0)
124 pkt->flags |= AV_PKT_FLAG_KEY;
125
126 return 0;
127}
128
130{
131 AvsFormat *avs = s->priv_data;
132 int ret;
134
135 size = avio_tell(s->pb);
137 size = avio_tell(s->pb) - size;
139
140 if (ret == AVERROR(EIO))
141 return 0; /* this indicate EOS */
142 if (ret < 0)
143 return ret;
144 if (size != (int)size) {
146 return AVERROR(EDOM);
147 }
148
149 pkt->stream_index = avs->st_audio->index;
150 pkt->flags |= AV_PKT_FLAG_KEY;
151
152 return size;
153}
154
156{
157 AvsFormat *avs = s->priv_data;
158 int sub_type = 0, size = 0;
160 int palette_size = 0;
161 uint8_t palette[4 + 3 * 256];
162 int ret;
163
164 if (avs->remaining_audio_size > 0)
165 if (avs_read_audio_packet(s, pkt) > 0)
166 return 0;
167
168 while (1) {
169 if (avs->remaining_frame_size <= 0) {
170 if (!avio_rl16(s->pb)) /* found EOF */
171 return AVERROR_INVALIDDATA;
172 avs->remaining_frame_size = avio_rl16(s->pb) - 4;
173 }
174
175 while (avs->remaining_frame_size > 0) {
176 sub_type = avio_r8(s->pb);
177 type = avio_r8(s->pb);
178 size = avio_rl16(s->pb);
179 if (size < 4)
180 return AVERROR_INVALIDDATA;
182
183 switch (type) {
184 case AVS_PALETTE:
185 if (size - 4 > sizeof(palette))
186 return AVERROR_INVALIDDATA;
187 ret = ffio_read_size(s->pb, palette, size - 4);
188 if (ret < 0)
189 return ret;
190 palette_size = size;
191 break;
192
193 case AVS_VIDEO:
194 if (!avs->st_video) {
196 if (!avs->st_video)
197 return AVERROR(ENOMEM);
200 avs->st_video->codecpar->width = avs->width;
201 avs->st_video->codecpar->height = avs->height;
203 avs->st_video->nb_frames = avs->nb_frames;
204#if FF_API_R_FRAME_RATE
205 avs->st_video->r_frame_rate =
206#endif
207 avs->st_video->avg_frame_rate = (AVRational){avs->fps, 1};
208 }
209 return avs_read_video_packet(s, pkt, type, sub_type, size,
210 palette, palette_size);
211
212 case AVS_AUDIO:
213 if (!avs->st_audio) {
215 if (!avs->st_audio)
216 return AVERROR(ENOMEM);
218 }
219 avs->remaining_audio_size = size - 4;
221 if (size != 0)
222 return size;
223 break;
224
225 default:
226 avio_skip(s->pb, size - 4);
227 }
228 }
229 }
230}
231
233 .p.name = "avs",
234 .p.long_name = NULL_IF_CONFIG_SMALL("Argonaut Games Creature Shock"),
235 .priv_data_size = sizeof(AvsFormat),
239};
const FFInputFormat ff_avs_demuxer
Definition avs.c:232
Main libavformat public API header.
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition avformat.h:1284
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
unsigned int avio_rl16(AVIOContext *s)
Definition aviobuf.c:717
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rl32(AVIOContext *s)
Definition aviobuf.c:733
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_AVS
Definition codec_id.h:132
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition packet.c:98
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
cl_device_type type
AvsBlockType
Definition avs.c:32
@ AVS_PALETTE
Definition avs.c:35
@ AVS_AUDIO
Definition avs.c:34
@ AVS_GAME_DATA
Definition avs.c:36
@ AVS_VIDEO
Definition avs.c:33
@ AVS_NONE
Definition avs.c:48
static int avs_read_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition avs.c:129
static int avs_read_header(AVFormatContext *s)
Definition avs.c:68
static int avs_probe(const AVProbeData *p)
Definition avs.c:55
static int avs_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition avs.c:155
static int avs_read_video_packet(AVFormatContext *s, AVPacket *pkt, AvsBlockType type, int sub_type, int size, uint8_t *palette, int palette_size)
Definition avs.c:94
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
Format I/O context.
Definition avformat.h:1333
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Rational number (pair of numerator and denominator).
Definition rational.h:58
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int index
stream index in AVFormatContext
Definition avformat.h:772
AVRational avg_frame_rate
Average framerate.
Definition avformat.h:855
AVRational r_frame_rate
Real base framerate of the stream.
Definition avformat.h:900
int remaining_frame_size
Definition avs.c:43
AVStream * st_audio
Definition avs.c:37
int fps
Definition avs.c:41
int height
Definition avs.c:39
int bits_per_sample
Definition avs.c:40
int nb_frames
Definition avs.c:42
AVStream * st_video
Definition avs.c:36
VocDecContext voc
Definition avs.c:35
int remaining_audio_size
Definition avs.c:44
int width
Definition avs.c:38
#define av_log(a,...)
int size
int ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition voc_packet.c:27