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.

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