swresample.c 33 KB

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