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.

775 lines
32KB

  1. /*
  2. * Copyright (C) 2011 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/audioconvert.h"
  25. #include <float.h>
  26. #define C30DB M_SQRT2
  27. #define C15DB 1.189207115
  28. #define C__0DB 1.0
  29. #define C_15DB 0.840896415
  30. #define C_30DB M_SQRT1_2
  31. #define C_45DB 0.594603558
  32. #define C_60DB 0.5
  33. #define ALIGN 32
  34. //TODO split options array out?
  35. #define OFFSET(x) offsetof(SwrContext,x)
  36. #define PARAM AV_OPT_FLAG_AUDIO_PARAM
  37. static const AVOption options[]={
  38. {"ich" , "Input Channel Count" , OFFSET( in.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
  39. {"in_channel_count" , "Input Channel Count" , OFFSET( in.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
  40. {"och" , "Output Channel Count" , OFFSET(out.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
  41. {"out_channel_count" , "Output Channel Count" , OFFSET(out.ch_count ), AV_OPT_TYPE_INT , {.dbl=2 }, 0 , SWR_CH_MAX, PARAM},
  42. {"uch" , "Used Channel Count" , OFFSET(used_ch_count ), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , SWR_CH_MAX, PARAM},
  43. {"used_channel_count" , "Used Channel Count" , OFFSET(used_ch_count ), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , SWR_CH_MAX, PARAM},
  44. {"isr" , "Input Sample Rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
  45. {"in_sample_rate" , "Input Sample Rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
  46. {"osr" , "Output Sample Rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
  47. {"out_sample_rate" , "Output Sample Rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , INT_MAX , PARAM},
  48. {"isf" , "Input Sample Format" , OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
  49. {"in_sample_fmt" , "Input Sample Format" , OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
  50. {"osf" , "Output Sample Format" , OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
  51. {"out_sample_fmt" , "Output Sample Format" , OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_NB-1+256, PARAM},
  52. {"tsf" , "Internal Sample Format" , OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_FLTP, PARAM},
  53. {"internal_sample_fmt" , "Internal Sample Format" , OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT , {.dbl=AV_SAMPLE_FMT_NONE }, -1 , AV_SAMPLE_FMT_FLTP, PARAM},
  54. {"icl" , "Input Channel Layout" , OFFSET( in_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
  55. {"in_channel_layout" , "Input Channel Layout" , OFFSET( in_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
  56. {"ocl" , "Output Channel Layout" , OFFSET(out_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
  57. {"out_channel_layout" , "Output Channel Layout" , OFFSET(out_ch_layout ), AV_OPT_TYPE_INT64, {.dbl=0 }, 0 , INT64_MAX , PARAM, "channel_layout"},
  58. {"clev" , "Center Mix Level" , OFFSET(clev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
  59. {"center_mix_level" , "Center Mix Level" , OFFSET(clev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
  60. {"slev" , "Sourround Mix Level" , OFFSET(slev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
  61. {"surround_mix_level" , "Sourround Mix Level" , OFFSET(slev ), AV_OPT_TYPE_FLOAT, {.dbl=C_30DB }, -32 , 32 , PARAM},
  62. {"lfe_mix_level" , "LFE Mix Level" , OFFSET(lfe_mix_level ), AV_OPT_TYPE_FLOAT, {.dbl=0 }, -32 , 32 , PARAM},
  63. {"rmvol" , "Rematrix Volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0 }, -1000 , 1000 , PARAM},
  64. {"rematrix_volume" , "Rematrix Volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0 }, -1000 , 1000 , PARAM},
  65. {"flags" , NULL , OFFSET(flags ), AV_OPT_TYPE_FLAGS, {.dbl=0 }, 0 , UINT_MAX , PARAM, "flags"},
  66. {"swr_flags" , NULL , OFFSET(flags ), AV_OPT_TYPE_FLAGS, {.dbl=0 }, 0 , UINT_MAX , PARAM, "flags"},
  67. {"res" , "Force Resampling" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE }, INT_MIN, INT_MAX , PARAM, "flags"},
  68. {"dither_scale" , "Dither Scale" , OFFSET(dither_scale ), AV_OPT_TYPE_FLOAT, {.dbl=1 }, 0 , INT_MAX , PARAM},
  69. {"dither_method" , "Dither Method" , OFFSET(dither_method ), AV_OPT_TYPE_INT , {.dbl=0 }, 0 , SWR_DITHER_NB-1, PARAM, "dither_method"},
  70. {"rectangular" , "Rectangular Dither" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_RECTANGULAR}, INT_MIN, INT_MAX , PARAM, "dither_method"},
  71. {"triangular" , "Triangular Dither" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR }, INT_MIN, INT_MAX , PARAM, "dither_method"},
  72. {"triangular_hp" , "Triangular Dither With High Pass" , 0 , AV_OPT_TYPE_CONST, {.dbl=SWR_DITHER_TRIANGULAR_HIGHPASS }, INT_MIN, INT_MAX, PARAM, "dither_method"},
  73. {"filter_size" , "Resampling Filter Size" , OFFSET(filter_size) , AV_OPT_TYPE_INT , {.dbl=16 }, 0 , INT_MAX , PARAM },
  74. {"phase_shift" , "Resampling Phase Shift" , OFFSET(phase_shift) , AV_OPT_TYPE_INT , {.dbl=10 }, 0 , 30 , PARAM },
  75. {"linear_interp" , "Use Linear Interpolation" , OFFSET(linear_interp) , AV_OPT_TYPE_INT , {.dbl=0 }, 0 , 1 , PARAM },
  76. {"cutoff" , "Cutoff Frequency Ratio" , OFFSET(cutoff) , AV_OPT_TYPE_DOUBLE,{.dbl=0.8 }, 0 , 1 , PARAM },
  77. {"min_comp" , "Minimum difference between timestamps and audio data (in seconds) below which no timestamp compensation of either kind is applied"
  78. , OFFSET(min_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=FLT_MAX }, 0 , FLT_MAX , PARAM },
  79. {"min_hard_comp" , "Minimum difference between timestamps and audio data (in seconds) to trigger padding/trimming the data."
  80. , OFFSET(min_hard_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=0.1 }, 0 , INT_MAX , PARAM },
  81. {"comp_duration" , "Duration (in seconds) over which data is stretched/squeezeed to make it match the timestamps."
  82. , OFFSET(soft_compensation_duration),AV_OPT_TYPE_FLOAT ,{.dbl=1 }, 0 , INT_MAX , PARAM },
  83. {"max_soft_comp" , "Maximum factor by which data is stretched/squeezeed to make it match the timestamps."
  84. , OFFSET(max_soft_compensation),AV_OPT_TYPE_FLOAT ,{.dbl=0 }, 0 , INT_MAX , PARAM },
  85. {0}
  86. };
  87. static const char* context_to_name(void* ptr) {
  88. return "SWR";
  89. }
  90. static const AVClass av_class = {
  91. .class_name = "SwrContext",
  92. .item_name = context_to_name,
  93. .option = options,
  94. .version = LIBAVUTIL_VERSION_INT,
  95. .log_level_offset_offset = OFFSET(log_level_offset),
  96. .parent_log_context_offset = OFFSET(log_ctx),
  97. };
  98. unsigned swresample_version(void)
  99. {
  100. av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
  101. return LIBSWRESAMPLE_VERSION_INT;
  102. }
  103. const char *swresample_configuration(void)
  104. {
  105. return FFMPEG_CONFIGURATION;
  106. }
  107. const char *swresample_license(void)
  108. {
  109. #define LICENSE_PREFIX "libswresample license: "
  110. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  111. }
  112. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
  113. if(!s || s->in_convert) // s needs to be allocated but not initialized
  114. return AVERROR(EINVAL);
  115. s->channel_map = channel_map;
  116. return 0;
  117. }
  118. const AVClass *swr_get_class(void)
  119. {
  120. return &av_class;
  121. }
  122. struct SwrContext *swr_alloc(void){
  123. SwrContext *s= av_mallocz(sizeof(SwrContext));
  124. if(s){
  125. s->av_class= &av_class;
  126. av_opt_set_defaults(s);
  127. }
  128. return s;
  129. }
  130. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  131. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  132. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  133. int log_offset, void *log_ctx){
  134. if(!s) s= swr_alloc();
  135. if(!s) return NULL;
  136. s->log_level_offset= log_offset;
  137. s->log_ctx= log_ctx;
  138. av_opt_set_int(s, "ocl", out_ch_layout, 0);
  139. av_opt_set_int(s, "osf", out_sample_fmt, 0);
  140. av_opt_set_int(s, "osr", out_sample_rate, 0);
  141. av_opt_set_int(s, "icl", in_ch_layout, 0);
  142. av_opt_set_int(s, "isf", in_sample_fmt, 0);
  143. av_opt_set_int(s, "isr", in_sample_rate, 0);
  144. av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_NONE, 0);
  145. av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
  146. av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
  147. av_opt_set_int(s, "uch", 0, 0);
  148. return s;
  149. }
  150. static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
  151. a->fmt = fmt;
  152. a->bps = av_get_bytes_per_sample(fmt);
  153. a->planar= av_sample_fmt_is_planar(fmt);
  154. }
  155. static void free_temp(AudioData *a){
  156. av_free(a->data);
  157. memset(a, 0, sizeof(*a));
  158. }
  159. void swr_free(SwrContext **ss){
  160. SwrContext *s= *ss;
  161. if(s){
  162. free_temp(&s->postin);
  163. free_temp(&s->midbuf);
  164. free_temp(&s->preout);
  165. free_temp(&s->in_buffer);
  166. free_temp(&s->dither);
  167. swri_audio_convert_free(&s-> in_convert);
  168. swri_audio_convert_free(&s->out_convert);
  169. swri_audio_convert_free(&s->full_convert);
  170. swri_resample_free(&s->resample);
  171. swri_rematrix_free(s);
  172. }
  173. av_freep(ss);
  174. }
  175. int swr_init(struct SwrContext *s){
  176. s->in_buffer_index= 0;
  177. s->in_buffer_count= 0;
  178. s->resample_in_constraint= 0;
  179. free_temp(&s->postin);
  180. free_temp(&s->midbuf);
  181. free_temp(&s->preout);
  182. free_temp(&s->in_buffer);
  183. free_temp(&s->dither);
  184. swri_audio_convert_free(&s-> in_convert);
  185. swri_audio_convert_free(&s->out_convert);
  186. swri_audio_convert_free(&s->full_convert);
  187. swri_rematrix_free(s);
  188. s->flushed = 0;
  189. if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
  190. av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
  191. return AVERROR(EINVAL);
  192. }
  193. if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
  194. av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
  195. return AVERROR(EINVAL);
  196. }
  197. if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
  198. if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_S16P){
  199. s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
  200. }else if(av_get_planar_sample_fmt(s->in_sample_fmt) <= AV_SAMPLE_FMT_FLTP){
  201. s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
  202. }else{
  203. av_log(s, AV_LOG_DEBUG, "Using double precission mode\n");
  204. s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
  205. }
  206. }
  207. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
  208. &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
  209. &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
  210. &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
  211. 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));
  212. return AVERROR(EINVAL);
  213. }
  214. set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
  215. set_audiodata_fmt(&s->out, s->out_sample_fmt);
  216. if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
  217. s->resample = swri_resample_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);
  218. }else
  219. swri_resample_free(&s->resample);
  220. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
  221. && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
  222. && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
  223. && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
  224. && s->resample){
  225. av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16/s32/flt/dbl\n");
  226. return -1;
  227. }
  228. if(!s->used_ch_count)
  229. s->used_ch_count= s->in.ch_count;
  230. if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
  231. av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
  232. s-> in_ch_layout= 0;
  233. }
  234. if(!s-> in_ch_layout)
  235. s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
  236. if(!s->out_ch_layout)
  237. s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
  238. s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
  239. s->rematrix_custom;
  240. #define RSC 1 //FIXME finetune
  241. if(!s-> in.ch_count)
  242. s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
  243. if(!s->used_ch_count)
  244. s->used_ch_count= s->in.ch_count;
  245. if(!s->out.ch_count)
  246. s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
  247. if(!s-> in.ch_count){
  248. av_assert0(!s->in_ch_layout);
  249. av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
  250. return -1;
  251. }
  252. if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
  253. av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
  254. return -1;
  255. }
  256. av_assert0(s->used_ch_count);
  257. av_assert0(s->out.ch_count);
  258. s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
  259. s->in_buffer= s->in;
  260. if(!s->resample && !s->rematrix && !s->channel_map && !s->dither_method){
  261. s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
  262. s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
  263. return 0;
  264. }
  265. s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
  266. s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
  267. s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
  268. s->int_sample_fmt, s->out.ch_count, NULL, 0);
  269. s->postin= s->in;
  270. s->preout= s->out;
  271. s->midbuf= s->in;
  272. if(s->channel_map){
  273. s->postin.ch_count=
  274. s->midbuf.ch_count= s->used_ch_count;
  275. if(s->resample)
  276. s->in_buffer.ch_count= s->used_ch_count;
  277. }
  278. if(!s->resample_first){
  279. s->midbuf.ch_count= s->out.ch_count;
  280. if(s->resample)
  281. s->in_buffer.ch_count = s->out.ch_count;
  282. }
  283. set_audiodata_fmt(&s->postin, s->int_sample_fmt);
  284. set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
  285. set_audiodata_fmt(&s->preout, s->int_sample_fmt);
  286. if(s->resample){
  287. set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
  288. }
  289. s->dither = s->preout;
  290. if(s->rematrix || s->dither_method)
  291. return swri_rematrix_init(s);
  292. return 0;
  293. }
  294. static int realloc_audio(AudioData *a, int count){
  295. int i, countb;
  296. AudioData old;
  297. if(a->count >= count)
  298. return 0;
  299. count*=2;
  300. countb= FFALIGN(count*a->bps, ALIGN);
  301. old= *a;
  302. av_assert0(a->bps);
  303. av_assert0(a->ch_count);
  304. a->data= av_malloc(countb*a->ch_count);
  305. if(!a->data)
  306. return AVERROR(ENOMEM);
  307. for(i=0; i<a->ch_count; i++){
  308. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  309. if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  310. }
  311. if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
  312. av_free(old.data);
  313. a->count= count;
  314. return 1;
  315. }
  316. static void copy(AudioData *out, AudioData *in,
  317. int count){
  318. av_assert0(out->planar == in->planar);
  319. av_assert0(out->bps == in->bps);
  320. av_assert0(out->ch_count == in->ch_count);
  321. if(out->planar){
  322. int ch;
  323. for(ch=0; ch<out->ch_count; ch++)
  324. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  325. }else
  326. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  327. }
  328. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  329. int i;
  330. if(!in_arg){
  331. memset(out->ch, 0, sizeof(out->ch));
  332. }else if(out->planar){
  333. for(i=0; i<out->ch_count; i++)
  334. out->ch[i]= in_arg[i];
  335. }else{
  336. for(i=0; i<out->ch_count; i++)
  337. out->ch[i]= in_arg[0] + i*out->bps;
  338. }
  339. }
  340. static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  341. int i;
  342. if(out->planar){
  343. for(i=0; i<out->ch_count; i++)
  344. in_arg[i]= out->ch[i];
  345. }else{
  346. in_arg[0]= out->ch[0];
  347. }
  348. }
  349. /**
  350. *
  351. * out may be equal in.
  352. */
  353. static void buf_set(AudioData *out, AudioData *in, int count){
  354. int ch;
  355. if(in->planar){
  356. for(ch=0; ch<out->ch_count; ch++)
  357. out->ch[ch]= in->ch[ch] + count*out->bps;
  358. }else{
  359. for(ch=out->ch_count-1; ch>=0; ch--)
  360. out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
  361. }
  362. }
  363. /**
  364. *
  365. * @return number of samples output per channel
  366. */
  367. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  368. const AudioData * in_param, int in_count){
  369. AudioData in, out, tmp;
  370. int ret_sum=0;
  371. int border=0;
  372. av_assert1(s->in_buffer.ch_count == in_param->ch_count);
  373. av_assert1(s->in_buffer.planar == in_param->planar);
  374. av_assert1(s->in_buffer.fmt == in_param->fmt);
  375. tmp=out=*out_param;
  376. in = *in_param;
  377. do{
  378. int ret, size, consumed;
  379. if(!s->resample_in_constraint && s->in_buffer_count){
  380. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  381. ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  382. out_count -= ret;
  383. ret_sum += ret;
  384. buf_set(&out, &out, ret);
  385. s->in_buffer_count -= consumed;
  386. s->in_buffer_index += consumed;
  387. if(!in_count)
  388. break;
  389. if(s->in_buffer_count <= border){
  390. buf_set(&in, &in, -s->in_buffer_count);
  391. in_count += s->in_buffer_count;
  392. s->in_buffer_count=0;
  393. s->in_buffer_index=0;
  394. border = 0;
  395. }
  396. }
  397. if(in_count && !s->in_buffer_count){
  398. s->in_buffer_index=0;
  399. ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
  400. out_count -= ret;
  401. ret_sum += ret;
  402. buf_set(&out, &out, ret);
  403. in_count -= consumed;
  404. buf_set(&in, &in, consumed);
  405. }
  406. //TODO is this check sane considering the advanced copy avoidance below
  407. size= s->in_buffer_index + s->in_buffer_count + in_count;
  408. if( size > s->in_buffer.count
  409. && s->in_buffer_count + in_count <= s->in_buffer_index){
  410. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  411. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  412. s->in_buffer_index=0;
  413. }else
  414. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  415. return ret;
  416. if(in_count){
  417. int count= in_count;
  418. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  419. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  420. copy(&tmp, &in, /*in_*/count);
  421. s->in_buffer_count += count;
  422. in_count -= count;
  423. border += count;
  424. buf_set(&in, &in, count);
  425. s->resample_in_constraint= 0;
  426. if(s->in_buffer_count != count || in_count)
  427. continue;
  428. }
  429. break;
  430. }while(1);
  431. s->resample_in_constraint= !!out_count;
  432. return ret_sum;
  433. }
  434. static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
  435. AudioData *in , int in_count){
  436. AudioData *postin, *midbuf, *preout;
  437. int ret/*, in_max*/;
  438. AudioData preout_tmp, midbuf_tmp;
  439. if(s->full_convert){
  440. av_assert0(!s->resample);
  441. swri_audio_convert(s->full_convert, out, in, in_count);
  442. return out_count;
  443. }
  444. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  445. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  446. if((ret=realloc_audio(&s->postin, in_count))<0)
  447. return ret;
  448. if(s->resample_first){
  449. av_assert0(s->midbuf.ch_count == s->used_ch_count);
  450. if((ret=realloc_audio(&s->midbuf, out_count))<0)
  451. return ret;
  452. }else{
  453. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  454. if((ret=realloc_audio(&s->midbuf, in_count))<0)
  455. return ret;
  456. }
  457. if((ret=realloc_audio(&s->preout, out_count))<0)
  458. return ret;
  459. postin= &s->postin;
  460. midbuf_tmp= s->midbuf;
  461. midbuf= &midbuf_tmp;
  462. preout_tmp= s->preout;
  463. preout= &preout_tmp;
  464. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
  465. postin= in;
  466. if(s->resample_first ? !s->resample : !s->rematrix)
  467. midbuf= postin;
  468. if(s->resample_first ? !s->rematrix : !s->resample)
  469. preout= midbuf;
  470. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
  471. if(preout==in){
  472. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  473. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  474. copy(out, in, out_count);
  475. return out_count;
  476. }
  477. else if(preout==postin) preout= midbuf= postin= out;
  478. else if(preout==midbuf) preout= midbuf= out;
  479. else preout= out;
  480. }
  481. if(in != postin){
  482. swri_audio_convert(s->in_convert, postin, in, in_count);
  483. }
  484. if(s->resample_first){
  485. if(postin != midbuf)
  486. out_count= resample(s, midbuf, out_count, postin, in_count);
  487. if(midbuf != preout)
  488. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  489. }else{
  490. if(postin != midbuf)
  491. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  492. if(midbuf != preout)
  493. out_count= resample(s, preout, out_count, midbuf, in_count);
  494. }
  495. if(preout != out && out_count){
  496. if(s->dither_method){
  497. int ch;
  498. int dither_count= FFMAX(out_count, 1<<16);
  499. av_assert0(preout != in);
  500. if((ret=realloc_audio(&s->dither, dither_count))<0)
  501. return ret;
  502. if(ret)
  503. for(ch=0; ch<s->dither.ch_count; ch++)
  504. swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
  505. av_assert0(s->dither.ch_count == preout->ch_count);
  506. if(s->dither_pos + out_count > s->dither.count)
  507. s->dither_pos = 0;
  508. for(ch=0; ch<preout->ch_count; ch++)
  509. s->mix_2_1_f(preout->ch[ch], preout->ch[ch], s->dither.ch[ch] + s->dither.bps * s->dither_pos, s->native_one, 0, 0, out_count);
  510. s->dither_pos += out_count;
  511. }
  512. //FIXME packed doesnt need more than 1 chan here!
  513. swri_audio_convert(s->out_convert, out, preout, out_count);
  514. }
  515. return out_count;
  516. }
  517. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  518. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  519. AudioData * in= &s->in;
  520. AudioData *out= &s->out;
  521. if(s->drop_output > 0){
  522. int ret;
  523. AudioData tmp = s->out;
  524. uint8_t *tmp_arg[SWR_CH_MAX];
  525. tmp.count = 0;
  526. tmp.data = NULL;
  527. if((ret=realloc_audio(&tmp, s->drop_output))<0)
  528. return ret;
  529. reversefill_audiodata(&tmp, tmp_arg);
  530. s->drop_output *= -1; //FIXME find a less hackish solution
  531. ret = swr_convert(s, tmp_arg, -s->drop_output, in_arg, in_count); //FIXME optimize but this is as good as never called so maybe it doesnt matter
  532. s->drop_output *= -1;
  533. if(ret>0)
  534. s->drop_output -= ret;
  535. av_freep(&tmp.data);
  536. if(s->drop_output || !out_arg)
  537. return 0;
  538. }
  539. if(!in_arg){
  540. if(s->in_buffer_count){
  541. if (s->resample && !s->flushed) {
  542. AudioData *a= &s->in_buffer;
  543. int i, j, ret;
  544. if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
  545. return ret;
  546. av_assert0(a->planar);
  547. for(i=0; i<a->ch_count; i++){
  548. for(j=0; j<s->in_buffer_count; j++){
  549. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  550. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  551. }
  552. }
  553. s->in_buffer_count += (s->in_buffer_count+1)/2;
  554. s->resample_in_constraint = 0;
  555. s->flushed = 1;
  556. }
  557. }else{
  558. return 0;
  559. }
  560. }else
  561. fill_audiodata(in , (void*)in_arg);
  562. fill_audiodata(out, out_arg);
  563. if(s->resample){
  564. int ret = swr_convert_internal(s, out, out_count, in, in_count);
  565. if(ret>0 && !s->drop_output)
  566. s->outpts += ret * (int64_t)s->in_sample_rate;
  567. return ret;
  568. }else{
  569. AudioData tmp= *in;
  570. int ret2=0;
  571. int ret, size;
  572. size = FFMIN(out_count, s->in_buffer_count);
  573. if(size){
  574. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  575. ret= swr_convert_internal(s, out, size, &tmp, size);
  576. if(ret<0)
  577. return ret;
  578. ret2= ret;
  579. s->in_buffer_count -= ret;
  580. s->in_buffer_index += ret;
  581. buf_set(out, out, ret);
  582. out_count -= ret;
  583. if(!s->in_buffer_count)
  584. s->in_buffer_index = 0;
  585. }
  586. if(in_count){
  587. size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
  588. if(in_count > out_count) { //FIXME move after swr_convert_internal
  589. if( size > s->in_buffer.count
  590. && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
  591. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  592. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  593. s->in_buffer_index=0;
  594. }else
  595. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  596. return ret;
  597. }
  598. if(out_count){
  599. size = FFMIN(in_count, out_count);
  600. ret= swr_convert_internal(s, out, size, in, size);
  601. if(ret<0)
  602. return ret;
  603. buf_set(in, in, ret);
  604. in_count -= ret;
  605. ret2 += ret;
  606. }
  607. if(in_count){
  608. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  609. copy(&tmp, in, in_count);
  610. s->in_buffer_count += in_count;
  611. }
  612. }
  613. if(ret2>0 && !s->drop_output)
  614. s->outpts += ret2 * (int64_t)s->in_sample_rate;
  615. return ret2;
  616. }
  617. }
  618. int swr_drop_output(struct SwrContext *s, int count){
  619. s->drop_output += count;
  620. if(s->drop_output <= 0)
  621. return 0;
  622. av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
  623. return swr_convert(s, NULL, s->drop_output, NULL, 0);
  624. }
  625. int swr_inject_silence(struct SwrContext *s, int count){
  626. int ret, i;
  627. AudioData silence = s->out;
  628. uint8_t *tmp_arg[SWR_CH_MAX];
  629. if(count <= 0)
  630. return 0;
  631. silence.count = 0;
  632. silence.data = NULL;
  633. if((ret=realloc_audio(&silence, count))<0)
  634. return ret;
  635. if(silence.planar) for(i=0; i<silence.ch_count; i++) {
  636. memset(silence.ch[i], silence.bps==1 ? 0x80 : 0, count*silence.bps);
  637. } else
  638. memset(silence.ch[0], silence.bps==1 ? 0x80 : 0, count*silence.bps*silence.ch_count);
  639. reversefill_audiodata(&silence, tmp_arg);
  640. av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
  641. ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
  642. av_freep(&silence.data);
  643. return ret;
  644. }
  645. int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
  646. if(pts == INT64_MIN)
  647. return s->outpts;
  648. if(s->min_compensation >= FLT_MAX) {
  649. return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
  650. } else {
  651. int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts;
  652. double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
  653. if(fabs(fdelta) > s->min_compensation) {
  654. if(!s->outpts || fabs(fdelta) > s->min_hard_compensation){
  655. if(delta > 0) swr_inject_silence(s, delta / s->out_sample_rate);
  656. else swr_drop_output (s, -delta / s-> in_sample_rate);
  657. } else if(s->soft_compensation_duration && s->max_soft_compensation) {
  658. int duration = s->out_sample_rate * s->soft_compensation_duration;
  659. int comp = av_clipf(fdelta, -s->max_soft_compensation, s->max_soft_compensation) * duration ;
  660. av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
  661. swr_set_compensation(s, comp, duration);
  662. }
  663. }
  664. return s->outpts;
  665. }
  666. }