3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
9 * a52dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * a52dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #define BUFFER_SIZE 4096
34 static uint8_t buffer[BUFFER_SIZE];
35 static FILE * in_file;
36 static int demux_track = 0x80;
37 static int demux_pid = 0;
39 static void print_usage (char ** argv)
41 fprintf (stderr, "usage: %s [-s <track>] [-t <pid>] <file>\n"
42 "\t-s\tset track number (0-7 or 0x80-0x87)\n"
43 "\t-t\tuse transport stream demultiplexer, pid 0x10-0x1ffe\n",
49 static void handle_args (int argc, char ** argv)
54 while ((c = getopt (argc, argv, "s:t:")) != -1)
57 demux_track = strtol (optarg, &s, 16);
58 if (demux_track < 0x80)
60 if ((demux_track < 0x80) || (demux_track > 0x87) || (*s)) {
61 fprintf (stderr, "Invalid track number: %s\n", optarg);
67 demux_pid = strtol (optarg, &s, 16);
68 if ((demux_pid < 0x10) || (demux_pid > 0x1ffe) || (*s)) {
69 fprintf (stderr, "Invalid pid: %s\n", optarg);
79 in_file = fopen (argv[optind], "rb");
81 fprintf (stderr, "%s - couldnt open file %s\n", strerror (errno),
89 #define DEMUX_PAYLOAD_START 1
90 static int demux (uint8_t * buf, uint8_t * end, int flags)
92 static int mpeg1_skip_table[16] = {
93 0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
97 * the demuxer keeps some state between calls:
98 * if "state" = DEMUX_HEADER, then "head_buf" contains the first
99 * "bytes" bytes from some header.
100 * if "state" == DEMUX_DATA, then we need to copy "bytes" bytes
101 * of ES data before the next header.
102 * if "state" == DEMUX_SKIP, then we need to skip "bytes" bytes
103 * of data before the next header.
105 * NEEDBYTES makes sure we have the requested number of bytes for a
106 * header. If we dont, it copies what we have into head_buf and returns,
107 * so that when we come back with more data we finish decoding this header.
109 * DONEBYTES updates "buf" to point after the header we just parsed.
112 #define DEMUX_HEADER 0
115 static int state = DEMUX_SKIP;
116 static int state_bytes = 0;
117 static uint8_t head_buf[268];
123 #define NEEDBYTES(x) \
127 missing = (x) - bytes; \
129 if (header == head_buf) { \
130 if (missing <= end - buf) { \
131 memcpy (header + bytes, buf, missing); \
135 memcpy (header + bytes, buf, end - buf); \
136 state_bytes = bytes + end - buf; \
140 memcpy (head_buf, header, bytes); \
141 state = DEMUX_HEADER; \
142 state_bytes = bytes; \
148 #define DONEBYTES(x) \
150 if (header != head_buf) \
151 buf = header + (x); \
154 if (flags & DEMUX_PAYLOAD_START)
158 if (state_bytes > 0) {
161 goto continue_header;
165 if (demux_pid || (state_bytes > end - buf)) {
166 fwrite (buf, end - buf, 1, stdout);
167 state_bytes -= end - buf;
170 fwrite (buf, state_bytes, 1, stdout);
174 if (demux_pid || (state_bytes > end - buf)) {
175 state_bytes -= end - buf;
192 if (header[0] || header[1] || (header[2] != 1)) {
196 } else if (header != head_buf) {
200 header[0] = header[1];
201 header[1] = header[2];
202 header[2] = header[3];
204 goto continue_header;
207 if (demux_pid && (header[3] != 0xbd)) {
208 fprintf (stderr, "bad stream id %x\n", header[3]);
212 case 0xb9: /* program end code */
216 case 0xba: /* pack header */
218 if ((header[4] & 0xc0) == 0x40) { /* mpeg2 */
220 len = 14 + (header[13] & 7);
223 /* header points to the mpeg2 pack header */
224 } else if ((header[4] & 0xf0) == 0x20) { /* mpeg1 */
226 /* header points to the mpeg1 pack header */
228 fprintf (stderr, "weird pack header\n");
232 case 0xbd: /* private stream 1 */
234 if ((header[6] & 0xc0) == 0x80) { /* mpeg2 */
236 len = 10 + header[8];
238 /* header points to the mpeg2 pes header */
241 while ((header-1)[len] == 0xff) {
245 fprintf (stderr, "too much stuffing\n");
249 if (((header-1)[len] & 0xc0) == 0x40) {
253 len += mpeg1_skip_table[(header - 1)[len] >> 4] + 1;
255 /* header points to the mpeg1 pes header */
257 if ((!demux_pid) && ((header-1)[len] != demux_track)) {
259 bytes = 6 + (header[4] << 8) + header[5] - len;
267 bytes = 6 + (header[4] << 8) + header[5] - len;
268 if (demux_pid || (bytes > end - buf)) {
269 fwrite (buf, end - buf, 1, stdout);
271 state_bytes = bytes - (end - buf);
273 } else if (bytes <= 0)
275 fwrite (buf, bytes, 1, stdout);
279 if (header[3] < 0xb9) {
281 "looks like a video stream, not system stream\n");
286 bytes = (header[4] << 8) + header[5];
288 if (bytes > end - buf) {
290 state_bytes = bytes - (end - buf);
299 static void ps_loop (void)
304 end = buffer + fread (buffer, 1, BUFFER_SIZE, in_file);
305 if (demux (buffer, end, 0))
306 break; /* hit program_end_code */
307 } while (end == buffer + BUFFER_SIZE);
310 static void ts_loop (void)
312 #define PACKETS (BUFFER_SIZE / 188)
321 packets = fread (buffer, 188, PACKETS, in_file);
322 for (i = 0; i < packets; i++) {
323 buf = buffer + i * 188;
325 if (buf[0] != 0x47) {
326 fprintf (stderr, "bad sync byte\n");
329 pid = ((buf[1] << 8) + buf[2]) & 0x1fff;
330 if (pid != demux_pid)
333 if (buf[3] & 0x20) { /* buf contains an adaptation field */
334 data = buf + 5 + buf[4];
339 demux (data, end, (buf[1] & 0x40) ? DEMUX_PAYLOAD_START : 0);
341 } while (packets == PACKETS);
344 int main (int argc, char ** argv)
346 handle_args (argc, argv);