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 // a legal MPEG program stream must start with a Pack header in the
116 return (buf[0] == 0x00) && (buf[1] == 0x00) &&
117 (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 );
191 /* open the file and see if it's a type we know about. return a stream
192 * reference structure if we can deal with it & NULL otherwise. */
193 if( ( d->file_handle = fopen( path, "rb" ) ) )
195 d->path = strdup( path );
196 if (d->path != NULL && hb_stream_get_type( d ) != 0 )
200 fclose( d->file_handle );
204 hb_log( "hb_stream_open: open %s failed", path );
209 /***********************************************************************
211 ***********************************************************************
212 * Closes and frees everything
213 **********************************************************************/
214 void hb_stream_close( hb_stream_t ** _d )
216 hb_stream_t * d = *_d;
220 fclose( d->file_handle );
221 d->file_handle = NULL;
225 for (i = 0; i < kNumDecodeBuffers; i++)
227 if (d->ps_decode_buffer[i].data)
229 free(d->ps_decode_buffer[i].data);
230 d->ps_decode_buffer[i].data = NULL;
234 for (i = 0; i < kMaxNumberDecodeStreams; i++)
236 if (d->ts_packetbuf[i])
238 free(d->ts_packetbuf[i]);
239 d->ts_packetbuf[i] = NULL;
247 /* when the file was first opened we made entries for all the audio elementary
248 * streams we found in it. Streams that were later found during the preview scan
249 * now have an audio codec, type, rate, etc., associated with them. At the end
250 * of the scan we delete all the audio entries that weren't found by the scan
251 * or don't have a format we support. This routine deletes audio entry 'indx'
252 * by copying all later entries down one slot. */
253 static void hb_stream_delete_audio_entry(hb_stream_t *stream, int indx)
257 for (i = indx+1; i < stream->ts_number_audio_pids; ++i)
259 stream->ts_audio_pids[indx] = stream->ts_audio_pids[i];
260 stream->ts_audio_stream_type[indx] = stream->ts_audio_stream_type[i];
263 --stream->ts_number_audio_pids;
266 /***********************************************************************
267 * hb_ps_stream_title_scan
268 ***********************************************************************
270 **********************************************************************/
271 hb_title_t * hb_stream_title_scan(hb_stream_t *stream)
274 hb_title_t *aTitle = hb_title_init( stream->path, 0 );
277 // Copy part of the stream path to the title name
278 char *sep = strrchr(stream->path, '/');
280 strcpy(aTitle->name, sep+1);
281 char *dot_term = strrchr(aTitle->name, '.');
285 // Height, width, rate and aspect ratio information is filled in when the previews are built
287 hb_stream_duration(stream, aTitle);
290 hb_chapter_t * chapter;
291 chapter = calloc( sizeof( hb_chapter_t ), 1 );
293 chapter->duration = aTitle->duration;
294 chapter->hours = aTitle->hours;
295 chapter->minutes = aTitle->minutes;
296 chapter->seconds = aTitle->seconds;
297 hb_list_add( aTitle->list_chapter, chapter );
299 // Figure out how many audio streams we really have:
300 // - For transport streams, for each PID listed in the PMT (whether
301 // or not it was an audio stream type) read the bitstream until we
302 // find an packet from that PID containing a PES header and see if
303 // the elementary stream is an audio type.
304 // - For program streams read the first 4MB and take every unique
305 // audio stream we find.
306 if (stream->stream_type == hb_stream_type_transport)
310 for (i=0; i < stream->ts_number_audio_pids; i++)
312 hb_audio_t *audio = hb_ts_stream_set_audio_id_and_codec(stream, i);
314 hb_list_add( aTitle->list_audio, audio );
318 hb_stream_delete_audio_entry(stream, i);
325 hb_ps_stream_find_audio_ids(stream, aTitle);
332 * scan the next MB of 'stream' to find the next start packet for
333 * the Packetized Elementary Stream associated with TS PID 'pid'.
335 static const uint8_t *hb_ts_stream_getPEStype(hb_stream_t *stream, uint32_t pid)
337 static uint8_t buf[188];
338 int npack = 100000; // max packets to read
342 if (fread(buf, 1, 188, stream->file_handle) != 188)
344 hb_log("hb_ts_stream_getPEStype: EOF while searching for PID 0x%x", pid);
349 hb_log("hb_ts_stream_getPEStype: lost sync while searching for PID 0x%x", pid);
350 align_to_next_packet(stream->file_handle);
355 * The PES header is only in TS packets with 'start' set so we check
356 * that first then check for the right PID.
358 if ((buf[1] & 0x40) == 0 || (buf[1] & 0x1f) != (pid >> 8) ||
359 buf[2] != (pid & 0xff))
361 // not a start packet or not the pid we want
365 /* skip over the TS hdr to return a pointer to the PES hdr */
367 switch (buf[3] & 0x30)
369 case 0x00: // illegal
370 case 0x20: // fill packet
373 case 0x30: // adaptation
376 hb_log("hb_ts_stream_getPEStype: invalid adaptation field length %d for PID 0x%x", buf[4], pid);
389 static uint64_t hb_ps_stream_getVideoPTS(hb_stream_t *stream)
391 hb_buffer_t *buf = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
392 hb_list_t *list = hb_list_init();
393 // how many blocks we read while searching for a video PES header
397 while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
401 // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
402 hb_demux_ps(buf, list);
404 while ( ( es = hb_list_item( list, 0 ) ) )
406 hb_list_rem( list, es );
407 if ( es->id == 0xe0 )
409 // this PES contains video - if there's a PTS we're done
410 // hb_demux_ps left the PTS in buf_es->start.
411 if ( es->start != ~0 )
418 hb_buffer_close( &es );
421 hb_list_empty( &list );
422 hb_buffer_close(&buf);
426 /***********************************************************************
428 ***********************************************************************
430 **********************************************************************/
432 uint64_t pos; /* file position of this PTS sample */
433 uint64_t pts; /* PTS from video stream */
436 static struct pts_pos hb_sample_pts(hb_stream_t *stream, uint64_t fpos)
438 struct pts_pos pp = { 0, 0 };
440 if ( stream->stream_type == hb_stream_type_program )
442 // round address down to nearest dvd sector start
443 fpos &=~ ( HB_DVD_READ_BUFFER_SIZE - 1 );
444 fseeko( stream->file_handle, fpos, SEEK_SET );
445 pp.pts = hb_ps_stream_getVideoPTS( stream );
450 fseeko( stream->file_handle, fpos, SEEK_SET );
451 align_to_next_packet( stream->file_handle );
452 buf = hb_ts_stream_getPEStype( stream, stream->ts_video_pids[0] );
455 hb_log("hb_sample_pts: couldn't find video packet near %llu", fpos);
458 if ( ( buf[7] >> 7 ) != 1 )
460 hb_log("hb_sample_pts: no PTS in video packet near %llu", fpos);
463 pp.pts = ( ( (uint64_t)buf[9] >> 1 ) & 7 << 30 ) |
464 ( (uint64_t)buf[10] << 22 ) |
465 ( ( (uint64_t)buf[11] >> 1 ) << 15 ) |
466 ( (uint64_t)buf[12] << 7 ) |
467 ( (uint64_t)buf[13] >> 1 );
469 pp.pos = ftello(stream->file_handle);
470 hb_log("hb_sample_pts: pts %lld at %llu", pp.pts, pp.pos );
474 static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle)
476 // To calculate the duration we get video presentation time stamps
477 // at a couple places in the file then use their difference scaled
478 // by the ratio of the distance between our measurement points &
479 // the size of the file. The issue is that our video file may have
480 // chunks from several different program fragments (main feature,
481 // commercials, station id, trailers, etc.) all with their own base
482 // pts value. We need to difference two pts's from the same program
483 // fragment. Since extraneous material is very likely at the beginning
484 // and end of the content, for now we take a time stamp from 25%
485 // into the file & 75% into the file then double their difference
486 // to get the total duration.
487 struct pts_pos first_pts, last_pts;
489 fseeko(stream->file_handle, 0, SEEK_END);
490 uint64_t fsize = ftello(stream->file_handle);
491 first_pts = hb_sample_pts(stream, fsize >> 2);
492 last_pts = hb_sample_pts(stream, fsize - (fsize >> 2) );
494 if ( first_pts.pos && last_pts.pos && first_pts.pos != last_pts.pos )
496 uint64_t dur = ( ( last_pts.pts - first_pts.pts ) * fsize ) /
497 ( last_pts.pos - first_pts.pos );
498 inTitle->duration = dur;
500 inTitle->hours = dur / 3600;
501 inTitle->minutes = ( dur % 3600 ) / 60;
502 inTitle->seconds = dur % 60;
504 rewind(stream->file_handle);
507 /***********************************************************************
509 ***********************************************************************
511 **********************************************************************/
512 int hb_stream_read( hb_stream_t * src_stream, hb_buffer_t * b )
514 if (src_stream->stream_type == hb_stream_type_program)
517 amt_read = fread(b->data, HB_DVD_READ_BUFFER_SIZE, 1, src_stream->file_handle);
523 else if (src_stream->stream_type == hb_stream_type_transport)
525 int read_buffer_index = src_stream->ps_current_read_buffer_index;
527 // Transport streams are a little more complex - we might be able to just
528 // read from the transport stream conversion buffer (if there's enough data)
529 // or we may need to transfer what's left and fill it again.
530 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)
532 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);
533 src_stream->ps_decode_buffer[read_buffer_index].read_pos += HB_DVD_READ_BUFFER_SIZE;
538 // Not quite enough data in the buffer - transfer what is present, fill the buffer and then
539 // transfer what's still needed.
540 int transfer_size = HB_DVD_READ_BUFFER_SIZE;
541 int amt_avail_to_transfer = src_stream->ps_decode_buffer[read_buffer_index].len - src_stream->ps_decode_buffer[read_buffer_index].read_pos;
542 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);
543 transfer_size -= amt_avail_to_transfer;
544 src_stream->ps_decode_buffer[read_buffer_index].read_pos += amt_avail_to_transfer;
546 // Give up this buffer - decoding may well need it, and we're done
547 src_stream->ps_decode_buffer[read_buffer_index].write_pos = 0;
548 src_stream->ps_decode_buffer[read_buffer_index].len = 0;
551 hb_ts_stream_decode(src_stream);
553 // Decoding will almost certainly have changed the current read buffer index
554 read_buffer_index = src_stream->ps_current_read_buffer_index;
556 if (src_stream->ps_decode_buffer[read_buffer_index].len == 0)
558 hb_log("hb_stream_read - buffer after decode has zero length data");
562 // Read the bit we still need
563 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);
564 src_stream->ps_decode_buffer[read_buffer_index].read_pos += transfer_size;
573 /***********************************************************************
575 ***********************************************************************
577 **********************************************************************/
578 int hb_stream_seek( hb_stream_t * src_stream, float f )
580 off_t stream_size, cur_pos, new_pos;
581 double pos_ratio = f;
582 cur_pos = ftello(src_stream->file_handle);
583 fseeko(src_stream->file_handle,0 ,SEEK_END);
584 stream_size = ftello(src_stream->file_handle);
585 new_pos = (off_t) ((double) (stream_size) * pos_ratio);
586 new_pos &=~ (HB_DVD_READ_BUFFER_SIZE - 1);
587 int r = fseeko(src_stream->file_handle, new_pos, SEEK_SET);
591 fseeko(src_stream->file_handle, cur_pos, SEEK_SET);
595 if (src_stream->stream_type == hb_stream_type_transport)
597 // We need to drop the current decoder output and move
598 // forwards to the next transport stream packet.
599 hb_ts_stream_reset(src_stream);
602 // Now we must scan forwards for a valid start code (0x000001BA)
604 hb_buffer_t *buf = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
607 if (hb_stream_read(src_stream,buf) == 1)
610 for (i=0; (i <= HB_DVD_READ_BUFFER_SIZE-4) && (!done); i++)
612 if ((buf->data[i] == 0x00) && (buf->data[i+1] == 0x00) && (buf->data[i+2] == 0x01) && (buf->data[i+3] == 0xba))
615 // 'Put Back' the data we've just read (up to this point)
616 hb_stream_put_back(src_stream, i);
621 done = 1; // End of data;
623 hb_buffer_close(&buf);
627 static hb_audio_t *hb_ts_stream_set_audio_id_and_codec(hb_stream_t *stream,
630 off_t cur_pos = ftello(stream->file_handle);
631 hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
634 fseeko(stream->file_handle, 0, SEEK_SET);
635 align_to_next_packet(stream->file_handle);
636 buf = hb_ts_stream_getPEStype(stream, stream->ts_audio_pids[aud_pid_index]);
638 /* check that we found a PES header */
639 if (buf && buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
643 audio->id = 0x80bd | (aud_pid_index << 8);
644 audio->codec = HB_ACODEC_AC3;
645 hb_log("transport stream pid 0x%x (type 0x%x) is AC-3 audio id 0x%x",
646 stream->ts_audio_pids[aud_pid_index],
647 stream->ts_audio_stream_type[aud_pid_index],
649 stream->ts_audio_stream_type[aud_pid_index] = 0x81;
651 else if ((buf[3] & 0xe0) == 0xc0)
654 audio->codec = HB_ACODEC_MPGA;
655 hb_log("transport stream pid 0x%x (type 0x%x) is MPEG audio id 0x%x",
656 stream->ts_audio_pids[aud_pid_index],
657 stream->ts_audio_stream_type[aud_pid_index],
659 stream->ts_audio_stream_type[aud_pid_index] = 0x03;
662 fseeko(stream->file_handle, cur_pos, SEEK_SET);
665 hb_log("transport stream pid 0x%x (type 0x%x) isn't audio",
666 stream->ts_audio_pids[aud_pid_index],
667 stream->ts_audio_stream_type[aud_pid_index]);
672 static void add_audio_to_title(hb_title_t *title, int id)
674 hb_audio_t *audio = calloc( sizeof( hb_audio_t ), 1 );
680 audio->codec = HB_ACODEC_MPGA;
681 hb_log("add_audio_to_title: added MPEG audio stream 0x%x", id);
684 // type 2 is a DVD subtitle stream - just ignore it */
688 audio->codec = HB_ACODEC_AC3;
689 hb_log("add_audio_to_title: added AC3 audio stream 0x%x", id);
692 audio->codec = HB_ACODEC_LPCM;
693 hb_log("add_audio_to_title: added LPCM audio stream 0x%x", id);
696 hb_log("add_audio_to_title: unknown audio stream type 0x%x", id);
701 hb_list_add( title->list_audio, audio );
704 static void hb_ps_stream_find_audio_ids(hb_stream_t *stream, hb_title_t *title)
706 off_t cur_pos = ftello(stream->file_handle);
707 hb_buffer_t *buf = hb_buffer_init(HB_DVD_READ_BUFFER_SIZE);
708 hb_list_t *list = hb_list_init();
709 // how many blocks we read while searching for audio streams
711 // there can be at most 16 unique streams in an MPEG PS (8 in a DVD)
712 // so we use a bitmap to keep track of the ones we've already seen.
713 // Bit 'i' of smap is set if we've already added the audio for
714 // audio substream id 'i' to the title's audio list.
717 // start looking 20% into the file since there's occasionally no
718 // audio at the beginning (particularly for vobs).
719 hb_stream_seek(stream, 0.2f);
721 while (--blksleft >= 0 && hb_stream_read(stream, buf) == 1)
725 // 'buf' contains an MPEG2 PACK - get a list of all it's elementary streams
726 hb_demux_ps(buf, list);
728 while ( ( es = hb_list_item( list, 0 ) ) )
730 hb_list_rem( list, es );
731 if ( (es->id & 0xff) == 0xbd || (es->id & 0xe0) == 0xc0 )
733 // this PES contains some kind of audio - get the substream id
734 // and check if we've seen it already.
735 int ssid = (es->id > 0xff ? es->id >> 8 : es->id) & 0xf;
736 if ( (smap & (1 << ssid)) == 0 )
738 // we haven't seen this stream before - add it to the
739 // title's list of audio streams.
741 add_audio_to_title(title, es->id);
744 hb_buffer_close( &es );
747 hb_list_empty( &list );
748 hb_buffer_close(&buf);
749 fseeko(stream->file_handle, cur_pos, SEEK_SET);
752 /***********************************************************************
753 * hb_stream_update_audio
754 ***********************************************************************
756 **********************************************************************/
757 void hb_stream_update_audio(hb_stream_t *stream, hb_audio_t *audio)
761 if (stream->stream_type == hb_stream_type_transport)
763 // Find the audio stream info for this PID
764 int i = (audio->id >> 8) & 0xf;
765 if (i >= stream->ts_number_audio_pids)
767 hb_log("hb_stream_update_audio: no PID for audio stream 0x%x",
772 lang = lang_for_code(stream->a52_info[i].lang_code);
774 audio->rate = stream->a52_info[i].rate;
776 audio->bitrate = stream->a52_info[i].bitrate;
777 if (!audio->config.a52.ac3flags)
778 audio->config.a52.ac3flags = audio->ac3flags = stream->a52_info[i].flags;
783 // XXX should try to get language code from the AC3 bitstream
784 lang = lang_for_code(0x0000);
787 if (!audio->input_channel_layout)
789 switch( audio->ac3flags & A52_CHANNEL_MASK )
795 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_MONO;
800 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
802 /* dolby (DPL1 aka Dolby Surround = 4.0 matrix-encoded) input */
804 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_DOLBY;
808 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F2R;
812 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F1R;
816 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_3F;
819 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F1R;
822 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_2F2R;
826 audio->input_channel_layout = HB_INPUT_CH_LAYOUT_STEREO;
829 /* add in our own LFE flag if the source has LFE */
830 if (audio->ac3flags & A52_LFE)
832 audio->input_channel_layout = audio->input_channel_layout | HB_INPUT_CH_LAYOUT_HAS_LFE;
836 snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)", strlen(lang->native_name) ? lang->native_name : lang->eng_name,
837 audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec == HB_ACODEC_MPGA ? "MPEG" : ( audio->codec == HB_ACODEC_DCA ? "DTS" : "LPCM" ) ) );
838 snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s", strlen(lang->native_name) ? lang->native_name : lang->eng_name );
839 snprintf( audio->iso639_2, sizeof( audio->iso639_2 ), "%s", lang->iso639_2);
841 if ( (audio->ac3flags & A52_CHANNEL_MASK) == A52_DOLBY ) {
842 sprintf( audio->lang + strlen( audio->lang ),
843 " (Dolby Surround)" );
845 sprintf( audio->lang + strlen( audio->lang ),
847 HB_INPUT_CH_LAYOUT_GET_DISCRETE_FRONT_COUNT(audio->input_channel_layout) +
848 HB_INPUT_CH_LAYOUT_GET_DISCRETE_REAR_COUNT(audio->input_channel_layout),
849 HB_INPUT_CH_LAYOUT_GET_DISCRETE_LFE_COUNT(audio->input_channel_layout));
852 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 );
856 /***********************************************************************
858 ***********************************************************************
860 **********************************************************************/
861 static void hb_stream_put_back(hb_stream_t *stream, int i)
863 if (stream->stream_type == hb_stream_type_program)
865 // Program streams are pretty easy - we just reposition the source file
867 fseeko(stream->file_handle, -(HB_DVD_READ_BUFFER_SIZE-i), SEEK_CUR);
869 else if (stream->stream_type == hb_stream_type_transport)
871 int read_buffer_index = stream->ps_current_read_buffer_index;
873 // Transport streams are a little more tricky - so long as the
874 // amount to back up is still within the current decode buffer
875 // we can just adjust the read pos.
876 if (stream->ps_decode_buffer[read_buffer_index].read_pos - i > 0)
878 stream->ps_decode_buffer[read_buffer_index].read_pos -= i;
881 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);
886 /***********************************************************************
888 ***********************************************************************
890 **********************************************************************/
891 #define PS_DECODE_BUFFER_SIZE ( 1024 * 1024 * 4)
893 static void hb_ts_stream_init(hb_stream_t *stream)
895 // Output Program Stream
897 for (i=0; i < kNumDecodeBuffers; i++)
899 stream->ps_decode_buffer[i].data = (unsigned char *) malloc(PS_DECODE_BUFFER_SIZE);
900 stream->ps_decode_buffer[i].read_pos = 0;
901 stream->ps_decode_buffer[i].size = PS_DECODE_BUFFER_SIZE;
902 stream->ps_decode_buffer[i].len = 0;
903 stream->ps_decode_buffer[i].write_pos = 0;
906 for (i=0; i < kMaxNumberDecodeStreams; i++)
908 stream->ts_streamcont[i] = -1;
911 stream->ps_current_write_buffer_index = 0;
912 stream->ps_current_read_buffer_index = 1;
914 // Find the audio and video pids in the stream
915 hb_ts_stream_find_pids(stream);
917 for (i=0; i < stream->ts_number_video_pids; i++)
919 // In progress audio/video data during the transport stream -> program stream processing
920 stream->ts_packetbuf[i] = (unsigned char *) malloc(1024 * 1024);
921 stream->ts_streamid[i] = 0xE0; // Stream is Video
924 for (i = stream->ts_number_video_pids; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
926 stream->ts_packetbuf[i] = (unsigned char *) malloc(1024 * 1024);
927 stream->ts_streamid[i] = 0xBD; // Stream 1 is AC-3 Audio
931 // ------------------------------------------------------------------------------------
933 static off_t align_to_next_packet(FILE* f)
935 unsigned char buf[188*20];
937 off_t start = ftello(f);
940 if (fread(buf, 188*20, 1, f) == 1)
943 while (!found && (pos < 188))
947 for (i = 0; i < 188*20; i += 188)
949 unsigned char c = buf[pos+i];
951 if ((c != 0x47) && (c != 0x72) && (c != 0x29))
953 // this offset failed, try next
963 pos = 0; // failed to find anything!!!!!?
965 fseeko(f, start+pos, SEEK_SET);
970 // ------------------------------------------------------------------------------------
973 unsigned int bitval = 0;
974 unsigned char* bitbuf = NULL;
975 unsigned int bitmask[] = {
976 0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,
977 0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff,0xffff,
978 0x1ffff,0x3ffff,0x7ffff,0xfffff,0x1fffff,0x3fffff,0x7fffff,0xffffff,
979 0x1ffffff,0x3ffffff,0x7ffffff,0xfffffff,0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
981 static inline void set_buf(unsigned char* buf, int bufsize, int clear)
985 bitval = (bitbuf[0] << 24) | (bitbuf[1] << 16) | (bitbuf[2] << 8) | bitbuf[3];
987 memset(bitbuf, 0, bufsize);
990 static inline int buf_size()
995 static inline void set_bits(unsigned int val, int bits)
997 val &= bitmask[bits];
1001 int bitsleft = (8 - (bitpos & 7));
1002 if (bits >= bitsleft)
1004 bitbuf[bitpos >> 3] |= val >> (bits - bitsleft);
1007 val &= bitmask[bits];
1011 bitbuf[bitpos >> 3] |= val << (bitsleft - bits);
1018 static inline unsigned int get_bits(int bits)
1021 int left = 32 - (bitpos & 31);
1025 val = (bitval >> (left - bits)) & bitmask[bits];
1030 val = (bitval & bitmask[left]) << (bits - left);
1034 int pos = bitpos >> 3;
1035 bitval = (bitbuf[pos] << 24) | (bitbuf[pos + 1] << 16) | (bitbuf[pos + 2] << 8) | bitbuf[pos + 3];
1039 val |= (bitval >> (32 - bits)) & bitmask[bits];
1047 // extract what useful information we can from the elementary stream
1048 // descriptor list at 'dp' and add it to the stream at 'esindx'.
1049 // Descriptors with info we don't currently use are ignored.
1050 // The descriptor list & descriptor item formats are defined in
1051 // ISO 13818-1 (2000E) section 2.6 (pg. 62).
1052 static void decode_element_descriptors(hb_stream_t* stream, int esindx,
1053 const uint8_t *dp, uint8_t dlen)
1055 const uint8_t *ep = dp + dlen;
1061 case 10: // ISO_639_language descriptor
1062 stream->a52_info[esindx].lang_code = lang_to_code(lang_for_code2((const char *)&dp[2]));
1072 int decode_program_map(hb_stream_t* stream)
1074 set_buf(stream->pmt_info.tablebuf, stream->pmt_info.tablepos, 0);
1076 unsigned char table_id = get_bits(8);
1078 unsigned int section_length = get_bits(12);
1079 stream->pmt_info.section_length = section_length;
1081 unsigned int program_number = get_bits(16);
1082 stream->pmt_info.program_number = program_number;
1084 unsigned char version_number = get_bits(5);
1086 unsigned char section_number = get_bits(8);
1087 unsigned char last_section_number = get_bits(8);
1089 unsigned int PCR_PID = get_bits(13);
1090 stream->pmt_info.PCR_PID = PCR_PID;
1092 unsigned int program_info_length = get_bits(12);
1093 stream->pmt_info.program_info_length = program_info_length;
1096 unsigned char *descriptor_buf = (unsigned char *) malloc(program_info_length);
1097 for (i = 0; i < program_info_length; i++)
1099 descriptor_buf[i] = get_bits(8);
1102 int cur_pos = 9 /* data after the section length field*/ + program_info_length;
1103 int done_reading_stream_types = 0;
1104 while (!done_reading_stream_types)
1106 unsigned char stream_type = get_bits(8);
1108 unsigned int elementary_PID = get_bits(13);
1110 unsigned int ES_info_length = get_bits(12);
1113 unsigned char *ES_info_buf = (unsigned char *) malloc(ES_info_length);
1114 for (i=0; i < ES_info_length; i++)
1116 ES_info_buf[i] = get_bits(8);
1119 if (stream_type == 0x02)
1121 if (stream->ts_number_video_pids <= kMaxNumberVideoPIDS)
1122 stream->ts_number_video_pids++;
1123 stream->ts_video_pids[stream->ts_number_video_pids-1] = elementary_PID;
1127 // Defined audio stream types are 0x81 for AC-3/A52 audio and 0x03
1128 // for mpeg audio. But content producers seem to use other
1129 // values (0x04 and 0x06 have both been observed) so at this point
1130 // we say everything that isn't a video pid is audio then at the end
1131 // of hb_stream_title_scan we'll figure out which are really audio
1132 // by looking at the PES headers.
1133 i = stream->ts_number_audio_pids;
1134 if (i < kMaxNumberAudioPIDS)
1135 stream->ts_number_audio_pids++;
1136 stream->ts_audio_pids[i] = elementary_PID;
1137 stream->ts_audio_stream_type[i] = stream_type;
1139 if (ES_info_length > 0)
1141 decode_element_descriptors(stream, i, ES_info_buf, ES_info_length);
1145 cur_pos += 5 /* stream header */ + ES_info_length;
1149 if (cur_pos >= section_length - 4 /* stop before the CRC */)
1150 done_reading_stream_types = 1;
1153 free(descriptor_buf);
1157 // ------------------------------------------------------------------------------------
1159 int build_program_map(unsigned char *buf, hb_stream_t *stream)
1161 // Get adaption header info
1163 int adaption = (buf[3] & 0x30) >> 4;
1166 else if (adaption == 0x2)
1168 else if (adaption == 0x3)
1169 adapt_len = buf[4] + 1;
1170 if (adapt_len > 184)
1173 // Get payload start indicator
1175 start = (buf[1] & 0x40) != 0;
1177 // Get pointer length - only valid in packets with a start flag
1178 int pointer_len = 0;
1179 if (start && stream->pmt_info.reading)
1181 // We just finished a bunch of packets - parse the program map details
1183 if (stream->pmt_info.tablebuf[0] == 0x02)
1184 decode_ok = decode_program_map(stream);
1185 free(stream->pmt_info.tablebuf);
1186 stream->pmt_info.tablebuf = NULL;
1187 stream->pmt_info.tablepos = 0;
1188 stream->pmt_info.reading = 0;
1195 pointer_len = buf[4 + adapt_len] + 1;
1196 stream->pmt_info.tablepos = 0;
1198 // Get Continuity Counter
1199 int continuity_counter = buf[3] & 0x0f;
1200 if (!start && (stream->pmt_info.current_continuity_counter + 1 != continuity_counter))
1202 hb_log("build_program_map - Continuity Counter %d out of sequence - expected %d", continuity_counter, stream->pmt_info.current_continuity_counter+1);
1205 stream->pmt_info.current_continuity_counter = continuity_counter;
1206 stream->pmt_info.reading |= start;
1208 // Add the payload for this packet to the current buffer
1209 int amount_to_copy = 184 - adapt_len - pointer_len;
1210 if (stream->pmt_info.reading && (amount_to_copy > 0))
1212 stream->pmt_info.tablebuf = realloc(stream->pmt_info.tablebuf, stream->pmt_info.tablepos + amount_to_copy);
1214 memcpy(stream->pmt_info.tablebuf + stream->pmt_info.tablepos, buf + 4 + adapt_len + pointer_len, amount_to_copy);
1215 stream->pmt_info.tablepos += amount_to_copy;
1221 int decode_PAT(unsigned char *buf, hb_stream_t *stream)
1223 unsigned char tablebuf[1024];
1224 unsigned int tablepos = 0;
1229 // Get adaption header info
1231 int adaption = (buf[3] & 0x30) >> 4;
1234 else if (adaption == 0x2)
1236 else if (adaption == 0x3)
1237 adapt_len = buf[4] + 1;
1238 if (adapt_len > 184)
1241 // Get pointer length
1242 int pointer_len = buf[4 + adapt_len] + 1;
1244 // Get payload start indicator
1246 start = (buf[1] & 0x40) != 0;
1251 // Add the payload for this packet to the current buffer
1252 if (reading && (184 - adapt_len) > 0)
1254 if (tablepos + 184 - adapt_len - pointer_len > 1024)
1256 hb_log("decode_PAT - Bad program section length (> 1024)");
1259 memcpy(tablebuf + tablepos, buf + 4 + adapt_len + pointer_len, 184 - adapt_len - pointer_len);
1260 tablepos += 184 - adapt_len - pointer_len;
1263 if (start && reading)
1265 memcpy(tablebuf + tablepos, buf + 4 + adapt_len + 1, pointer_len - 1);
1268 unsigned int pos = 0;
1269 //while (pos < tablepos)
1271 set_buf(tablebuf + pos, tablepos - pos, 0);
1273 unsigned char section_id = get_bits(8);
1275 unsigned int section_len = get_bits(12);
1276 unsigned int transport_id = get_bits(16);
1278 unsigned int version_num = get_bits(5);
1279 unsigned int current_next = get_bits(1);
1280 unsigned int section_num = get_bits(8);
1281 unsigned int last_section = get_bits(8);
1287 // Program Association Section
1288 section_len -= 5; // Already read transport stream ID, version num, section num, and last section num
1289 section_len -= 4; // Ignore the CRC
1291 stream->ts_number_pat_entries = 0;
1292 while ((curr_pos < section_len) && (stream->ts_number_pat_entries < kMaxNumberPMTStreams))
1294 unsigned int pkt_program_num = get_bits(16);
1295 stream->pat_info[stream->ts_number_pat_entries].program_number = pkt_program_num;
1297 get_bits(3); // Reserved
1298 if (pkt_program_num == 0)
1300 unsigned int pkt_network_PID = get_bits(13);
1304 unsigned int pkt_program_map_PID = get_bits(13);
1305 stream->pat_info[stream->ts_number_pat_entries].program_map_PID = pkt_program_map_PID;
1308 stream->ts_number_pat_entries++;
1322 pos += 3 + section_len;
1330 static int flushbuf(hb_stream_t *stream)
1332 int old_write_index = stream->ps_current_write_buffer_index;
1334 // Flip the buffers and start moving on to the next
1335 stream->ps_current_write_buffer_index++;
1336 if (stream->ps_current_write_buffer_index > kNumDecodeBuffers-1)
1337 stream->ps_current_write_buffer_index = 0;
1339 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) )
1341 hb_log("flushbuf - new buffer (index %d) has non zero length and write position !", stream->ps_current_write_buffer_index);
1345 stream->ps_current_read_buffer_index = old_write_index;
1346 stream->ps_decode_buffer[stream->ps_current_read_buffer_index].read_pos = 0;
1351 static int fwrite64(void* buf, int elsize, int elnum, hb_stream_t* stream)
1358 int current_write_index = stream->ps_current_write_buffer_index;
1360 if (size <= stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos)
1362 memcpy(stream->ps_decode_buffer[current_write_index].data + stream->ps_decode_buffer[current_write_index].write_pos, buf, size);
1363 stream->ps_decode_buffer[current_write_index].write_pos += size;
1364 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1369 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);
1370 written += stream->ps_decode_buffer[current_write_index].size - stream->ps_decode_buffer[current_write_index].write_pos;
1371 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;
1372 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1374 if (flushbuf(stream))
1376 // FLushing the buffer will have change the current write buffer
1377 current_write_index = stream->ps_current_write_buffer_index;
1379 memcpy(stream->ps_decode_buffer[current_write_index].data, (unsigned char*)buf + written, size - written);
1380 stream->ps_decode_buffer[current_write_index].write_pos += size - written;
1381 stream->ps_decode_buffer[current_write_index].len = stream->ps_decode_buffer[current_write_index].write_pos;
1382 written += size - written;
1387 if (elnum == 1 && written == size)
1390 return written / elsize;
1393 static int write_pack(hb_stream_t* stream, int64_t time)
1395 unsigned char buf[64];
1396 set_buf(buf, 64, 1); // clear buffer
1398 int64_t ext_time = time % 300;
1401 set_bits(0x000001ba, 32); // pack id 32
1402 set_bits(1, 2); // 0x01 2
1403 set_bits((unsigned int)(time >> 30), 3); // system_clock_reference_base 3
1404 set_bits(1, 1); // marker_bit 1
1405 set_bits((unsigned int)(time >> 15), 15); // system_clock_reference_base 15
1406 set_bits(1, 1); // marker_bit 1
1407 set_bits((unsigned int)time, 15); // system_clock_reference_base1 15
1408 set_bits(1, 1); // marker_bit 1
1409 set_bits((unsigned int)ext_time, 9); // system_clock_reference_extension 9
1410 set_bits(1, 1); // marker_bit 1
1411 set_bits(DEMUX, 22); // program_mux_rate 22
1412 set_bits(1, 1); // marker_bit 1
1413 set_bits(1, 1); // marker_bit 1
1414 set_bits(31, 5); // reserved 5
1415 set_bits(0, 3); // pack_stuffing_length 3
1417 return fwrite64(buf, buf_size(), 1, stream) == 1;
1420 static int pad_buffer(hb_stream_t *stream, int pad)
1425 buf[0] = '\x0'; buf[1] = '\x0'; buf[2] = '\x1'; buf[3] = '\xbe';
1426 buf[4] = pad >> 8; buf[5] = pad & 0xff;
1428 if (fwrite64(buf, 6, 1, stream) != 1)
1431 unsigned char padbyte = 0xff;
1433 for (i = 0; i < pad; i++)
1435 if (fwrite64(&padbyte, 1, 1, stream) != 1)
1442 int make_pes_header(unsigned char* buf, int streamid, int len, int64_t PTS, int64_t DTS)
1445 int PTS_DTS_flags = 0;
1460 set_buf(buf, 9 + hdrlen, 1); // clear the buffer
1462 set_bits(0x000001, 24); // packet_start_code_prefix 24
1463 set_bits((unsigned int)streamid, 8); // directory_stream_id 8
1464 set_bits(len, 16); // PES_packet_length 16
1465 set_bits(0x2, 2); // '10' 2
1466 set_bits(0, 2); // PES_scrambling_control 2
1467 set_bits(1, 1); // PES_priority 1
1468 set_bits(0, 1); // data_alignment_indicator 1
1469 set_bits(0, 1); // copyright 1
1470 set_bits(0, 1); // original_or_copy 1
1471 set_bits(PTS_DTS_flags, 2); // PTS_DTS_flags 2
1472 set_bits(0, 1); // ESCR_flag 1
1473 set_bits(0, 1); // ES_rate_flag 1
1474 set_bits(0, 1); // DSM_trick_mode_flag 1
1475 set_bits(0, 1); // additional_copy_info_flag 1
1476 set_bits(0, 1); // PES_CRC_flag 1
1477 set_bits(0, 1); // PES_extension_flag 1
1478 set_bits(hdrlen, 8); // PES_header_data_length 8
1480 if (PTS_DTS_flags == 2)
1482 set_bits(2, 4); // '0010' 4
1483 set_bits((unsigned int)(PTS >> 30), 3); // PTS [32..30] 3
1484 set_bits(1, 1); // marker bit 1
1485 set_bits((unsigned int)(PTS >> 15), 15); // PTS [29..15] 15
1486 set_bits(1, 1); // marker bit 1
1487 set_bits((unsigned int)PTS, 15); // PTS [14..0] 15
1488 set_bits(1, 1); // marker bit 1
1490 else if (PTS_DTS_flags == 3)
1492 set_bits(3, 4); // '0011' 4
1493 set_bits((unsigned int)(PTS >> 30), 3); // PTS [32..30] 3
1494 set_bits(1, 1); // marker bit 1
1495 set_bits((unsigned int)(PTS >> 15), 15); // PTS [29..15] 15
1496 set_bits(1, 1); // marker bit 1
1497 set_bits((unsigned int)PTS, 15); // PTS [14..0] 15
1498 set_bits(1, 1); // marker bit 1
1499 set_bits(1, 4); // '0001' 4
1500 set_bits((unsigned int)(DTS >> 30), 3); // DTS [32..30] 3
1501 set_bits(1, 1); // marker bit 1
1502 set_bits((unsigned int)(DTS >> 15), 15); // DTS [29..15] 15
1503 set_bits(1, 1); // marker bit 1
1504 set_bits((unsigned int)DTS, 15); // DTS [14..0] 15
1505 set_bits(1, 1); // marker bit 1
1511 int generate_output_data(hb_stream_t *stream, int write_ac3, int curstream, int pid)
1513 unsigned char ac3_substream_id[4];
1518 // Make a four byte DVD ac3 stream header
1519 int ssid = (curstream - stream->ts_number_video_pids) & 0xf;
1520 ac3_substream_id[0] = 0x80 | ssid; // substream id
1521 ac3_substream_id[1] = 0x01; // number of sync words
1522 ac3_substream_id[2] = 0x00; // first offset (16 bits)
1523 ac3_substream_id[3] = 0x02;
1527 int written = 0; // Bytes we've written to output file
1528 int pos = 0; // Position in PES packet buffer
1532 if ((stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len % HB_DVD_READ_BUFFER_SIZE) != 0)
1534 hb_log("write_output_stream - Packet's not falling on read buffer size boundries!");
1538 // Get total length of this pack
1539 int len = min(14 + ac3len + stream->ts_packetpos[curstream] - pos, HB_DVD_READ_BUFFER_SIZE);
1541 // Figure out stuffing (if we have less than 16 bytes left)
1543 if (len < HB_DVD_READ_BUFFER_SIZE && HB_DVD_READ_BUFFER_SIZE - len < 16)
1545 stuffing = HB_DVD_READ_BUFFER_SIZE - len;
1549 // Write out pack header
1550 off_t file_offset = ftello(stream->file_handle);
1551 int64_t packet_time = (file_offset * CLOCKRATE / STREAMRATE) + 0 /*file_time*/;
1552 if (!write_pack(stream, packet_time))
1554 hb_log("write_output_stream - Couldn't write pack header!");
1558 int index_of_selected_pid = -1;
1559 if ((index_of_selected_pid = index_of_video_pid(pid,stream)) < 0)
1561 if ((index_of_selected_pid = index_of_audio_pid(pid,stream)) < 0)
1563 hb_log("generate_output_data - cannot find pid 0x%x (%d) in selected audio or video pids", pid, pid);
1568 stream->ts_packetbuf[curstream][pos + 3] = stream->ts_streamid[stream->ts_number_video_pids + index_of_selected_pid];
1572 stream->ts_packetbuf[curstream][pos + 3] = stream->ts_streamid[index_of_selected_pid];
1575 // Subtract pack size (14) and pes id and len (6) from lenth
1576 stream->ts_packetbuf[curstream][pos + 4] = (len - 6 - 14) >> 8; stream->ts_packetbuf[curstream][pos + 5] = (len - 6 - 14) & 0xFF;
1578 // Add any stuffing bytes to header extra len
1579 int hdrsize = 9 + stream->ts_packetbuf[curstream][pos + 8];
1580 stream->ts_packetbuf[curstream][pos + 8] += stuffing; // Add stuffing to header bytes
1582 // Write out id, streamid, len
1583 if (fwrite64(stream->ts_packetbuf[curstream] + pos, hdrsize, 1, stream) != 1) // Write pes id, streamid, and len
1585 hb_log("write_output_stream - Failed to write output file!");
1591 for (i = 0; i < stuffing; i++) // Write any stuffing bytes
1593 unsigned char stuff = 0xff;
1594 if (fwrite64(&stuff, 1, 1, stream) != 1)
1596 hb_log("write_output_stream - Failed to write output file!");
1601 // Write ac3 streamid
1604 if (fwrite64(ac3_substream_id, ac3len, 1, stream) != 1)
1606 hb_log("write_output_stream - Failed to write output file!");
1611 // Write rest of data len minus headersize (9) stuffing, and pack size (14)
1612 if (fwrite64(stream->ts_packetbuf[curstream] + pos + hdrsize, len - hdrsize - 14 - stuffing - ac3len, 1, stream) != 1) // Write data bytes
1614 hb_log("write_output_stream - Failed to write output file!");
1619 // Add len minus stuff we added like the pack (14) and the stuffing.
1620 pos += len - 14 - stuffing - ac3len;
1621 if (pos == stream->ts_packetpos[curstream])
1624 // Add pes header for next packet
1626 // 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);
1627 make_pes_header(stream->ts_packetbuf[curstream] + pos, stream->ts_streamid[curstream], 0, -1, -1);
1631 if ((written % HB_DVD_READ_BUFFER_SIZE) != 0)
1633 int left = HB_DVD_READ_BUFFER_SIZE - (written % HB_DVD_READ_BUFFER_SIZE);
1635 // Pad out to HB_DVD_READ_BUFFER_SIZE bytes
1636 if (!pad_buffer(stream, left))
1638 hb_log("write_output_stream - Couldn't write pad buffer!");
1643 stream->ts_packetpos[curstream] = 0;
1644 stream->ts_streamcont[curstream] = -1;
1649 static void hb_ts_handle_mpeg_audio(hb_stream_t *stream, int curstream, unsigned char* buf, int adapt_len )
1651 // Although we don't have AC3/A52 audio here we can still use the same structure to record this useful information.
1653 stream->a52_info[curstream - stream->ts_number_video_pids].flags = A52_STEREO;
1654 stream->a52_info[curstream - stream->ts_number_video_pids].rate = 48000 /*Hz*/;
1655 stream->a52_info[curstream - stream->ts_number_video_pids].bitrate = 384000 /*Bps*/;
1658 static int hb_ts_handle_ac3_audio(hb_stream_t *stream, int curstream, unsigned char* buf, int adapt_len )
1662 // Make sure we start with 0x0b77
1663 if (stream->ts_packetbuf[curstream][9 + stream->ts_packetbuf[curstream][8]] != 0x0b || stream->ts_packetbuf[curstream][9 + stream->ts_packetbuf[curstream][8] + 1] != 0x77)
1665 spos = 9 + stream->ts_packetbuf[curstream][8];
1666 dpos = 9 + stream->ts_packetbuf[curstream][8];
1667 while (spos <= stream->ts_packetpos[curstream] - 2 && !(stream->ts_packetbuf[curstream][spos] == 0x0b && stream->ts_packetbuf[curstream][spos + 1] == 0x77))
1670 if (!(stream->ts_packetbuf[curstream][spos] == 0x0b && stream->ts_packetbuf[curstream][spos + 1] == 0x77))
1672 hb_log("hb_ts_stream_decode - Couldn't sync AC3 packet!");
1673 stream->ts_skipbad[curstream] = 1;
1677 while (spos < stream->ts_packetpos[curstream])
1679 stream->ts_packetbuf[curstream][dpos] = stream->ts_packetbuf[curstream][spos];
1683 stream->ts_packetpos[curstream] = dpos;
1686 // Check the next packet to make sure IT starts with a 0x0b77
1688 // if (buf[4 + adapt_len] == 0 && buf[4 + adapt_len + 1] == 0 && // Starting with an mpeg header?
1689 // buf[4 + adapt_len + 2] == 1 && buf[4 + adapt_len + 3] == 0xBD)
1690 plen = 9 + buf[4 + adapt_len + 8];
1691 int pstart = 4 + adapt_len + plen;
1692 if (buf[pstart] != 0x0b || buf[pstart + 1] != 0x77)
1695 while (spos < 188 - 2 && !(buf[spos] == 0x0b && buf[spos + 1] == 0x77))
1697 stream->ts_packetbuf[curstream][stream->ts_packetpos[curstream]] = buf[spos];
1698 stream->ts_packetpos[curstream]++;
1702 if (!(buf[spos] == 0x0b && buf[spos + 1] == 0x77))
1704 hb_log("hb_ts_stream_decode - Couldn't sync AC3 packet!");
1705 stream->ts_skipbad[curstream] = 1;
1709 adapt_len = spos - 4 - plen;
1713 while (spos >= pstart - plen)
1715 buf[dpos] = buf[spos];
1721 int flags, rate, bitrate;
1722 if( a52_syncinfo( &buf[pstart], &flags, &rate, &bitrate ) )
1724 stream->a52_info[curstream - stream->ts_number_video_pids].flags = flags;
1725 stream->a52_info[curstream - stream->ts_number_video_pids].rate = rate;
1726 stream->a52_info[curstream - stream->ts_number_video_pids].bitrate = bitrate;
1731 static void hb_ts_stream_find_pids(hb_stream_t *stream)
1733 unsigned char buf[188];
1735 // align to first packet
1736 align_to_next_packet(stream->file_handle);
1738 // Read the Transport Stream Packets (188 bytes each) looking at first for PID 0 (the PAT PID), then decode that
1739 // to find the program map PID and then decode that to get the list of audio and video PIDs
1741 int bytesReadInPacket = 0;
1744 // Try to read packet..
1746 if ((bytesRead = fread(buf+bytesReadInPacket, 1, 188-bytesReadInPacket, stream->file_handle)) != 188-bytesReadInPacket)
1750 bytesReadInPacket += bytesRead;
1752 hb_log("hb_ts_stream_find_pids - end of file");
1757 bytesReadInPacket = 0;
1761 if ((buf[0] != 0x47) && (buf[0] != 0x72) && (buf[0] != 0x29))
1763 hb_log("hb_ts_stream_find_pids - Bad transport packet (no sync byte 0x47)!");
1765 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1766 stream->ts_skipbad[i] = 1;
1771 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
1773 if ((pid == 0x0000) && (stream->ts_number_pat_entries == 0))
1775 decode_PAT(buf, stream);
1780 for (pat_index = 0; pat_index < stream->ts_number_pat_entries; pat_index++)
1782 // There are some streams where the PAT table has multiple entries as if their are
1783 // multiple programs in the same transport stream, and yet there's actually only one
1784 // program really in the stream. This seems to be true for transport streams that
1785 // originate in the HDHomeRun but have been output by EyeTV's export utility. What I think
1786 // is happening is that the HDHomeRun is sending the entire transport stream as broadcast,
1787 // but the EyeTV is only recording a single (selected) program number and not rewriting the
1788 // PAT info on export to match what's actually on the stream.
1789 // Until we have a way of handling multiple programs per transport stream elegantly we'll match
1790 // on the first pat entry for which we find a matching program map PID. The ideal solution would
1791 // be to build a title choice popup from the PAT program number details and then select from
1792 // their - but right now the API's not capable of that.
1793 if (pid == stream->pat_info[pat_index].program_map_PID)
1795 if (build_program_map(buf, stream) > 0)
1799 // Keep going until we have a complete set of PIDs
1800 if ((stream->ts_number_video_pids > 0) && (stream->ts_number_audio_pids > 0))
1804 hb_log("hb_ts_stream_find_pids - found the following PIDS");
1805 hb_log(" Video PIDS : ");
1807 for (i=0; i < stream->ts_number_video_pids; i++)
1809 hb_log(" 0x%x (%d)", stream->ts_video_pids[i], stream->ts_video_pids[i]);
1811 hb_log(" Audio PIDS : ");
1812 for (i = 0; i < stream->ts_number_audio_pids; i++)
1814 hb_log(" 0x%x (%d)", stream->ts_audio_pids[i], stream->ts_audio_pids[i]);
1818 int index_of_video_pid(int pid, hb_stream_t *stream)
1820 int found_pid = -1, i = 0;
1822 for (i = 0; (i < stream->ts_number_video_pids) && (found_pid < 0); i++)
1824 if (pid == stream->ts_video_pids[i])
1830 int index_of_audio_pid(int pid, hb_stream_t *stream)
1832 int i = 0, found_pid = -1;
1834 for (i = 0; (i < stream->ts_number_audio_pids) && (found_pid < 0); i++)
1836 if (pid == stream->ts_audio_pids[i])
1842 int index_of_pid(int pid, hb_stream_t *stream)
1846 if ((found_pid = index_of_video_pid(pid, stream)) >= 0)
1849 if ((found_pid = index_of_audio_pid(pid, stream)) >= 0)
1855 /***********************************************************************
1856 * hb_ts_stream_decode
1857 ***********************************************************************
1859 **********************************************************************/
1860 static void hb_ts_stream_decode(hb_stream_t *stream)
1862 unsigned char buf[188];
1867 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1869 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 0;
1870 stream->ts_skipbad[i] = 0;
1875 if ((stream->ts_number_video_pids == 0) || (stream->ts_number_audio_pids == 0))
1877 hb_log("hb_ts_stream_decode - no Video or Audio PID selected, cannot decode transport stream");
1881 int curr_write_buffer_index = stream->ps_current_write_buffer_index;
1883 // Write output data until a buffer switch occurs.
1884 while (curr_write_buffer_index == stream->ps_current_write_buffer_index)
1886 if ((fread(buf, 188, 1, stream->file_handle)) != 1)
1888 // end of file - we didn't finish filling our ps write buffer
1889 // so just discard the remainder (the partial buffer is useless)
1890 hb_log("hb_ts_stream_decode - eof");
1891 stream->ps_decode_buffer[stream->ps_current_write_buffer_index].len = 0;
1896 if ((buf[0] != 0x47) && (buf[0] != 0x72) && (buf[0] != 0x29))
1898 off_t pos = ftello(stream->file_handle);
1899 hb_log("hb_ts_stream_decode - no sync byte 0x47 @ %lld", pos);
1900 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1902 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 1;
1903 stream->ts_skipbad[i] = 1;
1909 int pid = (((buf[1] & 0x1F) << 8) | buf[2]) & 0x1FFF;
1911 // Get the pos and buf - we organize our streams as 'n' video streams then 'm' audio streams
1912 int index_of_selected_pid;
1913 if ((index_of_selected_pid = index_of_video_pid(pid,stream)) < 0)
1915 // Not a video PID perhaps audio ?
1916 if ((index_of_selected_pid = index_of_audio_pid(pid,stream)) < 0)
1918 // not a pid we want
1923 curstream = stream->ts_number_video_pids + index_of_selected_pid;
1927 curstream = index_of_selected_pid;
1931 start = (buf[1] & 0x40) != 0;
1933 if (!start && stream->ts_skipbad[curstream])
1937 int errorbit = (buf[1] & 0x80) != 0;
1940 hb_log("hb_ts_stream_decode - Error bit set in packet");
1941 stream->ts_skipbad[curstream] = 1;
1945 // Get adaption header info
1946 int adaption = (buf[3] & 0x30) >> 4;
1950 // Continuity only increments for adaption values of 0x3 or 0x01
1951 int continuity = (buf[3] & 0xF);
1952 if ((stream->ts_streamcont[curstream] != -1) && ((adaption & 0x01) != 0))
1954 if (continuity != ((stream->ts_streamcont[curstream] + 1) & 0xF))
1956 hb_log("hb_ts_stream_decode - Bad continuity code in packet");
1957 stream->ts_skipbad[curstream] = 1;
1960 stream->ts_streamcont[curstream] = continuity;
1963 // Get adaption header size
1966 hb_log("hb_ts_stream_decode - Bad adaption code (code was 0)!");
1967 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1969 stream->ts_skipbad[i] = 1;
1971 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 1;
1974 else if (adaption == 0x2)
1976 else if (adaption == 0x3)
1978 adapt_len = buf[4] + 1;
1979 if (adapt_len > 184)
1981 hb_log("hb_ts_stream_decode - Invalid adapt len (was > 183)!");
1982 for (i=0; i < stream->ts_number_video_pids + stream->ts_number_audio_pids; i++)
1984 stream->ts_skipbad[i] = 1;
1986 // stream->ts_skipbad[kAudioStream] = stream->ts_skipbad[kVideoStream] = 1;
1990 // HBO is slick, it doesn't bother to sync AC3 packets with PES elementary stream packets.. so
1991 // we have to swizzle them together! (ARGHH!)
1992 // if (pid == stream->ts_audio_pids[0] && start)
1993 if ((index_of_audio_pid(pid, stream) >= 0) && start)
1995 // Is there an AC3 packet start 0b77 code in this packet??
1997 unsigned char *p = buf + 4 + adapt_len;
1998 while (p <= buf + 186)
2000 if (p[0] == 0x0b && p[1] == 0x77)
2008 // Couldn't find an AC3 sync start in this packet.. don't make a PES packet!
2011 // int pos = ftell(fin);
2012 // error("AC3 packet sync not found in start frame");
2014 adapt_len += 9 + buf[4 + adapt_len + 8];
2019 // Found a random access point (now we can start a frame/audio packet..)
2022 // Check to see if this is an i_frame (group of picture start)
2023 if (pid == stream->ts_video_pids[0])
2025 // printf("Found Video Start for pid 0x%x\n", pid);
2026 // Look for the Group of Pictures packet.. indicates this is an I-Frame packet..
2028 unsigned int strid = 0;
2030 for (i = 4 + adapt_len; i < 188; i++)
2032 strid = (strid << 8) | buf[i];
2033 if (strid == 0x000001B8) // group_start_code
2035 // found a Group of Pictures header, subsequent picture must be an I-frame
2038 else if (strid == 0x000001B3) // sequence_header code
2042 else if (strid == 0x00000100) // picture_start_code
2044 // picture_header, let's see if it's an I-frame
2047 // int pic_start_code = (buf[i+2] >> 3) & 0x07;
2048 // hb_log("hb_ts_stream_decode - picture_start_code header value = 0x%x (%d)", pic_start_code, pic_start_code);
2049 // check if picture_coding_type == 1
2050 if ((buf[i+2] & (0x7 << 3)) == (1 << 3))
2052 // found an I-frame picture
2060 if (!stream->ts_foundfirst[curstream])
2062 stream->ts_foundfirst[curstream] = 1;
2063 // first_video_PCR = PCR;
2069 else if (index_of_audio_pid(pid, stream) >= 0)
2071 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
2073 stream->ts_foundfirst[curstream] |= 1;
2077 // If we were skipping a bad packet, start fresh on this new PES packet..
2078 if (stream->ts_skipbad[curstream] == 1)
2080 stream->ts_skipbad[curstream] = 0;
2081 stream->ts_packetpos[curstream] = 0;
2084 // Get the continuity code of this packet
2085 stream->ts_streamcont[curstream] = continuity;
2088 // Write a 2048 byte program stream packet..
2089 if (start && stream->ts_packetpos[curstream] > 0 && stream->ts_foundfirst[curstream] && !stream->ts_skipbad[curstream])
2091 // Save the substream id block so we can added it to subsequent blocks
2093 // if (pid == stream->ts_audio_pids[0] /*&& audstreamid == 0xBD*/)
2094 if (index_of_audio_pid(pid, stream) >= 0)
2096 // Curstream is a zero based index of streams and includes both video and audio streams, so we must subtract the numver of video streams
2097 // from the indes value used here since ts_audio_stream_type is indexed only by audio streams.
2098 if (stream->ts_audio_stream_type[curstream - stream->ts_number_video_pids] == 0x03)
2100 hb_ts_handle_mpeg_audio(stream, curstream, buf, adapt_len);
2104 write_ac3 = hb_ts_handle_ac3_audio(stream, curstream, buf, adapt_len);
2108 if (generate_output_data(stream, write_ac3, curstream, pid) != 0)
2112 // Add the payload for this packet to the current buffer
2113 if (stream->ts_foundfirst[curstream] && (184 - adapt_len) > 0)
2115 memcpy(stream->ts_packetbuf[curstream] + stream->ts_packetpos[curstream], buf + 4 + adapt_len, 184 - adapt_len);
2116 stream->ts_packetpos[curstream] += 184 - adapt_len;
2121 /***********************************************************************
2122 * hb_ts_stream_reset
2123 ***********************************************************************
2125 **********************************************************************/
2126 static void hb_ts_stream_reset(hb_stream_t *stream)
2129 for (i=0; i < kNumDecodeBuffers; i++)
2131 stream->ps_decode_buffer[i].read_pos = 0;
2132 stream->ps_decode_buffer[i].write_pos = 0;
2133 stream->ps_decode_buffer[i].len = 0;
2136 for (i=0; i < kMaxNumberDecodeStreams; i++)
2138 stream->ts_streamcont[i] = -1;
2141 stream->ps_current_write_buffer_index = 0;
2142 stream->ps_current_read_buffer_index = 1;
2144 align_to_next_packet(stream->file_handle);