網(wǎng)上有C#,JAVA,C++版本的YV12toRGB算法代碼,筆者因項(xiàng)目需要,,特根據(jù)網(wǎng)上資料綜合整改后,成功轉(zhuǎn)換成了DELPHI版的,。 關(guān)于??狄曨l服務(wù)器SDK以及開(kāi)發(fā)資料,請(qǐng)查閱??倒俜骄W(wǎng)站,。 function YV12_To_RGB(yv12:pchar;nWidth,nHeight:integer;var rgb24:pchar):boolean; const Table_fv1:array[0..255] of integer=( -180, -179, -177, -176, -174, -173, -172, -170, -169, -167, -166, -165, -163, -162, -160, -159, -158, -156, -155, -153, -152, -151, -149, -148, -146, -145, -144, -142, -141, -139, -138, -137, -135, -134, -132, -131, -130, -128, -127, -125, -124, -123, -121, -120, -118, -117, -115, -114, -113, -111, -110, -108, -107, -106, -104, -103, -101, -100, -99, -97, -96, -94, -93, -92, -90, -89, -87, -86, -85, -83, -82, -80, -79, -78, -76, -75, -73, -72, -71, -69, -68, -66, -65, -64, -62, -61, -59, -58, -57, -55, -54, -52, -51, -50, -48, -47, -45, -44, -43, -41, -40, -38, -37, -36, -34, -33, -31, -30, -29, -27, -26, -24, -23, -22, -20, -19, -17, -16, -15, -13, -12, -10, -9, -8, -6, -5, -3, -2, 0, 1, 2, 4, 5, 7, 8, 9, 11, 12, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 28, 29, 30, 32, 33, 35, 36, 37, 39, 40, 42, 43, 44, 46, 47, 49, 50, 51, 53, 54, 56, 57, 58, 60, 61, 63, 64, 65, 67, 68, 70, 71, 72, 74, 75, 77, 78, 79, 81, 82, 84, 85, 86, 88, 89, 91, 92, 93, 95, 96, 98, 99, 100, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 124, 126, 127, 129, 130, 131, 133, 134, 136, 137, 138, 140, 141, 143, 144, 145, 147, 148, 150, 151, 152, 154, 155, 157, 158, 159, 161, 162, 164, 165, 166, 168, 169, 171, 172, 173, 175, 176, 178 ); Table_fv2:array[0..255] of integer=( -92, -91, -91, -90, -89, -88, -88, -87, -86, -86, -85, -84, -83, -83, -82, -81, -81, -80, -79, -78, -78, -77, -76, -76, -75, -74, -73, -73, -72, -71, -71, -70, -69, -68, -68, -67, -66, -66, -65, -64, -63, -63, -62, -61, -61, -60, -59, -58, -58, -57, -56, -56, -55, -54, -53, -53, -52, -51, -51, -50, -49, -48, -48, -47, -46, -46, -45, -44, -43, -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35, -34, -33, -33, -32, -31, -31, -30, -29, -28, -28, -27, -26, -26, -25, -24, -23, -23, -22, -21, -21, -20, -19, -18, -18, -17, -16, -16, -15, -14, -13, -13, -12, -11, -11, -10, -9, -8, -8, -7, -6, -6, -5, -4, -3, -3, -2, -1, 0, 0, 1, 2, 2, 3, 4, 5, 5, 6, 7, 7, 8, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 22, 23, 24, 25, 25, 26, 27, 27, 28, 29, 30, 30, 31, 32, 32, 33, 34, 35, 35, 36, 37, 37, 38, 39, 40, 40, 41, 42, 42, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 50, 51, 52, 52, 53, 54, 55, 55, 56, 57, 57, 58, 59, 60, 60, 61, 62, 62, 63, 64, 65, 65, 66, 67, 67, 68, 69, 70, 70, 71, 72, 72, 73, 74, 75, 75, 76, 77, 77, 78, 79, 80, 80, 81, 82, 82, 83, 84, 85, 85, 86, 87, 87, 88, 89, 90, 90 ); Table_fu1:array[0..255] of integer=( -44, -44, -44, -43, -43, -43, -42, -42, -42, -41, -41, -41, -40, -40, -40, -39, -39, -39, -38, -38, -38, -37, -37, -37, -36, -36, -36, -35, -35, -35, -34, -34, -33, -33, -33, -32, -32, -32, -31, -31, -31, -30, -30, -30, -29, -29, -29, -28, -28, -28, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24, -24, -23, -23, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18, -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -11, -11, -11, -10, -10, -10, -9, -9, -9, -8, -8, -8, -7, -7, -7, -6, -6, -6, -5, -5, -5, -4, -4, -4, -3, -3, -3, -2, -2, -2, -1, -1, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 28, 28, 28, 29, 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 36, 36, 36, 37, 37, 37, 38, 38, 38, 39, 39, 39, 40, 40, 40, 41, 41, 41, 42, 42, 42, 43, 43 ); Table_fu2:array[0..255] of integer=( -227, -226, -224, -222, -220, -219, -217, -215, -213, -212, -210, -208, -206, -204, -203, -201, -199, -197, -196, -194, -192, -190, -188, -187, -185, -183, -181, -180, -178, -176, -174, -173, -171, -169, -167, -165, -164, -162, -160, -158, -157, -155, -153, -151, -149, -148, -146, -144, -142, -141, -139, -137, -135, -134, -132, -130, -128, -126, -125, -123, -121, -119, -118, -116, -114, -112, -110, -109, -107, -105, -103, -102, -100, -98, -96, -94, -93, -91, -89, -87, -86, -84, -82, -80, -79, -77, -75, -73, -71, -70, -68, -66, -64, -63, -61, -59, -57, -55, -54, -52, -50, -48, -47, -45, -43, -41, -40, -38, -36, -34, -32, -31, -29, -27, -25, -24, -22, -20, -18, -16, -15, -13, -11, -9, -8, -6, -4, -2, 0, 1, 3, 5, 7, 8, 10, 12, 14, 15, 17, 19, 21, 23, 24, 26, 28, 30, 31, 33, 35, 37, 39, 40, 42, 44, 46, 47, 49, 51, 53, 54, 56, 58, 60, 62, 63, 65, 67, 69, 70, 72, 74, 76, 78, 79, 81, 83, 85, 86, 88, 90, 92, 93, 95, 97, 99, 101, 102, 104, 106, 108, 109, 111, 113, 115, 117, 118, 120, 122, 124, 125, 127, 129, 131, 133, 134, 136, 138, 140, 141, 143, 145, 147, 148, 150, 152, 154, 156, 157, 159, 161, 163, 164, 166, 168, 170, 172, 173, 175, 177, 179, 180, 182, 184, 186, 187, 189, 191, 193, 195, 196, 198, 200, 202, 203, 205, 207, 209, 211, 212, 214, 216, 218, 219, 221, 223, 225 ); var nYLen,nHfWidth, i, j, m, n, x, y,rdif, invgdif, bdif,py:integer; yData,vData,uData:pchar; rgb:array[0..2] of integer; addHalf:boolean; begin nYLen:=nWidth*nHeight; nHfWidth:=nWidth shr 1; if (nYLen<1) or (nHfWidth<1) then result:=false; yData:=yv12; vData:=@yData[nYLen]; uData:=@vData[nYLen shr 2]; m := - nWidth; n := - nHfWidth; addHalf := true; for y:=0 to nHeight-1 do begin m :=m+nWidth; if addHalf then begin n:=n+nHfWidth; addHalf := false; end else begin addHalf := true; end; for x:=0 to nWidth-1 do begin i := m + x; j := n + (x shr 1); py := integer(byte(yData[i])); rdif := Table_fv1[integer(byte(vData[j]))]; invgdif := Table_fu1[integer(byte(uData[j]))] + Table_fv2[integer(byte(vData[j]))]; bdif := Table_fu2[integer(byte(uData[j]))]; rgb[2] := py + rdif; rgb[1] := py - invgdif; rgb[0] := py + bdif; j :=nYLen-nWidth- m + x; //這個(gè)地方是關(guān)鍵,網(wǎng)上原代碼(C#版)是沒(méi)有紅色部份的,,結(jié)果轉(zhuǎn)出來(lái)的圖像是倒的,、反的 i := (j shl 1) + j; for j:=0 to 2 do begin // rgb24[i+j]:=char(ifthen((rgb[j] < 0),0,ifthen((rgb[j] > 255),255,rgb[j]))); //這里上面那行怪怪的,改了一下 if (rgb[j]>=0) and (rgb[j]<=255) then rgb24[i+j]:=Char(rgb[j]); if rgb[j]<0 then rgb24[i+j]:=Char(0); if rgb[j]>255 then rgb24[i+j]:=Char(255); end; end; end; result:=true; end; 結(jié)合??狄曨l服務(wù)器視頻解碼調(diào)用示例: procedure TDecCBFun(nPort:LongInt;pBuf:pchar;nSize:dword;pFrameInfo:PHik_FRAME_INFO;nReserved1,nReserved2:LongInt); stdcall; var Bmp: TBitmap; pBMIInfo: PBITMAPINFO; pRGBBuf:pchar; begin pBMIInfo := AllocMem(sizeof(TBITMAPINFO)+ (255 * sizeof(TRGBQuad))); //設(shè)定BMP的頭信息 pBMIInfo^.bmiHeader.biSize := sizeof(BITMAPINFOHEADER); pBMIInfo^.bmiHeader.biWidth := pFrameInfo.nWidth; pBMIInfo^.bmiHeader.biHeight := pFrameInfo.nHeight; pBMIInfo^.bmiHeader.biPlanes := 1; pBMIInfo^.bmiHeader.biBitCount := 24; pBMIInfo^.bmiHeader.biCompression := BI_RGB; pBMIInfo^.bmiHeader.biSizeImage := 0; pBMIInfo^.bmiHeader.biXPelsPerMeter := 0; pBMIInfo^.bmiHeader.biYPelsPerMeter := 0; pBMIInfo^.bmiHeader.biClrUsed := 0; pBMIInfo^.bmiHeader.biClrImportant := 0;
Bmp := TBitmap.Create; Bmp.Width:= pFrameInfo.nWidth; Bmp.Height := pFrameInfo.nHeight; Bmp.PixelFormat := pf24bit; g_RecogImage_Heigh:=pFrameInfo.nHeight; g_RecogImage_Width:=pFrameInfo.nWidth; pRGBBuf :=AllocMem(40000000); //緩存要足夠大,,不然高清圖片處理會(huì)報(bào)錯(cuò) SetStretchBltMode(Bmp.Canvas.Handle,COLORONCOLOR); try Application.ProcessMessages; if (nSize>0) then YV12_To_RGB(pBuf,pFrameInfo.nWidth,pFrameInfo.nHeight,pRGBBuf); //實(shí)時(shí)顯示視頻流 SetDIBitsToDevice(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, 0, 0, 0, pBMIInfo^.bmiHeader.biHeight, pRGBBuf, pBMIInfo^, DIB_RGB_COLORS);
finally FreeMem(pRGBBuf); FreeMem(pBMIInfo); Bmp.Free; end; end; procedure TRealDataCallBack(lRealHandle:longint;dwDataType:DWORD; pBuffer:pBYTE; dwBufSize:DWORD; dwUser:DWORD);stdcall; begin //內(nèi)存流處理 if dwDataType=NET_DVR_SYSHEAD then begin if not PlayM4_GetPort(lPort) then begin exit; end; if (dwBufSize>0) then begin if not PlayM4_SetStreamOpenMode(lPort,STREAME_REALTIME) then begin exit; end; if not PlayM4_OpenStream(lPort,pBuffer,dwBufSize,1024*1024) then begin exit; end; end; end; if dwDataType= NET_DVR_STREAMDATA then begin if (dwBufSize>0) and (lPort<>-1) then begin if not PlayM4_SetDecCBStream(lPort,1) then begin exit; end; end; if not PlayM4_SetDecCallBackEx(lPort,@TDecCBFun,nil,0) then //試版本不同,調(diào)用有所不同 begin exit; end; end; end; procedure InitHik(var Hv: ArrayHikVedio); var nHvLen,i,nRealHandle:Integer; CLIENTINFO:array of NET_DVR_CLIENTINFO; DEVICEINFO:array of NET_DVR_DEVICEINFO; begin NET_DVR_Init(); NET_DVR_SetConnectTime(2000,1); NET_DVR_SetReconnect(10000, true); Setlength(DEVICEINFO,2); Setlength(CLIENTINFO,2); nHvLen:=length(Hv); for i:=0 to nHvLen-1 do begin if hv[i].sIP <>'' then begin Hv[i].nUserID:=NET_DVR_Login(pCHAR(Hv[i].sIP),Hv[i].nPort,pchar(Hv[i].sUser),pchar(Hv[i].sPower),@DEVICEINFO[i]); if (Hv[i].nUserID<0) then begin NET_DVR_Cleanup(); break; end; CLIENTINFO[i].hPlayWnd :=Hv[i].panShow.Handle; CLIENTINFO[i].lChannel :=Hv[i].nWay; CLIENTINFO[i].lLinkMode :=0; nRealHandle:=NET_DVR_RealPlay(Hv[i].nUserID,@CLIENTINFO[i]); Hv[i].nRealHandle:=nRealHandle; NET_DVR_SetRealDataCallBack(nRealHandle,@TRealDataCallBack,Hv[i].nUserID); end; end; end; 結(jié)構(gòu)體說(shuō)明: THikVedio=Record sIP:string; sUser:string; sPower:string; nPort:integer; nWay:integer; nUserID:integer; nRealHandle:integer; panShow:TPanel;//用于顯示圖像 imgShow:TImage;//顯示圖片 end; ArrayHikVedio= array of THikVedio;
|