swresample.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  1. /*
  2. * Copyright (C) 2011-2013 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/channel_layout.h"
  25. #include "libavutil/internal.h"
  26. #include <float.h>
  27. #define ALIGN 32
  28. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
  29. if(!s || s->in_convert) // s needs to be allocated but not initialized
  30. return AVERROR(EINVAL);
  31. s->channel_map = channel_map;
  32. return 0;
  33. }
  34. #if FF_API_OLD_CHANNEL_LAYOUT
  35. FF_DISABLE_DEPRECATION_WARNINGS
  36. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  37. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  38. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  39. int log_offset, void *log_ctx){
  40. if(!s) s= swr_alloc();
  41. if(!s) return NULL;
  42. s->log_level_offset= log_offset;
  43. s->log_ctx= log_ctx;
  44. if (av_opt_set_int(s, "ocl", out_ch_layout, 0) < 0)
  45. goto fail;
  46. if (av_opt_set_int(s, "osf", out_sample_fmt, 0) < 0)
  47. goto fail;
  48. if (av_opt_set_int(s, "osr", out_sample_rate, 0) < 0)
  49. goto fail;
  50. if (av_opt_set_int(s, "icl", in_ch_layout, 0) < 0)
  51. goto fail;
  52. if (av_opt_set_int(s, "isf", in_sample_fmt, 0) < 0)
  53. goto fail;
  54. if (av_opt_set_int(s, "isr", in_sample_rate, 0) < 0)
  55. goto fail;
  56. if (av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> user_in_ch_layout), 0) < 0)
  57. goto fail;
  58. if (av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->user_out_ch_layout), 0) < 0)
  59. goto fail;
  60. av_opt_set_int(s, "uch", 0, 0);
  61. return s;
  62. fail:
  63. av_log(s, AV_LOG_ERROR, "Failed to set option\n");
  64. swr_free(&s);
  65. return NULL;
  66. }
  67. FF_ENABLE_DEPRECATION_WARNINGS
  68. #endif
  69. int swr_alloc_set_opts2(struct SwrContext **ps,
  70. const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  71. const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  72. int log_offset, void *log_ctx) {
  73. struct SwrContext *s = *ps;
  74. int ret;
  75. if (!s) s = swr_alloc();
  76. if (!s) return AVERROR(ENOMEM);
  77. *ps = s;
  78. s->log_level_offset = log_offset;
  79. s->log_ctx = log_ctx;
  80. if ((ret = av_opt_set_chlayout(s, "ochl", out_ch_layout, 0)) < 0)
  81. goto fail;
  82. if ((ret = av_opt_set_int(s, "osf", out_sample_fmt, 0)) < 0)
  83. goto fail;
  84. if ((ret = av_opt_set_int(s, "osr", out_sample_rate, 0)) < 0)
  85. goto fail;
  86. if ((ret = av_opt_set_chlayout(s, "ichl", in_ch_layout, 0)) < 0)
  87. goto fail;
  88. if ((ret = av_opt_set_int(s, "isf", in_sample_fmt, 0)) < 0)
  89. goto fail;
  90. if ((ret = av_opt_set_int(s, "isr", in_sample_rate, 0)) < 0)
  91. goto fail;
  92. av_opt_set_int(s, "uch", 0, 0);
  93. #if FF_API_OLD_CHANNEL_LAYOUT
  94. // Clear old API values so they don't take precedence in swr_init()
  95. av_opt_set_int(s, "icl", 0, 0);
  96. av_opt_set_int(s, "ocl", 0, 0);
  97. av_opt_set_int(s, "ich", 0, 0);
  98. av_opt_set_int(s, "och", 0, 0);
  99. #endif
  100. return 0;
  101. fail:
  102. av_log(s, AV_LOG_ERROR, "Failed to set option\n");
  103. swr_free(ps);
  104. return ret;
  105. }
  106. static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
  107. a->fmt = fmt;
  108. a->bps = av_get_bytes_per_sample(fmt);
  109. a->planar= av_sample_fmt_is_planar(fmt);
  110. if (a->ch_count == 1)
  111. a->planar = 1;
  112. }
  113. static void free_temp(AudioData *a){
  114. av_free(a->data);
  115. memset(a, 0, sizeof(*a));
  116. }
  117. static void clear_context(SwrContext *s){
  118. s->in_buffer_index= 0;
  119. s->in_buffer_count= 0;
  120. s->resample_in_constraint= 0;
  121. memset(s->in.ch, 0, sizeof(s->in.ch));
  122. memset(s->out.ch, 0, sizeof(s->out.ch));
  123. free_temp(&s->postin);
  124. free_temp(&s->midbuf);
  125. free_temp(&s->preout);
  126. free_temp(&s->in_buffer);
  127. free_temp(&s->silence);
  128. free_temp(&s->drop_temp);
  129. free_temp(&s->dither.noise);
  130. free_temp(&s->dither.temp);
  131. av_channel_layout_uninit(&s->in_ch_layout);
  132. av_channel_layout_uninit(&s->out_ch_layout);
  133. av_channel_layout_uninit(&s->used_ch_layout);
  134. swri_audio_convert_free(&s-> in_convert);
  135. swri_audio_convert_free(&s->out_convert);
  136. swri_audio_convert_free(&s->full_convert);
  137. swri_rematrix_free(s);
  138. s->delayed_samples_fixup = 0;
  139. s->flushed = 0;
  140. }
  141. av_cold void swr_free(SwrContext **ss){
  142. SwrContext *s= *ss;
  143. if(s){
  144. clear_context(s);
  145. av_channel_layout_uninit(&s->user_in_chlayout);
  146. av_channel_layout_uninit(&s->user_out_chlayout);
  147. av_channel_layout_uninit(&s->user_used_chlayout);
  148. if (s->resampler)
  149. s->resampler->free(&s->resample);
  150. }
  151. av_freep(ss);
  152. }
  153. av_cold void swr_close(SwrContext *s){
  154. clear_context(s);
  155. }
  156. av_cold int swr_init(struct SwrContext *s){
  157. int ret;
  158. char l1[1024], l2[1024];
  159. clear_context(s);
  160. if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
  161. av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
  162. return AVERROR(EINVAL);
  163. }
  164. if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
  165. av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
  166. return AVERROR(EINVAL);
  167. }
  168. if(s-> in_sample_rate <= 0){
  169. av_log(s, AV_LOG_ERROR, "Requested input sample rate %d is invalid\n", s->in_sample_rate);
  170. return AVERROR(EINVAL);
  171. }
  172. if(s->out_sample_rate <= 0){
  173. av_log(s, AV_LOG_ERROR, "Requested output sample rate %d is invalid\n", s->out_sample_rate);
  174. return AVERROR(EINVAL);
  175. }
  176. #if FF_API_OLD_CHANNEL_LAYOUT
  177. s->out.ch_count = s-> user_out_ch_count;
  178. s-> in.ch_count = s-> user_in_ch_count;
  179. // if the old/new fields are set inconsistently, prefer the old ones
  180. if (s->user_used_ch_count && s->user_used_ch_count != s->user_used_chlayout.nb_channels) {
  181. av_channel_layout_uninit(&s->used_ch_layout);
  182. s->used_ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  183. s->used_ch_layout.nb_channels = s->user_used_ch_count;
  184. } else if (av_channel_layout_check(&s->user_used_chlayout)) {
  185. ret = av_channel_layout_copy(&s->used_ch_layout, &s->user_used_chlayout);
  186. if (ret < 0)
  187. return ret;
  188. }
  189. if ((s->user_in_ch_count && s->user_in_ch_count != s->user_in_chlayout.nb_channels) ||
  190. (s->user_in_ch_layout && (s->user_in_chlayout.order != AV_CHANNEL_ORDER_NATIVE ||
  191. s->user_in_chlayout.u.mask != s->user_in_ch_layout))) {
  192. av_channel_layout_uninit(&s->in_ch_layout);
  193. if (s->user_in_ch_layout)
  194. av_channel_layout_from_mask(&s->in_ch_layout, s->user_in_ch_layout);
  195. else {
  196. s->in_ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  197. s->in_ch_layout.nb_channels = s->user_in_ch_count;
  198. }
  199. } else if (av_channel_layout_check(&s->user_in_chlayout))
  200. av_channel_layout_copy(&s->in_ch_layout, &s->user_in_chlayout);
  201. if ((s->user_out_ch_count && s->user_out_ch_count != s->user_out_chlayout.nb_channels) ||
  202. (s->user_out_ch_layout && (s->user_out_chlayout.order != AV_CHANNEL_ORDER_NATIVE ||
  203. s->user_out_chlayout.u.mask != s->user_out_ch_layout))) {
  204. av_channel_layout_uninit(&s->out_ch_layout);
  205. if (s->user_out_ch_layout)
  206. av_channel_layout_from_mask(&s->out_ch_layout, s->user_out_ch_layout);
  207. else {
  208. s->out_ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
  209. s->out_ch_layout.nb_channels = s->user_out_ch_count;
  210. }
  211. } else if (av_channel_layout_check(&s->user_out_chlayout))
  212. av_channel_layout_copy(&s->out_ch_layout, &s->user_out_chlayout);
  213. if (!s->out.ch_count)
  214. s->out.ch_count = s->out_ch_layout.nb_channels;
  215. if (!s-> in.ch_count)
  216. s-> in.ch_count = s->in_ch_layout.nb_channels;
  217. if (!(ret = av_channel_layout_check(&s->in_ch_layout)) || s->in_ch_layout.nb_channels > SWR_CH_MAX) {
  218. if (ret)
  219. av_channel_layout_describe(&s->in_ch_layout, l1, sizeof(l1));
  220. av_log(s, AV_LOG_WARNING, "Input channel layout \"%s\" is invalid or unsupported.\n", ret ? l1 : "");
  221. return AVERROR(EINVAL);
  222. }
  223. if (!(ret = av_channel_layout_check(&s->out_ch_layout)) || s->out_ch_layout.nb_channels > SWR_CH_MAX) {
  224. if (ret)
  225. av_channel_layout_describe(&s->out_ch_layout, l2, sizeof(l2));
  226. av_log(s, AV_LOG_WARNING, "Output channel layout \"%s\" is invalid or unsupported.\n", ret ? l2 : "");
  227. return AVERROR(EINVAL);
  228. }
  229. #else
  230. s->out.ch_count = s-> user_out_chlayout.nb_channels;
  231. s-> in.ch_count = s-> user_in_chlayout.nb_channels;
  232. if (!(ret = av_channel_layout_check(&s->user_in_chlayout)) || s->user_in_chlayout.nb_channels > SWR_CH_MAX) {
  233. if (ret)
  234. av_channel_layout_describe(&s->user_in_chlayout, l1, sizeof(l1));
  235. av_log(s, AV_LOG_WARNING, "Input channel layout \"%s\" is invalid or unsupported.\n", ret ? l1 : "");
  236. return AVERROR(EINVAL);
  237. }
  238. if (!(ret = av_channel_layout_check(&s->user_out_chlayout)) || s->user_out_chlayout.nb_channels > SWR_CH_MAX) {
  239. if (ret)
  240. av_channel_layout_describe(&s->user_out_chlayout, l2, sizeof(l2));
  241. av_log(s, AV_LOG_WARNING, "Output channel layout \"%s\" is invalid or unsupported.\n", ret ? l2 : "");
  242. return AVERROR(EINVAL);
  243. }
  244. ret = av_channel_layout_copy(&s->in_ch_layout, &s->user_in_chlayout);
  245. ret |= av_channel_layout_copy(&s->out_ch_layout, &s->user_out_chlayout);
  246. ret |= av_channel_layout_copy(&s->used_ch_layout, &s->user_used_chlayout);
  247. if (ret < 0)
  248. return ret;
  249. #endif
  250. s->int_sample_fmt= s->user_int_sample_fmt;
  251. s->dither.method = s->user_dither_method;
  252. switch(s->engine){
  253. #if CONFIG_LIBSOXR
  254. case SWR_ENGINE_SOXR: s->resampler = &swri_soxr_resampler; break;
  255. #endif
  256. case SWR_ENGINE_SWR : s->resampler = &swri_resampler; break;
  257. default:
  258. av_log(s, AV_LOG_ERROR, "Requested resampling engine is unavailable\n");
  259. return AVERROR(EINVAL);
  260. }
  261. if (!av_channel_layout_check(&s->used_ch_layout))
  262. av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
  263. if (s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels)
  264. av_channel_layout_uninit(&s->in_ch_layout);
  265. if (s->used_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
  266. av_channel_layout_default(&s->used_ch_layout, s->used_ch_layout.nb_channels);
  267. if (s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
  268. ret = av_channel_layout_copy(&s->in_ch_layout, &s->used_ch_layout);
  269. if (ret < 0)
  270. return ret;
  271. }
  272. if (s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
  273. av_channel_layout_default(&s->out_ch_layout, s->out.ch_count);
  274. s->rematrix = av_channel_layout_compare(&s->out_ch_layout, &s->in_ch_layout) ||
  275. s->rematrix_volume!=1.0 ||
  276. s->rematrix_custom;
  277. if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
  278. if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
  279. && av_get_bytes_per_sample(s->out_sample_fmt) <= 2){
  280. s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
  281. }else if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
  282. && !s->rematrix
  283. && s->out_sample_rate==s->in_sample_rate
  284. && !(s->flags & SWR_FLAG_RESAMPLE)){
  285. s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
  286. }else if( av_get_planar_sample_fmt(s-> in_sample_fmt) == AV_SAMPLE_FMT_S32P
  287. && av_get_planar_sample_fmt(s->out_sample_fmt) == AV_SAMPLE_FMT_S32P
  288. && !s->rematrix
  289. && s->out_sample_rate == s->in_sample_rate
  290. && !(s->flags & SWR_FLAG_RESAMPLE)
  291. && s->engine != SWR_ENGINE_SOXR){
  292. s->int_sample_fmt= AV_SAMPLE_FMT_S32P;
  293. }else if(av_get_bytes_per_sample(s->in_sample_fmt) <= 4){
  294. s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
  295. }else{
  296. s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
  297. }
  298. }
  299. av_log(s, AV_LOG_DEBUG, "Using %s internally between filters\n", av_get_sample_fmt_name(s->int_sample_fmt));
  300. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
  301. &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
  302. &&s->int_sample_fmt != AV_SAMPLE_FMT_S64P
  303. &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
  304. &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
  305. av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, s16p/s32p/s64p/fltp/dblp are supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
  306. return AVERROR(EINVAL);
  307. }
  308. set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
  309. set_audiodata_fmt(&s->out, s->out_sample_fmt);
  310. if (s->firstpts_in_samples != AV_NOPTS_VALUE) {
  311. if (!s->async && s->min_compensation >= FLT_MAX/2)
  312. s->async = 1;
  313. s->firstpts =
  314. s->outpts = s->firstpts_in_samples * s->out_sample_rate;
  315. } else
  316. s->firstpts = AV_NOPTS_VALUE;
  317. if (s->async) {
  318. if (s->min_compensation >= FLT_MAX/2)
  319. s->min_compensation = 0.001;
  320. if (s->async > 1.0001) {
  321. s->max_soft_compensation = s->async / (double) s->in_sample_rate;
  322. }
  323. }
  324. if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
  325. s->resample = s->resampler->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, s->filter_type, s->kaiser_beta, s->precision, s->cheby, s->exact_rational);
  326. if (!s->resample) {
  327. av_log(s, AV_LOG_ERROR, "Failed to initialize resampler\n");
  328. return AVERROR(ENOMEM);
  329. }
  330. }else
  331. s->resampler->free(&s->resample);
  332. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
  333. && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
  334. && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
  335. && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
  336. && s->resample){
  337. av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16p/s32p/fltp/dblp\n");
  338. ret = AVERROR(EINVAL);
  339. goto fail;
  340. }
  341. #define RSC 1 //FIXME finetune
  342. if(!s-> in.ch_count)
  343. s-> in.ch_count = s->in_ch_layout.nb_channels;
  344. if (!av_channel_layout_check(&s->used_ch_layout))
  345. av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
  346. if(!s->out.ch_count)
  347. s->out.ch_count = s->out_ch_layout.nb_channels;
  348. if(!s-> in.ch_count){
  349. av_assert0(s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
  350. av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
  351. ret = AVERROR(EINVAL);
  352. goto fail;
  353. }
  354. av_channel_layout_describe(&s->out_ch_layout, l2, sizeof(l2));
  355. #if FF_API_OLD_CHANNEL_LAYOUT
  356. if (s->out_ch_layout.order != AV_CHANNEL_ORDER_UNSPEC && s->out.ch_count != s->out_ch_layout.nb_channels) {
  357. av_log(s, AV_LOG_ERROR, "Output channel layout %s mismatches specified channel count %d\n", l2, s->out.ch_count);
  358. ret = AVERROR(EINVAL);
  359. goto fail;
  360. }
  361. #endif
  362. av_channel_layout_describe(&s->in_ch_layout, l1, sizeof(l1));
  363. if (s->in_ch_layout.order != AV_CHANNEL_ORDER_UNSPEC && s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels) {
  364. av_log(s, AV_LOG_ERROR, "Input channel layout %s mismatches specified channel count %d\n", l1, s->used_ch_layout.nb_channels);
  365. ret = AVERROR(EINVAL);
  366. goto fail;
  367. }
  368. if (( s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC
  369. || s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) && s->used_ch_layout.nb_channels != s->out.ch_count && !s->rematrix_custom) {
  370. av_log(s, AV_LOG_ERROR, "Rematrix is needed between %s and %s "
  371. "but there is not enough information to do it\n", l1, l2);
  372. ret = AVERROR(EINVAL);
  373. goto fail;
  374. }
  375. av_assert0(s->used_ch_layout.nb_channels);
  376. av_assert0(s->out.ch_count);
  377. s->resample_first= RSC*s->out.ch_count/s->used_ch_layout.nb_channels - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
  378. s->in_buffer= s->in;
  379. s->silence = s->in;
  380. s->drop_temp= s->out;
  381. if ((ret = swri_dither_init(s, s->out_sample_fmt, s->int_sample_fmt)) < 0)
  382. goto fail;
  383. if(!s->resample && !s->rematrix && !s->channel_map && !s->dither.method){
  384. s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
  385. s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
  386. return 0;
  387. }
  388. s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
  389. s-> in_sample_fmt, s->used_ch_layout.nb_channels, s->channel_map, 0);
  390. s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
  391. s->int_sample_fmt, s->out.ch_count, NULL, 0);
  392. if (!s->in_convert || !s->out_convert) {
  393. ret = AVERROR(ENOMEM);
  394. goto fail;
  395. }
  396. s->postin= s->in;
  397. s->preout= s->out;
  398. s->midbuf= s->in;
  399. if(s->channel_map){
  400. s->postin.ch_count=
  401. s->midbuf.ch_count= s->used_ch_layout.nb_channels;
  402. if(s->resample)
  403. s->in_buffer.ch_count= s->used_ch_layout.nb_channels;
  404. }
  405. if(!s->resample_first){
  406. s->midbuf.ch_count= s->out.ch_count;
  407. if(s->resample)
  408. s->in_buffer.ch_count = s->out.ch_count;
  409. }
  410. set_audiodata_fmt(&s->postin, s->int_sample_fmt);
  411. set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
  412. set_audiodata_fmt(&s->preout, s->int_sample_fmt);
  413. if(s->resample){
  414. set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
  415. }
  416. av_assert0(!s->preout.count);
  417. s->dither.noise = s->preout;
  418. s->dither.temp = s->preout;
  419. if (s->dither.method > SWR_DITHER_NS) {
  420. s->dither.noise.bps = 4;
  421. s->dither.noise.fmt = AV_SAMPLE_FMT_FLTP;
  422. s->dither.noise_scale = 1;
  423. }
  424. if(s->rematrix || s->dither.method) {
  425. ret = swri_rematrix_init(s);
  426. if (ret < 0)
  427. goto fail;
  428. }
  429. return 0;
  430. fail:
  431. swr_close(s);
  432. return ret;
  433. }
  434. int swri_realloc_audio(AudioData *a, int count){
  435. int i, countb;
  436. AudioData old;
  437. if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
  438. return AVERROR(EINVAL);
  439. if(a->count >= count)
  440. return 0;
  441. count*=2;
  442. countb= FFALIGN(count*a->bps, ALIGN);
  443. old= *a;
  444. av_assert0(a->bps);
  445. av_assert0(a->ch_count);
  446. a->data = av_calloc(countb, a->ch_count);
  447. if(!a->data)
  448. return AVERROR(ENOMEM);
  449. for(i=0; i<a->ch_count; i++){
  450. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  451. if(a->count && a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  452. }
  453. if(a->count && !a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
  454. av_freep(&old.data);
  455. a->count= count;
  456. return 1;
  457. }
  458. static void copy(AudioData *out, AudioData *in,
  459. int count){
  460. av_assert0(out->planar == in->planar);
  461. av_assert0(out->bps == in->bps);
  462. av_assert0(out->ch_count == in->ch_count);
  463. if(out->planar){
  464. int ch;
  465. for(ch=0; ch<out->ch_count; ch++)
  466. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  467. }else
  468. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  469. }
  470. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  471. int i;
  472. if(!in_arg){
  473. memset(out->ch, 0, sizeof(out->ch));
  474. }else if(out->planar){
  475. for(i=0; i<out->ch_count; i++)
  476. out->ch[i]= in_arg[i];
  477. }else{
  478. for(i=0; i<out->ch_count; i++)
  479. out->ch[i]= in_arg[0] + i*out->bps;
  480. }
  481. }
  482. static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  483. int i;
  484. if(out->planar){
  485. for(i=0; i<out->ch_count; i++)
  486. in_arg[i]= out->ch[i];
  487. }else{
  488. in_arg[0]= out->ch[0];
  489. }
  490. }
  491. /**
  492. *
  493. * out may be equal in.
  494. */
  495. static void buf_set(AudioData *out, AudioData *in, int count){
  496. int ch;
  497. if(in->planar){
  498. for(ch=0; ch<out->ch_count; ch++)
  499. out->ch[ch]= in->ch[ch] + count*out->bps;
  500. }else{
  501. for(ch=out->ch_count-1; ch>=0; ch--)
  502. out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
  503. }
  504. }
  505. /**
  506. *
  507. * @return number of samples output per channel
  508. */
  509. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  510. const AudioData * in_param, int in_count){
  511. AudioData in, out, tmp;
  512. int ret_sum=0;
  513. int border=0;
  514. int padless = ARCH_X86 && s->engine == SWR_ENGINE_SWR ? 7 : 0;
  515. av_assert1(s->in_buffer.ch_count == in_param->ch_count);
  516. av_assert1(s->in_buffer.planar == in_param->planar);
  517. av_assert1(s->in_buffer.fmt == in_param->fmt);
  518. tmp=out=*out_param;
  519. in = *in_param;
  520. border = s->resampler->invert_initial_buffer(s->resample, &s->in_buffer,
  521. &in, in_count, &s->in_buffer_index, &s->in_buffer_count);
  522. if (border == INT_MAX) {
  523. return 0;
  524. } else if (border < 0) {
  525. return border;
  526. } else if (border) {
  527. buf_set(&in, &in, border);
  528. in_count -= border;
  529. s->resample_in_constraint = 0;
  530. }
  531. do{
  532. int ret, size, consumed;
  533. if(!s->resample_in_constraint && s->in_buffer_count){
  534. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  535. ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  536. out_count -= ret;
  537. ret_sum += ret;
  538. buf_set(&out, &out, ret);
  539. s->in_buffer_count -= consumed;
  540. s->in_buffer_index += consumed;
  541. if(!in_count)
  542. break;
  543. if(s->in_buffer_count <= border){
  544. buf_set(&in, &in, -s->in_buffer_count);
  545. in_count += s->in_buffer_count;
  546. s->in_buffer_count=0;
  547. s->in_buffer_index=0;
  548. border = 0;
  549. }
  550. }
  551. if((s->flushed || in_count > padless) && !s->in_buffer_count){
  552. s->in_buffer_index=0;
  553. ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, FFMAX(in_count-padless, 0), &consumed);
  554. out_count -= ret;
  555. ret_sum += ret;
  556. buf_set(&out, &out, ret);
  557. in_count -= consumed;
  558. buf_set(&in, &in, consumed);
  559. }
  560. //TODO is this check sane considering the advanced copy avoidance below
  561. size= s->in_buffer_index + s->in_buffer_count + in_count;
  562. if( size > s->in_buffer.count
  563. && s->in_buffer_count + in_count <= s->in_buffer_index){
  564. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  565. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  566. s->in_buffer_index=0;
  567. }else
  568. if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
  569. return ret;
  570. if(in_count){
  571. int count= in_count;
  572. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  573. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  574. copy(&tmp, &in, /*in_*/count);
  575. s->in_buffer_count += count;
  576. in_count -= count;
  577. border += count;
  578. buf_set(&in, &in, count);
  579. s->resample_in_constraint= 0;
  580. if(s->in_buffer_count != count || in_count)
  581. continue;
  582. if (padless) {
  583. padless = 0;
  584. continue;
  585. }
  586. }
  587. break;
  588. }while(1);
  589. s->resample_in_constraint= !!out_count;
  590. return ret_sum;
  591. }
  592. static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
  593. AudioData *in , int in_count){
  594. AudioData *postin, *midbuf, *preout;
  595. int ret/*, in_max*/;
  596. AudioData preout_tmp, midbuf_tmp;
  597. if(s->full_convert){
  598. av_assert0(!s->resample);
  599. swri_audio_convert(s->full_convert, out, in, in_count);
  600. return out_count;
  601. }
  602. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  603. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  604. if((ret=swri_realloc_audio(&s->postin, in_count))<0)
  605. return ret;
  606. if(s->resample_first){
  607. av_assert0(s->midbuf.ch_count == s->used_ch_layout.nb_channels);
  608. if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)
  609. return ret;
  610. }else{
  611. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  612. if((ret=swri_realloc_audio(&s->midbuf, in_count))<0)
  613. return ret;
  614. }
  615. if((ret=swri_realloc_audio(&s->preout, out_count))<0)
  616. return ret;
  617. postin= &s->postin;
  618. midbuf_tmp= s->midbuf;
  619. midbuf= &midbuf_tmp;
  620. preout_tmp= s->preout;
  621. preout= &preout_tmp;
  622. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)
  623. postin= in;
  624. if(s->resample_first ? !s->resample : !s->rematrix)
  625. midbuf= postin;
  626. if(s->resample_first ? !s->rematrix : !s->resample)
  627. preout= midbuf;
  628. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar
  629. && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){
  630. if(preout==in){
  631. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  632. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  633. copy(out, in, out_count);
  634. return out_count;
  635. }
  636. else if(preout==postin) preout= midbuf= postin= out;
  637. else if(preout==midbuf) preout= midbuf= out;
  638. else preout= out;
  639. }
  640. if(in != postin){
  641. swri_audio_convert(s->in_convert, postin, in, in_count);
  642. }
  643. if(s->resample_first){
  644. if(postin != midbuf)
  645. if ((out_count = resample(s, midbuf, out_count, postin, in_count)) < 0)
  646. return out_count;
  647. if(midbuf != preout)
  648. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  649. }else{
  650. if(postin != midbuf)
  651. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  652. if(midbuf != preout)
  653. if ((out_count = resample(s, preout, out_count, midbuf, in_count)) < 0)
  654. return out_count;
  655. }
  656. if(preout != out && out_count){
  657. AudioData *conv_src = preout;
  658. if(s->dither.method){
  659. int ch;
  660. int dither_count= FFMAX(out_count, 1<<16);
  661. if (preout == in) {
  662. conv_src = &s->dither.temp;
  663. if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)
  664. return ret;
  665. }
  666. if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)
  667. return ret;
  668. if(ret)
  669. for(ch=0; ch<s->dither.noise.ch_count; ch++)
  670. if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828U, s->dither.noise.fmt))<0)
  671. return ret;
  672. av_assert0(s->dither.noise.ch_count == preout->ch_count);
  673. if(s->dither.noise_pos + out_count > s->dither.noise.count)
  674. s->dither.noise_pos = 0;
  675. if (s->dither.method < SWR_DITHER_NS){
  676. if (s->mix_2_1_simd) {
  677. int len1= out_count&~15;
  678. int off = len1 * preout->bps;
  679. if(len1)
  680. for(ch=0; ch<preout->ch_count; ch++)
  681. s->mix_2_1_simd(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_simd_one, 0, 0, len1);
  682. if(out_count != len1)
  683. for(ch=0; ch<preout->ch_count; ch++)
  684. s->mix_2_1_f(conv_src->ch[ch] + off, preout->ch[ch] + off, s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos + off, s->native_one, 0, 0, out_count - len1);
  685. } else {
  686. for(ch=0; ch<preout->ch_count; ch++)
  687. s->mix_2_1_f(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, s->native_one, 0, 0, out_count);
  688. }
  689. } else {
  690. switch(s->int_sample_fmt) {
  691. case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;
  692. case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;
  693. case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;
  694. case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;
  695. }
  696. }
  697. s->dither.noise_pos += out_count;
  698. }
  699. //FIXME packed doesn't need more than 1 chan here!
  700. swri_audio_convert(s->out_convert, out, conv_src, out_count);
  701. }
  702. return out_count;
  703. }
  704. int swr_is_initialized(struct SwrContext *s) {
  705. return !!s->in_buffer.ch_count;
  706. }
  707. int attribute_align_arg swr_convert(struct SwrContext *s,
  708. uint8_t **out_arg, int out_count,
  709. const uint8_t **in_arg, int in_count)
  710. {
  711. AudioData * in= &s->in;
  712. AudioData *out= &s->out;
  713. int av_unused max_output;
  714. if (!swr_is_initialized(s)) {
  715. av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");
  716. return AVERROR(EINVAL);
  717. }
  718. #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
  719. max_output = swr_get_out_samples(s, in_count);
  720. #endif
  721. while(s->drop_output > 0){
  722. int ret;
  723. uint8_t *tmp_arg[SWR_CH_MAX];
  724. #define MAX_DROP_STEP 16384
  725. if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)
  726. return ret;
  727. reversefill_audiodata(&s->drop_temp, tmp_arg);
  728. s->drop_output *= -1; //FIXME find a less hackish solution
  729. ret = swr_convert(s, tmp_arg, FFMIN(-s->drop_output, MAX_DROP_STEP), in_arg, in_count); //FIXME optimize but this is as good as never called so maybe it doesn't matter
  730. s->drop_output *= -1;
  731. in_count = 0;
  732. if(ret>0) {
  733. s->drop_output -= ret;
  734. if (!s->drop_output && !out_arg)
  735. return 0;
  736. continue;
  737. }
  738. av_assert0(s->drop_output);
  739. return 0;
  740. }
  741. if(!in_arg){
  742. if(s->resample){
  743. if (!s->flushed)
  744. s->resampler->flush(s);
  745. s->resample_in_constraint = 0;
  746. s->flushed = 1;
  747. }else if(!s->in_buffer_count){
  748. return 0;
  749. }
  750. }else
  751. fill_audiodata(in , (void*)in_arg);
  752. fill_audiodata(out, out_arg);
  753. if(s->resample){
  754. int ret = swr_convert_internal(s, out, out_count, in, in_count);
  755. if(ret>0 && !s->drop_output)
  756. s->outpts += ret * (int64_t)s->in_sample_rate;
  757. av_assert2(max_output < 0 || ret <= max_output);
  758. return ret;
  759. }else{
  760. AudioData tmp= *in;
  761. int ret2=0;
  762. int ret, size;
  763. size = FFMIN(out_count, s->in_buffer_count);
  764. if(size){
  765. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  766. ret= swr_convert_internal(s, out, size, &tmp, size);
  767. if(ret<0)
  768. return ret;
  769. ret2= ret;
  770. s->in_buffer_count -= ret;
  771. s->in_buffer_index += ret;
  772. buf_set(out, out, ret);
  773. out_count -= ret;
  774. if(!s->in_buffer_count)
  775. s->in_buffer_index = 0;
  776. }
  777. if(in_count){
  778. size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
  779. if(in_count > out_count) { //FIXME move after swr_convert_internal
  780. if( size > s->in_buffer.count
  781. && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
  782. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  783. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  784. s->in_buffer_index=0;
  785. }else
  786. if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
  787. return ret;
  788. }
  789. if(out_count){
  790. size = FFMIN(in_count, out_count);
  791. ret= swr_convert_internal(s, out, size, in, size);
  792. if(ret<0)
  793. return ret;
  794. buf_set(in, in, ret);
  795. in_count -= ret;
  796. ret2 += ret;
  797. }
  798. if(in_count){
  799. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  800. copy(&tmp, in, in_count);
  801. s->in_buffer_count += in_count;
  802. }
  803. }
  804. if(ret2>0 && !s->drop_output)
  805. s->outpts += ret2 * (int64_t)s->in_sample_rate;
  806. av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
  807. return ret2;
  808. }
  809. }
  810. int swr_drop_output(struct SwrContext *s, int count){
  811. const uint8_t *tmp_arg[SWR_CH_MAX];
  812. s->drop_output += count;
  813. if(s->drop_output <= 0)
  814. return 0;
  815. av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
  816. return swr_convert(s, NULL, s->drop_output, tmp_arg, 0);
  817. }
  818. int swr_inject_silence(struct SwrContext *s, int count){
  819. int ret, i;
  820. uint8_t *tmp_arg[SWR_CH_MAX];
  821. if(count <= 0)
  822. return 0;
  823. #define MAX_SILENCE_STEP 16384
  824. while (count > MAX_SILENCE_STEP) {
  825. if ((ret = swr_inject_silence(s, MAX_SILENCE_STEP)) < 0)
  826. return ret;
  827. count -= MAX_SILENCE_STEP;
  828. }
  829. if((ret=swri_realloc_audio(&s->silence, count))<0)
  830. return ret;
  831. if(s->silence.planar) for(i=0; i<s->silence.ch_count; i++) {
  832. memset(s->silence.ch[i], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps);
  833. } else
  834. memset(s->silence.ch[0], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps*s->silence.ch_count);
  835. reversefill_audiodata(&s->silence, tmp_arg);
  836. av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
  837. ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
  838. return ret;
  839. }
  840. int64_t swr_get_delay(struct SwrContext *s, int64_t base){
  841. if (s->resampler && s->resample){
  842. return s->resampler->get_delay(s, base);
  843. }else{
  844. return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
  845. }
  846. }
  847. int swr_get_out_samples(struct SwrContext *s, int in_samples)
  848. {
  849. int64_t out_samples;
  850. if (in_samples < 0)
  851. return AVERROR(EINVAL);
  852. if (s->resampler && s->resample) {
  853. if (!s->resampler->get_out_samples)
  854. return AVERROR(ENOSYS);
  855. out_samples = s->resampler->get_out_samples(s, in_samples);
  856. } else {
  857. out_samples = s->in_buffer_count + in_samples;
  858. av_assert0(s->out_sample_rate == s->in_sample_rate);
  859. }
  860. if (out_samples > INT_MAX)
  861. return AVERROR(EINVAL);
  862. return out_samples;
  863. }
  864. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
  865. int ret;
  866. if (!s || compensation_distance < 0)
  867. return AVERROR(EINVAL);
  868. if (!compensation_distance && sample_delta)
  869. return AVERROR(EINVAL);
  870. if (!s->resample) {
  871. s->flags |= SWR_FLAG_RESAMPLE;
  872. ret = swr_init(s);
  873. if (ret < 0)
  874. return ret;
  875. }
  876. if (!s->resampler->set_compensation){
  877. return AVERROR(EINVAL);
  878. }else{
  879. return s->resampler->set_compensation(s->resample, sample_delta, compensation_distance);
  880. }
  881. }
  882. int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
  883. if(pts == INT64_MIN)
  884. return s->outpts;
  885. if (s->firstpts == AV_NOPTS_VALUE)
  886. s->outpts = s->firstpts = pts;
  887. if(s->min_compensation >= FLT_MAX) {
  888. return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
  889. } else {
  890. int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts + s->drop_output*(int64_t)s->in_sample_rate;
  891. double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
  892. if(fabs(fdelta) > s->min_compensation) {
  893. if(s->outpts == s->firstpts || fabs(fdelta) > s->min_hard_compensation){
  894. int ret;
  895. if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
  896. else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
  897. if(ret<0){
  898. av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
  899. }
  900. } else if(s->soft_compensation_duration && s->max_soft_compensation) {
  901. int duration = s->out_sample_rate * s->soft_compensation_duration;
  902. double max_soft_compensation = s->max_soft_compensation / (s->max_soft_compensation < 0 ? -s->in_sample_rate : 1);
  903. int comp = av_clipf(fdelta, -max_soft_compensation, max_soft_compensation) * duration ;
  904. av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
  905. swr_set_compensation(s, comp, duration);
  906. }
  907. }
  908. return s->outpts;
  909. }
  910. }