ffmpeg实现yuv转mpeg

extern "C"
{
#include "libavformat/avformat.h"
#include "libavutil/time.h"
#include "libswscale/swscale.h"
#include "libavcodec/avcodec.h"
#include "libavutil/imgutils.h"
};
#include "sdl.h" 


#define VIDEO_WIDTH   352  
#define VIDEO_HEIGHT  288


SDL_Overlay *bmp;   
SDL_Surface *screen;   
SDL_Rect rect;   
SDL_Event event;  
struct SwsContext *img_convert_ctx; 
void ShowInit(int nWidth,int nHeight,int nPixelFormat)
{
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER);
screen = SDL_SetVideoMode(nWidth, nHeight, 0, 0); 
bmp = SDL_CreateYUVOverlay(nWidth,nHeight,SDL_YV12_OVERLAY, screen); 
}


void ShowVideo(AVPicture pict,int nWidth,int nHeight)
{
SDL_LockYUVOverlay(bmp);   
 
bmp->pixels[0] = pict.data[0];   
bmp->pixels[2] = pict.data[1];   
bmp->pixels[1] = pict.data[2];   


bmp->pitches[0] = pict.linesize[0];   
bmp->pitches[2] = pict.linesize[1];   
bmp->pitches[1] = pict.linesize[2]; 


SDL_UnlockYUVOverlay(bmp);   


rect.x = 0;   
rect.y = 0;   
rect.w = nWidth;   
rect.h = nHeight;   


SDL_DisplayYUVOverlay(bmp, &rect); 
}


extern AVFrame *get_av_frame(AVCodecContext *codec_context);


void EncoderTest()
{
printf("ffmpeg encoder test.\r\n");


FILE *outfile = fopen("test.mpg", "wb");
if(outfile == NULL)
{
printf("open outfile failed.\r\n");
exit(1);
}


FILE *infile = fopen("src.yuv","rb");
if(infile == NULL)
{
printf("open infile failed.\r\n");
exit(1);
}


av_register_all();
avcodec_register_all(); 


AVCodec *pVideoCodec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
if(pVideoCodec == NULL)
{
printf("find special codec failed.\r\n");
return;
}


AVCodecContext *pCodecContext = avcodec_alloc_context3(pVideoCodec);
pCodecContext->bit_rate = 400000; 
pCodecContext->width = VIDEO_WIDTH;
pCodecContext->height = VIDEO_HEIGHT;
AVRational avrational = {1,25};
pCodecContext->time_base= avrational;
pCodecContext->gop_size = 10;
pCodecContext->max_b_frames=1;
pCodecContext->thread_count = 1;
pCodecContext->pix_fmt = PIX_FMT_YUV420P;


int nret = avcodec_open2(pCodecContext, pVideoCodec, NULL);
if(nret<0)
{
printf("open special codec failed.\r\n");
return;
}


AVFrame *pFrame = avcodec_alloc_frame();
if(pFrame == NULL)
{
printf("alloc frame failed.\r\n");
return;
}


ShowInit(VIDEO_WIDTH,VIDEO_HEIGHT,PIX_FMT_YUV420P);


char *pszBuffer = new char[VIDEO_WIDTH*VIDEO_HEIGHT*3/2];


while(true)
{
int nret = fread(pszBuffer,sizeof(char),VIDEO_WIDTH*VIDEO_HEIGHT*3/2,infile);
if(nret == 0)
{
printf("file end.\r\n");
break;
}
else
{
//编码前显示
AVPicture picture;
avpicture_fill(&picture,(const uint8_t *)pszBuffer,AV_PIX_FMT_YUV420P, VIDEO_WIDTH, VIDEO_HEIGHT);
ShowVideo(picture,VIDEO_WIDTH,VIDEO_HEIGHT);


static int npts = 0;
AVFrame * frame = get_av_frame(pCodecContext);
frame->pts = npts++;

int ret = avpicture_fill((AVPicture*)(frame), (uint8_t*)pszBuffer, 
PIX_FMT_YUV420P, VIDEO_WIDTH, VIDEO_HEIGHT);


//编码
   int nEncodecFinish = 0;
AVPacket *pkt = new AVPacket;
av_init_packet(pkt);
pkt->data = NULL;
pkt->size = 0;


ret = avcodec_encode_video2(pCodecContext, pkt,frame, &nEncodecFinish);


if (nEncodecFinish) {
fwrite(pkt->data, 1, pkt->size, outfile);
av_free_packet(pkt);
}

   memset(pszBuffer,0,sizeof(char) * VIDEO_WIDTH * VIDEO_HEIGHT * 3 /2);


av_usleep(50000);
}
}
delete pszBuffer;pszBuffer=NULL;
fclose(outfile);
fclose(infile);

}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值