msopenh264.cpp 10.7 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
H.264 encoder/decoder plugin for mediastreamer2 based on the openh264 library.
Copyright (C) 2006-2012 Belledonne Communications, Grenoble

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

20

Ghislain MARY's avatar
Ghislain MARY committed
21 22 23
#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msvideo.h"

24 25
#include "msopenh264dec.h"
#include "msopenh264enc.h"
Ghislain MARY's avatar
Ghislain MARY committed
26 27 28 29 30 31 32 33 34 35 36

#ifndef VERSION
#define VERSION "0.1.0"
#endif


/******************************************************************************
 * Implementation of the decoder                                              *
 *****************************************************************************/

static void msopenh264_dec_init(MSFilter *f) {
37
	MSOpenH264Decoder *d = new MSOpenH264Decoder();
Ghislain MARY's avatar
Ghislain MARY committed
38 39 40 41
	f->data = d;
}

static void msopenh264_dec_preprocess(MSFilter *f) {
42 43
	MSOpenH264Decoder *d = static_cast<MSOpenH264Decoder *>(f->data);
	d->initialize();
Ghislain MARY's avatar
Ghislain MARY committed
44 45 46
}

static void msopenh264_dec_process(MSFilter *f) {
47 48 49 50 51 52 53
	MSOpenH264Decoder *d = static_cast<MSOpenH264Decoder *>(f->data);
	d->feed(f);
}

static void msopenh264_dec_postprocess(MSFilter *f) {
	MSOpenH264Decoder *d = static_cast<MSOpenH264Decoder *>(f->data);
	d->uninitialize();
Ghislain MARY's avatar
Ghislain MARY committed
54 55 56
}

static void msopenh264_dec_uninit(MSFilter *f) {
57 58
	MSOpenH264Decoder *d = static_cast<MSOpenH264Decoder *>(f->data);
	delete d;
Ghislain MARY's avatar
Ghislain MARY committed
59 60 61 62 63 64 65 66
}


/******************************************************************************
 * Methods to configure the decoder                                           *
 *****************************************************************************/

static int msopenh264_reset_first_image(MSFilter *f, void *data) {
67 68
	//MSOpenH264Decoder *d = static_cast<MSOpenH264Decoder *>(f->data);
	//d->first_image_decoded = FALSE;
Ghislain MARY's avatar
Ghislain MARY committed
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
	return 0;
}

static MSFilterMethod msopenh264_dec_methods[] = {
	{ MS_VIDEO_DECODER_RESET_FIRST_IMAGE_NOTIFICATION, msopenh264_reset_first_image },
	{ 0,                                               NULL                         }
};

/******************************************************************************
 * Definition of the decoder                                                  *
 *****************************************************************************/

#define MSOPENH264_DEC_NAME        "MSOpenH264Dec"
#define MSOPENH264_DEC_DESCRIPTION "A H.264 decoder based on the openh264 library"
#define MSOPENH264_DEC_CATEGORY    MS_FILTER_DECODER
#define MSOPENH264_DEC_ENC_FMT     "H264"
#define MSOPENH264_DEC_NINPUTS     1
#define MSOPENH264_DEC_NOUTPUTS    1
#define MSOPENH264_DEC_FLAGS       0

#ifndef _MSC_VER

MSFilterDesc msopenh264_dec_desc = {
	.id = MS_FILTER_PLUGIN_ID,
	.name = MSOPENH264_DEC_NAME,
	.text = MSOPENH264_DEC_DESCRIPTION,
	.category = MSOPENH264_DEC_CATEGORY,
	.enc_fmt = MSOPENH264_DEC_ENC_FMT,
	.ninputs = MSOPENH264_DEC_NINPUTS,
	.noutputs = MSOPENH264_DEC_NOUTPUTS,
	.init = msopenh264_dec_init,
	.preprocess = msopenh264_dec_preprocess,
	.process = msopenh264_dec_process,
	.postprocess = NULL,
	.uninit = msopenh264_dec_uninit,
	.methods = msopenh264_dec_methods,
	.flags = MSOPENH264_DEC_FLAGS
};

#else

MSFilterDesc msopenh264_dec_desc = {
	MS_FILTER_PLUGIN_ID,
	MSOPENH264_DEC_NAME,
	MSOPENH264_DEC_DESCRIPTION,
	MSOPENH264_DEC_CATEGORY,
	MSOPENH264_DEC_ENC_FMT,
	MSOPENH264_DEC_NINPUTS,
	MSOPENH264_DEC_NOUTPUTS,
	msopenh264_dec_init,
	msopenh264_dec_preprocess,
	msopenh264_dec_process,
	NULL,
	msopenh264_dec_uninit,
	msopenh264_dec_methods,
	MSOPENH264_DEC_FLAGS
};

#endif


/******************************************************************************
 * Implementation of the encoder                                              *
 *****************************************************************************/

static void msopenh264_enc_init(MSFilter *f) {
135 136
	MSOpenH264Encoder *e = new MSOpenH264Encoder();
	f->data = e;
Ghislain MARY's avatar
Ghislain MARY committed
137 138 139
}

static void msopenh264_enc_preprocess(MSFilter *f) {
140 141
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	e->initialize();
Ghislain MARY's avatar
Ghislain MARY committed
142 143 144
}

static void msopenh264_enc_process(MSFilter *f) {
145 146
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	e->feed(f);
Ghislain MARY's avatar
Ghislain MARY committed
147 148 149
}

static void msopenh264_enc_postprocess(MSFilter *f) {
150 151
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	e->uninitialize();
Ghislain MARY's avatar
Ghislain MARY committed
152 153 154
}

static void msopenh264_enc_uninit(MSFilter *f) {
155 156
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	delete e;
Ghislain MARY's avatar
Ghislain MARY committed
157 158 159 160 161 162 163 164
}


/******************************************************************************
 * Methods to configure the encoder                                           *
 *****************************************************************************/

static int msopenh264_enc_set_fps(MSFilter *f, void *arg) {
165 166 167
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	float *fps = static_cast<float *>(arg);
	e->setFps(*fps);
Ghislain MARY's avatar
Ghislain MARY committed
168 169 170 171
	return 0;
}

static int msopenh264_enc_get_fps(MSFilter *f, void *arg) {
172 173 174
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	float *fps = static_cast<float *>(arg);
	*fps = e->getFps();
Ghislain MARY's avatar
Ghislain MARY committed
175 176 177 178
	return 0;
}

static int msopenh264_enc_set_bitrate(MSFilter *f, void *arg) {
179 180 181
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	int *bitrate = static_cast<int *>(arg);
	e->setBitrate(*bitrate);
Ghislain MARY's avatar
Ghislain MARY committed
182 183 184
	return 0;
}

185 186 187 188
static int msopenh264_enc_get_bitrate(MSFilter *f, void *arg) {
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	int *bitrate = static_cast<int *>(arg);
	*bitrate = e->getBitrate();
Ghislain MARY's avatar
Ghislain MARY committed
189 190 191 192
	return 0;
}

static int msopenh264_enc_set_vsize(MSFilter *f, void *arg) {
193 194 195
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	MSVideoSize *vsize = static_cast<MSVideoSize *>(arg);
	e->setSize(*vsize);
Ghislain MARY's avatar
Ghislain MARY committed
196 197 198 199
	return 0;
}

static int msopenh264_enc_get_vsize(MSFilter *f, void *arg) {
200 201 202
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	MSVideoSize *vsize = static_cast<MSVideoSize *>(arg);
	*vsize = e->getSize();
Ghislain MARY's avatar
Ghislain MARY committed
203 204 205 206
	return 0;
}

static int msopenh264_enc_set_pix_fmt(MSFilter *f, void *arg) {
207 208 209
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	MSPixFmt *fmt = static_cast<MSPixFmt *>(arg);
	e->setPixFormat(*fmt);
Ghislain MARY's avatar
Ghislain MARY committed
210 211 212 213
	return 0;
}

static int msopenh264_enc_add_fmtp(MSFilter *f, void *arg) {
214 215 216
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	const char *fmtp = static_cast<const char *>(arg);
	e->addFmtp(fmtp);
Ghislain MARY's avatar
Ghislain MARY committed
217 218 219 220
	return 0;
}

static int msopenh264_enc_has_builtin_converter(MSFilter *f, void *arg) {
221 222
	bool_t *conv = static_cast<bool_t *>(arg);
	*conv = FALSE;
Ghislain MARY's avatar
Ghislain MARY committed
223 224 225 226
	return 0;
}

static int msopenh264_enc_req_vfu(MSFilter *f, void *arg) {
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	e->generateKeyframe();
	return 0;
}

static int msopenh264_enc_get_configuration_list(MSFilter *f, void *arg) {
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	const MSVideoConfiguration **vconf_list = static_cast<const MSVideoConfiguration **>(arg);
	*vconf_list = e->getConfigurationList();
	return 0;
}

static int msopenh264_enc_set_configuration(MSFilter *f, void *arg) {
	MSOpenH264Encoder *e = static_cast<MSOpenH264Encoder *>(f->data);
	MSVideoConfiguration *vconf = static_cast<MSVideoConfiguration *>(arg);
	e->setConfiguration(*vconf);
Ghislain MARY's avatar
Ghislain MARY committed
243 244 245 246
	return 0;
}

static MSFilterMethod msopenh264_enc_methods[] = {
247 248 249 250 251 252 253 254 255 256
	{ MS_FILTER_SET_FPS,                       msopenh264_enc_set_fps                },
	{ MS_FILTER_GET_FPS,                       msopenh264_enc_get_fps                },
	{ MS_FILTER_SET_BITRATE,                   msopenh264_enc_set_bitrate            },
	{ MS_FILTER_GET_BITRATE,                   msopenh264_enc_get_bitrate            },
	{ MS_FILTER_SET_VIDEO_SIZE,                msopenh264_enc_set_vsize              },
	{ MS_FILTER_GET_VIDEO_SIZE,                msopenh264_enc_get_vsize              },
	{ MS_FILTER_SET_PIX_FMT,                   msopenh264_enc_set_pix_fmt            },
	{ MS_FILTER_ADD_FMTP,                      msopenh264_enc_add_fmtp               },
	{ MS_VIDEO_ENCODER_HAS_BUILTIN_CONVERTER,  msopenh264_enc_has_builtin_converter  },
	{ MS_FILTER_REQ_VFU,                       msopenh264_enc_req_vfu                },
Ghislain MARY's avatar
Ghislain MARY committed
257
#ifdef MS_VIDEO_ENCODER_REQ_VFU
258
	{ MS_VIDEO_ENCODER_REQ_VFU,                msopenh264_enc_req_vfu                },
Ghislain MARY's avatar
Ghislain MARY committed
259
#endif
260 261 262
	{ MS_VIDEO_ENCODER_GET_CONFIGURATION_LIST, msopenh264_enc_get_configuration_list },
	{ MS_VIDEO_ENCODER_SET_CONFIGURATION,      msopenh264_enc_set_configuration      },
	{ 0,                                       NULL                                  }
Ghislain MARY's avatar
Ghislain MARY committed
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
};

/******************************************************************************
 * Definition of the encoder                                                  *
 *****************************************************************************/

#define MSOPENH264_ENC_NAME        "MSOpenH264Enc"
#define MSOPENH264_ENC_DESCRIPTION "A H.264 encoder based on the openh264 library"
#define MSOPENH264_ENC_CATEGORY    MS_FILTER_ENCODER
#define MSOPENH264_ENC_ENC_FMT     "H264"
#define MSOPENH264_ENC_NINPUTS     1
#define MSOPENH264_ENC_NOUTPUTS    1
#define MSOPENH264_ENC_FLAGS       0

#ifndef _MSC_VER

MSFilterDesc msopenh264_enc_desc = {
	.id = MS_FILTER_PLUGIN_ID,
	.name = MSOPENH264_ENC_NAME,
	.text = MSOPENH264_ENC_DESCRIPTION,
	.category = MSOPENH264_ENC_CATEGORY,
	.enc_fmt = MSOPENH264_ENC_ENC_FMT,
	.ninputs = MSOPENH264_ENC_NINPUTS,
	.noutputs = MSOPENH264_ENC_NOUTPUTS,
	.init = msopenh264_enc_init,
	.preprocess = msopenh264_enc_preprocess,
	.process = msopenh264_enc_process,
	.postprocess = msopenh264_enc_postprocess,
	.uninit = msopenh264_enc_uninit,
	.methods = msopenh264_enc_methods,
	.flags = MSOPENH264_ENC_FLAGS
};

#else

MSFilterDesc msopenh264_enc_desc = {
	MS_FILTER_PLUGIN_ID,
	MSOPENH264_ENC_NAME,
	MSOPENH264_ENC_DESCRIPTION,
	MSOPENH264_ENC_CATEGORY,
	MSOPENH264_ENC_ENC_FMT,
	MSOPENH264_ENC_NINPUTS,
	MSOPENH264_ENC_NOUTPUTS,
	msopenh264_enc_init,
	msopenh264_enc_preprocess,
	msopenh264_enc_process,
	msopenh264_enc_postprocess,
	msopenh264_enc_uninit,
	msopenh264_enc_methods,
	MSOPENH264_ENC_FLAGS
};

#endif


318 319 320 321 322 323 324
#ifdef _MSC_VER
#define MS_PLUGIN_DECLARE(type) extern "C" __declspec(dllexport) type
#else
#define MS_PLUGIN_DECLARE(type) extern "C" type
#endif

MS_PLUGIN_DECLARE(void) libmsopenh264_init(void){
Ghislain MARY's avatar
Ghislain MARY committed
325 326 327 328
	ms_filter_register(&msopenh264_dec_desc);
	ms_filter_register(&msopenh264_enc_desc);
	ms_message("msopenh264-" VERSION " plugin registered.");
}