ffmpeg解码出现no frame的问题

为取经而来 为取经而来 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这几天测试ffmpeg实时解码的问题。先用X264编码,编码完的多个NAL拼接成一帧,将其复制到内存空间,丢给解码器,但是解码时一直出现no frame 的问题。
下面是我的所有代码,由于是测试,只对一张图片进行了编码,没有调用摄像头:

int video_decode(unsigned char *videoData)
{
int len = sizeof(videoData);
int ret;
int got_picture;

avcodec_register_all();
AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_H264);
AVCodecContext *codecCtx = avcodec_alloc_context3(codec);
avcodec_open2(codecCtx, codec, NULL);

AVFrame *frame = av_frame_alloc();
AVPacket *packet = new AVPacket;

av_new_packet(packet, len);
//memcpy(packet.data, videoData, len);
packet->data = videoData;



ret = avcodec_decode_video2(codecCtx, frame, &got_picture, packet);
printf("ret = %d\n",ret);
printf("got_picture = %d\n", got_picture);

if (ret > 0)
{
if (got_picture)
{
printf("1111111111111111\n");
}
}
return 0;
}

int handle(int width, int height, unsigned char *yuv_frame)
{
int fps = 25;
size_t yuv_size = width * height * 3 / 2;
x264_t *encoder;
x264_picture_t pic_in, pic_out;
uint8_t *yuv_buffer;

x264_param_t param;
x264_param_default_preset(¶m, "veryfast", "zerolatency");
param.i_threads = 1;
param.i_width = width;
param.i_height = height;
param.i_fps_num = fps;
param.i_fps_den = 1;
param.b_intra_refresh = 1;
param.b_annexb = 1;
param.b_repeat_headers = 1;
param.rc.b_mb_tree = 0;
param.i_level_idc = 30;//编码复杂度
param.rc.f_rf_constant = 25; //图像质量
param.rc.f_rf_constant_max = 45; //图像质量
param.i_keyint_max = 10;
param.i_bframe_adaptive = X264_B_ADAPT_TRELLIS;
param.i_threads = 1;
param.b_sliced_threads = 0;

x264_param_apply_profile(¶m, "baseline");
encoder = x264_encoder_open(¶m);
x264_picture_alloc(&pic_in, X264_CSP_I420, width, height);
yuv_buffer = (uint8_t*)malloc(yuv_size);

memcpy(yuv_buffer, yuv_frame, yuv_size);

pic_in.img.plane[0] = yuv_buffer;
pic_in.img.plane[1] = pic_in.img.plane[0] + width * height;
pic_in.img.plane[2] = pic_in.img.plane[1] + width * height / 4;

int64_t i_pts = 0;
x264_nal_t *nals = NULL;
x264_nal_t *nal;
int nnal;

pic_in.i_pts = i_pts++;

x264_encoder_encode(encoder, &nals, &nnal, &pic_in, &pic_out);

unsigned char *nalu_buf = (unsigned char *)malloc(800000);
printf("%d\n", nnal);

int i = 0;
for (nal = nals; nal < nals + nnal; nal++)
{
memcpy(nalu_buf + i, nal->p_payload, nal->i_payload);
/*for (int j = 0; j < 24; j++)
{
printf("%x\n", nalu_buf[j]);
}*/

i = i + nal->i_payload;
}
video_decode(nalu_buf);

x264_encoder_close(encoder);
free(yuv_buffer);
return 0;
}

int main(int argc, char* argv[])
{
const char* image_name = "1.jpg";
Mat img = imread(image_name);

cv::Mat yuvImg;
cv::cvtColor(img, yuvImg, CV_BGR2YUV_I420);
memcpy(pYuvBuf, yuvImg.data, yuv_bufLen);
handle(w, h, pYuvBuf);

return 0;
}

求解啊,我看网上差不多也是这么写的,就是不清楚为什么会有no frame,查看数据的时候SPS、PPS也都存在。。。。
...全文
评论 6
收藏 已收藏
分享
返回顶部