OSDN Git Service

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