Pixel Format Conversion Acceleration - BGR packed in RGB planar

From the SDK I am getting images that are in BGR pixel format, packed, i.e. BGRBGRBGR

... For another application I need to convert this format to RGB planar RRRGGGBBB

. I don't want to use an additional library for just this task, so I need to use my own code to convert between formats.

I am using C # .NET 4.5 32bit and the data in byte arrays is the same size.

Right now I iterate through the array source and assign the BGR values ​​to their respective locations in the target array, but it takes too long (250ms for a 1.3MP image). The processor on which the code is executed is Intel Atom E680 and has access to MMX, SSE, SSE2, SSE3, SSSE3.

Unfortunately I have no knowledge of intrinsics and it is not possible to convert the code for a similar problem like Fast way to copy memory with translation - ARGB to BGR my needs.

I am currently using the code to convert between pixel formats:

// the array with the BGRBGRBGR pixel data
byte[] source;
// the array with the RRRGGGBBB pixel data
byte[] result;
// the amount of pixels in one channel, width*height
int imageSize;

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3] = source[i + 2]; // R
    result[i/3 + imageSize] = source[i + 1]; // G
    result[i/3 + imageSize * 2] = source[i]; // B
}

      

I tried to split the access to the original array into three loops, one for each channel, but that didn't really help. Therefore, I am open to suggestions.

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3] = source[i + 2]; // R
}

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3 + imageSize] = source[i + 1]; // G
}

for (int i = 0; i < source.Length; i += 3)
{
    result[i/3 + imageSize * 2] = source[i]; // B
}

      

edit: I got it down to 180ms by removing division and multiplication like this, but is there a way to make it even faster? It's still very slow, which I think is because reading / writing to memory is not very optimal.

int targetPosition = 0;
int imageSize2 = imageSize * 2;
for (int i = 0; i < source.Length; i += 3)
{
    result[targetPosition] = source[i + 2]; // R
    targetPosition++;
}

targetPosition = 0;

for (int i = 0; i < source.Length; i += 3)
{
    result[targetPosition + imageSize] = source[i + 1]; // G
    targetPosition++;
}

targetPosition = 0;

for (int i = 0; i < source.Length; i += 3)
{
    result[targetPosition + imageSize2] = source[i]; // B
    targetPosition++;
}

      

Thanks to MBo's answer, I was able to cut the time from 180ms to 90ms! Here is the code:

Converter.cpp:

#include "stdafx.h"

BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) {
return  TRUE;
}

const unsigned char Mask[] = { 0, 3, 6, 9, 
                           1, 4, 7, 10, 
                           2, 5, 8, 11, 
                           12, 13, 14, 15};

extern "C" __declspec(dllexport) char* __stdcall ConvertPixelFormat(unsigned char* source, unsigned char *target, int imgSize) {

_asm {
    //interleave r1g1b1 r2g2b2 r3g3b3 r4b4g4 r5b5g5 r6... to planar
    //           r1r2r3r4r5..... g1g2g3g4g5... b1b2b3b4b5...
        push edi
        push esi
        mov eax, source      //A address
        mov edx, target      //B address
        mov ecx, imgSize
        movdqu xmm5, Mask    //load shuffling mask
        mov edi, imgSize     //load interleave step
        mov esi, eax
        add esi, edi
        add esi, edi
        add esi, edi
        shr ecx, 2           //divide count by 4
        dec ecx              //exclude last array chunk
        jle Rest

    Cycle:
        movdqu xmm0, [eax]        //load 16 bytes
        pshufb xmm0, xmm5         //shuffle bytes, we are interested in 12 ones
        movd [edx], xmm0          //store 4 bytes of R
        psrldq xmm0, 4            //shift right register, now G is on the end
        movd [edx + edi], xmm0    //store 4 bytes of G to proper place
        psrldq xmm0, 4            //do the same for B
        movd [edx + 2 * edi], xmm0
        add eax, 12               //shift source index to the next portion
        add edx, 4                //shift destination index
        loop Cycle

    Rest:                       //treat the rest of array
        cmp eax, esi
        jae Finish
        mov ecx, [eax]
        mov [edx], cl           //R
        mov [edx + edi], ch     //G
        shr ecx, 16
        mov [edx + 2 * edi], cl //B
        add eax, 3
        add edx, 1
        jmp Rest

    Finish:
        pop esi
        pop edi
    }
}

      

C # file:

// Code to define the method
[DllImport("Converter.dll")]
unsafe static extern void ConvertPixelFormat(byte* source, byte* target, int imgSize);

// Code to execute the conversion
unsafe
{
    fixed (byte* sourcePointer = &source[0])
    {
        fixed (byte* resultPointer = &result[0])
        {
            ConvertPixelFormat(sourcePointer, resultPointer, imageSize);
        }
    }
}

      

+3


source to share


4 answers


I have implemented this interleaved problem in Delphi and have looked at inline asm. I have no built-in functions, so a simple assembler is used.
 pshufb

equals _mm_shuffle_epi8

( internal SSSE3 )

At each step of the loop, I load 16 bytes (r1g1b1 r2g2b2 r3g3b3 r4b4g4 r5b5g5 r6)

into a 128-bit XMM register, shuffle them in order, (r1r2r3r4 g1g2g3g4 b1b2b3b4 xxxx)

and store r, g, b chunks into the target memory (ignoring the last 4 bytes). The next step is downloading (r5b5g5 r6g6b6 r7g7b7 ...)

and so on.

Note that for the sake of simplicity, I did not consider the tail of the array itself correctly in the first version of the code. Since you can use this code, I made the necessary corrections.



An example of the first version:
imgSize = 32
array size = 96 bytes
32/4 = 8 loops The
last loop starts at 84th byte and reads 16 bytes to 99th byte, so we have exhausted the range of arrays!
I just added the protection bytes: here GetMem(A, Size * 3 + 15);

, but for a real task this may not be applicable, so it is worth having special handling on the last array of the array.

This code takes 967ms for the pascal variant and 140ms for the asm variant to convert two hundred 1.3MHz frames on an i5-4670 machine (the processor itself is 6-8x faster for a single thread than the Atom 680). The speed is around 0.75 GB / sec (pas) and 5.4 GB / sec (ASM).

const
  Mask: array[0..15] of Byte = ( 0, 3, 6, 9,
                                 1, 4, 7, 10,
                                 2, 5, 8, 11,
                                 12, 13, 14, 15);
var
  A, B: PByteArray;
  i, N, Size: Integer;
  t1, t2: DWord;
begin
  Size := 1280 * 960 * 200;
  GetMem(A, Size * 3);
  GetMem(B, Size * 3);

  for i := 0 to Size - 1 do begin
    A[3 * i] := 1;
    A[3 * i + 1] := 2;
    A[3 * i + 2] := 3;
  end;

  t1 := GetTickCount;
  for i := 0 to Size - 1 do begin
    B[i] := A[3 * i];
    B[i + Size] := A[3 * i + 1];
    B[i + 2 * Size] := A[3 * i + 2];
  end;
  t2:= GetTickCount;

    //interleave r1g1b1 r2g2b2 r3g3b3 r4b4g4 r5b5g5 r6... to planar
    //r1r2r3r4r5..... g1g2g3g4g5... b1b2b3b4b5...
  asm
    push edi
    push esi
    mov eax, A      //A address
    mov edx, B      //B address
    mov ecx, Size
    movdqu xmm5, Mask   //load shuffling mask
    mov edi, Size       //load interleave step
    mov esi, eax
    add esi, edi
    add esi, edi
    add esi, edi
    shr ecx, 2      //divide count by 4
    dec ecx         //exclude last array chunk
    jle @@Rest

  @@Cycle:
    movdqu xmm0, [eax]   //load 16 bytes
    pshufb xmm0, xmm5    //shuffle bytes, we are interested in 12 ones
    movd [edx], xmm0     //store 4 bytes of R
    psrldq xmm0, 4        //shift right register, now G is on the end
    movd [edx + edi], xmm0   //store 4 bytes of G to proper place
    psrldq xmm0, 4            //do the same for B
    movd [edx + 2 * edi], xmm0
    add eax, 12               //shift source index to the next portion
    add edx, 4                //shift destination index
    loop @@Cycle

   @@Rest:       //treat the rest of array
    cmp eax, esi
    jae @@Finish
    mov ecx, [eax]
    mov [edx], cl   //R
    mov [edx + edi], ch  //G
    shr ecx, 16
    mov [edx + 2 * edi], cl //B
    add eax, 3
    add edx, 1
    jmp @@Rest
  @@Finish:

    pop esi
    pop edi
  end;

  Memo1.Lines.Add(Format('pas %d asm %d', [t2-t1, GetTickCount - t2]));
  FreeMem(A);
  FreeMem(B);

      

+1


source


You can try to count backwards i.e. int i = source.Length - 1; i >=0 ; i -= 3

, so the property source.Length

is read only once per loop, not at every iteration.



0


source


I followed Ivan's advice and came up with this improvement that gets rid of division (implemented in C):

    int offset = 0;
    for (int i = 0; i < ARRAYSIZE(source); i += 3) {
        offset++;
        result[offset] = source[i + 2];  // R
        result[offset + imageSize] = source[i + 1];  // G
        result[offset + imageSize * 2] = source[i];  // B
    }

      

this saves about 40% of the execution time on my machine.

0


source


First step: avoid reading the source multiple times (see answer fooobar.com/questions/2185866 / ... ). It also plays well with the processor cache which is currently underutilized: you are reading 1 byte out of 3, so 2 / 3ds cache lines are thrown away. So it might look like this:

int targetPositionR = 0;
int targetPositionG = imageSize;
int targetPositionB = imageSize * 2;
for (int i = 0; i < source.Length; i += 3)
{
    result[targetPositionB] = source[i]; // B
    result[targetPositionG] = source[i + 1]; // G
    result[targetPositionR] = source[i + 2]; // R
    targetPositionB++;
    targetPositionG++;
    targetPositionR++;
}

      

Second step: writes 4 bytes at a time, not 1 byte. However, this requires an extra buffer and a copy:

int[] dwPlanar = new int[imageSize*3/4];
int targetPositionR = 0;
int targetPositionG = imageSize / 4;
int targetPositionB = imageSize * 2 / 4;
for (int i = 0; i < source.Length; i += 12)
{
    int dwB = (source[i  ]) | (source[i+3] << 8) | (source[i+6] << 16) | (source[i+9]  << 24);
    int dwG = (source[i+1]) | (source[i+4] << 8) | (source[i+7] << 16) | (source[i+10] << 24);
    int dwR = (source[i+2]) | (source[i+5] << 8) | (source[i+8] << 16) | (source[i+11] << 24);
    dwPlanar[targetPositionB] = dwB; // B
    dwPlanar[targetPositionG] = dwG; // G
    dwPlanar[targetPositionR] = dwR; // R
    targetPositionB++;
    targetPositionG++;
    targetPositionR++;
}
Buffer.BlockCopy(dwPlanar,0,result,0,imageSize * 3);

      

This will help, I suppose, because C # will do fewer array bounds checks and it is generally better to write larger chunks whenever possible.

(Disclaimer: I'm not familiar with C # and I don't know if this code will even compile, it is just an algorithm).

0


source







All Articles