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.

857 lines
29KB

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