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
28
30
32
34 if(!
s ||
s->in_convert)
// s needs to be allocated but not initialized
37 return 0;
38 }
39
43 int log_offset, void *log_ctx) {
46
49
51
52 s->log_level_offset = log_offset;
54
57
60
63
66
69
72
73 return 0;
78 }
79
86 }
87
90 memset(
a, 0,
sizeof(*
a));
91 }
92
94 s->in_buffer_index= 0;
95 s->in_buffer_count= 0;
96 s->resample_in_constraint= 0;
97 memset(
s->in.ch, 0,
sizeof(
s->in.ch));
98 memset(
s->out.ch, 0,
sizeof(
s->out.ch));
114
115 s->delayed_samples_fixup = 0;
117 }
118
126
128 s->resampler->free(&
s->resample);
129 }
130
132 }
133
136 }
137
140 char l1[1024], l2[1024];
141
143
147 }
151 }
152
156 }
157 if(
s->out_sample_rate <= 0){
160 }
161
164
170 }
171
177 }
178
184
185 s->int_sample_fmt=
s->user_int_sample_fmt;
186
187 s->dither.method =
s->user_dither_method;
188
190 #if CONFIG_LIBSOXR
192 #endif
194 default:
197 }
198
201
202 if (
s->used_ch_layout.nb_channels !=
s->in_ch_layout.nb_channels)
204
211 }
214
216 s->rematrix_volume!=1.0 ||
218
220 // 16bit or less to 16bit or less with the same sample rate
223 &&
s->out_sample_rate==
s->in_sample_rate) {
225 // 8 -> 8, 16->8, 8->16bit
231 &&
s->out_sample_rate==
s->in_sample_rate
237 &&
s->out_sample_rate ==
s->in_sample_rate
243 }else{
245 }
246 }
248
256 }
257
260
262 if (!
s->async &&
s->min_compensation >= FLT_MAX/2)
266 s->outpts =
s->firstpts_in_samples *
s->out_sample_rate;
267 } else
269
271 if (
s->min_compensation >= FLT_MAX/2)
272 s->min_compensation = 0.001;
273 if (
s->async > 1.0001) {
274 s->max_soft_compensation =
s->async / (
double)
s->in_sample_rate;
275 }
276 }
277
279 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);
283 }
284 }else
285 s->resampler->free(&
s->resample);
294 }
295
296 #define RSC 1 //FIXME finetune
302 s->out.ch_count =
s->out_ch_layout.nb_channels;
303
309 }
310
314 av_log(
s,
AV_LOG_ERROR,
"Input channel layout %s mismatches specified channel count %d\n", l1,
s->used_ch_layout.nb_channels);
317 }
318
322 "but there is not enough information to do it\n", l1, l2);
325 }
326
330
333 s->drop_temp=
s->out;
334
337
338 if(!
s->resample && !
s->rematrix && !
s->channel_map && !
s->dither.method){
341 return 0;
342 }
343
347 s->int_sample_fmt,
s->out.ch_count,
NULL, 0);
348
349 if (!
s->in_convert || !
s->out_convert) {
352 }
353
357
360 s->midbuf.ch_count=
s->used_ch_layout.nb_channels;
362 s->in_buffer.ch_count=
s->used_ch_layout.nb_channels;
363 }
364 if(!
s->resample_first){
365 s->midbuf.ch_count=
s->out.ch_count;
367 s->in_buffer.ch_count =
s->out.ch_count;
368 }
369
373
376 }
377
379 s->dither.noise =
s->preout;
380 s->dither.temp =
s->preout;
382 s->dither.noise.bps = 4;
384 s->dither.noise_scale = 1;
385 }
386
387 if(
s->rematrix ||
s->dither.method) {
391 }
392
393 return 0;
397
398 }
399
403
404 if(count < 0 || count > INT_MAX/2/
a->bps/
a->ch_count)
406
407 if(
a->count >= count)
408 return 0;
409
410 count*=2;
411
414
417
421 for(
i=0;
i<
a->ch_count;
i++){
422 a->ch[
i]=
a->data +
i*(
a->planar ? countb :
a->bps);
423 if(
a->count &&
a->planar) memcpy(
a->ch[
i], old.
ch[
i],
a->count*
a->bps);
424 }
425 if(
a->count && !
a->planar) memcpy(
a->ch[0], old.
ch[0],
a->count*
a->ch_count*
a->bps);
428
429 return 1;
430 }
431
433 int count){
438 int ch;
439 for(ch=0; ch<
out->ch_count; ch++)
440 memcpy(
out->ch[ch],
in->
ch[ch], count*
out->bps);
441 }else
443 }
444
446 {
448 if(!in_arg){
449 memset(
out->ch, 0,
sizeof(
out->ch));
450 }
else if(
out->planar){
451 for(
i=0;
i<
out->ch_count;
i++)
452 out->ch[
i]= in_arg[
i];
453 }else{
454 for(
i=0;
i<
out->ch_count;
i++)
456 }
457 }
458
462 for(
i=0;
i<
out->ch_count;
i++)
463 in_arg[
i]=
out->ch[
i];
464 }else{
465 in_arg[0]=
out->ch[0];
466 }
467 }
468
469 /**
470 *
471 * out may be equal in.
472 */
474 int ch;
476 for(ch=0; ch<
out->ch_count; ch++)
478 }else{
479 for(ch=
out->ch_count-1; ch>=0; ch--)
480 out->ch[ch]=
in->
ch[0] + (ch + count*
out->ch_count) *
out->bps;
481 }
482 }
483
484 /**
485 *
486 * @return number of samples output per channel
487 */
489 const AudioData * in_param,
int in_count){
491 int ret_sum=0;
492 int border=0;
494
498
501
502 border =
s->resampler->invert_initial_buffer(
s->resample, &
s->in_buffer,
503 &
in, in_count, &
s->in_buffer_index, &
s->in_buffer_count);
504 if (border == INT_MAX) {
505 return 0;
506 } else if (border < 0) {
507 return border;
508 } else if (border) {
510 in_count -= border;
511 s->resample_in_constraint = 0;
512 }
513
514 do{
516 if(!
s->resample_in_constraint &&
s->in_buffer_count){
518 ret=
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
tmp,
s->in_buffer_count, &consumed);
522 s->in_buffer_count -= consumed;
523 s->in_buffer_index += consumed;
524
525 if(!in_count)
526 break;
527 if(
s->in_buffer_count <= border){
529 in_count +=
s->in_buffer_count;
530 s->in_buffer_count=0;
531 s->in_buffer_index=0;
532 border = 0;
533 }
534 }
535
536 if((
s->flushed || in_count > padless) && !
s->in_buffer_count){
537 s->in_buffer_index=0;
538 ret=
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
in,
FFMAX(in_count-padless, 0), &consumed);
542 in_count -= consumed;
544 }
545
546 //TODO is this check sane considering the advanced copy avoidance below
547 size=
s->in_buffer_index +
s->in_buffer_count + in_count;
548 if(
size >
s->in_buffer.count
551 copy(&
s->in_buffer, &
tmp,
s->in_buffer_count);
552 s->in_buffer_index=0;
553 }else
556
557 if(in_count){
558 int count= in_count;
559 if(
s->in_buffer_count &&
s->in_buffer_count+2 < count && out_count) count=
s->in_buffer_count+2;
560
561 buf_set(&
tmp, &
s->in_buffer,
s->in_buffer_index +
s->in_buffer_count);
563 s->in_buffer_count += count;
564 in_count -= count;
565 border += count;
567 s->resample_in_constraint= 0;
568 if(
s->in_buffer_count != count || in_count)
569 continue;
570 if (padless) {
571 padless = 0;
572 continue;
573 }
574 }
575 break;
576 }while(1);
577
578 s->resample_in_constraint= !!out_count;
579
580 return ret_sum;
581 }
582
586 int ret /*, in_max*/;
588
592 return out_count;
593 }
594
595 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
596 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
597
600 if(
s->resample_first){
601 av_assert0(
s->midbuf.ch_count ==
s->used_ch_layout.nb_channels);
604 }else{
608 }
611
613
614 midbuf_tmp=
s->midbuf;
616 preout_tmp=
s->preout;
618
621
622 if(
s->resample_first ? !
s->resample : !
s->rematrix)
624
625 if(
s->resample_first ? !
s->rematrix : !
s->resample)
627
628 if(
s->int_sample_fmt ==
s->out_sample_fmt &&
s->out.planar
631 out_count=
FFMIN(out_count, in_count);
//TODO check at the end if this is needed or redundant
632 av_assert0(
s->in.planar);
//we only support planar internally so it has to be, we support copying non planar though
634 return out_count;
635 }
639 }
640
643 }
644
645 if(
s->resample_first){
648 return out_count;
651 }else{
656 return out_count;
657 }
658
661 if(
s->dither.method){
662 int ch;
663 int dither_count=
FFMAX(out_count, 1<<16);
664
666 conv_src = &
s->dither.temp;
669 }
670
674 for(ch=0; ch<
s->dither.noise.ch_count; ch++)
675 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)
678
679 if(
s->dither.noise_pos + out_count >
s->dither.noise.count)
680 s->dither.noise_pos = 0;
681
683 if (
s->mix_2_1_simd) {
684 int len1= out_count&~15;
686
687 if(len1)
689 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);
690 if(out_count != len1)
692 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);
693 } else {
695 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);
696 }
697 } else {
698 switch(
s->int_sample_fmt) {
703 }
704 }
705 s->dither.noise_pos += out_count;
706 }
707 //FIXME packed doesn't need more than 1 chan here!
709 }
710 return out_count;
711 }
712
714 return !!
s->in_buffer.ch_count;
715 }
716
718 uint8_t * const *out_arg, int out_count,
719 const uint8_t * const *in_arg, int in_count)
720 {
723
727 }
728 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
730 #endif
731
732 while(
s->drop_output > 0){
735 #define MAX_DROP_STEP 16384
738
740 s->drop_output *= -1;
//FIXME find a less hackish solution
741 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
742 s->drop_output *= -1;
743 in_count = 0;
745 s->drop_output -=
ret;
746 if (!
s->drop_output && !out_arg)
747 return 0;
748 continue;
749 }
750
752 return 0;
753 }
754
755 if(!in_arg){
758 s->resampler->flush(
s);
759 s->resample_in_constraint = 0;
761 }
else if(!
s->in_buffer_count){
762 return 0;
763 }
764 }else
766
768
771 if(
ret>0 && !
s->drop_output)
773
775
777 }else{
779 int ret2=0;
788 s->in_buffer_count -=
ret;
789 s->in_buffer_index +=
ret;
792 if(!
s->in_buffer_count)
793 s->in_buffer_index = 0;
794 }
795
796 if(in_count){
797 size=
s->in_buffer_index +
s->in_buffer_count + in_count - out_count;
798
799 if(in_count > out_count) { //FIXME move after swr_convert_internal
800 if(
size >
s->in_buffer.count
803 copy(&
s->in_buffer, &
tmp,
s->in_buffer_count);
804 s->in_buffer_index=0;
805 }else
808 }
809
810 if(out_count){
818 }
819 if(in_count){
820 buf_set(&
tmp, &
s->in_buffer,
s->in_buffer_index +
s->in_buffer_count);
822 s->in_buffer_count += in_count;
823 }
824 }
825 if(ret2>0 && !
s->drop_output)
826 s->outpts += ret2 * (
int64_t)
s->in_sample_rate;
827 av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
828 return ret2;
829 }
830 }
831
834 s->drop_output += count;
835
836 if(
s->drop_output <= 0)
837 return 0;
838
841 }
842
846
847 if(count <= 0)
848 return 0;
849
850 #define MAX_SILENCE_STEP 16384
855 }
856
859
860 if(
s->silence.planar)
for(
i=0;
i<
s->silence.ch_count;
i++) {
861 memset(
s->silence.ch[
i],
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps);
862 } else
863 memset(
s->silence.ch[0],
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps*
s->silence.ch_count);
864
869 }
870
872 if (
s->resampler &&
s->resample){
873 return s->resampler->get_delay(
s,
base);
874 }else{
875 return (
s->in_buffer_count*
base + (
s->in_sample_rate>>1))/
s->in_sample_rate;
876 }
877 }
878
880 {
882
883 if (in_samples < 0)
885
886 if (
s->resampler &&
s->resample) {
887 if (!
s->resampler->get_out_samples)
889 out_samples =
s->resampler->get_out_samples(
s, in_samples);
890 } else {
891 out_samples =
s->in_buffer_count + in_samples;
893 }
894
895 if (out_samples > INT_MAX)
897
898 return out_samples;
899 }
900
903
904 if (!
s || compensation_distance < 0)
906 if (!compensation_distance && sample_delta)
913 }
914 if (!
s->resampler->set_compensation){
916 }else{
917 return s->resampler->set_compensation(
s->resample, sample_delta, compensation_distance);
918 }
919 }
920
924
926 s->outpts =
s->firstpts =
pts;
927
928 if(
s->min_compensation >= FLT_MAX) {
930 } else {
932 double fdelta =
delta /(
double)(
s->in_sample_rate * (
int64_t)
s->out_sample_rate);
933
934 if(
fabs(fdelta) >
s->min_compensation) {
935 if(
s->outpts ==
s->firstpts ||
fabs(fdelta) >
s->min_hard_compensation){
941 }
942 }
else if(
s->soft_compensation_duration &&
s->max_soft_compensation) {
943 int duration =
s->out_sample_rate *
s->soft_compensation_duration;
948 }
949 }
950
952 }
953 }