forked from kynesim/tstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
882 lines (814 loc) · 27.3 KB
/
filter.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
/*
* Support for "filtering" ES, outputting to either ES or TS.
*
* This provides the ability to "fast forward" through ES data.
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the MPEG TS, PS and ES tools.
*
* The Initial Developer of the Original Code is Amino Communications Ltd.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Amino Communications Ltd, Swavesey, Cambridge UK
*
* ***** END LICENSE BLOCK *****
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include "compat.h"
#include "es_fns.h"
#include "ts_fns.h"
#include "accessunit_fns.h"
#include "h262_fns.h"
#include "misc_fns.h"
#include "printing_fns.h"
#include "filter_fns.h"
#define DEBUG 0
// ============================================================
// Managing H.262 filter contexts
// ============================================================
/*
* Build a new H.262 (MPEG-2 and also MPEG-1) filter context
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
static int new_h262_filter_context(h262_filter_context_p *fcontext)
{
h262_filter_context_p new = malloc(SIZEOF_H262_FILTER_CONTEXT);
if (new == NULL)
{
print_err("### Unable to allocate H.262 filter context\n");
return 1;
}
new->h262 = NULL;
new->last_seq_hdr = NULL;
new->new_seq_hdr = FALSE;
reset_h262_filter_context(new);
*fcontext = new;
return 0;
}
/*
* Build a new filter context for "stripping" H.262 data
*
* - `fcontext` is the new filter context
* - `h262` is the H.262 stream to read from
* - `all_IP` is true if the software should keep all I and P pictures
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
extern int build_h262_filter_context_strip(h262_filter_context_p *fcontext,
h262_context_p h262,
int all_IP)
{
int err = new_h262_filter_context(fcontext);
if (err) return 1;
(*fcontext)->h262 = h262;
(*fcontext)->filter = FALSE;
(*fcontext)->allref = all_IP;
return 0;
}
/*
* Build a new filter context for "filtering" H.262 data
*
* - `fcontext` is the new filter context
* - `h262` is the H.262 stream to read from
* - `freq` is the desired speed-up, or the frequency at which frames
* should (ideally) be kept
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
extern int build_h262_filter_context(h262_filter_context_p *fcontext,
h262_context_p h262,
int freq)
{
int err = new_h262_filter_context(fcontext);
if (err) return 1;
(*fcontext)->h262 = h262;
(*fcontext)->filter = TRUE;
(*fcontext)->freq = freq;
return 0;
}
/*
* Reset an H.262 filter context, ready to start filtering anew.
*/
extern void reset_h262_filter_context(h262_filter_context_p fcontext)
{
fcontext->pending_EOF = FALSE;
fcontext->last_was_slice = FALSE;
fcontext->had_previous_picture = FALSE;
if (fcontext->last_seq_hdr != NULL)
free_h262_picture(&fcontext->last_seq_hdr);
fcontext->new_seq_hdr = FALSE;
fcontext->count = 0;
fcontext->frames_seen = 0;
fcontext->frames_written = 0;
}
/*
* Free a filter context
*
* NOTE that this does *not* free the H.262 datastructure to which the
* filter context refers.
*
* - `fcontext` is the filter context, which will be freed, and returned
* as NULL.
*/
extern void free_h262_filter_context(h262_filter_context_p *fcontext)
{
if ((*fcontext) == NULL)
return;
// It's a little wasteful to call this, but on the other hand it is
// guaranteed to free everything we want freeing
reset_h262_filter_context(*fcontext);
// Just lose our reference to the H.262 datastructure, don't free it
(*fcontext)->h262 = NULL;
free(*fcontext);
*fcontext = NULL;
return;
}
// ============================================================
// Managing H.264 filter contexts
// ============================================================
/*
* Build a new H.264 filter context
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
static int new_h264_filter_context(h264_filter_context_p *fcontext)
{
h264_filter_context_p new = malloc(SIZEOF_H264_FILTER_CONTEXT);
if (new == NULL)
{
print_err("### Unable to allocate H.264 filter context\n");
return 1;
}
// Unset the important things - things we might otherwise try to free
// (or, for new->es, things that stop us doing anything until we're
// setup properly by the user)
new->access_unit_context = NULL;
reset_h264_filter_context(new);
*fcontext = new;
return 0;
}
/*
* Build a new filter context for "stripping" ES data
*
* - `fcontext` is the new filter context
* - `access` is the access unit context to read from
* - `allref` is true if the software should keep all reference pictures
* (H.264) or all I and P pictures (H.264)
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
extern int build_h264_filter_context_strip(h264_filter_context_p *fcontext,
access_unit_context_p access,
int allref)
{
int err = new_h264_filter_context(fcontext);
if (err) return 1;
(*fcontext)->access_unit_context = access;
(*fcontext)->filter = FALSE;
(*fcontext)->allref = allref;
return 0;
}
/*
* Build a new filter context for "filtering" ES data
*
* - `fcontext` is the new filter context
* - `access` is the access unit context to read from
* - `freq` is the desired speed-up, or the frequency at which frames
* should (ideally) be kept
*
* Returns 0 if all goes well, 1 if something goes wrong
*/
extern int build_h264_filter_context(h264_filter_context_p *fcontext,
access_unit_context_p access,
int freq)
{
int err = new_h264_filter_context(fcontext);
if (err) return 1;
(*fcontext)->access_unit_context = access;
(*fcontext)->filter = TRUE;
(*fcontext)->freq = freq;
return 0;
}
/*
* Reset an H.264 filter context, ready to start filtering anew.
*/
extern void reset_h264_filter_context(h264_filter_context_p fcontext)
{
// `skipped_ref_pic` is TRUE if we've skipped any reference pictures
// since our last IDR (hmm - should it start off True or False?)
fcontext->skipped_ref_pic = FALSE;
// `last_accepted_was_not_IDR` is TRUE if the last frame kept (output)
// was not an IDR. We set it TRUE initially so that we will decide
// to output the first IDR we *do* find, regardless of the count.
fcontext->last_accepted_was_not_IDR = TRUE;
// And plainly we didn't have a previous access unit
fcontext->had_previous_access_unit = FALSE;
// Especially not an IDR
fcontext->not_had_IDR = TRUE;
fcontext->count = 0;
fcontext->frames_seen = 0;
fcontext->frames_written = 0;
}
/*
* Free an H.264 filter context
*
* NOTE that this does *not* free the access unit context to which the
* filter context refers.
*
* - `fcontext` is the filter context, which will be freed, and returned
* as NULL.
*/
extern void free_h264_filter_context(h264_filter_context_p *fcontext)
{
if ((*fcontext) == NULL)
return;
// It's a little wasteful to call this, but on the other hand it is
// guaranteed to free everything we want freeing
reset_h264_filter_context(*fcontext);
// Just lose our reference to the access unit context, don't free it
(*fcontext)->access_unit_context = NULL;
free(*fcontext);
*fcontext = NULL;
return;
}
// ============================================================
// Filtering H.262
// ============================================================
/*
* Retrieve the next I (and/or, if fcontext->allref, P) frame in this H.262 ES.
*
* Any sequence end "pictures" will be ignored.
*
* Note that the ES data being read should be video-only.
*
* - `fcontext` is the information that tells us what to filter and how
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*
* - `seq_hdr` is a sequence header, i.e., that used by the next frame to
* output. This will be NULL if the sequence header has not changed since
* the last call of this function.
*
* Note that the caller should *not* free this, and that it will not be
* maintained over calls of this function (i.e., it is a reference to a
* value within the `fcontext` which is altered by this function).
*
* - `frame` is the next frame to output.
*
* Note that it is the caller's responsibility to free this with
* `free_h262_picture()`.
*
* If an error or EOF is returned, this value is undefined.
*
* - `frames_seen` is the number of I and P frames (start code 0)
* found by this call of the function, including the item returned
* if appropriate.
*
* Returns 0 if it succeeds, EOF if end-of-file is read (or the last call
* returned a sequence end item), 1 if some error occurs.
*
* If command input is enabled, then it can also return COMMAND_RETURN_CODE
* if the current command has changed.
*/
extern int get_next_stripped_h262_frame(h262_filter_context_p fcontext,
int verbose,
int quiet,
h262_picture_p *seq_hdr,
h262_picture_p *frame,
int *frames_seen)
{
int err;
// A picture is built up from several items - we start with none in hand
h262_picture_p this_picture = NULL;
*frames_seen = 0;
if (fcontext->filter)
{
print_err("### Calling get_next_stripped_h262_frame with a context"
" set for filtering\n");
return 1;
}
// Otherwise, look for something we want to keep
for (;;)
{
if (es_command_changed(fcontext->h262->es))
{
*frame = *seq_hdr = NULL;
return COMMAND_RETURN_CODE;
}
err = get_next_h262_frame(fcontext->h262,verbose,quiet,&this_picture);
if (err == EOF)
{
*frame = *seq_hdr = NULL;
return err;
}
else if (err)
{
print_err("### Error filtering H.262 frames\n");
return 1;
}
// Now to stripping
if (this_picture->is_picture)
{
(*frames_seen) ++;
if ((this_picture->picture_coding_type == 1) ||
(this_picture->picture_coding_type == 2 && fcontext->allref) )
{
*frame = this_picture;
if (fcontext->new_seq_hdr)
*seq_hdr = fcontext->last_seq_hdr;
else
*seq_hdr = NULL;
fcontext->new_seq_hdr = FALSE;
if (verbose) fprint_msg(">> %s picture \n",
(this_picture->picture_coding_type==1?"I":"P"));
return 0;
}
else
free_h262_picture(&this_picture);
}
else if (this_picture->is_sequence_header)
{
// We maybe want to remember this sequence header for the next picture
if (fcontext->last_seq_hdr == NULL)
{
fcontext->last_seq_hdr = this_picture;
fcontext->new_seq_hdr = TRUE;
if (verbose) print_msg(">> First sequence header\n");
}
else if (!same_h262_picture(this_picture,fcontext->last_seq_hdr))
{
if (verbose) print_msg(">> Different sequence header\n");
free_h262_picture(&fcontext->last_seq_hdr);
fcontext->last_seq_hdr = this_picture;
fcontext->new_seq_hdr = TRUE;
}
else
{
fcontext->new_seq_hdr = FALSE;
if (verbose) print_msg(">> Identical sequence header\n");
free_h262_picture(&this_picture);
}
}
}
}
/*
* Retrieve the next I frame, from the H.262 ES, aiming for an "apparent" kept
* frequency as stated.
*
* Any sequence end "pictures" will be ignored.
*
* Note that the ES data being read should be video-only.
*
* - `fcontext` is the information that tells us what to filter and how
* (including the desired frequency)
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*
* - `seq_hdr` is a sequence header, i.e., that used by the next picture to
* output. This will be NULL if `frame` is NULL.
*
* Note that the caller should *not* free this, and that it will not be
* maintained over calls of this function (i.e., it is a reference to a
* value within the `fcontext` which is altered by this function).
*
* - `frame` is the next frame to output. This will be NULL if the last frame
* should be output again, to provide the requested apparent frequency.
*
* Note that it is the caller's responsibility to free this with
* `free_h262_picture()`.
*
* If an error or EOF is returned, this value is undefined.
*
* - `frames_seen` is the number of I and P frames found by this call of
* the function, including the item returned if appropriate.
*
* Returns 0 if it succeeds, EOF if end-of-file is read, or we've just read a
* sequence end item, or the last call ended a picture on a sequence end
* item, 1 if some error occurs.
*
* If command input is enabled, then it can also return COMMAND_RETURN_CODE
* if the current command has changed.
*/
extern int get_next_filtered_h262_frame(h262_filter_context_p fcontext,
int verbose,
int quiet,
h262_picture_p *seq_hdr,
h262_picture_p *frame,
int *frames_seen)
{
int err;
// A picture is built up from several items - we start with none in hand
h262_picture_p this_picture = NULL;
*frames_seen = 0;
if (!fcontext->filter)
{
print_err("### Calling get_next_filtered_h262_frame with a context"
" set for stripping\n");
return 1;
}
// Otherwise, look for something we want to keep
for (;;)
{
if (es_command_changed(fcontext->h262->es))
{
*frame = *seq_hdr = NULL;
return COMMAND_RETURN_CODE;
}
// If the picture is an I picture, we want it to contain an appropriate
// AFD - so ask for that
fcontext->h262->add_fake_afd = TRUE;
err = get_next_h262_frame(fcontext->h262,verbose,quiet,&this_picture);
if (err == EOF)
{
*frame = *seq_hdr = NULL;
fcontext->h262->add_fake_afd = FALSE;
return err;
}
else if (err)
{
print_err("### Error filtering H.262 frames\n");
fcontext->h262->add_fake_afd = FALSE;
return 1;
}
// Reinstate normal "only include actual AFDs"
fcontext->h262->add_fake_afd = FALSE;
// Now to filtering
if (this_picture->is_picture)
{
fcontext->count ++;
(*frames_seen) ++;
fcontext->frames_seen ++;
if (this_picture->picture_coding_type == 1 &&
fcontext->count < fcontext->freq)
{
// It is an I picture, but it is too soon
if (verbose)
{
fprint_msg("+++ %d/%d DROP: Too soon\n",fcontext->count,fcontext->freq);
}
}
else if (this_picture->picture_coding_type != 1)
{
// It is not an I picture
if (verbose)
{
fprint_msg("+++ %d/%d DROP: %s picture\n",fcontext->count,fcontext->freq,
H262_PICTURE_CODING_STR(this_picture->picture_coding_type));
}
// But do we want to pad with (i.e., repeat) the previous I picture?
if (fcontext->freq > 0)
{
int pictures_wanted = fcontext->frames_seen / fcontext->freq;
int repeat = pictures_wanted - fcontext->frames_written;
if (repeat > 0 && fcontext->had_previous_picture)
{
if (verbose) print_msg(">>> output last picture again\n");
free_h262_picture(&this_picture);
*seq_hdr = NULL;
*frame = NULL;
fcontext->frames_written ++;
return 0;
}
}
}
else
{
// It was an I picture, and not too soon
if (verbose)
{
fprint_msg("+++ %d/%d KEEP\n",fcontext->count,fcontext->freq);
}
fcontext->count = 0;
fcontext->had_previous_picture = TRUE;
*seq_hdr = fcontext->last_seq_hdr;
*frame = this_picture;
fcontext->frames_written ++;
return 0;
}
free_h262_picture(&this_picture);
}
else if (this_picture->is_sequence_header)
{
// We want to remember the sequence header for the next picture
if (fcontext->last_seq_hdr != NULL)
free_h262_picture(&fcontext->last_seq_hdr);
fcontext->last_seq_hdr = this_picture;
}
}
}
// ============================================================
// Filtering H.264
// ============================================================
/*
* Return the next IDR or I (and maybe any reference) frame from this H.264 ES.
*
* Note that the ES data being read should be video-only.
*
* - `fcontext` is the information that tells us what to filter and how
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
* - `frame` is the next frame to output.
* Note that it is the caller's responsibility to free this with
* `free_access_unit()`.
* If an error or EOF is returned, this value is undefined.
* - `frames_seen` is the number of frames found by this call
* of the function, including the frame returned.
*
* Returns 0 if it succeeds, EOF if end-of-file is read (or an an end of
* stream NAL unit has been passed), 1 if some error occurs.
*
* If command input is enabled, then it can also return COMMAND_RETURN_CODE
* if the current command has changed.
*/
extern int get_next_stripped_h264_frame(h264_filter_context_p fcontext,
int verbose,
int quiet,
access_unit_p *frame,
int *frames_seen)
{
int err = 0;
int keep = FALSE; // Should we keep the current access unit?
access_unit_p this_access_unit = NULL;
*frames_seen = 0;
for (;;)
{
if (es_command_changed(fcontext->access_unit_context->nac->es))
return COMMAND_RETURN_CODE;
if (verbose)
print_msg("\n");
err = get_next_h264_frame(fcontext->access_unit_context,quiet,verbose,
&this_access_unit);
if (err == EOF)
return err;
else if (err)
return 1;
(*frames_seen) ++;
if (this_access_unit->primary_start == NULL)
{
// We don't have a primary picture - no VCL NAL
// There seems little point in keeping the access unit
keep = FALSE;
if (verbose)
print_msg("++ DROP: no primary picture\n");
}
else if (this_access_unit->primary_start->nal_ref_idc == 0)
{
// This is not a reference frame, so it's of no interest
keep = FALSE;
if (verbose)
print_msg("++ DROP: not reference\n");
}
else if (fcontext->allref)
{
// We want to keep all reference frames
if (this_access_unit->primary_start->nal_unit_type == NAL_IDR ||
this_access_unit->primary_start->nal_unit_type == NAL_NON_IDR)
{
keep = TRUE;
if (verbose)
print_msg("++ KEEP: reference picture\n");
}
else
{
keep = FALSE;
if (verbose)
print_msg("++ DROP: sequence or parameter set, etc.\n");
}
}
else
{
// We only want to keep IDR and I frames
if (this_access_unit->primary_start->nal_unit_type == NAL_IDR)
{
keep = TRUE;
if (verbose)
print_msg("++ KEEP: IDR picture\n");
}
else if (this_access_unit->primary_start->nal_unit_type == NAL_NON_IDR &&
all_slices_I(this_access_unit))
{
keep = TRUE;
if (verbose)
print_msg("++ KEEP: all slices I\n");
}
else
{
keep = FALSE;
if (verbose)
print_msg("++ DROP: not IDR or all slices I\n");
}
}
if (keep)
{
*frame = this_access_unit;
return 0;
}
// We've no further use for this access unit
free_access_unit(&this_access_unit);
}
}
/*
* Retrieve the next frame from the H.264 (MPEG-4/AVC) ES, aiming
* for an "apparent" kept frequency as stated.
*
* Note that the ES data being read should be video-only.
*
* - `fcontext` is the information that tells us what to filter and how
* (including the desired frequency)
* - if `verbose` is true, then extra information will be output
* - if `quiet` is true, then only errors will be reported
*
* - `frame` is the next frame to output.
*
* If the function succeeds and `frame` is NULL, it means that the
* last frame should be output again.
*
* Note that it is the caller's responsibility to free this frame with
* `free_access_unit()`.
*
* If an error or EOF is returned, this value is undefined.
*
* - `frames_seen` is the number of frames found by this call of the function,
* including the frame returned.
*
* Returns 0 if all went well, 1 if something went wrong.
*
* If command input is enabled, then it can also return COMMAND_RETURN_CODE
* if the current command has changed.
*/
extern int get_next_filtered_h264_frame(h264_filter_context_p fcontext,
int verbose,
int quiet,
access_unit_p *frame,
int *frames_seen)
{
int err = 0;
int keep = FALSE; // Should we keep the current access unit?
access_unit_p this_access_unit = NULL;
*frames_seen = 0;
for (;;)
{
if (es_command_changed(fcontext->access_unit_context->nac->es))
return COMMAND_RETURN_CODE;
if (verbose)
print_msg("\n");
err = get_next_h264_frame(fcontext->access_unit_context,quiet,verbose,
&this_access_unit);
if (err == EOF)
return err;
else if (err)
return 1;
fcontext->count ++;
(*frames_seen) ++;
fcontext->frames_seen ++;
if (this_access_unit->primary_start == NULL)
{
// We don't have a primary picture - no VCL NAL
// There seems little point in keeping the access unit
keep = FALSE;
if (verbose)
fprint_msg("++ %d/%d DROP: no primary picture\n",
fcontext->count,fcontext->freq);
}
else if (this_access_unit->primary_start->nal_ref_idc == 0)
{
// This is not a reference frame, so it's of no interest
keep = FALSE;
if (verbose)
fprint_msg("++ %d/%d DROP: not a reference frame\n",
fcontext->count,fcontext->freq);
}
else if (this_access_unit->primary_start->nal_unit_type == NAL_IDR &&
fcontext->last_accepted_was_not_IDR)
{
// This frame is an IDR, and the last frame kept was not, so
// we'll output it regardless - we don't expect to get enough
// IDR pictures that this will be a problem, and they're
// valuable because they're the "limit" for other frames that
// refer backwards
// (should we reset the count to zero? - seems sensible)
keep = TRUE;
fcontext->not_had_IDR = FALSE;
fcontext->skipped_ref_pic = FALSE;
fcontext->last_accepted_was_not_IDR = FALSE;
if (verbose)
fprint_msg("++ %d/%d KEEP: IDR and last was not\n",
fcontext->count,fcontext->freq);
}
else if (this_access_unit->primary_start->nal_unit_type == NAL_IDR &&
fcontext->not_had_IDR)
{
// We haven't had an IDR yet in this filter run, so we had better
// output this one as a "good start"
keep = TRUE;
fcontext->skipped_ref_pic = FALSE;
fcontext->last_accepted_was_not_IDR = FALSE;
if (verbose)
fprint_msg("++ %d/%d KEEP: IDR and first IDR of filter run\n",
fcontext->count,fcontext->freq);
}
else if (fcontext->count < fcontext->freq)
{
// It's too soon, so ignore it - but notice that we *have*
// ignored a reference picture
keep = FALSE;
fcontext->skipped_ref_pic = TRUE;
if (verbose)
fprint_msg("++ %d/%d DROP: Too soon (skipping ref frame)\n",
fcontext->count,fcontext->freq);
}
else if (this_access_unit->primary_start->nal_unit_type == NAL_IDR)
{
// It's an IDR, so output it
keep = TRUE;
fcontext->skipped_ref_pic = FALSE;
fcontext->last_accepted_was_not_IDR = FALSE;
if (verbose)
fprint_msg("++ %d/%d KEEP: IDR\n",fcontext->count,fcontext->freq);
}
else if (all_slices_I(this_access_unit))
{
// It is an I picture (either it has all of its slices
// type "I", or it has a single slice which is of type "I")
keep = TRUE;
fcontext->last_accepted_was_not_IDR = TRUE;
if (verbose)
fprint_msg("++ %d/%d KEEP: I frame\n",fcontext->count,fcontext->freq);
}
else if (!fcontext->skipped_ref_pic && all_slices_I_or_P(this_access_unit))
{
// It is a P or I&P picture, but we know that we have output all
// the reference pictures since the last IDR, so it is
// safe to output it
keep = TRUE;
fcontext->last_accepted_was_not_IDR = TRUE;
if (verbose)
fprint_msg("++ %d/%d KEEP: P frame. no skipped ref frames\n",
fcontext->count,fcontext->freq);
}
else
{
keep = FALSE;
fcontext->skipped_ref_pic = TRUE;
if (verbose)
fprint_msg("++ %d/%d DROP: ref frame skipped earlier\n",
fcontext->count,fcontext->freq);
}
if (keep)
{
*frame = this_access_unit;
fcontext->had_previous_access_unit = TRUE;
fcontext->frames_written ++;
fcontext->count = 0;
return 0;
}
else
{
if (fcontext->freq > 0)
{
int access_units_wanted = fcontext->frames_seen / fcontext->freq;
int repeat = access_units_wanted - fcontext->frames_written;
if (repeat > 0 && fcontext->had_previous_access_unit)
{
if (verbose) print_msg(">>> output last access unit again\n");
free_access_unit(&this_access_unit);
*frame = NULL;
fcontext->frames_written ++;
return 0;
}
}
// We've no further use for this access unit
free_access_unit(&this_access_unit);
}
}
}
// Local Variables:
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 2
// End:
// vim: set tabstop=8 shiftwidth=2 expandtab: