~kalon33/corsix-th/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright (c) 2012 Stephen Baker

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef TH_VIDEO_H
#define TH_VIDEO_H
#define PICTURE_BUFFER_SIZE 4
#define MOVIE_ERROR_BUFFER_SIZE 128

#include <string>
#include <queue>
#include "config.h"

#ifdef CORSIX_TH_USE_FFMPEG
#include "SDL_mixer.h"

extern "C"
{
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
}

class SDL_Overlay;
class SDL_Surface;
struct SDL_mutex;
struct SDL_cond;

class THMoviePicture
{
public:
    THMoviePicture();
    ~THMoviePicture();

    void allocate(int iX, int iY, int iWidth, int iHeight);
    void deallocate();
    void draw();

    SDL_Overlay *m_pOverlay;
    PixelFormat m_pixelFormat;
    SDL_Surface *m_pSurface;
    int m_iX, m_iY, m_iWidth, m_iHeight;
    double m_dPts;
    SDL_mutex *m_pMutex;
    SDL_cond *m_pCond;
};

class THMoviePictureBuffer
{
public:
    THMoviePictureBuffer();
    ~THMoviePictureBuffer();

    //NB: The following functions are called by the main program thread
    void abort();
    void reset();
    void allocate(int iX, int iY, int iWidth, int iHeight);
    void deallocate();
    bool advance();
    void draw();
    double getCurrentPts();
    double getNextPts();
    bool empty();

    //NB: These functions are called by a second thread
    bool full();
    int write(AVFrame* pFrame, double dPts);
protected:
    bool m_fAborting;
    bool m_fAllocated;
    int m_iCount;
    int m_iReadIndex;
    int m_iWriteIndex;
    SwsContext* m_pSwsContext;
    SDL_mutex *m_pMutex;
    SDL_cond *m_pCond;
    THMoviePicture m_aPictureQueue[PICTURE_BUFFER_SIZE];
};

class THAVPacketQueue
{
public:
    THAVPacketQueue();
    ~THAVPacketQueue();
    void push(AVPacket *packet);
    AVPacket* pull(bool block);
    int getCount();
    void release();
private:
    AVPacketList *m_pFirstPacket, *m_pLastPacket;
    int iCount;
    SDL_mutex *m_pMutex;
    SDL_cond *m_pCond;
};
#endif //CORSIX_TH_USE_FFMPEG

class THMovie
{
public:
    THMovie();
    ~THMovie();

    bool moviesEnabled();

    bool load(const char* szFilepath);
    void unload();

    void play(int iX, int iY, int iWidth, int iHeight, int iChannel);
    void stop();

    int getNativeHeight();
    int getNativeWidth();
    bool hasAudioTrack();
    bool requiresVideoReset();

    const char* getLastError();
    void clearLastError();

    void refresh();
    void deallocatePictureBuffer();
    void allocatePictureBuffer();

    void readStreams();
    void runVideo();
    void copyAudioToStream(uint8_t *pbStream, int iStreamSize);
protected:
#ifdef CORSIX_TH_USE_FFMPEG
    int decodeAudioFrame(bool fFirst);
    int getVideoFrame(AVFrame *pFrame, int64_t *piPts);

    //last error
    std::string m_sLastError;
    char m_szErrorBuffer[MOVIE_ERROR_BUFFER_SIZE];

    //abort playing movie
    bool m_fAborting;

    //current movie dimensions and placement
    int m_iX, m_iY, m_iWidth, m_iHeight;

    //ffmpeg movie information
    AVFormatContext* m_pFormatContext;
    int m_iVideoStream;
    int m_iAudioStream;
    AVCodecContext* m_pVideoCodecContext;
    AVCodecContext* m_pAudioCodecContext;

    //queues for transfering data between threads
    THAVPacketQueue *m_pVideoQueue;
    THAVPacketQueue *m_pAudioQueue;
    THMoviePictureBuffer *m_pMoviePictureBuffer;

    //clock sync parameters
    int m_iCurSyncPtsSystemTime;
    double m_iCurSyncPts;

    //audio resample context
    SwrContext* m_pSwrContext;

    //decoded audio buffer
    int m_iAudioBufferSize;
    int m_iAudioBufferIndex;
    int m_iAudioBufferMaxSize;
    uint8_t* m_pbAudioBuffer;

    //decoding audio packet
    AVPacket* m_pAudioPacket;
    int m_iAudioPacketSize;
    uint8_t *m_pbAudioPacketData;

    //decoding audio frame
    AVFrame* m_frame;

    //empty raw chunk for SDL_mixer
    Mix_Chunk* m_pChunk;
    uint8_t* m_pbChunkBuffer;

    //SDL_mixer parameters
    int m_iChannel;
    int m_iMixerChannels;
    int m_iMixerFrequency;

    //signal packet indicating flush
    AVPacket* m_flushPacket;

    //threads
    SDL_Thread* m_pStreamThread;
    SDL_Thread* m_pVideoThread;
#endif //CORSIX_TH_USE_FFMPEG
};

#endif // TH_VIDEO_H