swresample.c 31 KB

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