chanadapt.c 3.51 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) 2010  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 25 26 27 28 29 30 31 32
*/

#include "mediastreamer2/msfilter.h"
#include "mediastreamer2/mschanadapter.h"

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

typedef struct AdapterState{
	int inputchans;
	int outputchans;
}AdapterState;

static void adapter_init(MSFilter *f){
33
	AdapterState *s=ms_new0(AdapterState,1);
34 35 36 37 38 39 40 41 42 43 44 45
	s->inputchans=1;
	s->outputchans=1;
	f->data=s;
}

static void adapter_uninit(MSFilter *f){
	ms_free(f->data);
}

static void adapter_process(MSFilter *f){
	AdapterState *s=(AdapterState*)f->data;
	mblk_t *im,*om;
46
	size_t msgsize;
47 48 49 50 51 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	
	while((im=ms_queue_get(f->inputs[0]))!=NULL){
		if (s->inputchans==s->outputchans){
			ms_queue_put(f->outputs[0],im);
		}else if (s->inputchans==2){
			msgsize=msgdsize(im)/2;
			om=allocb(msgsize,0);
			for (;im->b_rptr<im->b_wptr;im->b_rptr+=4,om->b_wptr+=2){
				*(int16_t*)om->b_wptr=*(int16_t*)im->b_rptr;
			}
			ms_queue_put(f->outputs[0],om);
			freemsg(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);
		}
	}
}

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

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

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

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

static MSFilterMethod methods[]={
	{	MS_FILTER_SET_NCHANNELS , adapter_set_nchannels },
	{	MS_FILTER_GET_NCHANNELS, adapter_get_nchannels },
	{	MS_CHANNEL_ADAPTER_SET_OUTPUT_NCHANNELS, adapter_set_out_nchannels },
	{  MS_CHANNEL_ADAPTER_GET_OUTPUT_NCHANNELS, adapter_get_out_nchannels },
	{ 0,	NULL }
};


#ifdef _MSC_VER

MSFilterDesc ms_channel_adapter_desc={
	MS_CHANNEL_ADAPTER_ID,
	"MSChannelAdapter",
	N_("A filter that converts from mono to stereo and vice versa."),
	MS_FILTER_OTHER,
	NULL,
	1,
	1,
	adapter_init,
	NULL,
	adapter_process,
	NULL,
	adapter_uninit,
	methods
};

#else

MSFilterDesc ms_channel_adapter_desc={
	.id=MS_CHANNEL_ADAPTER_ID,
	.name="MSChannelAdapter",
	.text=N_("A filter that converts from mono to stereo and vice versa."),
	.category=MS_FILTER_OTHER,
	.ninputs=1,
	.noutputs=1,
	.init=adapter_init,
	.process=adapter_process,
	.uninit=adapter_uninit,
	.methods=methods
};
#endif

MS_FILTER_DESC_EXPORT(ms_channel_adapter_desc)