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);
{
#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);
}