summaryrefslogtreecommitdiff
path: root/libavresample/utils.c
diff options
context:
space:
mode:
authorLuca Barbato <lu_zero@gentoo.org>2014-05-25 17:49:15 +0200
committerLuca Barbato <lu_zero@gentoo.org>2014-08-10 14:26:37 +0200
commitfb1ddcdc8f51b9d261ae8e9c26b91e81f7b6bf45 (patch)
tree00f966d82d400e4326e70871723c066c6126ac04 /libavresample/utils.c
parent8c6f430291374cf6d2cfb85cdbb809803b5a7d83 (diff)
downloadffmpeg-fb1ddcdc8f51b9d261ae8e9c26b91e81f7b6bf45.tar.gz
avresample: Introduce AVFrame-based API
Diffstat (limited to 'libavresample/utils.c')
-rw-r--r--libavresample/utils.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/libavresample/utils.c b/libavresample/utils.c
index 851cd354d8..5d095c054b 100644
--- a/libavresample/utils.c
+++ b/libavresample/utils.c
@@ -21,6 +21,7 @@
#include "libavutil/common.h"
#include "libavutil/dict.h"
#include "libavutil/error.h"
+#include "libavutil/frame.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
@@ -500,6 +501,135 @@ int attribute_align_arg avresample_convert(AVAudioResampleContext *avr,
current_buffer);
}
+int avresample_config(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
+{
+ if (avresample_is_open(avr)) {
+ avresample_close(avr);
+ }
+
+ if (in) {
+ avr->in_channel_layout = in->channel_layout;
+ avr->in_sample_rate = in->sample_rate;
+ avr->in_sample_fmt = in->format;
+ }
+
+ if (out) {
+ avr->out_channel_layout = out->channel_layout;
+ avr->out_sample_rate = out->sample_rate;
+ avr->out_sample_fmt = out->format;
+ }
+
+ return 0;
+}
+
+static int config_changed(AVAudioResampleContext *avr,
+ AVFrame *out, AVFrame *in)
+{
+ int ret = 0;
+
+ if (in) {
+ if (avr->in_channel_layout != in->channel_layout ||
+ avr->in_sample_rate != in->sample_rate ||
+ avr->in_sample_fmt != in->format) {
+ ret |= AVERROR_INPUT_CHANGED;
+ }
+ }
+
+ if (out) {
+ if (avr->out_channel_layout != out->channel_layout ||
+ avr->out_sample_rate != out->sample_rate ||
+ avr->out_sample_fmt != out->format) {
+ ret |= AVERROR_OUTPUT_CHANGED;
+ }
+ }
+
+ return ret;
+}
+
+static inline int convert_frame(AVAudioResampleContext *avr,
+ AVFrame *out, AVFrame *in)
+{
+ int ret;
+ uint8_t **out_data = NULL, **in_data = NULL;
+ int out_linesize = 0, in_linesize = 0;
+ int out_nb_samples = 0, in_nb_samples = 0;
+
+ if (out) {
+ out_data = out->extended_data;
+ out_linesize = out->linesize[0];
+ out_nb_samples = out->nb_samples;
+ }
+
+ if (in) {
+ in_data = in->extended_data;
+ in_linesize = in->linesize[0];
+ in_nb_samples = in->nb_samples;
+ }
+
+ ret = avresample_convert(avr, out_data, out_linesize,
+ out_nb_samples,
+ in_data, in_linesize,
+ in_nb_samples);
+
+ if (ret < 0) {
+ if (out)
+ out->nb_samples = 0;
+ return ret;
+ }
+
+ if (out)
+ out->nb_samples = ret;
+
+ return 0;
+}
+
+static inline int available_samples(AVFrame *out)
+{
+ int bytes_per_sample = av_get_bytes_per_sample(out->format);
+ int samples = out->linesize[0] / bytes_per_sample;
+
+ if (av_sample_fmt_is_planar(out->format)) {
+ return samples;
+ } else {
+ int channels = av_get_channel_layout_nb_channels(out->channel_layout);
+ return samples / channels;
+ }
+}
+
+int avresample_convert_frame(AVAudioResampleContext *avr,
+ AVFrame *out, AVFrame *in)
+{
+ int ret, setup = 0;
+
+ if (!avresample_is_open(avr)) {
+ if ((ret = avresample_config(avr, out, in)) < 0)
+ return ret;
+ if ((ret = avresample_open(avr)) < 0)
+ return ret;
+ setup = 1;
+ } else {
+ // return as is or reconfigure for input changes?
+ if ((ret = config_changed(avr, out, in)))
+ return ret;
+ }
+
+ if (out) {
+ if (!out->linesize[0]) {
+ out->nb_samples = avresample_get_out_samples(avr, in->nb_samples);
+ if ((ret = av_frame_get_buffer(out, 0)) < 0) {
+ if (setup)
+ avresample_close(avr);
+ return ret;
+ }
+ } else {
+ if (!out->nb_samples)
+ out->nb_samples = available_samples(out);
+ }
+ }
+
+ return convert_frame(avr, out, in);
+}
+
int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
int stride)
{