首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于avcodec_decode_video 总是返回-1是咋回事

2012-05-14 
关于avcodec_decode_video 总是返回-1是怎么回事最近在做一个关于视频传输的项目,采用的是ffmpeg 编解码。

关于avcodec_decode_video 总是返回-1是怎么回事
最近在做一个关于视频传输的项目,采用的是ffmpeg 编解码。
其中解码代码如下:
static struct SwsContext * m_pSwsCtxDst;
static AVCodec *m_pCodec;
static AVCodecContext *m_pContext;
初始化:
avcodec_init();
  avcodec_register_all();
最近在做一个关于视频传输的项目,采用的是ffmpeg 编解码。
其中解码代码如下:
static struct SwsContext * m_pSwsCtxDst;
static AVCodec *m_pCodec;
static AVCodecContext *m_pContext;
初始化:
avcodec_init();
  avcodec_register_all();
m_pCodec = avcodec_find_decoder(CODEC_ID_H264);
m_pContext = avcodec_alloc_context();
if(m_pCodec->capabilities&CODEC_CAP_TRUNCATED)
{
  m_pContext->flags|= CODEC_FLAG_TRUNCATED; 
}
avcodec_open(m_pContext, m_pCodec) ;
m_pSwsCtxDst = sws_getContext(320,240,PIX_FMT_YUV420P,320,240,PIX_FMT_BGR24,SWS_BICUBIC,NULL, NULL, NULL);
解码是在一个线程里:
DWORD WINAPI CMainDlg:ecodeProc( LPVOID lpParameter )
{
AVFrame *picture = NULL;
while( !m_bOverDecode )
{
  packet_t *pkt = NULL;
  u_auto_lock * pLock = new u_auto_lock(&m_lock); //上锁
  if( !m_queRecv.empty() )  
  { 
  pkt = m_queRecv.front(); //数据在队列m_queRecv中存放
  m_queRecv.pop_front();
  delete pLock;
  int got_picture, len;
   
  picture= avcodec_alloc_frame();
  int size = pkt->length;
  while ( size > 0)
  {
  len = avcodec_decode_video( m_pContext , picture , &got_picture , pkt->data , size );
  if (len < 0) //
  {
  break;
  }
  if (got_picture) //解码之后显示
  {  
  AVFrame * picture_rgb = avcodec_alloc_frame();
  uint8_t *buf;
  int PictureSize = avpicture_get_size (PIX_FMT_BGR24, 320 , 240 );
  buf = (uint8_t *)av_malloc(PictureSize);
  avpicture_fill ( (AVPicture *)picture_rgb, buf, PIX_FMT_BGR24, 320 , 240 ); 
  sws_scale ( m_pSwsCtxDst, picture->data, picture->linesize, 0, 240 , picture_rgb->data, picture_rgb->linesize);
  BITMAPINFOHEADER bi;
  UCHAR* lpcFileBuffer= 0;
  bi.biSize = sizeof(BITMAPINFOHEADER);
  bi.biWidth = 320;
  bi.biHeight = 240;
  bi.biPlanes = 1;
  bi.biBitCount = 24;  
  bi.biCompression = BI_RGB;
  bi.biSizeImage = 320 * 240 * 3;
  bi.biXPelsPerMeter = 0;
  bi.biYPelsPerMeter = 0;
  bi.biClrUsed = 0;
  bi.biClrImportant = 0;
  //BYTE * lpbuffer ;
  HDC drawhdc = ::GetDC( m_sRemoteImg );
  RECT rc;
  ::GetClientRect( m_sRemoteImg , &rc );
  ::SetDIBitsToDevice(drawhdc,0,0,rc.right - rc.left ,rc.bottom - rc.top,0,0,0,rc.bottom - rc.top,picture_rgb->data[0] ,(BITMAPINFO*)&bi,DIB_RGB_COLORS);
  ::ReleaseDC( m_sRemoteImg , drawhdc );
  av_free (buf);
  av_free (picture_rgb);
   
// av_free (picture);
  }
  size -= len;
  pkt->data += len;
  }
  av_free (picture);
  free(pkt);  
  }
  else
  {
  delete pLock;
  ::WaitForSingleObject( m_hRecvData , INFINITE );
  }
}
return 0;
}

现在的问题是,有时候能正常显示而有时候就不能显示,随机性比较大,是哪里出了问题。解码的数据是从socket传输过来的


[解决办法]
先看你每次从socket中的数据是否正确

热点排行