0

我正在尝试解码用 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 frame\n");
    }

    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 empty\n";

}

len = avcodec_decode_video2(avctx ,frame ,&got_frame ,&avpkt);

if( len < 0){

    return "error while decoding\n";

}

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 和 Codec)初始化为其他方法。

请帮助我找到解决方案。

4

1 回答 1

0

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

于 2013-02-05T11:45:39.530 回答