ffmpeg 内存 demo

来源:互联网 发布:seajs require其他js 编辑:程序博客网 时间:2024/04/30 03:53
        CFile myfile;
        VideoHead  _VideoHead;//自己定义的数据结构
        AVCodecContext  * m_pavCoedContext;
        AVCodec  * m_pavCode;
        AVFrame *m_pVidoeFrameRGB;
        BYTE *m_buffer;
        AVFrame *m_pVideoFrame;
        m_pavCoedContext = avcodec_alloc_context();
m_pavCode=avcodec_find_decoder(CODEC_ID_MPEG4);
int wid=400;int hei=500;//初始设定的图像大小
m_pavCoedContext->width=wid;
  m_pavCoedContext->height=hei;

  m_pavCoedContext->codec_type=CODEC_TYPE_VIDEO;
  m_pVideoFrame=avcodec_alloc_frame();
m_pVidoeFrameRGB=avcodec_alloc_frame();
if (m_pVidoeFrameRGB==NULL||m_pVideoFrame==NULL)
{
        return;
}
  int iBits=avpicture_get_size(PIX_FMT_RGB24,m_pavCoedContext->width,m_pavCoedContext->height);
m_buffer=new BYTE[iBits];
avpicture_fill((AVPicture*)m_pVidoeFrameRGB,m_buffer,
         PIX_FMT_RGB24,m_pavCoedContext->width,
         m_pavCoedContext->height);
if (avcodec_open(m_pavCoedContext,m_pavCode)<0)
{
        return;
}

                  int _k=0;

                        int k=        myfile.Open("c:\\m5.mp4",CFile::modeRead);
                          if (!k)
                        {
                         return ;
                        }
                         int iIndex =0;
                while (myfile.Read(&_VideoHead,sizeof(VideoHead)))
                {
          int             frameFinished=0;
          int      bytesRemaining=0;
          int bytesDecoded=0;
         
           uint8_t  *rawData;
                        int _iBufferSize=_VideoHead.dwDataLen;
bytesRemaining=_iBufferSize;
                                BYTE* pInBuffer;pInBuffer=NULL;
         pInBuffer=(BYTE *)malloc(_iBufferSize);
          myfile.Read(pInBuffer,_iBufferSize);
        switch(_VideoHead.iPlayType)
                          
                  {
        case 96://视频数据
                        if (bytesRemaining>0)
                {
                         
                                //memcpy(m_pVideoFrame->data,pInBuffer,bytesRemaining);
                                rawData=pInBuffer;
                        bytesDecoded=avcodec_decode_video(m_pavCoedContext,
                                m_pVideoFrame,
                                &frameFinished,rawData,bytesRemaining);
                        /*
解视频从rawdata 到pfrmae,解码器有pAVContect定义,bytesRemaining表示还剩多少数据没有解码,bytesDecoded表示此次解码的
数据长度,frameFinished表示这帧解码是否结束
  */
                        if(bytesDecoded < 0)
            {
                fprintf(stderr, "Error while decoding frame\n");
                return ;
            }
bytesRemaining-=bytesDecoded;
rawData+=bytesDecoded;
                
if (frameFinished)//解压结束
{
         if (m_pavCoedContext->width!=wid||m_pavCoedContext->height!=hei)
         {
                 wid=m_pavCoedContext->width;
hei=m_pavCoedContext->height;
                 delete m_buffer;
                 m_buffer=NULL;
int iBits=avpicture_get_size(PIX_FMT_RGB24,m_pavCoedContext->width,m_pavCoedContext->height);
m_buffer=new BYTE[iBits];
avpicture_fill((AVPicture*)m_pVidoeFrameRGB,m_buffer,
         PIX_FMT_RGB24,m_pavCoedContext->width,
         m_pavCoedContext->height);
         }
img_convert((AVPicture *)m_pVidoeFrameRGB,PIX_FMT_RGB24,(AVPicture *)m_pVideoFrame,
                        m_pavCoedContext->pix_fmt,
         m_pavCoedContext->width,m_pavCoedContext->height);
iIndex ++;
if (iIndex<100)
{

FILE *pFile;
    char szFilename[32];
    int  y;

    // Open file
    sprintf(szFilename, "c:\\frame%d.raw", iIndex);
    pFile=fopen(szFilename, "wb");
    if(pFile==NULL)
        return;

  
  fwrite(m_pVidoeFrameRGB->data[0] , 1, m_pavCoedContext->width*3*m_pavCoedContext->height, pFile);
    // Close file
    fclose(pFile);
}
else
{
        MessageBox("转化完成");
return;
}

}
                }
                break;
        default:
                break;
        }
        delete pInBuffer;
                }
原创粉丝点击