/* * TS2sec - simple MPEG Transport stream to DVB sections convert * * Copyright (C) 2009,2010 Ondrej Caletka * TEST PROJECT ONLY, PROBABLY DOES NOT WORK WELL * USE AT YOUR OWN RISK, YOU HAVE BEEN WARNED * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file COPYING included with this * distribution); if not, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #define _GNU_SOURCE #ifdef HAVE_CONFIG_H # include "config.h" #endif /* HAVE_CONFIG_H */ #include #include #include #include #include #include #include #include #include #define BUFLEN 1024 #define max(a,b) ((a)>(b) ? (a):(b)) #define min(a,b) ((a)<(b) ? (a):(b)) int main(int argc, char *argv[]) { int input_fd, output_fd; int pid_req = 18; int r; uint8_t buf[BUFLEN], *bhead=buf; int n=0; int pid_actual; uint8_t outbuf[1<<12], *outhead; int seclen, secleft; int first; uint8_t tscont; uint8_t secptr; //Pointer to section start input_fd = fileno(stdin); if (isatty(input_fd)) { fprintf(stderr, "Input should be redirected. Quitting.\n"); exit(EXIT_FAILURE); } output_fd = fileno(stdout); if (isatty(output_fd)) { fprintf(stderr, "Output should be redirected. Not writing any data.\n"); output_fd = open("/dev/null", O_WRONLY); } if (output_fd < 0) { fprintf(stderr, "Cannot open output file: %s\n", strerror(errno)); exit(EXIT_FAILURE); } first=1; secleft=1; while(1) { //fprintf(stderr,"First: %d, seclen: %d, secleft: %d\n",first, seclen, secleft); if (secleft == 0) { //write the output outhead=outbuf; while (seclen>0) { r = write(output_fd, outhead, seclen); if (r<0) { fprintf(stderr, "Write failed: %s\n", strerror(errno)); close(output_fd); exit(EXIT_FAILURE); } outhead+=r; seclen-=r; } secleft=first=1; //dprintf(1,"\xde\xad\xbe\xef"); //tcdrain(1); } /* move remaining data to front of buffer */ if (n>0) { memmove(buf, bhead, n); } if (n0) { /*SYNC BYTE*/ bhead++; n--; } if (n<188) continue; pid_actual = ((*(bhead+1) & 0x1F) << 8) | *(bhead+2); if (first || ((*(bhead+1) & 0x40) && pid_actual == pid_req)) { if (!(*(bhead+1) & 0x40) || pid_actual != pid_req) { n-=188; bhead += 188; continue; } tscont = *(bhead+3) & 0x0F; //continuity counter first=0; secptr = *(bhead+4); if (secptr > 182) { fprintf(stderr, "ERROR: Bad section pointer\n"); exit(1); } seclen=secleft=3 + (((*(bhead+6+secptr) & 0x0F) << 8) | *(bhead+7+secptr)); outhead=outbuf; r=min(183-secptr,secleft); memcpy(outhead, bhead+5+secptr, r); //fprintf(stderr,"Copyed %d bytes\n",r); secleft -= r; outhead += r; n-=188; bhead+=188; continue; } else { if ((*(bhead+3) & 0x0F) != (++tscont & 0x0F)) { secleft=first=1; n-=188; bhead+=188; continue; } if (pid_actual != pid_req) { n-=188; bhead += 188; continue; } r=min(184,secleft); memcpy(outhead, bhead+4, r); //fprintf(stderr,"Copyed %d bytes\n",r); secleft -= r; outhead += r; n-=188; bhead+=188; continue; } } return 0; }