AVPKit
MediaFilter.h
1 /*******************************************************************************
2  * Copyright (c) 2024, 2026, Olivier Ayache. All rights reserved.
3  *
4  * This file is part of AVPKit.
5  *
6  * AVPKit is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * AVPKit is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with AVPKit. If not, see <http://www.gnu.org/licenses/>.
18  *******************************************************************************/
19 
20 #ifndef MEDIAFILTER_H
21 #define MEDIAFILTER_H
22 
23 #include <com/avpkit/ferry/RefCounted.h>
24 #include <com/avpkit/core/IMediaFilter.h>
25 
26 extern "C" {
27 #include "libavfilter/avfilter.h"
28 #include "libavutil/opt.h"
29 }
30 
31 namespace com {
32  namespace avpkit {
33  namespace core {
34 
35  class MediaFilter : public virtual IMediaFilter {
36  VS_JNIUTILS_REFCOUNTED_OBJECT_PRIVATE_MAKE(MediaFilter)
37  public:
38  static MediaFilter* make(AVFilterGraph* graph, const char* name);
39 
40  virtual int setIntProperty(const char* name, int value) {
41  if (mFilterContext) {
42  return av_opt_set_int(mFilterContext, name, value, AV_OPT_SEARCH_CHILDREN);
43  }
44  return -1;
45  }
46 
47  virtual int setDoubleProperty(const char* name, double value) {
48  if (mFilterContext) {
49  return av_opt_set_double(mFilterContext, name, value, AV_OPT_SEARCH_CHILDREN);
50  }
51  return -1;
52 
53  }
54 
55 
56  virtual int setProperty(const char* name, const char* value) {
57  if (mFilterContext) {
58  avfilter_process_command(mFilterContext, name, value, 0, 0, 0);
59  return av_opt_set(mFilterContext, name, value, AV_OPT_SEARCH_CHILDREN);
60  }
61  return -1;
62  }
63 
64  virtual int setRationalProperty(const char* name, IRational* value) {
65  if (mFilterContext) {
66 
67  return av_opt_set_q(mFilterContext, name, (AVRational) {
68  value->getNumerator(), value->getDenominator()
69  }, AV_OPT_SEARCH_CHILDREN);
70  }
71  return -1;
72  }
73 
74  virtual int initFilter() {
75  return avfilter_init_str(mFilterContext, NULL);
76  }
77 
78  virtual int addFilter(IMediaFilter* filter);
79 
80  virtual int addSink(IBufferSink* filterSink);
81 
82  virtual void setReady(){
83  ready = true;
84  }
85 
86  protected:
87 
88  const AVFilter* mFilter;
89  AVFilterContext* mFilterContext;
90  bool ready;
91 
92  MediaFilter();
93 
94  virtual ~MediaFilter();
95 
96  int getAvailableInput() {
97  for (int i = 0; i < mFilterContext->nb_inputs; i++) {
98  if (!mFilterContext->inputs[i]) {
99  return i;
100  }
101  }
102  return -1;
103  }
104 
105  int getAvailableOutput() {
106  for (int i = 0; i < mFilterContext->nb_outputs; i++) {
107  if (!mFilterContext->outputs[i]) {
108  return i;
109  }
110  }
111  return -1;
112  }
113 
114  AVFilterContext* getAVFilter() {
115  return mFilterContext;
116  }
117 
118  private:
119  //TODO: add RefPoint to next filter
120 
121 
122  };
123 
124  }
125  }
126 }
127 
128 #endif /* MEDIAFILTER_H */
129 
This class wraps represents a Rational number for the AVPKit.
Definition: IRational.h:43
virtual int32_t getDenominator()=0
Get the denominator for this rational.
virtual int32_t getNumerator()=0
Get the numerator for this rational.
virtual int addSink(IBufferSink *filterSink)
Add a filter as an output of this filter.
Definition: MediaFilter.cpp:51
virtual int setRationalProperty(const char *name, IRational *value)
Sets a rational parameter for this filter.
Definition: MediaFilter.h:64
virtual int addFilter(IMediaFilter *filter)
Add a filter as an output of this filter.
Definition: MediaFilter.cpp:42
virtual int setIntProperty(const char *name, int value)
Sets an integer parameter for this filter.
Definition: MediaFilter.h:40
virtual int initFilter()
Initialize filter.
Definition: MediaFilter.h:74
virtual int setDoubleProperty(const char *name, double value)
Sets a double parameter for this filter.
Definition: MediaFilter.h:47
virtual int setProperty(const char *name, const char *value)
Sets a string parameter for this filter.
Definition: MediaFilter.h:56
WARNING: Do not use logging in this class, and do not set any static file variables to values other t...