swresample.c 36 KB

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