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.

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