forked from nexgenta/tstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pes.c
3845 lines (3565 loc) · 112 KB
/
pes.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
/*
* PES reading facilities
*
* ***** 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 <errno.h>
#include <string.h>
#include "compat.h"
#include "ts_fns.h"
#include "ps_fns.h"
#include "es_fns.h"
#include "pes_fns.h"
#include "pidint_fns.h"
#include "h262_fns.h"
#include "tswrite_fns.h"
#include "printing_fns.h"
#include "misc_fns.h"
//#define DEBUG
#define DEBUG_READ_PACKETS 0
#define DEBUG_PES_ASSEMBLY 0
#define DEBUG_PROGRAM_INFO 1
#define SHOW_PROGRAM_INFO 0
#define ALLOW_OVERLONG_PACKETS 1
// PES packet stream ids
// See H.222.0 Table 2-17 and Table 2-18
// These are specifically the stream ids used to decide if a PES packet
// contains header data, filler bytes or what
#define STREAM_ID_PROGRAM_STREAM_MAP 0xbc
#define STREAM_ID_PRIVATE_STREAM_1 0xbd
#define STREAM_ID_PADDING_STREAM 0xbe
#define STREAM_ID_PRIVATE_STREAM_2 0xbf
#define STREAM_ID_ECM_STREAM 0xf0
#define STREAM_ID_EMM_STREAM 0xf1
#define STREAM_ID_DSMCC_STREAM 0xf2
#define STREAM_ID_13522_STREAM 0xf3
#define STREAM_ID_H222_A_STREAM 0xf4
#define STREAM_ID_H222_B_STREAM 0xf5
#define STREAM_ID_H222_C_STREAM 0xf6
#define STREAM_ID_H222_D_STREAM 0xf7
#define STREAM_ID_H222_E_STREAM 0xf8
#define STREAM_ID_ANCILLARY_STREAM 0xf9
#define STREAM_ID_PROGRAM_STREAM_DIRECTORY 0xff
// ============================================================
// PES packet data
// ============================================================
/*
* Build a new PES packet datastructure
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
static int build_PES_packet_data(PES_packet_data_p *data)
{
PES_packet_data_p new = malloc(SIZEOF_PES_PACKET_DATA);
if (new == NULL)
{
print_err("### Unable to allocate PES packet datastructure\n");
return 1;
}
new->data = NULL;
new->data_len = 0;
new->es_data_len = 0;
new->length = 0;
new->posn = 0;
new->is_video = TRUE; // a guess
new->data_alignment_indicator = FALSE; // another
new->has_PTS = FALSE; // assumed until told otherwise
*data = new;
return 0;
}
/*
* Add some data to a PES packet datastructure
*
* - `data` is the PES packet datastructure concerned
* - `bytes` is the data to add
* - `bytes_len` is how much data there is
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
static inline int extend_PES_packet_data(PES_packet_data_p data,
byte bytes[],
int bytes_len)
{
if (data->data == NULL)
{
data->data = malloc(bytes_len);
if (data->data == NULL)
{
print_err("### Unable to extend PES packet data array\n");
return 1;
}
memcpy(data->data,bytes,bytes_len);
data->data_len = bytes_len;
}
else
{
data->data = realloc(data->data,data->data_len + bytes_len);
if (data->data == NULL)
{
print_err("### Unable to extend PES packet data array\n");
return 1;
}
memcpy(&(data->data[data->data_len]),bytes,bytes_len);
data->data_len = data->data_len + bytes_len;
}
return 0;
}
/*
* Build a dummy PES packet datastructure.
*
* - `data` is the "new" dummy PES packet. Do not free this,
* as it is remembered statically inside the function.
* - `data_len` is the required (total) size of the dummy PES packet
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
static inline int build_dummy_PES_packet_data(PES_packet_data_p *data,
int data_len)
{
int err;
static PES_packet_data_p local_data = NULL;
if (local_data == NULL)
{
err = build_PES_packet_data(&local_data);
if (err)
{
print_err("### Error building dummy PES packet\n");
return 1;
}
local_data->is_video = FALSE;
}
if (local_data->data == NULL)
{
local_data->data = malloc(data_len);
if (local_data->data == NULL)
{
print_err("### Unable to extend dummy PES packet data array\n");
return 1;
}
memset(local_data->data,0xFF,data_len);
}
else if (data_len > local_data->data_len)
{
local_data->data = realloc(local_data->data,data_len);
if (local_data->data == NULL)
{
print_err("### Unable to extend dummy PES packet data array\n");
return 1;
}
memset(local_data->data,0xFF,data_len);
}
if (data_len != local_data->data_len)
{
int PES_packet_len = data_len - 6;
// Set up the data in the PES packet
local_data->data[0] = 0x00;
local_data->data[1] = 0x00;
local_data->data[2] = 0x01; // end of the packet_start_code_prefix
local_data->data[3] = STREAM_ID_PADDING_STREAM;
if (PES_packet_len > 0xFFFF)
{
local_data->data[4] = 0;
local_data->data[5] = 0;
}
else
{
local_data->data[4] = (byte) ((PES_packet_len & 0xFF00) >> 8);
local_data->data[5] = (byte) ((PES_packet_len & 0x00FF));
}
local_data->data_len = data_len;
}
*data = local_data;
return 0;
}
/*
* Free a PES packet datastructure
*
* - `data` is the PES packet datastructure, which will be freed,
* and returned as NULL.
*/
extern void free_PES_packet_data(PES_packet_data_p *data)
{
if ((*data) == NULL)
return;
if ((*data)->data != NULL)
{
free((*data)->data);
(*data)->data = NULL;
}
(*data)->data_len = 0;
(*data)->length = 0;
free(*data);
*data = NULL;
return;
}
// ============================================================
// Transport Stream support - PID -> PES data
// (datastructure support based on that for pidint lists in ts.c)
// ============================================================
/*
* Initialise a new PID/PES data datastructure.
*/
static int init_peslist(peslist_p list)
{
int ii;
list->length = 0;
list->size = PESLIST_START_SIZE;
list->data = malloc(SIZEOF_PES_PACKET_DATA*PESLIST_START_SIZE);
if (list->data == NULL)
{
print_err("### Unable to allocate PES array in PID/PES data array");
return 1;
}
list->pid = malloc(sizeof(uint32_t)*PESLIST_START_SIZE);
if (list->pid == NULL)
{
free(list->data);
print_err("### Unable to allocate PID array in PID/PES data array\n");
return 1;
}
// Just in case...
for (ii=0; ii<list->size; ii++)
list->data[ii] = NULL;
return 0;
}
/*
* Build a new PID/PES data datastructure.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int build_peslist(peslist_p *list)
{
peslist_p new = malloc(SIZEOF_PESLIST);
if (new == NULL)
{
print_err("### Unable to allocate PID/PES data array\n");
return 1;
}
if (init_peslist(new))
return 1;
*list = new;
return 0;
}
/*
* Tidy up and free a PID/PES data datastructure after we've finished with it
*
* Clears the datastructure, frees it and returns `list` as NULL.
*
* Does nothing if `list` is already NULL.
*/
static void free_peslist(peslist_p *peslist)
{
peslist_p list = *peslist;
if (list == NULL)
return;
if (list->data != NULL)
{
int ii;
for (ii=0; ii<list->length; ii++)
{
if (list->data[ii] != NULL)
free_PES_packet_data(&list->data[ii]);
}
free(list->data);
list->data = NULL;
}
if (list->pid != NULL)
{
free(list->pid);
list->pid = NULL;
}
list->length = 0;
list->size = 0;
free(list);
*peslist = NULL;
}
/*
* Lookup a PID to find its index in a PID/PES data array.
*
* Note that if `list` is NULL, then -1 will be returned.
*
* Returns its index (0 or more) if the PID is in the list, -1 if it is not.
*/
static inline int pid_index_in_peslist(peslist_p list,
uint32_t pid)
{
int ii;
if (list == NULL)
return -1;
for (ii = 0; ii < list->length; ii++)
{
if (list->pid[ii] == pid)
return ii;
}
return -1;
}
/*
* Lookup a PID to see if it is in a PID/PES data array.
*
* Note that if `list` is NULL, then FALSE will be returned.
*
* Returns TRUE if the PID is in the list, FALSE if it is not.
*/
static inline int pid_in_peslist(peslist_p list,
uint32_t pid)
{
return pid_index_in_peslist(list,pid) != -1;
}
/*
* Start a new PES packet for a PID.
*
* Creates a new entry for the given PID, and returns the corresponding new
* PES packet.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int start_packet_in_peslist(PES_reader_p reader,
uint32_t pid,
int is_video,
PES_packet_data_p *data)
{
int err;
int ii;
peslist_p list = reader->packets;
if (list == NULL)
{
print_err("### Unable to append to NULL PID/PES data array\n");
return 1;
}
err = build_PES_packet_data(data);
if (err)
{
print_err("### Unable to build new PES packet datastructure"
" for PID/PES data array\n");
return 1;
}
(*data)->is_video = is_video;
for (ii=0; ii<list->length; ii++)
{
if (list->pid[ii] == pid)
{
// There is already an entry for this PID - does it have data?
if (list->data[ii] != NULL)
{
PES_packet_data_p packet = list->data[ii];
if (reader->give_warning)
fprint_err("!!! PID %04x (%d) already has an unfinished PES packet"
" associated with it\n %d byte%s of %d bytes were already"
" read - ignoring them\n",pid,pid,packet->data_len,
(packet->data_len==1?"":"s"),packet->length);
free_PES_packet_data(&(list->data[ii]));
}
list->data[ii] = *data;
return 0;
}
}
// Otherwise, we need to add a new entry to the list
if (list->length == list->size)
{
int newsize = list->size + PESLIST_INCREMENT;
list->data = realloc(list->data,newsize*SIZEOF_PES_PACKET_DATA);
if (list->data == NULL)
{
print_err("### Unable to extend PID/PES data array\n");
free_PES_packet_data(data);
return 1;
}
list->pid = realloc(list->pid,newsize*sizeof(uint32_t));
if (list->pid == NULL)
{
print_err("### Unable to extend PID/PES data array\n");
free_PES_packet_data(data);
return 1;
}
list->size = newsize;
}
list->pid[list->length] = pid;
list->data[list->length] = *data;
list->length++;
return 0;
}
/*
* Find the PES packet for a PID.
*
* NB: returns `data` as NULL if the data for the PID *is* NULL.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static inline int find_packet_in_peslist(peslist_p list,
uint32_t pid,
PES_packet_data_p *data)
{
int index = pid_index_in_peslist(list,pid);
if (index == -1)
return 1;
*data = list->data[index];
return 0;
}
/*
* Clear the PES packet for a PID.
*
* Leaves the entry in the PID/PES array (with NULL data pointer) around for
* reuse.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int clear_packet_in_peslist(peslist_p list,
uint32_t pid)
{
int index;
if (list == NULL)
{
print_msg("Unable to clear PES packet in NULL PID/PES data array\n");
return 1;
}
index = pid_index_in_peslist(list,pid);
if (index == -1)
{
fprint_err("### Unable to find PID %04x (%x) in PID/PES data array,"
" so cannot clear its data\n",pid,pid);
return 1;
}
list->data[index] = NULL;
return 0;
}
// ============================================================
// Reading the Program Stream
// ============================================================
/*
* Return the next PES packet from the input PS file
*
* - `reader` is a PES reader context
* - `packet_data` is the packet data (NULL if EOF is read)
*
* Returns 0 if all goes well, EOF if end of file is read, and 1 if
* something goes wrong.
*/
static int read_next_PES_packet_from_PS(PES_reader_p reader,
PES_packet_data_p *packet_data)
{
// Read PS packets
// If a packet is a PES packet, return it
for (;;)
{
int err;
byte stream_id; // The packet's stream id
int keep = FALSE; // Keep this packet?
int is_video = FALSE;
struct PS_packet packet = {0};
struct PS_pack_header header = {0};
err = read_PS_packet_start(reader->psreader,FALSE,&reader->posn,
&stream_id);
if (err == EOF)
{
*packet_data = NULL;
return EOF;
}
else if (err)
return 1;
// If it's the pack header, read it and ignore it
if (stream_id == 0xba)
{
err = read_PS_pack_header_body(reader->psreader,&header);
if (err == EOF)
{
fprint_err("!!! Unexpected EOF - partial PS packet at "
OFFSET_T_FORMAT " ignored\n",reader->posn);
*packet_data = NULL;
return EOF;
}
else if (err)
{
fprint_err("### Error reading data for pack header starting at "
OFFSET_T_FORMAT "\n",reader->posn);
return 1;
}
continue;
}
err = read_PS_packet_body(reader->psreader,stream_id,&packet);
if (err == EOF)
{
fprint_err("!!! Unexpected EOF - partial PS packet at "
OFFSET_T_FORMAT " ignored\n",reader->posn);
*packet_data = NULL;
return EOF;
}
else if (err)
{
fprint_err("### Error reading PS packet starting at "
OFFSET_T_FORMAT "\n",reader->posn);
return 1;
}
// We have to decide whether to discard this data because it is not
// a "PES" packet.
// First, we know we can discard things that we are sure are part of the
// PS infrastructure. Note that we don't need to check for 0xba (pack
// header) because we already did that above, and we shouldn't have to
// check for 0xb9 (MPEG_program_end_code), because that should already
// have been interpreted as EOF by read_PS_packet_start().
if (stream_id == 0xbb || // PS system header
stream_id == 0xbc || // PS map
stream_id == 0x01) // PS directory
{
/* pass */;
}
else if (stream_id == PRIVATE1_AUDIO_STREAM_ID)
{
// It's private stream 1, traditionally used for Dolby (AC-3) audio
if (reader->video_only)
keep = FALSE;
else if (reader->audio_stream_id == stream_id)
{
keep = TRUE;
is_video = FALSE;
}
}
else if ((stream_id >= 0xc0) && (stream_id <= 0xdf))
{
// It's a non-Dolby audio stream
if (reader->video_only)
keep = FALSE;
else if (reader->audio_stream_id == stream_id)
{
keep = TRUE;
is_video = FALSE;
}
else if (reader->audio_stream_id == 0)
{
// Aha - we're looking for an audio stream to use, and this is it
reader->audio_stream_id = stream_id;
keep = TRUE;
is_video = FALSE;
if (reader->give_info)
fprint_msg("Selecting audio stream number %d\n",stream_id & 0x1F);
}
}
else if (stream_id >= 0xe0 && stream_id <= 0xef)
{
// It's a video stream. We're assuming we only get one video
// stream, so this is a "keeper" regardless
keep = TRUE;
is_video = TRUE;
}
if (keep)
{
err = build_PES_packet_data(packet_data);
if (err) return 1;
// We needn't copy the bytes from one "packet" to another,
// it's easier to just transfer the array, if we're careful
(*packet_data)->data = packet.data;
(*packet_data)->data_len = packet.data_len;
(*packet_data)->length = packet.data_len;
(*packet_data)->posn = reader->posn;
(*packet_data)->is_video = is_video;
// So the data array is no longer "present" in the orignal "packet"
packet.data = NULL;
packet.data_len = 0;
break;
}
clear_PS_packet(&packet);
}
return 0;
}
// ============================================================
// Reading the Transport Stream
// ============================================================
/*
* Look in the current program map to decide on the video and audio PIDs
*
* This is only expected to be called if the PMT has changed.
*
* TODO: The detection of different types of audio is not really strong enough
* TODO: Take account of descriptors in deciding what things are, as well
*/
static void decide_pids(PES_reader_p reader)
{
pmt_p pmt = reader->program_map;
int ii;
int had_video = FALSE;
int had_audio = FALSE;
if (pmt == NULL)
return;
// Since we're not expecting our datastructure to be very big,
// or this function to be called very often, we can look at audio
// and video separately.
for (ii = 0; ii < pmt->num_streams; ii++)
{
if (IS_VIDEO_STREAM_TYPE(pmt->streams[ii].stream_type))
{
if (had_video)
{
if (reader->give_warning)
fprint_err("!!! Multiple video streams in TS program %d, PMT"
" at " OFFSET_T_FORMAT " - using PID %04x\n",
reader->program_number,reader->posn,reader->video_pid);
break;
}
else if (reader->video_pid == 0)
{
reader->video_pid = pmt->streams[ii].elementary_PID;
switch (pmt->streams[ii].stream_type)
{
case AVC_VIDEO_STREAM_TYPE:
reader->is_h264 = TRUE;
reader->video_type = VIDEO_H264;
break;
case MPEG2_VIDEO_STREAM_TYPE:
case MPEG1_VIDEO_STREAM_TYPE: // well, more-or-less
reader->is_h264 = FALSE;
reader->video_type = VIDEO_H262;
break;
case AVS_VIDEO_STREAM_TYPE:
reader->is_h264 = FALSE;
reader->video_type = VIDEO_AVS;
break;
default:
reader->is_h264 = FALSE;
reader->video_type = VIDEO_UNKNOWN;
break;
}
if (reader->give_info)
fprint_msg(" Chose video PID %04x\n",reader->video_pid);
}
else if (pmt->streams[ii].elementary_PID != reader->video_pid)
{
if (reader->give_warning)
fprint_err("!!! Video streams altered in TS program %d, PMT"
" at " OFFSET_T_FORMAT " - still using PID %04x\n",
reader->program_number,reader->posn,reader->video_pid);
break;
}
had_video = TRUE;
}
}
if (reader->video_only)
{
if (reader->give_info)
print_msg(" Not interested in any audio streams\n");
return;
}
for (ii = 0; ii < pmt->num_streams; ii++)
{
// Note that the audio detection will accept either DVB or ADTS Dolby
// (AC-3) stream types
if (IS_AUDIO_STREAM_TYPE(pmt->streams[ii].stream_type))
{
if (had_audio)
{
if (reader->give_warning)
fprint_err("!!! Multiple audio streams in TS program %d, PMT"
" at " OFFSET_T_FORMAT " - using PID %04x\n",
reader->program_number,reader->posn,reader->audio_pid);
break;
}
else if (reader->audio_pid == 0)
{
reader->audio_pid = pmt->streams[ii].elementary_PID;
if (reader->give_info)
fprint_msg(" Chose audio PID %04x\n",reader->audio_pid);
if (IS_DOLBY_STREAM_TYPE(pmt->streams[ii].stream_type))
{
// Remember what stream type this Dolby data is using
// - we'll assume that this doesn't change under our feet,
// and thus we don't need to report if it changes
reader->dolby_stream_type = pmt->streams[ii].stream_type;
// If we're not overriding the output stream type, use it as is
if (!reader->override_dolby_stream_type)
reader->output_dolby_stream_type = reader->dolby_stream_type;
}
}
else if (pmt->streams[ii].elementary_PID != reader->audio_pid)
{
if (reader->give_warning)
fprint_err("!!! Audio streams altered in TS program %d, PMT"
" at " OFFSET_T_FORMAT " - still using PID %04x\n",
reader->program_number,reader->posn,reader->audio_pid);
break;
}
had_audio = TRUE;
}
}
if (!reader->override_program_data)
{
// Our output program data is to be the same as our input
reader->output_video_pid = reader->video_pid;
reader->output_audio_pid = reader->audio_pid;
reader->output_pcr_pid = reader->pcr_pid;
reader->output_pmt_pid = reader->pmt_pid;
}
}
/*
* Called to use the information extracted from a PMT packet.
*
* Note: don't free `pmt` after this call, as it is remembered
* by the `reader`.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
static int refine_TS_program_info(PES_reader_p reader,
pmt_p pmt)
{
// If this is the *first* PMT, then just adopt its data wholesale
if (reader->program_map == NULL)
{
reader->program_map = pmt;
reader->pcr_pid = pmt->PCR_pid;
#if DEBUG_PROGRAM_INFO
if (reader->give_info)
{
fprint_msg("PMT packet at " OFFSET_T_FORMAT ": first PMT, used as-is\n",
reader->posn);
report_pmt(TRUE," ",reader->program_map);
}
#else
if (reader->give_info)
report_pmt(TRUE," ",reader->program_map);
#endif
// And use its information to determine our video/audio PIDs
decide_pids(reader);
reader->got_program_data = TRUE;
return 0;
}
// Otherwise, check if this PMT contains the same information
if (pmt->program_number != reader->program_number)
{
if (reader->give_info)
fprint_msg("Ignoring PMT for program %d\n",pmt->program_number);
free_pmt(&pmt); // since our caller will not free it
return 0;
}
if (same_pmt(pmt,reader->program_map))
{
free_pmt(&pmt); // since our caller will not free it
return 0;
}
// Grumble or replace? Maybe both is safest
if (reader->give_warning)
{
fprint_err("!!! PMT in TS packet at " OFFSET_T_FORMAT
" replaces previous program information\n",reader->posn);
print_err(" Program information was:\n");
report_pmt(FALSE," ",reader->program_map);
print_err(" New program information is:\n");
report_pmt(FALSE," ",pmt);
}
else if (reader->give_info)
{
#if DEBUG_PROGRAM_INFO
fprint_msg("PMT packet at " OFFSET_T_FORMAT ": updating program info\n",
reader->posn);
#endif
report_pmt(TRUE," ",pmt);
}
free_pmt(&reader->program_map);
reader->program_map = pmt;
reader->pcr_pid = pmt->PCR_pid;
// And use this new information to determine/check our video/audio PIDs
decide_pids(reader);
return 0;
}
/*
* Call this on each PMT found for the program being read from TS,
* to refine its idea of what is in that program.
*
* - `reader` is the PES reader context corresponding to the newly
* opened file.
*
* Note that it is assumed that a particular program number will refer
* to the same program stream throughout a TS file.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
static int extract_and_refine_TS_program_info(PES_reader_p reader,
uint32_t pmt_pid,
byte pmt_data[],
int pmt_data_len)
{
int err;
pmt_p pmt = NULL;
err = extract_pmt(FALSE,pmt_data,pmt_data_len,pmt_pid,&pmt);
if (err)
{
print_err("### Error extracting stream list from PMT\n");
return 1;
}
// If it's the wrong program, we're not interested
if (pmt->program_number != reader->program_number)
{
#if DEBUG_PROGRAM_INFO
if (reader->give_info)
fprint_msg("PMT packet at " OFFSET_T_FORMAT ": program number %d (not %d)\n",
reader->posn,pmt->program_number,reader->program_number);
#endif
free_pmt(&pmt);
return 0;
}
err = refine_TS_program_info(reader,pmt);
if (err)
{
print_err("### Error refining TS program information from PMT\n");
free_pmt(&pmt);
return 1;
}
// Mustn't free `pmt` because it is remembered by the reader
return 0;
}
/*
* Look up the (first) PAT in a Transport Stream
*
* - `reader` is the PES reader context corresponding to the newly
* opened file.
* - if `quiet` is not true, and the program number was given as 0, then
* the function will report on finding the first PAT and what program
* number it deduces therefrom.
*
* Note that it is assumed that a particular program number will refer
* to the same program stream throughout a TS file.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
static int find_first_PAT(PES_reader_p reader)
{
int err;
int num_read;
pidint_list_p prog_list = NULL;
err = find_pat(reader->tsreader,0,FALSE,!reader->give_info,
&num_read,&prog_list);
if (err)
{
print_err("### Error finding first PAT\n");
return 1;
}
if (prog_list->length == 0)
{
fprint_err("### No programs defined in first PAT (at " OFFSET_T_FORMAT
")\n",reader->tsreader->posn - TS_PACKET_SIZE);
free_pidint_list(&prog_list);
return 1;
}
else if (prog_list->length > 1 && reader->give_info)
print_msg("Multiple programs in PAT - using the first\n\n");
if (reader->program_number == 0)
{
reader->program_number = prog_list->number[0];
reader->pmt_pid = prog_list->pid[0];
}
else
{
int ii;
int got_program = FALSE;
for (ii=0; ii<prog_list->length; ii++)
{
if (prog_list->number[ii] == reader->program_number)
{
got_program = TRUE;
reader->pmt_pid = prog_list->pid[ii];
break;
}
}
if (!got_program)
{
fprint_err("### Program %d not found in first PAT at "
OFFSET_T_FORMAT "\n",reader->program_number,
reader->tsreader->posn - TS_PACKET_SIZE);
return 1;
}
}
free_pidint_list(&prog_list);
return 0;
}
/*
* Look up the (first after the first PAT) PMT in a Transport Stream
*
* - `reader` is the PES reader context corresponding to the newly
* opened file.
*
* Returns 0 if all goes well, 1 if something goes wrong, EOF if end-of-file
* is found before the first (useful) PMT.
*/
static int find_first_PMT(PES_reader_p reader)
{
int err;
int nread = 0;
pmt_p pmt = NULL;
for (;;)
{
err = find_next_pmt(reader->tsreader,reader->pmt_pid,-1,0,
FALSE,!reader->give_info,&nread,&pmt);
if (err)
{
fprint_err("### Error looking for program %d PMT with PID %04x"
" after first PAT\n",reader->program_number,reader->pmt_pid);
return 1;
}
if (pmt->program_number == reader->program_number)
break;
if (reader->give_info)
fprint_msg("(Program is %d, not %d - ignoring it)\n",
pmt->program_number,reader->program_number);
free_pmt(&pmt);
}
err = refine_TS_program_info(reader,pmt);
if (err)
{
print_err("### Error refining TS program information from PMT\n");
free_pmt(&pmt);
return 1;
}
// Mustn't free `pmt` because it is remembered by the reader
return 0;
}
/*