OSDN Git Service

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