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.

657 lines
27KB

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