1 #ifndef GAME_SOUND_FFMPEG_DECODER_H
2 #define GAME_SOUND_FFMPEG_DECODER_H
3 
4 #include <stdint.h>
5 
6 #if defined(_MSC_VER)
7     #pragma warning (push)
8     #pragma warning (disable : 4244)
9 #endif
10 
11 extern "C"
12 {
13 #include <libavcodec/avcodec.h>
14 #include <libavformat/avformat.h>
15 #include <libavutil/channel_layout.h>
16 
17 // From version 54.56 binkaudio encoding format changed from S16 to FLTP. See:
18 // https://gitorious.org/ffmpeg/ffmpeg/commit/7bfd1766d1c18f07b0a2dd042418a874d49ea60d
19 // https://ffmpeg.zeranoe.com/forum/viewtopic.php?f=15&t=872
20 #include <libswresample/swresample.h>
21 }
22 
23 #if defined(_MSC_VER)
24     #pragma warning (pop)
25 #endif
26 
27 #include <components/files/constrainedfilestream.hpp>
28 
29 #include <string>
30 #include <istream>
31 
32 #include "sound_decoder.hpp"
33 
34 
35 namespace MWSound
36 {
37     class FFmpeg_Decoder final : public Sound_Decoder
38     {
39         AVFormatContext *mFormatCtx;
40         AVCodecContext *mCodecCtx;
41         AVStream **mStream;
42 
43         AVPacket mPacket;
44         AVFrame *mFrame;
45 
46         int mFrameSize;
47         int mFramePos;
48 
49         double mNextPts;
50 
51         SwrContext *mSwr;
52         enum AVSampleFormat mOutputSampleFormat;
53         int64_t mOutputChannelLayout;
54         uint8_t *mDataBuf;
55         uint8_t **mFrameData;
56         int mDataBufLen;
57 
58         bool getNextPacket();
59 
60         Files::IStreamPtr mDataStream;
61 
62         static int readPacket(void *user_data, uint8_t *buf, int buf_size);
63         static int writePacket(void *user_data, uint8_t *buf, int buf_size);
64         static int64_t seek(void *user_data, int64_t offset, int whence);
65 
66         bool getAVAudioData();
67         size_t readAVAudioData(void *data, size_t length);
68 
69         void open(const std::string &fname) override;
70         void close() override;
71 
72         std::string getName() override;
73         void getInfo(int *samplerate, ChannelConfig *chans, SampleType *type) override;
74 
75         size_t read(char *buffer, size_t bytes) override;
76         void readAll(std::vector<char> &output) override;
77         size_t getSampleOffset() override;
78 
79         FFmpeg_Decoder& operator=(const FFmpeg_Decoder &rhs);
80         FFmpeg_Decoder(const FFmpeg_Decoder &rhs);
81 
82     public:
83         explicit FFmpeg_Decoder(const VFS::Manager* vfs);
84 
85         virtual ~FFmpeg_Decoder();
86 
87         friend class SoundManager;
88     };
89 }
90 
91 #endif
92