Spaces:
Runtime error
Runtime error
File size: 7,211 Bytes
b86f76f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
/*
* Copyright (c) 2014 Luca Barbato <[email protected]>
* Copyright (c) 2014 Michael Niedermayer <[email protected]>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "swresample_internal.h"
#include "libavutil/channel_layout.h"
#include "libavutil/frame.h"
#include "libavutil/opt.h"
int swr_config_frame(SwrContext *s, const AVFrame *out, const AVFrame *in)
{
AVChannelLayout ch_layout = { 0 };
int ret;
swr_close(s);
if (in) {
#if FF_API_OLD_CHANNEL_LAYOUT
FF_DISABLE_DEPRECATION_WARNINGS
// if the old/new fields are set inconsistently, prefer the old ones
if ((in->channel_layout && (in->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
in->ch_layout.u.mask != in->channel_layout))) {
av_channel_layout_from_mask(&ch_layout, in->channel_layout);
FF_ENABLE_DEPRECATION_WARNINGS
} else
#endif
if ((ret = av_channel_layout_copy(&ch_layout, &in->ch_layout)) < 0)
goto fail;
if ((ret = av_opt_set_chlayout(s, "ichl", &ch_layout, 0)) < 0)
goto fail;
if ((ret = av_opt_set_int(s, "isf", in->format, 0)) < 0)
goto fail;
if ((ret = av_opt_set_int(s, "isr", in->sample_rate, 0)) < 0)
goto fail;
}
if (out) {
#if FF_API_OLD_CHANNEL_LAYOUT
FF_DISABLE_DEPRECATION_WARNINGS
// if the old/new fields are set inconsistently, prefer the old ones
if ((out->channel_layout && (out->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
out->ch_layout.u.mask != out->channel_layout))) {
av_channel_layout_uninit(&ch_layout);
av_channel_layout_from_mask(&ch_layout, out->channel_layout);
FF_ENABLE_DEPRECATION_WARNINGS
} else
#endif
if ((ret = av_channel_layout_copy(&ch_layout, &out->ch_layout)) < 0)
goto fail;
if ((ret = av_opt_set_chlayout(s, "ochl", &ch_layout, 0)) < 0)
goto fail;
if ((ret = av_opt_set_int(s, "osf", out->format, 0)) < 0)
goto fail;
if ((ret = av_opt_set_int(s, "osr", out->sample_rate, 0)) < 0)
goto fail;
}
ret = 0;
fail:
if (ret < 0)
av_log(s, AV_LOG_ERROR, "Failed to set option\n");
av_channel_layout_uninit(&ch_layout);
return ret;
}
static int config_changed(SwrContext *s,
const AVFrame *out, const AVFrame *in)
{
AVChannelLayout ch_layout = { 0 };
int ret = 0, err;
if (in) {
#if FF_API_OLD_CHANNEL_LAYOUT
FF_DISABLE_DEPRECATION_WARNINGS
// if the old/new fields are set inconsistently, prefer the old ones
if ((in->channel_layout && (in->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
in->ch_layout.u.mask != in->channel_layout))) {
av_channel_layout_from_mask(&ch_layout, in->channel_layout);
FF_ENABLE_DEPRECATION_WARNINGS
} else
#endif
if ((err = av_channel_layout_copy(&ch_layout, &in->ch_layout)) < 0)
return err;
if (av_channel_layout_compare(&s->in_ch_layout, &ch_layout) ||
s->in_sample_rate != in->sample_rate ||
s->in_sample_fmt != in->format) {
ret |= AVERROR_INPUT_CHANGED;
}
}
if (out) {
#if FF_API_OLD_CHANNEL_LAYOUT
FF_DISABLE_DEPRECATION_WARNINGS
// if the old/new fields are set inconsistently, prefer the old ones
if ((out->channel_layout && (out->ch_layout.order != AV_CHANNEL_ORDER_NATIVE ||
out->ch_layout.u.mask != out->channel_layout))) {
av_channel_layout_uninit(&ch_layout);
av_channel_layout_from_mask(&ch_layout, out->channel_layout);
FF_ENABLE_DEPRECATION_WARNINGS
} else
#endif
if ((err = av_channel_layout_copy(&ch_layout, &out->ch_layout)) < 0)
return err;
if (av_channel_layout_compare(&s->out_ch_layout, &ch_layout) ||
s->out_sample_rate != out->sample_rate ||
s->out_sample_fmt != out->format) {
ret |= AVERROR_OUTPUT_CHANGED;
}
}
av_channel_layout_uninit(&ch_layout);
return ret;
}
static inline int convert_frame(SwrContext *s,
AVFrame *out, const AVFrame *in)
{
int ret;
uint8_t **out_data = NULL;
const uint8_t **in_data = NULL;
int out_nb_samples = 0, in_nb_samples = 0;
if (out) {
out_data = out->extended_data;
out_nb_samples = out->nb_samples;
}
if (in) {
in_data = (const uint8_t **)in->extended_data;
in_nb_samples = in->nb_samples;
}
ret = swr_convert(s, out_data, out_nb_samples, in_data, 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;
#if FF_API_OLD_CHANNEL_LAYOUT
FF_DISABLE_DEPRECATION_WARNINGS
channels = av_get_channel_layout_nb_channels(out->channel_layout);
FF_ENABLE_DEPRECATION_WARNINGS
if (!channels)
#endif
channels = out->ch_layout.nb_channels;
return samples / channels;
}
}
int swr_convert_frame(SwrContext *s,
AVFrame *out, const AVFrame *in)
{
int ret, setup = 0;
if (!swr_is_initialized(s)) {
if ((ret = swr_config_frame(s, out, in)) < 0)
return ret;
if ((ret = swr_init(s)) < 0)
return ret;
setup = 1;
} else {
// return as is or reconfigure for input changes?
if ((ret = config_changed(s, out, in)))
return ret;
}
if (out) {
if (!out->linesize[0]) {
out->nb_samples = swr_get_delay(s, s->out_sample_rate) + 3;
if (in) {
out->nb_samples += in->nb_samples*(int64_t)s->out_sample_rate / s->in_sample_rate;
}
if ((ret = av_frame_get_buffer(out, 0)) < 0) {
if (setup)
swr_close(s);
return ret;
}
} else {
if (!out->nb_samples)
out->nb_samples = available_samples(out);
}
}
return convert_frame(s, out, in);
}
|