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.

872 lines
30KB

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