00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __WVOGGVORBIS_H
00009 #define __WVOGGVORBIS_H
00010
00011 #include "wvtypedencoder.h"
00012 #include "wvstringlist.h"
00013 #include <ogg/ogg.h>
00014 #include <vorbis/codec.h>
00015 #include <vorbis/vorbisenc.h>
00016
00026 class WvOggVorbisEncoder :
00027 public WvTypedEncoder<float, unsigned char>
00028 {
00029 public:
00030 static const long RANDOM_SERIALNO = 0;
00031
00039 class BitrateSpec
00040 {
00041 friend class WvOggVorbisEncoder;
00042 protected:
00043 enum Mode { VBR_QUALITY, VBR_BITRATE };
00044 Mode mode;
00045 float quality_index;
00046 long max_bitrate;
00047 long nominal_bitrate;
00048 long min_bitrate;
00049
00050 BitrateSpec(Mode mode) : mode(mode) { }
00051 public:
00052
00053 BitrateSpec() { }
00054 };
00055
00060 class VBRQuality : public BitrateSpec
00061 {
00062 public:
00067 VBRQuality(float quality) : BitrateSpec(VBR_QUALITY)
00068 {
00069 quality_index = quality;
00070 }
00071 };
00072
00077 class VBRBitrate : public BitrateSpec
00078 {
00079 public:
00084 VBRBitrate(long nominal) : BitrateSpec(VBR_BITRATE)
00085 {
00086 max_bitrate = -1;
00087 nominal_bitrate = nominal;
00088 min_bitrate = -1;
00089 }
00096 VBRBitrate(long max, long nominal, long min) :
00097 BitrateSpec(VBR_BITRATE)
00098 {
00099 max_bitrate = max;
00100 nominal_bitrate = nominal;
00101 min_bitrate = min;
00102 }
00103 };
00104
00120 WvOggVorbisEncoder(const BitrateSpec &bitratespec,
00121 int samplingrate, int channels = 1,
00122 long serialno = RANDOM_SERIALNO);
00123
00124 virtual ~WvOggVorbisEncoder();
00125
00133 void add_comment(WvStringParm comment);
00134
00141 void add_comment(WVSTRING_FORMAT_DECL)
00142 { add_comment(WvString(WVSTRING_FORMAT_CALL)); }
00143
00156 void add_tag(WvStringParm tag, WvStringParm value);
00157
00158 protected:
00159 virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf,
00160 bool flush);
00161 virtual bool _typedfinish(OBuffer &outbuf);
00162
00163 private:
00164 ogg_stream_state *oggstream;
00165 vorbis_info *ovinfo;
00166 vorbis_comment *ovcomment;
00167 vorbis_dsp_state *ovdsp;
00168 vorbis_block *ovblock;
00169 bool wrote_header;
00170
00171 bool write_header(OBuffer &outbuf);
00172 bool write_stream(OBuffer &outbuf, bool flush = false);
00173 bool process_audio(OBuffer &outbuf);
00174 };
00175
00176
00191 class WvOggVorbisDecoder :
00192 public WvTypedEncoder<unsigned char, float>
00193 {
00194 WvStringList comment_list;
00195
00196 public:
00204 WvOggVorbisDecoder();
00205 virtual ~WvOggVorbisDecoder();
00206
00215 bool isheaderok() const;
00216
00222 WvString vendor() const
00223 { return ovcomment->vendor; }
00224
00232 WvStringList &comments()
00233 { return comment_list; }
00234
00240 int channels() const
00241 { return ovinfo->channels; }
00242
00248 int samplingrate() const
00249 { return ovinfo->rate; }
00250
00251 protected:
00252 virtual bool _typedencode(IBuffer &inbuf, OBuffer &outbuf,
00253 bool flush);
00254 virtual bool _typedfinish(OBuffer &outbuf);
00255
00256 private:
00257 ogg_sync_state *oggsync;
00258 ogg_stream_state *oggstream;
00259 ogg_page *oggpage;
00260 vorbis_info *ovinfo;
00261 vorbis_comment *ovcomment;
00262 vorbis_dsp_state *ovdsp;
00263 vorbis_block *ovblock;
00264 bool need_serialno;
00265 int need_headers;
00266
00267 bool process_page(ogg_page *oggpage, OBuffer &outbuf);
00268 bool process_packet(ogg_packet *oggpacket, OBuffer &outbuf);
00269 bool prepare_dsp();
00270 bool prepare_stream(long serialno);
00271 };
00272
00273 #endif // __WVOGGVORBIS