| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2005-2006, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: muxts.c $ |
|---|
| 11 | * $brcm_Revision: Irvine_BSEAVSW_Devel/1 $ |
|---|
| 12 | * $brcm_Date: 2/21/06 3:46p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /SetTop/bcmplayer/utils/muxts.c $ |
|---|
| 19 | * |
|---|
| 20 | * Irvine_BSEAVSW_Devel/1 2/21/06 3:46p erickson |
|---|
| 21 | * PR17108: added muxts, not working yet |
|---|
| 22 | * |
|---|
| 23 | *************************************************************************/ |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <stdio.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <assert.h> |
|---|
| 28 | #include "ts_utils.h" |
|---|
| 29 | #include <unistd.h> |
|---|
| 30 | |
|---|
| 31 | #define MAXPIDS 100 |
|---|
| 32 | #define BUFSIZE (1024*32) |
|---|
| 33 | #define MAX_PACKET_PAYLOAD 184 |
|---|
| 34 | |
|---|
| 35 | struct pidstruct { |
|---|
| 36 | FILE *file; |
|---|
| 37 | unsigned short pid; |
|---|
| 38 | unsigned char buf[BUFSIZE]; |
|---|
| 39 | int bufptr, bufsize; |
|---|
| 40 | int continuity_counter; |
|---|
| 41 | } pids[MAXPIDS]; |
|---|
| 42 | int totalpids = 0; |
|---|
| 43 | |
|---|
| 44 | /* TODO: just about everything |
|---|
| 45 | 1. A/V sync (we can make gross assumptions about the files, but some sync should be done) |
|---|
| 46 | 2. PES layer and PCR's |
|---|
| 47 | 3. constant bit rate (padding the result with NULL's) |
|---|
| 48 | */ |
|---|
| 49 | |
|---|
| 50 | void printUsage() |
|---|
| 51 | { |
|---|
| 52 | fprintf(stderr, |
|---|
| 53 | "Usage: muxts FILE PID [FILE PID ...]\n" |
|---|
| 54 | " Transport output is written to stdout.\n" |
|---|
| 55 | ); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | unsigned char pkt[188]; |
|---|
| 59 | |
|---|
| 60 | int write_packet(struct pidstruct *pid) |
|---|
| 61 | { |
|---|
| 62 | int size = pid->bufsize - pid->bufptr; /* max size that could be written */ |
|---|
| 63 | int headersize = 4; |
|---|
| 64 | int payload; |
|---|
| 65 | |
|---|
| 66 | pkt[0] = 0x47; /* SYNC BYTE */ |
|---|
| 67 | pkt[1] = (pid->pid >> 8) & 0x1f; |
|---|
| 68 | pkt[2] = pid->pid & 0xff; /* PID */ |
|---|
| 69 | pkt[3] = 0x10 | pid->continuity_counter; /* not scrambled, payload only, continuity counter */ |
|---|
| 70 | if (++pid->continuity_counter > 0xF) |
|---|
| 71 | pid->continuity_counter = 0; |
|---|
| 72 | |
|---|
| 73 | payload = 188 - headersize; |
|---|
| 74 | |
|---|
| 75 | if (size < payload) { |
|---|
| 76 | /* pad with adaptation field, should only happen once at end */ |
|---|
| 77 | pkt[3] |= 0x20; |
|---|
| 78 | pkt[4] = payload - size - 1; |
|---|
| 79 | headersize = 188 - size; |
|---|
| 80 | payload = size; |
|---|
| 81 | /* TODO: set for private data */ |
|---|
| 82 | } |
|---|
| 83 | else if (size > 188 - headersize) { |
|---|
| 84 | size = 188 - headersize; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | fwrite(pkt, headersize, 1, stdout); |
|---|
| 88 | fwrite(&pid->buf[pid->bufptr], size, 1, stdout); |
|---|
| 89 | |
|---|
| 90 | return size; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | int main(int argc, char **argv) |
|---|
| 94 | { |
|---|
| 95 | int curarg = 1; |
|---|
| 96 | |
|---|
| 97 | while (curarg < argc-1) { /* must have two more params */ |
|---|
| 98 | const char *filename = argv[curarg]; |
|---|
| 99 | pids[totalpids].file = fopen(filename, "r"); |
|---|
| 100 | if (!pids[totalpids].file) { |
|---|
| 101 | fprintf(stderr, "ERROR: Unable to open %s\n", filename); |
|---|
| 102 | exit(1); |
|---|
| 103 | } |
|---|
| 104 | curarg++; |
|---|
| 105 | pids[totalpids].pid = strtoul(argv[curarg], NULL, 0); |
|---|
| 106 | pids[totalpids].bufptr = pids[totalpids].bufsize = 0; |
|---|
| 107 | totalpids++; |
|---|
| 108 | } |
|---|
| 109 | if (!totalpids) { |
|---|
| 110 | printUsage(); |
|---|
| 111 | exit(1); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | while (1) { |
|---|
| 115 | int i; |
|---|
| 116 | |
|---|
| 117 | /* read data */ |
|---|
| 118 | int more = 0; |
|---|
| 119 | for (i=0;i<totalpids;i++) { |
|---|
| 120 | int dataleft = pids[i].bufsize-pids[i].bufptr; |
|---|
| 121 | /* always make sure there's one packet's worth of data available */ |
|---|
| 122 | if (dataleft < MAX_PACKET_PAYLOAD && pids[i].file) { |
|---|
| 123 | int n; |
|---|
| 124 | |
|---|
| 125 | /* move remainder to head of buffer */ |
|---|
| 126 | if (dataleft) { |
|---|
| 127 | memmove(pids[i].buf, &pids[i].buf[pids[i].bufptr], dataleft); |
|---|
| 128 | pids[i].bufptr = dataleft; |
|---|
| 129 | } |
|---|
| 130 | else { |
|---|
| 131 | pids[i].bufptr = 0; |
|---|
| 132 | } |
|---|
| 133 | pids[i].bufsize = pids[i].bufptr; |
|---|
| 134 | |
|---|
| 135 | n = fread(&pids[i].buf[pids[i].bufptr], 1, BUFSIZE - pids[i].bufptr, pids[i].file); |
|---|
| 136 | if (n < 0) { |
|---|
| 137 | fprintf(stderr, "ERROR: unable to read file\n"); |
|---|
| 138 | exit(1); |
|---|
| 139 | } |
|---|
| 140 | if (n == 0) { |
|---|
| 141 | /* done with file */ |
|---|
| 142 | fclose(pids[i].file); |
|---|
| 143 | pids[i].file = NULL; |
|---|
| 144 | } |
|---|
| 145 | else { |
|---|
| 146 | pids[i].bufsize += n; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | if (pids[i].bufsize) |
|---|
| 151 | more = 1; |
|---|
| 152 | } |
|---|
| 153 | if (!more) break; |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | /* now output data */ |
|---|
| 157 | for (i=0;i<totalpids;i++) { |
|---|
| 158 | if (pids[i].bufsize) { |
|---|
| 159 | int n = write_packet(&pids[i]); |
|---|
| 160 | pids[i].bufptr += n; |
|---|
| 161 | if (pids[i].bufptr == pids[i].bufsize) { |
|---|
| 162 | pids[i].bufptr = pids[i].bufsize = 0; |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | return 0; |
|---|
| 169 | } |
|---|
| 170 | |
|---|