mkv.c 86.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
mediastreamer2 library - modular sound and video processing and streaming
Copyright (C) 2006  Simon MORLAT (simon.morlat@linphone.org)

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
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 19
*/

20 21 22 23 24
#define bool_t matroska_bool_t
#include <matroska/matroska.h>
#include <matroska/matroska_sem.h>
#undef bool_t

25 26 27 28
#define bool_t ms_bool_t
#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/msvideo.h"
#include "mediastreamer2/rfc3984.h"
29
#include "mediastreamer2/msticker.h"
30
#include "waveheader.h"
31
#include "mediastreamer2/formats.h"
32
#include "vp8rtpfmt.h"
33
#include "mkv_reader.h"
34 35 36 37
#undef bool_t

#define bool_t ambigous use ms_bool_t or matroska_bool_t

38 39
static int recorder_close(MSFilter *f, void *arg);

Simon Morlat's avatar
Simon Morlat committed
40

41 42 43
/*********************************************************************************************
 * Module interface                                                                          *
 *********************************************************************************************/
44
typedef void *(*ModuleNewFunc)(MSFactory *factory);
François Grisez's avatar
François Grisez committed
45
typedef void (*ModuleFreeFunc)(void *obj);
46
typedef void (*ModuleSetFunc)(void *obj, const MSFmtDescriptor *fmt);
47
typedef int (*ModulePreProFunc)(void *obj, MSQueue *input, MSQueue *output);
48
typedef mblk_t *(*ModuleProFunc)(void *obj, mblk_t *buffer, ms_bool_t *isKeyFrame, ms_bool_t *isVisible, uint8_t **codecPrivateData, size_t *codecPrivateSize);
49
typedef void (*ModuleReverseFunc)(MSFactory* factory, void *obj, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize);
50
typedef void (*ModulePrivateDataFunc)(const void *obj, uint8_t **data, size_t *data_size);
51
typedef void (*ModulePrivateDataLoadFunc)(void *obj, const uint8_t *data, size_t size);
52
typedef ms_bool_t (*ModuleIsKeyFrameFunc)(const mblk_t *frame);
53

François Grisez's avatar
François Grisez committed
54
typedef struct {
55 56
	const char *rfcName;
	const char *codecId;
François Grisez's avatar
François Grisez committed
57 58
	ModuleNewFunc new_module;
	ModuleFreeFunc free_module;
59 60 61
	ModuleSetFunc set;
	ModulePreProFunc preprocess;
	ModuleProFunc process;
62
	ModuleReverseFunc reverse;
63 64 65
	ModulePrivateDataFunc get_private_data;
	ModulePrivateDataLoadFunc load_private_data;
	ModuleIsKeyFrameFunc is_key_frame;
66 67 68 69 70 71
} ModuleDesc;

/*********************************************************************************************
 * h264 module                                                                               *
 *********************************************************************************************/
/* H264Private */
François Grisez's avatar
François Grisez committed
72
typedef struct {
73 74 75
	uint8_t profile;
	uint8_t level;
	uint8_t NALULenghtSizeMinusOne;
76 77
	bctbx_list_t *sps_list;
	bctbx_list_t *pps_list;
78 79
} H264Private;

80 81
static void _ms_list_append_copy(const mblk_t *buffer, bctbx_list_t **list) {
	*list = bctbx_list_append(*list, copymsg(buffer));
82 83
}

84
static void H264Private_init(H264Private *obj, const bctbx_list_t *spsList, const bctbx_list_t *ppsList) {
85
	memset(obj, 0, sizeof(H264Private));
86
	obj->NALULenghtSizeMinusOne = 0xFF;
87 88
	bctbx_list_for_each2(spsList, (MSIterate2Func)_ms_list_append_copy, &obj->sps_list);
	bctbx_list_for_each2(ppsList, (MSIterate2Func)_ms_list_append_copy, &obj->pps_list);
89
	if(obj->sps_list != NULL) {
90
		const mblk_t *firstSPS = (const mblk_t *)bctbx_list_nth_data(obj->sps_list, 0);
91 92 93
		obj->profile = firstSPS->b_rptr[1];
		obj->level = firstSPS->b_rptr[3];
	}
94 95
}

96
static void H264Private_uninit(H264Private *obj) {
97 98
	if(obj->sps_list != NULL) bctbx_list_free_with_data(obj->sps_list, (void ( *)(void *))freemsg);
	if(obj->pps_list != NULL) bctbx_list_free_with_data(obj->pps_list, (void ( *)(void *))freemsg);
99 100
}

101
static H264Private *H264Private_new(const bctbx_list_t *spsList, const bctbx_list_t *ppsList) {
102
	H264Private *obj = (H264Private *)ms_new0(H264Private, 1);
103 104
	H264Private_init(obj, spsList, ppsList);
	return obj;
105 106
}

107 108 109
static void H264Private_free(H264Private *obj) {
	H264Private_uninit(obj);
	ms_free(obj);
110 111
}

112
static const bctbx_list_t *H264Private_getSPS(const H264Private *obj) {
113
	return obj->sps_list;
114 115
}

116
static const bctbx_list_t *H264Private_getPPS(const H264Private *obj) {
117
	return obj->pps_list;
118 119
}

François Grisez's avatar
François Grisez committed
120
static void H264Private_serialize(const H264Private *obj, uint8_t **data, size_t *size) {
121
	uint8_t nbSPS, nbPPS;
122
	bctbx_list_t *it = NULL;
123
	uint8_t *result = NULL;
Ghislain MARY's avatar
Ghislain MARY committed
124
	size_t i;
125

126 127
	nbSPS = (uint8_t)bctbx_list_size(obj->sps_list);
	nbPPS = (uint8_t)bctbx_list_size(obj->pps_list);
128

129
	*size = 7;
130 131
	*size += (nbSPS + nbPPS) * 2;

132 133
	for(it = obj->sps_list; it != NULL; it = it->next) {
		mblk_t *buff = (mblk_t *)(it->data);
134 135
		*size += msgdsize(buff);
	}
136 137
	for(it = obj->pps_list; it != NULL; it = it->next) {
		mblk_t *buff = (mblk_t *)(it->data);
138 139 140
		*size += msgdsize(buff);
	}

141
	result = (uint8_t *)ms_new0(uint8_t, *size);
142 143 144 145 146 147
	result[0] = 0x01;
	result[1] = obj->profile;
	result[3] = obj->level;
	result[4] = obj->NALULenghtSizeMinusOne;
	result[5] = nbSPS & 0x1F;

148 149 150
	i = 6;
	for(it = obj->sps_list; it != NULL; it = it->next) {
		mblk_t *buff = (mblk_t *)(it->data);
151
		size_t buff_size = msgdsize(buff);
152
		uint16_t buff_size_be = htons((uint16_t)buff_size);
153
		memcpy(&result[i], &buff_size_be, sizeof(buff_size_be));
154
		i += sizeof(buff_size_be);
155 156 157 158 159 160 161
		memcpy(&result[i], buff->b_rptr, buff_size);
		i += buff_size;
	}

	result[i] = nbPPS;
	i++;

162 163
	for(it = obj->pps_list; it != NULL; it = it->next) {
		mblk_t *buff = (mblk_t *)(it->data);
Ghislain MARY's avatar
Ghislain MARY committed
164
		size_t buff_size = msgdsize(buff);
165
		uint16_t buff_size_be = htons((uint16_t)buff_size);
166
		memcpy(&result[i], &buff_size_be, sizeof(buff_size_be));
167
		i += sizeof(buff_size_be);
168 169 170 171
		memcpy(&result[i], buff->b_rptr, buff_size);
		i += buff_size;
	}
	*data = result;
172 173
}

François Grisez's avatar
François Grisez committed
174
static void H264Private_load(H264Private *obj, const uint8_t *data) {
175 176 177 178
	int i, N;
	const uint8_t *r_ptr = NULL;
	uint16_t nalu_size;
	mblk_t *nalu = NULL;
179

180 181
	H264Private_uninit(obj);
	H264Private_init(obj, NULL, NULL);
182

183 184
	N = data[5] & 0x1F;
	r_ptr = data + 6;
185 186 187
	for(i = 0; i < N; i++) {
		memcpy(&nalu_size, r_ptr, sizeof(uint16_t));
		r_ptr += sizeof(uint16_t);
188
		nalu_size = ntohs(nalu_size);
189
		nalu = allocb(nalu_size, 0);
190 191 192
		memcpy(nalu->b_wptr, r_ptr, nalu_size);
		nalu->b_wptr += nalu_size;
		r_ptr += nalu_size;
193
		obj->sps_list = bctbx_list_append(obj->sps_list, nalu);
194 195
	}

196 197 198 199 200
	N = *r_ptr;
	r_ptr += 1;
	for(i = 0; i < N; i++) {
		memcpy(&nalu_size, r_ptr, sizeof(uint16_t));
		r_ptr += sizeof(uint16_t);
201
		nalu_size = ntohs(nalu_size);
202
		nalu = allocb(nalu_size, 0);
203 204 205
		memcpy(nalu->b_wptr, r_ptr, nalu_size);
		nalu->b_wptr += nalu_size;
		r_ptr += nalu_size;
206
		obj->pps_list = bctbx_list_append(obj->pps_list, nalu);
207
	}
208

209
	if(obj->sps_list != NULL) {
210
		const mblk_t *firstSPS = (const mblk_t *)bctbx_list_nth_data(obj->sps_list, 0);
211 212 213
		obj->profile = firstSPS->b_rptr[1];
		obj->level = firstSPS->b_rptr[3];
	}
214 215 216
}

/* h264 module */
217
typedef struct {
218
	Rfc3984Context rfc3984Context;
219
	H264Private *codecPrivate;
220 221
} H264Module;

222
static void *h264_module_new(MSFactory *factory) {
223
	H264Module *mod = ms_new0(H264Module, 1);
224 225
	rfc3984_init(&mod->rfc3984Context);
	mod->rfc3984Context.maxsz = ms_factory_get_payload_max_size(factory);
226
	rfc3984_set_mode(&mod->rfc3984Context, 1);
François Grisez's avatar
François Grisez committed
227
	return mod;
228 229
}

230
static void h264_module_free(void *data) {
231 232
	H264Module *obj = (H264Module *)data;
	rfc3984_uninit(&obj->rfc3984Context);
233 234 235
	if(obj->codecPrivate != NULL) {
		H264Private_free(obj->codecPrivate);
	}
236
	ms_free(obj);
237 238
}

239
static int h264_module_preprocessing(void *data, MSQueue *input, MSQueue *output) {
240 241
	H264Module *obj = (H264Module *)data;
	MSQueue queue;
242
	mblk_t *inputBuffer = NULL;
243 244

	ms_queue_init(&queue);
245
	while((inputBuffer = ms_queue_get(input)) != NULL) {
246
		rfc3984_unpack(&obj->rfc3984Context, inputBuffer, &queue);
247
		if(!ms_queue_empty(&queue)) {
248 249
			mblk_t *frame = ms_queue_get(&queue);
			mblk_t *end = frame;
250
			while(!ms_queue_empty(&queue)) {
251 252 253 254 255
				end = concatb(end, ms_queue_get(&queue));
			}
			ms_queue_put(output, frame);
		}
	}
256 257

	return 0;
258 259
}

260
static int h264_nalu_type(const mblk_t *nalu) {
261
	return (nalu->b_rptr[0]) & ((1 << 5) - 1);
262 263
}

264
static ms_bool_t h264_is_key_frame(const mblk_t *frame) {
265
	const mblk_t *curNalu = NULL;
266 267
	for(curNalu = frame; curNalu != NULL && h264_nalu_type(curNalu) != 5; curNalu = curNalu->b_cont);
	return curNalu != NULL;
268 269
}

270
static void nalus_to_frame(mblk_t *buffer, mblk_t **frame, bctbx_list_t **spsList, bctbx_list_t **ppsList, ms_bool_t *isKeyFrame) {
271
	mblk_t *curNalu = NULL;
272
	uint32_t timecode = mblk_get_timestamp_info(buffer);
273 274
	*frame = NULL;
	*isKeyFrame = FALSE;
275

276

277
	for(curNalu = buffer; curNalu != NULL;) {
278
		mblk_t *buff = curNalu;
279
		int type = h264_nalu_type(buff);
280

281 282
		curNalu = curNalu->b_cont;
		buff->b_cont = NULL;
283
		switch(type) {
284
			case 7:
285
				*spsList = bctbx_list_append(*spsList, buff);
286
				break;
287

288
			case 8:
289
				*ppsList = bctbx_list_append(*ppsList, buff);
290
				break;
291

292
			default:
293
			{
Ghislain MARY's avatar
Ghislain MARY committed
294
				uint32_t bufferSize = htonl((uint32_t)msgdsize(buff));
295
				mblk_t *size = allocb(4, 0);
296

297 298 299 300 301 302 303 304 305 306 307 308 309
				if(type == 5) {
					*isKeyFrame = TRUE;
				}
				memcpy(size->b_wptr, &bufferSize, sizeof(bufferSize));
				size->b_wptr = size->b_wptr + sizeof(bufferSize);
				concatb(size, buff);
				buff = size;

				if(*frame == NULL) {
					*frame = buff;
				} else {
					concatb(*frame, buff);
				}
310
			}
311 312 313
		}
	}

314
	if(*frame != NULL) {
315
		msgpullup(*frame, (size_t)-1);
316 317
		mblk_set_timestamp_info(*frame, timecode);
	}
318 319
}

320
static mblk_t *h264_module_processing(void *data, mblk_t *nalus, ms_bool_t *isKeyFrame, ms_bool_t *isVisible, uint8_t **codecPrivateData, size_t *codecPrivateSize) {
321
	H264Module *obj = (H264Module *)data;
322
	mblk_t *frame = NULL;
323
	bctbx_list_t *spsList = NULL, *ppsList = NULL;
324 325 326
	nalus_to_frame(nalus, &frame, &spsList, &ppsList, isKeyFrame);
	if(spsList != NULL || ppsList != NULL) {
		H264Private *codecPrivateStruct = H264Private_new(spsList, ppsList);
327
		ms_message("MKVRecorder: Received H264 SPS [%p] or PPS [%p]", spsList, ppsList);
328 329 330 331 332 333
		if(obj->codecPrivate == NULL) {
			obj->codecPrivate = codecPrivateStruct;
		} else {
			H264Private_serialize(codecPrivateStruct, codecPrivateData, codecPrivateSize);
			H264Private_free(codecPrivateStruct);
		}
334 335
		if(spsList != NULL) bctbx_list_free_with_data(spsList, (void ( *)(void *))freemsg);
		if(ppsList != NULL) bctbx_list_free_with_data(ppsList, (void ( *)(void *))freemsg);
336
	}
337
	*isVisible = TRUE;
338
	return frame;
339 340
}

341
static void h264_module_reverse(MSFactory* factory, void *data, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize) {
342
	mblk_t *buffer = NULL, *bufferFrag = NULL;
343 344
	H264Module *obj = (H264Module *)data;
	MSQueue queue;
345
	const bctbx_list_t *it = NULL;
346 347
	H264Private *codecPrivate = NULL, *selectedCodecPrivate = NULL;

348 349 350 351
	ms_queue_init(&queue);
	while(input->b_rptr != input->b_wptr) {
		uint32_t naluSize;
		mblk_t *nalu;
352 353
		memcpy(&naluSize, input->b_rptr, sizeof(uint32_t));
		input->b_rptr += sizeof(uint32_t);
354 355
		naluSize = ntohl(naluSize);
		nalu = allocb(naluSize, 0);
356 357 358
		memcpy(nalu->b_wptr, input->b_rptr, naluSize);
		nalu->b_wptr += naluSize;
		input->b_rptr += naluSize;
359 360 361 362 363 364
		if(buffer == NULL) {
			buffer = nalu;
		} else {
			concatb(buffer, nalu);
		}
	}
365 366 367 368 369 370 371 372 373 374 375
	if(isFirstFrame) {
		selectedCodecPrivate = obj->codecPrivate;
	} else if(codecPrivateData != NULL) {
		codecPrivate = H264Private_new(NULL, NULL);
		H264Private_load(codecPrivate, codecPrivateData);
		selectedCodecPrivate = codecPrivate;
	}
	if(selectedCodecPrivate != NULL) {
		for(it = H264Private_getSPS(selectedCodecPrivate); it != NULL; it = it->next) {
			mblk_t *sps = copymsg((mblk_t *)it->data);
			ms_queue_put(&queue, sps);
376
			ms_message("MKVPlayer: send SPS");
377 378 379 380
		}
		for(it = H264Private_getPPS(selectedCodecPrivate); it != NULL; it = it->next) {
			mblk_t *pps = copymsg((mblk_t *)it->data);
			ms_queue_put(&queue, pps);
381
			ms_message("MKVPlayer: send PPS");
382
		}
383
	}
384 385 386

	if(codecPrivate != NULL) H264Private_free(codecPrivate);

387 388 389 390 391 392 393
	for(bufferFrag = buffer; bufferFrag != NULL;) {
		mblk_t *curBuff = bufferFrag;
		bufferFrag = bufferFrag->b_cont;
		curBuff->b_cont = NULL;
		ms_queue_put(&queue, curBuff);
	}
	rfc3984_pack(&obj->rfc3984Context, &queue, output, mblk_get_timestamp_info(input));
394
	freemsg(input);
395 396 397
}

static void h264_module_get_private_data(const void *o, uint8_t **data, size_t *data_size) {
398
	const H264Module *obj = (const H264Module *)o;
399 400 401 402 403
	if (obj->codecPrivate == NULL) {
		ms_warning("MKVRecorder: No H264 private data available");
	} else {
		H264Private_serialize(obj->codecPrivate, data, data_size);
	}
404 405
}

406
static void h264_module_load_private_data(void *o, const uint8_t *data, size_t size) {
407
	H264Module *obj = (H264Module *)o;
408 409
	obj->codecPrivate = H264Private_new(NULL, NULL);
	H264Private_load(obj->codecPrivate, data);
410 411
}

412
/* h264 module description */
413
#ifdef _MSC_VER
414
static const ModuleDesc h264_module_desc = {
415 416
	"H264",
	"V_MPEG4/ISO/AVC",
François Grisez's avatar
François Grisez committed
417 418
	h264_module_new,
	h264_module_free,
419 420 421
	NULL,
	h264_module_preprocessing,
	h264_module_processing,
422
	h264_module_reverse,
423 424 425
	h264_module_get_private_data,
	h264_module_load_private_data,
	h264_is_key_frame
426
};
427
#else
428
static const ModuleDesc h264_module_desc = {
429 430 431 432 433 434 435 436 437 438 439 440 441
	.rfcName = "H264",
	.codecId = "V_MPEG4/ISO/AVC",
	.new_module = h264_module_new,
	.free_module = h264_module_free,
	.set = NULL,
	.preprocess = h264_module_preprocessing,
	.process = h264_module_processing,
	.reverse = h264_module_reverse,
	.get_private_data = h264_module_get_private_data,
	.load_private_data = h264_module_load_private_data,
	.is_key_frame = h264_is_key_frame
};
#endif
442

443 444 445
/*********************************************************************************************
 * VP8 module                                                                               *
 *********************************************************************************************/
Ghislain MARY's avatar
Ghislain MARY committed
446 447 448

#ifdef HAVE_VPX

449 450 451
typedef struct _Vp8Module {
	Vp8RtpFmtUnpackerCtx unpacker;
	Vp8RtpFmtPackerCtx packer;
François Grisez's avatar
François Grisez committed
452
	uint16_t cseq;
453 454
} Vp8Module;

455
static void *vp8_module_new(MSFactory *factory) {
456 457 458 459 460 461
	Vp8Module *obj = (Vp8Module *)ms_new0(Vp8Module, 1);
	vp8rtpfmt_unpacker_init(&obj->unpacker, NULL, FALSE, TRUE, FALSE);
	vp8rtpfmt_packer_init(&obj->packer);
	return obj;
}

462
static void vp8_module_free(void *obj) {
463 464 465 466 467 468
	Vp8Module *mod = (Vp8Module *)obj;
	vp8rtpfmt_unpacker_uninit(&mod->unpacker);
	vp8rtpfmt_packer_uninit(&mod->packer);
	ms_free(mod);
}

469
static int vp8_module_preprocess(void *obj, MSQueue *input, MSQueue *output) {
470 471 472
	Vp8RtpFmtUnpackerCtx *unpacker = (Vp8RtpFmtUnpackerCtx *)obj;
	Vp8RtpFmtFrameInfo infos;
	vp8rtpfmt_unpacker_feed(unpacker, input);
473
	return vp8rtpfmt_unpacker_get_frame(unpacker, output, &infos);
474 475
}

476
static mblk_t *vp8_module_process(void *obj, mblk_t *buffer, ms_bool_t *isKeyFrame, ms_bool_t *isVisible, uint8_t **codecPrivateData, size_t *codecPrivateSize) {
477 478 479
	uint8_t first_byte = *buffer->b_rptr;
	*isKeyFrame = !(first_byte & 0x01);
	*isVisible = first_byte & 0x10;
480
	return buffer;
481 482
}

483
static void vp8_module_reverse(MSFactory * f, void *obj, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize) {
484
	Vp8Module *mod = (Vp8Module *)obj;
485
	bctbx_list_t *packer_input = NULL;
François Grisez's avatar
François Grisez committed
486 487 488
	Vp8RtpFmtPacket *packet = ms_new0(Vp8RtpFmtPacket, 1);
	MSQueue q;
	mblk_t *m;
489

François Grisez's avatar
François Grisez committed
490 491 492 493 494 495 496 497 498
	ms_queue_init(&q);
	packet->m = input;
	packet->pd = ms_new0(Vp8RtpFmtPayloadDescriptor, 1);
	packet->pd->start_of_partition = TRUE;
	packet->pd->non_reference_frame = TRUE;
	packet->pd->extended_control_bits_present = FALSE;
	packet->pd->pictureid_present = FALSE;
	packet->pd->pid = 0;
	mblk_set_marker_info(packet->m, TRUE);
499
	packer_input = bctbx_list_append(packer_input, packet);
500
	vp8rtpfmt_packer_process(&mod->packer, packer_input, &q, f);
501

Simon Morlat's avatar
Simon Morlat committed
502
	while((m = ms_queue_get(&q))) {
François Grisez's avatar
François Grisez committed
503 504 505
		mblk_set_cseq(m, mod->cseq++);
		ms_queue_put(output, m);
	}
506 507
}

508
static ms_bool_t vp8_module_is_keyframe(const mblk_t *frame) {
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
	uint8_t first_byte = *frame->b_rptr;
	return !(first_byte & 0x01);
}

/* VP8 module description */
#ifdef _MSC_VER
static const ModuleDesc vp8_module_desc = {
	"VP8",
	"V_VP8",
	vp8_module_new,
	vp8_module_free,
	NULL,
	vp8_module_preprocess,
	vp8_module_process,
	vp8_module_reverse,
	NULL,
	NULL,
	vp8_module_is_keyframe
};
#else
static const ModuleDesc vp8_module_desc = {
	.rfcName = "VP8",
	.codecId = "V_VP8",
	.new_module = vp8_module_new,
	.free_module = vp8_module_free,
	.set = NULL,
	.preprocess = vp8_module_preprocess,
	.process = vp8_module_process,
	.reverse = vp8_module_reverse,
	.get_private_data = NULL,
	.load_private_data = NULL,
	.is_key_frame = vp8_module_is_keyframe
};
#endif

Ghislain MARY's avatar
Ghislain MARY committed
544 545
#endif /* HAVE_VPX */

546 547 548 549
/*********************************************************************************************
 * µLaw module                                                                               *
 *********************************************************************************************/
/* WavPrivate */
François Grisez's avatar
François Grisez committed
550
typedef struct {
551 552 553 554 555 556 557
	uint16_t wFormatTag;
	uint16_t nbChannels;
	uint32_t nSamplesPerSec;
	uint32_t nAvgBytesPerSec;
	uint16_t nBlockAlign;
	uint16_t wBitsPerSample;
	uint16_t cbSize;
558 559
} WavPrivate;

François Grisez's avatar
François Grisez committed
560
static void wav_private_set(WavPrivate *data, const MSFmtDescriptor *obj) {
561
	uint16_t bitsPerSample = 8;
562
	uint16_t nbBlockAlign = (uint16_t)((bitsPerSample * obj->nchannels) / 8);
563
	uint32_t bitrate = bitsPerSample * obj->nchannels * obj->rate;
564

565 566 567 568 569 570 571
	data->wFormatTag = le_uint16((uint16_t)7);
	data->nbChannels = le_uint16((uint16_t)obj->nchannels);
	data->nSamplesPerSec = le_uint32((uint32_t)obj->rate);
	data->nAvgBytesPerSec = le_uint32(bitrate);
	data->nBlockAlign = le_uint16((uint16_t)nbBlockAlign);
	data->wBitsPerSample = le_uint16(bitsPerSample);
	data->cbSize = 0;
572 573
}

François Grisez's avatar
François Grisez committed
574
static void wav_private_serialize(const WavPrivate *obj, uint8_t **data, size_t *size) {
François Grisez's avatar
François Grisez committed
575
	*size = sizeof(WavPrivate);
576 577
	*data = (uint8_t *)ms_new0(uint8_t, *size);
	memcpy(*data, obj, *size);
578 579
}

580
static void wav_private_load(WavPrivate *obj, const uint8_t *data) {
François Grisez's avatar
François Grisez committed
581
	memcpy(obj, data, sizeof(WavPrivate));
582 583 584
}

/* µLaw module */
585
static void *mu_law_module_new(MSFactory *factory) {
586
	return ms_new0(WavPrivate, 1);
587 588
}

François Grisez's avatar
François Grisez committed
589
static void mu_law_module_free(void *o) {
590
	ms_free(o);
591 592
}

593
static void mu_law_module_set(void *o, const MSFmtDescriptor *fmt) {
François Grisez's avatar
François Grisez committed
594
	WavPrivate *obj = (WavPrivate *)o;
595
	wav_private_set(obj, fmt);
596 597
}

François Grisez's avatar
François Grisez committed
598
static void mu_law_module_get_private_data(const void *o, uint8_t **data, size_t *data_size) {
François Grisez's avatar
François Grisez committed
599 600
	const WavPrivate *obj = (const WavPrivate *)o;
	wav_private_serialize(obj, data, data_size);
601 602
}

603
static void mu_law_module_load_private(void *o, const uint8_t *data, size_t size) {
François Grisez's avatar
François Grisez committed
604 605
	WavPrivate *obj = (WavPrivate *)o;
	wav_private_load(obj, data);
606 607
}

608
/* µLaw module description */
609
#ifdef _MSC_VER
610
static const ModuleDesc mu_law_module_desc = {
611 612
	"pcmu",
	"A_MS/ACM",
François Grisez's avatar
François Grisez committed
613 614
	mu_law_module_new,
	mu_law_module_free,
615 616 617
	mu_law_module_set,
	NULL,
	NULL,
618
	NULL,
619 620 621
	mu_law_module_get_private_data,
	mu_law_module_load_private,
	NULL
622
};
623
#else
624
static const ModuleDesc mu_law_module_desc = {
625 626 627 628 629 630 631 632 633 634 635 636 637
	.rfcName = "pcmu",
	.codecId = "A_MS/ACM",
	.new_module = mu_law_module_new,
	.free_module = mu_law_module_free,
	.set = mu_law_module_set,
	.preprocess = NULL,
	.process = NULL,
	.reverse = NULL,
	.get_private_data = mu_law_module_get_private_data,
	.load_private_data = mu_law_module_load_private,
	.is_key_frame = NULL
};
#endif
638

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
/*********************************************************************************************
 * Opus module                                                                               *
 *********************************************************************************************/
// OpusCodecPrivate
typedef struct {
	uint8_t version;
	uint8_t channelCount;
	uint16_t preSkip;
	uint32_t inputSampleRate;
	uint16_t outputGain;
	uint8_t mappingFamily;
} OpusCodecPrivate;

static void opus_codec_private_init(OpusCodecPrivate *obj) {
	memset(obj, 0, sizeof(OpusCodecPrivate));
	obj->version = 1;
	obj->preSkip = le_uint16(3840); // 80ms at 48kHz
	obj->outputGain = le_uint16(0);
}

static void opus_codec_private_set(OpusCodecPrivate *obj, int nChannels, int inputSampleRate) {
660
	obj->channelCount = (uint8_t)nChannels;
661 662 663 664 665 666 667 668
	obj->inputSampleRate = le_uint32((uint32_t)inputSampleRate);
}

static void opus_codec_private_serialize(const OpusCodecPrivate *obj, uint8_t **data, size_t *size) {
	const char signature[9] = "OpusHead";
	*size = 19;
	*data = ms_new0(uint8_t, *size);
	memcpy(*data, signature, 8);
669
	memcpy((*data) + 8, obj, 11);
670 671
}

672
static void opus_codec_private_load(OpusCodecPrivate *obj, const uint8_t *data, size_t size) {
673
	memcpy(obj, data + 8, 11);
674 675 676
}

// OpusModule
677
static void *opus_module_new(MSFactory *factory) {
678
	OpusCodecPrivate *obj = ms_new0(OpusCodecPrivate, 1);
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	opus_codec_private_init(obj);
	return obj;
}

static void opus_module_free(void *o) {
	OpusCodecPrivate *obj = (OpusCodecPrivate *)o;
	ms_free(obj);
}

static void opus_module_set(void *o, const MSFmtDescriptor *fmt) {
	OpusCodecPrivate *obj = (OpusCodecPrivate *)o;
	opus_codec_private_set(obj, fmt->nchannels, 0);
}

static void opus_module_get_private_data(const void *o, uint8_t **data, size_t *dataSize) {
	const OpusCodecPrivate *obj = (const OpusCodecPrivate *)o;
	opus_codec_private_serialize(obj, data, dataSize);
}

static void opus_module_load_private_data(void *o, const uint8_t *data, size_t size) {
	OpusCodecPrivate *obj = (OpusCodecPrivate *)o;
	opus_codec_private_load(obj, data, size);
}

#ifdef _MSC_VER
static const ModuleDesc opus_module_desc = {
	"opus",
	"A_OPUS",
	opus_module_new,
	opus_module_free,
	opus_module_set,
	NULL,
	NULL,
	NULL,
	opus_module_get_private_data,
	opus_module_load_private_data,
	NULL
};
#else
static const ModuleDesc opus_module_desc = {
	.rfcName = "opus",
	.codecId = "A_OPUS",
	.new_module = opus_module_new,
	.free_module = opus_module_free,
	.set = opus_module_set,
	.preprocess = NULL,
	.process = NULL,
	.reverse = NULL,
	.get_private_data = opus_module_get_private_data,
	.load_private_data = opus_module_load_private_data,
	.is_key_frame = NULL
};
#endif

733
/*********************************************************************************************
François Grisez's avatar
François Grisez committed
734
 * Modules list                                                                              *
735
 *********************************************************************************************/
736
#if 0
François Grisez's avatar
François Grisez committed
737
typedef enum {
738 739 740 741 742
    NONE_ID,
    H264_MOD_ID,
    VP8_MOD_ID,
    MU_LAW_MOD_ID,
    OPUS_MOD_ID
743
} ModuleId;
744
#endif
745

François Grisez's avatar
François Grisez committed
746
static const ModuleDesc *moduleDescs[] = {
747
	&h264_module_desc,
Ghislain MARY's avatar
Ghislain MARY committed
748
#ifdef HAVE_VPX
749
	&vp8_module_desc,
Ghislain MARY's avatar
Ghislain MARY committed
750
#endif /* HAVE_VPX */
751
	&mu_law_module_desc,
752
	&opus_module_desc,
753
	NULL
754 755
};

756 757 758 759
static const ModuleDesc *find_module_desc_from_rfc_name(const char *rfcName) {
	int i;
	for(i = 0; moduleDescs[i] && strcasecmp(moduleDescs[i]->rfcName, rfcName) != 0; i++);
	return moduleDescs[i];
760 761
}

762 763 764 765
static const ModuleDesc *find_module_desc_from_codec_id(const char *codecId) {
	int i;
	for(i = 0; moduleDescs[i] && strcmp(moduleDescs[i]->codecId, codecId) != 0; i++);
	return moduleDescs[i];
766 767
}

768
static const char *codec_id_to_rfc_name(const char *codecId) {
769 770
	const ModuleDesc *desc = find_module_desc_from_codec_id(codecId);
	return desc ? desc->rfcName : NULL;
771 772
}

François Grisez's avatar
François Grisez committed
773 774 775
/*********************************************************************************************
 * Module                                                                                    *
 *********************************************************************************************/
776
typedef struct {
777
	const ModuleDesc *desc;
François Grisez's avatar
François Grisez committed
778 779 780
	void *data;
} Module;

781
static Module *module_new(MSFactory *factory, const char *rfcName) {
782 783
	const ModuleDesc *desc = find_module_desc_from_rfc_name(rfcName);
	if(desc == NULL) {
François Grisez's avatar
François Grisez committed
784
		return NULL;
785 786
	} else {
		Module *module = (Module *)ms_new0(Module, 1);
787
		module->desc = desc;
788
		if(desc->new_module) module->data = desc->new_module(factory);
François Grisez's avatar
François Grisez committed
789 790 791 792
		return module;
	}
}

793
static void module_free(Module *module) {
794
	if(module->desc->free_module) module->desc->free_module(module->data);
François Grisez's avatar
François Grisez committed
795 796 797
	ms_free(module);
}

798
static void module_set(Module *module, const MSFmtDescriptor *format) {
799 800
	if(module->desc->set != NULL) {
		module->desc->set(module->data, format);
801
	}
François Grisez's avatar
François Grisez committed
802 803
}

804
static int module_preprocess(Module *module, MSQueue *input, MSQueue *output) {
805 806
	if(module->desc->preprocess != NULL) {
		return module->desc->preprocess(module->data, input, output);
807
	} else {
François Grisez's avatar
François Grisez committed
808
		mblk_t *buffer;
809
		while((buffer = ms_queue_get(input)) != NULL) {
François Grisez's avatar
François Grisez committed
810 811
			ms_queue_put(output, buffer);
		}
812
		return 0;
François Grisez's avatar
François Grisez committed
813 814 815
	}
}

816
static mblk_t *module_process(Module *module, mblk_t *buffer, ms_bool_t *isKeyFrame, ms_bool_t *isVisible, uint8_t **codecPrivateData, size_t *codecPrivateSize) {
François Grisez's avatar
François Grisez committed
817
	mblk_t *frame;
818 819
	if(module->desc->process != NULL) {
		frame = module->desc->process(module->data, buffer, isKeyFrame, isVisible, codecPrivateData, codecPrivateSize);
820
	} else {
François Grisez's avatar
François Grisez committed
821 822
		frame = buffer;
		*isKeyFrame = TRUE;
823
		*isVisible = TRUE;
824
		*codecPrivateData = NULL;
François Grisez's avatar
François Grisez committed
825 826 827 828
	}
	return frame;
}