swresample.c 37 KB

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