-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTestVC.cpp
228 lines (197 loc) · 6.57 KB
/
TestVC.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
/**
MIT License
Copyright (c) 2018 David G. Starkweather
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
#include <thread>
#include "VideoCapture.hpp"
#ifdef USE_ASOUNDLIB
#include "playaudio.hpp"
#endif
#include <opencv2/core/core_c.h>
#include <opencv2/highgui/highgui_c.h>
using namespace std;
void pause(int64_t current_pts, int64_t &last_pts, AVRational time_base){
if (current_pts != AV_NOPTS_VALUE){
if (last_pts != AV_NOPTS_VALUE){
int64_t lag = current_pts - last_pts;
int64_t delay = av_rescale_q(lag, time_base, AV_TIME_BASE_Q);
if (delay > 0){
usleep(delay);
}
}
last_pts = current_pts;
}
}
void print_metadata(ph::MetaData &mdata){
cout << "artist: " << mdata.artist_str << endl;
cout << "title: " << mdata.title_str << endl;
cout << "album: " << mdata.album_str << endl;
cout << "genre: " << mdata.genre_str << endl;
cout << "composer: " << mdata.composer_str << endl;
cout << "performer: " << mdata.performer_str << endl;
cout << "album artist: " << mdata.album_artist_str << endl;
cout << "copyright: " << mdata.copyright_str << endl;
cout << "date: " << mdata.date_str << endl;
cout << "track: " << mdata.track_str << endl;
cout << "disc: " << mdata.disc_str << endl;
}
void process_timestamp(int64_t ts, AVRational tb, int &hrs, int &mins, int &secs){
int64_t seconds = av_rescale(ts, tb.num, tb.den);
hrs = seconds/3600;
int64_t remaining_secs = seconds%3600;
mins = remaining_secs/60;
secs = remaining_secs%60;
}
void process_video(ph::VideoCapture *vc){
int count = 0;
int64_t last_pts = AV_NOPTS_VALUE;
AVRational time_base = vc->GetVideoTimebase();
AVFrame *frame = vc->PullVideoFrame();
if (frame == NULL) return;
cvNamedWindow("main", CV_WINDOW_AUTOSIZE);
CvSize sz;
sz.width = frame->width;
sz.height = frame->height;
IplImage *img = cvCreateImageHeader(sz, IPL_DEPTH_8U, 1);
assert(img);
int64_t start_ts = frame->pts, ts;
while (frame != NULL){
ts = frame->pts;
cvSetData(img, frame->data[0], frame->linesize[0]);
pause(frame->pts, last_pts, time_base);
cvShowImage("main", img);
cvWaitKey(10);
count++;
av_frame_free(&frame);
frame = vc->PullVideoFrame();
}
int hrs, mins, secs;
process_timestamp(ts - start_ts, time_base, hrs, mins, secs);
cout << "video frames processed " << count << " in " << hrs << ":" << mins << ":" << secs << endl;
cvDestroyAllWindows();
}
void process_audio(ph::VideoCapture *vc){
const string hwdev = "plughw:0,0";
int nbsamples;
int total = 0;
int count = 0;
int buffer_size = 1024;
#ifdef USE_ASOUNDLIB
int sr = vc->GetAudioSampleRate();
snd_pcm_t *pcm_handle = audio_init(hwdev, sr, buffer_size);
cout << "buffer size " << buffer_size << endl;
#endif
#ifdef USE_ASOUNDLIB
assert(pcm_handle);
#endif
int N = buffer_size;
int16_t *buf = new int16_t[N];
while ((nbsamples = vc->PullAudioSamples(buf, N)) > 0){
count++;
total += nbsamples;
#ifdef USE_ASOUNDLIB
play_samples(pcm_handle, buf, nbsamples);
#endif
}
cout << "no. audio buffers: " << count << endl;
cout << "no. audio samples: " << total << endl;
#ifdef USE_ASOUNDLIB
close_audio(pcm_handle);
#endif
delete[] buf;
}
void process_subs(ph::VideoCapture *vc){
AVSubtitle *sub = NULL;
int count = 0;
while ((sub = vc->PullSubtitle()) != NULL){
for (unsigned int i=0;i<sub->num_rects;i++){
cout << "(" << sub->pts << ") " << string(sub->rects[i]->ass) << endl;
}
count++;
avsubtitle_free(sub);
free(sub);
}
cout << "no. subtitles: " << count << endl;
}
void process_main(ph::VideoCapture *vc, int64_t secs){
assert(vc != NULL);
try {
vc->Process(secs);
} catch (ph::VideoCaptureException &ex){
cout << "VC Exception: unable to process video: " << ex.what() << endl;
} catch (ph::AudioCaptureException &ex){
cout << "AC Exception: unable to process audio: " << ex.what() << endl;
}
}
int main(int argc, char **argv){
if (argc < 4){
cout << "not enough args." << endl;
cout << "usage: prog filename secs fps" << endl;
return 0;
}
const string filename = argv[1];
const int margin = 0;
const int sr = 8000;
const int64_t secs = atoi(argv[2]);
const int fps = atoi(argv[3]);
const int width = 400;
const bool warn = true;
cout << "file: " << filename << endl;
cout << "margin: " << margin << endl;
cout << "no. seconds to play: " << secs << endl;
cout << "width: " << width << endl;
cout << "fps: " << fps << endl;
thread video_thr;
thread audio_thr;
thread subtitle_thr;
thread main_thr;
int flag = PHCAPTURE_ALL_FLAG;
int audio_fmt = PHAUDIO_S16_FMT; /** PHAUDIO_FLT_FMT, PHAUDIO_S16_FMT **/
try {
cout << "initialize video capture with flag: " << flag<< endl;
ph::VideoCapture *vc = new ph::VideoCapture(filename, margin, margin,
margin, margin, sr, width, flag, audio_fmt, fps, warn);
cout << "no. streams: " << vc->GetNumberStreams() << endl;
cout << "no. programs: " << vc->GetNumberPrograms() << endl;
ph::MetaData mdata = vc->GetMetaData();
print_metadata(mdata);
uint32_t nbframes = vc->CountVideoPackets();
double fr = vc->GetAvgFrameRate_d();
cout << "no. video frames: " << nbframes << endl;
cout << "avg frame rate: " << fr << endl;
main_thr = thread(process_main, vc, secs);
video_thr = thread(process_video, vc);
audio_thr = thread(process_audio, vc);
subtitle_thr = thread(process_subs, vc);
main_thr.join();
video_thr.join();
audio_thr.join();
subtitle_thr.join();
delete vc;
} catch (ph::VideoCaptureException &ex){
cout << "vc error: " << ex.what() << endl;
} catch (ph::AudioCaptureException &ex){
cout << "vc audio error: " << ex.what() << endl;
}
cout << "done." << endl;
return 0;
}