-
Notifications
You must be signed in to change notification settings - Fork 95
/
ts.c
3962 lines (3645 loc) · 126 KB
/
ts.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
/*
* Utilities for working with H.222 Transport Stream packets
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the MPEG TS, PS and ES tools.
*
* The Initial Developer of the Original Code is Amino Communications Ltd.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Amino Communications Ltd, Swavesey, Cambridge UK
*
* ***** END LICENSE BLOCK *****
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#ifdef _WIN32
#include <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include "compat.h"
#include "ts_fns.h"
#include "tswrite_fns.h"
#include "misc_fns.h"
#include "printing_fns.h"
#include "pidint_fns.h"
#include "pes_fns.h"
#define DEBUG 0
#define DEBUG_DTS 0
#define DEBUG_WRITE_PACKETS 0
// Should we report reserved bits that are set to the wrong value?
// For the moment, make this a global, since it lets me suppress
// it easily. There should be some sort of command line switch
// to set this to FALSE in utilities that it matters for.
static int report_bad_reserved_bits = FALSE;
// ============================================================
// Suppport for the creation of Transport Streams.
// ============================================================
// Remember the continuity counter value for each/any PID
// (do I really want to have an array of size 8191+1?
// and do I want it static? not if this ever becomes a
// library module...)
static int continuity_counter[0x1fff+1] = {0};
/*
* Return the next value of continuity_counter for the given pid
*/
static inline int next_continuity_count(uint32_t pid)
{
uint32_t next = (continuity_counter[pid] + 1) & 0x0f;
continuity_counter[pid] = next;
return next;
}
/*
* Create a PES header for our data.
*
* - `data_len` is the length of our ES data
* If this is too long to fit into 16 bits, then we will create a header
* with 0 as its length. Note this is only allowed (by the standard) for
* video data.
* - `stream_id` is the elementary stream id to use (see H.222 Table 2-18).
* If the stream id indicates an audio stream (as elucidated by Note 2 in
* that same table), then the data_alignment_indicator flag will be set
* in the PES header - i.e., we assume that the audio frame *starts*
* (has its syncword) at the start of the PES packet payload.
* - `with_PTS` should be TRUE if the PTS value in `pts` should be written
* to the PES header.
* - `with_DTS` should be TRUE if the DTS value in `dts` should be written
* to the PES header. Note that if `with_DTS` is TRUE, then `with_PTS`
* must also be TRUE. If it is not, then the DTS value will be used for
* the PTS.
* - `PES_hdr` is the resultant PES packet header, and
* - `PES_hdr_len` its length (at the moment that's always the same, as
* we're not yet outputting any timing information (PTS/DTS), and so
* can get away with a minimal PES header).
*/
extern void PES_header(uint32_t data_len,
byte stream_id,
int with_PTS,
uint64_t pts,
int with_DTS,
uint64_t dts,
byte *PES_hdr,
int *PES_hdr_len)
{
int extra_len = 0;
if (with_DTS && !with_PTS)
{
with_PTS = TRUE;
pts = dts;
}
// If PTS=DTS then there is no point explictly coding the DTS so junk it
if (with_DTS && pts == dts)
with_DTS = FALSE;
// packet_start_code_prefix
PES_hdr[0] = 0x00;
PES_hdr[1] = 0x00;
PES_hdr[2] = 0x01;
PES_hdr[3] = stream_id;
// PES_packet_length comes next, but we'll actually sort it out
// at the end, when we know what else we've put into our header
// Flags: '10' then PES_scrambling_control .. original_or_copy
// If it appears to be an audio stream, we set the data alignment indicator
// flag, to indicate that the audio data starts with its syncword. For video
// data, we leave the flag unset.
if (IS_AUDIO_STREAM_ID(stream_id))
PES_hdr[6] = 0x84; // just data alignment indicator flag set
else
PES_hdr[6] = 0x80; // no flags set
// Flags: PTS_DTS_flags .. PES_extension_flag
if (with_DTS && with_PTS)
PES_hdr[7] = 0xC0;
else if (with_PTS)
PES_hdr[7] = 0x80;
else
PES_hdr[7] = 0x00; // yet more unset flags (nb: no PTS/DTS info)
// PES_header_data_length
if (with_DTS && with_PTS)
{
PES_hdr[8] = 0x0A;
encode_pts_dts(&(PES_hdr[9]),3,pts);
encode_pts_dts(&(PES_hdr[14]),1,dts);
*PES_hdr_len = 9 + 10;
extra_len = 3 + 10; // 3 bytes after the length field, plus our PTS & DTS
}
else if (with_PTS)
{
PES_hdr[8] = 0x05;
encode_pts_dts(&(PES_hdr[9]),2,pts);
*PES_hdr_len = 9 + 5;
extra_len = 3 + 5; // 3 bytes after the length field, plus our PTS
}
else
{
PES_hdr[8] = 0x00; // 0 means there is no more data
*PES_hdr_len = 9;
extra_len = 3; // just the basic 3 bytes after the length field
}
// So now we can set the length field itself...
if (data_len > 0xFFFF || (data_len + extra_len) > 0xFFFF)
{
// If the length is too great, we just set it "unset"
// @@@ (this should only really be done for TS-wrapped video, so perhaps
// we should complain if this is not video?)
PES_hdr[4] = 0;
PES_hdr[5] = 0;
}
else
{
// The packet length doesn't include the bytes up to and including the
// packet length field, but it *does* include any bytes of the PES header
// after it.
data_len += extra_len;
PES_hdr[4] = (byte) ((data_len & 0xFF00) >> 8);
PES_hdr[5] = (byte) ((data_len & 0x00FF));
}
}
/*
* Write out our TS packet, as composed from its parts.
*
* - `output` is the TS writer context to write with
* - `TS_packet` is the TS packet buffer, already filled to length `TS_hdr_len`
* with TS header information
* - `pes_hdr` is the PES header data, length `pes_hdr_len`, which is
* written out thereafter
* - `data` is then the actual ES data, length `data_len`
*
* When outputting asynchronously, the writer needs to know any timing
* information that is available for the packet. Thus:
*
* - `pid` is the PID for the packet
* - `got_pcr` is TRUE if we have a PCR for the packet, in which case
* - `pcr` is that PCR.
*
* Restrictions
* ============
*
* `TS_hdr_len` + `pes_hdr_len` + `data_len` should equal 188.
*
* `pes_hdr_len` may be 0 if there is no PES header data to be written
* (i.e., this is not the start of the PES packet's data, or the packet
* is not a PES packet).
*
* For real data, `data_len` should never be 0 (the exception is when
* writing NULL packets).
*
* `TS_hdr_len` must never be 0.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
static int write_TS_packet_parts(TS_writer_p output,
byte TS_packet[TS_PACKET_SIZE],
int TS_hdr_len,
byte pes_hdr[],
int pes_hdr_len,
byte data[],
int data_len,
uint32_t pid,
int got_pcr,
uint64_t pcr)
{
int err;
int total_len = TS_hdr_len + pes_hdr_len + data_len;
if (total_len != TS_PACKET_SIZE)
{
fprint_err("### TS packet length is %d, not 188 (composed of %d + %d + %d)\n",
total_len,TS_hdr_len,pes_hdr_len,data_len);
return 1;
}
// We want to make a single write, so we need to assemble the package
// into our packet buffer
if (pes_hdr_len > 0)
memcpy(&(TS_packet[TS_hdr_len]),pes_hdr,pes_hdr_len);
if (data_len > 0)
memcpy(&(TS_packet[TS_hdr_len+pes_hdr_len]),data,data_len);
err = tswrite_write(output,TS_packet,pid,got_pcr,pcr);
if (err)
{
fprint_err("### Error writing out TS packet: %s\n",strerror(errno));
return 1;
}
return 0;
}
/*
* Write our data as a (series of) Transport Stream PES packets.
*
* - `output` is the TS writer context we're using to write our TS data out
* - `pes_hdr` is NULL if the data to be written out is already PES, and is
* otherwise a PES header constructed with PES_header()
* - `pes_hdr_len` is the length of said PES header (or 0)
* - `data` is (the remainder of) our ES data (e.g., a NAL unit) or PES packet
* - `data_len` is its length
* - `start` is true if this is the first time we've called this function
* to output (part of) this data (in other words, this should be true
* when someone else calls this function, and false when the function
* calls itself). This is expected to be TRUE if a PES header is given...
* - `set_pusi` is TRUE if we should set the payload unit start indicator
* (generally true if `start` is TRUE). This is ignored if `start` is FALSE.
* - `pid` is the PID to use for this TS packet
* - `stream_id` is the PES packet stream id to use (e.g.,
* DEFAULT_VIDEO_STREAM_ID)
* - `got_PCR` is TRUE if we have a `PCR` value (this is only
* relevant when `start` is also TRUE).
* - `PCR_base` and `PCR_extn` then encode that PCR value (ditto)
*
* Returns 0 if it worked, 1 if something went wrong.
*/
static int write_some_TS_PES_packet(TS_writer_p output,
byte *pes_hdr,
int pes_hdr_len,
byte *data,
uint32_t data_len,
int start,
int set_pusi,
uint32_t pid,
byte stream_id,
int got_PCR,
uint64_t PCR_base,
uint32_t PCR_extn)
{
#define DEBUG_THIS 0
byte TS_packet[TS_PACKET_SIZE];
int TS_hdr_len;
uint32_t controls = 0;
uint32_t pes_data_len = 0;
int err;
int got_adaptation_field = FALSE;
uint32_t space_left; // Bytes available for payload, after the TS header
if (pid < 0x0010 || pid > 0x1ffe)
{
fprint_err("### PID %03x is outside legal program stream range",pid);
return 1;
}
// If this is the first time we've "seen" this data, and it is not
// already wrapped up as PES, then we need to remember its PES header
// in our calculations
if (pes_hdr)
pes_data_len = data_len + pes_hdr_len;
else
{
pes_hdr_len = 0;
pes_data_len = data_len;
}
#if DEBUG_THIS
if (start)
print_msg("TS_PES ");
else
print_msg(" ");
print_data(TRUE,"",data,data_len,20);
#endif
// We always start with a sync_byte to identify this as a
// Transport Stream packet
TS_packet[0] = 0x47;
// Should we set the "payload_unit_start_indicator" bit?
// Only for the first packet containing our data.
if (start && set_pusi)
TS_packet[1] = (byte)(0x40 | ((pid & 0x1f00) >> 8));
else
TS_packet[1] = (byte)(0x00 | ((pid & 0x1f00) >> 8));
TS_packet[2] = (byte)(pid & 0xff);
// Sort out the adaptation field, if any
if (start && got_PCR)
{
// This is the start of the data, and we have a PCR value to output,
// so we know we have an adaptation field
controls = 0x30; // adaptation field control = '11' = both
TS_packet[3] = (byte) (controls | next_continuity_count(pid));
// And construct said adaptation field...
TS_packet[4] = 7; // initial adaptation field length
TS_packet[5] = 0x10; // flag bits 0001 0000 -> got PCR
TS_packet[6] = (byte) (PCR_base >> 25);
TS_packet[7] = (byte) ((PCR_base >> 17) & 0xFF);
TS_packet[8] = (byte) ((PCR_base >> 9) & 0xFF);
TS_packet[9] = (byte) ((PCR_base >> 1) & 0xFF);
TS_packet[10] = (byte) (((PCR_base & 0x1) << 7) | 0x7E | (PCR_extn >> 8));
TS_packet[11] = (byte) (PCR_extn >> 1);
TS_hdr_len = 12;
space_left = MAX_TS_PAYLOAD_SIZE - 8;
got_adaptation_field = TRUE;
#if DEBUG_THIS
fprint_msg(" start & got_PCR -> with adaptation field, space left %d, TS_packet[4] %d\n",space_left,TS_packet[4]);
#endif
}
else if (pes_data_len < MAX_TS_PAYLOAD_SIZE)
{
// Our data is less than 184 bytes long, which means it won't fill
// the payload, so we need to pad it out with an (empty) adaptation
// field, padded to the appropriate length
controls = 0x30; // adaptation field control = '11' = both
TS_packet[3] = (byte)(controls | next_continuity_count(pid));
if (pes_data_len == (MAX_TS_PAYLOAD_SIZE - 1)) // i.e., 183
{
TS_packet[4] = 0; // just the length used to pad
TS_hdr_len = 5;
space_left = MAX_TS_PAYLOAD_SIZE - 1;
}
else
{
TS_packet[4] = 1; // initial length
TS_packet[5] = 0; // unset flag bits
TS_hdr_len = 6;
space_left = MAX_TS_PAYLOAD_SIZE - 2; // i.e., 182
}
got_adaptation_field = TRUE;
#if DEBUG_THIS
fprint_msg(" <184, pad with empty adaptation field, space left %d, TS_packet[4] %d\n",space_left,TS_packet[4]);
#endif
}
else
{
// The data either fits exactly, or is too long and will need to be
// continued in further TS packets. In either case, we don't need an
// adaptation field
controls = 0x10; // adaptation field control = '01' = payload only
TS_packet[3] = (byte)(controls | next_continuity_count(pid));
TS_hdr_len = 4;
space_left = MAX_TS_PAYLOAD_SIZE;
#if DEBUG_THIS
fprint_msg(" >=184, space left %d\n",space_left);
#endif
}
if (got_adaptation_field)
{
// Do we need to add stuffing bytes to allow for short PES data?
if (pes_data_len < space_left)
{
int ii;
int padlen = space_left - pes_data_len;
for (ii = 0; ii < padlen; ii++)
TS_packet[TS_hdr_len+ii] = 0xFF;
TS_packet[4] += padlen;
TS_hdr_len += padlen;
space_left -= padlen;
#if DEBUG_THIS
fprint_msg(" stuffing %d, space left %d, TS_packet[4] %d\n",padlen,space_left,TS_packet[4]);
#endif
}
}
if (pes_data_len == space_left)
{
#if DEBUG_THIS
print_msg(" == fits exactly\n");
#endif
// Our data fits exactly
err = write_TS_packet_parts(output,
TS_packet,TS_hdr_len,
pes_hdr,pes_hdr_len,
data,data_len,
pid,got_PCR,(PCR_base*300)+PCR_extn);
if (err) return err;
}
else
{
// We need to look at more than one packet...
// Write out the first 184-pes_hdr_len bytes
int increment = space_left - pes_hdr_len;
err = write_TS_packet_parts(output,
TS_packet,TS_hdr_len,
pes_hdr,pes_hdr_len,
data,increment,
pid,got_PCR,(PCR_base*300)+PCR_extn);
if (err) return err;
#if DEBUG_THIS
fprint_msg(" == wrote %d, leaving %d\n",increment,data_len-increment);
#endif
// Leaving data_len - (184-pes_hdr_len) bytes still to go
// Is recursion going to be efficient enough?
if ((data_len - increment) > 0)
{
err = write_some_TS_PES_packet(output,NULL,0,
&(data[increment]),data_len-increment,
FALSE,FALSE,pid,stream_id,FALSE,0,0);
if (err) return err;
}
}
return 0;
}
/*
* Write out our ES data as a Transport Stream PES packet.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `data` is our ES data (e.g., a NAL unit)
* - `data_len` is its length
* - `pid` is the PID to use for this TS packet
* - `stream_id` is the PES packet stream id to use (e.g.,
* DEFAULT_VIDEO_STREAM_ID)
*
* If the data to be written is more than 65535 bytes long (i.e., the
* length will not fit into 2 bytes), then the PES packet written will
* have PES_packet_length set to zero (see ISO/IEC 13818-1 (H.222.0)
* 2.4.3.7, Semantic definitions of fields in PES packet). This is only
* allowed for video streams.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_ES_as_TS_PES_packet(TS_writer_p output,
byte data[],
uint32_t data_len,
uint32_t pid,
byte stream_id)
{
byte pes_hdr[TS_PACKET_SIZE]; // better be more than long enough!
int pes_hdr_len = 0;
#if DEBUG_WRITE_PACKETS
fprint_msg("|| ES as TS/PES, pid %x (%d)\n",pid,pid);
#endif
PES_header(data_len,stream_id,FALSE,0,FALSE,0,pes_hdr,&pes_hdr_len);
return write_some_TS_PES_packet(output,pes_hdr,pes_hdr_len,
data,data_len,TRUE,TRUE,pid,stream_id,
FALSE,0,0);
}
/*
* Write out our ES data as a Transport Stream PES packet, with PTS and/or DTS
* if we've got them, and some attempt to write out a sensible PCR.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `data` is our ES data (e.g., a NAL unit)
* - `data_len` is its length
* - `pid` is the PID to use for this TS packet
* - `stream_id` is the PES packet stream id to use (e.g.,
* DEFAULT_VIDEO_STREAM_ID)
* - `got_pts` is TRUE if we have a PTS value, in which case
* - `pts` is said PTS value
* - `got_dts` is TRUE if we also have DTS, in which case
* - `dts` is said DTS value.
*
* We also want to try to write out a sensible PCR value.
*
* PTS can go up as well as down (it is the time at which the next frame
* should be presented to the user, but frames do not necessarily occur
* in presentation order).
*
* DTS only goes up, since it is the time that the frame should be decoded.
*
* Thus, if we have it, the DTS is sensible to use for the PCR...
*
* If the data to be written is more than 65535 bytes long (i.e., the
* length will not fit into 2 bytes), then the PES packet written will
* have PES_packet_length set to zero (see ISO/IEC 13818-1 (H.222.0)
* 2.4.3.7, Semantic definitions of fields in PES packet). This is only
* allowed for video streams.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_ES_as_TS_PES_packet_with_pts_dts(TS_writer_p output,
byte data[],
uint32_t data_len,
uint32_t pid,
byte stream_id,
int got_pts,
uint64_t pts,
int got_dts,
uint64_t dts)
{
byte pes_hdr[TS_PACKET_SIZE]; // better be more than long enough!
int pes_hdr_len = 0;
#if DEBUG_WRITE_PACKETS
fprint_msg("|| ES as TS/PES with PTS/DTS, pid %x (%d)\n",pid,pid);
#endif
PES_header(data_len,stream_id,got_pts,pts,got_dts,dts,pes_hdr,&pes_hdr_len);
return write_some_TS_PES_packet(output,pes_hdr,pes_hdr_len,
data,data_len,TRUE,TRUE,pid,stream_id,
got_dts,dts,0);
}
/*
* Write out our ES data as a Transport Stream PES packet, with PCR.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `data` is our ES data (e.g., a NAL unit)
* - `data_len` is its length
* - `pid` is the PID to use for this TS packet
* - `stream_id` is the PES packet stream id to use (e.g.,
* DEFAULT_VIDEO_STREAM_ID)
* - `pcr_base` and `pcr_extn` encode the PCR value.
*
* If the data to be written is more than 65535 bytes long (i.e., the
* length will not fit into 2 bytes), then the PES packet written will
* have PES_packet_length set to zero (see ISO/IEC 13818-1 (H.222.0)
* 2.4.3.7, Semantic definitions of fields in PES packet). This is only
* allowed for video streams.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_ES_as_TS_PES_packet_with_pcr(TS_writer_p output,
byte data[],
uint32_t data_len,
uint32_t pid,
byte stream_id,
uint64_t pcr_base,
uint32_t pcr_extn)
{
byte pes_hdr[TS_PACKET_SIZE]; // better be more than long enough!
int pes_hdr_len = 0;
#if DEBUG_WRITE_PACKETS
fprint_msg("|| ES as TS/PES with PCR, pid %x (%d)\n",pid,pid);
#endif
PES_header(data_len,stream_id,FALSE,0,FALSE,0,pes_hdr,&pes_hdr_len);
return write_some_TS_PES_packet(output,pes_hdr,pes_hdr_len,
data,data_len,TRUE,TRUE,pid,stream_id,
TRUE,pcr_base,pcr_extn);
}
/*
* Write out a PES packet's data as a Transport Stream PES packet.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `data` is our PES data (e.g., a program stream video data packet)
* - `data_len` is its length
* - `pid` is the PID to use for this TS packet
* - `stream_id` is the PES packet stream id to use (e.g.,
* DEFAULT_VIDEO_STREAM_ID)
* - `got_pcr` is TRUE if we have values for the PCR in this packet,
* in which case `pcr_base` and `pcr_extn` are the parts of the PCR.
*
* If the data to be written is more than 65535 bytes long (i.e., the
* length will not fit into 2 bytes), then the PES packet written will
* have PES_packet_length set to zero (see ISO/IEC 13818-1 (H.222.0)
* 2.4.3.7, Semantic definitions of fields in PES packet). This is only
* allowed for video streams.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_PES_as_TS_PES_packet(TS_writer_p output,
byte data[],
uint32_t data_len,
uint32_t pid,
byte stream_id,
int got_pcr,
uint64_t pcr_base,
uint32_t pcr_extn)
{
// Should we write MPEG-1 packet data out as ES (wrapped in MPEG-2 PES in TS),
// rather than writing the packets out directly in TS? (that latter doesn't
// work very well, as TS is not defined to work for MPEG-1 style packets).
#define MPEG1_AS_ES 1
#if DEBUG_WRITE_PACKETS
fprint_msg("|| PES as TS/PES, pid %x (%d)\n",pid,pid);
#endif
#if 0 // XXX
print_data(TRUE,"TS_PES",data,data_len,20);
print_end_of_data(" ",data,data_len,20);
#endif // XXX
#if MPEG1_AS_ES
if (IS_H222_PES(data))
{
#endif // MPEG1_AS_ES
return write_some_TS_PES_packet(output,NULL,0,
data,data_len,TRUE,TRUE,pid,stream_id,
got_pcr,pcr_base,pcr_extn);
#if MPEG1_AS_ES
}
else
{
// Write MPEG-1 data out as ES in (MPEG-2) PES
int got_pts, got_dts;
uint64_t pts, dts;
int offset = calc_mpeg1_pes_offset(data,data_len);
int err = find_PTS_DTS_in_PES(data,data_len,
&got_pts,&pts,&got_dts,&dts);
if (err) // Just try to carry on...
{
got_pts = FALSE;
got_dts = FALSE;
}
return write_ES_as_TS_PES_packet_with_pts_dts(output,
data + offset,
data_len - offset,
pid,
stream_id,
got_pts,pts,
got_dts,dts);
}
#endif // MPEG1_AS_ES
}
/*
* Construct a Transport Stream packet header for PAT or PMT data.
*
* The data is required to fit within a single TS packet - i.e., to be
* 183 bytes or less.
*
* - `pid` is the PID to use for this packet.
* - `data_len` is the length of the PAT or PMT data
* - `TS_hdr` is a byte array into (the start of) which to write the
* TS header data.
* - `TS_hdr_len` returns how much data we've written therein.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
static int TS_program_packet_hdr(uint32_t pid,
int data_len,
byte TS_hdr[TS_PACKET_SIZE],
int *TS_hdr_len)
{
uint32_t controls = 0;
int pointer, ii;
if (data_len > (TS_PACKET_SIZE - 5)) // i.e., 183
{
fprint_err("### PMT/PAT data for PID %02x is too long (%d > 183)",
pid,data_len);
return 1;
}
// We always start with a sync_byte to identify this as a
// Transport Stream packet
TS_hdr[0] = 0x47;
// We want the "payload_unit_start_indicator" bit set
TS_hdr[1] = (byte)(0x40 | ((pid & 0x1f00) >> 8));
TS_hdr[2] = (byte)(pid & 0xff);
// We don't need any adaptation field controls
controls = 0x10;
TS_hdr[3] = (byte)(controls | next_continuity_count(pid));
// Next comes a pointer to the actual payload data
// (i.e., 0 if the data is 183 bytes long)
// followed by pad bytes until we *get* to the data
pointer = (byte)(TS_PACKET_SIZE - 5 - data_len);
TS_hdr[4] = pointer;
for (ii=0; ii<pointer; ii++)
TS_hdr[5+ii] = 0xff;
*TS_hdr_len = 5+pointer;
return 0;
}
/*
* Write out a Transport Stream PAT and PMT, for a single stream.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `transport_stream_id` is the id for this particular transport stream.
* - `program_number` is the program number to use for the PID.
* - `pmt_pid` is the PID for the PMT.
* - `pid` is the PID of the stream to enter in the tables. This is also
* used as the PCR PID.
* - `stream_type` is the type of stream. MPEG-2 video is 0x01,
* MPEG-4/AVC (H.264) is 0x1b.
*
* Since we're outputting a TS representing a single ES, we only need to
* support a single program stream, containing a single PID.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_TS_program_data(TS_writer_p output,
uint32_t transport_stream_id,
uint32_t program_number,
uint32_t pmt_pid,
uint32_t pid,
byte stream_type)
{
int err;
pidint_list_p prog_list;
pmt_p pmt;
// We have a single program stream
err = build_pidint_list(&prog_list);
if (err) return 1;
err = append_to_pidint_list(prog_list,pmt_pid,program_number);
if (err)
{
free_pidint_list(&prog_list);
return 1;
}
pmt = build_pmt((uint16_t)program_number,0,pid); // Use program stream PID as PCR PID
if (pmt == NULL)
{
free_pidint_list(&prog_list);
return 1;
}
err = add_stream_to_pmt(pmt,pid,stream_type,0,NULL);
if (err)
{
free_pidint_list(&prog_list);
free_pmt(&pmt);
return 1;
}
err = write_pat_and_pmt(output,transport_stream_id,prog_list,pmt_pid,pmt);
if (err)
{
free_pidint_list(&prog_list);
free_pmt(&pmt);
return 1;
}
free_pidint_list(&prog_list);
free_pmt(&pmt);
return 0;
}
/*
* Write out a Transport Stream PAT and PMT, for multiple streams.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `transport_stream_id` is the id for this particular transport stream.
* - `program_number` is the program number to use for the PMT PID.
* - `pmt_pid` is the PID for the PMT.
* - `pcr_pid` is the PID that contains the PCR.
* - `num_progs` is how many program streams are to be defined.
* - `prog_pid` is an array of audio/video PIDs
* - `prog_type` is an array of the corresponding stream types
*
* Note that if `num_progs` is 0, `pcr_pid` is ignored.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_TS_program_data2(TS_writer_p output,
uint32_t transport_stream_id,
uint32_t program_number,
uint32_t pmt_pid,
uint32_t pcr_pid,
int num_progs,
uint32_t prog_pid[],
byte prog_type[])
{
int err;
int ii;
pidint_list_p prog_list;
pmt_p pmt;
// We have a single program stream
err = build_pidint_list(&prog_list);
if (err) return 1;
err = append_to_pidint_list(prog_list,pmt_pid,program_number);
if (err)
{
free_pidint_list(&prog_list);
return 1;
}
pmt = build_pmt((uint16_t)program_number,0,pcr_pid);
if (pmt == NULL)
{
free_pidint_list(&prog_list);
return 1;
}
for (ii=0; ii<num_progs; ii++)
{
err = add_stream_to_pmt(pmt,prog_pid[ii],prog_type[ii],0,NULL);
if (err)
{
free_pidint_list(&prog_list);
free_pmt(&pmt);
return 1;
}
}
err = write_pat_and_pmt(output,transport_stream_id,prog_list,pmt_pid,pmt);
if (err)
{
free_pidint_list(&prog_list);
free_pmt(&pmt);
return 1;
}
free_pidint_list(&prog_list);
free_pmt(&pmt);
return 0;
}
/*
* Write out a Transport Stream PAT.
*
* - `output` is the TS output context returned by `tswrite_open`
* - `transport_stream_id` is the id for this particular transport stream.
* - `prog_list` is a PIDINT list of program number / PID pairs.
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_pat(TS_writer_p output,
uint32_t transport_stream_id,
pidint_list_p prog_list)
{
int ii;
byte data[1021+3];
byte TS_packet[TS_PACKET_SIZE];
int TS_hdr_len;
int err;
int section_length;
int offset, data_length;
uint32_t crc32;
#if DEBUG_WRITE_PACKETS
print_msg("|| PAT pid 0\n");
#endif
section_length = 9 + prog_list->length * 4;
if (section_length > 1021)
{
print_err("### PAT data is too long - will not fit in 1021 bytes\n");
// TODO: Ideally, would be to stderr
report_pidint_list(prog_list,"Program list","Program",FALSE);
return 1;
}
data[0] = 0x00;
// The section length is fixed because our data is fixed
data[1] = (byte) (0xb0 | ((section_length & 0x0F00) >> 8));
data[2] = (byte) (section_length & 0x0FF);
data[3] = (byte) ((transport_stream_id & 0xFF00) >> 8);
data[4] = (byte) (transport_stream_id & 0x00FF);
// For simplicity, we'll have a version_id of 0
data[5] = 0xc1;
// First section of the PAT has section number 0, and there is only
// that section
data[6] = 0x00;
data[7] = 0x00;
offset = 8;
for (ii = 0; ii < prog_list->length; ii++)
{
data[offset+0] = (byte) ((prog_list->number[ii] & 0xFF00) >> 8);
data[offset+1] = (byte) (prog_list->number[ii] & 0x00FF);
data[offset+2] = (byte) (0xE0 | ((prog_list->pid[ii] & 0x1F00) >> 8));
data[offset+3] = (byte) (prog_list->pid[ii] & 0x00FF);
offset += 4;
}
crc32 = crc32_block(0xffffffff,data,offset);
data[12] = (byte) ((crc32 & 0xff000000) >> 24);
data[13] = (byte) ((crc32 & 0x00ff0000) >> 16);
data[14] = (byte) ((crc32 & 0x0000ff00) >> 8);
data[15] = (byte) (crc32 & 0x000000ff);
data_length = offset+4;
#if 1
if (data_length != section_length + 3)
{
fprint_err("### PAT length %d, section length+3 %d\n",
data_length,section_length+3);
return 1;
}
#endif
crc32 = crc32_block(0xffffffff,data,data_length);
if (crc32 != 0)
{
print_err("### PAT CRC does not self-cancel\n");
return 1;
}
err = TS_program_packet_hdr(0x00,data_length,TS_packet,&TS_hdr_len);
if (err)
{
print_err("### Error constructing PAT packet header\n");
return 1;
}
err = write_TS_packet_parts(output,TS_packet,TS_hdr_len,NULL,0,
data,data_length,0x00,FALSE,0);
if (err)
{
print_err("### Error writing PAT\n");
return 1;
}
return 0;
}
/*
* Write out a Transport Stream PMT, given a PMT datastructure
*
* - `output` is the TS output context returned by `tswrite_open`
* - `pmt_pid` is the PID for the PMT.
* - 'pmt' is the datastructure containing the PMT information
*
* Returns 0 if it worked, 1 if something went wrong.
*/
extern int write_pmt(TS_writer_p output,
uint32_t pmt_pid,
pmt_p pmt)
{
int ii;
byte data[3+1021]; // maximum PMT size
byte TS_packet[TS_PACKET_SIZE];
int TS_hdr_len;
int err;
int section_length;
int offset, data_length;
uint32_t crc32;
#if DEBUG_WRITE_PACKETS
fprint_msg("|| PMT pid %x (%d)\n",pmt_pid,pmt_pid);
#endif
if (pmt_pid < 0x0010 || pmt_pid > 0x1ffe)
{
fprint_err("### PMT PID %03x is outside legal range\n",pmt_pid);
return 1;
}
if (pid_in_pmt(pmt,pmt_pid))
{
fprint_err("### PMT PID and program %d PID are both %03x\n",
pid_index_in_pmt(pmt,pmt_pid),pmt_pid);
return 1;
}
// Much of the PMT should look very familiar, after the PAT