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
35 #ifdef HAVE_SYS_TIME_H
42 #include "audio_out.h"
45 #define BUFFER_SIZE 4096
46 static uint8_t buffer[BUFFER_SIZE];
47 static FILE * in_file;
48 static int demux_track = 0;
49 static int demux_pid = 0;
50 static int disable_accel = 0;
51 static int disable_dynrng = 0;
52 static ao_open_t * output_open = NULL;
53 static ao_instance_t * output;
54 static a52_state_t * state;
56 #ifdef HAVE_SYS_TIME_H
58 static void print_fps (int final);
60 static RETSIGTYPE signal_handler (int sig)
63 signal (sig, SIG_DFL);
67 static void print_fps (int final)
69 static uint32_t frame_counter = 0;
70 static struct timeval tv_beg, tv_start;
71 static int total_elapsed;
72 static int last_count = 0;
73 struct timeval tv_end;
74 int fps, tfps, frames, elapsed;
76 gettimeofday (&tv_end, NULL);
79 tv_start = tv_beg = tv_end;
80 signal (SIGINT, signal_handler);
83 elapsed = (tv_end.tv_sec - tv_beg.tv_sec) * 100 +
84 (tv_end.tv_usec - tv_beg.tv_usec) / 10000;
85 total_elapsed = (tv_end.tv_sec - tv_start.tv_sec) * 100 +
86 (tv_end.tv_usec - tv_start.tv_usec) / 10000;
90 tfps = frame_counter * 10000 / total_elapsed;
94 fprintf (stderr,"\n%d frames decoded in %d.%02d "
95 "seconds (%d.%02d fps)\n", frame_counter,
96 total_elapsed / 100, total_elapsed % 100,
97 tfps / 100, tfps % 100);
104 if (elapsed < 50) /* only display every 0.50 seconds */
108 frames = frame_counter - last_count;
110 fps = frames * 10000 / elapsed; /* 100x */
111 tfps = frame_counter * 10000 / total_elapsed; /* 100x */
113 fprintf (stderr, "%d frames in %d.%02d sec (%d.%02d fps), "
114 "%d last %d.%02d sec (%d.%02d fps)\033[K\r", frame_counter,
115 total_elapsed / 100, total_elapsed % 100,
116 tfps / 100, tfps % 100, frames, elapsed / 100, elapsed % 100,
117 fps / 100, fps % 100);
119 last_count = frame_counter;
122 #else /* !HAVE_SYS_TIME_H */
124 static void print_fps (int final)
130 static void print_usage (char ** argv)
133 ao_driver_t * drivers;
135 fprintf (stderr, "usage: "
136 "%s [-o <mode>] [-s [<track>]] [-t <pid>] [-c] [-r] <file>\n"
137 "\t-s\tuse program stream demultiplexer, track 0-7 or 0x80-0x87\n"
138 "\t-t\tuse transport stream demultiplexer, pid 0x10-0x1ffe\n"
139 "\t-c\tuse c implementation, disables all accelerations\n"
140 "\t-r\tdisable dynamic range compression\n"
141 "\t-o\taudio output mode\n", argv[0]);
143 drivers = ao_drivers ();
144 for (i = 0; drivers[i].name; i++)
145 fprintf (stderr, "\t\t\t%s\n", drivers[i].name);
150 static void handle_args (int argc, char ** argv)
153 ao_driver_t * drivers;
157 drivers = ao_drivers ();
158 while ((c = getopt (argc, argv, "s::t:cro:")) != -1)
161 for (i = 0; drivers[i].name != NULL; i++)
162 if (strcmp (drivers[i].name, optarg) == 0)
163 output_open = drivers[i].open;
164 if (output_open == NULL) {
165 fprintf (stderr, "Invalid video driver: %s\n", optarg);
172 if (optarg != NULL) {
173 demux_track = strtol (optarg, &s, 16);
174 if (demux_track < 0x80)
176 if ((demux_track < 0x80) || (demux_track > 0x87) || (*s)) {
177 fprintf (stderr, "Invalid track number: %s\n", optarg);
184 demux_pid = strtol (optarg, &s, 16);
185 if ((demux_pid < 0x10) || (demux_pid > 0x1ffe) || (*s)) {
186 fprintf (stderr, "Invalid pid: %s\n", optarg);
203 /* -o not specified, use a default driver */
204 if (output_open == NULL)
205 output_open = drivers[0].open;
208 in_file = fopen (argv[optind], "rb");
210 fprintf (stderr, "%s - couldnt open file %s\n", strerror (errno),
218 void a52_decode_data (uint8_t * start, uint8_t * end)
220 static uint8_t buf[3840];
221 static uint8_t * bufptr = buf;
222 static uint8_t * bufpos = buf + 7;
225 * sample_rate and flags are static because this routine could
226 * exit between the a52_syncinfo() and the ao_setup(), and we want
227 * to have the same values when we get back !
230 static int sample_rate;
239 if (len > bufpos - bufptr)
240 len = bufpos - bufptr;
241 memcpy (bufptr, start, len);
244 if (bufptr == bufpos) {
245 if (bufpos == buf + 7) {
248 length = a52_syncinfo (buf, &flags, &sample_rate, &bit_rate);
250 fprintf (stderr, "skip\n");
251 for (bufptr = buf; bufptr < buf + 6; bufptr++)
252 bufptr[0] = bufptr[1];
255 bufpos = buf + length;
257 sample_t level, bias;
260 if (ao_setup (output, sample_rate, &flags, &level, &bias))
262 flags |= A52_ADJUST_LEVEL;
263 if (a52_frame (state, buf, &flags, &level, bias))
266 a52_dynrng (state, NULL, NULL);
267 for (i = 0; i < 6; i++) {
268 if (a52_block (state))
270 if (ao_play (output, flags, a52_samples (state)))
278 fprintf (stderr, "error\n");
286 #define DEMUX_PAYLOAD_START 1
287 static int demux (uint8_t * buf, uint8_t * end, int flags)
289 static int mpeg1_skip_table[16] = {
290 0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
294 * the demuxer keeps some state between calls:
295 * if "state" = DEMUX_HEADER, then "head_buf" contains the first
296 * "bytes" bytes from some header.
297 * if "state" == DEMUX_DATA, then we need to copy "bytes" bytes
298 * of ES data before the next header.
299 * if "state" == DEMUX_SKIP, then we need to skip "bytes" bytes
300 * of data before the next header.
302 * NEEDBYTES makes sure we have the requested number of bytes for a
303 * header. If we dont, it copies what we have into head_buf and returns,
304 * so that when we come back with more data we finish decoding this header.
306 * DONEBYTES updates "buf" to point after the header we just parsed.
309 #define DEMUX_HEADER 0
312 static int state = DEMUX_SKIP;
313 static int state_bytes = 0;
314 static uint8_t head_buf[268];
320 #define NEEDBYTES(x) \
324 missing = (x) - bytes; \
326 if (header == head_buf) { \
327 if (missing <= end - buf) { \
328 memcpy (header + bytes, buf, missing); \
332 memcpy (header + bytes, buf, end - buf); \
333 state_bytes = bytes + end - buf; \
337 memcpy (head_buf, header, bytes); \
338 state = DEMUX_HEADER; \
339 state_bytes = bytes; \
345 #define DONEBYTES(x) \
347 if (header != head_buf) \
348 buf = header + (x); \
351 if (flags & DEMUX_PAYLOAD_START)
355 if (state_bytes > 0) {
358 goto continue_header;
362 if (demux_pid || (state_bytes > end - buf)) {
363 a52_decode_data (buf, end);
364 state_bytes -= end - buf;
367 a52_decode_data (buf, buf + state_bytes);
371 if (demux_pid || (state_bytes > end - buf)) {
372 state_bytes -= end - buf;
389 if (header[0] || header[1] || (header[2] != 1)) {
393 } else if (header != head_buf) {
397 header[0] = header[1];
398 header[1] = header[2];
399 header[2] = header[3];
401 goto continue_header;
404 if (demux_pid && (header[3] != 0xbd)) {
405 fprintf (stderr, "bad stream id %x\n", header[3]);
409 case 0xb9: /* program end code */
413 case 0xba: /* pack header */
415 if ((header[4] & 0xc0) == 0x40) { /* mpeg2 */
417 len = 14 + (header[13] & 7);
420 /* header points to the mpeg2 pack header */
421 } else if ((header[4] & 0xf0) == 0x20) { /* mpeg1 */
423 /* header points to the mpeg1 pack header */
425 fprintf (stderr, "weird pack header\n");
429 case 0xbd: /* private stream 1 */
431 if ((header[6] & 0xc0) == 0x80) { /* mpeg2 */
433 len = 10 + header[8];
435 /* header points to the mpeg2 pes header */
438 while ((header-1)[len] == 0xff) {
442 fprintf (stderr, "too much stuffing\n");
446 if (((header-1)[len] & 0xc0) == 0x40) {
450 len += mpeg1_skip_table[(header - 1)[len] >> 4] + 1;
452 /* header points to the mpeg1 pes header */
454 if ((!demux_pid) && ((header-1)[len] != demux_track)) {
456 bytes = 6 + (header[4] << 8) + header[5] - len;
464 bytes = 6 + (header[4] << 8) + header[5] - len;
465 if (demux_pid || (bytes > end - buf)) {
466 a52_decode_data (buf, end);
468 state_bytes = bytes - (end - buf);
470 } else if (bytes <= 0)
472 a52_decode_data (buf, buf + bytes);
476 if (header[3] < 0xb9) {
478 "looks like a video stream, not system stream\n");
483 bytes = (header[4] << 8) + header[5];
485 if (bytes > end - buf) {
487 state_bytes = bytes - (end - buf);
496 static void ps_loop (void)
501 end = buffer + fread (buffer, 1, BUFFER_SIZE, in_file);
502 if (demux (buffer, end, 0))
503 break; /* hit program_end_code */
504 } while (end == buffer + BUFFER_SIZE);
507 static void ts_loop (void)
509 #define PACKETS (BUFFER_SIZE / 188)
518 packets = fread (buffer, 188, PACKETS, in_file);
519 for (i = 0; i < packets; i++) {
520 buf = buffer + i * 188;
522 if (buf[0] != 0x47) {
523 fprintf (stderr, "bad sync byte\n");
526 pid = ((buf[1] << 8) + buf[2]) & 0x1fff;
527 if (pid != demux_pid)
530 if (buf[3] & 0x20) { /* buf contains an adaptation field */
531 data = buf + 5 + buf[4];
536 demux (data, end, (buf[1] & 0x40) ? DEMUX_PAYLOAD_START : 0);
538 } while (packets == PACKETS);
541 static void es_loop (void)
546 size = fread (buffer, 1, BUFFER_SIZE, in_file);
547 a52_decode_data (buffer, buffer + size);
548 } while (size == BUFFER_SIZE);
551 int main (int argc, char ** argv)
556 setmode (fileno (stdout), O_BINARY);
559 fprintf (stderr, PACKAGE"-"VERSION
560 " - by Michel Lespinasse <walken@zoy.org> and Aaron Holtzman\n");
562 handle_args (argc, argv);
564 accel = disable_accel ? 0 : MM_ACCEL_DJBFFT;
566 output = ao_open (output_open);
567 if (output == NULL) {
568 fprintf (stderr, "Can not open output\n");
572 state = a52_init (accel);
574 fprintf (stderr, "A52 init failed\n");
580 else if (demux_track)