3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
9 #include "a52dec/a52.h"
13 #define min(a, b) a < b ? a : b
15 typedef enum { hb_stream_type_unknown = 0, hb_stream_type_transport, hb_stream_type_program } hb_stream_type_t;
17 #define kMaxNumberDecodeStreams 8
18 #define kMaxNumberVideoPIDS 16
19 #define kMaxNumberAudioPIDS 16
20 //#define kVideoStream 0
21 //#define kAudioStream 1
22 #define kNumDecodeBuffers 2
23 #define kMaxNumberPMTStreams 32
25 #define CLOCKRATE ((int64_t)27000000) // MPEG System clock rate
26 #define STREAMRATE ((int64_t)2401587) // Original HD stream rate 19.2 Mbps
27 #define DEMUX (((int)STREAMRATE * 8) / 50)// Demux value for HD content STREAMRATE / 50
33 hb_stream_type_t stream_type;
35 int ps_current_write_buffer_index;
36 int ps_current_read_buffer_index;
44 } ps_decode_buffer[kNumDecodeBuffers];
51 } a52_info[kMaxNumberAudioPIDS];
53 int ts_video_pids[kMaxNumberVideoPIDS];
54 int ts_audio_pids[kMaxNumberAudioPIDS];
56 int ts_number_video_pids;
57 int ts_number_audio_pids;
59 unsigned char* ts_packetbuf[kMaxNumberDecodeStreams];
60 int ts_packetpos[kMaxNumberDecodeStreams];
61 // int ts_bufpackets[kMaxNumberDecodeStreams];
62 int ts_foundfirst[kMaxNumberDecodeStreams];
63 int ts_skipbad[kMaxNumberDecodeStreams];
64 int ts_streamcont[kMaxNumberDecodeStreams];
65 int ts_streamid[kMaxNumberDecodeStreams];
66 int ts_audio_stream_type[kMaxNumberAudioPIDS];
70 unsigned short program_number;
71 unsigned short program_map_PID;
72 } pat_info[kMaxNumberPMTStreams];
73 int ts_number_pat_entries;
78 unsigned char *tablebuf;
79 unsigned int tablepos;
80 unsigned char current_continuity_counter;
85 int program_info_length;
86 unsigned char *progam_info_descriptor_data;
89 unsigned char stream_type;
90 unsigned short elementary_PID;
91 unsigned short ES_info_length;
92 unsigned char *es_info_descriptor_data;
93 } pmt_stream_info[kMaxNumberPMTStreams];
97 /***********************************************************************
99 **********************************************************************/
100 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle);
101 static void hb_ts_stream_init(hb_stream_t *stream);
102 static void hb_ts_stream_find_pids(hb_stream_t *stream);
103 static void hb_ts_stream_decode(hb_stream_t *stream);
104 static void hb_ts_stream_reset(hb_stream_t *stream);
105 static void hb_stream_put_back(hb_stream_t *stream, int i);
106 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
108 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title);
109 static off_t align_to_next_packet(FILE* f);
112 static inline int check_ps_sync(const uint8_t *buf)
114 // must have a Pack header
115 // (note: the operator '&' instead of '&&' is used deliberately for better
116 // performance with a multi-issue pipeline.)
117 return (buf[0] == 0x00) & (buf[1] == 0x00) & (buf[2] == 0x01) & (buf[3] == 0xba);
120 static inline int check_ts_sync(const uint8_t *buf)
122 // must have initial sync byte, no scrambling & a legal adaptation ctrl
123 return (buf[0] == 0x47) & ((buf[3] >> 6) == 0) & ((buf[3] >> 4) > 0);
126 static inline int have_ts_sync(const uint8_t *buf)
128 return check_ts_sync(&buf[0*188]) & check_ts_sync(&buf[1*188]) &
129 check_ts_sync(&buf[2*188]) & check_ts_sync(&buf[3*188]) &
130 check_ts_sync(&buf[4*188]) & check_ts_sync(&buf[5*188]) &
131 check_ts_sync(&buf[6*188]) & check_ts_sync(&buf[7*188]);
134 static int hb_stream_check_for_ts(const uint8_t *buf)
136 // transport streams should have a sync byte every 188 bytes.
137 // search the first KB of buf looking for at least 8 consecutive
138 // correctly located sync patterns.
141 for ( offset = 0; offset < 1024; ++offset )
143 if ( have_ts_sync( &buf[offset]) )
149 static int hb_stream_check_for_ps(const uint8_t *buf)
151 // program streams should have a Pack header every 2048 bytes.
152 // check that we have 4 of these.
153 return check_ps_sync(&buf[0*2048]) & check_ps_sync(&buf[1*2048]) &
154 check_ps_sync(&buf[2*2048]) & check_ps_sync(&buf[3*2048]);
157 static int hb_stream_get_type(hb_stream_t *stream)
161 if ( fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) )
163 if ( hb_stream_check_for_ts(buf) != 0 )
165 hb_log("file is MPEG Transport Stream");
166 stream->stream_type = hb_stream_type_transport;
167 hb_ts_stream_init(stream);
170 if ( hb_stream_check_for_ps(buf) != 0 )
172 hb_log("file is MPEG Program Stream");
173 stream->stream_type = hb_stream_type_program;
180 /***********************************************************************
182 ***********************************************************************
184 **********************************************************************/
185 hb_stream_t * hb_stream_open( char * path )
189 d = calloc( sizeof( hb_stream_t ), 1 );
192 if( ( d->file_handle = fopen( path, "rb" ) ) )
194 d->path = strdup( path );
195 if ( hb_stream_get_type( d ) != 0 )
199 fclose( d->file_handle );
204 hb_log( "hb_stream_open: fopen failed (%s)", path );
210 /***********************************************************************
212 ***********************************************************************
213 * Closes and frees everything
214 **********************************************************************/
215 void hb_stream_close( hb_stream_t ** _d )
217 hb_stream_t * d = *_d;
221 fclose( d->file_handle );
222 d->file_handle = NULL;
226 for (i = 0; i < kNumDecodeBuffers; i++)
228 if (d->ps_decode_buffer[i].data)
230 free(d->ps_decode_buffer[i].data);
231 d->ps_decode_buffer[i].data = NULL;
235 for (i = 0; i < kMaxNumberDecodeStreams; i++)
237 if (d->ts_packetbuf[i])
239 free(d->ts_packetbuf[i]);
240 d->ts_packetbuf[i] = NULL;
248 static void hb_stream_delete_audio_entry(hb_stream_t *stream, int indx)
252 for (i = indx+1; i < stream->ts_number_audio_pids; ++i)
254 stream->ts_audio_pids[indx] = stream->ts_audio_pids[i];
255 stream->ts_audio_stream_type[indx] = stream->ts_audio_stream_type[i];
258 --stream->ts_number_audio_pids;
261 /***********************************************************************
262 * hb_ps_stream_title_scan
263 ***********************************************************************
265 **********************************************************************/
266 hb_title_t * hb_stream_title_scan(hb_stream_t *stream)
269 hb_title_t *aTitle = hb_title_init( stream->path, 0 );
272 // Copy part of the stream path to the title name
273 char *sep = strrchr(stream->path, '/');
275 strcpy(aTitle->name, sep+1);
276 char *dot_term = strrchr(aTitle->name, '.');
280 // Height, width, rate and aspect ratio information is filled in when the previews are built
282 hb_stream_duration(stream, aTitle);
285 hb_chapter_t * chapter;
286 chapter = calloc( sizeof( hb_chapter_t ), 1 );
288 chapter->duration = aTitle->duration;
289 chapter->hours = aTitle->hours;
290 chapter->minutes = aTitle->minutes;
291 chapter->seconds = aTitle->seconds;
292 hb_list_add( aTitle->list_chapter, chapter );
294 // Figure out how many audio streams we really have:
295 // - For transport streams, for each PID listed in the PMT (whether
296 // or not it was an audio stream type) read the bitstream until we
297 // find an packet from that PID containing a PES header and see if
298 // the elementary stream is an audio type.
299 // - For program streams read the first 4MB and take every unique
300 // audio stream we find.
301 if (stream->stream_type == hb_stream_type_transport)
305 for (i=0; i < stream->ts_number_audio_pids; i++)
307 hb_audio_t *audio = hb_ts_stream_set_audio_id_and_codec(stream, i);
309 hb_list_add( aTitle->list_audio, audio );
313 hb_stream_delete_audio_entry(stream, i);
320 hb_ps_stream_find_audio_ids(stream, aTitle);
327 * scan the next MB of 'stream' to find the next start packet for
328 * the Packetized Elementary Stream associated with TS PID 'pid'.
330 static const uint8_t *hb_ts_stream_getPEStype(hb_stream_t *stream, uint32_t pid)
332 static uint8_t buf[188];
333 int npack = 100000; // max packets to read
337 if (fread(buf, 1, 188, stream->file_handle) != 188)
339 hb_log("hb_ts_stream_getPEStype: EOF while searching for PID 0x%x", pid);
344 hb_log("hb_ts_stream_getPEStype: lost sync while searching for PID 0x%x", pid);
345 align_to_next_packet(stream->file_handle);
350 * The PES header is only in TS packets with 'start' set so we check
351 * that first then check for the right PID.
353 if ((buf[1] & 0x40) == 0 || (buf[1] & 0x1f) != (pid >> 8) ||
354 buf[2] != (pid & 0xff))
356 // not a start packet or not the pid we want
360 /* skip over the TS hdr to return a pointer to the PES hdr */
362 switch (buf[3] & 0x30)
364 case 0x00: // illegal
365 case 0x20: // fill packet
368 case 0x30: // adaptation
371 hb_log("hb_ts_stream_getPEStype: invalid adaptation field length %d for PID 0x%x", buf[4], pid);
384 /***********************************************************************
386 ***********************************************************************
388 **********************************************************************/
389 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle)
391 // VOB Files often have exceedingly unusual PTS values in them - they will progress for a while
392 // and then reset without warning !
393 if (strstr(stream->path,".vob") != NULL)
395 // So we'll use a 'fake duration' that should give enough time !
396 int64_t duration = 4 * 3600 * 90000;
397 inTitle->duration = duration; //90LL * dvdtime2msec( &d->pgc->playback_time );
398 inTitle->hours = inTitle->duration / 90000 / 3600;
399 inTitle->minutes = ( ( inTitle->duration / 90000 ) % 3600 ) / 60;
400 inTitle->seconds = ( inTitle->duration / 90000 ) % 60;
404 uint64_t first_pts, last_pts;
407 // To calculate the duration we get video presentation time stamps
408 // at a couple places in the file then use their difference scaled
409 // by the ratio of the distance between our measurement points &
410 // the size of the file. The issue is that our video file may have
411 // chunks from several different program fragments (main feature,
412 // commercials, station id, trailers, etc.) all with their on base
413 // pts value. We need to difference two pts's from the same program
414 // fragment. Since extraneous material is very likely at the beginning
415 // and end of the content, for now we take a time stamp from 25%
416 // into the file & 75% into the file then double their difference
417 // to get the total duration.
418 fseeko(stream->file_handle, 0, SEEK_END);
419 uint64_t fsize = ftello(stream->file_handle);
420 fseeko(stream->file_handle, fsize >> 2, SEEK_SET);
421 align_to_next_packet(stream->file_handle);
422 buf = hb_ts_stream_getPEStype(stream, stream->ts_video_pids[0]);
425 hb_log("hb_stream_duration: couldn't find video start packet");
428 if ((buf[7] >> 7) != 1)
430 hb_log("hb_stream_duration: no PTS in initial video packet");
433 first_pts = ( ( (uint64_t)buf[9] >> 1 ) & 7 << 30 ) |
434 ( (uint64_t)buf[10] << 22 ) |
435 ( ( (uint64_t)buf[11] >> 1 ) << 15 ) |
436 ( (uint64_t)buf[12] << 7 ) |
437 ( (uint64_t)buf[13] >> 1 );
438 hb_log("hb_stream_duration: first pts %lld", first_pts);
440 // now get a pts from a frame near the end of the file.
441 fseeko(stream->file_handle, fsize - (fsize >> 2), SEEK_SET);
442 align_to_next_packet(stream->file_handle);
443 buf = hb_ts_stream_getPEStype(stream, stream->ts_video_pids[0]);
446 hb_log("hb_stream_duration: couldn't find video end packet");
449 if ((buf[7] >> 7) != 1)
451 hb_log("hb_stream_duration: no PTS in final video packet");
452 inTitle->duration = 0;
455 last_pts = ( ( (uint64_t)buf[9] >> 1 ) & 7 << 30 ) |
456 ( (uint64_t)buf[10] << 22 ) |
457 ( ( (uint64_t)buf[11] >> 1 ) << 15 ) |
458 ( (uint64_t)buf[12] << 7 ) |
459 ( (uint64_t)buf[13] >> 1 );
460 hb_log("hb_stream_duration: last pts %lld", last_pts);
462 inTitle->duration = (last_pts - first_pts) * 2 + 90000 * 60;
463 inTitle->hours = inTitle->duration / 90000 / 3600;
464 inTitle->minutes = ( ( inTitle->duration / 90000 ) % 3600 ) / 60;
465 inTitle->seconds = ( inTitle->duration / 90000 ) % 60;
467 rewind(stream->file_handle);
470 /***********************************************************************
472 ***********************************************************************
474 **********************************************************************/
475 int hb_stream_read( hb_stream_t * src_stream, hb_buffer_t * b )
477 if (src_stream->stream_type == hb_stream_type_program)
480 amt_read = fread(b->data, HB_DVD_READ_BUFFER_SIZE, 1, src_stream->file_handle);
486 else if (src_stream->stream_type == hb_stream_type_transport)
488 int read_buffer_index = src_stream->ps_current_read_buffer_index;
490 // Transport streams are a little more complex - we might be able to just
491 // read from the transport stream conversion buffer (if there's enough data)
492 // or we may need to transfer what's left and fill it again.
493 if (src_stream->ps_decode_buffer[read_buffer_index].len - src_stream->ps_decode_buffer[read_buffer_index].read_pos > HB_DVD_READ_BUFFER_SIZE)
495 memcpy(b->data, src_stream->ps_decode_buffer[read_buffer_index].data + src_stream->ps_decode_buffer[read_buffer_index].read_pos,HB_DVD_READ_BUFFER_SIZE);
496 src_stream->ps_decode_buffer[read_buffer_index].read_pos += HB_DVD_READ_BUFFER_SIZE;
501 // Not quite enough data in the buffer - transfer what is present, fill the buffer and then
502 // transfer what's still needed.
503 int transfer_size = HB_DVD_READ_BUFFER_SIZE;
504 int amt_avail_to_transfer = src_stream->ps_decode_buffer[read_buffer_index].len - src_stream->ps_decode_buffer[read_buffer_index].read_pos;
505 memcpy(b->data, src_stream->ps_decode_buffer[read_buffer_index].data + src_stream->ps_decode_buffer[read_buffer_index].read_pos, amt_avail_to_transfer);
506 transfer_size -= amt_avail_to_transfer;
507 src_stream->ps_decode_buffer[read_buffer_index].read_pos += amt_avail_to_transfer;
509 // Give up this buffer - decoding may well need it, and we're done
510 src_stream->ps_decode_buffer[read_buffer_index].write_pos = 0;
511 src_stream->ps_decode_buffer[read_buffer_index].len = 0;
514 hb_ts_stream_decode(src_stream);
516 // Decoding will almost certainly have changed the current read buffer index
517 read_buffer_index = src_stream->ps_current_read_buffer_index;
519 if (src_stream->ps_decode_buffer[read_buffer_index].len == 0)
521 hb_log("hb_stream_read - buffer after decode has zero length data");
525 // Read the bit we still need
526 memcpy(b->data+amt_avail_to_transfer, src_stream->ps_decode_buffer[read_buffer_index].data + src_stream->ps_decode_buffer[read_buffer_index].read_pos,transfer_size);
527 src_stream->ps_decode_buffer[read_buffer_index].read_pos += transfer_size;
536 /***********************************************************************
538 ***********************************************************************
540 **********************************************************************/
541 int hb_stream_seek( hb_stream_t * src_stream, float f )
543 off_t stream_size, cur_pos, new_pos;
544 double pos_ratio = f;
545 cur_pos = ftello(src_stream->file_handle);
546 fseeko(src_stream->file_handle,0 ,SEEK_END);
547 stream_size = ftello(src_stream->file_handle);
548 new_pos = (off_t) ((double) (stream_size) * pos_ratio);
549 int r = fseeko(src_stream->file_handle, new_pos, SEEK_SET);
553 fseeko(src_stream->file_handle, cur_pos, SEEK_SET);
557 if (src_stream->stream_type == hb_stream_type_transport)
559 // We need to drop the current decoder output and move
560 // forwards to the next transport stream packet.
561 hb_ts_stream_reset(src_stream);
564 // Now we must scan forwards for a valid start code (0x000001BA)
566 hb_buffer_t *buf = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
569 if (hb_stream_read(src_stream,buf) == 1)
572 for (i=0; (i <= HB_DVD_READ_BUFFER_SIZE-4) && (!done); i++)
574 if ((buf->data[i] == 0x00) && (buf->data[i+1] == 0x00) && (buf->data[i+2] == 0x01) && (buf->data[i+3] == 0xba))
577 // 'Put Back' the data we've just read (up to this point)
578 hb_stream_put_back(src_stream, i);
583 done = 1; // End of data;
585 hb_buffer_close(&buf);
589 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
592 off_t cur_pos = ftello(stream->file_handle);
593 hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
596 fseeko(stream->file_handle, 0, SEEK_SET);
597 align_to_next_packet(stream->file_handle);
598 buf = hb_ts_stream_getPEStype(stream, stream->ts_audio_pids[aud_pid_index]);
600 /* check that we found a PES header */
601 if (buf && buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
605 audio->id = 0x80bd | (aud_pid_index << 8);
606 audio->codec = HB_ACODEC_AC3;
607 hb_log("transport stream pid 0x%x (type 0x%x) is AC-3 audio id 0x%x",
608 stream->ts_audio_pids[aud_pid_index],
609 stream->ts_audio_stream_type[aud_pid_index],
611 stream->ts_audio_stream_type[aud_pid_index] = 0x81;
613 else if ((buf[3] & 0xe0) == 0xc0)
616 audio->codec = HB_ACODEC_MPGA;
617 hb_log("transport stream pid 0x%x (type 0x%x) is MPEG audio id 0x%x",
618 stream->ts_audio_pids[aud_pid_index],
619 stream->ts_audio_stream_type[aud_pid_index],
621 stream->ts_audio_stream_type[aud_pid_index] = 0x03;
624 fseeko(stream->file_handle, cur_pos, SEEK_SET);
627 hb_log("transport stream pid 0x%x (type 0x%x) isn't audio",
628 stream->ts_audio_pids[aud_pid_index],
629 stream->ts_audio_stream_type[aud_pid_index]);
634 static void add_audio_to_title(hb_title_t *title, int id)
636 hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
642 audio->codec = HB_ACODEC_MPGA;
643 hb_log("hb_ps_stream_find_audio_ids: added MPEG audio stream 0x%x", id);
646 audio->codec = HB_ACODEC_LPCM;
647 hb_log("hb_ps_stream_find_audio_ids: added LPCM audio stream 0x%x", id);
650 audio->codec = HB_ACODEC_DCA;
651 hb_log("hb_ps_stream_find_audio_ids: added DCA audio stream 0x%x", id);
654 audio->codec = HB_ACODEC_AC3;
655 hb_log("hb_ps_stream_find_audio_ids: added AC3 audio stream 0x%x", id);
658 hb_list_add( title->list_audio, audio );
661 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title)
663 off_t cur_pos = ftello(stream->file_handle);
664 hb_buffer_t *buf = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
665 hb_list_t *list = hb_list_init();
666 // how many blocks we read while searching for audio streams
668 // there can be at most 16 unique streams in an MPEG PS (8 in a DVD)
669 // so we use a bitmap to keep track of the ones we've already seen.
670 // Bit 'i' of smap is set if we've already added the audio for
671 // audio substream id 'i' to the title's audio list.
674 hb_stream_seek(stream, 0.0f);
676 while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
680 // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
681 hb_demux_ps(buf, list);
683 while ( ( es = hb_list_item( list, 0 ) ) )
685 hb_list_rem( list, es );
686 if ( (es->id & 0xff) == 0xbd || (es->id & 0xe0) == 0xc0 )
688 // this PES contains some kind of audio - get the substream id
689 // and check if we've seen it already.
690 int ssid = (es->id > 0xff ? es->id >> 8 : es->id) & 0xf;
691 if ( (smap & (1 << ssid)) == 0 )
693 // we haven't seen this stream before - add it to the
694 // title's list of audio streams.
696 add_audio_to_title(title, es->id);
699 hb_buffer_close( &es );
702 hb_list_empty( &list );
703 hb_buffer_close(&buf);
704 fseeko(stream->file_handle, cur_pos, SEEK_SET);
707 /***********************************************************************
708 * hb_stream_update_audio
709 ***********************************************************************
711 **********************************************************************/
712 void hb_stream_update_audio(hb_stream_t *stream, hb_audio_t *audio)
716 if (stream->stream_type == hb_stream_type_transport)
718 // Find the audio stream info for this PID
719 int i = (audio->id >> 8) & 0xf;
720 if (i >= stream->ts_number_audio_pids)
722 hb_log("hb_stream_update_audio: no PID for audio stream 0x%x",
727 lang = lang_for_code(stream->a52_info[i].lang_code);
729 audio->rate = stream->a52_info[i].rate;
731 audio->bitrate = stream->a52_info[i].bitrate;
732 if (!audio->config.a52.ac3flags)
733 audio->config.a52.ac3flags = audio->ac3flags = stream->a52_info[i].flags;
738 // XXX should try to get language code from the AC3 bitstream
739 lang = lang_for_code(0x0000);
742 if (!audio->input_channel_layout)
744 switch( audio->ac3flags & A52_CHANNEL_MASK )
750 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
755 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
757 /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
759 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
763 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
767 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
771 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
774 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
777 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
781 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
784 /* add in our own LFE flag if the source has LFE */
785 if (audio->ac3flags & A52_LFE)
787 audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
791 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)", strlen(lang->native_name) ? lang->native_name : lang->eng_name,
792 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec == HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) );
793 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s", strlen(lang->native_name) ? lang->native_name : lang->eng_name );
794 snprintf( audio->iso639_2, sizeof( audio->iso639_2 ), "%s", lang->iso639_2);
796 if ( (audio->ac3flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
797 sprintf( audio->lang + strlen( audio->lang ),
798 " (Dolby Surround)" );
800 sprintf( audio->lang + strlen( audio->lang ),
802 HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(audio->input_channel_layout) +
803 HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(audio->input_channel_layout),
804 HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(audio->input_channel_layout));
807 hb_log( "hb_stream_update_audio: id=%x, lang=%s, 3cc=%s, rate = %d, bitrate = %d, flags = 0x%x (%d)", audio->id, audio->lang, audio->iso639_2, audio->rate, audio->bitrate, audio->ac3flags, audio->ac3flags );
811 /***********************************************************************
813 ***********************************************************************
815 **********************************************************************/
816 static void hb_stream_put_back(hb_stream_t *stream, int i)
818 if (stream->stream_type == hb_stream_type_program)
820 // Program streams are pretty easy - we just reposition the source file
822 fseeko(stream->file_handle, -(HB_DVD_READ_BUFFER_SIZE-i), SEEK_CUR);
824 else if (stream->stream_type == hb_stream_type_transport)
826 int read_buffer_index = stream->ps_current_read_buffer_index;
828 // Transport streams are a little more tricky - so long as the
829 // amount to back up is still within the current decode buffer
830 // we can just adjust the read pos.
831 if (stream->ps_decode_buffer[read_buffer_index].read_pos - i > 0)
833 stream->ps_decode_buffer[read_buffer_index].read_pos -= i;
836 hb_error("hb_stream_put_back - trying to step beyond the start of the buffer, read_pos = %d amt to put back = %d\n", stream->ps_decode_buffer[read_buffer_index].read_pos, i);
841 /***********************************************************************
843 ***********************************************************************
845 **********************************************************************/
846 #define PS_DECODE_BUFFER_SIZE ( 1024 * 1024 * 4)
848 static void hb_ts_stream_init(hb_stream_t *stream)
850 // Output Program Stream
852 for (i=0; i < kNumDecodeBuffers; i++)
854 stream->ps_decode_buffer[i].data = (unsigned char *) malloc(PS_DECODE_BUFFER_SIZE);
855 stream->ps_decode_buffer[i].read_pos = 0;
856 stream->ps_decode_buffer[i].size = PS_DECODE_BUFFER_SIZE;
857 stream->ps_decode_buffer[i].len = 0;
858 stream->ps_decode_buffer[i].write_pos = 0;
861 for (i=0; i < kMaxNumberDecodeStreams; i++)
863 stream->ts_streamcont[i] = -1;
866 stream->ps_current_write_buffer_index = 0;
867 stream->ps_current_read_buffer_index = 1;
869 // Find the audio and video pids in the stream
870 hb_ts_stream_find_pids(stream);
872 for (i=0; i < stream->ts_number_video_pids; i++)
874 // In progress audio/video data during the transport stream -> program stream processing
875 stream->ts_packetbuf[i] = (unsigned char *) malloc(1024 * 1024);
876 stream->ts_streamid[i] = 0xE0; // Stream is Video
879 for (i = stream->ts_number_video_pids; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
881 stream->ts_packetbuf[i] = (unsigned char *) malloc(1024 * 1024);
882 stream->ts_streamid[i] = 0xBD; // Stream 1 is AC-3 Audio
886 // ------------------------------------------------------------------------------------
888 static off_t align_to_next_packet(FILE* f)
890 unsigned char buf[188*20];
892 off_t start = ftello(f);
895 if (fread(buf, 188*20, 1, f) == 1)
898 while (!found && (pos < 188))
902 for (i = 0; i < 188*20; i += 188)
904 unsigned char c = buf[pos+i];
906 if ((c != 0x47) && (c != 0x72) && (c != 0x29))
908 // this offset failed, try next
918 pos = 0; // failed to find anything!!!!!?
920 fseeko(f, start+pos, SEEK_SET);
925 // ------------------------------------------------------------------------------------
928 unsigned int bitval = 0;
929 unsigned char* bitbuf = NULL;
930 unsigned int bitmask[] = {
931 0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,
932 0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff,0xffff,
933 0x1ffff,0x3ffff,0x7ffff,0xfffff,0x1fffff,0x3fffff,0x7fffff,0xffffff,
934 0x1ffffff,0x3ffffff,0x7ffffff,0xfffffff,0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
936 static inline void set_buf(unsigned char* buf, int bufsize, int clear)
940 bitval = (bitbuf[0] << 24) | (bitbuf[1] << 16) | (bitbuf[2] << 8) | bitbuf[3];
942 memset(bitbuf, 0, bufsize);
945 static inline int buf_size()
950 static inline void set_bits(unsigned int val, int bits)
952 val &= bitmask[bits];
956 int bitsleft = (8 - (bitpos & 7));
957 if (bits >= bitsleft)
959 bitbuf[bitpos >> 3] |= val >> (bits - bitsleft);
962 val &= bitmask[bits];
966 bitbuf[bitpos >> 3] |= val << (bitsleft - bits);
973 static inline unsigned int get_bits(int bits)
976 int left = 32 - (bitpos & 31);
980 val = (bitval >> (left - bits)) & bitmask[bits];
985 val = (bitval & bitmask[left]) << (bits - left);
989 int pos = bitpos >> 3;
990 bitval = (bitbuf[pos] << 24) | (bitbuf[pos + 1] << 16) | (bitbuf[pos + 2] << 8) | bitbuf[pos + 3];
994 val |= (bitval >> (32 - bits)) & bitmask[bits];
1002 // extract what useful information we can from the elementary stream
1003 // descriptor list at 'dp' and add it to the stream at 'esindx'.
1004 // Descriptors with info we don't currently use are ignored.
1005 // The descriptor list & descriptor item formats are defined in
1006 // ISO 13818-1 (2000E) section 2.6 (pg. 62).
1007 static void decode_element_descriptors(hb_stream_t* stream, int esindx,
1008 const uint8_t *dp, uint8_t dlen)
1010 const uint8_t *ep = dp + dlen;
1016 case 10: // ISO_639_language descriptor
1017 stream->a52_info[esindx].lang_code = lang_to_code(lang_for_code2((const char *)&dp[2]));
1027 int decode_program_map(hb_stream_t* stream)
1029 set_buf(stream->pmt_info.tablebuf, stream->pmt_info.tablepos, 0);
1031 unsigned char table_id = get_bits(8);
1033 unsigned int section_length = get_bits(12);
1034 stream->pmt_info.section_length = section_length;
1036 unsigned int program_number = get_bits(16);
1037 stream->pmt_info.program_number = program_number;
1039 unsigned char version_number = get_bits(5);
1041 unsigned char section_number = get_bits(8);
1042 unsigned char last_section_number = get_bits(8);
1044 unsigned int PCR_PID = get_bits(13);
1045 stream->pmt_info.PCR_PID = PCR_PID;
1047 unsigned int program_info_length = get_bits(12);
1048 stream->pmt_info.program_info_length = program_info_length;
1051 unsigned char *descriptor_buf = (unsigned char *) malloc(program_info_length);
1052 for (i = 0; i < program_info_length; i++)
1054 descriptor_buf[i] = get_bits(8);
1057 int cur_pos = 9 /* data after the section length field*/ + program_info_length;
1058 int done_reading_stream_types = 0;
1059 while (!done_reading_stream_types)
1061 unsigned char stream_type = get_bits(8);
1063 unsigned int elementary_PID = get_bits(13);
1065 unsigned int ES_info_length = get_bits(12);
1068 unsigned char *ES_info_buf = (unsigned char *) malloc(ES_info_length);
1069 for (i=0; i < ES_info_length; i++)
1071 ES_info_buf[i] = get_bits(8);
1074 if (stream_type == 0x02)
1076 if (stream->ts_number_video_pids <= kMaxNumberVideoPIDS)
1077 stream->ts_number_video_pids++;
1078 stream->ts_video_pids[stream->ts_number_video_pids-1] = elementary_PID;
1082 // Defined audio stream types are 0x81 for AC-3/A52 audio and 0x03
1083 // for mpeg audio. But content producers seem to use other
1084 // values (0x04 and 0x06 have both been observed) so at this point
1085 // we say everything that isn't a video pid is audio then at the end
1086 // of hb_stream_title_scan we'll figure out which are really audio
1087 // by looking at the PES headers.
1088 i = stream->ts_number_audio_pids;
1089 if (i < kMaxNumberAudioPIDS)
1090 stream->ts_number_audio_pids++;
1091 stream->ts_audio_pids[i] = elementary_PID;
1092 stream->ts_audio_stream_type[i] = stream_type;
1094 if (ES_info_length > 0)
1096 decode_element_descriptors(stream, i, ES_info_buf, ES_info_length);
1100 cur_pos += 5 /* stream header */ + ES_info_length;
1104 if (cur_pos >= section_length - 4 /* stop before the CRC */)
1105 done_reading_stream_types = 1;
1108 free(descriptor_buf);
1112 // ------------------------------------------------------------------------------------
1114 int build_program_map(unsigned char *buf, hb_stream_t *stream)
1116 // Get adaption header info
1118 int adaption = (buf[3] & 0x30) >> 4;
1121 else if (adaption == 0x2)
1123 else if (adaption == 0x3)
1124 adapt_len = buf[4] + 1;
1125 if (adapt_len > 184)
1128 // Get payload start indicator
1130 start = (buf[1] & 0x40) != 0;
1132 // Get pointer length - only valid in packets with a start flag
1133 int pointer_len = 0;
1134 if (start && stream->pmt_info.reading)
1136 // We just finished a bunch of packets - parse the program map details
1138 if (stream->pmt_info.tablebuf[0] == 0x02)
1139 decode_ok = decode_program_map(stream);
1140 free(stream->pmt_info.tablebuf);
1141 stream->pmt_info.tablebuf = NULL;
1142 stream->pmt_info.tablepos = 0;
1143 stream->pmt_info.reading = 0;
1150 pointer_len = buf[4 + adapt_len] + 1;
1151 stream->pmt_info.tablepos = 0;
1153 // Get Continuity Counter
1154 int continuity_counter = buf[3] & 0x0f;
1155 if (!start && (stream->pmt_info.current_continuity_counter + 1 != continuity_counter))
1157 hb_log("build_program_map - Continuity Counter %d out of sequence - expected %d", continuity_counter, stream->pmt_info.current_continuity_counter+1);
1160 stream->pmt_info.current_continuity_counter = continuity_counter;
1161 stream->pmt_info.reading |= start;
1163 // Add the payload for this packet to the current buffer
1164 int amount_to_copy = 184 - adapt_len - pointer_len;
1165 if (stream->pmt_info.reading && (amount_to_copy > 0))
1167 stream->pmt_info.tablebuf = realloc(stream->pmt_info.tablebuf, stream->pmt_info.tablepos + amount_to_copy);
1169 memcpy(stream->pmt_info.tablebuf + stream->pmt_info.tablepos, buf + 4 + adapt_len + pointer_len, amount_to_copy);
1170 stream->pmt_info.tablepos += amount_to_copy;
1176 int decode_PAT(unsigned char *buf, hb_stream_t *stream)
1178 unsigned char tablebuf[1024];
1179 unsigned int tablepos = 0;
1184 // Get adaption header info
1186 int adaption = (buf[3] & 0x30) >> 4;
1189 else if (adaption == 0x2)
1191 else if (adaption == 0x3)
1192 adapt_len = buf[4] + 1;
1193 if (adapt_len > 184)
1196 // Get pointer length
1197 int pointer_len = buf[4 + adapt_len] + 1;
1199 // Get payload start indicator
1201 start = (buf[1] & 0x40) != 0;
1206 // Add the payload for this packet to the current buffer
1207 if (reading && (184 - adapt_len) > 0)
1209 if (tablepos + 184 - adapt_len - pointer_len > 1024)
1211 hb_log("decode_PAT - Bad program section length (> 1024)");
1214 memcpy(tablebuf + tablepos, buf + 4 + adapt_len + pointer_len, 184 - adapt_len - pointer_len);
1215 tablepos += 184 - adapt_len - pointer_len;
1218 if (start && reading)
1220 memcpy(tablebuf + tablepos, buf + 4 + adapt_len + 1, pointer_len - 1);
1223 unsigned int pos = 0;
1224 //while (pos < tablepos)
1226 set_buf(tablebuf + pos, tablepos - pos, 0);
1228 unsigned char section_id = get_bits(8);
1230 unsigned int section_len = get_bits(12);
1231 unsigned int transport_id = get_bits(16);
1233 unsigned int version_num = get_bits(5);
1234 unsigned int current_next = get_bits(1);
1235 unsigned int section_num = get_bits(8);
1236 unsigned int last_section = get_bits(8);
1242 // Program Association Section
1243 section_len -= 5; // Already read transport stream ID, version num, section num, and last section num
1244 section_len -= 4; // Ignore the CRC
1246 stream->ts_number_pat_entries = 0;
1247 while ((curr_pos < section_len) && (stream->ts_number_pat_entries < kMaxNumberPMTStreams))
1249 unsigned int pkt_program_num = get_bits(16);
1250 stream->pat_info[stream->ts_number_pat_entries].program_number = pkt_program_num;
1252 get_bits(3); // Reserved
1253 if (pkt_program_num == 0)
1255 unsigned int pkt_network_PID = get_bits(13);
1259 unsigned int pkt_program_map_PID = get_bits(13);
1260 stream->pat_info[stream->ts_number_pat_entries].program_map_PID = pkt_program_map_PID;
1263 stream->ts_number_pat_entries++;
1277 pos += 3 + section_len;
1285 static int flushbuf(hb_stream_t *stream)
1287 int old_write_index = stream->ps_current_write_buffer_index;
1289 // Flip the buffers and start moving on to the next
1290 stream->ps_current_write_buffer_index++;
1291 if (stream->ps_current_write_buffer_index > kNumDecodeBuffers-1)
1292 stream->ps_current_write_buffer_index = 0;
1294 if ( (stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len != 0) || (stream->ps_decode_buffer[stream->ps_current_write_buffer_index].write_pos != 0) )
1296 hb_log("flushbuf - new buffer (index %d) has non zero length and write position !", stream->ps_current_write_buffer_index);
1300 stream->ps_current_read_buffer_index = old_write_index;
1301 stream->ps_decode_buffer[stream->ps_current_read_buffer_index].read_pos = 0;
1306 static int fwrite64(void* buf, int elsize, int elnum, hb_stream_t* stream)
1313 int current_write_index = stream->ps_current_write_buffer_index;
1315 if (size <= stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos)
1317 memcpy(stream->ps_decode_buffer[current_write_index].data + stream->ps_decode_buffer[current_write_index].write_pos, buf, size);
1318 stream->ps_decode_buffer[current_write_index].write_pos += size;
1319 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1324 memcpy(stream->ps_decode_buffer[current_write_index].data + stream->ps_decode_buffer[current_write_index].write_pos, buf, stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos);
1325 written += stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos;
1326 stream->ps_decode_buffer[current_write_index].write_pos += stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos;
1327 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1329 if (flushbuf(stream))
1331 // FLushing the buffer will have change the current write buffer
1332 current_write_index = stream->ps_current_write_buffer_index;
1334 memcpy(stream->ps_decode_buffer[current_write_index].data, (unsigned char*)buf + written, size - written);
1335 stream->ps_decode_buffer[current_write_index].write_pos += size - written;
1336 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1337 written += size - written;
1342 if (elnum == 1 && written == size)
1345 return written / elsize;
1348 static int write_pack(hb_stream_t* stream, int64_t time)
1350 unsigned char buf[64];
1351 set_buf(buf, 64, 1); // clear buffer
1353 int64_t ext_time = time % 300;
1356 set_bits(0x000001ba, 32); // pack id 32
1357 set_bits(1, 2); // 0x01 2
1358 set_bits((unsigned int)(time >> 30), 3); // system_clock_reference_base 3
1359 set_bits(1, 1); // marker_bit 1
1360 set_bits((unsigned int)(time >> 15), 15); // system_clock_reference_base 15
1361 set_bits(1, 1); // marker_bit 1
1362 set_bits((unsigned int)time, 15); // system_clock_reference_base1 15
1363 set_bits(1, 1); // marker_bit 1
1364 set_bits((unsigned int)ext_time, 9); // system_clock_reference_extension 9
1365 set_bits(1, 1); // marker_bit 1
1366 set_bits(DEMUX, 22); // program_mux_rate 22
1367 set_bits(1, 1); // marker_bit 1
1368 set_bits(1, 1); // marker_bit 1
1369 set_bits(31, 5); // reserved 5
1370 set_bits(0, 3); // pack_stuffing_length 3
1372 return fwrite64(buf, buf_size(), 1, stream) == 1;
1375 static int pad_buffer(hb_stream_t *stream, int pad)
1380 buf[0] = '\x0'; buf[1] = '\x0'; buf[2] = '\x1'; buf[3] = '\xbe';
1381 buf[4] = pad >> 8; buf[5] = pad & 0xff;
1383 if (fwrite64(buf, 6, 1, stream) != 1)
1386 unsigned char padbyte = 0xff;
1388 for (i = 0; i < pad; i++)
1390 if (fwrite64(&padbyte, 1, 1, stream) != 1)
1397 int make_pes_header(unsigned char* buf, int streamid, int len, int64_t PTS, int64_t DTS)
1400 int PTS_DTS_flags = 0;
1415 set_buf(buf, 9 + hdrlen, 1); // clear the buffer
1417 set_bits(0x000001, 24); // packet_start_code_prefix 24
1418 set_bits((unsigned int)streamid, 8); // directory_stream_id 8
1419 set_bits(len, 16); // PES_packet_length 16
1420 set_bits(0x2, 2); // '10' 2
1421 set_bits(0, 2); // PES_scrambling_control 2
1422 set_bits(1, 1); // PES_priority 1
1423 set_bits(0, 1); // data_alignment_indicator 1
1424 set_bits(0, 1); // copyright 1
1425 set_bits(0, 1); // original_or_copy 1
1426 set_bits(PTS_DTS_flags, 2); // PTS_DTS_flags 2
1427 set_bits(0, 1); // ESCR_flag 1
1428 set_bits(0, 1); // ES_rate_flag 1
1429 set_bits(0, 1); // DSM_trick_mode_flag 1
1430 set_bits(0, 1); // additional_copy_info_flag 1
1431 set_bits(0, 1); // PES_CRC_flag 1
1432 set_bits(0, 1); // PES_extension_flag 1
1433 set_bits(hdrlen, 8); // PES_header_data_length 8
1435 if (PTS_DTS_flags == 2)
1437 set_bits(2, 4); // '0010' 4
1438 set_bits((unsigned int)(PTS >> 30), 3); // PTS [32..30] 3
1439 set_bits(1, 1); // marker bit 1
1440 set_bits((unsigned int)(PTS >> 15), 15); // PTS [29..15] 15
1441 set_bits(1, 1); // marker bit 1
1442 set_bits((unsigned int)PTS, 15); // PTS [14..0] 15
1443 set_bits(1, 1); // marker bit 1
1445 else if (PTS_DTS_flags == 3)
1447 set_bits(3, 4); // '0011' 4
1448 set_bits((unsigned int)(PTS >> 30), 3); // PTS [32..30] 3
1449 set_bits(1, 1); // marker bit 1
1450 set_bits((unsigned int)(PTS >> 15), 15); // PTS [29..15] 15
1451 set_bits(1, 1); // marker bit 1
1452 set_bits((unsigned int)PTS, 15); // PTS [14..0] 15
1453 set_bits(1, 1); // marker bit 1
1454 set_bits(1, 4); // '0001' 4
1455 set_bits((unsigned int)(DTS >> 30), 3); // DTS [32..30] 3
1456 set_bits(1, 1); // marker bit 1
1457 set_bits((unsigned int)(DTS >> 15), 15); // DTS [29..15] 15
1458 set_bits(1, 1); // marker bit 1
1459 set_bits((unsigned int)DTS, 15); // DTS [14..0] 15
1460 set_bits(1, 1); // marker bit 1
1466 int generate_output_data(hb_stream_t *stream, int write_ac3, int curstream, int pid)
1468 unsigned char ac3_substream_id[4];
1473 // Make a four byte DVD ac3 stream header
1474 int ssid = (curstream - stream->ts_number_video_pids) & 0xf;
1475 ac3_substream_id[0] = 0x80 | ssid; // substream id
1476 ac3_substream_id[1] = 0x01; // number of sync words
1477 ac3_substream_id[2] = 0x00; // first offset (16 bits)
1478 ac3_substream_id[3] = 0x02;
1482 int written = 0; // Bytes we've written to output file
1483 int pos = 0; // Position in PES packet buffer
1487 if ((stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len % HB_DVD_READ_BUFFER_SIZE) != 0)
1489 hb_log("write_output_stream - Packet's not falling on read buffer size boundries!");
1493 // Get total length of this pack
1494 int len = min(14 + ac3len + stream->ts_packetpos[curstream] - pos, HB_DVD_READ_BUFFER_SIZE);
1496 // Figure out stuffing (if we have less than 16 bytes left)
1498 if (len < HB_DVD_READ_BUFFER_SIZE && HB_DVD_READ_BUFFER_SIZE - len < 16)
1500 stuffing = HB_DVD_READ_BUFFER_SIZE - len;
1504 // Write out pack header
1505 off_t file_offset = ftello(stream->file_handle);
1506 int64_t packet_time = (file_offset * CLOCKRATE / STREAMRATE) + 0 /*file_time*/;
1507 if (!write_pack(stream, packet_time))
1509 hb_log("write_output_stream - Couldn't write pack header!");
1513 int index_of_selected_pid = -1;
1514 if ((index_of_selected_pid = index_of_video_pid(pid,stream)) < 0)
1516 if ((index_of_selected_pid = index_of_audio_pid(pid,stream)) < 0)
1518 hb_log("generate_output_data - cannot find pid 0x%x (%d) in selected audio or video pids", pid, pid);
1523 stream->ts_packetbuf[curstream][pos + 3] = stream->ts_streamid[stream->ts_number_video_pids + index_of_selected_pid];
1527 stream->ts_packetbuf[curstream][pos + 3] = stream->ts_streamid[index_of_selected_pid];
1530 // Subtract pack size (14) and pes id and len (6) from lenth
1531 stream->ts_packetbuf[curstream][pos + 4] = (len - 6 - 14) >> 8; stream->ts_packetbuf[curstream][pos + 5] = (len - 6 - 14) & 0xFF;
1533 // Add any stuffing bytes to header extra len
1534 int hdrsize = 9 + stream->ts_packetbuf[curstream][pos + 8];
1535 stream->ts_packetbuf[curstream][pos + 8] += stuffing; // Add stuffing to header bytes
1537 // Write out id, streamid, len
1538 if (fwrite64(stream->ts_packetbuf[curstream] + pos, hdrsize, 1, stream) != 1) // Write pes id, streamid, and len
1540 hb_log("write_output_stream - Failed to write output file!");
1546 for (i = 0; i < stuffing; i++) // Write any stuffing bytes
1548 unsigned char stuff = 0xff;
1549 if (fwrite64(&stuff, 1, 1, stream) != 1)
1551 hb_log("write_output_stream - Failed to write output file!");
1556 // Write ac3 streamid
1559 if (fwrite64(ac3_substream_id, ac3len, 1, stream) != 1)
1561 hb_log("write_output_stream - Failed to write output file!");
1566 // Write rest of data len minus headersize (9) stuffing, and pack size (14)
1567 if (fwrite64(stream->ts_packetbuf[curstream] + pos + hdrsize, len - hdrsize - 14 - stuffing - ac3len, 1, stream) != 1) // Write data bytes
1569 hb_log("write_output_stream - Failed to write output file!");
1574 // Add len minus stuff we added like the pack (14) and the stuffing.
1575 pos += len - 14 - stuffing - ac3len;
1576 if (pos == stream->ts_packetpos[curstream])
1579 // Add pes header for next packet
1581 // make_pes_header(stream->ts_packetbuf[curstream] + pos, (pid == stream->ts_video_pids[0] ? stream->ts_streamid[kVideoStream] : stream->ts_streamid[kAudioStream]), 0, -1, -1);
1582 make_pes_header(stream->ts_packetbuf[curstream] + pos, stream->ts_streamid[curstream], 0, -1, -1);
1586 if ((written % HB_DVD_READ_BUFFER_SIZE) != 0)
1588 int left = HB_DVD_READ_BUFFER_SIZE - (written % HB_DVD_READ_BUFFER_SIZE);
1590 // Pad out to HB_DVD_READ_BUFFER_SIZE bytes
1591 if (!pad_buffer(stream, left))
1593 hb_log("write_output_stream - Couldn't write pad buffer!");
1598 stream->ts_packetpos[curstream] = 0;
1599 stream->ts_streamcont[curstream] = -1;
1604 static void hb_ts_handle_mpeg_audio(hb_stream_t *stream, int curstream, unsigned char* buf, int adapt_len )
1606 // Although we don't have AC3/A52 audio here we can still use the same structure to record this useful information.
1608 stream->a52_info[curstream - stream->ts_number_video_pids].flags = A52_STEREO;
1609 stream->a52_info[curstream - stream->ts_number_video_pids].rate = 48000 /*Hz*/;
1610 stream->a52_info[curstream - stream->ts_number_video_pids].bitrate = 384000 /*Bps*/;
1613 static int hb_ts_handle_ac3_audio(hb_stream_t *stream, int curstream, unsigned char* buf, int adapt_len )
1617 // Make sure we start with 0x0b77
1618 if (stream->ts_packetbuf[curstream][9 + stream->ts_packetbuf[curstream][8]] != 0x0b || stream->ts_packetbuf[curstream][9 + stream->ts_packetbuf[curstream][8] + 1] != 0x77)
1620 spos = 9 + stream->ts_packetbuf[curstream][8];
1621 dpos = 9 + stream->ts_packetbuf[curstream][8];
1622 while (spos <= stream->ts_packetpos[curstream] - 2 && !(stream->ts_packetbuf[curstream][spos] == 0x0b && stream->ts_packetbuf[curstream][spos + 1] == 0x77))
1625 if (!(stream->ts_packetbuf[curstream][spos] == 0x0b && stream->ts_packetbuf[curstream][spos + 1] == 0x77))
1627 hb_log("hb_ts_stream_decode - Couldn't sync AC3 packet!");
1628 stream->ts_skipbad[curstream] = 1;
1632 while (spos < stream->ts_packetpos[curstream])
1634 stream->ts_packetbuf[curstream][dpos] = stream->ts_packetbuf[curstream][spos];
1638 stream->ts_packetpos[curstream] = dpos;
1641 // Check the next packet to make sure IT starts with a 0x0b77
1643 // if (buf[4 + adapt_len] == 0 && buf[4 + adapt_len + 1] == 0 && // Starting with an mpeg header?
1644 // buf[4 + adapt_len + 2] == 1 && buf[4 + adapt_len + 3] == 0xBD)
1645 plen = 9 + buf[4 + adapt_len + 8];
1646 int pstart = 4 + adapt_len + plen;
1647 if (buf[pstart] != 0x0b || buf[pstart + 1] != 0x77)
1650 while (spos < 188 - 2 && !(buf[spos] == 0x0b && buf[spos + 1] == 0x77))
1652 stream->ts_packetbuf[curstream][stream->ts_packetpos[curstream]] = buf[spos];
1653 stream->ts_packetpos[curstream]++;
1657 if (!(buf[spos] == 0x0b && buf[spos + 1] == 0x77))
1659 hb_log("hb_ts_stream_decode - Couldn't sync AC3 packet!");
1660 stream->ts_skipbad[curstream] = 1;
1664 adapt_len = spos - 4 - plen;
1668 while (spos >= pstart - plen)
1670 buf[dpos] = buf[spos];
1676 int flags, rate, bitrate;
1677 if( a52_syncinfo( &buf[pstart], &flags, &rate, &bitrate ) )
1679 stream->a52_info[curstream - stream->ts_number_video_pids].flags = flags;
1680 stream->a52_info[curstream - stream->ts_number_video_pids].rate = rate;
1681 stream->a52_info[curstream - stream->ts_number_video_pids].bitrate = bitrate;
1686 static void hb_ts_stream_find_pids(hb_stream_t *stream)
1688 unsigned char buf[188];
1690 // align to first packet
1691 align_to_next_packet(stream->file_handle);
1693 // Read the Transport Stream Packets (188 bytes each) looking at first for PID 0 (the PAT PID), then decode that
1694 // to find the program map PID and then decode that to get the list of audio and video PIDs
1696 int bytesReadInPacket = 0;
1699 // Try to read packet..
1701 if ((bytesRead = fread(buf+bytesReadInPacket, 1, 188-bytesReadInPacket, stream->file_handle)) != 188-bytesReadInPacket)
1705 bytesReadInPacket += bytesRead;
1707 hb_log("hb_ts_stream_find_pids - end of file");
1712 bytesReadInPacket = 0;
1716 if ((buf[0] != 0x47) && (buf[0] != 0x72) && (buf[0] != 0x29))
1718 hb_log("hb_ts_stream_find_pids - Bad transport packet (no sync byte 0x47)!");
1720 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1721 stream->ts_skipbad[i] = 1;
1726 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
1728 if ((pid == 0x0000) && (stream->ts_number_pat_entries == 0))
1730 decode_PAT(buf, stream);
1735 for (pat_index = 0; pat_index < stream->ts_number_pat_entries; pat_index++)
1737 // There are some streams where the PAT table has multiple entries as if their are
1738 // multiple programs in the same transport stream, and yet there's actually only one
1739 // program really in the stream. This seems to be true for transport streams that
1740 // originate in the HDHomeRun but have been output by EyeTV's export utility. What I think
1741 // is happening is that the HDHomeRun is sending the entire transport stream as broadcast,
1742 // but the EyeTV is only recording a single (selected) program number and not rewriting the
1743 // PAT info on export to match what's actually on the stream.
1744 // Until we have a way of handling multiple programs per transport stream elegantly we'll match
1745 // on the first pat entry for which we find a matching program map PID. The ideal solution would
1746 // be to build a title choice popup from the PAT program number details and then select from
1747 // their - but right now the API's not capable of that.
1748 if (pid == stream->pat_info[pat_index].program_map_PID)
1750 if (build_program_map(buf, stream) > 0)
1754 // Keep going until we have a complete set of PIDs
1755 if ((stream->ts_number_video_pids > 0) && (stream->ts_number_audio_pids > 0))
1759 hb_log("hb_ts_stream_find_pids - found the following PIDS");
1760 hb_log(" Video PIDS : ");
1762 for (i=0; i < stream->ts_number_video_pids; i++)
1764 hb_log(" 0x%x (%d)", stream->ts_video_pids[i], stream->ts_video_pids[i]);
1766 hb_log(" Audio PIDS : ");
1767 for (i = 0; i < stream->ts_number_audio_pids; i++)
1769 hb_log(" 0x%x (%d)", stream->ts_audio_pids[i], stream->ts_audio_pids[i]);
1773 int index_of_video_pid(int pid, hb_stream_t *stream)
1775 int found_pid = -1, i = 0;
1777 for (i = 0; (i < stream->ts_number_video_pids) && (found_pid < 0); i++)
1779 if (pid == stream->ts_video_pids[i])
1785 int index_of_audio_pid(int pid, hb_stream_t *stream)
1787 int i = 0, found_pid = -1;
1789 for (i = 0; (i < stream->ts_number_audio_pids) && (found_pid < 0); i++)
1791 if (pid == stream->ts_audio_pids[i])
1797 int index_of_pid(int pid, hb_stream_t *stream)
1801 if ((found_pid = index_of_video_pid(pid, stream)) >= 0)
1804 if ((found_pid = index_of_audio_pid(pid, stream)) >= 0)
1810 /***********************************************************************
1811 * hb_ts_stream_decode
1812 ***********************************************************************
1814 **********************************************************************/
1815 static void hb_ts_stream_decode(hb_stream_t *stream)
1817 unsigned char buf[188];
1822 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1824 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 0;
1825 stream->ts_skipbad[i] = 0;
1830 if ((stream->ts_number_video_pids == 0) || (stream->ts_number_audio_pids == 0))
1832 hb_log("hb_ts_stream_decode - no Video or Audio PID selected, cannot decode transport stream");
1836 int curr_write_buffer_index = stream->ps_current_write_buffer_index;
1838 // Write output data until a buffer switch occurs.
1839 while (curr_write_buffer_index == stream->ps_current_write_buffer_index)
1841 if ((fread(buf, 188, 1, stream->file_handle)) != 1)
1843 // end of file - we didn't finish filling our ps write buffer
1844 // so just discard the remainder (the partial buffer is useless)
1845 hb_log("hb_ts_stream_decode - eof");
1846 stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len = 0;
1851 if ((buf[0] != 0x47) && (buf[0] != 0x72) && (buf[0] != 0x29))
1853 off_t pos = ftello(stream->file_handle);
1854 hb_log("hb_ts_stream_decode - no sync byte 0x47 @ %lld", pos);
1855 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1857 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 1;
1858 stream->ts_skipbad[i] = 1;
1864 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
1866 // Get the pos and buf - we organize our streams as 'n' video streams then 'm' audio streams
1867 int index_of_selected_pid;
1868 if ((index_of_selected_pid = index_of_video_pid(pid,stream)) < 0)
1870 // Not a video PID perhaps audio ?
1871 if ((index_of_selected_pid = index_of_audio_pid(pid,stream)) < 0)
1873 // not a pid we want
1878 curstream = stream->ts_number_video_pids + index_of_selected_pid;
1882 curstream = index_of_selected_pid;
1886 start = (buf[1] & 0x40) != 0;
1888 if (!start && stream->ts_skipbad[curstream])
1892 int errorbit = (buf[1] & 0x80) != 0;
1895 hb_log("hb_ts_stream_decode - Error bit set in packet");
1896 stream->ts_skipbad[curstream] = 1;
1900 // Get adaption header info
1901 int adaption = (buf[3] & 0x30) >> 4;
1905 // Continuity only increments for adaption values of 0x3 or 0x01
1906 int continuity = (buf[3] & 0xF);
1907 if ((stream->ts_streamcont[curstream] != -1) && ((adaption & 0x01) != 0))
1909 if (continuity != ((stream->ts_streamcont[curstream] + 1) & 0xF))
1911 hb_log("hb_ts_stream_decode - Bad continuity code in packet");
1912 stream->ts_skipbad[curstream] = 1;
1915 stream->ts_streamcont[curstream] = continuity;
1918 // Get adaption header size
1921 hb_log("hb_ts_stream_decode - Bad adaption code (code was 0)!");
1922 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1924 stream->ts_skipbad[i] = 1;
1926 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 1;
1929 else if (adaption == 0x2)
1931 else if (adaption == 0x3)
1933 adapt_len = buf[4] + 1;
1934 if (adapt_len > 184)
1936 hb_log("hb_ts_stream_decode - Invalid adapt len (was > 183)!");
1937 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1939 stream->ts_skipbad[i] = 1;
1941 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 1;
1945 // HBO is slick, it doesn't bother to sync AC3 packets with PES elementary stream packets.. so
1946 // we have to swizzle them together! (ARGHH!)
1947 // if (pid == stream->ts_audio_pids[0] && start)
1948 if ((index_of_audio_pid(pid, stream) >= 0) && start)
1950 // Is there an AC3 packet start 0b77 code in this packet??
1952 unsigned char *p = buf + 4 + adapt_len;
1953 while (p <= buf + 186)
1955 if (p[0] == 0x0b && p[1] == 0x77)
1963 // Couldn't find an AC3 sync start in this packet.. don't make a PES packet!
1966 // int pos = ftell(fin);
1967 // error("AC3 packet sync not found in start frame");
1969 adapt_len += 9 + buf[4 + adapt_len + 8];
1974 // Found a random access point (now we can start a frame/audio packet..)
1977 // Check to see if this is an i_frame (group of picture start)
1978 if (pid == stream->ts_video_pids[0])
1980 // printf("Found Video Start for pid 0x%x\n", pid);
1981 // Look for the Group of Pictures packet.. indicates this is an I-Frame packet..
1983 unsigned int strid = 0;
1985 for (i = 4 + adapt_len; i < 188; i++)
1987 strid = (strid << 8) | buf[i];
1988 if (strid == 0x000001B8) // group_start_code
1990 // found a Group of Pictures header, subsequent picture must be an I-frame
1993 else if (strid == 0x000001B3) // sequence_header code
1997 else if (strid == 0x00000100) // picture_start_code
1999 // picture_header, let's see if it's an I-frame
2002 // int pic_start_code = (buf[i+2] >> 3) & 0x07;
2003 // hb_log("hb_ts_stream_decode - picture_start_code header value = 0x%x (%d)", pic_start_code, pic_start_code);
2004 // check if picture_coding_type == 1
2005 if ((buf[i+2] & (0x7 << 3)) == (1 << 3))
2007 // found an I-frame picture
2015 if (!stream->ts_foundfirst[curstream])
2017 stream->ts_foundfirst[curstream] = 1;
2018 // first_video_PCR = PCR;
2024 else if (index_of_audio_pid(pid, stream) >= 0)
2026 if (stream->ts_foundfirst[0]) // Set audio found first ONLY after first video frame found. There's an assumption here that stream '0' is a video stream
2028 stream->ts_foundfirst[curstream] |= 1;
2032 // If we were skipping a bad packet, start fresh on this new PES packet..
2033 if (stream->ts_skipbad[curstream] == 1)
2035 stream->ts_skipbad[curstream] = 0;
2036 stream->ts_packetpos[curstream] = 0;
2039 // Get the continuity code of this packet
2040 stream->ts_streamcont[curstream] = continuity;
2043 // Write a 2048 byte program stream packet..
2044 if (start && stream->ts_packetpos[curstream] > 0 && stream->ts_foundfirst[curstream] && !stream->ts_skipbad[curstream])
2046 // Save the substream id block so we can added it to subsequent blocks
2048 // if (pid == stream->ts_audio_pids[0] /*&& audstreamid == 0xBD*/)
2049 if (index_of_audio_pid(pid, stream) >= 0)
2051 // Curstream is a zero based index of streams and includes both video and audio streams, so we must subtract the numver of video streams
2052 // from the indes value used here since ts_audio_stream_type is indexed only by audio streams.
2053 if (stream->ts_audio_stream_type[curstream - stream->ts_number_video_pids] == 0x03)
2055 hb_ts_handle_mpeg_audio(stream, curstream, buf, adapt_len);
2059 write_ac3 = hb_ts_handle_ac3_audio(stream, curstream, buf, adapt_len);
2063 if (generate_output_data(stream, write_ac3, curstream, pid) != 0)
2067 // Add the payload for this packet to the current buffer
2068 if (stream->ts_foundfirst[curstream] && (184 - adapt_len) > 0)
2070 memcpy(stream->ts_packetbuf[curstream] + stream->ts_packetpos[curstream], buf + 4 + adapt_len, 184 - adapt_len);
2071 stream->ts_packetpos[curstream] += 184 - adapt_len;
2076 /***********************************************************************
2077 * hb_ts_stream_reset
2078 ***********************************************************************
2080 **********************************************************************/
2081 static void hb_ts_stream_reset(hb_stream_t *stream)
2084 for (i=0; i < kNumDecodeBuffers; i++)
2086 stream->ps_decode_buffer[i].read_pos = 0;
2087 stream->ps_decode_buffer[i].write_pos = 0;
2088 stream->ps_decode_buffer[i].len = 0;
2091 for (i=0; i < kMaxNumberDecodeStreams; i++)
2093 stream->ts_streamcont[i] = -1;
2096 stream->ps_current_write_buffer_index = 0;
2097 stream->ps_current_read_buffer_index = 1;
2099 align_to_next_packet(stream->file_handle);