forked from nexgenta/tstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessunit.c
1436 lines (1341 loc) · 46.3 KB
/
accessunit.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 working with access units in 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 "compat.h"
#include "printing_fns.h"
#include "es_fns.h"
#include "ts_fns.h"
#include "nalunit_fns.h"
#include "accessunit_fns.h"
#include "reverse_fns.h"
#define DEBUG 0
/*
* Build a new access unit datastructure.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static inline int build_access_unit(access_unit_p *acc_unit,
uint32_t index)
{
int err;
access_unit_p new = malloc(SIZEOF_ACCESS_UNIT);
if (new == NULL)
{
print_err("### Unable to allocate access unit datastructure\n");
return 1;
}
err = build_nal_unit_list(&(new->nal_units));
if (err)
{
free(new);
*acc_unit = NULL;
return err;
}
new->index = index;
new->started_primary_picture = FALSE;
new->primary_start = NULL;
new->ignored_broken_NAL_units = 0;
new->frame_num = new->field_pic_flag = new->bottom_field_flag = 0;
*acc_unit = new;
return 0;
}
/*
* Tidy up an access unit datastructure after we've finished with it.
*
* If `deep` is TRUE, also frees all of the NAL units in the NAL unit
* list (which is normally what we want to do).
*/
static inline void clear_access_unit(access_unit_p acc_unit,
int deep)
{
free_nal_unit_list(&(acc_unit->nal_units),deep);
acc_unit->primary_start = NULL;
}
/*
* Tidy up and free an access unit datastructure after we've finished with it.
*
* Clears the datastructure, frees it, and returns `acc_unit` as NULL.
*
* Does nothing if `acc_unit` is already NULL.
*/
extern void free_access_unit(access_unit_p *acc_unit)
{
if (*acc_unit == NULL)
return;
clear_access_unit(*acc_unit,TRUE);
free(*acc_unit);
*acc_unit = NULL;
}
/*
* Report on this access unit
*/
extern void report_access_unit(access_unit_p access_unit)
{
int ii;
fprint_msg("Access unit %u",access_unit->index);
if (access_unit->started_primary_picture)
fprint_msg(" (%s)",access_unit->primary_start->start_reason);
print_msg(":\n");
if (access_unit->field_pic_flag)
fprint_msg(" %s field of frame %u\n",
(access_unit->bottom_field_flag==1?"Bottom":"Top"),
access_unit->frame_num);
else
fprint_msg(" Frame %u\n",access_unit->frame_num);
if (access_unit->ignored_broken_NAL_units)
fprint_msg(" Ignored %d broken NAL unit%s\n",
access_unit->ignored_broken_NAL_units,
(access_unit->ignored_broken_NAL_units==1?"":"s"));
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal = access_unit->nal_units->array[ii];
if (nal == NULL)
print_msg(" <null>\n");
else
{
fprint_msg(" %c",((access_unit->primary_start == nal)?'*':' '));
report_nal(TRUE,nal);
}
}
}
/*
* How many slices (VCL NAL units) are there in this access unit?
*/
static inline int num_slices(access_unit_p access_unit)
{
int count = 0;
int ii;
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
if (nal_is_slice(access_unit->nal_units->array[ii]))
count ++;
}
return count;
}
/*
* Retrieve the bounds of this access unit in the file it was read from.
*
* - `access_unit` is the access unit we're interested in
* - `start` is its start position (i.e., the location at which to start
* reading to retrieve all of the data for the access unit, including
* the 00 00 01 prefix at the start of the first NAL unit therein)
* - `length` is the total length of the NAL units within this access unit
*
* Returns 0 if all goes well, 1 if the access unit has no content.
*/
extern int get_access_unit_bounds(access_unit_p access_unit,
ES_offset *start,
uint32_t *length)
{
int ii;
if (access_unit->primary_start == NULL)
{
print_err("### Cannot determine bounds of an access unit with no content\n");
return 1;
}
*start = access_unit->nal_units->array[0]->unit.start_posn;
*length = 0;
// Maybe we should precalculate, or even cache, the total length...
for (ii=0; ii<access_unit->nal_units->length; ii++)
(*length) += access_unit->nal_units->array[ii]->unit.data_len;
return 0;
}
/*
* Are all slices in this access unit I slices?
*/
extern int all_slices_I(access_unit_p access_unit)
{
int ii;
if (access_unit->primary_start == NULL)
return FALSE;
if (!nal_is_slice(access_unit->primary_start))
return FALSE;
// All I
if (access_unit->primary_start->u.slice.slice_type == ALL_SLICES_I)
return TRUE;
// Only one slice, and it's I
if (num_slices(access_unit) == 1 &&
access_unit->primary_start->u.slice.slice_type == SLICE_I)
return TRUE;
// Are any of the slices not I?
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal_unit = access_unit->nal_units->array[ii];
if (nal_is_slice(nal_unit) && nal_unit->u.slice.slice_type != SLICE_I)
return FALSE;
}
return TRUE;
}
/*
* Are all slices in this access unit P slices?
*/
extern int all_slices_P(access_unit_p access_unit)
{
int ii;
if (access_unit->primary_start == NULL)
return FALSE;
if (!nal_is_slice(access_unit->primary_start))
return FALSE;
// All P
if (access_unit->primary_start->u.slice.slice_type == ALL_SLICES_P)
return TRUE;
// Only one slice, and it's P
if (num_slices(access_unit) == 1 &&
access_unit->primary_start->u.slice.slice_type == SLICE_P)
return TRUE;
// Are any of the slices not P?
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal_unit = access_unit->nal_units->array[ii];
if (nal_is_slice(nal_unit) && nal_unit->u.slice.slice_type != SLICE_P)
return FALSE;
}
return TRUE;
}
/*
* Are all slices in this access unit I or P slices?
*/
extern int all_slices_I_or_P(access_unit_p access_unit)
{
int ii;
if (access_unit->primary_start == NULL)
return FALSE;
if (!nal_is_slice(access_unit->primary_start))
return FALSE;
// All P or all I
if (access_unit->primary_start->u.slice.slice_type == SLICE_I ||
access_unit->primary_start->u.slice.slice_type == SLICE_P)
return TRUE;
// Only one slice, and it's P or I
if (num_slices(access_unit) == 1 &&
(access_unit->primary_start->u.slice.slice_type == ALL_SLICES_I ||
access_unit->primary_start->u.slice.slice_type == ALL_SLICES_P))
return TRUE;
// Are any of the slices not either P or I?
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal_unit = access_unit->nal_units->array[ii];
if (nal_is_slice(nal_unit) &&
(nal_unit->u.slice.slice_type != SLICE_I &&
nal_unit->u.slice.slice_type != SLICE_P))
return FALSE;
}
return TRUE;
}
/*
* Are all slices in this access unit B slices?
*/
extern int all_slices_B(access_unit_p access_unit)
{
int ii;
if (access_unit->primary_start == NULL)
return FALSE;
if (!nal_is_slice(access_unit->primary_start))
return FALSE;
// All B
if (access_unit->primary_start->u.slice.slice_type == ALL_SLICES_B)
return TRUE;
// Only one slice, and it's B
if (num_slices(access_unit) == 1 &&
access_unit->primary_start->u.slice.slice_type == SLICE_B)
return TRUE;
// Are any of the slices not B?
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal_unit = access_unit->nal_units->array[ii];
if (nal_is_slice(nal_unit) && nal_unit->u.slice.slice_type != SLICE_B)
return FALSE;
}
return TRUE;
}
/*
* Append a NAL unit to the list of NAL units for this access unit
*
* NB: `pending` may be NULL
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int access_unit_append(access_unit_p access_unit,
nal_unit_p nal,
int starts_primary,
nal_unit_list_p pending)
{
int err;
if (starts_primary && access_unit->started_primary_picture)
{
// Our caller should have started a new access unit instead
fprint_err("### Already had a start of primary picture in access"
" unit %d\n",access_unit->index);
return 1;
}
if (starts_primary)
{
access_unit->primary_start = nal;
access_unit->started_primary_picture = TRUE;
access_unit->frame_num = nal->u.slice.frame_num;
access_unit->field_pic_flag = nal->u.slice.field_pic_flag;
access_unit->bottom_field_flag = nal->u.slice.bottom_field_flag;
}
if (pending != NULL && pending->length > 0)
{
int ii;
for (ii=0; ii<pending->length; ii++)
{
err = append_to_nal_unit_list(access_unit->nal_units,
pending->array[ii]);
if (err)
{
fprint_err("### Error extending access unit %d\n",
access_unit->index);
return err;
}
}
}
if (nal != NULL)
{
err = append_to_nal_unit_list(access_unit->nal_units,nal);
if (err)
{
fprint_err("### Error extending access unit %d\n",
access_unit->index);
return err;
}
}
return 0;
}
/*
* Merge the NAL units of the second access unit into the first, and then
* free the second access unit.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int merge_access_unit_nals(access_unit_p access_unit1,
access_unit_p *access_unit2)
{
int err, ii;
for (ii = 0; ii < (*access_unit2)->nal_units->length; ii++)
{
err = append_to_nal_unit_list(access_unit1->nal_units,
(*access_unit2)->nal_units->array[ii]);
if (err)
{
print_err("### Error merging two access units\n");
return err;
}
}
// Don't forget that we're now "sharing" any ignored NAL units
access_unit1->ignored_broken_NAL_units +=
(*access_unit2)->ignored_broken_NAL_units;
// Take care not to free the individual NAL units in our second access
// unit, as they are still being used by the first
clear_access_unit(*access_unit2,FALSE);
free(*access_unit2);
*access_unit2 = NULL;
// Fake the flags in our remaining access unit to make us "look" like
// a frame
access_unit1->field_pic_flag = 0;
return 0;
}
/*
* Write out an access unit as ES.
*
* Also writes out any end of sequence or end of stream NAL unit found in the
* `context` (since they are assumed to have immediately followed this access
* unit).
*
* - `access_unit` is the access unit to write out
* - `context` may contain additional things to write (see above), but may
* legitimately be NULL if there is no context.
* - `output` is the ES file to write to
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int write_access_unit_as_ES(access_unit_p access_unit,
access_unit_context_p context,
FILE *output)
{
int ii, err;
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
err = write_ES_unit(output,&(access_unit->nal_units->array[ii]->unit));
if (err)
{
print_err("### Error writing NAL unit ");
report_nal(FALSE,access_unit->nal_units->array[ii]);
return err;
}
}
if (context != NULL && context->end_of_sequence)
{
err = write_ES_unit(output,&(context->end_of_sequence->unit));
if (err)
{
print_err("### Error writing end of sequence NAL unit ");
report_nal(FALSE,context->end_of_sequence);
return err;
}
free_nal_unit(&context->end_of_sequence);
}
if (context != NULL && context->end_of_stream)
{
err = write_ES_unit(output,&(context->end_of_stream->unit));
if (err)
{
print_err("### Error writing end of stream NAL unit ");
report_nal(FALSE,context->end_of_sequence);
return err;
}
free_nal_unit(&context->end_of_stream);
}
return 0;
}
/*
* Write out the (potential) trailing components of an access unit as TS.
*
* I.e., writes out any end of sequence or end of stream NAL unit found in the
* `context` (since they are assumed to have immediately followed this access
* unit).
*
* - `context` may contain additional things to write (see above), but may
* legitimately be NULL if there is no context.
* - `tswriter` is the TS context to write with
* - `video_pid` is the PID to use to write the data
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int write_access_unit_trailer_as_TS(access_unit_context_p context,
TS_writer_p tswriter,
uint32_t video_pid)
{
int err;
if (context != NULL && context->end_of_sequence)
{
nal_unit_p nal = context->end_of_sequence;
err = write_ES_as_TS_PES_packet(tswriter,nal->unit.data,nal->unit.data_len,
video_pid,DEFAULT_VIDEO_STREAM_ID);
if (err)
{
print_err("### Error writing end of sequence NAL unit ");
report_nal(FALSE,nal);
return err;
}
free_nal_unit(&context->end_of_sequence);
}
if (context != NULL && context->end_of_stream)
{
nal_unit_p nal = context->end_of_stream;
err = write_ES_as_TS_PES_packet(tswriter,nal->unit.data,nal->unit.data_len,
video_pid,DEFAULT_VIDEO_STREAM_ID);
if (err)
{
print_err("### Error writing end of stream NAL unit ");
report_nal(FALSE,nal);
return err;
}
free_nal_unit(&context->end_of_stream);
}
return 0;
}
/*
* Write out an access unit as TS.
*
* Also writes out any end of sequence or end of stream NAL unit found in the
* `context` (since they are assumed to have immediately followed this access
* unit).
*
* - `access_unit` is the access unit to write out
* - `context` may contain additional things to write (see above), but may
* legitimately be NULL if there is no context.
* - `tswriter` is the TS context to write with
* - `video_pid` is the PID to use to write the data
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int write_access_unit_as_TS(access_unit_p access_unit,
access_unit_context_p context,
TS_writer_p tswriter,
uint32_t video_pid)
{
int ii, err;
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal = access_unit->nal_units->array[ii];
err = write_ES_as_TS_PES_packet(tswriter,
nal->unit.data,nal->unit.data_len,
video_pid,DEFAULT_VIDEO_STREAM_ID);
if (err)
{
print_err("### Error writing NAL unit ");
report_nal(FALSE,nal);
return err;
}
}
return write_access_unit_trailer_as_TS(context,tswriter,video_pid);
}
/*
* Write out an access unit as TS, with PTS timing in the first PES packet
* (and PCR timing in the first TS of the frame).
*
* Also writes out any end of sequence or end of stream NAL unit found in the
* `context` (since they are assumed to have immediately followed this access
* unit).
*
* - `access_unit` is the access unit to write out
* - `context` may contain additional things to write (see above), but may
* legitimately be NULL if there is no context.
* - `tswriter` is the TS context to write with
* - `video_pid` is the PID to use to write the data
* - `got_pts` is TRUE if we have a PTS value, in which case
* - `pts` is said PTS value
* - `got_dts` is TRUE if we also have DTS, in which case
* - `dts` is said DTS value.
*
* If we are given a DTS (which must, by definition, always go up) we will also
* use it as the value for PCR.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int write_access_unit_as_TS_with_pts_dts(access_unit_p access_unit,
access_unit_context_p context,
TS_writer_p tswriter,
uint32_t video_pid,
int got_pts,
uint64_t pts,
int got_dts,
uint64_t dts)
{
int ii, err;
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal = access_unit->nal_units->array[ii];
// Only write the first PES packet out with PTS
if (ii == 0)
err = write_ES_as_TS_PES_packet_with_pts_dts(tswriter,
nal->unit.data,
nal->unit.data_len,
video_pid,
DEFAULT_VIDEO_STREAM_ID,
got_pts,pts,
got_dts,dts);
else
err = write_ES_as_TS_PES_packet(tswriter,
nal->unit.data,nal->unit.data_len,
video_pid,DEFAULT_VIDEO_STREAM_ID);
if (err)
{
print_err("### Error writing NAL unit ");
report_nal(FALSE,nal);
return err;
}
}
return write_access_unit_trailer_as_TS(context,tswriter,video_pid);
}
/*
* Write out an access unit as TS, with PCR timing in the first TS of the
* frame.
*
* Also writes out any end of sequence or end of stream NAL unit found in the
* `context` (since they are assumed to have immediately followed this access
* unit).
*
* - `access_unit` is the access unit to write out
* - `context` may contain additional things to write (see above), but may
* legitimately be NULL if there is no context.
* - `tswriter` is the TS context to write with
* - `video_pid` is the PID to use to write the data
* - `pcr_base` and `pcr_extn` encode the PCR value.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int write_access_unit_as_TS_with_PCR(access_unit_p access_unit,
access_unit_context_p context,
TS_writer_p tswriter,
uint32_t video_pid,
uint64_t pcr_base,
uint32_t pcr_extn)
{
int ii, err;
for (ii=0; ii<access_unit->nal_units->length; ii++)
{
nal_unit_p nal = access_unit->nal_units->array[ii];
// Only write the first PES packet out with PCR
if (ii == 0)
err = write_ES_as_TS_PES_packet_with_pcr(tswriter,
nal->unit.data,
nal->unit.data_len,
video_pid,
DEFAULT_VIDEO_STREAM_ID,
pcr_base,pcr_extn);
else
err = write_ES_as_TS_PES_packet(tswriter,
nal->unit.data,nal->unit.data_len,
video_pid,DEFAULT_VIDEO_STREAM_ID);
if (err)
{
print_err("### Error writing NAL unit ");
report_nal(FALSE,nal);
return err;
}
}
return write_access_unit_trailer_as_TS(context,tswriter,video_pid);
}
/*
* End this access unit.
*
* - `access_unit` is the access unit to end.
* - if `show_details` is true, then a summary of its contents is printed
* out.
*
* Actually, with the current code scheme, this only does much if
* `show_details` is true. However, it may still be a useful hook
* for actual work later on.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static inline int end_access_unit(access_unit_context_p context,
access_unit_p access_unit,
int show_details)
{
if (show_details)
{
report_access_unit(access_unit);
if (context->pending_nal)
{
print_msg("... pending: ");
report_nal(TRUE,context->pending_nal);
}
if (context->end_of_sequence)
{
print_msg("--> EndOfSequence ");
report_nal(TRUE,context->end_of_sequence);
}
if (context->end_of_stream)
{
print_msg("--> EndOfStream ");
report_nal(TRUE,context->end_of_stream);
}
}
return 0;
}
/*
* Build a new access unit context datastructure.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int build_access_unit_context(ES_p es,
access_unit_context_p *context)
{
int err;
access_unit_context_p new = malloc(SIZEOF_ACCESS_UNIT_CONTEXT);
if (new == NULL)
{
print_err("### Unable to allocate access unit context datastructure\n");
return 1;
}
new->pending_nal = NULL;
new->end_of_stream = NULL;
new->end_of_sequence = NULL;
new->access_unit_index = 0;
new->reverse_data = NULL;
new->no_more_data = FALSE;
new->earlier_primary_start = NULL;
err = build_nal_unit_context(es,&new->nac);
if (err)
{
print_err("### Error building access unit context datastructure\n");
free(new);
return err;
}
err = build_nal_unit_list(&new->pending_list);
if (err)
{
print_err("### Error building access unit context datastructure\n");
free_nal_unit_context(&new->nac);
free(new);
return err;
}
*context = new;
return 0;
}
/*
* Free a new access unit context datastructure.
*
* Clears the datastructure, frees it, and returns `context` as NULL.
*
* Does not free any `reverse_data` datastructure.
*
* Does nothing if `context` is already NULL.
*/
extern void free_access_unit_context(access_unit_context_p *context)
{
access_unit_context_p cc = *context;
if (cc == NULL)
return;
// We assume no-one else has an interest in the NAL units in
// our "pending" list.
free_nal_unit_list(&cc->pending_list,TRUE);
// And similarly, we should be the only "person" holding on to these
free_nal_unit(&cc->earlier_primary_start); // although this is bluff
free_nal_unit(&cc->end_of_sequence);
free_nal_unit(&cc->end_of_stream);
free_nal_unit(&cc->pending_nal);
free_nal_unit_context(&cc->nac);
cc->reverse_data = NULL;
free(*context);
*context = NULL;
return;
}
/*
* Reset an acccess unit context, so it "forgets" its current information
* about what it is reading, etc.
*/
extern void reset_access_unit_context(access_unit_context_p context)
{
free_nal_unit(&context->earlier_primary_start);
free_nal_unit(&context->end_of_sequence);
free_nal_unit(&context->end_of_stream);
free_nal_unit(&context->pending_nal);
reset_nal_unit_list(context->pending_list,FALSE); // @@@ leak???
context->no_more_data = FALSE;
// We have to hope that the "previous" sequence parameter and picture
// parameter dictionaries are still applicable, since we don't still
// have a record of the ones that would have been in effect at this
// point.
}
/*
* Rewind a file being read as access units.
*
* This is a wrapper for `rewind_nal_unit_context` that also knows to
* unset things appropriate to the access unit context.
*
* If a reverse context is attached to this access unit, it also will
* be "rewound" appropriately.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
extern int rewind_access_unit_context(access_unit_context_p context)
{
// First, forget where we are
reset_access_unit_context(context);
context->access_unit_index = 0; // no access units read from this file yet
// Next, take care of rewinding
if (context->reverse_data)
{
context->reverse_data->last_posn_added = -1; // next entry to be 0
}
// And then, do the relocation itself
return rewind_nal_unit_context(context->nac);
}
/*
* Remember the required information from the previous access unit's
* first VLC NAL unit (i.e., the one that starts its primary picture).
*
* If we just remembered the (address of the) NAL unit itself, we would
* have a problem if/when the access unit containing it was freed, since
* that would also free the NAL unit. Luckily, the information we want
* to remember is well defined, and does not require us to do anything
* other than copy data, so we can reuse the same "internal" NAL unit
* without needing to do lots of mallocing around.
*
* It *should* be obvious, given its intended use, but do not call this
* on a NAL unit that has not been decoded - things may fall apart
* messily later on...
*
* (NB: the "pseudo" NAL unit we use to remember the information is
* a true NAL unit except for not having any of the data/rbsp arrays
* filled in, so it *does* cause the NAL unit id to be incremented,
* which has confused me at least once when reading diagnostic output.)
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int remember_earlier_primary_start(access_unit_context_p context,
nal_unit_p nal)
{
nal_unit_p tgt = context->earlier_primary_start;
if (tgt == NULL)
{
int err = build_nal_unit(&tgt);
if (err)
{
print_err("### Error building NAL unit for 'earlier primary start'\n");
free(tgt);
return err;
}
context->earlier_primary_start = tgt;
}
tgt->starts_picture_decided = nal->starts_picture_decided;
tgt->starts_picture = nal->starts_picture;
tgt->start_reason = nal->start_reason;
tgt->decoded = nal->decoded;
tgt->nal_ref_idc = nal->nal_ref_idc;
tgt->nal_unit_type = nal->nal_unit_type;
tgt->u = nal->u;
// Lastly, we may not need the following, but they are sufficient to
// allow us to read the whole NAL unit back in if we should need to.
tgt->unit.start_posn = nal->unit.start_posn;
tgt->unit.data_len = nal->unit.data_len;
return 0;
}
/*
* Maybe remember an access unit for reversing - either an IDR or one with all
* frames I
*/
static int maybe_remember_access_unit(reverse_data_p reverse_data,
access_unit_p access_unit,
int verbose)
{
// Keep it if it is an IDR, or all of its contents are I slices
if (access_unit->primary_start != NULL &&
access_unit->primary_start->nal_ref_idc != 0 &&
(access_unit->primary_start->nal_unit_type == NAL_IDR ||
all_slices_I(access_unit)))
{
ES_offset start_posn = {0,0};
uint32_t num_bytes = 0;
int err = get_access_unit_bounds(access_unit,&start_posn,&num_bytes);
if (err)
{
fprint_err("### Error working out position/size of access unit %d"
" for reversing\n",access_unit->index);
return 1;
}
err = remember_reverse_h264_data(reverse_data,access_unit->index,
start_posn,num_bytes);
if (err)
{
fprint_err("### Error remembering access unit %d for reversing\n",
access_unit->index);
return 1;
}
if (verbose) fprint_msg("REMEMBER IDR %5d at " OFFSET_T_FORMAT_08
"/%04d for %5d\n",access_unit->index,
start_posn.infile,start_posn.inpacket,num_bytes);
}
return 0;
}
/*
* Retrieve the next access unit from the given elementary stream.
*
* - `context` is the context information needed to allow us to find
* successive access units.
* - `quiet` is true if we should try to be silent about it
* - `show_details` is true if we should output more info than normal
* - `ret_access_unit` is the next access unit.
*
* If the access unit was ended because an end of sequence or end of
* stream NAL unit was encountered, then said end of sequence/stream
* NAL unit will be remembered in the `context`.
*
* Note that it is possible to get back an *empty* access unit in
* certain situations - the most obvious of which is if we get two
* ``end of sequence`` NAL units with nothing betwen them.
*
* Because of this possibility, some care should be taken to allow for
* access units that do not contain a primary picture (no VCL NAL unit),
* and contain zero NAL units. Also, if one is trying for an accurate
* count of access units, such instances should probably be ignored.
*
* Returns 0 if it succeeds, EOF if there is no more data to read, or 1 if
* some error occurs.
*
* EOF can be returned because the end of file has been reached, or because an
* end of stream NAL unit has been encountered. The two may be distinguished
* by looking at `context->end_of_stream`, which will be NULL if it was a true
* EOF.
*
* Note that `ret_access_unit` will be NULL if EOF is returned.
*/
extern int get_next_access_unit(access_unit_context_p context,
int quiet,
int show_details,
access_unit_p *ret_access_unit)
{
int err;
nal_unit_p nal = NULL;
access_unit_p access_unit;
// Is there anything more to read from the input stream?
if (context->no_more_data)
{
*ret_access_unit = NULL;
return EOF;
}
// Since we're expecting to return a new access unit,
// we'd better build it...
err = build_access_unit(&access_unit,context->access_unit_index+1);
if (err) return err;
// Did we have any left over stuff to put at its start?
if (context->pending_nal != NULL)
{
err = access_unit_append(access_unit,
context->pending_nal,TRUE,context->pending_list);
if (err) goto give_up;
context->pending_nal = NULL;
reset_nal_unit_list(context->pending_list,FALSE);
}
for (;;)
{
err = find_next_NAL_unit(context->nac,FALSE,&nal);
if (err == EOF)
{
context->no_more_data = TRUE; // prevent future reads on this stream
break;
}
else if (err == 2)
{
// The NAL unit was broken. Should we:
// a) ignore it and pretend it never happened (i.e., ``continue``)
// b) ignore it and give up on the current access unit (i.e., unset
// our current status, and hunt for the start of the next access
// unit).
// Clearly, option (a) is the easiest to try, so let's see how that
// works for now...
print_err("!!! Ignoring broken NAL unit\n");
access_unit->ignored_broken_NAL_units ++;
continue;
}
else if (err)
{
print_err("### Error retrieving next NAL\n");
goto give_up;