You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

940 lines
32KB

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