3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of FFmpeg.
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.
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.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
26 typedef struct avs_format {
27 voc_dec_context_t voc;
35 int remaining_frame_size;
36 int remaining_audio_size;
39 typedef enum avs_block_type {
46 static int avs_probe(AVProbeData * p)
51 if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
57 static int avs_read_header(AVFormatContext * s, AVFormatParameters * ap)
59 avs_format_t *avs = s->priv_data;
61 s->ctx_flags |= AVFMTCTX_NOHEADER;
64 avs->width = get_le16(&s->pb);
65 avs->height = get_le16(&s->pb);
66 avs->bits_per_sample = get_le16(&s->pb);
67 avs->fps = get_le16(&s->pb);
68 avs->nb_frames = get_le32(&s->pb);
69 avs->remaining_frame_size = 0;
70 avs->remaining_audio_size = 0;
72 avs->st_video = avs->st_audio = NULL;
74 if (avs->width != 318 || avs->height != 198)
75 av_log(s, AV_LOG_ERROR, "This avs pretend to be %dx%d "
76 "when the avs format is supposed to be 318x198 only.\n",
77 avs->width, avs->height);
83 avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
84 avs_block_type_t type, int sub_type, int size,
85 uint8_t * palette, int palette_size)
87 avs_format_t *avs = s->priv_data;
90 ret = av_new_packet(pkt, size + palette_size);
97 pkt->data[2] = palette_size & 0xFF;
98 pkt->data[3] = (palette_size >> 8) & 0xFF;
99 memcpy(pkt->data + 4, palette, palette_size - 4);
102 pkt->data[palette_size + 0] = sub_type;
103 pkt->data[palette_size + 1] = type;
104 pkt->data[palette_size + 2] = size & 0xFF;
105 pkt->data[palette_size + 3] = (size >> 8) & 0xFF;
106 ret = get_buffer(&s->pb, pkt->data + palette_size + 4, size - 4) + 4;
112 pkt->size = ret + palette_size;
113 pkt->stream_index = avs->st_video->index;
115 pkt->flags |= PKT_FLAG_KEY;
120 static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
122 avs_format_t *avs = s->priv_data;
125 size = url_ftell(&s->pb);
126 ret = voc_get_packet(s, pkt, avs->st_audio, avs->remaining_audio_size);
127 size = url_ftell(&s->pb) - size;
128 avs->remaining_audio_size -= size;
130 if (ret == AVERROR_IO)
131 return 0; /* this indicate EOS */
135 pkt->stream_index = avs->st_audio->index;
136 pkt->flags |= PKT_FLAG_KEY;
141 static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
143 avs_format_t *avs = s->priv_data;
144 int sub_type = 0, size = 0;
145 avs_block_type_t type = 0;
146 int palette_size = 0;
147 uint8_t palette[4 + 3 * 256];
150 if (avs->remaining_audio_size > 0)
151 if (avs_read_audio_packet(s, pkt) > 0)
155 if (avs->remaining_frame_size <= 0) {
156 if (!get_le16(&s->pb)) /* found EOF */
158 avs->remaining_frame_size = get_le16(&s->pb) - 4;
161 while (avs->remaining_frame_size > 0) {
162 sub_type = get_byte(&s->pb);
163 type = get_byte(&s->pb);
164 size = get_le16(&s->pb);
165 avs->remaining_frame_size -= size;
169 ret = get_buffer(&s->pb, palette, size - 4);
176 if (!avs->st_video) {
177 avs->st_video = av_new_stream(s, AVS_VIDEO);
178 if (avs->st_video == NULL)
179 return AVERROR_NOMEM;
180 avs->st_video->codec->codec_type = CODEC_TYPE_VIDEO;
181 avs->st_video->codec->codec_id = CODEC_ID_AVS;
182 avs->st_video->codec->width = avs->width;
183 avs->st_video->codec->height = avs->height;
184 avs->st_video->codec->bits_per_sample=avs->bits_per_sample;
185 avs->st_video->nb_frames = avs->nb_frames;
186 avs->st_video->codec->time_base = (AVRational) {
189 return avs_read_video_packet(s, pkt, type, sub_type, size,
190 palette, palette_size);
193 if (!avs->st_audio) {
194 avs->st_audio = av_new_stream(s, AVS_AUDIO);
195 if (avs->st_audio == NULL)
196 return AVERROR_NOMEM;
197 avs->st_audio->codec->codec_type = CODEC_TYPE_AUDIO;
199 avs->remaining_audio_size = size - 4;
200 size = avs_read_audio_packet(s, pkt);
206 url_fskip(&s->pb, size - 4);
212 static int avs_read_close(AVFormatContext * s)
217 AVInputFormat avs_demuxer = {
220 sizeof(avs_format_t),