FFMpeg编码MPEG-1,2视频
来源:互联网 发布:seo学徒招聘 编辑:程序博客网 时间:2024/05/16 08:54
//STMPEG.h
#include <stdlib.h>
#include <string.h>
#pragma once
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include <libswscale/swscale.h>
#ifdef __cplusplus
}
#endif
class CSTMPEG
{
public:
private:
void InitMPEGData(MPEG_TYPE type);
//把图象颠倒过来
void RGBBuffer2RGBFrame(AVFrame *pRGBFrame, int width, int height,int bpp, uint8_t* pRGBBuffer);
AVFrame *alloc_picture(int pix_fmt, int width, int height);
bool open_video(AVFormatContext *pFormatContext, AVStream*pVideoStream);
AVStream *add_video_stream(AVFormatContext *pFormatContext, intcodec_id);
bool write_video_frame(AVFormatContext *pFormatcontext, AVStream*pAudioStream);
void close_video(AVFormatContext *pFormatcontext, AVStream*pVideoStream);
private:
MPEG_TYPE m_MPEGType;
int m_nWidth, m_nHeight;
float m_fFrameRate;
float m_fBitRate;
int
int srcWidth;
int srcHeight;
AVFrame
AVFrame
uint8_t*
const int
//使用流来读写文件
AVOutputFormat *m_pOutputFormat;
AVFormatContext *m_pFormatContext;
AVStream
AVStream
};
实现文件:
//STMPEG.cpp
#include "stmpeg.h"
#define LineWidthBytes(biWidth, biBitCount) ((biWidth * biBitCount+ 31) / 32 * 4)
CSTMPEG::CSTMPEG(): m_nOutBufSize(200000)
{
m_MPEGType
m_nWidth
m_nHeight
m_fFrameRate = 25;
m_fBitRate
m_nFrame
m_pRGBFrame
m_pYUVFrame
m_pFormatContext= NULL;
m_pVideoStream = NULL;
m_pAudioStream = NULL;
}
CSTMPEG::~CSTMPEG(void)
{
}
void CSTMPEG::InitMPEGData(MPEG_TYPE type)
{
switch(type) {
case VCD_PAL:
{
}
break;
case VCD_NTSC:
{
}
break;
case SVCD_PAL:
{
}
break;
case SVCD_NTSC:
{
}
break;
case DVD_PAL:
{
}
break;
case DVD_NTSC:
{
}
break;
default:
break;
}
}
void CSTMPEG::SetMPEGFormat(MPEG_TYPE type)
{
if (type != m_MPEGType)
{
}
}
void CSTMPEG::SetMPEGWidth(int width)
{
if (m_MPEGType == CUSTOM_MPEG1 || m_MPEGType == CUSTOM_MPEG2 )
}
void CSTMPEG::SetMPEGHeight(int height)
{
if (m_MPEGType == CUSTOM_MPEG1 || m_MPEGType == CUSTOM_MPEG2 )
}
void CSTMPEG::SetFrameRate(float rate)
{
if (m_MPEGType == CUSTOM_MPEG1 || m_MPEGType == CUSTOM_MPEG2 )
}
void CSTMPEG::SetMPEGVideoBitRate(float rate)
{
m_fBitRate = rate * 1000;
}
void CSTMPEG::RGBBuffer2RGBFrame(AVFrame *pRGBFrame, int width, intheight, int bpp, uint8_t* pRGBBuffer)
{
int linebytes = LineWidthBytes(width, bpp);
int x, y;
for(y=0;y<height;y++)
{
pRGBBuffer[ (height - y - 1)
pRGBBuffer[ (height - y - 1)
pRGBBuffer[ (height - y - 1)
}
}
AVFrame *CSTMPEG::alloc_picture(int pix_fmt, int width, int height)
{
AVFrame* picture = avcodec_alloc_frame();
if (!picture)
int size = avpicture_get_size(pix_fmt, width, height);
uint8_t *picture_buf = (uint8_t *)malloc(size);
if (!picture_buf) {
}
avpicture_fill((AVPicture *)picture, picture_buf,
}
bool CSTMPEG::OpenMPEG(const char* strfile)
{
av_register_all();
switch(m_MPEGType)
{