3 * Copyright (c) 2006 Aurelien Jacobs <aurel@gnuage.org>
5 * This file is part of Libav.
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.
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.
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
26 typedef struct avs_format {
35 int remaining_frame_size;
36 int remaining_audio_size;
39 typedef enum avs_block_type {
47 static int avs_probe(AVProbeData * p)
52 if (d[0] == 'w' && d[1] == 'W' && d[2] == 0x10 && d[3] == 0)
58 static int avs_read_header(AVFormatContext * s)
60 AvsFormat *avs = s->priv_data;
62 s->ctx_flags |= AVFMTCTX_NOHEADER;
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;
73 avs->st_video = avs->st_audio = NULL;
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);
84 avs_read_video_packet(AVFormatContext * s, AVPacket * pkt,
85 AvsBlockType type, int sub_type, int size,
86 uint8_t * palette, int palette_size)
88 AvsFormat *avs = s->priv_data;
91 ret = av_new_packet(pkt, size + palette_size);
98 pkt->data[2] = palette_size & 0xFF;
99 pkt->data[3] = (palette_size >> 8) & 0xFF;
100 memcpy(pkt->data + 4, palette, palette_size - 4);
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;
113 pkt->size = ret + palette_size;
114 pkt->stream_index = avs->st_video->index;
116 pkt->flags |= AV_PKT_FLAG_KEY;
121 static int avs_read_audio_packet(AVFormatContext * s, AVPacket * pkt)
123 AvsFormat *avs = s->priv_data;
126 size = avio_tell(s->pb);
127 ret = ff_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;
131 if (ret == AVERROR(EIO))
132 return 0; /* this indicate EOS */
136 pkt->stream_index = avs->st_audio->index;
137 pkt->flags |= AV_PKT_FLAG_KEY;
142 static int avs_read_packet(AVFormatContext * s, AVPacket * pkt)
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];
151 if (avs->remaining_audio_size > 0)
152 if (avs_read_audio_packet(s, pkt) > 0)
156 if (avs->remaining_frame_size <= 0) {
157 if (!avio_rl16(s->pb)) /* found EOF */
159 avs->remaining_frame_size = avio_rl16(s->pb) - 4;
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);
167 return AVERROR_INVALIDDATA;
168 avs->remaining_frame_size -= size;
172 if (size - 4 > sizeof(palette))
173 return AVERROR_INVALIDDATA;
174 ret = avio_read(s->pb, palette, size - 4);
181 if (!avs->st_video) {
182 avs->st_video = avformat_new_stream(s, NULL);
183 if (avs->st_video == NULL)
184 return AVERROR(ENOMEM);
185 avs->st_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
186 avs->st_video->codec->codec_id = CODEC_ID_AVS;
187 avs->st_video->codec->width = avs->width;
188 avs->st_video->codec->height = avs->height;
189 avs->st_video->codec->bits_per_coded_sample=avs->bits_per_sample;
190 avs->st_video->nb_frames = avs->nb_frames;
191 avs->st_video->r_frame_rate = avs->st_video->avg_frame_rate =
192 (AVRational){avs->fps, 1};
194 return avs_read_video_packet(s, pkt, type, sub_type, size,
195 palette, palette_size);
198 if (!avs->st_audio) {
199 avs->st_audio = avformat_new_stream(s, NULL);
200 if (avs->st_audio == NULL)
201 return AVERROR(ENOMEM);
202 avs->st_audio->codec->codec_type = AVMEDIA_TYPE_AUDIO;
204 avs->remaining_audio_size = size - 4;
205 size = avs_read_audio_packet(s, pkt);
211 avio_skip(s->pb, size - 4);
217 static int avs_read_close(AVFormatContext * s)
222 AVInputFormat ff_avs_demuxer = {
224 .long_name = NULL_IF_CONFIG_SMALL("AVS format"),
225 .priv_data_size = sizeof(AvsFormat),
226 .read_probe = avs_probe,
227 .read_header = avs_read_header,
228 .read_packet = avs_read_packet,
229 .read_close = avs_read_close,