swresample.c 16 KB

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