OSDN Git Service

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