You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

635 lines
23KB

  1. /*
  2. * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
  3. *
  4. * This file is part of libswresample
  5. *
  6. * libswresample is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * libswresample is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with libswresample; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include "libavutil/opt.h"
  21. #include "swresample_internal.h"
  22. #include "audioconvert.h"
  23. #include "libavutil/avassert.h"
  24. #include "libavutil/audioconvert.h"
  25. #define C30DB M_SQRT2
  26. #define C15DB 1.189207115
  27. #define C__0DB 1.0
  28. #define C_15DB 0.840896415
  29. #define C_30DB M_SQRT1_2
  30. #define C_45DB 0.594603558
  31. #define C_60DB 0.5
  32. //TODO split options array out?
  33. #define OFFSET(x) offsetof(SwrContext,x)
  34. static const AVOption options[]={
  35. {"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
  36. {"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
  37. {"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0},
  38. {"isr", "input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
  39. {"osr", "output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
  40. //{"ip" , "input planar" , OFFSET( in.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
  41. //{"op" , "output planar" , OFFSET(out.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
  42. {"isf", "input sample format", OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
  43. {"osf", "output sample format", OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
  44. {"tsf", "internal sample format", OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0},
  45. {"icl", "input channel layout" , OFFSET( in_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
  46. {"ocl", "output channel layout", OFFSET(out_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
  47. {"clev", "center mix level" , OFFSET(clev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
  48. {"slev", "sourround mix level" , OFFSET(slev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
  49. {"rmvol", "rematrix volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, -1000, 1000, 0},
  50. {"flags", NULL , OFFSET(flags) , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0, UINT_MAX, 0, "flags"},
  51. {"swr_flags", NULL , OFFSET(flags) , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0, UINT_MAX, 0, "flags"},
  52. {"res", "force resampling", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"},
  53. {"dither_scale" , "dither scale" , OFFSET(dither_scale ), AV_OPT_TYPE_FLOAT, {.dbl=1}, 0, INT_MAX, 0},
  54. {"dither_method", "dither method" , OFFSET(dither_method), AV_OPT_TYPE_INT , {.dbl=0}, 0, SWR_DITHER_NB-1, 0, "dither_method"},
  55. {"rectangular", "rectangular dither", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_RECTANGULAR}, INT_MIN, INT_MAX, 0, "dither_method"},
  56. {"triangular" , "triangular dither" , 0, AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR }, INT_MIN, INT_MAX, 0, "dither_method"},
  57. {"triangular_hp" , "triangular dither with high pass" , 0, AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR_HIGHPASS }, INT_MIN, INT_MAX, 0, "dither_method"},
  58. {0}
  59. };
  60. static const char* context_to_name(void* ptr) {
  61. return "SWR";
  62. }
  63. static const AVClass av_class = {
  64. .class_name = "SwrContext",
  65. .item_name = context_to_name,
  66. .option = options,
  67. .version = LIBAVUTIL_VERSION_INT,
  68. .log_level_offset_offset = OFFSET(log_level_offset),
  69. .parent_log_context_offset = OFFSET(log_ctx),
  70. };
  71. unsigned swresample_version(void)
  72. {
  73. av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
  74. return LIBSWRESAMPLE_VERSION_INT;
  75. }
  76. const char *swresample_configuration(void)
  77. {
  78. return FFMPEG_CONFIGURATION;
  79. }
  80. const char *swresample_license(void)
  81. {
  82. #define LICENSE_PREFIX "libswresample license: "
  83. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  84. }
  85. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
  86. if(!s || s->in_convert) // s needs to be allocated but not initialized
  87. return AVERROR(EINVAL);
  88. s->channel_map = channel_map;
  89. return 0;
  90. }
  91. const AVClass *swr_get_class(void)
  92. {
  93. return &av_class;
  94. }
  95. struct SwrContext *swr_alloc(void){
  96. SwrContext *s= av_mallocz(sizeof(SwrContext));
  97. if(s){
  98. s->av_class= &av_class;
  99. av_opt_set_defaults(s);
  100. }
  101. return s;
  102. }
  103. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  104. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  105. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  106. int log_offset, void *log_ctx){
  107. if(!s) s= swr_alloc();
  108. if(!s) return NULL;
  109. s->log_level_offset= log_offset;
  110. s->log_ctx= log_ctx;
  111. av_opt_set_int(s, "ocl", out_ch_layout, 0);
  112. av_opt_set_int(s, "osf", out_sample_fmt, 0);
  113. av_opt_set_int(s, "osr", out_sample_rate, 0);
  114. av_opt_set_int(s, "icl", in_ch_layout, 0);
  115. av_opt_set_int(s, "isf", in_sample_fmt, 0);
  116. av_opt_set_int(s, "isr", in_sample_rate, 0);
  117. av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_NONE, 0);
  118. av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
  119. av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
  120. av_opt_set_int(s, "uch", 0, 0);
  121. return s;
  122. }
  123. static void free_temp(AudioData *a){
  124. av_free(a->data);
  125. memset(a, 0, sizeof(*a));
  126. }
  127. void swr_free(SwrContext **ss){
  128. SwrContext *s= *ss;
  129. if(s){
  130. free_temp(&s->postin);
  131. free_temp(&s->midbuf);
  132. free_temp(&s->preout);
  133. free_temp(&s->in_buffer);
  134. free_temp(&s->dither);
  135. swri_audio_convert_free(&s-> in_convert);
  136. swri_audio_convert_free(&s->out_convert);
  137. swri_audio_convert_free(&s->full_convert);
  138. swri_resample_free(&s->resample);
  139. }
  140. av_freep(ss);
  141. }
  142. int swr_init(struct SwrContext *s){
  143. s->in_buffer_index= 0;
  144. s->in_buffer_count= 0;
  145. s->resample_in_constraint= 0;
  146. free_temp(&s->postin);
  147. free_temp(&s->midbuf);
  148. free_temp(&s->preout);
  149. free_temp(&s->in_buffer);
  150. free_temp(&s->dither);
  151. swri_audio_convert_free(&s-> in_convert);
  152. swri_audio_convert_free(&s->out_convert);
  153. swri_audio_convert_free(&s->full_convert);
  154. s->flushed = 0;
  155. s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt);
  156. s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt);
  157. s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0);
  158. s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0);
  159. if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
  160. av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
  161. return AVERROR(EINVAL);
  162. }
  163. if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
  164. av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
  165. return AVERROR(EINVAL);
  166. }
  167. //FIXME should we allow/support using FLT on material that doesnt need it ?
  168. if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
  169. s->int_sample_fmt= AV_SAMPLE_FMT_S16;
  170. }else
  171. s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
  172. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16
  173. &&s->int_sample_fmt != AV_SAMPLE_FMT_S32
  174. &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
  175. av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, S16/S32/FLT is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
  176. return AVERROR(EINVAL);
  177. }
  178. if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
  179. s->resample = swri_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8, s->int_sample_fmt);
  180. }else
  181. swri_resample_free(&s->resample);
  182. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16
  183. && s->int_sample_fmt != AV_SAMPLE_FMT_S32
  184. && s->int_sample_fmt != AV_SAMPLE_FMT_FLT
  185. && s->resample){
  186. av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16/s32/flt\n");
  187. return -1;
  188. }
  189. if(!s->used_ch_count)
  190. s->used_ch_count= s->in.ch_count;
  191. if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
  192. av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
  193. s-> in_ch_layout= 0;
  194. }
  195. if(!s-> in_ch_layout)
  196. s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
  197. if(!s->out_ch_layout)
  198. s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
  199. s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
  200. s->rematrix_custom;
  201. #define RSC 1 //FIXME finetune
  202. if(!s-> in.ch_count)
  203. s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
  204. if(!s->used_ch_count)
  205. s->used_ch_count= s->in.ch_count;
  206. if(!s->out.ch_count)
  207. s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
  208. if(!s-> in.ch_count){
  209. av_assert0(!s->in_ch_layout);
  210. av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
  211. return -1;
  212. }
  213. if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
  214. av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
  215. return -1;
  216. }
  217. av_assert0(s->used_ch_count);
  218. av_assert0(s->out.ch_count);
  219. s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
  220. s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
  221. s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
  222. s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
  223. s->in_buffer= s->in;
  224. if(!s->resample && !s->rematrix && !s->channel_map){
  225. s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
  226. s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
  227. return 0;
  228. }
  229. s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
  230. s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
  231. s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
  232. s->int_sample_fmt, s->out.ch_count, NULL, 0);
  233. s->postin= s->in;
  234. s->preout= s->out;
  235. s->midbuf= s->in;
  236. if(s->channel_map){
  237. s->postin.ch_count=
  238. s->midbuf.ch_count= s->used_ch_count;
  239. if(s->resample)
  240. s->in_buffer.ch_count= s->used_ch_count;
  241. }
  242. if(!s->resample_first){
  243. s->midbuf.ch_count= s->out.ch_count;
  244. if(s->resample)
  245. s->in_buffer.ch_count = s->out.ch_count;
  246. }
  247. s->postin.bps = s->midbuf.bps = s->preout.bps = s->int_bps;
  248. s->postin.planar = s->midbuf.planar = s->preout.planar = 1;
  249. if(s->resample){
  250. s->in_buffer.bps = s->int_bps;
  251. s->in_buffer.planar = 1;
  252. }
  253. s->dither = s->preout;
  254. if(s->rematrix)
  255. return swri_rematrix_init(s);
  256. return 0;
  257. }
  258. static int realloc_audio(AudioData *a, int count){
  259. int i, countb;
  260. AudioData old;
  261. if(a->count >= count)
  262. return 0;
  263. count*=2;
  264. countb= FFALIGN(count*a->bps, 32);
  265. old= *a;
  266. av_assert0(a->bps);
  267. av_assert0(a->ch_count);
  268. a->data= av_malloc(countb*a->ch_count);
  269. if(!a->data)
  270. return AVERROR(ENOMEM);
  271. for(i=0; i<a->ch_count; i++){
  272. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  273. if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  274. }
  275. if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
  276. av_free(old.data);
  277. a->count= count;
  278. return 1;
  279. }
  280. static void copy(AudioData *out, AudioData *in,
  281. int count){
  282. av_assert0(out->planar == in->planar);
  283. av_assert0(out->bps == in->bps);
  284. av_assert0(out->ch_count == in->ch_count);
  285. if(out->planar){
  286. int ch;
  287. for(ch=0; ch<out->ch_count; ch++)
  288. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  289. }else
  290. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  291. }
  292. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  293. int i;
  294. if(out->planar){
  295. for(i=0; i<out->ch_count; i++)
  296. out->ch[i]= in_arg[i];
  297. }else{
  298. for(i=0; i<out->ch_count; i++)
  299. out->ch[i]= in_arg[0] + i*out->bps;
  300. }
  301. }
  302. /**
  303. *
  304. * out may be equal in.
  305. */
  306. static void buf_set(AudioData *out, AudioData *in, int count){
  307. int ch;
  308. if(in->planar){
  309. for(ch=0; ch<out->ch_count; ch++)
  310. out->ch[ch]= in->ch[ch] + count*out->bps;
  311. }else{
  312. for(ch=0; ch<out->ch_count; ch++)
  313. out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
  314. }
  315. }
  316. /**
  317. *
  318. * @return number of samples output per channel
  319. */
  320. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  321. const AudioData * in_param, int in_count){
  322. AudioData in, out, tmp;
  323. int ret_sum=0;
  324. int border=0;
  325. tmp=out=*out_param;
  326. in = *in_param;
  327. do{
  328. int ret, size, consumed;
  329. if(!s->resample_in_constraint && s->in_buffer_count){
  330. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  331. ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  332. out_count -= ret;
  333. ret_sum += ret;
  334. buf_set(&out, &out, ret);
  335. s->in_buffer_count -= consumed;
  336. s->in_buffer_index += consumed;
  337. if(!in_count)
  338. break;
  339. if(s->in_buffer_count <= border){
  340. buf_set(&in, &in, -s->in_buffer_count);
  341. in_count += s->in_buffer_count;
  342. s->in_buffer_count=0;
  343. s->in_buffer_index=0;
  344. border = 0;
  345. }
  346. }
  347. if(in_count && !s->in_buffer_count){
  348. s->in_buffer_index=0;
  349. ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
  350. out_count -= ret;
  351. ret_sum += ret;
  352. buf_set(&out, &out, ret);
  353. in_count -= consumed;
  354. buf_set(&in, &in, consumed);
  355. }
  356. //TODO is this check sane considering the advanced copy avoidance below
  357. size= s->in_buffer_index + s->in_buffer_count + in_count;
  358. if( size > s->in_buffer.count
  359. && s->in_buffer_count + in_count <= s->in_buffer_index){
  360. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  361. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  362. s->in_buffer_index=0;
  363. }else
  364. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  365. return ret;
  366. if(in_count){
  367. int count= in_count;
  368. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  369. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  370. copy(&tmp, &in, /*in_*/count);
  371. s->in_buffer_count += count;
  372. in_count -= count;
  373. border += count;
  374. buf_set(&in, &in, count);
  375. s->resample_in_constraint= 0;
  376. if(s->in_buffer_count != count || in_count)
  377. continue;
  378. }
  379. break;
  380. }while(1);
  381. s->resample_in_constraint= !!out_count;
  382. return ret_sum;
  383. }
  384. static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
  385. AudioData *in , int in_count){
  386. AudioData *postin, *midbuf, *preout;
  387. int ret/*, in_max*/;
  388. AudioData preout_tmp, midbuf_tmp;
  389. if(s->full_convert){
  390. av_assert0(!s->resample);
  391. swri_audio_convert(s->full_convert, out, in, in_count);
  392. return out_count;
  393. }
  394. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  395. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  396. if((ret=realloc_audio(&s->postin, in_count))<0)
  397. return ret;
  398. if(s->resample_first){
  399. av_assert0(s->midbuf.ch_count == s->used_ch_count);
  400. if((ret=realloc_audio(&s->midbuf, out_count))<0)
  401. return ret;
  402. }else{
  403. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  404. if((ret=realloc_audio(&s->midbuf, in_count))<0)
  405. return ret;
  406. }
  407. if((ret=realloc_audio(&s->preout, out_count))<0)
  408. return ret;
  409. postin= &s->postin;
  410. midbuf_tmp= s->midbuf;
  411. midbuf= &midbuf_tmp;
  412. preout_tmp= s->preout;
  413. preout= &preout_tmp;
  414. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
  415. postin= in;
  416. if(s->resample_first ? !s->resample : !s->rematrix)
  417. midbuf= postin;
  418. if(s->resample_first ? !s->rematrix : !s->resample)
  419. preout= midbuf;
  420. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
  421. if(preout==in){
  422. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  423. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  424. copy(out, in, out_count);
  425. return out_count;
  426. }
  427. else if(preout==postin) preout= midbuf= postin= out;
  428. else if(preout==midbuf) preout= midbuf= out;
  429. else preout= out;
  430. }
  431. if(in != postin){
  432. swri_audio_convert(s->in_convert, postin, in, in_count);
  433. }
  434. if(s->resample_first){
  435. if(postin != midbuf)
  436. out_count= resample(s, midbuf, out_count, postin, in_count);
  437. if(midbuf != preout)
  438. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  439. }else{
  440. if(postin != midbuf)
  441. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  442. if(midbuf != preout)
  443. out_count= resample(s, preout, out_count, midbuf, in_count);
  444. }
  445. if(preout != out && out_count){
  446. if(s->dither_method){
  447. int ch;
  448. int dither_count= FFMAX(out_count, 1<<16);
  449. av_assert0(preout != in);
  450. if((ret=realloc_audio(&s->dither, dither_count))<0)
  451. return ret;
  452. if(ret)
  453. for(ch=0; ch<s->dither.ch_count; ch++)
  454. swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
  455. av_assert0(s->dither.ch_count == preout->ch_count);
  456. if(s->dither_pos + out_count > s->dither.count)
  457. s->dither_pos = 0;
  458. for(ch=0; ch<preout->ch_count; ch++)
  459. swri_sum2(s->int_sample_fmt, preout->ch[ch], preout->ch[ch], s->dither.ch[ch] + s->dither.bps * s->dither_pos, 1, 1, out_count);
  460. s->dither_pos += out_count;
  461. }
  462. //FIXME packed doesnt need more than 1 chan here!
  463. swri_audio_convert(s->out_convert, out, preout, out_count);
  464. }
  465. return out_count;
  466. }
  467. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  468. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  469. AudioData * in= &s->in;
  470. AudioData *out= &s->out;
  471. if(!in_arg){
  472. if(s->in_buffer_count){
  473. if (s->resample && !s->flushed) {
  474. AudioData *a= &s->in_buffer;
  475. int i, j, ret;
  476. if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
  477. return ret;
  478. av_assert0(a->planar);
  479. for(i=0; i<a->ch_count; i++){
  480. for(j=0; j<s->in_buffer_count; j++){
  481. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  482. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  483. }
  484. }
  485. s->in_buffer_count += (s->in_buffer_count+1)/2;
  486. s->resample_in_constraint = 0;
  487. s->flushed = 1;
  488. }
  489. }else{
  490. return 0;
  491. }
  492. }else
  493. fill_audiodata(in , (void*)in_arg);
  494. fill_audiodata(out, out_arg);
  495. if(s->resample){
  496. return swr_convert_internal(s, out, out_count, in, in_count);
  497. }else{
  498. AudioData tmp= *in;
  499. int ret2=0;
  500. int ret, size;
  501. size = FFMIN(out_count, s->in_buffer_count);
  502. if(size){
  503. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  504. ret= swr_convert_internal(s, out, size, &tmp, size);
  505. if(ret<0)
  506. return ret;
  507. ret2= ret;
  508. s->in_buffer_count -= ret;
  509. s->in_buffer_index += ret;
  510. buf_set(out, out, ret);
  511. out_count -= ret;
  512. if(!s->in_buffer_count)
  513. s->in_buffer_index = 0;
  514. }
  515. if(in_count){
  516. size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
  517. if(in_count > out_count) { //FIXME move after swr_convert_internal
  518. if( size > s->in_buffer.count
  519. && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
  520. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  521. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  522. s->in_buffer_index=0;
  523. }else
  524. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  525. return ret;
  526. }
  527. if(out_count){
  528. size = FFMIN(in_count, out_count);
  529. ret= swr_convert_internal(s, out, size, in, size);
  530. if(ret<0)
  531. return ret;
  532. buf_set(in, in, ret);
  533. in_count -= ret;
  534. ret2 += ret;
  535. }
  536. if(in_count){
  537. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  538. copy(&tmp, in, in_count);
  539. s->in_buffer_count += in_count;
  540. }
  541. }
  542. return ret2;
  543. }
  544. }