media_filter.h 4.97 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2018-2019 Savoir-faire Linux Inc.
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
 *
 *  Author: Philippe Gorley <philippe.gorley@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#pragma once

#include "config.h"
#include "media_stream.h"
#include "noncopyable.h"

#include <map>
#include <string>
#include <vector>

31
extern "C" {
32 33 34
struct AVFilterContext;
struct AVFilterGraph;
struct AVFilterInOut;
35
}
36

Adrien Béraud's avatar
Adrien Béraud committed
37
namespace jami {
38 39 40 41

/**
 * Provides access to libavfilter.
 *
42
 * Can be used for filters with unlimited number of inputs.
43 44 45 46 47 48
 * Multiple outputs are not supported. They add complexity for little gain.
 *
 * For information on how to write a filter graph description, see:
 * https://ffmpeg.org/ffmpeg-filters.html
 * http://trac.ffmpeg.org/wiki/FilteringGuide
 *
49 50
 * It is required to name each filter graph input. These names are used to feed the correct input.
 * It is the same name that will be passed as second argument to feedInput(AVFrame*, std::string).
51
 *
52
 * Examples:
53
 *
54 55 56 57
 * - "[in1] scale=320:240"
 * Scales the input to 320x240.
 *
 * - "[in1] scale=iw/4:ih/4 [mid]; [in2] [mid] overlay=main_w-overlay_w-10:main_h-overlay_h-10"
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
 * in1 will be scaled to 1/16th its size and placed over in2 in the bottom right corner. When feeding frames to
 * the filter, you need to specify whether the frame is destined for in1 or in2.
 */
class MediaFilter {
    public:
        MediaFilter();
        ~MediaFilter();

        /**
         * Returns the current filter graph string.
         */
        std::string getFilterDesc() const;

        /**
         * Initializes the filter graph with one or more inputs and one output. Returns a negative code on error.
         */
        int initialize(const std::string& filterDesc, std::vector<MediaStream> msps);

76 77 78 79 80
        /**
         * Returns a MediaStream object describing the input specified by @inputName.
         */
        MediaStream getInputParams(const std::string& inputName) const;

81 82 83 84 85
        /**
         * Returns a MediaStream struct describing the frames that will be output.
         *
         * When called in an invalid state, the returned format will be invalid (less than 0).
         */
86
        MediaStream getOutputParams() const;
87

88 89 90 91 92
        /**
         * Give the specified source filter an input frame. Caller is responsible for freeing the frame.
         *
         * NOTE Will fail if @inputName is not found in the graph.
         */
93
        int feedInput(AVFrame* frame, const std::string& inputName);
94 95 96 97 98

        /**
         * Pull a frame from the filter graph. Caller owns the frame reference.
         *
         * Returns AVERROR(EAGAIN) if filter graph requires more input.
99 100
         *
         * NOTE Frame reference belongs to the caller
101
         */
Adrien Béraud's avatar
Adrien Béraud committed
102
        std::unique_ptr<MediaFrame> readOutput();
103

Philippe Gorley's avatar
Philippe Gorley committed
104 105 106 107 108
        /**
         * Flush filter to indicate EOF.
         */
        void flush();

109 110 111 112 113 114 115 116 117 118 119
    private:
        NON_COPYABLE(MediaFilter);

        /**
         * Initializes output of filter graph.
         */
        int initOutputFilter(AVFilterInOut* out);

        /**
         * Initializes an input of filter graph.
         */
120
        int initInputFilter(AVFilterInOut* in, MediaStream msp);
121

122 123 124 125 126
        /**
         * Reinitializes the filter graph with @inputParams_, which should be updated beforehand.
         */
        int reinitialize();

127 128 129 130 131
        /**
         * Convenience method that prints @msg and returns err.
         *
         * NOTE @msg should not be null.
         */
132
        int fail(std::string msg, int err) const;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

        /**
         * Frees resources used by MediaFilter.
         */
        void clean();

        /**
         * Filter graph pointer.
         */
        AVFilterGraph* graph_ = nullptr;

        /**
         * Filter graph output. Corresponds to a buffersink/abuffersink filter.
         */
        AVFilterContext* output_ = nullptr;

        /**
         * List of filter graph inputs. Each corresponds to a buffer/abuffer filter.
         */
        std::vector<AVFilterContext*> inputs_;

        /**
155
         * List of filter graph input parameters. Same order as @inputs_.
156
         */
157
        std::vector<MediaStream> inputParams_;
158 159 160 161 162 163 164 165 166 167 168 169

        /**
         * Filter graph string.
         */
        std::string desc_ {};

        /**
         * Flag to know whether or not the filter graph is initialized.
         */
        bool initialized_ {false};
};

Adrien Béraud's avatar
Adrien Béraud committed
170
}; // namespace jami