swresample.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
  298. return AVERROR(EINVAL);
  299. if(a->count >= count)
  300. return 0;
  301. count*=2;
  302. countb= FFALIGN(count*a->bps, ALIGN);
  303. old= *a;
  304. av_assert0(a->bps);
  305. av_assert0(a->ch_count);
  306. a->data= av_malloc(countb*a->ch_count);
  307. if(!a->data)
  308. return AVERROR(ENOMEM);
  309. for(i=0; i<a->ch_count; i++){
  310. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  311. if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  312. }
  313. if(!a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
  314. av_free(old.data);
  315. a->count= count;
  316. return 1;
  317. }
  318. static void copy(AudioData *out, AudioData *in,
  319. int count){
  320. av_assert0(out->planar == in->planar);
  321. av_assert0(out->bps == in->bps);
  322. av_assert0(out->ch_count == in->ch_count);
  323. if(out->planar){
  324. int ch;
  325. for(ch=0; ch<out->ch_count; ch++)
  326. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  327. }else
  328. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  329. }
  330. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  331. int i;
  332. if(!in_arg){
  333. memset(out->ch, 0, sizeof(out->ch));
  334. }else if(out->planar){
  335. for(i=0; i<out->ch_count; i++)
  336. out->ch[i]= in_arg[i];
  337. }else{
  338. for(i=0; i<out->ch_count; i++)
  339. out->ch[i]= in_arg[0] + i*out->bps;
  340. }
  341. }
  342. static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  343. int i;
  344. if(out->planar){
  345. for(i=0; i<out->ch_count; i++)
  346. in_arg[i]= out->ch[i];
  347. }else{
  348. in_arg[0]= out->ch[0];
  349. }
  350. }
  351. /**
  352. *
  353. * out may be equal in.
  354. */
  355. static void buf_set(AudioData *out, AudioData *in, int count){
  356. int ch;
  357. if(in->planar){
  358. for(ch=0; ch<out->ch_count; ch++)
  359. out->ch[ch]= in->ch[ch] + count*out->bps;
  360. }else{
  361. for(ch=out->ch_count-1; ch>=0; ch--)
  362. out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
  363. }
  364. }
  365. /**
  366. *
  367. * @return number of samples output per channel
  368. */
  369. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  370. const AudioData * in_param, int in_count){
  371. AudioData in, out, tmp;
  372. int ret_sum=0;
  373. int border=0;
  374. av_assert1(s->in_buffer.ch_count == in_param->ch_count);
  375. av_assert1(s->in_buffer.planar == in_param->planar);
  376. av_assert1(s->in_buffer.fmt == in_param->fmt);
  377. tmp=out=*out_param;
  378. in = *in_param;
  379. do{
  380. int ret, size, consumed;
  381. if(!s->resample_in_constraint && s->in_buffer_count){
  382. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  383. ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  384. out_count -= ret;
  385. ret_sum += ret;
  386. buf_set(&out, &out, ret);
  387. s->in_buffer_count -= consumed;
  388. s->in_buffer_index += consumed;
  389. if(!in_count)
  390. break;
  391. if(s->in_buffer_count <= border){
  392. buf_set(&in, &in, -s->in_buffer_count);
  393. in_count += s->in_buffer_count;
  394. s->in_buffer_count=0;
  395. s->in_buffer_index=0;
  396. border = 0;
  397. }
  398. }
  399. if(in_count && !s->in_buffer_count){
  400. s->in_buffer_index=0;
  401. ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
  402. out_count -= ret;
  403. ret_sum += ret;
  404. buf_set(&out, &out, ret);
  405. in_count -= consumed;
  406. buf_set(&in, &in, consumed);
  407. }
  408. //TODO is this check sane considering the advanced copy avoidance below
  409. size= s->in_buffer_index + s->in_buffer_count + in_count;
  410. if( size > s->in_buffer.count
  411. && s->in_buffer_count + in_count <= s->in_buffer_index){
  412. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  413. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  414. s->in_buffer_index=0;
  415. }else
  416. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  417. return ret;
  418. if(in_count){
  419. int count= in_count;
  420. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  421. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  422. copy(&tmp, &in, /*in_*/count);
  423. s->in_buffer_count += count;
  424. in_count -= count;
  425. border += count;
  426. buf_set(&in, &in, count);
  427. s->resample_in_constraint= 0;
  428. if(s->in_buffer_count != count || in_count)
  429. continue;
  430. }
  431. break;
  432. }while(1);
  433. s->resample_in_constraint= !!out_count;
  434. return ret_sum;
  435. }
  436. static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
  437. AudioData *in , int in_count){
  438. AudioData *postin, *midbuf, *preout;
  439. int ret/*, in_max*/;
  440. AudioData preout_tmp, midbuf_tmp;
  441. if(s->full_convert){
  442. av_assert0(!s->resample);
  443. swri_audio_convert(s->full_convert, out, in, in_count);
  444. return out_count;
  445. }
  446. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  447. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  448. if((ret=realloc_audio(&s->postin, in_count))<0)
  449. return ret;
  450. if(s->resample_first){
  451. av_assert0(s->midbuf.ch_count == s->used_ch_count);
  452. if((ret=realloc_audio(&s->midbuf, out_count))<0)
  453. return ret;
  454. }else{
  455. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  456. if((ret=realloc_audio(&s->midbuf, in_count))<0)
  457. return ret;
  458. }
  459. if((ret=realloc_audio(&s->preout, out_count))<0)
  460. return ret;
  461. postin= &s->postin;
  462. midbuf_tmp= s->midbuf;
  463. midbuf= &midbuf_tmp;
  464. preout_tmp= s->preout;
  465. preout= &preout_tmp;
  466. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
  467. postin= in;
  468. if(s->resample_first ? !s->resample : !s->rematrix)
  469. midbuf= postin;
  470. if(s->resample_first ? !s->rematrix : !s->resample)
  471. preout= midbuf;
  472. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
  473. if(preout==in){
  474. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  475. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  476. copy(out, in, out_count);
  477. return out_count;
  478. }
  479. else if(preout==postin) preout= midbuf= postin= out;
  480. else if(preout==midbuf) preout= midbuf= out;
  481. else preout= out;
  482. }
  483. if(in != postin){
  484. swri_audio_convert(s->in_convert, postin, in, in_count);
  485. }
  486. if(s->resample_first){
  487. if(postin != midbuf)
  488. out_count= resample(s, midbuf, out_count, postin, in_count);
  489. if(midbuf != preout)
  490. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  491. }else{
  492. if(postin != midbuf)
  493. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  494. if(midbuf != preout)
  495. out_count= resample(s, preout, out_count, midbuf, in_count);
  496. }
  497. if(preout != out && out_count){
  498. if(s->dither_method){
  499. int ch;
  500. int dither_count= FFMAX(out_count, 1<<16);
  501. av_assert0(preout != in);
  502. if((ret=realloc_audio(&s->dither, dither_count))<0)
  503. return ret;
  504. if(ret)
  505. for(ch=0; ch<s->dither.ch_count; ch++)
  506. swri_get_dither(s, s->dither.ch[ch], s->dither.count, 12345678913579<<ch, s->out_sample_fmt, s->int_sample_fmt);
  507. av_assert0(s->dither.ch_count == preout->ch_count);
  508. if(s->dither_pos + out_count > s->dither.count)
  509. s->dither_pos = 0;
  510. for(ch=0; ch<preout->ch_count; ch++)
  511. 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);
  512. s->dither_pos += out_count;
  513. }
  514. //FIXME packed doesnt need more than 1 chan here!
  515. swri_audio_convert(s->out_convert, out, preout, out_count);
  516. }
  517. return out_count;
  518. }
  519. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  520. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  521. AudioData * in= &s->in;
  522. AudioData *out= &s->out;
  523. if(s->drop_output > 0){
  524. int ret;
  525. AudioData tmp = s->out;
  526. uint8_t *tmp_arg[SWR_CH_MAX];
  527. tmp.count = 0;
  528. tmp.data = NULL;
  529. if((ret=realloc_audio(&tmp, s->drop_output))<0)
  530. return ret;
  531. reversefill_audiodata(&tmp, tmp_arg);
  532. s->drop_output *= -1; //FIXME find a less hackish solution
  533. 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
  534. s->drop_output *= -1;
  535. if(ret>0)
  536. s->drop_output -= ret;
  537. av_freep(&tmp.data);
  538. if(s->drop_output || !out_arg)
  539. return 0;
  540. in_count = 0;
  541. }
  542. if(!in_arg){
  543. if(s->in_buffer_count){
  544. if (s->resample && !s->flushed) {
  545. AudioData *a= &s->in_buffer;
  546. int i, j, ret;
  547. if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
  548. return ret;
  549. av_assert0(a->planar);
  550. for(i=0; i<a->ch_count; i++){
  551. for(j=0; j<s->in_buffer_count; j++){
  552. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  553. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  554. }
  555. }
  556. s->in_buffer_count += (s->in_buffer_count+1)/2;
  557. s->resample_in_constraint = 0;
  558. s->flushed = 1;
  559. }
  560. }else{
  561. return 0;
  562. }
  563. }else
  564. fill_audiodata(in , (void*)in_arg);
  565. fill_audiodata(out, out_arg);
  566. if(s->resample){
  567. int ret = swr_convert_internal(s, out, out_count, in, in_count);
  568. if(ret>0 && !s->drop_output)
  569. s->outpts += ret * (int64_t)s->in_sample_rate;
  570. return ret;
  571. }else{
  572. AudioData tmp= *in;
  573. int ret2=0;
  574. int ret, size;
  575. size = FFMIN(out_count, s->in_buffer_count);
  576. if(size){
  577. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  578. ret= swr_convert_internal(s, out, size, &tmp, size);
  579. if(ret<0)
  580. return ret;
  581. ret2= ret;
  582. s->in_buffer_count -= ret;
  583. s->in_buffer_index += ret;
  584. buf_set(out, out, ret);
  585. out_count -= ret;
  586. if(!s->in_buffer_count)
  587. s->in_buffer_index = 0;
  588. }
  589. if(in_count){
  590. size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
  591. if(in_count > out_count) { //FIXME move after swr_convert_internal
  592. if( size > s->in_buffer.count
  593. && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
  594. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  595. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  596. s->in_buffer_index=0;
  597. }else
  598. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  599. return ret;
  600. }
  601. if(out_count){
  602. size = FFMIN(in_count, out_count);
  603. ret= swr_convert_internal(s, out, size, in, size);
  604. if(ret<0)
  605. return ret;
  606. buf_set(in, in, ret);
  607. in_count -= ret;
  608. ret2 += ret;
  609. }
  610. if(in_count){
  611. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  612. copy(&tmp, in, in_count);
  613. s->in_buffer_count += in_count;
  614. }
  615. }
  616. if(ret2>0 && !s->drop_output)
  617. s->outpts += ret2 * (int64_t)s->in_sample_rate;
  618. return ret2;
  619. }
  620. }
  621. int swr_drop_output(struct SwrContext *s, int count){
  622. s->drop_output += count;
  623. if(s->drop_output <= 0)
  624. return 0;
  625. av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
  626. return swr_convert(s, NULL, s->drop_output, NULL, 0);
  627. }
  628. int swr_inject_silence(struct SwrContext *s, int count){
  629. int ret, i;
  630. AudioData silence = s->out;
  631. uint8_t *tmp_arg[SWR_CH_MAX];
  632. if(count <= 0)
  633. return 0;
  634. silence.count = 0;
  635. silence.data = NULL;
  636. if((ret=realloc_audio(&silence, count))<0)
  637. return ret;
  638. if(silence.planar) for(i=0; i<silence.ch_count; i++) {
  639. memset(silence.ch[i], silence.bps==1 ? 0x80 : 0, count*silence.bps);
  640. } else
  641. memset(silence.ch[0], silence.bps==1 ? 0x80 : 0, count*silence.bps*silence.ch_count);
  642. reversefill_audiodata(&silence, tmp_arg);
  643. av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
  644. ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
  645. av_freep(&silence.data);
  646. return ret;
  647. }
  648. int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
  649. if(pts == INT64_MIN)
  650. return s->outpts;
  651. if(s->min_compensation >= FLT_MAX) {
  652. return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
  653. } else {
  654. int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts;
  655. double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
  656. if(fabs(fdelta) > s->min_compensation) {
  657. if(!s->outpts || fabs(fdelta) > s->min_hard_compensation){
  658. int ret;
  659. if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
  660. else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
  661. if(ret<0){
  662. av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
  663. }
  664. } else if(s->soft_compensation_duration && s->max_soft_compensation) {
  665. int duration = s->out_sample_rate * s->soft_compensation_duration;
  666. int comp = av_clipf(fdelta, -s->max_soft_compensation, s->max_soft_compensation) * duration ;
  667. av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
  668. swr_set_compensation(s, comp, duration);
  669. }
  670. }
  671. return s->outpts;
  672. }
  673. }