/
Decoder.hpp
409 lines (321 loc) · 11.5 KB
/
Decoder.hpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
extern "C"
{
#include <libavutil/avutil.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
}
typedef enum {
kxMovieErrorNone,
kxMovieErrorOpenFile,
kxMovieErrorStreamInfoNotFound,
kxMovieErrorStreamNotFound,
kxMovieErrorCodecNotFound,
kxMovieErrorOpenCodec,
kxMovieErrorAllocateFrame,
kxMovieErroSetupScaler,
kxMovieErroReSampler,
kxMovieErroUnsupported,
} kxMovieError;
static size_t FeedCallBackFun(void* userData, const ChunkInfoRef chunkinfo, bool terminated);
//static int interruptCallback(void *)
//{
// SP::printf("interruptCallback!!!!!\n");
// return 0;
//}
struct DecoderSignals
{
void* _pusr;
void (*fileEOF)(void *pusr);
void (*fileOpen)(bool , int channel, int freq, void *pusr);
};
static const char * errorMessage (kxMovieError errorCode)
{
switch (errorCode) {
case kxMovieErrorNone:
return "";
case kxMovieErrorOpenFile:
return "Unable to open file";
case kxMovieErrorStreamInfoNotFound:
return "Unable to find stream information";
case kxMovieErrorStreamNotFound:
return "Unable to find stream";
case kxMovieErrorCodecNotFound:
return "Unable to find codec";
case kxMovieErrorOpenCodec:
return "Unable to open codec";
case kxMovieErrorAllocateFrame:
return "Unable to allocate frame";
case kxMovieErroSetupScaler:
return "Unable to setup scaler";
case kxMovieErroReSampler:
return "Unable to setup resampler";
case kxMovieErroUnsupported:
return "The ability is not supported";
}
}
#include <SDL2/SDL_audio.h>
struct Decoder : public IceUtil::Thread
{
BytesBufferPtr _pBuffer;
BufferChunk _feedChunk;
AVFormatContext *_formatCtx;
AVCodecContext *_audioCodecCtx;
AVFrame *_audioFrame;
SwrContext *_swrContext;
AVPacket _packet;
size_t _audioStream;
std::string _file;
float _audioTimeBase;
bool _isEOF;
//SDL_AudioFormat _format;
int (*_interruptCallback) (void*);
DecoderSignals _signals;
IceUtil::Mutex _mutex;
bool _stopFlag;
Decoder(BytesBufferPtr buffer)
:_pBuffer(buffer), _stopFlag(false)
{
_feedChunk._userData = this;
_feedChunk._callback = FeedCallBackFun;
_formatCtx = NULL;
_audioCodecCtx = NULL;
_audioFrame = NULL;
_audioStream = -1;
_swrContext = NULL;
_isEOF = false;
_interruptCallback = NULL;
//_interruptCallback = interruptCallback;
}
size_t feedCallBackFun(const ChunkInfoRef chunkinfo, bool terminated)
{
if (terminated) {
return 0;
}
if (_swrContext ) {
uint8_t *outbuf[2] = { (uint8_t*)chunkinfo->_data, 0 };
int numFrames = swr_convert(_swrContext,
outbuf,
(int) (_audioFrame->nb_samples),
(const uint8_t **)_audioFrame->data,
_audioFrame->nb_samples);
const int bufSize = av_samples_get_buffer_size(NULL,
_audioCodecCtx->channels,
numFrames,
AV_SAMPLE_FMT_S16,
1);
return bufSize;
} else {
memcpy(chunkinfo->_data, _audioFrame->data[0], chunkinfo->_size);
return chunkinfo->_size;
}
}
void onSDLAudioStop()
{
}
void openFile(const char* filepath)
{
_file = filepath;
start();
}
void closeFile()
{
IceUtil::Mutex::Lock lock(_mutex);
_stopFlag = true;
}
kxMovieError openAudioStream()
{
kxMovieError errCode = kxMovieErrorStreamNotFound;
_audioStream = -1;
for (size_t i = 0; i < _formatCtx->nb_streams; ++i ) {
if (AVMEDIA_TYPE_AUDIO == _formatCtx->streams[i]->codec->codec_type) {
errCode = openAudioStream(i);
if (errCode == kxMovieErrorNone)
break;
else {
if (kxMovieErrorNone != errCode) {
SP::printf("%s", errorMessage(errCode));
}
}
}
}
return errCode;
}
bool audioCodecIsSupported(AVCodecContext *audio)
{
return audio->sample_fmt == AV_SAMPLE_FMT_S16;
}
kxMovieError openAudioStream(size_t audioStream)
{
AVCodecContext *codecCtx = _formatCtx->streams[audioStream]->codec;
SwrContext *swrContext = NULL;
AVCodec *codec = avcodec_find_decoder(codecCtx->codec_id);
if(!codec)
return kxMovieErrorCodecNotFound;
if (avcodec_open2(codecCtx, codec, NULL) < 0)
return kxMovieErrorOpenCodec;
if (!audioCodecIsSupported(codecCtx))
{
swrContext = swr_alloc_set_opts(NULL,
av_get_default_channel_layout(codecCtx->channels),
AV_SAMPLE_FMT_S16,
codecCtx->sample_rate,
av_get_default_channel_layout(codecCtx->channels),
codecCtx->sample_fmt,
codecCtx->sample_rate,
0,
NULL);
if (!swrContext ||
swr_init(swrContext)) {
if (swrContext)
swr_free(&swrContext);
avcodec_close(codecCtx);
return kxMovieErroReSampler;
}
}
_audioFrame = av_frame_alloc();
if (!_audioFrame) {
if (swrContext)
swr_free(&swrContext);
avcodec_close(codecCtx);
return kxMovieErrorAllocateFrame;
}
_audioStream = audioStream;
_audioCodecCtx = codecCtx;
_swrContext = swrContext;
return kxMovieErrorNone;
}
void handleAudioFrame()
{
//SP::printf("handleAudioFrame \n");
if (!_audioFrame->data[0])
return ;
const int bufSize = av_samples_get_buffer_size(NULL,
_audioCodecCtx->channels,
(int)(_audioFrame->nb_samples),
AV_SAMPLE_FMT_S16,
1);
_pBuffer->feed(bufSize, &_feedChunk);
}
void decodeFrames(float)
{
if ( _audioStream == -1) {
return ;
}
bool finished = false;
while (!finished) {
if (av_read_frame(_formatCtx, &_packet) < 0) {
_isEOF = true;
break;
}
if (_packet.stream_index == _audioStream) {
int pktSize = _packet.size;
while (pktSize > 0) {
int gotframe = 0;
int len = avcodec_decode_audio4(_audioCodecCtx,
_audioFrame,
&gotframe,
&_packet);
if (len < 0) {
break;
}
if (gotframe) {
handleAudioFrame();
finished = true;
}
if (0 == len)
break;
pktSize -= len;
}
}
}
}
void closeAudioStream()
{
_audioStream = -1;
if (_swrContext) {
swr_free(&_swrContext);
_swrContext = NULL;
}
if (_audioFrame) {
av_free(_audioFrame);
_audioFrame = NULL;
}
if (_audioCodecCtx) {
avcodec_close(_audioCodecCtx);
_audioCodecCtx = NULL;
}
avformat_close_input(&_formatCtx);
//av_register_all();
}
kxMovieError openInput(const char* file)
{
AVFormatContext *formatCtx = NULL;
if (_interruptCallback) {
formatCtx = avformat_alloc_context();
if (!formatCtx)
return kxMovieErrorOpenFile;
AVIOInterruptCB cb = {_interruptCallback, this};
formatCtx->interrupt_callback = cb;
}
if (avformat_open_input(&formatCtx, file, NULL, NULL) < 0) {
if (formatCtx)
avformat_free_context(formatCtx);
return kxMovieErrorOpenFile;
}
if (avformat_find_stream_info(formatCtx, NULL) < 0) {
avformat_close_input(&formatCtx);
return kxMovieErrorStreamInfoNotFound;
}
av_dump_format(formatCtx, 0, file, false);
_formatCtx = formatCtx;
return kxMovieErrorNone;
}
static void init()
{
avformat_network_init();
av_register_all();
}
static void uninit()
{
avformat_network_deinit();
}
void run()
{
do
{
kxMovieError errCode = openInput(_file.c_str());
if (errCode == kxMovieErrorNone) {
errCode = openAudioStream();
}
if (errCode == kxMovieErrorNone) {
_signals.fileOpen(errCode == kxMovieErrorNone, _audioCodecCtx->channels, _audioCodecCtx->sample_rate, _signals._pusr);
} else {
_signals.fileOpen(errCode == kxMovieErrorNone, 0, 0, _signals._pusr);
break;
}
do {
decodeFrames(0.1);
IceUtil::Mutex::Lock lock(_mutex);
if (_stopFlag || _isEOF) {
break;
}
} while (true);
if (_isEOF) {
_signals.fileEOF(_signals._pusr);
//SP::printf("download finished!!!!!!! \n");
} else {
//SP::printf("download aborted !!!!!!! \n");
}
} while (false) ;
closeAudioStream();
}
};
typedef IceUtil::Handle< Decoder> DecoderPtr;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
size_t FeedCallBackFun(void* userData, const ChunkInfoRef chunkinfo, bool terminated)
{
Decoder* thiz = (Decoder*) userData;
return thiz->feedCallBackFun(chunkinfo, terminated);
}