opus_multistream_encoder.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. /* Copyright (c) 2011 Xiph.Org Foundation
  2. Written by Jean-Marc Valin */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  16. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "opus_multistream.h"
  28. #include "opus.h"
  29. #include "opus_private.h"
  30. #include "stack_alloc.h"
  31. #include <stdarg.h>
  32. #include "float_cast.h"
  33. #include "os_support.h"
  34. #include "mathops.h"
  35. #include "mdct.h"
  36. #include "modes.h"
  37. #include "bands.h"
  38. #include "quant_bands.h"
  39. #include "pitch.h"
  40. typedef struct {
  41. int nb_streams;
  42. int nb_coupled_streams;
  43. unsigned char mapping[8];
  44. } VorbisLayout;
  45. /* Index is nb_channel-1*/
  46. static const VorbisLayout vorbis_mappings[8] = {
  47. {1, 0, {0}}, /* 1: mono */
  48. {1, 1, {0, 1}}, /* 2: stereo */
  49. {2, 1, {0, 2, 1}}, /* 3: 1-d surround */
  50. {2, 2, {0, 1, 2, 3}}, /* 4: quadraphonic surround */
  51. {3, 2, {0, 4, 1, 2, 3}}, /* 5: 5-channel surround */
  52. {4, 2, {0, 4, 1, 2, 3, 5}}, /* 6: 5.1 surround */
  53. {4, 3, {0, 4, 1, 2, 3, 5, 6}}, /* 7: 6.1 surround */
  54. {5, 3, {0, 6, 1, 2, 3, 4, 5, 7}}, /* 8: 7.1 surround */
  55. };
  56. typedef void (*opus_copy_channel_in_func)(
  57. opus_val16 *dst,
  58. int dst_stride,
  59. const void *src,
  60. int src_stride,
  61. int src_channel,
  62. int frame_size
  63. );
  64. typedef enum {
  65. MAPPING_TYPE_NONE,
  66. MAPPING_TYPE_SURROUND
  67. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  68. , /* Do not include comma at end of enumerator list */
  69. MAPPING_TYPE_AMBISONICS
  70. #endif
  71. } MappingType;
  72. struct OpusMSEncoder {
  73. ChannelLayout layout;
  74. int arch;
  75. int lfe_stream;
  76. int application;
  77. int variable_duration;
  78. MappingType mapping_type;
  79. opus_int32 bitrate_bps;
  80. float subframe_mem[3];
  81. /* Encoder states go here */
  82. /* then opus_val32 window_mem[channels*120]; */
  83. /* then opus_val32 preemph_mem[channels]; */
  84. };
  85. static opus_val32 *ms_get_preemph_mem(OpusMSEncoder *st)
  86. {
  87. int s;
  88. char *ptr;
  89. int coupled_size, mono_size;
  90. coupled_size = opus_encoder_get_size(2);
  91. mono_size = opus_encoder_get_size(1);
  92. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  93. for (s=0;s<st->layout.nb_streams;s++)
  94. {
  95. if (s < st->layout.nb_coupled_streams)
  96. ptr += align(coupled_size);
  97. else
  98. ptr += align(mono_size);
  99. }
  100. /* void* cast avoids clang -Wcast-align warning */
  101. return (opus_val32*)(void*)(ptr+st->layout.nb_channels*120*sizeof(opus_val32));
  102. }
  103. static opus_val32 *ms_get_window_mem(OpusMSEncoder *st)
  104. {
  105. int s;
  106. char *ptr;
  107. int coupled_size, mono_size;
  108. coupled_size = opus_encoder_get_size(2);
  109. mono_size = opus_encoder_get_size(1);
  110. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  111. for (s=0;s<st->layout.nb_streams;s++)
  112. {
  113. if (s < st->layout.nb_coupled_streams)
  114. ptr += align(coupled_size);
  115. else
  116. ptr += align(mono_size);
  117. }
  118. /* void* cast avoids clang -Wcast-align warning */
  119. return (opus_val32*)(void*)ptr;
  120. }
  121. static int validate_encoder_layout(const ChannelLayout *layout)
  122. {
  123. int s;
  124. for (s=0;s<layout->nb_streams;s++)
  125. {
  126. if (s < layout->nb_coupled_streams)
  127. {
  128. if (get_left_channel(layout, s, -1)==-1)
  129. return 0;
  130. if (get_right_channel(layout, s, -1)==-1)
  131. return 0;
  132. } else {
  133. if (get_mono_channel(layout, s, -1)==-1)
  134. return 0;
  135. }
  136. }
  137. return 1;
  138. }
  139. static void channel_pos(int channels, int pos[8])
  140. {
  141. /* Position in the mix: 0 don't mix, 1: left, 2: center, 3:right */
  142. if (channels==4)
  143. {
  144. pos[0]=1;
  145. pos[1]=3;
  146. pos[2]=1;
  147. pos[3]=3;
  148. } else if (channels==3||channels==5||channels==6)
  149. {
  150. pos[0]=1;
  151. pos[1]=2;
  152. pos[2]=3;
  153. pos[3]=1;
  154. pos[4]=3;
  155. pos[5]=0;
  156. } else if (channels==7)
  157. {
  158. pos[0]=1;
  159. pos[1]=2;
  160. pos[2]=3;
  161. pos[3]=1;
  162. pos[4]=3;
  163. pos[5]=2;
  164. pos[6]=0;
  165. } else if (channels==8)
  166. {
  167. pos[0]=1;
  168. pos[1]=2;
  169. pos[2]=3;
  170. pos[3]=1;
  171. pos[4]=3;
  172. pos[5]=1;
  173. pos[6]=3;
  174. pos[7]=0;
  175. }
  176. }
  177. #if 1
  178. /* Computes a rough approximation of log2(2^a + 2^b) */
  179. static opus_val16 logSum(opus_val16 a, opus_val16 b)
  180. {
  181. opus_val16 max;
  182. opus_val32 diff;
  183. opus_val16 frac;
  184. static const opus_val16 diff_table[17] = {
  185. QCONST16(0.5000000f, DB_SHIFT), QCONST16(0.2924813f, DB_SHIFT), QCONST16(0.1609640f, DB_SHIFT), QCONST16(0.0849625f, DB_SHIFT),
  186. QCONST16(0.0437314f, DB_SHIFT), QCONST16(0.0221971f, DB_SHIFT), QCONST16(0.0111839f, DB_SHIFT), QCONST16(0.0056136f, DB_SHIFT),
  187. QCONST16(0.0028123f, DB_SHIFT)
  188. };
  189. int low;
  190. if (a>b)
  191. {
  192. max = a;
  193. diff = SUB32(EXTEND32(a),EXTEND32(b));
  194. } else {
  195. max = b;
  196. diff = SUB32(EXTEND32(b),EXTEND32(a));
  197. }
  198. if (!(diff < QCONST16(8.f, DB_SHIFT))) /* inverted to catch NaNs */
  199. return max;
  200. #ifdef FIXED_POINT
  201. low = SHR32(diff, DB_SHIFT-1);
  202. frac = SHL16(diff - SHL16(low, DB_SHIFT-1), 16-DB_SHIFT);
  203. #else
  204. low = (int)floor(2*diff);
  205. frac = 2*diff - low;
  206. #endif
  207. return max + diff_table[low] + MULT16_16_Q15(frac, SUB16(diff_table[low+1], diff_table[low]));
  208. }
  209. #else
  210. opus_val16 logSum(opus_val16 a, opus_val16 b)
  211. {
  212. return log2(pow(4, a)+ pow(4, b))/2;
  213. }
  214. #endif
  215. void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *bandLogE, opus_val32 *mem, opus_val32 *preemph_mem,
  216. int len, int overlap, int channels, int rate, opus_copy_channel_in_func copy_channel_in, int arch
  217. )
  218. {
  219. int c;
  220. int i;
  221. int LM;
  222. int pos[8] = {0};
  223. int upsample;
  224. int frame_size;
  225. opus_val16 channel_offset;
  226. opus_val32 bandE[21];
  227. opus_val16 maskLogE[3][21];
  228. VARDECL(opus_val32, in);
  229. VARDECL(opus_val16, x);
  230. VARDECL(opus_val32, freq);
  231. SAVE_STACK;
  232. upsample = resampling_factor(rate);
  233. frame_size = len*upsample;
  234. /* LM = log2(frame_size / 120) */
  235. for (LM=0;LM<celt_mode->maxLM;LM++)
  236. if (celt_mode->shortMdctSize<<LM==frame_size)
  237. break;
  238. ALLOC(in, frame_size+overlap, opus_val32);
  239. ALLOC(x, len, opus_val16);
  240. ALLOC(freq, frame_size, opus_val32);
  241. channel_pos(channels, pos);
  242. for (c=0;c<3;c++)
  243. for (i=0;i<21;i++)
  244. maskLogE[c][i] = -QCONST16(28.f, DB_SHIFT);
  245. for (c=0;c<channels;c++)
  246. {
  247. OPUS_COPY(in, mem+c*overlap, overlap);
  248. (*copy_channel_in)(x, 1, pcm, channels, c, len);
  249. celt_preemphasis(x, in+overlap, frame_size, 1, upsample, celt_mode->preemph, preemph_mem+c, 0);
  250. #ifndef FIXED_POINT
  251. {
  252. opus_val32 sum;
  253. sum = celt_inner_prod(in, in, frame_size+overlap, 0);
  254. /* This should filter out both NaNs and ridiculous signals that could
  255. cause NaNs further down. */
  256. if (!(sum < 1e18f) || celt_isnan(sum))
  257. {
  258. OPUS_CLEAR(in, frame_size+overlap);
  259. preemph_mem[c] = 0;
  260. }
  261. }
  262. #endif
  263. clt_mdct_forward(&celt_mode->mdct, in, freq, celt_mode->window,
  264. overlap, celt_mode->maxLM-LM, 1, arch);
  265. if (upsample != 1)
  266. {
  267. int bound = len;
  268. for (i=0;i<bound;i++)
  269. freq[i] *= upsample;
  270. for (;i<frame_size;i++)
  271. freq[i] = 0;
  272. }
  273. compute_band_energies(celt_mode, freq, bandE, 21, 1, LM);
  274. amp2Log2(celt_mode, 21, 21, bandE, bandLogE+21*c, 1);
  275. /* Apply spreading function with -6 dB/band going up and -12 dB/band going down. */
  276. for (i=1;i<21;i++)
  277. bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i-1]-QCONST16(1.f, DB_SHIFT));
  278. for (i=19;i>=0;i--)
  279. bandLogE[21*c+i] = MAX16(bandLogE[21*c+i], bandLogE[21*c+i+1]-QCONST16(2.f, DB_SHIFT));
  280. if (pos[c]==1)
  281. {
  282. for (i=0;i<21;i++)
  283. maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]);
  284. } else if (pos[c]==3)
  285. {
  286. for (i=0;i<21;i++)
  287. maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]);
  288. } else if (pos[c]==2)
  289. {
  290. for (i=0;i<21;i++)
  291. {
  292. maskLogE[0][i] = logSum(maskLogE[0][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT));
  293. maskLogE[2][i] = logSum(maskLogE[2][i], bandLogE[21*c+i]-QCONST16(.5f, DB_SHIFT));
  294. }
  295. }
  296. #if 0
  297. for (i=0;i<21;i++)
  298. printf("%f ", bandLogE[21*c+i]);
  299. float sum=0;
  300. for (i=0;i<21;i++)
  301. sum += bandLogE[21*c+i];
  302. printf("%f ", sum/21);
  303. #endif
  304. OPUS_COPY(mem+c*overlap, in+frame_size, overlap);
  305. }
  306. for (i=0;i<21;i++)
  307. maskLogE[1][i] = MIN32(maskLogE[0][i],maskLogE[2][i]);
  308. channel_offset = HALF16(celt_log2(QCONST32(2.f,14)/(channels-1)));
  309. for (c=0;c<3;c++)
  310. for (i=0;i<21;i++)
  311. maskLogE[c][i] += channel_offset;
  312. #if 0
  313. for (c=0;c<3;c++)
  314. {
  315. for (i=0;i<21;i++)
  316. printf("%f ", maskLogE[c][i]);
  317. }
  318. #endif
  319. for (c=0;c<channels;c++)
  320. {
  321. opus_val16 *mask;
  322. if (pos[c]!=0)
  323. {
  324. mask = &maskLogE[pos[c]-1][0];
  325. for (i=0;i<21;i++)
  326. bandLogE[21*c+i] = bandLogE[21*c+i] - mask[i];
  327. } else {
  328. for (i=0;i<21;i++)
  329. bandLogE[21*c+i] = 0;
  330. }
  331. #if 0
  332. for (i=0;i<21;i++)
  333. printf("%f ", bandLogE[21*c+i]);
  334. printf("\n");
  335. #endif
  336. #if 0
  337. float sum=0;
  338. for (i=0;i<21;i++)
  339. sum += bandLogE[21*c+i];
  340. printf("%f ", sum/(float)QCONST32(21.f, DB_SHIFT));
  341. printf("\n");
  342. #endif
  343. }
  344. RESTORE_STACK;
  345. }
  346. opus_int32 opus_multistream_encoder_get_size(int nb_streams, int nb_coupled_streams)
  347. {
  348. int coupled_size;
  349. int mono_size;
  350. if(nb_streams<1||nb_coupled_streams>nb_streams||nb_coupled_streams<0)return 0;
  351. coupled_size = opus_encoder_get_size(2);
  352. mono_size = opus_encoder_get_size(1);
  353. return align(sizeof(OpusMSEncoder))
  354. + nb_coupled_streams * align(coupled_size)
  355. + (nb_streams-nb_coupled_streams) * align(mono_size);
  356. }
  357. opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_family)
  358. {
  359. int nb_streams;
  360. int nb_coupled_streams;
  361. opus_int32 size;
  362. if (mapping_family==0)
  363. {
  364. if (channels==1)
  365. {
  366. nb_streams=1;
  367. nb_coupled_streams=0;
  368. } else if (channels==2)
  369. {
  370. nb_streams=1;
  371. nb_coupled_streams=1;
  372. } else
  373. return 0;
  374. } else if (mapping_family==1 && channels<=8 && channels>=1)
  375. {
  376. nb_streams=vorbis_mappings[channels-1].nb_streams;
  377. nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams;
  378. } else if (mapping_family==255)
  379. {
  380. nb_streams=channels;
  381. nb_coupled_streams=0;
  382. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  383. } else if (mapping_family==254)
  384. {
  385. nb_streams=channels;
  386. nb_coupled_streams=0;
  387. #endif
  388. } else
  389. return 0;
  390. size = opus_multistream_encoder_get_size(nb_streams, nb_coupled_streams);
  391. if (channels>2)
  392. {
  393. size += channels*(120*sizeof(opus_val32) + sizeof(opus_val32));
  394. }
  395. return size;
  396. }
  397. static int opus_multistream_encoder_init_impl(
  398. OpusMSEncoder *st,
  399. opus_int32 Fs,
  400. int channels,
  401. int streams,
  402. int coupled_streams,
  403. const unsigned char *mapping,
  404. int application,
  405. MappingType mapping_type
  406. )
  407. {
  408. int coupled_size;
  409. int mono_size;
  410. int i, ret;
  411. char *ptr;
  412. if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
  413. (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
  414. return OPUS_BAD_ARG;
  415. st->arch = opus_select_arch();
  416. st->layout.nb_channels = channels;
  417. st->layout.nb_streams = streams;
  418. st->layout.nb_coupled_streams = coupled_streams;
  419. st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0;
  420. if (mapping_type != MAPPING_TYPE_SURROUND)
  421. st->lfe_stream = -1;
  422. st->bitrate_bps = OPUS_AUTO;
  423. st->application = application;
  424. st->variable_duration = OPUS_FRAMESIZE_ARG;
  425. for (i=0;i<st->layout.nb_channels;i++)
  426. st->layout.mapping[i] = mapping[i];
  427. if (!validate_layout(&st->layout) || !validate_encoder_layout(&st->layout))
  428. return OPUS_BAD_ARG;
  429. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  430. coupled_size = opus_encoder_get_size(2);
  431. mono_size = opus_encoder_get_size(1);
  432. for (i=0;i<st->layout.nb_coupled_streams;i++)
  433. {
  434. ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 2, application);
  435. if(ret!=OPUS_OK)return ret;
  436. if (i==st->lfe_stream)
  437. opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1));
  438. ptr += align(coupled_size);
  439. }
  440. for (;i<st->layout.nb_streams;i++)
  441. {
  442. ret = opus_encoder_init((OpusEncoder*)ptr, Fs, 1, application);
  443. if (i==st->lfe_stream)
  444. opus_encoder_ctl((OpusEncoder*)ptr, OPUS_SET_LFE(1));
  445. if(ret!=OPUS_OK)return ret;
  446. ptr += align(mono_size);
  447. }
  448. if (mapping_type == MAPPING_TYPE_SURROUND)
  449. {
  450. OPUS_CLEAR(ms_get_preemph_mem(st), channels);
  451. OPUS_CLEAR(ms_get_window_mem(st), channels*120);
  452. }
  453. st->mapping_type = mapping_type;
  454. return OPUS_OK;
  455. }
  456. int opus_multistream_encoder_init(
  457. OpusMSEncoder *st,
  458. opus_int32 Fs,
  459. int channels,
  460. int streams,
  461. int coupled_streams,
  462. const unsigned char *mapping,
  463. int application
  464. )
  465. {
  466. return opus_multistream_encoder_init_impl(st, Fs, channels, streams,
  467. coupled_streams, mapping,
  468. application, MAPPING_TYPE_NONE);
  469. }
  470. int opus_multistream_surround_encoder_init(
  471. OpusMSEncoder *st,
  472. opus_int32 Fs,
  473. int channels,
  474. int mapping_family,
  475. int *streams,
  476. int *coupled_streams,
  477. unsigned char *mapping,
  478. int application
  479. )
  480. {
  481. MappingType mapping_type;
  482. if ((channels>255) || (channels<1))
  483. return OPUS_BAD_ARG;
  484. st->lfe_stream = -1;
  485. if (mapping_family==0)
  486. {
  487. if (channels==1)
  488. {
  489. *streams=1;
  490. *coupled_streams=0;
  491. mapping[0]=0;
  492. } else if (channels==2)
  493. {
  494. *streams=1;
  495. *coupled_streams=1;
  496. mapping[0]=0;
  497. mapping[1]=1;
  498. } else
  499. return OPUS_UNIMPLEMENTED;
  500. } else if (mapping_family==1 && channels<=8 && channels>=1)
  501. {
  502. int i;
  503. *streams=vorbis_mappings[channels-1].nb_streams;
  504. *coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams;
  505. for (i=0;i<channels;i++)
  506. mapping[i] = vorbis_mappings[channels-1].mapping[i];
  507. if (channels>=6)
  508. st->lfe_stream = *streams-1;
  509. } else if (mapping_family==255)
  510. {
  511. int i;
  512. *streams=channels;
  513. *coupled_streams=0;
  514. for(i=0;i<channels;i++)
  515. mapping[i] = i;
  516. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  517. } else if (mapping_family==254)
  518. {
  519. int i;
  520. *streams=channels;
  521. *coupled_streams=0;
  522. for(i=0;i<channels;i++)
  523. mapping[i] = i;
  524. #endif
  525. } else
  526. return OPUS_UNIMPLEMENTED;
  527. if (channels>2 && mapping_family==1) {
  528. mapping_type = MAPPING_TYPE_SURROUND;
  529. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  530. } else if (mapping_family==254)
  531. {
  532. mapping_type = MAPPING_TYPE_AMBISONICS;
  533. #endif
  534. } else
  535. {
  536. mapping_type = MAPPING_TYPE_NONE;
  537. }
  538. return opus_multistream_encoder_init_impl(st, Fs, channels, *streams,
  539. *coupled_streams, mapping,
  540. application, mapping_type);
  541. }
  542. OpusMSEncoder *opus_multistream_encoder_create(
  543. opus_int32 Fs,
  544. int channels,
  545. int streams,
  546. int coupled_streams,
  547. const unsigned char *mapping,
  548. int application,
  549. int *error
  550. )
  551. {
  552. int ret;
  553. OpusMSEncoder *st;
  554. if ((channels>255) || (channels<1) || (coupled_streams>streams) ||
  555. (streams<1) || (coupled_streams<0) || (streams>255-coupled_streams))
  556. {
  557. if (error)
  558. *error = OPUS_BAD_ARG;
  559. return NULL;
  560. }
  561. st = (OpusMSEncoder *)opus_alloc(opus_multistream_encoder_get_size(streams, coupled_streams));
  562. if (st==NULL)
  563. {
  564. if (error)
  565. *error = OPUS_ALLOC_FAIL;
  566. return NULL;
  567. }
  568. ret = opus_multistream_encoder_init(st, Fs, channels, streams, coupled_streams, mapping, application);
  569. if (ret != OPUS_OK)
  570. {
  571. opus_free(st);
  572. st = NULL;
  573. }
  574. if (error)
  575. *error = ret;
  576. return st;
  577. }
  578. OpusMSEncoder *opus_multistream_surround_encoder_create(
  579. opus_int32 Fs,
  580. int channels,
  581. int mapping_family,
  582. int *streams,
  583. int *coupled_streams,
  584. unsigned char *mapping,
  585. int application,
  586. int *error
  587. )
  588. {
  589. int ret;
  590. opus_int32 size;
  591. OpusMSEncoder *st;
  592. if ((channels>255) || (channels<1))
  593. {
  594. if (error)
  595. *error = OPUS_BAD_ARG;
  596. return NULL;
  597. }
  598. size = opus_multistream_surround_encoder_get_size(channels, mapping_family);
  599. if (!size)
  600. {
  601. if (error)
  602. *error = OPUS_UNIMPLEMENTED;
  603. return NULL;
  604. }
  605. st = (OpusMSEncoder *)opus_alloc(size);
  606. if (st==NULL)
  607. {
  608. if (error)
  609. *error = OPUS_ALLOC_FAIL;
  610. return NULL;
  611. }
  612. ret = opus_multistream_surround_encoder_init(st, Fs, channels, mapping_family, streams, coupled_streams, mapping, application);
  613. if (ret != OPUS_OK)
  614. {
  615. opus_free(st);
  616. st = NULL;
  617. }
  618. if (error)
  619. *error = ret;
  620. return st;
  621. }
  622. static void surround_rate_allocation(
  623. OpusMSEncoder *st,
  624. opus_int32 *rate,
  625. int frame_size,
  626. opus_int32 Fs
  627. )
  628. {
  629. int i;
  630. opus_int32 channel_rate;
  631. int stream_offset;
  632. int lfe_offset;
  633. int coupled_ratio; /* Q8 */
  634. int lfe_ratio; /* Q8 */
  635. if (st->bitrate_bps > st->layout.nb_channels*40000)
  636. stream_offset = 20000;
  637. else
  638. stream_offset = st->bitrate_bps/st->layout.nb_channels/2;
  639. stream_offset += 60*(Fs/frame_size-50);
  640. /* We start by giving each stream (coupled or uncoupled) the same bitrate.
  641. This models the main saving of coupled channels over uncoupled. */
  642. /* The LFE stream is an exception to the above and gets fewer bits. */
  643. lfe_offset = 3500 + 60*(Fs/frame_size-50);
  644. /* Coupled streams get twice the mono rate after the first 20 kb/s. */
  645. coupled_ratio = 512;
  646. /* Should depend on the bitrate, for now we assume LFE gets 1/8 the bits of mono */
  647. lfe_ratio = 32;
  648. /* Compute bitrate allocation between streams */
  649. if (st->bitrate_bps==OPUS_AUTO)
  650. {
  651. channel_rate = Fs+60*Fs/frame_size;
  652. } else if (st->bitrate_bps==OPUS_BITRATE_MAX)
  653. {
  654. channel_rate = 300000;
  655. } else {
  656. int nb_lfe;
  657. int nb_uncoupled;
  658. int nb_coupled;
  659. int total;
  660. nb_lfe = (st->lfe_stream!=-1);
  661. nb_coupled = st->layout.nb_coupled_streams;
  662. nb_uncoupled = st->layout.nb_streams-nb_coupled-nb_lfe;
  663. total = (nb_uncoupled<<8) /* mono */
  664. + coupled_ratio*nb_coupled /* stereo */
  665. + nb_lfe*lfe_ratio;
  666. channel_rate = 256*(st->bitrate_bps-lfe_offset*nb_lfe-stream_offset*(nb_coupled+nb_uncoupled))/total;
  667. }
  668. #ifndef FIXED_POINT
  669. if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50)
  670. {
  671. opus_int32 bonus;
  672. bonus = 60*(Fs/frame_size-50);
  673. channel_rate += bonus;
  674. }
  675. #endif
  676. for (i=0;i<st->layout.nb_streams;i++)
  677. {
  678. if (i<st->layout.nb_coupled_streams)
  679. rate[i] = stream_offset+(channel_rate*coupled_ratio>>8);
  680. else if (i!=st->lfe_stream)
  681. rate[i] = stream_offset+channel_rate;
  682. else
  683. rate[i] = lfe_offset+(channel_rate*lfe_ratio>>8);
  684. }
  685. }
  686. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  687. static void ambisonics_rate_allocation(
  688. OpusMSEncoder *st,
  689. opus_int32 *rate,
  690. int frame_size,
  691. opus_int32 Fs
  692. )
  693. {
  694. int i;
  695. int non_mono_rate;
  696. int total_rate;
  697. /* The mono channel gets (rate_ratio_num / rate_ratio_den) times as many bits
  698. * as all other channels */
  699. const int rate_ratio_num = 4;
  700. const int rate_ratio_den = 3;
  701. const int num_channels = st->layout.nb_streams;
  702. if (st->bitrate_bps==OPUS_AUTO)
  703. {
  704. total_rate = num_channels * (20000 + st->layout.nb_streams*(Fs+60*Fs/frame_size));
  705. } else if (st->bitrate_bps==OPUS_BITRATE_MAX)
  706. {
  707. total_rate = num_channels * 320000;
  708. } else {
  709. total_rate = st->bitrate_bps;
  710. }
  711. /* Let y be the non-mono rate and let p, q be integers such that the mono
  712. * channel rate is (p/q) * y.
  713. * Also let T be the total bitrate to allocate. Then
  714. * (n - 1) y + (p/q) y = T
  715. * y = (T q) / (qn - q + p)
  716. */
  717. non_mono_rate =
  718. total_rate * rate_ratio_den
  719. / (rate_ratio_den*num_channels + rate_ratio_num - rate_ratio_den);
  720. #ifndef FIXED_POINT
  721. if (st->variable_duration==OPUS_FRAMESIZE_VARIABLE && frame_size != Fs/50)
  722. {
  723. opus_int32 bonus = 60*(Fs/frame_size-50);
  724. non_mono_rate += bonus;
  725. }
  726. #endif
  727. rate[0] = total_rate - (num_channels - 1) * non_mono_rate;
  728. for (i=1;i<st->layout.nb_streams;i++)
  729. {
  730. rate[i] = non_mono_rate;
  731. }
  732. }
  733. #endif /* ENABLE_EXPERIMENTAL_AMBISONICS */
  734. static opus_int32 rate_allocation(
  735. OpusMSEncoder *st,
  736. opus_int32 *rate,
  737. int frame_size
  738. )
  739. {
  740. int i;
  741. opus_int32 rate_sum=0;
  742. opus_int32 Fs;
  743. char *ptr;
  744. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  745. opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs));
  746. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  747. if (st->mapping_type == MAPPING_TYPE_AMBISONICS) {
  748. ambisonics_rate_allocation(st, rate, frame_size, Fs);
  749. } else
  750. #endif
  751. {
  752. surround_rate_allocation(st, rate, frame_size, Fs);
  753. }
  754. for (i=0;i<st->layout.nb_streams;i++)
  755. {
  756. rate[i] = IMAX(rate[i], 500);
  757. rate_sum += rate[i];
  758. }
  759. return rate_sum;
  760. }
  761. /* Max size in case the encoder decides to return three frames */
  762. #define MS_FRAME_TMP (3*1275+7)
  763. static int opus_multistream_encode_native
  764. (
  765. OpusMSEncoder *st,
  766. opus_copy_channel_in_func copy_channel_in,
  767. const void *pcm,
  768. int analysis_frame_size,
  769. unsigned char *data,
  770. opus_int32 max_data_bytes,
  771. int lsb_depth,
  772. downmix_func downmix,
  773. int float_api
  774. )
  775. {
  776. opus_int32 Fs;
  777. int coupled_size;
  778. int mono_size;
  779. int s;
  780. char *ptr;
  781. int tot_size;
  782. VARDECL(opus_val16, buf);
  783. VARDECL(opus_val16, bandSMR);
  784. unsigned char tmp_data[MS_FRAME_TMP];
  785. OpusRepacketizer rp;
  786. opus_int32 vbr;
  787. const CELTMode *celt_mode;
  788. opus_int32 bitrates[256];
  789. opus_val16 bandLogE[42];
  790. opus_val32 *mem = NULL;
  791. opus_val32 *preemph_mem=NULL;
  792. int frame_size;
  793. opus_int32 rate_sum;
  794. opus_int32 smallest_packet;
  795. ALLOC_STACK;
  796. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  797. {
  798. preemph_mem = ms_get_preemph_mem(st);
  799. mem = ms_get_window_mem(st);
  800. }
  801. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  802. opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_SAMPLE_RATE(&Fs));
  803. opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_VBR(&vbr));
  804. opus_encoder_ctl((OpusEncoder*)ptr, CELT_GET_MODE(&celt_mode));
  805. {
  806. opus_int32 delay_compensation;
  807. int channels;
  808. channels = st->layout.nb_streams + st->layout.nb_coupled_streams;
  809. opus_encoder_ctl((OpusEncoder*)ptr, OPUS_GET_LOOKAHEAD(&delay_compensation));
  810. delay_compensation -= Fs/400;
  811. frame_size = compute_frame_size(pcm, analysis_frame_size,
  812. st->variable_duration, channels, Fs, st->bitrate_bps,
  813. delay_compensation, downmix
  814. #ifndef DISABLE_FLOAT_API
  815. , st->subframe_mem
  816. #endif
  817. );
  818. }
  819. if (400*frame_size < Fs)
  820. {
  821. RESTORE_STACK;
  822. return OPUS_BAD_ARG;
  823. }
  824. /* Validate frame_size before using it to allocate stack space.
  825. This mirrors the checks in opus_encode[_float](). */
  826. if (400*frame_size != Fs && 200*frame_size != Fs &&
  827. 100*frame_size != Fs && 50*frame_size != Fs &&
  828. 25*frame_size != Fs && 50*frame_size != 3*Fs)
  829. {
  830. RESTORE_STACK;
  831. return OPUS_BAD_ARG;
  832. }
  833. /* Smallest packet the encoder can produce. */
  834. smallest_packet = st->layout.nb_streams*2-1;
  835. if (max_data_bytes < smallest_packet)
  836. {
  837. RESTORE_STACK;
  838. return OPUS_BUFFER_TOO_SMALL;
  839. }
  840. ALLOC(buf, 2*frame_size, opus_val16);
  841. coupled_size = opus_encoder_get_size(2);
  842. mono_size = opus_encoder_get_size(1);
  843. ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16);
  844. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  845. {
  846. surround_analysis(celt_mode, pcm, bandSMR, mem, preemph_mem, frame_size, 120, st->layout.nb_channels, Fs, copy_channel_in, st->arch);
  847. }
  848. /* Compute bitrate allocation between streams (this could be a lot better) */
  849. rate_sum = rate_allocation(st, bitrates, frame_size);
  850. if (!vbr)
  851. {
  852. if (st->bitrate_bps == OPUS_AUTO)
  853. {
  854. max_data_bytes = IMIN(max_data_bytes, 3*rate_sum/(3*8*Fs/frame_size));
  855. } else if (st->bitrate_bps != OPUS_BITRATE_MAX)
  856. {
  857. max_data_bytes = IMIN(max_data_bytes, IMAX(smallest_packet,
  858. 3*st->bitrate_bps/(3*8*Fs/frame_size)));
  859. }
  860. }
  861. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  862. for (s=0;s<st->layout.nb_streams;s++)
  863. {
  864. OpusEncoder *enc;
  865. enc = (OpusEncoder*)ptr;
  866. if (s < st->layout.nb_coupled_streams)
  867. ptr += align(coupled_size);
  868. else
  869. ptr += align(mono_size);
  870. opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s]));
  871. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  872. {
  873. opus_int32 equiv_rate;
  874. equiv_rate = st->bitrate_bps;
  875. if (frame_size*50 < Fs)
  876. equiv_rate -= 60*(Fs/frame_size - 50)*st->layout.nb_channels;
  877. if (equiv_rate > 10000*st->layout.nb_channels)
  878. opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_FULLBAND));
  879. else if (equiv_rate > 7000*st->layout.nb_channels)
  880. opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_SUPERWIDEBAND));
  881. else if (equiv_rate > 5000*st->layout.nb_channels)
  882. opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_WIDEBAND));
  883. else
  884. opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND));
  885. if (s < st->layout.nb_coupled_streams)
  886. {
  887. /* To preserve the spatial image, force stereo CELT on coupled streams */
  888. opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY));
  889. opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(2));
  890. }
  891. }
  892. #ifdef ENABLE_EXPERIMENTAL_AMBISONICS
  893. else if (st->mapping_type == MAPPING_TYPE_AMBISONICS) {
  894. opus_encoder_ctl(enc, OPUS_SET_FORCE_MODE(MODE_CELT_ONLY));
  895. }
  896. #endif
  897. }
  898. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  899. /* Counting ToC */
  900. tot_size = 0;
  901. for (s=0;s<st->layout.nb_streams;s++)
  902. {
  903. OpusEncoder *enc;
  904. int len;
  905. int curr_max;
  906. int c1, c2;
  907. int ret;
  908. opus_repacketizer_init(&rp);
  909. enc = (OpusEncoder*)ptr;
  910. if (s < st->layout.nb_coupled_streams)
  911. {
  912. int i;
  913. int left, right;
  914. left = get_left_channel(&st->layout, s, -1);
  915. right = get_right_channel(&st->layout, s, -1);
  916. (*copy_channel_in)(buf, 2,
  917. pcm, st->layout.nb_channels, left, frame_size);
  918. (*copy_channel_in)(buf+1, 2,
  919. pcm, st->layout.nb_channels, right, frame_size);
  920. ptr += align(coupled_size);
  921. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  922. {
  923. for (i=0;i<21;i++)
  924. {
  925. bandLogE[i] = bandSMR[21*left+i];
  926. bandLogE[21+i] = bandSMR[21*right+i];
  927. }
  928. }
  929. c1 = left;
  930. c2 = right;
  931. } else {
  932. int i;
  933. int chan = get_mono_channel(&st->layout, s, -1);
  934. (*copy_channel_in)(buf, 1,
  935. pcm, st->layout.nb_channels, chan, frame_size);
  936. ptr += align(mono_size);
  937. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  938. {
  939. for (i=0;i<21;i++)
  940. bandLogE[i] = bandSMR[21*chan+i];
  941. }
  942. c1 = chan;
  943. c2 = -1;
  944. }
  945. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  946. opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE));
  947. /* number of bytes left (+Toc) */
  948. curr_max = max_data_bytes - tot_size;
  949. /* Reserve one byte for the last stream and two for the others */
  950. curr_max -= IMAX(0,2*(st->layout.nb_streams-s-1)-1);
  951. curr_max = IMIN(curr_max,MS_FRAME_TMP);
  952. /* Repacketizer will add one or two bytes for self-delimited frames */
  953. if (s != st->layout.nb_streams-1) curr_max -= curr_max>253 ? 2 : 1;
  954. if (!vbr && s == st->layout.nb_streams-1)
  955. opus_encoder_ctl(enc, OPUS_SET_BITRATE(curr_max*(8*Fs/frame_size)));
  956. len = opus_encode_native(enc, buf, frame_size, tmp_data, curr_max, lsb_depth,
  957. pcm, analysis_frame_size, c1, c2, st->layout.nb_channels, downmix, float_api);
  958. if (len<0)
  959. {
  960. RESTORE_STACK;
  961. return len;
  962. }
  963. /* We need to use the repacketizer to add the self-delimiting lengths
  964. while taking into account the fact that the encoder can now return
  965. more than one frame at a time (e.g. 60 ms CELT-only) */
  966. ret = opus_repacketizer_cat(&rp, tmp_data, len);
  967. /* If the opus_repacketizer_cat() fails, then something's seriously wrong
  968. with the encoder. */
  969. if (ret != OPUS_OK)
  970. {
  971. RESTORE_STACK;
  972. return OPUS_INTERNAL_ERROR;
  973. }
  974. len = opus_repacketizer_out_range_impl(&rp, 0, opus_repacketizer_get_nb_frames(&rp),
  975. data, max_data_bytes-tot_size, s != st->layout.nb_streams-1, !vbr && s == st->layout.nb_streams-1);
  976. data += len;
  977. tot_size += len;
  978. }
  979. /*printf("\n");*/
  980. RESTORE_STACK;
  981. return tot_size;
  982. }
  983. #if !defined(DISABLE_FLOAT_API)
  984. static void opus_copy_channel_in_float(
  985. opus_val16 *dst,
  986. int dst_stride,
  987. const void *src,
  988. int src_stride,
  989. int src_channel,
  990. int frame_size
  991. )
  992. {
  993. const float *float_src;
  994. opus_int32 i;
  995. float_src = (const float *)src;
  996. for (i=0;i<frame_size;i++)
  997. #if defined(FIXED_POINT)
  998. dst[i*dst_stride] = FLOAT2INT16(float_src[i*src_stride+src_channel]);
  999. #else
  1000. dst[i*dst_stride] = float_src[i*src_stride+src_channel];
  1001. #endif
  1002. }
  1003. #endif
  1004. static void opus_copy_channel_in_short(
  1005. opus_val16 *dst,
  1006. int dst_stride,
  1007. const void *src,
  1008. int src_stride,
  1009. int src_channel,
  1010. int frame_size
  1011. )
  1012. {
  1013. const opus_int16 *short_src;
  1014. opus_int32 i;
  1015. short_src = (const opus_int16 *)src;
  1016. for (i=0;i<frame_size;i++)
  1017. #if defined(FIXED_POINT)
  1018. dst[i*dst_stride] = short_src[i*src_stride+src_channel];
  1019. #else
  1020. dst[i*dst_stride] = (1/32768.f)*short_src[i*src_stride+src_channel];
  1021. #endif
  1022. }
  1023. #ifdef FIXED_POINT
  1024. int opus_multistream_encode(
  1025. OpusMSEncoder *st,
  1026. const opus_val16 *pcm,
  1027. int frame_size,
  1028. unsigned char *data,
  1029. opus_int32 max_data_bytes
  1030. )
  1031. {
  1032. return opus_multistream_encode_native(st, opus_copy_channel_in_short,
  1033. pcm, frame_size, data, max_data_bytes, 16, downmix_int, 0);
  1034. }
  1035. #ifndef DISABLE_FLOAT_API
  1036. int opus_multistream_encode_float(
  1037. OpusMSEncoder *st,
  1038. const float *pcm,
  1039. int frame_size,
  1040. unsigned char *data,
  1041. opus_int32 max_data_bytes
  1042. )
  1043. {
  1044. return opus_multistream_encode_native(st, opus_copy_channel_in_float,
  1045. pcm, frame_size, data, max_data_bytes, 16, downmix_float, 1);
  1046. }
  1047. #endif
  1048. #else
  1049. int opus_multistream_encode_float
  1050. (
  1051. OpusMSEncoder *st,
  1052. const opus_val16 *pcm,
  1053. int frame_size,
  1054. unsigned char *data,
  1055. opus_int32 max_data_bytes
  1056. )
  1057. {
  1058. return opus_multistream_encode_native(st, opus_copy_channel_in_float,
  1059. pcm, frame_size, data, max_data_bytes, 24, downmix_float, 1);
  1060. }
  1061. int opus_multistream_encode(
  1062. OpusMSEncoder *st,
  1063. const opus_int16 *pcm,
  1064. int frame_size,
  1065. unsigned char *data,
  1066. opus_int32 max_data_bytes
  1067. )
  1068. {
  1069. return opus_multistream_encode_native(st, opus_copy_channel_in_short,
  1070. pcm, frame_size, data, max_data_bytes, 16, downmix_int, 0);
  1071. }
  1072. #endif
  1073. int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...)
  1074. {
  1075. va_list ap;
  1076. int coupled_size, mono_size;
  1077. char *ptr;
  1078. int ret = OPUS_OK;
  1079. va_start(ap, request);
  1080. coupled_size = opus_encoder_get_size(2);
  1081. mono_size = opus_encoder_get_size(1);
  1082. ptr = (char*)st + align(sizeof(OpusMSEncoder));
  1083. switch (request)
  1084. {
  1085. case OPUS_SET_BITRATE_REQUEST:
  1086. {
  1087. opus_int32 value = va_arg(ap, opus_int32);
  1088. if (value<0 && value!=OPUS_AUTO && value!=OPUS_BITRATE_MAX)
  1089. {
  1090. goto bad_arg;
  1091. }
  1092. st->bitrate_bps = value;
  1093. }
  1094. break;
  1095. case OPUS_GET_BITRATE_REQUEST:
  1096. {
  1097. int s;
  1098. opus_int32 *value = va_arg(ap, opus_int32*);
  1099. if (!value)
  1100. {
  1101. goto bad_arg;
  1102. }
  1103. *value = 0;
  1104. for (s=0;s<st->layout.nb_streams;s++)
  1105. {
  1106. opus_int32 rate;
  1107. OpusEncoder *enc;
  1108. enc = (OpusEncoder*)ptr;
  1109. if (s < st->layout.nb_coupled_streams)
  1110. ptr += align(coupled_size);
  1111. else
  1112. ptr += align(mono_size);
  1113. opus_encoder_ctl(enc, request, &rate);
  1114. *value += rate;
  1115. }
  1116. }
  1117. break;
  1118. case OPUS_GET_LSB_DEPTH_REQUEST:
  1119. case OPUS_GET_VBR_REQUEST:
  1120. case OPUS_GET_APPLICATION_REQUEST:
  1121. case OPUS_GET_BANDWIDTH_REQUEST:
  1122. case OPUS_GET_COMPLEXITY_REQUEST:
  1123. case OPUS_GET_PACKET_LOSS_PERC_REQUEST:
  1124. case OPUS_GET_DTX_REQUEST:
  1125. case OPUS_GET_VOICE_RATIO_REQUEST:
  1126. case OPUS_GET_VBR_CONSTRAINT_REQUEST:
  1127. case OPUS_GET_SIGNAL_REQUEST:
  1128. case OPUS_GET_LOOKAHEAD_REQUEST:
  1129. case OPUS_GET_SAMPLE_RATE_REQUEST:
  1130. case OPUS_GET_INBAND_FEC_REQUEST:
  1131. case OPUS_GET_FORCE_CHANNELS_REQUEST:
  1132. case OPUS_GET_PREDICTION_DISABLED_REQUEST:
  1133. {
  1134. OpusEncoder *enc;
  1135. /* For int32* GET params, just query the first stream */
  1136. opus_int32 *value = va_arg(ap, opus_int32*);
  1137. enc = (OpusEncoder*)ptr;
  1138. ret = opus_encoder_ctl(enc, request, value);
  1139. }
  1140. break;
  1141. case OPUS_GET_FINAL_RANGE_REQUEST:
  1142. {
  1143. int s;
  1144. opus_uint32 *value = va_arg(ap, opus_uint32*);
  1145. opus_uint32 tmp;
  1146. if (!value)
  1147. {
  1148. goto bad_arg;
  1149. }
  1150. *value=0;
  1151. for (s=0;s<st->layout.nb_streams;s++)
  1152. {
  1153. OpusEncoder *enc;
  1154. enc = (OpusEncoder*)ptr;
  1155. if (s < st->layout.nb_coupled_streams)
  1156. ptr += align(coupled_size);
  1157. else
  1158. ptr += align(mono_size);
  1159. ret = opus_encoder_ctl(enc, request, &tmp);
  1160. if (ret != OPUS_OK) break;
  1161. *value ^= tmp;
  1162. }
  1163. }
  1164. break;
  1165. case OPUS_SET_LSB_DEPTH_REQUEST:
  1166. case OPUS_SET_COMPLEXITY_REQUEST:
  1167. case OPUS_SET_VBR_REQUEST:
  1168. case OPUS_SET_VBR_CONSTRAINT_REQUEST:
  1169. case OPUS_SET_MAX_BANDWIDTH_REQUEST:
  1170. case OPUS_SET_BANDWIDTH_REQUEST:
  1171. case OPUS_SET_SIGNAL_REQUEST:
  1172. case OPUS_SET_APPLICATION_REQUEST:
  1173. case OPUS_SET_INBAND_FEC_REQUEST:
  1174. case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
  1175. case OPUS_SET_DTX_REQUEST:
  1176. case OPUS_SET_FORCE_MODE_REQUEST:
  1177. case OPUS_SET_FORCE_CHANNELS_REQUEST:
  1178. case OPUS_SET_PREDICTION_DISABLED_REQUEST:
  1179. {
  1180. int s;
  1181. /* This works for int32 params */
  1182. opus_int32 value = va_arg(ap, opus_int32);
  1183. for (s=0;s<st->layout.nb_streams;s++)
  1184. {
  1185. OpusEncoder *enc;
  1186. enc = (OpusEncoder*)ptr;
  1187. if (s < st->layout.nb_coupled_streams)
  1188. ptr += align(coupled_size);
  1189. else
  1190. ptr += align(mono_size);
  1191. ret = opus_encoder_ctl(enc, request, value);
  1192. if (ret != OPUS_OK)
  1193. break;
  1194. }
  1195. }
  1196. break;
  1197. case OPUS_MULTISTREAM_GET_ENCODER_STATE_REQUEST:
  1198. {
  1199. int s;
  1200. opus_int32 stream_id;
  1201. OpusEncoder **value;
  1202. stream_id = va_arg(ap, opus_int32);
  1203. if (stream_id<0 || stream_id >= st->layout.nb_streams)
  1204. ret = OPUS_BAD_ARG;
  1205. value = va_arg(ap, OpusEncoder**);
  1206. if (!value)
  1207. {
  1208. goto bad_arg;
  1209. }
  1210. for (s=0;s<stream_id;s++)
  1211. {
  1212. if (s < st->layout.nb_coupled_streams)
  1213. ptr += align(coupled_size);
  1214. else
  1215. ptr += align(mono_size);
  1216. }
  1217. *value = (OpusEncoder*)ptr;
  1218. }
  1219. break;
  1220. case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
  1221. {
  1222. opus_int32 value = va_arg(ap, opus_int32);
  1223. st->variable_duration = value;
  1224. }
  1225. break;
  1226. case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST:
  1227. {
  1228. opus_int32 *value = va_arg(ap, opus_int32*);
  1229. if (!value)
  1230. {
  1231. goto bad_arg;
  1232. }
  1233. *value = st->variable_duration;
  1234. }
  1235. break;
  1236. case OPUS_RESET_STATE:
  1237. {
  1238. int s;
  1239. st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0;
  1240. if (st->mapping_type == MAPPING_TYPE_SURROUND)
  1241. {
  1242. OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels);
  1243. OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120);
  1244. }
  1245. for (s=0;s<st->layout.nb_streams;s++)
  1246. {
  1247. OpusEncoder *enc;
  1248. enc = (OpusEncoder*)ptr;
  1249. if (s < st->layout.nb_coupled_streams)
  1250. ptr += align(coupled_size);
  1251. else
  1252. ptr += align(mono_size);
  1253. ret = opus_encoder_ctl(enc, OPUS_RESET_STATE);
  1254. if (ret != OPUS_OK)
  1255. break;
  1256. }
  1257. }
  1258. break;
  1259. default:
  1260. ret = OPUS_UNIMPLEMENTED;
  1261. break;
  1262. }
  1263. va_end(ap);
  1264. return ret;
  1265. bad_arg:
  1266. va_end(ap);
  1267. return OPUS_BAD_ARG;
  1268. }
  1269. void opus_multistream_encoder_destroy(OpusMSEncoder *st)
  1270. {
  1271. opus_free(st);
  1272. }