Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092549
  • 博文数量: 282
  • 博客积分: 10865
  • 博客等级: 上将
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-12 12:35
文章存档

2017年(1)

2016年(3)

2015年(10)

2014年(12)

2013年(5)

2012年(10)

2011年(29)

2010年(3)

2008年(13)

2007年(92)

2006年(104)

我的朋友

分类: WINDOWS

2008-10-20 09:02:53

{ 最新更新,经过测试,QQ摄像头的帧率20fps,算是不错了。实时性还可以,

位率平均25k左右,完全可以网络传输,视频大小320 * 240,清晰度挺不错的。

xvid_encFrame.quant可以调整图像质量,数值越到位率越低,质量越差(1~31,最佳为4)

}

unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DSUtil, StdCtrls, DSPack, DirectShow9, Menus, ExtCtrls,lib_xvid;

type
TVideoForm = class(TForm)
FilterGraph: TFilterGraph;
VideoWindow: TVideoWindow;
MainMenu1: TMainMenu;
Devices: TMenuItem;
Filter: TFilter;
Image: TImage;
SampleGrabber: TSampleGrabber;
SnapShot: TButton;
CallBack: TCheckBox;
mmo1: TMemo;
lbl1: TLabel;
tmr1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure SnapShotClick(Sender: TObject);
procedure SampleGrabberBuffer(sender: TObject; SampleTime: Double;
pBuffer: Pointer; BufferLen: Integer);
procedure tmr1Timer(Sender: TObject);
private
// XVID ENCODER
xvid_gbl: xvid_gbl_init_t;
xvid_enc : xvid_enc_create_t;
xvid_encFrame: xvid_enc_frame_t;
xvid_encStats: xvid_enc_stats_t;

xVid_decode : xvid_dec_create_t;
xvid_decFrame: xvid_dec_frame_t;
xvid_decStats: xvid_dec_stats_t;
public
procedure OnSelectDevice(sender: TObject);
end;

const
MaxPixelCount = 65536;
type
//Pf24bit真彩色位图像素数据结构体
pRGBTripleArray = ^TRGBTripleArray;
TRGBTripleArray = array[0..MaxPixelCount - 1] of TRGBTriple;
var
VideoForm: TVideoForm;
SysDev: TSysDevEnum;
FrameBuf:array [0..352*288*3 ] of Byte;
FrameImg:array [0..352*288*3 ] of Byte;
FrameSequece : LongWord= 0;
spf:TFileStream;
xvid_Version:Integer;
FrameByteCount : LongWord = 0;
CurFrame: LongWord = 0;
implementation

{$R *.dfm}

procedure TVideoForm.FormCreate(Sender: TObject);
var
i: integer;
Device: TMenuItem;
begin
xvid_Version :=XVID_MAKE_VERSION(1,1,0);
SysDev:= TSysDevEnum.Create(CLSID_VideoInputDeviceCategory);
if SysDev.CountFilters > 0 then
for i := 0 to SysDev.CountFilters - 1 do
begin
Device := TMenuItem.Create(Devices);
Device.Caption := SysDev.Filters[i].FriendlyName;
Device.Tag := i;
Device.OnClick := OnSelectDevice;
Devices.Add(Device);
end;
end;

procedure TVideoForm.OnSelectDevice(sender: TObject);
begin
Self.DoubleBuffered := True;
FilterGraph.ClearGraph;
FilterGraph.Active := false;
Filter.BaseFilter.Moniker := SysDev.GetMoniker(TMenuItem(Sender).tag);
FilterGraph.Active := true;
with FilterGraph as ICaptureGraphBuilder2 do
begin
RenderStream(@PIN_CATEGORY_PREVIEW, nil, Filter as IBaseFilter, SampleGrabber as IBaseFilter, VideoWindow as IbaseFilter);
end;
try
//XVID库初始化操作
xvid_gbl.version := xvid_Version; //Version:1.1.0
xvid_gbl.cpu_flags := Word(XVID_CPU_FORCE or XVID_CPU_ASM);//0:自动检查CPU,XVID_CPU_FORCE or XVID_CPU_ASM:强制使用ASM汇编优化
xvid_gbl.debug := 0; //调试级别

//初始化编解码
xvid_global(nil, XVID_GBL_INIT, @xvid_gbl, nil);

// XVID编码器初始化
xvid_enc.version := xvid_Version;
//编码器参数
xvid_enc.global := XVID_GLOBAL_PACKED; //全局标志
xvid_enc.width := 320; //压缩视频宽度
xvid_enc.height := 240; //压缩视频高度
xvid_enc.fbase := 3; //基本帧率/每秒 = fbase * 10 = 30
xvid_enc.fincr := 1; //帧率增长步长,0:可变步长,>1实际增长步长
xvid_enc.profile := XVID_PROFILE_AS_L2; //压缩级别,MPEG4-ASP最高压缩级别
xvid_enc.max_key_interval := 0; //最大关键帧间隔
xvid_enc.frame_drop_ratio := 0; //丢帧率;0~100
xvid_enc.max_bframes := 0; //是否采用B帧,一般采用I,P帧,如果1=PB帧
xvid_enc.bquant_offset := 0;
xvid_enc.bquant_ratio := 0;

//创建编码器
xvid_encore(nil, XVID_ENC_CREATE, @xvid_enc, nil);
//初始化压缩数据帧结构
xvid_encFrame.version := xvid_Version;
// --- VOL FLAGS
xvid_encFrame.vol_flags := XVID_VOL_MPEGQUANT OR XVID_VOL_QUARTERPEL OR XVID_VOL_GMC;
// --- VOP FLAGS
xvid_encFrame.vop_flags := XVID_VOP_INTER4V or XVID_VOP_CHROMAOPT ;
xvid_encFrame.motion := XVID_ME_HALFPELREFINE16 or XVID_ME_CHROMA_PVOP or XVID_ME_ADVANCEDDIAMOND16; //运动估计
xvid_encFrame.quant := 4; //质量控制=量化参数,0~31,数值越小质量越高和码率成反比

xvid_encFrame.coding_type := XVID_TYPE_AUTO; //XVID_TYPE_AUTO=让编码器自动决定,I帧编码是关键帧,P帧编码是帧内预测

// XVID解码器初始化
xVid_decode.version := xvid_Version;
xVid_decode.width := 320;
xVid_decode.height := 240;

//创建解码器
xvid_decore(nil,XVID_DEC_CREATE,@xVid_decode, nil);

spf := TFileStream.Create('c:\test.mp4',fmCreate or fmOpenReadWrite);
except
Exit;
end;
FilterGraph.Play;
end;

procedure TVideoForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CallBack.Checked := False;
SysDev.Free;
FilterGraph.ClearGraph;
FilterGraph.Active := false;
//关闭编解码器
if Assigned(xvid_enc.handle) then
xvid_encore(xvid_enc.handle,XVID_ENC_DESTROY, @xvid_enc, nil);

if Assigned(xVid_decode.handle) then
xvid_decore(xVid_decode.handle,XVID_DEC_DESTROY,@xvid_decode,nil);
end;

procedure TVideoForm.SnapShotClick(Sender: TObject);
begin
SampleGrabber.GetBitmap(Image.Picture.Bitmap);
end;

procedure TVideoForm.SampleGrabberBuffer(sender: TObject;
SampleTime: Double; pBuffer: Pointer; BufferLen: Integer);
var
SpBmp:TBitmap;
BmpLineData:prgbtriplearray;
yCount,uCount,vCount:Integer;
Row,Col:Integer;
Ret:Integer;
begin
if CallBack.Checked then
begin
tmr1.Enabled := True;
try
xvid_encFrame.bitstream := @FrameBuf[0];
xvid_encFrame.length := BufferLen;
xvid_encFrame.input.csp := XVID_CSP_BGR; //输入是rgb位图
xvid_encFrame.input.plane[0] := pBuffer; //RGB位图数据,每个像素有3个字节,(R,G,B)
xvid_encFrame.input.stride[0] := 320 * 3; //每行字节数
{
FillChar(xvid_encStats,SizeOf(xvid_encStats),0);
FillChar(xvid_decStats,SizeOf(xvid_decStats),0);
xvid_encStats.version := xvid_Version;
xvid_decStats.version := xvid_Version;
}

//开始压缩
//Ret := xvid_encore(xvid_enc.handle, XVID_ENC_ENCODE, @xvid_encFrame,@xvid_encStats); //返回编码之后的字节
Ret := xvid_encore(xvid_enc.handle, XVID_ENC_ENCODE, @xvid_encFrame,nil); //返回编码之后的字节
//spf.Write(FrameBuf,Ret);
mmo1.Lines.Add('Codec_num=' + IntToStr(FrameSequece) + ' Len=' + IntToStr(Ret));
Inc(FrameSequece);

FrameByteCount := FrameByteCount + Ret;
//实时解码部分
if Ret > 0 then
begin
try
SpBmp := TBitmap.Create;
SpBmp.PixelFormat := pf24bit;

//初始化解码数据帧结构
xvid_decFrame.version := xvid_Version;
xvid_decFrame.general := 0;
xvid_decFrame.bitstream := @FrameBuf[0]; //输入解压的位流
xvid_decFrame.length := Ret; //输入位流长度
xvid_decFrame.output.csp := XVID_CSP_BGR; //色彩空间
xvid_decFrame.output.plane[0] := @FrameImg[0]; //解压后输出的缓冲区
xvid_decFrame.output.stride[0] := 320 * 3; //每行字节数
//ret := xvid_decore(xVid_decode.handle , XVID_DEC_DECODE, @xvid_decFrame, @xvid_decStats);
ret := xvid_decore(xVid_decode.handle , XVID_DEC_DECODE, @xvid_decFrame, nil);
if ret > 0 then
begin
try
SampleGrabber.GetBitmap(SpBmp, @FrameImg, 320*240*3); //从解码出来的RGB缓冲区中画出原始bmp位图
Image.Canvas.Draw(0,0,SpBmp);
except
end;
end;
finally
SpBmp.Free;
end;
end else begin

end;

except

end;
end;
end;

procedure TVideoForm.tmr1Timer(Sender: TObject);
begin
CurFrame := FrameSequece - CurFrame;
lbl1.Caption := 'bytes=' + IntToStr( FrameByteCount div 1024) + ' k ' + IntToStr(CurFrame) + ' fps';
CurFrame := FrameSequece;
end;

end.
阅读(1870) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~