By joringyang
on Wed, 06/20/2012 - 17:12
Hello
I have problem to use the FFmpeg in the ImageTargets.cpp .
I want to use the FFmpeg to decode the video file, then convert frame to OpenGL ES texture.
Here is my ffmpeg and openGL ES code in the ImageTargets.cpp:
[CODE] JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_drawFrame(JNIEnv *, jobject)
{
...
while((i==0) && (av_read_frame(pFormatCtx, &packet)>=0)) {
if(packet.stream_index==videoStream) {
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if(frameFinished) {
LOGI("packet pts %llu", packet.pts);
img_convert_ctx = sws_getCachedContext(img_convert_ctx, pCodecCtx->width, pCodecCtx->height,
pCodecCtx->pix_fmt,
target_width, target_height, PIX_FMT_RGB24, SWS_BICUBIC,
NULL, NULL, NULL);
if(img_convert_ctx == NULL) {
LOGE("could not initialize conversion context\n");
return;
}
sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
videoTextures[0]->mWidth = 512;
videoTextures[0]->mHeight = 256;
videoTextures[0]->mData = pFrameRGB->data[0];
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glGenTextures(1, &s_texture);
videoTextures[0]->mTextureID = s_texture;
glBindTexture(GL_TEXTURE_2D,s_texture);
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(0 == got_texture)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, videoTextures[0]->mWidth, videoTextures[0]->mHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) videoTextures[0]->mData);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, videoTextures[0]->mWidth, videoTextures[0]->mHeight, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) videoTextures[0]->mData);
}else
{
glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0, videoTextures[0]->mWidth, videoTextures[0]->mHeight, GL_RGB, GL_UNSIGNED_BYTE, (GLvoid *) videoTextures[0]->mData);
LOGI("glTexSubImage2D!!");
}
LOGI("size of texture: %d x %d", videoTextures[0]->mWidth, videoTextures[0]->mHeight);
i = 1;
error = glGetError();
if( error != GL_NO_ERROR ) {
LOGE("couldn't create texture!!");
}
else {
got_texture = 1;
LOGE("no error");
}
}
}
av_free_packet(&packet);
}
isFirstRender = 0;
}[/CODE]
Then I call this method in the onResume() of ImageTargets.java, I use a thread to control the drawFrame(), let the texture changes according to the frame rate. Here is my code:
[CODE] private Handler videoHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
mGlView.queueEvent(new Runnable() {
public void run() {
mRenderer.drawFrame();
}
});
};
};
protected void onResume()
{
DebugLog.LOGD("ImageTargets::onResume");
super.onResume();
ImageTargetsRenderer.mainActivityHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
........
new Thread(){
public void run(){
int stopFlag = 0;
while(unStop){
try {
sleep(msPerFrame);// ms per frame
videoHandler.sendEmptyMessage(0);
stopFlag++;
if(stopFlag == videoFrameNum - 1)
unStop = false;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
};
}[/CODE]
The texture works correctly, but after decoded some frames, it always error , the error message is:
[CODE]06-18 20:32:50.362: A/libc(31948): @@@ ABORTING: HEAP MEMORY CORRUPTION IN dlfree
06-18 20:32:50.362: A/libc(31948): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)
[/CODE]
Why emerge this error?
Anyone with a solution?
Thanks.
Joe.
Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1)
I find the solution by myself, I use AVPicture instead of AVFrame.