-
Notifications
You must be signed in to change notification settings - Fork 95
/
ps.c
1967 lines (1829 loc) · 62.8 KB
/
ps.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 reading program stream data.
*
* ***** 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 <fcntl.h>
#ifdef _WIN32
#include <stddef.h>
#include <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include "compat.h"
#include "ps_fns.h"
#include "ts_fns.h"
#include "pes_fns.h"
#include "pidint_fns.h"
#include "misc_fns.h"
#include "printing_fns.h"
#define DEBUG 0
#define DEBUG_AC3 0
// How many bytes to look through for a pack header, before giving up
#define PACK_HEADER_SEARCH_DISTANCE 100000
// If we lose where we are, should we look for the next PS pack header?
// (instead of just giving up)
#define RECOVER_BROKEN_PS 1
// ============================================================
// PS to TS datastructures
// ============================================================
// Data we need to write PAT/PMT and otherwise manage our program streams
//
// DVD allows one video stream and up to 8 audio streams,
// but we're only interested in a single video and a single audio stream
struct program_data
{
// Information supplied by the user
uint32_t transport_stream_id;
uint32_t program_number;
uint32_t pmt_pid; // PID to use for the PMT
uint32_t pcr_pid; // PID to use for the PCR
uint32_t video_pid; // PID to use for our single stream of video
uint32_t audio_pid; // PID to use for our single stream of audio
int video_stream; // Which stream id our video is (-1 means use first)
int audio_stream; // Which stream id our audio is (-1 means use first)
int want_ac3; // True means audio_stream is private_1/AC3 substream
int audio_substream; // Which substream id if DVD and want_ac3
int video_type; // Is our video H.264, H.262, etc. (user must decide)
int is_dvd; // Is our data DVD program stream (ditto)
int output_dolby_as_dvb; // Output Dolby (AC-3) audio as DVB or ATSC?
// Information derived from the data
// PAT and PMT data
pidint_list_p prog_list;
pmt_p pmt;
};
// ============================================================
// Read ahead support
// ============================================================
/*
* Read some more data into our read-ahead buffer.
*
* Returns 0 if it succeeds, EOF if the end-of-file is read, otherwise
* 1 if some error occurs.
*/
static inline int get_more_data(PS_reader_p ps)
{
// Call `read` directly - we don't particularly mind if we get a "short"
// read, since we'll just catch up later on
#ifdef _WIN32
int len = _read(ps->input,&ps->data,PS_READ_AHEAD_SIZE);
#else
ssize_t len = read(ps->input,&ps->data,PS_READ_AHEAD_SIZE);
#endif
if (len == 0)
return EOF;
else if (len == -1)
{
fprint_err("### Error reading next bytes: %s\n",strerror(errno));
return 1;
}
ps->data_posn += ps->data_len; // length of the *last* buffer
ps->data_len = len;
ps->data_end = ps->data + len; // one beyond the last byte
ps->data_ptr = ps->data; // start at the beginning
return 0;
}
/*
* Build a program stream context attached to an input file. This handles
* read-ahead buffering for the PS.
*
* - `input` is the file stream to read from.
* - If `quiet`, then don't report on ignored bytes at the start of the file
* - `ps` is the new PS context
*
* Returns 0 if all goes well, 1 otherwise.
*/
extern int build_PS_reader(int input,
int quiet,
PS_reader_p *ps)
{
int err;
PS_reader_p new = malloc(SIZEOF_PS_READER);
if (new == NULL)
{
print_err("### Unable to allocate program stream read context\n");
return 1;
}
new->input = input;
new->data_posn = 0;
new->data_len = 0;
new->start = 0;
err = get_more_data(new);
if (err)
{
print_err("### Unable to start reading from new PS read context\n");
free(new);
return 1;
}
// And look for the first pack header
err = find_PS_pack_header_start(new,FALSE,PACK_HEADER_SEARCH_DISTANCE,
&(new->start));
if (err)
{
fprint_err("### File does not appear to be PS\n"
" Cannot find PS pack header in first %d bytes of file\n",
PACK_HEADER_SEARCH_DISTANCE);
free(new);
return 1;
}
// Seeking won't work on standard input, so don't even try
if (input != STDIN_FILENO)
{
// But we don't *really* want to have read its start yet
err = seek_using_PS_reader(new,new->start);
if (err)
{
print_err("### Error seeking to start of first pack header\n");
free(new);
return 1;
}
}
if (!quiet && new->start != 0)
fprint_err("!!! PS file does not start with pack header\n"
" First PS pack header is at " OFFSET_T_FORMAT "\n",new->start);
*ps = new;
return 0;
}
/*
* Tidy up the PS read-ahead context after we've finished with it.
*
* Specifically:
*
* - free the datastructure
* - set `ps` to NULL
*
* Does not close the associated file.
*/
extern void free_PS_reader(PS_reader_p *ps)
{
if (*ps != NULL)
{
(*ps)->input = -1; // "forget" our input
free(*ps);
*ps = NULL;
}
}
/*
* Open a PS file for reading.
*
* - `name` is the name of the file. If this is NULL, then standard input
* is used.
* - If `quiet`, then don't report on ignored bytes at the start of the file
* - `ps` is the new PS context
*
* Returns 0 if all goes well, 1 otherwise.
*/
extern int open_PS_file(char *name,
int quiet,
PS_reader_p *ps)
{
int f;
if (name == NULL)
f = STDIN_FILENO;
else
{
f = open_binary_file(name,FALSE);
if (f == -1) return 1;
}
return build_PS_reader(f,quiet,ps);
}
/*
* Close a PS file, and free the reader context
*
* (Doesn't close the file if it was standard input)
*
* Returns 0 if all goes well, 1 otherwise.
*/
extern int close_PS_file(PS_reader_p *ps)
{
if ((*ps)->input != STDIN_FILENO)
{
int err = close_file((*ps)->input);
if (err) return 1;
}
free_PS_reader(ps);
return 0;
}
/*
* Given a program stream, attempt to determine if it holds H.262 or H.264
* data.
*
* Leaves the PS rewound to its "start".
*
* NOTE: It is probably better to use determine_PS_video_type().
*
* - `ps` is the program stream to check (assumed just to have been
* opened/built). This cannot be standard input, as it must be
* seekable.
* - `is_h264` is the result
*
* Returns 0 if all goes well, 1 if there was an error (including the
* stream not appearing to be either).
*/
extern int determine_if_PS_is_h264(PS_reader_p ps,
int *is_h264)
{
int err;
PES_reader_p reader;
// Try to decide what sort of data stream we have
// The simplest (albeit clumsy) way to do this is to use technology
// that already does this for us - i.e., build a temporary PES reader
// around our file
// It's then safe to build the temporary PES reader
err = build_PS_PES_reader(ps,FALSE,FALSE,&reader);
if (err)
{
print_err("### Error trying to determine PS stream type\n");
return 1;
}
// Which knows the file type
*is_h264 = reader->is_h264;
(void) free_PES_reader(&reader);
// And then make sure that our file position is where we think it should be
err = rewind_program_stream(ps);
if (err)
{
print_err("### Error rewinding PS file after determining its type\n");
return 1;
}
return 0;
}
/*
* Given a program stream, attempt to determine what type of video data it
* contains.
*
* Leaves the PS rewound to its "start".
*
* - `ps` is the program stream to check (assumed just to have been
* opened/built). This cannot be standard input, as it must be
* seekable.
* - `video_type` is the result. Calls determine_PES_video_type().
*
* Returns 0 if all goes well, 1 if there was an error (including the
* stream not appearing to be either).
*/
extern int determine_PS_video_type(PS_reader_p ps,
int *video_type)
{
int err;
PES_reader_p reader;
// Try to decide what sort of data stream we have
// The simplest (albeit clumsy) way to do this is to use technology
// that already does this for us - i.e., build a temporary PES reader
// around our file
// It's then safe to build the temporary PES reader
err = build_PS_PES_reader(ps,FALSE,FALSE,&reader);
if (err)
{
print_err("### Error trying to determine PS stream type\n");
return 1;
}
// Which thinks it knows the file type
*video_type = reader->video_type;
(void) free_PES_reader(&reader);
// And then make sure that our file position is where we think it should be
err = rewind_program_stream(ps);
if (err)
{
print_err("### Error rewinding PS file after determining its type\n");
return 1;
}
return 0;
}
/*
* Seek within the PS file.
*
* Note that if the intent is to *rewind* to the start of the PS data,
* then `rewind_program_stream` should be used instead, as offset 0 is
* not necessarily the same as the start of the program stream.
*
* - `ps` is the PS read-ahead context
* - `posn` is the file offset to seek to
*
* Return 0 if all goes well, 1 if something goes wrong
*/
extern int seek_using_PS_reader(PS_reader_p ps,
offset_t posn)
{
int err = seek_file(ps->input,posn);
if (err) return 1;
ps->data_posn = posn;
ps->data_len = 0;
return get_more_data(ps);
}
/*
* Rewind the PS context to the remembered "start of data"
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
extern int rewind_program_stream(PS_reader_p ps)
{
return seek_using_PS_reader(ps,ps->start);
}
/*
* Retrieve the next N bytes from the program stream, into an existing array.
*
* - `ps` is the PS read-ahead context
* - `num_bytes` is how many bytes to read
* - `buffer` is the buffer to read them into
* - `posn` is the offset of said data in the file (NULL if the value is not
* wanted).
*
* Returns 0 if all goes well, EOF if end-of-file is encountered before all
* of the bytes have been read, 1 if some other error occurred.
*/
static int read_PS_bytes(PS_reader_p ps,
int num_bytes,
byte *buffer,
offset_t *posn)
{
int err;
int offset = 0;
int num_bytes_wanted = num_bytes;
int num_bytes_left = ps->data_end - ps->data_ptr;
if (posn != NULL)
*posn = ps->data_posn + (ps->data_ptr - ps->data);
for (;;)
{
if (num_bytes_left < num_bytes_wanted)
{
memcpy(&(buffer[offset]),ps->data_ptr,num_bytes_left);
offset += num_bytes_left;
num_bytes_wanted -= num_bytes_left;
err = get_more_data(ps);
if (err) return err;
num_bytes_left = ps->data_len;
}
else
{
memcpy(&(buffer[offset]),ps->data_ptr,num_bytes_wanted);
ps->data_ptr += num_bytes_wanted;
break;
}
}
return 0;
}
// ============================================================
// Primitives
// ============================================================
/*
* Print out a stream id in a manner consistent with the PS usages
* of the stream id values.
*/
extern void print_stream_id(int is_msg,
byte stream_id)
{
byte number;
char *str = NULL;
switch (stream_id)
{
// H.222 Program stream specific codes
case 0xB9: str = "PS MPEG_program_end_code"; break;
case 0xBA: str = "PS Pack header start code"; break;
case 0xBB: str = "PS System header start code"; break;
case 0xBC: str = "PS Program stream map"; break;
case 0xFF: str = "PS Program stream directory"; break;
// Other "simple" values from H.222 Table 2-18, page 32
case 0xBD: str = "Private stream 1"; break;
case 0xBE: str = "Padding stream"; break;
case 0xBF: str = "Private stream 2"; break;
case 0xF0: str = "ECM stream"; break;
case 0xF1: str = "EMM stream"; break;
case 0xF2: str = "DSMCC stream"; break;
case 0xF3: str = "13522 stream"; break;
case 0xF4: str = "H.222.1 A stream"; break;
case 0xF5: str = "H.222.1 B stream"; break;
case 0xF6: str = "H.222.1 C stream"; break;
case 0xF7: str = "H.222.1 D stream"; break;
case 0xF8: str = "H.222.1 E stream"; break;
case 0xF9: str = "Ancillary stream"; break;
case 0x00: str = "H.262 Picture"; break;
case 0xB2: str = "H.262 User data"; break;
case 0xB3: str = "H.262 Sequence header"; break;
case 0xB4: str = "H.262 Sequence error"; break;
case 0xB5: str = "H.262 Extension"; break;
case 0xB7: str = "H.262 Sequence end"; break;
case 0xB8: str = "H.262 Group start"; break;
default: str = NULL; break;
}
if (str != NULL)
fprint_msg_or_err(is_msg,str);
else if (stream_id >= 0xC0 && stream_id <=0xDF)
{
number = stream_id & 0x1F;
fprint_msg_or_err(is_msg,"Audio stream 0x%02X",number);
}
else if (stream_id >= 0xE0 && stream_id <= 0xEF)
{
number = stream_id & 0x0F;
fprint_msg_or_err(is_msg,"Video stream 0x%X",number);
}
else if (stream_id >= 0xFC && stream_id <= 0xFE)
fprint_msg_or_err(is_msg,"Reserved data stream");
else
fprint_msg_or_err(is_msg,"Unrecognised stream id");
}
/*
* Look for the start (the first 4 bytes) of the next program stream packet.
*
* Assumes that (for some reason) alignment has been lost, and thus it is
* necessary to scan forwards to find the next 00 00 01 prefix.
*
* Otherwise equivalent to a call of `read_PS_packet_start`.
*
* - `ps` is the PS read-ahead context we're reading from
* - if `verbose`, then we want to explain what we're doing
* - if `max` is non-zero, then it is the maximum number of bytes
* to scan before giving up.
* - `posn` is the file offset of the start of the packet
* - `stream_id` is the identifying byte, after the 00 00 01 prefix. Note
* that this is set correctly if MPEG_program_end_code was read, and is
* 0 if an error occurred.
*
* Returns:
* * 0 if it succeeds,
* * EOF if EOF is read, or an MPEG_program_end_code is read, or
* * 1 if some error (including the first 3 bytes not being 00 00 01) occurs.
*/
extern int find_PS_packet_start(PS_reader_p ps,
int verbose,
uint32_t max,
offset_t *posn,
byte *stream_id)
{
int err;
byte prev1 = 0xff;
byte prev2 = 0xff;
byte prev3 = 0xff;
uint32_t count = 0;
*stream_id = 0;
for (;;)
{
byte *ptr;
for (ptr = ps->data_ptr; ptr < ps->data_end; ptr++)
{
if (prev3 == 0x00 && prev2 == 0x00 && prev1 == 0x01)
{
if (*ptr == 0xB9) // MPEG_program_end_code
{
if (verbose)
print_msg("Stopping at MPEG_program_end_code\n");
*stream_id = 0xB9;
return EOF;
}
else
{
*stream_id = *ptr;
*posn = ps->data_posn + (ptr - ps->data) - 3;
ps->data_ptr = ptr + 1;
return 0;
}
}
if (max > 0)
{
count ++;
if (count > max)
{
fprint_err("### No PS packet start found in %d bytes\n",max);
return 1;
}
}
prev3 = prev2;
prev2 = prev1;
prev1 = *ptr;
}
// We've run out of data - get some more
err = get_more_data(ps);
if (err) return err;
}
}
/*
* Look for the next PS pack header.
*
* Equivalent to calling `find_PS_packet_start` until `stream_id` is 0xBA
* (in other words, equivalent to having read the pack header start with
* `read_PS_packet_start`).
*
* If you want to call `read_PS_packet_start` to read this pack header start
* in again, then call ``seek_using_PS_reader(ps,posn)`` to reposition ready
* to read it.
*
* - `ps` is the PS read-ahead context we're reading from
* - if `verbose`, then the 00 00 01 XX sequences found will be logged
* to stderr, indicating the progress of our search
* - if `max` is non-zero, then it is the maximum number of bytes
* to scan before giving up.
* - `posn` is the file offset of the start of the packet found
*
* Returns:
* * 0 if it succeeds,
* * EOF if EOF is read, or an MPEG_program_end_code is read, or
* * 1 if some error (including the first 3 bytes not being 00 00 01) occurs.
*/
extern int find_PS_pack_header_start(PS_reader_p ps,
int verbose,
uint32_t max,
offset_t *posn)
{
int err;
byte stream_id = 0;
while (stream_id != 0xBA)
{
err = find_PS_packet_start(ps,verbose,max,posn,&stream_id);
if (err)
{
print_err("### Error looking for PS pack header (0xBA)\n");
return 1;
}
if (verbose)
{
fprint_err(" Found: stream id %02X at " OFFSET_T_FORMAT " (",
stream_id,*posn);
print_stream_id(FALSE,stream_id);
print_err(")\n");
}
}
return 0;
}
/*
* Read in (the rest of) a PS packet according to its length.
*
* Suitable for use reading PS PES packets and PS system header packets.
*
* NOTE that the `data` buffer in the `packet` is realloc'ed by this
* function. It is thus important to ensure that the `packet` datastructure
* contains a NULL pointer for said buffer before the first call of this
* function.
*
* - `ps` is the PS read-ahead context we're reading from
* - `stream_id` identifies what sort of packet it is
* - `packet` is the packet we're reading the PES packet into.
*
* Returns 0 if it succeeds, EOF if it unexpectedly reads end-of-file, and 1
* if some other error occurs. `packet->data` will be NULL if EOF is returned.
*/
extern int read_PS_packet_body(PS_reader_p ps,
byte stream_id,
PS_packet_p packet)
{
int err;
byte buf[2];
// First, the packet length
err = read_PS_bytes(ps,2,buf,NULL);
if (err)
{
fprint_err("### %s reading PS packet length\n",
(err==EOF?"Unexpected end of file":"Error"));
if (packet->data!=NULL) free(packet->data);
packet->data = NULL;
return err;
}
packet->packet_length = (buf[0] << 8) | buf[1];
#if DEBUG
fprint_msg("Packet length %d\n",packet->packet_length);
#endif
// Remember that the packet length is the length of data
// *after* the packet length field. Also, it is only allowed
// to be 0 within a Transport Stream, so it should never be 0 for us
// - but let's check anyway
if (packet->packet_length == 0)
{
print_err("### Packet has length 0 - not allowed in PS\n");
if (packet->data!=NULL) free(packet->data);
packet->data = NULL;
return 1;
}
// Since we are, in general, expecting to write the packet out again
// at some point, it is convenient to also store the leading bytes
#if 0 // XXX naughty stuff
packet->data = realloc(packet->data,packet->packet_length + 6 + 10);
#else
packet->data = realloc(packet->data,packet->packet_length + 6);
#endif
if (packet->data == NULL)
{
print_err("### Unable to allocate PS packet data buffer\n");
return 1;
}
packet->data_len = packet->packet_length + 6;
// So let us reestablish said leading bytes
packet->data[0] = 0;
packet->data[1] = 0;
packet->data[2] = 1;
packet->data[3] = stream_id;
packet->data[4] = buf[0];
packet->data[5] = buf[1];
// And now we can read in the rest of the packet's data
err = read_PS_bytes(ps,packet->packet_length,&(packet->data[6]),NULL);
if (err)
{
fprint_err("### %s reading rest of PS packet\n",
(err==EOF?"Unexpected end of file":"Error"));
if (packet->data!=NULL) free(packet->data);
packet->data = NULL;
return err;
}
#if 0 // XXX naughty stuff - add some trailing zero bytes
packet->data[packet->data_len + 0] = 0;
packet->data[packet->data_len + 1] = 0;
packet->data[packet->data_len + 2] = 0;
packet->data[packet->data_len + 3] = 0;
packet->data[packet->data_len + 4] = 0;
packet->data[packet->data_len + 5] = 0;
packet->data[packet->data_len + 6] = 0;
packet->data[packet->data_len + 7] = 0;
packet->data[packet->data_len + 8] = 0;
packet->data[packet->data_len + 9] = 0;
packet->packet_length += 10;
packet->data_len += 10;
#endif
return 0;
}
/*
* Read in the body of the pack header (but *not* the system header packets
* therein).
*
* - `ps` is the PS read-ahead context we're reading from
* - `hdr` is the packet we've read
*
* Returns 0 if it succeeds, EOF if it unexpectedly reads end-of-file, and
* 1 if some other error occurs.
*/
extern int read_PS_pack_header_body(PS_reader_p ps,
PS_pack_header_p hdr)
{
int err;
byte dummy[8]; // a 3 bit length means no more than 7 stuffing bytes
// Read just the first 8 bytes, in case it's an MPEG-1 pack header
err = read_PS_bytes(ps,8,hdr->data,NULL);
if (err)
{
fprint_err("### %s reading body of PS pack header\n",
(err==EOF?"Unexpected end of file":"Error"));
return err;
}
if ((hdr->data[0] & 0xF0) == 0x20)
{
#if DEBUG
print_msg("ISO/IEC 11171-1/MPEG-1 pack header\n");
print_data(TRUE,"Pack header",hdr->data,8,8);
#endif
hdr->pack_stuffing_length = 0; // since it doesn't exist
hdr->scr =
(((uint64_t)(hdr->data[0] & 0x09)) << 29) |
(((uint64_t) hdr->data[1] ) << 22) |
(((uint64_t)(hdr->data[2] & 0xFE)) << 14) |
(((uint64_t) hdr->data[3] ) << 7) |
(((uint64_t)(hdr->data[4] & 0xFE)) >> 1);
hdr->program_mux_rate =
(((uint32_t)(hdr->data[5] & 0x7F)) << 15) |
(((uint32_t) hdr->data[6] ) << 7) |
(((uint32_t)(hdr->data[7] & 0xFE)) >> 1);
// In MPEG-1, SCR = NINT(SysClockFreq * t[i]) % 2**33
// where SysClockFreq = 90,000 Hz
//
// In H.222.0, SCR = SCRbase[i] * 300 + SCRext[i]
// = (((SysClockFreq * t[i]) DIV 300) % 2**33) * 300 +
// ((SysClockFreq * t[i]) DIV 1) % 300
// where SysClockFreq = 27,000,000 Hz
// Fudge these to match H.222.0 in case anyone tries to use them later on
hdr->scr = hdr->scr * 300;
hdr->scr_base = hdr->scr / 300;
hdr->scr_extn = 0; // i.e., hdr->scr % 300
}
else
{
#if DEBUG
print_msg("ISO/IEC 13818-1/H.222.0 pack header\n");
#endif
err = read_PS_bytes(ps,2,&(hdr->data[8]),NULL);
if (err)
{
fprint_err("### %s reading last 2 bytes of body of PS pack header\n",
(err==EOF?"Unexpected end of file":"Error"));
return err;
}
#if DEBUG
print_data(TRUE,"Pack header",hdr->data,10,10);
#endif
hdr->scr_base =
(((uint64_t)(hdr->data[0] & 0x38)) << 27) |
(((uint64_t)(hdr->data[0] & 0x03)) << 28) |
(((uint64_t) hdr->data[1] ) << 20) |
(((uint64_t)(hdr->data[2] & 0xF8)) << 12) |
(((uint64_t)(hdr->data[2] & 0x03)) << 13) |
(((uint64_t) hdr->data[3] ) << 5) |
(((uint64_t)(hdr->data[4] & 0xF8)) >> 3);
hdr->scr_extn =
(((uint32_t)(hdr->data[4] & 0x03)) << 7) |
(((uint32_t) hdr->data[5] ) >> 1);
hdr->scr = hdr->scr_base * 300 + hdr->scr_extn;
hdr->program_mux_rate =
(((uint32_t)hdr->data[6] << 14)) |
(((uint32_t)hdr->data[7] << 6)) |
(((uint32_t)hdr->data[8] >> 2));
hdr->pack_stuffing_length = hdr->data[9] & 0x07;
}
#if DEBUG // XXX
fprint_msg("Pack header body: scr_base " LLU_FORMAT ", scr_extn %u, scr "
LLU_FORMAT ", mux rate %u, stuffing %d\n",
hdr->scr_base,hdr->scr_extn,hdr->scr,hdr->program_mux_rate,
hdr->pack_stuffing_length);
#endif
// And ignore that many stuffing bytes...
if (hdr->pack_stuffing_length > 0)
{
err = read_PS_bytes(ps,hdr->pack_stuffing_length,dummy,NULL);
if (err)
{
fprint_err("### %s reading PS pack header stuffing bytes\n",
(err==EOF?"Unexpected end of file":"Error"));
return err;
}
}
return 0;
}
/*
* Clear the contents of a PS packet datastructure. Frees the internal
* `data` array.
*/
extern void clear_PS_packet(PS_packet_p packet)
{
if (packet->data != NULL)
{
free(packet->data);
packet->data = NULL;
packet->data_len = 0;
}
packet->packet_length = 0;
}
/*
* Tidy up and free a PS packet datastructure after we've finished with it.
*
* Empties the PS packet datastructure, frees it, and sets `unit` to NULL.
*
* If `unit` is already NULL, does nothing.
*/
extern void free_PS_packet(PS_packet_p *packet)
{
if (*packet == NULL)
return;
clear_PS_packet(*packet);
free(*packet);
*packet = NULL;
}
/*
* Read in the start (the first 4 bytes) of the next program stream packet.
*
* If the bytes read don't appear to be valid (i.e., they do not start with
* the 00 00 01 prefix), then the next pack header will be sought and read in.
*
* Note that sequences of 00 bytes before the 00 00 01 will be ignored.
*
* - `ps` is the PS read-ahead context we're reading from
* - if `verbose`, then we want to explain what we're doing
* - `posn` is the file offset of the start of the packet
* - `stream_id` is the identifying byte, after the 00 00 01 prefix. Note
* that this is set correctly if MPEG_program_end_code was read, and is
* 0 if an error occurred.
*
* Returns:
* * 0 if it succeeds,
* * EOF if EOF is read, or an MPEG_program_end_code is read,
* * 2 if the bytes read are not 00 00 01 `stream_id`, or
* * 1 if some other error occurs.
*/
extern int read_PS_packet_start(PS_reader_p ps,
int verbose,
offset_t *posn,
byte *stream_id)
{
int err;
byte buf[4];
*stream_id = 0;
err = read_PS_bytes(ps,4,buf,posn);
if (err == EOF)
return EOF;
else if (err)
{
print_err("### Error reading start of PS packet\n");
return 1;
}
// It's not uncommon to get a sequence of 00 bytes between packs
// (in particular, after an audio pack). We don't really want to grumble
// about such things, but just to cope
if (buf[0] == 0 && buf[1] == 0 && buf[2] == 0)
{
#if 0 // XXX
fprint_msg("// %02x %02x %02x %02x\n",buf[0],buf[1],buf[2],buf[3]);
#endif
while (buf[2] == 0) // we already know buf[0] and buf[1] are zero
{
buf[2] = buf[3];
err = read_PS_bytes(ps,1,&(buf[3]),posn);
if (err == EOF)
return EOF;
else if (err)
{
print_err("### Error skipping 00 bytes before start of PS packet\n");
return 1;
}
}
#if 0 // XXX
fprint_msg("\\\\ %02x %02x %02x %02x\n",buf[0],buf[1],buf[2],buf[3]);
#endif
}
if (buf[0] != 0 || buf[1] != 0 || buf[2] != 1)
{
fprint_err("!!! PS packet at " OFFSET_T_FORMAT " should start "
"00 00 01, but instead found %02X %02X %02X\n",
*posn,buf[0],buf[1],buf[2]);
#if RECOVER_BROKEN_PS
print_err("!!! Attempting to find next PS pack header\n");
err = find_PS_pack_header_start(ps,TRUE,0,posn);
if (err == EOF)
return EOF;
else if (err)
{
print_err("### Error trying to find start of next pack header\n");
return 1;
}
fprint_err("!!! Continuing with PS pack header at " OFFSET_T_FORMAT
"\n",*posn);
*stream_id = 0xBA;
return 0;
#else
return 2;
#endif
}
*stream_id = buf[3];
#if DEBUG
fprint_msg("Packet at " OFFSET_T_FORMAT ", stream id %02X (",*posn,*stream_id);
print_stream_id(TRUE,*stream_id);
print_msg(")\n");
#endif
if (buf[3] == 0xB9) // MPEG_program_end_code
{
if (verbose)
print_msg("Stopping at MPEG_program_end_code\n");
return EOF;
}
else
return 0;
}
/* Determine the details of an AC3 stream, by looking at its start
*
* Naughtily assume that `data` is long enough...
*/
static inline void determine_ac3_details(byte *data, int verbose,
byte *bsmod, byte *acmod)
{
// The end of the syncinfo
int fscod = (data[4] & 0xC0) >> 6;
int frmsizecode = (data[4] & 0x3F);
// The start of the bit stream info
int bsid = (data[5] & 0xF8) >> 3;
*bsmod = (data[5] & 0x07);
*acmod = (data[6] & 0xC0) >> 6;
if (verbose)
{
fprint_msg(" fscod %x (sample rate %skHz)\n",fscod,
(fscod==0?"48":fscod==1?"44.1":fscod==2?"32":"??"));
fprint_msg(" frmsizecode %x\n",frmsizecode);
fprint_msg(" bsid %x (%s)\n",bsid,
(bsid==8?"standard":bsid==6?"A52a alternate":
bsid<8?"standard subset":"???"));
fprint_msg(" bsmod %x (%s)\n",*bsmod,BSMOD_STR(*bsmod,*acmod));
fprint_msg(" acmod %x (%s)\n",*acmod,ACMOD_STR(*acmod));
}
}
/*
* Inspect the given PS packet, and determine if it contains AC3 or DTS audio data.
*