delphi版的YV12toRGB,针对海康视频服务器视频解码。

网上有C#,JAVA,C++版本的YV12toRGB算法代码,笔者因项目需要,特根据网上资料综合整改后,成功转换成了DELPHI版的。

关于海康视频服务器SDK以及开发资料,请查阅海康官方网站。

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;       //这个地方是关键,网上原代码(C#版)是没有红色部份的,结果转出来的图像是倒的、反的
            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])));
            end;

        end;
    end;
    result:=true;

end;

 

结合海康视频服务器视频解码调用示例:

 


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)));
    //设定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);  //缓存要足够大,不然高清图片处理会报错
    SetStretchBltMode(Bmp.Canvas.Handle,COLORONCOLOR);
    try
      Application.ProcessMessages;
      if (nSize>0) then YV12_To_RGB(pBuf,pFrameInfo.nWidth,pFrameInfo.nHeight,pRGBBuf);
      //实时显示视频流
      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
    //内存流处理
    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    //试版本不同,调用有所不同
         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;

 

结构体说明:

 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;

 

 

 

yv12rgb是一种像素格式的转换方法,它将YUV颜色空间中的图像数据转换为RGB颜色空间中的图像数据。在OpenCV中,可以使用以下源代码实现yv12rgb的操作。 ```c++ #include <opencv2/opencv.hpp> void yv12ToRgb(const unsigned char* y, const unsigned char* u, const unsigned char* v, int width, int height, unsigned char* rgb) { cv::Mat yMat = cv::Mat(height, width, CV_8UC1, const_cast<unsigned char*>(y)); cv::Mat uMat = cv::Mat(height / 2, width / 2, CV_8UC1, const_cast<unsigned char*>(u)); cv::Mat vMat = cv::Mat(height / 2, width / 2, CV_8UC1, const_cast<unsigned char*>(v)); cv::Mat yuvMat; cv::merge(std::vector<cv::Mat>{yMat, uMat, vMat}, yuvMat); cv::cvtColor(yuvMat, yuvMat, cv::COLOR_YUV2RGB_I420); cv::Mat rgbMat; cv::cvtColor(yuvMat, rgbMat, cv::COLOR_YUV2BGR); memcpy(rgb, rgbMat.data, width * height * 3); } int main() { int width = 640; int height = 480; unsigned char* yData = new unsigned char[width * height]; unsigned char* uData = new unsigned char[width * height / 4]; unsigned char* vData = new unsigned char[width * height / 4]; // 假设已经将y、u、v数据填充到了相应的数组中 unsigned char* rgbData = new unsigned char[width * height * 3]; yv12ToRgb(yData, uData, vData, width, height, rgbData); // 处理rgbData,可以将其写入文件或进行其他操作 delete[] yData; delete[] uData; delete[] vData; delete[] rgbData; return 0; } ``` 上述代码中,`yv12ToRgb`函数接受原始的Y、U、V分量数据以及图像的宽度和高度,将其转换为RGB格式,并将结果存储在`rgb`数组中。`main`函数中的测试代码展示了如何使用该函数进行yv12rgb的操作。注意,在使用完数组后使用`delete[]`进行内存释放,以防止内存泄漏。 以上是利用OpenCV的源码实现yv12rgb的方法,可以根据具体的使用场景进行相应的调整和优化。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值