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.

621 lines
22KB

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