-
Notifications
You must be signed in to change notification settings - Fork 41
/
libmpegts.c
2172 lines (1810 loc) · 71.7 KB
/
libmpegts.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
/*****************************************************************************
* libmpegts.c :
*****************************************************************************
* Copyright (C) 2010 Kieran Kunhya
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
*****************************************************************************/
#include "common.h"
#include "codecs.h"
#include "atsc/atsc.h"
#include "cablelabs/cablelabs.h"
#include "dvb/dvb.h"
#include "hdmv/hdmv.h"
#include "isdb/isdb.h"
#include "smpte/smpte.h"
#include "crc/crc.h"
#include <math.h>
static const int stream_type_table[30][2] =
{
{ LIBMPEGTS_VIDEO_MPEG2, VIDEO_MPEG2 },
{ LIBMPEGTS_VIDEO_AVC, VIDEO_AVC },
{ LIBMPEGTS_VIDEO_DIRAC, 0xd1 },
{ LIBMPEGTS_AUDIO_MPEG1, AUDIO_MPEG1 },
{ LIBMPEGTS_AUDIO_MPEG2, AUDIO_MPEG2 },
{ LIBMPEGTS_AUDIO_ADTS, AUDIO_ADTS },
{ LIBMPEGTS_AUDIO_LATM, AUDIO_LATM },
{ LIBMPEGTS_AUDIO_AC3, AUDIO_AC3 }, /* ATSC/Generic */
{ LIBMPEGTS_AUDIO_AC3, PRIVATE_DATA }, /* DVB */
{ LIBMPEGTS_AUDIO_EAC3, AUDIO_EAC3 }, /* ATSC/Generic */
{ LIBMPEGTS_AUDIO_EAC3, PRIVATE_DATA }, /* DVB */
{ LIBMPEGTS_AUDIO_LPCM, AUDIO_LPCM },
{ LIBMPEGTS_AUDIO_DTS, AUDIO_DTS },
{ LIBMPEGTS_AUDIO_DOLBY_LOSSLESS, AUDIO_DOLBY_LOSSLESS },
{ LIBMPEGTS_AUDIO_DTS_HD, AUDIO_DTS_HD },
{ LIBMPEGTS_AUDIO_DTS_HD_XLL, AUDIO_DTS_HD_XLL },
{ LIBMPEGTS_AUDIO_EAC3_SECONDARY, AUDIO_EAC3_SECONDARY },
{ LIBMPEGTS_AUDIO_DTS_HD_SECONDARY, AUDIO_DTS_HD_SECONDARY },
{ LIBMPEGTS_SUB_PRESENTATION_GRAPHICS, SUB_PRESENTATION_GRAPHICS },
{ LIBMPEGTS_SUB_INTERACTIVE_GRAPHICS, SUB_INTERACTIVE_GRAPHICS },
{ LIBMPEGTS_SUB_TEXT, SUB_TEXT },
{ LIBMPEGTS_AUDIO_302M, PRIVATE_DATA },
{ LIBMPEGTS_DVB_SUB, PRIVATE_DATA },
{ LIBMPEGTS_DVB_TELETEXT, PRIVATE_DATA },
{ LIBMPEGTS_DVB_VBI, PRIVATE_DATA },
{ LIBMPEGTS_ANCILLARY_RDD11, PRIVATE_DATA },
{ LIBMPEGTS_ANCILLARY_2038, PRIVATE_DATA },
{ LIBMPEGTS_AUDIO_OPUS, PRIVATE_DATA },
{ LIBMPEGTS_DATA_SCTE35, DATA_SCTE35 },
{ 0 },
};
/**** Descriptors ****/
/** MPEG-2 Systems Descriptors **/
/* Registration Descriptor */
void write_registration_descriptor( bs_t *s, int descriptor_tag, int descriptor_length, char *format_id )
{
bs_write( s, 8, descriptor_tag ); // descriptor_tag
bs_write( s, 8, descriptor_length ); // descriptor_length
while( *format_id != '\0' )
bs_write( s, 8, *format_id++ ); // format_identifier
}
/* First loop of PMT Descriptors */
static void write_smoothing_buffer_descriptor( bs_t *s, ts_int_program_t *program )
{
bs_write( s, 8, SMOOTHING_BUFFER_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 0x6 ); // descriptor_length
bs_write( s, 2, 0x3 ); // reserved
bs_write( s, 22, program->sb_leak_rate ); // sb_leak_rate
bs_write( s, 2, 0x3 ); // reserved
bs_write( s, 22, program->sb_size ); // sb_size
}
/* Second loop of PMT Descriptors */
#if 0
static void write_video_stream_descriptor( bs_t *s, ts_int_stream_t *stream )
{
bs_write( s, 8, VIDEO_STREAM_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 0x04 ); // descriptor_length
bs_write1( s, 0 ); // multiple_frame_rate_flag
bs_write( s, 4, 0 ); // frame_rate_code FIXME
bs_write1( s, 0 ); // MPEG_1_only_flag
bs_write1( s, 0 ); // constrained_parameter_flag
bs_write1( s, 0 ); // still_picture_flag
bs_write( s, 8, 0 ); // profile_and_level_indication FIXME
bs_write( s, 2, 0 ); // chroma_format FIXME
bs_write1( s, 0 ); // frame_rate_extension_flag FIXME
bs_write( s, 5, 0x1f ); // reserved
}
#endif
static void write_avc_descriptor( bs_t *s, ts_int_program_t *program, ts_int_stream_t *stream )
{
bs_write( s, 8, AVC_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 0x04 ); // descriptor_length
bs_write( s, 8, avc_profiles[stream->mpegvideo_ctx->profile] ); // profile_idc
bs_write1( s, stream->mpegvideo_ctx->profile == AVC_BASELINE ); // constraint_set0_flag
bs_write1( s, stream->mpegvideo_ctx->profile <= AVC_MAIN ); // constraint_set1_flag
bs_write1( s, 0 ); // constraint_set2_flag
if( stream->mpegvideo_ctx->level == 9 && stream->mpegvideo_ctx->profile <= AVC_MAIN ) // level 1b
bs_write1( s, 1 ); // constraint_set3_flag
else if( stream->mpegvideo_ctx->profile == AVC_HIGH_10_INTRA ||
stream->mpegvideo_ctx->profile == AVC_CAVLC_444_INTRA ||
stream->mpegvideo_ctx->profile == AVC_HIGH_444_INTRA )
bs_write1( s, 1 ); // constraint_set3_flag
else
bs_write1( s, 0 ); // constraint_set3_flag
bs_write1( s, 0 ); // constraint_set4_flag
bs_write1( s, 0 ); // constraint_set5_flag
bs_write( s, 2, 0 ); // reserved
bs_write( s, 8, stream->mpegvideo_ctx->level & 0xff ); // level_idc
bs_write( s, 1, 0 ); // AVC_still_present
bs_write( s, 1, 0 ); // AVC_24_hour_picture_flag
bs_write( s, 1, !program->is_3dtv ); // Frame_Packing_SEI_not_present_flag
bs_write( s, 5, 0x1f ); // reserved
}
static void write_data_stream_alignment_descriptor( bs_t *s )
{
bs_write( s, 8, DATA_STREAM_ALIGNMENT_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 1 ); // descriptor_length
// FIXME make UK-DTG compliant
bs_write( s, 8, 1 ); // alignment_type
}
static void write_mpeg2_aac_descriptor( bs_t *s, ts_int_stream_t *stream )
{
bs_write( s, 8, MPEG2_AAC_AUDIO_DESCRIPTOR ); // descriptor_tag
bs_write( s, 8, 0x3 ); // descriptor_length
bs_write( s, 8, stream->aac_profile ); // MPEG-2_AAC_profile
bs_write( s, 8, stream->aac_channel_map ); // MPEG-2_AAC_channel_configuration
bs_write( s, 8, 0 ); // MPEG-2_AAC_additional_information (anybody use this?)
}
/* AC-3 Descriptor for DVB and Blu-Ray */
static void write_ac3_descriptor( ts_writer_t *w, bs_t *s, int e_ac3 )
{
if( w->ts_type == TS_TYPE_BLU_RAY )
bs_write( s, 8, HDMV_AC3_DESCRIPTOR_TAG ); // descriptor_tag
else if( e_ac3 )
bs_write( s, 8, DVB_EAC3_DESCRIPTOR_TAG ); // descriptor_tag
else
bs_write( s, 8, DVB_AC3_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 1 ); // descriptor_length
/* does anything need these set? */
bs_write1( s, 0 ); // component_type_flag
bs_write1( s, 0 ); // bsid_flag
bs_write1( s, 0 ); // mainid_flag
bs_write1( s, 0 ); // asvc_flag
if( e_ac3 )
{
bs_write1( s, 0 ); // mixinfoexists
bs_write1( s, 0 ); // substream1_flag
bs_write1( s, 0 ); // substream2_flag
bs_write1( s, 0 ); // substream3_flag
}
else
bs_write( s, 4, 0 ); // reserved
}
static void write_iso_lang_descriptor( bs_t *s, ts_int_stream_t *stream )
{
bs_write( s, 8, ISO_693_LANGUAGE_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 0x4 ); // descriptor_length
for( int i = 0; i < 3; i++ )
bs_write( s, 8, stream->lang_code[i] );
bs_write(s, 8, stream->audio_type ); // audio_type
}
/** Misc descriptors **/
static void write_opus_descriptor( bs_t *s, ts_int_stream_t *stream )
{
bs_write( s, 8, DVB_EXTENSION_DESCRIPTOR_TAG ); // descriptor_tag
bs_write( s, 8, 0x2 ); // descriptor_length
bs_write( s, 8, 0x80 ); // descriptor_tag_extension (User defined)
bs_write( s, 8, stream->opus_channel_map ); // channel_config_code
}
/**** PCR functions ****/
static int64_t get_pcr_int( ts_writer_t *w, double offset )
{
return (int64_t)((8.0 * (w->packets_written * TS_PACKET_SIZE + offset) / w->ts_muxrate) * TS_CLOCK + 0.5) + w->pcr_start;
}
static double get_pcr_double( ts_writer_t *w, double offset )
{
return (8.0 * (w->packets_written * TS_PACKET_SIZE + offset) / w->ts_muxrate) + (double)w->pcr_start / TS_CLOCK;
}
static int check_pcr( ts_writer_t *w, ts_int_program_t *program )
{
// if the next packet written goes over the max pcr retransmit boundary, write the pcr in the next packet
int64_t next_pkt_pcr = get_pcr_int( w, (TS_PACKET_SIZE + 7) * 8 ) - program->last_pcr;
if( next_pkt_pcr >= w->pcr_period * (TS_CLOCK/1000) )
return 1;
return 0;
}
/**** Buffer management ****/
static void add_to_buffer( buffer_t *buffer )
{
buffer->cur_buf += TS_PACKET_SIZE * 8;
}
static void drip_buffer( ts_writer_t *w, ts_int_program_t *program, int rx, buffer_t *buffer, double next_pcr )
{
int iters;
double offset;
/* Although this uses floating point arithmetic, the values are backed by integers
* Transport buffer fullness does not need to be exact */
double cur_pcr = get_pcr_double( w, 0 );
if( buffer->last_byte_removal_time == 0.0 )
{
buffer->last_byte_removal_time = cur_pcr;
buffer->cur_buf -= 8;
}
iters = floor( (next_pcr - buffer->last_byte_removal_time) / (8.0 / rx) );
buffer->cur_buf -= 8*iters;
/* Avoid compounded error from floating point addition by making calculations relative to next_pcr */
offset = next_pcr - (buffer->last_byte_removal_time + 8.0 * iters / rx);
buffer->last_byte_removal_time = next_pcr - offset;
buffer->cur_buf = MAX( buffer->cur_buf, 0 );
}
static int write_adaptation_field( ts_writer_t *w, bs_t *s, ts_int_program_t *program, ts_int_pes_t *pes,
int write_pcr, int flags, int stuffing, int discontinuity )
{
int private_data_flag, write_dvb_au, random_access, priority;
int start = bs_pos( s );
uint8_t temp[512], temp2[256];
bs_t q, r;
private_data_flag = write_dvb_au = random_access = priority = 0;
if( pes && ( pes->data == pes->cur_pos ) )
{
ts_int_stream_t *stream = pes->stream;
random_access = pes->random_access;
if( IS_VIDEO( stream ) )
{
if( !write_pcr )
random_access = 0;
if( stream->dvb_au )
private_data_flag = write_dvb_au = 1;
}
priority = pes->priority;
pes->random_access = 0; /* don't write this flag again */
}
/* initialise temporary bitstream */
bs_init( &q, temp, 256 );
if( flags )
{
int64_t pcr = get_pcr_int( w, 7 ); /* 7 bytes until end of PCR field */
bs_write1( &q, discontinuity ); // discontinuity_indicator
bs_write1( &q, random_access ); // random_access_indicator
bs_write1( &q, priority ); // elementary_stream_priority_indicator
bs_write1( &q, write_pcr ); // PCR_flag
bs_write1( &q, 0 ); // OPCR_flag
bs_write1( &q, 0 ); // splicing_point_flag
bs_write1( &q, private_data_flag ); // transport_private_data_flag
bs_write1( &q, 0 ); // adaptation_field_extension_flag
if( write_pcr )
{
uint64_t base, extension;
int64_t mod = (int64_t)1 << 33;
program->last_pcr = pcr;
base = (pcr / 300) % mod;
extension = pcr % 300;
// program_clock_reference_base
bs_write32( &q, base >> 1 );
bs_write1( &q, (base & 1) );
// reserved
bs_write( &q, 6, 0x3f );
// program_clock_reference_extension
bs_write( &q, 8, (extension >> 1) & 0xff );
bs_write1( &q, (extension & 1 ) );
}
}
if( private_data_flag )
{
/* initialise another temporary bitstream */
bs_init( &r, temp2, 128 );
if( write_dvb_au )
write_dvb_au_information( &r, pes );
bs_flush ( &r );
bs_write( &q, 8, bs_pos( &r ) >> 3 ); // transport_private_data_length
write_bytes( &q, temp2, bs_pos( &r ) >> 3 );
}
for( int i = 0; i < stuffing; i++ )
bs_write( &q, 8, 0xff );
bs_flush( &q );
bs_write( s, 8, bs_pos( &q ) >> 3 ); // adaptation_field_length
write_bytes( s, temp, bs_pos( &q ) >> 3 );
return (bs_pos( s ) - start) >> 3;
}
static int write_pcr_empty( ts_writer_t *w, ts_int_program_t *program, int first )
{
bs_t *s = &w->out.bs;
write_packet_header( w, s, 0, program->pcr_stream->pid, ADAPT_FIELD_ONLY, &program->pcr_stream->cc );
int stuffing = 184 - 6 - 2; /* pcr, flags and length */
write_adaptation_field( w, s, program, NULL, 1, 1, stuffing, first );
add_to_buffer( &program->pcr_stream->tb );
if( increase_pcr( w, 1, 0 ) < 0 )
return -1;
return 0;
}
/**** PSI ****/
static int write_pat( ts_writer_t *w )
{
int start;
bs_t *s = &w->out.bs;
write_packet_header( w, s, 1, PAT_PID, PAYLOAD_ONLY, &w->pat_cc );
bs_write( s, 8, 0 ); // pointer field
start = bs_pos( s );
bs_write( s, 8, PAT_TID ); // table_id
bs_write1( s, 1 ); // section_syntax_indicator
bs_write1( s, 0 ); // '0'
bs_write( s, 2, 0x03 ); // reserved`
// FIXME when multiple programs are allowed do this properly
int section_length = w->num_programs * 4 + w->network_pid * 4 + 9;
bs_write( s, 12, section_length & 0x3ff );
bs_write( s, 16, w->ts_id & 0xffff ); // transport_stream_id
bs_write( s, 2, 0x03 ); // reserved
bs_write( s, 5, w->pat_version ); // version_number
bs_write1( s, 1 ); // current_next_indicator
bs_write( s, 8, 0 ); // section_number
bs_write( s, 8, 0 ); // last_section_number
if( w->network_pid )
{
bs_write( s, 16, 0 ); // program_number
bs_write( s, 3, 0x07 ); // reserved
bs_write( s, 13, w->network_pid & 0x1fff ); // network_PID
}
for( int i = 0; i < w->num_programs; i++ )
{
bs_write( s, 16, w->programs[i]->program_num & 0xffff ); // program_number
bs_write( s, 3, 0x07 ); // reserved
bs_write( s, 13, w->programs[i]->pmt.pid & 0x1fff ); // program_map_PID
}
bs_flush( s );
write_crc( s, start );
// -40 to include header and pointer field
write_padding( s, start - 40 );
add_to_buffer( &w->tb );
if( increase_pcr( w, 1, 0 ) < 0 )
return -1;
return 0;
}
static int eject_queued_pmt( ts_writer_t *w, ts_int_program_t *program, bs_t *s )
{
uint8_t **temp;
write_bytes( s, program->pmt_packets[0], TS_PACKET_SIZE );
if( program->num_queued_pmt > 1 )
memmove( &program->pmt_packets[0], &program->pmt_packets[1], (program->num_queued_pmt-1) * sizeof(uint8_t*) );
temp = realloc( program->pmt_packets, (program->num_queued_pmt-1) * sizeof(uint8_t*) );
program->pmt_packets = temp;
program->num_queued_pmt--;
add_to_buffer( &w->tb );
if( increase_pcr( w, 1, 0 ) < 0 )
return -1;
return 0;
};
static int write_pmt( ts_writer_t *w, ts_int_program_t *program )
{
int start;
bs_t *s = &w->out.bs;
uint8_t pmt_buf[2048] = {0}, temp[2048] = {0}, temp1[2048] = {0};
bs_t o, p, q;
int section_length;
uint8_t **temp2;
/* this should never happen */
if( program->num_queued_pmt )
return eject_queued_pmt( w, program, s );
start = bs_pos( s );
write_packet_header( w, s, 1, program->pmt.pid, PAYLOAD_ONLY, &program->pmt.cc );
bs_write( s, 8, 0 ); // pointer field
bs_init( &o, pmt_buf, 2048 );
bs_write( &o, 8, PMT_TID ); // table_id = program_map_section
bs_write1( &o, 1 ); // section_syntax_indicator
bs_write1( &o, 0 ); // '0'
bs_write( &o, 2, 0x3 ); // reserved
bs_init( &p, temp, 2048 );
bs_write( &p, 16, program->program_num & 0xffff ); // program_number
bs_write( &p, 2, 0x3 ); // reserved
bs_write( &p, 5, program->pmt_version ); // version_number
bs_write1( &p, 1 ); // current_next_indicator
bs_write( &p, 8, 0 ); // section_number
bs_write( &p, 8, 0 ); // last_section_number
bs_write( &p, 3, 0x7 ); // reserved
bs_write( &p, 13, program->pcr_stream[0].pid & 0x1fff ); // PCR PID
bs_write( &p, 4, 0xf ); // reserved
/* setup temporary bitstream context */
bs_init( &q, temp1, 2048 );
if( w->ts_type == TS_TYPE_ATSC )
{
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "GA94" );
write_smoothing_buffer_descriptor( &q, program );
}
else if( w->ts_type == TS_TYPE_CABLELABS )
{
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "SCTE" );
if( program->is_3dtv )
write_cablelabs_3d_descriptor( &q );
}
else if( w->ts_type == TS_TYPE_BLU_RAY )
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "HDMV" );
for( int i = 0; i < program->num_streams; i++ )
{
ts_int_stream_t *stream = program->streams[i];
if( stream->stream_format == LIBMPEGTS_DATA_SCTE35 )
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "CUEI" );
}
/* Optional descriptor(s) here */
bs_flush( &q );
bs_write( &p, 12, bs_pos( &q ) >> 3 ); // program_info_length
write_bytes( &p, temp1, bs_pos( &q ) >> 3 );
for( int i = 0; i < program->num_streams; i++ )
{
ts_int_stream_t *stream = program->streams[i];
bs_write( &p, 8, stream->stream_type & 0xff ); // stream_type
bs_write( &p, 3, 0x7 ); // reserved
bs_write( &p, 13, stream->pid & 0x1fff ); // elementary_PID
bs_write( &p, 4, 0xf ); // reserved
/* reset temporary bitstream context for streams loop */
bs_init( &q, temp1, 512 );
if( stream->stream_format != LIBMPEGTS_ANCILLARY_RDD11 && stream->stream_format != LIBMPEGTS_DATA_SCTE35 )
write_data_stream_alignment_descriptor( &q );
if( stream->dvb_au )
{
if( w->ts_type == TS_TYPE_DVB )
write_adaptation_field_data_descriptor( &q, AU_INFORMATION_DATA_FIELD );
else if( w->ts_type == TS_TYPE_CABLELABS )
write_scte_adaptation_descriptor( &q );
}
if( stream->write_lang_code )
write_iso_lang_descriptor( &q, stream );
if( stream->has_stream_identifier )
write_stream_identifier_descriptor( &q, stream->stream_identifier );
if( stream->stream_format == LIBMPEGTS_VIDEO_MPEG2 )
{
if( w->ts_type == TS_TYPE_BLU_RAY )
write_hdmv_video_registration_descriptor( &q, stream );
}
else if( stream->stream_format == LIBMPEGTS_VIDEO_AVC )
{
write_avc_descriptor( &q, program, stream );
if( w->ts_type == TS_TYPE_BLU_RAY )
write_hdmv_video_registration_descriptor( &q, stream );
}
else if( stream->stream_format == LIBMPEGTS_VIDEO_DIRAC )
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "BBCD" );
else if( stream->stream_format == LIBMPEGTS_AUDIO_MPEG1 ||
stream->stream_format == LIBMPEGTS_AUDIO_MPEG2 )
{
// TODO
}
else if( stream->stream_format == LIBMPEGTS_AUDIO_ADTS || stream->stream_format == LIBMPEGTS_AUDIO_LATM )
{
/* strictly speaking in DVB only LATM is allowed for MPEG-4 AAC audio. ADTS is commonly used however */
if( !stream->aac_is_mpeg4 )
write_mpeg2_aac_descriptor( &q, stream );
else if( w->ts_type == TS_TYPE_DVB )
write_aac_descriptor( &q, stream );
}
else if( stream->stream_format == LIBMPEGTS_AUDIO_AC3 )
{
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "AC-3" );
if( stream->atsc_ac3_ctx && ( w->ts_type == TS_TYPE_ATSC || w->ts_type == TS_TYPE_CABLELABS ) )
write_atsc_ac3_descriptor( &q, stream->atsc_ac3_ctx );
else
write_ac3_descriptor( w, &q, 0 );
}
else if( stream->stream_format == LIBMPEGTS_AUDIO_EAC3 || stream->stream_format == LIBMPEGTS_AUDIO_EAC3_SECONDARY )
write_ac3_descriptor( w, &q, 1 );
else if( stream->stream_format == LIBMPEGTS_AUDIO_DTS )
{
// TODO
}
else if( stream->stream_format == LIBMPEGTS_AUDIO_302M )
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "BSSD" );
else if( stream->stream_format == LIBMPEGTS_DVB_SUB )
write_dvb_subtitling_descriptor( &q, stream );
else if( stream->stream_format == LIBMPEGTS_DVB_TELETEXT )
write_teletext_descriptor( &q, stream, 0 );
else if( stream->stream_format == LIBMPEGTS_DVB_VBI )
{
write_vbi_descriptor( &q, stream );
if( stream->num_dvb_ttx )
write_teletext_descriptor( &q, stream, 1 );
}
else if( stream->stream_format == LIBMPEGTS_ANCILLARY_RDD11 )
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "LU-A" );
else if( stream->stream_format == LIBMPEGTS_ANCILLARY_2038 )
{
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "VANC" );
write_anc_data_descriptor( &q );
}
else if( stream->stream_format == LIBMPEGTS_AUDIO_OPUS )
{
write_registration_descriptor( &q, REGISTRATION_DESCRIPTOR_TAG, 4, "Opus" );
write_opus_descriptor( &q, stream );
}
else if( stream->stream_format == LIBMPEGTS_DATA_SCTE35 )
write_scte35_cue_identifier_descriptor( &q );
// TODO other stream_type descriptors
/* Optional descriptor(s) here */
bs_flush( &q );
bs_write( &p, 12, bs_pos( &q ) >> 3 ); // ES_info_length
write_bytes( &p, temp1, bs_pos( &q ) >> 3 );
}
/* section length includes crc */
section_length = (bs_pos( &p ) >> 3) + 4;
bs_write( &o, 12, section_length & 0x3ff );
/* write main chunk into pmt array */
bs_flush( &p );
write_bytes( &o, temp, bs_pos( &p ) >> 3 );
/* take crc of the whole program map section */
bs_flush( &o );
write_crc( &o, 0 );
int length = bs_pos( &o ) >> 3;
int bytes_left = TS_PACKET_SIZE - ((bs_pos( s ) - start) >> 3);
bs_flush( &o );
write_bytes( s, pmt_buf, MIN( bytes_left, length ) );
bs_flush( s );
write_padding( s, start );
add_to_buffer( &w->tb );
if( increase_pcr( w, 1, 0 ) < 0 )
return -1;
int pos = MIN( bytes_left, length );
length -= pos;
bytes_left = 184;
/* queue up pmt packets for spaced output */
while( length > 0 )
{
bs_t z;
temp2 = realloc( program->pmt_packets, (program->num_queued_pmt + 1) * sizeof(uint8_t*));
if( !temp2 )
{
fprintf( stderr, "malloc failed" );
return -1;
}
program->pmt_packets = temp2;
program->pmt_packets[program->num_queued_pmt] = malloc( TS_PACKET_SIZE );
if( !program->pmt_packets[program->num_queued_pmt] )
{
fprintf( stderr, "malloc failed" );
return -1;
}
bs_init( &z, program->pmt_packets[program->num_queued_pmt], 188 );
write_packet_header( w, &z, 0, program->pmt.pid, PAYLOAD_ONLY, &program->pmt.cc );
write_bytes( &z, &pmt_buf[pos], MIN( bytes_left, length ) );
bs_flush( &z );
write_padding( &z, 0 );
pos += MIN( bytes_left, length );
length -= MIN( bytes_left, length );
program->num_queued_pmt++;
}
return 0;
}
static void retransmit_psi_and_si( ts_writer_t *w, ts_int_program_t *program )
{
// TODO make this work with multiple programs
int64_t cur_pcr = get_pcr_int( w, 0 );
if( cur_pcr - w->last_pat >= w->pat_period * 27000LL || !w->last_pat )
{
/* Although it is not in line with the mux strategy it is good practice to write PAT and PMT together */
w->last_pat = cur_pcr;
write_pat( w ); // FIXME handle failure
write_pmt( w, program ); // FIXME handle failure
}
cur_pcr = get_pcr_int( w, 0 );
if( w->sdt && ( cur_pcr - w->last_sdt >= w->sdt_period * 27000LL || !w->last_sdt ) )
{
w->last_sdt = cur_pcr;
write_sdt( w );
}
}
/* DVB / Blu-Ray Service Information */
static int write_sit( ts_writer_t *w )
{
int start;
int len = 0; // FIXME
bs_t *s = &w->out.bs;
write_packet_header( w, s, 1, SIT_PID, PAYLOAD_ONLY, &w->sit->cc );
bs_write( s, 8, 0 ); // pointer field
start = bs_pos( s );
bs_write( s, 8, SIT_TID ); // table_id
bs_write1( s, 1 ); // section_syntax_indicator
bs_write1( s, 1 ); // DVB_reserved_future_use
bs_write( s, 2, 0x03 ); // ISO_reserved
// FIXME do length properly
bs_write( s, 12, 11 + len ); // section_length
bs_write( s, 16, 0xffff ); // DVB_reserved_future_use
bs_write( s, 2, 0x03 ); // ISO_reserved
bs_write( s, 5, w->sit->version_number ); // version_number
bs_write1( s, 1 ); // current_next_indicator
bs_write( s, 8, 0 ); // section_number
bs_write( s, 8, 0 ); // last_section_number
bs_write( s, 4, 0x0f ); // DVB_reserved_for_future_use
bs_write( s, 12, len ); // transmission_info_loop_length
if( w->ts_type == TS_TYPE_BLU_RAY )
write_partial_ts_descriptor( w, s );
for( int i = 0; i < w->num_programs; i++ )
{
bs_write( s, 16, w->programs[i]->program_num & 0xffff ); // service_id (equivalent to program_number)
bs_write1( s, 1 ); // DVB_reserved_future_use
bs_write( s, 3, 0x07 ); // running_status
bs_write( s, 12, 0 ); // service_loop_length
}
bs_flush( s );
write_crc( s, start );
// -40 to include header and pointer field
write_padding( s, start - 40 );
if( increase_pcr( w, 1, 0 ) < 0 )
return -1;
return 0;
}
static void write_timestamp( bs_t *s, uint64_t timestamp )
{
bs_write( s, 3, (timestamp >> 30) & 0x07 ); // timestamp [32..30]
bs_write1( s, 1 ); // marker_bit
bs_write( s, 8, (timestamp >> 22) & 0xff ); // timestamp [29..15]
bs_write( s, 7, (timestamp >> 15) & 0x7f ); // timestamp [29..15]
bs_write1( s, 1 ); // marker_bit
bs_write( s, 8, (timestamp >> 7) & 0xff ); // timestamp [14..0]
bs_write( s, 7, timestamp & 0x7f ); // timestamp [14..0]
bs_write1( s, 1 ); // marker_bit
}
static int write_pes( ts_writer_t *w, ts_int_program_t *program, ts_frame_t *in_frame, ts_int_pes_t *out_pes )
{
bs_t s, q;
uint8_t temp[1024];
int header_size, total_size;
int64_t mod = (int64_t)1 << 33;
if( out_pes->dts > out_pes->pts )
fprintf( stderr, "\nError: DTS > PTS\n" );
bs_init( &s, out_pes->data, in_frame->size + 200 );
ts_int_stream_t *stream = out_pes->stream;
bs_write( &s, 24, 1 ); // packet_start_code_prefix
bs_write( &s, 8, stream->stream_id ); // stream_id
/* Initialise temp buffer */
bs_init( &q, temp, 1024 );
bs_write( &q, 2, 0x2 ); // '10'
bs_write( &q, 2, 0 ); // PES_scrambling_control
bs_write1( &q, 0 ); // PES_priority
bs_write1( &q, stream->stream_format != LIBMPEGTS_ANCILLARY_RDD11 ); // data_alignment_indicator
bs_write1( &q, 1 ); // copyright
bs_write1( &q, 1 ); // original_or_copy
int same_timestamps = out_pes->dts == out_pes->pts;
bs_write( &q, 2, 0x02 + !same_timestamps ); // pts_dts_flags
bs_write1( &q, 0 ); // ESCR_flag
bs_write1( &q, 0 ); // ES_rate_flag
bs_write1( &q, 0 ); // DSM_trick_mode_flag
bs_write1( &q, 0 ); // additional_copy_info_flag
bs_write1( &q, 0 ); // PES_CRC_flag
bs_write1( &q, stream->stream_format == LIBMPEGTS_VIDEO_DIRAC ); // PES_extension_flag
if( stream->stream_format == LIBMPEGTS_VIDEO_DIRAC )
bs_write( &q, 8, 0x08 ); // PES_header_data_length
else if( stream->stream_format == LIBMPEGTS_DVB_TELETEXT || stream->stream_format == LIBMPEGTS_DVB_VBI )
bs_write( &q, 8, 0x24 ); // PES_header_data_length
else if( same_timestamps )
bs_write( &q, 8, 0x05 ); // PES_header_data_length (PTS only)
else
bs_write( &q, 8, 0x0a ); // PES_header_data_length (PTS and DTS)
bs_write( &q, 4, 0x02 + !same_timestamps ); // '0010' or '0011'
write_timestamp( &q, out_pes->pts % mod ); // PTS
if( !same_timestamps )
{
bs_write( &q, 4, 1 ); // '0001'
write_timestamp( &q, out_pes->dts % mod ); // DTS
}
if( stream->stream_format == LIBMPEGTS_VIDEO_DIRAC )
{
bs_write1( &q, 0 ); // PES_private_data_flag
bs_write1( &q, 0 ); // pack_header_field_flag
bs_write1( &q, 0 ); // program_packet_sequence_counter_flag
bs_write1( &q, 0 ); // P-STD_buffer_flag
bs_write( &q, 3, 0x0a ); // reserved
bs_write1( &q, 1 ); // PES_extension_flag_2
bs_write1( &q, 1 ); // marker_bit
bs_write( &q, 7, 1 ); // PES_header_data_length
bs_write1( &q, 0 ); // stream_id_extension_flag
bs_write( &q, 7, 0x60 ); // stream_id_extension
}
/* TTX and VBI require extra stuffing */
if( stream->stream_format == LIBMPEGTS_DVB_TELETEXT || stream->stream_format == LIBMPEGTS_DVB_VBI )
{
/* Total PES header is 45 bytes but don't count 6 bytes for the startcode, stream_id and length */
int num_stuffing = 45 - 6 - (bs_pos( &q ) >> 3);
for( int i = 0; i < num_stuffing; i++ )
bs_write( &q, 8, 0xff );
}
bs_flush( &q );
total_size = in_frame->size + (bs_pos( &q ) >> 3);
if( stream->stream_format == LIBMPEGTS_VIDEO_MPEG2 || stream->stream_format == LIBMPEGTS_VIDEO_AVC ||
stream->stream_format == LIBMPEGTS_VIDEO_DIRAC )
bs_write( &s, 16, 0 ); // PES_packet_length
else
bs_write( &s, 16, total_size ); // PES_packet_length
write_bytes( &s, temp, bs_pos( &q ) >> 3 );
header_size = bs_pos( &s ) >> 3;
write_bytes( &s, in_frame->data, in_frame->size );
bs_flush( &s );
out_pes->size = out_pes->bytes_left = bs_pos( &s ) >> 3;
out_pes->cur_pos = out_pes->data;
return header_size;
}
static int write_null_packet( ts_writer_t *w )
{
int start;
int cc = 0;
bs_t *s = &w->out.bs;
start = bs_pos( s );
write_packet_header( w, s, 0, NULL_PID, PAYLOAD_ONLY, &cc );
write_padding( s, start );
if( increase_pcr( w, 1, 0 ) < 0 )
return -1;
return 0;
}
static int check_bitstream( ts_writer_t *w )
{
if( w->out.bs.p_end - w->out.bs.p < 18800 )
{
bs_flush( &w->out.bs );
uint8_t *bs_bak = w->out.p_bitstream;
w->out.i_bitstream += 100000;
uint8_t *temp2 = realloc( w->out.p_bitstream, w->out.i_bitstream );
if( !temp2 )
{
fprintf( stderr, "realloc failed\n" );
return -1;
}
w->out.p_bitstream = temp2;
intptr_t delta = w->out.p_bitstream - bs_bak;
w->out.bs.p_start += delta;
w->out.bs.p += delta;
w->out.bs.p_end = w->out.p_bitstream + w->out.i_bitstream;
bs_realign( &w->out.bs );
}
return 0;
}
/* set updatable ts parameters */
static void update_ts_params( ts_writer_t *w, ts_main_t *params )
{
w->ts_muxrate = params->muxrate;
w->cbr = params->cbr;
w->legacy_constraints = params->legacy_constraints;
w->lowlatency = params->lowlatency;
w->pcr_period = params->pcr_period ? params->pcr_period : PCR_MAX_RETRANS_TIME;
w->pat_period = params->pat_period ? params->pat_period : PAT_MAX_RETRANS_TIME;
w->sdt_period = params->sdt_period ? params->sdt_period : SDT_MAX_RETRANS_TIME;
w->r_sys = MAX( R_SYS_DEFAULT, (double)w->ts_muxrate / 500 );
}
ts_writer_t *ts_create_writer( void )
{
ts_writer_t *w = calloc( 1, sizeof(*w) );
if( !w )
{
fprintf( stderr, "Malloc failed\n" );
return NULL;
}
return w;
}
int ts_setup_transport_stream( ts_writer_t *w, ts_main_t *params )
{
// TODO check for PID collisions, add MPTS support
if( params->ts_type < TS_TYPE_GENERIC || params->ts_type > TS_TYPE_BLU_RAY )
{
fprintf( stderr, "Invalid Transport Stream type.\n" );
return -1;
}
if( params->num_programs > 1 )
{
fprintf( stderr, "Multiple program transport streams are not yet supported.\n" );
return -1;
}
if( !params->cbr && params->num_programs > 1 )
{
fprintf( stderr, "Multiple program transport streams cannot be variable bitrate.\n" );
return -1;
}
if( params->network_pid && ( params->network_pid < 0x10 || params->network_pid == 0x1fff ) )
{
fprintf( stderr, "Invalid network_PID.\n" );
return -1;
}
if( !params->muxrate )
{
fprintf( stderr, "Muxrate must be nonzero\n" );
return -1;
}
BOOLIFY( params->cbr );
BOOLIFY( params->legacy_constraints );
int internal_pcr_pid, video_stream;
internal_pcr_pid = video_stream = 0;
ts_int_program_t *cur_program = calloc( 1, sizeof(*cur_program) );
if( !cur_program )
{
fprintf( stderr, "Malloc failed\n" );
return -1;
}
w->ts_type = params->ts_type;
w->num_programs = 1;
w->programs[0] = cur_program;
cur_program->pmt.pid = params->programs[0].pmt_pid;
cur_program->program_num = params->programs[0].program_num;
cur_program->is_3dtv = params->programs[0].is_3dtv;
cur_program->sb_leak_rate = params->programs[0].sb_leak_rate;
cur_program->sb_size = params->programs[0].sb_size;
cur_program->video_dts = -1;
cur_program->sdt_ctx.service_type = params->programs[0].sdt.service_type;
if( params->programs[0].sdt.service_name )
{
cur_program->sdt_ctx.service_name = malloc( strlen( params->programs[0].sdt.service_name ) + 1 );
if( !cur_program->sdt_ctx.service_name )
{
fprintf( stderr, "Malloc failed\n" );
return -1;
}
strcpy( cur_program->sdt_ctx.service_name, params->programs[0].sdt.service_name );
}
if( params->programs[0].sdt.provider_name )
{
cur_program->sdt_ctx.provider_name = malloc( strlen( params->programs[0].sdt.provider_name ) + 1 );
if( !cur_program->sdt_ctx.provider_name )
{
fprintf( stderr, "Malloc failed\n" );
return -1;
}
strcpy( cur_program->sdt_ctx.provider_name, params->programs[0].sdt.provider_name );
}
for( int i = 0; i < params->programs[0].num_streams; i++ )