//TODO split options array out?
#define OFFSET(x) offsetof(SwrContext,x)
static const AVOption options[]={
-{"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
-{"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
+{"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
+{"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0},
{"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0},
{"isr", "input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
{"osr", "output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
.parent_log_context_offset = OFFSET(log_ctx),
};
-static int resample(SwrContext *s, AudioData *out_param, int out_count,
- const AudioData * in_param, int in_count);
+unsigned swresample_version(void)
+{
+ av_assert0(LIBSWRESAMPLE_VERSION_MICRO >= 100);
+ return LIBSWRESAMPLE_VERSION_INT;
+}
+
+const char *swresample_configuration(void)
+{
+ return FFMPEG_CONFIGURATION;
+}
+
+const char *swresample_license(void)
+{
+#define LICENSE_PREFIX "libswresample license: "
+ return LICENSE_PREFIX FFMPEG_LICENSE + sizeof(LICENSE_PREFIX) - 1;
+}
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
if(!s || s->in_convert) // s needs to be allocated but not initialized
av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_S16, 0);
av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0);
av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0);
+ av_opt_set_int(s, "uch", 0, 0);
return s;
}
swri_audio_convert_free(&s->out_convert);
swri_audio_convert_free(&s->full_convert);
- s-> in.planar= s-> in_sample_fmt >= 0x100;
- s->out.planar= s->out_sample_fmt >= 0x100;
- s-> in_sample_fmt &= 0xFF;
- s->out_sample_fmt &= 0xFF;
+ s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt);
+ s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt);
+ s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0);
+ s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0);
if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
- av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->in_sample_fmt));
+ av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
return AVERROR(EINVAL);
}
if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
- av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->out_sample_fmt));
+ av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
return AVERROR(EINVAL);
}
if(!s->out.ch_count)
s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
-av_assert0(s-> in.ch_count);
+ if(!s-> in.ch_count){
+ av_assert0(!s->in_ch_layout);
+ av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
+ return -1;
+ }
+
av_assert0(s->used_ch_count);
av_assert0(s->out.ch_count);
s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
}
}
+/**
+ *
+ * out may be equal in.
+ */
+static void buf_set(AudioData *out, AudioData *in, int count){
+ if(in->planar){
+ int ch;
+ for(ch=0; ch<out->ch_count; ch++)
+ out->ch[ch]= in->ch[ch] + count*out->bps;
+ }else
+ out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
+}
+
+/**
+ *
+ * @return number of samples output per channel
+ */
+static int resample(SwrContext *s, AudioData *out_param, int out_count,
+ const AudioData * in_param, int in_count){
+ AudioData in, out, tmp;
+ int ret_sum=0;
+ int border=0;
+
+ tmp=out=*out_param;
+ in = *in_param;
+
+ do{
+ int ret, size, consumed;
+ if(!s->resample_in_constraint && s->in_buffer_count){
+ buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
+ ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
+ out_count -= ret;
+ ret_sum += ret;
+ buf_set(&out, &out, ret);
+ s->in_buffer_count -= consumed;
+ s->in_buffer_index += consumed;
+
+ if(!in_count)
+ break;
+ if(s->in_buffer_count <= border){
+ buf_set(&in, &in, -s->in_buffer_count);
+ in_count += s->in_buffer_count;
+ s->in_buffer_count=0;
+ s->in_buffer_index=0;
+ border = 0;
+ }
+ }
+
+ if(in_count && !s->in_buffer_count){
+ s->in_buffer_index=0;
+ ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
+ out_count -= ret;
+ ret_sum += ret;
+ buf_set(&out, &out, ret);
+ in_count -= consumed;
+ buf_set(&in, &in, consumed);
+ }
+
+ //TODO is this check sane considering the advanced copy avoidance below
+ size= s->in_buffer_index + s->in_buffer_count + in_count;
+ if( size > s->in_buffer.count
+ && s->in_buffer_count + in_count <= s->in_buffer_index){
+ buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
+ copy(&s->in_buffer, &tmp, s->in_buffer_count);
+ s->in_buffer_index=0;
+ }else
+ if((ret=realloc_audio(&s->in_buffer, size)) < 0)
+ return ret;
+
+ if(in_count){
+ int count= in_count;
+ if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
+
+ buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
+ copy(&tmp, &in, /*in_*/count);
+ s->in_buffer_count += count;
+ in_count -= count;
+ border += count;
+ buf_set(&in, &in, count);
+ s->resample_in_constraint= 0;
+ if(s->in_buffer_count != count || in_count)
+ continue;
+ }
+ break;
+ }while(1);
+
+ s->resample_in_constraint= !!out_count;
+
+ return ret_sum;
+}
+
int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
const uint8_t *in_arg [SWR_CH_MAX], int in_count){
AudioData *postin, *midbuf, *preout;
return out_count;
}
-/**
- *
- * out may be equal in.
- */
-static void buf_set(AudioData *out, AudioData *in, int count){
- if(in->planar){
- int ch;
- for(ch=0; ch<out->ch_count; ch++)
- out->ch[ch]= in->ch[ch] + count*out->bps;
- }else
- out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
-}
-
-/**
- *
- * @return number of samples output per channel
- */
-static int resample(SwrContext *s, AudioData *out_param, int out_count,
- const AudioData * in_param, int in_count){
- AudioData in, out, tmp;
- int ret_sum=0;
- int border=0;
-
- tmp=out=*out_param;
- in = *in_param;
-
- do{
- int ret, size, consumed;
- if(!s->resample_in_constraint && s->in_buffer_count){
- buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
- ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
- out_count -= ret;
- ret_sum += ret;
- buf_set(&out, &out, ret);
- s->in_buffer_count -= consumed;
- s->in_buffer_index += consumed;
-
- if(!in_count)
- break;
- if(s->in_buffer_count <= border){
- buf_set(&in, &in, -s->in_buffer_count);
- in_count += s->in_buffer_count;
- s->in_buffer_count=0;
- s->in_buffer_index=0;
- border = 0;
- }
- }
-
- if(in_count && !s->in_buffer_count){
- s->in_buffer_index=0;
- ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
- out_count -= ret;
- ret_sum += ret;
- buf_set(&out, &out, ret);
- in_count -= consumed;
- buf_set(&in, &in, consumed);
- }
-
- //TODO is this check sane considering the advanced copy avoidance below
- size= s->in_buffer_index + s->in_buffer_count + in_count;
- if( size > s->in_buffer.count
- && s->in_buffer_count + in_count <= s->in_buffer_index){
- buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
- copy(&s->in_buffer, &tmp, s->in_buffer_count);
- s->in_buffer_index=0;
- }else
- if((ret=realloc_audio(&s->in_buffer, size)) < 0)
- return ret;
-
- if(in_count){
- int count= in_count;
- if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
-
- buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
- copy(&tmp, &in, /*in_*/count);
- s->in_buffer_count += count;
- in_count -= count;
- border += count;
- buf_set(&in, &in, count);
- s->resample_in_constraint= 0;
- if(s->in_buffer_count != count || in_count)
- continue;
- }
- break;
- }while(1);
-
- s->resample_in_constraint= !!out_count;
-
- return ret_sum;
-}