2,543
社区成员
发帖
与我相关
我的任务
分享
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;
}