Commit fc06d761 authored by Mans Rullgard's avatar Mans Rullgard

mqadec: detect correct output sample rate

The output rate returned by ssc_decode_open can be bogus for some files.
Detect the actual rate from the number of output samples instead.
parent 68545393
......@@ -36,11 +36,17 @@
#define BUF_SIZE 4096
static SNDFILE *infile;
static SF_INFO infmt;
static uint8_t buf[BUF_SIZE];
static int buf_pos;
static int buf_end;
static int samples_in;
static int samples_out;
static const char *outname;
static SNDFILE *outfile;
static SF_INFO outfmt;
static int get_samples(int frame_size, uint8_t **samples, int eof, int *end)
{
......@@ -85,6 +91,7 @@ static int get_samples(int frame_size, uint8_t **samples, int eof, int *end)
static void consume(int len)
{
buf_pos += len;
samples_in += len / (4 * infmt.channels);
}
static size_t write_samples(void *p, void *buf, size_t len)
......@@ -92,6 +99,24 @@ static size_t write_samples(void *p, void *buf, size_t len)
int32_t *s = buf;
int i;
samples_out += len;
if (!outfile) {
int rs;
for (rs = 0; samples_out > samples_in << rs; rs++);
outfmt.samplerate = infmt.samplerate << rs;
outfmt.channels = infmt.channels;
outfmt.format = SF_FORMAT_WAV | SF_FORMAT_PCM_24;
outfile = sf_open(outname, SFM_WRITE, &outfmt);
if (!outfile) {
fprintf(stderr, "%s: %s\n", outname, sf_strerror(NULL));
exit(1);
}
}
for (i = 0; i < len * 2; i++)
s[i] <<= 8;
......@@ -106,8 +131,6 @@ static int write_size(void *p)
int main(int argc, char **argv)
{
struct ssc_decode *sscd;
SF_INFO infmt;
SF_INFO outfmt;
int dummy = 0;
int channels = 2;
int rate1 = 0;
......@@ -145,6 +168,7 @@ int main(int argc, char **argv)
rate1 = infmt.samplerate;
channels = infmt.channels;
outname = argv[1];
sscd = ssc_decode_open(&channels, &rate1, &bits, rate2,
get_samples, consume, write_samples,
......@@ -156,17 +180,6 @@ int main(int argc, char **argv)
return 1;
}
memset(&outfmt, 0, sizeof(outfmt));
outfmt.samplerate = rate1;
outfmt.channels = channels;
outfmt.format = SF_FORMAT_WAV | SF_FORMAT_PCM_24;
outfile = sf_open(argv[1], SFM_WRITE, &outfmt);
if (!outfile) {
fprintf(stderr, "%s: %s\n", argv[1], sf_strerror(NULL));
return 1;
}
while (ssc_decode_read(sscd) > 0) {
if (!mqa) {
const char *s = ssc_decode_status(sscd);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment