forked from kynesim/tstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.c
1664 lines (1537 loc) · 47.3 KB
/
es.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 H.264 elementary streams.
*
* ***** 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>
#ifdef _WIN32
#include <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include "compat.h"
#include "printing_fns.h"
#include "misc_fns.h"
#include "pes_fns.h"
#include "tswrite_fns.h"
#include "es_fns.h"
#include "printing_fns.h"
#define DEBUG 0
// A lone forwards reference
static inline int get_more_data(ES_p es);
// ------------------------------------------------------------
// Basic functions
// ------------------------------------------------------------
/*
* Open an ES file and build an elementary stream datastructure to read
* it with.
*
* - `filename` is the ES files name. As a special case, if this is NULL
* then standard input (STDIN_FILENO) will be read from.
*
* Opens the file for read, builds the datastructure, and reads the first 3
* bytes of the input file (this is done to prime the triple-byte search
* mechanism).
*
* Returns 0 if all goes well, 1 otherwise.
*/
extern int open_elementary_stream(char *filename,
ES_p *es)
{
int err;
int input;
if (filename == NULL)
input = STDIN_FILENO;
else
{
input = open_binary_file(filename,FALSE);
if (input == -1) return 1;
}
err = build_elementary_stream_file(input,es);
if (err)
{
fprint_err("### Error building elementary stream for file %s\n", filename);
return 1;
}
return 0;
}
static int setup_readahead(ES_p es)
{
int err;
es->read_ahead_len = 0;
es->read_ahead_posn = 0;
es->data = NULL;
es->data_end = NULL;
es->data_ptr = NULL;
es->last_packet_posn = 0;
es->last_packet_es_data_len = 0;
// Try to get the first chunk of data from the file
err = get_more_data(es);
if (err) return err;
if (es->reading_ES)
{
if (es->read_ahead_len < 3)
{
fprint_err("### File only contains %d byte%s\n",
es->read_ahead_len,(es->read_ahead_len==1?"":"s"));
return 1;
}
}
else
{
if (es->reader->packet->es_data_len < 3)
{
fprint_err("### File PES packet only contains %d byte%s\n",
es->reader->packet->es_data_len,
(es->reader->packet->es_data_len==1?"":"s"));
return 1;
}
}
if (DEBUG)
fprint_msg("File starts %02x %02x %02x\n",es->data[0],es->data[1],es->data[2]);
// Despite (maybe) reporting the above, we haven't actually read anything
// yet
es->prev2_byte = es->prev1_byte = es->cur_byte = 0xFF;
es->posn_of_next_byte.infile = 0;
es->posn_of_next_byte.inpacket = 0;
return 0;
}
/*
* Build an elementary stream datastructure attached to an input file.
* This is intended for reading ES data files.
*
* - `input` is the file stream to read from.
*
* Builds the datastructure, and reads the first 3 bytes of the input
* file (this is done to prime the triple-byte search mechanism).
*
* Returns 0 if all goes well, 1 otherwise.
*/
extern int build_elementary_stream_file(int input,
ES_p *es)
{
ES_p new = malloc(SIZEOF_ES);
if (new == NULL)
{
print_err("### Unable to allocate elementary stream datastructure\n");
return 1;
}
new->reading_ES = TRUE;
new->input = input;
new->reader = NULL;
setup_readahead(new);
*es = new;
return 0;
}
/*
* Build an elementary stream datastructure for use with a PES reader.
* Reads the first (or next) three bytes of the ES.
*
* This reads data from the PES video data, ignoring any audio data.
*
* - `reader` is the PES reader we want to use to read our TS or PS data.
*
* The caller must explicitly close the PES reader as well as closing the
* elementary stream (closing the ES does not affect the PES reader).
*
* Returns 0 if all goes well, 1 otherwise.
*/
extern int build_elementary_stream_PES(PES_reader_p reader,
ES_p *es)
{
ES_p new = malloc(SIZEOF_ES);
if (new == NULL)
{
print_err("### Unable to allocate elementary stream datastructure\n");
return 1;
}
new->reading_ES = FALSE;
new->input = -1;
new->reader = reader;
setup_readahead(new);
*es = new;
return 0;
}
/*
* Tidy up the elementary stream datastructure after we've finished with it.
*
* Specifically:
*
* - free the datastructure
* - set `es` to NULL
*
* No return status is given, since there's not much one can do if anything
* *did* go wrong, and if something went wrong and the program is continuing,
* it's bound to show up pretty soon.
*/
extern void free_elementary_stream(ES_p *es)
{
(*es)->input = -1; // "forget" our input
free(*es);
*es = NULL;
}
/*
* Tidy up the elementary stream datastructure after we've finished with it.
*
* Specifically:
*
* - close the input file (if its stream is set, and if it's not STDIN)
* - call `free_elementary_stream()`
*
* No return status is given, since there's not much one can do if anything
* *did* go wrong, and if something went wrong and the program is continuing,
* it's bound to show up pretty soon.
*/
extern void close_elementary_stream(ES_p *es)
{
int input;
if (*es == NULL)
return;
input = (*es)->input;
if (input != -1 && input != STDIN_FILENO)
(void) close_file(input);
free_elementary_stream(es);
}
/*
* Ask an ES context if changed input is available.
*
* This is a convenience wrapper to save querying the ES context to see
* if it is (a) reading from PES, (b) automatically writing the PES packets
* out via a TS writer, and (c) if said TS writer has a changed command.
*
* Calls `tswrite_command_changed()` on the TS writer associated with this ES.
*
* Returns TRUE if there is a changed command.
*/
extern int es_command_changed(ES_p es)
{
if (es->reading_ES)
return FALSE;
if (es->reader->tswriter == NULL)
return FALSE;
return tswrite_command_changed(es->reader->tswriter);
}
// ------------------------------------------------------------
// Handling elementary stream data units
// ------------------------------------------------------------
/*
* Prepare the contents of a (new) ES unit datastructure.
*
* Allocates a new data array, and unsets the counts.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int setup_ES_unit(ES_unit_p unit)
{
unit->data = malloc(ES_UNIT_DATA_START_SIZE);
if (unit->data == NULL)
{
print_err("### Unable to allocate ES unit data buffer\n");
return 1;
}
unit->data_len = 0;
unit->data_size = ES_UNIT_DATA_START_SIZE;
unit->start_posn.infile = 0;
unit->start_posn.inpacket = 0;
unit->PES_had_PTS = FALSE; // See the header file
return 0;
}
/*
* Tidy up an ES unit datastructure after we've finished with it.
*
* (Frees the internal data array, and unsets the counts)
*/
extern void clear_ES_unit(ES_unit_p unit)
{
if (unit->data != NULL)
{
free(unit->data);
unit->data = NULL;
unit->data_size = 0;
unit->data_len = 0;
}
}
/*
* Build a new ES unit datastructure.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int build_ES_unit(ES_unit_p *unit)
{
int err;
ES_unit_p new = malloc(SIZEOF_ES_UNIT);
if (new == NULL)
{
print_err("### Unable to allocate ES unit datastructure\n");
return 1;
}
err = setup_ES_unit(new);
if (err)
{
free(new);
return 1;
}
*unit = new;
return 0;
}
/*
* Build a new ES unit datastructure, from a given data array.
*
* Takes a copy of 'data'. Sets 'start_code' appropriately,
* sets 'start_posn' to (0,0), and 'PES_had_PTS' to FALSE.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int build_ES_unit_from_data(ES_unit_p *unit,
byte *data,
uint32_t data_len)
{
ES_unit_p new = malloc(SIZEOF_ES_UNIT);
if (new == NULL)
{
print_err("### Unable to allocate ES unit datastructure\n");
return 1;
}
new->data = malloc(data_len);
if (new->data == NULL)
{
print_err("### Unable to allocate ES unit data buffer\n");
return 1;
}
(void) memcpy(new->data, data, data_len);
new->data_len = data_len;
new->data_size = data_len;
new->start_code = data[3];
new->start_posn.infile = 0;
new->start_posn.inpacket = 0;
new->PES_had_PTS = FALSE; // See the header file
*unit = new;
return 0;
}
/*
* Tidy up and free an ES unit datastructure after we've finished with it.
*
* Empties the ES unit datastructure, frees it, and sets `unit` to NULL.
*
* If `unit` is already NULL, does nothing.
*/
extern void free_ES_unit(ES_unit_p *unit)
{
if (*unit == NULL)
return;
clear_ES_unit(*unit);
free(*unit);
*unit = NULL;
}
/*
* Print out some information this ES unit, on normal or error output
*/
extern void report_ES_unit(int is_msg,
ES_unit_p unit)
{
byte s = unit->start_code;
fprint_msg_or_err(is_msg,
OFFSET_T_FORMAT_08 "/%4d: ES unit (%02x '%d%d%d%d %d%d%d%d')",
unit->start_posn.infile,unit->start_posn.inpacket,s,
(s&0x80)>>7,(s&0x40)>>6,(s&0x20)>>5,(s&0x10)>>4,
(s&0x08)>>3,(s&0x04)>>2,(s&0x02)>>1,(s&0x01));
// Show the data bytes - but we don't need to show the first 4,
// since we know they're 00 00 01 <start-code>
if (unit->data_len > 0)
{
int ii;
int data_len = unit->data_len - 4;
int show_len = (data_len>10?10:data_len);
fprint_msg_or_err(is_msg," %6d:",data_len);
for (ii = 0; ii < show_len; ii++)
fprint_msg_or_err(is_msg," %02x",unit->data[4+ii]);
if (show_len < data_len)
fprint_msg_or_err(is_msg,"...");
}
fprint_msg_or_err(is_msg,"\n");
}
// ------------------------------------------------------------
// ES unit *data* stuff
// ------------------------------------------------------------
/*
* A wrapper for `read_next_PES_ES_packet()`, to save us forgetting things
* we need to do when we call it.
*
* Returns 0 if it succeeds, EOF if the end-of-file is read, otherwise
* 1 if some error occurs.
*/
static inline int get_next_pes_packet(ES_p es)
{
int err;
PES_reader_p reader = es->reader;
// Before reading the *next* packet, remember where the last one was
if (reader->packet == NULL)
{
// What can we do if there was no last packet?
es->last_packet_posn = 0;
es->last_packet_es_data_len = 0;
}
else
{
es->last_packet_posn = reader->packet->posn;
es->last_packet_es_data_len = reader->packet->es_data_len;
}
err = read_next_PES_ES_packet(es->reader);
if (err) return err;
// Point to our (new) data buffer
es->data = reader->packet->es_data;
es->data_end = es->data + reader->packet->es_data_len;
es->data_ptr = es->data;
es->posn_of_next_byte.infile = reader->packet->posn;
es->posn_of_next_byte.inpacket = 0;
return 0;
}
/*
* Read some more data into our read-ahead buffer. For a "bare" file,
* reads the next buffer-full in, and for PES based data, reads the
* next PES packet that contains ES data.
*
* 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(ES_p es)
{
if (es->reading_ES)
{
// 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(es->input,&es->read_ahead,ES_READ_AHEAD_SIZE);
#else
ssize_t len = read(es->input,&es->read_ahead,ES_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;
}
es->read_ahead_posn += es->read_ahead_len; // length of the *last* buffer
es->read_ahead_len = len;
es->data = es->read_ahead; // should be done in the setup function
es->data_end = es->data + len; // one beyond the last byte
es->data_ptr = es->data;
return 0;
}
else
{
return get_next_pes_packet(es);
}
}
/*
* Find the start of the next ES unit - i.e., a 00 00 01 start code prefix.
*
* Doesn't move the read position if we're already *at* the start of
* an ES unit.
*
* ((new scheme: Leaves the data_ptr set to read the *next* byte, since
* we know that we've "used up" the 00 00 01 at the start of this unit.))
*
* Returns 0 if it succeeds, EOF if the end-of-file is read, otherwise
* 1 if some error occurs.
*/
static int find_ES_unit_start(ES_p es,
ES_unit_p unit)
{
int err;
byte prev1 = es->prev1_byte;
byte prev2 = es->prev2_byte;
// In almost all cases (hopefully, except for the very start of the file),
// a previous call to find_ES_unit_end will already have positioned us
// "over" the start of the next unit
for (;;)
{
byte *ptr;
for (ptr = es->data_ptr; ptr < es->data_end; ptr++)
{
if (prev2 == 0x00 && prev1 == 0x00 && *ptr == 0x01)
{
es->prev1_byte = es->prev2_byte = 0x00;
es->cur_byte = 0x01;
if (es->reading_ES)
{
unit->start_posn.infile = es->read_ahead_posn + (ptr - es->data) - 2;
}
else
{
unit->start_posn.infile = es->reader->packet->posn;
unit->start_posn.inpacket = (ptr - es->data) - 2;
if (unit->start_posn.inpacket < 0)
{
unit->start_posn.infile = es->last_packet_posn;
unit->start_posn.inpacket += es->last_packet_es_data_len;
}
// Does the PES packet that we are starting in have a PTS?
unit->PES_had_PTS = es->reader->packet->has_PTS;
}
es->data_ptr = ptr + 1; // the *next* byte to read
unit->data[0] = 0x00; // i.e., the values we just read
unit->data[1] = 0x00;
unit->data[2] = 0x01;
unit->data_len = 3;
return 0;
}
prev2 = prev1;
prev1 = *ptr;
}
// We've run out of data - get some more
err = get_more_data(es);
if (err) return err;
}
}
/*
* Find (read to) the end of the current ES unit.
*
* Reads to just before the next 00 00 01 start code prefix.
*
* H.264 rules would also allow us to read to just before a 00 00 00
* sequence, but we shall ignore this ability, because when reading
* ES we want to ensure that there are no bytes "left out" of the ES
* units, so that the sum of the lengths of all the ES units we read will
* be the same as the length of data we have read. This is desirable
* because it makes handling ES via PES much easier (we can, for instance,
* position to the first ES unit of a picture, and then just read in N
* bytes, where N is the sum of the lengths of the ES units making up the
* picture, which is much more efficient than having to read in individual
* ES units, and takes less room to remember than having to remember the
* end position (offset of PES packet in file + offset of end of ES unit in
* PES packet)).
*
* ((new scheme: Leaves the data_ptr set to read the current byte again,
* since we know that, in general, we want to detect it in find_ES_unit_start
* as the 01 following on from a 00 and a 00.))
*
* Returns 0 if it succeeds, otherwise 1 if some error occurs.
*
* Note that finding end-of-file is not counted as an error - it is
* assumed that it is just the natural end of the ES unit.
*/
static int find_ES_unit_end(ES_p es,
ES_unit_p unit)
{
int err;
byte prev1 = es->cur_byte;
byte prev2 = es->prev1_byte;
for (;;)
{
byte *ptr;
for (ptr = es->data_ptr; ptr < es->data_end; ptr++)
{
// Have we reached the end of our unit?
// We know we are if we've found the next 00 00 01 start code prefix.
// (as stated in the header comment above, we're ignoring the H.264
// ability to end if we've found a 00 00 00 sequence)
if (prev2 == 0x00 && prev1 == 0x00 && *ptr == 0x01)
{
es->data_ptr = ptr; // remember where we've got to
es->prev2_byte = 0x00; // we know prev1_byte is already 0
es->cur_byte = 0x01;
// We've read two 00 bytes we don't need into our data buffer...
unit->data_len -= 2;
if (es->reading_ES)
{
es->posn_of_next_byte.infile = es->read_ahead_posn +
(ptr - es->data) - 2;
}
else
{
es->posn_of_next_byte.infile = es->reader->packet->posn;
es->posn_of_next_byte.inpacket = (ptr - es->data) - 2;
}
return 0;
}
// Otherwise, it's a data byte
if (unit->data_len == unit->data_size)
{
int newsize = unit->data_size + ES_UNIT_DATA_INCREMENT;
unit->data = realloc(unit->data,newsize);
if (unit->data == NULL)
{
print_err("### Unable to extend ES unit data array\n");
return 1;
}
unit->data_size = newsize;
}
unit->data[unit->data_len++] = *ptr;
prev2 = prev1;
prev1 = *ptr;
}
// We've run out of data (ptr == es->data_end) - get some more
err = get_more_data(es);
if (err == EOF)
{
// Reaching the end of file is a legitimate way of stopping!
es->data_ptr = ptr; // remember where we've got to
es->prev2_byte = prev2;
es->prev1_byte = prev1;
es->cur_byte = 0xFF; // the notional byte off the end of the file
//es->cur_byte = *ptr;
// Pretend there's a "next byte"
if (es->reading_ES)
{
es->posn_of_next_byte.infile = es->read_ahead_posn + (ptr - es->data);
}
else
{
es->posn_of_next_byte.inpacket = (ptr - es->data);
}
return 0;
}
else if (err)
return err;
if (!es->reading_ES)
{
// If we update this now, it will be correct when we return,
// even if we return because of a later EOF
es->posn_of_next_byte.infile = es->reader->packet->posn;
// Does the PES packet that we have just read in have a PTS?
// If it does, then there's a very good chance (subject to a 00 00 01
// being split between PES packets) that our ES unit has a PTS "around"
// it
if (es->reader->packet->has_PTS)
unit->PES_had_PTS = TRUE;
}
}
}
/*
* Find and read in the next ES unit.
*
* In general, unless there are compelling reasons, use
* `find_and_build_next_ES_unit()` instead.
*
* - `es` is the elementary stream we're reading from.
* - `unit` is the datastructure into which to read the ES unit
* - any previous content will be lost.
*
* Returns 0 if it succeeds, EOF if the end-of-file is read (i.e., there
* is no next ES unit), otherwise 1 if some error occurs.
*/
extern int find_next_ES_unit(ES_p es,
ES_unit_p unit)
{
int err;
err = find_ES_unit_start(es,unit);
if (err) return err; // 1 or EOF
err = find_ES_unit_end(es,unit);
if (err) return err;
// The first byte after the 00 00 01 prefix tells us what sort of thing
// we've found - we'll be friendly and extract it for the user
unit->start_code = unit->data[3];
return 0;
}
/*
* Find and read the next ES unit into a new datastructure.
*
* - `es` is the elementary stream we're reading from.
* - `unit` is the datastructure containing the ES unit found, or NULL
* if there was none.
*
* Returns 0 if it succeeds, EOF if the end-of-file is read (i.e., there
* is no next ES unit), otherwise 1 if some error occurs.
*/
extern int find_and_build_next_ES_unit(ES_p es,
ES_unit_p *unit)
{
int err;
err = build_ES_unit(unit);
if (err) return 1;
err = find_next_ES_unit(es,*unit);
if (err)
{
free_ES_unit(unit);
return err;
}
return 0;
}
/*
* Write (copy) the current ES unit to the output stream.
*
* Note that it writes out all of the data for this ES unit,
* including its 00 00 01 start code prefix.
*
* - `output` is the output stream (file descriptor) to write to
* - `unit` is the ES unit to write
*
* Returns 0 if all went well, 1 if something went wrong.
*/
extern int write_ES_unit(FILE *output,
ES_unit_p unit)
{
size_t written = fwrite(unit->data,1,unit->data_len,output);
if (written != unit->data_len)
{
fprint_err("### Error writing out ES unit data: %s\n"
" Wrote %ld bytes instead of %d\n",
strerror(errno),(long int)written,unit->data_len);
return 1;
}
else
return 0;
}
// ------------------------------------------------------------
// Arbitrary reading from ES data
// ------------------------------------------------------------
/*
* Seek within PES underlying ES.
*
* This should only be used to seek to data that starts with 00 00 01.
*
* "Unsets" the triple byte context.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
static int seek_in_PES(ES_p es,
ES_offset where)
{
int err;
if (es->reader == NULL)
{
print_err("### Attempt to seek in PES for an ES reader that"
" is not attached to a PES reader\n");
return 1;
}
// Force the reader to forget its current packet
if (es->reader->packet != NULL)
free_PES_packet_data(&es->reader->packet);
// Seek to the right packet in the PES data
err = set_PES_reader_position(es->reader,where.infile);
if (err)
{
fprint_err("### Error seeking for PES packet at " OFFSET_T_FORMAT
"\n",where.infile);
return 1;
}
// Read the PES packet containing ES (ignoring packets we don't care about)
err = get_next_pes_packet(es);
if (err)
{
fprint_err("### Error reading PES packet at " OFFSET_T_FORMAT "/%d\n",
where.infile,where.inpacket);
return 1;
}
// Now sort out the byte offset
if (where.inpacket > es->reader->packet->es_data_len)
{
fprint_err("### Error seeking PES packet at " OFFSET_T_FORMAT "/%d: "
" packet ES data is only %d bytes long\n",where.infile,
where.inpacket,es->reader->packet->es_data_len);
return 1;
}
es->posn_of_next_byte = where;
return 0;
}
/*
* Update our current position information after a seek or direct read.
*/
static inline void deduce_correct_position(ES_p es)
{
// We don't know what the previous three bytes were, but we (strongly)
// assume that they were not 00 00 01
es->cur_byte = 0xff;
es->prev1_byte = 0xff;
es->prev2_byte = 0xff;
if (es->reading_ES)
{
// For ES data, we want to force new data to be read in from the file
es->data_ptr = es->data_end = NULL;
es->read_ahead_len = 0; // to stop the read ahead posn being incremented
es->read_ahead_posn = es->posn_of_next_byte.infile;
}
else
{
// For PES data, we have whatever is left in the current packet
PES_packet_data_p packet = es->reader->packet;
es->data = packet->es_data;
es->data_ptr = packet->es_data + es->posn_of_next_byte.inpacket;
es->data_end = packet->es_data + packet->es_data_len;
// And, of course, we have no idea about the *previous* packet in the file
es->last_packet_posn = es->last_packet_es_data_len = 0;
}
}
/*
* "Seek" to the given position in the ES data, which is assumed to
* be an offset ready to read a 00 00 01 sequence.
*
* If the ES reader is using PES to read its data, then both fields
* of `where` are significant, but if the underlying file *is* just a file,
* only `where.infile` is used.
*
* Returns 0 if all went well, 1 is something went wrong
*/
extern int seek_ES(ES_p es,
ES_offset where)
{
int err;
if (es->reading_ES)
{
err = seek_file(es->input,where.infile);
if (err)
{
print_err("### Error seeking within ES file\n");
return 1;
}
}
else
{
err = seek_in_PES(es,where);
if (err)
{
fprint_err("### Error seeking within ES over PES (offset " OFFSET_T_FORMAT
"/%d)\n",where.infile,where.inpacket);
return 1;
}
}
// And make it look as if we reached this position sensibly
es->posn_of_next_byte = where;
deduce_correct_position(es);
return 0;
}
/*
* Retrieve ES bytes from PES as requested
*
* Leaves the PES reader set to read on after this data.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
static int read_bytes_from_PES(ES_p es,
byte *data,
uint32_t num_bytes)
{
int err;
int offset = 0;
int num_bytes_wanted = num_bytes;
int32_t from = es->posn_of_next_byte.inpacket;
int32_t num_bytes_left = es->reader->packet->es_data_len - from;
for (;;)
{
if (num_bytes_left < num_bytes_wanted)
{
memcpy(&(data[offset]),&(es->reader->packet->es_data[from]),
num_bytes_left);
offset += num_bytes_left;
num_bytes_wanted -= num_bytes_left;
err = get_next_pes_packet(es);
if (err) return err;
from = 0;
num_bytes_left = es->reader->packet->es_data_len;
}
else
{
memcpy(&(data[offset]),&(es->reader->packet->es_data[from]),
num_bytes_wanted);
from += num_bytes_wanted;
break;
}
}
es->posn_of_next_byte.inpacket = from;
//es->posn_of_next_byte.infile = es->reader->packet->posn;
return 0;
}
/*
* Read in some ES data from disk.
*
* Suitable for use when reading in a set of ES units whose bounds
* (start offset and total number of bytes) have been remembered.
*
* "Seeks" to the given position in the ES data, which is assumed to
* be an offset ready to read a 00 00 01 sequence, and reads data thereafter.
*
* After this function, the triple byte context is set to FF FF FF, and the
* position of said bytes are undefined, but the next position to read a byte
* from *is* defined.
*
* The intent is to allow the caller to have a data array (`data`) that
* always contains the last data read, and is of the required size, and
* need only be freed when no more data is needed.
*
* - `es` is where to read our data from
* - `start_posn` is the file offset to start reading at
* - `num_bytes` is how many bytes we want to read
* - `data_len` may be NULL or a pointer to a value.
* If it is NULL, then the data array will be reallocated to size
* `num_bytes` regardless. If it is non-NULL, it should be passed *in*
* as the size that `data` *was*, and will be returned as the size
* that `data` is when the function returns.
* - `data` is the data array to read into. If this is NULL, or if `num_bytes`
* is NULL, or if `num_bytes` is greater than `data_len`, then it will be
* reallocated to size `num_bytes`.
*
* Returns 0 if all went well, 1 if something went wrong.
*/
extern int read_ES_data(ES_p es,
ES_offset start_posn,
uint32_t num_bytes,
uint32_t *data_len,
byte **data)
{
int err;
if (*data == NULL || data_len == NULL || num_bytes > *data_len)
{
*data = realloc(*data,num_bytes);
if (*data == NULL)
{
print_err("### Unable to reallocate data space\n");
return 1;
}
if (data_len != NULL)
*data_len = num_bytes;
}
err = seek_ES(es,start_posn);
if (err) return err;
if (es->reading_ES)
{
err = read_bytes(es->input,num_bytes,*data);
if (err)
{
if (err == EOF)
{
fprint_err("### Error (EOF) reading %d bytes\n",num_bytes);
return 1;
}
else
return err;
}
es->posn_of_next_byte.infile = start_posn.infile + num_bytes;