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.

466 lines
16KB

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