swresample.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #define C30DB M_SQRT2
  26. #define C15DB 1.189207115
  27. #define C__0DB 1.0
  28. #define C_15DB 0.840896415
  29. #define C_30DB M_SQRT1_2
  30. #define C_45DB 0.594603558
  31. #define C_60DB 0.5
  32. //TODO split options array out?
  33. #define OFFSET(x) offsetof(SwrContext,x)
  34. static const AVOption options[]={
  35. {"ich", "input channel count", OFFSET( in.ch_count ), FF_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
  36. {"och", "output channel count", OFFSET(out.ch_count ), FF_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
  37. {"isr", "input sample rate" , OFFSET( in_sample_rate), FF_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
  38. {"osr", "output sample rate" , OFFSET(out_sample_rate), FF_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
  39. //{"ip" , "input planar" , OFFSET( in.planar ), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
  40. //{"op" , "output planar" , OFFSET(out.planar ), FF_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
  41. {"isf", "input sample format", OFFSET( in_sample_fmt ), FF_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
  42. {"osf", "output sample format", OFFSET(out_sample_fmt ), FF_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
  43. {"tsf", "internal sample format", OFFSET(int_sample_fmt ), FF_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0},
  44. {"icl", "input channel layout" , OFFSET( in_ch_layout), FF_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
  45. {"ocl", "output channel layout", OFFSET(out_ch_layout), FF_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
  46. {"clev", "center mix level" , OFFSET(clev) , FF_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
  47. {"slev", "sourround mix level" , OFFSET(slev) , FF_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
  48. {"flags", NULL , OFFSET(flags) , FF_OPT_TYPE_FLAGS, {.dbl=0}, 0, UINT_MAX, 0, "flags"},
  49. {"res", "force resampling", 0, FF_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"},
  50. {0}
  51. };
  52. static const char* context_to_name(void* ptr) {
  53. return "SWR";
  54. }
  55. static const AVClass av_class = { "SwrContext", context_to_name, options, LIBAVUTIL_VERSION_INT, OFFSET(log_level_offset), OFFSET(log_ctx) };
  56. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  57. const AudioData * in_param, int in_count);
  58. SwrContext *swr_alloc(void){
  59. SwrContext *s= av_mallocz(sizeof(SwrContext));
  60. if(s){
  61. s->av_class= &av_class;
  62. av_opt_set_defaults2(s, 0, 0);
  63. }
  64. return s;
  65. }
  66. SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  67. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  68. int log_offset, void *log_ctx){
  69. if(!s) s= swr_alloc();
  70. if(!s) return NULL;
  71. s->log_level_offset= log_offset;
  72. s->log_ctx= log_ctx;
  73. av_set_int(s, "ocl", out_ch_layout);
  74. av_set_int(s, "osf", out_sample_fmt);
  75. av_set_int(s, "osr", out_sample_rate);
  76. av_set_int(s, "icl", in_ch_layout);
  77. av_set_int(s, "isf", in_sample_fmt);
  78. av_set_int(s, "isr", in_sample_rate);
  79. s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
  80. s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
  81. s->int_sample_fmt = AV_SAMPLE_FMT_S16;
  82. return s;
  83. }
  84. static void free_temp(AudioData *a){
  85. av_free(a->data);
  86. memset(a, 0, sizeof(*a));
  87. }
  88. void swr_free(SwrContext **ss){
  89. SwrContext *s= *ss;
  90. if(s){
  91. free_temp(&s->postin);
  92. free_temp(&s->midbuf);
  93. free_temp(&s->preout);
  94. free_temp(&s->in_buffer);
  95. swr_audio_convert_free(&s-> in_convert);
  96. swr_audio_convert_free(&s->out_convert);
  97. swr_audio_convert_free(&s->full_convert);
  98. swr_resample_free(&s->resample);
  99. }
  100. av_freep(ss);
  101. }
  102. int swr_init(SwrContext *s){
  103. s->in_buffer_index= 0;
  104. s->in_buffer_count= 0;
  105. s->resample_in_constraint= 0;
  106. free_temp(&s->postin);
  107. free_temp(&s->midbuf);
  108. free_temp(&s->preout);
  109. free_temp(&s->in_buffer);
  110. swr_audio_convert_free(&s-> in_convert);
  111. swr_audio_convert_free(&s->out_convert);
  112. swr_audio_convert_free(&s->full_convert);
  113. s-> in.planar= s-> in_sample_fmt >= 0x100;
  114. s->out.planar= s->out_sample_fmt >= 0x100;
  115. s-> in_sample_fmt &= 0xFF;
  116. s->out_sample_fmt &= 0xFF;
  117. if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
  118. av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->in_sample_fmt));
  119. return AVERROR(EINVAL);
  120. }
  121. if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
  122. av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->out_sample_fmt));
  123. return AVERROR(EINVAL);
  124. }
  125. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16
  126. &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
  127. av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
  128. return AVERROR(EINVAL);
  129. }
  130. //FIXME should we allow/support using FLT on material that doesnt need it ?
  131. if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
  132. s->int_sample_fmt= AV_SAMPLE_FMT_S16;
  133. }else
  134. s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
  135. if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
  136. s->resample = swr_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);
  137. }else
  138. swr_resample_free(&s->resample);
  139. if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){
  140. av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME
  141. return -1;
  142. }
  143. if(s-> in.ch_count && s-> in_ch_layout && s->in.ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
  144. av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than there actually is, ignoring layout\n");
  145. s-> in_ch_layout= 0;
  146. }
  147. if(!s-> in_ch_layout)
  148. s-> in_ch_layout= av_get_default_channel_layout(s->in.ch_count);
  149. if(!s->out_ch_layout)
  150. s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
  151. s->rematrix= s->out_ch_layout !=s->in_ch_layout;
  152. #define RSC 1 //FIXME finetune
  153. if(!s-> in.ch_count)
  154. s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
  155. if(!s->out.ch_count)
  156. s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
  157. av_assert0(s-> in.ch_count);
  158. av_assert0(s->out.ch_count);
  159. s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
  160. s-> in.bps= av_get_bits_per_sample_fmt(s-> in_sample_fmt)/8;
  161. s->int_bps= av_get_bits_per_sample_fmt(s->int_sample_fmt)/8;
  162. s->out.bps= av_get_bits_per_sample_fmt(s->out_sample_fmt)/8;
  163. if(!s->resample && !s->rematrix){
  164. s->full_convert = swr_audio_convert_alloc(s->out_sample_fmt,
  165. s-> in_sample_fmt, s-> in.ch_count, 0);
  166. return 0;
  167. }
  168. s->in_convert = swr_audio_convert_alloc(s->int_sample_fmt,
  169. s-> in_sample_fmt, s-> in.ch_count, 0);
  170. s->out_convert= swr_audio_convert_alloc(s->out_sample_fmt,
  171. s->int_sample_fmt, s->out.ch_count, 0);
  172. s->postin= s->in;
  173. s->preout= s->out;
  174. s->midbuf= s->in;
  175. s->in_buffer= s->in;
  176. if(!s->resample_first){
  177. s->midbuf.ch_count= s->out.ch_count;
  178. s->in_buffer.ch_count = s->out.ch_count;
  179. }
  180. s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps = s->int_bps;
  181. s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar = 1;
  182. if(s->rematrix && swr_rematrix_init(s)<0)
  183. return -1;
  184. return 0;
  185. }
  186. static int realloc_audio(AudioData *a, int count){
  187. int i, countb;
  188. AudioData old;
  189. if(a->count >= count)
  190. return 0;
  191. count*=2;
  192. countb= FFALIGN(count*a->bps, 32);
  193. old= *a;
  194. av_assert0(a->planar);
  195. av_assert0(a->bps);
  196. av_assert0(a->ch_count);
  197. a->data= av_malloc(countb*a->ch_count);
  198. if(!a->data)
  199. return AVERROR(ENOMEM);
  200. for(i=0; i<a->ch_count; i++){
  201. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  202. if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  203. }
  204. av_free(old.data);
  205. a->count= count;
  206. return 1;
  207. }
  208. static void copy(AudioData *out, AudioData *in,
  209. int count){
  210. av_assert0(out->planar == in->planar);
  211. av_assert0(out->bps == in->bps);
  212. av_assert0(out->ch_count == in->ch_count);
  213. if(out->planar){
  214. int ch;
  215. for(ch=0; ch<out->ch_count; ch++)
  216. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  217. }else
  218. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  219. }
  220. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  221. int i;
  222. if(out->planar){
  223. for(i=0; i<out->ch_count; i++)
  224. out->ch[i]= in_arg[i];
  225. }else{
  226. for(i=0; i<out->ch_count; i++)
  227. out->ch[i]= in_arg[0] + i*out->bps;
  228. }
  229. }
  230. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  231. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  232. AudioData *postin, *midbuf, *preout;
  233. int ret, i/*, in_max*/;
  234. AudioData * in= &s->in;
  235. AudioData *out= &s->out;
  236. AudioData preout_tmp, midbuf_tmp;
  237. if(!s->resample){
  238. if(in_count > out_count)
  239. return -1;
  240. out_count = in_count;
  241. }
  242. fill_audiodata(in , in_arg);
  243. fill_audiodata(out, out_arg);
  244. if(s->full_convert){
  245. av_assert0(!s->resample);
  246. swr_audio_convert(s->full_convert, out, in, in_count);
  247. return out_count;
  248. }
  249. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  250. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  251. if((ret=realloc_audio(&s->postin, in_count))<0)
  252. return ret;
  253. if(s->resample_first){
  254. av_assert0(s->midbuf.ch_count == s-> in.ch_count);
  255. if((ret=realloc_audio(&s->midbuf, out_count))<0)
  256. return ret;
  257. }else{
  258. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  259. if((ret=realloc_audio(&s->midbuf, in_count))<0)
  260. return ret;
  261. }
  262. if((ret=realloc_audio(&s->preout, out_count))<0)
  263. return ret;
  264. postin= &s->postin;
  265. midbuf_tmp= s->midbuf;
  266. midbuf= &midbuf_tmp;
  267. preout_tmp= s->preout;
  268. preout= &preout_tmp;
  269. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
  270. postin= in;
  271. if(s->resample_first ? !s->resample : !s->rematrix)
  272. midbuf= postin;
  273. if(s->resample_first ? !s->rematrix : !s->resample)
  274. preout= midbuf;
  275. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
  276. if(preout==in){
  277. out_count= FFMIN(out_count, in_count); //TODO check at teh end if this is needed or redundant
  278. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  279. copy(out, in, out_count);
  280. return out_count;
  281. }
  282. else if(preout==postin) preout= midbuf= postin= out;
  283. else if(preout==midbuf) preout= midbuf= out;
  284. else preout= out;
  285. }
  286. if(in != postin){
  287. swr_audio_convert(s->in_convert, postin, in, in_count);
  288. }
  289. if(s->resample_first){
  290. if(postin != midbuf)
  291. out_count= resample(s, midbuf, out_count, postin, in_count);
  292. if(midbuf != preout)
  293. swr_rematrix(s, preout, midbuf, out_count, preout==out);
  294. }else{
  295. if(postin != midbuf)
  296. swr_rematrix(s, midbuf, postin, in_count, midbuf==out);
  297. if(midbuf != preout)
  298. out_count= resample(s, preout, out_count, midbuf, in_count);
  299. }
  300. if(preout != out){
  301. //FIXME packed doesnt need more than 1 chan here!
  302. swr_audio_convert(s->out_convert, out, preout, out_count);
  303. }
  304. return out_count;
  305. }
  306. /**
  307. *
  308. * out may be equal in.
  309. */
  310. static void buf_set(AudioData *out, AudioData *in, int count){
  311. if(in->planar){
  312. int ch;
  313. for(ch=0; ch<out->ch_count; ch++)
  314. out->ch[ch]= in->ch[ch] + count*out->bps;
  315. }else
  316. out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
  317. }
  318. /**
  319. *
  320. * @return number of samples output per channel
  321. */
  322. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  323. const AudioData * in_param, int in_count){
  324. AudioData in, out, tmp;
  325. int ret_sum=0;
  326. int border=0;
  327. tmp=out=*out_param;
  328. in = *in_param;
  329. do{
  330. int ret, size, consumed;
  331. if(!s->resample_in_constraint && s->in_buffer_count){
  332. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  333. ret= swr_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  334. out_count -= ret;
  335. ret_sum += ret;
  336. buf_set(&out, &out, ret);
  337. s->in_buffer_count -= consumed;
  338. s->in_buffer_index += consumed;
  339. if(!in_count)
  340. break;
  341. if(s->in_buffer_count <= border){
  342. buf_set(&in, &in, -s->in_buffer_count);
  343. in_count += s->in_buffer_count;
  344. s->in_buffer_count=0;
  345. s->in_buffer_index=0;
  346. border = 0;
  347. }
  348. }
  349. if(in_count && !s->in_buffer_count){
  350. s->in_buffer_index=0;
  351. ret= swr_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
  352. out_count -= ret;
  353. ret_sum += ret;
  354. buf_set(&out, &out, ret);
  355. in_count -= consumed;
  356. buf_set(&in, &in, consumed);
  357. }
  358. //TODO is this check sane considering the advanced copy avoidance below
  359. size= s->in_buffer_index + s->in_buffer_count + in_count;
  360. if( size > s->in_buffer.count
  361. && s->in_buffer_count + in_count <= s->in_buffer_index){
  362. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  363. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  364. s->in_buffer_index=0;
  365. }else
  366. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  367. return ret;
  368. if(in_count){
  369. int count= in_count;
  370. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  371. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  372. copy(&tmp, &in, /*in_*/count);
  373. s->in_buffer_count += count;
  374. in_count -= count;
  375. border += count;
  376. buf_set(&in, &in, count);
  377. s->resample_in_constraint= 0;
  378. if(s->in_buffer_count != count || in_count)
  379. continue;
  380. }
  381. break;
  382. }while(1);
  383. s->resample_in_constraint= !!out_count;
  384. return ret_sum;
  385. }