forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.c
6483 lines (5856 loc) · 206 KB
/
stream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* stream.c
Copyright (c) 2003-2024 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "handbrake/handbrake.h"
#include "handbrake/hbffmpeg.h"
#include "handbrake/lang.h"
#include "handbrake/extradata.h"
#include "libbluray/bluray.h"
#define min(a, b) a < b ? a : b
#define HB_MAX_PROBE_SIZE (1*1024*1024)
#define HB_MAX_PROBES 3
/*
* This table defines how ISO MPEG stream type codes map to HandBrake
* codecs. It is indexed by the 8 bit stream type and contains the codec
* worker object id and a parameter for that worker proc (ignored except
* for the ffmpeg-based codecs in which case it is the ffmpeg codec id).
*
* Entries with a worker proc id of 0 or a kind of 'U' indicate that HB
* doesn't handle the stream type.
* N - Not used
* U - Unknown (to be determined by further processing)
* A - Audio
* V - Video
* S - Subtitle
* P - PCR
*/
typedef enum { U, N, A, V, P, S } kind_t;
typedef struct {
kind_t kind; /* not handled / unknown / audio / video */
int codec; /* HB worker object id of codec */
int codec_param; /* param for codec (usually ffmpeg codec id) */
const char* name; /* description of type */
} stream2codec_t;
#define st(id, kind, codec, codec_param, name) \
[id] = { kind, codec, codec_param, name }
static const stream2codec_t st2codec[256] = {
st(0x00, U, 0, 0, NULL),
st(0x01, V, WORK_DECAVCODECV, AV_CODEC_ID_MPEG1VIDEO, "MPEG1"),
st(0x02, V, WORK_DECAVCODECV, AV_CODEC_ID_MPEG2VIDEO, "MPEG2"),
st(0x03, A, HB_ACODEC_MP2, AV_CODEC_ID_MP2, "MPEG1"),
st(0x04, A, HB_ACODEC_MP2, AV_CODEC_ID_MP2, "MPEG2"),
st(0x05, N, 0, 0, "ISO 13818-1 private section"),
st(0x06, U, 0, 0, "ISO 13818-1 PES private data"),
st(0x07, N, 0, 0, "ISO 13522 MHEG"),
st(0x08, N, 0, 0, "ISO 13818-1 DSM-CC"),
st(0x09, N, 0, 0, "ISO 13818-1 auxiliary"),
st(0x0a, N, 0, 0, "ISO 13818-6 encap"),
st(0x0b, N, 0, 0, "ISO 13818-6 DSM-CC U-N msgs"),
st(0x0c, N, 0, 0, "ISO 13818-6 Stream descriptors"),
st(0x0d, N, 0, 0, "ISO 13818-6 Sections"),
st(0x0e, N, 0, 0, "ISO 13818-1 auxiliary"),
st(0x0f, A, HB_ACODEC_FFAAC, AV_CODEC_ID_AAC, "AAC"),
st(0x10, V, WORK_DECAVCODECV, AV_CODEC_ID_MPEG4, "MPEG4"),
st(0x11, A, HB_ACODEC_FFMPEG, AV_CODEC_ID_AAC_LATM, "LATM AAC"),
st(0x12, U, 0, 0, "MPEG4 generic"),
st(0x14, N, 0, 0, "ISO 13818-6 DSM-CC download"),
st(0x1b, V, WORK_DECAVCODECV, AV_CODEC_ID_H264, "H.264"),
st(0x80, U, HB_ACODEC_FFMPEG, AV_CODEC_ID_PCM_BLURAY, "Digicipher II Video"),
st(0x81, A, HB_ACODEC_AC3, AV_CODEC_ID_AC3, "AC3"),
st(0x82, A, HB_ACODEC_DCA, AV_CODEC_ID_DTS, "DTS"),
// 0x83 can be LPCM or BD TrueHD. Set to 'unknown' till we know more.
st(0x83, U, HB_ACODEC_LPCM, 0, "LPCM"),
// BD E-AC3 Primary audio
st(0x84, U, 0, 0, "SDDS"),
st(0x85, U, 0, 0, "ATSC Program ID"),
// 0x86 can be BD DTS-HD/DTS. Set to 'unknown' till we know more.
st(0x86, U, HB_ACODEC_DCA_HD, AV_CODEC_ID_DTS, "DTS-HD MA"),
st(0x87, A, HB_ACODEC_FFEAC3, AV_CODEC_ID_EAC3, "E-AC3"),
st(0x8a, A, HB_ACODEC_DCA, AV_CODEC_ID_DTS, "DTS"),
st(0x90, S, WORK_DECAVSUB, AV_CODEC_ID_HDMV_PGS_SUBTITLE, "PGS Subtitle"),
// 0x91 can be AC3 or BD Interactive Graphics Stream.
st(0x91, U, 0, 0, "AC3/IGS"),
st(0x92, N, 0, 0, "Subtitle"),
st(0x94, U, 0, 0, "SDDS"),
st(0xa0, V, 0, 0, "MSCODEC"),
// BD E-AC3 Secondary audio
st(0xa1, U, 0, 0, "E-AC3"),
// BD DTS-HD Secondary audio
st(0xa2, U, HB_ACODEC_DCA_HD, AV_CODEC_ID_DTS, "DTS-HD LBR"),
st(0xea, V, WORK_DECAVCODECV, AV_CODEC_ID_VC1, "VC-1"),
};
#undef st
typedef enum {
hb_stream_type_unknown = 0,
transport,
program,
ffmpeg
} hb_stream_type_t;
#define MAX_PS_PROBE_SIZE (32*1024*1024)
#define kMaxNumberPMTStreams 32
typedef struct
{
uint8_t has_stream_id_ext;
uint8_t stream_id;
uint8_t stream_id_ext;
uint8_t bd_substream_id;
int64_t pts;
int64_t dts;
int64_t scr;
int header_len;
int packet_len;
int stuffing_len;
} hb_pes_info_t;
typedef struct {
hb_buffer_t * buf;
hb_pes_info_t pes_info;
int8_t pes_info_valid;
int packet_len;
int packet_offset;
int8_t skipbad;
int8_t continuity;
uint8_t pkt_summary[8];
int pid;
uint8_t is_pcr;
int pes_list;
int start;
} hb_ts_stream_t;
typedef struct {
int map_idx;
int stream_id;
uint8_t stream_id_ext;
uint8_t stream_type;
kind_t stream_kind;
int lang_code;
uint32_t format_id;
#define TS_FORMAT_ID_AC3 (('A' << 24) | ('C' << 16) | ('-' << 8) | '3')
int codec; // HB worker object id of codec
int codec_param; // param for codec (usually ffmpeg codec id)
char codec_name[80];
int next; // next pointer for list
// hb_ts_stream_t points to a list of
// hb_pes_stream_t
hb_buffer_t * probe_buf;
int probe_next_size;
int probe_count;
uint8_t * extradata;
int extradata_size;
} hb_pes_stream_t;
struct hb_stream_s
{
hb_handle_t * h;
int scan;
int frames; /* video frames so far */
int errors; /* total errors so far */
int last_error_frame; /* frame # at last error message */
int last_error_count; /* # errors at last error message */
int packetsize; /* Transport Stream packet size */
int need_keyframe; // non-zero if want to start at a keyframe
int chapter; /* Chapter that we are currently in */
int64_t chapter_end; /* HB time that the current chapter ends */
struct
{
int discontinuity;
uint8_t found_pcr; // non-zero if we've found at least one pcr
int64_t pcr; // most recent input pcr
int64_t last_timestamp; // used for discontinuity detection when
// there are no PCRs
uint8_t *packet; // buffer for one TS packet
hb_ts_stream_t *list;
int count;
int alloc;
} ts;
struct
{
uint8_t found_scr; // non-zero if we've found at least one scr
int64_t scr; // most recent input scr
hb_pes_stream_t *list;
int count;
int alloc;
} pes;
/*
* Stuff before this point is dynamic state updated as we read the
* stream. Stuff after this point is stream description state that
* we learn during the initial scan but cache so it can be
* reused during the conversion read.
*/
uint8_t has_IDRs; // # IDRs found during duration scan
uint8_t ts_flags; // stream characteristics:
#define TS_HAS_PCR (1 << 0) // at least one PCR seen
#define TS_HAS_RAP (1 << 1) // Random Access Point bit seen
#define TS_HAS_RSEI (1 << 2) // "Restart point" SEI seen
char *path;
FILE *file_handle;
hb_stream_type_t hb_stream_type;
hb_title_t *title;
AVFormatContext *ffmpeg_ic;
AVPacket *ffmpeg_pkt;
uint8_t ffmpeg_video_id;
uint32_t reg_desc; // 4 byte registration code that identifies
// stream semantics
struct
{
unsigned short program_number;
unsigned short program_map_PID;
} pat_info[kMaxNumberPMTStreams];
int ts_number_pat_entries;
struct
{
int reading;
unsigned char *tablebuf;
unsigned int tablepos;
unsigned char current_continuity_counter;
unsigned int PCR_PID;
} pmt_info;
};
typedef struct {
uint8_t *buf;
uint32_t val;
int pos;
int size;
} bitbuf_t;
/***********************************************************************
* Local prototypes
**********************************************************************/
static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle);
static off_t align_to_next_packet(hb_stream_t *stream);
static int64_t pes_timestamp( const uint8_t *pes );
static int hb_ts_stream_init(hb_stream_t *stream);
static hb_buffer_t * hb_ts_stream_decode(hb_stream_t *stream);
static void hb_init_audio_list(hb_stream_t *stream, hb_title_t *title);
static void hb_init_subtitle_list(hb_stream_t *stream, hb_title_t *title);
static int hb_ts_stream_find_pids(hb_stream_t *stream);
static void hb_ps_stream_init(hb_stream_t *stream);
static hb_buffer_t * hb_ps_stream_decode(hb_stream_t *stream);
static void hb_ps_stream_find_streams(hb_stream_t *stream);
static int hb_ps_read_packet( hb_stream_t * stream, hb_buffer_t *b );
static int update_ps_streams( hb_stream_t * stream, int stream_id, int stream_id_ext, int stream_type, int in_kind );
static int update_ts_streams( hb_stream_t * stream, int pid, int stream_id_ext, int stream_type, int in_kind, int *pes_idx );
static void update_pes_kind( hb_stream_t * stream, int idx );
static int ffmpeg_open( hb_stream_t *stream, hb_title_t *title, int scan );
static void ffmpeg_close( hb_stream_t *d );
static hb_title_t *ffmpeg_title_scan( hb_stream_t *stream, hb_title_t *title );
hb_buffer_t *hb_ffmpeg_read( hb_stream_t *stream );
static int ffmpeg_seek( hb_stream_t *stream, float frac );
static int ffmpeg_seek_ts( hb_stream_t *stream, int64_t ts );
static inline unsigned int bits_get(bitbuf_t *bb, int bits);
static inline void bits_init(bitbuf_t *bb, uint8_t* buf, int bufsize, int clear);
static inline unsigned int bits_peek(bitbuf_t *bb, int bits);
static void pes_add_audio_to_title(hb_stream_t *s, int i, hb_title_t *t, int sort);
static int hb_parse_ps( hb_stream_t *stream, uint8_t *buf, int len, hb_pes_info_t *pes_info );
static void hb_ts_resolve_pid_types(hb_stream_t *stream);
static void hb_ps_resolve_stream_types(hb_stream_t *stream);
void hb_ts_stream_reset(hb_stream_t *stream);
void hb_ps_stream_reset(hb_stream_t *stream);
/*
* logging routines.
* these frontend hb_log because transport streams can have a lot of errors
* so we want to rate limit messages. this routine limits the number of
* messages to at most one per minute of video. other errors that occur
* during the minute are counted & the count is output with the next
* error msg we print.
*/
static void ts_warn_helper( hb_stream_t *stream, char *log, va_list args )
{
// limit error printing to at most one per minute of video (at 30fps)
++stream->errors;
if ( stream->frames - stream->last_error_frame >= 30*60 )
{
char msg[256];
vsnprintf( msg, sizeof(msg), log, args );
if ( stream->errors - stream->last_error_count < 10 )
{
hb_log( "stream: error near frame %d: %s", stream->frames, msg );
}
else
{
int Edelta = stream->errors - stream->last_error_count;
double Epcnt = (double)Edelta * 100. /
(stream->frames - stream->last_error_frame);
hb_log( "stream: %d new errors (%.0f%%) up to frame %d: %s",
Edelta, Epcnt, stream->frames, msg );
}
stream->last_error_frame = stream->frames;
stream->last_error_count = stream->errors;
}
}
static void ts_warn( hb_stream_t*, char*, ... ) HB_WPRINTF(2,3);
static void ts_err( hb_stream_t*, int, char*, ... ) HB_WPRINTF(3,4);
static void ts_warn( hb_stream_t *stream, char *log, ... )
{
va_list args;
va_start( args, log );
ts_warn_helper( stream, log, args );
va_end( args );
}
static int get_id(hb_pes_stream_t *pes)
{
return ( pes->stream_id_ext << 16 ) + pes->stream_id;
}
static int index_of_id(hb_stream_t *stream, int id)
{
int i;
for ( i = 0; i < stream->pes.count; ++i )
{
if ( id == get_id( &stream->pes.list[i] ) )
return i;
}
return -1;
}
static int index_of_pid(hb_stream_t *stream, int pid)
{
int i;
for ( i = 0; i < stream->ts.count; ++i )
{
if ( pid == stream->ts.list[i].pid )
{
return i;
}
}
return -1;
}
static int index_of_ps_stream(hb_stream_t *stream, int id, int sid)
{
int i;
for ( i = 0; i < stream->pes.count; ++i )
{
if ( id == stream->pes.list[i].stream_id &&
sid == stream->pes.list[i].stream_id_ext )
{
return i;
}
}
// If there is no match on the stream_id_ext, try matching
// on only the stream_id.
for ( i = 0; i < stream->pes.count; ++i )
{
if ( id == stream->pes.list[i].stream_id &&
0 == stream->pes.list[i].stream_id_ext )
{
return i;
}
}
return -1;
}
static kind_t ts_stream_kind( hb_stream_t * stream, int idx )
{
if ( stream->ts.list[idx].pes_list != -1 )
{
// Returns kind for the first pes substream in the pes list
// All substreams in a TS stream are the same kind.
return stream->pes.list[stream->ts.list[idx].pes_list].stream_kind;
}
else
{
return U;
}
}
static kind_t ts_stream_type( hb_stream_t * stream, int idx )
{
if ( stream->ts.list[idx].pes_list != -1 )
{
// Returns stream type for the first pes substream in the pes list
// All substreams in a TS stream are the same stream type.
return stream->pes.list[stream->ts.list[idx].pes_list].stream_type;
}
else
{
return 0x00;
}
}
static int pes_index_of_video(hb_stream_t *stream)
{
int i;
for ( i = 0; i < stream->pes.count; ++i )
if ( V == stream->pes.list[i].stream_kind )
return i;
return -1;
}
static int ts_index_of_video(hb_stream_t *stream)
{
int i;
for ( i = 0; i < stream->ts.count; ++i )
if ( V == ts_stream_kind( stream, i ) )
return i;
return -1;
}
static void ts_err( hb_stream_t *stream, int curstream, char *log, ... )
{
va_list args;
va_start( args, log );
ts_warn_helper( stream, log, args );
va_end( args );
if (curstream >= 0)
{
stream->ts.list[curstream].skipbad = 1;
stream->ts.list[curstream].continuity = -1;
}
}
static int check_ps_sync(const uint8_t *buf)
{
// a legal MPEG program stream must start with a Pack header in the
// first four bytes.
return (buf[0] == 0x00) && (buf[1] == 0x00) &&
(buf[2] == 0x01) && (buf[3] == 0xba);
}
static int check_ps_sc(const uint8_t *buf)
{
// a legal MPEG program stream must start with a Pack followed by a
// some other start code. If we've already verified the pack, this skip
// it and checks for a start code prefix.
int pos;
int mark = buf[4] >> 4;
if ( mark == 0x02 )
{
// Check other marker bits to make it less likely
// that we are being spoofed.
if( ( buf[4] & 0xf1 ) != 0x21 ||
( buf[6] & 0x01 ) != 0x01 ||
( buf[8] & 0x01 ) != 0x01 ||
( buf[9] & 0x80 ) != 0x80 ||
( buf[11] & 0x01 ) != 0x01 )
{
return 0;
}
// mpeg-1 pack header
pos = 12; // skip over the PACK
}
else
{
// Check other marker bits to make it less likely
// that we are being spoofed.
if( ( buf[4] & 0xC4 ) != 0x44 ||
( buf[6] & 0x04 ) != 0x04 ||
( buf[8] & 0x04 ) != 0x04 ||
( buf[9] & 0x01 ) != 0x01 ||
( buf[12] & 0x03 ) != 0x03 )
{
return 0;
}
// mpeg-2 pack header
pos = 14 + ( buf[13] & 0x7 ); // skip over the PACK
}
return (buf[pos+0] == 0x00) && (buf[pos+1] == 0x00) && (buf[pos+2] == 0x01);
}
static int check_ts_sync(const uint8_t *buf)
{
// must have initial sync byte & a legal adaptation ctrl
return (buf[0] == 0x47) && (((buf[3] & 0x30) >> 4) > 0);
}
static int have_ts_sync(const uint8_t *buf, int psize, int count)
{
int ii;
for ( ii = 0; ii < count; ii++ )
{
if ( !check_ts_sync(&buf[ii*psize]) )
return 0;
}
return 1;
}
static int hb_stream_check_for_ts(const uint8_t *buf)
{
// transport streams should have a sync byte every 188 bytes.
// search the first 8KB of buf looking for at least 8 consecutive
// correctly located sync patterns.
int offset = 0;
int count = 16;
for ( offset = 0; offset < 8*1024-count*188; ++offset )
{
if ( have_ts_sync( &buf[offset], 188, count) )
return 188 | (offset << 8);
if ( have_ts_sync( &buf[offset], 192, count) )
return 192 | (offset << 8);
if ( have_ts_sync( &buf[offset], 204, count) )
return 204 | (offset << 8);
if ( have_ts_sync( &buf[offset], 208, count) )
return 208 | (offset << 8);
}
return 0;
}
static int hb_stream_check_for_ps(hb_stream_t *stream)
{
uint8_t buf[2048*4];
uint8_t sc_buf[4];
int pos = 0;
fseek(stream->file_handle, 0, SEEK_SET);
// program streams should start with a PACK then some other mpeg start
// code (usually a SYS but that might be missing if we only have a clip).
while (pos < 512 * 1024)
{
int offset;
if ( fread(buf, 1, sizeof(buf), stream->file_handle) != sizeof(buf) )
return 0;
for ( offset = 0; offset < 8*1024-27; ++offset )
{
if ( check_ps_sync( &buf[offset] ) && check_ps_sc( &buf[offset] ) )
{
int pes_offset, prev, data_len;
uint8_t sid;
uint8_t *b = buf+offset;
// Skip the pack header
int mark = buf[4] >> 4;
if ( mark == 0x02 )
{
// mpeg-1 pack header
pes_offset = 12;
}
else
{
// mpeg-2 pack header
pes_offset = 14 + ( buf[13] & 0x7 );
}
b += pes_offset;
// Get the next stream id
sid = b[3];
data_len = (b[4] << 8) + b[5];
if ( data_len && sid > 0xba && sid < 0xf9 )
{
prev = ftell( stream->file_handle );
pos = prev - ( sizeof(buf) - offset );
pos += pes_offset + 6 + data_len;
fseek( stream->file_handle, pos, SEEK_SET );
if ( fread(sc_buf, 1, 4, stream->file_handle) != 4 )
return 0;
if (sc_buf[0] == 0x00 && sc_buf[1] == 0x00 &&
sc_buf[2] == 0x01)
{
return 1;
}
fseek( stream->file_handle, prev, SEEK_SET );
}
}
}
fseek( stream->file_handle, -27, SEEK_CUR );
pos = ftell( stream->file_handle );
}
return 0;
}
static int hb_stream_get_type(hb_stream_t *stream)
{
uint8_t buf[2048*4];
if ( fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) )
{
int psize;
if ( ( psize = hb_stream_check_for_ts(buf) ) != 0 )
{
int offset = psize >> 8;
psize &= 0xff;
hb_log("file is MPEG Transport Stream with %d byte packets"
" offset %d bytes", psize, offset);
stream->packetsize = psize;
stream->hb_stream_type = transport;
if (hb_ts_stream_init(stream) == 0)
return 1;
}
else if ( hb_stream_check_for_ps(stream) != 0 )
{
hb_log("file is MPEG Program Stream");
stream->hb_stream_type = program;
hb_ps_stream_init(stream);
// We default to mpeg codec for ps streams if no
// video found in program stream map
return 1;
}
}
return 0;
}
static void hb_stream_delete_dynamic( hb_stream_t *d )
{
if( d->file_handle )
{
fclose( d->file_handle );
d->file_handle = NULL;
}
int i=0;
if ( d->ts.packet )
{
free( d->ts.packet );
d->ts.packet = NULL;
}
if ( d->ts.list )
{
for (i = 0; i < d->ts.count; i++)
{
if (d->ts.list[i].buf)
{
hb_buffer_close(&(d->ts.list[i].buf));
d->ts.list[i].buf = NULL;
}
}
}
if ( d->pes.list )
{
for (i = 0; i < d->pes.count; i++)
{
if (d->pes.list[i].extradata)
{
free(d->pes.list[i].extradata);
}
}
}
}
static void hb_stream_delete( hb_stream_t *d )
{
hb_stream_delete_dynamic( d );
free( d->ts.list );
free( d->pes.list );
free( d->path );
free( d );
}
static int audio_inactive( hb_stream_t *stream, int id, int stream_id_ext )
{
if ( id < 0 )
{
// PID declared inactive by hb_stream_title_scan
return 1;
}
if ( id == stream->pmt_info.PCR_PID )
{
// PCR PID is always active
return 0;
}
int i;
for ( i = 0; i < hb_list_count( stream->title->list_audio ); ++i )
{
hb_audio_t *audio = hb_list_item( stream->title->list_audio, i );
if ( audio->id == ((stream_id_ext << 16) | id) )
{
return 0;
}
}
return 1;
}
/* when the file was first opened we made entries for all the audio elementary
* streams we found in it. Streams that were later found during the preview scan
* now have an audio codec, type, rate, etc., associated with them. At the end
* of the scan we delete all the audio entries that weren't found by the scan
* or don't have a format we support. This routine deletes audio entry 'indx'
* by setting its PID to an invalid value so no packet will match it. (We can't
* move any of the entries since the index of the entry is used as the id
* of the media stream for HB. */
static void hb_stream_delete_ts_entry(hb_stream_t *stream, int indx)
{
if ( stream->ts.list[indx].pid > 0 )
{
stream->ts.list[indx].pid = -stream->ts.list[indx].pid;
}
}
static int hb_stream_try_delete_ts_entry(hb_stream_t *stream, int indx)
{
int ii;
if ( stream->ts.list[indx].pid < 0 )
return 1;
for ( ii = stream->ts.list[indx].pes_list; ii != -1;
ii = stream->pes.list[ii].next )
{
if ( stream->pes.list[ii].stream_id >= 0 )
return 0;
}
stream->ts.list[indx].pid = -stream->ts.list[indx].pid;
return 1;
}
static void hb_stream_delete_ps_entry(hb_stream_t *stream, int indx)
{
if ( stream->pes.list[indx].stream_id > 0 )
{
stream->pes.list[indx].stream_id = -stream->pes.list[indx].stream_id;
}
}
static void prune_streams(hb_stream_t *d)
{
if ( d->hb_stream_type == transport )
{
int ii, jj;
for ( ii = 0; ii < d->ts.count; ii++)
{
// If probing didn't find audio or video, and the pid
// is not the PCR, remove the track
if ( ts_stream_kind ( d, ii ) == U &&
!d->ts.list[ii].is_pcr )
{
hb_stream_delete_ts_entry(d, ii);
continue;
}
if ( ts_stream_kind ( d, ii ) == A )
{
for ( jj = d->ts.list[ii].pes_list; jj != -1;
jj = d->pes.list[jj].next )
{
if ( audio_inactive( d, d->pes.list[jj].stream_id,
d->pes.list[jj].stream_id_ext ) )
{
hb_stream_delete_ps_entry(d, jj);
}
}
if ( !d->ts.list[ii].is_pcr &&
hb_stream_try_delete_ts_entry(d, ii) )
{
continue;
}
}
}
// reset to beginning of file and reset some stream
// state information
hb_stream_seek( d, 0. );
}
else if ( d->hb_stream_type == program )
{
int ii;
for ( ii = 0; ii < d->pes.count; ii++)
{
// If probing didn't find audio or video, remove the track
if ( d->pes.list[ii].stream_kind == U )
{
hb_stream_delete_ps_entry(d, ii);
}
if ( d->pes.list[ii].stream_kind == A &&
audio_inactive( d, d->pes.list[ii].stream_id,
d->pes.list[ii].stream_id_ext ) )
{
// this PID isn't wanted (we don't have a codec for it
// or scan didn't find audio parameters)
hb_stream_delete_ps_entry(d, ii);
continue;
}
}
// reset to beginning of file and reset some stream
// state information
hb_stream_seek( d, 0. );
}
}
/***********************************************************************
* hb_stream_open
***********************************************************************
*
**********************************************************************/
hb_stream_t *
hb_stream_open(hb_handle_t *h, const char *path, hb_title_t *title, int scan)
{
if (title == NULL)
{
hb_log("hb_stream_open: title is null");
return NULL;
}
FILE *f = hb_fopen(path, "rb");
if ( f == NULL )
{
hb_log( "hb_stream_open: open %s failed", path );
return NULL;
}
hb_stream_t *d = calloc( sizeof( hb_stream_t ), 1 );
if ( d == NULL )
{
fclose( f );
hb_log( "hb_stream_open: can't allocate space for %s stream state", path );
return NULL;
}
if (!(title->flags & HBTF_NO_IDR))
{
d->has_IDRs = 1;
}
/*
* If it's something we can deal with (MPEG2 PS or TS) return a stream
* reference structure & null otherwise.
*/
d->h = h;
d->file_handle = f;
d->title = title;
d->scan = scan;
d->path = strdup( path );
if (d->path != NULL )
{
if (hb_stream_get_type( d ) != 0)
{
if( !scan )
{
prune_streams( d );
}
// reset to beginning of file and reset some stream
// state information
hb_stream_seek( d, 0. );
return d;
}
fclose( d->file_handle );
d->file_handle = NULL;
if ( ffmpeg_open( d, title, scan ) )
{
return d;
}
}
if ( d->file_handle )
{
fclose( d->file_handle );
}
if (d->path)
{
free( d->path );
}
hb_log( "hb_stream_open: open %s failed", path );
free( d );
return NULL;
}
static int new_pid( hb_stream_t * stream )
{
int num = stream->ts.alloc;
if ( stream->ts.count == stream->ts.alloc )
{
num = stream->ts.alloc ? stream->ts.alloc * 2 : 32;
stream->ts.list = realloc( stream->ts.list,
sizeof( hb_ts_stream_t ) * num );
}
int ii;
for ( ii = stream->ts.alloc; ii < num; ii++ )
{
memset(&stream->ts.list[ii], 0, sizeof( hb_ts_stream_t ));
stream->ts.list[ii].continuity = -1;
stream->ts.list[ii].pid = -1;
stream->ts.list[ii].pes_list = -1;
}
stream->ts.alloc = num;
num = stream->ts.count;
stream->ts.count++;
return num;
}
static int new_pes( hb_stream_t * stream )
{
int num = stream->pes.alloc;
if ( stream->pes.count == stream->pes.alloc )
{
num = stream->pes.alloc ? stream->pes.alloc * 2 : 32;
stream->pes.list = realloc( stream->pes.list,
sizeof( hb_pes_stream_t ) * num );
}
int ii;
for ( ii = stream->pes.alloc; ii < num; ii++ )
{
memset(&stream->pes.list[ii], 0, sizeof( hb_pes_stream_t ));
stream->pes.list[ii].stream_id = -1;
stream->pes.list[ii].next = -1;
}
stream->pes.alloc = num;
num = stream->pes.count;
stream->pes.count++;
return num;
}
hb_stream_t * hb_bd_stream_open( hb_handle_t *h, hb_title_t *title )
{
int ii;
hb_stream_t *d = calloc( sizeof( hb_stream_t ), 1 );
if ( d == NULL )
{
hb_error( "hb_bd_stream_open: can't allocate space for stream state" );
return NULL;
}
d->h = h;
d->file_handle = NULL;
d->title = title;
d->path = NULL;
d->ts.packet = NULL;
int pid = title->video_id;
int stream_type = title->video_stream_type;
update_ts_streams( d, pid, 0, stream_type, V, NULL );
hb_audio_t * audio;
for ( ii = 0; ( audio = hb_list_item( title->list_audio, ii ) ); ++ii )
{
int stream_id_ext = audio->config.in.substream_type;
pid = audio->id & 0xFFFF;
stream_type = audio->config.in.stream_type;
update_ts_streams( d, pid, stream_id_ext, stream_type, A, NULL );
}
hb_subtitle_t * subtitle;
for ( ii = 0; ( subtitle = hb_list_item( title->list_subtitle, ii ) ); ++ii )
{
// If the subtitle track is CC embedded in the video stream, then
// it does not have an independent pid. In this case, we assigned
// the subtitle->id to HB_SUBTITLE_EMBEDDED_CC_TAG.
if (subtitle->id != HB_SUBTITLE_EMBEDDED_CC_TAG)
{
pid = subtitle->id & 0xFFFF;
stream_type = subtitle->stream_type;
update_ts_streams( d, pid, 0, stream_type, S, NULL );
}
}
// We don't need to wait for a PCR when scanning. In fact, it
// trips us up on the first preview of every title since we would
// have to read quite a lot of data before finding the PCR.
if ( title->flags & HBTF_SCAN_COMPLETE )
{
/* BD has PCRs, but the BD index always points to a packet
* after a PCR packet, so we will not see the initial PCR