media-codec-h264-encoder.cpp 7.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 Mediastreamer2 media-codec-h264-encoder.cpp
 Copyright (C) 2018 Belledonne Communications SARL

 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 2
 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.
*/

#include "media-codec-encoder.h"

François Grisez's avatar
François Grisez committed
22
#define MS_MEDIACODECH265_CONF(required_bitrate, bitrate_limit, resolution, fps, ncpus) \
François Grisez's avatar
François Grisez committed
23 24 25
{ required_bitrate, bitrate_limit, { MS_VIDEO_SIZE_ ## resolution ## _W, MS_VIDEO_SIZE_ ## resolution ## _H }, fps, ncpus, nullptr }

static const MSVideoConfiguration _media_codec_h264_conf_list[] = {
François Grisez's avatar
François Grisez committed
26 27 28 29 30 31 32 33 34 35
	MS_MEDIACODECH265_CONF(2048000, 1000000,       UXGA, 25,  2),
	MS_MEDIACODECH265_CONF(1024000, 5000000, SXGA_MINUS, 25,  2),
	MS_MEDIACODECH265_CONF(1024000, 5000000,       720P, 30,  2),
	MS_MEDIACODECH265_CONF( 750000, 2048000,        XGA, 25,  2),
	MS_MEDIACODECH265_CONF( 500000, 1024000,       SVGA, 15,  2),
	MS_MEDIACODECH265_CONF( 600000, 3000000,        VGA, 30,  2),
	MS_MEDIACODECH265_CONF( 400000,  800000,        VGA, 15,  2),
	MS_MEDIACODECH265_CONF( 128000,  512000,        CIF, 15,  1),
	MS_MEDIACODECH265_CONF( 100000,  380000,       QVGA, 15,  1),
	MS_MEDIACODECH265_CONF(      0,  170000,       QCIF, 10,  1),
François Grisez's avatar
François Grisez committed
36 37
};

38 39
namespace mediastreamer {

40 41
class MediaCodecH264Encoder: public MediaCodecEncoder {
public:
42
	MediaCodecH264Encoder(): MediaCodecEncoder("video/avc") {}
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

private:
	AMediaFormat *createMediaFormat() const override {
		AMediaFormat *format = MediaCodecEncoder::createMediaFormat();
		AMediaFormat_setInt32(format, "profile", _profile);
		return format;
	}

	std::ostringstream getMediaForamtAsString() const override {
		std::ostringstream os = MediaCodecEncoder::getMediaForamtAsString();
		os << "\tprofile: " << _profile << std::endl;
		return os;
	}

	static const int32_t _profile = 1; // AVCProfileBaseline
58 59
};

60 61 62
class MediaCodecH264EncoderFilterImpl: public MediaCodecEncoderFilterImpl {
public:

François Grisez's avatar
François Grisez committed
63 64
	MediaCodecH264EncoderFilterImpl(MSFilter *f): MediaCodecEncoderFilterImpl(
		f,
65
		new MediaCodecH264Encoder(),
François Grisez's avatar
François Grisez committed
66 67
		new H264NalPacker(),
		_media_codec_h264_conf_list) {}
François Grisez's avatar
François Grisez committed
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
	static void onFilterInit(MSFilter *f) {
		f->data = new MediaCodecH264EncoderFilterImpl(f);
	}

	static void onFilterPreprocess(MSFilter *f) {
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->preprocess();
	}

	static void onFilterPostprocess(MSFilter *f) {
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->postprocess();
	}

	static void onFilterUninit(MSFilter *f) {
		delete static_cast<MediaCodecH264EncoderFilterImpl *>(f->data);
	}

	static void onFilterProcess(MSFilter *f) {
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->process();
	}

	static int onGetBitrateCall(MSFilter *f, void *arg) {
		int *bitrate = static_cast<int *>(arg);
		*bitrate = static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->getBitrate();
		return 0;
	}

	static int onSetConfigurationCall(MSFilter *f, void *arg) {
		const MSVideoConfiguration *vconf = static_cast<MSVideoConfiguration *>(arg);
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->setVideoConfiguration(vconf);
		return 0;
	}

	static int onSetBitrateCall(MSFilter *f, void *arg) {
		int br = *static_cast<int *>(arg);
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->setBitrate(br);
		return 0;
	}

	static int onSetFpsCall(MSFilter *f, void *arg) {
		float fps = *static_cast<float *>(arg);
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->setFps(fps);
		return 0;
	}

	static int onGetFpsCall(MSFilter *f, void *arg) {
		float *fps = static_cast<float *>(arg);
		*fps = static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->getFps();
		return 0;
	}

	static int onGetVideoSizeCall(MSFilter *f, void *arg) {
		MSVideoSize *vsize = static_cast<MSVideoSize *>(arg);
		*vsize = static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->getVideoSize();
		return 0;
	}

	static int onEnableAvpfCall(MSFilter *f, void *data) {
		bool_t enable = *static_cast<bool_t *>(data);
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->enableAvpf(enable);
		return 0;
	}

	static int onSetVideoSizeCall(MSFilter *f, void *arg) {
		const MSVideoSize *vsize = static_cast<MSVideoSize *>(arg);
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->setVideoSize(*vsize);
		return 0;
	}

	static int onNotifyPliCall(MSFilter *f, void *data) {
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->notifyPli();
		return 0;
	}

	static int onNotifyFirCall(MSFilter *f, void *data) {
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->notifyFir();
		return 0;
	}

	static int onGetVideoConfigurationsCall(MSFilter *f, void *data) {
		const MSVideoConfiguration **vconfs = static_cast<const MSVideoConfiguration **>(data);
		*vconfs = static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->getVideoConfiguratons();
		return 0;
	}

	static int onSetVideoConfigurationsCall(MSFilter *f, void *data) {
154 155
		const MSVideoConfiguration * const *vconfs = static_cast<const MSVideoConfiguration * const *>(data);
		static_cast<MediaCodecH264EncoderFilterImpl *>(f->data)->setVideoConfigurations(*vconfs);
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
		return 0;
	}
};

} // mamespace mediastreamer

using namespace mediastreamer;

static MSFilterMethod  mediacodec_h264_enc_methods[] = {
	{ MS_FILTER_SET_FPS                       , MediaCodecH264EncoderFilterImpl::onSetFpsCall                 },
	{ MS_FILTER_SET_BITRATE                   , MediaCodecH264EncoderFilterImpl::onSetBitrateCall             },
	{ MS_FILTER_GET_BITRATE                   , MediaCodecH264EncoderFilterImpl::onGetBitrateCall             },
	{ MS_FILTER_GET_FPS                       , MediaCodecH264EncoderFilterImpl::onGetFpsCall                 },
	{ MS_FILTER_GET_VIDEO_SIZE                , MediaCodecH264EncoderFilterImpl::onGetVideoSizeCall           },
	{ MS_VIDEO_ENCODER_NOTIFY_PLI             , MediaCodecH264EncoderFilterImpl::onNotifyPliCall              },
	{ MS_VIDEO_ENCODER_NOTIFY_FIR             , MediaCodecH264EncoderFilterImpl::onNotifyFirCall              },
	{ MS_FILTER_SET_VIDEO_SIZE                , MediaCodecH264EncoderFilterImpl::onSetVideoSizeCall           },
	{ MS_VIDEO_ENCODER_ENABLE_AVPF            , MediaCodecH264EncoderFilterImpl::onEnableAvpfCall             },
	{ MS_VIDEO_ENCODER_GET_CONFIGURATION_LIST , MediaCodecH264EncoderFilterImpl::onGetVideoConfigurationsCall },
	{ MS_VIDEO_ENCODER_SET_CONFIGURATION_LIST , MediaCodecH264EncoderFilterImpl::onSetVideoConfigurationsCall },
	{ MS_VIDEO_ENCODER_SET_CONFIGURATION      , MediaCodecH264EncoderFilterImpl::onSetConfigurationCall       },
	{ 0                                       , nullptr                                                       }
};


extern "C" MSFilterDesc ms_mediacodec_h264_enc_desc = {
	.id = MS_MEDIACODEC_H264_ENC_ID,
	.name = "MSMediaCodecH264Enc",
	.text = "A H264 encoder based on MediaCodec API.",
	.category = MS_FILTER_ENCODER,
	.enc_fmt = "H264",
	.ninputs = 1,
	.noutputs = 1,
	.init = MediaCodecH264EncoderFilterImpl::onFilterInit,
	.preprocess = MediaCodecH264EncoderFilterImpl::onFilterPreprocess,
	.process = MediaCodecH264EncoderFilterImpl::onFilterProcess,
	.postprocess = MediaCodecH264EncoderFilterImpl::onFilterPostprocess,
	.uninit = MediaCodecH264EncoderFilterImpl::onFilterUninit,
	.methods = mediacodec_h264_enc_methods,
	.flags = MS_FILTER_IS_PUMP
};