Hi

I want to convert the AVFrames into a bmp file. The code *Hoang Ong *has
provided works good for JPEG. But when I change the codec to CODEC_ID_BMP
and the pixelformat to PIX_FMT_RGB32 my bitmap is not saved. Can you help me
how to convert it to BMP. Here is the code that I am using

int WriteBMP (AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo){
         AVCodecContext         *pOCodecCtx;
         AVCodec                *pOCodec;
         uint8_t                *Buffer;
         int                     BufSiz;
         int                     BufSizActual;
         int                     ImgFmt = PIX_FMT_RGB32; //for the newer
ffmpeg version, this int to pixelformat
         FILE                   *BMPFile;
         char                    BMPName[256];

         BufSiz = avpicture_get_size (
ImgFmt,pCodecCtx->width,pCodecCtx->height );

         Buffer = (uint8_t *)malloc ( BufSiz );
         if ( Buffer == NULL )
                 return ( 0 );
         memset ( Buffer, 0, BufSiz );

         pOCodecCtx = avcodec_alloc_context ( );
         if ( !pOCodecCtx ) {
                  free ( Buffer );
                  return ( 0 );
                  }

         pOCodecCtx->bit_rate      = pCodecCtx->bit_rate;
         pOCodecCtx->bit_rate_tolerance      =
pCodecCtx->bit_rate_tolerance;
         pOCodecCtx->width         = pCodecCtx->width;
         pOCodecCtx->height        = pCodecCtx->height;
         pOCodecCtx->pix_fmt       = ImgFmt;
         pOCodecCtx->codec_id      = CODEC_ID_BMP;
         pOCodecCtx->codec_type    = CODEC_TYPE_VIDEO;
         pOCodecCtx->time_base.num = pCodecCtx->time_base.num;
         pOCodecCtx->time_base.den = pCodecCtx->time_base.den;

         pOCodec = avcodec_find_encoder ( pOCodecCtx->codec_id );
         if ( !pOCodec ) {
                  free ( Buffer );
                 return ( 0 );
                  }
         if ( avcodec_open ( pOCodecCtx, pOCodec ) < 0 ) {
                  free ( Buffer );
                  return ( 0 );
                 }

                pOCodecCtx->mb_lmin        = pOCodecCtx->lmin =
pOCodecCtx->qmin * FF_QP2LAMBDA;
         pOCodecCtx->mb_lmax        = pOCodecCtx->lmax =
pOCodecCtx->qmax * FF_QP2LAMBDA;
         pOCodecCtx->flags          = CODEC_FLAG_QSCALE;
         pOCodecCtx->global_quality = pOCodecCtx->qmin * FF_QP2LAMBDA;

         pFrame->pts     = 1;
         pFrame->quality = pOCodecCtx->global_quality;
         BufSizActual = avcodec_encode_video(pOCodecCtx,Buffer,BufSiz,pFrame
);

         sprintf ( BMPName, "%d.bmp", FrameNo );
         BMPFile = fopen ( BMPName, "wb" );
         fwrite ( Buffer, 1, BufSizActual, BMPFile );
         fclose ( BMPFile );

         avcodec_close ( pOCodecCtx );
         free ( Buffer );
         return ( BufSizActual );
}


Thanks
Varun Dua
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user

Reply via email to