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.

790 lines
33KB

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