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.

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