chanadapt.c 5.52 KB
Newer Older
1 2
/*
mediastreamer2 library - modular sound and video processing and streaming
Erwan Croze's avatar
Erwan Croze committed
3
Copyright (C) 2010-2018  Simon MORLAT (simon.morlat@linphone.org)
4 5 6 7 8 9 10 11 12 13 14 15 16

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

#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/mschanadapter.h"
Erwan Croze's avatar
Erwan Croze committed
22
#include "mediastreamer2/msticker.h"
23 24 25 26 27

/*
 This filter transforms stereo buffers to mono and vice versa.
*/

Erwan Croze's avatar
Erwan Croze committed
28
typedef struct AdapterState {
29 30
	int inputchans;
	int outputchans;
Erwan Croze's avatar
Erwan Croze committed
31 32 33 34 35 36
	int sample_rate;
	size_t buffer_size;
	uint8_t *buffer1;
	uint8_t *buffer2;
	MSBufferizer input_buffer1;
	MSBufferizer input_buffer2;
37 38
}AdapterState;

Erwan Croze's avatar
Erwan Croze committed
39 40 41 42 43 44
static void adapter_init(MSFilter *f) {
	AdapterState *s = ms_new0(AdapterState, 1);
	s->inputchans = 1;
	s->outputchans = 1;
	s->sample_rate = 8000;
	f->data = s;
45 46
}

Erwan Croze's avatar
Erwan Croze committed
47 48 49
static void adapter_uninit(MSFilter *f) {
	AdapterState *s = (AdapterState*)f->data;
	ms_free(s);
50 51
}

Erwan Croze's avatar
Erwan Croze committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static void adapter_preprocess(MSFilter *f) {
	AdapterState *s = (AdapterState*)f->data;
	if (s->inputchans == 2 && s->outputchans == 1) {
		s->buffer_size = ((f->ticker->interval * s->sample_rate) / 1000) * 2;
		s->buffer1 = ms_new(uint8_t, s->buffer_size);
		s->buffer2 = ms_new(uint8_t, s->buffer_size);
		ms_bufferizer_init(&s->input_buffer1);
		ms_bufferizer_init(&s->input_buffer2);
	}
}

static void adapter_process(MSFilter *f) {
	AdapterState *s = (AdapterState*)f->data;

	// Two mono input t stereo output
	if (s->inputchans == 2 && s->outputchans == 1) {
		size_t buffer_size1, buffer_size2;

		ms_bufferizer_put_from_queue(&s->input_buffer1, f->inputs[0]);
		ms_bufferizer_put_from_queue(&s->input_buffer2, f->inputs[1]);

		buffer_size1 = ms_bufferizer_get_avail(&s->input_buffer1);
		buffer_size2 = ms_bufferizer_get_avail(&s->input_buffer2);

		while (buffer_size1 >= s->buffer_size || buffer_size2 >= s->buffer_size) {
			mblk_t *om;

			if (buffer_size1 < s->buffer_size) memset(s->buffer1, 0, s->buffer_size);
			if (buffer_size2 < s->buffer_size) memset(s->buffer2, 0, s->buffer_size);

			ms_bufferizer_read(&s->input_buffer1, s->buffer1, s->buffer_size);
			ms_bufferizer_read(&s->input_buffer2, s->buffer2, s->buffer_size);

			om = allocb(s->buffer_size * 2, 0);

			for (unsigned int i = 0 ; i < s->buffer_size / sizeof(int16_t) ; i++ , om->b_wptr += 4) {
				((int16_t*)om->b_wptr)[0] = ((int16_t*)s->buffer1)[i];
				((int16_t*)om->b_wptr)[1] = ((int16_t*)s->buffer2)[i];
90
			}
Erwan Croze's avatar
Erwan Croze committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

			ms_queue_put(f->outputs[0], om);

			buffer_size1 = ms_bufferizer_get_avail(&s->input_buffer1);
			buffer_size2 = ms_bufferizer_get_avail(&s->input_buffer2);
		}
	} else {
		mblk_t *im, *om;
		size_t msgsize;

		while ((im = ms_queue_get(f->inputs[0])) != NULL) {
			if (s->inputchans == s->outputchans) {
				ms_queue_put(f->outputs[0], im);
			} else if (s->outputchans == 2) {
				msgsize = msgdsize(im) * 2;
				om = allocb(msgsize, 0);
				for (;im->b_rptr < im->b_wptr ; im->b_rptr += 2 ,om->b_wptr += 4){
					((int16_t*)om->b_wptr)[0] = *(int16_t*)im->b_rptr;
					((int16_t*)om->b_wptr)[1] = *(int16_t*)im->b_rptr;
				}
				ms_queue_put(f->outputs[0], om);
				freemsg(im);
113 114 115 116 117
			}
		}
	}
}

Erwan Croze's avatar
Erwan Croze committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
static void adapter_postprocess(MSFilter *f) {
	AdapterState *s = (AdapterState*)f->data;
	ms_bufferizer_uninit(&s->input_buffer1);
	ms_bufferizer_uninit(&s->input_buffer2);
	if (s->buffer1) ms_free(s->buffer1);
	if (s->buffer2) ms_free(s->buffer2);
	s->buffer1 = NULL;
	s->buffer2 = NULL;
}

static int adapter_set_sample_rate(MSFilter *f, void *data) {
	AdapterState *s = (AdapterState*)f->data;
	s->sample_rate = *(int*)data;
	return 0;
}

static int adapter_get_sample_rate(MSFilter *f, void *data) {
	AdapterState *s = (AdapterState*)f->data;
	*(int*)data = s->sample_rate;
	return 0;
}

static int adapter_set_nchannels(MSFilter *f, void *data) {
	AdapterState *s = (AdapterState*)f->data;
	s->inputchans = *(int*)data;
143 144 145
	return 0;
}

Erwan Croze's avatar
Erwan Croze committed
146 147 148
static int adapter_get_nchannels(MSFilter *f, void *data) {
	AdapterState *s = (AdapterState*)f->data;
	*(int*)data = s->inputchans;
149 150 151
	return 0;
}

Erwan Croze's avatar
Erwan Croze committed
152 153 154
static int adapter_set_out_nchannels(MSFilter *f, void *data) {
	AdapterState *s = (AdapterState*)f->data;
	s->outputchans = *(int*)data;
155 156 157
	return 0;
}

Erwan Croze's avatar
Erwan Croze committed
158 159 160
static int adapter_get_out_nchannels(MSFilter *f, void *data) {
	AdapterState *s = (AdapterState*)f->data;
	*(int*)data = s->outputchans;
161 162 163
	return 0;
}

Erwan Croze's avatar
Erwan Croze committed
164 165 166
static MSFilterMethod methods[] = {
	{	MS_FILTER_SET_SAMPLE_RATE, adapter_set_sample_rate},
	{	MS_FILTER_GET_SAMPLE_RATE, adapter_get_sample_rate},
167 168 169
	{	MS_FILTER_SET_NCHANNELS , adapter_set_nchannels },
	{	MS_FILTER_GET_NCHANNELS, adapter_get_nchannels },
	{	MS_CHANNEL_ADAPTER_SET_OUTPUT_NCHANNELS, adapter_set_out_nchannels },
Erwan Croze's avatar
Erwan Croze committed
170 171
	{	MS_CHANNEL_ADAPTER_GET_OUTPUT_NCHANNELS, adapter_get_out_nchannels },
	{	0,	NULL }
172 173
};

Erwan Croze's avatar
Erwan Croze committed
174
MSFilterDesc ms_channel_adapter_desc = {
175 176 177 178 179
	MS_CHANNEL_ADAPTER_ID,
	"MSChannelAdapter",
	N_("A filter that converts from mono to stereo and vice versa."),
	MS_FILTER_OTHER,
	NULL,
Erwan Croze's avatar
Erwan Croze committed
180
	2,
181 182
	1,
	adapter_init,
Erwan Croze's avatar
Erwan Croze committed
183
	adapter_preprocess,
184
	adapter_process,
Erwan Croze's avatar
Erwan Croze committed
185
	adapter_postprocess,
186 187 188 189 190
	adapter_uninit,
	methods
};

MS_FILTER_DESC_EXPORT(ms_channel_adapter_desc)