Skip to content

Commit b4e74a5

Browse files
committed
jfVideo : convert Media*Coder -> MediaInput/Output
1 parent d3990ea commit b4e74a5

File tree

2 files changed

+73
-54
lines changed

2 files changed

+73
-54
lines changed

projects/jfvideo/src/Element.java

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import javaforce.*;
1313
import javaforce.awt.*;
14+
import javaforce.voip.*;
1415
import javaforce.media.*;
1516

1617
import com.jhlabs.image.*;
@@ -93,7 +94,10 @@ public long seek(MediaCoder coder, long pos, int how) {
9394
private boolean ready;
9495
private JFImage srcImage;
9596
private int width, height;
96-
private MediaDecoder ff;
97+
private MediaInput decoder;
98+
private MediaAudioDecoder audio_decoder;
99+
private MediaVideoDecoder video_decoder;
100+
private CodecInfo info;
97101
private short audio[];
98102
private int audioPos, audioSize;
99103
private ArrayList<int[]> frames = new ArrayList<int[]>();
@@ -156,20 +160,24 @@ public boolean start(Config config) {
156160
JFLog.log(e);
157161
return false;
158162
}
159-
ff = new MediaDecoder();
160-
if (!ff.start(this, config.width, config.height, config.audioChannels, config.audioRate, true)) {
163+
decoder = new MediaInput();
164+
if (!decoder.open(this)) {
161165
JFAWT.showError("Error", "Failed to decode file:" + path[0]);
162166
return false;
163167
}
168+
info = decoder.getCodecInfo();
169+
audio_decoder = decoder.createAudioDecoder();
170+
video_decoder = decoder.createVideoDecoder();
171+
164172
if (type == TYPE_VIDEO) {
165173
//calc frameRate
166-
JFLog.log("input frameRate=" + ff.getFrameRate());
174+
JFLog.log("input frameRate=" + info.fps);
167175
JFLog.log("output frameRate=" + videoRate);
168-
frameRateRatio = ff.getFrameRate() / videoRate;
176+
frameRateRatio = info.fps / videoRate;
169177
JFLog.log("frameRateRatio=" + frameRateRatio);
170178
currentFrame = 0.0;
171179
}
172-
JFLog.log("audioRate=" + ff.getAudioBitRate());
180+
JFLog.log("audioRate=" + decoder.getAudioBitRate());
173181
audio = null;
174182
audioSize = 0;
175183
audioPos = 0;
@@ -220,44 +228,38 @@ public void stop() {
220228
audioSize = 0;
221229
audioPos = 0;
222230
frames.clear();
223-
if (ff != null) {
224-
ff.stop();
225-
ff = null;
231+
if (decoder != null) {
232+
decoder.close();
233+
decoder = null;
226234
}
227235
ready = false;
228236
}
229237
private void readMore() {
230238
if (eof) return;
231-
switch (ff.read()) {
232-
case MediaCoder.AUDIO_FRAME:
233-
//JFLog.log("AUDIO_FRAME");
234-
short buf[] = ff.getAudio();
235-
if (audioSize == 0) {
236-
audio = buf;
237-
audioSize = buf.length;
238-
audioPos = 0;
239-
} else {
240-
//add to audio
241-
short newAudio[] = new short[audioSize + buf.length];
242-
System.arraycopy(audio, audioPos, newAudio, 0, audioSize);
243-
System.arraycopy(buf, 0, newAudio, audioSize, buf.length);
244-
audio = newAudio;
245-
audioPos = 0;
246-
audioSize += buf.length;
247-
}
248-
break;
249-
case MediaCoder.VIDEO_FRAME:
250-
//JFLog.log("VIDEO_FRAME");
251-
int px[] = ff.getVideo();
252-
frames.add(px);
253-
break;
254-
case MediaCoder.NULL_FRAME:
255-
//JFLog.log("NULL_FRAME");
256-
break;
257-
case MediaCoder.END_FRAME:
258-
//JFLog.log("END_FRAME");
259-
eof = true;
260-
break;
239+
Packet packet = decoder.readPacket();
240+
if (packet == null) {
241+
eof = true;
242+
return;
243+
}
244+
if (packet.stream == info.audio_stream) {
245+
short buf[] = audio_decoder.decode(packet);
246+
if (audioSize == 0) {
247+
audio = buf;
248+
audioSize = buf.length;
249+
audioPos = 0;
250+
} else {
251+
//add to audio
252+
short newAudio[] = new short[audioSize + buf.length];
253+
System.arraycopy(audio, audioPos, newAudio, 0, audioSize);
254+
System.arraycopy(buf, 0, newAudio, audioSize, buf.length);
255+
audio = newAudio;
256+
audioPos = 0;
257+
audioSize += buf.length;
258+
}
259+
}
260+
if (packet.stream == info.video_stream) {
261+
int px[] = video_decoder.decode(packet);
262+
frames.add(px);
261263
}
262264
}
263265

@@ -404,7 +406,7 @@ public void renderAudio(short render[]) {
404406
if (type == TYPE_SPECIAL_TEXT) return;
405407
int renderSize = render.length;
406408
int renderPos = 0;
407-
if (ff.getAudioBitRate() == 0) return;
409+
if (decoder.getAudioBitRate() == 0) return;
408410
while (renderSize > 0) {
409411
while (!eof && audioSize < renderSize) readMore();
410412
int toCopy = renderSize;
@@ -442,12 +444,12 @@ public void audioCopyAttn(short in[], int inpos, short out[], int outpos, int le
442444
}
443445
}
444446
public long getDuration() {
445-
if (ff == null) return 0;
446-
return ff.getDuration();
447+
if (decoder == null) return 0;
448+
return info.duration;
447449
}
448450
public void seek(int pos) {
449-
if (ff == null) return;
450-
ff.seek(pos);
451+
if (decoder == null) return;
452+
decoder.seek(pos);
451453
}
452454
public void createPreview(JFTask task) {
453455
if (!start(previewConfig)) return;
@@ -481,7 +483,7 @@ public void createPreview(JFTask task) {
481483
audio = null;
482484
audioSize = 0;
483485
previewImage = new JFImage(64, 60);
484-
ff.seek(frame);
486+
decoder.seek(frame);
485487
preRenderVideo();
486488
renderVideo(previewImage, 0, 0);
487489
if (audioSize > 0) {

projects/jfvideo/src/ProjectPanel.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import javaforce.*;
1616
import javaforce.awt.*;
17+
import javaforce.voip.*;
1718
import javaforce.media.*;
1819
import javaforce.gl.*;
1920

@@ -1107,17 +1108,30 @@ public boolean work() {
11071108
this.setProgress(0);
11081109
int audioLengthPerFrame = config.audioRate / config.videoRate;
11091110
int audioFraction = config.audioRate % config.videoRate;
1110-
MediaEncoder ff = new MediaEncoder();
1111-
ff.set1000over1001(config.v1001);
1111+
MediaOutput encoder = new MediaOutput();
1112+
1113+
CodecInfo info = new CodecInfo();
1114+
Packet packet;
1115+
1116+
info.audio_bit_rate = config.audioBitRate;
1117+
info.freq = config.audioRate;
1118+
info.chs = config.audioChannels;
1119+
info.bits = 16;
1120+
1121+
info.video_bit_rate = config.videoBitRate;
1122+
info.fps = config.videoRate;
1123+
info.width = config.width;
1124+
info.height = config.height;
1125+
11121126
System.out.println("bitRates:" + config.videoBitRate + "," + config.audioBitRate);
1113-
ff.setVideoBitRate(config.videoBitRate);
1114-
ff.setAudioBitRate(config.audioBitRate);
1115-
if (!ff.start(ProjectPanel.this, config.width, config.height, config.videoRate, config.audioChannels
1116-
, config.audioRate, "avi", true, true))
1127+
if (!encoder.create(ProjectPanel.this, "avi"))
11171128
{
11181129
this.setLabel("Error:Encoder failed");
11191130
return false;
11201131
}
1132+
MediaAudioEncoder audio_encoder = encoder.createAudioEncoder(info);
1133+
MediaVideoEncoder video_encoder = encoder.createVideoEncoder(info);
1134+
11211135
int audioFractionCounter = 0;
11221136
short audio0[] = new short[audioLengthPerFrame * config.audioChannels];
11231137
short audio1[] = new short[(audioLengthPerFrame+1) * config.audioChannels];
@@ -1196,12 +1210,15 @@ public boolean work() {
11961210
if (track.isCut()) cut = true;
11971211
} //track
11981212
if (cut) continue;
1199-
ff.addVideo(image.getBuffer());
1200-
ff.addAudio(audio);
1213+
packet = audio_encoder.encode(audio, 0, audio.length);
1214+
encoder.writePacket(packet);
1215+
int[] px = image.getBuffer();
1216+
packet = video_encoder.encode(px, 0, px.length);
1217+
encoder.writePacket(packet);
12011218
} //frame
12021219
} //second
12031220
uninit3d();
1204-
ff.stop();
1221+
encoder.close();
12051222
for(int trackNo=1;trackNo<tracks.getComponentCount();trackNo++) {
12061223
TrackPanel track = (TrackPanel)tracks.getComponent(trackNo);
12071224
track.stop();

0 commit comments

Comments
 (0)