FFmpeg
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 "voc.h"
30 
31 
32 typedef struct avs_format {
33  VocDecContext voc;
36  int width;
37  int height;
39  int fps;
40  int nb_frames;
43 } AvsFormat;
44 
45 typedef enum avs_block_type {
46  AVS_NONE = 0x00,
47  AVS_VIDEO = 0x01,
48  AVS_AUDIO = 0x02,
49  AVS_PALETTE = 0x03,
50  AVS_GAME_DATA = 0x04,
51 } AvsBlockType;
52 
53 static int avs_probe(const AVProbeData * p)
54 {
55  const uint8_t *d;
56 
57  d = p->buf;
58  if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
59  /* Ensure the buffer probe scores higher than the extension probe.
60  * This avoids problems with misdetection as AviSynth scripts. */
61  return AVPROBE_SCORE_EXTENSION + 5;
62 
63  return 0;
64 }
65 
67 {
68  AvsFormat *avs = s->priv_data;
69 
70  s->ctx_flags |= AVFMTCTX_NOHEADER;
71 
72  avio_skip(s->pb, 4);
73  avs->width = avio_rl16(s->pb);
74  avs->height = avio_rl16(s->pb);
75  avs->bits_per_sample = avio_rl16(s->pb);
76  avs->fps = avio_rl16(s->pb);
77  avs->nb_frames = avio_rl32(s->pb);
78  avs->remaining_frame_size = 0;
79  avs->remaining_audio_size = 0;
80 
81  avs->st_video = avs->st_audio = NULL;
82 
83  if (avs->width != 318 || avs->height != 198)
84  av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
85  "when the avs format is supposed to be 318x198 only.\n",
86  avs->width, avs->height);
87 
88  return 0;
89 }
90 
91 static int
93  AvsBlockType type, int sub_type, int size,
94  uint8_t * palette, int palette_size)
95 {
96  AvsFormat *avs = s->priv_data;
97  int ret;
98 
99  ret = av_new_packet(pkt, size + palette_size);
100  if (ret < 0)
101  return ret;
102 
103  if (palette_size) {
104  pkt->data[0] = 0x00;
105  pkt->data[1] = 0x03;
106  pkt->data[2] = palette_size & 0xFF;
107  pkt->data[3] = (palette_size >> 8) & 0xFF;
108  memcpy(pkt->data + 4, palette, palette_size - 4);
109  }
110 
111  pkt->data[palette_size + 0] = sub_type;
112  pkt->data[palette_size + 1] = type;
113  pkt->data[palette_size + 2] = size & 0xFF;
114  pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
115  ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
116  if (ret < size) {
118  return AVERROR(EIO);
119  }
120 
121  pkt->size = ret + palette_size;
122  pkt->stream_index = avs->st_video->index;
123  if (sub_type == 0)
125 
126  return 0;
127 }
128 
130 {
131  AvsFormat *avs = s->priv_data;
132  int ret;
133  int64_t size;
134 
135  size = avio_tell(s->pb);
136  ret = ff_voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
137  size = avio_tell(s->pb) - size;
138  avs->remaining_audio_size -= size;
139 
140  if (ret == AVERROR(EIO))
141  return 0; /* this indicate EOS */
142  if (ret < 0)
143  return ret;
144 
145  pkt->stream_index = avs->st_audio->index;
147 
148  return size;
149 }
150 
152 {
153  AvsFormat *avs = s->priv_data;
154  int sub_type = 0, size = 0;
156  int palette_size = 0;
157  uint8_t palette[4 + 3 * 256];
158  int ret;
159 
160  if (avs->remaining_audio_size > 0)
161  if (avs_read_audio_packet(s, pkt) > 0)
162  return 0;
163 
164  while (1) {
165  if (avs->remaining_frame_size <= 0) {
166  if (!avio_rl16(s->pb)) /* found EOF */
167  return AVERROR(EIO);
168  avs->remaining_frame_size = avio_rl16(s->pb) - 4;
169  }
170 
171  while (avs->remaining_frame_size > 0) {
172  sub_type = avio_r8(s->pb);
173  type = avio_r8(s->pb);
174  size = avio_rl16(s->pb);
175  if (size < 4)
176  return AVERROR_INVALIDDATA;
177  avs->remaining_frame_size -= size;
178 
179  switch (type) {
180  case AVS_PALETTE:
181  if (size - 4 > sizeof(palette))
182  return AVERROR_INVALIDDATA;
183  ret = avio_read(s->pb, palette, size - 4);
184  if (ret < size - 4)
185  return AVERROR(EIO);
186  palette_size = size;
187  break;
188 
189  case AVS_VIDEO:
190  if (!avs->st_video) {
191  avs->st_video = avformat_new_stream(s, NULL);
192  if (!avs->st_video)
193  return AVERROR(ENOMEM);
194  avs->st_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
195  avs->st_video->codecpar->codec_id = AV_CODEC_ID_AVS;
196  avs->st_video->codecpar->width = avs->width;
197  avs->st_video->codecpar->height = avs->height;
198  avs->st_video->codecpar->bits_per_coded_sample=avs->bits_per_sample;
199  avs->st_video->nb_frames = avs->nb_frames;
200 #if FF_API_R_FRAME_RATE
201  avs->st_video->r_frame_rate =
202 #endif
203  avs->st_video->avg_frame_rate = (AVRational){avs->fps, 1};
204  }
205  return avs_read_video_packet(s, pkt, type, sub_type, size,
206  palette, palette_size);
207 
208  case AVS_AUDIO:
209  if (!avs->st_audio) {
210  avs->st_audio = avformat_new_stream(s, NULL);
211  if (!avs->st_audio)
212  return AVERROR(ENOMEM);
213  avs->st_audio->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
214  }
215  avs->remaining_audio_size = size - 4;
217  if (size != 0)
218  return size;
219  break;
220 
221  default:
222  avio_skip(s->pb, size - 4);
223  }
224  }
225  }
226 }
227 
229 {
230  return 0;
231 }
232 
234  .name = "avs",
235  .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games Creature Shock"),
236  .priv_data_size = sizeof(AvsFormat),
241 };
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:599
AVS_NONE
@ AVS_NONE
Definition: avs.c:46
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame 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. Frame references ownership and permissions
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4480
AVS_VIDEO
@ AVS_VIDEO
Definition: avs.c:31
avs_read_audio_packet
static int avs_read_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avs.c:129
avs_format::height
int height
Definition: avs.c:37
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
avs_read_video_packet
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:92
AVS_AUDIO
@ AVS_AUDIO
Definition: avs.c:32
avs_format::nb_frames
int nb_frames
Definition: avs.c:40
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
avs_format::width
int width
Definition: avs.c:36
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:753
AvsBlockType
AvsBlockType
Definition: avs.c:30
avs_format::bits_per_sample
int bits_per_sample
Definition: avs.c:38
AVS_GAME_DATA
@ AVS_GAME_DATA
Definition: avs.c:34
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVInputFormat
Definition: avformat.h:640
avs_format
Definition: avs.c:32
s
#define s(width, name)
Definition: cbs_vp9.c:257
av_new_packet
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
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:448
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
avs_format::st_video
AVStream * st_video
Definition: avs.c:34
avs_format::voc
VocDecContext voc
Definition: avs.c:33
AVFormatContext
Format I/O context.
Definition: avformat.h:1342
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:530
NULL
#define NULL
Definition: coverity.c:32
read_probe
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1291
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:446
avs_format::st_audio
AVStream * st_audio
Definition: avs.c:35
ff_avs_demuxer
AVInputFormat ff_avs_demuxer
Definition: avs.c:233
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:456
avs_read_close
static int avs_read_close(AVFormatContext *s)
Definition: avs.c:228
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:769
AVPacket::size
int size
Definition: avcodec.h:1478
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
size
int size
Definition: twinvq_data.h:11134
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:638
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
AVS_PALETTE
@ AVS_PALETTE
Definition: avs.c:33
voc.h
ff_voc_get_packet
int ff_voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition: voc_packet.c:27
AV_CODEC_ID_AVS
@ AV_CODEC_ID_AVS
Definition: avcodec.h:300
avs_read_packet
static int avs_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avs.c:151
uint8_t
uint8_t
Definition: audio_convert.c:194
avs_format::fps
int fps
Definition: avs.c:39
ret
ret
Definition: filter_design.txt:187
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
AVStream
Stream structure.
Definition: avformat.h:870
avformat.h
pkt
static AVPacket pkt
Definition: demuxing_decoding.c:54
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:647
AVPacket::stream_index
int stream_index
Definition: avcodec.h:1479
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
avs_block_type
avs_block_type
Definition: avs.c:45
avs_format::remaining_frame_size
int remaining_frame_size
Definition: avs.c:41
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
avs_format::remaining_audio_size
int remaining_audio_size
Definition: avs.c:42
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
avs_read_header
static int avs_read_header(AVFormatContext *s)
Definition: avs.c:66
avs_probe
static int avs_probe(const AVProbeData *p)
Definition: avs.c:53