#include #include FILE *s_in, *a_in, *s_out; int pass() /* copy byte from s_in to s_out and return it */ { int c; if ((c = getc(s_in)) == EOF) exit(0); putc(c, s_out); return c; } void passn(int n) /* copy n bytes from s_in to s_out */ { int i; for (i = 0; i < n; i++) pass(); } void copyn(int n) /* copy n bytes from a_in to s_out, skip n bytes of s_in */ { int i; for (i = 0; i < n; i++) { getc(s_in); putc(getc(a_in), s_out); } } void stream(int s) /* process stream of type s */ { int c, l, n; for (;;) { while (pass() != 0) /* sync */ ; n = 1; while ((c = pass()) == 0) /* padding */ n++; if (n < 2 || c != 0x01 || pass() != s) /* start code */ continue; l = pass(); l = (l << 8) | pass(); /* packet length */ n = 1; while ((c = pass()) == 0xFF) /* stuffing */ n++; if ((c >> 6) == 1) { /* buffer size */ n += 2; pass(); c = pass(); } switch (c >> 4) { /* time stamps */ case 2: passn(4); n += 4; break; case 3: passn(9); n += 9; } copyn(l - n); /* data */ } } FILE * fileopen(char *f, char *m) { FILE *o; if ((o = fopen(f, m)) == NULL) { perror(f); exit(1); } return o; } int main(int argc, char *argv[]) /* audiodub - Copyright (C) 1997, Bruce Ellis */ { if (argc != 4) { fprintf(stderr, "usage: %s mpegin audioin mpegout\n", argv[0]); return 1; } s_in = fileopen(argv[1], "rb"); a_in = fileopen(argv[2], "rb"); s_out = fileopen(argv[3], "wb"); stream(0xC0); return 0; }