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.

785 lines
32KB

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