C++ avcodec接收数据包出错(gdi屏幕截图和ffmpeg)

C++ avcodec接收数据包出错(gdi屏幕截图和ffmpeg),c++,c,winapi,ffmpeg,C++,C,Winapi,Ffmpeg,我尝试用ffmpeg捕捉windows屏幕。 一切正常,但avcodec\u接收\u数据包返回错误AVERROR(EAGAIN) 我无法理解为什么会这样。 有人能提供建议吗 SwsContext* convertContext = sws_getContext(c->width, c->height, AV_PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NU

我尝试用ffmpeg捕捉windows屏幕。 一切正常,但avcodec\u接收\u数据包返回错误AVERROR(EAGAIN) 我无法理解为什么会这样。 有人能提供建议吗

SwsContext* convertContext = sws_getContext(c->width, c->height, AV_PIX_FMT_BGRA, c->width, c->height, c->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);

for (int i = 0; i < fps*seconds; i++)
{
fflush(stdout);

/* make sure the frame data is writable */
ret = av_frame_make_writable(frame);
if (ret < 0)
  exit(1);

gdi->MakeScreenshoot();

OutFrame->pts = i;

int ret = av_image_fill_arrays(GDIFrame->data, GDIFrame->linesize, gdi->m_bufferGDIBits, AV_PIX_FMT_BGRA, c->width, c->height, 1);
ret = av_image_fill_arrays(OutFrame->data, OutFrame->linesize, outbuffer, c->pix_fmt, c->width, c->height, 1);

GDIFrame->data[0] += GDIFrame->linesize[0] * (c->height - 1);                                                      // flipping frame
GDIFrame->linesize[0] *= -1;

int hslice = sws_scale(convertContext, GDIFrame->data, GDIFrame->linesize, 0, c->height,OutFrame->data, OutFrame->linesize);

/* encode the image */
encode(c, OutFrame, pkt, f);
}

static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt, FILE *outfile)
{
 int ret = -1;

 /* send the frame to the encoder */
 /*if (frame)
 printf("Send frame %3"PRId64"\n", frame->pts);*/

 ret = avcodec_send_frame(enc_ctx, frame);
 if (ret < 0)
 {
   fprintf(stderr, "Error sending a frame for encoding\n");
   exit(1);
 }

 while (ret >= 0)
 {
   ret = avcodec_receive_packet(enc_ctx, pkt);
   if (ret == AVERROR_EOF)
     return;
   if (ret == AVERROR(EAGAIN))
     return;
   else
  if (ret < 0)
  {
    fprintf(stderr, "Error during encoding\n");
    exit(1);
  }

  //printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
  fwrite(pkt->data, 1, pkt->size, outfile);
  av_packet_unref(pkt);
  }
}
SwsContext*convertContext=sws\u getContext(c->width,c->height,AV\u PIX\u FMT\u BGRA,c->width,c->height,c->PIX\u FMT,sws\u FAST\u双线性,NULL,NULL,NULL);
对于(int i=0;imakescreenshot();
外框->pts=i;
int-ret=av_image_fill_数组(GDIFrame->data,GDIFrame->linesize,gdi->m_bufferGDIBits,av_PIX_FMT_BGRA,c->width,c->height,1);
ret=av_图像_填充_阵列(外帧->数据,外帧->线条大小,突出,c->pix_fmt,c->宽度,c->高度,1);
GDIFrame->data[0]+=GDIFrame->linesize[0]*(c->height-1);//翻转帧
GDIFrame->linesize[0]*=-1;
int hslice=sws_比例(转换上下文,GDIFrame->data,GDIFrame->linesize,0,c->height,OUTPRAME->data,OUTPRAME->linesize);
/*对图像进行编码*/
编码(c,外帧,pkt,f);
}
静态无效编码(AVCodecContext*enc_ctx、AVFrame*frame、AVPacket*pkt、FILE*outfile)
{
int-ret=-1;
/*将帧发送到编码器*/
/*如果(帧)
printf(“发送帧%3”PRId64“\n”,帧->pts)*/
ret=avcodec_发送_帧(enc_ctx,帧);
如果(ret<0)
{
fprintf(stderr,“发送编码帧时出错”);
出口(1);
}
而(ret>=0)
{
ret=avcodec_接收_数据包(enc_ctx,pkt);
如果(ret==平均误差)
返回;
如果(ret==AVERROR(EAGAIN))
返回;
其他的
如果(ret<0)
{
fprintf(stderr,“编码过程中出错\n”);
出口(1);
}
//printf(“写入数据包%3”PRId64(大小=%5d)\n”,pkt->pts,pkt->size);
fwrite(pkt->data,1,pkt->size,outfile);
av_-packet_-unref(pkt);
}
}
如果我评论

if (ret == AVERROR_EOF)
  return;
if (ret == AVERROR(EAGAIN))
  return;
else
  if (ret < 0)
  {
    fprintf(stderr, "Error during encoding\n");
    exit(1);
  }
if(ret==AVERROR\u EOF)
返回;
如果(ret==AVERROR(EAGAIN))
返回;
其他的
如果(ret<0)
{
fprintf(stderr,“编码过程中出错\n”);
出口(1);
}
在编码功能中-一切都很好,文件将被写入并且将是正确的,但我想解决这个问题。

晚会有点晚了。 我也在用encode_video.c来尝试这一点

似乎avcodec_receive_packet()在编码器未准备好完整数据包时将返回这些“错误”。。。下一次调用avcodec_send_frame()可能会生成一个完整的数据包

所以继续前进。
使用NULL frame对avcodec_send_frame()的最终调用将刷新最终数据包

您是否试图理解,值
EAGAIN
报告了什么错误情况?否。如何?ret=-11。我在ffmpeg中没有找到描述EAGAIN,但在POSIX中它是“资源暂时不可用”。