OSDN Git Service

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