| @@ -0,0 +1,109 @@ |
| | 1 | #include <u.h> |
| | 2 | #include <libc.h> |
| | 3 | #include <bio.h> |
| | 4 | |
| | 5 | static int |
| | 6 | Bu16le(Biobuf *b, u16int *o) |
| | 7 | { |
| | 8 | int x; |
| | 9 | |
| | 10 | x = Bgetc(b); |
| | 11 | x |= Bgetc(b)<<8; |
| | 12 | *o = x; |
| | 13 | if(x < 0) |
| | 14 | werrstr("failed to read 2 bytes"); |
| | 15 | |
| | 16 | return x < 0 ? -1 : 0; |
| | 17 | } |
| | 18 | |
| | 19 | static int |
| | 20 | Bu32le(Biobuf *b, u32int *o) |
| | 21 | { |
| | 22 | int x, i; |
| | 23 | |
| | 24 | *o = 0; |
| | 25 | for(i = 0; i < 4; *o |= x<<(i*8), i++){ |
| | 26 | if((x = Bgetc(b)) < 0){ |
| | 27 | werrstr("failed to read 4 bytes"); |
| | 28 | return -1; |
| | 29 | } |
| | 30 | } |
| | 31 | |
| | 32 | return 0; |
| | 33 | } |
| | 34 | |
| | 35 | static int |
| | 36 | Bu64le(Biobuf *b, u64int *o) |
| | 37 | { |
| | 38 | int x, i; |
| | 39 | |
| | 40 | *o = 0; |
| | 41 | for(i = 0; i < 8; *o |= x<<(i*8), i++){ |
| | 42 | if((x = Bgetc(b)) < 0){ |
| | 43 | werrstr("failed to read 8 bytes"); |
| | 44 | return -1; |
| | 45 | } |
| | 46 | } |
| | 47 | |
| | 48 | return 0; |
| | 49 | } |
| | 50 | |
| | 51 | static void |
| | 52 | usage(void) |
| | 53 | { |
| | 54 | fprint(2, "usage: %s <file.ivf >file.raw\n", argv0); |
| | 55 | exits("usage"); |
| | 56 | } |
| | 57 | |
| | 58 | void |
| | 59 | main(int argc, char **argv) |
| | 60 | { |
| | 61 | int n, bufsz, framenum; |
| | 62 | u32int tbnum, tbdenum; |
| | 63 | u16int w, h, hlen; |
| | 64 | u64int timestamp; |
| | 65 | Biobuf in, out; |
| | 66 | char tmp[6]; |
| | 67 | u8int *buf; |
| | 68 | u32int sz; |
| | 69 | |
| | 70 | ARGBEGIN{ |
| | 71 | }ARGEND |
| | 72 | |
| | 73 | if(argc != 0) |
| | 74 | usage(); |
| | 75 | |
| | 76 | Binit(&in, 0, OREAD); |
| | 77 | Binit(&out, 1, OWRITE); |
| | 78 | |
| | 79 | if(Bread(&in, tmp, 6) != 6 || Bu16le(&in, &hlen) < 0) |
| | 80 | sysfatal("header read failed"); |
| | 81 | if(memcmp(tmp, "DKIF", 4) != 0) |
| | 82 | sysfatal("expected DKIF, got %02x%02x%02x%02x", tmp[0], tmp[1], tmp[2], tmp[3]); |
| | 83 | if(hlen < 0x20 || Bread(&in, tmp, 4) != 4) |
| | 84 | sysfatal("invalid header: hlen=%d", hlen); |
| | 85 | if(Bu16le(&in, &w) < 0 || Bu16le(&in, &h) < 0 || Bu32le(&in, &tbdenum) < 0 || Bu32le(&in, &tbnum) < 0) |
| | 86 | sysfatal("invalid header: %r"); |
| | 87 | if(Bseek(&in, hlen, 0) != hlen) |
| | 88 | sysfatal("invalid IVF stream"); |
| | 89 | |
| | 90 | bufsz = 0; |
| | 91 | buf = nil; |
| | 92 | for(framenum = 0;; framenum++){ |
| | 93 | if(Bu32le(&in, &sz) < 0 || Bu64le(&in, ×tamp) || (int)sz < 0) |
| | 94 | break; |
| | 95 | if(bufsz < sz){ |
| | 96 | bufsz = sz; |
| | 97 | if((buf = realloc(buf, bufsz)) == nil) |
| | 98 | sysfatal("frame %d is too big: %d bytes", framenum, bufsz); |
| | 99 | } |
| | 100 | if((n = Bread(&in, buf, sz)) != sz) |
| | 101 | sysfatal("short read (%d < %d)", n, sz); |
| | 102 | Bwrite(&out, buf, sz); |
| | 103 | } |
| | 104 | |
| | 105 | Bterm(&in); |
| | 106 | Bterm(&out); |
| | 107 | |
| | 108 | exits(nil); |
| | 109 | } |