-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecodec.cpp
1210 lines (1123 loc) · 37.1 KB
/
decodec.cpp
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "decodec.h"
#include <chrono>
#include <cmath>
#include <sys/time.h>
#include <time.h>
extern "C"
{
#include "log.h"
}
#define MAX_AUDIO_FRAME_SIZE 96000 // 50 ms of 48khz 16-bit audio 2channel
/* Minimum SDL audio buffer size, in samples. */
#define SDL_AUDIO_MIN_BUFFER_SIZE 512
/* Calculate actual buffer size keeping in mind not cause too frequent audio
* callbacks */
#define SDL_AUDIO_MAX_CALLBACKS_PER_SEC 30
static void sdl_audio_callback(void *opaque, Uint8 *stream, int len);
#define AUDIOFRAME 8
#define VIDEOFRAME 8
// #define AV_NOSYNC_THRESHOLD 10.0
/* polls for possible required screen refresh at least this often, should be
* less than 1/fps */
#define REFRESH_RATE 20
/* no AV sync correction is done if below the minimum AV sync threshold */
#define AV_SYNC_THRESHOLD_MIN 40 // 40ms
/* AV sync correction is done if above the maximum AV sync threshold */
#define AV_SYNC_THRESHOLD_MAX 100 // 100ms
/* If a frame duration is longer than this, it will not be duplicated to
* compensate AV sync */
#define AV_SYNC_FRAMEDUP_THRESHOLD 40
/* no AV correction is done if too big error */
#define AV_NOSYNC_THRESHOLD 10000 // 10s
static FILE *file = fopen("log.txt", "wb+");
static void print_current_time_with_ms(std::string str);
Decodec::Decodec()
{
log_add_fp(file, LOG_TRACE);
dump_file_ = std::make_unique<FileDump>("decode_video.yuv");
audio_clock_ = std::make_unique<Clock>();
video_clock_ = std::make_unique<Clock>();
if (!(audio_frame_.frame = av_frame_alloc()))
{
log_error("Could not allocate audio audio_frame_\n");
}
// if (!(video_frame_ = av_frame_alloc())) {
// log_error("Could not allocate audio video_frame_\n");
// }
aduio_codec_info_ = (AVCodecParameters *)malloc(sizeof(AVCodecParameters));
video_codec_info_ = (AVCodecParameters *)malloc(sizeof(AVCodecParameters));
audio_decodec_ctx_ = nullptr;
video_decodec_ctx_ = nullptr;
audio_decodec_ = nullptr;
video_decodec_ = nullptr;
frame_timer = NAN;
mute_ = false;
volume_ = 100;
current_audio_clock_ = -1;
renderer_info_ = {0};
audio_callbacl_time_ = 0;
remaining_time_ = 0;
last_video_frame_.duration = NAN;
last_video_frame_.pts = NAN;
last_video_frame_.frame = nullptr;
frame_drops_late = 0;
player_state_ = PlayerState::PlayerState_Stop;
}
Decodec::~Decodec()
{
log_info("~Decodec in ");
log_info("SDL_Quit() IN audio_dev = %d",audio_dev);
//SDL_CloseAudioDevice(audio_dev);
log_info("SDL_Quit() OUT ");
audio_dev = 0;
log_info("~audio_decode_thread_ in ");
if (audio_decode_thread_->joinable())
{
audio_decode_thread_->join();
}
log_info("~audio_decode_thread_ out ");
log_info("~video_decode_thread_ in ");
if (video_decode_thread_->joinable())
{
video_decode_thread_->join();
}
log_info("~video_decode_thread_ out ");
log_info("~video_rendor_thread_ in ");
if (video_rendor_thread_->joinable())
{
video_rendor_thread_->join();
}
log_info("~video_rendor_thread_ out ");
free(aduio_codec_info_);
free(video_codec_info_);
// SDL_DestroyTexture(video_texture_);
// SDL_DestroyRenderer(renderer_);
// SDL_DestroyWindow(window_);
av_frame_free(&audio_frame_.frame);
// av_frame_free(&last_video_frame_);
// av_frame_free(&audio_frame_resample_);
fclose(file);
log_info("~Decodec out ");
}
void Decodec::set_player_state(PlayerState player_state)
{
log_debug("player state = %d\n", player_state);
player_state_ = player_state;
if (player_state_ == PlayerState::PlayerState_Play)
{
frame_timer += av_gettime_relative() / 1000 - video_clock_->last_updated_;
video_clock_->set_clock(video_clock_->get_clock(), 0);
set_mute(false);
}
else if (player_state_ == PlayerState::PlayerState_Pause)
{
set_mute(true);
} else if(player_state_ == PlayerState::PlayerState_Stop) {
log_info("SDL_CloseAudioDevice in ");
SDL_CloseAudioDevice(audio_dev);
log_info("SDL_CloseAudioDevice out");
}
video_clock_->paused_ = audio_clock_->paused_ =
video_clock_->pause_state_trans(player_state_);
}
PlayerState Decodec::get_player_state()
{
std::lock_guard<std::mutex> lck(mtx_);
return player_state_;
}
Ret Decodec::InitSDL()
{
sdl_flag_ = SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER;
if (SDL_Init(sdl_flag_) < 0)
{
log_error("Could not initialize SDL - %s\n", SDL_GetError());
log_error("(Did you set the DISPLAY variable?)\n");
return Ret_ERROR;
}
// 使用QML不需要SDL显示 video
// if (InitVideoSDL() != Ret_OK)
// {
// log_error("init sdl video failed");
// return Ret_ERROR;
// }
#ifdef USE_VIDEO_SDL
video_rendor_thread_ =
std::make_unique<std::thread>(&Decodec::VideoRendor, this);
#endif
// video_rendor_thread_ =
// std::make_unique<std::thread>(&Decodec::VideoRendor0, this);
// InitAuido要在InitVideo之后执行具体原因不清楚
if (InitAudioSDL() != Ret_OK)
{
log_error("init sdl audio failed");
return Ret_ERROR;
}
return Ret_OK;
}
Ret Decodec::InitAudioSDL()
{
// ToDo 目前写死
want_audio_spec_.freq = 48000;
// want_audio_spec_.format = AV_SAMPLE_FMT_S16; // ffmpeg定义
want_audio_spec_.format = AUDIO_S16LSB; // SDL 定义
want_audio_spec_.channels = 2;
want_audio_spec_.silence = 0;
// want_audio_spec_.samples = 1024;
want_audio_spec_.samples = FFMAX(
SDL_AUDIO_MIN_BUFFER_SIZE,
2 << av_log2(want_audio_spec_.freq / SDL_AUDIO_MAX_CALLBACKS_PER_SEC));
want_audio_spec_.callback = sdl_audio_callback;
want_audio_spec_.userdata = this;
int num = SDL_GetNumAudioDevices(0);
const char *deviceName = SDL_GetAudioDeviceName(num, 0);
if ((audio_dev = SDL_OpenAudioDevice(deviceName, 0, &want_audio_spec_, &spec_,
SDL_AUDIO_ALLOW_FREQUENCY_CHANGE)) < 2)
{
log_error("can't open audio.\n");
return Ret_ERROR;
}
SDL_PauseAudioDevice(audio_dev, 0);
bytes_per_sec = av_samples_get_buffer_size(
NULL, want_audio_spec_.channels, want_audio_spec_.freq,
TransforSDLFormattoFFmpeg(want_audio_spec_.format), 1);
log_debug("want_audio_spec_.samples = %d, spec.size = %d spec.samples = %d "
"bytes_per_sec = %d",
want_audio_spec_.samples, spec_.size, spec_.samples, bytes_per_sec);
return Ret_OK;
}
Ret Decodec::InitVideoSDL()
{
default_window_width_ = 640;
default_window_height_ = 480;
alwaysontop_ = false; // 始终置顶
borderless_ = false; // 无边框
window_ = nullptr;
#if SDL_VERSION_ATLEAST(2, 0, 5)
sdl_flag_ |= SDL_WINDOW_ALWAYS_ON_TOP;
#else
log_debug("Your SDL version doesn't support SDL_WINDOW_ALWAYS_ON_TOP.Feature "
"will be inactive");
#endif
if (borderless_)
sdl_flag_ |= SDL_WINDOW_BORDERLESS;
else
sdl_flag_ |= SDL_WINDOW_RESIZABLE;
window_ = SDL_CreateWindow("Big Fat Player", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, video_codec_info_->width,
video_codec_info_->height, SDL_WINDOW_SHOWN);
// 告诉 SDL 在渲染器缩放纹理时,使用线性插值方法进行过滤
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
if (window_)
{
// 创建renderer
renderer_ = SDL_CreateRenderer(
window_, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer_)
{
log_error("Failed to initialize a hardware accelerated renderer: %s\n",
SDL_GetError());
renderer_ = SDL_CreateRenderer(window_, -1, 0);
}
if (renderer_)
{
if (!SDL_GetRendererInfo(renderer_, &renderer_info_))
log_debug(NULL, AV_LOG_VERBOSE, "Initialized %s renderer.\n",
renderer_info_.name);
}
}
video_texture_ = SDL_CreateTexture(
renderer_, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,
video_codec_info_->width, video_codec_info_->height);
if (!window_ || !renderer_ || !renderer_info_.num_texture_formats ||
!video_texture_)
{
log_error("Failed to create window or renderer: %s", SDL_GetError());
return Ret_ERROR;
}
return Ret_OK;
}
#if 0
void Decodec::VideoRendor0()
{
int ret = 0;
if (InitVideoSDL() != Ret_OK)
{
log_error("init sdl video failed");
// return Ret_ERROR;
}
while (1)
{
Frame *video_frame = (Frame *)video_frame_queue_->Pop();
// dump_file_->WriteVideoYUV420PData(video_frame);
if (video_frame)
{
ret = SDL_UpdateYUVTexture(
video_texture_, nullptr, video_frame->frame->data[0],
video_frame->frame->linesize[0], video_frame->frame->data[1],
video_frame->frame->linesize[1], video_frame->frame->data[2],
video_frame->frame->linesize[2]);
// SDL_UpdateTexture(video_texture_, NULL, video_frame->data[0],
// video_frame->linesize[0]);
if (ret < 0)
{
log_error("Failed to SDL_UpdateYUVTexture: %s", SDL_GetError());
}
ret = SDL_RenderClear(renderer_);
if (ret < 0)
{
log_error("Failed to SDL_RenderClear %s", SDL_GetError());
}
ret = SDL_RenderCopy(renderer_, video_texture_, nullptr, nullptr);
if (ret < 0)
{
log_error("Failed to render texture: %s", SDL_GetError());
}
SDL_RenderPresent(renderer_);
SDL_Delay(40); // 控制帧率大约为 25fps (1000ms / 25 = 40ms)
SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
av_frame_free(&video_frame->frame);
}
av_frame_free(&video_frame->frame);
delete video_frame;
}
}
}
#endif
void Decodec::Start_Video_Rendor() {
video_rendor_thread_ =
std::make_unique<std::thread>(&Decodec::VideoRendor, this);
}
void Decodec::VideoRendor()
{
// 使用QML显示不需要SDL
#ifdef USE_VIDEO_SDL
if (InitVideoSDL() != Ret_OK)
{
log_error("init sdl video failed");
// return Ret_ERROR;
}
#endif
while (1)
{
#ifndef USE_VIDEO_SDL
if (player_state_ == PlayerState::PlayerState_Stop)
{
log_info("PlayerState_Stop exit VideoRendor");
return;
}
#endif
if (remaining_time_ > 0.0)
{
av_usleep((int64_t)(remaining_time_ *
1000.0)); // remaining_time <= REFRESH_RATE
}
// remaining_time_ = REFRESH_RATE;
// dump_file_->WriteVideoYUV420PData(video_frame);
if(video_refresh() == Ret_ERROR) {
return ;
}
}
}
Ret Decodec::video_refresh()
{
Ret ret = Ret_OK;
log_debug("\n\n-----------------------------------");
print_current_time_with_ms("video_refresh in ");
double last_duration, duration, delay;
Frame *video_frame = (Frame *)video_frame_queue_->Peek(); // 读取带显示的帧
if(video_frame->eof ==1) {
log_info("video_refresh receive null_Frame exit\n");
ret = Ret_ERROR;
return ret;
}
last_duration = vp_duration(&last_video_frame_, video_frame);
delay = compute_target_delay(last_duration);
log_debug("video_pts = %0.3f last_duration = %0.3f delay = %0.3f\n",
video_frame->pts, last_duration, delay);
double time = av_gettime_relative() / 1000.0;
if (std::isnan(frame_timer))
{
frame_timer = time;
}
log_debug("frame_timer + delay - time = %0.3f", frame_timer + delay - time);
if (time < frame_timer + delay && time != frame_timer)
{
// 判断是否继续显示上一帧
// 当前系统时刻还未到达上一帧的结束时刻,那么还应该继续显示上一帧。
// 计算出最小等待时间
remaining_time_ = FFMIN(frame_timer + delay - time, REFRESH_RATE);
log_debug("remaining_time_ = %0.3f", remaining_time_);
video_display(last_video_frame_.frame);
return ret;
}
remaining_time_ = 0.0;
// 走到这一步,说明已经到了或过了该显示的时间,待显示帧vp的状态变更为当前要显示的帧
frame_timer += delay; // 更新当前帧播放时间
log_debug("delay = %0.3f frame_timer = %0.3f ftime = %0.3f diff = %0.3f\n",
delay, frame_timer, time, time - frame_timer);
if (delay > 0 && time - frame_timer > AV_SYNC_THRESHOLD_MAX)
{
frame_timer = time; // 如果和系统时间差距太大,就纠正为系统时间
}
if (!std::isnan(video_frame->pts))
{
video_clock_->set_clock(video_frame->pts, 0);
}
// 丢帧逻辑
if (video_frame_queue_->Size() > 1)
{
Frame *Next_Frame = (Frame *)video_frame_queue_->PeekNext();
duration = vp_duration(video_frame, Next_Frame);
if (time > frame_timer + duration)
{
frame_drops_late++;
log_debug(" frame_timer + duration - time:%0.3f, duration = %0.3f drop "
"frame = %d\n",
(frame_timer + duration) - time, duration, frame_drops_late);
video_frame = (Frame *)video_frame_queue_->Pop();
if (last_video_frame_.frame != nullptr)
{
av_frame_free(
&last_video_frame_.frame); // 在clone新的数据之前需要释放掉之前的
}
last_video_frame_.frame = av_frame_clone(video_frame->frame);
last_video_frame_.pts = video_frame->pts;
last_video_frame_.duration = video_frame->duration;
av_frame_free(&video_frame->frame);
delete video_frame;
return ret ;
}
}
video_frame = (Frame *)video_frame_queue_->Pop();
video_display(video_frame->frame);
if (last_video_frame_.frame != nullptr)
{
av_frame_free(
&last_video_frame_.frame); // 在clone新的数据之前需要释放掉之前的
}
// 克隆当前帧数据
last_video_frame_.frame = av_frame_clone(video_frame->frame);
last_video_frame_.pts = video_frame->pts;
last_video_frame_.duration = video_frame->duration;
av_frame_free(&video_frame->frame);
delete video_frame;
print_current_time_with_ms("video_refresh out ");
}
double Decodec::compute_target_delay(double delay)
{
double sync_threshold, diff = 0;
double video_pts = video_clock_->get_clock();
double audio_pts = audio_clock_->get_clock();
log_debug("delay = %0.3f video_clock_->get_clock = %0.3f "
"audio_clock_->get_clock = %0.3f\n",
delay, video_pts, audio_pts);
diff = video_pts - audio_pts;
/* skip or repeat frame. We take into account the
delay to compute the threshold. I still don't know
if it is the best guess */
sync_threshold =
FFMAX(AV_SYNC_THRESHOLD_MIN, FFMIN(AV_SYNC_THRESHOLD_MAX, delay));
if (!std::isnan(diff))
{
if (diff < -sync_threshold) // 视频已经落后
{
delay = FFMAX(0, delay + diff); // 上一帧持续的时间往小的方向去调整
}
else if (diff >= sync_threshold && delay > AV_SYNC_FRAMEDUP_THRESHOLD)
{
// delay = 0.2秒
// diff = 1秒
// delay = 0.2 + 1 = 1.2
// 视频超前
// AV_SYNC_FRAMEDUP_THRESHOLD是0.1,此时如果delay>0.1,
// 如果2*delay时间就有点久
delay = delay + diff; // 上一帧持续时间往大的方向去调整
// log_debug( "video: delay=%0.3f A-V=%f\n",
// delay, -diff);
}
else if (diff >= sync_threshold)
{
// 上一帧持续时间往大的方向去调整
// delay = 0.2 *2 = 0.4
delay =
2 *
delay; // 保持在 2 * AV_SYNC_FRAMEDUP_THRESHOLD内, 即是2*0.1 = 0.2秒内
// delay = delay + diff; //
// 上一帧持续时间往大的方向去调整
}
else
{
// 音视频同步精度在 -sync_threshold ~ +sync_threshold
// 其他条件就是 delay = delay; 维持原来的delay,
// 依靠frame_timer+duration和当前时间进行对比
}
}
log_debug("video: delay=%0.3f A-V=%f\n", delay, -diff);
return delay;
}
double Decodec::vp_duration(Frame *vp, Frame *nextvp)
{
double duration = 0;
log_debug("nextvp->pts = %0.3f,vp->pts = %0.3f\n", nextvp->pts, vp->pts);
if (vp != nullptr && !std::isnan(vp->pts))
{
duration = nextvp->pts - vp->pts;
if (std::isnan(duration) || duration <= 0)
{
// log_debug("duration2 = %0.3f",vp->duration);
return vp->duration;
}
else
{
return duration;
}
}
else
{
return nextvp->duration; // 第一帧情况
}
}
void print_current_time_with_ms(std::string str)
{
struct timeval tv;
gettimeofday(&tv, NULL); // 获取当前时间
// 获取秒和微秒,并计算毫秒
time_t seconds = tv.tv_sec;
struct tm *timeinfo = localtime(&seconds);
int milliseconds = tv.tv_usec / 1000;
// 格式化时间并打印
log_debug("%s: \"%02d:%02d:%02d.%03d\",\n", str.c_str(), timeinfo->tm_hour,
timeinfo->tm_min, timeinfo->tm_sec, milliseconds);
}
void Decodec::video_display(AVFrame *video_frame)
{
print_current_time_with_ms("video_display");
#ifdef USE_VIDEO_SDL
if (video_frame)
{
int ret = 0;
ret = SDL_UpdateYUVTexture(video_texture_, nullptr, video_frame->data[0],
video_frame->linesize[0], video_frame->data[1],
video_frame->linesize[1], video_frame->data[2],
video_frame->linesize[2]);
// print_current_time_with_ms("SDL_UpdateYUVTexture");
if (ret < 0)
{
log_error("Failed to SDL_UpdateYUVTexture: %s", SDL_GetError());
}
ret = SDL_RenderClear(renderer_);
if (ret < 0)
{
log_error("Failed to SDL_RenderClear %s", SDL_GetError());
}
// print_current_time_with_ms("SDL_RenderClear");
ret = SDL_RenderCopy(renderer_, video_texture_, nullptr, nullptr);
if (ret < 0)
{
log_error("Failed to render texture: %s", SDL_GetError());
}
// print_current_time_with_ms("SDL_RenderCopy");
SDL_RenderPresent(renderer_);
// SDL_Delay(40); // 控制帧率大约为 25fps (1000ms / 25 = 40ms)
// print_current_time_with_ms("SDL_RenderPresent");
SDL_Event event;
SDL_PollEvent(&event);
if (event.type == SDL_QUIT)
{
}
// print_current_time_with_ms("SDL_PollEvent");
}
#else
write_yuv_data_cb_(video_frame);
#endif
}
void Decodec::set_audio_timebase(AVRational tb) { audio_tb_ = tb; }
void Decodec::set_video_timebase(AVRational tb) { video_tb_ = tb; }
void Decodec::set_video_frame_rate(AVRational frame_rate)
{
video_frame_rate_ = frame_rate;
}
Ret Decodec::Init()
{
Ret ret = Ret_OK;
audio_frame_queue_ = std::make_shared<PacketQueue>(AUDIOFRAME, "audioframe");
audio_ringbuf_ = std::make_shared<RingBuffer>(MAX_AUDIO_FRAME_SIZE);
video_frame_queue_ = std::make_shared<PacketQueue>(VIDEOFRAME, "vidoeframe");
ret = InitAudio();
ret = InitVideo();
if (InitSDL() != Ret_OK)
{
log_error("init sdl failed");
}
audio_resample_ = std::make_unique<AudioReSample>();
audio_resample_->Init(
aduio_codec_info_->sample_rate,
av_get_channel_layout_nb_channels(aduio_codec_info_->channel_layout),
(AVSampleFormat)aduio_codec_info_->format, want_audio_spec_.freq,
want_audio_spec_.channels,
TransforSDLFormattoFFmpeg((AVSampleFormat)want_audio_spec_.format));
return ret;
}
void Decodec::setAudioQueue(std::shared_ptr<PacketQueue> pkt_queue)
{
audio_pkt_queue_ = std::move(pkt_queue);
}
void Decodec::setVideoQueue(std::shared_ptr<PacketQueue> pkt_queue)
{
video_pkt_queue_ = std::move(pkt_queue);
}
Ret Decodec::setVolume(int volume) { return Ret_OK; }
void Decodec::StartDecode()
{
// DumpAudioCodecInfo();
video_decode_thread_ =
std::make_unique<std::thread>(&Decodec::VideoThread, this);
audio_decode_thread_ =
std::make_unique<std::thread>(&Decodec::AudioThread, this);
}
void Decodec::PushNullFrame() {
video_frame_queue_->Clear();
Frame *video_null_frame = new Frame();
video_null_frame->eof = 1;
video_null_frame->frame = av_frame_alloc();
video_frame_queue_->Push(video_null_frame);
audio_frame_queue_->Clear();
Frame *audio_null_frame = new Frame();
audio_null_frame->eof = 1;
audio_null_frame->frame = av_frame_alloc();
audio_frame_queue_->Push(audio_null_frame);
}
void Decodec::VideoThread()
{
PlayerState temp;
while (1)
{
if (player_state_ == PlayerState::PlayerState_Stop)
{
log_info("PlayerState_Stop exit VideoThread\n");
return;
}
if (get_player_state() == PlayerState::PlayerState_Pause)
{
// std::this_thread::sleep_for(std::chrono::milliseconds(20));
continue;
}
AVPacket *video_pkt = (AVPacket *)video_pkt_queue_->Pop();
if(video_pkt->data == nullptr) {
log_info("video Thread exit\n");
av_packet_free(&video_pkt);
//PushNullFrame();
return;
}
DecodeVideo(video_pkt);
av_packet_free(&video_pkt);
}
}
Ret Decodec::DecodeVideo(AVPacket *video_pkt)
{
int ret = avcodec_send_packet(video_decodec_ctx_, video_pkt);
if (ret < 0)
{
log_error("Failed to send packet to decoder\n");
return Ret_ERROR;
}
/* read all the output frames (in general there may be any number of them */
while (ret >= 0)
{
Frame *video_frame = new Frame();
video_frame->eof = 0;
if (!(video_frame->frame = av_frame_alloc()))
{
log_error("Could not allocate audio video_frame_\n");
}
ret = avcodec_receive_frame(video_decodec_ctx_, video_frame->frame);
if (video_frame)
{
if (video_frame->frame->pts != AV_NOPTS_VALUE)
{
video_frame->pts =
(double)video_frame->frame->pts * av_q2d(video_tb_) * 1000;
// 有的码流 没有pkt_duration
// video_frame->duration = (double)av_q2d(video_tb_) * 1000 *
// video_frame->frame->pkt_duration;
video_frame->duration =
av_q2d((AVRational){video_frame_rate_.den, video_frame_rate_.num}) *
1000;
// log_debug("duration3 = %0.3f",video_frame->duration);
double diff = video_frame->pts - audio_clock_->get_clock();
// if (!std::isnan(diff) && fabs(diff) < AV_NOSYNC_THRESHOLD)
// {
// av_frame_unref(video_frame->frame);
// delete video_frame;
// continue;
// }
}
}
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
av_frame_unref(video_frame->frame);
delete video_frame;
return Ret_ERROR;
}
else if (ret < 0)
{
log_error("Error during decoding\n");
av_frame_unref(video_frame->frame);
delete video_frame;
return Ret_ERROR;
}
video_frame_queue_->Push(video_frame);
//dump_file_->WriteVideoYUV420PData(video_frame->frame);
}
}
void Decodec::AudioThread()
{
// std::this_thread::sleep_for(std::chrono::microseconds(200));
while (1)
{
if (player_state_ == PlayerState::PlayerState_Stop)
{
log_info("PlayerState_Stop exit AudioThread");
return;
}
if (get_player_state() == PlayerState::PlayerState_Pause)
{
// std::this_thread::sleep_for(std::chrono::milliseconds(20));
continue;
}
AVPacket *audio_pkt = (AVPacket *)audio_pkt_queue_->Pop();
// dump_file_->WriteBitStream(audio_pkt,
// audio_pkt->size,audio_decodec_ctx_->codec_id);
if(audio_pkt->data == nullptr) {
log_info("AudioThread exit");
av_packet_free(&audio_pkt);
return;
}
DecodeAudio(audio_pkt);
av_packet_free(&audio_pkt);
}
}
Ret Decodec::DecodeAudio(AVPacket *audio_pkt)
{
int ret = avcodec_send_packet(audio_decodec_ctx_, audio_pkt);
if (ret < 0)
{
log_error("Failed to send packet to decoder\n");
return Ret_ERROR;
}
int data_size = 0;
int channel_index = 0;
/* read all the output frames (in general there may be any number of them */
while (ret >= 0)
{
// 解析出来的是32bit float planar 小端数据
ret = avcodec_receive_frame(audio_decodec_ctx_, audio_frame_.frame);
// if (ret >= 0) {
// AVRational tb = (AVRational){1, audio_frame_.frame->sample_rate}; //
// if (audio_frame_.frame->pts != AV_NOPTS_VALUE) {
// // 如果frame->pts正常则先将其从pkt_timebase转成{1,
// frame->sample_rate}
// // pkt_timebase实质就是stream->time_base
// audio_frame_.frame->pts = av_rescale_q(audio_frame_.frame->pts,
// video_tb_, tb);
// }
// }
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return Ret_ERROR;
else if (ret < 0)
{
log_error("Error during decoding\n");
return Ret_ERROR;
}
data_size = av_get_bytes_per_sample(audio_decodec_ctx_->sample_fmt);
if (data_size < 0)
{
/* This should not occur, checking just for paranoia */
log_error("Failed to calculate data size\n");
return Ret_ERROR;
}
// dump_file_->WritePcmPlanarData(audio_frame_.data, data_size);
// log_debug("audio_frame nb_samples = %d", audio_frame_.nb_samples);
Frame *audio_frame_resample_ = new Frame();
if (audio_frame_resample_ != nullptr)
{
if (!(audio_frame_resample_->frame = AllocOutFrame()))
{
log_error("Could not allocate audio audio_frame_resample_\n");
}
}
else
{
log_error("Could not allocate audio_frame_resample_");
return Ret_ERROR;
}
// // audio_frame_resample_是resample之后的数据,
int resample_sample_number = audio_resample_->audio_resampler_send_frame(
audio_frame_.frame, audio_frame_resample_->frame);
// dump_file_->WritePcmData(audio_frame_resample_->extended_data,
// resample_sample_number * 2 * 2);
// audio_ringbuf_->push(audio_frame_resample_->extended_data[0],
// resample_sample_number * 2 * 2);
// std::vector<uint8_t> temp = audio_ringbuf_->pop(resample_sample_number *
// 2 *1.5); //for test
// dump_file_->WritePcmData(temp.data(),temp.size());
log_info("audio_thread push in ");
audio_frame_queue_->Push(audio_frame_resample_);
log_info("audio_thread push out ");
}
}
int Decodec::get_audio_decode_frame()
{
Frame *temp_frame = (Frame *)audio_frame_queue_->Pop();
if(temp_frame->eof ==1) {
log_info("audio sdl receive null frame \n");
return 0;
}
// printf("temp_frame_pts = %lld temp_frame_nb_sample = %ld\n",
// temp_frame->pts, temp_frame->nb_samples);
int frame_size = av_samples_get_buffer_size(
NULL, temp_frame->frame->channels, temp_frame->frame->nb_samples,
(AVSampleFormat)temp_frame->frame->format, 1);
audio_ringbuf_->push(temp_frame->frame->extended_data[0], frame_size);
if (temp_frame)
{ // 注意要根据AVStream或者AVCodecContext中的time_base来计算pts
// 不能使用sample_rate来计算。因为有的audio
// pts不是根据sample_rate传输的
// audio_tb_ = (AVRational){
// 1000, temp_frame->frame->sample_rate}; /
temp_frame->pts = (double)temp_frame->frame->pts * av_q2d(audio_tb_) * 1000;
log_debug("temp_frame->frame->pts = %lld temp_frame_pts = %0.3f "
"temp_frame_nb_sample = %ld\n",
temp_frame->frame->pts, temp_frame->pts,
temp_frame->frame->nb_samples);
}
double current_frame_duration_time =
((double)temp_frame->frame->nb_samples /
audio_resample_->get_reframe_samplerate()) *
1000;
/* update the audio clock with the pts */
if (temp_frame->pts > 0)
{
// 当前frame的pts加上持续的播放时间
current_audio_clock_ = temp_frame->pts + current_frame_duration_time;
}
else
{
current_audio_clock_ = -1;
}
#ifdef DEBUG
{
static double last_clock = 0;
printf("audio: delay=%3f clock=%3f current_frame_duration_time = %3f\n",
current_audio_clock_ - last_clock, current_audio_clock_,
current_frame_duration_time);
last_clock = current_audio_clock_;
}
#endif
av_frame_free(&temp_frame->frame);
delete (temp_frame);
return frame_size;
}
AVFrame *Decodec::AllocOutFrame()
{
int ret;
AVFrame *frame = av_frame_alloc();
if (!frame)
{
return NULL;
}
// frame->nb_samples = want_audio_spec_.samples;
frame->nb_samples =
2048; // 临时设置因为不清楚resample之后的nb_sample,先设置一个较大的nb_sample
frame->channel_layout =
av_get_default_channel_layout(want_audio_spec_.channels);
frame->format = TransforSDLFormattoFFmpeg(want_audio_spec_.format);
frame->sample_rate = want_audio_spec_.freq;
ret = av_frame_get_buffer(frame, 0);
if (ret < 0)
{
log_error("cannot allocate audio data buffer\n");
return NULL;
}
return frame;
}
Ret Decodec::setAudioAvCodecInfo(AVCodecParameters *dec)
{
if (aduio_codec_info_ == nullptr && dec == nullptr)
{
return Ret_ERROR;
}
memcpy(aduio_codec_info_, dec, sizeof(AVCodecParameters));
return Ret_OK;
}
Ret Decodec::setVideoAvCodecInfo(AVCodecParameters *dec)
{
if (video_codec_info_ == nullptr && dec == nullptr)
{
return Ret_ERROR;
}
memcpy(video_codec_info_, dec, sizeof(AVCodecParameters));
return Ret_OK;
}
void Decodec::DumpAudioCodecInfo()
{
log_debug("\n----- Audio info:\n");
// sample_rate: 音频编解码器的采样率,单位为Hz
log_debug("samplerate:%dHz\n", aduio_codec_info_->sample_rate);
// channels: 音频通道数,单声道为1,双声道为2,5.1声道为6
log_debug("channels:%d\n", aduio_codec_info_->channels);
// channel_layout:
// 音频通道布局,用于描述音频通道的布局,例如立体声为2个通道,左声道为1,右声道为2
log_debug("channel_layout:%d\n", aduio_codec_info_->channel_layout);
// codec_type: 音频编解码器的类型,通常为AVMEDIA_TYPE_AUDIO
log_debug("codec_type:%d\n", aduio_codec_info_->codec_type);
}
void Decodec::DumpvideoCodecInfo()
{
log_debug("\n----- Video info:\n");
// index: 每个流成分在ffmpeg解复用分析后都有唯一的index作为标识
}
Ret Decodec::InitAudio()
{
/* find decoder for the stream */
audio_decodec_ = avcodec_find_decoder(aduio_codec_info_->codec_id);
if (!audio_decodec_)
{
log_error("Failed to find %s codec\n",
av_get_media_type_string(aduio_codec_info_->codec_type));
return Ret_ERROR;
}
/* Allocate a codec context for the decoder */
audio_decodec_ctx_ = avcodec_alloc_context3(audio_decodec_);
if (!audio_decodec_ctx_)
{
log_error("Failed to allocate the %s codec context\n",
av_get_media_type_string(aduio_codec_info_->codec_type));
return Ret_ERROR;
}
/* Copy codec parameters from input stream to output codec context */
if (avcodec_parameters_to_context(audio_decodec_ctx_, aduio_codec_info_) <
0)
{
log_error("Failed to copy %s codec parameters to decoder context\n",
av_get_media_type_string(aduio_codec_info_->codec_type));
return Ret_ERROR;
}
/* Init the decoders, with or without reference counting */
// av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0);
if (avcodec_open2(audio_decodec_ctx_, audio_decodec_, nullptr) < 0)
{
log_error("Failed to open %s codec\n",
av_get_media_type_string(aduio_codec_info_->codec_type));
return Ret_ERROR;
}
}
Ret Decodec::InitVideo()
{
/* find decoder for the stream */
video_decodec_ = avcodec_find_decoder(video_codec_info_->codec_id);
if (!video_decodec_)
{
log_error("Failed to find %s codec\n",
av_get_media_type_string(video_codec_info_->codec_type));
return Ret_ERROR;
}
/* Allocate a codec context for the decoder */
video_decodec_ctx_ = avcodec_alloc_context3(video_decodec_);
if (!video_decodec_ctx_)