swresample.c 37 KB

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