michael@0: /* Copyright (c) 2011 Xiph.Org Foundation michael@0: Written by Jean-Marc Valin */ michael@0: /* michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: michael@0: - Redistributions of source code must retain the above copyright michael@0: notice, this list of conditions and the following disclaimer. michael@0: michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER michael@0: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF michael@0: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING michael@0: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS michael@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "opus_multistream.h" michael@0: #include "opus.h" michael@0: #include "opus_private.h" michael@0: #include "stack_alloc.h" michael@0: #include michael@0: #include "float_cast.h" michael@0: #include "os_support.h" michael@0: #include "mathops.h" michael@0: #include "mdct.h" michael@0: #include "modes.h" michael@0: #include "bands.h" michael@0: #include "quant_bands.h" michael@0: michael@0: typedef struct { michael@0: int nb_streams; michael@0: int nb_coupled_streams; michael@0: unsigned char mapping[8]; michael@0: } VorbisLayout; michael@0: michael@0: /* Index is nb_channel-1*/ michael@0: static const VorbisLayout vorbis_mappings[8] = { michael@0: {1, 0, {0}}, /* 1: mono */ michael@0: {1, 1, {0, 1}}, /* 2: stereo */ michael@0: {2, 1, {0, 2, 1}}, /* 3: 1-d surround */ michael@0: {2, 2, {0, 1, 2, 3}}, /* 4: quadraphonic surround */ michael@0: {3, 2, {0, 4, 1, 2, 3}}, /* 5: 5-channel surround */ michael@0: {4, 2, {0, 4, 1, 2, 3, 5}}, /* 6: 5.1 surround */ michael@0: {4, 3, {0, 4, 1, 2, 3, 5, 6}}, /* 7: 6.1 surround */ michael@0: {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */ michael@0: }; michael@0: michael@0: typedef void (*opus_copy_channel_in_func)( michael@0: opus_val16 *dst, michael@0: int dst_stride, michael@0: const void *src, michael@0: int src_stride, michael@0: int src_channel, michael@0: int frame_size michael@0: ); michael@0: michael@0: struct OpusMSEncoder { michael@0: ChannelLayout layout; michael@0: int lfe_stream; michael@0: int application; michael@0: int variable_duration; michael@0: int surround; michael@0: opus_int32 bitrate_bps; michael@0: float subframe_mem[3]; michael@0: /* Encoder states go here */ michael@0: /* then opus_val32 window_mem[channels*120]; */ michael@0: /* then opus_val32 preemph_mem[channels]; */ michael@0: }; michael@0: michael@0: static opus_val32 *ms_get_preemph_mem(OpusMSEncoder *st) michael@0: { michael@0: int s; michael@0: char *ptr; michael@0: int coupled_size, mono_size; michael@0: michael@0: coupled_size = opus_encoder_get_size(2); michael@0: mono_size = opus_encoder_get_size(1); michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: } michael@0: return (opus_val32*)(ptr+st->layout.nb_channels*120*sizeof(opus_val32)); michael@0: } michael@0: michael@0: static opus_val32 *ms_get_window_mem(OpusMSEncoder *st) michael@0: { michael@0: int s; michael@0: char *ptr; michael@0: int coupled_size, mono_size; michael@0: michael@0: coupled_size = opus_encoder_get_size(2); michael@0: mono_size = opus_encoder_get_size(1); michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: } michael@0: return (opus_val32*)ptr; michael@0: } michael@0: michael@0: static int validate_encoder_layout(const ChannelLayout *layout) michael@0: { michael@0: int s; michael@0: for (s=0;snb_streams;s++) michael@0: { michael@0: if (s < layout->nb_coupled_streams) michael@0: { michael@0: if (get_left_channel(layout, s, -1)==-1) michael@0: return 0; michael@0: if (get_right_channel(layout, s, -1)==-1) michael@0: return 0; michael@0: } else { michael@0: if (get_mono_channel(layout, s, -1)==-1) michael@0: return 0; michael@0: } michael@0: } michael@0: return 1; michael@0: } michael@0: michael@0: static void channel_pos(int channels, int pos[8]) michael@0: { michael@0: /* Position in the mix: 0 don't mix, 1: left, 2: center, 3:right */ michael@0: if (channels==4) michael@0: { michael@0: pos[0]=1; michael@0: pos[1]=3; michael@0: pos[2]=1; michael@0: pos[3]=3; michael@0: } else if (channels==3||channels==5||channels==6) michael@0: { michael@0: pos[0]=1; michael@0: pos[1]=2; michael@0: pos[2]=3; michael@0: pos[3]=1; michael@0: pos[4]=3; michael@0: pos[5]=0; michael@0: } else if (channels==7) michael@0: { michael@0: pos[0]=1; michael@0: pos[1]=2; michael@0: pos[2]=3; michael@0: pos[3]=1; michael@0: pos[4]=3; michael@0: pos[5]=2; michael@0: pos[6]=0; michael@0: } else if (channels==8) michael@0: { michael@0: pos[0]=1; michael@0: pos[1]=2; michael@0: pos[2]=3; michael@0: pos[3]=1; michael@0: pos[4]=3; michael@0: pos[5]=1; michael@0: pos[6]=3; michael@0: pos[7]=0; michael@0: } michael@0: } michael@0: michael@0: #if 1 michael@0: /* Computes a rough approximation of log2(2^a + 2^b) */ michael@0: static opus_val16 logSum(opus_val16 a, opus_val16 b) michael@0: { michael@0: opus_val16 max; michael@0: opus_val32 diff; michael@0: opus_val16 frac; michael@0: static const opus_val16 diff_table[17] = { michael@0: QCONST16(0.5000000f, DB_SHIFT), QCONST16(0.2924813f, DB_SHIFT), QCONST16(0.1609640f, DB_SHIFT), QCONST16(0.0849625f, DB_SHIFT), michael@0: QCONST16(0.0437314f, DB_SHIFT), QCONST16(0.0221971f, DB_SHIFT), QCONST16(0.0111839f, DB_SHIFT), QCONST16(0.0056136f, DB_SHIFT), michael@0: QCONST16(0.0028123f, DB_SHIFT) michael@0: }; michael@0: int low; michael@0: if (a>b) michael@0: { michael@0: max = a; michael@0: diff = SUB32(EXTEND32(a),EXTEND32(b)); michael@0: } else { michael@0: max = b; michael@0: diff = SUB32(EXTEND32(b),EXTEND32(a)); michael@0: } michael@0: if (diff >= QCONST16(8.f, DB_SHIFT)) michael@0: return max; michael@0: #ifdef FIXED_POINT michael@0: low = SHR32(diff, DB_SHIFT-1); michael@0: frac = SHL16(diff - SHL16(low, DB_SHIFT-1), 16-DB_SHIFT); michael@0: #else michael@0: low = (int)floor(2*diff); michael@0: frac = 2*diff - low; michael@0: #endif michael@0: return max + diff_table[low] + MULT16_16_Q15(frac, SUB16(diff_table[low+1], diff_table[low])); michael@0: } michael@0: #else michael@0: opus_val16 logSum(opus_val16 a, opus_val16 b) michael@0: { michael@0: return log2(pow(4, a)+ pow(4, b))/2; michael@0: } michael@0: #endif michael@0: michael@0: void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *bandLogE, opus_val32 *mem, opus_val32 *preemph_mem, michael@0: int len, int overlap, int channels, int rate, opus_copy_channel_in_func copy_channel_in michael@0: ) michael@0: { michael@0: int c; michael@0: int i; michael@0: int LM; michael@0: int pos[8] = {0}; michael@0: int upsample; michael@0: int frame_size; michael@0: opus_val16 channel_offset; michael@0: opus_val32 bandE[21]; michael@0: opus_val16 maskLogE[3][21]; michael@0: VARDECL(opus_val32, in); michael@0: VARDECL(opus_val16, x); michael@0: VARDECL(opus_val32, freq); michael@0: SAVE_STACK; michael@0: michael@0: upsample = resampling_factor(rate); michael@0: frame_size = len*upsample; michael@0: michael@0: for (LM=0;LMmaxLM;LM++) michael@0: if (celt_mode->shortMdctSize<preemph, preemph_mem+c, 0); michael@0: clt_mdct_forward(&celt_mode->mdct, in, freq, celt_mode->window, overlap, celt_mode->maxLM-LM, 1); michael@0: if (upsample != 1) michael@0: { michael@0: int bound = len; michael@0: for (i=0;i=0;i--) michael@0: bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16(2.f, DB_SHIFT)); michael@0: if (pos[c]==1) michael@0: { michael@0: for (i=0;i<21;i++) michael@0: maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]); michael@0: } else if (pos[c]==3) michael@0: { michael@0: for (i=0;i<21;i++) michael@0: maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]); michael@0: } else if (pos[c]==2) michael@0: { michael@0: for (i=0;i<21;i++) michael@0: { michael@0: maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); michael@0: maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT)); michael@0: } michael@0: } michael@0: #if 0 michael@0: for (i=0;i<21;i++) michael@0: printf("%f ", bandLogE[21*c+i]); michael@0: float sum=0; michael@0: for (i=0;i<21;i++) michael@0: sum += bandLogE[21*c+i]; michael@0: printf("%f ", sum/21); michael@0: #endif michael@0: OPUS_COPY(mem+c*overlap, in+frame_size, overlap); michael@0: } michael@0: for (i=0;i<21;i++) michael@0: maskLogE[1][i] = MIN32(maskLogE[0][i],maskLogE[2][i]); michael@0: channel_offset = HALF16(celt_log2(QCONST32(2.f,14)/(channels-1))); michael@0: for (c=0;c<3;c++) michael@0: for (i=0;i<21;i++) michael@0: maskLogE[c][i] += channel_offset; michael@0: #if 0 michael@0: for (c=0;c<3;c++) michael@0: { michael@0: for (i=0;i<21;i++) michael@0: printf("%f ", maskLogE[c][i]); michael@0: } michael@0: #endif michael@0: for (c=0;cnb_streams||nb_coupled_streams<0)return 0; michael@0: coupled_size = opus_encoder_get_size(2); michael@0: mono_size = opus_encoder_get_size(1); michael@0: return align(sizeof(OpusMSEncoder)) michael@0: + nb_coupled_streams * align(coupled_size) michael@0: + (nb_streams-nb_coupled_streams) * align(mono_size); michael@0: } michael@0: michael@0: opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_family) michael@0: { michael@0: int nb_streams; michael@0: int nb_coupled_streams; michael@0: opus_int32 size; michael@0: michael@0: if (mapping_family==0) michael@0: { michael@0: if (channels==1) michael@0: { michael@0: nb_streams=1; michael@0: nb_coupled_streams=0; michael@0: } else if (channels==2) michael@0: { michael@0: nb_streams=1; michael@0: nb_coupled_streams=1; michael@0: } else michael@0: return 0; michael@0: } else if (mapping_family==1 && channels<=8 && channels>=1) michael@0: { michael@0: nb_streams=vorbis_mappings[channels-1].nb_streams; michael@0: nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; michael@0: } else if (mapping_family==255) michael@0: { michael@0: nb_streams=channels; michael@0: nb_coupled_streams=0; michael@0: } else michael@0: return 0; michael@0: size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams); michael@0: if (channels>2) michael@0: { michael@0: size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32)); michael@0: } michael@0: return size; michael@0: } michael@0: michael@0: michael@0: static int opus_multistream_encoder_init_impl( michael@0: OpusMSEncoder *st, michael@0: opus_int32 Fs, michael@0: int channels, michael@0: int streams, michael@0: int coupled_streams, michael@0: const unsigned char *mapping, michael@0: int application, michael@0: int surround michael@0: ) michael@0: { michael@0: int coupled_size; michael@0: int mono_size; michael@0: int i, ret; michael@0: char *ptr; michael@0: michael@0: if ((channels>255) || (channels<1) || (coupled_streams>streams) || michael@0: (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0)) michael@0: return OPUS_BAD_ARG; michael@0: michael@0: st->layout.nb_channels = channels; michael@0: st->layout.nb_streams = streams; michael@0: st->layout.nb_coupled_streams = coupled_streams; michael@0: st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0; michael@0: if (!surround) michael@0: st->lfe_stream = -1; michael@0: st->bitrate_bps = OPUS_AUTO; michael@0: st->application = application; michael@0: st->variable_duration = OPUS_FRAMESIZE_ARG; michael@0: for (i=0;ilayout.nb_channels;i++) michael@0: st->layout.mapping[i] = mapping[i]; michael@0: if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout)) michael@0: return OPUS_BAD_ARG; michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: coupled_size = opus_encoder_get_size(2); michael@0: mono_size = opus_encoder_get_size(1); michael@0: michael@0: for (i=0;ilayout.nb_coupled_streams;i++) michael@0: { michael@0: ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application); michael@0: if(ret!=OPUS_OK)return ret; michael@0: if (i==st->lfe_stream) michael@0: opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); michael@0: ptr += align(coupled_size); michael@0: } michael@0: for (;ilayout.nb_streams;i++) michael@0: { michael@0: ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 1, application); michael@0: if (i==st->lfe_stream) michael@0: opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1)); michael@0: if(ret!=OPUS_OK)return ret; michael@0: ptr += align(mono_size); michael@0: } michael@0: if (surround) michael@0: { michael@0: OPUS_CLEAR(ms_get_preemph_mem(st), channels); michael@0: OPUS_CLEAR(ms_get_window_mem(st), channels*120); michael@0: } michael@0: st->surround = surround; michael@0: return OPUS_OK; michael@0: } michael@0: michael@0: int opus_multistream_encoder_init( michael@0: OpusMSEncoder *st, michael@0: opus_int32 Fs, michael@0: int channels, michael@0: int streams, michael@0: int coupled_streams, michael@0: const unsigned char *mapping, michael@0: int application michael@0: ) michael@0: { michael@0: return opus_multistream_encoder_init_impl(st, Fs, channels, streams, coupled_streams, mapping, application, 0); michael@0: } michael@0: michael@0: int opus_multistream_surround_encoder_init( michael@0: OpusMSEncoder *st, michael@0: opus_int32 Fs, michael@0: int channels, michael@0: int mapping_family, michael@0: int *streams, michael@0: int *coupled_streams, michael@0: unsigned char *mapping, michael@0: int application michael@0: ) michael@0: { michael@0: if ((channels>255) || (channels<1)) michael@0: return OPUS_BAD_ARG; michael@0: st->lfe_stream = -1; michael@0: if (mapping_family==0) michael@0: { michael@0: if (channels==1) michael@0: { michael@0: *streams=1; michael@0: *coupled_streams=0; michael@0: mapping[0]=0; michael@0: } else if (channels==2) michael@0: { michael@0: *streams=1; michael@0: *coupled_streams=1; michael@0: mapping[0]=0; michael@0: mapping[1]=1; michael@0: } else michael@0: return OPUS_UNIMPLEMENTED; michael@0: } else if (mapping_family==1 && channels<=8 && channels>=1) michael@0: { michael@0: int i; michael@0: *streams=vorbis_mappings[channels-1].nb_streams; michael@0: *coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams; michael@0: for (i=0;i=6) michael@0: st->lfe_stream = *streams-1; michael@0: } else if (mapping_family==255) michael@0: { michael@0: int i; michael@0: *streams=channels; michael@0: *coupled_streams=0; michael@0: for(i=0;i2&&mapping_family==1); michael@0: } michael@0: michael@0: OpusMSEncoder *opus_multistream_encoder_create( michael@0: opus_int32 Fs, michael@0: int channels, michael@0: int streams, michael@0: int coupled_streams, michael@0: const unsigned char *mapping, michael@0: int application, michael@0: int *error michael@0: ) michael@0: { michael@0: int ret; michael@0: OpusMSEncoder *st; michael@0: if ((channels>255) || (channels<1) || (coupled_streams>streams) || michael@0: (coupled_streams+streams>255) || (streams<1) || (coupled_streams<0)) michael@0: { michael@0: if (error) michael@0: *error = OPUS_BAD_ARG; michael@0: return NULL; michael@0: } michael@0: st = (OpusMSEncoder *)opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams)); michael@0: if (st==NULL) michael@0: { michael@0: if (error) michael@0: *error = OPUS_ALLOC_FAIL; michael@0: return NULL; michael@0: } michael@0: ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application); michael@0: if (ret != OPUS_OK) michael@0: { michael@0: opus_free(st); michael@0: st = NULL; michael@0: } michael@0: if (error) michael@0: *error = ret; michael@0: return st; michael@0: } michael@0: michael@0: OpusMSEncoder *opus_multistream_surround_encoder_create( michael@0: opus_int32 Fs, michael@0: int channels, michael@0: int mapping_family, michael@0: int *streams, michael@0: int *coupled_streams, michael@0: unsigned char *mapping, michael@0: int application, michael@0: int *error michael@0: ) michael@0: { michael@0: int ret; michael@0: OpusMSEncoder *st; michael@0: if ((channels>255) || (channels<1)) michael@0: { michael@0: if (error) michael@0: *error = OPUS_BAD_ARG; michael@0: return NULL; michael@0: } michael@0: st = (OpusMSEncoder *)opus_alloc(opus_multistream_surround_encoder_get_size(channels, mapping_family)); michael@0: if (st==NULL) michael@0: { michael@0: if (error) michael@0: *error = OPUS_ALLOC_FAIL; michael@0: return NULL; michael@0: } michael@0: ret = opus_multistream_surround_encoder_init(st, Fs, channels, mapping_family, streams, coupled_streams, mapping, application); michael@0: if (ret != OPUS_OK) michael@0: { michael@0: opus_free(st); michael@0: st = NULL; michael@0: } michael@0: if (error) michael@0: *error = ret; michael@0: return st; michael@0: } michael@0: michael@0: static void surround_rate_allocation( michael@0: OpusMSEncoder *st, michael@0: opus_int32 *rate, michael@0: int frame_size michael@0: ) michael@0: { michael@0: int i; michael@0: opus_int32 channel_rate; michael@0: opus_int32 Fs; michael@0: char *ptr; michael@0: int stream_offset; michael@0: int lfe_offset; michael@0: int coupled_ratio; /* Q8 */ michael@0: int lfe_ratio; /* Q8 */ michael@0: michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); michael@0: michael@0: if (st->bitrate_bps > st->layout.nb_channels*40000) michael@0: stream_offset = 20000; michael@0: else michael@0: stream_offset = st->bitrate_bps/st->layout.nb_channels/2; michael@0: stream_offset += 60*(Fs/frame_size-50); michael@0: /* We start by giving each stream (coupled or uncoupled) the same bitrate. michael@0: This models the main saving of coupled channels over uncoupled. */ michael@0: /* The LFE stream is an exception to the above and gets fewer bits. */ michael@0: lfe_offset = 3500 + 60*(Fs/frame_size-50); michael@0: /* Coupled streams get twice the mono rate after the first 20 kb/s. */ michael@0: coupled_ratio = 512; michael@0: /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of mono */ michael@0: lfe_ratio = 32; michael@0: michael@0: /* Compute bitrate allocation between streams */ michael@0: if (st->bitrate_bps==OPUS_AUTO) michael@0: { michael@0: channel_rate = Fs+60*Fs/frame_size; michael@0: } else if (st->bitrate_bps==OPUS_BITRATE_MAX) michael@0: { michael@0: channel_rate = 300000; michael@0: } else { michael@0: int nb_lfe; michael@0: int nb_uncoupled; michael@0: int nb_coupled; michael@0: int total; michael@0: nb_lfe = (st->lfe_stream!=-1); michael@0: nb_coupled = st->layout.nb_coupled_streams; michael@0: nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe; michael@0: total = (nb_uncoupled<<8) /* mono */ michael@0: + coupled_ratio*nb_coupled /* stereo */ michael@0: + nb_lfe*lfe_ratio; michael@0: channel_rate = 256*(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_coupled+nb_uncoupled))/total; michael@0: } michael@0: #ifndef FIXED_POINT michael@0: if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50) michael@0: { michael@0: opus_int32 bonus; michael@0: bonus = 60*(Fs/frame_size-50); michael@0: channel_rate += bonus; michael@0: } michael@0: #endif michael@0: michael@0: for (i=0;ilayout.nb_streams;i++) michael@0: { michael@0: if (ilayout.nb_coupled_streams) michael@0: rate[i] = stream_offset+(channel_rate*coupled_ratio>>8); michael@0: else if (i!=st->lfe_stream) michael@0: rate[i] = stream_offset+channel_rate; michael@0: else michael@0: rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8); michael@0: } michael@0: } michael@0: michael@0: /* Max size in case the encoder decides to return three frames */ michael@0: #define MS_FRAME_TMP (3*1275+7) michael@0: static int opus_multistream_encode_native michael@0: ( michael@0: OpusMSEncoder *st, michael@0: opus_copy_channel_in_func copy_channel_in, michael@0: const void *pcm, michael@0: int analysis_frame_size, michael@0: unsigned char *data, michael@0: opus_int32 max_data_bytes, michael@0: int lsb_depth, michael@0: downmix_func downmix michael@0: ) michael@0: { michael@0: opus_int32 Fs; michael@0: int coupled_size; michael@0: int mono_size; michael@0: int s; michael@0: char *ptr; michael@0: int tot_size; michael@0: VARDECL(opus_val16, buf); michael@0: VARDECL(opus_val16, bandSMR); michael@0: unsigned char tmp_data[MS_FRAME_TMP]; michael@0: OpusRepacketizer rp; michael@0: opus_int32 vbr; michael@0: const CELTMode *celt_mode; michael@0: opus_int32 bitrates[256]; michael@0: opus_val16 bandLogE[42]; michael@0: opus_val32 *mem = NULL; michael@0: opus_val32 *preemph_mem=NULL; michael@0: int frame_size; michael@0: ALLOC_STACK; michael@0: michael@0: if (st->surround) michael@0: { michael@0: preemph_mem = ms_get_preemph_mem(st); michael@0: mem = ms_get_window_mem(st); michael@0: } michael@0: michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs)); michael@0: opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr)); michael@0: opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode)); michael@0: michael@0: { michael@0: opus_int32 delay_compensation; michael@0: int channels; michael@0: michael@0: channels = st->layout.nb_streams + st->layout.nb_coupled_streams; michael@0: opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation)); michael@0: delay_compensation -= Fs/400; michael@0: frame_size = compute_frame_size(pcm, analysis_frame_size, michael@0: st->variable_duration, channels, Fs, st->bitrate_bps, michael@0: delay_compensation, downmix michael@0: #ifndef DISABLE_FLOAT_API michael@0: , st->subframe_mem michael@0: #endif michael@0: ); michael@0: } michael@0: michael@0: if (400*frame_size < Fs) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_BAD_ARG; michael@0: } michael@0: /* Validate frame_size before using it to allocate stack space. michael@0: This mirrors the checks in opus_encode[_float](). */ michael@0: if (400*frame_size != Fs && 200*frame_size != Fs && michael@0: 100*frame_size != Fs && 50*frame_size != Fs && michael@0: 25*frame_size != Fs && 50*frame_size != 3*Fs) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_BAD_ARG; michael@0: } michael@0: ALLOC(buf, 2*frame_size, opus_val16); michael@0: coupled_size = opus_encoder_get_size(2); michael@0: mono_size = opus_encoder_get_size(1); michael@0: michael@0: ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16); michael@0: if (st->surround) michael@0: { michael@0: surround_analysis(celt_mode, pcm, bandSMR, mem, preemph_mem, frame_size, 120, st->layout.nb_channels, Fs, copy_channel_in); michael@0: } michael@0: michael@0: if (max_data_bytes < 4*st->layout.nb_streams-1) michael@0: { michael@0: RESTORE_STACK; michael@0: return OPUS_BUFFER_TOO_SMALL; michael@0: } michael@0: michael@0: /* Compute bitrate allocation between streams (this could be a lot better) */ michael@0: surround_rate_allocation(st, bitrates, frame_size); michael@0: michael@0: if (!vbr) michael@0: max_data_bytes = IMIN(max_data_bytes, 3*st->bitrate_bps/(3*8*Fs/frame_size)); michael@0: michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: OpusEncoder *enc; michael@0: enc = (OpusEncoder*)ptr; michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s])); michael@0: if (st->surround) michael@0: { michael@0: opus_int32 equiv_rate; michael@0: equiv_rate = st->bitrate_bps; michael@0: if (frame_size*50 < Fs) michael@0: equiv_rate -= 60*(Fs/frame_size - 50)*st->layout.nb_channels; michael@0: if (equiv_rate > 10000*st->layout.nb_channels) michael@0: opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND)); michael@0: else if (equiv_rate > 7000*st->layout.nb_channels) michael@0: opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND)); michael@0: else if (equiv_rate > 5000*st->layout.nb_channels) michael@0: opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND)); michael@0: else michael@0: opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); michael@0: if (s < st->layout.nb_coupled_streams) michael@0: { michael@0: /* To preserve the spatial image, force stereo CELT on coupled streams */ michael@0: opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY)); michael@0: opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(2)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: ptr = (char*)st + align(sizeof(OpusMSEncoder)); michael@0: /* Counting ToC */ michael@0: tot_size = 0; michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: OpusEncoder *enc; michael@0: int len; michael@0: int curr_max; michael@0: int c1, c2; michael@0: michael@0: opus_repacketizer_init(&rp); michael@0: enc = (OpusEncoder*)ptr; michael@0: if (s < st->layout.nb_coupled_streams) michael@0: { michael@0: int i; michael@0: int left, right; michael@0: left = get_left_channel(&st->layout, s, -1); michael@0: right = get_right_channel(&st->layout, s, -1); michael@0: (*copy_channel_in)(buf, 2, michael@0: pcm, st->layout.nb_channels, left, frame_size); michael@0: (*copy_channel_in)(buf+1, 2, michael@0: pcm, st->layout.nb_channels, right, frame_size); michael@0: ptr += align(coupled_size); michael@0: if (st->surround) michael@0: { michael@0: for (i=0;i<21;i++) michael@0: { michael@0: bandLogE[i] = bandSMR[21*left+i]; michael@0: bandLogE[21+i] = bandSMR[21*right+i]; michael@0: } michael@0: } michael@0: c1 = left; michael@0: c2 = right; michael@0: } else { michael@0: int i; michael@0: int chan = get_mono_channel(&st->layout, s, -1); michael@0: (*copy_channel_in)(buf, 1, michael@0: pcm, st->layout.nb_channels, chan, frame_size); michael@0: ptr += align(mono_size); michael@0: if (st->surround) michael@0: { michael@0: for (i=0;i<21;i++) michael@0: bandLogE[i] = bandSMR[21*chan+i]; michael@0: } michael@0: c1 = chan; michael@0: c2 = -1; michael@0: } michael@0: if (st->surround) michael@0: opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE)); michael@0: /* number of bytes left (+Toc) */ michael@0: curr_max = max_data_bytes - tot_size; michael@0: /* Reserve three bytes for the last stream and four for the others */ michael@0: curr_max -= IMAX(0,4*(st->layout.nb_streams-s-1)-1); michael@0: curr_max = IMIN(curr_max,MS_FRAME_TMP); michael@0: if (!vbr && s == st->layout.nb_streams-1) michael@0: opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size))); michael@0: len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_depth, michael@0: pcm, analysis_frame_size, c1, c2, st->layout.nb_channels, downmix); michael@0: if (len<0) michael@0: { michael@0: RESTORE_STACK; michael@0: return len; michael@0: } michael@0: /* We need to use the repacketizer to add the self-delimiting lengths michael@0: while taking into account the fact that the encoder can now return michael@0: more than one frame at a time (e.g. 60 ms CELT-only) */ michael@0: opus_repacketizer_cat(&rp, tmp_data, len); michael@0: len = opus_repacketizer_out_range_impl(&rp, 0, opus_repacketizer_get_nb_frames(&rp), michael@0: data, max_data_bytes-tot_size, s != st->layout.nb_streams-1, !vbr && s == st->layout.nb_streams-1); michael@0: data += len; michael@0: tot_size += len; michael@0: } michael@0: /*printf("\n");*/ michael@0: RESTORE_STACK; michael@0: return tot_size; michael@0: } michael@0: michael@0: #if !defined(DISABLE_FLOAT_API) michael@0: static void opus_copy_channel_in_float( michael@0: opus_val16 *dst, michael@0: int dst_stride, michael@0: const void *src, michael@0: int src_stride, michael@0: int src_channel, michael@0: int frame_size michael@0: ) michael@0: { michael@0: const float *float_src; michael@0: opus_int32 i; michael@0: float_src = (const float *)src; michael@0: for (i=0;ibitrate_bps = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_BITRATE_REQUEST: michael@0: { michael@0: int s; michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = 0; michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: opus_int32 rate; michael@0: OpusEncoder *enc; michael@0: enc = (OpusEncoder*)ptr; michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: opus_encoder_ctl(enc, request, &rate); michael@0: *value += rate; michael@0: } michael@0: } michael@0: break; michael@0: case OPUS_GET_LSB_DEPTH_REQUEST: michael@0: case OPUS_GET_VBR_REQUEST: michael@0: case OPUS_GET_APPLICATION_REQUEST: michael@0: case OPUS_GET_BANDWIDTH_REQUEST: michael@0: case OPUS_GET_COMPLEXITY_REQUEST: michael@0: case OPUS_GET_PACKET_LOSS_PERC_REQUEST: michael@0: case OPUS_GET_DTX_REQUEST: michael@0: case OPUS_GET_VOICE_RATIO_REQUEST: michael@0: case OPUS_GET_VBR_CONSTRAINT_REQUEST: michael@0: case OPUS_GET_SIGNAL_REQUEST: michael@0: case OPUS_GET_LOOKAHEAD_REQUEST: michael@0: case OPUS_GET_SAMPLE_RATE_REQUEST: michael@0: case OPUS_GET_INBAND_FEC_REQUEST: michael@0: case OPUS_GET_FORCE_CHANNELS_REQUEST: michael@0: case OPUS_GET_PREDICTION_DISABLED_REQUEST: michael@0: { michael@0: OpusEncoder *enc; michael@0: /* For int32* GET params, just query the first stream */ michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: enc = (OpusEncoder*)ptr; michael@0: ret = opus_encoder_ctl(enc, request, value); michael@0: } michael@0: break; michael@0: case OPUS_GET_FINAL_RANGE_REQUEST: michael@0: { michael@0: int s; michael@0: opus_uint32 *value = va_arg(ap, opus_uint32*); michael@0: opus_uint32 tmp; michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value=0; michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: OpusEncoder *enc; michael@0: enc = (OpusEncoder*)ptr; michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: ret = opus_encoder_ctl(enc, request, &tmp); michael@0: if (ret != OPUS_OK) break; michael@0: *value ^= tmp; michael@0: } michael@0: } michael@0: break; michael@0: case OPUS_SET_LSB_DEPTH_REQUEST: michael@0: case OPUS_SET_COMPLEXITY_REQUEST: michael@0: case OPUS_SET_VBR_REQUEST: michael@0: case OPUS_SET_VBR_CONSTRAINT_REQUEST: michael@0: case OPUS_SET_MAX_BANDWIDTH_REQUEST: michael@0: case OPUS_SET_BANDWIDTH_REQUEST: michael@0: case OPUS_SET_SIGNAL_REQUEST: michael@0: case OPUS_SET_APPLICATION_REQUEST: michael@0: case OPUS_SET_INBAND_FEC_REQUEST: michael@0: case OPUS_SET_PACKET_LOSS_PERC_REQUEST: michael@0: case OPUS_SET_DTX_REQUEST: michael@0: case OPUS_SET_FORCE_MODE_REQUEST: michael@0: case OPUS_SET_FORCE_CHANNELS_REQUEST: michael@0: case OPUS_SET_PREDICTION_DISABLED_REQUEST: michael@0: { michael@0: int s; michael@0: /* This works for int32 params */ michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: OpusEncoder *enc; michael@0: michael@0: enc = (OpusEncoder*)ptr; michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: ret = opus_encoder_ctl(enc, request, value); michael@0: if (ret != OPUS_OK) michael@0: break; michael@0: } michael@0: } michael@0: break; michael@0: case OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST: michael@0: { michael@0: int s; michael@0: opus_int32 stream_id; michael@0: OpusEncoder **value; michael@0: stream_id = va_arg(ap, opus_int32); michael@0: if (stream_id<0 || stream_id >= st->layout.nb_streams) michael@0: ret = OPUS_BAD_ARG; michael@0: value = va_arg(ap, OpusEncoder**); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: for (s=0;slayout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: } michael@0: *value = (OpusEncoder*)ptr; michael@0: } michael@0: break; michael@0: case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: michael@0: { michael@0: opus_int32 value = va_arg(ap, opus_int32); michael@0: st->variable_duration = value; michael@0: } michael@0: break; michael@0: case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST: michael@0: { michael@0: opus_int32 *value = va_arg(ap, opus_int32*); michael@0: if (!value) michael@0: { michael@0: goto bad_arg; michael@0: } michael@0: *value = st->variable_duration; michael@0: } michael@0: break; michael@0: case OPUS_RESET_STATE: michael@0: { michael@0: int s; michael@0: st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0; michael@0: if (st->surround) michael@0: { michael@0: OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels); michael@0: OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120); michael@0: } michael@0: for (s=0;slayout.nb_streams;s++) michael@0: { michael@0: OpusEncoder *enc; michael@0: enc = (OpusEncoder*)ptr; michael@0: if (s < st->layout.nb_coupled_streams) michael@0: ptr += align(coupled_size); michael@0: else michael@0: ptr += align(mono_size); michael@0: ret = opus_encoder_ctl(enc, OPUS_RESET_STATE); michael@0: if (ret != OPUS_OK) michael@0: break; michael@0: } michael@0: } michael@0: break; michael@0: default: michael@0: ret = OPUS_UNIMPLEMENTED; michael@0: break; michael@0: } michael@0: michael@0: va_end(ap); michael@0: return ret; michael@0: bad_arg: michael@0: va_end(ap); michael@0: return OPUS_BAD_ARG; michael@0: } michael@0: michael@0: void opus_multistream_encoder_destroy(OpusMSEncoder *st) michael@0: { michael@0: opus_free(st); michael@0: }