[Ffmpeg-devel] Re: How to encode videos usinglibavcodec/libavformatDLLs?

RahulOP rahul.prasad
Tue Jun 20 15:06:55 CEST 2006


Lars Blumberg <Lars.Blumberg <at> aquasoft.de> writes:
> > Thanks Martin,
> that helps a lot to test my code!
> Best regards
> Lars
> "Martin Boehme" <boehme <at> inb.uni-luebeck.de> schrieb im Newsbeitrag 
> news:4378513F.2070904 <at> inb.uni-luebeck.de...
> Hi Lars, 
> > I want to use the libavcodec/libavformat DLLs to encode a video/sound 
> > stream. Therefore I want to put every single frame into the stream. Is 
> > there any place where I can find some information about how to do that, 
> > may be a sample application, tutorial or something like that? I am 
> > programming with Delphi, but I guess als VC++ or VB code would be 
> > helpfull. I want to load the DLLs dynamically into the application. Thanks 
> > for any comments.
> Try output_example.c (included in the FFmpeg distribution). That's a
> pretty accessible example of how to output video and audio streams.
> > Martin


Has anyone been able to successfully use libavcodec dll in a VC++ program? I
tried doing this by creating a Dialog based applicatio, dynamically linking and
then mashing/modifying the code given in apiexample.c and output_example.c I now
have a  workable code that encodes a 12Mb file to a 100kb MPEG video file but
this file doesnt even play on WindMediaPLayer. I dont think I am encoding the
entire file/ all the frames so any help will be greatly appreciated. Am posting
the code that I have written so far

void Cfflib4Dlg::pgmsave(unsigned char *buf,int wrap, int xsize,int ysize,char
*filename) 
{
    FILE *f2;
    int i;
     
	f2=fopen("C:/test.mpeg","w");
	if(!f2){
	CString str;
	   int a = GetLastError();
	   str.Format("%d",a);
	   AfxMessageBox(str);
	   AfxMessageBox("file");
}
    fprintf(f2,"P5\n%d %d\n%d\n",xsize,ysize,255);
    for(i=0;i<ysize;i++)
        fwrite(buf + i * wrap,1,xsize,f2);
    fclose(f2);
}

void Cfflib4Dlg::OnBnClickedOk()
{
	//	int frame, sized, got_picture, len,outbuf_size;
    uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
    char buf[1024];
    /* set end of buffer to 0 (this ensures that no overreading happens for
damaged mpeg streams) */
    memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
	int i, out_size, size, x, y, outbuf_size,len,got_picture,frame;
    FILE *f;    
    uint8_t *outbuf, *picture_buf;

	

	HMODULE hDll = LoadLibrary("avcodec.dll");
	
   if(hDll== NULL)
   {
	   CString str;
	   int a = GetLastError();
	   str.Format("%d",a);
	   AfxMessageBox(str);
	   AfxMessageBox("first");
AfxMessageBox("Failed loading1");
   }

   typedef void (avcodec_init)();
avcodec_init* p = (avcodec_init*)GetProcAddress(hDll, "avcodec_init");
if(p==NULL)
AfxMessageBox("Failed loading2");
p();

typedef void (avcodec_register_all)();
avcodec_register_all* p1a = (avcodec_register_all*)GetProcAddress(hDll,
"avcodec_register_all");
if(p1a==NULL)
AfxMessageBox("Failed loading3");
p1a();

typedef AVCodec * (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll,
"avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MPEG1VIDEO);


AVCodec *codec=p3(CODEC_ID_MPEG1VIDEO);

typedef AVCodecContext* (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll,
"avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();

AVCodecContext *avctx = p4();

typedef AVFrame* (avcodec_alloc_frame)();
avcodec_alloc_frame* p7
=(avcodec_alloc_frame*)GetProcAddress(hDll,"avcodec_alloc_frame");
p7();
AVFrame *picture =p7();

 avctx->width = 352;  
    avctx->height = 288;
    avctx->frame_rate = 25; 

typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2(avctx,codec);

typedef int (avcodec_encode_video)(AVCodecContext *avctx, uint8_t *buf, int
buf_size,
                         const AVFrame *pict);
avcodec_encode_video *p9=(avcodec_encode_video*)GetProcAddress(hDll,
"avcodec_encode_video");
if(p9==NULL)
AfxMessageBox("Failed loading9");

typedef int (avcodec_close)(AVCodecContext *avctx);
avcodec_close *p10=(avcodec_close*)GetProcAddress(hDll, "avcodec_close");
if(p10==NULL)
AfxMessageBox("Failed loading9");

f=fopen("C:/Rahul.avi","r");
if(!f){
	CString str;
	   int a = GetLastError();
	   str.Format("%d",a);
	   AfxMessageBox(str);
	   AfxMessageBox("file");
}
outbuf_size = 100000;
    outbuf = (uint8_t*)malloc(outbuf_size);
    size = avctx->width * avctx->height;
    picture_buf = (uint8_t*)malloc((size * 3) / 2); /* size for YUV 420 */
    
    picture->data[0] = picture_buf;
    picture->data[1] = picture->data[0] + size;
    picture->data[2] = picture->data[1] + size / 4;
    picture->linesize[0] = avctx->width;
    picture->linesize[1] = avctx->width / 2;
    picture->linesize[2] = avctx->width / 2;

frame = 0;
    for(;;) {
        size = fread(inbuf, 1, INBUF_SIZE, f);
        if (size == 0)
            break;
        inbuf_ptr = inbuf;
        while (size > 0)
		{

			len = p9(avctx,outbuf, outbuf_size, picture);
            if (len < 0) {
                //fprintf(stderr, "Error while decoding frame %d\n", frame);
//("error while encoding");          
				exit(1);
            }
			else
			 {
                //printf("saving frame %3d\n", frame);
                fflush(stdout);
                
				_snprintf(buf, sizeof(buf),"C:\test.mpeg",frame);
				//buf +=sprintf(buf,frame);
				pgmsave(picture->data[0], picture->linesize[0], 
                         avctx->width, avctx->height, buf);
                frame++;
            }
            size -= len;
            inbuf_ptr += len;
        }
    }

	 len = p9(avctx,outbuf, outbuf_size, picture);
//    if (got_picture) {
    //    printf("saving last frame %3d\n", frame);
        fflush(stdout);
        
        /* the picture is allocated by the decoder. no need to
           free it */
		_snprintf(buf, sizeof(buf),"C:\test.mpeg",frame);
        pgmsave(picture->data[0], picture->linesize[0], 
                 avctx->width, avctx->height, buf);
        frame++;
  //  }
         outbuf[0] = 0x00;
    outbuf[1] = 0x00;
    outbuf[2] = 0x01;
    outbuf[3] = 0xb7;
FILE *f2;
 //   int i;
     
	f2=fopen("C:/test.mpeg","a+");
	  fwrite(outbuf,1,4,f2);

    fclose(f);
fclose(f2);

    p10(avctx);

	// TODO: Add your control notification handler code here
FreeLibrary(hDll);
	
	OnOK();
}





More information about the ffmpeg-devel mailing list