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.

656 lines
27KB

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