Add support for i.MX6 VPU

This commit is contained in:
Iwan Timmer 2014-08-12 14:29:47 +00:00
parent 09f3f851fd
commit 4c27ff8090
8 changed files with 508 additions and 2 deletions

View File

@ -9,6 +9,7 @@
<property name="libs.opus.dir" location="${basedir}/jni/nv_opus_dec/"/>
<property name="libs.omx.dir" location="${basedir}/jni/nv_omx_dec/"/>
<property name="libs.imx.dir" location="${basedir}/jni/nv_imx_dec/"/>
<property name="libs.alsa.dir" location="${basedir}/jni/nv_alsa/"/>
<property name="libs.io.dir" location="${basedir}/jni/nv_io/"/>
@ -36,6 +37,12 @@
<exec executable="make" dir="${libs.omx.dir}">
<arg line="clean"/>
</exec>
<exec executable="make" dir="${libs.imx.dir}">
<arg line="clean"/>
</exec>
<exec executable="make" dir="${libs.imx.dir}">
<arg line="clean"/>
</exec>
<exec executable="make" dir="${libs.opus.dir}">
<arg line="clean"/>
</exec>
@ -53,6 +60,7 @@
<target name="compile-native">
<exec executable="make" dir="${libs.omx.dir}"/>
<exec executable="make" dir="${libs.imx.dir}"/>
<exec executable="make" dir="${libs.opus.dir}"/>
<exec executable="make" dir="${libs.alsa.dir}"/>
<exec executable="make" dir="${libs.io.dir}"/>
@ -101,6 +109,7 @@
<binlib>
<fileset dir="${libs.opus.dir}" includes="*.so"/>
<fileset dir="${libs.omx.dir}" includes="*.so"/>
<fileset dir="${libs.imx.dir}" includes="*.so"/>
<fileset dir="${libs.alsa.dir}" includes="*.so"/>
<fileset dir="${libs.io.dir}" includes="*.so"/>
</binlib>

17
jni/nv_imx_dec/makefile Normal file
View File

@ -0,0 +1,17 @@
CFLAGS = -fPIC -I ${JAVA_HOME}/include -I ${JAVA_HOME}/include/linux -I /opt/fsl/include/
LDFLAGS = -shared -Wl,--no-undefined -L. -L /opt/fsl/lib/ -lvpu
SRCS = nv_imx_dec.c nv_imx_dec_jni.c
TARGET_LIB = libnv_imx_dec.so
OBJS = $(SRCS:.c=.o)
$(TARGET_LIB): $(OBJS)
$(CC) ${LDFLAGS} -o $@ $^
all: $(TARGET_LIB)
clean:
rm -f ${TARGET_LIB} ${OBJS} $(SRCS:.c=.d)
.PHONY: all clean

381
jni/nv_imx_dec/nv_imx_dec.c Normal file
View File

@ -0,0 +1,381 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <libv4l2.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>
#include <linux/mxc_v4l2.h>
#include <linux/mxcfb.h>
#include <linux/v4l2-common.h>
#include <linux/v4l2-controls.h>
#include <linux/videodev2.h>
#include "vpu_io.h"
#include "vpu_lib.h"
#define STREAM_BUF_SIZE 0x200000
#define PS_SAVE_SIZE 0x080000
#define MODE420 1
#define MODE422 2
#define MODE224 3
struct v4l_buf {
void *start;
off_t offset;
size_t length;
};
vpu_mem_desc mem_desc = {0};
vpu_mem_desc ps_mem_desc = {0};
vpu_mem_desc slice_mem_desc = {0};
DecHandle handle = {0};
DecParam decparam = {0};
int fd;
int initialized = 0, decoding = 0;
int loop_id = 0;
int threshold;
int queued_count = 0;
int ncount = 0;
int disp_clr_index = 0;
FrameBuffer *fb;
struct v4l2_buffer dbuf;
int nv_imx_init(void) {
struct mxcfb_gbl_alpha alpha;
dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
dbuf.memory = V4L2_MEMORY_MMAP;
int fd_fb = open("/dev/fb0", O_RDWR, 0);
if (fd_fb < 0) {
return -1;
}
alpha.alpha = 0;
alpha.enable = 1;
if (ioctl(fd_fb, MXCFB_SET_GBL_ALPHA, &alpha) < 0) {
return -2;
}
close(fd_fb);
if (vpu_Init(NULL) != RETCODE_SUCCESS)
return -3;
mem_desc.size = STREAM_BUF_SIZE;
if (IOGetPhyMem(&mem_desc)) {
return -4;
}
if (IOGetVirtMem(&mem_desc) <= 0) {
return -5;
}
ps_mem_desc.size = PS_SAVE_SIZE;
if (IOGetPhyMem(&ps_mem_desc)) {
return -6;
}
DecOpenParam oparam = {0};
oparam.bitstreamFormat = STD_AVC;
oparam.bitstreamBuffer = mem_desc.phy_addr;
oparam.bitstreamBufferSize = STREAM_BUF_SIZE;
oparam.pBitStream = (Uint8 *) mem_desc.virt_uaddr;
oparam.reorderEnable = 1;
oparam.mp4DeblkEnable = 0;
oparam.chromaInterleave = 0;
oparam.avcExtension = oparam.mp4Class = 0;
oparam.mjpg_thumbNailDecEnable = 0;
oparam.mapType = LINEAR_FRAME_MAP;
oparam.tiled2LinearEnable = 0;
oparam.bitstreamMode = 1;
oparam.psSaveBuffer = ps_mem_desc.phy_addr;
oparam.psSaveBufferSize = PS_SAVE_SIZE;
if (vpu_DecOpen(&handle, &oparam) != RETCODE_SUCCESS) {
return -7;
}
decparam.dispReorderBuf = 0;
decparam.skipframeMode = 0;
decparam.skipframeNum = 0;
decparam.iframeSearchEnable = 0;
return 0;
}
int nv_imx_decode(const unsigned char* indata, int data_len, int last) {
//Write information
Uint32 target_addr, space;
PhysicalAddress pa_read_ptr, pa_write_ptr;
RetCode ret;
int room;
size_t read;
//Initialize
DecInitialInfo initinfo = {0};
DecBufInfo bufinfo;
//Display using v4l
int type;
struct timeval tv;
if (vpu_DecGetBitstreamBuffer(handle, &pa_read_ptr, &pa_write_ptr, &space) != RETCODE_SUCCESS) {
return -1;
}
target_addr = mem_desc.virt_uaddr + (pa_write_ptr - mem_desc.phy_addr);
if ( (target_addr + data_len) > mem_desc.virt_uaddr + STREAM_BUF_SIZE) {
room = mem_desc.virt_uaddr + STREAM_BUF_SIZE - target_addr;
memcpy((void *)target_addr, indata, room);
memcpy((void *)mem_desc.virt_uaddr, indata + room, data_len - room);
} else {
memcpy((void *)target_addr, indata, data_len);
}
vpu_DecUpdateBitstreamBuffer(handle, data_len);
if (last) {
if (!initialized) {
initialized = 1;
vpu_DecSetEscSeqInit(handle, 1);
if (vpu_DecGetInitialInfo(handle, &initinfo) != RETCODE_SUCCESS) {
return -2;
}
vpu_DecSetEscSeqInit(handle, 0);
int regfbcount = initinfo.minFrameBufferCount + 2;
threshold = regfbcount - initinfo.minFrameBufferCount;
int picWidth = ((initinfo.picWidth + 15) & ~15);
int picHeight = ((initinfo.picHeight + 15) & ~15);
int stride = picWidth;
int phy_slicebuf_size = initinfo.worstSliceSize * 1024;
slice_mem_desc.size = phy_slicebuf_size;
if (IOGetPhyMem(&slice_mem_desc)) {
return -3;
}
fb = calloc(regfbcount, sizeof(FrameBuffer));
if (fb == NULL) {
return -4;
}
char v4l_device[16], node[8];
sprintf(node, "%d", 17);
strcpy(v4l_device, "/dev/video");
strcat(v4l_device, node);
fd = open(v4l_device, O_RDWR, 0);
struct v4l2_format fmt = {0};
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
fmt.fmt.pix.width = picWidth;
fmt.fmt.pix.height = picHeight;
fmt.fmt.pix.bytesperline = picWidth;
fmt.fmt.pix.field = V4L2_FIELD_ANY;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
if (ioctl(fd, VIDIOC_S_FMT, &fmt) < 0) {
return -5;
}
if (ioctl(fd, VIDIOC_G_FMT, &fmt) < 0) {
return -6;
}
struct v4l2_requestbuffers reqbuf = {0};
reqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
reqbuf.memory = V4L2_MEMORY_MMAP;
reqbuf.count = regfbcount;
struct v4l_buf* buffers[regfbcount];
if (ioctl(fd, VIDIOC_REQBUFS, &reqbuf) < 0) {
return -7;
}
if (reqbuf.count < regfbcount) {
return -8;
}
int i;
for (i = 0; i < regfbcount; i++) {
struct v4l2_buffer buffer = {0};
struct v4l_buf *buf;
buf = calloc(1, sizeof(struct v4l_buf));
if (buf == NULL) {
return -9;
}
buffers[i] = buf;
buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
buffer.memory = V4L2_MEMORY_MMAP;
buffer.index = i;
if (ioctl(fd, VIDIOC_QUERYBUF, &buffer) < 0) {
return -10;
}
buf->start = mmap(NULL, buffer.length, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, buffer.m.offset);
/*
* Workaround for new V4L interface change, this change
* will be removed after V4L driver is updated for this.
* Need to call QUERYBUF ioctl again after mmap.
*/
if (ioctl(fd, VIDIOC_QUERYBUF, &buffer) < 0) {
return -11;
}
buf->offset = buffer.m.offset;
buf->length = buffer.length;
if (buf->start == MAP_FAILED) {
return -12;
}
}
int img_size = stride * picHeight;
vpu_mem_desc *mvcol_md = NULL;
int mjpg_fmt = MODE420;
int divX = (mjpg_fmt == MODE420 || mjpg_fmt == MODE422) ? 2 : 1;
int divY = (mjpg_fmt == MODE420 || mjpg_fmt == MODE224) ? 2 : 1;
mvcol_md = calloc(regfbcount, sizeof(vpu_mem_desc));
for (i = 0; i < regfbcount; i++) {
fb[i].myIndex = i;
fb[i].bufY = buffers[i]->offset;
fb[i].bufCb = fb[i].bufY + img_size;
fb[i].bufCr = fb[i].bufCb + (img_size / divX / divY);
/* allocate MvCol buffer here */
memset(&mvcol_md[i], 0, sizeof(vpu_mem_desc));
mvcol_md[i].size = img_size / divX / divY;
if (IOGetPhyMem(&mvcol_md[i])) {
return -13;
}
fb[i].bufMvCol = mvcol_md[i].phy_addr;
}
bufinfo.avcSliceBufInfo.bufferBase = slice_mem_desc.phy_addr;
bufinfo.avcSliceBufInfo.bufferSize = phy_slicebuf_size;
bufinfo.maxDecFrmInfo.maxMbX = stride / 16;
bufinfo.maxDecFrmInfo.maxMbY = picHeight / 16;
bufinfo.maxDecFrmInfo.maxMbNum = stride * picHeight / 256;
int delay = -1;
vpu_DecGiveCommand(handle, DEC_SET_FRAME_DELAY, &delay);
if (vpu_DecRegisterFrameBuffer(handle, fb, regfbcount, stride, &bufinfo) != RETCODE_SUCCESS) {
return -14;
}
}
if (!decoding) {
if (vpu_DecStartOneFrame(handle, &decparam) != RETCODE_SUCCESS) {
return -1;
}
decoding = 15;
}
}
while (vpu_IsBusy()) {
if (loop_id > 50) {
vpu_SWReset(handle, 0);
return -16;
}
vpu_WaitForInt(100);
loop_id++;
}
loop_id = 0;
if (decoding) {
decoding = 0;
DecOutputInfo outinfo = {0};
if (vpu_DecGetOutputInfo(handle, &outinfo) != RETCODE_SUCCESS) {
return -17;
}
if (outinfo.decodingSuccess & 0x10) {
return 0;
} else if (outinfo.notSufficientPsBuffer) {
return -19;
} else if (outinfo.notSufficientSliceBuffer) {
return -20;
}
if (outinfo.indexFrameDisplay >= 0) {
gettimeofday(&tv, 0);
dbuf.timestamp.tv_sec = tv.tv_sec;
dbuf.timestamp.tv_usec = tv.tv_usec;
dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
dbuf.memory = V4L2_MEMORY_MMAP;
dbuf.index = outinfo.indexFrameDisplay;
if (ioctl(fd, VIDIOC_QUERYBUF, &dbuf) < 0) {
return -21;
}
dbuf.index = outinfo.indexFrameDisplay;
dbuf.field = V4L2_FIELD_NONE;
if (ioctl(fd, VIDIOC_QBUF, &dbuf) < 0) {
return -22;
}
if (ncount == 1) {
type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
if (ioctl(fd, VIDIOC_STREAMON, &type) < 0) {
return -23;
}
}
ncount++;
queued_count++;
if (queued_count > threshold) {
dbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
dbuf.memory = V4L2_MEMORY_MMAP;
if (ioctl(fd, VIDIOC_DQBUF, &dbuf) < 0) {
return -24;
} else
queued_count--;
} else
dbuf.index = -1;
if (disp_clr_index >= 0)
vpu_DecClrDispFlag(handle, disp_clr_index);
disp_clr_index = outinfo.indexFrameDisplay;
} else if (outinfo.indexFrameDisplay == -1)
return -25;
}
return 0;
}
void nv_imx_destroy(void) {
IOFreePhyMem(&ps_mem_desc);
IOFreePhyMem(&slice_mem_desc);
IOFreeVirtMem(&mem_desc);
IOFreePhyMem(&mem_desc);
vpu_UnInit();
}

View File

@ -0,0 +1,7 @@
#include <jni.h>
int nv_imx_init(void);
int nv_imx_decode(unsigned char* indata, int inlen, int last);
void nv_imx_destroy(void);

40
jni/nv_imx_dec/nv_imx_dec_jni.c Executable file
View File

@ -0,0 +1,40 @@
#include "nv_imx_dec.h"
#include <stdlib.h>
#include <jni.h>
// This function must be called before
// any other decoding functions
JNIEXPORT jint JNICALL
Java_com_limelight_binding_video_ImxDecoder_init(JNIEnv *env, jobject this)
{
return nv_imx_init();
}
// This function must be called after
// decoding is finished
JNIEXPORT void JNICALL
Java_com_limelight_binding_video_ImxDecoder_destroy(JNIEnv *env, jobject this) {
nv_imx_destroy();
}
// packets must be decoded in order
// the input buffer must have proper padding
// returns 0 on success, < 0 on error
JNIEXPORT jint JNICALL
Java_com_limelight_binding_video_ImxDecoder_decode(
JNIEnv *env, jobject this, // JNI parameters
jbyteArray indata, jint inoff, jint inlen, jboolean last)
{
jint ret;
jbyte* jni_input_data;
jni_input_data = (*env)->GetByteArrayElements(env, indata, 0);
ret = nv_imx_decode(&jni_input_data[inoff], inlen, last);
// The input data isn't changed so it can be safely aborted
(*env)->ReleaseByteArrayElements(env, indata, jni_input_data, JNI_ABORT);
return ret;
}

View File

@ -4,7 +4,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import com.limelight.binding.audio.AlsaAudioRenderer;
import com.limelight.binding.video.OmxDecoderRenderer;
import com.limelight.binding.video.ImxDecoderRenderer;
import com.limelight.binding.crypto.PcCryptoProvider;
import com.limelight.nvstream.av.audio.AudioRenderer;
import com.limelight.nvstream.av.video.VideoDecoderRenderer;
@ -21,7 +21,7 @@ public class PlatformBinding {
* @return a video decoder and renderer
*/
public static VideoDecoderRenderer getVideoDecoderRenderer() {
return new OmxDecoderRenderer();
return new ImxDecoderRenderer();
}
/**

View File

@ -0,0 +1,17 @@
package com.limelight.binding.video;
/**
* JNI Decoder bindings
* @author Iwan Timmer
*/
public class ImxDecoder {
static {
System.loadLibrary("nv_imx_dec");
}
public static native int init();
public static native void destroy();
public static native int decode(byte[] indata, int inoff, int inlen, boolean last);
}

View File

@ -0,0 +1,35 @@
package com.limelight.binding.video;
import com.limelight.nvstream.av.ByteBufferDescriptor;
import com.limelight.nvstream.av.DecodeUnit;
import java.util.List;
/**
* Implementation of a video decoder and renderer.
* @author Iwan Timmer
*/
public class ImxDecoderRenderer extends AbstractVideoRenderer {
@Override
public boolean setup(int width, int height, int redrawRate, Object renderTarget, int drFlags) {
return ImxDecoder.init() == 0;
}
@Override
public void release() {
ImxDecoder.destroy();
}
@Override
public void decodeUnit(DecodeUnit decodeUnit) {
List<ByteBufferDescriptor> units = decodeUnit.getBufferList();
boolean ok = true;
for (int i=0;i<units.size();i++) {
ByteBufferDescriptor bbd = units.get(i);
if (ok)
ok = (ImxDecoder.decode(bbd.data, bbd.offset, bbd.length, i == (units.size()-1)) == 0);
}
}
}