OSDN Git Service

- More robust stream duration estimation: take 16 (position,pts) samples then do...
[handbrake-jp/handbrake-jp-git.git] / libhb / stream.c
1 /* $Id$
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8 #include "lang.h"
9 #include "a52dec/a52.h"
10
11 #include <string.h>
12
13 #define min(a, b) a < b ? a : b
14
15 typedef enum { hb_stream_type_unknown = 0, hb_stream_type_transport, hb_stream_type_program } hb_stream_type_t;
16  
17 #define kMaxNumberDecodeStreams 8
18 #define kMaxNumberVideoPIDS 16
19 #define kMaxNumberAudioPIDS 16
20 //#define kVideoStream 0
21 //#define kAudioStream 1
22 #define kNumDecodeBuffers 2
23 #define kMaxNumberPMTStreams 32
24
25 #define CLOCKRATE               ((int64_t)27000000)                     // MPEG System clock rate
26 #define STREAMRATE              ((int64_t)2401587)                      // Original HD stream rate 19.2 Mbps
27 #define DEMUX                   (((int)STREAMRATE * 8) / 50)// Demux value for HD content STREAMRATE / 50
28
29 struct hb_stream_s
30 {
31     char         * path;
32         FILE             * file_handle;
33         hb_stream_type_t stream_type;
34         
35         int                              ps_current_write_buffer_index;
36         int                              ps_current_read_buffer_index;
37
38         struct {
39                 int                              size;
40                 int                              len;
41                 int                              read_pos;
42                 int                              write_pos;
43                 unsigned char *  data;
44         } ps_decode_buffer[kNumDecodeBuffers];
45         
46         struct {
47                 int lang_code;
48                 int flags;
49                 int rate;
50                 int bitrate;
51         } a52_info[kMaxNumberAudioPIDS];
52         
53         int                              ts_video_pids[kMaxNumberVideoPIDS];
54         int                              ts_audio_pids[kMaxNumberAudioPIDS];
55         
56         int                              ts_number_video_pids;
57         int                              ts_number_audio_pids;
58         
59         unsigned char*   ts_packetbuf[kMaxNumberDecodeStreams];
60         int                              ts_packetpos[kMaxNumberDecodeStreams];
61 //      int                              ts_bufpackets[kMaxNumberDecodeStreams];
62         int                              ts_foundfirst[kMaxNumberDecodeStreams];
63         int                              ts_skipbad[kMaxNumberDecodeStreams];
64         int                              ts_streamcont[kMaxNumberDecodeStreams];
65         int                              ts_streamid[kMaxNumberDecodeStreams];
66         int                              ts_audio_stream_type[kMaxNumberAudioPIDS];
67         
68         struct 
69         {
70                 unsigned short program_number;
71                 unsigned short program_map_PID;
72         } pat_info[kMaxNumberPMTStreams];
73         int      ts_number_pat_entries;
74         
75         struct
76         {
77                 int reading;
78                 unsigned char *tablebuf;
79                 unsigned int tablepos;
80                 unsigned char current_continuity_counter;
81                 
82                 int section_length;
83                 int program_number;
84                 unsigned int PCR_PID;
85                 int program_info_length;
86                 unsigned char *progam_info_descriptor_data;
87                 struct
88                 {
89                         unsigned char stream_type;
90                         unsigned short elementary_PID;
91                         unsigned short ES_info_length;
92                         unsigned char *es_info_descriptor_data;
93                 } pmt_stream_info[kMaxNumberPMTStreams];
94         } pmt_info;
95 };
96
97 /***********************************************************************
98  * Local prototypes
99  **********************************************************************/
100 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle);
101 static void hb_ts_stream_init(hb_stream_t *stream);
102 static void hb_ts_stream_find_pids(hb_stream_t *stream);
103 static void hb_ts_stream_decode(hb_stream_t *stream);
104 static void hb_ts_stream_reset(hb_stream_t *stream);
105 static void hb_stream_put_back(hb_stream_t *stream, int i);
106 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
107                                                        int aud_pid_index);
108 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title);
109 static off_t align_to_next_packet(FILE* f);
110
111 /*
112  * streams have a bunch of state that's learned during the scan. We don't
113  * want to throw away the state when scan does a close then relearn
114  * everything when reader does an open. So we basically ignore
115  * a stream close, remember the most recent stream we've opened and only
116  * delete it when a stream of a different name is opened.
117  */
118 static hb_stream_t *current_stream;
119
120
121 static inline int check_ps_sync(const uint8_t *buf)
122 {
123     // a legal MPEG program stream must start with a Pack header in the
124     // first four bytes.
125     return (buf[0] == 0x00) && (buf[1] == 0x00) &&
126            (buf[2] == 0x01) && (buf[3] == 0xba);
127 }
128
129 static inline int check_ts_sync(const uint8_t *buf)
130 {
131     // must have initial sync byte, no scrambling & a legal adaptation ctrl
132     return (buf[0] == 0x47) && ((buf[3] >> 6) == 0) && ((buf[3] >> 4) > 0);
133 }
134
135 static inline int have_ts_sync(const uint8_t *buf)
136 {
137     return check_ts_sync(&buf[0*188]) && check_ts_sync(&buf[1*188]) &&
138            check_ts_sync(&buf[2*188]) && check_ts_sync(&buf[3*188]) &&
139            check_ts_sync(&buf[4*188]) && check_ts_sync(&buf[5*188]) &&
140            check_ts_sync(&buf[6*188]) && check_ts_sync(&buf[7*188]);
141 }
142
143 static int hb_stream_check_for_ts(const uint8_t *buf)
144 {
145     // transport streams should have a sync byte every 188 bytes.
146     // search the first KB of buf looking for at least 8 consecutive
147     // correctly located sync patterns.
148     int offset = 0;
149
150     for ( offset = 0; offset < 1024; ++offset )
151     {
152         if ( have_ts_sync( &buf[offset]) )
153             return 1;
154     }
155     return 0;
156 }
157
158 static int hb_stream_check_for_ps(const uint8_t *buf)
159 {
160     // program streams should have a Pack header every 2048 bytes.
161     // check that we have 4 of these.
162     return check_ps_sync(&buf[0*2048]) && check_ps_sync(&buf[1*2048]) &&
163            check_ps_sync(&buf[2*2048]) && check_ps_sync(&buf[3*2048]);
164 }
165
166 static int hb_stream_get_type(hb_stream_t *stream)
167 {
168     uint8_t buf[2048*4];
169
170     if ( fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) )
171     {
172         if ( hb_stream_check_for_ts(buf) != 0 )
173         {
174             hb_log("file is MPEG Transport Stream");
175             stream->stream_type = hb_stream_type_transport;
176             hb_ts_stream_init(stream);
177             return 1;
178         }
179         if ( hb_stream_check_for_ps(buf) != 0 )
180         {
181             hb_log("file is MPEG Program Stream");
182             stream->stream_type = hb_stream_type_program;
183             return 1;
184         }
185     }
186     return 0;
187 }
188
189 static void hb_stream_delete( hb_stream_t ** _d )
190 {
191     hb_stream_t * d = *_d;
192
193     if( d->file_handle )
194     {
195         fclose( d->file_handle );
196                 d->file_handle = NULL;
197     }
198
199         int i=0;
200         for (i = 0; i < kNumDecodeBuffers; i++)
201         {
202                 if (d->ps_decode_buffer[i].data)
203                 {
204                         free(d->ps_decode_buffer[i].data);
205                         d->ps_decode_buffer[i].data = NULL;
206                 }
207         }
208         
209         for (i = 0; i < kMaxNumberDecodeStreams; i++)
210         {
211                 if (d->ts_packetbuf[i])
212                 {
213                         free(d->ts_packetbuf[i]);
214                         d->ts_packetbuf[i] = NULL;
215                 }
216         }
217     free( d->path );
218     free( d );
219     *_d = NULL;
220 }
221
222 /***********************************************************************
223  * hb_stream_open
224  ***********************************************************************
225  *
226  **********************************************************************/
227 hb_stream_t * hb_stream_open( char * path )
228 {
229     if (current_stream)
230     {
231         if (strcmp( path, current_stream->path ) == 0 )
232         {
233             hb_stream_seek( current_stream, 0. );
234             return current_stream;
235         }
236         hb_stream_delete( &current_stream );
237     }
238     hb_stream_t *d = calloc( sizeof( hb_stream_t ), 1 );
239
240     /* open the file and see if it's a type we know about. return a stream
241      * reference structure if we can deal with it & NULL otherwise. */
242     if( ( d->file_handle = fopen( path, "rb" ) ) )
243     {
244         d->path = strdup( path );
245         if (d->path != NULL &&  hb_stream_get_type( d ) != 0 )
246         {
247             current_stream = d;
248             return d;
249         }
250         fclose( d->file_handle );
251         if (d->path)
252             free( d->path );
253     }
254     hb_log( "hb_stream_open: open %s failed", path );
255     free( d );
256     return NULL;
257 }
258
259 /***********************************************************************
260  * hb_stream_close
261  ***********************************************************************
262  * Closes and frees everything
263  **********************************************************************/
264 void hb_stream_close( hb_stream_t ** _d )
265 {
266 }
267
268 /* when the file was first opened we made entries for all the audio elementary
269  * streams we found in it. Streams that were later found during the preview scan
270  * now have an audio codec, type, rate, etc., associated with them. At the end
271  * of the scan we delete all the audio entries that weren't found by the scan
272  * or don't have a format we support. This routine deletes audio entry 'indx'
273  * by copying all later entries down one slot. */
274 static void hb_stream_delete_audio_entry(hb_stream_t *stream, int indx)
275 {
276     int i;
277
278     for (i = indx+1; i < stream->ts_number_audio_pids; ++i)
279     {
280         stream->ts_audio_pids[indx] = stream->ts_audio_pids[i];
281         stream->ts_audio_stream_type[indx] = stream->ts_audio_stream_type[i];
282         stream->ts_streamid[stream->ts_number_video_pids + indx] =
283             stream->ts_streamid[stream->ts_number_video_pids + i];
284         ++indx;
285     }
286     --stream->ts_number_audio_pids;
287 }
288
289 /***********************************************************************
290  * hb_ps_stream_title_scan
291  ***********************************************************************
292  *
293  **********************************************************************/
294 hb_title_t * hb_stream_title_scan(hb_stream_t *stream)
295 {
296     // 'Barebones Title'
297     hb_title_t *aTitle = hb_title_init( stream->path, 0 );
298     aTitle->index = 1;
299
300         // Copy part of the stream path to the title name
301         char *sep = strrchr(stream->path, '/');
302         if (sep)
303                 strcpy(aTitle->name, sep+1);
304         char *dot_term = strrchr(aTitle->name, '.');
305         if (dot_term)
306                 *dot_term = '\0';
307         
308     // Height, width,  rate and aspect ratio information is filled in when the previews are built
309
310     hb_stream_duration(stream, aTitle);
311     
312     // One Chapter
313     hb_chapter_t * chapter;
314     chapter = calloc( sizeof( hb_chapter_t ), 1 );
315     chapter->index = 1;
316     chapter->duration = aTitle->duration;
317     chapter->hours = aTitle->hours;
318     chapter->minutes = aTitle->minutes;
319     chapter->seconds = aTitle->seconds;
320     hb_list_add( aTitle->list_chapter, chapter );
321     
322     // Figure out how many audio streams we really have:
323     // - For transport streams, for each PID listed in the PMT (whether
324     //   or not it was an audio stream type) read the bitstream until we
325     //   find an packet from that PID containing a PES header and see if
326     //   the elementary stream is an audio type.
327     // - For program streams read the first 4MB and take every unique
328     //   audio stream we find.
329         if (stream->stream_type == hb_stream_type_transport)
330         {
331         int i;
332
333         for (i=0; i < stream->ts_number_audio_pids; i++)
334         {
335             hb_audio_t *audio = hb_ts_stream_set_audio_id_and_codec(stream, i);
336             if (audio->codec)
337                 hb_list_add( aTitle->list_audio, audio );
338             else
339             {
340                 free(audio);
341                 hb_stream_delete_audio_entry(stream, i);
342                 --i;
343             }
344         }
345         }
346     else
347     {
348         hb_ps_stream_find_audio_ids(stream, aTitle);
349     }
350
351   return aTitle;
352 }
353
354 /*
355  * scan the next MB of 'stream' to find the next start packet for
356  * the Packetized Elementary Stream associated with TS PID 'pid'.
357  */
358 static const uint8_t *hb_ts_stream_getPEStype(hb_stream_t *stream, uint32_t pid)
359 {
360     static uint8_t buf[188];
361     int npack = 100000; // max packets to read
362
363     while (--npack >= 0)
364     {
365         if (fread(buf, 1, 188, stream->file_handle) != 188)
366         {
367             hb_log("hb_ts_stream_getPEStype: EOF while searching for PID 0x%x", pid);
368             return 0;
369         }
370         if (buf[0] != 0x47)
371         {
372             hb_log("hb_ts_stream_getPEStype: lost sync while searching for PID 0x%x", pid);
373             align_to_next_packet(stream->file_handle);
374             continue;
375         }
376
377         /*
378          * The PES header is only in TS packets with 'start' set so we check
379          * that first then check for the right PID.
380          */
381         if ((buf[1] & 0x40) == 0 || (buf[1] & 0x1f) != (pid >> 8) ||
382             buf[2] != (pid & 0xff))
383         {
384             // not a start packet or not the pid we want
385             continue;
386         }
387
388         /* skip over the TS hdr to return a pointer to the PES hdr */
389         int udata = 4;
390         switch (buf[3] & 0x30)
391         {
392             case 0x00: // illegal
393             case 0x20: // fill packet
394                 continue;
395
396             case 0x30: // adaptation
397                 if (buf[4] > 182)
398                 {
399                     hb_log("hb_ts_stream_getPEStype: invalid adaptation field length %d for PID 0x%x", buf[4], pid);
400                     continue;
401                 }
402                 udata += buf[4] + 1;
403                 break;
404         }
405         return &buf[udata];
406     }
407
408     /* didn't find it */
409     return 0;
410 }
411
412 static uint64_t hb_ps_stream_getVideoPTS(hb_stream_t *stream)
413 {
414     hb_buffer_t *buf  = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
415     hb_list_t *list = hb_list_init();
416     // how many blocks we read while searching for a video PES header
417     int blksleft = 1024;
418     uint64_t pts = 0;
419
420     while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
421     {
422         hb_buffer_t *es;
423
424         // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
425         hb_demux_ps(buf, list);
426
427         while ( ( es = hb_list_item( list, 0 ) ) )
428         {
429             hb_list_rem( list, es );
430             if ( es->id == 0xe0 )
431             {
432                 // this PES contains video - if there's a PTS we're done
433                 // hb_demux_ps left the PTS in buf_es->start.
434                 if ( es->start != ~0 )
435                 {
436                     pts = es->start;
437                     blksleft = 0;
438                     break;
439                 }
440             }
441             hb_buffer_close( &es );
442         }
443     }
444     hb_list_empty( &list );
445     hb_buffer_close(&buf);
446     return pts;
447 }
448
449 /***********************************************************************
450  * hb_stream_duration
451  ***********************************************************************
452  *
453  * Finding stream duration is difficult.  One issue is that the video file
454  * may have chunks from several different program fragments (main feature,
455  * commercials, station id, trailers, etc.) all with their own base pts
456  * value.  We can't find the piece boundaries without reading the entire
457  * file but if we compute a rate based on time stamps from two different
458  * pieces the result will be meaningless.  The second issue is that the
459  * data rate of compressed video normally varies by 5-10x over the length
460  * of the video. This says that we want to compute the rate over relatively
461  * long segments to get a representative average but long segments increase
462  * the likelihood that we'll cross a piece boundary.
463  * 
464  * What we do is take time stamp samples at several places in the file
465  * (currently 16) then compute the average rate (i.e., ticks of video per
466  * byte of the file) for all pairs of samples (N^2 rates computed for N
467  * samples). Some of those rates will be absurd because the samples came
468  * from different segments. Some will be way low or high because the
469  * samples came from a low or high motion part of the segment. But given
470  * that we're comparing *all* pairs the majority of the computed rates
471  * should be near the overall average.  So we median filter the computed
472  * rates to pick the most representative value.
473  *
474  **********************************************************************/
475 struct pts_pos {
476     uint64_t pos;   /* file position of this PTS sample */
477     uint64_t pts;   /* PTS from video stream */
478 };
479
480 #define NDURSAMPLES 16
481
482 // get one (position, timestamp) sampple from a transport or program
483 // stream.
484 static struct pts_pos hb_sample_pts(hb_stream_t *stream, uint64_t fpos)
485 {
486     struct pts_pos pp = { 0, 0 };
487
488     if ( stream->stream_type == hb_stream_type_program )
489     {
490         // round address down to nearest dvd sector start
491         fpos &=~ ( HB_DVD_READ_BUFFER_SIZE - 1 );
492         fseeko( stream->file_handle, fpos, SEEK_SET );
493         pp.pts = hb_ps_stream_getVideoPTS( stream );
494     }
495     else
496     {
497         const uint8_t *buf;
498         fseeko( stream->file_handle, fpos, SEEK_SET );
499         align_to_next_packet( stream->file_handle );
500         buf = hb_ts_stream_getPEStype( stream, stream->ts_video_pids[0] );
501         if ( buf == NULL )
502         {
503             hb_log("hb_sample_pts: couldn't find video packet near %llu", fpos);
504             return pp;
505         }
506         if ( ( buf[7] >> 7 ) != 1 )
507         {
508             hb_log("hb_sample_pts: no PTS in video packet near %llu", fpos);
509             return pp;
510         }
511         pp.pts = ( ( (uint64_t)buf[9] >> 1 ) & 7 << 30 ) |
512                  ( (uint64_t)buf[10] << 22 ) |
513                  ( ( (uint64_t)buf[11] >> 1 ) << 15 ) |
514                  ( (uint64_t)buf[12] << 7 ) |
515                  ( (uint64_t)buf[13] >> 1 );
516     }
517     pp.pos = ftello(stream->file_handle);
518     hb_log("hb_sample_pts: pts %lld at %llu", pp.pts, pp.pos );
519     return pp;
520 }
521
522 static int dur_compare( const void *a, const void *b )
523 {
524     const double *aval = a, *bval = b;
525     return ( *aval < *bval ? -1 : ( *aval == *bval ? 0 : 1 ) );
526 }
527
528 // given an array of (position, time) samples, compute a max-likelihood
529 // estimate of the average rate by computing the rate between all pairs
530 // of samples then taking the median of those rates.
531 static double compute_stream_rate( struct pts_pos *pp, int n )
532 {
533     int i, j;
534     double rates[NDURSAMPLES * NDURSAMPLES / 2];
535     double *rp = rates;
536
537     // the following nested loops compute the rates between all pairs.
538     *rp = 0;
539     for ( i = 0; i < n-1; ++i )
540     {
541         // Bias the median filter by not including pairs that are "far"
542         // frome one another. This is to handle cases where the file is
543         // made of roughly equal size pieces where a symmetric choice of
544         // pairs results in having the same number of intra-piece &
545         // inter-piece rate estimates. This would mean that the median
546         // could easily fall in the inter-piece part of the data which
547         // would give a bogus estimate. The 'ns' index creates an
548         // asymmetry that favors locality.
549         int ns = i + ( n >> 1 );
550         if ( ns > n )
551             ns = n;
552         for ( j = i+1; j < ns; ++j )
553         {
554             if ( pp[j].pts != pp[i].pts && pp[j].pos > pp[i].pos )
555             {
556                 *rp = ((double)( pp[j].pts - pp[i].pts )) /
557                       ((double)( pp[j].pos - pp[i].pos ));
558                                 ++rp;
559             }
560         }
561     }
562     // now compute and return the median of all the (n*n/2) rates we computed
563     // above.
564     int nrates = rp - rates;
565     qsort( rates, nrates, sizeof (rates[0] ), dur_compare );
566     return rates[nrates >> 1];
567 }
568
569 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle)
570 {
571     struct pts_pos ptspos[NDURSAMPLES];
572     struct pts_pos *pp = ptspos;
573     int i;
574
575     fseeko(stream->file_handle, 0, SEEK_END);
576     uint64_t fsize = ftello(stream->file_handle);
577     uint64_t fincr = fsize / NDURSAMPLES;
578     uint64_t fpos = fincr / 2;
579     for ( i = NDURSAMPLES; --i >= 0; fpos += fincr )
580     {
581         *pp++ = hb_sample_pts(stream, fpos);
582     }
583     uint64_t dur = compute_stream_rate( ptspos, pp - ptspos ) * (double)fsize;
584     inTitle->duration = dur;
585     dur /= 90000;
586     inTitle->hours    = dur / 3600;
587     inTitle->minutes  = ( dur % 3600 ) / 60;
588     inTitle->seconds  = dur % 60;
589
590     rewind(stream->file_handle);
591 }
592
593 /***********************************************************************
594  * hb_stream_read
595  ***********************************************************************
596  *
597  **********************************************************************/
598 int hb_stream_read( hb_stream_t * src_stream, hb_buffer_t * b )
599 {
600   if (src_stream->stream_type == hb_stream_type_program)
601   {
602           size_t amt_read;
603           amt_read = fread(b->data, HB_DVD_READ_BUFFER_SIZE, 1, src_stream->file_handle);
604           if (amt_read > 0)
605                 return 1;
606           else
607                 return 0;
608   }
609   else if  (src_stream->stream_type == hb_stream_type_transport)
610   {
611         int read_buffer_index = src_stream->ps_current_read_buffer_index;
612
613         // Transport streams are a little more complex  - we might be able to just
614         // read from the transport stream conversion buffer (if there's enough data)
615         // or we may need to transfer what's left and fill it again.
616         if (src_stream->ps_decode_buffer[read_buffer_index].len
617           - src_stream->ps_decode_buffer[read_buffer_index].read_pos
618         >= HB_DVD_READ_BUFFER_SIZE)
619         {
620                 memcpy(b->data,
621                src_stream->ps_decode_buffer[read_buffer_index].data +
622                  src_stream->ps_decode_buffer[read_buffer_index].read_pos,
623                HB_DVD_READ_BUFFER_SIZE);
624                 src_stream->ps_decode_buffer[read_buffer_index].read_pos += HB_DVD_READ_BUFFER_SIZE;
625                 return 1;
626         }
627         else
628         {
629                 // Not quite enough data in the buffer - transfer what is present, fill the buffer and then 
630                 // transfer what's still needed.
631                 int transfer_size = HB_DVD_READ_BUFFER_SIZE;
632                 int amt_avail_to_transfer = src_stream->ps_decode_buffer[read_buffer_index].len - src_stream->ps_decode_buffer[read_buffer_index].read_pos;
633                 memcpy(b->data, src_stream->ps_decode_buffer[read_buffer_index].data + src_stream->ps_decode_buffer[read_buffer_index].read_pos, amt_avail_to_transfer);
634                 transfer_size -= amt_avail_to_transfer;
635
636                 // Give up this buffer - decoding may well need it, and we're done
637                 src_stream->ps_decode_buffer[read_buffer_index].read_pos = 0;
638                 src_stream->ps_decode_buffer[read_buffer_index].write_pos = 0;
639                 src_stream->ps_decode_buffer[read_buffer_index].len = 0;
640                 
641                 // Fill the buffer
642                 hb_ts_stream_decode(src_stream);
643                 
644                 // Decoding will almost certainly have changed the current read buffer index
645                 read_buffer_index = src_stream->ps_current_read_buffer_index;
646                 
647                 if (src_stream->ps_decode_buffer[read_buffer_index].len == 0)
648                 {
649                         hb_log("hb_stream_read - buffer after decode has zero length data");
650                         return 0;
651                 }
652                 
653                 // Read the bit we still need
654                 memcpy(b->data+amt_avail_to_transfer, src_stream->ps_decode_buffer[read_buffer_index].data + src_stream->ps_decode_buffer[read_buffer_index].read_pos,transfer_size);
655                 src_stream->ps_decode_buffer[read_buffer_index].read_pos += transfer_size;
656                 
657                 return 1;
658         }       
659   }
660   else
661         return 0;
662 }
663
664 /***********************************************************************
665  * hb_stream_seek
666  ***********************************************************************
667  *
668  **********************************************************************/
669 int hb_stream_seek( hb_stream_t * src_stream, float f )
670 {
671   off_t stream_size, cur_pos, new_pos;
672   double pos_ratio = f;
673   cur_pos = ftello(src_stream->file_handle);
674   fseeko(src_stream->file_handle,0 ,SEEK_END);
675   stream_size = ftello(src_stream->file_handle);
676   new_pos = (off_t) ((double) (stream_size) * pos_ratio);
677   new_pos &=~ (HB_DVD_READ_BUFFER_SIZE - 1);
678   int r = fseeko(src_stream->file_handle, new_pos, SEEK_SET);
679   
680   if (r == -1)
681   {
682     fseeko(src_stream->file_handle, cur_pos, SEEK_SET);
683     return 0;
684   }
685   
686   if (src_stream->stream_type == hb_stream_type_transport)
687   {
688         // We need to drop the current decoder output and move
689         // forwards to the next transport stream packet.
690         hb_ts_stream_reset(src_stream);
691   }
692   
693   // Now we must scan forwards for a valid start code (0x000001BA)
694   int done = 0;
695   hb_buffer_t *buf = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
696   while (!done)
697   {
698     if (hb_stream_read(src_stream,buf) == 1)
699     {
700       int i=0;
701       for (i=0; (i <= HB_DVD_READ_BUFFER_SIZE-4) && (!done); i++)
702       {
703         if ((buf->data[i] == 0x00) && (buf->data[i+1] == 0x00) && (buf->data[i+2] == 0x01) && (buf->data[i+3] == 0xba))
704         {
705           done = 1;
706                   // 'Put Back' the data we've just read (up to this point)
707                   hb_stream_put_back(src_stream, i);
708         }
709       }
710     }
711     else
712       done = 1;    // End of data;
713   }
714   hb_buffer_close(&buf);
715   return 1;
716 }
717
718 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
719                                                        int aud_pid_index)
720 {
721     off_t cur_pos = ftello(stream->file_handle);
722     hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
723     const uint8_t *buf;
724
725     fseeko(stream->file_handle, 0, SEEK_SET);
726     align_to_next_packet(stream->file_handle);
727     buf = hb_ts_stream_getPEStype(stream, stream->ts_audio_pids[aud_pid_index]);
728
729     /* check that we found a PES header */
730     if (buf && buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
731     {
732         if (buf[3] == 0xbd)
733         {
734             audio->id = 0x80bd | (aud_pid_index << 8);
735             audio->codec = HB_ACODEC_AC3;
736             hb_log("transport stream pid 0x%x (type 0x%x) is AC-3 audio id 0x%x",
737                    stream->ts_audio_pids[aud_pid_index],
738                    stream->ts_audio_stream_type[aud_pid_index],
739                    audio->id);
740             stream->ts_audio_stream_type[aud_pid_index] = 0x81;
741             stream->ts_streamid[stream->ts_number_video_pids + aud_pid_index] = buf[3];
742         }
743         else if ((buf[3] & 0xe0) == 0xc0)
744         {
745             audio->id = buf[3] | aud_pid_index;
746             audio->codec = HB_ACODEC_MPGA;
747             hb_log("transport stream pid 0x%x (type 0x%x) is MPEG audio id 0x%x",
748                    stream->ts_audio_pids[aud_pid_index],
749                    stream->ts_audio_stream_type[aud_pid_index],
750                    audio->id);
751             stream->ts_audio_stream_type[aud_pid_index] = 0x03;
752             stream->ts_streamid[stream->ts_number_video_pids + aud_pid_index] = buf[3];
753         }
754     }
755     fseeko(stream->file_handle, cur_pos, SEEK_SET);
756     if (! audio->codec)
757     {
758         hb_log("transport stream pid 0x%x (type 0x%x) isn't audio",
759                 stream->ts_audio_pids[aud_pid_index],
760                 stream->ts_audio_stream_type[aud_pid_index]);
761         }
762     return audio;
763 }
764
765 static void add_audio_to_title(hb_title_t *title, int id)
766 {
767     hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
768
769     audio->id = id;
770     switch ( id >> 12 )
771     {
772         case 0x0:
773             audio->codec = HB_ACODEC_MPGA;
774             hb_log("add_audio_to_title: added MPEG audio stream 0x%x", id);
775             break;
776         case 0x2:
777             // type 2 is a DVD subtitle stream - just ignore it */
778             free( audio );
779             return;
780         case 0x8:
781             audio->codec = HB_ACODEC_AC3;
782             hb_log("add_audio_to_title: added AC3 audio stream 0x%x", id);
783             break;
784         case 0xa:
785             audio->codec = HB_ACODEC_LPCM;
786             hb_log("add_audio_to_title: added LPCM audio stream 0x%x", id);
787             break;
788         default:
789             hb_log("add_audio_to_title: unknown audio stream type 0x%x", id);
790             free( audio );
791             return;
792
793     }
794     hb_list_add( title->list_audio, audio );
795 }
796
797 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title)
798 {
799     off_t cur_pos = ftello(stream->file_handle);
800     hb_buffer_t *buf  = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
801     hb_list_t *list = hb_list_init();
802     // how many blocks we read while searching for audio streams
803     int blksleft = 4096;
804     // there can be at most 16 unique streams in an MPEG PS (8 in a DVD)
805     // so we use a bitmap to keep track of the ones we've already seen.
806     // Bit 'i' of smap is set if we've already added the audio for
807     // audio substream id 'i' to the title's audio list.
808     uint32_t smap = 0;
809
810     // start looking 20% into the file since there's occasionally no
811     // audio at the beginning (particularly for vobs).
812     hb_stream_seek(stream, 0.2f);
813                 
814     while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
815     {
816         hb_buffer_t *es;
817
818         // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
819         hb_demux_ps(buf, list);
820
821         while ( ( es = hb_list_item( list, 0 ) ) )
822         {
823             hb_list_rem( list, es );
824             if ( (es->id & 0xff) == 0xbd || (es->id & 0xe0) == 0xc0 )
825             {
826                 // this PES contains some kind of audio - get the substream id
827                 // and check if we've seen it already.
828                 int ssid = (es->id > 0xff ? es->id >> 8 : es->id) & 0xf;
829                 if ( (smap & (1 << ssid)) == 0 )
830                 {
831                     // we haven't seen this stream before - add it to the
832                     // title's list of audio streams.
833                     smap |= (1 << ssid);
834                     add_audio_to_title(title, es->id);
835                 }
836             }
837             hb_buffer_close( &es );
838         }
839     }
840     hb_list_empty( &list );
841     hb_buffer_close(&buf);
842     fseeko(stream->file_handle, cur_pos, SEEK_SET);
843 }
844
845 /***********************************************************************
846  * hb_stream_update_audio
847  ***********************************************************************
848  *
849  **********************************************************************/
850 void hb_stream_update_audio(hb_stream_t *stream, hb_audio_t *audio)
851 {
852         iso639_lang_t *lang;
853         
854     if (stream->stream_type == hb_stream_type_transport)
855         {
856         // Find the audio stream info for this PID. The stream index is
857         // the subchannel id which is in the bottom four bits for MPEG audio
858         // and the bottom four bits of the upper byte for everything else.
859         int i = ( audio->id >= 0xd0 ? audio->id >> 8 : audio->id ) & 0xf;
860         if (i >= stream->ts_number_audio_pids)
861                 {
862             hb_log("hb_stream_update_audio: no PID for audio stream 0x%x",
863                     audio->id);
864                         return;
865                 }
866         if (audio->id < 0xd0)
867         {
868             /* XXX fake mpeg audio sample rate & bps */
869             stream->a52_info[i].flags = A52_STEREO;
870             stream->a52_info[i].rate = 48000 /*Hz*/;
871             stream->a52_info[i].bitrate = 384000 /*Bps*/;
872         }
873                 
874                 lang = lang_for_code(stream->a52_info[i].lang_code);
875                 if (!audio->rate)
876                         audio->rate = stream->a52_info[i].rate;
877                 if (!audio->bitrate)
878                         audio->bitrate = stream->a52_info[i].bitrate;
879                 if (!audio->config.a52.ac3flags)
880                         audio->config.a52.ac3flags = audio->ac3flags = stream->a52_info[i].flags;
881
882         }
883     else
884     {
885         // XXX should try to get language code from the AC3 bitstream
886         lang = lang_for_code(0x0000);
887     }
888         
889         if (!audio->input_channel_layout)
890         {
891                 switch( audio->ac3flags & A52_CHANNEL_MASK )
892                 {
893                         /* mono sources */
894                         case A52_MONO:
895                         case A52_CHANNEL1:
896                         case A52_CHANNEL2:
897                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
898                                 break;
899                         /* stereo input */
900                         case A52_CHANNEL:
901                         case A52_STEREO:
902                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
903                                 break;
904                         /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
905                         case A52_DOLBY:
906                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
907                                 break;
908                         /* 3F/2R input */
909                         case A52_3F2R:
910                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
911                                 break;
912                         /* 3F/1R input */
913                         case A52_3F1R:
914                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
915                                 break;
916                         /* other inputs */
917                         case A52_3F:
918                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
919                                 break;
920                         case A52_2F1R:
921                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
922                                 break;
923                         case A52_2F2R:
924                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
925                                 break;
926                         /* unknown */
927                         default:
928                                 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
929                 }
930
931                 /* add in our own LFE flag if the source has LFE */
932                 if (audio->ac3flags & A52_LFE)
933                 {
934                         audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
935                 }
936         }
937         
938         snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)", strlen(lang->native_name) ? lang->native_name : lang->eng_name,
939           audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec == HB_ACODEC_MPGA ? "MPEG" : ( audio->codec == HB_ACODEC_DCA ? "DTS" : "LPCM" ) ) );
940         snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s", strlen(lang->native_name) ? lang->native_name : lang->eng_name );
941         snprintf( audio->iso639_2, sizeof( audio->iso639_2 ), "%s", lang->iso639_2);
942
943         if ( (audio->ac3flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
944                 sprintf( audio->lang + strlen( audio->lang ),
945                          " (Dolby Surround)" );
946         } else {
947                 sprintf( audio->lang + strlen( audio->lang ),
948                          " (%d.%d ch)",
949                         HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(audio->input_channel_layout) +
950                         HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(audio->input_channel_layout),
951                         HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(audio->input_channel_layout));
952         }
953
954         hb_log( "hb_stream_update_audio: id=%x, lang=%s, 3cc=%s, rate = %d, bitrate = %d, flags = 0x%x (%d)", audio->id, audio->lang, audio->iso639_2, audio->rate, audio->bitrate, audio->ac3flags, audio->ac3flags );
955
956 }
957
958 /***********************************************************************
959  * hb_stream_put_back
960  ***********************************************************************
961  *
962  **********************************************************************/
963 static void hb_stream_put_back(hb_stream_t *stream, int i)
964 {
965         if (stream->stream_type == hb_stream_type_program)
966         {
967                 // Program streams are pretty easy - we just reposition the source file
968                 // pointer
969                 fseeko(stream->file_handle, -(HB_DVD_READ_BUFFER_SIZE-i), SEEK_CUR);
970         }
971         else if (stream->stream_type == hb_stream_type_transport)
972         {
973                 int read_buffer_index = stream->ps_current_read_buffer_index;
974                 
975                 // Transport streams are a little more tricky - so long as the 
976                 // amount to back up is still within the current decode buffer
977                 // we can just adjust the read pos.
978                 if (stream->ps_decode_buffer[read_buffer_index].read_pos - i > 0)
979                 {
980                         stream->ps_decode_buffer[read_buffer_index].read_pos -= i;
981                 }
982                 else
983                   hb_error("hb_stream_put_back - trying to step beyond the start of the buffer, read_pos = %d amt to put back = %d\n", stream->ps_decode_buffer[read_buffer_index].read_pos, i);
984         }
985 }
986
987
988 /***********************************************************************
989  * hb_ts_stream_init
990  ***********************************************************************
991  *
992  **********************************************************************/
993  #define PS_DECODE_BUFFER_SIZE ( 1024 * 1024 * 4)
994  
995 static void hb_ts_stream_init(hb_stream_t *stream)
996 {
997         // Output Program Stream
998         int i=0;
999         for (i=0; i < kNumDecodeBuffers; i++)
1000         {
1001                 stream->ps_decode_buffer[i].data = (unsigned char *) malloc(PS_DECODE_BUFFER_SIZE);
1002                 stream->ps_decode_buffer[i].read_pos = 0;
1003                 stream->ps_decode_buffer[i].size = PS_DECODE_BUFFER_SIZE;
1004                 stream->ps_decode_buffer[i].len = 0;
1005                 stream->ps_decode_buffer[i].write_pos = 0;
1006         }
1007         
1008         for (i=0; i < kMaxNumberDecodeStreams; i++)
1009         {
1010                 stream->ts_streamcont[i] = -1;
1011         }
1012         
1013         stream->ps_current_write_buffer_index = 0;
1014         stream->ps_current_read_buffer_index = 1;
1015         
1016         // Find the audio and video pids in the stream
1017         hb_ts_stream_find_pids(stream);
1018         
1019         for (i=0; i < stream->ts_number_video_pids; i++)
1020         {
1021                 // In progress audio/video data during the transport stream -> program stream processing
1022                 stream->ts_packetbuf[i] = (unsigned char *) malloc(1024 * 1024);
1023                 stream->ts_streamid[i] = 0xE0;          // Stream is Video
1024         }
1025         
1026         for (i = stream->ts_number_video_pids; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1027         {
1028                 stream->ts_packetbuf[i] = (unsigned char *) malloc(1024 * 1024);
1029         }
1030 }
1031
1032 // ------------------------------------------------------------------------------------
1033
1034 static off_t align_to_next_packet(FILE* f)
1035 {
1036         unsigned char buf[188*20];
1037
1038         off_t start = ftello(f);
1039         off_t pos = 0;
1040
1041         if (fread(buf, 188*20, 1, f) == 1)
1042         {
1043                 int found = 0;
1044                 while (!found && (pos < 188))
1045                 {
1046                         found = 1;
1047                         int i = 0;
1048                         for (i = 0; i < 188*20; i += 188)
1049                         {
1050                                 unsigned char c = buf[pos+i];
1051                                 // Check sync byte
1052                                 if ((c != 0x47) && (c != 0x72) && (c != 0x29))
1053                                 {
1054                                         // this offset failed, try next
1055                                         found = 0;
1056                                         pos++;
1057                                         break;
1058                                 }
1059                         }
1060                 }
1061         }
1062
1063         if (pos == 188)
1064                 pos = 0;                // failed to find anything!!!!!?
1065
1066     fseeko(f, start+pos, SEEK_SET);
1067
1068         return pos;
1069 }
1070
1071 // ------------------------------------------------------------------------------------
1072
1073 int bitpos = 0;
1074 unsigned int bitval = 0;
1075 unsigned char* bitbuf = NULL;
1076 unsigned int bitmask[] = {
1077         0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,
1078         0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff,0xffff,
1079         0x1ffff,0x3ffff,0x7ffff,0xfffff,0x1fffff,0x3fffff,0x7fffff,0xffffff,
1080         0x1ffffff,0x3ffffff,0x7ffffff,0xfffffff,0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
1081
1082 static inline void set_buf(unsigned char* buf, int bufsize, int clear)
1083 {
1084         bitpos = 0;
1085         bitbuf = buf;
1086         bitval = (bitbuf[0] << 24) | (bitbuf[1] << 16) | (bitbuf[2] << 8) | bitbuf[3];
1087         if (clear)
1088                 memset(bitbuf, 0, bufsize);
1089 }
1090
1091 static inline int buf_size()
1092 {
1093         return bitpos >> 3;
1094 }
1095
1096 static inline void set_bits(unsigned int val, int bits)
1097 {
1098         val &= bitmask[bits];
1099
1100         while (bits > 0)
1101         {
1102                 int bitsleft = (8 - (bitpos & 7));
1103                 if (bits >= bitsleft)
1104                 {
1105                         bitbuf[bitpos >> 3] |= val >> (bits - bitsleft);
1106                         bitpos += bitsleft;
1107                         bits -= bitsleft;
1108                         val &= bitmask[bits];
1109                 }
1110                 else
1111                 {
1112                         bitbuf[bitpos >> 3] |= val << (bitsleft - bits);
1113                         bitpos += bits;
1114                         bits = 0;
1115                 }
1116         }
1117 }
1118
1119 static inline unsigned int get_bits(int bits)
1120 {
1121         unsigned int val;
1122         int left = 32 - (bitpos & 31);
1123
1124         if (bits < left)
1125         {
1126                 val = (bitval >> (left - bits)) & bitmask[bits];
1127                 bitpos += bits;
1128         }
1129         else
1130         {
1131                 val = (bitval & bitmask[left]) << (bits - left);
1132                 bitpos += left;
1133                 bits -= left;
1134
1135                 int pos = bitpos >> 3;
1136                 bitval = (bitbuf[pos] << 24) | (bitbuf[pos + 1] << 16) | (bitbuf[pos + 2] << 8) | bitbuf[pos + 3];
1137                 
1138                 if (bits > 0)
1139                 {
1140                         val |= (bitval >> (32 - bits)) & bitmask[bits];
1141                         bitpos += bits;
1142                 }
1143         }
1144
1145         return val;
1146 }
1147
1148 // extract what useful information we can from the elementary stream
1149 // descriptor list at 'dp' and add it to the stream at 'esindx'.
1150 // Descriptors with info we don't currently use are ignored.
1151 // The descriptor list & descriptor item formats are defined in
1152 // ISO 13818-1 (2000E) section 2.6 (pg. 62).
1153 static void decode_element_descriptors(hb_stream_t* stream, int esindx,
1154                                        const uint8_t *dp, uint8_t dlen)
1155 {
1156     const uint8_t *ep = dp + dlen;
1157
1158     while (dp < ep)
1159     {
1160         switch (dp[0])
1161         {
1162             case 10:    // ISO_639_language descriptor
1163                 stream->a52_info[esindx].lang_code = lang_to_code(lang_for_code2((const char *)&dp[2]));
1164                 break;
1165
1166             default:
1167                 break;
1168         }
1169         dp += dp[1] + 2;
1170     }
1171 }
1172
1173 int decode_program_map(hb_stream_t* stream)
1174 {
1175         set_buf(stream->pmt_info.tablebuf, stream->pmt_info.tablepos, 0);
1176
1177     get_bits(8);  // table_id
1178     get_bits(4);
1179     unsigned int section_length = get_bits(12);
1180     stream->pmt_info.section_length = section_length;
1181
1182     unsigned int program_number = get_bits(16);
1183     stream->pmt_info.program_number = program_number;
1184     get_bits(2);
1185     get_bits(5);  // version_number
1186     get_bits(1);
1187     get_bits(8);  // section_number
1188     get_bits(8);  // last_section_number
1189     get_bits(3);
1190     unsigned int PCR_PID = get_bits(13);
1191     stream->pmt_info.PCR_PID = PCR_PID;
1192     get_bits(4);
1193     unsigned int program_info_length = get_bits(12);
1194     stream->pmt_info.program_info_length = program_info_length;
1195
1196         int i=0;
1197         unsigned char *descriptor_buf = (unsigned char *) malloc(program_info_length);
1198         for (i = 0; i < program_info_length; i++)
1199         {
1200           descriptor_buf[i] = get_bits(8);
1201         }                                 
1202         
1203         int cur_pos =  9 /* data after the section length field*/ + program_info_length;
1204         int done_reading_stream_types = 0;
1205         while (!done_reading_stream_types)
1206         {
1207           unsigned char stream_type = get_bits(8);
1208                   get_bits(3);
1209           unsigned int elementary_PID = get_bits(13);
1210                   get_bits(4);
1211           unsigned int ES_info_length = get_bits(12);
1212           
1213           int i=0;
1214           unsigned char *ES_info_buf = (unsigned char *) malloc(ES_info_length);
1215           for (i=0; i < ES_info_length; i++)
1216           {
1217                 ES_info_buf[i] = get_bits(8);
1218           }
1219         
1220           if (stream_type == 0x02)
1221           {
1222                 if (stream->ts_number_video_pids <= kMaxNumberVideoPIDS)
1223                   stream->ts_number_video_pids++;
1224                 stream->ts_video_pids[stream->ts_number_video_pids-1] = elementary_PID;
1225           }
1226       else
1227       {
1228         // Defined audio stream types are 0x81 for AC-3/A52 audio and 0x03
1229         // for mpeg audio. But content producers seem to use other
1230         // values (0x04 and 0x06 have both been observed) so at this point
1231         // we say everything that isn't a video pid is audio then at the end
1232         // of hb_stream_title_scan we'll figure out which are really audio
1233         // by looking at the PES headers.
1234         i = stream->ts_number_audio_pids;
1235         if (i < kMaxNumberAudioPIDS)
1236             stream->ts_number_audio_pids++;
1237         stream->ts_audio_pids[i] = elementary_PID;
1238         stream->ts_audio_stream_type[i] = stream_type;
1239                 
1240                 if (ES_info_length > 0)
1241                 {
1242             decode_element_descriptors(stream, i, ES_info_buf, ES_info_length);
1243                 }
1244           }
1245
1246           cur_pos += 5 /* stream header */ + ES_info_length;
1247           
1248           free(ES_info_buf);
1249           
1250           if (cur_pos >= section_length - 4 /* stop before the CRC */)
1251                 done_reading_stream_types = 1;
1252         }
1253                                                          
1254         free(descriptor_buf);
1255         return 1;
1256 }
1257
1258 // ------------------------------------------------------------------------------------
1259
1260 int build_program_map(unsigned char *buf, hb_stream_t *stream)
1261 {
1262     // Get adaption header info
1263     int adapt_len = 0;
1264     int adaption = (buf[3] & 0x30) >> 4;
1265     if (adaption == 0)
1266             return 0;
1267     else if (adaption == 0x2)
1268             adapt_len = 184;
1269     else if (adaption == 0x3)
1270             adapt_len = buf[4] + 1;
1271     if (adapt_len > 184)
1272             return 0;
1273
1274     // Get payload start indicator
1275     int start;
1276     start = (buf[1] & 0x40) != 0;
1277
1278     // Get pointer length - only valid in packets with a start flag
1279     int pointer_len = 0;
1280         if (start && stream->pmt_info.reading)
1281         {
1282                 // We just finished a bunch of packets - parse the program map details
1283                 int decode_ok = 0;
1284                 if (stream->pmt_info.tablebuf[0] == 0x02)
1285                         decode_ok = decode_program_map(stream);
1286                 free(stream->pmt_info.tablebuf);
1287                 stream->pmt_info.tablebuf = NULL;
1288                 stream->pmt_info.tablepos = 0;
1289         stream->pmt_info.reading = 0;
1290         if (decode_ok)
1291                         return decode_ok;
1292         }
1293
1294         if (start)
1295         {
1296                 pointer_len = buf[4 + adapt_len] + 1;
1297                 stream->pmt_info.tablepos = 0;
1298         }       
1299         // Get Continuity Counter
1300         int continuity_counter = buf[3] & 0x0f;
1301         if (!start && (stream->pmt_info.current_continuity_counter + 1 != continuity_counter))
1302         {
1303                 hb_log("build_program_map - Continuity Counter %d out of sequence - expected %d", continuity_counter, stream->pmt_info.current_continuity_counter+1);
1304                 return 0;
1305         }
1306         stream->pmt_info.current_continuity_counter = continuity_counter;
1307         stream->pmt_info.reading |= start;
1308
1309     // Add the payload for this packet to the current buffer
1310         int amount_to_copy = 184 - adapt_len - pointer_len;
1311     if (stream->pmt_info.reading && (amount_to_copy > 0))
1312     {
1313                         stream->pmt_info.tablebuf = realloc(stream->pmt_info.tablebuf, stream->pmt_info.tablepos + amount_to_copy);
1314                                 
1315             memcpy(stream->pmt_info.tablebuf + stream->pmt_info.tablepos, buf + 4 + adapt_len + pointer_len, amount_to_copy);
1316             stream->pmt_info.tablepos += amount_to_copy;
1317     }
1318
1319     return 0;
1320 }
1321
1322 int decode_PAT(unsigned char *buf, hb_stream_t *stream)
1323 {
1324     unsigned char tablebuf[1024];
1325     unsigned int tablepos = 0;
1326     
1327     int reading = 0;
1328
1329
1330     // Get adaption header info
1331     int adapt_len = 0;
1332     int adaption = (buf[3] & 0x30) >> 4;
1333     if (adaption == 0)
1334             return 0;
1335     else if (adaption == 0x2)
1336             adapt_len = 184;
1337     else if (adaption == 0x3)
1338             adapt_len = buf[4] + 1;
1339     if (adapt_len > 184)
1340             return 0;
1341
1342     // Get pointer length
1343     int pointer_len = buf[4 + adapt_len] + 1;
1344
1345     // Get payload start indicator
1346     int start;
1347     start = (buf[1] & 0x40) != 0;
1348
1349     if (start)
1350             reading = 1;
1351
1352     // Add the payload for this packet to the current buffer
1353     if (reading && (184 - adapt_len) > 0)
1354     {
1355             if (tablepos + 184 - adapt_len - pointer_len > 1024)
1356             {
1357                     hb_log("decode_PAT - Bad program section length (> 1024)");
1358                     return 0;
1359             }
1360             memcpy(tablebuf + tablepos, buf + 4 + adapt_len + pointer_len, 184 - adapt_len - pointer_len);
1361             tablepos += 184 - adapt_len - pointer_len;
1362     }
1363
1364     if (start && reading)
1365     {
1366             memcpy(tablebuf + tablepos, buf + 4 + adapt_len + 1, pointer_len - 1);
1367
1368                         
1369             unsigned int pos = 0;
1370             //while (pos < tablepos)
1371             {
1372                     set_buf(tablebuf + pos, tablepos - pos, 0);
1373
1374                     unsigned char section_id    = get_bits(8);
1375                     get_bits(4);
1376                     unsigned int section_len    = get_bits(12);
1377                     get_bits(16); // transport_id
1378                     get_bits(2);
1379                     get_bits(5);  // version_num
1380                     get_bits(1);  // current_next
1381                     get_bits(8);  // section_num
1382                     get_bits(8);  // last_section
1383
1384                     switch (section_id)
1385                     {
1386                       case 0x00:
1387                         {
1388                           // Program Association Section
1389                           section_len -= 5;    // Already read transport stream ID, version num, section num, and last section num
1390                           section_len -= 4;   // Ignore the CRC
1391                           int curr_pos = 0;
1392                                                   stream->ts_number_pat_entries = 0;
1393                           while ((curr_pos < section_len) && (stream->ts_number_pat_entries < kMaxNumberPMTStreams))
1394                           {
1395                             unsigned int pkt_program_num = get_bits(16);
1396                                                         stream->pat_info[stream->ts_number_pat_entries].program_number = pkt_program_num;
1397                               
1398                             get_bits(3);  // Reserved
1399                             if (pkt_program_num == 0)
1400                             {
1401                               get_bits(13); // pkt_network_id
1402                             }
1403                             else
1404                             {
1405                               unsigned int pkt_program_map_PID = get_bits(13);
1406                                 stream->pat_info[stream->ts_number_pat_entries].program_map_PID = pkt_program_map_PID;
1407                             }
1408                             curr_pos += 4;
1409                                                         stream->ts_number_pat_entries++;
1410                           }
1411                         }
1412                         break;
1413                       case 0xC7:
1414                             {
1415                                     break;
1416                             }
1417                       case 0xC8:
1418                             {
1419                                     break;
1420                             }
1421                     }
1422
1423                     pos += 3 + section_len;
1424             }
1425
1426             tablepos = 0;
1427     }
1428     return 1;
1429 }
1430
1431 static int flushbuf(hb_stream_t *stream)
1432 {
1433         int old_write_index = stream->ps_current_write_buffer_index;
1434
1435         // Flip the buffers and start moving on to the next
1436         stream->ps_current_write_buffer_index++;
1437         if (stream->ps_current_write_buffer_index > kNumDecodeBuffers-1)
1438                 stream->ps_current_write_buffer_index = 0;
1439         
1440         if ( (stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len != 0) || (stream->ps_decode_buffer[stream->ps_current_write_buffer_index].write_pos != 0) )
1441         {
1442                 hb_log("flushbuf - new buffer (index %d) has non zero length and write position !", stream->ps_current_write_buffer_index);
1443                 return 0;
1444         }
1445         
1446         stream->ps_current_read_buffer_index = old_write_index;
1447         stream->ps_decode_buffer[stream->ps_current_read_buffer_index].read_pos = 0;
1448         
1449         return 1;
1450 }
1451
1452 static int fwrite64(void* buf, int elsize, int elnum, hb_stream_t* stream)
1453 {
1454         int size = elsize;
1455         if (elnum > 1)
1456                 size *= elnum;
1457         
1458         int written = 0;
1459         int current_write_index = stream->ps_current_write_buffer_index;
1460         
1461         if (size <= stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos)
1462         {
1463                 memcpy(stream->ps_decode_buffer[current_write_index].data + stream->ps_decode_buffer[current_write_index].write_pos, buf, size);
1464                 stream->ps_decode_buffer[current_write_index].write_pos += size;
1465                 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1466                 written = size;
1467         }
1468         else
1469         {
1470                 memcpy(stream->ps_decode_buffer[current_write_index].data + stream->ps_decode_buffer[current_write_index].write_pos, buf, stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos);
1471                 written += stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos;
1472                 stream->ps_decode_buffer[current_write_index].write_pos += stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos;
1473                 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1474
1475                 if (flushbuf(stream))
1476                 {
1477                         // FLushing the buffer will have change the current write buffer
1478                         current_write_index = stream->ps_current_write_buffer_index;
1479                         
1480                         memcpy(stream->ps_decode_buffer[current_write_index].data, (unsigned char*)buf + written, size - written);
1481                         stream->ps_decode_buffer[current_write_index].write_pos += size - written;
1482                         stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1483                         written += size - written;
1484                 }
1485         }
1486
1487
1488         if (elnum == 1 && written == size)
1489                 return 1;
1490         else
1491                 return written / elsize;
1492 }
1493
1494 static int write_pack(hb_stream_t* stream, int64_t time)
1495 {
1496         unsigned char buf[64];
1497         set_buf(buf, 64, 1);                                            // clear buffer
1498
1499         int64_t ext_time = time % 300;
1500         time = time / 300;
1501
1502         set_bits(0x000001ba, 32);                                       // pack id                                                              32
1503         set_bits(1, 2);                                                         // 0x01                                                                 2
1504         set_bits((unsigned int)(time >> 30), 3);        // system_clock_reference_base                  3
1505         set_bits(1, 1);                                                         // marker_bit                                                   1
1506         set_bits((unsigned int)(time >> 15), 15);       // system_clock_reference_base                  15
1507         set_bits(1, 1);                                                         // marker_bit                                                   1
1508         set_bits((unsigned int)time, 15);                       // system_clock_reference_base1                 15
1509         set_bits(1, 1);                                                         // marker_bit                                                   1
1510         set_bits((unsigned int)ext_time, 9);            // system_clock_reference_extension             9
1511         set_bits(1, 1);                                                         // marker_bit                                                   1
1512         set_bits(DEMUX, 22);                                            // program_mux_rate                                             22
1513         set_bits(1, 1);                                                         // marker_bit                                                   1
1514         set_bits(1, 1);                                                         // marker_bit                                                   1
1515         set_bits(31, 5);                                                        // reserved                                                             5
1516         set_bits(0, 3);                                                         // pack_stuffing_length                                 3
1517
1518         return fwrite64(buf, buf_size(), 1, stream) == 1;
1519 }
1520
1521 static int pad_buffer(hb_stream_t *stream, int pad)
1522 {
1523         pad -= 6;
1524
1525         char buf[6];
1526         buf[0] = '\x0'; buf[1] = '\x0'; buf[2] = '\x1'; buf[3] = '\xbe';
1527         buf[4] = pad >> 8; buf[5] = pad & 0xff;
1528
1529         if (fwrite64(buf, 6, 1, stream) != 1)
1530                 return 0;
1531
1532         unsigned char padbyte = 0xff;
1533         int i=0;
1534         for (i = 0; i < pad; i++)
1535         {
1536                 if (fwrite64(&padbyte, 1, 1, stream) != 1)
1537                         return 0;
1538         }
1539
1540         return 1;
1541 }
1542
1543 int make_pes_header(unsigned char* buf, int streamid, int len, int64_t PTS, int64_t DTS)
1544 {
1545         int hdrlen = 0;
1546         int PTS_DTS_flags = 0;
1547         if (PTS != -1)
1548         {
1549                 if (DTS != -1)
1550                 {
1551                         PTS_DTS_flags = 3;
1552                         hdrlen += 10;
1553                 }
1554                 else
1555                 {
1556                         PTS_DTS_flags = 2;
1557                         hdrlen += 5;
1558                 }
1559         }
1560
1561         set_buf(buf, 9 + hdrlen, 1);                            // clear the buffer
1562
1563         set_bits(0x000001, 24);                                         // packet_start_code_prefix                             24
1564         set_bits((unsigned int)streamid, 8);            // directory_stream_id                                  8
1565         set_bits(len, 16);                                                      // PES_packet_length                                    16
1566         set_bits(0x2, 2);                                                       // '10'                                                                 2
1567         set_bits(0, 2);                                                         // PES_scrambling_control                               2
1568         set_bits(1, 1);                                                         // PES_priority                                                 1
1569         set_bits(0, 1);                                                         // data_alignment_indicator                             1
1570         set_bits(0, 1);                                                         // copyright                                                    1
1571         set_bits(0, 1);                                                         // original_or_copy                                             1
1572         set_bits(PTS_DTS_flags, 2);                                     // PTS_DTS_flags                                                2
1573         set_bits(0, 1);                                                         // ESCR_flag                                                    1
1574         set_bits(0, 1);                                                         // ES_rate_flag                                                 1
1575         set_bits(0, 1);                                                         // DSM_trick_mode_flag                                  1
1576         set_bits(0, 1);                                                         // additional_copy_info_flag                    1
1577         set_bits(0, 1);                                                         // PES_CRC_flag                                                 1
1578         set_bits(0, 1);                                                         // PES_extension_flag                                   1
1579         set_bits(hdrlen, 8);                                            // PES_header_data_length                               8
1580         
1581         if (PTS_DTS_flags == 2)
1582         {
1583                 set_bits(2, 4);                                                         // '0010'                                                       4
1584                 set_bits((unsigned int)(PTS >> 30), 3);         // PTS [32..30]                                         3
1585                 set_bits(1, 1);                                                         // marker bit                                           1
1586                 set_bits((unsigned int)(PTS >> 15), 15);        // PTS [29..15]                                         15
1587                 set_bits(1, 1);                                                         // marker bit                                           1
1588                 set_bits((unsigned int)PTS, 15);                        // PTS [14..0]                                          15
1589                 set_bits(1, 1);                                                         // marker bit                                           1
1590         }
1591         else if (PTS_DTS_flags == 3)
1592         {
1593                 set_bits(3, 4);                                                         // '0011'                                                       4
1594                 set_bits((unsigned int)(PTS >> 30), 3);         // PTS [32..30]                                         3
1595                 set_bits(1, 1);                                                         // marker bit                                           1
1596                 set_bits((unsigned int)(PTS >> 15), 15);        // PTS [29..15]                                         15
1597                 set_bits(1, 1);                                                         // marker bit                                           1
1598                 set_bits((unsigned int)PTS, 15);                        // PTS [14..0]                                          15
1599                 set_bits(1, 1);                                                         // marker bit                                           1
1600                 set_bits(1, 4);                                                         // '0001'                                                       4
1601                 set_bits((unsigned int)(DTS >> 30), 3);         // DTS [32..30]                                         3
1602                 set_bits(1, 1);                                                         // marker bit                                           1
1603                 set_bits((unsigned int)(DTS >> 15), 15);        // DTS [29..15]                                         15
1604                 set_bits(1, 1);                                                         // marker bit                                           1
1605                 set_bits((unsigned int)DTS, 15);                        // DTS [14..0]                                          15
1606                 set_bits(1, 1);                                                         // marker bit                                           1
1607         }
1608
1609         return buf_size();
1610 }
1611
1612 int generate_output_data(hb_stream_t *stream, int write_ac3, int curstream, int pid)
1613 {
1614                         unsigned char ac3_substream_id[4];
1615                         int ac3len = 0;
1616                         
1617                         if (write_ac3)
1618                         {
1619                 // Make a four byte DVD ac3 stream header
1620                 int ssid = (curstream - stream->ts_number_video_pids) & 0xf;
1621                 ac3_substream_id[0] = 0x80 | ssid;  // substream id
1622                 ac3_substream_id[1] = 0x01;         // number of sync words
1623                 ac3_substream_id[2] = 0x00;         // first offset (16 bits)
1624                 ac3_substream_id[3] = 0x02;
1625                 ac3len = 4;
1626                         }
1627                         
1628                         int written = 0;        // Bytes we've written to output file
1629                         int pos = 0;            // Position in PES packet buffer
1630                         
1631                         for (;;)
1632                         {
1633                                 if ((stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len % HB_DVD_READ_BUFFER_SIZE) != 0)
1634                                 {
1635                                         hb_log("write_output_stream - Packet's not falling on read buffer size boundries!");
1636                                         return 1;
1637                                 }
1638
1639                                 // Get total length of this pack
1640                                 int len = min(14 + ac3len + stream->ts_packetpos[curstream] - pos, HB_DVD_READ_BUFFER_SIZE);
1641
1642                                 // Figure out stuffing (if we have less than 16 bytes left)
1643                                 int stuffing = 0;
1644                                 if (len < HB_DVD_READ_BUFFER_SIZE && HB_DVD_READ_BUFFER_SIZE - len < 16)
1645                                 {
1646                                         stuffing = HB_DVD_READ_BUFFER_SIZE - len;
1647                                         len += stuffing;
1648                                 }
1649
1650                                 // Write out pack header
1651                                 off_t file_offset = ftello(stream->file_handle);
1652                                 int64_t packet_time = (file_offset * CLOCKRATE / STREAMRATE) + 0 /*file_time*/;
1653                                 if (!write_pack(stream, packet_time))
1654                                 {
1655                                         hb_log("write_output_stream - Couldn't write pack header!");
1656                                         return 1;
1657                                 }
1658
1659                 stream->ts_packetbuf[curstream][pos + 3] =
1660                     stream->ts_streamid[curstream];
1661
1662                                 // Packet length..
1663                                 // Subtract pack size (14) and pes id and len (6) from lenth
1664                                 stream->ts_packetbuf[curstream][pos + 4] = (len - 6 - 14) >> 8; stream->ts_packetbuf[curstream][pos + 5] = (len - 6 - 14) & 0xFF;
1665
1666                                 // Add any stuffing bytes to header extra len
1667                                 int hdrsize = 9 + stream->ts_packetbuf[curstream][pos + 8];
1668                                 stream->ts_packetbuf[curstream][pos + 8] += stuffing;                                   // Add stuffing to header bytes
1669
1670                                 // Write out id, streamid, len
1671                                 if (fwrite64(stream->ts_packetbuf[curstream] + pos, hdrsize, 1, stream) != 1)   // Write pes id, streamid, and len
1672                                 {
1673                                         hb_log("write_output_stream - Failed to write output file!");
1674                                         return 1;
1675                                 }
1676                                 
1677                                 // Write stuffing
1678                                 int i=0;
1679                                 for (i = 0; i < stuffing; i++)                          // Write any stuffing bytes
1680                                 {
1681                                         unsigned char stuff = 0xff;
1682                                         if (fwrite64(&stuff, 1, 1, stream) != 1)
1683                                         {
1684                                                 hb_log("write_output_stream - Failed to write output file!");
1685                                                 return 1;
1686                                         }
1687                                 }
1688
1689                                 // Write ac3 streamid
1690                                 if (ac3len != 0)
1691                                 {
1692                                         if (fwrite64(ac3_substream_id, ac3len, 1, stream) != 1)
1693                                         {
1694                                                 hb_log("write_output_stream - Failed to write output file!");
1695                                                 return 1;
1696                                         }
1697                                 }
1698
1699                                 // Write rest of data len minus headersize (9) stuffing, and pack size (14)
1700                                 if (fwrite64(stream->ts_packetbuf[curstream] + pos + hdrsize, len - hdrsize - 14 - stuffing - ac3len, 1, stream) != 1)  // Write data bytes
1701                                 {
1702                                         hb_log("write_output_stream - Failed to write output file!");
1703                                         return 1;
1704                                 }
1705                                 written += len;
1706
1707                                 // Add len minus stuff we added like the pack (14) and the stuffing.
1708                                 pos += len - 14 - stuffing - ac3len;
1709                                 if (pos == stream->ts_packetpos[curstream])
1710                                         break;
1711
1712                                 // Add pes header for next packet
1713                                 pos -= 9;
1714                                 make_pes_header(stream->ts_packetbuf[curstream] + pos, stream->ts_streamid[curstream], 0, -1, -1);
1715                         }
1716
1717                         stream->ts_packetpos[curstream] = 0;
1718                         stream->ts_streamcont[curstream] = -1;
1719
1720                         // Write padding
1721                         if ((written % HB_DVD_READ_BUFFER_SIZE) != 0)
1722                         {
1723                                 int left = HB_DVD_READ_BUFFER_SIZE - (written % HB_DVD_READ_BUFFER_SIZE);
1724
1725                                 // Pad out to HB_DVD_READ_BUFFER_SIZE bytes
1726                                 if (!pad_buffer(stream, left))
1727                                 {
1728                                         hb_log("write_output_stream - Couldn't write pad buffer!");
1729                                         return 1;
1730                                 }
1731                         }
1732
1733         return 0;
1734 }
1735
1736 static void hb_ts_handle_mpeg_audio(hb_stream_t *stream, int curstream, unsigned char* buf, int adapt_len )
1737 {
1738         // Although we don't have AC3/A52 audio here we can still use the same structure to record this useful information.
1739         
1740         stream->a52_info[curstream - stream->ts_number_video_pids].flags = A52_STEREO;
1741         stream->a52_info[curstream - stream->ts_number_video_pids].rate = 48000 /*Hz*/;
1742         stream->a52_info[curstream - stream->ts_number_video_pids].bitrate = 384000 /*Bps*/;
1743 }
1744
1745 static int hb_ts_handle_ac3_audio(hb_stream_t *stream, int curstream, unsigned char* buf, int adapt_len )
1746 {
1747         int spos, dpos;
1748
1749         // Make sure we start with 0x0b77
1750         if (stream->ts_packetbuf[curstream][9 + stream->ts_packetbuf[curstream][8]] != 0x0b || stream->ts_packetbuf[curstream][9 + stream->ts_packetbuf[curstream][8] + 1] != 0x77)
1751         {
1752                 spos = 9 + stream->ts_packetbuf[curstream][8];
1753                 dpos = 9 + stream->ts_packetbuf[curstream][8];
1754                 while (spos <= stream->ts_packetpos[curstream] - 2 && !(stream->ts_packetbuf[curstream][spos] == 0x0b && stream->ts_packetbuf[curstream][spos + 1] == 0x77))
1755                         spos++;
1756
1757                 if (!(stream->ts_packetbuf[curstream][spos] == 0x0b && stream->ts_packetbuf[curstream][spos + 1] == 0x77))
1758                 {
1759                         hb_log("hb_ts_stream_decode - Couldn't sync AC3 packet!");
1760                         stream->ts_skipbad[curstream] = 1;
1761                         return 0;
1762                 }
1763
1764                 while (spos < stream->ts_packetpos[curstream])
1765                 {
1766                         stream->ts_packetbuf[curstream][dpos] = stream->ts_packetbuf[curstream][spos];
1767                         spos++;
1768                         dpos++;
1769                 }
1770                 stream->ts_packetpos[curstream] = dpos;
1771         }
1772
1773         // Check the next packet to make sure IT starts with a 0x0b77
1774         int plen = 0;
1775     plen = 9 + buf[4 + adapt_len + 8];
1776         int pstart = 4 + adapt_len + plen;
1777         if (buf[pstart] != 0x0b || buf[pstart + 1] != 0x77)
1778         {
1779                 spos = pstart;
1780                 while (spos < 188 - 2 && !(buf[spos] == 0x0b && buf[spos + 1] == 0x77))
1781                 {
1782                         stream->ts_packetbuf[curstream][stream->ts_packetpos[curstream]] = buf[spos];
1783                         stream->ts_packetpos[curstream]++;
1784                         spos++;
1785                 }
1786
1787                 if (!(buf[spos] == 0x0b && buf[spos + 1] == 0x77))
1788                 {
1789                         hb_log("hb_ts_stream_decode - Couldn't sync AC3 packet!");
1790                         stream->ts_skipbad[curstream] = 1;
1791                         return 0;
1792                 }
1793
1794                 adapt_len = spos - 4 - plen;
1795
1796                 dpos = spos - 1;
1797                 spos = pstart - 1;
1798                 while (spos >= pstart - plen)
1799                 {
1800                         buf[dpos] = buf[spos];
1801                         spos--;
1802                         dpos--;
1803                 }
1804         }
1805
1806         int flags, rate, bitrate;
1807         if( a52_syncinfo( &buf[pstart], &flags, &rate, &bitrate ) )
1808         {
1809                 stream->a52_info[curstream - stream->ts_number_video_pids].flags = flags;
1810                 stream->a52_info[curstream - stream->ts_number_video_pids].rate = rate;
1811                 stream->a52_info[curstream - stream->ts_number_video_pids].bitrate = bitrate;
1812         }
1813         return 1;
1814 }
1815
1816 static void hb_ts_stream_find_pids(hb_stream_t *stream)
1817 {
1818         unsigned char buf[188];
1819
1820         // align to first packet
1821         align_to_next_packet(stream->file_handle);
1822
1823         // Read the Transport Stream Packets (188 bytes each) looking at first for PID 0 (the PAT PID), then decode that
1824         // to find the program map PID and then decode that to get the list of audio and video PIDs
1825         
1826         int bytesReadInPacket = 0;
1827         for (;;)
1828         {
1829                 // Try to read packet..
1830                 int bytesRead;
1831                 if ((bytesRead = fread(buf+bytesReadInPacket, 1, 188-bytesReadInPacket, stream->file_handle)) != 188-bytesReadInPacket)
1832                 {
1833                         if (bytesRead < 0)
1834                                 bytesRead = 0;
1835                         bytesReadInPacket += bytesRead;
1836
1837                         hb_log("hb_ts_stream_find_pids - end of file");
1838                         break;  
1839                 }
1840                 else
1841                 {
1842                         bytesReadInPacket = 0;
1843                 }
1844
1845                 // Check sync byte
1846                 if ((buf[0] != 0x47) && (buf[0] != 0x72) && (buf[0] != 0x29))
1847                 {
1848                         hb_log("hb_ts_stream_find_pids - Bad transport packet (no sync byte 0x47)!");
1849                         int i = 0;
1850                         for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1851                                 stream->ts_skipbad[i] = 1;
1852                         continue;
1853                 }
1854
1855                 // Get pid
1856                 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
1857                 
1858         if ((pid == 0x0000) && (stream->ts_number_pat_entries == 0))
1859                 {
1860                   decode_PAT(buf, stream);
1861                   continue;
1862                 }
1863                 
1864                 int pat_index = 0;
1865                 for (pat_index = 0; pat_index < stream->ts_number_pat_entries; pat_index++)
1866                 {
1867                         // There are some streams where the PAT table has multiple entries as if their are
1868                         // multiple programs in the same transport stream, and yet there's actually only one
1869                         // program really in the stream. This seems to be true for transport streams that
1870                         // originate in the HDHomeRun but have been output by EyeTV's export utility. What I think
1871                         // is happening is that the HDHomeRun is sending the entire transport stream as broadcast, 
1872                         // but the EyeTV is only recording a single (selected) program number and not rewriting the 
1873                         // PAT info on export to match what's actually on the stream.
1874                         // Until we have a way of handling multiple programs per transport stream elegantly we'll match
1875                         // on the first pat entry for which we find a matching program map PID.  The ideal solution would
1876                         // be to build a title choice popup from the PAT program number details and then select from
1877                         // their - but right now the API's not capable of that.
1878                         if (pid == stream->pat_info[pat_index].program_map_PID)
1879                         {
1880                           if (build_program_map(buf, stream) > 0)
1881                                 break;
1882                         }
1883                 }
1884                 // Keep going  until we have a complete set of PIDs
1885                 if ((stream->ts_number_video_pids > 0) && (stream->ts_number_audio_pids > 0))
1886                   break;
1887         }
1888         
1889         hb_log("hb_ts_stream_find_pids - found the following PIDS");
1890         hb_log("    Video PIDS : ");
1891         int i=0;
1892         for (i=0; i < stream->ts_number_video_pids; i++)
1893         {
1894                 hb_log("      0x%x (%d)", stream->ts_video_pids[i], stream->ts_video_pids[i]);
1895         }
1896         hb_log("    Audio PIDS : ");
1897         for (i = 0; i < stream->ts_number_audio_pids; i++)
1898         {
1899                 hb_log("      0x%x (%d)", stream->ts_audio_pids[i], stream->ts_audio_pids[i]);
1900         }
1901  }
1902
1903 int index_of_video_pid(int pid, hb_stream_t *stream)
1904 {
1905         int found_pid = -1, i = 0;
1906         
1907         for (i = 0; (i < stream->ts_number_video_pids) && (found_pid < 0); i++)
1908         {
1909                 if (pid == stream->ts_video_pids[i])
1910                         found_pid = i;
1911         }
1912         return found_pid;
1913 }
1914
1915 int index_of_audio_pid(int pid, hb_stream_t *stream)
1916 {
1917         int i = 0, found_pid = -1;
1918
1919         for (i = 0; (i < stream->ts_number_audio_pids) && (found_pid < 0); i++)
1920         {
1921                 if (pid == stream->ts_audio_pids[i])
1922                         found_pid = i;
1923         }
1924         return found_pid;
1925 }
1926
1927 int index_of_pid(int pid, hb_stream_t *stream)
1928 {
1929         int found_pid = -1;
1930         
1931         if ((found_pid = index_of_video_pid(pid, stream)) >= 0)
1932                 return found_pid;
1933         
1934         if ((found_pid = index_of_audio_pid(pid, stream)) >= 0)
1935                 return found_pid;
1936                 
1937         return found_pid;
1938 }
1939
1940 /***********************************************************************
1941  * hb_ts_stream_decode
1942  ***********************************************************************
1943  *
1944  **********************************************************************/
1945 static void hb_ts_stream_decode(hb_stream_t *stream)
1946 {
1947         unsigned char buf[188];
1948         int curstream;
1949         int doing_iframe;
1950         
1951         int i = 0;
1952         for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1953         {
1954                 stream->ts_skipbad[i] = 0;
1955         }
1956         
1957         doing_iframe = 0;
1958         
1959         if ((stream->ts_number_video_pids == 0) || (stream->ts_number_audio_pids == 0))
1960         {
1961                 hb_log("hb_ts_stream_decode  - no Video or Audio PID selected, cannot decode transport stream");
1962                 return;
1963         }
1964         
1965         int curr_write_buffer_index = stream->ps_current_write_buffer_index;
1966         
1967         // Write output data until a buffer switch occurs.
1968         while (curr_write_buffer_index == stream->ps_current_write_buffer_index)
1969         {
1970       if ((fread(buf, 188, 1, stream->file_handle)) != 1)
1971                 {
1972             // end of file - we didn't finish filling our ps write buffer
1973             // so just discard the remainder (the partial buffer is useless)
1974             hb_log("hb_ts_stream_decode - eof");
1975             stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len = 0;
1976             return;
1977                 }
1978
1979                 // Check sync byte
1980                 if ((buf[0] != 0x47) && (buf[0] != 0x72) && (buf[0] != 0x29))
1981                 {
1982             // lost sync - back up to where we started then try to 
1983             // re-establish sync.
1984             off_t pos = ftello(stream->file_handle) - 188;
1985             off_t pos2 = align_to_next_packet(stream->file_handle);
1986             if ( pos2 == 0 )
1987             {
1988                 hb_log( "hb_ts_stream_decode: eof while re-establishing sync @ %lld",
1989                         pos );
1990                 stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len = 0;
1991                 return;
1992             }
1993             hb_log("hb_ts_stream_decode: sync lost @%lld, regained after %lld bytes",
1994                     pos, pos2 );
1995                         for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1996                         {
1997                                 stream->ts_skipbad[i] = 1;
1998                         }
1999                         continue;
2000                 }
2001
2002                 // Get pid
2003                 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
2004
2005                 // Get the pos and buf - we organize our streams as 'n' video streams then 'm' audio streams
2006       int index_of_selected_pid;
2007                 if ((index_of_selected_pid = index_of_video_pid(pid,stream)) < 0)
2008                 {
2009                         // Not a video PID perhaps audio ?
2010                         if ((index_of_selected_pid = index_of_audio_pid(pid,stream)) < 0)
2011                         {
2012             // not a pid we want
2013                                 continue;
2014                         }
2015                         else
2016                         {
2017                                 curstream = stream->ts_number_video_pids + index_of_selected_pid;
2018                         }
2019                 }
2020                 else
2021                         curstream = index_of_selected_pid;
2022                 
2023                 // Get start code
2024                 int start;
2025                 start = (buf[1] & 0x40) != 0;
2026                   
2027                 if (!start && stream->ts_skipbad[curstream])
2028                         continue;
2029
2030                 // Get error
2031                 int errorbit = (buf[1] & 0x80) != 0;
2032                 if (errorbit)
2033                 {
2034                         hb_log("hb_ts_stream_decode - Error bit set in packet");
2035                         stream->ts_skipbad[curstream] = 1;
2036                         continue;
2037                 }
2038
2039                 // Get adaption header info
2040                 int adaption = (buf[3] & 0x30) >> 4;
2041                 int adapt_len = 0;
2042
2043                 // Get continuity
2044         // Continuity only increments for adaption values of 0x3 or 0x01
2045                 int continuity = (buf[3] & 0xF);
2046         if ((stream->ts_streamcont[curstream] != -1) && ((adaption & 0x01) != 0))
2047                 {
2048                         if (continuity != ((stream->ts_streamcont[curstream] + 1) & 0xF))
2049                         {
2050                                 hb_log("hb_ts_stream_decode - Bad continuity code in packet");
2051                                 stream->ts_skipbad[curstream] = 1;
2052                                 continue;
2053                         }
2054                         stream->ts_streamcont[curstream] = continuity;
2055                 }
2056                         
2057                 // Get adaption header size
2058                 if (adaption == 0)
2059                 {
2060                         hb_log("hb_ts_stream_decode - Bad adaption code (code was 0)!");
2061                         for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
2062                         {
2063                                 stream->ts_skipbad[i] = 1;
2064                         }
2065                         continue;
2066                 }
2067                 else if (adaption == 0x2)
2068                         adapt_len = 184;
2069                 else if (adaption == 0x3)
2070                 {
2071                         adapt_len = buf[4] + 1;
2072                         if (adapt_len > 184)
2073                         {
2074                                 hb_log("hb_ts_stream_decode - Invalid adapt len %d", adapt_len);
2075                                 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
2076                                 {
2077                                         stream->ts_skipbad[i] = 1;
2078                                 }
2079                         }
2080                 }
2081
2082                 // HBO is slick, it doesn't bother to sync AC3 packets with PES elementary stream packets.. so
2083                 // we have to swizzle them together!  (ARGHH!)
2084                 if (start && curstream >= stream->ts_number_video_pids &&
2085             stream->ts_audio_stream_type[curstream - stream->ts_number_video_pids]
2086               != 0x03)
2087                 {
2088                         // Is there an AC3 packet start 0b77 code in this packet??
2089                         int sync_found = 0;
2090                         unsigned char *p = buf + 4 + adapt_len;
2091                         while (p <= buf + 186)
2092                         {
2093                                 if (p[0] == 0x0b && p[1] == 0x77)
2094                                 {
2095                                         sync_found = 1;
2096                                         break;
2097                                 }
2098                                 p++;
2099                         }
2100
2101                         // Couldn't find an AC3 sync start in this packet.. don't make a PES packet!
2102                         if (!sync_found)
2103                         {
2104                                 adapt_len = 184;        
2105                                 start = 0;
2106                         }
2107                 }
2108
2109                 // Found a random access point (now we can start a frame/audio packet..)
2110                 if (start)
2111                 {
2112                         // Check to see if this is an i_frame (group of picture start)
2113                         if (pid == stream->ts_video_pids[0])
2114                         {
2115                                 // Look for the Group of Pictures packet.. indicates this is an I-Frame packet..
2116                                 doing_iframe = 0;
2117                                 unsigned int strid = 0;
2118                                 int i = 4;
2119                                 for (i = 4 + adapt_len; i < 188; i++)
2120                                 {
2121                                         strid = (strid << 8) | buf[i];
2122                                         if (strid == 0x000001B8) // group_start_code
2123                                         {
2124                                                 // found a Group of Pictures header, subsequent picture must be an I-frame
2125                                                 doing_iframe = 1;
2126                                         }
2127                                         else if (strid == 0x000001B3) // sequence_header code
2128                                         {
2129                                                 doing_iframe = 1;
2130                                         }
2131                                         else if (strid == 0x00000100) // picture_start_code
2132                                         {
2133                                                 // picture_header, let's see if it's an I-frame
2134                                                 if (i<187)
2135                                                 {
2136                                                         // check if picture_coding_type == 1
2137                                                         if ((buf[i+2] & (0x7 << 3)) == (1 << 3))
2138                                                         {
2139                                                                 // found an I-frame picture
2140                                                                 doing_iframe = 1;
2141                                                         }
2142                                                 }
2143                                         }
2144
2145                                         if (doing_iframe)
2146                                         {
2147                                                 if (!stream->ts_foundfirst[curstream])
2148                                                 {
2149                                                         stream->ts_foundfirst[curstream] = 1;
2150 //                                                      first_video_PCR = PCR;
2151                                                 }
2152                                                 break;
2153                                         }
2154                                 }
2155                         }
2156                         else if (index_of_audio_pid(pid, stream) >= 0)
2157                         {
2158                             if (stream->ts_foundfirst[0])  // Set audio found first ONLY after first video frame found. There's an assumption here that stream '0' is a video stream
2159                                 {
2160                                         stream->ts_foundfirst[curstream] |= 1;
2161                                 }
2162                         }
2163                         
2164                         // If we were skipping a bad packet, start fresh on this new PES packet..
2165                         if (stream->ts_skipbad[curstream] == 1)
2166                         {
2167                                 stream->ts_skipbad[curstream] = 0;
2168                                 stream->ts_packetpos[curstream] = 0;
2169                         }
2170
2171                         // Get the continuity code of this packet
2172                         stream->ts_streamcont[curstream] = continuity;
2173                 }
2174
2175                 // Write a 2048 byte program stream packet..
2176                 if (start && stream->ts_packetpos[curstream] > 0 && stream->ts_foundfirst[curstream] && !stream->ts_skipbad[curstream])
2177                 {
2178                         // Save the substream id block so we can added it to subsequent blocks
2179                         int write_ac3 = 0;
2180             if (curstream >= stream->ts_number_video_pids)
2181                         {
2182                                 // Curstream is a zero based index of streams and includes both video and audio streams, so we must subtract the numver of video streams
2183                                 // from the indes value used here since ts_audio_stream_type is indexed only by audio streams.
2184                 if (stream->ts_audio_stream_type[curstream - stream->ts_number_video_pids] == 0x03)
2185                                 {
2186                                         hb_ts_handle_mpeg_audio(stream, curstream, buf, adapt_len);
2187                                 }
2188                                 else
2189                                 {
2190                     write_ac3 = hb_ts_handle_ac3_audio(stream, curstream, buf, adapt_len);
2191                                 }
2192                         }
2193                 
2194                 if (generate_output_data(stream, write_ac3, curstream, pid) != 0)
2195                         return ;
2196                 }
2197
2198                 // Add the payload for this packet to the current buffer
2199                 if (!stream->ts_skipbad[curstream] && stream->ts_foundfirst[curstream] &&
2200             (184 - adapt_len) > 0)
2201                 {
2202             // XXX this shouldn't happen but we'll be paranoid
2203             if (stream->ts_packetpos[curstream] + 184 - adapt_len > 1024*1024)
2204             {
2205                 hb_log("hb_ts_stream_decode: ts_packetbuf overflow, pos = %d ,"
2206                        "len = %d", stream->ts_packetpos[curstream],
2207                        184 - adapt_len );
2208                 return;
2209             }
2210                         memcpy(stream->ts_packetbuf[curstream] + stream->ts_packetpos[curstream], buf + 4 + adapt_len, 184 - adapt_len);
2211                         stream->ts_packetpos[curstream] += 184 - adapt_len;
2212                 }
2213         }
2214 }
2215
2216 /***********************************************************************
2217  * hb_ts_stream_reset
2218  ***********************************************************************
2219  *
2220  **********************************************************************/
2221 static void hb_ts_stream_reset(hb_stream_t *stream)
2222 {
2223         int i=0;
2224         for (i=0; i < kNumDecodeBuffers; i++)
2225         {
2226                 stream->ps_decode_buffer[i].read_pos = 0;
2227                 stream->ps_decode_buffer[i].write_pos = 0;
2228                 stream->ps_decode_buffer[i].len = 0;
2229         }
2230
2231         for (i=0; i < kMaxNumberDecodeStreams; i++)
2232         {
2233                 stream->ts_streamcont[i] = -1;
2234         }
2235
2236         stream->ps_current_write_buffer_index = 0;
2237         stream->ps_current_read_buffer_index = 1;
2238
2239         align_to_next_packet(stream->file_handle);
2240 }
2241