+hb_stream_t * hb_bd_stream_open( hb_title_t *title )
+{
+ int ii;
+
+ hb_stream_t *d = calloc( sizeof( hb_stream_t ), 1 );
+ if ( d == NULL )
+ {
+ hb_log( "hb_bd_stream_open: can't allocate space for stream state" );
+ return NULL;
+ }
+
+ for (ii = 0; ii < kMaxNumberDecodeStreams; ii++)
+ {
+ d->ts_streamcont[ii] = -1;
+ d->ts_pids[ii] = -1;
+ }
+
+ d->file_handle = NULL;
+ d->title = title;
+ d->path = NULL;
+ d->ts_packet = NULL;
+
+ d->ts_number_pids = 0;
+ d->ts_pids[0] = title->video_id;
+ d->ts_stream_type[0] = title->video_stream_type;
+ d->ts_stream_kind[0] = V;
+ d->ts_number_pids++;
+
+ hb_audio_t * audio;
+ for ( ii = 0; ( audio = hb_list_item( title->list_audio, ii ) ); ++ii )
+ {
+ d->ts_pids[d->ts_number_pids] = audio->id;
+ d->ts_stream_type[d->ts_number_pids] = audio->config.in.stream_type;
+ d->ts_stream_kind[d->ts_number_pids] = A;
+
+ if ( d->ts_stream_type[d->ts_number_pids] == 0x83 &&
+ title->reg_desc == STR4_TO_UINT32("HDMV") )
+ {
+ // This is an interleaved TrueHD/AC-3 stream and the esid of
+ // the AC-3 is 0x76
+ d->ts_multiplexed[d->ts_number_pids] = 0x76;
+ d->ts_stream_type[d->ts_number_pids] = 0x81;
+ }
+ if ( d->ts_stream_type[d->ts_number_pids] == 0x86 &&
+ title->reg_desc == STR4_TO_UINT32("HDMV") )
+ {
+ // This is an interleaved DTS-HD/DTS stream and the esid of
+ // the DTS is 0x71
+ d->ts_multiplexed[d->ts_number_pids] = 0x71;
+ d->ts_stream_type[d->ts_number_pids] = 0x82;
+ }
+
+ d->ts_number_pids++;
+ }
+
+ d->ts_flags = TS_HAS_RAP;
+ // When scanning, title->job == NULL. We don't need to wait for
+ // a PCR when scanning. In fact, it trips us up on the first
+ // preview of every title since we would have to read quite a
+ // lot of data before finding the PCR.
+ if (title->pcr_pid != 0xFFFF && title->job)
+ {
+ if ( index_of_pid( d, title->pcr_pid ) < 0 )
+ {
+ // BD PCR PID is specified to always be 0x1001
+ d->ts_pids[d->ts_number_pids] = 0x1001;
+ d->ts_stream_kind[d->ts_number_pids] = P;
+ d->ts_number_pids++;
+ }
+ }
+
+ d->packetsize = 192;
+ d->hb_stream_type = transport;
+
+ for ( ii = 0; ii < d->ts_number_pids; ii++ )
+ {
+ d->ts_buf[ii] = hb_buffer_init(d->packetsize);
+ d->ts_buf[ii]->size = 0;
+ }
+
+ return d;
+}
+