|
11 | 11 |
|
12 | 12 | import javaforce.*; |
13 | 13 | import javaforce.awt.*; |
| 14 | +import javaforce.voip.*; |
14 | 15 | import javaforce.media.*; |
15 | 16 |
|
16 | 17 | import com.jhlabs.image.*; |
@@ -93,7 +94,10 @@ public long seek(MediaCoder coder, long pos, int how) { |
93 | 94 | private boolean ready; |
94 | 95 | private JFImage srcImage; |
95 | 96 | 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; |
97 | 101 | private short audio[]; |
98 | 102 | private int audioPos, audioSize; |
99 | 103 | private ArrayList<int[]> frames = new ArrayList<int[]>(); |
@@ -156,20 +160,24 @@ public boolean start(Config config) { |
156 | 160 | JFLog.log(e); |
157 | 161 | return false; |
158 | 162 | } |
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)) { |
161 | 165 | JFAWT.showError("Error", "Failed to decode file:" + path[0]); |
162 | 166 | return false; |
163 | 167 | } |
| 168 | + info = decoder.getCodecInfo(); |
| 169 | + audio_decoder = decoder.createAudioDecoder(); |
| 170 | + video_decoder = decoder.createVideoDecoder(); |
| 171 | + |
164 | 172 | if (type == TYPE_VIDEO) { |
165 | 173 | //calc frameRate |
166 | | - JFLog.log("input frameRate=" + ff.getFrameRate()); |
| 174 | + JFLog.log("input frameRate=" + info.fps); |
167 | 175 | JFLog.log("output frameRate=" + videoRate); |
168 | | - frameRateRatio = ff.getFrameRate() / videoRate; |
| 176 | + frameRateRatio = info.fps / videoRate; |
169 | 177 | JFLog.log("frameRateRatio=" + frameRateRatio); |
170 | 178 | currentFrame = 0.0; |
171 | 179 | } |
172 | | - JFLog.log("audioRate=" + ff.getAudioBitRate()); |
| 180 | + JFLog.log("audioRate=" + decoder.getAudioBitRate()); |
173 | 181 | audio = null; |
174 | 182 | audioSize = 0; |
175 | 183 | audioPos = 0; |
@@ -220,44 +228,38 @@ public void stop() { |
220 | 228 | audioSize = 0; |
221 | 229 | audioPos = 0; |
222 | 230 | frames.clear(); |
223 | | - if (ff != null) { |
224 | | - ff.stop(); |
225 | | - ff = null; |
| 231 | + if (decoder != null) { |
| 232 | + decoder.close(); |
| 233 | + decoder = null; |
226 | 234 | } |
227 | 235 | ready = false; |
228 | 236 | } |
229 | 237 | private void readMore() { |
230 | 238 | 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); |
261 | 263 | } |
262 | 264 | } |
263 | 265 |
|
@@ -404,7 +406,7 @@ public void renderAudio(short render[]) { |
404 | 406 | if (type == TYPE_SPECIAL_TEXT) return; |
405 | 407 | int renderSize = render.length; |
406 | 408 | int renderPos = 0; |
407 | | - if (ff.getAudioBitRate() == 0) return; |
| 409 | + if (decoder.getAudioBitRate() == 0) return; |
408 | 410 | while (renderSize > 0) { |
409 | 411 | while (!eof && audioSize < renderSize) readMore(); |
410 | 412 | int toCopy = renderSize; |
@@ -442,12 +444,12 @@ public void audioCopyAttn(short in[], int inpos, short out[], int outpos, int le |
442 | 444 | } |
443 | 445 | } |
444 | 446 | public long getDuration() { |
445 | | - if (ff == null) return 0; |
446 | | - return ff.getDuration(); |
| 447 | + if (decoder == null) return 0; |
| 448 | + return info.duration; |
447 | 449 | } |
448 | 450 | public void seek(int pos) { |
449 | | - if (ff == null) return; |
450 | | - ff.seek(pos); |
| 451 | + if (decoder == null) return; |
| 452 | + decoder.seek(pos); |
451 | 453 | } |
452 | 454 | public void createPreview(JFTask task) { |
453 | 455 | if (!start(previewConfig)) return; |
@@ -481,7 +483,7 @@ public void createPreview(JFTask task) { |
481 | 483 | audio = null; |
482 | 484 | audioSize = 0; |
483 | 485 | previewImage = new JFImage(64, 60); |
484 | | - ff.seek(frame); |
| 486 | + decoder.seek(frame); |
485 | 487 | preRenderVideo(); |
486 | 488 | renderVideo(previewImage, 0, 0); |
487 | 489 | if (audioSize > 0) { |
|
0 commit comments