-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTTransportStreamParser.m
1314 lines (1162 loc) · 37.9 KB
/
TTTransportStreamParser.m
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
/* $Id$
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. */
#import "TTTransportStreamParser.h"
// From HB internal.h
enum
{
WORK_SYNC_VIDEO = 1,
WORK_SYNC_AUDIO,
WORK_DECMPEG2,
WORK_DECCC608,
WORK_DECVOBSUB,
WORK_DECSRTSUB,
WORK_ENCVOBSUB,
WORK_RENDER,
WORK_ENCAVCODEC,
WORK_ENCX264,
WORK_ENCTHEORA,
WORK_DECA52,
WORK_DECDCA,
WORK_DECAVCODEC,
WORK_DECAVCODECV,
WORK_DECAVCODECVI,
WORK_DECAVCODECAI,
WORK_DECLPCM,
WORK_ENCFAAC,
WORK_ENCLAME,
WORK_ENCVORBIS,
WORK_ENC_CA_AAC,
WORK_MUX
};
// from HB common.h
/* Audio starts here */
/* Audio Codecs */
#define HB_ACODEC_MASK 0x00FF00
#define HB_ACODEC_FAAC 0x000100
#define HB_ACODEC_LAME 0x000200
#define HB_ACODEC_VORBIS 0x000400
#define HB_ACODEC_AC3 0x000800
#define HB_ACODEC_MPGA 0x001000
#define HB_ACODEC_LPCM 0x002000
#define HB_ACODEC_DCA 0x004000
#define HB_ACODEC_FFMPEG 0x008000
#define HB_ACODEC_CA_AAC 0x010000
// from ffmpeg avcodec.h
enum CodecID {
CODEC_ID_NONE,
/* video codecs */
CODEC_ID_MPEG1VIDEO,
CODEC_ID_MPEG2VIDEO, ///< preferred ID for MPEG-1/2 video decoding
CODEC_ID_MPEG2VIDEO_XVMC,
CODEC_ID_H261,
CODEC_ID_H263,
CODEC_ID_RV10,
CODEC_ID_RV20,
CODEC_ID_MJPEG,
CODEC_ID_MJPEGB,
CODEC_ID_LJPEG,
CODEC_ID_SP5X,
CODEC_ID_JPEGLS,
CODEC_ID_MPEG4,
CODEC_ID_RAWVIDEO,
CODEC_ID_MSMPEG4V1,
CODEC_ID_MSMPEG4V2,
CODEC_ID_MSMPEG4V3,
CODEC_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_ID_SMACKVIDEO,
CODEC_ID_NUV,
CODEC_ID_KMVC,
CODEC_ID_FLASHSV,
CODEC_ID_CAVS,
CODEC_ID_JPEG2000,
CODEC_ID_VMNC,
CODEC_ID_VP5,
CODEC_ID_VP6,
CODEC_ID_VP6F,
CODEC_ID_TARGA,
CODEC_ID_DSICINVIDEO,
CODEC_ID_TIERTEXSEQVIDEO,
CODEC_ID_TIFF,
CODEC_ID_GIF,
CODEC_ID_FFH264,
CODEC_ID_DXA,
CODEC_ID_DNXHD,
CODEC_ID_THP,
CODEC_ID_SGI,
CODEC_ID_C93,
CODEC_ID_BETHSOFTVID,
CODEC_ID_PTX,
CODEC_ID_TXD,
CODEC_ID_VP6A,
CODEC_ID_AMV,
CODEC_ID_VB,
CODEC_ID_PCX,
CODEC_ID_SUNRAST,
CODEC_ID_INDEO4,
CODEC_ID_INDEO5,
CODEC_ID_MIMIC,
CODEC_ID_RL2,
CODEC_ID_8SVX_EXP,
CODEC_ID_8SVX_FIB,
CODEC_ID_ESCAPE124,
CODEC_ID_DIRAC,
CODEC_ID_BFI,
CODEC_ID_CMV,
CODEC_ID_MOTIONPIXELS,
CODEC_ID_TGV,
CODEC_ID_TGQ,
CODEC_ID_TQI,
CODEC_ID_AURA,
CODEC_ID_AURA2,
CODEC_ID_V210X,
CODEC_ID_TMV,
CODEC_ID_V210,
CODEC_ID_DPX,
CODEC_ID_MAD,
CODEC_ID_FRWU,
CODEC_ID_FLASHSV2,
CODEC_ID_CDGRAPHICS,
CODEC_ID_R210,
CODEC_ID_ANM,
CODEC_ID_BINKVIDEO,
CODEC_ID_IFF_ILBM,
CODEC_ID_IFF_BYTERUN1,
CODEC_ID_KGV1,
CODEC_ID_YOP,
CODEC_ID_VP8,
/* various PCM "codecs" */
CODEC_ID_PCM_S16LE= 0x10000,
CODEC_ID_PCM_S16BE,
CODEC_ID_PCM_U16LE,
CODEC_ID_PCM_U16BE,
CODEC_ID_PCM_S8,
CODEC_ID_PCM_U8,
CODEC_ID_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
CODEC_ID_RA_144= 0x13000,
CODEC_ID_RA_288,
/* various DPCM codecs */
CODEC_ID_ROQ_DPCM= 0x14000,
CODEC_ID_INTERPLAY_DPCM,
CODEC_ID_XAN_DPCM,
CODEC_ID_SOL_DPCM,
/* audio codecs */
CODEC_ID_MP2= 0x15000,
CODEC_ID_MP3, ///< preferred ID for decoding MPEG audio layer 1, 2 or 3
CODEC_ID_AAC,
CODEC_ID_AC3,
CODEC_ID_DTS,
CODEC_ID_VORBIS,
CODEC_ID_DVAUDIO,
CODEC_ID_WMAV1,
CODEC_ID_WMAV2,
CODEC_ID_MACE3,
CODEC_ID_MACE6,
CODEC_ID_VMDAUDIO,
CODEC_ID_SONIC,
CODEC_ID_SONIC_LS,
CODEC_ID_FLAC,
CODEC_ID_MP3ADU,
CODEC_ID_MP3ON4,
CODEC_ID_SHORTEN,
CODEC_ID_ALAC,
CODEC_ID_WESTWOOD_SND1,
CODEC_ID_GSM, ///< as in Berlin toast format
CODEC_ID_QDM2,
CODEC_ID_COOK,
CODEC_ID_TRUESPEECH,
CODEC_ID_TTA,
CODEC_ID_SMACKAUDIO,
CODEC_ID_QCELP,
CODEC_ID_WAVPACK,
CODEC_ID_DSICINAUDIO,
CODEC_ID_IMC,
CODEC_ID_MUSEPACK7,
CODEC_ID_MLP,
CODEC_ID_GSM_MS, /* as found in WAV */
CODEC_ID_ATRAC3,
CODEC_ID_VOXWARE,
CODEC_ID_APE,
CODEC_ID_NELLYMOSER,
CODEC_ID_MUSEPACK8,
CODEC_ID_SPEEX,
CODEC_ID_WMAVOICE,
CODEC_ID_WMAPRO,
CODEC_ID_WMALOSSLESS,
CODEC_ID_ATRAC3P,
CODEC_ID_EAC3,
CODEC_ID_SIPR,
CODEC_ID_MP1,
CODEC_ID_TWINVQ,
CODEC_ID_TRUEHD,
CODEC_ID_MP4ALS,
CODEC_ID_ATRAC1,
CODEC_ID_BINKAUDIO_RDFT,
CODEC_ID_BINKAUDIO_DCT,
CODEC_ID_AAC_LATM,
/* subtitle codecs */
CODEC_ID_DVD_SUBTITLE= 0x17000,
CODEC_ID_DVB_SUBTITLE,
CODEC_ID_TEXT, ///< raw UTF-8 text
CODEC_ID_XSUB,
CODEC_ID_SSA,
CODEC_ID_MOV_TEXT,
CODEC_ID_HDMV_PGS_SUBTITLE,
CODEC_ID_DVB_TELETEXT,
/* other specific kind of codecs (generally used for attachments) */
CODEC_ID_TTF= 0x18000,
CODEC_ID_PROBE= 0x19000, ///< codec_id is not known (like CODEC_ID_NONE) but lavf should attempt to identify it
CODEC_ID_MPEG2TS= 0x20000, /**< _FAKE_ codec to indicate a raw MPEG-2 TS
* stream (only used by libavformat) */
};
/*
* 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.
*/
typedef struct {
enum { N, U, A, V } 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(0x01, V, WORK_DECMPEG2, 0, "MPEG1"),
st(0x02, V, WORK_DECMPEG2, 0, "MPEG2"),
st(0x03, A, HB_ACODEC_MPGA, CODEC_ID_MP2, "MPEG1"),
st(0x04, A, HB_ACODEC_MPGA, 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_MPGA, CODEC_ID_AAC, "ISO 13818-7 AAC Audio"),
st(0x10, V, WORK_DECAVCODECV, CODEC_ID_MPEG4, "MPEG4"),
st(0x11, A, HB_ACODEC_MPGA, CODEC_ID_AAC_LATM, "MPEG4 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, CODEC_ID_H264, "H.264"),
st(0x80, N, 0, 0, "DigiCipher II Video"),
st(0x81, A, HB_ACODEC_AC3, 0, "AC-3"),
st(0x82, A, HB_ACODEC_DCA, 0, "HDMV DTS"),
st(0x83, A, HB_ACODEC_LPCM, 0, "LPCM"),
st(0x84, A, 0, 0, "SDDS"),
st(0x85, U, 0, 0, "ATSC Program ID"),
st(0x86, A, HB_ACODEC_DCA, 0, "DTS-HD"),
st(0x87, A, 0, 0, "E-AC-3"),
st(0x8a, A, HB_ACODEC_DCA, 0, "DTS"),
st(0x91, A, HB_ACODEC_AC3, 0, "AC-3"),
st(0x92, N, 0, 0, "Subtitle"),
st(0x94, A, 0, 0, "SDDS"),
st(0xa0, V, 0, 0, "MSCODEC"),
st(0xea, V, WORK_DECAVCODECV, CODEC_ID_VC1, "VC1"),
};
#undef st
typedef enum {
hb_stream_type_unknown = 0,
transport,
program,
dvd_program,
ffmpeg
} hb_stream_type_t;
#define kMaxNumberVideoPIDS 1
#define kMaxNumberAudioPIDS 31
#define kMaxNumberDecodeStreams (kMaxNumberVideoPIDS+kMaxNumberAudioPIDS)
#define kMaxNumberPMTStreams 32
typedef struct hb_stream_s hb_stream_t;
struct hb_stream_s
{
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 */
uint8_t need_keyframe; // non-zero if want to start at a keyframe
uint8_t ts_found_pcr; // non-zero if we've found at least one input pcr
int ts_pcr_out; // sequence number of most recent output pcr
int ts_pcr_in; // sequence number of most recent input pcr
int64_t ts_pcr; // most recent input pcr
int64_t ts_pcrhist[4]; // circular buffer of output pcrs
uint8_t *ts_packet; /* buffer for one TS packet */
int chapter; /* Chapter that we are currently in */
int64_t chapter_end; /* HB time that the current chapter ends */
/*
* 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 ts_number_video_pids;
uint8_t ts_number_audio_pids;
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
uint8_t ts_IDRs; // # IDRs found during duration scan
int16_t ts_video_pids[kMaxNumberVideoPIDS];
int16_t ts_audio_pids[kMaxNumberAudioPIDS];
uint32_t ts_format_id[kMaxNumberDecodeStreams];
#define TS_FORMAT_ID_AC3 (('A' << 24) | ('C' << 16) | ('-' << 8) | '3')
uint8_t ts_stream_type[kMaxNumberDecodeStreams];
uint8_t ts_multiplexed[kMaxNumberDecodeStreams];
char *path;
NSFileHandle *file_handle;
hb_stream_type_t hb_stream_type;
uint8_t ffmpeg_video_id;
struct {
int lang_code;
int flags;
int rate;
int bitrate;
} a52_info[kMaxNumberAudioPIDS];
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;
int section_length;
int program_number;
unsigned int PCR_PID;
int program_info_length;
unsigned char *progam_info_descriptor_data;
struct
{
unsigned char stream_type;
unsigned short elementary_PID;
unsigned short ES_info_length;
unsigned char *es_info_descriptor_data;
} pmt_stream_info[kMaxNumberPMTStreams];
} pmt_info;
};
/***********************************************************************
* Local prototypes
**********************************************************************/
//static void hb_stream_duration(hb_stream_t *stream, hb_title_t *inTitle);
static void hb_ts_stream_init(hb_stream_t *stream);
static void hb_ts_stream_find_pids(hb_stream_t *stream);
static off_t align_to_next_packet(hb_stream_t *stream);
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 = 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, no scrambling & a legal adaptation ctrl
return (buf[0] == 0x47) && ((buf[3] >> 6) == 0) && ((buf[3] >> 4) > 0);
}
static int have_ts_sync(const uint8_t *buf, int psize)
{
return check_ts_sync(&buf[0*psize]) && check_ts_sync(&buf[1*psize]) &&
check_ts_sync(&buf[2*psize]) && check_ts_sync(&buf[3*psize]) &&
check_ts_sync(&buf[4*psize]) && check_ts_sync(&buf[5*psize]) &&
check_ts_sync(&buf[6*psize]) && check_ts_sync(&buf[7*psize]);
}
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;
for ( offset = 0; offset < 8*1024-8*188; ++offset )
{
if ( have_ts_sync( &buf[offset], 188) )
return 188 | (offset << 8);
if ( have_ts_sync( &buf[offset], 192) )
return 192 | (offset << 8);
if ( have_ts_sync( &buf[offset], 204) )
return 204 | (offset << 8);
if ( have_ts_sync( &buf[offset], 208) )
return 208 | (offset << 8);
}
return 0;
}
static int hb_stream_check_for_ps(const uint8_t *buf)
{
// 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).
return check_ps_sync(buf) && check_ps_sc(buf);
}
static int hb_stream_check_for_dvd_ps(const uint8_t *buf)
{
// DVD program streams should have a Pack header every 2048 bytes.
// check that we have 4 of these in a row.
return check_ps_sync(&buf[0*2048]) && check_ps_sync(&buf[1*2048]) &&
check_ps_sync(&buf[2*2048]) && check_ps_sync(&buf[3*2048]);
}
static int hb_stream_get_type(hb_stream_t *stream)
{
uint8_t buf[2048*4];
NSData *bufData = [stream->file_handle readDataOfLength:sizeof(buf)];
if ([bufData length] < sizeof(buf))
{
return 0;
}
[bufData getBytes:&buf length:sizeof(buf)];
int psize;
if ( ( psize = hb_stream_check_for_ts(buf) ) != 0 )
{
int offset = psize >> 8;
psize &= 0xff;
NSLog(@"file is MPEG Transport Stream with %d byte packets"
" offset %d bytes", psize, offset);
stream->packetsize = psize;
stream->hb_stream_type = transport;
hb_ts_stream_init(stream);
if ( !stream->ts_number_video_pids || !stream->ts_number_audio_pids )
{
return 0;
}
return 1;
}
if ( hb_stream_check_for_dvd_ps(buf) != 0 )
{
NSLog(@"file is MPEG DVD Program Stream");
stream->hb_stream_type = dvd_program;
return 1;
}
if ( hb_stream_check_for_ps(buf) != 0 )
{
NSLog(@"file is MPEG Program Stream");
stream->hb_stream_type = program;
return 1;
}
return 0;
}
static void hb_stream_delete_dynamic( hb_stream_t *d )
{
if( d->file_handle )
{
[d->file_handle closeFile];
[d->file_handle release];
d->file_handle = NULL;
}
if ( d->ts_packet )
{
free( d->ts_packet );
d->ts_packet = NULL;
}
}
static void hb_stream_delete( hb_stream_t *d )
{
hb_stream_delete_dynamic( d );
free( d->path );
free( d );
}
/***********************************************************************
* hb_stream_close
***********************************************************************
* Closes and frees everything
**********************************************************************/
void hb_stream_close( hb_stream_t ** _d )
{
hb_stream_t *stream = * _d;
if ( stream->hb_stream_type == ffmpeg )
{
//ffmpeg_close( stream );
hb_stream_delete( stream );
*_d = NULL;
return;
}
if ( stream->frames )
{
NSLog(@"stream: %d good frames, %d errors (%.0f%%)", stream->frames,
stream->errors, (double)stream->errors * 100. /
(double)stream->frames );
}
/*
* if the stream was opened for a scan, cache the result, otherwise delete
* the state.
*/
{
hb_stream_delete( stream );
}
*_d = NULL;
}
static int index_of_pid(int pid, hb_stream_t *stream)
{
int i;
if ( pid == stream->ts_video_pids[0] )
return 0;
for ( i = 0; i < stream->ts_number_audio_pids; ++i )
if ( pid == stream->ts_audio_pids[i] )
return i + 1;
return -1;
}
/*
* read the next transport stream packet from 'stream'. Return NULL if
* we hit eof & a pointer to the sync byte otherwise.
*/
static const uint8_t *next_packet( hb_stream_t *stream )
{
uint8_t *buf = stream->ts_packet + stream->packetsize - 188;
while ( 1 )
{
NSData *packetData = [stream->file_handle readDataOfLength:stream->packetsize];
if ([packetData length] != stream->packetsize)
{
return NULL;
}
[packetData getBytes:stream->ts_packet length:stream->packetsize];
if (buf[0] == 0x47)
{
return buf;
}
// lost sync - back up to where we started then try to re-establish.
off_t pos = [stream->file_handle offsetInFile] - stream->packetsize;
off_t pos2 = align_to_next_packet(stream);
if ( pos2 == 0 )
{
NSLog(@"next_packet: eof while re-establishing sync @ %llu", pos );
return NULL;
}
NSLog(@"next_packet: sync lost @ %llu, regained after %llu bytes", pos, pos2 );
}
}
/***********************************************************************
* hb_ts_stream_init
***********************************************************************
*
**********************************************************************/
static void hb_ts_stream_init(hb_stream_t *stream)
{
int i;
for (i=0; i < kMaxNumberDecodeStreams; i++)
{
//stream->ts_streamcont[i] = -1;
}
stream->ts_video_pids[0] = -1;
for ( i = 0; i < stream->ts_number_audio_pids; i++ )
{
stream-> ts_audio_pids[i] = -1;
}
stream->ts_packet = malloc( stream->packetsize );
// Find the audio and video pids in the stream
hb_ts_stream_find_pids(stream);
}
#define MAX_HOLE 208*80
static off_t align_to_next_packet(hb_stream_t *stream)
{
uint8_t buf[MAX_HOLE];
off_t pos = 0;
off_t start = [stream->file_handle offsetInFile];
if ( start >= stream->packetsize )
{
start -= stream->packetsize;
[stream->file_handle seekToFileOffset:start];
}
NSData *bufData = [stream->file_handle readDataOfLength:sizeof(buf)];
if (bufData && [bufData length] == sizeof(buf))
{
[bufData getBytes:&buf length:sizeof(buf)];
const uint8_t *bp = buf;
int i;
for ( i = sizeof(buf); --i >= 0; ++bp )
{
if ( have_ts_sync( bp, stream->packetsize ) )
{
break;
}
}
if ( i >= 0 )
{
pos = ( bp - buf ) - stream->packetsize + 188;
if ( pos < 0 )
pos = 0;
}
}
[stream->file_handle seekToFileOffset:start+pos];
return pos;
}
typedef struct {
uint8_t *buf;
uint32_t val;
int pos;
} bitbuf_t;
static const unsigned int bitmask[] = {
0x0,0x1,0x3,0x7,0xf,0x1f,0x3f,0x7f,0xff,
0x1ff,0x3ff,0x7ff,0xfff,0x1fff,0x3fff,0x7fff,0xffff,
0x1ffff,0x3ffff,0x7ffff,0xfffff,0x1fffff,0x3fffff,0x7fffff,0xffffff,
0x1ffffff,0x3ffffff,0x7ffffff,0xfffffff,0x1fffffff,0x3fffffff,0x7fffffff,0xffffffff};
static inline void set_buf(bitbuf_t *bb, uint8_t* buf, int bufsize, int clear)
{
bb->pos = 0;
bb->buf = buf;
bb->val = (bb->buf[0] << 24) | (bb->buf[1] << 16) |
(bb->buf[2] << 8) | bb->buf[3];
if (clear)
memset(bb->buf, 0, bufsize);
}
static inline int buf_size(bitbuf_t *bb)
{
return bb->pos >> 3;
}
static inline unsigned int get_bits(bitbuf_t *bb, int bits)
{
unsigned int val;
int left = 32 - (bb->pos & 31);
if (bits < left)
{
val = (bb->val >> (left - bits)) & bitmask[bits];
bb->pos += bits;
}
else
{
val = (bb->val & bitmask[left]) << (bits - left);
bb->pos += left;
bits -= left;
int pos = bb->pos >> 3;
bb->val = (bb->buf[pos] << 24) | (bb->buf[pos + 1] << 16) | (bb->buf[pos + 2] << 8) | bb->buf[pos + 3];
if (bits > 0)
{
val |= (bb->val >> (32 - bits)) & bitmask[bits];
bb->pos += bits;
}
}
return val;
}
static const char *stream_type_name (uint8_t stream_type)
{
return st2codec[stream_type].name? st2codec[stream_type].name : "Unknown";
}
// extract what useful information we can from the elementary stream
// descriptor list at 'dp' and add it to the stream at 'esindx'.
// Descriptors with info we don't currently use are ignored.
// The descriptor list & descriptor item formats are defined in
// ISO 13818-1 (2000E) section 2.6 (pg. 62).
static void decode_element_descriptors(hb_stream_t* stream, int esindx,
const uint8_t *dp, uint8_t dlen)
{
const uint8_t *ep = dp + dlen;
while (dp < ep)
{
switch (dp[0])
{
case 5: // Registration descriptor
stream->ts_format_id[esindx+1] = (dp[2] << 24) | (dp[3] << 16) |
(dp[4] << 8) | dp[5];
break;
case 10: // ISO_639_language descriptor
//stream->a52_info[esindx].lang_code = lang_to_code(lang_for_code2((const char *)&dp[2]));
break;
case 0x6a: // DVB AC-3 descriptor
stream->ts_stream_type[esindx+1] = 0x81;
break;
default:
break;
}
dp += dp[1] + 2;
}
}
int decode_program_map(hb_stream_t* stream)
{
bitbuf_t bb;
set_buf(&bb, stream->pmt_info.tablebuf, stream->pmt_info.tablepos, 0);
get_bits(&bb, 8); // table_id
get_bits(&bb, 4);
unsigned int section_length = get_bits(&bb, 12);
stream->pmt_info.section_length = section_length;
unsigned int program_number = get_bits(&bb, 16);
stream->pmt_info.program_number = program_number;
get_bits(&bb, 2);
get_bits(&bb, 5); // version_number
get_bits(&bb, 1);
get_bits(&bb, 8); // section_number
get_bits(&bb, 8); // last_section_number
get_bits(&bb, 3);
unsigned int PCR_PID = get_bits(&bb, 13);
stream->pmt_info.PCR_PID = PCR_PID;
get_bits(&bb, 4);
unsigned int program_info_length = get_bits(&bb, 12);
stream->pmt_info.program_info_length = program_info_length;
int i=0;
unsigned char *descriptor_buf = (unsigned char *) malloc(program_info_length);
for (i = 0; i < program_info_length; i++)
{
descriptor_buf[i] = get_bits(&bb, 8);
}
int cur_pos = 9 /* data after the section length field*/ + program_info_length;
int done_reading_stream_types = 0;
while (!done_reading_stream_types)
{
unsigned char stream_type = get_bits(&bb, 8);
get_bits(&bb, 3);
unsigned int elementary_PID = get_bits(&bb, 13);
get_bits(&bb, 4);
unsigned int ES_info_length = get_bits(&bb, 12);
int i=0;
unsigned char *ES_info_buf = (unsigned char *) malloc(ES_info_length);
for (i=0; i < ES_info_length; i++)
{
ES_info_buf[i] = get_bits(&bb, 8);
}
if ( index_of_pid( elementary_PID, stream ) < 0 )
{
// don't have this pid yet
if (stream->ts_number_video_pids == 0 &&
st2codec[stream_type].kind == V )
{
stream->ts_video_pids[0] = elementary_PID;
stream->ts_stream_type[0] = stream_type;
stream->ts_number_video_pids = 1;
}
else
{
// Defined audio stream types are 0x81 for AC-3/A52 audio
// and 0x03 for mpeg audio. But content producers seem to
// use other values (0x04 and 0x06 have both been observed)
// so at this point we say everything that isn't a video
// pid is audio then at the end of hb_stream_title_scan
// we'll figure out which are really audio by looking at
// the PES headers.
i = stream->ts_number_audio_pids;
if (i < kMaxNumberAudioPIDS)
{
stream->ts_audio_pids[i] = elementary_PID;
stream->ts_stream_type[1 + i] = stream_type;
if (ES_info_length > 0)
{
decode_element_descriptors(stream, i, ES_info_buf,
ES_info_length);
}
++stream->ts_number_audio_pids;
}
}
}
cur_pos += 5 /* stream header */ + ES_info_length;
free(ES_info_buf);
if (cur_pos >= section_length - 4 /* stop before the CRC */)
done_reading_stream_types = 1;
}
free(descriptor_buf);
return 1;
}
static int build_program_map(const uint8_t *buf, hb_stream_t *stream)
{
// Get adaption header info
int adapt_len = 0;
int adaption = (buf[3] & 0x30) >> 4;
if (adaption == 0)
return 0;
else if (adaption == 0x2)
adapt_len = 184;