-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path07_PlayingAudio - 1.cpp
More file actions
588 lines (487 loc) · 27.3 KB
/
Copy path07_PlayingAudio - 1.cpp
File metadata and controls
588 lines (487 loc) · 27.3 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//https://gist.github.com/Lovesan/ac2434a7e6aa1d5d0d0f6ee32af3d8ad original code
#define __STDC_CONSTANT_MACROS
#include <stdint.h>
#include <inttypes.h>
#include <windows.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavfilter/avfilter.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavutil/avstring.h>
#include <libswresample/swresample.h>
#define SDL_MAIN_HANDLED
#include <sdl2/SDL.h>
#include <sdl2/SDL_thread.h>
#include <sdl2/SDL_syswm.h>
#include <sdl2/SDL_render.h>
#include <sdl2/SDL_audio.h>
#ifdef __cplusplus
}
#endif
#define FF_ALLOC_EVENT (SDL_USEREVENT)
#define FF_REFRESH_EVENT (SDL_USEREVENT + 1)
#define FF_QUIT_EVENT (SDL_USEREVENT + 2)
#define MAX_QUEUE_SIZE (15 * 1024 * 1024) // 15728640
//#define VIDEO_PICTURE_QUEUE_SIZE (1)
#define SDL_AUDIO_BUFFER_SIZE 1024
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000
/** The output bit rate in kbit/s */
#define OUTPUT_BIT_RATE 48000
/** The number of output channels */
#define OUTPUT_CHANNELS 2
/** The audio sample output format */
#define OUTPUT_SAMPLE_FORMAT AV_SAMPLE_FMT_S16
typedef struct _PacketQueue
{
AVPacketList *first, *last;
int nb_packets, size;
} PacketQueue;
typedef struct _VideoState
{
AVFormatContext * pFormatCtx;
AVCodecContext * audioCtx;
struct SwrContext * pSwrCtx;
int audioStream;
AVStream * audioSt;
PacketQueue audioq;
__declspec(align(16)) uint8_t audioBuf[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2]; // bigger then parser could give
unsigned int audioBufSize, audioBufIndex;
AVPacket audioPkt;
AVFrame * pAudioFrame;
__declspec(align(16)) uint8_t audioConvertedData[(AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2];
//uint8_t * pFrameBuffer;
HANDLE hParseThread;
SDL_Renderer * renderer;
char filename[1024];
int quit;
} VideoState;
VideoState * global_video_state;
void PacketQueueInit(PacketQueue * pq)
{
memset(pq, 0, sizeof(PacketQueue));
}
int PacketQueuePut(PacketQueue * pq, const AVPacket * srcPkt)
{
AVPacketList *elt;
AVPacket pkt;
int rv;
if (!pq) return -1;
rv = av_packet_ref(&pkt, srcPkt);
if (rv) return rv;
elt = (AVPacketList*)av_malloc(sizeof(AVPacketList));
if (!elt) return -1;
elt->pkt = pkt;
elt->next = NULL;
if (!pq->last)
pq->first = elt;
else
pq->last->next = elt;
pq->last = elt;
pq->nb_packets++;
pq->size += elt->pkt.size;
return 0;
}
int PacketQueueGet(PacketQueue *pq, AVPacket *pkt)
{
AVPacketList * elt;
int rv;
if (!pq || !pkt) return -1;
for (;;)
{
if (global_video_state->quit)
{
rv = -1;
break;
}
elt = pq->first;
if (elt)
{
pq->first = elt->next;
if (!pq->first)
pq->last = NULL;
pq->nb_packets--;
pq->size -= elt->pkt.size;
*pkt = elt->pkt;
av_free(elt);
rv = 1;
break;
}
}
return rv;
}
//
//int DecodeAudioFrame2(VideoState * is)
//{
// int len2, dataSize = 0, outSize = 0, rv, hasPacket = 0;
// int64_t len1;
// uint8_t * converted = &is->audioConvertedData[0];
//
// for (;;)
// {
// while (is->hasAudioFrames) {
//
// rv = avcodec_receive_frame(is->audioCtx, is->pAudioFrame);
// if (rv)
// {
// is->hasAudioFrames = 0;
// break;
// }
//
// dataSize = av_samples_get_buffer_size(NULL,
// is->audioCtx->channels,
// is->pAudioFrame->nb_samples,
// is->audioCtx->sample_fmt,
// 1);
//
// outSize = av_samples_get_buffer_size(NULL,
// is->audioCtx->channels,
// is->pAudioFrame->nb_samples,
// AV_SAMPLE_FMT_FLT,
// 1);
//
// // returns the number of samples per channel in one audio frame -- 8192
// len2 = swr_convert(is->pSwrCtx,
// &converted,
// is->pAudioFrame->nb_samples,
// (const uint8_t**)&is->pAudioFrame->data[0],
// is->pAudioFrame->nb_samples);
// memcpy(is->audioBuf, converted, outSize);
// dataSize = outSize;
//
// /* We have data, return it and come back for more later */
// return dataSize;
// }
//
// if (hasPacket)
// {
// av_packet_unref(&is->audioPkt);
// }
//
// if (is->quit)
// return -1;
//
// if (PacketQueueGet(&is->audioq, &is->audioPkt) < 0)
// return -1;
//
// hasPacket = 1;
//
// rv = avcodec_send_packet(is->audioCtx, &is->audioPkt);
// if (rv) return rv;
//
// is->hasAudioFrames = 1;
// }
//
// return -1;
//}
int DecodeAudioFrame(VideoState * is)
{
int ret;
int len2, dataSize = 0, outSize = 0;
uint8_t * converted = &is->audioConvertedData[0];
while (true)
{
if (PacketQueueGet(&is->audioq, &is->audioPkt) < 0)
return -1;
ret = avcodec_send_packet(is->audioCtx, &is->audioPkt);
if (ret) return ret;
while (!ret)
{
ret = avcodec_receive_frame(is->audioCtx, is->pAudioFrame);
dataSize = av_samples_get_buffer_size(NULL,
is->audioCtx->channels,
is->pAudioFrame->nb_samples,
is->audioCtx->sample_fmt,
1);
outSize = av_samples_get_buffer_size(NULL,
is->audioCtx->channels,
is->pAudioFrame->nb_samples,
AV_SAMPLE_FMT_FLT,
1);
// returns the number of samples per channel in one audio frame -- 8192
len2 = swr_convert(is->pSwrCtx,
&converted, // output
is->pAudioFrame->nb_samples,
(const uint8_t**)&is->pAudioFrame->data[0], // input
is->pAudioFrame->nb_samples);
memcpy(is->audioBuf, converted, outSize);
// memcpy(is->audioBuf, (const uint8_t*)is->pAudioFrame->data[0], sizeof(is->pAudioFrame->data[0]));
// dataSize = outSize;
/* We have data, return it and come back for more later */
return outSize;
}
av_packet_unref(&is->audioPkt);
}
return -1;
}
void AudioCallback(void *userdata, uint8_t * stream, int len)
{
VideoState * is = (VideoState*)userdata;
int len1, audioSize, len3;
while (len > 0)
{
len3 = len;
if (is->audioBufIndex >= is->audioBufSize) // local buffer already empty
{
// already sent all data; get more
audioSize = DecodeAudioFrame(is);
if (audioSize < 0)
{
// error
is->audioBufSize = SDL_AUDIO_BUFFER_SIZE; // bufferı preset değeri kadar yap ve sıfırla
memset(is->audioBuf, 0, sizeof(is->audioBuf));
}
else
{
is->audioBufSize = audioSize; // buffer size ı decode'dan çıkan sampleların sizeı kadar yap
}
is->audioBufIndex = 0; // buffer indexi sıfırla
}
len1 = is->audioBufSize - is->audioBufIndex; // get buffer size that not yet sent to sdl
if (len1 > len) // if it is bigger then sdl wants make sure it is as much as sdl wants
len1 = len;
memcpy(stream, (uint8_t *)is->audioBuf + is->audioBufIndex, len1); // copy not sent data to sdl stream
len -= len1; // decrease sdl buffer size as much as you have sent
stream += len1; // forward sdl stream
is->audioBufIndex += len1; // forward buffer index
printf("BufSize:%d BufIndex:%d StreamLen:%d Len:%d\n", is->audioBufSize, is->audioBufIndex, len1, len3);
}
}
int StreamComponentOpen(VideoState * is, int streamIndex)
{
AVFormatContext * pFormatCtx = is->pFormatCtx;
AVCodecContext * codecCtx;
AVCodec * codec;
AVCodecParameters * codecPar;
SDL_AudioSpec wantedSpec = { 0 }, audioSpec = { 0 };
int rv, rgbFrameSize;
if (streamIndex < 0 || streamIndex >= pFormatCtx->nb_streams)
return -1;
codecPar = pFormatCtx->streams[streamIndex]->codecpar;
codec = avcodec_find_decoder(codecPar->codec_id);
if (!codec) return -1;
codecCtx = avcodec_alloc_context3(codec);
if (!codecCtx) return -1;
rv = avcodec_parameters_to_context(codecCtx, codecPar);
if (rv < 0)
{
avcodec_free_context(&codecCtx);
return rv;
}
rv = avcodec_open2(codecCtx, codec, NULL);
if (rv < 0)
{
avcodec_free_context(&codecCtx);
return rv;
}
if (codecPar->codec_type == AVMEDIA_TYPE_AUDIO)
{
is->audioCtx = codecCtx;
is->audioCtx->channels = OUTPUT_CHANNELS;
is->audioCtx->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
is->audioStream = streamIndex;
is->audioBufSize = 0;
is->audioBufIndex = 0;
is->audioSt = pFormatCtx->streams[streamIndex];
memset(&is->audioPkt, 0, sizeof(is->audioPkt));
is->pAudioFrame = av_frame_alloc();
if (!is->pAudioFrame) return -1;
is->pSwrCtx = swr_alloc();
if (!is->pSwrCtx) return -1;
//;
//Unlike libavcodec and libavformat, this structure is opaque. This means that if you would like to set options, you must use the AVOptions API and cannot directly set values to members of the structure.
av_opt_set_channel_layout(is->pSwrCtx, "in_channel_layout", codecCtx->channel_layout, 0); // mono or streo or something else
av_opt_set_channel_layout(is->pSwrCtx, "out_channel_layout", codecCtx->channel_layout, 0);
av_opt_set_int(is->pSwrCtx, "in_sample_rate", codecCtx->sample_rate, 0); // number of samples in one second
av_opt_set_int(is->pSwrCtx, "out_sample_rate", codecCtx->sample_rate, 0);
// av_opt_set_int(is->pSwrCtx, "out_sample_rate", 44100, 0);
av_opt_set_sample_fmt(is->pSwrCtx, "in_sample_fmt", codecCtx->sample_fmt, 0); // data structure of samples, data size // float planar
av_opt_set_sample_fmt(is->pSwrCtx, "out_sample_fmt", AV_SAMPLE_FMT_FLT, 0); // float
rv = swr_init(is->pSwrCtx);
if (rv < 0) return rv;
wantedSpec.channels = codecCtx->channels;
wantedSpec.freq = codecCtx->sample_rate;
wantedSpec.format = AUDIO_F32; // 32 bit floating
wantedSpec.silence = 0;
wantedSpec.samples = SDL_AUDIO_BUFFER_SIZE; //1024 samples of 4 bytes each * 2 channels = 8192
wantedSpec.userdata = is;
wantedSpec.callback = AudioCallback;
if (SDL_OpenAudio(&wantedSpec, &audioSpec) < 0) {
fprintf(stderr, "SDL_OpenAudio: %s\n", SDL_GetError());
return -1;
}
PacketQueueInit(&is->audioq);
SDL_PauseAudio(0);
}
else
{
avcodec_free_context(&codecCtx);
return -1;
}
return 0;
}
unsigned __stdcall DecodeThread(void* pUserData)
{
VideoState * is = (VideoState*)pUserData;
AVPacket pkt;
int rv;
for (;;)
{
if (is->quit) break;
if (is->audioq.size >= MAX_QUEUE_SIZE)
{
Sleep(10);
continue;
}
rv = av_read_frame(is->pFormatCtx, &pkt);
if (rv < 0) break;
if (pkt.stream_index == is->audioStream) // if it is audio stream packets, put packets to packet queue
{
PacketQueuePut(&is->audioq, &pkt);
}
av_packet_unref(&pkt);
}
while (!is->quit)
{
Sleep(100);
}
fail:
if (1)
{
SDL_Event evt;
evt.type = FF_QUIT_EVENT;
evt.user.data1 = is;
SDL_PushEvent(&evt);
}
return 0;
}
Uint32 TimerRefreshCallback(Uint32 interval, void* param)
{
SDL_Event evt;
evt.type = FF_REFRESH_EVENT;
evt.user.data1 = param;
SDL_PushEvent(&evt);
return 0;
}
void ScheduleRefresh(VideoState* is, int delay)
{
SDL_AddTimer(delay, TimerRefreshCallback, is);
}
int DecodeInterruptCallback(void* userData)
{
VideoState * is = (VideoState*)userData;
return is && is->quit;
}
int main()
{
int rv = 0, audioStream = -1;//, videoStream = -1;
unsigned int s;
char* filename = "D:\\Wildlife.wmv";
// char* filename = "c:\\aa.mp3";
// char* filename = "d:\\movie\\DeathWish2018.mkv";
char err[1024];
SDL_Event evt;
VideoState * is = NULL;
av_register_all();
avcodec_register_all();
avdevice_register_all();
avformat_network_init();
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
fprintf(stderr, "Unable to init SDL: %s \n", SDL_GetError());
goto cleanup;
}
is = (VideoState*)av_mallocz(sizeof(VideoState));
if (!is) goto cleanup;
global_video_state = is;
av_strlcpy(&is->filename[0], filename, sizeof(is->filename));
is->audioStream = -1;
is->pFormatCtx = avformat_alloc_context();
if (!is->pFormatCtx) goto cleanup;
is->pFormatCtx->interrupt_callback.callback = DecodeInterruptCallback;
is->pFormatCtx->interrupt_callback.opaque = is;
rv = avformat_open_input(&is->pFormatCtx, &is->filename[0], NULL, NULL);
if (rv < 0) goto cleanup;
rv = avformat_find_stream_info(is->pFormatCtx, NULL);
if (rv < 0) goto cleanup;
av_dump_format(is->pFormatCtx, 0, &is->filename[0], 0);
for (s = 0; s < is->pFormatCtx->nb_streams; ++s)
{
av_dump_format(is->pFormatCtx, s, &is->filename[0], FALSE);
if (is->pFormatCtx->streams[s]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audioStream < 0)
{
//if (is->pFormatCtx->streams[s]->codecpar->channels > 2) continue;
audioStream = s;
}
}
if (audioStream < 0)
{
rv = -1;
goto cleanup;
}
is->audioStream = audioStream;
if (audioStream >= 0)
{
rv = StreamComponentOpen(is, audioStream);
if (rv < 0) goto cleanup;
}
is->hParseThread = (HANDLE*)_beginthreadex(NULL, 0, DecodeThread, is, 0, NULL);
if (!is->hParseThread) goto cleanup;
ScheduleRefresh(is, 40);
for (;;)
{
SDL_WaitEvent(&evt);
switch (evt.type)
{
case FF_QUIT_EVENT:
case SDL_QUIT:
is->quit = 1;
SDL_Quit();
goto cleanup;
/*case FF_ALLOC_EVENT:
AllocPicture(evt.user.data1);
break;
case FF_REFRESH_EVENT:
VideoRefreshTimer(evt.user.data1);
break;*/
default:
break;
}
}
cleanup:
if (is->hParseThread)
{
WaitForSingleObject(is->hParseThread, INFINITE);
}
if (is->pAudioFrame)
{
av_frame_free(&is->pAudioFrame);
}
//if (is->pFrameRGB)
//{
// av_frame_free(&is->pFrameRGB);
//}
//if (is->pFrameBuffer)
//{
// av_free(is->pFrameBuffer);
//}
if (is->audioCtx)
{
avcodec_free_context(&is->audioCtx);
}
if (is->pSwrCtx)
{
swr_free(&is->pSwrCtx);
}
if (is->pFormatCtx)
{
avformat_close_input(&is->pFormatCtx);
}
if (is) av_free(is);
avformat_network_deinit();
return rv;
}