-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMpegStreamer.cpp
2225 lines (2097 loc) · 77.4 KB
/
MpegStreamer.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 "stdafx.h"
#define NO_IMPORT_ARRAY
#define PY_ARRAY_UNIQUE_SYMBOL MPEGARRAY_API
#include <numpy/arrayobject.h>
#include "MpegCoder.h"
#include "MpegStreamer.h"
cmpc::CMpegClient::CMpegClient(void) :
videoPath(), width(0), height(0), widthDst(0), heightDst(0),
PPixelFormat(AVPixelFormat::AV_PIX_FMT_NONE), PFormatCtx(nullptr), PCodecCtx(nullptr),
PVideoStream(nullptr), frame(nullptr), PVideoStreamIDX(0), PVideoFrameCount(0),
buffer(), PswsCtx(nullptr), cache_size(0), read_size(0),
frameRate({ 0,0 }), read_handle(), read_check(), info_lock(), reading(false),
_str_codec(), _duration(0), _predictFrameNum(0), nthread(0), refcount(1) {
}
cmpc::CMpegClient::~CMpegClient(void) {
clear();
}
cmpc::CMpegClient::CMpegClient(CMpegClient&& ref) noexcept :
videoPath(std::move(ref.videoPath)), width(ref.width), height(ref.height),
widthDst(ref.widthDst), heightDst(ref.heightDst),
PPixelFormat(ref.PPixelFormat), PFormatCtx(ref.PFormatCtx), PCodecCtx(ref.PCodecCtx),
PVideoStream(ref.PVideoStream), frame(ref.frame),
PVideoStreamIDX(ref.PVideoStreamIDX), PVideoFrameCount(ref.PVideoFrameCount),
buffer(std::move(ref.buffer)), PswsCtx(ref.PswsCtx),
cache_size(ref.cache_size), read_size(ref.read_size),
frameRate(ref.frameRate), read_handle(std::move(std::thread())), read_check(), info_lock(),
reading(ref.reading), _str_codec(std::move(ref._str_codec)), _duration(ref._duration),
_predictFrameNum(ref._predictFrameNum), nthread(ref.nthread), refcount(ref.refcount) {
ref.PFormatCtx = nullptr;
ref.PCodecCtx = nullptr;
ref.PVideoStream = nullptr;
ref.frame = nullptr;
ref.PswsCtx = nullptr;
}
cmpc::CMpegClient& cmpc::CMpegClient::operator=(CMpegClient&& ref) noexcept {
if (this != &ref) {
videoPath = std::move(ref.videoPath);
width = ref.width;
height = ref.height;
widthDst = ref.widthDst;
heightDst = ref.heightDst;
PPixelFormat = ref.PPixelFormat;
PVideoStreamIDX = ref.PVideoStreamIDX;
PVideoFrameCount = ref.PVideoFrameCount;
cache_size = ref.cache_size;
read_size = ref.read_size;
frameRate = ref.frameRate;
reading = ref.reading;
_duration = ref._duration;
_predictFrameNum = ref._predictFrameNum;
refcount = ref.refcount;
PFormatCtx = ref.PFormatCtx;
PCodecCtx = ref.PCodecCtx;
PVideoStream = ref.PVideoStream;
frame = ref.frame;
PswsCtx = ref.PswsCtx;
buffer = std::move(ref.buffer);
read_handle = std::move(std::thread());
nthread = ref.nthread;
ref.PFormatCtx = nullptr;
ref.PCodecCtx = nullptr;
ref.PVideoStream = nullptr;
ref.frame = nullptr;
ref.PswsCtx = nullptr;
}
return *this;
}
void cmpc::CMpegClient::meta_protected_clear(void) {
auto protectWidth = widthDst;
auto protectHeight = heightDst;
auto protectCacheSize = cache_size;
auto protectReadSize = read_size;
auto protectFrameRate = frameRate;
auto protectNthread = nthread;
clear();
widthDst = protectWidth;
heightDst = protectHeight;
cache_size = protectCacheSize;
read_size = protectReadSize;
frameRate = protectFrameRate;
nthread = protectNthread;
}
void cmpc::CMpegClient::clear(void) {
if (read_handle.joinable()) {
read_check.lock();
reading = false;
read_check.unlock();
read_handle.join();
//std::terminate();
read_handle = std::move(std::thread());
}
else {
read_handle = std::move(std::thread());
}
width = height = 0;
widthDst = heightDst = 0;
PPixelFormat = AVPixelFormat::AV_PIX_FMT_NONE;
PVideoStreamIDX = -1;
PVideoFrameCount = 0;
_duration = 0;
_predictFrameNum = 0;
_str_codec.clear();
//videoPath.clear();
buffer.clear();
cache_size = 0;
read_size = 0;
frameRate = _setAVRational(0, 0);
read_check.lock();
read_check.unlock();
info_lock.lock();
info_lock.unlock();
nthread = 0;
PVideoStream = nullptr;
if (frame) {
av_frame_free(&frame);
frame = nullptr;
}
if (PswsCtx) {
sws_freeContext(PswsCtx);
PswsCtx = nullptr;
}
if (PCodecCtx) {
avcodec_free_context(&PCodecCtx);
PCodecCtx = nullptr;
}
if (PFormatCtx) {
avformat_close_input(&PFormatCtx);
PFormatCtx = nullptr;
}
refcount = 1;
}
int cmpc::CMpegClient::_open_codec_context(int& stream_idx, AVCodecContext*& dec_ctx, \
AVFormatContext* PFormatCtx, enum cmpc::AVMediaType type) { // Search the correct decoder, and make the configurations.
int ret;
//search video stream
ret = av_find_best_stream(PFormatCtx, type, -1, -1, nullptr, 0);
if (ret < 0) {
cerr << "Could not find " << av_get_media_type_string(type) << \
" stream in input address: '" << videoPath << "'" << endl;
return ret;
}
else {
auto stream_index = ret;
auto st = PFormatCtx->streams[stream_index]; // The AVStream object.
/* find decoder for the stream */
auto dec = avcodec_find_decoder(st->codecpar->codec_id); // Decoder (AVCodec).
if (!dec) {
cerr << "Failed to find " << av_get_media_type_string(type) << " codec" << endl;
return AVERROR(EINVAL);
}
_str_codec.assign(dec->name);
/* Allocate a codec context for the decoder / Add this to allocate the context by codec */
auto dec_ctx_ = avcodec_alloc_context3(dec); // Decoder context (AVCodecContext).
if (!dec_ctx_) {
cerr << "Failed to allocate the " << av_get_media_type_string(type) << " codec context" << endl;
return AVERROR(ENOMEM);
}
if (nthread > 0) {
dec_ctx_->thread_count = nthread;
}
/* Copy codec parameters from input stream to output codec context */
if ((ret = avcodec_parameters_to_context(dec_ctx_, st->codecpar)) < 0) {
cerr << "Failed to copy " << av_get_media_type_string(type) << \
" codec parameters to decoder context" << endl;
return ret;
}
/* Init the decoders, with or without reference counting */
AVDictionary* opts = nullptr; // The uninitialized argument dictionary.
av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0);
if ((ret = avcodec_open2(dec_ctx_, dec, &opts)) < 0) {
cerr << "Failed to open " << av_get_media_type_string(type) << " codec" << endl;
return ret;
}
dec_ctx = dec_ctx_;
stream_idx = stream_index;
}
return 0;
}
bool cmpc::CMpegClient::__setup_check() const {
if (cache_size > 0 && read_size > 0 && frameRate.den > 0 && frameRate.num > 0 && (!read_handle.joinable())) {
return true;
}
else {
return false;
}
}
bool cmpc::CMpegClient::FFmpegSetup(string inVideoPath) {
videoPath.assign(inVideoPath);
return FFmpegSetup();
}
bool cmpc::CMpegClient::FFmpegSetup() {
if (!__setup_check()) {
cerr << "Have not get necessary and correct configurations, so FFmpegSetup() should not be called." << endl;
return false;
}
meta_protected_clear();
/* open Stream: register all formats and codecs */
if (avformat_open_input(&PFormatCtx, videoPath.c_str(), nullptr, nullptr) < 0) {
cerr << "Could not open source address " << videoPath << endl;
clear();
return false;
} // For example, "rtsp://localhost:8554/h264.3gp"
/* retrieve stream information */
if (avformat_find_stream_info(PFormatCtx, nullptr) < 0) {
cerr << "Could not find stream information" << endl;
clear();
return false;
}
AVRational time_base, frame_base;
if (_open_codec_context(PVideoStreamIDX, PCodecCtx, PFormatCtx, AVMEDIA_TYPE_VIDEO) >= 0) {
PVideoStream = PFormatCtx->streams[PVideoStreamIDX];
time_base = PVideoStream->time_base;
frame_base = PVideoStream->avg_frame_rate;
/* allocate image where the decoded image will be put */
width = PCodecCtx->width;
height = PCodecCtx->height;
if (widthDst <= 0) {
widthDst = width;
}
if (heightDst <= 0) {
heightDst = height;
}
PPixelFormat = PCodecCtx->pix_fmt;
_duration = static_cast<double>(PVideoStream->duration) / static_cast<double>(time_base.den) * static_cast<double>(time_base.num);
_predictFrameNum = av_rescale(static_cast<int64_t>(_duration * 0xFFFF), frame_base.num, frame_base.den) / 0xFFFF;
}
else {
cerr << "Could not get codec context from the stream, aborting" << endl;
clear();
return false;
}
/* dump input information to stderr */
if (__dumpControl > 1) {
av_dump_format(PFormatCtx, 0, videoPath.c_str(), 0);
}
if (!PVideoStream) { // Check whether the video stream is correctly opened.
cerr << "Could not find audio or video stream in the network, aborting" << endl;
clear();
return false;
}
if (width == 0 || height == 0) {
cerr << "Could not get enough meta-data in the network, aborting" << endl;
clear();
return false;
}
PswsCtx = sws_getContext(width, height, PCodecCtx->pix_fmt, widthDst, heightDst, AV_PIX_FMT_RGB24,
SCALE_FLAGS, nullptr, nullptr, nullptr);
buffer.set(cache_size, width, height, widthDst, heightDst);
buffer.set_timer(frameRate, time_base);
if (!buffer.reset_memory()) { // Check whether the buffer is allocated correctly.
cerr << "Could not allocate the memory of frame buffer list." << endl;
clear();
return false;
}
read_check.lock();
reading = true;
read_check.unlock();
return true;
}
void cmpc::CMpegClient::dumpFormat() {
if ((!videoPath.empty()) && PFormatCtx) {
av_dump_format(PFormatCtx, 0, videoPath.c_str(), 0);
}
else {
cerr << "Still need to FFmpegSetup()" << endl;
}
}
void cmpc::CMpegClient::resetPath(string inVideoPath) {
videoPath.assign(inVideoPath);
}
cmpc::AVRational cmpc::CMpegClient::_setAVRational(int num, int den) {
AVRational res;
res.num = num; res.den = den;
return res;
}
int cmpc::CMpegClient::__save_frame(AVFrame*& frame, AVPacket*& pkt, bool& got_frame, int cached) {
int ret = 0;
int decoded = pkt->size;
got_frame = false;
if (pkt->stream_index == PVideoStreamIDX) {
/* decode video frame */
ret = __avcodec_decode_video2(PCodecCtx, frame, got_frame, pkt);
if (ret < 0) {
cout << "Error decoding video frame (" << av_err2str(ret) << ")" << endl;
return ret;
}
if (got_frame) {
if (frame->width != width || frame->height != height ||
frame->format != PPixelFormat) {
/* To handle this change, one could call av_image_alloc again and
* decode the following frames into another rawvideo file. */
cout << "Error: Width, height and pixel format have to be "
"constant in a rawvideo file, but the width, height or "
"pixel format of the input video changed:\n"
"old: width = " << width << ", height = " << height << ", format = "
<< av_get_pix_fmt_name(PPixelFormat) << endl <<
"new: width = " << frame->width << ", height = " << frame->height << ", format = "
<< av_get_pix_fmt_name(static_cast<AVPixelFormat>(frame->format)) << endl;
return -1;
}
info_lock.lock();
PVideoFrameCount++;
info_lock.unlock();
if (__dumpControl > 0) {
std::ostringstream str_data;
str_data << "video_frame" << (cached ? "(cached)" : "") << " n:" << PVideoFrameCount <<
" coded_n:" << frame->coded_picture_number << endl;
auto str_data_s = str_data.str();
av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str());
}
/* copy decoded frame to destination buffer:
* this is required since rawvideo expects non aligned data */
buffer.write(PswsCtx, frame);
}
}
/* If we use frame reference counting, we own the data and need
* to de-reference it when we don't use it anymore */
if (got_frame && refcount)
av_frame_unref(frame);
return decoded;
}
void cmpc::CMpegClient::__client_holder() {
int ret;
bool got_frame;
if (frame) {
cerr << "Current frame is occupied, could not start a new client." << endl;
return;
}
frame = av_frame_alloc();
auto pkt = av_packet_alloc();
if (!frame) {
cerr << "Could not allocate frame" << endl;
ret = AVERROR(ENOMEM);
return;
}
/* initialize packet, set data to NULL, let the demuxer fill it */
if (PVideoStream && (__dumpControl > 0)) {
std::ostringstream str_data;
str_data << "Demuxing video from address '" << videoPath << "' into Python-List" << endl;
auto str_data_s = str_data.str();
av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str());
}
/* Reset the contex to remove the flushed state. */
avcodec_flush_buffers(PCodecCtx);
/* read frames from the file */
info_lock.lock();
PVideoFrameCount = 0;
info_lock.unlock();
//start reading packets from stream and write them to file
av_read_play(PFormatCtx); //play RTSP
auto temp_pkt = av_packet_alloc();
while (av_read_frame(PFormatCtx, pkt) >= 0) {
//cout << "[Test - " << pkt.size << " ]" << endl;
av_packet_ref(temp_pkt, pkt);
do {
ret = __save_frame(frame, temp_pkt, got_frame, 0);
if (ret < 0)
break;
temp_pkt->data += ret;
temp_pkt->size -= ret;
} while (temp_pkt->size > 0);
/* flush cached frames */
av_packet_unref(pkt);
av_packet_unref(temp_pkt);
read_check.lock();
if (!reading) {
read_check.unlock();
break;
}
else {
read_check.unlock();
}
}
av_packet_free(&temp_pkt);
do {
__save_frame(frame, pkt, got_frame, 1);
} while (got_frame);
//cout << "Demuxing succeeded." << endl;
if (PVideoStream && (__dumpControl > 0)) {
std::ostringstream str_data;
str_data << "End of stream client." << endl;
auto str_data_s = str_data.str();
av_log(nullptr, AV_LOG_INFO, "%s", str_data_s.c_str());
}
if (frame) {
av_frame_free(&frame);
frame = nullptr;
}
if (pkt) {
av_packet_free(&pkt);
}
read_check.lock();
reading = false;
read_check.unlock();
}
int cmpc::CMpegClient::__avcodec_decode_video2(AVCodecContext* avctx, AVFrame* frame, bool& got_frame, AVPacket* pkt) {
int ret;
got_frame = false;
if (pkt) {
ret = avcodec_send_packet(avctx, pkt);
// In particular, we don't expect AVERROR(EAGAIN), because we read all
// decoded frames with avcodec_receive_frame() until done.
if (ret < 0) {
//cout << ret << ", " << AVERROR(EAGAIN) << ", " << AVERROR_EOF << endl;
return ret == AVERROR_EOF ? 0 : ret;
}
}
ret = avcodec_receive_frame(avctx, frame);
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
return ret;
if (ret >= 0)
got_frame = true;
//cout << ret << ", " << AVERROR(EAGAIN) << ", " << AVERROR_EOF << endl;
return 0;
}
PyObject* cmpc::CMpegClient::ExtractFrame() {
return ExtractFrame(read_size);
}
PyObject* cmpc::CMpegClient::ExtractFrame(int64_t readsize) {
if (readsize == 0 || readsize > cache_size) {
cerr << "Read size of frames is out of range." << endl;
return nullptr;
}
else if (frame == nullptr) {
cerr << "Current frame object is empty, maybe the client has not been started." << endl;
return nullptr;
}
buffer.freeze_write(readsize);
auto res = buffer.read();
if (res == nullptr) {
cerr << "Unable to get frames from current buffer." << endl;
}
return res;
}
void cmpc::CMpegClient::setParameter(string keyword, void* ptr) {
if (keyword.compare("widthDst") == 0) {
auto ref = reinterpret_cast<int*>(ptr);
widthDst = *ref;
}
else if (keyword.compare("heightDst") == 0) {
auto ref = reinterpret_cast<int*>(ptr);
heightDst = *ref;
}
else if (keyword.compare("cacheSize") == 0) {
auto ref = reinterpret_cast<int64_t*>(ptr);
cache_size = *ref;
}
else if (keyword.compare("readSize") == 0) {
auto ref = reinterpret_cast<int64_t*>(ptr);
read_size = *ref;
}
else if (keyword.compare("dstFrameRate") == 0) {
PyObject* ref = reinterpret_cast<PyObject*>(ptr);
auto refObj = PyTuple_GetItem(ref, 0);
int num = static_cast<int>(PyLong_AsLong(refObj));
refObj = PyTuple_GetItem(ref, 1);
int den = static_cast<int>(PyLong_AsLong(refObj));
frameRate = _setAVRational(num, den);
}
else if (keyword.compare("nthread") == 0) {
auto ref = reinterpret_cast<int*>(ptr);
if (PCodecCtx) {
PCodecCtx->thread_count = *ref;
}
nthread = *ref;
}
}
PyObject* cmpc::CMpegClient::getParameter(string keyword) {
if (keyword.compare("videoAddress") == 0) {
return PyUnicode_DecodeFSDefaultAndSize(videoPath.c_str(), static_cast<Py_ssize_t>(videoPath.size()));
}
else if (keyword.compare("width") == 0) {
return Py_BuildValue("i", width);
}
else if (keyword.compare("height") == 0) {
return Py_BuildValue("i", height);
}
else if (keyword.compare("frameCount") == 0) {
info_lock.lock();
auto value = Py_BuildValue("i", PVideoFrameCount);
info_lock.unlock();
return value;
}
else if (keyword.compare("coderName") == 0) {
return PyUnicode_DecodeFSDefaultAndSize(_str_codec.c_str(), static_cast<Py_ssize_t>(_str_codec.size()));
}
else if (keyword.compare("duration") == 0) {
return Py_BuildValue("d", _duration);
}
else if (keyword.compare("estFrameNum") == 0) {
return Py_BuildValue("L", _predictFrameNum);
}
else if (keyword.compare("srcFrameRate") == 0) {
if (!PVideoStream) {
return Py_BuildValue("d", 0.0);
}
auto frame_base = PVideoStream->avg_frame_rate;
double srcFrameRate = static_cast<double>(frame_base.num) / static_cast<double>(frame_base.den);
return Py_BuildValue("d", srcFrameRate);
}
else if (keyword.compare("nthread") == 0) {
if (PCodecCtx) {
return Py_BuildValue("i", PCodecCtx->thread_count);
}
else {
return Py_BuildValue("i", nthread);
}
}
else {
Py_RETURN_NONE;
}
}
PyObject* cmpc::CMpegClient::getParameter() {
auto res = PyDict_New();
string key;
PyObject* val = nullptr;
// Fill the values.
key.assign("videoAddress");
val = Py_BuildValue("y", videoPath.c_str());
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
key.assign("codecName");
val = Py_BuildValue("y", _str_codec.c_str());
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
if (PCodecCtx) {
key.assign("bitRate");
val = Py_BuildValue("L", PCodecCtx->bit_rate);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
key.assign("GOPSize");
val = Py_BuildValue("i", PCodecCtx->gop_size);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
key.assign("maxBframe");
val = Py_BuildValue("i", PCodecCtx->max_b_frames);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
key.assign("nthread");
val = Py_BuildValue("i", PCodecCtx->thread_count);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
}
else {
key.assign("nthread");
val = Py_BuildValue("i", nthread);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
}
if (widthDst > 0) {
key.assign("widthDst");
val = Py_BuildValue("i", widthDst);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
}
if (heightDst > 0) {
key.assign("heightDst");
val = Py_BuildValue("i", heightDst);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
}
key.assign("width");
val = Py_BuildValue("i", width);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
key.assign("height");
val = Py_BuildValue("i", height);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
if (PVideoStream) {
key.assign("frameRate");
auto& frame_rate = PVideoStream->avg_frame_rate;
val = Py_BuildValue("(ii)", frame_rate.num, frame_rate.den);
PyDict_SetItemString(res, key.c_str(), val);
Py_DECREF(val);
}
return res;
}
bool cmpc::CMpegClient::start() {
if (reading && (frame == nullptr)) {
read_handle = std::move(std::thread(std::mem_fn(&CMpegClient::__client_holder), std::ref(*this)));
return true;
}
return false;
}
void cmpc::CMpegClient::terminate() {
read_check.lock();
auto protectReading = reading;
read_check.unlock();
if (read_handle.joinable()) {
read_check.lock();
reading = false;
read_check.unlock();
read_handle.join();
//std::terminate();
read_handle = std::move(std::thread());
}
else {
read_handle = std::move(std::thread());
}
info_lock.lock();
info_lock.unlock();
read_check.lock();
reading = protectReading;
read_check.unlock();
if (frame) {
av_frame_free(&frame);
}
}
ostream& cmpc::operator<<(ostream& out, cmpc::CMpegClient& self_class) {
double dstFrameRate;
out << std::setw(1) << "/";
out << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl;
out << std::setw(1) << " * Packed FFmpeg Client - Y. Jin V" << MPEGCODER_CURRENT_VERSION << endl;
out << " " << std::setfill('*') << std::setw(44) << "" << std::setfill(' ') << endl;
out << std::setiosflags(std::ios::left) << std::setw(25) << " * VideoAddress: " \
<< self_class.videoPath << endl;
out << std::setiosflags(std::ios::left) << std::setw(25) << " * (Width, Height): " \
<< self_class.width << ", " << self_class.height << endl;
if (self_class.widthDst > 0 && self_class.heightDst > 0) {
out << std::setiosflags(std::ios::left) << std::setw(25) << " * (WidthDst, HeightDst): " \
<< self_class.widthDst << ", " << self_class.heightDst << endl;
}
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Deccoder: " \
<< self_class._str_codec << endl;
if (self_class.PCodecCtx) {
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number: " \
<< self_class.PCodecCtx->thread_count << endl;
}
else {
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Thread number (P): " \
<< self_class.nthread << endl;
}
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Duration: " \
<< self_class._duration << " [s]" << endl;
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Predicted FrameNum: " \
<< self_class._predictFrameNum << endl;
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Read/Cache size: " \
<< self_class.read_size << "/" << self_class.cache_size << endl;
if (self_class.PVideoStream) {
auto frame_base = self_class.PVideoStream->avg_frame_rate;
double srcFrameRate = static_cast<double>(frame_base.num) / static_cast<double>(frame_base.den);
if (self_class.frameRate.den) {
dstFrameRate = static_cast<double>(self_class.frameRate.num) / static_cast<double>(self_class.frameRate.den);
}
else {
dstFrameRate = 0;
}
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Dst./Src. frame rate: " \
<< std::setprecision(3) << dstFrameRate << "/" << srcFrameRate << std::setprecision(6) << endl;
}
else {
if (self_class.frameRate.den) {
dstFrameRate = static_cast<double>(self_class.frameRate.num) / static_cast<double>(self_class.frameRate.den);
}
else {
dstFrameRate = 0;
}
out << std::setiosflags(std::ios::left) << std::setw(25) << " * Src. frame rate: " \
<< std::setprecision(3) << dstFrameRate << std::setprecision(6) << endl;
}
out << std::setw(1) << " */";
return out;
}
cmpc::BufferList::BufferList(void) :
_Buffer_pos(0), _Buffer_rpos(-1), _Buffer_size(0), __Read_size(0),
next_pts(0), interval_pts(0), dst_width(0), dst_height(0),
src_width(0), src_height(0), _Buffer_capacity(0),
frameRGB(nullptr), _Buffer_List(nullptr) {
}
cmpc::BufferList::~BufferList(void) {
if (_Buffer_List) {
for (auto i = 0; i < _Buffer_size; i++) {
if (_Buffer_List[i]) {
av_free(_Buffer_List[i]);
_Buffer_List[i] = nullptr;
}
}
delete[]_Buffer_List;
_Buffer_List = nullptr;
}
if (frameRGB) {
av_frame_free(&frameRGB);
}
}
cmpc::BufferList::BufferList(const BufferList& ref) :
_Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size),
__Read_size(ref.__Read_size), next_pts(ref.next_pts), interval_pts(ref.interval_pts),
dst_width(ref.dst_width), dst_height(ref.dst_height),
src_width(ref.src_width), src_height(ref.src_height),
_Buffer_capacity(ref._Buffer_capacity), frameRGB(ref.frameRGB), _Buffer_List(nullptr) {
if (!(frameRGB = av_frame_alloc())) {
cerr << "Could Allocate Temp Frame (RGB)" << endl;
return;
}
_Buffer_List = new uint8_t * [_Buffer_size];
memset(_Buffer_List, 0, _Buffer_size * sizeof(uint8_t*));
if (_Buffer_capacity > 0) {
for (auto i = 0; i < _Buffer_size; i++) {
if (ref._Buffer_List[i] != nullptr) {
_Buffer_List[i] = (uint8_t*)av_malloc(_Buffer_capacity * sizeof(uint8_t));
memcpy(_Buffer_List[i], ref._Buffer_List[i], _Buffer_capacity * sizeof(uint8_t));
}
}
}
}
cmpc::BufferList& cmpc::BufferList::operator=(const BufferList& ref) {
if (this != &ref) {
_Buffer_pos = ref._Buffer_pos;
_Buffer_rpos = ref._Buffer_rpos;
_Buffer_size = ref._Buffer_size;
__Read_size = ref.__Read_size;
next_pts = ref.next_pts;
interval_pts = ref.interval_pts;
dst_width = ref.dst_width;
dst_height = ref.dst_height;
src_width = ref.src_width;
src_height = ref.src_height;
_Buffer_capacity = ref._Buffer_capacity;
if (!(frameRGB = av_frame_alloc())) {
cerr << "Could Allocate Temp Frame (RGB)" << endl;
return *this;
}
_Buffer_List = new uint8_t * [_Buffer_size];
memset(_Buffer_List, 0, _Buffer_size * sizeof(uint8_t*));
if (_Buffer_capacity > 0) {
for (auto i = 0; i < _Buffer_size; i++) {
if (ref._Buffer_List[i] != nullptr) {
_Buffer_List[i] = (uint8_t*)av_malloc(_Buffer_capacity * sizeof(uint8_t));
memcpy(_Buffer_List[i], ref._Buffer_List[i], _Buffer_capacity * sizeof(uint8_t));
}
}
}
}
return *this;
}
cmpc::BufferList::BufferList(BufferList&& ref) noexcept :
_Buffer_pos(ref._Buffer_pos), _Buffer_rpos(ref._Buffer_rpos), _Buffer_size(ref._Buffer_size),
__Read_size(ref.__Read_size), next_pts(ref.next_pts), interval_pts(ref.interval_pts),
dst_width(ref.dst_width), dst_height(ref.dst_height),
src_width(ref.src_width), src_height(ref.src_height),
_Buffer_capacity(ref._Buffer_capacity), frameRGB(ref.frameRGB), _Buffer_List(ref._Buffer_List) {
ref._Buffer_List = nullptr;
ref.frameRGB = nullptr;
}
cmpc::BufferList& cmpc::BufferList::operator=(BufferList&& ref) noexcept {
if (this != &ref) {
_Buffer_pos = ref._Buffer_pos;
_Buffer_rpos = ref._Buffer_rpos;
_Buffer_size = ref._Buffer_size;
__Read_size = ref.__Read_size;
interval_pts = ref.interval_pts;
next_pts = ref.next_pts;
dst_width = ref.dst_width;
dst_height = ref.dst_height;
src_width = ref.src_width;
src_height = ref.src_height;
_Buffer_capacity = ref._Buffer_capacity;
_Buffer_List = ref._Buffer_List;
frameRGB = ref.frameRGB;
ref._Buffer_List = nullptr;
ref.frameRGB = nullptr;
}
return *this;
}
void cmpc::BufferList::clear(void) {
if (_Buffer_List) {
for (auto i = 0; i < _Buffer_size; i++) {
if (_Buffer_List[i]) {
av_free(_Buffer_List[i]);
_Buffer_List[i] = nullptr;
}
}
delete[]_Buffer_List;
_Buffer_List = nullptr;
}
_Buffer_pos = 0;
_Buffer_rpos = -1;
_Buffer_size = 0;
__Read_size = 0;
next_pts = 0;
interval_pts = 0;
src_width = 0;
src_height = 0;
dst_width = 0;
dst_height = 0;
if (frameRGB) {
av_frame_free(&frameRGB);
}
}
const int64_t cmpc::BufferList::size() const {
return _Buffer_size;
}
void cmpc::BufferList::set(int64_t set_size, int width, int height, int widthDst, int heightDst) {
_Buffer_size = set_size;
if (widthDst != 0) {
dst_width = widthDst;
}
else {
dst_width = width;
}
if (heightDst != 0) {
dst_height = heightDst;
}
else {
dst_height = height;
}
src_width = width;
src_height = height;
_Buffer_capacity = av_image_get_buffer_size(AV_PIX_FMT_RGB24, dst_width, dst_height, 1);
}
void cmpc::BufferList::set_timer(AVRational targetFrameRate, AVRational timeBase) {
interval_pts = av_rescale(av_rescale(1, timeBase.den, timeBase.num), targetFrameRate.den, targetFrameRate.num);
}
bool cmpc::BufferList::reset_memory() {
if (!frameRGB) {
if (!(frameRGB = av_frame_alloc())) {
cerr << "Could Allocate Temp Frame (RGB)" << endl;
return false;
}
}
if (!_Buffer_List) {
_Buffer_List = new uint8_t * [_Buffer_size];
memset(_Buffer_List, 0, _Buffer_size * sizeof(uint8_t*));
}
for (auto i = 0; i < _Buffer_size; i++) {
if (!_Buffer_List[i]) {
_Buffer_List[i] = (uint8_t*)av_malloc(_Buffer_capacity * sizeof(uint8_t));
}
memset(_Buffer_List[i], 0, _Buffer_capacity * sizeof(uint8_t));
}
return true;
}
void cmpc::BufferList::freeze_write(int64_t read_size) {
auto read_pos = _Buffer_pos - read_size;
if (read_pos < 0) {
read_pos += _Buffer_size;
}
_Buffer_rpos = read_pos;
__Read_size = read_size;
}
bool cmpc::BufferList::write(SwsContext* PswsCtx, AVFrame* frame) {
if (frame->pts < next_pts) {
if (frame->pts > (next_pts - 2 * interval_pts)) {
return false;
}
else {
next_pts = frame->pts + interval_pts;
}
}
else {
if (next_pts > 0)
next_pts += interval_pts;
else
next_pts = frame->pts;
}
if (_Buffer_pos == _Buffer_rpos) {
return false;
}
av_image_fill_arrays(frameRGB->data, frameRGB->linesize, _Buffer_List[_Buffer_pos], AV_PIX_FMT_RGB24, dst_width, dst_height, 1);
sws_scale(PswsCtx, frame->data, frame->linesize, 0, src_height, frameRGB->data, frameRGB->linesize);
_Buffer_pos++;
if (_Buffer_pos >= _Buffer_size)
_Buffer_pos -= _Buffer_size;
return true;
}
PyObject* cmpc::BufferList::read() {
if (_Buffer_rpos < 0) {
return nullptr;
}
auto _Buffer_rend = (_Buffer_rpos + __Read_size) % _Buffer_size;
npy_intp dims[] = { __Read_size, dst_height, dst_width, 3 };
auto newdata = new uint8_t[__Read_size * _Buffer_capacity];
auto p = newdata;
for (auto i = _Buffer_rpos; i != _Buffer_rend; i = (i + 1) % _Buffer_size) {
memcpy(p, _Buffer_List[i], _Buffer_capacity * sizeof(uint8_t));
p += _Buffer_capacity;
}
PyObject* PyFrame = PyArray_SimpleNewFromData(4, dims, NPY_UINT8, reinterpret_cast<void*>(newdata));
PyArray_ENABLEFLAGS((PyArrayObject*)PyFrame, NPY_ARRAY_OWNDATA);
_Buffer_rpos = -1;
__Read_size = 0;
return PyArray_Return((PyArrayObject*)PyFrame);
//Py_RETURN_NONE;
}
/**
* Related with the encoder.
*/
// Constructors following 3-5 law.
cmpc::CMpegServer::CMpegServer(void) :
videoPath(), __formatName(), codecName(), bitRate(1024),
__start_time(0), __cur_time(0), width(100), height(100), widthSrc(0), heightSrc(0),
timeBase(_setAVRational(1, 25)), frameRate(_setAVRational(25, 1)),
time_base_q(_setAVRational(1, AV_TIME_BASE)), GOPSize(10), MaxBFrame(1),
PStreamContex({ 0 }), PFormatCtx(nullptr), Ppacket(nullptr), PswsCtx(nullptr),
__frameRGB(nullptr), RGBbuffer(nullptr), __have_video(false), __enable_header(false),
nthread(0) {
__pts_ahead = av_rescale(av_rescale(20, timeBase.den, timeBase.num), frameRate.den, frameRate.num);
}
void cmpc::CMpegServer::meta_protected_clear(void) {
auto protectWidth = width;
auto protectHeight = height;
auto protectWidthSrc = widthSrc;
auto protectHeightSrc = heightSrc;
auto protectBitRate = bitRate;
auto protectGOPSize = GOPSize;
auto protectMaxBFrame = MaxBFrame;
auto protectPTSAhead = __pts_ahead;
auto protectVideoPath(videoPath);
auto protectFormatName(__formatName);
auto protectCodecName(codecName);
auto protectTimeBase(timeBase);
auto protectFrameRate(frameRate);
auto protectNthread = nthread;
clear();
width = protectWidth;
height = protectHeight;
widthSrc = protectWidthSrc;
heightSrc = protectHeightSrc;
bitRate = protectBitRate;
GOPSize = protectGOPSize;
MaxBFrame = protectMaxBFrame;
timeBase = protectTimeBase;
frameRate = protectFrameRate;
__pts_ahead = protectPTSAhead;
videoPath.assign(protectVideoPath);
__formatName.assign(protectFormatName);
codecName.assign(protectCodecName);
nthread = protectNthread;
}
void cmpc::CMpegServer::clear(void) {
FFmpegClose();
videoPath.clear();
__formatName.clear();
codecName.clear();
bitRate = 1024;