mkv.c 83.3 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                                                                          *
 *********************************************************************************************/
François Grisez's avatar
François Grisez committed
44 45
typedef void *(*ModuleNewFunc)();
typedef void (*ModuleFreeFunc)(void *obj);
46
typedef void (*ModuleSetFunc)(void *obj, const MSFmtDescriptor *fmt);
47
typedef void (*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 216
static void *h264_module_new() {
	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 void 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
}

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

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

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

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

		case 8:
279
			*ppsList = ms_list_append(*ppsList, buff);
280 281 282
			break;

		default:
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
			{
				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);
				}
300
			}
301 302 303
		}
	}

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

310
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) {
311
	H264Module *obj = (H264Module *)data;
312 313 314 315 316 317 318 319 320 321 322 323 324
	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);
325
	}
326
	*isVisible = TRUE;
327
	return frame;
328 329
}

330 331
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;
332 333
	H264Module *obj = (H264Module *)data;
	MSQueue queue;
334 335 336
	const MSList *it = NULL;
	H264Private *codecPrivate = NULL, *selectedCodecPrivate = NULL;

337 338 339 340 341 342 343 344 345 346 347 348 349 350
	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);
		}
	}
351 352 353 354 355 356 357 358 359 360 361
	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);
362
			ms_message("MKVPlayer: send SPS");
363 364 365 366
		}
		for(it = H264Private_getPPS(selectedCodecPrivate); it != NULL; it = it->next) {
			mblk_t *pps = copymsg((mblk_t *)it->data);
			ms_queue_put(&queue, pps);
367
			ms_message("MKVPlayer: send PPS");
368
		}
369
	}
370 371 372

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

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

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

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

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

425 426 427 428 429 430
/*********************************************************************************************
 * VP8 module                                                                               *
 *********************************************************************************************/
typedef struct _Vp8Module {
	Vp8RtpFmtUnpackerCtx unpacker;
	Vp8RtpFmtPackerCtx packer;
François Grisez's avatar
François Grisez committed
431
	uint16_t cseq;
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
} Vp8Module;

void *vp8_module_new() {
	Vp8Module *obj = (Vp8Module *)ms_new0(Vp8Module, 1);
	vp8rtpfmt_unpacker_init(&obj->unpacker, NULL, FALSE, TRUE, FALSE);
	vp8rtpfmt_packer_init(&obj->packer);
	return obj;
}

void vp8_module_free(void *obj) {
	Vp8Module *mod = (Vp8Module *)obj;
	vp8rtpfmt_unpacker_uninit(&mod->unpacker);
	vp8rtpfmt_packer_uninit(&mod->packer);
	ms_free(mod);
}

void vp8_module_preprocess(void *obj, MSQueue *input, MSQueue *output) {
	Vp8RtpFmtUnpackerCtx *unpacker = (Vp8RtpFmtUnpackerCtx *)obj;
	Vp8RtpFmtFrameInfo infos;
	vp8rtpfmt_unpacker_feed(unpacker, input);
	vp8rtpfmt_unpacker_get_frame(unpacker, output, &infos);
}

mblk_t *vp8_module_process(void *obj, mblk_t *buffer, ms_bool_t *isKeyFrame, ms_bool_t *isVisible, uint8_t **codecPrivateData, size_t *codecPrivateSize) {
	uint8_t first_byte = *buffer->b_rptr;
	*isKeyFrame = !(first_byte & 0x01);
	*isVisible = first_byte & 0x10;
459
	return buffer;
460 461 462 463 464
}

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

ms_bool_t vp8_module_is_keyframe(const mblk_t *frame) {
	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

523 524 525 526
/*********************************************************************************************
 * µLaw module                                                                               *
 *********************************************************************************************/
/* WavPrivate */
François Grisez's avatar
François Grisez committed
527
typedef struct {
528 529 530 531 532 533 534
	uint16_t wFormatTag;
	uint16_t nbChannels;
	uint32_t nSamplesPerSec;
	uint32_t nAvgBytesPerSec;
	uint16_t nBlockAlign;
	uint16_t wBitsPerSample;
	uint16_t cbSize;
535 536
} WavPrivate;

François Grisez's avatar
François Grisez committed
537
static void wav_private_set(WavPrivate *data, const MSFmtDescriptor *obj) {
538 539 540
	uint16_t bitsPerSample = 8;
	uint16_t nbBlockAlign = (bitsPerSample * obj->nchannels)/8;
	uint32_t bitrate = bitsPerSample * obj->nchannels * obj->rate;
541

542 543 544 545 546 547 548
	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;
549 550
}

François Grisez's avatar
François Grisez committed
551
static void wav_private_serialize(const WavPrivate *obj, uint8_t **data, size_t *size) {
François Grisez's avatar
François Grisez committed
552
	*size = sizeof(WavPrivate);
553 554
	*data = (uint8_t *)ms_new0(uint8_t, *size);
	memcpy(*data, obj, *size);
555 556
}

557
static void wav_private_load(WavPrivate *obj, const uint8_t *data) {
François Grisez's avatar
François Grisez committed
558
	memcpy(obj, data, sizeof(WavPrivate));
559 560 561
}

/* µLaw module */
François Grisez's avatar
François Grisez committed
562
static void *mu_law_module_new() {
563
	return ms_new0(WavPrivate, 1);
564 565
}

François Grisez's avatar
François Grisez committed
566
static void mu_law_module_free(void *o) {
567
	ms_free(o);
568 569
}

570
static void mu_law_module_set(void *o, const MSFmtDescriptor *fmt) {
François Grisez's avatar
François Grisez committed
571
	WavPrivate *obj = (WavPrivate *)o;
572
	wav_private_set(obj, fmt);
573 574
}

François Grisez's avatar
François Grisez committed
575
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
576 577
	const WavPrivate *obj = (const WavPrivate *)o;
	wav_private_serialize(obj, data, data_size);
578 579
}

580
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
581 582
	WavPrivate *obj = (WavPrivate *)o;
	wav_private_load(obj, data);
583 584
}

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

616 617 618 619 620 621 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
/*********************************************************************************************
 * 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);
}

649
static void opus_codec_private_load(OpusCodecPrivate *obj, const uint8_t *data, size_t size) {
650 651 652 653 654
	memcpy(obj, data+8, 11);
}

// OpusModule
static void *opus_module_new() {
655
	OpusCodecPrivate *obj = ms_new0(OpusCodecPrivate, 1);
656 657 658 659 660 661 662 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
	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

710
/*********************************************************************************************
François Grisez's avatar
François Grisez committed
711
 * Modules list                                                                              *
712
 *********************************************************************************************/
François Grisez's avatar
François Grisez committed
713
typedef enum {
714
	H264_MOD_ID,
715
	VP8_MOD_ID,
François Grisez's avatar
François Grisez committed
716
	MU_LAW_MOD_ID,
717
	OPUS_MOD_ID,
François Grisez's avatar
François Grisez committed
718
	NONE_ID
719 720
} ModuleId;

François Grisez's avatar
François Grisez committed
721
static const ModuleDesc *moduleDescs[] = {
722
	&h264_module_desc,
723
	&vp8_module_desc,
724
	&mu_law_module_desc,
725
	&opus_module_desc,
726
	NULL
727 728
};

François Grisez's avatar
François Grisez committed
729
static int find_module_id_from_rfc_name(const char *rfcName) {
730
	int id;
731
	for(id=0; moduleDescs[id] && strcasecmp(moduleDescs[id]->rfcName, rfcName) != 0; id++);
732
	return id;
733 734
}

François Grisez's avatar
François Grisez committed
735
static int find_module_id_from_codec_id(const char *codecId) {
736
	int id;
737
	for(id=0; moduleDescs[id] && strcmp(moduleDescs[id]->codecId, codecId) != 0; id++);
738
	return id;
739 740
}

741 742 743 744 745 746 747 748 749
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
750 751 752
/*********************************************************************************************
 * Module                                                                                    *
 *********************************************************************************************/
753
typedef struct {
François Grisez's avatar
François Grisez committed
754 755 756 757
	ModuleId id;
	void *data;
} Module;

758
static Module *module_new(const char *rfcName) {
759
	ModuleId id = find_module_id_from_rfc_name(rfcName);
760
	if(id == NONE_ID) {
François Grisez's avatar
François Grisez committed
761
		return NULL;
762 763
	} else {
		Module *module = (Module *)ms_new0(Module, 1);
François Grisez's avatar
François Grisez committed
764
		module->id = id;
765
		if(moduleDescs[module->id]->new_module) module->data = moduleDescs[module->id]->new_module();
François Grisez's avatar
François Grisez committed
766 767 768 769
		return module;
	}
}

770
static void module_free(Module *module) {
771
	if(moduleDescs[module->id]->free_module) moduleDescs[module->id]->free_module(module->data);
François Grisez's avatar
François Grisez committed
772 773 774
	ms_free(module);
}

775 776
static void module_set(Module *module, const MSFmtDescriptor *format) {
	if(moduleDescs[module->id]->set != NULL) {
François Grisez's avatar
François Grisez committed
777
		moduleDescs[module->id]->set(module->data, format);
778
	}
François Grisez's avatar
François Grisez committed
779 780
}

781 782
static void module_preprocess(Module *module, MSQueue *input, MSQueue *output) {
	if(moduleDescs[module->id]->preprocess != NULL) {
François Grisez's avatar
François Grisez committed
783
		moduleDescs[module->id]->preprocess(module->data, input, output);
784
	} else {
François Grisez's avatar
François Grisez committed
785
		mblk_t *buffer;
786
		while((buffer = ms_queue_get(input)) != NULL) {
François Grisez's avatar
François Grisez committed
787 788 789 790 791
			ms_queue_put(output, buffer);
		}
	}
}

792
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
793
	mblk_t *frame;
794
	if(moduleDescs[module->id]->process != NULL) {
795
		frame = moduleDescs[module->id]->process(module->data, buffer, isKeyFrame, isVisible, codecPrivateData, codecPrivateSize);
796
	} else {
François Grisez's avatar
François Grisez committed
797 798
		frame = buffer;
		*isKeyFrame = TRUE;
799
		*isVisible = TRUE;
800
		*codecPrivateData = NULL;
François Grisez's avatar
François Grisez committed
801 802 803 804
	}
	return frame;
}

805
static void module_reverse(Module *module, mblk_t *input, MSQueue *output, ms_bool_t isFirstFrame, const uint8_t *codecPrivateData, size_t codecPrivateSize) {
806 807 808
	if(moduleDescs[module->id]->reverse == NULL) {
		ms_queue_put(output, input);
	} else {
809
		moduleDescs[module->id]->reverse(module->data, input, output, isFirstFrame, codecPrivateData, codecPrivateSize);
810 811 812
	}
}

813
static void module_get_private_data(const Module *module, uint8_t **data, size_t *dataSize) {
814 815 816 817 818 819
	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
820 821
}

822
static void module_load_private_data(Module *module, const uint8_t *data, size_t size) {
823 824 825
	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
826 827
}

828
static ms_bool_t module_is_key_frame(const Module *module, const mblk_t *frame) {
829 830 831 832 833
	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
834 835
}

836
static const char *module_get_codec_id(const Module *module) {
François Grisez's avatar
François Grisez committed
837 838 839
	return moduleDescs[module->id]->codecId;
}

840 841 842
/*********************************************************************************************
 * Matroska                                                                                  *
 *********************************************************************************************/
843
#define WRITE_DEFAULT_ELEMENT FALSE
844 845

static const timecode_t MKV_TIMECODE_SCALE = 1000000;
846
static const int MKV_DOCTYPE_VERSION = 4;
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
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
866
static void loadModules(nodemodule *modules) {
867 868 869 870 871 872 873 874 875 876 877
	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);
878 879
}

880 881 882 883 884 885
typedef enum {
	MKV_OPEN_CREATE,
	MKV_OPEN_APPEND,
	MKV_OPEN_RO
} MatroskaOpenMode;

François Grisez's avatar
François Grisez committed
886
typedef struct {
887 888
	parsercontext *p;
	stream *output;
889
	ebml_master *header, *segment, *cluster, *info, *tracks, *metaSeek, *cues, *firstCluster, *currentCluster;
890
	matroska_seekpoint *infoMeta, *tracksMeta, *cuesMeta;
891
	matroska_block *currentBlock;
892 893 894
	timecode_t timecodeScale;
	filepos_t segmentInfoPosition;
	int nbClusters;
895 896
} Matroska;

Simon Morlat's avatar
Simon Morlat committed
897

François Grisez's avatar
François Grisez committed
898
static void matroska_init(Matroska *obj) {
899
	memset(obj, 0, sizeof(Matroska));
Simon Morlat's avatar
Simon Morlat committed
900
	obj->p = (parsercontext *)ms_new0(parsercontext, 1);
901 902 903
	ParserContext_Init(obj->p, NULL, NULL, NULL);
	loadModules((nodemodule*)obj->p);
	MATROSKA_Init((nodecontext*)obj->p);
904 905
	obj->segmentInfoPosition = -1;
	obj->timecodeScale = -1;
906 907
}

François Grisez's avatar
François Grisez committed
908
static void matroska_uninit(Matroska *obj) {
François Grisez's avatar
François Grisez committed
909 910 911
	MATROSKA_Done((nodecontext*)obj->p);
	ParserContext_Done(obj->p);
	ms_free(obj->p);
912 913
}

914 915
static int ebml_reading_profile(ebml_master *head) {
	char docType[9];
916
	int profile;
917 918
	int docTypeReadVersion;
	
919 920
	EBML_StringGet((ebml_string *)EBML_MasterGetChild(head, &EBML_ContextDocType), docType, sizeof(docType));
	docTypeReadVersion = EBML_IntegerValue((ebml_integer *)EBML_MasterGetChild(head, &EBML_ContextDocTypeReadVersion));
921
	
922

923
	if (strcmp(docType, "matroska")==0) {
François Grisez's avatar
François Grisez committed
924
		switch (docTypeReadVersion) {
925 926 927 928 929 930 931 932