Sirikata
|
00001 // Copyright (c) 2011 Sirikata Authors. All rights reserved. 00002 // Use of this source code is governed by a BSD-style license that can 00003 // be found in the LICENSE file. 00004 00005 #ifndef _SIRIKATA_SDLAUDIO_FFMPEG_AUDIO_STREAM_HPP_ 00006 #define _SIRIKATA_SDLAUDIO_FFMPEG_AUDIO_STREAM_HPP_ 00007 00008 #include <sirikata/proxyobject/Platform.hpp> 00009 00010 extern "C" { 00011 struct AVCodecContext; 00012 struct AVCodec; 00013 struct AVPacket; 00014 struct AVAudioConvert; 00015 00016 struct SDL_AudioCVT; 00017 } 00018 00019 namespace Sirikata { 00020 namespace SDL { 00021 00022 class FFmpegStream; 00023 typedef std::tr1::shared_ptr<FFmpegStream> FFmpegStreamPtr; 00024 00025 class FFmpegAudioStream; 00026 typedef std::tr1::shared_ptr<FFmpegAudioStream> FFmpegAudioStreamPtr; 00027 00029 class FFmpegAudioStream { 00030 public: 00031 ~FFmpegAudioStream(); 00032 00033 // Returns true if this stream has finished decoding. 00034 bool finished() const; 00035 00045 void samples(int16* samples_out, bool loop); 00046 00047 private: 00048 friend class FFmpegStream; 00049 00050 FFmpegAudioStream(FFmpegStreamPtr parent, uint32 stream_idx, uint8 target_nchannels); 00051 00052 // These are factored out so we can rerun them. Other components shouldn't 00053 // need cleanup and reinitialization. 00054 bool openCodec(); 00055 void closeCodec(); 00056 00057 AVPacket* getNextPacket(bool loop); 00058 void decodeSome(bool loop); 00059 void convertFormat(int decoded_size); 00060 00061 FFmpegStreamPtr mParent; 00062 AVCodecContext* mCodecCtx; 00063 AVCodec* mCodec; 00064 // Index of the stream in the parent stream 00065 uint32 mStreamIndex; 00066 00067 bool mFinished; 00068 00069 // Data we're still working on decoding and our progress through it 00070 AVPacket* mCurrentPacket; 00071 int mCurrentPacketOffset; 00072 00073 // Decoded audio data, directly from ffmpeg 00074 uint8* mDecodedData; 00075 00076 // Decoded audio data, converted to target format 00077 SDL_AudioCVT* mConverter; 00078 uint8 mTargetChannels; 00079 uint16* mConvertedData; 00080 // Note that unlike some other places, these are in terms of 00081 // uint16 (individual samples, not bytes or 00082 // samples*channels). This matches the storage (mConvertedData's 00083 // type) so we can index using the offset. 00084 int mConvertedOffset; 00085 int mConvertedSize; 00086 }; 00087 00088 00089 } // namespace SDL 00090 } // namespace Sirikata 00091 00092 #endif //_SIRIKATA_SDLAUDIO_FFMPEG_AUDIO_STREAM_HPP_