mqbscan.c 4.06 KB
Newer Older
Mans Rullgard's avatar
Mans Rullgard committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
  Copyright (c) 2017, Mans Rullgard <mans@mansr.com>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <sndfile.h>

#include "bits.h"
#include "sfbits.h"
#include "mqa-common.h"
#include "mqbcrc.h"

39
#define MQB_MARKER	0x00048c49e0ab2c
Mans Rullgard's avatar
Mans Rullgard committed
40 41 42 43 44 45 46 47

struct bitreader br;
struct bitreader fb;

static unsigned sample_pos;
static jmp_buf eofjmp;

static const struct bitfield frame_00[] = {
48
	{ 8,	BF_RET,		"size" },
Mans Rullgard's avatar
Mans Rullgard committed
49 50 51 52 53 54 55 56 57 58 59 60
	{ 32,	BF_HEX,		"magic" },
	{ 0 },
};

static const char *bitdepth_tab[4] = {
	"20 bits",
	"18 bits",
	"16 bits",
	"15 bits",
};

static const struct bitfield frame_01[] = {
61
	{ 8,	BF_RET,		"size" },
Mans Rullgard's avatar
Mans Rullgard committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
	{ 5,	0,		"resampling_filter" },
	{ 2,	0,		"noise_shaping_filter" },
	{ 2,	0,		"bitdepth",	bitdepth_tab },
	{ 1,	0,		"unknown" },
	{ 10,	BF_SIGNED,	"replay_gain" },
	{ 5,	BF_HEX,		"orig_rate",	mqa_rates },
	{ 2,	0,		"unknown" },
	{ 1,	0,		"unknown" },
	{ 1,	0,		"swizzle" },
	{ 3,	0,		"pad" },
	{ 0 },
};

static const struct bitfield frame_02[] = {
76
	{ 8,	BF_RET,		"size" },
Mans Rullgard's avatar
Mans Rullgard committed
77 78 79 80 81
	{ 16,	BF_SIGNED,	"gain" },
	{ 0 },
};

static const struct bitfield frame_04[] = {
82
	{ 8,	BF_RET,		"size" },
Mans Rullgard's avatar
Mans Rullgard committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	{ 8,	0,		"type" },
	{ 0 },
};

static const struct frame frame_types[256] = {
	[0] = {
		.name	= "magic",
		.fields	= frame_00,
	},
	[1] = {
		.name	= "stream_params",
		.fields	= frame_01,
	},
	[2] = {
		.name	= "gain",
		.fields	= frame_02,
	},
	[4] = {
		.name	= "metadata",
		.fields	= frame_04,
	},
};

static uint32_t crc;

static void fill_bits_mqb(struct bitreader *b, int n)
{
	int bits;

	while (b->bitend < b->bitpos + n) {
		bits = get_ubits(&br, 8);
114
		put_byte(b, bits);
Mans Rullgard's avatar
Mans Rullgard committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	}
}

static void print_mqb_frame(struct bitreader *b)
{
	unsigned spos = sample_pos;
	int type;
	int size;
	int epos;
	int cc;
	int cr;

	crc = 0;

	type = get_ubits(b, 8);
130 131
	size = print_frame(b, &frame_types[type], type, spos);
	epos = spos + 8 + size * 8;
Mans Rullgard's avatar
Mans Rullgard committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

	while (sample_pos < epos)
		get_ubits(b, 8);

	cc = mqb_crc_end(crc);
	cr = print_field(b, 8, BF_HEX, "crc", NULL);

	if (cc != cr)
		printf("%08x: checksum error\n", sample_pos);
}

static void get_bits_cb(uint64_t bits, int n)
{
	crc = mqb_crc(crc, bits);
	sample_pos += n;
}

static void eof_cb(void)
{
	longjmp(eofjmp, 1);
}

int main(int argc, char **argv)
{
	SNDFILE *file;
	SF_INFO fmt;

	if (argc < 2)
		return 1;

	memset(&fmt, 0, sizeof(fmt));
	file = sf_open(argv[1], SFM_READ, &fmt);
	if (!file) {
		fprintf(stderr, "%s: %s\n", argv[1], sf_strerror(NULL));
		return 1;
	}

	if (fmt.channels != 2) {
		fprintf(stderr, "Only 2 channels supported\n");
		return 1;
	}

	if (!setjmp(eofjmp)) {
		init_bits_sf(&br, file, 0);
		br.msb = 1;
		br.get_bits_cb = get_bits_cb;
		br.eof_cb = eof_cb;

		init_bits(&fb);
		fb.fill_bits = fill_bits_mqb;

183 184 185
		if (!find_sync(&br, MQB_MARKER, 56, 1024))
			for (;;)
				print_mqb_frame(&fb);
Mans Rullgard's avatar
Mans Rullgard committed
186 187 188 189 190 191
	}

	sf_close(file);

	return 0;
}