OSDN Git Service

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