OSDN Git Service

Don't reject MPEG PS files just because they're missing a SYS header (clips won't...
[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.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include <string.h>
8 #include <ctype.h>
9 #include <errno.h>
10
11 #include "hb.h"
12 #include "lang.h"
13 #include "a52dec/a52.h"
14 #include "libavcodec/avcodec.h"
15 #include "libavformat/avformat.h"
16 #include "mp4v2/mp4v2.h"
17
18 #define min(a, b) a < b ? a : b
19
20 /*
21  * This table defines how ISO MPEG stream type codes map to HandBrake
22  * codecs. It is indexed by the 8 bit stream type and contains the codec
23  * worker object id and a parameter for that worker proc (ignored except
24  * for the ffmpeg-based codecs in which case it is the ffmpeg codec id).
25  *
26  * Entries with a worker proc id of 0 or a kind of 'U' indicate that HB
27  * doesn't handle the stream type.
28  */
29 typedef struct {
30     enum { N, U, A, V } kind; /* not handled / unknown / audio / video */
31     int codec;          /* HB worker object id of codec */
32     int codec_param;    /* param for codec (usually ffmpeg codec id) */
33     const char* name;   /* description of type */
34 } stream2codec_t;
35
36 #define st(id, kind, codec, codec_param, name) \
37  [id] = { kind, codec, codec_param, name }
38
39 static const stream2codec_t st2codec[256] = {
40     st(0x01, V, WORK_DECMPEG2,     0,              "MPEG1"),
41     st(0x02, V, WORK_DECMPEG2,     0,              "MPEG2"),
42     st(0x03, A, HB_ACODEC_MPGA,    CODEC_ID_MP2,   "MPEG1"),
43     st(0x04, A, HB_ACODEC_MPGA,    CODEC_ID_MP2,   "MPEG2"),
44     st(0x05, N, 0,                 0,              "ISO 13818-1 private section"),
45     st(0x06, U, 0,                 0,              "ISO 13818-1 PES private data"),
46     st(0x07, N, 0,                 0,              "ISO 13522 MHEG"),
47     st(0x08, N, 0,                 0,              "ISO 13818-1 DSM-CC"),
48     st(0x09, N, 0,                 0,              "ISO 13818-1 auxiliary"),
49     st(0x0a, N, 0,                 0,              "ISO 13818-6 encap"),
50     st(0x0b, N, 0,                 0,              "ISO 13818-6 DSM-CC U-N msgs"),
51     st(0x0c, N, 0,                 0,              "ISO 13818-6 Stream descriptors"),
52     st(0x0d, N, 0,                 0,              "ISO 13818-6 Sections"),
53     st(0x0e, N, 0,                 0,              "ISO 13818-1 auxiliary"),
54     st(0x0f, A, HB_ACODEC_MPGA,    CODEC_ID_AAC,   "ISO 13818-7 AAC Audio"),
55     st(0x10, V, WORK_DECAVCODECV,  CODEC_ID_MPEG4, "MPEG4"),
56     st(0x11, A, HB_ACODEC_MPGA,    CODEC_ID_AAC_LATM, "MPEG4 LATM AAC"),
57     st(0x12, U, 0,                 0,              "MPEG4 generic"),
58
59     st(0x14, N, 0,                 0,              "ISO 13818-6 DSM-CC download"),
60
61     st(0x1b, V, WORK_DECAVCODECV,  CODEC_ID_H264,  "H.264"),
62
63     st(0x80, N, 0,                 0,              "DigiCipher II Video"),
64     st(0x81, A, HB_ACODEC_AC3,     0,              "AC-3"),
65     st(0x82, A, HB_ACODEC_DCA,     0,              "HDMV DTS"),
66     st(0x83, A, HB_ACODEC_LPCM,    0,              "LPCM"),
67     st(0x84, A, 0,                 0,              "SDDS"),
68     st(0x85, U, 0,                 0,              "ATSC Program ID"),
69     st(0x86, A, HB_ACODEC_DCA,     0,              "DTS-HD"),
70     st(0x87, A, 0,                 0,              "E-AC-3"),
71
72     st(0x8a, A, HB_ACODEC_DCA,     0,              "DTS"),
73
74     st(0x91, A, HB_ACODEC_AC3,     0,              "AC-3"),
75     st(0x92, N, 0,                 0,              "Subtitle"),
76
77     st(0x94, A, 0,                 0,              "SDDS"),
78     st(0xa0, V, 0,                 0,              "MSCODEC"),
79
80     st(0xea, V, WORK_DECAVCODECV,  CODEC_ID_VC1,   "VC1"),
81 };
82 #undef st
83
84 typedef enum {
85     hb_stream_type_unknown = 0,
86     transport,
87     program,
88     dvd_program,
89     ffmpeg
90 } hb_stream_type_t;
91
92 #define kMaxNumberVideoPIDS 1
93 #define kMaxNumberAudioPIDS 15
94 #define kMaxNumberDecodeStreams (kMaxNumberVideoPIDS+kMaxNumberAudioPIDS)
95 #define kMaxNumberPMTStreams 32
96
97
98 struct hb_stream_s
99 {
100     int     frames;             /* video frames so far */
101     int     errors;             /* total errors so far */
102     int     last_error_frame;   /* frame # at last error message */
103     int     last_error_count;   /* # errors at last error message */
104     int     packetsize;         /* Transport Stream packet size */
105
106     int8_t  need_keyframe;      // non-zero if want to start at a keyframe
107     int8_t  ts_no_RAP;          // non-zero if there are no random access points
108
109     int8_t  ts_found_pcr;       // non-zero if we've found at least one input pcr
110     int     ts_pcr_out;         // sequence number of most recent output pcr
111     int     ts_pcr_in;          // sequence number of most recent input pcr
112     int64_t ts_pcr;             // most recent input pcr
113     int64_t ts_pcrhist[4];      // circular buffer of output pcrs
114
115     uint8_t *ts_packet;         /* buffer for one TS packet */
116     hb_buffer_t *ts_buf[kMaxNumberDecodeStreams];
117     int     ts_pos[kMaxNumberDecodeStreams];
118     int8_t  ts_skipbad[kMaxNumberDecodeStreams];
119     int8_t  ts_streamcont[kMaxNumberDecodeStreams];
120
121     hb_buffer_t *fwrite_buf;      /* PS buffer (set by hb_ts_stream_decode) */
122
123     int      chapter;           /* Chapter that we are currently in */
124     uint64_t chapter_end;       /* HB time that the current chapter ends */
125
126     /*
127      * Stuff before this point is dynamic state updated as we read the
128      * stream. Stuff after this point is stream description state that
129      * we learn during the initial scan but cache so it can be
130      * reused during the conversion read.
131      */
132     uint8_t ts_number_video_pids;
133     uint8_t ts_number_audio_pids;
134
135     int16_t ts_video_pids[kMaxNumberVideoPIDS];
136     int16_t ts_audio_pids[kMaxNumberAudioPIDS];
137
138     uint32_t ts_format_id[kMaxNumberDecodeStreams];
139 #define TS_FORMAT_ID_AC3 (('A' << 24) | ('C' << 16) | ('-' << 8) | '3')
140     uint8_t ts_stream_type[kMaxNumberDecodeStreams];
141     uint8_t ts_multiplexed[kMaxNumberDecodeStreams];
142
143     char    *path;
144     FILE    *file_handle;
145     hb_stream_type_t hb_stream_type;
146     hb_title_t *title;
147
148     AVFormatContext *ffmpeg_ic;
149     AVPacket *ffmpeg_pkt;
150     double ffmpeg_tsconv[MAX_STREAMS];
151     uint8_t ffmpeg_video_id;
152
153     struct {
154         int lang_code;
155         int flags;
156         int rate;
157         int bitrate;
158     } a52_info[kMaxNumberAudioPIDS];
159
160     struct
161     {
162         unsigned short program_number;
163         unsigned short program_map_PID;
164     } pat_info[kMaxNumberPMTStreams];
165     int     ts_number_pat_entries;
166
167     struct
168     {
169         int reading;
170         unsigned char *tablebuf;
171         unsigned int tablepos;
172         unsigned char current_continuity_counter;
173
174         int section_length;
175         int program_number;
176         unsigned int PCR_PID;
177         int program_info_length;
178         unsigned char *progam_info_descriptor_data;
179         struct
180         {
181             unsigned char stream_type;
182             unsigned short elementary_PID;
183             unsigned short ES_info_length;
184             unsigned char *es_info_descriptor_data;
185         } pmt_stream_info[kMaxNumberPMTStreams];
186     } pmt_info;
187 };
188
189 /***********************************************************************
190  * Local prototypes
191  **********************************************************************/
192 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle);
193 static void hb_ts_stream_init(hb_stream_t *stream);
194 static void hb_ts_stream_find_pids(hb_stream_t *stream);
195 static int hb_ts_stream_decode(hb_stream_t *stream, hb_buffer_t *obuf);
196 static void hb_ts_stream_reset(hb_stream_t *stream);
197 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
198                                                        int aud_pid_index);
199 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title);
200 static off_t align_to_next_packet(hb_stream_t *stream);
201
202 static int ffmpeg_open( hb_stream_t *stream, hb_title_t *title );
203 static void ffmpeg_close( hb_stream_t *d );
204 static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream );
205 static int ffmpeg_read( hb_stream_t *stream, hb_buffer_t *buf );
206 static int ffmpeg_seek( hb_stream_t *stream, float frac );
207
208 /*
209  * streams have a bunch of state that's learned during the scan. We don't
210  * want to throw away the state when scan does a close then relearn
211  * everything when reader does an open. So we save the stream state on
212  * the close following a scan and reuse it when 'reader' does an open.
213  */
214 static hb_list_t *stream_state_list;
215
216 static hb_stream_t *hb_stream_lookup( const char *path )
217 {
218     if ( stream_state_list == NULL )
219         return NULL;
220
221     hb_stream_t *ss;
222     int i = 0;
223
224     while ( ( ss = hb_list_item( stream_state_list, i++ ) ) != NULL )
225     {
226         if ( strcmp( path, ss->path ) == 0 )
227         {
228             break;
229         }
230     }
231     return ss;
232 }
233
234 static void hb_stream_state_delete( hb_stream_t *ss )
235 {
236     hb_list_rem( stream_state_list, ss );
237     free( ss->path );
238     free( ss );
239 }
240
241 /*
242  * logging routines.
243  * these frontend hb_log because transport streams can have a lot of errors
244  * so we want to rate limit messages. this routine limits the number of
245  * messages to at most one per minute of video. other errors that occur
246  * during the minute are counted & the count is output with the next
247  * error msg we print.
248  */
249 static void ts_warn_helper( hb_stream_t *stream, char *log, va_list args )
250 {
251     // limit error printing to at most one per minute of video (at 30fps)
252     ++stream->errors;
253     if ( stream->frames - stream->last_error_frame >= 30*60 )
254     {
255         char msg[256];
256
257         vsnprintf( msg, sizeof(msg), log, args );
258
259         if ( stream->errors - stream->last_error_count < 10 )
260         {
261             hb_log( "stream: error near frame %d: %s", stream->frames, msg );
262         }
263         else
264         {
265             int Edelta = stream->errors - stream->last_error_count;
266             double Epcnt = (double)Edelta * 100. /
267                             (stream->frames - stream->last_error_frame);
268             hb_log( "stream: %d new errors (%.0f%%) up to frame %d: %s",
269                     Edelta, Epcnt, stream->frames, msg );
270         }
271         stream->last_error_frame = stream->frames;
272         stream->last_error_count = stream->errors;
273     }
274 }
275
276 static void ts_warn( hb_stream_t *stream, char *log, ... )
277 {
278     va_list     args;
279     va_start( args, log );
280     ts_warn_helper( stream, log, args );
281     va_end( args );
282 }
283
284 static void ts_err( hb_stream_t *stream, int curstream, char *log, ... )
285 {
286     va_list     args;
287     va_start( args, log );
288     ts_warn_helper( stream, log, args );
289     va_end( args );
290
291     stream->ts_skipbad[curstream] = 1;
292     stream->ts_pos[curstream] = 0;
293     stream->ts_streamcont[curstream] = -1;
294 }
295
296 static int check_ps_sync(const uint8_t *buf)
297 {
298     // a legal MPEG program stream must start with a Pack header in the
299     // first four bytes.
300     return (buf[0] == 0x00) && (buf[1] == 0x00) &&
301            (buf[2] == 0x01) && (buf[3] == 0xba);
302 }
303
304 static int check_ps_sc(const uint8_t *buf)
305 {
306     // a legal MPEG program stream must start with a Pack followed by a
307     // some other start code. If we've already verified the pack, this skip
308     // it and checks for a start code prefix.
309     int pos = 14 + ( buf[13] & 0x7 );   // skip over the PACK
310     return (buf[pos+0] == 0x00) && (buf[pos+1] == 0x00) && (buf[pos+2] == 0x01);
311 }
312
313 static int check_ts_sync(const uint8_t *buf)
314 {
315     // must have initial sync byte, no scrambling & a legal adaptation ctrl
316     return (buf[0] == 0x47) && ((buf[3] >> 6) == 0) && ((buf[3] >> 4) > 0);
317 }
318
319 static int have_ts_sync(const uint8_t *buf, int psize)
320 {
321     return check_ts_sync(&buf[0*psize]) && check_ts_sync(&buf[1*psize]) &&
322            check_ts_sync(&buf[2*psize]) && check_ts_sync(&buf[3*psize]) &&
323            check_ts_sync(&buf[4*psize]) && check_ts_sync(&buf[5*psize]) &&
324            check_ts_sync(&buf[6*psize]) && check_ts_sync(&buf[7*psize]);
325 }
326
327 static int hb_stream_check_for_ts(const uint8_t *buf)
328 {
329     // transport streams should have a sync byte every 188 bytes.
330     // search the first 8KB of buf looking for at least 8 consecutive
331     // correctly located sync patterns.
332     int offset = 0;
333
334     for ( offset = 0; offset < 8*1024-8*188; ++offset )
335     {
336         if ( have_ts_sync( &buf[offset], 188) )
337             return 188 | (offset << 8);
338         if ( have_ts_sync( &buf[offset], 192) )
339             return 192 | (offset << 8);
340         if ( have_ts_sync( &buf[offset], 204) )
341             return 204 | (offset << 8);
342         if ( have_ts_sync( &buf[offset], 208) )
343             return 208 | (offset << 8);
344     }
345     return 0;
346 }
347
348 static int hb_stream_check_for_ps(const uint8_t *buf)
349 {
350     // program streams should start with a PACK then some other mpeg start
351     // code (usually a SYS but that might be missing if we only have a clip).
352     return check_ps_sync(buf) && check_ps_sc(buf);
353 }
354
355 static int hb_stream_check_for_dvd_ps(const uint8_t *buf)
356 {
357     // DVD program streams should have a Pack header every 2048 bytes.
358     // check that we have 4 of these in a row.
359     return check_ps_sync(&buf[0*2048]) && check_ps_sync(&buf[1*2048]) &&
360            check_ps_sync(&buf[2*2048]) && check_ps_sync(&buf[3*2048]);
361 }
362
363 static int hb_stream_get_type(hb_stream_t *stream)
364 {
365     uint8_t buf[2048*4];
366
367     if ( fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) )
368     {
369         int psize;
370         if ( ( psize = hb_stream_check_for_ts(buf) ) != 0 )
371         {
372             int offset = psize >> 8;
373             psize &= 0xff;
374             hb_log("file is MPEG Transport Stream with %d byte packets"
375                    " offset %d bytes", psize, offset);
376             stream->packetsize = psize;
377             stream->hb_stream_type = transport;
378             hb_ts_stream_init(stream);
379             if ( !stream->ts_number_video_pids || !stream->ts_number_audio_pids )
380             {
381                 return 0;
382             }
383             return 1;
384         }
385         if ( hb_stream_check_for_dvd_ps(buf) != 0 )
386         {
387             hb_log("file is MPEG DVD Program Stream");
388             stream->hb_stream_type = dvd_program;
389             return 1;
390         }
391         if ( hb_stream_check_for_ps(buf) != 0 )
392         {
393             hb_log("file is MPEG Program Stream");
394             stream->hb_stream_type = program;
395             return 1;
396         }
397     }
398     return 0;
399 }
400
401 static void hb_stream_delete_dynamic( hb_stream_t *d )
402 {
403     if( d->file_handle )
404     {
405         fclose( d->file_handle );
406                 d->file_handle = NULL;
407     }
408
409         int i=0;
410
411     if ( d->ts_packet )
412     {
413         free( d->ts_packet );
414         d->ts_packet = NULL;
415     }
416         for (i = 0; i < kMaxNumberDecodeStreams; i++)
417         {
418                 if (d->ts_buf[i])
419                 {
420                         hb_buffer_close(&(d->ts_buf[i]));
421                         d->ts_buf[i] = NULL;
422                 }
423         }
424 }
425
426 static void hb_stream_delete( hb_stream_t *d )
427 {
428     hb_stream_delete_dynamic( d );
429     free( d->path );
430     free( d );
431 }
432
433 static int audio_inactive( hb_stream_t *stream, int indx )
434 {
435     int aud_indx = indx - 1;
436
437     if ( stream->ts_audio_pids[aud_indx] < 0 )
438     {
439         // PID declared inactive by hb_stream_title_scan
440         return 1;
441     }
442     if ( stream->ts_audio_pids[aud_indx] == stream->pmt_info.PCR_PID )
443     {
444         // PCR PID is always active
445         return 0;
446     }
447
448     // see if we should make the stream inactive because scan.c didn't
449     // find a valid audio bitstream.
450     int i;
451     for ( i = 0; i < hb_list_count( stream->title->list_audio ); ++i )
452     {
453         hb_audio_t *audio = hb_list_item( stream->title->list_audio, i );
454         if ( audio->id == indx )
455         {
456             return 0;
457         }
458     }
459     // not in the title's audio list - declare the PID inactive
460     stream->ts_audio_pids[aud_indx] = -stream->ts_audio_pids[aud_indx];
461     return 1;
462 }
463
464 /***********************************************************************
465  * hb_stream_open
466  ***********************************************************************
467  *
468  **********************************************************************/
469 hb_stream_t * hb_stream_open( char *path, hb_title_t *title )
470 {
471     FILE *f = fopen( path, "r" );
472     if ( f == NULL )
473     {
474         hb_log( "hb_stream_open: open %s failed", path );
475         return NULL;
476     }
477
478     hb_stream_t *d = calloc( sizeof( hb_stream_t ), 1 );
479     if ( d == NULL )
480     {
481         fclose( f );
482         hb_log( "hb_stream_open: can't allocate space for %s stream state", path );
483         return NULL;
484     }
485
486     /*
487      * if we're opening the stream to read & convert, we need
488      * the state we saved when we scanned the stream. if we're
489      * opening the stream to scan it we want to rebuild the state
490      * (even if we have saved state, the stream may have changed).
491      */
492     hb_stream_t *ss = hb_stream_lookup( path );
493     if ( title && ss && ss->hb_stream_type != ffmpeg )
494     {
495         /*
496          * copy the saved state since we might be encoding the same stream
497          * multiple times.
498          */
499         memcpy( d, ss, sizeof(*d) );
500         d->file_handle = f;
501         d->title = title;
502         d->path = strdup( path );
503
504         if ( d->hb_stream_type == transport )
505         {
506             d->ts_packet = malloc( d->packetsize );
507
508             int i = 0;
509             for ( ; i < d->ts_number_video_pids + d->ts_number_audio_pids; i++)
510             {
511                 if ( i && audio_inactive( d, i ) )
512                 {
513                     // this PID isn't wanted (we don't have a codec for it
514                     // or scan didn't find audio parameters)
515                     continue;
516                 }
517                 d->ts_buf[i] = hb_buffer_init(d->packetsize);
518                                 d->ts_buf[i]->size = 0;
519             }
520             hb_stream_seek( d, 0. );
521         }
522         return d;
523     }
524
525     /*
526      * opening for scan - delete any saved state then (re)scan the stream.
527      * If it's something we can deal with (MPEG2 PS or TS) return a stream
528      * reference structure & null otherwise.
529      */
530     if ( ss != NULL )
531     {
532         hb_stream_state_delete( ss );
533     }
534     d->file_handle = f;
535     d->title = title;
536     d->path = strdup( path );
537     if (d->path != NULL )
538     {
539         if ( hb_stream_get_type( d ) != 0 )
540         {
541             return d;
542         }
543         fclose( d->file_handle );
544                 d->file_handle = NULL;
545         if ( ffmpeg_open( d, title ) )
546         {
547             return d;
548         }
549     }
550     if ( d->file_handle )
551     {
552         fclose( d->file_handle );
553     }
554     if (d->path)
555     {
556         free( d->path );
557     }
558     hb_log( "hb_stream_open: open %s failed", path );
559     free( d );
560     return NULL;
561 }
562
563 /***********************************************************************
564  * hb_stream_close
565  ***********************************************************************
566  * Closes and frees everything
567  **********************************************************************/
568 void hb_stream_close( hb_stream_t ** _d )
569 {
570     hb_stream_t *stream = * _d;
571
572     if ( stream->hb_stream_type == ffmpeg )
573     {
574         ffmpeg_close( stream );
575         hb_stream_delete( stream );
576         *_d = NULL;
577         return;
578     }
579
580     if ( stream->frames )
581     {
582         hb_log( "stream: %d good frames, %d errors (%.0f%%)", stream->frames,
583                 stream->errors, (double)stream->errors * 100. /
584                 (double)stream->frames );
585     }
586
587     /*
588      * if the stream was opened for a scan, cache the result, otherwise delete
589      * the state.
590      */
591     if ( stream->title == NULL )
592     {
593         hb_stream_delete_dynamic( stream );
594         if ( stream_state_list == NULL )
595         {
596             stream_state_list = hb_list_init();
597         }
598         hb_list_add( stream_state_list, stream );
599     }
600     else
601     {
602         hb_stream_delete( stream );
603     }
604     *_d = NULL;
605 }
606
607 /* when the file was first opened we made entries for all the audio elementary
608  * streams we found in it. Streams that were later found during the preview scan
609  * now have an audio codec, type, rate, etc., associated with them. At the end
610  * of the scan we delete all the audio entries that weren't found by the scan
611  * or don't have a format we support. This routine deletes audio entry 'indx'
612  * by setting its PID to an invalid value so no packet will match it. (We can't
613  * move any of the entries since the index of the entry is used as the id
614  * of the media stream for HB. */
615 static void hb_stream_delete_audio_entry(hb_stream_t *stream, int indx)
616 {
617     if ( stream->ts_audio_pids[indx] > 0 )
618     {
619         stream->ts_audio_pids[indx] = -stream->ts_audio_pids[indx];
620     }
621 }
622
623 static int index_of_pid(int pid, hb_stream_t *stream)
624 {
625     int i;
626
627     if ( pid == stream->ts_video_pids[0] )
628         return 0;
629
630     for ( i = 0; i < stream->ts_number_audio_pids; ++i )
631         if ( pid == stream->ts_audio_pids[i] )
632             return i + 1;
633
634     return -1;
635 }
636
637 /***********************************************************************
638  * hb_ps_stream_title_scan
639  ***********************************************************************
640  *
641  **********************************************************************/
642 hb_title_t * hb_stream_title_scan(hb_stream_t *stream)
643 {
644         if ( stream->hb_stream_type == ffmpeg )
645         return ffmpeg_title_scan( stream );
646
647     // 'Barebones Title'
648     hb_title_t *aTitle = hb_title_init( stream->path, 0 );
649     aTitle->index = 1;
650
651         // Copy part of the stream path to the title name
652         char *sep = strrchr(stream->path, '/');
653         if (sep)
654                 strcpy(aTitle->name, sep+1);
655         char *dot_term = strrchr(aTitle->name, '.');
656         if (dot_term)
657                 *dot_term = '\0';
658
659     // Height, width,  rate and aspect ratio information is filled in when the previews are built
660
661     hb_stream_duration(stream, aTitle);
662
663     // One Chapter
664     hb_chapter_t * chapter;
665     chapter = calloc( sizeof( hb_chapter_t ), 1 );
666     chapter->index = 1;
667     chapter->duration = aTitle->duration;
668     chapter->hours = aTitle->hours;
669     chapter->minutes = aTitle->minutes;
670     chapter->seconds = aTitle->seconds;
671     hb_list_add( aTitle->list_chapter, chapter );
672
673     // Figure out how many audio streams we really have:
674     // - For transport streams, for each PID listed in the PMT (whether
675     //   or not it was an audio stream type) read the bitstream until we
676     //   find an packet from that PID containing a PES header and see if
677     //   the elementary stream is an audio type.
678     // - For program streams read the first 4MB and take every unique
679     //   audio stream we find.
680         if (stream->hb_stream_type == transport)
681         {
682         int i;
683
684         for (i=0; i < stream->ts_number_audio_pids; i++)
685         {
686             hb_audio_t *audio = hb_ts_stream_set_audio_id_and_codec(stream, i);
687             if (audio->config.in.codec)
688                 hb_list_add( aTitle->list_audio, audio );
689             else
690             {
691                 free(audio);
692                 hb_stream_delete_audio_entry(stream, i);
693             }
694         }
695
696         // make sure we're grabbing the PCR PID
697         if ( index_of_pid( stream->pmt_info.PCR_PID, stream ) < 0 )
698         {
699             stream->ts_audio_pids[stream->ts_number_audio_pids++] =
700                 stream->pmt_info.PCR_PID;
701         }
702
703         // set the video id, codec & muxer
704         aTitle->video_id = 0;
705         aTitle->video_codec = st2codec[stream->ts_stream_type[0]].codec;
706         aTitle->video_codec_param = st2codec[stream->ts_stream_type[0]].codec_param;
707         aTitle->demuxer = HB_MPEG2_TS_DEMUXER;
708         }
709     else
710     {
711         hb_ps_stream_find_audio_ids(stream, aTitle);
712     }
713
714   return aTitle;
715 }
716
717 /*
718  * read the next transport stream packet from 'stream'. Return NULL if
719  * we hit eof & a pointer to the sync byte otherwise.
720  */
721 static const uint8_t *next_packet( hb_stream_t *stream )
722 {
723     uint8_t *buf = stream->ts_packet + stream->packetsize - 188;
724
725     while ( 1 )
726     {
727         if ( fread(stream->ts_packet, 1, stream->packetsize, stream->file_handle) !=
728              stream->packetsize )
729         {
730             return NULL;
731         }
732         if (buf[0] == 0x47)
733         {
734             return buf;
735         }
736         // lost sync - back up to where we started then try to re-establish.
737         off_t pos = ftello(stream->file_handle) - stream->packetsize;
738         off_t pos2 = align_to_next_packet(stream);
739         if ( pos2 == 0 )
740         {
741             hb_log( "next_packet: eof while re-establishing sync @ %lld", pos );
742             return NULL;
743         }
744         ts_warn( stream, "next_packet: sync lost @ %lld, regained after %lld bytes",
745                  pos, pos2 );
746     }
747 }
748
749 /*
750  * skip to the start of the next PACK header in program stream src_stream.
751  */
752 static void skip_to_next_pack( hb_stream_t *src_stream )
753 {
754     // scan forward until we find the start of the next pack
755     uint32_t strt_code = -1;
756     int c;
757
758     flockfile( src_stream->file_handle );
759     while ( ( c = getc_unlocked( src_stream->file_handle ) ) != EOF )
760     {
761         strt_code = ( strt_code << 8 ) | c;
762         if ( strt_code == 0x000001ba )
763             // we found the start of the next pack
764             break;
765     }
766     funlockfile( src_stream->file_handle );
767
768     // if we didn't terminate on an eof back up so the next read
769     // starts on the pack boundary.
770     if ( c != EOF )
771     {
772         fseeko( src_stream->file_handle, -4, SEEK_CUR );
773     }
774 }
775
776 /*
777  * scan the next MB of 'stream' to try to find a random access point
778  */
779 static void hb_ts_stream_find_RAP( hb_stream_t *stream )
780 {
781     off_t starting_point = ftello(stream->file_handle);
782     int npack = 300000; // max packets to read
783
784     while (--npack >= 0)
785     {
786         off_t cur = ftello(stream->file_handle);
787         const uint8_t *buf = next_packet( stream );
788         if ( buf == NULL )
789         {
790             break;
791         }
792         switch (buf[3] & 0x30)
793         {
794             case 0x00: // illegal
795                 continue;
796
797             case 0x20: // fill packet
798             case 0x30: // adaptation
799                 if ( buf[5] & 0x40 )
800                 {
801                     // found a random access point
802                     fseeko( stream->file_handle, cur, SEEK_SET );
803                     return;
804                 }
805                 continue;
806         }
807     }
808
809     /* didn't find it */
810     fseeko( stream->file_handle, starting_point, SEEK_SET );
811     stream->ts_no_RAP = 1;
812 }
813
814 /*
815  * scan the next MB of 'stream' to find the next start packet for
816  * the Packetized Elementary Stream associated with TS PID 'pid'.
817  */
818 static const uint8_t *hb_ts_stream_getPEStype(hb_stream_t *stream, uint32_t pid)
819 {
820     int npack = 300000; // max packets to read
821
822     while (--npack >= 0)
823     {
824         const uint8_t *buf = next_packet( stream );
825         if ( buf == NULL )
826         {
827             hb_log("hb_ts_stream_getPEStype: EOF while searching for PID 0x%x", pid);
828             return 0;
829         }
830
831         /*
832          * The PES header is only in TS packets with 'start' set so we check
833          * that first then check for the right PID.
834          */
835         if ((buf[1] & 0x40) == 0 || (buf[1] & 0x1f) != (pid >> 8) ||
836             buf[2] != (pid & 0xff))
837         {
838             // not a start packet or not the pid we want
839             continue;
840         }
841
842         /* skip over the TS hdr to return a pointer to the PES hdr */
843         int udata = 4;
844         switch (buf[3] & 0x30)
845         {
846             case 0x00: // illegal
847             case 0x20: // fill packet
848                 continue;
849
850             case 0x30: // adaptation
851                 if (buf[4] > 182)
852                 {
853                     hb_log("hb_ts_stream_getPEStype: invalid adaptation field length %d for PID 0x%x", buf[4], pid);
854                     continue;
855                 }
856                 udata += buf[4] + 1;
857                 break;
858         }
859         /* PES hdr has to begin with an mpeg start code */
860         if (buf[udata+0] == 0x00 && buf[udata+1] == 0x00 && buf[udata+2] == 0x01)
861         {
862             return &buf[udata];
863         }
864     }
865
866     /* didn't find it */
867     return 0;
868 }
869
870 static uint64_t hb_ps_stream_getVideoPTS(hb_stream_t *stream)
871 {
872     hb_buffer_t *buf  = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
873     hb_list_t *list = hb_list_init();
874     // how many blocks we read while searching for a video PES header
875     int blksleft = 1024;
876     uint64_t pts = 0;
877
878     while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
879     {
880         hb_buffer_t *es;
881
882         // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
883         hb_demux_ps( buf, list, 0 );
884
885         while ( ( es = hb_list_item( list, 0 ) ) )
886         {
887             hb_list_rem( list, es );
888             if ( es->id == 0xe0 )
889             {
890                 // this PES contains video - if there's a PTS we're done
891                 // hb_demux_ps left the PTS in buf_es->start.
892                 if ( es->start != ~0 )
893                 {
894                     pts = es->start;
895                     blksleft = 0;
896                     break;
897                 }
898             }
899             hb_buffer_close( &es );
900         }
901     }
902     hb_list_empty( &list );
903     hb_buffer_close(&buf);
904     return pts;
905 }
906
907 /***********************************************************************
908  * hb_stream_duration
909  ***********************************************************************
910  *
911  * Finding stream duration is difficult.  One issue is that the video file
912  * may have chunks from several different program fragments (main feature,
913  * commercials, station id, trailers, etc.) all with their own base pts
914  * value.  We can't find the piece boundaries without reading the entire
915  * file but if we compute a rate based on time stamps from two different
916  * pieces the result will be meaningless.  The second issue is that the
917  * data rate of compressed video normally varies by 5-10x over the length
918  * of the video. This says that we want to compute the rate over relatively
919  * long segments to get a representative average but long segments increase
920  * the likelihood that we'll cross a piece boundary.
921  *
922  * What we do is take time stamp samples at several places in the file
923  * (currently 16) then compute the average rate (i.e., ticks of video per
924  * byte of the file) for all pairs of samples (N^2 rates computed for N
925  * samples). Some of those rates will be absurd because the samples came
926  * from different segments. Some will be way low or high because the
927  * samples came from a low or high motion part of the segment. But given
928  * that we're comparing *all* pairs the majority of the computed rates
929  * should be near the overall average.  So we median filter the computed
930  * rates to pick the most representative value.
931  *
932  **********************************************************************/
933 struct pts_pos {
934     uint64_t pos;   /* file position of this PTS sample */
935     uint64_t pts;   /* PTS from video stream */
936 };
937
938 #define NDURSAMPLES 64
939
940 // get one (position, timestamp) sampple from a transport or program
941 // stream.
942 static struct pts_pos hb_sample_pts(hb_stream_t *stream, uint64_t fpos)
943 {
944     struct pts_pos pp = { 0, 0 };
945
946     if ( stream->hb_stream_type == transport )
947     {
948         const uint8_t *buf;
949         fseeko( stream->file_handle, fpos, SEEK_SET );
950         align_to_next_packet( stream );
951         buf = hb_ts_stream_getPEStype( stream, stream->ts_video_pids[0] );
952         if ( buf == NULL )
953         {
954             hb_log("hb_sample_pts: couldn't find video packet near %llu", fpos);
955             return pp;
956         }
957         if ( ( buf[7] >> 7 ) != 1 )
958         {
959             hb_log("hb_sample_pts: no PTS in video packet near %llu", fpos);
960             return pp;
961         }
962         pp.pts = ( ( (uint64_t)buf[9] >> 1 ) & 7 << 30 ) |
963                  ( (uint64_t)buf[10] << 22 ) |
964                  ( ( (uint64_t)buf[11] >> 1 ) << 15 ) |
965                  ( (uint64_t)buf[12] << 7 ) |
966                  ( (uint64_t)buf[13] >> 1 );
967     }
968     else
969     {
970         // round address down to nearest dvd sector start
971         fpos &=~ ( HB_DVD_READ_BUFFER_SIZE - 1 );
972         fseeko( stream->file_handle, fpos, SEEK_SET );
973         if ( stream->hb_stream_type == program )
974         {
975             skip_to_next_pack( stream );
976         }
977         pp.pts = hb_ps_stream_getVideoPTS( stream );
978     }
979     pp.pos = ftello(stream->file_handle);
980     return pp;
981 }
982
983 static int dur_compare( const void *a, const void *b )
984 {
985     const double *aval = a, *bval = b;
986     return ( *aval < *bval ? -1 : ( *aval == *bval ? 0 : 1 ) );
987 }
988
989 // given an array of (position, time) samples, compute a max-likelihood
990 // estimate of the average rate by computing the rate between all pairs
991 // of samples then taking the median of those rates.
992 static double compute_stream_rate( struct pts_pos *pp, int n )
993 {
994     int i, j;
995     double rates[NDURSAMPLES * NDURSAMPLES / 8];
996     double *rp = rates;
997
998     // the following nested loops compute the rates between all pairs.
999     *rp = 0;
1000     for ( i = 0; i < n-1; ++i )
1001     {
1002         // Bias the median filter by not including pairs that are "far"
1003         // from one another. This is to handle cases where the file is
1004         // made of roughly equal size pieces where a symmetric choice of
1005         // pairs results in having the same number of intra-piece &
1006         // inter-piece rate estimates. This would mean that the median
1007         // could easily fall in the inter-piece part of the data which
1008         // would give a bogus estimate. The 'ns' index creates an
1009         // asymmetry that favors locality.
1010         int ns = i + ( n >> 3 );
1011         if ( ns > n )
1012             ns = n;
1013         for ( j = i+1; j < ns; ++j )
1014         {
1015             if ( (uint64_t)(pp[j].pts - pp[i].pts) > 90000LL*3600*6 )
1016                 break;
1017             if ( pp[j].pts != pp[i].pts && pp[j].pos > pp[i].pos )
1018             {
1019                 *rp = ((double)( pp[j].pts - pp[i].pts )) /
1020                       ((double)( pp[j].pos - pp[i].pos ));
1021                                 ++rp;
1022             }
1023         }
1024     }
1025     // now compute and return the median of all the (n*n/2) rates we computed
1026     // above.
1027     int nrates = rp - rates;
1028     qsort( rates, nrates, sizeof (rates[0] ), dur_compare );
1029     return rates[nrates >> 1];
1030 }
1031
1032 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle)
1033 {
1034     struct pts_pos ptspos[NDURSAMPLES];
1035     struct pts_pos *pp = ptspos;
1036     int i;
1037
1038     fseeko(stream->file_handle, 0, SEEK_END);
1039     uint64_t fsize = ftello(stream->file_handle);
1040     uint64_t fincr = fsize / NDURSAMPLES;
1041     uint64_t fpos = fincr / 2;
1042     for ( i = NDURSAMPLES; --i >= 0; fpos += fincr )
1043     {
1044         *pp++ = hb_sample_pts(stream, fpos);
1045     }
1046     uint64_t dur = compute_stream_rate( ptspos, pp - ptspos ) * (double)fsize;
1047     inTitle->duration = dur;
1048     dur /= 90000;
1049     inTitle->hours    = dur / 3600;
1050     inTitle->minutes  = ( dur % 3600 ) / 60;
1051     inTitle->seconds  = dur % 60;
1052
1053     rewind(stream->file_handle);
1054 }
1055
1056 /***********************************************************************
1057  * hb_stream_read
1058  ***********************************************************************
1059  *
1060  **********************************************************************/
1061 int hb_stream_read( hb_stream_t * src_stream, hb_buffer_t * b )
1062 {
1063         if ( src_stream->hb_stream_type == ffmpeg )
1064     {
1065         return ffmpeg_read( src_stream, b );
1066     }
1067     if ( src_stream->hb_stream_type == dvd_program )
1068     {
1069         size_t amt_read = fread(b->data, HB_DVD_READ_BUFFER_SIZE, 1,
1070                                 src_stream->file_handle);
1071         return (amt_read > 0);
1072     }
1073     if ( src_stream->hb_stream_type == program )
1074     {
1075         // a general program stream has arbitrary sized pack's. we're
1076         // currently positioned at the start of a pack so read up to but
1077         // not including the start of the next, expanding the buffer
1078         // as necessary.
1079         uint8_t *cp = b->data;
1080         uint8_t *ep = cp + b->alloc;
1081         uint32_t strt_code = -1;
1082         int c;
1083
1084         // consume the first byte of the initial pack so we don't match on
1085         // it in the loop below.
1086         if ( ( c = getc( src_stream->file_handle ) ) == EOF )
1087             return 0;
1088
1089         *cp++ = c;
1090
1091         flockfile( src_stream->file_handle );
1092         while ( ( c = getc_unlocked( src_stream->file_handle ) ) != EOF )
1093         {
1094             strt_code = ( strt_code << 8 ) | c;
1095             if ( strt_code == 0x000001ba )
1096                 // we found the start of the next pack
1097                 break;
1098             if ( cp >= ep )
1099             {
1100                 // need to expand the buffer
1101                 int curSize = cp - b->data;
1102                 hb_buffer_realloc( b, curSize * 2 );
1103                 cp = b->data + curSize;
1104                 ep = b->data + b->alloc;
1105             }
1106             *cp++ = c;
1107         }
1108         funlockfile( src_stream->file_handle );
1109
1110         // if we didn't terminate on an eof back up so the next read
1111         // starts on the pack boundary.
1112         b->size = cp - b->data;
1113         if ( c != EOF )
1114         {
1115             fseeko( src_stream->file_handle, -4, SEEK_CUR );
1116             b->size -= 4;
1117         }
1118         return 1;
1119     }
1120     return hb_ts_stream_decode( src_stream, b );
1121 }
1122
1123 /***********************************************************************
1124  * hb_stream_seek_chapter
1125  ***********************************************************************
1126  *
1127  **********************************************************************/
1128 int hb_stream_seek_chapter( hb_stream_t * stream, int chapter_num )
1129 {
1130     AVFormatContext *ic = stream->ffmpeg_ic;
1131     uint64_t end_offset = 0;
1132     uint64_t start_offset = 0;
1133     uint64_t pos = 0;
1134     hb_chapter_t *chapter = NULL;
1135     int i;
1136
1137     if( !stream || !stream->title )
1138     {
1139         return 0;
1140     }
1141     if ( stream->hb_stream_type != ffmpeg )
1142     {
1143         // currently meaningliess for transport and program streams
1144         return 1;
1145     }
1146
1147     for( i = 0; i < chapter_num; i++)
1148     {
1149         chapter = hb_list_item( stream->title->list_chapter,
1150                                 i );
1151         
1152         if( chapter )
1153         {
1154             /*
1155              * Seeking to a chapter means that we are in that chapter,
1156              * so track which chapter we are in so that we can output
1157              * the correct chapter numbers in buf->new_chap
1158              */
1159             start_offset = end_offset;
1160             end_offset += chapter->duration;
1161             stream->chapter = i;
1162             stream->chapter_end = end_offset;
1163         } else {
1164             return 0;
1165         }
1166     }
1167
1168     /*
1169      * Is the the correct way to convert timebases? It seems to get it pretty
1170      * much right - plus a few seconds, which is odd.
1171      */
1172     pos = ((start_offset * AV_TIME_BASE) / 90000);
1173
1174     hb_deep_log( 2, "Seeking to chapter %d time (starts: %lld ends %lld) AV pos %lld", chapter_num-1, start_offset, end_offset, pos);
1175
1176     av_seek_frame( ic, -1, pos, 0);
1177
1178     return 1;
1179 }
1180
1181 /***********************************************************************
1182  * hb_stream_chapter
1183  ***********************************************************************
1184  * Return the number of the chapter that we are currently in. We store
1185  * the chapter number starting from 0, so + 1 for the real chpater num.
1186  **********************************************************************/
1187 int hb_stream_chapter( hb_stream_t * src_stream )
1188 {
1189     return( src_stream->chapter + 1 );
1190 }
1191
1192 /***********************************************************************
1193  * hb_stream_seek
1194  ***********************************************************************
1195  *
1196  **********************************************************************/
1197 int hb_stream_seek( hb_stream_t * stream, float f )
1198 {
1199         if ( stream->hb_stream_type == ffmpeg )
1200     {
1201         return ffmpeg_seek( stream, f );
1202     }
1203     off_t stream_size, cur_pos, new_pos;
1204     double pos_ratio = f;
1205     cur_pos = ftello( stream->file_handle );
1206     fseeko( stream->file_handle, 0, SEEK_END );
1207     stream_size = ftello( stream->file_handle );
1208     new_pos = (off_t) ((double) (stream_size) * pos_ratio);
1209     new_pos &=~ (HB_DVD_READ_BUFFER_SIZE - 1);
1210
1211     int r = fseeko( stream->file_handle, new_pos, SEEK_SET );
1212     if (r == -1)
1213     {
1214         fseeko( stream->file_handle, cur_pos, SEEK_SET );
1215         return 0;
1216     }
1217
1218     if ( stream->hb_stream_type == transport )
1219     {
1220         // We need to drop the current decoder output and move
1221         // forwards to the next transport stream packet.
1222         hb_ts_stream_reset(stream);
1223         if ( f > 0 )
1224         {
1225             if ( !stream->ts_no_RAP )
1226             {
1227                 // we're not at the beginning - try to find a random access point
1228                 hb_ts_stream_find_RAP( stream );
1229             }
1230             stream->need_keyframe = 1;
1231         }
1232         else
1233         {
1234             // we're at the beginning - say we have video sync so that we
1235             // won't drop initial SPS & PPS data on an AVC stream.
1236             stream->need_keyframe = 0;
1237         }
1238     }
1239     else if ( stream->hb_stream_type == program )
1240     {
1241         skip_to_next_pack( stream );
1242     }
1243
1244     return 1;
1245 }
1246
1247 static const char* make_upper( const char* s )
1248 {
1249     static char name[8];
1250     char *cp = name;
1251     char *ep = cp + sizeof(name)-1;
1252
1253     while ( *s && cp < ep )
1254     {
1255         *cp++ = islower(*s)? toupper(*s) : *s;
1256         ++s;
1257     }
1258     *cp = 0;
1259     return name;
1260 }
1261
1262 static void set_audio_description( hb_audio_t *audio, iso639_lang_t *lang )
1263 {
1264     /* XXX
1265      * This is a duplicate of code in dvd.c - it should get factored out
1266      * into a common routine. We probably should only be putting the lang
1267      * code or a lang pointer into the audio config & let the common description
1268      * formatting routine in scan.c do all the stuff below.
1269      */
1270     const char *codec_name;
1271     AVCodecContext *cc;
1272
1273     if ( audio->config.in.codec == HB_ACODEC_FFMPEG &&
1274          ( cc = hb_ffmpeg_context( audio->config.in.codec_param ) ) &&
1275          avcodec_find_decoder( cc->codec_id ) )
1276     {
1277         codec_name = make_upper( avcodec_find_decoder( cc->codec_id )->name );
1278         if ( !strcmp( codec_name, "LIBFAAD" ) )
1279         {
1280             codec_name = "AAC";
1281         }
1282     }
1283     else if ( audio->config.in.codec == HB_ACODEC_MPGA &&
1284               avcodec_find_decoder( audio->config.in.codec_param ) )
1285     {
1286         codec_name = avcodec_find_decoder( audio->config.in.codec_param )->name;
1287     }
1288     else
1289     {
1290         codec_name = audio->config.in.codec == HB_ACODEC_AC3 ? "AC3" :
1291                      audio->config.in.codec == HB_ACODEC_DCA ? "DTS" :
1292                      audio->config.in.codec == HB_ACODEC_MPGA ? "MPEG" : 
1293                      audio->config.in.codec == HB_ACODEC_LPCM ? "LPCM" : 
1294                      audio->config.in.codec == HB_ACODEC_FFMPEG ? "FFMPEG" :
1295                      "Unknown";
1296     }
1297     snprintf( audio->config.lang.description,
1298               sizeof( audio->config.lang.description ), "%s (%s)",
1299               strlen(lang->native_name) ? lang->native_name : lang->eng_name,
1300               codec_name );
1301     snprintf( audio->config.lang.simple, sizeof( audio->config.lang.simple ), "%s",
1302               strlen(lang->native_name) ? lang->native_name : lang->eng_name );
1303     snprintf( audio->config.lang.iso639_2, sizeof( audio->config.lang.iso639_2 ),
1304               "%s", lang->iso639_2);
1305 }
1306
1307 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
1308                                                        int aud_pid_index)
1309 {
1310     off_t cur_pos = ftello(stream->file_handle);
1311     hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
1312     const uint8_t *buf;
1313
1314     fseeko(stream->file_handle, 0, SEEK_SET);
1315     align_to_next_packet(stream);
1316     buf = hb_ts_stream_getPEStype(stream, stream->ts_audio_pids[aud_pid_index]);
1317
1318     /* check that we found a PES header */
1319     uint8_t stype = 0;
1320     if (buf && buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
1321     {
1322         stype = stream->ts_stream_type[1 + aud_pid_index];
1323
1324         // 0xbd ("private stream 1") is the normal container for non-ISO
1325         // media - AC3/DCA/PCM/etc.
1326         if ( buf[3] == 0xbd )
1327         {
1328             if ( st2codec[stype].kind == U )
1329             {
1330                 // XXX assume unknown stream types are AC-3 (if they're not
1331                 // audio we'll find that out during the scan but if they're
1332                 // some other type of audio we'll end up ignoring them).
1333                 stype = 0x81;
1334                 stream->ts_stream_type[1 + aud_pid_index] = 0x81;
1335             }
1336         }
1337         else if ( buf[3] == 0xfd )
1338         {
1339             // 0xfd indicates an extended stream id (ISO 13818-1(2007)).
1340             // the blu ray consortium apparently forgot to read the portion
1341             // of the MPEG spec that says one PID should map to one media
1342             // stream and multiplexed multiple types of audio into one PID
1343             // using the extended stream identifier of the PES header to
1344             // distinguish them. So we have to check if that's happening and
1345             // if so tell the runtime what esid we want.
1346             if ( st2codec[stype].kind == A && stype == 0x83 &&
1347                  stream->ts_format_id[1 + aud_pid_index] == TS_FORMAT_ID_AC3 )
1348             {
1349                 // This is an interleaved TrueHD/AC-3 stream and the esid of
1350                 // the AC-3 is 0x76
1351                 stream->ts_multiplexed[1 + aud_pid_index] = 0x76;
1352                 stype = 0x81;
1353                 stream->ts_stream_type[1 + aud_pid_index] = 0x81;
1354             }
1355             if ( st2codec[stype].kind == A && stype == 0x86 )
1356             {
1357                 // This is an interleaved DTS-HD/DTS stream and the esid of
1358                 // the DTS is 0x71
1359                 stream->ts_multiplexed[1 + aud_pid_index] = 0x71;
1360                 stype = 0x82;
1361                 stream->ts_stream_type[1 + aud_pid_index] = 0x82;
1362             }
1363         }
1364         else if ((buf[3] & 0xe0) == 0xc0)
1365         {
1366             // 0xC0 - 0xCF are the normal containers for ISO-standard
1367             // media (mpeg2 audio and mpeg4 AAC).
1368             if ( st2codec[stype].kind == U )
1369             {
1370                 // XXX assume unknown stream types are MPEG audio
1371                 stype = 0x03;
1372                 stream->ts_stream_type[1 + aud_pid_index] = 0x03;
1373             }
1374         }
1375         else
1376         {
1377             stype = 0;
1378         }
1379     }
1380     // if we found an audio stream type & HB has a codec that can decode it
1381     // finish configuring the audio so we'll add it to the title's list.
1382     if ( st2codec[stype].kind == A && st2codec[stype].codec )
1383     {
1384         audio->id = 1 + aud_pid_index;
1385         audio->config.in.codec = st2codec[stype].codec;
1386         audio->config.in.codec_param = st2codec[stype].codec_param;
1387                 set_audio_description( audio,
1388                   lang_for_code( stream->a52_info[aud_pid_index].lang_code ) );
1389         hb_log("transport stream pid 0x%x (type 0x%x) may be %s audio (id 0x%x)",
1390                stream->ts_audio_pids[aud_pid_index],
1391                stype, st2codec[stype].name, audio->id);
1392     }
1393     else
1394     {
1395         if ( buf )
1396         {
1397             hb_log("transport stream pid 0x%x (type 0x%x, substream 0x%x) "
1398                     "isn't audio", stream->ts_audio_pids[aud_pid_index],
1399                     stream->ts_stream_type[1 + aud_pid_index], buf[3]);
1400         }
1401         else
1402         {
1403             hb_log("transport stream pid 0x%x (type 0x%x) isn't audio",
1404                     stream->ts_audio_pids[aud_pid_index],
1405                     stream->ts_stream_type[1 + aud_pid_index]);
1406         }
1407         }
1408     fseeko(stream->file_handle, cur_pos, SEEK_SET);
1409     return audio;
1410 }
1411
1412 static void add_audio_to_title(hb_title_t *title, int id)
1413 {
1414     hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
1415
1416     audio->id = id;
1417     switch ( id >> 12 )
1418     {
1419         case 0x0:
1420             audio->config.in.codec = HB_ACODEC_MPGA;
1421             hb_log("add_audio_to_title: added MPEG audio stream 0x%x", id);
1422             break;
1423         case 0x2:
1424             // type 2 is a DVD subtitle stream - just ignore it */
1425             free( audio );
1426             return;
1427         case 0x8:
1428             audio->config.in.codec = HB_ACODEC_AC3;
1429             hb_log("add_audio_to_title: added AC3 audio stream 0x%x", id);
1430             break;
1431         case 0xa:
1432             audio->config.in.codec = HB_ACODEC_LPCM;
1433             hb_log("add_audio_to_title: added LPCM audio stream 0x%x", id);
1434             break;
1435         default:
1436             hb_log("add_audio_to_title: unknown audio stream type 0x%x", id);
1437             free( audio );
1438             return;
1439
1440     }
1441     set_audio_description( audio, lang_for_code( 0 ) );
1442     hb_list_add( title->list_audio, audio );
1443 }
1444
1445 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title)
1446 {
1447     off_t cur_pos = ftello(stream->file_handle);
1448     hb_buffer_t *buf  = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
1449     hb_list_t *list = hb_list_init();
1450     // how many blocks we read while searching for audio streams
1451     int blksleft = 4096;
1452     // there can be at most 16 unique streams in an MPEG PS (8 in a DVD)
1453     // so we use a bitmap to keep track of the ones we've already seen.
1454     // Bit 'i' of smap is set if we've already added the audio for
1455     // audio substream id 'i' to the title's audio list.
1456     uint32_t smap = 0;
1457
1458     // start looking 20% into the file since there's occasionally no
1459     // audio at the beginning (particularly for vobs).
1460     hb_stream_seek(stream, 0.2f);
1461
1462     while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
1463     {
1464         hb_buffer_t *es;
1465
1466         // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
1467         hb_demux_ps( buf, list, 0 );
1468
1469         while ( ( es = hb_list_item( list, 0 ) ) )
1470         {
1471             hb_list_rem( list, es );
1472             if ( (es->id & 0xff) == 0xbd || (es->id & 0xe0) == 0xc0 )
1473             {
1474                 // this PES contains some kind of audio - get the substream id
1475                 // and check if we've seen it already.
1476                 int ssid = (es->id > 0xff ? es->id >> 8 : es->id) & 0xf;
1477                 if ( (smap & (1 << ssid)) == 0 )
1478                 {
1479                     // we haven't seen this stream before - add it to the
1480                     // title's list of audio streams.
1481                     smap |= (1 << ssid);
1482                     add_audio_to_title(title, es->id);
1483                 }
1484             }
1485             hb_buffer_close( &es );
1486         }
1487     }
1488     hb_list_empty( &list );
1489     hb_buffer_close(&buf);
1490     fseeko(stream->file_handle, cur_pos, SEEK_SET);
1491 }
1492
1493 /***********************************************************************
1494  * hb_ts_stream_init
1495  ***********************************************************************
1496  *
1497  **********************************************************************/
1498
1499 static void hb_ts_stream_init(hb_stream_t *stream)
1500 {
1501         int i;
1502
1503         for (i=0; i < kMaxNumberDecodeStreams; i++)
1504         {
1505                 stream->ts_streamcont[i] = -1;
1506         }
1507         stream->ts_video_pids[0] = -1;
1508     for ( i = 0; i < stream->ts_number_audio_pids; i++ )
1509     {
1510         stream-> ts_audio_pids[i] = -1;
1511     }
1512
1513     stream->ts_packet = malloc( stream->packetsize );
1514
1515         // Find the audio and video pids in the stream
1516         hb_ts_stream_find_pids(stream);
1517
1518         for (i = 0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1519         {
1520         // demuxing buffer for TS to PS conversion
1521                 stream->ts_buf[i] = hb_buffer_init(stream->packetsize);
1522                 stream->ts_buf[i]->size = 0;
1523         }
1524 }
1525
1526 #define MAX_HOLE 208*80
1527
1528 static off_t align_to_next_packet(hb_stream_t *stream)
1529 {
1530     uint8_t buf[MAX_HOLE];
1531         off_t pos = 0;
1532     off_t start = ftello(stream->file_handle);
1533
1534     if ( start >= stream->packetsize ) {
1535         start -= stream->packetsize;
1536         fseeko(stream->file_handle, start, SEEK_SET);
1537     }
1538
1539     if (fread(buf, sizeof(buf), 1, stream->file_handle) == 1)
1540         {
1541         const uint8_t *bp = buf;
1542         int i;
1543
1544         for ( i = sizeof(buf); --i >= 0; ++bp )
1545         {
1546             if ( have_ts_sync( bp, stream->packetsize ) )
1547             {
1548                 break;
1549             }
1550         }
1551         if ( i >= 0 )
1552         {
1553             pos = ( bp - buf ) - stream->packetsize + 188;
1554             if ( pos < 0 )
1555                 pos = 0;
1556         }
1557         }
1558     fseeko(stream->file_handle, start+pos, SEEK_SET);
1559         return pos;
1560 }
1561
1562
1563 typedef struct {
1564     uint8_t *buf;
1565     uint32_t val;
1566     int pos;
1567 } bitbuf_t;
1568
1569 static const unsigned int bitmask[] = {
1570         0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,
1571         0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff,0xffff,
1572         0x1ffff,0x3ffff,0x7ffff,0xfffff,0x1fffff,0x3fffff,0x7fffff,0xffffff,
1573         0x1ffffff,0x3ffffff,0x7ffffff,0xfffffff,0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
1574
1575 static inline void set_buf(bitbuf_t *bb, uint8_t* buf, int bufsize, int clear)
1576 {
1577         bb->pos = 0;
1578         bb->buf = buf;
1579         bb->val = (bb->buf[0] << 24) | (bb->buf[1] << 16) |
1580               (bb->buf[2] << 8) | bb->buf[3];
1581         if (clear)
1582                 memset(bb->buf, 0, bufsize);
1583 }
1584
1585 static inline int buf_size(bitbuf_t *bb)
1586 {
1587         return bb->pos >> 3;
1588 }
1589
1590 static inline unsigned int get_bits(bitbuf_t *bb, int bits)
1591 {
1592         unsigned int val;
1593         int left = 32 - (bb->pos & 31);
1594
1595         if (bits < left)
1596         {
1597                 val = (bb->val >> (left - bits)) & bitmask[bits];
1598                 bb->pos += bits;
1599         }
1600         else
1601         {
1602                 val = (bb->val & bitmask[left]) << (bits - left);
1603                 bb->pos += left;
1604                 bits -= left;
1605
1606                 int pos = bb->pos >> 3;
1607                 bb->val = (bb->buf[pos] << 24) | (bb->buf[pos + 1] << 16) | (bb->buf[pos + 2] << 8) | bb->buf[pos + 3];
1608
1609                 if (bits > 0)
1610                 {
1611                         val |= (bb->val >> (32 - bits)) & bitmask[bits];
1612                         bb->pos += bits;
1613                 }
1614         }
1615
1616         return val;
1617 }
1618
1619 // extract what useful information we can from the elementary stream
1620 // descriptor list at 'dp' and add it to the stream at 'esindx'.
1621 // Descriptors with info we don't currently use are ignored.
1622 // The descriptor list & descriptor item formats are defined in
1623 // ISO 13818-1 (2000E) section 2.6 (pg. 62).
1624 static void decode_element_descriptors(hb_stream_t* stream, int esindx,
1625                                        const uint8_t *dp, uint8_t dlen)
1626 {
1627     const uint8_t *ep = dp + dlen;
1628
1629     while (dp < ep)
1630     {
1631         switch (dp[0])
1632         {
1633             case 5:    // Registration descriptor
1634                 stream->ts_format_id[esindx] = (dp[2] << 24) | (dp[3] << 16) |
1635                                                (dp[4] << 8)  | dp[5];
1636                 break;
1637
1638             case 10:    // ISO_639_language descriptor
1639                 stream->a52_info[esindx].lang_code = lang_to_code(lang_for_code2((const char *)&dp[2]));
1640                 break;
1641
1642             default:
1643                 break;
1644         }
1645         dp += dp[1] + 2;
1646     }
1647 }
1648
1649 static const char *stream_type_name (uint8_t stream_type)
1650 {
1651     return st2codec[stream_type].name? st2codec[stream_type].name : "Unknown";
1652 }
1653
1654 int decode_program_map(hb_stream_t* stream)
1655 {
1656     bitbuf_t bb;
1657         set_buf(&bb, stream->pmt_info.tablebuf, stream->pmt_info.tablepos, 0);
1658
1659     get_bits(&bb, 8);  // table_id
1660     get_bits(&bb, 4);
1661     unsigned int section_length = get_bits(&bb, 12);
1662     stream->pmt_info.section_length = section_length;
1663
1664     unsigned int program_number = get_bits(&bb, 16);
1665     stream->pmt_info.program_number = program_number;
1666     get_bits(&bb, 2);
1667     get_bits(&bb, 5);  // version_number
1668     get_bits(&bb, 1);
1669     get_bits(&bb, 8);  // section_number
1670     get_bits(&bb, 8);  // last_section_number
1671     get_bits(&bb, 3);
1672     unsigned int PCR_PID = get_bits(&bb, 13);
1673     stream->pmt_info.PCR_PID = PCR_PID;
1674     get_bits(&bb, 4);
1675     unsigned int program_info_length = get_bits(&bb, 12);
1676     stream->pmt_info.program_info_length = program_info_length;
1677
1678         int i=0;
1679         unsigned char *descriptor_buf = (unsigned char *) malloc(program_info_length);
1680         for (i = 0; i < program_info_length; i++)
1681         {
1682           descriptor_buf[i] = get_bits(&bb, 8);
1683         }
1684
1685         int cur_pos =  9 /* data after the section length field*/ + program_info_length;
1686         int done_reading_stream_types = 0;
1687         while (!done_reading_stream_types)
1688     {
1689         unsigned char stream_type = get_bits(&bb, 8);
1690         get_bits(&bb, 3);
1691         unsigned int elementary_PID = get_bits(&bb, 13);
1692         get_bits(&bb, 4);
1693         unsigned int ES_info_length = get_bits(&bb, 12);
1694
1695         int i=0;
1696         unsigned char *ES_info_buf = (unsigned char *) malloc(ES_info_length);
1697         for (i=0; i < ES_info_length; i++)
1698         {
1699             ES_info_buf[i] = get_bits(&bb, 8);
1700         }
1701
1702
1703         if ( index_of_pid( elementary_PID, stream ) < 0 )
1704         {
1705             // already have this pid - do nothing
1706         }
1707         if (stream->ts_number_video_pids == 0 && st2codec[stream_type].kind == V )
1708         {
1709             stream->ts_video_pids[0] = elementary_PID;
1710             stream->ts_stream_type[0] = stream_type;
1711             stream->ts_number_video_pids = 1;
1712         }
1713         else
1714         {
1715             // Defined audio stream types are 0x81 for AC-3/A52 audio and 0x03
1716             // for mpeg audio. But content producers seem to use other
1717             // values (0x04 and 0x06 have both been observed) so at this point
1718             // we say everything that isn't a video pid is audio then at the end
1719             // of hb_stream_title_scan we'll figure out which are really audio
1720             // by looking at the PES headers.
1721             i = stream->ts_number_audio_pids;
1722             if (i < kMaxNumberAudioPIDS)
1723             {
1724                 stream->ts_audio_pids[i] = elementary_PID;
1725                 stream->ts_stream_type[1 + i] = stream_type;
1726                 if (ES_info_length > 0)
1727                 {
1728                     decode_element_descriptors(stream, i, ES_info_buf,
1729                                                ES_info_length);
1730                 }
1731                 ++stream->ts_number_audio_pids;
1732             }
1733         }
1734
1735         cur_pos += 5 /* stream header */ + ES_info_length;
1736
1737         free(ES_info_buf);
1738
1739         if (cur_pos >= section_length - 4 /* stop before the CRC */)
1740         done_reading_stream_types = 1;
1741     }
1742
1743         free(descriptor_buf);
1744         return 1;
1745 }
1746
1747 static int build_program_map(const uint8_t *buf, hb_stream_t *stream)
1748 {
1749     // Get adaption header info
1750     int adapt_len = 0;
1751     int adaption = (buf[3] & 0x30) >> 4;
1752     if (adaption == 0)
1753             return 0;
1754     else if (adaption == 0x2)
1755             adapt_len = 184;
1756     else if (adaption == 0x3)
1757             adapt_len = buf[4] + 1;
1758     if (adapt_len > 184)
1759             return 0;
1760
1761     // Get payload start indicator
1762     int start;
1763     start = (buf[1] & 0x40) != 0;
1764
1765     // Get pointer length - only valid in packets with a start flag
1766     int pointer_len = 0;
1767
1768         if (start)
1769         {
1770                 pointer_len = buf[4 + adapt_len] + 1;
1771                 stream->pmt_info.tablepos = 0;
1772         }
1773         // Get Continuity Counter
1774         int continuity_counter = buf[3] & 0x0f;
1775         if (!start && (stream->pmt_info.current_continuity_counter + 1 != continuity_counter))
1776         {
1777                 hb_log("build_program_map - Continuity Counter %d out of sequence - expected %d", continuity_counter, stream->pmt_info.current_continuity_counter+1);
1778                 return 0;
1779         }
1780         stream->pmt_info.current_continuity_counter = continuity_counter;
1781         stream->pmt_info.reading |= start;
1782
1783     // Add the payload for this packet to the current buffer
1784         int amount_to_copy = 184 - adapt_len - pointer_len;
1785     if (stream->pmt_info.reading && (amount_to_copy > 0))
1786     {
1787                         stream->pmt_info.tablebuf = realloc(stream->pmt_info.tablebuf, stream->pmt_info.tablepos + amount_to_copy);
1788
1789             memcpy(stream->pmt_info.tablebuf + stream->pmt_info.tablepos, buf + 4 + adapt_len + pointer_len, amount_to_copy);
1790             stream->pmt_info.tablepos += amount_to_copy;
1791     }
1792     if (stream->pmt_info.tablepos > 3)
1793     {
1794         // We have enough to check the section length
1795         int length;
1796         length = ((stream->pmt_info.tablebuf[1] << 8) + 
1797                   stream->pmt_info.tablebuf[2]) & 0xFFF;
1798         if (stream->pmt_info.tablepos > length + 1)
1799         {
1800             // We just finished a bunch of packets - parse the program map details
1801             int decode_ok = 0;
1802             if (stream->pmt_info.tablebuf[0] == 0x02)
1803                 decode_ok = decode_program_map(stream);
1804             free(stream->pmt_info.tablebuf);
1805             stream->pmt_info.tablebuf = NULL;
1806             stream->pmt_info.tablepos = 0;
1807             stream->pmt_info.reading = 0;
1808             if (decode_ok)
1809                 return decode_ok;
1810         }
1811
1812     }
1813
1814     return 0;
1815 }
1816
1817 static int decode_PAT(const uint8_t *buf, hb_stream_t *stream)
1818 {
1819     unsigned char tablebuf[1024];
1820     unsigned int tablepos = 0;
1821
1822     int reading = 0;
1823
1824
1825     // Get adaption header info
1826     int adapt_len = 0;
1827     int adaption = (buf[3] & 0x30) >> 4;
1828     if (adaption == 0)
1829             return 0;
1830     else if (adaption == 0x2)
1831             adapt_len = 184;
1832     else if (adaption == 0x3)
1833             adapt_len = buf[4] + 1;
1834     if (adapt_len > 184)
1835             return 0;
1836
1837     // Get pointer length
1838     int pointer_len = buf[4 + adapt_len] + 1;
1839
1840     // Get payload start indicator
1841     int start;
1842     start = (buf[1] & 0x40) != 0;
1843
1844     if (start)
1845             reading = 1;
1846
1847     // Add the payload for this packet to the current buffer
1848     if (reading && (184 - adapt_len) > 0)
1849     {
1850             if (tablepos + 184 - adapt_len - pointer_len > 1024)
1851             {
1852                     hb_log("decode_PAT - Bad program section length (> 1024)");
1853                     return 0;
1854             }
1855             memcpy(tablebuf + tablepos, buf + 4 + adapt_len + pointer_len, 184 - adapt_len - pointer_len);
1856             tablepos += 184 - adapt_len - pointer_len;
1857     }
1858
1859     if (start && reading)
1860     {
1861             memcpy(tablebuf + tablepos, buf + 4 + adapt_len + 1, pointer_len - 1);
1862
1863
1864             unsigned int pos = 0;
1865             //while (pos < tablepos)
1866             {
1867                     bitbuf_t bb;
1868                     set_buf(&bb, tablebuf + pos, tablepos - pos, 0);
1869
1870                     unsigned char section_id    = get_bits(&bb, 8);
1871                     get_bits(&bb, 4);
1872                     unsigned int section_len    = get_bits(&bb, 12);
1873                     get_bits(&bb, 16); // transport_id
1874                     get_bits(&bb, 2);
1875                     get_bits(&bb, 5);  // version_num
1876                     get_bits(&bb, 1);  // current_next
1877                     get_bits(&bb, 8);  // section_num
1878                     get_bits(&bb, 8);  // last_section
1879
1880                     switch (section_id)
1881                     {
1882                       case 0x00:
1883                         {
1884                           // Program Association Section
1885                           section_len -= 5;    // Already read transport stream ID, version num, section num, and last section num
1886                           section_len -= 4;   // Ignore the CRC
1887                           int curr_pos = 0;
1888                                                   stream->ts_number_pat_entries = 0;
1889                           while ((curr_pos < section_len) && (stream->ts_number_pat_entries < kMaxNumberPMTStreams))
1890                           {
1891                             unsigned int pkt_program_num = get_bits(&bb, 16);
1892                                                         stream->pat_info[stream->ts_number_pat_entries].program_number = pkt_program_num;
1893
1894                             get_bits(&bb, 3);  // Reserved
1895                             if (pkt_program_num == 0)
1896                             {
1897                               get_bits(&bb, 13); // pkt_network_id
1898                             }
1899                             else
1900                             {
1901                               unsigned int pkt_program_map_PID = get_bits(&bb, 13);
1902                                 stream->pat_info[stream->ts_number_pat_entries].program_map_PID = pkt_program_map_PID;
1903                             }
1904                             curr_pos += 4;
1905                                                         stream->ts_number_pat_entries++;
1906                           }
1907                         }
1908                         break;
1909                       case 0xC7:
1910                             {
1911                                     break;
1912                             }
1913                       case 0xC8:
1914                             {
1915                                     break;
1916                             }
1917                     }
1918
1919                     pos += 3 + section_len;
1920             }
1921
1922             tablepos = 0;
1923     }
1924     return 1;
1925 }
1926
1927 static void hb_ts_stream_find_pids(hb_stream_t *stream)
1928 {
1929         // align to first packet
1930     align_to_next_packet(stream);
1931
1932         // Read the Transport Stream Packets (188 bytes each) looking at first for PID 0 (the PAT PID), then decode that
1933         // to find the program map PID and then decode that to get the list of audio and video PIDs
1934
1935         for (;;)
1936         {
1937         const uint8_t *buf = next_packet( stream );
1938
1939         if ( buf == NULL )
1940         {
1941                         hb_log("hb_ts_stream_find_pids - end of file");
1942                         break;
1943                 }
1944
1945                 // Get pid
1946                 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
1947
1948         if ((pid == 0x0000) && (stream->ts_number_pat_entries == 0))
1949                 {
1950                   decode_PAT(buf, stream);
1951                   continue;
1952                 }
1953
1954                 int pat_index = 0;
1955                 for (pat_index = 0; pat_index < stream->ts_number_pat_entries; pat_index++)
1956                 {
1957                         // There are some streams where the PAT table has multiple entries as if their are
1958                         // multiple programs in the same transport stream, and yet there's actually only one
1959                         // program really in the stream. This seems to be true for transport streams that
1960                         // originate in the HDHomeRun but have been output by EyeTV's export utility. What I think
1961                         // is happening is that the HDHomeRun is sending the entire transport stream as broadcast,
1962                         // but the EyeTV is only recording a single (selected) program number and not rewriting the
1963                         // PAT info on export to match what's actually on the stream.
1964                         // Until we have a way of handling multiple programs per transport stream elegantly we'll match
1965                         // on the first pat entry for which we find a matching program map PID.  The ideal solution would
1966                         // be to build a title choice popup from the PAT program number details and then select from
1967                         // their - but right now the API's not capable of that.
1968             if (stream->pat_info[pat_index].program_number != 0 &&
1969                 pid == stream->pat_info[pat_index].program_map_PID)
1970                         {
1971                           if (build_program_map(buf, stream) > 0)
1972                                 break;
1973                         }
1974                 }
1975                 // Keep going  until we have a complete set of PIDs
1976                 if ((stream->ts_number_video_pids > 0) && (stream->ts_number_audio_pids > 0))
1977                   break;
1978         }
1979
1980         hb_log("hb_ts_stream_find_pids - found the following PIDS");
1981         hb_log("    Video PIDS : ");
1982     int i;
1983         for (i=0; i < stream->ts_number_video_pids; i++)
1984         {
1985         hb_log( "      0x%x type %s (0x%x)", 
1986                 stream->ts_video_pids[i],
1987                 stream_type_name(stream->ts_stream_type[i]),
1988                 stream->ts_stream_type[i]);
1989         }
1990         hb_log("    Audio PIDS : ");
1991         for (i = 0; i < stream->ts_number_audio_pids; i++)
1992         {
1993         hb_log( "      0x%x type %s (0x%x)", 
1994                 stream->ts_audio_pids[i],
1995                 stream_type_name(stream->ts_stream_type[i+1]),
1996                 stream->ts_stream_type[i+1] );
1997         }
1998  }
1999
2000
2001 static void fwrite64( hb_stream_t *stream, void *buf, int len )
2002 {
2003     int pos;
2004
2005     pos = stream->fwrite_buf->size;
2006     if ( pos + len > stream->fwrite_buf->alloc )
2007     {
2008         int size = MAX(stream->fwrite_buf->alloc * 2, pos + len);
2009         hb_buffer_realloc(stream->fwrite_buf, size);
2010     }
2011     memcpy( &(stream->fwrite_buf->data[pos]), buf, len );
2012     stream->fwrite_buf->size += len;
2013 }
2014
2015 // convert a PES PTS or DTS to an int64
2016 static int64_t pes_timestamp( const uint8_t *pes )
2017 {
2018     int64_t ts = ( (uint64_t)(pes[0] & 0xe ) << 29 );
2019     ts |= ( pes[1] << 22 ) | ( ( pes[2] >> 1 ) << 15 ) |
2020           ( pes[3] << 7 ) | ( pes[4] >> 1 );
2021     return ts;
2022 }
2023
2024 static void generate_output_data(hb_stream_t *stream, int curstream)
2025 {
2026     hb_buffer_t *buf = stream->fwrite_buf;
2027     uint8_t *tdat = stream->ts_buf[curstream]->data;
2028
2029     buf->id = curstream;
2030
2031     // check if this packet was referenced to an older pcr and if that
2032     // pcr was significantly different than the one we're using now.
2033     // (the reason for the uint cast on the pcr difference is that the
2034     // difference is significant if it advanced by more than 200ms or if
2035     // it went backwards by any amount. The negative numbers look like huge
2036     // unsigned ints so the cast allows both conditions to be checked at once.
2037     int bufpcr = stream->ts_buf[curstream]->cur;
2038     int curpcr = stream->ts_pcr_out;
2039     if ( bufpcr && bufpcr < curpcr &&
2040          (uint64_t)(stream->ts_pcrhist[curpcr & 3] - stream->ts_pcrhist[bufpcr & 3]) > 200*90LL )
2041     {
2042         // we've sent up a new pcr but have a packet referenced to an
2043         // old pcr and the difference was enough to trigger a discontinuity
2044         // correction. smash the timestamps or we'll mess up the correction.
2045         buf->start = -1;
2046         buf->renderOffset = -1;
2047     }
2048     else
2049     {
2050         if ( stream->ts_pcr_out != stream->ts_pcr_in )
2051         {
2052             // we have a new pcr
2053             stream->ts_pcr_out = stream->ts_pcr_in;
2054             buf->stop = stream->ts_pcr;
2055             stream->ts_pcrhist[stream->ts_pcr_out & 3] = stream->ts_pcr;
2056         }
2057         else
2058         {
2059             buf->stop = -1;
2060         }
2061
2062         // put the PTS & possible DTS into 'start' & 'renderOffset' then strip
2063         // off the PES header.
2064         if ( tdat[7] & 0xc0 )
2065         {
2066             buf->start = pes_timestamp( tdat + 9 );
2067             buf->renderOffset = ( tdat[7] & 0x40 )? pes_timestamp( tdat + 14 ) :
2068                                                     buf->start;
2069         }
2070         else
2071         {
2072             buf->start = -1;
2073             buf->renderOffset = -1;
2074         }
2075     }
2076     int hlen = tdat[8] + 9;
2077
2078     fwrite64( stream,  tdat + hlen, stream->ts_pos[curstream] - hlen );
2079
2080     stream->ts_pos[curstream] = 0;
2081     stream->ts_buf[curstream]->size = 0;
2082 }
2083
2084 static int isIframe( hb_stream_t *stream, const uint8_t *buf, int adapt_len )
2085 {
2086     // For mpeg2: look for a gop start or i-frame picture start
2087     // for h.264: look for idr nal type or a slice header for an i-frame
2088     // for vc1:   ???
2089     int i;
2090     uint32_t strid = 0;
2091
2092
2093     if ( stream->ts_stream_type[0] <= 2 )
2094     {
2095         // This section of the code handles MPEG-1 and MPEG-2 video streams
2096         for (i = 13 + adapt_len; i < 188; i++)
2097         {
2098             strid = (strid << 8) | buf[i];
2099             if ( ( strid >> 8 ) == 1 )
2100             {
2101                 // we found a start code
2102                 uint8_t id = strid;
2103                 switch ( id )
2104                 {
2105                     case 0xB8: // group_start_code (GOP header)
2106                     case 0xB3: // sequence_header code
2107                         return 1;
2108
2109                     case 0x00: // picture_start_code
2110                         // picture_header, let's see if it's an I-frame
2111                         if (i<185)
2112                         {
2113                             // check if picture_coding_type == 1
2114                             if ((buf[i+2] & (0x7 << 3)) == (1 << 3))
2115                             {
2116                                 // found an I-frame picture
2117                                 return 1;
2118                             }
2119                         }
2120                         break;
2121                 }
2122             }
2123         }
2124         // didn't find an I-frame
2125         return 0;
2126     }
2127     if ( stream->ts_stream_type[0] == 0x1b )
2128     {
2129         // we have an h.264 stream 
2130         for (i = 13 + adapt_len; i < 188; i++)
2131         {
2132             strid = (strid << 8) | buf[i];
2133             if ( ( strid >> 8 ) == 1 )
2134             {
2135                 // we found a start code - remove the ref_idc from the nal type
2136                 uint8_t nal_type = strid & 0x1f;
2137                 if ( nal_type == 0x05 )
2138                     // h.264 IDR picture start
2139                     return 1;
2140
2141                 if ( stream->packetsize == 192 )
2142                 {
2143                     // m2ts files have idr frames so keep looking for one
2144                     continue;
2145                 }
2146
2147                 // h.264 in ts files (ATSC or DVB video) often seem to be
2148                 // missing IDR frames so look for at least an I
2149                 if ( nal_type == 0x01 )
2150                 {
2151                     // h.264 slice: has to be start MB 0 & type I (2, 4, 7 or 9)
2152                     uint8_t id = buf[i+1];
2153                     if ( ( id >> 4 ) == 0x0b || ( id >> 2 ) == 0x25 ||
2154                          id == 0x88 || id == 0x8a )
2155                     {
2156                         return 1;
2157                     }
2158                 }
2159             }
2160         }
2161         // didn't find an I-frame
2162         return 0;
2163     }
2164     if ( stream->ts_stream_type[0] == 0xea )
2165     {
2166         // we have an vc1 stream 
2167         for (i = 13 + adapt_len; i < 188; i++)
2168         {
2169             strid = (strid << 8) | buf[i];
2170             if ( strid == 0x10f )
2171             {
2172                 // the ffmpeg vc1 decoder requires a seq hdr code in the first
2173                 // frame.
2174                 return 1;
2175             }
2176         }
2177         // didn't find an I-frame
2178         return 0;
2179     }
2180
2181     // we don't understand the stream type so just say "yes" otherwise
2182     // we'll discard all the video.
2183     return 1;
2184 }
2185
2186 static void hb_ts_stream_append_pkt(hb_stream_t *stream, int idx, const uint8_t *buf, int len)
2187 {
2188     if (stream->ts_pos[idx] + len > stream->ts_buf[idx]->alloc)
2189     {
2190         int size;
2191
2192         size = MAX(stream->ts_buf[idx]->alloc * 2, stream->ts_pos[idx] + len);
2193         hb_buffer_realloc(stream->ts_buf[idx], size);
2194     }
2195     memcpy(stream->ts_buf[idx]->data + stream->ts_pos[idx], buf, len);
2196     stream->ts_pos[idx] += len;
2197     stream->ts_buf[idx]->size += len;
2198 }
2199
2200 /***********************************************************************
2201  * hb_ts_stream_decode
2202  ***********************************************************************
2203  *
2204  **********************************************************************/
2205 static int hb_ts_stream_decode( hb_stream_t *stream, hb_buffer_t *obuf )
2206 {
2207     /*
2208      * stash the output buffer pointer in our stream so we don't have to
2209      * pass it & its original value to everything we call.
2210      */
2211     obuf->size = 0;
2212     stream->fwrite_buf = obuf;
2213
2214         // spin until we get a packet of data from some stream or hit eof
2215         while ( 1 )
2216         {
2217         int curstream;
2218
2219         const uint8_t *buf = next_packet(stream);
2220         if ( buf == NULL )
2221         {
2222             // end of file - we didn't finish filling our ps write buffer
2223             // so just discard the remainder (the partial buffer is useless)
2224             hb_log("hb_ts_stream_decode - eof");
2225             return 0;
2226                 }
2227
2228         /* This next section validates the packet */
2229
2230                 // Get pid and use it to find stream state.
2231                 int pid = ((buf[1] & 0x1F) << 8) | buf[2];
2232         if ( ( curstream = index_of_pid( pid, stream ) ) < 0 )
2233             continue;
2234
2235                 // Get error
2236                 int errorbit = (buf[1] & 0x80) != 0;
2237                 if (errorbit)
2238                 {
2239                         ts_err( stream, curstream,  "packet error bit set");
2240                         continue;
2241                 }
2242
2243                 // Get adaption header info
2244                 int adaption = (buf[3] & 0x30) >> 4;
2245                 int adapt_len = 0;
2246                 if (adaption == 0)
2247                 {
2248                         ts_err( stream, curstream,  "adaptation code 0");
2249                         continue;
2250                 }
2251                 else if (adaption == 0x2)
2252                         adapt_len = 184;
2253                 else if (adaption == 0x3)
2254                 {
2255                         adapt_len = buf[4] + 1;
2256                         if (adapt_len > 184)
2257                         {
2258                                 ts_err( stream, curstream,  "invalid adapt len %d", adapt_len);
2259                 continue;
2260                         }
2261                 }
2262
2263         if ( adapt_len > 0 )
2264         {
2265             if ( buf[5] & 0x40 )
2266             {
2267                 // found a random access point
2268             }
2269             // if there's an adaptation header & PCR_flag is set
2270             // get the PCR (Program Clock Reference)
2271             if ( adapt_len > 7 && ( buf[5] & 0x10 ) != 0 )
2272             {
2273                 stream->ts_pcr = ( (uint64_t)buf[6] << (33 - 8) ) |
2274                                  ( (uint64_t)buf[7] << (33 - 16) ) |
2275                                  ( (uint64_t)buf[8] << (33 - 24) ) |
2276                                  ( (uint64_t)buf[9] << (33 - 32) ) |
2277                                  ( buf[10] >> 7 );
2278                 ++stream->ts_pcr_in;
2279                 stream->ts_found_pcr = 1;
2280             }
2281         }
2282
2283         // If we don't have a pcr yet, the right thing to do here would
2284         // be a 'continue' so we don't process anything until we have a
2285         // clock reference. Unfortunately the HD Home Run appears to null
2286         // out the pcr field of some streams so we keep going & substitute
2287         // the video stream dts for the pcr when there's no pcr.
2288
2289                 // Get continuity
2290         // Continuity only increments for adaption values of 0x3 or 0x01
2291         // and is not checked for start packets.
2292
2293                 int start = (buf[1] & 0x40) != 0;
2294
2295         if ( (adaption & 0x01) != 0 )
2296                 {
2297             int continuity = (buf[3] & 0xF);
2298             if ( continuity == stream->ts_streamcont[curstream] )
2299             {
2300                 // we got a duplicate packet (usually used to introduce
2301                 // a PCR when one is needed). The only thing that can
2302                 // change in the dup is the PCR which we grabbed above
2303                 // so ignore the rest.
2304                 continue;
2305             }
2306             if ( !start && (stream->ts_streamcont[curstream] != -1) &&
2307                  !stream->ts_skipbad[curstream] &&
2308                  (continuity != ( (stream->ts_streamcont[curstream] + 1) & 0xf ) ) )
2309                         {
2310                                 ts_err( stream, curstream,  "continuity error: got %d expected %d",
2311                         (int)continuity,
2312                         (stream->ts_streamcont[curstream] + 1) & 0xf );
2313                 stream->ts_streamcont[curstream] = continuity;
2314                                 continue;
2315                         }
2316                         stream->ts_streamcont[curstream] = continuity;
2317                 }
2318
2319         /* If we get here the packet is valid - process its data */
2320
2321         if ( start )
2322         {
2323             // Found a random access point (now we can start a frame/audio packet..)
2324
2325             if ( stream->need_keyframe )
2326             {
2327                 // we're looking for the first video frame because we're
2328                 // doing random access during 'scan'
2329                 if (curstream != 0 || !isIframe( stream, buf, adapt_len ) )
2330                 {
2331                     // not the video stream or didn't find an I frame
2332                     continue;
2333                 }
2334                 stream->need_keyframe = 0;
2335             }
2336
2337                         // If we were skipping a bad packet, start fresh on this new PES packet..
2338                         if (stream->ts_skipbad[curstream] == 1)
2339                         {
2340                                 stream->ts_skipbad[curstream] = 0;
2341                         }
2342
2343                         if ( curstream == 0 )
2344             {
2345                 ++stream->frames;
2346
2347                 // if we don't have a pcr yet use the dts from this frame
2348                 if ( !stream->ts_found_pcr )
2349                 {
2350                     // PES must begin with an mpeg start code & contain
2351                     // a DTS or PTS.
2352                     const uint8_t *pes = buf + adapt_len + 4;
2353                     if ( pes[0] != 0x00 || pes[1] != 0x00 || pes[2] != 0x01 ||
2354                          ( pes[7] >> 6 ) == 0 )
2355                     {
2356                         continue;
2357                     }
2358                     // if we have a dts use it otherwise use the pts
2359                     stream->ts_pcr = pes_timestamp( pes + ( pes[7] & 0x40? 14 : 9 ) );
2360                     ++stream->ts_pcr_in;
2361                 }
2362             }
2363
2364             // if this is a multiplexed stream make sure this is the
2365             // substream we want.
2366             if ( stream->ts_multiplexed[curstream] )
2367             {
2368                 // PES must begin with an mpeg start code & contain
2369                 // a DTS or PTS.
2370                 const uint8_t *pes = buf + adapt_len + 4;
2371                 if ( pes[0] != 0x00 || pes[1] != 0x00 || pes[2] != 0x01 ||
2372                      pes[3] != 0xfd )
2373                 {
2374                     stream->ts_skipbad[curstream] = 1;
2375                     continue;
2376                 }
2377                 // the last byte of the header is the extension id. see if
2378                 // it's the one we want.
2379                 if ( pes[pes[8]+8] != stream->ts_multiplexed[curstream] )
2380                 {
2381                     stream->ts_skipbad[curstream] = 1;
2382                     continue;
2383                 }
2384             }
2385
2386             // If we have some data already on this stream, turn it into
2387             // a program stream packet. Then add the payload for this
2388             // packet to the current pid's buffer.
2389             if ( stream->ts_pos[curstream] )
2390             {
2391                 // we have to ship the old packet before updating the pcr
2392                 // since the packet we've been accumulating is referenced
2393                 // to the old pcr.
2394                 generate_output_data(stream, curstream);
2395
2396                 // remember the pcr that was in effect when we started
2397                 // this packet.
2398                 stream->ts_buf[curstream]->cur = stream->ts_pcr_in;
2399                 hb_ts_stream_append_pkt(stream, curstream, buf + 4 + adapt_len,
2400                                         184 - adapt_len);
2401                 return 1;
2402             }
2403             // remember the pcr that was in effect when we started this packet.
2404             stream->ts_buf[curstream]->cur = stream->ts_pcr_in;
2405         }
2406
2407                 // Add the payload for this packet to the current buffer
2408                 if (!stream->ts_skipbad[curstream] && (184 - adapt_len) > 0)
2409                 {
2410             hb_ts_stream_append_pkt(stream, curstream, buf + 4 + adapt_len,
2411                                     184 - adapt_len);
2412             // see if we've hit the end of this PES packet
2413             const uint8_t *pes = stream->ts_buf[curstream]->data;
2414             int len = ( pes[4] << 8 ) + pes[5] + 6;
2415             if ( len > 6 && stream->ts_pos[curstream] == len &&
2416                  pes[0] == 0x00 && pes[1] == 0x00 && pes[2] == 0x01 )
2417             {
2418                 generate_output_data(stream, curstream);
2419                 return 1;
2420             }
2421                 }
2422         }
2423 }
2424
2425 static void hb_ts_stream_reset(hb_stream_t *stream)
2426 {
2427         int i;
2428
2429         for (i=0; i < kMaxNumberDecodeStreams; i++)
2430         {
2431                 stream->ts_pos[i] = 0;
2432                 stream->ts_skipbad[i] = 1;
2433                 stream->ts_streamcont[i] = -1;
2434         }
2435
2436     stream->need_keyframe = 0;
2437
2438     stream->ts_found_pcr = 0;
2439     stream->ts_pcr_out = 0;
2440     stream->ts_pcr_in = 0;
2441     stream->ts_pcr = 0;
2442
2443     stream->frames = 0;
2444     stream->errors = 0;
2445     stream->last_error_frame = -10000;
2446     stream->last_error_count = 0;
2447
2448     align_to_next_packet(stream);
2449 }
2450
2451 // ------------------------------------------------------------------
2452 // Support for reading media files via the ffmpeg libraries.
2453
2454 static void ffmpeg_add_codec( hb_stream_t *stream, int stream_index )
2455 {
2456     // add a codec to the context here so it will be there when we
2457     // read the first packet.
2458     AVCodecContext *context = stream->ffmpeg_ic->streams[stream_index]->codec;
2459     context->workaround_bugs = FF_BUG_AUTODETECT;
2460     context->error_recognition = 1;
2461     context->error_concealment = FF_EC_GUESS_MVS|FF_EC_DEBLOCK;
2462     AVCodec *codec = avcodec_find_decoder( context->codec_id );
2463     avcodec_open( context, codec );
2464 }
2465
2466 // The ffmpeg stream reader / parser shares a lot of state with the 
2467 // decoder via a codec context kept in the AVStream of the reader's
2468 // AVFormatContext. Since decoding is done in a different thread we
2469 // have to somehow pass this codec context to the decoder and we have
2470 // to do it before the first packet is read (so we can't put the info
2471 // in the buf we'll send downstream). Decoders don't have any way to
2472 // get to the stream directly (they're not passed the title or job
2473 // pointers during a scan) so this is a back door for the decoder to
2474 // get the codec context. We just stick the stream pointer in the next
2475 // slot an array of pointers maintained as a circular list then return
2476 // the index into the list combined with the ffmpeg stream index as the
2477 // codec_param that will be passed to the decoder init routine. We make
2478 // the list 'big' (enough for 1024 simultaneously open ffmpeg streams)
2479 // so that we don't have to do a complicated allocator or worry about
2480 // deleting entries on close. 
2481 //
2482 // Entries can only be added to this list during a scan and are never
2483 // deleted so the list access doesn't require locking.
2484 static hb_stream_t **ffmpeg_streams;    // circular list of stream pointers
2485 static int ffmpeg_stream_cur;           // where we put the last stream pointer
2486 #define ffmpeg_sl_bits (10)             // log2 stream list size (in entries)
2487 #define ffmpeg_sl_size (1 << ffmpeg_sl_bits)
2488
2489 // add a stream to the list & return the appropriate codec_param to access it
2490 static int ffmpeg_codec_param( hb_stream_t *stream, int stream_index )
2491 {
2492     if ( !ffmpeg_streams )
2493     {
2494         ffmpeg_streams = calloc( ffmpeg_sl_size, sizeof(stream) );
2495     }
2496
2497     // the title scan adds all the ffmpeg media streams at once so we
2498     // only add a new entry to our stream list if the stream is different
2499     // than last time.
2500     int slot = ffmpeg_stream_cur;
2501     if ( ffmpeg_streams[slot] != stream )
2502     {
2503         // new stream - put it in the next slot of the stream list
2504         slot = ++ffmpeg_stream_cur & (ffmpeg_sl_size - 1);
2505         ffmpeg_streams[slot] = stream;
2506     }
2507
2508     ffmpeg_add_codec( stream, stream_index );
2509
2510     return ( stream_index << ffmpeg_sl_bits ) | slot;
2511 }
2512
2513 // we're about to open 'title' to convert it - remap the stream associated
2514 // with the video & audio codec params of the title to refer to 'stream'
2515 // (the original scan stream was closed and no longer exists).
2516 static void ffmpeg_remap_stream( hb_stream_t *stream, hb_title_t *title )
2517 {
2518     // tell ffmpeg we want a pts on every frame it returns
2519     stream->ffmpeg_ic->flags |= AVFMT_FLAG_GENPTS;
2520
2521     // all the video & audio came from the same stream so remapping
2522     // the video's stream slot takes care of everything.
2523     int slot = title->video_codec_param & (ffmpeg_sl_size - 1);
2524     ffmpeg_streams[slot] = stream;
2525
2526     // add codecs for all the streams used by the title
2527     ffmpeg_add_codec( stream, title->video_codec_param >> ffmpeg_sl_bits );
2528
2529     int i;
2530     hb_audio_t *audio;
2531     for ( i = 0; ( audio = hb_list_item( title->list_audio, i ) ); ++i )
2532     {
2533         if ( audio->config.in.codec == HB_ACODEC_FFMPEG )
2534         {
2535             ffmpeg_add_codec( stream,
2536                               audio->config.in.codec_param >> ffmpeg_sl_bits );
2537         }
2538     }
2539 }
2540
2541 void *hb_ffmpeg_context( int codec_param )
2542 {
2543     int slot = codec_param & (ffmpeg_sl_size - 1);
2544     int stream_index = codec_param >> ffmpeg_sl_bits;
2545     return ffmpeg_streams[slot]->ffmpeg_ic->streams[stream_index]->codec;
2546 }
2547
2548 void *hb_ffmpeg_avstream( int codec_param )
2549 {
2550     int slot = codec_param & (ffmpeg_sl_size - 1);
2551     int stream_index = codec_param >> ffmpeg_sl_bits;
2552     return ffmpeg_streams[slot]->ffmpeg_ic->streams[stream_index];
2553 }
2554
2555 static AVFormatContext *ffmpeg_deferred_close;
2556
2557 static int ffmpeg_open( hb_stream_t *stream, hb_title_t *title )
2558 {
2559     if ( ffmpeg_deferred_close )
2560     {
2561         av_close_input_file( ffmpeg_deferred_close );
2562         ffmpeg_deferred_close = NULL;
2563     }
2564     AVFormatContext *ic;
2565
2566     av_log_set_level( AV_LOG_ERROR );
2567     if ( av_open_input_file( &ic, stream->path, NULL, 0, NULL ) < 0 )
2568     {
2569         return 0;
2570     }
2571     if ( av_find_stream_info( ic ) < 0 )
2572         goto fail;
2573
2574     stream->ffmpeg_ic = ic;
2575     stream->hb_stream_type = ffmpeg;
2576     stream->ffmpeg_pkt = malloc(sizeof(*stream->ffmpeg_pkt));
2577     av_init_packet( stream->ffmpeg_pkt );
2578
2579     if ( title )
2580     {
2581         // we're opening for read. scan passed out codec params that
2582         // indexed its stream so we need to remap them so they point
2583         // to this stream.
2584         ffmpeg_remap_stream( stream, title );
2585         av_log_set_level( AV_LOG_ERROR );
2586     }
2587     else
2588     {
2589         // we're opening for scan. let ffmpeg put some info into the
2590         // log about what we've got.
2591         av_log_set_level( AV_LOG_INFO );
2592         dump_format( ic, 0, stream->path, 0 );
2593         av_log_set_level( AV_LOG_ERROR );
2594
2595         // accept this file if it has at least one video stream we can decode
2596         int i;
2597         for (i = 0; i < ic->nb_streams; ++i )
2598         {
2599             if ( ic->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO )
2600             {
2601                 break;
2602             }
2603         }
2604         if ( i >= ic->nb_streams )
2605             goto fail;
2606     }
2607     return 1;
2608
2609   fail:
2610     av_close_input_file( ic );
2611     return 0;
2612 }
2613
2614 static void ffmpeg_close( hb_stream_t *d )
2615 {
2616     // XXX since we're sharing the CodecContext with the downstream
2617     // decoder proc we can't close the stream. We need to reference count
2618     // this so we can close it when both are done with their instance but
2619     // for now just defer the close until the next stream open or close.
2620     if ( ffmpeg_deferred_close )
2621     {
2622         av_close_input_file( ffmpeg_deferred_close );
2623     }
2624     ffmpeg_deferred_close = d->ffmpeg_ic;
2625     if ( d->ffmpeg_pkt != NULL )
2626     {
2627         free( d->ffmpeg_pkt );
2628         d->ffmpeg_pkt = NULL;
2629     }
2630 }
2631
2632 static void add_ffmpeg_audio( hb_title_t *title, hb_stream_t *stream, int id )
2633 {
2634     AVStream *st = stream->ffmpeg_ic->streams[id];
2635     AVCodecContext *codec = st->codec;
2636
2637     // scan will ignore any audio without a bitrate. Since we've already
2638     // typed the audio in order to determine its codec we set up the audio
2639     // paramters here.
2640     if ( codec->bit_rate || codec->sample_rate )
2641     {
2642         static const int chan2layout[] = {
2643             HB_INPUT_CH_LAYOUT_MONO,  // We should allow no audio really.
2644             HB_INPUT_CH_LAYOUT_MONO,   
2645             HB_INPUT_CH_LAYOUT_STEREO,
2646             HB_INPUT_CH_LAYOUT_2F1R,   
2647             HB_INPUT_CH_LAYOUT_2F2R,
2648             HB_INPUT_CH_LAYOUT_3F2R,   
2649             HB_INPUT_CH_LAYOUT_4F2R,
2650             HB_INPUT_CH_LAYOUT_STEREO, 
2651             HB_INPUT_CH_LAYOUT_STEREO,
2652         };
2653
2654         hb_audio_t *audio = calloc( 1, sizeof(*audio) );;
2655
2656         audio->id = id;
2657         if ( codec->codec_id == CODEC_ID_AC3 )
2658         {
2659             audio->config.in.codec = HB_ACODEC_AC3;
2660         }
2661         else if ( codec->codec_id == CODEC_ID_DTS )
2662         {
2663             audio->config.in.codec = HB_ACODEC_DCA;
2664         }
2665         else
2666         {
2667             audio->config.in.codec = HB_ACODEC_FFMPEG;
2668             audio->config.in.codec_param = ffmpeg_codec_param( stream, id );
2669
2670             audio->config.in.bitrate = codec->bit_rate? codec->bit_rate : 1;
2671             audio->config.in.samplerate = codec->sample_rate;
2672             audio->config.in.channel_layout = chan2layout[codec->channels & 7];
2673         }
2674
2675         set_audio_description( audio, lang_for_code2( st->language ) );
2676
2677         hb_list_add( title->list_audio, audio );
2678     }
2679 }
2680
2681 static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream )
2682 {
2683     AVFormatContext *ic = stream->ffmpeg_ic;
2684
2685     // 'Barebones Title'
2686     hb_title_t *title = hb_title_init( stream->path, 0 );
2687     title->index = 1;
2688
2689         // Copy part of the stream path to the title name
2690         char *sep = strrchr(stream->path, '/');
2691         if (sep)
2692                 strcpy(title->name, sep+1);
2693         char *dot_term = strrchr(title->name, '.');
2694         if (dot_term)
2695                 *dot_term = '\0';
2696
2697     uint64_t dur = ic->duration * 90000 / AV_TIME_BASE;
2698     title->duration = dur;
2699     dur /= 90000;
2700     title->hours    = dur / 3600;
2701     title->minutes  = ( dur % 3600 ) / 60;
2702     title->seconds  = dur % 60;
2703
2704     // set the title to decode the first video stream in the file
2705     title->demuxer = HB_NULL_DEMUXER;
2706     title->video_codec = 0;
2707     int i;
2708     for (i = 0; i < ic->nb_streams; ++i )
2709     {
2710         if ( ic->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO &&
2711              avcodec_find_decoder( ic->streams[i]->codec->codec_id ) &&
2712              title->video_codec == 0 )
2713         {
2714             title->video_id = i;
2715             stream->ffmpeg_video_id = i;
2716
2717             // We have to use the 'internal' avcodec decoder because
2718             // it needs to share the codec context from this video
2719             // stream. The parser internal to av_read_frame
2720             // passes a bunch of state info to the decoder via the context.
2721             title->video_codec = WORK_DECAVCODECVI;
2722             title->video_codec_param = ffmpeg_codec_param( stream, i );
2723         }
2724         else if ( ic->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO &&
2725                   avcodec_find_decoder( ic->streams[i]->codec->codec_id ) )
2726         {
2727             add_ffmpeg_audio( title, stream, i );
2728         }
2729     }
2730
2731     title->container_name = strdup( ic->iformat->name );
2732     title->data_rate = ic->bit_rate;
2733
2734     hb_deep_log( 2, "Found ffmpeg %d chapters, container=%s", ic->nb_chapters, ic->iformat->name);
2735
2736     /*
2737      * Fill the metadata.
2738      */
2739     decmetadata( title );
2740
2741     if( hb_list_count( title->list_chapter ) == 0 )
2742     {
2743         // Need at least one chapter
2744         hb_chapter_t * chapter;
2745         chapter = calloc( sizeof( hb_chapter_t ), 1 );
2746         chapter->index = 1;
2747         chapter->duration = title->duration;
2748         chapter->hours = title->hours;
2749         chapter->minutes = title->minutes;
2750         chapter->seconds = title->seconds;
2751         hb_list_add( title->list_chapter, chapter );
2752     }
2753
2754     return title;
2755 }
2756
2757 static int64_t av_to_hb_pts( int64_t pts, double conv_factor )
2758 {
2759     if ( pts == AV_NOPTS_VALUE )
2760         return -1;
2761     return (int64_t)( (double)pts * conv_factor );
2762 }
2763
2764 static int ffmpeg_read( hb_stream_t *stream, hb_buffer_t *buf )
2765 {
2766     int err;
2767   again:
2768     if ( ( err = av_read_frame( stream->ffmpeg_ic, stream->ffmpeg_pkt )) < 0 )
2769     {
2770         // XXX the following conditional is to handle avi files that
2771         // use M$ 'packed b-frames' and occasionally have negative
2772         // sizes for the null frames these require.
2773         if ( err != AVERROR_NOMEM || stream->ffmpeg_pkt->size >= 0 )
2774             // eof
2775             return 0;
2776     }
2777     if ( stream->ffmpeg_pkt->size <= 0 )
2778     {
2779         // M$ "invalid and inefficient" packed b-frames require 'null frames'
2780         // following them to preserve the timing (since the packing puts two
2781         // or more frames in what looks like one avi frame). The contents and
2782         // size of these null frames are ignored by the ff_h263_decode_frame
2783         // as long as they're < 20 bytes. We need a positive size so we use
2784         // one byte if we're given a zero or negative size. We don't know
2785         // if the pkt data points anywhere reasonable so we just stick a
2786         // byte of zero in our outbound buf.
2787         buf->size = 1;
2788         *buf->data = 0;
2789     }
2790     else
2791     {
2792         if ( stream->ffmpeg_pkt->size > buf->alloc )
2793         {
2794             // sometimes we get absurd sizes from ffmpeg
2795             if ( stream->ffmpeg_pkt->size >= (1 << 25) )
2796             {
2797                 hb_log( "ffmpeg_read: pkt too big: %d bytes", stream->ffmpeg_pkt->size );
2798                 av_free_packet( stream->ffmpeg_pkt );
2799                 return ffmpeg_read( stream, buf );
2800             }
2801             // need to expand buffer
2802             hb_buffer_realloc( buf, stream->ffmpeg_pkt->size );
2803         }
2804         memcpy( buf->data, stream->ffmpeg_pkt->data, stream->ffmpeg_pkt->size );
2805         buf->size = stream->ffmpeg_pkt->size;
2806     }
2807     buf->id = stream->ffmpeg_pkt->stream_index;
2808     if ( buf->id == stream->ffmpeg_video_id )
2809     {
2810         if ( stream->need_keyframe &&
2811              stream->ffmpeg_ic->streams[stream->ffmpeg_video_id]->codec->codec_id == 
2812                CODEC_ID_VC1 )
2813         {
2814             // XXX the VC1 codec doesn't seek to key frames so to get previews
2815             // we do it ourselves here. The decoder gets messed up if it
2816             // doesn't get a SEQ header first so we consider that to be a key frame.
2817             uint8_t *pkt = stream->ffmpeg_pkt->data;
2818             if ( pkt[0] || pkt[1] || pkt[2] != 1 || pkt[3] != 0x0f )
2819             {
2820                 av_free_packet( stream->ffmpeg_pkt );
2821                 goto again;
2822             }
2823             stream->need_keyframe = 0;
2824         }
2825         ++stream->frames;
2826     }
2827
2828     // if we haven't done it already, compute a conversion factor to go
2829     // from the ffmpeg timebase for the stream to HB's 90KHz timebase.
2830     double tsconv = stream->ffmpeg_tsconv[stream->ffmpeg_pkt->stream_index];
2831     if ( ! tsconv )
2832     {
2833         AVStream *s = stream->ffmpeg_ic->streams[stream->ffmpeg_pkt->stream_index];
2834         tsconv = 90000. * (double)s->time_base.num / (double)s->time_base.den;
2835         stream->ffmpeg_tsconv[stream->ffmpeg_pkt->stream_index] = tsconv;
2836     }
2837
2838     buf->start = av_to_hb_pts( stream->ffmpeg_pkt->pts, tsconv );
2839     buf->renderOffset = av_to_hb_pts( stream->ffmpeg_pkt->dts, tsconv );
2840     if ( buf->renderOffset >= 0 && buf->start == -1 )
2841     {
2842         buf->start = buf->renderOffset;
2843     }
2844
2845     /*
2846      * Check to see whether this video buffer is on a chapter
2847      * boundary, if so mark it as such in the buffer. The chapters for
2848      * a stream have a simple duration for each chapter. So we keep
2849      * track of what chapter we are in currently, and when it is due
2850      * to end.
2851      */
2852     hb_deep_log( 3, "title=0x%x, job=0x%x, chapter_markers=%d, time=%lld, chapter=%d, end_chapter=%lld",
2853                  stream->title, 
2854                  stream->title ? (stream->title->job ? stream->title->job : 0x0) : 0x0, 
2855                  stream->title ? (stream->title->job ? stream->title->job->chapter_markers : 2) : 0x0,  
2856                  buf->start, stream->chapter, stream->chapter_end);
2857
2858     if( stream->title &&
2859         stream->title->job &&
2860         stream->title->job->chapter_markers &&
2861         buf->id == stream->ffmpeg_video_id &&
2862         buf->start >= stream->chapter_end )
2863     {
2864         hb_chapter_t *chapter = NULL;
2865
2866         /*
2867          * Store when this chapter ends using HB time.
2868          */
2869         chapter = hb_list_item( stream->title->list_chapter,
2870                                 stream->chapter );
2871
2872         if( chapter )
2873         {
2874             if( stream->chapter != 0 )
2875             {
2876                 buf->new_chap = stream->chapter + 2;
2877             }
2878
2879             hb_deep_log( 2, "Starting chapter %i at %lld", buf->new_chap, buf->start);
2880             stream->chapter_end += chapter->duration;
2881             stream->chapter++;
2882             hb_deep_log( 2, "Looking for chapter %i at %lld", stream->chapter+1, stream->chapter_end );
2883         } else {
2884             /*
2885              * Must have run out of chapters, stop looking.
2886              */
2887             stream->chapter_end = -1;
2888         }
2889     } else {
2890         buf->new_chap = 0;
2891     }
2892     av_free_packet( stream->ffmpeg_pkt );
2893     return 1;
2894 }
2895
2896 static int ffmpeg_seek( hb_stream_t *stream, float frac )
2897 {
2898     AVFormatContext *ic = stream->ffmpeg_ic;
2899     int64_t pos = (double)ic->duration * (double)frac;
2900     if ( pos )
2901     {
2902         av_seek_frame( ic, -1, pos, 0 );
2903         stream->need_keyframe = 1;
2904     }
2905     else
2906     {
2907         av_seek_frame( ic, -1, pos, AVSEEK_FLAG_BACKWARD );
2908     }
2909     return 1;
2910 }