-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMpegPyd.h
1675 lines (1586 loc) · 72.2 KB
/
MpegPyd.h
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
#ifndef MPEGPYD_H_INCLUDED
#define MPEGPYD_H_INCLUDED
#define PY_ARRAY_UNIQUE_SYMBOL MPEGARRAY_API
#include <cstdint>
#include <iostream>
#include <Python.h>
#include <numpy/arrayobject.h>
#include <structmember.h>
#include <windows.h>
#include <string>
#include <sstream>
#include "MpegCoder.h"
#include "MpegStreamer.h"
using std::string;
using std::ostringstream;
PyObject *str2PyStr(string Str) { // Convert the output string to the widechar unicode string.
int wlen = MultiByteToWideChar(CP_ACP, NULL, Str.c_str(), int(Str.size()), NULL, 0);
wchar_t* wszString = new wchar_t[static_cast<size_t>(wlen) + 1];
MultiByteToWideChar(CP_ACP, NULL, Str.c_str(), int(Str.size()), wszString, wlen);
wszString[wlen] = 0;
PyObject* res = PyUnicode_FromWideChar(wszString, wlen);
delete[] wszString;
return res;
}
bool PyStr2str(PyObject* py_str, string& s_str) { // Convert a python str to std::string.
if (!py_str) {
return false;
}
if (PyUnicode_Check(py_str)) {
auto py_bytes = PyUnicode_EncodeFSDefault(py_str);
if (!py_bytes) {
PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to encode the unicode str.'");
return false;
}
auto c_str = PyBytes_AsString(py_bytes);
if (!c_str) {
PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to parse data from the encoded str.'");
return false;
}
s_str.assign(c_str);
Py_DECREF(py_bytes);
}
else {
if (PyBytes_Check(py_str)) {
auto c_str = PyBytes_AsString(py_str);
if (!c_str) {
PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to parse data from the bytes object.'");
return false;
}
s_str.assign(c_str);
}
else {
PyErr_SetString(PyExc_TypeError, "Error.PyStr2str: fail to convert the object to string, maybe the object is not str or bytes.'");
return false;
}
}
return true;
}
/*****************************************************************************
* C style definition of Python classes.
* Each class would ref the C implemented class directly.
* No extra python data member is added to these classes,
* because the data members have been already packed as private members of the
* C classes.
*****************************************************************************/
typedef struct _C_MpegDecoder
{
PyObject_HEAD // == PyObject ob_base; Define the PyObject header.
cmpc::CMpegDecoder* _in_Handle; // Define the implementation of the C Object.
} C_MpegDecoder;
typedef struct _C_MpegEncoder
{
PyObject_HEAD // == PyObject ob_base; Define the PyObject header.
cmpc::CMpegEncoder* _in_Handle; // Define the implementation of the C Object.
} C_MpegEncoder;
typedef struct _C_MpegClient
{
PyObject_HEAD // == PyObject ob_base; Define the PyObject header.
cmpc::CMpegClient* _in_Handle; // Define the implementation of the C Object.
} C_MpegClient;
typedef struct _C_MpegServer
{
PyObject_HEAD // == PyObject ob_base; Define the PyObject header.
cmpc::CMpegServer* _in_Handle; // Define the implementation of the C Object.
} C_MpegServer;
static PyMemberDef C_MPDC_DataMembers[] = // Register the members of the python class.
{ // Do not register any data, because all data of this class is private.
//{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."},
{ "hAddress", T_ULONGLONG, offsetof(C_MpegDecoder, _in_Handle), READONLY, "The address of the handle in memory." },
{ nullptr, 0, 0, 0, nullptr }
};
static PyMemberDef C_MPEC_DataMembers[] = // Register the members of the python class.
{ // Do not register any data, because all data of this class is private.
//{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."},
{ "hAddress", T_ULONGLONG, offsetof(C_MpegEncoder, _in_Handle), READONLY, "The address of the handle in memory." },
{ nullptr, 0, 0, 0, nullptr }
};
static PyMemberDef C_MPCT_DataMembers[] = // Register the members of the python class.
{ // Do not register any data, because all data of this class is private.
//{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."},
{ "hAddress", T_ULONGLONG, offsetof(C_MpegClient, _in_Handle), READONLY, "The address of the handle in memory." },
{ nullptr, 0, 0, 0, nullptr }
};
static PyMemberDef C_MPSV_DataMembers[] = // Register the members of the python class.
{ // Do not register any data, because all data of this class is private.
//{"m_dEnglish", T_FLOAT, offsetof(CScore, m_dEnglish), 0, "The English score of instance."},
{ "hAddress", T_ULONGLONG, offsetof(C_MpegServer, _in_Handle), READONLY, "The address of the handle in memory." },
{ nullptr, 0, 0, 0, nullptr }
};
/*****************************************************************************
* Delearaction of all methods and functions.
* Prepare the function objects for the registeration of the classes and
* functions.
*****************************************************************************/
/*static void Example(ClassName* Self, PyObject* pArgs);
PyMODINIT_FUNC PyFunc_Example(void);*/
static PyObject* C_MPC_Global(PyObject* Self, PyObject* args, PyObject* kwargs) {
char dumpLevel = -1;
cmpc::CharList kwlist_str({ "dumpLevel" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|B", kwlist, &dumpLevel)) {
PyErr_SetString(PyExc_TypeError, "Error.GlobalSettings: invalid keyword'");
return nullptr;
}
if (dumpLevel != -1) {
cmpc::__dumpControl = static_cast<int8_t>(dumpLevel);
switch (dumpLevel) {
case 0:
cmpc::av_log_set_level(AV_LOG_ERROR);
break;
case 1:
cmpc::av_log_set_level(AV_LOG_INFO);
break;
case 2:
default:
cmpc::av_log_set_level(AV_LOG_DEBUG);
break;
}
}
Py_RETURN_NONE;
}
static PyObject* C_MPC_Help(PyObject* Self) {
cout << R"(================================================================================
__ _ _ _ _ ,___
( / / / o ( / ) ) / / /
(__/ , , _, /_ _ _ _' ( / / / ,_ _ _, / __ __/ _ _
_/_(_/_(__/ /_(/_/ / /_/_)_ / / (__/|_)_(/_(_)_(___/(_)(_/_(/_/ (_
// /| /|
(/ (/ (/
================================================================================
Yuchen's Mpeg Coder - Readme
This is a mpegcoder adapted from FFmpeg & Python-c-api.Using it you could
get access to processing video easily. Just use it as a common module in
python like this.
>>> import mpegCoder
Noted that this API need you to install numpy.
An example of decoding a video in an arbitrary format:
>>> d = mpegCoder.MpegDecoder()
>>> d.FFmpegSetup(b'inputVideo.mp4')
>>> p = d.ExtractGOP(10) # Get a gop of current video by setting the
start position of 10th frame.
>>> p = d.ExtractGOP() # Get a gop of current video, using the current
position after the last ExtractGOP.
>>> d.ExtractFrame(100, 100) # Extract 100 frames from the begining of
100th frame.
An example of transfer the coding of a video with an assigned codec:
>>> d = mpegCoder.MpegDecoder()
>>> d.FFmpegSetup(b'i.avi')
>>> e = mpegCoder.MpegEncoder()
>>> e.setParameter(decoder=d, codecName=b'libx264', videoPath=b'o.mp4')
# inherit most of parameters from the decoder.
>>> opened = e.FFmpegSetup() # Load the encoder.
>>> if opened: # If encoder is not loaded successfully, do not continue.
... p = True
... while p:
... p = d.ExtractGOP() # Extract current GOP.
... if p is not None:
... for i in p: # Select every frame.
... e.EncodeFrame(i) # Encode current frame.
... e.FFmpegClose() # End encoding, and flush all frames in cache.
>>> d.clear() # Close the input video.
An example of demuxing the video streamer from a server:
>>> d = mpegCoder.MpegClient() # create the handle
>>> d.setParameter(dstFrameRate=(5,1), readSize=5, cacheSize=12)
# normalize the frame rate to 5 FPS, and use a cache which size is
# 12 frames. Read 5 frames each time.
>>> success = d.FFmpegSetup(b'rtsp://localhost:8554/video')
>>> if not success: # exit if fail to connect with the server
... exit()
>>> d.start() # start the sub-thread for demuxing the stream.
>>> for i in range(10): # processing loop
... time.sleep(5)
... p = d.ExtractFrame() # every 5 seconds, read 5 frames (1 sec.)
... # do some processing
>>> d.terminate() # shut down the current thread. You could call start()
# and let it restart.
>>> d.clear() # Disconnect with the stream.
For more instructions, you could tap help(mpegCoder).
================================================================================
V3.2.0 update report:
1. Upgrade FFMpeg to 5.0.
2. Fix the const assignment bug caused by the codec configuration method.
V3.1.0 update report:
1. Support str() type for all string arguments.
2. Support http, ftp, sftp streams for MpegServer.
3. Support "nthread" option for MpegDecoder, MpegEncoder, MpegClient and
MpegServer.
4. Fix a bug caused by the constructor MpegServer().
5. Clean up all gcc warnings of the source codes.
6. Fix typos in docstrings.
V3.0.0 update report:
1. Fix a severe memory leaking bugs when using AVPacket.
2. Fix a bug caused by using MpegClient.terminate() when a video is closed
by the server.
3. Support the MpegServer. This class is used for serving the online video
streams.
4. Refactor the implementation of the loggings.
5. Add getParameter() and setParameter(configDict) APIs to MpegEncoder and
MpegServer.
6. Move FFMpeg depedencies and the OutputStream class to the cmpc space.
7. Fix dependency issues and cpp standard issues.
8. Upgrade to `FFMpeg 4.4` Version.
9. Add a quick script for fetching the `FFMpeg` dependencies.
V2.05 update report:
1. Fix a severe bug that causes the memory leak when using MpegClient.
This bug also exists in MpegDecoder, but it seems that the bug would not cause
memory leak in that case. (Although we have also fixed it now.)
2. Upgrade to FFMpeg 4.0 Version.
V2.01 update report:
Fix a bug that occurs when the first received frame may has a PTS larger than
zero.
V2.0 update report:
1. Revise the bug of the encoder which may cause the stream duration is shorter
than the real duration of the video in some not advanced media players.
2. Improve the structure of the code and remove some unnecessary codes.
3. Provide a complete version of client, which could demux the video stream
from a server in any network protocol.
V1.8 update report:
1. Provide options (widthDst, heightDst) to let MpegDecoder could control the
output size manually. To ensure the option is valid, we must use the method
'setParameter' before 'FFmpegSetup'.
2. Optimize some realization of Decoder so that its efficiency could be
improved.
V1.7 update report:
1. Realize the encoder totally.
2. Provide a global option 'dumpLevel' to control the log shown in the screen.
3. Fix bugs in initalize functions.
V1.5 update report:
1. Provide an incomplete version of encoder, which could encode frames as a
video stream that could not be played by player.
V1.4 update report:
1. Fix a severe bug of the decoder, which causes the memory collapsed if
decoding a lot of frames.
V1.2 update report:
1. Use numpy array to replace the native pyList, which improves the speed
significantlly.
V1.0 update report:
1. Provide the decoder which could decode videos in arbitrary formats and
arbitrary coding.
)";
Py_RETURN_NONE;
}
/*****************************************************************************
* Declare the core methods of the classes.
*****************************************************************************/
static int C_MPDC_init(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) { // Construct
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoPath" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.Initialize: need 'videoPath(str)'");
return -1;
}
string in_vpath;
if (!vpath) {
in_vpath.clear();
}
else if (!PyStr2str(vpath, in_vpath)) {
return -1;
}
Self->_in_Handle = new cmpc::CMpegDecoder;
if (!in_vpath.empty()) {
Self->_in_Handle->FFmpegSetup(in_vpath);
}
in_vpath.clear();
//cout << sizeof(Self->_in_Handle) << " - " << sizeof(unsigned long long) << endl;
return 0;
}
static int C_MPEC_init(C_MpegEncoder* Self) { // Construct
Self->_in_Handle = new cmpc::CMpegEncoder;
return 0;
}
static int C_MPCT_init(C_MpegClient* Self) { // Construct
Self->_in_Handle = new cmpc::CMpegClient;
return 0;
}
static int C_MPSV_init(C_MpegServer* Self) { // Construct
Self->_in_Handle = new cmpc::CMpegServer;
return 0;
}
static void C_MPDC_Destruct(C_MpegDecoder* Self) { // Destructor
delete Self->_in_Handle; // Delete the allocated class implementation.
/* If there are still other members, also need to deallocate them,
* for example, Py_XDECREF(Self->Member); */
Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject.
}
static void C_MPEC_Destruct(C_MpegEncoder* Self) { // Destructor
delete Self->_in_Handle; // Delete the allocated class implementation.
/* If there are still other members, also need to deallocate them,
* for example, Py_XDECREF(Self->Member); */
Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject.
}
static void C_MPCT_Destruct(C_MpegClient* Self) { // Destructor
delete Self->_in_Handle; // Delete the allocated class implementation.
/* If there are still other members, also need to deallocate them,
* for example, Py_XDECREF(Self->Member); */
Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject.
}
static void C_MPSV_Destruct(C_MpegServer* Self) { // Destructor
delete Self->_in_Handle; // Delete the allocated class implementation.
/* If there are still other members, also need to deallocate them,
* for example, Py_XDECREF(Self->Member); */
Py_TYPE(Self)->tp_free((PyObject*)Self); // Destruct the PyObject.
}
static PyObject* C_MPDC_Str(C_MpegDecoder* Self) { // The __str__ (print) operator.
ostringstream OStr;
OStr << *(Self->_in_Handle);
string Str = OStr.str();
return str2PyStr(Str); // Convert the string to unicode wide char.
}
static PyObject* C_MPEC_Str(C_MpegEncoder* Self) { // The __str__ (print) operator.
ostringstream OStr;
OStr << *(Self->_in_Handle);
string Str = OStr.str();
return str2PyStr(Str); // Convert the string to unicode wide char.
}
static PyObject* C_MPCT_Str(C_MpegClient* Self) { // The __str__ (print) operator.
ostringstream OStr;
OStr << *(Self->_in_Handle);
string Str = OStr.str();
return str2PyStr(Str); // Convert the string to unicode wide char.
}
static PyObject* C_MPSV_Str(C_MpegServer* Self) { // The __str__ (print) operator.
ostringstream OStr;
OStr << *(Self->_in_Handle);
string Str = OStr.str();
return str2PyStr(Str); // Convert the string to unicode wide char.
}
static PyObject* C_MPDC_Repr(C_MpegDecoder* Self) { // The __repr__ operator.
return C_MPDC_Str(Self);
}
static PyObject* C_MPEC_Repr(C_MpegEncoder* Self) { // The __repr__ operator.
return C_MPEC_Str(Self);
}
static PyObject* C_MPCT_Repr(C_MpegClient* Self) { // The __repr__ operator.
return C_MPCT_Str(Self);
}
static PyObject* C_MPSV_Repr(C_MpegServer* Self) { // The __repr__ operator.
return C_MPSV_Str(Self);
}
/*****************************************************************************
* Define the Python-C-APIs for .
* C_MPDC_Setup: Configure the decoder by the video.
* C_MPDC_ExtractFrame Extract serveral frames.
*****************************************************************************/
static PyObject* C_MPDC_Setup(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPDC_Setup method, the inputs are:
* videoPath [str/bytes->str]: the video path to be decoded.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoPath" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'");
return nullptr;
}
string in_vpath;
if (!vpath) {
in_vpath.clear();
}
else if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
bool res;
if (!in_vpath.empty())
res = Self->_in_Handle->FFmpegSetup(in_vpath);
else
res = Self->_in_Handle->FFmpegSetup();
in_vpath.clear();
if (res)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPEC_Setup(C_MpegEncoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPEC_Setup method, the inputs are:
* videoPath [str/bytes->str]: the video path to be encoded.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoPath" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'");
return nullptr;
}
string in_vpath;
if (!vpath) {
in_vpath.clear();
}
else if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
bool res;
if (!in_vpath.empty())
res = Self->_in_Handle->FFmpegSetup(in_vpath);
else
res = Self->_in_Handle->FFmpegSetup();
in_vpath.clear();
if (res)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPCT_Setup(C_MpegClient* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPCT_Setup method, the inputs are:
* videoAddress [str/bytes->str]: the video path to be demuxed.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoAddress" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'");
return nullptr;
}
string in_vpath;
if (!vpath) {
in_vpath.clear();
}
else if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
bool res;
if (!in_vpath.empty())
res = Self->_in_Handle->FFmpegSetup(in_vpath);
else
res = Self->_in_Handle->FFmpegSetup();
in_vpath.clear();
if (res)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPSV_Setup(C_MpegServer* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPSV_Setup method, the inputs are:
* videoAddress [str/bytes->str]: the video address to be served.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoAddress" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'");
return nullptr;
}
string in_vpath;
if (!vpath) {
in_vpath.clear();
}
else if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
bool res;
if (!in_vpath.empty())
res = Self->_in_Handle->FFmpegSetup(in_vpath);
else
res = Self->_in_Handle->FFmpegSetup();
in_vpath.clear();
if (res)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPDC_resetPath(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPDC_resetPath method, the inputs are:
* videoPath [str/bytes->str]: the video path to be decoded.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoPath" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'");
return nullptr;
}
string in_vpath;
if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
Self->_in_Handle->resetPath(in_vpath);
in_vpath.clear();
Py_RETURN_NONE;
}
static PyObject* C_MPEC_resetPath(C_MpegEncoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPEC_resetPath method, the inputs are:
* videoPath [str/bytes->str]: the video path to be encoded.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoPath" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoPath(str)'");
return nullptr;
}
string in_vpath;
if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
Self->_in_Handle->resetPath(in_vpath);
in_vpath.clear();
Py_RETURN_NONE;
}
static PyObject* C_MPCT_resetPath(C_MpegClient* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPCT_resetPath method, the inputs are:
* videoAddress [str/bytes->str]: the video path to be demuxed.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoAddress" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'");
return nullptr;
}
string in_vpath;
if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
Self->_in_Handle->resetPath(in_vpath);
in_vpath.clear();
Py_RETURN_NONE;
}
static PyObject* C_MPSV_resetPath(C_MpegServer* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPSV_resetPath method, the inputs are:
* videoAddress [str/bytes->str]: the video address to be served.
*/
PyObject* vpath = nullptr;
cmpc::CharList kwlist_str({ "videoAddress" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &vpath)) {
PyErr_SetString(PyExc_TypeError, "Error.FFmpegSetup: need 'videoAddress(str)'");
return nullptr;
}
string in_vpath;
if (!PyStr2str(vpath, in_vpath)) {
return nullptr;
}
Self->_in_Handle->resetPath(in_vpath);
in_vpath.clear();
Py_RETURN_NONE;
}
static PyObject* C_MPCT_Start(C_MpegClient* Self) {
/* Wrapped (void)Start method, the input is required to be empty. */
auto success = Self->_in_Handle->start();
if (!success) {
PyErr_SetString(PyExc_ConnectionError, "Error.Start: before call this method, need to call FFmpegSetup() successfully, and also you should not call it when the decoding thread is running.'");
return nullptr;
}
Py_RETURN_NONE;
}
static PyObject* C_MPCT_Terminate(C_MpegClient* Self) {
/* Wrapped (void)Terminate method, the input is required to be empty. */
Self->_in_Handle->terminate();
Py_RETURN_NONE;
}
/* Pay attention to the following two methods :
* Why do we remove the Py_IN/DECREF?
* Because no temp variables are created, so we do not need to manage them,
* but just use None as the returned value. */
static PyObject* FreePyArray(PyArrayObject* PyArray) {
uint8_t* out_dataptr = (uint8_t*)PyArray_DATA(PyArray);
delete[] out_dataptr;
return nullptr;
}
void FreePyList(PyObject* PyList) {
Py_ssize_t getlen = PyList_Size(PyList);
for (Py_ssize_t i = 0; i < getlen; i++) {
PyObject* Item = PyList_GetItem(PyList, i);
FreePyArray((PyArrayObject*)Item);
}
Py_DECREF(PyList);
PyGC_Collect();
}
static PyObject* C_MPDC_ExtractFrame(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (int)ExtractFrame method, the inputs are:
* framePos [int->int64_t]: the start position of the extracted frames.
* frameNum [int->int64_t]: the number of extracted frames.
*/
int64_t framePos = 0, frameNum = 1;
cmpc::CharList kwlist_str({ "framePos", "frameNum" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|LL", kwlist, &framePos, &frameNum)) {
PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame: need 'framePos(int)/frameNum(int)'");
return nullptr;
}
PyObject* PyFrameList = PyList_New(static_cast<Py_ssize_t>(0));
//cout << framePos << " - " << frameNum << endl;
bool res = Self->_in_Handle->ExtractFrame(PyFrameList, framePos, frameNum, 0, 0);
Py_ssize_t getlen = PyList_Size(PyFrameList);
res = res && (getlen > 0);
if (res) {
PyObject* PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4);
FreePyList(PyFrameList);
return PyFrameArray;
}
else {
Py_DECREF(PyFrameList);
PyGC_Collect();
Py_RETURN_NONE;
}
}
static PyObject* C_MPDC_ExtractFrame_Time(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (int)ExtractFrame method, the inputs are:
* timePos [float->double]: the start position (time unit) of the extracted frames.
* frameNum [int->int64_t]: the number of extracted frames.
*/
double timePos = 0;
int64_t frameNum = 1;
cmpc::CharList kwlist_str({ "timePos", "frameNum" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|dL", kwlist, &timePos, &frameNum)) {
PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame_Time: need 'timePos(float)/frameNum(int)'");
return nullptr;
}
PyObject* PyFrameList = PyList_New(static_cast<Py_ssize_t>(0));
//cout << framePos << " - " << frameNum << endl;
bool res = Self->_in_Handle->ExtractFrame(PyFrameList, 0, frameNum, timePos, 1);
Py_ssize_t getlen = PyList_Size(PyFrameList);
res = res && (getlen > 0);
if (res) {
PyObject* PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4);
FreePyList(PyFrameList);
return PyFrameArray;
}
else {
Py_DECREF(PyFrameList);
PyGC_Collect();
Py_RETURN_NONE;
}
}
static PyObject* C_MPEC_EncodeFrame(C_MpegEncoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)EncodeFrame method, the inputs are:
* PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded.
*/
PyObject* PyArrayFrame = nullptr;
cmpc::CharList kwlist_str({ "PyArrayFrame" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) {
PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'");
return nullptr;
}
int res = Self->_in_Handle->EncodeFrame(reinterpret_cast<PyArrayObject*>(PyArrayFrame));
if (res >= 0)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPSV_ServeFrame(C_MpegServer* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)ServeFrame method, the inputs are:
* PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded and served.
*/
PyObject* PyArrayFrame = nullptr;
cmpc::CharList kwlist_str({ "PyArrayFrame" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) {
PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'");
return nullptr;
}
int res = Self->_in_Handle->ServeFrame(reinterpret_cast<PyArrayObject*>(PyArrayFrame));
if (res >= 0)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPSV_ServeFrameBlock(C_MpegServer* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)ServeFrameBlock method, the inputs are:
* PyArrayFrame [ndarray->PyArrayObject]: the frame to be encoded and served.
*/
PyObject* PyArrayFrame = nullptr;
cmpc::CharList kwlist_str({ "PyArrayFrame" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &PyArrayFrame)) {
PyErr_SetString(PyExc_TypeError, "Error.EncodeFrame: need 'PyArrayFrame(ndarray)'");
return nullptr;
}
int res = Self->_in_Handle->ServeFrameBlock(reinterpret_cast<PyArrayObject*>(PyArrayFrame));
if (res >= 0)
Py_RETURN_TRUE;
else
Py_RETURN_FALSE;
}
static PyObject* C_MPCT_ExtractFrame(C_MpegClient* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (int)ExtractFrame method, the inputs are:
* readSize [int->int64_t]: the number of frames to be readed. This value could not
* exceeded the size of the frame buffer.
*/
int64_t readSize = 0;
cmpc::CharList kwlist_str({ "readSize" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &readSize)) {
PyErr_SetString(PyExc_TypeError, "Error.ExtractFrame: need 'readSize(int)'");
return nullptr;
}
PyObject* res = nullptr;
if (readSize > 0)
res = Self->_in_Handle->ExtractFrame(readSize);
else
res = Self->_in_Handle->ExtractFrame();
if (res) {
return res;
}
else {
Py_RETURN_NONE;
}
}
static PyObject* C_MPDC_ExtractGOP(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (int)ExtractGOP method, the inputs are:
* framePos [int->int64_t]: the start position of the GOP to be extracted.
*/
int64_t framePos = -1;
cmpc::CharList kwlist_str({ "framePos" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|L", kwlist, &framePos)) {
PyErr_SetString(PyExc_TypeError, "Error.ExtractGOP: need 'framePos(int)'");
return nullptr;
}
PyObject* PyFrameList = PyList_New(static_cast<Py_ssize_t>(0));
//cout << framePos << " - " << frameNum << endl;
if (!(framePos < 0))
Self->_in_Handle->setGOPPosition(framePos);
bool res = Self->_in_Handle->ExtractGOP(PyFrameList);
Py_ssize_t getlen = PyList_Size(PyFrameList);
res = res && (getlen > 0);
if (res) {
PyObject* PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4);
FreePyList(PyFrameList);
return PyFrameArray;
}
else {
Py_DECREF(PyFrameList);
PyGC_Collect();
Py_RETURN_NONE;
}
}
static PyObject* C_MPDC_ExtractGOP_Time(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (int)ExtractGOP_Time method, the inputs are:
* timePos [float->double]: the start position (time unit) of the GOP to be extracted.
*/
double timePos = -1;
cmpc::CharList kwlist_str({ "timePos" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|d", kwlist, &timePos)) {
PyErr_SetString(PyExc_TypeError, "Error.ExtractGOP_Time: need 'timePos(float)'");
return nullptr;
}
PyObject* PyFrameList = PyList_New(static_cast<Py_ssize_t>(0));
//cout << framePos << " - " << frameNum << endl;
if (!(timePos < 0))
Self->_in_Handle->setGOPPosition(timePos);
bool res = Self->_in_Handle->ExtractGOP(PyFrameList);
Py_ssize_t getlen = PyList_Size(PyFrameList);
res = res && (getlen > 0);
if (res) {
PyObject* PyFrameArray = PyArray_FromObject(PyFrameList, NPY_UINT8, 4, 4);
FreePyList(PyFrameList);
return PyFrameArray;
}
else {
Py_DECREF(PyFrameList);
PyGC_Collect();
Py_RETURN_NONE;
}
}
static PyObject* C_MPDC_setGOPPosition(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (void)setGOPPosition method, the inputs are:
* framePos [int->int64_t]: the start position of the GOP to be extracted.
* timePos [float->double]: the start position (time unit) of the GOP to be extracted.
*/
int64_t framePos = -1;
double timePos = -1;
cmpc::CharList kwlist_str({ "framePos", "timePos" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ld", kwlist, &framePos, &timePos)) {
PyErr_SetString(PyExc_TypeError, "Error.setGOPPosition: need 'framePos(int)'/'timePos(float)'");
return nullptr;
}
if (!(framePos < 0))
Self->_in_Handle->setGOPPosition(framePos);
else if (!(timePos < 0))
Self->_in_Handle->setGOPPosition(timePos);
Py_RETURN_NONE;
}
static PyObject* C_MPDC_getParam(C_MpegDecoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPDC_getParam function, the inputs are:
* paramName [str/bytes->str]: The name of the parameter to be gotten, could be.
* videoPath: [str] Path of the current video.
* width/height: [int] The width / height of the frame.
* frameCount: [int] The count of frames of the current decoding work.
* coderName: [str] The name of the decoder.
* nthread: [int] The number of decoder threads.
* duration: [float] The duration of the video.
* estFrameNum: [int] The estimated total frame number.
* avgFrameRate [float] The average frame rate.
*/
PyObject* param = nullptr;
cmpc::CharList kwlist_str({ "paramName" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) {
PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'");
return nullptr;
}
string in_param;
if (!param) {
in_param.clear();
}
else if (!PyStr2str(param, in_param)) {
return nullptr;
}
PyObject* res = nullptr;
if (in_param.empty()) {
res = Self->_in_Handle->getParameter();
}
else {
res = Self->_in_Handle->getParameter(in_param);
}
in_param.clear();
return res;
}
static PyObject* C_MPEC_getParam(C_MpegEncoder* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPEC_getParam function, the inputs are:
* paramName [str/bytes->str]: The name of the parameter to be gotten, could be.
* videoPath: [str] Path of the current video.
* codecName: [str] The name of the codec.
* nthread: [int] The number of encoder threads.
* bitRate: [int] The target bit rate.
* width/height: [int] The width / height of the encoded frame.
* widthSrc/heightSrc: [int] The width / height of the input frame.
* GOPSize: [int] The size of one GOP.
* maxBframe: [int] The maximal number of continuous B frames.
* frameRate: [float] The target frame rate.
*/
PyObject* param = nullptr;
cmpc::CharList kwlist_str({ "paramName" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) {
PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'");
return nullptr;
}
string in_param;
if (!param) {
in_param.clear();
}
else if (!PyStr2str(param, in_param)) {
return nullptr;
}
PyObject* res = nullptr;
if (in_param.empty()) {
res = Self->_in_Handle->getParameter();
}
else {
res = Self->_in_Handle->getParameter(in_param);
}
in_param.clear();
return res;
}
static PyObject* C_MPCT_getParam(C_MpegClient* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPCT_getParam method, the inputs are:
* parameter [str/bytes->str]: The name of the parameter to be gotten, could be.
* videoAddress: [str] The address of the current video.
* width/height: [int] The width / height of the received frame.
* frameCount: [int] The count of frames of the current decoding work.
* coderName: [str] The name of the decoder.
* nthread: [int] The number of decoder threads.
* duration: [float] The duration of the video.
* estFrameNum: [int] The estimated total frame number.
* avgFrameRate [float] The average frame rate.
*/
PyObject* param = nullptr;
cmpc::CharList kwlist_str({ "paramName" });
auto kwlist_ptr = kwlist_str.c_str();
auto kwlist = (char**)(kwlist_ptr.get());
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, ¶m)) {
PyErr_SetString(PyExc_TypeError, "Error.getParameter: need 'paramName(str)'");
return nullptr;
}
string in_param;
if (!param) {
in_param.clear();
}
else if (!PyStr2str(param, in_param)) {
return nullptr;
}
PyObject* res = nullptr;
if (in_param.empty()) {
res = Self->_in_Handle->getParameter();
}
else {
res = Self->_in_Handle->getParameter(in_param);
}
in_param.clear();
return res;
}
static PyObject* C_MPSV_getParam(C_MpegServer* Self, PyObject* args, PyObject* kwargs) {
/* Wrapped (bool)C_MPSV_getParam function, the inputs are:
* paramName [str/bytes->str]: The name of the parameter to be gotten, could be.
* videoAddress: [str] The address of the current video.
* codecName: [str] The name of the codec.
* formatName: [str] The name of the stream format.
* nthread: [int] The number of encoder threads.