FastLED 3.9.15
Loading...
Searching...
No Matches

◆ plm_audio_decode_header()

int fl::third_party::plm_audio_decode_header ( plm_audio_t * self)

Definition at line 3339 of file pl_mpeg.hpp.

3339 {
3340 if (!plm_buffer_has(self->buffer, 48)) {
3341 return 0;
3342 }
3343
3344 plm_buffer_skip_bytes(self->buffer, 0x00);
3345 int sync = plm_buffer_read(self->buffer, 11);
3346
3347
3348 // Attempt to resync if no syncword was found. This sucks balls. The MP2
3349 // stream contains a syncword just before every frame (11 bits set to 1).
3350 // However, this syncword is not guaranteed to not occur elsewhere in the
3351 // stream. So, if we have to resync, we also have to check if the header
3352 // (samplerate, bitrate) differs from the one we had before. This all
3353 // may still lead to garbage data being decoded :/
3354
3355 if (sync != PLM_AUDIO_FRAME_SYNC && !plm_audio_find_frame_sync(self)) {
3356 return 0;
3357 }
3358
3359 self->version = plm_buffer_read(self->buffer, 2);
3360 self->layer = plm_buffer_read(self->buffer, 2);
3361 int hasCRC = !plm_buffer_read(self->buffer, 1);
3362
3363 if (
3364 self->version != PLM_AUDIO_MPEG_1 ||
3365 self->layer != PLM_AUDIO_LAYER_II
3366 ) {
3367 return 0;
3368 }
3369
3370 int bitrate_index = plm_buffer_read(self->buffer, 4) - 1;
3371 if (bitrate_index > 13) {
3372 return 0;
3373 }
3374
3375 int samplerate_index = plm_buffer_read(self->buffer, 2);
3376 if (samplerate_index == 3) {
3377 return 0;
3378 }
3379
3380 int padding = plm_buffer_read(self->buffer, 1);
3381 plm_buffer_skip(self->buffer, 1); // f_private
3382 int mode = plm_buffer_read(self->buffer, 2);
3383
3384 // If we already have a header, make sure the samplerate, bitrate and mode
3385 // are still the same, otherwise we might have missed sync.
3386 if (
3387 self->has_header && (
3388 self->bitrate_index != bitrate_index ||
3389 self->samplerate_index != samplerate_index ||
3390 self->mode != mode
3391 )
3392 ) {
3393 return 0;
3394 }
3395
3396 self->bitrate_index = bitrate_index;
3397 self->samplerate_index = samplerate_index;
3398 self->mode = mode;
3399 self->has_header = TRUE;
3400
3401 // Parse the mode_extension, set up the stereo bound
3402 if (mode == PLM_AUDIO_MODE_JOINT_STEREO) {
3403 self->bound = (plm_buffer_read(self->buffer, 2) + 1) << 2;
3404 }
3405 else {
3406 plm_buffer_skip(self->buffer, 2);
3407 self->bound = (mode == PLM_AUDIO_MODE_MONO) ? 0 : 32;
3408 }
3409
3410 // Discard the last 4 bits of the header and the CRC value, if present
3411 plm_buffer_skip(self->buffer, 4); // copyright(1), original(1), emphasis(2)
3412 if (hasCRC) {
3413 plm_buffer_skip(self->buffer, 16);
3414 }
3415
3416 // Compute frame size, check if we have enough data to decode the whole
3417 // frame.
3418 int bitrate = PLM_AUDIO_BIT_RATE[self->bitrate_index];
3419 int samplerate = PLM_AUDIO_SAMPLE_RATE[self->samplerate_index];
3420 int frame_size = (144000 * bitrate / samplerate) + padding;
3421 return frame_size - (hasCRC ? 6 : 4);
3422}
static const int PLM_AUDIO_FRAME_SYNC
Definition pl_mpeg.hpp:3002
int plm_buffer_skip_bytes(plm_buffer_t *self, uint8_t v) FL_NOEXCEPT
Definition pl_mpeg.hpp:1050
static const int PLM_AUDIO_MODE_JOINT_STEREO
Definition pl_mpeg.hpp:3013
int plm_buffer_read(plm_buffer_t *self, int count) FL_NOEXCEPT
Definition pl_mpeg.hpp:1017
static const short PLM_AUDIO_BIT_RATE[]
Definition pl_mpeg.hpp:3022
static const int PLM_AUDIO_MODE_MONO
Definition pl_mpeg.hpp:3015
static const int PLM_AUDIO_LAYER_II
Definition pl_mpeg.hpp:3009
int plm_buffer_has(plm_buffer_t *self, size_t count) FL_NOEXCEPT
Definition pl_mpeg.hpp:998
static const int PLM_AUDIO_MPEG_1
Definition pl_mpeg.hpp:3006
static const unsigned short PLM_AUDIO_SAMPLE_RATE[]
Definition pl_mpeg.hpp:3017
int plm_audio_find_frame_sync(plm_audio_t *self) FL_NOEXCEPT
Definition pl_mpeg.hpp:3324
void plm_buffer_skip(plm_buffer_t *self, size_t count) FL_NOEXCEPT
Definition pl_mpeg.hpp:1044
#define TRUE
Definition pl_mpeg.hpp:174

References FL_NOEXCEPT, PLM_AUDIO_BIT_RATE, plm_audio_find_frame_sync(), PLM_AUDIO_FRAME_SYNC, PLM_AUDIO_LAYER_II, PLM_AUDIO_MODE_JOINT_STEREO, PLM_AUDIO_MODE_MONO, PLM_AUDIO_MPEG_1, PLM_AUDIO_SAMPLE_RATE, plm_buffer_has(), plm_buffer_read(), plm_buffer_skip(), plm_buffer_skip_bytes(), and TRUE.

Referenced by plm_audio_create_with_buffer(), plm_audio_decode(), and plm_audio_has_header().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: