MAD Decoder: Support changing format

Support the weird scenario of format changes mid-stream. Probably
highly unlikely, and likely to break things if it does occur, but
whatever, it might actually happen in some weird file.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
CQTexperiment
Christopher Snowhill 2022-02-26 01:43:54 -08:00
parent 53209485c0
commit ca3dec4497
1 changed files with 44 additions and 0 deletions

View File

@ -584,9 +584,46 @@
return 1;
}
- (BOOL)syncFormat:(BOOL)updateNow {
float _sampleRate = _frame.header.samplerate;
int _channels = MAD_NCHANNELS(&_frame.header);
int _layer = 3;
switch(_frame.header.layer) {
case MAD_LAYER_I:
_layer = 1;
break;
case MAD_LAYER_II:
_layer = 2;
break;
case MAD_LAYER_III:
_layer = 3;
break;
default:
break;
}
BOOL changed = (_sampleRate != sampleRate ||
_channels != channels ||
_layer != layer);
if(changed && updateNow) {
sampleRate = _sampleRate;
channels = _channels;
layer = _layer;
[self willChangeValueForKey:@"properties"];
[self didChangeValueForKey:@"properties"];
}
return changed;
}
- (int)readAudio:(void *)buffer frames:(UInt32)frames {
int framesRead = 0;
[self syncFormat:YES];
for(;;) {
long framesRemaining = frames - framesRead;
long framesToCopy = (_outputFrames > framesRemaining ? framesRemaining : _outputFrames);
@ -614,6 +651,13 @@
[self writeOutput];
// DLog(@"Wrote output");
if([self syncFormat:NO]) {
if(framesRead)
break;
else
[self syncFormat:YES];
}
}
[self updateMetadata];