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.

537 lines
19KB

  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}, 0, SWR_CH_MAX, 0},
  36. {"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
  37. {"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 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 = {
  58. .class_name = "SwrContext",
  59. .item_name = context_to_name,
  60. .option = options,
  61. .version = LIBAVUTIL_VERSION_INT,
  62. .log_level_offset_offset = OFFSET(log_level_offset),
  63. .parent_log_context_offset = OFFSET(log_ctx),
  64. };
  65. unsigned swresample_version(void)
  66. {
  67. av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
  68. return LIBSWRESAMPLE_VERSION_INT;
  69. }
  70. const char *swresample_configuration(void)
  71. {
  72. return FFMPEG_CONFIGURATION;
  73. }
  74. const char *swresample_license(void)
  75. {
  76. #define LICENSE_PREFIX "libswresample license: "
  77. return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
  78. }
  79. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
  80. if(!s || s->in_convert) // s needs to be allocated but not initialized
  81. return AVERROR(EINVAL);
  82. s->channel_map = channel_map;
  83. return 0;
  84. }
  85. struct SwrContext *swr_alloc(void){
  86. SwrContext *s= av_mallocz(sizeof(SwrContext));
  87. if(s){
  88. s->av_class= &av_class;
  89. av_opt_set_defaults(s);
  90. }
  91. return s;
  92. }
  93. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  94. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  95. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  96. int log_offset, void *log_ctx){
  97. if(!s) s= swr_alloc();
  98. if(!s) return NULL;
  99. s->log_level_offset= log_offset;
  100. s->log_ctx= log_ctx;
  101. av_opt_set_int(s, "ocl", out_ch_layout, 0);
  102. av_opt_set_int(s, "osf", out_sample_fmt, 0);
  103. av_opt_set_int(s, "osr", out_sample_rate, 0);
  104. av_opt_set_int(s, "icl", in_ch_layout, 0);
  105. av_opt_set_int(s, "isf", in_sample_fmt, 0);
  106. av_opt_set_int(s, "isr", in_sample_rate, 0);
  107. av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_S16, 0);
  108. av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
  109. av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
  110. av_opt_set_int(s, "uch", 0, 0);
  111. return s;
  112. }
  113. static void free_temp(AudioData *a){
  114. av_free(a->data);
  115. memset(a, 0, sizeof(*a));
  116. }
  117. void swr_free(SwrContext **ss){
  118. SwrContext *s= *ss;
  119. if(s){
  120. free_temp(&s->postin);
  121. free_temp(&s->midbuf);
  122. free_temp(&s->preout);
  123. free_temp(&s->in_buffer);
  124. swri_audio_convert_free(&s-> in_convert);
  125. swri_audio_convert_free(&s->out_convert);
  126. swri_audio_convert_free(&s->full_convert);
  127. swri_resample_free(&s->resample);
  128. }
  129. av_freep(ss);
  130. }
  131. int swr_init(struct SwrContext *s){
  132. s->in_buffer_index= 0;
  133. s->in_buffer_count= 0;
  134. s->resample_in_constraint= 0;
  135. free_temp(&s->postin);
  136. free_temp(&s->midbuf);
  137. free_temp(&s->preout);
  138. free_temp(&s->in_buffer);
  139. swri_audio_convert_free(&s-> in_convert);
  140. swri_audio_convert_free(&s->out_convert);
  141. swri_audio_convert_free(&s->full_convert);
  142. s->flushed = 0;
  143. s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt);
  144. s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt);
  145. s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0);
  146. s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0);
  147. if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
  148. av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
  149. return AVERROR(EINVAL);
  150. }
  151. if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
  152. av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
  153. return AVERROR(EINVAL);
  154. }
  155. if( s->int_sample_fmt != AV_SAMPLE_FMT_S16
  156. &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
  157. 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));
  158. return AVERROR(EINVAL);
  159. }
  160. //FIXME should we allow/support using FLT on material that doesnt need it ?
  161. if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
  162. s->int_sample_fmt= AV_SAMPLE_FMT_S16;
  163. }else
  164. s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
  165. if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
  166. s->resample = swri_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);
  167. }else
  168. swri_resample_free(&s->resample);
  169. if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){
  170. av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME
  171. return -1;
  172. }
  173. if(!s->used_ch_count)
  174. s->used_ch_count= s->in.ch_count;
  175. if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
  176. av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
  177. s-> in_ch_layout= 0;
  178. }
  179. if(!s-> in_ch_layout)
  180. s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
  181. if(!s->out_ch_layout)
  182. s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
  183. s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0 ||
  184. s->rematrix_custom;
  185. #define RSC 1 //FIXME finetune
  186. if(!s-> in.ch_count)
  187. s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
  188. if(!s->used_ch_count)
  189. s->used_ch_count= s->in.ch_count;
  190. if(!s->out.ch_count)
  191. s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
  192. if(!s-> in.ch_count){
  193. av_assert0(!s->in_ch_layout);
  194. av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
  195. return -1;
  196. }
  197. if ((!s->out_ch_layout || !s->in_ch_layout) && s->used_ch_count != s->out.ch_count && !s->rematrix_custom) {
  198. av_log(s, AV_LOG_ERROR, "Rematrix is needed but there is not enough information to do it\n");
  199. return -1;
  200. }
  201. av_assert0(s->used_ch_count);
  202. av_assert0(s->out.ch_count);
  203. s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
  204. s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
  205. s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
  206. s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
  207. if(!s->resample && !s->rematrix && !s->channel_map){
  208. s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
  209. s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
  210. return 0;
  211. }
  212. s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
  213. s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
  214. s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
  215. s->int_sample_fmt, s->out.ch_count, NULL, 0);
  216. s->postin= s->in;
  217. s->preout= s->out;
  218. s->midbuf= s->in;
  219. s->in_buffer= s->in;
  220. if(s->channel_map){
  221. s->postin.ch_count=
  222. s->midbuf.ch_count=
  223. s->in_buffer.ch_count= s->used_ch_count;
  224. }
  225. if(!s->resample_first){
  226. s->midbuf.ch_count= s->out.ch_count;
  227. s->in_buffer.ch_count = s->out.ch_count;
  228. }
  229. s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps = s->int_bps;
  230. s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar = 1;
  231. if(s->rematrix)
  232. return swri_rematrix_init(s);
  233. return 0;
  234. }
  235. static int realloc_audio(AudioData *a, int count){
  236. int i, countb;
  237. AudioData old;
  238. if(a->count >= count)
  239. return 0;
  240. count*=2;
  241. countb= FFALIGN(count*a->bps, 32);
  242. old= *a;
  243. av_assert0(a->planar);
  244. av_assert0(a->bps);
  245. av_assert0(a->ch_count);
  246. a->data= av_malloc(countb*a->ch_count);
  247. if(!a->data)
  248. return AVERROR(ENOMEM);
  249. for(i=0; i<a->ch_count; i++){
  250. a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
  251. if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
  252. }
  253. av_free(old.data);
  254. a->count= count;
  255. return 1;
  256. }
  257. static void copy(AudioData *out, AudioData *in,
  258. int count){
  259. av_assert0(out->planar == in->planar);
  260. av_assert0(out->bps == in->bps);
  261. av_assert0(out->ch_count == in->ch_count);
  262. if(out->planar){
  263. int ch;
  264. for(ch=0; ch<out->ch_count; ch++)
  265. memcpy(out->ch[ch], in->ch[ch], count*out->bps);
  266. }else
  267. memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
  268. }
  269. static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
  270. int i;
  271. if(out->planar){
  272. for(i=0; i<out->ch_count; i++)
  273. out->ch[i]= in_arg[i];
  274. }else{
  275. for(i=0; i<out->ch_count; i++)
  276. out->ch[i]= in_arg[0] + i*out->bps;
  277. }
  278. }
  279. /**
  280. *
  281. * out may be equal in.
  282. */
  283. static void buf_set(AudioData *out, AudioData *in, int count){
  284. if(in->planar){
  285. int ch;
  286. for(ch=0; ch<out->ch_count; ch++)
  287. out->ch[ch]= in->ch[ch] + count*out->bps;
  288. }else
  289. out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
  290. }
  291. /**
  292. *
  293. * @return number of samples output per channel
  294. */
  295. static int resample(SwrContext *s, AudioData *out_param, int out_count,
  296. const AudioData * in_param, int in_count){
  297. AudioData in, out, tmp;
  298. int ret_sum=0;
  299. int border=0;
  300. tmp=out=*out_param;
  301. in = *in_param;
  302. do{
  303. int ret, size, consumed;
  304. if(!s->resample_in_constraint && s->in_buffer_count){
  305. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  306. ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
  307. out_count -= ret;
  308. ret_sum += ret;
  309. buf_set(&out, &out, ret);
  310. s->in_buffer_count -= consumed;
  311. s->in_buffer_index += consumed;
  312. if(!in_count)
  313. break;
  314. if(s->in_buffer_count <= border){
  315. buf_set(&in, &in, -s->in_buffer_count);
  316. in_count += s->in_buffer_count;
  317. s->in_buffer_count=0;
  318. s->in_buffer_index=0;
  319. border = 0;
  320. }
  321. }
  322. if(in_count && !s->in_buffer_count){
  323. s->in_buffer_index=0;
  324. ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
  325. out_count -= ret;
  326. ret_sum += ret;
  327. buf_set(&out, &out, ret);
  328. in_count -= consumed;
  329. buf_set(&in, &in, consumed);
  330. }
  331. //TODO is this check sane considering the advanced copy avoidance below
  332. size= s->in_buffer_index + s->in_buffer_count + in_count;
  333. if( size > s->in_buffer.count
  334. && s->in_buffer_count + in_count <= s->in_buffer_index){
  335. buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
  336. copy(&s->in_buffer, &tmp, s->in_buffer_count);
  337. s->in_buffer_index=0;
  338. }else
  339. if((ret=realloc_audio(&s->in_buffer, size)) < 0)
  340. return ret;
  341. if(in_count){
  342. int count= in_count;
  343. if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
  344. buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
  345. copy(&tmp, &in, /*in_*/count);
  346. s->in_buffer_count += count;
  347. in_count -= count;
  348. border += count;
  349. buf_set(&in, &in, count);
  350. s->resample_in_constraint= 0;
  351. if(s->in_buffer_count != count || in_count)
  352. continue;
  353. }
  354. break;
  355. }while(1);
  356. s->resample_in_constraint= !!out_count;
  357. return ret_sum;
  358. }
  359. int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
  360. const uint8_t *in_arg [SWR_CH_MAX], int in_count){
  361. AudioData *postin, *midbuf, *preout;
  362. int ret/*, in_max*/;
  363. AudioData * in= &s->in;
  364. AudioData *out= &s->out;
  365. AudioData preout_tmp, midbuf_tmp;
  366. if(!s->resample){
  367. if(in_count > out_count)
  368. return -1;
  369. out_count = in_count;
  370. }
  371. if(!in_arg){
  372. if(s->in_buffer_count){
  373. if (!s->flushed) {
  374. AudioData *a= &s->in_buffer;
  375. int i, j, ret;
  376. if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
  377. return ret;
  378. av_assert0(a->planar);
  379. for(i=0; i<a->ch_count; i++){
  380. for(j=0; j<s->in_buffer_count; j++){
  381. memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
  382. a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
  383. }
  384. }
  385. s->in_buffer_count += (s->in_buffer_count+1)/2;
  386. s->resample_in_constraint = 0;
  387. s->flushed = 1;
  388. }
  389. }else{
  390. return 0;
  391. }
  392. }else
  393. fill_audiodata(in , (void*)in_arg);
  394. fill_audiodata(out, out_arg);
  395. if(s->full_convert){
  396. av_assert0(!s->resample);
  397. swri_audio_convert(s->full_convert, out, in, in_count);
  398. return out_count;
  399. }
  400. // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
  401. // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
  402. if((ret=realloc_audio(&s->postin, in_count))<0)
  403. return ret;
  404. if(s->resample_first){
  405. av_assert0(s->midbuf.ch_count == s->used_ch_count);
  406. if((ret=realloc_audio(&s->midbuf, out_count))<0)
  407. return ret;
  408. }else{
  409. av_assert0(s->midbuf.ch_count == s->out.ch_count);
  410. if((ret=realloc_audio(&s->midbuf, in_count))<0)
  411. return ret;
  412. }
  413. if((ret=realloc_audio(&s->preout, out_count))<0)
  414. return ret;
  415. postin= &s->postin;
  416. midbuf_tmp= s->midbuf;
  417. midbuf= &midbuf_tmp;
  418. preout_tmp= s->preout;
  419. preout= &preout_tmp;
  420. if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
  421. postin= in;
  422. if(s->resample_first ? !s->resample : !s->rematrix)
  423. midbuf= postin;
  424. if(s->resample_first ? !s->rematrix : !s->resample)
  425. preout= midbuf;
  426. if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
  427. if(preout==in){
  428. out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
  429. av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
  430. copy(out, in, out_count);
  431. return out_count;
  432. }
  433. else if(preout==postin) preout= midbuf= postin= out;
  434. else if(preout==midbuf) preout= midbuf= out;
  435. else preout= out;
  436. }
  437. if(in != postin){
  438. swri_audio_convert(s->in_convert, postin, in, in_count);
  439. }
  440. if(s->resample_first){
  441. if(postin != midbuf)
  442. out_count= resample(s, midbuf, out_count, postin, in_count);
  443. if(midbuf != preout)
  444. swri_rematrix(s, preout, midbuf, out_count, preout==out);
  445. }else{
  446. if(postin != midbuf)
  447. swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
  448. if(midbuf != preout)
  449. out_count= resample(s, preout, out_count, midbuf, in_count);
  450. }
  451. if(preout != out){
  452. //FIXME packed doesnt need more than 1 chan here!
  453. swri_audio_convert(s->out_convert, out, preout, out_count);
  454. }
  455. return out_count;
  456. }