Skip to content

Commit

Permalink
Modularize presets for OpenCV and FFmpeg (pull #681)
Browse files Browse the repository at this point in the history
  • Loading branch information
HGuillemet authored and saudet committed Mar 4, 2019
1 parent 17a2345 commit 94f52b3
Show file tree
Hide file tree
Showing 971 changed files with 80,799 additions and 59,947 deletions.
6 changes: 5 additions & 1 deletion ci/install-ppc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec 'echo "deb [arch=ppc64el] ht
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec 'echo "deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports trusty-updates main restricted universe multiverse" >> /etc/apt/sources.list'
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec 'echo "deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports trusty-backports main restricted universe multiverse" >> /etc/apt/sources.list'
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec 'echo "deb [arch=ppc64el] http://ports.ubuntu.com/ubuntu-ports trusty-security main restricted universe multiverse" >> /etc/apt/sources.list'
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec 'echo "deb [arch=amd64] http://ppa.launchpad.net/openjdk-r/ppa/ubuntu trusty main" >> /etc/apt/sources.list'
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "sed -i 's/deb http/deb [arch=i386,amd64] http/g' /etc/apt/sources.list"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EB9B1D8886F44E2A"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "apt-get update"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "apt-get -y install python python2.7 python-minimal python2.7-minimal libgtk2.0-dev:ppc64el libasound2-dev:ppc64el libusb-dev:ppc64el libusb-1.0-0-dev:ppc64el zlib1g-dev:ppc64el libxcb1-dev:ppc64el"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "apt-get -y install pkg-config ccache gcc-powerpc64le-linux-gnu g++-powerpc64le-linux-gnu gfortran-powerpc64le-linux-gnu linux-libc-dev-ppc64el-cross binutils-multiarch default-jdk ant python python-dev python-numpy swig git file wget unzip tar bzip2 patch autoconf-archive autogen automake make libtool perl nasm yasm curl cmake3"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "apt-get -y install pkg-config ccache gcc-powerpc64le-linux-gnu g++-powerpc64le-linux-gnu gfortran-powerpc64le-linux-gnu linux-libc-dev-ppc64el-cross binutils-multiarch openjdk-7-jdk-headless openjdk-8-jdk-headless ant python python-dev python-numpy swig git file wget unzip tar bzip2 patch autoconf-archive autogen automake make libtool perl nasm yasm curl cmake3"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "apt-get -y remove libxdmcp-dev libx11-dev libxcb1-dev libxt-dev"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "echo 2 | update-alternatives --config java"
docker exec -ti $DOCKER_CONTAINER_ID /bin/bash -xec "echo 2 | update-alternatives --config javac"

if [[ "$PROJ" =~ cuda ]]; then
echo "Setting up for cuda build"
Expand Down
27 changes: 17 additions & 10 deletions ffmpeg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Here is a simple example of FFmpeg ported to Java from this C source file:

* https://github.com/monday0rsunday/ffmpeg-tutorial/blob/master/002_read_few_frame.c

We can use [Maven 3](http://maven.apache.org/) to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the `pom.xml` and `src/main/java/ReadFewFrame.java` source files below, simply execute on the command line:
We can use [Maven 3](http://maven.apache.org/) to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the `pom.xml` and `ReadFewFrame.java` source files below, simply execute on the command line:
```bash
$ mvn compile exec:java -Dexec.args="myvideofile.mpg"
```
Expand All @@ -32,30 +32,37 @@ We can use [Maven 3](http://maven.apache.org/) to download and install automatic
```xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.bytedeco.javacpp-presets.ffmpeg</groupId>
<groupId>org.bytedeco.ffmpeg</groupId>
<artifactId>readfewframe</artifactId>
<version>1.4.5-SNAPSHOT</version>
<version>1.5-SNAPSHOT</version>
<properties>
<exec.mainClass>ReadFewFrame</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.1.1-1.4.5-SNAPSHOT</version>
<version>4.1.1-1.5-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>.</sourceDirectory>
</build>
</project>
```

### The `src/main/java/ReadFewFrame.java` source file
### The `ReadFewFrame.java` source file
```java
import java.io.*;
import org.bytedeco.javacpp.*;
import static org.bytedeco.javacpp.avcodec.*;
import static org.bytedeco.javacpp.avformat.*;
import static org.bytedeco.javacpp.avutil.*;
import static org.bytedeco.javacpp.swscale.*;
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 {
/**
Expand Down
2 changes: 1 addition & 1 deletion ffmpeg/platform/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<relativePath>../../</relativePath>
</parent>

<groupId>org.bytedeco.javacpp-presets</groupId>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg-platform</artifactId>
<version>4.1.1-${project.parent.version}</version>
<name>JavaCPP Presets Platform for FFmpeg</name>
Expand Down
6 changes: 5 additions & 1 deletion ffmpeg/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>1.5-SNAPSHOT</version>
</parent>

<groupId>org.bytedeco.javacpp-presets</groupId>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>4.1.1-${project.parent.version}</version>
<name>JavaCPP Presets for FFmpeg</name>
Expand All @@ -36,6 +36,10 @@
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.moditect</groupId>
<artifactId>moditect-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
Expand Down
166 changes: 166 additions & 0 deletions ffmpeg/samples/ReadFewFrame.java
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);
}
}
19 changes: 19 additions & 0 deletions ffmpeg/samples/pom.xml
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 ffmpeg/src/gen/java/org/bytedeco/ffmpeg/avcodec/AVBSFContext.java
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);
}

0 comments on commit 94f52b3

Please sign in to comment.