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.

662 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. if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
  188. if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_S16P){
  189. s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
  190. }else if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_FLTP){
  191. s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
  192. }else{
  193. av_log(s, AV_LOG_DEBUG, "Using double precission mode\n");
  194. s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
  195. }
  196. }
  197. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
  198. &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
  199. &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
  200. &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
  201. 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));
  202. return AVERROR(EINVAL);
  203. }
  204. set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
  205. set_audiodata_fmt(&s->out, s->out_sample_fmt);
  206. if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
  207. 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);
  208. }else
  209. swri_resample_free(&s->resample);
  210. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
  211. && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
  212. && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
  213. && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
  214. && s->resample){
  215. av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16/s32/flt/dbl\n");
  216. return -1;
  217. }
  218. if(!s->used_ch_count)
  219. s->used_ch_count= s->in.ch_count;
  220. if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
  221. av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
  222. s-> in_ch_layout= 0;
  223. }
  224. if(!s-> in_ch_layout)
  225. s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
  226. if(!s->out_ch_layout)
  227. s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
  228. s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
  229. s->rematrix_custom;
  230. #define RSC 1 //FIXME finetune
  231. if(!s-> in.ch_count)
  232. s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
  233. if(!s->used_ch_count)
  234. s->used_ch_count= s->in.ch_count;
  235. if(!s->out.ch_count)
  236. s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
  237. if(!s-> in.ch_count){
  238. av_assert0(!s->in_ch_layout);
  239. av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
  240. return -1;
  241. }
  242. if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
  243. av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
  244. return -1;
  245. }
  246. av_assert0(s->used_ch_count);
  247. av_assert0(s->out.ch_count);
  248. s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
  249. s->in_buffer= s->in;
  250. if(!s->resample && !s->rematrix && !s->channel_map && !s->dither_method){
  251. s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
  252. s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
  253. return 0;
  254. }
  255. s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
  256. s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
  257. s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
  258. s->int_sample_fmt, s->out.ch_count, NULL, 0);
  259. s->postin= s->in;
  260. s->preout= s->out;
  261. s->midbuf= s->in;
  262. if(s->channel_map){
  263. s->postin.ch_count=
  264. s->midbuf.ch_count= s->used_ch_count;
  265. if(s->resample)
  266. s->in_buffer.ch_count= s->used_ch_count;
  267. }
  268. if(!s->resample_first){
  269. s->midbuf.ch_count= s->out.ch_count;
  270. if(s->resample)
  271. s->in_buffer.ch_count = s->out.ch_count;
  272. }
  273. set_audiodata_fmt(&s->postin, s->int_sample_fmt);
  274. set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
  275. set_audiodata_fmt(&s->preout, s->int_sample_fmt);
  276. if(s->resample){
  277. set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
  278. }
  279. s->dither = s->preout;
  280. if(s->rematrix || s->dither_method)
  281. return swri_rematrix_init(s);
  282. return 0;
  283. }
  284. static int realloc_audio(AudioData *a, int count){
  285. int i, countb;
  286. AudioData old;
  287. if(a->count >= count)
  288. return 0;
  289. count*=2;
  290. countb= FFALIGN(count*a->bps, 32);
  291. old= *a;
  292. av_assert0(a->bps);
  293. av_assert0(a->ch_count);
  294. a->data= av_malloc(countb*a->ch_count);
  295. if(!a->data)
  296. return AVERROR(ENOMEM);
  297. for(i=0; i<a->ch_count; i++){
  298. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  299. if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  300. }
  301. if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
  302. av_free(old.data);
  303. a->count= count;
  304. return 1;
  305. }
  306. static void copy(AudioData *out, AudioData *in,
  307. int count){
  308. av_assert0(out->planar == in->planar);
  309. av_assert0(out->bps == in->bps);
  310. av_assert0(out->ch_count == in->ch_count);
  311. if(out->planar){
  312. int ch;
  313. for(ch=0; ch<out->ch_count; ch++)
  314. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  315. }else
  316. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  317. }
  318. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  319. int i;
  320. if(out->planar){
  321. for(i=0; i<out->ch_count; i++)
  322. out->ch[i]= in_arg[i];
  323. }else{
  324. for(i=0; i<out->ch_count; i++)
  325. out->ch[i]= in_arg[0] + i*out->bps;
  326. }
  327. }
  328. /**
  329. *
  330. * out may be equal in.
  331. */
  332. static void buf_set(AudioData *out, AudioData *in, int count){
  333. int ch;
  334. if(in->planar){
  335. for(ch=0; ch<out->ch_count; ch++)
  336. out->ch[ch]= in->ch[ch] + count*out->bps;
  337. }else{
  338. for(ch=0; ch<out->ch_count; ch++)
  339. out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
  340. }
  341. }
  342. /**
  343. *
  344. * @return number of samples output per channel
  345. */
  346. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  347. const AudioData * in_param, int in_count){
  348. AudioData in, out, tmp;
  349. int ret_sum=0;
  350. int border=0;
  351. tmp=out=*out_param;
  352. in = *in_param;
  353. do{
  354. int ret, size, consumed;
  355. if(!s->resample_in_constraint && s->in_buffer_count){
  356. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  357. ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  358. out_count -= ret;
  359. ret_sum += ret;
  360. buf_set(&out, &out, ret);
  361. s->in_buffer_count -= consumed;
  362. s->in_buffer_index += consumed;
  363. if(!in_count)
  364. break;
  365. if(s->in_buffer_count <= border){
  366. buf_set(&in, &in, -s->in_buffer_count);
  367. in_count += s->in_buffer_count;
  368. s->in_buffer_count=0;
  369. s->in_buffer_index=0;
  370. border = 0;
  371. }
  372. }
  373. if(in_count && !s->in_buffer_count){
  374. s->in_buffer_index=0;
  375. ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
  376. out_count -= ret;
  377. ret_sum += ret;
  378. buf_set(&out, &out, ret);
  379. in_count -= consumed;
  380. buf_set(&in, &in, consumed);
  381. }
  382. //TODO is this check sane considering the advanced copy avoidance below
  383. size= s->in_buffer_index + s->in_buffer_count + in_count;
  384. if( size > s->in_buffer.count
  385. && s->in_buffer_count + in_count <= s->in_buffer_index){
  386. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  387. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  388. s->in_buffer_index=0;
  389. }else
  390. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  391. return ret;
  392. if(in_count){
  393. int count= in_count;
  394. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  395. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  396. copy(&tmp, &in, /*in_*/count);
  397. s->in_buffer_count += count;
  398. in_count -= count;
  399. border += count;
  400. buf_set(&in, &in, count);
  401. s->resample_in_constraint= 0;
  402. if(s->in_buffer_count != count || in_count)
  403. continue;
  404. }
  405. break;
  406. }while(1);
  407. s->resample_in_constraint= !!out_count;
  408. return ret_sum;
  409. }
  410. static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
  411. AudioData *in , int in_count){
  412. AudioData *postin, *midbuf, *preout;
  413. int ret/*, in_max*/;
  414. AudioData preout_tmp, midbuf_tmp;
  415. if(s->full_convert){
  416. av_assert0(!s->resample);
  417. swri_audio_convert(s->full_convert, out, in, in_count);
  418. return out_count;
  419. }
  420. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  421. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  422. if((ret=realloc_audio(&s->postin, in_count))<0)
  423. return ret;
  424. if(s->resample_first){
  425. av_assert0(s->midbuf.ch_count == s->used_ch_count);
  426. if((ret=realloc_audio(&s->midbuf, out_count))<0)
  427. return ret;
  428. }else{
  429. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  430. if((ret=realloc_audio(&s->midbuf, in_count))<0)
  431. return ret;
  432. }
  433. if((ret=realloc_audio(&s->preout, out_count))<0)
  434. return ret;
  435. postin= &s->postin;
  436. midbuf_tmp= s->midbuf;
  437. midbuf= &midbuf_tmp;
  438. preout_tmp= s->preout;
  439. preout= &preout_tmp;
  440. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
  441. postin= in;
  442. if(s->resample_first ? !s->resample : !s->rematrix)
  443. midbuf= postin;
  444. if(s->resample_first ? !s->rematrix : !s->resample)
  445. preout= midbuf;
  446. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
  447. if(preout==in){
  448. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  449. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  450. copy(out, in, out_count);
  451. return out_count;
  452. }
  453. else if(preout==postin) preout= midbuf= postin= out;
  454. else if(preout==midbuf) preout= midbuf= out;
  455. else preout= out;
  456. }
  457. if(in != postin){
  458. swri_audio_convert(s->in_convert, postin, in, in_count);
  459. }
  460. if(s->resample_first){
  461. if(postin != midbuf)
  462. out_count= resample(s, midbuf, out_count, postin, in_count);
  463. if(midbuf != preout)
  464. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  465. }else{
  466. if(postin != midbuf)
  467. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  468. if(midbuf != preout)
  469. out_count= resample(s, preout, out_count, midbuf, in_count);
  470. }
  471. if(preout != out && out_count){
  472. if(s->dither_method){
  473. int ch;
  474. int dither_count= FFMAX(out_count, 1<<16);
  475. av_assert0(preout != in);
  476. if((ret=realloc_audio(&s->dither, dither_count))<0)
  477. return ret;
  478. if(ret)
  479. for(ch=0; ch<s->dither.ch_count; ch++)
  480. swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
  481. av_assert0(s->dither.ch_count == preout->ch_count);
  482. if(s->dither_pos + out_count > s->dither.count)
  483. s->dither_pos = 0;
  484. for(ch=0; ch<preout->ch_count; ch++)
  485. 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);
  486. s->dither_pos += out_count;
  487. }
  488. //FIXME packed doesnt need more than 1 chan here!
  489. swri_audio_convert(s->out_convert, out, preout, out_count);
  490. }
  491. return out_count;
  492. }
  493. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  494. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  495. AudioData * in= &s->in;
  496. AudioData *out= &s->out;
  497. if(!in_arg){
  498. if(s->in_buffer_count){
  499. if (s->resample && !s->flushed) {
  500. AudioData *a= &s->in_buffer;
  501. int i, j, ret;
  502. if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
  503. return ret;
  504. av_assert0(a->planar);
  505. for(i=0; i<a->ch_count; i++){
  506. for(j=0; j<s->in_buffer_count; j++){
  507. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  508. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  509. }
  510. }
  511. s->in_buffer_count += (s->in_buffer_count+1)/2;
  512. s->resample_in_constraint = 0;
  513. s->flushed = 1;
  514. }
  515. }else{
  516. return 0;
  517. }
  518. }else
  519. fill_audiodata(in , (void*)in_arg);
  520. fill_audiodata(out, out_arg);
  521. if(s->resample){
  522. return swr_convert_internal(s, out, out_count, in, in_count);
  523. }else{
  524. AudioData tmp= *in;
  525. int ret2=0;
  526. int ret, size;
  527. size = FFMIN(out_count, s->in_buffer_count);
  528. if(size){
  529. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  530. ret= swr_convert_internal(s, out, size, &tmp, size);
  531. if(ret<0)
  532. return ret;
  533. ret2= ret;
  534. s->in_buffer_count -= ret;
  535. s->in_buffer_index += ret;
  536. buf_set(out, out, ret);
  537. out_count -= ret;
  538. if(!s->in_buffer_count)
  539. s->in_buffer_index = 0;
  540. }
  541. if(in_count){
  542. size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
  543. if(in_count > out_count) { //FIXME move after swr_convert_internal
  544. if( size > s->in_buffer.count
  545. && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
  546. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  547. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  548. s->in_buffer_index=0;
  549. }else
  550. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  551. return ret;
  552. }
  553. if(out_count){
  554. size = FFMIN(in_count, out_count);
  555. ret= swr_convert_internal(s, out, size, in, size);
  556. if(ret<0)
  557. return ret;
  558. buf_set(in, in, ret);
  559. in_count -= ret;
  560. ret2 += ret;
  561. }
  562. if(in_count){
  563. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  564. copy(&tmp, in, in_count);
  565. s->in_buffer_count += in_count;
  566. }
  567. }
  568. return ret2;
  569. }
  570. }