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.

928 lines
32KB

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