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 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 
22 #include "avformat.h"
23 #include "voc.h"
24 
25 
26 typedef struct avs_format {
30  int width;
31  int height;
33  int fps;
34  int nb_frames;
37 } AvsFormat;
38 
39 typedef enum avs_block_type {
40  AVS_NONE = 0x00,
41  AVS_VIDEO = 0x01,
42  AVS_AUDIO = 0x02,
43  AVS_PALETTE = 0x03,
44  AVS_GAME_DATA = 0x04,
45 } AvsBlockType;
46 
47 static int avs_probe(AVProbeData * p)
48 {
49  const uint8_t *d;
50 
51  d = p->buf;
52  if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
53  return 50;
54 
55  return 0;
56 }
57 
59 {
60  AvsFormat *avs = s->priv_data;
61 
63 
64  avio_skip(s->pb, 4);
65  avs->width = avio_rl16(s->pb);
66  avs->height = avio_rl16(s->pb);
67  avs->bits_per_sample = avio_rl16(s->pb);
68  avs->fps = avio_rl16(s->pb);
69  avs->nb_frames = avio_rl32(s->pb);
70  avs->remaining_frame_size = 0;
71  avs->remaining_audio_size = 0;
72 
73  avs->st_video = avs->st_audio = NULL;
74 
75  if (avs->width != 318 || avs->height != 198)
76  av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
77  "when the avs format is supposed to be 318x198 only.\n",
78  avs->width, avs->height);
79 
80  return 0;
81 }
82 
83 static int
85  AvsBlockType type, int sub_type, int size,
86  uint8_t * palette, int palette_size)
87 {
88  AvsFormat *avs = s->priv_data;
89  int ret;
90 
91  ret = av_new_packet(pkt, size + palette_size);
92  if (ret < 0)
93  return ret;
94 
95  if (palette_size) {
96  pkt->data[0] = 0x00;
97  pkt->data[1] = 0x03;
98  pkt->data[2] = palette_size & 0xFF;
99  pkt->data[3] = (palette_size >> 8) & 0xFF;
100  memcpy(pkt->data + 4, palette, palette_size - 4);
101  }
102 
103  pkt->data[palette_size + 0] = sub_type;
104  pkt->data[palette_size + 1] = type;
105  pkt->data[palette_size + 2] = size & 0xFF;
106  pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
107  ret = avio_read(s->pb, pkt->data + palette_size + 4, size - 4) + 4;
108  if (ret < size) {
109  av_free_packet(pkt);
110  return AVERROR(EIO);
111  }
112 
113  pkt->size = ret + palette_size;
114  pkt->stream_index = avs->st_video->index;
115  if (sub_type == 0)
116  pkt->flags |= AV_PKT_FLAG_KEY;
117 
118  return 0;
119 }
120 
122 {
123  AvsFormat *avs = s->priv_data;
124  int ret, size;
125 
126  size = avio_tell(s->pb);
127  ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
128  size = avio_tell(s->pb) - size;
129  avs->remaining_audio_size -= size;
130 
131  if (ret == AVERROR(EIO))
132  return 0; /* this indicate EOS */
133  if (ret < 0)
134  return ret;
135 
136  pkt->stream_index = avs->st_audio->index;
137  pkt->flags |= AV_PKT_FLAG_KEY;
138 
139  return size;
140 }
141 
143 {
144  AvsFormat *avs = s->priv_data;
145  int sub_type = 0, size = 0;
146  AvsBlockType type = AVS_NONE;
147  int palette_size = 0;
148  uint8_t palette[4 + 3 * 256];
149  int ret;
150 
151  if (avs->remaining_audio_size > 0)
152  if (avs_read_audio_packet(s, pkt) > 0)
153  return 0;
154 
155  while (1) {
156  if (avs->remaining_frame_size <= 0) {
157  if (!avio_rl16(s->pb)) /* found EOF */
158  return AVERROR(EIO);
159  avs->remaining_frame_size = avio_rl16(s->pb) - 4;
160  }
161 
162  while (avs->remaining_frame_size > 0) {
163  sub_type = avio_r8(s->pb);
164  type = avio_r8(s->pb);
165  size = avio_rl16(s->pb);
166  if (size < 4)
167  return AVERROR_INVALIDDATA;
168  avs->remaining_frame_size -= size;
169 
170  switch (type) {
171  case AVS_PALETTE:
172  if (size - 4 > sizeof(palette))
173  return AVERROR_INVALIDDATA;
174  ret = avio_read(s->pb, palette, size - 4);
175  if (ret < size - 4)
176  return AVERROR(EIO);
177  palette_size = size;
178  break;
179 
180  case AVS_VIDEO:
181  if (!avs->st_video) {
182  avs->st_video = avformat_new_stream(s, NULL);
183  if (avs->st_video == NULL)
184  return AVERROR(ENOMEM);
187  avs->st_video->codec->width = avs->width;
188  avs->st_video->codec->height = avs->height;
190  avs->st_video->nb_frames = avs->nb_frames;
191  avs->st_video->codec->time_base = (AVRational) {
192  1, avs->fps};
193  }
194  return avs_read_video_packet(s, pkt, type, sub_type, size,
195  palette, palette_size);
196 
197  case AVS_AUDIO:
198  if (!avs->st_audio) {
199  avs->st_audio = avformat_new_stream(s, NULL);
200  if (avs->st_audio == NULL)
201  return AVERROR(ENOMEM);
203  }
204  avs->remaining_audio_size = size - 4;
205  size = avs_read_audio_packet(s, pkt);
206  if (size != 0)
207  return size;
208  break;
209 
210  default:
211  avio_skip(s->pb, size - 4);
212  }
213  }
214  }
215 }
216 
218 {
219  return 0;
220 }
221 
223  .name = "avs",
224  .long_name = NULL_IF_CONFIG_SMALL("AVS format"),
225  .priv_data_size = sizeof(AvsFormat),
230 };
struct avs_format AvsFormat
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:60
int index
stream index in AVFormatContext
Definition: avformat.h:621
int size
Definition: avcodec.h:909
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:954
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:919
avs_block_type
Definition: avs.c:39
int height
Definition: avs.c:31
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1398
Definition: avs.c:40
Format I/O context.
Definition: avformat.h:863
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:844
static int avs_read_close(AVFormatContext *s)
Definition: avs.c:217
int fps
Definition: avs.c:33
static int read_header(FFV1Context *f)
Definition: ffv1.c:1513
uint8_t * data
Definition: avcodec.h:908
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:151
VocDecContext voc
Definition: avs.c:27
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:140
int remaining_frame_size
Definition: avs.c:35
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:492
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1974
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
Definition: avs.c:26
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:738
#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:191
Definition: avs.c:31
AVInputFormat ff_avs_demuxer
Definition: avs.c:222
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:140
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2776
int voc_get_packet(AVFormatContext *s, AVPacket *pkt, AVStream *st, int max_size)
Definition: vocdec.c:65
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:914
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:632
AVCodecContext * codec
codec context
Definition: avformat.h:623
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:341
struct AVRational AVRational
rational number numerator/denominator
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:84
int width
picture width / height.
Definition: avcodec.h:1408
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:483
AVStream * st_video
Definition: avs.c:28
static int read_probe(AVProbeData *p)
Definition: img2.c:185
int bits_per_sample
Definition: avs.c:32
static int avs_probe(AVProbeData *p)
Definition: avs.c:47
Stream structure.
Definition: avformat.h:620
NULL
Definition: eval.c:50
enum AVMediaType codec_type
Definition: avcodec.h:1574
AVIOContext * pb
Definition: avformat.h:896
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:111
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:109
static int avs_read_audio_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avs.c:121
AvsBlockType
Definition: avs.c:30
This structure contains the data a format has to probe a file.
Definition: avformat.h:339
int nb_frames
Definition: avs.c:34
int palette
Definition: v4l.c:63
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:722
Main libavformat public API header.
Definition: avs.c:32
int remaining_audio_size
Definition: avs.c:36
AVStream * st_audio
Definition: avs.c:29
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:701
void * priv_data
Format private data.
Definition: avformat.h:883
static int avs_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: avs.c:142
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:460
int stream_index
Definition: avcodec.h:910
static int avs_read_header(AVFormatContext *s, AVFormatParameters *ap)
Definition: avs.c:58
int width
Definition: avs.c:30
enum CodecID codec_id
Definition: avcodec.h:1575