1 /*
2  * extract_dca.c
3  * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of libdca, a free DTS Coherent Acoustics stream decoder.
7  * See http://www.videolan.org/developers/libdca.html for updates.
8  *
9  * libdca 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.
13  *
14  * libdca 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.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the
21  * Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include "config.h"
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <getopt.h>
32 #ifdef HAVE_IO_H
33 #include <fcntl.h>
34 #include <io.h>
35 #endif
36 #include <inttypes.h>
37 
38 #define BUFFER_SIZE 4096
39 static uint8_t buffer[BUFFER_SIZE];
40 static FILE * in_file;
41 static int demux_track = 0x80;
42 static int demux_pid = 0;
43 static int demux_pes = 0;
44 
print_usage(char ** argv)45 static void print_usage (char ** argv)
46 {
47     fprintf (stderr, "usage: %s [-h] [-s <track>] [-t <pid>] <file>\n"
48 	     "\t-h\tdisplay help\n"
49 	     "\t-s\tset track number (0-7 or 0x80-0x87)\n"
50 	     "\t-t\tuse transport stream demultiplexer, pid 0x10-0x1ffe\n"
51 	     "\t-T\tuse transport stream PES demultiplexer\n",
52 	     argv[0]);
53 
54     exit (1);
55 }
56 
handle_args(int argc,char ** argv)57 static void handle_args (int argc, char ** argv)
58 {
59     int c;
60     char * s;
61 
62     while ((c = getopt (argc, argv, "hs:t:T")) != -1)
63 	switch (c) {
64 	case 's':
65 	    demux_track = strtol (optarg, &s, 0);
66 	    if (demux_track < 0x80)
67 		demux_track += 0x80;
68 	    if (demux_track < 0x80 || demux_track > 0x87 || *s) {
69 		fprintf (stderr, "Invalid track number: %s\n", optarg);
70 		print_usage (argv);
71 	    }
72 	    break;
73 
74 	case 't':
75 	    demux_pid = strtol (optarg, &s, 0);
76 	    if (demux_pid < 0x10 || demux_pid > 0x1ffe || *s) {
77 		fprintf (stderr, "Invalid pid: %s\n", optarg);
78 		print_usage (argv);
79 	    }
80 	    break;
81 
82 	case 'T':
83 	    demux_pes = 1;
84 	    break;
85 
86 	default:
87 	    print_usage (argv);
88 	}
89 
90     if (optind < argc) {
91 	in_file = fopen (argv[optind], "rb");
92 	if (!in_file) {
93 	    fprintf (stderr, "%s - could not open file %s\n", strerror (errno),
94 		     argv[optind]);
95 	    exit (1);
96 	}
97     } else
98 	in_file = stdin;
99 }
100 
101 #define DEMUX_PAYLOAD_START 1
demux(uint8_t * buf,uint8_t * end,int flags)102 static int demux (uint8_t * buf, uint8_t * end, int flags)
103 {
104     static int mpeg1_skip_table[16] = {
105 	0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
106     };
107 
108     /*
109      * the demuxer keeps some state between calls:
110      * if "state" = DEMUX_HEADER, then "head_buf" contains the first
111      *     "bytes" bytes from some header.
112      * if "state" == DEMUX_DATA, then we need to copy "bytes" bytes
113      *     of ES data before the next header.
114      * if "state" == DEMUX_SKIP, then we need to skip "bytes" bytes
115      *     of data before the next header.
116      *
117      * NEEDBYTES makes sure we have the requested number of bytes for a
118      * header. If we dont, it copies what we have into head_buf and returns,
119      * so that when we come back with more data we finish decoding this header.
120      *
121      * DONEBYTES updates "buf" to point after the header we just parsed.
122      */
123 
124 #define DEMUX_HEADER 0
125 #define DEMUX_DATA 1
126 #define DEMUX_SKIP 2
127     static int state = DEMUX_SKIP;
128     static int state_bytes = 0;
129     static uint8_t head_buf[268];
130 
131     uint8_t * header;
132     int bytes;
133     int len;
134 
135 #define NEEDBYTES(x)						\
136     do {							\
137 	int missing;						\
138 								\
139 	missing = (x) - bytes;					\
140 	if (missing > 0) {					\
141 	    if (header == head_buf) {				\
142 		if (missing <= end - buf) {			\
143 		    memcpy (header + bytes, buf, missing);	\
144 		    buf += missing;				\
145 		    bytes = (x);				\
146 		} else {					\
147 		    memcpy (header + bytes, buf, end - buf);	\
148 		    state_bytes = bytes + end - buf;		\
149 		    return 0;					\
150 		}						\
151 	    } else {						\
152 		memcpy (head_buf, header, bytes);		\
153 		state = DEMUX_HEADER;				\
154 		state_bytes = bytes;				\
155 		return 0;					\
156 	    }							\
157 	}							\
158     } while (0)
159 
160 #define DONEBYTES(x)		\
161     do {			\
162 	if (header != head_buf)	\
163 	    buf = header + (x);	\
164     } while (0)
165 
166     if (flags & DEMUX_PAYLOAD_START)
167 	goto payload_start;
168     switch (state) {
169     case DEMUX_HEADER:
170 	if (state_bytes > 0) {
171 	    header = head_buf;
172 	    bytes = state_bytes;
173 	    goto continue_header;
174 	}
175 	break;
176     case DEMUX_DATA:
177 	if (demux_pid || (state_bytes > end - buf)) {
178 	    fwrite (buf, end - buf, 1, stdout);
179 	    state_bytes -= end - buf;
180 	    return 0;
181 	}
182 	fwrite (buf, state_bytes, 1, stdout);
183 	buf += state_bytes;
184 	break;
185     case DEMUX_SKIP:
186 	if (demux_pid || (state_bytes > end - buf)) {
187 	    state_bytes -= end - buf;
188 	    return 0;
189 	}
190 	buf += state_bytes;
191 	break;
192     }
193 
194     while (1) {
195 	if (demux_pid) {
196 	    state = DEMUX_SKIP;
197 	    return 0;
198 	}
199     payload_start:
200 	header = buf;
201 	bytes = end - buf;
202     continue_header:
203 	NEEDBYTES (4);
204 	if (header[0] || header[1] || (header[2] != 1)) {
205 	    if (demux_pid) {
206 		state = DEMUX_SKIP;
207 		return 0;
208 	    } else if (header != head_buf) {
209 		buf++;
210 		goto payload_start;
211 	    } else {
212 		header[0] = header[1];
213 		header[1] = header[2];
214 		header[2] = header[3];
215 		bytes = 3;
216 		goto continue_header;
217 	    }
218 	}
219 	if (demux_pid || demux_pes) {
220 	    if (header[3] != 0xbd) {
221 		fprintf (stderr, "bad stream id %x\n", header[3]);
222 		exit (1);
223 	    }
224 	    NEEDBYTES (9);
225 	    if ((header[6] & 0xc0) != 0x80) {	/* not mpeg2 */
226 		fprintf (stderr, "bad multiplex - not mpeg2\n");
227 		exit (1);
228 	    }
229 	    len = 9 + header[8];
230 	    NEEDBYTES (len);
231 	    DONEBYTES (len);
232 	    bytes = 6 + (header[4] << 8) + header[5] - len;
233 	    if (bytes > end - buf) {
234 		fwrite (buf, end - buf, 1, stdout);
235 		state = DEMUX_DATA;
236 		state_bytes = bytes - (end - buf);
237 		return 0;
238 	    } else if (bytes > 0) {
239 		fwrite (buf, bytes, 1, stdout);
240 		buf += bytes;
241 	    }
242 	} else switch (header[3]) {
243 	case 0xb9:	/* program end code */
244 	    /* DONEBYTES (4); */
245 	    /* break;         */
246 	    return 1;
247 	case 0xba:	/* pack header */
248 	    NEEDBYTES (5);
249 	    if ((header[4] & 0xc0) == 0x40) {	/* mpeg2 */
250 		NEEDBYTES (14);
251 		len = 14 + (header[13] & 7);
252 		NEEDBYTES (len);
253 		DONEBYTES (len);
254 		/* header points to the mpeg2 pack header */
255 	    } else if ((header[4] & 0xf0) == 0x20) {	/* mpeg1 */
256 		NEEDBYTES (12);
257 		DONEBYTES (12);
258 		/* header points to the mpeg1 pack header */
259 	    } else {
260 		fprintf (stderr, "weird pack header\n");
261 		DONEBYTES (5);
262 	    }
263 	    break;
264 	case 0xbd:	/* private stream 1 */
265 	    NEEDBYTES (7);
266 	    if ((header[6] & 0xc0) == 0x80) {	/* mpeg2 */
267 		NEEDBYTES (9);
268 		len = 10 + header[8];
269 		NEEDBYTES (len);
270 		/* header points to the mpeg2 pes header */
271 	    } else {	/* mpeg1 */
272 		len = 7;
273 		while ((header-1)[len] == 0xff) {
274 		    len++;
275 		    NEEDBYTES (len);
276 		    if (len == 23) {
277 			fprintf (stderr, "too much stuffing\n");
278 			break;
279 		    }
280 		}
281 		if (((header-1)[len] & 0xc0) == 0x40) {
282 		    len += 2;
283 		    NEEDBYTES (len);
284 		}
285 		len += mpeg1_skip_table[(header - 1)[len] >> 4] + 1;
286 		NEEDBYTES (len);
287 		/* header points to the mpeg1 pes header */
288 	    }
289 	    if ((header-1)[len] != demux_track) {
290 		DONEBYTES (len);
291 		bytes = 6 + (header[4] << 8) + header[5] - len;
292 		if (bytes <= 0)
293 		    continue;
294 		goto skip;
295 	    }
296 	    len += 3;
297 	    NEEDBYTES (len);
298 	    DONEBYTES (len);
299 	    bytes = 6 + (header[4] << 8) + header[5] - len;
300 	    if (bytes > end - buf) {
301 		fwrite (buf, end - buf, 1, stdout);
302 		state = DEMUX_DATA;
303 		state_bytes = bytes - (end - buf);
304 		return 0;
305 	    } else if (bytes > 0) {
306 		fwrite (buf, bytes, 1, stdout);
307 		buf += bytes;
308 	    }
309 	    break;
310 	default:
311 	    if (header[3] < 0xb9) {
312 		fprintf (stderr,
313 			 "looks like a video stream, not system stream\n");
314 		exit (1);
315 	    } else {
316 		NEEDBYTES (6);
317 		DONEBYTES (6);
318 		bytes = (header[4] << 8) + header[5];
319 	    skip:
320 		if (bytes > end - buf) {
321 		    state = DEMUX_SKIP;
322 		    state_bytes = bytes - (end - buf);
323 		    return 0;
324 		}
325 		buf += bytes;
326 	    }
327 	}
328     }
329 }
330 
ps_loop(void)331 static void ps_loop (void)
332 {
333     uint8_t * end;
334 
335     do {
336 	end = buffer + fread (buffer, 1, BUFFER_SIZE, in_file);
337 	if (demux (buffer, end, 0))
338 	    break;	/* hit program_end_code */
339     } while (end == buffer + BUFFER_SIZE);
340 }
341 
ts_loop(void)342 static void ts_loop (void)
343 {
344     uint8_t * buf;
345     uint8_t * nextbuf;
346     uint8_t * data;
347     uint8_t * end;
348     int pid;
349 
350     buf = buffer;
351     while (1) {
352 	end = buf + fread (buf, 1, buffer + BUFFER_SIZE - buf, in_file);
353 	buf = buffer;
354 	for (; (nextbuf = buf + 188) <= end; buf = nextbuf) {
355 	    if (*buf != 0x47) {
356 		fprintf (stderr, "bad sync byte\n");
357 		nextbuf = buf + 1;
358 		continue;
359 	    }
360 	    pid = ((buf[1] << 8) + buf[2]) & 0x1fff;
361 	    if (pid != demux_pid)
362 		continue;
363 	    data = buf + 4;
364 	    if (buf[3] & 0x20) {	/* buf contains an adaptation field */
365 		data = buf + 5 + buf[4];
366 		if (data > nextbuf)
367 		    continue;
368 	    }
369 	    if (buf[3] & 0x10)
370 		demux (data, nextbuf,
371 		       (buf[1] & 0x40) ? DEMUX_PAYLOAD_START : 0);
372 	}
373 	if (end != buffer + BUFFER_SIZE)
374 	    break;
375 	memcpy (buffer, buf, end - buf);
376 	buf = buffer + (end - buf);
377     }
378 }
379 
main(int argc,char ** argv)380 int main (int argc, char ** argv)
381 {
382 #ifdef HAVE_IO_H
383     setmode (fileno (stdin), O_BINARY);
384     setmode (fileno (stdout), O_BINARY);
385 #endif
386 
387     handle_args (argc, argv);
388 
389     if (demux_pid)
390 	ts_loop ();
391     else
392 	ps_loop ();
393 
394     return 0;
395 }
396