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.

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