-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modularize presets for OpenCV and FFmpeg (pull #681)
- Loading branch information
1 parent
e4817b5
commit 0c650e3
Showing
971 changed files
with
80,798 additions
and
59,946 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import java.io.*; | ||
import org.bytedeco.javacpp.*; | ||
import org.bytedeco.ffmpeg.avcodec.*; | ||
import org.bytedeco.ffmpeg.avformat.*; | ||
import org.bytedeco.ffmpeg.avutil.*; | ||
import org.bytedeco.ffmpeg.swscale.*; | ||
import static org.bytedeco.ffmpeg.global.avcodec.*; | ||
import static org.bytedeco.ffmpeg.global.avformat.*; | ||
import static org.bytedeco.ffmpeg.global.avutil.*; | ||
import static org.bytedeco.ffmpeg.global.swscale.*; | ||
|
||
public class ReadFewFrame { | ||
/** | ||
* Write image data using simple image format ppm | ||
* @see https://en.wikipedia.org/wiki/Netpbm_format | ||
*/ | ||
static void save_frame(AVFrame pFrame, int width, int height, int f_idx) throws IOException { | ||
// Open file | ||
String szFilename = String.format("frame%d_.ppm", f_idx); | ||
OutputStream pFile = new FileOutputStream(szFilename); | ||
|
||
// Write header | ||
pFile.write(String.format("P6\n%d %d\n255\n", width, height).getBytes()); | ||
|
||
// Write pixel data | ||
BytePointer data = pFrame.data(0); | ||
byte[] bytes = new byte[width * 3]; | ||
int l = pFrame.linesize(0); | ||
for(int y = 0; y < height; y++) { | ||
data.position(y * l).get(bytes); | ||
pFile.write(bytes); | ||
} | ||
|
||
// Close file | ||
pFile.close(); | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
System.out.println("Read few frame and write to image"); | ||
if (args.length < 1) { | ||
System.out.println("Missing input video file"); | ||
System.exit(-1); | ||
} | ||
int ret = -1, i = 0, v_stream_idx = -1; | ||
String vf_path = args[0]; | ||
AVFormatContext fmt_ctx = new AVFormatContext(null); | ||
AVPacket pkt = new AVPacket(); | ||
|
||
ret = avformat_open_input(fmt_ctx, vf_path, null, null); | ||
if (ret < 0) { | ||
System.out.printf("Open video file %s failed \n", vf_path); | ||
throw new IllegalStateException(); | ||
} | ||
|
||
// i dont know but without this function, sws_getContext does not work | ||
if (avformat_find_stream_info(fmt_ctx, (PointerPointer)null) < 0) { | ||
System.exit(-1); | ||
} | ||
|
||
av_dump_format(fmt_ctx, 0, args[0], 0); | ||
|
||
for (i = 0; i < fmt_ctx.nb_streams(); i++) { | ||
if (fmt_ctx.streams(i).codecpar().codec_type() == AVMEDIA_TYPE_VIDEO) { | ||
v_stream_idx = i; | ||
break; | ||
} | ||
} | ||
if (v_stream_idx == -1) { | ||
System.out.println("Cannot find video stream"); | ||
throw new IllegalStateException(); | ||
} else { | ||
System.out.printf("Video stream %d with resolution %dx%d\n", v_stream_idx, | ||
fmt_ctx.streams(i).codecpar().width(), | ||
fmt_ctx.streams(i).codecpar().height()); | ||
} | ||
|
||
AVCodecContext codec_ctx = avcodec_alloc_context3(null); | ||
avcodec_parameters_to_context(codec_ctx, fmt_ctx.streams(v_stream_idx).codecpar()); | ||
|
||
AVCodec codec = avcodec_find_decoder(codec_ctx.codec_id()); | ||
if (codec == null) { | ||
System.out.println("Unsupported codec for video file"); | ||
throw new IllegalStateException(); | ||
} | ||
ret = avcodec_open2(codec_ctx, codec, (PointerPointer)null); | ||
if (ret < 0) { | ||
System.out.println("Can not open codec"); | ||
throw new IllegalStateException(); | ||
} | ||
|
||
AVFrame frm = av_frame_alloc(); | ||
|
||
// Allocate an AVFrame structure | ||
AVFrame pFrameRGB = av_frame_alloc(); | ||
if (pFrameRGB == null) { | ||
System.exit(-1); | ||
} | ||
|
||
// Determine required buffer size and allocate buffer | ||
int numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, codec_ctx.width(), | ||
codec_ctx.height(), 1); | ||
BytePointer buffer = new BytePointer(av_malloc(numBytes)); | ||
|
||
SwsContext sws_ctx = sws_getContext( | ||
codec_ctx.width(), | ||
codec_ctx.height(), | ||
codec_ctx.pix_fmt(), | ||
codec_ctx.width(), | ||
codec_ctx.height(), | ||
AV_PIX_FMT_RGB24, | ||
SWS_BILINEAR, | ||
null, | ||
null, | ||
(DoublePointer)null | ||
); | ||
|
||
if (sws_ctx == null) { | ||
System.out.println("Can not use sws"); | ||
throw new IllegalStateException(); | ||
} | ||
|
||
av_image_fill_arrays(pFrameRGB.data(), pFrameRGB.linesize(), | ||
buffer, AV_PIX_FMT_RGB24, codec_ctx.width(), codec_ctx.height(), 1); | ||
|
||
i = 0; | ||
int ret1 = -1, ret2 = -1, fi = -1; | ||
while (av_read_frame(fmt_ctx, pkt) >= 0) { | ||
if (pkt.stream_index() == v_stream_idx) { | ||
ret1 = avcodec_send_packet(codec_ctx, pkt); | ||
ret2 = avcodec_receive_frame(codec_ctx, frm); | ||
System.out.printf("ret1 %d ret2 %d\n", ret1, ret2); | ||
// avcodec_decode_video2(codec_ctx, frm, fi, pkt); | ||
} | ||
// if not check ret2, error occur [swscaler @ 0x1cb3c40] bad src image pointers | ||
// ret2 same as fi | ||
// if (fi && ++i <= 5) { | ||
if (ret2 >= 0 && ++i <= 5) { | ||
sws_scale( | ||
sws_ctx, | ||
frm.data(), | ||
frm.linesize(), | ||
0, | ||
codec_ctx.height(), | ||
pFrameRGB.data(), | ||
pFrameRGB.linesize() | ||
); | ||
|
||
save_frame(pFrameRGB, codec_ctx.width(), codec_ctx.height(), i); | ||
// save_frame(frm, codec_ctx.width(), codec_ctx.height(), i); | ||
} | ||
av_packet_unref(pkt); | ||
if (i >= 5) { | ||
break; | ||
} | ||
} | ||
|
||
av_frame_free(frm); | ||
|
||
avcodec_close(codec_ctx); | ||
avcodec_free_context(codec_ctx); | ||
|
||
avformat_close_input(fmt_ctx); | ||
System.out.println("Shutdown"); | ||
System.exit(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.bytedeco.ffmpeg</groupId> | ||
<artifactId>readfewframe</artifactId> | ||
<version>1.5-SNAPSHOT</version> | ||
<properties> | ||
<exec.mainClass>ReadFewFrame</exec.mainClass> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.bytedeco</groupId> | ||
<artifactId>ffmpeg-platform</artifactId> | ||
<version>4.1-1.5-SNAPSHOT</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<sourceDirectory>.</sourceDirectory> | ||
</build> | ||
</project> |
88 changes: 88 additions & 0 deletions
88
ffmpeg/src/gen/java/org/bytedeco/ffmpeg/avcodec/AVBSFContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Targeted by JavaCPP version 1.5-SNAPSHOT: DO NOT EDIT THIS FILE | ||
|
||
package org.bytedeco.ffmpeg.avcodec; | ||
|
||
import java.nio.*; | ||
import org.bytedeco.javacpp.*; | ||
import org.bytedeco.javacpp.annotation.*; | ||
|
||
import org.bytedeco.ffmpeg.avutil.*; | ||
import static org.bytedeco.ffmpeg.global.avutil.*; | ||
import org.bytedeco.ffmpeg.swresample.*; | ||
import static org.bytedeco.ffmpeg.global.swresample.*; | ||
|
||
import static org.bytedeco.ffmpeg.global.avcodec.*; | ||
|
||
|
||
/** | ||
* The bitstream filter state. | ||
* | ||
* This struct must be allocated with av_bsf_alloc() and freed with | ||
* av_bsf_free(). | ||
* | ||
* The fields in the struct will only be changed (by the caller or by the | ||
* filter) as described in their documentation, and are to be considered | ||
* immutable otherwise. | ||
*/ | ||
@Properties(inherit = org.bytedeco.ffmpeg.presets.avcodec.class) | ||
public class AVBSFContext extends Pointer { | ||
static { Loader.load(); } | ||
/** Default native constructor. */ | ||
public AVBSFContext() { super((Pointer)null); allocate(); } | ||
/** Native array allocator. Access with {@link Pointer#position(long)}. */ | ||
public AVBSFContext(long size) { super((Pointer)null); allocateArray(size); } | ||
/** Pointer cast constructor. Invokes {@link Pointer#Pointer(Pointer)}. */ | ||
public AVBSFContext(Pointer p) { super(p); } | ||
private native void allocate(); | ||
private native void allocateArray(long size); | ||
@Override public AVBSFContext position(long position) { | ||
return (AVBSFContext)super.position(position); | ||
} | ||
|
||
/** | ||
* A class for logging and AVOptions | ||
*/ | ||
@MemberGetter public native @Const AVClass av_class(); | ||
|
||
/** | ||
* The bitstream filter this context is an instance of. | ||
*/ | ||
@MemberGetter public native @Const AVBitStreamFilter filter(); | ||
|
||
/** | ||
* Opaque libavcodec internal data. Must not be touched by the caller in any | ||
* way. | ||
*/ | ||
public native AVBSFInternal internal(); public native AVBSFContext internal(AVBSFInternal internal); | ||
|
||
/** | ||
* Opaque filter-specific private data. If filter->priv_class is non-NULL, | ||
* this is an AVOptions-enabled struct. | ||
*/ | ||
public native Pointer priv_data(); public native AVBSFContext priv_data(Pointer priv_data); | ||
|
||
/** | ||
* Parameters of the input stream. This field is allocated in | ||
* av_bsf_alloc(), it needs to be filled by the caller before | ||
* av_bsf_init(). | ||
*/ | ||
public native AVCodecParameters par_in(); public native AVBSFContext par_in(AVCodecParameters par_in); | ||
|
||
/** | ||
* Parameters of the output stream. This field is allocated in | ||
* av_bsf_alloc(), it is set by the filter in av_bsf_init(). | ||
*/ | ||
public native AVCodecParameters par_out(); public native AVBSFContext par_out(AVCodecParameters par_out); | ||
|
||
/** | ||
* The timebase used for the timestamps of the input packets. Set by the | ||
* caller before av_bsf_init(). | ||
*/ | ||
public native @ByRef AVRational time_base_in(); public native AVBSFContext time_base_in(AVRational time_base_in); | ||
|
||
/** | ||
* The timebase used for the timestamps of the output packets. Set by the | ||
* filter in av_bsf_init(). | ||
*/ | ||
public native @ByRef AVRational time_base_out(); public native AVBSFContext time_base_out(AVRational time_base_out); | ||
} |
Oops, something went wrong.