forked from kynesim/tstools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
esreport.c
1261 lines (1133 loc) · 36.1 KB
/
esreport.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
/*
* Report on the contents of an H.264 (MPEG-4/AVC) or H.262 (MPEG-2)
* elementary stream.
*
* ***** 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 <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include "compat.h"
#include "es_fns.h"
#include "nalunit_fns.h"
#include "ts_fns.h"
#include "pes_fns.h"
#include "accessunit_fns.h"
#include "h262_fns.h"
#include "avs_fns.h"
#include "misc_fns.h"
#include "printing_fns.h"
#include "version.h"
#define FRAMES_PER_SECOND 25
#define FRAMES_PER_MINUTE (FRAMES_PER_SECOND * 60)
/*
* Report on the content of an AVS file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` AVS items
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
* - if `count_sizes` is true, then a summary of frame sizes will be kept
*/
static void report_avs_frames(ES_p es,
int max,
int verbose,
int quiet,
int count_sizes)
{
int err;
int count = 0;
int num_frames = 0;
int num_sequence_headers = 0;
int num_sequence_ends = 0;
uint32_t min_frame_size = 1000000;
uint32_t max_frame_size = 0;
uint32_t sum_frame_size = 0;
// I, P, B = 0, 1, 2 (we "make up" picture coding type 0 for I frames)
uint32_t min_x_frame_size[3] = {1000000,1000000,1000000};
uint32_t max_x_frame_size[3] = {0,0,0};
uint32_t sum_x_frame_size[3] = {0,0,0};
int num_x_frames[3] = {0,0,0};
uint32_t min_seq_hdr_size = 1000000;
uint32_t max_seq_hdr_size = 0;
uint32_t sum_seq_hdr_size = 0;
ES_offset start;
uint32_t length;
avs_context_p avs;
err = build_avs_context(es,&avs);
if (err)
{
print_err("### Error trying to build AVS reader from ES reader\n");
return;
}
for (;;)
{
avs_frame_p frame;
err = get_next_avs_frame(avs,verbose,quiet,&frame);
if (err == EOF)
break;
else if (err)
{
print_err("### Error getting next AVS frame\n");
break;
}
count++;
if (!quiet)
report_avs_frame(frame,FALSE);
else if (verbose)
report_avs_frame(frame,TRUE);
if (frame->is_frame)
{
if (count_sizes)
{
err = get_ES_unit_list_bounds(frame->list,&start,&length);
if (err) break;
if (min_frame_size > length) min_frame_size = length;
if (max_frame_size < length) max_frame_size = length;
sum_frame_size += length;
if (frame->picture_coding_type < 3) // paranoia - check in array bounds
{
// I, P or B -- even though there isn't a "real" picture coding type
// for I, we forge one when we read the frame
int ii = frame->picture_coding_type;
num_x_frames[ii] ++;
if (min_x_frame_size[ii] > length) min_x_frame_size[ii] = length;
if (max_x_frame_size[ii] < length) max_x_frame_size[ii] = length;
sum_x_frame_size[ii] += length;
}
}
num_frames ++;
if (frame->picture_coding_type < 3) // paranoia - check in array bounds
num_x_frames[frame->picture_coding_type] ++;
}
else if (frame->is_sequence_header)
{
if (count_sizes)
{
err = get_ES_unit_list_bounds(frame->list,&start,&length);
if (err) break;
if (min_seq_hdr_size > length) min_seq_hdr_size = length;
if (max_seq_hdr_size < length) max_seq_hdr_size = length;
sum_seq_hdr_size += length;
}
num_sequence_headers ++;
}
else
num_sequence_ends ++;
free_avs_frame(&frame);
if (max > 0 && count >= max)
break;
}
free_avs_context(&avs);
fprint_msg("Found %d AVS 'frame'%s:\n"
" %5d frame%s (%d I, %d P, %d B)\n"
" %5d sequence header%s\n"
" %5d sequence end%s\n",
count,(count==1?"":"s"),
num_frames,(num_frames==1?"":"s"),
num_x_frames[AVS_I_PICTURE_CODING],
num_x_frames[AVS_P_PICTURE_CODING],
num_x_frames[AVS_B_PICTURE_CODING],
num_sequence_headers,(num_sequence_headers==1?"":"s"),
num_sequence_ends,(num_sequence_ends==1?"":"s"));
{
double total_seconds = num_frames / (double)FRAMES_PER_SECOND;
int minutes = (int)(total_seconds / 60);
double seconds = total_seconds - 60*minutes;
fprint_msg("At 25 frames/second, that is %dm %.1fs (%.2fs)\n",minutes,seconds,
total_seconds);
}
if (count_sizes)
{
int ii;
if (num_frames > 0)
fprint_msg("Frame sizes ranged from %5u to %7u bytes, mean %9.2f\n",
min_frame_size,max_frame_size,
sum_frame_size/(double)num_frames);
for (ii = 0; ii < 3; ii++)
{
if (num_x_frames[ii] > 0)
fprint_msg(" %s frames from %5u to %7u bytes, mean %9.2f\n",
(ii==0?"I":
ii==1?"P":
ii==2?"B":"?"),
min_x_frame_size[ii],max_x_frame_size[ii],
sum_x_frame_size[ii]/(double)num_x_frames[ii]);
}
if (num_sequence_headers > 0)
{
if (min_seq_hdr_size == max_seq_hdr_size)
fprint_msg("Sequence headers were all %u bytes\n",min_seq_hdr_size);
else
fprint_msg("Sequence headers from %5u to %7u bytes, mean %9.2f\n",
min_seq_hdr_size,max_seq_hdr_size,
sum_seq_hdr_size/(double)num_sequence_headers);
}
}
}
/*
* Report on the ES units in a file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` MPEG items
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*/
static void report_ES_units(ES_p es,
int max,
int verbose,
int quiet)
{
int err;
int count = 0;
struct ES_unit unit;
(void) setup_ES_unit(&unit);
for (;;)
{
err = find_next_ES_unit(es,&unit);
if (err == EOF)
break;
else if (err)
{
print_err("### Error finding next ES unit\n");
break;
}
count++;
if (!quiet)
report_ES_unit(TRUE,&unit);
if (verbose)
print_data(TRUE," Data",
unit.data,unit.data_len,10);
if (max > 0 && count >= max)
break;
}
clear_ES_unit(&unit);
fprint_msg("Found %d ES unit%s\n",count,(count==1?"":"s"));
}
/*
* Report on the content of an MPEG2 file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` MPEG items
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*/
static void find_h262_fields(ES_p es,
int max,
int verbose)
{
int err;
int count = 0;
int num_fields = 0;
int num_frames = 0;
int num_sequence_headers = 0;
int num_sequence_ends = 0;
h262_context_p h262;
err = build_h262_context(es,&h262);
if (err)
{
print_err("### Error trying to build H.262 reader from ES reader\n");
return;
}
for (;;)
{
h262_picture_p picture;
err = get_next_h262_single_picture(h262,verbose,&picture);
if (err == EOF)
break;
else if (err)
{
print_err("### Error getting next H.262 picture\n");
break;
}
count++;
if (picture->is_picture)
{
if (picture->picture_structure < 3)
{
report_h262_picture(picture,verbose);
num_fields ++;
}
else
num_frames ++;
}
else if (picture->is_sequence_header)
num_sequence_headers ++;
else
num_sequence_ends ++;
free_h262_picture(&picture);
if (max > 0 && count >= max)
break;
}
free_h262_context(&h262);
fprint_msg("Found %d MPEG-2 'picture'%s:\n"
" %5d field%s\n"
" %5d frame%s\n"
" %5d sequence header%s\n"
" %5d sequence end%s\n",
count,(count==1?"":"s"),
num_fields,(num_fields==1?"":"s"),
num_frames,(num_frames==1?"":"s"),
num_sequence_headers,(num_sequence_headers==1?"":"s"),
num_sequence_ends,(num_sequence_ends==1?"":"s"));
}
/*
* Report on the content of an MPEG2 file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` MPEG items
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
* - if `count_sizes` is true, then a summary of frame sizes will be kept
*/
static void report_h262_frames(ES_p es,
int max,
int verbose,
int quiet,
int count_sizes)
{
int err;
int count = 0;
int num_frames = 0;
int num_sequence_headers = 0;
int num_sequence_ends = 0;
uint32_t min_frame_size = 1000000;
uint32_t max_frame_size = 0;
uint32_t sum_frame_size = 0;
// I=1, P=2, B=3, D=4 -- so subtract one before using the picture coding type
// as an index into the arrays...
uint32_t min_x_frame_size[4] = {1000000,1000000,1000000,1000000};
uint32_t max_x_frame_size[4] = {0,0,0,0};
uint32_t sum_x_frame_size[4] = {0,0,0,0};
int num_x_frames[4] = {0,0,0,0};
uint32_t min_seq_hdr_size = 1000000;
uint32_t max_seq_hdr_size = 0;
uint32_t sum_seq_hdr_size = 0;
ES_offset start;
uint32_t length;
h262_context_p h262;
err = build_h262_context(es,&h262);
if (err)
{
print_err("### Error trying to build H.262 reader from ES reader\n");
return;
}
for (;;)
{
h262_picture_p picture;
err = get_next_h262_frame(h262,verbose,quiet,&picture);
if (err == EOF)
break;
else if (err)
{
print_err("### Error getting next H.262 picture\n");
break;
}
count++;
if (!quiet)
report_h262_picture(picture,FALSE);
else if (verbose)
report_h262_picture(picture,TRUE);
if (picture->is_picture)
{
if (count_sizes)
{
err = get_ES_unit_list_bounds(picture->list,&start,&length);
if (err) break;
if (min_frame_size > length) min_frame_size = length;
if (max_frame_size < length) max_frame_size = length;
sum_frame_size += length;
if (picture->picture_coding_type < 5 &&
picture->picture_coding_type > 0) // paranoia - check for array bounds
{
// I, P, B or D frame
int ii = picture->picture_coding_type - 1;
if (min_x_frame_size[ii] > length) min_x_frame_size[ii] = length;
if (max_x_frame_size[ii] < length) max_x_frame_size[ii] = length;
sum_x_frame_size[ii] += length;
}
}
num_frames ++;
if (picture->picture_coding_type < 5 &&
picture->picture_coding_type > 0) // paranoia - check for array bounds
num_x_frames[picture->picture_coding_type - 1] ++;
}
else if (picture->is_sequence_header)
{
if (count_sizes)
{
err = get_ES_unit_list_bounds(picture->list,&start,&length);
if (err) break;
if (min_seq_hdr_size > length) min_seq_hdr_size = length;
if (max_seq_hdr_size < length) max_seq_hdr_size = length;
sum_seq_hdr_size += length;
}
num_sequence_headers ++;
}
else
num_sequence_ends ++;
free_h262_picture(&picture);
if (max > 0 && count >= max)
break;
}
free_h262_context(&h262);
fprint_msg("Found %d MPEG-2 'picture'%s:\n"
" %5d frame%s (%d I, %d P, %d B, %d D)\n"
" %5d sequence header%s\n"
" %5d sequence end%s\n",
count,(count==1?"":"s"),
num_frames,(num_frames==1?"":"s"),
num_x_frames[0],
num_x_frames[1],
num_x_frames[2],
num_x_frames[3],
num_sequence_headers,(num_sequence_headers==1?"":"s"),
num_sequence_ends,(num_sequence_ends==1?"":"s"));
{
double total_seconds = num_frames / (double)FRAMES_PER_SECOND;
int minutes = (int)(total_seconds / 60);
double seconds = total_seconds - 60*minutes;
fprint_msg("At 25 frames/second, that is %dm %.1fs (%.2fs)\n",minutes,seconds,
total_seconds);
}
if (count_sizes)
{
int ii;
if (num_frames > 0)
fprint_msg("Frame sizes ranged from %5u to %7u bytes, mean %9.2f\n",
min_frame_size,max_frame_size,
sum_frame_size/(double)num_frames);
for (ii = 0; ii < 4; ii++)
{
if (num_x_frames[ii] > 0)
fprint_msg(" %s frames from %5u to %7u bytes, mean %9.2f\n",
H262_PICTURE_CODING_STR(ii),
min_x_frame_size[ii],max_x_frame_size[ii],
sum_x_frame_size[ii]/(double)num_x_frames[ii]);
}
if (num_sequence_headers > 0)
{
if (min_seq_hdr_size == max_seq_hdr_size)
fprint_msg("Sequence headers were all %u bytes\n",min_seq_hdr_size);
else
fprint_msg("Sequence headers from %5u to %7u bytes, mean %9.2f\n",
min_seq_hdr_size,max_seq_hdr_size,
sum_seq_hdr_size/(double)num_sequence_headers);
}
}
}
/*
* Report on changes in AFD in an MPEG2 file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` MPEG items
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*/
static void report_h262_afds(ES_p es,
int max,
int verbose,
int quiet)
{
int err;
int frames = 0;
byte afd = 0; // not '1000', so we see the first value
h262_context_p h262;
int report_every = 5 * FRAMES_PER_MINUTE;
err = build_h262_context(es,&h262);
if (err)
{
print_err("### Error trying to build H.262 reader from ES reader\n");
return;
}
for (;;)
{
h262_picture_p picture;
err = get_next_h262_frame(h262,verbose,quiet,&picture);
if (err == EOF)
break;
else if (err)
{
print_err("### Error getting next H.262 picture\n");
break;
}
if (picture->is_picture)
{
// NB: the time at which the frame *starts*
if (frames % report_every == 0)
fprint_msg("%d minute%s\n",frames/FRAMES_PER_MINUTE,
(frames/FRAMES_PER_MINUTE==1?"":"s"));
frames ++;
}
if (picture->is_picture && picture->afd != afd)
{
double total_seconds = frames / (double)FRAMES_PER_SECOND;
int minutes = (int)(total_seconds / 60);
double seconds = total_seconds - 60*minutes;
fprint_msg("%dm %4.1fs (frame %d @ %.2fs): ",minutes,seconds,
frames,total_seconds);
report_h262_picture(picture,FALSE);
afd = picture->afd;
}
free_h262_picture(&picture);
if (max > 0 && frames >= max)
break;
}
free_h262_context(&h262);
{
double total_seconds = frames / (double)FRAMES_PER_SECOND;
int minutes = (int)(total_seconds / 60);
double seconds = total_seconds - 60*minutes;
fprint_msg("Found %d MPEG-2 frame%s",frames,(frames==1?"":"s"));
fprint_msg(" which is %dm %.1fs (%.2fs)\n",minutes,seconds,total_seconds);
}
}
/*
* Report on the content of an MPEG2 file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` MPEG items
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*/
static void report_h262_items(ES_p es,
int max,
int verbose,
int quiet)
{
int err;
int count = 0;
for (;;)
{
h262_item_p item;
err = find_next_h262_item(es,&item);
if (err == EOF)
break;
else if (err)
{
print_err("### Error finding next H.262 item\n");
break;
}
count++;
if (!quiet)
report_h262_item(item);
if (verbose)
print_data(TRUE," Data",
item->unit.data,item->unit.data_len,10);
free_h262_item(&item);
if (max > 0 && count >= max)
break;
}
fprint_msg("Found %d MPEG-2 item%s\n",count,(count==1?"":"s"));
}
/*
* Report on the data by NAL units.
*/
static void report_by_nal_unit(ES_p es,
int max,
int quiet,
int show_nal_details)
{
int err = 0;
nal_unit_context_p context = NULL;
int ref_idcs[4] = {0}; // values 0,1,2,3
int unit_types[15] = {0};
int slice_types[10] = {0};
err = build_nal_unit_context(es,&context);
if (err)
{
print_err("### Unable to build NAL unit context to read ES\n");
return;
}
if (show_nal_details)
set_show_nal_reading_details(context,TRUE);
for (;;)
{
nal_unit_p nal;
if (max > 0 && context->count >= max)
{
fprint_msg("\nStopping because %d NAL units have been read\n",
context->count);
break;
}
err = find_next_NAL_unit(context,!quiet,&nal);
if (err == 2)
{
print_msg("... ignoring broken NAL unit\n");
continue;
}
else if (err)
break;
ref_idcs[nal->nal_ref_idc] ++;
if (nal->nal_unit_type < 13)
unit_types[nal->nal_unit_type] ++;
else if (nal->nal_unit_type < 24)
unit_types[13] ++;
else
unit_types[14] ++;
if (nal_is_slice(nal))
slice_types[nal->u.slice.slice_type] ++;
free_nal_unit(&nal);
}
if (err == EOF && !quiet)
print_msg("EOF\n");
if (err == 0 || err == EOF)
{
int ii;
fprint_msg("Found %d NAL unit%s\n",context->count,(context->count==1?"":"s"));
print_msg("nal_ref_idc:\n");
for (ii=0; ii<4; ii++)
if (ref_idcs[ii] > 0)
fprint_msg(" %8d of %2d%s\n",ref_idcs[ii],ii,ii?"":" (non-reference)");
print_msg("nal_unit_type:\n");
for (ii=0; ii<13; ii++)
if (unit_types[ii] > 0)
fprint_msg(" %8d of type %2d (%s)\n",unit_types[ii],ii,NAL_UNIT_TYPE_STR(ii));
if (unit_types[13] > 0)
fprint_msg(" %8d of type 13..23 (Reserved)\n",unit_types[13]);
if (unit_types[14] > 0)
fprint_msg(" %8d of typ 24..31 (Unspecified)\n",unit_types[14]);
print_msg("slice_type:\n");
for (ii=0; ii<10; ii++)
if (slice_types[ii] > 0)
fprint_msg(" %8d of type %2d (%s)\n",slice_types[ii],ii,
NAL_SLICE_TYPE_STR(ii));
}
else
print_err("### Abandoning reporting due to error\n");
free_nal_unit_context(&context);
}
/*
* Report on the content of an MPEG2 file
*
* - `es` is the input elementary stream
* - if `max` is non-zero, then reporting will stop after `max` MPEG items
* - if `quiet` is true, then only errors will be reported
*/
static void find_h264_fields(ES_p es,
int max,
int quiet,
int verbose,
int show_nal_details)
{
int err;
int count = 0;
int num_fields = 0;
int num_frames = 0;
access_unit_context_p context;
uint32_t num_with_PTS = 0;
err = build_access_unit_context(es,&context);
if (err) return;
if (show_nal_details)
set_show_nal_reading_details(context->nac,TRUE);
for (;;)
{
access_unit_p access_unit;
// NB: remember *not* to call get_next_h264_frame!
err = get_next_access_unit(context,quiet,verbose,&access_unit);
if (err == EOF)
break;
else if (err)
{
print_err("### Error getting next access unit\n");
break;
}
count++;
if (access_unit->field_pic_flag == 1)
{
report_access_unit(access_unit);
num_fields ++;
}
else
num_frames ++;
if (access_unit_has_PTS(access_unit))
num_with_PTS ++;
free_access_unit(&access_unit);
if (max > 0 && count >= max)
break;
}
fprint_msg("Found %d MPEG-4 picture%s, %d field%s, %d frame%s\n",
count,(count==1?"":"s"),
num_fields,(num_fields==1?"":"s"),
num_frames,(num_frames==1?"":"s"));
fprint_msg("Fields with PTS associated: %u\n",num_with_PTS);
free_access_unit_context(&context);
}
/*
* Report on data by access unit.
*/
static void report_h264_frames(ES_p es,
int max,
int quiet,
int verbose,
int show_nal_details,
int count_sizes,
int count_types)
{
int err = 0;
int access_unit_count = 0;
access_unit_context_p context;
uint32_t min_frame_size = 1000000;
uint32_t max_frame_size = 0;
uint32_t sum_frame_size = 0;
uint32_t num_with_PTS = 0;
#define I_NON_REF 0
#define I_REF_IDR 1
#define I_REF_NON_IDR 2
#define I_OTHER 3
#define I_SLICE_I 0
#define I_SLICE_P 1
#define I_SLICE_B 2
#define I_SLICE_MIX 3
uint32_t slice_types[3][4] = {{0},{0}};
uint32_t slice_categories[4] = {0};
ES_offset start;
uint32_t length;
err = build_access_unit_context(es,&context);
if (err) return;
if (show_nal_details)
set_show_nal_reading_details(context->nac,TRUE);
for (;;)
{
access_unit_p access_unit;
access_unit_count ++;
err = get_next_h264_frame(context,quiet,verbose,&access_unit);
if (err)
break;
if (!quiet)
report_access_unit(access_unit);
if (count_sizes)
{
err = get_access_unit_bounds(access_unit,&start,&length);
if (err) break;
if (min_frame_size > length) min_frame_size = length;
if (max_frame_size < length) max_frame_size = length;
sum_frame_size += length;
}
if (count_types && access_unit->primary_start != NULL)
{
if (access_unit->primary_start->nal_ref_idc == 0)
{
slice_categories[I_NON_REF] ++;
if (all_slices_I(access_unit))
slice_types[I_NON_REF][I_SLICE_I] ++;
else if (all_slices_P(access_unit))
slice_types[I_NON_REF][I_SLICE_P] ++;
else if (all_slices_B(access_unit))
slice_types[I_NON_REF][I_SLICE_B] ++;
else
slice_types[I_NON_REF][I_SLICE_MIX] ++;
}
else if (access_unit->primary_start->nal_unit_type == NAL_IDR)
{
// Yes, I know that only I and SI frames should be allowed for IDR
slice_categories[I_REF_IDR] ++;
if (all_slices_I(access_unit))
slice_types[I_REF_IDR][I_SLICE_I] ++;
else if (all_slices_P(access_unit))
slice_types[I_REF_IDR][I_SLICE_P] ++;
else if (all_slices_B(access_unit))
slice_types[I_REF_IDR][I_SLICE_B] ++;
else
slice_types[I_REF_IDR][I_SLICE_MIX] ++;
}
else if (access_unit->primary_start->nal_unit_type == NAL_NON_IDR)
{
slice_categories[I_REF_NON_IDR] ++;
if (all_slices_I(access_unit))
slice_types[I_REF_NON_IDR][I_SLICE_I] ++;
else if (all_slices_P(access_unit))
slice_types[I_REF_NON_IDR][I_SLICE_P] ++;
else if (all_slices_B(access_unit))
slice_types[I_REF_NON_IDR][I_SLICE_B] ++;
else
slice_types[I_REF_NON_IDR][I_SLICE_MIX] ++;
}
else
slice_categories[I_OTHER] ++;
}
if (access_unit_has_PTS(access_unit))
num_with_PTS ++;
free_access_unit(&access_unit);
// Did the logical stream end after the last access unit?
if (context->end_of_stream)
{
if (!quiet) print_msg("Found End-of-stream NAL unit\n");
break;
}
if (max > 0 && access_unit_count >= max)
{
fprint_msg("\nStopping because (at least) %d frames have been read\n",
access_unit_count);
break;
}
}
fprint_msg("Found %d frame%s (%d NAL unit%s)\n",
access_unit_count,(access_unit_count==1?"":"s"),
context->nac->count,(context->nac->count==1?"":"s"));
if (count_types)
{
if (slice_categories[I_NON_REF] > 0)
{
print_msg("Non-reference frames:\n");
if (slice_types[I_NON_REF][I_SLICE_I] != 0)
fprint_msg(" I frames %7d\n",slice_types[I_NON_REF][I_SLICE_I]);
if (slice_types[I_NON_REF][I_SLICE_P] != 0)
fprint_msg(" P frames %7d\n",slice_types[I_NON_REF][I_SLICE_P]);
if (slice_types[I_NON_REF][I_SLICE_B] != 0)
fprint_msg(" B frames %7d\n",slice_types[I_NON_REF][I_SLICE_B]);
if (slice_types[I_NON_REF][I_SLICE_MIX] != 0)
fprint_msg(" Mixed/other %7d\n",slice_types[I_NON_REF][I_SLICE_MIX]);
}
if (slice_categories[I_REF_IDR] > 0)
{
print_msg("IDR frames\n");
if (slice_types[I_REF_IDR][I_SLICE_I] != 0)
fprint_msg(" I frames %7d\n",slice_types[I_REF_IDR][I_SLICE_I]);
if (slice_types[I_REF_IDR][I_SLICE_P] != 0)
fprint_msg(" P frames %7d\n",slice_types[I_REF_IDR][I_SLICE_P]);
if (slice_types[I_REF_IDR][I_SLICE_B] != 0)
fprint_msg(" B frames %7d\n",slice_types[I_REF_IDR][I_SLICE_B]);
if (slice_types[I_REF_IDR][I_SLICE_MIX] != 0)
fprint_msg(" Mixed/other %7d\n",slice_types[I_REF_IDR][I_SLICE_MIX]);
}
if (slice_categories[I_REF_NON_IDR] > 0)
{
print_msg("Non-IDR reference frames:\n");
if (slice_types[I_REF_NON_IDR][I_SLICE_I] != 0)
fprint_msg(" I frames %7d\n",slice_types[I_REF_NON_IDR][I_SLICE_I]);
if (slice_types[I_REF_NON_IDR][I_SLICE_P] != 0)
fprint_msg(" P frames %7d\n",slice_types[I_REF_NON_IDR][I_SLICE_P]);
if (slice_types[I_REF_NON_IDR][I_SLICE_B] != 0)
fprint_msg(" B frames %7d\n",slice_types[I_REF_NON_IDR][I_SLICE_B]);
if (slice_types[I_REF_NON_IDR][I_SLICE_MIX] != 0)
fprint_msg(" Mixed/other %7d\n",slice_types[I_REF_NON_IDR][I_SLICE_MIX]);
}
if (slice_categories[I_OTHER] > 0)
fprint_msg("Other frame types: %d\n",slice_categories[I_OTHER]);
}
{
double total_seconds = access_unit_count / (double)FRAMES_PER_SECOND;
int minutes = (int)(total_seconds / 60);
double seconds = total_seconds - 60*minutes;
fprint_msg("At 25 frames/second, that is %dm %.1fs (%.2fs)\n",minutes,seconds,
total_seconds);
}
if (count_sizes && access_unit_count > 0)
fprint_msg("Frame sizes ranged from %u to %u bytes, mean %.2f\n",
min_frame_size,max_frame_size,
sum_frame_size/(double)access_unit_count);
fprint_msg("Frames with PTS associated: %u\n",num_with_PTS);
free_access_unit_context(&context);
}
static void print_usage()
{
print_msg(
"Usage: esreport [switches] [<infile>]\n"
"\n"
);
REPORT_VERSION("esreport");
print_msg(
"\n"
" Report on the content of an elementary stream containing H.264\n"
" (MPEG-4/AVC), H.262 (MPEG-2) or AVS video data.\n"
"\n"
"Files:\n"
" <infile> is the Elementary Stream file (but see -stdin below)\n"
"\n"
"What to report:\n"
" The default is to report on H.262 items, AVS frames or H.264 NAL units.\n"
" Other choices are:\n"
"\n"
" -frames Report by frames. The default for AVS.\n"
" -findfields Report on any fields in the data. Ignored for AVS.\n"
" -afd Report (just) on AFD changes in H.262. Ignored for the\n"
" other types of file.\n"
" -es Report on ES units.\n"
"\n"
" Reporting on frames may be modified by:\n"
"\n"
" -framesize Report on the sizes of frames (mean, etc.).\n"
" -frametype Report on the numbers of different type of frame.\n"
"\n"
" (in fact, both of these imply -frame).\n"
"\n"
"Other switches:\n"
" -err stdout Write error messages to standard output (the default)\n"