OSDN Git Service

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