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.

496 lines
17KB

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