3 * Copyright (c) 2006 Konstantin Shishkov
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
22 #include "libavutil/intreadwrite.h"
27 // specs say that maximum block size is 1Mb
28 #define WV_BLOCK_LIMIT 1047576
30 #define WV_EXTRA_SIZE 12
46 static const int wv_rates
[16] = {
47 6000, 8000, 9600, 11025, 12000, 16000, 22050, 24000,
48 32000, 44100, 48000, 64000, 88200, 96000, 192000, -1
52 uint32_t blksize
, flags
;
54 uint32_t samples
, soff
;
56 uint8_t extra
[WV_EXTRA_SIZE
];
60 static int wv_probe(AVProbeData
*p
)
62 /* check file header */
63 if (p
->buf_size
<= 32)
65 if (p
->buf
[0] == 'w' && p
->buf
[1] == 'v' &&
66 p
->buf
[2] == 'p' && p
->buf
[3] == 'k')
67 return AVPROBE_SCORE_MAX
;
72 static int wv_read_block_header(AVFormatContext
*ctx
, ByteIOContext
*pb
)
74 WVContext
*wc
= ctx
->priv_data
;
79 wc
->pos
= url_ftell(pb
);
81 if (tag
!= MKTAG('w', 'v', 'p', 'k'))
84 if(size
< 24 || size
> WV_BLOCK_LIMIT
){
85 av_log(ctx
, AV_LOG_ERROR
, "Incorrect block size %i\n", size
);
90 if(ver
< 0x402 || ver
> 0x410){
91 av_log(ctx
, AV_LOG_ERROR
, "Unsupported version %03X\n", ver
);
94 get_byte(pb
); // track no
95 get_byte(pb
); // track sub index
96 wc
->samples
= get_le32(pb
); // total samples in file
97 wc
->soff
= get_le32(pb
); // offset in samples of current block
98 get_buffer(pb
, wc
->extra
, WV_EXTRA_SIZE
);
99 wc
->flags
= AV_RL32(wc
->extra
+ 4);
101 bpp
= ((wc
->flags
& 3) + 1) << 3;
102 chan
= 1 + !(wc
->flags
& WV_MONO
);
103 rate
= wv_rates
[(wc
->flags
>> 23) & 0xF];
104 if(rate
== -1 && !wc
->block_parsed
){
105 int64_t block_end
= url_ftell(pb
) + wc
->blksize
- 24;
106 if(url_is_streamed(pb
)){
107 av_log(ctx
, AV_LOG_ERROR
, "Cannot determine custom sampling rate\n");
110 while(url_ftell(pb
) < block_end
){
113 size
= (id
& 0x80) ? get_le24(pb
) : get_byte(pb
);
117 if((id
&0x3F) == 0x27){
125 av_log(ctx
, AV_LOG_ERROR
, "Cannot determine custom sampling rate\n");
128 url_fseek(pb
, block_end
- wc
->blksize
+ 24, SEEK_SET
);
130 if(!wc
->bpp
) wc
->bpp
= bpp
;
131 if(!wc
->chan
) wc
->chan
= chan
;
132 if(!wc
->rate
) wc
->rate
= rate
;
134 if(wc
->flags
&& bpp
!= wc
->bpp
){
135 av_log(ctx
, AV_LOG_ERROR
, "Bits per sample differ, this block: %i, header block: %i\n", bpp
, wc
->bpp
);
138 if(wc
->flags
&& chan
!= wc
->chan
){
139 av_log(ctx
, AV_LOG_ERROR
, "Channels differ, this block: %i, header block: %i\n", chan
, wc
->chan
);
142 if(wc
->flags
&& rate
!= -1 && rate
!= wc
->rate
){
143 av_log(ctx
, AV_LOG_ERROR
, "Sampling rate differ, this block: %i, header block: %i\n", rate
, wc
->rate
);
146 wc
->blksize
= size
- 24;
150 static int wv_read_header(AVFormatContext
*s
,
151 AVFormatParameters
*ap
)
153 ByteIOContext
*pb
= s
->pb
;
154 WVContext
*wc
= s
->priv_data
;
157 wc
->block_parsed
= 0;
158 if(wv_read_block_header(s
, pb
) < 0)
161 /* now we are ready: build format streams */
162 st
= av_new_stream(s
, 0);
165 st
->codec
->codec_type
= CODEC_TYPE_AUDIO
;
166 st
->codec
->codec_id
= CODEC_ID_WAVPACK
;
167 st
->codec
->channels
= wc
->chan
;
168 st
->codec
->sample_rate
= wc
->rate
;
169 st
->codec
->bits_per_coded_sample
= wc
->bpp
;
170 av_set_pts_info(st
, 64, 1, wc
->rate
);
172 s
->duration
= (int64_t)wc
->samples
* AV_TIME_BASE
/ st
->codec
->sample_rate
;
174 if(!url_is_streamed(s
->pb
)) {
175 int64_t cur
= url_ftell(s
->pb
);
177 if(!av_metadata_get(s
->metadata
, "", NULL
, AV_METADATA_IGNORE_SUFFIX
))
179 url_fseek(s
->pb
, cur
, SEEK_SET
);
185 static int wv_read_packet(AVFormatContext
*s
,
188 WVContext
*wc
= s
->priv_data
;
193 if(wc
->block_parsed
){
194 if(wv_read_block_header(s
, s
->pb
) < 0)
198 if(av_new_packet(pkt
, wc
->blksize
+ WV_EXTRA_SIZE
) < 0)
199 return AVERROR(ENOMEM
);
200 memcpy(pkt
->data
, wc
->extra
, WV_EXTRA_SIZE
);
201 ret
= get_buffer(s
->pb
, pkt
->data
+ WV_EXTRA_SIZE
, wc
->blksize
);
202 if(ret
!= wc
->blksize
){
206 pkt
->stream_index
= 0;
207 wc
->block_parsed
= 1;
208 pkt
->size
= ret
+ WV_EXTRA_SIZE
;
210 av_add_index_entry(s
->streams
[0], wc
->pos
, pkt
->pts
, 0, 0, AVINDEX_KEYFRAME
);
214 static int wv_read_seek(AVFormatContext
*s
, int stream_index
, int64_t timestamp
, int flags
)
216 AVStream
*st
= s
->streams
[stream_index
];
217 WVContext
*wc
= s
->priv_data
;
218 AVPacket pkt1
, *pkt
= &pkt1
;
220 int index
= av_index_search_timestamp(st
, timestamp
, flags
);
223 /* if found, seek there */
225 wc
->block_parsed
= 1;
226 url_fseek(s
->pb
, st
->index_entries
[index
].pos
, SEEK_SET
);
229 /* if timestamp is out of bounds, return error */
230 if(timestamp
< 0 || timestamp
>= s
->duration
)
233 pos
= url_ftell(s
->pb
);
235 ret
= av_read_frame(s
, pkt
);
237 url_fseek(s
->pb
, pos
, SEEK_SET
);
242 }while(pts
< timestamp
);
246 AVInputFormat wv_demuxer
= {
248 NULL_IF_CONFIG_SMALL("WavPack"),