swresample.c 33 KB

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