forked from Zabrimus/VDRSternELEC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-vdr-2.8.2-pes2ts.patch
More file actions
1799 lines (1775 loc) · 60.6 KB
/
Copy path08-vdr-2.8.2-pes2ts.patch
File metadata and controls
1799 lines (1775 loc) · 60.6 KB
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
diff --new-file -ru8bBwd a/channels.h b/channels.h
--- a/channels.h 2026-06-01 18:59:58.000000000 +0200
+++ b/channels.h 2026-06-30 07:51:53.925950267 +0200
@@ -155,24 +155,32 @@
int Ppid(void) const { return ppid; }
int Vtype(void) const { return vtype; }
const int *Apids(void) const { return apids; }
const int *Dpids(void) const { return dpids; }
const int *Spids(void) const { return spids; }
int Apid(int i) const { return (0 <= i && i < MAXAPIDS) ? apids[i] : 0; }
int Dpid(int i) const { return (0 <= i && i < MAXDPIDS) ? dpids[i] : 0; }
int Spid(int i) const { return (0 <= i && i < MAXSPIDS) ? spids[i] : 0; }
+ const char (*Alangs() const)[MAXLANGCODE2] { return alangs; }
+ const char (*Dlangs() const)[MAXLANGCODE2] { return dlangs; }
+ const char (*Slangs() const)[MAXLANGCODE2] { return slangs; }
const char *Alang(int i) const { return (0 <= i && i < MAXAPIDS) ? alangs[i] : ""; }
const char *Dlang(int i) const { return (0 <= i && i < MAXDPIDS) ? dlangs[i] : ""; }
const char *Slang(int i) const { return (0 <= i && i < MAXSPIDS) ? slangs[i] : ""; }
+ const int *Atypes(void) const { return atypes; }
+ const int *Dtypes(void) const { return dtypes; }
+ const uchar *Stypes(void) const { return subtitlingTypes; }
int Atype(int i) const { return (0 <= i && i < MAXAPIDS) ? atypes[i] : 0; }
int Dtype(int i) const { return (0 <= i && i < MAXDPIDS) ? dtypes[i] : 0; }
uchar SubtitlingType(int i) const { return (0 <= i && i < MAXSPIDS) ? subtitlingTypes[i] : uchar(0); }
uint16_t CompositionPageId(int i) const { return (0 <= i && i < MAXSPIDS) ? compositionPageIds[i] : uint16_t(0); }
uint16_t AncillaryPageId(int i) const { return (0 <= i && i < MAXSPIDS) ? ancillaryPageIds[i] : uint16_t(0); }
+ const uint16_t *CompositionPageIds(void) const { return compositionPageIds; }
+ const uint16_t *AncillaryPageIds(void) const { return ancillaryPageIds; }
int Tpid(void) const { return tpid; }
const int *Caids(void) const { return caids; }
int Ca(int Index = 0) const { return Index < MAXCAIDS ? caids[Index] : 0; }
int Nid(void) const { return nid; }
int Tid(void) const { return tid; }
int Sid(void) const { return sid; }
int Rid(void) const { return rid; }
int Lcn(void) const { return lcn; }
diff --new-file -ru8bBwd a/Makefile b/Makefile
--- a/Makefile 2026-06-01 18:59:58.000000000 +0200
+++ b/Makefile 2026-06-30 07:51:53.925667102 +0200
@@ -85,17 +85,17 @@
endif
# Object files
SILIB = $(LSIDIR)/libsi.a
OBJS = args.o audio.o channels.o ci.o config.o cutter.o device.o diseqc.o dvbdevice.o dvbci.o\
dvbplayer.o dvbspu.o dvbsubtitle.o eit.o eitscan.o epg.o filter.o font.o i18n.o interface.o keys.o\
- lirc.o menu.o menuitems.o mtd.o nit.o osdbase.o osd.o pat.o player.o plugin.o positioner.o\
+ lirc.o menu.o menuitems.o mtd.o nit.o osdbase.o osd.o pat.o pes2ts.o player.o plugin.o positioner.o\
receiver.o recorder.o recording.o remote.o remux.o ringbuffer.o sdt.o sections.o shutdown.o\
skinclassic.o skinlcars.o skins.o skinsttng.o sourceparams.o sources.o spu.o status.o svdrp.o themes.o thread.o\
timers.o tools.o transfer.o vdr.o videodir.o
DEFINES += $(CDEFINES)
INCLUDES += $(CINCLUDES)
ifdef HDRDIR
diff --new-file -ru8bBwd a/pes2ts.c b/pes2ts.c
--- a/pes2ts.c 1970-01-01 01:00:00.000000000 +0100
+++ b/pes2ts.c 2026-06-30 07:49:49.259272489 +0200
@@ -0,0 +1,1261 @@
+/*
+ * pes2ts.c: Convert legacy PES recordings to TS
+ *
+ * See the main source file 'vdr.c' for copyright information and
+ * how to reach the author.
+ *
+ * $Id$
+ */
+
+#include "pes2ts.h"
+#include "config.h"
+#include "recording.h"
+#include "tools.h"
+
+// --- Helpers ---------------------------------------------------------------
+
+#define USE __attribute__((warn_unused_result)) // marks functions where the retrun value must be checked
+
+static cString LogFileName;
+
+#define LOGFILESUFFIX "/pes2ts.log"
+
+static void SetLogFileName(const char *FileName)
+{
+ LogFileName = AddDirectory(FileName, LOGFILESUFFIX);
+ unlink(LogFileName);
+}
+
+static void Log(const char *s)
+{
+ if (FILE *f = fopen(LogFileName, "a")) {
+ fputs(s, f);
+ fputc('\n', f);
+ fclose(f);
+ }
+}
+
+static void Info(int Level, const char *Fmt, ...) __attribute__ ((format (printf, 2, 3)));
+static void Info(int Level, const char *Fmt, ...)
+{
+ va_list ap;
+ va_start(ap, Fmt);
+ cString s = cString::vsprintf(Fmt, ap);
+ if (Level > 1)
+ s = cString::sprintf("%*s%s", (Level - 1) * 3, "", *s);
+ va_end(ap);
+ if (Level <= SysLogLevel) {
+ fputs(s, stdout);
+ fputc('\n', stdout);
+ }
+ Log(s);
+}
+
+static void Warning(const char *Fmt, ...) __attribute__ ((format (printf, 1, 2)));
+static void Warning(const char *Fmt, ...)
+{
+ va_list ap;
+ va_start(ap, Fmt);
+ cString s = cString::vsprintf(Fmt, ap);
+ s = cString::sprintf("WARNING: %s", *s);
+ va_end(ap);
+ fputs(s, stdout);
+ fputc('\n', stdout);
+ Log(s);
+}
+
+static bool Error(const char *Fmt, ...) __attribute__ ((format (printf, 1, 2)));
+static bool Error(const char *Fmt, ...)
+{
+ va_list ap;
+ va_start(ap, Fmt);
+ cString s = cString::vsprintf(Fmt, ap);
+ s = cString::sprintf("ERROR: %s", *s);
+ va_end(ap);
+ fputs(s, stderr);
+ fputc('\n', stderr);
+ Log(s);
+ return false; // convenience return value
+}
+
+// --- CopyMarks -------------------------------------------------------------
+
+#define MARKSFILESUFFIX "/marks"
+
+static USE bool CopyMarks(const char *PesFileName, const char *TsFileName)
+{
+ cString PesMarksFileName = AddDirectory(PesFileName, MARKSFILESUFFIX ".vdr");
+ cString TsMarksFileName = AddDirectory(TsFileName, MARKSFILESUFFIX);
+ if (FILE *fp = fopen(PesMarksFileName, "r")) {
+ Info(2, "copying PES marks file %s to %s", *PesMarksFileName, *TsMarksFileName);
+ if (FILE *ft = fopen(TsMarksFileName, "w")) {
+ cReadLine ReadLine;
+ while (char *s = ReadLine.Read(fp)) {
+ fprintf(ft, "%s\n", s);
+ Info(3, "%s", s);
+ }
+ fclose(ft);
+ }
+ else if (errno != ENOENT) {
+ fclose(fp);
+ return Error("%s: %m", *TsMarksFileName);
+ }
+ fclose(fp);
+ }
+ else if (errno != ENOENT)
+ return Error("%s: %m", *PesMarksFileName);
+ return true; // a missing marks file is not an error
+}
+
+// --- cRecordingTimerId -----------------------------------------------------
+
+// Creates a recording timer id to make sure replay of the resulting TS recording can
+// begin immediately, and makes sure the id is removed when finished.
+
+class cRecordingTimerId {
+private:
+ cString fileName;
+public:
+ cRecordingTimerId(const char *FileName);
+ ~cRecordingTimerId();
+ };
+
+cRecordingTimerId::cRecordingTimerId(const char *FileName)
+{
+ fileName = FileName;
+ SetRecordingTimerId(fileName, cString::sprintf("%d@%s", 0, Setup.SVDRPHostName));
+}
+
+cRecordingTimerId::~cRecordingTimerId()
+{
+ SetRecordingTimerId(fileName, NULL);
+}
+
+// --- cPesIndex -------------------------------------------------------------
+
+// Stripped down version of cIndexFile, just enough to read a PES index file.
+
+struct __attribute__((packed)) tIndexPes {
+ uint32_t offset;
+ uchar type;
+ uchar number;
+ uint16_t reserved;
+ };
+
+class cPesIndex {
+private:
+ cString fileName;
+ tIndexPes *index;
+ int last;
+public:
+ cPesIndex(void);
+ ~cPesIndex();
+ USE bool Load(const char *FileName);
+ USE bool IsValid(int Index) { return Index >= 0 && Index <= last; }
+ USE bool Get(int Index, uchar &FileNumber, uint32_t &FileOffset, bool &Independent, int &Length);
+ };
+
+#define INDEXFILESUFFIX "/index.vdr"
+
+cPesIndex::cPesIndex(void)
+{
+ index = NULL;
+ last = -1;
+}
+
+bool cPesIndex::Load(const char *FileName)
+{
+ fileName = AddDirectory(FileName, INDEXFILESUFFIX);
+ if (access(fileName, R_OK) == 0) {
+ struct stat buf;
+ if (stat(fileName, &buf) == 0) {
+ int delta = int(buf.st_size % sizeof(tIndexPes));
+ if (delta) {
+ delta = sizeof(tIndexPes) - delta;
+ Warning("invalid file size (%u) in '%s', continuing anyway", uint32_t(buf.st_size), *fileName);
+ // no 'return false' here
+ }
+ last = int((buf.st_size + delta) / sizeof(tIndexPes) - 1);
+ if (last >= 0) {
+ int size = last + 1;
+ index = MALLOC(tIndexPes, size);
+ if (index) {
+ int f = open(fileName, O_RDONLY);
+ if (f >= 0) {
+ if (safe_read(f, index, size_t(buf.st_size)) != buf.st_size) {
+ Error("can't read from file '%s'", *fileName);
+ free(index);
+ last = -1;
+ index = NULL;
+ }
+ close(f);
+ }
+ else
+ Error("%s: %m", *fileName);
+ }
+ else
+ Error("can't allocate %zd bytes for index '%s'", size * sizeof(tIndexPes), *fileName);
+ }
+ else
+ Error("%s: improper file size", *fileName);
+ }
+ else
+ Error("%s: %m", *fileName);
+ }
+ else
+ Error("missing index file %s", *fileName);
+ return index != NULL;
+}
+
+cPesIndex::~cPesIndex()
+{
+ free(index);
+}
+
+bool cPesIndex::Get(int Index, uchar &FileNumber, uint32_t &FileOffset, bool &Independent, int &Length)
+{
+ if (IsValid(Index)) {
+ FileNumber = index[Index].number;
+ FileOffset = index[Index].offset;
+ Independent = index[Index].type == 1;
+ if (Index < last) {
+ uchar fn = index[Index + 1].number;
+ uint32_t fo = index[Index + 1].offset;
+ if (fn == FileNumber)
+ Length = int(fo - FileOffset);
+ else
+ Length = -1; // this means "everything up to EOF"
+ }
+ else
+ Length = -1;
+ return true;
+ }
+ return false;
+}
+
+// --- cPesResume ------------------------------------------------------------
+
+// Stripped down version of cResumeFile, just enough to read a PES resume file.
+
+class cPesResume {
+private:
+ int index;
+public:
+ cPesResume(void);
+ USE bool Read(const char *FileName);
+ USE bool Write(const char *TsFileName);
+ };
+
+#define RESUMEFILESUFFIX "/resume"
+
+cPesResume::cPesResume(void)
+{
+ index = -1;
+}
+
+bool cPesResume::Read(const char *FileName)
+{
+ cString PesResumeFileName = AddDirectory(FileName, RESUMEFILESUFFIX ".vdr");
+ int f = open(PesResumeFileName, O_RDONLY);
+ if (f >= 0) {
+ Info(2, "reading PES resume file %s", *PesResumeFileName);
+ int Read = safe_read(f, &index, sizeof(index));
+ close(f);
+ if (Read != sizeof(index)) {
+ index = -1;
+ return Error("%s: %m", *PesResumeFileName);
+ }
+ Info(3, "index = %d", index);
+ }
+ else if (errno != ENOENT)
+ return Error("%s: %m", *PesResumeFileName);
+ return true; // a missing resume file is not an error
+}
+
+bool cPesResume::Write(const char *TsFileName)
+{
+ if (index >= 0) {
+ cString TsResumeFileName = AddDirectory(TsFileName, RESUMEFILESUFFIX);
+ Info(2, "writing TS resume file %s", *TsResumeFileName);
+ FILE *f = fopen(TsResumeFileName, "w");
+ if (f) {
+ fprintf(f, "I %d\n", index);
+ Info(3, "I %d", index);
+ fclose(f);
+ }
+ else
+ return Error("%s: %m", *TsResumeFileName);
+ }
+ return true;
+}
+
+// --- cPesSummary -----------------------------------------------------------
+
+#define SUMMARYFILESUFFIX "/summary.vdr"
+
+class cPesSummary {
+private:
+ cString data[3];
+public:
+ USE bool Read(const char *FileName);
+ const char *Title(void) { return data[0]; }
+ const char *ShortText(void) { return data[1]; }
+ const char *Description(void) { return data[2]; }
+ };
+
+bool cPesSummary::Read(const char *FileName)
+{
+ cString SummaryFileName = AddDirectory(FileName, SUMMARYFILESUFFIX);
+ if (FILE *f = fopen(SummaryFileName, "r")) {
+ Info(2, "reading PES summary file %s", *SummaryFileName);
+ int line = 0;
+ cReadLine ReadLine;
+ while (char *s = ReadLine.Read(f)) {
+ if (*s) {
+ if (*data[line]) {
+ data[line].Append("|");
+ data[line].Append(s);
+ }
+ else
+ data[line] = s;
+ if (line < 2)
+ line++;
+ }
+ }
+ fclose(f);
+ if (*data[1] && strlen(data[1]) > 80) {
+ // If line 1 is too long, it can't be the short text,
+ // so assume the short text is missing and concatenate
+ // line 1 and line 2 to be the description:
+ if (*data[2]) {
+ data[1].Append("|");
+ data[1].Append(data[2]);
+ }
+ data[2] = data[1];
+ data[1] = NULL;
+ }
+ Info(3, "Title = '%s'", *data[0]);
+ Info(3, "Short text = '%s'", *data[1]);
+ Info(3, "Description = '%s'", *data[2]);
+ }
+ else if (errno != ENOENT)
+ return Error("%s: %m", *SummaryFileName);
+ return true; // a missing summary file is not an error
+}
+
+// --- cPesInfo --------------------------------------------------------------
+
+class cPesInfo {
+private:
+ cStringList info;
+ cComponents components;
+public:
+ USE bool Read(const char *FileName);
+ USE bool Write(const char *TsFileName);
+ cComponents *Components(void) { return &components;}
+ };
+
+#define INFOFILESUFFIX "/info"
+
+bool cPesInfo::Read(const char *FileName)
+{
+ cString InfoFileName = AddDirectory(FileName, INFOFILESUFFIX ".vdr");
+ if (access(InfoFileName, R_OK) == 0) {
+ Info(2, "reading PES info file %s", *InfoFileName);
+ if (FILE *f = fopen(InfoFileName, "r")) {
+ cReadLine ReadLine;
+ while (char *s = ReadLine.Read(f)) {
+ if (*s == 'X') {
+ int i = components.NumComponents();
+ components.SetComponent(i, skipspace(s + 1));
+ if (!*components.Component(i)->language) {
+ // This is a dummy component, inserted to handle an offset in the PES file.
+ // We need it during the conversion from PES to TS, but not in the final TS info file.
+ Info(3, "%s - skipped!", s);
+ continue;
+ }
+ }
+ info.Append(strdup(s));
+ Info(3, "%s", s);
+ }
+ fclose(f);
+ }
+ else
+ return Error("%s: %m", *InfoFileName);
+ }
+ else {
+ cPesSummary PesSummary;
+ if (PesSummary.Read(FileName)) {
+ if (PesSummary.Title())
+ info.Append(strdup(cString::sprintf("T %s", PesSummary.Title())));
+ if (PesSummary.ShortText())
+ info.Append(strdup(cString::sprintf("S %s", PesSummary.ShortText())));
+ if (PesSummary.Description())
+ info.Append(strdup(cString::sprintf("D %s", PesSummary.Description())));
+ }
+ else
+ return false;
+ }
+ Info(2, "taking priority and lifetime from PES info file name %s", *InfoFileName);
+ int Lifetime = MAXLIFETIME;
+ int Priority = MAXPRIORITY;
+ int n = 0;
+ int len = strlen(FileName);
+ const char *p = FileName + len - 1;
+ while (len-- > 0) {
+ if (*p == '.') {
+ n++;
+ if (n == 2)
+ Lifetime = atoi(p + 1);
+ else if (n == 3) {
+ Priority = atoi(p + 1);
+ break;
+ }
+ }
+ p--;
+ }
+ info.Append(strdup(cString::sprintf("L %d", Lifetime)));
+ info.Append(strdup(cString::sprintf("P %d", Priority)));
+ Info(3, "priority = %d", Priority);
+ Info(3, "lifetime = %d", Lifetime);
+ return true;
+}
+
+bool cPesInfo::Write(const char *TsFileName)
+{
+ cString InfoFileName = AddDirectory(TsFileName, INFOFILESUFFIX);
+ Info(2, "writing TS info file %s", *InfoFileName);
+ if (FILE *f = fopen(InfoFileName, "w")) {
+ for (int i = 0; i < info.Size(); i++) {
+ fprintf(f, "%s\n", info[i]);
+ Info(3, "%s", info[i]);
+ }
+ fclose(f);
+ return true;
+ }
+ return Error("%s: %m", *InfoFileName);
+}
+
+// --- cPesFile --------------------------------------------------------------
+
+class cPesFile {
+private:
+ cString fileName;
+ int number;
+ int file;
+ uint32_t size;
+ uchar frame[MAXFRAMESIZE];
+public:
+ cPesFile(void);
+ ~cPesFile();
+ USE bool Init(const char *FileName);
+ // Initializes the PES file handler.
+ // Returns false in case of an error.
+ USE bool Open(int Number);
+ // Opens the PES recording file with the given number.
+ // Returns false in case of an error.
+ void Close(void);
+ // Closes the currently open PES file.
+ USE const uchar *Read(int Number, uint32_t Offset, int Length, int &Bytes);
+ // Reads Length bytes at Offset from the PES file with the given Number.
+ // Returns a pointer to the read bytes. Bytes returns the number of
+ // bytes actually read, which must normally be equal to Length, but
+ // is the remaining number of bytes in the PES file if Length is -1.
+ // Returns NULL in case of an error.
+ };
+
+#define MAXFILESPERRECORDINGPES 255
+#define RECORDFILESUFFIXPES "/%03d.vdr"
+
+cPesFile::cPesFile(void)
+{
+ number = 0;
+ file = -1;
+ size = 0;
+}
+
+cPesFile::~cPesFile()
+{
+ Close();
+}
+
+bool cPesFile::Init(const char *FileName)
+{
+ fileName = FileName;
+ return true;
+}
+
+bool cPesFile::Open(int Number)
+{
+ if (number == Number)
+ return true;
+ Close();
+ if (0 < Number && Number <= MAXFILESPERRECORDINGPES) {
+ cString Name = cString::sprintf("%s" RECORDFILESUFFIXPES, *fileName, Number);
+ if (access(Name, R_OK) == 0) {
+ file = open(Name, O_RDONLY | O_LARGEFILE);
+ if (file >= 0) {
+ size = FileSize(Name);
+ return true;
+ }
+ }
+ Error("%s: %m", *Name);
+ }
+ else
+ Error("invalid file number '%d'", Number);
+ return false;
+}
+
+void cPesFile::Close(void)
+{
+ if (file >= 0) {
+ close(file);
+ file = -1;
+ }
+}
+
+const uchar *cPesFile::Read(int Number, uint32_t Offset, int Length, int &Bytes)
+{
+ if (Length < 0)
+ Length = size - Offset;
+ if (Length <= MAXFRAMESIZE) {
+ if (Open(Number)) {
+ if (Offset >= 0 && lseek(file, Offset, SEEK_SET) == Offset) {
+ Bytes = safe_read(file, frame, Length);
+ if (Bytes == Length)
+ return frame;
+ Error("invalid length '%d', only read '%d' bytes", Length, Bytes);
+ }
+ else
+ Error("invalid offset '%u'", Offset);
+ }
+ else
+ Error("invalid number '%d'", Number);
+ }
+ else
+ Error("invalid length '%d'", Length);
+ return NULL;
+}
+
+// --- cPesReader ------------------------------------------------------------
+
+class cPesReader {
+private:
+ cPesIndex index;
+ cPesFile file;
+public:
+ USE bool Init(const char *FileName);
+ // Initializes this PES reader.
+ USE bool HasData(int Index) { return index.IsValid(Index); }
+ // Returns true if data exists for the given Index.
+ const uchar *ReadPesFrame(int Index, bool &Independent, int &Bytes);
+ // Reads one complete PES frame at the given Index.
+ // Returns a pointer to the read bytes. Bytes returns the number of
+ // bytes actually read. Independent is true if this in an I-frame.
+ // Returns NULL in case of an error.
+ };
+
+bool cPesReader::Init(const char *FileName)
+{
+ return file.Init(FileName) && index.Load(FileName);
+}
+
+const uchar *cPesReader::ReadPesFrame(int Index, bool &Independent, int &Bytes)
+{
+ uchar FileNumber;
+ uint32_t FileOffset;
+ int Length;
+ if (index.Get(Index, FileNumber, FileOffset, Independent, Length))
+ return file.Read(FileNumber, FileOffset, Length, Bytes);
+ return NULL;
+}
+
+// --- cPesSubtitleAssembler -------------------------------------------------
+
+#define MAX_PES_PACKET_SIZE (0xFFFF + 6) // 6 = PES header size
+
+class cPesSubtitleAssembler {
+private:
+ uchar *data;
+ int size;
+ int length;
+ uint16_t compositionPageId;
+ bool complete;
+public:
+ cPesSubtitleAssembler(void);
+ ~cPesSubtitleAssembler();
+ USE bool Put(const uchar *Data, int Length);
+ const uchar *Get(int &Length);
+ uint16_t CompositionPageId(void) { return compositionPageId; }
+ };
+
+cPesSubtitleAssembler::cPesSubtitleAssembler(void)
+{
+ size = MAX_PES_PACKET_SIZE;
+ data = MALLOC(uchar, size);
+ length = 0;
+ compositionPageId = 0;
+ complete = false;
+}
+
+cPesSubtitleAssembler::~cPesSubtitleAssembler()
+{
+ free(data);
+}
+
+bool cPesSubtitleAssembler::Put(const uchar *Data, int Length)
+{
+ if (Data && PesLongEnough(Length)) {
+ int PayloadOffset = PesPayloadOffset(Data);
+ int SubstreamHeaderLength = 4;
+ bool ResetSubtitleAssembler = Data[PayloadOffset + 3] == 0x00;
+
+ // Compatibility mode for old subtitles plugin:
+ if ((Data[7] & 0x01) && (Data[PayloadOffset - 3] & 0x81) == 0x01 && Data[PayloadOffset - 2] == 0x81) {
+ SubstreamHeaderLength = 0;
+ ResetSubtitleAssembler = Data[8] >= 5; // has PTS
+ }
+
+ if (ResetSubtitleAssembler)
+ length = 0;
+
+ if (length == 0) {
+ // First packet, take header:
+ memcpy(data, Data, PayloadOffset);
+ length += PayloadOffset;
+ }
+ Data += PayloadOffset + SubstreamHeaderLength;
+ Length -= (PayloadOffset + SubstreamHeaderLength);
+ if (length + Length > size)
+ return Error("packet too large in subtitle assembler");
+ if (compositionPageId == 0) {
+ for (int i = 0; i < Length; i++) {
+ if (Data[i] == 0x0F && i + 3 < Length) {
+ compositionPageId = (Data[i + 2] << 8) | Data[i + 3];
+ break;
+ }
+ }
+ }
+ memcpy(data + length, Data, Length);
+ length += Length;
+ uint16_t NewLength = length - 6;
+ data[4] = NewLength >> 8;
+ data[5] = NewLength & 0xFF;
+ // Check for end of packet
+ uchar *p = data + length - 1;
+ if (Length < 2048 && *p == 0xFF)
+ complete = true;
+ if (*p-- == 0xFF && *p-- == 0x00 && *p-- == 0x00 && *p-- == (compositionPageId & 0xFF) && *p-- == (compositionPageId >> 8) && *p-- == 0x80 && *p-- == 0x0F)//XXX composition page id
+ complete = true;
+ return true;
+ }
+ return Error("no data for subtitle assembler");
+}
+
+const uchar *cPesSubtitleAssembler::Get(int &Length)
+{
+ if (complete && length > 0) {
+ Length = length;
+ length = 0;
+ complete = false;
+ return data;
+ }
+ return NULL;
+}
+
+// --- cTsGenerator ----------------------------------------------------------
+
+class cTsGenerator {
+private:
+ int pid;
+ uchar counter;
+public:
+ cTsGenerator(int Pid);
+ // Sets up a TS generator for the given Pid.
+ int GenerateTs(const uchar *Data, int Length, bool Pusi, uchar *Dest);
+ // Generates one TS packet from the first bytes in Data, and writes the result
+ // (which is always exactly TS_SIZE bytes in size) to Dest. If Pusi is true, the
+ // "payload unit start indicator" flag will be set.
+ // Returns the number of bytes used from Data.
+ };
+
+cTsGenerator::cTsGenerator(int Pid)
+{
+ pid = Pid;
+ counter = 0;
+}
+
+int cTsGenerator::GenerateTs(const uchar *Data, int Length, bool Pusi, uchar *Dest)
+{
+ uchar *p = Dest;
+ *p++ = TS_SYNC_BYTE; // TS indicator
+ *p++ = (Pusi ? TS_PAYLOAD_START : 0) | (pid >> 8); // flags (3), pid hi (5)
+ *p++ = pid & 0xFF; // pid low
+ int Payload = TS_SIZE - 4; // maximum payload after TS header
+ int Fill = Payload - Length;
+ if (Fill > 0) {
+ // Last TS packet, use adaptation field for stuffing:
+ *p++ = 0x30 | counter; // adaptation field + payload
+ if (Fill == 1) // minimal adaptation field, only length byte
+ *p++ = 0x00; // adaptation_field_length = 0
+ else { // normal adaptation field with stuffing
+ *p++ = Fill - 1; // adaptation_field_length
+ *p++ = 0x00; // adaptation flags
+ if (Fill > 2)
+ memset(p, 0xFF, Fill - 2); // stuffing bytes
+ p += Fill - 2;
+ }
+ // Copy remaining PES data into payload
+ memcpy(p, Data, Length);
+ Payload = Length;
+ }
+ else {
+ *p++ = 0x10 | counter; // payload only
+ memcpy(p, Data, Payload);
+ }
+ counter = (counter + 1) & 0x0F;
+ return Payload; // number of PES bytes written into this TS packet
+}
+
+// --- cPatPmtMaker ----------------------------------------------------------
+
+#define PID_BASE_VIDEO 101
+#define PID_BASE_AUDIO 201
+#define PID_BASE_DOLBY 301
+#define PID_BASE_SUBT 401
+#define PID_BASE_LCPM 501
+
+class cPatPmtMaker : public cPatPmtGenerator {
+private:
+ int vpid;
+ int vtype;
+ int ppid;
+ cVector<int> apids;
+ cVector<int> atypes;
+ char alangs[MAXAPIDS][MAXLANGCODE2];
+ cVector<int> dpids;
+ cVector<int> dtypes;
+ char dlangs[MAXDPIDS][MAXLANGCODE2];
+ cVector<int> spids;
+ cVector<uchar> stypes;
+ char slangs[MAXSPIDS][MAXLANGCODE2];
+ cVector<uint16_t> compositionPageIds; // also used for ancillaryPageIds
+ cVector<bool> usedPids;
+ cComponents *components;
+public:
+ cPatPmtMaker(cComponents *Components);
+ void ResetPids(void);
+ USE bool ListPids(void);
+ void MakePatPmt(void);
+ void SetVpid(int Vpid, int Vtype);
+ void SetApid(int Apid, int Atype, int Index);
+ void SetDpid(int Dpid, int Dtype, int Index);
+ void SetSpid(int Spid, uchar Stype, uint16_t CompositionPageId, int Index);
+ };
+
+cPatPmtMaker::cPatPmtMaker(cComponents *Components)
+{
+ components = Components;
+ ResetPids();
+}
+
+void cPatPmtMaker::ResetPids(void)
+{
+ vpid = 0;
+ vtype = 0;
+ ppid = 0;
+ apids.Clear();
+ atypes.Clear();
+ memset(alangs, 0, sizeof(alangs));
+ dpids.Clear();
+ dtypes.Clear();
+ memset(dlangs, 0, sizeof(dlangs));
+ spids.Clear();
+ stypes.Clear();
+ memset(slangs, 0, sizeof(slangs));
+ compositionPageIds.Clear();
+ usedPids.Clear();
+}
+
+bool cPatPmtMaker::ListPids(void)
+{
+ bool HasPids = false;
+ if (vpid) {
+ Info(3, "vpid = %d %02X", vpid, vtype);
+ HasPids = true;
+ }
+ for (int i = 0; apids[i]; i++) {
+ Info(3, "apid[%d] = %d %02X '%s'", i, apids[i], atypes[i], alangs[i]);
+ HasPids = true;
+ }
+ for (int i = 0; dpids[i]; i++) {
+ Info(3, "dpid[%d] = %d %02X '%s'", i, dpids[i], dtypes[i], dlangs[i]);
+ HasPids = true;
+ }
+ for (int i = 0; spids[i]; i++) {
+ Info(3, "spid[%d] = %d %02X '%s' %d", i, spids[i], stypes[i], slangs[i], compositionPageIds[i]);
+ HasPids = true;
+ }
+ if (!HasPids)
+ return Error("no PIDs found");
+ return true;
+}
+
+void cPatPmtMaker::MakePatPmt(void)
+{
+ SetPids(vpid, vtype, ppid, 0, apids.Data(), atypes.Data(), alangs, dpids.Data(), dtypes.Data(), dlangs, spids.Data(), stypes.Data(), slangs, compositionPageIds.Data(), compositionPageIds.Data());
+}
+
+void cPatPmtMaker::SetVpid(int Vpid, int Vtype)
+{
+ if (vpid != Vpid) {
+ vpid = Vpid;
+ vtype = Vtype;
+ ppid = vpid;
+ MakePatPmt();
+ }
+}
+
+template<class T> int AppendZeroTerminated(cVector<T> &Pids, T Pid)
+{
+ int i = Pids.Size();
+ if (i > 0)
+ i--;
+ Pids[i] = Pid;
+ Pids[i + 1] = 0;
+ return i;
+}
+
+void cPatPmtMaker::SetApid(int Apid, int Atype, int Index)
+{
+ if (!usedPids[Apid]) {
+ int i = AppendZeroTerminated(apids, Apid);
+ atypes.Append(Atype);
+ if (tComponent *Component = components->GetComponent(Index, 2, 0))
+ strn0cpy(alangs[i], Component->language, MAXLANGCODE2);
+ MakePatPmt();
+ usedPids[Apid] = true;
+ }
+}
+
+void cPatPmtMaker::SetDpid(int Dpid, int Dtype, int Index)
+{
+ if (!usedPids[Dpid]) {
+ int i = AppendZeroTerminated(dpids, Dpid);
+ dtypes.Append(Dtype);
+ if (tComponent *Component = components->GetComponent(Index, 2, 0))
+ strn0cpy(dlangs[i], Component->language, MAXLANGCODE2);
+ MakePatPmt();
+ usedPids[Dpid] = true;
+ }
+}
+
+void cPatPmtMaker::SetSpid(int Spid, uchar Stype, uint16_t CompositionPageId, int Index)
+{
+ if (!usedPids[Spid]) {
+ int i = AppendZeroTerminated(spids, Spid);
+ stypes.Append(Stype);
+ if (tComponent *Component = components->GetComponent(Index, 3, 0))
+ strn0cpy(slangs[i], Component->language, MAXLANGCODE2);
+ compositionPageIds.Append(CompositionPageId);
+ MakePatPmt();
+ usedPids[Spid] = true;
+ }
+}
+
+// --- cPes2TsConverter ------------------------------------------------------
+
+#define MIN_PRE_1_3_19_PRIVATESTREAM 10 // the minimum number of unknown PS1 packets to consider this a "pre 1.3.19 private stream"
+
+#define MAX_SUBSTREAMS 32
+#define MAX_PIDS (PID_BASE_LCPM + MAX_SUBSTREAMS)
+
+class cPes2TsConverter {
+private:
+ int pre_1_3_19_PrivateStream;
+ cPatPmtMaker patPmtMaker;
+ cTsGenerator *tsGenerators[MAX_PIDS];
+ cPesSubtitleAssembler *pesSubtitleAssemblers[MAX_SUBSTREAMS];
+ uchar frame[MAXFRAMESIZE];
+ int length;
+ bool analyzing;
+ USE bool MakeTs(const uchar *Data, int Length, int Pid);
+ // Converts the PES packet in Data to a sequence of TS packets for Pid and stores them
+ // in the member variable frame.
+ // Returns false in case of an error.
+ USE bool ProcessPesPacket(const uchar *Data, int Length);
+ // Processes the PES packet in Data.
+ // Returns false in case of an error.
+public:
+ cPes2TsConverter(cComponents *Components);
+ // Components contains the data of the stream components, if available.
+ ~cPes2TsConverter();
+ bool IsPre_1_3_19_PrivateStream(void) { return pre_1_3_19_PrivateStream > MIN_PRE_1_3_19_PRIVATESTREAM; }
+ void SetAnalyzing(bool Analyzing) { analyzing = Analyzing; }
+ // Sets this converter to analyzing mode.
+ void ResetPids(void);
+ // Resets any PIDs detected so far.
+ USE bool ListPids(void);
+ // Lists all PIDs detected so far.
+ // Returns true is any PIDs have been found,
+ USE bool ConvertPesFrame(const uchar *Data, int Length, bool Independent);
+ // Converts the complete PES frame in Data into a TS frame.
+ // If Independent is true, this is an I-frame.
+ // Returns false in case of an error.
+ const uchar *TsFrame(int &Length) { Length = length; return frame; }
+ // Returns the resulting TS frame and its Length.
+ // The result is only valid if the previous call to ConvertPesFrame() has returned true.
+ };
+
+cPes2TsConverter::cPes2TsConverter(cComponents *Components)
+:patPmtMaker(Components)
+{
+ pre_1_3_19_PrivateStream = 0;
+ memset(tsGenerators, 0, sizeof(tsGenerators));
+ memset(pesSubtitleAssemblers, 0, sizeof(pesSubtitleAssemblers));
+ memset(frame, 0, sizeof(frame));
+ length = 0;
+ analyzing = false;
+}
+
+cPes2TsConverter::~cPes2TsConverter()
+{
+ for (int i = 0; i < MAX_PIDS; i++)
+ delete tsGenerators[i];
+ for (int i = 0; i < MAX_SUBSTREAMS; i++)
+ delete pesSubtitleAssemblers[i];
+}
+
+void cPes2TsConverter::ResetPids(void)
+{
+ patPmtMaker.ResetPids();
+}
+
+bool cPes2TsConverter::ListPids(void)
+{
+ return patPmtMaker.ListPids();
+}
+