~ft/mcfs

ref: 49dc43fd64bb77001494be6721380895f1bba971 mcfs/extra/adtsdump.c -rw-r--r-- 2.1 KiB
49dc43fd — Sigrid Solveig Haflínudóttir matroska: handle av1 11 months ago
                                                                                
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <u.h>
#include <libc.h>
#include <bio.h>

static char *objtype[] = {
	"aac main",
	"aac lc  ",
	"aac ssr ",
	"aac ltp ",
};

static char *rate[] = {
	"96000",
	"88200",
	"64000",
	"48000",
	"44100",
	"32000",
	"24000",
	"22050",
	"16000",
	"12000",
	"11025",
	"8000 ",
	"7350 ",
	"???  ",
	"???  ",
	"expl ",
};

static char *chan[] = {
	"AOT",
	"1  ",
	"2  ",
	"3  ",
	"4  ",
	"5  ",
	"6  ",
	"8  ",
	"???",
	"???",
	"???",
	"???",
	"???",
	"???",
	"???",
	"???",
};

static void
usage(void)
{
	fprint(2, "usage: %s [-h hxsz] [file.aac]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	int hxsz, fd, d, n, sz;
	Biobuf in, out;
	u8int b[7], *h;

	hxsz = 0;
	ARGBEGIN{
	case 'h':
		if((hxsz = atoi(EARGF(usage()))) > 0){
			fmtinstall('H', encodefmt);
			break;
		}
	default:
		usage();
	}ARGEND

	fd = 0;
	if(argc == 1){
		if((fd = open(*argv, OREAD)) < 0)
			sysfatal("%r");
	}else if(argc != 0)
		usage();

	if((h = malloc(hxsz)) == nil)
		sysfatal("memory");

	Binit(&in, fd, OREAD);
	Binit(&out, 1, OWRITE);
	fmtinstall('H', encodefmt);

	for(;;){
		if((n = Bread(&in, b, 7)) == 0)
			break;
		if(n < 0)
			goto err;
		if(n != 7){
			werrstr("short ADTS: %d != 7", n);
			goto err;
		}

		if(b[0] != 0xff || (b[0]&0xf0) != 0xf0){
			werrstr("invalid syncword %2x%02x", b[0], b[1]);
			goto err;
		}
		sz = (b[3]&3)<<11 | b[4]<<3 | b[5]>>5;
		sz -= 7;
		Bprint(&out, "%s %s %s %s %d %d",
			(b[1]&8) ? "mpeg2" : "mpeg4",
			objtype[b[2]>>6],
			rate[(b[2]>>2)&0xf],
			chan[(b[2]&3)<<2 | b[3]>>6],
			sz - ((b[1]&1) == 0 ? 2 : 0),
			(b[6]&3)+1
		);
		if((b[1]&1) == 0){ /* skip crc */
			if((n = Bread(&in, b, 2)) != 2){ 
				werrstr("short ADTS.crc: %d != 2", n);
				goto err;
			}
			sz -= 2;
		}
		d = sz < hxsz ? sz : hxsz;
		sz -= d;
		if(d > 0){
			if((n = Bread(&in, h, d)) != d){
				werrstr("short read: %d != %d", n, d);
				goto err;
			}
			Bprint(&out, " [%.*H]", d, h);
		}
		Bprint(&out, "\n");
		if(Bseek(&in, sz, 1) < 0)
			goto err;
	}

	Bterm(&out);
	Bterm(&in);

	exits(nil);
err:
	fprint(2, "%s: %r (at %#llx)\n", fd == 0 ? "stdin" : *argv, Boffset(&in));
	exits("error");
}