Error in avcodec_open

Using FFmpeg's dlls in C, C++, C#, etc...

Error in avcodec_open

Postby Umakant on Thu Mar 12, 2009 4:07 pm

Hi All,

I am not able to open codec.
Every time when i call API avcodec_open, it returns error.
please go through the following code snippet and point me
where i am wrong.
AVCodecContext *context = avcodec_alloc_context();
if (NULL == context)
{
char buffer [] = "Fail to Allocate Memory\n";
fwrite (buffer, sizeof buffer,1, Fptr);
fflush (Fptr);
}
else
{

context->bit_rate = 400000;
context->pix_fmt = PIX_FMT_YUV420P;
context->codec_type = CODEC_TYPE_VIDEO;
context->width = 352;//img->width;
context->height = 288;//img->height;
context->time_base.den = 1;
context->time_base.num = 25;
context->gop_size = 10;
context->max_b_frames = 1;

AVCodec *codec = avcodec_find_encoder (CODEC_ID_MPEG4);
if (NULL == codec)
{
char buffer [] = "Fail to Find Codec\n";
fwrite (buffer, sizeof buffer,1, Fptr);
fflush (Fptr);
}
else
{
char buffer [] = "Codec has been successfully found\n";
fwrite (buffer, sizeof buffer,1, Fptr);
fflush (Fptr);
}
/* open it */
if (avcodec_open (context, codec) < 0)
{
char buffer [] = "Fail to Open Codec\n";
fwrite (buffer, sizeof buffer,1, Fptr);
fflush (Fptr);
}
else
{
char buffer [] = "Codec has been Successfully Opened\n";
fwrite (buffer, sizeof buffer,1, Fptr);
fflush (Fptr);
}
}
Any help would be highly appreciated.
Thanks in Advance.
Umakant
 
Posts: 37
Joined: Thu Mar 12, 2009 11:09 am

Re: Error in avcodec_open

Postby minyaev on Mon Feb 01, 2010 1:34 pm

I have got the same problem. How can I solve it?
minyaev
 
Posts: 1
Joined: Mon Feb 01, 2010 1:31 pm

Re: Error in avcodec_open

Postby powernet on Thu Feb 18, 2010 2:45 am

me too,why you tell us? thank!!!
powernet
 
Posts: 7
Joined: Thu Feb 18, 2010 2:43 am

Re: Error in avcodec_open

Postby powernet on Mon Feb 22, 2010 12:44 am

ding
powernet
 
Posts: 7
Joined: Thu Feb 18, 2010 2:43 am

Re: Error in avcodec_open

Postby Umakant on Tue Feb 23, 2010 9:20 am

Sorry for being late.
i do not remember the exact cause of the problem.
If you share your code snippet with me then i can help you.
Umakant
 
Posts: 37
Joined: Thu Mar 12, 2009 11:09 am

Re: Error in avcodec_open

Postby powernet on Fri Feb 26, 2010 12:41 pm

Code: Select all
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
extern "C"
{
#include "avformat.h"
#include "swscale.h"
#include "avcodec.h"
#include "avutil.h"
#pragma comment(lib, "avformat-52.lib")
#pragma comment(lib, "avcodec-52.lib")
#pragma comment(lib, "avutil-50.lib")
#pragma comment(lib, "swscale-0.lib")
};

AVCodecContext *pCodecCtx;
AVCodec *pCodec;
int mWidth;
int mHeight;
AVFrame *pFrame;
#define VHALIGNCALL16(x) \
   {\
      _asm { mov ebx, esp }\
      _asm { and esp, 0xfffffff0 }\
      _asm { sub esp, 12 }\
      _asm { push ebx }\
      x;\
      _asm { pop ebx }\
      _asm { mov esp, ebx }\
   }
AVFrame *alloc_picture(int pix_fmt, int width, int height)
{
    AVFrame *picture;
    uint8_t *picture_buf;
    int size;

    picture = avcodec_alloc_frame();
    if (!picture)
        return NULL;
    size = avpicture_get_size((PixelFormat)pix_fmt, width, height);
    picture_buf = (uint8_t *)av_malloc(size);
    if (!picture_buf) {
        av_free(picture);
        return NULL;
    }
    avpicture_fill((AVPicture *)picture, picture_buf,(PixelFormat)pix_fmt, width, height);
    return picture;
}

int img_convert(AVPicture* dst, PixelFormat dst_pix_fmt, AVPicture* src, PixelFormat pix_fmt, int width, int height)
{
   int av_log = av_log_get_level();
   av_log_set_level(AV_LOG_QUIET);
   SwsContext *img_convert_ctx = sws_getContext(width, height, pix_fmt, width, height, dst_pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
   int result = sws_scale(img_convert_ctx, src->data, src->linesize, 0, height, dst->data, dst->linesize);
   sws_freeContext(img_convert_ctx);
   av_log_set_level(av_log);
   return result;
}

int ffmpegEncode(unsigned char **x264InputFrame, int inLen, unsigned char **x264OutputFrame)
{
   int n = avpicture_fill((AVPicture*)pFrame,*x264InputFrame,PIX_FMT_YUV422P,mWidth,mHeight);
   if(n<0) return -1;
   return avcodec_encode_video(pCodecCtx,*x264OutputFrame,mHeight*mWidth*3/2, pFrame);
   

   
   //AVFrame *picture;
   ////uint8_t video_outbuf_size=200000;
   ////uint8_t* video_outbuf =new uint8_t[video_outbuf_size];
   //picture = alloc_picture(pCodecCtx ->pix_fmt, pCodecCtx ->width, pCodecCtx ->height);
   //picture->data[0]=*x264InputFrame;
   //return avcodec_encode_video(pCodecCtx,*x264OutputFrame,mHeight*mWidth*3/2, picture);

   
}

int ffmpegInit(int width,int height)
{
   
   mWidth=width;
   mHeight=height;
   avcodec_init();
    av_register_all();
   
   
   pCodecCtx = avcodec_alloc_context();
   avcodec_get_context_defaults2(pCodecCtx, CODEC_TYPE_VIDEO);
   /* 视频相关参数 */
   pCodecCtx->codec_id = CODEC_ID_H264,
   pCodecCtx->codec_type = CODEC_TYPE_VIDEO;
   pCodecCtx->width = mWidth;
   pCodecCtx->height =mHeight;
   pCodecCtx->bit_rate = 400000;
   pCodecCtx->time_base.den = 25;
   pCodecCtx->time_base.num = 1;
   pCodecCtx->dct_algo = 0;
   pCodecCtx->gop_size = 10;
   pCodecCtx->me_pre_cmp=2;
   pCodecCtx->cqp = 26;
   pCodecCtx->me_method =7;
   pCodecCtx->qmin = 3;
   pCodecCtx->qmax = 31;
   pCodecCtx->max_qdiff = 3;
   pCodecCtx->max_b_frames=1;
   pCodecCtx->qcompress = 0.5;
   pCodecCtx->qblur = 0.5;
   pCodecCtx->nsse_weight = 8;
   pCodecCtx->i_quant_factor = (float)0.8;
   pCodecCtx->b_quant_factor = 1.25;
   pCodecCtx->b_quant_offset = 1.25;
   pCodecCtx->pix_fmt = PIX_FMT_YUV420P;//当前YUV420
   
   /* 查找视频解码器 */
   pCodec= avcodec_find_encoder(CODEC_ID_H264);
   if (pCodec==NULL)
   {
      return -1;
   }

   /* 打开视频解码器 */
    int iRetOpen=avcodec_open(pCodecCtx,pCodec);//avcodec_open,iRetOpen alway <0?????????????????????????
   if (iRetOpen< 0)
   {
       printf("avcodec_open error:",iRetOpen);
      return -1;
   }

   
   pFrame= avcodec_alloc_frame();
   if(pFrame== NULL)return -1;
   avcodec_get_frame_defaults(pFrame);
    return 0;
}

//int ffmpegInit(int width,int height)
//{
//   mWidth=width;
//   mHeight=height;
//   av_register_all();
//   //
//   pCodecCtx = avcodec_alloc_context();
//   avcodec_get_context_defaults(pCodecCtx);
//   //
//
//   pCodec=avcodec_find_encoder(CODEC_ID_H264);
//   if(pCodec==NULL)return -1;
//   
//      /* 视频相关参数 */
//      pCodecCtx->codec_id = CODEC_ID_H264,
//      pCodecCtx->codec_type = CODEC_TYPE_VIDEO;
//      pCodecCtx->width = mWidth;
//      pCodecCtx->height =mHeight;
//      pCodecCtx->bit_rate = 400000;
//      pCodecCtx->time_base.den = 25;
//      pCodecCtx->time_base.num = 1;
//      pCodecCtx->dct_algo = 0;
//      pCodecCtx->gop_size = 10;
//      pCodecCtx->me_pre_cmp=2;
//      pCodecCtx->cqp = 26;
//      pCodecCtx->me_method =7;
//      pCodecCtx->qmin = 3;
//      pCodecCtx->qmax = 31;
//      pCodecCtx->max_qdiff = 3;
//      pCodecCtx->max_b_frames=1;
//      pCodecCtx->qcompress = 0.5;
//      pCodecCtx->qblur = 0.5;
//      pCodecCtx->nsse_weight = 8;
//      pCodecCtx->i_quant_factor = (float)0.8;
//      pCodecCtx->b_quant_factor = 1.25;
//      pCodecCtx->b_quant_offset = 1.25;
//      pCodecCtx->pix_fmt = PIX_FMT_YUV420P;//当前YUV420
//
//   //
//   if(avcodec_open(pCodecCtx, pCodec)<0)
//      return -1;
//   //  avcodec_close(pCodecCtx);
//   //
//
//   
//   return 0;
//}

static void ffmpeg_avcodec_log(void *ptr, int val, const char * msg, va_list ap)
                        
{
   AVClass* avc= ptr ? *(AVClass**)ptr : NULL;

   FILE *flog=fopen("MMedia.log","a");
   fprintf(flog, "[%s @ %p] ", avc->item_name(ptr), avc);
   vfprintf(flog, msg, ap);
   fclose(flog);
}
void ffmpegUninit()
{
   avcodec_close(pCodecCtx);
   av_free(pCodec);
}
int yuy2toi420(unsigned char* out_buf,unsigned char* src_buf)
{

   int src_size, out_size, tem_size;
   unsigned char *tem_buf;
   unsigned char *Y, *U, *V;
   unsigned char * Y2, *U2, *V2;
   unsigned char * p=NULL;
   src_size = (mWidth * mHeight) << 1 ;//对于YUY2 4:2:2
   tem_size = (mWidth *mHeight) << 1; ///对于YUV 4:2:2
   tem_buf = new unsigned char[tem_size*sizeof(char)];
   memset(tem_buf, 0, tem_size);

   out_size =(int)( mWidth*mHeight * 1.5);//对于YUV 4:2:0
   //out_buf = (unsigned char *)malloc(out_size*sizeof(char));
   //memset(out_buf, 0, out_size);

   p = src_buf;
   Y = tem_buf;
   U = Y +mWidth*mHeight;
   V = U + (mWidth*mHeight>>1);

   Y2 = out_buf;
   U2 = Y2 + mWidth*mHeight;
   V2 = U2 + (mWidth*mHeight>>2);

   /*由打包YUYV变成平板YUV*/
   for(int k=0; k<mHeight; ++k)
   {
      for(int j=0; j<(mWidth>>1); ++j)
      {   
         Y[j*2] = p[4*j];   
         U[j] = p[4*j+1];
         Y[j*2+1] = p[4*j+2];
         V[j] = p[4*j+3];
      }
      p = p + mWidth*2;

      Y = Y + mWidth;
      U = U + (mWidth>>1);
      V = V + (mWidth>>1);
   }

   //复位
   Y = tem_buf;
   U = Y + mWidth*mHeight;
   V = U + (mWidth*mHeight>>1);

   for(int l=0; l<mHeight/2; ++l)
   {
      memcpy(U2, U, mWidth>>1);
      memcpy(V2, V, mWidth>>1);

      U2 = U2 + (mWidth>>1);
      V2 = V2 + (mWidth>>1);

      U = U + (mWidth);
      V = V + (mWidth);
   }
   memcpy(Y2, Y, mWidth*mHeight);
   delete[] tem_buf;
   return 0;
}
powernet
 
Posts: 7
Joined: Thu Feb 18, 2010 2:43 am

Re: Error in avcodec_open

Postby powernet on Tue Mar 02, 2010 3:36 am

who can tell me?Thanks
powernet
 
Posts: 7
Joined: Thu Feb 18, 2010 2:43 am

Re: Error in avcodec_open

Postby Umakant on Tue Mar 02, 2010 8:57 am

Try to include ffmpeg library in that order libavutil.lib libavcodec.lib libavformat.lib.
I got your code today only. i will try to find out the cause of the problem if any.
Umakant
 
Posts: 37
Joined: Thu Mar 12, 2009 11:09 am

Re: Error in avcodec_open

Postby Umakant on Tue Mar 02, 2010 9:08 am

Please also share what enviornment are u using to build the application?
can you please let me know what all libraries are u including in your project and in which order ?
Umakant
 
Posts: 37
Joined: Thu Mar 12, 2009 11:09 am

Re: Error in avcodec_open

Postby scandinaf on Tue Mar 02, 2010 10:11 am

It seems that ffmpeg libraries were built with help of gcc 4.3 or 4.4 and then used under Windows in the MVS. Am I right?
scandinaf
 
Posts: 11
Joined: Mon Dec 21, 2009 11:47 am

Re: Error in avcodec_open

Postby Umakant on Tue Mar 02, 2010 11:18 am

In which order are u including your libraries in your code/project? please share the name of all the libraries.
Which version of MVS are u using?
Umakant
 
Posts: 37
Joined: Thu Mar 12, 2009 11:09 am

Re: Error in avcodec_open

Postby Arthur on Fri Mar 05, 2010 1:40 pm

I have exactly the same problem.

I used last week's ffmpeg build, (ffmpeg-r21566-swscale-r30472-mingw-w64-shared.7z for example) and I am not able to open a H264 encoder. I am using VC++2005

I am using the proposed include and library order.

Does anyone have a suggestion?
Arthur
 
Posts: 6
Joined: Wed Feb 04, 2009 3:14 pm

Re: Error in avcodec_open

Postby Umakant on Mon Mar 08, 2010 8:31 am

Have u tried to open H.263/MPEG4 codec?
Umakant
 
Posts: 37
Joined: Thu Mar 12, 2009 11:09 am

Re: Error in avcodec_open

Postby Pogsquog on Thu Jun 10, 2010 8:41 am

Make sure you have the following lines of code...

/* must be called before using avcodec lib */
avcodec_init();

/* register all the codecs */
avcodec_register_all();
Pogsquog
 
Posts: 1
Joined: Thu Jun 10, 2010 8:40 am


Return to Using libav* in your projects

Who is online

Users browsing this forum: MSN [Bot] and 1 guest

cron