forked from bytedeco/javacv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeinterlacedVideoPlayer.java
83 lines (68 loc) · 2.16 KB
/
DeinterlacedVideoPlayer.java
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
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.FFmpegFrameFilter;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameFilter;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.bytedeco.ffmpeg.global.avutil;
public class DeinterlacedVideoPlayer {
private static final int DEVICE_ID = 0;
private static final int WIDTH = 640;
private static final int HEIGHT = 480;
private static final int FRAMERATE = 25;
private static final int PIXEL_FORMAT = avutil.AV_PIX_FMT_BGR24;;
private String ffmpegString = "yadif=mode=0:parity=-1:deint=0,format=bgr24";
private FrameGrabber grabber;
public DeinterlacedVideoPlayer() {}
public void start() {
FrameFilter filter = null;
try {
startFrameGrabber();
Frame frame = null;
while ((frame = grabber.grab()) != null) {
if (filter == null) {
filter = new FFmpegFrameFilter(ffmpegString, frame.imageWidth, frame.imageHeight);
filter.setPixelFormat(PIXEL_FORMAT);
filter.start();
}
filter.push(frame);
frame = filter.pull();
// do something with the filtered frame
}
} catch (Exception | org.bytedeco.javacv.FrameFilter.Exception e) {
throw new RuntimeException(e.getMessage(), e);
} finally {
releaseGrabberAndFilter(this.grabber, filter);
}
}
private void startFrameGrabber() throws Exception {
grabber = new OpenCVFrameGrabber(DEVICE_ID);
grabber.setImageWidth(WIDTH);
grabber.setImageHeight(HEIGHT);
grabber.setFrameRate(FRAMERATE);
grabber.setPixelFormat(PIXEL_FORMAT);
grabber.start();
}
private void releaseGrabberAndFilter(FrameGrabber grabber, FrameFilter filter) {
try {
if (grabber != null) {
grabber.release();
}
} catch (Exception e) {
throw new RuntimeException("Cannot release frame grabber!", e);
} finally {
releaseFilter(filter);
}
}
private void releaseFilter(FrameFilter filter) {
if (filter == null) {
return;
}
try {
filter.close();
} catch (org.bytedeco.javacv.FrameFilter.Exception e) {
throw new RuntimeException("Cannot close frame filter!", e);
}
}
}