2013-10-30 17:07:01 -07:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2013-12-02 21:24:38 -08:00
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
2013-10-30 17:07:01 -07:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "../util/bmem.h"
|
|
|
|
#include "audio-resampler.h"
|
2014-02-07 02:03:54 -08:00
|
|
|
#include "audio-io.h"
|
2014-02-15 04:44:37 -08:00
|
|
|
#include <libavutil/avutil.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2014-01-19 02:16:41 -08:00
|
|
|
#include <libswresample/swresample.h>
|
2013-10-30 17:07:01 -07:00
|
|
|
|
|
|
|
struct audio_resampler {
|
|
|
|
struct SwrContext *context;
|
|
|
|
bool opened;
|
|
|
|
|
|
|
|
uint32_t input_freq;
|
|
|
|
uint64_t input_layout;
|
|
|
|
enum AVSampleFormat input_format;
|
|
|
|
|
2014-02-14 14:13:36 -08:00
|
|
|
uint8_t *output_buffer[MAX_AV_PLANES];
|
2013-10-30 17:07:01 -07:00
|
|
|
uint64_t output_layout;
|
|
|
|
enum AVSampleFormat output_format;
|
|
|
|
int output_size;
|
|
|
|
uint32_t output_ch;
|
|
|
|
uint32_t output_freq;
|
2014-02-07 02:03:54 -08:00
|
|
|
uint32_t output_planes;
|
2013-10-30 17:07:01 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static inline enum AVSampleFormat convert_audio_format(enum audio_format format)
|
|
|
|
{
|
|
|
|
switch (format) {
|
2014-02-07 02:03:54 -08:00
|
|
|
case AUDIO_FORMAT_UNKNOWN: return AV_SAMPLE_FMT_S16;
|
|
|
|
case AUDIO_FORMAT_U8BIT: return AV_SAMPLE_FMT_U8;
|
|
|
|
case AUDIO_FORMAT_16BIT: return AV_SAMPLE_FMT_S16;
|
|
|
|
case AUDIO_FORMAT_32BIT: return AV_SAMPLE_FMT_S32;
|
|
|
|
case AUDIO_FORMAT_FLOAT: return AV_SAMPLE_FMT_FLT;
|
|
|
|
case AUDIO_FORMAT_U8BIT_PLANAR: return AV_SAMPLE_FMT_U8P;
|
|
|
|
case AUDIO_FORMAT_16BIT_PLANAR: return AV_SAMPLE_FMT_S16P;
|
|
|
|
case AUDIO_FORMAT_32BIT_PLANAR: return AV_SAMPLE_FMT_S32P;
|
|
|
|
case AUDIO_FORMAT_FLOAT_PLANAR: return AV_SAMPLE_FMT_FLTP;
|
2013-10-30 17:07:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* shouldn't get here */
|
|
|
|
return AV_SAMPLE_FMT_S16;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t convert_speaker_layout(enum speaker_layout layout)
|
|
|
|
{
|
|
|
|
switch (layout) {
|
|
|
|
case SPEAKERS_UNKNOWN: return 0;
|
|
|
|
case SPEAKERS_MONO: return AV_CH_LAYOUT_MONO;
|
|
|
|
case SPEAKERS_STEREO: return AV_CH_LAYOUT_STEREO;
|
|
|
|
case SPEAKERS_2POINT1: return AV_CH_LAYOUT_2_1;
|
|
|
|
case SPEAKERS_QUAD: return AV_CH_LAYOUT_QUAD;
|
|
|
|
case SPEAKERS_4POINT1: return AV_CH_LAYOUT_4POINT1;
|
|
|
|
case SPEAKERS_5POINT1: return AV_CH_LAYOUT_5POINT1;
|
|
|
|
case SPEAKERS_5POINT1_SURROUND: return AV_CH_LAYOUT_5POINT1_BACK;
|
|
|
|
case SPEAKERS_7POINT1: return AV_CH_LAYOUT_7POINT1;
|
|
|
|
case SPEAKERS_7POINT1_SURROUND: return AV_CH_LAYOUT_7POINT1_WIDE_BACK;
|
|
|
|
case SPEAKERS_SURROUND: return AV_CH_LAYOUT_SURROUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shouldn't get here */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
audio_resampler_t *audio_resampler_create(const struct resample_info *dst,
|
2014-02-09 04:51:06 -08:00
|
|
|
const struct resample_info *src)
|
2013-10-30 17:07:01 -07:00
|
|
|
{
|
2014-02-09 11:34:07 -08:00
|
|
|
struct audio_resampler *rs = bzalloc(sizeof(struct audio_resampler));
|
2013-10-30 17:07:01 -07:00
|
|
|
int errcode;
|
|
|
|
|
|
|
|
rs->opened = false;
|
|
|
|
rs->input_freq = src->samples_per_sec;
|
|
|
|
rs->input_layout = convert_speaker_layout(src->speakers);
|
|
|
|
rs->input_format = convert_audio_format(src->format);
|
|
|
|
rs->output_size = 0;
|
|
|
|
rs->output_ch = get_audio_channels(dst->speakers);
|
|
|
|
rs->output_freq = dst->samples_per_sec;
|
|
|
|
rs->output_layout = convert_speaker_layout(dst->speakers);
|
|
|
|
rs->output_format = convert_audio_format(dst->format);
|
2014-02-07 02:03:54 -08:00
|
|
|
rs->output_planes = is_audio_planar(dst->format) ? rs->output_ch : 1;
|
2013-10-30 17:07:01 -07:00
|
|
|
|
|
|
|
rs->context = swr_alloc_set_opts(NULL,
|
|
|
|
rs->output_layout, rs->output_format, dst->samples_per_sec,
|
|
|
|
rs->input_layout, rs->input_format, src->samples_per_sec,
|
|
|
|
0, NULL);
|
|
|
|
|
|
|
|
if (!rs->context) {
|
|
|
|
blog(LOG_ERROR, "swr_alloc_set_opts failed");
|
|
|
|
audio_resampler_destroy(rs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
errcode = swr_init(rs->context);
|
|
|
|
if (errcode != 0) {
|
|
|
|
blog(LOG_ERROR, "avresample_open failed: error code %d",
|
|
|
|
errcode);
|
|
|
|
audio_resampler_destroy(rs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rs;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void audio_resampler_destroy(audio_resampler_t *rs)
|
2013-10-30 17:07:01 -07:00
|
|
|
{
|
|
|
|
if (rs) {
|
|
|
|
if (rs->context)
|
|
|
|
swr_free(&rs->context);
|
|
|
|
if (rs->output_buffer)
|
2014-02-07 02:03:54 -08:00
|
|
|
av_freep(&rs->output_buffer[0]);
|
2013-10-30 17:07:01 -07:00
|
|
|
|
|
|
|
bfree(rs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
bool audio_resampler_resample(audio_resampler_t *rs,
|
2014-02-07 02:03:54 -08:00
|
|
|
uint8_t *output[], uint32_t *out_frames, uint64_t *ts_offset,
|
|
|
|
const uint8_t *const input[], uint32_t in_frames)
|
2013-10-30 17:07:01 -07:00
|
|
|
{
|
2014-02-23 21:39:33 -08:00
|
|
|
if (!rs) return false;
|
|
|
|
|
2013-10-30 17:07:01 -07:00
|
|
|
struct SwrContext *context = rs->context;
|
|
|
|
int ret;
|
2014-02-07 02:03:54 -08:00
|
|
|
|
2013-10-30 17:07:01 -07:00
|
|
|
int64_t delay = swr_get_delay(context, rs->input_freq);
|
|
|
|
int estimated = (int)av_rescale_rnd(
|
|
|
|
delay + (int64_t)in_frames,
|
|
|
|
(int64_t)rs->output_freq, (int64_t)rs->input_freq,
|
|
|
|
AV_ROUND_UP);
|
|
|
|
|
2014-02-07 02:03:54 -08:00
|
|
|
*ts_offset = (uint64_t)swr_get_delay(context, 1000000000);
|
2013-10-31 10:28:47 -07:00
|
|
|
|
2013-10-30 17:07:01 -07:00
|
|
|
/* resize the buffer if bigger */
|
|
|
|
if (estimated > rs->output_size) {
|
2014-02-07 02:03:54 -08:00
|
|
|
if (rs->output_buffer[0])
|
|
|
|
av_freep(&rs->output_buffer[0]);
|
|
|
|
|
|
|
|
av_samples_alloc(rs->output_buffer, NULL, rs->output_ch,
|
2013-10-30 17:07:01 -07:00
|
|
|
estimated, rs->output_format, 0);
|
|
|
|
|
|
|
|
rs->output_size = estimated;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = swr_convert(context,
|
2014-02-07 02:03:54 -08:00
|
|
|
rs->output_buffer, rs->output_size,
|
|
|
|
(const uint8_t**)input, in_frames);
|
2013-10-30 17:07:01 -07:00
|
|
|
|
|
|
|
if (ret < 0) {
|
|
|
|
blog(LOG_ERROR, "swr_convert failed: %d", ret);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:03:54 -08:00
|
|
|
for (uint32_t i = 0; i < rs->output_planes; i++)
|
|
|
|
output[i] = rs->output_buffer[i];
|
|
|
|
|
2013-10-30 17:07:01 -07:00
|
|
|
*out_frames = (uint32_t)ret;
|
|
|
|
return true;
|
|
|
|
}
|