Commit ce749b66 authored by Mans Rullgard's avatar Mans Rullgard

mqbscan: support -1psv flags, multiple files

parent 6ddc07fb
......@@ -29,6 +29,7 @@
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <unistd.h>
#include <sndfile.h>
#include "bits.h"
......@@ -44,6 +45,10 @@ struct bitreader fb;
static unsigned sample_pos;
static jmp_buf eofjmp;
static int verbosity = 1;
static int packet_type = -1;
static int single;
static const struct bitfield frame_00[] = {
{ 8, BF_RET, "size" },
{ 32, BF_HEX, "magic" },
......@@ -115,7 +120,7 @@ static void fill_bits_mqb(struct bitreader *b, int n)
}
}
static void print_mqb_frame(struct bitreader *b)
static int print_mqb_frame(struct bitreader *b)
{
unsigned spos = sample_pos;
int type;
......@@ -127,6 +132,10 @@ static void print_mqb_frame(struct bitreader *b)
crc = 0;
type = get_ubits(b, 8);
if (packet_type >= 0 && type != packet_type)
print_verbosity(0);
size = print_frame(b, &frame_types[type], type, spos);
epos = spos + 8 + size * 8;
......@@ -136,8 +145,22 @@ static void print_mqb_frame(struct bitreader *b)
cc = mqb_crc_end(crc);
cr = print_field(b, 8, BF_HEX, "crc", NULL);
print_verbosity(verbosity);
if (cc != cr)
printf("%08x: checksum error\n", sample_pos);
return type;
}
static void scan_mqb(struct bitreader *b)
{
for (;;) {
int type = print_mqb_frame(b);
if (single && (packet_type < 0 || type == packet_type))
break;
}
}
static void get_bits_cb(uint64_t bits, int n)
......@@ -151,26 +174,28 @@ static void eof_cb(void)
longjmp(eofjmp, 1);
}
int main(int argc, char **argv)
int scan_file(const char *name, int start)
{
SNDFILE *file;
SF_INFO fmt;
if (argc < 2)
return 1;
memset(&fmt, 0, sizeof(fmt));
file = sf_open(argv[1], SFM_READ, &fmt);
file = sf_open(name, SFM_READ, &fmt);
if (!file) {
fprintf(stderr, "%s: %s\n", argv[1], sf_strerror(NULL));
return 1;
fprintf(stderr, "%s: %s\n", name, sf_strerror(NULL));
return -1;
}
if (fmt.channels != 2) {
fprintf(stderr, "Only 2 channels supported\n");
return 1;
return -1;
}
print_verbosity(verbosity);
sf_seek(file, start, SEEK_SET);
sample_pos = start;
if (!setjmp(eofjmp)) {
init_bits_sf(&br, file, 0);
br.msb = 1;
......@@ -181,11 +206,53 @@ int main(int argc, char **argv)
fb.fill_bits = fill_bits_mqb;
if (!find_sync(&br, MQB_MARKER, 56, 1024))
for (;;)
print_mqb_frame(&fb);
scan_mqb(&fb);
}
sf_close(file);
return 0;
}
int main(int argc, char **argv)
{
int start = 0;
int opt;
int i;
while ((opt = getopt(argc, argv, "1p:s:v")) != -1) {
switch (opt) {
case '1':
single = 1;
if (packet_type < 0)
packet_type = 1;
break;
case 'p':
packet_type = strtol(optarg, NULL, 0);
break;
case 's':
start = strtol(optarg, NULL, 0);
break;
case 'v':
verbosity++;
break;
default:
return 1;
}
}
argc -= optind;
argv += optind;
if (argc < 1)
return 1;
for (i = 0; i < argc; i++) {
if (argc > 1)
printf("%s\n", argv[i]);
scan_file(argv[i], start);
}
return 0;
}
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