mkv.c 83.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

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

#define bool_t matroska_bool_t
#include <matroska/matroska.h>
#include <matroska/matroska_sem.h>
#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)(void);
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)(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 76 77
	uint8_t profile;
	uint8_t level;
	uint8_t NALULenghtSizeMinusOne;
	MSList *sps_list;
	MSList *pps_list;
78 79
} H264Private;

80 81 82 83 84 85
static void _ms_list_append_copy(const mblk_t *buffer, MSList **list) {
	*list = ms_list_append(*list, copymsg(buffer));
}

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

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

101
static H264Private *H264Private_new(const MSList *spsList, const MSList *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 MSList *H264Private_getSPS(const H264Private *obj) {
113
	return obj->sps_list;
114 115
}

116
static const MSList *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 122 123 124
	uint8_t nbSPS, nbPPS;
	MSList *it = NULL;
	uint8_t *result = NULL;
	int i;
125

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

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

François Grisez's avatar
François Grisez committed
132
	for(it=obj->sps_list;it!=NULL;it=it->next) {
133 134 135
		mblk_t *buff = (mblk_t*)(it->data);
		*size += msgdsize(buff);
	}
François Grisez's avatar
François Grisez committed
136
	for(it=obj->pps_list;it!=NULL;it=it->next) {
137 138 139 140
		mblk_t *buff = (mblk_t*)(it->data);
		*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
	i=6;
François Grisez's avatar
François Grisez committed
149
	for(it=obj->sps_list; it!=NULL; it=it->next) {
150 151 152 153 154 155 156 157 158 159 160 161
		mblk_t *buff = (mblk_t*)(it->data);
		size_t buff_size = msgdsize(buff);
		uint16_t buff_size_be = htons(buff_size);
		memcpy(&result[i], &buff_size_be, sizeof(buff_size_be));
		i+=sizeof(buff_size_be);
		memcpy(&result[i], buff->b_rptr, buff_size);
		i += buff_size;
	}

	result[i] = nbPPS;
	i++;

François Grisez's avatar
François Grisez committed
162
	for(it=obj->pps_list; it!=NULL; it=it->next) {
163 164 165 166 167 168 169 170 171
		mblk_t *buff = (mblk_t*)(it->data);
		int buff_size = msgdsize(buff);
		uint16_t buff_size_be = htons(buff_size);
		memcpy(&result[i], &buff_size_be, sizeof(buff_size_be));
		i+=sizeof(buff_size_be);
		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 185
	N = data[5] & 0x1F;
	r_ptr = data + 6;
	for(i=0;i<N;i++) {
186 187
		memcpy(&nalu_size, r_ptr, sizeof(uint16_t)); r_ptr += sizeof(uint16_t);
		nalu_size = ntohs(nalu_size);
188
		nalu = allocb(nalu_size, 0);
189 190 191 192
		memcpy(nalu->b_wptr, r_ptr, nalu_size); nalu->b_wptr += nalu_size; r_ptr += nalu_size;
		obj->sps_list = ms_list_append(obj->sps_list, nalu);
	}

193 194
	N = *r_ptr; r_ptr += 1;
	for(i=0;i<N;i++) {
195 196
		memcpy(&nalu_size, r_ptr, sizeof(uint16_t)); r_ptr += sizeof(uint16_t);
		nalu_size = ntohs(nalu_size);
197
		nalu = allocb(nalu_size, 0);
198 199 200
		memcpy(nalu->b_wptr, r_ptr, nalu_size); nalu->b_wptr += nalu_size; r_ptr += nalu_size;
		obj->pps_list = ms_list_append(obj->pps_list, nalu);
	}
201

202 203 204 205 206
	if(obj->sps_list != NULL) {
		const mblk_t *firstSPS = (const mblk_t *)ms_list_nth_data(obj->sps_list, 0);
		obj->profile = firstSPS->b_rptr[1];
		obj->level = firstSPS->b_rptr[3];
	}
207 208 209
}

/* h264 module */
210
typedef struct {
211
	Rfc3984Context rfc3984Context;
212
	H264Private *codecPrivate;
213 214
} H264Module;

215
static void *h264_module_new(void) {
216
	H264Module *mod = ms_new0(H264Module, 1);
217
	rfc3984_init(&mod->rfc3984Context);
218
	rfc3984_set_mode(&mod->rfc3984Context,1);
François Grisez's avatar
François Grisez committed
219
	return mod;
220 221
}

222
static void h264_module_free(void *data) {
223 224
	H264Module *obj = (H264Module *)data;
	rfc3984_uninit(&obj->rfc3984Context);
225 226 227
	if(obj->codecPrivate != NULL) {
		H264Private_free(obj->codecPrivate);
	}
228
	ms_free(obj);
229 230
}

231
static int h264_module_preprocessing(void *data, MSQueue *input, MSQueue *output) {
232 233
	H264Module *obj = (H264Module *)data;
	MSQueue queue;
234
	mblk_t *inputBuffer = NULL;
235 236

	ms_queue_init(&queue);
237
	while((inputBuffer = ms_queue_get(input)) != NULL) {
238
		rfc3984_unpack(&obj->rfc3984Context, inputBuffer, &queue);
239
		if(!ms_queue_empty(&queue)) {
240 241
			mblk_t *frame = ms_queue_get(&queue);
			mblk_t *end = frame;
242
			while(!ms_queue_empty(&queue)) {
243 244 245 246 247
				end = concatb(end, ms_queue_get(&queue));
			}
			ms_queue_put(output, frame);
		}
	}
248 249

	return 0;
250 251
}

252
static int h264_nalu_type(const mblk_t *nalu) {
253
	return (nalu->b_rptr[0]) & ((1<<5)-1);
254 255
}

256
static ms_bool_t h264_is_key_frame(const mblk_t *frame) {
257
	const mblk_t *curNalu = NULL;
258 259
	for(curNalu = frame; curNalu != NULL && h264_nalu_type(curNalu) != 5; curNalu = curNalu->b_cont);
	return curNalu != NULL;
260 261
}

262 263
static void nalus_to_frame(mblk_t *buffer, mblk_t **frame, MSList **spsList, MSList **ppsList, ms_bool_t *isKeyFrame) {
	mblk_t *curNalu = NULL;
264
	uint32_t timecode = mblk_get_timestamp_info(buffer);
265 266
	*frame = NULL;
	*isKeyFrame = FALSE;
267
	
268

269
	for(curNalu = buffer; curNalu != NULL;) {
270
		mblk_t *buff = curNalu;
271 272
		int type = h264_nalu_type(buff);
		
273 274
		curNalu = curNalu->b_cont;
		buff->b_cont = NULL;
275
		switch(type) {
276
		case 7:
277
			*spsList = ms_list_append(*spsList, buff);
278 279 280
			break;

		case 8:
281
			*ppsList = ms_list_append(*ppsList, buff);
282 283 284
			break;

		default:
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
			{
				uint32_t bufferSize = htonl(msgdsize(buff));
				mblk_t *size = allocb(4, 0);
				
				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);
				}
302
			}
303 304 305
		}
	}

306
	if(*frame != NULL) {
307 308 309
		msgpullup(*frame, -1);
		mblk_set_timestamp_info(*frame, timecode);
	}
310 311
}

312
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) {
313
	H264Module *obj = (H264Module *)data;
314 315 316 317 318 319 320 321 322 323 324 325 326
	mblk_t *frame = NULL;
	MSList *spsList = NULL, *ppsList = NULL;
	nalus_to_frame(nalus, &frame, &spsList, &ppsList, isKeyFrame);
	if(spsList != NULL || ppsList != NULL) {
		H264Private *codecPrivateStruct = H264Private_new(spsList, ppsList);
		if(obj->codecPrivate == NULL) {
			obj->codecPrivate = codecPrivateStruct;
		} else {
			H264Private_serialize(codecPrivateStruct, codecPrivateData, codecPrivateSize);
			H264Private_free(codecPrivateStruct);
		}
		if(spsList != NULL) ms_list_free_with_data(spsList, (void (*)(void *))freemsg);
		if(ppsList != NULL) ms_list_free_with_data(ppsList, (void (*)(void *))freemsg);
327
	}
328
	*isVisible = TRUE;
329
	return frame;
330 331
}

332 333
static void h264_module_reverse(void *data, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize) {
	mblk_t *buffer = NULL, *bufferFrag = NULL;
334 335
	H264Module *obj = (H264Module *)data;
	MSQueue queue;
336 337 338
	const MSList *it = NULL;
	H264Private *codecPrivate = NULL, *selectedCodecPrivate = NULL;

339 340 341 342 343 344 345 346 347 348 349 350 351 352
	ms_queue_init(&queue);
	while(input->b_rptr != input->b_wptr) {
		uint32_t naluSize;
		mblk_t *nalu;
		memcpy(&naluSize, input->b_rptr, sizeof(uint32_t)); input->b_rptr += sizeof(uint32_t);
		naluSize = ntohl(naluSize);
		nalu = allocb(naluSize, 0);
		memcpy(nalu->b_wptr, input->b_rptr, naluSize); nalu->b_wptr += naluSize; input->b_rptr += naluSize;
		if(buffer == NULL) {
			buffer = nalu;
		} else {
			concatb(buffer, nalu);
		}
	}
353 354 355 356 357 358 359 360 361 362 363
	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);
364
			ms_message("MKVPlayer: send SPS");
365 366 367 368
		}
		for(it = H264Private_getPPS(selectedCodecPrivate); it != NULL; it = it->next) {
			mblk_t *pps = copymsg((mblk_t *)it->data);
			ms_queue_put(&queue, pps);
369
			ms_message("MKVPlayer: send PPS");
370
		}
371
	}
372 373 374

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

375 376 377 378 379 380 381
	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));
382
	freemsg(input);
383 384 385
}

static void h264_module_get_private_data(const void *o, uint8_t **data, size_t *data_size) {
386
	const H264Module *obj = (const H264Module *)o;
387
	H264Private_serialize(obj->codecPrivate, data, data_size);
388 389
}

390
static void h264_module_load_private_data(void *o, const uint8_t *data, size_t size) {
391
	H264Module *obj = (H264Module *)o;
392 393
	obj->codecPrivate = H264Private_new(NULL, NULL);
	H264Private_load(obj->codecPrivate, data);
394 395
}

396
/* h264 module description */
397
#ifdef _MSC_VER
398
static const ModuleDesc h264_module_desc = {
399 400
	"H264",
	"V_MPEG4/ISO/AVC",
François Grisez's avatar
François Grisez committed
401 402
	h264_module_new,
	h264_module_free,
403 404 405
	NULL,
	h264_module_preprocessing,
	h264_module_processing,
406
	h264_module_reverse,
407 408 409
	h264_module_get_private_data,
	h264_module_load_private_data,
	h264_is_key_frame
410
};
411
#else
412
static const ModuleDesc h264_module_desc = {
413 414 415 416 417 418 419 420 421 422 423 424 425
	.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
426

427 428 429
/*********************************************************************************************
 * VP8 module                                                                               *
 *********************************************************************************************/
Ghislain MARY's avatar
Ghislain MARY committed
430 431 432

#ifdef HAVE_VPX

433 434 435
typedef struct _Vp8Module {
	Vp8RtpFmtUnpackerCtx unpacker;
	Vp8RtpFmtPackerCtx packer;
François Grisez's avatar
François Grisez committed
436
	uint16_t cseq;
437 438
} Vp8Module;

439
static void *vp8_module_new(void) {
440 441 442 443 444 445
	Vp8Module *obj = (Vp8Module *)ms_new0(Vp8Module, 1);
	vp8rtpfmt_unpacker_init(&obj->unpacker, NULL, FALSE, TRUE, FALSE);
	vp8rtpfmt_packer_init(&obj->packer);
	return obj;
}

446
static void vp8_module_free(void *obj) {
447 448 449 450 451 452
	Vp8Module *mod = (Vp8Module *)obj;
	vp8rtpfmt_unpacker_uninit(&mod->unpacker);
	vp8rtpfmt_packer_uninit(&mod->packer);
	ms_free(mod);
}

453
static int vp8_module_preprocess(void *obj, MSQueue *input, MSQueue *output) {
454 455 456
	Vp8RtpFmtUnpackerCtx *unpacker = (Vp8RtpFmtUnpackerCtx *)obj;
	Vp8RtpFmtFrameInfo infos;
	vp8rtpfmt_unpacker_feed(unpacker, input);
457
	return vp8rtpfmt_unpacker_get_frame(unpacker, output, &infos);
458 459
}

460
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) {
461 462 463
	uint8_t first_byte = *buffer->b_rptr;
	*isKeyFrame = !(first_byte & 0x01);
	*isVisible = first_byte & 0x10;
464
	return buffer;
465 466
}

467
static void vp8_module_reverse(void *obj, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize) {
468 469
	Vp8Module *mod = (Vp8Module *)obj;
	MSList *packer_input = NULL;
François Grisez's avatar
François Grisez committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	Vp8RtpFmtPacket *packet = ms_new0(Vp8RtpFmtPacket, 1);
	MSQueue q;
	mblk_t *m;
	
	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);
	packer_input = ms_list_append(packer_input, packet);
	vp8rtpfmt_packer_process(&mod->packer, packer_input, &q);
	
Simon Morlat's avatar
Simon Morlat committed
486
	while((m = ms_queue_get(&q))) {
François Grisez's avatar
François Grisez committed
487 488 489
		mblk_set_cseq(m, mod->cseq++);
		ms_queue_put(output, m);
	}
490 491
}

492
static ms_bool_t vp8_module_is_keyframe(const mblk_t *frame) {
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	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
528 529
#endif /* HAVE_VPX */

530 531 532 533
/*********************************************************************************************
 * µLaw module                                                                               *
 *********************************************************************************************/
/* WavPrivate */
François Grisez's avatar
François Grisez committed
534
typedef struct {
535 536 537 538 539 540 541
	uint16_t wFormatTag;
	uint16_t nbChannels;
	uint32_t nSamplesPerSec;
	uint32_t nAvgBytesPerSec;
	uint16_t nBlockAlign;
	uint16_t wBitsPerSample;
	uint16_t cbSize;
542 543
} WavPrivate;

François Grisez's avatar
François Grisez committed
544
static void wav_private_set(WavPrivate *data, const MSFmtDescriptor *obj) {
545 546 547
	uint16_t bitsPerSample = 8;
	uint16_t nbBlockAlign = (bitsPerSample * obj->nchannels)/8;
	uint32_t bitrate = bitsPerSample * obj->nchannels * obj->rate;
548

549 550 551 552 553 554 555
	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;
556 557
}

François Grisez's avatar
François Grisez committed
558
static void wav_private_serialize(const WavPrivate *obj, uint8_t **data, size_t *size) {
François Grisez's avatar
François Grisez committed
559
	*size = sizeof(WavPrivate);
560 561
	*data = (uint8_t *)ms_new0(uint8_t, *size);
	memcpy(*data, obj, *size);
562 563
}

564
static void wav_private_load(WavPrivate *obj, const uint8_t *data) {
François Grisez's avatar
François Grisez committed
565
	memcpy(obj, data, sizeof(WavPrivate));
566 567 568
}

/* µLaw module */
569
static void *mu_law_module_new(void) {
570
	return ms_new0(WavPrivate, 1);
571 572
}

François Grisez's avatar
François Grisez committed
573
static void mu_law_module_free(void *o) {
574
	ms_free(o);
575 576
}

577
static void mu_law_module_set(void *o, const MSFmtDescriptor *fmt) {
François Grisez's avatar
François Grisez committed
578
	WavPrivate *obj = (WavPrivate *)o;
579
	wav_private_set(obj, fmt);
580 581
}

François Grisez's avatar
François Grisez committed
582
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
583 584
	const WavPrivate *obj = (const WavPrivate *)o;
	wav_private_serialize(obj, data, data_size);
585 586
}

587
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
588 589
	WavPrivate *obj = (WavPrivate *)o;
	wav_private_load(obj, data);
590 591
}

592
/* µLaw module description */
593
#ifdef _MSC_VER
594
static const ModuleDesc mu_law_module_desc = {
595 596
	"pcmu",
	"A_MS/ACM",
François Grisez's avatar
François Grisez committed
597 598
	mu_law_module_new,
	mu_law_module_free,
599 600 601
	mu_law_module_set,
	NULL,
	NULL,
602
	NULL,
603 604 605
	mu_law_module_get_private_data,
	mu_law_module_load_private,
	NULL
606
};
607
#else
608
static const ModuleDesc mu_law_module_desc = {
609 610 611 612 613 614 615 616 617 618 619 620 621
	.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
622

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
/*********************************************************************************************
 * 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) {
	obj->channelCount = nChannels;
	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);
	memcpy((*data)+8, obj, 11);
}

656
static void opus_codec_private_load(OpusCodecPrivate *obj, const uint8_t *data, size_t size) {
657 658 659 660
	memcpy(obj, data+8, 11);
}

// OpusModule
661
static void *opus_module_new(void) {
662
	OpusCodecPrivate *obj = ms_new0(OpusCodecPrivate, 1);
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 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
	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

717
/*********************************************************************************************
François Grisez's avatar
François Grisez committed
718
 * Modules list                                                                              *
719
 *********************************************************************************************/
François Grisez's avatar
François Grisez committed
720
typedef enum {
721
	NONE_ID,
722
	H264_MOD_ID,
723
	VP8_MOD_ID,
François Grisez's avatar
François Grisez committed
724
	MU_LAW_MOD_ID,
725
	OPUS_MOD_ID
726 727
} ModuleId;

François Grisez's avatar
François Grisez committed
728
static const ModuleDesc *moduleDescs[] = {
729
	&h264_module_desc,
Ghislain MARY's avatar
Ghislain MARY committed
730
#ifdef HAVE_VPX
731
	&vp8_module_desc,
Ghislain MARY's avatar
Ghislain MARY committed
732
#endif /* HAVE_VPX */
733
	&mu_law_module_desc,
734
	&opus_module_desc,
735
	NULL
736 737
};

François Grisez's avatar
François Grisez committed
738
static int find_module_id_from_rfc_name(const char *rfcName) {
739
	int id;
740
	for(id=0; moduleDescs[id] && strcasecmp(moduleDescs[id]->rfcName, rfcName) != 0; id++);
741
	return moduleDescs[id] ? id : NONE_ID;
742 743
}

François Grisez's avatar
François Grisez committed
744
static int find_module_id_from_codec_id(const char *codecId) {
745
	int id;
746
	for(id=0; moduleDescs[id] && strcmp(moduleDescs[id]->codecId, codecId) != 0; id++);
747
	return moduleDescs[id] ? id : NONE_ID;
748 749
}

750 751 752 753 754 755 756 757 758
static const char *codec_id_to_rfc_name(const char *codecId) {
	ModuleId id = find_module_id_from_codec_id(codecId);
	if(id == NONE_ID) {
		return NULL;
	} else {
		return moduleDescs[id]->rfcName;
	}
}

François Grisez's avatar
François Grisez committed
759 760 761
/*********************************************************************************************
 * Module                                                                                    *
 *********************************************************************************************/
762
typedef struct {
François Grisez's avatar
François Grisez committed
763 764 765 766
	ModuleId id;
	void *data;
} Module;

767
static Module *module_new(const char *rfcName) {
768
	ModuleId id = find_module_id_from_rfc_name(rfcName);
769
	if(id == NONE_ID) {
François Grisez's avatar
François Grisez committed
770
		return NULL;
771 772
	} else {
		Module *module = (Module *)ms_new0(Module, 1);
François Grisez's avatar
François Grisez committed
773
		module->id = id;
774
		if(moduleDescs[module->id]->new_module) module->data = moduleDescs[module->id]->new_module();
François Grisez's avatar
François Grisez committed
775 776 777 778
		return module;
	}
}

779
static void module_free(Module *module) {
780
	if(moduleDescs[module->id]->free_module) moduleDescs[module->id]->free_module(module->data);
François Grisez's avatar
François Grisez committed
781 782 783
	ms_free(module);
}

784 785
static void module_set(Module *module, const MSFmtDescriptor *format) {
	if(moduleDescs[module->id]->set != NULL) {
François Grisez's avatar
François Grisez committed
786
		moduleDescs[module->id]->set(module->data, format);
787
	}
François Grisez's avatar
François Grisez committed
788 789
}

790
static int module_preprocess(Module *module, MSQueue *input, MSQueue *output) {
791
	if(moduleDescs[module->id]->preprocess != NULL) {
792
		return moduleDescs[module->id]->preprocess(module->data, input, output);
793
	} else {
François Grisez's avatar
François Grisez committed
794
		mblk_t *buffer;
795
		while((buffer = ms_queue_get(input)) != NULL) {
François Grisez's avatar
François Grisez committed
796 797
			ms_queue_put(output, buffer);
		}
798
		return 0;
François Grisez's avatar
François Grisez committed
799 800 801
	}
}

802
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
803
	mblk_t *frame;
804
	if(moduleDescs[module->id]->process != NULL) {
805
		frame = moduleDescs[module->id]->process(module->data, buffer, isKeyFrame, isVisible, codecPrivateData, codecPrivateSize);
806
	} else {
François Grisez's avatar
François Grisez committed
807 808
		frame = buffer;
		*isKeyFrame = TRUE;
809
		*isVisible = TRUE;
810
		*codecPrivateData = NULL;
François Grisez's avatar
François Grisez committed
811 812 813 814
	}
	return frame;
}

815
static void module_reverse(Module *module, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize) {
816 817 818
	if(moduleDescs[module->id]->reverse == NULL) {
		ms_queue_put(output, input);
	} else {
819
		moduleDescs[module->id]->reverse(module->data, input, output, isFirstFrame, codecPrivateData, codecPrivateSize);
820 821 822
	}
}

823
static void module_get_private_data(const Module *module, uint8_t **data, size_t *dataSize) {
824 825 826 827 828 829
	if(moduleDescs[module->id]->get_private_data) {
		moduleDescs[module->id]->get_private_data(module->data, data, dataSize);
	} else {
		*data = NULL;
		*dataSize = 0;
	}
François Grisez's avatar
François Grisez committed
830 831
}

832
static void module_load_private_data(Module *module, const uint8_t *data, size_t size) {
833 834 835
	if(moduleDescs[module->id]->load_private_data) {
		moduleDescs[module->id]->load_private_data(module->data, data, size);
	}
François Grisez's avatar
François Grisez committed
836 837
}

838
static ms_bool_t module_is_key_frame(const Module *module, const mblk_t *frame) {
839 840 841 842 843
	if(moduleDescs[module->id]->is_key_frame) {
		return moduleDescs[module->id]->is_key_frame(frame);
	} else {
		return TRUE;
	}
François Grisez's avatar
François Grisez committed
844 845
}

846
static const char *module_get_codec_id(const Module *module) {
François Grisez's avatar
François Grisez committed
847 848 849
	return moduleDescs[module->id]->codecId;
}

850 851 852
/*********************************************************************************************
 * Matroska                                                                                  *
 *********************************************************************************************/
853
#define WRITE_DEFAULT_ELEMENT FALSE
854 855

static const timecode_t MKV_TIMECODE_SCALE = 1000000;
856
static const int MKV_DOCTYPE_VERSION = 4;
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
static const int MKV_DOCTYPE_READ_VERSION = 2;

extern const nodemeta LangStr_Class[];
extern const nodemeta UrlPart_Class[];
extern const nodemeta BufStream_Class[];
extern const nodemeta MemStream_Class[];
extern const nodemeta Streams_Class[];
extern const nodemeta File_Class[];
extern const nodemeta Stdio_Class[];
extern const nodemeta Matroska_Class[];
extern const nodemeta EBMLElement_Class[];
extern const nodemeta EBMLMaster_Class[];
extern const nodemeta EBMLBinary_Class[];
extern const nodemeta EBMLString_Class[];
extern const nodemeta EBMLInteger_Class[];
extern const nodemeta EBMLCRC_Class[];
extern const nodemeta EBMLDate_Class[];
extern const nodemeta EBMLVoid_Class[];

François Grisez's avatar
François Grisez committed
876
static void loadModules(nodemodule *modules) {
877 878 879 880 881 882 883 884 885 886 887
	NodeRegisterClassEx(modules, Streams_Class);
	NodeRegisterClassEx(modules, File_Class);
	NodeRegisterClassEx(modules, Matroska_Class);
	NodeRegisterClassEx(modules, EBMLElement_Class);
	NodeRegisterClassEx(modules, EBMLMaster_Class);
	NodeRegisterClassEx(modules, EBMLBinary_Class);
	NodeRegisterClassEx(modules, EBMLString_Class);
	NodeRegisterClassEx(modules, EBMLInteger_Class);
	NodeRegisterClassEx(modules, EBMLCRC_Class);
	NodeRegisterClassEx(modules, EBMLDate_Class);
	NodeRegisterClassEx(modules, EBMLVoid_Class);
888 889
}

890 891 892 893 894 895
typedef enum {
	MKV_OPEN_CREATE,
	MKV_OPEN_APPEND,
	MKV_OPEN_RO
} MatroskaOpenMode;

François Grisez's avatar
François Grisez committed
896
typedef struct {