OSDN Git Service

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