Valkka  1.6.1
OpenSource Video Management
framefilter.h
Go to the documentation of this file.
1 #ifndef framefilter_HEADER_GUARD
2 #define framefilter_HEADER_GUARD
3 /*
4  * framefilter.h : Definition of FrameFilter and derived classes for various purposes
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
36 #include "frame.h"
37 #include "framefifo.h"
38 
44 class FrameFilter { // <pyapi>
45 
46 public: // <pyapi>
53  FrameFilter(const char *name, FrameFilter *next = NULL); // don't include into the python api (this class is abstract)
54  virtual ~FrameFilter();
55 
56 public:
57  std::string name;
58 
59 public: // sometimes ffmpeg static functions need to access this! (in muxing at least)
61 
62 protected: // <pyapi>
63  // does the filtering
64  virtual void go(Frame *frame) = 0;
65 
66 public: // API
69  virtual void run(Frame *frame);
70  void setVoid();
71 }; // <pyapi>
72 
76 class DummyFrameFilter : public FrameFilter { // <pyapi>
77 
78 public: // <pyapi>
79  DummyFrameFilter(const char *name, bool verbose = true, FrameFilter *next = NULL); // <pyapi>
80 
81 protected:
82  bool verbose;
83 
84 protected:
85  void go(Frame *frame);
86 
87 }; // <pyapi>
88 
92 class InfoFrameFilter : public FrameFilter { // <pyapi>
93 
94 public: // <pyapi>
95  InfoFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
96 
97 protected:
98  void go(Frame *frame);
99 }; // <pyapi>
100 
104 class BriefInfoFrameFilter : public FrameFilter { // <pyapi>
105 
106 public: // <pyapi>
107  BriefInfoFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
108 
109 protected:
110  void go(Frame *frame);
111 }; // <pyapi>
112 
122 class ThreadSafeFrameFilter : public FrameFilter { // <pyapi>
123 
124 private:
125  std::mutex mutex;
126 
127 public: // <pyapi>
128  ThreadSafeFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
129 
130 protected:
131  void go(Frame *frame);
132 
133 public:
134  void run(Frame *frame);
135 }; // <pyapi>
136 
141 class ForkFrameFilter : public FrameFilter { // <pyapi>
142 
143 public: // <pyapi>
149  ForkFrameFilter(const char *name, FrameFilter *next = NULL, FrameFilter *next2 = NULL); // <pyapi>
150 
151 protected:
152  FrameFilter *next2;
153 
154 protected:
155  void go(Frame *frame);
156 
157 public:
158  void run(Frame *frame);
159 }; // <pyapi>
160 
165 class ForkFrameFilter3 : public FrameFilter { // <pyapi>
166 
167 public: // <pyapi>
174  ForkFrameFilter3(const char *name, FrameFilter *next = NULL, FrameFilter *next2 = NULL, FrameFilter *next3 = NULL); // <pyapi>
175 
176 protected:
177  FrameFilter *next2;
178  FrameFilter *next3;
179 
180 protected:
181  void go(Frame *frame);
182 
183 public:
184  void run(Frame *frame);
185 }; // <pyapi>
186 
193 class ForkFrameFilterN : public FrameFilter { // <pyapi>
194 
195 public: // <pyapi>
201  ForkFrameFilterN(const char *name); // <pyapi>
204  virtual ~ForkFrameFilterN(); // <pyapi>
205 
206 protected:
207  std::mutex mutex;
208  std::map<std::string, FrameFilter *> framefilters;
209 
210 protected:
211  void go(Frame *frame);
212 
213 public:
214  void run(Frame *frame);
215 
216 public: // <pyapi>
223  bool connect(const char *tag, FrameFilter *filter); // <pyapi>
229  bool disconnect(const char *tag); // <pyapi>
230 }; // <pyapi>
231 
235 class SlotFrameFilter : public FrameFilter { // <pyapi>
236 
237 public: // <pyapi>
238  SlotFrameFilter(const char *name, SlotNumber n_slot, FrameFilter *next = NULL); // <pyapi>
239 
240 protected:
241  unsigned n_slot;
242 
243 protected:
244  void go(Frame *frame);
245 
246 }; // <pyapi>
247 
252 class PassSlotFrameFilter : public FrameFilter { // <pyapi>
253 
254 public: // <pyapi>
255  PassSlotFrameFilter(const char *name, SlotNumber n_slot, FrameFilter *next = NULL); // <pyapi>
256 
257 protected:
258  unsigned n_slot;
259 
260 protected:
261  void go(Frame *frame);
262 
263 public:
264  void run(Frame *frame);
265 }; // <pyapi>
266 
270 class DumpFrameFilter : public FrameFilter { // <pyapi>
271 
272 public: // <pyapi>
273  DumpFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
274 
275 protected:
276  int count;
277 
278 protected:
279  void go(Frame *frame);
280 }; // <pyapi>
281 
286 class CountFrameFilter : public FrameFilter { // <pyapi>
287 
288 public: // <pyapi>
289  CountFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
290 
291 protected:
292  int count;
293 
294 protected:
295  void go(Frame *frame);
296 }; // <pyapi>
297 
301 class TimestampFrameFilter : public FrameFilter { // <pyapi>
302 
303 public: // <pyapi>
304  TimestampFrameFilter(const char *name, FrameFilter *next = NULL, long int msdiff_max = TIMESTAMP_CORRECT_TRESHOLD); // <pyapi>
305 
306 protected:
307  long int mstime_delta;
308  long int msdiff_max;
309 
310 protected:
311  void go(Frame *frame);
312 }; // <pyapi>
313 
317 class TimestampFrameFilter2 : public FrameFilter { // <pyapi>
318 
319 public: // <pyapi>
320  TimestampFrameFilter2(const char *name, FrameFilter *next = NULL, long int msdiff_max = TIMESTAMP_CORRECT_TRESHOLD); // <pyapi>
321 
322 protected:
323  long int mstime_delta;
324  long int msdiff_max;
325  long int savedtimestamp;
326 
327 protected:
328  void go(Frame *frame);
329 }; // <pyapi>
330 
334 class DummyTimestampFrameFilter : public FrameFilter { // <pyapi>
335 
336 public: // <pyapi>
337  DummyTimestampFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
338 
339 protected:
340  void go(Frame *frame);
341 }; // <pyapi>
342 
349 class RepeatH264ParsFrameFilter : public FrameFilter { // <pyapi>
350 
351 public: // <pyapi>
352  RepeatH264ParsFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
353 
354 protected:
355  BasicFrame sps, pps;
356  int phase;
357 
358 protected:
359  void go(Frame *frame);
360 
361 public:
362  void run(Frame *frame);
363 
364 }; // <pyapi>
365 
374 class GateFrameFilter : public FrameFilter { // <pyapi>
375 
376 public: // <pyapi>
377  GateFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
378 
379 protected:
380  bool on;
381  bool config_frames;
382  std::mutex mutex;
383 
384 protected:
385  void go(Frame *frame);
386 
387 public:
388  void run(Frame *frame);
389 
390 public: // <pyapi>
391  void set(); // <pyapi>
392  void unSet(); // <pyapi>
393  void passConfigFrames(); // <pyapi>
394  void noConfigFrames(); // <pyapi>
395 }; // <pyapi>
396 
401 class SwitchFrameFilter : public FrameFilter { // <pyapi>
402 
403 public: // <pyapi>
404  SwitchFrameFilter(const char *name, FrameFilter *next1 = NULL, FrameFilter *next2 = NULL); // <pyapi>
405 
406 protected:
407  FrameFilter *next1;
408  FrameFilter *next2;
409 
410 protected:
411  int index;
412  std::mutex mutex;
413 
414 protected:
415  void go(Frame *frame);
416 
417 public:
418  void run(Frame *frame);
419 
420 public:
421  void set1(); // <pyapi>
422  void set2(); // <pyapi>
423 }; // <pyapi>
424 
431 {
432 
433 public:
434  TypeFrameFilter(const char *name, FrameClass frameclass, FrameFilter *next = NULL);
435 
436 protected:
437  FrameClass frameclass;
438 
439 protected:
440  void go(Frame *frame);
441  void run(Frame *frame);
442 };
443 
449 class CachingGateFrameFilter : public FrameFilter { // <pyapi>
450 
451 public: // <pyapi>
452  CachingGateFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
453 
454 protected:
455  bool on;
456  std::mutex mutex;
457  SetupFrame setupframe; // TODO: shouldn't we have array of setupframes here?
458  bool got_setup;
459 
460 protected:
461  void go(Frame *frame);
462 
463 public:
464  void run(Frame *frame);
465 
466 public: // <pyapi>
467  void set(); // <pyapi>
468  void unSet(); // <pyapi>
469 }; // <pyapi>
470 
479 class SetSlotFrameFilter : public FrameFilter { // <pyapi>
480 
481 public: // <pyapi>
482  SetSlotFrameFilter(const char *name, FrameFilter *next = NULL); // <pyapi>
483 
484 protected:
485  SlotNumber n_slot;
486  std::mutex mutex;
487 
488 protected:
489  void go(Frame *frame);
490 
491 public: // <pyapi>
492  void setSlot(SlotNumber n = 0); // <pyapi>
493 
494 }; // <pyapi>
495 
506 class TimeIntervalFrameFilter : public FrameFilter { // <pyapi>
507 
508 public: // <pyapi>
509  TimeIntervalFrameFilter(const char *name, long int mstimedelta, FrameFilter *next = NULL); // <pyapi>
510 
511 protected:
512  long int mstimedelta;
513  long int prevmstimestamp;
514 
515 protected:
516  void go(Frame *frame);
517 
518 public:
519  void run(Frame *frame);
520 
521 }; // <pyapi>
522 
530 class FifoFrameFilter : public FrameFilter { // <pyapi>
531 
532 public: // <pyapi>
538  FifoFrameFilter(const char *name, FrameFifo *framefifo);
539 
540 protected:
541  FrameFifo *framefifo;
542 
543 protected:
544  void go(Frame *frame);
545 }; // <pyapi>
546 
554 class BlockingFifoFrameFilter : public FrameFilter { // <pyapi>
555 
556 public: // <pyapi>
557  BlockingFifoFrameFilter(const char *name, FrameFifo *framefifo);
558 
559 protected:
560  FrameFifo *framefifo;
561 
562 protected:
563  void go(Frame *frame);
564 }; // <pyapi>
565 
575 class SwScaleFrameFilter : public FrameFilter { // <pyapi>
576 
577 public: // <pyapi>
578  SwScaleFrameFilter(const char *name, int target_width, int target_height, FrameFilter *next = NULL);
580 
581 protected: // initialized at constructor
584  int width;
585  int height;
586  AVRGBFrame outputframe;
587  SwsContext *sws_ctx;
588 
589 protected:
590  void go(Frame *frame);
591 
592 public:
593  void run(Frame *frame);
594 }; // <pyapi>
595 
596 #endif
Decoded YUV frame in a non-planar format (thus "NP")
Definition: frame.h:437
Custom payload Frame.
Definition: frame.h:166
Passes frames to a multiprocessing fifo.
Definition: framefilter.h:554
BlockingFifoFrameFilter(const char *name, FrameFifo *framefifo)
Default constructor // <pyapi>
Definition: framefilter.cpp:709
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:711
Dump the beginning of Frame's payload into stdout in a one-liner.
Definition: framefilter.h:104
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:92
Caches SetupFrame s.
Definition: framefilter.h:449
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:606
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:602
Counts frames passed through this filter.
Definition: framefilter.h:286
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:305
A "hello world" demo class: prints its own name if verbose is set to true.
Definition: framefilter.h:76
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:63
Substitute timestamps with the time they arrive to the client.
Definition: framefilter.h:334
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:397
Dumps each received packet to a file: use with care! For debugging purposes only.
Definition: framefilter.h:270
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:274
Passes frames to a FrameFifo.
Definition: framefilter.h:530
FifoFrameFilter(const char *name, FrameFifo *framefifo)
Default constructor.
Definition: framefilter.cpp:702
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:704
Replicates frame flow to three filters Use this frame filter to create frame filter tree structures.
Definition: framefilter.h:165
ForkFrameFilter3(const char *name, FrameFilter *next=NULL, FrameFilter *next2=NULL, FrameFilter *next3=NULL)
Default constructor.
Definition: framefilter.cpp:146
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:177
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:150
Replicates frame flow to arbitrary number of outputs.
Definition: framefilter.h:193
bool connect(const char *tag, FrameFilter *filter)
Connect a new terminal FrameFilter.
Definition: framefilter.cpp:200
virtual ~ForkFrameFilterN()
Default virtual dtor.
Definition: framefilter.cpp:186
bool disconnect(const char *tag)
Disconnect a connection tagged with a name.
Definition: framefilter.cpp:220
void run(Frame *frame)
called by other FrameFilter(s)
Definition: framefilter.cpp:190
ForkFrameFilterN(const char *name)
Default ctor.
Definition: framefilter.cpp:182
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:237
std::map< std::string, FrameFilter * > framefilters
nametag to connecting FrameFilter mapping
Definition: framefilter.h:208
Replicates frame flow to two filters Use this frame filter to create frame filter tree structures.
Definition: framefilter.h:141
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:121
ForkFrameFilter(const char *name, FrameFilter *next=NULL, FrameFilter *next2=NULL)
Default constructor.
Definition: framefilter.cpp:117
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:141
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:44
FrameFilter(const char *name, FrameFilter *next=NULL)
Default constructor.
Definition: framefilter.cpp:39
void setVoid()
nullifies the next framefilter in the chain -> cuts the framefilter chain
Definition: framefilter.cpp:53
virtual void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:43
FrameFilter * next
The next frame filter in the chain to be applied.
Definition: framefilter.h:60
virtual void go(Frame *frame)=0
Does the actual filtering/modification to the Frame. Define in subclass.
virtual ~FrameFilter()
Virtual destructor // <pyapi>
Definition: framefilter.cpp:41
Frame: An abstract queueable class.
Definition: frame.h:112
When turned on, passes frames.
Definition: framefilter.h:374
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:506
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:502
Dump the beginning of Frame's payload into stdout.
Definition: framefilter.h:92
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:76
Passes through frames with a certain slot number only.
Definition: framefilter.h:252
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:254
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:258
For H264, some cameras don't send sps and pps packets again before every keyframe.
Definition: framefilter.h:349
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:412
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:406
Changes the slot number of the Frame.
Definition: framefilter.h:479
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:648
Setup frame.
Definition: frame.h:277
Sets the frame slot value.
Definition: framefilter.h:235
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:245
Interpolate from YUV bitmap to RGB.
Definition: framefilter.h:575
SwsContext * sws_ctx
FFmpeg scaling context structure.
Definition: framefilter.h:587
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:791
int target_width
target frame width
Definition: framefilter.h:582
int target_height
target frame height
Definition: framefilter.h:583
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:782
SwScaleFrameFilter(const char *name, int target_width, int target_height, FrameFilter *next=NULL)
Default constructor // <pyapi>
Definition: framefilter.cpp:750
~SwScaleFrameFilter()
Default destructor // <pyapi>
Definition: framefilter.cpp:770
Passes frame to one of the two terminals.
Definition: framefilter.h:401
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:552
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:556
FrameFilter s that are fed from various different threads, should be protected with this.
Definition: framefilter.h:122
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:113
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:101
Pass frames, but not all of them - only on regular intervals.
Definition: framefilter.h:506
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:668
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:674
Corrects erroneous timestamps (while preserving timestamp distances).
Definition: framefilter.h:317
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:347
Corrects erroneous timestamps (while preserving timestamp distances).
Definition: framefilter.h:301
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:315
Passes through frames of certain type only.
Definition: framefilter.h:431
void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next !...
Definition: framefilter.cpp:590
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: framefilter.cpp:586
Frame classes.
Thread safe system of fifo and a stack.
FrameClass
Enumeration of Frame classes used by Valkka.
Definition: frame.h:53