initial commit
[goodguy/history.git] / cinelerra-5.0 / libzmpeg3 / a52dec-0.7.3 / src / extract_a52.c
1 /*
2  * extract_a52.c
3  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
4  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5  *
6  * This file is part of a52dec, a free ATSC A-52 stream decoder.
7  * See http://liba52.sourceforge.net/ for updates.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <getopt.h>
31 #include <inttypes.h>
32
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;
38
39 static void print_usage (char ** argv)
40 {
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",
44              argv[0]);
45
46     exit (1);
47 }
48
49 static void handle_args (int argc, char ** argv)
50 {
51     int c;
52     char * s;
53
54     while ((c = getopt (argc, argv, "s:t:")) != -1)
55         switch (c) {
56         case 's':
57             demux_track = strtol (optarg, &s, 16);
58             if (demux_track < 0x80)
59                 demux_track += 0x80;
60             if ((demux_track < 0x80) || (demux_track > 0x87) || (*s)) {
61                 fprintf (stderr, "Invalid track number: %s\n", optarg);
62                 print_usage (argv);
63             }
64             break;
65
66         case 't':
67             demux_pid = strtol (optarg, &s, 16);
68             if ((demux_pid < 0x10) || (demux_pid > 0x1ffe) || (*s)) {
69                 fprintf (stderr, "Invalid pid: %s\n", optarg);
70                 print_usage (argv);
71             }
72             break;
73
74         default:
75             print_usage (argv);
76         }
77
78     if (optind < argc) {
79         in_file = fopen (argv[optind], "rb");
80         if (!in_file) {
81             fprintf (stderr, "%s - couldnt open file %s\n", strerror (errno),
82                      argv[optind]);
83             exit (1);
84         }
85     } else
86         in_file = stdin;
87 }
88
89 #define DEMUX_PAYLOAD_START 1
90 static int demux (uint8_t * buf, uint8_t * end, int flags)
91 {
92     static int mpeg1_skip_table[16] = {
93         0, 0, 4, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
94     };
95
96     /*
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.
104      *
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.
108      *
109      * DONEBYTES updates "buf" to point after the header we just parsed.
110      */
111
112 #define DEMUX_HEADER 0
113 #define DEMUX_DATA 1
114 #define DEMUX_SKIP 2
115     static int state = DEMUX_SKIP;
116     static int state_bytes = 0;
117     static uint8_t head_buf[268];
118
119     uint8_t * header;
120     int bytes;
121     int len;
122
123 #define NEEDBYTES(x)                                            \
124     do {                                                        \
125         int missing;                                            \
126                                                                 \
127         missing = (x) - bytes;                                  \
128         if (missing > 0) {                                      \
129             if (header == head_buf) {                           \
130                 if (missing <= end - buf) {                     \
131                     memcpy (header + bytes, buf, missing);      \
132                     buf += missing;                             \
133                     bytes = (x);                                \
134                 } else {                                        \
135                     memcpy (header + bytes, buf, end - buf);    \
136                     state_bytes = bytes + end - buf;            \
137                     return 0;                                   \
138                 }                                               \
139             } else {                                            \
140                 memcpy (head_buf, header, bytes);               \
141                 state = DEMUX_HEADER;                           \
142                 state_bytes = bytes;                            \
143                 return 0;                                       \
144             }                                                   \
145         }                                                       \
146     } while (0)
147
148 #define DONEBYTES(x)            \
149     do {                        \
150         if (header != head_buf) \
151             buf = header + (x); \
152     } while (0)
153
154     if (flags & DEMUX_PAYLOAD_START)
155         goto payload_start;
156     switch (state) {
157     case DEMUX_HEADER:
158         if (state_bytes > 0) {
159             header = head_buf;
160             bytes = state_bytes;
161             goto continue_header;
162         }
163         break;
164     case DEMUX_DATA:
165         if (demux_pid || (state_bytes > end - buf)) {
166             fwrite (buf, end - buf, 1, stdout);
167             state_bytes -= end - buf;
168             return 0;
169         }
170         fwrite (buf, state_bytes, 1, stdout);
171         buf += state_bytes;
172         break;
173     case DEMUX_SKIP:
174         if (demux_pid || (state_bytes > end - buf)) {
175             state_bytes -= end - buf;
176             return 0;
177         }
178         buf += state_bytes;
179         break;
180     }
181
182     while (1) {
183         if (demux_pid) {
184             state = DEMUX_SKIP;
185             return 0;
186         }
187     payload_start:
188         header = buf;
189         bytes = end - buf;
190     continue_header:
191         NEEDBYTES (4);
192         if (header[0] || header[1] || (header[2] != 1)) {
193             if (demux_pid) {
194                 state = DEMUX_SKIP;
195                 return 0;
196             } else if (header != head_buf) {
197                 buf++;
198                 goto payload_start;
199             } else {
200                 header[0] = header[1];
201                 header[1] = header[2];
202                 header[2] = header[3];
203                 bytes = 3;
204                 goto continue_header;
205             }
206         }
207         if (demux_pid && (header[3] != 0xbd)) {
208             fprintf (stderr, "bad stream id %x\n", header[3]);
209             exit (1);
210         }
211         switch (header[3]) {
212         case 0xb9:      /* program end code */
213             /* DONEBYTES (4); */
214             /* break;         */
215             return 1;
216         case 0xba:      /* pack header */
217             NEEDBYTES (12);
218             if ((header[4] & 0xc0) == 0x40) {   /* mpeg2 */
219                 NEEDBYTES (14);
220                 len = 14 + (header[13] & 7);
221                 NEEDBYTES (len);
222                 DONEBYTES (len);
223                 /* header points to the mpeg2 pack header */
224             } else if ((header[4] & 0xf0) == 0x20) {    /* mpeg1 */
225                 DONEBYTES (12);
226                 /* header points to the mpeg1 pack header */
227             } else {
228                 fprintf (stderr, "weird pack header\n");
229                 exit (1);
230             }
231             break;
232         case 0xbd:      /* private stream 1 */
233             NEEDBYTES (7);
234             if ((header[6] & 0xc0) == 0x80) {   /* mpeg2 */
235                 NEEDBYTES (9);
236                 len = 10 + header[8];
237                 NEEDBYTES (len);
238                 /* header points to the mpeg2 pes header */
239             } else {    /* mpeg1 */
240                 len = 7;
241                 while ((header-1)[len] == 0xff) {
242                     len++;
243                     NEEDBYTES (len);
244                     if (len == 23) {
245                         fprintf (stderr, "too much stuffing\n");
246                         break;
247                     }
248                 }
249                 if (((header-1)[len] & 0xc0) == 0x40) {
250                     len += 2;
251                     NEEDBYTES (len);
252                 }
253                 len += mpeg1_skip_table[(header - 1)[len] >> 4] + 1;
254                 NEEDBYTES (len);
255                 /* header points to the mpeg1 pes header */
256             }
257             if ((!demux_pid) && ((header-1)[len] != demux_track)) {
258                 DONEBYTES (len);
259                 bytes = 6 + (header[4] << 8) + header[5] - len;
260                 if (bytes <= 0)
261                     continue;
262                 goto skip;
263             }
264             len += 3;
265             NEEDBYTES (len);
266             DONEBYTES (len);
267             bytes = 6 + (header[4] << 8) + header[5] - len;
268             if (demux_pid || (bytes > end - buf)) {
269                 fwrite (buf, end - buf, 1, stdout);
270                 state = DEMUX_DATA;
271                 state_bytes = bytes - (end - buf);
272                 return 0;
273             } else if (bytes <= 0)
274                 continue;
275             fwrite (buf, bytes, 1, stdout);
276             buf += bytes;
277             break;
278         default:
279             if (header[3] < 0xb9) {
280                 fprintf (stderr,
281                          "looks like a video stream, not system stream\n");
282                 exit (1);
283             } else {
284                 NEEDBYTES (6);
285                 DONEBYTES (6);
286                 bytes = (header[4] << 8) + header[5];
287             skip:
288                 if (bytes > end - buf) {
289                     state = DEMUX_SKIP;
290                     state_bytes = bytes - (end - buf);
291                     return 0;
292                 }
293                 buf += bytes;
294             }
295         }
296     }
297 }
298
299 static void ps_loop (void)
300 {
301     uint8_t * end;
302
303     do {
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);
308 }
309
310 static void ts_loop (void)
311 {
312 #define PACKETS (BUFFER_SIZE / 188)
313     uint8_t * buf;
314     uint8_t * data;
315     uint8_t * end;
316     int packets;
317     int i;
318     int pid;
319
320     do {
321         packets = fread (buffer, 188, PACKETS, in_file);
322         for (i = 0; i < packets; i++) {
323             buf = buffer + i * 188;
324             end = buf + 188;
325             if (buf[0] != 0x47) {
326                 fprintf (stderr, "bad sync byte\n");
327                 exit (1);
328             }
329             pid = ((buf[1] << 8) + buf[2]) & 0x1fff;
330             if (pid != demux_pid)
331                 continue;
332             data = buf + 4;
333             if (buf[3] & 0x20) {        /* buf contains an adaptation field */
334                 data = buf + 5 + buf[4];
335                 if (data > end)
336                     continue;
337             }
338             if (buf[3] & 0x10)
339                 demux (data, end, (buf[1] & 0x40) ? DEMUX_PAYLOAD_START : 0);
340         }
341     } while (packets == PACKETS);
342 }
343
344 int main (int argc, char ** argv)
345 {
346     handle_args (argc, argv);
347
348     if (demux_pid)
349         ts_loop ();
350     else
351         ps_loop ();
352
353     return 0;
354 }