-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.c
1596 lines (1483 loc) · 54.6 KB
/
reverse.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
/*
* Support for reversing
*
* ***** 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 *****
*/
/* The reverse-data arrays are populated as a side-effect of processing the
* file forwards. The "build a picture" routines in h262.c and accessunit.c
* call the "remember" functions herein for appropriate pictures.
*
* Reversing is then handled by going backwards through those arrays, selecting
* appropriate "rememebered" values. For H.264 pictures, and for H.262 sequence
* headers, it is sufficient to read the appropriate "chunk" of memory back
* in (from the ES data stream), and just output that without any further work.
*
* For H.262 pictures, life is a little more complex, as some of the frames
* contain AFD user data, which overrides the aspect ratio in the preceding
* sequence header. If one thus wants to precede each frame *with* a sequence
* header (which is a Good Idea), then one also needs to ensure it has an
* appropriate AFD entry (whether it did in the original data or not).
*
* Thus for H.262 pictures, one must re-read the picture data from the input
* file (starting at the specified offset), and "invent" an AFD entry within
* its item list, if necessary.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h> // for ctime/time
#include "compat.h"
#include "misc_defns.h"
#include "printing_fns.h"
#include "es_fns.h"
#include "h262_fns.h"
#include "nalunit_fns.h"
#include "accessunit_fns.h"
#include "ts_fns.h"
#include "tswrite_fns.h"
#include "reverse_fns.h"
#define DEBUG 0
// ------------------------------------------------------------
// A useful macro to tell us if the `idx` entry in the reverse_data
// structure `rev` is a sequence header or not (or did you guess?)
#define SEQUENCE_HEADER_ENTRY(rev,idx) (!(rev)->is_h264 && \
(rev)->seq_offset[idx] == 0)
// ============================================================
// Remembering start/length information for reversing video sequences
// ============================================================
/*
* Build the internal arrays to remember video sequence bounds in,
* for reversing.
*
* Builds a new `reverse_data` datastructure. If `is_h264` is FALSE (i.e., the
* data to be reversed is not MPEG-1 or MPEG-2), then this datastructure may
* be smaller.
*
* To collect reversing data, attach this datastructure to an H.262 or access
* unit context (with add_h262/access_unit_reverse_context), and then use
* get_next_h262_frame() or get_next_h264_frame() to read through the data
* stream - appropriate pictures/access units will be remembered
* automatically.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int build_reverse_data(reverse_data_p *reverse_data,
int is_h264)
{
int newsize = REVERSE_ARRAY_START_SIZE;
reverse_data_p new = malloc(SIZEOF_REVERSE_DATA);
if (new == NULL)
{
print_err("### Unable to allocate reverse data datastructure\n");
return 1;
}
new->start_file = malloc(newsize*sizeof(offset_t));
if (new->start_file == NULL)
{
print_err("### Unable to allocate reverse data array (start_file)\n");
free(new);
return 1;
}
new->start_pkt = malloc(newsize*sizeof(int32_t));
if (new->start_pkt == NULL)
{
print_err("### Unable to allocate reverse data array (start_pkt)\n");
free(new->start_file);
free(new);
return 1;
}
new->index = malloc(newsize*sizeof(uint32_t));
if (new->index == NULL)
{
print_err("### Unable to allocate reverse data array (index)\n");
free(new->start_file);
free(new->start_pkt);
free(new);
return 1;
}
new->data_len = malloc(newsize*sizeof(int32_t));
if (new->data_len == NULL)
{
print_err("### Unable to allocate reverse data array (data_len)\n");
free(new->start_file);
free(new->start_pkt);
free(new->index);
free(new);
return 1;
}
if (is_h264)
{
new->seq_offset = NULL;
new->afd_byte = NULL;
}
else
{
new->seq_offset = malloc(newsize);
if (new->seq_offset == NULL)
{
print_err("### Unable to allocate reverse data array (seq offset)\n");
free(new->start_file);
free(new->start_pkt);
free(new->index);
free(new->data_len);
free(new);
return 1;
}
new->afd_byte = malloc(newsize);
if (new->afd_byte == NULL)
{
print_err("### Unable to allocate reverse data array (AFD)\n");
free(new->seq_offset);
free(new->start_file);
free(new->start_pkt);
free(new->index);
free(new->data_len);
free(new);
return 1;
}
}
new->size = newsize;
new->length = 0;
new->num_pictures = 0;
new->is_h264 = is_h264;
new->pictures_written = 0;
new->pictures_kept = 0;
new->first_written = 0;
new->last_written = 0;
new->last_posn_added = 0; // although undefined if `length` == 0
new->output_sequence_headers = !is_h264;
new->pid = DEFAULT_VIDEO_PID;
new->stream_id = DEFAULT_VIDEO_STREAM_ID;
*reverse_data = new;
return 0;
}
/*
* Set the video PID and stream id for TS output.
*
* This need only be called if reverse data *is* being output as TS,
* and if the standard default values (DEFAULT_VIDEO_PID and
* DEFAULT_VIDEO_STREAM_ID) are not correct.
*/
extern void set_reverse_pid(reverse_data_p reverse_data,
uint32_t pid,
byte stream_id)
{
reverse_data->pid = pid;
reverse_data->stream_id = stream_id;
}
/*
* Add a reversing context to an H.262 context (and vice versa).
*
* Does not check if there is one present already.
*
* Returns 0 if all is well, 1 if something goes wrong.
*/
extern int add_h262_reverse_context(h262_context_p h262,
reverse_data_p reverse_data)
{
if (reverse_data->is_h264)
{
print_err("### Cannot add an H.262 context to an H.264 reverse data"
" context\n");
return 1;
}
h262->reverse_data = reverse_data;
reverse_data->h262 = h262;
return 0;
}
/*
* Add a reversing context to an access unit context (and vice versa).
*
* Does not check if there is one present already.
*
* Returns 0 if all is well, 1 if something goes wrong.
*/
extern int add_access_unit_reverse_context(access_unit_context_p context,
reverse_data_p reverse_data)
{
if (!reverse_data->is_h264)
{
print_err("### Cannot add an H.264 access unit context to an"
" H.262 reverse data context\n");
return 1;
}
context->reverse_data = reverse_data;
reverse_data->h264 = context;
return 0;
}
/*
* Free the datastructure we used to remember reversing data
*
* Sets `reverse_data` to NULL.
*/
extern void free_reverse_data(reverse_data_p *reverse_data)
{
reverse_data_p this = *reverse_data;
if (this == NULL)
return;
if (this->seq_offset != NULL)
{
free(this->seq_offset);
this->seq_offset = NULL;
}
free(this->index);
free(this->start_file);
free(this->start_pkt);
free(this->data_len);
this->index = NULL;
this->start_file = NULL;
this->start_pkt = NULL;
this->data_len = NULL;
this->length = this->size = 0;
free(this);
*reverse_data = NULL;
}
/*
* Compare an offset and two position components. `offset2` is composed
* of file_posn2 and pkt_posn2.
*
* Returns -1 if offset1 < offset2, 0 if they are the same, and 1 if
* offset1 > offset2.
*/
static inline int cmp_offsets(ES_offset offset1,
offset_t file_posn2,
int32_t pkt_posn2)
{
if (offset1.infile < file_posn2)
return -1;
else if (offset1.infile > file_posn2)
return 1;
else if (offset1.inpacket < pkt_posn2)
return -1;
else if (offset1.inpacket > pkt_posn2)
return 1;
else
return 0;
}
static void debug_reverse_data_problem(reverse_data_p reverse_data,
uint32_t index,
ES_offset start_posn,
uint32_t idx)
{
FILE *tempfile;
char *tempfilename = "tsserve_reverse_problem.txt";
int ii;
tempfile = fopen(tempfilename,"a+");
if (tempfile == NULL)
{
fprint_err("### Unable to open file %s - writing diagnostics"
" to stderr instead\n",tempfilename);
tempfile = stderr;
}
else
{
time_t now;
fprint_err("### Appending diagnostics to file %s\n",tempfilename);
now = time(NULL);
fprintf(tempfile,"** %s:\n",ctime(&now));
}
fprintf(tempfile,"Trying to add reverse data [%d] " OFFSET_T_FORMAT
"/%d at index %d (again),\nbut previous entry was [%d] "
OFFSET_T_FORMAT "/%d\n",
index,start_posn.infile,start_posn.inpacket,idx,
reverse_data->index[idx],reverse_data->start_file[idx],
reverse_data->start_pkt[idx]);
fprintf(tempfile,"Last posn added %d, length %d, index %d\n",
reverse_data->last_posn_added,reverse_data->length,index);
for (ii=0; ii<reverse_data->length; ii++)
if (reverse_data->is_h264 || reverse_data->seq_offset[ii])
fprintf(tempfile," %3d: %4d at " OFFSET_T_FORMAT "/%d for %d\n",
ii,reverse_data->index[ii],
reverse_data->start_file[ii],
reverse_data->start_pkt[ii],
reverse_data->data_len[ii]);
else
fprintf(tempfile," %3d: seqh at " OFFSET_T_FORMAT "/%d for %d\n",
ii,
reverse_data->start_file[ii],
reverse_data->start_pkt[ii],
reverse_data->data_len[ii]);
if (tempfile != stderr)
{
fprintf(tempfile,"\n\n");
fclose(tempfile);
}
}
/*
* Remember video sequence bounds for H.262 data
*
* - `reverse_data` is the datastructure we want to add our entry to
* - `index` indicates which picture (counted from the start of the file)
* this one is (i.e., we're assuming that not all pictures will be stored).
* If the entry is an H.262 sequence header, then this is ignored.
* - `start_posn` is the location of the start of the entry in the file,
* The entry will be ignored if `start_posn` comes before the last
* existing entry in the arrays.
* - `length` is the number of bytes in the entry
* - `seq_offset` should be 0 for a sequence header, and is otherwise the
* offset backwards to the previous nearest sequence header (i.e., 1 if
* the sequence header is the previous entry).
* - `afd` is the effective AFD byte for this picture
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int remember_reverse_h262_data(reverse_data_p reverse_data,
uint32_t index,
ES_offset start_posn,
uint32_t length,
byte seq_offset,
byte afd)
{
if (reverse_data->length > 0 &&
(reverse_data->last_posn_added + 1) < (uint32_t)reverse_data->length)
{
// We're repeating an entry we previously added - check it hasn't
// changed (since the only obvious way for this to have happened
// is if we've rewound and are then moving forwards again, it should
// not be possible for the data to have changed at a particular index)
int idx = reverse_data->last_posn_added + 1;
int cmp = cmp_offsets(start_posn,
reverse_data->start_file[idx],
reverse_data->start_pkt[idx]);
if (cmp == 0)
{
#if DEBUG
fprint_msg("++ Added [%d] " OFFSET_T_FORMAT "/%d again\n",
index,start_posn.infile,start_posn.inpacket);
#endif
reverse_data->last_posn_added ++;
return 0;
}
else
{
fprint_err("### Trying to add reverse data [%d] " OFFSET_T_FORMAT
"/%d at index %d (again),\n but previous entry was [%d] "
OFFSET_T_FORMAT "/%d\n",
index,start_posn.infile,start_posn.inpacket,idx,
reverse_data->index[idx],reverse_data->start_file[idx],
reverse_data->start_pkt[idx]);
debug_reverse_data_problem(reverse_data,index,start_posn,idx);
return 1;
}
}
if (reverse_data->size == reverse_data->length)
{
int newsize = reverse_data->size + REVERSE_ARRAY_INCREMENT_SIZE;
reverse_data->index = realloc(reverse_data->index,
newsize*sizeof(uint32_t));
if (reverse_data->index == NULL)
{
print_err("### Unable to extend reverse data array (index)\n");
return 1;
}
reverse_data->start_file = realloc(reverse_data->start_file,
newsize*sizeof(offset_t));
if (reverse_data->start_file == NULL)
{
print_err("### Unable to extend reverse data array (start_file)\n");
return 1;
}
reverse_data->start_pkt = realloc(reverse_data->start_pkt,
newsize*sizeof(int32_t));
if (reverse_data->start_pkt == NULL)
{
print_err("### Unable to extend reverse data array (start_pkt)\n");
return 1;
}
reverse_data->data_len = realloc(reverse_data->data_len,
newsize*sizeof(int32_t));
if (reverse_data->data_len == NULL)
{
print_err("### Unable to extend reverse data array (length)\n");
return 1;
}
if (!reverse_data->is_h264)
{
reverse_data->seq_offset = realloc(reverse_data->seq_offset,newsize);
if (reverse_data->seq_offset == NULL)
{
print_err("### Unable to extend reverse data array (seq offset)\n");
return 1;
}
reverse_data->afd_byte = realloc(reverse_data->afd_byte,newsize);
if (reverse_data->afd_byte == NULL)
{
print_err("### Unable to extend reverse data array (AFD)\n");
return 1;
}
}
reverse_data->size = newsize;
}
// If we're not an H.262 sequence header, remember our index
if (seq_offset != 0)
{
reverse_data->num_pictures ++;
reverse_data->index[reverse_data->length] = index;
reverse_data->seq_offset[reverse_data->length] = seq_offset;
reverse_data->afd_byte[reverse_data->length] = afd;
}
else
{
reverse_data->index[reverse_data->length] = 0;
reverse_data->seq_offset[reverse_data->length] = 0;
reverse_data->afd_byte[reverse_data->length] = 0;
}
reverse_data->start_file[reverse_data->length] = start_posn.infile;
reverse_data->start_pkt[reverse_data->length] = start_posn.inpacket;
reverse_data->data_len[reverse_data->length] = length;
reverse_data->last_posn_added = reverse_data->length;
reverse_data->length ++;
return 0;
}
/*
* Remember video sequence bounds for H.264 data
*
* - `reverse_data` is the datastructure we want to add our entry to
* - `index` indicates which picture (counted from the start of the file)
* this one is (i.e., we're assuming that not all pictures will be stored).
* If the entry is an H.262 sequence header, then this is ignored.
* - `start_posn` is the location of the start of the entry in the file,
* The entry will be ignored if `start_posn` comes before the last
* existing entry in the arrays.
* - `length` is the number of bytes in the entry
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int remember_reverse_h264_data(reverse_data_p reverse_data,
uint32_t index,
ES_offset start_posn,
uint32_t length)
{
if (reverse_data->length > 0 &&
(reverse_data->last_posn_added + 1) < (uint32_t)reverse_data->length)
{
// We're repeating an entry we previously added - check it hasn't
// changed (since the only obvious way for this to have happened
// is if we've rewound and are then moving forwards again, it should
// not be possible for the data to have changed at a particular index)
int idx = reverse_data->last_posn_added + 1;
int cmp = cmp_offsets(start_posn,
reverse_data->start_file[idx],
reverse_data->start_pkt[idx]);
if (cmp == 0)
{
#if DEBUG
fprint_msg("++ Added [%d] " OFFSET_T_FORMAT "/%d again\n",
index,start_posn.infile,start_posn.inpacket);
#endif
reverse_data->last_posn_added ++;
return 0;
}
else
{
fprint_err("### Trying to add reverse data [%d] " OFFSET_T_FORMAT
"/%d at index %d (again),\n but previous entry was [%d] "
OFFSET_T_FORMAT "/%d\n",
index,start_posn.infile,start_posn.inpacket,idx,
reverse_data->index[idx],reverse_data->start_file[idx],
reverse_data->start_pkt[idx]);
debug_reverse_data_problem(reverse_data,index,start_posn,idx);
return 1;
}
}
if (reverse_data->size == reverse_data->length)
{
int newsize = reverse_data->size + REVERSE_ARRAY_INCREMENT_SIZE;
reverse_data->index = realloc(reverse_data->index,
newsize*sizeof(uint32_t));
if (reverse_data->index == NULL)
{
print_err("### Unable to extend reverse data array (index)\n");
return 1;
}
reverse_data->start_file = realloc(reverse_data->start_file,
newsize*sizeof(offset_t));
if (reverse_data->start_file == NULL)
{
print_err("### Unable to extend reverse data array (start_file)\n");
return 1;
}
reverse_data->start_pkt = realloc(reverse_data->start_pkt,
newsize*sizeof(int32_t));
if (reverse_data->start_pkt == NULL)
{
print_err("### Unable to extend reverse data array (start_pkt)\n");
return 1;
}
reverse_data->data_len = realloc(reverse_data->data_len,
newsize*sizeof(int32_t));
if (reverse_data->data_len == NULL)
{
print_err("### Unable to extend reverse data array (length)\n");
return 1;
}
reverse_data->size = newsize;
}
reverse_data->num_pictures ++;
reverse_data->index[reverse_data->length] = index;
reverse_data->start_file[reverse_data->length] = start_posn.infile;
reverse_data->start_pkt[reverse_data->length] = start_posn.inpacket;
reverse_data->data_len[reverse_data->length] = length;
reverse_data->last_posn_added = reverse_data->length;
reverse_data->length ++;
return 0;
}
/*
* Retrieve video sequence bounds for entry `which`
*
* - `reverse_data` is the datastructure we want to get our entry from
* - `which` indicates which entry we'd like to retrieve. The first
* entry in the `reverse_data` is number 0.
* - `index` indicates which picture (counted from the start of the file)
* this one is (i.e., we're assuming that not all pictures will be stored).
* `index` may be passed as NULL if the value is of no interest - i.e.,
* typically when the entry is for an H.262 sequence header.
* The first picture in the file has index 1.
* - `start_posn` is the location of the start of the entry in the file,
* - `length` is the number of bytes in the entry
* - for H.262 data, if the entry is a picture, then `seq_offset` will
* be the offset backwards to the previous nearest sequence header
* (i.e., 1 if the sequence header is the previous entry), and if it is
* a sequence header, `seq_offset` will be 0. For H.264 data, the value
* will always be 0. `seq_offset` may be passed as NULL if the value is
* of no interest.
* - for H.262 data, if the entry is a picture, then `afd` will be its
* (effective) AFD byte. Otherwise it will be 0. `afd` may be passed as NULL
* if the value if of no interest.
*
* To clarify, all of the following are legitimate calls::
*
* err = get_reverse_data(reverse_data,10,&index,&start,&length,&offset,&afd);
* err = get_reverse_data(reverse_data,10,&index,&start,&length,NULL,NULL);
* err = get_reverse_data(reverse_data,10,NULL,&start,&length,NULL,NULL);
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int get_reverse_data(reverse_data_p reverse_data,
int which,
uint32_t *index,
ES_offset *start_posn,
uint32_t *length,
byte *seq_offset,
byte *afd)
{
if (which >= reverse_data->length || which < 0)
{
fprint_err("Requested reverse data index (%d) is out of range 0-%d\n",
which,reverse_data->length-1);
return 1;
}
if (index != NULL)
*index = reverse_data->index[which];
start_posn->infile = reverse_data->start_file[which];
start_posn->inpacket = reverse_data->start_pkt[which];
*length = reverse_data->data_len[which];
if (seq_offset != NULL)
{
if (reverse_data->is_h264)
*seq_offset = 0;
else
*seq_offset = reverse_data->seq_offset[which];
}
if (afd != NULL)
{
if (reverse_data->is_h264)
*afd = 0;
else
*afd = reverse_data->afd_byte[which];
}
return 0;
}
// ============================================================
// Collecting pictures
// ============================================================
/*
* Locate and remember sequence headers and I pictures, for later reversal.
*
* - `h262` is the H.262 stream reading context
* - if `max` is non-zero, then collecting will stop after `max` pictures
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*
* Returns 0 if all went well, EOF if the end of file is reached,
* and 1 if an error occurred.
*
* If command input is enabled, then it can also return COMMAND_RETURN_CODE
* if the current command has changed.
*/
extern int collect_reverse_h262(h262_context_p h262,
int max,
int verbose,
int quiet)
{
int err = 0;
// In order to stop after `max` items, we need to count pictures
int picture_count = 0;
if (h262->reverse_data == NULL)
{
print_err("### Unable to collect reverse data for H.262 pictures\n");
print_err(" H.262 context does not have reverse data"
" information attached to it\n");
return 1;
}
for (;;)
{
h262_picture_p picture = NULL;
if (es_command_changed(h262->es))
return COMMAND_RETURN_CODE;
err = get_next_h262_frame(h262,verbose,quiet,&picture);
if (err == EOF)
return EOF;
else if (err)
return 1;
if (picture->is_picture)
picture_count ++;
free_h262_picture(&picture);
if (max > 0 && picture_count >= max)
break;
}
return 0;
}
/*
* Find IDR and I slices, and remember their access units for later output
* in reverse order.
*
* - `acontext` is the access unit reading context
* - if `max` is non-zero, then collecting will stop after `max` access units
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*
* Returns 0 if all went well, EOF if the end of file is reached,
* and 1 if an error occurred.
*
* If command input is enabled, then it can also return COMMAND_RETURN_CODE
* if the current command has changed.
*/
extern int collect_reverse_access_units(access_unit_context_p acontext,
int max,
int verbose,
int quiet)
{
int err = 0;
int access_unit_count = 0;
if (acontext->reverse_data == NULL)
{
print_err("### Unable to collect reverse data for access units\n");
print_err(" Access unit context does not have reverse data"
" information attached to it\n");
return 1;
}
for (;;)
{
access_unit_p access_unit;
if (es_command_changed(acontext->nac->es))
return COMMAND_RETURN_CODE;
if (verbose)
print_msg("\n");
err = get_next_h264_frame(acontext,quiet,verbose,&access_unit);
if (err == EOF)
return EOF;
else if (err)
return 1;
access_unit_count ++;
free_access_unit(&access_unit);
if (!verbose && !quiet && (access_unit_count % 5000 == 0))
fprint_msg("Scanned %d NAL units in %d frames,"
" remembered %d frames\n",
acontext->nac->count,access_unit_count,
acontext->reverse_data->length);
// Did the logical stream end after the last access unit?
if (acontext->end_of_stream)
{
if (!quiet) print_msg("Found End-of-stream NAL unit\n");
break;
}
if (max > 0 && access_unit_count >= max)
{
if (verbose)
fprint_msg("\nStopping because %d frames have been read\n",
access_unit_count);
break;
}
}
return 0;
}
/*
* Write out packet data as ES or TS
*
* ``extern`` (but unadvertised in a header file) so that it can be used
* internally in esreverse.c
*
* Note that the last two arguments (`pid` and `stream_id`) are only
* used if the data `is_TS`.
*/
extern int write_packet_data(WRITER output,
int as_TS,
byte data[],
int data_len,
uint32_t pid,
byte stream_id)
{
int err;
if (as_TS)
{
// If we're writing TS data, then wrap the whole thing up as PES and
// write it out as TS packets.
// Unfortunately, it's not *quite* that simple, as a PES packet has
// a length specified either as a 16 bit quantity, or as 0 (allowed
// for video data). And it is known that some pictures are longer
// than 65535 bytes.
err = write_ES_as_TS_PES_packet(output.ts_output,data,data_len,
pid,stream_id);
if (err)
{
print_err("### Error writing data as TS PES packet\n");
return 1;
}
}
else
{
// Otherwise, just write it out as is
size_t written = fwrite(data,1,data_len,output.es_output);
if (written != data_len)
{
fprint_err("### Error writing out data: %s\n"
" Wrote %d bytes instead of %d\n",
strerror(errno),(int)written,data_len);
return 1;
}
}
return 0;
}
/*
* Write out H.262 picture data as ES or TS
*/
static int write_picture_data(WRITER output,
int as_TS,
h262_picture_p picture,
uint32_t pid)
{
int err;
if (as_TS)
{
err = write_h262_picture_as_TS(output.ts_output,picture,pid);
if (err)
{
print_err("### Error writing data as TS PES packet\n");
return 1;
}
}
else
{
err = write_h262_picture_as_ES(output.es_output,picture);
if (err)
{
print_err("### Error writing data as ES\n");
return 1;
}
}
return 0;
}
/*
* Read an H.262 picture from the given location
*
* Note that this only does the bare minimum necessary for our purposes
* here - it is *not* an adequate basis for a general "seek in H.262" mechanism
* (which is why it is here, rather than in h262.c).
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
static int read_h262_picture(h262_context_p context,
ES_offset where,
byte afd,
int verbose,
h262_picture_p *picture)
{
int err;
reverse_data_p reverse_data = NULL;
// Ensure that the H.262 context doesn't think it has any ES items in hand
// so that it will start building a picture from scratch
if (context->last_item)
free_h262_item(&context->last_item);
err = seek_ES(context->es,where);
if (err)
{
print_err("### Error seeking for H.262 picture to reverse\n");
return 1;
}
// Hmm - we don't want this call to "remember" the picture for us,
// since we've already done so. Thus we'll have to pretend that we
// don't have a reverse data context whilst we're making the call...
reverse_data = context->reverse_data;
context->reverse_data = NULL;
// But we *do* want to insist that the picture contain an AFD
context->add_fake_afd = TRUE;
context->last_afd = afd; // the value to use if the picture doesn't have one
err = get_next_h262_frame(context,verbose,TRUE,picture);
context->reverse_data = reverse_data;
if (err)
{
print_err("### Error reading H.262 picture when reversing\n");
return 1;
}
return 0;
}
/*
* Output an H.262 sequence header.
*
* - `es` is the input elementary stream
* - `output` is the stream to write to
* - if `as_TS` is true, then output as TS packets, not ES
* - if `verbose` is true, then extra information will be output
* - `seq_index` is the index of the sequence header in the `reverse_data`
* - `reverse_data` contains the list of pictures/access units to reverse.
*/
static int output_sequence_header(ES_p es,
WRITER output,
int as_TS,
int verbose,
uint16_t seq_index,
reverse_data_p reverse_data)
{
int err;
ES_offset seq_posn;
uint32_t seq_len;
byte *seq_data = NULL;
err = get_reverse_data(reverse_data,seq_index,NULL,&seq_posn,&seq_len,
NULL,NULL);
if (err)
{
fprint_err("### Error retrieving sequence header location at %d\n",
seq_index);
return 1;
}
if (verbose)
fprint_msg("Writing sequence header %2d from " OFFSET_T_FORMAT_08
"/%04d for %5d\n",
seq_index,seq_posn.infile,seq_posn.inpacket,seq_len);
err = read_ES_data(es,seq_posn,seq_len,NULL,&seq_data);
if (err)
{
fprint_err("### Error reading (sequence header) data"
" from " OFFSET_T_FORMAT "/%d for %d\n",
seq_posn.infile,seq_posn.inpacket,seq_len);
return 1;
}
err = write_packet_data(output,as_TS,seq_data,seq_len,reverse_data->pid,
reverse_data->stream_id);
free(seq_data);
if (err)
{
print_err("### Error writing (sequence header) data as"
" TS PES packet\n");
return 1;
}
return 0;
}
/*
* Output the last picture (or an earlier one) from the reverse arrays.
*
* This is expected to be used after the whole of the data stream has been
* played, so that the last picture in the reverse arrays is the last I or
* IDR picture in the data stream.
*
* - `es` is the input elementary stream
* - `output` is the stream to write to
* - if `as_TS` is true, then output as TS packets, not ES
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
* - `offset` is the offset from the end of the array of the picture
* to output - so 0 means the last picture, 1 the picture before that,
* and so on. Sequence headers do not count for this purpose.
* - `reverse_data` is the reverse data context.
*
* Returns 0 if all went well, or 1 if something went wrong.
*/
static int output_from_reverse_data(ES_p es,
WRITER output,
int as_TS,
int verbose,
int quiet,
uint32_t offset,
reverse_data_p reverse_data)
{
int with_sequence_headers = (!reverse_data->is_h264 &&
reverse_data->output_sequence_headers);
uint32_t which = reverse_data->length - 1; // the maximum picture index
int is_h262 = !reverse_data->is_h264;