JNIEXPORT int JNICALL Java_com_example_testlive_MainActivity_Init( JNIEnv* env, jobject thiz)
{
//avcodec_init();
avcodec_register_all();
// Register all formats and codecs
av_register_all();
LOGI("register");
// Find the decoder for the video stream
pCodec=avcodec_find_decoder(CODEC_ID_H264);
if(pCodec == NULL)
{
LOGI("avcodec_find_decoder failed");
return -1; // Codec not found
}
else
{
pCodecCtx=avcodec_alloc_context3(pCodec);
if (pCodecCtx == NULL)
{
LOGI("avcodec_alloc_context3 failed");
}
pCodecCtx->time_base.num = 1; //
pCodecCtx->time_base.den = 25;
pCodecCtx->bit_rate = 0; //
pCodecCtx->frame_number = 1; //
pCodecCtx->codec_type = CODEC_ID_H264;
pCodecCtx->width = 704; //
pCodecCtx->height = 576;
}
// Open codec
LOGI("avcodec_find_decoder susscess");
//av_dict_set(&optionsDict, "b", "2.5M", 0);
//pCodec->capabilities=CODEC_CAP_DELAY;
if(pCodec->capabilities&CODEC_CAP_TRUNCATED)
pCodecCtx->flags|= CODEC_FLAG_TRUNCATED;
int ret = 0;
if((ret = avcodec_open2(pCodecCtx, pCodec, NULL))<0)
{
LOGI("avcodec_open2 failed");
return ret; // Could not open codec ******************程序执行到这里返回-22************
}
// Allocate video frame
pFrame=avcodec_alloc_frame();
// Allocate an AVFrame structure
pFrameRGBA=avcodec_alloc_frame();
if(pFrameRGBA==NULL)
{
LOGI("avcodec_alloc_frame failed");
return -1;
}
//get the scaling context
sws_ctx = sws_getContext
(
pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
pCodecCtx->width,
pCodecCtx->height,
AV_PIX_FMT_RGBA,
SWS_BILINEAR,
NULL,
NULL,
NULL
);
// Assign appropriate parts of bitmap to image planes in pFrameRGBA
// Note that pFrameRGBA is an AVFrame, but AVFrame is a superset
// of AVPicture
//avpicture_fill((AVPicture *)pFrameRGBA, buffer, AV_PIX_FMT_RGBA,
// pCodecCtx->width, pCodecCtx->height);
return 0;
}
我发现把CODEC_ID_H264换成CODEC_ID_MPEG4就成功了,为什么呢?我需要的是CODEC_ID_H264啊,
avcodec_find_decoder(CODEC_ID_MPEG4);//CODEC_ID_H264
CODEC_ID_H264---->AV_CODEC_ID_H264,版本问题