-2

I have a 10 bit SDI stream, when I receive it, it will be stored into uint8_t *buffer and off course when I read it I get completely different value from what expected, except for the first:

10Bit -> 00 0000 0001 | 00 0101 1010 → Hex: A5 10

8 Bit -> 0000 | 0000 0100 | 0101 1010 → Hex: A5 40

is there a function I can use to map it correctly? (C++ style)

If it does not exist, how do I implement it?

mattobob
  • 833
  • 4
  • 15
  • 37
  • the 10-bit values are `001 05A`, the 8-bit ones are `0 04 5A`, how do you get `A5 10` and `A5 80`? – phuclv Aug 01 '16 at 09:34
  • *is there a function I can use to map it correctly? (C++ style)* Not that I'm aware of. You're likely to have to write one yourself. This question is similar: http://stackoverflow.com/questions/11680248/reading-a-binary-file-bit-by-bit – Andrew Henle Aug 01 '16 at 09:41
  • Yes sorry, i read it from right to left because the first value the is coming from the stream is the right one, and off course 8Bit value will be 4 and not 8, i wil correct it, but the problem does not change, I read wrong values even if it is the opposite way around, is there a function to convert it? – mattobob Aug 01 '16 at 09:43
  • 2
    You are asking: I have these 3 colors, how can i map them to these 2 other colors. Well you can't without loosing some information. If you can guarantee that one color does not appear (the upper 2 bits being used) then you could just cut them off. Or you can divide the number you get from the 10 bits by / 4 you could map them at the loss of some precision. – RedX Aug 01 '16 at 11:27
  • 1
    Do you mean a) you want to discard 2 bits from each element and store it in 8 bits? Or do you mean b) you want to store the 10 bits in a 16-bit array so that when you access the first array element you get the first 10-bit value from your original stream and when you access the second array element you get the second 10-bit value from your original stream? a) involves loss of accuracy/resolution, b) involves padding/bit-twiddling. – Mark Setchell Aug 01 '16 at 11:35
  • It is a SDI stream coded starting from a uint8_t *buffer, it has been mapped into a 10 Bit buffer because of the SDI standard, there is no extra information, the two extra bit (from 10bit buffer) are the first two bit of the next element (8 bit buffer) so it must be remapped into a uint8_t, i want to remap it into a uint8_t without loss of information, mantianing the original structure. – mattobob Aug 01 '16 at 11:53

1 Answers1

1

Basically you need to use fread() with the correct parameters to read exactly 5 bytes, i.e. 40 bits, into a temporary buffer. You use that amount because it corresponds to a whole number of bytes on the input stream and also a whole number of output bytes on the output stream.

You then use left and right SHIFTs (<< and >>) and bitwise masks (&) to extract the 5 output bytes and put them in your uint8_t buffer.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432