~ft/mcfs

ref: 49dc43fd64bb77001494be6721380895f1bba971 mcfs/main.c -rw-r--r-- 1.7 KiB
49dc43fd — Sigrid Solveig Haflínudóttir matroska: handle av1 8 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
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "common.h"

enum
{
	Outbufsz = 256*1024,
	Inbufsz = 256*1024,
};

Biobuf stderr, out;
int dflag, trackdump;

static void
usage(void)
{
	fprint(2, "usage: %s [-t INDEX|audio|video] [FILE]\n", argv0);
	exits("usage");
}

void
main(int argc, char **argv)
{
	u8int d[4], *outbuf, *inbuf;
	char *s, *status;
	Biobuf f;
	int fd;

	fmtinstall('H', encodefmt);
	fmtinstall('T', isotypefmt);
	fmtinstall('P', srttsfmt);

	dflag = 0;
	trackdump = Nodump;
	ARGBEGIN{
	case 'd':
		dflag++;
		break;
	case 't':
		s = EARGF(usage());
		if(strcmp(s, "audio") == 0)
			trackdump = Dumpaudio;
		else if(strcmp(s, "video") == 0)
			trackdump = Dumpvideo;
		else
			trackdump = atoi(s);
		break;
	default:
		usage();
	}ARGEND

	if((outbuf = malloc(Outbufsz)) == nil)
		sysfatal("memory");
	if((inbuf = malloc(Inbufsz)) == nil)
		sysfatal("memory");

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

	if(Binits(&f, fd, OREAD, inbuf, Inbufsz) != 0)
		sysfatal("%r");
	if(Binits(&out, 1, OWRITE, outbuf, Outbufsz) != 0)
		sysfatal("%r");
	if(Binit(&stderr, 2, OWRITE) != 0)
		sysfatal("%r");

	status = nil;
	if(Bread(&f, d, 4) != 4){
		Bprint(&stderr, "unknown format\n");
		status = "unknown format";
	}else if(Bungetc(&f) < 0 || Bungetc(&f) < 0 || Bungetc(&f) < 0 || Bungetc(&f) < 0){
		Bprint(&stderr, "seek back failed: %r\n");
		status = "seek failed";
	}else if(memcmp(d, "\x1a\x45\xdf\xa3", 4) == 0){
		if(matroskarun(&f) != 0){
			Bprint(&stderr, "matroska: %r\n");
			status = "matroska failed";
		}
	}else if(isorun(&f) != 0){
		Bprint(&stderr, "iso: %r\n");
		status = "iso failed";
	}

	Bterm(&f);
	Bterm(&out);
	Bterm(&stderr);

	exits(status);
}