|
6 | 6 |
|
7 | 7 | import org.jcodec.api.specific.AVCMP4Adaptor; |
8 | 8 | import org.jcodec.api.specific.ContainerAdaptor; |
| 9 | +import org.jcodec.codecs.h264.H264Decoder; |
| 10 | +import org.jcodec.codecs.mpeg12.MPEGDecoder; |
| 11 | +import org.jcodec.codecs.prores.ProresDecoder; |
| 12 | +import org.jcodec.common.Codec; |
9 | 13 | import org.jcodec.common.DemuxerTrack; |
10 | 14 | import org.jcodec.common.DemuxerTrackMeta; |
11 | 15 | import org.jcodec.common.JCodecUtil; |
12 | 16 | import org.jcodec.common.JCodecUtil.Format; |
13 | 17 | import org.jcodec.common.SeekableDemuxerTrack; |
| 18 | +import org.jcodec.common.VideoDecoder; |
14 | 19 | import org.jcodec.common.io.FileChannelWrapper; |
15 | 20 | import org.jcodec.common.io.NIOUtils; |
16 | 21 | import org.jcodec.common.io.SeekableByteChannel; |
17 | 22 | import org.jcodec.common.model.Packet; |
18 | | -import org.jcodec.common.model.Picture; |
19 | 23 | import org.jcodec.common.model.Picture8Bit; |
| 24 | +import org.jcodec.common.model.Size; |
| 25 | +import org.jcodec.containers.mp4.MP4Packet; |
| 26 | +import org.jcodec.containers.mp4.boxes.SampleEntry; |
| 27 | +import org.jcodec.containers.mp4.demuxer.AbstractMP4DemuxerTrack; |
20 | 28 | import org.jcodec.containers.mp4.demuxer.MP4Demuxer; |
21 | 29 |
|
22 | 30 | /** |
@@ -41,50 +49,18 @@ public class FrameGrab8Bit { |
41 | 49 | private ContainerAdaptor decoder; |
42 | 50 | private ThreadLocal<byte[][]> buffers; |
43 | 51 |
|
44 | | - private static int _detectKeyFrame(DemuxerTrack videoTrack, int start) throws IOException { |
45 | | - int[] seekFrames = videoTrack.getMeta().getSeekFrames(); |
46 | | - if (seekFrames == null) |
47 | | - return start; |
48 | | - int prev = seekFrames[0]; |
49 | | - for (int i = 1; i < seekFrames.length; i++) { |
50 | | - if (seekFrames[i] > start) |
51 | | - break; |
52 | | - prev = seekFrames[i]; |
53 | | - } |
54 | | - return prev; |
55 | | - } |
56 | | - |
57 | | - private static ContainerAdaptor _detectDecoder(SeekableDemuxerTrack sdt, Packet frame) throws JCodecException { |
58 | | - DemuxerTrackMeta meta = sdt.getMeta(); |
59 | | - switch (meta.getCodec()) { |
60 | | - case H264: |
61 | | - return new AVCMP4Adaptor(meta); |
62 | | - default: |
63 | | - throw new UnsupportedFormatException("Codec is not supported"); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - static ContainerAdaptor detectDecoder(SeekableDemuxerTrack sdt) throws IOException, JCodecException { |
68 | | - int curFrame = (int) sdt.getCurFrame(); |
69 | | - int keyFrame = _detectKeyFrame(sdt, curFrame); |
70 | | - sdt.gotoFrame(keyFrame); |
71 | | - |
72 | | - Packet frame = sdt.nextFrame(); |
73 | | - ContainerAdaptor decoder = _detectDecoder(sdt, frame); |
74 | | - return decoder; |
75 | | - } |
76 | | - |
77 | | - public static FrameGrab8Bit createFrameGrab(SeekableByteChannel _in) throws IOException, JCodecException { |
| 52 | + public static FrameGrab8Bit createFrameGrab8Bit(SeekableByteChannel _in) throws IOException, JCodecException { |
| 53 | + FrameGrab8Bit fg = new FrameGrab8Bit(null, null); |
| 54 | + fg.buffers = new ThreadLocal<byte[][]>(); |
78 | 55 | ByteBuffer header = ByteBuffer.allocate(65536); |
79 | 56 | _in.read(header); |
80 | 57 | header.flip(); |
81 | 58 | Format detectFormat = JCodecUtil.detectFormatBuffer(header); |
82 | | - SeekableDemuxerTrack videoTrack = null; |
83 | 59 |
|
84 | 60 | switch (detectFormat) { |
85 | 61 | case MOV: |
86 | 62 | MP4Demuxer d1 = new MP4Demuxer(_in); |
87 | | - videoTrack = d1.getVideoTrack(); |
| 63 | + fg.videoTrack = d1.getVideoTrack(); |
88 | 64 | break; |
89 | 65 | case MPEG_PS: |
90 | 66 | throw new UnsupportedFormatException("MPEG PS is temporarily unsupported."); |
@@ -93,16 +69,11 @@ public static FrameGrab8Bit createFrameGrab(SeekableByteChannel _in) throws IOEx |
93 | 69 | default: |
94 | 70 | throw new UnsupportedFormatException("Container format is not supported by JCodec"); |
95 | 71 | } |
96 | | - ContainerAdaptor decoder = detectDecoder(videoTrack); |
97 | | - FrameGrab8Bit fg = new FrameGrab8Bit(videoTrack, decoder); |
| 72 | + fg.decodeLeadingFrames(); |
98 | 73 | return fg; |
99 | 74 | } |
100 | 75 |
|
101 | 76 | public FrameGrab8Bit(SeekableDemuxerTrack videoTrack, ContainerAdaptor decoder) { |
102 | | - if (decoder == null || videoTrack == null) { |
103 | | - throw new NullPointerException(); |
104 | | - } |
105 | | - this.buffers = new ThreadLocal<byte[][]>(); |
106 | 77 | this.videoTrack = videoTrack; |
107 | 78 | this.decoder = decoder; |
108 | 79 | } |
@@ -206,6 +177,8 @@ private void decodeLeadingFrames() throws IOException, JCodecException { |
206 | 177 | sdt.gotoFrame(keyFrame); |
207 | 178 |
|
208 | 179 | Packet frame = sdt.nextFrame(); |
| 180 | + if (decoder == null) |
| 181 | + decoder = detectDecoder(sdt, frame); |
209 | 182 |
|
210 | 183 | while (frame.getFrameNo() < curFrame) { |
211 | 184 | decoder.decodeFrame8Bit(frame, getBuffer()); |
@@ -236,6 +209,16 @@ private int detectKeyFrame(int start) throws IOException { |
236 | 209 | return prev; |
237 | 210 | } |
238 | 211 |
|
| 212 | + private ContainerAdaptor detectDecoder(SeekableDemuxerTrack videoTrack, Packet frame) throws JCodecException { |
| 213 | + DemuxerTrackMeta meta = videoTrack.getMeta(); |
| 214 | + switch (meta.getCodec()) { |
| 215 | + case H264: |
| 216 | + return new AVCMP4Adaptor(meta); |
| 217 | + default: |
| 218 | + throw new UnsupportedFormatException("Codec is not supported"); |
| 219 | + } |
| 220 | + } |
| 221 | + |
239 | 222 | /** |
240 | 223 | * Get frame at current position in JCodec native image |
241 | 224 | * |
@@ -266,26 +249,36 @@ public Picture8Bit getNativeFrame() throws IOException { |
266 | 249 | } |
267 | 250 |
|
268 | 251 | /** |
269 | | - * Gets info about the media |
| 252 | + * Get frame at a specified second as JCodec image |
270 | 253 | * |
| 254 | + * @param file |
| 255 | + * @param second |
271 | 256 | * @return |
| 257 | + * @throws IOException |
| 258 | + * @throws JCodecException |
272 | 259 | */ |
273 | | - public MediaInfo getMediaInfo() { |
274 | | - return decoder.getMediaInfo(); |
| 260 | + public static Picture8Bit getFrameAtSec(File file, double second) throws IOException, JCodecException { |
| 261 | + FileChannelWrapper ch = null; |
| 262 | + try { |
| 263 | + ch = NIOUtils.readableChannel(file); |
| 264 | + return createFrameGrab8Bit(ch).seekToSecondPrecise(second).getNativeFrame(); |
| 265 | + } finally { |
| 266 | + NIOUtils.closeQuietly(ch); |
| 267 | + } |
275 | 268 | } |
276 | 269 |
|
277 | 270 | /** |
278 | | - * Get frame at a specified frame number as JCodec image |
| 271 | + * Get frame at a specified second as JCodec image |
279 | 272 | * |
280 | 273 | * @param file |
281 | 274 | * @param second |
282 | 275 | * @return |
283 | 276 | * @throws IOException |
284 | 277 | * @throws JCodecException |
285 | 278 | */ |
286 | | - public static Picture8Bit getFrameFromChannel(SeekableByteChannel ch, int frameNumber) |
| 279 | + public static Picture8Bit getFrameFromChannelAtSec(SeekableByteChannel file, double second) |
287 | 280 | throws JCodecException, IOException { |
288 | | - return createFrameGrab(ch).seekToFramePrecise(frameNumber).getNativeFrame(); |
| 281 | + return createFrameGrab8Bit(file).seekToSecondPrecise(second).getNativeFrame(); |
289 | 282 | } |
290 | 283 |
|
291 | 284 | /** |
@@ -301,42 +294,94 @@ public static Picture8Bit getFrameFromFile(File file, int frameNumber) throws IO |
301 | 294 | FileChannelWrapper ch = null; |
302 | 295 | try { |
303 | 296 | ch = NIOUtils.readableChannel(file); |
304 | | - return createFrameGrab(ch).seekToFramePrecise(frameNumber).getNativeFrame(); |
| 297 | + return createFrameGrab8Bit(ch).seekToFramePrecise(frameNumber).getNativeFrame(); |
305 | 298 | } finally { |
306 | 299 | NIOUtils.closeQuietly(ch); |
307 | 300 | } |
308 | 301 | } |
309 | 302 |
|
310 | 303 | /** |
311 | | - * Get frame at a specified second as JCodec image |
| 304 | + * Get frame at a specified frame number as JCodec image |
312 | 305 | * |
313 | | - * @param ch |
| 306 | + * @param file |
314 | 307 | * @param second |
315 | 308 | * @return |
316 | 309 | * @throws IOException |
317 | 310 | * @throws JCodecException |
318 | 311 | */ |
319 | | - public static Picture8Bit getFrameAtSecFromChannel(SeekableByteChannel ch, double second) |
| 312 | + public static Picture8Bit getFrameFromChannel(SeekableByteChannel file, int frameNumber) |
320 | 313 | throws JCodecException, IOException { |
321 | | - return createFrameGrab(ch).seekToSecondPrecise(second).getNativeFrame(); |
| 314 | + return createFrameGrab8Bit(file).seekToFramePrecise(frameNumber).getNativeFrame(); |
322 | 315 | } |
323 | 316 |
|
324 | 317 | /** |
325 | | - * Get frame at a specified second as JCodec image |
| 318 | + * Get a specified frame by number from an already open demuxer track |
326 | 319 | * |
327 | | - * @param file |
328 | | - * @param second |
| 320 | + * @param vt |
| 321 | + * @param decoder |
| 322 | + * @param frameNumber |
329 | 323 | * @return |
330 | 324 | * @throws IOException |
331 | 325 | * @throws JCodecException |
332 | 326 | */ |
333 | | - public static Picture8Bit getFrameAtSec(File file, double second) throws IOException, JCodecException { |
334 | | - FileChannelWrapper ch = null; |
335 | | - try { |
336 | | - ch = NIOUtils.readableChannel(file); |
337 | | - return createFrameGrab(ch).seekToSecondPrecise(second).getNativeFrame(); |
338 | | - } finally { |
339 | | - NIOUtils.closeQuietly(ch); |
340 | | - } |
| 327 | + public static Picture8Bit getNativeFrame(SeekableDemuxerTrack vt, ContainerAdaptor decoder, int frameNumber) |
| 328 | + throws IOException, JCodecException { |
| 329 | + return new FrameGrab8Bit(vt, decoder).seekToFramePrecise(frameNumber).getNativeFrame(); |
| 330 | + } |
| 331 | + |
| 332 | + /** |
| 333 | + * Get a specified frame by second from an already open demuxer track |
| 334 | + * |
| 335 | + * @param vt |
| 336 | + * @param decoder |
| 337 | + * @param frameNumber |
| 338 | + * @return |
| 339 | + * @throws IOException |
| 340 | + * @throws JCodecException |
| 341 | + */ |
| 342 | + public static Picture8Bit getNativeFrameAtSec(SeekableDemuxerTrack vt, ContainerAdaptor decoder, double second) |
| 343 | + throws IOException, JCodecException { |
| 344 | + return new FrameGrab8Bit(vt, decoder).seekToSecondPrecise(second).getNativeFrame(); |
| 345 | + } |
| 346 | + |
| 347 | + /** |
| 348 | + * Get a specified frame by number from an already open demuxer track ( |
| 349 | + * sloppy mode, i.e. nearest keyframe ) |
| 350 | + * |
| 351 | + * @param vt |
| 352 | + * @param decoder |
| 353 | + * @param frameNumber |
| 354 | + * @return |
| 355 | + * @throws IOException |
| 356 | + * @throws JCodecException |
| 357 | + */ |
| 358 | + public static Picture8Bit getNativeFrameSloppy(SeekableDemuxerTrack vt, ContainerAdaptor decoder, int frameNumber) |
| 359 | + throws IOException, JCodecException { |
| 360 | + return new FrameGrab8Bit(vt, decoder).seekToFrameSloppy(frameNumber).getNativeFrame(); |
| 361 | + } |
| 362 | + |
| 363 | + /** |
| 364 | + * Get a specified frame by second from an already open demuxer track ( |
| 365 | + * sloppy mode, i.e. nearest keyframe ) |
| 366 | + * |
| 367 | + * @param vt |
| 368 | + * @param decoder |
| 369 | + * @param frameNumber |
| 370 | + * @return |
| 371 | + * @throws IOException |
| 372 | + * @throws JCodecException |
| 373 | + */ |
| 374 | + public static Picture8Bit getNativeFrameAtSecSloppy(SeekableDemuxerTrack vt, ContainerAdaptor decoder, double second) |
| 375 | + throws IOException, JCodecException { |
| 376 | + return new FrameGrab8Bit(vt, decoder).seekToSecondSloppy(second).getNativeFrame(); |
| 377 | + } |
| 378 | + |
| 379 | + /** |
| 380 | + * Gets info about the media |
| 381 | + * |
| 382 | + * @return |
| 383 | + */ |
| 384 | + public MediaInfo getMediaInfo() { |
| 385 | + return decoder.getMediaInfo(); |
341 | 386 | } |
342 | 387 | } |