OSDN Git Service

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