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.

649 lines
26KB

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