Reputation: 27
For working with the Opus audio codec, I need to parse a TOC byte from a char buffer and extract the config, s and c segments, each representing a number.
I would need to store these numbers into separate variables for further use and am wondering what the best way to achieve this (in C++) is.
A well-formed Opus packet MUST contain at least one byte [R1].
This byte forms a table-of-contents (TOC) header:
0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+
| config |s| c |
+-+-+-+-+-+-+-+-+
The top five bits of the TOC byte, labeled "config", encode one of 32 possible configurations of operating mode, audio bandwidth, and frame size.
One additional bit, labeled "s", signals mono vs. stereo, with 0 indicating mono and 1 indicating stereo.
The remaining two bits of the TOC byte, labeled "c", code the number of frames per packet (codes 0 to 3).
This format is according to the RFC found here: https://www.rfc-editor.org/rfc/rfc6716#section-3.1
Thanks!
Upvotes: 0
Views: 1034
Reputation: 60923
Just use a right shift (>>
) to shift the bits to the low bits if needed, and a bitwise and (&
) to mask out the other bits.
int config = (toc >> 3) & 31;
int s = (toc >> 2) & 1;
int c = toc & 3;
Upvotes: 2