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 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)
53 /* Ensure the buffer probe scores higher than the extension probe.
54 * This avoids problems with misdetection as AviSynth scripts. */
55 return AVPROBE_SCORE_EXTENSION
+ 5;
60 static int avs_read_header(AVFormatContext
* s
)
62 AvsFormat
*avs
= s
->priv_data
;
64 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
67 avs
->width
= avio_rl16(s
->pb
);
68 avs
->height
= avio_rl16(s
->pb
);
69 avs
->bits_per_sample
= avio_rl16(s
->pb
);
70 avs
->fps
= avio_rl16(s
->pb
);
71 avs
->nb_frames
= avio_rl32(s
->pb
);
72 avs
->remaining_frame_size
= 0;
73 avs
->remaining_audio_size
= 0;
75 avs
->st_video
= avs
->st_audio
= NULL
;
77 if (avs
->width
!= 318 || avs
->height
!= 198)
78 av_log(s
, AV_LOG_ERROR
, "This avs pretend to be %dx%d "
79 "when the avs format is supposed to be 318x198 only.\n",
80 avs
->width
, avs
->height
);
86 avs_read_video_packet(AVFormatContext
* s
, AVPacket
* pkt
,
87 AvsBlockType type
, int sub_type
, int size
,
88 uint8_t * palette
, int palette_size
)
90 AvsFormat
*avs
= s
->priv_data
;
93 ret
= av_new_packet(pkt
, size
+ palette_size
);
100 pkt
->data
[2] = palette_size
& 0xFF;
101 pkt
->data
[3] = (palette_size
>> 8) & 0xFF;
102 memcpy(pkt
->data
+ 4, palette
, palette_size
- 4);
105 pkt
->data
[palette_size
+ 0] = sub_type
;
106 pkt
->data
[palette_size
+ 1] = type
;
107 pkt
->data
[palette_size
+ 2] = size
& 0xFF;
108 pkt
->data
[palette_size
+ 3] = (size
>> 8) & 0xFF;
109 ret
= avio_read(s
->pb
, pkt
->data
+ palette_size
+ 4, size
- 4) + 4;
115 pkt
->size
= ret
+ palette_size
;
116 pkt
->stream_index
= avs
->st_video
->index
;
118 pkt
->flags
|= AV_PKT_FLAG_KEY
;
123 static int avs_read_audio_packet(AVFormatContext
* s
, AVPacket
* pkt
)
125 AvsFormat
*avs
= s
->priv_data
;
128 size
= avio_tell(s
->pb
);
129 ret
= ff_voc_get_packet(s
, pkt
, avs
->st_audio
, avs
->remaining_audio_size
);
130 size
= avio_tell(s
->pb
) - size
;
131 avs
->remaining_audio_size
-= size
;
133 if (ret
== AVERROR(EIO
))
134 return 0; /* this indicate EOS */
138 pkt
->stream_index
= avs
->st_audio
->index
;
139 pkt
->flags
|= AV_PKT_FLAG_KEY
;
144 static int avs_read_packet(AVFormatContext
* s
, AVPacket
* pkt
)
146 AvsFormat
*avs
= s
->priv_data
;
147 int sub_type
= 0, size
= 0;
148 AvsBlockType type
= AVS_NONE
;
149 int palette_size
= 0;
150 uint8_t palette
[4 + 3 * 256];
153 if (avs
->remaining_audio_size
> 0)
154 if (avs_read_audio_packet(s
, pkt
) > 0)
158 if (avs
->remaining_frame_size
<= 0) {
159 if (!avio_rl16(s
->pb
)) /* found EOF */
161 avs
->remaining_frame_size
= avio_rl16(s
->pb
) - 4;
164 while (avs
->remaining_frame_size
> 0) {
165 sub_type
= avio_r8(s
->pb
);
166 type
= avio_r8(s
->pb
);
167 size
= avio_rl16(s
->pb
);
169 return AVERROR_INVALIDDATA
;
170 avs
->remaining_frame_size
-= size
;
174 if (size
- 4 > sizeof(palette
))
175 return AVERROR_INVALIDDATA
;
176 ret
= avio_read(s
->pb
, palette
, size
- 4);
183 if (!avs
->st_video
) {
184 avs
->st_video
= avformat_new_stream(s
, NULL
);
186 return AVERROR(ENOMEM
);
187 avs
->st_video
->codec
->codec_type
= AVMEDIA_TYPE_VIDEO
;
188 avs
->st_video
->codec
->codec_id
= AV_CODEC_ID_AVS
;
189 avs
->st_video
->codec
->width
= avs
->width
;
190 avs
->st_video
->codec
->height
= avs
->height
;
191 avs
->st_video
->codec
->bits_per_coded_sample
=avs
->bits_per_sample
;
192 avs
->st_video
->nb_frames
= avs
->nb_frames
;
193 #if FF_API_R_FRAME_RATE
194 avs
->st_video
->r_frame_rate
=
196 avs
->st_video
->avg_frame_rate
= (AVRational
){avs
->fps
, 1};
198 return avs_read_video_packet(s
, pkt
, type
, sub_type
, size
,
199 palette
, palette_size
);
202 if (!avs
->st_audio
) {
203 avs
->st_audio
= avformat_new_stream(s
, NULL
);
205 return AVERROR(ENOMEM
);
206 avs
->st_audio
->codec
->codec_type
= AVMEDIA_TYPE_AUDIO
;
208 avs
->remaining_audio_size
= size
- 4;
209 size
= avs_read_audio_packet(s
, pkt
);
215 avio_skip(s
->pb
, size
- 4);
221 static int avs_read_close(AVFormatContext
* s
)
226 AVInputFormat ff_avs_demuxer
= {
228 .long_name
= NULL_IF_CONFIG_SMALL("AVS"),
229 .priv_data_size
= sizeof(AvsFormat
),
230 .read_probe
= avs_probe
,
231 .read_header
= avs_read_header
,
232 .read_packet
= avs_read_packet
,
233 .read_close
= avs_read_close
,