libavcodec视频解码不起作用

libavcodec video decoding not working

本文关键字:不起作用 视频解码 libavcodec      更新时间:2023-10-16

我正在尝试解码用H264编码的视频。我正在将AVPacket的数据及其大小发送到解码器代码。在那里,我正在尝试解码框架并将其显示在 GUI 上。问题是当我解码帧时,它返回的帧字节数与数据包的大小相同,这意味着它没有解压缩数据。谁能说出问题是什么。我的编码程序工作正常。

这是用于编码的代码

  static struct SwsContext *img_convert_ctx;
  pkt.data = NULL;   
  pkt.size = 0; 

  avpicture_fill((AVPicture *)srcFrame, frame,AV_PIX_FMT_BGR24, 640, 480);
 if(img_convert_ctx == NULL) {
  int w = 640;
  int h = 480;
  img_convert_ctx = sws_getContext(w, h, 
      AV_PIX_FMT_BGR24, c->width, c->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
   if(img_convert_ctx == NULL) {
    fprintf(stderr, "Cannot initialize the conversion context!n");
  }
}
  sws_scale(img_convert_ctx, srcFrame->data, srcFrame->linesize, 0,480,picture->data, picture->linesize);

  fflush(stdout);
  picture->pts=counter;

  ret = avcodec_encode_video2(c, &pkt, picture, &got_output);
    if (ret < 0) {
        fprintf(stderr, "Error encoding framen");
    }
    if (got_output) {
        vdec.decode_frame(pkt.data ,pkt.size);
        av_free_packet(&pkt);
    }

解码器代码...

    int len ,got_frame;
avpkt.size = data_length;
avpkt.data = frame_buffer;
if(!frame_buffer){
return "frame buffer emptyn";
}
len = avcodec_decode_video2(avctx ,frame ,&got_frame ,&avpkt);
if( len < 0){
    return "error while decodingn";
}
if( got_frame ){
static struct SwsContext *img_convert_ctx;  
 if(img_convert_ctx == NULL) {
  img_convert_ctx = sws_getContext(w, h, 
      PIX_FMT_YUV420P, avctx->width,
      avctx->height, PIX_FMT_BGR24, 
      SWS_BICUBIC, NULL, NULL, NULL);
   if(img_convert_ctx == NULL) {
    return  "Cannot initialize the conversion context!n";
  }
}
j=sws_scale(img_convert_ctx, 
    frame->data , frame->linesize ,
    0, h ,picture->data,
    picture->linesize );
if(j==0){
exit(1);
}

我正在将所有其他代码(如AVCodecContext和编解码器)初始化为其他方法。

请帮助我找到解决方案。

avcodec_decode_video2函数应返回处理的字节数,而不是结果图片的字节数。您只需要检查got_frame的值即可了解何时解码完整帧。