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
24KB

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