简体   繁体   English

将位填充(位移?)添加到存储在字节数组中的 10 位值

[英]Add bit padding (bit shifting?) to 10bit values stored in a byte array

I'm looking for an efficient way to bit shift left ( << ) 10 bit values that are stored within a byte array using C++/Win32.我正在寻找一种有效的方法来左移( << )10 位值,这些值使用 C++/Win32 存储在字节数组中。

I am receiving an uncompressed 4:2:2 10 bit video stream via UDP, the data is stored within an unsigned char array due to the packaging of the bits.我通过 UDP 接收未压缩的 4:2:2 10 位视频 stream,由于位的打包,数据存储在无符号字符数组中。

The data is always sent so that groups of pixels finish on a byte boundary (in this case, 4 pixels sampled at a bit-depth of 10 use 5 bytes):始终发送数据,以便像素组在字节边界上完成(在这种情况下,以 10 位深度采样的 4 个像素使用 5 个字节):

位包装

The renderer I am using ( Media Foundation Enhanced Video Renderer ) requires that 10 bit values are placed into a 16 bit WORD with 6 padding bits to the right , whilst this is annoying I assume it's to help them ensure a 1-byte memory alignment:我正在使用的渲染器( Media Foundation Enhanced Video Renderer )要求将 10 位值放入16 位 WORD 中,右侧有 6 个填充位,虽然这很烦人,但我认为这是为了帮助他们确保 1 字节 memory alignment:

带填充的 10 位表示

What is an efficient way of left shifting each 10 bit value 6 times (and moving to a new array if needed)?将每个 10 位值左移 6 次(并在需要时移动到新数组)的有效方法是什么? Although I will be receiving varying lengths of data, they will always be comprised of these 40 bit blocks.虽然我会收到不同长度的数据,但它们总是由这 40 位块组成。

I'm sure a crude loop would suffice with some bit-masking(?) but that sounds expensive to me and I have to process 1500 packets/second, each with ~1200 bytes of payload.我确信一个粗略的循环可以满足一些位掩码(?),但这对我来说听起来很昂贵,我必须处理 1500 个数据包/秒,每个数据包有大约 1200 字节的有效负载。

Edit for clarity为清晰起见进行编辑

Example Input:示例输入:

unsigned char byteArray[5] = {0b01110101, 0b01111010, 0b00001010, 0b11111010, 0b00000110}

Desired Output:所需的 Output:

WORD wordArray[4] = {0b0111010101000000, 0b1110100000000000, 0b1010111110000000, 0b1000000110000000}

(or the same resulting data in a byte array) (或字节数组中的相同结果数据)

This does the job:这可以完成工作:

void ProcessPGroup(const uint8_t byteArrayIn[5], uint16_t twoByteArrayOut[4])
{
    twoByteArrayOut[0] = (((uint16_t)byteArrayIn[0] & 0b11111111u) << (0 + 8)) | (((uint16_t)byteArrayIn[1] & 0b11000000u) << 0);
    twoByteArrayOut[1] = (((uint16_t)byteArrayIn[1] & 0b00111111u) << (2 + 8)) | (((uint16_t)byteArrayIn[2] & 0b11110000u) << 2);
    twoByteArrayOut[2] = (((uint16_t)byteArrayIn[2] & 0b00001111u) << (4 + 8)) | (((uint16_t)byteArrayIn[3] & 0b11111100u) << 4);
    twoByteArrayOut[3] = (((uint16_t)byteArrayIn[3] & 0b00000011u) << (6 + 8)) | (((uint16_t)byteArrayIn[4] & 0b11111111u) << 6);
}

Don't be confused by the [5] and [4] values in the function signature above.不要被上面 function 签名中的[5][4]值混淆。 They don't do anything except tell you, the user, that that is the mandatory, expected number of elements in each array.他们什么都不做,只是告诉你,用户,这是每个数组中强制性的、预期的元素数量。 See my answer here on this: Passing an array as an argument to a function in C .请在此处查看我的答案: 将数组作为参数传递给 C 中的 function Passing an array that is shorter will result in undefined behavior and is a bug!传递更短的数组将导致未定义的行为并且是一个错误!

Full test code (download it here ):完整的测试代码( 在这里下载):

test.cpp测试.cpp

/*

GS
17 Mar. 2021

To compile and run:
    mkdir -p bin && g++ -Wall -Wextra -Werror -ggdb -std=c++17 -o bin/test \
    test.cpp && bin/test

*/

#include <bitset>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iostream>

// Get the number of elements in any C array
// - Usage example: [my own answer]:
//   https://arduino.stackexchange.com/questions/80236/initializing-array-of-structs/80289#80289
#define ARRAY_LEN(array) (sizeof(array)/sizeof(array[0]))

/// \brief      Process a packed video P group, which is 4 pixels of 10 bits each (exactly 5 uint8_t
///             bytes) into a uint16_t 4-element array (1 element per pixel).
/// \details    Each group of 10-bits for a pixel will be placed into a 16-bit word, with all 10
///             bits left-shifted to the far left edge, leaving 6 empty (zero) bits in the right
///             side of the word.
/// \param[in]  byteArrayIn  5 bytes of 10-bit pixel data for exactly 4 pixels; any array size < 5
///                        will result in undefined behavior! So, ensure you pass the proper array
///                        size in!
/// \param[out] twoByteArrayOut  The output array into which the 4 pixels will be packed, 10 bits per
///                        16-bit word, all 10 bits shifted to the left edge; any array size < 4
///                        will result in undefined behavior!
/// \return     None
void ProcessPGroup(const uint8_t byteArrayIn[5], uint16_t twoByteArrayOut[4])
{
    twoByteArrayOut[0] = (((uint16_t)byteArrayIn[0] & 0b11111111u) << (0 + 8)) | (((uint16_t)byteArrayIn[1] & 0b11000000u) << 0);
    twoByteArrayOut[1] = (((uint16_t)byteArrayIn[1] & 0b00111111u) << (2 + 8)) | (((uint16_t)byteArrayIn[2] & 0b11110000u) << 2);
    twoByteArrayOut[2] = (((uint16_t)byteArrayIn[2] & 0b00001111u) << (4 + 8)) | (((uint16_t)byteArrayIn[3] & 0b11111100u) << 4);
    twoByteArrayOut[3] = (((uint16_t)byteArrayIn[3] & 0b00000011u) << (6 + 8)) | (((uint16_t)byteArrayIn[4] & 0b11111111u) << 6);
}

// Reference: https://stackoverflow.com/questions/7349689/how-to-print-using-cout-a-number-in-binary-form/7349767
void PrintArrayAsBinary(const uint16_t* twoByteArray, size_t len)
{
    std::cout << "{\n";
    for (size_t i = 0; i < len; i++)
    {
        std::cout << std::bitset<16>(twoByteArray[i]);
        if (i < len - 1)
        {
            std::cout << ",";
        }
        std::cout << std::endl;
    }
    std::cout << "}\n";
}

int main()
{
    printf("Processing 10-bit video data example\n");

    constexpr uint8_t TEST_BYTE_ARRAY_INPUT[5] = {0b01110101, 0b01111010, 0b00001010, 0b11111010, 0b00000110};
    constexpr uint16_t TEST_TWO_BYTE_ARRAY_OUTPUT[4] = {
        0b0111010101000000, 0b1110100000000000, 0b1010111110000000, 0b1000000110000000};

    uint16_t twoByteArrayOut[4];
    ProcessPGroup(TEST_BYTE_ARRAY_INPUT, twoByteArrayOut);

    if (std::memcmp(twoByteArrayOut, TEST_TWO_BYTE_ARRAY_OUTPUT, sizeof(TEST_TWO_BYTE_ARRAY_OUTPUT)) == 0)
    {
        printf("TEST PASSED!\n");
    }
    else
    {
        printf("TEST ==FAILED!==\n");

        std::cout << "expected = \n";
        PrintArrayAsBinary(TEST_TWO_BYTE_ARRAY_OUTPUT, ARRAY_LEN(TEST_TWO_BYTE_ARRAY_OUTPUT));

        std::cout << "actual = \n";
        PrintArrayAsBinary(twoByteArrayOut, ARRAY_LEN(twoByteArrayOut));
    }

    return 0;
}

Sample run and output:样品运行和 output:

$ mkdir -p bin && g++ -Wall -Wextra -Werror -ggdb -std=c++17 \
-o bin/test test.cpp && bin/test
Processing 10-bit video data example
TEST PASSED!

I've now also placed this code into my eRCaGuy_hello_world repo here: cpp/process_10_bit_video_data.cpp .我现在也将此代码放入我的eRCaGuy_hello_world 存储库中: cpp/process_10_bit_video_data.cpp

References:参考:

  1. How to print (using cout) a number in binary form? 如何打印(使用 cout)二进制形式的数字?
  2. [my answer] Passing an array as an argument to a function in C [我的回答] 将数组作为参数传递给 C 中的 function
  3. [my eRCaGuy_hello_world repo] ARRAY_LEN() macro: see utilities.h [我的eRCaGuy_hello_world 存储库] ARRAY_LEN()宏:参见实用程序.h
  4. https://en.cppreference.com/w/cpp/string/byte/memcmp https://en.cppreference.com/w/cpp/string/byte/memcmp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM