2
0

swresample.c 32 KB

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