1

我正在编写一种 Skype 应用程序,其中我首先使用 x265 编码器对视频进行编码。您可以在下面看到编码器的设置以及对视频进行编码并获取 nal_units 的行。

x265_param_default_preset(param, "ultrafast", "zerolatency");
x265_param_parse(param, "fps", "30");
x265_param_parse(param, "input-res", "160x120"); //wxh
x265_param_parse(param, "bframes", "3");
x265_param_parse(param, "rc-lookahead", "5");
x265_param_parse(param, "repeat-headers", "1");

x265_nal *pp_nal=NULL;
uint32_t pi_nal=0;

int encoded = x265_encoder_encode(encoder, &pp_nal, &pi_nal, pic_in, pic_recon);

在此之后,我将 pp_nal 和 pi_nal 发送到客户端,然后我想使用 ffmpeg 将这些 nal_units 解码为 yuv。我初始化我的解码器:

AVCodec *codec;
AVCodecContext *av_codec_context = NULL;
avcodec_register_all();

int frame_count;
FILE *f;
AVFrame *frame;
AVPacket avpkt;
av_init_packet(&avpkt);

codec = avcodec_find_decoder(AV_CODEC_ID_H265);
if (!codec) {
    fprintf(stderr, "Codec not found\n");
    exit(1);
}

av_codec_context = avcodec_alloc_context3(codec);
if (!av_codec_context) {
    fprintf(stderr, "Could not allocate video codec context\n");
    exit(1);
}
av_codec_context->width = 160;
av_codec_context->height = 120;
av_codec_context->extradata = NULL;
av_codec_context->pix_fmt = PIX_FMT_YUV420P;

/* open it */
if (avcodec_open2(av_codec_context, codec, NULL) < 0) {
    fprintf(stderr, "Could not open codec\n");
    exit(1);
}
AVFrame *av_frame_ = av_frame_alloc();
frame_count = 0;
int got_frame;

然后对于每个最终数据包,我执行以下操作:

AVPacket av_packet;
av_new_packet(&av_packet, pp_nal->sizeBytes);
av_packet.data = (uint8_t *) pp_nal->payload;
av_packet.size = pp_nal->sizeBytes;

avcodec_decode_video2(av_codec_context, av_frame_, &got_frame, &av_packet);

但这不起作用并给出以下错误“解析最终单元时出错”。我究竟做错了什么?

4

0 回答 0