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.

854 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) {
  366. return 0;
  367. } else if (border < 0) {
  368. return border;
  369. } else if (border) {
  370. buf_set(&in, &in, border);
  371. in_count -= border;
  372. s->resample_in_constraint = 0;
  373. }
  374. do{
  375. int ret, size, consumed;
  376. if(!s->resample_in_constraint && s->in_buffer_count){
  377. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  378. ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  379. out_count -= ret;
  380. ret_sum += ret;
  381. buf_set(&out, &out, ret);
  382. s->in_buffer_count -= consumed;
  383. s->in_buffer_index += consumed;
  384. if(!in_count)
  385. break;
  386. if(s->in_buffer_count <= border){
  387. buf_set(&in, &in, -s->in_buffer_count);
  388. in_count += s->in_buffer_count;
  389. s->in_buffer_count=0;
  390. s->in_buffer_index=0;
  391. border = 0;
  392. }
  393. }
  394. if((s->flushed || in_count > padless) && !s->in_buffer_count){
  395. s->in_buffer_index=0;
  396. ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, FFMAX(in_count-padless, 0), &consumed);
  397. out_count -= ret;
  398. ret_sum += ret;
  399. buf_set(&out, &out, ret);
  400. in_count -= consumed;
  401. buf_set(&in, &in, consumed);
  402. }
  403. //TODO is this check sane considering the advanced copy avoidance below
  404. size= s->in_buffer_index + s->in_buffer_count + in_count;
  405. if( size > s->in_buffer.count
  406. && s->in_buffer_count + in_count <= s->in_buffer_index){
  407. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  408. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  409. s->in_buffer_index=0;
  410. }else
  411. if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
  412. return ret;
  413. if(in_count){
  414. int count= in_count;
  415. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  416. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  417. copy(&tmp, &in, /*in_*/count);
  418. s->in_buffer_count += count;
  419. in_count -= count;
  420. border += count;
  421. buf_set(&in, &in, count);
  422. s->resample_in_constraint= 0;
  423. if(s->in_buffer_count != count || in_count)
  424. continue;
  425. if (padless) {
  426. padless = 0;
  427. continue;
  428. }
  429. }
  430. break;
  431. }while(1);
  432. s->resample_in_constraint= !!out_count;
  433. return ret_sum;
  434. }
  435. static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
  436. AudioData *in , int in_count){
  437. AudioData *postin, *midbuf, *preout;
  438. int ret/*, in_max*/;
  439. AudioData preout_tmp, midbuf_tmp;
  440. if(s->full_convert){
  441. av_assert0(!s->resample);
  442. swri_audio_convert(s->full_convert, out, in, in_count);
  443. return out_count;
  444. }
  445. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  446. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  447. if((ret=swri_realloc_audio(&s->postin, in_count))<0)
  448. return ret;
  449. if(s->resample_first){
  450. av_assert0(s->midbuf.ch_count == s->used_ch_count);
  451. if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)
  452. return ret;
  453. }else{
  454. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  455. if((ret=swri_realloc_audio(&s->midbuf, in_count))<0)
  456. return ret;
  457. }
  458. if((ret=swri_realloc_audio(&s->preout, out_count))<0)
  459. return ret;
  460. postin= &s->postin;
  461. midbuf_tmp= s->midbuf;
  462. midbuf= &midbuf_tmp;
  463. preout_tmp= s->preout;
  464. preout= &preout_tmp;
  465. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)
  466. postin= in;
  467. if(s->resample_first ? !s->resample : !s->rematrix)
  468. midbuf= postin;
  469. if(s->resample_first ? !s->rematrix : !s->resample)
  470. preout= midbuf;
  471. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar
  472. && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){
  473. if(preout==in){
  474. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  475. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  476. copy(out, in, out_count);
  477. return out_count;
  478. }
  479. else if(preout==postin) preout= midbuf= postin= out;
  480. else if(preout==midbuf) preout= midbuf= out;
  481. else preout= out;
  482. }
  483. if(in != postin){
  484. swri_audio_convert(s->in_convert, postin, in, in_count);
  485. }
  486. if(s->resample_first){
  487. if(postin != midbuf)
  488. out_count= resample(s, midbuf, out_count, postin, in_count);
  489. if(midbuf != preout)
  490. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  491. }else{
  492. if(postin != midbuf)
  493. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  494. if(midbuf != preout)
  495. out_count= resample(s, preout, out_count, midbuf, in_count);
  496. }
  497. if(preout != out && out_count){
  498. AudioData *conv_src = preout;
  499. if(s->dither.method){
  500. int ch;
  501. int dither_count= FFMAX(out_count, 1<<16);
  502. if (preout == in) {
  503. conv_src = &s->dither.temp;
  504. if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)
  505. return ret;
  506. }
  507. if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)
  508. return ret;
  509. if(ret)
  510. for(ch=0; ch<s->dither.noise.ch_count; ch++)
  511. swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, 12345678913579<<ch, s->dither.noise.fmt);
  512. av_assert0(s->dither.noise.ch_count == preout->ch_count);
  513. if(s->dither.noise_pos + out_count > s->dither.noise.count)
  514. s->dither.noise_pos = 0;
  515. if (s->dither.method < SWR_DITHER_NS){
  516. if (s->mix_2_1_simd) {
  517. int len1= out_count&~15;
  518. int off = len1 * preout->bps;
  519. if(len1)
  520. for(ch=0; ch<preout->ch_count; ch++)
  521. 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);
  522. if(out_count != len1)
  523. for(ch=0; ch<preout->ch_count; ch++)
  524. 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);
  525. } else {
  526. for(ch=0; ch<preout->ch_count; ch++)
  527. 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);
  528. }
  529. } else {
  530. switch(s->int_sample_fmt) {
  531. case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;
  532. case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;
  533. case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;
  534. case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;
  535. }
  536. }
  537. s->dither.noise_pos += out_count;
  538. }
  539. //FIXME packed doesn't need more than 1 chan here!
  540. swri_audio_convert(s->out_convert, out, conv_src, out_count);
  541. }
  542. return out_count;
  543. }
  544. int swr_is_initialized(struct SwrContext *s) {
  545. return !!s->in_buffer.ch_count;
  546. }
  547. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  548. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  549. AudioData * in= &s->in;
  550. AudioData *out= &s->out;
  551. if (!swr_is_initialized(s)) {
  552. av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");
  553. return AVERROR(EINVAL);
  554. }
  555. while(s->drop_output > 0){
  556. int ret;
  557. uint8_t *tmp_arg[SWR_CH_MAX];
  558. #define MAX_DROP_STEP 16384
  559. if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)
  560. return ret;
  561. reversefill_audiodata(&s->drop_temp, tmp_arg);
  562. s->drop_output *= -1; //FIXME find a less hackish solution
  563. 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
  564. s->drop_output *= -1;
  565. in_count = 0;
  566. if(ret>0) {
  567. s->drop_output -= ret;
  568. if (!s->drop_output && !out_arg)
  569. return 0;
  570. continue;
  571. }
  572. av_assert0(s->drop_output);
  573. return 0;
  574. }
  575. if(!in_arg){
  576. if(s->resample){
  577. if (!s->flushed)
  578. s->resampler->flush(s);
  579. s->resample_in_constraint = 0;
  580. s->flushed = 1;
  581. }else if(!s->in_buffer_count){
  582. return 0;
  583. }
  584. }else
  585. fill_audiodata(in , (void*)in_arg);
  586. fill_audiodata(out, out_arg);
  587. if(s->resample){
  588. int ret = swr_convert_internal(s, out, out_count, in, in_count);
  589. if(ret>0 && !s->drop_output)
  590. s->outpts += ret * (int64_t)s->in_sample_rate;
  591. return ret;
  592. }else{
  593. AudioData tmp= *in;
  594. int ret2=0;
  595. int ret, size;
  596. size = FFMIN(out_count, s->in_buffer_count);
  597. if(size){
  598. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  599. ret= swr_convert_internal(s, out, size, &tmp, size);
  600. if(ret<0)
  601. return ret;
  602. ret2= ret;
  603. s->in_buffer_count -= ret;
  604. s->in_buffer_index += ret;
  605. buf_set(out, out, ret);
  606. out_count -= ret;
  607. if(!s->in_buffer_count)
  608. s->in_buffer_index = 0;
  609. }
  610. if(in_count){
  611. size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
  612. if(in_count > out_count) { //FIXME move after swr_convert_internal
  613. if( size > s->in_buffer.count
  614. && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
  615. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  616. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  617. s->in_buffer_index=0;
  618. }else
  619. if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
  620. return ret;
  621. }
  622. if(out_count){
  623. size = FFMIN(in_count, out_count);
  624. ret= swr_convert_internal(s, out, size, in, size);
  625. if(ret<0)
  626. return ret;
  627. buf_set(in, in, ret);
  628. in_count -= ret;
  629. ret2 += ret;
  630. }
  631. if(in_count){
  632. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  633. copy(&tmp, in, in_count);
  634. s->in_buffer_count += in_count;
  635. }
  636. }
  637. if(ret2>0 && !s->drop_output)
  638. s->outpts += ret2 * (int64_t)s->in_sample_rate;
  639. return ret2;
  640. }
  641. }
  642. int swr_drop_output(struct SwrContext *s, int count){
  643. const uint8_t *tmp_arg[SWR_CH_MAX];
  644. s->drop_output += count;
  645. if(s->drop_output <= 0)
  646. return 0;
  647. av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
  648. return swr_convert(s, NULL, s->drop_output, tmp_arg, 0);
  649. }
  650. int swr_inject_silence(struct SwrContext *s, int count){
  651. int ret, i;
  652. uint8_t *tmp_arg[SWR_CH_MAX];
  653. if(count <= 0)
  654. return 0;
  655. #define MAX_SILENCE_STEP 16384
  656. while (count > MAX_SILENCE_STEP) {
  657. if ((ret = swr_inject_silence(s, MAX_SILENCE_STEP)) < 0)
  658. return ret;
  659. count -= MAX_SILENCE_STEP;
  660. }
  661. if((ret=swri_realloc_audio(&s->silence, count))<0)
  662. return ret;
  663. if(s->silence.planar) for(i=0; i<s->silence.ch_count; i++) {
  664. memset(s->silence.ch[i], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps);
  665. } else
  666. memset(s->silence.ch[0], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps*s->silence.ch_count);
  667. reversefill_audiodata(&s->silence, tmp_arg);
  668. av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
  669. ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
  670. return ret;
  671. }
  672. int64_t swr_get_delay(struct SwrContext *s, int64_t base){
  673. if (s->resampler && s->resample){
  674. return s->resampler->get_delay(s, base);
  675. }else{
  676. return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
  677. }
  678. }
  679. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
  680. int ret;
  681. if (!s || compensation_distance < 0)
  682. return AVERROR(EINVAL);
  683. if (!compensation_distance && sample_delta)
  684. return AVERROR(EINVAL);
  685. if (!s->resample) {
  686. s->flags |= SWR_FLAG_RESAMPLE;
  687. ret = swr_init(s);
  688. if (ret < 0)
  689. return ret;
  690. }
  691. if (!s->resampler->set_compensation){
  692. return AVERROR(EINVAL);
  693. }else{
  694. return s->resampler->set_compensation(s->resample, sample_delta, compensation_distance);
  695. }
  696. }
  697. int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
  698. if(pts == INT64_MIN)
  699. return s->outpts;
  700. if (s->firstpts == AV_NOPTS_VALUE)
  701. s->outpts = s->firstpts = pts;
  702. if(s->min_compensation >= FLT_MAX) {
  703. return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
  704. } else {
  705. 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;
  706. double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
  707. if(fabs(fdelta) > s->min_compensation) {
  708. if(s->outpts == s->firstpts || fabs(fdelta) > s->min_hard_compensation){
  709. int ret;
  710. if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
  711. else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
  712. if(ret<0){
  713. av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
  714. }
  715. } else if(s->soft_compensation_duration && s->max_soft_compensation) {
  716. int duration = s->out_sample_rate * s->soft_compensation_duration;
  717. double max_soft_compensation = s->max_soft_compensation / (s->max_soft_compensation < 0 ? -s->in_sample_rate : 1);
  718. int comp = av_clipf(fdelta, -max_soft_compensation, max_soft_compensation) * duration ;
  719. av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
  720. swr_set_compensation(s, comp, duration);
  721. }
  722. }
  723. return s->outpts;
  724. }
  725. }