1 /*
2 * Copyright (C) 2011-2013 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
27
29
31
33 if(!
s ||
s->in_convert)
// s needs to be allocated but not initialized
36 return 0;
37 }
38
42 int log_offset, void *log_ctx) {
45
48
50
51 s->log_level_offset = log_offset;
53
56
59
62
65
68
71
73
74 return 0;
79 }
80
87 }
88
91 memset(
a, 0,
sizeof(*
a));
92 }
93
95 s->in_buffer_index= 0;
96 s->in_buffer_count= 0;
97 s->resample_in_constraint= 0;
98 memset(
s->in.ch, 0,
sizeof(
s->in.ch));
99 memset(
s->out.ch, 0,
sizeof(
s->out.ch));
115
116 s->delayed_samples_fixup = 0;
118 }
119
127
129 s->resampler->free(&
s->resample);
130 }
131
133 }
134
137 }
138
141 char l1[1024], l2[1024];
142
144
148 }
152 }
153
157 }
158 if(
s->out_sample_rate <= 0){
161 }
162
165
171 }
172
178 }
179
185
186 s->int_sample_fmt=
s->user_int_sample_fmt;
187
188 s->dither.method =
s->user_dither_method;
189
191 #if CONFIG_LIBSOXR
193 #endif
195 default:
198 }
199
202
203 if (
s->used_ch_layout.nb_channels !=
s->in_ch_layout.nb_channels)
205
212 }
215
217 s->rematrix_volume!=1.0 ||
219
221 // 16bit or less to 16bit or less with the same sample rate
224 &&
s->out_sample_rate==
s->in_sample_rate) {
226 // 8 -> 8, 16->8, 8->16bit
232 &&
s->out_sample_rate==
s->in_sample_rate
238 &&
s->out_sample_rate ==
s->in_sample_rate
244 }else{
246 }
247 }
249
257 }
258
261
263 if (!
s->async &&
s->min_compensation >= FLT_MAX/2)
267 s->outpts =
s->firstpts_in_samples *
s->out_sample_rate;
268 } else
270
272 if (
s->min_compensation >= FLT_MAX/2)
273 s->min_compensation = 0.001;
274 if (
s->async > 1.0001) {
275 s->max_soft_compensation =
s->async / (
double)
s->in_sample_rate;
276 }
277 }
278
280 s->resample =
s->resampler->init(
s->resample,
s->out_sample_rate,
s->in_sample_rate,
s->filter_size,
s->phase_shift,
s->linear_interp,
s->cutoff,
s->int_sample_fmt,
s->filter_type,
s->kaiser_beta,
s->precision,
s->cheby,
s->exact_rational);
284 }
285 }else
286 s->resampler->free(&
s->resample);
295 }
296
297 #define RSC 1 //FIXME finetune
303 s->out.ch_count =
s->out_ch_layout.nb_channels;
304
310 }
311
315 av_log(
s,
AV_LOG_ERROR,
"Input channel layout %s mismatches specified channel count %d\n", l1,
s->used_ch_layout.nb_channels);
318 }
319
323 "but there is not enough information to do it\n", l1, l2);
326 }
327
331
334 s->drop_temp=
s->out;
335
338
339 if(!
s->resample && !
s->rematrix && !
s->channel_map && !
s->dither.method){
342 return 0;
343 }
344
348 s->int_sample_fmt,
s->out.ch_count,
NULL, 0);
349
350 if (!
s->in_convert || !
s->out_convert) {
353 }
354
358
361 s->midbuf.ch_count=
s->used_ch_layout.nb_channels;
363 s->in_buffer.ch_count=
s->used_ch_layout.nb_channels;
364 }
365 if(!
s->resample_first){
366 s->midbuf.ch_count=
s->out.ch_count;
368 s->in_buffer.ch_count =
s->out.ch_count;
369 }
370
374
377 }
378
380 s->dither.noise =
s->preout;
381 s->dither.temp =
s->preout;
383 s->dither.noise.bps = 4;
385 s->dither.noise_scale = 1;
386 }
387
388 if(
s->rematrix ||
s->dither.method) {
392 }
393
394 return 0;
398
399 }
400
404
405 if(count < 0 || count > INT_MAX/2/
a->bps/
a->ch_count)
407
408 if(
a->count >= count)
409 return 0;
410
411 count*=2;
412
415
418
422 for(
i=0;
i<
a->ch_count;
i++){
423 a->ch[
i]=
a->data +
i*(
a->planar ? countb :
a->bps);
424 if(
a->count &&
a->planar) memcpy(
a->ch[
i], old.
ch[
i],
a->count*
a->bps);
425 }
426 if(
a->count && !
a->planar) memcpy(
a->ch[0], old.
ch[0],
a->count*
a->ch_count*
a->bps);
429
430 return 1;
431 }
432
434 int count){
439 int ch;
440 for(ch=0; ch<
out->ch_count; ch++)
441 memcpy(
out->ch[ch],
in->
ch[ch], count*
out->bps);
442 }else
444 }
445
447 {
449 if(!in_arg){
450 memset(
out->ch, 0,
sizeof(
out->ch));
451 }
else if(
out->planar){
452 for(
i=0;
i<
out->ch_count;
i++)
453 out->ch[
i]= in_arg[
i];
454 }else{
455 for(
i=0;
i<
out->ch_count;
i++)
457 }
458 }
459
463 for(
i=0;
i<
out->ch_count;
i++)
464 in_arg[
i]=
out->ch[
i];
465 }else{
466 in_arg[0]=
out->ch[0];
467 }
468 }
469
470 /**
471 *
472 * out may be equal in.
473 */
475 int ch;
477 for(ch=0; ch<
out->ch_count; ch++)
479 }else{
480 for(ch=
out->ch_count-1; ch>=0; ch--)
481 out->ch[ch]=
in->
ch[0] + (ch + count*
out->ch_count) *
out->bps;
482 }
483 }
484
485 /**
486 *
487 * @return number of samples output per channel
488 */
490 const AudioData * in_param,
int in_count){
492 int ret_sum=0;
493 int border=0;
495
499
502
503 border =
s->resampler->invert_initial_buffer(
s->resample, &
s->in_buffer,
504 &
in, in_count, &
s->in_buffer_index, &
s->in_buffer_count);
505 if (border == INT_MAX) {
506 return 0;
507 } else if (border < 0) {
508 return border;
509 } else if (border) {
511 in_count -= border;
512 s->resample_in_constraint = 0;
513 }
514
515 do{
517 if(!
s->resample_in_constraint &&
s->in_buffer_count){
519 ret=
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
tmp,
s->in_buffer_count, &consumed);
523 s->in_buffer_count -= consumed;
524 s->in_buffer_index += consumed;
525
526 if(!in_count)
527 break;
528 if(
s->in_buffer_count <= border){
530 in_count +=
s->in_buffer_count;
531 s->in_buffer_count=0;
532 s->in_buffer_index=0;
533 border = 0;
534 }
535 }
536
537 if((
s->flushed || in_count > padless) && !
s->in_buffer_count){
538 s->in_buffer_index=0;
539 ret=
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
in,
FFMAX(in_count-padless, 0), &consumed);
543 in_count -= consumed;
545 }
546
547 //TODO is this check sane considering the advanced copy avoidance below
548 size=
s->in_buffer_index +
s->in_buffer_count + in_count;
549 if(
size >
s->in_buffer.count
552 copy(&
s->in_buffer, &
tmp,
s->in_buffer_count);
553 s->in_buffer_index=0;
554 }else
557
558 if(in_count){
559 int count= in_count;
560 if(
s->in_buffer_count &&
s->in_buffer_count+2 < count && out_count) count=
s->in_buffer_count+2;
561
562 buf_set(&
tmp, &
s->in_buffer,
s->in_buffer_index +
s->in_buffer_count);
564 s->in_buffer_count += count;
565 in_count -= count;
566 border += count;
568 s->resample_in_constraint= 0;
569 if(
s->in_buffer_count != count || in_count)
570 continue;
571 if (padless) {
572 padless = 0;
573 continue;
574 }
575 }
576 break;
577 }while(1);
578
579 s->resample_in_constraint= !!out_count;
580
581 return ret_sum;
582 }
583
587 int ret /*, in_max*/;
589
593 return out_count;
594 }
595
596 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
597 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
598
601 if(
s->resample_first){
602 av_assert0(
s->midbuf.ch_count ==
s->used_ch_layout.nb_channels);
605 }else{
609 }
612
614
615 midbuf_tmp=
s->midbuf;
617 preout_tmp=
s->preout;
619
622
623 if(
s->resample_first ? !
s->resample : !
s->rematrix)
625
626 if(
s->resample_first ? !
s->rematrix : !
s->resample)
628
629 if(
s->int_sample_fmt ==
s->out_sample_fmt &&
s->out.planar
632 out_count=
FFMIN(out_count, in_count);
//TODO check at the end if this is needed or redundant
633 av_assert0(
s->in.planar);
//we only support planar internally so it has to be, we support copying non planar though
635 return out_count;
636 }
640 }
641
644 }
645
646 if(
s->resample_first){
649 return out_count;
652 }else{
657 return out_count;
658 }
659
662 if(
s->dither.method){
663 int ch;
664 int dither_count=
FFMAX(out_count, 1<<16);
665
667 conv_src = &
s->dither.temp;
670 }
671
675 for(ch=0; ch<
s->dither.noise.ch_count; ch++)
676 if((
ret=
swri_get_dither(
s,
s->dither.noise.ch[ch],
s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828
U,
s->dither.noise.fmt))<0)
679
680 if(
s->dither.noise_pos + out_count >
s->dither.noise.count)
681 s->dither.noise_pos = 0;
682
684 if (
s->mix_2_1_simd) {
685 int len1= out_count&~15;
687
688 if(len1)
690 s->mix_2_1_simd(conv_src->
ch[ch],
preout->
ch[ch],
s->dither.noise.ch[ch] +
s->dither.noise.bps *
s->dither.noise_pos,
s->native_simd_one, 0, 0, len1);
691 if(out_count != len1)
693 s->mix_2_1_f(conv_src->
ch[ch] + off,
preout->
ch[ch] + off,
s->dither.noise.ch[ch] +
s->dither.noise.bps *
s->dither.noise_pos + off,
s->native_one, 0, 0, out_count - len1);
694 } else {
696 s->mix_2_1_f(conv_src->
ch[ch],
preout->
ch[ch],
s->dither.noise.ch[ch] +
s->dither.noise.bps *
s->dither.noise_pos,
s->native_one, 0, 0, out_count);
697 }
698 } else {
699 switch(
s->int_sample_fmt) {
704 }
705 }
706 s->dither.noise_pos += out_count;
707 }
708 //FIXME packed doesn't need more than 1 chan here!
710 }
711 return out_count;
712 }
713
715 return !!
s->in_buffer.ch_count;
716 }
717
719 uint8_t * const *out_arg, int out_count,
720 const uint8_t * const *in_arg, int in_count)
721 {
725
729 }
730 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
732 #endif
733
734 while(
s->drop_output > 0){
737 #define MAX_DROP_STEP 16384
740
742 s->drop_output *= -1;
//FIXME find a less hackish solution
743 ret =
swr_convert(
s, tmp_arg,
FFMIN(-
s->drop_output,
MAX_DROP_STEP), in_arg, in_count);
//FIXME optimize but this is as good as never called so maybe it doesn't matter
744 s->drop_output *= -1;
745 in_count = 0;
747 s->drop_output -=
ret;
748 if (!
s->drop_output && !out_arg)
749 return 0;
750 continue;
751 }
752
754 return 0;
755 }
756
757 if(!in_arg){
760 s->resampler->flush(
s);
761 s->resample_in_constraint = 0;
763 }
else if(!
s->in_buffer_count){
764 return 0;
765 }
766 }else
768
770
773 if(
ret>0 && !
s->drop_output)
775
777
779 }else{
781 int ret2=0;
790 s->in_buffer_count -=
ret;
791 s->in_buffer_index +=
ret;
794 if(!
s->in_buffer_count)
795 s->in_buffer_index = 0;
796 }
797
798 if(in_count){
799 size=
s->in_buffer_index +
s->in_buffer_count + in_count - out_count;
800
801 if(in_count > out_count) { //FIXME move after swr_convert_internal
802 if(
size >
s->in_buffer.count
805 copy(&
s->in_buffer, &
tmp,
s->in_buffer_count);
806 s->in_buffer_index=0;
807 }else
810 }
811
812 if(out_count){
820 }
821 if(in_count){
822 buf_set(&
tmp, &
s->in_buffer,
s->in_buffer_index +
s->in_buffer_count);
824 s->in_buffer_count += in_count;
825 }
826 }
827 if(ret2>0 && !
s->drop_output)
828 s->outpts += ret2 * (
int64_t)
s->in_sample_rate;
829 av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
830 return ret2;
831 }
832 }
833
836 s->drop_output += count;
837
838 if(
s->drop_output <= 0)
839 return 0;
840
843 }
844
848
849 if(count <= 0)
850 return 0;
851
852 #define MAX_SILENCE_STEP 16384
857 }
858
861
862 if(
s->silence.planar)
for(
i=0;
i<
s->silence.ch_count;
i++) {
863 memset(
s->silence.ch[
i],
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps);
864 } else
865 memset(
s->silence.ch[0],
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps*
s->silence.ch_count);
866
871 }
872
874 if (
s->resampler &&
s->resample){
875 return s->resampler->get_delay(
s,
base);
876 }else{
877 return (
s->in_buffer_count*
base + (
s->in_sample_rate>>1))/
s->in_sample_rate;
878 }
879 }
880
882 {
884
885 if (in_samples < 0)
887
888 if (
s->resampler &&
s->resample) {
889 if (!
s->resampler->get_out_samples)
891 out_samples =
s->resampler->get_out_samples(
s, in_samples);
892 } else {
893 out_samples =
s->in_buffer_count + in_samples;
895 }
896
897 if (out_samples > INT_MAX)
899
900 return out_samples;
901 }
902
905
906 if (!
s || compensation_distance < 0)
908 if (!compensation_distance && sample_delta)
915 }
916 if (!
s->resampler->set_compensation){
918 }else{
919 return s->resampler->set_compensation(
s->resample, sample_delta, compensation_distance);
920 }
921 }
922
926
928 s->outpts =
s->firstpts =
pts;
929
930 if(
s->min_compensation >= FLT_MAX) {
932 } else {
934 double fdelta =
delta /(
double)(
s->in_sample_rate * (
int64_t)
s->out_sample_rate);
935
936 if(
fabs(fdelta) >
s->min_compensation) {
937 if(
s->outpts ==
s->firstpts ||
fabs(fdelta) >
s->min_hard_compensation){
943 }
944 }
else if(
s->soft_compensation_duration &&
s->max_soft_compensation) {
945 int duration =
s->out_sample_rate *
s->soft_compensation_duration;
950 }
951 }
952
954 }
955 }