OSDN Git Service

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